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