pcf857x.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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/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. return -EINVAL;
  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->gpio_base;
  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->n_latch;
  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, "gpios %d..%d on a %s%s\n",
  243. gpio->chip.base,
  244. gpio->chip.base + gpio->chip.ngpio - 1,
  245. client->name,
  246. client->irq ? " (irq ignored)" : "");
  247. /* Let platform code set up the GPIOs and their users.
  248. * Now is the first time anyone could use them.
  249. */
  250. if (pdata->setup) {
  251. status = pdata->setup(client,
  252. gpio->chip.base, gpio->chip.ngpio,
  253. pdata->context);
  254. if (status < 0)
  255. dev_warn(&client->dev, "setup --> %d\n", status);
  256. }
  257. return 0;
  258. fail:
  259. dev_dbg(&client->dev, "probe error %d for '%s'\n",
  260. status, client->name);
  261. kfree(gpio);
  262. return status;
  263. }
  264. static int pcf857x_remove(struct i2c_client *client)
  265. {
  266. struct pcf857x_platform_data *pdata = client->dev.platform_data;
  267. struct pcf857x *gpio = i2c_get_clientdata(client);
  268. int status = 0;
  269. if (pdata->teardown) {
  270. status = pdata->teardown(client,
  271. gpio->chip.base, gpio->chip.ngpio,
  272. pdata->context);
  273. if (status < 0) {
  274. dev_err(&client->dev, "%s --> %d\n",
  275. "teardown", status);
  276. return status;
  277. }
  278. }
  279. status = gpiochip_remove(&gpio->chip);
  280. if (status == 0)
  281. kfree(gpio);
  282. else
  283. dev_err(&client->dev, "%s --> %d\n", "remove", status);
  284. return status;
  285. }
  286. static struct i2c_driver pcf857x_driver = {
  287. .driver = {
  288. .name = "pcf857x",
  289. .owner = THIS_MODULE,
  290. },
  291. .probe = pcf857x_probe,
  292. .remove = pcf857x_remove,
  293. .id_table = pcf857x_id,
  294. };
  295. static int __init pcf857x_init(void)
  296. {
  297. return i2c_add_driver(&pcf857x_driver);
  298. }
  299. /* register after i2c postcore initcall and before
  300. * subsys initcalls that may rely on these GPIOs
  301. */
  302. subsys_initcall(pcf857x_init);
  303. static void __exit pcf857x_exit(void)
  304. {
  305. i2c_del_driver(&pcf857x_driver);
  306. }
  307. module_exit(pcf857x_exit);
  308. MODULE_LICENSE("GPL");
  309. MODULE_AUTHOR("David Brownell");