blob: 60b38defd9bccf3d86a1428c7a87dc1544100321 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * handle saa7134 IR remotes via linux kernel input layer.
4 *
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.
9 *
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.
14 *
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
18 *
19 */
20
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>
28
29#include "saa7134-reg.h"
30#include "saa7134.h"
31
32static unsigned int disable_ir = 0;
33module_param(disable_ir, int, 0444);
34MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
35
36static unsigned int ir_debug = 0;
37module_param(ir_debug, int, 0644);
38MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
39
Sylvain Pascheb93eedb2006-03-25 23:14:42 -030040static int pinnacle_remote = 0;
41module_param(pinnacle_remote, int, 0644); /* Choose Pinnacle PCTV remote */
42MODULE_PARM_DESC(pinnacle_remote, "Specify Pinnacle PCTV remote: 0=coloured, 1=grey (defaults to 0)");
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define dprintk(fmt, arg...) if (ir_debug) \
45 printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -080046#define i2cdprintk(fmt, arg...) if (ir_debug) \
47 printk(KERN_DEBUG "%s/ir: " fmt, ir->c.name , ## arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -080049/* -------------------- GPIO generic keycode builder -------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51static int build_key(struct saa7134_dev *dev)
52{
53 struct saa7134_ir *ir = dev->remote;
54 u32 gpio, data;
55
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);
59
60 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080061 if (ir->polling) {
62 if (ir->last_gpio == gpio)
63 return 0;
64 ir->last_gpio = gpio;
65 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080067 data = ir_extract_bits(gpio, ir->mask_keycode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
69 gpio, ir->mask_keycode, data);
70
Peter Missel0602fbb2006-01-09 18:21:23 -020071 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);
77 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
Peter Missel0602fbb2006-01-09 18:21:23 -020079 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);
84 }
85 }
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return 0;
88}
89
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -080090/* --------------------- Chip specific I2C key builders ----------------- */
91
92static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
93{
94 unsigned char b;
95
96 /* poll IR chip */
97 if (1 != i2c_master_recv(&ir->c,&b,1)) {
98 i2cdprintk("read error\n");
99 return -EIO;
100 }
101
102 /* no button press */
103 if (b==0)
104 return 0;
105
106 /* repeating */
107 if (b & 0x80)
108 return 1;
109
110 *ir_key = b;
111 *ir_raw = b;
112 return 1;
113}
114
Thomas Genty177aaaf2006-11-29 21:57:24 -0300115static int get_key_hvr1110(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
116{
117 unsigned char buf[5], cod4, code3, code4;
118
119 /* poll IR chip */
120 if (5 != i2c_master_recv(&ir->c,buf,5))
121 return -EIO;
122
123 cod4 = buf[4];
124 code4 = (cod4 >> 2);
125 code3 = buf[3];
126 if (code3 == 0)
127 /* no key pressed */
128 return 0;
129
130 /* return key */
131 *ir_key = code4;
132 *ir_raw = code4;
133 return 1;
134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136void saa7134_input_irq(struct saa7134_dev *dev)
137{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800138 struct saa7134_ir *ir = dev->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800140 if (!ir->polling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 build_key(dev);
142}
143
144static void saa7134_input_timer(unsigned long data)
145{
146 struct saa7134_dev *dev = (struct saa7134_dev*)data;
147 struct saa7134_ir *ir = dev->remote;
148 unsigned long timeout;
149
150 build_key(dev);
151 timeout = jiffies + (ir->polling * HZ / 1000);
152 mod_timer(&ir->timer, timeout);
153}
154
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300155static void saa7134_ir_start(struct saa7134_dev *dev, struct saa7134_ir *ir)
156{
157 if (ir->polling) {
158 init_timer(&ir->timer);
159 ir->timer.function = saa7134_input_timer;
160 ir->timer.data = (unsigned long)dev;
161 ir->timer.expires = jiffies + HZ;
162 add_timer(&ir->timer);
163 }
164}
165
166static void saa7134_ir_stop(struct saa7134_dev *dev)
167{
168 if (dev->remote->polling)
169 del_timer_sync(&dev->remote->timer);
170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172int saa7134_input_init1(struct saa7134_dev *dev)
173{
174 struct saa7134_ir *ir;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500175 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 IR_KEYTAB_TYPE *ir_codes = NULL;
177 u32 mask_keycode = 0;
178 u32 mask_keydown = 0;
179 u32 mask_keyup = 0;
180 int polling = 0;
181 int ir_type = IR_TYPE_OTHER;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300182 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Ricardo Cerqueiracb2444d2005-11-08 21:38:47 -0800184 if (dev->has_remote != SAA7134_REMOTE_GPIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return -ENODEV;
186 if (disable_ir)
187 return -ENODEV;
188
189 /* detect & configure */
190 switch (dev->board) {
191 case SAA7134_BOARD_FLYVIDEO2000:
192 case SAA7134_BOARD_FLYVIDEO3000:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800193 case SAA7134_BOARD_FLYTVPLATINUM_FM:
Arnaud Patard6af90ab2005-11-08 21:36:55 -0800194 case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
Ricardo Cerqueira4c0f6312006-01-23 09:42:06 -0200195 ir_codes = ir_codes_flyvideo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 mask_keycode = 0xEC00000;
197 mask_keydown = 0x0040000;
198 break;
199 case SAA7134_BOARD_CINERGY400:
200 case SAA7134_BOARD_CINERGY600:
201 case SAA7134_BOARD_CINERGY600_MK3:
Ricardo Cerqueira4c0f6312006-01-23 09:42:06 -0200202 ir_codes = ir_codes_cinergy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 mask_keycode = 0x00003f;
204 mask_keyup = 0x040000;
205 break;
206 case SAA7134_BOARD_ECS_TVP3XP:
207 case SAA7134_BOARD_ECS_TVP3XP_4CB5:
Ricardo Cerqueira4c0f6312006-01-23 09:42:06 -0200208 ir_codes = ir_codes_eztv;
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700209 mask_keycode = 0x00017c;
210 mask_keyup = 0x000002;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 polling = 50; // ms
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700212 break;
213 case SAA7134_BOARD_KWORLD_XPERT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 case SAA7134_BOARD_AVACSSMARTTV:
Nickolay V. Shmyrevb639f9d2006-01-23 09:44:10 -0200215 ir_codes = ir_codes_pixelview;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 mask_keycode = 0x00001F;
217 mask_keyup = 0x000020;
218 polling = 50; // ms
219 break;
220 case SAA7134_BOARD_MD2819:
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700221 case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 case SAA7134_BOARD_AVERMEDIA_305:
223 case SAA7134_BOARD_AVERMEDIA_307:
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700224 case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
225 case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
Mikhail Fedotov3ac706d2006-10-06 20:23:47 -0300226 case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700227 case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
Nickolay V. Shmyrevb639f9d2006-01-23 09:44:10 -0200228 ir_codes = ir_codes_avermedia;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 mask_keycode = 0x0007C8;
230 mask_keydown = 0x000010;
231 polling = 50; // ms
232 /* Set GPIO pin2 to high to enable the IR controller */
233 saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
234 saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
235 break;
pasky@ucw.cz450efcf2006-11-12 14:22:32 -0300236 case SAA7134_BOARD_AVERMEDIA_777:
pasky@ucw.cz29e0f1a2006-11-12 14:23:32 -0300237 case SAA7134_BOARD_AVERMEDIA_A16AR:
pasky@ucw.cz450efcf2006-11-12 14:22:32 -0300238 ir_codes = ir_codes_avermedia;
239 mask_keycode = 0x02F200;
240 mask_keydown = 0x000400;
241 polling = 50; // ms
242 /* Without this we won't receive key up events */
243 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
244 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
Linus Torvalds4dd74062006-11-13 09:50:11 -0800245 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800246 case SAA7134_BOARD_KWORLD_TERMINATOR:
Nickolay V. Shmyrevb639f9d2006-01-23 09:44:10 -0200247 ir_codes = ir_codes_pixelview;
James R. Webbdc2286c2005-11-08 21:37:00 -0800248 mask_keycode = 0x00001f;
249 mask_keyup = 0x000060;
250 polling = 50; // ms
251 break;
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700252 case SAA7134_BOARD_MANLI_MTV001:
253 case SAA7134_BOARD_MANLI_MTV002:
Nickolay V. Shmyreva8ff4172005-11-08 21:36:16 -0800254 case SAA7134_BOARD_BEHOLD_409FM:
Ricardo Cerqueira4c0f6312006-01-23 09:42:06 -0200255 ir_codes = ir_codes_manli;
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700256 mask_keycode = 0x001f00;
257 mask_keyup = 0x004000;
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700258 polling = 50; // ms
259 break;
Ricardo Cerqueira17ce1ff2005-11-08 21:38:47 -0800260 case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
Ricardo Cerqueira4c0f6312006-01-23 09:42:06 -0200261 ir_codes = ir_codes_pctv_sedna;
Pavel Mihaylovc3d93192005-11-08 21:38:43 -0800262 mask_keycode = 0x001f00;
263 mask_keyup = 0x004000;
264 polling = 50; // ms
265 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800266 case SAA7134_BOARD_GOTVIEW_7135:
Ricardo Cerqueira4c0f6312006-01-23 09:42:06 -0200267 ir_codes = ir_codes_gotview7135;
Nickolay V. Shmyrev6b961442005-11-08 21:36:22 -0800268 mask_keycode = 0x0003EC;
269 mask_keyup = 0x008000;
270 mask_keydown = 0x000010;
271 polling = 50; // ms
272 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 case SAA7134_BOARD_VIDEOMATE_TV_PVR:
Nickolay V. Shmyrev2a9a9a82006-01-09 15:25:33 -0200274 case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700275 case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
Ricardo Cerqueira4c0f6312006-01-23 09:42:06 -0200276 ir_codes = ir_codes_videomate_tv_pvr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 mask_keycode = 0x00003F;
278 mask_keyup = 0x400000;
279 polling = 50; // ms
280 break;
Michal Majchrowiczb04c1ba2006-09-13 16:42:42 -0300281 case SAA7134_BOARD_PROTEUS_2309:
282 ir_codes = ir_codes_proteus_2309;
283 mask_keycode = 0x00007F;
284 mask_keyup = 0x000080;
285 polling = 50; // ms
286 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800287 case SAA7134_BOARD_VIDEOMATE_DVBT_300:
288 case SAA7134_BOARD_VIDEOMATE_DVBT_200:
Ricardo Cerqueira4c0f6312006-01-23 09:42:06 -0200289 ir_codes = ir_codes_videomate_tv_pvr;
Nickolay V. Shmyrevfea095f2005-11-08 21:36:47 -0800290 mask_keycode = 0x003F00;
291 mask_keyup = 0x040000;
292 break;
Giampiero Giancipoli3d8466e2006-02-07 06:49:09 -0200293 case SAA7134_BOARD_FLYDVBT_LR301:
Rudo Thomasa8029172006-02-27 00:08:46 -0300294 case SAA7134_BOARD_FLYDVBTDUO:
Giampiero Giancipoli3d8466e2006-02-07 06:49:09 -0200295 ir_codes = ir_codes_flydvb;
296 mask_keycode = 0x0001F00;
297 mask_keydown = 0x0040000;
298 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300 if (NULL == ir_codes) {
301 printk("%s: Oops: IR config error [card=%d]\n",
302 dev->name, dev->board);
303 return -ENODEV;
304 }
305
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500306 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
307 input_dev = input_allocate_device();
308 if (!ir || !input_dev) {
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300309 err = -ENOMEM;
310 goto err_out_free;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Dmitry Torokhovd271d1c2005-11-20 00:56:54 -0500313 ir->dev = input_dev;
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /* init hardware-specific stuff */
316 ir->mask_keycode = mask_keycode;
317 ir->mask_keydown = mask_keydown;
318 ir->mask_keyup = mask_keyup;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800319 ir->polling = polling;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 /* init input device */
322 snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
323 saa7134_boards[dev->board].name);
324 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
325 pci_name(dev->pci));
326
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500327 ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
328 input_dev->name = ir->name;
329 input_dev->phys = ir->phys;
330 input_dev->id.bustype = BUS_PCI;
331 input_dev->id.version = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (dev->pci->subsystem_vendor) {
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500333 input_dev->id.vendor = dev->pci->subsystem_vendor;
334 input_dev->id.product = dev->pci->subsystem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 } else {
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500336 input_dev->id.vendor = dev->pci->vendor;
337 input_dev->id.product = dev->pci->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500339 input_dev->cdev.dev = &dev->pci->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 dev->remote = ir;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300342 saa7134_ir_start(dev, ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300344 err = input_register_device(ir->dev);
345 if (err)
346 goto err_out_stop;
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return 0;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300349
350 err_out_stop:
351 saa7134_ir_stop(dev);
352 dev->remote = NULL;
353 err_out_free:
354 input_free_device(input_dev);
355 kfree(ir);
356 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
359void saa7134_input_fini(struct saa7134_dev *dev)
360{
361 if (NULL == dev->remote)
362 return;
363
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300364 saa7134_ir_stop(dev);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500365 input_unregister_device(dev->remote->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 kfree(dev->remote);
367 dev->remote = NULL;
368}
369
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800370void saa7134_set_i2c_ir(struct saa7134_dev *dev, struct IR_i2c *ir)
371{
372 if (disable_ir) {
Ricardo Cerqueiracb2444d2005-11-08 21:38:47 -0800373 dprintk("Found supported i2c remote, but IR has been disabled\n");
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800374 ir->get_key=NULL;
375 return;
376 }
377
378 switch (dev->board) {
379 case SAA7134_BOARD_PINNACLE_PCTV_110i:
Hartmut Hackmanneb591af2006-10-12 19:46:16 -0300380 case SAA7134_BOARD_PINNACLE_PCTV_310i:
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800381 snprintf(ir->c.name, sizeof(ir->c.name), "Pinnacle PCTV");
Sylvain Pascheb93eedb2006-03-25 23:14:42 -0300382 if (pinnacle_remote == 0) {
383 ir->get_key = get_key_pinnacle_color;
384 ir->ir_codes = ir_codes_pinnacle_color;
385 } else {
386 ir->get_key = get_key_pinnacle_grey;
387 ir->ir_codes = ir_codes_pinnacle_grey;
388 }
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800389 break;
390 case SAA7134_BOARD_UPMOST_PURPLE_TV:
391 snprintf(ir->c.name, sizeof(ir->c.name), "Purple TV");
392 ir->get_key = get_key_purpletv;
393 ir->ir_codes = ir_codes_purpletv;
394 break;
Thomas Genty177aaaf2006-11-29 21:57:24 -0300395 case SAA7134_BOARD_HAUPPAUGE_HVR1110:
396 snprintf(ir->c.name, sizeof(ir->c.name), "HVR 1110");
397 ir->get_key = get_key_hvr1110;
398 ir->ir_codes = ir_codes_hauppauge_new;
399 break;
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800400 default:
401 dprintk("Shouldn't get here: Unknown board %x for I2C IR?\n",dev->board);
402 break;
403 }
404
405}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/* ----------------------------------------------------------------------
407 * Local variables:
408 * c-basic-offset: 8
409 * End:
410 */