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