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