dvc.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. #include "rsnd.h"
  12. #define RSND_DVC_NAME_SIZE 16
  13. #define DVC_NAME "dvc"
  14. struct rsnd_dvc {
  15. struct rsnd_dvc_platform_info *info; /* rcar_snd.h */
  16. struct rsnd_mod mod;
  17. struct rsnd_kctrl_cfg_m volume;
  18. struct rsnd_kctrl_cfg_m mute;
  19. struct rsnd_kctrl_cfg_s ren; /* Ramp Enable */
  20. struct rsnd_kctrl_cfg_s rup; /* Ramp Rate Up */
  21. struct rsnd_kctrl_cfg_s rdown; /* Ramp Rate Down */
  22. };
  23. #define rsnd_dvc_nr(priv) ((priv)->dvc_nr)
  24. #define rsnd_dvc_of_node(priv) \
  25. of_get_child_by_name(rsnd_priv_to_dev(priv)->of_node, "rcar_sound,dvc")
  26. #define rsnd_mod_to_dvc(_mod) \
  27. container_of((_mod), struct rsnd_dvc, mod)
  28. #define for_each_rsnd_dvc(pos, priv, i) \
  29. for ((i) = 0; \
  30. ((i) < rsnd_dvc_nr(priv)) && \
  31. ((pos) = (struct rsnd_dvc *)(priv)->dvc + i); \
  32. i++)
  33. static const char * const dvc_ramp_rate[] = {
  34. "128 dB/1 step", /* 00000 */
  35. "64 dB/1 step", /* 00001 */
  36. "32 dB/1 step", /* 00010 */
  37. "16 dB/1 step", /* 00011 */
  38. "8 dB/1 step", /* 00100 */
  39. "4 dB/1 step", /* 00101 */
  40. "2 dB/1 step", /* 00110 */
  41. "1 dB/1 step", /* 00111 */
  42. "0.5 dB/1 step", /* 01000 */
  43. "0.25 dB/1 step", /* 01001 */
  44. "0.125 dB/1 step", /* 01010 */
  45. "0.125 dB/2 steps", /* 01011 */
  46. "0.125 dB/4 steps", /* 01100 */
  47. "0.125 dB/8 steps", /* 01101 */
  48. "0.125 dB/16 steps", /* 01110 */
  49. "0.125 dB/32 steps", /* 01111 */
  50. "0.125 dB/64 steps", /* 10000 */
  51. "0.125 dB/128 steps", /* 10001 */
  52. "0.125 dB/256 steps", /* 10010 */
  53. "0.125 dB/512 steps", /* 10011 */
  54. "0.125 dB/1024 steps", /* 10100 */
  55. "0.125 dB/2048 steps", /* 10101 */
  56. "0.125 dB/4096 steps", /* 10110 */
  57. "0.125 dB/8192 steps", /* 10111 */
  58. };
  59. static void rsnd_dvc_soft_reset(struct rsnd_mod *mod)
  60. {
  61. rsnd_mod_write(mod, DVC_SWRSR, 0);
  62. rsnd_mod_write(mod, DVC_SWRSR, 1);
  63. }
  64. #define rsnd_dvc_initialize_lock(mod) __rsnd_dvc_initialize_lock(mod, 1)
  65. #define rsnd_dvc_initialize_unlock(mod) __rsnd_dvc_initialize_lock(mod, 0)
  66. static void __rsnd_dvc_initialize_lock(struct rsnd_mod *mod, u32 enable)
  67. {
  68. rsnd_mod_write(mod, DVC_DVUIR, enable);
  69. }
  70. static void rsnd_dvc_volume_update(struct rsnd_dai_stream *io,
  71. struct rsnd_mod *mod)
  72. {
  73. struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
  74. u32 val[RSND_DVC_CHANNELS];
  75. u32 dvucr = 0;
  76. u32 mute = 0;
  77. int i;
  78. for (i = 0; i < dvc->mute.cfg.size; i++)
  79. mute |= (!!dvc->mute.cfg.val[i]) << i;
  80. /* Disable DVC Register access */
  81. rsnd_mod_write(mod, DVC_DVUER, 0);
  82. /* Enable Ramp */
  83. if (dvc->ren.val) {
  84. dvucr |= 0x10;
  85. /* Digital Volume Max */
  86. for (i = 0; i < RSND_DVC_CHANNELS; i++)
  87. val[i] = dvc->volume.cfg.max;
  88. rsnd_mod_write(mod, DVC_VRCTR, 0xff);
  89. rsnd_mod_write(mod, DVC_VRPDR, dvc->rup.val << 8 |
  90. dvc->rdown.val);
  91. /*
  92. * FIXME !!
  93. * use scale-downed Digital Volume
  94. * as Volume Ramp
  95. * 7F FFFF -> 3FF
  96. */
  97. rsnd_mod_write(mod, DVC_VRDBR,
  98. 0x3ff - (dvc->volume.val[0] >> 13));
  99. } else {
  100. for (i = 0; i < RSND_DVC_CHANNELS; i++)
  101. val[i] = dvc->volume.val[i];
  102. }
  103. /* Enable Digital Volume */
  104. dvucr |= 0x100;
  105. rsnd_mod_write(mod, DVC_VOL0R, val[0]);
  106. rsnd_mod_write(mod, DVC_VOL1R, val[1]);
  107. /* Enable Mute */
  108. if (mute) {
  109. dvucr |= 0x1;
  110. rsnd_mod_write(mod, DVC_ZCMCR, mute);
  111. }
  112. rsnd_mod_write(mod, DVC_DVUCR, dvucr);
  113. /* Enable DVC Register access */
  114. rsnd_mod_write(mod, DVC_DVUER, 1);
  115. }
  116. static int rsnd_dvc_remove_gen2(struct rsnd_mod *mod,
  117. struct rsnd_dai_stream *io,
  118. struct rsnd_priv *priv)
  119. {
  120. struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
  121. rsnd_kctrl_remove(dvc->volume);
  122. rsnd_kctrl_remove(dvc->mute);
  123. rsnd_kctrl_remove(dvc->ren);
  124. rsnd_kctrl_remove(dvc->rup);
  125. rsnd_kctrl_remove(dvc->rdown);
  126. return 0;
  127. }
  128. static int rsnd_dvc_init(struct rsnd_mod *mod,
  129. struct rsnd_dai_stream *io,
  130. struct rsnd_priv *priv)
  131. {
  132. rsnd_mod_hw_start(mod);
  133. rsnd_dvc_soft_reset(mod);
  134. rsnd_dvc_initialize_lock(mod);
  135. rsnd_path_parse(priv, io);
  136. rsnd_mod_write(mod, DVC_ADINR, rsnd_get_adinr_bit(mod, io));
  137. /* ch0/ch1 Volume */
  138. rsnd_dvc_volume_update(io, mod);
  139. rsnd_adg_set_cmd_timsel_gen2(mod, io);
  140. return 0;
  141. }
  142. static int rsnd_dvc_quit(struct rsnd_mod *mod,
  143. struct rsnd_dai_stream *io,
  144. struct rsnd_priv *priv)
  145. {
  146. rsnd_mod_hw_stop(mod);
  147. return 0;
  148. }
  149. static int rsnd_dvc_start(struct rsnd_mod *mod,
  150. struct rsnd_dai_stream *io,
  151. struct rsnd_priv *priv)
  152. {
  153. rsnd_dvc_initialize_unlock(mod);
  154. rsnd_mod_write(mod, CMD_CTRL, 0x10);
  155. return 0;
  156. }
  157. static int rsnd_dvc_stop(struct rsnd_mod *mod,
  158. struct rsnd_dai_stream *io,
  159. struct rsnd_priv *priv)
  160. {
  161. rsnd_mod_write(mod, CMD_CTRL, 0);
  162. return 0;
  163. }
  164. static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
  165. struct rsnd_dai_stream *io,
  166. struct snd_soc_pcm_runtime *rtd)
  167. {
  168. struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
  169. int is_play = rsnd_io_is_play(io);
  170. int ret;
  171. /* Volume */
  172. ret = rsnd_kctrl_new_m(mod, io, rtd,
  173. is_play ?
  174. "DVC Out Playback Volume" : "DVC In Capture Volume",
  175. rsnd_dvc_volume_update,
  176. &dvc->volume, 0x00800000 - 1);
  177. if (ret < 0)
  178. return ret;
  179. /* Mute */
  180. ret = rsnd_kctrl_new_m(mod, io, rtd,
  181. is_play ?
  182. "DVC Out Mute Switch" : "DVC In Mute Switch",
  183. rsnd_dvc_volume_update,
  184. &dvc->mute, 1);
  185. if (ret < 0)
  186. return ret;
  187. /* Ramp */
  188. ret = rsnd_kctrl_new_s(mod, io, rtd,
  189. is_play ?
  190. "DVC Out Ramp Switch" : "DVC In Ramp Switch",
  191. rsnd_dvc_volume_update,
  192. &dvc->ren, 1);
  193. if (ret < 0)
  194. return ret;
  195. ret = rsnd_kctrl_new_e(mod, io, rtd,
  196. is_play ?
  197. "DVC Out Ramp Up Rate" : "DVC In Ramp Up Rate",
  198. &dvc->rup,
  199. rsnd_dvc_volume_update,
  200. dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
  201. if (ret < 0)
  202. return ret;
  203. ret = rsnd_kctrl_new_e(mod, io, rtd,
  204. is_play ?
  205. "DVC Out Ramp Down Rate" : "DVC In Ramp Down Rate",
  206. &dvc->rdown,
  207. rsnd_dvc_volume_update,
  208. dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
  209. if (ret < 0)
  210. return ret;
  211. return 0;
  212. }
  213. static struct dma_chan *rsnd_dvc_dma_req(struct rsnd_dai_stream *io,
  214. struct rsnd_mod *mod)
  215. {
  216. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  217. return rsnd_dma_request_channel(rsnd_dvc_of_node(priv),
  218. mod, "tx");
  219. }
  220. static struct rsnd_mod_ops rsnd_dvc_ops = {
  221. .name = DVC_NAME,
  222. .dma_req = rsnd_dvc_dma_req,
  223. .remove = rsnd_dvc_remove_gen2,
  224. .init = rsnd_dvc_init,
  225. .quit = rsnd_dvc_quit,
  226. .start = rsnd_dvc_start,
  227. .stop = rsnd_dvc_stop,
  228. .pcm_new = rsnd_dvc_pcm_new,
  229. };
  230. struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
  231. {
  232. if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
  233. id = 0;
  234. return &((struct rsnd_dvc *)(priv->dvc) + id)->mod;
  235. }
  236. static void rsnd_of_parse_dvc(struct platform_device *pdev,
  237. const struct rsnd_of_data *of_data,
  238. struct rsnd_priv *priv)
  239. {
  240. struct device_node *node;
  241. struct rsnd_dvc_platform_info *dvc_info;
  242. struct rcar_snd_info *info = rsnd_priv_to_info(priv);
  243. struct device *dev = &pdev->dev;
  244. int nr;
  245. if (!of_data)
  246. return;
  247. node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
  248. if (!node)
  249. return;
  250. nr = of_get_child_count(node);
  251. if (!nr)
  252. goto rsnd_of_parse_dvc_end;
  253. dvc_info = devm_kzalloc(dev,
  254. sizeof(struct rsnd_dvc_platform_info) * nr,
  255. GFP_KERNEL);
  256. if (!dvc_info) {
  257. dev_err(dev, "dvc info allocation error\n");
  258. goto rsnd_of_parse_dvc_end;
  259. }
  260. info->dvc_info = dvc_info;
  261. info->dvc_info_nr = nr;
  262. rsnd_of_parse_dvc_end:
  263. of_node_put(node);
  264. }
  265. int rsnd_dvc_probe(struct platform_device *pdev,
  266. const struct rsnd_of_data *of_data,
  267. struct rsnd_priv *priv)
  268. {
  269. struct rcar_snd_info *info = rsnd_priv_to_info(priv);
  270. struct device *dev = rsnd_priv_to_dev(priv);
  271. struct rsnd_dvc *dvc;
  272. struct clk *clk;
  273. char name[RSND_DVC_NAME_SIZE];
  274. int i, nr, ret;
  275. /* This driver doesn't support Gen1 at this point */
  276. if (rsnd_is_gen1(priv)) {
  277. dev_warn(dev, "CMD is not supported on Gen1\n");
  278. return -EINVAL;
  279. }
  280. rsnd_of_parse_dvc(pdev, of_data, priv);
  281. nr = info->dvc_info_nr;
  282. if (!nr)
  283. return 0;
  284. dvc = devm_kzalloc(dev, sizeof(*dvc) * nr, GFP_KERNEL);
  285. if (!dvc)
  286. return -ENOMEM;
  287. priv->dvc_nr = nr;
  288. priv->dvc = dvc;
  289. for_each_rsnd_dvc(dvc, priv, i) {
  290. snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
  291. DVC_NAME, i);
  292. clk = devm_clk_get(dev, name);
  293. if (IS_ERR(clk))
  294. return PTR_ERR(clk);
  295. dvc->info = &info->dvc_info[i];
  296. ret = rsnd_mod_init(priv, &dvc->mod, &rsnd_dvc_ops,
  297. clk, RSND_MOD_DVC, i);
  298. if (ret)
  299. return ret;
  300. }
  301. return 0;
  302. }
  303. void rsnd_dvc_remove(struct platform_device *pdev,
  304. struct rsnd_priv *priv)
  305. {
  306. struct rsnd_dvc *dvc;
  307. int i;
  308. for_each_rsnd_dvc(dvc, priv, i) {
  309. rsnd_mod_quit(&dvc->mod);
  310. }
  311. }