s3c2443-ac97.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * s3c2443-ac97.c -- ALSA Soc Audio Layer
  3. *
  4. * (c) 2007 Wolfson Microelectronics PLC.
  5. * Graeme Gregory graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
  6. *
  7. * Copyright (C) 2005, Sean Choi <sh428.choi@samsung.com>
  8. * All rights reserved.
  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/init.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/wait.h>
  20. #include <linux/delay.h>
  21. #include <linux/gpio.h>
  22. #include <linux/clk.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include <sound/ac97_codec.h>
  26. #include <sound/initval.h>
  27. #include <sound/soc.h>
  28. #include <mach/hardware.h>
  29. #include <plat/regs-ac97.h>
  30. #include <mach/regs-gpio.h>
  31. #include <mach/regs-clock.h>
  32. #include <plat/audio.h>
  33. #include <asm/dma.h>
  34. #include <mach/dma.h>
  35. #include "s3c24xx-pcm.h"
  36. #include "s3c24xx-ac97.h"
  37. struct s3c24xx_ac97_info {
  38. void __iomem *regs;
  39. struct clk *ac97_clk;
  40. };
  41. static struct s3c24xx_ac97_info s3c24xx_ac97;
  42. static DECLARE_COMPLETION(ac97_completion);
  43. static u32 codec_ready;
  44. static DECLARE_MUTEX(ac97_mutex);
  45. static unsigned short s3c2443_ac97_read(struct snd_ac97 *ac97,
  46. unsigned short reg)
  47. {
  48. u32 ac_glbctrl;
  49. u32 ac_codec_cmd;
  50. u32 stat, addr, data;
  51. down(&ac97_mutex);
  52. codec_ready = S3C_AC97_GLBSTAT_CODECREADY;
  53. ac_codec_cmd = readl(s3c24xx_ac97.regs + S3C_AC97_CODEC_CMD);
  54. ac_codec_cmd = S3C_AC97_CODEC_CMD_READ | AC_CMD_ADDR(reg);
  55. writel(ac_codec_cmd, s3c24xx_ac97.regs + S3C_AC97_CODEC_CMD);
  56. udelay(50);
  57. ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  58. ac_glbctrl |= S3C_AC97_GLBCTRL_CODECREADYIE;
  59. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  60. wait_for_completion(&ac97_completion);
  61. stat = readl(s3c24xx_ac97.regs + S3C_AC97_STAT);
  62. addr = (stat >> 16) & 0x7f;
  63. data = (stat & 0xffff);
  64. if (addr != reg)
  65. printk(KERN_ERR "s3c24xx-ac97: req addr = %02x,"
  66. " rep addr = %02x\n", reg, addr);
  67. up(&ac97_mutex);
  68. return (unsigned short)data;
  69. }
  70. static void s3c2443_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
  71. unsigned short val)
  72. {
  73. u32 ac_glbctrl;
  74. u32 ac_codec_cmd;
  75. down(&ac97_mutex);
  76. codec_ready = S3C_AC97_GLBSTAT_CODECREADY;
  77. ac_codec_cmd = readl(s3c24xx_ac97.regs + S3C_AC97_CODEC_CMD);
  78. ac_codec_cmd = AC_CMD_ADDR(reg) | AC_CMD_DATA(val);
  79. writel(ac_codec_cmd, s3c24xx_ac97.regs + S3C_AC97_CODEC_CMD);
  80. udelay(50);
  81. ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  82. ac_glbctrl |= S3C_AC97_GLBCTRL_CODECREADYIE;
  83. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  84. wait_for_completion(&ac97_completion);
  85. ac_codec_cmd = readl(s3c24xx_ac97.regs + S3C_AC97_CODEC_CMD);
  86. ac_codec_cmd |= S3C_AC97_CODEC_CMD_READ;
  87. writel(ac_codec_cmd, s3c24xx_ac97.regs + S3C_AC97_CODEC_CMD);
  88. up(&ac97_mutex);
  89. }
  90. static void s3c2443_ac97_warm_reset(struct snd_ac97 *ac97)
  91. {
  92. u32 ac_glbctrl;
  93. ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  94. ac_glbctrl = S3C_AC97_GLBCTRL_WARMRESET;
  95. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  96. msleep(1);
  97. ac_glbctrl = 0;
  98. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  99. msleep(1);
  100. }
  101. static void s3c2443_ac97_cold_reset(struct snd_ac97 *ac97)
  102. {
  103. u32 ac_glbctrl;
  104. ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  105. ac_glbctrl = S3C_AC97_GLBCTRL_COLDRESET;
  106. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  107. msleep(1);
  108. ac_glbctrl = 0;
  109. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  110. msleep(1);
  111. ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  112. ac_glbctrl = S3C_AC97_GLBCTRL_ACLINKON;
  113. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  114. msleep(1);
  115. ac_glbctrl |= S3C_AC97_GLBCTRL_TRANSFERDATAENABLE;
  116. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  117. msleep(1);
  118. ac_glbctrl |= S3C_AC97_GLBCTRL_PCMOUTTM_DMA |
  119. S3C_AC97_GLBCTRL_PCMINTM_DMA | S3C_AC97_GLBCTRL_MICINTM_DMA;
  120. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  121. }
  122. static irqreturn_t s3c2443_ac97_irq(int irq, void *dev_id)
  123. {
  124. int status;
  125. u32 ac_glbctrl;
  126. status = readl(s3c24xx_ac97.regs + S3C_AC97_GLBSTAT) & codec_ready;
  127. if (status) {
  128. ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  129. ac_glbctrl &= ~S3C_AC97_GLBCTRL_CODECREADYIE;
  130. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  131. complete(&ac97_completion);
  132. }
  133. return IRQ_HANDLED;
  134. }
  135. struct snd_ac97_bus_ops soc_ac97_ops = {
  136. .read = s3c2443_ac97_read,
  137. .write = s3c2443_ac97_write,
  138. .warm_reset = s3c2443_ac97_warm_reset,
  139. .reset = s3c2443_ac97_cold_reset,
  140. };
  141. static struct s3c2410_dma_client s3c2443_dma_client_out = {
  142. .name = "AC97 PCM Stereo out"
  143. };
  144. static struct s3c2410_dma_client s3c2443_dma_client_in = {
  145. .name = "AC97 PCM Stereo in"
  146. };
  147. static struct s3c2410_dma_client s3c2443_dma_client_micin = {
  148. .name = "AC97 Mic Mono in"
  149. };
  150. static struct s3c24xx_pcm_dma_params s3c2443_ac97_pcm_stereo_out = {
  151. .client = &s3c2443_dma_client_out,
  152. .channel = DMACH_PCM_OUT,
  153. .dma_addr = S3C2440_PA_AC97 + S3C_AC97_PCM_DATA,
  154. .dma_size = 4,
  155. };
  156. static struct s3c24xx_pcm_dma_params s3c2443_ac97_pcm_stereo_in = {
  157. .client = &s3c2443_dma_client_in,
  158. .channel = DMACH_PCM_IN,
  159. .dma_addr = S3C2440_PA_AC97 + S3C_AC97_PCM_DATA,
  160. .dma_size = 4,
  161. };
  162. static struct s3c24xx_pcm_dma_params s3c2443_ac97_mic_mono_in = {
  163. .client = &s3c2443_dma_client_micin,
  164. .channel = DMACH_MIC_IN,
  165. .dma_addr = S3C2440_PA_AC97 + S3C_AC97_MIC_DATA,
  166. .dma_size = 4,
  167. };
  168. static int s3c2443_ac97_probe(struct platform_device *pdev,
  169. struct snd_soc_dai *dai)
  170. {
  171. int ret;
  172. u32 ac_glbctrl;
  173. s3c24xx_ac97.regs = ioremap(S3C2440_PA_AC97, 0x100);
  174. if (s3c24xx_ac97.regs == NULL)
  175. return -ENXIO;
  176. s3c24xx_ac97.ac97_clk = clk_get(&pdev->dev, "ac97");
  177. if (s3c24xx_ac97.ac97_clk == NULL) {
  178. printk(KERN_ERR "s3c2443-ac97 failed to get ac97_clock\n");
  179. iounmap(s3c24xx_ac97.regs);
  180. return -ENODEV;
  181. }
  182. clk_enable(s3c24xx_ac97.ac97_clk);
  183. s3c2410_gpio_cfgpin(S3C2410_GPE0, S3C2443_GPE0_AC_nRESET);
  184. s3c2410_gpio_cfgpin(S3C2410_GPE1, S3C2443_GPE1_AC_SYNC);
  185. s3c2410_gpio_cfgpin(S3C2410_GPE2, S3C2443_GPE2_AC_BITCLK);
  186. s3c2410_gpio_cfgpin(S3C2410_GPE3, S3C2443_GPE3_AC_SDI);
  187. s3c2410_gpio_cfgpin(S3C2410_GPE4, S3C2443_GPE4_AC_SDO);
  188. ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  189. ac_glbctrl = S3C_AC97_GLBCTRL_COLDRESET;
  190. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  191. msleep(1);
  192. ac_glbctrl = 0;
  193. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  194. msleep(1);
  195. ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  196. ac_glbctrl = S3C_AC97_GLBCTRL_ACLINKON;
  197. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  198. msleep(1);
  199. ac_glbctrl |= S3C_AC97_GLBCTRL_TRANSFERDATAENABLE;
  200. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  201. ret = request_irq(IRQ_S3C244x_AC97, s3c2443_ac97_irq,
  202. IRQF_DISABLED, "AC97", NULL);
  203. if (ret < 0) {
  204. printk(KERN_ERR "s3c24xx-ac97: interrupt request failed.\n");
  205. clk_disable(s3c24xx_ac97.ac97_clk);
  206. clk_put(s3c24xx_ac97.ac97_clk);
  207. iounmap(s3c24xx_ac97.regs);
  208. }
  209. return ret;
  210. }
  211. static void s3c2443_ac97_remove(struct platform_device *pdev,
  212. struct snd_soc_dai *dai)
  213. {
  214. free_irq(IRQ_S3C244x_AC97, NULL);
  215. clk_disable(s3c24xx_ac97.ac97_clk);
  216. clk_put(s3c24xx_ac97.ac97_clk);
  217. iounmap(s3c24xx_ac97.regs);
  218. }
  219. static int s3c2443_ac97_hw_params(struct snd_pcm_substream *substream,
  220. struct snd_pcm_hw_params *params,
  221. struct snd_soc_dai *dai)
  222. {
  223. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  224. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  225. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  226. cpu_dai->dma_data = &s3c2443_ac97_pcm_stereo_out;
  227. else
  228. cpu_dai->dma_data = &s3c2443_ac97_pcm_stereo_in;
  229. return 0;
  230. }
  231. static int s3c2443_ac97_trigger(struct snd_pcm_substream *substream, int cmd,
  232. struct snd_soc_dai *dai)
  233. {
  234. u32 ac_glbctrl;
  235. ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  236. switch (cmd) {
  237. case SNDRV_PCM_TRIGGER_START:
  238. case SNDRV_PCM_TRIGGER_RESUME:
  239. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  240. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  241. ac_glbctrl |= S3C_AC97_GLBCTRL_PCMINTM_DMA;
  242. else
  243. ac_glbctrl |= S3C_AC97_GLBCTRL_PCMOUTTM_DMA;
  244. break;
  245. case SNDRV_PCM_TRIGGER_STOP:
  246. case SNDRV_PCM_TRIGGER_SUSPEND:
  247. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  248. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  249. ac_glbctrl &= ~S3C_AC97_GLBCTRL_PCMINTM_MASK;
  250. else
  251. ac_glbctrl &= ~S3C_AC97_GLBCTRL_PCMOUTTM_MASK;
  252. break;
  253. }
  254. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  255. return 0;
  256. }
  257. static int s3c2443_ac97_hw_mic_params(struct snd_pcm_substream *substream,
  258. struct snd_pcm_hw_params *params,
  259. struct snd_soc_dai *dai)
  260. {
  261. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  262. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  263. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  264. return -ENODEV;
  265. else
  266. cpu_dai->dma_data = &s3c2443_ac97_mic_mono_in;
  267. return 0;
  268. }
  269. static int s3c2443_ac97_mic_trigger(struct snd_pcm_substream *substream,
  270. int cmd, struct snd_soc_dai *dai)
  271. {
  272. u32 ac_glbctrl;
  273. ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  274. switch (cmd) {
  275. case SNDRV_PCM_TRIGGER_START:
  276. case SNDRV_PCM_TRIGGER_RESUME:
  277. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  278. ac_glbctrl |= S3C_AC97_GLBCTRL_PCMINTM_DMA;
  279. break;
  280. case SNDRV_PCM_TRIGGER_STOP:
  281. case SNDRV_PCM_TRIGGER_SUSPEND:
  282. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  283. ac_glbctrl &= ~S3C_AC97_GLBCTRL_PCMINTM_MASK;
  284. }
  285. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  286. return 0;
  287. }
  288. #define s3c2443_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  289. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
  290. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
  291. static struct snd_soc_dai_ops s3c2443_ac97_dai_ops = {
  292. .hw_params = s3c2443_ac97_hw_params,
  293. .trigger = s3c2443_ac97_trigger,
  294. };
  295. static struct snd_soc_dai_ops s3c2443_ac97_mic_dai_ops = {
  296. .hw_params = s3c2443_ac97_hw_mic_params,
  297. .trigger = s3c2443_ac97_mic_trigger,
  298. };
  299. struct snd_soc_dai s3c2443_ac97_dai[] = {
  300. {
  301. .name = "s3c2443-ac97",
  302. .id = 0,
  303. .ac97_control = 1,
  304. .probe = s3c2443_ac97_probe,
  305. .remove = s3c2443_ac97_remove,
  306. .playback = {
  307. .stream_name = "AC97 Playback",
  308. .channels_min = 2,
  309. .channels_max = 2,
  310. .rates = s3c2443_AC97_RATES,
  311. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  312. .capture = {
  313. .stream_name = "AC97 Capture",
  314. .channels_min = 2,
  315. .channels_max = 2,
  316. .rates = s3c2443_AC97_RATES,
  317. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  318. .ops = &s3c2443_ac97_dai_ops,
  319. },
  320. {
  321. .name = "pxa2xx-ac97-mic",
  322. .id = 1,
  323. .ac97_control = 1,
  324. .capture = {
  325. .stream_name = "AC97 Mic Capture",
  326. .channels_min = 1,
  327. .channels_max = 1,
  328. .rates = s3c2443_AC97_RATES,
  329. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  330. .ops = &s3c2443_ac97_mic_dai_ops,
  331. },
  332. };
  333. EXPORT_SYMBOL_GPL(s3c2443_ac97_dai);
  334. EXPORT_SYMBOL_GPL(soc_ac97_ops);
  335. static int __init s3c2443_ac97_init(void)
  336. {
  337. return snd_soc_register_dais(s3c2443_ac97_dai,
  338. ARRAY_SIZE(s3c2443_ac97_dai));
  339. }
  340. module_init(s3c2443_ac97_init);
  341. static void __exit s3c2443_ac97_exit(void)
  342. {
  343. snd_soc_unregister_dais(s3c2443_ac97_dai,
  344. ARRAY_SIZE(s3c2443_ac97_dai));
  345. }
  346. module_exit(s3c2443_ac97_exit);
  347. MODULE_AUTHOR("Graeme Gregory");
  348. MODULE_DESCRIPTION("AC97 driver for the Samsung s3c2443 chip");
  349. MODULE_LICENSE("GPL");