blob: 25217ae6606c4e3b55ee899f2c097f5ceaba2bb0 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/interrupt.h>
25#include <linux/input.h>
26
27#include "saa7134-reg.h"
28#include "saa7134.h"
29
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030030static unsigned int disable_ir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031module_param(disable_ir, int, 0444);
32MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
33
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030034static unsigned int ir_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035module_param(ir_debug, int, 0644);
36MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
37
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030038static int pinnacle_remote;
Sylvain Pascheb93eedb2006-03-25 23:14:42 -030039module_param(pinnacle_remote, int, 0644); /* Choose Pinnacle PCTV remote */
40MODULE_PARM_DESC(pinnacle_remote, "Specify Pinnacle PCTV remote: 0=coloured, 1=grey (defaults to 0)");
41
Adrian Bunkc408a6f2006-12-28 12:47:47 -030042static int ir_rc5_remote_gap = 885;
Hermann Pitton91607232006-12-07 21:45:28 -030043module_param(ir_rc5_remote_gap, int, 0644);
Adrian Bunkc408a6f2006-12-28 12:47:47 -030044static int ir_rc5_key_timeout = 115;
Hermann Pitton91607232006-12-07 21:45:28 -030045module_param(ir_rc5_key_timeout, int, 0644);
46
Pedro0938e312007-10-17 17:58:40 -030047static int repeat_delay = 500;
48module_param(repeat_delay, int, 0644);
49MODULE_PARM_DESC(repeat_delay, "delay before key repeat started");
50static int repeat_period = 33;
51module_param(repeat_period, int, 0644);
Joe Perchesac9bb7f2007-11-20 09:00:35 -030052MODULE_PARM_DESC(repeat_period, "repeat period between "
Pedro0938e312007-10-17 17:58:40 -030053 "keypresses when key is down");
54
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -030055static unsigned int disable_other_ir;
56module_param(disable_other_ir, int, 0644);
57MODULE_PARM_DESC(disable_other_ir, "disable full codes of "
58 "alternative remotes from other manufacturers");
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define dprintk(fmt, arg...) if (ir_debug) \
61 printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -080062#define i2cdprintk(fmt, arg...) if (ir_debug) \
63 printk(KERN_DEBUG "%s/ir: " fmt, ir->c.name , ## arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -030065/* Helper functions for RC5 and NEC decoding at GPIO16 or GPIO18 */
Hermann Pitton91607232006-12-07 21:45:28 -030066static int saa7134_rc5_irq(struct saa7134_dev *dev);
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -030067static int saa7134_nec_irq(struct saa7134_dev *dev);
68static void nec_task(unsigned long data);
69static void saa7134_nec_timer(unsigned long data);
Hermann Pitton91607232006-12-07 21:45:28 -030070
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -080071/* -------------------- GPIO generic keycode builder -------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73static int build_key(struct saa7134_dev *dev)
74{
Hermann Pitton91607232006-12-07 21:45:28 -030075 struct card_ir *ir = dev->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 u32 gpio, data;
77
Pedro0938e312007-10-17 17:58:40 -030078 /* here comes the additional handshake steps for some cards */
79 switch (dev->board) {
80 case SAA7134_BOARD_GOTVIEW_7135:
81 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x80);
82 saa_clearb(SAA7134_GPIO_GPSTATUS1, 0x80);
83 break;
84 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 /* rising SAA7134_GPIO_GPRESCAN reads the status */
86 saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
87 saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
88
89 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080090 if (ir->polling) {
91 if (ir->last_gpio == gpio)
92 return 0;
93 ir->last_gpio = gpio;
94 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080096 data = ir_extract_bits(gpio, ir->mask_keycode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
98 gpio, ir->mask_keycode, data);
99
Mauro Carvalho Chehab26d5f3a2008-12-07 13:19:29 -0300100 switch (dev->board) {
101 case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
102 if (data == ir->mask_keycode)
103 ir_input_nokey(ir->dev, &ir->ir);
104 else
105 ir_input_keydown(ir->dev, &ir->ir, data, data);
106 return 0;
107 }
108
Peter Missel0602fbb2006-01-09 18:21:23 -0200109 if (ir->polling) {
110 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
111 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
112 ir_input_keydown(ir->dev, &ir->ir, data, data);
113 } else {
114 ir_input_nokey(ir->dev, &ir->ir);
115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
Peter Missel0602fbb2006-01-09 18:21:23 -0200117 else { /* IRQ driven mode - handle key press and release in one go */
118 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
119 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
120 ir_input_keydown(ir->dev, &ir->ir, data, data);
121 ir_input_nokey(ir->dev, &ir->ir);
122 }
123 }
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return 0;
126}
127
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800128/* --------------------- Chip specific I2C key builders ----------------- */
129
Brian Rogersba340b42008-10-13 08:37:06 -0300130static int get_key_msi_tvanywhere_plus(struct IR_i2c *ir, u32 *ir_key,
131 u32 *ir_raw)
132{
133 unsigned char b;
134 int gpio;
135
136 /* <dev> is needed to access GPIO. Used by the saa_readl macro. */
137 struct saa7134_dev *dev = ir->c.adapter->algo_data;
138 if (dev == NULL) {
139 dprintk("get_key_msi_tvanywhere_plus: "
140 "gir->c.adapter->algo_data is NULL!\n");
141 return -EIO;
142 }
143
144 /* rising SAA7134_GPIO_GPRESCAN reads the status */
145
146 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
147 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
148
149 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
150
151 /* GPIO&0x40 is pulsed low when a button is pressed. Don't do
152 I2C receive if gpio&0x40 is not low. */
153
154 if (gpio & 0x40)
155 return 0; /* No button press */
156
157 /* GPIO says there is a button press. Get it. */
158
159 if (1 != i2c_master_recv(&ir->c, &b, 1)) {
160 i2cdprintk("read error\n");
161 return -EIO;
162 }
163
164 /* No button press */
165
166 if (b == 0xff)
167 return 0;
168
169 /* Button pressed */
170
171 dprintk("get_key_msi_tvanywhere_plus: Key = 0x%02X\n", b);
172 *ir_key = b;
173 *ir_raw = b;
174 return 1;
175}
176
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800177static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
178{
179 unsigned char b;
180
181 /* poll IR chip */
182 if (1 != i2c_master_recv(&ir->c,&b,1)) {
183 i2cdprintk("read error\n");
184 return -EIO;
185 }
186
187 /* no button press */
188 if (b==0)
189 return 0;
190
191 /* repeating */
192 if (b & 0x80)
193 return 1;
194
195 *ir_key = b;
196 *ir_raw = b;
197 return 1;
198}
199
Thomas Genty177aaaf2006-11-29 21:57:24 -0300200static int get_key_hvr1110(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
201{
202 unsigned char buf[5], cod4, code3, code4;
203
204 /* poll IR chip */
205 if (5 != i2c_master_recv(&ir->c,buf,5))
206 return -EIO;
207
208 cod4 = buf[4];
209 code4 = (cod4 >> 2);
210 code3 = buf[3];
211 if (code3 == 0)
212 /* no key pressed */
213 return 0;
214
215 /* return key */
216 *ir_key = code4;
217 *ir_raw = code4;
218 return 1;
219}
220
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300221
222static int get_key_beholdm6xx(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
223{
224 unsigned char data[12];
225 u32 gpio;
226
227 struct saa7134_dev *dev = ir->c.adapter->algo_data;
228
229 /* rising SAA7134_GPIO_GPRESCAN reads the status */
230 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
231 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
232
233 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
234
Mauro Carvalho Chehab616f8872008-01-07 05:18:36 -0300235 if (0x400000 & ~gpio)
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300236 return 0; /* No button press */
237
238 ir->c.addr = 0x5a >> 1;
239
240 if (12 != i2c_master_recv(&ir->c, data, 12)) {
241 i2cdprintk("read error\n");
242 return -EIO;
243 }
244 /* IR of this card normally decode signals NEC-standard from
245 * - Sven IHOO MT 5.1R remote. xxyye718
246 * - Sven DVD HD-10xx remote. xxyyf708
247 * - BBK ...
248 * - mayby others
249 * So, skip not our, if disable full codes mode.
250 */
251 if (data[10] != 0x6b && data[11] != 0x86 && disable_other_ir)
252 return 0;
253
254 *ir_key = data[9];
255 *ir_raw = data[9];
256
257 return 1;
258}
259
Mauro Carvalho Chehab1c22dad2008-07-17 22:31:29 -0300260/* Common (grey or coloured) pinnacle PCTV remote handling
261 *
262 */
263static int get_key_pinnacle(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw,
264 int parity_offset, int marker, int code_modulo)
265{
266 unsigned char b[4];
267 unsigned int start = 0,parity = 0,code = 0;
268
269 /* poll IR chip */
270 if (4 != i2c_master_recv(&ir->c, b, 4)) {
271 i2cdprintk("read error\n");
272 return -EIO;
273 }
274
275 for (start = 0; start < ARRAY_SIZE(b); start++) {
276 if (b[start] == marker) {
277 code=b[(start+parity_offset + 1) % 4];
278 parity=b[(start+parity_offset) % 4];
279 }
280 }
281
282 /* Empty Request */
283 if (parity == 0)
284 return 0;
285
286 /* Repeating... */
287 if (ir->old == parity)
288 return 0;
289
290 ir->old = parity;
291
292 /* drop special codes when a key is held down a long time for the grey controller
293 In this case, the second bit of the code is asserted */
294 if (marker == 0xfe && (code & 0x40))
295 return 0;
296
297 code %= code_modulo;
298
299 *ir_raw = code;
300 *ir_key = code;
301
302 i2cdprintk("Pinnacle PCTV key %02x\n", code);
303
304 return 1;
305}
306
307/* The grey pinnacle PCTV remote
308 *
309 * There are one issue with this remote:
310 * - I2c packet does not change when the same key is pressed quickly. The workaround
311 * is to hold down each key for about half a second, so that another code is generated
312 * in the i2c packet, and the function can distinguish key presses.
313 *
314 * Sylvain Pasche <sylvain.pasche@gmail.com>
315 */
316static int get_key_pinnacle_grey(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
317{
318
319 return get_key_pinnacle(ir, ir_key, ir_raw, 1, 0xfe, 0xff);
320}
321
322
323/* The new pinnacle PCTV remote (with the colored buttons)
324 *
325 * Ricardo Cerqueira <v4l@cerqueira.org>
326 */
327static int get_key_pinnacle_color(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
328{
329 /* code_modulo parameter (0x88) is used to reduce code value to fit inside IR_KEYTAB_SIZE
330 *
331 * this is the only value that results in 42 unique
332 * codes < 128
333 */
334
335 return get_key_pinnacle(ir, ir_key, ir_raw, 2, 0x80, 0x88);
336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338void saa7134_input_irq(struct saa7134_dev *dev)
339{
Hermann Pitton91607232006-12-07 21:45:28 -0300340 struct card_ir *ir = dev->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -0300342 if (ir->nec_gpio) {
343 saa7134_nec_irq(dev);
344 } else if (!ir->polling && !ir->rc5_gpio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 build_key(dev);
Hermann Pitton91607232006-12-07 21:45:28 -0300346 } else if (ir->rc5_gpio) {
347 saa7134_rc5_irq(dev);
348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
351static void saa7134_input_timer(unsigned long data)
352{
Dmitry Torokhovb4ba7882007-05-21 11:41:02 -0300353 struct saa7134_dev *dev = (struct saa7134_dev *)data;
Hermann Pitton91607232006-12-07 21:45:28 -0300354 struct card_ir *ir = dev->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 build_key(dev);
Dmitry Torokhovb4ba7882007-05-21 11:41:02 -0300357 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Maxim Levitskyc4584732007-10-12 00:57:15 -0300360void saa7134_ir_start(struct saa7134_dev *dev, struct card_ir *ir)
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300361{
362 if (ir->polling) {
Dmitry Torokhovb4ba7882007-05-21 11:41:02 -0300363 setup_timer(&ir->timer, saa7134_input_timer,
364 (unsigned long)dev);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300365 ir->timer.expires = jiffies + HZ;
366 add_timer(&ir->timer);
Hermann Pitton91607232006-12-07 21:45:28 -0300367 } else if (ir->rc5_gpio) {
368 /* set timer_end for code completion */
369 init_timer(&ir->timer_end);
370 ir->timer_end.function = ir_rc5_timer_end;
371 ir->timer_end.data = (unsigned long)ir;
372 init_timer(&ir->timer_keyup);
373 ir->timer_keyup.function = ir_rc5_timer_keyup;
374 ir->timer_keyup.data = (unsigned long)ir;
375 ir->shift_by = 2;
376 ir->start = 0x2;
377 ir->addr = 0x17;
378 ir->rc5_key_timeout = ir_rc5_key_timeout;
379 ir->rc5_remote_gap = ir_rc5_remote_gap;
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -0300380 } else if (ir->nec_gpio) {
381 setup_timer(&ir->timer_keyup, saa7134_nec_timer,
382 (unsigned long)dev);
383 tasklet_init(&ir->tlet, nec_task, (unsigned long)dev);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300384 }
385}
386
Maxim Levitskyc4584732007-10-12 00:57:15 -0300387void saa7134_ir_stop(struct saa7134_dev *dev)
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300388{
389 if (dev->remote->polling)
390 del_timer_sync(&dev->remote->timer);
391}
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393int saa7134_input_init1(struct saa7134_dev *dev)
394{
Hermann Pitton91607232006-12-07 21:45:28 -0300395 struct card_ir *ir;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500396 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 IR_KEYTAB_TYPE *ir_codes = NULL;
398 u32 mask_keycode = 0;
399 u32 mask_keydown = 0;
400 u32 mask_keyup = 0;
401 int polling = 0;
Hermann Pitton91607232006-12-07 21:45:28 -0300402 int rc5_gpio = 0;
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -0300403 int nec_gpio = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 int ir_type = IR_TYPE_OTHER;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300405 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Ricardo Cerqueiracb2444d2005-11-08 21:38:47 -0800407 if (dev->has_remote != SAA7134_REMOTE_GPIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return -ENODEV;
409 if (disable_ir)
410 return -ENODEV;
411
412 /* detect & configure */
413 switch (dev->board) {
414 case SAA7134_BOARD_FLYVIDEO2000:
415 case SAA7134_BOARD_FLYVIDEO3000:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800416 case SAA7134_BOARD_FLYTVPLATINUM_FM:
Arnaud Patard6af90ab2005-11-08 21:36:55 -0800417 case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
Ricardo Cerqueira4c0f6312006-01-23 09:42:06 -0200418 ir_codes = ir_codes_flyvideo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 mask_keycode = 0xEC00000;
420 mask_keydown = 0x0040000;
421 break;
422 case SAA7134_BOARD_CINERGY400:
423 case SAA7134_BOARD_CINERGY600:
424 case SAA7134_BOARD_CINERGY600_MK3:
Ricardo Cerqueira4c0f6312006-01-23 09:42:06 -0200425 ir_codes = ir_codes_cinergy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 mask_keycode = 0x00003f;
427 mask_keyup = 0x040000;
428 break;
429 case SAA7134_BOARD_ECS_TVP3XP:
430 case SAA7134_BOARD_ECS_TVP3XP_4CB5:
Ricardo Cerqueira4c0f6312006-01-23 09:42:06 -0200431 ir_codes = ir_codes_eztv;
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700432 mask_keycode = 0x00017c;
433 mask_keyup = 0x000002;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 polling = 50; // ms
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700435 break;
436 case SAA7134_BOARD_KWORLD_XPERT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 case SAA7134_BOARD_AVACSSMARTTV:
Nickolay V. Shmyrevb639f9d2006-01-23 09:44:10 -0200438 ir_codes = ir_codes_pixelview;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 mask_keycode = 0x00001F;
440 mask_keyup = 0x000020;
441 polling = 50; // ms
442 break;
443 case SAA7134_BOARD_MD2819:
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700444 case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 case SAA7134_BOARD_AVERMEDIA_305:
446 case SAA7134_BOARD_AVERMEDIA_307:
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700447 case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
448 case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
Mikhail Fedotov3ac706d2006-10-06 20:23:47 -0300449 case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
Andy Shevchenkodf0dbbe2009-04-08 14:01:19 -0300450 case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700451 case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
Albert Grahamd2761f22007-12-09 09:44:38 -0300452 case SAA7134_BOARD_AVERMEDIA_M102:
Pham Thanh Nam6a2d8022008-12-30 23:26:09 -0300453 case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
Nickolay V. Shmyrevb639f9d2006-01-23 09:44:10 -0200454 ir_codes = ir_codes_avermedia;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 mask_keycode = 0x0007C8;
456 mask_keydown = 0x000010;
457 polling = 50; // ms
458 /* Set GPIO pin2 to high to enable the IR controller */
459 saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
460 saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
461 break;
Mauro Carvalho Chehab36f6bb92008-06-26 17:03:00 -0300462 case SAA7134_BOARD_AVERMEDIA_M135A:
463 ir_codes = ir_codes_avermedia_m135a;
464 mask_keydown = 0x0040000;
465 mask_keycode = 0x00013f;
466 nec_gpio = 1;
467 break;
pasky@ucw.cz450efcf2006-11-12 14:22:32 -0300468 case SAA7134_BOARD_AVERMEDIA_777:
pasky@ucw.cz29e0f1a2006-11-12 14:23:32 -0300469 case SAA7134_BOARD_AVERMEDIA_A16AR:
pasky@ucw.cz450efcf2006-11-12 14:22:32 -0300470 ir_codes = ir_codes_avermedia;
471 mask_keycode = 0x02F200;
472 mask_keydown = 0x000400;
473 polling = 50; // ms
474 /* Without this we won't receive key up events */
475 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
476 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
Linus Torvalds4dd74062006-11-13 09:50:11 -0800477 break;
Tim Farrington6e501a32008-06-15 13:33:42 -0300478 case SAA7134_BOARD_AVERMEDIA_A16D:
479 ir_codes = ir_codes_avermedia_a16d;
480 mask_keycode = 0x02F200;
481 mask_keydown = 0x000400;
482 polling = 50; /* ms */
483 /* Without this we won't receive key up events */
484 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
485 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
486 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800487 case SAA7134_BOARD_KWORLD_TERMINATOR:
Nickolay V. Shmyrevb639f9d2006-01-23 09:44:10 -0200488 ir_codes = ir_codes_pixelview;
James R. Webbdc2286c2005-11-08 21:37:00 -0800489 mask_keycode = 0x00001f;
490 mask_keyup = 0x000060;
491 polling = 50; // ms
492 break;
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700493 case SAA7134_BOARD_MANLI_MTV001:
494 case SAA7134_BOARD_MANLI_MTV002:
Dmitry Belimovb34dddb2008-04-23 14:09:08 -0300495 ir_codes = ir_codes_manli;
496 mask_keycode = 0x001f00;
497 mask_keyup = 0x004000;
498 polling = 50; /* ms */
499 break;
Nickolay V. Shmyreva8ff4172005-11-08 21:36:16 -0800500 case SAA7134_BOARD_BEHOLD_409FM:
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300501 case SAA7134_BOARD_BEHOLD_401:
502 case SAA7134_BOARD_BEHOLD_403:
503 case SAA7134_BOARD_BEHOLD_403FM:
504 case SAA7134_BOARD_BEHOLD_405:
505 case SAA7134_BOARD_BEHOLD_405FM:
506 case SAA7134_BOARD_BEHOLD_407:
507 case SAA7134_BOARD_BEHOLD_407FM:
508 case SAA7134_BOARD_BEHOLD_409:
509 case SAA7134_BOARD_BEHOLD_505FM:
510 case SAA7134_BOARD_BEHOLD_507_9FM:
Ricardo Cerqueira4c0f6312006-01-23 09:42:06 -0200511 ir_codes = ir_codes_manli;
Dmitry Belimovb34dddb2008-04-23 14:09:08 -0300512 mask_keycode = 0x003f00;
513 mask_keyup = 0x004000;
514 polling = 50; /* ms */
515 break;
516 case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
517 ir_codes = ir_codes_behold_columbus;
518 mask_keycode = 0x003f00;
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700519 mask_keyup = 0x004000;
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700520 polling = 50; // ms
521 break;
Ricardo Cerqueira17ce1ff2005-11-08 21:38:47 -0800522 case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
Ricardo Cerqueira4c0f6312006-01-23 09:42:06 -0200523 ir_codes = ir_codes_pctv_sedna;
Pavel Mihaylovc3d93192005-11-08 21:38:43 -0800524 mask_keycode = 0x001f00;
525 mask_keyup = 0x004000;
526 polling = 50; // ms
527 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800528 case SAA7134_BOARD_GOTVIEW_7135:
Ricardo Cerqueira4c0f6312006-01-23 09:42:06 -0200529 ir_codes = ir_codes_gotview7135;
Pedro0938e312007-10-17 17:58:40 -0300530 mask_keycode = 0x0003CC;
Nickolay V. Shmyrev6b961442005-11-08 21:36:22 -0800531 mask_keydown = 0x000010;
Pedro0938e312007-10-17 17:58:40 -0300532 polling = 5; /* ms */
533 saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
Nickolay V. Shmyrev6b961442005-11-08 21:36:22 -0800534 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 case SAA7134_BOARD_VIDEOMATE_TV_PVR:
Nickolay V. Shmyrev2a9a9a82006-01-09 15:25:33 -0200536 case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700537 case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
Ricardo Cerqueira4c0f6312006-01-23 09:42:06 -0200538 ir_codes = ir_codes_videomate_tv_pvr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 mask_keycode = 0x00003F;
540 mask_keyup = 0x400000;
541 polling = 50; // ms
542 break;
Michal Majchrowiczb04c1ba2006-09-13 16:42:42 -0300543 case SAA7134_BOARD_PROTEUS_2309:
544 ir_codes = ir_codes_proteus_2309;
545 mask_keycode = 0x00007F;
546 mask_keyup = 0x000080;
547 polling = 50; // ms
548 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800549 case SAA7134_BOARD_VIDEOMATE_DVBT_300:
550 case SAA7134_BOARD_VIDEOMATE_DVBT_200:
Ricardo Cerqueira4c0f6312006-01-23 09:42:06 -0200551 ir_codes = ir_codes_videomate_tv_pvr;
Nickolay V. Shmyrevfea095f2005-11-08 21:36:47 -0800552 mask_keycode = 0x003F00;
553 mask_keyup = 0x040000;
554 break;
James Le Cuirotf5c965a2007-07-02 12:53:25 -0300555 case SAA7134_BOARD_FLYDVBS_LR300:
Giampiero Giancipoli3d8466e2006-02-07 06:49:09 -0200556 case SAA7134_BOARD_FLYDVBT_LR301:
Rudo Thomasa8029172006-02-27 00:08:46 -0300557 case SAA7134_BOARD_FLYDVBTDUO:
Giampiero Giancipoli3d8466e2006-02-07 06:49:09 -0200558 ir_codes = ir_codes_flydvb;
559 mask_keycode = 0x0001F00;
560 mask_keydown = 0x0040000;
561 break;
Hermann Pitton91607232006-12-07 21:45:28 -0300562 case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
Ed Vipas904ab882007-03-29 18:32:49 -0300563 case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
Hermann Pitton0b17d0e2008-07-12 12:27:59 -0300564 case SAA7134_BOARD_ASUSTeK_P7131_ANALOG:
Hermann Pitton91607232006-12-07 21:45:28 -0300565 ir_codes = ir_codes_asus_pc39;
566 mask_keydown = 0x0040000;
567 rc5_gpio = 1;
568 break;
Juan Pablo Sormanic36c4592006-12-27 12:46:36 -0300569 case SAA7134_BOARD_ENCORE_ENLTV:
570 case SAA7134_BOARD_ENCORE_ENLTV_FM:
571 ir_codes = ir_codes_encore_enltv;
572 mask_keycode = 0x00007f;
573 mask_keyup = 0x040000;
574 polling = 50; // ms
575 break;
Mauro Carvalho Chehabbf1ece62008-06-26 17:03:00 -0300576 case SAA7134_BOARD_ENCORE_ENLTV_FM53:
577 ir_codes = ir_codes_encore_enltv_fm53;
578 mask_keydown = 0x0040000;
579 mask_keycode = 0x00007f;
580 nec_gpio = 1;
581 break;
Tony Wan480f75a2007-05-11 11:33:50 -0300582 case SAA7134_BOARD_10MOONSTVMASTER3:
583 ir_codes = ir_codes_encore_enltv;
584 mask_keycode = 0x5f80000;
585 mask_keyup = 0x8000000;
586 polling = 50; //ms
587 break;
Adrian Pardinif0ba3562008-02-11 12:40:53 -0300588 case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
589 ir_codes = ir_codes_genius_tvgo_a11mce;
590 mask_keycode = 0xff;
591 mask_keydown = 0xf00000;
592 polling = 50; /* ms */
593 break;
Mauro Carvalho Chehab9b000192008-06-26 17:03:00 -0300594 case SAA7134_BOARD_REAL_ANGEL_220:
595 ir_codes = ir_codes_real_audio_220_32_keys;
596 mask_keycode = 0x3f00;
597 mask_keyup = 0x4000;
598 polling = 50; /* ms */
599 break;
Mauro Carvalho Chehab26d5f3a2008-12-07 13:19:29 -0300600 case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
601 ir_codes = ir_codes_kworld_plus_tv_analog;
602 mask_keycode = 0x7f;
603 polling = 40; /* ms */
604 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606 if (NULL == ir_codes) {
607 printk("%s: Oops: IR config error [card=%d]\n",
608 dev->name, dev->board);
609 return -ENODEV;
610 }
611
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500612 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
613 input_dev = input_allocate_device();
614 if (!ir || !input_dev) {
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300615 err = -ENOMEM;
616 goto err_out_free;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Dmitry Torokhovd271d1c2005-11-20 00:56:54 -0500619 ir->dev = input_dev;
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 /* init hardware-specific stuff */
622 ir->mask_keycode = mask_keycode;
623 ir->mask_keydown = mask_keydown;
624 ir->mask_keyup = mask_keyup;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800625 ir->polling = polling;
Hermann Pitton91607232006-12-07 21:45:28 -0300626 ir->rc5_gpio = rc5_gpio;
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -0300627 ir->nec_gpio = nec_gpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 /* init input device */
630 snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
631 saa7134_boards[dev->board].name);
632 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
633 pci_name(dev->pci));
634
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500635 ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
636 input_dev->name = ir->name;
637 input_dev->phys = ir->phys;
638 input_dev->id.bustype = BUS_PCI;
639 input_dev->id.version = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (dev->pci->subsystem_vendor) {
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500641 input_dev->id.vendor = dev->pci->subsystem_vendor;
642 input_dev->id.product = dev->pci->subsystem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 } else {
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500644 input_dev->id.vendor = dev->pci->vendor;
645 input_dev->id.product = dev->pci->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
Dmitry Torokhov2c8a3a32007-07-16 09:28:15 -0300647 input_dev->dev.parent = &dev->pci->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 dev->remote = ir;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300650 saa7134_ir_start(dev, ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300652 err = input_register_device(ir->dev);
653 if (err)
654 goto err_out_stop;
655
Pedro0938e312007-10-17 17:58:40 -0300656 /* the remote isn't as bouncy as a keyboard */
657 ir->dev->rep[REP_DELAY] = repeat_delay;
658 ir->dev->rep[REP_PERIOD] = repeat_period;
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return 0;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300661
662 err_out_stop:
663 saa7134_ir_stop(dev);
664 dev->remote = NULL;
665 err_out_free:
666 input_free_device(input_dev);
667 kfree(ir);
668 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671void saa7134_input_fini(struct saa7134_dev *dev)
672{
673 if (NULL == dev->remote)
674 return;
675
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300676 saa7134_ir_stop(dev);
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500677 input_unregister_device(dev->remote->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 kfree(dev->remote);
679 dev->remote = NULL;
680}
681
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800682void saa7134_set_i2c_ir(struct saa7134_dev *dev, struct IR_i2c *ir)
683{
684 if (disable_ir) {
Ricardo Cerqueiracb2444d2005-11-08 21:38:47 -0800685 dprintk("Found supported i2c remote, but IR has been disabled\n");
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800686 ir->get_key=NULL;
687 return;
688 }
689
690 switch (dev->board) {
691 case SAA7134_BOARD_PINNACLE_PCTV_110i:
Hartmut Hackmanneb591af2006-10-12 19:46:16 -0300692 case SAA7134_BOARD_PINNACLE_PCTV_310i:
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800693 snprintf(ir->c.name, sizeof(ir->c.name), "Pinnacle PCTV");
Sylvain Pascheb93eedb2006-03-25 23:14:42 -0300694 if (pinnacle_remote == 0) {
695 ir->get_key = get_key_pinnacle_color;
696 ir->ir_codes = ir_codes_pinnacle_color;
697 } else {
698 ir->get_key = get_key_pinnacle_grey;
699 ir->ir_codes = ir_codes_pinnacle_grey;
700 }
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800701 break;
702 case SAA7134_BOARD_UPMOST_PURPLE_TV:
703 snprintf(ir->c.name, sizeof(ir->c.name), "Purple TV");
704 ir->get_key = get_key_purpletv;
705 ir->ir_codes = ir_codes_purpletv;
706 break;
Brian Rogersba340b42008-10-13 08:37:06 -0300707 case SAA7134_BOARD_MSI_TVATANYWHERE_PLUS:
708 snprintf(ir->c.name, sizeof(ir->c.name), "MSI TV@nywhere Plus");
709 ir->get_key = get_key_msi_tvanywhere_plus;
710 ir->ir_codes = ir_codes_msi_tvanywhere_plus;
711 break;
Thomas Genty177aaaf2006-11-29 21:57:24 -0300712 case SAA7134_BOARD_HAUPPAUGE_HVR1110:
713 snprintf(ir->c.name, sizeof(ir->c.name), "HVR 1110");
714 ir->get_key = get_key_hvr1110;
715 ir->ir_codes = ir_codes_hauppauge_new;
716 break;
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300717 case SAA7134_BOARD_BEHOLD_607_9FM:
718 case SAA7134_BOARD_BEHOLD_M6:
Dmitry Belimov878cf2a2008-06-10 14:22:00 -0300719 case SAA7134_BOARD_BEHOLD_M63:
720 case SAA7134_BOARD_BEHOLD_M6_EXTRA:
Igor Kuznetsov02505272008-04-26 14:53:48 -0300721 case SAA7134_BOARD_BEHOLD_H6:
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300722 snprintf(ir->c.name, sizeof(ir->c.name), "BeholdTV");
723 ir->get_key = get_key_beholdm6xx;
724 ir->ir_codes = ir_codes_behold;
725 break;
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800726 default:
727 dprintk("Shouldn't get here: Unknown board %x for I2C IR?\n",dev->board);
728 break;
729 }
730
731}
Hermann Pitton91607232006-12-07 21:45:28 -0300732
733static int saa7134_rc5_irq(struct saa7134_dev *dev)
734{
735 struct card_ir *ir = dev->remote;
736 struct timeval tv;
737 u32 gap;
738 unsigned long current_jiffies, timeout;
739
740 /* get time of bit */
741 current_jiffies = jiffies;
742 do_gettimeofday(&tv);
743
744 /* avoid overflow with gap >1s */
745 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
746 gap = 200000;
747 } else {
748 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
749 tv.tv_usec - ir->base_time.tv_usec;
750 }
751
752 /* active code => add bit */
753 if (ir->active) {
754 /* only if in the code (otherwise spurious IRQ or timer
755 late) */
756 if (ir->last_bit < 28) {
757 ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
758 ir_rc5_remote_gap;
759 ir->code |= 1 << ir->last_bit;
760 }
761 /* starting new code */
762 } else {
763 ir->active = 1;
764 ir->code = 0;
765 ir->base_time = tv;
766 ir->last_bit = 0;
767
768 timeout = current_jiffies + (500 + 30 * HZ) / 1000;
769 mod_timer(&ir->timer_end, timeout);
770 }
771
772 return 1;
773}
774
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -0300775
776/* On NEC protocol, One has 2.25 ms, and zero has 1.125 ms
777 The first pulse (start) has 9 + 4.5 ms
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 */
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -0300779
780static void saa7134_nec_timer(unsigned long data)
781{
782 struct saa7134_dev *dev = (struct saa7134_dev *) data;
783 struct card_ir *ir = dev->remote;
784
785 dprintk("Cancel key repeat\n");
786
787 ir_input_nokey(ir->dev, &ir->ir);
788}
789
790static void nec_task(unsigned long data)
791{
792 struct saa7134_dev *dev = (struct saa7134_dev *) data;
793 struct card_ir *ir;
794 struct timeval tv;
795 int count, pulse, oldpulse, gap;
796 u32 ircode = 0, not_code = 0;
797 int ngap = 0;
798
799 if (!data) {
800 printk(KERN_ERR "saa713x/ir: Can't recover dev struct\n");
801 /* GPIO will be kept disabled */
802 return;
803 }
804
805 ir = dev->remote;
806
807 /* rising SAA7134_GPIO_GPRESCAN reads the status */
808 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
809 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
810
811 oldpulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
812 pulse = oldpulse;
813
814 do_gettimeofday(&tv);
815 ir->base_time = tv;
816
817 /* Decode NEC pulsecode. This code can take up to 76.5 ms to run.
818 Unfortunately, using IRQ to decode pulse didn't work, since it uses
819 a pulse train of 38KHz. This means one pulse on each 52 us
820 */
821 do {
822 /* Wait until the end of pulse/space or 5 ms */
823 for (count = 0; count < 500; count++) {
824 udelay(10);
825 /* rising SAA7134_GPIO_GPRESCAN reads the status */
826 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
827 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
828 pulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2)
829 & ir->mask_keydown;
830 if (pulse != oldpulse)
831 break;
832 }
833
834 do_gettimeofday(&tv);
835 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
836 tv.tv_usec - ir->base_time.tv_usec;
837
838 if (!pulse) {
839 /* Bit 0 has 560 us, while bit 1 has 1120 us.
840 Do something only if bit == 1
841 */
842 if (ngap && (gap > 560 + 280)) {
843 unsigned int shift = ngap - 1;
844
845 /* Address first, then command */
846 if (shift < 8) {
847 shift += 8;
848 ircode |= 1 << shift;
849 } else if (shift < 16) {
850 not_code |= 1 << shift;
851 } else if (shift < 24) {
852 shift -= 16;
853 ircode |= 1 << shift;
854 } else {
855 shift -= 24;
856 not_code |= 1 << shift;
857 }
858 }
859 ngap++;
860 }
861
862
863 ir->base_time = tv;
864
865 /* TIMEOUT - Long pulse */
866 if (gap >= 5000)
867 break;
868 oldpulse = pulse;
869 } while (ngap < 32);
870
871 if (ngap == 32) {
872 /* FIXME: should check if not_code == ~ircode */
873 ir->code = ir_extract_bits(ircode, ir->mask_keycode);
874
875 dprintk("scancode = 0x%02x (code = 0x%02x, notcode= 0x%02x)\n",
876 ir->code, ircode, not_code);
877
878 ir_input_keydown(ir->dev, &ir->ir, ir->code, ir->code);
879 } else
880 dprintk("Repeat last key\n");
881
882 /* Keep repeating the last key */
883 mod_timer(&ir->timer_keyup, jiffies + msecs_to_jiffies(150));
884
885 saa_setl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
886}
887
888static int saa7134_nec_irq(struct saa7134_dev *dev)
889{
890 struct card_ir *ir = dev->remote;
891
892 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
893 tasklet_schedule(&ir->tlet);
894
895 return 1;
896}