gpio-mcp23s08.c 25 KB

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