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