s3c2443-ac97.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 <asm/dma.h>
  33. #include <mach/dma.h>
  34. #include "s3c-dma.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 DEFINE_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. mutex_lock(&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. mutex_unlock(&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. mutex_lock(&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. mutex_unlock(&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 s3c_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 s3c_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 s3c_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. struct snd_soc_dai *dai)
  221. {
  222. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  223. struct snd_soc_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. struct snd_soc_dai *dai)
  232. {
  233. u32 ac_glbctrl;
  234. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  235. int channel = ((struct s3c_dma_params *)
  236. rtd->dai->cpu_dai->dma_data)->channel;
  237. ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  238. switch (cmd) {
  239. case SNDRV_PCM_TRIGGER_START:
  240. case SNDRV_PCM_TRIGGER_RESUME:
  241. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  242. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  243. ac_glbctrl |= S3C_AC97_GLBCTRL_PCMINTM_DMA;
  244. else
  245. ac_glbctrl |= S3C_AC97_GLBCTRL_PCMOUTTM_DMA;
  246. break;
  247. case SNDRV_PCM_TRIGGER_STOP:
  248. case SNDRV_PCM_TRIGGER_SUSPEND:
  249. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  250. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  251. ac_glbctrl &= ~S3C_AC97_GLBCTRL_PCMINTM_MASK;
  252. else
  253. ac_glbctrl &= ~S3C_AC97_GLBCTRL_PCMOUTTM_MASK;
  254. break;
  255. }
  256. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  257. s3c2410_dma_ctrl(channel, S3C2410_DMAOP_STARTED);
  258. return 0;
  259. }
  260. static int s3c2443_ac97_hw_mic_params(struct snd_pcm_substream *substream,
  261. struct snd_pcm_hw_params *params,
  262. struct snd_soc_dai *dai)
  263. {
  264. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  265. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  266. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  267. return -ENODEV;
  268. else
  269. cpu_dai->dma_data = &s3c2443_ac97_mic_mono_in;
  270. return 0;
  271. }
  272. static int s3c2443_ac97_mic_trigger(struct snd_pcm_substream *substream,
  273. int cmd, struct snd_soc_dai *dai)
  274. {
  275. u32 ac_glbctrl;
  276. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  277. int channel = ((struct s3c_dma_params *)
  278. rtd->dai->cpu_dai->dma_data)->channel;
  279. ac_glbctrl = readl(s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  280. switch (cmd) {
  281. case SNDRV_PCM_TRIGGER_START:
  282. case SNDRV_PCM_TRIGGER_RESUME:
  283. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  284. ac_glbctrl |= S3C_AC97_GLBCTRL_PCMINTM_DMA;
  285. break;
  286. case SNDRV_PCM_TRIGGER_STOP:
  287. case SNDRV_PCM_TRIGGER_SUSPEND:
  288. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  289. ac_glbctrl &= ~S3C_AC97_GLBCTRL_PCMINTM_MASK;
  290. }
  291. writel(ac_glbctrl, s3c24xx_ac97.regs + S3C_AC97_GLBCTRL);
  292. s3c2410_dma_ctrl(channel, S3C2410_DMAOP_STARTED);
  293. return 0;
  294. }
  295. #define s3c2443_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  296. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
  297. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
  298. static struct snd_soc_dai_ops s3c2443_ac97_dai_ops = {
  299. .hw_params = s3c2443_ac97_hw_params,
  300. .trigger = s3c2443_ac97_trigger,
  301. };
  302. static struct snd_soc_dai_ops s3c2443_ac97_mic_dai_ops = {
  303. .hw_params = s3c2443_ac97_hw_mic_params,
  304. .trigger = s3c2443_ac97_mic_trigger,
  305. };
  306. struct snd_soc_dai s3c2443_ac97_dai[] = {
  307. {
  308. .name = "s3c2443-ac97",
  309. .id = 0,
  310. .ac97_control = 1,
  311. .probe = s3c2443_ac97_probe,
  312. .remove = s3c2443_ac97_remove,
  313. .playback = {
  314. .stream_name = "AC97 Playback",
  315. .channels_min = 2,
  316. .channels_max = 2,
  317. .rates = s3c2443_AC97_RATES,
  318. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  319. .capture = {
  320. .stream_name = "AC97 Capture",
  321. .channels_min = 2,
  322. .channels_max = 2,
  323. .rates = s3c2443_AC97_RATES,
  324. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  325. .ops = &s3c2443_ac97_dai_ops,
  326. },
  327. {
  328. .name = "pxa2xx-ac97-mic",
  329. .id = 1,
  330. .ac97_control = 1,
  331. .capture = {
  332. .stream_name = "AC97 Mic Capture",
  333. .channels_min = 1,
  334. .channels_max = 1,
  335. .rates = s3c2443_AC97_RATES,
  336. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  337. .ops = &s3c2443_ac97_mic_dai_ops,
  338. },
  339. };
  340. EXPORT_SYMBOL_GPL(s3c2443_ac97_dai);
  341. EXPORT_SYMBOL_GPL(soc_ac97_ops);
  342. static int __init s3c2443_ac97_init(void)
  343. {
  344. return snd_soc_register_dais(s3c2443_ac97_dai,
  345. ARRAY_SIZE(s3c2443_ac97_dai));
  346. }
  347. module_init(s3c2443_ac97_init);
  348. static void __exit s3c2443_ac97_exit(void)
  349. {
  350. snd_soc_unregister_dais(s3c2443_ac97_dai,
  351. ARRAY_SIZE(s3c2443_ac97_dai));
  352. }
  353. module_exit(s3c2443_ac97_exit);
  354. MODULE_AUTHOR("Graeme Gregory");
  355. MODULE_DESCRIPTION("AC97 driver for the Samsung s3c2443 chip");
  356. MODULE_LICENSE("GPL");