s3c2443-ac97.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 <mach/hardware.h>
  28. #include <asm/plat-s3c/regs-ac97.h>
  29. #include <mach/regs-gpio.h>
  30. #include <mach/regs-clock.h>
  31. #include <mach/audio.h>
  32. #include <asm/dma.h>
  33. #include <mach/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. struct snd_soc_dai *dai)
  169. {
  170. int ret;
  171. u32 ac_glbctrl;
  172. s3c24xx_ac97.regs = ioremap(S3C2440_PA_AC97, 0x100);
  173. if (s3c24xx_ac97.regs == NULL)
  174. return -ENXIO;
  175. s3c24xx_ac97.ac97_clk = clk_get(&pdev->dev, "ac97");
  176. if (s3c24xx_ac97.ac97_clk == NULL) {
  177. printk(KERN_ERR "s3c2443-ac97 failed to get ac97_clock\n");
  178. iounmap(s3c24xx_ac97.regs);
  179. return -ENODEV;
  180. }
  181. clk_enable(s3c24xx_ac97.ac97_clk);
  182. s3c2410_gpio_cfgpin(S3C2410_GPE0, S3C2443_GPE0_AC_nRESET);
  183. s3c2410_gpio_cfgpin(S3C2410_GPE1, S3C2443_GPE1_AC_SYNC);
  184. s3c2410_gpio_cfgpin(S3C2410_GPE2, S3C2443_GPE2_AC_BITCLK);
  185. s3c2410_gpio_cfgpin(S3C2410_GPE3, S3C2443_GPE3_AC_SDI);
  186. s3c2410_gpio_cfgpin(S3C2410_GPE4, S3C2443_GPE4_AC_SDO);
  187. ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  188. ac_glbctrl = S3C_AC97_GLBCTRL_COLDRESET;
  189. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  190. msleep(1);
  191. ac_glbctrl = 0;
  192. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  193. msleep(1);
  194. ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  195. ac_glbctrl = S3C_AC97_GLBCTRL_ACLINKON;
  196. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  197. msleep(1);
  198. ac_glbctrl |= S3C_AC97_GLBCTRL_TRANSFERDATAENABLE;
  199. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  200. ret = request_irq(IRQ_S3C244x_AC97, s3c2443_ac97_irq,
  201. IRQF_DISABLED, "AC97", NULL);
  202. if (ret < 0) {
  203. printk(KERN_ERR "s3c24xx-ac97: interrupt request failed.\n");
  204. clk_disable(s3c24xx_ac97.ac97_clk);
  205. clk_put(s3c24xx_ac97.ac97_clk);
  206. iounmap(s3c24xx_ac97.regs);
  207. }
  208. return ret;
  209. }
  210. static void s3c2443_ac97_remove(struct platform_device *pdev,
  211. struct snd_soc_dai *dai)
  212. {
  213. free_irq(IRQ_S3C244x_AC97, NULL);
  214. clk_disable(s3c24xx_ac97.ac97_clk);
  215. clk_put(s3c24xx_ac97.ac97_clk);
  216. iounmap(s3c24xx_ac97.regs);
  217. }
  218. static int s3c2443_ac97_hw_params(struct snd_pcm_substream *substream,
  219. struct snd_pcm_hw_params *params)
  220. {
  221. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  222. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  223. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  224. cpu_dai->dma_data = &s3c2443_ac97_pcm_stereo_out;
  225. else
  226. cpu_dai->dma_data = &s3c2443_ac97_pcm_stereo_in;
  227. return 0;
  228. }
  229. static int s3c2443_ac97_trigger(struct snd_pcm_substream *substream, int cmd)
  230. {
  231. u32 ac_glbctrl;
  232. ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  233. switch (cmd) {
  234. case SNDRV_PCM_TRIGGER_START:
  235. case SNDRV_PCM_TRIGGER_RESUME:
  236. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  237. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  238. ac_glbctrl |= S3C_AC97_GLBCTRL_PCMINTM_DMA;
  239. else
  240. ac_glbctrl |= S3C_AC97_GLBCTRL_PCMOUTTM_DMA;
  241. break;
  242. case SNDRV_PCM_TRIGGER_STOP:
  243. case SNDRV_PCM_TRIGGER_SUSPEND:
  244. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  245. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  246. ac_glbctrl &= ~S3C_AC97_GLBCTRL_PCMINTM_MASK;
  247. else
  248. ac_glbctrl &= ~S3C_AC97_GLBCTRL_PCMOUTTM_MASK;
  249. break;
  250. }
  251. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  252. return 0;
  253. }
  254. static int s3c2443_ac97_hw_mic_params(struct snd_pcm_substream *substream,
  255. struct snd_pcm_hw_params *params)
  256. {
  257. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  258. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  259. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  260. return -ENODEV;
  261. else
  262. cpu_dai->dma_data = &s3c2443_ac97_mic_mono_in;
  263. return 0;
  264. }
  265. static int s3c2443_ac97_mic_trigger(struct snd_pcm_substream *substream,
  266. int cmd)
  267. {
  268. u32 ac_glbctrl;
  269. ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  270. switch (cmd) {
  271. case SNDRV_PCM_TRIGGER_START:
  272. case SNDRV_PCM_TRIGGER_RESUME:
  273. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  274. ac_glbctrl |= S3C_AC97_GLBCTRL_PCMINTM_DMA;
  275. break;
  276. case SNDRV_PCM_TRIGGER_STOP:
  277. case SNDRV_PCM_TRIGGER_SUSPEND:
  278. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  279. ac_glbctrl &= ~S3C_AC97_GLBCTRL_PCMINTM_MASK;
  280. }
  281. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  282. return 0;
  283. }
  284. #define s3c2443_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  285. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
  286. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
  287. struct snd_soc_dai s3c2443_ac97_dai[] = {
  288. {
  289. .name = "s3c2443-ac97",
  290. .id = 0,
  291. .type = SND_SOC_DAI_AC97,
  292. .probe = s3c2443_ac97_probe,
  293. .remove = s3c2443_ac97_remove,
  294. .playback = {
  295. .stream_name = "AC97 Playback",
  296. .channels_min = 2,
  297. .channels_max = 2,
  298. .rates = s3c2443_AC97_RATES,
  299. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  300. .capture = {
  301. .stream_name = "AC97 Capture",
  302. .channels_min = 2,
  303. .channels_max = 2,
  304. .rates = s3c2443_AC97_RATES,
  305. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  306. .ops = {
  307. .hw_params = s3c2443_ac97_hw_params,
  308. .trigger = s3c2443_ac97_trigger},
  309. },
  310. {
  311. .name = "pxa2xx-ac97-mic",
  312. .id = 1,
  313. .type = SND_SOC_DAI_AC97,
  314. .capture = {
  315. .stream_name = "AC97 Mic Capture",
  316. .channels_min = 1,
  317. .channels_max = 1,
  318. .rates = s3c2443_AC97_RATES,
  319. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  320. .ops = {
  321. .hw_params = s3c2443_ac97_hw_mic_params,
  322. .trigger = s3c2443_ac97_mic_trigger,},
  323. },
  324. };
  325. EXPORT_SYMBOL_GPL(s3c2443_ac97_dai);
  326. EXPORT_SYMBOL_GPL(soc_ac97_ops);
  327. MODULE_AUTHOR("Graeme Gregory");
  328. MODULE_DESCRIPTION("AC97 driver for the Samsung s3c2443 chip");
  329. MODULE_LICENSE("GPL");