2 * ALSA mixer controls for the
3 * ALSA interface to ivtv PCM capture streams
5 * Copyright (C) 2009,2012 Andy Walls <awalls@md.metrocast.net>
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.
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.
18 #include "ivtv-alsa.h"
19 #include "ivtv-alsa-mixer.h"
20 #include "ivtv-driver.h"
22 #include <linux/videodev2.h>
24 #include <sound/core.h>
25 #include <sound/control.h>
26 #include <sound/tlv.h>
29 * Note the cx25840-core volume scale is funny, due to the alignment of the
30 * scale with another chip's range:
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
36 * 0x2c00 - 0x2dff 22 -97 -96 228
37 * 0x2e00 - 0x2fff 23 -96 -96 228
38 * 0x3000 - 0x31ff 24 -95 -95 226
40 * 0xee00 - 0xefff 119 0 0 36
42 * 0xfe00 - 0xffff 127 +8 +8 20
44 static inline int dB_to_cx25840_vol(int dB
)
50 return (dB
+ 119) << 9;
53 static inline int cx25840_vol_to_dB(int v
)
57 else if (v
> (127 << 9))
59 return (v
>> 9) - 119;
62 static int snd_ivtv_mixer_tv_vol_info(struct snd_kcontrol
*kcontrol
,
63 struct snd_ctl_elem_info
*uinfo
)
65 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
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;
74 static int snd_ivtv_mixer_tv_vol_get(struct snd_kcontrol
*kctl
,
75 struct snd_ctl_elem_value
*uctl
)
77 struct snd_ivtv_card
*itvsc
= snd_kcontrol_chip(kctl
);
78 struct ivtv
*itv
= to_ivtv(itvsc
->v4l2_dev
);
79 struct v4l2_control vctrl
;
82 vctrl
.id
= V4L2_CID_AUDIO_VOLUME
;
83 vctrl
.value
= dB_to_cx25840_vol(uctl
->value
.integer
.value
[0]);
86 ret
= v4l2_g_ctrl(itv
->sd_audio
->ctrl_handler
, &vctrl
);
87 snd_ivtv_unlock(itvsc
);
90 uctl
->value
.integer
.value
[0] = cx25840_vol_to_dB(vctrl
.value
);
94 static int snd_ivtv_mixer_tv_vol_put(struct snd_kcontrol
*kctl
,
95 struct snd_ctl_elem_value
*uctl
)
97 struct snd_ivtv_card
*itvsc
= snd_kcontrol_chip(kctl
);
98 struct ivtv
*itv
= to_ivtv(itvsc
->v4l2_dev
);
99 struct v4l2_control vctrl
;
102 vctrl
.id
= V4L2_CID_AUDIO_VOLUME
;
103 vctrl
.value
= dB_to_cx25840_vol(uctl
->value
.integer
.value
[0]);
105 snd_ivtv_lock(itvsc
);
107 /* Fetch current state */
108 ret
= v4l2_g_ctrl(itv
->sd_audio
->ctrl_handler
, &vctrl
);
111 (cx25840_vol_to_dB(vctrl
.value
) != uctl
->value
.integer
.value
[0])) {
114 vctrl
.value
= dB_to_cx25840_vol(uctl
->value
.integer
.value
[0]);
115 ret
= v4l2_s_ctrl(itv
->sd_audio
->ctrl_handler
, &vctrl
);
117 ret
= 1; /* Indicate control was changed w/o error */
119 snd_ivtv_unlock(itvsc
);
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);
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
139 /* FIXME - add mute switch and balance, bass, treble sliders:
142 V4L2_CID_AUDIO_BALANCE
145 V4L2_CID_AUDIO_TREBLE
148 /* FIXME - add stereo, lang1, lang2, mono menu */
149 /* FIXME - add I2S volume */
151 int __init
snd_ivtv_mixer_create(struct snd_ivtv_card
*itvsc
)
153 struct v4l2_device
*v4l2_dev
= itvsc
->v4l2_dev
;
154 struct snd_card
*sc
= itvsc
->sc
;
157 strlcpy(sc
->mixername
, "CX2341[56] Mixer", sizeof(sc
->mixername
));
159 ret
= snd_ctl_add(sc
, snd_ctl_new1(&snd_ivtv_mixer_tv_vol
, itvsc
));
161 IVTV_ALSA_WARN("%s: failed to add %s control, err %d\n",
162 __func__
, snd_ivtv_mixer_tv_vol
.name
, ret
);