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