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