bf5xx-ac97.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. static struct snd_ac97_bus_ops bf5xx_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. #ifdef CONFIG_PM
  182. static int bf5xx_ac97_suspend(struct snd_soc_dai *dai)
  183. {
  184. struct sport_device *sport = snd_soc_dai_get_drvdata(dai);
  185. pr_debug("%s : sport %d\n", __func__, dai->id);
  186. if (!dai->active)
  187. return 0;
  188. if (dai->capture_active)
  189. sport_rx_stop(sport);
  190. if (dai->playback_active)
  191. sport_tx_stop(sport);
  192. return 0;
  193. }
  194. static int bf5xx_ac97_resume(struct snd_soc_dai *dai)
  195. {
  196. int ret;
  197. struct sport_device *sport = snd_soc_dai_get_drvdata(dai);
  198. pr_debug("%s : sport %d\n", __func__, dai->id);
  199. if (!dai->active)
  200. return 0;
  201. #if defined(CONFIG_SND_BF5XX_MULTICHAN_SUPPORT)
  202. ret = sport_set_multichannel(sport, 16, 0x3FF, 0x3FF, 1);
  203. #else
  204. ret = sport_set_multichannel(sport, 16, 0x1F, 0x1F, 1);
  205. #endif
  206. if (ret) {
  207. pr_err("SPORT is busy!\n");
  208. return -EBUSY;
  209. }
  210. ret = sport_config_rx(sport, IRFS, 0xF, 0, (16*16-1));
  211. if (ret) {
  212. pr_err("SPORT is busy!\n");
  213. return -EBUSY;
  214. }
  215. ret = sport_config_tx(sport, ITFS, 0xF, 0, (16*16-1));
  216. if (ret) {
  217. pr_err("SPORT is busy!\n");
  218. return -EBUSY;
  219. }
  220. return 0;
  221. }
  222. #else
  223. #define bf5xx_ac97_suspend NULL
  224. #define bf5xx_ac97_resume NULL
  225. #endif
  226. static struct snd_soc_dai_driver bfin_ac97_dai = {
  227. .bus_control = true,
  228. .suspend = bf5xx_ac97_suspend,
  229. .resume = bf5xx_ac97_resume,
  230. .playback = {
  231. .stream_name = "AC97 Playback",
  232. .channels_min = 2,
  233. #if defined(CONFIG_SND_BF5XX_MULTICHAN_SUPPORT)
  234. .channels_max = 6,
  235. #else
  236. .channels_max = 2,
  237. #endif
  238. .rates = SNDRV_PCM_RATE_48000,
  239. .formats = SNDRV_PCM_FMTBIT_S16_LE, },
  240. .capture = {
  241. .stream_name = "AC97 Capture",
  242. .channels_min = 2,
  243. .channels_max = 2,
  244. .rates = SNDRV_PCM_RATE_48000,
  245. .formats = SNDRV_PCM_FMTBIT_S16_LE, },
  246. };
  247. static const struct snd_soc_component_driver bfin_ac97_component = {
  248. .name = "bfin-ac97",
  249. };
  250. static int asoc_bfin_ac97_probe(struct platform_device *pdev)
  251. {
  252. struct sport_device *sport_handle;
  253. int ret;
  254. #ifdef CONFIG_SND_BF5XX_HAVE_COLD_RESET
  255. /* Request PB3 as reset pin */
  256. ret = devm_gpio_request_one(&pdev->dev,
  257. CONFIG_SND_BF5XX_RESET_GPIO_NUM,
  258. GPIOF_OUT_INIT_HIGH, "SND_AD198x RESET");
  259. if (ret) {
  260. dev_err(&pdev->dev,
  261. "Failed to request GPIO_%d for reset: %d\n",
  262. CONFIG_SND_BF5XX_RESET_GPIO_NUM, ret);
  263. return ret;
  264. }
  265. #endif
  266. sport_handle = sport_init(pdev, 2, sizeof(struct ac97_frame),
  267. PAGE_SIZE);
  268. if (!sport_handle) {
  269. ret = -ENODEV;
  270. goto sport_err;
  271. }
  272. /*SPORT works in TDM mode to simulate AC97 transfers*/
  273. #if defined(CONFIG_SND_BF5XX_MULTICHAN_SUPPORT)
  274. ret = sport_set_multichannel(sport_handle, 16, 0x3FF, 0x3FF, 1);
  275. #else
  276. ret = sport_set_multichannel(sport_handle, 16, 0x1F, 0x1F, 1);
  277. #endif
  278. if (ret) {
  279. pr_err("SPORT is busy!\n");
  280. ret = -EBUSY;
  281. goto sport_config_err;
  282. }
  283. ret = sport_config_rx(sport_handle, IRFS, 0xF, 0, (16*16-1));
  284. if (ret) {
  285. pr_err("SPORT is busy!\n");
  286. ret = -EBUSY;
  287. goto sport_config_err;
  288. }
  289. ret = sport_config_tx(sport_handle, ITFS, 0xF, 0, (16*16-1));
  290. if (ret) {
  291. pr_err("SPORT is busy!\n");
  292. ret = -EBUSY;
  293. goto sport_config_err;
  294. }
  295. ret = snd_soc_set_ac97_ops(&bf5xx_ac97_ops);
  296. if (ret != 0) {
  297. dev_err(&pdev->dev, "Failed to set AC'97 ops: %d\n", ret);
  298. goto sport_config_err;
  299. }
  300. ret = snd_soc_register_component(&pdev->dev, &bfin_ac97_component,
  301. &bfin_ac97_dai, 1);
  302. if (ret) {
  303. pr_err("Failed to register DAI: %d\n", ret);
  304. goto sport_config_err;
  305. }
  306. ac97_sport_handle = sport_handle;
  307. return 0;
  308. sport_config_err:
  309. sport_done(sport_handle);
  310. sport_err:
  311. snd_soc_set_ac97_ops(NULL);
  312. return ret;
  313. }
  314. static int asoc_bfin_ac97_remove(struct platform_device *pdev)
  315. {
  316. struct sport_device *sport_handle = platform_get_drvdata(pdev);
  317. snd_soc_unregister_component(&pdev->dev);
  318. sport_done(sport_handle);
  319. snd_soc_set_ac97_ops(NULL);
  320. return 0;
  321. }
  322. static struct platform_driver asoc_bfin_ac97_driver = {
  323. .driver = {
  324. .name = "bfin-ac97",
  325. },
  326. .probe = asoc_bfin_ac97_probe,
  327. .remove = asoc_bfin_ac97_remove,
  328. };
  329. module_platform_driver(asoc_bfin_ac97_driver);
  330. MODULE_AUTHOR("Roy Huang");
  331. MODULE_DESCRIPTION("AC97 driver for ADI Blackfin");
  332. MODULE_LICENSE("GPL");