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 		/*
287 		 * Because it will count port/endpoint if it doesn't have "reg".
288 		 * But, we can't judge whether it has "no reg", or "reg = <0>"
289 		 * only of_graph_parse_endpoint().
290 		 * We need to check "reg" property
291 		 */
292 		if (of_get_property(ep,   "reg", NULL))
293 			return info.id;
294 
295 		node = of_get_parent(ep);
296 		of_node_put(node);
297 		if (of_get_property(node, "reg", NULL))
298 			return info.port;
299 	}
300 	node = of_graph_get_port_parent(ep);
301 
302 	/*
303 	 * Non HDMI sound case, counting port/endpoint on its DT
304 	 * is enough. Let's count it.
305 	 */
306 	i = 0;
307 	id = -1;
308 	for_each_endpoint_of_node(node, endpoint) {
309 		if (endpoint == ep)
310 			id = i;
311 		i++;
312 	}
313 
314 	of_node_put(node);
315 
316 	if (id < 0)
317 		return -ENODEV;
318 
319 	return id;
320 }
321 
322 int asoc_simple_card_parse_graph_dai(struct device_node *ep,
323 				     struct snd_soc_dai_link_component *dlc,
324 				     struct device_node **dai_of_node,
325 				     const char **dai_name)
326 {
327 	struct device_node *node;
328 	struct of_phandle_args args;
329 	int ret;
330 
331 	/*
332 	 * Use snd_soc_dai_link_component instead of legacy style.
333 	 * It is only for codec, but cpu will be supported in the future.
334 	 * see
335 	 *	soc-core.c :: snd_soc_init_multicodec()
336 	 */
337 	if (dlc) {
338 		dai_name	= &dlc->dai_name;
339 		dai_of_node	= &dlc->of_node;
340 	}
341 
342 	if (!ep)
343 		return 0;
344 	if (!dai_name)
345 		return 0;
346 
347 	node = of_graph_get_port_parent(ep);
348 
349 	/* Get dai->name */
350 	args.np		= node;
351 	args.args[0]	= asoc_simple_card_get_dai_id(ep);
352 	args.args_count	= (of_graph_get_endpoint_count(node) > 1);
353 
354 	ret = snd_soc_get_dai_name(&args, dai_name);
355 	if (ret < 0)
356 		return ret;
357 
358 	*dai_of_node = node;
359 
360 	return 0;
361 }
362 EXPORT_SYMBOL_GPL(asoc_simple_card_parse_graph_dai);
363 
364 int asoc_simple_card_init_dai(struct snd_soc_dai *dai,
365 			      struct asoc_simple_dai *simple_dai)
366 {
367 	int ret;
368 
369 	if (!simple_dai)
370 		return 0;
371 
372 	if (simple_dai->sysclk) {
373 		ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk,
374 					     simple_dai->clk_direction);
375 		if (ret && ret != -ENOTSUPP) {
376 			dev_err(dai->dev, "simple-card: set_sysclk error\n");
377 			return ret;
378 		}
379 	}
380 
381 	if (simple_dai->slots) {
382 		ret = snd_soc_dai_set_tdm_slot(dai,
383 					       simple_dai->tx_slot_mask,
384 					       simple_dai->rx_slot_mask,
385 					       simple_dai->slots,
386 					       simple_dai->slot_width);
387 		if (ret && ret != -ENOTSUPP) {
388 			dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
389 			return ret;
390 		}
391 	}
392 
393 	return 0;
394 }
395 EXPORT_SYMBOL_GPL(asoc_simple_card_init_dai);
396 
397 void asoc_simple_card_canonicalize_platform(struct snd_soc_dai_link *dai_link)
398 {
399 	/* Assumes platform == cpu */
400 	if (!dai_link->platforms->of_node)
401 		dai_link->platforms->of_node = dai_link->cpu_of_node;
402 }
403 EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_platform);
404 
405 void asoc_simple_card_canonicalize_cpu(struct snd_soc_dai_link *dai_link,
406 				       int is_single_links)
407 {
408 	/*
409 	 * In soc_bind_dai_link() will check cpu name after
410 	 * of_node matching if dai_link has cpu_dai_name.
411 	 * but, it will never match if name was created by
412 	 * fmt_single_name() remove cpu_dai_name if cpu_args
413 	 * was 0. See:
414 	 *	fmt_single_name()
415 	 *	fmt_multiple_name()
416 	 */
417 	if (is_single_links)
418 		dai_link->cpu_dai_name = NULL;
419 }
420 EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_cpu);
421 
422 int asoc_simple_card_clean_reference(struct snd_soc_card *card)
423 {
424 	struct snd_soc_dai_link *dai_link;
425 	int i;
426 
427 	for_each_card_prelinks(card, i, dai_link) {
428 		of_node_put(dai_link->cpu_of_node);
429 		of_node_put(dai_link->codecs->of_node);
430 	}
431 	return 0;
432 }
433 EXPORT_SYMBOL_GPL(asoc_simple_card_clean_reference);
434 
435 int asoc_simple_card_of_parse_routing(struct snd_soc_card *card,
436 				      char *prefix)
437 {
438 	struct device_node *node = card->dev->of_node;
439 	char prop[128];
440 
441 	if (!prefix)
442 		prefix = "";
443 
444 	snprintf(prop, sizeof(prop), "%s%s", prefix, "routing");
445 
446 	if (!of_property_read_bool(node, prop))
447 		return 0;
448 
449 	return snd_soc_of_parse_audio_routing(card, prop);
450 }
451 EXPORT_SYMBOL_GPL(asoc_simple_card_of_parse_routing);
452 
453 int asoc_simple_card_of_parse_widgets(struct snd_soc_card *card,
454 				      char *prefix)
455 {
456 	struct device_node *node = card->dev->of_node;
457 	char prop[128];
458 
459 	if (!prefix)
460 		prefix = "";
461 
462 	snprintf(prop, sizeof(prop), "%s%s", prefix, "widgets");
463 
464 	if (of_property_read_bool(node, prop))
465 		return snd_soc_of_parse_audio_simple_widgets(card, prop);
466 
467 	/* no widgets is not error */
468 	return 0;
469 }
470 EXPORT_SYMBOL_GPL(asoc_simple_card_of_parse_widgets);
471 
472 int asoc_simple_card_init_jack(struct snd_soc_card *card,
473 			       struct asoc_simple_jack *sjack,
474 			       int is_hp, char *prefix)
475 {
476 	struct device *dev = card->dev;
477 	enum of_gpio_flags flags;
478 	char prop[128];
479 	char *pin_name;
480 	char *gpio_name;
481 	int mask;
482 	int det;
483 
484 	if (!prefix)
485 		prefix = "";
486 
487 	sjack->gpio.gpio = -ENOENT;
488 
489 	if (is_hp) {
490 		snprintf(prop, sizeof(prop), "%shp-det-gpio", prefix);
491 		pin_name	= "Headphones";
492 		gpio_name	= "Headphone detection";
493 		mask		= SND_JACK_HEADPHONE;
494 	} else {
495 		snprintf(prop, sizeof(prop), "%smic-det-gpio", prefix);
496 		pin_name	= "Mic Jack";
497 		gpio_name	= "Mic detection";
498 		mask		= SND_JACK_MICROPHONE;
499 	}
500 
501 	det = of_get_named_gpio_flags(dev->of_node, prop, 0, &flags);
502 	if (det == -EPROBE_DEFER)
503 		return -EPROBE_DEFER;
504 
505 	if (gpio_is_valid(det)) {
506 		sjack->pin.pin		= pin_name;
507 		sjack->pin.mask		= mask;
508 
509 		sjack->gpio.name	= gpio_name;
510 		sjack->gpio.report	= mask;
511 		sjack->gpio.gpio	= det;
512 		sjack->gpio.invert	= !!(flags & OF_GPIO_ACTIVE_LOW);
513 		sjack->gpio.debounce_time = 150;
514 
515 		snd_soc_card_jack_new(card, pin_name, mask,
516 				      &sjack->jack,
517 				      &sjack->pin, 1);
518 
519 		snd_soc_jack_add_gpios(&sjack->jack, 1,
520 				       &sjack->gpio);
521 	}
522 
523 	return 0;
524 }
525 EXPORT_SYMBOL_GPL(asoc_simple_card_init_jack);
526 
527 /* Module information */
528 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
529 MODULE_DESCRIPTION("ALSA SoC Simple Card Utils");
530 MODULE_LICENSE("GPL v2");
531