ctu.c 10 KB

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