2 * tegra210_ope_alt.c - Tegra210 OPE driver
4 * Copyright (c) 2014-2017, NVIDIA CORPORATION. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <linux/clk.h>
20 #include <linux/device.h>
22 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/regmap.h>
27 #include <linux/slab.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include <linux/pinctrl/consumer.h>
33 #include <linux/of_device.h>
35 #include "tegra210_xbar_alt.h"
36 #include "tegra210_ope_alt.h"
38 #define DRV_NAME "tegra210-ope"
40 static const struct reg_default tegra210_ope_reg_defaults[] = {
41 { TEGRA210_OPE_AXBAR_RX_INT_MASK, 0x00000001},
42 { TEGRA210_OPE_AXBAR_RX_CIF_CTRL, 0x00007700},
43 { TEGRA210_OPE_AXBAR_TX_INT_MASK, 0x00000001},
44 { TEGRA210_OPE_AXBAR_TX_CIF_CTRL, 0x00007700},
45 { TEGRA210_OPE_CG, 0x1},
48 static int tegra210_ope_runtime_suspend(struct device *dev)
50 struct tegra210_ope *ope = dev_get_drvdata(dev);
52 regcache_cache_only(ope->mbdrc_regmap, true);
53 regcache_cache_only(ope->peq_regmap, true);
54 regcache_cache_only(ope->regmap, true);
55 regcache_mark_dirty(ope->regmap);
56 regcache_mark_dirty(ope->peq_regmap);
57 regcache_mark_dirty(ope->mbdrc_regmap);
62 static int tegra210_ope_runtime_resume(struct device *dev)
64 struct tegra210_ope *ope = dev_get_drvdata(dev);
66 regcache_cache_only(ope->regmap, false);
67 regcache_cache_only(ope->peq_regmap, false);
68 regcache_cache_only(ope->mbdrc_regmap, false);
70 if (!ope->is_shutdown) {
71 regcache_sync(ope->regmap);
72 regcache_sync(ope->peq_regmap);
73 regcache_sync(ope->mbdrc_regmap);
79 #ifdef CONFIG_PM_SLEEP
80 static int tegra210_ope_suspend(struct device *dev)
82 if (pm_runtime_status_suspended(dev))
85 return tegra210_ope_runtime_suspend(dev);
88 static int tegra210_ope_resume(struct device *dev)
90 if (pm_runtime_status_suspended(dev))
93 return tegra210_ope_runtime_resume(dev);
97 static int tegra210_ope_set_audio_cif(struct tegra210_ope *ope,
98 struct snd_pcm_hw_params *params,
101 int channels, audio_bits;
102 struct tegra210_xbar_cif_conf cif_conf;
104 memset(&cif_conf, 0, sizeof(struct tegra210_xbar_cif_conf));
106 channels = params_channels(params);
110 switch (params_format(params)) {
111 case SNDRV_PCM_FORMAT_S16_LE:
112 audio_bits = TEGRA210_AUDIOCIF_BITS_16;
114 case SNDRV_PCM_FORMAT_S32_LE:
115 audio_bits = TEGRA210_AUDIOCIF_BITS_32;
121 cif_conf.audio_channels = channels;
122 cif_conf.client_channels = channels;
123 cif_conf.audio_bits = audio_bits;
124 cif_conf.client_bits = audio_bits;
126 ope->soc_data->set_audio_cif(ope->regmap, reg, &cif_conf);
131 static int tegra210_ope_hw_params(struct snd_pcm_substream *substream,
132 struct snd_pcm_hw_params *params,
133 struct snd_soc_dai *dai)
135 struct device *dev = dai->dev;
136 struct tegra210_ope *ope = snd_soc_dai_get_drvdata(dai);
139 /* set RX cif and TX cif */
140 ret = tegra210_ope_set_audio_cif(ope, params,
141 TEGRA210_OPE_AXBAR_RX_CIF_CTRL);
143 dev_err(dev, "Can't set OPE RX CIF: %d\n", ret);
147 ret = tegra210_ope_set_audio_cif(ope, params,
148 TEGRA210_OPE_AXBAR_TX_CIF_CTRL);
150 dev_err(dev, "Can't set OPE TX CIF: %d\n", ret);
157 static int tegra210_ope_codec_probe(struct snd_soc_codec *codec)
159 struct tegra210_ope *ope = snd_soc_codec_get_drvdata(codec);
161 codec->control_data = ope->regmap;
163 ope->soc_data->peq_soc_data.codec_init(codec);
164 ope->soc_data->mbdrc_soc_data.codec_init(codec);
169 static struct regmap *tegra210_ope_init_regmap(struct device *dev)
171 struct tegra210_ope *ope = dev_get_drvdata(dev);
176 static struct snd_soc_dai_ops tegra210_ope_dai_ops = {
177 .hw_params = tegra210_ope_hw_params,
180 static struct snd_soc_dai_driver tegra210_ope_dais[] = {
184 .stream_name = "OPE Receive",
187 .rates = SNDRV_PCM_RATE_8000_192000,
188 .formats = SNDRV_PCM_FMTBIT_S8 |
189 SNDRV_PCM_FMTBIT_S16_LE |
190 SNDRV_PCM_FMTBIT_S24_LE |
191 SNDRV_PCM_FMTBIT_S32_LE,
197 .stream_name = "OPE Transmit",
200 .rates = SNDRV_PCM_RATE_8000_192000,
201 .formats = SNDRV_PCM_FMTBIT_S8 |
202 SNDRV_PCM_FMTBIT_S16_LE |
203 SNDRV_PCM_FMTBIT_S24_LE |
204 SNDRV_PCM_FMTBIT_S32_LE,
206 .ops = &tegra210_ope_dai_ops,
210 static const struct snd_soc_dapm_widget tegra210_ope_widgets[] = {
211 SND_SOC_DAPM_AIF_IN("OPE RX", NULL, 0, SND_SOC_NOPM,
213 SND_SOC_DAPM_AIF_OUT("OPE TX", NULL, 0, TEGRA210_OPE_ENABLE,
214 TEGRA210_OPE_EN_SHIFT, 0),
217 static const struct snd_soc_dapm_route tegra210_ope_routes[] = {
218 { "OPE RX", NULL, "OPE Receive" },
219 { "OPE TX", NULL, "OPE RX" },
220 { "OPE Transmit", NULL, "OPE TX" },
223 static const struct snd_kcontrol_new tegra210_ope_controls[] = {
224 SOC_SINGLE("direction peq to mbdrc", TEGRA210_OPE_DIRECTION,
225 TEGRA210_OPE_DIRECTION_SHIFT, 1, 0),
228 static struct snd_soc_codec_driver tegra210_ope_codec = {
229 .probe = tegra210_ope_codec_probe,
230 .dapm_widgets = tegra210_ope_widgets,
231 .num_dapm_widgets = ARRAY_SIZE(tegra210_ope_widgets),
232 .dapm_routes = tegra210_ope_routes,
233 .num_dapm_routes = ARRAY_SIZE(tegra210_ope_routes),
234 .controls = tegra210_ope_controls,
235 .num_controls = ARRAY_SIZE(tegra210_ope_controls),
237 .get_regmap = tegra210_ope_init_regmap,
240 static bool tegra210_ope_wr_reg(struct device *dev, unsigned int reg)
243 case TEGRA210_OPE_AXBAR_RX_INT_MASK:
244 case TEGRA210_OPE_AXBAR_RX_INT_SET:
245 case TEGRA210_OPE_AXBAR_RX_INT_CLEAR:
246 case TEGRA210_OPE_AXBAR_RX_CIF_CTRL:
248 case TEGRA210_OPE_AXBAR_TX_INT_MASK:
249 case TEGRA210_OPE_AXBAR_TX_INT_SET:
250 case TEGRA210_OPE_AXBAR_TX_INT_CLEAR:
251 case TEGRA210_OPE_AXBAR_TX_CIF_CTRL:
253 case TEGRA210_OPE_ENABLE:
254 case TEGRA210_OPE_SOFT_RESET:
255 case TEGRA210_OPE_CG:
256 case TEGRA210_OPE_DIRECTION:
263 static bool tegra210_ope_rd_reg(struct device *dev, unsigned int reg)
266 case TEGRA210_OPE_AXBAR_RX_STATUS:
267 case TEGRA210_OPE_AXBAR_RX_INT_STATUS:
268 case TEGRA210_OPE_AXBAR_RX_INT_MASK:
269 case TEGRA210_OPE_AXBAR_RX_INT_SET:
270 case TEGRA210_OPE_AXBAR_RX_INT_CLEAR:
271 case TEGRA210_OPE_AXBAR_RX_CIF_CTRL:
273 case TEGRA210_OPE_AXBAR_TX_STATUS:
274 case TEGRA210_OPE_AXBAR_TX_INT_STATUS:
275 case TEGRA210_OPE_AXBAR_TX_INT_MASK:
276 case TEGRA210_OPE_AXBAR_TX_INT_SET:
277 case TEGRA210_OPE_AXBAR_TX_INT_CLEAR:
278 case TEGRA210_OPE_AXBAR_TX_CIF_CTRL:
280 case TEGRA210_OPE_ENABLE:
281 case TEGRA210_OPE_SOFT_RESET:
282 case TEGRA210_OPE_CG:
283 case TEGRA210_OPE_STATUS:
284 case TEGRA210_OPE_INT_STATUS:
285 case TEGRA210_OPE_DIRECTION:
292 static bool tegra210_ope_volatile_reg(struct device *dev, unsigned int reg)
295 case TEGRA210_OPE_AXBAR_RX_STATUS:
296 case TEGRA210_OPE_AXBAR_RX_INT_SET:
297 case TEGRA210_OPE_AXBAR_RX_INT_STATUS:
299 case TEGRA210_OPE_AXBAR_TX_STATUS:
300 case TEGRA210_OPE_AXBAR_TX_INT_SET:
301 case TEGRA210_OPE_AXBAR_TX_INT_STATUS:
303 case TEGRA210_OPE_SOFT_RESET:
304 case TEGRA210_OPE_STATUS:
305 case TEGRA210_OPE_INT_STATUS:
312 static const struct regmap_config tegra210_ope_regmap_config = {
316 .max_register = TEGRA210_OPE_DIRECTION,
317 .writeable_reg = tegra210_ope_wr_reg,
318 .readable_reg = tegra210_ope_rd_reg,
319 .volatile_reg = tegra210_ope_volatile_reg,
320 .reg_defaults = tegra210_ope_reg_defaults,
321 .num_reg_defaults = ARRAY_SIZE(tegra210_ope_reg_defaults),
322 .cache_type = REGCACHE_FLAT,
325 static const struct tegra210_ope_soc_data soc_data_tegra210 = {
326 .set_audio_cif = tegra210_xbar_set_cif,
328 .init = tegra210_peq_init,
329 .codec_init = tegra210_peq_codec_init,
332 .init = tegra210_mbdrc_init,
333 .codec_init = tegra210_mbdrc_codec_init,
337 static const struct of_device_id tegra210_ope_of_match[] = {
338 { .compatible = "nvidia,tegra210-ope", .data = &soc_data_tegra210 },
342 static int tegra210_ope_platform_probe(struct platform_device *pdev)
344 struct tegra210_ope *ope;
345 struct resource *mem, *memregion;
348 const struct of_device_id *match;
349 struct tegra210_ope_soc_data *soc_data;
351 pr_info("OPE platform probe\n");
353 match = of_match_device(tegra210_ope_of_match, &pdev->dev);
355 dev_err(&pdev->dev, "Error: No device match found\n");
359 soc_data = (struct tegra210_ope_soc_data *)match->data;
361 ope = devm_kzalloc(&pdev->dev, sizeof(struct tegra210_ope), GFP_KERNEL);
363 dev_err(&pdev->dev, "Can't allocate ope\n");
368 ope->soc_data = soc_data;
369 ope->is_shutdown = false;
371 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
373 dev_err(&pdev->dev, "No memory resource\n");
378 memregion = devm_request_mem_region(&pdev->dev, mem->start,
379 resource_size(mem), pdev->name);
381 dev_err(&pdev->dev, "Memory region already claimed\n");
386 regs = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
388 dev_err(&pdev->dev, "ioremap failed\n");
393 ope->regmap = devm_regmap_init_mmio(&pdev->dev, regs,
394 &tegra210_ope_regmap_config);
395 if (IS_ERR(ope->regmap)) {
396 dev_err(&pdev->dev, "regmap init failed\n");
397 ret = PTR_ERR(ope->regmap);
400 regcache_cache_only(ope->regmap, true);
402 dev_set_drvdata(&pdev->dev, ope);
404 ret = ope->soc_data->peq_soc_data.init(pdev,
405 TEGRA210_PEQ_IORESOURCE_MEM);
407 dev_err(&pdev->dev, "peq init failed\n");
410 regcache_cache_only(ope->peq_regmap, true);
412 ret = ope->soc_data->mbdrc_soc_data.init(pdev,
413 TEGRA210_MBDRC_IORESOURCE_MEM);
415 dev_err(&pdev->dev, "mbdrc init failed\n");
418 regcache_cache_only(ope->mbdrc_regmap, true);
420 if (of_property_read_u32(pdev->dev.of_node,
421 "nvidia,ahub-ope-id",
422 &pdev->dev.id) < 0) {
424 "Missing property nvidia,ahub-ope-id\n");
429 pm_runtime_enable(&pdev->dev);
430 if (!pm_runtime_enabled(&pdev->dev)) {
431 ret = tegra210_ope_runtime_resume(&pdev->dev);
436 ret = snd_soc_register_codec(&pdev->dev, &tegra210_ope_codec,
438 ARRAY_SIZE(tegra210_ope_dais));
440 dev_err(&pdev->dev, "Could not register CODEC: %d\n", ret);
444 pr_info("OPE platform probe successful\n");
449 if (!pm_runtime_status_suspended(&pdev->dev))
450 tegra210_ope_runtime_suspend(&pdev->dev);
452 pm_runtime_disable(&pdev->dev);
457 static void tegra210_ope_platform_shutdown(struct platform_device *pdev)
459 struct tegra210_ope *ope = dev_get_drvdata(&pdev->dev);
461 ope->is_shutdown = true;
464 static int tegra210_ope_platform_remove(struct platform_device *pdev)
466 snd_soc_unregister_codec(&pdev->dev);
468 pm_runtime_disable(&pdev->dev);
469 if (!pm_runtime_status_suspended(&pdev->dev))
470 tegra210_ope_runtime_suspend(&pdev->dev);
475 static const struct dev_pm_ops tegra210_ope_pm_ops = {
476 SET_RUNTIME_PM_OPS(tegra210_ope_runtime_suspend,
477 tegra210_ope_runtime_resume, NULL)
478 SET_LATE_SYSTEM_SLEEP_PM_OPS(tegra210_ope_suspend, tegra210_ope_resume)
481 static struct platform_driver tegra210_ope_driver = {
484 .owner = THIS_MODULE,
485 .of_match_table = tegra210_ope_of_match,
486 .pm = &tegra210_ope_pm_ops,
488 .probe = tegra210_ope_platform_probe,
489 .remove = tegra210_ope_platform_remove,
490 .shutdown = tegra210_ope_platform_shutdown,
492 module_platform_driver(tegra210_ope_driver)
494 MODULE_AUTHOR("Sumit Bhattacharya <sumitb@nvidia.com>");
495 MODULE_DESCRIPTION("Tegra210 OPE ASoC driver");
496 MODULE_LICENSE("GPL");
497 MODULE_ALIAS("platform:" DRV_NAME);
498 MODULE_DEVICE_TABLE(of, tegra210_ope_of_match);