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