blob: 82c838753c0694cd2108e7fe0b04f3796644261d [file] [log] [blame]
Vipin Kumarace36d82012-06-21 15:54:53 +05301/*
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
28struct spdif_in_params {
29 u32 format;
30};
31
32struct 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
42static 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
Lars-Peter Clausen46fdd8b2013-04-20 19:29:06 +020052static int spdif_in_dai_probe(struct snd_soc_dai *dai)
Vipin Kumarace36d82012-06-21 15:54:53 +053053{
Lars-Peter Clausen46fdd8b2013-04-20 19:29:06 +020054 struct spdif_in_dev *host = snd_soc_dai_get_drvdata(dai);
Vipin Kumarace36d82012-06-21 15:54:53 +053055
Lars-Peter Clausen46fdd8b2013-04-20 19:29:06 +020056 dai->capture_dma_data = &host->dma_params;
Vipin Kumarace36d82012-06-21 15:54:53 +053057
Vipin Kumarace36d82012-06-21 15:54:53 +053058 return 0;
59}
60
61static void spdif_in_shutdown(struct snd_pcm_substream *substream,
62 struct snd_soc_dai *dai)
63{
64 struct spdif_in_dev *host = snd_soc_dai_get_drvdata(dai);
65
66 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
67 return;
68
69 writel(0x0, host->io_base + SPDIF_IN_IRQ_MASK);
Vipin Kumarace36d82012-06-21 15:54:53 +053070}
71
72static void spdif_in_format(struct spdif_in_dev *host, u32 format)
73{
74 u32 ctrl = readl(host->io_base + SPDIF_IN_CTRL);
75
76 switch (format) {
77 case SNDRV_PCM_FORMAT_S16_LE:
78 ctrl |= SPDIF_XTRACT_16BIT;
79 break;
80
81 case SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE:
82 ctrl &= ~SPDIF_XTRACT_16BIT;
83 break;
84 }
85
86 writel(ctrl, host->io_base + SPDIF_IN_CTRL);
87}
88
89static int spdif_in_hw_params(struct snd_pcm_substream *substream,
90 struct snd_pcm_hw_params *params,
91 struct snd_soc_dai *dai)
92{
93 struct spdif_in_dev *host = snd_soc_dai_get_drvdata(dai);
94 u32 format;
95
96 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
97 return -EINVAL;
98
99 format = params_format(params);
100 host->saved_params.format = format;
101
102 return 0;
103}
104
105static int spdif_in_trigger(struct snd_pcm_substream *substream, int cmd,
106 struct snd_soc_dai *dai)
107{
108 struct spdif_in_dev *host = snd_soc_dai_get_drvdata(dai);
109 u32 ctrl;
110 int ret = 0;
111
112 if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
113 return -EINVAL;
114
115 switch (cmd) {
116 case SNDRV_PCM_TRIGGER_START:
117 case SNDRV_PCM_TRIGGER_RESUME:
118 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
119 clk_enable(host->clk);
120 spdif_in_configure(host);
121 spdif_in_format(host, host->saved_params.format);
122
123 ctrl = readl(host->io_base + SPDIF_IN_CTRL);
124 ctrl |= SPDIF_IN_SAMPLE | SPDIF_IN_ENB;
125 writel(ctrl, host->io_base + SPDIF_IN_CTRL);
126 writel(0xF, host->io_base + SPDIF_IN_IRQ_MASK);
127 break;
128
129 case SNDRV_PCM_TRIGGER_STOP:
130 case SNDRV_PCM_TRIGGER_SUSPEND:
131 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
132 ctrl = readl(host->io_base + SPDIF_IN_CTRL);
133 ctrl &= ~(SPDIF_IN_SAMPLE | SPDIF_IN_ENB);
134 writel(ctrl, host->io_base + SPDIF_IN_CTRL);
135 writel(0x0, host->io_base + SPDIF_IN_IRQ_MASK);
136
137 if (host->reset_perip)
138 host->reset_perip();
139 clk_disable(host->clk);
140 break;
141
142 default:
143 ret = -EINVAL;
144 break;
145 }
146 return ret;
147}
148
149static struct snd_soc_dai_ops spdif_in_dai_ops = {
Vipin Kumarace36d82012-06-21 15:54:53 +0530150 .shutdown = spdif_in_shutdown,
151 .trigger = spdif_in_trigger,
152 .hw_params = spdif_in_hw_params,
153};
154
155struct snd_soc_dai_driver spdif_in_dai = {
Lars-Peter Clausen46fdd8b2013-04-20 19:29:06 +0200156 .probe = spdif_in_dai_probe,
Vipin Kumarace36d82012-06-21 15:54:53 +0530157 .capture = {
158 .channels_min = 2,
159 .channels_max = 2,
160 .rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
161 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | \
162 SNDRV_PCM_RATE_192000),
163 .formats = SNDRV_PCM_FMTBIT_S16_LE | \
164 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
165 },
166 .ops = &spdif_in_dai_ops,
167};
168
Kuninori Morimoto669b49762013-03-21 03:37:11 -0700169static const struct snd_soc_component_driver spdif_in_component = {
170 .name = "spdif-in",
171};
172
Vipin Kumarace36d82012-06-21 15:54:53 +0530173static irqreturn_t spdif_in_irq(int irq, void *arg)
174{
175 struct spdif_in_dev *host = (struct spdif_in_dev *)arg;
176
177 u32 irq_status = readl(host->io_base + SPDIF_IN_IRQ);
178
179 if (!irq_status)
180 return IRQ_NONE;
181
182 if (irq_status & SPDIF_IRQ_FIFOWRITE)
183 dev_err(host->dev, "spdif in: fifo write error");
184 if (irq_status & SPDIF_IRQ_EMPTYFIFOREAD)
185 dev_err(host->dev, "spdif in: empty fifo read error");
186 if (irq_status & SPDIF_IRQ_FIFOFULL)
187 dev_err(host->dev, "spdif in: fifo full error");
188 if (irq_status & SPDIF_IRQ_OUTOFRANGE)
189 dev_err(host->dev, "spdif in: out of range error");
190
191 writel(0, host->io_base + SPDIF_IN_IRQ);
192
193 return IRQ_HANDLED;
194}
195
196static int spdif_in_probe(struct platform_device *pdev)
197{
198 struct spdif_in_dev *host;
199 struct spear_spdif_platform_data *pdata;
200 struct resource *res, *res_fifo;
201 int ret;
202
203 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
204 if (!res)
205 return -EINVAL;
206
207 res_fifo = platform_get_resource(pdev, IORESOURCE_IO, 0);
208 if (!res_fifo)
209 return -EINVAL;
210
211 if (!devm_request_mem_region(&pdev->dev, res->start,
212 resource_size(res), pdev->name)) {
213 dev_warn(&pdev->dev, "Failed to get memory resourse\n");
214 return -ENOENT;
215 }
216
217 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
218 if (!host) {
219 dev_warn(&pdev->dev, "kzalloc fail\n");
220 return -ENOMEM;
221 }
222
223 host->io_base = devm_ioremap(&pdev->dev, res->start,
224 resource_size(res));
225 if (!host->io_base) {
226 dev_warn(&pdev->dev, "ioremap failed\n");
227 return -ENOMEM;
228 }
229
230 host->irq = platform_get_irq(pdev, 0);
231 if (host->irq < 0)
232 return -EINVAL;
233
234 host->clk = clk_get(&pdev->dev, NULL);
235 if (IS_ERR(host->clk))
236 return PTR_ERR(host->clk);
237
238 pdata = dev_get_platdata(&pdev->dev);
239
240 if (!pdata)
241 return -EINVAL;
242
243 host->dma_params.data = pdata->dma_params;
244 host->dma_params.addr = res_fifo->start;
245 host->dma_params.max_burst = 16;
246 host->dma_params.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
247 host->dma_params.filter = pdata->filter;
248 host->reset_perip = pdata->reset_perip;
249
250 host->dev = &pdev->dev;
251 dev_set_drvdata(&pdev->dev, host);
252
253 ret = devm_request_irq(&pdev->dev, host->irq, spdif_in_irq, 0,
254 "spdif-in", host);
255 if (ret) {
256 clk_put(host->clk);
257 dev_warn(&pdev->dev, "request_irq failed\n");
258 return ret;
259 }
260
Kuninori Morimoto669b49762013-03-21 03:37:11 -0700261 ret = snd_soc_register_component(&pdev->dev, &spdif_in_component,
262 &spdif_in_dai, 1);
Vipin Kumarace36d82012-06-21 15:54:53 +0530263 if (ret != 0) {
264 clk_put(host->clk);
265 return ret;
266 }
267
268 return 0;
269}
270
271static int spdif_in_remove(struct platform_device *pdev)
272{
273 struct spdif_in_dev *host = dev_get_drvdata(&pdev->dev);
274
Kuninori Morimoto669b49762013-03-21 03:37:11 -0700275 snd_soc_unregister_component(&pdev->dev);
Vipin Kumarace36d82012-06-21 15:54:53 +0530276 dev_set_drvdata(&pdev->dev, NULL);
277
278 clk_put(host->clk);
279
280 return 0;
281}
282
283
284static struct platform_driver spdif_in_driver = {
285 .probe = spdif_in_probe,
286 .remove = spdif_in_remove,
287 .driver = {
288 .name = "spdif-in",
289 .owner = THIS_MODULE,
290 },
291};
292
293module_platform_driver(spdif_in_driver);
294
295MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>");
296MODULE_DESCRIPTION("SPEAr SPDIF IN SoC Interface");
297MODULE_LICENSE("GPL");
298MODULE_ALIAS("platform:spdif_in");