Path: blob/master/drivers/media/video/ivtv/ivtv-gpio.c
17745 views
/*1gpio functions.2Merging GPIO support into driver:3Copyright (C) 2004 Chris Kennedy <[email protected]>4Copyright (C) 2005-2007 Hans Verkuil <[email protected]>56This program is free software; you can redistribute it and/or modify7it under the terms of the GNU General Public License as published by8the Free Software Foundation; either version 2 of the License, or9(at your option) any later version.1011This program is distributed in the hope that it will be useful,12but WITHOUT ANY WARRANTY; without even the implied warranty of13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14GNU General Public License for more details.1516You should have received a copy of the GNU General Public License17along with this program; if not, write to the Free Software18Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA19*/2021#include "ivtv-driver.h"22#include "ivtv-cards.h"23#include "ivtv-gpio.h"24#include "tuner-xc2028.h"25#include <media/tuner.h>26#include <media/v4l2-ctrls.h>2728/*29* GPIO assignment of Yuan MPG600/MPG16030*31* bit 15 14 13 12 | 11 10 9 8 | 7 6 5 4 | 3 2 1 032* OUTPUT IN1 IN0 AM3 AM2 AM1 AM033* INPUT DM1 DM034*35* IN* : Input selection36* IN1 IN037* 1 1 N/A38* 1 0 Line39* 0 1 N/A40* 0 0 Tuner41*42* AM* : Audio Mode43* AM3 0: Normal 1: Mixed(Sub+Main channel)44* AM2 0: Subchannel 1: Main channel45* AM1 0: Stereo 1: Mono46* AM0 0: Normal 1: Mute47*48* DM* : Detected tuner audio Mode49* DM1 0: Stereo 1: Mono50* DM0 0: Multiplex 1: Normal51*52* GPIO Initial Settings53* MPG600 MPG16054* DIR 0x3080 0x708055* OUTPUT 0x000C 0x400C56*57* Special thanks to Makoto Iguchi <[email protected]> and Mr. Anonymous58* for analyzing GPIO of MPG160.59*60*****************************************************************************61*62* GPIO assignment of Avermedia M179 (per information direct from AVerMedia)63*64* bit 15 14 13 12 | 11 10 9 8 | 7 6 5 4 | 3 2 1 065* OUTPUT IN0 AM0 IN1 AM1 AM2 IN2 BR0 BR166* INPUT67*68* IN* : Input selection69* IN0 IN1 IN270* * 1 * Mute71* 0 0 0 Line-In72* 1 0 0 TV Tuner Audio73* 0 0 1 FM Audio74* 1 0 1 Mute75*76* AM* : Audio Mode77* AM0 AM1 AM278* 0 0 0 TV Tuner Audio: L_OUT=(L+R)/2, R_OUT=SAP79* 0 0 1 TV Tuner Audio: L_OUT=R_OUT=SAP (SAP)80* 0 1 0 TV Tuner Audio: L_OUT=L, R_OUT=R (stereo)81* 0 1 1 TV Tuner Audio: mute82* 1 * * TV Tuner Audio: L_OUT=R_OUT=(L+R)/2 (mono)83*84* BR* : Audio Sample Rate (BR stands for bitrate for some reason)85* BR0 BR186* 0 0 32 kHz87* 0 1 44.1 kHz88* 1 0 48 kHz89*90* DM* : Detected tuner audio Mode91* Unknown currently92*93* Special thanks to AVerMedia Technologies, Inc. and Jiun-Kuei Jung at94* AVerMedia for providing the GPIO information used to add support95* for the M179 cards.96*/9798/********************* GPIO stuffs *********************/99100/* GPIO registers */101#define IVTV_REG_GPIO_IN 0x9008102#define IVTV_REG_GPIO_OUT 0x900c103#define IVTV_REG_GPIO_DIR 0x9020104105void ivtv_reset_ir_gpio(struct ivtv *itv)106{107int curdir, curout;108109if (itv->card->type != IVTV_CARD_PVR_150)110return;111IVTV_DEBUG_INFO("Resetting PVR150 IR\n");112curout = read_reg(IVTV_REG_GPIO_OUT);113curdir = read_reg(IVTV_REG_GPIO_DIR);114curdir |= 0x80;115write_reg(curdir, IVTV_REG_GPIO_DIR);116curout = (curout & ~0xF) | 1;117write_reg(curout, IVTV_REG_GPIO_OUT);118/* We could use something else for smaller time */119schedule_timeout_interruptible(msecs_to_jiffies(1));120curout |= 2;121write_reg(curout, IVTV_REG_GPIO_OUT);122curdir &= ~0x80;123write_reg(curdir, IVTV_REG_GPIO_DIR);124}125126/* Xceive tuner reset function */127int ivtv_reset_tuner_gpio(void *dev, int component, int cmd, int value)128{129struct i2c_algo_bit_data *algo = dev;130struct ivtv *itv = algo->data;131u32 curout;132133if (cmd != XC2028_TUNER_RESET)134return 0;135IVTV_DEBUG_INFO("Resetting tuner\n");136curout = read_reg(IVTV_REG_GPIO_OUT);137curout &= ~(1 << itv->card->xceive_pin);138write_reg(curout, IVTV_REG_GPIO_OUT);139schedule_timeout_interruptible(msecs_to_jiffies(1));140141curout |= 1 << itv->card->xceive_pin;142write_reg(curout, IVTV_REG_GPIO_OUT);143schedule_timeout_interruptible(msecs_to_jiffies(1));144return 0;145}146147static inline struct ivtv *sd_to_ivtv(struct v4l2_subdev *sd)148{149return container_of(sd, struct ivtv, sd_gpio);150}151152static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)153{154return &container_of(ctrl->handler, struct ivtv, hdl_gpio)->sd_gpio;155}156157static int subdev_s_clock_freq(struct v4l2_subdev *sd, u32 freq)158{159struct ivtv *itv = sd_to_ivtv(sd);160u16 mask, data;161162mask = itv->card->gpio_audio_freq.mask;163switch (freq) {164case 32000:165data = itv->card->gpio_audio_freq.f32000;166break;167case 44100:168data = itv->card->gpio_audio_freq.f44100;169break;170case 48000:171default:172data = itv->card->gpio_audio_freq.f48000;173break;174}175if (mask)176write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);177return 0;178}179180static int subdev_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)181{182struct ivtv *itv = sd_to_ivtv(sd);183u16 mask;184185mask = itv->card->gpio_audio_detect.mask;186if (mask == 0 || (read_reg(IVTV_REG_GPIO_IN) & mask))187vt->rxsubchans = V4L2_TUNER_SUB_STEREO |188V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;189else190vt->rxsubchans = V4L2_TUNER_SUB_MONO;191return 0;192}193194static int subdev_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)195{196struct ivtv *itv = sd_to_ivtv(sd);197u16 mask, data;198199mask = itv->card->gpio_audio_mode.mask;200switch (vt->audmode) {201case V4L2_TUNER_MODE_LANG1:202data = itv->card->gpio_audio_mode.lang1;203break;204case V4L2_TUNER_MODE_LANG2:205data = itv->card->gpio_audio_mode.lang2;206break;207case V4L2_TUNER_MODE_MONO:208data = itv->card->gpio_audio_mode.mono;209break;210case V4L2_TUNER_MODE_STEREO:211case V4L2_TUNER_MODE_LANG1_LANG2:212default:213data = itv->card->gpio_audio_mode.stereo;214break;215}216if (mask)217write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);218return 0;219}220221static int subdev_s_radio(struct v4l2_subdev *sd)222{223struct ivtv *itv = sd_to_ivtv(sd);224u16 mask, data;225226mask = itv->card->gpio_audio_input.mask;227data = itv->card->gpio_audio_input.radio;228if (mask)229write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);230return 0;231}232233static int subdev_s_audio_routing(struct v4l2_subdev *sd,234u32 input, u32 output, u32 config)235{236struct ivtv *itv = sd_to_ivtv(sd);237u16 mask, data;238239if (input > 2)240return -EINVAL;241mask = itv->card->gpio_audio_input.mask;242switch (input) {243case 0:244data = itv->card->gpio_audio_input.tuner;245break;246case 1:247data = itv->card->gpio_audio_input.linein;248break;249case 2:250default:251data = itv->card->gpio_audio_input.radio;252break;253}254if (mask)255write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);256return 0;257}258259static int subdev_s_ctrl(struct v4l2_ctrl *ctrl)260{261struct v4l2_subdev *sd = to_sd(ctrl);262struct ivtv *itv = sd_to_ivtv(sd);263u16 mask, data;264265switch (ctrl->id) {266case V4L2_CID_AUDIO_MUTE:267mask = itv->card->gpio_audio_mute.mask;268data = ctrl->val ? itv->card->gpio_audio_mute.mute : 0;269if (mask)270write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) |271(data & mask), IVTV_REG_GPIO_OUT);272return 0;273}274return -EINVAL;275}276277278static int subdev_log_status(struct v4l2_subdev *sd)279{280struct ivtv *itv = sd_to_ivtv(sd);281282IVTV_INFO("GPIO status: DIR=0x%04x OUT=0x%04x IN=0x%04x\n",283read_reg(IVTV_REG_GPIO_DIR), read_reg(IVTV_REG_GPIO_OUT),284read_reg(IVTV_REG_GPIO_IN));285v4l2_ctrl_handler_log_status(&itv->hdl_gpio, sd->name);286return 0;287}288289static int subdev_s_video_routing(struct v4l2_subdev *sd,290u32 input, u32 output, u32 config)291{292struct ivtv *itv = sd_to_ivtv(sd);293u16 mask, data;294295if (input > 2) /* 0:Tuner 1:Composite 2:S-Video */296return -EINVAL;297mask = itv->card->gpio_video_input.mask;298if (input == 0)299data = itv->card->gpio_video_input.tuner;300else if (input == 1)301data = itv->card->gpio_video_input.composite;302else303data = itv->card->gpio_video_input.svideo;304if (mask)305write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);306return 0;307}308309static const struct v4l2_ctrl_ops gpio_ctrl_ops = {310.s_ctrl = subdev_s_ctrl,311};312313static const struct v4l2_subdev_core_ops subdev_core_ops = {314.log_status = subdev_log_status,315.g_ext_ctrls = v4l2_subdev_g_ext_ctrls,316.try_ext_ctrls = v4l2_subdev_try_ext_ctrls,317.s_ext_ctrls = v4l2_subdev_s_ext_ctrls,318.g_ctrl = v4l2_subdev_g_ctrl,319.s_ctrl = v4l2_subdev_s_ctrl,320.queryctrl = v4l2_subdev_queryctrl,321.querymenu = v4l2_subdev_querymenu,322};323324static const struct v4l2_subdev_tuner_ops subdev_tuner_ops = {325.s_radio = subdev_s_radio,326.g_tuner = subdev_g_tuner,327.s_tuner = subdev_s_tuner,328};329330static const struct v4l2_subdev_audio_ops subdev_audio_ops = {331.s_clock_freq = subdev_s_clock_freq,332.s_routing = subdev_s_audio_routing,333};334335static const struct v4l2_subdev_video_ops subdev_video_ops = {336.s_routing = subdev_s_video_routing,337};338339static const struct v4l2_subdev_ops subdev_ops = {340.core = &subdev_core_ops,341.tuner = &subdev_tuner_ops,342.audio = &subdev_audio_ops,343.video = &subdev_video_ops,344};345346int ivtv_gpio_init(struct ivtv *itv)347{348u16 pin = 0;349350if (itv->card->xceive_pin)351pin = 1 << itv->card->xceive_pin;352353if ((itv->card->gpio_init.direction | pin) == 0)354return 0;355356IVTV_DEBUG_INFO("GPIO initial dir: %08x out: %08x\n",357read_reg(IVTV_REG_GPIO_DIR), read_reg(IVTV_REG_GPIO_OUT));358359/* init output data then direction */360write_reg(itv->card->gpio_init.initial_value | pin, IVTV_REG_GPIO_OUT);361write_reg(itv->card->gpio_init.direction | pin, IVTV_REG_GPIO_DIR);362v4l2_subdev_init(&itv->sd_gpio, &subdev_ops);363snprintf(itv->sd_gpio.name, sizeof(itv->sd_gpio.name), "%s-gpio", itv->v4l2_dev.name);364itv->sd_gpio.grp_id = IVTV_HW_GPIO;365v4l2_ctrl_handler_init(&itv->hdl_gpio, 1);366v4l2_ctrl_new_std(&itv->hdl_gpio, &gpio_ctrl_ops,367V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);368if (itv->hdl_gpio.error)369return itv->hdl_gpio.error;370itv->sd_gpio.ctrl_handler = &itv->hdl_gpio;371v4l2_ctrl_handler_setup(&itv->hdl_gpio);372return v4l2_device_register_subdev(&itv->v4l2_dev, &itv->sd_gpio);373}374375376