atmel_spi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /*
  2. * Driver for Atmel AT32 and AT91 SPI Controllers
  3. *
  4. * Copyright (C) 2006 Atmel Corporation
  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. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/clk.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/delay.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/err.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/spi/spi.h>
  20. #include <asm/io.h>
  21. #include <asm/arch/board.h>
  22. #include <asm/arch/gpio.h>
  23. #ifdef CONFIG_ARCH_AT91
  24. #include <asm/arch/cpu.h>
  25. #endif
  26. #include "atmel_spi.h"
  27. /*
  28. * The core SPI transfer engine just talks to a register bank to set up
  29. * DMA transfers; transfer queue progress is driven by IRQs. The clock
  30. * framework provides the base clock, subdivided for each spi_device.
  31. *
  32. * Newer controllers, marked with "new_1" flag, have:
  33. * - CR.LASTXFER
  34. * - SPI_MR.DIV32 may become FDIV or must-be-zero (here: always zero)
  35. * - SPI_SR.TXEMPTY, SPI_SR.NSSR (and corresponding irqs)
  36. * - SPI_CSRx.CSAAT
  37. * - SPI_CSRx.SBCR allows faster clocking
  38. */
  39. struct atmel_spi {
  40. spinlock_t lock;
  41. void __iomem *regs;
  42. int irq;
  43. struct clk *clk;
  44. struct platform_device *pdev;
  45. unsigned new_1:1;
  46. u8 stopping;
  47. struct list_head queue;
  48. struct spi_transfer *current_transfer;
  49. unsigned long remaining_bytes;
  50. void *buffer;
  51. dma_addr_t buffer_dma;
  52. };
  53. #define BUFFER_SIZE PAGE_SIZE
  54. #define INVALID_DMA_ADDRESS 0xffffffff
  55. /*
  56. * Earlier SPI controllers (e.g. on at91rm9200) have a design bug whereby
  57. * they assume that spi slave device state will not change on deselect, so
  58. * that automagic deselection is OK. Not so! Workaround uses nCSx pins
  59. * as GPIOs; or newer controllers have CSAAT and friends.
  60. *
  61. * Since the CSAAT functionality is a bit weird on newer controllers
  62. * as well, we use GPIO to control nCSx pins on all controllers.
  63. */
  64. static inline void cs_activate(struct spi_device *spi)
  65. {
  66. unsigned gpio = (unsigned) spi->controller_data;
  67. unsigned active = spi->mode & SPI_CS_HIGH;
  68. dev_dbg(&spi->dev, "activate %u%s\n", gpio, active ? " (high)" : "");
  69. gpio_set_value(gpio, active);
  70. }
  71. static inline void cs_deactivate(struct spi_device *spi)
  72. {
  73. unsigned gpio = (unsigned) spi->controller_data;
  74. unsigned active = spi->mode & SPI_CS_HIGH;
  75. dev_dbg(&spi->dev, "DEactivate %u%s\n", gpio, active ? " (low)" : "");
  76. gpio_set_value(gpio, !active);
  77. }
  78. /*
  79. * Submit next transfer for DMA.
  80. * lock is held, spi irq is blocked
  81. */
  82. static void atmel_spi_next_xfer(struct spi_master *master,
  83. struct spi_message *msg)
  84. {
  85. struct atmel_spi *as = spi_master_get_devdata(master);
  86. struct spi_transfer *xfer;
  87. u32 len;
  88. dma_addr_t tx_dma, rx_dma;
  89. xfer = as->current_transfer;
  90. if (!xfer || as->remaining_bytes == 0) {
  91. if (xfer)
  92. xfer = list_entry(xfer->transfer_list.next,
  93. struct spi_transfer, transfer_list);
  94. else
  95. xfer = list_entry(msg->transfers.next,
  96. struct spi_transfer, transfer_list);
  97. as->remaining_bytes = xfer->len;
  98. as->current_transfer = xfer;
  99. }
  100. len = as->remaining_bytes;
  101. tx_dma = xfer->tx_dma;
  102. rx_dma = xfer->rx_dma;
  103. /* use scratch buffer only when rx or tx data is unspecified */
  104. if (rx_dma == INVALID_DMA_ADDRESS) {
  105. rx_dma = as->buffer_dma;
  106. if (len > BUFFER_SIZE)
  107. len = BUFFER_SIZE;
  108. }
  109. if (tx_dma == INVALID_DMA_ADDRESS) {
  110. tx_dma = as->buffer_dma;
  111. if (len > BUFFER_SIZE)
  112. len = BUFFER_SIZE;
  113. memset(as->buffer, 0, len);
  114. dma_sync_single_for_device(&as->pdev->dev,
  115. as->buffer_dma, len, DMA_TO_DEVICE);
  116. }
  117. spi_writel(as, RPR, rx_dma);
  118. spi_writel(as, TPR, tx_dma);
  119. as->remaining_bytes -= len;
  120. if (msg->spi->bits_per_word > 8)
  121. len >>= 1;
  122. /* REVISIT: when xfer->delay_usecs == 0, the PDC "next transfer"
  123. * mechanism might help avoid the IRQ latency between transfers
  124. *
  125. * We're also waiting for ENDRX before we start the next
  126. * transfer because we need to handle some difficult timing
  127. * issues otherwise. If we wait for ENDTX in one transfer and
  128. * then starts waiting for ENDRX in the next, it's difficult
  129. * to tell the difference between the ENDRX interrupt we're
  130. * actually waiting for and the ENDRX interrupt of the
  131. * previous transfer.
  132. *
  133. * It should be doable, though. Just not now...
  134. */
  135. spi_writel(as, TNCR, 0);
  136. spi_writel(as, RNCR, 0);
  137. spi_writel(as, IER, SPI_BIT(ENDRX) | SPI_BIT(OVRES));
  138. dev_dbg(&msg->spi->dev,
  139. " start xfer %p: len %u tx %p/%08x rx %p/%08x imr %03x\n",
  140. xfer, xfer->len, xfer->tx_buf, xfer->tx_dma,
  141. xfer->rx_buf, xfer->rx_dma, spi_readl(as, IMR));
  142. spi_writel(as, TCR, len);
  143. spi_writel(as, RCR, len);
  144. spi_writel(as, PTCR, SPI_BIT(TXTEN) | SPI_BIT(RXTEN));
  145. }
  146. static void atmel_spi_next_message(struct spi_master *master)
  147. {
  148. struct atmel_spi *as = spi_master_get_devdata(master);
  149. struct spi_message *msg;
  150. u32 mr;
  151. BUG_ON(as->current_transfer);
  152. msg = list_entry(as->queue.next, struct spi_message, queue);
  153. /* Select the chip */
  154. mr = spi_readl(as, MR);
  155. mr = SPI_BFINS(PCS, ~(1 << msg->spi->chip_select), mr);
  156. spi_writel(as, MR, mr);
  157. cs_activate(msg->spi);
  158. atmel_spi_next_xfer(master, msg);
  159. }
  160. static void
  161. atmel_spi_dma_map_xfer(struct atmel_spi *as, struct spi_transfer *xfer)
  162. {
  163. xfer->tx_dma = xfer->rx_dma = INVALID_DMA_ADDRESS;
  164. if (xfer->tx_buf)
  165. xfer->tx_dma = dma_map_single(&as->pdev->dev,
  166. (void *) xfer->tx_buf, xfer->len,
  167. DMA_TO_DEVICE);
  168. if (xfer->rx_buf)
  169. xfer->rx_dma = dma_map_single(&as->pdev->dev,
  170. xfer->rx_buf, xfer->len,
  171. DMA_FROM_DEVICE);
  172. }
  173. static void atmel_spi_dma_unmap_xfer(struct spi_master *master,
  174. struct spi_transfer *xfer)
  175. {
  176. if (xfer->tx_dma != INVALID_DMA_ADDRESS)
  177. dma_unmap_single(master->cdev.dev, xfer->tx_dma,
  178. xfer->len, DMA_TO_DEVICE);
  179. if (xfer->rx_dma != INVALID_DMA_ADDRESS)
  180. dma_unmap_single(master->cdev.dev, xfer->rx_dma,
  181. xfer->len, DMA_FROM_DEVICE);
  182. }
  183. static void
  184. atmel_spi_msg_done(struct spi_master *master, struct atmel_spi *as,
  185. struct spi_message *msg, int status)
  186. {
  187. cs_deactivate(msg->spi);
  188. list_del(&msg->queue);
  189. msg->status = status;
  190. dev_dbg(master->cdev.dev,
  191. "xfer complete: %u bytes transferred\n",
  192. msg->actual_length);
  193. spin_unlock(&as->lock);
  194. msg->complete(msg->context);
  195. spin_lock(&as->lock);
  196. as->current_transfer = NULL;
  197. /* continue if needed */
  198. if (list_empty(&as->queue) || as->stopping)
  199. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  200. else
  201. atmel_spi_next_message(master);
  202. }
  203. static irqreturn_t
  204. atmel_spi_interrupt(int irq, void *dev_id)
  205. {
  206. struct spi_master *master = dev_id;
  207. struct atmel_spi *as = spi_master_get_devdata(master);
  208. struct spi_message *msg;
  209. struct spi_transfer *xfer;
  210. u32 status, pending, imr;
  211. int ret = IRQ_NONE;
  212. spin_lock(&as->lock);
  213. xfer = as->current_transfer;
  214. msg = list_entry(as->queue.next, struct spi_message, queue);
  215. imr = spi_readl(as, IMR);
  216. status = spi_readl(as, SR);
  217. pending = status & imr;
  218. if (pending & SPI_BIT(OVRES)) {
  219. int timeout;
  220. ret = IRQ_HANDLED;
  221. spi_writel(as, IDR, (SPI_BIT(ENDTX) | SPI_BIT(ENDRX)
  222. | SPI_BIT(OVRES)));
  223. /*
  224. * When we get an overrun, we disregard the current
  225. * transfer. Data will not be copied back from any
  226. * bounce buffer and msg->actual_len will not be
  227. * updated with the last xfer.
  228. *
  229. * We will also not process any remaning transfers in
  230. * the message.
  231. *
  232. * First, stop the transfer and unmap the DMA buffers.
  233. */
  234. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  235. if (!msg->is_dma_mapped)
  236. atmel_spi_dma_unmap_xfer(master, xfer);
  237. /* REVISIT: udelay in irq is unfriendly */
  238. if (xfer->delay_usecs)
  239. udelay(xfer->delay_usecs);
  240. dev_warn(master->cdev.dev, "fifo overrun (%u/%u remaining)\n",
  241. spi_readl(as, TCR), spi_readl(as, RCR));
  242. /*
  243. * Clean up DMA registers and make sure the data
  244. * registers are empty.
  245. */
  246. spi_writel(as, RNCR, 0);
  247. spi_writel(as, TNCR, 0);
  248. spi_writel(as, RCR, 0);
  249. spi_writel(as, TCR, 0);
  250. for (timeout = 1000; timeout; timeout--)
  251. if (spi_readl(as, SR) & SPI_BIT(TXEMPTY))
  252. break;
  253. if (!timeout)
  254. dev_warn(master->cdev.dev,
  255. "timeout waiting for TXEMPTY");
  256. while (spi_readl(as, SR) & SPI_BIT(RDRF))
  257. spi_readl(as, RDR);
  258. /* Clear any overrun happening while cleaning up */
  259. spi_readl(as, SR);
  260. atmel_spi_msg_done(master, as, msg, -EIO);
  261. } else if (pending & SPI_BIT(ENDRX)) {
  262. ret = IRQ_HANDLED;
  263. spi_writel(as, IDR, pending);
  264. if (as->remaining_bytes == 0) {
  265. msg->actual_length += xfer->len;
  266. if (!msg->is_dma_mapped)
  267. atmel_spi_dma_unmap_xfer(master, xfer);
  268. /* REVISIT: udelay in irq is unfriendly */
  269. if (xfer->delay_usecs)
  270. udelay(xfer->delay_usecs);
  271. if (msg->transfers.prev == &xfer->transfer_list) {
  272. /* report completed message */
  273. atmel_spi_msg_done(master, as, msg, 0);
  274. } else {
  275. if (xfer->cs_change) {
  276. cs_deactivate(msg->spi);
  277. udelay(1);
  278. cs_activate(msg->spi);
  279. }
  280. /*
  281. * Not done yet. Submit the next transfer.
  282. *
  283. * FIXME handle protocol options for xfer
  284. */
  285. atmel_spi_next_xfer(master, msg);
  286. }
  287. } else {
  288. /*
  289. * Keep going, we still have data to send in
  290. * the current transfer.
  291. */
  292. atmel_spi_next_xfer(master, msg);
  293. }
  294. }
  295. spin_unlock(&as->lock);
  296. return ret;
  297. }
  298. #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH)
  299. static int atmel_spi_setup(struct spi_device *spi)
  300. {
  301. struct atmel_spi *as;
  302. u32 scbr, csr;
  303. unsigned int bits = spi->bits_per_word;
  304. unsigned long bus_hz, sck_hz;
  305. unsigned int npcs_pin;
  306. int ret;
  307. as = spi_master_get_devdata(spi->master);
  308. if (as->stopping)
  309. return -ESHUTDOWN;
  310. if (spi->chip_select > spi->master->num_chipselect) {
  311. dev_dbg(&spi->dev,
  312. "setup: invalid chipselect %u (%u defined)\n",
  313. spi->chip_select, spi->master->num_chipselect);
  314. return -EINVAL;
  315. }
  316. if (bits == 0)
  317. bits = 8;
  318. if (bits < 8 || bits > 16) {
  319. dev_dbg(&spi->dev,
  320. "setup: invalid bits_per_word %u (8 to 16)\n",
  321. bits);
  322. return -EINVAL;
  323. }
  324. if (spi->mode & ~MODEBITS) {
  325. dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
  326. spi->mode & ~MODEBITS);
  327. return -EINVAL;
  328. }
  329. /* speed zero convention is used by some upper layers */
  330. bus_hz = clk_get_rate(as->clk);
  331. if (spi->max_speed_hz) {
  332. /* assume div32/fdiv/mbz == 0 */
  333. if (!as->new_1)
  334. bus_hz /= 2;
  335. scbr = ((bus_hz + spi->max_speed_hz - 1)
  336. / spi->max_speed_hz);
  337. if (scbr >= (1 << SPI_SCBR_SIZE)) {
  338. dev_dbg(&spi->dev, "setup: %d Hz too slow, scbr %u\n",
  339. spi->max_speed_hz, scbr);
  340. return -EINVAL;
  341. }
  342. } else
  343. scbr = 0xff;
  344. sck_hz = bus_hz / scbr;
  345. csr = SPI_BF(SCBR, scbr) | SPI_BF(BITS, bits - 8);
  346. if (spi->mode & SPI_CPOL)
  347. csr |= SPI_BIT(CPOL);
  348. if (!(spi->mode & SPI_CPHA))
  349. csr |= SPI_BIT(NCPHA);
  350. /* TODO: DLYBS and DLYBCT */
  351. csr |= SPI_BF(DLYBS, 10);
  352. csr |= SPI_BF(DLYBCT, 10);
  353. /* chipselect must have been muxed as GPIO (e.g. in board setup) */
  354. npcs_pin = (unsigned int)spi->controller_data;
  355. if (!spi->controller_state) {
  356. ret = gpio_request(npcs_pin, "spi_npcs");
  357. if (ret)
  358. return ret;
  359. spi->controller_state = (void *)npcs_pin;
  360. gpio_direction_output(npcs_pin, !(spi->mode & SPI_CS_HIGH));
  361. }
  362. dev_dbg(&spi->dev,
  363. "setup: %lu Hz bpw %u mode 0x%x -> csr%d %08x\n",
  364. sck_hz, bits, spi->mode, spi->chip_select, csr);
  365. spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
  366. return 0;
  367. }
  368. static int atmel_spi_transfer(struct spi_device *spi, struct spi_message *msg)
  369. {
  370. struct atmel_spi *as;
  371. struct spi_transfer *xfer;
  372. unsigned long flags;
  373. struct device *controller = spi->master->cdev.dev;
  374. as = spi_master_get_devdata(spi->master);
  375. dev_dbg(controller, "new message %p submitted for %s\n",
  376. msg, spi->dev.bus_id);
  377. if (unlikely(list_empty(&msg->transfers)
  378. || !spi->max_speed_hz))
  379. return -EINVAL;
  380. if (as->stopping)
  381. return -ESHUTDOWN;
  382. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  383. if (!(xfer->tx_buf || xfer->rx_buf)) {
  384. dev_dbg(&spi->dev, "missing rx or tx buf\n");
  385. return -EINVAL;
  386. }
  387. /* FIXME implement these protocol options!! */
  388. if (xfer->bits_per_word || xfer->speed_hz) {
  389. dev_dbg(&spi->dev, "no protocol options yet\n");
  390. return -ENOPROTOOPT;
  391. }
  392. }
  393. /* scrub dcache "early" */
  394. if (!msg->is_dma_mapped) {
  395. list_for_each_entry(xfer, &msg->transfers, transfer_list)
  396. atmel_spi_dma_map_xfer(as, xfer);
  397. }
  398. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  399. dev_dbg(controller,
  400. " xfer %p: len %u tx %p/%08x rx %p/%08x\n",
  401. xfer, xfer->len,
  402. xfer->tx_buf, xfer->tx_dma,
  403. xfer->rx_buf, xfer->rx_dma);
  404. }
  405. msg->status = -EINPROGRESS;
  406. msg->actual_length = 0;
  407. spin_lock_irqsave(&as->lock, flags);
  408. list_add_tail(&msg->queue, &as->queue);
  409. if (!as->current_transfer)
  410. atmel_spi_next_message(spi->master);
  411. spin_unlock_irqrestore(&as->lock, flags);
  412. return 0;
  413. }
  414. static void atmel_spi_cleanup(struct spi_device *spi)
  415. {
  416. if (spi->controller_state)
  417. gpio_free((unsigned int)spi->controller_data);
  418. }
  419. /*-------------------------------------------------------------------------*/
  420. static int __init atmel_spi_probe(struct platform_device *pdev)
  421. {
  422. struct resource *regs;
  423. int irq;
  424. struct clk *clk;
  425. int ret;
  426. struct spi_master *master;
  427. struct atmel_spi *as;
  428. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  429. if (!regs)
  430. return -ENXIO;
  431. irq = platform_get_irq(pdev, 0);
  432. if (irq < 0)
  433. return irq;
  434. clk = clk_get(&pdev->dev, "spi_clk");
  435. if (IS_ERR(clk))
  436. return PTR_ERR(clk);
  437. /* setup spi core then atmel-specific driver state */
  438. ret = -ENOMEM;
  439. master = spi_alloc_master(&pdev->dev, sizeof *as);
  440. if (!master)
  441. goto out_free;
  442. master->bus_num = pdev->id;
  443. master->num_chipselect = 4;
  444. master->setup = atmel_spi_setup;
  445. master->transfer = atmel_spi_transfer;
  446. master->cleanup = atmel_spi_cleanup;
  447. platform_set_drvdata(pdev, master);
  448. as = spi_master_get_devdata(master);
  449. as->buffer = dma_alloc_coherent(&pdev->dev, BUFFER_SIZE,
  450. &as->buffer_dma, GFP_KERNEL);
  451. if (!as->buffer)
  452. goto out_free;
  453. spin_lock_init(&as->lock);
  454. INIT_LIST_HEAD(&as->queue);
  455. as->pdev = pdev;
  456. as->regs = ioremap(regs->start, (regs->end - regs->start) + 1);
  457. if (!as->regs)
  458. goto out_free_buffer;
  459. as->irq = irq;
  460. as->clk = clk;
  461. #ifdef CONFIG_ARCH_AT91
  462. if (!cpu_is_at91rm9200())
  463. as->new_1 = 1;
  464. #endif
  465. ret = request_irq(irq, atmel_spi_interrupt, 0,
  466. pdev->dev.bus_id, master);
  467. if (ret)
  468. goto out_unmap_regs;
  469. /* Initialize the hardware */
  470. clk_enable(clk);
  471. spi_writel(as, CR, SPI_BIT(SWRST));
  472. spi_writel(as, MR, SPI_BIT(MSTR) | SPI_BIT(MODFDIS));
  473. spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
  474. spi_writel(as, CR, SPI_BIT(SPIEN));
  475. /* go! */
  476. dev_info(&pdev->dev, "Atmel SPI Controller at 0x%08lx (irq %d)\n",
  477. (unsigned long)regs->start, irq);
  478. ret = spi_register_master(master);
  479. if (ret)
  480. goto out_reset_hw;
  481. return 0;
  482. out_reset_hw:
  483. spi_writel(as, CR, SPI_BIT(SWRST));
  484. clk_disable(clk);
  485. free_irq(irq, master);
  486. out_unmap_regs:
  487. iounmap(as->regs);
  488. out_free_buffer:
  489. dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
  490. as->buffer_dma);
  491. out_free:
  492. clk_put(clk);
  493. spi_master_put(master);
  494. return ret;
  495. }
  496. static int __exit atmel_spi_remove(struct platform_device *pdev)
  497. {
  498. struct spi_master *master = platform_get_drvdata(pdev);
  499. struct atmel_spi *as = spi_master_get_devdata(master);
  500. struct spi_message *msg;
  501. /* reset the hardware and block queue progress */
  502. spin_lock_irq(&as->lock);
  503. as->stopping = 1;
  504. spi_writel(as, CR, SPI_BIT(SWRST));
  505. spi_readl(as, SR);
  506. spin_unlock_irq(&as->lock);
  507. /* Terminate remaining queued transfers */
  508. list_for_each_entry(msg, &as->queue, queue) {
  509. /* REVISIT unmapping the dma is a NOP on ARM and AVR32
  510. * but we shouldn't depend on that...
  511. */
  512. msg->status = -ESHUTDOWN;
  513. msg->complete(msg->context);
  514. }
  515. dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
  516. as->buffer_dma);
  517. clk_disable(as->clk);
  518. clk_put(as->clk);
  519. free_irq(as->irq, master);
  520. iounmap(as->regs);
  521. spi_unregister_master(master);
  522. return 0;
  523. }
  524. #ifdef CONFIG_PM
  525. static int atmel_spi_suspend(struct platform_device *pdev, pm_message_t mesg)
  526. {
  527. struct spi_master *master = platform_get_drvdata(pdev);
  528. struct atmel_spi *as = spi_master_get_devdata(master);
  529. clk_disable(as->clk);
  530. return 0;
  531. }
  532. static int atmel_spi_resume(struct platform_device *pdev)
  533. {
  534. struct spi_master *master = platform_get_drvdata(pdev);
  535. struct atmel_spi *as = spi_master_get_devdata(master);
  536. clk_enable(as->clk);
  537. return 0;
  538. }
  539. #else
  540. #define atmel_spi_suspend NULL
  541. #define atmel_spi_resume NULL
  542. #endif
  543. static struct platform_driver atmel_spi_driver = {
  544. .driver = {
  545. .name = "atmel_spi",
  546. .owner = THIS_MODULE,
  547. },
  548. .suspend = atmel_spi_suspend,
  549. .resume = atmel_spi_resume,
  550. .remove = __exit_p(atmel_spi_remove),
  551. };
  552. static int __init atmel_spi_init(void)
  553. {
  554. return platform_driver_probe(&atmel_spi_driver, atmel_spi_probe);
  555. }
  556. module_init(atmel_spi_init);
  557. static void __exit atmel_spi_exit(void)
  558. {
  559. platform_driver_unregister(&atmel_spi_driver);
  560. }
  561. module_exit(atmel_spi_exit);
  562. MODULE_DESCRIPTION("Atmel AT32/AT91 SPI Controller driver");
  563. MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
  564. MODULE_LICENSE("GPL");