blob: c88ad961e652b1019358df9237b8145df35494c4 [file] [log] [blame]
Googlere00b8eb2019-07-08 16:37:07 -07001/*
2 * Copyright (c) 2014 Google, Inc
Googler695f9d92023-09-11 15:38:29 +08003 *
4 * SPDX-License-Identifier: GPL-2.0+
Googlere00b8eb2019-07-08 16:37:07 -07005 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
Googler695f9d92023-09-11 15:38:29 +080010#include <fdtdec.h>
Googlere00b8eb2019-07-08 16:37:07 -070011#include <i2c.h>
12#include <malloc.h>
13#include <dm/device-internal.h>
14#include <dm/lists.h>
Googler695f9d92023-09-11 15:38:29 +080015#include <dm/root.h>
16#ifdef CONFIG_SYS_I2C_MESON
17#include <amlogic/i2c.h>
Googlere00b8eb2019-07-08 16:37:07 -070018#endif
19
Googler695f9d92023-09-11 15:38:29 +080020DECLARE_GLOBAL_DATA_PTR;
21
Googlere00b8eb2019-07-08 16:37:07 -070022#define I2C_MAX_OFFSET_LEN 4
23
Googlere00b8eb2019-07-08 16:37:07 -070024/**
25 * i2c_setup_offset() - Set up a new message with a chip offset
26 *
27 * @chip: Chip to use
28 * @offset: Byte offset within chip
29 * @offset_buf: Place to put byte offset
30 * @msg: Message buffer
31 * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
32 * message is still set up but will not contain an offset.
33 */
34static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
35 uint8_t offset_buf[], struct i2c_msg *msg)
36{
37 int offset_len;
38
39 msg->addr = chip->chip_addr;
40 msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
41 msg->len = chip->offset_len;
42 msg->buf = offset_buf;
43 if (!chip->offset_len)
44 return -EADDRNOTAVAIL;
45 assert(chip->offset_len <= I2C_MAX_OFFSET_LEN);
46 offset_len = chip->offset_len;
47 while (offset_len--)
48 *offset_buf++ = offset >> (8 * offset_len);
49
50 return 0;
51}
52
53static int i2c_read_bytewise(struct udevice *dev, uint offset,
54 uint8_t *buffer, int len)
55{
Googler695f9d92023-09-11 15:38:29 +080056 struct dm_i2c_chip *chip = dev_get_parentdata(dev);
Googlere00b8eb2019-07-08 16:37:07 -070057 struct udevice *bus = dev_get_parent(dev);
58 struct dm_i2c_ops *ops = i2c_get_ops(bus);
59 struct i2c_msg msg[2], *ptr;
60 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
61 int ret;
62 int i;
63
64 for (i = 0; i < len; i++) {
65 if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
66 return -EINVAL;
67 ptr = msg + 1;
68 ptr->addr = chip->chip_addr;
69 ptr->flags = msg->flags | I2C_M_RD;
70 ptr->len = 1;
71 ptr->buf = &buffer[i];
72 ptr++;
73
74 ret = ops->xfer(bus, msg, ptr - msg);
75 if (ret)
76 return ret;
77 }
78
79 return 0;
80}
81
82static int i2c_write_bytewise(struct udevice *dev, uint offset,
83 const uint8_t *buffer, int len)
84{
Googler695f9d92023-09-11 15:38:29 +080085 struct dm_i2c_chip *chip = dev_get_parentdata(dev);
Googlere00b8eb2019-07-08 16:37:07 -070086 struct udevice *bus = dev_get_parent(dev);
87 struct dm_i2c_ops *ops = i2c_get_ops(bus);
88 struct i2c_msg msg[1];
89 uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
90 int ret;
91 int i;
92
93 for (i = 0; i < len; i++) {
94 if (i2c_setup_offset(chip, offset + i, buf, msg))
95 return -EINVAL;
96 buf[msg->len++] = buffer[i];
97
98 ret = ops->xfer(bus, msg, 1);
99 if (ret)
100 return ret;
101 }
102
103 return 0;
104}
105
Googler695f9d92023-09-11 15:38:29 +0800106int i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
Googlere00b8eb2019-07-08 16:37:07 -0700107{
Googler695f9d92023-09-11 15:38:29 +0800108 struct dm_i2c_chip *chip = dev_get_parentdata(dev);
Googlere00b8eb2019-07-08 16:37:07 -0700109 struct udevice *bus = dev_get_parent(dev);
110 struct dm_i2c_ops *ops = i2c_get_ops(bus);
111 struct i2c_msg msg[2], *ptr;
112 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
113 int msg_count;
114
115 if (!ops->xfer)
116 return -ENOSYS;
117 if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
118 return i2c_read_bytewise(dev, offset, buffer, len);
119 ptr = msg;
120 if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
121 ptr++;
122
123 if (len) {
124 ptr->addr = chip->chip_addr;
125 ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
126 ptr->flags |= I2C_M_RD;
127 ptr->len = len;
128 ptr->buf = buffer;
129 ptr++;
130 }
131 msg_count = ptr - msg;
132
133 return ops->xfer(bus, msg, msg_count);
134}
135
Googler695f9d92023-09-11 15:38:29 +0800136int i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer, int len)
Googlere00b8eb2019-07-08 16:37:07 -0700137{
Googler695f9d92023-09-11 15:38:29 +0800138 struct dm_i2c_chip *chip = dev_get_parentdata(dev);
Googlere00b8eb2019-07-08 16:37:07 -0700139 struct udevice *bus = dev_get_parent(dev);
140 struct dm_i2c_ops *ops = i2c_get_ops(bus);
141 struct i2c_msg msg[1];
142
143 if (!ops->xfer)
144 return -ENOSYS;
145
146 if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
147 return i2c_write_bytewise(dev, offset, buffer, len);
148 /*
149 * The simple approach would be to send two messages here: one to
150 * set the offset and one to write the bytes. However some drivers
151 * will not be expecting this, and some chips won't like how the
152 * driver presents this on the I2C bus.
153 *
154 * The API does not support separate offset and data. We could extend
155 * it with a flag indicating that there is data in the next message
156 * that needs to be processed in the same transaction. We could
157 * instead add an additional buffer to each message. For now, handle
158 * this in the uclass since it isn't clear what the impact on drivers
159 * would be with this extra complication. Unfortunately this means
160 * copying the message.
161 *
162 * Use the stack for small messages, malloc() for larger ones. We
163 * need to allow space for the offset (up to 4 bytes) and the message
164 * itself.
165 */
166 if (len < 64) {
167 uint8_t buf[I2C_MAX_OFFSET_LEN + len];
168
169 i2c_setup_offset(chip, offset, buf, msg);
170 msg->len += len;
171 memcpy(buf + chip->offset_len, buffer, len);
172
173 return ops->xfer(bus, msg, 1);
174 } else {
175 uint8_t *buf;
176 int ret;
177
178 buf = malloc(I2C_MAX_OFFSET_LEN + len);
179 if (!buf)
180 return -ENOMEM;
181 i2c_setup_offset(chip, offset, buf, msg);
182 msg->len += len;
183 memcpy(buf + chip->offset_len, buffer, len);
184
185 ret = ops->xfer(bus, msg, 1);
186 free(buf);
187 return ret;
188 }
189}
190
Googlere00b8eb2019-07-08 16:37:07 -0700191/**
192 * i2c_probe_chip() - probe for a chip on a bus
193 *
194 * @bus: Bus to probe
195 * @chip_addr: Chip address to probe
196 * @flags: Flags for the chip
197 * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
198 * does not respond to probe
199 */
200static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
201 enum dm_i2c_chip_flags chip_flags)
202{
203 struct dm_i2c_ops *ops = i2c_get_ops(bus);
204 struct i2c_msg msg[1];
205 int ret;
206
207 if (ops->probe_chip) {
208 ret = ops->probe_chip(bus, chip_addr, chip_flags);
209 if (!ret || ret != -ENOSYS)
210 return ret;
211 }
212
213 if (!ops->xfer)
214 return -ENOSYS;
215
216 /* Probe with a zero-length message */
217 msg->addr = chip_addr;
218 msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
219 msg->len = 0;
220 msg->buf = NULL;
221
222 return ops->xfer(bus, msg, 1);
223}
224
Googler695f9d92023-09-11 15:38:29 +0800225static int i2c_bind_driver(struct udevice *bus, uint chip_addr,
Googlere00b8eb2019-07-08 16:37:07 -0700226 struct udevice **devp)
227{
Googler695f9d92023-09-11 15:38:29 +0800228 struct dm_i2c_chip chip;
Googlere00b8eb2019-07-08 16:37:07 -0700229 char name[30], *str;
230 struct udevice *dev;
231 int ret;
232
233 snprintf(name, sizeof(name), "generic_%x", chip_addr);
234 str = strdup(name);
Googlere00b8eb2019-07-08 16:37:07 -0700235 ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
236 debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
237 if (ret)
238 goto err_bind;
239
240 /* Tell the device what we know about it */
Googler695f9d92023-09-11 15:38:29 +0800241 memset(&chip, '\0', sizeof(chip));
242 chip.chip_addr = chip_addr;
243 chip.offset_len = 1; /* we assume */
244 ret = device_probe_child(dev, &chip);
245 debug("%s: device_probe_child: ret=%d\n", __func__, ret);
Googlere00b8eb2019-07-08 16:37:07 -0700246 if (ret)
247 goto err_probe;
248
249 *devp = dev;
250 return 0;
251
252err_probe:
Googlere00b8eb2019-07-08 16:37:07 -0700253 device_unbind(dev);
254err_bind:
255 free(str);
256 return ret;
257}
258
Googler695f9d92023-09-11 15:38:29 +0800259int i2c_get_chip(struct udevice *bus, uint chip_addr, struct udevice **devp)
Googlere00b8eb2019-07-08 16:37:07 -0700260{
261 struct udevice *dev;
262
263 debug("%s: Searching bus '%s' for address %02x: ", __func__,
264 bus->name, chip_addr);
265 for (device_find_first_child(bus, &dev); dev;
266 device_find_next_child(&dev)) {
Googler695f9d92023-09-11 15:38:29 +0800267 struct dm_i2c_chip store;
268 struct dm_i2c_chip *chip = dev_get_parentdata(dev);
Googlere00b8eb2019-07-08 16:37:07 -0700269 int ret;
270
Googler695f9d92023-09-11 15:38:29 +0800271 if (!chip) {
272 chip = &store;
273 i2c_chip_ofdata_to_platdata(gd->fdt_blob,
274 dev->of_offset, chip);
275 }
Googlere00b8eb2019-07-08 16:37:07 -0700276 if (chip->chip_addr == chip_addr) {
277 ret = device_probe(dev);
278 debug("found, ret=%d\n", ret);
279 if (ret)
280 return ret;
281 *devp = dev;
282 return 0;
283 }
284 }
285 debug("not found\n");
Googler695f9d92023-09-11 15:38:29 +0800286 return i2c_bind_driver(bus, chip_addr, devp);
Googlere00b8eb2019-07-08 16:37:07 -0700287}
288
Googler695f9d92023-09-11 15:38:29 +0800289int i2c_get_chip_for_busnum(int busnum, int chip_addr, struct udevice **devp)
Googlere00b8eb2019-07-08 16:37:07 -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 }
Googler695f9d92023-09-11 15:38:29 +0800299 ret = i2c_get_chip(bus, chip_addr, devp);
Googlere00b8eb2019-07-08 16:37:07 -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
Googler695f9d92023-09-11 15:38:29 +0800309int i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
310 struct udevice **devp)
Googlere00b8eb2019-07-08 16:37:07 -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 */
Googler695f9d92023-09-11 15:38:29 +0800324 ret = i2c_get_chip(bus, chip_addr, devp);
Googlere00b8eb2019-07-08 16:37:07 -0700325 debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
326
327 return ret;
328}
329
Googler695f9d92023-09-11 15:38:29 +0800330int i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
Googlere00b8eb2019-07-08 16:37:07 -0700331{
332 struct dm_i2c_ops *ops = i2c_get_ops(bus);
Googler695f9d92023-09-11 15:38:29 +0800333 struct dm_i2c_bus *i2c = bus->uclass_priv;
Googlere00b8eb2019-07-08 16:37:07 -0700334 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
Googler695f9d92023-09-11 15:38:29 +0800351/*
352 * i2c_get_bus_speed:
353 *
354 * Returns speed of selected I2C bus in Hz
355 */
356int i2c_get_bus_speed(struct udevice *bus)
Googlere00b8eb2019-07-08 16:37:07 -0700357{
358 struct dm_i2c_ops *ops = i2c_get_ops(bus);
Googler695f9d92023-09-11 15:38:29 +0800359 struct dm_i2c_bus *i2c = bus->uclass_priv;
Googlere00b8eb2019-07-08 16:37:07 -0700360
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;
Googler695f9d92023-09-11 15:38:29 +0800370 struct dm_i2c_chip *chip = dev_get_parentdata(dev);
Googlere00b8eb2019-07-08 16:37:07 -0700371 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{
Googler695f9d92023-09-11 15:38:29 +0800386 struct dm_i2c_chip *chip = dev_get_parentdata(dev);
Googlere00b8eb2019-07-08 16:37:07 -0700387
388 *flagsp = chip->flags;
389
390 return 0;
391}
392
393int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
394{
Googler695f9d92023-09-11 15:38:29 +0800395 struct dm_i2c_chip *chip = dev_get_parentdata(dev);
Googlere00b8eb2019-07-08 16:37:07 -0700396
397 if (offset_len > I2C_MAX_OFFSET_LEN)
398 return -EINVAL;
399 chip->offset_len = offset_len;
400
401 return 0;
402}
403
Googlere00b8eb2019-07-08 16:37:07 -0700404int i2c_deblock(struct udevice *bus)
405{
406 struct dm_i2c_ops *ops = i2c_get_ops(bus);
407
Googler695f9d92023-09-11 15:38:29 +0800408 /*
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 */
Googlere00b8eb2019-07-08 16:37:07 -0700416 if (!ops->deblock)
Googler695f9d92023-09-11 15:38:29 +0800417 return -ENOSYS;
Googlere00b8eb2019-07-08 16:37:07 -0700418
419 return ops->deblock(bus);
420}
421
Googler695f9d92023-09-11 15:38:29 +0800422int i2c_chip_ofdata_to_platdata(const void *blob, int node,
423 struct dm_i2c_chip *chip)
Googlere00b8eb2019-07-08 16:37:07 -0700424{
Googler695f9d92023-09-11 15:38:29 +0800425 chip->offset_len = 1; /* default */
Googlere00b8eb2019-07-08 16:37:07 -0700426 chip->flags = 0;
Googler695f9d92023-09-11 15:38:29 +0800427 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));
Googlere00b8eb2019-07-08 16:37:07 -0700431 return -EINVAL;
432 }
Googlere00b8eb2019-07-08 16:37:07 -0700433
434 return 0;
435}
Googlere00b8eb2019-07-08 16:37:07 -0700436
437static int i2c_post_probe(struct udevice *dev)
438{
Googler695f9d92023-09-11 15:38:29 +0800439 struct dm_i2c_bus *i2c = dev->uclass_priv;
Googlere00b8eb2019-07-08 16:37:07 -0700440
Googler695f9d92023-09-11 15:38:29 +0800441 i2c->speed_hz = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
442 "clock-frequency", 100000);
Googlere00b8eb2019-07-08 16:37:07 -0700443
Googler695f9d92023-09-11 15:38:29 +0800444#ifdef CONFIG_SYS_I2C_MESON
445 i2c->speed_hz = ((struct meson_i2c_platdata *)dev->platdata)->clock_frequency;
Googlerfc3e29a2022-11-22 14:17:45 +0800446#endif
Googler695f9d92023-09-11 15:38:29 +0800447
448 return i2c_set_bus_speed(dev, i2c->speed_hz);
Googlerfc3e29a2022-11-22 14:17:45 +0800449}
Googler15b35d92022-10-03 17:48:21 +0800450
Googler695f9d92023-09-11 15:38:29 +0800451static unsigned int i2c_req_seq = 0;
452
453int i2c_post_bind(struct udevice *dev)
Googler25e92cf2023-12-13 10:05:01 +0000454{
Googler695f9d92023-09-11 15:38:29 +0800455#ifdef CONFIG_OF_CONTROL
456 /* Scan the bus for devices */
457 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
Googler25e92cf2023-12-13 10:05:01 +0000458#else
Googler695f9d92023-09-11 15:38:29 +0800459 dev->req_seq = i2c_req_seq++;
Googler25e92cf2023-12-13 10:05:01 +0000460 return 0;
461#endif
Googler695f9d92023-09-11 15:38:29 +0800462
Googler25e92cf2023-12-13 10:05:01 +0000463}
Googler40bc9d02023-12-15 16:42:49 +0800464
Googler695f9d92023-09-11 15:38:29 +0800465
Googlere00b8eb2019-07-08 16:37:07 -0700466UCLASS_DRIVER(i2c) = {
467 .id = UCLASS_I2C,
468 .name = "i2c",
Googler25e92cf2023-12-13 10:05:01 +0000469 .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
Googler695f9d92023-09-11 15:38:29 +0800470 .post_bind = i2c_post_bind,
471 .post_probe = i2c_post_probe,
Googlere00b8eb2019-07-08 16:37:07 -0700472};
473
474UCLASS_DRIVER(i2c_generic) = {
475 .id = UCLASS_I2C_GENERIC,
476 .name = "i2c_generic",
477};
478
479U_BOOT_DRIVER(i2c_generic_chip_drv) = {
480 .name = "i2c_generic_chip_drv",
481 .id = UCLASS_I2C_GENERIC,
482};