pcf857x.c 9.1 KB

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