GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / media / pci / ivtv / ivtv-alsa-mixer.c
1 /*
2  *  ALSA mixer controls for the
3  *  ALSA interface to ivtv PCM capture streams
4  *
5  *  Copyright (C) 2009,2012  Andy Walls <awalls@md.metrocast.net>
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 as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  */
17
18 #include "ivtv-alsa.h"
19 #include "ivtv-alsa-mixer.h"
20 #include "ivtv-driver.h"
21
22 #include <linux/videodev2.h>
23
24 #include <sound/core.h>
25 #include <sound/control.h>
26 #include <sound/tlv.h>
27
28 /*
29  * Note the cx25840-core volume scale is funny, due to the alignment of the
30  * scale with another chip's range:
31  *
32  * v4l2_control value   /512    indicated dB    actual dB       reg 0x8d4
33  * 0x0000 - 0x01ff        0     -119            -96             228
34  * 0x0200 - 0x02ff        1     -118            -96             228
35  * ...
36  * 0x2c00 - 0x2dff       22      -97            -96             228
37  * 0x2e00 - 0x2fff       23      -96            -96             228
38  * 0x3000 - 0x31ff       24      -95            -95             226
39  * ...
40  * 0xee00 - 0xefff      119        0              0              36
41  * ...
42  * 0xfe00 - 0xffff      127       +8             +8              20
43  */
44 static inline int dB_to_cx25840_vol(int dB)
45 {
46         if (dB < -96)
47                 dB = -96;
48         else if (dB > 8)
49                 dB = 8;
50         return (dB + 119) << 9;
51 }
52
53 static inline int cx25840_vol_to_dB(int v)
54 {
55         if (v < (23 << 9))
56                 v = (23 << 9);
57         else if (v > (127 << 9))
58                 v = (127 << 9);
59         return (v >> 9) - 119;
60 }
61
62 static int snd_ivtv_mixer_tv_vol_info(struct snd_kcontrol *kcontrol,
63                                       struct snd_ctl_elem_info *uinfo)
64 {
65         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
66         uinfo->count = 1;
67         /* We're already translating values, just keep this control in dB */
68         uinfo->value.integer.min  = -96;
69         uinfo->value.integer.max  =   8;
70         uinfo->value.integer.step =   1;
71         return 0;
72 }
73
74 static int snd_ivtv_mixer_tv_vol_get(struct snd_kcontrol *kctl,
75                                      struct snd_ctl_elem_value *uctl)
76 {
77         struct snd_ivtv_card *itvsc = snd_kcontrol_chip(kctl);
78         struct ivtv *itv = to_ivtv(itvsc->v4l2_dev);
79         struct v4l2_control vctrl;
80         int ret;
81
82         vctrl.id = V4L2_CID_AUDIO_VOLUME;
83         vctrl.value = dB_to_cx25840_vol(uctl->value.integer.value[0]);
84
85         snd_ivtv_lock(itvsc);
86         ret = v4l2_g_ctrl(itv->sd_audio->ctrl_handler, &vctrl);
87         snd_ivtv_unlock(itvsc);
88
89         if (!ret)
90                 uctl->value.integer.value[0] = cx25840_vol_to_dB(vctrl.value);
91         return ret;
92 }
93
94 static int snd_ivtv_mixer_tv_vol_put(struct snd_kcontrol *kctl,
95                                      struct snd_ctl_elem_value *uctl)
96 {
97         struct snd_ivtv_card *itvsc = snd_kcontrol_chip(kctl);
98         struct ivtv *itv = to_ivtv(itvsc->v4l2_dev);
99         struct v4l2_control vctrl;
100         int ret;
101
102         vctrl.id = V4L2_CID_AUDIO_VOLUME;
103         vctrl.value = dB_to_cx25840_vol(uctl->value.integer.value[0]);
104
105         snd_ivtv_lock(itvsc);
106
107         /* Fetch current state */
108         ret = v4l2_g_ctrl(itv->sd_audio->ctrl_handler, &vctrl);
109
110         if (ret ||
111             (cx25840_vol_to_dB(vctrl.value) != uctl->value.integer.value[0])) {
112
113                 /* Set, if needed */
114                 vctrl.value = dB_to_cx25840_vol(uctl->value.integer.value[0]);
115                 ret = v4l2_s_ctrl(itv->sd_audio->ctrl_handler, &vctrl);
116                 if (!ret)
117                         ret = 1; /* Indicate control was changed w/o error */
118         }
119         snd_ivtv_unlock(itvsc);
120
121         return ret;
122 }
123
124
125 /* This is a bit of overkill, the slider is already in dB internally */
126 static DECLARE_TLV_DB_SCALE(snd_ivtv_mixer_tv_vol_db_scale, -9600, 100, 0);
127
128 static struct snd_kcontrol_new snd_ivtv_mixer_tv_vol __initdata = {
129         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
130         .name = "Analog TV Capture Volume",
131         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
132                   SNDRV_CTL_ELEM_ACCESS_TLV_READ,
133         .info = snd_ivtv_mixer_tv_volume_info,
134         .get = snd_ivtv_mixer_tv_volume_get,
135         .put = snd_ivtv_mixer_tv_volume_put,
136         .tlv.p = snd_ivtv_mixer_tv_vol_db_scale
137 };
138
139 /* FIXME - add mute switch and balance, bass, treble sliders:
140         V4L2_CID_AUDIO_MUTE
141
142         V4L2_CID_AUDIO_BALANCE
143
144         V4L2_CID_AUDIO_BASS
145         V4L2_CID_AUDIO_TREBLE
146 */
147
148 /* FIXME - add stereo, lang1, lang2, mono menu */
149 /* FIXME - add I2S volume */
150
151 int __init snd_ivtv_mixer_create(struct snd_ivtv_card *itvsc)
152 {
153         struct v4l2_device *v4l2_dev = itvsc->v4l2_dev;
154         struct snd_card *sc = itvsc->sc;
155         int ret;
156
157         strlcpy(sc->mixername, "CX2341[56] Mixer", sizeof(sc->mixername));
158
159         ret = snd_ctl_add(sc, snd_ctl_new1(&snd_ivtv_mixer_tv_vol, itvsc));
160         if (ret) {
161                 IVTV_ALSA_WARN("%s: failed to add %s control, err %d\n",
162                                __func__, snd_ivtv_mixer_tv_vol.name, ret);
163         }
164         return ret;
165 }