2 * (C) Copyright 2015, Samsung Electronics
3 * Przemyslaw Marczak <p.marczak@samsung.com>
5 * This file is based on: drivers/i2c/soft-i2c.c,
6 * with added driver-model support and code cleanup.
14 #define DEFAULT_UDELAY 5
19 DECLARE_GLOBAL_DATA_PTR
;
29 * udelay - delay [us] between GPIO toggle operations,
30 * which is 1/4 of I2C speed clock period.
34 struct gpio_desc gpios
[PIN_COUNT
];
37 static int i2c_gpio_sda_get(struct gpio_desc
*sda
)
39 return dm_gpio_get_value(sda
);
42 static void i2c_gpio_sda_set(struct gpio_desc
*sda
, int bit
)
45 dm_gpio_set_dir_flags(sda
, GPIOD_IS_IN
);
47 dm_gpio_set_dir_flags(sda
, GPIOD_IS_OUT
);
50 static void i2c_gpio_scl_set(struct gpio_desc
*scl
, int bit
)
52 ulong flags
= GPIOD_IS_OUT
;
55 flags
|= GPIOD_IS_OUT_ACTIVE
;
56 dm_gpio_set_dir_flags(scl
, flags
);
59 static void i2c_gpio_write_bit(struct gpio_desc
*scl
, struct gpio_desc
*sda
,
62 i2c_gpio_scl_set(scl
, 0);
64 i2c_gpio_sda_set(sda
, bit
);
66 i2c_gpio_scl_set(scl
, 1);
70 static int i2c_gpio_read_bit(struct gpio_desc
*scl
, struct gpio_desc
*sda
,
75 i2c_gpio_scl_set(scl
, 1);
77 value
= i2c_gpio_sda_get(sda
);
79 i2c_gpio_scl_set(scl
, 0);
85 /* START: High -> Low on SDA while SCL is High */
86 static void i2c_gpio_send_start(struct gpio_desc
*scl
, struct gpio_desc
*sda
,
90 i2c_gpio_sda_set(sda
, 1);
92 i2c_gpio_scl_set(scl
, 1);
94 i2c_gpio_sda_set(sda
, 0);
98 /* STOP: Low -> High on SDA while SCL is High */
99 static void i2c_gpio_send_stop(struct gpio_desc
*scl
, struct gpio_desc
*sda
,
102 i2c_gpio_scl_set(scl
, 0);
104 i2c_gpio_sda_set(sda
, 0);
106 i2c_gpio_scl_set(scl
, 1);
108 i2c_gpio_sda_set(sda
, 1);
112 /* ack should be I2C_ACK or I2C_NOACK */
113 static void i2c_gpio_send_ack(struct gpio_desc
*scl
, struct gpio_desc
*sda
,
116 i2c_gpio_write_bit(scl
, sda
, delay
, ack
);
117 i2c_gpio_scl_set(scl
, 0);
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.
126 static void i2c_gpio_send_reset(struct gpio_desc
*scl
, struct gpio_desc
*sda
,
131 for (j
= 0; j
< 9; j
++)
132 i2c_gpio_write_bit(scl
, sda
, delay
, 1);
134 i2c_gpio_send_stop(scl
, sda
, delay
);
137 /* Set sda high with low clock, before reading slave data */
138 static void i2c_gpio_sda_high(struct gpio_desc
*scl
, struct gpio_desc
*sda
,
141 i2c_gpio_scl_set(scl
, 0);
143 i2c_gpio_sda_set(sda
, 1);
147 /* Send 8 bits and look for an acknowledgement */
148 static int i2c_gpio_write_byte(struct gpio_desc
*scl
, struct gpio_desc
*sda
,
149 int delay
, uchar data
)
154 for (j
= 0; j
< 8; j
++) {
155 i2c_gpio_write_bit(scl
, sda
, delay
, data
& 0x80);
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
);
165 return nack
; /* not a nack is an ack */
169 * if ack == I2C_ACK, ACK the byte so can continue reading, else
170 * send I2C_NOACK to end the read.
172 static uchar
i2c_gpio_read_byte(struct gpio_desc
*scl
, struct gpio_desc
*sda
,
178 i2c_gpio_sda_high(scl
, sda
, delay
);
180 for (j
= 0; j
< 8; j
++) {
182 data
|= i2c_gpio_read_bit(scl
, sda
, delay
);
184 i2c_gpio_send_ack(scl
, sda
, delay
, ack
);
189 /* send start and the slave chip address */
190 int i2c_send_slave_addr(struct gpio_desc
*scl
, struct gpio_desc
*sda
, int delay
,
193 i2c_gpio_send_start(scl
, sda
, delay
);
195 if (i2c_gpio_write_byte(scl
, sda
, delay
, chip
)) {
196 i2c_gpio_send_stop(scl
, sda
, delay
);
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
)
207 struct gpio_desc
*scl
= &bus
->gpios
[PIN_SCL
];
208 struct gpio_desc
*sda
= &bus
->gpios
[PIN_SDA
];
209 unsigned int delay
= bus
->udelay
;
212 debug("%s: chip %x buffer %p len %d\n", __func__
, chip
, buffer
, len
);
214 if (i2c_send_slave_addr(scl
, sda
, delay
, chip
<< 1)) {
215 debug("i2c_write, no chip responded %02X\n", chip
);
220 if (i2c_gpio_write_byte(scl
, sda
, delay
, *buffer
++))
224 if (!end_with_repeated_start
) {
225 i2c_gpio_send_stop(scl
, sda
, delay
);
229 if (i2c_send_slave_addr(scl
, sda
, delay
, (chip
<< 1) | 0x1)) {
230 debug("i2c_write, no chip responded %02X\n", chip
);
237 static int i2c_gpio_read_data(struct i2c_gpio_bus
*bus
, uchar chip
,
238 uchar
*buffer
, int len
)
240 struct gpio_desc
*scl
= &bus
->gpios
[PIN_SCL
];
241 struct gpio_desc
*sda
= &bus
->gpios
[PIN_SDA
];
242 unsigned int delay
= bus
->udelay
;
244 debug("%s: chip %x buffer: %p len %d\n", __func__
, chip
, buffer
, len
);
247 *buffer
++ = i2c_gpio_read_byte(scl
, sda
, delay
, len
== 0);
249 i2c_gpio_send_stop(scl
, sda
, delay
);
254 static int i2c_gpio_xfer(struct udevice
*dev
, struct i2c_msg
*msg
, int nmsgs
)
256 struct i2c_gpio_bus
*bus
= dev_get_priv(dev
);
259 for (; nmsgs
> 0; nmsgs
--, msg
++) {
260 bool next_is_read
= nmsgs
> 1 && (msg
[1].flags
& I2C_M_RD
);
262 if (msg
->flags
& I2C_M_RD
) {
263 ret
= i2c_gpio_read_data(bus
, msg
->addr
, msg
->buf
,
266 ret
= i2c_gpio_write_data(bus
, msg
->addr
, msg
->buf
,
267 msg
->len
, next_is_read
);
277 static int i2c_gpio_probe(struct udevice
*dev
, uint chip
, uint chip_flags
)
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
;
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
);
289 debug("%s: bus: %d (%s) chip: %x flags: %x ret: %d\n",
290 __func__
, dev
->seq
, dev
->name
, chip
, chip_flags
, ret
);
295 static int i2c_gpio_set_bus_speed(struct udevice
*dev
, unsigned int speed_hz
)
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
];
301 bus
->udelay
= 1000000 / (speed_hz
<< 2);
303 i2c_gpio_send_reset(scl
, sda
, bus
->udelay
);
308 static int i2c_gpio_ofdata_to_platdata(struct udevice
*dev
)
310 struct i2c_gpio_bus
*bus
= dev_get_priv(dev
);
311 const void *blob
= gd
->fdt_blob
;
312 int node
= dev_of_offset(dev
);
315 ret
= gpio_request_list_by_name(dev
, "gpios", bus
->gpios
,
316 ARRAY_SIZE(bus
->gpios
), 0);
320 bus
->udelay
= fdtdec_get_int(blob
, node
, "i2c-gpio,delay-us",
325 pr_err("Can't get %s gpios! Error: %d", dev
->name
, ret
);
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
,
335 static const struct udevice_id i2c_gpio_ids
[] = {
336 { .compatible
= "i2c-gpio" },
340 U_BOOT_DRIVER(i2c_gpio
) = {
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
,