Merge branch 'drm-next' of git://people.freedesktop.org/~airlied/linux
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / sound / soc / spear / spdif_in.c
1 /*
2 * ALSA SoC SPDIF In Audio Layer for spear processors
3 *
4 * Copyright (C) 2012 ST Microelectronics
5 * Vipin Kumar <vipin.kumar@st.com>
6 *
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
10 */
11
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/io.h>
18 #include <linux/ioport.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/soc.h>
24 #include <sound/spear_dma.h>
25 #include <sound/spear_spdif.h>
26 #include "spdif_in_regs.h"
27
28 struct spdif_in_params {
29 u32 format;
30 };
31
32 struct spdif_in_dev {
33 struct clk *clk;
34 struct spear_dma_data dma_params;
35 struct spdif_in_params saved_params;
36 void *io_base;
37 struct device *dev;
38 void (*reset_perip)(void);
39 int irq;
40 };
41
42 static void spdif_in_configure(struct spdif_in_dev *host)
43 {
44 u32 ctrl = SPDIF_IN_PRTYEN | SPDIF_IN_STATEN | SPDIF_IN_USREN |
45 SPDIF_IN_VALEN | SPDIF_IN_BLKEN;
46 ctrl |= SPDIF_MODE_16BIT | SPDIF_FIFO_THRES_16;
47
48 writel(ctrl, host->io_base + SPDIF_IN_CTRL);
49 writel(0xF, host->io_base + SPDIF_IN_IRQ_MASK);
50 }
51
52 static int spdif_in_startup(struct snd_pcm_substream *substream,
53 struct snd_soc_dai *cpu_dai)
54 {
55 struct spdif_in_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
56
57 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
58 return -EINVAL;
59
60 snd_soc_dai_set_dma_data(cpu_dai, substream, (void *)&host->dma_params);
61 return 0;
62 }
63
64 static void spdif_in_shutdown(struct snd_pcm_substream *substream,
65 struct snd_soc_dai *dai)
66 {
67 struct spdif_in_dev *host = snd_soc_dai_get_drvdata(dai);
68
69 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
70 return;
71
72 writel(0x0, host->io_base + SPDIF_IN_IRQ_MASK);
73 snd_soc_dai_set_dma_data(dai, substream, NULL);
74 }
75
76 static void spdif_in_format(struct spdif_in_dev *host, u32 format)
77 {
78 u32 ctrl = readl(host->io_base + SPDIF_IN_CTRL);
79
80 switch (format) {
81 case SNDRV_PCM_FORMAT_S16_LE:
82 ctrl |= SPDIF_XTRACT_16BIT;
83 break;
84
85 case SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE:
86 ctrl &= ~SPDIF_XTRACT_16BIT;
87 break;
88 }
89
90 writel(ctrl, host->io_base + SPDIF_IN_CTRL);
91 }
92
93 static int spdif_in_hw_params(struct snd_pcm_substream *substream,
94 struct snd_pcm_hw_params *params,
95 struct snd_soc_dai *dai)
96 {
97 struct spdif_in_dev *host = snd_soc_dai_get_drvdata(dai);
98 u32 format;
99
100 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
101 return -EINVAL;
102
103 format = params_format(params);
104 host->saved_params.format = format;
105
106 return 0;
107 }
108
109 static int spdif_in_trigger(struct snd_pcm_substream *substream, int cmd,
110 struct snd_soc_dai *dai)
111 {
112 struct spdif_in_dev *host = snd_soc_dai_get_drvdata(dai);
113 u32 ctrl;
114 int ret = 0;
115
116 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
117 return -EINVAL;
118
119 switch (cmd) {
120 case SNDRV_PCM_TRIGGER_START:
121 case SNDRV_PCM_TRIGGER_RESUME:
122 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
123 clk_enable(host->clk);
124 spdif_in_configure(host);
125 spdif_in_format(host, host->saved_params.format);
126
127 ctrl = readl(host->io_base + SPDIF_IN_CTRL);
128 ctrl |= SPDIF_IN_SAMPLE | SPDIF_IN_ENB;
129 writel(ctrl, host->io_base + SPDIF_IN_CTRL);
130 writel(0xF, host->io_base + SPDIF_IN_IRQ_MASK);
131 break;
132
133 case SNDRV_PCM_TRIGGER_STOP:
134 case SNDRV_PCM_TRIGGER_SUSPEND:
135 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
136 ctrl = readl(host->io_base + SPDIF_IN_CTRL);
137 ctrl &= ~(SPDIF_IN_SAMPLE | SPDIF_IN_ENB);
138 writel(ctrl, host->io_base + SPDIF_IN_CTRL);
139 writel(0x0, host->io_base + SPDIF_IN_IRQ_MASK);
140
141 if (host->reset_perip)
142 host->reset_perip();
143 clk_disable(host->clk);
144 break;
145
146 default:
147 ret = -EINVAL;
148 break;
149 }
150 return ret;
151 }
152
153 static struct snd_soc_dai_ops spdif_in_dai_ops = {
154 .startup = spdif_in_startup,
155 .shutdown = spdif_in_shutdown,
156 .trigger = spdif_in_trigger,
157 .hw_params = spdif_in_hw_params,
158 };
159
160 struct snd_soc_dai_driver spdif_in_dai = {
161 .capture = {
162 .channels_min = 2,
163 .channels_max = 2,
164 .rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
165 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | \
166 SNDRV_PCM_RATE_192000),
167 .formats = SNDRV_PCM_FMTBIT_S16_LE | \
168 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
169 },
170 .ops = &spdif_in_dai_ops,
171 };
172
173 static const struct snd_soc_component_driver spdif_in_component = {
174 .name = "spdif-in",
175 };
176
177 static irqreturn_t spdif_in_irq(int irq, void *arg)
178 {
179 struct spdif_in_dev *host = (struct spdif_in_dev *)arg;
180
181 u32 irq_status = readl(host->io_base + SPDIF_IN_IRQ);
182
183 if (!irq_status)
184 return IRQ_NONE;
185
186 if (irq_status & SPDIF_IRQ_FIFOWRITE)
187 dev_err(host->dev, "spdif in: fifo write error");
188 if (irq_status & SPDIF_IRQ_EMPTYFIFOREAD)
189 dev_err(host->dev, "spdif in: empty fifo read error");
190 if (irq_status & SPDIF_IRQ_FIFOFULL)
191 dev_err(host->dev, "spdif in: fifo full error");
192 if (irq_status & SPDIF_IRQ_OUTOFRANGE)
193 dev_err(host->dev, "spdif in: out of range error");
194
195 writel(0, host->io_base + SPDIF_IN_IRQ);
196
197 return IRQ_HANDLED;
198 }
199
200 static int spdif_in_probe(struct platform_device *pdev)
201 {
202 struct spdif_in_dev *host;
203 struct spear_spdif_platform_data *pdata;
204 struct resource *res, *res_fifo;
205 int ret;
206
207 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
208 if (!res)
209 return -EINVAL;
210
211 res_fifo = platform_get_resource(pdev, IORESOURCE_IO, 0);
212 if (!res_fifo)
213 return -EINVAL;
214
215 if (!devm_request_mem_region(&pdev->dev, res->start,
216 resource_size(res), pdev->name)) {
217 dev_warn(&pdev->dev, "Failed to get memory resourse\n");
218 return -ENOENT;
219 }
220
221 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
222 if (!host) {
223 dev_warn(&pdev->dev, "kzalloc fail\n");
224 return -ENOMEM;
225 }
226
227 host->io_base = devm_ioremap(&pdev->dev, res->start,
228 resource_size(res));
229 if (!host->io_base) {
230 dev_warn(&pdev->dev, "ioremap failed\n");
231 return -ENOMEM;
232 }
233
234 host->irq = platform_get_irq(pdev, 0);
235 if (host->irq < 0)
236 return -EINVAL;
237
238 host->clk = clk_get(&pdev->dev, NULL);
239 if (IS_ERR(host->clk))
240 return PTR_ERR(host->clk);
241
242 pdata = dev_get_platdata(&pdev->dev);
243
244 if (!pdata)
245 return -EINVAL;
246
247 host->dma_params.data = pdata->dma_params;
248 host->dma_params.addr = res_fifo->start;
249 host->dma_params.max_burst = 16;
250 host->dma_params.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
251 host->dma_params.filter = pdata->filter;
252 host->reset_perip = pdata->reset_perip;
253
254 host->dev = &pdev->dev;
255 dev_set_drvdata(&pdev->dev, host);
256
257 ret = devm_request_irq(&pdev->dev, host->irq, spdif_in_irq, 0,
258 "spdif-in", host);
259 if (ret) {
260 clk_put(host->clk);
261 dev_warn(&pdev->dev, "request_irq failed\n");
262 return ret;
263 }
264
265 ret = snd_soc_register_component(&pdev->dev, &spdif_in_component,
266 &spdif_in_dai, 1);
267 if (ret != 0) {
268 clk_put(host->clk);
269 return ret;
270 }
271
272 return 0;
273 }
274
275 static int spdif_in_remove(struct platform_device *pdev)
276 {
277 struct spdif_in_dev *host = dev_get_drvdata(&pdev->dev);
278
279 snd_soc_unregister_component(&pdev->dev);
280 dev_set_drvdata(&pdev->dev, NULL);
281
282 clk_put(host->clk);
283
284 return 0;
285 }
286
287
288 static struct platform_driver spdif_in_driver = {
289 .probe = spdif_in_probe,
290 .remove = spdif_in_remove,
291 .driver = {
292 .name = "spdif-in",
293 .owner = THIS_MODULE,
294 },
295 };
296
297 module_platform_driver(spdif_in_driver);
298
299 MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>");
300 MODULE_DESCRIPTION("SPEAr SPDIF IN SoC Interface");
301 MODULE_LICENSE("GPL");
302 MODULE_ALIAS("platform:spdif_in");