dvc.c 9.4 KB

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