xref: /openbmc/u-boot/drivers/i2c/i2c-uclass.c (revision 474e9312a803ee36dc984217b6e9b7190eca9865)
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 <i2c.h>
11 #include <malloc.h>
12 #include <dm/device-internal.h>
13 #include <dm/lists.h>
14 #include <dm/pinctrl.h>
15 #ifdef CONFIG_DM_GPIO
16 #include <asm/gpio.h>
17 #endif
18 
19 #define I2C_MAX_OFFSET_LEN	4
20 
21 enum {
22 	PIN_SDA = 0,
23 	PIN_SCL,
24 	PIN_COUNT,
25 };
26 
27 /* Useful debugging function */
28 void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
29 {
30 	int i;
31 
32 	for (i = 0; i < nmsgs; i++) {
33 		struct i2c_msg *m = &msg[i];
34 
35 		printf("   %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W",
36 		       msg->addr, msg->len);
37 		if (!(m->flags & I2C_M_RD))
38 			printf(": %x", m->buf[0]);
39 		printf("\n");
40 	}
41 }
42 
43 /**
44  * i2c_setup_offset() - Set up a new message with a chip offset
45  *
46  * @chip:	Chip to use
47  * @offset:	Byte offset within chip
48  * @offset_buf:	Place to put byte offset
49  * @msg:	Message buffer
50  * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
51  * message is still set up but will not contain an offset.
52  */
53 static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
54 			    uint8_t offset_buf[], struct i2c_msg *msg)
55 {
56 	int offset_len;
57 
58 	msg->addr = chip->chip_addr;
59 	msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
60 	msg->len = chip->offset_len;
61 	msg->buf = offset_buf;
62 	if (!chip->offset_len)
63 		return -EADDRNOTAVAIL;
64 	assert(chip->offset_len <= I2C_MAX_OFFSET_LEN);
65 	offset_len = chip->offset_len;
66 	while (offset_len--)
67 		*offset_buf++ = offset >> (8 * offset_len);
68 
69 	return 0;
70 }
71 
72 static int i2c_read_bytewise(struct udevice *dev, uint offset,
73 			     uint8_t *buffer, int len)
74 {
75 	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
76 	struct udevice *bus = dev_get_parent(dev);
77 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
78 	struct i2c_msg msg[2], *ptr;
79 	uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
80 	int ret;
81 	int i;
82 
83 	for (i = 0; i < len; i++) {
84 		if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
85 			return -EINVAL;
86 		ptr = msg + 1;
87 		ptr->addr = chip->chip_addr;
88 		ptr->flags = msg->flags | I2C_M_RD;
89 		ptr->len = 1;
90 		ptr->buf = &buffer[i];
91 		ptr++;
92 
93 		ret = ops->xfer(bus, msg, ptr - msg);
94 		if (ret)
95 			return ret;
96 	}
97 
98 	return 0;
99 }
100 
101 static int i2c_write_bytewise(struct udevice *dev, uint offset,
102 			     const uint8_t *buffer, int len)
103 {
104 	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
105 	struct udevice *bus = dev_get_parent(dev);
106 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
107 	struct i2c_msg msg[1];
108 	uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
109 	int ret;
110 	int i;
111 
112 	for (i = 0; i < len; i++) {
113 		if (i2c_setup_offset(chip, offset + i, buf, msg))
114 			return -EINVAL;
115 		buf[msg->len++] = buffer[i];
116 
117 		ret = ops->xfer(bus, msg, 1);
118 		if (ret)
119 			return ret;
120 	}
121 
122 	return 0;
123 }
124 
125 int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
126 {
127 	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
128 	struct udevice *bus = dev_get_parent(dev);
129 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
130 	struct i2c_msg msg[2], *ptr;
131 	uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
132 	int msg_count;
133 
134 	if (!ops->xfer)
135 		return -ENOSYS;
136 	if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
137 		return i2c_read_bytewise(dev, offset, buffer, len);
138 	ptr = msg;
139 	if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
140 		ptr++;
141 
142 	if (len) {
143 		ptr->addr = chip->chip_addr;
144 		ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
145 		ptr->flags |= I2C_M_RD;
146 		ptr->len = len;
147 		ptr->buf = buffer;
148 		ptr++;
149 	}
150 	msg_count = ptr - msg;
151 
152 	return ops->xfer(bus, msg, msg_count);
153 }
154 
155 int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
156 		 int len)
157 {
158 	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
159 	struct udevice *bus = dev_get_parent(dev);
160 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
161 	struct i2c_msg msg[1];
162 
163 	if (!ops->xfer)
164 		return -ENOSYS;
165 
166 	if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
167 		return i2c_write_bytewise(dev, offset, buffer, len);
168 	/*
169 	 * The simple approach would be to send two messages here: one to
170 	 * set the offset and one to write the bytes. However some drivers
171 	 * will not be expecting this, and some chips won't like how the
172 	 * driver presents this on the I2C bus.
173 	 *
174 	 * The API does not support separate offset and data. We could extend
175 	 * it with a flag indicating that there is data in the next message
176 	 * that needs to be processed in the same transaction. We could
177 	 * instead add an additional buffer to each message. For now, handle
178 	 * this in the uclass since it isn't clear what the impact on drivers
179 	 * would be with this extra complication. Unfortunately this means
180 	 * copying the message.
181 	 *
182 	 * Use the stack for small messages, malloc() for larger ones. We
183 	 * need to allow space for the offset (up to 4 bytes) and the message
184 	 * itself.
185 	 */
186 	if (len < 64) {
187 		uint8_t buf[I2C_MAX_OFFSET_LEN + len];
188 
189 		i2c_setup_offset(chip, offset, buf, msg);
190 		msg->len += len;
191 		memcpy(buf + chip->offset_len, buffer, len);
192 
193 		return ops->xfer(bus, msg, 1);
194 	} else {
195 		uint8_t *buf;
196 		int ret;
197 
198 		buf = malloc(I2C_MAX_OFFSET_LEN + len);
199 		if (!buf)
200 			return -ENOMEM;
201 		i2c_setup_offset(chip, offset, buf, msg);
202 		msg->len += len;
203 		memcpy(buf + chip->offset_len, buffer, len);
204 
205 		ret = ops->xfer(bus, msg, 1);
206 		free(buf);
207 		return ret;
208 	}
209 }
210 
211 int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
212 {
213 	struct udevice *bus = dev_get_parent(dev);
214 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
215 
216 	if (!ops->xfer)
217 		return -ENOSYS;
218 
219 	return ops->xfer(bus, msg, nmsgs);
220 }
221 
222 int dm_i2c_reg_read(struct udevice *dev, uint offset)
223 {
224 	uint8_t val;
225 	int ret;
226 
227 	ret = dm_i2c_read(dev, offset, &val, 1);
228 	if (ret < 0)
229 		return ret;
230 
231 	return val;
232 }
233 
234 int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
235 {
236 	uint8_t val = value;
237 
238 	return dm_i2c_write(dev, offset, &val, 1);
239 }
240 
241 /**
242  * i2c_probe_chip() - probe for a chip on a bus
243  *
244  * @bus:	Bus to probe
245  * @chip_addr:	Chip address to probe
246  * @flags:	Flags for the chip
247  * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
248  * does not respond to probe
249  */
250 static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
251 			  enum dm_i2c_chip_flags chip_flags)
252 {
253 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
254 	struct i2c_msg msg[1];
255 	int ret;
256 
257 	if (ops->probe_chip) {
258 		ret = ops->probe_chip(bus, chip_addr, chip_flags);
259 		if (!ret || ret != -ENOSYS)
260 			return ret;
261 	}
262 
263 	if (!ops->xfer)
264 		return -ENOSYS;
265 
266 	/* Probe with a zero-length message */
267 	msg->addr = chip_addr;
268 	msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
269 	msg->len = 0;
270 	msg->buf = NULL;
271 
272 	return ops->xfer(bus, msg, 1);
273 }
274 
275 static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
276 			   struct udevice **devp)
277 {
278 	struct dm_i2c_chip *chip;
279 	char name[30], *str;
280 	struct udevice *dev;
281 	int ret;
282 
283 	snprintf(name, sizeof(name), "generic_%x", chip_addr);
284 	str = strdup(name);
285 	if (!str)
286 		return -ENOMEM;
287 	ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
288 	debug("%s:  device_bind_driver: ret=%d\n", __func__, ret);
289 	if (ret)
290 		goto err_bind;
291 
292 	/* Tell the device what we know about it */
293 	chip = dev_get_parent_platdata(dev);
294 	chip->chip_addr = chip_addr;
295 	chip->offset_len = offset_len;
296 	ret = device_probe(dev);
297 	debug("%s:  device_probe: ret=%d\n", __func__, ret);
298 	if (ret)
299 		goto err_probe;
300 
301 	*devp = dev;
302 	return 0;
303 
304 err_probe:
305 	/*
306 	 * If the device failed to probe, unbind it. There is nothing there
307 	 * on the bus so we don't want to leave it lying around
308 	 */
309 	device_unbind(dev);
310 err_bind:
311 	free(str);
312 	return ret;
313 }
314 
315 int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
316 		 struct udevice **devp)
317 {
318 	struct udevice *dev;
319 
320 	debug("%s: Searching bus '%s' for address %02x: ", __func__,
321 	      bus->name, chip_addr);
322 	for (device_find_first_child(bus, &dev); dev;
323 			device_find_next_child(&dev)) {
324 		struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
325 		int ret;
326 
327 		if (chip->chip_addr == chip_addr) {
328 			ret = device_probe(dev);
329 			debug("found, ret=%d\n", ret);
330 			if (ret)
331 				return ret;
332 			*devp = dev;
333 			return 0;
334 		}
335 	}
336 	debug("not found\n");
337 	return i2c_bind_driver(bus, chip_addr, offset_len, devp);
338 }
339 
340 int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
341 			    struct udevice **devp)
342 {
343 	struct udevice *bus;
344 	int ret;
345 
346 	ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
347 	if (ret) {
348 		debug("Cannot find I2C bus %d\n", busnum);
349 		return ret;
350 	}
351 	ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
352 	if (ret) {
353 		debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
354 		      busnum);
355 		return ret;
356 	}
357 
358 	return 0;
359 }
360 
361 int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
362 		 struct udevice **devp)
363 {
364 	int ret;
365 
366 	*devp = NULL;
367 
368 	/* First probe that chip */
369 	ret = i2c_probe_chip(bus, chip_addr, chip_flags);
370 	debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
371 	      chip_addr, ret);
372 	if (ret)
373 		return ret;
374 
375 	/* The chip was found, see if we have a driver, and probe it */
376 	ret = i2c_get_chip(bus, chip_addr, 1, devp);
377 	debug("%s:  i2c_get_chip: ret=%d\n", __func__, ret);
378 
379 	return ret;
380 }
381 
382 int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
383 {
384 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
385 	struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
386 	int ret;
387 
388 	/*
389 	 * If we have a method, call it. If not then the driver probably wants
390 	 * to deal with speed changes on the next transfer. It can easily read
391 	 * the current speed from this uclass
392 	 */
393 	if (ops->set_bus_speed) {
394 		ret = ops->set_bus_speed(bus, speed);
395 		if (ret)
396 			return ret;
397 	}
398 	i2c->speed_hz = speed;
399 
400 	return 0;
401 }
402 
403 int dm_i2c_get_bus_speed(struct udevice *bus)
404 {
405 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
406 	struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
407 
408 	if (!ops->get_bus_speed)
409 		return i2c->speed_hz;
410 
411 	return ops->get_bus_speed(bus);
412 }
413 
414 int i2c_set_chip_flags(struct udevice *dev, uint flags)
415 {
416 	struct udevice *bus = dev->parent;
417 	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
418 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
419 	int ret;
420 
421 	if (ops->set_flags) {
422 		ret = ops->set_flags(dev, flags);
423 		if (ret)
424 			return ret;
425 	}
426 	chip->flags = flags;
427 
428 	return 0;
429 }
430 
431 int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
432 {
433 	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
434 
435 	*flagsp = chip->flags;
436 
437 	return 0;
438 }
439 
440 int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
441 {
442 	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
443 
444 	if (offset_len > I2C_MAX_OFFSET_LEN)
445 		return -EINVAL;
446 	chip->offset_len = offset_len;
447 
448 	return 0;
449 }
450 
451 int i2c_get_chip_offset_len(struct udevice *dev)
452 {
453 	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
454 
455 	return chip->offset_len;
456 }
457 
458 #ifdef CONFIG_DM_GPIO
459 static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
460 {
461 	if (bit)
462 		dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
463 	else
464 		dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
465 					   GPIOD_ACTIVE_LOW |
466 					   GPIOD_IS_OUT_ACTIVE);
467 }
468 
469 static int i2c_gpio_get_pin(struct gpio_desc *pin)
470 {
471 	return dm_gpio_get_value(pin);
472 }
473 
474 static int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
475 				 struct gpio_desc *scl_pin)
476 {
477 	int counter = 9;
478 	int ret = 0;
479 
480 	i2c_gpio_set_pin(sda_pin, 1);
481 	i2c_gpio_set_pin(scl_pin, 1);
482 	udelay(5);
483 
484 	/*  Toggle SCL until slave release SDA */
485 	while (counter-- >= 0) {
486 		i2c_gpio_set_pin(scl_pin, 1);
487 		udelay(5);
488 		i2c_gpio_set_pin(scl_pin, 0);
489 		udelay(5);
490 		if (i2c_gpio_get_pin(sda_pin))
491 			break;
492 	}
493 
494 	/* Then, send I2C stop */
495 	i2c_gpio_set_pin(sda_pin, 0);
496 	udelay(5);
497 
498 	i2c_gpio_set_pin(scl_pin, 1);
499 	udelay(5);
500 
501 	i2c_gpio_set_pin(sda_pin, 1);
502 	udelay(5);
503 
504 	if (!i2c_gpio_get_pin(sda_pin) || !i2c_gpio_get_pin(scl_pin))
505 		ret = -EREMOTEIO;
506 
507 	return ret;
508 }
509 
510 static int i2c_deblock_gpio(struct udevice *bus)
511 {
512 	struct gpio_desc gpios[PIN_COUNT];
513 	int ret, ret0;
514 
515 	ret = gpio_request_list_by_name(bus, "gpios", gpios,
516 					ARRAY_SIZE(gpios), GPIOD_IS_IN);
517 	if (ret != ARRAY_SIZE(gpios)) {
518 		debug("%s: I2C Node '%s' has no 'gpios' property %s\n",
519 		      __func__, dev_read_name(bus), bus->name);
520 		if (ret >= 0) {
521 			gpio_free_list(bus, gpios, ret);
522 			ret = -ENOENT;
523 		}
524 		goto out;
525 	}
526 
527 	ret = pinctrl_select_state(bus, "gpio");
528 	if (ret) {
529 		debug("%s: I2C Node '%s' has no 'gpio' pinctrl state. %s\n",
530 		      __func__, dev_read_name(bus), bus->name);
531 		goto out_no_pinctrl;
532 	}
533 
534 	ret0 = i2c_deblock_gpio_loop(&gpios[PIN_SDA], &gpios[PIN_SCL]);
535 
536 	ret = pinctrl_select_state(bus, "default");
537 	if (ret) {
538 		debug("%s: I2C Node '%s' has no 'default' pinctrl state. %s\n",
539 		      __func__, dev_read_name(bus), bus->name);
540 	}
541 
542 	ret = !ret ? ret0 : ret;
543 
544 out_no_pinctrl:
545 	gpio_free_list(bus, gpios, ARRAY_SIZE(gpios));
546 out:
547 	return ret;
548 }
549 #else
550 static int i2c_deblock_gpio(struct udevice *bus)
551 {
552 	return -ENOSYS;
553 }
554 #endif // CONFIG_DM_GPIO
555 
556 int i2c_deblock(struct udevice *bus)
557 {
558 	struct dm_i2c_ops *ops = i2c_get_ops(bus);
559 
560 	if (!ops->deblock)
561 		return i2c_deblock_gpio(bus);
562 
563 	return ops->deblock(bus);
564 }
565 
566 #if CONFIG_IS_ENABLED(OF_CONTROL)
567 int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip)
568 {
569 	int addr;
570 
571 	chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len",
572 						1);
573 	chip->flags = 0;
574 	addr = dev_read_u32_default(dev, "reg", -1);
575 	if (addr == -1) {
576 		debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__,
577 		      dev_read_name(dev), dev->name);
578 		return -EINVAL;
579 	}
580 	chip->chip_addr = addr;
581 
582 	return 0;
583 }
584 #endif
585 
586 static int i2c_post_probe(struct udevice *dev)
587 {
588 #if CONFIG_IS_ENABLED(OF_CONTROL)
589 	struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
590 
591 	i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency", 100000);
592 
593 	return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
594 #else
595 	return 0;
596 #endif
597 }
598 
599 static int i2c_child_post_bind(struct udevice *dev)
600 {
601 #if CONFIG_IS_ENABLED(OF_CONTROL)
602 	struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
603 
604 	if (!dev_of_valid(dev))
605 		return 0;
606 	return i2c_chip_ofdata_to_platdata(dev, plat);
607 #else
608 	return 0;
609 #endif
610 }
611 
612 UCLASS_DRIVER(i2c) = {
613 	.id		= UCLASS_I2C,
614 	.name		= "i2c",
615 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
616 #if CONFIG_IS_ENABLED(OF_CONTROL)
617 	.post_bind	= dm_scan_fdt_dev,
618 #endif
619 	.post_probe	= i2c_post_probe,
620 	.per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
621 	.per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
622 	.child_post_bind = i2c_child_post_bind,
623 };
624 
625 UCLASS_DRIVER(i2c_generic) = {
626 	.id		= UCLASS_I2C_GENERIC,
627 	.name		= "i2c_generic",
628 };
629 
630 U_BOOT_DRIVER(i2c_generic_chip_drv) = {
631 	.name		= "i2c_generic_chip_drv",
632 	.id		= UCLASS_I2C_GENERIC,
633 };
634