1
2
3
4
5
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
9#include <i2c.h>
10#include <malloc.h>
11#include <dm/device-internal.h>
12#include <dm/lists.h>
13#include <dm/pinctrl.h>
14#ifdef CONFIG_DM_GPIO
15#include <asm/gpio.h>
16#endif
17
18#define I2C_MAX_OFFSET_LEN 4
19
20enum {
21 PIN_SDA = 0,
22 PIN_SCL,
23 PIN_COUNT,
24};
25
26
27void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
28{
29 int i;
30
31 for (i = 0; i < nmsgs; i++) {
32 struct i2c_msg *m = &msg[i];
33
34 printf(" %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W",
35 msg->addr, msg->len);
36 if (!(m->flags & I2C_M_RD))
37 printf(": %x", m->buf[0]);
38 printf("\n");
39 }
40}
41
42
43
44
45
46
47
48
49
50
51
52static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
53 uint8_t offset_buf[], struct i2c_msg *msg)
54{
55 int offset_len;
56
57 msg->addr = chip->chip_addr;
58 msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
59 msg->len = chip->offset_len;
60 msg->buf = offset_buf;
61 if (!chip->offset_len)
62 return -EADDRNOTAVAIL;
63 assert(chip->offset_len <= I2C_MAX_OFFSET_LEN);
64 offset_len = chip->offset_len;
65 while (offset_len--)
66 *offset_buf++ = offset >> (8 * offset_len);
67
68 return 0;
69}
70
71static int i2c_read_bytewise(struct udevice *dev, uint offset,
72 uint8_t *buffer, int len)
73{
74 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
75 struct udevice *bus = dev_get_parent(dev);
76 struct dm_i2c_ops *ops = i2c_get_ops(bus);
77 struct i2c_msg msg[2], *ptr;
78 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
79 int ret;
80 int i;
81
82 for (i = 0; i < len; i++) {
83 if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
84 return -EINVAL;
85 ptr = msg + 1;
86 ptr->addr = chip->chip_addr;
87 ptr->flags = msg->flags | I2C_M_RD;
88 ptr->len = 1;
89 ptr->buf = &buffer[i];
90 ptr++;
91
92 ret = ops->xfer(bus, msg, ptr - msg);
93 if (ret)
94 return ret;
95 }
96
97 return 0;
98}
99
100static int i2c_write_bytewise(struct udevice *dev, uint offset,
101 const uint8_t *buffer, int len)
102{
103 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
104 struct udevice *bus = dev_get_parent(dev);
105 struct dm_i2c_ops *ops = i2c_get_ops(bus);
106 struct i2c_msg msg[1];
107 uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
108 int ret;
109 int i;
110
111 for (i = 0; i < len; i++) {
112 if (i2c_setup_offset(chip, offset + i, buf, msg))
113 return -EINVAL;
114 buf[msg->len++] = buffer[i];
115
116 ret = ops->xfer(bus, msg, 1);
117 if (ret)
118 return ret;
119 }
120
121 return 0;
122}
123
124int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
125{
126 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
127 struct udevice *bus = dev_get_parent(dev);
128 struct dm_i2c_ops *ops = i2c_get_ops(bus);
129 struct i2c_msg msg[2], *ptr;
130 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
131 int msg_count;
132
133 if (!ops->xfer)
134 return -ENOSYS;
135 if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
136 return i2c_read_bytewise(dev, offset, buffer, len);
137 ptr = msg;
138 if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
139 ptr++;
140
141 if (len) {
142 ptr->addr = chip->chip_addr;
143 ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
144 ptr->flags |= I2C_M_RD;
145 ptr->len = len;
146 ptr->buf = buffer;
147 ptr++;
148 }
149 msg_count = ptr - msg;
150
151 return ops->xfer(bus, msg, msg_count);
152}
153
154int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
155 int len)
156{
157 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
158 struct udevice *bus = dev_get_parent(dev);
159 struct dm_i2c_ops *ops = i2c_get_ops(bus);
160 struct i2c_msg msg[1];
161
162 if (!ops->xfer)
163 return -ENOSYS;
164
165 if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
166 return i2c_write_bytewise(dev, offset, buffer, len);
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 if (len < 64) {
186 uint8_t buf[I2C_MAX_OFFSET_LEN + len];
187
188 i2c_setup_offset(chip, offset, buf, msg);
189 msg->len += len;
190 memcpy(buf + chip->offset_len, buffer, len);
191
192 return ops->xfer(bus, msg, 1);
193 } else {
194 uint8_t *buf;
195 int ret;
196
197 buf = malloc(I2C_MAX_OFFSET_LEN + len);
198 if (!buf)
199 return -ENOMEM;
200 i2c_setup_offset(chip, offset, buf, msg);
201 msg->len += len;
202 memcpy(buf + chip->offset_len, buffer, len);
203
204 ret = ops->xfer(bus, msg, 1);
205 free(buf);
206 return ret;
207 }
208}
209
210int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
211{
212 struct udevice *bus = dev_get_parent(dev);
213 struct dm_i2c_ops *ops = i2c_get_ops(bus);
214
215 if (!ops->xfer)
216 return -ENOSYS;
217
218 return ops->xfer(bus, msg, nmsgs);
219}
220
221int dm_i2c_reg_read(struct udevice *dev, uint offset)
222{
223 uint8_t val;
224 int ret;
225
226 ret = dm_i2c_read(dev, offset, &val, 1);
227 if (ret < 0)
228 return ret;
229
230 return val;
231}
232
233int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
234{
235 uint8_t val = value;
236
237 return dm_i2c_write(dev, offset, &val, 1);
238}
239
240
241
242
243
244
245
246
247
248
249static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
250 enum dm_i2c_chip_flags chip_flags)
251{
252 struct dm_i2c_ops *ops = i2c_get_ops(bus);
253 struct i2c_msg msg[1];
254 int ret;
255
256 if (ops->probe_chip) {
257 ret = ops->probe_chip(bus, chip_addr, chip_flags);
258 if (!ret || ret != -ENOSYS)
259 return ret;
260 }
261
262 if (!ops->xfer)
263 return -ENOSYS;
264
265
266 msg->addr = chip_addr;
267 msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
268 msg->len = 0;
269 msg->buf = NULL;
270
271 return ops->xfer(bus, msg, 1);
272}
273
274static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
275 struct udevice **devp)
276{
277 struct dm_i2c_chip *chip;
278 char name[30], *str;
279 struct udevice *dev;
280 int ret;
281
282 snprintf(name, sizeof(name), "generic_%x", chip_addr);
283 str = strdup(name);
284 if (!str)
285 return -ENOMEM;
286 ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
287 debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
288 if (ret)
289 goto err_bind;
290
291
292 chip = dev_get_parent_platdata(dev);
293 chip->chip_addr = chip_addr;
294 chip->offset_len = offset_len;
295 ret = device_probe(dev);
296 debug("%s: device_probe: ret=%d\n", __func__, ret);
297 if (ret)
298 goto err_probe;
299
300 *devp = dev;
301 return 0;
302
303err_probe:
304
305
306
307
308 device_unbind(dev);
309err_bind:
310 free(str);
311 return ret;
312}
313
314int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
315 struct udevice **devp)
316{
317 struct udevice *dev;
318
319 debug("%s: Searching bus '%s' for address %02x: ", __func__,
320 bus->name, chip_addr);
321 for (device_find_first_child(bus, &dev); dev;
322 device_find_next_child(&dev)) {
323 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
324 int ret;
325
326 if (chip->chip_addr == chip_addr) {
327 ret = device_probe(dev);
328 debug("found, ret=%d\n", ret);
329 if (ret)
330 return ret;
331 *devp = dev;
332 return 0;
333 }
334 }
335 debug("not found\n");
336 return i2c_bind_driver(bus, chip_addr, offset_len, devp);
337}
338
339int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
340 struct udevice **devp)
341{
342 struct udevice *bus;
343 int ret;
344
345 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
346 if (ret) {
347 debug("Cannot find I2C bus %d\n", busnum);
348 return ret;
349 }
350 ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
351 if (ret) {
352 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
353 busnum);
354 return ret;
355 }
356
357 return 0;
358}
359
360int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
361 struct udevice **devp)
362{
363 int ret;
364
365 *devp = NULL;
366
367
368 ret = i2c_probe_chip(bus, chip_addr, chip_flags);
369 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
370 chip_addr, ret);
371 if (ret)
372 return ret;
373
374
375 ret = i2c_get_chip(bus, chip_addr, 1, devp);
376 debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
377
378 return ret;
379}
380
381int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
382{
383 struct dm_i2c_ops *ops = i2c_get_ops(bus);
384 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
385 int ret;
386
387
388
389
390
391
392 if (ops->set_bus_speed) {
393 ret = ops->set_bus_speed(bus, speed);
394 if (ret)
395 return ret;
396 }
397 i2c->speed_hz = speed;
398
399 return 0;
400}
401
402int dm_i2c_get_bus_speed(struct udevice *bus)
403{
404 struct dm_i2c_ops *ops = i2c_get_ops(bus);
405 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
406
407 if (!ops->get_bus_speed)
408 return i2c->speed_hz;
409
410 return ops->get_bus_speed(bus);
411}
412
413int i2c_set_chip_flags(struct udevice *dev, uint flags)
414{
415 struct udevice *bus = dev->parent;
416 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
417 struct dm_i2c_ops *ops = i2c_get_ops(bus);
418 int ret;
419
420 if (ops->set_flags) {
421 ret = ops->set_flags(dev, flags);
422 if (ret)
423 return ret;
424 }
425 chip->flags = flags;
426
427 return 0;
428}
429
430int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
431{
432 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
433
434 *flagsp = chip->flags;
435
436 return 0;
437}
438
439int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
440{
441 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
442
443 if (offset_len > I2C_MAX_OFFSET_LEN)
444 return -EINVAL;
445 chip->offset_len = offset_len;
446
447 return 0;
448}
449
450int i2c_get_chip_offset_len(struct udevice *dev)
451{
452 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
453
454 return chip->offset_len;
455}
456
457#ifdef CONFIG_DM_GPIO
458static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
459{
460 if (bit)
461 dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
462 else
463 dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
464 GPIOD_ACTIVE_LOW |
465 GPIOD_IS_OUT_ACTIVE);
466}
467
468static int i2c_gpio_get_pin(struct gpio_desc *pin)
469{
470 return dm_gpio_get_value(pin);
471}
472
473static int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
474 struct gpio_desc *scl_pin)
475{
476 int counter = 9;
477 int ret = 0;
478
479 i2c_gpio_set_pin(sda_pin, 1);
480 i2c_gpio_set_pin(scl_pin, 1);
481 udelay(5);
482
483
484 while (counter-- >= 0) {
485 i2c_gpio_set_pin(scl_pin, 1);
486 udelay(5);
487 i2c_gpio_set_pin(scl_pin, 0);
488 udelay(5);
489 if (i2c_gpio_get_pin(sda_pin))
490 break;
491 }
492
493
494 i2c_gpio_set_pin(sda_pin, 0);
495 udelay(5);
496
497 i2c_gpio_set_pin(scl_pin, 1);
498 udelay(5);
499
500 i2c_gpio_set_pin(sda_pin, 1);
501 udelay(5);
502
503 if (!i2c_gpio_get_pin(sda_pin) || !i2c_gpio_get_pin(scl_pin))
504 ret = -EREMOTEIO;
505
506 return ret;
507}
508
509static int i2c_deblock_gpio(struct udevice *bus)
510{
511 struct gpio_desc gpios[PIN_COUNT];
512 int ret, ret0;
513
514 ret = gpio_request_list_by_name(bus, "gpios", gpios,
515 ARRAY_SIZE(gpios), GPIOD_IS_IN);
516 if (ret != ARRAY_SIZE(gpios)) {
517 debug("%s: I2C Node '%s' has no 'gpios' property %s\n",
518 __func__, dev_read_name(bus), bus->name);
519 if (ret >= 0) {
520 gpio_free_list(bus, gpios, ret);
521 ret = -ENOENT;
522 }
523 goto out;
524 }
525
526 ret = pinctrl_select_state(bus, "gpio");
527 if (ret) {
528 debug("%s: I2C Node '%s' has no 'gpio' pinctrl state. %s\n",
529 __func__, dev_read_name(bus), bus->name);
530 goto out_no_pinctrl;
531 }
532
533 ret0 = i2c_deblock_gpio_loop(&gpios[PIN_SDA], &gpios[PIN_SCL]);
534
535 ret = pinctrl_select_state(bus, "default");
536 if (ret) {
537 debug("%s: I2C Node '%s' has no 'default' pinctrl state. %s\n",
538 __func__, dev_read_name(bus), bus->name);
539 }
540
541 ret = !ret ? ret0 : ret;
542
543out_no_pinctrl:
544 gpio_free_list(bus, gpios, ARRAY_SIZE(gpios));
545out:
546 return ret;
547}
548#else
549static int i2c_deblock_gpio(struct udevice *bus)
550{
551 return -ENOSYS;
552}
553#endif
554
555int i2c_deblock(struct udevice *bus)
556{
557 struct dm_i2c_ops *ops = i2c_get_ops(bus);
558
559 if (!ops->deblock)
560 return i2c_deblock_gpio(bus);
561
562 return ops->deblock(bus);
563}
564
565#if CONFIG_IS_ENABLED(OF_CONTROL)
566int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip)
567{
568 int addr;
569
570 chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len",
571 1);
572 chip->flags = 0;
573 addr = dev_read_u32_default(dev, "reg", -1);
574 if (addr == -1) {
575 debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__,
576 dev_read_name(dev), dev->name);
577 return -EINVAL;
578 }
579 chip->chip_addr = addr;
580
581 return 0;
582}
583#endif
584
585static int i2c_post_probe(struct udevice *dev)
586{
587#if CONFIG_IS_ENABLED(OF_CONTROL)
588 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
589
590 i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency", 100000);
591
592 return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
593#else
594 return 0;
595#endif
596}
597
598static int i2c_child_post_bind(struct udevice *dev)
599{
600#if CONFIG_IS_ENABLED(OF_CONTROL)
601 struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
602
603 if (!dev_of_valid(dev))
604 return 0;
605 return i2c_chip_ofdata_to_platdata(dev, plat);
606#else
607 return 0;
608#endif
609}
610
611UCLASS_DRIVER(i2c) = {
612 .id = UCLASS_I2C,
613 .name = "i2c",
614 .flags = DM_UC_FLAG_SEQ_ALIAS,
615#if CONFIG_IS_ENABLED(OF_CONTROL)
616 .post_bind = dm_scan_fdt_dev,
617#endif
618 .post_probe = i2c_post_probe,
619 .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
620 .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
621 .child_post_bind = i2c_child_post_bind,
622};
623
624UCLASS_DRIVER(i2c_generic) = {
625 .id = UCLASS_I2C_GENERIC,
626 .name = "i2c_generic",
627};
628
629U_BOOT_DRIVER(i2c_generic_chip_drv) = {
630 .name = "i2c_generic_chip_drv",
631 .id = UCLASS_I2C_GENERIC,
632};
633