pinctrl-mcp23s08.c 29 KB

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