ACPI: sysfs: Fix BERT error region memory mapping
[linux-2.6-microblaze.git] / sound / soc / generic / simple-card-utils.c
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/pcm_params.h>
16 #include <sound/simple_card_utils.h>
17
18 void asoc_simple_convert_fixup(struct asoc_simple_data *data,
19                                struct snd_pcm_hw_params *params)
20 {
21         struct snd_interval *rate = hw_param_interval(params,
22                                                 SNDRV_PCM_HW_PARAM_RATE);
23         struct snd_interval *channels = hw_param_interval(params,
24                                                 SNDRV_PCM_HW_PARAM_CHANNELS);
25
26         if (data->convert_rate)
27                 rate->min =
28                 rate->max = data->convert_rate;
29
30         if (data->convert_channels)
31                 channels->min =
32                 channels->max = data->convert_channels;
33 }
34 EXPORT_SYMBOL_GPL(asoc_simple_convert_fixup);
35
36 void asoc_simple_parse_convert(struct device_node *np,
37                                char *prefix,
38                                struct asoc_simple_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 EXPORT_SYMBOL_GPL(asoc_simple_parse_convert);
54
55 int asoc_simple_parse_daifmt(struct device *dev,
56                              struct device_node *node,
57                              struct device_node *codec,
58                              char *prefix,
59                              unsigned int *retfmt)
60 {
61         struct device_node *bitclkmaster = NULL;
62         struct device_node *framemaster = NULL;
63         unsigned int daifmt;
64
65         daifmt = snd_soc_daifmt_parse_format(node, prefix);
66
67         snd_soc_daifmt_parse_clock_provider_as_phandle(node, prefix, &bitclkmaster, &framemaster);
68         if (!bitclkmaster && !framemaster) {
69                 /*
70                  * No dai-link level and master setting was not found from
71                  * sound node level, revert back to legacy DT parsing and
72                  * take the settings from codec node.
73                  */
74                 dev_dbg(dev, "Revert to legacy daifmt parsing\n");
75
76                 daifmt |= snd_soc_daifmt_parse_clock_provider_as_flag(codec, NULL);
77         } else {
78                 daifmt |= snd_soc_daifmt_clock_provider_from_bitmap(
79                                 ((codec == bitclkmaster) << 4) | (codec == framemaster));
80         }
81
82         of_node_put(bitclkmaster);
83         of_node_put(framemaster);
84
85         *retfmt = daifmt;
86
87         return 0;
88 }
89 EXPORT_SYMBOL_GPL(asoc_simple_parse_daifmt);
90
91 int asoc_simple_parse_tdm_width_map(struct device *dev, struct device_node *np,
92                                     struct asoc_simple_dai *dai)
93 {
94         u32 *array_values, *p;
95         int n, i, ret;
96
97         if (!of_property_read_bool(np, "dai-tdm-slot-width-map"))
98                 return 0;
99
100         n = of_property_count_elems_of_size(np, "dai-tdm-slot-width-map", sizeof(u32));
101         if (n % 3) {
102                 dev_err(dev, "Invalid number of cells for dai-tdm-slot-width-map\n");
103                 return -EINVAL;
104         }
105
106         dai->tdm_width_map = devm_kcalloc(dev, n, sizeof(*dai->tdm_width_map), GFP_KERNEL);
107         if (!dai->tdm_width_map)
108                 return -ENOMEM;
109
110         array_values = kcalloc(n, sizeof(*array_values), GFP_KERNEL);
111         if (!array_values)
112                 return -ENOMEM;
113
114         ret = of_property_read_u32_array(np, "dai-tdm-slot-width-map", array_values, n);
115         if (ret < 0) {
116                 dev_err(dev, "Could not read dai-tdm-slot-width-map: %d\n", ret);
117                 goto out;
118         }
119
120         p = array_values;
121         for (i = 0; i < n / 3; ++i) {
122                 dai->tdm_width_map[i].sample_bits = *p++;
123                 dai->tdm_width_map[i].slot_width = *p++;
124                 dai->tdm_width_map[i].slot_count = *p++;
125         }
126
127         dai->n_tdm_widths = i;
128         ret = 0;
129 out:
130         kfree(array_values);
131
132         return ret;
133 }
134 EXPORT_SYMBOL_GPL(asoc_simple_parse_tdm_width_map);
135
136 int asoc_simple_set_dailink_name(struct device *dev,
137                                  struct snd_soc_dai_link *dai_link,
138                                  const char *fmt, ...)
139 {
140         va_list ap;
141         char *name = NULL;
142         int ret = -ENOMEM;
143
144         va_start(ap, fmt);
145         name = devm_kvasprintf(dev, GFP_KERNEL, fmt, ap);
146         va_end(ap);
147
148         if (name) {
149                 ret = 0;
150
151                 dai_link->name          = name;
152                 dai_link->stream_name   = name;
153         }
154
155         return ret;
156 }
157 EXPORT_SYMBOL_GPL(asoc_simple_set_dailink_name);
158
159 int asoc_simple_parse_card_name(struct snd_soc_card *card,
160                                 char *prefix)
161 {
162         int ret;
163
164         if (!prefix)
165                 prefix = "";
166
167         /* Parse the card name from DT */
168         ret = snd_soc_of_parse_card_name(card, "label");
169         if (ret < 0 || !card->name) {
170                 char prop[128];
171
172                 snprintf(prop, sizeof(prop), "%sname", prefix);
173                 ret = snd_soc_of_parse_card_name(card, prop);
174                 if (ret < 0)
175                         return ret;
176         }
177
178         if (!card->name && card->dai_link)
179                 card->name = card->dai_link->name;
180
181         return 0;
182 }
183 EXPORT_SYMBOL_GPL(asoc_simple_parse_card_name);
184
185 static int asoc_simple_clk_enable(struct asoc_simple_dai *dai)
186 {
187         if (dai)
188                 return clk_prepare_enable(dai->clk);
189
190         return 0;
191 }
192
193 static void asoc_simple_clk_disable(struct asoc_simple_dai *dai)
194 {
195         if (dai)
196                 clk_disable_unprepare(dai->clk);
197 }
198
199 int asoc_simple_parse_clk(struct device *dev,
200                           struct device_node *node,
201                           struct asoc_simple_dai *simple_dai,
202                           struct snd_soc_dai_link_component *dlc)
203 {
204         struct clk *clk;
205         u32 val;
206
207         /*
208          * Parse dai->sysclk come from "clocks = <&xxx>"
209          * (if system has common clock)
210          *  or "system-clock-frequency = <xxx>"
211          *  or device's module clock.
212          */
213         clk = devm_get_clk_from_child(dev, node, NULL);
214         simple_dai->clk_fixed = of_property_read_bool(
215                 node, "system-clock-fixed");
216         if (!IS_ERR(clk)) {
217                 simple_dai->sysclk = clk_get_rate(clk);
218
219                 simple_dai->clk = clk;
220         } else if (!of_property_read_u32(node, "system-clock-frequency", &val)) {
221                 simple_dai->sysclk = val;
222                 simple_dai->clk_fixed = true;
223         } else {
224                 clk = devm_get_clk_from_child(dev, dlc->of_node, NULL);
225                 if (!IS_ERR(clk))
226                         simple_dai->sysclk = clk_get_rate(clk);
227         }
228
229         if (of_property_read_bool(node, "system-clock-direction-out"))
230                 simple_dai->clk_direction = SND_SOC_CLOCK_OUT;
231
232         return 0;
233 }
234 EXPORT_SYMBOL_GPL(asoc_simple_parse_clk);
235
236 static int asoc_simple_check_fixed_sysclk(struct device *dev,
237                                           struct asoc_simple_dai *dai,
238                                           unsigned int *fixed_sysclk)
239 {
240         if (dai->clk_fixed) {
241                 if (*fixed_sysclk && *fixed_sysclk != dai->sysclk) {
242                         dev_err(dev, "inconsistent fixed sysclk rates (%u vs %u)\n",
243                                 *fixed_sysclk, dai->sysclk);
244                         return -EINVAL;
245                 }
246                 *fixed_sysclk = dai->sysclk;
247         }
248
249         return 0;
250 }
251
252 int asoc_simple_startup(struct snd_pcm_substream *substream)
253 {
254         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
255         struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card);
256         struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num);
257         struct asoc_simple_dai *dai;
258         unsigned int fixed_sysclk = 0;
259         int i1, i2, i;
260         int ret;
261
262         for_each_prop_dai_cpu(props, i1, dai) {
263                 ret = asoc_simple_clk_enable(dai);
264                 if (ret)
265                         goto cpu_err;
266                 ret = asoc_simple_check_fixed_sysclk(rtd->dev, dai, &fixed_sysclk);
267                 if (ret)
268                         goto cpu_err;
269         }
270
271         for_each_prop_dai_codec(props, i2, dai) {
272                 ret = asoc_simple_clk_enable(dai);
273                 if (ret)
274                         goto codec_err;
275                 ret = asoc_simple_check_fixed_sysclk(rtd->dev, dai, &fixed_sysclk);
276                 if (ret)
277                         goto codec_err;
278         }
279
280         if (fixed_sysclk && props->mclk_fs) {
281                 unsigned int fixed_rate = fixed_sysclk / props->mclk_fs;
282
283                 if (fixed_sysclk % props->mclk_fs) {
284                         dev_err(rtd->dev, "fixed sysclk %u not divisible by mclk_fs %u\n",
285                                 fixed_sysclk, props->mclk_fs);
286                         return -EINVAL;
287                 }
288                 ret = snd_pcm_hw_constraint_minmax(substream->runtime, SNDRV_PCM_HW_PARAM_RATE,
289                         fixed_rate, fixed_rate);
290                 if (ret)
291                         goto codec_err;
292         }
293
294         return 0;
295
296 codec_err:
297         for_each_prop_dai_codec(props, i, dai) {
298                 if (i >= i2)
299                         break;
300                 asoc_simple_clk_disable(dai);
301         }
302 cpu_err:
303         for_each_prop_dai_cpu(props, i, dai) {
304                 if (i >= i1)
305                         break;
306                 asoc_simple_clk_disable(dai);
307         }
308         return ret;
309 }
310 EXPORT_SYMBOL_GPL(asoc_simple_startup);
311
312 void asoc_simple_shutdown(struct snd_pcm_substream *substream)
313 {
314         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
315         struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card);
316         struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num);
317         struct asoc_simple_dai *dai;
318         int i;
319
320         for_each_prop_dai_cpu(props, i, dai) {
321                 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, i);
322
323                 if (props->mclk_fs && !dai->clk_fixed && !snd_soc_dai_active(cpu_dai))
324                         snd_soc_dai_set_sysclk(cpu_dai,
325                                                0, 0, SND_SOC_CLOCK_IN);
326
327                 asoc_simple_clk_disable(dai);
328         }
329         for_each_prop_dai_codec(props, i, dai) {
330                 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, i);
331
332                 if (props->mclk_fs && !dai->clk_fixed && !snd_soc_dai_active(codec_dai))
333                         snd_soc_dai_set_sysclk(codec_dai,
334                                                0, 0, SND_SOC_CLOCK_IN);
335
336                 asoc_simple_clk_disable(dai);
337         }
338 }
339 EXPORT_SYMBOL_GPL(asoc_simple_shutdown);
340
341 static int asoc_simple_set_clk_rate(struct device *dev,
342                                     struct asoc_simple_dai *simple_dai,
343                                     unsigned long rate)
344 {
345         if (!simple_dai)
346                 return 0;
347
348         if (simple_dai->clk_fixed && rate != simple_dai->sysclk) {
349                 dev_err(dev, "dai %s invalid clock rate %lu\n", simple_dai->name, rate);
350                 return -EINVAL;
351         }
352
353         if (!simple_dai->clk)
354                 return 0;
355
356         if (clk_get_rate(simple_dai->clk) == rate)
357                 return 0;
358
359         return clk_set_rate(simple_dai->clk, rate);
360 }
361
362 static int asoc_simple_set_tdm(struct snd_soc_dai *dai,
363                                 struct asoc_simple_dai *simple_dai,
364                                 struct snd_pcm_hw_params *params)
365 {
366         int sample_bits = params_width(params);
367         int slot_width = simple_dai->slot_width;
368         int slot_count = simple_dai->slots;
369         int i, ret;
370
371         if (!simple_dai || !simple_dai->tdm_width_map)
372                 return 0;
373
374         if (slot_width == 0)
375                 slot_width = sample_bits;
376
377         for (i = 0; i < simple_dai->n_tdm_widths; ++i) {
378                 if (simple_dai->tdm_width_map[i].sample_bits == sample_bits) {
379                         slot_width = simple_dai->tdm_width_map[i].slot_width;
380                         slot_count = simple_dai->tdm_width_map[i].slot_count;
381                         break;
382                 }
383         }
384
385         ret = snd_soc_dai_set_tdm_slot(dai,
386                                        simple_dai->tx_slot_mask,
387                                        simple_dai->rx_slot_mask,
388                                        slot_count,
389                                        slot_width);
390         if (ret && ret != -ENOTSUPP) {
391                 dev_err(dai->dev, "simple-card: set_tdm_slot error: %d\n", ret);
392                 return ret;
393         }
394
395         return 0;
396 }
397
398 int asoc_simple_hw_params(struct snd_pcm_substream *substream,
399                           struct snd_pcm_hw_params *params)
400 {
401         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
402         struct asoc_simple_dai *pdai;
403         struct snd_soc_dai *sdai;
404         struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card);
405         struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num);
406         unsigned int mclk, mclk_fs = 0;
407         int i, ret;
408
409         if (props->mclk_fs)
410                 mclk_fs = props->mclk_fs;
411
412         if (mclk_fs) {
413                 struct snd_soc_component *component;
414                 mclk = params_rate(params) * mclk_fs;
415
416                 for_each_prop_dai_codec(props, i, pdai) {
417                         ret = asoc_simple_set_clk_rate(rtd->dev, pdai, mclk);
418                         if (ret < 0)
419                                 return ret;
420                 }
421
422                 for_each_prop_dai_cpu(props, i, pdai) {
423                         ret = asoc_simple_set_clk_rate(rtd->dev, pdai, mclk);
424                         if (ret < 0)
425                                 return ret;
426                 }
427
428                 /* Ensure sysclk is set on all components in case any
429                  * (such as platform components) are missed by calls to
430                  * snd_soc_dai_set_sysclk.
431                  */
432                 for_each_rtd_components(rtd, i, component) {
433                         ret = snd_soc_component_set_sysclk(component, 0, 0,
434                                                            mclk, SND_SOC_CLOCK_IN);
435                         if (ret && ret != -ENOTSUPP)
436                                 return ret;
437                 }
438
439                 for_each_rtd_codec_dais(rtd, i, sdai) {
440                         ret = snd_soc_dai_set_sysclk(sdai, 0, mclk, SND_SOC_CLOCK_IN);
441                         if (ret && ret != -ENOTSUPP)
442                                 return ret;
443                 }
444
445                 for_each_rtd_cpu_dais(rtd, i, sdai) {
446                         ret = snd_soc_dai_set_sysclk(sdai, 0, mclk, SND_SOC_CLOCK_OUT);
447                         if (ret && ret != -ENOTSUPP)
448                                 return ret;
449                 }
450         }
451
452         for_each_prop_dai_codec(props, i, pdai) {
453                 sdai = asoc_rtd_to_codec(rtd, i);
454                 ret = asoc_simple_set_tdm(sdai, pdai, params);
455                 if (ret < 0)
456                         return ret;
457         }
458
459         for_each_prop_dai_cpu(props, i, pdai) {
460                 sdai = asoc_rtd_to_cpu(rtd, i);
461                 ret = asoc_simple_set_tdm(sdai, pdai, params);
462                 if (ret < 0)
463                         return ret;
464         }
465
466         return 0;
467 }
468 EXPORT_SYMBOL_GPL(asoc_simple_hw_params);
469
470 int asoc_simple_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
471                                    struct snd_pcm_hw_params *params)
472 {
473         struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card);
474         struct simple_dai_props *dai_props = simple_priv_to_props(priv, rtd->num);
475
476         asoc_simple_convert_fixup(&dai_props->adata, params);
477
478         return 0;
479 }
480 EXPORT_SYMBOL_GPL(asoc_simple_be_hw_params_fixup);
481
482 static int asoc_simple_init_dai(struct snd_soc_dai *dai,
483                                      struct asoc_simple_dai *simple_dai)
484 {
485         int ret;
486
487         if (!simple_dai)
488                 return 0;
489
490         if (simple_dai->sysclk) {
491                 ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk,
492                                              simple_dai->clk_direction);
493                 if (ret && ret != -ENOTSUPP) {
494                         dev_err(dai->dev, "simple-card: set_sysclk error\n");
495                         return ret;
496                 }
497         }
498
499         if (simple_dai->slots) {
500                 ret = snd_soc_dai_set_tdm_slot(dai,
501                                                simple_dai->tx_slot_mask,
502                                                simple_dai->rx_slot_mask,
503                                                simple_dai->slots,
504                                                simple_dai->slot_width);
505                 if (ret && ret != -ENOTSUPP) {
506                         dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
507                         return ret;
508                 }
509         }
510
511         return 0;
512 }
513
514 static int asoc_simple_init_dai_link_params(struct snd_soc_pcm_runtime *rtd,
515                                             struct simple_dai_props *dai_props)
516 {
517         struct snd_soc_dai_link *dai_link = rtd->dai_link;
518         struct snd_soc_component *component;
519         struct snd_soc_pcm_stream *params;
520         struct snd_pcm_hardware hw;
521         int i, ret, stream;
522
523         /* Only Codecs */
524         for_each_rtd_components(rtd, i, component) {
525                 if (!snd_soc_component_is_codec(component))
526                         return 0;
527         }
528
529         /* Assumes the capabilities are the same for all supported streams */
530         for_each_pcm_streams(stream) {
531                 ret = snd_soc_runtime_calc_hw(rtd, &hw, stream);
532                 if (ret == 0)
533                         break;
534         }
535
536         if (ret < 0) {
537                 dev_err(rtd->dev, "simple-card: no valid dai_link params\n");
538                 return ret;
539         }
540
541         params = devm_kzalloc(rtd->dev, sizeof(*params), GFP_KERNEL);
542         if (!params)
543                 return -ENOMEM;
544
545         params->formats = hw.formats;
546         params->rates = hw.rates;
547         params->rate_min = hw.rate_min;
548         params->rate_max = hw.rate_max;
549         params->channels_min = hw.channels_min;
550         params->channels_max = hw.channels_max;
551
552         dai_link->params = params;
553         dai_link->num_params = 1;
554
555         return 0;
556 }
557
558 int asoc_simple_dai_init(struct snd_soc_pcm_runtime *rtd)
559 {
560         struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card);
561         struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num);
562         struct asoc_simple_dai *dai;
563         int i, ret;
564
565         for_each_prop_dai_codec(props, i, dai) {
566                 ret = asoc_simple_init_dai(asoc_rtd_to_codec(rtd, i), dai);
567                 if (ret < 0)
568                         return ret;
569         }
570         for_each_prop_dai_cpu(props, i, dai) {
571                 ret = asoc_simple_init_dai(asoc_rtd_to_cpu(rtd, i), dai);
572                 if (ret < 0)
573                         return ret;
574         }
575
576         ret = asoc_simple_init_dai_link_params(rtd, props);
577         if (ret < 0)
578                 return ret;
579
580         return 0;
581 }
582 EXPORT_SYMBOL_GPL(asoc_simple_dai_init);
583
584 void asoc_simple_canonicalize_platform(struct snd_soc_dai_link_component *platforms,
585                                        struct snd_soc_dai_link_component *cpus)
586 {
587         /* Assumes platform == cpu */
588         if (!platforms->of_node)
589                 platforms->of_node = cpus->of_node;
590 }
591 EXPORT_SYMBOL_GPL(asoc_simple_canonicalize_platform);
592
593 void asoc_simple_canonicalize_cpu(struct snd_soc_dai_link_component *cpus,
594                                   int is_single_links)
595 {
596         /*
597          * In soc_bind_dai_link() will check cpu name after
598          * of_node matching if dai_link has cpu_dai_name.
599          * but, it will never match if name was created by
600          * fmt_single_name() remove cpu_dai_name if cpu_args
601          * was 0. See:
602          *      fmt_single_name()
603          *      fmt_multiple_name()
604          */
605         if (is_single_links)
606                 cpus->dai_name = NULL;
607 }
608 EXPORT_SYMBOL_GPL(asoc_simple_canonicalize_cpu);
609
610 int asoc_simple_clean_reference(struct snd_soc_card *card)
611 {
612         struct snd_soc_dai_link *dai_link;
613         struct snd_soc_dai_link_component *cpu;
614         struct snd_soc_dai_link_component *codec;
615         int i, j;
616
617         for_each_card_prelinks(card, i, dai_link) {
618                 for_each_link_cpus(dai_link, j, cpu)
619                         of_node_put(cpu->of_node);
620                 for_each_link_codecs(dai_link, j, codec)
621                         of_node_put(codec->of_node);
622         }
623         return 0;
624 }
625 EXPORT_SYMBOL_GPL(asoc_simple_clean_reference);
626
627 int asoc_simple_parse_routing(struct snd_soc_card *card,
628                               char *prefix)
629 {
630         struct device_node *node = card->dev->of_node;
631         char prop[128];
632
633         if (!prefix)
634                 prefix = "";
635
636         snprintf(prop, sizeof(prop), "%s%s", prefix, "routing");
637
638         if (!of_property_read_bool(node, prop))
639                 return 0;
640
641         return snd_soc_of_parse_audio_routing(card, prop);
642 }
643 EXPORT_SYMBOL_GPL(asoc_simple_parse_routing);
644
645 int asoc_simple_parse_widgets(struct snd_soc_card *card,
646                               char *prefix)
647 {
648         struct device_node *node = card->dev->of_node;
649         char prop[128];
650
651         if (!prefix)
652                 prefix = "";
653
654         snprintf(prop, sizeof(prop), "%s%s", prefix, "widgets");
655
656         if (of_property_read_bool(node, prop))
657                 return snd_soc_of_parse_audio_simple_widgets(card, prop);
658
659         /* no widgets is not error */
660         return 0;
661 }
662 EXPORT_SYMBOL_GPL(asoc_simple_parse_widgets);
663
664 int asoc_simple_parse_pin_switches(struct snd_soc_card *card,
665                                    char *prefix)
666 {
667         char prop[128];
668
669         if (!prefix)
670                 prefix = "";
671
672         snprintf(prop, sizeof(prop), "%s%s", prefix, "pin-switches");
673
674         return snd_soc_of_parse_pin_switches(card, prop);
675 }
676 EXPORT_SYMBOL_GPL(asoc_simple_parse_pin_switches);
677
678 int asoc_simple_init_jack(struct snd_soc_card *card,
679                           struct asoc_simple_jack *sjack,
680                           int is_hp, char *prefix,
681                           char *pin)
682 {
683         struct device *dev = card->dev;
684         enum of_gpio_flags flags;
685         char prop[128];
686         char *pin_name;
687         char *gpio_name;
688         int mask;
689         int det;
690
691         if (!prefix)
692                 prefix = "";
693
694         sjack->gpio.gpio = -ENOENT;
695
696         if (is_hp) {
697                 snprintf(prop, sizeof(prop), "%shp-det-gpio", prefix);
698                 pin_name        = pin ? pin : "Headphones";
699                 gpio_name       = "Headphone detection";
700                 mask            = SND_JACK_HEADPHONE;
701         } else {
702                 snprintf(prop, sizeof(prop), "%smic-det-gpio", prefix);
703                 pin_name        = pin ? pin : "Mic Jack";
704                 gpio_name       = "Mic detection";
705                 mask            = SND_JACK_MICROPHONE;
706         }
707
708         det = of_get_named_gpio_flags(dev->of_node, prop, 0, &flags);
709         if (det == -EPROBE_DEFER)
710                 return -EPROBE_DEFER;
711
712         if (gpio_is_valid(det)) {
713                 sjack->pin.pin          = pin_name;
714                 sjack->pin.mask         = mask;
715
716                 sjack->gpio.name        = gpio_name;
717                 sjack->gpio.report      = mask;
718                 sjack->gpio.gpio        = det;
719                 sjack->gpio.invert      = !!(flags & OF_GPIO_ACTIVE_LOW);
720                 sjack->gpio.debounce_time = 150;
721
722                 snd_soc_card_jack_new(card, pin_name, mask,
723                                       &sjack->jack,
724                                       &sjack->pin, 1);
725
726                 snd_soc_jack_add_gpios(&sjack->jack, 1,
727                                        &sjack->gpio);
728         }
729
730         return 0;
731 }
732 EXPORT_SYMBOL_GPL(asoc_simple_init_jack);
733
734 int asoc_simple_init_priv(struct asoc_simple_priv *priv,
735                           struct link_info *li)
736 {
737         struct snd_soc_card *card = simple_priv_to_card(priv);
738         struct device *dev = simple_priv_to_dev(priv);
739         struct snd_soc_dai_link *dai_link;
740         struct simple_dai_props *dai_props;
741         struct asoc_simple_dai *dais;
742         struct snd_soc_dai_link_component *dlcs;
743         struct snd_soc_codec_conf *cconf = NULL;
744         struct snd_soc_pcm_stream *c2c_conf = NULL;
745         int i, dai_num = 0, dlc_num = 0, cnf_num = 0, c2c_num = 0;
746
747         dai_props = devm_kcalloc(dev, li->link, sizeof(*dai_props), GFP_KERNEL);
748         dai_link  = devm_kcalloc(dev, li->link, sizeof(*dai_link),  GFP_KERNEL);
749         if (!dai_props || !dai_link)
750                 return -ENOMEM;
751
752         /*
753          * dais (= CPU+Codec)
754          * dlcs (= CPU+Codec+Platform)
755          */
756         for (i = 0; i < li->link; i++) {
757                 int cc = li->num[i].cpus + li->num[i].codecs;
758
759                 dai_num += cc;
760                 dlc_num += cc + li->num[i].platforms;
761
762                 if (!li->num[i].cpus)
763                         cnf_num += li->num[i].codecs;
764
765                 c2c_num += li->num[i].c2c;
766         }
767
768         dais = devm_kcalloc(dev, dai_num, sizeof(*dais), GFP_KERNEL);
769         dlcs = devm_kcalloc(dev, dlc_num, sizeof(*dlcs), GFP_KERNEL);
770         if (!dais || !dlcs)
771                 return -ENOMEM;
772
773         if (cnf_num) {
774                 cconf = devm_kcalloc(dev, cnf_num, sizeof(*cconf), GFP_KERNEL);
775                 if (!cconf)
776                         return -ENOMEM;
777         }
778
779         if (c2c_num) {
780                 c2c_conf = devm_kcalloc(dev, c2c_num, sizeof(*c2c_conf), GFP_KERNEL);
781                 if (!c2c_conf)
782                         return -ENOMEM;
783         }
784
785         dev_dbg(dev, "link %d, dais %d, ccnf %d\n",
786                 li->link, dai_num, cnf_num);
787
788         /* dummy CPU/Codec */
789         priv->dummy.of_node     = NULL;
790         priv->dummy.dai_name    = "snd-soc-dummy-dai";
791         priv->dummy.name        = "snd-soc-dummy";
792
793         priv->dai_props         = dai_props;
794         priv->dai_link          = dai_link;
795         priv->dais              = dais;
796         priv->dlcs              = dlcs;
797         priv->codec_conf        = cconf;
798         priv->c2c_conf          = c2c_conf;
799
800         card->dai_link          = priv->dai_link;
801         card->num_links         = li->link;
802         card->codec_conf        = cconf;
803         card->num_configs       = cnf_num;
804
805         for (i = 0; i < li->link; i++) {
806                 if (li->num[i].cpus) {
807                         /* Normal CPU */
808                         dai_props[i].cpus       =
809                         dai_link[i].cpus        = dlcs;
810                         dai_props[i].num.cpus   =
811                         dai_link[i].num_cpus    = li->num[i].cpus;
812                         dai_props[i].cpu_dai    = dais;
813
814                         dlcs += li->num[i].cpus;
815                         dais += li->num[i].cpus;
816
817                         if (li->num[i].c2c) {
818                                 /* Codec2Codec */
819                                 dai_props[i].c2c_conf = c2c_conf;
820                                 c2c_conf += li->num[i].c2c;
821                         }
822                 } else {
823                         /* DPCM Be's CPU = dummy */
824                         dai_props[i].cpus       =
825                         dai_link[i].cpus        = &priv->dummy;
826                         dai_props[i].num.cpus   =
827                         dai_link[i].num_cpus    = 1;
828                 }
829
830                 if (li->num[i].codecs) {
831                         /* Normal Codec */
832                         dai_props[i].codecs     =
833                         dai_link[i].codecs      = dlcs;
834                         dai_props[i].num.codecs =
835                         dai_link[i].num_codecs  = li->num[i].codecs;
836                         dai_props[i].codec_dai  = dais;
837
838                         dlcs += li->num[i].codecs;
839                         dais += li->num[i].codecs;
840
841                         if (!li->num[i].cpus) {
842                                 /* DPCM Be's Codec */
843                                 dai_props[i].codec_conf = cconf;
844                                 cconf += li->num[i].codecs;
845                         }
846                 } else {
847                         /* DPCM Fe's Codec = dummy */
848                         dai_props[i].codecs     =
849                         dai_link[i].codecs      = &priv->dummy;
850                         dai_props[i].num.codecs =
851                         dai_link[i].num_codecs  = 1;
852                 }
853
854                 if (li->num[i].platforms) {
855                         /* Have Platform */
856                         dai_props[i].platforms          =
857                         dai_link[i].platforms           = dlcs;
858                         dai_props[i].num.platforms      =
859                         dai_link[i].num_platforms       = li->num[i].platforms;
860
861                         dlcs += li->num[i].platforms;
862                 } else {
863                         /* Doesn't have Platform */
864                         dai_props[i].platforms          =
865                         dai_link[i].platforms           = NULL;
866                         dai_props[i].num.platforms      =
867                         dai_link[i].num_platforms       = 0;
868                 }
869         }
870
871         return 0;
872 }
873 EXPORT_SYMBOL_GPL(asoc_simple_init_priv);
874
875 int asoc_simple_remove(struct platform_device *pdev)
876 {
877         struct snd_soc_card *card = platform_get_drvdata(pdev);
878
879         return asoc_simple_clean_reference(card);
880 }
881 EXPORT_SYMBOL_GPL(asoc_simple_remove);
882
883 int asoc_graph_card_probe(struct snd_soc_card *card)
884 {
885         struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(card);
886         int ret;
887
888         ret = asoc_simple_init_hp(card, &priv->hp_jack, NULL);
889         if (ret < 0)
890                 return ret;
891
892         ret = asoc_simple_init_mic(card, &priv->mic_jack, NULL);
893         if (ret < 0)
894                 return ret;
895
896         return 0;
897 }
898 EXPORT_SYMBOL_GPL(asoc_graph_card_probe);
899
900 int asoc_graph_is_ports0(struct device_node *np)
901 {
902         struct device_node *port, *ports, *ports0, *top;
903         int ret;
904
905         /* np is "endpoint" or "port" */
906         if (of_node_name_eq(np, "endpoint")) {
907                 port = of_get_parent(np);
908         } else {
909                 port = np;
910                 of_node_get(port);
911         }
912
913         ports   = of_get_parent(port);
914         top     = of_get_parent(ports);
915         ports0  = of_get_child_by_name(top, "ports");
916
917         ret = ports0 == ports;
918
919         of_node_put(port);
920         of_node_put(ports);
921         of_node_put(ports0);
922         of_node_put(top);
923
924         return ret;
925 }
926 EXPORT_SYMBOL_GPL(asoc_graph_is_ports0);
927
928 /* Module information */
929 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
930 MODULE_DESCRIPTION("ALSA SoC Simple Card Utils");
931 MODULE_LICENSE("GPL v2");