ctu.c 11 KB

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