bf5xx-ac97.c 11 KB

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