gpio-pcf857x.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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/gpio.h>
  21. #include <linux/i2c.h>
  22. #include <linux/i2c/pcf857x.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/irq.h>
  25. #include <linux/irqdomain.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/slab.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/workqueue.h>
  31. static const struct i2c_device_id pcf857x_id[] = {
  32. { "pcf8574", 8 },
  33. { "pcf8574a", 8 },
  34. { "pca8574", 8 },
  35. { "pca9670", 8 },
  36. { "pca9672", 8 },
  37. { "pca9674", 8 },
  38. { "pcf8575", 16 },
  39. { "pca8575", 16 },
  40. { "pca9671", 16 },
  41. { "pca9673", 16 },
  42. { "pca9675", 16 },
  43. { "max7328", 8 },
  44. { "max7329", 8 },
  45. { "tca9554", 8 },
  46. { }
  47. };
  48. MODULE_DEVICE_TABLE(i2c, pcf857x_id);
  49. /*
  50. * The pcf857x, pca857x, and pca967x chips only expose one read and one
  51. * write register. Writing a "one" bit (to match the reset state) lets
  52. * that pin be used as an input; it's not an open-drain model, but acts
  53. * a bit like one. This is described as "quasi-bidirectional"; read the
  54. * chip documentation for details.
  55. *
  56. * Many other I2C GPIO expander chips (like the pca953x models) have
  57. * more complex register models and more conventional circuitry using
  58. * push/pull drivers. They often use the same 0x20..0x27 addresses as
  59. * pcf857x parts, making the "legacy" I2C driver model problematic.
  60. */
  61. struct pcf857x {
  62. struct gpio_chip chip;
  63. struct i2c_client *client;
  64. struct mutex lock; /* protect 'out' */
  65. struct work_struct work; /* irq demux work */
  66. struct irq_domain *irq_domain; /* for irq demux */
  67. spinlock_t slock; /* protect irq demux */
  68. unsigned out; /* software latch */
  69. unsigned status; /* current status */
  70. int irq; /* real irq number */
  71. int (*write)(struct i2c_client *client, unsigned data);
  72. int (*read)(struct i2c_client *client);
  73. };
  74. /*-------------------------------------------------------------------------*/
  75. /* Talk to 8-bit I/O expander */
  76. static int i2c_write_le8(struct i2c_client *client, unsigned data)
  77. {
  78. return i2c_smbus_write_byte(client, data);
  79. }
  80. static int i2c_read_le8(struct i2c_client *client)
  81. {
  82. return (int)i2c_smbus_read_byte(client);
  83. }
  84. /* Talk to 16-bit I/O expander */
  85. static int i2c_write_le16(struct i2c_client *client, unsigned word)
  86. {
  87. u8 buf[2] = { word & 0xff, word >> 8, };
  88. int status;
  89. status = i2c_master_send(client, buf, 2);
  90. return (status < 0) ? status : 0;
  91. }
  92. static int i2c_read_le16(struct i2c_client *client)
  93. {
  94. u8 buf[2];
  95. int status;
  96. status = i2c_master_recv(client, buf, 2);
  97. if (status < 0)
  98. return status;
  99. return (buf[1] << 8) | buf[0];
  100. }
  101. /*-------------------------------------------------------------------------*/
  102. static int pcf857x_input(struct gpio_chip *chip, unsigned offset)
  103. {
  104. struct pcf857x *gpio = container_of(chip, struct pcf857x, chip);
  105. int status;
  106. mutex_lock(&gpio->lock);
  107. gpio->out |= (1 << offset);
  108. status = gpio->write(gpio->client, gpio->out);
  109. mutex_unlock(&gpio->lock);
  110. return status;
  111. }
  112. static int pcf857x_get(struct gpio_chip *chip, unsigned offset)
  113. {
  114. struct pcf857x *gpio = container_of(chip, struct pcf857x, chip);
  115. int value;
  116. value = gpio->read(gpio->client);
  117. return (value < 0) ? 0 : (value & (1 << offset));
  118. }
  119. static int pcf857x_output(struct gpio_chip *chip, unsigned offset, int value)
  120. {
  121. struct pcf857x *gpio = container_of(chip, struct pcf857x, chip);
  122. unsigned bit = 1 << offset;
  123. int status;
  124. mutex_lock(&gpio->lock);
  125. if (value)
  126. gpio->out |= bit;
  127. else
  128. gpio->out &= ~bit;
  129. status = gpio->write(gpio->client, gpio->out);
  130. mutex_unlock(&gpio->lock);
  131. return status;
  132. }
  133. static void pcf857x_set(struct gpio_chip *chip, unsigned offset, int value)
  134. {
  135. pcf857x_output(chip, offset, value);
  136. }
  137. /*-------------------------------------------------------------------------*/
  138. static int pcf857x_to_irq(struct gpio_chip *chip, unsigned offset)
  139. {
  140. struct pcf857x *gpio = container_of(chip, struct pcf857x, chip);
  141. return irq_create_mapping(gpio->irq_domain, offset);
  142. }
  143. static void pcf857x_irq_demux_work(struct work_struct *work)
  144. {
  145. struct pcf857x *gpio = container_of(work,
  146. struct pcf857x,
  147. work);
  148. unsigned long change, i, status, flags;
  149. status = gpio->read(gpio->client);
  150. spin_lock_irqsave(&gpio->slock, flags);
  151. change = gpio->status ^ status;
  152. for_each_set_bit(i, &change, gpio->chip.ngpio)
  153. generic_handle_irq(irq_find_mapping(gpio->irq_domain, i));
  154. gpio->status = status;
  155. spin_unlock_irqrestore(&gpio->slock, flags);
  156. }
  157. static irqreturn_t pcf857x_irq_demux(int irq, void *data)
  158. {
  159. struct pcf857x *gpio = data;
  160. /*
  161. * pcf857x can't read/write data here,
  162. * since i2c data access might go to sleep.
  163. */
  164. schedule_work(&gpio->work);
  165. return IRQ_HANDLED;
  166. }
  167. static int pcf857x_irq_domain_map(struct irq_domain *domain, unsigned int virq,
  168. irq_hw_number_t hw)
  169. {
  170. irq_set_chip_and_handler(virq,
  171. &dummy_irq_chip,
  172. handle_level_irq);
  173. return 0;
  174. }
  175. static struct irq_domain_ops pcf857x_irq_domain_ops = {
  176. .map = pcf857x_irq_domain_map,
  177. };
  178. static void pcf857x_irq_domain_cleanup(struct pcf857x *gpio)
  179. {
  180. if (gpio->irq_domain)
  181. irq_domain_remove(gpio->irq_domain);
  182. if (gpio->irq)
  183. free_irq(gpio->irq, gpio);
  184. }
  185. static int pcf857x_irq_domain_init(struct pcf857x *gpio,
  186. struct i2c_client *client)
  187. {
  188. int status;
  189. gpio->irq_domain = irq_domain_add_linear(client->dev.of_node,
  190. gpio->chip.ngpio,
  191. &pcf857x_irq_domain_ops,
  192. NULL);
  193. if (!gpio->irq_domain)
  194. goto fail;
  195. /* enable real irq */
  196. status = request_irq(client->irq, pcf857x_irq_demux, 0,
  197. dev_name(&client->dev), gpio);
  198. if (status)
  199. goto fail;
  200. /* enable gpio_to_irq() */
  201. INIT_WORK(&gpio->work, pcf857x_irq_demux_work);
  202. gpio->chip.to_irq = pcf857x_to_irq;
  203. gpio->irq = client->irq;
  204. return 0;
  205. fail:
  206. pcf857x_irq_domain_cleanup(gpio);
  207. return -EINVAL;
  208. }
  209. /*-------------------------------------------------------------------------*/
  210. static int pcf857x_probe(struct i2c_client *client,
  211. const struct i2c_device_id *id)
  212. {
  213. struct pcf857x_platform_data *pdata;
  214. struct pcf857x *gpio;
  215. int status;
  216. pdata = dev_get_platdata(&client->dev);
  217. if (!pdata) {
  218. dev_dbg(&client->dev, "no platform data\n");
  219. }
  220. /* Allocate, initialize, and register this gpio_chip. */
  221. gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL);
  222. if (!gpio)
  223. return -ENOMEM;
  224. mutex_init(&gpio->lock);
  225. spin_lock_init(&gpio->slock);
  226. gpio->chip.base = pdata ? pdata->gpio_base : -1;
  227. gpio->chip.can_sleep = 1;
  228. gpio->chip.dev = &client->dev;
  229. gpio->chip.owner = THIS_MODULE;
  230. gpio->chip.get = pcf857x_get;
  231. gpio->chip.set = pcf857x_set;
  232. gpio->chip.direction_input = pcf857x_input;
  233. gpio->chip.direction_output = pcf857x_output;
  234. gpio->chip.ngpio = id->driver_data;
  235. /* enable gpio_to_irq() if platform has settings */
  236. if (client->irq) {
  237. status = pcf857x_irq_domain_init(gpio, client);
  238. if (status < 0) {
  239. dev_err(&client->dev, "irq_domain init failed\n");
  240. goto fail;
  241. }
  242. }
  243. /* NOTE: the OnSemi jlc1562b is also largely compatible with
  244. * these parts, notably for output. It has a low-resolution
  245. * DAC instead of pin change IRQs; and its inputs can be the
  246. * result of comparators.
  247. */
  248. /* 8574 addresses are 0x20..0x27; 8574a uses 0x38..0x3f;
  249. * 9670, 9672, 9764, and 9764a use quite a variety.
  250. *
  251. * NOTE: we don't distinguish here between *4 and *4a parts.
  252. */
  253. if (gpio->chip.ngpio == 8) {
  254. gpio->write = i2c_write_le8;
  255. gpio->read = i2c_read_le8;
  256. if (!i2c_check_functionality(client->adapter,
  257. I2C_FUNC_SMBUS_BYTE))
  258. status = -EIO;
  259. /* fail if there's no chip present */
  260. else
  261. status = i2c_smbus_read_byte(client);
  262. /* '75/'75c addresses are 0x20..0x27, just like the '74;
  263. * the '75c doesn't have a current source pulling high.
  264. * 9671, 9673, and 9765 use quite a variety of addresses.
  265. *
  266. * NOTE: we don't distinguish here between '75 and '75c parts.
  267. */
  268. } else if (gpio->chip.ngpio == 16) {
  269. gpio->write = i2c_write_le16;
  270. gpio->read = i2c_read_le16;
  271. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  272. status = -EIO;
  273. /* fail if there's no chip present */
  274. else
  275. status = i2c_read_le16(client);
  276. } else {
  277. dev_dbg(&client->dev, "unsupported number of gpios\n");
  278. status = -EINVAL;
  279. }
  280. if (status < 0)
  281. goto fail;
  282. gpio->chip.label = client->name;
  283. gpio->client = client;
  284. i2c_set_clientdata(client, gpio);
  285. /* NOTE: these chips have strange "quasi-bidirectional" I/O pins.
  286. * We can't actually know whether a pin is configured (a) as output
  287. * and driving the signal low, or (b) as input and reporting a low
  288. * value ... without knowing the last value written since the chip
  289. * came out of reset (if any). We can't read the latched output.
  290. *
  291. * In short, the only reliable solution for setting up pin direction
  292. * is to do it explicitly. The setup() method can do that, but it
  293. * may cause transient glitching since it can't know the last value
  294. * written (some pins may need to be driven low).
  295. *
  296. * Using pdata->n_latch avoids that trouble. When left initialized
  297. * to zero, our software copy of the "latch" then matches the chip's
  298. * all-ones reset state. Otherwise it flags pins to be driven low.
  299. */
  300. gpio->out = pdata ? ~pdata->n_latch : ~0;
  301. gpio->status = gpio->out;
  302. status = gpiochip_add(&gpio->chip);
  303. if (status < 0)
  304. goto fail;
  305. /* Let platform code set up the GPIOs and their users.
  306. * Now is the first time anyone could use them.
  307. */
  308. if (pdata && pdata->setup) {
  309. status = pdata->setup(client,
  310. gpio->chip.base, gpio->chip.ngpio,
  311. pdata->context);
  312. if (status < 0)
  313. dev_warn(&client->dev, "setup --> %d\n", status);
  314. }
  315. dev_info(&client->dev, "probed\n");
  316. return 0;
  317. fail:
  318. dev_dbg(&client->dev, "probe error %d for '%s'\n",
  319. status, client->name);
  320. if (client->irq)
  321. pcf857x_irq_domain_cleanup(gpio);
  322. return status;
  323. }
  324. static int pcf857x_remove(struct i2c_client *client)
  325. {
  326. struct pcf857x_platform_data *pdata = dev_get_platdata(&client->dev);
  327. struct pcf857x *gpio = i2c_get_clientdata(client);
  328. int status = 0;
  329. if (pdata && pdata->teardown) {
  330. status = pdata->teardown(client,
  331. gpio->chip.base, gpio->chip.ngpio,
  332. pdata->context);
  333. if (status < 0) {
  334. dev_err(&client->dev, "%s --> %d\n",
  335. "teardown", status);
  336. return status;
  337. }
  338. }
  339. if (client->irq)
  340. pcf857x_irq_domain_cleanup(gpio);
  341. status = gpiochip_remove(&gpio->chip);
  342. if (status)
  343. dev_err(&client->dev, "%s --> %d\n", "remove", status);
  344. return status;
  345. }
  346. static struct i2c_driver pcf857x_driver = {
  347. .driver = {
  348. .name = "pcf857x",
  349. .owner = THIS_MODULE,
  350. },
  351. .probe = pcf857x_probe,
  352. .remove = pcf857x_remove,
  353. .id_table = pcf857x_id,
  354. };
  355. static int __init pcf857x_init(void)
  356. {
  357. return i2c_add_driver(&pcf857x_driver);
  358. }
  359. /* register after i2c postcore initcall and before
  360. * subsys initcalls that may rely on these GPIOs
  361. */
  362. subsys_initcall(pcf857x_init);
  363. static void __exit pcf857x_exit(void)
  364. {
  365. i2c_del_driver(&pcf857x_driver);
  366. }
  367. module_exit(pcf857x_exit);
  368. MODULE_LICENSE("GPL");
  369. MODULE_AUTHOR("David Brownell");