2 * Renesas R-Car DVC support
4 * Copyright (C) 2014 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
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.
14 * amixer set "DVC Out" 100%
17 * amixer set "DVC In" 100%
20 * amixer set "DVC Out Mute" on
23 * amixer set "DVC In Mute" on
26 * amixer set "DVC Out Ramp Up Rate" "0.125 dB/64 steps"
27 * amixer set "DVC Out Ramp Down Rate" "0.125 dB/512 steps"
28 * amixer set "DVC Out Ramp" on
30 * amixer set "DVC Out" 80% // Volume Down
31 * amixer set "DVC Out" 100% // Volume Up
36 #define RSND_DVC_NAME_SIZE 16
38 #define DVC_NAME "dvc"
42 struct rsnd_kctrl_cfg_m volume
;
43 struct rsnd_kctrl_cfg_m mute
;
44 struct rsnd_kctrl_cfg_s ren
; /* Ramp Enable */
45 struct rsnd_kctrl_cfg_s rup
; /* Ramp Rate Up */
46 struct rsnd_kctrl_cfg_s rdown
; /* Ramp Rate Down */
49 #define rsnd_dvc_get(priv, id) ((struct rsnd_dvc *)(priv->dvc) + id)
50 #define rsnd_dvc_nr(priv) ((priv)->dvc_nr)
51 #define rsnd_dvc_of_node(priv) \
52 of_get_child_by_name(rsnd_priv_to_dev(priv)->of_node, "rcar_sound,dvc")
54 #define rsnd_mod_to_dvc(_mod) \
55 container_of((_mod), struct rsnd_dvc, mod)
57 #define for_each_rsnd_dvc(pos, priv, i) \
59 ((i) < rsnd_dvc_nr(priv)) && \
60 ((pos) = (struct rsnd_dvc *)(priv)->dvc + i); \
63 static const char * const dvc_ramp_rate
[] = {
64 "128 dB/1 step", /* 00000 */
65 "64 dB/1 step", /* 00001 */
66 "32 dB/1 step", /* 00010 */
67 "16 dB/1 step", /* 00011 */
68 "8 dB/1 step", /* 00100 */
69 "4 dB/1 step", /* 00101 */
70 "2 dB/1 step", /* 00110 */
71 "1 dB/1 step", /* 00111 */
72 "0.5 dB/1 step", /* 01000 */
73 "0.25 dB/1 step", /* 01001 */
74 "0.125 dB/1 step", /* 01010 */
75 "0.125 dB/2 steps", /* 01011 */
76 "0.125 dB/4 steps", /* 01100 */
77 "0.125 dB/8 steps", /* 01101 */
78 "0.125 dB/16 steps", /* 01110 */
79 "0.125 dB/32 steps", /* 01111 */
80 "0.125 dB/64 steps", /* 10000 */
81 "0.125 dB/128 steps", /* 10001 */
82 "0.125 dB/256 steps", /* 10010 */
83 "0.125 dB/512 steps", /* 10011 */
84 "0.125 dB/1024 steps", /* 10100 */
85 "0.125 dB/2048 steps", /* 10101 */
86 "0.125 dB/4096 steps", /* 10110 */
87 "0.125 dB/8192 steps", /* 10111 */
90 static void rsnd_dvc_activation(struct rsnd_mod
*mod
)
92 rsnd_mod_write(mod
, DVC_SWRSR
, 0);
93 rsnd_mod_write(mod
, DVC_SWRSR
, 1);
96 static void rsnd_dvc_halt(struct rsnd_mod
*mod
)
98 rsnd_mod_write(mod
, DVC_DVUIR
, 1);
99 rsnd_mod_write(mod
, DVC_SWRSR
, 0);
102 #define rsnd_dvc_get_vrpdr(dvc) (dvc->rup.val << 8 | dvc->rdown.val)
103 #define rsnd_dvc_get_vrdbr(dvc) (0x3ff - (dvc->volume.val[0] >> 13))
105 static void rsnd_dvc_volume_parameter(struct rsnd_dai_stream
*io
,
106 struct rsnd_mod
*mod
)
108 struct rsnd_dvc
*dvc
= rsnd_mod_to_dvc(mod
);
109 u32 val
[RSND_MAX_CHANNELS
];
114 for (i
= 0; i
< RSND_MAX_CHANNELS
; i
++)
115 val
[i
] = dvc
->volume
.cfg
.max
;
117 for (i
= 0; i
< RSND_MAX_CHANNELS
; i
++)
118 val
[i
] = dvc
->volume
.val
[i
];
120 /* Enable Digital Volume */
121 rsnd_mod_write(mod
, DVC_VOL0R
, val
[0]);
122 rsnd_mod_write(mod
, DVC_VOL1R
, val
[1]);
123 rsnd_mod_write(mod
, DVC_VOL2R
, val
[2]);
124 rsnd_mod_write(mod
, DVC_VOL3R
, val
[3]);
125 rsnd_mod_write(mod
, DVC_VOL4R
, val
[4]);
126 rsnd_mod_write(mod
, DVC_VOL5R
, val
[5]);
127 rsnd_mod_write(mod
, DVC_VOL6R
, val
[6]);
128 rsnd_mod_write(mod
, DVC_VOL7R
, val
[7]);
131 static void rsnd_dvc_volume_init(struct rsnd_dai_stream
*io
,
132 struct rsnd_mod
*mod
)
134 struct rsnd_dvc
*dvc
= rsnd_mod_to_dvc(mod
);
141 adinr
= rsnd_get_adinr_bit(mod
, io
) |
142 rsnd_runtime_channel_after_ctu(io
);
144 /* Enable Digital Volume, Zero Cross Mute Mode */
153 * use scale-downed Digital Volume
158 vrpdr
= rsnd_dvc_get_vrpdr(dvc
);
159 vrdbr
= rsnd_dvc_get_vrdbr(dvc
);
162 /* Initialize operation */
163 rsnd_mod_write(mod
, DVC_DVUIR
, 1);
165 /* General Information */
166 rsnd_mod_write(mod
, DVC_ADINR
, adinr
);
167 rsnd_mod_write(mod
, DVC_DVUCR
, dvucr
);
169 /* Volume Ramp Parameter */
170 rsnd_mod_write(mod
, DVC_VRCTR
, vrctr
);
171 rsnd_mod_write(mod
, DVC_VRPDR
, vrpdr
);
172 rsnd_mod_write(mod
, DVC_VRDBR
, vrdbr
);
174 /* Digital Volume Function Parameter */
175 rsnd_dvc_volume_parameter(io
, mod
);
177 /* cancel operation */
178 rsnd_mod_write(mod
, DVC_DVUIR
, 0);
181 static void rsnd_dvc_volume_update(struct rsnd_dai_stream
*io
,
182 struct rsnd_mod
*mod
)
184 struct rsnd_dvc
*dvc
= rsnd_mod_to_dvc(mod
);
190 for (i
= 0; i
< dvc
->mute
.cfg
.size
; i
++)
191 zcmcr
|= (!!dvc
->mute
.cfg
.val
[i
]) << i
;
194 vrpdr
= rsnd_dvc_get_vrpdr(dvc
);
195 vrdbr
= rsnd_dvc_get_vrdbr(dvc
);
198 /* Disable DVC Register access */
199 rsnd_mod_write(mod
, DVC_DVUER
, 0);
201 /* Zero Cross Mute Function */
202 rsnd_mod_write(mod
, DVC_ZCMCR
, zcmcr
);
204 /* Volume Ramp Function */
205 rsnd_mod_write(mod
, DVC_VRPDR
, vrpdr
);
206 rsnd_mod_write(mod
, DVC_VRDBR
, vrdbr
);
207 /* add DVC_VRWTR here */
209 /* Digital Volume Function Parameter */
210 rsnd_dvc_volume_parameter(io
, mod
);
212 /* Enable DVC Register access */
213 rsnd_mod_write(mod
, DVC_DVUER
, 1);
216 static int rsnd_dvc_probe_(struct rsnd_mod
*mod
,
217 struct rsnd_dai_stream
*io
,
218 struct rsnd_priv
*priv
)
220 return rsnd_cmd_attach(io
, rsnd_mod_id(mod
));
223 static int rsnd_dvc_remove_(struct rsnd_mod
*mod
,
224 struct rsnd_dai_stream
*io
,
225 struct rsnd_priv
*priv
)
227 struct rsnd_dvc
*dvc
= rsnd_mod_to_dvc(mod
);
229 rsnd_kctrl_remove(dvc
->volume
);
230 rsnd_kctrl_remove(dvc
->mute
);
231 rsnd_kctrl_remove(dvc
->ren
);
232 rsnd_kctrl_remove(dvc
->rup
);
233 rsnd_kctrl_remove(dvc
->rdown
);
238 static int rsnd_dvc_init(struct rsnd_mod
*mod
,
239 struct rsnd_dai_stream
*io
,
240 struct rsnd_priv
*priv
)
242 rsnd_mod_power_on(mod
);
244 rsnd_dvc_activation(mod
);
246 rsnd_dvc_volume_init(io
, mod
);
248 rsnd_dvc_volume_update(io
, mod
);
253 static int rsnd_dvc_quit(struct rsnd_mod
*mod
,
254 struct rsnd_dai_stream
*io
,
255 struct rsnd_priv
*priv
)
259 rsnd_mod_power_off(mod
);
264 static int rsnd_dvc_pcm_new(struct rsnd_mod
*mod
,
265 struct rsnd_dai_stream
*io
,
266 struct snd_soc_pcm_runtime
*rtd
)
268 struct rsnd_dvc
*dvc
= rsnd_mod_to_dvc(mod
);
269 int is_play
= rsnd_io_is_play(io
);
270 int slots
= rsnd_get_slot(io
);
274 ret
= rsnd_kctrl_new_m(mod
, io
, rtd
,
276 "DVC Out Playback Volume" : "DVC In Capture Volume",
277 rsnd_dvc_volume_update
,
284 ret
= rsnd_kctrl_new_m(mod
, io
, rtd
,
286 "DVC Out Mute Switch" : "DVC In Mute Switch",
287 rsnd_dvc_volume_update
,
294 ret
= rsnd_kctrl_new_s(mod
, io
, rtd
,
296 "DVC Out Ramp Switch" : "DVC In Ramp Switch",
297 rsnd_dvc_volume_update
,
302 ret
= rsnd_kctrl_new_e(mod
, io
, rtd
,
304 "DVC Out Ramp Up Rate" : "DVC In Ramp Up Rate",
306 rsnd_dvc_volume_update
,
307 dvc_ramp_rate
, ARRAY_SIZE(dvc_ramp_rate
));
311 ret
= rsnd_kctrl_new_e(mod
, io
, rtd
,
313 "DVC Out Ramp Down Rate" : "DVC In Ramp Down Rate",
315 rsnd_dvc_volume_update
,
316 dvc_ramp_rate
, ARRAY_SIZE(dvc_ramp_rate
));
324 static struct dma_chan
*rsnd_dvc_dma_req(struct rsnd_dai_stream
*io
,
325 struct rsnd_mod
*mod
)
327 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
329 return rsnd_dma_request_channel(rsnd_dvc_of_node(priv
),
333 static struct rsnd_mod_ops rsnd_dvc_ops
= {
335 .dma_req
= rsnd_dvc_dma_req
,
336 .probe
= rsnd_dvc_probe_
,
337 .remove
= rsnd_dvc_remove_
,
338 .init
= rsnd_dvc_init
,
339 .quit
= rsnd_dvc_quit
,
340 .pcm_new
= rsnd_dvc_pcm_new
,
343 struct rsnd_mod
*rsnd_dvc_mod_get(struct rsnd_priv
*priv
, int id
)
345 if (WARN_ON(id
< 0 || id
>= rsnd_dvc_nr(priv
)))
348 return rsnd_mod_get(rsnd_dvc_get(priv
, id
));
351 int rsnd_dvc_probe(struct rsnd_priv
*priv
)
353 struct device_node
*node
;
354 struct device_node
*np
;
355 struct device
*dev
= rsnd_priv_to_dev(priv
);
356 struct rsnd_dvc
*dvc
;
358 char name
[RSND_DVC_NAME_SIZE
];
361 /* This driver doesn't support Gen1 at this point */
362 if (rsnd_is_gen1(priv
))
365 node
= rsnd_dvc_of_node(priv
);
367 return 0; /* not used is not error */
369 nr
= of_get_child_count(node
);
372 goto rsnd_dvc_probe_done
;
375 dvc
= devm_kzalloc(dev
, sizeof(*dvc
) * nr
, GFP_KERNEL
);
378 goto rsnd_dvc_probe_done
;
386 for_each_child_of_node(node
, np
) {
387 dvc
= rsnd_dvc_get(priv
, i
);
389 snprintf(name
, RSND_DVC_NAME_SIZE
, "%s.%d",
392 clk
= devm_clk_get(dev
, name
);
395 goto rsnd_dvc_probe_done
;
398 ret
= rsnd_mod_init(priv
, rsnd_mod_get(dvc
), &rsnd_dvc_ops
,
399 clk
, rsnd_mod_get_status
, RSND_MOD_DVC
, i
);
401 goto rsnd_dvc_probe_done
;
412 void rsnd_dvc_remove(struct rsnd_priv
*priv
)
414 struct rsnd_dvc
*dvc
;
417 for_each_rsnd_dvc(dvc
, priv
, i
) {
418 rsnd_mod_quit(rsnd_mod_get(dvc
));