Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/roland...
[linux-2.6] / 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 <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
104 void 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         schedule_timeout_interruptible(msecs_to_jiffies(1));
119         curout |= 2;
120         write_reg(curout, IVTV_REG_GPIO_OUT);
121         curdir &= ~0x80;
122         write_reg(curdir, IVTV_REG_GPIO_DIR);
123 }
124
125
126 void ivtv_gpio_init(struct ivtv *itv)
127 {
128         if (itv->card->gpio_init.direction == 0)
129                 return;
130
131         IVTV_DEBUG_INFO("GPIO initial dir: %08x out: %08x\n",
132                    read_reg(IVTV_REG_GPIO_DIR), read_reg(IVTV_REG_GPIO_OUT));
133
134         /* init output data then direction */
135         write_reg(itv->card->gpio_init.initial_value, IVTV_REG_GPIO_OUT);
136         write_reg(itv->card->gpio_init.direction, IVTV_REG_GPIO_DIR);
137 }
138
139 static struct v4l2_queryctrl gpio_ctrl_mute = {
140         .id            = V4L2_CID_AUDIO_MUTE,
141         .type          = V4L2_CTRL_TYPE_BOOLEAN,
142         .name          = "Mute",
143         .minimum       = 0,
144         .maximum       = 1,
145         .step          = 1,
146         .default_value = 1,
147         .flags         = 0,
148 };
149
150 int ivtv_gpio(struct ivtv *itv, unsigned int command, void *arg)
151 {
152         struct v4l2_tuner *tuner = arg;
153         struct v4l2_control *ctrl = arg;
154         struct v4l2_routing *route = arg;
155         u16 mask, data;
156
157         switch (command) {
158         case VIDIOC_INT_AUDIO_CLOCK_FREQ:
159                 mask = itv->card->gpio_audio_freq.mask;
160                 switch (*(u32 *)arg) {
161                 case 32000:
162                         data = itv->card->gpio_audio_freq.f32000;
163                         break;
164                 case 44100:
165                         data = itv->card->gpio_audio_freq.f44100;
166                         break;
167                 case 48000:
168                 default:
169                         data = itv->card->gpio_audio_freq.f48000;
170                         break;
171                 }
172                 break;
173
174         case VIDIOC_G_TUNER:
175                 mask = itv->card->gpio_audio_detect.mask;
176                 if (mask == 0 || (read_reg(IVTV_REG_GPIO_IN) & mask))
177                         tuner->rxsubchans = V4L2_TUNER_MODE_STEREO |
178                                V4L2_TUNER_MODE_LANG1 | V4L2_TUNER_MODE_LANG2;
179                 else
180                         tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
181                 return 0;
182
183         case VIDIOC_S_TUNER:
184                 mask = itv->card->gpio_audio_mode.mask;
185                 switch (tuner->audmode) {
186                 case V4L2_TUNER_MODE_LANG1:
187                         data = itv->card->gpio_audio_mode.lang1;
188                         break;
189                 case V4L2_TUNER_MODE_LANG2:
190                         data = itv->card->gpio_audio_mode.lang2;
191                         break;
192                 case V4L2_TUNER_MODE_MONO:
193                         data = itv->card->gpio_audio_mode.mono;
194                         break;
195                 case V4L2_TUNER_MODE_STEREO:
196                 case V4L2_TUNER_MODE_LANG1_LANG2:
197                 default:
198                         data = itv->card->gpio_audio_mode.stereo;
199                         break;
200                 }
201                 break;
202
203         case AUDC_SET_RADIO:
204                 mask = itv->card->gpio_audio_input.mask;
205                 data = itv->card->gpio_audio_input.radio;
206                 break;
207
208         case VIDIOC_S_STD:
209                 mask = itv->card->gpio_audio_input.mask;
210                 data = itv->card->gpio_audio_input.tuner;
211                 break;
212
213         case VIDIOC_INT_S_AUDIO_ROUTING:
214                 if (route->input > 2)
215                         return -EINVAL;
216                 mask = itv->card->gpio_audio_input.mask;
217                 switch (route->input) {
218                         case 0:
219                                 data = itv->card->gpio_audio_input.tuner;
220                                 break;
221                         case 1:
222                                 data = itv->card->gpio_audio_input.linein;
223                                 break;
224                         case 2:
225                         default:
226                                 data = itv->card->gpio_audio_input.radio;
227                                 break;
228                 }
229                 break;
230
231         case VIDIOC_G_CTRL:
232                 if (ctrl->id != V4L2_CID_AUDIO_MUTE)
233                         return -EINVAL;
234                 mask = itv->card->gpio_audio_mute.mask;
235                 data = itv->card->gpio_audio_mute.mute;
236                 ctrl->value = (read_reg(IVTV_REG_GPIO_OUT) & mask) == data;
237                 return 0;
238
239         case VIDIOC_S_CTRL:
240                 if (ctrl->id != V4L2_CID_AUDIO_MUTE)
241                         return -EINVAL;
242                 mask = itv->card->gpio_audio_mute.mask;
243                 data = ctrl->value ? itv->card->gpio_audio_mute.mute : 0;
244                 break;
245
246         case VIDIOC_QUERYCTRL:
247         {
248                 struct v4l2_queryctrl *qc = arg;
249
250                 if (qc->id != V4L2_CID_AUDIO_MUTE)
251                         return -EINVAL;
252                 *qc = gpio_ctrl_mute;
253                 return 0;
254         }
255
256         case VIDIOC_LOG_STATUS:
257                 IVTV_INFO("GPIO status: DIR=0x%04x OUT=0x%04x IN=0x%04x\n",
258                         read_reg(IVTV_REG_GPIO_DIR), read_reg(IVTV_REG_GPIO_OUT),
259                         read_reg(IVTV_REG_GPIO_IN));
260                 return 0;
261
262         case VIDIOC_INT_S_VIDEO_ROUTING:
263                 if (route->input > 2) /* 0:Tuner 1:Composite 2:S-Video */
264                         return -EINVAL;
265                 mask = itv->card->gpio_video_input.mask;
266                 if  (route->input == 0)
267                         data = itv->card->gpio_video_input.tuner;
268                 else if  (route->input == 1)
269                         data = itv->card->gpio_video_input.composite;
270                 else
271                         data = itv->card->gpio_video_input.svideo;
272                 break;
273
274         default:
275                 return -EINVAL;
276         }
277         if (mask)
278                 write_reg((read_reg(IVTV_REG_GPIO_OUT) & ~mask) | (data & mask), IVTV_REG_GPIO_OUT);
279         return 0;
280 }