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 | 7b5ce97 | 2014-01-23 18:39:32 -0800 | [diff] [blame] | 124 | int id = rsnd_mod_id(mod); |
| 125 | |
| 126 | /* |
| 127 | * SSI_MODE0 |
| 128 | */ |
| 129 | rsnd_mod_bset(mod, SSI_MODE0, (1 << id), |
Kuninori Morimoto | 96c7c0d | 2014-01-23 18:40:54 -0800 | [diff] [blame] | 130 | rsnd_scu_hpbif_is_enable(scu) ? 0 : (1 << id)); |
Kuninori Morimoto | 7b5ce97 | 2014-01-23 18:39:32 -0800 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * SSI_MODE1 |
| 134 | */ |
| 135 | if (rsnd_ssi_is_pin_sharing(rsnd_ssi_mod_get(priv, id))) { |
| 136 | int shift = -1; |
| 137 | switch (id) { |
| 138 | case 1: |
| 139 | shift = 0; |
| 140 | break; |
| 141 | case 2: |
| 142 | shift = 2; |
| 143 | break; |
| 144 | case 4: |
| 145 | shift = 16; |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | if (shift >= 0) |
| 150 | rsnd_mod_bset(mod, SSI_MODE1, |
| 151 | 0x3 << shift, |
| 152 | rsnd_dai_is_clk_master(rdai) ? |
| 153 | 0x2 << shift : 0x1 << shift); |
| 154 | } |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 159 | unsigned int rsnd_scu_get_ssi_rate(struct rsnd_priv *priv, |
| 160 | struct rsnd_mod *ssi_mod, |
| 161 | struct snd_pcm_runtime *runtime) |
| 162 | { |
| 163 | struct rsnd_scu *scu; |
| 164 | unsigned int rate; |
| 165 | |
| 166 | /* this function is assuming SSI id = SCU id here */ |
| 167 | scu = rsnd_mod_to_scu(rsnd_scu_mod_get(priv, rsnd_mod_id(ssi_mod))); |
| 168 | |
| 169 | /* |
| 170 | * return convert rate if SRC is used, |
| 171 | * otherwise, return runtime->rate as usual |
| 172 | */ |
| 173 | rate = rsnd_scu_convert_rate(scu); |
| 174 | if (!rate) |
| 175 | rate = runtime->rate; |
| 176 | |
| 177 | return rate; |
| 178 | } |
| 179 | |
| 180 | static int rsnd_scu_set_convert_rate(struct rsnd_mod *mod, |
| 181 | struct rsnd_dai *rdai, |
| 182 | struct rsnd_dai_stream *io) |
| 183 | { |
| 184 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
| 185 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); |
| 186 | u32 convert_rate = rsnd_scu_convert_rate(scu); |
| 187 | u32 adinr = runtime->channels; |
| 188 | u32 fsrate = 0; |
| 189 | |
| 190 | if (convert_rate) |
| 191 | fsrate = 0x0400000 / convert_rate * runtime->rate; |
| 192 | |
| 193 | /* set/clear soft reset */ |
| 194 | rsnd_mod_write(mod, SRC_SWRSR, 0); |
| 195 | rsnd_mod_write(mod, SRC_SWRSR, 1); |
| 196 | |
| 197 | /* |
| 198 | * Initialize the operation of the SRC internal circuits |
| 199 | * see rsnd_scu_start() |
| 200 | */ |
| 201 | rsnd_mod_write(mod, SRC_SRCIR, 1); |
| 202 | |
| 203 | /* Set channel number and output bit length */ |
| 204 | switch (runtime->sample_bits) { |
| 205 | case 16: |
| 206 | adinr |= OTBL_16; |
| 207 | break; |
| 208 | case 32: |
| 209 | adinr |= OTBL_24; |
| 210 | break; |
| 211 | default: |
| 212 | return -EIO; |
| 213 | } |
| 214 | rsnd_mod_write(mod, SRC_ADINR, adinr); |
| 215 | |
| 216 | /* Enable the initial value of IFS */ |
| 217 | if (fsrate) { |
| 218 | rsnd_mod_write(mod, SRC_IFSCR, 1); |
| 219 | |
| 220 | /* Set initial value of IFS */ |
| 221 | rsnd_mod_write(mod, SRC_IFSVR, fsrate); |
| 222 | } |
| 223 | |
| 224 | /* use DMA transfer */ |
| 225 | rsnd_mod_write(mod, SRC_BUSIF_MODE, 1); |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | static int rsnd_scu_init(struct rsnd_mod *mod, |
| 231 | struct rsnd_dai *rdai, |
| 232 | struct rsnd_dai_stream *io) |
| 233 | { |
| 234 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); |
| 235 | int ret; |
| 236 | |
| 237 | clk_enable(scu->clk); |
| 238 | |
| 239 | ret = rsnd_scu_ssi_mode_init(mod, rdai, io); |
| 240 | if (ret < 0) |
| 241 | return ret; |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static int rsnd_scu_quit(struct rsnd_mod *mod, |
| 247 | struct rsnd_dai *rdai, |
| 248 | struct rsnd_dai_stream *io) |
| 249 | { |
| 250 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); |
| 251 | |
| 252 | clk_disable(scu->clk); |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static int rsnd_scu_start(struct rsnd_mod *mod, |
| 258 | struct rsnd_dai *rdai, |
| 259 | struct rsnd_dai_stream *io) |
| 260 | { |
| 261 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); |
| 262 | |
| 263 | /* |
| 264 | * Cancel the initialization and operate the SRC function |
| 265 | * see rsnd_scu_set_convert_rate() |
| 266 | */ |
| 267 | rsnd_mod_write(mod, SRC_SRCIR, 0); |
| 268 | |
| 269 | if (rsnd_scu_convert_rate(scu)) |
| 270 | rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1); |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | static int rsnd_scu_stop(struct rsnd_mod *mod, |
| 277 | struct rsnd_dai *rdai, |
| 278 | struct rsnd_dai_stream *io) |
| 279 | { |
| 280 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); |
| 281 | |
| 282 | if (rsnd_scu_convert_rate(scu)) |
| 283 | rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0); |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | static struct rsnd_mod_ops rsnd_scu_non_ops = { |
| 289 | .name = "scu (non)", |
| 290 | }; |
| 291 | |
| 292 | /* |
| 293 | * Gen1 functions |
| 294 | */ |
| 295 | static int rsnd_src_set_route_gen1(struct rsnd_mod *mod, |
| 296 | struct rsnd_dai *rdai, |
| 297 | struct rsnd_dai_stream *io) |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 298 | { |
| 299 | struct scu_route_config { |
| 300 | u32 mask; |
| 301 | int shift; |
| 302 | } routes[] = { |
| 303 | { 0xF, 0, }, /* 0 */ |
| 304 | { 0xF, 4, }, /* 1 */ |
| 305 | { 0xF, 8, }, /* 2 */ |
| 306 | { 0x7, 12, }, /* 3 */ |
| 307 | { 0x7, 16, }, /* 4 */ |
| 308 | { 0x7, 20, }, /* 5 */ |
| 309 | { 0x7, 24, }, /* 6 */ |
| 310 | { 0x3, 28, }, /* 7 */ |
| 311 | { 0x3, 30, }, /* 8 */ |
| 312 | }; |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 313 | u32 mask; |
| 314 | u32 val; |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 315 | int id; |
| 316 | |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 317 | id = rsnd_mod_id(mod); |
Dan Carpenter | b5f3d7a | 2013-11-08 12:46:10 +0300 | [diff] [blame] | 318 | if (id < 0 || id >= ARRAY_SIZE(routes)) |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 319 | return -EIO; |
| 320 | |
| 321 | /* |
| 322 | * SRC_ROUTE_SELECT |
| 323 | */ |
| 324 | val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2; |
| 325 | val = val << routes[id].shift; |
| 326 | mask = routes[id].mask << routes[id].shift; |
| 327 | |
| 328 | rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val); |
| 329 | |
Kuninori Morimoto | 28dc4b6 | 2014-01-23 18:41:10 -0800 | [diff] [blame] | 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | static int rsnd_scu_set_convert_timing_gen1(struct rsnd_mod *mod, |
| 334 | struct rsnd_dai *rdai, |
| 335 | struct rsnd_dai_stream *io) |
| 336 | { |
| 337 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
| 338 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); |
| 339 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
| 340 | u32 convert_rate = rsnd_scu_convert_rate(scu); |
| 341 | u32 mask; |
| 342 | u32 val; |
| 343 | int shift; |
| 344 | int id = rsnd_mod_id(mod); |
| 345 | int ret; |
| 346 | |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 347 | /* |
| 348 | * SRC_TIMING_SELECT |
| 349 | */ |
| 350 | shift = (id % 4) * 8; |
| 351 | mask = 0x1F << shift; |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 352 | |
| 353 | /* |
| 354 | * ADG is used as source clock if SRC was used, |
| 355 | * then, SSI WS is used as destination clock. |
| 356 | * SSI WS is used as source clock if SRC is not used |
| 357 | * (when playback, source/destination become reverse when capture) |
| 358 | */ |
Kuninori Morimoto | 28dc4b6 | 2014-01-23 18:41:10 -0800 | [diff] [blame] | 359 | ret = 0; |
| 360 | if (convert_rate) { |
| 361 | /* use ADG */ |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 362 | val = 0; |
Kuninori Morimoto | 28dc4b6 | 2014-01-23 18:41:10 -0800 | [diff] [blame] | 363 | ret = rsnd_adg_set_convert_clk_gen1(priv, mod, |
| 364 | runtime->rate, |
| 365 | convert_rate); |
| 366 | } else if (8 == id) { |
| 367 | /* use SSI WS, but SRU8 is special */ |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 368 | val = id << shift; |
Kuninori Morimoto | 28dc4b6 | 2014-01-23 18:41:10 -0800 | [diff] [blame] | 369 | } else { |
| 370 | /* use SSI WS */ |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 371 | val = (id + 1) << shift; |
Kuninori Morimoto | 28dc4b6 | 2014-01-23 18:41:10 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | if (ret < 0) |
| 375 | return ret; |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 376 | |
| 377 | switch (id / 4) { |
| 378 | case 0: |
| 379 | rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val); |
| 380 | break; |
| 381 | case 1: |
| 382 | rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val); |
| 383 | break; |
| 384 | case 2: |
| 385 | rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val); |
| 386 | break; |
| 387 | } |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 392 | static int rsnd_scu_set_convert_rate_gen1(struct rsnd_mod *mod, |
| 393 | struct rsnd_dai *rdai, |
| 394 | struct rsnd_dai_stream *io) |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 395 | { |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 396 | int ret; |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 397 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 398 | ret = rsnd_scu_set_convert_rate(mod, rdai, io); |
| 399 | if (ret < 0) |
| 400 | return ret; |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 401 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 402 | /* Select SRC mode (fixed value) */ |
| 403 | rsnd_mod_write(mod, SRC_SRCCR, 0x00010110); |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 404 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 405 | /* Set the restriction value of the FS ratio (98%) */ |
| 406 | rsnd_mod_write(mod, SRC_MNFSR, |
| 407 | rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98); |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 408 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 409 | /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */ |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 410 | |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 411 | return 0; |
| 412 | } |
| 413 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 414 | static int rsnd_scu_init_gen1(struct rsnd_mod *mod, |
| 415 | struct rsnd_dai *rdai, |
| 416 | struct rsnd_dai_stream *io) |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 417 | { |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 418 | int ret; |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 419 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 420 | ret = rsnd_scu_init(mod, rdai, io); |
Kuninori Morimoto | 7b5ce97 | 2014-01-23 18:39:32 -0800 | [diff] [blame] | 421 | if (ret < 0) |
| 422 | return ret; |
| 423 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 424 | ret = rsnd_src_set_route_gen1(mod, rdai, io); |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 425 | if (ret < 0) |
| 426 | return ret; |
| 427 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 428 | ret = rsnd_scu_set_convert_rate_gen1(mod, rdai, io); |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 429 | if (ret < 0) |
| 430 | return ret; |
| 431 | |
Kuninori Morimoto | 28dc4b6 | 2014-01-23 18:41:10 -0800 | [diff] [blame] | 432 | ret = rsnd_scu_set_convert_timing_gen1(mod, rdai, io); |
| 433 | if (ret < 0) |
| 434 | return ret; |
| 435 | |
Kuninori Morimoto | a204d90 | 2014-01-23 18:38:33 -0800 | [diff] [blame] | 436 | return 0; |
| 437 | } |
| 438 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 439 | static int rsnd_scu_start_gen1(struct rsnd_mod *mod, |
| 440 | struct rsnd_dai *rdai, |
| 441 | struct rsnd_dai_stream *io) |
Kuninori Morimoto | a204d90 | 2014-01-23 18:38:33 -0800 | [diff] [blame] | 442 | { |
Kuninori Morimoto | e7ce74e | 2014-01-23 18:38:50 -0800 | [diff] [blame] | 443 | int id = rsnd_mod_id(mod); |
Kuninori Morimoto | a204d90 | 2014-01-23 18:38:33 -0800 | [diff] [blame] | 444 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 445 | rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id)); |
Kuninori Morimoto | e7ce74e | 2014-01-23 18:38:50 -0800 | [diff] [blame] | 446 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 447 | return rsnd_scu_start(mod, rdai, io); |
Kuninori Morimoto | a204d90 | 2014-01-23 18:38:33 -0800 | [diff] [blame] | 448 | } |
| 449 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 450 | static int rsnd_scu_stop_gen1(struct rsnd_mod *mod, |
| 451 | struct rsnd_dai *rdai, |
| 452 | struct rsnd_dai_stream *io) |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 453 | { |
Kuninori Morimoto | e7ce74e | 2014-01-23 18:38:50 -0800 | [diff] [blame] | 454 | int id = rsnd_mod_id(mod); |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 455 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 456 | rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0); |
Kuninori Morimoto | e7ce74e | 2014-01-23 18:38:50 -0800 | [diff] [blame] | 457 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 458 | return rsnd_scu_stop(mod, rdai, io); |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 459 | } |
| 460 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 461 | static struct rsnd_mod_ops rsnd_scu_gen1_ops = { |
| 462 | .name = "sru (gen1)", |
| 463 | .init = rsnd_scu_init_gen1, |
Kuninori Morimoto | a204d90 | 2014-01-23 18:38:33 -0800 | [diff] [blame] | 464 | .quit = rsnd_scu_quit, |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 465 | .start = rsnd_scu_start_gen1, |
| 466 | .stop = rsnd_scu_stop_gen1, |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 467 | }; |
| 468 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 469 | static struct rsnd_mod_ops rsnd_scu_non_gen1_ops = { |
| 470 | .name = "non-sru (gen1)", |
Kuninori Morimoto | 7b5ce97 | 2014-01-23 18:39:32 -0800 | [diff] [blame] | 471 | .init = rsnd_scu_ssi_mode_init, |
Kuninori Morimoto | 013f38f | 2014-01-23 18:38:26 -0800 | [diff] [blame] | 472 | }; |
| 473 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 474 | /* |
| 475 | * Gen2 functions |
| 476 | */ |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 477 | static int rsnd_scu_set_convert_rate_gen2(struct rsnd_mod *mod, |
| 478 | struct rsnd_dai *rdai, |
| 479 | struct rsnd_dai_stream *io) |
| 480 | { |
| 481 | int ret; |
| 482 | |
| 483 | ret = rsnd_scu_set_convert_rate(mod, rdai, io); |
| 484 | if (ret < 0) |
| 485 | return ret; |
| 486 | |
| 487 | rsnd_mod_write(mod, SSI_BUSIF_ADINR, rsnd_mod_read(mod, SRC_ADINR)); |
| 488 | rsnd_mod_write(mod, SSI_BUSIF_MODE, rsnd_mod_read(mod, SRC_BUSIF_MODE)); |
| 489 | |
| 490 | rsnd_mod_write(mod, SRC_SRCCR, 0x00011110); |
| 491 | |
| 492 | rsnd_mod_write(mod, SRC_BSDSR, 0x01800000); |
| 493 | rsnd_mod_write(mod, SRC_BSISR, 0x00100060); |
| 494 | |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | static int rsnd_scu_set_convert_timing_gen2(struct rsnd_mod *mod, |
| 499 | struct rsnd_dai *rdai, |
| 500 | struct rsnd_dai_stream *io) |
| 501 | { |
| 502 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
| 503 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); |
| 504 | u32 convert_rate = rsnd_scu_convert_rate(scu); |
| 505 | int ret; |
| 506 | |
| 507 | if (convert_rate) |
| 508 | ret = rsnd_adg_set_convert_clk_gen2(mod, rdai, io, |
| 509 | runtime->rate, |
| 510 | convert_rate); |
| 511 | else |
| 512 | ret = rsnd_adg_set_convert_timing_gen2(mod, rdai, io); |
| 513 | |
| 514 | return ret; |
| 515 | } |
| 516 | |
| 517 | static int rsnd_scu_init_gen2(struct rsnd_mod *mod, |
| 518 | struct rsnd_dai *rdai, |
| 519 | struct rsnd_dai_stream *io) |
| 520 | { |
| 521 | int ret; |
| 522 | |
| 523 | ret = rsnd_scu_init(mod, rdai, io); |
| 524 | if (ret < 0) |
| 525 | return ret; |
| 526 | |
| 527 | ret = rsnd_scu_set_convert_rate_gen2(mod, rdai, io); |
| 528 | if (ret < 0) |
| 529 | return ret; |
| 530 | |
| 531 | ret = rsnd_scu_set_convert_timing_gen2(mod, rdai, io); |
| 532 | if (ret < 0) |
| 533 | return ret; |
| 534 | |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | static int rsnd_scu_start_gen2(struct rsnd_mod *mod, |
| 539 | struct rsnd_dai *rdai, |
| 540 | struct rsnd_dai_stream *io) |
| 541 | { |
| 542 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); |
| 543 | |
| 544 | rsnd_dma_start(rsnd_mod_to_dma(&scu->mod)); |
| 545 | |
| 546 | rsnd_mod_write(mod, SSI_CTRL, 0x1); |
| 547 | rsnd_mod_write(mod, SRC_CTRL, 0x11); |
| 548 | |
| 549 | return rsnd_scu_start(mod, rdai, io); |
| 550 | } |
| 551 | |
| 552 | static int rsnd_scu_stop_gen2(struct rsnd_mod *mod, |
| 553 | struct rsnd_dai *rdai, |
| 554 | struct rsnd_dai_stream *io) |
| 555 | { |
| 556 | struct rsnd_scu *scu = rsnd_mod_to_scu(mod); |
| 557 | |
| 558 | rsnd_mod_write(mod, SSI_CTRL, 0); |
| 559 | rsnd_mod_write(mod, SRC_CTRL, 0); |
| 560 | |
| 561 | rsnd_dma_stop(rsnd_mod_to_dma(&scu->mod)); |
| 562 | |
| 563 | return rsnd_scu_stop(mod, rdai, io); |
| 564 | } |
| 565 | |
| 566 | static struct rsnd_mod_ops rsnd_scu_gen2_ops = { |
| 567 | .name = "scu (gen2)", |
| 568 | .init = rsnd_scu_init_gen2, |
| 569 | .quit = rsnd_scu_quit, |
| 570 | .start = rsnd_scu_start_gen2, |
| 571 | .stop = rsnd_scu_stop_gen2, |
| 572 | }; |
| 573 | |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 574 | static int rsnd_scu_start_non_gen2(struct rsnd_mod *mod, |
| 575 | struct rsnd_dai *rdai, |
| 576 | struct rsnd_dai_stream *io) |
| 577 | { |
| 578 | /* enable PIO interrupt */ |
| 579 | rsnd_mod_write(mod, INT_ENABLE, 0x0f000000); |
| 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | static struct rsnd_mod_ops rsnd_scu_non_gen2_ops = { |
| 585 | .name = "non-scu (gen2)", |
| 586 | .init = rsnd_scu_ssi_mode_init, |
| 587 | .start = rsnd_scu_start_non_gen2, |
| 588 | }; |
| 589 | |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 590 | struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id) |
| 591 | { |
Takashi Iwai | 8b14719 | 2013-11-05 18:40:05 +0100 | [diff] [blame] | 592 | if (WARN_ON(id < 0 || id >= rsnd_scu_nr(priv))) |
| 593 | id = 0; |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 594 | |
| 595 | return &((struct rsnd_scu *)(priv->scu) + id)->mod; |
| 596 | } |
| 597 | |
| 598 | int rsnd_scu_probe(struct platform_device *pdev, |
| 599 | struct rcar_snd_info *info, |
| 600 | struct rsnd_priv *priv) |
| 601 | { |
| 602 | struct device *dev = rsnd_priv_to_dev(priv); |
| 603 | struct rsnd_scu *scu; |
Kuninori Morimoto | 013f38f | 2014-01-23 18:38:26 -0800 | [diff] [blame] | 604 | struct rsnd_mod_ops *ops; |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 605 | struct clk *clk; |
| 606 | char name[RSND_SCU_NAME_SIZE]; |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 607 | int i, nr; |
| 608 | |
| 609 | /* |
| 610 | * init SCU |
| 611 | */ |
| 612 | nr = info->scu_info_nr; |
| 613 | scu = devm_kzalloc(dev, sizeof(*scu) * nr, GFP_KERNEL); |
| 614 | if (!scu) { |
| 615 | dev_err(dev, "SCU allocate failed\n"); |
| 616 | return -ENOMEM; |
| 617 | } |
| 618 | |
| 619 | priv->scu_nr = nr; |
| 620 | priv->scu = scu; |
| 621 | |
| 622 | for_each_rsnd_scu(scu, priv, i) { |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 623 | snprintf(name, RSND_SCU_NAME_SIZE, "scu.%d", i); |
| 624 | |
| 625 | clk = devm_clk_get(dev, name); |
| 626 | if (IS_ERR(clk)) |
| 627 | return PTR_ERR(clk); |
| 628 | |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 629 | scu->info = &info->scu_info[i]; |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 630 | scu->clk = clk; |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 631 | |
Kuninori Morimoto | 013f38f | 2014-01-23 18:38:26 -0800 | [diff] [blame] | 632 | ops = &rsnd_scu_non_ops; |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 633 | if (rsnd_scu_hpbif_is_enable(scu)) { |
| 634 | if (rsnd_is_gen1(priv)) |
| 635 | ops = &rsnd_scu_gen1_ops; |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 636 | if (rsnd_is_gen2(priv)) { |
| 637 | struct rsnd_mod *ssi = rsnd_ssi_mod_get(priv, i); |
| 638 | int ret = rsnd_dma_init(priv, |
| 639 | rsnd_mod_to_dma(&scu->mod), |
| 640 | rsnd_ssi_is_play(ssi), |
| 641 | scu->info->dma_id); |
| 642 | if (ret < 0) |
| 643 | return ret; |
| 644 | |
| 645 | ops = &rsnd_scu_gen2_ops; |
| 646 | } |
Kuninori Morimoto | 1b7b08ef | 2014-01-23 18:41:36 -0800 | [diff] [blame] | 647 | } else { |
| 648 | if (rsnd_is_gen1(priv)) |
| 649 | ops = &rsnd_scu_non_gen1_ops; |
| 650 | if (rsnd_is_gen2(priv)) |
| 651 | ops = &rsnd_scu_non_gen2_ops; |
| 652 | } |
Kuninori Morimoto | 013f38f | 2014-01-23 18:38:26 -0800 | [diff] [blame] | 653 | |
| 654 | rsnd_mod_init(priv, &scu->mod, ops, i); |
| 655 | |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 656 | dev_dbg(dev, "SCU%d probed\n", i); |
| 657 | } |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 658 | dev_dbg(dev, "scu probed\n"); |
| 659 | |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | void rsnd_scu_remove(struct platform_device *pdev, |
| 664 | struct rsnd_priv *priv) |
| 665 | { |
Kuninori Morimoto | 629509c | 2014-01-23 18:42:00 -0800 | [diff] [blame] | 666 | struct rsnd_scu *scu; |
| 667 | int i; |
| 668 | |
| 669 | for_each_rsnd_scu(scu, priv, i) { |
| 670 | if (rsnd_scu_dma_available(scu)) |
| 671 | rsnd_dma_quit(priv, rsnd_mod_to_dma(&scu->mod)); |
| 672 | } |
Kuninori Morimoto | 07539c1 | 2013-07-21 21:36:35 -0700 | [diff] [blame] | 673 | } |