pcf857x.c 9.5 KB

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