bf5xx-ac97.c 11 KB

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