pinctrl-mcp23s08.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  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 mcp23x17_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 mcp23x17_volatile_range = {
  115. .range_min = MCP_INTF << 1,
  116. .range_max = MCP_GPIO << 1,
  117. };
  118. static const struct regmap_access_table mcp23x17_volatile_table = {
  119. .yes_ranges = &mcp23x17_volatile_range,
  120. .n_yes_ranges = 1,
  121. };
  122. static const struct regmap_range mcp23x17_precious_range = {
  123. .range_min = MCP_INTCAP << 1,
  124. .range_max = MCP_GPIO << 1,
  125. };
  126. static const struct regmap_access_table mcp23x17_precious_table = {
  127. .yes_ranges = &mcp23x17_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 = &mcp23x17_volatile_table,
  136. .precious_table = &mcp23x17_precious_table,
  137. .reg_defaults = mcp23x17_defaults,
  138. .num_reg_defaults = ARRAY_SIZE(mcp23x17_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. mcp->cached_gpio = status;
  346. status = !!(status & (1 << offset));
  347. }
  348. mutex_unlock(&mcp->lock);
  349. return status;
  350. }
  351. static int __mcp23s08_set(struct mcp23s08 *mcp, unsigned mask, bool value)
  352. {
  353. return mcp_set_mask(mcp, MCP_OLAT, mask, value);
  354. }
  355. static void mcp23s08_set(struct gpio_chip *chip, unsigned offset, int value)
  356. {
  357. struct mcp23s08 *mcp = gpiochip_get_data(chip);
  358. unsigned mask = BIT(offset);
  359. mutex_lock(&mcp->lock);
  360. __mcp23s08_set(mcp, mask, !!value);
  361. mutex_unlock(&mcp->lock);
  362. }
  363. static int
  364. mcp23s08_direction_output(struct gpio_chip *chip, unsigned offset, int value)
  365. {
  366. struct mcp23s08 *mcp = gpiochip_get_data(chip);
  367. unsigned mask = BIT(offset);
  368. int status;
  369. mutex_lock(&mcp->lock);
  370. status = __mcp23s08_set(mcp, mask, value);
  371. if (status == 0) {
  372. status = mcp_set_mask(mcp, MCP_IODIR, mask, false);
  373. }
  374. mutex_unlock(&mcp->lock);
  375. return status;
  376. }
  377. /*----------------------------------------------------------------------*/
  378. static irqreturn_t mcp23s08_irq(int irq, void *data)
  379. {
  380. struct mcp23s08 *mcp = data;
  381. int intcap, intcon, intf, i, gpio, gpio_orig, intcap_mask, defval;
  382. unsigned int child_irq;
  383. bool intf_set, intcap_changed, gpio_bit_changed,
  384. defval_changed, gpio_set;
  385. mutex_lock(&mcp->lock);
  386. if (mcp_read(mcp, MCP_INTF, &intf) < 0) {
  387. mutex_unlock(&mcp->lock);
  388. return IRQ_HANDLED;
  389. }
  390. if (mcp_read(mcp, MCP_INTCAP, &intcap) < 0) {
  391. mutex_unlock(&mcp->lock);
  392. return IRQ_HANDLED;
  393. }
  394. if (mcp_read(mcp, MCP_INTCON, &intcon) < 0) {
  395. mutex_unlock(&mcp->lock);
  396. return IRQ_HANDLED;
  397. }
  398. if (mcp_read(mcp, MCP_DEFVAL, &defval) < 0) {
  399. mutex_unlock(&mcp->lock);
  400. return IRQ_HANDLED;
  401. }
  402. /* This clears the interrupt(configurable on S18) */
  403. if (mcp_read(mcp, MCP_GPIO, &gpio) < 0) {
  404. mutex_unlock(&mcp->lock);
  405. return IRQ_HANDLED;
  406. }
  407. gpio_orig = mcp->cached_gpio;
  408. mcp->cached_gpio = gpio;
  409. mutex_unlock(&mcp->lock);
  410. if (intf == 0) {
  411. /* There is no interrupt pending */
  412. return IRQ_HANDLED;
  413. }
  414. dev_dbg(mcp->chip.parent,
  415. "intcap 0x%04X intf 0x%04X gpio_orig 0x%04X gpio 0x%04X\n",
  416. intcap, intf, gpio_orig, gpio);
  417. for (i = 0; i < mcp->chip.ngpio; i++) {
  418. /* We must check all of the inputs on the chip,
  419. * otherwise we may not notice a change on >=2 pins.
  420. *
  421. * On at least the mcp23s17, INTCAP is only updated
  422. * one byte at a time(INTCAPA and INTCAPB are
  423. * not written to at the same time - only on a per-bank
  424. * basis).
  425. *
  426. * INTF only contains the single bit that caused the
  427. * interrupt per-bank. On the mcp23s17, there is
  428. * INTFA and INTFB. If two pins are changed on the A
  429. * side at the same time, INTF will only have one bit
  430. * set. If one pin on the A side and one pin on the B
  431. * side are changed at the same time, INTF will have
  432. * two bits set. Thus, INTF can't be the only check
  433. * to see if the input has changed.
  434. */
  435. intf_set = intf & BIT(i);
  436. if (i < 8 && intf_set)
  437. intcap_mask = 0x00FF;
  438. else if (i >= 8 && intf_set)
  439. intcap_mask = 0xFF00;
  440. else
  441. intcap_mask = 0x00;
  442. intcap_changed = (intcap_mask &
  443. (intcap & BIT(i))) !=
  444. (intcap_mask & (BIT(i) & gpio_orig));
  445. gpio_set = BIT(i) & gpio;
  446. gpio_bit_changed = (BIT(i) & gpio_orig) !=
  447. (BIT(i) & gpio);
  448. defval_changed = (BIT(i) & intcon) &&
  449. ((BIT(i) & gpio) !=
  450. (BIT(i) & defval));
  451. if (((gpio_bit_changed || intcap_changed) &&
  452. (BIT(i) & mcp->irq_rise) && gpio_set) ||
  453. ((gpio_bit_changed || intcap_changed) &&
  454. (BIT(i) & mcp->irq_fall) && !gpio_set) ||
  455. defval_changed) {
  456. child_irq = irq_find_mapping(mcp->chip.irqdomain, i);
  457. handle_nested_irq(child_irq);
  458. }
  459. }
  460. return IRQ_HANDLED;
  461. }
  462. static void mcp23s08_irq_mask(struct irq_data *data)
  463. {
  464. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  465. struct mcp23s08 *mcp = gpiochip_get_data(gc);
  466. unsigned int pos = data->hwirq;
  467. mcp_set_bit(mcp, MCP_GPINTEN, pos, false);
  468. }
  469. static void mcp23s08_irq_unmask(struct irq_data *data)
  470. {
  471. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  472. struct mcp23s08 *mcp = gpiochip_get_data(gc);
  473. unsigned int pos = data->hwirq;
  474. mcp_set_bit(mcp, MCP_GPINTEN, pos, true);
  475. }
  476. static int mcp23s08_irq_set_type(struct irq_data *data, unsigned int type)
  477. {
  478. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  479. struct mcp23s08 *mcp = gpiochip_get_data(gc);
  480. unsigned int pos = data->hwirq;
  481. int status = 0;
  482. if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
  483. mcp_set_bit(mcp, MCP_INTCON, pos, false);
  484. mcp->irq_rise |= BIT(pos);
  485. mcp->irq_fall |= BIT(pos);
  486. } else if (type & IRQ_TYPE_EDGE_RISING) {
  487. mcp_set_bit(mcp, MCP_INTCON, pos, false);
  488. mcp->irq_rise |= BIT(pos);
  489. mcp->irq_fall &= ~BIT(pos);
  490. } else if (type & IRQ_TYPE_EDGE_FALLING) {
  491. mcp_set_bit(mcp, MCP_INTCON, pos, false);
  492. mcp->irq_rise &= ~BIT(pos);
  493. mcp->irq_fall |= BIT(pos);
  494. } else if (type & IRQ_TYPE_LEVEL_HIGH) {
  495. mcp_set_bit(mcp, MCP_INTCON, pos, true);
  496. mcp_set_bit(mcp, MCP_DEFVAL, pos, false);
  497. } else if (type & IRQ_TYPE_LEVEL_LOW) {
  498. mcp_set_bit(mcp, MCP_INTCON, pos, true);
  499. mcp_set_bit(mcp, MCP_DEFVAL, pos, true);
  500. } else
  501. return -EINVAL;
  502. return status;
  503. }
  504. static void mcp23s08_irq_bus_lock(struct irq_data *data)
  505. {
  506. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  507. struct mcp23s08 *mcp = gpiochip_get_data(gc);
  508. mutex_lock(&mcp->lock);
  509. regcache_cache_only(mcp->regmap, true);
  510. }
  511. static void mcp23s08_irq_bus_unlock(struct irq_data *data)
  512. {
  513. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  514. struct mcp23s08 *mcp = gpiochip_get_data(gc);
  515. regcache_cache_only(mcp->regmap, false);
  516. regcache_sync(mcp->regmap);
  517. mutex_unlock(&mcp->lock);
  518. }
  519. static struct irq_chip mcp23s08_irq_chip = {
  520. .name = "gpio-mcp23xxx",
  521. .irq_mask = mcp23s08_irq_mask,
  522. .irq_unmask = mcp23s08_irq_unmask,
  523. .irq_set_type = mcp23s08_irq_set_type,
  524. .irq_bus_lock = mcp23s08_irq_bus_lock,
  525. .irq_bus_sync_unlock = mcp23s08_irq_bus_unlock,
  526. };
  527. static int mcp23s08_irq_setup(struct mcp23s08 *mcp)
  528. {
  529. struct gpio_chip *chip = &mcp->chip;
  530. int err;
  531. unsigned long irqflags = IRQF_ONESHOT | IRQF_SHARED;
  532. if (mcp->irq_active_high)
  533. irqflags |= IRQF_TRIGGER_HIGH;
  534. else
  535. irqflags |= IRQF_TRIGGER_LOW;
  536. err = devm_request_threaded_irq(chip->parent, mcp->irq, NULL,
  537. mcp23s08_irq,
  538. irqflags, dev_name(chip->parent), mcp);
  539. if (err != 0) {
  540. dev_err(chip->parent, "unable to request IRQ#%d: %d\n",
  541. mcp->irq, err);
  542. return err;
  543. }
  544. return 0;
  545. }
  546. static int mcp23s08_irqchip_setup(struct mcp23s08 *mcp)
  547. {
  548. struct gpio_chip *chip = &mcp->chip;
  549. int err;
  550. err = gpiochip_irqchip_add_nested(chip,
  551. &mcp23s08_irq_chip,
  552. 0,
  553. handle_simple_irq,
  554. IRQ_TYPE_NONE);
  555. if (err) {
  556. dev_err(chip->parent,
  557. "could not connect irqchip to gpiochip: %d\n", err);
  558. return err;
  559. }
  560. gpiochip_set_nested_irqchip(chip,
  561. &mcp23s08_irq_chip,
  562. mcp->irq);
  563. return 0;
  564. }
  565. /*----------------------------------------------------------------------*/
  566. #ifdef CONFIG_DEBUG_FS
  567. #include <linux/seq_file.h>
  568. /*
  569. * This compares the chip's registers with the register
  570. * cache and corrects any incorrectly set register. This
  571. * can be used to fix state for MCP23xxx, that temporary
  572. * lost its power supply.
  573. */
  574. #define MCP23S08_CONFIG_REGS 8
  575. static int __check_mcp23s08_reg_cache(struct mcp23s08 *mcp)
  576. {
  577. int cached[MCP23S08_CONFIG_REGS];
  578. int err = 0, i;
  579. /* read cached config registers */
  580. for (i = 0; i < MCP23S08_CONFIG_REGS; i++) {
  581. err = mcp_read(mcp, i, &cached[i]);
  582. if (err)
  583. goto out;
  584. }
  585. regcache_cache_bypass(mcp->regmap, true);
  586. for (i = 0; i < MCP23S08_CONFIG_REGS; i++) {
  587. int uncached;
  588. err = mcp_read(mcp, i, &uncached);
  589. if (err)
  590. goto out;
  591. if (uncached != cached[i]) {
  592. dev_err(mcp->dev, "restoring reg 0x%02x from 0x%04x to 0x%04x (power-loss?)\n",
  593. i, uncached, cached[i]);
  594. mcp_write(mcp, i, cached[i]);
  595. }
  596. }
  597. out:
  598. if (err)
  599. dev_err(mcp->dev, "read error: reg=%02x, err=%d", i, err);
  600. regcache_cache_bypass(mcp->regmap, false);
  601. return err;
  602. }
  603. /*
  604. * This shows more info than the generic gpio dump code:
  605. * pullups, deglitching, open drain drive.
  606. */
  607. static void mcp23s08_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  608. {
  609. struct mcp23s08 *mcp;
  610. char bank;
  611. int t;
  612. unsigned mask;
  613. int iodir, gpio, gppu;
  614. mcp = gpiochip_get_data(chip);
  615. /* NOTE: we only handle one bank for now ... */
  616. bank = '0' + ((mcp->addr >> 1) & 0x7);
  617. mutex_lock(&mcp->lock);
  618. t = __check_mcp23s08_reg_cache(mcp);
  619. if (t) {
  620. seq_printf(s, " I/O Error\n");
  621. goto done;
  622. }
  623. t = mcp_read(mcp, MCP_IODIR, &iodir);
  624. if (t) {
  625. seq_printf(s, " I/O Error\n");
  626. goto done;
  627. }
  628. t = mcp_read(mcp, MCP_GPIO, &gpio);
  629. if (t) {
  630. seq_printf(s, " I/O Error\n");
  631. goto done;
  632. }
  633. t = mcp_read(mcp, MCP_GPPU, &gppu);
  634. if (t) {
  635. seq_printf(s, " I/O Error\n");
  636. goto done;
  637. }
  638. for (t = 0, mask = BIT(0); t < chip->ngpio; t++, mask <<= 1) {
  639. const char *label;
  640. label = gpiochip_is_requested(chip, t);
  641. if (!label)
  642. continue;
  643. seq_printf(s, " gpio-%-3d P%c.%d (%-12s) %s %s %s",
  644. chip->base + t, bank, t, label,
  645. (iodir & mask) ? "in " : "out",
  646. (gpio & mask) ? "hi" : "lo",
  647. (gppu & mask) ? "up" : " ");
  648. /* NOTE: ignoring the irq-related registers */
  649. seq_puts(s, "\n");
  650. }
  651. done:
  652. mutex_unlock(&mcp->lock);
  653. }
  654. #else
  655. #define mcp23s08_dbg_show NULL
  656. #endif
  657. /*----------------------------------------------------------------------*/
  658. static int mcp23s08_probe_one(struct mcp23s08 *mcp, struct device *dev,
  659. void *data, unsigned addr, unsigned type,
  660. unsigned int base, int cs)
  661. {
  662. int status, ret;
  663. bool mirror = false;
  664. struct regmap_config *one_regmap_config = NULL;
  665. mutex_init(&mcp->lock);
  666. mcp->dev = dev;
  667. mcp->addr = addr;
  668. mcp->irq_active_high = false;
  669. mcp->chip.direction_input = mcp23s08_direction_input;
  670. mcp->chip.get = mcp23s08_get;
  671. mcp->chip.direction_output = mcp23s08_direction_output;
  672. mcp->chip.set = mcp23s08_set;
  673. mcp->chip.dbg_show = mcp23s08_dbg_show;
  674. #ifdef CONFIG_OF_GPIO
  675. mcp->chip.of_gpio_n_cells = 2;
  676. mcp->chip.of_node = dev->of_node;
  677. #endif
  678. switch (type) {
  679. #ifdef CONFIG_SPI_MASTER
  680. case MCP_TYPE_S08:
  681. case MCP_TYPE_S17:
  682. switch (type) {
  683. case MCP_TYPE_S08:
  684. one_regmap_config =
  685. devm_kmemdup(dev, &mcp23x08_regmap,
  686. sizeof(struct regmap_config), GFP_KERNEL);
  687. mcp->reg_shift = 0;
  688. mcp->chip.ngpio = 8;
  689. mcp->chip.label = "mcp23s08";
  690. break;
  691. case MCP_TYPE_S17:
  692. one_regmap_config =
  693. devm_kmemdup(dev, &mcp23x17_regmap,
  694. sizeof(struct regmap_config), GFP_KERNEL);
  695. mcp->reg_shift = 1;
  696. mcp->chip.ngpio = 16;
  697. mcp->chip.label = "mcp23s17";
  698. break;
  699. }
  700. if (!one_regmap_config)
  701. return -ENOMEM;
  702. one_regmap_config->name = devm_kasprintf(dev, GFP_KERNEL, "%d", (addr & ~0x40) >> 1);
  703. mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp,
  704. one_regmap_config);
  705. break;
  706. case MCP_TYPE_S18:
  707. if (!one_regmap_config)
  708. return -ENOMEM;
  709. mcp->regmap = devm_regmap_init(dev, &mcp23sxx_spi_regmap, mcp,
  710. &mcp23x17_regmap);
  711. mcp->reg_shift = 1;
  712. mcp->chip.ngpio = 16;
  713. mcp->chip.label = "mcp23s18";
  714. break;
  715. #endif /* CONFIG_SPI_MASTER */
  716. #if IS_ENABLED(CONFIG_I2C)
  717. case MCP_TYPE_008:
  718. mcp->regmap = devm_regmap_init_i2c(data, &mcp23x08_regmap);
  719. mcp->reg_shift = 0;
  720. mcp->chip.ngpio = 8;
  721. mcp->chip.label = "mcp23008";
  722. break;
  723. case MCP_TYPE_017:
  724. mcp->regmap = devm_regmap_init_i2c(data, &mcp23x17_regmap);
  725. mcp->reg_shift = 1;
  726. mcp->chip.ngpio = 16;
  727. mcp->chip.label = "mcp23017";
  728. break;
  729. #endif /* CONFIG_I2C */
  730. default:
  731. dev_err(dev, "invalid device type (%d)\n", type);
  732. return -EINVAL;
  733. }
  734. if (IS_ERR(mcp->regmap))
  735. return PTR_ERR(mcp->regmap);
  736. mcp->chip.base = base;
  737. mcp->chip.can_sleep = true;
  738. mcp->chip.parent = dev;
  739. mcp->chip.owner = THIS_MODULE;
  740. /* verify MCP_IOCON.SEQOP = 0, so sequential reads work,
  741. * and MCP_IOCON.HAEN = 1, so we work with all chips.
  742. */
  743. ret = mcp_read(mcp, MCP_IOCON, &status);
  744. if (ret < 0)
  745. goto fail;
  746. mcp->irq_controller =
  747. device_property_read_bool(dev, "interrupt-controller");
  748. if (mcp->irq && mcp->irq_controller) {
  749. mcp->irq_active_high =
  750. device_property_read_bool(dev,
  751. "microchip,irq-active-high");
  752. mirror = device_property_read_bool(dev, "microchip,irq-mirror");
  753. }
  754. if ((status & IOCON_SEQOP) || !(status & IOCON_HAEN) || mirror ||
  755. mcp->irq_active_high) {
  756. /* mcp23s17 has IOCON twice, make sure they are in sync */
  757. status &= ~(IOCON_SEQOP | (IOCON_SEQOP << 8));
  758. status |= IOCON_HAEN | (IOCON_HAEN << 8);
  759. if (mcp->irq_active_high)
  760. status |= IOCON_INTPOL | (IOCON_INTPOL << 8);
  761. else
  762. status &= ~(IOCON_INTPOL | (IOCON_INTPOL << 8));
  763. if (mirror)
  764. status |= IOCON_MIRROR | (IOCON_MIRROR << 8);
  765. if (type == MCP_TYPE_S18)
  766. status |= IOCON_INTCC | (IOCON_INTCC << 8);
  767. ret = mcp_write(mcp, MCP_IOCON, status);
  768. if (ret < 0)
  769. goto fail;
  770. }
  771. if (mcp->irq && mcp->irq_controller) {
  772. ret = mcp23s08_irqchip_setup(mcp);
  773. if (ret)
  774. goto fail;
  775. }
  776. ret = devm_gpiochip_add_data(dev, &mcp->chip, mcp);
  777. if (ret < 0)
  778. goto fail;
  779. mcp->pinctrl_desc.name = "mcp23xxx-pinctrl";
  780. mcp->pinctrl_desc.pctlops = &mcp_pinctrl_ops;
  781. mcp->pinctrl_desc.confops = &mcp_pinconf_ops;
  782. mcp->pinctrl_desc.npins = mcp->chip.ngpio;
  783. if (mcp->pinctrl_desc.npins == 8)
  784. mcp->pinctrl_desc.pins = mcp23x08_pins;
  785. else if (mcp->pinctrl_desc.npins == 16)
  786. mcp->pinctrl_desc.pins = mcp23x17_pins;
  787. mcp->pinctrl_desc.owner = THIS_MODULE;
  788. mcp->pctldev = devm_pinctrl_register(dev, &mcp->pinctrl_desc, mcp);
  789. if (IS_ERR(mcp->pctldev)) {
  790. ret = PTR_ERR(mcp->pctldev);
  791. goto fail;
  792. }
  793. if (mcp->irq)
  794. ret = mcp23s08_irq_setup(mcp);
  795. fail:
  796. if (ret < 0)
  797. dev_dbg(dev, "can't setup chip %d, --> %d\n", addr, ret);
  798. return ret;
  799. }
  800. /*----------------------------------------------------------------------*/
  801. #ifdef CONFIG_OF
  802. #ifdef CONFIG_SPI_MASTER
  803. static const struct of_device_id mcp23s08_spi_of_match[] = {
  804. {
  805. .compatible = "microchip,mcp23s08",
  806. .data = (void *) MCP_TYPE_S08,
  807. },
  808. {
  809. .compatible = "microchip,mcp23s17",
  810. .data = (void *) MCP_TYPE_S17,
  811. },
  812. {
  813. .compatible = "microchip,mcp23s18",
  814. .data = (void *) MCP_TYPE_S18,
  815. },
  816. /* NOTE: The use of the mcp prefix is deprecated and will be removed. */
  817. {
  818. .compatible = "mcp,mcp23s08",
  819. .data = (void *) MCP_TYPE_S08,
  820. },
  821. {
  822. .compatible = "mcp,mcp23s17",
  823. .data = (void *) MCP_TYPE_S17,
  824. },
  825. { },
  826. };
  827. MODULE_DEVICE_TABLE(of, mcp23s08_spi_of_match);
  828. #endif
  829. #if IS_ENABLED(CONFIG_I2C)
  830. static const struct of_device_id mcp23s08_i2c_of_match[] = {
  831. {
  832. .compatible = "microchip,mcp23008",
  833. .data = (void *) MCP_TYPE_008,
  834. },
  835. {
  836. .compatible = "microchip,mcp23017",
  837. .data = (void *) MCP_TYPE_017,
  838. },
  839. /* NOTE: The use of the mcp prefix is deprecated and will be removed. */
  840. {
  841. .compatible = "mcp,mcp23008",
  842. .data = (void *) MCP_TYPE_008,
  843. },
  844. {
  845. .compatible = "mcp,mcp23017",
  846. .data = (void *) MCP_TYPE_017,
  847. },
  848. { },
  849. };
  850. MODULE_DEVICE_TABLE(of, mcp23s08_i2c_of_match);
  851. #endif
  852. #endif /* CONFIG_OF */
  853. #if IS_ENABLED(CONFIG_I2C)
  854. static int mcp230xx_probe(struct i2c_client *client,
  855. const struct i2c_device_id *id)
  856. {
  857. struct mcp23s08_platform_data *pdata, local_pdata;
  858. struct mcp23s08 *mcp;
  859. int status;
  860. pdata = dev_get_platdata(&client->dev);
  861. if (!pdata) {
  862. pdata = &local_pdata;
  863. pdata->base = -1;
  864. }
  865. mcp = devm_kzalloc(&client->dev, sizeof(*mcp), GFP_KERNEL);
  866. if (!mcp)
  867. return -ENOMEM;
  868. mcp->irq = client->irq;
  869. status = mcp23s08_probe_one(mcp, &client->dev, client, client->addr,
  870. id->driver_data, pdata->base, 0);
  871. if (status)
  872. return status;
  873. i2c_set_clientdata(client, mcp);
  874. return 0;
  875. }
  876. static const struct i2c_device_id mcp230xx_id[] = {
  877. { "mcp23008", MCP_TYPE_008 },
  878. { "mcp23017", MCP_TYPE_017 },
  879. { },
  880. };
  881. MODULE_DEVICE_TABLE(i2c, mcp230xx_id);
  882. static struct i2c_driver mcp230xx_driver = {
  883. .driver = {
  884. .name = "mcp230xx",
  885. .of_match_table = of_match_ptr(mcp23s08_i2c_of_match),
  886. },
  887. .probe = mcp230xx_probe,
  888. .id_table = mcp230xx_id,
  889. };
  890. static int __init mcp23s08_i2c_init(void)
  891. {
  892. return i2c_add_driver(&mcp230xx_driver);
  893. }
  894. static void mcp23s08_i2c_exit(void)
  895. {
  896. i2c_del_driver(&mcp230xx_driver);
  897. }
  898. #else
  899. static int __init mcp23s08_i2c_init(void) { return 0; }
  900. static void mcp23s08_i2c_exit(void) { }
  901. #endif /* CONFIG_I2C */
  902. /*----------------------------------------------------------------------*/
  903. #ifdef CONFIG_SPI_MASTER
  904. static int mcp23s08_probe(struct spi_device *spi)
  905. {
  906. struct mcp23s08_platform_data *pdata, local_pdata;
  907. unsigned addr;
  908. int chips = 0;
  909. struct mcp23s08_driver_data *data;
  910. int status, type;
  911. unsigned ngpio = 0;
  912. const struct of_device_id *match;
  913. match = of_match_device(of_match_ptr(mcp23s08_spi_of_match), &spi->dev);
  914. if (match)
  915. type = (int)(uintptr_t)match->data;
  916. else
  917. type = spi_get_device_id(spi)->driver_data;
  918. pdata = dev_get_platdata(&spi->dev);
  919. if (!pdata) {
  920. pdata = &local_pdata;
  921. pdata->base = -1;
  922. status = device_property_read_u32(&spi->dev,
  923. "microchip,spi-present-mask", &pdata->spi_present_mask);
  924. if (status) {
  925. status = device_property_read_u32(&spi->dev,
  926. "mcp,spi-present-mask",
  927. &pdata->spi_present_mask);
  928. if (status) {
  929. dev_err(&spi->dev, "missing spi-present-mask");
  930. return -ENODEV;
  931. }
  932. }
  933. }
  934. if (!pdata->spi_present_mask || pdata->spi_present_mask > 0xff) {
  935. dev_err(&spi->dev, "invalid spi-present-mask");
  936. return -ENODEV;
  937. }
  938. for (addr = 0; addr < MCP_MAX_DEV_PER_CS; addr++) {
  939. if (pdata->spi_present_mask & BIT(addr))
  940. chips++;
  941. }
  942. if (!chips)
  943. return -ENODEV;
  944. data = devm_kzalloc(&spi->dev,
  945. sizeof(*data) + chips * sizeof(struct mcp23s08),
  946. GFP_KERNEL);
  947. if (!data)
  948. return -ENOMEM;
  949. spi_set_drvdata(spi, data);
  950. for (addr = 0; addr < MCP_MAX_DEV_PER_CS; addr++) {
  951. if (!(pdata->spi_present_mask & BIT(addr)))
  952. continue;
  953. chips--;
  954. data->mcp[addr] = &data->chip[chips];
  955. data->mcp[addr]->irq = spi->irq;
  956. status = mcp23s08_probe_one(data->mcp[addr], &spi->dev, spi,
  957. 0x40 | (addr << 1), type,
  958. pdata->base, addr);
  959. if (status < 0)
  960. return status;
  961. if (pdata->base != -1)
  962. pdata->base += data->mcp[addr]->chip.ngpio;
  963. ngpio += data->mcp[addr]->chip.ngpio;
  964. }
  965. data->ngpio = ngpio;
  966. return 0;
  967. }
  968. static const struct spi_device_id mcp23s08_ids[] = {
  969. { "mcp23s08", MCP_TYPE_S08 },
  970. { "mcp23s17", MCP_TYPE_S17 },
  971. { "mcp23s18", MCP_TYPE_S18 },
  972. { },
  973. };
  974. MODULE_DEVICE_TABLE(spi, mcp23s08_ids);
  975. static struct spi_driver mcp23s08_driver = {
  976. .probe = mcp23s08_probe,
  977. .id_table = mcp23s08_ids,
  978. .driver = {
  979. .name = "mcp23s08",
  980. .of_match_table = of_match_ptr(mcp23s08_spi_of_match),
  981. },
  982. };
  983. static int __init mcp23s08_spi_init(void)
  984. {
  985. return spi_register_driver(&mcp23s08_driver);
  986. }
  987. static void mcp23s08_spi_exit(void)
  988. {
  989. spi_unregister_driver(&mcp23s08_driver);
  990. }
  991. #else
  992. static int __init mcp23s08_spi_init(void) { return 0; }
  993. static void mcp23s08_spi_exit(void) { }
  994. #endif /* CONFIG_SPI_MASTER */
  995. /*----------------------------------------------------------------------*/
  996. static int __init mcp23s08_init(void)
  997. {
  998. int ret;
  999. ret = mcp23s08_spi_init();
  1000. if (ret)
  1001. goto spi_fail;
  1002. ret = mcp23s08_i2c_init();
  1003. if (ret)
  1004. goto i2c_fail;
  1005. return 0;
  1006. i2c_fail:
  1007. mcp23s08_spi_exit();
  1008. spi_fail:
  1009. return ret;
  1010. }
  1011. /* register after spi/i2c postcore initcall and before
  1012. * subsys initcalls that may rely on these GPIOs
  1013. */
  1014. subsys_initcall(mcp23s08_init);
  1015. static void __exit mcp23s08_exit(void)
  1016. {
  1017. mcp23s08_spi_exit();
  1018. mcp23s08_i2c_exit();
  1019. }
  1020. module_exit(mcp23s08_exit);
  1021. MODULE_LICENSE("GPL");