blob: 4ac6ef84f58007ca45987b4e3713dc8e501d9027 [file] [log] [blame]
Simon Glassc7a58902014-12-10 08:55:47 -07001/*
2 * Copyright (c) 2014 Google, Inc
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
Simon Glassc7a58902014-12-10 08:55:47 -070010#include <i2c.h>
11#include <malloc.h>
12#include <dm/device-internal.h>
13#include <dm/lists.h>
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +030014#include <dm/pinctrl.h>
15#ifdef CONFIG_DM_GPIO
16#include <asm/gpio.h>
17#endif
Simon Glassc7a58902014-12-10 08:55:47 -070018
Simon Glassc7a58902014-12-10 08:55:47 -070019#define I2C_MAX_OFFSET_LEN 4
20
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +030021enum {
22 PIN_SDA = 0,
23 PIN_SCL,
24 PIN_COUNT,
25};
26
Simon Glassa8e64c42015-07-02 18:15:39 -060027/* Useful debugging function */
28void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
29{
30 int i;
31
32 for (i = 0; i < nmsgs; i++) {
33 struct i2c_msg *m = &msg[i];
34
35 printf(" %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W",
36 msg->addr, msg->len);
37 if (!(m->flags & I2C_M_RD))
38 printf(": %x", m->buf[0]);
39 printf("\n");
40 }
41}
42
Simon Glassc7a58902014-12-10 08:55:47 -070043/**
44 * i2c_setup_offset() - Set up a new message with a chip offset
45 *
46 * @chip: Chip to use
47 * @offset: Byte offset within chip
48 * @offset_buf: Place to put byte offset
49 * @msg: Message buffer
50 * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
51 * message is still set up but will not contain an offset.
52 */
53static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
54 uint8_t offset_buf[], struct i2c_msg *msg)
55{
56 int offset_len;
57
58 msg->addr = chip->chip_addr;
59 msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
60 msg->len = chip->offset_len;
61 msg->buf = offset_buf;
62 if (!chip->offset_len)
63 return -EADDRNOTAVAIL;
64 assert(chip->offset_len <= I2C_MAX_OFFSET_LEN);
65 offset_len = chip->offset_len;
66 while (offset_len--)
67 *offset_buf++ = offset >> (8 * offset_len);
68
69 return 0;
70}
71
72static int i2c_read_bytewise(struct udevice *dev, uint offset,
73 uint8_t *buffer, int len)
74{
Simon Glass713c3f02015-01-25 08:27:13 -070075 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -070076 struct udevice *bus = dev_get_parent(dev);
77 struct dm_i2c_ops *ops = i2c_get_ops(bus);
78 struct i2c_msg msg[2], *ptr;
79 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
80 int ret;
81 int i;
82
83 for (i = 0; i < len; i++) {
84 if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
85 return -EINVAL;
86 ptr = msg + 1;
87 ptr->addr = chip->chip_addr;
88 ptr->flags = msg->flags | I2C_M_RD;
89 ptr->len = 1;
90 ptr->buf = &buffer[i];
91 ptr++;
92
93 ret = ops->xfer(bus, msg, ptr - msg);
94 if (ret)
95 return ret;
96 }
97
98 return 0;
99}
100
101static int i2c_write_bytewise(struct udevice *dev, uint offset,
102 const uint8_t *buffer, int len)
103{
Simon Glass713c3f02015-01-25 08:27:13 -0700104 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700105 struct udevice *bus = dev_get_parent(dev);
106 struct dm_i2c_ops *ops = i2c_get_ops(bus);
107 struct i2c_msg msg[1];
108 uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
109 int ret;
110 int i;
111
112 for (i = 0; i < len; i++) {
113 if (i2c_setup_offset(chip, offset + i, buf, msg))
114 return -EINVAL;
115 buf[msg->len++] = buffer[i];
116
117 ret = ops->xfer(bus, msg, 1);
118 if (ret)
119 return ret;
120 }
121
122 return 0;
123}
124
Simon Glass7d722762015-01-12 18:02:07 -0700125int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
Simon Glassc7a58902014-12-10 08:55:47 -0700126{
Simon Glass713c3f02015-01-25 08:27:13 -0700127 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700128 struct udevice *bus = dev_get_parent(dev);
129 struct dm_i2c_ops *ops = i2c_get_ops(bus);
130 struct i2c_msg msg[2], *ptr;
131 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
132 int msg_count;
133
134 if (!ops->xfer)
135 return -ENOSYS;
136 if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
137 return i2c_read_bytewise(dev, offset, buffer, len);
138 ptr = msg;
139 if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
140 ptr++;
141
142 if (len) {
143 ptr->addr = chip->chip_addr;
144 ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
145 ptr->flags |= I2C_M_RD;
146 ptr->len = len;
147 ptr->buf = buffer;
148 ptr++;
149 }
150 msg_count = ptr - msg;
151
152 return ops->xfer(bus, msg, msg_count);
153}
154
Simon Glass7d722762015-01-12 18:02:07 -0700155int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
156 int len)
Simon Glassc7a58902014-12-10 08:55:47 -0700157{
Simon Glass713c3f02015-01-25 08:27:13 -0700158 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700159 struct udevice *bus = dev_get_parent(dev);
160 struct dm_i2c_ops *ops = i2c_get_ops(bus);
161 struct i2c_msg msg[1];
162
163 if (!ops->xfer)
164 return -ENOSYS;
165
166 if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
167 return i2c_write_bytewise(dev, offset, buffer, len);
168 /*
169 * The simple approach would be to send two messages here: one to
170 * set the offset and one to write the bytes. However some drivers
171 * will not be expecting this, and some chips won't like how the
172 * driver presents this on the I2C bus.
173 *
174 * The API does not support separate offset and data. We could extend
175 * it with a flag indicating that there is data in the next message
176 * that needs to be processed in the same transaction. We could
177 * instead add an additional buffer to each message. For now, handle
178 * this in the uclass since it isn't clear what the impact on drivers
179 * would be with this extra complication. Unfortunately this means
180 * copying the message.
181 *
182 * Use the stack for small messages, malloc() for larger ones. We
183 * need to allow space for the offset (up to 4 bytes) and the message
184 * itself.
185 */
186 if (len < 64) {
187 uint8_t buf[I2C_MAX_OFFSET_LEN + len];
188
189 i2c_setup_offset(chip, offset, buf, msg);
190 msg->len += len;
191 memcpy(buf + chip->offset_len, buffer, len);
192
193 return ops->xfer(bus, msg, 1);
194 } else {
195 uint8_t *buf;
196 int ret;
197
198 buf = malloc(I2C_MAX_OFFSET_LEN + len);
199 if (!buf)
200 return -ENOMEM;
201 i2c_setup_offset(chip, offset, buf, msg);
202 msg->len += len;
203 memcpy(buf + chip->offset_len, buffer, len);
204
205 ret = ops->xfer(bus, msg, 1);
206 free(buf);
207 return ret;
208 }
209}
210
Simon Glassf2818852015-07-02 18:15:42 -0600211int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
212{
213 struct udevice *bus = dev_get_parent(dev);
214 struct dm_i2c_ops *ops = i2c_get_ops(bus);
215
216 if (!ops->xfer)
217 return -ENOSYS;
218
219 return ops->xfer(bus, msg, nmsgs);
220}
221
Simon Glass0c702612015-04-20 12:37:14 -0600222int dm_i2c_reg_read(struct udevice *dev, uint offset)
223{
224 uint8_t val;
225 int ret;
226
227 ret = dm_i2c_read(dev, offset, &val, 1);
228 if (ret < 0)
229 return ret;
230
231 return val;
232}
233
234int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
235{
236 uint8_t val = value;
237
238 return dm_i2c_write(dev, offset, &val, 1);
239}
240
Simon Glassc7a58902014-12-10 08:55:47 -0700241/**
242 * i2c_probe_chip() - probe for a chip on a bus
243 *
244 * @bus: Bus to probe
245 * @chip_addr: Chip address to probe
246 * @flags: Flags for the chip
247 * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
248 * does not respond to probe
249 */
250static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
251 enum dm_i2c_chip_flags chip_flags)
252{
253 struct dm_i2c_ops *ops = i2c_get_ops(bus);
254 struct i2c_msg msg[1];
255 int ret;
256
257 if (ops->probe_chip) {
258 ret = ops->probe_chip(bus, chip_addr, chip_flags);
259 if (!ret || ret != -ENOSYS)
260 return ret;
261 }
262
263 if (!ops->xfer)
264 return -ENOSYS;
265
266 /* Probe with a zero-length message */
267 msg->addr = chip_addr;
268 msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
269 msg->len = 0;
270 msg->buf = NULL;
271
272 return ops->xfer(bus, msg, 1);
273}
274
Simon Glassa2723ae2015-01-25 08:26:55 -0700275static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
Simon Glassc7a58902014-12-10 08:55:47 -0700276 struct udevice **devp)
277{
Simon Glass713c3f02015-01-25 08:27:13 -0700278 struct dm_i2c_chip *chip;
Simon Glassc7a58902014-12-10 08:55:47 -0700279 char name[30], *str;
280 struct udevice *dev;
281 int ret;
282
283 snprintf(name, sizeof(name), "generic_%x", chip_addr);
284 str = strdup(name);
Simon Glassb75c4492015-02-18 14:10:28 -0700285 if (!str)
286 return -ENOMEM;
Simon Glassc7a58902014-12-10 08:55:47 -0700287 ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
288 debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
289 if (ret)
290 goto err_bind;
291
292 /* Tell the device what we know about it */
Simon Glass713c3f02015-01-25 08:27:13 -0700293 chip = dev_get_parent_platdata(dev);
294 chip->chip_addr = chip_addr;
295 chip->offset_len = offset_len;
296 ret = device_probe(dev);
297 debug("%s: device_probe: ret=%d\n", __func__, ret);
Simon Glassc7a58902014-12-10 08:55:47 -0700298 if (ret)
299 goto err_probe;
300
301 *devp = dev;
302 return 0;
303
304err_probe:
Simon Glass713c3f02015-01-25 08:27:13 -0700305 /*
306 * If the device failed to probe, unbind it. There is nothing there
307 * on the bus so we don't want to leave it lying around
308 */
Simon Glassc7a58902014-12-10 08:55:47 -0700309 device_unbind(dev);
310err_bind:
311 free(str);
312 return ret;
313}
314
Simon Glassa2723ae2015-01-25 08:26:55 -0700315int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
316 struct udevice **devp)
Simon Glassc7a58902014-12-10 08:55:47 -0700317{
318 struct udevice *dev;
319
320 debug("%s: Searching bus '%s' for address %02x: ", __func__,
321 bus->name, chip_addr);
322 for (device_find_first_child(bus, &dev); dev;
323 device_find_next_child(&dev)) {
Simon Glass713c3f02015-01-25 08:27:13 -0700324 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700325 int ret;
326
Simon Glassc7a58902014-12-10 08:55:47 -0700327 if (chip->chip_addr == chip_addr) {
328 ret = device_probe(dev);
329 debug("found, ret=%d\n", ret);
330 if (ret)
331 return ret;
332 *devp = dev;
333 return 0;
334 }
335 }
336 debug("not found\n");
Simon Glassa2723ae2015-01-25 08:26:55 -0700337 return i2c_bind_driver(bus, chip_addr, offset_len, devp);
Simon Glassc7a58902014-12-10 08:55:47 -0700338}
339
Simon Glassa2723ae2015-01-25 08:26:55 -0700340int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
341 struct udevice **devp)
Simon Glassc7a58902014-12-10 08:55:47 -0700342{
343 struct udevice *bus;
344 int ret;
345
346 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
347 if (ret) {
348 debug("Cannot find I2C bus %d\n", busnum);
349 return ret;
350 }
Simon Glassa2723ae2015-01-25 08:26:55 -0700351 ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
Simon Glassc7a58902014-12-10 08:55:47 -0700352 if (ret) {
353 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
354 busnum);
355 return ret;
356 }
357
358 return 0;
359}
360
Simon Glass7d722762015-01-12 18:02:07 -0700361int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
362 struct udevice **devp)
Simon Glassc7a58902014-12-10 08:55:47 -0700363{
364 int ret;
365
366 *devp = NULL;
367
368 /* First probe that chip */
369 ret = i2c_probe_chip(bus, chip_addr, chip_flags);
370 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
371 chip_addr, ret);
372 if (ret)
373 return ret;
374
375 /* The chip was found, see if we have a driver, and probe it */
Simon Glassa2723ae2015-01-25 08:26:55 -0700376 ret = i2c_get_chip(bus, chip_addr, 1, devp);
Simon Glassc7a58902014-12-10 08:55:47 -0700377 debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
378
379 return ret;
380}
381
Simon Glasse4e8ff22015-02-05 21:41:32 -0700382int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
Simon Glassc7a58902014-12-10 08:55:47 -0700383{
384 struct dm_i2c_ops *ops = i2c_get_ops(bus);
Simon Glassde0977b2015-03-05 12:25:20 -0700385 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glassc7a58902014-12-10 08:55:47 -0700386 int ret;
387
388 /*
389 * If we have a method, call it. If not then the driver probably wants
390 * to deal with speed changes on the next transfer. It can easily read
391 * the current speed from this uclass
392 */
393 if (ops->set_bus_speed) {
394 ret = ops->set_bus_speed(bus, speed);
395 if (ret)
396 return ret;
397 }
398 i2c->speed_hz = speed;
399
400 return 0;
401}
402
Simon Glasse4e8ff22015-02-05 21:41:32 -0700403int dm_i2c_get_bus_speed(struct udevice *bus)
Simon Glassc7a58902014-12-10 08:55:47 -0700404{
405 struct dm_i2c_ops *ops = i2c_get_ops(bus);
Simon Glassde0977b2015-03-05 12:25:20 -0700406 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glassc7a58902014-12-10 08:55:47 -0700407
408 if (!ops->get_bus_speed)
409 return i2c->speed_hz;
410
411 return ops->get_bus_speed(bus);
412}
413
414int i2c_set_chip_flags(struct udevice *dev, uint flags)
415{
416 struct udevice *bus = dev->parent;
Simon Glass713c3f02015-01-25 08:27:13 -0700417 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700418 struct dm_i2c_ops *ops = i2c_get_ops(bus);
419 int ret;
420
421 if (ops->set_flags) {
422 ret = ops->set_flags(dev, flags);
423 if (ret)
424 return ret;
425 }
426 chip->flags = flags;
427
428 return 0;
429}
430
431int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
432{
Simon Glass713c3f02015-01-25 08:27:13 -0700433 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700434
435 *flagsp = chip->flags;
436
437 return 0;
438}
439
440int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
441{
Simon Glass713c3f02015-01-25 08:27:13 -0700442 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700443
444 if (offset_len > I2C_MAX_OFFSET_LEN)
445 return -EINVAL;
446 chip->offset_len = offset_len;
447
448 return 0;
449}
450
Simon Glass9a1589a2015-05-04 11:30:58 -0600451int i2c_get_chip_offset_len(struct udevice *dev)
452{
453 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
454
455 return chip->offset_len;
456}
457
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300458#ifdef CONFIG_DM_GPIO
459static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
460{
461 if (bit)
462 dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
463 else
464 dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
465 GPIOD_ACTIVE_LOW |
466 GPIOD_IS_OUT_ACTIVE);
467}
468
469static int i2c_gpio_get_pin(struct gpio_desc *pin)
470{
471 return dm_gpio_get_value(pin);
472}
473
474static int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
475 struct gpio_desc *scl_pin)
476{
477 int counter = 9;
478 int ret = 0;
479
480 i2c_gpio_set_pin(sda_pin, 1);
481 i2c_gpio_set_pin(scl_pin, 1);
482 udelay(5);
483
484 /* Toggle SCL until slave release SDA */
485 while (counter-- >= 0) {
486 i2c_gpio_set_pin(scl_pin, 1);
487 udelay(5);
488 i2c_gpio_set_pin(scl_pin, 0);
489 udelay(5);
490 if (i2c_gpio_get_pin(sda_pin))
491 break;
492 }
493
494 /* Then, send I2C stop */
495 i2c_gpio_set_pin(sda_pin, 0);
496 udelay(5);
497
498 i2c_gpio_set_pin(scl_pin, 1);
499 udelay(5);
500
501 i2c_gpio_set_pin(sda_pin, 1);
502 udelay(5);
503
504 if (!i2c_gpio_get_pin(sda_pin) || !i2c_gpio_get_pin(scl_pin))
505 ret = -EREMOTEIO;
506
507 return ret;
508}
509
510static int i2c_deblock_gpio(struct udevice *bus)
511{
512 struct gpio_desc gpios[PIN_COUNT];
513 int ret, ret0;
514
515 ret = gpio_request_list_by_name(bus, "gpios", gpios,
516 ARRAY_SIZE(gpios), GPIOD_IS_IN);
517 if (ret != ARRAY_SIZE(gpios)) {
518 debug("%s: I2C Node '%s' has no 'gpios' property %s\n",
519 __func__, dev_read_name(bus), bus->name);
520 if (ret >= 0) {
521 gpio_free_list(bus, gpios, ret);
522 ret = -ENOENT;
523 }
524 goto out;
525 }
526
527 ret = pinctrl_select_state(bus, "gpio");
528 if (ret) {
529 debug("%s: I2C Node '%s' has no 'gpio' pinctrl state. %s\n",
530 __func__, dev_read_name(bus), bus->name);
531 goto out_no_pinctrl;
532 }
533
534 ret0 = i2c_deblock_gpio_loop(&gpios[PIN_SDA], &gpios[PIN_SCL]);
535
536 ret = pinctrl_select_state(bus, "default");
537 if (ret) {
538 debug("%s: I2C Node '%s' has no 'default' pinctrl state. %s\n",
539 __func__, dev_read_name(bus), bus->name);
540 }
541
542 ret = !ret ? ret0 : ret;
543
544out_no_pinctrl:
545 gpio_free_list(bus, gpios, ARRAY_SIZE(gpios));
546out:
547 return ret;
548}
549#else
550static int i2c_deblock_gpio(struct udevice *bus)
551{
552 return -ENOSYS;
553}
554#endif // CONFIG_DM_GPIO
555
Simon Glassc7a58902014-12-10 08:55:47 -0700556int i2c_deblock(struct udevice *bus)
557{
558 struct dm_i2c_ops *ops = i2c_get_ops(bus);
559
Simon Glassc7a58902014-12-10 08:55:47 -0700560 if (!ops->deblock)
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300561 return i2c_deblock_gpio(bus);
Simon Glassc7a58902014-12-10 08:55:47 -0700562
563 return ops->deblock(bus);
564}
565
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530566#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass654e3a02017-05-18 20:09:30 -0600567int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip)
Simon Glassc7a58902014-12-10 08:55:47 -0700568{
Simon Glass654e3a02017-05-18 20:09:30 -0600569 int addr;
570
571 chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len",
572 1);
Simon Glassc7a58902014-12-10 08:55:47 -0700573 chip->flags = 0;
Simon Glass654e3a02017-05-18 20:09:30 -0600574 addr = dev_read_u32_default(dev, "reg", -1);
575 if (addr == -1) {
576 debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__,
577 dev_read_name(dev), dev->name);
Simon Glassc7a58902014-12-10 08:55:47 -0700578 return -EINVAL;
579 }
Simon Glass654e3a02017-05-18 20:09:30 -0600580 chip->chip_addr = addr;
Simon Glassc7a58902014-12-10 08:55:47 -0700581
582 return 0;
583}
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530584#endif
Simon Glassc7a58902014-12-10 08:55:47 -0700585
586static int i2c_post_probe(struct udevice *dev)
587{
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530588#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glassde0977b2015-03-05 12:25:20 -0700589 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700590
Simon Glass654e3a02017-05-18 20:09:30 -0600591 i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency", 100000);
Simon Glassc7a58902014-12-10 08:55:47 -0700592
Simon Glasse4e8ff22015-02-05 21:41:32 -0700593 return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530594#else
595 return 0;
596#endif
Simon Glassc7a58902014-12-10 08:55:47 -0700597}
598
Simon Glass713c3f02015-01-25 08:27:13 -0700599static int i2c_child_post_bind(struct udevice *dev)
600{
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530601#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass713c3f02015-01-25 08:27:13 -0700602 struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
603
Simon Glass654e3a02017-05-18 20:09:30 -0600604 if (!dev_of_valid(dev))
Simon Glass713c3f02015-01-25 08:27:13 -0700605 return 0;
Simon Glass654e3a02017-05-18 20:09:30 -0600606 return i2c_chip_ofdata_to_platdata(dev, plat);
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530607#else
608 return 0;
609#endif
Simon Glass713c3f02015-01-25 08:27:13 -0700610}
611
Simon Glassc7a58902014-12-10 08:55:47 -0700612UCLASS_DRIVER(i2c) = {
613 .id = UCLASS_I2C,
614 .name = "i2c",
Simon Glass0ccb0972015-01-25 08:27:05 -0700615 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass18230342016-07-05 17:10:10 -0600616#if CONFIG_IS_ENABLED(OF_CONTROL)
617 .post_bind = dm_scan_fdt_dev,
618#endif
Simon Glassc7a58902014-12-10 08:55:47 -0700619 .post_probe = i2c_post_probe,
Simon Glass713c3f02015-01-25 08:27:13 -0700620 .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
621 .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
622 .child_post_bind = i2c_child_post_bind,
Simon Glassc7a58902014-12-10 08:55:47 -0700623};
624
625UCLASS_DRIVER(i2c_generic) = {
626 .id = UCLASS_I2C_GENERIC,
627 .name = "i2c_generic",
628};
629
630U_BOOT_DRIVER(i2c_generic_chip_drv) = {
631 .name = "i2c_generic_chip_drv",
632 .id = UCLASS_I2C_GENERIC,
633};