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