s3c2443-ac97.c 10 KB

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