gpio-pcf857x.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for pcf857x, pca857x, and pca967x I2C GPIO expanders
  4. *
  5. * Copyright (C) 2007 David Brownell
  6. */
  7. #include <linux/gpio/driver.h>
  8. #include <linux/i2c.h>
  9. #include <linux/platform_data/pcf857x.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/irq.h>
  12. #include <linux/irqdomain.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/slab.h>
  18. #include <linux/spinlock.h>
  19. static const struct i2c_device_id pcf857x_id[] = {
  20. { "pcf8574", 8 },
  21. { "pcf8574a", 8 },
  22. { "pca8574", 8 },
  23. { "pca9670", 8 },
  24. { "pca9672", 8 },
  25. { "pca9674", 8 },
  26. { "pcf8575", 16 },
  27. { "pca8575", 16 },
  28. { "pca9671", 16 },
  29. { "pca9673", 16 },
  30. { "pca9675", 16 },
  31. { "max7328", 8 },
  32. { "max7329", 8 },
  33. { }
  34. };
  35. MODULE_DEVICE_TABLE(i2c, pcf857x_id);
  36. #ifdef CONFIG_OF
  37. static const struct of_device_id pcf857x_of_table[] = {
  38. { .compatible = "nxp,pcf8574" },
  39. { .compatible = "nxp,pcf8574a" },
  40. { .compatible = "nxp,pca8574" },
  41. { .compatible = "nxp,pca9670" },
  42. { .compatible = "nxp,pca9672" },
  43. { .compatible = "nxp,pca9674" },
  44. { .compatible = "nxp,pcf8575" },
  45. { .compatible = "nxp,pca8575" },
  46. { .compatible = "nxp,pca9671" },
  47. { .compatible = "nxp,pca9673" },
  48. { .compatible = "nxp,pca9675" },
  49. { .compatible = "maxim,max7328" },
  50. { .compatible = "maxim,max7329" },
  51. { }
  52. };
  53. MODULE_DEVICE_TABLE(of, pcf857x_of_table);
  54. #endif
  55. /*
  56. * The pcf857x, pca857x, and pca967x chips only expose one read and one
  57. * write register. Writing a "one" bit (to match the reset state) lets
  58. * that pin be used as an input; it's not an open-drain model, but acts
  59. * a bit like one. This is described as "quasi-bidirectional"; read the
  60. * chip documentation for details.
  61. *
  62. * Many other I2C GPIO expander chips (like the pca953x models) have
  63. * more complex register models and more conventional circuitry using
  64. * push/pull drivers. They often use the same 0x20..0x27 addresses as
  65. * pcf857x parts, making the "legacy" I2C driver model problematic.
  66. */
  67. struct pcf857x {
  68. struct gpio_chip chip;
  69. struct i2c_client *client;
  70. struct mutex lock; /* protect 'out' */
  71. unsigned out; /* software latch */
  72. unsigned status; /* current status */
  73. unsigned irq_enabled; /* enabled irqs */
  74. int (*write)(struct i2c_client *client, unsigned data);
  75. int (*read)(struct i2c_client *client);
  76. };
  77. /*-------------------------------------------------------------------------*/
  78. /* Talk to 8-bit I/O expander */
  79. static int i2c_write_le8(struct i2c_client *client, unsigned data)
  80. {
  81. return i2c_smbus_write_byte(client, data);
  82. }
  83. static int i2c_read_le8(struct i2c_client *client)
  84. {
  85. return (int)i2c_smbus_read_byte(client);
  86. }
  87. /* Talk to 16-bit I/O expander */
  88. static int i2c_write_le16(struct i2c_client *client, unsigned word)
  89. {
  90. u8 buf[2] = { word & 0xff, word >> 8, };
  91. int status;
  92. status = i2c_master_send(client, buf, 2);
  93. return (status < 0) ? status : 0;
  94. }
  95. static int i2c_read_le16(struct i2c_client *client)
  96. {
  97. u8 buf[2];
  98. int status;
  99. status = i2c_master_recv(client, buf, 2);
  100. if (status < 0)
  101. return status;
  102. return (buf[1] << 8) | buf[0];
  103. }
  104. /*-------------------------------------------------------------------------*/
  105. static int pcf857x_input(struct gpio_chip *chip, unsigned offset)
  106. {
  107. struct pcf857x *gpio = gpiochip_get_data(chip);
  108. int status;
  109. mutex_lock(&gpio->lock);
  110. gpio->out |= (1 << offset);
  111. status = gpio->write(gpio->client, gpio->out);
  112. mutex_unlock(&gpio->lock);
  113. return status;
  114. }
  115. static int pcf857x_get(struct gpio_chip *chip, unsigned offset)
  116. {
  117. struct pcf857x *gpio = gpiochip_get_data(chip);
  118. int value;
  119. value = gpio->read(gpio->client);
  120. return (value < 0) ? value : !!(value & (1 << offset));
  121. }
  122. static int pcf857x_output(struct gpio_chip *chip, unsigned offset, int value)
  123. {
  124. struct pcf857x *gpio = gpiochip_get_data(chip);
  125. unsigned bit = 1 << offset;
  126. int status;
  127. mutex_lock(&gpio->lock);
  128. if (value)
  129. gpio->out |= bit;
  130. else
  131. gpio->out &= ~bit;
  132. status = gpio->write(gpio->client, gpio->out);
  133. mutex_unlock(&gpio->lock);
  134. return status;
  135. }
  136. static void pcf857x_set(struct gpio_chip *chip, unsigned offset, int value)
  137. {
  138. pcf857x_output(chip, offset, value);
  139. }
  140. /*-------------------------------------------------------------------------*/
  141. static irqreturn_t pcf857x_irq(int irq, void *data)
  142. {
  143. struct pcf857x *gpio = data;
  144. unsigned long change, i, status;
  145. status = gpio->read(gpio->client);
  146. /*
  147. * call the interrupt handler iff gpio is used as
  148. * interrupt source, just to avoid bad irqs
  149. */
  150. mutex_lock(&gpio->lock);
  151. change = (gpio->status ^ status) & gpio->irq_enabled;
  152. gpio->status = status;
  153. mutex_unlock(&gpio->lock);
  154. for_each_set_bit(i, &change, gpio->chip.ngpio)
  155. handle_nested_irq(irq_find_mapping(gpio->chip.irq.domain, i));
  156. return IRQ_HANDLED;
  157. }
  158. /*
  159. * NOP functions
  160. */
  161. static void noop(struct irq_data *data) { }
  162. static int pcf857x_irq_set_wake(struct irq_data *data, unsigned int on)
  163. {
  164. struct pcf857x *gpio = irq_data_get_irq_chip_data(data);
  165. return irq_set_irq_wake(gpio->client->irq, on);
  166. }
  167. static void pcf857x_irq_enable(struct irq_data *data)
  168. {
  169. struct pcf857x *gpio = irq_data_get_irq_chip_data(data);
  170. irq_hw_number_t hwirq = irqd_to_hwirq(data);
  171. gpiochip_enable_irq(&gpio->chip, hwirq);
  172. gpio->irq_enabled |= (1 << hwirq);
  173. }
  174. static void pcf857x_irq_disable(struct irq_data *data)
  175. {
  176. struct pcf857x *gpio = irq_data_get_irq_chip_data(data);
  177. irq_hw_number_t hwirq = irqd_to_hwirq(data);
  178. gpio->irq_enabled &= ~(1 << hwirq);
  179. gpiochip_disable_irq(&gpio->chip, hwirq);
  180. }
  181. static void pcf857x_irq_bus_lock(struct irq_data *data)
  182. {
  183. struct pcf857x *gpio = irq_data_get_irq_chip_data(data);
  184. mutex_lock(&gpio->lock);
  185. }
  186. static void pcf857x_irq_bus_sync_unlock(struct irq_data *data)
  187. {
  188. struct pcf857x *gpio = irq_data_get_irq_chip_data(data);
  189. mutex_unlock(&gpio->lock);
  190. }
  191. static const struct irq_chip pcf857x_irq_chip = {
  192. .name = "pcf857x",
  193. .irq_enable = pcf857x_irq_enable,
  194. .irq_disable = pcf857x_irq_disable,
  195. .irq_ack = noop,
  196. .irq_mask = noop,
  197. .irq_unmask = noop,
  198. .irq_set_wake = pcf857x_irq_set_wake,
  199. .irq_bus_lock = pcf857x_irq_bus_lock,
  200. .irq_bus_sync_unlock = pcf857x_irq_bus_sync_unlock,
  201. .flags = IRQCHIP_IMMUTABLE,
  202. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  203. };
  204. /*-------------------------------------------------------------------------*/
  205. static int pcf857x_probe(struct i2c_client *client,
  206. const struct i2c_device_id *id)
  207. {
  208. struct pcf857x_platform_data *pdata = dev_get_platdata(&client->dev);
  209. struct device_node *np = client->dev.of_node;
  210. struct pcf857x *gpio;
  211. unsigned int n_latch = 0;
  212. int status;
  213. if (IS_ENABLED(CONFIG_OF) && np)
  214. of_property_read_u32(np, "lines-initial-states", &n_latch);
  215. else if (pdata)
  216. n_latch = pdata->n_latch;
  217. else
  218. dev_dbg(&client->dev, "no platform data\n");
  219. /* Allocate, initialize, and register this gpio_chip. */
  220. gpio = devm_kzalloc(&client->dev, sizeof(*gpio), GFP_KERNEL);
  221. if (!gpio)
  222. return -ENOMEM;
  223. mutex_init(&gpio->lock);
  224. gpio->chip.base = pdata ? pdata->gpio_base : -1;
  225. gpio->chip.can_sleep = true;
  226. gpio->chip.parent = &client->dev;
  227. gpio->chip.owner = THIS_MODULE;
  228. gpio->chip.get = pcf857x_get;
  229. gpio->chip.set = pcf857x_set;
  230. gpio->chip.direction_input = pcf857x_input;
  231. gpio->chip.direction_output = pcf857x_output;
  232. gpio->chip.ngpio = id->driver_data;
  233. /* NOTE: the OnSemi jlc1562b is also largely compatible with
  234. * these parts, notably for output. It has a low-resolution
  235. * DAC instead of pin change IRQs; and its inputs can be the
  236. * result of comparators.
  237. */
  238. /* 8574 addresses are 0x20..0x27; 8574a uses 0x38..0x3f;
  239. * 9670, 9672, 9764, and 9764a use quite a variety.
  240. *
  241. * NOTE: we don't distinguish here between *4 and *4a parts.
  242. */
  243. if (gpio->chip.ngpio == 8) {
  244. gpio->write = i2c_write_le8;
  245. gpio->read = i2c_read_le8;
  246. if (!i2c_check_functionality(client->adapter,
  247. I2C_FUNC_SMBUS_BYTE))
  248. status = -EIO;
  249. /* fail if there's no chip present */
  250. else
  251. status = i2c_smbus_read_byte(client);
  252. /* '75/'75c addresses are 0x20..0x27, just like the '74;
  253. * the '75c doesn't have a current source pulling high.
  254. * 9671, 9673, and 9765 use quite a variety of addresses.
  255. *
  256. * NOTE: we don't distinguish here between '75 and '75c parts.
  257. */
  258. } else if (gpio->chip.ngpio == 16) {
  259. gpio->write = i2c_write_le16;
  260. gpio->read = i2c_read_le16;
  261. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  262. status = -EIO;
  263. /* fail if there's no chip present */
  264. else
  265. status = i2c_read_le16(client);
  266. } else {
  267. dev_dbg(&client->dev, "unsupported number of gpios\n");
  268. status = -EINVAL;
  269. }
  270. if (status < 0)
  271. goto fail;
  272. gpio->chip.label = client->name;
  273. gpio->client = client;
  274. i2c_set_clientdata(client, gpio);
  275. /* NOTE: these chips have strange "quasi-bidirectional" I/O pins.
  276. * We can't actually know whether a pin is configured (a) as output
  277. * and driving the signal low, or (b) as input and reporting a low
  278. * value ... without knowing the last value written since the chip
  279. * came out of reset (if any). We can't read the latched output.
  280. *
  281. * In short, the only reliable solution for setting up pin direction
  282. * is to do it explicitly. The setup() method can do that, but it
  283. * may cause transient glitching since it can't know the last value
  284. * written (some pins may need to be driven low).
  285. *
  286. * Using n_latch avoids that trouble. When left initialized to zero,
  287. * our software copy of the "latch" then matches the chip's all-ones
  288. * reset state. Otherwise it flags pins to be driven low.
  289. */
  290. gpio->out = ~n_latch;
  291. gpio->status = gpio->read(gpio->client);
  292. /* Enable irqchip if we have an interrupt */
  293. if (client->irq) {
  294. struct gpio_irq_chip *girq;
  295. status = devm_request_threaded_irq(&client->dev, client->irq,
  296. NULL, pcf857x_irq, IRQF_ONESHOT |
  297. IRQF_TRIGGER_FALLING | IRQF_SHARED,
  298. dev_name(&client->dev), gpio);
  299. if (status)
  300. goto fail;
  301. girq = &gpio->chip.irq;
  302. gpio_irq_chip_set_chip(girq, &pcf857x_irq_chip);
  303. /* This will let us handle the parent IRQ in the driver */
  304. girq->parent_handler = NULL;
  305. girq->num_parents = 0;
  306. girq->parents = NULL;
  307. girq->default_type = IRQ_TYPE_NONE;
  308. girq->handler = handle_level_irq;
  309. girq->threaded = true;
  310. }
  311. status = devm_gpiochip_add_data(&client->dev, &gpio->chip, gpio);
  312. if (status < 0)
  313. goto fail;
  314. /* Let platform code set up the GPIOs and their users.
  315. * Now is the first time anyone could use them.
  316. */
  317. if (pdata && pdata->setup) {
  318. status = pdata->setup(client,
  319. gpio->chip.base, gpio->chip.ngpio,
  320. pdata->context);
  321. if (status < 0)
  322. dev_warn(&client->dev, "setup --> %d\n", status);
  323. }
  324. dev_info(&client->dev, "probed\n");
  325. return 0;
  326. fail:
  327. dev_dbg(&client->dev, "probe error %d for '%s'\n", status,
  328. client->name);
  329. return status;
  330. }
  331. static void pcf857x_remove(struct i2c_client *client)
  332. {
  333. struct pcf857x_platform_data *pdata = dev_get_platdata(&client->dev);
  334. struct pcf857x *gpio = i2c_get_clientdata(client);
  335. if (pdata && pdata->teardown)
  336. pdata->teardown(client, gpio->chip.base, gpio->chip.ngpio,
  337. pdata->context);
  338. }
  339. static void pcf857x_shutdown(struct i2c_client *client)
  340. {
  341. struct pcf857x *gpio = i2c_get_clientdata(client);
  342. /* Drive all the I/O lines high */
  343. gpio->write(gpio->client, BIT(gpio->chip.ngpio) - 1);
  344. }
  345. static struct i2c_driver pcf857x_driver = {
  346. .driver = {
  347. .name = "pcf857x",
  348. .of_match_table = of_match_ptr(pcf857x_of_table),
  349. },
  350. .probe = pcf857x_probe,
  351. .remove = pcf857x_remove,
  352. .shutdown = pcf857x_shutdown,
  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");