dvc.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Renesas R-Car DVC support
  4. //
  5. // Copyright (C) 2014 Renesas Solutions Corp.
  6. // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  7. /*
  8. * Playback Volume
  9. * amixer set "DVC Out" 100%
  10. *
  11. * Capture Volume
  12. * amixer set "DVC In" 100%
  13. *
  14. * Playback Mute
  15. * amixer set "DVC Out Mute" on
  16. *
  17. * Capture Mute
  18. * amixer set "DVC In Mute" on
  19. *
  20. * Volume Ramp
  21. * amixer set "DVC Out Ramp Up Rate" "0.125 dB/64 steps"
  22. * amixer set "DVC Out Ramp Down Rate" "0.125 dB/512 steps"
  23. * amixer set "DVC Out Ramp" on
  24. * aplay xxx.wav &
  25. * amixer set "DVC Out" 80% // Volume Down
  26. * amixer set "DVC Out" 100% // Volume Up
  27. */
  28. #include "rsnd.h"
  29. #define RSND_DVC_NAME_SIZE 16
  30. #define DVC_NAME "dvc"
  31. struct rsnd_dvc {
  32. struct rsnd_mod mod;
  33. struct rsnd_kctrl_cfg_m volume;
  34. struct rsnd_kctrl_cfg_m mute;
  35. struct rsnd_kctrl_cfg_s ren; /* Ramp Enable */
  36. struct rsnd_kctrl_cfg_s rup; /* Ramp Rate Up */
  37. struct rsnd_kctrl_cfg_s rdown; /* Ramp Rate Down */
  38. };
  39. #define rsnd_dvc_get(priv, id) ((struct rsnd_dvc *)(priv->dvc) + id)
  40. #define rsnd_dvc_nr(priv) ((priv)->dvc_nr)
  41. #define rsnd_mod_to_dvc(_mod) \
  42. container_of((_mod), struct rsnd_dvc, mod)
  43. #define for_each_rsnd_dvc(pos, priv, i) \
  44. for ((i) = 0; \
  45. ((i) < rsnd_dvc_nr(priv)) && \
  46. ((pos) = (struct rsnd_dvc *)(priv)->dvc + i); \
  47. i++)
  48. static void rsnd_dvc_activation(struct rsnd_mod *mod)
  49. {
  50. rsnd_mod_write(mod, DVC_SWRSR, 0);
  51. rsnd_mod_write(mod, DVC_SWRSR, 1);
  52. }
  53. static void rsnd_dvc_halt(struct rsnd_mod *mod)
  54. {
  55. rsnd_mod_write(mod, DVC_DVUIR, 1);
  56. rsnd_mod_write(mod, DVC_SWRSR, 0);
  57. }
  58. #define rsnd_dvc_get_vrpdr(dvc) (rsnd_kctrl_vals(dvc->rup) << 8 | \
  59. rsnd_kctrl_vals(dvc->rdown))
  60. #define rsnd_dvc_get_vrdbr(dvc) (0x3ff - (rsnd_kctrl_valm(dvc->volume, 0) >> 13))
  61. static void rsnd_dvc_volume_parameter(struct rsnd_dai_stream *io,
  62. struct rsnd_mod *mod)
  63. {
  64. struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
  65. u32 val[RSND_MAX_CHANNELS];
  66. int i;
  67. /* Enable Ramp */
  68. if (rsnd_kctrl_vals(dvc->ren))
  69. for (i = 0; i < RSND_MAX_CHANNELS; i++)
  70. val[i] = rsnd_kctrl_max(dvc->volume);
  71. else
  72. for (i = 0; i < RSND_MAX_CHANNELS; i++)
  73. val[i] = rsnd_kctrl_valm(dvc->volume, i);
  74. /* Enable Digital Volume */
  75. rsnd_mod_write(mod, DVC_VOL0R, val[0]);
  76. rsnd_mod_write(mod, DVC_VOL1R, val[1]);
  77. rsnd_mod_write(mod, DVC_VOL2R, val[2]);
  78. rsnd_mod_write(mod, DVC_VOL3R, val[3]);
  79. rsnd_mod_write(mod, DVC_VOL4R, val[4]);
  80. rsnd_mod_write(mod, DVC_VOL5R, val[5]);
  81. rsnd_mod_write(mod, DVC_VOL6R, val[6]);
  82. rsnd_mod_write(mod, DVC_VOL7R, val[7]);
  83. }
  84. static void rsnd_dvc_volume_init(struct rsnd_dai_stream *io,
  85. struct rsnd_mod *mod)
  86. {
  87. struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
  88. u32 adinr = 0;
  89. u32 dvucr = 0;
  90. u32 vrctr = 0;
  91. u32 vrpdr = 0;
  92. u32 vrdbr = 0;
  93. adinr = rsnd_get_adinr_bit(mod, io) |
  94. rsnd_runtime_channel_after_ctu(io);
  95. /* Enable Digital Volume, Zero Cross Mute Mode */
  96. dvucr |= 0x101;
  97. /* Enable Ramp */
  98. if (rsnd_kctrl_vals(dvc->ren)) {
  99. dvucr |= 0x10;
  100. /*
  101. * FIXME !!
  102. * use scale-downed Digital Volume
  103. * as Volume Ramp
  104. * 7F FFFF -> 3FF
  105. */
  106. vrctr = 0xff;
  107. vrpdr = rsnd_dvc_get_vrpdr(dvc);
  108. vrdbr = rsnd_dvc_get_vrdbr(dvc);
  109. }
  110. /* Initialize operation */
  111. rsnd_mod_write(mod, DVC_DVUIR, 1);
  112. /* General Information */
  113. rsnd_mod_write(mod, DVC_ADINR, adinr);
  114. rsnd_mod_write(mod, DVC_DVUCR, dvucr);
  115. /* Volume Ramp Parameter */
  116. rsnd_mod_write(mod, DVC_VRCTR, vrctr);
  117. rsnd_mod_write(mod, DVC_VRPDR, vrpdr);
  118. rsnd_mod_write(mod, DVC_VRDBR, vrdbr);
  119. /* Digital Volume Function Parameter */
  120. rsnd_dvc_volume_parameter(io, mod);
  121. /* cancel operation */
  122. rsnd_mod_write(mod, DVC_DVUIR, 0);
  123. }
  124. static void rsnd_dvc_volume_update(struct rsnd_dai_stream *io,
  125. struct rsnd_mod *mod)
  126. {
  127. struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
  128. u32 zcmcr = 0;
  129. u32 vrpdr = 0;
  130. u32 vrdbr = 0;
  131. int i;
  132. for (i = 0; i < rsnd_kctrl_size(dvc->mute); i++)
  133. zcmcr |= (!!rsnd_kctrl_valm(dvc->mute, i)) << i;
  134. if (rsnd_kctrl_vals(dvc->ren)) {
  135. vrpdr = rsnd_dvc_get_vrpdr(dvc);
  136. vrdbr = rsnd_dvc_get_vrdbr(dvc);
  137. }
  138. /* Disable DVC Register access */
  139. rsnd_mod_write(mod, DVC_DVUER, 0);
  140. /* Zero Cross Mute Function */
  141. rsnd_mod_write(mod, DVC_ZCMCR, zcmcr);
  142. /* Volume Ramp Function */
  143. rsnd_mod_write(mod, DVC_VRPDR, vrpdr);
  144. rsnd_mod_write(mod, DVC_VRDBR, vrdbr);
  145. /* add DVC_VRWTR here */
  146. /* Digital Volume Function Parameter */
  147. rsnd_dvc_volume_parameter(io, mod);
  148. /* Enable DVC Register access */
  149. rsnd_mod_write(mod, DVC_DVUER, 1);
  150. }
  151. static int rsnd_dvc_probe_(struct rsnd_mod *mod,
  152. struct rsnd_dai_stream *io,
  153. struct rsnd_priv *priv)
  154. {
  155. return rsnd_cmd_attach(io, rsnd_mod_id(mod));
  156. }
  157. static int rsnd_dvc_init(struct rsnd_mod *mod,
  158. struct rsnd_dai_stream *io,
  159. struct rsnd_priv *priv)
  160. {
  161. rsnd_mod_power_on(mod);
  162. rsnd_dvc_activation(mod);
  163. rsnd_dvc_volume_init(io, mod);
  164. rsnd_dvc_volume_update(io, mod);
  165. return 0;
  166. }
  167. static int rsnd_dvc_quit(struct rsnd_mod *mod,
  168. struct rsnd_dai_stream *io,
  169. struct rsnd_priv *priv)
  170. {
  171. rsnd_dvc_halt(mod);
  172. rsnd_mod_power_off(mod);
  173. return 0;
  174. }
  175. static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
  176. struct rsnd_dai_stream *io,
  177. struct snd_soc_pcm_runtime *rtd)
  178. {
  179. struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
  180. struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
  181. int is_play = rsnd_io_is_play(io);
  182. int channels = rsnd_rdai_channels_get(rdai);
  183. int ret;
  184. /* Volume */
  185. ret = rsnd_kctrl_new_m(mod, io, rtd,
  186. is_play ?
  187. "DVC Out Playback Volume" : "DVC In Capture Volume",
  188. rsnd_kctrl_accept_anytime,
  189. rsnd_dvc_volume_update,
  190. &dvc->volume, channels,
  191. 0x00800000 - 1);
  192. if (ret < 0)
  193. return ret;
  194. /* Mute */
  195. ret = rsnd_kctrl_new_m(mod, io, rtd,
  196. is_play ?
  197. "DVC Out Mute Switch" : "DVC In Mute Switch",
  198. rsnd_kctrl_accept_anytime,
  199. rsnd_dvc_volume_update,
  200. &dvc->mute, channels,
  201. 1);
  202. if (ret < 0)
  203. return ret;
  204. /* Ramp */
  205. ret = rsnd_kctrl_new_s(mod, io, rtd,
  206. is_play ?
  207. "DVC Out Ramp Switch" : "DVC In Ramp Switch",
  208. rsnd_kctrl_accept_anytime,
  209. rsnd_dvc_volume_update,
  210. &dvc->ren, 1);
  211. if (ret < 0)
  212. return ret;
  213. ret = rsnd_kctrl_new_e(mod, io, rtd,
  214. is_play ?
  215. "DVC Out Ramp Up Rate" : "DVC In Ramp Up Rate",
  216. rsnd_kctrl_accept_anytime,
  217. rsnd_dvc_volume_update,
  218. &dvc->rup,
  219. volume_ramp_rate,
  220. VOLUME_RAMP_MAX_DVC);
  221. if (ret < 0)
  222. return ret;
  223. ret = rsnd_kctrl_new_e(mod, io, rtd,
  224. is_play ?
  225. "DVC Out Ramp Down Rate" : "DVC In Ramp Down Rate",
  226. rsnd_kctrl_accept_anytime,
  227. rsnd_dvc_volume_update,
  228. &dvc->rdown,
  229. volume_ramp_rate,
  230. VOLUME_RAMP_MAX_DVC);
  231. if (ret < 0)
  232. return ret;
  233. return 0;
  234. }
  235. static struct dma_chan *rsnd_dvc_dma_req(struct rsnd_dai_stream *io,
  236. struct rsnd_mod *mod)
  237. {
  238. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  239. return rsnd_dma_request_channel(rsnd_dvc_of_node(priv),
  240. mod, "tx");
  241. }
  242. static struct rsnd_mod_ops rsnd_dvc_ops = {
  243. .name = DVC_NAME,
  244. .dma_req = rsnd_dvc_dma_req,
  245. .probe = rsnd_dvc_probe_,
  246. .init = rsnd_dvc_init,
  247. .quit = rsnd_dvc_quit,
  248. .pcm_new = rsnd_dvc_pcm_new,
  249. };
  250. struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
  251. {
  252. if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
  253. id = 0;
  254. return rsnd_mod_get(rsnd_dvc_get(priv, id));
  255. }
  256. int rsnd_dvc_probe(struct rsnd_priv *priv)
  257. {
  258. struct device_node *node;
  259. struct device_node *np;
  260. struct device *dev = rsnd_priv_to_dev(priv);
  261. struct rsnd_dvc *dvc;
  262. struct clk *clk;
  263. char name[RSND_DVC_NAME_SIZE];
  264. int i, nr, ret;
  265. /* This driver doesn't support Gen1 at this point */
  266. if (rsnd_is_gen1(priv))
  267. return 0;
  268. node = rsnd_dvc_of_node(priv);
  269. if (!node)
  270. return 0; /* not used is not error */
  271. nr = of_get_child_count(node);
  272. if (!nr) {
  273. ret = -EINVAL;
  274. goto rsnd_dvc_probe_done;
  275. }
  276. dvc = devm_kcalloc(dev, nr, sizeof(*dvc), GFP_KERNEL);
  277. if (!dvc) {
  278. ret = -ENOMEM;
  279. goto rsnd_dvc_probe_done;
  280. }
  281. priv->dvc_nr = nr;
  282. priv->dvc = dvc;
  283. i = 0;
  284. ret = 0;
  285. for_each_child_of_node(node, np) {
  286. dvc = rsnd_dvc_get(priv, i);
  287. snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
  288. DVC_NAME, i);
  289. clk = devm_clk_get(dev, name);
  290. if (IS_ERR(clk)) {
  291. ret = PTR_ERR(clk);
  292. of_node_put(np);
  293. goto rsnd_dvc_probe_done;
  294. }
  295. ret = rsnd_mod_init(priv, rsnd_mod_get(dvc), &rsnd_dvc_ops,
  296. clk, rsnd_mod_get_status, RSND_MOD_DVC, i);
  297. if (ret) {
  298. of_node_put(np);
  299. goto rsnd_dvc_probe_done;
  300. }
  301. i++;
  302. }
  303. rsnd_dvc_probe_done:
  304. of_node_put(node);
  305. return ret;
  306. }
  307. void rsnd_dvc_remove(struct rsnd_priv *priv)
  308. {
  309. struct rsnd_dvc *dvc;
  310. int i;
  311. for_each_rsnd_dvc(dvc, priv, i) {
  312. rsnd_mod_quit(rsnd_mod_get(dvc));
  313. }
  314. }