bf5xx-ac97.c 9.6 KB

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