bf5xx-ac97.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * bf5xx-ac97.c -- AC97 support for the ADI blackfin chip.
  3. *
  4. * Author: Roy Huang
  5. * Created: 11th. June 2007
  6. * Copyright: Analog Device Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/wait.h>
  17. #include <linux/delay.h>
  18. #include <linux/slab.h>
  19. #include <sound/core.h>
  20. #include <sound/pcm.h>
  21. #include <sound/ac97_codec.h>
  22. #include <sound/initval.h>
  23. #include <sound/soc.h>
  24. #include <asm/irq.h>
  25. #include <asm/portmux.h>
  26. #include <linux/mutex.h>
  27. #include <linux/gpio.h>
  28. #include "bf5xx-sport.h"
  29. #include "bf5xx-ac97.h"
  30. /* Anomaly notes:
  31. * 05000250 - AD1980 is running in TDM mode and RFS/TFS are generated by SPORT
  32. * contrtoller. But, RFSDIV and TFSDIV are always set to 16*16-1,
  33. * while the max AC97 data size is 13*16. The DIV is always larger
  34. * than data size. AD73311 and ad2602 are not running in TDM mode.
  35. * AD1836 and AD73322 depend on external RFS/TFS only. So, this
  36. * anomaly does not affect blackfin sound drivers.
  37. */
  38. static int *cmd_count;
  39. static int sport_num = CONFIG_SND_BF5XX_SPORT_NUM;
  40. #define SPORT_REQ(x) \
  41. [x] = {P_SPORT##x##_TFS, P_SPORT##x##_DTPRI, P_SPORT##x##_TSCLK, \
  42. P_SPORT##x##_RFS, P_SPORT##x##_DRPRI, P_SPORT##x##_RSCLK, 0}
  43. static u16 sport_req[][7] = {
  44. #ifdef SPORT0_TCR1
  45. SPORT_REQ(0),
  46. #endif
  47. #ifdef SPORT1_TCR1
  48. SPORT_REQ(1),
  49. #endif
  50. #ifdef SPORT2_TCR1
  51. SPORT_REQ(2),
  52. #endif
  53. #ifdef SPORT3_TCR1
  54. SPORT_REQ(3),
  55. #endif
  56. };
  57. #define SPORT_PARAMS(x) \
  58. [x] = { \
  59. .dma_rx_chan = CH_SPORT##x##_RX, \
  60. .dma_tx_chan = CH_SPORT##x##_TX, \
  61. .err_irq = IRQ_SPORT##x##_ERROR, \
  62. .regs = (struct sport_register *)SPORT##x##_TCR1, \
  63. }
  64. static struct sport_param sport_params[4] = {
  65. #ifdef SPORT0_TCR1
  66. SPORT_PARAMS(0),
  67. #endif
  68. #ifdef SPORT1_TCR1
  69. SPORT_PARAMS(1),
  70. #endif
  71. #ifdef SPORT2_TCR1
  72. SPORT_PARAMS(2),
  73. #endif
  74. #ifdef SPORT3_TCR1
  75. SPORT_PARAMS(3),
  76. #endif
  77. };
  78. void bf5xx_pcm_to_ac97(struct ac97_frame *dst, const __u16 *src,
  79. size_t count, unsigned int chan_mask)
  80. {
  81. while (count--) {
  82. dst->ac97_tag = TAG_VALID;
  83. if (chan_mask & SP_FL) {
  84. dst->ac97_pcm_r = *src++;
  85. dst->ac97_tag |= TAG_PCM_RIGHT;
  86. }
  87. if (chan_mask & SP_FR) {
  88. dst->ac97_pcm_l = *src++;
  89. dst->ac97_tag |= TAG_PCM_LEFT;
  90. }
  91. #if defined(CONFIG_SND_BF5XX_MULTICHAN_SUPPORT)
  92. if (chan_mask & SP_SR) {
  93. dst->ac97_sl = *src++;
  94. dst->ac97_tag |= TAG_PCM_SL;
  95. }
  96. if (chan_mask & SP_SL) {
  97. dst->ac97_sr = *src++;
  98. dst->ac97_tag |= TAG_PCM_SR;
  99. }
  100. if (chan_mask & SP_LFE) {
  101. dst->ac97_lfe = *src++;
  102. dst->ac97_tag |= TAG_PCM_LFE;
  103. }
  104. if (chan_mask & SP_FC) {
  105. dst->ac97_center = *src++;
  106. dst->ac97_tag |= TAG_PCM_CENTER;
  107. }
  108. #endif
  109. dst++;
  110. }
  111. }
  112. EXPORT_SYMBOL(bf5xx_pcm_to_ac97);
  113. void bf5xx_ac97_to_pcm(const struct ac97_frame *src, __u16 *dst,
  114. size_t count)
  115. {
  116. while (count--) {
  117. *(dst++) = src->ac97_pcm_l;
  118. *(dst++) = src->ac97_pcm_r;
  119. src++;
  120. }
  121. }
  122. EXPORT_SYMBOL(bf5xx_ac97_to_pcm);
  123. static unsigned int sport_tx_curr_frag(struct sport_device *sport)
  124. {
  125. return sport->tx_curr_frag = sport_curr_offset_tx(sport) /
  126. sport->tx_fragsize;
  127. }
  128. static void enqueue_cmd(struct snd_ac97 *ac97, __u16 addr, __u16 data)
  129. {
  130. struct sport_device *sport = sport_handle;
  131. int nextfrag = sport_tx_curr_frag(sport);
  132. struct ac97_frame *nextwrite;
  133. sport_incfrag(sport, &nextfrag, 1);
  134. nextwrite = (struct ac97_frame *)(sport->tx_buf +
  135. nextfrag * sport->tx_fragsize);
  136. pr_debug("sport->tx_buf:%p, nextfrag:0x%x nextwrite:%p, cmd_count:%d\n",
  137. sport->tx_buf, nextfrag, nextwrite, cmd_count[nextfrag]);
  138. nextwrite[cmd_count[nextfrag]].ac97_tag |= TAG_CMD;
  139. nextwrite[cmd_count[nextfrag]].ac97_addr = addr;
  140. nextwrite[cmd_count[nextfrag]].ac97_data = data;
  141. ++cmd_count[nextfrag];
  142. pr_debug("ac97_sport: Inserting %02x/%04x into fragment %d\n",
  143. addr >> 8, data, nextfrag);
  144. }
  145. static unsigned short bf5xx_ac97_read(struct snd_ac97 *ac97,
  146. unsigned short reg)
  147. {
  148. struct ac97_frame out_frame[2], in_frame[2];
  149. pr_debug("%s enter 0x%x\n", __func__, reg);
  150. /* When dma descriptor is enabled, the register should not be read */
  151. if (sport_handle->tx_run || sport_handle->rx_run) {
  152. pr_err("Could you send a mail to cliff.cai@analog.com "
  153. "to report this?\n");
  154. return -EFAULT;
  155. }
  156. memset(&out_frame, 0, 2 * sizeof(struct ac97_frame));
  157. memset(&in_frame, 0, 2 * sizeof(struct ac97_frame));
  158. out_frame[0].ac97_tag = TAG_VALID | TAG_CMD;
  159. out_frame[0].ac97_addr = ((reg << 8) | 0x8000);
  160. sport_send_and_recv(sport_handle, (unsigned char *)&out_frame,
  161. (unsigned char *)&in_frame,
  162. 2 * sizeof(struct ac97_frame));
  163. return in_frame[1].ac97_data;
  164. }
  165. void bf5xx_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
  166. unsigned short val)
  167. {
  168. pr_debug("%s enter 0x%x:0x%04x\n", __func__, reg, val);
  169. if (sport_handle->tx_run) {
  170. enqueue_cmd(ac97, (reg << 8), val); /* write */
  171. enqueue_cmd(ac97, (reg << 8) | 0x8000, 0); /* read back */
  172. } else {
  173. struct ac97_frame frame;
  174. memset(&frame, 0, sizeof(struct ac97_frame));
  175. frame.ac97_tag = TAG_VALID | TAG_CMD;
  176. frame.ac97_addr = (reg << 8);
  177. frame.ac97_data = val;
  178. sport_send_and_recv(sport_handle, (unsigned char *)&frame, \
  179. NULL, sizeof(struct ac97_frame));
  180. }
  181. }
  182. static void bf5xx_ac97_warm_reset(struct snd_ac97 *ac97)
  183. {
  184. #if defined(CONFIG_BF54x) || defined(CONFIG_BF561) || \
  185. (defined(BF537_FAMILY) && (CONFIG_SND_BF5XX_SPORT_NUM == 1))
  186. #define CONCAT(a, b, c) a ## b ## c
  187. #define BFIN_SPORT_RFS(x) CONCAT(P_SPORT, x, _RFS)
  188. u16 per = BFIN_SPORT_RFS(CONFIG_SND_BF5XX_SPORT_NUM);
  189. u16 gpio = P_IDENT(BFIN_SPORT_RFS(CONFIG_SND_BF5XX_SPORT_NUM));
  190. pr_debug("%s enter\n", __func__);
  191. peripheral_free(per);
  192. gpio_request(gpio, "bf5xx-ac97");
  193. gpio_direction_output(gpio, 1);
  194. udelay(2);
  195. gpio_set_value(gpio, 0);
  196. udelay(1);
  197. gpio_free(gpio);
  198. peripheral_request(per, "soc-audio");
  199. #else
  200. pr_info("%s: Not implemented\n", __func__);
  201. #endif
  202. }
  203. static void bf5xx_ac97_cold_reset(struct snd_ac97 *ac97)
  204. {
  205. #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET
  206. pr_debug("%s enter\n", __func__);
  207. /* It is specified for bf548-ezkit */
  208. gpio_set_value(CONFIG_SND_BF5XX_RESET_GPIO_NUM, 0);
  209. /* Keep reset pin low for 1 ms */
  210. mdelay(1);
  211. gpio_set_value(CONFIG_SND_BF5XX_RESET_GPIO_NUM, 1);
  212. /* Wait for bit clock recover */
  213. mdelay(1);
  214. #else
  215. pr_info("%s: Not implemented\n", __func__);
  216. #endif
  217. }
  218. struct snd_ac97_bus_ops soc_ac97_ops = {
  219. .read = bf5xx_ac97_read,
  220. .write = bf5xx_ac97_write,
  221. .warm_reset = bf5xx_ac97_warm_reset,
  222. .reset = bf5xx_ac97_cold_reset,
  223. };
  224. EXPORT_SYMBOL_GPL(soc_ac97_ops);
  225. #ifdef CONFIG_PM
  226. static int bf5xx_ac97_suspend(struct snd_soc_dai *dai)
  227. {
  228. struct sport_device *sport = snd_soc_dai_get_drvdata(dai);
  229. pr_debug("%s : sport %d\n", __func__, dai->id);
  230. if (!dai->active)
  231. return 0;
  232. if (dai->capture_active)
  233. sport_rx_stop(sport);
  234. if (dai->playback_active)
  235. sport_tx_stop(sport);
  236. return 0;
  237. }
  238. static int bf5xx_ac97_resume(struct snd_soc_dai *dai)
  239. {
  240. int ret;
  241. struct sport_device *sport = snd_soc_dai_get_drvdata(dai);
  242. pr_debug("%s : sport %d\n", __func__, dai->id);
  243. if (!dai->active)
  244. return 0;
  245. #if defined(CONFIG_SND_BF5XX_MULTICHAN_SUPPORT)
  246. ret = sport_set_multichannel(sport, 16, 0x3FF, 1);
  247. #else
  248. ret = sport_set_multichannel(sport, 16, 0x1F, 1);
  249. #endif
  250. if (ret) {
  251. pr_err("SPORT is busy!\n");
  252. return -EBUSY;
  253. }
  254. ret = sport_config_rx(sport, IRFS, 0xF, 0, (16*16-1));
  255. if (ret) {
  256. pr_err("SPORT is busy!\n");
  257. return -EBUSY;
  258. }
  259. ret = sport_config_tx(sport, ITFS, 0xF, 0, (16*16-1));
  260. if (ret) {
  261. pr_err("SPORT is busy!\n");
  262. return -EBUSY;
  263. }
  264. return 0;
  265. }
  266. #else
  267. #define bf5xx_ac97_suspend NULL
  268. #define bf5xx_ac97_resume NULL
  269. #endif
  270. static int bf5xx_ac97_probe(struct snd_soc_dai *dai)
  271. {
  272. int ret = 0;
  273. cmd_count = (int *)get_zeroed_page(GFP_KERNEL);
  274. if (cmd_count == NULL)
  275. return -ENOMEM;
  276. if (peripheral_request_list(sport_req[sport_num], "soc-audio")) {
  277. pr_err("Requesting Peripherals failed\n");
  278. ret = -EFAULT;
  279. goto peripheral_err;
  280. }
  281. #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET
  282. /* Request PB3 as reset pin */
  283. if (gpio_request(CONFIG_SND_BF5XX_RESET_GPIO_NUM, "SND_AD198x RESET")) {
  284. pr_err("Failed to request GPIO_%d for reset\n",
  285. CONFIG_SND_BF5XX_RESET_GPIO_NUM);
  286. ret = -1;
  287. goto gpio_err;
  288. }
  289. gpio_direction_output(CONFIG_SND_BF5XX_RESET_GPIO_NUM, 1);
  290. #endif
  291. sport_handle = sport_init(&sport_params[sport_num], 2, \
  292. sizeof(struct ac97_frame), NULL);
  293. if (!sport_handle) {
  294. ret = -ENODEV;
  295. goto sport_err;
  296. }
  297. /*SPORT works in TDM mode to simulate AC97 transfers*/
  298. #if defined(CONFIG_SND_BF5XX_MULTICHAN_SUPPORT)
  299. ret = sport_set_multichannel(sport_handle, 16, 0x3FF, 1);
  300. #else
  301. ret = sport_set_multichannel(sport_handle, 16, 0x1F, 1);
  302. #endif
  303. if (ret) {
  304. pr_err("SPORT is busy!\n");
  305. ret = -EBUSY;
  306. goto sport_config_err;
  307. }
  308. ret = sport_config_rx(sport_handle, IRFS, 0xF, 0, (16*16-1));
  309. if (ret) {
  310. pr_err("SPORT is busy!\n");
  311. ret = -EBUSY;
  312. goto sport_config_err;
  313. }
  314. ret = sport_config_tx(sport_handle, ITFS, 0xF, 0, (16*16-1));
  315. if (ret) {
  316. pr_err("SPORT is busy!\n");
  317. ret = -EBUSY;
  318. goto sport_config_err;
  319. }
  320. return 0;
  321. sport_config_err:
  322. kfree(sport_handle);
  323. sport_err:
  324. #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET
  325. gpio_free(CONFIG_SND_BF5XX_RESET_GPIO_NUM);
  326. gpio_err:
  327. #endif
  328. peripheral_free_list(sport_req[sport_num]);
  329. peripheral_err:
  330. free_page((unsigned long)cmd_count);
  331. cmd_count = NULL;
  332. return ret;
  333. }
  334. static int bf5xx_ac97_remove(struct snd_soc_dai *dai)
  335. {
  336. free_page((unsigned long)cmd_count);
  337. cmd_count = NULL;
  338. peripheral_free_list(sport_req[sport_num]);
  339. #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET
  340. gpio_free(CONFIG_SND_BF5XX_RESET_GPIO_NUM);
  341. #endif
  342. return 0;
  343. }
  344. struct snd_soc_dai_driver bfin_ac97_dai = {
  345. .ac97_control = 1,
  346. .probe = bf5xx_ac97_probe,
  347. .remove = bf5xx_ac97_remove,
  348. .suspend = bf5xx_ac97_suspend,
  349. .resume = bf5xx_ac97_resume,
  350. .playback = {
  351. .stream_name = "AC97 Playback",
  352. .channels_min = 2,
  353. #if defined(CONFIG_SND_BF5XX_MULTICHAN_SUPPORT)
  354. .channels_max = 6,
  355. #else
  356. .channels_max = 2,
  357. #endif
  358. .rates = SNDRV_PCM_RATE_48000,
  359. .formats = SNDRV_PCM_FMTBIT_S16_LE, },
  360. .capture = {
  361. .stream_name = "AC97 Capture",
  362. .channels_min = 2,
  363. .channels_max = 2,
  364. .rates = SNDRV_PCM_RATE_48000,
  365. .formats = SNDRV_PCM_FMTBIT_S16_LE, },
  366. };
  367. EXPORT_SYMBOL_GPL(bfin_ac97_dai);
  368. static __devinit int asoc_bfin_ac97_probe(struct platform_device *pdev)
  369. {
  370. return snd_soc_register_dai(&pdev->dev, &bfin_ac97_dai);
  371. }
  372. static int __devexit asoc_bfin_ac97_remove(struct platform_device *pdev)
  373. {
  374. snd_soc_unregister_dai(&pdev->dev);
  375. return 0;
  376. }
  377. static struct platform_driver asoc_bfin_ac97_driver = {
  378. .driver = {
  379. .name = "bfin-ac97",
  380. .owner = THIS_MODULE,
  381. },
  382. .probe = asoc_bfin_ac97_probe,
  383. .remove = __devexit_p(asoc_bfin_ac97_remove),
  384. };
  385. static int __init bfin_ac97_init(void)
  386. {
  387. return platform_driver_register(&asoc_bfin_ac97_driver);
  388. }
  389. module_init(bfin_ac97_init);
  390. static void __exit bfin_ac97_exit(void)
  391. {
  392. platform_driver_unregister(&asoc_bfin_ac97_driver);
  393. }
  394. module_exit(bfin_ac97_exit);
  395. MODULE_AUTHOR("Roy Huang");
  396. MODULE_DESCRIPTION("AC97 driver for ADI Blackfin");
  397. MODULE_LICENSE("GPL");