spi_s3c24xx.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /* linux/drivers/spi/spi_s3c24xx.c
  2. *
  3. * Copyright (c) 2006 Ben Dooks
  4. * Copyright 2006-2009 Simtec Electronics
  5. * Ben Dooks <ben@simtec.co.uk>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/init.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/delay.h>
  17. #include <linux/errno.h>
  18. #include <linux/err.h>
  19. #include <linux/clk.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/gpio.h>
  22. #include <linux/io.h>
  23. #include <linux/slab.h>
  24. #include <linux/spi/spi.h>
  25. #include <linux/spi/spi_bitbang.h>
  26. #include <plat/regs-spi.h>
  27. #include <mach/spi.h>
  28. #include <plat/fiq.h>
  29. #include <asm/fiq.h>
  30. #include "spi_s3c24xx_fiq.h"
  31. /**
  32. * s3c24xx_spi_devstate - per device data
  33. * @hz: Last frequency calculated for @sppre field.
  34. * @mode: Last mode setting for the @spcon field.
  35. * @spcon: Value to write to the SPCON register.
  36. * @sppre: Value to write to the SPPRE register.
  37. */
  38. struct s3c24xx_spi_devstate {
  39. unsigned int hz;
  40. unsigned int mode;
  41. u8 spcon;
  42. u8 sppre;
  43. };
  44. enum spi_fiq_mode {
  45. FIQ_MODE_NONE = 0,
  46. FIQ_MODE_TX = 1,
  47. FIQ_MODE_RX = 2,
  48. FIQ_MODE_TXRX = 3,
  49. };
  50. struct s3c24xx_spi {
  51. /* bitbang has to be first */
  52. struct spi_bitbang bitbang;
  53. struct completion done;
  54. void __iomem *regs;
  55. int irq;
  56. int len;
  57. int count;
  58. struct fiq_handler fiq_handler;
  59. enum spi_fiq_mode fiq_mode;
  60. unsigned char fiq_inuse;
  61. unsigned char fiq_claimed;
  62. void (*set_cs)(struct s3c2410_spi_info *spi,
  63. int cs, int pol);
  64. /* data buffers */
  65. const unsigned char *tx;
  66. unsigned char *rx;
  67. struct clk *clk;
  68. struct resource *ioarea;
  69. struct spi_master *master;
  70. struct spi_device *curdev;
  71. struct device *dev;
  72. struct s3c2410_spi_info *pdata;
  73. };
  74. #define SPCON_DEFAULT (S3C2410_SPCON_MSTR | S3C2410_SPCON_SMOD_INT)
  75. #define SPPIN_DEFAULT (S3C2410_SPPIN_KEEP)
  76. static inline struct s3c24xx_spi *to_hw(struct spi_device *sdev)
  77. {
  78. return spi_master_get_devdata(sdev->master);
  79. }
  80. static void s3c24xx_spi_gpiocs(struct s3c2410_spi_info *spi, int cs, int pol)
  81. {
  82. gpio_set_value(spi->pin_cs, pol);
  83. }
  84. static void s3c24xx_spi_chipsel(struct spi_device *spi, int value)
  85. {
  86. struct s3c24xx_spi_devstate *cs = spi->controller_state;
  87. struct s3c24xx_spi *hw = to_hw(spi);
  88. unsigned int cspol = spi->mode & SPI_CS_HIGH ? 1 : 0;
  89. /* change the chipselect state and the state of the spi engine clock */
  90. switch (value) {
  91. case BITBANG_CS_INACTIVE:
  92. hw->set_cs(hw->pdata, spi->chip_select, cspol^1);
  93. writeb(cs->spcon, hw->regs + S3C2410_SPCON);
  94. break;
  95. case BITBANG_CS_ACTIVE:
  96. writeb(cs->spcon | S3C2410_SPCON_ENSCK,
  97. hw->regs + S3C2410_SPCON);
  98. hw->set_cs(hw->pdata, spi->chip_select, cspol);
  99. break;
  100. }
  101. }
  102. static int s3c24xx_spi_update_state(struct spi_device *spi,
  103. struct spi_transfer *t)
  104. {
  105. struct s3c24xx_spi *hw = to_hw(spi);
  106. struct s3c24xx_spi_devstate *cs = spi->controller_state;
  107. unsigned int bpw;
  108. unsigned int hz;
  109. unsigned int div;
  110. unsigned long clk;
  111. bpw = t ? t->bits_per_word : spi->bits_per_word;
  112. hz = t ? t->speed_hz : spi->max_speed_hz;
  113. if (!bpw)
  114. bpw = 8;
  115. if (!hz)
  116. hz = spi->max_speed_hz;
  117. if (bpw != 8) {
  118. dev_err(&spi->dev, "invalid bits-per-word (%d)\n", bpw);
  119. return -EINVAL;
  120. }
  121. if (spi->mode != cs->mode) {
  122. u8 spcon = SPCON_DEFAULT | S3C2410_SPCON_ENSCK;
  123. if (spi->mode & SPI_CPHA)
  124. spcon |= S3C2410_SPCON_CPHA_FMTB;
  125. if (spi->mode & SPI_CPOL)
  126. spcon |= S3C2410_SPCON_CPOL_HIGH;
  127. cs->mode = spi->mode;
  128. cs->spcon = spcon;
  129. }
  130. if (cs->hz != hz) {
  131. clk = clk_get_rate(hw->clk);
  132. div = DIV_ROUND_UP(clk, hz * 2) - 1;
  133. if (div > 255)
  134. div = 255;
  135. dev_dbg(&spi->dev, "pre-scaler=%d (wanted %d, got %ld)\n",
  136. div, hz, clk / (2 * (div + 1)));
  137. cs->hz = hz;
  138. cs->sppre = div;
  139. }
  140. return 0;
  141. }
  142. static int s3c24xx_spi_setupxfer(struct spi_device *spi,
  143. struct spi_transfer *t)
  144. {
  145. struct s3c24xx_spi_devstate *cs = spi->controller_state;
  146. struct s3c24xx_spi *hw = to_hw(spi);
  147. int ret;
  148. ret = s3c24xx_spi_update_state(spi, t);
  149. if (!ret)
  150. writeb(cs->sppre, hw->regs + S3C2410_SPPRE);
  151. return ret;
  152. }
  153. static int s3c24xx_spi_setup(struct spi_device *spi)
  154. {
  155. struct s3c24xx_spi_devstate *cs = spi->controller_state;
  156. struct s3c24xx_spi *hw = to_hw(spi);
  157. int ret;
  158. /* allocate settings on the first call */
  159. if (!cs) {
  160. cs = kzalloc(sizeof(struct s3c24xx_spi_devstate), GFP_KERNEL);
  161. if (!cs) {
  162. dev_err(&spi->dev, "no memory for controller state\n");
  163. return -ENOMEM;
  164. }
  165. cs->spcon = SPCON_DEFAULT;
  166. cs->hz = -1;
  167. spi->controller_state = cs;
  168. }
  169. /* initialise the state from the device */
  170. ret = s3c24xx_spi_update_state(spi, NULL);
  171. if (ret)
  172. return ret;
  173. spin_lock(&hw->bitbang.lock);
  174. if (!hw->bitbang.busy) {
  175. hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE);
  176. /* need to ndelay for 0.5 clocktick ? */
  177. }
  178. spin_unlock(&hw->bitbang.lock);
  179. return 0;
  180. }
  181. static void s3c24xx_spi_cleanup(struct spi_device *spi)
  182. {
  183. kfree(spi->controller_state);
  184. }
  185. static inline unsigned int hw_txbyte(struct s3c24xx_spi *hw, int count)
  186. {
  187. return hw->tx ? hw->tx[count] : 0;
  188. }
  189. #ifdef CONFIG_SPI_S3C24XX_FIQ
  190. /* Support for FIQ based pseudo-DMA to improve the transfer speed.
  191. *
  192. * This code uses the assembly helper in spi_s3c24xx_spi.S which is
  193. * used by the FIQ core to move data between main memory and the peripheral
  194. * block. Since this is code running on the processor, there is no problem
  195. * with cache coherency of the buffers, so we can use any buffer we like.
  196. */
  197. /**
  198. * struct spi_fiq_code - FIQ code and header
  199. * @length: The length of the code fragment, excluding this header.
  200. * @ack_offset: The offset from @data to the word to place the IRQ ACK bit at.
  201. * @data: The code itself to install as a FIQ handler.
  202. */
  203. struct spi_fiq_code {
  204. u32 length;
  205. u32 ack_offset;
  206. u8 data[0];
  207. };
  208. extern struct spi_fiq_code s3c24xx_spi_fiq_txrx;
  209. extern struct spi_fiq_code s3c24xx_spi_fiq_tx;
  210. extern struct spi_fiq_code s3c24xx_spi_fiq_rx;
  211. /**
  212. * ack_bit - turn IRQ into IRQ acknowledgement bit
  213. * @irq: The interrupt number
  214. *
  215. * Returns the bit to write to the interrupt acknowledge register.
  216. */
  217. static inline u32 ack_bit(unsigned int irq)
  218. {
  219. return 1 << (irq - IRQ_EINT0);
  220. }
  221. /**
  222. * s3c24xx_spi_tryfiq - attempt to claim and setup FIQ for transfer
  223. * @hw: The hardware state.
  224. *
  225. * Claim the FIQ handler (only one can be active at any one time) and
  226. * then setup the correct transfer code for this transfer.
  227. *
  228. * This call updates all the necessary state information if successful,
  229. * so the caller does not need to do anything more than start the transfer
  230. * as normal, since the IRQ will have been re-routed to the FIQ handler.
  231. */
  232. void s3c24xx_spi_tryfiq(struct s3c24xx_spi *hw)
  233. {
  234. struct pt_regs regs;
  235. enum spi_fiq_mode mode;
  236. struct spi_fiq_code *code;
  237. int ret;
  238. if (!hw->fiq_claimed) {
  239. /* try and claim fiq if we haven't got it, and if not
  240. * then return and simply use another transfer method */
  241. ret = claim_fiq(&hw->fiq_handler);
  242. if (ret)
  243. return;
  244. }
  245. if (hw->tx && !hw->rx)
  246. mode = FIQ_MODE_TX;
  247. else if (hw->rx && !hw->tx)
  248. mode = FIQ_MODE_RX;
  249. else
  250. mode = FIQ_MODE_TXRX;
  251. regs.uregs[fiq_rspi] = (long)hw->regs;
  252. regs.uregs[fiq_rrx] = (long)hw->rx;
  253. regs.uregs[fiq_rtx] = (long)hw->tx + 1;
  254. regs.uregs[fiq_rcount] = hw->len - 1;
  255. regs.uregs[fiq_rirq] = (long)S3C24XX_VA_IRQ;
  256. set_fiq_regs(&regs);
  257. if (hw->fiq_mode != mode) {
  258. u32 *ack_ptr;
  259. hw->fiq_mode = mode;
  260. switch (mode) {
  261. case FIQ_MODE_TX:
  262. code = &s3c24xx_spi_fiq_tx;
  263. break;
  264. case FIQ_MODE_RX:
  265. code = &s3c24xx_spi_fiq_rx;
  266. break;
  267. case FIQ_MODE_TXRX:
  268. code = &s3c24xx_spi_fiq_txrx;
  269. break;
  270. default:
  271. code = NULL;
  272. }
  273. BUG_ON(!code);
  274. ack_ptr = (u32 *)&code->data[code->ack_offset];
  275. *ack_ptr = ack_bit(hw->irq);
  276. set_fiq_handler(&code->data, code->length);
  277. }
  278. s3c24xx_set_fiq(hw->irq, true);
  279. hw->fiq_mode = mode;
  280. hw->fiq_inuse = 1;
  281. }
  282. /**
  283. * s3c24xx_spi_fiqop - FIQ core code callback
  284. * @pw: Data registered with the handler
  285. * @release: Whether this is a release or a return.
  286. *
  287. * Called by the FIQ code when another module wants to use the FIQ, so
  288. * return whether we are currently using this or not and then update our
  289. * internal state.
  290. */
  291. static int s3c24xx_spi_fiqop(void *pw, int release)
  292. {
  293. struct s3c24xx_spi *hw = pw;
  294. int ret = 0;
  295. if (release) {
  296. if (hw->fiq_inuse)
  297. ret = -EBUSY;
  298. /* note, we do not need to unroute the FIQ, as the FIQ
  299. * vector code de-routes it to signal the end of transfer */
  300. hw->fiq_mode = FIQ_MODE_NONE;
  301. hw->fiq_claimed = 0;
  302. } else {
  303. hw->fiq_claimed = 1;
  304. }
  305. return ret;
  306. }
  307. /**
  308. * s3c24xx_spi_initfiq - setup the information for the FIQ core
  309. * @hw: The hardware state.
  310. *
  311. * Setup the fiq_handler block to pass to the FIQ core.
  312. */
  313. static inline void s3c24xx_spi_initfiq(struct s3c24xx_spi *hw)
  314. {
  315. hw->fiq_handler.dev_id = hw;
  316. hw->fiq_handler.name = dev_name(hw->dev);
  317. hw->fiq_handler.fiq_op = s3c24xx_spi_fiqop;
  318. }
  319. /**
  320. * s3c24xx_spi_usefiq - return if we should be using FIQ.
  321. * @hw: The hardware state.
  322. *
  323. * Return true if the platform data specifies whether this channel is
  324. * allowed to use the FIQ.
  325. */
  326. static inline bool s3c24xx_spi_usefiq(struct s3c24xx_spi *hw)
  327. {
  328. return hw->pdata->use_fiq;
  329. }
  330. /**
  331. * s3c24xx_spi_usingfiq - return if channel is using FIQ
  332. * @spi: The hardware state.
  333. *
  334. * Return whether the channel is currently using the FIQ (separate from
  335. * whether the FIQ is claimed).
  336. */
  337. static inline bool s3c24xx_spi_usingfiq(struct s3c24xx_spi *spi)
  338. {
  339. return spi->fiq_inuse;
  340. }
  341. #else
  342. static inline void s3c24xx_spi_initfiq(struct s3c24xx_spi *s) { }
  343. static inline void s3c24xx_spi_tryfiq(struct s3c24xx_spi *s) { }
  344. static inline bool s3c24xx_spi_usefiq(struct s3c24xx_spi *s) { return false; }
  345. static inline bool s3c24xx_spi_usingfiq(struct s3c24xx_spi *s) { return false; }
  346. #endif /* CONFIG_SPI_S3C24XX_FIQ */
  347. static int s3c24xx_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
  348. {
  349. struct s3c24xx_spi *hw = to_hw(spi);
  350. hw->tx = t->tx_buf;
  351. hw->rx = t->rx_buf;
  352. hw->len = t->len;
  353. hw->count = 0;
  354. init_completion(&hw->done);
  355. hw->fiq_inuse = 0;
  356. if (s3c24xx_spi_usefiq(hw) && t->len >= 3)
  357. s3c24xx_spi_tryfiq(hw);
  358. /* send the first byte */
  359. writeb(hw_txbyte(hw, 0), hw->regs + S3C2410_SPTDAT);
  360. wait_for_completion(&hw->done);
  361. return hw->count;
  362. }
  363. static irqreturn_t s3c24xx_spi_irq(int irq, void *dev)
  364. {
  365. struct s3c24xx_spi *hw = dev;
  366. unsigned int spsta = readb(hw->regs + S3C2410_SPSTA);
  367. unsigned int count = hw->count;
  368. if (spsta & S3C2410_SPSTA_DCOL) {
  369. dev_dbg(hw->dev, "data-collision\n");
  370. complete(&hw->done);
  371. goto irq_done;
  372. }
  373. if (!(spsta & S3C2410_SPSTA_READY)) {
  374. dev_dbg(hw->dev, "spi not ready for tx?\n");
  375. complete(&hw->done);
  376. goto irq_done;
  377. }
  378. if (!s3c24xx_spi_usingfiq(hw)) {
  379. hw->count++;
  380. if (hw->rx)
  381. hw->rx[count] = readb(hw->regs + S3C2410_SPRDAT);
  382. count++;
  383. if (count < hw->len)
  384. writeb(hw_txbyte(hw, count), hw->regs + S3C2410_SPTDAT);
  385. else
  386. complete(&hw->done);
  387. } else {
  388. hw->count = hw->len;
  389. hw->fiq_inuse = 0;
  390. if (hw->rx)
  391. hw->rx[hw->len-1] = readb(hw->regs + S3C2410_SPRDAT);
  392. complete(&hw->done);
  393. }
  394. irq_done:
  395. return IRQ_HANDLED;
  396. }
  397. static void s3c24xx_spi_initialsetup(struct s3c24xx_spi *hw)
  398. {
  399. /* for the moment, permanently enable the clock */
  400. clk_enable(hw->clk);
  401. /* program defaults into the registers */
  402. writeb(0xff, hw->regs + S3C2410_SPPRE);
  403. writeb(SPPIN_DEFAULT, hw->regs + S3C2410_SPPIN);
  404. writeb(SPCON_DEFAULT, hw->regs + S3C2410_SPCON);
  405. if (hw->pdata) {
  406. if (hw->set_cs == s3c24xx_spi_gpiocs)
  407. gpio_direction_output(hw->pdata->pin_cs, 1);
  408. if (hw->pdata->gpio_setup)
  409. hw->pdata->gpio_setup(hw->pdata, 1);
  410. }
  411. }
  412. static int __init s3c24xx_spi_probe(struct platform_device *pdev)
  413. {
  414. struct s3c2410_spi_info *pdata;
  415. struct s3c24xx_spi *hw;
  416. struct spi_master *master;
  417. struct resource *res;
  418. int err = 0;
  419. master = spi_alloc_master(&pdev->dev, sizeof(struct s3c24xx_spi));
  420. if (master == NULL) {
  421. dev_err(&pdev->dev, "No memory for spi_master\n");
  422. err = -ENOMEM;
  423. goto err_nomem;
  424. }
  425. hw = spi_master_get_devdata(master);
  426. memset(hw, 0, sizeof(struct s3c24xx_spi));
  427. hw->master = spi_master_get(master);
  428. hw->pdata = pdata = pdev->dev.platform_data;
  429. hw->dev = &pdev->dev;
  430. if (pdata == NULL) {
  431. dev_err(&pdev->dev, "No platform data supplied\n");
  432. err = -ENOENT;
  433. goto err_no_pdata;
  434. }
  435. platform_set_drvdata(pdev, hw);
  436. init_completion(&hw->done);
  437. /* initialise fiq handler */
  438. s3c24xx_spi_initfiq(hw);
  439. /* setup the master state. */
  440. /* the spi->mode bits understood by this driver: */
  441. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  442. master->num_chipselect = hw->pdata->num_cs;
  443. master->bus_num = pdata->bus_num;
  444. /* setup the state for the bitbang driver */
  445. hw->bitbang.master = hw->master;
  446. hw->bitbang.setup_transfer = s3c24xx_spi_setupxfer;
  447. hw->bitbang.chipselect = s3c24xx_spi_chipsel;
  448. hw->bitbang.txrx_bufs = s3c24xx_spi_txrx;
  449. hw->master->setup = s3c24xx_spi_setup;
  450. hw->master->cleanup = s3c24xx_spi_cleanup;
  451. dev_dbg(hw->dev, "bitbang at %p\n", &hw->bitbang);
  452. /* find and map our resources */
  453. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  454. if (res == NULL) {
  455. dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
  456. err = -ENOENT;
  457. goto err_no_iores;
  458. }
  459. hw->ioarea = request_mem_region(res->start, resource_size(res),
  460. pdev->name);
  461. if (hw->ioarea == NULL) {
  462. dev_err(&pdev->dev, "Cannot reserve region\n");
  463. err = -ENXIO;
  464. goto err_no_iores;
  465. }
  466. hw->regs = ioremap(res->start, resource_size(res));
  467. if (hw->regs == NULL) {
  468. dev_err(&pdev->dev, "Cannot map IO\n");
  469. err = -ENXIO;
  470. goto err_no_iomap;
  471. }
  472. hw->irq = platform_get_irq(pdev, 0);
  473. if (hw->irq < 0) {
  474. dev_err(&pdev->dev, "No IRQ specified\n");
  475. err = -ENOENT;
  476. goto err_no_irq;
  477. }
  478. err = request_irq(hw->irq, s3c24xx_spi_irq, 0, pdev->name, hw);
  479. if (err) {
  480. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  481. goto err_no_irq;
  482. }
  483. hw->clk = clk_get(&pdev->dev, "spi");
  484. if (IS_ERR(hw->clk)) {
  485. dev_err(&pdev->dev, "No clock for device\n");
  486. err = PTR_ERR(hw->clk);
  487. goto err_no_clk;
  488. }
  489. /* setup any gpio we can */
  490. if (!pdata->set_cs) {
  491. if (pdata->pin_cs < 0) {
  492. dev_err(&pdev->dev, "No chipselect pin\n");
  493. goto err_register;
  494. }
  495. err = gpio_request(pdata->pin_cs, dev_name(&pdev->dev));
  496. if (err) {
  497. dev_err(&pdev->dev, "Failed to get gpio for cs\n");
  498. goto err_register;
  499. }
  500. hw->set_cs = s3c24xx_spi_gpiocs;
  501. gpio_direction_output(pdata->pin_cs, 1);
  502. } else
  503. hw->set_cs = pdata->set_cs;
  504. s3c24xx_spi_initialsetup(hw);
  505. /* register our spi controller */
  506. err = spi_bitbang_start(&hw->bitbang);
  507. if (err) {
  508. dev_err(&pdev->dev, "Failed to register SPI master\n");
  509. goto err_register;
  510. }
  511. return 0;
  512. err_register:
  513. if (hw->set_cs == s3c24xx_spi_gpiocs)
  514. gpio_free(pdata->pin_cs);
  515. clk_disable(hw->clk);
  516. clk_put(hw->clk);
  517. err_no_clk:
  518. free_irq(hw->irq, hw);
  519. err_no_irq:
  520. iounmap(hw->regs);
  521. err_no_iomap:
  522. release_resource(hw->ioarea);
  523. kfree(hw->ioarea);
  524. err_no_iores:
  525. err_no_pdata:
  526. spi_master_put(hw->master);
  527. err_nomem:
  528. return err;
  529. }
  530. static int __exit s3c24xx_spi_remove(struct platform_device *dev)
  531. {
  532. struct s3c24xx_spi *hw = platform_get_drvdata(dev);
  533. platform_set_drvdata(dev, NULL);
  534. spi_bitbang_stop(&hw->bitbang);
  535. clk_disable(hw->clk);
  536. clk_put(hw->clk);
  537. free_irq(hw->irq, hw);
  538. iounmap(hw->regs);
  539. if (hw->set_cs == s3c24xx_spi_gpiocs)
  540. gpio_free(hw->pdata->pin_cs);
  541. release_resource(hw->ioarea);
  542. kfree(hw->ioarea);
  543. spi_master_put(hw->master);
  544. return 0;
  545. }
  546. #ifdef CONFIG_PM
  547. static int s3c24xx_spi_suspend(struct device *dev)
  548. {
  549. struct s3c24xx_spi *hw = platform_get_drvdata(to_platform_device(dev));
  550. if (hw->pdata && hw->pdata->gpio_setup)
  551. hw->pdata->gpio_setup(hw->pdata, 0);
  552. clk_disable(hw->clk);
  553. return 0;
  554. }
  555. static int s3c24xx_spi_resume(struct device *dev)
  556. {
  557. struct s3c24xx_spi *hw = platform_get_drvdata(to_platform_device(dev));
  558. s3c24xx_spi_initialsetup(hw);
  559. return 0;
  560. }
  561. static const struct dev_pm_ops s3c24xx_spi_pmops = {
  562. .suspend = s3c24xx_spi_suspend,
  563. .resume = s3c24xx_spi_resume,
  564. };
  565. #define S3C24XX_SPI_PMOPS &s3c24xx_spi_pmops
  566. #else
  567. #define S3C24XX_SPI_PMOPS NULL
  568. #endif /* CONFIG_PM */
  569. MODULE_ALIAS("platform:s3c2410-spi");
  570. static struct platform_driver s3c24xx_spi_driver = {
  571. .remove = __exit_p(s3c24xx_spi_remove),
  572. .driver = {
  573. .name = "s3c2410-spi",
  574. .owner = THIS_MODULE,
  575. .pm = S3C24XX_SPI_PMOPS,
  576. },
  577. };
  578. static int __init s3c24xx_spi_init(void)
  579. {
  580. return platform_driver_probe(&s3c24xx_spi_driver, s3c24xx_spi_probe);
  581. }
  582. static void __exit s3c24xx_spi_exit(void)
  583. {
  584. platform_driver_unregister(&s3c24xx_spi_driver);
  585. }
  586. module_init(s3c24xx_spi_init);
  587. module_exit(s3c24xx_spi_exit);
  588. MODULE_DESCRIPTION("S3C24XX SPI Driver");
  589. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  590. MODULE_LICENSE("GPL");