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