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