gpio-pcf857x.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Driver for pcf857x, pca857x, and pca967x I2C GPIO expanders
  3. *
  4. * Copyright (C) 2007 David Brownell
  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 as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/slab.h>
  22. #include <linux/gpio.h>
  23. #include <linux/i2c.h>
  24. #include <linux/i2c/pcf857x.h>
  25. #include <linux/module.h>
  26. static const struct i2c_device_id pcf857x_id[] = {
  27. { "pcf8574", 8 },
  28. { "pcf8574a", 8 },
  29. { "pca8574", 8 },
  30. { "pca9670", 8 },
  31. { "pca9672", 8 },
  32. { "pca9674", 8 },
  33. { "pcf8575", 16 },
  34. { "pca8575", 16 },
  35. { "pca9671", 16 },
  36. { "pca9673", 16 },
  37. { "pca9675", 16 },
  38. { "max7328", 8 },
  39. { "max7329", 8 },
  40. { }
  41. };
  42. MODULE_DEVICE_TABLE(i2c, pcf857x_id);
  43. /*
  44. * The pcf857x, pca857x, and pca967x chips only expose one read and one
  45. * write register. Writing a "one" bit (to match the reset state) lets
  46. * that pin be used as an input; it's not an open-drain model, but acts
  47. * a bit like one. This is described as "quasi-bidirectional"; read the
  48. * chip documentation for details.
  49. *
  50. * Many other I2C GPIO expander chips (like the pca953x models) have
  51. * more complex register models and more conventional circuitry using
  52. * push/pull drivers. They often use the same 0x20..0x27 addresses as
  53. * pcf857x parts, making the "legacy" I2C driver model problematic.
  54. */
  55. struct pcf857x {
  56. struct gpio_chip chip;
  57. struct i2c_client *client;
  58. struct mutex lock; /* protect 'out' */
  59. unsigned out; /* software latch */
  60. int (*write)(struct i2c_client *client, unsigned data);
  61. int (*read)(struct i2c_client *client);
  62. };
  63. /*-------------------------------------------------------------------------*/
  64. /* Talk to 8-bit I/O expander */
  65. static int i2c_write_le8(struct i2c_client *client, unsigned data)
  66. {
  67. return i2c_smbus_write_byte(client, data);
  68. }
  69. static int i2c_read_le8(struct i2c_client *client)
  70. {
  71. return (int)i2c_smbus_read_byte(client);
  72. }
  73. /* Talk to 16-bit I/O expander */
  74. static int i2c_write_le16(struct i2c_client *client, unsigned word)
  75. {
  76. u8 buf[2] = { word & 0xff, word >> 8, };
  77. int status;
  78. status = i2c_master_send(client, buf, 2);
  79. return (status < 0) ? status : 0;
  80. }
  81. static int i2c_read_le16(struct i2c_client *client)
  82. {
  83. u8 buf[2];
  84. int status;
  85. status = i2c_master_recv(client, buf, 2);
  86. if (status < 0)
  87. return status;
  88. return (buf[1] << 8) | buf[0];
  89. }
  90. /*-------------------------------------------------------------------------*/
  91. static int pcf857x_input(struct gpio_chip *chip, unsigned offset)
  92. {
  93. struct pcf857x *gpio = container_of(chip, struct pcf857x, chip);
  94. int status;
  95. mutex_lock(&gpio->lock);
  96. gpio->out |= (1 << offset);
  97. status = gpio->write(gpio->client, gpio->out);
  98. mutex_unlock(&gpio->lock);
  99. return status;
  100. }
  101. static int pcf857x_get(struct gpio_chip *chip, unsigned offset)
  102. {
  103. struct pcf857x *gpio = container_of(chip, struct pcf857x, chip);
  104. int value;
  105. value = gpio->read(gpio->client);
  106. return (value < 0) ? 0 : (value & (1 << offset));
  107. }
  108. static int pcf857x_output(struct gpio_chip *chip, unsigned offset, int value)
  109. {
  110. struct pcf857x *gpio = container_of(chip, struct pcf857x, chip);
  111. unsigned bit = 1 << offset;
  112. int status;
  113. mutex_lock(&gpio->lock);
  114. if (value)
  115. gpio->out |= bit;
  116. else
  117. gpio->out &= ~bit;
  118. status = gpio->write(gpio->client, gpio->out);
  119. mutex_unlock(&gpio->lock);
  120. return status;
  121. }
  122. static void pcf857x_set(struct gpio_chip *chip, unsigned offset, int value)
  123. {
  124. pcf857x_output(chip, offset, value);
  125. }
  126. /*-------------------------------------------------------------------------*/
  127. static int pcf857x_probe(struct i2c_client *client,
  128. const struct i2c_device_id *id)
  129. {
  130. struct pcf857x_platform_data *pdata;
  131. struct pcf857x *gpio;
  132. int status;
  133. pdata = client->dev.platform_data;
  134. if (!pdata) {
  135. dev_dbg(&client->dev, "no platform data\n");
  136. }
  137. /* Allocate, initialize, and register this gpio_chip. */
  138. gpio = kzalloc(sizeof *gpio, GFP_KERNEL);
  139. if (!gpio)
  140. return -ENOMEM;
  141. mutex_init(&gpio->lock);
  142. gpio->chip.base = pdata ? pdata->gpio_base : -1;
  143. gpio->chip.can_sleep = 1;
  144. gpio->chip.dev = &client->dev;
  145. gpio->chip.owner = THIS_MODULE;
  146. gpio->chip.get = pcf857x_get;
  147. gpio->chip.set = pcf857x_set;
  148. gpio->chip.direction_input = pcf857x_input;
  149. gpio->chip.direction_output = pcf857x_output;
  150. gpio->chip.ngpio = id->driver_data;
  151. /* NOTE: the OnSemi jlc1562b is also largely compatible with
  152. * these parts, notably for output. It has a low-resolution
  153. * DAC instead of pin change IRQs; and its inputs can be the
  154. * result of comparators.
  155. */
  156. /* 8574 addresses are 0x20..0x27; 8574a uses 0x38..0x3f;
  157. * 9670, 9672, 9764, and 9764a use quite a variety.
  158. *
  159. * NOTE: we don't distinguish here between *4 and *4a parts.
  160. */
  161. if (gpio->chip.ngpio == 8) {
  162. gpio->write = i2c_write_le8;
  163. gpio->read = i2c_read_le8;
  164. if (!i2c_check_functionality(client->adapter,
  165. I2C_FUNC_SMBUS_BYTE))
  166. status = -EIO;
  167. /* fail if there's no chip present */
  168. else
  169. status = i2c_smbus_read_byte(client);
  170. /* '75/'75c addresses are 0x20..0x27, just like the '74;
  171. * the '75c doesn't have a current source pulling high.
  172. * 9671, 9673, and 9765 use quite a variety of addresses.
  173. *
  174. * NOTE: we don't distinguish here between '75 and '75c parts.
  175. */
  176. } else if (gpio->chip.ngpio == 16) {
  177. gpio->write = i2c_write_le16;
  178. gpio->read = i2c_read_le16;
  179. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  180. status = -EIO;
  181. /* fail if there's no chip present */
  182. else
  183. status = i2c_read_le16(client);
  184. } else {
  185. dev_dbg(&client->dev, "unsupported number of gpios\n");
  186. status = -EINVAL;
  187. }
  188. if (status < 0)
  189. goto fail;
  190. gpio->chip.label = client->name;
  191. gpio->client = client;
  192. i2c_set_clientdata(client, gpio);
  193. /* NOTE: these chips have strange "quasi-bidirectional" I/O pins.
  194. * We can't actually know whether a pin is configured (a) as output
  195. * and driving the signal low, or (b) as input and reporting a low
  196. * value ... without knowing the last value written since the chip
  197. * came out of reset (if any). We can't read the latched output.
  198. *
  199. * In short, the only reliable solution for setting up pin direction
  200. * is to do it explicitly. The setup() method can do that, but it
  201. * may cause transient glitching since it can't know the last value
  202. * written (some pins may need to be driven low).
  203. *
  204. * Using pdata->n_latch avoids that trouble. When left initialized
  205. * to zero, our software copy of the "latch" then matches the chip's
  206. * all-ones reset state. Otherwise it flags pins to be driven low.
  207. */
  208. gpio->out = pdata ? ~pdata->n_latch : ~0;
  209. status = gpiochip_add(&gpio->chip);
  210. if (status < 0)
  211. goto fail;
  212. /* NOTE: these chips can issue "some pin-changed" IRQs, which we
  213. * don't yet even try to use. Among other issues, the relevant
  214. * genirq state isn't available to modular drivers; and most irq
  215. * methods can't be called from sleeping contexts.
  216. */
  217. dev_info(&client->dev, "%s\n",
  218. client->irq ? " (irq ignored)" : "");
  219. /* Let platform code set up the GPIOs and their users.
  220. * Now is the first time anyone could use them.
  221. */
  222. if (pdata && pdata->setup) {
  223. status = pdata->setup(client,
  224. gpio->chip.base, gpio->chip.ngpio,
  225. pdata->context);
  226. if (status < 0)
  227. dev_warn(&client->dev, "setup --> %d\n", status);
  228. }
  229. return 0;
  230. fail:
  231. dev_dbg(&client->dev, "probe error %d for '%s'\n",
  232. status, client->name);
  233. kfree(gpio);
  234. return status;
  235. }
  236. static int pcf857x_remove(struct i2c_client *client)
  237. {
  238. struct pcf857x_platform_data *pdata = client->dev.platform_data;
  239. struct pcf857x *gpio = i2c_get_clientdata(client);
  240. int status = 0;
  241. if (pdata && pdata->teardown) {
  242. status = pdata->teardown(client,
  243. gpio->chip.base, gpio->chip.ngpio,
  244. pdata->context);
  245. if (status < 0) {
  246. dev_err(&client->dev, "%s --> %d\n",
  247. "teardown", status);
  248. return status;
  249. }
  250. }
  251. status = gpiochip_remove(&gpio->chip);
  252. if (status == 0)
  253. kfree(gpio);
  254. else
  255. dev_err(&client->dev, "%s --> %d\n", "remove", status);
  256. return status;
  257. }
  258. static struct i2c_driver pcf857x_driver = {
  259. .driver = {
  260. .name = "pcf857x",
  261. .owner = THIS_MODULE,
  262. },
  263. .probe = pcf857x_probe,
  264. .remove = pcf857x_remove,
  265. .id_table = pcf857x_id,
  266. };
  267. static int __init pcf857x_init(void)
  268. {
  269. return i2c_add_driver(&pcf857x_driver);
  270. }
  271. /* register after i2c postcore initcall and before
  272. * subsys initcalls that may rely on these GPIOs
  273. */
  274. subsys_initcall(pcf857x_init);
  275. static void __exit pcf857x_exit(void)
  276. {
  277. i2c_del_driver(&pcf857x_driver);
  278. }
  279. module_exit(pcf857x_exit);
  280. MODULE_LICENSE("GPL");
  281. MODULE_AUTHOR("David Brownell");