davinci_spi.c 32 KB

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