pinctrl-mcp23s08.c 19 KB

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