1 /*
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>
10 #include <i2c.h>
11 #include <malloc.h>
12 #include <dm/device-internal.h>
13 #include <dm/lists.h>
14
15 DECLARE_GLOBAL_DATA_PTR;
16
17 #define I2C_MAX_OFFSET_LEN 4
18
19 /* Useful debugging function */
i2c_dump_msgs(struct i2c_msg * msg,int nmsgs)20 void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
21 {
22 int i;
23
24 for (i = 0; i < nmsgs; i++) {
25 struct i2c_msg *m = &msg[i];
26
27 printf(" %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W",
28 msg->addr, msg->len);
29 if (!(m->flags & I2C_M_RD))
30 printf(": %x", m->buf[0]);
31 printf("\n");
32 }
33 }
34
35 /**
36 * i2c_setup_offset() - Set up a new message with a chip offset
37 *
38 * @chip: Chip to use
39 * @offset: Byte offset within chip
40 * @offset_buf: Place to put byte offset
41 * @msg: Message buffer
42 * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
43 * message is still set up but will not contain an offset.
44 */
i2c_setup_offset(struct dm_i2c_chip * chip,uint offset,uint8_t offset_buf[],struct i2c_msg * msg)45 static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
46 uint8_t offset_buf[], struct i2c_msg *msg)
47 {
48 int offset_len;
49
50 msg->addr = chip->chip_addr;
51 msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
52 msg->len = chip->offset_len;
53 msg->buf = offset_buf;
54 if (!chip->offset_len)
55 return -EADDRNOTAVAIL;
56 assert(chip->offset_len <= I2C_MAX_OFFSET_LEN);
57 offset_len = chip->offset_len;
58 while (offset_len--)
59 *offset_buf++ = offset >> (8 * offset_len);
60
61 return 0;
62 }
63
i2c_read_bytewise(struct udevice * dev,uint offset,uint8_t * buffer,int len)64 static int i2c_read_bytewise(struct udevice *dev, uint offset,
65 uint8_t *buffer, int len)
66 {
67 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
68 struct udevice *bus = dev_get_parent(dev);
69 struct dm_i2c_ops *ops = i2c_get_ops(bus);
70 struct i2c_msg msg[2], *ptr;
71 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
72 int ret;
73 int i;
74
75 for (i = 0; i < len; i++) {
76 if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
77 return -EINVAL;
78 ptr = msg + 1;
79 ptr->addr = chip->chip_addr;
80 ptr->flags = msg->flags | I2C_M_RD;
81 ptr->len = 1;
82 ptr->buf = &buffer[i];
83 ptr++;
84
85 ret = ops->xfer(bus, msg, ptr - msg);
86 if (ret)
87 return ret;
88 }
89
90 return 0;
91 }
92
i2c_write_bytewise(struct udevice * dev,uint offset,const uint8_t * buffer,int len)93 static int i2c_write_bytewise(struct udevice *dev, uint offset,
94 const uint8_t *buffer, int len)
95 {
96 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
97 struct udevice *bus = dev_get_parent(dev);
98 struct dm_i2c_ops *ops = i2c_get_ops(bus);
99 struct i2c_msg msg[1];
100 uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
101 int ret;
102 int i;
103
104 for (i = 0; i < len; i++) {
105 if (i2c_setup_offset(chip, offset + i, buf, msg))
106 return -EINVAL;
107 buf[msg->len++] = buffer[i];
108
109 ret = ops->xfer(bus, msg, 1);
110 if (ret)
111 return ret;
112 }
113
114 return 0;
115 }
116
dm_i2c_read(struct udevice * dev,uint offset,uint8_t * buffer,int len)117 int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
118 {
119 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
120 struct udevice *bus = dev_get_parent(dev);
121 struct dm_i2c_ops *ops = i2c_get_ops(bus);
122 struct i2c_msg msg[2], *ptr;
123 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
124 int msg_count;
125
126 if (!ops->xfer)
127 return -ENOSYS;
128 if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
129 return i2c_read_bytewise(dev, offset, buffer, len);
130 ptr = msg;
131 if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
132 ptr++;
133
134 if (len) {
135 ptr->addr = chip->chip_addr;
136 ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
137 ptr->flags |= I2C_M_RD;
138 ptr->len = len;
139 ptr->buf = buffer;
140 ptr++;
141 }
142 msg_count = ptr - msg;
143
144 return ops->xfer(bus, msg, msg_count);
145 }
146
dm_i2c_write(struct udevice * dev,uint offset,const uint8_t * buffer,int len)147 int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
148 int len)
149 {
150 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
151 struct udevice *bus = dev_get_parent(dev);
152 struct dm_i2c_ops *ops = i2c_get_ops(bus);
153 struct i2c_msg msg[1];
154
155 if (!ops->xfer)
156 return -ENOSYS;
157
158 if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
159 return i2c_write_bytewise(dev, offset, buffer, len);
160 /*
161 * The simple approach would be to send two messages here: one to
162 * set the offset and one to write the bytes. However some drivers
163 * will not be expecting this, and some chips won't like how the
164 * driver presents this on the I2C bus.
165 *
166 * The API does not support separate offset and data. We could extend
167 * it with a flag indicating that there is data in the next message
168 * that needs to be processed in the same transaction. We could
169 * instead add an additional buffer to each message. For now, handle
170 * this in the uclass since it isn't clear what the impact on drivers
171 * would be with this extra complication. Unfortunately this means
172 * copying the message.
173 *
174 * Use the stack for small messages, malloc() for larger ones. We
175 * need to allow space for the offset (up to 4 bytes) and the message
176 * itself.
177 */
178 if (len < 64) {
179 uint8_t buf[I2C_MAX_OFFSET_LEN + len];
180
181 i2c_setup_offset(chip, offset, buf, msg);
182 msg->len += len;
183 memcpy(buf + chip->offset_len, buffer, len);
184
185 return ops->xfer(bus, msg, 1);
186 } else {
187 uint8_t *buf;
188 int ret;
189
190 buf = malloc(I2C_MAX_OFFSET_LEN + len);
191 if (!buf)
192 return -ENOMEM;
193 i2c_setup_offset(chip, offset, buf, msg);
194 msg->len += len;
195 memcpy(buf + chip->offset_len, buffer, len);
196
197 ret = ops->xfer(bus, msg, 1);
198 free(buf);
199 return ret;
200 }
201 }
202
dm_i2c_xfer(struct udevice * dev,struct i2c_msg * msg,int nmsgs)203 int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
204 {
205 struct udevice *bus = dev_get_parent(dev);
206 struct dm_i2c_ops *ops = i2c_get_ops(bus);
207
208 if (!ops->xfer)
209 return -ENOSYS;
210
211 return ops->xfer(bus, msg, nmsgs);
212 }
213
dm_i2c_reg_read(struct udevice * dev,uint offset)214 int dm_i2c_reg_read(struct udevice *dev, uint offset)
215 {
216 uint8_t val;
217 int ret;
218
219 ret = dm_i2c_read(dev, offset, &val, 1);
220 if (ret < 0)
221 return ret;
222
223 return val;
224 }
225
dm_i2c_reg_write(struct udevice * dev,uint offset,uint value)226 int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
227 {
228 uint8_t val = value;
229
230 return dm_i2c_write(dev, offset, &val, 1);
231 }
232
dm_i2c_reg_clrset(struct udevice * dev,uint offset,u32 clr,u32 set)233 int dm_i2c_reg_clrset(struct udevice *dev, uint offset, u32 clr, u32 set)
234 {
235 uint8_t val;
236 int ret;
237
238 ret = dm_i2c_read(dev, offset, &val, 1);
239 if (ret < 0)
240 return ret;
241
242 val &= ~clr;
243 val |= set;
244
245 return dm_i2c_write(dev, offset, &val, 1);
246 }
247
248 /**
249 * i2c_probe_chip() - probe for a chip on a bus
250 *
251 * @bus: Bus to probe
252 * @chip_addr: Chip address to probe
253 * @flags: Flags for the chip
254 * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
255 * does not respond to probe
256 */
i2c_probe_chip(struct udevice * bus,uint chip_addr,enum dm_i2c_chip_flags chip_flags)257 static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
258 enum dm_i2c_chip_flags chip_flags)
259 {
260 struct dm_i2c_ops *ops = i2c_get_ops(bus);
261 struct i2c_msg msg[1];
262 int ret;
263
264 if (ops->probe_chip) {
265 ret = ops->probe_chip(bus, chip_addr, chip_flags);
266 if (!ret || ret != -ENOSYS)
267 return ret;
268 }
269
270 if (!ops->xfer)
271 return -ENOSYS;
272
273 /* Probe with a zero-length message */
274 msg->addr = chip_addr;
275 msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
276 msg->len = 0;
277 msg->buf = NULL;
278
279 return ops->xfer(bus, msg, 1);
280 }
281
i2c_bind_driver(struct udevice * bus,uint chip_addr,uint offset_len,struct udevice ** devp)282 static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
283 struct udevice **devp)
284 {
285 struct dm_i2c_chip *chip;
286 char name[30], *str;
287 struct udevice *dev;
288 int ret;
289
290 snprintf(name, sizeof(name), "generic_%x", chip_addr);
291 str = strdup(name);
292 if (!str)
293 return -ENOMEM;
294 ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
295 debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
296 if (ret)
297 goto err_bind;
298
299 /* Tell the device what we know about it */
300 chip = dev_get_parent_platdata(dev);
301 chip->chip_addr = chip_addr;
302 chip->offset_len = offset_len;
303 ret = device_probe(dev);
304 debug("%s: device_probe: ret=%d\n", __func__, ret);
305 if (ret)
306 goto err_probe;
307
308 *devp = dev;
309 return 0;
310
311 err_probe:
312 /*
313 * If the device failed to probe, unbind it. There is nothing there
314 * on the bus so we don't want to leave it lying around
315 */
316 device_unbind(dev);
317 err_bind:
318 free(str);
319 return ret;
320 }
321
i2c_get_chip(struct udevice * bus,uint chip_addr,uint offset_len,struct udevice ** devp)322 int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
323 struct udevice **devp)
324 {
325 struct udevice *dev;
326
327 debug("%s: Searching bus '%s' for address %02x: ", __func__,
328 bus->name, chip_addr);
329 for (device_find_first_child(bus, &dev); dev;
330 device_find_next_child(&dev)) {
331 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
332 int ret;
333
334 if (chip->chip_addr == chip_addr) {
335 ret = device_probe(dev);
336 debug("found, ret=%d\n", ret);
337 if (ret)
338 return ret;
339 *devp = dev;
340 return 0;
341 }
342 }
343 debug("not found\n");
344 return i2c_bind_driver(bus, chip_addr, offset_len, devp);
345 }
346
i2c_get_chip_for_busnum(int busnum,int chip_addr,uint offset_len,struct udevice ** devp)347 int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
348 struct udevice **devp)
349 {
350 struct udevice *bus;
351 int ret;
352
353 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
354 if (ret) {
355 debug("Cannot find I2C bus %d\n", busnum);
356 return ret;
357 }
358 ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
359 if (ret) {
360 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
361 busnum);
362 return ret;
363 }
364
365 return 0;
366 }
367
dm_i2c_probe(struct udevice * bus,uint chip_addr,uint chip_flags,struct udevice ** devp)368 int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
369 struct udevice **devp)
370 {
371 int ret;
372
373 *devp = NULL;
374
375 /* First probe that chip */
376 ret = i2c_probe_chip(bus, chip_addr, chip_flags);
377 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
378 chip_addr, ret);
379 if (ret)
380 return ret;
381
382 /* The chip was found, see if we have a driver, and probe it */
383 ret = i2c_get_chip(bus, chip_addr, 1, devp);
384 debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
385
386 return ret;
387 }
388
dm_i2c_set_bus_speed(struct udevice * bus,unsigned int speed)389 int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
390 {
391 struct dm_i2c_ops *ops = i2c_get_ops(bus);
392 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
393 int ret;
394
395 /*
396 * If we have a method, call it. If not then the driver probably wants
397 * to deal with speed changes on the next transfer. It can easily read
398 * the current speed from this uclass
399 */
400 if (ops->set_bus_speed) {
401 ret = ops->set_bus_speed(bus, speed);
402 if (ret)
403 return ret;
404 }
405 i2c->speed_hz = speed;
406
407 return 0;
408 }
409
dm_i2c_get_bus_speed(struct udevice * bus)410 int dm_i2c_get_bus_speed(struct udevice *bus)
411 {
412 struct dm_i2c_ops *ops = i2c_get_ops(bus);
413 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
414
415 if (!ops->get_bus_speed)
416 return i2c->speed_hz;
417
418 return ops->get_bus_speed(bus);
419 }
420
i2c_set_chip_flags(struct udevice * dev,uint flags)421 int i2c_set_chip_flags(struct udevice *dev, uint flags)
422 {
423 struct udevice *bus = dev->parent;
424 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
425 struct dm_i2c_ops *ops = i2c_get_ops(bus);
426 int ret;
427
428 if (ops->set_flags) {
429 ret = ops->set_flags(dev, flags);
430 if (ret)
431 return ret;
432 }
433 chip->flags = flags;
434
435 return 0;
436 }
437
i2c_get_chip_flags(struct udevice * dev,uint * flagsp)438 int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
439 {
440 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
441
442 *flagsp = chip->flags;
443
444 return 0;
445 }
446
i2c_set_chip_offset_len(struct udevice * dev,uint offset_len)447 int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
448 {
449 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
450
451 if (offset_len > I2C_MAX_OFFSET_LEN)
452 return -EINVAL;
453 chip->offset_len = offset_len;
454
455 return 0;
456 }
457
i2c_get_chip_offset_len(struct udevice * dev)458 int i2c_get_chip_offset_len(struct udevice *dev)
459 {
460 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
461
462 return chip->offset_len;
463 }
464
i2c_deblock(struct udevice * bus)465 int i2c_deblock(struct udevice *bus)
466 {
467 struct dm_i2c_ops *ops = i2c_get_ops(bus);
468
469 /*
470 * We could implement a software deblocking here if we could get
471 * access to the GPIOs used by I2C, and switch them to GPIO mode
472 * and then back to I2C. This is somewhat beyond our powers in
473 * driver model at present, so for now just fail.
474 *
475 * See https://patchwork.ozlabs.org/patch/399040/
476 */
477 if (!ops->deblock)
478 return -ENOSYS;
479
480 return ops->deblock(bus);
481 }
482
483 #if CONFIG_IS_ENABLED(OF_CONTROL)
i2c_chip_ofdata_to_platdata(struct udevice * dev,struct dm_i2c_chip * chip)484 int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip)
485 {
486 int addr;
487
488 chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len",
489 1);
490 chip->flags = 0;
491 addr = dev_read_u32_default(dev, "reg", -1);
492 if (addr == -1) {
493 debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__,
494 dev_read_name(dev), dev->name);
495 return -EINVAL;
496 }
497 chip->chip_addr = addr;
498
499 return 0;
500 }
501 #endif
502
i2c_post_probe(struct udevice * dev)503 static int i2c_post_probe(struct udevice *dev)
504 {
505 #if CONFIG_IS_ENABLED(OF_CONTROL)
506 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
507 int seq;
508
509 i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency", 100000);
510
511 if (dev_read_alias_seq(dev, &seq) >= 0)
512 printf("I2c%d speed: %dHz\n", seq, i2c->speed_hz);
513 else
514 printf("I2c speed: %dHz\n", i2c->speed_hz);
515
516 return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
517 #else
518 return 0;
519 #endif
520 }
521
i2c_child_post_bind(struct udevice * dev)522 static int i2c_child_post_bind(struct udevice *dev)
523 {
524 #if CONFIG_IS_ENABLED(OF_CONTROL)
525 struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
526
527 if (!dev_of_valid(dev))
528 return 0;
529 return i2c_chip_ofdata_to_platdata(dev, plat);
530 #else
531 return 0;
532 #endif
533 }
534
535 UCLASS_DRIVER(i2c) = {
536 .id = UCLASS_I2C,
537 .name = "i2c",
538 .flags = DM_UC_FLAG_SEQ_ALIAS,
539 #if CONFIG_IS_ENABLED(OF_CONTROL)
540 .post_bind = dm_scan_fdt_dev,
541 #endif
542 .post_probe = i2c_post_probe,
543 .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
544 .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
545 .child_post_bind = i2c_child_post_bind,
546 };
547
548 UCLASS_DRIVER(i2c_generic) = {
549 .id = UCLASS_I2C_GENERIC,
550 .name = "i2c_generic",
551 };
552
553 U_BOOT_DRIVER(i2c_generic_chip_drv) = {
554 .name = "i2c_generic_chip_drv",
555 .id = UCLASS_I2C_GENERIC,
556 };
557