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