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