s3c2443-ac97.c 11 KB

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