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 | { | |
282 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); | |
283 | struct device *dev = rsnd_priv_to_dev(priv); | |
284 | ||
285 | if (!mod) { | |
286 | dev_err(dev, "NULL mod\n"); | |
287 | return -EIO; | |
288 | } | |
289 | ||
290 | if (!list_empty(&mod->list)) { | |
291 | dev_err(dev, "%s%d is not empty\n", | |
292 | rsnd_mod_name(mod), | |
293 | rsnd_mod_id(mod)); | |
294 | return -EIO; | |
295 | } | |
296 | ||
297 | list_add_tail(&mod->list, &io->head); | |
298 | ||
299 | return 0; | |
300 | } | |
301 | ||
302 | int rsnd_dai_disconnect(struct rsnd_mod *mod) | |
303 | { | |
304 | list_del_init(&mod->list); | |
305 | ||
306 | return 0; | |
307 | } | |
308 | ||
4b4dab82 KM |
309 | int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai) |
310 | { | |
311 | int id = rdai - priv->rdai; | |
312 | ||
313 | if ((id < 0) || (id >= rsnd_dai_nr(priv))) | |
314 | return -EINVAL; | |
315 | ||
316 | return id; | |
317 | } | |
318 | ||
1536a968 KM |
319 | struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id) |
320 | { | |
2192f81c KM |
321 | if ((id < 0) || (id >= rsnd_dai_nr(priv))) |
322 | return NULL; | |
323 | ||
1536a968 KM |
324 | return priv->rdai + id; |
325 | } | |
326 | ||
327 | static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai) | |
328 | { | |
329 | struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai); | |
330 | ||
331 | return rsnd_dai_get(priv, dai->id); | |
332 | } | |
333 | ||
334 | int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io) | |
335 | { | |
336 | return &rdai->playback == io; | |
337 | } | |
338 | ||
339 | /* | |
340 | * rsnd_soc_dai functions | |
341 | */ | |
342 | int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional) | |
343 | { | |
344 | struct snd_pcm_substream *substream = io->substream; | |
345 | struct snd_pcm_runtime *runtime = substream->runtime; | |
346 | int pos = io->byte_pos + additional; | |
347 | ||
348 | pos %= (runtime->periods * io->byte_per_period); | |
349 | ||
350 | return pos; | |
351 | } | |
352 | ||
353 | void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte) | |
354 | { | |
355 | io->byte_pos += byte; | |
356 | ||
357 | if (io->byte_pos >= io->next_period_byte) { | |
358 | struct snd_pcm_substream *substream = io->substream; | |
359 | struct snd_pcm_runtime *runtime = substream->runtime; | |
360 | ||
361 | io->period_pos++; | |
362 | io->next_period_byte += io->byte_per_period; | |
363 | ||
364 | if (io->period_pos >= runtime->periods) { | |
365 | io->byte_pos = 0; | |
366 | io->period_pos = 0; | |
367 | io->next_period_byte = io->byte_per_period; | |
368 | } | |
369 | ||
370 | snd_pcm_period_elapsed(substream); | |
371 | } | |
372 | } | |
373 | ||
374 | static int rsnd_dai_stream_init(struct rsnd_dai_stream *io, | |
375 | struct snd_pcm_substream *substream) | |
376 | { | |
377 | struct snd_pcm_runtime *runtime = substream->runtime; | |
378 | ||
379 | if (!list_empty(&io->head)) | |
380 | return -EIO; | |
381 | ||
382 | INIT_LIST_HEAD(&io->head); | |
383 | io->substream = substream; | |
384 | io->byte_pos = 0; | |
385 | io->period_pos = 0; | |
386 | io->byte_per_period = runtime->period_size * | |
387 | runtime->channels * | |
388 | samples_to_bytes(runtime, 1); | |
389 | io->next_period_byte = io->byte_per_period; | |
390 | ||
391 | return 0; | |
392 | } | |
393 | ||
394 | static | |
395 | struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream) | |
396 | { | |
397 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
398 | ||
399 | return rtd->cpu_dai; | |
400 | } | |
401 | ||
402 | static | |
403 | struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai, | |
404 | struct snd_pcm_substream *substream) | |
405 | { | |
406 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) | |
407 | return &rdai->playback; | |
408 | else | |
409 | return &rdai->capture; | |
410 | } | |
411 | ||
412 | static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd, | |
413 | struct snd_soc_dai *dai) | |
414 | { | |
415 | struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai); | |
416 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); | |
417 | struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); | |
4b4dab82 KM |
418 | struct rsnd_mod *mod = rsnd_ssi_mod_get_frm_dai(priv, |
419 | rsnd_dai_id(priv, rdai), | |
420 | rsnd_dai_is_play(rdai, io)); | |
421 | int ssi_id = rsnd_mod_id(mod); | |
1536a968 KM |
422 | int ret; |
423 | unsigned long flags; | |
424 | ||
425 | rsnd_lock(priv, flags); | |
426 | ||
427 | switch (cmd) { | |
428 | case SNDRV_PCM_TRIGGER_START: | |
429 | ret = rsnd_dai_stream_init(io, substream); | |
430 | if (ret < 0) | |
431 | goto dai_trigger_end; | |
432 | ||
433 | ret = rsnd_platform_call(priv, dai, start, ssi_id); | |
434 | if (ret < 0) | |
435 | goto dai_trigger_end; | |
436 | ||
3337744a KM |
437 | ret = rsnd_gen_path_init(priv, rdai, io); |
438 | if (ret < 0) | |
439 | goto dai_trigger_end; | |
440 | ||
cdaa3cdf KM |
441 | ret = rsnd_dai_call(rdai, io, init); |
442 | if (ret < 0) | |
443 | goto dai_trigger_end; | |
444 | ||
445 | ret = rsnd_dai_call(rdai, io, start); | |
446 | if (ret < 0) | |
447 | goto dai_trigger_end; | |
1536a968 KM |
448 | break; |
449 | case SNDRV_PCM_TRIGGER_STOP: | |
cdaa3cdf KM |
450 | ret = rsnd_dai_call(rdai, io, stop); |
451 | if (ret < 0) | |
452 | goto dai_trigger_end; | |
453 | ||
454 | ret = rsnd_dai_call(rdai, io, quit); | |
455 | if (ret < 0) | |
456 | goto dai_trigger_end; | |
457 | ||
3337744a | 458 | ret = rsnd_gen_path_exit(priv, rdai, io); |
1536a968 KM |
459 | if (ret < 0) |
460 | goto dai_trigger_end; | |
461 | ||
3337744a KM |
462 | ret = rsnd_platform_call(priv, dai, stop, ssi_id); |
463 | if (ret < 0) | |
464 | goto dai_trigger_end; | |
1536a968 KM |
465 | break; |
466 | default: | |
467 | ret = -EINVAL; | |
468 | } | |
469 | ||
470 | dai_trigger_end: | |
471 | rsnd_unlock(priv, flags); | |
472 | ||
473 | return ret; | |
474 | } | |
475 | ||
476 | static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) | |
477 | { | |
478 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); | |
479 | ||
480 | /* set master/slave audio interface */ | |
481 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { | |
482 | case SND_SOC_DAIFMT_CBM_CFM: | |
483 | rdai->clk_master = 1; | |
484 | break; | |
485 | case SND_SOC_DAIFMT_CBS_CFS: | |
486 | rdai->clk_master = 0; | |
487 | break; | |
488 | default: | |
489 | return -EINVAL; | |
490 | } | |
491 | ||
492 | /* set clock inversion */ | |
493 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { | |
494 | case SND_SOC_DAIFMT_NB_IF: | |
495 | rdai->bit_clk_inv = 0; | |
496 | rdai->frm_clk_inv = 1; | |
497 | break; | |
498 | case SND_SOC_DAIFMT_IB_NF: | |
499 | rdai->bit_clk_inv = 1; | |
500 | rdai->frm_clk_inv = 0; | |
501 | break; | |
502 | case SND_SOC_DAIFMT_IB_IF: | |
503 | rdai->bit_clk_inv = 1; | |
504 | rdai->frm_clk_inv = 1; | |
505 | break; | |
506 | case SND_SOC_DAIFMT_NB_NF: | |
507 | default: | |
508 | rdai->bit_clk_inv = 0; | |
509 | rdai->frm_clk_inv = 0; | |
510 | break; | |
511 | } | |
512 | ||
513 | /* set format */ | |
514 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { | |
515 | case SND_SOC_DAIFMT_I2S: | |
516 | rdai->sys_delay = 0; | |
517 | rdai->data_alignment = 0; | |
518 | break; | |
519 | case SND_SOC_DAIFMT_LEFT_J: | |
520 | rdai->sys_delay = 1; | |
521 | rdai->data_alignment = 0; | |
522 | break; | |
523 | case SND_SOC_DAIFMT_RIGHT_J: | |
524 | rdai->sys_delay = 1; | |
525 | rdai->data_alignment = 1; | |
526 | break; | |
527 | } | |
528 | ||
529 | return 0; | |
530 | } | |
531 | ||
532 | static const struct snd_soc_dai_ops rsnd_soc_dai_ops = { | |
533 | .trigger = rsnd_soc_dai_trigger, | |
534 | .set_fmt = rsnd_soc_dai_set_fmt, | |
535 | }; | |
536 | ||
537 | static int rsnd_dai_probe(struct platform_device *pdev, | |
538 | struct rcar_snd_info *info, | |
539 | struct rsnd_priv *priv) | |
540 | { | |
541 | struct snd_soc_dai_driver *drv; | |
542 | struct rsnd_dai *rdai; | |
4b4dab82 | 543 | struct rsnd_mod *pmod, *cmod; |
1536a968 | 544 | struct device *dev = rsnd_priv_to_dev(priv); |
4b4dab82 KM |
545 | int dai_nr; |
546 | int i; | |
547 | ||
548 | /* get max dai nr */ | |
549 | for (dai_nr = 0; dai_nr < 32; dai_nr++) { | |
550 | pmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 1); | |
551 | cmod = rsnd_ssi_mod_get_frm_dai(priv, dai_nr, 0); | |
552 | ||
553 | if (!pmod && !cmod) | |
554 | break; | |
555 | } | |
556 | ||
557 | if (!dai_nr) { | |
558 | dev_err(dev, "no dai\n"); | |
559 | return -EIO; | |
560 | } | |
1536a968 KM |
561 | |
562 | drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL); | |
563 | rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL); | |
564 | if (!drv || !rdai) { | |
565 | dev_err(dev, "dai allocate failed\n"); | |
566 | return -ENOMEM; | |
567 | } | |
568 | ||
569 | for (i = 0; i < dai_nr; i++) { | |
1536a968 | 570 | |
4b4dab82 KM |
571 | pmod = rsnd_ssi_mod_get_frm_dai(priv, i, 1); |
572 | cmod = rsnd_ssi_mod_get_frm_dai(priv, i, 0); | |
1536a968 KM |
573 | |
574 | /* | |
575 | * init rsnd_dai | |
576 | */ | |
577 | INIT_LIST_HEAD(&rdai[i].playback.head); | |
578 | INIT_LIST_HEAD(&rdai[i].capture.head); | |
579 | ||
1536a968 KM |
580 | snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i); |
581 | ||
582 | /* | |
583 | * init snd_soc_dai_driver | |
584 | */ | |
585 | drv[i].name = rdai[i].name; | |
586 | drv[i].ops = &rsnd_soc_dai_ops; | |
4b4dab82 | 587 | if (pmod) { |
1536a968 KM |
588 | drv[i].playback.rates = RSND_RATES; |
589 | drv[i].playback.formats = RSND_FMTS; | |
590 | drv[i].playback.channels_min = 2; | |
591 | drv[i].playback.channels_max = 2; | |
592 | } | |
4b4dab82 | 593 | if (cmod) { |
1536a968 KM |
594 | drv[i].capture.rates = RSND_RATES; |
595 | drv[i].capture.formats = RSND_FMTS; | |
596 | drv[i].capture.channels_min = 2; | |
597 | drv[i].capture.channels_max = 2; | |
598 | } | |
599 | ||
4b4dab82 KM |
600 | dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name, |
601 | pmod ? "play" : " -- ", | |
602 | cmod ? "capture" : " -- "); | |
1536a968 KM |
603 | } |
604 | ||
605 | priv->dai_nr = dai_nr; | |
606 | priv->daidrv = drv; | |
607 | priv->rdai = rdai; | |
608 | ||
609 | return 0; | |
610 | } | |
611 | ||
612 | static void rsnd_dai_remove(struct platform_device *pdev, | |
613 | struct rsnd_priv *priv) | |
614 | { | |
615 | } | |
616 | ||
617 | /* | |
618 | * pcm ops | |
619 | */ | |
620 | static struct snd_pcm_hardware rsnd_pcm_hardware = { | |
621 | .info = SNDRV_PCM_INFO_INTERLEAVED | | |
622 | SNDRV_PCM_INFO_MMAP | | |
623 | SNDRV_PCM_INFO_MMAP_VALID | | |
624 | SNDRV_PCM_INFO_PAUSE, | |
625 | .formats = RSND_FMTS, | |
626 | .rates = RSND_RATES, | |
627 | .rate_min = 8000, | |
628 | .rate_max = 192000, | |
629 | .channels_min = 2, | |
630 | .channels_max = 2, | |
631 | .buffer_bytes_max = 64 * 1024, | |
632 | .period_bytes_min = 32, | |
633 | .period_bytes_max = 8192, | |
634 | .periods_min = 1, | |
635 | .periods_max = 32, | |
636 | .fifo_size = 256, | |
637 | }; | |
638 | ||
639 | static int rsnd_pcm_open(struct snd_pcm_substream *substream) | |
640 | { | |
641 | struct snd_pcm_runtime *runtime = substream->runtime; | |
642 | int ret = 0; | |
643 | ||
644 | snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware); | |
645 | ||
646 | ret = snd_pcm_hw_constraint_integer(runtime, | |
647 | SNDRV_PCM_HW_PARAM_PERIODS); | |
648 | ||
649 | return ret; | |
650 | } | |
651 | ||
652 | static int rsnd_hw_params(struct snd_pcm_substream *substream, | |
653 | struct snd_pcm_hw_params *hw_params) | |
654 | { | |
655 | return snd_pcm_lib_malloc_pages(substream, | |
656 | params_buffer_bytes(hw_params)); | |
657 | } | |
658 | ||
659 | static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream) | |
660 | { | |
661 | struct snd_pcm_runtime *runtime = substream->runtime; | |
662 | struct snd_soc_dai *dai = rsnd_substream_to_dai(substream); | |
663 | struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai); | |
664 | struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream); | |
665 | ||
666 | return bytes_to_frames(runtime, io->byte_pos); | |
667 | } | |
668 | ||
669 | static struct snd_pcm_ops rsnd_pcm_ops = { | |
670 | .open = rsnd_pcm_open, | |
671 | .ioctl = snd_pcm_lib_ioctl, | |
672 | .hw_params = rsnd_hw_params, | |
673 | .hw_free = snd_pcm_lib_free_pages, | |
674 | .pointer = rsnd_pointer, | |
675 | }; | |
676 | ||
677 | /* | |
678 | * snd_soc_platform | |
679 | */ | |
680 | ||
681 | #define PREALLOC_BUFFER (32 * 1024) | |
682 | #define PREALLOC_BUFFER_MAX (32 * 1024) | |
683 | ||
684 | static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd) | |
685 | { | |
686 | return snd_pcm_lib_preallocate_pages_for_all( | |
687 | rtd->pcm, | |
688 | SNDRV_DMA_TYPE_DEV, | |
689 | rtd->card->snd_card->dev, | |
690 | PREALLOC_BUFFER, PREALLOC_BUFFER_MAX); | |
691 | } | |
692 | ||
693 | static void rsnd_pcm_free(struct snd_pcm *pcm) | |
694 | { | |
695 | snd_pcm_lib_preallocate_free_for_all(pcm); | |
696 | } | |
697 | ||
698 | static struct snd_soc_platform_driver rsnd_soc_platform = { | |
699 | .ops = &rsnd_pcm_ops, | |
700 | .pcm_new = rsnd_pcm_new, | |
701 | .pcm_free = rsnd_pcm_free, | |
702 | }; | |
703 | ||
704 | static const struct snd_soc_component_driver rsnd_soc_component = { | |
705 | .name = "rsnd", | |
706 | }; | |
707 | ||
708 | /* | |
709 | * rsnd probe | |
710 | */ | |
711 | static int rsnd_probe(struct platform_device *pdev) | |
712 | { | |
713 | struct rcar_snd_info *info; | |
714 | struct rsnd_priv *priv; | |
715 | struct device *dev = &pdev->dev; | |
716 | int ret; | |
717 | ||
718 | info = pdev->dev.platform_data; | |
719 | if (!info) { | |
720 | dev_err(dev, "driver needs R-Car sound information\n"); | |
721 | return -ENODEV; | |
722 | } | |
723 | ||
724 | /* | |
725 | * init priv data | |
726 | */ | |
727 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); | |
728 | if (!priv) { | |
729 | dev_err(dev, "priv allocate failed\n"); | |
730 | return -ENODEV; | |
731 | } | |
732 | ||
733 | priv->dev = dev; | |
734 | priv->info = info; | |
735 | spin_lock_init(&priv->lock); | |
736 | ||
737 | /* | |
738 | * init each module | |
739 | */ | |
3337744a KM |
740 | ret = rsnd_gen_probe(pdev, info, priv); |
741 | if (ret < 0) | |
742 | return ret; | |
743 | ||
07539c1d KM |
744 | ret = rsnd_scu_probe(pdev, info, priv); |
745 | if (ret < 0) | |
746 | return ret; | |
747 | ||
dfc9403b KM |
748 | ret = rsnd_adg_probe(pdev, info, priv); |
749 | if (ret < 0) | |
750 | return ret; | |
751 | ||
ae5c3223 KM |
752 | ret = rsnd_ssi_probe(pdev, info, priv); |
753 | if (ret < 0) | |
754 | return ret; | |
755 | ||
4b4dab82 KM |
756 | ret = rsnd_dai_probe(pdev, info, priv); |
757 | if (ret < 0) | |
758 | return ret; | |
759 | ||
1536a968 KM |
760 | /* |
761 | * asoc register | |
762 | */ | |
763 | ret = snd_soc_register_platform(dev, &rsnd_soc_platform); | |
764 | if (ret < 0) { | |
765 | dev_err(dev, "cannot snd soc register\n"); | |
766 | return ret; | |
767 | } | |
768 | ||
769 | ret = snd_soc_register_component(dev, &rsnd_soc_component, | |
770 | priv->daidrv, rsnd_dai_nr(priv)); | |
771 | if (ret < 0) { | |
772 | dev_err(dev, "cannot snd dai register\n"); | |
773 | goto exit_snd_soc; | |
774 | } | |
775 | ||
776 | dev_set_drvdata(dev, priv); | |
777 | ||
778 | pm_runtime_enable(dev); | |
779 | ||
780 | dev_info(dev, "probed\n"); | |
781 | return ret; | |
782 | ||
783 | exit_snd_soc: | |
784 | snd_soc_unregister_platform(dev); | |
785 | ||
786 | return ret; | |
787 | } | |
788 | ||
789 | static int rsnd_remove(struct platform_device *pdev) | |
790 | { | |
791 | struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev); | |
792 | ||
793 | pm_runtime_disable(&pdev->dev); | |
794 | ||
795 | /* | |
796 | * remove each module | |
797 | */ | |
ae5c3223 | 798 | rsnd_ssi_remove(pdev, priv); |
dfc9403b | 799 | rsnd_adg_remove(pdev, priv); |
07539c1d | 800 | rsnd_scu_remove(pdev, priv); |
1536a968 | 801 | rsnd_dai_remove(pdev, priv); |
3337744a | 802 | rsnd_gen_remove(pdev, priv); |
1536a968 KM |
803 | |
804 | return 0; | |
805 | } | |
806 | ||
807 | static struct platform_driver rsnd_driver = { | |
808 | .driver = { | |
809 | .name = "rcar_sound", | |
810 | }, | |
811 | .probe = rsnd_probe, | |
812 | .remove = rsnd_remove, | |
813 | }; | |
814 | module_platform_driver(rsnd_driver); | |
815 | ||
816 | MODULE_LICENSE("GPL"); | |
817 | MODULE_DESCRIPTION("Renesas R-Car audio driver"); | |
818 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); | |
819 | MODULE_ALIAS("platform:rcar-pcm-audio"); |