V4L/DVB (5846): Clean up setting state and scheduling timeouts
[deliverable/linux.git] / drivers / media / video / ivtv / ivtv-gpio.c
CommitLineData
1a0adaf3
HV
1/*
2 gpio functions.
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>
6
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.
11
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.
16
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
20 */
21
22#include "ivtv-driver.h"
23#include "ivtv-cards.h"
24#include "ivtv-gpio.h"
25#include <media/tuner.h>
26
27/*
28 * GPIO assignment of Yuan MPG600/MPG160
29 *
30 * bit 15 14 13 12 | 11 10 9 8 | 7 6 5 4 | 3 2 1 0
31 * OUTPUT IN1 IN0 AM3 AM2 AM1 AM0
32 * INPUT DM1 DM0
33 *
34 * IN* : Input selection
35 * IN1 IN0
36 * 1 1 N/A
37 * 1 0 Line
38 * 0 1 N/A
39 * 0 0 Tuner
40 *
41 * AM* : Audio Mode
42 * AM3 0: Normal 1: Mixed(Sub+Main channel)
43 * AM2 0: Subchannel 1: Main channel
44 * AM1 0: Stereo 1: Mono
45 * AM0 0: Normal 1: Mute
46 *
47 * DM* : Detected tuner audio Mode
48 * DM1 0: Stereo 1: Mono
49 * DM0 0: Multiplex 1: Normal
50 *
51 * GPIO Initial Settings
52 * MPG600 MPG160
53 * DIR 0x3080 0x7080
54 * OUTPUT 0x000C 0x400C
55 *
56 * Special thanks to Makoto Iguchi <iguchi@tahoo.org> and Mr. Anonymous
57 * for analyzing GPIO of MPG160.
58 *
59 *****************************************************************************
60 *
61 * GPIO assignment of Avermedia M179 (per information direct from AVerMedia)
62 *
63 * bit 15 14 13 12 | 11 10 9 8 | 7 6 5 4 | 3 2 1 0
64 * OUTPUT IN0 AM0 IN1 AM1 AM2 IN2 BR0 BR1
65 * INPUT
66 *
67 * IN* : Input selection
68 * IN0 IN1 IN2
69 * * 1 * Mute
70 * 0 0 0 Line-In
71 * 1 0 0 TV Tuner Audio
72 * 0 0 1 FM Audio
73 * 1 0 1 Mute
74 *
75 * AM* : Audio Mode
76 * AM0 AM1 AM2
77 * 0 0 0 TV Tuner Audio: L_OUT=(L+R)/2, R_OUT=SAP
78 * 0 0 1 TV Tuner Audio: L_OUT=R_OUT=SAP (SAP)
79 * 0 1 0 TV Tuner Audio: L_OUT=L, R_OUT=R (stereo)
80 * 0 1 1 TV Tuner Audio: mute
81 * 1 * * TV Tuner Audio: L_OUT=R_OUT=(L+R)/2 (mono)
82 *
83 * BR* : Audio Sample Rate (BR stands for bitrate for some reason)
84 * BR0 BR1
85 * 0 0 32 kHz
86 * 0 1 44.1 kHz
87 * 1 0 48 kHz
88 *
89 * DM* : Detected tuner audio Mode
90 * Unknown currently
91 *
92 * Special thanks to AVerMedia Technologies, Inc. and Jiun-Kuei Jung at
93 * AVerMedia for providing the GPIO information used to add support
94 * for the M179 cards.
95 */
96
97/********************* GPIO stuffs *********************/
98
99/* GPIO registers */
100#define IVTV_REG_GPIO_IN 0x9008
101#define IVTV_REG_GPIO_OUT 0x900c
102#define IVTV_REG_GPIO_DIR 0x9020
103
104void ivtv_reset_ir_gpio(struct ivtv *itv)
105{
106 int curdir, curout;
107
108 if (itv->card->type != IVTV_CARD_PVR_150)
109 return;
110 IVTV_DEBUG_INFO("Resetting PVR150 IR\n");
111 curout = read_reg(IVTV_REG_GPIO_OUT);
112 curdir = read_reg(IVTV_REG_GPIO_DIR);
113 curdir |= 0x80;
114 write_reg(curdir, IVTV_REG_GPIO_DIR);
115 curout = (curout & ~0xF) | 1;
116 write_reg(curout, IVTV_REG_GPIO_OUT);
117 /* We could use something else for smaller time */
118 current->state = TASK_INTERRUPTIBLE;
119 schedule_timeout(1);
120 curout |= 2;
121 write_reg(curout, IVTV_REG_GPIO_OUT);
122 curdir &= ~0x80;
123 write_reg(curdir, IVTV_REG_GPIO_DIR);
124}
125
126#ifdef HAVE_XC3028
127int ivtv_reset_tuner_gpio(enum v4l2_tuner_type mode, void *priv, int ptr)
128{
129 int curdir, curout;
130 struct ivtv *itv = (struct ivtv *) priv;
131
132 if (itv->card->type != IVTV_CARD_PG600V2 || itv->options.tuner != TUNER_XCEIVE_XC3028)
133 return -EINVAL;
134 IVTV_INFO("Resetting tuner.\n");
135 curout = read_reg(IVTV_REG_GPIO_OUT);
136 curdir = read_reg(IVTV_REG_GPIO_DIR);
137 curdir |= (1 << 12); /* GPIO bit 12 */
138
139 curout &= ~(1 << 12);
140 write_reg(curout, IVTV_REG_GPIO_OUT);
141 current->state = TASK_INTERRUPTIBLE;
142 schedule_timeout(1);
143
144 curout |= (1 << 12);
145 write_reg(curout, IVTV_REG_GPIO_OUT);
146 current->state = TASK_INTERRUPTIBLE;
147 schedule_timeout(1);
148
149 return 0;
150}
151#endif
152
153void ivtv_gpio_init(struct ivtv *itv)
154{
155 if (itv->card->gpio_init.direction == 0)
156 return;
157
158 IVTV_DEBUG_INFO("GPIO initial dir: %08x out: %08x\n",
159 read_reg(IVTV_REG_GPIO_DIR), read_reg(IVTV_REG_GPIO_OUT));
160
161 /* init output data then direction */
162 write_reg(itv->card->gpio_init.initial_value, IVTV_REG_GPIO_OUT);
163 write_reg(itv->card->gpio_init.direction, IVTV_REG_GPIO_DIR);
164}
165
166static struct v4l2_queryctrl gpio_ctrl_mute = {
167 .id = V4L2_CID_AUDIO_MUTE,
168 .type = V4L2_CTRL_TYPE_BOOLEAN,
169 .name = "Mute",
170 .minimum = 0,
171 .maximum = 1,
172 .step = 1,
173 .default_value = 1,
174 .flags = 0,
175};
176
177int ivtv_gpio(struct ivtv *itv, unsigned int command, void *arg)
178{
179 struct v4l2_tuner *tuner = arg;
180 struct v4l2_control *ctrl = arg;
181 struct v4l2_routing *route = arg;
182 u16 mask, data;
183
184 switch (command) {
185 case VIDIOC_INT_AUDIO_CLOCK_FREQ:
186 mask = itv->card->gpio_audio_freq.mask;
187 switch (*(u32 *)arg) {
188 case 32000:
189 data = itv->card->gpio_audio_freq.f32000;
190 break;
191 case 44100:
192 data = itv->card->gpio_audio_freq.f44100;
193 break;
194 case 48000:
195 default:
196 data = itv->card->gpio_audio_freq.f48000;
197 break;
198 }
199 break;
200
201 case VIDIOC_G_TUNER:
202 mask = itv->card->gpio_audio_detect.mask;
203 if (mask == 0 || (read_reg(IVTV_REG_GPIO_IN) & mask))
204 tuner->rxsubchans = V4L2_TUNER_MODE_STEREO |
205 V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2;
206 else
207 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
208 return 0;
209
210 case VIDIOC_S_TUNER:
211 mask = itv->card->gpio_audio_mode.mask;
212 switch (tuner->audmode) {
213 case V4L2_TUNER_MODE_LANG1:
214 data = itv->card->gpio_audio_mode.lang1;
215 break;
216 case V4L2_TUNER_MODE_LANG2:
217 data = itv->card->gpio_audio_mode.lang2;
218 break;
219 case V4L2_TUNER_MODE_MONO:
220 data = itv->card->gpio_audio_mode.mono;
221 break;
222 case V4L2_TUNER_MODE_STEREO:
223 case V4L2_TUNER_MODE_LANG1_LANG2:
224 default:
225 data = itv->card->gpio_audio_mode.stereo;
226 break;
227 }
228 break;
229
230 case AUDC_SET_RADIO:
231 mask = itv->card->gpio_audio_input.mask;
232 data = itv->card->gpio_audio_input.radio;
233 break;
234
235 case VIDIOC_S_STD:
236 mask = itv->card->gpio_audio_input.mask;
237 data = itv->card->gpio_audio_input.tuner;
238 break;
239
240 case VIDIOC_INT_S_AUDIO_ROUTING:
241 if (route->input > 2)
242 return -EINVAL;
243 mask = itv->card->gpio_audio_input.mask;
244 switch (route->input) {
245 case 0:
246 data = itv->card->gpio_audio_input.tuner;
247 break;
248 case 1:
249 data = itv->card->gpio_audio_input.linein;
250 break;
251 case 2:
252 default:
253 data = itv->card->gpio_audio_input.radio;
254 break;
255 }
256 break;
257
258 case VIDIOC_G_CTRL:
259 if (ctrl->id != V4L2_CID_AUDIO_MUTE)
260 return -EINVAL;
261 mask = itv->card->gpio_audio_mute.mask;
262 data = itv->card->gpio_audio_mute.mute;
263 ctrl->value = (read_reg(IVTV_REG_GPIO_OUT) & mask) == data;
264 return 0;
265
266 case VIDIOC_S_CTRL:
267 if (ctrl->id != V4L2_CID_AUDIO_MUTE)
268 return -EINVAL;
269 mask = itv->card->gpio_audio_mute.mask;
270 data = ctrl->value ? itv->card->gpio_audio_mute.mute : 0;
271 break;
272
273 case VIDIOC_QUERYCTRL:
274 {
275 struct v4l2_queryctrl *qc = arg;
276
277 if (qc->id != V4L2_CID_AUDIO_MUTE)
278 return -EINVAL;
279 *qc = gpio_ctrl_mute;
280 return 0;
281 }
282
283 case VIDIOC_LOG_STATUS:
284 IVTV_INFO("GPIO status: DIR=0x%04x OUT=0x%04x IN=0x%04x\n",
285 read_reg(IVTV_REG_GPIO_DIR), read_reg(IVTV_REG_GPIO_OUT),
286 read_reg(IVTV_REG_GPIO_IN));
287 return 0;
288
289 case VIDIOC_INT_S_VIDEO_ROUTING:
290 if (route->input > 2) /* 0:Tuner 1:Composite 2:S-Video */
291 return -EINVAL;
292 mask = itv->card->gpio_video_input.mask;
293 if (route->input == 0)
294 data = itv->card->gpio_video_input.tuner;
295 else if (route->input == 1)
296 data = itv->card->gpio_video_input.composite;
297 else
298 data = itv->card->gpio_video_input.svideo;
299 break;
300
301 default:
302 return -EINVAL;
303 }
304 if (mask)
305 write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
306 return 0;
307}
This page took 0.0749 seconds and 5 git commands to generate.