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 */
50 #define KCTRL_INITIALIZED (1 << 0)
52 #define rsnd_dvc_get(priv, id) ((struct rsnd_dvc *)(priv->dvc) + id)
53 #define rsnd_dvc_nr(priv) ((priv)->dvc_nr)
55 #define rsnd_mod_to_dvc(_mod) \
56 container_of((_mod), struct rsnd_dvc, mod)
58 #define for_each_rsnd_dvc(pos, priv, i) \
60 ((i) < rsnd_dvc_nr(priv)) && \
61 ((pos) = (struct rsnd_dvc *)(priv)->dvc + i); \
64 static void rsnd_dvc_activation(struct rsnd_mod
*mod
)
66 rsnd_mod_write(mod
, DVC_SWRSR
, 0);
67 rsnd_mod_write(mod
, DVC_SWRSR
, 1);
70 static void rsnd_dvc_halt(struct rsnd_mod
*mod
)
72 rsnd_mod_write(mod
, DVC_DVUIR
, 1);
73 rsnd_mod_write(mod
, DVC_SWRSR
, 0);
76 #define rsnd_dvc_get_vrpdr(dvc) (rsnd_kctrl_vals(dvc->rup) << 8 | \
77 rsnd_kctrl_vals(dvc->rdown))
78 #define rsnd_dvc_get_vrdbr(dvc) (0x3ff - (rsnd_kctrl_valm(dvc->volume, 0) >> 13))
80 static void rsnd_dvc_volume_parameter(struct rsnd_dai_stream
*io
,
83 struct rsnd_dvc
*dvc
= rsnd_mod_to_dvc(mod
);
84 u32 val
[RSND_MAX_CHANNELS
];
88 if (rsnd_kctrl_vals(dvc
->ren
))
89 for (i
= 0; i
< RSND_MAX_CHANNELS
; i
++)
90 val
[i
] = rsnd_kctrl_max(dvc
->volume
);
92 for (i
= 0; i
< RSND_MAX_CHANNELS
; i
++)
93 val
[i
] = rsnd_kctrl_valm(dvc
->volume
, i
);
95 /* Enable Digital Volume */
96 rsnd_mod_write(mod
, DVC_VOL0R
, val
[0]);
97 rsnd_mod_write(mod
, DVC_VOL1R
, val
[1]);
98 rsnd_mod_write(mod
, DVC_VOL2R
, val
[2]);
99 rsnd_mod_write(mod
, DVC_VOL3R
, val
[3]);
100 rsnd_mod_write(mod
, DVC_VOL4R
, val
[4]);
101 rsnd_mod_write(mod
, DVC_VOL5R
, val
[5]);
102 rsnd_mod_write(mod
, DVC_VOL6R
, val
[6]);
103 rsnd_mod_write(mod
, DVC_VOL7R
, val
[7]);
106 static void rsnd_dvc_volume_init(struct rsnd_dai_stream
*io
,
107 struct rsnd_mod
*mod
)
109 struct rsnd_dvc
*dvc
= rsnd_mod_to_dvc(mod
);
116 adinr
= rsnd_get_adinr_bit(mod
, io
) |
117 rsnd_runtime_channel_after_ctu(io
);
119 /* Enable Digital Volume, Zero Cross Mute Mode */
123 if (rsnd_kctrl_vals(dvc
->ren
)) {
128 * use scale-downed Digital Volume
133 vrpdr
= rsnd_dvc_get_vrpdr(dvc
);
134 vrdbr
= rsnd_dvc_get_vrdbr(dvc
);
137 /* Initialize operation */
138 rsnd_mod_write(mod
, DVC_DVUIR
, 1);
140 /* General Information */
141 rsnd_mod_write(mod
, DVC_ADINR
, adinr
);
142 rsnd_mod_write(mod
, DVC_DVUCR
, dvucr
);
144 /* Volume Ramp Parameter */
145 rsnd_mod_write(mod
, DVC_VRCTR
, vrctr
);
146 rsnd_mod_write(mod
, DVC_VRPDR
, vrpdr
);
147 rsnd_mod_write(mod
, DVC_VRDBR
, vrdbr
);
149 /* Digital Volume Function Parameter */
150 rsnd_dvc_volume_parameter(io
, mod
);
152 /* cancel operation */
153 rsnd_mod_write(mod
, DVC_DVUIR
, 0);
156 static void rsnd_dvc_volume_update(struct rsnd_dai_stream
*io
,
157 struct rsnd_mod
*mod
)
159 struct rsnd_dvc
*dvc
= rsnd_mod_to_dvc(mod
);
165 for (i
= 0; i
< rsnd_kctrl_size(dvc
->mute
); i
++)
166 zcmcr
|= (!!rsnd_kctrl_valm(dvc
->mute
, i
)) << i
;
168 if (rsnd_kctrl_vals(dvc
->ren
)) {
169 vrpdr
= rsnd_dvc_get_vrpdr(dvc
);
170 vrdbr
= rsnd_dvc_get_vrdbr(dvc
);
173 /* Disable DVC Register access */
174 rsnd_mod_write(mod
, DVC_DVUER
, 0);
176 /* Zero Cross Mute Function */
177 rsnd_mod_write(mod
, DVC_ZCMCR
, zcmcr
);
179 /* Volume Ramp Function */
180 rsnd_mod_write(mod
, DVC_VRPDR
, vrpdr
);
181 rsnd_mod_write(mod
, DVC_VRDBR
, vrdbr
);
182 /* add DVC_VRWTR here */
184 /* Digital Volume Function Parameter */
185 rsnd_dvc_volume_parameter(io
, mod
);
187 /* Enable DVC Register access */
188 rsnd_mod_write(mod
, DVC_DVUER
, 1);
191 static int rsnd_dvc_probe_(struct rsnd_mod
*mod
,
192 struct rsnd_dai_stream
*io
,
193 struct rsnd_priv
*priv
)
195 return rsnd_cmd_attach(io
, rsnd_mod_id(mod
));
198 static int rsnd_dvc_init(struct rsnd_mod
*mod
,
199 struct rsnd_dai_stream
*io
,
200 struct rsnd_priv
*priv
)
202 rsnd_mod_power_on(mod
);
204 rsnd_dvc_activation(mod
);
206 rsnd_dvc_volume_init(io
, mod
);
208 rsnd_dvc_volume_update(io
, mod
);
213 static int rsnd_dvc_quit(struct rsnd_mod
*mod
,
214 struct rsnd_dai_stream
*io
,
215 struct rsnd_priv
*priv
)
219 rsnd_mod_power_off(mod
);
224 static int rsnd_dvc_pcm_new(struct rsnd_mod
*mod
,
225 struct rsnd_dai_stream
*io
,
226 struct snd_soc_pcm_runtime
*rtd
)
228 struct rsnd_dvc
*dvc
= rsnd_mod_to_dvc(mod
);
229 struct rsnd_dai
*rdai
= rsnd_io_to_rdai(io
);
230 int is_play
= rsnd_io_is_play(io
);
231 int channels
= rsnd_rdai_channels_get(rdai
);
234 if (rsnd_flags_has(dvc
, KCTRL_INITIALIZED
))
238 ret
= rsnd_kctrl_new_m(mod
, io
, rtd
,
240 "DVC Out Playback Volume" : "DVC In Capture Volume",
241 rsnd_kctrl_accept_anytime
,
242 rsnd_dvc_volume_update
,
243 &dvc
->volume
, channels
,
249 ret
= rsnd_kctrl_new_m(mod
, io
, rtd
,
251 "DVC Out Mute Switch" : "DVC In Mute Switch",
252 rsnd_kctrl_accept_anytime
,
253 rsnd_dvc_volume_update
,
254 &dvc
->mute
, channels
,
260 ret
= rsnd_kctrl_new_s(mod
, io
, rtd
,
262 "DVC Out Ramp Switch" : "DVC In Ramp Switch",
263 rsnd_kctrl_accept_anytime
,
264 rsnd_dvc_volume_update
,
269 ret
= rsnd_kctrl_new_e(mod
, io
, rtd
,
271 "DVC Out Ramp Up Rate" : "DVC In Ramp Up Rate",
272 rsnd_kctrl_accept_anytime
,
273 rsnd_dvc_volume_update
,
276 VOLUME_RAMP_MAX_DVC
);
280 ret
= rsnd_kctrl_new_e(mod
, io
, rtd
,
282 "DVC Out Ramp Down Rate" : "DVC In Ramp Down Rate",
283 rsnd_kctrl_accept_anytime
,
284 rsnd_dvc_volume_update
,
287 VOLUME_RAMP_MAX_DVC
);
292 rsnd_flags_set(dvc
, KCTRL_INITIALIZED
);
297 static struct dma_chan
*rsnd_dvc_dma_req(struct rsnd_dai_stream
*io
,
298 struct rsnd_mod
*mod
)
300 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
302 return rsnd_dma_request_channel(rsnd_dvc_of_node(priv
),
306 static struct rsnd_mod_ops rsnd_dvc_ops
= {
308 .dma_req
= rsnd_dvc_dma_req
,
309 .probe
= rsnd_dvc_probe_
,
310 .init
= rsnd_dvc_init
,
311 .quit
= rsnd_dvc_quit
,
312 .pcm_new
= rsnd_dvc_pcm_new
,
315 struct rsnd_mod
*rsnd_dvc_mod_get(struct rsnd_priv
*priv
, int id
)
317 if (WARN_ON(id
< 0 || id
>= rsnd_dvc_nr(priv
)))
320 return rsnd_mod_get(rsnd_dvc_get(priv
, id
));
323 int rsnd_dvc_probe(struct rsnd_priv
*priv
)
325 struct device_node
*node
;
326 struct device_node
*np
;
327 struct device
*dev
= rsnd_priv_to_dev(priv
);
328 struct rsnd_dvc
*dvc
;
330 char name
[RSND_DVC_NAME_SIZE
];
333 /* This driver doesn't support Gen1 at this point */
334 if (rsnd_is_gen1(priv
))
337 node
= rsnd_dvc_of_node(priv
);
339 return 0; /* not used is not error */
341 nr
= of_get_child_count(node
);
344 goto rsnd_dvc_probe_done
;
347 dvc
= devm_kcalloc(dev
, nr
, sizeof(*dvc
), GFP_KERNEL
);
350 goto rsnd_dvc_probe_done
;
358 for_each_child_of_node(node
, np
) {
359 dvc
= rsnd_dvc_get(priv
, i
);
361 snprintf(name
, RSND_DVC_NAME_SIZE
, "%s.%d",
364 clk
= devm_clk_get(dev
, name
);
368 goto rsnd_dvc_probe_done
;
371 ret
= rsnd_mod_init(priv
, rsnd_mod_get(dvc
), &rsnd_dvc_ops
,
372 clk
, rsnd_mod_get_status
, RSND_MOD_DVC
, i
);
375 goto rsnd_dvc_probe_done
;
387 void rsnd_dvc_remove(struct rsnd_priv
*priv
)
389 struct rsnd_dvc
*dvc
;
392 for_each_rsnd_dvc(dvc
, priv
, i
) {
393 rsnd_mod_quit(rsnd_mod_get(dvc
));