GNU Linux-libre 4.19.264-gnu1
[releases.git] / sound / soc / sh / rcar / dvc.c
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Renesas R-Car DVC support
4 //
5 // Copyright (C) 2014 Renesas Solutions Corp.
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7
8 /*
9  * Playback Volume
10  *      amixer set "DVC Out" 100%
11  *
12  * Capture Volume
13  *      amixer set "DVC In" 100%
14  *
15  * Playback Mute
16  *      amixer set "DVC Out Mute" on
17  *
18  * Capture Mute
19  *      amixer set "DVC In Mute" on
20  *
21  * Volume Ramp
22  *      amixer set "DVC Out Ramp Up Rate"   "0.125 dB/64 steps"
23  *      amixer set "DVC Out Ramp Down Rate" "0.125 dB/512 steps"
24  *      amixer set "DVC Out Ramp" on
25  *      aplay xxx.wav &
26  *      amixer set "DVC Out"  80%  // Volume Down
27  *      amixer set "DVC Out" 100%  // Volume Up
28  */
29
30 #include "rsnd.h"
31
32 #define RSND_DVC_NAME_SIZE      16
33
34 #define DVC_NAME "dvc"
35
36 struct rsnd_dvc {
37         struct rsnd_mod mod;
38         struct rsnd_kctrl_cfg_m volume;
39         struct rsnd_kctrl_cfg_m mute;
40         struct rsnd_kctrl_cfg_s ren;    /* Ramp Enable */
41         struct rsnd_kctrl_cfg_s rup;    /* Ramp Rate Up */
42         struct rsnd_kctrl_cfg_s rdown;  /* Ramp Rate Down */
43 };
44
45 #define rsnd_dvc_get(priv, id) ((struct rsnd_dvc *)(priv->dvc) + id)
46 #define rsnd_dvc_nr(priv) ((priv)->dvc_nr)
47
48 #define rsnd_mod_to_dvc(_mod)   \
49         container_of((_mod), struct rsnd_dvc, mod)
50
51 #define for_each_rsnd_dvc(pos, priv, i)                         \
52         for ((i) = 0;                                           \
53              ((i) < rsnd_dvc_nr(priv)) &&                       \
54              ((pos) = (struct rsnd_dvc *)(priv)->dvc + i);      \
55              i++)
56
57 static void rsnd_dvc_activation(struct rsnd_mod *mod)
58 {
59         rsnd_mod_write(mod, DVC_SWRSR, 0);
60         rsnd_mod_write(mod, DVC_SWRSR, 1);
61 }
62
63 static void rsnd_dvc_halt(struct rsnd_mod *mod)
64 {
65         rsnd_mod_write(mod, DVC_DVUIR, 1);
66         rsnd_mod_write(mod, DVC_SWRSR, 0);
67 }
68
69 #define rsnd_dvc_get_vrpdr(dvc) (rsnd_kctrl_vals(dvc->rup) << 8 | \
70                                  rsnd_kctrl_vals(dvc->rdown))
71 #define rsnd_dvc_get_vrdbr(dvc) (0x3ff - (rsnd_kctrl_valm(dvc->volume, 0) >> 13))
72
73 static void rsnd_dvc_volume_parameter(struct rsnd_dai_stream *io,
74                                               struct rsnd_mod *mod)
75 {
76         struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
77         u32 val[RSND_MAX_CHANNELS];
78         int i;
79
80         /* Enable Ramp */
81         if (rsnd_kctrl_vals(dvc->ren))
82                 for (i = 0; i < RSND_MAX_CHANNELS; i++)
83                         val[i] = rsnd_kctrl_max(dvc->volume);
84         else
85                 for (i = 0; i < RSND_MAX_CHANNELS; i++)
86                         val[i] = rsnd_kctrl_valm(dvc->volume, i);
87
88         /* Enable Digital Volume */
89         rsnd_mod_write(mod, DVC_VOL0R, val[0]);
90         rsnd_mod_write(mod, DVC_VOL1R, val[1]);
91         rsnd_mod_write(mod, DVC_VOL2R, val[2]);
92         rsnd_mod_write(mod, DVC_VOL3R, val[3]);
93         rsnd_mod_write(mod, DVC_VOL4R, val[4]);
94         rsnd_mod_write(mod, DVC_VOL5R, val[5]);
95         rsnd_mod_write(mod, DVC_VOL6R, val[6]);
96         rsnd_mod_write(mod, DVC_VOL7R, val[7]);
97 }
98
99 static void rsnd_dvc_volume_init(struct rsnd_dai_stream *io,
100                                  struct rsnd_mod *mod)
101 {
102         struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
103         u32 adinr = 0;
104         u32 dvucr = 0;
105         u32 vrctr = 0;
106         u32 vrpdr = 0;
107         u32 vrdbr = 0;
108
109         adinr = rsnd_get_adinr_bit(mod, io) |
110                 rsnd_runtime_channel_after_ctu(io);
111
112         /* Enable Digital Volume, Zero Cross Mute Mode */
113         dvucr |= 0x101;
114
115         /* Enable Ramp */
116         if (rsnd_kctrl_vals(dvc->ren)) {
117                 dvucr |= 0x10;
118
119                 /*
120                  * FIXME !!
121                  * use scale-downed Digital Volume
122                  * as Volume Ramp
123                  * 7F FFFF -> 3FF
124                  */
125                 vrctr = 0xff;
126                 vrpdr = rsnd_dvc_get_vrpdr(dvc);
127                 vrdbr = rsnd_dvc_get_vrdbr(dvc);
128         }
129
130         /* Initialize operation */
131         rsnd_mod_write(mod, DVC_DVUIR, 1);
132
133         /* General Information */
134         rsnd_mod_write(mod, DVC_ADINR, adinr);
135         rsnd_mod_write(mod, DVC_DVUCR, dvucr);
136
137         /* Volume Ramp Parameter */
138         rsnd_mod_write(mod, DVC_VRCTR, vrctr);
139         rsnd_mod_write(mod, DVC_VRPDR, vrpdr);
140         rsnd_mod_write(mod, DVC_VRDBR, vrdbr);
141
142         /* Digital Volume Function Parameter */
143         rsnd_dvc_volume_parameter(io, mod);
144
145         /* cancel operation */
146         rsnd_mod_write(mod, DVC_DVUIR, 0);
147 }
148
149 static void rsnd_dvc_volume_update(struct rsnd_dai_stream *io,
150                                    struct rsnd_mod *mod)
151 {
152         struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
153         u32 zcmcr = 0;
154         u32 vrpdr = 0;
155         u32 vrdbr = 0;
156         int i;
157
158         for (i = 0; i < rsnd_kctrl_size(dvc->mute); i++)
159                 zcmcr |= (!!rsnd_kctrl_valm(dvc->mute, i)) << i;
160
161         if (rsnd_kctrl_vals(dvc->ren)) {
162                 vrpdr = rsnd_dvc_get_vrpdr(dvc);
163                 vrdbr = rsnd_dvc_get_vrdbr(dvc);
164         }
165
166         /* Disable DVC Register access */
167         rsnd_mod_write(mod, DVC_DVUER, 0);
168
169         /* Zero Cross Mute Function */
170         rsnd_mod_write(mod, DVC_ZCMCR, zcmcr);
171
172         /* Volume Ramp Function */
173         rsnd_mod_write(mod, DVC_VRPDR, vrpdr);
174         rsnd_mod_write(mod, DVC_VRDBR, vrdbr);
175         /* add DVC_VRWTR here */
176
177         /* Digital Volume Function Parameter */
178         rsnd_dvc_volume_parameter(io, mod);
179
180         /* Enable DVC Register access */
181         rsnd_mod_write(mod, DVC_DVUER, 1);
182 }
183
184 static int rsnd_dvc_probe_(struct rsnd_mod *mod,
185                            struct rsnd_dai_stream *io,
186                            struct rsnd_priv *priv)
187 {
188         return rsnd_cmd_attach(io, rsnd_mod_id(mod));
189 }
190
191 static int rsnd_dvc_init(struct rsnd_mod *mod,
192                          struct rsnd_dai_stream *io,
193                          struct rsnd_priv *priv)
194 {
195         rsnd_mod_power_on(mod);
196
197         rsnd_dvc_activation(mod);
198
199         rsnd_dvc_volume_init(io, mod);
200
201         rsnd_dvc_volume_update(io, mod);
202
203         return 0;
204 }
205
206 static int rsnd_dvc_quit(struct rsnd_mod *mod,
207                          struct rsnd_dai_stream *io,
208                          struct rsnd_priv *priv)
209 {
210         rsnd_dvc_halt(mod);
211
212         rsnd_mod_power_off(mod);
213
214         return 0;
215 }
216
217 static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
218                             struct rsnd_dai_stream *io,
219                             struct snd_soc_pcm_runtime *rtd)
220 {
221         struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
222         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
223         int is_play = rsnd_io_is_play(io);
224         int channels = rsnd_rdai_channels_get(rdai);
225         int ret;
226
227         /* Volume */
228         ret = rsnd_kctrl_new_m(mod, io, rtd,
229                         is_play ?
230                         "DVC Out Playback Volume" : "DVC In Capture Volume",
231                         rsnd_kctrl_accept_anytime,
232                         rsnd_dvc_volume_update,
233                         &dvc->volume, channels,
234                         0x00800000 - 1);
235         if (ret < 0)
236                 return ret;
237
238         /* Mute */
239         ret = rsnd_kctrl_new_m(mod, io, rtd,
240                         is_play ?
241                         "DVC Out Mute Switch" : "DVC In Mute Switch",
242                         rsnd_kctrl_accept_anytime,
243                         rsnd_dvc_volume_update,
244                         &dvc->mute, channels,
245                         1);
246         if (ret < 0)
247                 return ret;
248
249         /* Ramp */
250         ret = rsnd_kctrl_new_s(mod, io, rtd,
251                         is_play ?
252                         "DVC Out Ramp Switch" : "DVC In Ramp Switch",
253                         rsnd_kctrl_accept_anytime,
254                         rsnd_dvc_volume_update,
255                         &dvc->ren, 1);
256         if (ret < 0)
257                 return ret;
258
259         ret = rsnd_kctrl_new_e(mod, io, rtd,
260                         is_play ?
261                         "DVC Out Ramp Up Rate" : "DVC In Ramp Up Rate",
262                         rsnd_kctrl_accept_anytime,
263                         rsnd_dvc_volume_update,
264                         &dvc->rup,
265                         volume_ramp_rate,
266                         VOLUME_RAMP_MAX_DVC);
267         if (ret < 0)
268                 return ret;
269
270         ret = rsnd_kctrl_new_e(mod, io, rtd,
271                         is_play ?
272                         "DVC Out Ramp Down Rate" : "DVC In Ramp Down Rate",
273                         rsnd_kctrl_accept_anytime,
274                         rsnd_dvc_volume_update,
275                         &dvc->rdown,
276                         volume_ramp_rate,
277                         VOLUME_RAMP_MAX_DVC);
278
279         if (ret < 0)
280                 return ret;
281
282         return 0;
283 }
284
285 static struct dma_chan *rsnd_dvc_dma_req(struct rsnd_dai_stream *io,
286                                          struct rsnd_mod *mod)
287 {
288         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
289
290         return rsnd_dma_request_channel(rsnd_dvc_of_node(priv),
291                                         mod, "tx");
292 }
293
294 static struct rsnd_mod_ops rsnd_dvc_ops = {
295         .name           = DVC_NAME,
296         .dma_req        = rsnd_dvc_dma_req,
297         .probe          = rsnd_dvc_probe_,
298         .init           = rsnd_dvc_init,
299         .quit           = rsnd_dvc_quit,
300         .pcm_new        = rsnd_dvc_pcm_new,
301 };
302
303 struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
304 {
305         if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
306                 id = 0;
307
308         return rsnd_mod_get(rsnd_dvc_get(priv, id));
309 }
310
311 int rsnd_dvc_probe(struct rsnd_priv *priv)
312 {
313         struct device_node *node;
314         struct device_node *np;
315         struct device *dev = rsnd_priv_to_dev(priv);
316         struct rsnd_dvc *dvc;
317         struct clk *clk;
318         char name[RSND_DVC_NAME_SIZE];
319         int i, nr, ret;
320
321         /* This driver doesn't support Gen1 at this point */
322         if (rsnd_is_gen1(priv))
323                 return 0;
324
325         node = rsnd_dvc_of_node(priv);
326         if (!node)
327                 return 0; /* not used is not error */
328
329         nr = of_get_child_count(node);
330         if (!nr) {
331                 ret = -EINVAL;
332                 goto rsnd_dvc_probe_done;
333         }
334
335         dvc     = devm_kcalloc(dev, nr, sizeof(*dvc), GFP_KERNEL);
336         if (!dvc) {
337                 ret = -ENOMEM;
338                 goto rsnd_dvc_probe_done;
339         }
340
341         priv->dvc_nr    = nr;
342         priv->dvc       = dvc;
343
344         i = 0;
345         ret = 0;
346         for_each_child_of_node(node, np) {
347                 dvc = rsnd_dvc_get(priv, i);
348
349                 snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
350                          DVC_NAME, i);
351
352                 clk = devm_clk_get(dev, name);
353                 if (IS_ERR(clk)) {
354                         ret = PTR_ERR(clk);
355                         of_node_put(np);
356                         goto rsnd_dvc_probe_done;
357                 }
358
359                 ret = rsnd_mod_init(priv, rsnd_mod_get(dvc), &rsnd_dvc_ops,
360                                     clk, rsnd_mod_get_status, RSND_MOD_DVC, i);
361                 if (ret) {
362                         of_node_put(np);
363                         goto rsnd_dvc_probe_done;
364                 }
365
366                 i++;
367         }
368
369 rsnd_dvc_probe_done:
370         of_node_put(node);
371
372         return ret;
373 }
374
375 void rsnd_dvc_remove(struct rsnd_priv *priv)
376 {
377         struct rsnd_dvc *dvc;
378         int i;
379
380         for_each_rsnd_dvc(dvc, priv, i) {
381                 rsnd_mod_quit(rsnd_mod_get(dvc));
382         }
383 }