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