davinci_spi.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  1. /*
  2. * Copyright (C) 2009 Texas Instruments.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/gpio.h>
  21. #include <linux/module.h>
  22. #include <linux/delay.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/err.h>
  25. #include <linux/clk.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/spi/spi_bitbang.h>
  29. #include <linux/slab.h>
  30. #include <mach/spi.h>
  31. #include <mach/edma.h>
  32. #define SPI_NO_RESOURCE ((resource_size_t)-1)
  33. #define SPI_MAX_CHIPSELECT 2
  34. #define CS_DEFAULT 0xFF
  35. #define SPI_BUFSIZ (SMP_CACHE_BYTES + 1)
  36. #define DAVINCI_DMA_DATA_TYPE_S8 0x01
  37. #define DAVINCI_DMA_DATA_TYPE_S16 0x02
  38. #define DAVINCI_DMA_DATA_TYPE_S32 0x04
  39. #define SPIFMT_PHASE_MASK BIT(16)
  40. #define SPIFMT_POLARITY_MASK BIT(17)
  41. #define SPIFMT_DISTIMER_MASK BIT(18)
  42. #define SPIFMT_SHIFTDIR_MASK BIT(20)
  43. #define SPIFMT_WAITENA_MASK BIT(21)
  44. #define SPIFMT_PARITYENA_MASK BIT(22)
  45. #define SPIFMT_ODD_PARITY_MASK BIT(23)
  46. #define SPIFMT_WDELAY_MASK 0x3f000000u
  47. #define SPIFMT_WDELAY_SHIFT 24
  48. #define SPIFMT_CHARLEN_MASK 0x0000001Fu
  49. /* SPIPC0 */
  50. #define SPIPC0_DIFUN_MASK BIT(11) /* MISO */
  51. #define SPIPC0_DOFUN_MASK BIT(10) /* MOSI */
  52. #define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */
  53. #define SPIPC0_SPIENA_MASK BIT(8) /* nREADY */
  54. #define SPIINT_MASKALL 0x0101035F
  55. #define SPI_INTLVL_1 0x000001FFu
  56. #define SPI_INTLVL_0 0x00000000u
  57. /* SPIDAT1 */
  58. #define SPIDAT1_CSHOLD_MASK BIT(28)
  59. #define SPIDAT1_CSNR_SHIFT 16
  60. #define SPIGCR1_CLKMOD_MASK BIT(1)
  61. #define SPIGCR1_MASTER_MASK BIT(0)
  62. #define SPIGCR1_LOOPBACK_MASK BIT(16)
  63. #define SPIGCR1_SPIENA_MASK BIT(24)
  64. /* SPIBUF */
  65. #define SPIBUF_TXFULL_MASK BIT(29)
  66. #define SPIBUF_RXEMPTY_MASK BIT(31)
  67. /* Error Masks */
  68. #define SPIFLG_DLEN_ERR_MASK BIT(0)
  69. #define SPIFLG_TIMEOUT_MASK BIT(1)
  70. #define SPIFLG_PARERR_MASK BIT(2)
  71. #define SPIFLG_DESYNC_MASK BIT(3)
  72. #define SPIFLG_BITERR_MASK BIT(4)
  73. #define SPIFLG_OVRRUN_MASK BIT(6)
  74. #define SPIFLG_RX_INTR_MASK BIT(8)
  75. #define SPIFLG_TX_INTR_MASK BIT(9)
  76. #define SPIFLG_BUF_INIT_ACTIVE_MASK BIT(24)
  77. #define SPIINT_BITERR_INTR BIT(4)
  78. #define SPIINT_OVRRUN_INTR BIT(6)
  79. #define SPIINT_RX_INTR BIT(8)
  80. #define SPIINT_TX_INTR BIT(9)
  81. #define SPIINT_DMA_REQ_EN BIT(16)
  82. #define SPI_T2CDELAY_SHIFT 16
  83. #define SPI_C2TDELAY_SHIFT 24
  84. /* SPI Controller registers */
  85. #define SPIGCR0 0x00
  86. #define SPIGCR1 0x04
  87. #define SPIINT 0x08
  88. #define SPILVL 0x0c
  89. #define SPIFLG 0x10
  90. #define SPIPC0 0x14
  91. #define SPIDAT1 0x3c
  92. #define SPIBUF 0x40
  93. #define SPIDELAY 0x48
  94. #define SPIDEF 0x4c
  95. #define SPIFMT0 0x50
  96. struct davinci_spi_slave {
  97. u32 cmd_to_write;
  98. u32 clk_ctrl_to_write;
  99. u32 bytes_per_word;
  100. u8 active_cs;
  101. };
  102. /* We have 2 DMA channels per CS, one for RX and one for TX */
  103. struct davinci_spi_dma {
  104. int dma_tx_channel;
  105. int dma_rx_channel;
  106. int dma_tx_sync_dev;
  107. int dma_rx_sync_dev;
  108. enum dma_event_q eventq;
  109. struct completion dma_tx_completion;
  110. struct completion dma_rx_completion;
  111. };
  112. /* SPI Controller driver's private data. */
  113. struct davinci_spi {
  114. struct spi_bitbang bitbang;
  115. struct clk *clk;
  116. u8 version;
  117. resource_size_t pbase;
  118. void __iomem *base;
  119. size_t region_size;
  120. u32 irq;
  121. struct completion done;
  122. const void *tx;
  123. void *rx;
  124. u8 *tmp_buf;
  125. int count;
  126. struct davinci_spi_dma *dma_channels;
  127. struct davinci_spi_platform_data *pdata;
  128. void (*get_rx)(u32 rx_data, struct davinci_spi *);
  129. u32 (*get_tx)(struct davinci_spi *);
  130. struct davinci_spi_slave slave[SPI_MAX_CHIPSELECT];
  131. };
  132. static unsigned use_dma;
  133. static void davinci_spi_rx_buf_u8(u32 data, struct davinci_spi *davinci_spi)
  134. {
  135. u8 *rx = davinci_spi->rx;
  136. *rx++ = (u8)data;
  137. davinci_spi->rx = rx;
  138. }
  139. static void davinci_spi_rx_buf_u16(u32 data, struct davinci_spi *davinci_spi)
  140. {
  141. u16 *rx = davinci_spi->rx;
  142. *rx++ = (u16)data;
  143. davinci_spi->rx = rx;
  144. }
  145. static u32 davinci_spi_tx_buf_u8(struct davinci_spi *davinci_spi)
  146. {
  147. u32 data;
  148. const u8 *tx = davinci_spi->tx;
  149. data = *tx++;
  150. davinci_spi->tx = tx;
  151. return data;
  152. }
  153. static u32 davinci_spi_tx_buf_u16(struct davinci_spi *davinci_spi)
  154. {
  155. u32 data;
  156. const u16 *tx = davinci_spi->tx;
  157. data = *tx++;
  158. davinci_spi->tx = tx;
  159. return data;
  160. }
  161. static inline void set_io_bits(void __iomem *addr, u32 bits)
  162. {
  163. u32 v = ioread32(addr);
  164. v |= bits;
  165. iowrite32(v, addr);
  166. }
  167. static inline void clear_io_bits(void __iomem *addr, u32 bits)
  168. {
  169. u32 v = ioread32(addr);
  170. v &= ~bits;
  171. iowrite32(v, addr);
  172. }
  173. static inline void set_fmt_bits(void __iomem *addr, u32 bits, int cs_num)
  174. {
  175. set_io_bits(addr + SPIFMT0 + (0x4 * cs_num), bits);
  176. }
  177. static inline void clear_fmt_bits(void __iomem *addr, u32 bits, int cs_num)
  178. {
  179. clear_io_bits(addr + SPIFMT0 + (0x4 * cs_num), bits);
  180. }
  181. static void davinci_spi_set_dma_req(const struct spi_device *spi, int enable)
  182. {
  183. struct davinci_spi *davinci_spi = spi_master_get_devdata(spi->master);
  184. if (enable)
  185. set_io_bits(davinci_spi->base + SPIINT, SPIINT_DMA_REQ_EN);
  186. else
  187. clear_io_bits(davinci_spi->base + SPIINT, SPIINT_DMA_REQ_EN);
  188. }
  189. /*
  190. * Interface to control the chip select signal
  191. */
  192. static void davinci_spi_chipselect(struct spi_device *spi, int value)
  193. {
  194. struct davinci_spi *davinci_spi;
  195. struct davinci_spi_platform_data *pdata;
  196. u32 data1_reg_val;
  197. u8 chip_sel = spi->chip_select;
  198. davinci_spi = spi_master_get_devdata(spi->master);
  199. pdata = davinci_spi->pdata;
  200. /*
  201. * Board specific chip select logic decides the polarity and cs
  202. * line for the controller
  203. */
  204. data1_reg_val = CS_DEFAULT << SPIDAT1_CSNR_SHIFT;
  205. if (value == BITBANG_CS_ACTIVE) {
  206. data1_reg_val |= SPIDAT1_CSHOLD_MASK;
  207. data1_reg_val &= ~((0x1 << chip_sel) << SPIDAT1_CSNR_SHIFT);
  208. }
  209. iowrite32(data1_reg_val, davinci_spi->base + SPIDAT1);
  210. while ((ioread32(davinci_spi->base + SPIBUF)
  211. & SPIBUF_RXEMPTY_MASK) == 0)
  212. cpu_relax();
  213. }
  214. /**
  215. * davinci_spi_setup_transfer - This functions will determine transfer method
  216. * @spi: spi device on which data transfer to be done
  217. * @t: spi transfer in which transfer info is filled
  218. *
  219. * This function determines data transfer method (8/16/32 bit transfer).
  220. * It will also set the SPI Clock Control register according to
  221. * SPI slave device freq.
  222. */
  223. static int davinci_spi_setup_transfer(struct spi_device *spi,
  224. struct spi_transfer *t)
  225. {
  226. struct davinci_spi *davinci_spi;
  227. u8 bits_per_word = 0;
  228. u32 hz = 0, prescale = 0, clkspeed;
  229. davinci_spi = spi_master_get_devdata(spi->master);
  230. if (t) {
  231. bits_per_word = t->bits_per_word;
  232. hz = t->speed_hz;
  233. }
  234. /* if bits_per_word is not set then set it default */
  235. if (!bits_per_word)
  236. bits_per_word = spi->bits_per_word;
  237. /*
  238. * Assign function pointer to appropriate transfer method
  239. * 8bit, 16bit or 32bit transfer
  240. */
  241. if (bits_per_word <= 8 && bits_per_word >= 2) {
  242. davinci_spi->get_rx = davinci_spi_rx_buf_u8;
  243. davinci_spi->get_tx = davinci_spi_tx_buf_u8;
  244. davinci_spi->slave[spi->chip_select].bytes_per_word = 1;
  245. } else if (bits_per_word <= 16 && bits_per_word >= 2) {
  246. davinci_spi->get_rx = davinci_spi_rx_buf_u16;
  247. davinci_spi->get_tx = davinci_spi_tx_buf_u16;
  248. davinci_spi->slave[spi->chip_select].bytes_per_word = 2;
  249. } else
  250. return -EINVAL;
  251. if (!hz)
  252. hz = spi->max_speed_hz;
  253. clear_fmt_bits(davinci_spi->base, SPIFMT_CHARLEN_MASK,
  254. spi->chip_select);
  255. set_fmt_bits(davinci_spi->base, bits_per_word & 0x1f,
  256. spi->chip_select);
  257. clkspeed = clk_get_rate(davinci_spi->clk);
  258. if (hz > clkspeed / 2)
  259. prescale = 1 << 8;
  260. if (hz < clkspeed / 256)
  261. prescale = 255 << 8;
  262. if (!prescale)
  263. prescale = ((clkspeed / hz - 1) << 8) & 0x0000ff00;
  264. clear_fmt_bits(davinci_spi->base, 0x0000ff00, spi->chip_select);
  265. set_fmt_bits(davinci_spi->base, prescale, spi->chip_select);
  266. return 0;
  267. }
  268. static void davinci_spi_dma_rx_callback(unsigned lch, u16 ch_status, void *data)
  269. {
  270. struct spi_device *spi = (struct spi_device *)data;
  271. struct davinci_spi *davinci_spi;
  272. struct davinci_spi_dma *davinci_spi_dma;
  273. davinci_spi = spi_master_get_devdata(spi->master);
  274. davinci_spi_dma = &(davinci_spi->dma_channels[spi->chip_select]);
  275. if (ch_status == DMA_COMPLETE)
  276. edma_stop(davinci_spi_dma->dma_rx_channel);
  277. else
  278. edma_clean_channel(davinci_spi_dma->dma_rx_channel);
  279. complete(&davinci_spi_dma->dma_rx_completion);
  280. /* We must disable the DMA RX request */
  281. davinci_spi_set_dma_req(spi, 0);
  282. }
  283. static void davinci_spi_dma_tx_callback(unsigned lch, u16 ch_status, void *data)
  284. {
  285. struct spi_device *spi = (struct spi_device *)data;
  286. struct davinci_spi *davinci_spi;
  287. struct davinci_spi_dma *davinci_spi_dma;
  288. davinci_spi = spi_master_get_devdata(spi->master);
  289. davinci_spi_dma = &(davinci_spi->dma_channels[spi->chip_select]);
  290. if (ch_status == DMA_COMPLETE)
  291. edma_stop(davinci_spi_dma->dma_tx_channel);
  292. else
  293. edma_clean_channel(davinci_spi_dma->dma_tx_channel);
  294. complete(&davinci_spi_dma->dma_tx_completion);
  295. /* We must disable the DMA TX request */
  296. davinci_spi_set_dma_req(spi, 0);
  297. }
  298. static int davinci_spi_request_dma(struct spi_device *spi)
  299. {
  300. struct davinci_spi *davinci_spi;
  301. struct davinci_spi_dma *davinci_spi_dma;
  302. struct device *sdev;
  303. int r;
  304. davinci_spi = spi_master_get_devdata(spi->master);
  305. davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
  306. sdev = davinci_spi->bitbang.master->dev.parent;
  307. r = edma_alloc_channel(davinci_spi_dma->dma_rx_sync_dev,
  308. davinci_spi_dma_rx_callback, spi,
  309. davinci_spi_dma->eventq);
  310. if (r < 0) {
  311. dev_dbg(sdev, "Unable to request DMA channel for SPI RX\n");
  312. return -EAGAIN;
  313. }
  314. davinci_spi_dma->dma_rx_channel = r;
  315. r = edma_alloc_channel(davinci_spi_dma->dma_tx_sync_dev,
  316. davinci_spi_dma_tx_callback, spi,
  317. davinci_spi_dma->eventq);
  318. if (r < 0) {
  319. edma_free_channel(davinci_spi_dma->dma_rx_channel);
  320. davinci_spi_dma->dma_rx_channel = -1;
  321. dev_dbg(sdev, "Unable to request DMA channel for SPI TX\n");
  322. return -EAGAIN;
  323. }
  324. davinci_spi_dma->dma_tx_channel = r;
  325. return 0;
  326. }
  327. /**
  328. * davinci_spi_setup - This functions will set default transfer method
  329. * @spi: spi device on which data transfer to be done
  330. *
  331. * This functions sets the default transfer method.
  332. */
  333. static int davinci_spi_setup(struct spi_device *spi)
  334. {
  335. int retval;
  336. struct davinci_spi *davinci_spi;
  337. struct davinci_spi_dma *davinci_spi_dma;
  338. struct device *sdev;
  339. davinci_spi = spi_master_get_devdata(spi->master);
  340. sdev = davinci_spi->bitbang.master->dev.parent;
  341. /* if bits per word length is zero then set it default 8 */
  342. if (!spi->bits_per_word)
  343. spi->bits_per_word = 8;
  344. davinci_spi->slave[spi->chip_select].cmd_to_write = 0;
  345. if (use_dma && davinci_spi->dma_channels) {
  346. davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
  347. if ((davinci_spi_dma->dma_rx_channel == -1)
  348. || (davinci_spi_dma->dma_tx_channel == -1)) {
  349. retval = davinci_spi_request_dma(spi);
  350. if (retval < 0)
  351. return retval;
  352. }
  353. }
  354. /*
  355. * SPI in DaVinci and DA8xx operate between
  356. * 600 KHz and 50 MHz
  357. */
  358. if (spi->max_speed_hz < 600000 || spi->max_speed_hz > 50000000) {
  359. dev_dbg(sdev, "Operating frequency is not in acceptable "
  360. "range\n");
  361. return -EINVAL;
  362. }
  363. /*
  364. * Set up SPIFMTn register, unique to this chipselect.
  365. *
  366. * NOTE: we could do all of these with one write. Also, some
  367. * of the "version 2" features are found in chips that don't
  368. * support all of them...
  369. */
  370. if (spi->mode & SPI_LSB_FIRST)
  371. set_fmt_bits(davinci_spi->base, SPIFMT_SHIFTDIR_MASK,
  372. spi->chip_select);
  373. else
  374. clear_fmt_bits(davinci_spi->base, SPIFMT_SHIFTDIR_MASK,
  375. spi->chip_select);
  376. if (spi->mode & SPI_CPOL)
  377. set_fmt_bits(davinci_spi->base, SPIFMT_POLARITY_MASK,
  378. spi->chip_select);
  379. else
  380. clear_fmt_bits(davinci_spi->base, SPIFMT_POLARITY_MASK,
  381. spi->chip_select);
  382. if (!(spi->mode & SPI_CPHA))
  383. set_fmt_bits(davinci_spi->base, SPIFMT_PHASE_MASK,
  384. spi->chip_select);
  385. else
  386. clear_fmt_bits(davinci_spi->base, SPIFMT_PHASE_MASK,
  387. spi->chip_select);
  388. /*
  389. * Version 1 hardware supports two basic SPI modes:
  390. * - Standard SPI mode uses 4 pins, with chipselect
  391. * - 3 pin SPI is a 4 pin variant without CS (SPI_NO_CS)
  392. * (distinct from SPI_3WIRE, with just one data wire;
  393. * or similar variants without MOSI or without MISO)
  394. *
  395. * Version 2 hardware supports an optional handshaking signal,
  396. * so it can support two more modes:
  397. * - 5 pin SPI variant is standard SPI plus SPI_READY
  398. * - 4 pin with enable is (SPI_READY | SPI_NO_CS)
  399. */
  400. if (davinci_spi->version == SPI_VERSION_2) {
  401. clear_fmt_bits(davinci_spi->base, SPIFMT_WDELAY_MASK,
  402. spi->chip_select);
  403. set_fmt_bits(davinci_spi->base,
  404. (davinci_spi->pdata->wdelay
  405. << SPIFMT_WDELAY_SHIFT)
  406. & SPIFMT_WDELAY_MASK,
  407. spi->chip_select);
  408. if (davinci_spi->pdata->odd_parity)
  409. set_fmt_bits(davinci_spi->base,
  410. SPIFMT_ODD_PARITY_MASK,
  411. spi->chip_select);
  412. else
  413. clear_fmt_bits(davinci_spi->base,
  414. SPIFMT_ODD_PARITY_MASK,
  415. spi->chip_select);
  416. if (davinci_spi->pdata->parity_enable)
  417. set_fmt_bits(davinci_spi->base,
  418. SPIFMT_PARITYENA_MASK,
  419. spi->chip_select);
  420. else
  421. clear_fmt_bits(davinci_spi->base,
  422. SPIFMT_PARITYENA_MASK,
  423. spi->chip_select);
  424. if (davinci_spi->pdata->wait_enable)
  425. set_fmt_bits(davinci_spi->base,
  426. SPIFMT_WAITENA_MASK,
  427. spi->chip_select);
  428. else
  429. clear_fmt_bits(davinci_spi->base,
  430. SPIFMT_WAITENA_MASK,
  431. spi->chip_select);
  432. if (davinci_spi->pdata->timer_disable)
  433. set_fmt_bits(davinci_spi->base,
  434. SPIFMT_DISTIMER_MASK,
  435. spi->chip_select);
  436. else
  437. clear_fmt_bits(davinci_spi->base,
  438. SPIFMT_DISTIMER_MASK,
  439. spi->chip_select);
  440. }
  441. retval = davinci_spi_setup_transfer(spi, NULL);
  442. return retval;
  443. }
  444. static void davinci_spi_cleanup(struct spi_device *spi)
  445. {
  446. struct davinci_spi *davinci_spi = spi_master_get_devdata(spi->master);
  447. struct davinci_spi_dma *davinci_spi_dma;
  448. davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
  449. if (use_dma && davinci_spi->dma_channels) {
  450. davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
  451. if ((davinci_spi_dma->dma_rx_channel != -1)
  452. && (davinci_spi_dma->dma_tx_channel != -1)) {
  453. edma_free_channel(davinci_spi_dma->dma_tx_channel);
  454. edma_free_channel(davinci_spi_dma->dma_rx_channel);
  455. }
  456. }
  457. }
  458. static int davinci_spi_bufs_prep(struct spi_device *spi,
  459. struct davinci_spi *davinci_spi)
  460. {
  461. int op_mode = 0;
  462. /*
  463. * REVISIT unless devices disagree about SPI_LOOP or
  464. * SPI_READY (SPI_NO_CS only allows one device!), this
  465. * should not need to be done before each message...
  466. * optimize for both flags staying cleared.
  467. */
  468. op_mode = SPIPC0_DIFUN_MASK
  469. | SPIPC0_DOFUN_MASK
  470. | SPIPC0_CLKFUN_MASK;
  471. if (!(spi->mode & SPI_NO_CS))
  472. op_mode |= 1 << spi->chip_select;
  473. if (spi->mode & SPI_READY)
  474. op_mode |= SPIPC0_SPIENA_MASK;
  475. iowrite32(op_mode, davinci_spi->base + SPIPC0);
  476. if (spi->mode & SPI_LOOP)
  477. set_io_bits(davinci_spi->base + SPIGCR1,
  478. SPIGCR1_LOOPBACK_MASK);
  479. else
  480. clear_io_bits(davinci_spi->base + SPIGCR1,
  481. SPIGCR1_LOOPBACK_MASK);
  482. return 0;
  483. }
  484. static int davinci_spi_check_error(struct davinci_spi *davinci_spi,
  485. int int_status)
  486. {
  487. struct device *sdev = davinci_spi->bitbang.master->dev.parent;
  488. if (int_status & SPIFLG_TIMEOUT_MASK) {
  489. dev_dbg(sdev, "SPI Time-out Error\n");
  490. return -ETIMEDOUT;
  491. }
  492. if (int_status & SPIFLG_DESYNC_MASK) {
  493. dev_dbg(sdev, "SPI Desynchronization Error\n");
  494. return -EIO;
  495. }
  496. if (int_status & SPIFLG_BITERR_MASK) {
  497. dev_dbg(sdev, "SPI Bit error\n");
  498. return -EIO;
  499. }
  500. if (davinci_spi->version == SPI_VERSION_2) {
  501. if (int_status & SPIFLG_DLEN_ERR_MASK) {
  502. dev_dbg(sdev, "SPI Data Length Error\n");
  503. return -EIO;
  504. }
  505. if (int_status & SPIFLG_PARERR_MASK) {
  506. dev_dbg(sdev, "SPI Parity Error\n");
  507. return -EIO;
  508. }
  509. if (int_status & SPIFLG_OVRRUN_MASK) {
  510. dev_dbg(sdev, "SPI Data Overrun error\n");
  511. return -EIO;
  512. }
  513. if (int_status & SPIFLG_TX_INTR_MASK) {
  514. dev_dbg(sdev, "SPI TX intr bit set\n");
  515. return -EIO;
  516. }
  517. if (int_status & SPIFLG_BUF_INIT_ACTIVE_MASK) {
  518. dev_dbg(sdev, "SPI Buffer Init Active\n");
  519. return -EBUSY;
  520. }
  521. }
  522. return 0;
  523. }
  524. /**
  525. * davinci_spi_bufs - functions which will handle transfer data
  526. * @spi: spi device on which data transfer to be done
  527. * @t: spi transfer in which transfer info is filled
  528. *
  529. * This function will put data to be transferred into data register
  530. * of SPI controller and then wait until the completion will be marked
  531. * by the IRQ Handler.
  532. */
  533. static int davinci_spi_bufs_pio(struct spi_device *spi, struct spi_transfer *t)
  534. {
  535. struct davinci_spi *davinci_spi;
  536. int int_status, count, ret;
  537. u8 conv;
  538. u32 tx_data, data1_reg_val;
  539. u32 buf_val, flg_val;
  540. struct davinci_spi_platform_data *pdata;
  541. davinci_spi = spi_master_get_devdata(spi->master);
  542. pdata = davinci_spi->pdata;
  543. davinci_spi->tx = t->tx_buf;
  544. davinci_spi->rx = t->rx_buf;
  545. /* convert len to words based on bits_per_word */
  546. conv = davinci_spi->slave[spi->chip_select].bytes_per_word;
  547. davinci_spi->count = t->len / conv;
  548. data1_reg_val = ioread32(davinci_spi->base + SPIDAT1);
  549. INIT_COMPLETION(davinci_spi->done);
  550. ret = davinci_spi_bufs_prep(spi, davinci_spi);
  551. if (ret)
  552. return ret;
  553. /* Enable SPI */
  554. set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
  555. iowrite32(0 | (pdata->c2tdelay << SPI_C2TDELAY_SHIFT) |
  556. (pdata->t2cdelay << SPI_T2CDELAY_SHIFT),
  557. davinci_spi->base + SPIDELAY);
  558. count = davinci_spi->count;
  559. /* Determine the command to execute READ or WRITE */
  560. if (t->tx_buf) {
  561. clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKALL);
  562. while (1) {
  563. tx_data = davinci_spi->get_tx(davinci_spi);
  564. data1_reg_val &= ~(0xFFFF);
  565. data1_reg_val |= (0xFFFF & tx_data);
  566. buf_val = ioread32(davinci_spi->base + SPIBUF);
  567. if ((buf_val & SPIBUF_TXFULL_MASK) == 0) {
  568. iowrite32(data1_reg_val,
  569. davinci_spi->base + SPIDAT1);
  570. count--;
  571. }
  572. while (ioread32(davinci_spi->base + SPIBUF)
  573. & SPIBUF_RXEMPTY_MASK)
  574. cpu_relax();
  575. /* getting the returned byte */
  576. if (t->rx_buf) {
  577. buf_val = ioread32(davinci_spi->base + SPIBUF);
  578. davinci_spi->get_rx(buf_val, davinci_spi);
  579. }
  580. if (count <= 0)
  581. break;
  582. }
  583. } else {
  584. if (pdata->poll_mode) {
  585. while (1) {
  586. /* keeps the serial clock going */
  587. if ((ioread32(davinci_spi->base + SPIBUF)
  588. & SPIBUF_TXFULL_MASK) == 0)
  589. iowrite32(data1_reg_val,
  590. davinci_spi->base + SPIDAT1);
  591. while (ioread32(davinci_spi->base + SPIBUF) &
  592. SPIBUF_RXEMPTY_MASK)
  593. cpu_relax();
  594. flg_val = ioread32(davinci_spi->base + SPIFLG);
  595. buf_val = ioread32(davinci_spi->base + SPIBUF);
  596. davinci_spi->get_rx(buf_val, davinci_spi);
  597. count--;
  598. if (count <= 0)
  599. break;
  600. }
  601. } else { /* Receive in Interrupt mode */
  602. int i;
  603. for (i = 0; i < davinci_spi->count; i++) {
  604. set_io_bits(davinci_spi->base + SPIINT,
  605. SPIINT_BITERR_INTR
  606. | SPIINT_OVRRUN_INTR
  607. | SPIINT_RX_INTR);
  608. iowrite32(data1_reg_val,
  609. davinci_spi->base + SPIDAT1);
  610. while (ioread32(davinci_spi->base + SPIINT) &
  611. SPIINT_RX_INTR)
  612. cpu_relax();
  613. }
  614. iowrite32((data1_reg_val & 0x0ffcffff),
  615. davinci_spi->base + SPIDAT1);
  616. }
  617. }
  618. /*
  619. * Check for bit error, desync error,parity error,timeout error and
  620. * receive overflow errors
  621. */
  622. int_status = ioread32(davinci_spi->base + SPIFLG);
  623. ret = davinci_spi_check_error(davinci_spi, int_status);
  624. if (ret != 0)
  625. return ret;
  626. /* SPI Framework maintains the count only in bytes so convert back */
  627. davinci_spi->count *= conv;
  628. return t->len;
  629. }
  630. #define DAVINCI_DMA_DATA_TYPE_S8 0x01
  631. #define DAVINCI_DMA_DATA_TYPE_S16 0x02
  632. #define DAVINCI_DMA_DATA_TYPE_S32 0x04
  633. static int davinci_spi_bufs_dma(struct spi_device *spi, struct spi_transfer *t)
  634. {
  635. struct davinci_spi *davinci_spi;
  636. int int_status = 0;
  637. int count, temp_count;
  638. u8 conv = 1;
  639. u32 data1_reg_val;
  640. struct davinci_spi_dma *davinci_spi_dma;
  641. int word_len, data_type, ret;
  642. unsigned long tx_reg, rx_reg;
  643. struct davinci_spi_platform_data *pdata;
  644. struct device *sdev;
  645. davinci_spi = spi_master_get_devdata(spi->master);
  646. pdata = davinci_spi->pdata;
  647. sdev = davinci_spi->bitbang.master->dev.parent;
  648. davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
  649. tx_reg = (unsigned long)davinci_spi->pbase + SPIDAT1;
  650. rx_reg = (unsigned long)davinci_spi->pbase + SPIBUF;
  651. davinci_spi->tx = t->tx_buf;
  652. davinci_spi->rx = t->rx_buf;
  653. /* convert len to words based on bits_per_word */
  654. conv = davinci_spi->slave[spi->chip_select].bytes_per_word;
  655. davinci_spi->count = t->len / conv;
  656. data1_reg_val = ioread32(davinci_spi->base + SPIDAT1);
  657. INIT_COMPLETION(davinci_spi->done);
  658. init_completion(&davinci_spi_dma->dma_rx_completion);
  659. init_completion(&davinci_spi_dma->dma_tx_completion);
  660. word_len = conv * 8;
  661. if (word_len <= 8)
  662. data_type = DAVINCI_DMA_DATA_TYPE_S8;
  663. else if (word_len <= 16)
  664. data_type = DAVINCI_DMA_DATA_TYPE_S16;
  665. else if (word_len <= 32)
  666. data_type = DAVINCI_DMA_DATA_TYPE_S32;
  667. else
  668. return -EINVAL;
  669. ret = davinci_spi_bufs_prep(spi, davinci_spi);
  670. if (ret)
  671. return ret;
  672. /* Put delay val if required */
  673. iowrite32(0 | (pdata->c2tdelay << SPI_C2TDELAY_SHIFT) |
  674. (pdata->t2cdelay << SPI_T2CDELAY_SHIFT),
  675. davinci_spi->base + SPIDELAY);
  676. count = davinci_spi->count; /* the number of elements */
  677. /* disable all interrupts for dma transfers */
  678. clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKALL);
  679. /* Disable SPI to write configuration bits in SPIDAT */
  680. clear_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
  681. /* Enable SPI */
  682. set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
  683. if (t->tx_buf) {
  684. t->tx_dma = dma_map_single(&spi->dev, (void *)t->tx_buf, count,
  685. DMA_TO_DEVICE);
  686. if (dma_mapping_error(&spi->dev, t->tx_dma)) {
  687. dev_dbg(sdev, "Unable to DMA map a %d bytes"
  688. " TX buffer\n", count);
  689. return -ENOMEM;
  690. }
  691. temp_count = count;
  692. } else {
  693. /* We need TX clocking for RX transaction */
  694. t->tx_dma = dma_map_single(&spi->dev,
  695. (void *)davinci_spi->tmp_buf, count + 1,
  696. DMA_TO_DEVICE);
  697. if (dma_mapping_error(&spi->dev, t->tx_dma)) {
  698. dev_dbg(sdev, "Unable to DMA map a %d bytes"
  699. " TX tmp buffer\n", count);
  700. return -ENOMEM;
  701. }
  702. temp_count = count + 1;
  703. }
  704. edma_set_transfer_params(davinci_spi_dma->dma_tx_channel,
  705. data_type, temp_count, 1, 0, ASYNC);
  706. edma_set_dest(davinci_spi_dma->dma_tx_channel, tx_reg, INCR, W8BIT);
  707. edma_set_src(davinci_spi_dma->dma_tx_channel, t->tx_dma, INCR, W8BIT);
  708. edma_set_src_index(davinci_spi_dma->dma_tx_channel, data_type, 0);
  709. edma_set_dest_index(davinci_spi_dma->dma_tx_channel, 0, 0);
  710. if (t->rx_buf) {
  711. /* initiate transaction */
  712. iowrite32(data1_reg_val, davinci_spi->base + SPIDAT1);
  713. t->rx_dma = dma_map_single(&spi->dev, (void *)t->rx_buf, count,
  714. DMA_FROM_DEVICE);
  715. if (dma_mapping_error(&spi->dev, t->rx_dma)) {
  716. dev_dbg(sdev, "Couldn't DMA map a %d bytes RX buffer\n",
  717. count);
  718. if (t->tx_buf != NULL)
  719. dma_unmap_single(NULL, t->tx_dma,
  720. count, DMA_TO_DEVICE);
  721. return -ENOMEM;
  722. }
  723. edma_set_transfer_params(davinci_spi_dma->dma_rx_channel,
  724. data_type, count, 1, 0, ASYNC);
  725. edma_set_src(davinci_spi_dma->dma_rx_channel,
  726. rx_reg, INCR, W8BIT);
  727. edma_set_dest(davinci_spi_dma->dma_rx_channel,
  728. t->rx_dma, INCR, W8BIT);
  729. edma_set_src_index(davinci_spi_dma->dma_rx_channel, 0, 0);
  730. edma_set_dest_index(davinci_spi_dma->dma_rx_channel,
  731. data_type, 0);
  732. }
  733. if ((t->tx_buf) || (t->rx_buf))
  734. edma_start(davinci_spi_dma->dma_tx_channel);
  735. if (t->rx_buf)
  736. edma_start(davinci_spi_dma->dma_rx_channel);
  737. if ((t->rx_buf) || (t->tx_buf))
  738. davinci_spi_set_dma_req(spi, 1);
  739. if (t->tx_buf)
  740. wait_for_completion_interruptible(
  741. &davinci_spi_dma->dma_tx_completion);
  742. if (t->rx_buf)
  743. wait_for_completion_interruptible(
  744. &davinci_spi_dma->dma_rx_completion);
  745. dma_unmap_single(NULL, t->tx_dma, temp_count, DMA_TO_DEVICE);
  746. if (t->rx_buf)
  747. dma_unmap_single(NULL, t->rx_dma, count, DMA_FROM_DEVICE);
  748. /*
  749. * Check for bit error, desync error,parity error,timeout error and
  750. * receive overflow errors
  751. */
  752. int_status = ioread32(davinci_spi->base + SPIFLG);
  753. ret = davinci_spi_check_error(davinci_spi, int_status);
  754. if (ret != 0)
  755. return ret;
  756. /* SPI Framework maintains the count only in bytes so convert back */
  757. davinci_spi->count *= conv;
  758. return t->len;
  759. }
  760. /**
  761. * davinci_spi_irq - IRQ handler for DaVinci SPI
  762. * @irq: IRQ number for this SPI Master
  763. * @context_data: structure for SPI Master controller davinci_spi
  764. */
  765. static irqreturn_t davinci_spi_irq(s32 irq, void *context_data)
  766. {
  767. struct davinci_spi *davinci_spi = context_data;
  768. u32 int_status, rx_data = 0;
  769. irqreturn_t ret = IRQ_NONE;
  770. int_status = ioread32(davinci_spi->base + SPIFLG);
  771. while ((int_status & SPIFLG_RX_INTR_MASK)) {
  772. if (likely(int_status & SPIFLG_RX_INTR_MASK)) {
  773. ret = IRQ_HANDLED;
  774. rx_data = ioread32(davinci_spi->base + SPIBUF);
  775. davinci_spi->get_rx(rx_data, davinci_spi);
  776. /* Disable Receive Interrupt */
  777. iowrite32(~(SPIINT_RX_INTR | SPIINT_TX_INTR),
  778. davinci_spi->base + SPIINT);
  779. } else
  780. (void)davinci_spi_check_error(davinci_spi, int_status);
  781. int_status = ioread32(davinci_spi->base + SPIFLG);
  782. }
  783. return ret;
  784. }
  785. /**
  786. * davinci_spi_probe - probe function for SPI Master Controller
  787. * @pdev: platform_device structure which contains plateform specific data
  788. */
  789. static int davinci_spi_probe(struct platform_device *pdev)
  790. {
  791. struct spi_master *master;
  792. struct davinci_spi *davinci_spi;
  793. struct davinci_spi_platform_data *pdata;
  794. struct resource *r, *mem;
  795. resource_size_t dma_rx_chan = SPI_NO_RESOURCE;
  796. resource_size_t dma_tx_chan = SPI_NO_RESOURCE;
  797. resource_size_t dma_eventq = SPI_NO_RESOURCE;
  798. int i = 0, ret = 0;
  799. pdata = pdev->dev.platform_data;
  800. if (pdata == NULL) {
  801. ret = -ENODEV;
  802. goto err;
  803. }
  804. master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi));
  805. if (master == NULL) {
  806. ret = -ENOMEM;
  807. goto err;
  808. }
  809. dev_set_drvdata(&pdev->dev, master);
  810. davinci_spi = spi_master_get_devdata(master);
  811. if (davinci_spi == NULL) {
  812. ret = -ENOENT;
  813. goto free_master;
  814. }
  815. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  816. if (r == NULL) {
  817. ret = -ENOENT;
  818. goto free_master;
  819. }
  820. davinci_spi->pbase = r->start;
  821. davinci_spi->region_size = resource_size(r);
  822. davinci_spi->pdata = pdata;
  823. mem = request_mem_region(r->start, davinci_spi->region_size,
  824. pdev->name);
  825. if (mem == NULL) {
  826. ret = -EBUSY;
  827. goto free_master;
  828. }
  829. davinci_spi->base = ioremap(r->start, davinci_spi->region_size);
  830. if (davinci_spi->base == NULL) {
  831. ret = -ENOMEM;
  832. goto release_region;
  833. }
  834. davinci_spi->irq = platform_get_irq(pdev, 0);
  835. if (davinci_spi->irq <= 0) {
  836. ret = -EINVAL;
  837. goto unmap_io;
  838. }
  839. ret = request_irq(davinci_spi->irq, davinci_spi_irq, IRQF_DISABLED,
  840. dev_name(&pdev->dev), davinci_spi);
  841. if (ret)
  842. goto unmap_io;
  843. /* Allocate tmp_buf for tx_buf */
  844. davinci_spi->tmp_buf = kzalloc(SPI_BUFSIZ, GFP_KERNEL);
  845. if (davinci_spi->tmp_buf == NULL) {
  846. ret = -ENOMEM;
  847. goto irq_free;
  848. }
  849. davinci_spi->bitbang.master = spi_master_get(master);
  850. if (davinci_spi->bitbang.master == NULL) {
  851. ret = -ENODEV;
  852. goto free_tmp_buf;
  853. }
  854. davinci_spi->clk = clk_get(&pdev->dev, NULL);
  855. if (IS_ERR(davinci_spi->clk)) {
  856. ret = -ENODEV;
  857. goto put_master;
  858. }
  859. clk_enable(davinci_spi->clk);
  860. master->bus_num = pdev->id;
  861. master->num_chipselect = pdata->num_chipselect;
  862. master->setup = davinci_spi_setup;
  863. master->cleanup = davinci_spi_cleanup;
  864. davinci_spi->bitbang.chipselect = davinci_spi_chipselect;
  865. davinci_spi->bitbang.setup_transfer = davinci_spi_setup_transfer;
  866. davinci_spi->version = pdata->version;
  867. use_dma = pdata->use_dma;
  868. davinci_spi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP;
  869. if (davinci_spi->version == SPI_VERSION_2)
  870. davinci_spi->bitbang.flags |= SPI_READY;
  871. if (use_dma) {
  872. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  873. if (r)
  874. dma_rx_chan = r->start;
  875. r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  876. if (r)
  877. dma_tx_chan = r->start;
  878. r = platform_get_resource(pdev, IORESOURCE_DMA, 2);
  879. if (r)
  880. dma_eventq = r->start;
  881. }
  882. if (!use_dma ||
  883. dma_rx_chan == SPI_NO_RESOURCE ||
  884. dma_tx_chan == SPI_NO_RESOURCE ||
  885. dma_eventq == SPI_NO_RESOURCE) {
  886. davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_pio;
  887. use_dma = 0;
  888. } else {
  889. davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_dma;
  890. davinci_spi->dma_channels = kzalloc(master->num_chipselect
  891. * sizeof(struct davinci_spi_dma), GFP_KERNEL);
  892. if (davinci_spi->dma_channels == NULL) {
  893. ret = -ENOMEM;
  894. goto free_clk;
  895. }
  896. for (i = 0; i < master->num_chipselect; i++) {
  897. davinci_spi->dma_channels[i].dma_rx_channel = -1;
  898. davinci_spi->dma_channels[i].dma_rx_sync_dev =
  899. dma_rx_chan;
  900. davinci_spi->dma_channels[i].dma_tx_channel = -1;
  901. davinci_spi->dma_channels[i].dma_tx_sync_dev =
  902. dma_tx_chan;
  903. davinci_spi->dma_channels[i].eventq = dma_eventq;
  904. }
  905. dev_info(&pdev->dev, "DaVinci SPI driver in EDMA mode\n"
  906. "Using RX channel = %d , TX channel = %d and "
  907. "event queue = %d", dma_rx_chan, dma_tx_chan,
  908. dma_eventq);
  909. }
  910. davinci_spi->get_rx = davinci_spi_rx_buf_u8;
  911. davinci_spi->get_tx = davinci_spi_tx_buf_u8;
  912. init_completion(&davinci_spi->done);
  913. /* Reset In/OUT SPI module */
  914. iowrite32(0, davinci_spi->base + SPIGCR0);
  915. udelay(100);
  916. iowrite32(1, davinci_spi->base + SPIGCR0);
  917. /* Clock internal */
  918. if (davinci_spi->pdata->clk_internal)
  919. set_io_bits(davinci_spi->base + SPIGCR1,
  920. SPIGCR1_CLKMOD_MASK);
  921. else
  922. clear_io_bits(davinci_spi->base + SPIGCR1,
  923. SPIGCR1_CLKMOD_MASK);
  924. iowrite32(CS_DEFAULT, davinci_spi->base + SPIDEF);
  925. /* master mode default */
  926. set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_MASTER_MASK);
  927. if (davinci_spi->pdata->intr_level)
  928. iowrite32(SPI_INTLVL_1, davinci_spi->base + SPILVL);
  929. else
  930. iowrite32(SPI_INTLVL_0, davinci_spi->base + SPILVL);
  931. ret = spi_bitbang_start(&davinci_spi->bitbang);
  932. if (ret)
  933. goto free_clk;
  934. dev_info(&pdev->dev, "Controller at 0x%p\n", davinci_spi->base);
  935. if (!pdata->poll_mode)
  936. dev_info(&pdev->dev, "Operating in interrupt mode"
  937. " using IRQ %d\n", davinci_spi->irq);
  938. return ret;
  939. free_clk:
  940. clk_disable(davinci_spi->clk);
  941. clk_put(davinci_spi->clk);
  942. put_master:
  943. spi_master_put(master);
  944. free_tmp_buf:
  945. kfree(davinci_spi->tmp_buf);
  946. irq_free:
  947. free_irq(davinci_spi->irq, davinci_spi);
  948. unmap_io:
  949. iounmap(davinci_spi->base);
  950. release_region:
  951. release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
  952. free_master:
  953. kfree(master);
  954. err:
  955. return ret;
  956. }
  957. /**
  958. * davinci_spi_remove - remove function for SPI Master Controller
  959. * @pdev: platform_device structure which contains plateform specific data
  960. *
  961. * This function will do the reverse action of davinci_spi_probe function
  962. * It will free the IRQ and SPI controller's memory region.
  963. * It will also call spi_bitbang_stop to destroy the work queue which was
  964. * created by spi_bitbang_start.
  965. */
  966. static int __exit davinci_spi_remove(struct platform_device *pdev)
  967. {
  968. struct davinci_spi *davinci_spi;
  969. struct spi_master *master;
  970. master = dev_get_drvdata(&pdev->dev);
  971. davinci_spi = spi_master_get_devdata(master);
  972. spi_bitbang_stop(&davinci_spi->bitbang);
  973. clk_disable(davinci_spi->clk);
  974. clk_put(davinci_spi->clk);
  975. spi_master_put(master);
  976. kfree(davinci_spi->tmp_buf);
  977. free_irq(davinci_spi->irq, davinci_spi);
  978. iounmap(davinci_spi->base);
  979. release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
  980. return 0;
  981. }
  982. static struct platform_driver davinci_spi_driver = {
  983. .driver.name = "spi_davinci",
  984. .remove = __exit_p(davinci_spi_remove),
  985. };
  986. static int __init davinci_spi_init(void)
  987. {
  988. return platform_driver_probe(&davinci_spi_driver, davinci_spi_probe);
  989. }
  990. module_init(davinci_spi_init);
  991. static void __exit davinci_spi_exit(void)
  992. {
  993. platform_driver_unregister(&davinci_spi_driver);
  994. }
  995. module_exit(davinci_spi_exit);
  996. MODULE_DESCRIPTION("TI DaVinci SPI Master Controller Driver");
  997. MODULE_LICENSE("GPL");