dvc.c 9.3 KB

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