]> git.sur5r.net Git - u-boot/blob - drivers/i2c/i2c-uclass.c
dm: i2c: Add a message debug function
[u-boot] / drivers / i2c / i2c-uclass.c
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 <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
17 DECLARE_GLOBAL_DATA_PTR;
18
19 #define I2C_MAX_OFFSET_LEN      4
20
21 /* Useful debugging function */
22 void 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
37 /**
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  */
47 static 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
66 static int i2c_read_bytewise(struct udevice *dev, uint offset,
67                              uint8_t *buffer, int len)
68 {
69         struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
70         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
95 static int i2c_write_bytewise(struct udevice *dev, uint offset,
96                              const uint8_t *buffer, int len)
97 {
98         struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
99         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
119 int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
120 {
121         struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
122         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
149 int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
150                  int len)
151 {
152         struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
153         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
205 int dm_i2c_reg_read(struct udevice *dev, uint offset)
206 {
207         uint8_t val;
208         int ret;
209
210         ret = dm_i2c_read(dev, offset, &val, 1);
211         if (ret < 0)
212                 return ret;
213
214         return val;
215 }
216
217 int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
218 {
219         uint8_t val = value;
220
221         return dm_i2c_write(dev, offset, &val, 1);
222 }
223
224 /**
225  * i2c_probe_chip() - probe for a chip on a bus
226  *
227  * @bus:        Bus to probe
228  * @chip_addr:  Chip address to probe
229  * @flags:      Flags for the chip
230  * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
231  * does not respond to probe
232  */
233 static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
234                           enum dm_i2c_chip_flags chip_flags)
235 {
236         struct dm_i2c_ops *ops = i2c_get_ops(bus);
237         struct i2c_msg msg[1];
238         int ret;
239
240         if (ops->probe_chip) {
241                 ret = ops->probe_chip(bus, chip_addr, chip_flags);
242                 if (!ret || ret != -ENOSYS)
243                         return ret;
244         }
245
246         if (!ops->xfer)
247                 return -ENOSYS;
248
249         /* Probe with a zero-length message */
250         msg->addr = chip_addr;
251         msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
252         msg->len = 0;
253         msg->buf = NULL;
254
255         return ops->xfer(bus, msg, 1);
256 }
257
258 static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
259                            struct udevice **devp)
260 {
261         struct dm_i2c_chip *chip;
262         char name[30], *str;
263         struct udevice *dev;
264         int ret;
265
266         snprintf(name, sizeof(name), "generic_%x", chip_addr);
267         str = strdup(name);
268         if (!str)
269                 return -ENOMEM;
270         ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
271         debug("%s:  device_bind_driver: ret=%d\n", __func__, ret);
272         if (ret)
273                 goto err_bind;
274
275         /* Tell the device what we know about it */
276         chip = dev_get_parent_platdata(dev);
277         chip->chip_addr = chip_addr;
278         chip->offset_len = offset_len;
279         ret = device_probe(dev);
280         debug("%s:  device_probe: ret=%d\n", __func__, ret);
281         if (ret)
282                 goto err_probe;
283
284         *devp = dev;
285         return 0;
286
287 err_probe:
288         /*
289          * If the device failed to probe, unbind it. There is nothing there
290          * on the bus so we don't want to leave it lying around
291          */
292         device_unbind(dev);
293 err_bind:
294         free(str);
295         return ret;
296 }
297
298 int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
299                  struct udevice **devp)
300 {
301         struct udevice *dev;
302
303         debug("%s: Searching bus '%s' for address %02x: ", __func__,
304               bus->name, chip_addr);
305         for (device_find_first_child(bus, &dev); dev;
306                         device_find_next_child(&dev)) {
307                 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
308                 int ret;
309
310                 if (chip->chip_addr == chip_addr) {
311                         ret = device_probe(dev);
312                         debug("found, ret=%d\n", ret);
313                         if (ret)
314                                 return ret;
315                         *devp = dev;
316                         return 0;
317                 }
318         }
319         debug("not found\n");
320         return i2c_bind_driver(bus, chip_addr, offset_len, devp);
321 }
322
323 int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
324                             struct udevice **devp)
325 {
326         struct udevice *bus;
327         int ret;
328
329         ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
330         if (ret) {
331                 debug("Cannot find I2C bus %d\n", busnum);
332                 return ret;
333         }
334         ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
335         if (ret) {
336                 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
337                       busnum);
338                 return ret;
339         }
340
341         return 0;
342 }
343
344 int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
345                  struct udevice **devp)
346 {
347         int ret;
348
349         *devp = NULL;
350
351         /* First probe that chip */
352         ret = i2c_probe_chip(bus, chip_addr, chip_flags);
353         debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
354               chip_addr, ret);
355         if (ret)
356                 return ret;
357
358         /* The chip was found, see if we have a driver, and probe it */
359         ret = i2c_get_chip(bus, chip_addr, 1, devp);
360         debug("%s:  i2c_get_chip: ret=%d\n", __func__, ret);
361
362         return ret;
363 }
364
365 int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
366 {
367         struct dm_i2c_ops *ops = i2c_get_ops(bus);
368         struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
369         int ret;
370
371         /*
372          * If we have a method, call it. If not then the driver probably wants
373          * to deal with speed changes on the next transfer. It can easily read
374          * the current speed from this uclass
375          */
376         if (ops->set_bus_speed) {
377                 ret = ops->set_bus_speed(bus, speed);
378                 if (ret)
379                         return ret;
380         }
381         i2c->speed_hz = speed;
382
383         return 0;
384 }
385
386 int dm_i2c_get_bus_speed(struct udevice *bus)
387 {
388         struct dm_i2c_ops *ops = i2c_get_ops(bus);
389         struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
390
391         if (!ops->get_bus_speed)
392                 return i2c->speed_hz;
393
394         return ops->get_bus_speed(bus);
395 }
396
397 int i2c_set_chip_flags(struct udevice *dev, uint flags)
398 {
399         struct udevice *bus = dev->parent;
400         struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
401         struct dm_i2c_ops *ops = i2c_get_ops(bus);
402         int ret;
403
404         if (ops->set_flags) {
405                 ret = ops->set_flags(dev, flags);
406                 if (ret)
407                         return ret;
408         }
409         chip->flags = flags;
410
411         return 0;
412 }
413
414 int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
415 {
416         struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
417
418         *flagsp = chip->flags;
419
420         return 0;
421 }
422
423 int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
424 {
425         struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
426
427         if (offset_len > I2C_MAX_OFFSET_LEN)
428                 return -EINVAL;
429         chip->offset_len = offset_len;
430
431         return 0;
432 }
433
434 int i2c_get_chip_offset_len(struct udevice *dev)
435 {
436         struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
437
438         return chip->offset_len;
439 }
440
441 int i2c_deblock(struct udevice *bus)
442 {
443         struct dm_i2c_ops *ops = i2c_get_ops(bus);
444
445         /*
446          * We could implement a software deblocking here if we could get
447          * access to the GPIOs used by I2C, and switch them to GPIO mode
448          * and then back to I2C. This is somewhat beyond our powers in
449          * driver model at present, so for now just fail.
450          *
451          * See https://patchwork.ozlabs.org/patch/399040/
452          */
453         if (!ops->deblock)
454                 return -ENOSYS;
455
456         return ops->deblock(bus);
457 }
458
459 int i2c_chip_ofdata_to_platdata(const void *blob, int node,
460                                 struct dm_i2c_chip *chip)
461 {
462         chip->offset_len = fdtdec_get_int(gd->fdt_blob, node,
463                                           "u-boot,i2c-offset-len", 1);
464         chip->flags = 0;
465         chip->chip_addr = fdtdec_get_int(gd->fdt_blob, node, "reg", -1);
466         if (chip->chip_addr == -1) {
467                 debug("%s: I2C Node '%s' has no 'reg' property\n", __func__,
468                       fdt_get_name(blob, node, NULL));
469                 return -EINVAL;
470         }
471
472         return 0;
473 }
474
475 static int i2c_post_probe(struct udevice *dev)
476 {
477         struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
478
479         i2c->speed_hz = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
480                                      "clock-frequency", 100000);
481
482         return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
483 }
484
485 static int i2c_post_bind(struct udevice *dev)
486 {
487         /* Scan the bus for devices */
488         return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
489 }
490
491 static int i2c_child_post_bind(struct udevice *dev)
492 {
493         struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
494
495         if (dev->of_offset == -1)
496                 return 0;
497
498         return i2c_chip_ofdata_to_platdata(gd->fdt_blob, dev->of_offset, plat);
499 }
500
501 UCLASS_DRIVER(i2c) = {
502         .id             = UCLASS_I2C,
503         .name           = "i2c",
504         .flags          = DM_UC_FLAG_SEQ_ALIAS,
505         .post_bind      = i2c_post_bind,
506         .post_probe     = i2c_post_probe,
507         .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
508         .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
509         .child_post_bind = i2c_child_post_bind,
510 };
511
512 UCLASS_DRIVER(i2c_generic) = {
513         .id             = UCLASS_I2C_GENERIC,
514         .name           = "i2c_generic",
515 };
516
517 U_BOOT_DRIVER(i2c_generic_chip_drv) = {
518         .name           = "i2c_generic_chip_drv",
519         .id             = UCLASS_I2C_GENERIC,
520 };