1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * trimslice.c - TrimSlice machine ASoC driver
4 *
5 * Copyright (C) 2011 - CompuLab, Ltd.
6 * Author: Mike Rapoport <mike@compulab.co.il>
7 *
8 * Based on code copyright/by:
9 * Author: Stephen Warren <swarren@nvidia.com>
10 * Copyright (C) 2010-2011 - NVIDIA, Inc.
11 */
12
13#include <linux/module.h>
14#include <linux/of.h>
15#include <linux/platform_device.h>
16#include <linux/slab.h>
17
18#include <sound/core.h>
19#include <sound/jack.h>
20#include <sound/pcm.h>
21#include <sound/pcm_params.h>
22#include <sound/soc.h>
23
24#include "../codecs/tlv320aic23.h"
25
26#include "tegra_asoc_utils.h"
27
28#define DRV_NAME "tegra-snd-trimslice"
29
30struct tegra_trimslice {
31	struct tegra_asoc_utils_data util_data;
32};
33
34static int trimslice_asoc_hw_params(struct snd_pcm_substream *substream,
35					struct snd_pcm_hw_params *params)
36{
37	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
38	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
39	struct snd_soc_card *card = rtd->card;
40	struct tegra_trimslice *trimslice = snd_soc_card_get_drvdata(card);
41	int srate, mclk;
42	int err;
43
44	srate = params_rate(params);
45	mclk = 128 * srate;
46
47	err = tegra_asoc_utils_set_rate(&trimslice->util_data, srate, mclk);
48	if (err < 0) {
49		dev_err(card->dev, "Can't configure clocks\n");
50		return err;
51	}
52
53	err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
54					SND_SOC_CLOCK_IN);
55	if (err < 0) {
56		dev_err(card->dev, "codec_dai clock not set\n");
57		return err;
58	}
59
60	return 0;
61}
62
63static const struct snd_soc_ops trimslice_asoc_ops = {
64	.hw_params = trimslice_asoc_hw_params,
65};
66
67static const struct snd_soc_dapm_widget trimslice_dapm_widgets[] = {
68	SND_SOC_DAPM_HP("Line Out", NULL),
69	SND_SOC_DAPM_LINE("Line In", NULL),
70};
71
72static const struct snd_soc_dapm_route trimslice_audio_map[] = {
73	{"Line Out", NULL, "LOUT"},
74	{"Line Out", NULL, "ROUT"},
75
76	{"LLINEIN", NULL, "Line In"},
77	{"RLINEIN", NULL, "Line In"},
78};
79
80SND_SOC_DAILINK_DEFS(single_dsp,
81	DAILINK_COMP_ARRAY(COMP_EMPTY()),
82	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "tlv320aic23-hifi")),
83	DAILINK_COMP_ARRAY(COMP_EMPTY()));
84
85static struct snd_soc_dai_link trimslice_tlv320aic23_dai = {
86	.name = "TLV320AIC23",
87	.stream_name = "AIC23",
88	.ops = &trimslice_asoc_ops,
89	.dai_fmt = SND_SOC_DAIFMT_I2S |
90		   SND_SOC_DAIFMT_NB_NF |
91		   SND_SOC_DAIFMT_CBS_CFS,
92	SND_SOC_DAILINK_REG(single_dsp),
93};
94
95static struct snd_soc_card snd_soc_trimslice = {
96	.name = "tegra-trimslice",
97	.driver_name = "tegra",
98	.owner = THIS_MODULE,
99	.dai_link = &trimslice_tlv320aic23_dai,
100	.num_links = 1,
101
102	.dapm_widgets = trimslice_dapm_widgets,
103	.num_dapm_widgets = ARRAY_SIZE(trimslice_dapm_widgets),
104	.dapm_routes = trimslice_audio_map,
105	.num_dapm_routes = ARRAY_SIZE(trimslice_audio_map),
106	.fully_routed = true,
107};
108
109static int tegra_snd_trimslice_probe(struct platform_device *pdev)
110{
111	struct device_node *np = pdev->dev.of_node;
112	struct snd_soc_card *card = &snd_soc_trimslice;
113	struct tegra_trimslice *trimslice;
114	int ret;
115
116	trimslice = devm_kzalloc(&pdev->dev, sizeof(struct tegra_trimslice),
117				 GFP_KERNEL);
118	if (!trimslice)
119		return -ENOMEM;
120
121	card->dev = &pdev->dev;
122	snd_soc_card_set_drvdata(card, trimslice);
123
124	trimslice_tlv320aic23_dai.codecs->of_node = of_parse_phandle(np,
125			"nvidia,audio-codec", 0);
126	if (!trimslice_tlv320aic23_dai.codecs->of_node) {
127		dev_err(&pdev->dev,
128			"Property 'nvidia,audio-codec' missing or invalid\n");
129		return -EINVAL;
130	}
131
132	trimslice_tlv320aic23_dai.cpus->of_node = of_parse_phandle(np,
133			"nvidia,i2s-controller", 0);
134	if (!trimslice_tlv320aic23_dai.cpus->of_node) {
135		dev_err(&pdev->dev,
136			"Property 'nvidia,i2s-controller' missing or invalid\n");
137		return -EINVAL;
138	}
139
140	trimslice_tlv320aic23_dai.platforms->of_node =
141			trimslice_tlv320aic23_dai.cpus->of_node;
142
143	ret = tegra_asoc_utils_init(&trimslice->util_data, &pdev->dev);
144	if (ret)
145		return ret;
146
147	ret = devm_snd_soc_register_card(&pdev->dev, card);
148	if (ret) {
149		dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
150			ret);
151		return ret;
152	}
153
154	return 0;
155}
156
157static const struct of_device_id trimslice_of_match[] = {
158	{ .compatible = "nvidia,tegra-audio-trimslice", },
159	{},
160};
161MODULE_DEVICE_TABLE(of, trimslice_of_match);
162
163static struct platform_driver tegra_snd_trimslice_driver = {
164	.driver = {
165		.name = DRV_NAME,
166		.of_match_table = trimslice_of_match,
167	},
168	.probe = tegra_snd_trimslice_probe,
169};
170module_platform_driver(tegra_snd_trimslice_driver);
171
172MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
173MODULE_DESCRIPTION("Trimslice machine ASoC driver");
174MODULE_LICENSE("GPL");
175MODULE_ALIAS("platform:" DRV_NAME);
176