Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 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> |
Kuninori Morimoto | 9ade09d | 2013-10-29 00:52:19 -0700 | [diff] [blame] | 97 | #include <linux/shdma-base.h> |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 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...) \ |
Kuninori Morimoto | 740ad6c | 2013-10-11 00:06:34 -0700 | [diff] [blame] | 107 | (!(priv->info->func) ? 0 : \ |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 108 | priv->info->func(param)) |
| 109 | |
Kuninori Morimoto | 389933d | 2014-03-03 20:50:00 -0800 | [diff] [blame] | 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 | |
Kuninori Morimoto | 3337744 | 2013-07-21 21:36:21 -0700 | [diff] [blame] | 115 | /* |
Kuninori Morimoto | cdaa3cd | 2013-07-21 21:36:03 -0700 | [diff] [blame] | 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, |
Kuninori Morimoto | a126021 | 2014-03-02 23:42:55 -0800 | [diff] [blame] | 129 | enum rsnd_mod_type type, |
Kuninori Morimoto | cdaa3cd | 2013-07-21 21:36:03 -0700 | [diff] [blame] | 130 | int id) |
| 131 | { |
| 132 | mod->priv = priv; |
| 133 | mod->id = id; |
| 134 | mod->ops = ops; |
Kuninori Morimoto | a126021 | 2014-03-02 23:42:55 -0800 | [diff] [blame] | 135 | mod->type = type; |
Kuninori Morimoto | cdaa3cd | 2013-07-21 21:36:03 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 139 | * rsnd_dma functions |
| 140 | */ |
Kuninori Morimoto | f5cab3b | 2014-01-23 18:40:27 -0800 | [diff] [blame] | 141 | static void __rsnd_dma_start(struct rsnd_dma *dma); |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 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*/ |
Kuninori Morimoto | 4686a0a | 2014-01-23 18:41:44 -0800 | [diff] [blame] | 152 | dma->offset = 0; |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 153 | dma->submit_loop = 2; |
Kuninori Morimoto | f5cab3b | 2014-01-23 18:40:27 -0800 | [diff] [blame] | 154 | __rsnd_dma_start(dma); |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 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; |
Kuninori Morimoto | 4686a0a | 2014-01-23 18:41:44 -0800 | [diff] [blame] | 167 | struct rsnd_mod *mod = rsnd_dma_to_mod(dma); |
Kuninori Morimoto | 9b5ab57 | 2014-01-23 18:40:03 -0800 | [diff] [blame] | 168 | struct rsnd_priv *priv = rsnd_mod_to_priv(rsnd_dma_to_mod(dma)); |
Kuninori Morimoto | 4686a0a | 2014-01-23 18:41:44 -0800 | [diff] [blame] | 169 | struct rsnd_dai_stream *io = rsnd_mod_to_io(mod); |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 170 | unsigned long flags; |
| 171 | |
| 172 | rsnd_lock(priv, flags); |
| 173 | |
Kuninori Morimoto | 4686a0a | 2014-01-23 18:41:44 -0800 | [diff] [blame] | 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); |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 187 | |
| 188 | if (dma->submit_loop) |
| 189 | rsnd_dma_continue(dma); |
| 190 | |
| 191 | rsnd_unlock(priv, flags); |
| 192 | } |
| 193 | |
Kuninori Morimoto | f5cab3b | 2014-01-23 18:40:27 -0800 | [diff] [blame] | 194 | static void __rsnd_dma_start(struct rsnd_dma *dma) |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 195 | { |
Kuninori Morimoto | 4686a0a | 2014-01-23 18:41:44 -0800 | [diff] [blame] | 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); |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 200 | struct device *dev = rsnd_priv_to_dev(priv); |
| 201 | struct dma_async_tx_descriptor *desc; |
| 202 | dma_addr_t buf; |
Kuninori Morimoto | 4686a0a | 2014-01-23 18:41:44 -0800 | [diff] [blame] | 203 | size_t len = io->byte_per_period; |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 204 | int i; |
| 205 | |
| 206 | for (i = 0; i < dma->submit_loop; i++) { |
| 207 | |
Kuninori Morimoto | 4686a0a | 2014-01-23 18:41:44 -0800 | [diff] [blame] | 208 | buf = runtime->dma_addr + |
| 209 | rsnd_dai_pointer_offset(io, dma->offset + len); |
| 210 | dma->offset = len; |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 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 | |
Kuninori Morimoto | a0d32bc | 2013-11-17 18:45:16 -0800 | [diff] [blame] | 228 | dma_async_issue_pending(dma->chan); |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 229 | } |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Kuninori Morimoto | f5cab3b | 2014-01-23 18:40:27 -0800 | [diff] [blame] | 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 | |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 239 | int rsnd_dma_available(struct rsnd_dma *dma) |
| 240 | { |
| 241 | return !!dma->chan; |
| 242 | } |
| 243 | |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 244 | int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma, |
Kuninori Morimoto | 4686a0a | 2014-01-23 18:41:44 -0800 | [diff] [blame] | 245 | int is_play, int id) |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 246 | { |
| 247 | struct device *dev = rsnd_priv_to_dev(priv); |
Kuninori Morimoto | 9ade09d | 2013-10-29 00:52:19 -0700 | [diff] [blame] | 248 | struct dma_slave_config cfg; |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 249 | dma_cap_mask_t mask; |
Kuninori Morimoto | 9ade09d | 2013-10-29 00:52:19 -0700 | [diff] [blame] | 250 | int ret; |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 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 | |
Kuninori Morimoto | 9ade09d | 2013-10-29 00:52:19 -0700 | [diff] [blame] | 260 | dma->chan = dma_request_slave_channel_compat(mask, shdma_chan_filter, |
| 261 | (void *)id, dev, |
| 262 | is_play ? "tx" : "rx"); |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 263 | if (!dma->chan) { |
| 264 | dev_err(dev, "can't get dma channel\n"); |
| 265 | return -EIO; |
| 266 | } |
| 267 | |
Kuninori Morimoto | 9ade09d | 2013-10-29 00:52:19 -0700 | [diff] [blame] | 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 | |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 277 | dma->dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE; |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 278 | INIT_WORK(&dma->work, rsnd_dma_do_work); |
| 279 | |
| 280 | return 0; |
Kuninori Morimoto | 9ade09d | 2013-10-29 00:52:19 -0700 | [diff] [blame] | 281 | |
| 282 | rsnd_dma_init_err: |
| 283 | rsnd_dma_quit(priv, dma); |
| 284 | |
| 285 | return ret; |
Kuninori Morimoto | 0a4d94c | 2013-07-28 18:58:50 -0700 | [diff] [blame] | 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 | /* |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 298 | * rsnd_dai functions |
| 299 | */ |
Kuninori Morimoto | d870a91e | 2014-02-24 22:14:41 -0800 | [diff] [blame] | 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 | ({ \ |
Kuninori Morimoto | a126021 | 2014-03-02 23:42:55 -0800 | [diff] [blame] | 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; \ |
Kuninori Morimoto | d870a91e | 2014-02-24 22:14:41 -0800 | [diff] [blame] | 322 | ret = rsnd_mod_call(mod, fn, (rdai), (io)); \ |
| 323 | if (ret < 0) \ |
| 324 | break; \ |
| 325 | } \ |
| 326 | ret; \ |
Kuninori Morimoto | cdaa3cd | 2013-07-21 21:36:03 -0700 | [diff] [blame] | 327 | }) |
| 328 | |
Kuninori Morimoto | a126021 | 2014-03-02 23:42:55 -0800 | [diff] [blame] | 329 | static int rsnd_dai_connect(struct rsnd_mod *mod, |
Kuninori Morimoto | 9bfed6c | 2014-03-02 23:42:47 -0800 | [diff] [blame] | 330 | struct rsnd_dai_stream *io) |
Kuninori Morimoto | cdaa3cd | 2013-07-21 21:36:03 -0700 | [diff] [blame] | 331 | { |
Kuninori Morimoto | 6020779 | 2013-11-10 17:00:42 -0800 | [diff] [blame] | 332 | if (!mod) |
Kuninori Morimoto | cdaa3cd | 2013-07-21 21:36:03 -0700 | [diff] [blame] | 333 | return -EIO; |
Kuninori Morimoto | cdaa3cd | 2013-07-21 21:36:03 -0700 | [diff] [blame] | 334 | |
Kuninori Morimoto | a126021 | 2014-03-02 23:42:55 -0800 | [diff] [blame] | 335 | if (io->mod[mod->type]) { |
Kuninori Morimoto | 6020779 | 2013-11-10 17:00:42 -0800 | [diff] [blame] | 336 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
| 337 | struct device *dev = rsnd_priv_to_dev(priv); |
| 338 | |
Kuninori Morimoto | cdaa3cd | 2013-07-21 21:36:03 -0700 | [diff] [blame] | 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 | |
Kuninori Morimoto | a126021 | 2014-03-02 23:42:55 -0800 | [diff] [blame] | 345 | io->mod[mod->type] = mod; |
Kuninori Morimoto | 4686a0a | 2014-01-23 18:41:44 -0800 | [diff] [blame] | 346 | mod->io = io; |
Kuninori Morimoto | cdaa3cd | 2013-07-21 21:36:03 -0700 | [diff] [blame] | 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
Kuninori Morimoto | a126021 | 2014-03-02 23:42:55 -0800 | [diff] [blame] | 351 | static int rsnd_dai_disconnect(struct rsnd_mod *mod, struct rsnd_dai_stream *io) |
Kuninori Morimoto | cdaa3cd | 2013-07-21 21:36:03 -0700 | [diff] [blame] | 352 | { |
Kuninori Morimoto | a126021 | 2014-03-02 23:42:55 -0800 | [diff] [blame] | 353 | io->mod[mod->type] = NULL; |
Kuninori Morimoto | 4686a0a | 2014-01-23 18:41:44 -0800 | [diff] [blame] | 354 | mod->io = NULL; |
Kuninori Morimoto | cdaa3cd | 2013-07-21 21:36:03 -0700 | [diff] [blame] | 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
Kuninori Morimoto | 4b4dab8 | 2013-07-28 18:58:29 -0700 | [diff] [blame] | 359 | int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai) |
| 360 | { |
| 361 | int id = rdai - priv->rdai; |
| 362 | |
Kuninori Morimoto | ecba9e7 | 2014-02-24 22:15:18 -0800 | [diff] [blame] | 363 | if ((id < 0) || (id >= rsnd_rdai_nr(priv))) |
Kuninori Morimoto | 4b4dab8 | 2013-07-28 18:58:29 -0700 | [diff] [blame] | 364 | return -EINVAL; |
| 365 | |
| 366 | return id; |
| 367 | } |
| 368 | |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 369 | struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id) |
| 370 | { |
Kuninori Morimoto | ecba9e7 | 2014-02-24 22:15:18 -0800 | [diff] [blame] | 371 | if ((id < 0) || (id >= rsnd_rdai_nr(priv))) |
Kuninori Morimoto | 2192f81 | 2013-10-11 00:07:48 -0700 | [diff] [blame] | 372 | return NULL; |
| 373 | |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 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 | |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 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); |
Kuninori Morimoto | 4b4dab8 | 2013-07-28 18:58:29 -0700 | [diff] [blame] | 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); |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 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 | |
Kuninori Morimoto | cdaa3cd | 2013-07-21 21:36:03 -0700 | [diff] [blame] | 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; |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 490 | break; |
| 491 | case SNDRV_PCM_TRIGGER_STOP: |
Kuninori Morimoto | cdaa3cd | 2013-07-21 21:36:03 -0700 | [diff] [blame] | 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 | |
Kuninori Morimoto | 3337744 | 2013-07-21 21:36:21 -0700 | [diff] [blame] | 500 | ret = rsnd_platform_call(priv, dai, stop, ssi_id); |
| 501 | if (ret < 0) |
| 502 | goto dai_trigger_end; |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 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 | |
Kuninori Morimoto | 9bfed6c | 2014-03-02 23:42:47 -0800 | [diff] [blame] | 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; |
Kuninori Morimoto | 389933d | 2014-03-03 20:50:00 -0800 | [diff] [blame] | 580 | struct rsnd_dai_platform_info *dai_info = rdai->info; |
Kuninori Morimoto | 9bfed6c | 2014-03-02 23:42:47 -0800 | [diff] [blame] | 581 | int ret; |
Kuninori Morimoto | 389933d | 2014-03-03 20:50:00 -0800 | [diff] [blame] | 582 | int ssi_id = -1; |
| 583 | int scu_id = -1; |
Kuninori Morimoto | 9bfed6c | 2014-03-02 23:42:47 -0800 | [diff] [blame] | 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. |
Kuninori Morimoto | 9bfed6c | 2014-03-02 23:42:47 -0800 | [diff] [blame] | 594 | */ |
Kuninori Morimoto | 389933d | 2014-03-03 20:50:00 -0800 | [diff] [blame] | 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 | |
Kuninori Morimoto | 9bfed6c | 2014-03-02 23:42:47 -0800 | [diff] [blame] | 610 | ret = 0; |
| 611 | |
| 612 | /* SCU */ |
Kuninori Morimoto | 389933d | 2014-03-03 20:50:00 -0800 | [diff] [blame] | 613 | if (scu_id >= 0) { |
| 614 | mod = rsnd_scu_mod_get(priv, scu_id); |
Kuninori Morimoto | a126021 | 2014-03-02 23:42:55 -0800 | [diff] [blame] | 615 | ret = rsnd_dai_connect(mod, io); |
Kuninori Morimoto | 9bfed6c | 2014-03-02 23:42:47 -0800 | [diff] [blame] | 616 | if (ret < 0) |
| 617 | return ret; |
| 618 | } |
| 619 | |
| 620 | /* SSI */ |
Kuninori Morimoto | 389933d | 2014-03-03 20:50:00 -0800 | [diff] [blame] | 621 | if (ssi_id >= 0) { |
| 622 | mod = rsnd_ssi_mod_get(priv, ssi_id); |
Kuninori Morimoto | a126021 | 2014-03-02 23:42:55 -0800 | [diff] [blame] | 623 | ret = rsnd_dai_connect(mod, io); |
Kuninori Morimoto | 9bfed6c | 2014-03-02 23:42:47 -0800 | [diff] [blame] | 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 | { |
Kuninori Morimoto | a126021 | 2014-03-02 23:42:55 -0800 | [diff] [blame] | 635 | struct rsnd_mod *mod; |
| 636 | int ret = 0, i; |
Kuninori Morimoto | 9bfed6c | 2014-03-02 23:42:47 -0800 | [diff] [blame] | 637 | |
| 638 | /* |
| 639 | * remove all mod from rdai |
| 640 | */ |
Kuninori Morimoto | a126021 | 2014-03-02 23:42:55 -0800 | [diff] [blame] | 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 | } |
Kuninori Morimoto | 9bfed6c | 2014-03-02 23:42:47 -0800 | [diff] [blame] | 647 | |
| 648 | return ret; |
| 649 | } |
| 650 | |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 651 | static int rsnd_dai_probe(struct platform_device *pdev, |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 652 | struct rsnd_priv *priv) |
| 653 | { |
| 654 | struct snd_soc_dai_driver *drv; |
Kuninori Morimoto | 78f13d0 | 2014-03-03 20:49:50 -0800 | [diff] [blame] | 655 | struct rcar_snd_info *info = rsnd_priv_to_info(priv); |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 656 | struct rsnd_dai *rdai; |
Kuninori Morimoto | 4b4dab8 | 2013-07-28 18:58:29 -0700 | [diff] [blame] | 657 | struct rsnd_mod *pmod, *cmod; |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 658 | struct device *dev = rsnd_priv_to_dev(priv); |
Kuninori Morimoto | 78f13d0 | 2014-03-03 20:49:50 -0800 | [diff] [blame] | 659 | int dai_nr = info->dai_info_nr; |
Kuninori Morimoto | 4b4dab8 | 2013-07-28 18:58:29 -0700 | [diff] [blame] | 660 | int i; |
| 661 | |
Kuninori Morimoto | 78f13d0 | 2014-03-03 20:49:50 -0800 | [diff] [blame] | 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); |
Kuninori Morimoto | 4b4dab8 | 2013-07-28 18:58:29 -0700 | [diff] [blame] | 671 | |
Kuninori Morimoto | 78f13d0 | 2014-03-03 20:49:50 -0800 | [diff] [blame] | 672 | if (!pmod && !cmod) |
| 673 | break; |
| 674 | } |
Kuninori Morimoto | 4b4dab8 | 2013-07-28 18:58:29 -0700 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | if (!dai_nr) { |
| 678 | dev_err(dev, "no dai\n"); |
| 679 | return -EIO; |
| 680 | } |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 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 | |
Kuninori Morimoto | ecba9e7 | 2014-02-24 22:15:18 -0800 | [diff] [blame] | 689 | priv->rdai_nr = dai_nr; |
Kuninori Morimoto | 4984807 | 2014-02-24 22:14:33 -0800 | [diff] [blame] | 690 | priv->daidrv = drv; |
| 691 | priv->rdai = rdai; |
| 692 | |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 693 | for (i = 0; i < dai_nr; i++) { |
Kuninori Morimoto | 78f13d0 | 2014-03-03 20:49:50 -0800 | [diff] [blame] | 694 | if (info->dai_info) |
| 695 | rdai[i].info = &info->dai_info[i]; |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 696 | |
Kuninori Morimoto | 4b4dab8 | 2013-07-28 18:58:29 -0700 | [diff] [blame] | 697 | pmod = rsnd_ssi_mod_get_frm_dai(priv, i, 1); |
| 698 | cmod = rsnd_ssi_mod_get_frm_dai(priv, i, 0); |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 699 | |
| 700 | /* |
| 701 | * init rsnd_dai |
| 702 | */ |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 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; |
Kuninori Morimoto | 4b4dab8 | 2013-07-28 18:58:29 -0700 | [diff] [blame] | 710 | if (pmod) { |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 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; |
Kuninori Morimoto | 389933d | 2014-03-03 20:50:00 -0800 | [diff] [blame] | 715 | |
| 716 | if (info->dai_info) |
| 717 | rdai[i].playback.info = &info->dai_info[i].playback; |
Kuninori Morimoto | 9bfed6c | 2014-03-02 23:42:47 -0800 | [diff] [blame] | 718 | rsnd_path_init(priv, &rdai[i], &rdai[i].playback); |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 719 | } |
Kuninori Morimoto | 4b4dab8 | 2013-07-28 18:58:29 -0700 | [diff] [blame] | 720 | if (cmod) { |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 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; |
Kuninori Morimoto | 389933d | 2014-03-03 20:50:00 -0800 | [diff] [blame] | 725 | |
| 726 | if (info->dai_info) |
| 727 | rdai[i].capture.info = &info->dai_info[i].capture; |
Kuninori Morimoto | 9bfed6c | 2014-03-02 23:42:47 -0800 | [diff] [blame] | 728 | rsnd_path_init(priv, &rdai[i], &rdai[i].capture); |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 729 | } |
| 730 | |
Kuninori Morimoto | 4b4dab8 | 2013-07-28 18:58:29 -0700 | [diff] [blame] | 731 | dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name, |
| 732 | pmod ? "play" : " -- ", |
| 733 | cmod ? "capture" : " -- "); |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 734 | } |
| 735 | |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | static void rsnd_dai_remove(struct platform_device *pdev, |
| 740 | struct rsnd_priv *priv) |
| 741 | { |
Kuninori Morimoto | 9bfed6c | 2014-03-02 23:42:47 -0800 | [diff] [blame] | 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 | } |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 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, |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 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; |
Kuninori Morimoto | 7681f6a | 2014-03-03 20:50:33 -0800 | [diff] [blame^] | 845 | struct rsnd_dai *rdai; |
Kuninori Morimoto | d1ac970f | 2014-03-02 23:43:18 -0800 | [diff] [blame] | 846 | int (*probe_func[])(struct platform_device *pdev, |
| 847 | struct rsnd_priv *priv) = { |
| 848 | rsnd_gen_probe, |
| 849 | rsnd_ssi_probe, |
| 850 | rsnd_scu_probe, |
| 851 | rsnd_adg_probe, |
| 852 | rsnd_dai_probe, |
| 853 | }; |
| 854 | int ret, i; |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 855 | |
| 856 | info = pdev->dev.platform_data; |
| 857 | if (!info) { |
| 858 | dev_err(dev, "driver needs R-Car sound information\n"); |
| 859 | return -ENODEV; |
| 860 | } |
| 861 | |
| 862 | /* |
| 863 | * init priv data |
| 864 | */ |
| 865 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 866 | if (!priv) { |
| 867 | dev_err(dev, "priv allocate failed\n"); |
| 868 | return -ENODEV; |
| 869 | } |
| 870 | |
| 871 | priv->dev = dev; |
| 872 | priv->info = info; |
| 873 | spin_lock_init(&priv->lock); |
| 874 | |
| 875 | /* |
| 876 | * init each module |
| 877 | */ |
Kuninori Morimoto | d1ac970f | 2014-03-02 23:43:18 -0800 | [diff] [blame] | 878 | for (i = 0; i < ARRAY_SIZE(probe_func); i++) { |
| 879 | ret = probe_func[i](pdev, priv); |
| 880 | if (ret) |
| 881 | return ret; |
| 882 | } |
Kuninori Morimoto | 4b4dab8 | 2013-07-28 18:58:29 -0700 | [diff] [blame] | 883 | |
Kuninori Morimoto | 7681f6a | 2014-03-03 20:50:33 -0800 | [diff] [blame^] | 884 | for_each_rsnd_dai(rdai, priv, i) { |
| 885 | ret = rsnd_dai_call(rdai, &rdai->playback, probe); |
| 886 | if (ret) |
| 887 | return ret; |
| 888 | |
| 889 | ret = rsnd_dai_call(rdai, &rdai->capture, probe); |
| 890 | if (ret) |
| 891 | return ret; |
| 892 | } |
| 893 | |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 894 | /* |
| 895 | * asoc register |
| 896 | */ |
| 897 | ret = snd_soc_register_platform(dev, &rsnd_soc_platform); |
| 898 | if (ret < 0) { |
| 899 | dev_err(dev, "cannot snd soc register\n"); |
| 900 | return ret; |
| 901 | } |
| 902 | |
| 903 | ret = snd_soc_register_component(dev, &rsnd_soc_component, |
Kuninori Morimoto | ecba9e7 | 2014-02-24 22:15:18 -0800 | [diff] [blame] | 904 | priv->daidrv, rsnd_rdai_nr(priv)); |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 905 | if (ret < 0) { |
| 906 | dev_err(dev, "cannot snd dai register\n"); |
| 907 | goto exit_snd_soc; |
| 908 | } |
| 909 | |
| 910 | dev_set_drvdata(dev, priv); |
| 911 | |
| 912 | pm_runtime_enable(dev); |
| 913 | |
| 914 | dev_info(dev, "probed\n"); |
| 915 | return ret; |
| 916 | |
| 917 | exit_snd_soc: |
| 918 | snd_soc_unregister_platform(dev); |
| 919 | |
| 920 | return ret; |
| 921 | } |
| 922 | |
| 923 | static int rsnd_remove(struct platform_device *pdev) |
| 924 | { |
| 925 | struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev); |
Kuninori Morimoto | 7681f6a | 2014-03-03 20:50:33 -0800 | [diff] [blame^] | 926 | struct rsnd_dai *rdai; |
| 927 | int ret, i; |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 928 | |
| 929 | pm_runtime_disable(&pdev->dev); |
| 930 | |
Kuninori Morimoto | 7681f6a | 2014-03-03 20:50:33 -0800 | [diff] [blame^] | 931 | for_each_rsnd_dai(rdai, priv, i) { |
| 932 | ret = rsnd_dai_call(rdai, &rdai->playback, remove); |
| 933 | if (ret) |
| 934 | return ret; |
| 935 | |
| 936 | ret = rsnd_dai_call(rdai, &rdai->capture, remove); |
| 937 | if (ret) |
| 938 | return ret; |
| 939 | } |
| 940 | |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 941 | /* |
| 942 | * remove each module |
| 943 | */ |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 944 | rsnd_ssi_remove(pdev, priv); |
Kuninori Morimoto | dfc9403 | 2013-07-21 21:36:46 -0700 | [diff] [blame] | 945 | rsnd_adg_remove(pdev, priv); |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 946 | rsnd_scu_remove(pdev, priv); |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 947 | rsnd_dai_remove(pdev, priv); |
Kuninori Morimoto | 3337744 | 2013-07-21 21:36:21 -0700 | [diff] [blame] | 948 | rsnd_gen_remove(pdev, priv); |
Kuninori Morimoto | 1536a96 | 2013-07-21 21:35:52 -0700 | [diff] [blame] | 949 | |
| 950 | return 0; |
| 951 | } |
| 952 | |
| 953 | static struct platform_driver rsnd_driver = { |
| 954 | .driver = { |
| 955 | .name = "rcar_sound", |
| 956 | }, |
| 957 | .probe = rsnd_probe, |
| 958 | .remove = rsnd_remove, |
| 959 | }; |
| 960 | module_platform_driver(rsnd_driver); |
| 961 | |
| 962 | MODULE_LICENSE("GPL"); |
| 963 | MODULE_DESCRIPTION("Renesas R-Car audio driver"); |
| 964 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |
| 965 | MODULE_ALIAS("platform:rcar-pcm-audio"); |