gpio-mcp23s08.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * MCP23S08 SPI gpio expander driver
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/device.h>
  6. #include <linux/mutex.h>
  7. #include <linux/gpio.h>
  8. #include <linux/spi/spi.h>
  9. #include <linux/spi/mcp23s08.h>
  10. #include <linux/slab.h>
  11. #include <asm/byteorder.h>
  12. /**
  13. * MCP types supported by driver
  14. */
  15. #define MCP_TYPE_S08 0
  16. #define MCP_TYPE_S17 1
  17. /* Registers are all 8 bits wide.
  18. *
  19. * The mcp23s17 has twice as many bits, and can be configured to work
  20. * with either 16 bit registers or with two adjacent 8 bit banks.
  21. *
  22. * Also, there are I2C versions of both chips.
  23. */
  24. #define MCP_IODIR 0x00 /* init/reset: all ones */
  25. #define MCP_IPOL 0x01
  26. #define MCP_GPINTEN 0x02
  27. #define MCP_DEFVAL 0x03
  28. #define MCP_INTCON 0x04
  29. #define MCP_IOCON 0x05
  30. # define IOCON_SEQOP (1 << 5)
  31. # define IOCON_HAEN (1 << 3)
  32. # define IOCON_ODR (1 << 2)
  33. # define IOCON_INTPOL (1 << 1)
  34. #define MCP_GPPU 0x06
  35. #define MCP_INTF 0x07
  36. #define MCP_INTCAP 0x08
  37. #define MCP_GPIO 0x09
  38. #define MCP_OLAT 0x0a
  39. struct mcp23s08;
  40. struct mcp23s08_ops {
  41. int (*read)(struct mcp23s08 *mcp, unsigned reg);
  42. int (*write)(struct mcp23s08 *mcp, unsigned reg, unsigned val);
  43. int (*read_regs)(struct mcp23s08 *mcp, unsigned reg,
  44. u16 *vals, unsigned n);
  45. };
  46. struct mcp23s08 {
  47. struct spi_device *spi;
  48. u8 addr;
  49. u16 cache[11];
  50. /* lock protects the cached values */
  51. struct mutex lock;
  52. struct gpio_chip chip;
  53. const struct mcp23s08_ops *ops;
  54. };
  55. /* A given spi_device can represent up to eight mcp23sxx chips
  56. * sharing the same chipselect but using different addresses
  57. * (e.g. chips #0 and #3 might be populated, but not #1 or $2).
  58. * Driver data holds all the per-chip data.
  59. */
  60. struct mcp23s08_driver_data {
  61. unsigned ngpio;
  62. struct mcp23s08 *mcp[8];
  63. struct mcp23s08 chip[];
  64. };
  65. static int mcp23s08_read(struct mcp23s08 *mcp, unsigned reg)
  66. {
  67. u8 tx[2], rx[1];
  68. int status;
  69. tx[0] = mcp->addr | 0x01;
  70. tx[1] = reg;
  71. status = spi_write_then_read(mcp->spi, tx, sizeof tx, rx, sizeof rx);
  72. return (status < 0) ? status : rx[0];
  73. }
  74. static int mcp23s08_write(struct mcp23s08 *mcp, unsigned reg, unsigned val)
  75. {
  76. u8 tx[3];
  77. tx[0] = mcp->addr;
  78. tx[1] = reg;
  79. tx[2] = val;
  80. return spi_write_then_read(mcp->spi, tx, sizeof tx, NULL, 0);
  81. }
  82. static int
  83. mcp23s08_read_regs(struct mcp23s08 *mcp, unsigned reg, u16 *vals, unsigned n)
  84. {
  85. u8 tx[2], *tmp;
  86. int status;
  87. if ((n + reg) > sizeof mcp->cache)
  88. return -EINVAL;
  89. tx[0] = mcp->addr | 0x01;
  90. tx[1] = reg;
  91. tmp = (u8 *)vals;
  92. status = spi_write_then_read(mcp->spi, tx, sizeof tx, tmp, n);
  93. if (status >= 0) {
  94. while (n--)
  95. vals[n] = tmp[n]; /* expand to 16bit */
  96. }
  97. return status;
  98. }
  99. static int mcp23s17_read(struct mcp23s08 *mcp, unsigned reg)
  100. {
  101. u8 tx[2], rx[2];
  102. int status;
  103. tx[0] = mcp->addr | 0x01;
  104. tx[1] = reg << 1;
  105. status = spi_write_then_read(mcp->spi, tx, sizeof tx, rx, sizeof rx);
  106. return (status < 0) ? status : (rx[0] | (rx[1] << 8));
  107. }
  108. static int mcp23s17_write(struct mcp23s08 *mcp, unsigned reg, unsigned val)
  109. {
  110. u8 tx[4];
  111. tx[0] = mcp->addr;
  112. tx[1] = reg << 1;
  113. tx[2] = val;
  114. tx[3] = val >> 8;
  115. return spi_write_then_read(mcp->spi, tx, sizeof tx, NULL, 0);
  116. }
  117. static int
  118. mcp23s17_read_regs(struct mcp23s08 *mcp, unsigned reg, u16 *vals, unsigned n)
  119. {
  120. u8 tx[2];
  121. int status;
  122. if ((n + reg) > sizeof mcp->cache)
  123. return -EINVAL;
  124. tx[0] = mcp->addr | 0x01;
  125. tx[1] = reg << 1;
  126. status = spi_write_then_read(mcp->spi, tx, sizeof tx,
  127. (u8 *)vals, n * 2);
  128. if (status >= 0) {
  129. while (n--)
  130. vals[n] = __le16_to_cpu((__le16)vals[n]);
  131. }
  132. return status;
  133. }
  134. static const struct mcp23s08_ops mcp23s08_ops = {
  135. .read = mcp23s08_read,
  136. .write = mcp23s08_write,
  137. .read_regs = mcp23s08_read_regs,
  138. };
  139. static const struct mcp23s08_ops mcp23s17_ops = {
  140. .read = mcp23s17_read,
  141. .write = mcp23s17_write,
  142. .read_regs = mcp23s17_read_regs,
  143. };
  144. /*----------------------------------------------------------------------*/
  145. static int mcp23s08_direction_input(struct gpio_chip *chip, unsigned offset)
  146. {
  147. struct mcp23s08 *mcp = container_of(chip, struct mcp23s08, chip);
  148. int status;
  149. mutex_lock(&mcp->lock);
  150. mcp->cache[MCP_IODIR] |= (1 << offset);
  151. status = mcp->ops->write(mcp, MCP_IODIR, mcp->cache[MCP_IODIR]);
  152. mutex_unlock(&mcp->lock);
  153. return status;
  154. }
  155. static int mcp23s08_get(struct gpio_chip *chip, unsigned offset)
  156. {
  157. struct mcp23s08 *mcp = container_of(chip, struct mcp23s08, chip);
  158. int status;
  159. mutex_lock(&mcp->lock);
  160. /* REVISIT reading this clears any IRQ ... */
  161. status = mcp->ops->read(mcp, MCP_GPIO);
  162. if (status < 0)
  163. status = 0;
  164. else {
  165. mcp->cache[MCP_GPIO] = status;
  166. status = !!(status & (1 << offset));
  167. }
  168. mutex_unlock(&mcp->lock);
  169. return status;
  170. }
  171. static int __mcp23s08_set(struct mcp23s08 *mcp, unsigned mask, int value)
  172. {
  173. unsigned olat = mcp->cache[MCP_OLAT];
  174. if (value)
  175. olat |= mask;
  176. else
  177. olat &= ~mask;
  178. mcp->cache[MCP_OLAT] = olat;
  179. return mcp->ops->write(mcp, MCP_OLAT, olat);
  180. }
  181. static void mcp23s08_set(struct gpio_chip *chip, unsigned offset, int value)
  182. {
  183. struct mcp23s08 *mcp = container_of(chip, struct mcp23s08, chip);
  184. unsigned mask = 1 << offset;
  185. mutex_lock(&mcp->lock);
  186. __mcp23s08_set(mcp, mask, value);
  187. mutex_unlock(&mcp->lock);
  188. }
  189. static int
  190. mcp23s08_direction_output(struct gpio_chip *chip, unsigned offset, int value)
  191. {
  192. struct mcp23s08 *mcp = container_of(chip, struct mcp23s08, chip);
  193. unsigned mask = 1 << offset;
  194. int status;
  195. mutex_lock(&mcp->lock);
  196. status = __mcp23s08_set(mcp, mask, value);
  197. if (status == 0) {
  198. mcp->cache[MCP_IODIR] &= ~mask;
  199. status = mcp->ops->write(mcp, MCP_IODIR, mcp->cache[MCP_IODIR]);
  200. }
  201. mutex_unlock(&mcp->lock);
  202. return status;
  203. }
  204. /*----------------------------------------------------------------------*/
  205. #ifdef CONFIG_DEBUG_FS
  206. #include <linux/seq_file.h>
  207. /*
  208. * This shows more info than the generic gpio dump code:
  209. * pullups, deglitching, open drain drive.
  210. */
  211. static void mcp23s08_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  212. {
  213. struct mcp23s08 *mcp;
  214. char bank;
  215. int t;
  216. unsigned mask;
  217. mcp = container_of(chip, struct mcp23s08, chip);
  218. /* NOTE: we only handle one bank for now ... */
  219. bank = '0' + ((mcp->addr >> 1) & 0x7);
  220. mutex_lock(&mcp->lock);
  221. t = mcp->ops->read_regs(mcp, 0, mcp->cache, ARRAY_SIZE(mcp->cache));
  222. if (t < 0) {
  223. seq_printf(s, " I/O ERROR %d\n", t);
  224. goto done;
  225. }
  226. for (t = 0, mask = 1; t < chip->ngpio; t++, mask <<= 1) {
  227. const char *label;
  228. label = gpiochip_is_requested(chip, t);
  229. if (!label)
  230. continue;
  231. seq_printf(s, " gpio-%-3d P%c.%d (%-12s) %s %s %s",
  232. chip->base + t, bank, t, label,
  233. (mcp->cache[MCP_IODIR] & mask) ? "in " : "out",
  234. (mcp->cache[MCP_GPIO] & mask) ? "hi" : "lo",
  235. (mcp->cache[MCP_GPPU] & mask) ? " " : "up");
  236. /* NOTE: ignoring the irq-related registers */
  237. seq_printf(s, "\n");
  238. }
  239. done:
  240. mutex_unlock(&mcp->lock);
  241. }
  242. #else
  243. #define mcp23s08_dbg_show NULL
  244. #endif
  245. /*----------------------------------------------------------------------*/
  246. static int mcp23s08_probe_one(struct spi_device *spi, unsigned addr,
  247. unsigned type, unsigned base, unsigned pullups)
  248. {
  249. struct mcp23s08_driver_data *data = spi_get_drvdata(spi);
  250. struct mcp23s08 *mcp = data->mcp[addr];
  251. int status;
  252. mutex_init(&mcp->lock);
  253. mcp->spi = spi;
  254. mcp->addr = 0x40 | (addr << 1);
  255. mcp->chip.direction_input = mcp23s08_direction_input;
  256. mcp->chip.get = mcp23s08_get;
  257. mcp->chip.direction_output = mcp23s08_direction_output;
  258. mcp->chip.set = mcp23s08_set;
  259. mcp->chip.dbg_show = mcp23s08_dbg_show;
  260. if (type == MCP_TYPE_S17) {
  261. mcp->ops = &mcp23s17_ops;
  262. mcp->chip.ngpio = 16;
  263. mcp->chip.label = "mcp23s17";
  264. } else {
  265. mcp->ops = &mcp23s08_ops;
  266. mcp->chip.ngpio = 8;
  267. mcp->chip.label = "mcp23s08";
  268. }
  269. mcp->chip.base = base;
  270. mcp->chip.can_sleep = 1;
  271. mcp->chip.dev = &spi->dev;
  272. mcp->chip.owner = THIS_MODULE;
  273. /* verify MCP_IOCON.SEQOP = 0, so sequential reads work,
  274. * and MCP_IOCON.HAEN = 1, so we work with all chips.
  275. */
  276. status = mcp->ops->read(mcp, MCP_IOCON);
  277. if (status < 0)
  278. goto fail;
  279. if ((status & IOCON_SEQOP) || !(status & IOCON_HAEN)) {
  280. /* mcp23s17 has IOCON twice, make sure they are in sync */
  281. status &= ~(IOCON_SEQOP | (IOCON_SEQOP << 8));
  282. status |= IOCON_HAEN | (IOCON_HAEN << 8);
  283. status = mcp->ops->write(mcp, MCP_IOCON, status);
  284. if (status < 0)
  285. goto fail;
  286. }
  287. /* configure ~100K pullups */
  288. status = mcp->ops->write(mcp, MCP_GPPU, pullups);
  289. if (status < 0)
  290. goto fail;
  291. status = mcp->ops->read_regs(mcp, 0, mcp->cache, ARRAY_SIZE(mcp->cache));
  292. if (status < 0)
  293. goto fail;
  294. /* disable inverter on input */
  295. if (mcp->cache[MCP_IPOL] != 0) {
  296. mcp->cache[MCP_IPOL] = 0;
  297. status = mcp->ops->write(mcp, MCP_IPOL, 0);
  298. if (status < 0)
  299. goto fail;
  300. }
  301. /* disable irqs */
  302. if (mcp->cache[MCP_GPINTEN] != 0) {
  303. mcp->cache[MCP_GPINTEN] = 0;
  304. status = mcp->ops->write(mcp, MCP_GPINTEN, 0);
  305. if (status < 0)
  306. goto fail;
  307. }
  308. status = gpiochip_add(&mcp->chip);
  309. fail:
  310. if (status < 0)
  311. dev_dbg(&spi->dev, "can't setup chip %d, --> %d\n",
  312. addr, status);
  313. return status;
  314. }
  315. static int mcp23s08_probe(struct spi_device *spi)
  316. {
  317. struct mcp23s08_platform_data *pdata;
  318. unsigned addr;
  319. unsigned chips = 0;
  320. struct mcp23s08_driver_data *data;
  321. int status, type;
  322. unsigned base;
  323. type = spi_get_device_id(spi)->driver_data;
  324. pdata = spi->dev.platform_data;
  325. if (!pdata || !gpio_is_valid(pdata->base)) {
  326. dev_dbg(&spi->dev, "invalid or missing platform data\n");
  327. return -EINVAL;
  328. }
  329. for (addr = 0; addr < ARRAY_SIZE(pdata->chip); addr++) {
  330. if (!pdata->chip[addr].is_present)
  331. continue;
  332. chips++;
  333. if ((type == MCP_TYPE_S08) && (addr > 3)) {
  334. dev_err(&spi->dev,
  335. "mcp23s08 only supports address 0..3\n");
  336. return -EINVAL;
  337. }
  338. }
  339. if (!chips)
  340. return -ENODEV;
  341. data = kzalloc(sizeof *data + chips * sizeof(struct mcp23s08),
  342. GFP_KERNEL);
  343. if (!data)
  344. return -ENOMEM;
  345. spi_set_drvdata(spi, data);
  346. base = pdata->base;
  347. for (addr = 0; addr < ARRAY_SIZE(pdata->chip); addr++) {
  348. if (!pdata->chip[addr].is_present)
  349. continue;
  350. chips--;
  351. data->mcp[addr] = &data->chip[chips];
  352. status = mcp23s08_probe_one(spi, addr, type, base,
  353. pdata->chip[addr].pullups);
  354. if (status < 0)
  355. goto fail;
  356. base += (type == MCP_TYPE_S17) ? 16 : 8;
  357. }
  358. data->ngpio = base - pdata->base;
  359. /* NOTE: these chips have a relatively sane IRQ framework, with
  360. * per-signal masking and level/edge triggering. It's not yet
  361. * handled here...
  362. */
  363. if (pdata->setup) {
  364. status = pdata->setup(spi,
  365. pdata->base, data->ngpio,
  366. pdata->context);
  367. if (status < 0)
  368. dev_dbg(&spi->dev, "setup --> %d\n", status);
  369. }
  370. return 0;
  371. fail:
  372. for (addr = 0; addr < ARRAY_SIZE(data->mcp); addr++) {
  373. int tmp;
  374. if (!data->mcp[addr])
  375. continue;
  376. tmp = gpiochip_remove(&data->mcp[addr]->chip);
  377. if (tmp < 0)
  378. dev_err(&spi->dev, "%s --> %d\n", "remove", tmp);
  379. }
  380. kfree(data);
  381. return status;
  382. }
  383. static int mcp23s08_remove(struct spi_device *spi)
  384. {
  385. struct mcp23s08_driver_data *data = spi_get_drvdata(spi);
  386. struct mcp23s08_platform_data *pdata = spi->dev.platform_data;
  387. unsigned addr;
  388. int status = 0;
  389. if (pdata->teardown) {
  390. status = pdata->teardown(spi,
  391. pdata->base, data->ngpio,
  392. pdata->context);
  393. if (status < 0) {
  394. dev_err(&spi->dev, "%s --> %d\n", "teardown", status);
  395. return status;
  396. }
  397. }
  398. for (addr = 0; addr < ARRAY_SIZE(data->mcp); addr++) {
  399. int tmp;
  400. if (!data->mcp[addr])
  401. continue;
  402. tmp = gpiochip_remove(&data->mcp[addr]->chip);
  403. if (tmp < 0) {
  404. dev_err(&spi->dev, "%s --> %d\n", "remove", tmp);
  405. status = tmp;
  406. }
  407. }
  408. if (status == 0)
  409. kfree(data);
  410. return status;
  411. }
  412. static const struct spi_device_id mcp23s08_ids[] = {
  413. { "mcp23s08", MCP_TYPE_S08 },
  414. { "mcp23s17", MCP_TYPE_S17 },
  415. { },
  416. };
  417. MODULE_DEVICE_TABLE(spi, mcp23s08_ids);
  418. static struct spi_driver mcp23s08_driver = {
  419. .probe = mcp23s08_probe,
  420. .remove = mcp23s08_remove,
  421. .id_table = mcp23s08_ids,
  422. .driver = {
  423. .name = "mcp23s08",
  424. .owner = THIS_MODULE,
  425. },
  426. };
  427. /*----------------------------------------------------------------------*/
  428. static int __init mcp23s08_init(void)
  429. {
  430. return spi_register_driver(&mcp23s08_driver);
  431. }
  432. /* register after spi postcore initcall and before
  433. * subsys initcalls that may rely on these GPIOs
  434. */
  435. subsys_initcall(mcp23s08_init);
  436. static void __exit mcp23s08_exit(void)
  437. {
  438. spi_unregister_driver(&mcp23s08_driver);
  439. }
  440. module_exit(mcp23s08_exit);
  441. MODULE_LICENSE("GPL");