pcf857x.c 9.1 KB

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