gpio-mcp23s08.c 16 KB

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