spi_sh_msiof.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. * SuperH MSIOF SPI Master Interface
  3. *
  4. * Copyright (c) 2009 Magnus Damm
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/delay.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/completion.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/gpio.h>
  19. #include <linux/bitmap.h>
  20. #include <linux/clk.h>
  21. #include <linux/io.h>
  22. #include <linux/err.h>
  23. #include <linux/spi/spi.h>
  24. #include <linux/spi/spi_bitbang.h>
  25. #include <linux/spi/sh_msiof.h>
  26. #include <asm/unaligned.h>
  27. struct sh_msiof_spi_priv {
  28. struct spi_bitbang bitbang; /* must be first for spi_bitbang.c */
  29. void __iomem *mapbase;
  30. struct clk *clk;
  31. struct platform_device *pdev;
  32. struct sh_msiof_spi_info *info;
  33. struct completion done;
  34. unsigned long flags;
  35. int tx_fifo_size;
  36. int rx_fifo_size;
  37. };
  38. #define TMDR1 0x00
  39. #define TMDR2 0x04
  40. #define TMDR3 0x08
  41. #define RMDR1 0x10
  42. #define RMDR2 0x14
  43. #define RMDR3 0x18
  44. #define TSCR 0x20
  45. #define RSCR 0x22
  46. #define CTR 0x28
  47. #define FCTR 0x30
  48. #define STR 0x40
  49. #define IER 0x44
  50. #define TDR1 0x48
  51. #define TDR2 0x4c
  52. #define TFDR 0x50
  53. #define RDR1 0x58
  54. #define RDR2 0x5c
  55. #define RFDR 0x60
  56. #define CTR_TSCKE (1 << 15)
  57. #define CTR_TFSE (1 << 14)
  58. #define CTR_TXE (1 << 9)
  59. #define CTR_RXE (1 << 8)
  60. #define STR_TEOF (1 << 23)
  61. #define STR_REOF (1 << 7)
  62. static unsigned long sh_msiof_read(struct sh_msiof_spi_priv *p, int reg_offs)
  63. {
  64. switch (reg_offs) {
  65. case TSCR:
  66. case RSCR:
  67. return ioread16(p->mapbase + reg_offs);
  68. default:
  69. return ioread32(p->mapbase + reg_offs);
  70. }
  71. }
  72. static void sh_msiof_write(struct sh_msiof_spi_priv *p, int reg_offs,
  73. unsigned long value)
  74. {
  75. switch (reg_offs) {
  76. case TSCR:
  77. case RSCR:
  78. iowrite16(value, p->mapbase + reg_offs);
  79. break;
  80. default:
  81. iowrite32(value, p->mapbase + reg_offs);
  82. break;
  83. }
  84. }
  85. static int sh_msiof_modify_ctr_wait(struct sh_msiof_spi_priv *p,
  86. unsigned long clr, unsigned long set)
  87. {
  88. unsigned long mask = clr | set;
  89. unsigned long data;
  90. int k;
  91. data = sh_msiof_read(p, CTR);
  92. data &= ~clr;
  93. data |= set;
  94. sh_msiof_write(p, CTR, data);
  95. for (k = 100; k > 0; k--) {
  96. if ((sh_msiof_read(p, CTR) & mask) == set)
  97. break;
  98. udelay(10);
  99. }
  100. return k > 0 ? 0 : -ETIMEDOUT;
  101. }
  102. static irqreturn_t sh_msiof_spi_irq(int irq, void *data)
  103. {
  104. struct sh_msiof_spi_priv *p = data;
  105. /* just disable the interrupt and wake up */
  106. sh_msiof_write(p, IER, 0);
  107. complete(&p->done);
  108. return IRQ_HANDLED;
  109. }
  110. static struct {
  111. unsigned short div;
  112. unsigned short scr;
  113. } const sh_msiof_spi_clk_table[] = {
  114. { 1, 0x0007 },
  115. { 2, 0x0000 },
  116. { 4, 0x0001 },
  117. { 8, 0x0002 },
  118. { 16, 0x0003 },
  119. { 32, 0x0004 },
  120. { 64, 0x1f00 },
  121. { 128, 0x1f01 },
  122. { 256, 0x1f02 },
  123. { 512, 0x1f03 },
  124. { 1024, 0x1f04 },
  125. };
  126. static void sh_msiof_spi_set_clk_regs(struct sh_msiof_spi_priv *p,
  127. unsigned long parent_rate,
  128. unsigned long spi_hz)
  129. {
  130. unsigned long div = 1024;
  131. size_t k;
  132. if (!WARN_ON(!spi_hz || !parent_rate))
  133. div = parent_rate / spi_hz;
  134. /* TODO: make more fine grained */
  135. for (k = 0; k < ARRAY_SIZE(sh_msiof_spi_clk_table); k++) {
  136. if (sh_msiof_spi_clk_table[k].div >= div)
  137. break;
  138. }
  139. k = min_t(int, k, ARRAY_SIZE(sh_msiof_spi_clk_table) - 1);
  140. sh_msiof_write(p, TSCR, sh_msiof_spi_clk_table[k].scr);
  141. sh_msiof_write(p, RSCR, sh_msiof_spi_clk_table[k].scr);
  142. }
  143. static void sh_msiof_spi_set_pin_regs(struct sh_msiof_spi_priv *p,
  144. int cpol, int cpha,
  145. int tx_hi_z, int lsb_first)
  146. {
  147. unsigned long tmp;
  148. int edge;
  149. /*
  150. * CPOL CPHA TSCKIZ RSCKIZ TEDG REDG
  151. * 0 0 10 10 1 1
  152. * 0 1 10 10 0 0
  153. * 1 0 11 11 0 0
  154. * 1 1 11 11 1 1
  155. */
  156. sh_msiof_write(p, FCTR, 0);
  157. sh_msiof_write(p, TMDR1, 0xe2000005 | (lsb_first << 24));
  158. sh_msiof_write(p, RMDR1, 0x22000005 | (lsb_first << 24));
  159. tmp = 0xa0000000;
  160. tmp |= cpol << 30; /* TSCKIZ */
  161. tmp |= cpol << 28; /* RSCKIZ */
  162. edge = cpol ? cpha : !cpha;
  163. tmp |= edge << 27; /* TEDG */
  164. tmp |= edge << 26; /* REDG */
  165. tmp |= (tx_hi_z ? 2 : 0) << 22; /* TXDIZ */
  166. sh_msiof_write(p, CTR, tmp);
  167. }
  168. static void sh_msiof_spi_set_mode_regs(struct sh_msiof_spi_priv *p,
  169. const void *tx_buf, void *rx_buf,
  170. int bits, int words)
  171. {
  172. unsigned long dr2;
  173. dr2 = ((bits - 1) << 24) | ((words - 1) << 16);
  174. if (tx_buf)
  175. sh_msiof_write(p, TMDR2, dr2);
  176. else
  177. sh_msiof_write(p, TMDR2, dr2 | 1);
  178. if (rx_buf)
  179. sh_msiof_write(p, RMDR2, dr2);
  180. sh_msiof_write(p, IER, STR_TEOF | STR_REOF);
  181. }
  182. static void sh_msiof_reset_str(struct sh_msiof_spi_priv *p)
  183. {
  184. sh_msiof_write(p, STR, sh_msiof_read(p, STR));
  185. }
  186. static void sh_msiof_spi_write_fifo_8(struct sh_msiof_spi_priv *p,
  187. const void *tx_buf, int words, int fs)
  188. {
  189. const unsigned char *buf_8 = tx_buf;
  190. int k;
  191. for (k = 0; k < words; k++)
  192. sh_msiof_write(p, TFDR, buf_8[k] << fs);
  193. }
  194. static void sh_msiof_spi_write_fifo_16(struct sh_msiof_spi_priv *p,
  195. const void *tx_buf, int words, int fs)
  196. {
  197. const unsigned short *buf_16 = tx_buf;
  198. int k;
  199. for (k = 0; k < words; k++)
  200. sh_msiof_write(p, TFDR, buf_16[k] << fs);
  201. }
  202. static void sh_msiof_spi_write_fifo_16u(struct sh_msiof_spi_priv *p,
  203. const void *tx_buf, int words, int fs)
  204. {
  205. const unsigned short *buf_16 = tx_buf;
  206. int k;
  207. for (k = 0; k < words; k++)
  208. sh_msiof_write(p, TFDR, get_unaligned(&buf_16[k]) << fs);
  209. }
  210. static void sh_msiof_spi_write_fifo_32(struct sh_msiof_spi_priv *p,
  211. const void *tx_buf, int words, int fs)
  212. {
  213. const unsigned int *buf_32 = tx_buf;
  214. int k;
  215. for (k = 0; k < words; k++)
  216. sh_msiof_write(p, TFDR, buf_32[k] << fs);
  217. }
  218. static void sh_msiof_spi_write_fifo_32u(struct sh_msiof_spi_priv *p,
  219. const void *tx_buf, int words, int fs)
  220. {
  221. const unsigned int *buf_32 = tx_buf;
  222. int k;
  223. for (k = 0; k < words; k++)
  224. sh_msiof_write(p, TFDR, get_unaligned(&buf_32[k]) << fs);
  225. }
  226. static void sh_msiof_spi_read_fifo_8(struct sh_msiof_spi_priv *p,
  227. void *rx_buf, int words, int fs)
  228. {
  229. unsigned char *buf_8 = rx_buf;
  230. int k;
  231. for (k = 0; k < words; k++)
  232. buf_8[k] = sh_msiof_read(p, RFDR) >> fs;
  233. }
  234. static void sh_msiof_spi_read_fifo_16(struct sh_msiof_spi_priv *p,
  235. void *rx_buf, int words, int fs)
  236. {
  237. unsigned short *buf_16 = rx_buf;
  238. int k;
  239. for (k = 0; k < words; k++)
  240. buf_16[k] = sh_msiof_read(p, RFDR) >> fs;
  241. }
  242. static void sh_msiof_spi_read_fifo_16u(struct sh_msiof_spi_priv *p,
  243. void *rx_buf, int words, int fs)
  244. {
  245. unsigned short *buf_16 = rx_buf;
  246. int k;
  247. for (k = 0; k < words; k++)
  248. put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_16[k]);
  249. }
  250. static void sh_msiof_spi_read_fifo_32(struct sh_msiof_spi_priv *p,
  251. void *rx_buf, int words, int fs)
  252. {
  253. unsigned int *buf_32 = rx_buf;
  254. int k;
  255. for (k = 0; k < words; k++)
  256. buf_32[k] = sh_msiof_read(p, RFDR) >> fs;
  257. }
  258. static void sh_msiof_spi_read_fifo_32u(struct sh_msiof_spi_priv *p,
  259. void *rx_buf, int words, int fs)
  260. {
  261. unsigned int *buf_32 = rx_buf;
  262. int k;
  263. for (k = 0; k < words; k++)
  264. put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_32[k]);
  265. }
  266. static int sh_msiof_spi_bits(struct spi_device *spi, struct spi_transfer *t)
  267. {
  268. int bits;
  269. bits = t ? t->bits_per_word : 0;
  270. bits = bits ? bits : spi->bits_per_word;
  271. return bits;
  272. }
  273. static unsigned long sh_msiof_spi_hz(struct spi_device *spi,
  274. struct spi_transfer *t)
  275. {
  276. unsigned long hz;
  277. hz = t ? t->speed_hz : 0;
  278. hz = hz ? hz : spi->max_speed_hz;
  279. return hz;
  280. }
  281. static int sh_msiof_spi_setup_transfer(struct spi_device *spi,
  282. struct spi_transfer *t)
  283. {
  284. int bits;
  285. /* noting to check hz values against since parent clock is disabled */
  286. bits = sh_msiof_spi_bits(spi, t);
  287. if (bits < 8)
  288. return -EINVAL;
  289. if (bits > 32)
  290. return -EINVAL;
  291. return spi_bitbang_setup_transfer(spi, t);
  292. }
  293. static void sh_msiof_spi_chipselect(struct spi_device *spi, int is_on)
  294. {
  295. struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master);
  296. int value;
  297. /* chip select is active low unless SPI_CS_HIGH is set */
  298. if (spi->mode & SPI_CS_HIGH)
  299. value = (is_on == BITBANG_CS_ACTIVE) ? 1 : 0;
  300. else
  301. value = (is_on == BITBANG_CS_ACTIVE) ? 0 : 1;
  302. if (is_on == BITBANG_CS_ACTIVE) {
  303. if (!test_and_set_bit(0, &p->flags)) {
  304. pm_runtime_get_sync(&p->pdev->dev);
  305. clk_enable(p->clk);
  306. }
  307. /* Configure pins before asserting CS */
  308. sh_msiof_spi_set_pin_regs(p, !!(spi->mode & SPI_CPOL),
  309. !!(spi->mode & SPI_CPHA),
  310. !!(spi->mode & SPI_3WIRE),
  311. !!(spi->mode & SPI_LSB_FIRST));
  312. }
  313. /* use spi->controller data for CS (same strategy as spi_gpio) */
  314. gpio_set_value((unsigned)spi->controller_data, value);
  315. if (is_on == BITBANG_CS_INACTIVE) {
  316. if (test_and_clear_bit(0, &p->flags)) {
  317. clk_disable(p->clk);
  318. pm_runtime_put(&p->pdev->dev);
  319. }
  320. }
  321. }
  322. static int sh_msiof_spi_txrx_once(struct sh_msiof_spi_priv *p,
  323. void (*tx_fifo)(struct sh_msiof_spi_priv *,
  324. const void *, int, int),
  325. void (*rx_fifo)(struct sh_msiof_spi_priv *,
  326. void *, int, int),
  327. const void *tx_buf, void *rx_buf,
  328. int words, int bits)
  329. {
  330. int fifo_shift;
  331. int ret;
  332. /* limit maximum word transfer to rx/tx fifo size */
  333. if (tx_buf)
  334. words = min_t(int, words, p->tx_fifo_size);
  335. if (rx_buf)
  336. words = min_t(int, words, p->rx_fifo_size);
  337. /* the fifo contents need shifting */
  338. fifo_shift = 32 - bits;
  339. /* setup msiof transfer mode registers */
  340. sh_msiof_spi_set_mode_regs(p, tx_buf, rx_buf, bits, words);
  341. /* write tx fifo */
  342. if (tx_buf)
  343. tx_fifo(p, tx_buf, words, fifo_shift);
  344. /* setup clock and rx/tx signals */
  345. ret = sh_msiof_modify_ctr_wait(p, 0, CTR_TSCKE);
  346. if (rx_buf)
  347. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_RXE);
  348. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_TXE);
  349. /* start by setting frame bit */
  350. INIT_COMPLETION(p->done);
  351. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_TFSE);
  352. if (ret) {
  353. dev_err(&p->pdev->dev, "failed to start hardware\n");
  354. goto err;
  355. }
  356. /* wait for tx fifo to be emptied / rx fifo to be filled */
  357. wait_for_completion(&p->done);
  358. /* read rx fifo */
  359. if (rx_buf)
  360. rx_fifo(p, rx_buf, words, fifo_shift);
  361. /* clear status bits */
  362. sh_msiof_reset_str(p);
  363. /* shut down frame, tx/tx and clock signals */
  364. ret = sh_msiof_modify_ctr_wait(p, CTR_TFSE, 0);
  365. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TXE, 0);
  366. if (rx_buf)
  367. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_RXE, 0);
  368. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TSCKE, 0);
  369. if (ret) {
  370. dev_err(&p->pdev->dev, "failed to shut down hardware\n");
  371. goto err;
  372. }
  373. return words;
  374. err:
  375. sh_msiof_write(p, IER, 0);
  376. return ret;
  377. }
  378. static int sh_msiof_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
  379. {
  380. struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master);
  381. void (*tx_fifo)(struct sh_msiof_spi_priv *, const void *, int, int);
  382. void (*rx_fifo)(struct sh_msiof_spi_priv *, void *, int, int);
  383. int bits;
  384. int bytes_per_word;
  385. int bytes_done;
  386. int words;
  387. int n;
  388. bits = sh_msiof_spi_bits(spi, t);
  389. /* setup bytes per word and fifo read/write functions */
  390. if (bits <= 8) {
  391. bytes_per_word = 1;
  392. tx_fifo = sh_msiof_spi_write_fifo_8;
  393. rx_fifo = sh_msiof_spi_read_fifo_8;
  394. } else if (bits <= 16) {
  395. bytes_per_word = 2;
  396. if ((unsigned long)t->tx_buf & 0x01)
  397. tx_fifo = sh_msiof_spi_write_fifo_16u;
  398. else
  399. tx_fifo = sh_msiof_spi_write_fifo_16;
  400. if ((unsigned long)t->rx_buf & 0x01)
  401. rx_fifo = sh_msiof_spi_read_fifo_16u;
  402. else
  403. rx_fifo = sh_msiof_spi_read_fifo_16;
  404. } else {
  405. bytes_per_word = 4;
  406. if ((unsigned long)t->tx_buf & 0x03)
  407. tx_fifo = sh_msiof_spi_write_fifo_32u;
  408. else
  409. tx_fifo = sh_msiof_spi_write_fifo_32;
  410. if ((unsigned long)t->rx_buf & 0x03)
  411. rx_fifo = sh_msiof_spi_read_fifo_32u;
  412. else
  413. rx_fifo = sh_msiof_spi_read_fifo_32;
  414. }
  415. /* setup clocks (clock already enabled in chipselect()) */
  416. sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk),
  417. sh_msiof_spi_hz(spi, t));
  418. /* transfer in fifo sized chunks */
  419. words = t->len / bytes_per_word;
  420. bytes_done = 0;
  421. while (bytes_done < t->len) {
  422. n = sh_msiof_spi_txrx_once(p, tx_fifo, rx_fifo,
  423. t->tx_buf + bytes_done,
  424. t->rx_buf + bytes_done,
  425. words, bits);
  426. if (n < 0)
  427. break;
  428. bytes_done += n * bytes_per_word;
  429. words -= n;
  430. }
  431. return bytes_done;
  432. }
  433. static u32 sh_msiof_spi_txrx_word(struct spi_device *spi, unsigned nsecs,
  434. u32 word, u8 bits)
  435. {
  436. BUG(); /* unused but needed by bitbang code */
  437. return 0;
  438. }
  439. static int sh_msiof_spi_probe(struct platform_device *pdev)
  440. {
  441. struct resource *r;
  442. struct spi_master *master;
  443. struct sh_msiof_spi_priv *p;
  444. char clk_name[16];
  445. int i;
  446. int ret;
  447. master = spi_alloc_master(&pdev->dev, sizeof(struct sh_msiof_spi_priv));
  448. if (master == NULL) {
  449. dev_err(&pdev->dev, "failed to allocate spi master\n");
  450. ret = -ENOMEM;
  451. goto err0;
  452. }
  453. p = spi_master_get_devdata(master);
  454. platform_set_drvdata(pdev, p);
  455. p->info = pdev->dev.platform_data;
  456. init_completion(&p->done);
  457. snprintf(clk_name, sizeof(clk_name), "msiof%d", pdev->id);
  458. p->clk = clk_get(&pdev->dev, clk_name);
  459. if (IS_ERR(p->clk)) {
  460. dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
  461. ret = PTR_ERR(p->clk);
  462. goto err1;
  463. }
  464. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  465. i = platform_get_irq(pdev, 0);
  466. if (!r || i < 0) {
  467. dev_err(&pdev->dev, "cannot get platform resources\n");
  468. ret = -ENOENT;
  469. goto err2;
  470. }
  471. p->mapbase = ioremap_nocache(r->start, resource_size(r));
  472. if (!p->mapbase) {
  473. dev_err(&pdev->dev, "unable to ioremap\n");
  474. ret = -ENXIO;
  475. goto err2;
  476. }
  477. ret = request_irq(i, sh_msiof_spi_irq, IRQF_DISABLED,
  478. dev_name(&pdev->dev), p);
  479. if (ret) {
  480. dev_err(&pdev->dev, "unable to request irq\n");
  481. goto err3;
  482. }
  483. p->pdev = pdev;
  484. pm_runtime_enable(&pdev->dev);
  485. /* The standard version of MSIOF use 64 word FIFOs */
  486. p->tx_fifo_size = 64;
  487. p->rx_fifo_size = 64;
  488. /* Platform data may override FIFO sizes */
  489. if (p->info->tx_fifo_override)
  490. p->tx_fifo_size = p->info->tx_fifo_override;
  491. if (p->info->rx_fifo_override)
  492. p->rx_fifo_size = p->info->rx_fifo_override;
  493. /* init master and bitbang code */
  494. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  495. master->mode_bits |= SPI_LSB_FIRST | SPI_3WIRE;
  496. master->flags = 0;
  497. master->bus_num = pdev->id;
  498. master->num_chipselect = p->info->num_chipselect;
  499. master->setup = spi_bitbang_setup;
  500. master->cleanup = spi_bitbang_cleanup;
  501. p->bitbang.master = master;
  502. p->bitbang.chipselect = sh_msiof_spi_chipselect;
  503. p->bitbang.setup_transfer = sh_msiof_spi_setup_transfer;
  504. p->bitbang.txrx_bufs = sh_msiof_spi_txrx;
  505. p->bitbang.txrx_word[SPI_MODE_0] = sh_msiof_spi_txrx_word;
  506. p->bitbang.txrx_word[SPI_MODE_1] = sh_msiof_spi_txrx_word;
  507. p->bitbang.txrx_word[SPI_MODE_2] = sh_msiof_spi_txrx_word;
  508. p->bitbang.txrx_word[SPI_MODE_3] = sh_msiof_spi_txrx_word;
  509. ret = spi_bitbang_start(&p->bitbang);
  510. if (ret == 0)
  511. return 0;
  512. pm_runtime_disable(&pdev->dev);
  513. err3:
  514. iounmap(p->mapbase);
  515. err2:
  516. clk_put(p->clk);
  517. err1:
  518. spi_master_put(master);
  519. err0:
  520. return ret;
  521. }
  522. static int sh_msiof_spi_remove(struct platform_device *pdev)
  523. {
  524. struct sh_msiof_spi_priv *p = platform_get_drvdata(pdev);
  525. int ret;
  526. ret = spi_bitbang_stop(&p->bitbang);
  527. if (!ret) {
  528. pm_runtime_disable(&pdev->dev);
  529. free_irq(platform_get_irq(pdev, 0), sh_msiof_spi_irq);
  530. iounmap(p->mapbase);
  531. clk_put(p->clk);
  532. spi_master_put(p->bitbang.master);
  533. }
  534. return ret;
  535. }
  536. static int sh_msiof_spi_runtime_nop(struct device *dev)
  537. {
  538. /* Runtime PM callback shared between ->runtime_suspend()
  539. * and ->runtime_resume(). Simply returns success.
  540. *
  541. * This driver re-initializes all registers after
  542. * pm_runtime_get_sync() anyway so there is no need
  543. * to save and restore registers here.
  544. */
  545. return 0;
  546. }
  547. static struct dev_pm_ops sh_msiof_spi_dev_pm_ops = {
  548. .runtime_suspend = sh_msiof_spi_runtime_nop,
  549. .runtime_resume = sh_msiof_spi_runtime_nop,
  550. };
  551. static struct platform_driver sh_msiof_spi_drv = {
  552. .probe = sh_msiof_spi_probe,
  553. .remove = sh_msiof_spi_remove,
  554. .driver = {
  555. .name = "spi_sh_msiof",
  556. .owner = THIS_MODULE,
  557. .pm = &sh_msiof_spi_dev_pm_ops,
  558. },
  559. };
  560. static int __init sh_msiof_spi_init(void)
  561. {
  562. return platform_driver_register(&sh_msiof_spi_drv);
  563. }
  564. module_init(sh_msiof_spi_init);
  565. static void __exit sh_msiof_spi_exit(void)
  566. {
  567. platform_driver_unregister(&sh_msiof_spi_drv);
  568. }
  569. module_exit(sh_msiof_spi_exit);
  570. MODULE_DESCRIPTION("SuperH MSIOF SPI Master Interface Driver");
  571. MODULE_AUTHOR("Magnus Damm");
  572. MODULE_LICENSE("GPL v2");
  573. MODULE_ALIAS("platform:spi_sh_msiof");