davinci_spi.c 32 KB

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