mcp23s08.c 9.7 KB

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