sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / sound / soc / tegra / tegra20_spdif.c
bloba0c3640572b98a80a48601093d3848d2341a8509
1 /*
2 * tegra20_spdif.c - Tegra20 SPDIF driver
4 * Author: Stephen Warren <swarren@nvidia.com>
5 * Copyright (C) 2011-2012 - NVIDIA, Inc.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19 * 02110-1301 USA
23 #include <linux/clk.h>
24 #include <linux/device.h>
25 #include <linux/io.h>
26 #include <linux/module.h>
27 #include <linux/platform_device.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/regmap.h>
30 #include <linux/slab.h>
31 #include <sound/core.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/soc.h>
35 #include <sound/dmaengine_pcm.h>
37 #include "tegra20_spdif.h"
39 #define DRV_NAME "tegra20-spdif"
41 static int tegra20_spdif_runtime_suspend(struct device *dev)
43 struct tegra20_spdif *spdif = dev_get_drvdata(dev);
45 clk_disable_unprepare(spdif->clk_spdif_out);
47 return 0;
50 static int tegra20_spdif_runtime_resume(struct device *dev)
52 struct tegra20_spdif *spdif = dev_get_drvdata(dev);
53 int ret;
55 ret = clk_prepare_enable(spdif->clk_spdif_out);
56 if (ret) {
57 dev_err(dev, "clk_enable failed: %d\n", ret);
58 return ret;
61 return 0;
64 static int tegra20_spdif_hw_params(struct snd_pcm_substream *substream,
65 struct snd_pcm_hw_params *params,
66 struct snd_soc_dai *dai)
68 struct device *dev = dai->dev;
69 struct tegra20_spdif *spdif = snd_soc_dai_get_drvdata(dai);
70 unsigned int mask = 0, val = 0;
71 int ret, spdifclock;
73 mask |= TEGRA20_SPDIF_CTRL_PACK |
74 TEGRA20_SPDIF_CTRL_BIT_MODE_MASK;
75 switch (params_format(params)) {
76 case SNDRV_PCM_FORMAT_S16_LE:
77 val |= TEGRA20_SPDIF_CTRL_PACK |
78 TEGRA20_SPDIF_CTRL_BIT_MODE_16BIT;
79 break;
80 default:
81 return -EINVAL;
84 regmap_update_bits(spdif->regmap, TEGRA20_SPDIF_CTRL, mask, val);
86 switch (params_rate(params)) {
87 case 32000:
88 spdifclock = 4096000;
89 break;
90 case 44100:
91 spdifclock = 5644800;
92 break;
93 case 48000:
94 spdifclock = 6144000;
95 break;
96 case 88200:
97 spdifclock = 11289600;
98 break;
99 case 96000:
100 spdifclock = 12288000;
101 break;
102 case 176400:
103 spdifclock = 22579200;
104 break;
105 case 192000:
106 spdifclock = 24576000;
107 break;
108 default:
109 return -EINVAL;
112 ret = clk_set_rate(spdif->clk_spdif_out, spdifclock);
113 if (ret) {
114 dev_err(dev, "Can't set SPDIF clock rate: %d\n", ret);
115 return ret;
118 return 0;
121 static void tegra20_spdif_start_playback(struct tegra20_spdif *spdif)
123 regmap_update_bits(spdif->regmap, TEGRA20_SPDIF_CTRL,
124 TEGRA20_SPDIF_CTRL_TX_EN,
125 TEGRA20_SPDIF_CTRL_TX_EN);
128 static void tegra20_spdif_stop_playback(struct tegra20_spdif *spdif)
130 regmap_update_bits(spdif->regmap, TEGRA20_SPDIF_CTRL,
131 TEGRA20_SPDIF_CTRL_TX_EN, 0);
134 static int tegra20_spdif_trigger(struct snd_pcm_substream *substream, int cmd,
135 struct snd_soc_dai *dai)
137 struct tegra20_spdif *spdif = snd_soc_dai_get_drvdata(dai);
139 switch (cmd) {
140 case SNDRV_PCM_TRIGGER_START:
141 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
142 case SNDRV_PCM_TRIGGER_RESUME:
143 tegra20_spdif_start_playback(spdif);
144 break;
145 case SNDRV_PCM_TRIGGER_STOP:
146 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
147 case SNDRV_PCM_TRIGGER_SUSPEND:
148 tegra20_spdif_stop_playback(spdif);
149 break;
150 default:
151 return -EINVAL;
154 return 0;
157 static int tegra20_spdif_probe(struct snd_soc_dai *dai)
159 struct tegra20_spdif *spdif = snd_soc_dai_get_drvdata(dai);
161 dai->capture_dma_data = NULL;
162 dai->playback_dma_data = &spdif->playback_dma_data;
164 return 0;
167 static const struct snd_soc_dai_ops tegra20_spdif_dai_ops = {
168 .hw_params = tegra20_spdif_hw_params,
169 .trigger = tegra20_spdif_trigger,
172 static struct snd_soc_dai_driver tegra20_spdif_dai = {
173 .name = DRV_NAME,
174 .probe = tegra20_spdif_probe,
175 .playback = {
176 .stream_name = "Playback",
177 .channels_min = 2,
178 .channels_max = 2,
179 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
180 SNDRV_PCM_RATE_48000,
181 .formats = SNDRV_PCM_FMTBIT_S16_LE,
183 .ops = &tegra20_spdif_dai_ops,
186 static const struct snd_soc_component_driver tegra20_spdif_component = {
187 .name = DRV_NAME,
190 static bool tegra20_spdif_wr_rd_reg(struct device *dev, unsigned int reg)
192 switch (reg) {
193 case TEGRA20_SPDIF_CTRL:
194 case TEGRA20_SPDIF_STATUS:
195 case TEGRA20_SPDIF_STROBE_CTRL:
196 case TEGRA20_SPDIF_DATA_FIFO_CSR:
197 case TEGRA20_SPDIF_DATA_OUT:
198 case TEGRA20_SPDIF_DATA_IN:
199 case TEGRA20_SPDIF_CH_STA_RX_A:
200 case TEGRA20_SPDIF_CH_STA_RX_B:
201 case TEGRA20_SPDIF_CH_STA_RX_C:
202 case TEGRA20_SPDIF_CH_STA_RX_D:
203 case TEGRA20_SPDIF_CH_STA_RX_E:
204 case TEGRA20_SPDIF_CH_STA_RX_F:
205 case TEGRA20_SPDIF_CH_STA_TX_A:
206 case TEGRA20_SPDIF_CH_STA_TX_B:
207 case TEGRA20_SPDIF_CH_STA_TX_C:
208 case TEGRA20_SPDIF_CH_STA_TX_D:
209 case TEGRA20_SPDIF_CH_STA_TX_E:
210 case TEGRA20_SPDIF_CH_STA_TX_F:
211 case TEGRA20_SPDIF_USR_STA_RX_A:
212 case TEGRA20_SPDIF_USR_DAT_TX_A:
213 return true;
214 default:
215 return false;
219 static bool tegra20_spdif_volatile_reg(struct device *dev, unsigned int reg)
221 switch (reg) {
222 case TEGRA20_SPDIF_STATUS:
223 case TEGRA20_SPDIF_DATA_FIFO_CSR:
224 case TEGRA20_SPDIF_DATA_OUT:
225 case TEGRA20_SPDIF_DATA_IN:
226 case TEGRA20_SPDIF_CH_STA_RX_A:
227 case TEGRA20_SPDIF_CH_STA_RX_B:
228 case TEGRA20_SPDIF_CH_STA_RX_C:
229 case TEGRA20_SPDIF_CH_STA_RX_D:
230 case TEGRA20_SPDIF_CH_STA_RX_E:
231 case TEGRA20_SPDIF_CH_STA_RX_F:
232 case TEGRA20_SPDIF_USR_STA_RX_A:
233 case TEGRA20_SPDIF_USR_DAT_TX_A:
234 return true;
235 default:
236 return false;
240 static bool tegra20_spdif_precious_reg(struct device *dev, unsigned int reg)
242 switch (reg) {
243 case TEGRA20_SPDIF_DATA_OUT:
244 case TEGRA20_SPDIF_DATA_IN:
245 case TEGRA20_SPDIF_USR_STA_RX_A:
246 case TEGRA20_SPDIF_USR_DAT_TX_A:
247 return true;
248 default:
249 return false;
253 static const struct regmap_config tegra20_spdif_regmap_config = {
254 .reg_bits = 32,
255 .reg_stride = 4,
256 .val_bits = 32,
257 .max_register = TEGRA20_SPDIF_USR_DAT_TX_A,
258 .writeable_reg = tegra20_spdif_wr_rd_reg,
259 .readable_reg = tegra20_spdif_wr_rd_reg,
260 .volatile_reg = tegra20_spdif_volatile_reg,
261 .precious_reg = tegra20_spdif_precious_reg,
262 .cache_type = REGCACHE_FLAT,
265 static int tegra20_spdif_platform_probe(struct platform_device *pdev)
267 struct tegra20_spdif *spdif;
268 struct resource *mem, *dmareq;
269 void __iomem *regs;
270 int ret;
272 spdif = devm_kzalloc(&pdev->dev, sizeof(struct tegra20_spdif),
273 GFP_KERNEL);
274 if (!spdif) {
275 dev_err(&pdev->dev, "Can't allocate tegra20_spdif\n");
276 return -ENOMEM;
278 dev_set_drvdata(&pdev->dev, spdif);
280 spdif->clk_spdif_out = devm_clk_get(&pdev->dev, "spdif_out");
281 if (IS_ERR(spdif->clk_spdif_out)) {
282 pr_err("Can't retrieve spdif clock\n");
283 ret = PTR_ERR(spdif->clk_spdif_out);
284 return ret;
287 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
288 regs = devm_ioremap_resource(&pdev->dev, mem);
289 if (IS_ERR(regs))
290 return PTR_ERR(regs);
292 dmareq = platform_get_resource(pdev, IORESOURCE_DMA, 0);
293 if (!dmareq) {
294 dev_err(&pdev->dev, "No DMA resource\n");
295 return -ENODEV;
298 spdif->regmap = devm_regmap_init_mmio(&pdev->dev, regs,
299 &tegra20_spdif_regmap_config);
300 if (IS_ERR(spdif->regmap)) {
301 dev_err(&pdev->dev, "regmap init failed\n");
302 ret = PTR_ERR(spdif->regmap);
303 return ret;
306 spdif->playback_dma_data.addr = mem->start + TEGRA20_SPDIF_DATA_OUT;
307 spdif->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
308 spdif->playback_dma_data.maxburst = 4;
309 spdif->playback_dma_data.slave_id = dmareq->start;
311 pm_runtime_enable(&pdev->dev);
312 if (!pm_runtime_enabled(&pdev->dev)) {
313 ret = tegra20_spdif_runtime_resume(&pdev->dev);
314 if (ret)
315 goto err_pm_disable;
318 ret = snd_soc_register_component(&pdev->dev, &tegra20_spdif_component,
319 &tegra20_spdif_dai, 1);
320 if (ret) {
321 dev_err(&pdev->dev, "Could not register DAI: %d\n", ret);
322 ret = -ENOMEM;
323 goto err_suspend;
326 ret = tegra_pcm_platform_register(&pdev->dev);
327 if (ret) {
328 dev_err(&pdev->dev, "Could not register PCM: %d\n", ret);
329 goto err_unregister_component;
332 return 0;
334 err_unregister_component:
335 snd_soc_unregister_component(&pdev->dev);
336 err_suspend:
337 if (!pm_runtime_status_suspended(&pdev->dev))
338 tegra20_spdif_runtime_suspend(&pdev->dev);
339 err_pm_disable:
340 pm_runtime_disable(&pdev->dev);
342 return ret;
345 static int tegra20_spdif_platform_remove(struct platform_device *pdev)
347 pm_runtime_disable(&pdev->dev);
348 if (!pm_runtime_status_suspended(&pdev->dev))
349 tegra20_spdif_runtime_suspend(&pdev->dev);
351 tegra_pcm_platform_unregister(&pdev->dev);
352 snd_soc_unregister_component(&pdev->dev);
354 return 0;
357 static const struct dev_pm_ops tegra20_spdif_pm_ops = {
358 SET_RUNTIME_PM_OPS(tegra20_spdif_runtime_suspend,
359 tegra20_spdif_runtime_resume, NULL)
362 static struct platform_driver tegra20_spdif_driver = {
363 .driver = {
364 .name = DRV_NAME,
365 .pm = &tegra20_spdif_pm_ops,
367 .probe = tegra20_spdif_platform_probe,
368 .remove = tegra20_spdif_platform_remove,
371 module_platform_driver(tegra20_spdif_driver);
373 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
374 MODULE_DESCRIPTION("Tegra20 SPDIF ASoC driver");
375 MODULE_LICENSE("GPL");
376 MODULE_ALIAS("platform:" DRV_NAME);