Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Bitbanging I2C bus driver using the GPIO API |
| 3 | * |
| 4 | * Copyright (C) 2007 Atmel Corporation |
| 5 | * |
| 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. |
| 9 | */ |
| 10 | #include <linux/i2c.h> |
| 11 | #include <linux/i2c-algo-bit.h> |
| 12 | #include <linux/i2c-gpio.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 16 | #include <linux/platform_device.h> |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 17 | #include <linux/gpio.h> |
| 18 | #include <linux/of_gpio.h> |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 19 | |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 20 | struct i2c_gpio_private_data { |
| 21 | struct i2c_adapter adap; |
| 22 | struct i2c_algo_bit_data bit_data; |
| 23 | struct i2c_gpio_platform_data pdata; |
| 24 | }; |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 25 | |
| 26 | /* Toggle SDA by changing the direction of the pin */ |
| 27 | static void i2c_gpio_setsda_dir(void *data, int state) |
| 28 | { |
| 29 | struct i2c_gpio_platform_data *pdata = data; |
| 30 | |
| 31 | if (state) |
| 32 | gpio_direction_input(pdata->sda_pin); |
| 33 | else |
| 34 | gpio_direction_output(pdata->sda_pin, 0); |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | * Toggle SDA by changing the output value of the pin. This is only |
| 39 | * valid for pins configured as open drain (i.e. setting the value |
| 40 | * high effectively turns off the output driver.) |
| 41 | */ |
| 42 | static void i2c_gpio_setsda_val(void *data, int state) |
| 43 | { |
| 44 | struct i2c_gpio_platform_data *pdata = data; |
| 45 | |
| 46 | gpio_set_value(pdata->sda_pin, state); |
| 47 | } |
| 48 | |
| 49 | /* Toggle SCL by changing the direction of the pin. */ |
| 50 | static void i2c_gpio_setscl_dir(void *data, int state) |
| 51 | { |
| 52 | struct i2c_gpio_platform_data *pdata = data; |
| 53 | |
| 54 | if (state) |
| 55 | gpio_direction_input(pdata->scl_pin); |
| 56 | else |
| 57 | gpio_direction_output(pdata->scl_pin, 0); |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * Toggle SCL by changing the output value of the pin. This is used |
| 62 | * for pins that are configured as open drain and for output-only |
| 63 | * pins. The latter case will break the i2c protocol, but it will |
| 64 | * often work in practice. |
| 65 | */ |
| 66 | static void i2c_gpio_setscl_val(void *data, int state) |
| 67 | { |
| 68 | struct i2c_gpio_platform_data *pdata = data; |
| 69 | |
| 70 | gpio_set_value(pdata->scl_pin, state); |
| 71 | } |
| 72 | |
Atsushi Nemoto | 4d6ceed | 2007-07-12 14:12:30 +0200 | [diff] [blame] | 73 | static int i2c_gpio_getsda(void *data) |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 74 | { |
| 75 | struct i2c_gpio_platform_data *pdata = data; |
| 76 | |
| 77 | return gpio_get_value(pdata->sda_pin); |
| 78 | } |
| 79 | |
Atsushi Nemoto | 4d6ceed | 2007-07-12 14:12:30 +0200 | [diff] [blame] | 80 | static int i2c_gpio_getscl(void *data) |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 81 | { |
| 82 | struct i2c_gpio_platform_data *pdata = data; |
| 83 | |
| 84 | return gpio_get_value(pdata->scl_pin); |
| 85 | } |
| 86 | |
Jean Delvare | 1b295c8 | 2013-02-28 01:01:40 +0000 | [diff] [blame] | 87 | static int of_i2c_gpio_get_pins(struct device_node *np, |
| 88 | unsigned int *sda_pin, unsigned int *scl_pin) |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 89 | { |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 90 | if (of_gpio_count(np) < 2) |
| 91 | return -ENODEV; |
| 92 | |
Jean Delvare | 1b295c8 | 2013-02-28 01:01:40 +0000 | [diff] [blame] | 93 | *sda_pin = of_get_gpio(np, 0); |
| 94 | *scl_pin = of_get_gpio(np, 1); |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 95 | |
Jean Delvare | 1b295c8 | 2013-02-28 01:01:40 +0000 | [diff] [blame] | 96 | if (!gpio_is_valid(*sda_pin) || !gpio_is_valid(*scl_pin)) { |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 97 | pr_err("%s: invalid GPIO pins, sda=%d/scl=%d\n", |
Jean Delvare | 1b295c8 | 2013-02-28 01:01:40 +0000 | [diff] [blame] | 98 | np->full_name, *sda_pin, *scl_pin); |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 99 | return -ENODEV; |
| 100 | } |
| 101 | |
Jean Delvare | 1b295c8 | 2013-02-28 01:01:40 +0000 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static void of_i2c_gpio_get_props(struct device_node *np, |
| 106 | struct i2c_gpio_platform_data *pdata) |
| 107 | { |
| 108 | u32 reg; |
| 109 | |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 110 | of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay); |
| 111 | |
| 112 | if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", ®)) |
| 113 | pdata->timeout = msecs_to_jiffies(reg); |
| 114 | |
| 115 | pdata->sda_is_open_drain = |
| 116 | of_property_read_bool(np, "i2c-gpio,sda-open-drain"); |
| 117 | pdata->scl_is_open_drain = |
| 118 | of_property_read_bool(np, "i2c-gpio,scl-open-drain"); |
| 119 | pdata->scl_is_output_only = |
| 120 | of_property_read_bool(np, "i2c-gpio,scl-output-only"); |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 121 | } |
| 122 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 123 | static int i2c_gpio_probe(struct platform_device *pdev) |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 124 | { |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 125 | struct i2c_gpio_private_data *priv; |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 126 | struct i2c_gpio_platform_data *pdata; |
| 127 | struct i2c_algo_bit_data *bit_data; |
| 128 | struct i2c_adapter *adap; |
Jean Delvare | 1b295c8 | 2013-02-28 01:01:40 +0000 | [diff] [blame] | 129 | unsigned int sda_pin, scl_pin; |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 130 | int ret; |
| 131 | |
Jean Delvare | 1b295c8 | 2013-02-28 01:01:40 +0000 | [diff] [blame] | 132 | /* First get the GPIO pins; if it fails, we'll defer the probe. */ |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 133 | if (pdev->dev.of_node) { |
Jean Delvare | 1b295c8 | 2013-02-28 01:01:40 +0000 | [diff] [blame] | 134 | ret = of_i2c_gpio_get_pins(pdev->dev.of_node, |
| 135 | &sda_pin, &scl_pin); |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 136 | if (ret) |
| 137 | return ret; |
| 138 | } else { |
Jingoo Han | 6d4028c | 2013-07-30 16:59:33 +0900 | [diff] [blame] | 139 | if (!dev_get_platdata(&pdev->dev)) |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 140 | return -ENXIO; |
Jingoo Han | 6d4028c | 2013-07-30 16:59:33 +0900 | [diff] [blame] | 141 | pdata = dev_get_platdata(&pdev->dev); |
Jean Delvare | 1b295c8 | 2013-02-28 01:01:40 +0000 | [diff] [blame] | 142 | sda_pin = pdata->sda_pin; |
| 143 | scl_pin = pdata->scl_pin; |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 144 | } |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 145 | |
Jean Delvare | 1b295c8 | 2013-02-28 01:01:40 +0000 | [diff] [blame] | 146 | ret = gpio_request(sda_pin, "sda"); |
| 147 | if (ret) { |
| 148 | if (ret == -EINVAL) |
| 149 | ret = -EPROBE_DEFER; /* Try again later */ |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 150 | goto err_request_sda; |
Jean Delvare | 1b295c8 | 2013-02-28 01:01:40 +0000 | [diff] [blame] | 151 | } |
| 152 | ret = gpio_request(scl_pin, "scl"); |
| 153 | if (ret) { |
| 154 | if (ret == -EINVAL) |
| 155 | ret = -EPROBE_DEFER; /* Try again later */ |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 156 | goto err_request_scl; |
Jean Delvare | 1b295c8 | 2013-02-28 01:01:40 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
| 160 | if (!priv) { |
| 161 | ret = -ENOMEM; |
| 162 | goto err_add_bus; |
| 163 | } |
| 164 | adap = &priv->adap; |
| 165 | bit_data = &priv->bit_data; |
| 166 | pdata = &priv->pdata; |
| 167 | |
| 168 | if (pdev->dev.of_node) { |
| 169 | pdata->sda_pin = sda_pin; |
| 170 | pdata->scl_pin = scl_pin; |
| 171 | of_i2c_gpio_get_props(pdev->dev.of_node, pdata); |
| 172 | } else { |
Jingoo Han | 6d4028c | 2013-07-30 16:59:33 +0900 | [diff] [blame] | 173 | memcpy(pdata, dev_get_platdata(&pdev->dev), sizeof(*pdata)); |
Jean Delvare | 1b295c8 | 2013-02-28 01:01:40 +0000 | [diff] [blame] | 174 | } |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 175 | |
| 176 | if (pdata->sda_is_open_drain) { |
| 177 | gpio_direction_output(pdata->sda_pin, 1); |
| 178 | bit_data->setsda = i2c_gpio_setsda_val; |
| 179 | } else { |
| 180 | gpio_direction_input(pdata->sda_pin); |
| 181 | bit_data->setsda = i2c_gpio_setsda_dir; |
| 182 | } |
| 183 | |
| 184 | if (pdata->scl_is_open_drain || pdata->scl_is_output_only) { |
| 185 | gpio_direction_output(pdata->scl_pin, 1); |
| 186 | bit_data->setscl = i2c_gpio_setscl_val; |
| 187 | } else { |
| 188 | gpio_direction_input(pdata->scl_pin); |
| 189 | bit_data->setscl = i2c_gpio_setscl_dir; |
| 190 | } |
| 191 | |
| 192 | if (!pdata->scl_is_output_only) |
| 193 | bit_data->getscl = i2c_gpio_getscl; |
| 194 | bit_data->getsda = i2c_gpio_getsda; |
| 195 | |
| 196 | if (pdata->udelay) |
| 197 | bit_data->udelay = pdata->udelay; |
| 198 | else if (pdata->scl_is_output_only) |
| 199 | bit_data->udelay = 50; /* 10 kHz */ |
| 200 | else |
| 201 | bit_data->udelay = 5; /* 100 kHz */ |
| 202 | |
| 203 | if (pdata->timeout) |
| 204 | bit_data->timeout = pdata->timeout; |
| 205 | else |
| 206 | bit_data->timeout = HZ / 10; /* 100 ms */ |
| 207 | |
| 208 | bit_data->data = pdata; |
| 209 | |
| 210 | adap->owner = THIS_MODULE; |
Bo Shen | 58a7371 | 2012-10-15 15:51:17 +0800 | [diff] [blame] | 211 | if (pdev->dev.of_node) |
| 212 | strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name)); |
| 213 | else |
| 214 | snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id); |
| 215 | |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 216 | adap->algo_data = bit_data; |
Jean Delvare | 3401b2f | 2008-07-14 22:38:29 +0200 | [diff] [blame] | 217 | adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 218 | adap->dev.parent = &pdev->dev; |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 219 | adap->dev.of_node = pdev->dev.of_node; |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 220 | |
Karol Lewandowski | 44454ba | 2012-03-27 11:10:28 +0200 | [diff] [blame] | 221 | adap->nr = pdev->id; |
Atsushi Nemoto | 7e69c3a | 2007-07-12 14:12:30 +0200 | [diff] [blame] | 222 | ret = i2c_bit_add_numbered_bus(adap); |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 223 | if (ret) |
| 224 | goto err_add_bus; |
| 225 | |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 226 | platform_set_drvdata(pdev, priv); |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 227 | |
| 228 | dev_info(&pdev->dev, "using pins %u (SDA) and %u (SCL%s)\n", |
| 229 | pdata->sda_pin, pdata->scl_pin, |
| 230 | pdata->scl_is_output_only |
| 231 | ? ", no clock stretching" : ""); |
| 232 | |
| 233 | return 0; |
| 234 | |
| 235 | err_add_bus: |
Jean Delvare | 1b295c8 | 2013-02-28 01:01:40 +0000 | [diff] [blame] | 236 | gpio_free(scl_pin); |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 237 | err_request_scl: |
Jean Delvare | 1b295c8 | 2013-02-28 01:01:40 +0000 | [diff] [blame] | 238 | gpio_free(sda_pin); |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 239 | err_request_sda: |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 240 | return ret; |
| 241 | } |
| 242 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 243 | static int i2c_gpio_remove(struct platform_device *pdev) |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 244 | { |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 245 | struct i2c_gpio_private_data *priv; |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 246 | struct i2c_gpio_platform_data *pdata; |
| 247 | struct i2c_adapter *adap; |
| 248 | |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 249 | priv = platform_get_drvdata(pdev); |
| 250 | adap = &priv->adap; |
| 251 | pdata = &priv->pdata; |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 252 | |
| 253 | i2c_del_adapter(adap); |
| 254 | gpio_free(pdata->scl_pin); |
| 255 | gpio_free(pdata->sda_pin); |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 260 | #if defined(CONFIG_OF) |
| 261 | static const struct of_device_id i2c_gpio_dt_ids[] = { |
| 262 | { .compatible = "i2c-gpio", }, |
| 263 | { /* sentinel */ } |
| 264 | }; |
| 265 | |
| 266 | MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids); |
| 267 | #endif |
| 268 | |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 269 | static struct platform_driver i2c_gpio_driver = { |
| 270 | .driver = { |
| 271 | .name = "i2c-gpio", |
| 272 | .owner = THIS_MODULE, |
Jean-Christophe PLAGNIOL-VILLARD | 8ffaa0f | 2012-02-05 18:22:34 +0800 | [diff] [blame] | 273 | .of_match_table = of_match_ptr(i2c_gpio_dt_ids), |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 274 | }, |
Ben Dooks | 1efe7c5 | 2008-07-28 12:04:09 +0100 | [diff] [blame] | 275 | .probe = i2c_gpio_probe, |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 276 | .remove = i2c_gpio_remove, |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 277 | }; |
| 278 | |
| 279 | static int __init i2c_gpio_init(void) |
| 280 | { |
| 281 | int ret; |
| 282 | |
Ben Dooks | 1efe7c5 | 2008-07-28 12:04:09 +0100 | [diff] [blame] | 283 | ret = platform_driver_register(&i2c_gpio_driver); |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 284 | if (ret) |
| 285 | printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret); |
| 286 | |
| 287 | return ret; |
| 288 | } |
Marek Szyprowski | b868078 | 2010-05-21 18:40:58 +0200 | [diff] [blame] | 289 | subsys_initcall(i2c_gpio_init); |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 290 | |
| 291 | static void __exit i2c_gpio_exit(void) |
| 292 | { |
| 293 | platform_driver_unregister(&i2c_gpio_driver); |
| 294 | } |
| 295 | module_exit(i2c_gpio_exit); |
| 296 | |
Jean Delvare | e05503e | 2011-05-18 16:49:24 +0200 | [diff] [blame] | 297 | MODULE_AUTHOR("Haavard Skinnemoen (Atmel)"); |
Haavard Skinnemoen | 1c23af9 | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 298 | MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver"); |
| 299 | MODULE_LICENSE("GPL"); |
Kay Sievers | add8eda | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 300 | MODULE_ALIAS("platform:i2c-gpio"); |