gpio-mcp23s08.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /*
  2. * MCP23S08 SPI/GPIO 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/i2c.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. #define MCP_TYPE_008 2
  19. #define MCP_TYPE_017 3
  20. /* Registers are all 8 bits wide.
  21. *
  22. * The mcp23s17 has twice as many bits, and can be configured to work
  23. * with either 16 bit registers or with two adjacent 8 bit banks.
  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. 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. void *data; /* ops specific data */
  55. };
  56. /* A given spi_device can represent up to eight mcp23sxx chips
  57. * sharing the same chipselect but using different addresses
  58. * (e.g. chips #0 and #3 might be populated, but not #1 or $2).
  59. * Driver data holds all the per-chip data.
  60. */
  61. struct mcp23s08_driver_data {
  62. unsigned ngpio;
  63. struct mcp23s08 *mcp[8];
  64. struct mcp23s08 chip[];
  65. };
  66. /*----------------------------------------------------------------------*/
  67. #ifdef CONFIG_I2C
  68. static int mcp23008_read(struct mcp23s08 *mcp, unsigned reg)
  69. {
  70. return i2c_smbus_read_byte_data(mcp->data, reg);
  71. }
  72. static int mcp23008_write(struct mcp23s08 *mcp, unsigned reg, unsigned val)
  73. {
  74. return i2c_smbus_write_byte_data(mcp->data, reg, val);
  75. }
  76. static int
  77. mcp23008_read_regs(struct mcp23s08 *mcp, unsigned reg, u16 *vals, unsigned n)
  78. {
  79. while (n--) {
  80. int ret = mcp23008_read(mcp, reg++);
  81. if (ret < 0)
  82. return ret;
  83. *vals++ = ret;
  84. }
  85. return 0;
  86. }
  87. static int mcp23017_read(struct mcp23s08 *mcp, unsigned reg)
  88. {
  89. return i2c_smbus_read_word_data(mcp->data, reg << 1);
  90. }
  91. static int mcp23017_write(struct mcp23s08 *mcp, unsigned reg, unsigned val)
  92. {
  93. return i2c_smbus_write_word_data(mcp->data, reg << 1, val);
  94. }
  95. static int
  96. mcp23017_read_regs(struct mcp23s08 *mcp, unsigned reg, u16 *vals, unsigned n)
  97. {
  98. while (n--) {
  99. int ret = mcp23017_read(mcp, reg++);
  100. if (ret < 0)
  101. return ret;
  102. *vals++ = ret;
  103. }
  104. return 0;
  105. }
  106. static const struct mcp23s08_ops mcp23008_ops = {
  107. .read = mcp23008_read,
  108. .write = mcp23008_write,
  109. .read_regs = mcp23008_read_regs,
  110. };
  111. static const struct mcp23s08_ops mcp23017_ops = {
  112. .read = mcp23017_read,
  113. .write = mcp23017_write,
  114. .read_regs = mcp23017_read_regs,
  115. };
  116. #endif /* CONFIG_I2C */
  117. /*----------------------------------------------------------------------*/
  118. #ifdef CONFIG_SPI_MASTER
  119. static int mcp23s08_read(struct mcp23s08 *mcp, unsigned reg)
  120. {
  121. u8 tx[2], rx[1];
  122. int status;
  123. tx[0] = mcp->addr | 0x01;
  124. tx[1] = reg;
  125. status = spi_write_then_read(mcp->data, tx, sizeof tx, rx, sizeof rx);
  126. return (status < 0) ? status : rx[0];
  127. }
  128. static int mcp23s08_write(struct mcp23s08 *mcp, unsigned reg, unsigned val)
  129. {
  130. u8 tx[3];
  131. tx[0] = mcp->addr;
  132. tx[1] = reg;
  133. tx[2] = val;
  134. return spi_write_then_read(mcp->data, tx, sizeof tx, NULL, 0);
  135. }
  136. static int
  137. mcp23s08_read_regs(struct mcp23s08 *mcp, unsigned reg, u16 *vals, unsigned n)
  138. {
  139. u8 tx[2], *tmp;
  140. int status;
  141. if ((n + reg) > sizeof mcp->cache)
  142. return -EINVAL;
  143. tx[0] = mcp->addr | 0x01;
  144. tx[1] = reg;
  145. tmp = (u8 *)vals;
  146. status = spi_write_then_read(mcp->data, tx, sizeof tx, tmp, n);
  147. if (status >= 0) {
  148. while (n--)
  149. vals[n] = tmp[n]; /* expand to 16bit */
  150. }
  151. return status;
  152. }
  153. static int mcp23s17_read(struct mcp23s08 *mcp, unsigned reg)
  154. {
  155. u8 tx[2], rx[2];
  156. int status;
  157. tx[0] = mcp->addr | 0x01;
  158. tx[1] = reg << 1;
  159. status = spi_write_then_read(mcp->data, tx, sizeof tx, rx, sizeof rx);
  160. return (status < 0) ? status : (rx[0] | (rx[1] << 8));
  161. }
  162. static int mcp23s17_write(struct mcp23s08 *mcp, unsigned reg, unsigned val)
  163. {
  164. u8 tx[4];
  165. tx[0] = mcp->addr;
  166. tx[1] = reg << 1;
  167. tx[2] = val;
  168. tx[3] = val >> 8;
  169. return spi_write_then_read(mcp->data, tx, sizeof tx, NULL, 0);
  170. }
  171. static int
  172. mcp23s17_read_regs(struct mcp23s08 *mcp, unsigned reg, u16 *vals, unsigned n)
  173. {
  174. u8 tx[2];
  175. int status;
  176. if ((n + reg) > sizeof mcp->cache)
  177. return -EINVAL;
  178. tx[0] = mcp->addr | 0x01;
  179. tx[1] = reg << 1;
  180. status = spi_write_then_read(mcp->data, tx, sizeof tx,
  181. (u8 *)vals, n * 2);
  182. if (status >= 0) {
  183. while (n--)
  184. vals[n] = __le16_to_cpu((__le16)vals[n]);
  185. }
  186. return status;
  187. }
  188. static const struct mcp23s08_ops mcp23s08_ops = {
  189. .read = mcp23s08_read,
  190. .write = mcp23s08_write,
  191. .read_regs = mcp23s08_read_regs,
  192. };
  193. static const struct mcp23s08_ops mcp23s17_ops = {
  194. .read = mcp23s17_read,
  195. .write = mcp23s17_write,
  196. .read_regs = mcp23s17_read_regs,
  197. };
  198. #endif /* CONFIG_SPI_MASTER */
  199. /*----------------------------------------------------------------------*/
  200. static int mcp23s08_direction_input(struct gpio_chip *chip, unsigned offset)
  201. {
  202. struct mcp23s08 *mcp = container_of(chip, struct mcp23s08, chip);
  203. int status;
  204. mutex_lock(&mcp->lock);
  205. mcp->cache[MCP_IODIR] |= (1 << offset);
  206. status = mcp->ops->write(mcp, MCP_IODIR, mcp->cache[MCP_IODIR]);
  207. mutex_unlock(&mcp->lock);
  208. return status;
  209. }
  210. static int mcp23s08_get(struct gpio_chip *chip, unsigned offset)
  211. {
  212. struct mcp23s08 *mcp = container_of(chip, struct mcp23s08, chip);
  213. int status;
  214. mutex_lock(&mcp->lock);
  215. /* REVISIT reading this clears any IRQ ... */
  216. status = mcp->ops->read(mcp, MCP_GPIO);
  217. if (status < 0)
  218. status = 0;
  219. else {
  220. mcp->cache[MCP_GPIO] = status;
  221. status = !!(status & (1 << offset));
  222. }
  223. mutex_unlock(&mcp->lock);
  224. return status;
  225. }
  226. static int __mcp23s08_set(struct mcp23s08 *mcp, unsigned mask, int value)
  227. {
  228. unsigned olat = mcp->cache[MCP_OLAT];
  229. if (value)
  230. olat |= mask;
  231. else
  232. olat &= ~mask;
  233. mcp->cache[MCP_OLAT] = olat;
  234. return mcp->ops->write(mcp, MCP_OLAT, olat);
  235. }
  236. static void mcp23s08_set(struct gpio_chip *chip, unsigned offset, int value)
  237. {
  238. struct mcp23s08 *mcp = container_of(chip, struct mcp23s08, chip);
  239. unsigned mask = 1 << offset;
  240. mutex_lock(&mcp->lock);
  241. __mcp23s08_set(mcp, mask, value);
  242. mutex_unlock(&mcp->lock);
  243. }
  244. static int
  245. mcp23s08_direction_output(struct gpio_chip *chip, unsigned offset, int value)
  246. {
  247. struct mcp23s08 *mcp = container_of(chip, struct mcp23s08, chip);
  248. unsigned mask = 1 << offset;
  249. int status;
  250. mutex_lock(&mcp->lock);
  251. status = __mcp23s08_set(mcp, mask, value);
  252. if (status == 0) {
  253. mcp->cache[MCP_IODIR] &= ~mask;
  254. status = mcp->ops->write(mcp, MCP_IODIR, mcp->cache[MCP_IODIR]);
  255. }
  256. mutex_unlock(&mcp->lock);
  257. return status;
  258. }
  259. /*----------------------------------------------------------------------*/
  260. #ifdef CONFIG_DEBUG_FS
  261. #include <linux/seq_file.h>
  262. /*
  263. * This shows more info than the generic gpio dump code:
  264. * pullups, deglitching, open drain drive.
  265. */
  266. static void mcp23s08_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  267. {
  268. struct mcp23s08 *mcp;
  269. char bank;
  270. int t;
  271. unsigned mask;
  272. mcp = container_of(chip, struct mcp23s08, chip);
  273. /* NOTE: we only handle one bank for now ... */
  274. bank = '0' + ((mcp->addr >> 1) & 0x7);
  275. mutex_lock(&mcp->lock);
  276. t = mcp->ops->read_regs(mcp, 0, mcp->cache, ARRAY_SIZE(mcp->cache));
  277. if (t < 0) {
  278. seq_printf(s, " I/O ERROR %d\n", t);
  279. goto done;
  280. }
  281. for (t = 0, mask = 1; t < chip->ngpio; t++, mask <<= 1) {
  282. const char *label;
  283. label = gpiochip_is_requested(chip, t);
  284. if (!label)
  285. continue;
  286. seq_printf(s, " gpio-%-3d P%c.%d (%-12s) %s %s %s",
  287. chip->base + t, bank, t, label,
  288. (mcp->cache[MCP_IODIR] & mask) ? "in " : "out",
  289. (mcp->cache[MCP_GPIO] & mask) ? "hi" : "lo",
  290. (mcp->cache[MCP_GPPU] & mask) ? " " : "up");
  291. /* NOTE: ignoring the irq-related registers */
  292. seq_printf(s, "\n");
  293. }
  294. done:
  295. mutex_unlock(&mcp->lock);
  296. }
  297. #else
  298. #define mcp23s08_dbg_show NULL
  299. #endif
  300. /*----------------------------------------------------------------------*/
  301. static int mcp23s08_probe_one(struct mcp23s08 *mcp, struct device *dev,
  302. void *data, unsigned addr,
  303. unsigned type, unsigned base, unsigned pullups)
  304. {
  305. int status;
  306. mutex_init(&mcp->lock);
  307. mcp->data = data;
  308. mcp->addr = addr;
  309. mcp->chip.direction_input = mcp23s08_direction_input;
  310. mcp->chip.get = mcp23s08_get;
  311. mcp->chip.direction_output = mcp23s08_direction_output;
  312. mcp->chip.set = mcp23s08_set;
  313. mcp->chip.dbg_show = mcp23s08_dbg_show;
  314. switch (type) {
  315. #ifdef CONFIG_SPI_MASTER
  316. case MCP_TYPE_S08:
  317. mcp->ops = &mcp23s08_ops;
  318. mcp->chip.ngpio = 8;
  319. mcp->chip.label = "mcp23s08";
  320. break;
  321. case MCP_TYPE_S17:
  322. mcp->ops = &mcp23s17_ops;
  323. mcp->chip.ngpio = 16;
  324. mcp->chip.label = "mcp23s17";
  325. break;
  326. #endif /* CONFIG_SPI_MASTER */
  327. #ifdef CONFIG_I2C
  328. case MCP_TYPE_008:
  329. mcp->ops = &mcp23008_ops;
  330. mcp->chip.ngpio = 8;
  331. mcp->chip.label = "mcp23008";
  332. break;
  333. case MCP_TYPE_017:
  334. mcp->ops = &mcp23017_ops;
  335. mcp->chip.ngpio = 16;
  336. mcp->chip.label = "mcp23017";
  337. break;
  338. #endif /* CONFIG_I2C */
  339. default:
  340. dev_err(dev, "invalid device type (%d)\n", type);
  341. return -EINVAL;
  342. }
  343. mcp->chip.base = base;
  344. mcp->chip.can_sleep = 1;
  345. mcp->chip.dev = dev;
  346. mcp->chip.owner = THIS_MODULE;
  347. /* verify MCP_IOCON.SEQOP = 0, so sequential reads work,
  348. * and MCP_IOCON.HAEN = 1, so we work with all chips.
  349. */
  350. status = mcp->ops->read(mcp, MCP_IOCON);
  351. if (status < 0)
  352. goto fail;
  353. if ((status & IOCON_SEQOP) || !(status & IOCON_HAEN)) {
  354. /* mcp23s17 has IOCON twice, make sure they are in sync */
  355. status &= ~(IOCON_SEQOP | (IOCON_SEQOP << 8));
  356. status |= IOCON_HAEN | (IOCON_HAEN << 8);
  357. status = mcp->ops->write(mcp, MCP_IOCON, status);
  358. if (status < 0)
  359. goto fail;
  360. }
  361. /* configure ~100K pullups */
  362. status = mcp->ops->write(mcp, MCP_GPPU, pullups);
  363. if (status < 0)
  364. goto fail;
  365. status = mcp->ops->read_regs(mcp, 0, mcp->cache, ARRAY_SIZE(mcp->cache));
  366. if (status < 0)
  367. goto fail;
  368. /* disable inverter on input */
  369. if (mcp->cache[MCP_IPOL] != 0) {
  370. mcp->cache[MCP_IPOL] = 0;
  371. status = mcp->ops->write(mcp, MCP_IPOL, 0);
  372. if (status < 0)
  373. goto fail;
  374. }
  375. /* disable irqs */
  376. if (mcp->cache[MCP_GPINTEN] != 0) {
  377. mcp->cache[MCP_GPINTEN] = 0;
  378. status = mcp->ops->write(mcp, MCP_GPINTEN, 0);
  379. if (status < 0)
  380. goto fail;
  381. }
  382. status = gpiochip_add(&mcp->chip);
  383. fail:
  384. if (status < 0)
  385. dev_dbg(dev, "can't setup chip %d, --> %d\n",
  386. addr, status);
  387. return status;
  388. }
  389. /*----------------------------------------------------------------------*/
  390. #ifdef CONFIG_I2C
  391. static int __devinit mcp230xx_probe(struct i2c_client *client,
  392. const struct i2c_device_id *id)
  393. {
  394. struct mcp23s08_platform_data *pdata;
  395. struct mcp23s08 *mcp;
  396. int status;
  397. pdata = client->dev.platform_data;
  398. if (!pdata || !gpio_is_valid(pdata->base)) {
  399. dev_dbg(&client->dev, "invalid or missing platform data\n");
  400. return -EINVAL;
  401. }
  402. mcp = kzalloc(sizeof *mcp, GFP_KERNEL);
  403. if (!mcp)
  404. return -ENOMEM;
  405. status = mcp23s08_probe_one(mcp, &client->dev, client, client->addr,
  406. id->driver_data, pdata->base,
  407. pdata->chip[0].pullups);
  408. if (status)
  409. goto fail;
  410. i2c_set_clientdata(client, mcp);
  411. return 0;
  412. fail:
  413. kfree(mcp);
  414. return status;
  415. }
  416. static int __devexit mcp230xx_remove(struct i2c_client *client)
  417. {
  418. struct mcp23s08 *mcp = i2c_get_clientdata(client);
  419. int status;
  420. status = gpiochip_remove(&mcp->chip);
  421. if (status == 0)
  422. kfree(mcp);
  423. return status;
  424. }
  425. static const struct i2c_device_id mcp230xx_id[] = {
  426. { "mcp23008", MCP_TYPE_008 },
  427. { "mcp23017", MCP_TYPE_017 },
  428. { },
  429. };
  430. MODULE_DEVICE_TABLE(i2c, mcp230xx_id);
  431. static struct i2c_driver mcp230xx_driver = {
  432. .driver = {
  433. .name = "mcp230xx",
  434. .owner = THIS_MODULE,
  435. },
  436. .probe = mcp230xx_probe,
  437. .remove = __devexit_p(mcp230xx_remove),
  438. .id_table = mcp230xx_id,
  439. };
  440. static int __init mcp23s08_i2c_init(void)
  441. {
  442. return i2c_add_driver(&mcp230xx_driver);
  443. }
  444. static void mcp23s08_i2c_exit(void)
  445. {
  446. i2c_del_driver(&mcp230xx_driver);
  447. }
  448. #else
  449. static int __init mcp23s08_i2c_init(void) { return 0; }
  450. static void mcp23s08_i2c_exit(void) { }
  451. #endif /* CONFIG_I2C */
  452. /*----------------------------------------------------------------------*/
  453. #ifdef CONFIG_SPI_MASTER
  454. static int mcp23s08_probe(struct spi_device *spi)
  455. {
  456. struct mcp23s08_platform_data *pdata;
  457. unsigned addr;
  458. unsigned chips = 0;
  459. struct mcp23s08_driver_data *data;
  460. int status, type;
  461. unsigned base;
  462. type = spi_get_device_id(spi)->driver_data;
  463. pdata = spi->dev.platform_data;
  464. if (!pdata || !gpio_is_valid(pdata->base)) {
  465. dev_dbg(&spi->dev, "invalid or missing platform data\n");
  466. return -EINVAL;
  467. }
  468. for (addr = 0; addr < ARRAY_SIZE(pdata->chip); addr++) {
  469. if (!pdata->chip[addr].is_present)
  470. continue;
  471. chips++;
  472. if ((type == MCP_TYPE_S08) && (addr > 3)) {
  473. dev_err(&spi->dev,
  474. "mcp23s08 only supports address 0..3\n");
  475. return -EINVAL;
  476. }
  477. }
  478. if (!chips)
  479. return -ENODEV;
  480. data = kzalloc(sizeof *data + chips * sizeof(struct mcp23s08),
  481. GFP_KERNEL);
  482. if (!data)
  483. return -ENOMEM;
  484. spi_set_drvdata(spi, data);
  485. base = pdata->base;
  486. for (addr = 0; addr < ARRAY_SIZE(pdata->chip); addr++) {
  487. if (!pdata->chip[addr].is_present)
  488. continue;
  489. chips--;
  490. data->mcp[addr] = &data->chip[chips];
  491. status = mcp23s08_probe_one(data->mcp[addr], &spi->dev, spi,
  492. 0x40 | (addr << 1), type, base,
  493. pdata->chip[addr].pullups);
  494. if (status < 0)
  495. goto fail;
  496. base += (type == MCP_TYPE_S17) ? 16 : 8;
  497. }
  498. data->ngpio = base - pdata->base;
  499. /* NOTE: these chips have a relatively sane IRQ framework, with
  500. * per-signal masking and level/edge triggering. It's not yet
  501. * handled here...
  502. */
  503. return 0;
  504. fail:
  505. for (addr = 0; addr < ARRAY_SIZE(data->mcp); addr++) {
  506. int tmp;
  507. if (!data->mcp[addr])
  508. continue;
  509. tmp = gpiochip_remove(&data->mcp[addr]->chip);
  510. if (tmp < 0)
  511. dev_err(&spi->dev, "%s --> %d\n", "remove", tmp);
  512. }
  513. kfree(data);
  514. return status;
  515. }
  516. static int mcp23s08_remove(struct spi_device *spi)
  517. {
  518. struct mcp23s08_driver_data *data = spi_get_drvdata(spi);
  519. unsigned addr;
  520. int status = 0;
  521. for (addr = 0; addr < ARRAY_SIZE(data->mcp); addr++) {
  522. int tmp;
  523. if (!data->mcp[addr])
  524. continue;
  525. tmp = gpiochip_remove(&data->mcp[addr]->chip);
  526. if (tmp < 0) {
  527. dev_err(&spi->dev, "%s --> %d\n", "remove", tmp);
  528. status = tmp;
  529. }
  530. }
  531. if (status == 0)
  532. kfree(data);
  533. return status;
  534. }
  535. static const struct spi_device_id mcp23s08_ids[] = {
  536. { "mcp23s08", MCP_TYPE_S08 },
  537. { "mcp23s17", MCP_TYPE_S17 },
  538. { },
  539. };
  540. MODULE_DEVICE_TABLE(spi, mcp23s08_ids);
  541. static struct spi_driver mcp23s08_driver = {
  542. .probe = mcp23s08_probe,
  543. .remove = mcp23s08_remove,
  544. .id_table = mcp23s08_ids,
  545. .driver = {
  546. .name = "mcp23s08",
  547. .owner = THIS_MODULE,
  548. },
  549. };
  550. static int __init mcp23s08_spi_init(void)
  551. {
  552. return spi_register_driver(&mcp23s08_driver);
  553. }
  554. static void mcp23s08_spi_exit(void)
  555. {
  556. spi_unregister_driver(&mcp23s08_driver);
  557. }
  558. #else
  559. static int __init mcp23s08_spi_init(void) { return 0; }
  560. static void mcp23s08_spi_exit(void) { }
  561. #endif /* CONFIG_SPI_MASTER */
  562. /*----------------------------------------------------------------------*/
  563. static int __init mcp23s08_init(void)
  564. {
  565. int ret;
  566. ret = mcp23s08_spi_init();
  567. if (ret)
  568. goto spi_fail;
  569. ret = mcp23s08_i2c_init();
  570. if (ret)
  571. goto i2c_fail;
  572. return 0;
  573. i2c_fail:
  574. mcp23s08_spi_exit();
  575. spi_fail:
  576. return ret;
  577. }
  578. /* register after spi/i2c postcore initcall and before
  579. * subsys initcalls that may rely on these GPIOs
  580. */
  581. subsys_initcall(mcp23s08_init);
  582. static void __exit mcp23s08_exit(void)
  583. {
  584. mcp23s08_spi_exit();
  585. mcp23s08_i2c_exit();
  586. }
  587. module_exit(mcp23s08_exit);
  588. MODULE_LICENSE("GPL");