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