blob: 0a72d52d533ee6f8c657dde689231141cbe26505 [file] [log] [blame]
Vipin Kumare584f9b2012-07-04 16:11:13 +05301/*
2 * ALSA SoC SPDIF Out 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>
Stephen Warrene1771bc2013-12-10 12:35:25 -070021#include <sound/dmaengine_pcm.h>
Vipin Kumare584f9b2012-07-04 16:11:13 +053022#include <sound/soc.h>
23#include <sound/spear_dma.h>
24#include <sound/spear_spdif.h>
25#include "spdif_out_regs.h"
Stephen Warrenede38882013-12-10 12:35:24 -070026#include "spear_pcm.h"
Vipin Kumare584f9b2012-07-04 16:11:13 +053027
28struct spdif_out_params {
29 u32 rate;
30 u32 core_freq;
31 u32 mute;
32};
33
34struct spdif_out_dev {
35 struct clk *clk;
36 struct spear_dma_data dma_params;
37 struct spdif_out_params saved_params;
38 u32 running;
39 void __iomem *io_base;
Stephen Warrene1771bc2013-12-10 12:35:25 -070040 struct snd_dmaengine_dai_dma_data dma_params_tx;
41 struct snd_dmaengine_pcm_config config;
Vipin Kumare584f9b2012-07-04 16:11:13 +053042};
43
44static void spdif_out_configure(struct spdif_out_dev *host)
45{
46 writel(SPDIF_OUT_RESET, host->io_base + SPDIF_OUT_SOFT_RST);
47 mdelay(1);
48 writel(readl(host->io_base + SPDIF_OUT_SOFT_RST) & ~SPDIF_OUT_RESET,
49 host->io_base + SPDIF_OUT_SOFT_RST);
50
51 writel(SPDIF_OUT_FDMA_TRIG_16 | SPDIF_OUT_MEMFMT_16_16 |
52 SPDIF_OUT_VALID_HW | SPDIF_OUT_USER_HW |
53 SPDIF_OUT_CHNLSTA_HW | SPDIF_OUT_PARITY_HW,
54 host->io_base + SPDIF_OUT_CFG);
55
56 writel(0x7F, host->io_base + SPDIF_OUT_INT_STA_CLR);
57 writel(0x7F, host->io_base + SPDIF_OUT_INT_EN_CLR);
58}
59
60static int spdif_out_startup(struct snd_pcm_substream *substream,
61 struct snd_soc_dai *cpu_dai)
62{
63 struct spdif_out_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
64 int ret;
65
66 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
67 return -EINVAL;
68
Vipin Kumare584f9b2012-07-04 16:11:13 +053069 ret = clk_enable(host->clk);
70 if (ret)
71 return ret;
72
73 host->running = true;
74 spdif_out_configure(host);
75
76 return 0;
77}
78
79static void spdif_out_shutdown(struct snd_pcm_substream *substream,
80 struct snd_soc_dai *dai)
81{
82 struct spdif_out_dev *host = snd_soc_dai_get_drvdata(dai);
83
84 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
85 return;
86
87 clk_disable(host->clk);
88 host->running = false;
Vipin Kumare584f9b2012-07-04 16:11:13 +053089}
90
91static void spdif_out_clock(struct spdif_out_dev *host, u32 core_freq,
92 u32 rate)
93{
94 u32 divider, ctrl;
95
96 clk_set_rate(host->clk, core_freq);
97 divider = DIV_ROUND_CLOSEST(clk_get_rate(host->clk), (rate * 128));
98
99 ctrl = readl(host->io_base + SPDIF_OUT_CTRL);
100 ctrl &= ~SPDIF_DIVIDER_MASK;
101 ctrl |= (divider << SPDIF_DIVIDER_SHIFT) & SPDIF_DIVIDER_MASK;
102 writel(ctrl, host->io_base + SPDIF_OUT_CTRL);
103}
104
105static int spdif_out_hw_params(struct snd_pcm_substream *substream,
106 struct snd_pcm_hw_params *params,
107 struct snd_soc_dai *dai)
108{
109 struct spdif_out_dev *host = snd_soc_dai_get_drvdata(dai);
110 u32 rate, core_freq;
111
112 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
113 return -EINVAL;
114
115 rate = params_rate(params);
116
117 switch (rate) {
118 case 8000:
119 case 16000:
120 case 32000:
121 case 64000:
122 /*
123 * The clock is multiplied by 10 to bring it to feasible range
124 * of frequencies for sscg
125 */
126 core_freq = 64000 * 128 * 10; /* 81.92 MHz */
127 break;
128 case 5512:
129 case 11025:
130 case 22050:
131 case 44100:
132 case 88200:
133 case 176400:
134 core_freq = 176400 * 128; /* 22.5792 MHz */
135 break;
136 case 48000:
137 case 96000:
138 case 192000:
139 default:
140 core_freq = 192000 * 128; /* 24.576 MHz */
141 break;
142 }
143
144 spdif_out_clock(host, core_freq, rate);
145 host->saved_params.core_freq = core_freq;
146 host->saved_params.rate = rate;
147
148 return 0;
149}
150
151static int spdif_out_trigger(struct snd_pcm_substream *substream, int cmd,
152 struct snd_soc_dai *dai)
153{
154 struct spdif_out_dev *host = snd_soc_dai_get_drvdata(dai);
155 u32 ctrl;
156 int ret = 0;
157
158 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
159 return -EINVAL;
160
161 switch (cmd) {
162 case SNDRV_PCM_TRIGGER_START:
163 case SNDRV_PCM_TRIGGER_RESUME:
164 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
165 ctrl = readl(host->io_base + SPDIF_OUT_CTRL);
166 ctrl &= ~SPDIF_OPMODE_MASK;
167 if (!host->saved_params.mute)
168 ctrl |= SPDIF_OPMODE_AUD_DATA |
169 SPDIF_STATE_NORMAL;
170 else
171 ctrl |= SPDIF_OPMODE_MUTE_PCM;
172 writel(ctrl, host->io_base + SPDIF_OUT_CTRL);
173 break;
174
175 case SNDRV_PCM_TRIGGER_STOP:
176 case SNDRV_PCM_TRIGGER_SUSPEND:
177 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
178 ctrl = readl(host->io_base + SPDIF_OUT_CTRL);
179 ctrl &= ~SPDIF_OPMODE_MASK;
180 ctrl |= SPDIF_OPMODE_OFF;
181 writel(ctrl, host->io_base + SPDIF_OUT_CTRL);
182 break;
183
184 default:
185 ret = -EINVAL;
186 break;
187 }
188 return ret;
189}
190
191static int spdif_digital_mute(struct snd_soc_dai *dai, int mute)
192{
193 struct spdif_out_dev *host = snd_soc_dai_get_drvdata(dai);
194 u32 val;
195
196 host->saved_params.mute = mute;
197 val = readl(host->io_base + SPDIF_OUT_CTRL);
198 val &= ~SPDIF_OPMODE_MASK;
199
200 if (mute)
201 val |= SPDIF_OPMODE_MUTE_PCM;
202 else {
203 if (host->running)
204 val |= SPDIF_OPMODE_AUD_DATA | SPDIF_STATE_NORMAL;
205 else
206 val |= SPDIF_OPMODE_OFF;
207 }
208
209 writel(val, host->io_base + SPDIF_OUT_CTRL);
210 return 0;
211}
212
213static int spdif_mute_get(struct snd_kcontrol *kcontrol,
214 struct snd_ctl_elem_value *ucontrol)
215{
Lars-Peter Clausenbde24032014-03-03 08:57:49 +0100216 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
Vipin Kumare584f9b2012-07-04 16:11:13 +0530217 struct spdif_out_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
218
219 ucontrol->value.integer.value[0] = host->saved_params.mute;
220 return 0;
221}
222
223static int spdif_mute_put(struct snd_kcontrol *kcontrol,
224 struct snd_ctl_elem_value *ucontrol)
225{
Lars-Peter Clausenbde24032014-03-03 08:57:49 +0100226 struct snd_soc_dai *cpu_dai = snd_kcontrol_chip(kcontrol);
Vipin Kumare584f9b2012-07-04 16:11:13 +0530227 struct spdif_out_dev *host = snd_soc_dai_get_drvdata(cpu_dai);
228
229 if (host->saved_params.mute == ucontrol->value.integer.value[0])
230 return 0;
231
232 spdif_digital_mute(cpu_dai, ucontrol->value.integer.value[0]);
233
234 return 1;
235}
236static const struct snd_kcontrol_new spdif_out_controls[] = {
237 SOC_SINGLE_BOOL_EXT("IEC958 Playback Switch", 0,
238 spdif_mute_get, spdif_mute_put),
239};
240
Lars-Peter Clausen19c7efc2013-05-14 22:19:50 +0200241static int spdif_soc_dai_probe(struct snd_soc_dai *dai)
Vipin Kumare584f9b2012-07-04 16:11:13 +0530242{
Lars-Peter Clausen46fdd8b2013-04-20 19:29:06 +0200243 struct spdif_out_dev *host = snd_soc_dai_get_drvdata(dai);
244
Stephen Warrene1771bc2013-12-10 12:35:25 -0700245 host->dma_params_tx.filter_data = &host->dma_params;
246 dai->playback_dma_data = &host->dma_params_tx;
Lars-Peter Clausen46fdd8b2013-04-20 19:29:06 +0200247
Vipin Kumare584f9b2012-07-04 16:11:13 +0530248 return snd_soc_add_dai_controls(dai, spdif_out_controls,
249 ARRAY_SIZE(spdif_out_controls));
250}
251
252static const struct snd_soc_dai_ops spdif_out_dai_ops = {
253 .digital_mute = spdif_digital_mute,
254 .startup = spdif_out_startup,
255 .shutdown = spdif_out_shutdown,
256 .trigger = spdif_out_trigger,
257 .hw_params = spdif_out_hw_params,
258};
259
260static struct snd_soc_dai_driver spdif_out_dai = {
261 .playback = {
262 .channels_min = 2,
263 .channels_max = 2,
264 .rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
265 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | \
266 SNDRV_PCM_RATE_192000),
267 .formats = SNDRV_PCM_FMTBIT_S16_LE,
268 },
269 .probe = spdif_soc_dai_probe,
270 .ops = &spdif_out_dai_ops,
271};
272
Kuninori Morimotoa582d442013-03-21 03:37:00 -0700273static const struct snd_soc_component_driver spdif_out_component = {
274 .name = "spdif-out",
275};
276
Vipin Kumare584f9b2012-07-04 16:11:13 +0530277static int spdif_out_probe(struct platform_device *pdev)
278{
279 struct spdif_out_dev *host;
280 struct spear_spdif_platform_data *pdata;
281 struct resource *res;
Stephen Warrenede38882013-12-10 12:35:24 -0700282 int ret;
Vipin Kumare584f9b2012-07-04 16:11:13 +0530283
Vipin Kumare584f9b2012-07-04 16:11:13 +0530284 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
285 if (!host) {
286 dev_warn(&pdev->dev, "kzalloc fail\n");
287 return -ENOMEM;
288 }
289
Tushar Beherafc09cfb2013-06-17 16:10:57 +0530290 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
291 host->io_base = devm_ioremap_resource(&pdev->dev, res);
292 if (IS_ERR(host->io_base))
293 return PTR_ERR(host->io_base);
Vipin Kumare584f9b2012-07-04 16:11:13 +0530294
Uwe Kleine-Königbfcc74e2013-05-08 11:47:38 +0200295 host->clk = devm_clk_get(&pdev->dev, NULL);
Vipin Kumare584f9b2012-07-04 16:11:13 +0530296 if (IS_ERR(host->clk))
297 return PTR_ERR(host->clk);
298
299 pdata = dev_get_platdata(&pdev->dev);
300
301 host->dma_params.data = pdata->dma_params;
302 host->dma_params.addr = res->start + SPDIF_OUT_FIFO_DATA;
303 host->dma_params.max_burst = 16;
304 host->dma_params.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
Vipin Kumare584f9b2012-07-04 16:11:13 +0530305
306 dev_set_drvdata(&pdev->dev, host);
307
Stephen Warrenede38882013-12-10 12:35:24 -0700308 ret = devm_snd_soc_register_component(&pdev->dev, &spdif_out_component,
309 &spdif_out_dai, 1);
310 if (ret)
311 return ret;
312
Stephen Warrene1771bc2013-12-10 12:35:25 -0700313 return devm_spear_pcm_platform_register(&pdev->dev, &host->config,
314 pdata->filter);
Vipin Kumare584f9b2012-07-04 16:11:13 +0530315}
316
Vipin Kumare584f9b2012-07-04 16:11:13 +0530317#ifdef CONFIG_PM
318static int spdif_out_suspend(struct device *dev)
319{
320 struct platform_device *pdev = to_platform_device(dev);
321 struct spdif_out_dev *host = dev_get_drvdata(&pdev->dev);
322
323 if (host->running)
324 clk_disable(host->clk);
325
326 return 0;
327}
328
329static int spdif_out_resume(struct device *dev)
330{
331 struct platform_device *pdev = to_platform_device(dev);
332 struct spdif_out_dev *host = dev_get_drvdata(&pdev->dev);
333
334 if (host->running) {
335 clk_enable(host->clk);
336 spdif_out_configure(host);
337 spdif_out_clock(host, host->saved_params.core_freq,
338 host->saved_params.rate);
339 }
340 return 0;
341}
342
343static SIMPLE_DEV_PM_OPS(spdif_out_dev_pm_ops, spdif_out_suspend, \
344 spdif_out_resume);
345
346#define SPDIF_OUT_DEV_PM_OPS (&spdif_out_dev_pm_ops)
347
348#else
349#define SPDIF_OUT_DEV_PM_OPS NULL
350
351#endif
352
353static struct platform_driver spdif_out_driver = {
354 .probe = spdif_out_probe,
Vipin Kumare584f9b2012-07-04 16:11:13 +0530355 .driver = {
356 .name = "spdif-out",
Vipin Kumare584f9b2012-07-04 16:11:13 +0530357 .pm = SPDIF_OUT_DEV_PM_OPS,
358 },
359};
360
361module_platform_driver(spdif_out_driver);
362
363MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>");
364MODULE_DESCRIPTION("SPEAr SPDIF OUT SoC Interface");
365MODULE_LICENSE("GPL");
366MODULE_ALIAS("platform:spdif_out");