mcp23s08.c 12 KB

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