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