xref: /openbmc/linux/sound/soc/codecs/es7134.c (revision 6f2bde9b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2017 BayLibre, SAS.
4  * Author: Jerome Brunet <jbrunet@baylibre.com>
5  */
6 
7 #include <linux/of_platform.h>
8 #include <linux/module.h>
9 #include <sound/soc.h>
10 
11 /*
12  * The everest 7134 is a very simple DA converter with no register
13  */
14 
15 struct es7134_clock_mode {
16 	unsigned int rate_min;
17 	unsigned int rate_max;
18 	unsigned int *mclk_fs;
19 	unsigned int mclk_fs_num;
20 };
21 
22 struct es7134_chip {
23 	struct snd_soc_dai_driver *dai_drv;
24 	const struct es7134_clock_mode *modes;
25 	unsigned int mode_num;
26 	const struct snd_soc_dapm_widget *extra_widgets;
27 	unsigned int extra_widget_num;
28 	const struct snd_soc_dapm_route *extra_routes;
29 	unsigned int extra_route_num;
30 };
31 
32 struct es7134_data {
33 	unsigned int mclk;
34 	const struct es7134_chip *chip;
35 };
36 
37 static int es7134_check_mclk(struct snd_soc_dai *dai,
38 			     struct es7134_data *priv,
39 			     unsigned int rate)
40 {
41 	unsigned int mfs = priv->mclk / rate;
42 	int i, j;
43 
44 	for (i = 0; i < priv->chip->mode_num; i++) {
45 		const struct es7134_clock_mode *mode = &priv->chip->modes[i];
46 
47 		if (rate < mode->rate_min || rate > mode->rate_max)
48 			continue;
49 
50 		for (j = 0; j < mode->mclk_fs_num; j++) {
51 			if (mode->mclk_fs[j] == mfs)
52 				return 0;
53 		}
54 
55 		dev_err(dai->dev, "unsupported mclk_fs %u for rate %u\n",
56 			mfs, rate);
57 		return -EINVAL;
58 	}
59 
60 	/* should not happen */
61 	dev_err(dai->dev, "unsupported rate: %u\n", rate);
62 	return -EINVAL;
63 }
64 
65 static int es7134_hw_params(struct snd_pcm_substream *substream,
66 			    struct snd_pcm_hw_params *params,
67 			    struct snd_soc_dai *dai)
68 {
69 	struct es7134_data *priv = snd_soc_dai_get_drvdata(dai);
70 
71 	/* mclk has not been provided, assume it is OK */
72 	if (!priv->mclk)
73 		return 0;
74 
75 	return es7134_check_mclk(dai, priv, params_rate(params));
76 }
77 
78 static int es7134_set_sysclk(struct snd_soc_dai *dai, int clk_id,
79 			     unsigned int freq, int dir)
80 {
81 	struct es7134_data *priv = snd_soc_dai_get_drvdata(dai);
82 
83 	if (dir == SND_SOC_CLOCK_IN && clk_id == 0) {
84 		priv->mclk = freq;
85 		return 0;
86 	}
87 
88 	return -ENOTSUPP;
89 }
90 
91 static int es7134_set_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
92 {
93 	fmt &= (SND_SOC_DAIFMT_FORMAT_MASK | SND_SOC_DAIFMT_INV_MASK |
94 		SND_SOC_DAIFMT_MASTER_MASK);
95 
96 	if (fmt != (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
97 		    SND_SOC_DAIFMT_CBC_CFC)) {
98 		dev_err(codec_dai->dev, "Invalid DAI format\n");
99 		return -EINVAL;
100 	}
101 
102 	return 0;
103 }
104 
105 static int es7134_component_probe(struct snd_soc_component *c)
106 {
107 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(c);
108 	struct es7134_data *priv = snd_soc_component_get_drvdata(c);
109 	const struct es7134_chip *chip = priv->chip;
110 	int ret;
111 
112 	if (chip->extra_widget_num) {
113 		ret = snd_soc_dapm_new_controls(dapm, chip->extra_widgets,
114 						chip->extra_widget_num);
115 		if (ret) {
116 			dev_err(c->dev, "failed to add extra widgets\n");
117 			return ret;
118 		}
119 	}
120 
121 	if (chip->extra_route_num) {
122 		ret = snd_soc_dapm_add_routes(dapm, chip->extra_routes,
123 					      chip->extra_route_num);
124 		if (ret) {
125 			dev_err(c->dev, "failed to add extra routes\n");
126 			return ret;
127 		}
128 	}
129 
130 	return 0;
131 }
132 
133 static const struct snd_soc_dai_ops es7134_dai_ops = {
134 	.set_fmt	= es7134_set_fmt,
135 	.hw_params	= es7134_hw_params,
136 	.set_sysclk	= es7134_set_sysclk,
137 };
138 
139 static struct snd_soc_dai_driver es7134_dai = {
140 	.name = "es7134-hifi",
141 	.playback = {
142 		.stream_name = "Playback",
143 		.channels_min = 2,
144 		.channels_max = 2,
145 		.rates = (SNDRV_PCM_RATE_8000_48000 |
146 			  SNDRV_PCM_RATE_88200      |
147 			  SNDRV_PCM_RATE_96000      |
148 			  SNDRV_PCM_RATE_176400     |
149 			  SNDRV_PCM_RATE_192000),
150 		.formats = (SNDRV_PCM_FMTBIT_S16_LE  |
151 			    SNDRV_PCM_FMTBIT_S18_3LE |
152 			    SNDRV_PCM_FMTBIT_S20_3LE |
153 			    SNDRV_PCM_FMTBIT_S24_3LE |
154 			    SNDRV_PCM_FMTBIT_S24_LE),
155 	},
156 	.ops = &es7134_dai_ops,
157 };
158 
159 static const struct es7134_clock_mode es7134_modes[] = {
160 	{
161 		/* Single speed mode */
162 		.rate_min = 8000,
163 		.rate_max = 50000,
164 		.mclk_fs = (unsigned int[]) { 256, 384, 512, 768, 1024 },
165 		.mclk_fs_num = 5,
166 	}, {
167 		/* Double speed mode */
168 		.rate_min = 84000,
169 		.rate_max = 100000,
170 		.mclk_fs = (unsigned int[]) { 128, 192, 256, 384, 512 },
171 		.mclk_fs_num = 5,
172 	}, {
173 		/* Quad speed mode */
174 		.rate_min = 167000,
175 		.rate_max = 192000,
176 		.mclk_fs = (unsigned int[]) { 128, 192, 256 },
177 		.mclk_fs_num = 3,
178 	},
179 };
180 
181 /* Digital I/O are also supplied by VDD on the es7134 */
182 static const struct snd_soc_dapm_route es7134_extra_routes[] = {
183 	{ "Playback", NULL, "VDD", }
184 };
185 
186 static const struct es7134_chip es7134_chip __maybe_unused = {
187 	.dai_drv = &es7134_dai,
188 	.modes = es7134_modes,
189 	.mode_num = ARRAY_SIZE(es7134_modes),
190 	.extra_routes = es7134_extra_routes,
191 	.extra_route_num = ARRAY_SIZE(es7134_extra_routes),
192 };
193 
194 static const struct snd_soc_dapm_widget es7134_dapm_widgets[] = {
195 	SND_SOC_DAPM_OUTPUT("AOUTL"),
196 	SND_SOC_DAPM_OUTPUT("AOUTR"),
197 	SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
198 	SND_SOC_DAPM_REGULATOR_SUPPLY("VDD", 0, 0),
199 };
200 
201 static const struct snd_soc_dapm_route es7134_dapm_routes[] = {
202 	{ "AOUTL", NULL, "DAC" },
203 	{ "AOUTR", NULL, "DAC" },
204 	{ "DAC", NULL, "VDD" },
205 };
206 
207 static const struct snd_soc_component_driver es7134_component_driver = {
208 	.probe			= es7134_component_probe,
209 	.dapm_widgets		= es7134_dapm_widgets,
210 	.num_dapm_widgets	= ARRAY_SIZE(es7134_dapm_widgets),
211 	.dapm_routes		= es7134_dapm_routes,
212 	.num_dapm_routes	= ARRAY_SIZE(es7134_dapm_routes),
213 	.idle_bias_on		= 1,
214 	.use_pmdown_time	= 1,
215 	.endianness		= 1,
216 };
217 
218 static struct snd_soc_dai_driver es7154_dai = {
219 	.name = "es7154-hifi",
220 	.playback = {
221 		.stream_name = "Playback",
222 		.channels_min = 2,
223 		.channels_max = 2,
224 		.rates = (SNDRV_PCM_RATE_8000_48000 |
225 			  SNDRV_PCM_RATE_88200      |
226 			  SNDRV_PCM_RATE_96000),
227 		.formats = (SNDRV_PCM_FMTBIT_S16_LE  |
228 			    SNDRV_PCM_FMTBIT_S18_3LE |
229 			    SNDRV_PCM_FMTBIT_S20_3LE |
230 			    SNDRV_PCM_FMTBIT_S24_3LE |
231 			    SNDRV_PCM_FMTBIT_S24_LE),
232 	},
233 	.ops = &es7134_dai_ops,
234 };
235 
236 static const struct es7134_clock_mode es7154_modes[] = {
237 	{
238 		/* Single speed mode */
239 		.rate_min = 8000,
240 		.rate_max = 50000,
241 		.mclk_fs = (unsigned int[]) { 32, 64, 128, 192, 256,
242 					      384, 512, 768, 1024 },
243 		.mclk_fs_num = 9,
244 	}, {
245 		/* Double speed mode */
246 		.rate_min = 84000,
247 		.rate_max = 100000,
248 		.mclk_fs = (unsigned int[]) { 128, 192, 256, 384, 512,
249 					      768, 1024},
250 		.mclk_fs_num = 7,
251 	}
252 };
253 
254 /* Es7154 has a separate supply for digital I/O  */
255 static const struct snd_soc_dapm_widget es7154_extra_widgets[] = {
256 	SND_SOC_DAPM_REGULATOR_SUPPLY("PVDD", 0, 0),
257 };
258 
259 static const struct snd_soc_dapm_route es7154_extra_routes[] = {
260 	{ "Playback", NULL, "PVDD", }
261 };
262 
263 static const struct es7134_chip es7154_chip __maybe_unused = {
264 	.dai_drv = &es7154_dai,
265 	.modes = es7154_modes,
266 	.mode_num = ARRAY_SIZE(es7154_modes),
267 	.extra_routes = es7154_extra_routes,
268 	.extra_route_num = ARRAY_SIZE(es7154_extra_routes),
269 	.extra_widgets = es7154_extra_widgets,
270 	.extra_widget_num = ARRAY_SIZE(es7154_extra_widgets),
271 };
272 
273 static int es7134_probe(struct platform_device *pdev)
274 {
275 	struct device *dev = &pdev->dev;
276 	struct es7134_data *priv;
277 
278 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
279 	if (!priv)
280 		return -ENOMEM;
281 	platform_set_drvdata(pdev, priv);
282 
283 	priv->chip = of_device_get_match_data(dev);
284 	if (!priv->chip) {
285 		dev_err(dev, "failed to match device\n");
286 		return -ENODEV;
287 	}
288 
289 	return devm_snd_soc_register_component(&pdev->dev,
290 				      &es7134_component_driver,
291 				      priv->chip->dai_drv, 1);
292 }
293 
294 #ifdef CONFIG_OF
295 static const struct of_device_id es7134_ids[] = {
296 	{ .compatible = "everest,es7134", .data = &es7134_chip },
297 	{ .compatible = "everest,es7144", .data = &es7134_chip },
298 	{ .compatible = "everest,es7154", .data = &es7154_chip },
299 	{ }
300 };
301 MODULE_DEVICE_TABLE(of, es7134_ids);
302 #endif
303 
304 static struct platform_driver es7134_driver = {
305 	.driver = {
306 		.name = "es7134",
307 		.of_match_table = of_match_ptr(es7134_ids),
308 	},
309 	.probe = es7134_probe,
310 };
311 
312 module_platform_driver(es7134_driver);
313 
314 MODULE_DESCRIPTION("ASoC ES7134 audio codec driver");
315 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
316 MODULE_LICENSE("GPL");
317