ASoC: simple-card: add new asoc_simple_jack and use it
[deliverable/linux.git] / sound / soc / generic / simple-card.c
1 /*
2 * ASoC simple sound card support
3 *
4 * Copyright (C) 2012 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11 #include <linux/clk.h>
12 #include <linux/device.h>
13 #include <linux/gpio.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/of_gpio.h>
17 #include <linux/platform_device.h>
18 #include <linux/string.h>
19 #include <sound/jack.h>
20 #include <sound/simple_card.h>
21 #include <sound/soc-dai.h>
22 #include <sound/soc.h>
23
24 struct asoc_simple_jack {
25 struct snd_soc_jack jack;
26 struct snd_soc_jack_pin pin;
27 struct snd_soc_jack_gpio gpio;
28 };
29
30 struct simple_card_data {
31 struct snd_soc_card snd_card;
32 struct simple_dai_props {
33 struct asoc_simple_dai cpu_dai;
34 struct asoc_simple_dai codec_dai;
35 unsigned int mclk_fs;
36 } *dai_props;
37 unsigned int mclk_fs;
38 struct asoc_simple_jack hp_jack;
39 struct asoc_simple_jack mic_jack;
40 struct snd_soc_dai_link dai_link[]; /* dynamically allocated */
41 };
42
43 #define simple_priv_to_dev(priv) ((priv)->snd_card.dev)
44 #define simple_priv_to_link(priv, i) ((priv)->snd_card.dai_link + i)
45 #define simple_priv_to_props(priv, i) ((priv)->dai_props + i)
46
47 #define PREFIX "simple-audio-card,"
48
49 #define asoc_simple_card_init_hp(card, sjack, prefix)\
50 asoc_simple_card_init_jack(card, sjack, 1, prefix)
51 #define asoc_simple_card_init_mic(card, sjack, prefix)\
52 asoc_simple_card_init_jack(card, sjack, 0, prefix)
53 static int asoc_simple_card_init_jack(struct snd_soc_card *card,
54 struct asoc_simple_jack *sjack,
55 int is_hp, char *prefix)
56 {
57 struct device *dev = card->dev;
58 enum of_gpio_flags flags;
59 char prop[128];
60 char *pin_name;
61 char *gpio_name;
62 int mask;
63 int det;
64
65 sjack->gpio.gpio = -ENOENT;
66
67 if (is_hp) {
68 snprintf(prop, sizeof(prop), "%shp-det-gpio", prefix);
69 pin_name = "Headphones";
70 gpio_name = "Headphone detection";
71 mask = SND_JACK_HEADPHONE;
72 } else {
73 snprintf(prop, sizeof(prop), "%smic-det-gpio", prefix);
74 pin_name = "Mic Jack";
75 gpio_name = "Mic detection";
76 mask = SND_JACK_MICROPHONE;
77 }
78
79 det = of_get_named_gpio_flags(dev->of_node, prop, 0, &flags);
80 if (det == -EPROBE_DEFER)
81 return -EPROBE_DEFER;
82
83 if (gpio_is_valid(det)) {
84 sjack->pin.pin = pin_name;
85 sjack->pin.mask = mask;
86
87 sjack->gpio.name = gpio_name;
88 sjack->gpio.report = mask;
89 sjack->gpio.gpio = det;
90 sjack->gpio.invert = !!(flags & OF_GPIO_ACTIVE_LOW);
91 sjack->gpio.debounce_time = 150;
92
93 snd_soc_card_jack_new(card, pin_name, mask,
94 &sjack->jack,
95 &sjack->pin, 1);
96
97 snd_soc_jack_add_gpios(&sjack->jack, 1,
98 &sjack->gpio);
99 }
100
101 return 0;
102 }
103
104 static void asoc_simple_card_remove_jack(struct asoc_simple_jack *sjack)
105 {
106 if (gpio_is_valid(sjack->gpio.gpio))
107 snd_soc_jack_free_gpios(&sjack->jack, 1, &sjack->gpio);
108 }
109
110 static int asoc_simple_card_startup(struct snd_pcm_substream *substream)
111 {
112 struct snd_soc_pcm_runtime *rtd = substream->private_data;
113 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
114 struct simple_dai_props *dai_props =
115 &priv->dai_props[rtd->num];
116 int ret;
117
118 ret = clk_prepare_enable(dai_props->cpu_dai.clk);
119 if (ret)
120 return ret;
121
122 ret = clk_prepare_enable(dai_props->codec_dai.clk);
123 if (ret)
124 clk_disable_unprepare(dai_props->cpu_dai.clk);
125
126 return ret;
127 }
128
129 static void asoc_simple_card_shutdown(struct snd_pcm_substream *substream)
130 {
131 struct snd_soc_pcm_runtime *rtd = substream->private_data;
132 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
133 struct simple_dai_props *dai_props =
134 &priv->dai_props[rtd->num];
135
136 clk_disable_unprepare(dai_props->cpu_dai.clk);
137
138 clk_disable_unprepare(dai_props->codec_dai.clk);
139 }
140
141 static int asoc_simple_card_hw_params(struct snd_pcm_substream *substream,
142 struct snd_pcm_hw_params *params)
143 {
144 struct snd_soc_pcm_runtime *rtd = substream->private_data;
145 struct snd_soc_dai *codec_dai = rtd->codec_dai;
146 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
147 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
148 struct simple_dai_props *dai_props = &priv->dai_props[rtd->num];
149 unsigned int mclk, mclk_fs = 0;
150 int ret = 0;
151
152 if (priv->mclk_fs)
153 mclk_fs = priv->mclk_fs;
154 else if (dai_props->mclk_fs)
155 mclk_fs = dai_props->mclk_fs;
156
157 if (mclk_fs) {
158 mclk = params_rate(params) * mclk_fs;
159 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
160 SND_SOC_CLOCK_IN);
161 if (ret && ret != -ENOTSUPP)
162 goto err;
163
164 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
165 SND_SOC_CLOCK_OUT);
166 if (ret && ret != -ENOTSUPP)
167 goto err;
168 }
169 return 0;
170 err:
171 return ret;
172 }
173
174 static struct snd_soc_ops asoc_simple_card_ops = {
175 .startup = asoc_simple_card_startup,
176 .shutdown = asoc_simple_card_shutdown,
177 .hw_params = asoc_simple_card_hw_params,
178 };
179
180 static int __asoc_simple_card_dai_init(struct snd_soc_dai *dai,
181 struct asoc_simple_dai *set)
182 {
183 int ret;
184
185 if (set->sysclk) {
186 ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0);
187 if (ret && ret != -ENOTSUPP) {
188 dev_err(dai->dev, "simple-card: set_sysclk error\n");
189 goto err;
190 }
191 }
192
193 if (set->slots) {
194 ret = snd_soc_dai_set_tdm_slot(dai,
195 set->tx_slot_mask,
196 set->rx_slot_mask,
197 set->slots,
198 set->slot_width);
199 if (ret && ret != -ENOTSUPP) {
200 dev_err(dai->dev, "simple-card: set_tdm_slot error\n");
201 goto err;
202 }
203 }
204
205 ret = 0;
206
207 err:
208 return ret;
209 }
210
211 static int asoc_simple_card_dai_init(struct snd_soc_pcm_runtime *rtd)
212 {
213 struct simple_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
214 struct snd_soc_dai *codec = rtd->codec_dai;
215 struct snd_soc_dai *cpu = rtd->cpu_dai;
216 struct simple_dai_props *dai_props;
217 int ret;
218
219 dai_props = &priv->dai_props[rtd->num];
220 ret = __asoc_simple_card_dai_init(codec, &dai_props->codec_dai);
221 if (ret < 0)
222 return ret;
223
224 ret = __asoc_simple_card_dai_init(cpu, &dai_props->cpu_dai);
225 if (ret < 0)
226 return ret;
227
228 ret = asoc_simple_card_init_hp(rtd->card, &priv->hp_jack, PREFIX);
229 if (ret < 0)
230 return ret;
231
232 ret = asoc_simple_card_init_mic(rtd->card, &priv->hp_jack, PREFIX);
233 if (ret < 0)
234 return ret;
235
236 return 0;
237 }
238
239 static int
240 asoc_simple_card_sub_parse_of(struct device_node *np,
241 struct asoc_simple_dai *dai,
242 struct device_node **p_node,
243 const char **name,
244 int *args_count)
245 {
246 struct of_phandle_args args;
247 struct clk *clk;
248 u32 val;
249 int ret;
250
251 if (!np)
252 return 0;
253
254 /*
255 * Get node via "sound-dai = <&phandle port>"
256 * it will be used as xxx_of_node on soc_bind_dai_link()
257 */
258 ret = of_parse_phandle_with_args(np, "sound-dai",
259 "#sound-dai-cells", 0, &args);
260 if (ret)
261 return ret;
262
263 *p_node = args.np;
264
265 if (args_count)
266 *args_count = args.args_count;
267
268 /* Get dai->name */
269 if (name) {
270 ret = snd_soc_of_get_dai_name(np, name);
271 if (ret < 0)
272 return ret;
273 }
274
275 if (!dai)
276 return 0;
277
278 /* Parse TDM slot */
279 ret = snd_soc_of_parse_tdm_slot(np, &dai->tx_slot_mask,
280 &dai->rx_slot_mask,
281 &dai->slots, &dai->slot_width);
282 if (ret)
283 return ret;
284
285 /*
286 * Parse dai->sysclk come from "clocks = <&xxx>"
287 * (if system has common clock)
288 * or "system-clock-frequency = <xxx>"
289 * or device's module clock.
290 */
291 if (of_property_read_bool(np, "clocks")) {
292 clk = of_clk_get(np, 0);
293 if (IS_ERR(clk)) {
294 ret = PTR_ERR(clk);
295 return ret;
296 }
297
298 dai->sysclk = clk_get_rate(clk);
299 dai->clk = clk;
300 } else if (!of_property_read_u32(np, "system-clock-frequency", &val)) {
301 dai->sysclk = val;
302 } else {
303 clk = of_clk_get(args.np, 0);
304 if (!IS_ERR(clk))
305 dai->sysclk = clk_get_rate(clk);
306 }
307
308 return 0;
309 }
310
311 static int asoc_simple_card_parse_daifmt(struct device_node *node,
312 struct simple_card_data *priv,
313 struct device_node *codec,
314 char *prefix, int idx)
315 {
316 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, idx);
317 struct device *dev = simple_priv_to_dev(priv);
318 struct device_node *bitclkmaster = NULL;
319 struct device_node *framemaster = NULL;
320 unsigned int daifmt;
321
322 daifmt = snd_soc_of_parse_daifmt(node, prefix,
323 &bitclkmaster, &framemaster);
324 daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
325
326 if (strlen(prefix) && !bitclkmaster && !framemaster) {
327 /*
328 * No dai-link level and master setting was not found from
329 * sound node level, revert back to legacy DT parsing and
330 * take the settings from codec node.
331 */
332 dev_dbg(dev, "Revert to legacy daifmt parsing\n");
333
334 daifmt = snd_soc_of_parse_daifmt(codec, NULL, NULL, NULL) |
335 (daifmt & ~SND_SOC_DAIFMT_CLOCK_MASK);
336 } else {
337 if (codec == bitclkmaster)
338 daifmt |= (codec == framemaster) ?
339 SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS;
340 else
341 daifmt |= (codec == framemaster) ?
342 SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS;
343 }
344
345 dai_link->dai_fmt = daifmt;
346
347 of_node_put(bitclkmaster);
348 of_node_put(framemaster);
349
350 return 0;
351 }
352
353 static int asoc_simple_card_dai_link_of(struct device_node *node,
354 struct simple_card_data *priv,
355 int idx,
356 bool is_top_level_node)
357 {
358 struct device *dev = simple_priv_to_dev(priv);
359 struct snd_soc_dai_link *dai_link = simple_priv_to_link(priv, idx);
360 struct simple_dai_props *dai_props = simple_priv_to_props(priv, idx);
361 struct device_node *cpu = NULL;
362 struct device_node *plat = NULL;
363 struct device_node *codec = NULL;
364 char *name;
365 char prop[128];
366 char *prefix = "";
367 int ret, cpu_args;
368 u32 val;
369
370 /* For single DAI link & old style of DT node */
371 if (is_top_level_node)
372 prefix = PREFIX;
373
374 snprintf(prop, sizeof(prop), "%scpu", prefix);
375 cpu = of_get_child_by_name(node, prop);
376
377 snprintf(prop, sizeof(prop), "%splat", prefix);
378 plat = of_get_child_by_name(node, prop);
379
380 snprintf(prop, sizeof(prop), "%scodec", prefix);
381 codec = of_get_child_by_name(node, prop);
382
383 if (!cpu || !codec) {
384 ret = -EINVAL;
385 dev_err(dev, "%s: Can't find %s DT node\n", __func__, prop);
386 goto dai_link_of_err;
387 }
388
389 ret = asoc_simple_card_parse_daifmt(node, priv,
390 codec, prefix, idx);
391 if (ret < 0)
392 goto dai_link_of_err;
393
394 if (!of_property_read_u32(node, "mclk-fs", &val))
395 dai_props->mclk_fs = val;
396
397 ret = asoc_simple_card_sub_parse_of(cpu, &dai_props->cpu_dai,
398 &dai_link->cpu_of_node,
399 &dai_link->cpu_dai_name,
400 &cpu_args);
401 if (ret < 0)
402 goto dai_link_of_err;
403
404 ret = asoc_simple_card_sub_parse_of(codec, &dai_props->codec_dai,
405 &dai_link->codec_of_node,
406 &dai_link->codec_dai_name, NULL);
407 if (ret < 0)
408 goto dai_link_of_err;
409
410 ret = asoc_simple_card_sub_parse_of(plat, NULL,
411 &dai_link->platform_of_node,
412 NULL, NULL);
413 if (ret < 0)
414 goto dai_link_of_err;
415
416 if (!dai_link->cpu_dai_name || !dai_link->codec_dai_name) {
417 ret = -EINVAL;
418 goto dai_link_of_err;
419 }
420
421 /* Assumes platform == cpu */
422 if (!dai_link->platform_of_node)
423 dai_link->platform_of_node = dai_link->cpu_of_node;
424
425 /* DAI link name is created from CPU/CODEC dai name */
426 name = devm_kzalloc(dev,
427 strlen(dai_link->cpu_dai_name) +
428 strlen(dai_link->codec_dai_name) + 2,
429 GFP_KERNEL);
430 if (!name) {
431 ret = -ENOMEM;
432 goto dai_link_of_err;
433 }
434
435 sprintf(name, "%s-%s", dai_link->cpu_dai_name,
436 dai_link->codec_dai_name);
437 dai_link->name = dai_link->stream_name = name;
438 dai_link->ops = &asoc_simple_card_ops;
439 dai_link->init = asoc_simple_card_dai_init;
440
441 dev_dbg(dev, "\tname : %s\n", dai_link->stream_name);
442 dev_dbg(dev, "\tformat : %04x\n", dai_link->dai_fmt);
443 dev_dbg(dev, "\tcpu : %s / %d\n",
444 dai_link->cpu_dai_name,
445 dai_props->cpu_dai.sysclk);
446 dev_dbg(dev, "\tcodec : %s / %d\n",
447 dai_link->codec_dai_name,
448 dai_props->codec_dai.sysclk);
449
450 /*
451 * In soc_bind_dai_link() will check cpu name after
452 * of_node matching if dai_link has cpu_dai_name.
453 * but, it will never match if name was created by
454 * fmt_single_name() remove cpu_dai_name if cpu_args
455 * was 0. See:
456 * fmt_single_name()
457 * fmt_multiple_name()
458 */
459 if (!cpu_args)
460 dai_link->cpu_dai_name = NULL;
461
462 dai_link_of_err:
463 of_node_put(cpu);
464 of_node_put(codec);
465
466 return ret;
467 }
468
469 static int asoc_simple_card_parse_of(struct device_node *node,
470 struct simple_card_data *priv)
471 {
472 struct device *dev = simple_priv_to_dev(priv);
473 u32 val;
474 int ret;
475
476 if (!node)
477 return -EINVAL;
478
479 /* Parse the card name from DT */
480 snd_soc_of_parse_card_name(&priv->snd_card, PREFIX "name");
481
482 /* The off-codec widgets */
483 if (of_property_read_bool(node, PREFIX "widgets")) {
484 ret = snd_soc_of_parse_audio_simple_widgets(&priv->snd_card,
485 PREFIX "widgets");
486 if (ret)
487 return ret;
488 }
489
490 /* DAPM routes */
491 if (of_property_read_bool(node, PREFIX "routing")) {
492 ret = snd_soc_of_parse_audio_routing(&priv->snd_card,
493 PREFIX "routing");
494 if (ret)
495 return ret;
496 }
497
498 /* Factor to mclk, used in hw_params() */
499 ret = of_property_read_u32(node, PREFIX "mclk-fs", &val);
500 if (ret == 0)
501 priv->mclk_fs = val;
502
503 dev_dbg(dev, "New simple-card: %s\n", priv->snd_card.name ?
504 priv->snd_card.name : "");
505
506 /* Single/Muti DAI link(s) & New style of DT node */
507 if (of_get_child_by_name(node, PREFIX "dai-link")) {
508 struct device_node *np = NULL;
509 int i = 0;
510
511 for_each_child_of_node(node, np) {
512 dev_dbg(dev, "\tlink %d:\n", i);
513 ret = asoc_simple_card_dai_link_of(np, priv,
514 i, false);
515 if (ret < 0) {
516 of_node_put(np);
517 return ret;
518 }
519 i++;
520 }
521 } else {
522 /* For single DAI link & old style of DT node */
523 ret = asoc_simple_card_dai_link_of(node, priv, 0, true);
524 if (ret < 0)
525 return ret;
526 }
527
528 if (!priv->snd_card.name)
529 priv->snd_card.name = priv->snd_card.dai_link->name;
530
531 return 0;
532 }
533
534 /* Decrease the reference count of the device nodes */
535 static int asoc_simple_card_unref(struct snd_soc_card *card)
536 {
537 struct snd_soc_dai_link *dai_link;
538 int num_links;
539
540 for (num_links = 0, dai_link = card->dai_link;
541 num_links < card->num_links;
542 num_links++, dai_link++) {
543 of_node_put(dai_link->cpu_of_node);
544 of_node_put(dai_link->codec_of_node);
545 }
546 return 0;
547 }
548
549 static int asoc_simple_card_probe(struct platform_device *pdev)
550 {
551 struct simple_card_data *priv;
552 struct snd_soc_dai_link *dai_link;
553 struct device_node *np = pdev->dev.of_node;
554 struct device *dev = &pdev->dev;
555 int num_links, ret;
556
557 /* Get the number of DAI links */
558 if (np && of_get_child_by_name(np, PREFIX "dai-link"))
559 num_links = of_get_child_count(np);
560 else
561 num_links = 1;
562
563 /* Allocate the private data and the DAI link array */
564 priv = devm_kzalloc(dev,
565 sizeof(*priv) + sizeof(*dai_link) * num_links,
566 GFP_KERNEL);
567 if (!priv)
568 return -ENOMEM;
569
570 /* Init snd_soc_card */
571 priv->snd_card.owner = THIS_MODULE;
572 priv->snd_card.dev = dev;
573 dai_link = priv->dai_link;
574 priv->snd_card.dai_link = dai_link;
575 priv->snd_card.num_links = num_links;
576
577 /* Get room for the other properties */
578 priv->dai_props = devm_kzalloc(dev,
579 sizeof(*priv->dai_props) * num_links,
580 GFP_KERNEL);
581 if (!priv->dai_props)
582 return -ENOMEM;
583
584 if (np && of_device_is_available(np)) {
585
586 ret = asoc_simple_card_parse_of(np, priv);
587 if (ret < 0) {
588 if (ret != -EPROBE_DEFER)
589 dev_err(dev, "parse error %d\n", ret);
590 goto err;
591 }
592
593 } else {
594 struct asoc_simple_card_info *cinfo;
595
596 cinfo = dev->platform_data;
597 if (!cinfo) {
598 dev_err(dev, "no info for asoc-simple-card\n");
599 return -EINVAL;
600 }
601
602 if (!cinfo->name ||
603 !cinfo->codec_dai.name ||
604 !cinfo->codec ||
605 !cinfo->platform ||
606 !cinfo->cpu_dai.name) {
607 dev_err(dev, "insufficient asoc_simple_card_info settings\n");
608 return -EINVAL;
609 }
610
611 priv->snd_card.name = (cinfo->card) ? cinfo->card : cinfo->name;
612 dai_link->name = cinfo->name;
613 dai_link->stream_name = cinfo->name;
614 dai_link->platform_name = cinfo->platform;
615 dai_link->codec_name = cinfo->codec;
616 dai_link->cpu_dai_name = cinfo->cpu_dai.name;
617 dai_link->codec_dai_name = cinfo->codec_dai.name;
618 dai_link->dai_fmt = cinfo->daifmt;
619 dai_link->init = asoc_simple_card_dai_init;
620 memcpy(&priv->dai_props->cpu_dai, &cinfo->cpu_dai,
621 sizeof(priv->dai_props->cpu_dai));
622 memcpy(&priv->dai_props->codec_dai, &cinfo->codec_dai,
623 sizeof(priv->dai_props->codec_dai));
624
625 }
626
627 snd_soc_card_set_drvdata(&priv->snd_card, priv);
628
629 ret = devm_snd_soc_register_card(&pdev->dev, &priv->snd_card);
630 if (ret >= 0)
631 return ret;
632
633 err:
634 asoc_simple_card_unref(&priv->snd_card);
635 return ret;
636 }
637
638 static int asoc_simple_card_remove(struct platform_device *pdev)
639 {
640 struct snd_soc_card *card = platform_get_drvdata(pdev);
641 struct simple_card_data *priv = snd_soc_card_get_drvdata(card);
642
643 asoc_simple_card_remove_jack(&priv->hp_jack);
644 asoc_simple_card_remove_jack(&priv->mic_jack);
645
646 return asoc_simple_card_unref(card);
647 }
648
649 static const struct of_device_id asoc_simple_of_match[] = {
650 { .compatible = "simple-audio-card", },
651 {},
652 };
653 MODULE_DEVICE_TABLE(of, asoc_simple_of_match);
654
655 static struct platform_driver asoc_simple_card = {
656 .driver = {
657 .name = "asoc-simple-card",
658 .pm = &snd_soc_pm_ops,
659 .of_match_table = asoc_simple_of_match,
660 },
661 .probe = asoc_simple_card_probe,
662 .remove = asoc_simple_card_remove,
663 };
664
665 module_platform_driver(asoc_simple_card);
666
667 MODULE_ALIAS("platform:asoc-simple-card");
668 MODULE_LICENSE("GPL");
669 MODULE_DESCRIPTION("ASoC Simple Sound Card");
670 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
This page took 0.05597 seconds and 6 git commands to generate.