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