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