bf6xx-sport.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * bf6xx_sport.c Analog Devices BF6XX SPORT driver
  3. *
  4. * Copyright (c) 2012 Analog Devices Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/device.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include <asm/blackfin.h>
  26. #include <asm/dma.h>
  27. #include <asm/portmux.h>
  28. #include "bf6xx-sport.h"
  29. int sport_set_tx_params(struct sport_device *sport,
  30. struct sport_params *params)
  31. {
  32. if (sport->tx_regs->spctl & SPORT_CTL_SPENPRI)
  33. return -EBUSY;
  34. sport->tx_regs->spctl = params->spctl | SPORT_CTL_SPTRAN;
  35. sport->tx_regs->div = params->div;
  36. SSYNC();
  37. return 0;
  38. }
  39. EXPORT_SYMBOL(sport_set_tx_params);
  40. int sport_set_rx_params(struct sport_device *sport,
  41. struct sport_params *params)
  42. {
  43. if (sport->rx_regs->spctl & SPORT_CTL_SPENPRI)
  44. return -EBUSY;
  45. sport->rx_regs->spctl = params->spctl & ~SPORT_CTL_SPTRAN;
  46. sport->rx_regs->div = params->div;
  47. SSYNC();
  48. return 0;
  49. }
  50. EXPORT_SYMBOL(sport_set_rx_params);
  51. static int compute_wdsize(size_t wdsize)
  52. {
  53. switch (wdsize) {
  54. case 1:
  55. return WDSIZE_8 | PSIZE_8;
  56. case 2:
  57. return WDSIZE_16 | PSIZE_16;
  58. default:
  59. return WDSIZE_32 | PSIZE_32;
  60. }
  61. }
  62. void sport_tx_start(struct sport_device *sport)
  63. {
  64. set_dma_next_desc_addr(sport->tx_dma_chan, sport->tx_desc);
  65. set_dma_config(sport->tx_dma_chan, DMAFLOW_LIST | DI_EN
  66. | compute_wdsize(sport->wdsize) | NDSIZE_6);
  67. enable_dma(sport->tx_dma_chan);
  68. sport->tx_regs->spctl |= SPORT_CTL_SPENPRI;
  69. SSYNC();
  70. }
  71. EXPORT_SYMBOL(sport_tx_start);
  72. void sport_rx_start(struct sport_device *sport)
  73. {
  74. set_dma_next_desc_addr(sport->rx_dma_chan, sport->rx_desc);
  75. set_dma_config(sport->rx_dma_chan, DMAFLOW_LIST | DI_EN | WNR
  76. | compute_wdsize(sport->wdsize) | NDSIZE_6);
  77. enable_dma(sport->rx_dma_chan);
  78. sport->rx_regs->spctl |= SPORT_CTL_SPENPRI;
  79. SSYNC();
  80. }
  81. EXPORT_SYMBOL(sport_rx_start);
  82. void sport_tx_stop(struct sport_device *sport)
  83. {
  84. sport->tx_regs->spctl &= ~SPORT_CTL_SPENPRI;
  85. SSYNC();
  86. disable_dma(sport->tx_dma_chan);
  87. }
  88. EXPORT_SYMBOL(sport_tx_stop);
  89. void sport_rx_stop(struct sport_device *sport)
  90. {
  91. sport->rx_regs->spctl &= ~SPORT_CTL_SPENPRI;
  92. SSYNC();
  93. disable_dma(sport->rx_dma_chan);
  94. }
  95. EXPORT_SYMBOL(sport_rx_stop);
  96. void sport_set_tx_callback(struct sport_device *sport,
  97. void (*tx_callback)(void *), void *tx_data)
  98. {
  99. sport->tx_callback = tx_callback;
  100. sport->tx_data = tx_data;
  101. }
  102. EXPORT_SYMBOL(sport_set_tx_callback);
  103. void sport_set_rx_callback(struct sport_device *sport,
  104. void (*rx_callback)(void *), void *rx_data)
  105. {
  106. sport->rx_callback = rx_callback;
  107. sport->rx_data = rx_data;
  108. }
  109. EXPORT_SYMBOL(sport_set_rx_callback);
  110. static void setup_desc(struct dmasg *desc, void *buf, int fragcount,
  111. size_t fragsize, unsigned int cfg,
  112. unsigned int count, size_t wdsize)
  113. {
  114. int i;
  115. for (i = 0; i < fragcount; ++i) {
  116. desc[i].next_desc_addr = &(desc[i + 1]);
  117. desc[i].start_addr = (unsigned long)buf + i*fragsize;
  118. desc[i].cfg = cfg;
  119. desc[i].x_count = count;
  120. desc[i].x_modify = wdsize;
  121. desc[i].y_count = 0;
  122. desc[i].y_modify = 0;
  123. }
  124. /* make circular */
  125. desc[fragcount-1].next_desc_addr = desc;
  126. }
  127. int sport_config_tx_dma(struct sport_device *sport, void *buf,
  128. int fragcount, size_t fragsize)
  129. {
  130. unsigned int count;
  131. unsigned int cfg;
  132. dma_addr_t addr;
  133. count = fragsize/sport->wdsize;
  134. if (sport->tx_desc)
  135. dma_free_coherent(NULL, sport->tx_desc_size,
  136. sport->tx_desc, 0);
  137. sport->tx_desc = dma_alloc_coherent(NULL,
  138. fragcount * sizeof(struct dmasg), &addr, 0);
  139. sport->tx_desc_size = fragcount * sizeof(struct dmasg);
  140. if (!sport->tx_desc)
  141. return -ENOMEM;
  142. sport->tx_buf = buf;
  143. sport->tx_fragsize = fragsize;
  144. sport->tx_frags = fragcount;
  145. cfg = DMAFLOW_LIST | DI_EN | compute_wdsize(sport->wdsize) | NDSIZE_6;
  146. setup_desc(sport->tx_desc, buf, fragcount, fragsize,
  147. cfg|DMAEN, count, sport->wdsize);
  148. return 0;
  149. }
  150. EXPORT_SYMBOL(sport_config_tx_dma);
  151. int sport_config_rx_dma(struct sport_device *sport, void *buf,
  152. int fragcount, size_t fragsize)
  153. {
  154. unsigned int count;
  155. unsigned int cfg;
  156. dma_addr_t addr;
  157. count = fragsize/sport->wdsize;
  158. if (sport->rx_desc)
  159. dma_free_coherent(NULL, sport->rx_desc_size,
  160. sport->rx_desc, 0);
  161. sport->rx_desc = dma_alloc_coherent(NULL,
  162. fragcount * sizeof(struct dmasg), &addr, 0);
  163. sport->rx_desc_size = fragcount * sizeof(struct dmasg);
  164. if (!sport->rx_desc)
  165. return -ENOMEM;
  166. sport->rx_buf = buf;
  167. sport->rx_fragsize = fragsize;
  168. sport->rx_frags = fragcount;
  169. cfg = DMAFLOW_LIST | DI_EN | compute_wdsize(sport->wdsize)
  170. | WNR | NDSIZE_6;
  171. setup_desc(sport->rx_desc, buf, fragcount, fragsize,
  172. cfg|DMAEN, count, sport->wdsize);
  173. return 0;
  174. }
  175. EXPORT_SYMBOL(sport_config_rx_dma);
  176. unsigned long sport_curr_offset_tx(struct sport_device *sport)
  177. {
  178. unsigned long curr = get_dma_curr_addr(sport->tx_dma_chan);
  179. return (unsigned char *)curr - sport->tx_buf;
  180. }
  181. EXPORT_SYMBOL(sport_curr_offset_tx);
  182. unsigned long sport_curr_offset_rx(struct sport_device *sport)
  183. {
  184. unsigned long curr = get_dma_curr_addr(sport->rx_dma_chan);
  185. return (unsigned char *)curr - sport->rx_buf;
  186. }
  187. EXPORT_SYMBOL(sport_curr_offset_rx);
  188. static irqreturn_t sport_tx_irq(int irq, void *dev_id)
  189. {
  190. struct sport_device *sport = dev_id;
  191. static unsigned long status;
  192. status = get_dma_curr_irqstat(sport->tx_dma_chan);
  193. if (status & (DMA_DONE|DMA_ERR)) {
  194. clear_dma_irqstat(sport->tx_dma_chan);
  195. SSYNC();
  196. }
  197. if (sport->tx_callback)
  198. sport->tx_callback(sport->tx_data);
  199. return IRQ_HANDLED;
  200. }
  201. static irqreturn_t sport_rx_irq(int irq, void *dev_id)
  202. {
  203. struct sport_device *sport = dev_id;
  204. unsigned long status;
  205. status = get_dma_curr_irqstat(sport->rx_dma_chan);
  206. if (status & (DMA_DONE|DMA_ERR)) {
  207. clear_dma_irqstat(sport->rx_dma_chan);
  208. SSYNC();
  209. }
  210. if (sport->rx_callback)
  211. sport->rx_callback(sport->rx_data);
  212. return IRQ_HANDLED;
  213. }
  214. static irqreturn_t sport_err_irq(int irq, void *dev_id)
  215. {
  216. struct sport_device *sport = dev_id;
  217. struct device *dev = &sport->pdev->dev;
  218. if (sport->tx_regs->spctl & SPORT_CTL_DERRPRI)
  219. dev_err(dev, "sport error: TUVF\n");
  220. if (sport->rx_regs->spctl & SPORT_CTL_DERRPRI)
  221. dev_err(dev, "sport error: ROVF\n");
  222. return IRQ_HANDLED;
  223. }
  224. static int sport_get_resource(struct sport_device *sport)
  225. {
  226. struct platform_device *pdev = sport->pdev;
  227. struct device *dev = &pdev->dev;
  228. struct bfin_snd_platform_data *pdata = dev->platform_data;
  229. struct resource *res;
  230. if (!pdata) {
  231. dev_err(dev, "No platform data\n");
  232. return -ENODEV;
  233. }
  234. sport->pin_req = pdata->pin_req;
  235. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  236. if (!res) {
  237. dev_err(dev, "No tx MEM resource\n");
  238. return -ENODEV;
  239. }
  240. sport->tx_regs = (struct sport_register *)res->start;
  241. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  242. if (!res) {
  243. dev_err(dev, "No rx MEM resource\n");
  244. return -ENODEV;
  245. }
  246. sport->rx_regs = (struct sport_register *)res->start;
  247. res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  248. if (!res) {
  249. dev_err(dev, "No tx DMA resource\n");
  250. return -ENODEV;
  251. }
  252. sport->tx_dma_chan = res->start;
  253. res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  254. if (!res) {
  255. dev_err(dev, "No rx DMA resource\n");
  256. return -ENODEV;
  257. }
  258. sport->rx_dma_chan = res->start;
  259. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  260. if (!res) {
  261. dev_err(dev, "No tx error irq resource\n");
  262. return -ENODEV;
  263. }
  264. sport->tx_err_irq = res->start;
  265. res = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  266. if (!res) {
  267. dev_err(dev, "No rx error irq resource\n");
  268. return -ENODEV;
  269. }
  270. sport->rx_err_irq = res->start;
  271. return 0;
  272. }
  273. static int sport_request_resource(struct sport_device *sport)
  274. {
  275. struct device *dev = &sport->pdev->dev;
  276. int ret;
  277. ret = peripheral_request_list(sport->pin_req, "soc-audio");
  278. if (ret) {
  279. dev_err(dev, "Unable to request sport pin\n");
  280. return ret;
  281. }
  282. ret = request_dma(sport->tx_dma_chan, "SPORT TX Data");
  283. if (ret) {
  284. dev_err(dev, "Unable to allocate DMA channel for sport tx\n");
  285. goto err_tx_dma;
  286. }
  287. set_dma_callback(sport->tx_dma_chan, sport_tx_irq, sport);
  288. ret = request_dma(sport->rx_dma_chan, "SPORT RX Data");
  289. if (ret) {
  290. dev_err(dev, "Unable to allocate DMA channel for sport rx\n");
  291. goto err_rx_dma;
  292. }
  293. set_dma_callback(sport->rx_dma_chan, sport_rx_irq, sport);
  294. ret = request_irq(sport->tx_err_irq, sport_err_irq,
  295. 0, "SPORT TX ERROR", sport);
  296. if (ret) {
  297. dev_err(dev, "Unable to allocate tx error IRQ for sport\n");
  298. goto err_tx_irq;
  299. }
  300. ret = request_irq(sport->rx_err_irq, sport_err_irq,
  301. 0, "SPORT RX ERROR", sport);
  302. if (ret) {
  303. dev_err(dev, "Unable to allocate rx error IRQ for sport\n");
  304. goto err_rx_irq;
  305. }
  306. return 0;
  307. err_rx_irq:
  308. free_irq(sport->tx_err_irq, sport);
  309. err_tx_irq:
  310. free_dma(sport->rx_dma_chan);
  311. err_rx_dma:
  312. free_dma(sport->tx_dma_chan);
  313. err_tx_dma:
  314. peripheral_free_list(sport->pin_req);
  315. return ret;
  316. }
  317. static void sport_free_resource(struct sport_device *sport)
  318. {
  319. free_irq(sport->rx_err_irq, sport);
  320. free_irq(sport->tx_err_irq, sport);
  321. free_dma(sport->rx_dma_chan);
  322. free_dma(sport->tx_dma_chan);
  323. peripheral_free_list(sport->pin_req);
  324. }
  325. struct sport_device *sport_create(struct platform_device *pdev)
  326. {
  327. struct device *dev = &pdev->dev;
  328. struct sport_device *sport;
  329. int ret;
  330. sport = kzalloc(sizeof(*sport), GFP_KERNEL);
  331. if (!sport) {
  332. dev_err(dev, "Unable to allocate memory for sport device\n");
  333. return NULL;
  334. }
  335. sport->pdev = pdev;
  336. ret = sport_get_resource(sport);
  337. if (ret) {
  338. kfree(sport);
  339. return NULL;
  340. }
  341. ret = sport_request_resource(sport);
  342. if (ret) {
  343. kfree(sport);
  344. return NULL;
  345. }
  346. dev_dbg(dev, "SPORT create success\n");
  347. return sport;
  348. }
  349. EXPORT_SYMBOL(sport_create);
  350. void sport_delete(struct sport_device *sport)
  351. {
  352. if (sport->tx_desc)
  353. dma_free_coherent(NULL, sport->tx_desc_size,
  354. sport->tx_desc, 0);
  355. if (sport->rx_desc)
  356. dma_free_coherent(NULL, sport->rx_desc_size,
  357. sport->rx_desc, 0);
  358. sport_free_resource(sport);
  359. kfree(sport);
  360. }
  361. EXPORT_SYMBOL(sport_delete);
  362. MODULE_DESCRIPTION("Analog Devices BF6XX SPORT driver");
  363. MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
  364. MODULE_LICENSE("GPL v2");