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 | ||
141 | void rsnd_mod_init(struct rsnd_priv *priv, | |
142 | struct rsnd_mod *mod, | |
143 | struct rsnd_mod_ops *ops, | |
a126021d | 144 | enum rsnd_mod_type type, |
cdaa3cdf KM |
145 | int id) |
146 | { | |
147 | mod->priv = priv; | |
148 | mod->id = id; | |
149 | mod->ops = ops; | |
a126021d | 150 | mod->type = type; |
cdaa3cdf KM |
151 | } |
152 | ||
0a4d94c0 KM |
153 | /* |
154 | * rsnd_dma functions | |
155 | */ | |
f5cab3b8 | 156 | static void __rsnd_dma_start(struct rsnd_dma *dma); |
0a4d94c0 KM |
157 | static void rsnd_dma_continue(struct rsnd_dma *dma) |
158 | { | |
159 | /* push next A or B plane */ | |
160 | dma->submit_loop = 1; | |
161 | schedule_work(&dma->work); | |
162 | } | |
163 | ||
164 | void rsnd_dma_start(struct rsnd_dma *dma) | |
165 | { | |
166 | /* push both A and B plane*/ | |
4686a0ad | 167 | dma->offset = 0; |
0a4d94c0 | 168 | dma->submit_loop = 2; |
f5cab3b8 | 169 | __rsnd_dma_start(dma); |
0a4d94c0 KM |
170 | } |
171 | ||
172 | void rsnd_dma_stop(struct rsnd_dma *dma) | |
173 | { | |
174 | dma->submit_loop = 0; | |
175 | cancel_work_sync(&dma->work); | |
176 | dmaengine_terminate_all(dma->chan); | |
177 | } | |
178 | ||
179 | static void rsnd_dma_complete(void *data) | |
180 | { | |
181 | struct rsnd_dma *dma = (struct rsnd_dma *)data; | |
4686a0ad | 182 | struct rsnd_mod *mod = rsnd_dma_to_mod(dma); |
9b5ab573 | 183 | struct rsnd_priv *priv = rsnd_mod_to_priv(rsnd_dma_to_mod(dma)); |
4686a0ad | 184 | struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); |
0a4d94c0 KM |
185 | unsigned long flags; |
186 | ||
187 | rsnd_lock(priv, flags); | |
188 | ||
4686a0ad KM |
189 | /* |
190 | * Renesas sound Gen1 needs 1 DMAC, | |
191 | * Gen2 needs 2 DMAC. | |
192 | * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri. | |
193 | * But, Audio-DMAC-peri-peri doesn't have interrupt, | |
194 | * and this driver is assuming that here. | |
195 | * | |
196 | * If Audio-DMAC-peri-peri has interrpt, | |
197 | * rsnd_dai_pointer_update() will be called twice, | |
198 | * ant it will breaks io->byte_pos | |
199 | */ | |
200 | ||
201 | rsnd_dai_pointer_update(io, io->byte_per_period); | |
0a4d94c0 KM |
202 | |
203 | if (dma->submit_loop) | |
204 | rsnd_dma_continue(dma); | |
205 | ||
206 | rsnd_unlock(priv, flags); | |
207 | } | |
208 | ||
f5cab3b8 | 209 | static void __rsnd_dma_start(struct rsnd_dma *dma) |
0a4d94c0 | 210 | { |
4686a0ad KM |
211 | struct rsnd_mod *mod = rsnd_dma_to_mod(dma); |
212 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); | |
213 | struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); | |
214 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); | |
0a4d94c0 KM |
215 | struct device *dev = rsnd_priv_to_dev(priv); |
216 | struct dma_async_tx_descriptor *desc; | |
217 | dma_addr_t buf; | |
4686a0ad | 218 | size_t len = io->byte_per_period; |
0a4d94c0 KM |
219 | int i; |
220 | ||
221 | for (i = 0; i < dma->submit_loop; i++) { | |
222 | ||
4686a0ad KM |
223 | buf = runtime->dma_addr + |
224 | rsnd_dai_pointer_offset(io, dma->offset + len); | |
225 | dma->offset = len; | |
0a4d94c0 KM |
226 | |
227 | desc = dmaengine_prep_slave_single( | |
228 | dma->chan, buf, len, dma->dir, | |
229 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); | |
230 | if (!desc) { | |
231 | dev_err(dev, "dmaengine_prep_slave_sg() fail\n"); | |
232 | return; | |
233 | } | |
234 | ||
235 | desc->callback = rsnd_dma_complete; | |
236 | desc->callback_param = dma; | |
237 | ||
238 | if (dmaengine_submit(desc) < 0) { | |
239 | dev_err(dev, "dmaengine_submit() fail\n"); | |
240 | return; | |
241 | } | |
242 | ||
a0d32bca | 243 | dma_async_issue_pending(dma->chan); |
0a4d94c0 | 244 | } |
0a4d94c0 KM |
245 | } |
246 | ||
f5cab3b8 KM |
247 | static void rsnd_dma_do_work(struct work_struct *work) |
248 | { | |
249 | struct rsnd_dma *dma = container_of(work, struct rsnd_dma, work); | |
250 | ||
251 | __rsnd_dma_start(dma); | |
252 | } | |
253 | ||
0a4d94c0 KM |
254 | int rsnd_dma_available(struct rsnd_dma *dma) |
255 | { | |
256 | return !!dma->chan; | |
257 | } | |
258 | ||
0a4d94c0 | 259 | int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma, |
4686a0ad | 260 | int is_play, int id) |
0a4d94c0 KM |
261 | { |
262 | struct device *dev = rsnd_priv_to_dev(priv); | |
9ade09d6 | 263 | struct dma_slave_config cfg; |
0a4d94c0 | 264 | dma_cap_mask_t mask; |
9ade09d6 | 265 | int ret; |
0a4d94c0 KM |
266 | |
267 | if (dma->chan) { | |
268 | dev_err(dev, "it already has dma channel\n"); | |
269 | return -EIO; | |
270 | } | |
271 | ||
272 | dma_cap_zero(mask); | |
273 | dma_cap_set(DMA_SLAVE, mask); | |
274 | ||
9ade09d6 KM |
275 | dma->chan = dma_request_slave_channel_compat(mask, shdma_chan_filter, |
276 | (void *)id, dev, | |
277 | is_play ? "tx" : "rx"); | |
0a4d94c0 KM |
278 | if (!dma->chan) { |
279 | dev_err(dev, "can't get dma channel\n"); | |
280 | return -EIO; | |
281 | } | |
282 | ||
9ade09d6 KM |
283 | cfg.slave_id = id; |
284 | cfg.dst_addr = 0; /* use default addr when playback */ | |
285 | cfg.src_addr = 0; /* use default addr when capture */ | |
286 | cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM; | |
287 | ||
288 | ret = dmaengine_slave_config(dma->chan, &cfg); | |
289 | if (ret < 0) | |
290 | goto rsnd_dma_init_err; | |
291 | ||
0a4d94c0 | 292 | dma->dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE; |
0a4d94c0 KM |
293 | INIT_WORK(&dma->work, rsnd_dma_do_work); |
294 | ||
295 | return 0; | |
9ade09d6 KM |
296 | |
297 | rsnd_dma_init_err: | |
298 | rsnd_dma_quit(priv, dma); | |
299 | ||
300 | return ret; | |
0a4d94c0 KM |
301 | } |
302 | ||
303 | void rsnd_dma_quit(struct rsnd_priv *priv, | |
304 | struct rsnd_dma *dma) | |
305 | { | |
306 | if (dma->chan) | |
307 | dma_release_channel(dma->chan); | |
308 | ||
309 | dma->chan = NULL; | |
310 | } | |
311 | ||
d7bdbc5d KM |
312 | /* |
313 | * settting function | |
314 | */ | |
315 | u32 rsnd_get_adinr(struct rsnd_mod *mod) | |
316 | { | |
317 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); | |
318 | struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); | |
319 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); | |
320 | struct device *dev = rsnd_priv_to_dev(priv); | |
321 | u32 adinr = runtime->channels; | |
322 | ||
323 | switch (runtime->sample_bits) { | |
324 | case 16: | |
325 | adinr |= (8 << 16); | |
326 | break; | |
327 | case 32: | |
328 | adinr |= (0 << 16); | |
329 | break; | |
330 | default: | |
331 | dev_warn(dev, "not supported sample bits\n"); | |
332 | return 0; | |
333 | } | |
334 | ||
335 | return adinr; | |
336 | } | |
337 | ||
1536a968 KM |
338 | /* |
339 | * rsnd_dai functions | |
340 | */ | |
68b6af36 | 341 | #define __rsnd_mod_call(mod, func, rdai...) \ |
d870a91e KM |
342 | ({ \ |
343 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \ | |
344 | struct device *dev = rsnd_priv_to_dev(priv); \ | |
345 | dev_dbg(dev, "%s [%d] %s\n", \ | |
346 | rsnd_mod_name(mod), rsnd_mod_id(mod), #func); \ | |
b42fccf6 | 347 | (mod)->ops->func(mod, rdai); \ |
d870a91e KM |
348 | }) |
349 | ||
68b6af36 | 350 | #define rsnd_mod_call(mod, func, rdai...) \ |
d870a91e KM |
351 | (!(mod) ? -ENODEV : \ |
352 | !((mod)->ops->func) ? 0 : \ | |
68b6af36 | 353 | __rsnd_mod_call(mod, func, rdai)) |
d870a91e | 354 | |
68b6af36 | 355 | #define rsnd_dai_call(fn, io, rdai...) \ |
d870a91e | 356 | ({ \ |
a126021d KM |
357 | struct rsnd_mod *mod; \ |
358 | int ret = 0, i; \ | |
359 | for (i = 0; i < RSND_MOD_MAX; i++) { \ | |
360 | mod = (io)->mod[i]; \ | |
361 | if (!mod) \ | |
362 | continue; \ | |
68b6af36 | 363 | ret = rsnd_mod_call(mod, fn, rdai); \ |
d870a91e KM |
364 | if (ret < 0) \ |
365 | break; \ | |
366 | } \ | |
367 | ret; \ | |
cdaa3cdf KM |
368 | }) |
369 | ||
a126021d | 370 | static int rsnd_dai_connect(struct rsnd_mod *mod, |
9bfed6cf | 371 | struct rsnd_dai_stream *io) |
cdaa3cdf | 372 | { |
6020779b | 373 | if (!mod) |
cdaa3cdf | 374 | return -EIO; |
cdaa3cdf | 375 | |
a126021d | 376 | if (io->mod[mod->type]) { |
6020779b KM |
377 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
378 | struct device *dev = rsnd_priv_to_dev(priv); | |
379 | ||
cdaa3cdf KM |
380 | dev_err(dev, "%s%d is not empty\n", |
381 | rsnd_mod_name(mod), | |
382 | rsnd_mod_id(mod)); | |
383 | return -EIO; | |
384 | } | |
385 | ||
a126021d | 386 | io->mod[mod->type] = mod; |
4686a0ad | 387 | mod->io = io; |
cdaa3cdf KM |
388 | |
389 | return 0; | |
390 | } | |
391 | ||
4b4dab82 KM |
392 | int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai) |
393 | { | |
394 | int id = rdai - priv->rdai; | |
395 | ||
ecba9e72 | 396 | if ((id < 0) || (id >= rsnd_rdai_nr(priv))) |
4b4dab82 KM |
397 | return -EINVAL; |
398 | ||
399 | return id; | |
400 | } | |
401 | ||
1536a968 KM |
402 | struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id) |
403 | { | |
ecba9e72 | 404 | if ((id < 0) || (id >= rsnd_rdai_nr(priv))) |
2192f81c KM |
405 | return NULL; |
406 | ||
1536a968 KM |
407 | return priv->rdai + id; |
408 | } | |
409 | ||
410 | static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai) | |
411 | { | |
412 | struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai); | |
413 | ||
414 | return rsnd_dai_get(priv, dai->id); | |
415 | } | |
416 | ||
417 | int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io) | |
418 | { | |
419 | return &rdai->playback == io; | |
420 | } | |
421 | ||
422 | /* | |
423 | * rsnd_soc_dai functions | |
424 | */ | |
425 | int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional) | |
426 | { | |
427 | struct snd_pcm_substream *substream = io->substream; | |
428 | struct snd_pcm_runtime *runtime = substream->runtime; | |
429 | int pos = io->byte_pos + additional; | |
430 | ||
431 | pos %= (runtime->periods * io->byte_per_period); | |
432 | ||
433 | return pos; | |
434 | } | |
435 | ||
436 | void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte) | |
437 | { | |
438 | io->byte_pos += byte; | |
439 | ||
440 | if (io->byte_pos >= io->next_period_byte) { | |
441 | struct snd_pcm_substream *substream = io->substream; | |
442 | struct snd_pcm_runtime *runtime = substream->runtime; | |
443 | ||
444 | io->period_pos++; | |
445 | io->next_period_byte += io->byte_per_period; | |
446 | ||
447 | if (io->period_pos >= runtime->periods) { | |
448 | io->byte_pos = 0; | |
449 | io->period_pos = 0; | |
450 | io->next_period_byte = io->byte_per_period; | |
451 | } | |
452 | ||
453 | snd_pcm_period_elapsed(substream); | |
454 | } | |
455 | } | |
456 | ||
457 | static int rsnd_dai_stream_init(struct rsnd_dai_stream *io, | |
458 | struct snd_pcm_substream *substream) | |
459 | { | |
460 | struct snd_pcm_runtime *runtime = substream->runtime; | |
461 | ||
1536a968 KM |
462 | io->substream = substream; |
463 | io->byte_pos = 0; | |
464 | io->period_pos = 0; | |
465 | io->byte_per_period = runtime->period_size * | |
466 | runtime->channels * | |
467 | samples_to_bytes(runtime, 1); | |
468 | io->next_period_byte = io->byte_per_period; | |
469 | ||
470 | return 0; | |
471 | } | |
472 | ||
473 | static | |
474 | struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream) | |
475 | { | |
476 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
477 | ||
478 | return rtd->cpu_dai; | |
479 | } | |
480 | ||
481 | static | |
482 | struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai, | |
483 | struct snd_pcm_substream *substream) | |
484 | { | |
485 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
486 | return &rdai->playback; | |
487 | else | |
488 | return &rdai->capture; | |
489 | } | |
490 | ||
491 | static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd, | |
492 | struct snd_soc_dai *dai) | |
493 | { | |
494 | struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai); | |
495 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); | |
496 | struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); | |
29e69fd2 | 497 | int ssi_id = rsnd_mod_id(rsnd_io_to_mod_ssi(io)); |
1536a968 KM |
498 | int ret; |
499 | unsigned long flags; | |
500 | ||
501 | rsnd_lock(priv, flags); | |
502 | ||
503 | switch (cmd) { | |
504 | case SNDRV_PCM_TRIGGER_START: | |
505 | ret = rsnd_dai_stream_init(io, substream); | |
506 | if (ret < 0) | |
507 | goto dai_trigger_end; | |
508 | ||
509 | ret = rsnd_platform_call(priv, dai, start, ssi_id); | |
510 | if (ret < 0) | |
511 | goto dai_trigger_end; | |
512 | ||
68b6af36 | 513 | ret = rsnd_dai_call(init, io, rdai); |
cdaa3cdf KM |
514 | if (ret < 0) |
515 | goto dai_trigger_end; | |
516 | ||
68b6af36 | 517 | ret = rsnd_dai_call(start, io, rdai); |
cdaa3cdf KM |
518 | if (ret < 0) |
519 | goto dai_trigger_end; | |
1536a968 KM |
520 | break; |
521 | case SNDRV_PCM_TRIGGER_STOP: | |
68b6af36 | 522 | ret = rsnd_dai_call(stop, io, rdai); |
cdaa3cdf KM |
523 | if (ret < 0) |
524 | goto dai_trigger_end; | |
525 | ||
68b6af36 | 526 | ret = rsnd_dai_call(quit, io, rdai); |
cdaa3cdf KM |
527 | if (ret < 0) |
528 | goto dai_trigger_end; | |
529 | ||
3337744a KM |
530 | ret = rsnd_platform_call(priv, dai, stop, ssi_id); |
531 | if (ret < 0) | |
532 | goto dai_trigger_end; | |
1536a968 KM |
533 | break; |
534 | default: | |
535 | ret = -EINVAL; | |
536 | } | |
537 | ||
538 | dai_trigger_end: | |
539 | rsnd_unlock(priv, flags); | |
540 | ||
541 | return ret; | |
542 | } | |
543 | ||
544 | static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) | |
545 | { | |
546 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); | |
547 | ||
548 | /* set master/slave audio interface */ | |
549 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { | |
550 | case SND_SOC_DAIFMT_CBM_CFM: | |
e1508289 | 551 | rdai->clk_master = 0; |
1536a968 KM |
552 | break; |
553 | case SND_SOC_DAIFMT_CBS_CFS: | |
e1508289 | 554 | rdai->clk_master = 1; /* codec is slave, cpu is master */ |
1536a968 KM |
555 | break; |
556 | default: | |
557 | return -EINVAL; | |
558 | } | |
559 | ||
560 | /* set clock inversion */ | |
561 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { | |
562 | case SND_SOC_DAIFMT_NB_IF: | |
563 | rdai->bit_clk_inv = 0; | |
564 | rdai->frm_clk_inv = 1; | |
565 | break; | |
566 | case SND_SOC_DAIFMT_IB_NF: | |
567 | rdai->bit_clk_inv = 1; | |
568 | rdai->frm_clk_inv = 0; | |
569 | break; | |
570 | case SND_SOC_DAIFMT_IB_IF: | |
571 | rdai->bit_clk_inv = 1; | |
572 | rdai->frm_clk_inv = 1; | |
573 | break; | |
574 | case SND_SOC_DAIFMT_NB_NF: | |
575 | default: | |
576 | rdai->bit_clk_inv = 0; | |
577 | rdai->frm_clk_inv = 0; | |
578 | break; | |
579 | } | |
580 | ||
581 | /* set format */ | |
582 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { | |
583 | case SND_SOC_DAIFMT_I2S: | |
584 | rdai->sys_delay = 0; | |
585 | rdai->data_alignment = 0; | |
586 | break; | |
587 | case SND_SOC_DAIFMT_LEFT_J: | |
588 | rdai->sys_delay = 1; | |
589 | rdai->data_alignment = 0; | |
590 | break; | |
591 | case SND_SOC_DAIFMT_RIGHT_J: | |
592 | rdai->sys_delay = 1; | |
593 | rdai->data_alignment = 1; | |
594 | break; | |
595 | } | |
596 | ||
597 | return 0; | |
598 | } | |
599 | ||
600 | static const struct snd_soc_dai_ops rsnd_soc_dai_ops = { | |
601 | .trigger = rsnd_soc_dai_trigger, | |
602 | .set_fmt = rsnd_soc_dai_set_fmt, | |
603 | }; | |
604 | ||
739f9502 KM |
605 | #define rsnd_path_parse(priv, io, type) \ |
606 | ({ \ | |
607 | struct rsnd_mod *mod; \ | |
608 | int ret = 0; \ | |
609 | int id = -1; \ | |
610 | \ | |
611 | if (rsnd_is_enable_path(io, type)) { \ | |
612 | id = rsnd_info_id(priv, io, type); \ | |
613 | if (id >= 0) { \ | |
614 | mod = rsnd_##type##_mod_get(priv, id); \ | |
615 | ret = rsnd_dai_connect(mod, io); \ | |
616 | } \ | |
617 | } \ | |
618 | ret; \ | |
619 | }) | |
620 | ||
9bfed6cf KM |
621 | static int rsnd_path_init(struct rsnd_priv *priv, |
622 | struct rsnd_dai *rdai, | |
623 | struct rsnd_dai_stream *io) | |
624 | { | |
9bfed6cf | 625 | int ret; |
9bfed6cf KM |
626 | |
627 | /* | |
628 | * Gen1 is created by SRU/SSI, and this SRU is base module of | |
629 | * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU) | |
630 | * | |
631 | * Easy image is.. | |
632 | * Gen1 SRU = Gen2 SCU + SSIU + etc | |
633 | * | |
634 | * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is | |
635 | * using fixed path. | |
9bfed6cf | 636 | */ |
9bfed6cf | 637 | |
ba9c949f | 638 | /* SRC */ |
739f9502 KM |
639 | ret = rsnd_path_parse(priv, io, src); |
640 | if (ret < 0) | |
641 | return ret; | |
9bfed6cf KM |
642 | |
643 | /* SSI */ | |
739f9502 KM |
644 | ret = rsnd_path_parse(priv, io, ssi); |
645 | if (ret < 0) | |
646 | return ret; | |
9bfed6cf | 647 | |
bff58ea4 KM |
648 | /* DVC */ |
649 | ret = rsnd_path_parse(priv, io, dvc); | |
650 | if (ret < 0) | |
651 | return ret; | |
652 | ||
9bfed6cf KM |
653 | return ret; |
654 | } | |
655 | ||
90e8e50f KM |
656 | static void rsnd_of_parse_dai(struct platform_device *pdev, |
657 | const struct rsnd_of_data *of_data, | |
658 | struct rsnd_priv *priv) | |
659 | { | |
660 | struct device_node *dai_node, *dai_np; | |
661 | struct device_node *ssi_node, *ssi_np; | |
662 | struct device_node *src_node, *src_np; | |
663 | struct device_node *playback, *capture; | |
664 | struct rsnd_dai_platform_info *dai_info; | |
665 | struct rcar_snd_info *info = rsnd_priv_to_info(priv); | |
666 | struct device *dev = &pdev->dev; | |
667 | int nr, i; | |
668 | int dai_i, ssi_i, src_i; | |
669 | ||
670 | if (!of_data) | |
671 | return; | |
672 | ||
673 | dai_node = of_get_child_by_name(dev->of_node, "rcar_sound,dai"); | |
674 | if (!dai_node) | |
675 | return; | |
676 | ||
677 | nr = of_get_child_count(dai_node); | |
678 | if (!nr) | |
679 | return; | |
680 | ||
681 | dai_info = devm_kzalloc(dev, | |
682 | sizeof(struct rsnd_dai_platform_info) * nr, | |
683 | GFP_KERNEL); | |
684 | if (!dai_info) { | |
685 | dev_err(dev, "dai info allocation error\n"); | |
686 | return; | |
687 | } | |
688 | ||
689 | info->dai_info_nr = nr; | |
690 | info->dai_info = dai_info; | |
691 | ||
692 | ssi_node = of_get_child_by_name(dev->of_node, "rcar_sound,ssi"); | |
693 | src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src"); | |
694 | ||
695 | #define mod_parse(name) \ | |
696 | if (name##_node) { \ | |
697 | struct rsnd_##name##_platform_info *name##_info; \ | |
698 | \ | |
699 | name##_i = 0; \ | |
700 | for_each_child_of_node(name##_node, name##_np) { \ | |
701 | name##_info = info->name##_info + name##_i; \ | |
702 | \ | |
703 | if (name##_np == playback) \ | |
704 | dai_info->playback.name = name##_info; \ | |
705 | if (name##_np == capture) \ | |
706 | dai_info->capture.name = name##_info; \ | |
707 | \ | |
708 | name##_i++; \ | |
709 | } \ | |
710 | } | |
711 | ||
712 | /* | |
713 | * parse all dai | |
714 | */ | |
715 | dai_i = 0; | |
716 | for_each_child_of_node(dai_node, dai_np) { | |
717 | dai_info = info->dai_info + dai_i; | |
718 | ||
719 | for (i = 0;; i++) { | |
720 | ||
721 | playback = of_parse_phandle(dai_np, "playback", i); | |
722 | capture = of_parse_phandle(dai_np, "capture", i); | |
723 | ||
724 | if (!playback && !capture) | |
725 | break; | |
726 | ||
727 | mod_parse(ssi); | |
728 | mod_parse(src); | |
729 | ||
730 | if (playback) | |
731 | of_node_put(playback); | |
732 | if (capture) | |
733 | of_node_put(capture); | |
734 | } | |
735 | ||
736 | dai_i++; | |
737 | } | |
738 | } | |
739 | ||
1536a968 | 740 | static int rsnd_dai_probe(struct platform_device *pdev, |
90e8e50f | 741 | const struct rsnd_of_data *of_data, |
1536a968 KM |
742 | struct rsnd_priv *priv) |
743 | { | |
744 | struct snd_soc_dai_driver *drv; | |
78f13d0c | 745 | struct rcar_snd_info *info = rsnd_priv_to_info(priv); |
1536a968 | 746 | struct rsnd_dai *rdai; |
29e69fd2 | 747 | struct rsnd_ssi_platform_info *pmod, *cmod; |
1536a968 | 748 | struct device *dev = rsnd_priv_to_dev(priv); |
90e8e50f | 749 | int dai_nr; |
4b4dab82 KM |
750 | int i; |
751 | ||
90e8e50f KM |
752 | rsnd_of_parse_dai(pdev, of_data, priv); |
753 | ||
90e8e50f | 754 | dai_nr = info->dai_info_nr; |
4b4dab82 KM |
755 | if (!dai_nr) { |
756 | dev_err(dev, "no dai\n"); | |
757 | return -EIO; | |
758 | } | |
1536a968 KM |
759 | |
760 | drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL); | |
761 | rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL); | |
762 | if (!drv || !rdai) { | |
763 | dev_err(dev, "dai allocate failed\n"); | |
764 | return -ENOMEM; | |
765 | } | |
766 | ||
ecba9e72 | 767 | priv->rdai_nr = dai_nr; |
49848073 KM |
768 | priv->daidrv = drv; |
769 | priv->rdai = rdai; | |
770 | ||
1536a968 | 771 | for (i = 0; i < dai_nr; i++) { |
29e69fd2 | 772 | rdai[i].info = &info->dai_info[i]; |
1536a968 | 773 | |
29e69fd2 KM |
774 | pmod = rdai[i].info->playback.ssi; |
775 | cmod = rdai[i].info->capture.ssi; | |
1536a968 KM |
776 | |
777 | /* | |
778 | * init rsnd_dai | |
779 | */ | |
1536a968 KM |
780 | snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i); |
781 | ||
782 | /* | |
783 | * init snd_soc_dai_driver | |
784 | */ | |
785 | drv[i].name = rdai[i].name; | |
786 | drv[i].ops = &rsnd_soc_dai_ops; | |
4b4dab82 | 787 | if (pmod) { |
1536a968 KM |
788 | drv[i].playback.rates = RSND_RATES; |
789 | drv[i].playback.formats = RSND_FMTS; | |
790 | drv[i].playback.channels_min = 2; | |
791 | drv[i].playback.channels_max = 2; | |
389933d9 | 792 | |
29e69fd2 | 793 | rdai[i].playback.info = &info->dai_info[i].playback; |
9bfed6cf | 794 | rsnd_path_init(priv, &rdai[i], &rdai[i].playback); |
1536a968 | 795 | } |
4b4dab82 | 796 | if (cmod) { |
1536a968 KM |
797 | drv[i].capture.rates = RSND_RATES; |
798 | drv[i].capture.formats = RSND_FMTS; | |
799 | drv[i].capture.channels_min = 2; | |
800 | drv[i].capture.channels_max = 2; | |
389933d9 | 801 | |
29e69fd2 | 802 | rdai[i].capture.info = &info->dai_info[i].capture; |
9bfed6cf | 803 | rsnd_path_init(priv, &rdai[i], &rdai[i].capture); |
1536a968 KM |
804 | } |
805 | ||
4b4dab82 KM |
806 | dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name, |
807 | pmod ? "play" : " -- ", | |
808 | cmod ? "capture" : " -- "); | |
1536a968 KM |
809 | } |
810 | ||
1536a968 KM |
811 | return 0; |
812 | } | |
813 | ||
1536a968 KM |
814 | /* |
815 | * pcm ops | |
816 | */ | |
817 | static struct snd_pcm_hardware rsnd_pcm_hardware = { | |
818 | .info = SNDRV_PCM_INFO_INTERLEAVED | | |
819 | SNDRV_PCM_INFO_MMAP | | |
820 | SNDRV_PCM_INFO_MMAP_VALID | | |
821 | SNDRV_PCM_INFO_PAUSE, | |
1536a968 KM |
822 | .buffer_bytes_max = 64 * 1024, |
823 | .period_bytes_min = 32, | |
824 | .period_bytes_max = 8192, | |
825 | .periods_min = 1, | |
826 | .periods_max = 32, | |
827 | .fifo_size = 256, | |
828 | }; | |
829 | ||
830 | static int rsnd_pcm_open(struct snd_pcm_substream *substream) | |
831 | { | |
832 | struct snd_pcm_runtime *runtime = substream->runtime; | |
833 | int ret = 0; | |
834 | ||
835 | snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware); | |
836 | ||
837 | ret = snd_pcm_hw_constraint_integer(runtime, | |
838 | SNDRV_PCM_HW_PARAM_PERIODS); | |
839 | ||
840 | return ret; | |
841 | } | |
842 | ||
843 | static int rsnd_hw_params(struct snd_pcm_substream *substream, | |
844 | struct snd_pcm_hw_params *hw_params) | |
845 | { | |
846 | return snd_pcm_lib_malloc_pages(substream, | |
847 | params_buffer_bytes(hw_params)); | |
848 | } | |
849 | ||
850 | static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream) | |
851 | { | |
852 | struct snd_pcm_runtime *runtime = substream->runtime; | |
853 | struct snd_soc_dai *dai = rsnd_substream_to_dai(substream); | |
854 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); | |
855 | struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); | |
856 | ||
857 | return bytes_to_frames(runtime, io->byte_pos); | |
858 | } | |
859 | ||
860 | static struct snd_pcm_ops rsnd_pcm_ops = { | |
861 | .open = rsnd_pcm_open, | |
862 | .ioctl = snd_pcm_lib_ioctl, | |
863 | .hw_params = rsnd_hw_params, | |
864 | .hw_free = snd_pcm_lib_free_pages, | |
865 | .pointer = rsnd_pointer, | |
866 | }; | |
867 | ||
868 | /* | |
869 | * snd_soc_platform | |
870 | */ | |
871 | ||
872 | #define PREALLOC_BUFFER (32 * 1024) | |
873 | #define PREALLOC_BUFFER_MAX (32 * 1024) | |
874 | ||
875 | static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd) | |
876 | { | |
bff58ea4 KM |
877 | struct rsnd_priv *priv = snd_soc_dai_get_drvdata(rtd->cpu_dai); |
878 | struct rsnd_dai *rdai; | |
879 | int i, ret; | |
880 | ||
881 | for_each_rsnd_dai(rdai, priv, i) { | |
882 | ret = rsnd_dai_call(pcm_new, &rdai->playback, rdai, rtd); | |
883 | if (ret) | |
884 | return ret; | |
885 | ||
886 | ret = rsnd_dai_call(pcm_new, &rdai->capture, rdai, rtd); | |
887 | if (ret) | |
888 | return ret; | |
889 | } | |
890 | ||
1536a968 KM |
891 | return snd_pcm_lib_preallocate_pages_for_all( |
892 | rtd->pcm, | |
893 | SNDRV_DMA_TYPE_DEV, | |
894 | rtd->card->snd_card->dev, | |
895 | PREALLOC_BUFFER, PREALLOC_BUFFER_MAX); | |
896 | } | |
897 | ||
898 | static void rsnd_pcm_free(struct snd_pcm *pcm) | |
899 | { | |
900 | snd_pcm_lib_preallocate_free_for_all(pcm); | |
901 | } | |
902 | ||
903 | static struct snd_soc_platform_driver rsnd_soc_platform = { | |
904 | .ops = &rsnd_pcm_ops, | |
905 | .pcm_new = rsnd_pcm_new, | |
906 | .pcm_free = rsnd_pcm_free, | |
907 | }; | |
908 | ||
909 | static const struct snd_soc_component_driver rsnd_soc_component = { | |
910 | .name = "rsnd", | |
911 | }; | |
912 | ||
913 | /* | |
914 | * rsnd probe | |
915 | */ | |
916 | static int rsnd_probe(struct platform_device *pdev) | |
917 | { | |
918 | struct rcar_snd_info *info; | |
919 | struct rsnd_priv *priv; | |
920 | struct device *dev = &pdev->dev; | |
7681f6ac | 921 | struct rsnd_dai *rdai; |
90e8e50f KM |
922 | const struct of_device_id *of_id = of_match_device(rsnd_of_match, dev); |
923 | const struct rsnd_of_data *of_data; | |
d1ac970f | 924 | int (*probe_func[])(struct platform_device *pdev, |
90e8e50f | 925 | const struct rsnd_of_data *of_data, |
d1ac970f KM |
926 | struct rsnd_priv *priv) = { |
927 | rsnd_gen_probe, | |
928 | rsnd_ssi_probe, | |
ba9c949f | 929 | rsnd_src_probe, |
bff58ea4 | 930 | rsnd_dvc_probe, |
d1ac970f KM |
931 | rsnd_adg_probe, |
932 | rsnd_dai_probe, | |
933 | }; | |
934 | int ret, i; | |
1536a968 | 935 | |
90e8e50f KM |
936 | info = NULL; |
937 | of_data = NULL; | |
938 | if (of_id) { | |
939 | info = devm_kzalloc(&pdev->dev, | |
940 | sizeof(struct rcar_snd_info), GFP_KERNEL); | |
941 | of_data = of_id->data; | |
942 | } else { | |
943 | info = pdev->dev.platform_data; | |
944 | } | |
945 | ||
1536a968 KM |
946 | if (!info) { |
947 | dev_err(dev, "driver needs R-Car sound information\n"); | |
948 | return -ENODEV; | |
949 | } | |
950 | ||
951 | /* | |
952 | * init priv data | |
953 | */ | |
954 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); | |
955 | if (!priv) { | |
956 | dev_err(dev, "priv allocate failed\n"); | |
957 | return -ENODEV; | |
958 | } | |
959 | ||
960 | priv->dev = dev; | |
961 | priv->info = info; | |
962 | spin_lock_init(&priv->lock); | |
963 | ||
964 | /* | |
965 | * init each module | |
966 | */ | |
d1ac970f | 967 | for (i = 0; i < ARRAY_SIZE(probe_func); i++) { |
90e8e50f | 968 | ret = probe_func[i](pdev, of_data, priv); |
d1ac970f KM |
969 | if (ret) |
970 | return ret; | |
971 | } | |
07539c1d | 972 | |
7681f6ac | 973 | for_each_rsnd_dai(rdai, priv, i) { |
68b6af36 | 974 | ret = rsnd_dai_call(probe, &rdai->playback, rdai); |
7681f6ac KM |
975 | if (ret) |
976 | return ret; | |
dfc9403b | 977 | |
68b6af36 | 978 | ret = rsnd_dai_call(probe, &rdai->capture, rdai); |
7681f6ac KM |
979 | if (ret) |
980 | return ret; | |
981 | } | |
4b4dab82 | 982 | |
1536a968 KM |
983 | /* |
984 | * asoc register | |
985 | */ | |
986 | ret = snd_soc_register_platform(dev, &rsnd_soc_platform); | |
987 | if (ret < 0) { | |
988 | dev_err(dev, "cannot snd soc register\n"); | |
989 | return ret; | |
990 | } | |
991 | ||
992 | ret = snd_soc_register_component(dev, &rsnd_soc_component, | |
ecba9e72 | 993 | priv->daidrv, rsnd_rdai_nr(priv)); |
1536a968 KM |
994 | if (ret < 0) { |
995 | dev_err(dev, "cannot snd dai register\n"); | |
996 | goto exit_snd_soc; | |
997 | } | |
998 | ||
999 | dev_set_drvdata(dev, priv); | |
1000 | ||
1001 | pm_runtime_enable(dev); | |
1002 | ||
1003 | dev_info(dev, "probed\n"); | |
1004 | return ret; | |
1005 | ||
1006 | exit_snd_soc: | |
1007 | snd_soc_unregister_platform(dev); | |
1008 | ||
1009 | return ret; | |
1010 | } | |
1011 | ||
1012 | static int rsnd_remove(struct platform_device *pdev) | |
1013 | { | |
1014 | struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev); | |
7681f6ac KM |
1015 | struct rsnd_dai *rdai; |
1016 | int ret, i; | |
1536a968 KM |
1017 | |
1018 | pm_runtime_disable(&pdev->dev); | |
1019 | ||
7681f6ac | 1020 | for_each_rsnd_dai(rdai, priv, i) { |
68b6af36 | 1021 | ret = rsnd_dai_call(remove, &rdai->playback, rdai); |
7681f6ac KM |
1022 | if (ret) |
1023 | return ret; | |
1024 | ||
68b6af36 | 1025 | ret = rsnd_dai_call(remove, &rdai->capture, rdai); |
7681f6ac KM |
1026 | if (ret) |
1027 | return ret; | |
1028 | } | |
1536a968 KM |
1029 | |
1030 | return 0; | |
1031 | } | |
1032 | ||
1033 | static struct platform_driver rsnd_driver = { | |
1034 | .driver = { | |
1035 | .name = "rcar_sound", | |
90e8e50f | 1036 | .of_match_table = rsnd_of_match, |
1536a968 KM |
1037 | }, |
1038 | .probe = rsnd_probe, | |
1039 | .remove = rsnd_remove, | |
1040 | }; | |
1041 | module_platform_driver(rsnd_driver); | |
1042 | ||
1043 | MODULE_LICENSE("GPL"); | |
1044 | MODULE_DESCRIPTION("Renesas R-Car audio driver"); | |
1045 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); | |
1046 | MODULE_ALIAS("platform:rcar-pcm-audio"); |