mix.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // mix.c
  4. //
  5. // Copyright (c) 2015 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  6. /*
  7. * CTUn MIXn
  8. * +------+ +------+
  9. * [SRC3 / SRC6] -> |CTU n0| -> [MIX n0| ->
  10. * [SRC4 / SRC9] -> |CTU n1| -> [MIX n1| ->
  11. * [SRC0 / SRC1] -> |CTU n2| -> [MIX n2| ->
  12. * [SRC2 / SRC5] -> |CTU n3| -> [MIX n3| ->
  13. * +------+ +------+
  14. *
  15. * ex)
  16. * DAI0 : playback = <&src0 &ctu02 &mix0 &dvc0 &ssi0>;
  17. * DAI1 : playback = <&src2 &ctu03 &mix0 &dvc0 &ssi0>;
  18. *
  19. * MIX Volume
  20. * amixer set "MIX",0 100% // DAI0 Volume
  21. * amixer set "MIX",1 100% // DAI1 Volume
  22. *
  23. * Volume Ramp
  24. * amixer set "MIX Ramp Up Rate" "0.125 dB/1 step"
  25. * amixer set "MIX Ramp Down Rate" "4 dB/1 step"
  26. * amixer set "MIX Ramp" on
  27. * aplay xxx.wav &
  28. * amixer set "MIX",0 80% // DAI0 Volume Down
  29. * amixer set "MIX",1 100% // DAI1 Volume Up
  30. */
  31. #include "rsnd.h"
  32. #define MIX_NAME_SIZE 16
  33. #define MIX_NAME "mix"
  34. struct rsnd_mix {
  35. struct rsnd_mod mod;
  36. struct rsnd_kctrl_cfg_s volumeA; /* MDBAR */
  37. struct rsnd_kctrl_cfg_s volumeB; /* MDBBR */
  38. struct rsnd_kctrl_cfg_s volumeC; /* MDBCR */
  39. struct rsnd_kctrl_cfg_s volumeD; /* MDBDR */
  40. struct rsnd_kctrl_cfg_s ren; /* Ramp Enable */
  41. struct rsnd_kctrl_cfg_s rup; /* Ramp Rate Up */
  42. struct rsnd_kctrl_cfg_s rdw; /* Ramp Rate Down */
  43. u32 flags;
  44. };
  45. #define ONCE_KCTRL_INITIALIZED (1 << 0)
  46. #define HAS_VOLA (1 << 1)
  47. #define HAS_VOLB (1 << 2)
  48. #define HAS_VOLC (1 << 3)
  49. #define HAS_VOLD (1 << 4)
  50. #define VOL_MAX 0x3ff
  51. #define rsnd_mod_to_mix(_mod) \
  52. container_of((_mod), struct rsnd_mix, mod)
  53. #define rsnd_mix_get(priv, id) ((struct rsnd_mix *)(priv->mix) + id)
  54. #define rsnd_mix_nr(priv) ((priv)->mix_nr)
  55. #define for_each_rsnd_mix(pos, priv, i) \
  56. for ((i) = 0; \
  57. ((i) < rsnd_mix_nr(priv)) && \
  58. ((pos) = (struct rsnd_mix *)(priv)->mix + i); \
  59. i++)
  60. static void rsnd_mix_activation(struct rsnd_mod *mod)
  61. {
  62. rsnd_mod_write(mod, MIX_SWRSR, 0);
  63. rsnd_mod_write(mod, MIX_SWRSR, 1);
  64. }
  65. static void rsnd_mix_halt(struct rsnd_mod *mod)
  66. {
  67. rsnd_mod_write(mod, MIX_MIXIR, 1);
  68. rsnd_mod_write(mod, MIX_SWRSR, 0);
  69. }
  70. #define rsnd_mix_get_vol(mix, X) \
  71. rsnd_flags_has(mix, HAS_VOL##X) ? \
  72. (VOL_MAX - rsnd_kctrl_vals(mix->volume##X)) : 0
  73. static void rsnd_mix_volume_parameter(struct rsnd_dai_stream *io,
  74. struct rsnd_mod *mod)
  75. {
  76. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  77. struct device *dev = rsnd_priv_to_dev(priv);
  78. struct rsnd_mix *mix = rsnd_mod_to_mix(mod);
  79. u32 volA = rsnd_mix_get_vol(mix, A);
  80. u32 volB = rsnd_mix_get_vol(mix, B);
  81. u32 volC = rsnd_mix_get_vol(mix, C);
  82. u32 volD = rsnd_mix_get_vol(mix, D);
  83. dev_dbg(dev, "MIX A/B/C/D = %02x/%02x/%02x/%02x\n",
  84. volA, volB, volC, volD);
  85. rsnd_mod_write(mod, MIX_MDBAR, volA);
  86. rsnd_mod_write(mod, MIX_MDBBR, volB);
  87. rsnd_mod_write(mod, MIX_MDBCR, volC);
  88. rsnd_mod_write(mod, MIX_MDBDR, volD);
  89. }
  90. static void rsnd_mix_volume_init(struct rsnd_dai_stream *io,
  91. struct rsnd_mod *mod)
  92. {
  93. struct rsnd_mix *mix = rsnd_mod_to_mix(mod);
  94. rsnd_mod_write(mod, MIX_MIXIR, 1);
  95. /* General Information */
  96. rsnd_mod_write(mod, MIX_ADINR, rsnd_runtime_channel_after_ctu(io));
  97. /* volume step */
  98. rsnd_mod_write(mod, MIX_MIXMR, rsnd_kctrl_vals(mix->ren));
  99. rsnd_mod_write(mod, MIX_MVPDR, rsnd_kctrl_vals(mix->rup) << 8 |
  100. rsnd_kctrl_vals(mix->rdw));
  101. /* common volume parameter */
  102. rsnd_mix_volume_parameter(io, mod);
  103. rsnd_mod_write(mod, MIX_MIXIR, 0);
  104. }
  105. static void rsnd_mix_volume_update(struct rsnd_dai_stream *io,
  106. struct rsnd_mod *mod)
  107. {
  108. /* Disable MIX dB setting */
  109. rsnd_mod_write(mod, MIX_MDBER, 0);
  110. /* common volume parameter */
  111. rsnd_mix_volume_parameter(io, mod);
  112. /* Enable MIX dB setting */
  113. rsnd_mod_write(mod, MIX_MDBER, 1);
  114. }
  115. static int rsnd_mix_probe_(struct rsnd_mod *mod,
  116. struct rsnd_dai_stream *io,
  117. struct rsnd_priv *priv)
  118. {
  119. return rsnd_cmd_attach(io, rsnd_mod_id(mod));
  120. }
  121. static int rsnd_mix_init(struct rsnd_mod *mod,
  122. struct rsnd_dai_stream *io,
  123. struct rsnd_priv *priv)
  124. {
  125. rsnd_mod_power_on(mod);
  126. rsnd_mix_activation(mod);
  127. rsnd_mix_volume_init(io, mod);
  128. rsnd_mix_volume_update(io, mod);
  129. return 0;
  130. }
  131. static int rsnd_mix_quit(struct rsnd_mod *mod,
  132. struct rsnd_dai_stream *io,
  133. struct rsnd_priv *priv)
  134. {
  135. rsnd_mix_halt(mod);
  136. rsnd_mod_power_off(mod);
  137. return 0;
  138. }
  139. static int rsnd_mix_pcm_new(struct rsnd_mod *mod,
  140. struct rsnd_dai_stream *io,
  141. struct snd_soc_pcm_runtime *rtd)
  142. {
  143. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  144. struct device *dev = rsnd_priv_to_dev(priv);
  145. struct rsnd_mix *mix = rsnd_mod_to_mix(mod);
  146. struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
  147. struct rsnd_kctrl_cfg_s *volume;
  148. int ret;
  149. switch (rsnd_mod_id(src_mod)) {
  150. case 3:
  151. case 6: /* MDBAR */
  152. volume = &mix->volumeA;
  153. rsnd_flags_set(mix, HAS_VOLA);
  154. break;
  155. case 4:
  156. case 9: /* MDBBR */
  157. volume = &mix->volumeB;
  158. rsnd_flags_set(mix, HAS_VOLB);
  159. break;
  160. case 0:
  161. case 1: /* MDBCR */
  162. volume = &mix->volumeC;
  163. rsnd_flags_set(mix, HAS_VOLC);
  164. break;
  165. case 2:
  166. case 5: /* MDBDR */
  167. volume = &mix->volumeD;
  168. rsnd_flags_set(mix, HAS_VOLD);
  169. break;
  170. default:
  171. dev_err(dev, "unknown SRC is connected\n");
  172. return -EINVAL;
  173. }
  174. /* Volume */
  175. ret = rsnd_kctrl_new_s(mod, io, rtd,
  176. "MIX Playback Volume",
  177. rsnd_kctrl_accept_anytime,
  178. rsnd_mix_volume_update,
  179. volume, VOL_MAX);
  180. if (ret < 0)
  181. return ret;
  182. rsnd_kctrl_vals(*volume) = VOL_MAX;
  183. if (rsnd_flags_has(mix, ONCE_KCTRL_INITIALIZED))
  184. return ret;
  185. /* Ramp */
  186. ret = rsnd_kctrl_new_s(mod, io, rtd,
  187. "MIX Ramp Switch",
  188. rsnd_kctrl_accept_anytime,
  189. rsnd_mix_volume_update,
  190. &mix->ren, 1);
  191. if (ret < 0)
  192. return ret;
  193. ret = rsnd_kctrl_new_e(mod, io, rtd,
  194. "MIX Ramp Up Rate",
  195. rsnd_kctrl_accept_anytime,
  196. rsnd_mix_volume_update,
  197. &mix->rup,
  198. volume_ramp_rate,
  199. VOLUME_RAMP_MAX_MIX);
  200. if (ret < 0)
  201. return ret;
  202. ret = rsnd_kctrl_new_e(mod, io, rtd,
  203. "MIX Ramp Down Rate",
  204. rsnd_kctrl_accept_anytime,
  205. rsnd_mix_volume_update,
  206. &mix->rdw,
  207. volume_ramp_rate,
  208. VOLUME_RAMP_MAX_MIX);
  209. rsnd_flags_set(mix, ONCE_KCTRL_INITIALIZED);
  210. return ret;
  211. }
  212. static struct rsnd_mod_ops rsnd_mix_ops = {
  213. .name = MIX_NAME,
  214. .probe = rsnd_mix_probe_,
  215. .init = rsnd_mix_init,
  216. .quit = rsnd_mix_quit,
  217. .pcm_new = rsnd_mix_pcm_new,
  218. };
  219. struct rsnd_mod *rsnd_mix_mod_get(struct rsnd_priv *priv, int id)
  220. {
  221. if (WARN_ON(id < 0 || id >= rsnd_mix_nr(priv)))
  222. id = 0;
  223. return rsnd_mod_get(rsnd_mix_get(priv, id));
  224. }
  225. int rsnd_mix_probe(struct rsnd_priv *priv)
  226. {
  227. struct device_node *node;
  228. struct device_node *np;
  229. struct device *dev = rsnd_priv_to_dev(priv);
  230. struct rsnd_mix *mix;
  231. struct clk *clk;
  232. char name[MIX_NAME_SIZE];
  233. int i, nr, ret;
  234. /* This driver doesn't support Gen1 at this point */
  235. if (rsnd_is_gen1(priv))
  236. return 0;
  237. node = rsnd_mix_of_node(priv);
  238. if (!node)
  239. return 0; /* not used is not error */
  240. nr = of_get_child_count(node);
  241. if (!nr) {
  242. ret = -EINVAL;
  243. goto rsnd_mix_probe_done;
  244. }
  245. mix = devm_kcalloc(dev, nr, sizeof(*mix), GFP_KERNEL);
  246. if (!mix) {
  247. ret = -ENOMEM;
  248. goto rsnd_mix_probe_done;
  249. }
  250. priv->mix_nr = nr;
  251. priv->mix = mix;
  252. i = 0;
  253. ret = 0;
  254. for_each_child_of_node(node, np) {
  255. mix = rsnd_mix_get(priv, i);
  256. snprintf(name, MIX_NAME_SIZE, "%s.%d",
  257. MIX_NAME, i);
  258. clk = devm_clk_get(dev, name);
  259. if (IS_ERR(clk)) {
  260. ret = PTR_ERR(clk);
  261. of_node_put(np);
  262. goto rsnd_mix_probe_done;
  263. }
  264. ret = rsnd_mod_init(priv, rsnd_mod_get(mix), &rsnd_mix_ops,
  265. clk, rsnd_mod_get_status, RSND_MOD_MIX, i);
  266. if (ret) {
  267. of_node_put(np);
  268. goto rsnd_mix_probe_done;
  269. }
  270. i++;
  271. }
  272. rsnd_mix_probe_done:
  273. of_node_put(node);
  274. return ret;
  275. }
  276. void rsnd_mix_remove(struct rsnd_priv *priv)
  277. {
  278. struct rsnd_mix *mix;
  279. int i;
  280. for_each_rsnd_mix(mix, priv, i) {
  281. rsnd_mod_quit(rsnd_mod_get(mix));
  282. }
  283. }