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