spi_sh_msiof.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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/spi/spi.h>
  23. #include <linux/spi/spi_bitbang.h>
  24. #include <linux/spi/sh_msiof.h>
  25. #include <asm/spi.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 0
  152. * 0 1 10 10 0 1
  153. * 1 0 11 11 0 1
  154. * 1 1 11 11 1 0
  155. *
  156. * (!) Note: REDG is inverted recommended data sheet setting
  157. */
  158. sh_msiof_write(p, FCTR, 0);
  159. sh_msiof_write(p, TMDR1, 0xe2000005 | (lsb_first << 24));
  160. sh_msiof_write(p, RMDR1, 0x22000005 | (lsb_first << 24));
  161. tmp = 0xa0000000;
  162. tmp |= cpol << 30; /* TSCKIZ */
  163. tmp |= cpol << 28; /* RSCKIZ */
  164. edge = cpol ? cpha : !cpha;
  165. tmp |= edge << 27; /* TEDG */
  166. tmp |= !edge << 26; /* REDG */
  167. tmp |= (tx_hi_z ? 2 : 0) << 22; /* TXDIZ */
  168. sh_msiof_write(p, CTR, tmp);
  169. }
  170. static void sh_msiof_spi_set_mode_regs(struct sh_msiof_spi_priv *p,
  171. const void *tx_buf, void *rx_buf,
  172. int bits, int words)
  173. {
  174. unsigned long dr2;
  175. dr2 = ((bits - 1) << 24) | ((words - 1) << 16);
  176. if (tx_buf)
  177. sh_msiof_write(p, TMDR2, dr2);
  178. else
  179. sh_msiof_write(p, TMDR2, dr2 | 1);
  180. if (rx_buf)
  181. sh_msiof_write(p, RMDR2, dr2);
  182. sh_msiof_write(p, IER, STR_TEOF | STR_REOF);
  183. }
  184. static void sh_msiof_reset_str(struct sh_msiof_spi_priv *p)
  185. {
  186. sh_msiof_write(p, STR, sh_msiof_read(p, STR));
  187. }
  188. static void sh_msiof_spi_write_fifo_8(struct sh_msiof_spi_priv *p,
  189. const void *tx_buf, int words, int fs)
  190. {
  191. const unsigned char *buf_8 = tx_buf;
  192. int k;
  193. for (k = 0; k < words; k++)
  194. sh_msiof_write(p, TFDR, buf_8[k] << fs);
  195. }
  196. static void sh_msiof_spi_write_fifo_16(struct sh_msiof_spi_priv *p,
  197. const void *tx_buf, int words, int fs)
  198. {
  199. const unsigned short *buf_16 = tx_buf;
  200. int k;
  201. for (k = 0; k < words; k++)
  202. sh_msiof_write(p, TFDR, buf_16[k] << fs);
  203. }
  204. static void sh_msiof_spi_write_fifo_16u(struct sh_msiof_spi_priv *p,
  205. const void *tx_buf, int words, int fs)
  206. {
  207. const unsigned short *buf_16 = tx_buf;
  208. int k;
  209. for (k = 0; k < words; k++)
  210. sh_msiof_write(p, TFDR, get_unaligned(&buf_16[k]) << fs);
  211. }
  212. static void sh_msiof_spi_write_fifo_32(struct sh_msiof_spi_priv *p,
  213. const void *tx_buf, int words, int fs)
  214. {
  215. const unsigned int *buf_32 = tx_buf;
  216. int k;
  217. for (k = 0; k < words; k++)
  218. sh_msiof_write(p, TFDR, buf_32[k] << fs);
  219. }
  220. static void sh_msiof_spi_write_fifo_32u(struct sh_msiof_spi_priv *p,
  221. const void *tx_buf, int words, int fs)
  222. {
  223. const unsigned int *buf_32 = tx_buf;
  224. int k;
  225. for (k = 0; k < words; k++)
  226. sh_msiof_write(p, TFDR, get_unaligned(&buf_32[k]) << fs);
  227. }
  228. static void sh_msiof_spi_read_fifo_8(struct sh_msiof_spi_priv *p,
  229. void *rx_buf, int words, int fs)
  230. {
  231. unsigned char *buf_8 = rx_buf;
  232. int k;
  233. for (k = 0; k < words; k++)
  234. buf_8[k] = sh_msiof_read(p, RFDR) >> fs;
  235. }
  236. static void sh_msiof_spi_read_fifo_16(struct sh_msiof_spi_priv *p,
  237. void *rx_buf, int words, int fs)
  238. {
  239. unsigned short *buf_16 = rx_buf;
  240. int k;
  241. for (k = 0; k < words; k++)
  242. buf_16[k] = sh_msiof_read(p, RFDR) >> fs;
  243. }
  244. static void sh_msiof_spi_read_fifo_16u(struct sh_msiof_spi_priv *p,
  245. void *rx_buf, int words, int fs)
  246. {
  247. unsigned short *buf_16 = rx_buf;
  248. int k;
  249. for (k = 0; k < words; k++)
  250. put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_16[k]);
  251. }
  252. static void sh_msiof_spi_read_fifo_32(struct sh_msiof_spi_priv *p,
  253. void *rx_buf, int words, int fs)
  254. {
  255. unsigned int *buf_32 = rx_buf;
  256. int k;
  257. for (k = 0; k < words; k++)
  258. buf_32[k] = sh_msiof_read(p, RFDR) >> fs;
  259. }
  260. static void sh_msiof_spi_read_fifo_32u(struct sh_msiof_spi_priv *p,
  261. void *rx_buf, int words, int fs)
  262. {
  263. unsigned int *buf_32 = rx_buf;
  264. int k;
  265. for (k = 0; k < words; k++)
  266. put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_32[k]);
  267. }
  268. static int sh_msiof_spi_bits(struct spi_device *spi, struct spi_transfer *t)
  269. {
  270. int bits;
  271. bits = t ? t->bits_per_word : 0;
  272. bits = bits ? bits : spi->bits_per_word;
  273. return bits;
  274. }
  275. static unsigned long sh_msiof_spi_hz(struct spi_device *spi,
  276. struct spi_transfer *t)
  277. {
  278. unsigned long hz;
  279. hz = t ? t->speed_hz : 0;
  280. hz = hz ? hz : spi->max_speed_hz;
  281. return hz;
  282. }
  283. static int sh_msiof_spi_setup_transfer(struct spi_device *spi,
  284. struct spi_transfer *t)
  285. {
  286. int bits;
  287. /* noting to check hz values against since parent clock is disabled */
  288. bits = sh_msiof_spi_bits(spi, t);
  289. if (bits < 8)
  290. return -EINVAL;
  291. if (bits > 32)
  292. return -EINVAL;
  293. return spi_bitbang_setup_transfer(spi, t);
  294. }
  295. static void sh_msiof_spi_chipselect(struct spi_device *spi, int is_on)
  296. {
  297. struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master);
  298. int value;
  299. /* chip select is active low unless SPI_CS_HIGH is set */
  300. if (spi->mode & SPI_CS_HIGH)
  301. value = (is_on == BITBANG_CS_ACTIVE) ? 1 : 0;
  302. else
  303. value = (is_on == BITBANG_CS_ACTIVE) ? 0 : 1;
  304. if (is_on == BITBANG_CS_ACTIVE) {
  305. if (!test_and_set_bit(0, &p->flags)) {
  306. pm_runtime_get_sync(&p->pdev->dev);
  307. clk_enable(p->clk);
  308. }
  309. /* Configure pins before asserting CS */
  310. sh_msiof_spi_set_pin_regs(p, !!(spi->mode & SPI_CPOL),
  311. !!(spi->mode & SPI_CPHA),
  312. !!(spi->mode & SPI_3WIRE),
  313. !!(spi->mode & SPI_LSB_FIRST));
  314. }
  315. /* use spi->controller data for CS (same strategy as spi_gpio) */
  316. gpio_set_value((unsigned)spi->controller_data, value);
  317. if (is_on == BITBANG_CS_INACTIVE) {
  318. if (test_and_clear_bit(0, &p->flags)) {
  319. clk_disable(p->clk);
  320. pm_runtime_put(&p->pdev->dev);
  321. }
  322. }
  323. }
  324. static int sh_msiof_spi_txrx_once(struct sh_msiof_spi_priv *p,
  325. void (*tx_fifo)(struct sh_msiof_spi_priv *,
  326. const void *, int, int),
  327. void (*rx_fifo)(struct sh_msiof_spi_priv *,
  328. void *, int, int),
  329. const void *tx_buf, void *rx_buf,
  330. int words, int bits)
  331. {
  332. int fifo_shift;
  333. int ret;
  334. /* limit maximum word transfer to rx/tx fifo size */
  335. if (tx_buf)
  336. words = min_t(int, words, p->tx_fifo_size);
  337. if (rx_buf)
  338. words = min_t(int, words, p->rx_fifo_size);
  339. /* the fifo contents need shifting */
  340. fifo_shift = 32 - bits;
  341. /* setup msiof transfer mode registers */
  342. sh_msiof_spi_set_mode_regs(p, tx_buf, rx_buf, bits, words);
  343. /* write tx fifo */
  344. if (tx_buf)
  345. tx_fifo(p, tx_buf, words, fifo_shift);
  346. /* setup clock and rx/tx signals */
  347. ret = sh_msiof_modify_ctr_wait(p, 0, CTR_TSCKE);
  348. if (rx_buf)
  349. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_RXE);
  350. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_TXE);
  351. /* start by setting frame bit */
  352. INIT_COMPLETION(p->done);
  353. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_TFSE);
  354. if (ret) {
  355. dev_err(&p->pdev->dev, "failed to start hardware\n");
  356. goto err;
  357. }
  358. /* wait for tx fifo to be emptied / rx fifo to be filled */
  359. wait_for_completion(&p->done);
  360. /* read rx fifo */
  361. if (rx_buf)
  362. rx_fifo(p, rx_buf, words, fifo_shift);
  363. /* clear status bits */
  364. sh_msiof_reset_str(p);
  365. /* shut down frame, tx/tx and clock signals */
  366. ret = sh_msiof_modify_ctr_wait(p, CTR_TFSE, 0);
  367. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TXE, 0);
  368. if (rx_buf)
  369. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_RXE, 0);
  370. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TSCKE, 0);
  371. if (ret) {
  372. dev_err(&p->pdev->dev, "failed to shut down hardware\n");
  373. goto err;
  374. }
  375. return words;
  376. err:
  377. sh_msiof_write(p, IER, 0);
  378. return ret;
  379. }
  380. static int sh_msiof_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
  381. {
  382. struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master);
  383. void (*tx_fifo)(struct sh_msiof_spi_priv *, const void *, int, int);
  384. void (*rx_fifo)(struct sh_msiof_spi_priv *, void *, int, int);
  385. int bits;
  386. int bytes_per_word;
  387. int bytes_done;
  388. int words;
  389. int n;
  390. bits = sh_msiof_spi_bits(spi, t);
  391. /* setup bytes per word and fifo read/write functions */
  392. if (bits <= 8) {
  393. bytes_per_word = 1;
  394. tx_fifo = sh_msiof_spi_write_fifo_8;
  395. rx_fifo = sh_msiof_spi_read_fifo_8;
  396. } else if (bits <= 16) {
  397. bytes_per_word = 2;
  398. if ((unsigned long)t->tx_buf & 0x01)
  399. tx_fifo = sh_msiof_spi_write_fifo_16u;
  400. else
  401. tx_fifo = sh_msiof_spi_write_fifo_16;
  402. if ((unsigned long)t->rx_buf & 0x01)
  403. rx_fifo = sh_msiof_spi_read_fifo_16u;
  404. else
  405. rx_fifo = sh_msiof_spi_read_fifo_16;
  406. } else {
  407. bytes_per_word = 4;
  408. if ((unsigned long)t->tx_buf & 0x03)
  409. tx_fifo = sh_msiof_spi_write_fifo_32u;
  410. else
  411. tx_fifo = sh_msiof_spi_write_fifo_32;
  412. if ((unsigned long)t->rx_buf & 0x03)
  413. rx_fifo = sh_msiof_spi_read_fifo_32u;
  414. else
  415. rx_fifo = sh_msiof_spi_read_fifo_32;
  416. }
  417. /* setup clocks (clock already enabled in chipselect()) */
  418. sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk),
  419. sh_msiof_spi_hz(spi, t));
  420. /* transfer in fifo sized chunks */
  421. words = t->len / bytes_per_word;
  422. bytes_done = 0;
  423. while (bytes_done < t->len) {
  424. n = sh_msiof_spi_txrx_once(p, tx_fifo, rx_fifo,
  425. t->tx_buf + bytes_done,
  426. t->rx_buf + bytes_done,
  427. words, bits);
  428. if (n < 0)
  429. break;
  430. bytes_done += n * bytes_per_word;
  431. words -= n;
  432. }
  433. return bytes_done;
  434. }
  435. static u32 sh_msiof_spi_txrx_word(struct spi_device *spi, unsigned nsecs,
  436. u32 word, u8 bits)
  437. {
  438. BUG(); /* unused but needed by bitbang code */
  439. return 0;
  440. }
  441. static int sh_msiof_spi_probe(struct platform_device *pdev)
  442. {
  443. struct resource *r;
  444. struct spi_master *master;
  445. struct sh_msiof_spi_priv *p;
  446. char clk_name[16];
  447. int i;
  448. int ret;
  449. master = spi_alloc_master(&pdev->dev, sizeof(struct sh_msiof_spi_priv));
  450. if (master == NULL) {
  451. dev_err(&pdev->dev, "failed to allocate spi master\n");
  452. ret = -ENOMEM;
  453. goto err0;
  454. }
  455. p = spi_master_get_devdata(master);
  456. platform_set_drvdata(pdev, p);
  457. p->info = pdev->dev.platform_data;
  458. init_completion(&p->done);
  459. snprintf(clk_name, sizeof(clk_name), "msiof%d", pdev->id);
  460. p->clk = clk_get(&pdev->dev, clk_name);
  461. if (IS_ERR(p->clk)) {
  462. dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
  463. ret = PTR_ERR(p->clk);
  464. goto err1;
  465. }
  466. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  467. i = platform_get_irq(pdev, 0);
  468. if (!r || i < 0) {
  469. dev_err(&pdev->dev, "cannot get platform resources\n");
  470. ret = -ENOENT;
  471. goto err2;
  472. }
  473. p->mapbase = ioremap_nocache(r->start, resource_size(r));
  474. if (!p->mapbase) {
  475. dev_err(&pdev->dev, "unable to ioremap\n");
  476. ret = -ENXIO;
  477. goto err2;
  478. }
  479. ret = request_irq(i, sh_msiof_spi_irq, IRQF_DISABLED,
  480. dev_name(&pdev->dev), p);
  481. if (ret) {
  482. dev_err(&pdev->dev, "unable to request irq\n");
  483. goto err3;
  484. }
  485. p->pdev = pdev;
  486. pm_runtime_enable(&pdev->dev);
  487. /* The standard version of MSIOF use 64 word FIFOs */
  488. p->tx_fifo_size = 64;
  489. p->rx_fifo_size = 64;
  490. /* Platform data may override FIFO sizes */
  491. if (p->info->tx_fifo_override)
  492. p->tx_fifo_size = p->info->tx_fifo_override;
  493. if (p->info->rx_fifo_override)
  494. p->rx_fifo_size = p->info->rx_fifo_override;
  495. /* init master and bitbang code */
  496. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  497. master->mode_bits |= SPI_LSB_FIRST | SPI_3WIRE;
  498. master->flags = 0;
  499. master->bus_num = pdev->id;
  500. master->num_chipselect = p->info->num_chipselect;
  501. master->setup = spi_bitbang_setup;
  502. master->cleanup = spi_bitbang_cleanup;
  503. p->bitbang.master = master;
  504. p->bitbang.chipselect = sh_msiof_spi_chipselect;
  505. p->bitbang.setup_transfer = sh_msiof_spi_setup_transfer;
  506. p->bitbang.txrx_bufs = sh_msiof_spi_txrx;
  507. p->bitbang.txrx_word[SPI_MODE_0] = sh_msiof_spi_txrx_word;
  508. p->bitbang.txrx_word[SPI_MODE_1] = sh_msiof_spi_txrx_word;
  509. p->bitbang.txrx_word[SPI_MODE_2] = sh_msiof_spi_txrx_word;
  510. p->bitbang.txrx_word[SPI_MODE_3] = sh_msiof_spi_txrx_word;
  511. ret = spi_bitbang_start(&p->bitbang);
  512. if (ret == 0)
  513. return 0;
  514. pm_runtime_disable(&pdev->dev);
  515. err3:
  516. iounmap(p->mapbase);
  517. err2:
  518. clk_put(p->clk);
  519. err1:
  520. spi_master_put(master);
  521. err0:
  522. return ret;
  523. }
  524. static int sh_msiof_spi_remove(struct platform_device *pdev)
  525. {
  526. struct sh_msiof_spi_priv *p = platform_get_drvdata(pdev);
  527. int ret;
  528. ret = spi_bitbang_stop(&p->bitbang);
  529. if (!ret) {
  530. pm_runtime_disable(&pdev->dev);
  531. free_irq(platform_get_irq(pdev, 0), sh_msiof_spi_irq);
  532. iounmap(p->mapbase);
  533. clk_put(p->clk);
  534. spi_master_put(p->bitbang.master);
  535. }
  536. return ret;
  537. }
  538. static int sh_msiof_spi_runtime_nop(struct device *dev)
  539. {
  540. /* Runtime PM callback shared between ->runtime_suspend()
  541. * and ->runtime_resume(). Simply returns success.
  542. *
  543. * This driver re-initializes all registers after
  544. * pm_runtime_get_sync() anyway so there is no need
  545. * to save and restore registers here.
  546. */
  547. return 0;
  548. }
  549. static struct dev_pm_ops sh_msiof_spi_dev_pm_ops = {
  550. .runtime_suspend = sh_msiof_spi_runtime_nop,
  551. .runtime_resume = sh_msiof_spi_runtime_nop,
  552. };
  553. static struct platform_driver sh_msiof_spi_drv = {
  554. .probe = sh_msiof_spi_probe,
  555. .remove = sh_msiof_spi_remove,
  556. .driver = {
  557. .name = "spi_sh_msiof",
  558. .owner = THIS_MODULE,
  559. .pm = &sh_msiof_spi_dev_pm_ops,
  560. },
  561. };
  562. static int __init sh_msiof_spi_init(void)
  563. {
  564. return platform_driver_register(&sh_msiof_spi_drv);
  565. }
  566. module_init(sh_msiof_spi_init);
  567. static void __exit sh_msiof_spi_exit(void)
  568. {
  569. platform_driver_unregister(&sh_msiof_spi_drv);
  570. }
  571. module_exit(sh_msiof_spi_exit);
  572. MODULE_DESCRIPTION("SuperH MSIOF SPI Master Interface Driver");
  573. MODULE_AUTHOR("Magnus Damm");
  574. MODULE_LICENSE("GPL v2");
  575. MODULE_ALIAS("platform:spi_sh_msiof");