Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003     gpio functions.
0004     Merging GPIO support into driver:
0005     Copyright (C) 2004  Chris Kennedy <c@groovy.org>
0006     Copyright (C) 2005-2007  Hans Verkuil <hverkuil@xs4all.nl>
0007 
0008  */
0009 
0010 #include "ivtv-driver.h"
0011 #include "ivtv-cards.h"
0012 #include "ivtv-gpio.h"
0013 #include "xc2028.h"
0014 #include <media/tuner.h>
0015 #include <media/v4l2-ctrls.h>
0016 
0017 /*
0018  * GPIO assignment of Yuan MPG600/MPG160
0019  *
0020  *    bit 15  14  13  12 |  11  10   9   8 |   7   6   5   4 |   3   2   1   0
0021  * OUTPUT         IN1 IN0                                       AM3 AM2 AM1 AM0
0022  *  INPUT                   DM1         DM0
0023  *
0024  *   IN* : Input selection
0025  *          IN1 IN0
0026  *           1   1  N/A
0027  *           1   0  Line
0028  *           0   1  N/A
0029  *           0   0  Tuner
0030  *
0031  *   AM* : Audio Mode
0032  *          AM3  0: Normal        1: Mixed(Sub+Main channel)
0033  *          AM2  0: Subchannel    1: Main channel
0034  *          AM1  0: Stereo        1: Mono
0035  *          AM0  0: Normal        1: Mute
0036  *
0037  *   DM* : Detected tuner audio Mode
0038  *          DM1  0: Stereo        1: Mono
0039  *          DM0  0: Multiplex     1: Normal
0040  *
0041  * GPIO Initial Settings
0042  *           MPG600   MPG160
0043  *     DIR   0x3080   0x7080
0044  *  OUTPUT   0x000C   0x400C
0045  *
0046  *  Special thanks to Makoto Iguchi <iguchi@tahoo.org> and Mr. Anonymous
0047  *  for analyzing GPIO of MPG160.
0048  *
0049  *****************************************************************************
0050  *
0051  * GPIO assignment of Avermedia M179 (per information direct from AVerMedia)
0052  *
0053  *    bit 15  14  13  12 |  11  10   9   8 |   7   6   5   4 |   3   2   1   0
0054  * OUTPUT IN0 AM0 IN1               AM1 AM2       IN2     BR0   BR1
0055  *  INPUT
0056  *
0057  *   IN* : Input selection
0058  *          IN0 IN1 IN2
0059  *           *   1   *  Mute
0060  *           0   0   0  Line-In
0061  *           1   0   0  TV Tuner Audio
0062  *           0   0   1  FM Audio
0063  *           1   0   1  Mute
0064  *
0065  *   AM* : Audio Mode
0066  *          AM0 AM1 AM2
0067  *           0   0   0  TV Tuner Audio: L_OUT=(L+R)/2, R_OUT=SAP
0068  *           0   0   1  TV Tuner Audio: L_OUT=R_OUT=SAP   (SAP)
0069  *           0   1   0  TV Tuner Audio: L_OUT=L, R_OUT=R   (stereo)
0070  *           0   1   1  TV Tuner Audio: mute
0071  *           1   *   *  TV Tuner Audio: L_OUT=R_OUT=(L+R)/2   (mono)
0072  *
0073  *   BR* : Audio Sample Rate (BR stands for bitrate for some reason)
0074  *          BR0 BR1
0075  *           0   0   32 kHz
0076  *           0   1   44.1 kHz
0077  *           1   0   48 kHz
0078  *
0079  *   DM* : Detected tuner audio Mode
0080  *         Unknown currently
0081  *
0082  * Special thanks to AVerMedia Technologies, Inc. and Jiun-Kuei Jung at
0083  * AVerMedia for providing the GPIO information used to add support
0084  * for the M179 cards.
0085  */
0086 
0087 /********************* GPIO stuffs *********************/
0088 
0089 /* GPIO registers */
0090 #define IVTV_REG_GPIO_IN    0x9008
0091 #define IVTV_REG_GPIO_OUT   0x900c
0092 #define IVTV_REG_GPIO_DIR   0x9020
0093 
0094 void ivtv_reset_ir_gpio(struct ivtv *itv)
0095 {
0096     int curdir, curout;
0097 
0098     if (itv->card->type != IVTV_CARD_PVR_150)
0099         return;
0100     IVTV_DEBUG_INFO("Resetting PVR150 IR\n");
0101     curout = read_reg(IVTV_REG_GPIO_OUT);
0102     curdir = read_reg(IVTV_REG_GPIO_DIR);
0103     curdir |= 0x80;
0104     write_reg(curdir, IVTV_REG_GPIO_DIR);
0105     curout = (curout & ~0xF) | 1;
0106     write_reg(curout, IVTV_REG_GPIO_OUT);
0107     /* We could use something else for smaller time */
0108     schedule_timeout_interruptible(msecs_to_jiffies(1));
0109     curout |= 2;
0110     write_reg(curout, IVTV_REG_GPIO_OUT);
0111     curdir &= ~0x80;
0112     write_reg(curdir, IVTV_REG_GPIO_DIR);
0113 }
0114 
0115 /* Xceive tuner reset function */
0116 int ivtv_reset_tuner_gpio(void *dev, int component, int cmd, int value)
0117 {
0118     struct i2c_algo_bit_data *algo = dev;
0119     struct ivtv *itv = algo->data;
0120     u32 curout;
0121 
0122     if (cmd != XC2028_TUNER_RESET)
0123         return 0;
0124     IVTV_DEBUG_INFO("Resetting tuner\n");
0125     curout = read_reg(IVTV_REG_GPIO_OUT);
0126     curout &= ~(1 << itv->card->xceive_pin);
0127     write_reg(curout, IVTV_REG_GPIO_OUT);
0128     schedule_timeout_interruptible(msecs_to_jiffies(1));
0129 
0130     curout |= 1 << itv->card->xceive_pin;
0131     write_reg(curout, IVTV_REG_GPIO_OUT);
0132     schedule_timeout_interruptible(msecs_to_jiffies(1));
0133     return 0;
0134 }
0135 
0136 static inline struct ivtv *sd_to_ivtv(struct v4l2_subdev *sd)
0137 {
0138     return container_of(sd, struct ivtv, sd_gpio);
0139 }
0140 
0141 static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
0142 {
0143     return &container_of(ctrl->handler, struct ivtv, hdl_gpio)->sd_gpio;
0144 }
0145 
0146 static int subdev_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
0147 {
0148     struct ivtv *itv = sd_to_ivtv(sd);
0149     u16 mask, data;
0150 
0151     mask = itv->card->gpio_audio_freq.mask;
0152     switch (freq) {
0153     case 32000:
0154         data = itv->card->gpio_audio_freq.f32000;
0155         break;
0156     case 44100:
0157         data = itv->card->gpio_audio_freq.f44100;
0158         break;
0159     case 48000:
0160     default:
0161         data = itv->card->gpio_audio_freq.f48000;
0162         break;
0163     }
0164     if (mask)
0165         write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
0166     return 0;
0167 }
0168 
0169 static int subdev_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
0170 {
0171     struct ivtv *itv = sd_to_ivtv(sd);
0172     u16 mask;
0173 
0174     mask = itv->card->gpio_audio_detect.mask;
0175     if (mask == 0 || (read_reg(IVTV_REG_GPIO_IN) & mask))
0176         vt->rxsubchans = V4L2_TUNER_SUB_STEREO |
0177             V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
0178     else
0179         vt->rxsubchans = V4L2_TUNER_SUB_MONO;
0180     return 0;
0181 }
0182 
0183 static int subdev_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
0184 {
0185     struct ivtv *itv = sd_to_ivtv(sd);
0186     u16 mask, data;
0187 
0188     mask = itv->card->gpio_audio_mode.mask;
0189     switch (vt->audmode) {
0190     case V4L2_TUNER_MODE_LANG1:
0191         data = itv->card->gpio_audio_mode.lang1;
0192         break;
0193     case V4L2_TUNER_MODE_LANG2:
0194         data = itv->card->gpio_audio_mode.lang2;
0195         break;
0196     case V4L2_TUNER_MODE_MONO:
0197         data = itv->card->gpio_audio_mode.mono;
0198         break;
0199     case V4L2_TUNER_MODE_STEREO:
0200     case V4L2_TUNER_MODE_LANG1_LANG2:
0201     default:
0202         data = itv->card->gpio_audio_mode.stereo;
0203         break;
0204     }
0205     if (mask)
0206         write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
0207     return 0;
0208 }
0209 
0210 static int subdev_s_radio(struct v4l2_subdev *sd)
0211 {
0212     struct ivtv *itv = sd_to_ivtv(sd);
0213     u16 mask, data;
0214 
0215     mask = itv->card->gpio_audio_input.mask;
0216     data = itv->card->gpio_audio_input.radio;
0217     if (mask)
0218         write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
0219     return 0;
0220 }
0221 
0222 static int subdev_s_audio_routing(struct v4l2_subdev *sd,
0223                   u32 input, u32 output, u32 config)
0224 {
0225     struct ivtv *itv = sd_to_ivtv(sd);
0226     u16 mask, data;
0227 
0228     if (input > 2)
0229         return -EINVAL;
0230     mask = itv->card->gpio_audio_input.mask;
0231     switch (input) {
0232     case 0:
0233         data = itv->card->gpio_audio_input.tuner;
0234         break;
0235     case 1:
0236         data = itv->card->gpio_audio_input.linein;
0237         break;
0238     case 2:
0239     default:
0240         data = itv->card->gpio_audio_input.radio;
0241         break;
0242     }
0243     if (mask)
0244         write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
0245     return 0;
0246 }
0247 
0248 static int subdev_s_ctrl(struct v4l2_ctrl *ctrl)
0249 {
0250     struct v4l2_subdev *sd = to_sd(ctrl);
0251     struct ivtv *itv = sd_to_ivtv(sd);
0252     u16 mask, data;
0253 
0254     switch (ctrl->id) {
0255     case V4L2_CID_AUDIO_MUTE:
0256         mask = itv->card->gpio_audio_mute.mask;
0257         data = ctrl->val ? itv->card->gpio_audio_mute.mute : 0;
0258         if (mask)
0259             write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) |
0260                     (data & mask), IVTV_REG_GPIO_OUT);
0261         return 0;
0262     }
0263     return -EINVAL;
0264 }
0265 
0266 
0267 static int subdev_log_status(struct v4l2_subdev *sd)
0268 {
0269     struct ivtv *itv = sd_to_ivtv(sd);
0270 
0271     IVTV_INFO("GPIO status: DIR=0x%04x OUT=0x%04x IN=0x%04x\n",
0272             read_reg(IVTV_REG_GPIO_DIR), read_reg(IVTV_REG_GPIO_OUT),
0273             read_reg(IVTV_REG_GPIO_IN));
0274     v4l2_ctrl_handler_log_status(&itv->hdl_gpio, sd->name);
0275     return 0;
0276 }
0277 
0278 static int subdev_s_video_routing(struct v4l2_subdev *sd,
0279                   u32 input, u32 output, u32 config)
0280 {
0281     struct ivtv *itv = sd_to_ivtv(sd);
0282     u16 mask, data;
0283 
0284     if (input > 2) /* 0:Tuner 1:Composite 2:S-Video */
0285         return -EINVAL;
0286     mask = itv->card->gpio_video_input.mask;
0287     if (input == 0)
0288         data = itv->card->gpio_video_input.tuner;
0289     else if (input == 1)
0290         data = itv->card->gpio_video_input.composite;
0291     else
0292         data = itv->card->gpio_video_input.svideo;
0293     if (mask)
0294         write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
0295     return 0;
0296 }
0297 
0298 static const struct v4l2_ctrl_ops gpio_ctrl_ops = {
0299     .s_ctrl = subdev_s_ctrl,
0300 };
0301 
0302 static const struct v4l2_subdev_core_ops subdev_core_ops = {
0303     .log_status = subdev_log_status,
0304 };
0305 
0306 static const struct v4l2_subdev_tuner_ops subdev_tuner_ops = {
0307     .s_radio = subdev_s_radio,
0308     .g_tuner = subdev_g_tuner,
0309     .s_tuner = subdev_s_tuner,
0310 };
0311 
0312 static const struct v4l2_subdev_audio_ops subdev_audio_ops = {
0313     .s_clock_freq = subdev_s_clock_freq,
0314     .s_routing = subdev_s_audio_routing,
0315 };
0316 
0317 static const struct v4l2_subdev_video_ops subdev_video_ops = {
0318     .s_routing = subdev_s_video_routing,
0319 };
0320 
0321 static const struct v4l2_subdev_ops subdev_ops = {
0322     .core = &subdev_core_ops,
0323     .tuner = &subdev_tuner_ops,
0324     .audio = &subdev_audio_ops,
0325     .video = &subdev_video_ops,
0326 };
0327 
0328 int ivtv_gpio_init(struct ivtv *itv)
0329 {
0330     u16 pin = 0;
0331 
0332     if (itv->card->xceive_pin)
0333         pin = 1 << itv->card->xceive_pin;
0334 
0335     if ((itv->card->gpio_init.direction | pin) == 0)
0336         return 0;
0337 
0338     IVTV_DEBUG_INFO("GPIO initial dir: %08x out: %08x\n",
0339            read_reg(IVTV_REG_GPIO_DIR), read_reg(IVTV_REG_GPIO_OUT));
0340 
0341     /* init output data then direction */
0342     write_reg(itv->card->gpio_init.initial_value | pin, IVTV_REG_GPIO_OUT);
0343     write_reg(itv->card->gpio_init.direction | pin, IVTV_REG_GPIO_DIR);
0344     v4l2_subdev_init(&itv->sd_gpio, &subdev_ops);
0345     snprintf(itv->sd_gpio.name, sizeof(itv->sd_gpio.name), "%s-gpio", itv->v4l2_dev.name);
0346     itv->sd_gpio.grp_id = IVTV_HW_GPIO;
0347     v4l2_ctrl_handler_init(&itv->hdl_gpio, 1);
0348     v4l2_ctrl_new_std(&itv->hdl_gpio, &gpio_ctrl_ops,
0349             V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
0350     if (itv->hdl_gpio.error)
0351         return itv->hdl_gpio.error;
0352     itv->sd_gpio.ctrl_handler = &itv->hdl_gpio;
0353     v4l2_ctrl_handler_setup(&itv->hdl_gpio);
0354     return v4l2_device_register_subdev(&itv->v4l2_dev, &itv->sd_gpio);
0355 }