Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // ctu.c
0004 //
0005 // Copyright (c) 2015 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
0006 
0007 #include "rsnd.h"
0008 
0009 #define CTU_NAME_SIZE   16
0010 #define CTU_NAME "ctu"
0011 
0012 /*
0013  * User needs to setup CTU by amixer, and its settings are
0014  * based on below registers
0015  *
0016  * CTUn_CPMDR : amixser set "CTU Pass"
0017  * CTUn_SV0xR : amixser set "CTU SV0"
0018  * CTUn_SV1xR : amixser set "CTU SV1"
0019  * CTUn_SV2xR : amixser set "CTU SV2"
0020  * CTUn_SV3xR : amixser set "CTU SV3"
0021  *
0022  * [CTU Pass]
0023  * 0000: default
0024  * 0001: Connect input data of channel 0
0025  * 0010: Connect input data of channel 1
0026  * 0011: Connect input data of channel 2
0027  * 0100: Connect input data of channel 3
0028  * 0101: Connect input data of channel 4
0029  * 0110: Connect input data of channel 5
0030  * 0111: Connect input data of channel 6
0031  * 1000: Connect input data of channel 7
0032  * 1001: Connect calculated data by scale values of matrix row 0
0033  * 1010: Connect calculated data by scale values of matrix row 1
0034  * 1011: Connect calculated data by scale values of matrix row 2
0035  * 1100: Connect calculated data by scale values of matrix row 3
0036  *
0037  * [CTU SVx]
0038  * [Output0] = [SV00, SV01, SV02, SV03, SV04, SV05, SV06, SV07]
0039  * [Output1] = [SV10, SV11, SV12, SV13, SV14, SV15, SV16, SV17]
0040  * [Output2] = [SV20, SV21, SV22, SV23, SV24, SV25, SV26, SV27]
0041  * [Output3] = [SV30, SV31, SV32, SV33, SV34, SV35, SV36, SV37]
0042  * [Output4] = [ 0,   0,    0,    0,    0,    0,    0,    0   ]
0043  * [Output5] = [ 0,   0,    0,    0,    0,    0,    0,    0   ]
0044  * [Output6] = [ 0,   0,    0,    0,    0,    0,    0,    0   ]
0045  * [Output7] = [ 0,   0,    0,    0,    0,    0,    0,    0   ]
0046  *
0047  * [SVxx]
0048  * Plus                 Minus
0049  * value    time        dB  value       time        dB
0050  * -----------------------------------------------------------------------
0051  * H'7F_FFFF    2       6   H'80_0000   2       6
0052  * ...
0053  * H'40_0000    1       0   H'C0_0000   1       0
0054  * ...
0055  * H'00_0001    2.38 x 10^-7    -132
0056  * H'00_0000    0       Mute    H'FF_FFFF   2.38 x 10^-7    -132
0057  *
0058  *
0059  * Ex) Input ch -> Output ch
0060  *  1ch     ->  0ch
0061  *  0ch     ->  1ch
0062  *
0063  *  amixer set "CTU Reset" on
0064  *  amixer set "CTU Pass" 9,10
0065  *  amixer set "CTU SV0" 0,4194304
0066  *  amixer set "CTU SV1" 4194304,0
0067  * or
0068  *  amixer set "CTU Reset" on
0069  *  amixer set "CTU Pass" 2,1
0070  */
0071 
0072 struct rsnd_ctu {
0073     struct rsnd_mod mod;
0074     struct rsnd_kctrl_cfg_m pass;
0075     struct rsnd_kctrl_cfg_m sv[4];
0076     struct rsnd_kctrl_cfg_s reset;
0077     int channels;
0078     u32 flags;
0079 };
0080 
0081 #define KCTRL_INITIALIZED   (1 << 0)
0082 
0083 #define rsnd_ctu_nr(priv) ((priv)->ctu_nr)
0084 #define for_each_rsnd_ctu(pos, priv, i)                 \
0085     for ((i) = 0;                           \
0086          ((i) < rsnd_ctu_nr(priv)) &&               \
0087              ((pos) = (struct rsnd_ctu *)(priv)->ctu + i);  \
0088          i++)
0089 
0090 #define rsnd_mod_to_ctu(_mod)   \
0091     container_of((_mod), struct rsnd_ctu, mod)
0092 
0093 #define rsnd_ctu_get(priv, id) ((struct rsnd_ctu *)(priv->ctu) + id)
0094 
0095 static void rsnd_ctu_activation(struct rsnd_mod *mod)
0096 {
0097     rsnd_mod_write(mod, CTU_SWRSR, 0);
0098     rsnd_mod_write(mod, CTU_SWRSR, 1);
0099 }
0100 
0101 static void rsnd_ctu_halt(struct rsnd_mod *mod)
0102 {
0103     rsnd_mod_write(mod, CTU_CTUIR, 1);
0104     rsnd_mod_write(mod, CTU_SWRSR, 0);
0105 }
0106 
0107 static int rsnd_ctu_probe_(struct rsnd_mod *mod,
0108                struct rsnd_dai_stream *io,
0109                struct rsnd_priv *priv)
0110 {
0111     return rsnd_cmd_attach(io, rsnd_mod_id(mod));
0112 }
0113 
0114 static void rsnd_ctu_value_init(struct rsnd_dai_stream *io,
0115                    struct rsnd_mod *mod)
0116 {
0117     struct rsnd_ctu *ctu = rsnd_mod_to_ctu(mod);
0118     u32 cpmdr = 0;
0119     u32 scmdr = 0;
0120     int i, j;
0121 
0122     for (i = 0; i < RSND_MAX_CHANNELS; i++) {
0123         u32 val = rsnd_kctrl_valm(ctu->pass, i);
0124 
0125         cpmdr |= val << (28 - (i * 4));
0126 
0127         if ((val > 0x8) && (scmdr < (val - 0x8)))
0128             scmdr = val - 0x8;
0129     }
0130 
0131     rsnd_mod_write(mod, CTU_CTUIR, 1);
0132 
0133     rsnd_mod_write(mod, CTU_ADINR, rsnd_runtime_channel_original(io));
0134 
0135     rsnd_mod_write(mod, CTU_CPMDR, cpmdr);
0136 
0137     rsnd_mod_write(mod, CTU_SCMDR, scmdr);
0138 
0139     for (i = 0; i < 4; i++) {
0140 
0141         if (i >= scmdr)
0142             break;
0143 
0144         for (j = 0; j < RSND_MAX_CHANNELS; j++)
0145             rsnd_mod_write(mod, CTU_SVxxR(i, j), rsnd_kctrl_valm(ctu->sv[i], j));
0146     }
0147 
0148     rsnd_mod_write(mod, CTU_CTUIR, 0);
0149 }
0150 
0151 static void rsnd_ctu_value_reset(struct rsnd_dai_stream *io,
0152                  struct rsnd_mod *mod)
0153 {
0154     struct rsnd_ctu *ctu = rsnd_mod_to_ctu(mod);
0155     int i;
0156 
0157     if (!rsnd_kctrl_vals(ctu->reset))
0158         return;
0159 
0160     for (i = 0; i < RSND_MAX_CHANNELS; i++) {
0161         rsnd_kctrl_valm(ctu->pass, i) = 0;
0162         rsnd_kctrl_valm(ctu->sv[0],  i) = 0;
0163         rsnd_kctrl_valm(ctu->sv[1],  i) = 0;
0164         rsnd_kctrl_valm(ctu->sv[2],  i) = 0;
0165         rsnd_kctrl_valm(ctu->sv[3],  i) = 0;
0166     }
0167     rsnd_kctrl_vals(ctu->reset) = 0;
0168 }
0169 
0170 static int rsnd_ctu_init(struct rsnd_mod *mod,
0171              struct rsnd_dai_stream *io,
0172              struct rsnd_priv *priv)
0173 {
0174     rsnd_mod_power_on(mod);
0175 
0176     rsnd_ctu_activation(mod);
0177 
0178     rsnd_ctu_value_init(io, mod);
0179 
0180     return 0;
0181 }
0182 
0183 static int rsnd_ctu_quit(struct rsnd_mod *mod,
0184              struct rsnd_dai_stream *io,
0185              struct rsnd_priv *priv)
0186 {
0187     rsnd_ctu_halt(mod);
0188 
0189     rsnd_mod_power_off(mod);
0190 
0191     return 0;
0192 }
0193 
0194 static int rsnd_ctu_pcm_new(struct rsnd_mod *mod,
0195                 struct rsnd_dai_stream *io,
0196                 struct snd_soc_pcm_runtime *rtd)
0197 {
0198     struct rsnd_ctu *ctu = rsnd_mod_to_ctu(mod);
0199     int ret;
0200 
0201     if (rsnd_flags_has(ctu, KCTRL_INITIALIZED))
0202         return 0;
0203 
0204     /* CTU Pass */
0205     ret = rsnd_kctrl_new_m(mod, io, rtd, "CTU Pass",
0206                    rsnd_kctrl_accept_anytime,
0207                    NULL,
0208                    &ctu->pass, RSND_MAX_CHANNELS,
0209                    0xC);
0210     if (ret < 0)
0211         return ret;
0212 
0213     /* ROW0 */
0214     ret = rsnd_kctrl_new_m(mod, io, rtd, "CTU SV0",
0215                    rsnd_kctrl_accept_anytime,
0216                    NULL,
0217                    &ctu->sv[0], RSND_MAX_CHANNELS,
0218                    0x00FFFFFF);
0219     if (ret < 0)
0220         return ret;
0221 
0222     /* ROW1 */
0223     ret = rsnd_kctrl_new_m(mod, io, rtd, "CTU SV1",
0224                    rsnd_kctrl_accept_anytime,
0225                    NULL,
0226                    &ctu->sv[1], RSND_MAX_CHANNELS,
0227                    0x00FFFFFF);
0228     if (ret < 0)
0229         return ret;
0230 
0231     /* ROW2 */
0232     ret = rsnd_kctrl_new_m(mod, io, rtd, "CTU SV2",
0233                    rsnd_kctrl_accept_anytime,
0234                    NULL,
0235                    &ctu->sv[2], RSND_MAX_CHANNELS,
0236                    0x00FFFFFF);
0237     if (ret < 0)
0238         return ret;
0239 
0240     /* ROW3 */
0241     ret = rsnd_kctrl_new_m(mod, io, rtd, "CTU SV3",
0242                    rsnd_kctrl_accept_anytime,
0243                    NULL,
0244                    &ctu->sv[3], RSND_MAX_CHANNELS,
0245                    0x00FFFFFF);
0246     if (ret < 0)
0247         return ret;
0248 
0249     /* Reset */
0250     ret = rsnd_kctrl_new_s(mod, io, rtd, "CTU Reset",
0251                    rsnd_kctrl_accept_anytime,
0252                    rsnd_ctu_value_reset,
0253                    &ctu->reset, 1);
0254 
0255     rsnd_flags_set(ctu, KCTRL_INITIALIZED);
0256 
0257     return ret;
0258 }
0259 
0260 static int rsnd_ctu_id(struct rsnd_mod *mod)
0261 {
0262     /*
0263      * ctu00: -> 0, ctu01: -> 0, ctu02: -> 0, ctu03: -> 0
0264      * ctu10: -> 1, ctu11: -> 1, ctu12: -> 1, ctu13: -> 1
0265      */
0266     return mod->id / 4;
0267 }
0268 
0269 static int rsnd_ctu_id_sub(struct rsnd_mod *mod)
0270 {
0271     /*
0272      * ctu00: -> 0, ctu01: -> 1, ctu02: -> 2, ctu03: -> 3
0273      * ctu10: -> 0, ctu11: -> 1, ctu12: -> 2, ctu13: -> 3
0274      */
0275     return mod->id % 4;
0276 }
0277 
0278 #ifdef CONFIG_DEBUG_FS
0279 static void rsnd_ctu_debug_info(struct seq_file *m,
0280                 struct rsnd_dai_stream *io,
0281                 struct rsnd_mod *mod)
0282 {
0283     rsnd_debugfs_mod_reg_show(m, mod, RSND_GEN2_SCU,
0284                   0x500 + rsnd_mod_id_raw(mod) * 0x100, 0x100);
0285 }
0286 #define DEBUG_INFO .debug_info = rsnd_ctu_debug_info
0287 #else
0288 #define DEBUG_INFO
0289 #endif
0290 
0291 static struct rsnd_mod_ops rsnd_ctu_ops = {
0292     .name       = CTU_NAME,
0293     .probe      = rsnd_ctu_probe_,
0294     .init       = rsnd_ctu_init,
0295     .quit       = rsnd_ctu_quit,
0296     .pcm_new    = rsnd_ctu_pcm_new,
0297     .get_status = rsnd_mod_get_status,
0298     .id     = rsnd_ctu_id,
0299     .id_sub     = rsnd_ctu_id_sub,
0300     .id_cmd     = rsnd_mod_id_raw,
0301     DEBUG_INFO
0302 };
0303 
0304 struct rsnd_mod *rsnd_ctu_mod_get(struct rsnd_priv *priv, int id)
0305 {
0306     if (WARN_ON(id < 0 || id >= rsnd_ctu_nr(priv)))
0307         id = 0;
0308 
0309     return rsnd_mod_get(rsnd_ctu_get(priv, id));
0310 }
0311 
0312 int rsnd_ctu_probe(struct rsnd_priv *priv)
0313 {
0314     struct device_node *node;
0315     struct device_node *np;
0316     struct device *dev = rsnd_priv_to_dev(priv);
0317     struct rsnd_ctu *ctu;
0318     struct clk *clk;
0319     char name[CTU_NAME_SIZE];
0320     int i, nr, ret;
0321 
0322     /* This driver doesn't support Gen1 at this point */
0323     if (rsnd_is_gen1(priv))
0324         return 0;
0325 
0326     node = rsnd_ctu_of_node(priv);
0327     if (!node)
0328         return 0; /* not used is not error */
0329 
0330     nr = of_get_child_count(node);
0331     if (!nr) {
0332         ret = -EINVAL;
0333         goto rsnd_ctu_probe_done;
0334     }
0335 
0336     ctu = devm_kcalloc(dev, nr, sizeof(*ctu), GFP_KERNEL);
0337     if (!ctu) {
0338         ret = -ENOMEM;
0339         goto rsnd_ctu_probe_done;
0340     }
0341 
0342     priv->ctu_nr    = nr;
0343     priv->ctu   = ctu;
0344 
0345     i = 0;
0346     ret = 0;
0347     for_each_child_of_node(node, np) {
0348         ctu = rsnd_ctu_get(priv, i);
0349 
0350         /*
0351          * CTU00, CTU01, CTU02, CTU03 => CTU0
0352          * CTU10, CTU11, CTU12, CTU13 => CTU1
0353          */
0354         snprintf(name, CTU_NAME_SIZE, "%s.%d",
0355              CTU_NAME, i / 4);
0356 
0357         clk = devm_clk_get(dev, name);
0358         if (IS_ERR(clk)) {
0359             ret = PTR_ERR(clk);
0360             of_node_put(np);
0361             goto rsnd_ctu_probe_done;
0362         }
0363 
0364         ret = rsnd_mod_init(priv, rsnd_mod_get(ctu), &rsnd_ctu_ops,
0365                     clk, RSND_MOD_CTU, i);
0366         if (ret) {
0367             of_node_put(np);
0368             goto rsnd_ctu_probe_done;
0369         }
0370 
0371         i++;
0372     }
0373 
0374 
0375 rsnd_ctu_probe_done:
0376     of_node_put(node);
0377 
0378     return ret;
0379 }
0380 
0381 void rsnd_ctu_remove(struct rsnd_priv *priv)
0382 {
0383     struct rsnd_ctu *ctu;
0384     int i;
0385 
0386     for_each_rsnd_ctu(ctu, priv, i) {
0387         rsnd_mod_quit(rsnd_mod_get(ctu));
0388     }
0389 }