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