| 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> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/gpio/consumer.h> |
| 18 | #include <linux/of.h> |
| 19 | |
| 20 | struct i2c_gpio_private_data { |
| 21 | struct gpio_desc *sda; |
| 22 | struct gpio_desc *scl; |
| 23 | struct i2c_adapter adap; |
| 24 | struct i2c_algo_bit_data bit_data; |
| 25 | struct i2c_gpio_platform_data pdata; |
| 26 | }; |
| 27 | |
| 28 | /* Toggle SDA by changing the direction of the pin */ |
| 29 | static void i2c_gpio_setsda_dir(void *data, int state) |
| 30 | { |
| 31 | struct i2c_gpio_private_data *priv = data; |
| 32 | |
| 33 | /* |
| 34 | * This is a way of saying "do not drive |
| 35 | * me actively high" which means emulating open drain. |
| 36 | * The right way to do this is for gpiolib to |
| 37 | * handle this, by the function below. |
| 38 | */ |
| 39 | if (state) |
| 40 | gpiod_direction_input(priv->sda); |
| 41 | else |
| 42 | gpiod_direction_output(priv->sda, 0); |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * Toggle SDA by changing the output value of the pin. This is only |
| 47 | * valid for pins configured as open drain (i.e. setting the value |
| 48 | * high effectively turns off the output driver.) |
| 49 | */ |
| 50 | static void i2c_gpio_setsda_val(void *data, int state) |
| 51 | { |
| 52 | struct i2c_gpio_private_data *priv = data; |
| 53 | |
| 54 | gpiod_set_value(priv->sda, state); |
| 55 | } |
| 56 | |
| 57 | /* Toggle SCL by changing the direction of the pin. */ |
| 58 | static void i2c_gpio_setscl_dir(void *data, int state) |
| 59 | { |
| 60 | struct i2c_gpio_private_data *priv = data; |
| 61 | |
| 62 | if (state) |
| 63 | gpiod_direction_input(priv->scl); |
| 64 | else |
| 65 | gpiod_direction_output(priv->scl, 0); |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Toggle SCL by changing the output value of the pin. This is used |
| 70 | * for pins that are configured as open drain and for output-only |
| 71 | * pins. The latter case will break the i2c protocol, but it will |
| 72 | * often work in practice. |
| 73 | */ |
| 74 | static void i2c_gpio_setscl_val(void *data, int state) |
| 75 | { |
| 76 | struct i2c_gpio_private_data *priv = data; |
| 77 | |
| 78 | gpiod_set_value(priv->scl, state); |
| 79 | } |
| 80 | |
| 81 | static int i2c_gpio_getsda(void *data) |
| 82 | { |
| 83 | struct i2c_gpio_private_data *priv = data; |
| 84 | |
| 85 | return gpiod_get_value(priv->sda); |
| 86 | } |
| 87 | |
| 88 | static int i2c_gpio_getscl(void *data) |
| 89 | { |
| 90 | struct i2c_gpio_private_data *priv = data; |
| 91 | |
| 92 | return gpiod_get_value(priv->scl); |
| 93 | } |
| 94 | |
| 95 | static void of_i2c_gpio_get_props(struct device_node *np, |
| 96 | struct i2c_gpio_platform_data *pdata) |
| 97 | { |
| 98 | u32 reg; |
| 99 | |
| 100 | of_property_read_u32(np, "i2c-gpio,delay-us", &pdata->udelay); |
| 101 | |
| 102 | if (!of_property_read_u32(np, "i2c-gpio,timeout-ms", ®)) |
| 103 | pdata->timeout = msecs_to_jiffies(reg); |
| 104 | |
| 105 | pdata->sda_is_open_drain = |
| 106 | of_property_read_bool(np, "i2c-gpio,sda-open-drain"); |
| 107 | pdata->scl_is_open_drain = |
| 108 | of_property_read_bool(np, "i2c-gpio,scl-open-drain"); |
| 109 | pdata->scl_is_output_only = |
| 110 | of_property_read_bool(np, "i2c-gpio,scl-output-only"); |
| 111 | } |
| 112 | |
| 113 | static int i2c_gpio_probe(struct platform_device *pdev) |
| 114 | { |
| 115 | struct i2c_gpio_private_data *priv; |
| 116 | struct i2c_gpio_platform_data *pdata; |
| 117 | struct i2c_algo_bit_data *bit_data; |
| 118 | struct i2c_adapter *adap; |
| 119 | int ret; |
| 120 | |
| 121 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
| 122 | if (!priv) |
| 123 | return -ENOMEM; |
| 124 | |
| 125 | /* First get the GPIO pins; if it fails, we'll defer the probe. */ |
| 126 | priv->sda = devm_gpiod_get_index(&pdev->dev, NULL, 0, GPIOD_OUT_HIGH); |
| 127 | if (IS_ERR(priv->sda)) { |
| 128 | ret = PTR_ERR(priv->sda); |
| 129 | /* FIXME: hack in the old code, is this really necessary? */ |
| 130 | if (ret == -EINVAL) |
| 131 | ret = -EPROBE_DEFER; |
| 132 | return ret; |
| 133 | } |
| 134 | priv->scl = devm_gpiod_get_index(&pdev->dev, NULL, 1, GPIOD_OUT_LOW); |
| 135 | if (IS_ERR(priv->scl)) { |
| 136 | ret = PTR_ERR(priv->scl); |
| 137 | /* FIXME: hack in the old code, is this really necessary? */ |
| 138 | if (ret == -EINVAL) |
| 139 | ret = -EPROBE_DEFER; |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | adap = &priv->adap; |
| 144 | bit_data = &priv->bit_data; |
| 145 | pdata = &priv->pdata; |
| 146 | |
| 147 | if (pdev->dev.of_node) { |
| 148 | of_i2c_gpio_get_props(pdev->dev.of_node, pdata); |
| 149 | } else { |
| 150 | /* |
| 151 | * If all platform data settings are zero it is OK |
| 152 | * to not provide any platform data from the board. |
| 153 | */ |
| 154 | if (dev_get_platdata(&pdev->dev)) |
| 155 | memcpy(pdata, dev_get_platdata(&pdev->dev), |
| 156 | sizeof(*pdata)); |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * FIXME: this is a hack emulating the open drain emulation |
| 161 | * that gpiolib can already do for us. Make all clients properly |
| 162 | * flag their lines as open drain and get rid of this property |
| 163 | * and the special callback. |
| 164 | */ |
| 165 | if (pdata->sda_is_open_drain) { |
| 166 | gpiod_direction_output(priv->sda, 1); |
| 167 | bit_data->setsda = i2c_gpio_setsda_val; |
| 168 | } else { |
| 169 | gpiod_direction_input(priv->sda); |
| 170 | bit_data->setsda = i2c_gpio_setsda_dir; |
| 171 | } |
| 172 | |
| 173 | if (pdata->scl_is_open_drain || pdata->scl_is_output_only) { |
| 174 | gpiod_direction_output(priv->scl, 1); |
| 175 | bit_data->setscl = i2c_gpio_setscl_val; |
| 176 | } else { |
| 177 | gpiod_direction_input(priv->scl); |
| 178 | bit_data->setscl = i2c_gpio_setscl_dir; |
| 179 | } |
| 180 | |
| 181 | if (!pdata->scl_is_output_only) |
| 182 | bit_data->getscl = i2c_gpio_getscl; |
| 183 | bit_data->getsda = i2c_gpio_getsda; |
| 184 | |
| 185 | if (pdata->udelay) |
| 186 | bit_data->udelay = pdata->udelay; |
| 187 | else if (pdata->scl_is_output_only) |
| 188 | bit_data->udelay = 50; /* 10 kHz */ |
| 189 | else |
| 190 | bit_data->udelay = 5; /* 100 kHz */ |
| 191 | |
| 192 | if (pdata->timeout) |
| 193 | bit_data->timeout = pdata->timeout; |
| 194 | else |
| 195 | bit_data->timeout = HZ / 10; /* 100 ms */ |
| 196 | |
| 197 | bit_data->data = priv; |
| 198 | |
| 199 | adap->owner = THIS_MODULE; |
| 200 | if (pdev->dev.of_node) |
| 201 | strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name)); |
| 202 | else |
| 203 | snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id); |
| 204 | |
| 205 | adap->algo_data = bit_data; |
| 206 | adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; |
| 207 | adap->dev.parent = &pdev->dev; |
| 208 | adap->dev.of_node = pdev->dev.of_node; |
| 209 | |
| 210 | adap->nr = pdev->id; |
| 211 | ret = i2c_bit_add_numbered_bus(adap); |
| 212 | if (ret) |
| 213 | return ret; |
| 214 | |
| 215 | platform_set_drvdata(pdev, priv); |
| 216 | |
| 217 | /* |
| 218 | * FIXME: using global GPIO numbers is not helpful. If/when we |
| 219 | * get accessors to get the actual name of the GPIO line, |
| 220 | * from the descriptor, then provide that instead. |
| 221 | */ |
| 222 | dev_info(&pdev->dev, "using lines %u (SDA) and %u (SCL%s)\n", |
| 223 | desc_to_gpio(priv->sda), desc_to_gpio(priv->scl), |
| 224 | pdata->scl_is_output_only |
| 225 | ? ", no clock stretching" : ""); |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | static int i2c_gpio_remove(struct platform_device *pdev) |
| 231 | { |
| 232 | struct i2c_gpio_private_data *priv; |
| 233 | struct i2c_adapter *adap; |
| 234 | |
| 235 | priv = platform_get_drvdata(pdev); |
| 236 | adap = &priv->adap; |
| 237 | |
| 238 | i2c_del_adapter(adap); |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | #if defined(CONFIG_OF) |
| 244 | static const struct of_device_id i2c_gpio_dt_ids[] = { |
| 245 | { .compatible = "i2c-gpio", }, |
| 246 | { /* sentinel */ } |
| 247 | }; |
| 248 | |
| 249 | MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids); |
| 250 | #endif |
| 251 | |
| 252 | static struct platform_driver i2c_gpio_driver = { |
| 253 | .driver = { |
| 254 | .name = "i2c-gpio", |
| 255 | .of_match_table = of_match_ptr(i2c_gpio_dt_ids), |
| 256 | }, |
| 257 | .probe = i2c_gpio_probe, |
| 258 | .remove = i2c_gpio_remove, |
| 259 | }; |
| 260 | |
| 261 | static int __init i2c_gpio_init(void) |
| 262 | { |
| 263 | int ret; |
| 264 | |
| 265 | ret = platform_driver_register(&i2c_gpio_driver); |
| 266 | if (ret) |
| 267 | printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret); |
| 268 | |
| 269 | return ret; |
| 270 | } |
| 271 | subsys_initcall(i2c_gpio_init); |
| 272 | |
| 273 | static void __exit i2c_gpio_exit(void) |
| 274 | { |
| 275 | platform_driver_unregister(&i2c_gpio_driver); |
| 276 | } |
| 277 | module_exit(i2c_gpio_exit); |
| 278 | |
| 279 | MODULE_AUTHOR("Haavard Skinnemoen (Atmel)"); |
| 280 | MODULE_DESCRIPTION("Platform-independent bitbanging I2C driver"); |
| 281 | MODULE_LICENSE("GPL"); |
| 282 | MODULE_ALIAS("platform:i2c-gpio"); |