2 * Bitbanging I2C bus driver using the GPIO API
4 * Copyright (C) 2007 Atmel Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 #include <linux/debugfs.h>
11 #include <linux/delay.h>
12 #include <linux/i2c.h>
13 #include <linux/i2c-algo-bit.h>
14 #include <linux/platform_data/i2c-gpio.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/platform_device.h>
19 #include <linux/gpio/consumer.h>
22 struct i2c_gpio_private_data
{
23 struct gpio_desc
*sda
;
24 struct gpio_desc
*scl
;
25 struct i2c_adapter adap
;
26 struct i2c_algo_bit_data bit_data
;
27 struct i2c_gpio_platform_data pdata
;
28 #ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
29 struct dentry
*debug_dir
;
34 * Toggle SDA by changing the output value of the pin. This is only
35 * valid for pins configured as open drain (i.e. setting the value
36 * high effectively turns off the output driver.)
38 static void i2c_gpio_setsda_val(void *data
, int state
)
40 struct i2c_gpio_private_data
*priv
= data
;
42 gpiod_set_value_cansleep(priv
->sda
, state
);
46 * Toggle SCL by changing the output value of the pin. This is used
47 * for pins that are configured as open drain and for output-only
48 * pins. The latter case will break the i2c protocol, but it will
49 * often work in practice.
51 static void i2c_gpio_setscl_val(void *data
, int state
)
53 struct i2c_gpio_private_data
*priv
= data
;
55 gpiod_set_value_cansleep(priv
->scl
, state
);
58 static int i2c_gpio_getsda(void *data
)
60 struct i2c_gpio_private_data
*priv
= data
;
62 return gpiod_get_value_cansleep(priv
->sda
);
65 static int i2c_gpio_getscl(void *data
)
67 struct i2c_gpio_private_data
*priv
= data
;
69 return gpiod_get_value_cansleep(priv
->scl
);
72 #ifdef CONFIG_I2C_GPIO_FAULT_INJECTOR
73 static struct dentry
*i2c_gpio_debug_dir
;
75 #define setsda(bd, val) ((bd)->setsda((bd)->data, val))
76 #define setscl(bd, val) ((bd)->setscl((bd)->data, val))
77 #define getsda(bd) ((bd)->getsda((bd)->data))
78 #define getscl(bd) ((bd)->getscl((bd)->data))
80 #define WIRE_ATTRIBUTE(wire) \
81 static int fops_##wire##_get(void *data, u64 *val) \
83 struct i2c_gpio_private_data *priv = data; \
85 i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
86 *val = get##wire(&priv->bit_data); \
87 i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
90 static int fops_##wire##_set(void *data, u64 val) \
92 struct i2c_gpio_private_data *priv = data; \
94 i2c_lock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
95 set##wire(&priv->bit_data, val); \
96 i2c_unlock_bus(&priv->adap, I2C_LOCK_ROOT_ADAPTER); \
99 DEFINE_DEBUGFS_ATTRIBUTE(fops_##wire, fops_##wire##_get, fops_##wire##_set, "%llu\n")
104 static void i2c_gpio_incomplete_transfer(struct i2c_gpio_private_data
*priv
,
105 u32 pattern
, u8 pattern_size
)
107 struct i2c_algo_bit_data
*bit_data
= &priv
->bit_data
;
110 i2c_lock_bus(&priv
->adap
, I2C_LOCK_ROOT_ADAPTER
);
112 /* START condition */
114 udelay(bit_data
->udelay
);
116 /* Send pattern, request ACK, don't send STOP */
117 for (i
= pattern_size
- 1; i
>= 0; i
--) {
119 udelay(bit_data
->udelay
/ 2);
120 setsda(bit_data
, (pattern
>> i
) & 1);
121 udelay((bit_data
->udelay
+ 1) / 2);
123 udelay(bit_data
->udelay
);
126 i2c_unlock_bus(&priv
->adap
, I2C_LOCK_ROOT_ADAPTER
);
129 static int fops_incomplete_addr_phase_set(void *data
, u64 addr
)
131 struct i2c_gpio_private_data
*priv
= data
;
137 /* ADDR (7 bit) + RD (1 bit) + Client ACK, keep SDA hi (1 bit) */
138 pattern
= (addr
<< 2) | 3;
140 i2c_gpio_incomplete_transfer(priv
, pattern
, 9);
144 DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_addr_phase
, NULL
, fops_incomplete_addr_phase_set
, "%llu\n");
146 static int fops_incomplete_write_byte_set(void *data
, u64 addr
)
148 struct i2c_gpio_private_data
*priv
= data
;
154 /* ADDR (7 bit) + WR (1 bit) + Client ACK (1 bit) */
155 pattern
= (addr
<< 2) | 1;
156 /* 0x00 (8 bit) + Client ACK, keep SDA hi (1 bit) */
157 pattern
= (pattern
<< 9) | 1;
159 i2c_gpio_incomplete_transfer(priv
, pattern
, 18);
163 DEFINE_DEBUGFS_ATTRIBUTE(fops_incomplete_write_byte
, NULL
, fops_incomplete_write_byte_set
, "%llu\n");
165 static void i2c_gpio_fault_injector_init(struct platform_device
*pdev
)
167 struct i2c_gpio_private_data
*priv
= platform_get_drvdata(pdev
);
170 * If there will be a debugfs-dir per i2c adapter somewhen, put the
171 * 'fault-injector' dir there. Until then, we have a global dir with
172 * all adapters as subdirs.
174 if (!i2c_gpio_debug_dir
) {
175 i2c_gpio_debug_dir
= debugfs_create_dir("i2c-fault-injector", NULL
);
176 if (!i2c_gpio_debug_dir
)
180 priv
->debug_dir
= debugfs_create_dir(pdev
->name
, i2c_gpio_debug_dir
);
181 if (!priv
->debug_dir
)
184 debugfs_create_file_unsafe("scl", 0600, priv
->debug_dir
, priv
, &fops_scl
);
185 debugfs_create_file_unsafe("sda", 0600, priv
->debug_dir
, priv
, &fops_sda
);
186 debugfs_create_file_unsafe("incomplete_address_phase", 0200, priv
->debug_dir
,
187 priv
, &fops_incomplete_addr_phase
);
188 debugfs_create_file_unsafe("incomplete_write_byte", 0200, priv
->debug_dir
,
189 priv
, &fops_incomplete_write_byte
);
192 static void i2c_gpio_fault_injector_exit(struct platform_device
*pdev
)
194 struct i2c_gpio_private_data
*priv
= platform_get_drvdata(pdev
);
196 debugfs_remove_recursive(priv
->debug_dir
);
199 static inline void i2c_gpio_fault_injector_init(struct platform_device
*pdev
) {}
200 static inline void i2c_gpio_fault_injector_exit(struct platform_device
*pdev
) {}
201 #endif /* CONFIG_I2C_GPIO_FAULT_INJECTOR*/
203 static void of_i2c_gpio_get_props(struct device_node
*np
,
204 struct i2c_gpio_platform_data
*pdata
)
208 of_property_read_u32(np
, "i2c-gpio,delay-us", &pdata
->udelay
);
210 if (!of_property_read_u32(np
, "i2c-gpio,timeout-ms", ®
))
211 pdata
->timeout
= msecs_to_jiffies(reg
);
213 pdata
->sda_is_open_drain
=
214 of_property_read_bool(np
, "i2c-gpio,sda-open-drain");
215 pdata
->scl_is_open_drain
=
216 of_property_read_bool(np
, "i2c-gpio,scl-open-drain");
217 pdata
->scl_is_output_only
=
218 of_property_read_bool(np
, "i2c-gpio,scl-output-only");
221 static struct gpio_desc
*i2c_gpio_get_desc(struct device
*dev
,
224 enum gpiod_flags gflags
)
226 struct gpio_desc
*retdesc
;
229 retdesc
= devm_gpiod_get(dev
, con_id
, gflags
);
230 if (!IS_ERR(retdesc
)) {
231 dev_dbg(dev
, "got GPIO from name %s\n", con_id
);
235 retdesc
= devm_gpiod_get_index(dev
, NULL
, index
, gflags
);
236 if (!IS_ERR(retdesc
)) {
237 dev_dbg(dev
, "got GPIO from index %u\n", index
);
241 ret
= PTR_ERR(retdesc
);
243 /* FIXME: hack in the old code, is this really necessary? */
245 retdesc
= ERR_PTR(-EPROBE_DEFER
);
247 /* This happens if the GPIO driver is not yet probed, let's defer */
249 retdesc
= ERR_PTR(-EPROBE_DEFER
);
251 if (PTR_ERR(retdesc
) != -EPROBE_DEFER
)
252 dev_err(dev
, "error trying to get descriptor: %d\n", ret
);
257 static int i2c_gpio_probe(struct platform_device
*pdev
)
259 struct i2c_gpio_private_data
*priv
;
260 struct i2c_gpio_platform_data
*pdata
;
261 struct i2c_algo_bit_data
*bit_data
;
262 struct i2c_adapter
*adap
;
263 struct device
*dev
= &pdev
->dev
;
264 struct device_node
*np
= dev
->of_node
;
265 enum gpiod_flags gflags
;
268 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
273 bit_data
= &priv
->bit_data
;
274 pdata
= &priv
->pdata
;
277 of_i2c_gpio_get_props(np
, pdata
);
280 * If all platform data settings are zero it is OK
281 * to not provide any platform data from the board.
283 if (dev_get_platdata(dev
))
284 memcpy(pdata
, dev_get_platdata(dev
), sizeof(*pdata
));
288 * First get the GPIO pins; if it fails, we'll defer the probe.
289 * If the SDA line is marked from platform data or device tree as
290 * "open drain" it means something outside of our control is making
291 * this line being handled as open drain, and we should just handle
292 * it as any other output. Else we enforce open drain as this is
293 * required for an I2C bus.
295 if (pdata
->sda_is_open_drain
)
296 gflags
= GPIOD_OUT_HIGH
;
298 gflags
= GPIOD_OUT_HIGH_OPEN_DRAIN
;
299 priv
->sda
= i2c_gpio_get_desc(dev
, "sda", 0, gflags
);
300 if (IS_ERR(priv
->sda
))
301 return PTR_ERR(priv
->sda
);
304 * If the SCL line is marked from platform data or device tree as
305 * "open drain" it means something outside of our control is making
306 * this line being handled as open drain, and we should just handle
307 * it as any other output. Else we enforce open drain as this is
308 * required for an I2C bus.
310 if (pdata
->scl_is_open_drain
)
311 gflags
= GPIOD_OUT_HIGH
;
313 gflags
= GPIOD_OUT_HIGH_OPEN_DRAIN
;
314 priv
->scl
= i2c_gpio_get_desc(dev
, "scl", 1, gflags
);
315 if (IS_ERR(priv
->scl
))
316 return PTR_ERR(priv
->scl
);
318 if (gpiod_cansleep(priv
->sda
) || gpiod_cansleep(priv
->scl
))
319 dev_warn(dev
, "Slow GPIO pins might wreak havoc into I2C/SMBus bus timing");
321 bit_data
->setsda
= i2c_gpio_setsda_val
;
322 bit_data
->setscl
= i2c_gpio_setscl_val
;
324 if (!pdata
->scl_is_output_only
)
325 bit_data
->getscl
= i2c_gpio_getscl
;
326 bit_data
->getsda
= i2c_gpio_getsda
;
329 bit_data
->udelay
= pdata
->udelay
;
330 else if (pdata
->scl_is_output_only
)
331 bit_data
->udelay
= 50; /* 10 kHz */
333 bit_data
->udelay
= 5; /* 100 kHz */
336 bit_data
->timeout
= pdata
->timeout
;
338 bit_data
->timeout
= HZ
/ 10; /* 100 ms */
340 bit_data
->data
= priv
;
342 adap
->owner
= THIS_MODULE
;
344 strlcpy(adap
->name
, dev_name(dev
), sizeof(adap
->name
));
346 snprintf(adap
->name
, sizeof(adap
->name
), "i2c-gpio%d", pdev
->id
);
348 adap
->algo_data
= bit_data
;
349 adap
->class = I2C_CLASS_HWMON
| I2C_CLASS_SPD
;
350 adap
->dev
.parent
= dev
;
351 adap
->dev
.of_node
= np
;
354 ret
= i2c_bit_add_numbered_bus(adap
);
358 platform_set_drvdata(pdev
, priv
);
361 * FIXME: using global GPIO numbers is not helpful. If/when we
362 * get accessors to get the actual name of the GPIO line,
363 * from the descriptor, then provide that instead.
365 dev_info(dev
, "using lines %u (SDA) and %u (SCL%s)\n",
366 desc_to_gpio(priv
->sda
), desc_to_gpio(priv
->scl
),
367 pdata
->scl_is_output_only
368 ? ", no clock stretching" : "");
370 i2c_gpio_fault_injector_init(pdev
);
375 static int i2c_gpio_remove(struct platform_device
*pdev
)
377 struct i2c_gpio_private_data
*priv
;
378 struct i2c_adapter
*adap
;
380 i2c_gpio_fault_injector_exit(pdev
);
382 priv
= platform_get_drvdata(pdev
);
385 i2c_del_adapter(adap
);
390 #if defined(CONFIG_OF)
391 static const struct of_device_id i2c_gpio_dt_ids
[] = {
392 { .compatible
= "i2c-gpio", },
396 MODULE_DEVICE_TABLE(of
, i2c_gpio_dt_ids
);
399 static struct platform_driver i2c_gpio_driver
= {
402 .of_match_table
= of_match_ptr(i2c_gpio_dt_ids
),
404 .probe
= i2c_gpio_probe
,
405 .remove
= i2c_gpio_remove
,
408 static int __init
i2c_gpio_init(void)
412 ret
= platform_driver_register(&i2c_gpio_driver
);
414 printk(KERN_ERR
"i2c-gpio: probe failed: %d\n", ret
);
418 subsys_initcall(i2c_gpio_init
);
420 static void __exit
i2c_gpio_exit(void)
422 platform_driver_unregister(&i2c_gpio_driver
);
424 module_exit(i2c_gpio_exit
);
426 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
427 MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver");
428 MODULE_LICENSE("GPL");
429 MODULE_ALIAS("platform:i2c-gpio");