Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Renesas R-Car SCU support |
| 3 | * |
| 4 | * Copyright (C) 2013 Renesas Solutions Corp. |
| 5 | * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | #include "rsnd.h" |
| 12 | |
| 13 | struct rsnd_scu { |
| 14 | struct rsnd_scu_platform_info *info; /* rcar_snd.h */ |
| 15 | struct rsnd_mod mod; |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 16 | struct clk *clk; |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 17 | }; |
| 18 | |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 19 | #define RSND_SCU_NAME_SIZE 16 |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 20 | |
| 21 | /* |
| 22 | * ADINR |
| 23 | */ |
| 24 | #define OTBL_24 (0 << 16) |
| 25 | #define OTBL_22 (2 << 16) |
| 26 | #define OTBL_20 (4 << 16) |
| 27 | #define OTBL_18 (6 << 16) |
| 28 | #define OTBL_16 (8 << 16) |
| 29 | |
Kuninori Morimoto | 39cf3c4 | 2014-01-23 18:40:47 -0800 | [diff] [blame] | 30 | #define rsnd_scu_mode_flags(p) ((p)->info->flags) |
| 31 | #define rsnd_scu_convert_rate(p) ((p)->info->convert_rate) |
| 32 | #define rsnd_mod_to_scu(_mod) \ |
| 33 | container_of((_mod), struct rsnd_scu, mod) |
Kuninori Morimoto | 96c7c0d | 2014-01-23 18:40:54 -0800 | [diff] [blame] | 34 | #define rsnd_scu_hpbif_is_enable(scu) \ |
| 35 | (rsnd_scu_mode_flags(scu) & RSND_SCU_USE_HPBIF) |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 36 | #define rsnd_scu_dma_available(scu) \ |
| 37 | rsnd_dma_available(rsnd_mod_to_dma(&(scu)->mod)) |
Kuninori Morimoto | 39cf3c4 | 2014-01-23 18:40:47 -0800 | [diff] [blame] | 38 | |
| 39 | #define for_each_rsnd_scu(pos, priv, i) \ |
| 40 | for ((i) = 0; \ |
| 41 | ((i) < rsnd_scu_nr(priv)) && \ |
| 42 | ((pos) = (struct rsnd_scu *)(priv)->scu + i); \ |
| 43 | i++) |
| 44 | |
| 45 | |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 46 | /* |
| 47 | * image of SRC (Sampling Rate Converter) |
| 48 | * |
| 49 | * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+ |
| 50 | * 48kHz <-> | SRC | <------> | SSI | <-----> | codec | |
| 51 | * 44.1kHz <-> +-----+ +-----+ +-------+ |
| 52 | * ... |
| 53 | * |
| 54 | */ |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 55 | |
Kuninori Morimoto | 41c6221 | 2014-01-23 18:39:56 -0800 | [diff] [blame] | 56 | /* |
Kuninori Morimoto | c926b74 | 2014-01-23 18:40:41 -0800 | [diff] [blame] | 57 | * scu.c is caring... |
| 58 | * |
| 59 | * Gen1 |
| 60 | * |
| 61 | * [mem] -> [SRU] -> [SSI] |
| 62 | * |--------| |
| 63 | * |
| 64 | * Gen2 |
| 65 | * |
| 66 | * [mem] -> [SCU] -> [SSIU] -> [SSI] |
| 67 | * |-----------------| |
| 68 | */ |
| 69 | |
| 70 | /* |
Kuninori Morimoto | 41c6221 | 2014-01-23 18:39:56 -0800 | [diff] [blame] | 71 | * How to use SRC bypass mode for debugging |
| 72 | * |
| 73 | * SRC has bypass mode, and it is useful for debugging. |
| 74 | * In Gen2 case, |
| 75 | * SRCm_MODE controls whether SRC is used or not |
| 76 | * SSI_MODE0 controls whether SSIU which receives SRC data |
| 77 | * is used or not. |
| 78 | * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC, |
| 79 | * but SRC bypass mode needs SSI_MODE0 only. |
| 80 | * |
| 81 | * This driver request |
| 82 | * struct rsnd_scu_platform_info { |
| 83 | * u32 flags; |
| 84 | * u32 convert_rate; |
| 85 | * } |
| 86 | * |
| 87 | * rsnd_scu_hpbif_is_enable() will be true |
| 88 | * if flags had RSND_SCU_USE_HPBIF, |
| 89 | * and it controls whether SSIU is used or not. |
| 90 | * |
| 91 | * rsnd_scu_convert_rate() indicates |
| 92 | * above convert_rate, and it controls |
| 93 | * whether SRC is used or not. |
| 94 | * |
| 95 | * ex) doesn't use SRC |
| 96 | * struct rsnd_scu_platform_info info = { |
| 97 | * .flags = 0, |
| 98 | * .convert_rate = 0, |
| 99 | * }; |
| 100 | * |
| 101 | * ex) uses SRC |
| 102 | * struct rsnd_scu_platform_info info = { |
| 103 | * .flags = RSND_SCU_USE_HPBIF, |
| 104 | * .convert_rate = 48000, |
| 105 | * }; |
| 106 | * |
| 107 | * ex) uses SRC bypass mode |
| 108 | * struct rsnd_scu_platform_info info = { |
| 109 | * .flags = RSND_SCU_USE_HPBIF, |
| 110 | * .convert_rate = 0, |
| 111 | * }; |
| 112 | * |
| 113 | */ |
| 114 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 115 | /* |
| 116 | * Gen1/Gen2 common functions |
| 117 | */ |
Kuninori Morimoto | 7b5ce97 | 2014-01-23 18:39:32 -0800 | [diff] [blame] | 118 | static int rsnd_scu_ssi_mode_init(struct rsnd_mod *mod, |
| 119 | struct rsnd_dai *rdai, |
| 120 | struct rsnd_dai_stream *io) |
| 121 | { |
| 122 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
Kuninori Morimoto | 96c7c0d | 2014-01-23 18:40:54 -0800 | [diff] [blame] | 123 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); |
Kuninori Morimoto | 374e542 | 2014-03-02 23:43:03 -0800 | [diff] [blame] | 124 | struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io); |
| 125 | int ssi_id = rsnd_mod_id(ssi_mod); |
Kuninori Morimoto | a2070fe | 2014-02-11 21:05:26 -0800 | [diff] [blame] | 126 | u32 convert_rate = rsnd_scu_convert_rate(scu); |
| 127 | |
| 128 | if (convert_rate && !rsnd_dai_is_clk_master(rdai)) { |
| 129 | struct device *dev = rsnd_priv_to_dev(priv); |
| 130 | |
| 131 | dev_err(dev, "rsnd should be clk master when you rate convert\n"); |
| 132 | return -EINVAL; |
| 133 | } |
Kuninori Morimoto | 7b5ce97 | 2014-01-23 18:39:32 -0800 | [diff] [blame] | 134 | |
| 135 | /* |
| 136 | * SSI_MODE0 |
| 137 | */ |
Kuninori Morimoto | 374e542 | 2014-03-02 23:43:03 -0800 | [diff] [blame] | 138 | rsnd_mod_bset(mod, SSI_MODE0, (1 << ssi_id), |
| 139 | rsnd_scu_hpbif_is_enable(scu) ? 0 : (1 << ssi_id)); |
Kuninori Morimoto | 7b5ce97 | 2014-01-23 18:39:32 -0800 | [diff] [blame] | 140 | |
| 141 | /* |
| 142 | * SSI_MODE1 |
| 143 | */ |
Kuninori Morimoto | 374e542 | 2014-03-02 23:43:03 -0800 | [diff] [blame] | 144 | if (rsnd_ssi_is_pin_sharing(ssi_mod)) { |
Kuninori Morimoto | 7b5ce97 | 2014-01-23 18:39:32 -0800 | [diff] [blame] | 145 | int shift = -1; |
Kuninori Morimoto | 374e542 | 2014-03-02 23:43:03 -0800 | [diff] [blame] | 146 | switch (ssi_id) { |
Kuninori Morimoto | 7b5ce97 | 2014-01-23 18:39:32 -0800 | [diff] [blame] | 147 | case 1: |
| 148 | shift = 0; |
| 149 | break; |
| 150 | case 2: |
| 151 | shift = 2; |
| 152 | break; |
| 153 | case 4: |
| 154 | shift = 16; |
| 155 | break; |
| 156 | } |
| 157 | |
| 158 | if (shift >= 0) |
| 159 | rsnd_mod_bset(mod, SSI_MODE1, |
| 160 | 0x3 << shift, |
| 161 | rsnd_dai_is_clk_master(rdai) ? |
| 162 | 0x2 << shift : 0x1 << shift); |
| 163 | } |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 168 | unsigned int rsnd_scu_get_ssi_rate(struct rsnd_priv *priv, |
Kuninori Morimoto | 374e542 | 2014-03-02 23:43:03 -0800 | [diff] [blame] | 169 | struct rsnd_dai_stream *io, |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 170 | struct snd_pcm_runtime *runtime) |
| 171 | { |
| 172 | struct rsnd_scu *scu; |
| 173 | unsigned int rate; |
| 174 | |
Kuninori Morimoto | 374e542 | 2014-03-02 23:43:03 -0800 | [diff] [blame] | 175 | scu = rsnd_mod_to_scu(rsnd_io_to_mod_scu(io)); |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 176 | |
| 177 | /* |
| 178 | * return convert rate if SRC is used, |
| 179 | * otherwise, return runtime->rate as usual |
| 180 | */ |
| 181 | rate = rsnd_scu_convert_rate(scu); |
| 182 | if (!rate) |
| 183 | rate = runtime->rate; |
| 184 | |
| 185 | return rate; |
| 186 | } |
| 187 | |
| 188 | static int rsnd_scu_set_convert_rate(struct rsnd_mod *mod, |
| 189 | struct rsnd_dai *rdai, |
| 190 | struct rsnd_dai_stream *io) |
| 191 | { |
| 192 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
| 193 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); |
| 194 | u32 convert_rate = rsnd_scu_convert_rate(scu); |
| 195 | u32 adinr = runtime->channels; |
| 196 | u32 fsrate = 0; |
| 197 | |
| 198 | if (convert_rate) |
| 199 | fsrate = 0x0400000 / convert_rate * runtime->rate; |
| 200 | |
| 201 | /* set/clear soft reset */ |
| 202 | rsnd_mod_write(mod, SRC_SWRSR, 0); |
| 203 | rsnd_mod_write(mod, SRC_SWRSR, 1); |
| 204 | |
| 205 | /* |
| 206 | * Initialize the operation of the SRC internal circuits |
| 207 | * see rsnd_scu_start() |
| 208 | */ |
| 209 | rsnd_mod_write(mod, SRC_SRCIR, 1); |
| 210 | |
| 211 | /* Set channel number and output bit length */ |
| 212 | switch (runtime->sample_bits) { |
| 213 | case 16: |
| 214 | adinr |= OTBL_16; |
| 215 | break; |
| 216 | case 32: |
| 217 | adinr |= OTBL_24; |
| 218 | break; |
| 219 | default: |
| 220 | return -EIO; |
| 221 | } |
| 222 | rsnd_mod_write(mod, SRC_ADINR, adinr); |
| 223 | |
| 224 | /* Enable the initial value of IFS */ |
| 225 | if (fsrate) { |
| 226 | rsnd_mod_write(mod, SRC_IFSCR, 1); |
| 227 | |
| 228 | /* Set initial value of IFS */ |
| 229 | rsnd_mod_write(mod, SRC_IFSVR, fsrate); |
| 230 | } |
| 231 | |
| 232 | /* use DMA transfer */ |
| 233 | rsnd_mod_write(mod, SRC_BUSIF_MODE, 1); |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static int rsnd_scu_init(struct rsnd_mod *mod, |
| 239 | struct rsnd_dai *rdai, |
| 240 | struct rsnd_dai_stream *io) |
| 241 | { |
| 242 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); |
| 243 | int ret; |
| 244 | |
| 245 | clk_enable(scu->clk); |
| 246 | |
| 247 | ret = rsnd_scu_ssi_mode_init(mod, rdai, io); |
| 248 | if (ret < 0) |
| 249 | return ret; |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | static int rsnd_scu_quit(struct rsnd_mod *mod, |
| 255 | struct rsnd_dai *rdai, |
| 256 | struct rsnd_dai_stream *io) |
| 257 | { |
| 258 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); |
| 259 | |
| 260 | clk_disable(scu->clk); |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static int rsnd_scu_start(struct rsnd_mod *mod, |
| 266 | struct rsnd_dai *rdai, |
| 267 | struct rsnd_dai_stream *io) |
| 268 | { |
| 269 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); |
| 270 | |
| 271 | /* |
| 272 | * Cancel the initialization and operate the SRC function |
| 273 | * see rsnd_scu_set_convert_rate() |
| 274 | */ |
| 275 | rsnd_mod_write(mod, SRC_SRCIR, 0); |
| 276 | |
| 277 | if (rsnd_scu_convert_rate(scu)) |
| 278 | rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1); |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | |
| 284 | static int rsnd_scu_stop(struct rsnd_mod *mod, |
| 285 | struct rsnd_dai *rdai, |
| 286 | struct rsnd_dai_stream *io) |
| 287 | { |
| 288 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); |
| 289 | |
| 290 | if (rsnd_scu_convert_rate(scu)) |
| 291 | rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0); |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | static struct rsnd_mod_ops rsnd_scu_non_ops = { |
| 297 | .name = "scu (non)", |
| 298 | }; |
| 299 | |
| 300 | /* |
| 301 | * Gen1 functions |
| 302 | */ |
| 303 | static int rsnd_src_set_route_gen1(struct rsnd_mod *mod, |
| 304 | struct rsnd_dai *rdai, |
| 305 | struct rsnd_dai_stream *io) |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 306 | { |
| 307 | struct scu_route_config { |
| 308 | u32 mask; |
| 309 | int shift; |
| 310 | } routes[] = { |
| 311 | { 0xF, 0, }, /* 0 */ |
| 312 | { 0xF, 4, }, /* 1 */ |
| 313 | { 0xF, 8, }, /* 2 */ |
| 314 | { 0x7, 12, }, /* 3 */ |
| 315 | { 0x7, 16, }, /* 4 */ |
| 316 | { 0x7, 20, }, /* 5 */ |
| 317 | { 0x7, 24, }, /* 6 */ |
| 318 | { 0x3, 28, }, /* 7 */ |
| 319 | { 0x3, 30, }, /* 8 */ |
| 320 | }; |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 321 | u32 mask; |
| 322 | u32 val; |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 323 | int id; |
| 324 | |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 325 | id = rsnd_mod_id(mod); |
Dan Carpenter | b5f3d7a | 2013-11-08 12:46:10 +0300 | [diff] [blame] | 326 | if (id < 0 || id >= ARRAY_SIZE(routes)) |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 327 | return -EIO; |
| 328 | |
| 329 | /* |
| 330 | * SRC_ROUTE_SELECT |
| 331 | */ |
| 332 | val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2; |
| 333 | val = val << routes[id].shift; |
| 334 | mask = routes[id].mask << routes[id].shift; |
| 335 | |
| 336 | rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val); |
| 337 | |
Kuninori Morimoto | 28dc4b6 | 2014-01-23 18:41:10 -0800 | [diff] [blame] | 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | static int rsnd_scu_set_convert_timing_gen1(struct rsnd_mod *mod, |
| 342 | struct rsnd_dai *rdai, |
| 343 | struct rsnd_dai_stream *io) |
| 344 | { |
| 345 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
| 346 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); |
| 347 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
| 348 | u32 convert_rate = rsnd_scu_convert_rate(scu); |
| 349 | u32 mask; |
| 350 | u32 val; |
| 351 | int shift; |
| 352 | int id = rsnd_mod_id(mod); |
| 353 | int ret; |
| 354 | |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 355 | /* |
| 356 | * SRC_TIMING_SELECT |
| 357 | */ |
| 358 | shift = (id % 4) * 8; |
| 359 | mask = 0x1F << shift; |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 360 | |
| 361 | /* |
| 362 | * ADG is used as source clock if SRC was used, |
| 363 | * then, SSI WS is used as destination clock. |
| 364 | * SSI WS is used as source clock if SRC is not used |
| 365 | * (when playback, source/destination become reverse when capture) |
| 366 | */ |
Kuninori Morimoto | 28dc4b6 | 2014-01-23 18:41:10 -0800 | [diff] [blame] | 367 | ret = 0; |
| 368 | if (convert_rate) { |
| 369 | /* use ADG */ |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 370 | val = 0; |
Kuninori Morimoto | 28dc4b6 | 2014-01-23 18:41:10 -0800 | [diff] [blame] | 371 | ret = rsnd_adg_set_convert_clk_gen1(priv, mod, |
| 372 | runtime->rate, |
| 373 | convert_rate); |
| 374 | } else if (8 == id) { |
| 375 | /* use SSI WS, but SRU8 is special */ |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 376 | val = id << shift; |
Kuninori Morimoto | 28dc4b6 | 2014-01-23 18:41:10 -0800 | [diff] [blame] | 377 | } else { |
| 378 | /* use SSI WS */ |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 379 | val = (id + 1) << shift; |
Kuninori Morimoto | 28dc4b6 | 2014-01-23 18:41:10 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | if (ret < 0) |
| 383 | return ret; |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 384 | |
| 385 | switch (id / 4) { |
| 386 | case 0: |
| 387 | rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val); |
| 388 | break; |
| 389 | case 1: |
| 390 | rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val); |
| 391 | break; |
| 392 | case 2: |
| 393 | rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val); |
| 394 | break; |
| 395 | } |
| 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 400 | static int rsnd_scu_set_convert_rate_gen1(struct rsnd_mod *mod, |
| 401 | struct rsnd_dai *rdai, |
| 402 | struct rsnd_dai_stream *io) |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 403 | { |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 404 | int ret; |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 405 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 406 | ret = rsnd_scu_set_convert_rate(mod, rdai, io); |
| 407 | if (ret < 0) |
| 408 | return ret; |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 409 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 410 | /* Select SRC mode (fixed value) */ |
| 411 | rsnd_mod_write(mod, SRC_SRCCR, 0x00010110); |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 412 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 413 | /* Set the restriction value of the FS ratio (98%) */ |
| 414 | rsnd_mod_write(mod, SRC_MNFSR, |
| 415 | rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98); |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 416 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 417 | /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */ |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 418 | |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 419 | return 0; |
| 420 | } |
| 421 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 422 | static int rsnd_scu_init_gen1(struct rsnd_mod *mod, |
| 423 | struct rsnd_dai *rdai, |
| 424 | struct rsnd_dai_stream *io) |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 425 | { |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 426 | int ret; |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 427 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 428 | ret = rsnd_scu_init(mod, rdai, io); |
Kuninori Morimoto | 7b5ce97 | 2014-01-23 18:39:32 -0800 | [diff] [blame] | 429 | if (ret < 0) |
| 430 | return ret; |
| 431 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 432 | ret = rsnd_src_set_route_gen1(mod, rdai, io); |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 433 | if (ret < 0) |
| 434 | return ret; |
| 435 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 436 | ret = rsnd_scu_set_convert_rate_gen1(mod, rdai, io); |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 437 | if (ret < 0) |
| 438 | return ret; |
| 439 | |
Kuninori Morimoto | 28dc4b6 | 2014-01-23 18:41:10 -0800 | [diff] [blame] | 440 | ret = rsnd_scu_set_convert_timing_gen1(mod, rdai, io); |
| 441 | if (ret < 0) |
| 442 | return ret; |
| 443 | |
Kuninori Morimoto | a204d90 | 2014-01-23 18:38:33 -0800 | [diff] [blame] | 444 | return 0; |
| 445 | } |
| 446 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 447 | static int rsnd_scu_start_gen1(struct rsnd_mod *mod, |
| 448 | struct rsnd_dai *rdai, |
| 449 | struct rsnd_dai_stream *io) |
Kuninori Morimoto | a204d90 | 2014-01-23 18:38:33 -0800 | [diff] [blame] | 450 | { |
Kuninori Morimoto | e7ce74e | 2014-01-23 18:38:50 -0800 | [diff] [blame] | 451 | int id = rsnd_mod_id(mod); |
Kuninori Morimoto | a204d90 | 2014-01-23 18:38:33 -0800 | [diff] [blame] | 452 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 453 | rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id)); |
Kuninori Morimoto | e7ce74e | 2014-01-23 18:38:50 -0800 | [diff] [blame] | 454 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 455 | return rsnd_scu_start(mod, rdai, io); |
Kuninori Morimoto | a204d90 | 2014-01-23 18:38:33 -0800 | [diff] [blame] | 456 | } |
| 457 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 458 | static int rsnd_scu_stop_gen1(struct rsnd_mod *mod, |
| 459 | struct rsnd_dai *rdai, |
| 460 | struct rsnd_dai_stream *io) |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 461 | { |
Kuninori Morimoto | e7ce74e | 2014-01-23 18:38:50 -0800 | [diff] [blame] | 462 | int id = rsnd_mod_id(mod); |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 463 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 464 | rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0); |
Kuninori Morimoto | e7ce74e | 2014-01-23 18:38:50 -0800 | [diff] [blame] | 465 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 466 | return rsnd_scu_stop(mod, rdai, io); |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 467 | } |
| 468 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 469 | static struct rsnd_mod_ops rsnd_scu_gen1_ops = { |
| 470 | .name = "sru (gen1)", |
| 471 | .init = rsnd_scu_init_gen1, |
Kuninori Morimoto | a204d90 | 2014-01-23 18:38:33 -0800 | [diff] [blame] | 472 | .quit = rsnd_scu_quit, |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 473 | .start = rsnd_scu_start_gen1, |
| 474 | .stop = rsnd_scu_stop_gen1, |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 475 | }; |
| 476 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 477 | static struct rsnd_mod_ops rsnd_scu_non_gen1_ops = { |
| 478 | .name = "non-sru (gen1)", |
Kuninori Morimoto | 7b5ce97 | 2014-01-23 18:39:32 -0800 | [diff] [blame] | 479 | .init = rsnd_scu_ssi_mode_init, |
Kuninori Morimoto | 013f38f | 2014-01-23 18:38:26 -0800 | [diff] [blame] | 480 | }; |
| 481 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 482 | /* |
| 483 | * Gen2 functions |
| 484 | */ |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 485 | static int rsnd_scu_set_convert_rate_gen2(struct rsnd_mod *mod, |
| 486 | struct rsnd_dai *rdai, |
| 487 | struct rsnd_dai_stream *io) |
| 488 | { |
| 489 | int ret; |
| 490 | |
| 491 | ret = rsnd_scu_set_convert_rate(mod, rdai, io); |
| 492 | if (ret < 0) |
| 493 | return ret; |
| 494 | |
| 495 | rsnd_mod_write(mod, SSI_BUSIF_ADINR, rsnd_mod_read(mod, SRC_ADINR)); |
| 496 | rsnd_mod_write(mod, SSI_BUSIF_MODE, rsnd_mod_read(mod, SRC_BUSIF_MODE)); |
| 497 | |
| 498 | rsnd_mod_write(mod, SRC_SRCCR, 0x00011110); |
| 499 | |
| 500 | rsnd_mod_write(mod, SRC_BSDSR, 0x01800000); |
| 501 | rsnd_mod_write(mod, SRC_BSISR, 0x00100060); |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | static int rsnd_scu_set_convert_timing_gen2(struct rsnd_mod *mod, |
| 507 | struct rsnd_dai *rdai, |
| 508 | struct rsnd_dai_stream *io) |
| 509 | { |
| 510 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
| 511 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); |
| 512 | u32 convert_rate = rsnd_scu_convert_rate(scu); |
| 513 | int ret; |
| 514 | |
| 515 | if (convert_rate) |
| 516 | ret = rsnd_adg_set_convert_clk_gen2(mod, rdai, io, |
| 517 | runtime->rate, |
| 518 | convert_rate); |
| 519 | else |
| 520 | ret = rsnd_adg_set_convert_timing_gen2(mod, rdai, io); |
| 521 | |
| 522 | return ret; |
| 523 | } |
| 524 | |
| 525 | static int rsnd_scu_init_gen2(struct rsnd_mod *mod, |
| 526 | struct rsnd_dai *rdai, |
| 527 | struct rsnd_dai_stream *io) |
| 528 | { |
| 529 | int ret; |
| 530 | |
| 531 | ret = rsnd_scu_init(mod, rdai, io); |
| 532 | if (ret < 0) |
| 533 | return ret; |
| 534 | |
| 535 | ret = rsnd_scu_set_convert_rate_gen2(mod, rdai, io); |
| 536 | if (ret < 0) |
| 537 | return ret; |
| 538 | |
| 539 | ret = rsnd_scu_set_convert_timing_gen2(mod, rdai, io); |
| 540 | if (ret < 0) |
| 541 | return ret; |
| 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | static int rsnd_scu_start_gen2(struct rsnd_mod *mod, |
| 547 | struct rsnd_dai *rdai, |
| 548 | struct rsnd_dai_stream *io) |
| 549 | { |
| 550 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); |
| 551 | |
| 552 | rsnd_dma_start(rsnd_mod_to_dma(&scu->mod)); |
| 553 | |
| 554 | rsnd_mod_write(mod, SSI_CTRL, 0x1); |
| 555 | rsnd_mod_write(mod, SRC_CTRL, 0x11); |
| 556 | |
| 557 | return rsnd_scu_start(mod, rdai, io); |
| 558 | } |
| 559 | |
| 560 | static int rsnd_scu_stop_gen2(struct rsnd_mod *mod, |
| 561 | struct rsnd_dai *rdai, |
| 562 | struct rsnd_dai_stream *io) |
| 563 | { |
| 564 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); |
| 565 | |
| 566 | rsnd_mod_write(mod, SSI_CTRL, 0); |
| 567 | rsnd_mod_write(mod, SRC_CTRL, 0); |
| 568 | |
| 569 | rsnd_dma_stop(rsnd_mod_to_dma(&scu->mod)); |
| 570 | |
| 571 | return rsnd_scu_stop(mod, rdai, io); |
| 572 | } |
| 573 | |
| 574 | static struct rsnd_mod_ops rsnd_scu_gen2_ops = { |
| 575 | .name = "scu (gen2)", |
| 576 | .init = rsnd_scu_init_gen2, |
| 577 | .quit = rsnd_scu_quit, |
| 578 | .start = rsnd_scu_start_gen2, |
| 579 | .stop = rsnd_scu_stop_gen2, |
| 580 | }; |
| 581 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 582 | static int rsnd_scu_start_non_gen2(struct rsnd_mod *mod, |
| 583 | struct rsnd_dai *rdai, |
| 584 | struct rsnd_dai_stream *io) |
| 585 | { |
Kuninori Morimoto | 374e542 | 2014-03-02 23:43:03 -0800 | [diff] [blame] | 586 | struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io); |
| 587 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 588 | /* enable PIO interrupt */ |
Kuninori Morimoto | 374e542 | 2014-03-02 23:43:03 -0800 | [diff] [blame] | 589 | rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0f000000); |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 590 | |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | static struct rsnd_mod_ops rsnd_scu_non_gen2_ops = { |
| 595 | .name = "non-scu (gen2)", |
| 596 | .init = rsnd_scu_ssi_mode_init, |
| 597 | .start = rsnd_scu_start_non_gen2, |
| 598 | }; |
| 599 | |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 600 | struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id) |
| 601 | { |
Takashi Iwai | 8b14719 | 2013-11-05 18:40:05 +0100 | [diff] [blame] | 602 | if (WARN_ON(id < 0 || id >= rsnd_scu_nr(priv))) |
| 603 | id = 0; |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 604 | |
| 605 | return &((struct rsnd_scu *)(priv->scu) + id)->mod; |
| 606 | } |
| 607 | |
| 608 | int rsnd_scu_probe(struct platform_device *pdev, |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 609 | struct rsnd_priv *priv) |
| 610 | { |
Kuninori Morimoto | 5da39cf | 2014-02-24 22:15:00 -0800 | [diff] [blame] | 611 | struct rcar_snd_info *info = rsnd_priv_to_info(priv); |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 612 | struct device *dev = rsnd_priv_to_dev(priv); |
| 613 | struct rsnd_scu *scu; |
Kuninori Morimoto | 013f38f | 2014-01-23 18:38:26 -0800 | [diff] [blame] | 614 | struct rsnd_mod_ops *ops; |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 615 | struct clk *clk; |
| 616 | char name[RSND_SCU_NAME_SIZE]; |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 617 | int i, nr; |
| 618 | |
| 619 | /* |
| 620 | * init SCU |
| 621 | */ |
| 622 | nr = info->scu_info_nr; |
Kuninori Morimoto | 389933d | 2014-03-03 20:50:00 -0800 | [diff] [blame^] | 623 | if (!nr) |
| 624 | return 0; |
| 625 | |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 626 | scu = devm_kzalloc(dev, sizeof(*scu) * nr, GFP_KERNEL); |
| 627 | if (!scu) { |
| 628 | dev_err(dev, "SCU allocate failed\n"); |
| 629 | return -ENOMEM; |
| 630 | } |
| 631 | |
| 632 | priv->scu_nr = nr; |
| 633 | priv->scu = scu; |
| 634 | |
| 635 | for_each_rsnd_scu(scu, priv, i) { |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 636 | snprintf(name, RSND_SCU_NAME_SIZE, "scu.%d", i); |
| 637 | |
| 638 | clk = devm_clk_get(dev, name); |
| 639 | if (IS_ERR(clk)) |
| 640 | return PTR_ERR(clk); |
| 641 | |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 642 | scu->info = &info->scu_info[i]; |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 643 | scu->clk = clk; |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 644 | |
Kuninori Morimoto | 013f38f | 2014-01-23 18:38:26 -0800 | [diff] [blame] | 645 | ops = &rsnd_scu_non_ops; |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 646 | if (rsnd_scu_hpbif_is_enable(scu)) { |
| 647 | if (rsnd_is_gen1(priv)) |
| 648 | ops = &rsnd_scu_gen1_ops; |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 649 | if (rsnd_is_gen2(priv)) { |
Kuninori Morimoto | 389933d | 2014-03-03 20:50:00 -0800 | [diff] [blame^] | 650 | int ret; |
| 651 | int is_play; |
| 652 | |
| 653 | if (info->dai_info) { |
| 654 | is_play = rsnd_info_is_playback(priv, scu); |
| 655 | } else { |
| 656 | struct rsnd_mod *ssi = rsnd_ssi_mod_get(priv, i); |
| 657 | is_play = rsnd_ssi_is_play(ssi); |
| 658 | } |
| 659 | ret = rsnd_dma_init(priv, |
| 660 | rsnd_mod_to_dma(&scu->mod), |
| 661 | is_play, |
| 662 | scu->info->dma_id); |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 663 | if (ret < 0) |
| 664 | return ret; |
| 665 | |
| 666 | ops = &rsnd_scu_gen2_ops; |
| 667 | } |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 668 | } else { |
| 669 | if (rsnd_is_gen1(priv)) |
| 670 | ops = &rsnd_scu_non_gen1_ops; |
| 671 | if (rsnd_is_gen2(priv)) |
| 672 | ops = &rsnd_scu_non_gen2_ops; |
| 673 | } |
Kuninori Morimoto | 013f38f | 2014-01-23 18:38:26 -0800 | [diff] [blame] | 674 | |
Kuninori Morimoto | a126021 | 2014-03-02 23:42:55 -0800 | [diff] [blame] | 675 | rsnd_mod_init(priv, &scu->mod, ops, RSND_MOD_SCU, i); |
Kuninori Morimoto | 013f38f | 2014-01-23 18:38:26 -0800 | [diff] [blame] | 676 | |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 677 | dev_dbg(dev, "SCU%d probed\n", i); |
| 678 | } |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 679 | |
| 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | void rsnd_scu_remove(struct platform_device *pdev, |
| 684 | struct rsnd_priv *priv) |
| 685 | { |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 686 | struct rsnd_scu *scu; |
| 687 | int i; |
| 688 | |
| 689 | for_each_rsnd_scu(scu, priv, i) { |
| 690 | if (rsnd_scu_dma_available(scu)) |
| 691 | rsnd_dma_quit(priv, rsnd_mod_to_dma(&scu->mod)); |
| 692 | } |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 693 | } |