4 * Copyright (c) 2015 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
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.
12 #define CTU_NAME_SIZE 16
13 #define CTU_NAME "ctu"
16 * User needs to setup CTU by amixer, and its settings are
17 * based on below registers
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"
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
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 ]
52 * value time dB value time dB
53 * -----------------------------------------------------------------------
54 * H'7F_FFFF 2 6 H'80_0000 2 6
56 * H'40_0000 1 0 H'C0_0000 1 0
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
62 * Ex) Input ch -> Output ch
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
71 * amixer set "CTU Reset" on
72 * amixer set "CTU Pass" 2,1
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
;
86 #define rsnd_ctu_nr(priv) ((priv)->ctu_nr)
87 #define for_each_rsnd_ctu(pos, priv, i) \
89 ((i) < rsnd_ctu_nr(priv)) && \
90 ((pos) = (struct rsnd_ctu *)(priv)->ctu + i); \
93 #define rsnd_mod_to_ctu(_mod) \
94 container_of((_mod), struct rsnd_ctu, mod)
96 #define rsnd_ctu_get(priv, id) ((struct rsnd_ctu *)(priv->ctu) + id)
98 static void rsnd_ctu_activation(struct rsnd_mod
*mod
)
100 rsnd_mod_write(mod
, CTU_SWRSR
, 0);
101 rsnd_mod_write(mod
, CTU_SWRSR
, 1);
104 static void rsnd_ctu_halt(struct rsnd_mod
*mod
)
106 rsnd_mod_write(mod
, CTU_CTUIR
, 1);
107 rsnd_mod_write(mod
, CTU_SWRSR
, 0);
110 int rsnd_ctu_converted_channel(struct rsnd_mod
*mod
)
112 struct rsnd_ctu
*ctu
= rsnd_mod_to_ctu(mod
);
114 return ctu
->channels
;
117 static int rsnd_ctu_probe_(struct rsnd_mod
*mod
,
118 struct rsnd_dai_stream
*io
,
119 struct rsnd_priv
*priv
)
121 return rsnd_cmd_attach(io
, rsnd_mod_id(mod
) / 4);
124 static void rsnd_ctu_value_init(struct rsnd_dai_stream
*io
,
125 struct rsnd_mod
*mod
)
127 struct rsnd_ctu
*ctu
= rsnd_mod_to_ctu(mod
);
132 for (i
= 0; i
< RSND_MAX_CHANNELS
; i
++) {
133 u32 val
= ctu
->pass
.val
[i
];
135 cpmdr
|= val
<< (28 - (i
* 4));
137 if ((val
> 0x8) && (scmdr
< (val
- 0x8)))
141 rsnd_mod_write(mod
, CTU_CTUIR
, 1);
143 rsnd_mod_write(mod
, CTU_ADINR
, rsnd_runtime_channel_original(io
));
145 rsnd_mod_write(mod
, CTU_CPMDR
, cpmdr
);
147 rsnd_mod_write(mod
, CTU_SCMDR
, scmdr
);
150 rsnd_mod_write(mod
, CTU_SV00R
, ctu
->sv0
.val
[0]);
151 rsnd_mod_write(mod
, CTU_SV01R
, ctu
->sv0
.val
[1]);
152 rsnd_mod_write(mod
, CTU_SV02R
, ctu
->sv0
.val
[2]);
153 rsnd_mod_write(mod
, CTU_SV03R
, ctu
->sv0
.val
[3]);
154 rsnd_mod_write(mod
, CTU_SV04R
, ctu
->sv0
.val
[4]);
155 rsnd_mod_write(mod
, CTU_SV05R
, ctu
->sv0
.val
[5]);
156 rsnd_mod_write(mod
, CTU_SV06R
, ctu
->sv0
.val
[6]);
157 rsnd_mod_write(mod
, CTU_SV07R
, ctu
->sv0
.val
[7]);
160 rsnd_mod_write(mod
, CTU_SV10R
, ctu
->sv1
.val
[0]);
161 rsnd_mod_write(mod
, CTU_SV11R
, ctu
->sv1
.val
[1]);
162 rsnd_mod_write(mod
, CTU_SV12R
, ctu
->sv1
.val
[2]);
163 rsnd_mod_write(mod
, CTU_SV13R
, ctu
->sv1
.val
[3]);
164 rsnd_mod_write(mod
, CTU_SV14R
, ctu
->sv1
.val
[4]);
165 rsnd_mod_write(mod
, CTU_SV15R
, ctu
->sv1
.val
[5]);
166 rsnd_mod_write(mod
, CTU_SV16R
, ctu
->sv1
.val
[6]);
167 rsnd_mod_write(mod
, CTU_SV17R
, ctu
->sv1
.val
[7]);
170 rsnd_mod_write(mod
, CTU_SV20R
, ctu
->sv2
.val
[0]);
171 rsnd_mod_write(mod
, CTU_SV21R
, ctu
->sv2
.val
[1]);
172 rsnd_mod_write(mod
, CTU_SV22R
, ctu
->sv2
.val
[2]);
173 rsnd_mod_write(mod
, CTU_SV23R
, ctu
->sv2
.val
[3]);
174 rsnd_mod_write(mod
, CTU_SV24R
, ctu
->sv2
.val
[4]);
175 rsnd_mod_write(mod
, CTU_SV25R
, ctu
->sv2
.val
[5]);
176 rsnd_mod_write(mod
, CTU_SV26R
, ctu
->sv2
.val
[6]);
177 rsnd_mod_write(mod
, CTU_SV27R
, ctu
->sv2
.val
[7]);
180 rsnd_mod_write(mod
, CTU_SV30R
, ctu
->sv3
.val
[0]);
181 rsnd_mod_write(mod
, CTU_SV31R
, ctu
->sv3
.val
[1]);
182 rsnd_mod_write(mod
, CTU_SV32R
, ctu
->sv3
.val
[2]);
183 rsnd_mod_write(mod
, CTU_SV33R
, ctu
->sv3
.val
[3]);
184 rsnd_mod_write(mod
, CTU_SV34R
, ctu
->sv3
.val
[4]);
185 rsnd_mod_write(mod
, CTU_SV35R
, ctu
->sv3
.val
[5]);
186 rsnd_mod_write(mod
, CTU_SV36R
, ctu
->sv3
.val
[6]);
187 rsnd_mod_write(mod
, CTU_SV37R
, ctu
->sv3
.val
[7]);
190 rsnd_mod_write(mod
, CTU_CTUIR
, 0);
193 static void rsnd_ctu_value_reset(struct rsnd_dai_stream
*io
,
194 struct rsnd_mod
*mod
)
196 struct rsnd_ctu
*ctu
= rsnd_mod_to_ctu(mod
);
202 for (i
= 0; i
< RSND_MAX_CHANNELS
; i
++) {
203 ctu
->pass
.val
[i
] = 0;
212 static int rsnd_ctu_init(struct rsnd_mod
*mod
,
213 struct rsnd_dai_stream
*io
,
214 struct rsnd_priv
*priv
)
216 rsnd_mod_power_on(mod
);
218 rsnd_ctu_activation(mod
);
220 rsnd_ctu_value_init(io
, mod
);
225 static int rsnd_ctu_quit(struct rsnd_mod
*mod
,
226 struct rsnd_dai_stream
*io
,
227 struct rsnd_priv
*priv
)
231 rsnd_mod_power_off(mod
);
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
)
241 struct rsnd_ctu
*ctu
= rsnd_mod_to_ctu(mod
);
242 struct snd_soc_pcm_runtime
*fe
= substream
->private_data
;
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.
250 * dpcm_fe_dai_hw_params()
251 * dpcm_be_dai_hw_params()
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
;
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
);
267 dev_dbg(dev
, "CTU convert channels %d\n", ctu
->channels
);
273 static int rsnd_ctu_pcm_new(struct rsnd_mod
*mod
,
274 struct rsnd_dai_stream
*io
,
275 struct snd_soc_pcm_runtime
*rtd
)
277 struct rsnd_ctu
*ctu
= rsnd_mod_to_ctu(mod
);
281 ret
= rsnd_kctrl_new_m(mod
, io
, rtd
, "CTU Pass",
283 &ctu
->pass
, RSND_MAX_CHANNELS
,
287 ret
= rsnd_kctrl_new_m(mod
, io
, rtd
, "CTU SV0",
289 &ctu
->sv0
, RSND_MAX_CHANNELS
,
295 ret
= rsnd_kctrl_new_m(mod
, io
, rtd
, "CTU SV1",
297 &ctu
->sv1
, RSND_MAX_CHANNELS
,
303 ret
= rsnd_kctrl_new_m(mod
, io
, rtd
, "CTU SV2",
305 &ctu
->sv2
, RSND_MAX_CHANNELS
,
311 ret
= rsnd_kctrl_new_m(mod
, io
, rtd
, "CTU SV3",
313 &ctu
->sv3
, RSND_MAX_CHANNELS
,
319 ret
= rsnd_kctrl_new_s(mod
, io
, rtd
, "CTU Reset",
320 rsnd_ctu_value_reset
,
326 static struct rsnd_mod_ops rsnd_ctu_ops
= {
328 .probe
= rsnd_ctu_probe_
,
329 .init
= rsnd_ctu_init
,
330 .quit
= rsnd_ctu_quit
,
331 .hw_params
= rsnd_ctu_hw_params
,
332 .pcm_new
= rsnd_ctu_pcm_new
,
335 struct rsnd_mod
*rsnd_ctu_mod_get(struct rsnd_priv
*priv
, int id
)
337 if (WARN_ON(id
< 0 || id
>= rsnd_ctu_nr(priv
)))
340 return rsnd_mod_get(rsnd_ctu_get(priv
, id
));
343 int rsnd_ctu_probe(struct rsnd_priv
*priv
)
345 struct device_node
*node
;
346 struct device_node
*np
;
347 struct device
*dev
= rsnd_priv_to_dev(priv
);
348 struct rsnd_ctu
*ctu
;
350 char name
[CTU_NAME_SIZE
];
353 /* This driver doesn't support Gen1 at this point */
354 if (rsnd_is_gen1(priv
))
357 node
= rsnd_ctu_of_node(priv
);
359 return 0; /* not used is not error */
361 nr
= of_get_child_count(node
);
364 goto rsnd_ctu_probe_done
;
367 ctu
= devm_kzalloc(dev
, sizeof(*ctu
) * nr
, GFP_KERNEL
);
370 goto rsnd_ctu_probe_done
;
378 for_each_child_of_node(node
, np
) {
379 ctu
= rsnd_ctu_get(priv
, i
);
382 * CTU00, CTU01, CTU02, CTU03 => CTU0
383 * CTU10, CTU11, CTU12, CTU13 => CTU1
385 snprintf(name
, CTU_NAME_SIZE
, "%s.%d",
388 clk
= devm_clk_get(dev
, name
);
391 goto rsnd_ctu_probe_done
;
394 ret
= rsnd_mod_init(priv
, rsnd_mod_get(ctu
), &rsnd_ctu_ops
,
395 clk
, rsnd_mod_get_status
, RSND_MOD_CTU
, i
);
397 goto rsnd_ctu_probe_done
;
409 void rsnd_ctu_remove(struct rsnd_priv
*priv
)
411 struct rsnd_ctu
*ctu
;
414 for_each_rsnd_ctu(ctu
, priv
, i
) {
415 rsnd_mod_quit(rsnd_mod_get(ctu
));