Path: blob/master/drivers/media/video/cx18/cx18-alsa-mixer.c
17751 views
/*1* ALSA mixer controls for the2* ALSA interface to cx18 PCM capture streams3*4* Copyright (C) 2009 Andy Walls <[email protected]>5*6* This program is free software; you can redistribute it and/or modify7* it under the terms of the GNU General Public License as published by8* the Free Software Foundation; either version 2 of the License, or9* (at your option) any later version.10*11* This program is distributed in the hope that it will be useful,12* but WITHOUT ANY WARRANTY; without even the implied warranty of13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14* GNU General Public License for more details.15*16* You should have received a copy of the GNU General Public License17* along with this program; if not, write to the Free Software18* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA19* 02111-1307 USA20*/2122#include <linux/init.h>23#include <linux/kernel.h>24#include <linux/device.h>25#include <linux/spinlock.h>26#include <linux/videodev2.h>2728#include <media/v4l2-device.h>2930#include <sound/core.h>31#include <sound/control.h>32#include <sound/tlv.h>3334#include "cx18-alsa.h"35#include "cx18-driver.h"3637/*38* Note the cx18-av-core volume scale is funny, due to the alignment of the39* scale with another chip's range:40*41* v4l2_control value /512 indicated dB actual dB reg 0x8d442* 0x0000 - 0x01ff 0 -119 -96 22843* 0x0200 - 0x02ff 1 -118 -96 22844* ...45* 0x2c00 - 0x2dff 22 -97 -96 22846* 0x2e00 - 0x2fff 23 -96 -96 22847* 0x3000 - 0x31ff 24 -95 -95 22648* ...49* 0xee00 - 0xefff 119 0 0 3650* ...51* 0xfe00 - 0xffff 127 +8 +8 2052*/53static inline int dB_to_cx18_av_vol(int dB)54{55if (dB < -96)56dB = -96;57else if (dB > 8)58dB = 8;59return (dB + 119) << 9;60}6162static inline int cx18_av_vol_to_dB(int v)63{64if (v < (23 << 9))65v = (23 << 9);66else if (v > (127 << 9))67v = (127 << 9);68return (v >> 9) - 119;69}7071static int snd_cx18_mixer_tv_vol_info(struct snd_kcontrol *kcontrol,72struct snd_ctl_elem_info *uinfo)73{74uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;75uinfo->count = 1;76/* We're already translating values, just keep this control in dB */77uinfo->value.integer.min = -96;78uinfo->value.integer.max = 8;79uinfo->value.integer.step = 1;80return 0;81}8283static int snd_cx18_mixer_tv_vol_get(struct snd_kcontrol *kctl,84struct snd_ctl_elem_value *uctl)85{86struct snd_cx18_card *cxsc = snd_kcontrol_chip(kctl);87struct cx18 *cx = to_cx18(cxsc->v4l2_dev);88struct v4l2_control vctrl;89int ret;9091vctrl.id = V4L2_CID_AUDIO_VOLUME;92vctrl.value = dB_to_cx18_av_vol(uctl->value.integer.value[0]);9394snd_cx18_lock(cxsc);95ret = v4l2_subdev_call(cx->sd_av, core, g_ctrl, &vctrl);96snd_cx18_unlock(cxsc);9798if (!ret)99uctl->value.integer.value[0] = cx18_av_vol_to_dB(vctrl.value);100return ret;101}102103static int snd_cx18_mixer_tv_vol_put(struct snd_kcontrol *kctl,104struct snd_ctl_elem_value *uctl)105{106struct snd_cx18_card *cxsc = snd_kcontrol_chip(kctl);107struct cx18 *cx = to_cx18(cxsc->v4l2_dev);108struct v4l2_control vctrl;109int ret;110111vctrl.id = V4L2_CID_AUDIO_VOLUME;112vctrl.value = dB_to_cx18_av_vol(uctl->value.integer.value[0]);113114snd_cx18_lock(cxsc);115116/* Fetch current state */117ret = v4l2_subdev_call(cx->sd_av, core, g_ctrl, &vctrl);118119if (ret ||120(cx18_av_vol_to_dB(vctrl.value) != uctl->value.integer.value[0])) {121122/* Set, if needed */123vctrl.value = dB_to_cx18_av_vol(uctl->value.integer.value[0]);124ret = v4l2_subdev_call(cx->sd_av, core, s_ctrl, &vctrl);125if (!ret)126ret = 1; /* Indicate control was changed w/o error */127}128snd_cx18_unlock(cxsc);129130return ret;131}132133134/* This is a bit of overkill, the slider is already in dB internally */135static DECLARE_TLV_DB_SCALE(snd_cx18_mixer_tv_vol_db_scale, -9600, 100, 0);136137static struct snd_kcontrol_new snd_cx18_mixer_tv_vol __initdata = {138.iface = SNDRV_CTL_ELEM_IFACE_MIXER,139.name = "Analog TV Capture Volume",140.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |141SNDRV_CTL_ELEM_ACCESS_TLV_READ,142.info = snd_cx18_mixer_tv_volume_info,143.get = snd_cx18_mixer_tv_volume_get,144.put = snd_cx18_mixer_tv_volume_put,145.tlv.p = snd_cx18_mixer_tv_vol_db_scale146};147148/* FIXME - add mute switch and balance, bass, treble sliders:149V4L2_CID_AUDIO_MUTE150151V4L2_CID_AUDIO_BALANCE152153V4L2_CID_AUDIO_BASS154V4L2_CID_AUDIO_TREBLE155*/156157/* FIXME - add stereo, lang1, lang2, mono menu */158/* FIXME - add CS5345 I2S volume for HVR-1600 */159160int __init snd_cx18_mixer_create(struct snd_cx18_card *cxsc)161{162struct v4l2_device *v4l2_dev = cxsc->v4l2_dev;163struct snd_card *sc = cxsc->sc;164int ret;165166strlcpy(sc->mixername, "CX23418 Mixer", sizeof(sc->mixername));167168ret = snd_ctl_add(sc, snd_ctl_new1(snd_cx18_mixer_tv_vol, cxsc));169if (ret) {170CX18_ALSA_WARN("%s: failed to add %s control, err %d\n",171__func__, snd_cx18_mixer_tv_vol.name, ret);172}173return ret;174}175176177