gpio-mcp23s08.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. /*
  2. * MCP23S08 SPI/I2C GPIO gpio expander driver
  3. *
  4. * The inputs and outputs of the mcp23s08, mcp23s17, mcp23008 and mcp23017 are
  5. * supported.
  6. * For the I2C versions of the chips (mcp23008 and mcp23017) generation of
  7. * interrupts is also supported.
  8. * The hardware of the SPI versions of the chips (mcp23s08 and mcp23s17) is
  9. * also capable of generating interrupts, but the linux driver does not
  10. * support that yet.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/device.h>
  14. #include <linux/mutex.h>
  15. #include <linux/module.h>
  16. #include <linux/gpio.h>
  17. #include <linux/i2c.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/spi/mcp23s08.h>
  20. #include <linux/slab.h>
  21. #include <asm/byteorder.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/of_irq.h>
  24. #include <linux/of_device.h>
  25. /**
  26. * MCP types supported by driver
  27. */
  28. #define MCP_TYPE_S08 0
  29. #define MCP_TYPE_S17 1
  30. #define MCP_TYPE_008 2
  31. #define MCP_TYPE_017 3
  32. /* Registers are all 8 bits wide.
  33. *
  34. * The mcp23s17 has twice as many bits, and can be configured to work
  35. * with either 16 bit registers or with two adjacent 8 bit banks.
  36. */
  37. #define MCP_IODIR 0x00 /* init/reset: all ones */
  38. #define MCP_IPOL 0x01
  39. #define MCP_GPINTEN 0x02
  40. #define MCP_DEFVAL 0x03
  41. #define MCP_INTCON 0x04
  42. #define MCP_IOCON 0x05
  43. # define IOCON_MIRROR (1 << 6)
  44. # define IOCON_SEQOP (1 << 5)
  45. # define IOCON_HAEN (1 << 3)
  46. # define IOCON_ODR (1 << 2)
  47. # define IOCON_INTPOL (1 << 1)
  48. #define MCP_GPPU 0x06
  49. #define MCP_INTF 0x07
  50. #define MCP_INTCAP 0x08
  51. #define MCP_GPIO 0x09
  52. #define MCP_OLAT 0x0a
  53. struct mcp23s08;
  54. struct mcp23s08_ops {
  55. int (*read)(struct mcp23s08 *mcp, unsigned reg);
  56. int (*write)(struct mcp23s08 *mcp, unsigned reg, unsigned val);
  57. int (*read_regs)(struct mcp23s08 *mcp, unsigned reg,
  58. u16 *vals, unsigned n);
  59. };
  60. struct mcp23s08 {
  61. u8 addr;
  62. bool irq_active_high;
  63. u16 cache[11];
  64. u16 irq_rise;
  65. u16 irq_fall;
  66. int irq;
  67. bool irq_controller;
  68. /* lock protects the cached values */
  69. struct mutex lock;
  70. struct mutex irq_lock;
  71. struct irq_domain *irq_domain;
  72. struct gpio_chip chip;
  73. const struct mcp23s08_ops *ops;
  74. void *data; /* ops specific data */
  75. };
  76. /* A given spi_device can represent up to eight mcp23sxx chips
  77. * sharing the same chipselect but using different addresses
  78. * (e.g. chips #0 and #3 might be populated, but not #1 or $2).
  79. * Driver data holds all the per-chip data.
  80. */
  81. struct mcp23s08_driver_data {
  82. unsigned ngpio;
  83. struct mcp23s08 *mcp[8];
  84. struct mcp23s08 chip[];
  85. };
  86. /* This lock class tells lockdep that GPIO irqs are in a different
  87. * category than their parents, so it won't report false recursion.
  88. */
  89. static struct lock_class_key gpio_lock_class;
  90. /*----------------------------------------------------------------------*/
  91. #if IS_ENABLED(CONFIG_I2C)
  92. static int mcp23008_read(struct mcp23s08 *mcp, unsigned reg)
  93. {
  94. return i2c_smbus_read_byte_data(mcp->data, reg);
  95. }
  96. static int mcp23008_write(struct mcp23s08 *mcp, unsigned reg, unsigned val)
  97. {
  98. return i2c_smbus_write_byte_data(mcp->data, reg, val);
  99. }
  100. static int
  101. mcp23008_read_regs(struct mcp23s08 *mcp, unsigned reg, u16 *vals, unsigned n)
  102. {
  103. while (n--) {
  104. int ret = mcp23008_read(mcp, reg++);
  105. if (ret < 0)
  106. return ret;
  107. *vals++ = ret;
  108. }
  109. return 0;
  110. }
  111. static int mcp23017_read(struct mcp23s08 *mcp, unsigned reg)
  112. {
  113. return i2c_smbus_read_word_data(mcp->data, reg << 1);
  114. }
  115. static int mcp23017_write(struct mcp23s08 *mcp, unsigned reg, unsigned val)
  116. {
  117. return i2c_smbus_write_word_data(mcp->data, reg << 1, val);
  118. }
  119. static int
  120. mcp23017_read_regs(struct mcp23s08 *mcp, unsigned reg, u16 *vals, unsigned n)
  121. {
  122. while (n--) {
  123. int ret = mcp23017_read(mcp, reg++);
  124. if (ret < 0)
  125. return ret;
  126. *vals++ = ret;
  127. }
  128. return 0;
  129. }
  130. static const struct mcp23s08_ops mcp23008_ops = {
  131. .read = mcp23008_read,
  132. .write = mcp23008_write,
  133. .read_regs = mcp23008_read_regs,
  134. };
  135. static const struct mcp23s08_ops mcp23017_ops = {
  136. .read = mcp23017_read,
  137. .write = mcp23017_write,
  138. .read_regs = mcp23017_read_regs,
  139. };
  140. #endif /* CONFIG_I2C */
  141. /*----------------------------------------------------------------------*/
  142. #ifdef CONFIG_SPI_MASTER
  143. static int mcp23s08_read(struct mcp23s08 *mcp, unsigned reg)
  144. {
  145. u8 tx[2], rx[1];
  146. int status;
  147. tx[0] = mcp->addr | 0x01;
  148. tx[1] = reg;
  149. status = spi_write_then_read(mcp->data, tx, sizeof(tx), rx, sizeof(rx));
  150. return (status < 0) ? status : rx[0];
  151. }
  152. static int mcp23s08_write(struct mcp23s08 *mcp, unsigned reg, unsigned val)
  153. {
  154. u8 tx[3];
  155. tx[0] = mcp->addr;
  156. tx[1] = reg;
  157. tx[2] = val;
  158. return spi_write_then_read(mcp->data, tx, sizeof(tx), NULL, 0);
  159. }
  160. static int
  161. mcp23s08_read_regs(struct mcp23s08 *mcp, unsigned reg, u16 *vals, unsigned n)
  162. {
  163. u8 tx[2], *tmp;
  164. int status;
  165. if ((n + reg) > sizeof(mcp->cache))
  166. return -EINVAL;
  167. tx[0] = mcp->addr | 0x01;
  168. tx[1] = reg;
  169. tmp = (u8 *)vals;
  170. status = spi_write_then_read(mcp->data, tx, sizeof(tx), tmp, n);
  171. if (status >= 0) {
  172. while (n--)
  173. vals[n] = tmp[n]; /* expand to 16bit */
  174. }
  175. return status;
  176. }
  177. static int mcp23s17_read(struct mcp23s08 *mcp, unsigned reg)
  178. {
  179. u8 tx[2], rx[2];
  180. int status;
  181. tx[0] = mcp->addr | 0x01;
  182. tx[1] = reg << 1;
  183. status = spi_write_then_read(mcp->data, tx, sizeof(tx), rx, sizeof(rx));
  184. return (status < 0) ? status : (rx[0] | (rx[1] << 8));
  185. }
  186. static int mcp23s17_write(struct mcp23s08 *mcp, unsigned reg, unsigned val)
  187. {
  188. u8 tx[4];
  189. tx[0] = mcp->addr;
  190. tx[1] = reg << 1;
  191. tx[2] = val;
  192. tx[3] = val >> 8;
  193. return spi_write_then_read(mcp->data, tx, sizeof(tx), NULL, 0);
  194. }
  195. static int
  196. mcp23s17_read_regs(struct mcp23s08 *mcp, unsigned reg, u16 *vals, unsigned n)
  197. {
  198. u8 tx[2];
  199. int status;
  200. if ((n + reg) > sizeof(mcp->cache))
  201. return -EINVAL;
  202. tx[0] = mcp->addr | 0x01;
  203. tx[1] = reg << 1;
  204. status = spi_write_then_read(mcp->data, tx, sizeof(tx),
  205. (u8 *)vals, n * 2);
  206. if (status >= 0) {
  207. while (n--)
  208. vals[n] = __le16_to_cpu((__le16)vals[n]);
  209. }
  210. return status;
  211. }
  212. static const struct mcp23s08_ops mcp23s08_ops = {
  213. .read = mcp23s08_read,
  214. .write = mcp23s08_write,
  215. .read_regs = mcp23s08_read_regs,
  216. };
  217. static const struct mcp23s08_ops mcp23s17_ops = {
  218. .read = mcp23s17_read,
  219. .write = mcp23s17_write,
  220. .read_regs = mcp23s17_read_regs,
  221. };
  222. #endif /* CONFIG_SPI_MASTER */
  223. /*----------------------------------------------------------------------*/
  224. static int mcp23s08_direction_input(struct gpio_chip *chip, unsigned offset)
  225. {
  226. struct mcp23s08 *mcp = container_of(chip, struct mcp23s08, chip);
  227. int status;
  228. mutex_lock(&mcp->lock);
  229. mcp->cache[MCP_IODIR] |= (1 << offset);
  230. status = mcp->ops->write(mcp, MCP_IODIR, mcp->cache[MCP_IODIR]);
  231. mutex_unlock(&mcp->lock);
  232. return status;
  233. }
  234. static int mcp23s08_get(struct gpio_chip *chip, unsigned offset)
  235. {
  236. struct mcp23s08 *mcp = container_of(chip, struct mcp23s08, chip);
  237. int status;
  238. mutex_lock(&mcp->lock);
  239. /* REVISIT reading this clears any IRQ ... */
  240. status = mcp->ops->read(mcp, MCP_GPIO);
  241. if (status < 0)
  242. status = 0;
  243. else {
  244. mcp->cache[MCP_GPIO] = status;
  245. status = !!(status & (1 << offset));
  246. }
  247. mutex_unlock(&mcp->lock);
  248. return status;
  249. }
  250. static int __mcp23s08_set(struct mcp23s08 *mcp, unsigned mask, int value)
  251. {
  252. unsigned olat = mcp->cache[MCP_OLAT];
  253. if (value)
  254. olat |= mask;
  255. else
  256. olat &= ~mask;
  257. mcp->cache[MCP_OLAT] = olat;
  258. return mcp->ops->write(mcp, MCP_OLAT, olat);
  259. }
  260. static void mcp23s08_set(struct gpio_chip *chip, unsigned offset, int value)
  261. {
  262. struct mcp23s08 *mcp = container_of(chip, struct mcp23s08, chip);
  263. unsigned mask = 1 << offset;
  264. mutex_lock(&mcp->lock);
  265. __mcp23s08_set(mcp, mask, value);
  266. mutex_unlock(&mcp->lock);
  267. }
  268. static int
  269. mcp23s08_direction_output(struct gpio_chip *chip, unsigned offset, int value)
  270. {
  271. struct mcp23s08 *mcp = container_of(chip, struct mcp23s08, chip);
  272. unsigned mask = 1 << offset;
  273. int status;
  274. mutex_lock(&mcp->lock);
  275. status = __mcp23s08_set(mcp, mask, value);
  276. if (status == 0) {
  277. mcp->cache[MCP_IODIR] &= ~mask;
  278. status = mcp->ops->write(mcp, MCP_IODIR, mcp->cache[MCP_IODIR]);
  279. }
  280. mutex_unlock(&mcp->lock);
  281. return status;
  282. }
  283. /*----------------------------------------------------------------------*/
  284. static irqreturn_t mcp23s08_irq(int irq, void *data)
  285. {
  286. struct mcp23s08 *mcp = data;
  287. int intcap, intf, i;
  288. unsigned int child_irq;
  289. mutex_lock(&mcp->lock);
  290. intf = mcp->ops->read(mcp, MCP_INTF);
  291. if (intf < 0) {
  292. mutex_unlock(&mcp->lock);
  293. return IRQ_HANDLED;
  294. }
  295. mcp->cache[MCP_INTF] = intf;
  296. intcap = mcp->ops->read(mcp, MCP_INTCAP);
  297. if (intcap < 0) {
  298. mutex_unlock(&mcp->lock);
  299. return IRQ_HANDLED;
  300. }
  301. mcp->cache[MCP_INTCAP] = intcap;
  302. mutex_unlock(&mcp->lock);
  303. for (i = 0; i < mcp->chip.ngpio; i++) {
  304. if ((BIT(i) & mcp->cache[MCP_INTF]) &&
  305. ((BIT(i) & intcap & mcp->irq_rise) ||
  306. (mcp->irq_fall & ~intcap & BIT(i)))) {
  307. child_irq = irq_find_mapping(mcp->irq_domain, i);
  308. handle_nested_irq(child_irq);
  309. }
  310. }
  311. return IRQ_HANDLED;
  312. }
  313. static int mcp23s08_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  314. {
  315. struct mcp23s08 *mcp = container_of(chip, struct mcp23s08, chip);
  316. return irq_find_mapping(mcp->irq_domain, offset);
  317. }
  318. static void mcp23s08_irq_mask(struct irq_data *data)
  319. {
  320. struct mcp23s08 *mcp = irq_data_get_irq_chip_data(data);
  321. unsigned int pos = data->hwirq;
  322. mcp->cache[MCP_GPINTEN] &= ~BIT(pos);
  323. }
  324. static void mcp23s08_irq_unmask(struct irq_data *data)
  325. {
  326. struct mcp23s08 *mcp = irq_data_get_irq_chip_data(data);
  327. unsigned int pos = data->hwirq;
  328. mcp->cache[MCP_GPINTEN] |= BIT(pos);
  329. }
  330. static int mcp23s08_irq_set_type(struct irq_data *data, unsigned int type)
  331. {
  332. struct mcp23s08 *mcp = irq_data_get_irq_chip_data(data);
  333. unsigned int pos = data->hwirq;
  334. int status = 0;
  335. if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
  336. mcp->cache[MCP_INTCON] &= ~BIT(pos);
  337. mcp->irq_rise |= BIT(pos);
  338. mcp->irq_fall |= BIT(pos);
  339. } else if (type & IRQ_TYPE_EDGE_RISING) {
  340. mcp->cache[MCP_INTCON] &= ~BIT(pos);
  341. mcp->irq_rise |= BIT(pos);
  342. mcp->irq_fall &= ~BIT(pos);
  343. } else if (type & IRQ_TYPE_EDGE_FALLING) {
  344. mcp->cache[MCP_INTCON] &= ~BIT(pos);
  345. mcp->irq_rise &= ~BIT(pos);
  346. mcp->irq_fall |= BIT(pos);
  347. } else
  348. return -EINVAL;
  349. return status;
  350. }
  351. static void mcp23s08_irq_bus_lock(struct irq_data *data)
  352. {
  353. struct mcp23s08 *mcp = irq_data_get_irq_chip_data(data);
  354. mutex_lock(&mcp->irq_lock);
  355. }
  356. static void mcp23s08_irq_bus_unlock(struct irq_data *data)
  357. {
  358. struct mcp23s08 *mcp = irq_data_get_irq_chip_data(data);
  359. mutex_lock(&mcp->lock);
  360. mcp->ops->write(mcp, MCP_GPINTEN, mcp->cache[MCP_GPINTEN]);
  361. mcp->ops->write(mcp, MCP_DEFVAL, mcp->cache[MCP_DEFVAL]);
  362. mcp->ops->write(mcp, MCP_INTCON, mcp->cache[MCP_INTCON]);
  363. mutex_unlock(&mcp->lock);
  364. mutex_unlock(&mcp->irq_lock);
  365. }
  366. static int mcp23s08_irq_reqres(struct irq_data *data)
  367. {
  368. struct mcp23s08 *mcp = irq_data_get_irq_chip_data(data);
  369. if (gpiochip_lock_as_irq(&mcp->chip, data->hwirq)) {
  370. dev_err(mcp->chip.dev,
  371. "unable to lock HW IRQ %lu for IRQ usage\n",
  372. data->hwirq);
  373. return -EINVAL;
  374. }
  375. return 0;
  376. }
  377. static void mcp23s08_irq_relres(struct irq_data *data)
  378. {
  379. struct mcp23s08 *mcp = irq_data_get_irq_chip_data(data);
  380. gpiochip_unlock_as_irq(&mcp->chip, data->hwirq);
  381. }
  382. static struct irq_chip mcp23s08_irq_chip = {
  383. .name = "gpio-mcp23xxx",
  384. .irq_mask = mcp23s08_irq_mask,
  385. .irq_unmask = mcp23s08_irq_unmask,
  386. .irq_set_type = mcp23s08_irq_set_type,
  387. .irq_bus_lock = mcp23s08_irq_bus_lock,
  388. .irq_bus_sync_unlock = mcp23s08_irq_bus_unlock,
  389. .irq_request_resources = mcp23s08_irq_reqres,
  390. .irq_release_resources = mcp23s08_irq_relres,
  391. };
  392. static int mcp23s08_irq_setup(struct mcp23s08 *mcp)
  393. {
  394. struct gpio_chip *chip = &mcp->chip;
  395. int err, irq, j;
  396. unsigned long irqflags = IRQF_ONESHOT | IRQF_SHARED;
  397. mutex_init(&mcp->irq_lock);
  398. mcp->irq_domain = irq_domain_add_linear(chip->dev->of_node, chip->ngpio,
  399. &irq_domain_simple_ops, mcp);
  400. if (!mcp->irq_domain)
  401. return -ENODEV;
  402. if (mcp->irq_active_high)
  403. irqflags |= IRQF_TRIGGER_HIGH;
  404. else
  405. irqflags |= IRQF_TRIGGER_LOW;
  406. err = devm_request_threaded_irq(chip->dev, mcp->irq, NULL, mcp23s08_irq,
  407. irqflags, dev_name(chip->dev), mcp);
  408. if (err != 0) {
  409. dev_err(chip->dev, "unable to request IRQ#%d: %d\n",
  410. mcp->irq, err);
  411. return err;
  412. }
  413. chip->to_irq = mcp23s08_gpio_to_irq;
  414. for (j = 0; j < mcp->chip.ngpio; j++) {
  415. irq = irq_create_mapping(mcp->irq_domain, j);
  416. irq_set_lockdep_class(irq, &gpio_lock_class);
  417. irq_set_chip_data(irq, mcp);
  418. irq_set_chip(irq, &mcp23s08_irq_chip);
  419. irq_set_nested_thread(irq, true);
  420. #ifdef CONFIG_ARM
  421. set_irq_flags(irq, IRQF_VALID);
  422. #else
  423. irq_set_noprobe(irq);
  424. #endif
  425. }
  426. return 0;
  427. }
  428. static void mcp23s08_irq_teardown(struct mcp23s08 *mcp)
  429. {
  430. unsigned int irq, i;
  431. for (i = 0; i < mcp->chip.ngpio; i++) {
  432. irq = irq_find_mapping(mcp->irq_domain, i);
  433. if (irq > 0)
  434. irq_dispose_mapping(irq);
  435. }
  436. irq_domain_remove(mcp->irq_domain);
  437. }
  438. /*----------------------------------------------------------------------*/
  439. #ifdef CONFIG_DEBUG_FS
  440. #include <linux/seq_file.h>
  441. /*
  442. * This shows more info than the generic gpio dump code:
  443. * pullups, deglitching, open drain drive.
  444. */
  445. static void mcp23s08_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  446. {
  447. struct mcp23s08 *mcp;
  448. char bank;
  449. int t;
  450. unsigned mask;
  451. mcp = container_of(chip, struct mcp23s08, chip);
  452. /* NOTE: we only handle one bank for now ... */
  453. bank = '0' + ((mcp->addr >> 1) & 0x7);
  454. mutex_lock(&mcp->lock);
  455. t = mcp->ops->read_regs(mcp, 0, mcp->cache, ARRAY_SIZE(mcp->cache));
  456. if (t < 0) {
  457. seq_printf(s, " I/O ERROR %d\n", t);
  458. goto done;
  459. }
  460. for (t = 0, mask = 1; t < chip->ngpio; t++, mask <<= 1) {
  461. const char *label;
  462. label = gpiochip_is_requested(chip, t);
  463. if (!label)
  464. continue;
  465. seq_printf(s, " gpio-%-3d P%c.%d (%-12s) %s %s %s",
  466. chip->base + t, bank, t, label,
  467. (mcp->cache[MCP_IODIR] & mask) ? "in " : "out",
  468. (mcp->cache[MCP_GPIO] & mask) ? "hi" : "lo",
  469. (mcp->cache[MCP_GPPU] & mask) ? "up" : " ");
  470. /* NOTE: ignoring the irq-related registers */
  471. seq_puts(s, "\n");
  472. }
  473. done:
  474. mutex_unlock(&mcp->lock);
  475. }
  476. #else
  477. #define mcp23s08_dbg_show NULL
  478. #endif
  479. /*----------------------------------------------------------------------*/
  480. static int mcp23s08_probe_one(struct mcp23s08 *mcp, struct device *dev,
  481. void *data, unsigned addr, unsigned type,
  482. struct mcp23s08_platform_data *pdata, int cs)
  483. {
  484. int status;
  485. bool mirror = false;
  486. mutex_init(&mcp->lock);
  487. mcp->data = data;
  488. mcp->addr = addr;
  489. mcp->irq_active_high = false;
  490. mcp->chip.direction_input = mcp23s08_direction_input;
  491. mcp->chip.get = mcp23s08_get;
  492. mcp->chip.direction_output = mcp23s08_direction_output;
  493. mcp->chip.set = mcp23s08_set;
  494. mcp->chip.dbg_show = mcp23s08_dbg_show;
  495. #ifdef CONFIG_OF
  496. mcp->chip.of_gpio_n_cells = 2;
  497. mcp->chip.of_node = dev->of_node;
  498. #endif
  499. switch (type) {
  500. #ifdef CONFIG_SPI_MASTER
  501. case MCP_TYPE_S08:
  502. mcp->ops = &mcp23s08_ops;
  503. mcp->chip.ngpio = 8;
  504. mcp->chip.label = "mcp23s08";
  505. break;
  506. case MCP_TYPE_S17:
  507. mcp->ops = &mcp23s17_ops;
  508. mcp->chip.ngpio = 16;
  509. mcp->chip.label = "mcp23s17";
  510. break;
  511. #endif /* CONFIG_SPI_MASTER */
  512. #if IS_ENABLED(CONFIG_I2C)
  513. case MCP_TYPE_008:
  514. mcp->ops = &mcp23008_ops;
  515. mcp->chip.ngpio = 8;
  516. mcp->chip.label = "mcp23008";
  517. break;
  518. case MCP_TYPE_017:
  519. mcp->ops = &mcp23017_ops;
  520. mcp->chip.ngpio = 16;
  521. mcp->chip.label = "mcp23017";
  522. break;
  523. #endif /* CONFIG_I2C */
  524. default:
  525. dev_err(dev, "invalid device type (%d)\n", type);
  526. return -EINVAL;
  527. }
  528. mcp->chip.base = pdata->base;
  529. mcp->chip.can_sleep = true;
  530. mcp->chip.dev = dev;
  531. mcp->chip.owner = THIS_MODULE;
  532. /* verify MCP_IOCON.SEQOP = 0, so sequential reads work,
  533. * and MCP_IOCON.HAEN = 1, so we work with all chips.
  534. */
  535. status = mcp->ops->read(mcp, MCP_IOCON);
  536. if (status < 0)
  537. goto fail;
  538. mcp->irq_controller = pdata->irq_controller;
  539. if (mcp->irq && mcp->irq_controller) {
  540. mcp->irq_active_high =
  541. of_property_read_bool(mcp->chip.dev->of_node,
  542. "microchip,irq-active-high");
  543. if (type == MCP_TYPE_017)
  544. mirror = pdata->mirror;
  545. }
  546. if ((status & IOCON_SEQOP) || !(status & IOCON_HAEN) || mirror ||
  547. mcp->irq_active_high) {
  548. /* mcp23s17 has IOCON twice, make sure they are in sync */
  549. status &= ~(IOCON_SEQOP | (IOCON_SEQOP << 8));
  550. status |= IOCON_HAEN | (IOCON_HAEN << 8);
  551. if (mcp->irq_active_high)
  552. status |= IOCON_INTPOL | (IOCON_INTPOL << 8);
  553. else
  554. status &= ~(IOCON_INTPOL | (IOCON_INTPOL << 8));
  555. if (mirror)
  556. status |= IOCON_MIRROR | (IOCON_MIRROR << 8);
  557. status = mcp->ops->write(mcp, MCP_IOCON, status);
  558. if (status < 0)
  559. goto fail;
  560. }
  561. /* configure ~100K pullups */
  562. status = mcp->ops->write(mcp, MCP_GPPU, pdata->chip[cs].pullups);
  563. if (status < 0)
  564. goto fail;
  565. status = mcp->ops->read_regs(mcp, 0, mcp->cache, ARRAY_SIZE(mcp->cache));
  566. if (status < 0)
  567. goto fail;
  568. /* disable inverter on input */
  569. if (mcp->cache[MCP_IPOL] != 0) {
  570. mcp->cache[MCP_IPOL] = 0;
  571. status = mcp->ops->write(mcp, MCP_IPOL, 0);
  572. if (status < 0)
  573. goto fail;
  574. }
  575. /* disable irqs */
  576. if (mcp->cache[MCP_GPINTEN] != 0) {
  577. mcp->cache[MCP_GPINTEN] = 0;
  578. status = mcp->ops->write(mcp, MCP_GPINTEN, 0);
  579. if (status < 0)
  580. goto fail;
  581. }
  582. status = gpiochip_add(&mcp->chip);
  583. if (status < 0)
  584. goto fail;
  585. if (mcp->irq && mcp->irq_controller) {
  586. status = mcp23s08_irq_setup(mcp);
  587. if (status) {
  588. mcp23s08_irq_teardown(mcp);
  589. goto fail;
  590. }
  591. }
  592. fail:
  593. if (status < 0)
  594. dev_dbg(dev, "can't setup chip %d, --> %d\n",
  595. addr, status);
  596. return status;
  597. }
  598. /*----------------------------------------------------------------------*/
  599. #ifdef CONFIG_OF
  600. #ifdef CONFIG_SPI_MASTER
  601. static const struct of_device_id mcp23s08_spi_of_match[] = {
  602. {
  603. .compatible = "microchip,mcp23s08",
  604. .data = (void *) MCP_TYPE_S08,
  605. },
  606. {
  607. .compatible = "microchip,mcp23s17",
  608. .data = (void *) MCP_TYPE_S17,
  609. },
  610. /* NOTE: The use of the mcp prefix is deprecated and will be removed. */
  611. {
  612. .compatible = "mcp,mcp23s08",
  613. .data = (void *) MCP_TYPE_S08,
  614. },
  615. {
  616. .compatible = "mcp,mcp23s17",
  617. .data = (void *) MCP_TYPE_S17,
  618. },
  619. { },
  620. };
  621. MODULE_DEVICE_TABLE(of, mcp23s08_spi_of_match);
  622. #endif
  623. #if IS_ENABLED(CONFIG_I2C)
  624. static const struct of_device_id mcp23s08_i2c_of_match[] = {
  625. {
  626. .compatible = "microchip,mcp23008",
  627. .data = (void *) MCP_TYPE_008,
  628. },
  629. {
  630. .compatible = "microchip,mcp23017",
  631. .data = (void *) MCP_TYPE_017,
  632. },
  633. /* NOTE: The use of the mcp prefix is deprecated and will be removed. */
  634. {
  635. .compatible = "mcp,mcp23008",
  636. .data = (void *) MCP_TYPE_008,
  637. },
  638. {
  639. .compatible = "mcp,mcp23017",
  640. .data = (void *) MCP_TYPE_017,
  641. },
  642. { },
  643. };
  644. MODULE_DEVICE_TABLE(of, mcp23s08_i2c_of_match);
  645. #endif
  646. #endif /* CONFIG_OF */
  647. #if IS_ENABLED(CONFIG_I2C)
  648. static int mcp230xx_probe(struct i2c_client *client,
  649. const struct i2c_device_id *id)
  650. {
  651. struct mcp23s08_platform_data *pdata, local_pdata;
  652. struct mcp23s08 *mcp;
  653. int status;
  654. const struct of_device_id *match;
  655. match = of_match_device(of_match_ptr(mcp23s08_i2c_of_match),
  656. &client->dev);
  657. if (match) {
  658. pdata = &local_pdata;
  659. pdata->base = -1;
  660. pdata->chip[0].pullups = 0;
  661. pdata->irq_controller = of_property_read_bool(
  662. client->dev.of_node,
  663. "interrupt-controller");
  664. pdata->mirror = of_property_read_bool(client->dev.of_node,
  665. "microchip,irq-mirror");
  666. client->irq = irq_of_parse_and_map(client->dev.of_node, 0);
  667. } else {
  668. pdata = dev_get_platdata(&client->dev);
  669. if (!pdata || !gpio_is_valid(pdata->base)) {
  670. dev_dbg(&client->dev, "invalid platform data\n");
  671. return -EINVAL;
  672. }
  673. }
  674. mcp = kzalloc(sizeof(*mcp), GFP_KERNEL);
  675. if (!mcp)
  676. return -ENOMEM;
  677. mcp->irq = client->irq;
  678. status = mcp23s08_probe_one(mcp, &client->dev, client, client->addr,
  679. id->driver_data, pdata, 0);
  680. if (status)
  681. goto fail;
  682. i2c_set_clientdata(client, mcp);
  683. return 0;
  684. fail:
  685. kfree(mcp);
  686. return status;
  687. }
  688. static int mcp230xx_remove(struct i2c_client *client)
  689. {
  690. struct mcp23s08 *mcp = i2c_get_clientdata(client);
  691. if (client->irq && mcp->irq_controller)
  692. mcp23s08_irq_teardown(mcp);
  693. gpiochip_remove(&mcp->chip);
  694. kfree(mcp);
  695. return 0;
  696. }
  697. static const struct i2c_device_id mcp230xx_id[] = {
  698. { "mcp23008", MCP_TYPE_008 },
  699. { "mcp23017", MCP_TYPE_017 },
  700. { },
  701. };
  702. MODULE_DEVICE_TABLE(i2c, mcp230xx_id);
  703. static struct i2c_driver mcp230xx_driver = {
  704. .driver = {
  705. .name = "mcp230xx",
  706. .owner = THIS_MODULE,
  707. .of_match_table = of_match_ptr(mcp23s08_i2c_of_match),
  708. },
  709. .probe = mcp230xx_probe,
  710. .remove = mcp230xx_remove,
  711. .id_table = mcp230xx_id,
  712. };
  713. static int __init mcp23s08_i2c_init(void)
  714. {
  715. return i2c_add_driver(&mcp230xx_driver);
  716. }
  717. static void mcp23s08_i2c_exit(void)
  718. {
  719. i2c_del_driver(&mcp230xx_driver);
  720. }
  721. #else
  722. static int __init mcp23s08_i2c_init(void) { return 0; }
  723. static void mcp23s08_i2c_exit(void) { }
  724. #endif /* CONFIG_I2C */
  725. /*----------------------------------------------------------------------*/
  726. #ifdef CONFIG_SPI_MASTER
  727. static int mcp23s08_probe(struct spi_device *spi)
  728. {
  729. struct mcp23s08_platform_data *pdata, local_pdata;
  730. unsigned addr;
  731. int chips = 0;
  732. struct mcp23s08_driver_data *data;
  733. int status, type;
  734. unsigned ngpio = 0;
  735. const struct of_device_id *match;
  736. u32 spi_present_mask = 0;
  737. match = of_match_device(of_match_ptr(mcp23s08_spi_of_match), &spi->dev);
  738. if (match) {
  739. type = (int)(uintptr_t)match->data;
  740. status = of_property_read_u32(spi->dev.of_node,
  741. "microchip,spi-present-mask", &spi_present_mask);
  742. if (status) {
  743. status = of_property_read_u32(spi->dev.of_node,
  744. "mcp,spi-present-mask", &spi_present_mask);
  745. if (status) {
  746. dev_err(&spi->dev,
  747. "DT has no spi-present-mask\n");
  748. return -ENODEV;
  749. }
  750. }
  751. if ((spi_present_mask <= 0) || (spi_present_mask >= 256)) {
  752. dev_err(&spi->dev, "invalid spi-present-mask\n");
  753. return -ENODEV;
  754. }
  755. pdata = &local_pdata;
  756. pdata->base = -1;
  757. for (addr = 0; addr < ARRAY_SIZE(pdata->chip); addr++) {
  758. pdata->chip[addr].pullups = 0;
  759. if (spi_present_mask & (1 << addr))
  760. chips++;
  761. }
  762. pdata->irq_controller = of_property_read_bool(
  763. spi->dev.of_node,
  764. "interrupt-controller");
  765. pdata->mirror = of_property_read_bool(spi->dev.of_node,
  766. "microchip,irq-mirror");
  767. } else {
  768. type = spi_get_device_id(spi)->driver_data;
  769. pdata = dev_get_platdata(&spi->dev);
  770. if (!pdata || !gpio_is_valid(pdata->base)) {
  771. dev_dbg(&spi->dev,
  772. "invalid or missing platform data\n");
  773. return -EINVAL;
  774. }
  775. for (addr = 0; addr < ARRAY_SIZE(pdata->chip); addr++) {
  776. if (!pdata->chip[addr].is_present)
  777. continue;
  778. chips++;
  779. if ((type == MCP_TYPE_S08) && (addr > 3)) {
  780. dev_err(&spi->dev,
  781. "mcp23s08 only supports address 0..3\n");
  782. return -EINVAL;
  783. }
  784. spi_present_mask |= 1 << addr;
  785. }
  786. }
  787. if (!chips)
  788. return -ENODEV;
  789. data = kzalloc(sizeof(*data) + chips * sizeof(struct mcp23s08),
  790. GFP_KERNEL);
  791. if (!data)
  792. return -ENOMEM;
  793. spi_set_drvdata(spi, data);
  794. spi->irq = irq_of_parse_and_map(spi->dev.of_node, 0);
  795. for (addr = 0; addr < ARRAY_SIZE(pdata->chip); addr++) {
  796. if (!(spi_present_mask & (1 << addr)))
  797. continue;
  798. chips--;
  799. data->mcp[addr] = &data->chip[chips];
  800. data->mcp[addr]->irq = spi->irq;
  801. status = mcp23s08_probe_one(data->mcp[addr], &spi->dev, spi,
  802. 0x40 | (addr << 1), type, pdata,
  803. addr);
  804. if (status < 0)
  805. goto fail;
  806. if (pdata->base != -1)
  807. pdata->base += (type == MCP_TYPE_S17) ? 16 : 8;
  808. ngpio += (type == MCP_TYPE_S17) ? 16 : 8;
  809. }
  810. data->ngpio = ngpio;
  811. /* NOTE: these chips have a relatively sane IRQ framework, with
  812. * per-signal masking and level/edge triggering. It's not yet
  813. * handled here...
  814. */
  815. return 0;
  816. fail:
  817. for (addr = 0; addr < ARRAY_SIZE(data->mcp); addr++) {
  818. if (!data->mcp[addr])
  819. continue;
  820. gpiochip_remove(&data->mcp[addr]->chip);
  821. }
  822. kfree(data);
  823. return status;
  824. }
  825. static int mcp23s08_remove(struct spi_device *spi)
  826. {
  827. struct mcp23s08_driver_data *data = spi_get_drvdata(spi);
  828. unsigned addr;
  829. for (addr = 0; addr < ARRAY_SIZE(data->mcp); addr++) {
  830. if (!data->mcp[addr])
  831. continue;
  832. if (spi->irq && data->mcp[addr]->irq_controller)
  833. mcp23s08_irq_teardown(data->mcp[addr]);
  834. gpiochip_remove(&data->mcp[addr]->chip);
  835. }
  836. kfree(data);
  837. return 0;
  838. }
  839. static const struct spi_device_id mcp23s08_ids[] = {
  840. { "mcp23s08", MCP_TYPE_S08 },
  841. { "mcp23s17", MCP_TYPE_S17 },
  842. { },
  843. };
  844. MODULE_DEVICE_TABLE(spi, mcp23s08_ids);
  845. static struct spi_driver mcp23s08_driver = {
  846. .probe = mcp23s08_probe,
  847. .remove = mcp23s08_remove,
  848. .id_table = mcp23s08_ids,
  849. .driver = {
  850. .name = "mcp23s08",
  851. .owner = THIS_MODULE,
  852. .of_match_table = of_match_ptr(mcp23s08_spi_of_match),
  853. },
  854. };
  855. static int __init mcp23s08_spi_init(void)
  856. {
  857. return spi_register_driver(&mcp23s08_driver);
  858. }
  859. static void mcp23s08_spi_exit(void)
  860. {
  861. spi_unregister_driver(&mcp23s08_driver);
  862. }
  863. #else
  864. static int __init mcp23s08_spi_init(void) { return 0; }
  865. static void mcp23s08_spi_exit(void) { }
  866. #endif /* CONFIG_SPI_MASTER */
  867. /*----------------------------------------------------------------------*/
  868. static int __init mcp23s08_init(void)
  869. {
  870. int ret;
  871. ret = mcp23s08_spi_init();
  872. if (ret)
  873. goto spi_fail;
  874. ret = mcp23s08_i2c_init();
  875. if (ret)
  876. goto i2c_fail;
  877. return 0;
  878. i2c_fail:
  879. mcp23s08_spi_exit();
  880. spi_fail:
  881. return ret;
  882. }
  883. /* register after spi/i2c postcore initcall and before
  884. * subsys initcalls that may rely on these GPIOs
  885. */
  886. subsys_initcall(mcp23s08_init);
  887. static void __exit mcp23s08_exit(void)
  888. {
  889. mcp23s08_spi_exit();
  890. mcp23s08_i2c_exit();
  891. }
  892. module_exit(mcp23s08_exit);
  893. MODULE_LICENSE("GPL");