0001
0002
0003
0004
0005
0006
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
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
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
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
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)
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
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 }