blob: eaba965fa31a64c999e31339e07c0acfbaf0fc84 [file] [log] [blame]
Simon Glassc6202d82014-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
21/**
22 * i2c_setup_offset() - Set up a new message with a chip offset
23 *
24 * @chip: Chip to use
25 * @offset: Byte offset within chip
26 * @offset_buf: Place to put byte offset
27 * @msg: Message buffer
28 * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
29 * message is still set up but will not contain an offset.
30 */
31static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
32 uint8_t offset_buf[], struct i2c_msg *msg)
33{
34 int offset_len;
35
36 msg->addr = chip->chip_addr;
37 msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
38 msg->len = chip->offset_len;
39 msg->buf = offset_buf;
40 if (!chip->offset_len)
41 return -EADDRNOTAVAIL;
42 assert(chip->offset_len <= I2C_MAX_OFFSET_LEN);
43 offset_len = chip->offset_len;
44 while (offset_len--)
45 *offset_buf++ = offset >> (8 * offset_len);
46
47 return 0;
48}
49
50static int i2c_read_bytewise(struct udevice *dev, uint offset,
51 uint8_t *buffer, int len)
52{
Simon Glasse6f66ec2015-01-25 08:27:13 -070053 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -070054 struct udevice *bus = dev_get_parent(dev);
55 struct dm_i2c_ops *ops = i2c_get_ops(bus);
56 struct i2c_msg msg[2], *ptr;
57 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
58 int ret;
59 int i;
60
61 for (i = 0; i < len; i++) {
62 if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
63 return -EINVAL;
64 ptr = msg + 1;
65 ptr->addr = chip->chip_addr;
66 ptr->flags = msg->flags | I2C_M_RD;
67 ptr->len = 1;
68 ptr->buf = &buffer[i];
69 ptr++;
70
71 ret = ops->xfer(bus, msg, ptr - msg);
72 if (ret)
73 return ret;
74 }
75
76 return 0;
77}
78
79static int i2c_write_bytewise(struct udevice *dev, uint offset,
80 const uint8_t *buffer, int len)
81{
Simon Glasse6f66ec2015-01-25 08:27:13 -070082 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -070083 struct udevice *bus = dev_get_parent(dev);
84 struct dm_i2c_ops *ops = i2c_get_ops(bus);
85 struct i2c_msg msg[1];
86 uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
87 int ret;
88 int i;
89
90 for (i = 0; i < len; i++) {
91 if (i2c_setup_offset(chip, offset + i, buf, msg))
92 return -EINVAL;
93 buf[msg->len++] = buffer[i];
94
95 ret = ops->xfer(bus, msg, 1);
96 if (ret)
97 return ret;
98 }
99
100 return 0;
101}
102
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700103int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
Simon Glassc6202d82014-12-10 08:55:47 -0700104{
Simon Glasse6f66ec2015-01-25 08:27:13 -0700105 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700106 struct udevice *bus = dev_get_parent(dev);
107 struct dm_i2c_ops *ops = i2c_get_ops(bus);
108 struct i2c_msg msg[2], *ptr;
109 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
110 int msg_count;
111
112 if (!ops->xfer)
113 return -ENOSYS;
114 if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
115 return i2c_read_bytewise(dev, offset, buffer, len);
116 ptr = msg;
117 if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
118 ptr++;
119
120 if (len) {
121 ptr->addr = chip->chip_addr;
122 ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
123 ptr->flags |= I2C_M_RD;
124 ptr->len = len;
125 ptr->buf = buffer;
126 ptr++;
127 }
128 msg_count = ptr - msg;
129
130 return ops->xfer(bus, msg, msg_count);
131}
132
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700133int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
134 int len)
Simon Glassc6202d82014-12-10 08:55:47 -0700135{
Simon Glasse6f66ec2015-01-25 08:27:13 -0700136 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700137 struct udevice *bus = dev_get_parent(dev);
138 struct dm_i2c_ops *ops = i2c_get_ops(bus);
139 struct i2c_msg msg[1];
140
141 if (!ops->xfer)
142 return -ENOSYS;
143
144 if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
145 return i2c_write_bytewise(dev, offset, buffer, len);
146 /*
147 * The simple approach would be to send two messages here: one to
148 * set the offset and one to write the bytes. However some drivers
149 * will not be expecting this, and some chips won't like how the
150 * driver presents this on the I2C bus.
151 *
152 * The API does not support separate offset and data. We could extend
153 * it with a flag indicating that there is data in the next message
154 * that needs to be processed in the same transaction. We could
155 * instead add an additional buffer to each message. For now, handle
156 * this in the uclass since it isn't clear what the impact on drivers
157 * would be with this extra complication. Unfortunately this means
158 * copying the message.
159 *
160 * Use the stack for small messages, malloc() for larger ones. We
161 * need to allow space for the offset (up to 4 bytes) and the message
162 * itself.
163 */
164 if (len < 64) {
165 uint8_t buf[I2C_MAX_OFFSET_LEN + len];
166
167 i2c_setup_offset(chip, offset, buf, msg);
168 msg->len += len;
169 memcpy(buf + chip->offset_len, buffer, len);
170
171 return ops->xfer(bus, msg, 1);
172 } else {
173 uint8_t *buf;
174 int ret;
175
176 buf = malloc(I2C_MAX_OFFSET_LEN + len);
177 if (!buf)
178 return -ENOMEM;
179 i2c_setup_offset(chip, offset, buf, msg);
180 msg->len += len;
181 memcpy(buf + chip->offset_len, buffer, len);
182
183 ret = ops->xfer(bus, msg, 1);
184 free(buf);
185 return ret;
186 }
187}
188
Simon Glassba3864f2015-04-20 12:37:14 -0600189int dm_i2c_reg_read(struct udevice *dev, uint offset)
190{
191 uint8_t val;
192 int ret;
193
194 ret = dm_i2c_read(dev, offset, &val, 1);
195 if (ret < 0)
196 return ret;
197
198 return val;
199}
200
201int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
202{
203 uint8_t val = value;
204
205 return dm_i2c_write(dev, offset, &val, 1);
206}
207
Simon Glassc6202d82014-12-10 08:55:47 -0700208/**
209 * i2c_probe_chip() - probe for a chip on a bus
210 *
211 * @bus: Bus to probe
212 * @chip_addr: Chip address to probe
213 * @flags: Flags for the chip
214 * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
215 * does not respond to probe
216 */
217static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
218 enum dm_i2c_chip_flags chip_flags)
219{
220 struct dm_i2c_ops *ops = i2c_get_ops(bus);
221 struct i2c_msg msg[1];
222 int ret;
223
224 if (ops->probe_chip) {
225 ret = ops->probe_chip(bus, chip_addr, chip_flags);
226 if (!ret || ret != -ENOSYS)
227 return ret;
228 }
229
230 if (!ops->xfer)
231 return -ENOSYS;
232
233 /* Probe with a zero-length message */
234 msg->addr = chip_addr;
235 msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
236 msg->len = 0;
237 msg->buf = NULL;
238
239 return ops->xfer(bus, msg, 1);
240}
241
Simon Glass25ab4b02015-01-25 08:26:55 -0700242static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
Simon Glassc6202d82014-12-10 08:55:47 -0700243 struct udevice **devp)
244{
Simon Glasse6f66ec2015-01-25 08:27:13 -0700245 struct dm_i2c_chip *chip;
Simon Glassc6202d82014-12-10 08:55:47 -0700246 char name[30], *str;
247 struct udevice *dev;
248 int ret;
249
250 snprintf(name, sizeof(name), "generic_%x", chip_addr);
251 str = strdup(name);
Simon Glass5c2d23b2015-02-18 14:10:28 -0700252 if (!str)
253 return -ENOMEM;
Simon Glassc6202d82014-12-10 08:55:47 -0700254 ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
255 debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
256 if (ret)
257 goto err_bind;
258
259 /* Tell the device what we know about it */
Simon Glasse6f66ec2015-01-25 08:27:13 -0700260 chip = dev_get_parent_platdata(dev);
261 chip->chip_addr = chip_addr;
262 chip->offset_len = offset_len;
263 ret = device_probe(dev);
264 debug("%s: device_probe: ret=%d\n", __func__, ret);
Simon Glassc6202d82014-12-10 08:55:47 -0700265 if (ret)
266 goto err_probe;
267
268 *devp = dev;
269 return 0;
270
271err_probe:
Simon Glasse6f66ec2015-01-25 08:27:13 -0700272 /*
273 * If the device failed to probe, unbind it. There is nothing there
274 * on the bus so we don't want to leave it lying around
275 */
Simon Glassc6202d82014-12-10 08:55:47 -0700276 device_unbind(dev);
277err_bind:
278 free(str);
279 return ret;
280}
281
Simon Glass25ab4b02015-01-25 08:26:55 -0700282int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
283 struct udevice **devp)
Simon Glassc6202d82014-12-10 08:55:47 -0700284{
285 struct udevice *dev;
286
287 debug("%s: Searching bus '%s' for address %02x: ", __func__,
288 bus->name, chip_addr);
289 for (device_find_first_child(bus, &dev); dev;
290 device_find_next_child(&dev)) {
Simon Glasse6f66ec2015-01-25 08:27:13 -0700291 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700292 int ret;
293
Simon Glassc6202d82014-12-10 08:55:47 -0700294 if (chip->chip_addr == chip_addr) {
295 ret = device_probe(dev);
296 debug("found, ret=%d\n", ret);
297 if (ret)
298 return ret;
299 *devp = dev;
300 return 0;
301 }
302 }
303 debug("not found\n");
Simon Glass25ab4b02015-01-25 08:26:55 -0700304 return i2c_bind_driver(bus, chip_addr, offset_len, devp);
Simon Glassc6202d82014-12-10 08:55:47 -0700305}
306
Simon Glass25ab4b02015-01-25 08:26:55 -0700307int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
308 struct udevice **devp)
Simon Glassc6202d82014-12-10 08:55:47 -0700309{
310 struct udevice *bus;
311 int ret;
312
313 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
314 if (ret) {
315 debug("Cannot find I2C bus %d\n", busnum);
316 return ret;
317 }
Simon Glass25ab4b02015-01-25 08:26:55 -0700318 ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
Simon Glassc6202d82014-12-10 08:55:47 -0700319 if (ret) {
320 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
321 busnum);
322 return ret;
323 }
324
325 return 0;
326}
327
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700328int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
329 struct udevice **devp)
Simon Glassc6202d82014-12-10 08:55:47 -0700330{
331 int ret;
332
333 *devp = NULL;
334
335 /* First probe that chip */
336 ret = i2c_probe_chip(bus, chip_addr, chip_flags);
337 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
338 chip_addr, ret);
339 if (ret)
340 return ret;
341
342 /* The chip was found, see if we have a driver, and probe it */
Simon Glass25ab4b02015-01-25 08:26:55 -0700343 ret = i2c_get_chip(bus, chip_addr, 1, devp);
Simon Glassc6202d82014-12-10 08:55:47 -0700344 debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
345
346 return ret;
347}
348
Simon Glassca88b9b2015-02-05 21:41:32 -0700349int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
Simon Glassc6202d82014-12-10 08:55:47 -0700350{
351 struct dm_i2c_ops *ops = i2c_get_ops(bus);
Simon Glasse564f052015-03-05 12:25:20 -0700352 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glassc6202d82014-12-10 08:55:47 -0700353 int ret;
354
355 /*
356 * If we have a method, call it. If not then the driver probably wants
357 * to deal with speed changes on the next transfer. It can easily read
358 * the current speed from this uclass
359 */
360 if (ops->set_bus_speed) {
361 ret = ops->set_bus_speed(bus, speed);
362 if (ret)
363 return ret;
364 }
365 i2c->speed_hz = speed;
366
367 return 0;
368}
369
Simon Glassca88b9b2015-02-05 21:41:32 -0700370int dm_i2c_get_bus_speed(struct udevice *bus)
Simon Glassc6202d82014-12-10 08:55:47 -0700371{
372 struct dm_i2c_ops *ops = i2c_get_ops(bus);
Simon Glasse564f052015-03-05 12:25:20 -0700373 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glassc6202d82014-12-10 08:55:47 -0700374
375 if (!ops->get_bus_speed)
376 return i2c->speed_hz;
377
378 return ops->get_bus_speed(bus);
379}
380
381int i2c_set_chip_flags(struct udevice *dev, uint flags)
382{
383 struct udevice *bus = dev->parent;
Simon Glasse6f66ec2015-01-25 08:27:13 -0700384 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700385 struct dm_i2c_ops *ops = i2c_get_ops(bus);
386 int ret;
387
388 if (ops->set_flags) {
389 ret = ops->set_flags(dev, flags);
390 if (ret)
391 return ret;
392 }
393 chip->flags = flags;
394
395 return 0;
396}
397
398int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
399{
Simon Glasse6f66ec2015-01-25 08:27:13 -0700400 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700401
402 *flagsp = chip->flags;
403
404 return 0;
405}
406
407int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
408{
Simon Glasse6f66ec2015-01-25 08:27:13 -0700409 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700410
411 if (offset_len > I2C_MAX_OFFSET_LEN)
412 return -EINVAL;
413 chip->offset_len = offset_len;
414
415 return 0;
416}
417
Simon Glass01501802015-05-04 11:30:58 -0600418int i2c_get_chip_offset_len(struct udevice *dev)
419{
420 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
421
422 return chip->offset_len;
423}
424
Simon Glassc6202d82014-12-10 08:55:47 -0700425int i2c_deblock(struct udevice *bus)
426{
427 struct dm_i2c_ops *ops = i2c_get_ops(bus);
428
429 /*
430 * We could implement a software deblocking here if we could get
431 * access to the GPIOs used by I2C, and switch them to GPIO mode
432 * and then back to I2C. This is somewhat beyond our powers in
433 * driver model at present, so for now just fail.
434 *
435 * See https://patchwork.ozlabs.org/patch/399040/
436 */
437 if (!ops->deblock)
438 return -ENOSYS;
439
440 return ops->deblock(bus);
441}
442
443int i2c_chip_ofdata_to_platdata(const void *blob, int node,
444 struct dm_i2c_chip *chip)
445{
Simon Glass7132b9f2015-01-26 20:29:37 -0700446 chip->offset_len = fdtdec_get_int(gd->fdt_blob, node,
447 "u-boot,i2c-offset-len", 1);
Simon Glassc6202d82014-12-10 08:55:47 -0700448 chip->flags = 0;
449 chip->chip_addr = fdtdec_get_int(gd->fdt_blob, node, "reg", -1);
450 if (chip->chip_addr == -1) {
451 debug("%s: I2C Node '%s' has no 'reg' property\n", __func__,
452 fdt_get_name(blob, node, NULL));
453 return -EINVAL;
454 }
455
456 return 0;
457}
458
459static int i2c_post_probe(struct udevice *dev)
460{
Simon Glasse564f052015-03-05 12:25:20 -0700461 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700462
463 i2c->speed_hz = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
464 "clock-frequency", 100000);
465
Simon Glassca88b9b2015-02-05 21:41:32 -0700466 return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
Simon Glassc6202d82014-12-10 08:55:47 -0700467}
468
Simon Glasse6f66ec2015-01-25 08:27:13 -0700469static int i2c_post_bind(struct udevice *dev)
Simon Glassc6202d82014-12-10 08:55:47 -0700470{
471 /* Scan the bus for devices */
472 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
473}
474
Simon Glasse6f66ec2015-01-25 08:27:13 -0700475static int i2c_child_post_bind(struct udevice *dev)
476{
477 struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
478
479 if (dev->of_offset == -1)
480 return 0;
481
482 return i2c_chip_ofdata_to_platdata(gd->fdt_blob, dev->of_offset, plat);
483}
484
Simon Glassc6202d82014-12-10 08:55:47 -0700485UCLASS_DRIVER(i2c) = {
486 .id = UCLASS_I2C,
487 .name = "i2c",
Simon Glass9cc36a22015-01-25 08:27:05 -0700488 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glassc6202d82014-12-10 08:55:47 -0700489 .post_bind = i2c_post_bind,
490 .post_probe = i2c_post_probe,
Simon Glasse6f66ec2015-01-25 08:27:13 -0700491 .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
492 .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
493 .child_post_bind = i2c_child_post_bind,
Simon Glassc6202d82014-12-10 08:55:47 -0700494};
495
496UCLASS_DRIVER(i2c_generic) = {
497 .id = UCLASS_I2C_GENERIC,
498 .name = "i2c_generic",
499};
500
501U_BOOT_DRIVER(i2c_generic_chip_drv) = {
502 .name = "i2c_generic_chip_drv",
503 .id = UCLASS_I2C_GENERIC,
504};