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