pinctrl-mcp23s08.c 17 KB

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