1 /* 2 * ctu.c 3 * 4 * Copyright (c) 2015 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 as 8 * published by the Free Software Foundation. 9 */ 10 #include "rsnd.h" 11 12 #define CTU_NAME_SIZE 16 13 #define CTU_NAME "ctu" 14 15 /* 16 * User needs to setup CTU by amixer, and its settings are 17 * based on below registers 18 * 19 * CTUn_CPMDR : amixser set "CTU Pass" 20 * CTUn_SV0xR : amixser set "CTU SV0" 21 * CTUn_SV1xR : amixser set "CTU SV1" 22 * CTUn_SV2xR : amixser set "CTU SV2" 23 * CTUn_SV3xR : amixser set "CTU SV3" 24 * 25 * [CTU Pass] 26 * 0000: default 27 * 0001: Connect input data of channel 0 28 * 0010: Connect input data of channel 1 29 * 0011: Connect input data of channel 2 30 * 0100: Connect input data of channel 3 31 * 0101: Connect input data of channel 4 32 * 0110: Connect input data of channel 5 33 * 0111: Connect input data of channel 6 34 * 1000: Connect input data of channel 7 35 * 1001: Connect calculated data by scale values of matrix row 0 36 * 1010: Connect calculated data by scale values of matrix row 1 37 * 1011: Connect calculated data by scale values of matrix row 2 38 * 1100: Connect calculated data by scale values of matrix row 3 39 * 40 * [CTU SVx] 41 * [Output0] = [SV00, SV01, SV02, SV03, SV04, SV05, SV06, SV07] 42 * [Output1] = [SV10, SV11, SV12, SV13, SV14, SV15, SV16, SV17] 43 * [Output2] = [SV20, SV21, SV22, SV23, SV24, SV25, SV26, SV27] 44 * [Output3] = [SV30, SV31, SV32, SV33, SV34, SV35, SV36, SV37] 45 * [Output4] = [ 0, 0, 0, 0, 0, 0, 0, 0 ] 46 * [Output5] = [ 0, 0, 0, 0, 0, 0, 0, 0 ] 47 * [Output6] = [ 0, 0, 0, 0, 0, 0, 0, 0 ] 48 * [Output7] = [ 0, 0, 0, 0, 0, 0, 0, 0 ] 49 * 50 * [SVxx] 51 * Plus Minus 52 * value time dB value time dB 53 * ----------------------------------------------------------------------- 54 * H'7F_FFFF 2 6 H'80_0000 2 6 55 * ... 56 * H'40_0000 1 0 H'C0_0000 1 0 57 * ... 58 * H'00_0001 2.38 x 10^-7 -132 59 * H'00_0000 0 Mute H'FF_FFFF 2.38 x 10^-7 -132 60 * 61 * 62 * Ex) Input ch -> Output ch 63 * 1ch -> 0ch 64 * 0ch -> 1ch 65 * 66 * amixer set "CTU Reset" on 67 * amixer set "CTU Pass" 9,10 68 * amixer set "CTU SV0" 0,4194304 69 * amixer set "CTU SV1" 4194304,0 70 * or 71 * amixer set "CTU Reset" on 72 * amixer set "CTU Pass" 2,1 73 */ 74 75 struct rsnd_ctu { 76 struct rsnd_mod mod; 77 struct rsnd_kctrl_cfg_m pass; 78 struct rsnd_kctrl_cfg_m sv0; 79 struct rsnd_kctrl_cfg_m sv1; 80 struct rsnd_kctrl_cfg_m sv2; 81 struct rsnd_kctrl_cfg_m sv3; 82 struct rsnd_kctrl_cfg_s reset; 83 int channels; 84 u32 flags; 85 }; 86 87 #define KCTRL_INITIALIZED (1 << 0) 88 89 #define rsnd_ctu_nr(priv) ((priv)->ctu_nr) 90 #define for_each_rsnd_ctu(pos, priv, i) \ 91 for ((i) = 0; \ 92 ((i) < rsnd_ctu_nr(priv)) && \ 93 ((pos) = (struct rsnd_ctu *)(priv)->ctu + i); \ 94 i++) 95 96 #define rsnd_mod_to_ctu(_mod) \ 97 container_of((_mod), struct rsnd_ctu, mod) 98 99 #define rsnd_ctu_get(priv, id) ((struct rsnd_ctu *)(priv->ctu) + id) 100 101 static void rsnd_ctu_activation(struct rsnd_mod *mod) 102 { 103 rsnd_mod_write(mod, CTU_SWRSR, 0); 104 rsnd_mod_write(mod, CTU_SWRSR, 1); 105 } 106 107 static void rsnd_ctu_halt(struct rsnd_mod *mod) 108 { 109 rsnd_mod_write(mod, CTU_CTUIR, 1); 110 rsnd_mod_write(mod, CTU_SWRSR, 0); 111 } 112 113 int rsnd_ctu_converted_channel(struct rsnd_mod *mod) 114 { 115 struct rsnd_ctu *ctu = rsnd_mod_to_ctu(mod); 116 117 return ctu->channels; 118 } 119 120 static int rsnd_ctu_probe_(struct rsnd_mod *mod, 121 struct rsnd_dai_stream *io, 122 struct rsnd_priv *priv) 123 { 124 return rsnd_cmd_attach(io, rsnd_mod_id(mod) / 4); 125 } 126 127 static void rsnd_ctu_value_init(struct rsnd_dai_stream *io, 128 struct rsnd_mod *mod) 129 { 130 struct rsnd_ctu *ctu = rsnd_mod_to_ctu(mod); 131 u32 cpmdr = 0; 132 u32 scmdr = 0; 133 int i; 134 135 for (i = 0; i < RSND_MAX_CHANNELS; i++) { 136 u32 val = rsnd_kctrl_valm(ctu->pass, i); 137 138 cpmdr |= val << (28 - (i * 4)); 139 140 if ((val > 0x8) && (scmdr < (val - 0x8))) 141 scmdr = val - 0x8; 142 } 143 144 rsnd_mod_write(mod, CTU_CTUIR, 1); 145 146 rsnd_mod_write(mod, CTU_ADINR, rsnd_runtime_channel_original(io)); 147 148 rsnd_mod_write(mod, CTU_CPMDR, cpmdr); 149 150 rsnd_mod_write(mod, CTU_SCMDR, scmdr); 151 152 if (scmdr > 0) { 153 rsnd_mod_write(mod, CTU_SV00R, rsnd_kctrl_valm(ctu->sv0, 0)); 154 rsnd_mod_write(mod, CTU_SV01R, rsnd_kctrl_valm(ctu->sv0, 1)); 155 rsnd_mod_write(mod, CTU_SV02R, rsnd_kctrl_valm(ctu->sv0, 2)); 156 rsnd_mod_write(mod, CTU_SV03R, rsnd_kctrl_valm(ctu->sv0, 3)); 157 rsnd_mod_write(mod, CTU_SV04R, rsnd_kctrl_valm(ctu->sv0, 4)); 158 rsnd_mod_write(mod, CTU_SV05R, rsnd_kctrl_valm(ctu->sv0, 5)); 159 rsnd_mod_write(mod, CTU_SV06R, rsnd_kctrl_valm(ctu->sv0, 6)); 160 rsnd_mod_write(mod, CTU_SV07R, rsnd_kctrl_valm(ctu->sv0, 7)); 161 } 162 if (scmdr > 1) { 163 rsnd_mod_write(mod, CTU_SV10R, rsnd_kctrl_valm(ctu->sv1, 0)); 164 rsnd_mod_write(mod, CTU_SV11R, rsnd_kctrl_valm(ctu->sv1, 1)); 165 rsnd_mod_write(mod, CTU_SV12R, rsnd_kctrl_valm(ctu->sv1, 2)); 166 rsnd_mod_write(mod, CTU_SV13R, rsnd_kctrl_valm(ctu->sv1, 3)); 167 rsnd_mod_write(mod, CTU_SV14R, rsnd_kctrl_valm(ctu->sv1, 4)); 168 rsnd_mod_write(mod, CTU_SV15R, rsnd_kctrl_valm(ctu->sv1, 5)); 169 rsnd_mod_write(mod, CTU_SV16R, rsnd_kctrl_valm(ctu->sv1, 6)); 170 rsnd_mod_write(mod, CTU_SV17R, rsnd_kctrl_valm(ctu->sv1, 7)); 171 } 172 if (scmdr > 2) { 173 rsnd_mod_write(mod, CTU_SV20R, rsnd_kctrl_valm(ctu->sv2, 0)); 174 rsnd_mod_write(mod, CTU_SV21R, rsnd_kctrl_valm(ctu->sv2, 1)); 175 rsnd_mod_write(mod, CTU_SV22R, rsnd_kctrl_valm(ctu->sv2, 2)); 176 rsnd_mod_write(mod, CTU_SV23R, rsnd_kctrl_valm(ctu->sv2, 3)); 177 rsnd_mod_write(mod, CTU_SV24R, rsnd_kctrl_valm(ctu->sv2, 4)); 178 rsnd_mod_write(mod, CTU_SV25R, rsnd_kctrl_valm(ctu->sv2, 5)); 179 rsnd_mod_write(mod, CTU_SV26R, rsnd_kctrl_valm(ctu->sv2, 6)); 180 rsnd_mod_write(mod, CTU_SV27R, rsnd_kctrl_valm(ctu->sv2, 7)); 181 } 182 if (scmdr > 3) { 183 rsnd_mod_write(mod, CTU_SV30R, rsnd_kctrl_valm(ctu->sv3, 0)); 184 rsnd_mod_write(mod, CTU_SV31R, rsnd_kctrl_valm(ctu->sv3, 1)); 185 rsnd_mod_write(mod, CTU_SV32R, rsnd_kctrl_valm(ctu->sv3, 2)); 186 rsnd_mod_write(mod, CTU_SV33R, rsnd_kctrl_valm(ctu->sv3, 3)); 187 rsnd_mod_write(mod, CTU_SV34R, rsnd_kctrl_valm(ctu->sv3, 4)); 188 rsnd_mod_write(mod, CTU_SV35R, rsnd_kctrl_valm(ctu->sv3, 5)); 189 rsnd_mod_write(mod, CTU_SV36R, rsnd_kctrl_valm(ctu->sv3, 6)); 190 rsnd_mod_write(mod, CTU_SV37R, rsnd_kctrl_valm(ctu->sv3, 7)); 191 } 192 193 rsnd_mod_write(mod, CTU_CTUIR, 0); 194 } 195 196 static void rsnd_ctu_value_reset(struct rsnd_dai_stream *io, 197 struct rsnd_mod *mod) 198 { 199 struct rsnd_ctu *ctu = rsnd_mod_to_ctu(mod); 200 int i; 201 202 if (!rsnd_kctrl_vals(ctu->reset)) 203 return; 204 205 for (i = 0; i < RSND_MAX_CHANNELS; i++) { 206 rsnd_kctrl_valm(ctu->pass, i) = 0; 207 rsnd_kctrl_valm(ctu->sv0, i) = 0; 208 rsnd_kctrl_valm(ctu->sv1, i) = 0; 209 rsnd_kctrl_valm(ctu->sv2, i) = 0; 210 rsnd_kctrl_valm(ctu->sv3, i) = 0; 211 } 212 rsnd_kctrl_vals(ctu->reset) = 0; 213 } 214 215 static int rsnd_ctu_init(struct rsnd_mod *mod, 216 struct rsnd_dai_stream *io, 217 struct rsnd_priv *priv) 218 { 219 rsnd_mod_power_on(mod); 220 221 rsnd_ctu_activation(mod); 222 223 rsnd_ctu_value_init(io, mod); 224 225 return 0; 226 } 227 228 static int rsnd_ctu_quit(struct rsnd_mod *mod, 229 struct rsnd_dai_stream *io, 230 struct rsnd_priv *priv) 231 { 232 rsnd_ctu_halt(mod); 233 234 rsnd_mod_power_off(mod); 235 236 return 0; 237 } 238 239 static int rsnd_ctu_hw_params(struct rsnd_mod *mod, 240 struct rsnd_dai_stream *io, 241 struct snd_pcm_substream *substream, 242 struct snd_pcm_hw_params *fe_params) 243 { 244 struct rsnd_ctu *ctu = rsnd_mod_to_ctu(mod); 245 struct snd_soc_pcm_runtime *fe = substream->private_data; 246 247 /* 248 * CTU assumes that it is used under DPCM if user want to use 249 * channel transfer. Then, CTU should be FE. 250 * And then, this function will be called *after* BE settings. 251 * this means, each BE already has fixuped hw_params. 252 * see 253 * dpcm_fe_dai_hw_params() 254 * dpcm_be_dai_hw_params() 255 */ 256 ctu->channels = 0; 257 if (fe->dai_link->dynamic) { 258 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); 259 struct device *dev = rsnd_priv_to_dev(priv); 260 struct snd_soc_dpcm *dpcm; 261 struct snd_pcm_hw_params *be_params; 262 int stream = substream->stream; 263 264 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) { 265 be_params = &dpcm->hw_params; 266 if (params_channels(fe_params) != params_channels(be_params)) 267 ctu->channels = params_channels(be_params); 268 } 269 270 dev_dbg(dev, "CTU convert channels %d\n", ctu->channels); 271 } 272 273 return 0; 274 } 275 276 static int rsnd_ctu_pcm_new(struct rsnd_mod *mod, 277 struct rsnd_dai_stream *io, 278 struct snd_soc_pcm_runtime *rtd) 279 { 280 struct rsnd_ctu *ctu = rsnd_mod_to_ctu(mod); 281 int ret; 282 283 if (rsnd_flags_has(ctu, KCTRL_INITIALIZED)) 284 return 0; 285 286 /* CTU Pass */ 287 ret = rsnd_kctrl_new_m(mod, io, rtd, "CTU Pass", 288 rsnd_kctrl_accept_anytime, 289 NULL, 290 &ctu->pass, RSND_MAX_CHANNELS, 291 0xC); 292 293 /* ROW0 */ 294 ret = rsnd_kctrl_new_m(mod, io, rtd, "CTU SV0", 295 rsnd_kctrl_accept_anytime, 296 NULL, 297 &ctu->sv0, RSND_MAX_CHANNELS, 298 0x00FFFFFF); 299 if (ret < 0) 300 return ret; 301 302 /* ROW1 */ 303 ret = rsnd_kctrl_new_m(mod, io, rtd, "CTU SV1", 304 rsnd_kctrl_accept_anytime, 305 NULL, 306 &ctu->sv1, RSND_MAX_CHANNELS, 307 0x00FFFFFF); 308 if (ret < 0) 309 return ret; 310 311 /* ROW2 */ 312 ret = rsnd_kctrl_new_m(mod, io, rtd, "CTU SV2", 313 rsnd_kctrl_accept_anytime, 314 NULL, 315 &ctu->sv2, RSND_MAX_CHANNELS, 316 0x00FFFFFF); 317 if (ret < 0) 318 return ret; 319 320 /* ROW3 */ 321 ret = rsnd_kctrl_new_m(mod, io, rtd, "CTU SV3", 322 rsnd_kctrl_accept_anytime, 323 NULL, 324 &ctu->sv3, RSND_MAX_CHANNELS, 325 0x00FFFFFF); 326 if (ret < 0) 327 return ret; 328 329 /* Reset */ 330 ret = rsnd_kctrl_new_s(mod, io, rtd, "CTU Reset", 331 rsnd_kctrl_accept_anytime, 332 rsnd_ctu_value_reset, 333 &ctu->reset, 1); 334 335 rsnd_flags_set(ctu, KCTRL_INITIALIZED); 336 337 return ret; 338 } 339 340 static struct rsnd_mod_ops rsnd_ctu_ops = { 341 .name = CTU_NAME, 342 .probe = rsnd_ctu_probe_, 343 .init = rsnd_ctu_init, 344 .quit = rsnd_ctu_quit, 345 .hw_params = rsnd_ctu_hw_params, 346 .pcm_new = rsnd_ctu_pcm_new, 347 }; 348 349 struct rsnd_mod *rsnd_ctu_mod_get(struct rsnd_priv *priv, int id) 350 { 351 if (WARN_ON(id < 0 || id >= rsnd_ctu_nr(priv))) 352 id = 0; 353 354 return rsnd_mod_get(rsnd_ctu_get(priv, id)); 355 } 356 357 int rsnd_ctu_probe(struct rsnd_priv *priv) 358 { 359 struct device_node *node; 360 struct device_node *np; 361 struct device *dev = rsnd_priv_to_dev(priv); 362 struct rsnd_ctu *ctu; 363 struct clk *clk; 364 char name[CTU_NAME_SIZE]; 365 int i, nr, ret; 366 367 /* This driver doesn't support Gen1 at this point */ 368 if (rsnd_is_gen1(priv)) 369 return 0; 370 371 node = rsnd_ctu_of_node(priv); 372 if (!node) 373 return 0; /* not used is not error */ 374 375 nr = of_get_child_count(node); 376 if (!nr) { 377 ret = -EINVAL; 378 goto rsnd_ctu_probe_done; 379 } 380 381 ctu = devm_kzalloc(dev, sizeof(*ctu) * nr, GFP_KERNEL); 382 if (!ctu) { 383 ret = -ENOMEM; 384 goto rsnd_ctu_probe_done; 385 } 386 387 priv->ctu_nr = nr; 388 priv->ctu = ctu; 389 390 i = 0; 391 ret = 0; 392 for_each_child_of_node(node, np) { 393 ctu = rsnd_ctu_get(priv, i); 394 395 /* 396 * CTU00, CTU01, CTU02, CTU03 => CTU0 397 * CTU10, CTU11, CTU12, CTU13 => CTU1 398 */ 399 snprintf(name, CTU_NAME_SIZE, "%s.%d", 400 CTU_NAME, i / 4); 401 402 clk = devm_clk_get(dev, name); 403 if (IS_ERR(clk)) { 404 ret = PTR_ERR(clk); 405 of_node_put(np); 406 goto rsnd_ctu_probe_done; 407 } 408 409 ret = rsnd_mod_init(priv, rsnd_mod_get(ctu), &rsnd_ctu_ops, 410 clk, rsnd_mod_get_status, RSND_MOD_CTU, i); 411 if (ret) { 412 of_node_put(np); 413 goto rsnd_ctu_probe_done; 414 } 415 416 i++; 417 } 418 419 420 rsnd_ctu_probe_done: 421 of_node_put(node); 422 423 return ret; 424 } 425 426 void rsnd_ctu_remove(struct rsnd_priv *priv) 427 { 428 struct rsnd_ctu *ctu; 429 int i; 430 431 for_each_rsnd_ctu(ctu, priv, i) { 432 rsnd_mod_quit(rsnd_mod_get(ctu)); 433 } 434 } 435