1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // simple-card-utils.c
4 //
5 // Copyright (c) 2016 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 
7 #include <linux/clk.h>
8 #include <linux/gpio.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/of_gpio.h>
13 #include <linux/of_graph.h>
14 #include <sound/jack.h>
15 #include <sound/simple_card_utils.h>
16 
17 void asoc_simple_card_convert_fixup(struct asoc_simple_card_data *data,
18 				    struct snd_pcm_hw_params *params)
19 {
20 	struct snd_interval *rate = hw_param_interval(params,
21 						SNDRV_PCM_HW_PARAM_RATE);
22 	struct snd_interval *channels = hw_param_interval(params,
23 						SNDRV_PCM_HW_PARAM_CHANNELS);
24 
25 	if (data->convert_rate)
26 		rate->min =
27 		rate->max = data->convert_rate;
28 
29 	if (data->convert_channels)
30 		channels->min =
31 		channels->max = data->convert_channels;
32 }
33 EXPORT_SYMBOL_GPL(asoc_simple_card_convert_fixup);
34 
35 void asoc_simple_card_parse_convert(struct device *dev,
36 				    struct device_node *np,
37 				    char *prefix,
38 				    struct asoc_simple_card_data *data)
39 {
40 	char prop[128];
41 
42 	if (!prefix)
43 		prefix = "";
44 
45 	/* sampling rate convert */
46 	snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-rate");
47 	of_property_read_u32(np, prop, &data->convert_rate);
48 
49 	/* channels transfer */
50 	snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-channels");
51 	of_property_read_u32(np, prop, &data->convert_channels);
52 
53 	dev_dbg(dev, "convert_rate     %d\n", data->convert_rate);
54 	dev_dbg(dev, "convert_channels %d\n", data->convert_channels);
55 }
56 EXPORT_SYMBOL_GPL(asoc_simple_card_parse_convert);
57 
58 int asoc_simple_card_parse_daifmt(struct device *dev,
59 				  struct device_node *node,
60 				  struct device_node *codec,
61 				  char *prefix,
62 				  unsigned int *retfmt)
63 {
64 	struct device_node *bitclkmaster = NULL;
65 	struct device_node *framemaster = NULL;
66 	unsigned int daifmt;
67 
68 	daifmt = snd_soc_of_parse_daifmt(node, prefix,
69 					 &bitclkmaster, &framemaster);
70 	daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
71 
72 	if (!bitclkmaster && !framemaster) {
73 		/*
74 		 * No dai-link level and master setting was not found from
75 		 * sound node level, revert back to legacy DT parsing and
76 		 * take the settings from codec node.
77 		 */
78 		dev_dbg(dev, "Revert to legacy daifmt parsing\n");
79 
80 		daifmt = snd_soc_of_parse_daifmt(codec, NULL, NULL, NULL) |
81 			(daifmt & ~SND_SOC_DAIFMT_CLOCK_MASK);
82 	} else {
83 		if (codec == bitclkmaster)
84 			daifmt |= (codec == framemaster) ?
85 				SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS;
86 		else
87 			daifmt |= (codec == framemaster) ?
88 				SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS;
89 	}
90 
91 	of_node_put(bitclkmaster);
92 	of_node_put(framemaster);
93 
94 	*retfmt = daifmt;
95 
96 	dev_dbg(dev, "format : %04x\n", daifmt);
97 
98 	return 0;
99 }
100 EXPORT_SYMBOL_GPL(asoc_simple_card_parse_daifmt);
101 
102 int asoc_simple_card_set_dailink_name(struct device *dev,
103 				      struct snd_soc_dai_link *dai_link,
104 				      const char *fmt, ...)
105 {
106 	va_list ap;
107 	char *name = NULL;
108 	int ret = -ENOMEM;
109 
110 	va_start(ap, fmt);
111 	name = devm_kvasprintf(dev, GFP_KERNEL, fmt, ap);
112 	va_end(ap);
113 
114 	if (name) {
115 		ret = 0;
116 
117 		dai_link->name		= name;
118 		dai_link->stream_name	= name;
119 
120 		dev_dbg(dev, "name : %s\n", name);
121 	}
122 
123 	return ret;
124 }
125 EXPORT_SYMBOL_GPL(asoc_simple_card_set_dailink_name);
126 
127 int asoc_simple_card_parse_card_name(struct snd_soc_card *card,
128 				     char *prefix)
129 {
130 	int ret;
131 
132 	if (!prefix)
133 		prefix = "";
134 
135 	/* Parse the card name from DT */
136 	ret = snd_soc_of_parse_card_name(card, "label");
137 	if (ret < 0 || !card->name) {
138 		char prop[128];
139 
140 		snprintf(prop, sizeof(prop), "%sname", prefix);
141 		ret = snd_soc_of_parse_card_name(card, prop);
142 		if (ret < 0)
143 			return ret;
144 	}
145 
146 	if (!card->name && card->dai_link)
147 		card->name = card->dai_link->name;
148 
149 	dev_dbg(card->dev, "Card Name: %s\n", card->name ? card->name : "");
150 
151 	return 0;
152 }
153 EXPORT_SYMBOL_GPL(asoc_simple_card_parse_card_name);
154 
155 int asoc_simple_card_clk_enable(struct asoc_simple_dai *dai)
156 {
157 	if (dai)
158 		return clk_prepare_enable(dai->clk);
159 
160 	return 0;
161 }
162 EXPORT_SYMBOL_GPL(asoc_simple_card_clk_enable);
163 
164 void asoc_simple_card_clk_disable(struct asoc_simple_dai *dai)
165 {
166 	if (dai)
167 		clk_disable_unprepare(dai->clk);
168 }
169 EXPORT_SYMBOL_GPL(asoc_simple_card_clk_disable);
170 
171 int asoc_simple_card_parse_clk(struct device *dev,
172 			       struct device_node *node,
173 			       struct device_node *dai_of_node,
174 			       struct asoc_simple_dai *simple_dai,
175 			       const char *dai_name,
176 			       struct snd_soc_dai_link_component *dlc)
177 {
178 	struct clk *clk;
179 	u32 val;
180 
181 	/*
182 	 * Use snd_soc_dai_link_component instead of legacy style.
183 	 * It is only for codec, but cpu will be supported in the future.
184 	 * see
185 	 *	soc-core.c :: snd_soc_init_multicodec()
186 	 */
187 	if (dlc) {
188 		dai_of_node	= dlc->of_node;
189 		dai_name	= dlc->dai_name;
190 	}
191 
192 	/*
193 	 * Parse dai->sysclk come from "clocks = <&xxx>"
194 	 * (if system has common clock)
195 	 *  or "system-clock-frequency = <xxx>"
196 	 *  or device's module clock.
197 	 */
198 	clk = devm_get_clk_from_child(dev, node, NULL);
199 	if (!IS_ERR(clk)) {
200 		simple_dai->sysclk = clk_get_rate(clk);
201 
202 		simple_dai->clk = clk;
203 	} else if (!of_property_read_u32(node, "system-clock-frequency", &val)) {
204 		simple_dai->sysclk = val;
205 	} else {
206 		clk = devm_get_clk_from_child(dev, dai_of_node, NULL);
207 		if (!IS_ERR(clk))
208 			simple_dai->sysclk = clk_get_rate(clk);
209 	}
210 
211 	if (of_property_read_bool(node, "system-clock-direction-out"))
212 		simple_dai->clk_direction = SND_SOC_CLOCK_OUT;
213 
214 	dev_dbg(dev, "%s : sysclk = %d, direction %d\n", dai_name,
215 		simple_dai->sysclk, simple_dai->clk_direction);
216 
217 	return 0;
218 }
219 EXPORT_SYMBOL_GPL(asoc_simple_card_parse_clk);
220 
221 int asoc_simple_card_parse_dai(struct device_node *node,
222 				    struct snd_soc_dai_link_component *dlc,
223 				    struct device_node **dai_of_node,
224 				    const char **dai_name,
225 				    const char *list_name,
226 				    const char *cells_name,
227 				    int *is_single_link)
228 {
229 	struct of_phandle_args args;
230 	int ret;
231 
232 	if (!node)
233 		return 0;
234 
235 	/*
236 	 * Use snd_soc_dai_link_component instead of legacy style.
237 	 * It is only for codec, but cpu will be supported in the future.
238 	 * see
239 	 *	soc-core.c :: snd_soc_init_multicodec()
240 	 */
241 	if (dlc) {
242 		dai_name	= &dlc->dai_name;
243 		dai_of_node	= &dlc->of_node;
244 	}
245 
246 	/*
247 	 * Get node via "sound-dai = <&phandle port>"
248 	 * it will be used as xxx_of_node on soc_bind_dai_link()
249 	 */
250 	ret = of_parse_phandle_with_args(node, list_name, cells_name, 0, &args);
251 	if (ret)
252 		return ret;
253 
254 	/* Get dai->name */
255 	if (dai_name) {
256 		ret = snd_soc_of_get_dai_name(node, dai_name);
257 		if (ret < 0)
258 			return ret;
259 	}
260 
261 	*dai_of_node = args.np;
262 
263 	if (is_single_link)
264 		*is_single_link = !args.args_count;
265 
266 	return 0;
267 }
268 EXPORT_SYMBOL_GPL(asoc_simple_card_parse_dai);
269 
270 static int asoc_simple_card_get_dai_id(struct device_node *ep)
271 {
272 	struct device_node *node;
273 	struct device_node *endpoint;
274 	struct of_endpoint info;
275 	int i, id;
276 	int ret;
277 
278 	/* use driver specified DAI ID if exist */
279 	ret = snd_soc_get_dai_id(ep);
280 	if (ret != -ENOTSUPP)
281 		return ret;
282 
283 	/* use endpoint/port reg if exist */
284 	ret = of_graph_parse_endpoint(ep, &info);
285 	if (ret == 0) {
286 		if (info.id)
287 			return info.id;
288 		if (info.port)
289 			return info.port;
290 	}
291 
292 	node = of_graph_get_port_parent(ep);
293 
294 	/*
295 	 * Non HDMI sound case, counting port/endpoint on its DT
296 	 * is enough. Let's count it.
297 	 */
298 	i = 0;
299 	id = -1;
300 	for_each_endpoint_of_node(node, endpoint) {
301 		if (endpoint == ep)
302 			id = i;
303 		i++;
304 	}
305 
306 	of_node_put(node);
307 
308 	if (id < 0)
309 		return -ENODEV;
310 
311 	return id;
312 }
313 
314 int asoc_simple_card_parse_graph_dai(struct device_node *ep,
315 				     struct snd_soc_dai_link_component *dlc,
316 				     struct device_node **dai_of_node,
317 				     const char **dai_name)
318 {
319 	struct device_node *node;
320 	struct of_phandle_args args;
321 	int ret;
322 
323 	/*
324 	 * Use snd_soc_dai_link_component instead of legacy style.
325 	 * It is only for codec, but cpu will be supported in the future.
326 	 * see
327 	 *	soc-core.c :: snd_soc_init_multicodec()
328 	 */
329 	if (dlc) {
330 		dai_name	= &dlc->dai_name;
331 		dai_of_node	= &dlc->of_node;
332 	}
333 
334 	if (!ep)
335 		return 0;
336 	if (!dai_name)
337 		return 0;
338 
339 	node = of_graph_get_port_parent(ep);
340 
341 	/* Get dai->name */
342 	args.np		= node;
343 	args.args[0]	= asoc_simple_card_get_dai_id(ep);
344 	args.args_count	= (of_graph_get_endpoint_count(node) > 1);
345 
346 	ret = snd_soc_get_dai_name(&args, dai_name);
347 	if (ret < 0)
348 		return ret;
349 
350 	*dai_of_node = node;
351 
352 	return 0;
353 }
354 EXPORT_SYMBOL_GPL(asoc_simple_card_parse_graph_dai);
355 
356 int asoc_simple_card_init_dai(struct snd_soc_dai *dai,
357 			      struct asoc_simple_dai *simple_dai)
358 {
359 	int ret;
360 
361 	if (!simple_dai)
362 		return 0;
363 
364 	if (simple_dai->sysclk) {
365 		ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk,
366 					     simple_dai->clk_direction);
367 		if (ret && ret != -ENOTSUPP) {
368 			dev_err(dai->dev, "simple-card: set_sysclk error\n");
369 			return ret;
370 		}
371 	}
372 
373 	if (simple_dai->slots) {
374 		ret = snd_soc_dai_set_tdm_slot(dai,
375 					       simple_dai->tx_slot_mask,
376 					       simple_dai->rx_slot_mask,
377 					       simple_dai->slots,
378 					       simple_dai->slot_width);
379 		if (ret && ret != -ENOTSUPP) {
380 			dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
381 			return ret;
382 		}
383 	}
384 
385 	return 0;
386 }
387 EXPORT_SYMBOL_GPL(asoc_simple_card_init_dai);
388 
389 int asoc_simple_card_canonicalize_dailink(struct snd_soc_dai_link *dai_link)
390 {
391 	/* Assumes platform == cpu */
392 	if (!dai_link->platform->of_node)
393 		dai_link->platform->of_node = dai_link->cpu_of_node;
394 
395 	return 0;
396 
397 }
398 EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_dailink);
399 
400 void asoc_simple_card_canonicalize_cpu(struct snd_soc_dai_link *dai_link,
401 				       int is_single_links)
402 {
403 	/*
404 	 * In soc_bind_dai_link() will check cpu name after
405 	 * of_node matching if dai_link has cpu_dai_name.
406 	 * but, it will never match if name was created by
407 	 * fmt_single_name() remove cpu_dai_name if cpu_args
408 	 * was 0. See:
409 	 *	fmt_single_name()
410 	 *	fmt_multiple_name()
411 	 */
412 	if (is_single_links)
413 		dai_link->cpu_dai_name = NULL;
414 }
415 EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_cpu);
416 
417 int asoc_simple_card_clean_reference(struct snd_soc_card *card)
418 {
419 	struct snd_soc_dai_link *dai_link;
420 	int i;
421 
422 	for_each_card_prelinks(card, i, dai_link) {
423 		of_node_put(dai_link->cpu_of_node);
424 		of_node_put(dai_link->codecs->of_node);
425 	}
426 	return 0;
427 }
428 EXPORT_SYMBOL_GPL(asoc_simple_card_clean_reference);
429 
430 int asoc_simple_card_of_parse_routing(struct snd_soc_card *card,
431 				      char *prefix)
432 {
433 	struct device_node *node = card->dev->of_node;
434 	char prop[128];
435 
436 	if (!prefix)
437 		prefix = "";
438 
439 	snprintf(prop, sizeof(prop), "%s%s", prefix, "routing");
440 
441 	if (!of_property_read_bool(node, prop))
442 		return 0;
443 
444 	return snd_soc_of_parse_audio_routing(card, prop);
445 }
446 EXPORT_SYMBOL_GPL(asoc_simple_card_of_parse_routing);
447 
448 int asoc_simple_card_of_parse_widgets(struct snd_soc_card *card,
449 				      char *prefix)
450 {
451 	struct device_node *node = card->dev->of_node;
452 	char prop[128];
453 
454 	if (!prefix)
455 		prefix = "";
456 
457 	snprintf(prop, sizeof(prop), "%s%s", prefix, "widgets");
458 
459 	if (of_property_read_bool(node, prop))
460 		return snd_soc_of_parse_audio_simple_widgets(card, prop);
461 
462 	/* no widgets is not error */
463 	return 0;
464 }
465 EXPORT_SYMBOL_GPL(asoc_simple_card_of_parse_widgets);
466 
467 int asoc_simple_card_init_jack(struct snd_soc_card *card,
468 			       struct asoc_simple_jack *sjack,
469 			       int is_hp, char *prefix)
470 {
471 	struct device *dev = card->dev;
472 	enum of_gpio_flags flags;
473 	char prop[128];
474 	char *pin_name;
475 	char *gpio_name;
476 	int mask;
477 	int det;
478 
479 	if (!prefix)
480 		prefix = "";
481 
482 	sjack->gpio.gpio = -ENOENT;
483 
484 	if (is_hp) {
485 		snprintf(prop, sizeof(prop), "%shp-det-gpio", prefix);
486 		pin_name	= "Headphones";
487 		gpio_name	= "Headphone detection";
488 		mask		= SND_JACK_HEADPHONE;
489 	} else {
490 		snprintf(prop, sizeof(prop), "%smic-det-gpio", prefix);
491 		pin_name	= "Mic Jack";
492 		gpio_name	= "Mic detection";
493 		mask		= SND_JACK_MICROPHONE;
494 	}
495 
496 	det = of_get_named_gpio_flags(dev->of_node, prop, 0, &flags);
497 	if (det == -EPROBE_DEFER)
498 		return -EPROBE_DEFER;
499 
500 	if (gpio_is_valid(det)) {
501 		sjack->pin.pin		= pin_name;
502 		sjack->pin.mask		= mask;
503 
504 		sjack->gpio.name	= gpio_name;
505 		sjack->gpio.report	= mask;
506 		sjack->gpio.gpio	= det;
507 		sjack->gpio.invert	= !!(flags & OF_GPIO_ACTIVE_LOW);
508 		sjack->gpio.debounce_time = 150;
509 
510 		snd_soc_card_jack_new(card, pin_name, mask,
511 				      &sjack->jack,
512 				      &sjack->pin, 1);
513 
514 		snd_soc_jack_add_gpios(&sjack->jack, 1,
515 				       &sjack->gpio);
516 	}
517 
518 	return 0;
519 }
520 EXPORT_SYMBOL_GPL(asoc_simple_card_init_jack);
521 
522 /* Module information */
523 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
524 MODULE_DESCRIPTION("ALSA SoC Simple Card Utils");
525 MODULE_LICENSE("GPL v2");
526