blob: 1397f34dda0a6d6cf9d7a35eb2c2068e3d945611 [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>
Simon Glassc7a58902014-12-10 08:55:47 -070010#include <i2c.h>
11#include <malloc.h>
12#include <dm/device-internal.h>
13#include <dm/lists.h>
Simon Glassc7a58902014-12-10 08:55:47 -070014
15DECLARE_GLOBAL_DATA_PTR;
16
17#define I2C_MAX_OFFSET_LEN 4
18
Simon Glassa8e64c42015-07-02 18:15:39 -060019/* Useful debugging function */
20void 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
Simon Glassc7a58902014-12-10 08:55:47 -070035/**
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 */
45static 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
64static int i2c_read_bytewise(struct udevice *dev, uint offset,
65 uint8_t *buffer, int len)
66{
Simon Glass713c3f02015-01-25 08:27:13 -070067 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -070068 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
93static int i2c_write_bytewise(struct udevice *dev, uint offset,
94 const uint8_t *buffer, int len)
95{
Simon Glass713c3f02015-01-25 08:27:13 -070096 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -070097 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
Simon Glass7d722762015-01-12 18:02:07 -0700117int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
Simon Glassc7a58902014-12-10 08:55:47 -0700118{
Simon Glass713c3f02015-01-25 08:27:13 -0700119 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700120 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
Simon Glass7d722762015-01-12 18:02:07 -0700147int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
148 int len)
Simon Glassc7a58902014-12-10 08:55:47 -0700149{
Simon Glass713c3f02015-01-25 08:27:13 -0700150 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700151 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
Simon Glassf2818852015-07-02 18:15:42 -0600203int 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
Simon Glass0c702612015-04-20 12:37:14 -0600214int 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
226int 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
Simon Glassc7a58902014-12-10 08:55:47 -0700233/**
234 * i2c_probe_chip() - probe for a chip on a bus
235 *
236 * @bus: Bus to probe
237 * @chip_addr: Chip address to probe
238 * @flags: Flags for the chip
239 * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
240 * does not respond to probe
241 */
242static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
243 enum dm_i2c_chip_flags chip_flags)
244{
245 struct dm_i2c_ops *ops = i2c_get_ops(bus);
246 struct i2c_msg msg[1];
247 int ret;
248
249 if (ops->probe_chip) {
250 ret = ops->probe_chip(bus, chip_addr, chip_flags);
251 if (!ret || ret != -ENOSYS)
252 return ret;
253 }
254
255 if (!ops->xfer)
256 return -ENOSYS;
257
258 /* Probe with a zero-length message */
259 msg->addr = chip_addr;
260 msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
261 msg->len = 0;
262 msg->buf = NULL;
263
264 return ops->xfer(bus, msg, 1);
265}
266
Simon Glassa2723ae2015-01-25 08:26:55 -0700267static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
Simon Glassc7a58902014-12-10 08:55:47 -0700268 struct udevice **devp)
269{
Simon Glass713c3f02015-01-25 08:27:13 -0700270 struct dm_i2c_chip *chip;
Simon Glassc7a58902014-12-10 08:55:47 -0700271 char name[30], *str;
272 struct udevice *dev;
273 int ret;
274
275 snprintf(name, sizeof(name), "generic_%x", chip_addr);
276 str = strdup(name);
Simon Glassb75c4492015-02-18 14:10:28 -0700277 if (!str)
278 return -ENOMEM;
Simon Glassc7a58902014-12-10 08:55:47 -0700279 ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
280 debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
281 if (ret)
282 goto err_bind;
283
284 /* Tell the device what we know about it */
Simon Glass713c3f02015-01-25 08:27:13 -0700285 chip = dev_get_parent_platdata(dev);
286 chip->chip_addr = chip_addr;
287 chip->offset_len = offset_len;
288 ret = device_probe(dev);
289 debug("%s: device_probe: ret=%d\n", __func__, ret);
Simon Glassc7a58902014-12-10 08:55:47 -0700290 if (ret)
291 goto err_probe;
292
293 *devp = dev;
294 return 0;
295
296err_probe:
Simon Glass713c3f02015-01-25 08:27:13 -0700297 /*
298 * If the device failed to probe, unbind it. There is nothing there
299 * on the bus so we don't want to leave it lying around
300 */
Simon Glassc7a58902014-12-10 08:55:47 -0700301 device_unbind(dev);
302err_bind:
303 free(str);
304 return ret;
305}
306
Simon Glassa2723ae2015-01-25 08:26:55 -0700307int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
308 struct udevice **devp)
Simon Glassc7a58902014-12-10 08:55:47 -0700309{
310 struct udevice *dev;
311
312 debug("%s: Searching bus '%s' for address %02x: ", __func__,
313 bus->name, chip_addr);
314 for (device_find_first_child(bus, &dev); dev;
315 device_find_next_child(&dev)) {
Simon Glass713c3f02015-01-25 08:27:13 -0700316 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700317 int ret;
318
Simon Glassc7a58902014-12-10 08:55:47 -0700319 if (chip->chip_addr == chip_addr) {
320 ret = device_probe(dev);
321 debug("found, ret=%d\n", ret);
322 if (ret)
323 return ret;
324 *devp = dev;
325 return 0;
326 }
327 }
328 debug("not found\n");
Simon Glassa2723ae2015-01-25 08:26:55 -0700329 return i2c_bind_driver(bus, chip_addr, offset_len, devp);
Simon Glassc7a58902014-12-10 08:55:47 -0700330}
331
Simon Glassa2723ae2015-01-25 08:26:55 -0700332int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
333 struct udevice **devp)
Simon Glassc7a58902014-12-10 08:55:47 -0700334{
335 struct udevice *bus;
336 int ret;
337
338 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
339 if (ret) {
340 debug("Cannot find I2C bus %d\n", busnum);
341 return ret;
342 }
Simon Glassa2723ae2015-01-25 08:26:55 -0700343 ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
Simon Glassc7a58902014-12-10 08:55:47 -0700344 if (ret) {
345 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
346 busnum);
347 return ret;
348 }
349
350 return 0;
351}
352
Simon Glass7d722762015-01-12 18:02:07 -0700353int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
354 struct udevice **devp)
Simon Glassc7a58902014-12-10 08:55:47 -0700355{
356 int ret;
357
358 *devp = NULL;
359
360 /* First probe that chip */
361 ret = i2c_probe_chip(bus, chip_addr, chip_flags);
362 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
363 chip_addr, ret);
364 if (ret)
365 return ret;
366
367 /* The chip was found, see if we have a driver, and probe it */
Simon Glassa2723ae2015-01-25 08:26:55 -0700368 ret = i2c_get_chip(bus, chip_addr, 1, devp);
Simon Glassc7a58902014-12-10 08:55:47 -0700369 debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
370
371 return ret;
372}
373
Simon Glasse4e8ff22015-02-05 21:41:32 -0700374int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
Simon Glassc7a58902014-12-10 08:55:47 -0700375{
376 struct dm_i2c_ops *ops = i2c_get_ops(bus);
Simon Glassde0977b2015-03-05 12:25:20 -0700377 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glassc7a58902014-12-10 08:55:47 -0700378 int ret;
379
380 /*
381 * If we have a method, call it. If not then the driver probably wants
382 * to deal with speed changes on the next transfer. It can easily read
383 * the current speed from this uclass
384 */
385 if (ops->set_bus_speed) {
386 ret = ops->set_bus_speed(bus, speed);
387 if (ret)
388 return ret;
389 }
390 i2c->speed_hz = speed;
391
392 return 0;
393}
394
Simon Glasse4e8ff22015-02-05 21:41:32 -0700395int dm_i2c_get_bus_speed(struct udevice *bus)
Simon Glassc7a58902014-12-10 08:55:47 -0700396{
397 struct dm_i2c_ops *ops = i2c_get_ops(bus);
Simon Glassde0977b2015-03-05 12:25:20 -0700398 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glassc7a58902014-12-10 08:55:47 -0700399
400 if (!ops->get_bus_speed)
401 return i2c->speed_hz;
402
403 return ops->get_bus_speed(bus);
404}
405
406int i2c_set_chip_flags(struct udevice *dev, uint flags)
407{
408 struct udevice *bus = dev->parent;
Simon Glass713c3f02015-01-25 08:27:13 -0700409 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700410 struct dm_i2c_ops *ops = i2c_get_ops(bus);
411 int ret;
412
413 if (ops->set_flags) {
414 ret = ops->set_flags(dev, flags);
415 if (ret)
416 return ret;
417 }
418 chip->flags = flags;
419
420 return 0;
421}
422
423int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
424{
Simon Glass713c3f02015-01-25 08:27:13 -0700425 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700426
427 *flagsp = chip->flags;
428
429 return 0;
430}
431
432int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
433{
Simon Glass713c3f02015-01-25 08:27:13 -0700434 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700435
436 if (offset_len > I2C_MAX_OFFSET_LEN)
437 return -EINVAL;
438 chip->offset_len = offset_len;
439
440 return 0;
441}
442
Simon Glass9a1589a2015-05-04 11:30:58 -0600443int i2c_get_chip_offset_len(struct udevice *dev)
444{
445 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
446
447 return chip->offset_len;
448}
449
Simon Glassc7a58902014-12-10 08:55:47 -0700450int i2c_deblock(struct udevice *bus)
451{
452 struct dm_i2c_ops *ops = i2c_get_ops(bus);
453
454 /*
455 * We could implement a software deblocking here if we could get
456 * access to the GPIOs used by I2C, and switch them to GPIO mode
457 * and then back to I2C. This is somewhat beyond our powers in
458 * driver model at present, so for now just fail.
459 *
460 * See https://patchwork.ozlabs.org/patch/399040/
461 */
462 if (!ops->deblock)
463 return -ENOSYS;
464
465 return ops->deblock(bus);
466}
467
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530468#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass654e3a02017-05-18 20:09:30 -0600469int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip)
Simon Glassc7a58902014-12-10 08:55:47 -0700470{
Simon Glass654e3a02017-05-18 20:09:30 -0600471 int addr;
472
473 chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len",
474 1);
Simon Glassc7a58902014-12-10 08:55:47 -0700475 chip->flags = 0;
Simon Glass654e3a02017-05-18 20:09:30 -0600476 addr = dev_read_u32_default(dev, "reg", -1);
477 if (addr == -1) {
478 debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__,
479 dev_read_name(dev), dev->name);
Simon Glassc7a58902014-12-10 08:55:47 -0700480 return -EINVAL;
481 }
Simon Glass654e3a02017-05-18 20:09:30 -0600482 chip->chip_addr = addr;
Simon Glassc7a58902014-12-10 08:55:47 -0700483
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
Simon Glass654e3a02017-05-18 20:09:30 -0600493 i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency", 100000);
Simon Glassc7a58902014-12-10 08:55:47 -0700494
Simon Glasse4e8ff22015-02-05 21:41:32 -0700495 return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530496#else
497 return 0;
498#endif
Simon Glassc7a58902014-12-10 08:55:47 -0700499}
500
Simon Glass713c3f02015-01-25 08:27:13 -0700501static int i2c_child_post_bind(struct udevice *dev)
502{
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530503#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass713c3f02015-01-25 08:27:13 -0700504 struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
505
Simon Glass654e3a02017-05-18 20:09:30 -0600506 if (!dev_of_valid(dev))
Simon Glass713c3f02015-01-25 08:27:13 -0700507 return 0;
Simon Glass654e3a02017-05-18 20:09:30 -0600508 return i2c_chip_ofdata_to_platdata(dev, plat);
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530509#else
510 return 0;
511#endif
Simon Glass713c3f02015-01-25 08:27:13 -0700512}
513
Simon Glassc7a58902014-12-10 08:55:47 -0700514UCLASS_DRIVER(i2c) = {
515 .id = UCLASS_I2C,
516 .name = "i2c",
Simon Glass0ccb0972015-01-25 08:27:05 -0700517 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass18230342016-07-05 17:10:10 -0600518#if CONFIG_IS_ENABLED(OF_CONTROL)
519 .post_bind = dm_scan_fdt_dev,
520#endif
Simon Glassc7a58902014-12-10 08:55:47 -0700521 .post_probe = i2c_post_probe,
Simon Glass713c3f02015-01-25 08:27:13 -0700522 .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
523 .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
524 .child_post_bind = i2c_child_post_bind,
Simon Glassc7a58902014-12-10 08:55:47 -0700525};
526
527UCLASS_DRIVER(i2c_generic) = {
528 .id = UCLASS_I2C_GENERIC,
529 .name = "i2c_generic",
530};
531
532U_BOOT_DRIVER(i2c_generic_chip_drv) = {
533 .name = "i2c_generic_chip_drv",
534 .id = UCLASS_I2C_GENERIC,
535};