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 | * | | |
76 | * | ** these control scu | |
77 | * | | |
78 | * +- scu | |
79 | * | | |
80 | * +- scu[0] | |
81 | * +- scu[1] | |
82 | * +- scu[2] | |
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 | ||
103 | /* | |
104 | * rsnd_platform functions | |
105 | */ | |
106 | #define rsnd_platform_call(priv, dai, func, param...) \ | |
740ad6c3 | 107 | (!(priv->info->func) ? 0 : \ |
1536a968 KM |
108 | priv->info->func(param)) |
109 | ||
cdaa3cdf KM |
110 | /* |
111 | * rsnd_mod functions | |
112 | */ | |
113 | char *rsnd_mod_name(struct rsnd_mod *mod) | |
114 | { | |
115 | if (!mod || !mod->ops) | |
116 | return "unknown"; | |
117 | ||
118 | return mod->ops->name; | |
119 | } | |
120 | ||
121 | void rsnd_mod_init(struct rsnd_priv *priv, | |
122 | struct rsnd_mod *mod, | |
123 | struct rsnd_mod_ops *ops, | |
124 | int id) | |
125 | { | |
126 | mod->priv = priv; | |
127 | mod->id = id; | |
128 | mod->ops = ops; | |
129 | INIT_LIST_HEAD(&mod->list); | |
130 | } | |
131 | ||
0a4d94c0 KM |
132 | /* |
133 | * rsnd_dma functions | |
134 | */ | |
f5cab3b8 | 135 | static void __rsnd_dma_start(struct rsnd_dma *dma); |
0a4d94c0 KM |
136 | static void rsnd_dma_continue(struct rsnd_dma *dma) |
137 | { | |
138 | /* push next A or B plane */ | |
139 | dma->submit_loop = 1; | |
140 | schedule_work(&dma->work); | |
141 | } | |
142 | ||
143 | void rsnd_dma_start(struct rsnd_dma *dma) | |
144 | { | |
145 | /* push both A and B plane*/ | |
4686a0ad | 146 | dma->offset = 0; |
0a4d94c0 | 147 | dma->submit_loop = 2; |
f5cab3b8 | 148 | __rsnd_dma_start(dma); |
0a4d94c0 KM |
149 | } |
150 | ||
151 | void rsnd_dma_stop(struct rsnd_dma *dma) | |
152 | { | |
153 | dma->submit_loop = 0; | |
154 | cancel_work_sync(&dma->work); | |
155 | dmaengine_terminate_all(dma->chan); | |
156 | } | |
157 | ||
158 | static void rsnd_dma_complete(void *data) | |
159 | { | |
160 | struct rsnd_dma *dma = (struct rsnd_dma *)data; | |
4686a0ad | 161 | struct rsnd_mod *mod = rsnd_dma_to_mod(dma); |
9b5ab573 | 162 | struct rsnd_priv *priv = rsnd_mod_to_priv(rsnd_dma_to_mod(dma)); |
4686a0ad | 163 | struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); |
0a4d94c0 KM |
164 | unsigned long flags; |
165 | ||
166 | rsnd_lock(priv, flags); | |
167 | ||
4686a0ad KM |
168 | /* |
169 | * Renesas sound Gen1 needs 1 DMAC, | |
170 | * Gen2 needs 2 DMAC. | |
171 | * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri. | |
172 | * But, Audio-DMAC-peri-peri doesn't have interrupt, | |
173 | * and this driver is assuming that here. | |
174 | * | |
175 | * If Audio-DMAC-peri-peri has interrpt, | |
176 | * rsnd_dai_pointer_update() will be called twice, | |
177 | * ant it will breaks io->byte_pos | |
178 | */ | |
179 | ||
180 | rsnd_dai_pointer_update(io, io->byte_per_period); | |
0a4d94c0 KM |
181 | |
182 | if (dma->submit_loop) | |
183 | rsnd_dma_continue(dma); | |
184 | ||
185 | rsnd_unlock(priv, flags); | |
186 | } | |
187 | ||
f5cab3b8 | 188 | static void __rsnd_dma_start(struct rsnd_dma *dma) |
0a4d94c0 | 189 | { |
4686a0ad KM |
190 | struct rsnd_mod *mod = rsnd_dma_to_mod(dma); |
191 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); | |
192 | struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); | |
193 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); | |
0a4d94c0 KM |
194 | struct device *dev = rsnd_priv_to_dev(priv); |
195 | struct dma_async_tx_descriptor *desc; | |
196 | dma_addr_t buf; | |
4686a0ad | 197 | size_t len = io->byte_per_period; |
0a4d94c0 KM |
198 | int i; |
199 | ||
200 | for (i = 0; i < dma->submit_loop; i++) { | |
201 | ||
4686a0ad KM |
202 | buf = runtime->dma_addr + |
203 | rsnd_dai_pointer_offset(io, dma->offset + len); | |
204 | dma->offset = len; | |
0a4d94c0 KM |
205 | |
206 | desc = dmaengine_prep_slave_single( | |
207 | dma->chan, buf, len, dma->dir, | |
208 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); | |
209 | if (!desc) { | |
210 | dev_err(dev, "dmaengine_prep_slave_sg() fail\n"); | |
211 | return; | |
212 | } | |
213 | ||
214 | desc->callback = rsnd_dma_complete; | |
215 | desc->callback_param = dma; | |
216 | ||
217 | if (dmaengine_submit(desc) < 0) { | |
218 | dev_err(dev, "dmaengine_submit() fail\n"); | |
219 | return; | |
220 | } | |
221 | ||
a0d32bca | 222 | dma_async_issue_pending(dma->chan); |
0a4d94c0 | 223 | } |
0a4d94c0 KM |
224 | } |
225 | ||
f5cab3b8 KM |
226 | static void rsnd_dma_do_work(struct work_struct *work) |
227 | { | |
228 | struct rsnd_dma *dma = container_of(work, struct rsnd_dma, work); | |
229 | ||
230 | __rsnd_dma_start(dma); | |
231 | } | |
232 | ||
0a4d94c0 KM |
233 | int rsnd_dma_available(struct rsnd_dma *dma) |
234 | { | |
235 | return !!dma->chan; | |
236 | } | |
237 | ||
0a4d94c0 | 238 | int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma, |
4686a0ad | 239 | int is_play, int id) |
0a4d94c0 KM |
240 | { |
241 | struct device *dev = rsnd_priv_to_dev(priv); | |
9ade09d6 | 242 | struct dma_slave_config cfg; |
0a4d94c0 | 243 | dma_cap_mask_t mask; |
9ade09d6 | 244 | int ret; |
0a4d94c0 KM |
245 | |
246 | if (dma->chan) { | |
247 | dev_err(dev, "it already has dma channel\n"); | |
248 | return -EIO; | |
249 | } | |
250 | ||
251 | dma_cap_zero(mask); | |
252 | dma_cap_set(DMA_SLAVE, mask); | |
253 | ||
9ade09d6 KM |
254 | dma->chan = dma_request_slave_channel_compat(mask, shdma_chan_filter, |
255 | (void *)id, dev, | |
256 | is_play ? "tx" : "rx"); | |
0a4d94c0 KM |
257 | if (!dma->chan) { |
258 | dev_err(dev, "can't get dma channel\n"); | |
259 | return -EIO; | |
260 | } | |
261 | ||
9ade09d6 KM |
262 | cfg.slave_id = id; |
263 | cfg.dst_addr = 0; /* use default addr when playback */ | |
264 | cfg.src_addr = 0; /* use default addr when capture */ | |
265 | cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM; | |
266 | ||
267 | ret = dmaengine_slave_config(dma->chan, &cfg); | |
268 | if (ret < 0) | |
269 | goto rsnd_dma_init_err; | |
270 | ||
0a4d94c0 | 271 | dma->dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE; |
0a4d94c0 KM |
272 | INIT_WORK(&dma->work, rsnd_dma_do_work); |
273 | ||
274 | return 0; | |
9ade09d6 KM |
275 | |
276 | rsnd_dma_init_err: | |
277 | rsnd_dma_quit(priv, dma); | |
278 | ||
279 | return ret; | |
0a4d94c0 KM |
280 | } |
281 | ||
282 | void rsnd_dma_quit(struct rsnd_priv *priv, | |
283 | struct rsnd_dma *dma) | |
284 | { | |
285 | if (dma->chan) | |
286 | dma_release_channel(dma->chan); | |
287 | ||
288 | dma->chan = NULL; | |
289 | } | |
290 | ||
1536a968 KM |
291 | /* |
292 | * rsnd_dai functions | |
293 | */ | |
cdaa3cdf KM |
294 | #define rsnd_dai_call(rdai, io, fn) \ |
295 | ({ \ | |
296 | struct rsnd_mod *mod, *n; \ | |
297 | int ret = 0; \ | |
298 | for_each_rsnd_mod(mod, n, io) { \ | |
299 | ret = rsnd_mod_call(mod, fn, rdai, io); \ | |
300 | if (ret < 0) \ | |
301 | break; \ | |
302 | } \ | |
303 | ret; \ | |
304 | }) | |
305 | ||
306 | int rsnd_dai_connect(struct rsnd_dai *rdai, | |
307 | struct rsnd_mod *mod, | |
308 | struct rsnd_dai_stream *io) | |
309 | { | |
6020779b | 310 | if (!mod) |
cdaa3cdf | 311 | return -EIO; |
cdaa3cdf KM |
312 | |
313 | if (!list_empty(&mod->list)) { | |
6020779b KM |
314 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
315 | struct device *dev = rsnd_priv_to_dev(priv); | |
316 | ||
cdaa3cdf KM |
317 | dev_err(dev, "%s%d is not empty\n", |
318 | rsnd_mod_name(mod), | |
319 | rsnd_mod_id(mod)); | |
320 | return -EIO; | |
321 | } | |
322 | ||
323 | list_add_tail(&mod->list, &io->head); | |
4686a0ad | 324 | mod->io = io; |
cdaa3cdf KM |
325 | |
326 | return 0; | |
327 | } | |
328 | ||
329 | int rsnd_dai_disconnect(struct rsnd_mod *mod) | |
330 | { | |
331 | list_del_init(&mod->list); | |
4686a0ad | 332 | mod->io = NULL; |
cdaa3cdf KM |
333 | |
334 | return 0; | |
335 | } | |
336 | ||
4b4dab82 KM |
337 | int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai) |
338 | { | |
339 | int id = rdai - priv->rdai; | |
340 | ||
341 | if ((id < 0) || (id >= rsnd_dai_nr(priv))) | |
342 | return -EINVAL; | |
343 | ||
344 | return id; | |
345 | } | |
346 | ||
1536a968 KM |
347 | struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id) |
348 | { | |
2192f81c KM |
349 | if ((id < 0) || (id >= rsnd_dai_nr(priv))) |
350 | return NULL; | |
351 | ||
1536a968 KM |
352 | return priv->rdai + id; |
353 | } | |
354 | ||
355 | static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai) | |
356 | { | |
357 | struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai); | |
358 | ||
359 | return rsnd_dai_get(priv, dai->id); | |
360 | } | |
361 | ||
362 | int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io) | |
363 | { | |
364 | return &rdai->playback == io; | |
365 | } | |
366 | ||
367 | /* | |
368 | * rsnd_soc_dai functions | |
369 | */ | |
370 | int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional) | |
371 | { | |
372 | struct snd_pcm_substream *substream = io->substream; | |
373 | struct snd_pcm_runtime *runtime = substream->runtime; | |
374 | int pos = io->byte_pos + additional; | |
375 | ||
376 | pos %= (runtime->periods * io->byte_per_period); | |
377 | ||
378 | return pos; | |
379 | } | |
380 | ||
381 | void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte) | |
382 | { | |
383 | io->byte_pos += byte; | |
384 | ||
385 | if (io->byte_pos >= io->next_period_byte) { | |
386 | struct snd_pcm_substream *substream = io->substream; | |
387 | struct snd_pcm_runtime *runtime = substream->runtime; | |
388 | ||
389 | io->period_pos++; | |
390 | io->next_period_byte += io->byte_per_period; | |
391 | ||
392 | if (io->period_pos >= runtime->periods) { | |
393 | io->byte_pos = 0; | |
394 | io->period_pos = 0; | |
395 | io->next_period_byte = io->byte_per_period; | |
396 | } | |
397 | ||
398 | snd_pcm_period_elapsed(substream); | |
399 | } | |
400 | } | |
401 | ||
402 | static int rsnd_dai_stream_init(struct rsnd_dai_stream *io, | |
403 | struct snd_pcm_substream *substream) | |
404 | { | |
405 | struct snd_pcm_runtime *runtime = substream->runtime; | |
406 | ||
407 | if (!list_empty(&io->head)) | |
408 | return -EIO; | |
409 | ||
410 | INIT_LIST_HEAD(&io->head); | |
411 | io->substream = substream; | |
412 | io->byte_pos = 0; | |
413 | io->period_pos = 0; | |
414 | io->byte_per_period = runtime->period_size * | |
415 | runtime->channels * | |
416 | samples_to_bytes(runtime, 1); | |
417 | io->next_period_byte = io->byte_per_period; | |
418 | ||
419 | return 0; | |
420 | } | |
421 | ||
422 | static | |
423 | struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream) | |
424 | { | |
425 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
426 | ||
427 | return rtd->cpu_dai; | |
428 | } | |
429 | ||
430 | static | |
431 | struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai, | |
432 | struct snd_pcm_substream *substream) | |
433 | { | |
434 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
435 | return &rdai->playback; | |
436 | else | |
437 | return &rdai->capture; | |
438 | } | |
439 | ||
440 | static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd, | |
441 | struct snd_soc_dai *dai) | |
442 | { | |
443 | struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai); | |
444 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); | |
445 | struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); | |
4b4dab82 KM |
446 | struct rsnd_mod *mod = rsnd_ssi_mod_get_frm_dai(priv, |
447 | rsnd_dai_id(priv, rdai), | |
448 | rsnd_dai_is_play(rdai, io)); | |
449 | int ssi_id = rsnd_mod_id(mod); | |
1536a968 KM |
450 | int ret; |
451 | unsigned long flags; | |
452 | ||
453 | rsnd_lock(priv, flags); | |
454 | ||
455 | switch (cmd) { | |
456 | case SNDRV_PCM_TRIGGER_START: | |
457 | ret = rsnd_dai_stream_init(io, substream); | |
458 | if (ret < 0) | |
459 | goto dai_trigger_end; | |
460 | ||
461 | ret = rsnd_platform_call(priv, dai, start, ssi_id); | |
462 | if (ret < 0) | |
463 | goto dai_trigger_end; | |
464 | ||
3337744a KM |
465 | ret = rsnd_gen_path_init(priv, rdai, io); |
466 | if (ret < 0) | |
467 | goto dai_trigger_end; | |
468 | ||
cdaa3cdf KM |
469 | ret = rsnd_dai_call(rdai, io, init); |
470 | if (ret < 0) | |
471 | goto dai_trigger_end; | |
472 | ||
473 | ret = rsnd_dai_call(rdai, io, start); | |
474 | if (ret < 0) | |
475 | goto dai_trigger_end; | |
1536a968 KM |
476 | break; |
477 | case SNDRV_PCM_TRIGGER_STOP: | |
cdaa3cdf KM |
478 | ret = rsnd_dai_call(rdai, io, stop); |
479 | if (ret < 0) | |
480 | goto dai_trigger_end; | |
481 | ||
482 | ret = rsnd_dai_call(rdai, io, quit); | |
483 | if (ret < 0) | |
484 | goto dai_trigger_end; | |
485 | ||
3337744a | 486 | ret = rsnd_gen_path_exit(priv, rdai, io); |
1536a968 KM |
487 | if (ret < 0) |
488 | goto dai_trigger_end; | |
489 | ||
3337744a KM |
490 | ret = rsnd_platform_call(priv, dai, stop, ssi_id); |
491 | if (ret < 0) | |
492 | goto dai_trigger_end; | |
1536a968 KM |
493 | break; |
494 | default: | |
495 | ret = -EINVAL; | |
496 | } | |
497 | ||
498 | dai_trigger_end: | |
499 | rsnd_unlock(priv, flags); | |
500 | ||
501 | return ret; | |
502 | } | |
503 | ||
504 | static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) | |
505 | { | |
506 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); | |
507 | ||
508 | /* set master/slave audio interface */ | |
509 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { | |
510 | case SND_SOC_DAIFMT_CBM_CFM: | |
511 | rdai->clk_master = 1; | |
512 | break; | |
513 | case SND_SOC_DAIFMT_CBS_CFS: | |
514 | rdai->clk_master = 0; | |
515 | break; | |
516 | default: | |
517 | return -EINVAL; | |
518 | } | |
519 | ||
520 | /* set clock inversion */ | |
521 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { | |
522 | case SND_SOC_DAIFMT_NB_IF: | |
523 | rdai->bit_clk_inv = 0; | |
524 | rdai->frm_clk_inv = 1; | |
525 | break; | |
526 | case SND_SOC_DAIFMT_IB_NF: | |
527 | rdai->bit_clk_inv = 1; | |
528 | rdai->frm_clk_inv = 0; | |
529 | break; | |
530 | case SND_SOC_DAIFMT_IB_IF: | |
531 | rdai->bit_clk_inv = 1; | |
532 | rdai->frm_clk_inv = 1; | |
533 | break; | |
534 | case SND_SOC_DAIFMT_NB_NF: | |
535 | default: | |
536 | rdai->bit_clk_inv = 0; | |
537 | rdai->frm_clk_inv = 0; | |
538 | break; | |
539 | } | |
540 | ||
541 | /* set format */ | |
542 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { | |
543 | case SND_SOC_DAIFMT_I2S: | |
544 | rdai->sys_delay = 0; | |
545 | rdai->data_alignment = 0; | |
546 | break; | |
547 | case SND_SOC_DAIFMT_LEFT_J: | |
548 | rdai->sys_delay = 1; | |
549 | rdai->data_alignment = 0; | |
550 | break; | |
551 | case SND_SOC_DAIFMT_RIGHT_J: | |
552 | rdai->sys_delay = 1; | |
553 | rdai->data_alignment = 1; | |
554 | break; | |
555 | } | |
556 | ||
557 | return 0; | |
558 | } | |
559 | ||
560 | static const struct snd_soc_dai_ops rsnd_soc_dai_ops = { | |
561 | .trigger = rsnd_soc_dai_trigger, | |
562 | .set_fmt = rsnd_soc_dai_set_fmt, | |
563 | }; | |
564 | ||
565 | static int rsnd_dai_probe(struct platform_device *pdev, | |
566 | struct rcar_snd_info *info, | |
567 | struct rsnd_priv *priv) | |
568 | { | |
569 | struct snd_soc_dai_driver *drv; | |
570 | struct rsnd_dai *rdai; | |
4b4dab82 | 571 | struct rsnd_mod *pmod, *cmod; |
1536a968 | 572 | struct device *dev = rsnd_priv_to_dev(priv); |
4b4dab82 KM |
573 | int dai_nr; |
574 | int i; | |
575 | ||
576 | /* get max dai nr */ | |
577 | for (dai_nr = 0; dai_nr < 32; dai_nr++) { | |
578 | pmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 1); | |
579 | cmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 0); | |
580 | ||
581 | if (!pmod && !cmod) | |
582 | break; | |
583 | } | |
584 | ||
585 | if (!dai_nr) { | |
586 | dev_err(dev, "no dai\n"); | |
587 | return -EIO; | |
588 | } | |
1536a968 KM |
589 | |
590 | drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL); | |
591 | rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL); | |
592 | if (!drv || !rdai) { | |
593 | dev_err(dev, "dai allocate failed\n"); | |
594 | return -ENOMEM; | |
595 | } | |
596 | ||
597 | for (i = 0; i < dai_nr; i++) { | |
1536a968 | 598 | |
4b4dab82 KM |
599 | pmod = rsnd_ssi_mod_get_frm_dai(priv, i, 1); |
600 | cmod = rsnd_ssi_mod_get_frm_dai(priv, i, 0); | |
1536a968 KM |
601 | |
602 | /* | |
603 | * init rsnd_dai | |
604 | */ | |
605 | INIT_LIST_HEAD(&rdai[i].playback.head); | |
606 | INIT_LIST_HEAD(&rdai[i].capture.head); | |
607 | ||
1536a968 KM |
608 | snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i); |
609 | ||
610 | /* | |
611 | * init snd_soc_dai_driver | |
612 | */ | |
613 | drv[i].name = rdai[i].name; | |
614 | drv[i].ops = &rsnd_soc_dai_ops; | |
4b4dab82 | 615 | if (pmod) { |
1536a968 KM |
616 | drv[i].playback.rates = RSND_RATES; |
617 | drv[i].playback.formats = RSND_FMTS; | |
618 | drv[i].playback.channels_min = 2; | |
619 | drv[i].playback.channels_max = 2; | |
620 | } | |
4b4dab82 | 621 | if (cmod) { |
1536a968 KM |
622 | drv[i].capture.rates = RSND_RATES; |
623 | drv[i].capture.formats = RSND_FMTS; | |
624 | drv[i].capture.channels_min = 2; | |
625 | drv[i].capture.channels_max = 2; | |
626 | } | |
627 | ||
4b4dab82 KM |
628 | dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name, |
629 | pmod ? "play" : " -- ", | |
630 | cmod ? "capture" : " -- "); | |
1536a968 KM |
631 | } |
632 | ||
633 | priv->dai_nr = dai_nr; | |
634 | priv->daidrv = drv; | |
635 | priv->rdai = rdai; | |
636 | ||
637 | return 0; | |
638 | } | |
639 | ||
640 | static void rsnd_dai_remove(struct platform_device *pdev, | |
641 | struct rsnd_priv *priv) | |
642 | { | |
643 | } | |
644 | ||
645 | /* | |
646 | * pcm ops | |
647 | */ | |
648 | static struct snd_pcm_hardware rsnd_pcm_hardware = { | |
649 | .info = SNDRV_PCM_INFO_INTERLEAVED | | |
650 | SNDRV_PCM_INFO_MMAP | | |
651 | SNDRV_PCM_INFO_MMAP_VALID | | |
652 | SNDRV_PCM_INFO_PAUSE, | |
1536a968 KM |
653 | .buffer_bytes_max = 64 * 1024, |
654 | .period_bytes_min = 32, | |
655 | .period_bytes_max = 8192, | |
656 | .periods_min = 1, | |
657 | .periods_max = 32, | |
658 | .fifo_size = 256, | |
659 | }; | |
660 | ||
661 | static int rsnd_pcm_open(struct snd_pcm_substream *substream) | |
662 | { | |
663 | struct snd_pcm_runtime *runtime = substream->runtime; | |
664 | int ret = 0; | |
665 | ||
666 | snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware); | |
667 | ||
668 | ret = snd_pcm_hw_constraint_integer(runtime, | |
669 | SNDRV_PCM_HW_PARAM_PERIODS); | |
670 | ||
671 | return ret; | |
672 | } | |
673 | ||
674 | static int rsnd_hw_params(struct snd_pcm_substream *substream, | |
675 | struct snd_pcm_hw_params *hw_params) | |
676 | { | |
677 | return snd_pcm_lib_malloc_pages(substream, | |
678 | params_buffer_bytes(hw_params)); | |
679 | } | |
680 | ||
681 | static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream) | |
682 | { | |
683 | struct snd_pcm_runtime *runtime = substream->runtime; | |
684 | struct snd_soc_dai *dai = rsnd_substream_to_dai(substream); | |
685 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); | |
686 | struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); | |
687 | ||
688 | return bytes_to_frames(runtime, io->byte_pos); | |
689 | } | |
690 | ||
691 | static struct snd_pcm_ops rsnd_pcm_ops = { | |
692 | .open = rsnd_pcm_open, | |
693 | .ioctl = snd_pcm_lib_ioctl, | |
694 | .hw_params = rsnd_hw_params, | |
695 | .hw_free = snd_pcm_lib_free_pages, | |
696 | .pointer = rsnd_pointer, | |
697 | }; | |
698 | ||
699 | /* | |
700 | * snd_soc_platform | |
701 | */ | |
702 | ||
703 | #define PREALLOC_BUFFER (32 * 1024) | |
704 | #define PREALLOC_BUFFER_MAX (32 * 1024) | |
705 | ||
706 | static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd) | |
707 | { | |
708 | return snd_pcm_lib_preallocate_pages_for_all( | |
709 | rtd->pcm, | |
710 | SNDRV_DMA_TYPE_DEV, | |
711 | rtd->card->snd_card->dev, | |
712 | PREALLOC_BUFFER, PREALLOC_BUFFER_MAX); | |
713 | } | |
714 | ||
715 | static void rsnd_pcm_free(struct snd_pcm *pcm) | |
716 | { | |
717 | snd_pcm_lib_preallocate_free_for_all(pcm); | |
718 | } | |
719 | ||
720 | static struct snd_soc_platform_driver rsnd_soc_platform = { | |
721 | .ops = &rsnd_pcm_ops, | |
722 | .pcm_new = rsnd_pcm_new, | |
723 | .pcm_free = rsnd_pcm_free, | |
724 | }; | |
725 | ||
726 | static const struct snd_soc_component_driver rsnd_soc_component = { | |
727 | .name = "rsnd", | |
728 | }; | |
729 | ||
730 | /* | |
731 | * rsnd probe | |
732 | */ | |
733 | static int rsnd_probe(struct platform_device *pdev) | |
734 | { | |
735 | struct rcar_snd_info *info; | |
736 | struct rsnd_priv *priv; | |
737 | struct device *dev = &pdev->dev; | |
738 | int ret; | |
739 | ||
740 | info = pdev->dev.platform_data; | |
741 | if (!info) { | |
742 | dev_err(dev, "driver needs R-Car sound information\n"); | |
743 | return -ENODEV; | |
744 | } | |
745 | ||
746 | /* | |
747 | * init priv data | |
748 | */ | |
749 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); | |
750 | if (!priv) { | |
751 | dev_err(dev, "priv allocate failed\n"); | |
752 | return -ENODEV; | |
753 | } | |
754 | ||
755 | priv->dev = dev; | |
756 | priv->info = info; | |
757 | spin_lock_init(&priv->lock); | |
758 | ||
759 | /* | |
760 | * init each module | |
761 | */ | |
3337744a | 762 | ret = rsnd_gen_probe(pdev, info, priv); |
106d2eff | 763 | if (ret) |
3337744a KM |
764 | return ret; |
765 | ||
07539c1d | 766 | ret = rsnd_scu_probe(pdev, info, priv); |
106d2eff | 767 | if (ret) |
07539c1d KM |
768 | return ret; |
769 | ||
dfc9403b | 770 | ret = rsnd_adg_probe(pdev, info, priv); |
106d2eff | 771 | if (ret) |
dfc9403b KM |
772 | return ret; |
773 | ||
ae5c3223 | 774 | ret = rsnd_ssi_probe(pdev, info, priv); |
106d2eff | 775 | if (ret) |
ae5c3223 KM |
776 | return ret; |
777 | ||
4b4dab82 | 778 | ret = rsnd_dai_probe(pdev, info, priv); |
106d2eff | 779 | if (ret) |
4b4dab82 KM |
780 | return ret; |
781 | ||
1536a968 KM |
782 | /* |
783 | * asoc register | |
784 | */ | |
785 | ret = snd_soc_register_platform(dev, &rsnd_soc_platform); | |
786 | if (ret < 0) { | |
787 | dev_err(dev, "cannot snd soc register\n"); | |
788 | return ret; | |
789 | } | |
790 | ||
791 | ret = snd_soc_register_component(dev, &rsnd_soc_component, | |
792 | priv->daidrv, rsnd_dai_nr(priv)); | |
793 | if (ret < 0) { | |
794 | dev_err(dev, "cannot snd dai register\n"); | |
795 | goto exit_snd_soc; | |
796 | } | |
797 | ||
798 | dev_set_drvdata(dev, priv); | |
799 | ||
800 | pm_runtime_enable(dev); | |
801 | ||
802 | dev_info(dev, "probed\n"); | |
803 | return ret; | |
804 | ||
805 | exit_snd_soc: | |
806 | snd_soc_unregister_platform(dev); | |
807 | ||
808 | return ret; | |
809 | } | |
810 | ||
811 | static int rsnd_remove(struct platform_device *pdev) | |
812 | { | |
813 | struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev); | |
814 | ||
815 | pm_runtime_disable(&pdev->dev); | |
816 | ||
817 | /* | |
818 | * remove each module | |
819 | */ | |
ae5c3223 | 820 | rsnd_ssi_remove(pdev, priv); |
dfc9403b | 821 | rsnd_adg_remove(pdev, priv); |
07539c1d | 822 | rsnd_scu_remove(pdev, priv); |
1536a968 | 823 | rsnd_dai_remove(pdev, priv); |
3337744a | 824 | rsnd_gen_remove(pdev, priv); |
1536a968 KM |
825 | |
826 | return 0; | |
827 | } | |
828 | ||
829 | static struct platform_driver rsnd_driver = { | |
830 | .driver = { | |
831 | .name = "rcar_sound", | |
832 | }, | |
833 | .probe = rsnd_probe, | |
834 | .remove = rsnd_remove, | |
835 | }; | |
836 | module_platform_driver(rsnd_driver); | |
837 | ||
838 | MODULE_LICENSE("GPL"); | |
839 | MODULE_DESCRIPTION("Renesas R-Car audio driver"); | |
840 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); | |
841 | MODULE_ALIAS("platform:rcar-pcm-audio"); |