Commit | Line | Data |
---|---|---|
1536a968 KM |
1 | /* |
2 | * Renesas R-Car SRU/SCU/SSIU/SSI support | |
3 | * | |
4 | * Copyright (C) 2013 Renesas Solutions Corp. | |
5 | * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> | |
6 | * | |
7 | * Based on fsi.c | |
8 | * Kuninori Morimoto <morimoto.kuninori@renesas.com> | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License version 2 as | |
12 | * published by the Free Software Foundation. | |
13 | */ | |
14 | ||
15 | /* | |
16 | * Renesas R-Car sound device structure | |
17 | * | |
18 | * Gen1 | |
19 | * | |
20 | * SRU : Sound Routing Unit | |
21 | * - SRC : Sampling Rate Converter | |
22 | * - CMD | |
23 | * - CTU : Channel Count Conversion Unit | |
24 | * - MIX : Mixer | |
25 | * - DVC : Digital Volume and Mute Function | |
26 | * - SSI : Serial Sound Interface | |
27 | * | |
28 | * Gen2 | |
29 | * | |
30 | * SCU : Sampling Rate Converter Unit | |
31 | * - SRC : Sampling Rate Converter | |
32 | * - CMD | |
33 | * - CTU : Channel Count Conversion Unit | |
34 | * - MIX : Mixer | |
35 | * - DVC : Digital Volume and Mute Function | |
36 | * SSIU : Serial Sound Interface Unit | |
37 | * - SSI : Serial Sound Interface | |
38 | */ | |
39 | ||
40 | /* | |
41 | * driver data Image | |
42 | * | |
43 | * rsnd_priv | |
44 | * | | |
45 | * | ** this depends on Gen1/Gen2 | |
46 | * | | |
47 | * +- gen | |
48 | * | | |
49 | * | ** these depend on data path | |
50 | * | ** gen and platform data control it | |
51 | * | | |
52 | * +- rdai[0] | |
53 | * | | sru ssiu ssi | |
54 | * | +- playback -> [mod] -> [mod] -> [mod] -> ... | |
55 | * | | | |
56 | * | | sru ssiu ssi | |
57 | * | +- capture -> [mod] -> [mod] -> [mod] -> ... | |
58 | * | | |
59 | * +- rdai[1] | |
60 | * | | sru ssiu ssi | |
61 | * | +- playback -> [mod] -> [mod] -> [mod] -> ... | |
62 | * | | | |
63 | * | | sru ssiu ssi | |
64 | * | +- capture -> [mod] -> [mod] -> [mod] -> ... | |
65 | * ... | |
66 | * | | |
67 | * | ** these control ssi | |
68 | * | | |
69 | * +- ssi | |
70 | * | | | |
71 | * | +- ssi[0] | |
72 | * | +- ssi[1] | |
73 | * | +- ssi[2] | |
74 | * | ... | |
75 | * | | |
ba9c949f | 76 | * | ** these control src |
1536a968 | 77 | * | |
ba9c949f | 78 | * +- src |
1536a968 | 79 | * | |
ba9c949f KM |
80 | * +- src[0] |
81 | * +- src[1] | |
82 | * +- src[2] | |
1536a968 KM |
83 | * ... |
84 | * | |
85 | * | |
86 | * for_each_rsnd_dai(xx, priv, xx) | |
87 | * rdai[0] => rdai[1] => rdai[2] => ... | |
88 | * | |
89 | * for_each_rsnd_mod(xx, rdai, xx) | |
90 | * [mod] => [mod] => [mod] => ... | |
91 | * | |
92 | * rsnd_dai_call(xxx, fn ) | |
93 | * [mod]->fn() -> [mod]->fn() -> [mod]->fn()... | |
94 | * | |
95 | */ | |
96 | #include <linux/pm_runtime.h> | |
97 | #include "rsnd.h" | |
98 | ||
99 | #define RSND_RATES SNDRV_PCM_RATE_8000_96000 | |
100 | #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE) | |
101 | ||
969b8619 | 102 | static const struct rsnd_of_data rsnd_of_data_gen1 = { |
90e8e50f KM |
103 | .flags = RSND_GEN1, |
104 | }; | |
105 | ||
969b8619 | 106 | static const struct rsnd_of_data rsnd_of_data_gen2 = { |
90e8e50f KM |
107 | .flags = RSND_GEN2, |
108 | }; | |
109 | ||
33187fb4 | 110 | static const struct of_device_id rsnd_of_match[] = { |
90e8e50f KM |
111 | { .compatible = "renesas,rcar_sound-gen1", .data = &rsnd_of_data_gen1 }, |
112 | { .compatible = "renesas,rcar_sound-gen2", .data = &rsnd_of_data_gen2 }, | |
ac37a45b | 113 | { .compatible = "renesas,rcar_sound-gen3", .data = &rsnd_of_data_gen2 }, /* gen2 compatible */ |
90e8e50f KM |
114 | {}, |
115 | }; | |
116 | MODULE_DEVICE_TABLE(of, rsnd_of_match); | |
117 | ||
1536a968 KM |
118 | /* |
119 | * rsnd_platform functions | |
120 | */ | |
121 | #define rsnd_platform_call(priv, dai, func, param...) \ | |
740ad6c3 | 122 | (!(priv->info->func) ? 0 : \ |
1536a968 KM |
123 | priv->info->func(param)) |
124 | ||
389933d9 KM |
125 | #define rsnd_is_enable_path(io, name) \ |
126 | ((io)->info ? (io)->info->name : NULL) | |
127 | #define rsnd_info_id(priv, io, name) \ | |
128 | ((io)->info->name - priv->info->name##_info) | |
129 | ||
f1df1229 KM |
130 | void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type) |
131 | { | |
132 | if (mod->type != type) { | |
133 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); | |
134 | struct device *dev = rsnd_priv_to_dev(priv); | |
135 | ||
136 | dev_warn(dev, "%s[%d] is not your expected module\n", | |
137 | rsnd_mod_name(mod), rsnd_mod_id(mod)); | |
138 | } | |
139 | } | |
140 | ||
cdaa3cdf KM |
141 | /* |
142 | * rsnd_mod functions | |
143 | */ | |
144 | char *rsnd_mod_name(struct rsnd_mod *mod) | |
145 | { | |
146 | if (!mod || !mod->ops) | |
147 | return "unknown"; | |
148 | ||
149 | return mod->ops->name; | |
150 | } | |
151 | ||
9b99e9a7 KM |
152 | struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io, |
153 | struct rsnd_mod *mod) | |
d9288d0b | 154 | { |
72adc61f KM |
155 | if (!mod || !mod->ops || !mod->ops->dma_req) |
156 | return NULL; | |
d9288d0b | 157 | |
9b99e9a7 | 158 | return mod->ops->dma_req(io, mod); |
d9288d0b KM |
159 | } |
160 | ||
2099bc8e KM |
161 | int rsnd_mod_init(struct rsnd_priv *priv, |
162 | struct rsnd_mod *mod, | |
cdaa3cdf | 163 | struct rsnd_mod_ops *ops, |
85642952 | 164 | struct clk *clk, |
a126021d | 165 | enum rsnd_mod_type type, |
cdaa3cdf KM |
166 | int id) |
167 | { | |
2f78dd7f KM |
168 | int ret = clk_prepare(clk); |
169 | ||
170 | if (ret) | |
171 | return ret; | |
172 | ||
cdaa3cdf KM |
173 | mod->id = id; |
174 | mod->ops = ops; | |
a126021d | 175 | mod->type = type; |
85642952 | 176 | mod->clk = clk; |
2099bc8e | 177 | mod->priv = priv; |
2f78dd7f KM |
178 | |
179 | return ret; | |
180 | } | |
181 | ||
182 | void rsnd_mod_quit(struct rsnd_mod *mod) | |
183 | { | |
184 | if (mod->clk) | |
185 | clk_unprepare(mod->clk); | |
cdaa3cdf KM |
186 | } |
187 | ||
f501b7a4 KM |
188 | void rsnd_mod_interrupt(struct rsnd_mod *mod, |
189 | void (*callback)(struct rsnd_mod *mod, | |
190 | struct rsnd_dai_stream *io)) | |
191 | { | |
192 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); | |
193 | struct rsnd_dai_stream *io; | |
194 | struct rsnd_dai *rdai; | |
195 | int i, j; | |
196 | ||
197 | for_each_rsnd_dai(rdai, priv, j) { | |
198 | ||
199 | for (i = 0; i < RSND_MOD_MAX; i++) { | |
200 | io = &rdai->playback; | |
201 | if (mod == io->mod[i]) | |
202 | callback(mod, io); | |
203 | ||
204 | io = &rdai->capture; | |
205 | if (mod == io->mod[i]) | |
206 | callback(mod, io); | |
207 | } | |
208 | } | |
209 | } | |
210 | ||
d5bbe7de | 211 | int rsnd_io_is_working(struct rsnd_dai_stream *io) |
02299d98 | 212 | { |
02299d98 KM |
213 | /* see rsnd_dai_stream_init/quit() */ |
214 | return !!io->substream; | |
215 | } | |
216 | ||
d7bdbc5d | 217 | /* |
3023b384 | 218 | * ADINR function |
d7bdbc5d | 219 | */ |
3023b384 | 220 | u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io) |
d7bdbc5d KM |
221 | { |
222 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); | |
d7bdbc5d KM |
223 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
224 | struct device *dev = rsnd_priv_to_dev(priv); | |
225 | u32 adinr = runtime->channels; | |
226 | ||
227 | switch (runtime->sample_bits) { | |
228 | case 16: | |
229 | adinr |= (8 << 16); | |
230 | break; | |
231 | case 32: | |
232 | adinr |= (0 << 16); | |
233 | break; | |
234 | default: | |
235 | dev_warn(dev, "not supported sample bits\n"); | |
236 | return 0; | |
237 | } | |
238 | ||
239 | return adinr; | |
240 | } | |
241 | ||
bfe1360d KM |
242 | u32 rsnd_get_adinr_chan(struct rsnd_mod *mod, struct rsnd_dai_stream *io) |
243 | { | |
244 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); | |
245 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); | |
246 | struct device *dev = rsnd_priv_to_dev(priv); | |
247 | u32 chan = runtime->channels; | |
248 | ||
249 | switch (chan) { | |
250 | case 1: | |
251 | case 2: | |
252 | case 4: | |
253 | case 6: | |
254 | case 8: | |
255 | break; | |
256 | default: | |
257 | dev_warn(dev, "not supported channel\n"); | |
258 | chan = 0; | |
259 | break; | |
260 | } | |
261 | ||
262 | return chan; | |
263 | } | |
4689032b KM |
264 | |
265 | /* | |
266 | * DALIGN function | |
267 | */ | |
268 | u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io) | |
269 | { | |
270 | struct rsnd_mod *src = rsnd_io_to_mod_src(io); | |
271 | struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io); | |
272 | struct rsnd_mod *target = src ? src : ssi; | |
273 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); | |
274 | u32 val = 0x76543210; | |
275 | u32 mask = ~0; | |
276 | ||
277 | mask <<= runtime->channels * 4; | |
278 | val = val & mask; | |
279 | ||
280 | switch (runtime->sample_bits) { | |
281 | case 16: | |
282 | val |= 0x67452301 & ~mask; | |
283 | break; | |
284 | case 32: | |
285 | val |= 0x76543210 & ~mask; | |
286 | break; | |
287 | } | |
288 | ||
289 | /* | |
290 | * exchange channeles on SRC if possible, | |
291 | * otherwise, R/L volume settings on DVC | |
292 | * changes inverted channels | |
293 | */ | |
294 | if (mod == target) | |
295 | return val; | |
296 | else | |
297 | return 0x76543210; | |
298 | } | |
299 | ||
1536a968 KM |
300 | /* |
301 | * rsnd_dai functions | |
302 | */ | |
32a96d55 | 303 | #define rsnd_mod_call(mod, io, func, param...) \ |
d870a91e KM |
304 | ({ \ |
305 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \ | |
306 | struct device *dev = rsnd_priv_to_dev(priv); \ | |
5451ea44 KM |
307 | u32 mask = 0xF << __rsnd_mod_shift_##func; \ |
308 | u8 val = (mod->status >> __rsnd_mod_shift_##func) & 0xF; \ | |
309 | u8 add = ((val + __rsnd_mod_add_##func) & 0xF); \ | |
417f9642 | 310 | int ret = 0; \ |
32a96d55 | 311 | int call = (val == __rsnd_mod_call_##func) && (mod)->ops->func; \ |
a48e3f97 KM |
312 | mod->status = (mod->status & ~mask) + \ |
313 | (add << __rsnd_mod_shift_##func); \ | |
1355720a KM |
314 | dev_dbg(dev, "%s[%d]\t0x%08x %s\n", \ |
315 | rsnd_mod_name(mod), rsnd_mod_id(mod), \ | |
316 | mod->status, call ? #func : ""); \ | |
317 | if (call) \ | |
318 | ret = (mod)->ops->func(mod, io, param); \ | |
417f9642 | 319 | ret; \ |
d870a91e KM |
320 | }) |
321 | ||
690602fc | 322 | #define rsnd_dai_call(fn, io, param...) \ |
d870a91e | 323 | ({ \ |
a126021d KM |
324 | struct rsnd_mod *mod; \ |
325 | int ret = 0, i; \ | |
326 | for (i = 0; i < RSND_MOD_MAX; i++) { \ | |
327 | mod = (io)->mod[i]; \ | |
328 | if (!mod) \ | |
329 | continue; \ | |
89e3e2c3 | 330 | ret |= rsnd_mod_call(mod, io, fn, param); \ |
d870a91e KM |
331 | } \ |
332 | ret; \ | |
cdaa3cdf KM |
333 | }) |
334 | ||
a126021d | 335 | static int rsnd_dai_connect(struct rsnd_mod *mod, |
9bfed6cf | 336 | struct rsnd_dai_stream *io) |
cdaa3cdf | 337 | { |
48725e9c KM |
338 | struct rsnd_priv *priv; |
339 | struct device *dev; | |
84e95355 | 340 | |
6020779b | 341 | if (!mod) |
cdaa3cdf | 342 | return -EIO; |
cdaa3cdf | 343 | |
48725e9c KM |
344 | priv = rsnd_mod_to_priv(mod); |
345 | dev = rsnd_priv_to_dev(priv); | |
346 | ||
a126021d | 347 | io->mod[mod->type] = mod; |
cdaa3cdf | 348 | |
84e95355 KM |
349 | dev_dbg(dev, "%s[%d] is connected to io (%s)\n", |
350 | rsnd_mod_name(mod), rsnd_mod_id(mod), | |
351 | rsnd_io_is_play(io) ? "Playback" : "Capture"); | |
352 | ||
cdaa3cdf KM |
353 | return 0; |
354 | } | |
355 | ||
d3a76823 KM |
356 | static void rsnd_dai_disconnect(struct rsnd_mod *mod, |
357 | struct rsnd_dai_stream *io) | |
358 | { | |
d3a76823 KM |
359 | io->mod[mod->type] = NULL; |
360 | } | |
361 | ||
710d0889 | 362 | struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id) |
1536a968 | 363 | { |
ecba9e72 | 364 | if ((id < 0) || (id >= rsnd_rdai_nr(priv))) |
2192f81c KM |
365 | return NULL; |
366 | ||
1536a968 KM |
367 | return priv->rdai + id; |
368 | } | |
369 | ||
eb2535f5 | 370 | #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai) |
1536a968 KM |
371 | static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai) |
372 | { | |
eb2535f5 | 373 | struct rsnd_priv *priv = rsnd_dai_to_priv(dai); |
1536a968 | 374 | |
710d0889 | 375 | return rsnd_rdai_get(priv, dai->id); |
1536a968 KM |
376 | } |
377 | ||
1536a968 KM |
378 | /* |
379 | * rsnd_soc_dai functions | |
380 | */ | |
381 | int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional) | |
382 | { | |
383 | struct snd_pcm_substream *substream = io->substream; | |
384 | struct snd_pcm_runtime *runtime = substream->runtime; | |
385 | int pos = io->byte_pos + additional; | |
386 | ||
387 | pos %= (runtime->periods * io->byte_per_period); | |
388 | ||
389 | return pos; | |
390 | } | |
391 | ||
75defee0 | 392 | bool rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte) |
1536a968 KM |
393 | { |
394 | io->byte_pos += byte; | |
395 | ||
396 | if (io->byte_pos >= io->next_period_byte) { | |
397 | struct snd_pcm_substream *substream = io->substream; | |
398 | struct snd_pcm_runtime *runtime = substream->runtime; | |
399 | ||
400 | io->period_pos++; | |
401 | io->next_period_byte += io->byte_per_period; | |
402 | ||
403 | if (io->period_pos >= runtime->periods) { | |
404 | io->byte_pos = 0; | |
405 | io->period_pos = 0; | |
406 | io->next_period_byte = io->byte_per_period; | |
407 | } | |
408 | ||
75defee0 | 409 | return true; |
1536a968 | 410 | } |
75defee0 KM |
411 | |
412 | return false; | |
413 | } | |
414 | ||
415 | void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io) | |
416 | { | |
417 | struct snd_pcm_substream *substream = io->substream; | |
418 | ||
419 | /* | |
420 | * this function should be called... | |
421 | * | |
422 | * - if rsnd_dai_pointer_update() returns true | |
423 | * - without spin lock | |
424 | */ | |
425 | ||
426 | snd_pcm_period_elapsed(substream); | |
1536a968 KM |
427 | } |
428 | ||
5626ad08 | 429 | static void rsnd_dai_stream_init(struct rsnd_dai_stream *io, |
1536a968 KM |
430 | struct snd_pcm_substream *substream) |
431 | { | |
432 | struct snd_pcm_runtime *runtime = substream->runtime; | |
433 | ||
1536a968 KM |
434 | io->substream = substream; |
435 | io->byte_pos = 0; | |
436 | io->period_pos = 0; | |
437 | io->byte_per_period = runtime->period_size * | |
438 | runtime->channels * | |
439 | samples_to_bytes(runtime, 1); | |
440 | io->next_period_byte = io->byte_per_period; | |
5626ad08 | 441 | } |
1536a968 | 442 | |
5626ad08 KM |
443 | static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io) |
444 | { | |
445 | io->substream = NULL; | |
1536a968 KM |
446 | } |
447 | ||
448 | static | |
449 | struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream) | |
450 | { | |
451 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
452 | ||
453 | return rtd->cpu_dai; | |
454 | } | |
455 | ||
456 | static | |
457 | struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai, | |
458 | struct snd_pcm_substream *substream) | |
459 | { | |
460 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
461 | return &rdai->playback; | |
462 | else | |
463 | return &rdai->capture; | |
464 | } | |
465 | ||
466 | static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd, | |
467 | struct snd_soc_dai *dai) | |
468 | { | |
eb2535f5 | 469 | struct rsnd_priv *priv = rsnd_dai_to_priv(dai); |
1536a968 KM |
470 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); |
471 | struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); | |
29e69fd2 | 472 | int ssi_id = rsnd_mod_id(rsnd_io_to_mod_ssi(io)); |
1536a968 KM |
473 | int ret; |
474 | unsigned long flags; | |
475 | ||
02299d98 | 476 | spin_lock_irqsave(&priv->lock, flags); |
1536a968 KM |
477 | |
478 | switch (cmd) { | |
479 | case SNDRV_PCM_TRIGGER_START: | |
5626ad08 | 480 | rsnd_dai_stream_init(io, substream); |
1536a968 KM |
481 | |
482 | ret = rsnd_platform_call(priv, dai, start, ssi_id); | |
483 | if (ret < 0) | |
484 | goto dai_trigger_end; | |
485 | ||
690602fc | 486 | ret = rsnd_dai_call(init, io, priv); |
cdaa3cdf KM |
487 | if (ret < 0) |
488 | goto dai_trigger_end; | |
489 | ||
690602fc | 490 | ret = rsnd_dai_call(start, io, priv); |
cdaa3cdf KM |
491 | if (ret < 0) |
492 | goto dai_trigger_end; | |
1536a968 KM |
493 | break; |
494 | case SNDRV_PCM_TRIGGER_STOP: | |
690602fc | 495 | ret = rsnd_dai_call(stop, io, priv); |
cdaa3cdf | 496 | |
89e3e2c3 | 497 | ret |= rsnd_dai_call(quit, io, priv); |
cdaa3cdf | 498 | |
89e3e2c3 | 499 | ret |= rsnd_platform_call(priv, dai, stop, ssi_id); |
5626ad08 KM |
500 | |
501 | rsnd_dai_stream_quit(io); | |
1536a968 KM |
502 | break; |
503 | default: | |
504 | ret = -EINVAL; | |
505 | } | |
506 | ||
507 | dai_trigger_end: | |
02299d98 | 508 | spin_unlock_irqrestore(&priv->lock, flags); |
1536a968 KM |
509 | |
510 | return ret; | |
511 | } | |
512 | ||
513 | static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) | |
514 | { | |
515 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); | |
516 | ||
517 | /* set master/slave audio interface */ | |
518 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { | |
519 | case SND_SOC_DAIFMT_CBM_CFM: | |
e1508289 | 520 | rdai->clk_master = 0; |
1536a968 KM |
521 | break; |
522 | case SND_SOC_DAIFMT_CBS_CFS: | |
e1508289 | 523 | rdai->clk_master = 1; /* codec is slave, cpu is master */ |
1536a968 KM |
524 | break; |
525 | default: | |
526 | return -EINVAL; | |
527 | } | |
528 | ||
1536a968 KM |
529 | /* set format */ |
530 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { | |
531 | case SND_SOC_DAIFMT_I2S: | |
532 | rdai->sys_delay = 0; | |
533 | rdai->data_alignment = 0; | |
1a7889ca | 534 | rdai->frm_clk_inv = 0; |
1536a968 KM |
535 | break; |
536 | case SND_SOC_DAIFMT_LEFT_J: | |
537 | rdai->sys_delay = 1; | |
538 | rdai->data_alignment = 0; | |
1a7889ca | 539 | rdai->frm_clk_inv = 1; |
1536a968 KM |
540 | break; |
541 | case SND_SOC_DAIFMT_RIGHT_J: | |
542 | rdai->sys_delay = 1; | |
543 | rdai->data_alignment = 1; | |
1a7889ca KM |
544 | rdai->frm_clk_inv = 1; |
545 | break; | |
546 | } | |
547 | ||
548 | /* set clock inversion */ | |
549 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { | |
550 | case SND_SOC_DAIFMT_NB_IF: | |
551 | rdai->bit_clk_inv = rdai->bit_clk_inv; | |
552 | rdai->frm_clk_inv = !rdai->frm_clk_inv; | |
553 | break; | |
554 | case SND_SOC_DAIFMT_IB_NF: | |
555 | rdai->bit_clk_inv = !rdai->bit_clk_inv; | |
556 | rdai->frm_clk_inv = rdai->frm_clk_inv; | |
557 | break; | |
558 | case SND_SOC_DAIFMT_IB_IF: | |
559 | rdai->bit_clk_inv = !rdai->bit_clk_inv; | |
560 | rdai->frm_clk_inv = !rdai->frm_clk_inv; | |
561 | break; | |
562 | case SND_SOC_DAIFMT_NB_NF: | |
563 | default: | |
1536a968 KM |
564 | break; |
565 | } | |
566 | ||
567 | return 0; | |
568 | } | |
569 | ||
570 | static const struct snd_soc_dai_ops rsnd_soc_dai_ops = { | |
571 | .trigger = rsnd_soc_dai_trigger, | |
572 | .set_fmt = rsnd_soc_dai_set_fmt, | |
573 | }; | |
574 | ||
c8cf15f6 | 575 | #define rsnd_path_add(priv, io, type) \ |
739f9502 KM |
576 | ({ \ |
577 | struct rsnd_mod *mod; \ | |
578 | int ret = 0; \ | |
579 | int id = -1; \ | |
580 | \ | |
581 | if (rsnd_is_enable_path(io, type)) { \ | |
582 | id = rsnd_info_id(priv, io, type); \ | |
583 | if (id >= 0) { \ | |
584 | mod = rsnd_##type##_mod_get(priv, id); \ | |
585 | ret = rsnd_dai_connect(mod, io); \ | |
586 | } \ | |
587 | } \ | |
588 | ret; \ | |
589 | }) | |
590 | ||
c8cf15f6 | 591 | #define rsnd_path_remove(priv, io, type) \ |
d3a76823 KM |
592 | { \ |
593 | struct rsnd_mod *mod; \ | |
594 | int id = -1; \ | |
595 | \ | |
596 | if (rsnd_is_enable_path(io, type)) { \ | |
597 | id = rsnd_info_id(priv, io, type); \ | |
598 | if (id >= 0) { \ | |
599 | mod = rsnd_##type##_mod_get(priv, id); \ | |
600 | rsnd_dai_disconnect(mod, io); \ | |
601 | } \ | |
602 | } \ | |
603 | } | |
604 | ||
e2c08416 KM |
605 | void rsnd_path_parse(struct rsnd_priv *priv, |
606 | struct rsnd_dai_stream *io) | |
607 | { | |
e2c08416 | 608 | struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io); |
70fb1052 KM |
609 | struct rsnd_mod *mix = rsnd_io_to_mod_mix(io); |
610 | struct rsnd_mod *src = rsnd_io_to_mod_src(io); | |
611 | struct rsnd_mod *cmd; | |
612 | struct device *dev = rsnd_priv_to_dev(priv); | |
613 | u32 data; | |
e2c08416 KM |
614 | |
615 | /* Gen1 is not supported */ | |
616 | if (rsnd_is_gen1(priv)) | |
617 | return; | |
618 | ||
70fb1052 KM |
619 | if (!mix && !dvc) |
620 | return; | |
621 | ||
622 | if (mix) { | |
623 | struct rsnd_dai *rdai; | |
624 | int i; | |
625 | u32 path[] = { | |
626 | [0] = 0, | |
627 | [1] = 1 << 0, | |
628 | [2] = 0, | |
629 | [3] = 0, | |
630 | [4] = 0, | |
631 | [5] = 1 << 8 | |
632 | }; | |
633 | ||
634 | /* | |
635 | * it is assuming that integrater is well understanding about | |
636 | * data path. Here doesn't check impossible connection, | |
637 | * like src2 + src5 | |
638 | */ | |
639 | data = 0; | |
640 | for_each_rsnd_dai(rdai, priv, i) { | |
641 | io = &rdai->playback; | |
642 | if (mix == rsnd_io_to_mod_mix(io)) | |
643 | data |= path[rsnd_mod_id(src)]; | |
644 | ||
645 | io = &rdai->capture; | |
646 | if (mix == rsnd_io_to_mod_mix(io)) | |
647 | data |= path[rsnd_mod_id(src)]; | |
648 | } | |
649 | ||
650 | /* | |
651 | * We can't use ctu = rsnd_io_ctu() here. | |
652 | * Since, ID of dvc/mix are 0 or 1 (= same as CMD number) | |
653 | * but ctu IDs are 0 - 7 (= CTU00 - CTU13) | |
654 | */ | |
655 | cmd = mix; | |
656 | } else { | |
657 | u32 path[] = { | |
658 | [0] = 0x30000, | |
659 | [1] = 0x30001, | |
660 | [2] = 0x40000, | |
661 | [3] = 0x10000, | |
662 | [4] = 0x20000, | |
663 | [5] = 0x40100 | |
664 | }; | |
665 | ||
666 | data = path[rsnd_mod_id(src)]; | |
667 | ||
668 | cmd = dvc; | |
669 | } | |
670 | ||
671 | dev_dbg(dev, "ctu/mix path = 0x%08x", data); | |
672 | ||
673 | rsnd_mod_write(cmd, CMD_ROUTE_SLCT, data); | |
674 | ||
675 | rsnd_mod_write(cmd, CMD_CTRL, 0x10); | |
e2c08416 KM |
676 | } |
677 | ||
9bfed6cf KM |
678 | static int rsnd_path_init(struct rsnd_priv *priv, |
679 | struct rsnd_dai *rdai, | |
680 | struct rsnd_dai_stream *io) | |
681 | { | |
9bfed6cf | 682 | int ret; |
9bfed6cf KM |
683 | |
684 | /* | |
685 | * Gen1 is created by SRU/SSI, and this SRU is base module of | |
686 | * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU) | |
687 | * | |
688 | * Easy image is.. | |
689 | * Gen1 SRU = Gen2 SCU + SSIU + etc | |
690 | * | |
691 | * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is | |
692 | * using fixed path. | |
9bfed6cf | 693 | */ |
9bfed6cf | 694 | |
78edead4 KM |
695 | /* SSI */ |
696 | ret = rsnd_path_add(priv, io, ssi); | |
739f9502 KM |
697 | if (ret < 0) |
698 | return ret; | |
9bfed6cf | 699 | |
78edead4 KM |
700 | /* SRC */ |
701 | ret = rsnd_path_add(priv, io, src); | |
739f9502 KM |
702 | if (ret < 0) |
703 | return ret; | |
9bfed6cf | 704 | |
9269e3c3 KM |
705 | /* CTU */ |
706 | ret = rsnd_path_add(priv, io, ctu); | |
707 | if (ret < 0) | |
708 | return ret; | |
709 | ||
70fb1052 KM |
710 | /* MIX */ |
711 | ret = rsnd_path_add(priv, io, mix); | |
712 | if (ret < 0) | |
713 | return ret; | |
714 | ||
bff58ea4 | 715 | /* DVC */ |
c8cf15f6 | 716 | ret = rsnd_path_add(priv, io, dvc); |
bff58ea4 KM |
717 | if (ret < 0) |
718 | return ret; | |
9bfed6cf KM |
719 | |
720 | return ret; | |
721 | } | |
722 | ||
90e8e50f KM |
723 | static void rsnd_of_parse_dai(struct platform_device *pdev, |
724 | const struct rsnd_of_data *of_data, | |
725 | struct rsnd_priv *priv) | |
726 | { | |
727 | struct device_node *dai_node, *dai_np; | |
728 | struct device_node *ssi_node, *ssi_np; | |
729 | struct device_node *src_node, *src_np; | |
9269e3c3 | 730 | struct device_node *ctu_node, *ctu_np; |
70fb1052 | 731 | struct device_node *mix_node, *mix_np; |
34cb6123 | 732 | struct device_node *dvc_node, *dvc_np; |
90e8e50f KM |
733 | struct device_node *playback, *capture; |
734 | struct rsnd_dai_platform_info *dai_info; | |
735 | struct rcar_snd_info *info = rsnd_priv_to_info(priv); | |
736 | struct device *dev = &pdev->dev; | |
737 | int nr, i; | |
70fb1052 | 738 | int dai_i, ssi_i, src_i, ctu_i, mix_i, dvc_i; |
90e8e50f KM |
739 | |
740 | if (!of_data) | |
741 | return; | |
742 | ||
743 | dai_node = of_get_child_by_name(dev->of_node, "rcar_sound,dai"); | |
744 | if (!dai_node) | |
745 | return; | |
746 | ||
747 | nr = of_get_child_count(dai_node); | |
748 | if (!nr) | |
749 | return; | |
750 | ||
751 | dai_info = devm_kzalloc(dev, | |
752 | sizeof(struct rsnd_dai_platform_info) * nr, | |
753 | GFP_KERNEL); | |
754 | if (!dai_info) { | |
755 | dev_err(dev, "dai info allocation error\n"); | |
756 | return; | |
757 | } | |
758 | ||
759 | info->dai_info_nr = nr; | |
760 | info->dai_info = dai_info; | |
761 | ||
762 | ssi_node = of_get_child_by_name(dev->of_node, "rcar_sound,ssi"); | |
763 | src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src"); | |
9269e3c3 | 764 | ctu_node = of_get_child_by_name(dev->of_node, "rcar_sound,ctu"); |
70fb1052 | 765 | mix_node = of_get_child_by_name(dev->of_node, "rcar_sound,mix"); |
34cb6123 | 766 | dvc_node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc"); |
90e8e50f KM |
767 | |
768 | #define mod_parse(name) \ | |
769 | if (name##_node) { \ | |
770 | struct rsnd_##name##_platform_info *name##_info; \ | |
771 | \ | |
772 | name##_i = 0; \ | |
773 | for_each_child_of_node(name##_node, name##_np) { \ | |
774 | name##_info = info->name##_info + name##_i; \ | |
775 | \ | |
776 | if (name##_np == playback) \ | |
777 | dai_info->playback.name = name##_info; \ | |
778 | if (name##_np == capture) \ | |
779 | dai_info->capture.name = name##_info; \ | |
780 | \ | |
781 | name##_i++; \ | |
782 | } \ | |
783 | } | |
784 | ||
785 | /* | |
786 | * parse all dai | |
787 | */ | |
788 | dai_i = 0; | |
789 | for_each_child_of_node(dai_node, dai_np) { | |
790 | dai_info = info->dai_info + dai_i; | |
791 | ||
792 | for (i = 0;; i++) { | |
793 | ||
794 | playback = of_parse_phandle(dai_np, "playback", i); | |
795 | capture = of_parse_phandle(dai_np, "capture", i); | |
796 | ||
797 | if (!playback && !capture) | |
798 | break; | |
799 | ||
800 | mod_parse(ssi); | |
801 | mod_parse(src); | |
9269e3c3 | 802 | mod_parse(ctu); |
70fb1052 | 803 | mod_parse(mix); |
34cb6123 | 804 | mod_parse(dvc); |
90e8e50f | 805 | |
a493b6a6 JL |
806 | of_node_put(playback); |
807 | of_node_put(capture); | |
90e8e50f KM |
808 | } |
809 | ||
810 | dai_i++; | |
811 | } | |
812 | } | |
813 | ||
1536a968 | 814 | static int rsnd_dai_probe(struct platform_device *pdev, |
90e8e50f | 815 | const struct rsnd_of_data *of_data, |
1536a968 KM |
816 | struct rsnd_priv *priv) |
817 | { | |
818 | struct snd_soc_dai_driver *drv; | |
78f13d0c | 819 | struct rcar_snd_info *info = rsnd_priv_to_info(priv); |
1536a968 | 820 | struct rsnd_dai *rdai; |
29e69fd2 | 821 | struct rsnd_ssi_platform_info *pmod, *cmod; |
1536a968 | 822 | struct device *dev = rsnd_priv_to_dev(priv); |
90e8e50f | 823 | int dai_nr; |
4b4dab82 KM |
824 | int i; |
825 | ||
90e8e50f KM |
826 | rsnd_of_parse_dai(pdev, of_data, priv); |
827 | ||
90e8e50f | 828 | dai_nr = info->dai_info_nr; |
4b4dab82 KM |
829 | if (!dai_nr) { |
830 | dev_err(dev, "no dai\n"); | |
831 | return -EIO; | |
832 | } | |
1536a968 KM |
833 | |
834 | drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL); | |
835 | rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL); | |
836 | if (!drv || !rdai) { | |
837 | dev_err(dev, "dai allocate failed\n"); | |
838 | return -ENOMEM; | |
839 | } | |
840 | ||
ecba9e72 | 841 | priv->rdai_nr = dai_nr; |
49848073 KM |
842 | priv->daidrv = drv; |
843 | priv->rdai = rdai; | |
844 | ||
1536a968 | 845 | for (i = 0; i < dai_nr; i++) { |
1536a968 | 846 | |
7c57d76f KM |
847 | pmod = info->dai_info[i].playback.ssi; |
848 | cmod = info->dai_info[i].capture.ssi; | |
1536a968 KM |
849 | |
850 | /* | |
851 | * init rsnd_dai | |
852 | */ | |
1536a968 | 853 | snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i); |
1b13d118 | 854 | rdai[i].priv = priv; |
1536a968 KM |
855 | |
856 | /* | |
857 | * init snd_soc_dai_driver | |
858 | */ | |
859 | drv[i].name = rdai[i].name; | |
860 | drv[i].ops = &rsnd_soc_dai_ops; | |
4b4dab82 | 861 | if (pmod) { |
f8c3c309 KM |
862 | snprintf(rdai[i].playback.name, RSND_DAI_NAME_SIZE, |
863 | "DAI%d Playback", i); | |
864 | ||
1536a968 KM |
865 | drv[i].playback.rates = RSND_RATES; |
866 | drv[i].playback.formats = RSND_FMTS; | |
867 | drv[i].playback.channels_min = 2; | |
868 | drv[i].playback.channels_max = 2; | |
f8c3c309 | 869 | drv[i].playback.stream_name = rdai[i].playback.name; |
389933d9 | 870 | |
29e69fd2 | 871 | rdai[i].playback.info = &info->dai_info[i].playback; |
54cb5562 | 872 | rdai[i].playback.rdai = rdai + i; |
9bfed6cf | 873 | rsnd_path_init(priv, &rdai[i], &rdai[i].playback); |
1536a968 | 874 | } |
4b4dab82 | 875 | if (cmod) { |
f8c3c309 KM |
876 | snprintf(rdai[i].capture.name, RSND_DAI_NAME_SIZE, |
877 | "DAI%d Capture", i); | |
878 | ||
1536a968 KM |
879 | drv[i].capture.rates = RSND_RATES; |
880 | drv[i].capture.formats = RSND_FMTS; | |
881 | drv[i].capture.channels_min = 2; | |
882 | drv[i].capture.channels_max = 2; | |
f8c3c309 | 883 | drv[i].capture.stream_name = rdai[i].capture.name; |
389933d9 | 884 | |
29e69fd2 | 885 | rdai[i].capture.info = &info->dai_info[i].capture; |
54cb5562 | 886 | rdai[i].capture.rdai = rdai + i; |
9bfed6cf | 887 | rsnd_path_init(priv, &rdai[i], &rdai[i].capture); |
1536a968 KM |
888 | } |
889 | ||
4b4dab82 KM |
890 | dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name, |
891 | pmod ? "play" : " -- ", | |
892 | cmod ? "capture" : " -- "); | |
1536a968 KM |
893 | } |
894 | ||
1536a968 KM |
895 | return 0; |
896 | } | |
897 | ||
1536a968 KM |
898 | /* |
899 | * pcm ops | |
900 | */ | |
901 | static struct snd_pcm_hardware rsnd_pcm_hardware = { | |
902 | .info = SNDRV_PCM_INFO_INTERLEAVED | | |
903 | SNDRV_PCM_INFO_MMAP | | |
706c6621 | 904 | SNDRV_PCM_INFO_MMAP_VALID, |
1536a968 KM |
905 | .buffer_bytes_max = 64 * 1024, |
906 | .period_bytes_min = 32, | |
907 | .period_bytes_max = 8192, | |
908 | .periods_min = 1, | |
909 | .periods_max = 32, | |
910 | .fifo_size = 256, | |
911 | }; | |
912 | ||
913 | static int rsnd_pcm_open(struct snd_pcm_substream *substream) | |
914 | { | |
915 | struct snd_pcm_runtime *runtime = substream->runtime; | |
916 | int ret = 0; | |
917 | ||
918 | snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware); | |
919 | ||
920 | ret = snd_pcm_hw_constraint_integer(runtime, | |
921 | SNDRV_PCM_HW_PARAM_PERIODS); | |
922 | ||
923 | return ret; | |
924 | } | |
925 | ||
926 | static int rsnd_hw_params(struct snd_pcm_substream *substream, | |
927 | struct snd_pcm_hw_params *hw_params) | |
928 | { | |
3b7843ff KM |
929 | struct snd_soc_dai *dai = rsnd_substream_to_dai(substream); |
930 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); | |
931 | struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); | |
932 | int ret; | |
933 | ||
934 | ret = rsnd_dai_call(hw_params, io, substream, hw_params); | |
935 | if (ret) | |
936 | return ret; | |
937 | ||
1536a968 KM |
938 | return snd_pcm_lib_malloc_pages(substream, |
939 | params_buffer_bytes(hw_params)); | |
940 | } | |
941 | ||
942 | static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream) | |
943 | { | |
944 | struct snd_pcm_runtime *runtime = substream->runtime; | |
945 | struct snd_soc_dai *dai = rsnd_substream_to_dai(substream); | |
946 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); | |
947 | struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); | |
948 | ||
949 | return bytes_to_frames(runtime, io->byte_pos); | |
950 | } | |
951 | ||
952 | static struct snd_pcm_ops rsnd_pcm_ops = { | |
953 | .open = rsnd_pcm_open, | |
954 | .ioctl = snd_pcm_lib_ioctl, | |
955 | .hw_params = rsnd_hw_params, | |
956 | .hw_free = snd_pcm_lib_free_pages, | |
957 | .pointer = rsnd_pointer, | |
958 | }; | |
959 | ||
170a2497 KM |
960 | /* |
961 | * snd_kcontrol | |
962 | */ | |
963 | #define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value) | |
964 | static int rsnd_kctrl_info(struct snd_kcontrol *kctrl, | |
965 | struct snd_ctl_elem_info *uinfo) | |
966 | { | |
967 | struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl); | |
968 | ||
969 | if (cfg->texts) { | |
970 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | |
971 | uinfo->count = cfg->size; | |
972 | uinfo->value.enumerated.items = cfg->max; | |
973 | if (uinfo->value.enumerated.item >= cfg->max) | |
974 | uinfo->value.enumerated.item = cfg->max - 1; | |
975 | strlcpy(uinfo->value.enumerated.name, | |
976 | cfg->texts[uinfo->value.enumerated.item], | |
977 | sizeof(uinfo->value.enumerated.name)); | |
978 | } else { | |
979 | uinfo->count = cfg->size; | |
980 | uinfo->value.integer.min = 0; | |
981 | uinfo->value.integer.max = cfg->max; | |
982 | uinfo->type = (cfg->max == 1) ? | |
983 | SNDRV_CTL_ELEM_TYPE_BOOLEAN : | |
984 | SNDRV_CTL_ELEM_TYPE_INTEGER; | |
985 | } | |
986 | ||
987 | return 0; | |
988 | } | |
989 | ||
990 | static int rsnd_kctrl_get(struct snd_kcontrol *kctrl, | |
991 | struct snd_ctl_elem_value *uc) | |
992 | { | |
993 | struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl); | |
994 | int i; | |
995 | ||
996 | for (i = 0; i < cfg->size; i++) | |
997 | if (cfg->texts) | |
998 | uc->value.enumerated.item[i] = cfg->val[i]; | |
999 | else | |
1000 | uc->value.integer.value[i] = cfg->val[i]; | |
1001 | ||
1002 | return 0; | |
1003 | } | |
1004 | ||
1005 | static int rsnd_kctrl_put(struct snd_kcontrol *kctrl, | |
1006 | struct snd_ctl_elem_value *uc) | |
1007 | { | |
1008 | struct rsnd_mod *mod = snd_kcontrol_chip(kctrl); | |
1009 | struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl); | |
1010 | int i, change = 0; | |
1011 | ||
1012 | for (i = 0; i < cfg->size; i++) { | |
1013 | if (cfg->texts) { | |
1014 | change |= (uc->value.enumerated.item[i] != cfg->val[i]); | |
1015 | cfg->val[i] = uc->value.enumerated.item[i]; | |
1016 | } else { | |
1017 | change |= (uc->value.integer.value[i] != cfg->val[i]); | |
1018 | cfg->val[i] = uc->value.integer.value[i]; | |
1019 | } | |
1020 | } | |
1021 | ||
1022 | if (change) | |
b65a7ccc | 1023 | cfg->update(cfg->io, mod); |
170a2497 KM |
1024 | |
1025 | return change; | |
1026 | } | |
1027 | ||
1028 | static int __rsnd_kctrl_new(struct rsnd_mod *mod, | |
b65a7ccc | 1029 | struct rsnd_dai_stream *io, |
170a2497 KM |
1030 | struct snd_soc_pcm_runtime *rtd, |
1031 | const unsigned char *name, | |
1032 | struct rsnd_kctrl_cfg *cfg, | |
b65a7ccc KM |
1033 | void (*update)(struct rsnd_dai_stream *io, |
1034 | struct rsnd_mod *mod)) | |
170a2497 | 1035 | { |
da620d72 | 1036 | struct snd_soc_card *soc_card = rtd->card; |
170a2497 KM |
1037 | struct snd_card *card = rtd->card->snd_card; |
1038 | struct snd_kcontrol *kctrl; | |
1039 | struct snd_kcontrol_new knew = { | |
1040 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1041 | .name = name, | |
1042 | .info = rsnd_kctrl_info, | |
da620d72 | 1043 | .index = rtd - soc_card->rtd, |
170a2497 KM |
1044 | .get = rsnd_kctrl_get, |
1045 | .put = rsnd_kctrl_put, | |
1046 | .private_value = (unsigned long)cfg, | |
1047 | }; | |
1048 | int ret; | |
1049 | ||
1050 | kctrl = snd_ctl_new1(&knew, mod); | |
1051 | if (!kctrl) | |
1052 | return -ENOMEM; | |
1053 | ||
1054 | ret = snd_ctl_add(card, kctrl); | |
d1f83d6e KM |
1055 | if (ret < 0) { |
1056 | snd_ctl_free_one(kctrl); | |
170a2497 | 1057 | return ret; |
d1f83d6e | 1058 | } |
170a2497 KM |
1059 | |
1060 | cfg->update = update; | |
d1f83d6e KM |
1061 | cfg->card = card; |
1062 | cfg->kctrl = kctrl; | |
b65a7ccc | 1063 | cfg->io = io; |
170a2497 KM |
1064 | |
1065 | return 0; | |
1066 | } | |
1067 | ||
d1f83d6e KM |
1068 | void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg *cfg) |
1069 | { | |
1070 | snd_ctl_remove(cfg->card, cfg->kctrl); | |
1071 | } | |
1072 | ||
170a2497 | 1073 | int rsnd_kctrl_new_m(struct rsnd_mod *mod, |
b65a7ccc | 1074 | struct rsnd_dai_stream *io, |
170a2497 KM |
1075 | struct snd_soc_pcm_runtime *rtd, |
1076 | const unsigned char *name, | |
b65a7ccc KM |
1077 | void (*update)(struct rsnd_dai_stream *io, |
1078 | struct rsnd_mod *mod), | |
170a2497 KM |
1079 | struct rsnd_kctrl_cfg_m *_cfg, |
1080 | u32 max) | |
1081 | { | |
1082 | _cfg->cfg.max = max; | |
1083 | _cfg->cfg.size = RSND_DVC_CHANNELS; | |
1084 | _cfg->cfg.val = _cfg->val; | |
b65a7ccc | 1085 | return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update); |
170a2497 KM |
1086 | } |
1087 | ||
1088 | int rsnd_kctrl_new_s(struct rsnd_mod *mod, | |
b65a7ccc | 1089 | struct rsnd_dai_stream *io, |
170a2497 KM |
1090 | struct snd_soc_pcm_runtime *rtd, |
1091 | const unsigned char *name, | |
b65a7ccc KM |
1092 | void (*update)(struct rsnd_dai_stream *io, |
1093 | struct rsnd_mod *mod), | |
170a2497 KM |
1094 | struct rsnd_kctrl_cfg_s *_cfg, |
1095 | u32 max) | |
1096 | { | |
1097 | _cfg->cfg.max = max; | |
1098 | _cfg->cfg.size = 1; | |
1099 | _cfg->cfg.val = &_cfg->val; | |
b65a7ccc | 1100 | return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update); |
170a2497 KM |
1101 | } |
1102 | ||
1103 | int rsnd_kctrl_new_e(struct rsnd_mod *mod, | |
b65a7ccc | 1104 | struct rsnd_dai_stream *io, |
170a2497 KM |
1105 | struct snd_soc_pcm_runtime *rtd, |
1106 | const unsigned char *name, | |
1107 | struct rsnd_kctrl_cfg_s *_cfg, | |
b65a7ccc KM |
1108 | void (*update)(struct rsnd_dai_stream *io, |
1109 | struct rsnd_mod *mod), | |
170a2497 KM |
1110 | const char * const *texts, |
1111 | u32 max) | |
1112 | { | |
1113 | _cfg->cfg.max = max; | |
1114 | _cfg->cfg.size = 1; | |
1115 | _cfg->cfg.val = &_cfg->val; | |
1116 | _cfg->cfg.texts = texts; | |
b65a7ccc | 1117 | return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update); |
170a2497 KM |
1118 | } |
1119 | ||
1536a968 KM |
1120 | /* |
1121 | * snd_soc_platform | |
1122 | */ | |
1123 | ||
1124 | #define PREALLOC_BUFFER (32 * 1024) | |
1125 | #define PREALLOC_BUFFER_MAX (32 * 1024) | |
1126 | ||
1127 | static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd) | |
1128 | { | |
7c63f3c0 KM |
1129 | struct snd_soc_dai *dai = rtd->cpu_dai; |
1130 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); | |
ae11a9be | 1131 | int ret; |
bff58ea4 | 1132 | |
ae11a9be KM |
1133 | ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd); |
1134 | if (ret) | |
1135 | return ret; | |
bff58ea4 | 1136 | |
ae11a9be | 1137 | ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd); |
7c63f3c0 KM |
1138 | if (ret) |
1139 | return ret; | |
bff58ea4 | 1140 | |
1536a968 KM |
1141 | return snd_pcm_lib_preallocate_pages_for_all( |
1142 | rtd->pcm, | |
1143 | SNDRV_DMA_TYPE_DEV, | |
1144 | rtd->card->snd_card->dev, | |
1145 | PREALLOC_BUFFER, PREALLOC_BUFFER_MAX); | |
1146 | } | |
1147 | ||
1536a968 KM |
1148 | static struct snd_soc_platform_driver rsnd_soc_platform = { |
1149 | .ops = &rsnd_pcm_ops, | |
1150 | .pcm_new = rsnd_pcm_new, | |
1536a968 KM |
1151 | }; |
1152 | ||
1153 | static const struct snd_soc_component_driver rsnd_soc_component = { | |
1154 | .name = "rsnd", | |
1155 | }; | |
1156 | ||
d3a76823 | 1157 | static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv, |
f708d944 | 1158 | struct rsnd_dai_stream *io) |
d3a76823 | 1159 | { |
d3a76823 KM |
1160 | int ret; |
1161 | ||
690602fc | 1162 | ret = rsnd_dai_call(probe, io, priv); |
d3a76823 KM |
1163 | if (ret == -EAGAIN) { |
1164 | /* | |
1165 | * Fallback to PIO mode | |
1166 | */ | |
1167 | ||
1168 | /* | |
1169 | * call "remove" for SSI/SRC/DVC | |
1170 | * SSI will be switch to PIO mode if it was DMA mode | |
1171 | * see | |
1172 | * rsnd_dma_init() | |
97463e19 | 1173 | * rsnd_ssi_fallback() |
d3a76823 | 1174 | */ |
690602fc | 1175 | rsnd_dai_call(remove, io, priv); |
d3a76823 KM |
1176 | |
1177 | /* | |
1178 | * remove SRC/DVC from DAI, | |
1179 | */ | |
c8cf15f6 KM |
1180 | rsnd_path_remove(priv, io, src); |
1181 | rsnd_path_remove(priv, io, dvc); | |
d3a76823 | 1182 | |
97463e19 KM |
1183 | /* |
1184 | * fallback | |
1185 | */ | |
690602fc | 1186 | rsnd_dai_call(fallback, io, priv); |
97463e19 | 1187 | |
d3a76823 KM |
1188 | /* |
1189 | * retry to "probe". | |
1190 | * DAI has SSI which is PIO mode only now. | |
1191 | */ | |
690602fc | 1192 | ret = rsnd_dai_call(probe, io, priv); |
d3a76823 KM |
1193 | } |
1194 | ||
1195 | return ret; | |
1196 | } | |
1197 | ||
1536a968 KM |
1198 | /* |
1199 | * rsnd probe | |
1200 | */ | |
1201 | static int rsnd_probe(struct platform_device *pdev) | |
1202 | { | |
1203 | struct rcar_snd_info *info; | |
1204 | struct rsnd_priv *priv; | |
1205 | struct device *dev = &pdev->dev; | |
7681f6ac | 1206 | struct rsnd_dai *rdai; |
90e8e50f KM |
1207 | const struct of_device_id *of_id = of_match_device(rsnd_of_match, dev); |
1208 | const struct rsnd_of_data *of_data; | |
d1ac970f | 1209 | int (*probe_func[])(struct platform_device *pdev, |
90e8e50f | 1210 | const struct rsnd_of_data *of_data, |
d1ac970f KM |
1211 | struct rsnd_priv *priv) = { |
1212 | rsnd_gen_probe, | |
288f392e | 1213 | rsnd_dma_probe, |
d1ac970f | 1214 | rsnd_ssi_probe, |
ba9c949f | 1215 | rsnd_src_probe, |
9269e3c3 | 1216 | rsnd_ctu_probe, |
70fb1052 | 1217 | rsnd_mix_probe, |
bff58ea4 | 1218 | rsnd_dvc_probe, |
d1ac970f KM |
1219 | rsnd_adg_probe, |
1220 | rsnd_dai_probe, | |
1221 | }; | |
1222 | int ret, i; | |
1536a968 | 1223 | |
dcc448e6 GU |
1224 | info = devm_kzalloc(&pdev->dev, sizeof(struct rcar_snd_info), |
1225 | GFP_KERNEL); | |
1226 | if (!info) | |
1227 | return -ENOMEM; | |
1228 | of_data = of_id->data; | |
1536a968 KM |
1229 | |
1230 | /* | |
1231 | * init priv data | |
1232 | */ | |
1233 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); | |
1234 | if (!priv) { | |
1235 | dev_err(dev, "priv allocate failed\n"); | |
1236 | return -ENODEV; | |
1237 | } | |
1238 | ||
9f464f8e | 1239 | priv->pdev = pdev; |
1536a968 KM |
1240 | priv->info = info; |
1241 | spin_lock_init(&priv->lock); | |
1242 | ||
1243 | /* | |
1244 | * init each module | |
1245 | */ | |
d1ac970f | 1246 | for (i = 0; i < ARRAY_SIZE(probe_func); i++) { |
90e8e50f | 1247 | ret = probe_func[i](pdev, of_data, priv); |
d1ac970f KM |
1248 | if (ret) |
1249 | return ret; | |
1250 | } | |
07539c1d | 1251 | |
7681f6ac | 1252 | for_each_rsnd_dai(rdai, priv, i) { |
f708d944 | 1253 | ret = rsnd_rdai_continuance_probe(priv, &rdai->playback); |
7681f6ac | 1254 | if (ret) |
d62a3dcd | 1255 | goto exit_snd_probe; |
dfc9403b | 1256 | |
f708d944 | 1257 | ret = rsnd_rdai_continuance_probe(priv, &rdai->capture); |
7681f6ac | 1258 | if (ret) |
d62a3dcd | 1259 | goto exit_snd_probe; |
7681f6ac | 1260 | } |
4b4dab82 | 1261 | |
0b1f6ec7 KM |
1262 | dev_set_drvdata(dev, priv); |
1263 | ||
1536a968 KM |
1264 | /* |
1265 | * asoc register | |
1266 | */ | |
1267 | ret = snd_soc_register_platform(dev, &rsnd_soc_platform); | |
1268 | if (ret < 0) { | |
1269 | dev_err(dev, "cannot snd soc register\n"); | |
1270 | return ret; | |
1271 | } | |
1272 | ||
1273 | ret = snd_soc_register_component(dev, &rsnd_soc_component, | |
ecba9e72 | 1274 | priv->daidrv, rsnd_rdai_nr(priv)); |
1536a968 KM |
1275 | if (ret < 0) { |
1276 | dev_err(dev, "cannot snd dai register\n"); | |
1277 | goto exit_snd_soc; | |
1278 | } | |
1279 | ||
1536a968 KM |
1280 | pm_runtime_enable(dev); |
1281 | ||
1282 | dev_info(dev, "probed\n"); | |
1283 | return ret; | |
1284 | ||
1285 | exit_snd_soc: | |
1286 | snd_soc_unregister_platform(dev); | |
d62a3dcd KM |
1287 | exit_snd_probe: |
1288 | for_each_rsnd_dai(rdai, priv, i) { | |
690602fc KM |
1289 | rsnd_dai_call(remove, &rdai->playback, priv); |
1290 | rsnd_dai_call(remove, &rdai->capture, priv); | |
d62a3dcd | 1291 | } |
1536a968 KM |
1292 | |
1293 | return ret; | |
1294 | } | |
1295 | ||
1296 | static int rsnd_remove(struct platform_device *pdev) | |
1297 | { | |
1298 | struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev); | |
7681f6ac | 1299 | struct rsnd_dai *rdai; |
2f78dd7f KM |
1300 | void (*remove_func[])(struct platform_device *pdev, |
1301 | struct rsnd_priv *priv) = { | |
1302 | rsnd_ssi_remove, | |
1303 | rsnd_src_remove, | |
9269e3c3 | 1304 | rsnd_ctu_remove, |
70fb1052 | 1305 | rsnd_mix_remove, |
2f78dd7f KM |
1306 | rsnd_dvc_remove, |
1307 | }; | |
d62a3dcd | 1308 | int ret = 0, i; |
1536a968 KM |
1309 | |
1310 | pm_runtime_disable(&pdev->dev); | |
1311 | ||
7681f6ac | 1312 | for_each_rsnd_dai(rdai, priv, i) { |
690602fc KM |
1313 | ret |= rsnd_dai_call(remove, &rdai->playback, priv); |
1314 | ret |= rsnd_dai_call(remove, &rdai->capture, priv); | |
7681f6ac | 1315 | } |
1536a968 | 1316 | |
2f78dd7f KM |
1317 | for (i = 0; i < ARRAY_SIZE(remove_func); i++) |
1318 | remove_func[i](pdev, priv); | |
1319 | ||
d7c42ff8 KM |
1320 | snd_soc_unregister_component(&pdev->dev); |
1321 | snd_soc_unregister_platform(&pdev->dev); | |
1322 | ||
d62a3dcd | 1323 | return ret; |
1536a968 KM |
1324 | } |
1325 | ||
1326 | static struct platform_driver rsnd_driver = { | |
1327 | .driver = { | |
1328 | .name = "rcar_sound", | |
90e8e50f | 1329 | .of_match_table = rsnd_of_match, |
1536a968 KM |
1330 | }, |
1331 | .probe = rsnd_probe, | |
1332 | .remove = rsnd_remove, | |
1333 | }; | |
1334 | module_platform_driver(rsnd_driver); | |
1335 | ||
1336 | MODULE_LICENSE("GPL"); | |
1337 | MODULE_DESCRIPTION("Renesas R-Car audio driver"); | |
1338 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); | |
1339 | MODULE_ALIAS("platform:rcar-pcm-audio"); |