1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun //
3*4882a593Smuzhiyun // ALSA Soc Audio Layer - S3C2412 I2S driver
4*4882a593Smuzhiyun //
5*4882a593Smuzhiyun // Copyright (c) 2006 Wolfson Microelectronics PLC.
6*4882a593Smuzhiyun // Graeme Gregory graeme.gregory@wolfsonmicro.com
7*4882a593Smuzhiyun // linux@wolfsonmicro.com
8*4882a593Smuzhiyun //
9*4882a593Smuzhiyun // Copyright (c) 2007, 2004-2005 Simtec Electronics
10*4882a593Smuzhiyun // http://armlinux.simtec.co.uk/
11*4882a593Smuzhiyun // Ben Dooks <ben@simtec.co.uk>
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <linux/delay.h>
14*4882a593Smuzhiyun #include <linux/gpio.h>
15*4882a593Smuzhiyun #include <linux/clk.h>
16*4882a593Smuzhiyun #include <linux/io.h>
17*4882a593Smuzhiyun #include <linux/module.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #include <sound/soc.h>
20*4882a593Smuzhiyun #include <sound/pcm_params.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #include "dma.h"
23*4882a593Smuzhiyun #include "regs-i2s-v2.h"
24*4882a593Smuzhiyun #include "s3c2412-i2s.h"
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #include <linux/platform_data/asoc-s3c.h>
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun static struct snd_dmaengine_dai_dma_data s3c2412_i2s_pcm_stereo_out = {
29*4882a593Smuzhiyun .chan_name = "tx",
30*4882a593Smuzhiyun .addr_width = 4,
31*4882a593Smuzhiyun };
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun static struct snd_dmaengine_dai_dma_data s3c2412_i2s_pcm_stereo_in = {
34*4882a593Smuzhiyun .chan_name = "rx",
35*4882a593Smuzhiyun .addr_width = 4,
36*4882a593Smuzhiyun };
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun static struct s3c_i2sv2_info s3c2412_i2s;
39*4882a593Smuzhiyun
s3c2412_i2s_probe(struct snd_soc_dai * dai)40*4882a593Smuzhiyun static int s3c2412_i2s_probe(struct snd_soc_dai *dai)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun int ret;
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun pr_debug("Entered %s\n", __func__);
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun snd_soc_dai_init_dma_data(dai, &s3c2412_i2s_pcm_stereo_out,
47*4882a593Smuzhiyun &s3c2412_i2s_pcm_stereo_in);
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun ret = s3c_i2sv2_probe(dai, &s3c2412_i2s);
50*4882a593Smuzhiyun if (ret)
51*4882a593Smuzhiyun return ret;
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun s3c2412_i2s.dma_capture = &s3c2412_i2s_pcm_stereo_in;
54*4882a593Smuzhiyun s3c2412_i2s.dma_playback = &s3c2412_i2s_pcm_stereo_out;
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun s3c2412_i2s.iis_cclk = devm_clk_get(dai->dev, "i2sclk");
57*4882a593Smuzhiyun if (IS_ERR(s3c2412_i2s.iis_cclk)) {
58*4882a593Smuzhiyun pr_err("failed to get i2sclk clock\n");
59*4882a593Smuzhiyun ret = PTR_ERR(s3c2412_i2s.iis_cclk);
60*4882a593Smuzhiyun goto err;
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun /* Set MPLL as the source for IIS CLK */
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun clk_set_parent(s3c2412_i2s.iis_cclk, clk_get(NULL, "mpll"));
66*4882a593Smuzhiyun ret = clk_prepare_enable(s3c2412_i2s.iis_cclk);
67*4882a593Smuzhiyun if (ret)
68*4882a593Smuzhiyun goto err;
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun return 0;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun err:
73*4882a593Smuzhiyun s3c_i2sv2_cleanup(dai, &s3c2412_i2s);
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun return ret;
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun
s3c2412_i2s_remove(struct snd_soc_dai * dai)78*4882a593Smuzhiyun static int s3c2412_i2s_remove(struct snd_soc_dai *dai)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun clk_disable_unprepare(s3c2412_i2s.iis_cclk);
81*4882a593Smuzhiyun s3c_i2sv2_cleanup(dai, &s3c2412_i2s);
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun return 0;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun
s3c2412_i2s_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * cpu_dai)86*4882a593Smuzhiyun static int s3c2412_i2s_hw_params(struct snd_pcm_substream *substream,
87*4882a593Smuzhiyun struct snd_pcm_hw_params *params,
88*4882a593Smuzhiyun struct snd_soc_dai *cpu_dai)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun struct s3c_i2sv2_info *i2s = snd_soc_dai_get_drvdata(cpu_dai);
91*4882a593Smuzhiyun u32 iismod;
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun pr_debug("Entered %s\n", __func__);
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun iismod = readl(i2s->regs + S3C2412_IISMOD);
96*4882a593Smuzhiyun pr_debug("%s: r: IISMOD: %x\n", __func__, iismod);
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun switch (params_width(params)) {
99*4882a593Smuzhiyun case 8:
100*4882a593Smuzhiyun iismod |= S3C2412_IISMOD_8BIT;
101*4882a593Smuzhiyun break;
102*4882a593Smuzhiyun case 16:
103*4882a593Smuzhiyun iismod &= ~S3C2412_IISMOD_8BIT;
104*4882a593Smuzhiyun break;
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun writel(iismod, i2s->regs + S3C2412_IISMOD);
108*4882a593Smuzhiyun pr_debug("%s: w: IISMOD: %x\n", __func__, iismod);
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun return 0;
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun #ifdef CONFIG_PM
s3c2412_i2s_suspend(struct snd_soc_component * component)114*4882a593Smuzhiyun static int s3c2412_i2s_suspend(struct snd_soc_component *component)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun struct s3c_i2sv2_info *i2s = snd_soc_component_get_drvdata(component);
117*4882a593Smuzhiyun u32 iismod;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun if (component->active) {
120*4882a593Smuzhiyun i2s->suspend_iismod = readl(i2s->regs + S3C2412_IISMOD);
121*4882a593Smuzhiyun i2s->suspend_iiscon = readl(i2s->regs + S3C2412_IISCON);
122*4882a593Smuzhiyun i2s->suspend_iispsr = readl(i2s->regs + S3C2412_IISPSR);
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun /* some basic suspend checks */
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun iismod = readl(i2s->regs + S3C2412_IISMOD);
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun if (iismod & S3C2412_IISCON_RXDMA_ACTIVE)
129*4882a593Smuzhiyun pr_warn("%s: RXDMA active?\n", __func__);
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun if (iismod & S3C2412_IISCON_TXDMA_ACTIVE)
132*4882a593Smuzhiyun pr_warn("%s: TXDMA active?\n", __func__);
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun if (iismod & S3C2412_IISCON_IIS_ACTIVE)
135*4882a593Smuzhiyun pr_warn("%s: IIS active\n", __func__);
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun return 0;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
s3c2412_i2s_resume(struct snd_soc_component * component)141*4882a593Smuzhiyun static int s3c2412_i2s_resume(struct snd_soc_component *component)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun struct s3c_i2sv2_info *i2s = snd_soc_component_get_drvdata(component);
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun pr_info("component_active %d, IISMOD %08x, IISCON %08x\n",
146*4882a593Smuzhiyun component->active, i2s->suspend_iismod, i2s->suspend_iiscon);
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun if (component->active) {
149*4882a593Smuzhiyun writel(i2s->suspend_iiscon, i2s->regs + S3C2412_IISCON);
150*4882a593Smuzhiyun writel(i2s->suspend_iismod, i2s->regs + S3C2412_IISMOD);
151*4882a593Smuzhiyun writel(i2s->suspend_iispsr, i2s->regs + S3C2412_IISPSR);
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun writel(S3C2412_IISFIC_RXFLUSH | S3C2412_IISFIC_TXFLUSH,
154*4882a593Smuzhiyun i2s->regs + S3C2412_IISFIC);
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun ndelay(250);
157*4882a593Smuzhiyun writel(0x0, i2s->regs + S3C2412_IISFIC);
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun return 0;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun #else
163*4882a593Smuzhiyun #define s3c2412_i2s_suspend NULL
164*4882a593Smuzhiyun #define s3c2412_i2s_resume NULL
165*4882a593Smuzhiyun #endif
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun #define S3C2412_I2S_RATES \
168*4882a593Smuzhiyun (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_16000 | \
169*4882a593Smuzhiyun SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
170*4882a593Smuzhiyun SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun static const struct snd_soc_dai_ops s3c2412_i2s_dai_ops = {
173*4882a593Smuzhiyun .hw_params = s3c2412_i2s_hw_params,
174*4882a593Smuzhiyun };
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun static struct snd_soc_dai_driver s3c2412_i2s_dai = {
177*4882a593Smuzhiyun .probe = s3c2412_i2s_probe,
178*4882a593Smuzhiyun .remove = s3c2412_i2s_remove,
179*4882a593Smuzhiyun .playback = {
180*4882a593Smuzhiyun .channels_min = 2,
181*4882a593Smuzhiyun .channels_max = 2,
182*4882a593Smuzhiyun .rates = S3C2412_I2S_RATES,
183*4882a593Smuzhiyun .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,
184*4882a593Smuzhiyun },
185*4882a593Smuzhiyun .capture = {
186*4882a593Smuzhiyun .channels_min = 2,
187*4882a593Smuzhiyun .channels_max = 2,
188*4882a593Smuzhiyun .rates = S3C2412_I2S_RATES,
189*4882a593Smuzhiyun .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,
190*4882a593Smuzhiyun },
191*4882a593Smuzhiyun .ops = &s3c2412_i2s_dai_ops,
192*4882a593Smuzhiyun };
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun static const struct snd_soc_component_driver s3c2412_i2s_component = {
195*4882a593Smuzhiyun .name = "s3c2412-i2s",
196*4882a593Smuzhiyun .suspend = s3c2412_i2s_suspend,
197*4882a593Smuzhiyun .resume = s3c2412_i2s_resume,
198*4882a593Smuzhiyun };
199*4882a593Smuzhiyun
s3c2412_iis_dev_probe(struct platform_device * pdev)200*4882a593Smuzhiyun static int s3c2412_iis_dev_probe(struct platform_device *pdev)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun int ret = 0;
203*4882a593Smuzhiyun struct resource *res;
204*4882a593Smuzhiyun struct s3c_audio_pdata *pdata = dev_get_platdata(&pdev->dev);
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun if (!pdata) {
207*4882a593Smuzhiyun dev_err(&pdev->dev, "missing platform data");
208*4882a593Smuzhiyun return -ENXIO;
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
212*4882a593Smuzhiyun s3c2412_i2s.regs = devm_ioremap_resource(&pdev->dev, res);
213*4882a593Smuzhiyun if (IS_ERR(s3c2412_i2s.regs))
214*4882a593Smuzhiyun return PTR_ERR(s3c2412_i2s.regs);
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun s3c2412_i2s_pcm_stereo_out.addr = res->start + S3C2412_IISTXD;
217*4882a593Smuzhiyun s3c2412_i2s_pcm_stereo_out.filter_data = pdata->dma_playback;
218*4882a593Smuzhiyun s3c2412_i2s_pcm_stereo_in.addr = res->start + S3C2412_IISRXD;
219*4882a593Smuzhiyun s3c2412_i2s_pcm_stereo_in.filter_data = pdata->dma_capture;
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun ret = samsung_asoc_dma_platform_register(&pdev->dev,
222*4882a593Smuzhiyun pdata->dma_filter,
223*4882a593Smuzhiyun "tx", "rx", NULL);
224*4882a593Smuzhiyun if (ret) {
225*4882a593Smuzhiyun pr_err("failed to register the DMA: %d\n", ret);
226*4882a593Smuzhiyun return ret;
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun ret = s3c_i2sv2_register_component(&pdev->dev, -1,
230*4882a593Smuzhiyun &s3c2412_i2s_component,
231*4882a593Smuzhiyun &s3c2412_i2s_dai);
232*4882a593Smuzhiyun if (ret)
233*4882a593Smuzhiyun pr_err("failed to register the dai\n");
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun return ret;
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun static struct platform_driver s3c2412_iis_driver = {
239*4882a593Smuzhiyun .probe = s3c2412_iis_dev_probe,
240*4882a593Smuzhiyun .driver = {
241*4882a593Smuzhiyun .name = "s3c2412-iis",
242*4882a593Smuzhiyun },
243*4882a593Smuzhiyun };
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun module_platform_driver(s3c2412_iis_driver);
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun /* Module information */
248*4882a593Smuzhiyun MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
249*4882a593Smuzhiyun MODULE_DESCRIPTION("S3C2412 I2S SoC Interface");
250*4882a593Smuzhiyun MODULE_LICENSE("GPL");
251*4882a593Smuzhiyun MODULE_ALIAS("platform:s3c2412-iis");
252