gpio-pcf857x.c 9.8 KB

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