blob: 24e5ec68f2b4207d22ebaeb20a9e7e99d112d838 [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
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{
53 struct dm_i2c_chip *chip = dev_get_parentdata(dev);
54 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{
82 struct dm_i2c_chip *chip = dev_get_parentdata(dev);
83 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 Glass7d722762015-01-12 18:02:07 -0700103int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
Simon Glassc7a58902014-12-10 08:55:47 -0700104{
105 struct dm_i2c_chip *chip = dev_get_parentdata(dev);
106 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 Glass7d722762015-01-12 18:02:07 -0700133int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
134 int len)
Simon Glassc7a58902014-12-10 08:55:47 -0700135{
136 struct dm_i2c_chip *chip = dev_get_parentdata(dev);
137 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
189/**
190 * i2c_probe_chip() - probe for a chip on a bus
191 *
192 * @bus: Bus to probe
193 * @chip_addr: Chip address to probe
194 * @flags: Flags for the chip
195 * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
196 * does not respond to probe
197 */
198static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
199 enum dm_i2c_chip_flags chip_flags)
200{
201 struct dm_i2c_ops *ops = i2c_get_ops(bus);
202 struct i2c_msg msg[1];
203 int ret;
204
205 if (ops->probe_chip) {
206 ret = ops->probe_chip(bus, chip_addr, chip_flags);
207 if (!ret || ret != -ENOSYS)
208 return ret;
209 }
210
211 if (!ops->xfer)
212 return -ENOSYS;
213
214 /* Probe with a zero-length message */
215 msg->addr = chip_addr;
216 msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
217 msg->len = 0;
218 msg->buf = NULL;
219
220 return ops->xfer(bus, msg, 1);
221}
222
Simon Glassa2723ae2015-01-25 08:26:55 -0700223static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
Simon Glassc7a58902014-12-10 08:55:47 -0700224 struct udevice **devp)
225{
226 struct dm_i2c_chip chip;
227 char name[30], *str;
228 struct udevice *dev;
229 int ret;
230
231 snprintf(name, sizeof(name), "generic_%x", chip_addr);
232 str = strdup(name);
233 ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
234 debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
235 if (ret)
236 goto err_bind;
237
238 /* Tell the device what we know about it */
239 memset(&chip, '\0', sizeof(chip));
240 chip.chip_addr = chip_addr;
Simon Glassa2723ae2015-01-25 08:26:55 -0700241 chip.offset_len = offset_len;
Simon Glassc7a58902014-12-10 08:55:47 -0700242 ret = device_probe_child(dev, &chip);
243 debug("%s: device_probe_child: ret=%d\n", __func__, ret);
244 if (ret)
245 goto err_probe;
246
247 *devp = dev;
248 return 0;
249
250err_probe:
251 device_unbind(dev);
252err_bind:
253 free(str);
254 return ret;
255}
256
Simon Glassa2723ae2015-01-25 08:26:55 -0700257int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
258 struct udevice **devp)
Simon Glassc7a58902014-12-10 08:55:47 -0700259{
260 struct udevice *dev;
261
262 debug("%s: Searching bus '%s' for address %02x: ", __func__,
263 bus->name, chip_addr);
264 for (device_find_first_child(bus, &dev); dev;
265 device_find_next_child(&dev)) {
266 struct dm_i2c_chip store;
267 struct dm_i2c_chip *chip = dev_get_parentdata(dev);
268 int ret;
269
270 if (!chip) {
271 chip = &store;
272 i2c_chip_ofdata_to_platdata(gd->fdt_blob,
273 dev->of_offset, chip);
274 }
275 if (chip->chip_addr == chip_addr) {
276 ret = device_probe(dev);
277 debug("found, ret=%d\n", ret);
278 if (ret)
279 return ret;
280 *devp = dev;
281 return 0;
282 }
283 }
284 debug("not found\n");
Simon Glassa2723ae2015-01-25 08:26:55 -0700285 return i2c_bind_driver(bus, chip_addr, offset_len, devp);
Simon Glassc7a58902014-12-10 08:55:47 -0700286}
287
Simon Glassa2723ae2015-01-25 08:26:55 -0700288int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
289 struct udevice **devp)
Simon Glassc7a58902014-12-10 08:55:47 -0700290{
291 struct udevice *bus;
292 int ret;
293
294 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
295 if (ret) {
296 debug("Cannot find I2C bus %d\n", busnum);
297 return ret;
298 }
Simon Glassa2723ae2015-01-25 08:26:55 -0700299 ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
Simon Glassc7a58902014-12-10 08:55:47 -0700300 if (ret) {
301 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
302 busnum);
303 return ret;
304 }
305
306 return 0;
307}
308
Simon Glass7d722762015-01-12 18:02:07 -0700309int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
310 struct udevice **devp)
Simon Glassc7a58902014-12-10 08:55:47 -0700311{
312 int ret;
313
314 *devp = NULL;
315
316 /* First probe that chip */
317 ret = i2c_probe_chip(bus, chip_addr, chip_flags);
318 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
319 chip_addr, ret);
320 if (ret)
321 return ret;
322
323 /* The chip was found, see if we have a driver, and probe it */
Simon Glassa2723ae2015-01-25 08:26:55 -0700324 ret = i2c_get_chip(bus, chip_addr, 1, devp);
Simon Glassc7a58902014-12-10 08:55:47 -0700325 debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
326
327 return ret;
328}
329
330int i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
331{
332 struct dm_i2c_ops *ops = i2c_get_ops(bus);
333 struct dm_i2c_bus *i2c = bus->uclass_priv;
334 int ret;
335
336 /*
337 * If we have a method, call it. If not then the driver probably wants
338 * to deal with speed changes on the next transfer. It can easily read
339 * the current speed from this uclass
340 */
341 if (ops->set_bus_speed) {
342 ret = ops->set_bus_speed(bus, speed);
343 if (ret)
344 return ret;
345 }
346 i2c->speed_hz = speed;
347
348 return 0;
349}
350
351/*
352 * i2c_get_bus_speed:
353 *
354 * Returns speed of selected I2C bus in Hz
355 */
356int i2c_get_bus_speed(struct udevice *bus)
357{
358 struct dm_i2c_ops *ops = i2c_get_ops(bus);
359 struct dm_i2c_bus *i2c = bus->uclass_priv;
360
361 if (!ops->get_bus_speed)
362 return i2c->speed_hz;
363
364 return ops->get_bus_speed(bus);
365}
366
367int i2c_set_chip_flags(struct udevice *dev, uint flags)
368{
369 struct udevice *bus = dev->parent;
370 struct dm_i2c_chip *chip = dev_get_parentdata(dev);
371 struct dm_i2c_ops *ops = i2c_get_ops(bus);
372 int ret;
373
374 if (ops->set_flags) {
375 ret = ops->set_flags(dev, flags);
376 if (ret)
377 return ret;
378 }
379 chip->flags = flags;
380
381 return 0;
382}
383
384int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
385{
386 struct dm_i2c_chip *chip = dev_get_parentdata(dev);
387
388 *flagsp = chip->flags;
389
390 return 0;
391}
392
393int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
394{
395 struct dm_i2c_chip *chip = dev_get_parentdata(dev);
396
397 if (offset_len > I2C_MAX_OFFSET_LEN)
398 return -EINVAL;
399 chip->offset_len = offset_len;
400
401 return 0;
402}
403
404int i2c_deblock(struct udevice *bus)
405{
406 struct dm_i2c_ops *ops = i2c_get_ops(bus);
407
408 /*
409 * We could implement a software deblocking here if we could get
410 * access to the GPIOs used by I2C, and switch them to GPIO mode
411 * and then back to I2C. This is somewhat beyond our powers in
412 * driver model at present, so for now just fail.
413 *
414 * See https://patchwork.ozlabs.org/patch/399040/
415 */
416 if (!ops->deblock)
417 return -ENOSYS;
418
419 return ops->deblock(bus);
420}
421
422int i2c_chip_ofdata_to_platdata(const void *blob, int node,
423 struct dm_i2c_chip *chip)
424{
425 chip->offset_len = 1; /* default */
426 chip->flags = 0;
427 chip->chip_addr = fdtdec_get_int(gd->fdt_blob, node, "reg", -1);
428 if (chip->chip_addr == -1) {
429 debug("%s: I2C Node '%s' has no 'reg' property\n", __func__,
430 fdt_get_name(blob, node, NULL));
431 return -EINVAL;
432 }
433
434 return 0;
435}
436
437static int i2c_post_probe(struct udevice *dev)
438{
439 struct dm_i2c_bus *i2c = dev->uclass_priv;
440
441 i2c->speed_hz = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
442 "clock-frequency", 100000);
443
444 return i2c_set_bus_speed(dev, i2c->speed_hz);
445}
446
447int i2c_post_bind(struct udevice *dev)
448{
449 /* Scan the bus for devices */
450 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
451}
452
453UCLASS_DRIVER(i2c) = {
454 .id = UCLASS_I2C,
455 .name = "i2c",
456 .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
457 .post_bind = i2c_post_bind,
458 .post_probe = i2c_post_probe,
459};
460
461UCLASS_DRIVER(i2c_generic) = {
462 .id = UCLASS_I2C_GENERIC,
463 .name = "i2c_generic",
464};
465
466U_BOOT_DRIVER(i2c_generic_chip_drv) = {
467 .name = "i2c_generic_chip_drv",
468 .id = UCLASS_I2C_GENERIC,
469};