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