bf5xx-ac97.c 10 KB

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