dvc.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 RSND_DVC_VOLUME_MAX 100
  14. #define RSND_DVC_VOLUME_NUM 2
  15. #define DVC_NAME "dvc"
  16. struct rsnd_dvc {
  17. struct rsnd_dvc_platform_info *info; /* rcar_snd.h */
  18. struct rsnd_mod mod;
  19. struct clk *clk;
  20. u8 volume[RSND_DVC_VOLUME_NUM];
  21. u8 mute[RSND_DVC_VOLUME_NUM];
  22. };
  23. #define rsnd_mod_to_dvc(_mod) \
  24. container_of((_mod), struct rsnd_dvc, mod)
  25. #define for_each_rsnd_dvc(pos, priv, i) \
  26. for ((i) = 0; \
  27. ((i) < rsnd_dvc_nr(priv)) && \
  28. ((pos) = (struct rsnd_dvc *)(priv)->dvc + i); \
  29. i++)
  30. static void rsnd_dvc_volume_update(struct rsnd_mod *mod)
  31. {
  32. struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
  33. u32 max = (0x00800000 - 1);
  34. u32 vol[RSND_DVC_VOLUME_NUM];
  35. u32 mute = 0;
  36. int i;
  37. for (i = 0; i < RSND_DVC_VOLUME_NUM; i++) {
  38. vol[i] = max / RSND_DVC_VOLUME_MAX * dvc->volume[i];
  39. mute |= (!!dvc->mute[i]) << i;
  40. }
  41. rsnd_mod_write(mod, DVC_VOL0R, vol[0]);
  42. rsnd_mod_write(mod, DVC_VOL1R, vol[1]);
  43. rsnd_mod_write(mod, DVC_ZCMCR, mute);
  44. }
  45. static int rsnd_dvc_probe_gen2(struct rsnd_mod *mod,
  46. struct rsnd_dai *rdai)
  47. {
  48. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  49. struct device *dev = rsnd_priv_to_dev(priv);
  50. dev_dbg(dev, "%s (Gen2) is probed\n", rsnd_mod_name(mod));
  51. return 0;
  52. }
  53. static int rsnd_dvc_init(struct rsnd_mod *dvc_mod,
  54. struct rsnd_dai *rdai)
  55. {
  56. struct rsnd_dvc *dvc = rsnd_mod_to_dvc(dvc_mod);
  57. struct rsnd_dai_stream *io = rsnd_mod_to_io(dvc_mod);
  58. struct rsnd_priv *priv = rsnd_mod_to_priv(dvc_mod);
  59. struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
  60. struct device *dev = rsnd_priv_to_dev(priv);
  61. int dvc_id = rsnd_mod_id(dvc_mod);
  62. int src_id = rsnd_mod_id(src_mod);
  63. u32 route[] = {
  64. [0] = 0x30000,
  65. [1] = 0x30001,
  66. [2] = 0x40000,
  67. [3] = 0x10000,
  68. [4] = 0x20000,
  69. [5] = 0x40100
  70. };
  71. if (src_id >= ARRAY_SIZE(route)) {
  72. dev_err(dev, "DVC%d isn't connected to SRC%d\n", dvc_id, src_id);
  73. return -EINVAL;
  74. }
  75. clk_prepare_enable(dvc->clk);
  76. /*
  77. * fixme
  78. * it doesn't support CTU/MIX
  79. */
  80. rsnd_mod_write(dvc_mod, CMD_ROUTE_SLCT, route[src_id]);
  81. rsnd_mod_write(dvc_mod, DVC_SWRSR, 0);
  82. rsnd_mod_write(dvc_mod, DVC_SWRSR, 1);
  83. rsnd_mod_write(dvc_mod, DVC_DVUIR, 1);
  84. rsnd_mod_write(dvc_mod, DVC_ADINR, rsnd_get_adinr(dvc_mod));
  85. /* enable Volume / Mute */
  86. rsnd_mod_write(dvc_mod, DVC_DVUCR, 0x101);
  87. /* ch0/ch1 Volume */
  88. rsnd_dvc_volume_update(dvc_mod);
  89. rsnd_mod_write(dvc_mod, DVC_DVUIR, 0);
  90. rsnd_mod_write(dvc_mod, DVC_DVUER, 1);
  91. rsnd_adg_set_cmd_timsel_gen2(rdai, dvc_mod, io);
  92. return 0;
  93. }
  94. static int rsnd_dvc_quit(struct rsnd_mod *mod,
  95. struct rsnd_dai *rdai)
  96. {
  97. struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
  98. clk_disable_unprepare(dvc->clk);
  99. return 0;
  100. }
  101. static int rsnd_dvc_start(struct rsnd_mod *mod,
  102. struct rsnd_dai *rdai)
  103. {
  104. rsnd_mod_write(mod, CMD_CTRL, 0x10);
  105. return 0;
  106. }
  107. static int rsnd_dvc_stop(struct rsnd_mod *mod,
  108. struct rsnd_dai *rdai)
  109. {
  110. rsnd_mod_write(mod, CMD_CTRL, 0);
  111. return 0;
  112. }
  113. static int rsnd_dvc_volume_info(struct snd_kcontrol *kctrl,
  114. struct snd_ctl_elem_info *uinfo)
  115. {
  116. struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
  117. struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
  118. u8 *val = (u8 *)kctrl->private_value;
  119. uinfo->count = RSND_DVC_VOLUME_NUM;
  120. uinfo->value.integer.min = 0;
  121. if (val == dvc->volume) {
  122. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  123. uinfo->value.integer.max = RSND_DVC_VOLUME_MAX;
  124. } else {
  125. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  126. uinfo->value.integer.max = 1;
  127. }
  128. return 0;
  129. }
  130. static int rsnd_dvc_volume_get(struct snd_kcontrol *kctrl,
  131. struct snd_ctl_elem_value *ucontrol)
  132. {
  133. u8 *val = (u8 *)kctrl->private_value;
  134. int i;
  135. for (i = 0; i < RSND_DVC_VOLUME_NUM; i++)
  136. ucontrol->value.integer.value[i] = val[i];
  137. return 0;
  138. }
  139. static int rsnd_dvc_volume_put(struct snd_kcontrol *kctrl,
  140. struct snd_ctl_elem_value *ucontrol)
  141. {
  142. struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
  143. u8 *val = (u8 *)kctrl->private_value;
  144. int i, change = 0;
  145. for (i = 0; i < RSND_DVC_VOLUME_NUM; i++) {
  146. change |= (ucontrol->value.integer.value[i] != val[i]);
  147. val[i] = ucontrol->value.integer.value[i];
  148. }
  149. if (change)
  150. rsnd_dvc_volume_update(mod);
  151. return change;
  152. }
  153. static int __rsnd_dvc_pcm_new(struct rsnd_mod *mod,
  154. struct rsnd_dai *rdai,
  155. struct snd_soc_pcm_runtime *rtd,
  156. const unsigned char *name,
  157. u8 *private)
  158. {
  159. struct snd_card *card = rtd->card->snd_card;
  160. struct snd_kcontrol *kctrl;
  161. struct snd_kcontrol_new knew = {
  162. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  163. .name = name,
  164. .info = rsnd_dvc_volume_info,
  165. .get = rsnd_dvc_volume_get,
  166. .put = rsnd_dvc_volume_put,
  167. .private_value = (unsigned long)private,
  168. };
  169. int ret;
  170. kctrl = snd_ctl_new1(&knew, mod);
  171. if (!kctrl)
  172. return -ENOMEM;
  173. ret = snd_ctl_add(card, kctrl);
  174. if (ret < 0)
  175. return ret;
  176. return 0;
  177. }
  178. static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
  179. struct rsnd_dai *rdai,
  180. struct snd_soc_pcm_runtime *rtd)
  181. {
  182. struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
  183. struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
  184. int ret;
  185. /* Volume */
  186. ret = __rsnd_dvc_pcm_new(mod, rdai, rtd,
  187. rsnd_dai_is_play(rdai, io) ?
  188. "DVC Out Playback Volume" : "DVC In Capture Volume",
  189. dvc->volume);
  190. if (ret < 0)
  191. return ret;
  192. /* Mute */
  193. ret = __rsnd_dvc_pcm_new(mod, rdai, rtd,
  194. rsnd_dai_is_play(rdai, io) ?
  195. "DVC Out Mute Switch" : "DVC In Mute Switch",
  196. dvc->mute);
  197. if (ret < 0)
  198. return ret;
  199. return 0;
  200. }
  201. static struct rsnd_mod_ops rsnd_dvc_ops = {
  202. .name = DVC_NAME,
  203. .probe = rsnd_dvc_probe_gen2,
  204. .init = rsnd_dvc_init,
  205. .quit = rsnd_dvc_quit,
  206. .start = rsnd_dvc_start,
  207. .stop = rsnd_dvc_stop,
  208. .pcm_new = rsnd_dvc_pcm_new,
  209. };
  210. struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
  211. {
  212. if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
  213. id = 0;
  214. return &((struct rsnd_dvc *)(priv->dvc) + id)->mod;
  215. }
  216. static void rsnd_of_parse_dvc(struct platform_device *pdev,
  217. const struct rsnd_of_data *of_data,
  218. struct rsnd_priv *priv)
  219. {
  220. struct device_node *node;
  221. struct rsnd_dvc_platform_info *dvc_info;
  222. struct rcar_snd_info *info = rsnd_priv_to_info(priv);
  223. struct device *dev = &pdev->dev;
  224. int nr;
  225. if (!of_data)
  226. return;
  227. node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
  228. if (!node)
  229. return;
  230. nr = of_get_child_count(node);
  231. if (!nr)
  232. goto rsnd_of_parse_dvc_end;
  233. dvc_info = devm_kzalloc(dev,
  234. sizeof(struct rsnd_dvc_platform_info) * nr,
  235. GFP_KERNEL);
  236. if (!dvc_info) {
  237. dev_err(dev, "dvc info allocation error\n");
  238. goto rsnd_of_parse_dvc_end;
  239. }
  240. info->dvc_info = dvc_info;
  241. info->dvc_info_nr = nr;
  242. rsnd_of_parse_dvc_end:
  243. of_node_put(node);
  244. }
  245. int rsnd_dvc_probe(struct platform_device *pdev,
  246. const struct rsnd_of_data *of_data,
  247. struct rsnd_priv *priv)
  248. {
  249. struct rcar_snd_info *info = rsnd_priv_to_info(priv);
  250. struct device *dev = rsnd_priv_to_dev(priv);
  251. struct rsnd_dvc *dvc;
  252. struct clk *clk;
  253. char name[RSND_DVC_NAME_SIZE];
  254. int i, nr;
  255. rsnd_of_parse_dvc(pdev, of_data, priv);
  256. nr = info->dvc_info_nr;
  257. if (!nr)
  258. return 0;
  259. /* This driver doesn't support Gen1 at this point */
  260. if (rsnd_is_gen1(priv)) {
  261. dev_warn(dev, "CMD is not supported on Gen1\n");
  262. return -EINVAL;
  263. }
  264. dvc = devm_kzalloc(dev, sizeof(*dvc) * nr, GFP_KERNEL);
  265. if (!dvc) {
  266. dev_err(dev, "CMD allocate failed\n");
  267. return -ENOMEM;
  268. }
  269. priv->dvc_nr = nr;
  270. priv->dvc = dvc;
  271. for_each_rsnd_dvc(dvc, priv, i) {
  272. snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
  273. DVC_NAME, i);
  274. clk = devm_clk_get(dev, name);
  275. if (IS_ERR(clk))
  276. return PTR_ERR(clk);
  277. dvc->info = &info->dvc_info[i];
  278. dvc->clk = clk;
  279. rsnd_mod_init(priv, &dvc->mod, &rsnd_dvc_ops, RSND_MOD_DVC, i);
  280. dev_dbg(dev, "CMD%d probed\n", i);
  281. }
  282. return 0;
  283. }