ac97.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /* sound/soc/samsung/ac97.c
  2. *
  3. * ALSA SoC Audio Layer - S3C AC97 Controller driver
  4. * Evolved from s3c2443-ac97.c
  5. *
  6. * Copyright (c) 2010 Samsung Electronics Co. Ltd
  7. * Author: Jaswinder Singh <jassisinghbrar@gmail.com>
  8. * Credits: Graeme Gregory, Sean Choi
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/io.h>
  15. #include <linux/delay.h>
  16. #include <linux/clk.h>
  17. #include <linux/module.h>
  18. #include <sound/soc.h>
  19. #include <mach/dma.h>
  20. #include <plat/regs-ac97.h>
  21. #include <plat/audio.h>
  22. #include "dma.h"
  23. #define AC_CMD_ADDR(x) (x << 16)
  24. #define AC_CMD_DATA(x) (x & 0xffff)
  25. #define S3C_AC97_DAI_PCM 0
  26. #define S3C_AC97_DAI_MIC 1
  27. struct s3c_ac97_info {
  28. struct clk *ac97_clk;
  29. void __iomem *regs;
  30. struct mutex lock;
  31. struct completion done;
  32. };
  33. static struct s3c_ac97_info s3c_ac97;
  34. static struct s3c2410_dma_client s3c_dma_client_out = {
  35. .name = "AC97 PCMOut"
  36. };
  37. static struct s3c2410_dma_client s3c_dma_client_in = {
  38. .name = "AC97 PCMIn"
  39. };
  40. static struct s3c2410_dma_client s3c_dma_client_micin = {
  41. .name = "AC97 MicIn"
  42. };
  43. static struct s3c_dma_params s3c_ac97_pcm_out = {
  44. .client = &s3c_dma_client_out,
  45. .dma_size = 4,
  46. };
  47. static struct s3c_dma_params s3c_ac97_pcm_in = {
  48. .client = &s3c_dma_client_in,
  49. .dma_size = 4,
  50. };
  51. static struct s3c_dma_params s3c_ac97_mic_in = {
  52. .client = &s3c_dma_client_micin,
  53. .dma_size = 4,
  54. };
  55. static void s3c_ac97_activate(struct snd_ac97 *ac97)
  56. {
  57. u32 ac_glbctrl, stat;
  58. stat = readl(s3c_ac97.regs + S3C_AC97_GLBSTAT) & 0x7;
  59. if (stat == S3C_AC97_GLBSTAT_MAINSTATE_ACTIVE)
  60. return; /* Return if already active */
  61. INIT_COMPLETION(s3c_ac97.done);
  62. ac_glbctrl = readl(s3c_ac97.regs + S3C_AC97_GLBCTRL);
  63. ac_glbctrl = S3C_AC97_GLBCTRL_ACLINKON;
  64. writel(ac_glbctrl, s3c_ac97.regs + S3C_AC97_GLBCTRL);
  65. msleep(1);
  66. ac_glbctrl |= S3C_AC97_GLBCTRL_TRANSFERDATAENABLE;
  67. writel(ac_glbctrl, s3c_ac97.regs + S3C_AC97_GLBCTRL);
  68. msleep(1);
  69. ac_glbctrl = readl(s3c_ac97.regs + S3C_AC97_GLBCTRL);
  70. ac_glbctrl |= S3C_AC97_GLBCTRL_CODECREADYIE;
  71. writel(ac_glbctrl, s3c_ac97.regs + S3C_AC97_GLBCTRL);
  72. if (!wait_for_completion_timeout(&s3c_ac97.done, HZ))
  73. pr_err("AC97: Unable to activate!");
  74. }
  75. static unsigned short s3c_ac97_read(struct snd_ac97 *ac97,
  76. unsigned short reg)
  77. {
  78. u32 ac_glbctrl, ac_codec_cmd;
  79. u32 stat, addr, data;
  80. mutex_lock(&s3c_ac97.lock);
  81. s3c_ac97_activate(ac97);
  82. INIT_COMPLETION(s3c_ac97.done);
  83. ac_codec_cmd = readl(s3c_ac97.regs + S3C_AC97_CODEC_CMD);
  84. ac_codec_cmd = S3C_AC97_CODEC_CMD_READ | AC_CMD_ADDR(reg);
  85. writel(ac_codec_cmd, s3c_ac97.regs + S3C_AC97_CODEC_CMD);
  86. udelay(50);
  87. ac_glbctrl = readl(s3c_ac97.regs + S3C_AC97_GLBCTRL);
  88. ac_glbctrl |= S3C_AC97_GLBCTRL_CODECREADYIE;
  89. writel(ac_glbctrl, s3c_ac97.regs + S3C_AC97_GLBCTRL);
  90. if (!wait_for_completion_timeout(&s3c_ac97.done, HZ))
  91. pr_err("AC97: Unable to read!");
  92. stat = readl(s3c_ac97.regs + S3C_AC97_STAT);
  93. addr = (stat >> 16) & 0x7f;
  94. data = (stat & 0xffff);
  95. if (addr != reg)
  96. pr_err("ac97: req addr = %02x, rep addr = %02x\n",
  97. reg, addr);
  98. mutex_unlock(&s3c_ac97.lock);
  99. return (unsigned short)data;
  100. }
  101. static void s3c_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
  102. unsigned short val)
  103. {
  104. u32 ac_glbctrl, ac_codec_cmd;
  105. mutex_lock(&s3c_ac97.lock);
  106. s3c_ac97_activate(ac97);
  107. INIT_COMPLETION(s3c_ac97.done);
  108. ac_codec_cmd = readl(s3c_ac97.regs + S3C_AC97_CODEC_CMD);
  109. ac_codec_cmd = AC_CMD_ADDR(reg) | AC_CMD_DATA(val);
  110. writel(ac_codec_cmd, s3c_ac97.regs + S3C_AC97_CODEC_CMD);
  111. udelay(50);
  112. ac_glbctrl = readl(s3c_ac97.regs + S3C_AC97_GLBCTRL);
  113. ac_glbctrl |= S3C_AC97_GLBCTRL_CODECREADYIE;
  114. writel(ac_glbctrl, s3c_ac97.regs + S3C_AC97_GLBCTRL);
  115. if (!wait_for_completion_timeout(&s3c_ac97.done, HZ))
  116. pr_err("AC97: Unable to write!");
  117. ac_codec_cmd = readl(s3c_ac97.regs + S3C_AC97_CODEC_CMD);
  118. ac_codec_cmd |= S3C_AC97_CODEC_CMD_READ;
  119. writel(ac_codec_cmd, s3c_ac97.regs + S3C_AC97_CODEC_CMD);
  120. mutex_unlock(&s3c_ac97.lock);
  121. }
  122. static void s3c_ac97_cold_reset(struct snd_ac97 *ac97)
  123. {
  124. pr_debug("AC97: Cold reset\n");
  125. writel(S3C_AC97_GLBCTRL_COLDRESET,
  126. s3c_ac97.regs + S3C_AC97_GLBCTRL);
  127. msleep(1);
  128. writel(0, s3c_ac97.regs + S3C_AC97_GLBCTRL);
  129. msleep(1);
  130. }
  131. static void s3c_ac97_warm_reset(struct snd_ac97 *ac97)
  132. {
  133. u32 stat;
  134. stat = readl(s3c_ac97.regs + S3C_AC97_GLBSTAT) & 0x7;
  135. if (stat == S3C_AC97_GLBSTAT_MAINSTATE_ACTIVE)
  136. return; /* Return if already active */
  137. pr_debug("AC97: Warm reset\n");
  138. writel(S3C_AC97_GLBCTRL_WARMRESET, s3c_ac97.regs + S3C_AC97_GLBCTRL);
  139. msleep(1);
  140. writel(0, s3c_ac97.regs + S3C_AC97_GLBCTRL);
  141. msleep(1);
  142. s3c_ac97_activate(ac97);
  143. }
  144. static irqreturn_t s3c_ac97_irq(int irq, void *dev_id)
  145. {
  146. u32 ac_glbctrl, ac_glbstat;
  147. ac_glbstat = readl(s3c_ac97.regs + S3C_AC97_GLBSTAT);
  148. if (ac_glbstat & S3C_AC97_GLBSTAT_CODECREADY) {
  149. ac_glbctrl = readl(s3c_ac97.regs + S3C_AC97_GLBCTRL);
  150. ac_glbctrl &= ~S3C_AC97_GLBCTRL_CODECREADYIE;
  151. writel(ac_glbctrl, s3c_ac97.regs + S3C_AC97_GLBCTRL);
  152. complete(&s3c_ac97.done);
  153. }
  154. ac_glbctrl = readl(s3c_ac97.regs + S3C_AC97_GLBCTRL);
  155. ac_glbctrl |= (1<<30); /* Clear interrupt */
  156. writel(ac_glbctrl, s3c_ac97.regs + S3C_AC97_GLBCTRL);
  157. return IRQ_HANDLED;
  158. }
  159. struct snd_ac97_bus_ops soc_ac97_ops = {
  160. .read = s3c_ac97_read,
  161. .write = s3c_ac97_write,
  162. .warm_reset = s3c_ac97_warm_reset,
  163. .reset = s3c_ac97_cold_reset,
  164. };
  165. EXPORT_SYMBOL_GPL(soc_ac97_ops);
  166. static int s3c_ac97_hw_params(struct snd_pcm_substream *substream,
  167. struct snd_pcm_hw_params *params,
  168. struct snd_soc_dai *dai)
  169. {
  170. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  171. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  172. struct s3c_dma_params *dma_data;
  173. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  174. dma_data = &s3c_ac97_pcm_out;
  175. else
  176. dma_data = &s3c_ac97_pcm_in;
  177. snd_soc_dai_set_dma_data(cpu_dai, substream, dma_data);
  178. return 0;
  179. }
  180. static int s3c_ac97_trigger(struct snd_pcm_substream *substream, int cmd,
  181. struct snd_soc_dai *dai)
  182. {
  183. u32 ac_glbctrl;
  184. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  185. struct s3c_dma_params *dma_data =
  186. snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  187. ac_glbctrl = readl(s3c_ac97.regs + S3C_AC97_GLBCTRL);
  188. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  189. ac_glbctrl &= ~S3C_AC97_GLBCTRL_PCMINTM_MASK;
  190. else
  191. ac_glbctrl &= ~S3C_AC97_GLBCTRL_PCMOUTTM_MASK;
  192. switch (cmd) {
  193. case SNDRV_PCM_TRIGGER_START:
  194. case SNDRV_PCM_TRIGGER_RESUME:
  195. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  196. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  197. ac_glbctrl |= S3C_AC97_GLBCTRL_PCMINTM_DMA;
  198. else
  199. ac_glbctrl |= S3C_AC97_GLBCTRL_PCMOUTTM_DMA;
  200. break;
  201. case SNDRV_PCM_TRIGGER_STOP:
  202. case SNDRV_PCM_TRIGGER_SUSPEND:
  203. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  204. break;
  205. }
  206. writel(ac_glbctrl, s3c_ac97.regs + S3C_AC97_GLBCTRL);
  207. if (!dma_data->ops)
  208. dma_data->ops = samsung_dma_get_ops();
  209. dma_data->ops->started(dma_data->channel);
  210. return 0;
  211. }
  212. static int s3c_ac97_hw_mic_params(struct snd_pcm_substream *substream,
  213. struct snd_pcm_hw_params *params,
  214. struct snd_soc_dai *dai)
  215. {
  216. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  217. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  218. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  219. return -ENODEV;
  220. else
  221. snd_soc_dai_set_dma_data(cpu_dai, substream, &s3c_ac97_mic_in);
  222. return 0;
  223. }
  224. static int s3c_ac97_mic_trigger(struct snd_pcm_substream *substream,
  225. int cmd, struct snd_soc_dai *dai)
  226. {
  227. u32 ac_glbctrl;
  228. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  229. struct s3c_dma_params *dma_data =
  230. snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  231. ac_glbctrl = readl(s3c_ac97.regs + S3C_AC97_GLBCTRL);
  232. ac_glbctrl &= ~S3C_AC97_GLBCTRL_MICINTM_MASK;
  233. switch (cmd) {
  234. case SNDRV_PCM_TRIGGER_START:
  235. case SNDRV_PCM_TRIGGER_RESUME:
  236. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  237. ac_glbctrl |= S3C_AC97_GLBCTRL_MICINTM_DMA;
  238. break;
  239. case SNDRV_PCM_TRIGGER_STOP:
  240. case SNDRV_PCM_TRIGGER_SUSPEND:
  241. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  242. break;
  243. }
  244. writel(ac_glbctrl, s3c_ac97.regs + S3C_AC97_GLBCTRL);
  245. if (!dma_data->ops)
  246. dma_data->ops = samsung_dma_get_ops();
  247. dma_data->ops->started(dma_data->channel);
  248. return 0;
  249. }
  250. static const struct snd_soc_dai_ops s3c_ac97_dai_ops = {
  251. .hw_params = s3c_ac97_hw_params,
  252. .trigger = s3c_ac97_trigger,
  253. };
  254. static const struct snd_soc_dai_ops s3c_ac97_mic_dai_ops = {
  255. .hw_params = s3c_ac97_hw_mic_params,
  256. .trigger = s3c_ac97_mic_trigger,
  257. };
  258. static struct snd_soc_dai_driver s3c_ac97_dai[] = {
  259. [S3C_AC97_DAI_PCM] = {
  260. .name = "samsung-ac97",
  261. .ac97_control = 1,
  262. .playback = {
  263. .stream_name = "AC97 Playback",
  264. .channels_min = 2,
  265. .channels_max = 2,
  266. .rates = SNDRV_PCM_RATE_8000_48000,
  267. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  268. .capture = {
  269. .stream_name = "AC97 Capture",
  270. .channels_min = 2,
  271. .channels_max = 2,
  272. .rates = SNDRV_PCM_RATE_8000_48000,
  273. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  274. .ops = &s3c_ac97_dai_ops,
  275. },
  276. [S3C_AC97_DAI_MIC] = {
  277. .name = "samsung-ac97-mic",
  278. .ac97_control = 1,
  279. .capture = {
  280. .stream_name = "AC97 Mic Capture",
  281. .channels_min = 1,
  282. .channels_max = 1,
  283. .rates = SNDRV_PCM_RATE_8000_48000,
  284. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  285. .ops = &s3c_ac97_mic_dai_ops,
  286. },
  287. };
  288. static __devinit int s3c_ac97_probe(struct platform_device *pdev)
  289. {
  290. struct resource *mem_res, *dmatx_res, *dmarx_res, *dmamic_res, *irq_res;
  291. struct s3c_audio_pdata *ac97_pdata;
  292. int ret;
  293. ac97_pdata = pdev->dev.platform_data;
  294. if (!ac97_pdata || !ac97_pdata->cfg_gpio) {
  295. dev_err(&pdev->dev, "cfg_gpio callback not provided!\n");
  296. return -EINVAL;
  297. }
  298. /* Check for availability of necessary resource */
  299. dmatx_res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  300. if (!dmatx_res) {
  301. dev_err(&pdev->dev, "Unable to get AC97-TX dma resource\n");
  302. return -ENXIO;
  303. }
  304. dmarx_res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  305. if (!dmarx_res) {
  306. dev_err(&pdev->dev, "Unable to get AC97-RX dma resource\n");
  307. return -ENXIO;
  308. }
  309. dmamic_res = platform_get_resource(pdev, IORESOURCE_DMA, 2);
  310. if (!dmamic_res) {
  311. dev_err(&pdev->dev, "Unable to get AC97-MIC dma resource\n");
  312. return -ENXIO;
  313. }
  314. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  315. if (!mem_res) {
  316. dev_err(&pdev->dev, "Unable to get register resource\n");
  317. return -ENXIO;
  318. }
  319. irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  320. if (!irq_res) {
  321. dev_err(&pdev->dev, "AC97 IRQ not provided!\n");
  322. return -ENXIO;
  323. }
  324. if (!request_mem_region(mem_res->start,
  325. resource_size(mem_res), "ac97")) {
  326. dev_err(&pdev->dev, "Unable to request register region\n");
  327. return -EBUSY;
  328. }
  329. s3c_ac97_pcm_out.channel = dmatx_res->start;
  330. s3c_ac97_pcm_out.dma_addr = mem_res->start + S3C_AC97_PCM_DATA;
  331. s3c_ac97_pcm_in.channel = dmarx_res->start;
  332. s3c_ac97_pcm_in.dma_addr = mem_res->start + S3C_AC97_PCM_DATA;
  333. s3c_ac97_mic_in.channel = dmamic_res->start;
  334. s3c_ac97_mic_in.dma_addr = mem_res->start + S3C_AC97_MIC_DATA;
  335. init_completion(&s3c_ac97.done);
  336. mutex_init(&s3c_ac97.lock);
  337. s3c_ac97.regs = ioremap(mem_res->start, resource_size(mem_res));
  338. if (s3c_ac97.regs == NULL) {
  339. dev_err(&pdev->dev, "Unable to ioremap register region\n");
  340. ret = -ENXIO;
  341. goto err1;
  342. }
  343. s3c_ac97.ac97_clk = clk_get(&pdev->dev, "ac97");
  344. if (IS_ERR(s3c_ac97.ac97_clk)) {
  345. dev_err(&pdev->dev, "ac97 failed to get ac97_clock\n");
  346. ret = -ENODEV;
  347. goto err2;
  348. }
  349. clk_enable(s3c_ac97.ac97_clk);
  350. if (ac97_pdata->cfg_gpio(pdev)) {
  351. dev_err(&pdev->dev, "Unable to configure gpio\n");
  352. ret = -EINVAL;
  353. goto err3;
  354. }
  355. ret = request_irq(irq_res->start, s3c_ac97_irq,
  356. 0, "AC97", NULL);
  357. if (ret < 0) {
  358. dev_err(&pdev->dev, "ac97: interrupt request failed.\n");
  359. goto err4;
  360. }
  361. ret = snd_soc_register_dais(&pdev->dev, s3c_ac97_dai,
  362. ARRAY_SIZE(s3c_ac97_dai));
  363. if (ret)
  364. goto err5;
  365. return 0;
  366. err5:
  367. free_irq(irq_res->start, NULL);
  368. err4:
  369. err3:
  370. clk_disable(s3c_ac97.ac97_clk);
  371. clk_put(s3c_ac97.ac97_clk);
  372. err2:
  373. iounmap(s3c_ac97.regs);
  374. err1:
  375. release_mem_region(mem_res->start, resource_size(mem_res));
  376. return ret;
  377. }
  378. static __devexit int s3c_ac97_remove(struct platform_device *pdev)
  379. {
  380. struct resource *mem_res, *irq_res;
  381. snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(s3c_ac97_dai));
  382. irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  383. if (irq_res)
  384. free_irq(irq_res->start, NULL);
  385. clk_disable(s3c_ac97.ac97_clk);
  386. clk_put(s3c_ac97.ac97_clk);
  387. iounmap(s3c_ac97.regs);
  388. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  389. if (mem_res)
  390. release_mem_region(mem_res->start, resource_size(mem_res));
  391. return 0;
  392. }
  393. static struct platform_driver s3c_ac97_driver = {
  394. .probe = s3c_ac97_probe,
  395. .remove = __devexit_p(s3c_ac97_remove),
  396. .driver = {
  397. .name = "samsung-ac97",
  398. .owner = THIS_MODULE,
  399. },
  400. };
  401. module_platform_driver(s3c_ac97_driver);
  402. MODULE_AUTHOR("Jaswinder Singh, <jassisinghbrar@gmail.com>");
  403. MODULE_DESCRIPTION("AC97 driver for the Samsung SoC");
  404. MODULE_LICENSE("GPL");
  405. MODULE_ALIAS("platform:samsung-ac97");