mcp23s08.c 9.5 KB

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