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 | ||
33187fb4 | 102 | static const struct of_device_id rsnd_of_match[] = { |
e797f58e KM |
103 | { .compatible = "renesas,rcar_sound-gen1", .data = (void *)RSND_GEN1 }, |
104 | { .compatible = "renesas,rcar_sound-gen2", .data = (void *)RSND_GEN2 }, | |
105 | { .compatible = "renesas,rcar_sound-gen3", .data = (void *)RSND_GEN2 }, /* gen2 compatible */ | |
90e8e50f KM |
106 | {}, |
107 | }; | |
108 | MODULE_DEVICE_TABLE(of, rsnd_of_match); | |
109 | ||
81ad174d KM |
110 | /* |
111 | * rsnd_mod functions | |
112 | */ | |
f1df1229 KM |
113 | void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type) |
114 | { | |
115 | if (mod->type != type) { | |
116 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); | |
117 | struct device *dev = rsnd_priv_to_dev(priv); | |
118 | ||
119 | dev_warn(dev, "%s[%d] is not your expected module\n", | |
120 | rsnd_mod_name(mod), rsnd_mod_id(mod)); | |
121 | } | |
122 | } | |
123 | ||
cdaa3cdf KM |
124 | char *rsnd_mod_name(struct rsnd_mod *mod) |
125 | { | |
126 | if (!mod || !mod->ops) | |
127 | return "unknown"; | |
128 | ||
129 | return mod->ops->name; | |
130 | } | |
131 | ||
9b99e9a7 KM |
132 | struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io, |
133 | struct rsnd_mod *mod) | |
d9288d0b | 134 | { |
72adc61f KM |
135 | if (!mod || !mod->ops || !mod->ops->dma_req) |
136 | return NULL; | |
d9288d0b | 137 | |
9b99e9a7 | 138 | return mod->ops->dma_req(io, mod); |
d9288d0b KM |
139 | } |
140 | ||
5ba17b42 KM |
141 | u32 *rsnd_mod_get_status(struct rsnd_dai_stream *io, |
142 | struct rsnd_mod *mod, | |
143 | enum rsnd_mod_type type) | |
144 | { | |
145 | return &mod->status; | |
146 | } | |
147 | ||
2099bc8e KM |
148 | int rsnd_mod_init(struct rsnd_priv *priv, |
149 | struct rsnd_mod *mod, | |
5ba17b42 KM |
150 | struct rsnd_mod_ops *ops, |
151 | struct clk *clk, | |
152 | u32* (*get_status)(struct rsnd_dai_stream *io, | |
153 | struct rsnd_mod *mod, | |
154 | enum rsnd_mod_type type), | |
155 | enum rsnd_mod_type type, | |
156 | int id) | |
cdaa3cdf | 157 | { |
2f78dd7f KM |
158 | int ret = clk_prepare(clk); |
159 | ||
160 | if (ret) | |
161 | return ret; | |
162 | ||
cdaa3cdf KM |
163 | mod->id = id; |
164 | mod->ops = ops; | |
a126021d | 165 | mod->type = type; |
85642952 | 166 | mod->clk = clk; |
2099bc8e | 167 | mod->priv = priv; |
5ba17b42 | 168 | mod->get_status = get_status; |
2f78dd7f KM |
169 | |
170 | return ret; | |
171 | } | |
172 | ||
173 | void rsnd_mod_quit(struct rsnd_mod *mod) | |
174 | { | |
175 | if (mod->clk) | |
176 | clk_unprepare(mod->clk); | |
ea96380b | 177 | mod->clk = NULL; |
cdaa3cdf KM |
178 | } |
179 | ||
f501b7a4 KM |
180 | void rsnd_mod_interrupt(struct rsnd_mod *mod, |
181 | void (*callback)(struct rsnd_mod *mod, | |
182 | struct rsnd_dai_stream *io)) | |
183 | { | |
184 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); | |
185 | struct rsnd_dai_stream *io; | |
186 | struct rsnd_dai *rdai; | |
2daf71ad | 187 | int i; |
f501b7a4 | 188 | |
2daf71ad KM |
189 | for_each_rsnd_dai(rdai, priv, i) { |
190 | io = &rdai->playback; | |
191 | if (mod == io->mod[mod->type]) | |
192 | callback(mod, io); | |
f501b7a4 | 193 | |
2daf71ad KM |
194 | io = &rdai->capture; |
195 | if (mod == io->mod[mod->type]) | |
196 | callback(mod, io); | |
f501b7a4 KM |
197 | } |
198 | } | |
199 | ||
d5bbe7de | 200 | int rsnd_io_is_working(struct rsnd_dai_stream *io) |
02299d98 | 201 | { |
02299d98 KM |
202 | /* see rsnd_dai_stream_init/quit() */ |
203 | return !!io->substream; | |
204 | } | |
205 | ||
750fd445 KM |
206 | void rsnd_set_slot(struct rsnd_dai *rdai, |
207 | int slots, int num) | |
208 | { | |
209 | rdai->slots = slots; | |
210 | rdai->slots_num = num; | |
211 | } | |
212 | ||
c140284b | 213 | int rsnd_get_slot(struct rsnd_dai_stream *io) |
8ec85e7f | 214 | { |
c140284b KM |
215 | struct rsnd_dai *rdai = rsnd_io_to_rdai(io); |
216 | ||
8ec85e7f KM |
217 | return rdai->slots; |
218 | } | |
219 | ||
750fd445 KM |
220 | int rsnd_get_slot_num(struct rsnd_dai_stream *io) |
221 | { | |
222 | struct rsnd_dai *rdai = rsnd_io_to_rdai(io); | |
223 | ||
224 | return rdai->slots_num; | |
225 | } | |
226 | ||
eed76bb8 | 227 | int rsnd_runtime_channel_original(struct rsnd_dai_stream *io) |
8ec85e7f | 228 | { |
5858a7d1 | 229 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
b4c83b17 | 230 | |
eed76bb8 KM |
231 | return runtime->channels; |
232 | } | |
233 | ||
234 | int rsnd_runtime_channel_after_ctu(struct rsnd_dai_stream *io) | |
235 | { | |
236 | int chan = rsnd_runtime_channel_original(io); | |
237 | struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io); | |
238 | ||
239 | if (ctu_mod) { | |
240 | u32 converted_chan = rsnd_ctu_converted_channel(ctu_mod); | |
241 | ||
242 | if (converted_chan) | |
243 | return converted_chan; | |
244 | } | |
245 | ||
246 | return chan; | |
247 | } | |
248 | ||
249 | int rsnd_runtime_channel_for_ssi(struct rsnd_dai_stream *io) | |
250 | { | |
251 | int chan = rsnd_io_is_play(io) ? | |
252 | rsnd_runtime_channel_after_ctu(io) : | |
253 | rsnd_runtime_channel_original(io); | |
254 | ||
255 | /* Use Multi SSI */ | |
256 | if (rsnd_runtime_is_ssi_multi(io)) | |
b4c83b17 | 257 | chan /= rsnd_get_slot_num(io); |
8ec85e7f KM |
258 | |
259 | /* TDM Extend Mode needs 8ch */ | |
260 | if (chan == 6) | |
261 | chan = 8; | |
262 | ||
263 | return chan; | |
264 | } | |
265 | ||
eed76bb8 KM |
266 | int rsnd_runtime_is_ssi_multi(struct rsnd_dai_stream *io) |
267 | { | |
268 | int slots = rsnd_get_slot_num(io); | |
269 | int chan = rsnd_io_is_play(io) ? | |
270 | rsnd_runtime_channel_after_ctu(io) : | |
271 | rsnd_runtime_channel_original(io); | |
272 | ||
273 | return (chan >= 6) && (slots > 1); | |
274 | } | |
275 | ||
276 | int rsnd_runtime_is_ssi_tdm(struct rsnd_dai_stream *io) | |
277 | { | |
278 | return rsnd_runtime_channel_for_ssi(io) >= 6; | |
279 | } | |
280 | ||
d7bdbc5d | 281 | /* |
3023b384 | 282 | * ADINR function |
d7bdbc5d | 283 | */ |
3023b384 | 284 | u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io) |
d7bdbc5d KM |
285 | { |
286 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); | |
d7bdbc5d KM |
287 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
288 | struct device *dev = rsnd_priv_to_dev(priv); | |
d7bdbc5d KM |
289 | |
290 | switch (runtime->sample_bits) { | |
291 | case 16: | |
5e7b9edd | 292 | return 8 << 16; |
d7bdbc5d | 293 | case 32: |
5e7b9edd | 294 | return 0 << 16; |
d7bdbc5d KM |
295 | } |
296 | ||
5e7b9edd KM |
297 | dev_warn(dev, "not supported sample bits\n"); |
298 | ||
299 | return 0; | |
d7bdbc5d KM |
300 | } |
301 | ||
4689032b KM |
302 | /* |
303 | * DALIGN function | |
304 | */ | |
305 | u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io) | |
306 | { | |
4689032b | 307 | struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io); |
a504b1ee | 308 | struct rsnd_mod *target; |
4689032b KM |
309 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
310 | u32 val = 0x76543210; | |
311 | u32 mask = ~0; | |
312 | ||
a504b1ee KM |
313 | if (rsnd_io_is_play(io)) { |
314 | struct rsnd_mod *src = rsnd_io_to_mod_src(io); | |
315 | ||
316 | target = src ? src : ssi; | |
317 | } else { | |
318 | struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io); | |
319 | ||
320 | target = cmd ? cmd : ssi; | |
321 | } | |
322 | ||
4689032b KM |
323 | mask <<= runtime->channels * 4; |
324 | val = val & mask; | |
325 | ||
326 | switch (runtime->sample_bits) { | |
327 | case 16: | |
328 | val |= 0x67452301 & ~mask; | |
329 | break; | |
330 | case 32: | |
331 | val |= 0x76543210 & ~mask; | |
332 | break; | |
333 | } | |
334 | ||
335 | /* | |
336 | * exchange channeles on SRC if possible, | |
337 | * otherwise, R/L volume settings on DVC | |
338 | * changes inverted channels | |
339 | */ | |
340 | if (mod == target) | |
341 | return val; | |
342 | else | |
343 | return 0x76543210; | |
344 | } | |
345 | ||
1536a968 KM |
346 | /* |
347 | * rsnd_dai functions | |
348 | */ | |
c2dc47d5 | 349 | #define rsnd_mod_call(idx, io, func, param...) \ |
d870a91e KM |
350 | ({ \ |
351 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \ | |
c2dc47d5 | 352 | struct rsnd_mod *mod = (io)->mod[idx]; \ |
d870a91e | 353 | struct device *dev = rsnd_priv_to_dev(priv); \ |
5ba17b42 | 354 | u32 *status = mod->get_status(io, mod, idx); \ |
5451ea44 | 355 | u32 mask = 0xF << __rsnd_mod_shift_##func; \ |
c2dc47d5 | 356 | u8 val = (*status >> __rsnd_mod_shift_##func) & 0xF; \ |
5451ea44 | 357 | u8 add = ((val + __rsnd_mod_add_##func) & 0xF); \ |
417f9642 | 358 | int ret = 0; \ |
32a96d55 | 359 | int call = (val == __rsnd_mod_call_##func) && (mod)->ops->func; \ |
6c001f3a KM |
360 | if (add == 0xF) \ |
361 | call = 0; \ | |
362 | else \ | |
363 | *status = (*status & ~mask) + \ | |
364 | (add << __rsnd_mod_shift_##func); \ | |
1355720a KM |
365 | dev_dbg(dev, "%s[%d]\t0x%08x %s\n", \ |
366 | rsnd_mod_name(mod), rsnd_mod_id(mod), \ | |
c2dc47d5 | 367 | *status, call ? #func : ""); \ |
1355720a KM |
368 | if (call) \ |
369 | ret = (mod)->ops->func(mod, io, param); \ | |
4e880b26 KM |
370 | if (ret) \ |
371 | dev_dbg(dev, "%s[%d] : rsnd_mod_call error %d\n", \ | |
372 | rsnd_mod_name(mod), rsnd_mod_id(mod), ret); \ | |
417f9642 | 373 | ret; \ |
d870a91e KM |
374 | }) |
375 | ||
38587f4c KM |
376 | static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = { |
377 | { | |
378 | /* CAPTURE */ | |
379 | RSND_MOD_AUDMAPP, | |
380 | RSND_MOD_AUDMA, | |
381 | RSND_MOD_DVC, | |
382 | RSND_MOD_MIX, | |
383 | RSND_MOD_CTU, | |
384 | RSND_MOD_CMD, | |
385 | RSND_MOD_SRC, | |
386 | RSND_MOD_SSIU, | |
387 | RSND_MOD_SSIM3, | |
388 | RSND_MOD_SSIM2, | |
389 | RSND_MOD_SSIM1, | |
390 | RSND_MOD_SSIP, | |
391 | RSND_MOD_SSI, | |
392 | }, { | |
393 | /* PLAYBACK */ | |
394 | RSND_MOD_AUDMAPP, | |
395 | RSND_MOD_AUDMA, | |
396 | RSND_MOD_SSIM3, | |
397 | RSND_MOD_SSIM2, | |
398 | RSND_MOD_SSIM1, | |
399 | RSND_MOD_SSIP, | |
400 | RSND_MOD_SSI, | |
401 | RSND_MOD_SSIU, | |
402 | RSND_MOD_DVC, | |
403 | RSND_MOD_MIX, | |
404 | RSND_MOD_CTU, | |
405 | RSND_MOD_CMD, | |
406 | RSND_MOD_SRC, | |
407 | }, | |
408 | }; | |
409 | ||
690602fc | 410 | #define rsnd_dai_call(fn, io, param...) \ |
d870a91e | 411 | ({ \ |
a126021d | 412 | struct rsnd_mod *mod; \ |
38587f4c | 413 | int type, is_play = rsnd_io_is_play(io); \ |
a126021d KM |
414 | int ret = 0, i; \ |
415 | for (i = 0; i < RSND_MOD_MAX; i++) { \ | |
38587f4c KM |
416 | type = rsnd_mod_sequence[is_play][i]; \ |
417 | mod = (io)->mod[type]; \ | |
a126021d KM |
418 | if (!mod) \ |
419 | continue; \ | |
38587f4c | 420 | ret |= rsnd_mod_call(type, io, fn, param); \ |
d870a91e KM |
421 | } \ |
422 | ret; \ | |
cdaa3cdf KM |
423 | }) |
424 | ||
27924f32 KM |
425 | int rsnd_dai_connect(struct rsnd_mod *mod, |
426 | struct rsnd_dai_stream *io, | |
427 | enum rsnd_mod_type type) | |
cdaa3cdf | 428 | { |
48725e9c KM |
429 | struct rsnd_priv *priv; |
430 | struct device *dev; | |
84e95355 | 431 | |
6020779b | 432 | if (!mod) |
cdaa3cdf | 433 | return -EIO; |
cdaa3cdf | 434 | |
bfa3119c KM |
435 | if (io->mod[type] == mod) |
436 | return 0; | |
437 | ||
52dc6852 KM |
438 | if (io->mod[type]) |
439 | return -EINVAL; | |
440 | ||
48725e9c KM |
441 | priv = rsnd_mod_to_priv(mod); |
442 | dev = rsnd_priv_to_dev(priv); | |
443 | ||
27924f32 | 444 | io->mod[type] = mod; |
cdaa3cdf | 445 | |
84e95355 KM |
446 | dev_dbg(dev, "%s[%d] is connected to io (%s)\n", |
447 | rsnd_mod_name(mod), rsnd_mod_id(mod), | |
448 | rsnd_io_is_play(io) ? "Playback" : "Capture"); | |
449 | ||
cdaa3cdf KM |
450 | return 0; |
451 | } | |
452 | ||
d3a76823 | 453 | static void rsnd_dai_disconnect(struct rsnd_mod *mod, |
27924f32 KM |
454 | struct rsnd_dai_stream *io, |
455 | enum rsnd_mod_type type) | |
d3a76823 | 456 | { |
27924f32 | 457 | io->mod[type] = NULL; |
d3a76823 KM |
458 | } |
459 | ||
710d0889 | 460 | struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id) |
1536a968 | 461 | { |
ecba9e72 | 462 | if ((id < 0) || (id >= rsnd_rdai_nr(priv))) |
2192f81c KM |
463 | return NULL; |
464 | ||
1536a968 KM |
465 | return priv->rdai + id; |
466 | } | |
467 | ||
eb2535f5 | 468 | #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai) |
1536a968 KM |
469 | static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai) |
470 | { | |
eb2535f5 | 471 | struct rsnd_priv *priv = rsnd_dai_to_priv(dai); |
1536a968 | 472 | |
710d0889 | 473 | return rsnd_rdai_get(priv, dai->id); |
1536a968 KM |
474 | } |
475 | ||
1536a968 KM |
476 | /* |
477 | * rsnd_soc_dai functions | |
478 | */ | |
479 | int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional) | |
480 | { | |
481 | struct snd_pcm_substream *substream = io->substream; | |
482 | struct snd_pcm_runtime *runtime = substream->runtime; | |
483 | int pos = io->byte_pos + additional; | |
484 | ||
485 | pos %= (runtime->periods * io->byte_per_period); | |
486 | ||
487 | return pos; | |
488 | } | |
489 | ||
75defee0 | 490 | bool rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte) |
1536a968 KM |
491 | { |
492 | io->byte_pos += byte; | |
493 | ||
494 | if (io->byte_pos >= io->next_period_byte) { | |
495 | struct snd_pcm_substream *substream = io->substream; | |
496 | struct snd_pcm_runtime *runtime = substream->runtime; | |
497 | ||
498 | io->period_pos++; | |
499 | io->next_period_byte += io->byte_per_period; | |
500 | ||
501 | if (io->period_pos >= runtime->periods) { | |
502 | io->byte_pos = 0; | |
503 | io->period_pos = 0; | |
504 | io->next_period_byte = io->byte_per_period; | |
505 | } | |
506 | ||
75defee0 | 507 | return true; |
1536a968 | 508 | } |
75defee0 KM |
509 | |
510 | return false; | |
511 | } | |
512 | ||
513 | void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io) | |
514 | { | |
515 | struct snd_pcm_substream *substream = io->substream; | |
516 | ||
517 | /* | |
518 | * this function should be called... | |
519 | * | |
520 | * - if rsnd_dai_pointer_update() returns true | |
521 | * - without spin lock | |
522 | */ | |
523 | ||
524 | snd_pcm_period_elapsed(substream); | |
1536a968 KM |
525 | } |
526 | ||
5626ad08 | 527 | static void rsnd_dai_stream_init(struct rsnd_dai_stream *io, |
1536a968 KM |
528 | struct snd_pcm_substream *substream) |
529 | { | |
530 | struct snd_pcm_runtime *runtime = substream->runtime; | |
531 | ||
1536a968 KM |
532 | io->substream = substream; |
533 | io->byte_pos = 0; | |
534 | io->period_pos = 0; | |
535 | io->byte_per_period = runtime->period_size * | |
536 | runtime->channels * | |
537 | samples_to_bytes(runtime, 1); | |
538 | io->next_period_byte = io->byte_per_period; | |
5626ad08 | 539 | } |
1536a968 | 540 | |
5626ad08 KM |
541 | static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io) |
542 | { | |
543 | io->substream = NULL; | |
1536a968 KM |
544 | } |
545 | ||
546 | static | |
547 | struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream) | |
548 | { | |
549 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
550 | ||
551 | return rtd->cpu_dai; | |
552 | } | |
553 | ||
554 | static | |
555 | struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai, | |
556 | struct snd_pcm_substream *substream) | |
557 | { | |
558 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
559 | return &rdai->playback; | |
560 | else | |
561 | return &rdai->capture; | |
562 | } | |
563 | ||
564 | static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd, | |
565 | struct snd_soc_dai *dai) | |
566 | { | |
eb2535f5 | 567 | struct rsnd_priv *priv = rsnd_dai_to_priv(dai); |
1536a968 KM |
568 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); |
569 | struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); | |
1536a968 KM |
570 | int ret; |
571 | unsigned long flags; | |
572 | ||
02299d98 | 573 | spin_lock_irqsave(&priv->lock, flags); |
1536a968 KM |
574 | |
575 | switch (cmd) { | |
576 | case SNDRV_PCM_TRIGGER_START: | |
5626ad08 | 577 | rsnd_dai_stream_init(io, substream); |
1536a968 | 578 | |
690602fc | 579 | ret = rsnd_dai_call(init, io, priv); |
cdaa3cdf KM |
580 | if (ret < 0) |
581 | goto dai_trigger_end; | |
582 | ||
690602fc | 583 | ret = rsnd_dai_call(start, io, priv); |
cdaa3cdf KM |
584 | if (ret < 0) |
585 | goto dai_trigger_end; | |
b5b442ab KM |
586 | |
587 | ret = rsnd_dai_call(irq, io, priv, 1); | |
588 | if (ret < 0) | |
589 | goto dai_trigger_end; | |
590 | ||
1536a968 KM |
591 | break; |
592 | case SNDRV_PCM_TRIGGER_STOP: | |
b5b442ab KM |
593 | ret = rsnd_dai_call(irq, io, priv, 0); |
594 | ||
595 | ret |= rsnd_dai_call(stop, io, priv); | |
cdaa3cdf | 596 | |
89e3e2c3 | 597 | ret |= rsnd_dai_call(quit, io, priv); |
cdaa3cdf | 598 | |
5626ad08 | 599 | rsnd_dai_stream_quit(io); |
1536a968 KM |
600 | break; |
601 | default: | |
602 | ret = -EINVAL; | |
603 | } | |
604 | ||
605 | dai_trigger_end: | |
02299d98 | 606 | spin_unlock_irqrestore(&priv->lock, flags); |
1536a968 KM |
607 | |
608 | return ret; | |
609 | } | |
610 | ||
611 | static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) | |
612 | { | |
613 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); | |
614 | ||
615 | /* set master/slave audio interface */ | |
616 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { | |
617 | case SND_SOC_DAIFMT_CBM_CFM: | |
e1508289 | 618 | rdai->clk_master = 0; |
1536a968 KM |
619 | break; |
620 | case SND_SOC_DAIFMT_CBS_CFS: | |
e1508289 | 621 | rdai->clk_master = 1; /* codec is slave, cpu is master */ |
1536a968 KM |
622 | break; |
623 | default: | |
624 | return -EINVAL; | |
625 | } | |
626 | ||
1536a968 KM |
627 | /* set format */ |
628 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { | |
629 | case SND_SOC_DAIFMT_I2S: | |
630 | rdai->sys_delay = 0; | |
631 | rdai->data_alignment = 0; | |
1a7889ca | 632 | rdai->frm_clk_inv = 0; |
1536a968 KM |
633 | break; |
634 | case SND_SOC_DAIFMT_LEFT_J: | |
635 | rdai->sys_delay = 1; | |
636 | rdai->data_alignment = 0; | |
1a7889ca | 637 | rdai->frm_clk_inv = 1; |
1536a968 KM |
638 | break; |
639 | case SND_SOC_DAIFMT_RIGHT_J: | |
640 | rdai->sys_delay = 1; | |
641 | rdai->data_alignment = 1; | |
1a7889ca KM |
642 | rdai->frm_clk_inv = 1; |
643 | break; | |
644 | } | |
645 | ||
646 | /* set clock inversion */ | |
647 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { | |
648 | case SND_SOC_DAIFMT_NB_IF: | |
649 | rdai->bit_clk_inv = rdai->bit_clk_inv; | |
650 | rdai->frm_clk_inv = !rdai->frm_clk_inv; | |
651 | break; | |
652 | case SND_SOC_DAIFMT_IB_NF: | |
653 | rdai->bit_clk_inv = !rdai->bit_clk_inv; | |
654 | rdai->frm_clk_inv = rdai->frm_clk_inv; | |
655 | break; | |
656 | case SND_SOC_DAIFMT_IB_IF: | |
657 | rdai->bit_clk_inv = !rdai->bit_clk_inv; | |
658 | rdai->frm_clk_inv = !rdai->frm_clk_inv; | |
659 | break; | |
660 | case SND_SOC_DAIFMT_NB_NF: | |
661 | default: | |
1536a968 KM |
662 | break; |
663 | } | |
664 | ||
665 | return 0; | |
666 | } | |
667 | ||
186fadc1 KM |
668 | static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai, |
669 | u32 tx_mask, u32 rx_mask, | |
670 | int slots, int slot_width) | |
671 | { | |
672 | struct rsnd_priv *priv = rsnd_dai_to_priv(dai); | |
673 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); | |
674 | struct device *dev = rsnd_priv_to_dev(priv); | |
675 | ||
676 | switch (slots) { | |
677 | case 6: | |
678 | /* TDM Extend Mode */ | |
750fd445 | 679 | rsnd_set_slot(rdai, slots, 1); |
186fadc1 KM |
680 | break; |
681 | default: | |
682 | dev_err(dev, "unsupported TDM slots (%d)\n", slots); | |
683 | return -EINVAL; | |
684 | } | |
685 | ||
686 | return 0; | |
687 | } | |
688 | ||
1536a968 KM |
689 | static const struct snd_soc_dai_ops rsnd_soc_dai_ops = { |
690 | .trigger = rsnd_soc_dai_trigger, | |
691 | .set_fmt = rsnd_soc_dai_set_fmt, | |
186fadc1 | 692 | .set_tdm_slot = rsnd_soc_set_dai_tdm_slot, |
1536a968 KM |
693 | }; |
694 | ||
89b66174 KM |
695 | void rsnd_parse_connect_common(struct rsnd_dai *rdai, |
696 | struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id), | |
697 | struct device_node *node, | |
698 | struct device_node *playback, | |
699 | struct device_node *capture) | |
700 | { | |
701 | struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai); | |
702 | struct device_node *np; | |
703 | struct rsnd_mod *mod; | |
704 | int i; | |
705 | ||
706 | if (!node) | |
707 | return; | |
708 | ||
709 | i = 0; | |
710 | for_each_child_of_node(node, np) { | |
711 | mod = mod_get(priv, i); | |
712 | if (np == playback) | |
713 | rsnd_dai_connect(mod, &rdai->playback, mod->type); | |
714 | if (np == capture) | |
715 | rsnd_dai_connect(mod, &rdai->capture, mod->type); | |
716 | i++; | |
717 | } | |
718 | ||
719 | of_node_put(node); | |
720 | } | |
721 | ||
2ea6b074 | 722 | static int rsnd_dai_probe(struct rsnd_priv *priv) |
90e8e50f | 723 | { |
94e2710c | 724 | struct device_node *dai_node; |
89b66174 | 725 | struct device_node *dai_np; |
90e8e50f | 726 | struct device_node *playback, *capture; |
94e2710c KM |
727 | struct rsnd_dai_stream *io_playback; |
728 | struct rsnd_dai_stream *io_capture; | |
2ff2ecca | 729 | struct snd_soc_dai_driver *rdrv, *drv; |
94e2710c | 730 | struct rsnd_dai *rdai; |
2ea6b074 | 731 | struct device *dev = rsnd_priv_to_dev(priv); |
89b66174 | 732 | int nr, dai_i, io_i; |
94e2710c | 733 | int ret; |
90e8e50f | 734 | |
94e2710c | 735 | dai_node = rsnd_dai_of_node(priv); |
90e8e50f | 736 | nr = of_get_child_count(dai_node); |
94e2710c KM |
737 | if (!nr) { |
738 | ret = -EINVAL; | |
739 | goto rsnd_dai_probe_done; | |
90e8e50f KM |
740 | } |
741 | ||
2ff2ecca | 742 | rdrv = devm_kzalloc(dev, sizeof(*rdrv) * nr, GFP_KERNEL); |
94e2710c | 743 | rdai = devm_kzalloc(dev, sizeof(*rdai) * nr, GFP_KERNEL); |
2ff2ecca | 744 | if (!rdrv || !rdai) { |
94e2710c KM |
745 | ret = -ENOMEM; |
746 | goto rsnd_dai_probe_done; | |
747 | } | |
90e8e50f | 748 | |
94e2710c | 749 | priv->rdai_nr = nr; |
2ff2ecca | 750 | priv->daidrv = rdrv; |
94e2710c | 751 | priv->rdai = rdai; |
90e8e50f KM |
752 | |
753 | /* | |
754 | * parse all dai | |
755 | */ | |
756 | dai_i = 0; | |
757 | for_each_child_of_node(dai_node, dai_np) { | |
94e2710c | 758 | rdai = rsnd_rdai_get(priv, dai_i); |
2ff2ecca | 759 | drv = rdrv + dai_i; |
94e2710c KM |
760 | io_playback = &rdai->playback; |
761 | io_capture = &rdai->capture; | |
762 | ||
763 | snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i); | |
764 | ||
765 | rdai->priv = priv; | |
766 | drv->name = rdai->name; | |
767 | drv->ops = &rsnd_soc_dai_ops; | |
768 | ||
769 | snprintf(rdai->playback.name, RSND_DAI_NAME_SIZE, | |
770 | "DAI%d Playback", dai_i); | |
771 | drv->playback.rates = RSND_RATES; | |
772 | drv->playback.formats = RSND_FMTS; | |
773 | drv->playback.channels_min = 2; | |
186fadc1 | 774 | drv->playback.channels_max = 6; |
94e2710c KM |
775 | drv->playback.stream_name = rdai->playback.name; |
776 | ||
777 | snprintf(rdai->capture.name, RSND_DAI_NAME_SIZE, | |
778 | "DAI%d Capture", dai_i); | |
779 | drv->capture.rates = RSND_RATES; | |
780 | drv->capture.formats = RSND_FMTS; | |
781 | drv->capture.channels_min = 2; | |
186fadc1 | 782 | drv->capture.channels_max = 6; |
94e2710c KM |
783 | drv->capture.stream_name = rdai->capture.name; |
784 | ||
785 | rdai->playback.rdai = rdai; | |
786 | rdai->capture.rdai = rdai; | |
750fd445 | 787 | rsnd_set_slot(rdai, 2, 1); /* default */ |
90e8e50f | 788 | |
94e2710c KM |
789 | for (io_i = 0;; io_i++) { |
790 | playback = of_parse_phandle(dai_np, "playback", io_i); | |
791 | capture = of_parse_phandle(dai_np, "capture", io_i); | |
90e8e50f KM |
792 | |
793 | if (!playback && !capture) | |
794 | break; | |
795 | ||
89b66174 KM |
796 | rsnd_parse_connect_ssi(rdai, playback, capture); |
797 | rsnd_parse_connect_src(rdai, playback, capture); | |
798 | rsnd_parse_connect_ctu(rdai, playback, capture); | |
799 | rsnd_parse_connect_mix(rdai, playback, capture); | |
800 | rsnd_parse_connect_dvc(rdai, playback, capture); | |
90e8e50f | 801 | |
a493b6a6 JL |
802 | of_node_put(playback); |
803 | of_node_put(capture); | |
90e8e50f KM |
804 | } |
805 | ||
806 | dai_i++; | |
1536a968 | 807 | |
94e2710c KM |
808 | dev_dbg(dev, "%s (%s/%s)\n", rdai->name, |
809 | rsnd_io_to_mod_ssi(io_playback) ? "play" : " -- ", | |
810 | rsnd_io_to_mod_ssi(io_capture) ? "capture" : " -- "); | |
1536a968 KM |
811 | } |
812 | ||
94e2710c | 813 | ret = 0; |
49848073 | 814 | |
94e2710c KM |
815 | rsnd_dai_probe_done: |
816 | of_node_put(dai_node); | |
1536a968 | 817 | |
94e2710c | 818 | return ret; |
1536a968 KM |
819 | } |
820 | ||
1536a968 KM |
821 | /* |
822 | * pcm ops | |
823 | */ | |
824 | static struct snd_pcm_hardware rsnd_pcm_hardware = { | |
825 | .info = SNDRV_PCM_INFO_INTERLEAVED | | |
826 | SNDRV_PCM_INFO_MMAP | | |
706c6621 | 827 | SNDRV_PCM_INFO_MMAP_VALID, |
1536a968 KM |
828 | .buffer_bytes_max = 64 * 1024, |
829 | .period_bytes_min = 32, | |
830 | .period_bytes_max = 8192, | |
831 | .periods_min = 1, | |
832 | .periods_max = 32, | |
833 | .fifo_size = 256, | |
834 | }; | |
835 | ||
836 | static int rsnd_pcm_open(struct snd_pcm_substream *substream) | |
837 | { | |
838 | struct snd_pcm_runtime *runtime = substream->runtime; | |
839 | int ret = 0; | |
840 | ||
841 | snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware); | |
842 | ||
843 | ret = snd_pcm_hw_constraint_integer(runtime, | |
844 | SNDRV_PCM_HW_PARAM_PERIODS); | |
845 | ||
846 | return ret; | |
847 | } | |
848 | ||
849 | static int rsnd_hw_params(struct snd_pcm_substream *substream, | |
850 | struct snd_pcm_hw_params *hw_params) | |
851 | { | |
3b7843ff KM |
852 | struct snd_soc_dai *dai = rsnd_substream_to_dai(substream); |
853 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); | |
854 | struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); | |
855 | int ret; | |
856 | ||
857 | ret = rsnd_dai_call(hw_params, io, substream, hw_params); | |
858 | if (ret) | |
859 | return ret; | |
860 | ||
1536a968 KM |
861 | return snd_pcm_lib_malloc_pages(substream, |
862 | params_buffer_bytes(hw_params)); | |
863 | } | |
864 | ||
865 | static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream) | |
866 | { | |
867 | struct snd_pcm_runtime *runtime = substream->runtime; | |
868 | struct snd_soc_dai *dai = rsnd_substream_to_dai(substream); | |
869 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); | |
870 | struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); | |
871 | ||
872 | return bytes_to_frames(runtime, io->byte_pos); | |
873 | } | |
874 | ||
875 | static struct snd_pcm_ops rsnd_pcm_ops = { | |
876 | .open = rsnd_pcm_open, | |
877 | .ioctl = snd_pcm_lib_ioctl, | |
878 | .hw_params = rsnd_hw_params, | |
879 | .hw_free = snd_pcm_lib_free_pages, | |
880 | .pointer = rsnd_pointer, | |
881 | }; | |
882 | ||
170a2497 KM |
883 | /* |
884 | * snd_kcontrol | |
885 | */ | |
886 | #define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value) | |
887 | static int rsnd_kctrl_info(struct snd_kcontrol *kctrl, | |
888 | struct snd_ctl_elem_info *uinfo) | |
889 | { | |
890 | struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl); | |
891 | ||
892 | if (cfg->texts) { | |
893 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | |
894 | uinfo->count = cfg->size; | |
895 | uinfo->value.enumerated.items = cfg->max; | |
896 | if (uinfo->value.enumerated.item >= cfg->max) | |
897 | uinfo->value.enumerated.item = cfg->max - 1; | |
898 | strlcpy(uinfo->value.enumerated.name, | |
899 | cfg->texts[uinfo->value.enumerated.item], | |
900 | sizeof(uinfo->value.enumerated.name)); | |
901 | } else { | |
902 | uinfo->count = cfg->size; | |
903 | uinfo->value.integer.min = 0; | |
904 | uinfo->value.integer.max = cfg->max; | |
905 | uinfo->type = (cfg->max == 1) ? | |
906 | SNDRV_CTL_ELEM_TYPE_BOOLEAN : | |
907 | SNDRV_CTL_ELEM_TYPE_INTEGER; | |
908 | } | |
909 | ||
910 | return 0; | |
911 | } | |
912 | ||
913 | static int rsnd_kctrl_get(struct snd_kcontrol *kctrl, | |
914 | struct snd_ctl_elem_value *uc) | |
915 | { | |
916 | struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl); | |
917 | int i; | |
918 | ||
919 | for (i = 0; i < cfg->size; i++) | |
920 | if (cfg->texts) | |
921 | uc->value.enumerated.item[i] = cfg->val[i]; | |
922 | else | |
923 | uc->value.integer.value[i] = cfg->val[i]; | |
924 | ||
925 | return 0; | |
926 | } | |
927 | ||
928 | static int rsnd_kctrl_put(struct snd_kcontrol *kctrl, | |
929 | struct snd_ctl_elem_value *uc) | |
930 | { | |
931 | struct rsnd_mod *mod = snd_kcontrol_chip(kctrl); | |
932 | struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl); | |
933 | int i, change = 0; | |
934 | ||
935 | for (i = 0; i < cfg->size; i++) { | |
936 | if (cfg->texts) { | |
937 | change |= (uc->value.enumerated.item[i] != cfg->val[i]); | |
938 | cfg->val[i] = uc->value.enumerated.item[i]; | |
939 | } else { | |
940 | change |= (uc->value.integer.value[i] != cfg->val[i]); | |
941 | cfg->val[i] = uc->value.integer.value[i]; | |
942 | } | |
943 | } | |
944 | ||
d7289565 | 945 | if (change && cfg->update) |
b65a7ccc | 946 | cfg->update(cfg->io, mod); |
170a2497 KM |
947 | |
948 | return change; | |
949 | } | |
950 | ||
951 | static int __rsnd_kctrl_new(struct rsnd_mod *mod, | |
b65a7ccc | 952 | struct rsnd_dai_stream *io, |
170a2497 KM |
953 | struct snd_soc_pcm_runtime *rtd, |
954 | const unsigned char *name, | |
955 | struct rsnd_kctrl_cfg *cfg, | |
b65a7ccc KM |
956 | void (*update)(struct rsnd_dai_stream *io, |
957 | struct rsnd_mod *mod)) | |
170a2497 KM |
958 | { |
959 | struct snd_card *card = rtd->card->snd_card; | |
960 | struct snd_kcontrol *kctrl; | |
961 | struct snd_kcontrol_new knew = { | |
962 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
963 | .name = name, | |
964 | .info = rsnd_kctrl_info, | |
1a497983 | 965 | .index = rtd->num, |
170a2497 KM |
966 | .get = rsnd_kctrl_get, |
967 | .put = rsnd_kctrl_put, | |
968 | .private_value = (unsigned long)cfg, | |
969 | }; | |
970 | int ret; | |
971 | ||
972 | kctrl = snd_ctl_new1(&knew, mod); | |
973 | if (!kctrl) | |
974 | return -ENOMEM; | |
975 | ||
976 | ret = snd_ctl_add(card, kctrl); | |
d1f83d6e KM |
977 | if (ret < 0) { |
978 | snd_ctl_free_one(kctrl); | |
170a2497 | 979 | return ret; |
d1f83d6e | 980 | } |
170a2497 KM |
981 | |
982 | cfg->update = update; | |
d1f83d6e KM |
983 | cfg->card = card; |
984 | cfg->kctrl = kctrl; | |
b65a7ccc | 985 | cfg->io = io; |
170a2497 KM |
986 | |
987 | return 0; | |
988 | } | |
989 | ||
d1f83d6e KM |
990 | void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg *cfg) |
991 | { | |
992 | snd_ctl_remove(cfg->card, cfg->kctrl); | |
993 | } | |
994 | ||
170a2497 | 995 | int rsnd_kctrl_new_m(struct rsnd_mod *mod, |
b65a7ccc | 996 | struct rsnd_dai_stream *io, |
170a2497 KM |
997 | struct snd_soc_pcm_runtime *rtd, |
998 | const unsigned char *name, | |
b65a7ccc KM |
999 | void (*update)(struct rsnd_dai_stream *io, |
1000 | struct rsnd_mod *mod), | |
170a2497 | 1001 | struct rsnd_kctrl_cfg_m *_cfg, |
42ab9a79 | 1002 | int ch_size, |
170a2497 KM |
1003 | u32 max) |
1004 | { | |
d2240f0d | 1005 | if (ch_size > RSND_MAX_CHANNELS) |
42ab9a79 KM |
1006 | return -EINVAL; |
1007 | ||
170a2497 | 1008 | _cfg->cfg.max = max; |
42ab9a79 | 1009 | _cfg->cfg.size = ch_size; |
170a2497 | 1010 | _cfg->cfg.val = _cfg->val; |
b65a7ccc | 1011 | return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update); |
170a2497 KM |
1012 | } |
1013 | ||
1014 | int rsnd_kctrl_new_s(struct rsnd_mod *mod, | |
b65a7ccc | 1015 | struct rsnd_dai_stream *io, |
170a2497 KM |
1016 | struct snd_soc_pcm_runtime *rtd, |
1017 | const unsigned char *name, | |
b65a7ccc KM |
1018 | void (*update)(struct rsnd_dai_stream *io, |
1019 | struct rsnd_mod *mod), | |
170a2497 KM |
1020 | struct rsnd_kctrl_cfg_s *_cfg, |
1021 | u32 max) | |
1022 | { | |
1023 | _cfg->cfg.max = max; | |
1024 | _cfg->cfg.size = 1; | |
1025 | _cfg->cfg.val = &_cfg->val; | |
b65a7ccc | 1026 | return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update); |
170a2497 KM |
1027 | } |
1028 | ||
1029 | int rsnd_kctrl_new_e(struct rsnd_mod *mod, | |
b65a7ccc | 1030 | struct rsnd_dai_stream *io, |
170a2497 KM |
1031 | struct snd_soc_pcm_runtime *rtd, |
1032 | const unsigned char *name, | |
1033 | struct rsnd_kctrl_cfg_s *_cfg, | |
b65a7ccc KM |
1034 | void (*update)(struct rsnd_dai_stream *io, |
1035 | struct rsnd_mod *mod), | |
170a2497 KM |
1036 | const char * const *texts, |
1037 | u32 max) | |
1038 | { | |
1039 | _cfg->cfg.max = max; | |
1040 | _cfg->cfg.size = 1; | |
1041 | _cfg->cfg.val = &_cfg->val; | |
1042 | _cfg->cfg.texts = texts; | |
b65a7ccc | 1043 | return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update); |
170a2497 KM |
1044 | } |
1045 | ||
1536a968 KM |
1046 | /* |
1047 | * snd_soc_platform | |
1048 | */ | |
1049 | ||
1050 | #define PREALLOC_BUFFER (32 * 1024) | |
1051 | #define PREALLOC_BUFFER_MAX (32 * 1024) | |
1052 | ||
1053 | static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd) | |
1054 | { | |
7c63f3c0 KM |
1055 | struct snd_soc_dai *dai = rtd->cpu_dai; |
1056 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); | |
ae11a9be | 1057 | int ret; |
bff58ea4 | 1058 | |
ae11a9be KM |
1059 | ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd); |
1060 | if (ret) | |
1061 | return ret; | |
bff58ea4 | 1062 | |
ae11a9be | 1063 | ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd); |
7c63f3c0 KM |
1064 | if (ret) |
1065 | return ret; | |
bff58ea4 | 1066 | |
1536a968 KM |
1067 | return snd_pcm_lib_preallocate_pages_for_all( |
1068 | rtd->pcm, | |
1069 | SNDRV_DMA_TYPE_DEV, | |
1070 | rtd->card->snd_card->dev, | |
1071 | PREALLOC_BUFFER, PREALLOC_BUFFER_MAX); | |
1072 | } | |
1073 | ||
1536a968 KM |
1074 | static struct snd_soc_platform_driver rsnd_soc_platform = { |
1075 | .ops = &rsnd_pcm_ops, | |
1076 | .pcm_new = rsnd_pcm_new, | |
1536a968 KM |
1077 | }; |
1078 | ||
1079 | static const struct snd_soc_component_driver rsnd_soc_component = { | |
1080 | .name = "rsnd", | |
1081 | }; | |
1082 | ||
d3a76823 | 1083 | static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv, |
f708d944 | 1084 | struct rsnd_dai_stream *io) |
d3a76823 | 1085 | { |
d3a76823 KM |
1086 | int ret; |
1087 | ||
690602fc | 1088 | ret = rsnd_dai_call(probe, io, priv); |
d3a76823 | 1089 | if (ret == -EAGAIN) { |
48d58281 KM |
1090 | struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io); |
1091 | int i; | |
1092 | ||
d3a76823 KM |
1093 | /* |
1094 | * Fallback to PIO mode | |
1095 | */ | |
1096 | ||
1097 | /* | |
1098 | * call "remove" for SSI/SRC/DVC | |
1099 | * SSI will be switch to PIO mode if it was DMA mode | |
1100 | * see | |
1101 | * rsnd_dma_init() | |
97463e19 | 1102 | * rsnd_ssi_fallback() |
d3a76823 | 1103 | */ |
690602fc | 1104 | rsnd_dai_call(remove, io, priv); |
d3a76823 KM |
1105 | |
1106 | /* | |
48d58281 KM |
1107 | * remove all mod from io |
1108 | * and, re connect ssi | |
d3a76823 | 1109 | */ |
48d58281 KM |
1110 | for (i = 0; i < RSND_MOD_MAX; i++) |
1111 | rsnd_dai_disconnect((io)->mod[i], io, i); | |
1112 | rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI); | |
d3a76823 | 1113 | |
97463e19 KM |
1114 | /* |
1115 | * fallback | |
1116 | */ | |
690602fc | 1117 | rsnd_dai_call(fallback, io, priv); |
97463e19 | 1118 | |
d3a76823 KM |
1119 | /* |
1120 | * retry to "probe". | |
1121 | * DAI has SSI which is PIO mode only now. | |
1122 | */ | |
690602fc | 1123 | ret = rsnd_dai_call(probe, io, priv); |
d3a76823 KM |
1124 | } |
1125 | ||
1126 | return ret; | |
1127 | } | |
1128 | ||
1536a968 KM |
1129 | /* |
1130 | * rsnd probe | |
1131 | */ | |
1132 | static int rsnd_probe(struct platform_device *pdev) | |
1133 | { | |
1536a968 KM |
1134 | struct rsnd_priv *priv; |
1135 | struct device *dev = &pdev->dev; | |
7681f6ac | 1136 | struct rsnd_dai *rdai; |
2ea6b074 | 1137 | int (*probe_func[])(struct rsnd_priv *priv) = { |
d1ac970f | 1138 | rsnd_gen_probe, |
288f392e | 1139 | rsnd_dma_probe, |
d1ac970f | 1140 | rsnd_ssi_probe, |
c7f69ab5 | 1141 | rsnd_ssiu_probe, |
ba9c949f | 1142 | rsnd_src_probe, |
9269e3c3 | 1143 | rsnd_ctu_probe, |
70fb1052 | 1144 | rsnd_mix_probe, |
bff58ea4 | 1145 | rsnd_dvc_probe, |
1b2ca0ad | 1146 | rsnd_cmd_probe, |
d1ac970f KM |
1147 | rsnd_adg_probe, |
1148 | rsnd_dai_probe, | |
1149 | }; | |
1150 | int ret, i; | |
1536a968 | 1151 | |
1536a968 KM |
1152 | /* |
1153 | * init priv data | |
1154 | */ | |
1155 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); | |
1156 | if (!priv) { | |
1157 | dev_err(dev, "priv allocate failed\n"); | |
1158 | return -ENODEV; | |
1159 | } | |
1160 | ||
9f464f8e | 1161 | priv->pdev = pdev; |
6d8044b4 | 1162 | priv->flags = (unsigned long)of_device_get_match_data(dev); |
1536a968 KM |
1163 | spin_lock_init(&priv->lock); |
1164 | ||
1165 | /* | |
1166 | * init each module | |
1167 | */ | |
d1ac970f | 1168 | for (i = 0; i < ARRAY_SIZE(probe_func); i++) { |
2ea6b074 | 1169 | ret = probe_func[i](priv); |
d1ac970f KM |
1170 | if (ret) |
1171 | return ret; | |
1172 | } | |
07539c1d | 1173 | |
7681f6ac | 1174 | for_each_rsnd_dai(rdai, priv, i) { |
f708d944 | 1175 | ret = rsnd_rdai_continuance_probe(priv, &rdai->playback); |
7681f6ac | 1176 | if (ret) |
d62a3dcd | 1177 | goto exit_snd_probe; |
dfc9403b | 1178 | |
f708d944 | 1179 | ret = rsnd_rdai_continuance_probe(priv, &rdai->capture); |
7681f6ac | 1180 | if (ret) |
d62a3dcd | 1181 | goto exit_snd_probe; |
7681f6ac | 1182 | } |
4b4dab82 | 1183 | |
0b1f6ec7 KM |
1184 | dev_set_drvdata(dev, priv); |
1185 | ||
1536a968 KM |
1186 | /* |
1187 | * asoc register | |
1188 | */ | |
1189 | ret = snd_soc_register_platform(dev, &rsnd_soc_platform); | |
1190 | if (ret < 0) { | |
1191 | dev_err(dev, "cannot snd soc register\n"); | |
1192 | return ret; | |
1193 | } | |
1194 | ||
1195 | ret = snd_soc_register_component(dev, &rsnd_soc_component, | |
ecba9e72 | 1196 | priv->daidrv, rsnd_rdai_nr(priv)); |
1536a968 KM |
1197 | if (ret < 0) { |
1198 | dev_err(dev, "cannot snd dai register\n"); | |
1199 | goto exit_snd_soc; | |
1200 | } | |
1201 | ||
1536a968 KM |
1202 | pm_runtime_enable(dev); |
1203 | ||
1204 | dev_info(dev, "probed\n"); | |
1205 | return ret; | |
1206 | ||
1207 | exit_snd_soc: | |
1208 | snd_soc_unregister_platform(dev); | |
d62a3dcd KM |
1209 | exit_snd_probe: |
1210 | for_each_rsnd_dai(rdai, priv, i) { | |
690602fc KM |
1211 | rsnd_dai_call(remove, &rdai->playback, priv); |
1212 | rsnd_dai_call(remove, &rdai->capture, priv); | |
d62a3dcd | 1213 | } |
1536a968 KM |
1214 | |
1215 | return ret; | |
1216 | } | |
1217 | ||
1218 | static int rsnd_remove(struct platform_device *pdev) | |
1219 | { | |
1220 | struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev); | |
7681f6ac | 1221 | struct rsnd_dai *rdai; |
2ea6b074 | 1222 | void (*remove_func[])(struct rsnd_priv *priv) = { |
2f78dd7f | 1223 | rsnd_ssi_remove, |
c7f69ab5 | 1224 | rsnd_ssiu_remove, |
2f78dd7f | 1225 | rsnd_src_remove, |
9269e3c3 | 1226 | rsnd_ctu_remove, |
70fb1052 | 1227 | rsnd_mix_remove, |
2f78dd7f | 1228 | rsnd_dvc_remove, |
1b2ca0ad | 1229 | rsnd_cmd_remove, |
68a55024 | 1230 | rsnd_adg_remove, |
2f78dd7f | 1231 | }; |
d62a3dcd | 1232 | int ret = 0, i; |
1536a968 KM |
1233 | |
1234 | pm_runtime_disable(&pdev->dev); | |
1235 | ||
7681f6ac | 1236 | for_each_rsnd_dai(rdai, priv, i) { |
690602fc KM |
1237 | ret |= rsnd_dai_call(remove, &rdai->playback, priv); |
1238 | ret |= rsnd_dai_call(remove, &rdai->capture, priv); | |
7681f6ac | 1239 | } |
1536a968 | 1240 | |
2f78dd7f | 1241 | for (i = 0; i < ARRAY_SIZE(remove_func); i++) |
2ea6b074 | 1242 | remove_func[i](priv); |
2f78dd7f | 1243 | |
d7c42ff8 KM |
1244 | snd_soc_unregister_component(&pdev->dev); |
1245 | snd_soc_unregister_platform(&pdev->dev); | |
1246 | ||
d62a3dcd | 1247 | return ret; |
1536a968 KM |
1248 | } |
1249 | ||
1250 | static struct platform_driver rsnd_driver = { | |
1251 | .driver = { | |
1252 | .name = "rcar_sound", | |
90e8e50f | 1253 | .of_match_table = rsnd_of_match, |
1536a968 KM |
1254 | }, |
1255 | .probe = rsnd_probe, | |
1256 | .remove = rsnd_remove, | |
1257 | }; | |
1258 | module_platform_driver(rsnd_driver); | |
1259 | ||
1260 | MODULE_LICENSE("GPL"); | |
1261 | MODULE_DESCRIPTION("Renesas R-Car audio driver"); | |
1262 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); | |
1263 | MODULE_ALIAS("platform:rcar-pcm-audio"); |