gpio-mcp23s08.c 24 KB

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