e2c8473267b63631e20a6ef3f04ceb5ae5fd6e2c
[firefly-linux-kernel-4.4.55.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         const char * const *texts;
23 };
24
25 struct rsnd_dvc_cfg_m {
26         struct rsnd_dvc_cfg cfg;
27         u32 val[RSND_DVC_CHANNELS];
28 };
29
30 struct rsnd_dvc_cfg_s {
31         struct rsnd_dvc_cfg cfg;
32         u32 val;
33 };
34
35 struct rsnd_dvc {
36         struct rsnd_dvc_platform_info *info; /* rcar_snd.h */
37         struct rsnd_mod mod;
38         struct clk *clk;
39         struct rsnd_dvc_cfg_m volume;
40         struct rsnd_dvc_cfg_m mute;
41         struct rsnd_dvc_cfg_s ren;      /* Ramp Enable */
42         struct rsnd_dvc_cfg_s rup;      /* Ramp Rate Up */
43         struct rsnd_dvc_cfg_s rdown;    /* Ramp Rate Down */
44 };
45
46 #define rsnd_mod_to_dvc(_mod)   \
47         container_of((_mod), struct rsnd_dvc, mod)
48
49 #define for_each_rsnd_dvc(pos, priv, i)                         \
50         for ((i) = 0;                                           \
51              ((i) < rsnd_dvc_nr(priv)) &&                       \
52              ((pos) = (struct rsnd_dvc *)(priv)->dvc + i);      \
53              i++)
54
55 static const char const *dvc_ramp_rate[] = {
56         "128 dB/1 step",         /* 00000 */
57         "64 dB/1 step",          /* 00001 */
58         "32 dB/1 step",          /* 00010 */
59         "16 dB/1 step",          /* 00011 */
60         "8 dB/1 step",           /* 00100 */
61         "4 dB/1 step",           /* 00101 */
62         "2 dB/1 step",           /* 00110 */
63         "1 dB/1 step",           /* 00111 */
64         "0.5 dB/1 step",         /* 01000 */
65         "0.25 dB/1 step",        /* 01001 */
66         "0.125 dB/1 step",       /* 01010 */
67         "0.125 dB/2 steps",      /* 01011 */
68         "0.125 dB/4 steps",      /* 01100 */
69         "0.125 dB/8 steps",      /* 01101 */
70         "0.125 dB/16 steps",     /* 01110 */
71         "0.125 dB/32 steps",     /* 01111 */
72         "0.125 dB/64 steps",     /* 10000 */
73         "0.125 dB/128 steps",    /* 10001 */
74         "0.125 dB/256 steps",    /* 10010 */
75         "0.125 dB/512 steps",    /* 10011 */
76         "0.125 dB/1024 steps",   /* 10100 */
77         "0.125 dB/2048 steps",   /* 10101 */
78         "0.125 dB/4096 steps",   /* 10110 */
79         "0.125 dB/8192 steps",   /* 10111 */
80 };
81
82 static void rsnd_dvc_volume_update(struct rsnd_mod *mod)
83 {
84         struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
85         u32 val[RSND_DVC_CHANNELS];
86         u32 dvucr = 0;
87         u32 mute = 0;
88         int i;
89
90         for (i = 0; i < dvc->mute.cfg.size; i++)
91                 mute |= (!!dvc->mute.cfg.val[i]) << i;
92
93         /* Disable DVC Register access */
94         rsnd_mod_write(mod, DVC_DVUER, 0);
95
96         /* Enable Ramp */
97         if (dvc->ren.val) {
98                 dvucr |= 0x10;
99
100                 /* Digital Volume Max */
101                 for (i = 0; i < RSND_DVC_CHANNELS; i++)
102                         val[i] = dvc->volume.cfg.max;
103
104                 rsnd_mod_write(mod, DVC_VRCTR, 0xff);
105                 rsnd_mod_write(mod, DVC_VRPDR, dvc->rup.val << 8 |
106                                                dvc->rdown.val);
107                 /*
108                  * FIXME !!
109                  * use scale-downed Digital Volume
110                  * as Volume Ramp
111                  * 7F FFFF -> 3FF
112                  */
113                 rsnd_mod_write(mod, DVC_VRDBR,
114                                0x3ff - (dvc->volume.val[0] >> 13));
115
116         } else {
117                 for (i = 0; i < RSND_DVC_CHANNELS; i++)
118                         val[i] = dvc->volume.val[i];
119         }
120
121         /* Enable Digital Volume */
122         dvucr |= 0x100;
123         rsnd_mod_write(mod, DVC_VOL0R, val[0]);
124         rsnd_mod_write(mod, DVC_VOL1R, val[1]);
125
126         /*  Enable Mute */
127         if (mute) {
128                 dvucr |= 0x1;
129                 rsnd_mod_write(mod, DVC_ZCMCR, mute);
130         }
131
132         rsnd_mod_write(mod, DVC_DVUCR, dvucr);
133
134         /* Enable DVC Register access */
135         rsnd_mod_write(mod, DVC_DVUER, 1);
136 }
137
138 static int rsnd_dvc_probe_gen2(struct rsnd_mod *mod,
139                                struct rsnd_dai *rdai)
140 {
141         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
142         struct device *dev = rsnd_priv_to_dev(priv);
143
144         dev_dbg(dev, "%s[%d] (Gen2) is probed\n",
145                 rsnd_mod_name(mod), rsnd_mod_id(mod));
146
147         return 0;
148 }
149
150 static int rsnd_dvc_init(struct rsnd_mod *dvc_mod,
151                          struct rsnd_dai *rdai)
152 {
153         struct rsnd_dvc *dvc = rsnd_mod_to_dvc(dvc_mod);
154         struct rsnd_dai_stream *io = rsnd_mod_to_io(dvc_mod);
155         struct rsnd_priv *priv = rsnd_mod_to_priv(dvc_mod);
156         struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
157         struct device *dev = rsnd_priv_to_dev(priv);
158         int dvc_id = rsnd_mod_id(dvc_mod);
159         int src_id = rsnd_mod_id(src_mod);
160         u32 route[] = {
161                 [0] = 0x30000,
162                 [1] = 0x30001,
163                 [2] = 0x40000,
164                 [3] = 0x10000,
165                 [4] = 0x20000,
166                 [5] = 0x40100
167         };
168
169         if (src_id >= ARRAY_SIZE(route)) {
170                 dev_err(dev, "DVC%d isn't connected to SRC%d\n", dvc_id, src_id);
171                 return -EINVAL;
172         }
173
174         clk_prepare_enable(dvc->clk);
175
176         /*
177          * fixme
178          * it doesn't support CTU/MIX
179          */
180         rsnd_mod_write(dvc_mod, CMD_ROUTE_SLCT, route[src_id]);
181
182         rsnd_mod_write(dvc_mod, DVC_SWRSR, 0);
183         rsnd_mod_write(dvc_mod, DVC_SWRSR, 1);
184
185         rsnd_mod_write(dvc_mod, DVC_DVUIR, 1);
186
187         rsnd_mod_write(dvc_mod, DVC_ADINR, rsnd_get_adinr(dvc_mod));
188
189         /* ch0/ch1 Volume */
190         rsnd_dvc_volume_update(dvc_mod);
191
192         rsnd_mod_write(dvc_mod, DVC_DVUIR, 0);
193
194         rsnd_adg_set_cmd_timsel_gen2(rdai, dvc_mod, io);
195
196         return 0;
197 }
198
199 static int rsnd_dvc_quit(struct rsnd_mod *mod,
200                          struct rsnd_dai *rdai)
201 {
202         struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
203
204         clk_disable_unprepare(dvc->clk);
205
206         return 0;
207 }
208
209 static int rsnd_dvc_start(struct rsnd_mod *mod,
210                           struct rsnd_dai *rdai)
211 {
212         rsnd_mod_write(mod, CMD_CTRL, 0x10);
213
214         return 0;
215 }
216
217 static int rsnd_dvc_stop(struct rsnd_mod *mod,
218                          struct rsnd_dai *rdai)
219 {
220         rsnd_mod_write(mod, CMD_CTRL, 0);
221
222         return 0;
223 }
224
225 static int rsnd_dvc_volume_info(struct snd_kcontrol *kctrl,
226                                struct snd_ctl_elem_info *uinfo)
227 {
228         struct rsnd_dvc_cfg *cfg = (struct rsnd_dvc_cfg *)kctrl->private_value;
229
230         if (cfg->texts) {
231                 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
232                 uinfo->count = cfg->size;
233                 uinfo->value.enumerated.items = cfg->max;
234                 if (uinfo->value.enumerated.item >= cfg->max)
235                         uinfo->value.enumerated.item = cfg->max - 1;
236                 strlcpy(uinfo->value.enumerated.name,
237                         cfg->texts[uinfo->value.enumerated.item],
238                         sizeof(uinfo->value.enumerated.name));
239         } else {
240                 uinfo->count = cfg->size;
241                 uinfo->value.integer.min = 0;
242                 uinfo->value.integer.max = cfg->max;
243                 uinfo->type = (cfg->max == 1) ?
244                         SNDRV_CTL_ELEM_TYPE_BOOLEAN :
245                         SNDRV_CTL_ELEM_TYPE_INTEGER;
246         }
247
248         return 0;
249 }
250
251 static int rsnd_dvc_volume_get(struct snd_kcontrol *kctrl,
252                               struct snd_ctl_elem_value *ucontrol)
253 {
254         struct rsnd_dvc_cfg *cfg = (struct rsnd_dvc_cfg *)kctrl->private_value;
255         int i;
256
257         for (i = 0; i < cfg->size; i++)
258                 if (cfg->texts)
259                         ucontrol->value.enumerated.item[i] = cfg->val[i];
260                 else
261                         ucontrol->value.integer.value[i] = cfg->val[i];
262
263         return 0;
264 }
265
266 static int rsnd_dvc_volume_put(struct snd_kcontrol *kctrl,
267                               struct snd_ctl_elem_value *ucontrol)
268 {
269         struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
270         struct rsnd_dvc_cfg *cfg = (struct rsnd_dvc_cfg *)kctrl->private_value;
271         int i, change = 0;
272
273         for (i = 0; i < cfg->size; i++) {
274                 if (cfg->texts) {
275                         change |= (ucontrol->value.enumerated.item[i] != cfg->val[i]);
276                         cfg->val[i] = ucontrol->value.enumerated.item[i];
277                 } else {
278                         change |= (ucontrol->value.integer.value[i] != cfg->val[i]);
279                         cfg->val[i] = ucontrol->value.integer.value[i];
280                 }
281         }
282
283         if (change)
284                 rsnd_dvc_volume_update(mod);
285
286         return change;
287 }
288
289 static int __rsnd_dvc_pcm_new(struct rsnd_mod *mod,
290                               struct rsnd_dai *rdai,
291                               struct snd_soc_pcm_runtime *rtd,
292                               const unsigned char *name,
293                               struct rsnd_dvc_cfg *private)
294 {
295         struct snd_card *card = rtd->card->snd_card;
296         struct snd_kcontrol *kctrl;
297         struct snd_kcontrol_new knew = {
298                 .iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
299                 .name           = name,
300                 .info           = rsnd_dvc_volume_info,
301                 .get            = rsnd_dvc_volume_get,
302                 .put            = rsnd_dvc_volume_put,
303                 .private_value  = (unsigned long)private,
304         };
305         int ret;
306
307         kctrl = snd_ctl_new1(&knew, mod);
308         if (!kctrl)
309                 return -ENOMEM;
310
311         ret = snd_ctl_add(card, kctrl);
312         if (ret < 0)
313                 return ret;
314
315         return 0;
316 }
317
318 static int _rsnd_dvc_pcm_new_m(struct rsnd_mod *mod,
319                                struct rsnd_dai *rdai,
320                                struct snd_soc_pcm_runtime *rtd,
321                                const unsigned char *name,
322                                struct rsnd_dvc_cfg_m *private,
323                                u32 max)
324 {
325         private->cfg.max        = max;
326         private->cfg.size       = RSND_DVC_CHANNELS;
327         private->cfg.val        = private->val;
328         return __rsnd_dvc_pcm_new(mod, rdai, rtd, name, &private->cfg);
329 }
330
331 static int _rsnd_dvc_pcm_new_s(struct rsnd_mod *mod,
332                                struct rsnd_dai *rdai,
333                                struct snd_soc_pcm_runtime *rtd,
334                                const unsigned char *name,
335                                struct rsnd_dvc_cfg_s *private,
336                                u32 max)
337 {
338         private->cfg.max        = max;
339         private->cfg.size       = 1;
340         private->cfg.val        = &private->val;
341         return __rsnd_dvc_pcm_new(mod, rdai, rtd, name, &private->cfg);
342 }
343
344 static int _rsnd_dvc_pcm_new_e(struct rsnd_mod *mod,
345                                struct rsnd_dai *rdai,
346                                struct snd_soc_pcm_runtime *rtd,
347                                const unsigned char *name,
348                                struct rsnd_dvc_cfg_s *private,
349                                const char * const *texts,
350                                u32 max)
351 {
352         private->cfg.max        = max;
353         private->cfg.size       = 1;
354         private->cfg.val        = &private->val;
355         private->cfg.texts      = texts;
356         return __rsnd_dvc_pcm_new(mod, rdai, rtd, name, &private->cfg);
357 }
358
359 static int rsnd_dvc_pcm_new(struct rsnd_mod *mod,
360                             struct rsnd_dai *rdai,
361                             struct snd_soc_pcm_runtime *rtd)
362 {
363         struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
364         struct rsnd_dvc *dvc = rsnd_mod_to_dvc(mod);
365         int ret;
366
367         /* Volume */
368         ret = _rsnd_dvc_pcm_new_m(mod, rdai, rtd,
369                         rsnd_dai_is_play(rdai, io) ?
370                         "DVC Out Playback Volume" : "DVC In Capture Volume",
371                         &dvc->volume, 0x00800000 - 1);
372         if (ret < 0)
373                 return ret;
374
375         /* Mute */
376         ret = _rsnd_dvc_pcm_new_m(mod, rdai, rtd,
377                         rsnd_dai_is_play(rdai, io) ?
378                         "DVC Out Mute Switch" : "DVC In Mute Switch",
379                         &dvc->mute, 1);
380         if (ret < 0)
381                 return ret;
382
383         /* Ramp */
384         ret = _rsnd_dvc_pcm_new_s(mod, rdai, rtd,
385                         rsnd_dai_is_play(rdai, io) ?
386                         "DVC Out Ramp Switch" : "DVC In Ramp Switch",
387                         &dvc->ren, 1);
388         if (ret < 0)
389                 return ret;
390
391         ret = _rsnd_dvc_pcm_new_e(mod, rdai, rtd,
392                         rsnd_dai_is_play(rdai, io) ?
393                         "DVC Out Ramp Up Rate" : "DVC In Ramp Up Rate",
394                         &dvc->rup,
395                         dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
396         if (ret < 0)
397                 return ret;
398
399         ret = _rsnd_dvc_pcm_new_e(mod, rdai, rtd,
400                         rsnd_dai_is_play(rdai, io) ?
401                         "DVC Out Ramp Down Rate" : "DVC In Ramp Down Rate",
402                         &dvc->rdown,
403                         dvc_ramp_rate, ARRAY_SIZE(dvc_ramp_rate));
404
405         if (ret < 0)
406                 return ret;
407
408         return 0;
409 }
410
411 static struct rsnd_mod_ops rsnd_dvc_ops = {
412         .name           = DVC_NAME,
413         .probe          = rsnd_dvc_probe_gen2,
414         .init           = rsnd_dvc_init,
415         .quit           = rsnd_dvc_quit,
416         .start          = rsnd_dvc_start,
417         .stop           = rsnd_dvc_stop,
418         .pcm_new        = rsnd_dvc_pcm_new,
419 };
420
421 struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id)
422 {
423         if (WARN_ON(id < 0 || id >= rsnd_dvc_nr(priv)))
424                 id = 0;
425
426         return &((struct rsnd_dvc *)(priv->dvc) + id)->mod;
427 }
428
429 static void rsnd_of_parse_dvc(struct platform_device *pdev,
430                               const struct rsnd_of_data *of_data,
431                               struct rsnd_priv *priv)
432 {
433         struct device_node *node;
434         struct rsnd_dvc_platform_info *dvc_info;
435         struct rcar_snd_info *info = rsnd_priv_to_info(priv);
436         struct device *dev = &pdev->dev;
437         int nr;
438
439         if (!of_data)
440                 return;
441
442         node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
443         if (!node)
444                 return;
445
446         nr = of_get_child_count(node);
447         if (!nr)
448                 goto rsnd_of_parse_dvc_end;
449
450         dvc_info = devm_kzalloc(dev,
451                                 sizeof(struct rsnd_dvc_platform_info) * nr,
452                                 GFP_KERNEL);
453         if (!dvc_info) {
454                 dev_err(dev, "dvc info allocation error\n");
455                 goto rsnd_of_parse_dvc_end;
456         }
457
458         info->dvc_info          = dvc_info;
459         info->dvc_info_nr       = nr;
460
461 rsnd_of_parse_dvc_end:
462         of_node_put(node);
463 }
464
465 int rsnd_dvc_probe(struct platform_device *pdev,
466                    const struct rsnd_of_data *of_data,
467                    struct rsnd_priv *priv)
468 {
469         struct rcar_snd_info *info = rsnd_priv_to_info(priv);
470         struct device *dev = rsnd_priv_to_dev(priv);
471         struct rsnd_dvc *dvc;
472         struct clk *clk;
473         char name[RSND_DVC_NAME_SIZE];
474         int i, nr;
475
476         rsnd_of_parse_dvc(pdev, of_data, priv);
477
478         nr = info->dvc_info_nr;
479         if (!nr)
480                 return 0;
481
482         /* This driver doesn't support Gen1 at this point */
483         if (rsnd_is_gen1(priv)) {
484                 dev_warn(dev, "CMD is not supported on Gen1\n");
485                 return -EINVAL;
486         }
487
488         dvc     = devm_kzalloc(dev, sizeof(*dvc) * nr, GFP_KERNEL);
489         if (!dvc) {
490                 dev_err(dev, "CMD allocate failed\n");
491                 return -ENOMEM;
492         }
493
494         priv->dvc_nr    = nr;
495         priv->dvc       = dvc;
496
497         for_each_rsnd_dvc(dvc, priv, i) {
498                 snprintf(name, RSND_DVC_NAME_SIZE, "%s.%d",
499                          DVC_NAME, i);
500
501                 clk = devm_clk_get(dev, name);
502                 if (IS_ERR(clk))
503                         return PTR_ERR(clk);
504
505                 dvc->info = &info->dvc_info[i];
506                 dvc->clk  = clk;
507
508                 rsnd_mod_init(priv, &dvc->mod, &rsnd_dvc_ops, RSND_MOD_DVC, i);
509
510                 dev_dbg(dev, "CMD%d probed\n", i);
511         }
512
513         return 0;
514 }