pinctrl-mcp23s08.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* MCP23S08 SPI/I2C GPIO driver */
  3. #include <linux/bitops.h>
  4. #include <linux/kernel.h>
  5. #include <linux/device.h>
  6. #include <linux/mutex.h>
  7. #include <linux/mod_devicetable.h>
  8. #include <linux/module.h>
  9. #include <linux/export.h>
  10. #include <linux/gpio/driver.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/slab.h>
  14. #include <asm/byteorder.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/regmap.h>
  17. #include <linux/pinctrl/pinctrl.h>
  18. #include <linux/pinctrl/pinconf.h>
  19. #include <linux/pinctrl/pinconf-generic.h>
  20. #include "pinctrl-mcp23s08.h"
  21. /* Registers are all 8 bits wide.
  22. *
  23. * The mcp23s17 has twice as many bits, and can be configured to work
  24. * with either 16 bit registers or with two adjacent 8 bit banks.
  25. */
  26. #define MCP_IODIR 0x00 /* init/reset: all ones */
  27. #define MCP_IPOL 0x01
  28. #define MCP_GPINTEN 0x02
  29. #define MCP_DEFVAL 0x03
  30. #define MCP_INTCON 0x04
  31. #define MCP_IOCON 0x05
  32. # define IOCON_MIRROR (1 << 6)
  33. # define IOCON_SEQOP (1 << 5)
  34. # define IOCON_HAEN (1 << 3)
  35. # define IOCON_ODR (1 << 2)
  36. # define IOCON_INTPOL (1 << 1)
  37. # define IOCON_INTCC (1)
  38. #define MCP_GPPU 0x06
  39. #define MCP_INTF 0x07
  40. #define MCP_INTCAP 0x08
  41. #define MCP_GPIO 0x09
  42. #define MCP_OLAT 0x0a
  43. static const struct reg_default mcp23x08_defaults[] = {
  44. {.reg = MCP_IODIR, .def = 0xff},
  45. {.reg = MCP_IPOL, .def = 0x00},
  46. {.reg = MCP_GPINTEN, .def = 0x00},
  47. {.reg = MCP_DEFVAL, .def = 0x00},
  48. {.reg = MCP_INTCON, .def = 0x00},
  49. {.reg = MCP_IOCON, .def = 0x00},
  50. {.reg = MCP_GPPU, .def = 0x00},
  51. {.reg = MCP_OLAT, .def = 0x00},
  52. };
  53. static const struct regmap_range mcp23x08_volatile_range = {
  54. .range_min = MCP_INTF,
  55. .range_max = MCP_GPIO,
  56. };
  57. static const struct regmap_access_table mcp23x08_volatile_table = {
  58. .yes_ranges = &mcp23x08_volatile_range,
  59. .n_yes_ranges = 1,
  60. };
  61. static const struct regmap_range mcp23x08_precious_range = {
  62. .range_min = MCP_GPIO,
  63. .range_max = MCP_GPIO,
  64. };
  65. static const struct regmap_access_table mcp23x08_precious_table = {
  66. .yes_ranges = &mcp23x08_precious_range,
  67. .n_yes_ranges = 1,
  68. };
  69. const struct regmap_config mcp23x08_regmap = {
  70. .reg_bits = 8,
  71. .val_bits = 8,
  72. .reg_stride = 1,
  73. .volatile_table = &mcp23x08_volatile_table,
  74. .precious_table = &mcp23x08_precious_table,
  75. .reg_defaults = mcp23x08_defaults,
  76. .num_reg_defaults = ARRAY_SIZE(mcp23x08_defaults),
  77. .cache_type = REGCACHE_FLAT,
  78. .max_register = MCP_OLAT,
  79. .disable_locking = true, /* mcp->lock protects the regmap */
  80. };
  81. EXPORT_SYMBOL_GPL(mcp23x08_regmap);
  82. static const struct reg_default mcp23x17_defaults[] = {
  83. {.reg = MCP_IODIR << 1, .def = 0xffff},
  84. {.reg = MCP_IPOL << 1, .def = 0x0000},
  85. {.reg = MCP_GPINTEN << 1, .def = 0x0000},
  86. {.reg = MCP_DEFVAL << 1, .def = 0x0000},
  87. {.reg = MCP_INTCON << 1, .def = 0x0000},
  88. {.reg = MCP_IOCON << 1, .def = 0x0000},
  89. {.reg = MCP_GPPU << 1, .def = 0x0000},
  90. {.reg = MCP_OLAT << 1, .def = 0x0000},
  91. };
  92. static const struct regmap_range mcp23x17_volatile_range = {
  93. .range_min = MCP_INTF << 1,
  94. .range_max = MCP_GPIO << 1,
  95. };
  96. static const struct regmap_access_table mcp23x17_volatile_table = {
  97. .yes_ranges = &mcp23x17_volatile_range,
  98. .n_yes_ranges = 1,
  99. };
  100. static const struct regmap_range mcp23x17_precious_range = {
  101. .range_min = MCP_INTCAP << 1,
  102. .range_max = MCP_GPIO << 1,
  103. };
  104. static const struct regmap_access_table mcp23x17_precious_table = {
  105. .yes_ranges = &mcp23x17_precious_range,
  106. .n_yes_ranges = 1,
  107. };
  108. const struct regmap_config mcp23x17_regmap = {
  109. .reg_bits = 8,
  110. .val_bits = 16,
  111. .reg_stride = 2,
  112. .max_register = MCP_OLAT << 1,
  113. .volatile_table = &mcp23x17_volatile_table,
  114. .precious_table = &mcp23x17_precious_table,
  115. .reg_defaults = mcp23x17_defaults,
  116. .num_reg_defaults = ARRAY_SIZE(mcp23x17_defaults),
  117. .cache_type = REGCACHE_FLAT,
  118. .val_format_endian = REGMAP_ENDIAN_LITTLE,
  119. .disable_locking = true, /* mcp->lock protects the regmap */
  120. };
  121. EXPORT_SYMBOL_GPL(mcp23x17_regmap);
  122. static int mcp_read(struct mcp23s08 *mcp, unsigned int reg, unsigned int *val)
  123. {
  124. return regmap_read(mcp->regmap, reg << mcp->reg_shift, val);
  125. }
  126. static int mcp_write(struct mcp23s08 *mcp, unsigned int reg, unsigned int val)
  127. {
  128. return regmap_write(mcp->regmap, reg << mcp->reg_shift, val);
  129. }
  130. static int mcp_update_bits(struct mcp23s08 *mcp, unsigned int reg,
  131. unsigned int mask, unsigned int val)
  132. {
  133. return regmap_update_bits(mcp->regmap, reg << mcp->reg_shift,
  134. mask, val);
  135. }
  136. static int mcp_set_bit(struct mcp23s08 *mcp, unsigned int reg,
  137. unsigned int pin, bool enabled)
  138. {
  139. u16 mask = BIT(pin);
  140. return mcp_update_bits(mcp, reg, mask, enabled ? mask : 0);
  141. }
  142. static const struct pinctrl_pin_desc mcp23x08_pins[] = {
  143. PINCTRL_PIN(0, "gpio0"),
  144. PINCTRL_PIN(1, "gpio1"),
  145. PINCTRL_PIN(2, "gpio2"),
  146. PINCTRL_PIN(3, "gpio3"),
  147. PINCTRL_PIN(4, "gpio4"),
  148. PINCTRL_PIN(5, "gpio5"),
  149. PINCTRL_PIN(6, "gpio6"),
  150. PINCTRL_PIN(7, "gpio7"),
  151. };
  152. static const struct pinctrl_pin_desc mcp23x17_pins[] = {
  153. PINCTRL_PIN(0, "gpio0"),
  154. PINCTRL_PIN(1, "gpio1"),
  155. PINCTRL_PIN(2, "gpio2"),
  156. PINCTRL_PIN(3, "gpio3"),
  157. PINCTRL_PIN(4, "gpio4"),
  158. PINCTRL_PIN(5, "gpio5"),
  159. PINCTRL_PIN(6, "gpio6"),
  160. PINCTRL_PIN(7, "gpio7"),
  161. PINCTRL_PIN(8, "gpio8"),
  162. PINCTRL_PIN(9, "gpio9"),
  163. PINCTRL_PIN(10, "gpio10"),
  164. PINCTRL_PIN(11, "gpio11"),
  165. PINCTRL_PIN(12, "gpio12"),
  166. PINCTRL_PIN(13, "gpio13"),
  167. PINCTRL_PIN(14, "gpio14"),
  168. PINCTRL_PIN(15, "gpio15"),
  169. };
  170. static int mcp_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
  171. {
  172. return 0;
  173. }
  174. static const char *mcp_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
  175. unsigned int group)
  176. {
  177. return NULL;
  178. }
  179. static int mcp_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
  180. unsigned int group,
  181. const unsigned int **pins,
  182. unsigned int *num_pins)
  183. {
  184. return -ENOTSUPP;
  185. }
  186. static const struct pinctrl_ops mcp_pinctrl_ops = {
  187. .get_groups_count = mcp_pinctrl_get_groups_count,
  188. .get_group_name = mcp_pinctrl_get_group_name,
  189. .get_group_pins = mcp_pinctrl_get_group_pins,
  190. #ifdef CONFIG_OF
  191. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  192. .dt_free_map = pinconf_generic_dt_free_map,
  193. #endif
  194. };
  195. static int mcp_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
  196. unsigned long *config)
  197. {
  198. struct mcp23s08 *mcp = pinctrl_dev_get_drvdata(pctldev);
  199. enum pin_config_param param = pinconf_to_config_param(*config);
  200. unsigned int data, status;
  201. int ret;
  202. switch (param) {
  203. case PIN_CONFIG_BIAS_PULL_UP:
  204. mutex_lock(&mcp->lock);
  205. ret = mcp_read(mcp, MCP_GPPU, &data);
  206. mutex_unlock(&mcp->lock);
  207. if (ret < 0)
  208. return ret;
  209. status = (data & BIT(pin)) ? 1 : 0;
  210. break;
  211. default:
  212. return -ENOTSUPP;
  213. }
  214. *config = 0;
  215. return status ? 0 : -EINVAL;
  216. }
  217. static int mcp_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  218. unsigned long *configs, unsigned int num_configs)
  219. {
  220. struct mcp23s08 *mcp = pinctrl_dev_get_drvdata(pctldev);
  221. enum pin_config_param param;
  222. u32 arg;
  223. int ret = 0;
  224. int i;
  225. for (i = 0; i < num_configs; i++) {
  226. param = pinconf_to_config_param(configs[i]);
  227. arg = pinconf_to_config_argument(configs[i]);
  228. switch (param) {
  229. case PIN_CONFIG_BIAS_PULL_UP:
  230. mutex_lock(&mcp->lock);
  231. ret = mcp_set_bit(mcp, MCP_GPPU, pin, arg);
  232. mutex_unlock(&mcp->lock);
  233. break;
  234. default:
  235. dev_dbg(mcp->dev, "Invalid config param %04x\n", param);
  236. return -ENOTSUPP;
  237. }
  238. }
  239. return ret;
  240. }
  241. static const struct pinconf_ops mcp_pinconf_ops = {
  242. .pin_config_get = mcp_pinconf_get,
  243. .pin_config_set = mcp_pinconf_set,
  244. .is_generic = true,
  245. };
  246. /*----------------------------------------------------------------------*/
  247. static int mcp23s08_direction_input(struct gpio_chip *chip, unsigned offset)
  248. {
  249. struct mcp23s08 *mcp = gpiochip_get_data(chip);
  250. int status;
  251. mutex_lock(&mcp->lock);
  252. status = mcp_set_bit(mcp, MCP_IODIR, offset, true);
  253. mutex_unlock(&mcp->lock);
  254. return status;
  255. }
  256. static int mcp23s08_get(struct gpio_chip *chip, unsigned offset)
  257. {
  258. struct mcp23s08 *mcp = gpiochip_get_data(chip);
  259. int status, ret;
  260. mutex_lock(&mcp->lock);
  261. /* REVISIT reading this clears any IRQ ... */
  262. ret = mcp_read(mcp, MCP_GPIO, &status);
  263. if (ret < 0)
  264. status = 0;
  265. else {
  266. mcp->cached_gpio = status;
  267. status = !!(status & (1 << offset));
  268. }
  269. mutex_unlock(&mcp->lock);
  270. return status;
  271. }
  272. static int mcp23s08_get_multiple(struct gpio_chip *chip,
  273. unsigned long *mask, unsigned long *bits)
  274. {
  275. struct mcp23s08 *mcp = gpiochip_get_data(chip);
  276. unsigned int status;
  277. int ret;
  278. mutex_lock(&mcp->lock);
  279. /* REVISIT reading this clears any IRQ ... */
  280. ret = mcp_read(mcp, MCP_GPIO, &status);
  281. if (ret < 0)
  282. status = 0;
  283. else {
  284. mcp->cached_gpio = status;
  285. *bits = status;
  286. }
  287. mutex_unlock(&mcp->lock);
  288. return ret;
  289. }
  290. static int __mcp23s08_set(struct mcp23s08 *mcp, unsigned mask, bool value)
  291. {
  292. return mcp_update_bits(mcp, MCP_OLAT, mask, value ? mask : 0);
  293. }
  294. static void mcp23s08_set(struct gpio_chip *chip, unsigned offset, int value)
  295. {
  296. struct mcp23s08 *mcp = gpiochip_get_data(chip);
  297. unsigned mask = BIT(offset);
  298. mutex_lock(&mcp->lock);
  299. __mcp23s08_set(mcp, mask, !!value);
  300. mutex_unlock(&mcp->lock);
  301. }
  302. static void mcp23s08_set_multiple(struct gpio_chip *chip,
  303. unsigned long *mask, unsigned long *bits)
  304. {
  305. struct mcp23s08 *mcp = gpiochip_get_data(chip);
  306. mutex_lock(&mcp->lock);
  307. mcp_update_bits(mcp, MCP_OLAT, *mask, *bits);
  308. mutex_unlock(&mcp->lock);
  309. }
  310. static int
  311. mcp23s08_direction_output(struct gpio_chip *chip, unsigned offset, int value)
  312. {
  313. struct mcp23s08 *mcp = gpiochip_get_data(chip);
  314. unsigned mask = BIT(offset);
  315. int status;
  316. mutex_lock(&mcp->lock);
  317. status = __mcp23s08_set(mcp, mask, value);
  318. if (status == 0) {
  319. status = mcp_update_bits(mcp, MCP_IODIR, mask, 0);
  320. }
  321. mutex_unlock(&mcp->lock);
  322. return status;
  323. }
  324. /*----------------------------------------------------------------------*/
  325. static irqreturn_t mcp23s08_irq(int irq, void *data)
  326. {
  327. struct mcp23s08 *mcp = data;
  328. int intcap, intcon, intf, i, gpio, gpio_orig, intcap_mask, defval, gpinten;
  329. bool need_unmask = false;
  330. unsigned long int enabled_interrupts;
  331. unsigned int child_irq;
  332. bool intf_set, intcap_changed, gpio_bit_changed,
  333. defval_changed, gpio_set;
  334. mutex_lock(&mcp->lock);
  335. if (mcp_read(mcp, MCP_INTF, &intf))
  336. goto unlock;
  337. if (intf == 0) {
  338. /* There is no interrupt pending */
  339. goto unlock;
  340. }
  341. if (mcp_read(mcp, MCP_INTCON, &intcon))
  342. goto unlock;
  343. if (mcp_read(mcp, MCP_GPINTEN, &gpinten))
  344. goto unlock;
  345. if (mcp_read(mcp, MCP_DEFVAL, &defval))
  346. goto unlock;
  347. /* Mask level interrupts to avoid their immediate reactivation after clearing */
  348. if (intcon) {
  349. need_unmask = true;
  350. if (mcp_write(mcp, MCP_GPINTEN, gpinten & ~intcon))
  351. goto unlock;
  352. }
  353. if (mcp_read(mcp, MCP_INTCAP, &intcap))
  354. goto unlock;
  355. /* This clears the interrupt(configurable on S18) */
  356. if (mcp_read(mcp, MCP_GPIO, &gpio))
  357. goto unlock;
  358. gpio_orig = mcp->cached_gpio;
  359. mcp->cached_gpio = gpio;
  360. mutex_unlock(&mcp->lock);
  361. dev_dbg(mcp->chip.parent,
  362. "intcap 0x%04X intf 0x%04X gpio_orig 0x%04X gpio 0x%04X\n",
  363. intcap, intf, gpio_orig, gpio);
  364. enabled_interrupts = gpinten;
  365. for_each_set_bit(i, &enabled_interrupts, mcp->chip.ngpio) {
  366. /*
  367. * We must check all of the inputs with enabled interrupts
  368. * on the chip, otherwise we may not notice a change
  369. * on more than one pin.
  370. *
  371. * On at least the mcp23s17, INTCAP is only updated
  372. * one byte at a time(INTCAPA and INTCAPB are
  373. * not written to at the same time - only on a per-bank
  374. * basis).
  375. *
  376. * INTF only contains the single bit that caused the
  377. * interrupt per-bank. On the mcp23s17, there is
  378. * INTFA and INTFB. If two pins are changed on the A
  379. * side at the same time, INTF will only have one bit
  380. * set. If one pin on the A side and one pin on the B
  381. * side are changed at the same time, INTF will have
  382. * two bits set. Thus, INTF can't be the only check
  383. * to see if the input has changed.
  384. */
  385. intf_set = intf & BIT(i);
  386. if (i < 8 && intf_set)
  387. intcap_mask = 0x00FF;
  388. else if (i >= 8 && intf_set)
  389. intcap_mask = 0xFF00;
  390. else
  391. intcap_mask = 0x00;
  392. intcap_changed = (intcap_mask &
  393. (intcap & BIT(i))) !=
  394. (intcap_mask & (BIT(i) & gpio_orig));
  395. gpio_set = BIT(i) & gpio;
  396. gpio_bit_changed = (BIT(i) & gpio_orig) !=
  397. (BIT(i) & gpio);
  398. defval_changed = (BIT(i) & intcon) &&
  399. ((BIT(i) & gpio) !=
  400. (BIT(i) & defval));
  401. if (((gpio_bit_changed || intcap_changed) &&
  402. (BIT(i) & mcp->irq_rise) && gpio_set) ||
  403. ((gpio_bit_changed || intcap_changed) &&
  404. (BIT(i) & mcp->irq_fall) && !gpio_set) ||
  405. defval_changed) {
  406. child_irq = irq_find_mapping(mcp->chip.irq.domain, i);
  407. handle_nested_irq(child_irq);
  408. }
  409. }
  410. if (need_unmask) {
  411. mutex_lock(&mcp->lock);
  412. goto unlock;
  413. }
  414. return IRQ_HANDLED;
  415. unlock:
  416. if (need_unmask)
  417. if (mcp_write(mcp, MCP_GPINTEN, gpinten))
  418. dev_err(mcp->chip.parent, "can't unmask GPINTEN\n");
  419. mutex_unlock(&mcp->lock);
  420. return IRQ_HANDLED;
  421. }
  422. static void mcp23s08_irq_mask(struct irq_data *data)
  423. {
  424. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  425. struct mcp23s08 *mcp = gpiochip_get_data(gc);
  426. unsigned int pos = irqd_to_hwirq(data);
  427. mcp_set_bit(mcp, MCP_GPINTEN, pos, false);
  428. gpiochip_disable_irq(gc, pos);
  429. }
  430. static void mcp23s08_irq_unmask(struct irq_data *data)
  431. {
  432. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  433. struct mcp23s08 *mcp = gpiochip_get_data(gc);
  434. unsigned int pos = irqd_to_hwirq(data);
  435. gpiochip_enable_irq(gc, pos);
  436. mcp_set_bit(mcp, MCP_GPINTEN, pos, true);
  437. }
  438. static int mcp23s08_irq_set_type(struct irq_data *data, unsigned int type)
  439. {
  440. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  441. struct mcp23s08 *mcp = gpiochip_get_data(gc);
  442. unsigned int pos = irqd_to_hwirq(data);
  443. if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
  444. mcp_set_bit(mcp, MCP_INTCON, pos, false);
  445. mcp->irq_rise |= BIT(pos);
  446. mcp->irq_fall |= BIT(pos);
  447. } else if (type & IRQ_TYPE_EDGE_RISING) {
  448. mcp_set_bit(mcp, MCP_INTCON, pos, false);
  449. mcp->irq_rise |= BIT(pos);
  450. mcp->irq_fall &= ~BIT(pos);
  451. } else if (type & IRQ_TYPE_EDGE_FALLING) {
  452. mcp_set_bit(mcp, MCP_INTCON, pos, false);
  453. mcp->irq_rise &= ~BIT(pos);
  454. mcp->irq_fall |= BIT(pos);
  455. } else if (type & IRQ_TYPE_LEVEL_HIGH) {
  456. mcp_set_bit(mcp, MCP_INTCON, pos, true);
  457. mcp_set_bit(mcp, MCP_DEFVAL, pos, false);
  458. } else if (type & IRQ_TYPE_LEVEL_LOW) {
  459. mcp_set_bit(mcp, MCP_INTCON, pos, true);
  460. mcp_set_bit(mcp, MCP_DEFVAL, pos, true);
  461. } else
  462. return -EINVAL;
  463. return 0;
  464. }
  465. static void mcp23s08_irq_bus_lock(struct irq_data *data)
  466. {
  467. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  468. struct mcp23s08 *mcp = gpiochip_get_data(gc);
  469. mutex_lock(&mcp->lock);
  470. regcache_cache_only(mcp->regmap, true);
  471. }
  472. static void mcp23s08_irq_bus_unlock(struct irq_data *data)
  473. {
  474. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  475. struct mcp23s08 *mcp = gpiochip_get_data(gc);
  476. regcache_cache_only(mcp->regmap, false);
  477. regcache_sync(mcp->regmap);
  478. mutex_unlock(&mcp->lock);
  479. }
  480. static int mcp23s08_irq_setup(struct mcp23s08 *mcp)
  481. {
  482. struct gpio_chip *chip = &mcp->chip;
  483. int err;
  484. unsigned long irqflags = IRQF_ONESHOT | IRQF_SHARED;
  485. if (mcp->irq_active_high)
  486. irqflags |= IRQF_TRIGGER_HIGH;
  487. else
  488. irqflags |= IRQF_TRIGGER_LOW;
  489. err = devm_request_threaded_irq(chip->parent, mcp->irq, NULL,
  490. mcp23s08_irq,
  491. irqflags, dev_name(chip->parent), mcp);
  492. if (err != 0) {
  493. dev_err(chip->parent, "unable to request IRQ#%d: %d\n",
  494. mcp->irq, err);
  495. return err;
  496. }
  497. return 0;
  498. }
  499. static void mcp23s08_irq_print_chip(struct irq_data *d, struct seq_file *p)
  500. {
  501. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  502. struct mcp23s08 *mcp = gpiochip_get_data(gc);
  503. seq_printf(p, dev_name(mcp->dev));
  504. }
  505. static const struct irq_chip mcp23s08_irq_chip = {
  506. .irq_mask = mcp23s08_irq_mask,
  507. .irq_unmask = mcp23s08_irq_unmask,
  508. .irq_set_type = mcp23s08_irq_set_type,
  509. .irq_bus_lock = mcp23s08_irq_bus_lock,
  510. .irq_bus_sync_unlock = mcp23s08_irq_bus_unlock,
  511. .irq_print_chip = mcp23s08_irq_print_chip,
  512. .flags = IRQCHIP_IMMUTABLE,
  513. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  514. };
  515. /*----------------------------------------------------------------------*/
  516. int mcp23s08_probe_one(struct mcp23s08 *mcp, struct device *dev,
  517. unsigned int addr, unsigned int type, unsigned int base)
  518. {
  519. int status, ret;
  520. bool mirror = false;
  521. bool open_drain = false;
  522. mutex_init(&mcp->lock);
  523. mcp->dev = dev;
  524. mcp->addr = addr;
  525. mcp->irq_active_high = false;
  526. mcp->chip.direction_input = mcp23s08_direction_input;
  527. mcp->chip.get = mcp23s08_get;
  528. mcp->chip.get_multiple = mcp23s08_get_multiple;
  529. mcp->chip.direction_output = mcp23s08_direction_output;
  530. mcp->chip.set = mcp23s08_set;
  531. mcp->chip.set_multiple = mcp23s08_set_multiple;
  532. mcp->chip.base = base;
  533. mcp->chip.can_sleep = true;
  534. mcp->chip.parent = dev;
  535. mcp->chip.owner = THIS_MODULE;
  536. mcp->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
  537. /*
  538. * Reset the chip - we don't really know what state it's in, so reset
  539. * all pins to input first to prevent surprises.
  540. */
  541. ret = mcp_write(mcp, MCP_IODIR, mcp->chip.ngpio == 16 ? 0xFFFF : 0xFF);
  542. if (ret < 0)
  543. return ret;
  544. /* verify MCP_IOCON.SEQOP = 0, so sequential reads work,
  545. * and MCP_IOCON.HAEN = 1, so we work with all chips.
  546. */
  547. ret = mcp_read(mcp, MCP_IOCON, &status);
  548. if (ret < 0)
  549. return dev_err_probe(dev, ret, "can't identify chip %d\n", addr);
  550. mcp->irq_controller =
  551. device_property_read_bool(dev, "interrupt-controller");
  552. if (mcp->irq && mcp->irq_controller) {
  553. mcp->irq_active_high =
  554. device_property_read_bool(dev,
  555. "microchip,irq-active-high");
  556. mirror = device_property_read_bool(dev, "microchip,irq-mirror");
  557. open_drain = device_property_read_bool(dev, "drive-open-drain");
  558. }
  559. if ((status & IOCON_SEQOP) || !(status & IOCON_HAEN) || mirror ||
  560. mcp->irq_active_high || open_drain) {
  561. /* mcp23s17 has IOCON twice, make sure they are in sync */
  562. status &= ~(IOCON_SEQOP | (IOCON_SEQOP << 8));
  563. status |= IOCON_HAEN | (IOCON_HAEN << 8);
  564. if (mcp->irq_active_high)
  565. status |= IOCON_INTPOL | (IOCON_INTPOL << 8);
  566. else
  567. status &= ~(IOCON_INTPOL | (IOCON_INTPOL << 8));
  568. if (mirror)
  569. status |= IOCON_MIRROR | (IOCON_MIRROR << 8);
  570. if (open_drain)
  571. status |= IOCON_ODR | (IOCON_ODR << 8);
  572. if (type == MCP_TYPE_S18 || type == MCP_TYPE_018)
  573. status |= IOCON_INTCC | (IOCON_INTCC << 8);
  574. ret = mcp_write(mcp, MCP_IOCON, status);
  575. if (ret < 0)
  576. return dev_err_probe(dev, ret, "can't write IOCON %d\n", addr);
  577. }
  578. if (mcp->irq && mcp->irq_controller) {
  579. struct gpio_irq_chip *girq = &mcp->chip.irq;
  580. gpio_irq_chip_set_chip(girq, &mcp23s08_irq_chip);
  581. /* This will let us handle the parent IRQ in the driver */
  582. girq->parent_handler = NULL;
  583. girq->num_parents = 0;
  584. girq->parents = NULL;
  585. girq->default_type = IRQ_TYPE_NONE;
  586. girq->handler = handle_simple_irq;
  587. girq->threaded = true;
  588. }
  589. ret = devm_gpiochip_add_data(dev, &mcp->chip, mcp);
  590. if (ret < 0)
  591. return dev_err_probe(dev, ret, "can't add GPIO chip\n");
  592. mcp->pinctrl_desc.pctlops = &mcp_pinctrl_ops;
  593. mcp->pinctrl_desc.confops = &mcp_pinconf_ops;
  594. mcp->pinctrl_desc.npins = mcp->chip.ngpio;
  595. if (mcp->pinctrl_desc.npins == 8)
  596. mcp->pinctrl_desc.pins = mcp23x08_pins;
  597. else if (mcp->pinctrl_desc.npins == 16)
  598. mcp->pinctrl_desc.pins = mcp23x17_pins;
  599. mcp->pinctrl_desc.owner = THIS_MODULE;
  600. mcp->pctldev = devm_pinctrl_register(dev, &mcp->pinctrl_desc, mcp);
  601. if (IS_ERR(mcp->pctldev))
  602. return dev_err_probe(dev, PTR_ERR(mcp->pctldev), "can't register controller\n");
  603. if (mcp->irq) {
  604. ret = mcp23s08_irq_setup(mcp);
  605. if (ret)
  606. return dev_err_probe(dev, ret, "can't setup IRQ\n");
  607. }
  608. return 0;
  609. }
  610. EXPORT_SYMBOL_GPL(mcp23s08_probe_one);
  611. MODULE_DESCRIPTION("MCP23S08 SPI/I2C GPIO driver");
  612. MODULE_LICENSE("GPL");