V4L/DVB (4767): Make Remote control of the Pinnacle PCTV 310i work
[linux-2.6/kvm.git] / drivers / media / video / saa7134 / saa7134-input.c
blob83887d1876a9dd659a1d6becd8165da524e36b27
1 /*
3 * handle saa7134 IR remotes via linux kernel input layer.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/sched.h>
26 #include <linux/interrupt.h>
27 #include <linux/input.h>
29 #include "saa7134-reg.h"
30 #include "saa7134.h"
32 static unsigned int disable_ir = 0;
33 module_param(disable_ir, int, 0444);
34 MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
36 static unsigned int ir_debug = 0;
37 module_param(ir_debug, int, 0644);
38 MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
40 static int pinnacle_remote = 0;
41 module_param(pinnacle_remote, int, 0644); /* Choose Pinnacle PCTV remote */
42 MODULE_PARM_DESC(pinnacle_remote, "Specify Pinnacle PCTV remote: 0=coloured, 1=grey (defaults to 0)");
44 #define dprintk(fmt, arg...) if (ir_debug) \
45 printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
46 #define i2cdprintk(fmt, arg...) if (ir_debug) \
47 printk(KERN_DEBUG "%s/ir: " fmt, ir->c.name , ## arg)
49 /* -------------------- GPIO generic keycode builder -------------------- */
51 static int build_key(struct saa7134_dev *dev)
53 struct saa7134_ir *ir = dev->remote;
54 u32 gpio, data;
56 /* rising SAA7134_GPIO_GPRESCAN reads the status */
57 saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
58 saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
60 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
61 if (ir->polling) {
62 if (ir->last_gpio == gpio)
63 return 0;
64 ir->last_gpio = gpio;
67 data = ir_extract_bits(gpio, ir->mask_keycode);
68 dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
69 gpio, ir->mask_keycode, data);
71 if (ir->polling) {
72 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
73 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
74 ir_input_keydown(ir->dev, &ir->ir, data, data);
75 } else {
76 ir_input_nokey(ir->dev, &ir->ir);
79 else { /* IRQ driven mode - handle key press and release in one go */
80 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
81 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
82 ir_input_keydown(ir->dev, &ir->ir, data, data);
83 ir_input_nokey(ir->dev, &ir->ir);
87 return 0;
90 /* --------------------- Chip specific I2C key builders ----------------- */
92 static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
94 unsigned char b;
96 /* poll IR chip */
97 if (1 != i2c_master_recv(&ir->c,&b,1)) {
98 i2cdprintk("read error\n");
99 return -EIO;
102 /* no button press */
103 if (b==0)
104 return 0;
106 /* repeating */
107 if (b & 0x80)
108 return 1;
110 *ir_key = b;
111 *ir_raw = b;
112 return 1;
115 void saa7134_input_irq(struct saa7134_dev *dev)
117 struct saa7134_ir *ir = dev->remote;
119 if (!ir->polling)
120 build_key(dev);
123 static void saa7134_input_timer(unsigned long data)
125 struct saa7134_dev *dev = (struct saa7134_dev*)data;
126 struct saa7134_ir *ir = dev->remote;
127 unsigned long timeout;
129 build_key(dev);
130 timeout = jiffies + (ir->polling * HZ / 1000);
131 mod_timer(&ir->timer, timeout);
134 int saa7134_input_init1(struct saa7134_dev *dev)
136 struct saa7134_ir *ir;
137 struct input_dev *input_dev;
138 IR_KEYTAB_TYPE *ir_codes = NULL;
139 u32 mask_keycode = 0;
140 u32 mask_keydown = 0;
141 u32 mask_keyup = 0;
142 int polling = 0;
143 int ir_type = IR_TYPE_OTHER;
145 if (dev->has_remote != SAA7134_REMOTE_GPIO)
146 return -ENODEV;
147 if (disable_ir)
148 return -ENODEV;
150 /* detect & configure */
151 switch (dev->board) {
152 case SAA7134_BOARD_FLYVIDEO2000:
153 case SAA7134_BOARD_FLYVIDEO3000:
154 case SAA7134_BOARD_FLYTVPLATINUM_FM:
155 case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
156 ir_codes = ir_codes_flyvideo;
157 mask_keycode = 0xEC00000;
158 mask_keydown = 0x0040000;
159 break;
160 case SAA7134_BOARD_CINERGY400:
161 case SAA7134_BOARD_CINERGY600:
162 case SAA7134_BOARD_CINERGY600_MK3:
163 ir_codes = ir_codes_cinergy;
164 mask_keycode = 0x00003f;
165 mask_keyup = 0x040000;
166 break;
167 case SAA7134_BOARD_ECS_TVP3XP:
168 case SAA7134_BOARD_ECS_TVP3XP_4CB5:
169 ir_codes = ir_codes_eztv;
170 mask_keycode = 0x00017c;
171 mask_keyup = 0x000002;
172 polling = 50; // ms
173 break;
174 case SAA7134_BOARD_KWORLD_XPERT:
175 case SAA7134_BOARD_AVACSSMARTTV:
176 ir_codes = ir_codes_pixelview;
177 mask_keycode = 0x00001F;
178 mask_keyup = 0x000020;
179 polling = 50; // ms
180 break;
181 case SAA7134_BOARD_MD2819:
182 case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
183 case SAA7134_BOARD_AVERMEDIA_305:
184 case SAA7134_BOARD_AVERMEDIA_307:
185 case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
186 case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
187 case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
188 case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
189 ir_codes = ir_codes_avermedia;
190 mask_keycode = 0x0007C8;
191 mask_keydown = 0x000010;
192 polling = 50; // ms
193 /* Set GPIO pin2 to high to enable the IR controller */
194 saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
195 saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
196 break;
197 case SAA7134_BOARD_AVERMEDIA_777:
198 case SAA7134_BOARD_AVERMEDIA_A16AR:
199 ir_codes = ir_codes_avermedia;
200 mask_keycode = 0x02F200;
201 mask_keydown = 0x000400;
202 polling = 50; // ms
203 /* Without this we won't receive key up events */
204 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
205 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
206 break;
207 case SAA7134_BOARD_KWORLD_TERMINATOR:
208 ir_codes = ir_codes_pixelview;
209 mask_keycode = 0x00001f;
210 mask_keyup = 0x000060;
211 polling = 50; // ms
212 break;
213 case SAA7134_BOARD_MANLI_MTV001:
214 case SAA7134_BOARD_MANLI_MTV002:
215 case SAA7134_BOARD_BEHOLD_409FM:
216 ir_codes = ir_codes_manli;
217 mask_keycode = 0x001f00;
218 mask_keyup = 0x004000;
219 polling = 50; // ms
220 break;
221 case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
222 ir_codes = ir_codes_pctv_sedna;
223 mask_keycode = 0x001f00;
224 mask_keyup = 0x004000;
225 polling = 50; // ms
226 break;
227 case SAA7134_BOARD_GOTVIEW_7135:
228 ir_codes = ir_codes_gotview7135;
229 mask_keycode = 0x0003EC;
230 mask_keyup = 0x008000;
231 mask_keydown = 0x000010;
232 polling = 50; // ms
233 break;
234 case SAA7134_BOARD_VIDEOMATE_TV_PVR:
235 case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
236 case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
237 ir_codes = ir_codes_videomate_tv_pvr;
238 mask_keycode = 0x00003F;
239 mask_keyup = 0x400000;
240 polling = 50; // ms
241 break;
242 case SAA7134_BOARD_PROTEUS_2309:
243 ir_codes = ir_codes_proteus_2309;
244 mask_keycode = 0x00007F;
245 mask_keyup = 0x000080;
246 polling = 50; // ms
247 break;
248 case SAA7134_BOARD_VIDEOMATE_DVBT_300:
249 case SAA7134_BOARD_VIDEOMATE_DVBT_200:
250 ir_codes = ir_codes_videomate_tv_pvr;
251 mask_keycode = 0x003F00;
252 mask_keyup = 0x040000;
253 break;
254 case SAA7134_BOARD_FLYDVBT_LR301:
255 case SAA7134_BOARD_FLYDVBTDUO:
256 ir_codes = ir_codes_flydvb;
257 mask_keycode = 0x0001F00;
258 mask_keydown = 0x0040000;
259 break;
261 if (NULL == ir_codes) {
262 printk("%s: Oops: IR config error [card=%d]\n",
263 dev->name, dev->board);
264 return -ENODEV;
267 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
268 input_dev = input_allocate_device();
269 if (!ir || !input_dev) {
270 kfree(ir);
271 input_free_device(input_dev);
272 return -ENOMEM;
275 ir->dev = input_dev;
277 /* init hardware-specific stuff */
278 ir->mask_keycode = mask_keycode;
279 ir->mask_keydown = mask_keydown;
280 ir->mask_keyup = mask_keyup;
281 ir->polling = polling;
283 /* init input device */
284 snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
285 saa7134_boards[dev->board].name);
286 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
287 pci_name(dev->pci));
289 ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
290 input_dev->name = ir->name;
291 input_dev->phys = ir->phys;
292 input_dev->id.bustype = BUS_PCI;
293 input_dev->id.version = 1;
294 if (dev->pci->subsystem_vendor) {
295 input_dev->id.vendor = dev->pci->subsystem_vendor;
296 input_dev->id.product = dev->pci->subsystem_device;
297 } else {
298 input_dev->id.vendor = dev->pci->vendor;
299 input_dev->id.product = dev->pci->device;
301 input_dev->cdev.dev = &dev->pci->dev;
303 /* all done */
304 dev->remote = ir;
305 if (ir->polling) {
306 init_timer(&ir->timer);
307 ir->timer.function = saa7134_input_timer;
308 ir->timer.data = (unsigned long)dev;
309 ir->timer.expires = jiffies + HZ;
310 add_timer(&ir->timer);
313 input_register_device(ir->dev);
314 return 0;
317 void saa7134_input_fini(struct saa7134_dev *dev)
319 if (NULL == dev->remote)
320 return;
322 if (dev->remote->polling)
323 del_timer_sync(&dev->remote->timer);
324 input_unregister_device(dev->remote->dev);
325 kfree(dev->remote);
326 dev->remote = NULL;
329 void saa7134_set_i2c_ir(struct saa7134_dev *dev, struct IR_i2c *ir)
331 if (disable_ir) {
332 dprintk("Found supported i2c remote, but IR has been disabled\n");
333 ir->get_key=NULL;
334 return;
337 switch (dev->board) {
338 case SAA7134_BOARD_PINNACLE_PCTV_110i:
339 case SAA7134_BOARD_PINNACLE_PCTV_310i:
340 snprintf(ir->c.name, sizeof(ir->c.name), "Pinnacle PCTV");
341 if (pinnacle_remote == 0) {
342 ir->get_key = get_key_pinnacle_color;
343 ir->ir_codes = ir_codes_pinnacle_color;
344 } else {
345 ir->get_key = get_key_pinnacle_grey;
346 ir->ir_codes = ir_codes_pinnacle_grey;
348 break;
349 case SAA7134_BOARD_UPMOST_PURPLE_TV:
350 snprintf(ir->c.name, sizeof(ir->c.name), "Purple TV");
351 ir->get_key = get_key_purpletv;
352 ir->ir_codes = ir_codes_purpletv;
353 break;
354 default:
355 dprintk("Shouldn't get here: Unknown board %x for I2C IR?\n",dev->board);
356 break;
360 /* ----------------------------------------------------------------------
361 * Local variables:
362 * c-basic-offset: 8
363 * End: