1 /*
2  * (C) Copyright 2015, Samsung Electronics
3  * Przemyslaw Marczak <p.marczak@samsung.com>
4  *
5  * This file is based on: drivers/i2c/soft-i2c.c,
6  * with added driver-model support and code cleanup.
7  */
8 #include <common.h>
9 #include <errno.h>
10 #include <dm.h>
11 #include <i2c.h>
12 #include <asm/gpio.h>
13 
14 #define DEFAULT_UDELAY	5
15 #define RETRIES		0
16 #define I2C_ACK		0
17 #define I2C_NOACK	1
18 
19 DECLARE_GLOBAL_DATA_PTR;
20 
21 enum {
22 	PIN_SDA = 0,
23 	PIN_SCL,
24 	PIN_COUNT,
25 };
26 
27 struct i2c_gpio_bus {
28 	/**
29 	  * udelay - delay [us] between GPIO toggle operations,
30 	  * which is 1/4 of I2C speed clock period.
31 	 */
32 	int udelay;
33 	 /* sda, scl */
34 	struct gpio_desc gpios[PIN_COUNT];
35 };
36 
i2c_gpio_sda_get(struct gpio_desc * sda)37 static int i2c_gpio_sda_get(struct gpio_desc *sda)
38 {
39 	return dm_gpio_get_value(sda);
40 }
41 
i2c_gpio_sda_set(struct gpio_desc * sda,int bit)42 static void i2c_gpio_sda_set(struct gpio_desc *sda, int bit)
43 {
44 	if (bit)
45 		dm_gpio_set_dir_flags(sda, GPIOD_IS_IN);
46 	else
47 		dm_gpio_set_dir_flags(sda, GPIOD_IS_OUT);
48 }
49 
i2c_gpio_scl_set(struct gpio_desc * scl,int bit)50 static void i2c_gpio_scl_set(struct gpio_desc *scl, int bit)
51 {
52 	ulong flags = GPIOD_IS_OUT;
53 
54 	if (bit)
55 		flags |= GPIOD_IS_OUT_ACTIVE;
56 	dm_gpio_set_dir_flags(scl, flags);
57 }
58 
i2c_gpio_write_bit(struct gpio_desc * scl,struct gpio_desc * sda,int delay,uchar bit)59 static void i2c_gpio_write_bit(struct gpio_desc *scl, struct gpio_desc *sda,
60 			       int delay, uchar bit)
61 {
62 	i2c_gpio_scl_set(scl, 0);
63 	udelay(delay);
64 	i2c_gpio_sda_set(sda, bit);
65 	udelay(delay);
66 	i2c_gpio_scl_set(scl, 1);
67 	udelay(2 * delay);
68 }
69 
i2c_gpio_read_bit(struct gpio_desc * scl,struct gpio_desc * sda,int delay)70 static int i2c_gpio_read_bit(struct gpio_desc *scl, struct gpio_desc *sda,
71 			     int delay)
72 {
73 	int value;
74 
75 	i2c_gpio_scl_set(scl, 1);
76 	udelay(delay);
77 	value = i2c_gpio_sda_get(sda);
78 	udelay(delay);
79 	i2c_gpio_scl_set(scl, 0);
80 	udelay(2 * delay);
81 
82 	return value;
83 }
84 
85 /* START: High -> Low on SDA while SCL is High */
i2c_gpio_send_start(struct gpio_desc * scl,struct gpio_desc * sda,int delay)86 static void i2c_gpio_send_start(struct gpio_desc *scl, struct gpio_desc *sda,
87 				int delay)
88 {
89 	udelay(delay);
90 	i2c_gpio_sda_set(sda, 1);
91 	udelay(delay);
92 	i2c_gpio_scl_set(scl, 1);
93 	udelay(delay);
94 	i2c_gpio_sda_set(sda, 0);
95 	udelay(delay);
96 }
97 
98 /* STOP: Low -> High on SDA while SCL is High */
i2c_gpio_send_stop(struct gpio_desc * scl,struct gpio_desc * sda,int delay)99 static void i2c_gpio_send_stop(struct gpio_desc *scl, struct gpio_desc *sda,
100 			       int delay)
101 {
102 	i2c_gpio_scl_set(scl, 0);
103 	udelay(delay);
104 	i2c_gpio_sda_set(sda, 0);
105 	udelay(delay);
106 	i2c_gpio_scl_set(scl, 1);
107 	udelay(delay);
108 	i2c_gpio_sda_set(sda, 1);
109 	udelay(delay);
110 }
111 
112 /* ack should be I2C_ACK or I2C_NOACK */
i2c_gpio_send_ack(struct gpio_desc * scl,struct gpio_desc * sda,int delay,int ack)113 static void i2c_gpio_send_ack(struct gpio_desc *scl, struct gpio_desc *sda,
114 			      int delay, int ack)
115 {
116 	i2c_gpio_write_bit(scl, sda, delay, ack);
117 	i2c_gpio_scl_set(scl, 0);
118 	udelay(delay);
119 }
120 
121 /**
122  * Send a reset sequence consisting of 9 clocks with the data signal high
123  * to clock any confused device back into an idle state.  Also send a
124  * <stop> at the end of the sequence for belts & suspenders.
125  */
i2c_gpio_send_reset(struct gpio_desc * scl,struct gpio_desc * sda,int delay)126 static void i2c_gpio_send_reset(struct gpio_desc *scl, struct gpio_desc *sda,
127 				int delay)
128 {
129 	int j;
130 
131 	for (j = 0; j < 9; j++)
132 		i2c_gpio_write_bit(scl, sda, delay, 1);
133 
134 	i2c_gpio_send_stop(scl, sda, delay);
135 }
136 
137 /* Set sda high with low clock, before reading slave data */
i2c_gpio_sda_high(struct gpio_desc * scl,struct gpio_desc * sda,int delay)138 static void i2c_gpio_sda_high(struct gpio_desc *scl, struct gpio_desc *sda,
139 			      int delay)
140 {
141 	i2c_gpio_scl_set(scl, 0);
142 	udelay(delay);
143 	i2c_gpio_sda_set(sda, 1);
144 	udelay(delay);
145 }
146 
147 /* Send 8 bits and look for an acknowledgement */
i2c_gpio_write_byte(struct gpio_desc * scl,struct gpio_desc * sda,int delay,uchar data)148 static int i2c_gpio_write_byte(struct gpio_desc *scl, struct gpio_desc *sda,
149 			       int delay, uchar data)
150 {
151 	int j;
152 	int nack;
153 
154 	for (j = 0; j < 8; j++) {
155 		i2c_gpio_write_bit(scl, sda, delay, data & 0x80);
156 		data <<= 1;
157 	}
158 
159 	udelay(delay);
160 
161 	/* Look for an <ACK>(negative logic) and return it */
162 	i2c_gpio_sda_high(scl, sda, delay);
163 	nack = i2c_gpio_read_bit(scl, sda, delay);
164 
165 	return nack;	/* not a nack is an ack */
166 }
167 
168 /**
169  * if ack == I2C_ACK, ACK the byte so can continue reading, else
170  * send I2C_NOACK to end the read.
171  */
i2c_gpio_read_byte(struct gpio_desc * scl,struct gpio_desc * sda,int delay,int ack)172 static uchar i2c_gpio_read_byte(struct gpio_desc *scl, struct gpio_desc *sda,
173 				int delay, int ack)
174 {
175 	int  data;
176 	int  j;
177 
178 	i2c_gpio_sda_high(scl, sda, delay);
179 	data = 0;
180 	for (j = 0; j < 8; j++) {
181 		data <<= 1;
182 		data |= i2c_gpio_read_bit(scl, sda, delay);
183 	}
184 	i2c_gpio_send_ack(scl, sda, delay, ack);
185 
186 	return data;
187 }
188 
189 /* send start and the slave chip address */
i2c_send_slave_addr(struct gpio_desc * scl,struct gpio_desc * sda,int delay,uchar chip)190 int i2c_send_slave_addr(struct gpio_desc *scl, struct gpio_desc *sda, int delay,
191 			uchar chip)
192 {
193 	i2c_gpio_send_start(scl, sda, delay);
194 
195 	if (i2c_gpio_write_byte(scl, sda, delay, chip)) {
196 		i2c_gpio_send_stop(scl, sda, delay);
197 		return -EIO;
198 	}
199 
200 	return 0;
201 }
202 
i2c_gpio_write_data(struct i2c_gpio_bus * bus,uchar chip,uchar * buffer,int len,bool end_with_repeated_start)203 static int i2c_gpio_write_data(struct i2c_gpio_bus *bus, uchar chip,
204 			       uchar *buffer, int len,
205 			       bool end_with_repeated_start)
206 {
207 	struct gpio_desc *scl = &bus->gpios[PIN_SCL];
208 	struct gpio_desc *sda = &bus->gpios[PIN_SDA];
209 	unsigned int delay = bus->udelay;
210 	int failures = 0;
211 
212 	debug("%s: chip %x buffer %p len %d\n", __func__, chip, buffer, len);
213 
214 	if (i2c_send_slave_addr(scl, sda, delay, chip << 1)) {
215 		debug("i2c_write, no chip responded %02X\n", chip);
216 		return -EIO;
217 	}
218 
219 	while (len-- > 0) {
220 		if (i2c_gpio_write_byte(scl, sda, delay, *buffer++))
221 			failures++;
222 	}
223 
224 	if (!end_with_repeated_start) {
225 		i2c_gpio_send_stop(scl, sda, delay);
226 		return failures;
227 	}
228 
229 	if (i2c_send_slave_addr(scl, sda, delay, (chip << 1) | 0x1)) {
230 		debug("i2c_write, no chip responded %02X\n", chip);
231 		return -EIO;
232 	}
233 
234 	return failures;
235 }
236 
i2c_gpio_read_data(struct i2c_gpio_bus * bus,uchar chip,uchar * buffer,int len)237 static int i2c_gpio_read_data(struct i2c_gpio_bus *bus, uchar chip,
238 			      uchar *buffer, int len)
239 {
240 	struct gpio_desc *scl = &bus->gpios[PIN_SCL];
241 	struct gpio_desc *sda = &bus->gpios[PIN_SDA];
242 	unsigned int delay = bus->udelay;
243 
244 	debug("%s: chip %x buffer: %p len %d\n", __func__, chip, buffer, len);
245 
246 	while (len-- > 0)
247 		*buffer++ = i2c_gpio_read_byte(scl, sda, delay, len == 0);
248 
249 	i2c_gpio_send_stop(scl, sda, delay);
250 
251 	return 0;
252 }
253 
i2c_gpio_xfer(struct udevice * dev,struct i2c_msg * msg,int nmsgs)254 static int i2c_gpio_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
255 {
256 	struct i2c_gpio_bus *bus = dev_get_priv(dev);
257 	int ret;
258 
259 	for (; nmsgs > 0; nmsgs--, msg++) {
260 		bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD);
261 
262 		if (msg->flags & I2C_M_RD) {
263 			ret = i2c_gpio_read_data(bus, msg->addr, msg->buf,
264 						 msg->len);
265 		} else {
266 			ret = i2c_gpio_write_data(bus, msg->addr, msg->buf,
267 						  msg->len, next_is_read);
268 		}
269 
270 		if (ret)
271 			return -EREMOTEIO;
272 	}
273 
274 	return 0;
275 }
276 
i2c_gpio_probe(struct udevice * dev,uint chip,uint chip_flags)277 static int i2c_gpio_probe(struct udevice *dev, uint chip, uint chip_flags)
278 {
279 	struct i2c_gpio_bus *bus = dev_get_priv(dev);
280 	struct gpio_desc *scl = &bus->gpios[PIN_SCL];
281 	struct gpio_desc *sda = &bus->gpios[PIN_SDA];
282 	unsigned int delay = bus->udelay;
283 	int ret;
284 
285 	i2c_gpio_send_start(scl, sda, delay);
286 	ret = i2c_gpio_write_byte(scl, sda, delay, (chip << 1) | 0);
287 	i2c_gpio_send_stop(scl, sda, delay);
288 
289 	debug("%s: bus: %d (%s) chip: %x flags: %x ret: %d\n",
290 	      __func__, dev->seq, dev->name, chip, chip_flags, ret);
291 
292 	return ret;
293 }
294 
i2c_gpio_set_bus_speed(struct udevice * dev,unsigned int speed_hz)295 static int i2c_gpio_set_bus_speed(struct udevice *dev, unsigned int speed_hz)
296 {
297 	struct i2c_gpio_bus *bus = dev_get_priv(dev);
298 	struct gpio_desc *scl = &bus->gpios[PIN_SCL];
299 	struct gpio_desc *sda = &bus->gpios[PIN_SDA];
300 
301 	bus->udelay = 1000000 / (speed_hz << 2);
302 
303 	i2c_gpio_send_reset(scl, sda, bus->udelay);
304 
305 	return 0;
306 }
307 
i2c_gpio_ofdata_to_platdata(struct udevice * dev)308 static int i2c_gpio_ofdata_to_platdata(struct udevice *dev)
309 {
310 	struct i2c_gpio_bus *bus = dev_get_priv(dev);
311 	const void *blob = gd->fdt_blob;
312 	int node = dev_of_offset(dev);
313 	int ret;
314 
315 	ret = gpio_request_list_by_name(dev, "gpios", bus->gpios,
316 					ARRAY_SIZE(bus->gpios), 0);
317 	if (ret < 0)
318 		goto error;
319 
320 	bus->udelay = fdtdec_get_int(blob, node, "i2c-gpio,delay-us",
321 				     DEFAULT_UDELAY);
322 
323 	return 0;
324 error:
325 	pr_err("Can't get %s gpios! Error: %d", dev->name, ret);
326 	return ret;
327 }
328 
329 static const struct dm_i2c_ops i2c_gpio_ops = {
330 	.xfer		= i2c_gpio_xfer,
331 	.probe_chip	= i2c_gpio_probe,
332 	.set_bus_speed	= i2c_gpio_set_bus_speed,
333 };
334 
335 static const struct udevice_id i2c_gpio_ids[] = {
336 	{ .compatible = "i2c-gpio" },
337 	{ }
338 };
339 
340 U_BOOT_DRIVER(i2c_gpio) = {
341 	.name	= "i2c-gpio",
342 	.id	= UCLASS_I2C,
343 	.of_match = i2c_gpio_ids,
344 	.ofdata_to_platdata = i2c_gpio_ofdata_to_platdata,
345 	.priv_auto_alloc_size = sizeof(struct i2c_gpio_bus),
346 	.ops	= &i2c_gpio_ops,
347 };
348