32aed921b70a9479d51624b31e30518aa54d0d5d
[oweals/u-boot.git] / drivers / i2c / i2c-uclass.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2014 Google, Inc
4  */
5
6 #include <common.h>
7 #include <dm.h>
8 #include <errno.h>
9 #include <i2c.h>
10 #include <log.h>
11 #include <malloc.h>
12 #include <dm/device-internal.h>
13 #include <dm/lists.h>
14 #include <dm/pinctrl.h>
15 #if CONFIG_IS_ENABLED(DM_GPIO)
16 #include <asm/gpio.h>
17 #endif
18
19 #define I2C_MAX_OFFSET_LEN      4
20
21 enum {
22         PIN_SDA = 0,
23         PIN_SCL,
24         PIN_COUNT,
25 };
26
27 /* Useful debugging function */
28 void 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
43 /**
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  */
53 static 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 = chip->offset_len;
57
58         msg->addr = chip->chip_addr;
59         if (chip->chip_addr_offset_mask)
60                 msg->addr |= (offset >> (8 * offset_len)) &
61                         chip->chip_addr_offset_mask;
62         msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
63         msg->len = chip->offset_len;
64         msg->buf = offset_buf;
65         if (!offset_len)
66                 return -EADDRNOTAVAIL;
67         assert(offset_len <= I2C_MAX_OFFSET_LEN);
68
69         while (offset_len--)
70                 *offset_buf++ = offset >> (8 * offset_len);
71
72         return 0;
73 }
74
75 static int i2c_read_bytewise(struct udevice *dev, uint offset,
76                              uint8_t *buffer, int len)
77 {
78         struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
79         struct udevice *bus = dev_get_parent(dev);
80         struct dm_i2c_ops *ops = i2c_get_ops(bus);
81         struct i2c_msg msg[2], *ptr;
82         uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
83         int ret;
84         int i;
85
86         for (i = 0; i < len; i++) {
87                 if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
88                         return -EINVAL;
89                 ptr = msg + 1;
90                 ptr->addr = msg->addr;
91                 ptr->flags = msg->flags | I2C_M_RD;
92                 ptr->len = 1;
93                 ptr->buf = &buffer[i];
94                 ptr++;
95
96                 ret = ops->xfer(bus, msg, ptr - msg);
97                 if (ret)
98                         return ret;
99         }
100
101         return 0;
102 }
103
104 static int i2c_write_bytewise(struct udevice *dev, uint offset,
105                              const uint8_t *buffer, int len)
106 {
107         struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
108         struct udevice *bus = dev_get_parent(dev);
109         struct dm_i2c_ops *ops = i2c_get_ops(bus);
110         struct i2c_msg msg[1];
111         uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
112         int ret;
113         int i;
114
115         for (i = 0; i < len; i++) {
116                 if (i2c_setup_offset(chip, offset + i, buf, msg))
117                         return -EINVAL;
118                 buf[msg->len++] = buffer[i];
119
120                 ret = ops->xfer(bus, msg, 1);
121                 if (ret)
122                         return ret;
123         }
124
125         return 0;
126 }
127
128 int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
129 {
130         struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
131         struct udevice *bus = dev_get_parent(dev);
132         struct dm_i2c_ops *ops = i2c_get_ops(bus);
133         struct i2c_msg msg[2], *ptr;
134         uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
135         int msg_count;
136
137         if (!ops->xfer)
138                 return -ENOSYS;
139         if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
140                 return i2c_read_bytewise(dev, offset, buffer, len);
141         ptr = msg;
142         if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
143                 ptr++;
144
145         if (len) {
146                 ptr->addr = msg->addr;
147                 ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
148                 ptr->flags |= I2C_M_RD;
149                 ptr->len = len;
150                 ptr->buf = buffer;
151                 ptr++;
152         }
153         msg_count = ptr - msg;
154
155         return ops->xfer(bus, msg, msg_count);
156 }
157
158 int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
159                  int len)
160 {
161         struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
162         struct udevice *bus = dev_get_parent(dev);
163         struct dm_i2c_ops *ops = i2c_get_ops(bus);
164         struct i2c_msg msg[1];
165
166         if (!ops->xfer)
167                 return -ENOSYS;
168
169         if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
170                 return i2c_write_bytewise(dev, offset, buffer, len);
171         /*
172          * The simple approach would be to send two messages here: one to
173          * set the offset and one to write the bytes. However some drivers
174          * will not be expecting this, and some chips won't like how the
175          * driver presents this on the I2C bus.
176          *
177          * The API does not support separate offset and data. We could extend
178          * it with a flag indicating that there is data in the next message
179          * that needs to be processed in the same transaction. We could
180          * instead add an additional buffer to each message. For now, handle
181          * this in the uclass since it isn't clear what the impact on drivers
182          * would be with this extra complication. Unfortunately this means
183          * copying the message.
184          *
185          * Use the stack for small messages, malloc() for larger ones. We
186          * need to allow space for the offset (up to 4 bytes) and the message
187          * itself.
188          */
189         if (len < 64) {
190                 uint8_t buf[I2C_MAX_OFFSET_LEN + len];
191
192                 i2c_setup_offset(chip, offset, buf, msg);
193                 msg->len += len;
194                 memcpy(buf + chip->offset_len, buffer, len);
195
196                 return ops->xfer(bus, msg, 1);
197         } else {
198                 uint8_t *buf;
199                 int ret;
200
201                 buf = malloc(I2C_MAX_OFFSET_LEN + len);
202                 if (!buf)
203                         return -ENOMEM;
204                 i2c_setup_offset(chip, offset, buf, msg);
205                 msg->len += len;
206                 memcpy(buf + chip->offset_len, buffer, len);
207
208                 ret = ops->xfer(bus, msg, 1);
209                 free(buf);
210                 return ret;
211         }
212 }
213
214 int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
215 {
216         struct udevice *bus = dev_get_parent(dev);
217         struct dm_i2c_ops *ops = i2c_get_ops(bus);
218
219         if (!ops->xfer)
220                 return -ENOSYS;
221
222         return ops->xfer(bus, msg, nmsgs);
223 }
224
225 int dm_i2c_reg_read(struct udevice *dev, uint offset)
226 {
227         uint8_t val;
228         int ret;
229
230         ret = dm_i2c_read(dev, offset, &val, 1);
231         if (ret < 0)
232                 return ret;
233
234         return val;
235 }
236
237 int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
238 {
239         uint8_t val = value;
240
241         return dm_i2c_write(dev, offset, &val, 1);
242 }
243
244 /**
245  * i2c_probe_chip() - probe for a chip on a bus
246  *
247  * @bus:        Bus to probe
248  * @chip_addr:  Chip address to probe
249  * @flags:      Flags for the chip
250  * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
251  * does not respond to probe
252  */
253 static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
254                           enum dm_i2c_chip_flags chip_flags)
255 {
256         struct dm_i2c_ops *ops = i2c_get_ops(bus);
257         struct i2c_msg msg[1];
258         int ret;
259
260         if (ops->probe_chip) {
261                 ret = ops->probe_chip(bus, chip_addr, chip_flags);
262                 if (!ret || ret != -ENOSYS)
263                         return ret;
264         }
265
266         if (!ops->xfer)
267                 return -ENOSYS;
268
269         /* Probe with a zero-length message */
270         msg->addr = chip_addr;
271         msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
272         msg->len = 0;
273         msg->buf = NULL;
274
275         return ops->xfer(bus, msg, 1);
276 }
277
278 static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
279                            struct udevice **devp)
280 {
281         struct dm_i2c_chip *chip;
282         char name[30], *str;
283         struct udevice *dev;
284         int ret;
285
286         snprintf(name, sizeof(name), "generic_%x", chip_addr);
287         str = strdup(name);
288         if (!str)
289                 return -ENOMEM;
290         ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
291         debug("%s:  device_bind_driver: ret=%d\n", __func__, ret);
292         if (ret)
293                 goto err_bind;
294
295         /* Tell the device what we know about it */
296         chip = dev_get_parent_platdata(dev);
297         chip->chip_addr = chip_addr;
298         chip->offset_len = offset_len;
299         ret = device_probe(dev);
300         debug("%s:  device_probe: ret=%d\n", __func__, ret);
301         if (ret)
302                 goto err_probe;
303
304         *devp = dev;
305         return 0;
306
307 err_probe:
308         /*
309          * If the device failed to probe, unbind it. There is nothing there
310          * on the bus so we don't want to leave it lying around
311          */
312         device_unbind(dev);
313 err_bind:
314         free(str);
315         return ret;
316 }
317
318 int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
319                  struct udevice **devp)
320 {
321         struct udevice *dev;
322
323         debug("%s: Searching bus '%s' for address %02x: ", __func__,
324               bus->name, chip_addr);
325         for (device_find_first_child(bus, &dev); dev;
326                         device_find_next_child(&dev)) {
327                 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
328                 int ret;
329
330                 if (chip->chip_addr == (chip_addr &
331                                         ~chip->chip_addr_offset_mask)) {
332                         ret = device_probe(dev);
333                         debug("found, ret=%d\n", ret);
334                         if (ret)
335                                 return ret;
336                         *devp = dev;
337                         return 0;
338                 }
339         }
340         debug("not found\n");
341         return i2c_bind_driver(bus, chip_addr, offset_len, devp);
342 }
343
344 int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
345                             struct udevice **devp)
346 {
347         struct udevice *bus;
348         int ret;
349
350         ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
351         if (ret) {
352                 debug("Cannot find I2C bus %d\n", busnum);
353                 return ret;
354         }
355
356         /* detect the presence of the chip on the bus */
357         ret = i2c_probe_chip(bus, chip_addr, 0);
358         debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
359               chip_addr, ret);
360         if (ret) {
361                 debug("Cannot detect I2C chip %02x on bus %d\n", chip_addr,
362                       busnum);
363                 return ret;
364         }
365
366         ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
367         if (ret) {
368                 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
369                       busnum);
370                 return ret;
371         }
372
373         return 0;
374 }
375
376 int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
377                  struct udevice **devp)
378 {
379         int ret;
380
381         *devp = NULL;
382
383         /* First probe that chip */
384         ret = i2c_probe_chip(bus, chip_addr, chip_flags);
385         debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
386               chip_addr, ret);
387         if (ret)
388                 return ret;
389
390         /* The chip was found, see if we have a driver, and probe it */
391         ret = i2c_get_chip(bus, chip_addr, 1, devp);
392         debug("%s:  i2c_get_chip: ret=%d\n", __func__, ret);
393
394         return ret;
395 }
396
397 int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
398 {
399         struct dm_i2c_ops *ops = i2c_get_ops(bus);
400         struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
401         int ret;
402
403         /*
404          * If we have a method, call it. If not then the driver probably wants
405          * to deal with speed changes on the next transfer. It can easily read
406          * the current speed from this uclass
407          */
408         if (ops->set_bus_speed) {
409                 ret = ops->set_bus_speed(bus, speed);
410                 if (ret)
411                         return ret;
412         }
413         i2c->speed_hz = speed;
414
415         return 0;
416 }
417
418 int dm_i2c_get_bus_speed(struct udevice *bus)
419 {
420         struct dm_i2c_ops *ops = i2c_get_ops(bus);
421         struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
422
423         if (!ops->get_bus_speed)
424                 return i2c->speed_hz;
425
426         return ops->get_bus_speed(bus);
427 }
428
429 int i2c_set_chip_flags(struct udevice *dev, uint flags)
430 {
431         struct udevice *bus = dev->parent;
432         struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
433         struct dm_i2c_ops *ops = i2c_get_ops(bus);
434         int ret;
435
436         if (ops->set_flags) {
437                 ret = ops->set_flags(dev, flags);
438                 if (ret)
439                         return ret;
440         }
441         chip->flags = flags;
442
443         return 0;
444 }
445
446 int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
447 {
448         struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
449
450         *flagsp = chip->flags;
451
452         return 0;
453 }
454
455 int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
456 {
457         struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
458
459         if (offset_len > I2C_MAX_OFFSET_LEN)
460                 return -EINVAL;
461         chip->offset_len = offset_len;
462
463         return 0;
464 }
465
466 int i2c_get_chip_offset_len(struct udevice *dev)
467 {
468         struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
469
470         return chip->offset_len;
471 }
472
473 int i2c_set_chip_addr_offset_mask(struct udevice *dev, uint mask)
474 {
475         struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
476
477         chip->chip_addr_offset_mask = mask;
478
479         return 0;
480 }
481
482 uint i2c_get_chip_addr_offset_mask(struct udevice *dev)
483 {
484         struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
485
486         return chip->chip_addr_offset_mask;
487 }
488
489 #if CONFIG_IS_ENABLED(DM_GPIO)
490 static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
491 {
492         if (bit)
493                 dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
494         else
495                 dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
496                                            GPIOD_ACTIVE_LOW |
497                                            GPIOD_IS_OUT_ACTIVE);
498 }
499
500 static int i2c_gpio_get_pin(struct gpio_desc *pin)
501 {
502         return dm_gpio_get_value(pin);
503 }
504
505 int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
506                           struct gpio_desc *scl_pin,
507                           unsigned int scl_count,
508                           unsigned int start_count,
509                           unsigned int delay)
510 {
511         int i, ret = -EREMOTEIO;
512
513         i2c_gpio_set_pin(sda_pin, 1);
514         i2c_gpio_set_pin(scl_pin, 1);
515         udelay(delay);
516
517         /*  Toggle SCL until slave release SDA */
518         while (scl_count-- >= 0) {
519                 i2c_gpio_set_pin(scl_pin, 1);
520                 udelay(delay);
521                 i2c_gpio_set_pin(scl_pin, 0);
522                 udelay(delay);
523                 if (i2c_gpio_get_pin(sda_pin)) {
524                         ret = 0;
525                         break;
526                 }
527         }
528
529         if (!ret && start_count) {
530                 for (i = 0; i < start_count; i++) {
531                         /* Send start condition */
532                         udelay(delay);
533                         i2c_gpio_set_pin(sda_pin, 1);
534                         udelay(delay);
535                         i2c_gpio_set_pin(scl_pin, 1);
536                         udelay(delay);
537                         i2c_gpio_set_pin(sda_pin, 0);
538                         udelay(delay);
539                         i2c_gpio_set_pin(scl_pin, 0);
540                 }
541         }
542
543         /* Then, send I2C stop */
544         i2c_gpio_set_pin(sda_pin, 0);
545         udelay(delay);
546
547         i2c_gpio_set_pin(scl_pin, 1);
548         udelay(delay);
549
550         i2c_gpio_set_pin(sda_pin, 1);
551         udelay(delay);
552
553         if (!i2c_gpio_get_pin(sda_pin) || !i2c_gpio_get_pin(scl_pin))
554                 ret = -EREMOTEIO;
555
556         return ret;
557 }
558
559 static int i2c_deblock_gpio(struct udevice *bus)
560 {
561         struct gpio_desc gpios[PIN_COUNT];
562         int ret, ret0;
563
564         ret = gpio_request_list_by_name(bus, "gpios", gpios,
565                                         ARRAY_SIZE(gpios), GPIOD_IS_IN);
566         if (ret != ARRAY_SIZE(gpios)) {
567                 debug("%s: I2C Node '%s' has no 'gpios' property %s\n",
568                       __func__, dev_read_name(bus), bus->name);
569                 if (ret >= 0) {
570                         gpio_free_list(bus, gpios, ret);
571                         ret = -ENOENT;
572                 }
573                 goto out;
574         }
575
576         ret = pinctrl_select_state(bus, "gpio");
577         if (ret) {
578                 debug("%s: I2C Node '%s' has no 'gpio' pinctrl state. %s\n",
579                       __func__, dev_read_name(bus), bus->name);
580                 goto out_no_pinctrl;
581         }
582
583         ret0 = i2c_deblock_gpio_loop(&gpios[PIN_SDA], &gpios[PIN_SCL], 9, 0, 5);
584
585         ret = pinctrl_select_state(bus, "default");
586         if (ret) {
587                 debug("%s: I2C Node '%s' has no 'default' pinctrl state. %s\n",
588                       __func__, dev_read_name(bus), bus->name);
589         }
590
591         ret = !ret ? ret0 : ret;
592
593 out_no_pinctrl:
594         gpio_free_list(bus, gpios, ARRAY_SIZE(gpios));
595 out:
596         return ret;
597 }
598 #else
599 static int i2c_deblock_gpio(struct udevice *bus)
600 {
601         return -ENOSYS;
602 }
603 #endif /* DM_GPIO */
604
605 int i2c_deblock(struct udevice *bus)
606 {
607         struct dm_i2c_ops *ops = i2c_get_ops(bus);
608
609         if (!ops->deblock)
610                 return i2c_deblock_gpio(bus);
611
612         return ops->deblock(bus);
613 }
614
615 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
616 int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip)
617 {
618         int addr;
619
620         chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len",
621                                                 1);
622         chip->flags = 0;
623         addr = dev_read_u32_default(dev, "reg", -1);
624         if (addr == -1) {
625                 debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__,
626                       dev_read_name(dev), dev->name);
627                 return -EINVAL;
628         }
629         chip->chip_addr = addr;
630
631         return 0;
632 }
633 #endif
634
635 static int i2c_pre_probe(struct udevice *dev)
636 {
637 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
638         struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
639         unsigned int max = 0;
640         ofnode node;
641         int ret;
642
643         i2c->max_transaction_bytes = 0;
644         dev_for_each_subnode(node, dev) {
645                 ret = ofnode_read_u32(node,
646                                       "u-boot,i2c-transaction-bytes",
647                                       &max);
648                 if (!ret && max > i2c->max_transaction_bytes)
649                         i2c->max_transaction_bytes = max;
650         }
651
652         debug("%s: I2C bus: %s max transaction bytes: %d\n", __func__,
653               dev->name, i2c->max_transaction_bytes);
654 #endif
655         return 0;
656 }
657
658 static int i2c_post_probe(struct udevice *dev)
659 {
660 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
661         struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
662
663         i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency",
664                                              I2C_SPEED_STANDARD_RATE);
665
666         return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
667 #else
668         return 0;
669 #endif
670 }
671
672 static int i2c_child_post_bind(struct udevice *dev)
673 {
674 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
675         struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
676
677         if (!dev_of_valid(dev))
678                 return 0;
679         return i2c_chip_ofdata_to_platdata(dev, plat);
680 #else
681         return 0;
682 #endif
683 }
684
685 struct i2c_priv {
686         int max_id;
687 };
688
689 static int i2c_post_bind(struct udevice *dev)
690 {
691         struct uclass *class = dev->uclass;
692         struct i2c_priv *priv = class->priv;
693         int ret = 0;
694
695         /* Just for sure */
696         if (!priv)
697                 return -ENOMEM;
698
699         debug("%s: %s, req_seq=%d\n", __func__, dev->name, dev->req_seq);
700
701         /* if there is no alias ID, use the first free */
702         if (dev->req_seq == -1)
703                 dev->req_seq = ++priv->max_id;
704
705         debug("%s: %s, new req_seq=%d\n", __func__, dev->name, dev->req_seq);
706
707 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
708         ret = dm_scan_fdt_dev(dev);
709 #endif
710         return ret;
711 }
712
713 int i2c_uclass_init(struct uclass *class)
714 {
715         struct i2c_priv *priv = class->priv;
716
717         /* Just for sure */
718         if (!priv)
719                 return -ENOMEM;
720
721         /* Get the last allocated alias. */
722         if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA))
723                 priv->max_id = dev_read_alias_highest_id("i2c");
724         else
725                 priv->max_id = -1;
726
727         debug("%s: highest alias id is %d\n", __func__, priv->max_id);
728
729         return 0;
730 }
731
732 UCLASS_DRIVER(i2c) = {
733         .id             = UCLASS_I2C,
734         .name           = "i2c",
735         .flags          = DM_UC_FLAG_SEQ_ALIAS,
736         .post_bind      = i2c_post_bind,
737         .init           = i2c_uclass_init,
738         .priv_auto_alloc_size = sizeof(struct i2c_priv),
739         .pre_probe      = i2c_pre_probe,
740         .post_probe     = i2c_post_probe,
741         .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
742         .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
743         .child_post_bind = i2c_child_post_bind,
744 };
745
746 UCLASS_DRIVER(i2c_generic) = {
747         .id             = UCLASS_I2C_GENERIC,
748         .name           = "i2c_generic",
749 };
750
751 U_BOOT_DRIVER(i2c_generic_chip_drv) = {
752         .name           = "i2c_generic_chip_drv",
753         .id             = UCLASS_I2C_GENERIC,
754 };