pinctrl-mcp23s08.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  1. /* MCP23S08 SPI/I2C GPIO driver */
  2. #include <linux/kernel.h>
  3. #include <linux/device.h>
  4. #include <linux/mutex.h>
  5. #include <linux/module.h>
  6. #include <linux/gpio.h>
  7. #include <linux/i2c.h>
  8. #include <linux/spi/spi.h>
  9. #include <linux/spi/mcp23s08.h>
  10. #include <linux/slab.h>
  11. #include <asm/byteorder.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/of_device.h>
  14. #include <linux/regmap.h>
  15. #include <linux/pinctrl/pinctrl.h>
  16. #include <linux/pinctrl/pinconf.h>
  17. #include <linux/pinctrl/pinconf-generic.h>
  18. /*
  19. * MCP types supported by driver
  20. */
  21. #define MCP_TYPE_S08 0
  22. #define MCP_TYPE_S17 1
  23. #define MCP_TYPE_008 2
  24. #define MCP_TYPE_017 3
  25. #define MCP_TYPE_S18 4
  26. #define MCP_MAX_DEV_PER_CS 8
  27. /* Registers are all 8 bits wide.
  28. *
  29. * The mcp23s17 has twice as many bits, and can be configured to work
  30. * with either 16 bit registers or with two adjacent 8 bit banks.
  31. */
  32. #define MCP_IODIR 0x00 /* init/reset: all ones */
  33. #define MCP_IPOL 0x01
  34. #define MCP_GPINTEN 0x02
  35. #define MCP_DEFVAL 0x03
  36. #define MCP_INTCON 0x04
  37. #define MCP_IOCON 0x05
  38. # define IOCON_MIRROR (1 << 6)
  39. # define IOCON_SEQOP (1 << 5)
  40. # define IOCON_HAEN (1 << 3)
  41. # define IOCON_ODR (1 << 2)
  42. # define IOCON_INTPOL (1 << 1)
  43. # define IOCON_INTCC (1)
  44. #define MCP_GPPU 0x06
  45. #define MCP_INTF 0x07
  46. #define MCP_INTCAP 0x08
  47. #define MCP_GPIO 0x09
  48. #define MCP_OLAT 0x0a
  49. struct mcp23s08;
  50. struct mcp23s08 {
  51. u8 addr;
  52. bool irq_active_high;
  53. bool reg_shift;
  54. u16 irq_rise;
  55. u16 irq_fall;
  56. int irq;
  57. bool irq_controller;
  58. int cached_gpio;
  59. /* lock protects regmap access with bypass/cache flags */
  60. struct mutex lock;
  61. struct gpio_chip chip;
  62. struct regmap *regmap;
  63. struct device *dev;
  64. struct pinctrl_dev *pctldev;
  65. struct pinctrl_desc pinctrl_desc;
  66. };
  67. static const struct reg_default mcp23x08_defaults[] = {
  68. {.reg = MCP_IODIR, .def = 0xff},
  69. {.reg = MCP_IPOL, .def = 0x00},
  70. {.reg = MCP_GPINTEN, .def = 0x00},
  71. {.reg = MCP_DEFVAL, .def = 0x00},
  72. {.reg = MCP_INTCON, .def = 0x00},
  73. {.reg = MCP_IOCON, .def = 0x00},
  74. {.reg = MCP_GPPU, .def = 0x00},
  75. {.reg = MCP_OLAT, .def = 0x00},
  76. };
  77. static const struct regmap_range mcp23x08_volatile_range = {
  78. .range_min = MCP_INTF,
  79. .range_max = MCP_GPIO,
  80. };
  81. static const struct regmap_access_table mcp23x08_volatile_table = {
  82. .yes_ranges = &mcp23x08_volatile_range,
  83. .n_yes_ranges = 1,
  84. };
  85. static const struct regmap_range mcp23x08_precious_range = {
  86. .range_min = MCP_GPIO,
  87. .range_max = MCP_GPIO,
  88. };
  89. static const struct regmap_access_table mcp23x08_precious_table = {
  90. .yes_ranges = &mcp23x08_precious_range,
  91. .n_yes_ranges = 1,
  92. };
  93. static const struct regmap_config mcp23x08_regmap = {
  94. .reg_bits = 8,
  95. .val_bits = 8,
  96. .reg_stride = 1,
  97. .volatile_table = &mcp23x08_volatile_table,
  98. .precious_table = &mcp23x08_precious_table,
  99. .reg_defaults = mcp23x08_defaults,
  100. .num_reg_defaults = ARRAY_SIZE(mcp23x08_defaults),
  101. .cache_type = REGCACHE_FLAT,
  102. .max_register = MCP_OLAT,
  103. };
  104. static const struct reg_default mcp23x16_defaults[] = {
  105. {.reg = MCP_IODIR << 1, .def = 0xffff},
  106. {.reg = MCP_IPOL << 1, .def = 0x0000},
  107. {.reg = MCP_GPINTEN << 1, .def = 0x0000},
  108. {.reg = MCP_DEFVAL << 1, .def = 0x0000},
  109. {.reg = MCP_INTCON << 1, .def = 0x0000},
  110. {.reg = MCP_IOCON << 1, .def = 0x0000},
  111. {.reg = MCP_GPPU << 1, .def = 0x0000},
  112. {.reg = MCP_OLAT << 1, .def = 0x0000},
  113. };
  114. static const struct regmap_range mcp23x16_volatile_range = {
  115. .range_min = MCP_INTF << 1,
  116. .range_max = MCP_GPIO << 1,
  117. };
  118. static const struct regmap_access_table mcp23x16_volatile_table = {
  119. .yes_ranges = &mcp23x16_volatile_range,
  120. .n_yes_ranges = 1,
  121. };
  122. static const struct regmap_range mcp23x16_precious_range = {
  123. .range_min = MCP_GPIO << 1,
  124. .range_max = MCP_GPIO << 1,
  125. };
  126. static const struct regmap_access_table mcp23x16_precious_table = {
  127. .yes_ranges = &mcp23x16_precious_range,
  128. .n_yes_ranges = 1,
  129. };
  130. static const struct regmap_config mcp23x17_regmap = {
  131. .reg_bits = 8,
  132. .val_bits = 16,
  133. .reg_stride = 2,
  134. .max_register = MCP_OLAT << 1,
  135. .volatile_table = &mcp23x16_volatile_table,
  136. .precious_table = &mcp23x16_precious_table,
  137. .reg_defaults = mcp23x16_defaults,
  138. .num_reg_defaults = ARRAY_SIZE(mcp23x16_defaults),
  139. .cache_type = REGCACHE_FLAT,
  140. .val_format_endian = REGMAP_ENDIAN_LITTLE,
  141. };
  142. static int mcp_read(struct mcp23s08 *mcp, unsigned int reg, unsigned int *val)
  143. {
  144. return regmap_read(mcp->regmap, reg << mcp->reg_shift, val);
  145. }
  146. static int mcp_write(struct mcp23s08 *mcp, unsigned int reg, unsigned int val)
  147. {
  148. return regmap_write(mcp->regmap, reg << mcp->reg_shift, val);
  149. }
  150. static int mcp_set_mask(struct mcp23s08 *mcp, unsigned int reg,
  151. unsigned int mask, bool enabled)
  152. {
  153. u16 val = enabled ? 0xffff : 0x0000;
  154. return regmap_update_bits(mcp->regmap, reg << mcp->reg_shift,
  155. mask, val);
  156. }
  157. static int mcp_set_bit(struct mcp23s08 *mcp, unsigned int reg,
  158. unsigned int pin, bool enabled)
  159. {
  160. u16 mask = BIT(pin);
  161. return mcp_set_mask(mcp, reg, mask, enabled);
  162. }
  163. static const struct pinctrl_pin_desc mcp23x08_pins[] = {
  164. PINCTRL_PIN(0, "gpio0"),
  165. PINCTRL_PIN(1, "gpio1"),
  166. PINCTRL_PIN(2, "gpio2"),
  167. PINCTRL_PIN(3, "gpio3"),
  168. PINCTRL_PIN(4, "gpio4"),
  169. PINCTRL_PIN(5, "gpio5"),
  170. PINCTRL_PIN(6, "gpio6"),
  171. PINCTRL_PIN(7, "gpio7"),
  172. };
  173. static const struct pinctrl_pin_desc mcp23x17_pins[] = {
  174. PINCTRL_PIN(0, "gpio0"),
  175. PINCTRL_PIN(1, "gpio1"),
  176. PINCTRL_PIN(2, "gpio2"),
  177. PINCTRL_PIN(3, "gpio3"),
  178. PINCTRL_PIN(4, "gpio4"),
  179. PINCTRL_PIN(5, "gpio5"),
  180. PINCTRL_PIN(6, "gpio6"),
  181. PINCTRL_PIN(7, "gpio7"),
  182. PINCTRL_PIN(8, "gpio8"),
  183. PINCTRL_PIN(9, "gpio9"),
  184. PINCTRL_PIN(10, "gpio10"),
  185. PINCTRL_PIN(11, "gpio11"),
  186. PINCTRL_PIN(12, "gpio12"),
  187. PINCTRL_PIN(13, "gpio13"),
  188. PINCTRL_PIN(14, "gpio14"),
  189. PINCTRL_PIN(15, "gpio15"),
  190. };
  191. static int mcp_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
  192. {
  193. return 0;
  194. }
  195. static const char *mcp_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
  196. unsigned int group)
  197. {
  198. return NULL;
  199. }
  200. static int mcp_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
  201. unsigned int group,
  202. const unsigned int **pins,
  203. unsigned int *num_pins)
  204. {
  205. return -ENOTSUPP;
  206. }
  207. static const struct pinctrl_ops mcp_pinctrl_ops = {
  208. .get_groups_count = mcp_pinctrl_get_groups_count,
  209. .get_group_name = mcp_pinctrl_get_group_name,
  210. .get_group_pins = mcp_pinctrl_get_group_pins,
  211. #ifdef CONFIG_OF
  212. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  213. .dt_free_map = pinconf_generic_dt_free_map,
  214. #endif
  215. };
  216. static int mcp_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
  217. unsigned long *config)
  218. {
  219. struct mcp23s08 *mcp = pinctrl_dev_get_drvdata(pctldev);
  220. enum pin_config_param param = pinconf_to_config_param(*config);
  221. unsigned int data, status;
  222. int ret;
  223. switch (param) {
  224. case PIN_CONFIG_BIAS_PULL_UP:
  225. ret = mcp_read(mcp, MCP_GPPU, &data);
  226. if (ret < 0)
  227. return ret;
  228. status = (data & BIT(pin)) ? 1 : 0;
  229. break;
  230. default:
  231. dev_err(mcp->dev, "Invalid config param %04x\n", param);
  232. return -ENOTSUPP;
  233. }
  234. *config = 0;
  235. return status ? 0 : -EINVAL;
  236. }
  237. static int mcp_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  238. unsigned long *configs, unsigned int num_configs)
  239. {
  240. struct mcp23s08 *mcp = pinctrl_dev_get_drvdata(pctldev);
  241. enum pin_config_param param;
  242. u32 arg, mask;
  243. u16 val;
  244. int ret = 0;
  245. int i;
  246. for (i = 0; i < num_configs; i++) {
  247. param = pinconf_to_config_param(configs[i]);
  248. arg = pinconf_to_config_argument(configs[i]);
  249. switch (param) {
  250. case PIN_CONFIG_BIAS_PULL_UP:
  251. val = arg ? 0xFFFF : 0x0000;
  252. mask = BIT(pin);
  253. ret = mcp_set_bit(mcp, MCP_GPPU, pin, arg);
  254. break;
  255. default:
  256. dev_err(mcp->dev, "Invalid config param %04x\n", param);
  257. return -ENOTSUPP;
  258. }
  259. }
  260. return ret;
  261. }
  262. static const struct pinconf_ops mcp_pinconf_ops = {
  263. .pin_config_get = mcp_pinconf_get,
  264. .pin_config_set = mcp_pinconf_set,
  265. .is_generic = true,
  266. };
  267. /*----------------------------------------------------------------------*/
  268. #ifdef CONFIG_SPI_MASTER
  269. static int mcp23sxx_spi_write(void *context, const void *data, size_t count)
  270. {
  271. struct mcp23s08 *mcp = context;
  272. struct spi_device *spi = to_spi_device(mcp->dev);
  273. struct spi_message m;
  274. struct spi_transfer t[2] = { { .tx_buf = &mcp->addr, .len = 1, },
  275. { .tx_buf = data, .len = count, }, };
  276. spi_message_init(&m);
  277. spi_message_add_tail(&t[0], &m);
  278. spi_message_add_tail(&t[1], &m);
  279. return spi_sync(spi, &m);
  280. }
  281. static int mcp23sxx_spi_gather_write(void *context,
  282. const void *reg, size_t reg_size,
  283. const void *val, size_t val_size)
  284. {
  285. struct mcp23s08 *mcp = context;
  286. struct spi_device *spi = to_spi_device(mcp->dev);
  287. struct spi_message m;
  288. struct spi_transfer t[3] = { { .tx_buf = &mcp->addr, .len = 1, },
  289. { .tx_buf = reg, .len = reg_size, },
  290. { .tx_buf = val, .len = val_size, }, };
  291. spi_message_init(&m);
  292. spi_message_add_tail(&t[0], &m);
  293. spi_message_add_tail(&t[1], &m);
  294. spi_message_add_tail(&t[2], &m);
  295. return spi_sync(spi, &m);
  296. }
  297. static int mcp23sxx_spi_read(void *context, const void *reg, size_t reg_size,
  298. void *val, size_t val_size)
  299. {
  300. struct mcp23s08 *mcp = context;
  301. struct spi_device *spi = to_spi_device(mcp->dev);
  302. u8 tx[2];
  303. if (reg_size != 1)
  304. return -EINVAL;
  305. tx[0] = mcp->addr | 0x01;
  306. tx[1] = *((u8 *) reg);
  307. return spi_write_then_read(spi, tx, sizeof(tx), val, val_size);
  308. }
  309. static const struct regmap_bus mcp23sxx_spi_regmap = {
  310. .write = mcp23sxx_spi_write,
  311. .gather_write = mcp23sxx_spi_gather_write,
  312. .read = mcp23sxx_spi_read,
  313. };
  314. #endif /* CONFIG_SPI_MASTER */
  315. /*----------------------------------------------------------------------*/
  316. /* A given spi_device can represent up to eight mcp23sxx chips
  317. * sharing the same chipselect but using different addresses
  318. * (e.g. chips #0 and #3 might be populated, but not #1 or $2).
  319. * Driver data holds all the per-chip data.
  320. */
  321. struct mcp23s08_driver_data {
  322. unsigned ngpio;
  323. struct mcp23s08 *mcp[8];
  324. struct mcp23s08 chip[];
  325. };
  326. static int mcp23s08_direction_input(struct gpio_chip *chip, unsigned offset)
  327. {
  328. struct mcp23s08 *mcp = gpiochip_get_data(chip);
  329. int status;
  330. mutex_lock(&mcp->lock);
  331. status = mcp_set_bit(mcp, MCP_IODIR, offset, true);
  332. mutex_unlock(&mcp->lock);
  333. return status;
  334. }
  335. static int mcp23s08_get(struct gpio_chip *chip, unsigned offset)
  336. {
  337. struct mcp23s08 *mcp = gpiochip_get_data(chip);
  338. int status, ret;
  339. mutex_lock(&mcp->lock);
  340. /* REVISIT reading this clears any IRQ ... */
  341. ret = mcp_read(mcp, MCP_GPIO, &status);
  342. if (ret < 0)
  343. status = 0;
  344. else
  345. status = !!(status & (1 << offset));
  346. mcp->cached_gpio = status;
  347. mutex_unlock(&mcp->lock);
  348. return status;
  349. }
  350. static int __mcp23s08_set(struct mcp23s08 *mcp, unsigned mask, bool value)
  351. {
  352. return mcp_set_mask(mcp, MCP_OLAT, mask, value);
  353. }
  354. static void mcp23s08_set(struct gpio_chip *chip, unsigned offset, int value)
  355. {
  356. struct mcp23s08 *mcp = gpiochip_get_data(chip);
  357. unsigned mask = BIT(offset);
  358. mutex_lock(&mcp->lock);
  359. __mcp23s08_set(mcp, mask, !!value);
  360. mutex_unlock(&mcp->lock);
  361. }
  362. static int
  363. mcp23s08_direction_output(struct gpio_chip *chip, unsigned offset, int value)
  364. {
  365. struct mcp23s08 *mcp = gpiochip_get_data(chip);
  366. unsigned mask = BIT(offset);
  367. int status;
  368. mutex_lock(&mcp->lock);
  369. status = __mcp23s08_set(mcp, mask, value);
  370. if (status == 0) {
  371. status = mcp_set_mask(mcp, MCP_IODIR, mask, false);
  372. }
  373. mutex_unlock(&mcp->lock);
  374. return status;
  375. }
  376. /*----------------------------------------------------------------------*/
  377. static irqreturn_t mcp23s08_irq(int irq, void *data)
  378. {
  379. struct mcp23s08 *mcp = data;
  380. int intcap, intcon, intf, i, gpio, gpio_orig, intcap_mask, defval;
  381. unsigned int child_irq;
  382. bool intf_set, intcap_changed, gpio_bit_changed,
  383. defval_changed, gpio_set;
  384. mutex_lock(&mcp->lock);
  385. if (mcp_read(mcp, MCP_INTF, &intf) < 0) {
  386. mutex_unlock(&mcp->lock);
  387. return IRQ_HANDLED;
  388. }
  389. if (mcp_read(mcp, MCP_INTCAP, &intcap) < 0) {
  390. mutex_unlock(&mcp->lock);
  391. return IRQ_HANDLED;
  392. }
  393. if (mcp_read(mcp, MCP_INTCON, &intcon) < 0) {
  394. mutex_unlock(&mcp->lock);
  395. return IRQ_HANDLED;
  396. }
  397. if (mcp_read(mcp, MCP_DEFVAL, &defval) < 0) {
  398. mutex_unlock(&mcp->lock);
  399. return IRQ_HANDLED;
  400. }
  401. /* This clears the interrupt(configurable on S18) */
  402. if (mcp_read(mcp, MCP_GPIO, &gpio) < 0) {
  403. mutex_unlock(&mcp->lock);
  404. return IRQ_HANDLED;
  405. }
  406. gpio_orig = mcp->cached_gpio;
  407. mcp->cached_gpio = gpio;
  408. mutex_unlock(&mcp->lock);
  409. if (intf == 0) {
  410. /* There is no interrupt pending */
  411. return IRQ_HANDLED;
  412. }
  413. dev_dbg(mcp->chip.parent,
  414. "intcap 0x%04X intf 0x%04X gpio_orig 0x%04X gpio 0x%04X\n",
  415. intcap, intf, gpio_orig, gpio);
  416. for (i = 0; i < mcp->chip.ngpio; i++) {
  417. /* We must check all of the inputs on the chip,
  418. * otherwise we may not notice a change on >=2 pins.
  419. *
  420. * On at least the mcp23s17, INTCAP is only updated
  421. * one byte at a time(INTCAPA and INTCAPB are
  422. * not written to at the same time - only on a per-bank
  423. * basis).
  424. *
  425. * INTF only contains the single bit that caused the
  426. * interrupt per-bank. On the mcp23s17, there is
  427. * INTFA and INTFB. If two pins are changed on the A
  428. * side at the same time, INTF will only have one bit
  429. * set. If one pin on the A side and one pin on the B
  430. * side are changed at the same time, INTF will have
  431. * two bits set. Thus, INTF can't be the only check
  432. * to see if the input has changed.
  433. */
  434. intf_set = intf & BIT(i);
  435. if (i < 8 && intf_set)
  436. intcap_mask = 0x00FF;
  437. else if (i >= 8 && intf_set)
  438. intcap_mask = 0xFF00;
  439. else
  440. intcap_mask = 0x00;
  441. intcap_changed = (intcap_mask &
  442. (intcap & BIT(i))) !=
  443. (intcap_mask & (BIT(i) & gpio_orig));
  444. gpio_set = BIT(i) & gpio;
  445. gpio_bit_changed = (BIT(i) & gpio_orig) !=
  446. (BIT(i) & gpio);
  447. defval_changed = (BIT(i) & intcon) &&
  448. ((BIT(i) & gpio) !=
  449. (BIT(i) & defval));
  450. if (((gpio_bit_changed || intcap_changed) &&
  451. (BIT(i) & mcp->irq_rise) && gpio_set) ||
  452. ((gpio_bit_changed || intcap_changed) &&
  453. (BIT(i) & mcp->irq_fall) && !gpio_set) ||
  454. defval_changed) {
  455. child_irq = irq_find_mapping(mcp->chip.irq.domain, i);
  456. handle_nested_irq(child_irq);
  457. }
  458. }
  459. return IRQ_HANDLED;
  460. }
  461. static void mcp23s08_irq_mask(struct irq_data *data)
  462. {
  463. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  464. struct mcp23s08 *mcp = gpiochip_get_data(gc);
  465. unsigned int pos = data->hwirq;
  466. mcp_set_bit(mcp, MCP_GPINTEN, pos, false);
  467. }
  468. static void mcp23s08_irq_unmask(struct irq_data *data)
  469. {
  470. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  471. struct mcp23s08 *mcp = gpiochip_get_data(gc);
  472. unsigned int pos = data->hwirq;
  473. mcp_set_bit(mcp, MCP_GPINTEN, pos, true);
  474. }
  475. static int mcp23s08_irq_set_type(struct irq_data *data, unsigned int type)
  476. {
  477. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  478. struct mcp23s08 *mcp = gpiochip_get_data(gc);
  479. unsigned int pos = data->hwirq;
  480. int status = 0;
  481. if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
  482. mcp_set_bit(mcp, MCP_INTCON, pos, false);
  483. mcp->irq_rise |= BIT(pos);
  484. mcp->irq_fall |= BIT(pos);
  485. } else if (type & IRQ_TYPE_EDGE_RISING) {
  486. mcp_set_bit(mcp, MCP_INTCON, pos, false);
  487. mcp->irq_rise |= BIT(pos);
  488. mcp->irq_fall &= ~BIT(pos);
  489. } else if (type & IRQ_TYPE_EDGE_FALLING) {
  490. mcp_set_bit(mcp, MCP_INTCON, pos, false);
  491. mcp->irq_rise &= ~BIT(pos);
  492. mcp->irq_fall |= BIT(pos);
  493. } else if (type & IRQ_TYPE_LEVEL_HIGH) {
  494. mcp_set_bit(mcp, MCP_INTCON, pos, true);
  495. mcp_set_bit(mcp, MCP_DEFVAL, pos, false);
  496. } else if (type & IRQ_TYPE_LEVEL_LOW) {
  497. mcp_set_bit(mcp, MCP_INTCON, pos, true);
  498. mcp_set_bit(mcp, MCP_DEFVAL, pos, true);
  499. } else
  500. return -EINVAL;
  501. return status;
  502. }
  503. static void mcp23s08_irq_bus_lock(struct irq_data *data)
  504. {
  505. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  506. struct mcp23s08 *mcp = gpiochip_get_data(gc);
  507. mutex_lock(&mcp->lock);
  508. regcache_cache_only(mcp->regmap, true);
  509. }
  510. static void mcp23s08_irq_bus_unlock(struct irq_data *data)
  511. {
  512. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  513. struct mcp23s08 *mcp = gpiochip_get_data(gc);
  514. regcache_cache_only(mcp->regmap, false);
  515. regcache_sync(mcp->regmap);
  516. mutex_unlock(&mcp->lock);
  517. }
  518. static struct irq_chip mcp23s08_irq_chip = {
  519. .name = "gpio-mcp23xxx",
  520. .irq_mask = mcp23s08_irq_mask,
  521. .irq_unmask = mcp23s08_irq_unmask,
  522. .irq_set_type = mcp23s08_irq_set_type,
  523. .irq_bus_lock = mcp23s08_irq_bus_lock,
  524. .irq_bus_sync_unlock = mcp23s08_irq_bus_unlock,
  525. };
  526. static int mcp23s08_irq_setup(struct mcp23s08 *mcp)
  527. {
  528. struct gpio_chip *chip = &mcp->chip;
  529. int err;
  530. unsigned long irqflags = IRQF_ONESHOT | IRQF_SHARED;
  531. if (mcp->irq_active_high)
  532. irqflags |= IRQF_TRIGGER_HIGH;
  533. else
  534. irqflags |= IRQF_TRIGGER_LOW;
  535. err = devm_request_threaded_irq(chip->parent, mcp->irq, NULL,
  536. mcp23s08_irq,
  537. irqflags, dev_name(chip->parent), mcp);
  538. if (err != 0) {
  539. dev_err(chip->parent, "unable to request IRQ#%d: %d\n",
  540. mcp->irq, err);
  541. return err;
  542. }
  543. err = gpiochip_irqchip_add_nested(chip,
  544. &mcp23s08_irq_chip,
  545. 0,
  546. handle_simple_irq,
  547. IRQ_TYPE_NONE);
  548. if (err) {
  549. dev_err(chip->parent,
  550. "could not connect irqchip to gpiochip: %d\n", err);
  551. return err;
  552. }
  553. gpiochip_set_nested_irqchip(chip,
  554. &mcp23s08_irq_chip,
  555. mcp->irq);
  556. return 0;
  557. }
  558. /*----------------------------------------------------------------------*/
  559. #ifdef CONFIG_DEBUG_FS
  560. #include <linux/seq_file.h>
  561. /*
  562. * This compares the chip's registers with the register
  563. * cache and corrects any incorrectly set register. This
  564. * can be used to fix state for MCP23xxx, that temporary
  565. * lost its power supply.
  566. */
  567. #define MCP23S08_CONFIG_REGS 8
  568. static int __check_mcp23s08_reg_cache(struct mcp23s08 *mcp)
  569. {
  570. int cached[MCP23S08_CONFIG_REGS];
  571. int err = 0, i;
  572. /* read cached config registers */
  573. for (i = 0; i < MCP23S08_CONFIG_REGS; i++) {
  574. err = mcp_read(mcp, i, &cached[i]);
  575. if (err)
  576. goto out;
  577. }
  578. regcache_cache_bypass(mcp->regmap, true);
  579. for (i = 0; i < MCP23S08_CONFIG_REGS; i++) {
  580. int uncached;
  581. err = mcp_read(mcp, i, &uncached);
  582. if (err)
  583. goto out;
  584. if (uncached != cached[i]) {
  585. dev_err(mcp->dev, "restoring reg 0x%02x from 0x%04x to 0x%04x (power-loss?)\n",
  586. i, uncached, cached[i]);
  587. mcp_write(mcp, i, cached[i]);
  588. }
  589. }
  590. out:
  591. if (err)
  592. dev_err(mcp->dev, "read error: reg=%02x, err=%d", i, err);
  593. regcache_cache_bypass(mcp->regmap, false);
  594. return err;
  595. }
  596. /*
  597. * This shows more info than the generic gpio dump code:
  598. * pullups, deglitching, open drain drive.
  599. */
  600. static void mcp23s08_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  601. {
  602. struct mcp23s08 *mcp;
  603. char bank;
  604. int t;
  605. unsigned mask;
  606. int iodir, gpio, gppu;
  607. mcp = gpiochip_get_data(chip);
  608. /* NOTE: we only handle one bank for now ... */
  609. bank = '0' + ((mcp->addr >> 1) & 0x7);
  610. mutex_lock(&mcp->lock);
  611. t = __check_mcp23s08_reg_cache(mcp);
  612. if (t) {
  613. seq_printf(s, " I/O Error\n");
  614. goto done;
  615. }
  616. t = mcp_read(mcp, MCP_IODIR, &iodir);
  617. if (t) {
  618. seq_printf(s, " I/O Error\n");
  619. goto done;
  620. }
  621. t = mcp_read(mcp, MCP_GPIO, &gpio);
  622. if (t) {
  623. seq_printf(s, " I/O Error\n");
  624. goto done;
  625. }
  626. t = mcp_read(mcp, MCP_GPPU, &gppu);
  627. if (t) {
  628. seq_printf(s, " I/O Error\n");
  629. goto done;
  630. }
  631. for (t = 0, mask = BIT(0); t < chip->ngpio; t++, mask <<= 1) {
  632. const char *label;
  633. label = gpiochip_is_requested(chip, t);
  634. if (!label)
  635. continue;
  636. seq_printf(s, " gpio-%-3d P%c.%d (%-12s) %s %s %s",
  637. chip->base + t, bank, t, label,
  638. (iodir & mask) ? "in " : "out",
  639. (gpio & mask) ? "hi" : "lo",
  640. (gppu & mask) ? "up" : " ");
  641. /* NOTE: ignoring the irq-related registers */
  642. seq_puts(s, "\n");
  643. }
  644. done:
  645. mutex_unlock(&mcp->lock);
  646. }
  647. #else
  648. #define mcp23s08_dbg_show NULL
  649. #endif
  650. /*----------------------------------------------------------------------*/
  651. static int mcp23s08_probe_one(struct mcp23s08 *mcp, struct device *dev,
  652. void *data, unsigned addr, unsigned type,
  653. unsigned int base, int cs)
  654. {
  655. int status, ret;
  656. bool mirror = false;
  657. mutex_init(&mcp->lock);
  658. mcp->dev = dev;
  659. mcp->addr = addr;
  660. mcp->irq_active_high = false;
  661. mcp->chip.direction_input = mcp23s08_direction_input;
  662. mcp->chip.get = mcp23s08_get;
  663. mcp->chip.direction_output = mcp23s08_direction_output;
  664. mcp->chip.set = mcp23s08_set;
  665. mcp->chip.dbg_show = mcp23s08_dbg_show;
  666. #ifdef CONFIG_OF_GPIO
  667. mcp->chip.of_gpio_n_cells = 2;
  668. mcp->chip.of_node = dev->of_node;
  669. #endif
  670. switch (type) {
  671. #ifdef CONFIG_SPI_MASTER
  672. case MCP_TYPE_S08:
  673. mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp,
  674. &mcp23x08_regmap);
  675. mcp->reg_shift = 0;
  676. mcp->chip.ngpio = 8;
  677. mcp->chip.label = "mcp23s08";
  678. break;
  679. case MCP_TYPE_S17:
  680. mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp,
  681. &mcp23x17_regmap);
  682. mcp->reg_shift = 1;
  683. mcp->chip.ngpio = 16;
  684. mcp->chip.label = "mcp23s17";
  685. break;
  686. case MCP_TYPE_S18:
  687. mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp,
  688. &mcp23x17_regmap);
  689. mcp->reg_shift = 1;
  690. mcp->chip.ngpio = 16;
  691. mcp->chip.label = "mcp23s18";
  692. break;
  693. #endif /* CONFIG_SPI_MASTER */
  694. #if IS_ENABLED(CONFIG_I2C)
  695. case MCP_TYPE_008:
  696. mcp->regmap = devm_regmap_init_i2c(data, &mcp23x08_regmap);
  697. mcp->reg_shift = 0;
  698. mcp->chip.ngpio = 8;
  699. mcp->chip.label = "mcp23008";
  700. break;
  701. case MCP_TYPE_017:
  702. mcp->regmap = devm_regmap_init_i2c(data, &mcp23x17_regmap);
  703. mcp->reg_shift = 1;
  704. mcp->chip.ngpio = 16;
  705. mcp->chip.label = "mcp23017";
  706. break;
  707. #endif /* CONFIG_I2C */
  708. default:
  709. dev_err(dev, "invalid device type (%d)\n", type);
  710. return -EINVAL;
  711. }
  712. if (IS_ERR(mcp->regmap))
  713. return PTR_ERR(mcp->regmap);
  714. mcp->chip.base = base;
  715. mcp->chip.can_sleep = true;
  716. mcp->chip.parent = dev;
  717. mcp->chip.owner = THIS_MODULE;
  718. /* verify MCP_IOCON.SEQOP = 0, so sequential reads work,
  719. * and MCP_IOCON.HAEN = 1, so we work with all chips.
  720. */
  721. ret = mcp_read(mcp, MCP_IOCON, &status);
  722. if (ret < 0)
  723. goto fail;
  724. mcp->irq_controller =
  725. device_property_read_bool(dev, "interrupt-controller");
  726. if (mcp->irq && mcp->irq_controller) {
  727. mcp->irq_active_high =
  728. device_property_read_bool(dev,
  729. "microchip,irq-active-high");
  730. mirror = device_property_read_bool(dev, "microchip,irq-mirror");
  731. }
  732. if ((status & IOCON_SEQOP) || !(status & IOCON_HAEN) || mirror ||
  733. mcp->irq_active_high) {
  734. /* mcp23s17 has IOCON twice, make sure they are in sync */
  735. status &= ~(IOCON_SEQOP | (IOCON_SEQOP << 8));
  736. status |= IOCON_HAEN | (IOCON_HAEN << 8);
  737. if (mcp->irq_active_high)
  738. status |= IOCON_INTPOL | (IOCON_INTPOL << 8);
  739. else
  740. status &= ~(IOCON_INTPOL | (IOCON_INTPOL << 8));
  741. if (mirror)
  742. status |= IOCON_MIRROR | (IOCON_MIRROR << 8);
  743. if (type == MCP_TYPE_S18)
  744. status |= IOCON_INTCC | (IOCON_INTCC << 8);
  745. ret = mcp_write(mcp, MCP_IOCON, status);
  746. if (ret < 0)
  747. goto fail;
  748. }
  749. ret = devm_gpiochip_add_data(dev, &mcp->chip, mcp);
  750. if (ret < 0)
  751. goto fail;
  752. if (mcp->irq && mcp->irq_controller) {
  753. ret = mcp23s08_irq_setup(mcp);
  754. if (ret)
  755. goto fail;
  756. }
  757. mcp->pinctrl_desc.name = "mcp23xxx-pinctrl";
  758. mcp->pinctrl_desc.pctlops = &mcp_pinctrl_ops;
  759. mcp->pinctrl_desc.confops = &mcp_pinconf_ops;
  760. mcp->pinctrl_desc.npins = mcp->chip.ngpio;
  761. if (mcp->pinctrl_desc.npins == 8)
  762. mcp->pinctrl_desc.pins = mcp23x08_pins;
  763. else if (mcp->pinctrl_desc.npins == 16)
  764. mcp->pinctrl_desc.pins = mcp23x17_pins;
  765. mcp->pinctrl_desc.owner = THIS_MODULE;
  766. mcp->pctldev = devm_pinctrl_register(dev, &mcp->pinctrl_desc, mcp);
  767. if (IS_ERR(mcp->pctldev)) {
  768. ret = PTR_ERR(mcp->pctldev);
  769. goto fail;
  770. }
  771. fail:
  772. if (ret < 0)
  773. dev_dbg(dev, "can't setup chip %d, --> %d\n", addr, ret);
  774. return ret;
  775. }
  776. /*----------------------------------------------------------------------*/
  777. #ifdef CONFIG_OF
  778. #ifdef CONFIG_SPI_MASTER
  779. static const struct of_device_id mcp23s08_spi_of_match[] = {
  780. {
  781. .compatible = "microchip,mcp23s08",
  782. .data = (void *) MCP_TYPE_S08,
  783. },
  784. {
  785. .compatible = "microchip,mcp23s17",
  786. .data = (void *) MCP_TYPE_S17,
  787. },
  788. {
  789. .compatible = "microchip,mcp23s18",
  790. .data = (void *) MCP_TYPE_S18,
  791. },
  792. /* NOTE: The use of the mcp prefix is deprecated and will be removed. */
  793. {
  794. .compatible = "mcp,mcp23s08",
  795. .data = (void *) MCP_TYPE_S08,
  796. },
  797. {
  798. .compatible = "mcp,mcp23s17",
  799. .data = (void *) MCP_TYPE_S17,
  800. },
  801. { },
  802. };
  803. MODULE_DEVICE_TABLE(of, mcp23s08_spi_of_match);
  804. #endif
  805. #if IS_ENABLED(CONFIG_I2C)
  806. static const struct of_device_id mcp23s08_i2c_of_match[] = {
  807. {
  808. .compatible = "microchip,mcp23008",
  809. .data = (void *) MCP_TYPE_008,
  810. },
  811. {
  812. .compatible = "microchip,mcp23017",
  813. .data = (void *) MCP_TYPE_017,
  814. },
  815. /* NOTE: The use of the mcp prefix is deprecated and will be removed. */
  816. {
  817. .compatible = "mcp,mcp23008",
  818. .data = (void *) MCP_TYPE_008,
  819. },
  820. {
  821. .compatible = "mcp,mcp23017",
  822. .data = (void *) MCP_TYPE_017,
  823. },
  824. { },
  825. };
  826. MODULE_DEVICE_TABLE(of, mcp23s08_i2c_of_match);
  827. #endif
  828. #endif /* CONFIG_OF */
  829. #if IS_ENABLED(CONFIG_I2C)
  830. static int mcp230xx_probe(struct i2c_client *client,
  831. const struct i2c_device_id *id)
  832. {
  833. struct mcp23s08_platform_data *pdata, local_pdata;
  834. struct mcp23s08 *mcp;
  835. int status;
  836. pdata = dev_get_platdata(&client->dev);
  837. if (!pdata) {
  838. pdata = &local_pdata;
  839. pdata->base = -1;
  840. }
  841. mcp = devm_kzalloc(&client->dev, sizeof(*mcp), GFP_KERNEL);
  842. if (!mcp)
  843. return -ENOMEM;
  844. mcp->irq = client->irq;
  845. status = mcp23s08_probe_one(mcp, &client->dev, client, client->addr,
  846. id->driver_data, pdata->base, 0);
  847. if (status)
  848. return status;
  849. i2c_set_clientdata(client, mcp);
  850. return 0;
  851. }
  852. static const struct i2c_device_id mcp230xx_id[] = {
  853. { "mcp23008", MCP_TYPE_008 },
  854. { "mcp23017", MCP_TYPE_017 },
  855. { },
  856. };
  857. MODULE_DEVICE_TABLE(i2c, mcp230xx_id);
  858. static struct i2c_driver mcp230xx_driver = {
  859. .driver = {
  860. .name = "mcp230xx",
  861. .of_match_table = of_match_ptr(mcp23s08_i2c_of_match),
  862. },
  863. .probe = mcp230xx_probe,
  864. .id_table = mcp230xx_id,
  865. };
  866. static int __init mcp23s08_i2c_init(void)
  867. {
  868. return i2c_add_driver(&mcp230xx_driver);
  869. }
  870. static void mcp23s08_i2c_exit(void)
  871. {
  872. i2c_del_driver(&mcp230xx_driver);
  873. }
  874. #else
  875. static int __init mcp23s08_i2c_init(void) { return 0; }
  876. static void mcp23s08_i2c_exit(void) { }
  877. #endif /* CONFIG_I2C */
  878. /*----------------------------------------------------------------------*/
  879. #ifdef CONFIG_SPI_MASTER
  880. static int mcp23s08_probe(struct spi_device *spi)
  881. {
  882. struct mcp23s08_platform_data *pdata, local_pdata;
  883. unsigned addr;
  884. int chips = 0;
  885. struct mcp23s08_driver_data *data;
  886. int status, type;
  887. unsigned ngpio = 0;
  888. const struct of_device_id *match;
  889. match = of_match_device(of_match_ptr(mcp23s08_spi_of_match), &spi->dev);
  890. if (match)
  891. type = (int)(uintptr_t)match->data;
  892. else
  893. type = spi_get_device_id(spi)->driver_data;
  894. pdata = dev_get_platdata(&spi->dev);
  895. if (!pdata) {
  896. pdata = &local_pdata;
  897. pdata->base = -1;
  898. status = device_property_read_u32(&spi->dev,
  899. "microchip,spi-present-mask", &pdata->spi_present_mask);
  900. if (status) {
  901. status = device_property_read_u32(&spi->dev,
  902. "mcp,spi-present-mask",
  903. &pdata->spi_present_mask);
  904. if (status) {
  905. dev_err(&spi->dev, "missing spi-present-mask");
  906. return -ENODEV;
  907. }
  908. }
  909. }
  910. if (!pdata->spi_present_mask || pdata->spi_present_mask > 0xff) {
  911. dev_err(&spi->dev, "invalid spi-present-mask");
  912. return -ENODEV;
  913. }
  914. for (addr = 0; addr < MCP_MAX_DEV_PER_CS; addr++) {
  915. if (pdata->spi_present_mask & BIT(addr))
  916. chips++;
  917. }
  918. if (!chips)
  919. return -ENODEV;
  920. data = devm_kzalloc(&spi->dev,
  921. sizeof(*data) + chips * sizeof(struct mcp23s08),
  922. GFP_KERNEL);
  923. if (!data)
  924. return -ENOMEM;
  925. spi_set_drvdata(spi, data);
  926. for (addr = 0; addr < MCP_MAX_DEV_PER_CS; addr++) {
  927. if (!(pdata->spi_present_mask & BIT(addr)))
  928. continue;
  929. chips--;
  930. data->mcp[addr] = &data->chip[chips];
  931. data->mcp[addr]->irq = spi->irq;
  932. status = mcp23s08_probe_one(data->mcp[addr], &spi->dev, spi,
  933. 0x40 | (addr << 1), type,
  934. pdata->base, addr);
  935. if (status < 0)
  936. return status;
  937. if (pdata->base != -1)
  938. pdata->base += data->mcp[addr]->chip.ngpio;
  939. ngpio += data->mcp[addr]->chip.ngpio;
  940. }
  941. data->ngpio = ngpio;
  942. return 0;
  943. }
  944. static const struct spi_device_id mcp23s08_ids[] = {
  945. { "mcp23s08", MCP_TYPE_S08 },
  946. { "mcp23s17", MCP_TYPE_S17 },
  947. { "mcp23s18", MCP_TYPE_S18 },
  948. { },
  949. };
  950. MODULE_DEVICE_TABLE(spi, mcp23s08_ids);
  951. static struct spi_driver mcp23s08_driver = {
  952. .probe = mcp23s08_probe,
  953. .id_table = mcp23s08_ids,
  954. .driver = {
  955. .name = "mcp23s08",
  956. .of_match_table = of_match_ptr(mcp23s08_spi_of_match),
  957. },
  958. };
  959. static int __init mcp23s08_spi_init(void)
  960. {
  961. return spi_register_driver(&mcp23s08_driver);
  962. }
  963. static void mcp23s08_spi_exit(void)
  964. {
  965. spi_unregister_driver(&mcp23s08_driver);
  966. }
  967. #else
  968. static int __init mcp23s08_spi_init(void) { return 0; }
  969. static void mcp23s08_spi_exit(void) { }
  970. #endif /* CONFIG_SPI_MASTER */
  971. /*----------------------------------------------------------------------*/
  972. static int __init mcp23s08_init(void)
  973. {
  974. int ret;
  975. ret = mcp23s08_spi_init();
  976. if (ret)
  977. goto spi_fail;
  978. ret = mcp23s08_i2c_init();
  979. if (ret)
  980. goto i2c_fail;
  981. return 0;
  982. i2c_fail:
  983. mcp23s08_spi_exit();
  984. spi_fail:
  985. return ret;
  986. }
  987. /* register after spi/i2c postcore initcall and before
  988. * subsys initcalls that may rely on these GPIOs
  989. */
  990. subsys_initcall(mcp23s08_init);
  991. static void __exit mcp23s08_exit(void)
  992. {
  993. mcp23s08_spi_exit();
  994. mcp23s08_i2c_exit();
  995. }
  996. module_exit(mcp23s08_exit);
  997. MODULE_LICENSE("GPL");