3 Merging GPIO support into driver:
4 Copyright (C) 2004 Chris Kennedy <c@groovy.org>
5 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "ivtv-driver.h"
23 #include "ivtv-cards.h"
24 #include "ivtv-gpio.h"
25 #include "tuner-xc2028.h"
26 #include <media/tuner.h>
27 #include <media/v4l2-ctrls.h>
30 * GPIO assignment of Yuan MPG600/MPG160
32 * bit 15 14 13 12 | 11 10 9 8 | 7 6 5 4 | 3 2 1 0
33 * OUTPUT IN1 IN0 AM3 AM2 AM1 AM0
36 * IN* : Input selection
44 * AM3 0: Normal 1: Mixed(Sub+Main channel)
45 * AM2 0: Subchannel 1: Main channel
46 * AM1 0: Stereo 1: Mono
47 * AM0 0: Normal 1: Mute
49 * DM* : Detected tuner audio Mode
50 * DM1 0: Stereo 1: Mono
51 * DM0 0: Multiplex 1: Normal
53 * GPIO Initial Settings
56 * OUTPUT 0x000C 0x400C
58 * Special thanks to Makoto Iguchi <iguchi@tahoo.org> and Mr. Anonymous
59 * for analyzing GPIO of MPG160.
61 *****************************************************************************
63 * GPIO assignment of Avermedia M179 (per information direct from AVerMedia)
65 * bit 15 14 13 12 | 11 10 9 8 | 7 6 5 4 | 3 2 1 0
66 * OUTPUT IN0 AM0 IN1 AM1 AM2 IN2 BR0 BR1
69 * IN* : Input selection
73 * 1 0 0 TV Tuner Audio
79 * 0 0 0 TV Tuner Audio: L_OUT=(L+R)/2, R_OUT=SAP
80 * 0 0 1 TV Tuner Audio: L_OUT=R_OUT=SAP (SAP)
81 * 0 1 0 TV Tuner Audio: L_OUT=L, R_OUT=R (stereo)
82 * 0 1 1 TV Tuner Audio: mute
83 * 1 * * TV Tuner Audio: L_OUT=R_OUT=(L+R)/2 (mono)
85 * BR* : Audio Sample Rate (BR stands for bitrate for some reason)
91 * DM* : Detected tuner audio Mode
94 * Special thanks to AVerMedia Technologies, Inc. and Jiun-Kuei Jung at
95 * AVerMedia for providing the GPIO information used to add support
99 /********************* GPIO stuffs *********************/
102 #define IVTV_REG_GPIO_IN 0x9008
103 #define IVTV_REG_GPIO_OUT 0x900c
104 #define IVTV_REG_GPIO_DIR 0x9020
106 void ivtv_reset_ir_gpio(struct ivtv
*itv
)
110 if (itv
->card
->type
!= IVTV_CARD_PVR_150
)
112 IVTV_DEBUG_INFO("Resetting PVR150 IR\n");
113 curout
= read_reg(IVTV_REG_GPIO_OUT
);
114 curdir
= read_reg(IVTV_REG_GPIO_DIR
);
116 write_reg(curdir
, IVTV_REG_GPIO_DIR
);
117 curout
= (curout
& ~0xF) | 1;
118 write_reg(curout
, IVTV_REG_GPIO_OUT
);
119 /* We could use something else for smaller time */
120 schedule_timeout_interruptible(msecs_to_jiffies(1));
122 write_reg(curout
, IVTV_REG_GPIO_OUT
);
124 write_reg(curdir
, IVTV_REG_GPIO_DIR
);
127 /* Xceive tuner reset function */
128 int ivtv_reset_tuner_gpio(void *dev
, int component
, int cmd
, int value
)
130 struct i2c_algo_bit_data
*algo
= dev
;
131 struct ivtv
*itv
= algo
->data
;
134 if (cmd
!= XC2028_TUNER_RESET
)
136 IVTV_DEBUG_INFO("Resetting tuner\n");
137 curout
= read_reg(IVTV_REG_GPIO_OUT
);
138 curout
&= ~(1 << itv
->card
->xceive_pin
);
139 write_reg(curout
, IVTV_REG_GPIO_OUT
);
140 schedule_timeout_interruptible(msecs_to_jiffies(1));
142 curout
|= 1 << itv
->card
->xceive_pin
;
143 write_reg(curout
, IVTV_REG_GPIO_OUT
);
144 schedule_timeout_interruptible(msecs_to_jiffies(1));
148 static inline struct ivtv
*sd_to_ivtv(struct v4l2_subdev
*sd
)
150 return container_of(sd
, struct ivtv
, sd_gpio
);
153 static inline struct v4l2_subdev
*to_sd(struct v4l2_ctrl
*ctrl
)
155 return &container_of(ctrl
->handler
, struct ivtv
, hdl_gpio
)->sd_gpio
;
158 static int subdev_s_clock_freq(struct v4l2_subdev
*sd
, u32 freq
)
160 struct ivtv
*itv
= sd_to_ivtv(sd
);
163 mask
= itv
->card
->gpio_audio_freq
.mask
;
166 data
= itv
->card
->gpio_audio_freq
.f32000
;
169 data
= itv
->card
->gpio_audio_freq
.f44100
;
173 data
= itv
->card
->gpio_audio_freq
.f48000
;
177 write_reg((read_reg(IVTV_REG_GPIO_OUT
) & ~mask
) | (data
& mask
), IVTV_REG_GPIO_OUT
);
181 static int subdev_g_tuner(struct v4l2_subdev
*sd
, struct v4l2_tuner
*vt
)
183 struct ivtv
*itv
= sd_to_ivtv(sd
);
186 mask
= itv
->card
->gpio_audio_detect
.mask
;
187 if (mask
== 0 || (read_reg(IVTV_REG_GPIO_IN
) & mask
))
188 vt
->rxsubchans
= V4L2_TUNER_SUB_STEREO
|
189 V4L2_TUNER_SUB_LANG1
| V4L2_TUNER_SUB_LANG2
;
191 vt
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
195 static int subdev_s_tuner(struct v4l2_subdev
*sd
, const struct v4l2_tuner
*vt
)
197 struct ivtv
*itv
= sd_to_ivtv(sd
);
200 mask
= itv
->card
->gpio_audio_mode
.mask
;
201 switch (vt
->audmode
) {
202 case V4L2_TUNER_MODE_LANG1
:
203 data
= itv
->card
->gpio_audio_mode
.lang1
;
205 case V4L2_TUNER_MODE_LANG2
:
206 data
= itv
->card
->gpio_audio_mode
.lang2
;
208 case V4L2_TUNER_MODE_MONO
:
209 data
= itv
->card
->gpio_audio_mode
.mono
;
211 case V4L2_TUNER_MODE_STEREO
:
212 case V4L2_TUNER_MODE_LANG1_LANG2
:
214 data
= itv
->card
->gpio_audio_mode
.stereo
;
218 write_reg((read_reg(IVTV_REG_GPIO_OUT
) & ~mask
) | (data
& mask
), IVTV_REG_GPIO_OUT
);
222 static int subdev_s_radio(struct v4l2_subdev
*sd
)
224 struct ivtv
*itv
= sd_to_ivtv(sd
);
227 mask
= itv
->card
->gpio_audio_input
.mask
;
228 data
= itv
->card
->gpio_audio_input
.radio
;
230 write_reg((read_reg(IVTV_REG_GPIO_OUT
) & ~mask
) | (data
& mask
), IVTV_REG_GPIO_OUT
);
234 static int subdev_s_audio_routing(struct v4l2_subdev
*sd
,
235 u32 input
, u32 output
, u32 config
)
237 struct ivtv
*itv
= sd_to_ivtv(sd
);
242 mask
= itv
->card
->gpio_audio_input
.mask
;
245 data
= itv
->card
->gpio_audio_input
.tuner
;
248 data
= itv
->card
->gpio_audio_input
.linein
;
252 data
= itv
->card
->gpio_audio_input
.radio
;
256 write_reg((read_reg(IVTV_REG_GPIO_OUT
) & ~mask
) | (data
& mask
), IVTV_REG_GPIO_OUT
);
260 static int subdev_s_ctrl(struct v4l2_ctrl
*ctrl
)
262 struct v4l2_subdev
*sd
= to_sd(ctrl
);
263 struct ivtv
*itv
= sd_to_ivtv(sd
);
267 case V4L2_CID_AUDIO_MUTE
:
268 mask
= itv
->card
->gpio_audio_mute
.mask
;
269 data
= ctrl
->val
? itv
->card
->gpio_audio_mute
.mute
: 0;
271 write_reg((read_reg(IVTV_REG_GPIO_OUT
) & ~mask
) |
272 (data
& mask
), IVTV_REG_GPIO_OUT
);
279 static int subdev_log_status(struct v4l2_subdev
*sd
)
281 struct ivtv
*itv
= sd_to_ivtv(sd
);
283 IVTV_INFO("GPIO status: DIR=0x%04x OUT=0x%04x IN=0x%04x\n",
284 read_reg(IVTV_REG_GPIO_DIR
), read_reg(IVTV_REG_GPIO_OUT
),
285 read_reg(IVTV_REG_GPIO_IN
));
286 v4l2_ctrl_handler_log_status(&itv
->hdl_gpio
, sd
->name
);
290 static int subdev_s_video_routing(struct v4l2_subdev
*sd
,
291 u32 input
, u32 output
, u32 config
)
293 struct ivtv
*itv
= sd_to_ivtv(sd
);
296 if (input
> 2) /* 0:Tuner 1:Composite 2:S-Video */
298 mask
= itv
->card
->gpio_video_input
.mask
;
300 data
= itv
->card
->gpio_video_input
.tuner
;
302 data
= itv
->card
->gpio_video_input
.composite
;
304 data
= itv
->card
->gpio_video_input
.svideo
;
306 write_reg((read_reg(IVTV_REG_GPIO_OUT
) & ~mask
) | (data
& mask
), IVTV_REG_GPIO_OUT
);
310 static const struct v4l2_ctrl_ops gpio_ctrl_ops
= {
311 .s_ctrl
= subdev_s_ctrl
,
314 static const struct v4l2_subdev_core_ops subdev_core_ops
= {
315 .log_status
= subdev_log_status
,
318 static const struct v4l2_subdev_tuner_ops subdev_tuner_ops
= {
319 .s_radio
= subdev_s_radio
,
320 .g_tuner
= subdev_g_tuner
,
321 .s_tuner
= subdev_s_tuner
,
324 static const struct v4l2_subdev_audio_ops subdev_audio_ops
= {
325 .s_clock_freq
= subdev_s_clock_freq
,
326 .s_routing
= subdev_s_audio_routing
,
329 static const struct v4l2_subdev_video_ops subdev_video_ops
= {
330 .s_routing
= subdev_s_video_routing
,
333 static const struct v4l2_subdev_ops subdev_ops
= {
334 .core
= &subdev_core_ops
,
335 .tuner
= &subdev_tuner_ops
,
336 .audio
= &subdev_audio_ops
,
337 .video
= &subdev_video_ops
,
340 int ivtv_gpio_init(struct ivtv
*itv
)
344 if (itv
->card
->xceive_pin
)
345 pin
= 1 << itv
->card
->xceive_pin
;
347 if ((itv
->card
->gpio_init
.direction
| pin
) == 0)
350 IVTV_DEBUG_INFO("GPIO initial dir: %08x out: %08x\n",
351 read_reg(IVTV_REG_GPIO_DIR
), read_reg(IVTV_REG_GPIO_OUT
));
353 /* init output data then direction */
354 write_reg(itv
->card
->gpio_init
.initial_value
| pin
, IVTV_REG_GPIO_OUT
);
355 write_reg(itv
->card
->gpio_init
.direction
| pin
, IVTV_REG_GPIO_DIR
);
356 v4l2_subdev_init(&itv
->sd_gpio
, &subdev_ops
);
357 snprintf(itv
->sd_gpio
.name
, sizeof(itv
->sd_gpio
.name
), "%s-gpio", itv
->v4l2_dev
.name
);
358 itv
->sd_gpio
.grp_id
= IVTV_HW_GPIO
;
359 v4l2_ctrl_handler_init(&itv
->hdl_gpio
, 1);
360 v4l2_ctrl_new_std(&itv
->hdl_gpio
, &gpio_ctrl_ops
,
361 V4L2_CID_AUDIO_MUTE
, 0, 1, 1, 0);
362 if (itv
->hdl_gpio
.error
)
363 return itv
->hdl_gpio
.error
;
364 itv
->sd_gpio
.ctrl_handler
= &itv
->hdl_gpio
;
365 v4l2_ctrl_handler_setup(&itv
->hdl_gpio
);
366 return v4l2_device_register_subdev(&itv
->v4l2_dev
, &itv
->sd_gpio
);