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