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> | |
9ade09d6 | 97 | #include <linux/shdma-base.h> |
1536a968 KM |
98 | #include "rsnd.h" |
99 | ||
100 | #define RSND_RATES SNDRV_PCM_RATE_8000_96000 | |
101 | #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE) | |
102 | ||
90e8e50f KM |
103 | static struct rsnd_of_data rsnd_of_data_gen1 = { |
104 | .flags = RSND_GEN1, | |
105 | }; | |
106 | ||
107 | static struct rsnd_of_data rsnd_of_data_gen2 = { | |
108 | .flags = RSND_GEN2, | |
109 | }; | |
110 | ||
111 | static struct of_device_id rsnd_of_match[] = { | |
112 | { .compatible = "renesas,rcar_sound-gen1", .data = &rsnd_of_data_gen1 }, | |
113 | { .compatible = "renesas,rcar_sound-gen2", .data = &rsnd_of_data_gen2 }, | |
114 | {}, | |
115 | }; | |
116 | MODULE_DEVICE_TABLE(of, rsnd_of_match); | |
117 | ||
1536a968 KM |
118 | /* |
119 | * rsnd_platform functions | |
120 | */ | |
121 | #define rsnd_platform_call(priv, dai, func, param...) \ | |
740ad6c3 | 122 | (!(priv->info->func) ? 0 : \ |
1536a968 KM |
123 | priv->info->func(param)) |
124 | ||
389933d9 KM |
125 | #define rsnd_is_enable_path(io, name) \ |
126 | ((io)->info ? (io)->info->name : NULL) | |
127 | #define rsnd_info_id(priv, io, name) \ | |
128 | ((io)->info->name - priv->info->name##_info) | |
129 | ||
cdaa3cdf KM |
130 | /* |
131 | * rsnd_mod functions | |
132 | */ | |
133 | char *rsnd_mod_name(struct rsnd_mod *mod) | |
134 | { | |
135 | if (!mod || !mod->ops) | |
136 | return "unknown"; | |
137 | ||
138 | return mod->ops->name; | |
139 | } | |
140 | ||
d9288d0b KM |
141 | char *rsnd_mod_dma_name(struct rsnd_mod *mod) |
142 | { | |
143 | if (!mod || !mod->ops) | |
144 | return "unknown"; | |
145 | ||
146 | if (!mod->ops->dma_name) | |
147 | return mod->ops->name; | |
148 | ||
149 | return mod->ops->dma_name(mod); | |
150 | } | |
151 | ||
cdaa3cdf KM |
152 | void rsnd_mod_init(struct rsnd_priv *priv, |
153 | struct rsnd_mod *mod, | |
154 | struct rsnd_mod_ops *ops, | |
85642952 | 155 | struct clk *clk, |
a126021d | 156 | enum rsnd_mod_type type, |
cdaa3cdf KM |
157 | int id) |
158 | { | |
159 | mod->priv = priv; | |
160 | mod->id = id; | |
161 | mod->ops = ops; | |
a126021d | 162 | mod->type = type; |
85642952 | 163 | mod->clk = clk; |
cdaa3cdf KM |
164 | } |
165 | ||
0a4d94c0 KM |
166 | /* |
167 | * rsnd_dma functions | |
168 | */ | |
0a4d94c0 KM |
169 | void rsnd_dma_stop(struct rsnd_dma *dma) |
170 | { | |
0a4d94c0 KM |
171 | dmaengine_terminate_all(dma->chan); |
172 | } | |
173 | ||
174 | static void rsnd_dma_complete(void *data) | |
175 | { | |
176 | struct rsnd_dma *dma = (struct rsnd_dma *)data; | |
4686a0ad | 177 | struct rsnd_mod *mod = rsnd_dma_to_mod(dma); |
4686a0ad | 178 | struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); |
0a4d94c0 | 179 | |
4686a0ad KM |
180 | /* |
181 | * Renesas sound Gen1 needs 1 DMAC, | |
182 | * Gen2 needs 2 DMAC. | |
183 | * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri. | |
184 | * But, Audio-DMAC-peri-peri doesn't have interrupt, | |
185 | * and this driver is assuming that here. | |
186 | * | |
187 | * If Audio-DMAC-peri-peri has interrpt, | |
188 | * rsnd_dai_pointer_update() will be called twice, | |
189 | * ant it will breaks io->byte_pos | |
190 | */ | |
836b31fe KM |
191 | |
192 | rsnd_dai_pointer_update(io, io->byte_per_period); | |
0a4d94c0 KM |
193 | } |
194 | ||
ccd01559 | 195 | void rsnd_dma_start(struct rsnd_dma *dma) |
0a4d94c0 | 196 | { |
4686a0ad KM |
197 | struct rsnd_mod *mod = rsnd_dma_to_mod(dma); |
198 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); | |
199 | struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); | |
ccd01559 | 200 | struct snd_pcm_substream *substream = io->substream; |
0a4d94c0 KM |
201 | struct device *dev = rsnd_priv_to_dev(priv); |
202 | struct dma_async_tx_descriptor *desc; | |
0a4d94c0 | 203 | |
ccd01559 | 204 | desc = dmaengine_prep_dma_cyclic(dma->chan, |
34037100 | 205 | (dma->addr) ? dma->addr : |
ccd01559 KM |
206 | substream->runtime->dma_addr, |
207 | snd_pcm_lib_buffer_bytes(substream), | |
208 | snd_pcm_lib_period_bytes(substream), | |
209 | dma->dir, | |
210 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); | |
0a4d94c0 | 211 | |
ccd01559 KM |
212 | if (!desc) { |
213 | dev_err(dev, "dmaengine_prep_slave_sg() fail\n"); | |
214 | return; | |
0a4d94c0 | 215 | } |
0a4d94c0 | 216 | |
ccd01559 KM |
217 | desc->callback = rsnd_dma_complete; |
218 | desc->callback_param = dma; | |
219 | ||
220 | if (dmaengine_submit(desc) < 0) { | |
221 | dev_err(dev, "dmaengine_submit() fail\n"); | |
222 | return; | |
223 | } | |
f5cab3b8 | 224 | |
ccd01559 | 225 | dma_async_issue_pending(dma->chan); |
f5cab3b8 KM |
226 | } |
227 | ||
0a4d94c0 KM |
228 | int rsnd_dma_available(struct rsnd_dma *dma) |
229 | { | |
230 | return !!dma->chan; | |
231 | } | |
232 | ||
199e7688 KM |
233 | #define DMA_NAME_SIZE 16 |
234 | #define MOD_MAX 4 /* MEM/SSI/SRC/DVC */ | |
235 | static int _rsnd_dma_of_name(char *dma_name, struct rsnd_mod *mod) | |
236 | { | |
237 | if (mod) | |
238 | return snprintf(dma_name, DMA_NAME_SIZE / 2, "%s%d", | |
d9288d0b | 239 | rsnd_mod_dma_name(mod), rsnd_mod_id(mod)); |
199e7688 KM |
240 | else |
241 | return snprintf(dma_name, DMA_NAME_SIZE / 2, "mem"); | |
242 | ||
243 | } | |
244 | ||
37523034 KM |
245 | static void rsnd_dma_of_name(struct rsnd_mod *mod_from, |
246 | struct rsnd_mod *mod_to, | |
247 | char *dma_name) | |
248 | { | |
249 | int index = 0; | |
250 | ||
251 | index = _rsnd_dma_of_name(dma_name + index, mod_from); | |
252 | *(dma_name + index++) = '_'; | |
253 | index = _rsnd_dma_of_name(dma_name + index, mod_to); | |
254 | } | |
255 | ||
256 | static void rsnd_dma_of_path(struct rsnd_dma *dma, | |
257 | int is_play, | |
258 | struct rsnd_mod **mod_from, | |
259 | struct rsnd_mod **mod_to) | |
199e7688 KM |
260 | { |
261 | struct rsnd_mod *this = rsnd_dma_to_mod(dma); | |
262 | struct rsnd_dai_stream *io = rsnd_mod_to_io(this); | |
263 | struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io); | |
264 | struct rsnd_mod *src = rsnd_io_to_mod_src(io); | |
265 | struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io); | |
266 | struct rsnd_mod *mod[MOD_MAX]; | |
199e7688 KM |
267 | int i, index; |
268 | ||
269 | ||
270 | for (i = 0; i < MOD_MAX; i++) | |
271 | mod[i] = NULL; | |
272 | ||
273 | /* | |
274 | * in play case... | |
275 | * | |
276 | * src -> dst | |
277 | * | |
278 | * mem -> SSI | |
279 | * mem -> SRC -> SSI | |
280 | * mem -> SRC -> DVC -> SSI | |
281 | */ | |
282 | mod[0] = NULL; /* for "mem" */ | |
283 | index = 1; | |
284 | for (i = 1; i < MOD_MAX; i++) { | |
285 | if (!src) { | |
286 | mod[i] = ssi; | |
199e7688 KM |
287 | } else if (!dvc) { |
288 | mod[i] = src; | |
289 | src = NULL; | |
290 | } else { | |
34cb6123 KM |
291 | if ((!is_play) && (this == src)) |
292 | this = dvc; | |
293 | ||
294 | mod[i] = (is_play) ? src : dvc; | |
295 | i++; | |
296 | mod[i] = (is_play) ? dvc : src; | |
297 | src = NULL; | |
199e7688 KM |
298 | dvc = NULL; |
299 | } | |
300 | ||
301 | if (mod[i] == this) | |
302 | index = i; | |
c08c3b08 KM |
303 | |
304 | if (mod[i] == ssi) | |
305 | break; | |
199e7688 KM |
306 | } |
307 | ||
308 | if (is_play) { | |
37523034 KM |
309 | *mod_from = mod[index - 1]; |
310 | *mod_to = mod[index]; | |
199e7688 | 311 | } else { |
37523034 KM |
312 | *mod_from = mod[index]; |
313 | *mod_to = mod[index - 1]; | |
199e7688 | 314 | } |
199e7688 KM |
315 | } |
316 | ||
0a4d94c0 | 317 | int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma, |
4686a0ad | 318 | int is_play, int id) |
0a4d94c0 KM |
319 | { |
320 | struct device *dev = rsnd_priv_to_dev(priv); | |
9ade09d6 | 321 | struct dma_slave_config cfg; |
37523034 KM |
322 | struct rsnd_mod *mod_from; |
323 | struct rsnd_mod *mod_to; | |
199e7688 | 324 | char dma_name[DMA_NAME_SIZE]; |
0a4d94c0 | 325 | dma_cap_mask_t mask; |
9ade09d6 | 326 | int ret; |
0a4d94c0 KM |
327 | |
328 | if (dma->chan) { | |
329 | dev_err(dev, "it already has dma channel\n"); | |
330 | return -EIO; | |
331 | } | |
332 | ||
333 | dma_cap_zero(mask); | |
334 | dma_cap_set(DMA_SLAVE, mask); | |
335 | ||
37523034 KM |
336 | rsnd_dma_of_path(dma, is_play, &mod_from, &mod_to); |
337 | rsnd_dma_of_name(mod_from, mod_to, dma_name); | |
338 | ||
339 | cfg.slave_id = id; | |
340 | cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM; | |
341 | cfg.src_addr = rsnd_gen_dma_addr(priv, mod_from, is_play, 1); | |
342 | cfg.dst_addr = rsnd_gen_dma_addr(priv, mod_to, is_play, 0); | |
2bf865ba KM |
343 | cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
344 | cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; | |
199e7688 | 345 | |
37523034 KM |
346 | dev_dbg(dev, "dma : %s %pad -> %pad\n", |
347 | dma_name, &cfg.src_addr, &cfg.dst_addr); | |
199e7688 | 348 | |
9ade09d6 KM |
349 | dma->chan = dma_request_slave_channel_compat(mask, shdma_chan_filter, |
350 | (void *)id, dev, | |
199e7688 | 351 | dma_name); |
0a4d94c0 KM |
352 | if (!dma->chan) { |
353 | dev_err(dev, "can't get dma channel\n"); | |
d3a76823 | 354 | goto rsnd_dma_channel_err; |
0a4d94c0 KM |
355 | } |
356 | ||
9ade09d6 KM |
357 | ret = dmaengine_slave_config(dma->chan, &cfg); |
358 | if (ret < 0) | |
359 | goto rsnd_dma_init_err; | |
360 | ||
34037100 | 361 | dma->addr = is_play ? cfg.src_addr : cfg.dst_addr; |
cd7bcc60 | 362 | dma->dir = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM; |
0a4d94c0 KM |
363 | |
364 | return 0; | |
9ade09d6 KM |
365 | |
366 | rsnd_dma_init_err: | |
367 | rsnd_dma_quit(priv, dma); | |
d3a76823 | 368 | rsnd_dma_channel_err: |
9ade09d6 | 369 | |
d3a76823 KM |
370 | /* |
371 | * DMA failed. try to PIO mode | |
372 | * see | |
97463e19 | 373 | * rsnd_ssi_fallback() |
d3a76823 KM |
374 | * rsnd_rdai_continuance_probe() |
375 | */ | |
376 | return -EAGAIN; | |
0a4d94c0 KM |
377 | } |
378 | ||
379 | void rsnd_dma_quit(struct rsnd_priv *priv, | |
380 | struct rsnd_dma *dma) | |
381 | { | |
382 | if (dma->chan) | |
383 | dma_release_channel(dma->chan); | |
384 | ||
385 | dma->chan = NULL; | |
386 | } | |
387 | ||
d7bdbc5d KM |
388 | /* |
389 | * settting function | |
390 | */ | |
391 | u32 rsnd_get_adinr(struct rsnd_mod *mod) | |
392 | { | |
393 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); | |
394 | struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); | |
395 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); | |
396 | struct device *dev = rsnd_priv_to_dev(priv); | |
397 | u32 adinr = runtime->channels; | |
398 | ||
399 | switch (runtime->sample_bits) { | |
400 | case 16: | |
401 | adinr |= (8 << 16); | |
402 | break; | |
403 | case 32: | |
404 | adinr |= (0 << 16); | |
405 | break; | |
406 | default: | |
407 | dev_warn(dev, "not supported sample bits\n"); | |
408 | return 0; | |
409 | } | |
410 | ||
411 | return adinr; | |
412 | } | |
413 | ||
1536a968 KM |
414 | /* |
415 | * rsnd_dai functions | |
416 | */ | |
690602fc | 417 | #define __rsnd_mod_call(mod, func, param...) \ |
d870a91e KM |
418 | ({ \ |
419 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \ | |
420 | struct device *dev = rsnd_priv_to_dev(priv); \ | |
417f9642 KM |
421 | u32 mask = 1 << __rsnd_mod_shift_##func; \ |
422 | u32 call = __rsnd_mod_call_##func << __rsnd_mod_shift_##func; \ | |
423 | int ret = 0; \ | |
424 | if ((mod->status & mask) == call) { \ | |
425 | dev_dbg(dev, "%s[%d] %s\n", \ | |
426 | rsnd_mod_name(mod), rsnd_mod_id(mod), #func); \ | |
690602fc | 427 | ret = (mod)->ops->func(mod, param); \ |
417f9642 KM |
428 | mod->status = (mod->status & ~mask) | (~call & mask); \ |
429 | } \ | |
430 | ret; \ | |
d870a91e KM |
431 | }) |
432 | ||
690602fc | 433 | #define rsnd_mod_call(mod, func, param...) \ |
d870a91e KM |
434 | (!(mod) ? -ENODEV : \ |
435 | !((mod)->ops->func) ? 0 : \ | |
690602fc | 436 | __rsnd_mod_call(mod, func, param)) |
d870a91e | 437 | |
690602fc | 438 | #define rsnd_dai_call(fn, io, param...) \ |
d870a91e | 439 | ({ \ |
a126021d KM |
440 | struct rsnd_mod *mod; \ |
441 | int ret = 0, i; \ | |
442 | for (i = 0; i < RSND_MOD_MAX; i++) { \ | |
443 | mod = (io)->mod[i]; \ | |
444 | if (!mod) \ | |
445 | continue; \ | |
690602fc | 446 | ret = rsnd_mod_call(mod, fn, param); \ |
d870a91e KM |
447 | if (ret < 0) \ |
448 | break; \ | |
449 | } \ | |
450 | ret; \ | |
cdaa3cdf KM |
451 | }) |
452 | ||
a126021d | 453 | static int rsnd_dai_connect(struct rsnd_mod *mod, |
9bfed6cf | 454 | struct rsnd_dai_stream *io) |
cdaa3cdf | 455 | { |
6020779b | 456 | if (!mod) |
cdaa3cdf | 457 | return -EIO; |
cdaa3cdf | 458 | |
a126021d | 459 | if (io->mod[mod->type]) { |
6020779b KM |
460 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
461 | struct device *dev = rsnd_priv_to_dev(priv); | |
462 | ||
cdaa3cdf KM |
463 | dev_err(dev, "%s%d is not empty\n", |
464 | rsnd_mod_name(mod), | |
465 | rsnd_mod_id(mod)); | |
466 | return -EIO; | |
467 | } | |
468 | ||
a126021d | 469 | io->mod[mod->type] = mod; |
4686a0ad | 470 | mod->io = io; |
cdaa3cdf KM |
471 | |
472 | return 0; | |
473 | } | |
474 | ||
d3a76823 KM |
475 | static void rsnd_dai_disconnect(struct rsnd_mod *mod, |
476 | struct rsnd_dai_stream *io) | |
477 | { | |
478 | mod->io = NULL; | |
479 | io->mod[mod->type] = NULL; | |
480 | } | |
481 | ||
710d0889 | 482 | struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id) |
1536a968 | 483 | { |
ecba9e72 | 484 | if ((id < 0) || (id >= rsnd_rdai_nr(priv))) |
2192f81c KM |
485 | return NULL; |
486 | ||
1536a968 KM |
487 | return priv->rdai + id; |
488 | } | |
489 | ||
490 | static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai) | |
491 | { | |
492 | struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai); | |
493 | ||
710d0889 | 494 | return rsnd_rdai_get(priv, dai->id); |
1536a968 KM |
495 | } |
496 | ||
1536a968 KM |
497 | /* |
498 | * rsnd_soc_dai functions | |
499 | */ | |
500 | int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional) | |
501 | { | |
502 | struct snd_pcm_substream *substream = io->substream; | |
503 | struct snd_pcm_runtime *runtime = substream->runtime; | |
504 | int pos = io->byte_pos + additional; | |
505 | ||
506 | pos %= (runtime->periods * io->byte_per_period); | |
507 | ||
508 | return pos; | |
509 | } | |
510 | ||
511 | void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte) | |
512 | { | |
513 | io->byte_pos += byte; | |
514 | ||
515 | if (io->byte_pos >= io->next_period_byte) { | |
516 | struct snd_pcm_substream *substream = io->substream; | |
517 | struct snd_pcm_runtime *runtime = substream->runtime; | |
518 | ||
519 | io->period_pos++; | |
520 | io->next_period_byte += io->byte_per_period; | |
521 | ||
522 | if (io->period_pos >= runtime->periods) { | |
523 | io->byte_pos = 0; | |
524 | io->period_pos = 0; | |
525 | io->next_period_byte = io->byte_per_period; | |
526 | } | |
527 | ||
528 | snd_pcm_period_elapsed(substream); | |
529 | } | |
530 | } | |
531 | ||
532 | static int rsnd_dai_stream_init(struct rsnd_dai_stream *io, | |
533 | struct snd_pcm_substream *substream) | |
534 | { | |
535 | struct snd_pcm_runtime *runtime = substream->runtime; | |
536 | ||
1536a968 KM |
537 | io->substream = substream; |
538 | io->byte_pos = 0; | |
539 | io->period_pos = 0; | |
540 | io->byte_per_period = runtime->period_size * | |
541 | runtime->channels * | |
542 | samples_to_bytes(runtime, 1); | |
543 | io->next_period_byte = io->byte_per_period; | |
544 | ||
545 | return 0; | |
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 | { | |
569 | struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai); | |
570 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); | |
571 | struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); | |
29e69fd2 | 572 | int ssi_id = rsnd_mod_id(rsnd_io_to_mod_ssi(io)); |
1536a968 KM |
573 | int ret; |
574 | unsigned long flags; | |
575 | ||
576 | rsnd_lock(priv, flags); | |
577 | ||
578 | switch (cmd) { | |
579 | case SNDRV_PCM_TRIGGER_START: | |
580 | ret = rsnd_dai_stream_init(io, substream); | |
581 | if (ret < 0) | |
582 | goto dai_trigger_end; | |
583 | ||
584 | ret = rsnd_platform_call(priv, dai, start, ssi_id); | |
585 | if (ret < 0) | |
586 | goto dai_trigger_end; | |
587 | ||
690602fc | 588 | ret = rsnd_dai_call(init, io, priv); |
cdaa3cdf KM |
589 | if (ret < 0) |
590 | goto dai_trigger_end; | |
591 | ||
690602fc | 592 | ret = rsnd_dai_call(start, io, priv); |
cdaa3cdf KM |
593 | if (ret < 0) |
594 | goto dai_trigger_end; | |
1536a968 KM |
595 | break; |
596 | case SNDRV_PCM_TRIGGER_STOP: | |
690602fc | 597 | ret = rsnd_dai_call(stop, io, priv); |
cdaa3cdf KM |
598 | if (ret < 0) |
599 | goto dai_trigger_end; | |
600 | ||
690602fc | 601 | ret = rsnd_dai_call(quit, io, priv); |
cdaa3cdf KM |
602 | if (ret < 0) |
603 | goto dai_trigger_end; | |
604 | ||
3337744a KM |
605 | ret = rsnd_platform_call(priv, dai, stop, ssi_id); |
606 | if (ret < 0) | |
607 | goto dai_trigger_end; | |
1536a968 KM |
608 | break; |
609 | default: | |
610 | ret = -EINVAL; | |
611 | } | |
612 | ||
613 | dai_trigger_end: | |
614 | rsnd_unlock(priv, flags); | |
615 | ||
616 | return ret; | |
617 | } | |
618 | ||
619 | static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) | |
620 | { | |
621 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); | |
622 | ||
623 | /* set master/slave audio interface */ | |
624 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { | |
625 | case SND_SOC_DAIFMT_CBM_CFM: | |
e1508289 | 626 | rdai->clk_master = 0; |
1536a968 KM |
627 | break; |
628 | case SND_SOC_DAIFMT_CBS_CFS: | |
e1508289 | 629 | rdai->clk_master = 1; /* codec is slave, cpu is master */ |
1536a968 KM |
630 | break; |
631 | default: | |
632 | return -EINVAL; | |
633 | } | |
634 | ||
1536a968 KM |
635 | /* set format */ |
636 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { | |
637 | case SND_SOC_DAIFMT_I2S: | |
638 | rdai->sys_delay = 0; | |
639 | rdai->data_alignment = 0; | |
1a7889ca | 640 | rdai->frm_clk_inv = 0; |
1536a968 KM |
641 | break; |
642 | case SND_SOC_DAIFMT_LEFT_J: | |
643 | rdai->sys_delay = 1; | |
644 | rdai->data_alignment = 0; | |
1a7889ca | 645 | rdai->frm_clk_inv = 1; |
1536a968 KM |
646 | break; |
647 | case SND_SOC_DAIFMT_RIGHT_J: | |
648 | rdai->sys_delay = 1; | |
649 | rdai->data_alignment = 1; | |
1a7889ca KM |
650 | rdai->frm_clk_inv = 1; |
651 | break; | |
652 | } | |
653 | ||
654 | /* set clock inversion */ | |
655 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { | |
656 | case SND_SOC_DAIFMT_NB_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_IB_NF: | |
661 | rdai->bit_clk_inv = !rdai->bit_clk_inv; | |
662 | rdai->frm_clk_inv = rdai->frm_clk_inv; | |
663 | break; | |
664 | case SND_SOC_DAIFMT_IB_IF: | |
665 | rdai->bit_clk_inv = !rdai->bit_clk_inv; | |
666 | rdai->frm_clk_inv = !rdai->frm_clk_inv; | |
667 | break; | |
668 | case SND_SOC_DAIFMT_NB_NF: | |
669 | default: | |
1536a968 KM |
670 | break; |
671 | } | |
672 | ||
673 | return 0; | |
674 | } | |
675 | ||
676 | static const struct snd_soc_dai_ops rsnd_soc_dai_ops = { | |
677 | .trigger = rsnd_soc_dai_trigger, | |
678 | .set_fmt = rsnd_soc_dai_set_fmt, | |
679 | }; | |
680 | ||
739f9502 KM |
681 | #define rsnd_path_parse(priv, io, type) \ |
682 | ({ \ | |
683 | struct rsnd_mod *mod; \ | |
684 | int ret = 0; \ | |
685 | int id = -1; \ | |
686 | \ | |
687 | if (rsnd_is_enable_path(io, type)) { \ | |
688 | id = rsnd_info_id(priv, io, type); \ | |
689 | if (id >= 0) { \ | |
690 | mod = rsnd_##type##_mod_get(priv, id); \ | |
691 | ret = rsnd_dai_connect(mod, io); \ | |
692 | } \ | |
693 | } \ | |
694 | ret; \ | |
695 | }) | |
696 | ||
d3a76823 KM |
697 | #define rsnd_path_break(priv, io, type) \ |
698 | { \ | |
699 | struct rsnd_mod *mod; \ | |
700 | int id = -1; \ | |
701 | \ | |
702 | if (rsnd_is_enable_path(io, type)) { \ | |
703 | id = rsnd_info_id(priv, io, type); \ | |
704 | if (id >= 0) { \ | |
705 | mod = rsnd_##type##_mod_get(priv, id); \ | |
706 | rsnd_dai_disconnect(mod, io); \ | |
707 | } \ | |
708 | } \ | |
709 | } | |
710 | ||
9bfed6cf KM |
711 | static int rsnd_path_init(struct rsnd_priv *priv, |
712 | struct rsnd_dai *rdai, | |
713 | struct rsnd_dai_stream *io) | |
714 | { | |
9bfed6cf | 715 | int ret; |
9bfed6cf KM |
716 | |
717 | /* | |
718 | * Gen1 is created by SRU/SSI, and this SRU is base module of | |
719 | * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU) | |
720 | * | |
721 | * Easy image is.. | |
722 | * Gen1 SRU = Gen2 SCU + SSIU + etc | |
723 | * | |
724 | * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is | |
725 | * using fixed path. | |
9bfed6cf | 726 | */ |
9bfed6cf | 727 | |
ba9c949f | 728 | /* SRC */ |
739f9502 KM |
729 | ret = rsnd_path_parse(priv, io, src); |
730 | if (ret < 0) | |
731 | return ret; | |
9bfed6cf KM |
732 | |
733 | /* SSI */ | |
739f9502 KM |
734 | ret = rsnd_path_parse(priv, io, ssi); |
735 | if (ret < 0) | |
736 | return ret; | |
9bfed6cf | 737 | |
bff58ea4 KM |
738 | /* DVC */ |
739 | ret = rsnd_path_parse(priv, io, dvc); | |
740 | if (ret < 0) | |
741 | return ret; | |
9bfed6cf KM |
742 | |
743 | return ret; | |
744 | } | |
745 | ||
90e8e50f KM |
746 | static void rsnd_of_parse_dai(struct platform_device *pdev, |
747 | const struct rsnd_of_data *of_data, | |
748 | struct rsnd_priv *priv) | |
749 | { | |
750 | struct device_node *dai_node, *dai_np; | |
751 | struct device_node *ssi_node, *ssi_np; | |
752 | struct device_node *src_node, *src_np; | |
34cb6123 | 753 | struct device_node *dvc_node, *dvc_np; |
90e8e50f KM |
754 | struct device_node *playback, *capture; |
755 | struct rsnd_dai_platform_info *dai_info; | |
756 | struct rcar_snd_info *info = rsnd_priv_to_info(priv); | |
757 | struct device *dev = &pdev->dev; | |
758 | int nr, i; | |
34cb6123 | 759 | int dai_i, ssi_i, src_i, dvc_i; |
90e8e50f KM |
760 | |
761 | if (!of_data) | |
762 | return; | |
763 | ||
764 | dai_node = of_get_child_by_name(dev->of_node, "rcar_sound,dai"); | |
765 | if (!dai_node) | |
766 | return; | |
767 | ||
768 | nr = of_get_child_count(dai_node); | |
769 | if (!nr) | |
770 | return; | |
771 | ||
772 | dai_info = devm_kzalloc(dev, | |
773 | sizeof(struct rsnd_dai_platform_info) * nr, | |
774 | GFP_KERNEL); | |
775 | if (!dai_info) { | |
776 | dev_err(dev, "dai info allocation error\n"); | |
777 | return; | |
778 | } | |
779 | ||
780 | info->dai_info_nr = nr; | |
781 | info->dai_info = dai_info; | |
782 | ||
783 | ssi_node = of_get_child_by_name(dev->of_node, "rcar_sound,ssi"); | |
784 | src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src"); | |
34cb6123 | 785 | dvc_node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc"); |
90e8e50f KM |
786 | |
787 | #define mod_parse(name) \ | |
788 | if (name##_node) { \ | |
789 | struct rsnd_##name##_platform_info *name##_info; \ | |
790 | \ | |
791 | name##_i = 0; \ | |
792 | for_each_child_of_node(name##_node, name##_np) { \ | |
793 | name##_info = info->name##_info + name##_i; \ | |
794 | \ | |
795 | if (name##_np == playback) \ | |
796 | dai_info->playback.name = name##_info; \ | |
797 | if (name##_np == capture) \ | |
798 | dai_info->capture.name = name##_info; \ | |
799 | \ | |
800 | name##_i++; \ | |
801 | } \ | |
802 | } | |
803 | ||
804 | /* | |
805 | * parse all dai | |
806 | */ | |
807 | dai_i = 0; | |
808 | for_each_child_of_node(dai_node, dai_np) { | |
809 | dai_info = info->dai_info + dai_i; | |
810 | ||
811 | for (i = 0;; i++) { | |
812 | ||
813 | playback = of_parse_phandle(dai_np, "playback", i); | |
814 | capture = of_parse_phandle(dai_np, "capture", i); | |
815 | ||
816 | if (!playback && !capture) | |
817 | break; | |
818 | ||
819 | mod_parse(ssi); | |
820 | mod_parse(src); | |
34cb6123 | 821 | mod_parse(dvc); |
90e8e50f | 822 | |
a493b6a6 JL |
823 | of_node_put(playback); |
824 | of_node_put(capture); | |
90e8e50f KM |
825 | } |
826 | ||
827 | dai_i++; | |
828 | } | |
829 | } | |
830 | ||
1536a968 | 831 | static int rsnd_dai_probe(struct platform_device *pdev, |
90e8e50f | 832 | const struct rsnd_of_data *of_data, |
1536a968 KM |
833 | struct rsnd_priv *priv) |
834 | { | |
835 | struct snd_soc_dai_driver *drv; | |
78f13d0c | 836 | struct rcar_snd_info *info = rsnd_priv_to_info(priv); |
1536a968 | 837 | struct rsnd_dai *rdai; |
29e69fd2 | 838 | struct rsnd_ssi_platform_info *pmod, *cmod; |
1536a968 | 839 | struct device *dev = rsnd_priv_to_dev(priv); |
90e8e50f | 840 | int dai_nr; |
4b4dab82 KM |
841 | int i; |
842 | ||
90e8e50f KM |
843 | rsnd_of_parse_dai(pdev, of_data, priv); |
844 | ||
90e8e50f | 845 | dai_nr = info->dai_info_nr; |
4b4dab82 KM |
846 | if (!dai_nr) { |
847 | dev_err(dev, "no dai\n"); | |
848 | return -EIO; | |
849 | } | |
1536a968 KM |
850 | |
851 | drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL); | |
852 | rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL); | |
853 | if (!drv || !rdai) { | |
854 | dev_err(dev, "dai allocate failed\n"); | |
855 | return -ENOMEM; | |
856 | } | |
857 | ||
ecba9e72 | 858 | priv->rdai_nr = dai_nr; |
49848073 KM |
859 | priv->daidrv = drv; |
860 | priv->rdai = rdai; | |
861 | ||
1536a968 | 862 | for (i = 0; i < dai_nr; i++) { |
1536a968 | 863 | |
7c57d76f KM |
864 | pmod = info->dai_info[i].playback.ssi; |
865 | cmod = info->dai_info[i].capture.ssi; | |
1536a968 KM |
866 | |
867 | /* | |
868 | * init rsnd_dai | |
869 | */ | |
1536a968 KM |
870 | snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i); |
871 | ||
872 | /* | |
873 | * init snd_soc_dai_driver | |
874 | */ | |
875 | drv[i].name = rdai[i].name; | |
876 | drv[i].ops = &rsnd_soc_dai_ops; | |
4b4dab82 | 877 | if (pmod) { |
1536a968 KM |
878 | drv[i].playback.rates = RSND_RATES; |
879 | drv[i].playback.formats = RSND_FMTS; | |
880 | drv[i].playback.channels_min = 2; | |
881 | drv[i].playback.channels_max = 2; | |
389933d9 | 882 | |
29e69fd2 | 883 | rdai[i].playback.info = &info->dai_info[i].playback; |
54cb5562 | 884 | rdai[i].playback.rdai = rdai + i; |
9bfed6cf | 885 | rsnd_path_init(priv, &rdai[i], &rdai[i].playback); |
1536a968 | 886 | } |
4b4dab82 | 887 | if (cmod) { |
1536a968 KM |
888 | drv[i].capture.rates = RSND_RATES; |
889 | drv[i].capture.formats = RSND_FMTS; | |
890 | drv[i].capture.channels_min = 2; | |
891 | drv[i].capture.channels_max = 2; | |
389933d9 | 892 | |
29e69fd2 | 893 | rdai[i].capture.info = &info->dai_info[i].capture; |
54cb5562 | 894 | rdai[i].capture.rdai = rdai + i; |
9bfed6cf | 895 | rsnd_path_init(priv, &rdai[i], &rdai[i].capture); |
1536a968 KM |
896 | } |
897 | ||
4b4dab82 KM |
898 | dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name, |
899 | pmod ? "play" : " -- ", | |
900 | cmod ? "capture" : " -- "); | |
1536a968 KM |
901 | } |
902 | ||
1536a968 KM |
903 | return 0; |
904 | } | |
905 | ||
1536a968 KM |
906 | /* |
907 | * pcm ops | |
908 | */ | |
909 | static struct snd_pcm_hardware rsnd_pcm_hardware = { | |
910 | .info = SNDRV_PCM_INFO_INTERLEAVED | | |
911 | SNDRV_PCM_INFO_MMAP | | |
706c6621 | 912 | SNDRV_PCM_INFO_MMAP_VALID, |
1536a968 KM |
913 | .buffer_bytes_max = 64 * 1024, |
914 | .period_bytes_min = 32, | |
915 | .period_bytes_max = 8192, | |
916 | .periods_min = 1, | |
917 | .periods_max = 32, | |
918 | .fifo_size = 256, | |
919 | }; | |
920 | ||
921 | static int rsnd_pcm_open(struct snd_pcm_substream *substream) | |
922 | { | |
923 | struct snd_pcm_runtime *runtime = substream->runtime; | |
924 | int ret = 0; | |
925 | ||
926 | snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware); | |
927 | ||
928 | ret = snd_pcm_hw_constraint_integer(runtime, | |
929 | SNDRV_PCM_HW_PARAM_PERIODS); | |
930 | ||
931 | return ret; | |
932 | } | |
933 | ||
934 | static int rsnd_hw_params(struct snd_pcm_substream *substream, | |
935 | struct snd_pcm_hw_params *hw_params) | |
936 | { | |
937 | return snd_pcm_lib_malloc_pages(substream, | |
938 | params_buffer_bytes(hw_params)); | |
939 | } | |
940 | ||
941 | static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream) | |
942 | { | |
943 | struct snd_pcm_runtime *runtime = substream->runtime; | |
944 | struct snd_soc_dai *dai = rsnd_substream_to_dai(substream); | |
945 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); | |
946 | struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); | |
947 | ||
948 | return bytes_to_frames(runtime, io->byte_pos); | |
949 | } | |
950 | ||
951 | static struct snd_pcm_ops rsnd_pcm_ops = { | |
952 | .open = rsnd_pcm_open, | |
953 | .ioctl = snd_pcm_lib_ioctl, | |
954 | .hw_params = rsnd_hw_params, | |
955 | .hw_free = snd_pcm_lib_free_pages, | |
956 | .pointer = rsnd_pointer, | |
957 | }; | |
958 | ||
170a2497 KM |
959 | /* |
960 | * snd_kcontrol | |
961 | */ | |
962 | #define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value) | |
963 | static int rsnd_kctrl_info(struct snd_kcontrol *kctrl, | |
964 | struct snd_ctl_elem_info *uinfo) | |
965 | { | |
966 | struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl); | |
967 | ||
968 | if (cfg->texts) { | |
969 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | |
970 | uinfo->count = cfg->size; | |
971 | uinfo->value.enumerated.items = cfg->max; | |
972 | if (uinfo->value.enumerated.item >= cfg->max) | |
973 | uinfo->value.enumerated.item = cfg->max - 1; | |
974 | strlcpy(uinfo->value.enumerated.name, | |
975 | cfg->texts[uinfo->value.enumerated.item], | |
976 | sizeof(uinfo->value.enumerated.name)); | |
977 | } else { | |
978 | uinfo->count = cfg->size; | |
979 | uinfo->value.integer.min = 0; | |
980 | uinfo->value.integer.max = cfg->max; | |
981 | uinfo->type = (cfg->max == 1) ? | |
982 | SNDRV_CTL_ELEM_TYPE_BOOLEAN : | |
983 | SNDRV_CTL_ELEM_TYPE_INTEGER; | |
984 | } | |
985 | ||
986 | return 0; | |
987 | } | |
988 | ||
989 | static int rsnd_kctrl_get(struct snd_kcontrol *kctrl, | |
990 | struct snd_ctl_elem_value *uc) | |
991 | { | |
992 | struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl); | |
993 | int i; | |
994 | ||
995 | for (i = 0; i < cfg->size; i++) | |
996 | if (cfg->texts) | |
997 | uc->value.enumerated.item[i] = cfg->val[i]; | |
998 | else | |
999 | uc->value.integer.value[i] = cfg->val[i]; | |
1000 | ||
1001 | return 0; | |
1002 | } | |
1003 | ||
1004 | static int rsnd_kctrl_put(struct snd_kcontrol *kctrl, | |
1005 | struct snd_ctl_elem_value *uc) | |
1006 | { | |
1007 | struct rsnd_mod *mod = snd_kcontrol_chip(kctrl); | |
1008 | struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl); | |
1009 | int i, change = 0; | |
1010 | ||
1011 | for (i = 0; i < cfg->size; i++) { | |
1012 | if (cfg->texts) { | |
1013 | change |= (uc->value.enumerated.item[i] != cfg->val[i]); | |
1014 | cfg->val[i] = uc->value.enumerated.item[i]; | |
1015 | } else { | |
1016 | change |= (uc->value.integer.value[i] != cfg->val[i]); | |
1017 | cfg->val[i] = uc->value.integer.value[i]; | |
1018 | } | |
1019 | } | |
1020 | ||
1021 | if (change) | |
1022 | cfg->update(mod); | |
1023 | ||
1024 | return change; | |
1025 | } | |
1026 | ||
1027 | static int __rsnd_kctrl_new(struct rsnd_mod *mod, | |
170a2497 KM |
1028 | struct snd_soc_pcm_runtime *rtd, |
1029 | const unsigned char *name, | |
1030 | struct rsnd_kctrl_cfg *cfg, | |
1031 | void (*update)(struct rsnd_mod *mod)) | |
1032 | { | |
1033 | struct snd_card *card = rtd->card->snd_card; | |
1034 | struct snd_kcontrol *kctrl; | |
1035 | struct snd_kcontrol_new knew = { | |
1036 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1037 | .name = name, | |
1038 | .info = rsnd_kctrl_info, | |
1039 | .get = rsnd_kctrl_get, | |
1040 | .put = rsnd_kctrl_put, | |
1041 | .private_value = (unsigned long)cfg, | |
1042 | }; | |
1043 | int ret; | |
1044 | ||
1045 | kctrl = snd_ctl_new1(&knew, mod); | |
1046 | if (!kctrl) | |
1047 | return -ENOMEM; | |
1048 | ||
1049 | ret = snd_ctl_add(card, kctrl); | |
1050 | if (ret < 0) | |
1051 | return ret; | |
1052 | ||
1053 | cfg->update = update; | |
1054 | ||
1055 | return 0; | |
1056 | } | |
1057 | ||
1058 | int rsnd_kctrl_new_m(struct rsnd_mod *mod, | |
170a2497 KM |
1059 | struct snd_soc_pcm_runtime *rtd, |
1060 | const unsigned char *name, | |
1061 | void (*update)(struct rsnd_mod *mod), | |
1062 | struct rsnd_kctrl_cfg_m *_cfg, | |
1063 | u32 max) | |
1064 | { | |
1065 | _cfg->cfg.max = max; | |
1066 | _cfg->cfg.size = RSND_DVC_CHANNELS; | |
1067 | _cfg->cfg.val = _cfg->val; | |
f708d944 | 1068 | return __rsnd_kctrl_new(mod, rtd, name, &_cfg->cfg, update); |
170a2497 KM |
1069 | } |
1070 | ||
1071 | int rsnd_kctrl_new_s(struct rsnd_mod *mod, | |
170a2497 KM |
1072 | struct snd_soc_pcm_runtime *rtd, |
1073 | const unsigned char *name, | |
1074 | void (*update)(struct rsnd_mod *mod), | |
1075 | struct rsnd_kctrl_cfg_s *_cfg, | |
1076 | u32 max) | |
1077 | { | |
1078 | _cfg->cfg.max = max; | |
1079 | _cfg->cfg.size = 1; | |
1080 | _cfg->cfg.val = &_cfg->val; | |
f708d944 | 1081 | return __rsnd_kctrl_new(mod, rtd, name, &_cfg->cfg, update); |
170a2497 KM |
1082 | } |
1083 | ||
1084 | int rsnd_kctrl_new_e(struct rsnd_mod *mod, | |
170a2497 KM |
1085 | struct snd_soc_pcm_runtime *rtd, |
1086 | const unsigned char *name, | |
1087 | struct rsnd_kctrl_cfg_s *_cfg, | |
1088 | void (*update)(struct rsnd_mod *mod), | |
1089 | const char * const *texts, | |
1090 | u32 max) | |
1091 | { | |
1092 | _cfg->cfg.max = max; | |
1093 | _cfg->cfg.size = 1; | |
1094 | _cfg->cfg.val = &_cfg->val; | |
1095 | _cfg->cfg.texts = texts; | |
f708d944 | 1096 | return __rsnd_kctrl_new(mod, rtd, name, &_cfg->cfg, update); |
170a2497 KM |
1097 | } |
1098 | ||
1536a968 KM |
1099 | /* |
1100 | * snd_soc_platform | |
1101 | */ | |
1102 | ||
1103 | #define PREALLOC_BUFFER (32 * 1024) | |
1104 | #define PREALLOC_BUFFER_MAX (32 * 1024) | |
1105 | ||
1106 | static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd) | |
1107 | { | |
7c63f3c0 KM |
1108 | struct snd_soc_dai *dai = rtd->cpu_dai; |
1109 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); | |
1110 | int ret; | |
bff58ea4 | 1111 | |
690602fc | 1112 | ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd); |
7c63f3c0 KM |
1113 | if (ret) |
1114 | return ret; | |
bff58ea4 | 1115 | |
690602fc | 1116 | ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd); |
7c63f3c0 KM |
1117 | if (ret) |
1118 | return ret; | |
bff58ea4 | 1119 | |
1536a968 KM |
1120 | return snd_pcm_lib_preallocate_pages_for_all( |
1121 | rtd->pcm, | |
1122 | SNDRV_DMA_TYPE_DEV, | |
1123 | rtd->card->snd_card->dev, | |
1124 | PREALLOC_BUFFER, PREALLOC_BUFFER_MAX); | |
1125 | } | |
1126 | ||
1536a968 KM |
1127 | static struct snd_soc_platform_driver rsnd_soc_platform = { |
1128 | .ops = &rsnd_pcm_ops, | |
1129 | .pcm_new = rsnd_pcm_new, | |
1536a968 KM |
1130 | }; |
1131 | ||
1132 | static const struct snd_soc_component_driver rsnd_soc_component = { | |
1133 | .name = "rsnd", | |
1134 | }; | |
1135 | ||
d3a76823 | 1136 | static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv, |
f708d944 | 1137 | struct rsnd_dai_stream *io) |
d3a76823 | 1138 | { |
d3a76823 KM |
1139 | int ret; |
1140 | ||
690602fc | 1141 | ret = rsnd_dai_call(probe, io, priv); |
d3a76823 KM |
1142 | if (ret == -EAGAIN) { |
1143 | /* | |
1144 | * Fallback to PIO mode | |
1145 | */ | |
1146 | ||
1147 | /* | |
1148 | * call "remove" for SSI/SRC/DVC | |
1149 | * SSI will be switch to PIO mode if it was DMA mode | |
1150 | * see | |
1151 | * rsnd_dma_init() | |
97463e19 | 1152 | * rsnd_ssi_fallback() |
d3a76823 | 1153 | */ |
690602fc | 1154 | rsnd_dai_call(remove, io, priv); |
d3a76823 KM |
1155 | |
1156 | /* | |
1157 | * remove SRC/DVC from DAI, | |
1158 | */ | |
1159 | rsnd_path_break(priv, io, src); | |
1160 | rsnd_path_break(priv, io, dvc); | |
1161 | ||
97463e19 KM |
1162 | /* |
1163 | * fallback | |
1164 | */ | |
690602fc | 1165 | rsnd_dai_call(fallback, io, priv); |
97463e19 | 1166 | |
d3a76823 KM |
1167 | /* |
1168 | * retry to "probe". | |
1169 | * DAI has SSI which is PIO mode only now. | |
1170 | */ | |
690602fc | 1171 | ret = rsnd_dai_call(probe, io, priv); |
d3a76823 KM |
1172 | } |
1173 | ||
1174 | return ret; | |
1175 | } | |
1176 | ||
1536a968 KM |
1177 | /* |
1178 | * rsnd probe | |
1179 | */ | |
1180 | static int rsnd_probe(struct platform_device *pdev) | |
1181 | { | |
1182 | struct rcar_snd_info *info; | |
1183 | struct rsnd_priv *priv; | |
1184 | struct device *dev = &pdev->dev; | |
7681f6ac | 1185 | struct rsnd_dai *rdai; |
90e8e50f KM |
1186 | const struct of_device_id *of_id = of_match_device(rsnd_of_match, dev); |
1187 | const struct rsnd_of_data *of_data; | |
d1ac970f | 1188 | int (*probe_func[])(struct platform_device *pdev, |
90e8e50f | 1189 | const struct rsnd_of_data *of_data, |
d1ac970f KM |
1190 | struct rsnd_priv *priv) = { |
1191 | rsnd_gen_probe, | |
1192 | rsnd_ssi_probe, | |
ba9c949f | 1193 | rsnd_src_probe, |
bff58ea4 | 1194 | rsnd_dvc_probe, |
d1ac970f KM |
1195 | rsnd_adg_probe, |
1196 | rsnd_dai_probe, | |
1197 | }; | |
1198 | int ret, i; | |
1536a968 | 1199 | |
90e8e50f KM |
1200 | info = NULL; |
1201 | of_data = NULL; | |
1202 | if (of_id) { | |
1203 | info = devm_kzalloc(&pdev->dev, | |
1204 | sizeof(struct rcar_snd_info), GFP_KERNEL); | |
1205 | of_data = of_id->data; | |
1206 | } else { | |
1207 | info = pdev->dev.platform_data; | |
1208 | } | |
1209 | ||
1536a968 KM |
1210 | if (!info) { |
1211 | dev_err(dev, "driver needs R-Car sound information\n"); | |
1212 | return -ENODEV; | |
1213 | } | |
1214 | ||
1215 | /* | |
1216 | * init priv data | |
1217 | */ | |
1218 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); | |
1219 | if (!priv) { | |
1220 | dev_err(dev, "priv allocate failed\n"); | |
1221 | return -ENODEV; | |
1222 | } | |
1223 | ||
9f464f8e | 1224 | priv->pdev = pdev; |
1536a968 KM |
1225 | priv->info = info; |
1226 | spin_lock_init(&priv->lock); | |
1227 | ||
1228 | /* | |
1229 | * init each module | |
1230 | */ | |
d1ac970f | 1231 | for (i = 0; i < ARRAY_SIZE(probe_func); i++) { |
90e8e50f | 1232 | ret = probe_func[i](pdev, of_data, priv); |
d1ac970f KM |
1233 | if (ret) |
1234 | return ret; | |
1235 | } | |
07539c1d | 1236 | |
7681f6ac | 1237 | for_each_rsnd_dai(rdai, priv, i) { |
f708d944 | 1238 | ret = rsnd_rdai_continuance_probe(priv, &rdai->playback); |
7681f6ac | 1239 | if (ret) |
d62a3dcd | 1240 | goto exit_snd_probe; |
dfc9403b | 1241 | |
f708d944 | 1242 | ret = rsnd_rdai_continuance_probe(priv, &rdai->capture); |
7681f6ac | 1243 | if (ret) |
d62a3dcd | 1244 | goto exit_snd_probe; |
7681f6ac | 1245 | } |
4b4dab82 | 1246 | |
1536a968 KM |
1247 | /* |
1248 | * asoc register | |
1249 | */ | |
1250 | ret = snd_soc_register_platform(dev, &rsnd_soc_platform); | |
1251 | if (ret < 0) { | |
1252 | dev_err(dev, "cannot snd soc register\n"); | |
1253 | return ret; | |
1254 | } | |
1255 | ||
1256 | ret = snd_soc_register_component(dev, &rsnd_soc_component, | |
ecba9e72 | 1257 | priv->daidrv, rsnd_rdai_nr(priv)); |
1536a968 KM |
1258 | if (ret < 0) { |
1259 | dev_err(dev, "cannot snd dai register\n"); | |
1260 | goto exit_snd_soc; | |
1261 | } | |
1262 | ||
1263 | dev_set_drvdata(dev, priv); | |
1264 | ||
1265 | pm_runtime_enable(dev); | |
1266 | ||
1267 | dev_info(dev, "probed\n"); | |
1268 | return ret; | |
1269 | ||
1270 | exit_snd_soc: | |
1271 | snd_soc_unregister_platform(dev); | |
d62a3dcd KM |
1272 | exit_snd_probe: |
1273 | for_each_rsnd_dai(rdai, priv, i) { | |
690602fc KM |
1274 | rsnd_dai_call(remove, &rdai->playback, priv); |
1275 | rsnd_dai_call(remove, &rdai->capture, priv); | |
d62a3dcd | 1276 | } |
1536a968 KM |
1277 | |
1278 | return ret; | |
1279 | } | |
1280 | ||
1281 | static int rsnd_remove(struct platform_device *pdev) | |
1282 | { | |
1283 | struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev); | |
7681f6ac | 1284 | struct rsnd_dai *rdai; |
d62a3dcd | 1285 | int ret = 0, i; |
1536a968 KM |
1286 | |
1287 | pm_runtime_disable(&pdev->dev); | |
1288 | ||
7681f6ac | 1289 | for_each_rsnd_dai(rdai, priv, i) { |
690602fc KM |
1290 | ret |= rsnd_dai_call(remove, &rdai->playback, priv); |
1291 | ret |= rsnd_dai_call(remove, &rdai->capture, priv); | |
7681f6ac | 1292 | } |
1536a968 | 1293 | |
d62a3dcd | 1294 | return ret; |
1536a968 KM |
1295 | } |
1296 | ||
1297 | static struct platform_driver rsnd_driver = { | |
1298 | .driver = { | |
1299 | .name = "rcar_sound", | |
90e8e50f | 1300 | .of_match_table = rsnd_of_match, |
1536a968 KM |
1301 | }, |
1302 | .probe = rsnd_probe, | |
1303 | .remove = rsnd_remove, | |
1304 | }; | |
1305 | module_platform_driver(rsnd_driver); | |
1306 | ||
1307 | MODULE_LICENSE("GPL"); | |
1308 | MODULE_DESCRIPTION("Renesas R-Car audio driver"); | |
1309 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); | |
1310 | MODULE_ALIAS("platform:rcar-pcm-audio"); |