s3c2443-ac97.c 11 KB

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