blob: 20b30ffbeb5197217c382a84fa37f5b87c6b96e9 [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>
10#include <fdtdec.h>
11#include <i2c.h>
12#include <malloc.h>
13#include <dm/device-internal.h>
14#include <dm/lists.h>
15#include <dm/root.h>
16
17DECLARE_GLOBAL_DATA_PTR;
18
19#define I2C_MAX_OFFSET_LEN 4
20
Simon Glassa8e64c42015-07-02 18:15:39 -060021/* Useful debugging function */
22void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
23{
24 int i;
25
26 for (i = 0; i < nmsgs; i++) {
27 struct i2c_msg *m = &msg[i];
28
29 printf(" %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W",
30 msg->addr, msg->len);
31 if (!(m->flags & I2C_M_RD))
32 printf(": %x", m->buf[0]);
33 printf("\n");
34 }
35}
36
Simon Glassc7a58902014-12-10 08:55:47 -070037/**
38 * i2c_setup_offset() - Set up a new message with a chip offset
39 *
40 * @chip: Chip to use
41 * @offset: Byte offset within chip
42 * @offset_buf: Place to put byte offset
43 * @msg: Message buffer
44 * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
45 * message is still set up but will not contain an offset.
46 */
47static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
48 uint8_t offset_buf[], struct i2c_msg *msg)
49{
50 int offset_len;
51
52 msg->addr = chip->chip_addr;
53 msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
54 msg->len = chip->offset_len;
55 msg->buf = offset_buf;
56 if (!chip->offset_len)
57 return -EADDRNOTAVAIL;
58 assert(chip->offset_len <= I2C_MAX_OFFSET_LEN);
59 offset_len = chip->offset_len;
60 while (offset_len--)
61 *offset_buf++ = offset >> (8 * offset_len);
62
63 return 0;
64}
65
66static int i2c_read_bytewise(struct udevice *dev, uint offset,
67 uint8_t *buffer, int len)
68{
Simon Glass713c3f02015-01-25 08:27:13 -070069 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -070070 struct udevice *bus = dev_get_parent(dev);
71 struct dm_i2c_ops *ops = i2c_get_ops(bus);
72 struct i2c_msg msg[2], *ptr;
73 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
74 int ret;
75 int i;
76
77 for (i = 0; i < len; i++) {
78 if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
79 return -EINVAL;
80 ptr = msg + 1;
81 ptr->addr = chip->chip_addr;
82 ptr->flags = msg->flags | I2C_M_RD;
83 ptr->len = 1;
84 ptr->buf = &buffer[i];
85 ptr++;
86
87 ret = ops->xfer(bus, msg, ptr - msg);
88 if (ret)
89 return ret;
90 }
91
92 return 0;
93}
94
95static int i2c_write_bytewise(struct udevice *dev, uint offset,
96 const uint8_t *buffer, int len)
97{
Simon Glass713c3f02015-01-25 08:27:13 -070098 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -070099 struct udevice *bus = dev_get_parent(dev);
100 struct dm_i2c_ops *ops = i2c_get_ops(bus);
101 struct i2c_msg msg[1];
102 uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
103 int ret;
104 int i;
105
106 for (i = 0; i < len; i++) {
107 if (i2c_setup_offset(chip, offset + i, buf, msg))
108 return -EINVAL;
109 buf[msg->len++] = buffer[i];
110
111 ret = ops->xfer(bus, msg, 1);
112 if (ret)
113 return ret;
114 }
115
116 return 0;
117}
118
Simon Glass7d722762015-01-12 18:02:07 -0700119int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
Simon Glassc7a58902014-12-10 08:55:47 -0700120{
Simon Glass713c3f02015-01-25 08:27:13 -0700121 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700122 struct udevice *bus = dev_get_parent(dev);
123 struct dm_i2c_ops *ops = i2c_get_ops(bus);
124 struct i2c_msg msg[2], *ptr;
125 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
126 int msg_count;
127
128 if (!ops->xfer)
129 return -ENOSYS;
130 if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
131 return i2c_read_bytewise(dev, offset, buffer, len);
132 ptr = msg;
133 if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
134 ptr++;
135
136 if (len) {
137 ptr->addr = chip->chip_addr;
138 ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
139 ptr->flags |= I2C_M_RD;
140 ptr->len = len;
141 ptr->buf = buffer;
142 ptr++;
143 }
144 msg_count = ptr - msg;
145
146 return ops->xfer(bus, msg, msg_count);
147}
148
Simon Glass7d722762015-01-12 18:02:07 -0700149int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
150 int len)
Simon Glassc7a58902014-12-10 08:55:47 -0700151{
Simon Glass713c3f02015-01-25 08:27:13 -0700152 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700153 struct udevice *bus = dev_get_parent(dev);
154 struct dm_i2c_ops *ops = i2c_get_ops(bus);
155 struct i2c_msg msg[1];
156
157 if (!ops->xfer)
158 return -ENOSYS;
159
160 if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
161 return i2c_write_bytewise(dev, offset, buffer, len);
162 /*
163 * The simple approach would be to send two messages here: one to
164 * set the offset and one to write the bytes. However some drivers
165 * will not be expecting this, and some chips won't like how the
166 * driver presents this on the I2C bus.
167 *
168 * The API does not support separate offset and data. We could extend
169 * it with a flag indicating that there is data in the next message
170 * that needs to be processed in the same transaction. We could
171 * instead add an additional buffer to each message. For now, handle
172 * this in the uclass since it isn't clear what the impact on drivers
173 * would be with this extra complication. Unfortunately this means
174 * copying the message.
175 *
176 * Use the stack for small messages, malloc() for larger ones. We
177 * need to allow space for the offset (up to 4 bytes) and the message
178 * itself.
179 */
180 if (len < 64) {
181 uint8_t buf[I2C_MAX_OFFSET_LEN + len];
182
183 i2c_setup_offset(chip, offset, buf, msg);
184 msg->len += len;
185 memcpy(buf + chip->offset_len, buffer, len);
186
187 return ops->xfer(bus, msg, 1);
188 } else {
189 uint8_t *buf;
190 int ret;
191
192 buf = malloc(I2C_MAX_OFFSET_LEN + len);
193 if (!buf)
194 return -ENOMEM;
195 i2c_setup_offset(chip, offset, buf, msg);
196 msg->len += len;
197 memcpy(buf + chip->offset_len, buffer, len);
198
199 ret = ops->xfer(bus, msg, 1);
200 free(buf);
201 return ret;
202 }
203}
204
Simon Glassf2818852015-07-02 18:15:42 -0600205int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
206{
207 struct udevice *bus = dev_get_parent(dev);
208 struct dm_i2c_ops *ops = i2c_get_ops(bus);
209
210 if (!ops->xfer)
211 return -ENOSYS;
212
213 return ops->xfer(bus, msg, nmsgs);
214}
215
Simon Glass0c702612015-04-20 12:37:14 -0600216int dm_i2c_reg_read(struct udevice *dev, uint offset)
217{
218 uint8_t val;
219 int ret;
220
221 ret = dm_i2c_read(dev, offset, &val, 1);
222 if (ret < 0)
223 return ret;
224
225 return val;
226}
227
228int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
229{
230 uint8_t val = value;
231
232 return dm_i2c_write(dev, offset, &val, 1);
233}
234
Simon Glassc7a58902014-12-10 08:55:47 -0700235/**
236 * i2c_probe_chip() - probe for a chip on a bus
237 *
238 * @bus: Bus to probe
239 * @chip_addr: Chip address to probe
240 * @flags: Flags for the chip
241 * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
242 * does not respond to probe
243 */
244static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
245 enum dm_i2c_chip_flags chip_flags)
246{
247 struct dm_i2c_ops *ops = i2c_get_ops(bus);
248 struct i2c_msg msg[1];
249 int ret;
250
251 if (ops->probe_chip) {
252 ret = ops->probe_chip(bus, chip_addr, chip_flags);
253 if (!ret || ret != -ENOSYS)
254 return ret;
255 }
256
257 if (!ops->xfer)
258 return -ENOSYS;
259
260 /* Probe with a zero-length message */
261 msg->addr = chip_addr;
262 msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
263 msg->len = 0;
264 msg->buf = NULL;
265
266 return ops->xfer(bus, msg, 1);
267}
268
Simon Glassa2723ae2015-01-25 08:26:55 -0700269static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
Simon Glassc7a58902014-12-10 08:55:47 -0700270 struct udevice **devp)
271{
Simon Glass713c3f02015-01-25 08:27:13 -0700272 struct dm_i2c_chip *chip;
Simon Glassc7a58902014-12-10 08:55:47 -0700273 char name[30], *str;
274 struct udevice *dev;
275 int ret;
276
277 snprintf(name, sizeof(name), "generic_%x", chip_addr);
278 str = strdup(name);
Simon Glassb75c4492015-02-18 14:10:28 -0700279 if (!str)
280 return -ENOMEM;
Simon Glassc7a58902014-12-10 08:55:47 -0700281 ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
282 debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
283 if (ret)
284 goto err_bind;
285
286 /* Tell the device what we know about it */
Simon Glass713c3f02015-01-25 08:27:13 -0700287 chip = dev_get_parent_platdata(dev);
288 chip->chip_addr = chip_addr;
289 chip->offset_len = offset_len;
290 ret = device_probe(dev);
291 debug("%s: device_probe: ret=%d\n", __func__, ret);
Simon Glassc7a58902014-12-10 08:55:47 -0700292 if (ret)
293 goto err_probe;
294
295 *devp = dev;
296 return 0;
297
298err_probe:
Simon Glass713c3f02015-01-25 08:27:13 -0700299 /*
300 * If the device failed to probe, unbind it. There is nothing there
301 * on the bus so we don't want to leave it lying around
302 */
Simon Glassc7a58902014-12-10 08:55:47 -0700303 device_unbind(dev);
304err_bind:
305 free(str);
306 return ret;
307}
308
Simon Glassa2723ae2015-01-25 08:26:55 -0700309int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
310 struct udevice **devp)
Simon Glassc7a58902014-12-10 08:55:47 -0700311{
312 struct udevice *dev;
313
314 debug("%s: Searching bus '%s' for address %02x: ", __func__,
315 bus->name, chip_addr);
316 for (device_find_first_child(bus, &dev); dev;
317 device_find_next_child(&dev)) {
Simon Glass713c3f02015-01-25 08:27:13 -0700318 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700319 int ret;
320
Simon Glassc7a58902014-12-10 08:55:47 -0700321 if (chip->chip_addr == chip_addr) {
322 ret = device_probe(dev);
323 debug("found, ret=%d\n", ret);
324 if (ret)
325 return ret;
326 *devp = dev;
327 return 0;
328 }
329 }
330 debug("not found\n");
Simon Glassa2723ae2015-01-25 08:26:55 -0700331 return i2c_bind_driver(bus, chip_addr, offset_len, devp);
Simon Glassc7a58902014-12-10 08:55:47 -0700332}
333
Simon Glassa2723ae2015-01-25 08:26:55 -0700334int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
335 struct udevice **devp)
Simon Glassc7a58902014-12-10 08:55:47 -0700336{
337 struct udevice *bus;
338 int ret;
339
340 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
341 if (ret) {
342 debug("Cannot find I2C bus %d\n", busnum);
343 return ret;
344 }
Simon Glassa2723ae2015-01-25 08:26:55 -0700345 ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
Simon Glassc7a58902014-12-10 08:55:47 -0700346 if (ret) {
347 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
348 busnum);
349 return ret;
350 }
351
352 return 0;
353}
354
Simon Glass7d722762015-01-12 18:02:07 -0700355int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
356 struct udevice **devp)
Simon Glassc7a58902014-12-10 08:55:47 -0700357{
358 int ret;
359
360 *devp = NULL;
361
362 /* First probe that chip */
363 ret = i2c_probe_chip(bus, chip_addr, chip_flags);
364 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
365 chip_addr, ret);
366 if (ret)
367 return ret;
368
369 /* The chip was found, see if we have a driver, and probe it */
Simon Glassa2723ae2015-01-25 08:26:55 -0700370 ret = i2c_get_chip(bus, chip_addr, 1, devp);
Simon Glassc7a58902014-12-10 08:55:47 -0700371 debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
372
373 return ret;
374}
375
Simon Glasse4e8ff22015-02-05 21:41:32 -0700376int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
Simon Glassc7a58902014-12-10 08:55:47 -0700377{
378 struct dm_i2c_ops *ops = i2c_get_ops(bus);
Simon Glassde0977b2015-03-05 12:25:20 -0700379 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glassc7a58902014-12-10 08:55:47 -0700380 int ret;
381
382 /*
383 * If we have a method, call it. If not then the driver probably wants
384 * to deal with speed changes on the next transfer. It can easily read
385 * the current speed from this uclass
386 */
387 if (ops->set_bus_speed) {
388 ret = ops->set_bus_speed(bus, speed);
389 if (ret)
390 return ret;
391 }
392 i2c->speed_hz = speed;
393
394 return 0;
395}
396
Simon Glasse4e8ff22015-02-05 21:41:32 -0700397int dm_i2c_get_bus_speed(struct udevice *bus)
Simon Glassc7a58902014-12-10 08:55:47 -0700398{
399 struct dm_i2c_ops *ops = i2c_get_ops(bus);
Simon Glassde0977b2015-03-05 12:25:20 -0700400 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glassc7a58902014-12-10 08:55:47 -0700401
402 if (!ops->get_bus_speed)
403 return i2c->speed_hz;
404
405 return ops->get_bus_speed(bus);
406}
407
408int i2c_set_chip_flags(struct udevice *dev, uint flags)
409{
410 struct udevice *bus = dev->parent;
Simon Glass713c3f02015-01-25 08:27:13 -0700411 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700412 struct dm_i2c_ops *ops = i2c_get_ops(bus);
413 int ret;
414
415 if (ops->set_flags) {
416 ret = ops->set_flags(dev, flags);
417 if (ret)
418 return ret;
419 }
420 chip->flags = flags;
421
422 return 0;
423}
424
425int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
426{
Simon Glass713c3f02015-01-25 08:27:13 -0700427 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700428
429 *flagsp = chip->flags;
430
431 return 0;
432}
433
434int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
435{
Simon Glass713c3f02015-01-25 08:27:13 -0700436 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700437
438 if (offset_len > I2C_MAX_OFFSET_LEN)
439 return -EINVAL;
440 chip->offset_len = offset_len;
441
442 return 0;
443}
444
Simon Glass9a1589a2015-05-04 11:30:58 -0600445int i2c_get_chip_offset_len(struct udevice *dev)
446{
447 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
448
449 return chip->offset_len;
450}
451
Simon Glassc7a58902014-12-10 08:55:47 -0700452int i2c_deblock(struct udevice *bus)
453{
454 struct dm_i2c_ops *ops = i2c_get_ops(bus);
455
456 /*
457 * We could implement a software deblocking here if we could get
458 * access to the GPIOs used by I2C, and switch them to GPIO mode
459 * and then back to I2C. This is somewhat beyond our powers in
460 * driver model at present, so for now just fail.
461 *
462 * See https://patchwork.ozlabs.org/patch/399040/
463 */
464 if (!ops->deblock)
465 return -ENOSYS;
466
467 return ops->deblock(bus);
468}
469
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530470#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glassc7a58902014-12-10 08:55:47 -0700471int i2c_chip_ofdata_to_platdata(const void *blob, int node,
472 struct dm_i2c_chip *chip)
473{
Simon Glassf5428412015-01-26 20:29:37 -0700474 chip->offset_len = fdtdec_get_int(gd->fdt_blob, node,
475 "u-boot,i2c-offset-len", 1);
Simon Glassc7a58902014-12-10 08:55:47 -0700476 chip->flags = 0;
477 chip->chip_addr = fdtdec_get_int(gd->fdt_blob, node, "reg", -1);
478 if (chip->chip_addr == -1) {
479 debug("%s: I2C Node '%s' has no 'reg' property\n", __func__,
480 fdt_get_name(blob, node, NULL));
481 return -EINVAL;
482 }
483
484 return 0;
485}
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530486#endif
Simon Glassc7a58902014-12-10 08:55:47 -0700487
488static int i2c_post_probe(struct udevice *dev)
489{
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530490#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glassde0977b2015-03-05 12:25:20 -0700491 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700492
493 i2c->speed_hz = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
494 "clock-frequency", 100000);
495
Simon Glasse4e8ff22015-02-05 21:41:32 -0700496 return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530497#else
498 return 0;
499#endif
Simon Glassc7a58902014-12-10 08:55:47 -0700500}
501
Simon Glass713c3f02015-01-25 08:27:13 -0700502static int i2c_post_bind(struct udevice *dev)
Simon Glassc7a58902014-12-10 08:55:47 -0700503{
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530504#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glassc7a58902014-12-10 08:55:47 -0700505 /* Scan the bus for devices */
506 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530507#else
508 return 0;
509#endif
Simon Glassc7a58902014-12-10 08:55:47 -0700510}
511
Simon Glass713c3f02015-01-25 08:27:13 -0700512static int i2c_child_post_bind(struct udevice *dev)
513{
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530514#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass713c3f02015-01-25 08:27:13 -0700515 struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
516
517 if (dev->of_offset == -1)
518 return 0;
519
520 return i2c_chip_ofdata_to_platdata(gd->fdt_blob, dev->of_offset, plat);
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530521#else
522 return 0;
523#endif
Simon Glass713c3f02015-01-25 08:27:13 -0700524}
525
Simon Glassc7a58902014-12-10 08:55:47 -0700526UCLASS_DRIVER(i2c) = {
527 .id = UCLASS_I2C,
528 .name = "i2c",
Simon Glass0ccb0972015-01-25 08:27:05 -0700529 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glassc7a58902014-12-10 08:55:47 -0700530 .post_bind = i2c_post_bind,
531 .post_probe = i2c_post_probe,
Simon Glass713c3f02015-01-25 08:27:13 -0700532 .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
533 .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
534 .child_post_bind = i2c_child_post_bind,
Simon Glassc7a58902014-12-10 08:55:47 -0700535};
536
537UCLASS_DRIVER(i2c_generic) = {
538 .id = UCLASS_I2C_GENERIC,
539 .name = "i2c_generic",
540};
541
542U_BOOT_DRIVER(i2c_generic_chip_drv) = {
543 .name = "i2c_generic_chip_drv",
544 .id = UCLASS_I2C_GENERIC,
545};