xref: /openbmc/linux/sound/soc/samsung/snow.c (revision 113094f7)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // ASoC machine driver for Snow boards
4 
5 #include <linux/clk.h>
6 #include <linux/module.h>
7 #include <linux/platform_device.h>
8 #include <linux/of.h>
9 #include <linux/of_device.h>
10 #include <sound/pcm_params.h>
11 #include <sound/soc.h>
12 
13 #include "i2s.h"
14 
15 #define FIN_PLL_RATE		24000000
16 
17 SND_SOC_DAILINK_DEFS(links,
18 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
19 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
20 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
21 
22 struct snow_priv {
23 	struct snd_soc_dai_link dai_link;
24 	struct clk *clk_i2s_bus;
25 };
26 
27 static int snow_card_hw_params(struct snd_pcm_substream *substream,
28 				      struct snd_pcm_hw_params *params)
29 {
30 	static const unsigned int pll_rate[] = {
31 		73728000U, 67737602U, 49152000U, 45158401U, 32768001U
32 	};
33 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
34 	struct snow_priv *priv = snd_soc_card_get_drvdata(rtd->card);
35 	int bfs, psr, rfs, bitwidth;
36 	unsigned long int rclk;
37 	long int freq = -EINVAL;
38 	int ret, i;
39 
40 	bitwidth = snd_pcm_format_width(params_format(params));
41 	if (bitwidth < 0) {
42 		dev_err(rtd->card->dev, "Invalid bit-width: %d\n", bitwidth);
43 		return bitwidth;
44 	}
45 
46 	if (bitwidth != 16 && bitwidth != 24) {
47 		dev_err(rtd->card->dev, "Unsupported bit-width: %d\n", bitwidth);
48 		return -EINVAL;
49 	}
50 
51 	bfs = 2 * bitwidth;
52 
53 	switch (params_rate(params)) {
54 	case 16000:
55 	case 22050:
56 	case 24000:
57 	case 32000:
58 	case 44100:
59 	case 48000:
60 	case 88200:
61 	case 96000:
62 		rfs = 8 * bfs;
63 		break;
64 	case 64000:
65 		rfs = 384;
66 		break;
67 	case 8000:
68 	case 11025:
69 	case 12000:
70 		rfs = 16 * bfs;
71 		break;
72 	default:
73 		return -EINVAL;
74 	}
75 
76 	rclk = params_rate(params) * rfs;
77 
78 	for (psr = 8; psr > 0; psr /= 2) {
79 		for (i = 0; i < ARRAY_SIZE(pll_rate); i++) {
80 			if ((pll_rate[i] - rclk * psr) <= 2) {
81 				freq = pll_rate[i];
82 				break;
83 			}
84 		}
85 	}
86 	if (freq < 0) {
87 		dev_err(rtd->card->dev, "Unsupported RCLK rate: %lu\n", rclk);
88 		return -EINVAL;
89 	}
90 
91 	ret = clk_set_rate(priv->clk_i2s_bus, freq);
92 	if (ret < 0) {
93 		dev_err(rtd->card->dev, "I2S bus clock rate set failed\n");
94 		return ret;
95 	}
96 
97 	return 0;
98 }
99 
100 static const struct snd_soc_ops snow_card_ops = {
101 	.hw_params = snow_card_hw_params,
102 };
103 
104 static int snow_late_probe(struct snd_soc_card *card)
105 {
106 	struct snd_soc_pcm_runtime *rtd;
107 	struct snd_soc_dai *codec_dai;
108 
109 	rtd = snd_soc_get_pcm_runtime(card, card->dai_link[0].name);
110 
111 	/* In the multi-codec case codec_dais 0 is MAX98095 and 1 is HDMI. */
112 	if (rtd->num_codecs > 1)
113 		codec_dai = rtd->codec_dais[0];
114 	else
115 		codec_dai = rtd->codec_dai;
116 
117 	/* Set the MCLK rate for the codec */
118 	return snd_soc_dai_set_sysclk(codec_dai, 0,
119 				FIN_PLL_RATE, SND_SOC_CLOCK_IN);
120 }
121 
122 static struct snd_soc_card snow_snd = {
123 	.name = "Snow-I2S",
124 	.owner = THIS_MODULE,
125 	.late_probe = snow_late_probe,
126 };
127 
128 static int snow_probe(struct platform_device *pdev)
129 {
130 	struct device *dev = &pdev->dev;
131 	struct snd_soc_card *card = &snow_snd;
132 	struct device_node *cpu, *codec;
133 	struct snd_soc_dai_link *link;
134 	struct snow_priv *priv;
135 	int ret;
136 
137 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
138 	if (!priv)
139 		return -ENOMEM;
140 
141 	link = &priv->dai_link;
142 
143 	link->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
144 			SND_SOC_DAIFMT_CBS_CFS;
145 
146 	link->name = "Primary";
147 	link->stream_name = link->name;
148 
149 	link->cpus = links_cpus;
150 	link->num_cpus = ARRAY_SIZE(links_cpus);
151 	link->codecs = links_codecs;
152 	link->num_codecs = ARRAY_SIZE(links_codecs);
153 	link->platforms = links_platforms;
154 	link->num_platforms = ARRAY_SIZE(links_platforms);
155 
156 	card->dai_link = link;
157 	card->num_links = 1;
158 	card->dev = dev;
159 
160 	/* Try new DT bindings with HDMI support first. */
161 	cpu = of_get_child_by_name(dev->of_node, "cpu");
162 
163 	if (cpu) {
164 		link->ops = &snow_card_ops;
165 
166 		link->cpus->of_node = of_parse_phandle(cpu, "sound-dai", 0);
167 		of_node_put(cpu);
168 
169 		if (!link->cpus->of_node) {
170 			dev_err(dev, "Failed parsing cpu/sound-dai property\n");
171 			return -EINVAL;
172 		}
173 
174 		codec = of_get_child_by_name(dev->of_node, "codec");
175 		ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
176 		of_node_put(codec);
177 
178 		if (ret < 0) {
179 			of_node_put(link->cpus->of_node);
180 			dev_err(dev, "Failed parsing codec node\n");
181 			return ret;
182 		}
183 
184 		priv->clk_i2s_bus = of_clk_get_by_name(link->cpus->of_node,
185 						       "i2s_opclk0");
186 		if (IS_ERR(priv->clk_i2s_bus)) {
187 			snd_soc_of_put_dai_link_codecs(link);
188 			of_node_put(link->cpus->of_node);
189 			return PTR_ERR(priv->clk_i2s_bus);
190 		}
191 	} else {
192 		link->codecs->dai_name = "HiFi",
193 
194 		link->cpus->of_node = of_parse_phandle(dev->of_node,
195 						"samsung,i2s-controller", 0);
196 		if (!link->cpus->of_node) {
197 			dev_err(dev, "i2s-controller property parse error\n");
198 			return -EINVAL;
199 		}
200 
201 		link->codecs->of_node = of_parse_phandle(dev->of_node,
202 						"samsung,audio-codec", 0);
203 		if (!link->codecs->of_node) {
204 			of_node_put(link->cpus->of_node);
205 			dev_err(dev, "audio-codec property parse error\n");
206 			return -EINVAL;
207 		}
208 	}
209 
210 	link->platforms->of_node = link->cpus->of_node;
211 
212 	/* Update card-name if provided through DT, else use default name */
213 	snd_soc_of_parse_card_name(card, "samsung,model");
214 
215 	snd_soc_card_set_drvdata(card, priv);
216 
217 	ret = devm_snd_soc_register_card(dev, card);
218 	if (ret) {
219 		dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
220 		return ret;
221 	}
222 
223 	return ret;
224 }
225 
226 static int snow_remove(struct platform_device *pdev)
227 {
228 	struct snow_priv *priv = platform_get_drvdata(pdev);
229 	struct snd_soc_dai_link *link = &priv->dai_link;
230 
231 	of_node_put(link->cpus->of_node);
232 	of_node_put(link->codecs->of_node);
233 	snd_soc_of_put_dai_link_codecs(link);
234 
235 	clk_put(priv->clk_i2s_bus);
236 
237 	return 0;
238 }
239 
240 static const struct of_device_id snow_of_match[] = {
241 	{ .compatible = "google,snow-audio-max98090", },
242 	{ .compatible = "google,snow-audio-max98091", },
243 	{ .compatible = "google,snow-audio-max98095", },
244 	{},
245 };
246 MODULE_DEVICE_TABLE(of, snow_of_match);
247 
248 static struct platform_driver snow_driver = {
249 	.driver = {
250 		.name = "snow-audio",
251 		.pm = &snd_soc_pm_ops,
252 		.of_match_table = snow_of_match,
253 	},
254 	.probe = snow_probe,
255 	.remove = snow_remove,
256 };
257 
258 module_platform_driver(snow_driver);
259 
260 MODULE_DESCRIPTION("ALSA SoC Audio machine driver for Snow");
261 MODULE_LICENSE("GPL");
262