gpio-pcf857x.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. };
  61. /*-------------------------------------------------------------------------*/
  62. /* Talk to 8-bit I/O expander */
  63. static int pcf857x_input8(struct gpio_chip *chip, unsigned offset)
  64. {
  65. struct pcf857x *gpio = container_of(chip, struct pcf857x, chip);
  66. int status;
  67. mutex_lock(&gpio->lock);
  68. gpio->out |= (1 << offset);
  69. status = i2c_smbus_write_byte(gpio->client, gpio->out);
  70. mutex_unlock(&gpio->lock);
  71. return status;
  72. }
  73. static int pcf857x_get8(struct gpio_chip *chip, unsigned offset)
  74. {
  75. struct pcf857x *gpio = container_of(chip, struct pcf857x, chip);
  76. s32 value;
  77. value = i2c_smbus_read_byte(gpio->client);
  78. return (value < 0) ? 0 : (value & (1 << offset));
  79. }
  80. static int pcf857x_output8(struct gpio_chip *chip, unsigned offset, int value)
  81. {
  82. struct pcf857x *gpio = container_of(chip, struct pcf857x, chip);
  83. unsigned bit = 1 << offset;
  84. int status;
  85. mutex_lock(&gpio->lock);
  86. if (value)
  87. gpio->out |= bit;
  88. else
  89. gpio->out &= ~bit;
  90. status = i2c_smbus_write_byte(gpio->client, gpio->out);
  91. mutex_unlock(&gpio->lock);
  92. return status;
  93. }
  94. static void pcf857x_set8(struct gpio_chip *chip, unsigned offset, int value)
  95. {
  96. pcf857x_output8(chip, offset, value);
  97. }
  98. /*-------------------------------------------------------------------------*/
  99. /* Talk to 16-bit I/O expander */
  100. static int i2c_write_le16(struct i2c_client *client, u16 word)
  101. {
  102. u8 buf[2] = { word & 0xff, word >> 8, };
  103. int status;
  104. status = i2c_master_send(client, buf, 2);
  105. return (status < 0) ? status : 0;
  106. }
  107. static int i2c_read_le16(struct i2c_client *client)
  108. {
  109. u8 buf[2];
  110. int status;
  111. status = i2c_master_recv(client, buf, 2);
  112. if (status < 0)
  113. return status;
  114. return (buf[1] << 8) | buf[0];
  115. }
  116. static int pcf857x_input16(struct gpio_chip *chip, unsigned offset)
  117. {
  118. struct pcf857x *gpio = container_of(chip, struct pcf857x, chip);
  119. int status;
  120. mutex_lock(&gpio->lock);
  121. gpio->out |= (1 << offset);
  122. status = i2c_write_le16(gpio->client, gpio->out);
  123. mutex_unlock(&gpio->lock);
  124. return status;
  125. }
  126. static int pcf857x_get16(struct gpio_chip *chip, unsigned offset)
  127. {
  128. struct pcf857x *gpio = container_of(chip, struct pcf857x, chip);
  129. int value;
  130. value = i2c_read_le16(gpio->client);
  131. return (value < 0) ? 0 : (value & (1 << offset));
  132. }
  133. static int pcf857x_output16(struct gpio_chip *chip, unsigned offset, int value)
  134. {
  135. struct pcf857x *gpio = container_of(chip, struct pcf857x, chip);
  136. unsigned bit = 1 << offset;
  137. int status;
  138. mutex_lock(&gpio->lock);
  139. if (value)
  140. gpio->out |= bit;
  141. else
  142. gpio->out &= ~bit;
  143. status = i2c_write_le16(gpio->client, gpio->out);
  144. mutex_unlock(&gpio->lock);
  145. return status;
  146. }
  147. static void pcf857x_set16(struct gpio_chip *chip, unsigned offset, int value)
  148. {
  149. pcf857x_output16(chip, offset, value);
  150. }
  151. /*-------------------------------------------------------------------------*/
  152. static int pcf857x_probe(struct i2c_client *client,
  153. const struct i2c_device_id *id)
  154. {
  155. struct pcf857x_platform_data *pdata;
  156. struct pcf857x *gpio;
  157. int status;
  158. pdata = client->dev.platform_data;
  159. if (!pdata) {
  160. dev_dbg(&client->dev, "no platform data\n");
  161. }
  162. /* Allocate, initialize, and register this gpio_chip. */
  163. gpio = kzalloc(sizeof *gpio, GFP_KERNEL);
  164. if (!gpio)
  165. return -ENOMEM;
  166. mutex_init(&gpio->lock);
  167. gpio->chip.base = pdata ? pdata->gpio_base : -1;
  168. gpio->chip.can_sleep = 1;
  169. gpio->chip.dev = &client->dev;
  170. gpio->chip.owner = THIS_MODULE;
  171. /* NOTE: the OnSemi jlc1562b is also largely compatible with
  172. * these parts, notably for output. It has a low-resolution
  173. * DAC instead of pin change IRQs; and its inputs can be the
  174. * result of comparators.
  175. */
  176. /* 8574 addresses are 0x20..0x27; 8574a uses 0x38..0x3f;
  177. * 9670, 9672, 9764, and 9764a use quite a variety.
  178. *
  179. * NOTE: we don't distinguish here between *4 and *4a parts.
  180. */
  181. gpio->chip.ngpio = id->driver_data;
  182. if (gpio->chip.ngpio == 8) {
  183. gpio->chip.direction_input = pcf857x_input8;
  184. gpio->chip.get = pcf857x_get8;
  185. gpio->chip.direction_output = pcf857x_output8;
  186. gpio->chip.set = pcf857x_set8;
  187. if (!i2c_check_functionality(client->adapter,
  188. I2C_FUNC_SMBUS_BYTE))
  189. status = -EIO;
  190. /* fail if there's no chip present */
  191. else
  192. status = i2c_smbus_read_byte(client);
  193. /* '75/'75c addresses are 0x20..0x27, just like the '74;
  194. * the '75c doesn't have a current source pulling high.
  195. * 9671, 9673, and 9765 use quite a variety of addresses.
  196. *
  197. * NOTE: we don't distinguish here between '75 and '75c parts.
  198. */
  199. } else if (gpio->chip.ngpio == 16) {
  200. gpio->chip.direction_input = pcf857x_input16;
  201. gpio->chip.get = pcf857x_get16;
  202. gpio->chip.direction_output = pcf857x_output16;
  203. gpio->chip.set = pcf857x_set16;
  204. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  205. status = -EIO;
  206. /* fail if there's no chip present */
  207. else
  208. status = i2c_read_le16(client);
  209. } else {
  210. dev_dbg(&client->dev, "unsupported number of gpios\n");
  211. status = -EINVAL;
  212. }
  213. if (status < 0)
  214. goto fail;
  215. gpio->chip.label = client->name;
  216. gpio->client = client;
  217. i2c_set_clientdata(client, gpio);
  218. /* NOTE: these chips have strange "quasi-bidirectional" I/O pins.
  219. * We can't actually know whether a pin is configured (a) as output
  220. * and driving the signal low, or (b) as input and reporting a low
  221. * value ... without knowing the last value written since the chip
  222. * came out of reset (if any). We can't read the latched output.
  223. *
  224. * In short, the only reliable solution for setting up pin direction
  225. * is to do it explicitly. The setup() method can do that, but it
  226. * may cause transient glitching since it can't know the last value
  227. * written (some pins may need to be driven low).
  228. *
  229. * Using pdata->n_latch avoids that trouble. When left initialized
  230. * to zero, our software copy of the "latch" then matches the chip's
  231. * all-ones reset state. Otherwise it flags pins to be driven low.
  232. */
  233. gpio->out = pdata ? ~pdata->n_latch : ~0;
  234. status = gpiochip_add(&gpio->chip);
  235. if (status < 0)
  236. goto fail;
  237. /* NOTE: these chips can issue "some pin-changed" IRQs, which we
  238. * don't yet even try to use. Among other issues, the relevant
  239. * genirq state isn't available to modular drivers; and most irq
  240. * methods can't be called from sleeping contexts.
  241. */
  242. dev_info(&client->dev, "%s\n",
  243. client->irq ? " (irq ignored)" : "");
  244. /* Let platform code set up the GPIOs and their users.
  245. * Now is the first time anyone could use them.
  246. */
  247. if (pdata && pdata->setup) {
  248. status = pdata->setup(client,
  249. gpio->chip.base, gpio->chip.ngpio,
  250. pdata->context);
  251. if (status < 0)
  252. dev_warn(&client->dev, "setup --> %d\n", status);
  253. }
  254. return 0;
  255. fail:
  256. dev_dbg(&client->dev, "probe error %d for '%s'\n",
  257. status, client->name);
  258. kfree(gpio);
  259. return status;
  260. }
  261. static int pcf857x_remove(struct i2c_client *client)
  262. {
  263. struct pcf857x_platform_data *pdata = client->dev.platform_data;
  264. struct pcf857x *gpio = i2c_get_clientdata(client);
  265. int status = 0;
  266. if (pdata && pdata->teardown) {
  267. status = pdata->teardown(client,
  268. gpio->chip.base, gpio->chip.ngpio,
  269. pdata->context);
  270. if (status < 0) {
  271. dev_err(&client->dev, "%s --> %d\n",
  272. "teardown", status);
  273. return status;
  274. }
  275. }
  276. status = gpiochip_remove(&gpio->chip);
  277. if (status == 0)
  278. kfree(gpio);
  279. else
  280. dev_err(&client->dev, "%s --> %d\n", "remove", status);
  281. return status;
  282. }
  283. static struct i2c_driver pcf857x_driver = {
  284. .driver = {
  285. .name = "pcf857x",
  286. .owner = THIS_MODULE,
  287. },
  288. .probe = pcf857x_probe,
  289. .remove = pcf857x_remove,
  290. .id_table = pcf857x_id,
  291. };
  292. static int __init pcf857x_init(void)
  293. {
  294. return i2c_add_driver(&pcf857x_driver);
  295. }
  296. /* register after i2c postcore initcall and before
  297. * subsys initcalls that may rely on these GPIOs
  298. */
  299. subsys_initcall(pcf857x_init);
  300. static void __exit pcf857x_exit(void)
  301. {
  302. i2c_del_driver(&pcf857x_driver);
  303. }
  304. module_exit(pcf857x_exit);
  305. MODULE_LICENSE("GPL");
  306. MODULE_AUTHOR("David Brownell");