gpio-mcp23s08.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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) {
  670. pdata = devm_kzalloc(&client->dev,
  671. sizeof(struct mcp23s08_platform_data),
  672. GFP_KERNEL);
  673. pdata->base = -1;
  674. }
  675. }
  676. mcp = kzalloc(sizeof(*mcp), GFP_KERNEL);
  677. if (!mcp)
  678. return -ENOMEM;
  679. mcp->irq = client->irq;
  680. status = mcp23s08_probe_one(mcp, &client->dev, client, client->addr,
  681. id->driver_data, pdata, 0);
  682. if (status)
  683. goto fail;
  684. i2c_set_clientdata(client, mcp);
  685. return 0;
  686. fail:
  687. kfree(mcp);
  688. return status;
  689. }
  690. static int mcp230xx_remove(struct i2c_client *client)
  691. {
  692. struct mcp23s08 *mcp = i2c_get_clientdata(client);
  693. if (client->irq && mcp->irq_controller)
  694. mcp23s08_irq_teardown(mcp);
  695. gpiochip_remove(&mcp->chip);
  696. kfree(mcp);
  697. return 0;
  698. }
  699. static const struct i2c_device_id mcp230xx_id[] = {
  700. { "mcp23008", MCP_TYPE_008 },
  701. { "mcp23017", MCP_TYPE_017 },
  702. { },
  703. };
  704. MODULE_DEVICE_TABLE(i2c, mcp230xx_id);
  705. static struct i2c_driver mcp230xx_driver = {
  706. .driver = {
  707. .name = "mcp230xx",
  708. .owner = THIS_MODULE,
  709. .of_match_table = of_match_ptr(mcp23s08_i2c_of_match),
  710. },
  711. .probe = mcp230xx_probe,
  712. .remove = mcp230xx_remove,
  713. .id_table = mcp230xx_id,
  714. };
  715. static int __init mcp23s08_i2c_init(void)
  716. {
  717. return i2c_add_driver(&mcp230xx_driver);
  718. }
  719. static void mcp23s08_i2c_exit(void)
  720. {
  721. i2c_del_driver(&mcp230xx_driver);
  722. }
  723. #else
  724. static int __init mcp23s08_i2c_init(void) { return 0; }
  725. static void mcp23s08_i2c_exit(void) { }
  726. #endif /* CONFIG_I2C */
  727. /*----------------------------------------------------------------------*/
  728. #ifdef CONFIG_SPI_MASTER
  729. static int mcp23s08_probe(struct spi_device *spi)
  730. {
  731. struct mcp23s08_platform_data *pdata, local_pdata;
  732. unsigned addr;
  733. int chips = 0;
  734. struct mcp23s08_driver_data *data;
  735. int status, type;
  736. unsigned ngpio = 0;
  737. const struct of_device_id *match;
  738. u32 spi_present_mask = 0;
  739. match = of_match_device(of_match_ptr(mcp23s08_spi_of_match), &spi->dev);
  740. if (match) {
  741. type = (int)(uintptr_t)match->data;
  742. status = of_property_read_u32(spi->dev.of_node,
  743. "microchip,spi-present-mask", &spi_present_mask);
  744. if (status) {
  745. status = of_property_read_u32(spi->dev.of_node,
  746. "mcp,spi-present-mask", &spi_present_mask);
  747. if (status) {
  748. dev_err(&spi->dev,
  749. "DT has no spi-present-mask\n");
  750. return -ENODEV;
  751. }
  752. }
  753. if ((spi_present_mask <= 0) || (spi_present_mask >= 256)) {
  754. dev_err(&spi->dev, "invalid spi-present-mask\n");
  755. return -ENODEV;
  756. }
  757. pdata = &local_pdata;
  758. pdata->base = -1;
  759. for (addr = 0; addr < ARRAY_SIZE(pdata->chip); addr++) {
  760. pdata->chip[addr].pullups = 0;
  761. if (spi_present_mask & (1 << addr))
  762. chips++;
  763. }
  764. pdata->irq_controller = of_property_read_bool(
  765. spi->dev.of_node,
  766. "interrupt-controller");
  767. pdata->mirror = of_property_read_bool(spi->dev.of_node,
  768. "microchip,irq-mirror");
  769. } else {
  770. type = spi_get_device_id(spi)->driver_data;
  771. pdata = dev_get_platdata(&spi->dev);
  772. if (!pdata) {
  773. pdata = devm_kzalloc(&spi->dev,
  774. sizeof(struct mcp23s08_platform_data),
  775. GFP_KERNEL);
  776. pdata->base = -1;
  777. }
  778. for (addr = 0; addr < ARRAY_SIZE(pdata->chip); addr++) {
  779. if (!pdata->chip[addr].is_present)
  780. continue;
  781. chips++;
  782. if ((type == MCP_TYPE_S08) && (addr > 3)) {
  783. dev_err(&spi->dev,
  784. "mcp23s08 only supports address 0..3\n");
  785. return -EINVAL;
  786. }
  787. spi_present_mask |= 1 << addr;
  788. }
  789. }
  790. if (!chips)
  791. return -ENODEV;
  792. data = devm_kzalloc(&spi->dev,
  793. sizeof(*data) + chips * sizeof(struct mcp23s08),
  794. GFP_KERNEL);
  795. if (!data)
  796. return -ENOMEM;
  797. spi_set_drvdata(spi, data);
  798. spi->irq = irq_of_parse_and_map(spi->dev.of_node, 0);
  799. for (addr = 0; addr < ARRAY_SIZE(pdata->chip); addr++) {
  800. if (!(spi_present_mask & (1 << addr)))
  801. continue;
  802. chips--;
  803. data->mcp[addr] = &data->chip[chips];
  804. data->mcp[addr]->irq = spi->irq;
  805. status = mcp23s08_probe_one(data->mcp[addr], &spi->dev, spi,
  806. 0x40 | (addr << 1), type, pdata,
  807. addr);
  808. if (status < 0)
  809. goto fail;
  810. if (pdata->base != -1)
  811. pdata->base += (type == MCP_TYPE_S17) ? 16 : 8;
  812. ngpio += (type == MCP_TYPE_S17) ? 16 : 8;
  813. }
  814. data->ngpio = ngpio;
  815. /* NOTE: these chips have a relatively sane IRQ framework, with
  816. * per-signal masking and level/edge triggering. It's not yet
  817. * handled here...
  818. */
  819. return 0;
  820. fail:
  821. for (addr = 0; addr < ARRAY_SIZE(data->mcp); addr++) {
  822. if (!data->mcp[addr])
  823. continue;
  824. gpiochip_remove(&data->mcp[addr]->chip);
  825. }
  826. return status;
  827. }
  828. static int mcp23s08_remove(struct spi_device *spi)
  829. {
  830. struct mcp23s08_driver_data *data = spi_get_drvdata(spi);
  831. unsigned addr;
  832. for (addr = 0; addr < ARRAY_SIZE(data->mcp); addr++) {
  833. if (!data->mcp[addr])
  834. continue;
  835. if (spi->irq && data->mcp[addr]->irq_controller)
  836. mcp23s08_irq_teardown(data->mcp[addr]);
  837. gpiochip_remove(&data->mcp[addr]->chip);
  838. }
  839. return 0;
  840. }
  841. static const struct spi_device_id mcp23s08_ids[] = {
  842. { "mcp23s08", MCP_TYPE_S08 },
  843. { "mcp23s17", MCP_TYPE_S17 },
  844. { },
  845. };
  846. MODULE_DEVICE_TABLE(spi, mcp23s08_ids);
  847. static struct spi_driver mcp23s08_driver = {
  848. .probe = mcp23s08_probe,
  849. .remove = mcp23s08_remove,
  850. .id_table = mcp23s08_ids,
  851. .driver = {
  852. .name = "mcp23s08",
  853. .owner = THIS_MODULE,
  854. .of_match_table = of_match_ptr(mcp23s08_spi_of_match),
  855. },
  856. };
  857. static int __init mcp23s08_spi_init(void)
  858. {
  859. return spi_register_driver(&mcp23s08_driver);
  860. }
  861. static void mcp23s08_spi_exit(void)
  862. {
  863. spi_unregister_driver(&mcp23s08_driver);
  864. }
  865. #else
  866. static int __init mcp23s08_spi_init(void) { return 0; }
  867. static void mcp23s08_spi_exit(void) { }
  868. #endif /* CONFIG_SPI_MASTER */
  869. /*----------------------------------------------------------------------*/
  870. static int __init mcp23s08_init(void)
  871. {
  872. int ret;
  873. ret = mcp23s08_spi_init();
  874. if (ret)
  875. goto spi_fail;
  876. ret = mcp23s08_i2c_init();
  877. if (ret)
  878. goto i2c_fail;
  879. return 0;
  880. i2c_fail:
  881. mcp23s08_spi_exit();
  882. spi_fail:
  883. return ret;
  884. }
  885. /* register after spi/i2c postcore initcall and before
  886. * subsys initcalls that may rely on these GPIOs
  887. */
  888. subsys_initcall(mcp23s08_init);
  889. static void __exit mcp23s08_exit(void)
  890. {
  891. mcp23s08_spi_exit();
  892. mcp23s08_i2c_exit();
  893. }
  894. module_exit(mcp23s08_exit);
  895. MODULE_LICENSE("GPL");