atmel_spi.c 17 KB

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