blob: 32859e549afa0a0da4314c1a5a5b5201e5e60079 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#include "saa7134-reg.h"
29#include "saa7134.h"
30
Mauro Carvalho Chehab727e6252010-03-12 21:18:14 -030031#define MODULE_NAME "saa7134"
32
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030033static unsigned int disable_ir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034module_param(disable_ir, int, 0444);
35MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
36
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030037static unsigned int ir_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038module_param(ir_debug, int, 0644);
39MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
40
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030041static int pinnacle_remote;
Sylvain Pascheb93eedb2006-03-25 23:14:42 -030042module_param(pinnacle_remote, int, 0644); /* Choose Pinnacle PCTV remote */
43MODULE_PARM_DESC(pinnacle_remote, "Specify Pinnacle PCTV remote: 0=coloured, 1=grey (defaults to 0)");
44
Adrian Bunkc408a6f2006-12-28 12:47:47 -030045static int ir_rc5_remote_gap = 885;
Hermann Pitton91607232006-12-07 21:45:28 -030046module_param(ir_rc5_remote_gap, int, 0644);
Adrian Bunkc408a6f2006-12-28 12:47:47 -030047static int ir_rc5_key_timeout = 115;
Hermann Pitton91607232006-12-07 21:45:28 -030048module_param(ir_rc5_key_timeout, int, 0644);
49
Pedro0938e312007-10-17 17:58:40 -030050static int repeat_delay = 500;
51module_param(repeat_delay, int, 0644);
52MODULE_PARM_DESC(repeat_delay, "delay before key repeat started");
53static int repeat_period = 33;
54module_param(repeat_period, int, 0644);
Joe Perchesac9bb7f2007-11-20 09:00:35 -030055MODULE_PARM_DESC(repeat_period, "repeat period between "
Pedro0938e312007-10-17 17:58:40 -030056 "keypresses when key is down");
57
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -030058static unsigned int disable_other_ir;
59module_param(disable_other_ir, int, 0644);
60MODULE_PARM_DESC(disable_other_ir, "disable full codes of "
61 "alternative remotes from other manufacturers");
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define dprintk(fmt, arg...) if (ir_debug) \
64 printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -080065#define i2cdprintk(fmt, arg...) if (ir_debug) \
Jean Delvare1df8e982009-05-13 16:48:07 -030066 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -030068/* Helper functions for RC5 and NEC decoding at GPIO16 or GPIO18 */
Hermann Pitton91607232006-12-07 21:45:28 -030069static int saa7134_rc5_irq(struct saa7134_dev *dev);
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -030070static int saa7134_nec_irq(struct saa7134_dev *dev);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030071static int saa7134_raw_decode_irq(struct saa7134_dev *dev);
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -030072static void nec_task(unsigned long data);
73static void saa7134_nec_timer(unsigned long data);
Hermann Pitton91607232006-12-07 21:45:28 -030074
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -080075/* -------------------- GPIO generic keycode builder -------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77static int build_key(struct saa7134_dev *dev)
78{
Hermann Pitton91607232006-12-07 21:45:28 -030079 struct card_ir *ir = dev->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 u32 gpio, data;
81
Pedro0938e312007-10-17 17:58:40 -030082 /* here comes the additional handshake steps for some cards */
83 switch (dev->board) {
84 case SAA7134_BOARD_GOTVIEW_7135:
85 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x80);
86 saa_clearb(SAA7134_GPIO_GPSTATUS1, 0x80);
87 break;
88 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 /* rising SAA7134_GPIO_GPRESCAN reads the status */
90 saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
91 saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
92
93 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080094 if (ir->polling) {
95 if (ir->last_gpio == gpio)
96 return 0;
97 ir->last_gpio = gpio;
98 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800100 data = ir_extract_bits(gpio, ir->mask_keycode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
102 gpio, ir->mask_keycode, data);
103
Mauro Carvalho Chehab26d5f3a2008-12-07 13:19:29 -0300104 switch (dev->board) {
105 case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
106 if (data == ir->mask_keycode)
107 ir_input_nokey(ir->dev, &ir->ir);
108 else
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -0300109 ir_input_keydown(ir->dev, &ir->ir, data);
Mauro Carvalho Chehab26d5f3a2008-12-07 13:19:29 -0300110 return 0;
111 }
112
Peter Missel0602fbb2006-01-09 18:21:23 -0200113 if (ir->polling) {
114 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
115 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -0300116 ir_input_keydown(ir->dev, &ir->ir, data);
Peter Missel0602fbb2006-01-09 18:21:23 -0200117 } else {
118 ir_input_nokey(ir->dev, &ir->ir);
119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
Peter Missel0602fbb2006-01-09 18:21:23 -0200121 else { /* IRQ driven mode - handle key press and release in one go */
122 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
123 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -0300124 ir_input_keydown(ir->dev, &ir->ir, data);
Peter Missel0602fbb2006-01-09 18:21:23 -0200125 ir_input_nokey(ir->dev, &ir->ir);
126 }
127 }
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return 0;
130}
131
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800132/* --------------------- Chip specific I2C key builders ----------------- */
133
Lukas Karasd995a182009-11-24 12:06:52 -0300134static int get_key_flydvb_trio(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
135{
136 int gpio;
137 int attempt = 0;
138 unsigned char b;
139
140 /* We need this to access GPI Used by the saa_readl macro. */
141 struct saa7134_dev *dev = ir->c->adapter->algo_data;
142
143 if (dev == NULL) {
144 dprintk("get_key_flydvb_trio: "
145 "gir->c->adapter->algo_data is NULL!\n");
146 return -EIO;
147 }
148
149 /* rising SAA7134_GPIGPRESCAN reads the status */
150 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
151 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
152
153 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
154
155 if (0x40000 & ~gpio)
156 return 0; /* No button press */
157
158 /* No button press - only before first key pressed */
159 if (b == 0xFF)
160 return 0;
161
162 /* poll IR chip */
163 /* weak up the IR chip */
164 b = 0;
165
166 while (1 != i2c_master_send(ir->c, &b, 1)) {
167 if ((attempt++) < 10) {
168 /*
169 * wait a bit for next attempt -
170 * I don't know how make it better
171 */
172 msleep(10);
173 continue;
174 }
175 i2cdprintk("send wake up byte to pic16C505 (IR chip)"
176 "failed %dx\n", attempt);
177 return -EIO;
178 }
179 if (1 != i2c_master_recv(ir->c, &b, 1)) {
180 i2cdprintk("read error\n");
181 return -EIO;
182 }
183
184 *ir_key = b;
185 *ir_raw = b;
186 return 1;
187}
188
Brian Rogersba340b42008-10-13 08:37:06 -0300189static int get_key_msi_tvanywhere_plus(struct IR_i2c *ir, u32 *ir_key,
190 u32 *ir_raw)
191{
192 unsigned char b;
193 int gpio;
194
195 /* <dev> is needed to access GPIO. Used by the saa_readl macro. */
Jean Delvarec668f322009-05-13 16:48:50 -0300196 struct saa7134_dev *dev = ir->c->adapter->algo_data;
Brian Rogersba340b42008-10-13 08:37:06 -0300197 if (dev == NULL) {
198 dprintk("get_key_msi_tvanywhere_plus: "
Jean Delvarec668f322009-05-13 16:48:50 -0300199 "gir->c->adapter->algo_data is NULL!\n");
Brian Rogersba340b42008-10-13 08:37:06 -0300200 return -EIO;
201 }
202
203 /* rising SAA7134_GPIO_GPRESCAN reads the status */
204
205 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
206 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
207
208 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
209
210 /* GPIO&0x40 is pulsed low when a button is pressed. Don't do
211 I2C receive if gpio&0x40 is not low. */
212
213 if (gpio & 0x40)
214 return 0; /* No button press */
215
216 /* GPIO says there is a button press. Get it. */
217
Jean Delvarec668f322009-05-13 16:48:50 -0300218 if (1 != i2c_master_recv(ir->c, &b, 1)) {
Brian Rogersba340b42008-10-13 08:37:06 -0300219 i2cdprintk("read error\n");
220 return -EIO;
221 }
222
223 /* No button press */
224
225 if (b == 0xff)
226 return 0;
227
228 /* Button pressed */
229
230 dprintk("get_key_msi_tvanywhere_plus: Key = 0x%02X\n", b);
231 *ir_key = b;
232 *ir_raw = b;
233 return 1;
234}
235
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800236static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
237{
238 unsigned char b;
239
240 /* poll IR chip */
Jean Delvarec668f322009-05-13 16:48:50 -0300241 if (1 != i2c_master_recv(ir->c, &b, 1)) {
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800242 i2cdprintk("read error\n");
243 return -EIO;
244 }
245
246 /* no button press */
247 if (b==0)
248 return 0;
249
250 /* repeating */
251 if (b & 0x80)
252 return 1;
253
254 *ir_key = b;
255 *ir_raw = b;
256 return 1;
257}
258
Thomas Genty177aaaf2006-11-29 21:57:24 -0300259static int get_key_hvr1110(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
260{
261 unsigned char buf[5], cod4, code3, code4;
262
263 /* poll IR chip */
Jean Delvarec668f322009-05-13 16:48:50 -0300264 if (5 != i2c_master_recv(ir->c, buf, 5))
Thomas Genty177aaaf2006-11-29 21:57:24 -0300265 return -EIO;
266
267 cod4 = buf[4];
268 code4 = (cod4 >> 2);
269 code3 = buf[3];
270 if (code3 == 0)
271 /* no key pressed */
272 return 0;
273
274 /* return key */
275 *ir_key = code4;
276 *ir_raw = code4;
277 return 1;
278}
279
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300280
281static int get_key_beholdm6xx(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
282{
283 unsigned char data[12];
284 u32 gpio;
285
Jean Delvarec668f322009-05-13 16:48:50 -0300286 struct saa7134_dev *dev = ir->c->adapter->algo_data;
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300287
288 /* rising SAA7134_GPIO_GPRESCAN reads the status */
289 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
290 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
291
292 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
293
Mauro Carvalho Chehab616f8872008-01-07 05:18:36 -0300294 if (0x400000 & ~gpio)
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300295 return 0; /* No button press */
296
Jean Delvarec668f322009-05-13 16:48:50 -0300297 ir->c->addr = 0x5a >> 1;
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300298
Jean Delvarec668f322009-05-13 16:48:50 -0300299 if (12 != i2c_master_recv(ir->c, data, 12)) {
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300300 i2cdprintk("read error\n");
301 return -EIO;
302 }
303 /* IR of this card normally decode signals NEC-standard from
304 * - Sven IHOO MT 5.1R remote. xxyye718
305 * - Sven DVD HD-10xx remote. xxyyf708
306 * - BBK ...
307 * - mayby others
308 * So, skip not our, if disable full codes mode.
309 */
310 if (data[10] != 0x6b && data[11] != 0x86 && disable_other_ir)
311 return 0;
312
Dmitri Belimov2d21ffe2009-09-07 20:36:05 -0300313 /* Wrong data decode fix */
314 if (data[9] != (unsigned char)(~data[8]))
315 return 0;
316
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300317 *ir_key = data[9];
318 *ir_raw = data[9];
319
320 return 1;
321}
322
Mauro Carvalho Chehab1c22dad2008-07-17 22:31:29 -0300323/* Common (grey or coloured) pinnacle PCTV remote handling
324 *
325 */
326static int get_key_pinnacle(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw,
327 int parity_offset, int marker, int code_modulo)
328{
329 unsigned char b[4];
330 unsigned int start = 0,parity = 0,code = 0;
331
332 /* poll IR chip */
Jean Delvarec668f322009-05-13 16:48:50 -0300333 if (4 != i2c_master_recv(ir->c, b, 4)) {
Mauro Carvalho Chehab1c22dad2008-07-17 22:31:29 -0300334 i2cdprintk("read error\n");
335 return -EIO;
336 }
337
338 for (start = 0; start < ARRAY_SIZE(b); start++) {
339 if (b[start] == marker) {
340 code=b[(start+parity_offset + 1) % 4];
341 parity=b[(start+parity_offset) % 4];
342 }
343 }
344
345 /* Empty Request */
346 if (parity == 0)
347 return 0;
348
349 /* Repeating... */
350 if (ir->old == parity)
351 return 0;
352
353 ir->old = parity;
354
355 /* drop special codes when a key is held down a long time for the grey controller
356 In this case, the second bit of the code is asserted */
357 if (marker == 0xfe && (code & 0x40))
358 return 0;
359
360 code %= code_modulo;
361
362 *ir_raw = code;
363 *ir_key = code;
364
365 i2cdprintk("Pinnacle PCTV key %02x\n", code);
366
367 return 1;
368}
369
370/* The grey pinnacle PCTV remote
371 *
372 * There are one issue with this remote:
373 * - I2c packet does not change when the same key is pressed quickly. The workaround
374 * is to hold down each key for about half a second, so that another code is generated
375 * in the i2c packet, and the function can distinguish key presses.
376 *
377 * Sylvain Pasche <sylvain.pasche@gmail.com>
378 */
379static int get_key_pinnacle_grey(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
380{
381
382 return get_key_pinnacle(ir, ir_key, ir_raw, 1, 0xfe, 0xff);
383}
384
385
386/* The new pinnacle PCTV remote (with the colored buttons)
387 *
388 * Ricardo Cerqueira <v4l@cerqueira.org>
389 */
390static int get_key_pinnacle_color(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
391{
392 /* code_modulo parameter (0x88) is used to reduce code value to fit inside IR_KEYTAB_SIZE
393 *
394 * this is the only value that results in 42 unique
395 * codes < 128
396 */
397
398 return get_key_pinnacle(ir, ir_key, ir_raw, 2, 0x80, 0x88);
399}
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401void saa7134_input_irq(struct saa7134_dev *dev)
402{
Hermann Pitton91607232006-12-07 21:45:28 -0300403 struct card_ir *ir = dev->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -0300405 if (ir->nec_gpio) {
406 saa7134_nec_irq(dev);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300407 } else if (!ir->polling && !ir->rc5_gpio && !ir->raw_decode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 build_key(dev);
Hermann Pitton91607232006-12-07 21:45:28 -0300409 } else if (ir->rc5_gpio) {
410 saa7134_rc5_irq(dev);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300411 } else if (ir->raw_decode) {
412 saa7134_raw_decode_irq(dev);
Hermann Pitton91607232006-12-07 21:45:28 -0300413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
416static void saa7134_input_timer(unsigned long data)
417{
Dmitry Torokhovb4ba7882007-05-21 11:41:02 -0300418 struct saa7134_dev *dev = (struct saa7134_dev *)data;
Hermann Pitton91607232006-12-07 21:45:28 -0300419 struct card_ir *ir = dev->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 build_key(dev);
Dmitry Torokhovb4ba7882007-05-21 11:41:02 -0300422 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300425void ir_raw_decode_timer_end(unsigned long data)
426{
427 struct saa7134_dev *dev = (struct saa7134_dev *)data;
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -0300428 struct card_ir *ir = dev->remote;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300429
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300430 ir_raw_event_handle(dev->remote->dev);
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -0300431
432 ir->active = 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300433}
434
Maxim Levitskyc4584732007-10-12 00:57:15 -0300435void saa7134_ir_start(struct saa7134_dev *dev, struct card_ir *ir)
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300436{
Mauro Carvalho Chehab02108942010-03-20 00:25:37 -0300437 if (ir->running)
438 return;
439
440 ir->running = 1;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300441 if (ir->polling) {
Dmitry Torokhovb4ba7882007-05-21 11:41:02 -0300442 setup_timer(&ir->timer, saa7134_input_timer,
443 (unsigned long)dev);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300444 ir->timer.expires = jiffies + HZ;
445 add_timer(&ir->timer);
Hermann Pitton91607232006-12-07 21:45:28 -0300446 } else if (ir->rc5_gpio) {
447 /* set timer_end for code completion */
448 init_timer(&ir->timer_end);
449 ir->timer_end.function = ir_rc5_timer_end;
450 ir->timer_end.data = (unsigned long)ir;
451 init_timer(&ir->timer_keyup);
452 ir->timer_keyup.function = ir_rc5_timer_keyup;
453 ir->timer_keyup.data = (unsigned long)ir;
454 ir->shift_by = 2;
455 ir->start = 0x2;
456 ir->addr = 0x17;
457 ir->rc5_key_timeout = ir_rc5_key_timeout;
458 ir->rc5_remote_gap = ir_rc5_remote_gap;
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -0300459 } else if (ir->nec_gpio) {
460 setup_timer(&ir->timer_keyup, saa7134_nec_timer,
461 (unsigned long)dev);
462 tasklet_init(&ir->tlet, nec_task, (unsigned long)dev);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300463 } else if (ir->raw_decode) {
464 /* set timer_end for code completion */
465 init_timer(&ir->timer_end);
466 ir->timer_end.function = ir_raw_decode_timer_end;
467 ir->timer_end.data = (unsigned long)dev;
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -0300468 ir->active = 0;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300469 }
470}
471
Maxim Levitskyc4584732007-10-12 00:57:15 -0300472void saa7134_ir_stop(struct saa7134_dev *dev)
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300473{
Mauro Carvalho Chehab02108942010-03-20 00:25:37 -0300474 struct card_ir *ir = dev->remote;
475
476 if (!ir->running)
477 return;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300478 if (dev->remote->polling)
479 del_timer_sync(&dev->remote->timer);
Mauro Carvalho Chehab02108942010-03-20 00:25:37 -0300480 else if (ir->rc5_gpio)
481 del_timer_sync(&ir->timer_end);
482 else if (ir->nec_gpio)
483 tasklet_kill(&ir->tlet);
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -0300484 else if (ir->raw_decode) {
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300485 del_timer_sync(&ir->timer_end);
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -0300486 ir->active = 0;
487 }
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300488
Mauro Carvalho Chehab02108942010-03-20 00:25:37 -0300489 ir->running = 0;
490}
491
492int saa7134_ir_change_protocol(void *priv, u64 ir_type)
493{
494 struct saa7134_dev *dev = priv;
495 struct card_ir *ir = dev->remote;
496 u32 nec_gpio, rc5_gpio;
497
498 if (ir_type == IR_TYPE_RC5) {
499 dprintk("Changing protocol to RC5\n");
500 nec_gpio = 0;
501 rc5_gpio = 1;
502 } else if (ir_type == IR_TYPE_NEC) {
503 dprintk("Changing protocol to NEC\n");
504 nec_gpio = 1;
505 rc5_gpio = 0;
506 } else {
507 dprintk("IR protocol type %ud is not supported\n",
508 (unsigned)ir_type);
509 return -EINVAL;
510 }
511
512 if (ir->running) {
513 saa7134_ir_stop(dev);
514 ir->nec_gpio = nec_gpio;
515 ir->rc5_gpio = rc5_gpio;
516 saa7134_ir_start(dev, ir);
517 } else {
518 ir->nec_gpio = nec_gpio;
519 ir->rc5_gpio = rc5_gpio;
520 }
521
522 return 0;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300523}
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525int saa7134_input_init1(struct saa7134_dev *dev)
526{
Hermann Pitton91607232006-12-07 21:45:28 -0300527 struct card_ir *ir;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500528 struct input_dev *input_dev;
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300529 struct ir_scancode_table *ir_codes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 u32 mask_keycode = 0;
531 u32 mask_keydown = 0;
532 u32 mask_keyup = 0;
533 int polling = 0;
Hermann Pitton91607232006-12-07 21:45:28 -0300534 int rc5_gpio = 0;
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -0300535 int nec_gpio = 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300536 int raw_decode = 0;
Mauro Carvalho Chehab971e8292009-12-14 13:53:37 -0300537 u64 ir_type = IR_TYPE_OTHER;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300538 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Ricardo Cerqueiracb2444d2005-11-08 21:38:47 -0800540 if (dev->has_remote != SAA7134_REMOTE_GPIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return -ENODEV;
542 if (disable_ir)
543 return -ENODEV;
544
545 /* detect & configure */
546 switch (dev->board) {
547 case SAA7134_BOARD_FLYVIDEO2000:
548 case SAA7134_BOARD_FLYVIDEO3000:
Vladimir Ermakov8f27fd92010-03-10 12:44:57 -0300549 case SAA7134_BOARD_HAWELL_HW_404M7:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800550 case SAA7134_BOARD_FLYTVPLATINUM_FM:
Arnaud Patard6af90ab2005-11-08 21:36:55 -0800551 case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
Eugene Yudin23389b82009-08-29 09:32:11 -0300552 case SAA7134_BOARD_ROVERMEDIA_LINK_PRO_FM:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300553 ir_codes = &ir_codes_flyvideo_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 mask_keycode = 0xEC00000;
555 mask_keydown = 0x0040000;
556 break;
557 case SAA7134_BOARD_CINERGY400:
558 case SAA7134_BOARD_CINERGY600:
559 case SAA7134_BOARD_CINERGY600_MK3:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300560 ir_codes = &ir_codes_cinergy_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 mask_keycode = 0x00003f;
562 mask_keyup = 0x040000;
563 break;
564 case SAA7134_BOARD_ECS_TVP3XP:
565 case SAA7134_BOARD_ECS_TVP3XP_4CB5:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300566 ir_codes = &ir_codes_eztv_table;
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700567 mask_keycode = 0x00017c;
568 mask_keyup = 0x000002;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 polling = 50; // ms
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700570 break;
571 case SAA7134_BOARD_KWORLD_XPERT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 case SAA7134_BOARD_AVACSSMARTTV:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300573 ir_codes = &ir_codes_pixelview_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 mask_keycode = 0x00001F;
575 mask_keyup = 0x000020;
576 polling = 50; // ms
577 break;
578 case SAA7134_BOARD_MD2819:
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700579 case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 case SAA7134_BOARD_AVERMEDIA_305:
581 case SAA7134_BOARD_AVERMEDIA_307:
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700582 case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
Vasiliy Temnikov5a5e1da2009-08-26 22:10:55 -0300583 case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700584 case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
Mikhail Fedotov3ac706d2006-10-06 20:23:47 -0300585 case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
Andy Shevchenkodf0dbbe2009-04-08 14:01:19 -0300586 case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700587 case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
Albert Grahamd2761f22007-12-09 09:44:38 -0300588 case SAA7134_BOARD_AVERMEDIA_M102:
Pham Thanh Nam6a2d8022008-12-30 23:26:09 -0300589 case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300590 ir_codes = &ir_codes_avermedia_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 mask_keycode = 0x0007C8;
592 mask_keydown = 0x000010;
593 polling = 50; // ms
594 /* Set GPIO pin2 to high to enable the IR controller */
595 saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
596 saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
597 break;
Mauro Carvalho Chehab36f6bb92008-06-26 17:03:00 -0300598 case SAA7134_BOARD_AVERMEDIA_M135A:
Mauro Carvalho Chehabd152b8b2010-03-20 00:23:30 -0300599 ir_codes = &ir_codes_avermedia_m135a_rm_jx_table;
Mauro Carvalho Chehab36f6bb92008-06-26 17:03:00 -0300600 mask_keydown = 0x0040000;
Mauro Carvalho Chehabd152b8b2010-03-20 00:23:30 -0300601 mask_keycode = 0xffff;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300602 raw_decode = 1;
Mauro Carvalho Chehab36f6bb92008-06-26 17:03:00 -0300603 break;
pasky@ucw.cz450efcf2006-11-12 14:22:32 -0300604 case SAA7134_BOARD_AVERMEDIA_777:
pasky@ucw.cz29e0f1a2006-11-12 14:23:32 -0300605 case SAA7134_BOARD_AVERMEDIA_A16AR:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300606 ir_codes = &ir_codes_avermedia_table;
pasky@ucw.cz450efcf2006-11-12 14:22:32 -0300607 mask_keycode = 0x02F200;
608 mask_keydown = 0x000400;
609 polling = 50; // ms
610 /* Without this we won't receive key up events */
611 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
612 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
Linus Torvalds4dd74062006-11-13 09:50:11 -0800613 break;
Tim Farrington6e501a32008-06-15 13:33:42 -0300614 case SAA7134_BOARD_AVERMEDIA_A16D:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300615 ir_codes = &ir_codes_avermedia_a16d_table;
Tim Farrington6e501a32008-06-15 13:33:42 -0300616 mask_keycode = 0x02F200;
617 mask_keydown = 0x000400;
618 polling = 50; /* ms */
619 /* Without this we won't receive key up events */
620 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
621 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
622 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800623 case SAA7134_BOARD_KWORLD_TERMINATOR:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300624 ir_codes = &ir_codes_pixelview_table;
James R. Webbdc2286c2005-11-08 21:37:00 -0800625 mask_keycode = 0x00001f;
626 mask_keyup = 0x000060;
627 polling = 50; // ms
628 break;
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700629 case SAA7134_BOARD_MANLI_MTV001:
630 case SAA7134_BOARD_MANLI_MTV002:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300631 ir_codes = &ir_codes_manli_table;
Dmitry Belimovb34dddb2008-04-23 14:09:08 -0300632 mask_keycode = 0x001f00;
633 mask_keyup = 0x004000;
634 polling = 50; /* ms */
635 break;
Nickolay V. Shmyreva8ff4172005-11-08 21:36:16 -0800636 case SAA7134_BOARD_BEHOLD_409FM:
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300637 case SAA7134_BOARD_BEHOLD_401:
638 case SAA7134_BOARD_BEHOLD_403:
639 case SAA7134_BOARD_BEHOLD_403FM:
640 case SAA7134_BOARD_BEHOLD_405:
641 case SAA7134_BOARD_BEHOLD_405FM:
642 case SAA7134_BOARD_BEHOLD_407:
643 case SAA7134_BOARD_BEHOLD_407FM:
644 case SAA7134_BOARD_BEHOLD_409:
645 case SAA7134_BOARD_BEHOLD_505FM:
Dmitri Belimov9c6f97a2009-12-21 02:00:38 -0300646 case SAA7134_BOARD_BEHOLD_505RDS_MK5:
647 case SAA7134_BOARD_BEHOLD_505RDS_MK3:
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300648 case SAA7134_BOARD_BEHOLD_507_9FM:
Dmitri Belimov84d728c2009-04-23 02:32:49 -0300649 case SAA7134_BOARD_BEHOLD_507RDS_MK3:
650 case SAA7134_BOARD_BEHOLD_507RDS_MK5:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300651 ir_codes = &ir_codes_manli_table;
Dmitry Belimovb34dddb2008-04-23 14:09:08 -0300652 mask_keycode = 0x003f00;
653 mask_keyup = 0x004000;
654 polling = 50; /* ms */
655 break;
656 case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300657 ir_codes = &ir_codes_behold_columbus_table;
Dmitry Belimovb34dddb2008-04-23 14:09:08 -0300658 mask_keycode = 0x003f00;
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700659 mask_keyup = 0x004000;
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700660 polling = 50; // ms
661 break;
Ricardo Cerqueira17ce1ff2005-11-08 21:38:47 -0800662 case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300663 ir_codes = &ir_codes_pctv_sedna_table;
Pavel Mihaylovc3d93192005-11-08 21:38:43 -0800664 mask_keycode = 0x001f00;
665 mask_keyup = 0x004000;
666 polling = 50; // ms
667 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800668 case SAA7134_BOARD_GOTVIEW_7135:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300669 ir_codes = &ir_codes_gotview7135_table;
Pedro0938e312007-10-17 17:58:40 -0300670 mask_keycode = 0x0003CC;
Nickolay V. Shmyrev6b961442005-11-08 21:36:22 -0800671 mask_keydown = 0x000010;
Pedro0938e312007-10-17 17:58:40 -0300672 polling = 5; /* ms */
673 saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
Nickolay V. Shmyrev6b961442005-11-08 21:36:22 -0800674 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 case SAA7134_BOARD_VIDEOMATE_TV_PVR:
Nickolay V. Shmyrev2a9a9a82006-01-09 15:25:33 -0200676 case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700677 case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300678 ir_codes = &ir_codes_videomate_tv_pvr_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 mask_keycode = 0x00003F;
680 mask_keyup = 0x400000;
681 polling = 50; // ms
682 break;
Michal Majchrowiczb04c1ba2006-09-13 16:42:42 -0300683 case SAA7134_BOARD_PROTEUS_2309:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300684 ir_codes = &ir_codes_proteus_2309_table;
Michal Majchrowiczb04c1ba2006-09-13 16:42:42 -0300685 mask_keycode = 0x00007F;
686 mask_keyup = 0x000080;
687 polling = 50; // ms
688 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800689 case SAA7134_BOARD_VIDEOMATE_DVBT_300:
690 case SAA7134_BOARD_VIDEOMATE_DVBT_200:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300691 ir_codes = &ir_codes_videomate_tv_pvr_table;
Nickolay V. Shmyrevfea095f2005-11-08 21:36:47 -0800692 mask_keycode = 0x003F00;
693 mask_keyup = 0x040000;
694 break;
James Le Cuirotf5c965a2007-07-02 12:53:25 -0300695 case SAA7134_BOARD_FLYDVBS_LR300:
Giampiero Giancipoli3d8466e2006-02-07 06:49:09 -0200696 case SAA7134_BOARD_FLYDVBT_LR301:
Rudo Thomasa8029172006-02-27 00:08:46 -0300697 case SAA7134_BOARD_FLYDVBTDUO:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300698 ir_codes = &ir_codes_flydvb_table;
Giampiero Giancipoli3d8466e2006-02-07 06:49:09 -0200699 mask_keycode = 0x0001F00;
700 mask_keydown = 0x0040000;
701 break;
Hermann Pitton91607232006-12-07 21:45:28 -0300702 case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
Ed Vipas904ab882007-03-29 18:32:49 -0300703 case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
Mauro Carvalho Chehabdfb4ba12009-08-20 10:13:40 -0300704 case SAA7134_BOARD_ASUSTeK_P7131_ANALOG:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300705 ir_codes = &ir_codes_asus_pc39_table;
Hermann Pitton91607232006-12-07 21:45:28 -0300706 mask_keydown = 0x0040000;
707 rc5_gpio = 1;
708 break;
Juan Pablo Sormanic36c4592006-12-27 12:46:36 -0300709 case SAA7134_BOARD_ENCORE_ENLTV:
710 case SAA7134_BOARD_ENCORE_ENLTV_FM:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300711 ir_codes = &ir_codes_encore_enltv_table;
Juan Pablo Sormanic36c4592006-12-27 12:46:36 -0300712 mask_keycode = 0x00007f;
713 mask_keyup = 0x040000;
714 polling = 50; // ms
715 break;
Mauro Carvalho Chehabbf1ece62008-06-26 17:03:00 -0300716 case SAA7134_BOARD_ENCORE_ENLTV_FM53:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300717 ir_codes = &ir_codes_encore_enltv_fm53_table;
Mauro Carvalho Chehabbf1ece62008-06-26 17:03:00 -0300718 mask_keydown = 0x0040000;
719 mask_keycode = 0x00007f;
720 nec_gpio = 1;
721 break;
Tony Wan480f75a2007-05-11 11:33:50 -0300722 case SAA7134_BOARD_10MOONSTVMASTER3:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300723 ir_codes = &ir_codes_encore_enltv_table;
Tony Wan480f75a2007-05-11 11:33:50 -0300724 mask_keycode = 0x5f80000;
725 mask_keyup = 0x8000000;
726 polling = 50; //ms
727 break;
Adrian Pardinif0ba3562008-02-11 12:40:53 -0300728 case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300729 ir_codes = &ir_codes_genius_tvgo_a11mce_table;
Adrian Pardinif0ba3562008-02-11 12:40:53 -0300730 mask_keycode = 0xff;
731 mask_keydown = 0xf00000;
732 polling = 50; /* ms */
733 break;
Mauro Carvalho Chehab9b000192008-06-26 17:03:00 -0300734 case SAA7134_BOARD_REAL_ANGEL_220:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300735 ir_codes = &ir_codes_real_audio_220_32_keys_table;
Mauro Carvalho Chehab9b000192008-06-26 17:03:00 -0300736 mask_keycode = 0x3f00;
737 mask_keyup = 0x4000;
738 polling = 50; /* ms */
739 break;
Mauro Carvalho Chehab26d5f3a2008-12-07 13:19:29 -0300740 case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300741 ir_codes = &ir_codes_kworld_plus_tv_analog_table;
Mauro Carvalho Chehab26d5f3a2008-12-07 13:19:29 -0300742 mask_keycode = 0x7f;
743 polling = 40; /* ms */
744 break;
Igor M. Liplianinecfcfec2009-08-13 21:42:21 -0300745 case SAA7134_BOARD_VIDEOMATE_S350:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300746 ir_codes = &ir_codes_videomate_s350_table;
Igor M. Liplianinecfcfec2009-08-13 21:42:21 -0300747 mask_keycode = 0x003f00;
748 mask_keydown = 0x040000;
749 break;
Michael Obst0700ade2009-10-31 14:05:42 -0300750 case SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S:
751 ir_codes = &ir_codes_winfast_table;
752 mask_keycode = 0x5f00;
753 mask_keyup = 0x020000;
Michael Krufky117e1342009-11-01 11:16:10 -0300754 polling = 50; /* ms */
Michael Obst0700ade2009-10-31 14:05:42 -0300755 break;
Lukas Karasd995a182009-11-24 12:06:52 -0300756 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758 if (NULL == ir_codes) {
759 printk("%s: Oops: IR config error [card=%d]\n",
760 dev->name, dev->board);
761 return -ENODEV;
762 }
763
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500764 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
765 input_dev = input_allocate_device();
766 if (!ir || !input_dev) {
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300767 err = -ENOMEM;
768 goto err_out_free;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Dmitry Torokhovd271d1c2005-11-20 00:56:54 -0500771 ir->dev = input_dev;
Mauro Carvalho Chehab02108942010-03-20 00:25:37 -0300772 dev->remote = ir;
773
774 ir->running = 0;
Dmitry Torokhovd271d1c2005-11-20 00:56:54 -0500775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /* init hardware-specific stuff */
777 ir->mask_keycode = mask_keycode;
778 ir->mask_keydown = mask_keydown;
779 ir->mask_keyup = mask_keyup;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800780 ir->polling = polling;
Hermann Pitton91607232006-12-07 21:45:28 -0300781 ir->rc5_gpio = rc5_gpio;
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -0300782 ir->nec_gpio = nec_gpio;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300783 ir->raw_decode = raw_decode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 /* init input device */
786 snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
787 saa7134_boards[dev->board].name);
788 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
789 pci_name(dev->pci));
790
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300791 if (ir_codes->ir_type != IR_TYPE_OTHER && !raw_decode) {
Mauro Carvalho Chehab02108942010-03-20 00:25:37 -0300792 ir->props.allowed_protos = IR_TYPE_RC5 | IR_TYPE_NEC;
793 ir->props.priv = dev;
794 ir->props.change_protocol = saa7134_ir_change_protocol;
795
796 /* Set IR protocol */
797 saa7134_ir_change_protocol(ir->props.priv, ir_codes->ir_type);
798 }
Mauro Carvalho Chehab579e7d62009-12-11 11:20:59 -0300799 err = ir_input_init(input_dev, &ir->ir, ir_type);
Mauro Carvalho Chehab055cd552009-11-29 08:19:59 -0300800 if (err < 0)
801 goto err_out_free;
802
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500803 input_dev->name = ir->name;
804 input_dev->phys = ir->phys;
805 input_dev->id.bustype = BUS_PCI;
806 input_dev->id.version = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (dev->pci->subsystem_vendor) {
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500808 input_dev->id.vendor = dev->pci->subsystem_vendor;
809 input_dev->id.product = dev->pci->subsystem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 } else {
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500811 input_dev->id.vendor = dev->pci->vendor;
812 input_dev->id.product = dev->pci->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
Dmitry Torokhov2c8a3a32007-07-16 09:28:15 -0300814 input_dev->dev.parent = &dev->pci->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Mauro Carvalho Chehab02108942010-03-20 00:25:37 -0300816 err = ir_input_register(ir->dev, ir_codes, &ir->props, MODULE_NAME);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300817 if (err)
818 goto err_out_stop;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300819 if (ir_codes->ir_type != IR_TYPE_OTHER) {
820 err = ir_raw_event_register(ir->dev);
821 if (err)
822 goto err_out_stop;
823 }
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300824
Mauro Carvalho Chehab02108942010-03-20 00:25:37 -0300825 saa7134_ir_start(dev, ir);
826
Pedro0938e312007-10-17 17:58:40 -0300827 /* the remote isn't as bouncy as a keyboard */
828 ir->dev->rep[REP_DELAY] = repeat_delay;
829 ir->dev->rep[REP_PERIOD] = repeat_period;
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return 0;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300832
833 err_out_stop:
834 saa7134_ir_stop(dev);
835 dev->remote = NULL;
836 err_out_free:
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300837 kfree(ir);
838 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
841void saa7134_input_fini(struct saa7134_dev *dev)
842{
843 if (NULL == dev->remote)
844 return;
845
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300846 saa7134_ir_stop(dev);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300847 ir_raw_event_unregister(dev->remote->dev);
Mauro Carvalho Chehab38ef6aa2009-12-11 09:47:42 -0300848 ir_input_unregister(dev->remote->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 kfree(dev->remote);
850 dev->remote = NULL;
851}
852
Jean Delvarec668f322009-05-13 16:48:50 -0300853void saa7134_probe_i2c_ir(struct saa7134_dev *dev)
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800854{
Jean Delvare43e16ea2009-10-02 05:47:08 -0300855 struct i2c_board_info info;
Jean Delvarec668f322009-05-13 16:48:50 -0300856
Jean Delvarec668f322009-05-13 16:48:50 -0300857 struct i2c_msg msg_msi = {
858 .addr = 0x50,
859 .flags = I2C_M_RD,
860 .len = 0,
861 .buf = NULL,
862 };
863
Jean Delvarec668f322009-05-13 16:48:50 -0300864 int rc;
865
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800866 if (disable_ir) {
Jean Delvarec668f322009-05-13 16:48:50 -0300867 dprintk("IR has been disabled, not probing for i2c remote\n");
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800868 return;
869 }
870
Jean Delvare43e16ea2009-10-02 05:47:08 -0300871 memset(&info, 0, sizeof(struct i2c_board_info));
Mauro Carvalho Chehab7aedd5ec2009-09-07 02:22:01 -0300872 memset(&dev->init_data, 0, sizeof(dev->init_data));
Jean Delvare43e16ea2009-10-02 05:47:08 -0300873 strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300874
875 switch (dev->board) {
876 case SAA7134_BOARD_PINNACLE_PCTV_110i:
877 case SAA7134_BOARD_PINNACLE_PCTV_310i:
Mauro Carvalho Chehab7aedd5ec2009-09-07 02:22:01 -0300878 dev->init_data.name = "Pinnacle PCTV";
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300879 if (pinnacle_remote == 0) {
Mauro Carvalho Chehab7aedd5ec2009-09-07 02:22:01 -0300880 dev->init_data.get_key = get_key_pinnacle_color;
881 dev->init_data.ir_codes = &ir_codes_pinnacle_color_table;
Jean Delvare43e16ea2009-10-02 05:47:08 -0300882 info.addr = 0x47;
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300883 } else {
Mauro Carvalho Chehab7aedd5ec2009-09-07 02:22:01 -0300884 dev->init_data.get_key = get_key_pinnacle_grey;
885 dev->init_data.ir_codes = &ir_codes_pinnacle_grey_table;
Jean Delvare43e16ea2009-10-02 05:47:08 -0300886 info.addr = 0x47;
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300887 }
888 break;
889 case SAA7134_BOARD_UPMOST_PURPLE_TV:
Mauro Carvalho Chehab7aedd5ec2009-09-07 02:22:01 -0300890 dev->init_data.name = "Purple TV";
891 dev->init_data.get_key = get_key_purpletv;
892 dev->init_data.ir_codes = &ir_codes_purpletv_table;
Jean Delvareaef02aa2009-10-02 08:47:22 -0300893 info.addr = 0x7a;
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300894 break;
895 case SAA7134_BOARD_MSI_TVATANYWHERE_PLUS:
Mauro Carvalho Chehab7aedd5ec2009-09-07 02:22:01 -0300896 dev->init_data.name = "MSI TV@nywhere Plus";
897 dev->init_data.get_key = get_key_msi_tvanywhere_plus;
898 dev->init_data.ir_codes = &ir_codes_msi_tvanywhere_plus_table;
Jean Delvare43e16ea2009-10-02 05:47:08 -0300899 info.addr = 0x30;
Jean Delvareec218a42009-05-13 16:51:46 -0300900 /* MSI TV@nywhere Plus controller doesn't seem to
901 respond to probes unless we read something from
902 an existing device. Weird...
903 REVISIT: might no longer be needed */
904 rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
905 dprintk(KERN_DEBUG "probe 0x%02x @ %s: %s\n",
906 msg_msi.addr, dev->i2c_adap.name,
907 (1 == rc) ? "yes" : "no");
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300908 break;
909 case SAA7134_BOARD_HAUPPAUGE_HVR1110:
Mauro Carvalho Chehab7aedd5ec2009-09-07 02:22:01 -0300910 dev->init_data.name = "HVR 1110";
911 dev->init_data.get_key = get_key_hvr1110;
912 dev->init_data.ir_codes = &ir_codes_hauppauge_new_table;
Jean Delvare30093e82009-10-02 09:48:04 -0300913 info.addr = 0x71;
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300914 break;
915 case SAA7134_BOARD_BEHOLD_607FM_MK3:
916 case SAA7134_BOARD_BEHOLD_607FM_MK5:
917 case SAA7134_BOARD_BEHOLD_609FM_MK3:
918 case SAA7134_BOARD_BEHOLD_609FM_MK5:
919 case SAA7134_BOARD_BEHOLD_607RDS_MK3:
920 case SAA7134_BOARD_BEHOLD_607RDS_MK5:
921 case SAA7134_BOARD_BEHOLD_609RDS_MK3:
922 case SAA7134_BOARD_BEHOLD_609RDS_MK5:
923 case SAA7134_BOARD_BEHOLD_M6:
924 case SAA7134_BOARD_BEHOLD_M63:
925 case SAA7134_BOARD_BEHOLD_M6_EXTRA:
926 case SAA7134_BOARD_BEHOLD_H6:
Dmitri Belimov2012c872009-08-26 01:01:12 -0300927 case SAA7134_BOARD_BEHOLD_X7:
Mauro Carvalho Chehab7aedd5ec2009-09-07 02:22:01 -0300928 dev->init_data.name = "BeholdTV";
929 dev->init_data.get_key = get_key_beholdm6xx;
930 dev->init_data.ir_codes = &ir_codes_behold_table;
Dmitri Belimov2ffbb642010-03-04 02:40:37 -0300931 dev->init_data.type = IR_TYPE_NEC;
Jean Delvare30093e82009-10-02 09:48:04 -0300932 info.addr = 0x2d;
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300933 break;
Jean Delvared9a88e62009-05-13 16:52:44 -0300934 case SAA7134_BOARD_AVERMEDIA_CARDBUS_501:
935 case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
Jean Delvare43e16ea2009-10-02 05:47:08 -0300936 info.addr = 0x40;
Jean Delvared9a88e62009-05-13 16:52:44 -0300937 break;
Lukas Karasd995a182009-11-24 12:06:52 -0300938 case SAA7134_BOARD_FLYDVB_TRIO:
939 dev->init_data.name = "FlyDVB Trio";
940 dev->init_data.get_key = get_key_flydvb_trio;
941 dev->init_data.ir_codes = &ir_codes_flydvb_table;
942 info.addr = 0x0b;
943 break;
Jean Delvare30093e82009-10-02 09:48:04 -0300944 default:
945 dprintk("No I2C IR support for board %x\n", dev->board);
946 return;
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300947 }
948
Mauro Carvalho Chehab7aedd5ec2009-09-07 02:22:01 -0300949 if (dev->init_data.name)
Jean Delvare43e16ea2009-10-02 05:47:08 -0300950 info.platform_data = &dev->init_data;
Jean Delvare30093e82009-10-02 09:48:04 -0300951 i2c_new_device(&dev->i2c_adap, &info);
Jean Delvarec668f322009-05-13 16:48:50 -0300952}
953
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300954static int saa7134_raw_decode_irq(struct saa7134_dev *dev)
955{
956 struct card_ir *ir = dev->remote;
957 unsigned long timeout;
958 int count, pulse, oldpulse;
959
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300960 /* Generate initial event */
961 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
962 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
963 pulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
964 ir_raw_event_store(dev->remote->dev, pulse? IR_PULSE : IR_SPACE);
965
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -0300966
967 /*
968 * Wait 15 ms from the start of the first IR event before processing
969 * the event. This time is enough for NEC protocol. May need adjustments
970 * to work with other protocols.
971 */
972 if (!ir->active) {
973 timeout = jiffies + jiffies_to_msecs(15);
974 mod_timer(&ir->timer_end, timeout);
975 ir->active = 1;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300976 }
977
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300978 return 1;
979}
980
Hermann Pitton91607232006-12-07 21:45:28 -0300981static int saa7134_rc5_irq(struct saa7134_dev *dev)
982{
983 struct card_ir *ir = dev->remote;
984 struct timeval tv;
985 u32 gap;
986 unsigned long current_jiffies, timeout;
987
988 /* get time of bit */
989 current_jiffies = jiffies;
990 do_gettimeofday(&tv);
991
992 /* avoid overflow with gap >1s */
993 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
994 gap = 200000;
995 } else {
996 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
997 tv.tv_usec - ir->base_time.tv_usec;
998 }
999
1000 /* active code => add bit */
1001 if (ir->active) {
1002 /* only if in the code (otherwise spurious IRQ or timer
1003 late) */
1004 if (ir->last_bit < 28) {
1005 ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
1006 ir_rc5_remote_gap;
1007 ir->code |= 1 << ir->last_bit;
1008 }
1009 /* starting new code */
1010 } else {
1011 ir->active = 1;
1012 ir->code = 0;
1013 ir->base_time = tv;
1014 ir->last_bit = 0;
1015
1016 timeout = current_jiffies + (500 + 30 * HZ) / 1000;
1017 mod_timer(&ir->timer_end, timeout);
1018 }
1019
1020 return 1;
1021}
1022
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -03001023/* On NEC protocol, One has 2.25 ms, and zero has 1.125 ms
1024 The first pulse (start) has 9 + 4.5 ms
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 */
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -03001026
1027static void saa7134_nec_timer(unsigned long data)
1028{
1029 struct saa7134_dev *dev = (struct saa7134_dev *) data;
1030 struct card_ir *ir = dev->remote;
1031
1032 dprintk("Cancel key repeat\n");
1033
1034 ir_input_nokey(ir->dev, &ir->ir);
1035}
1036
1037static void nec_task(unsigned long data)
1038{
1039 struct saa7134_dev *dev = (struct saa7134_dev *) data;
1040 struct card_ir *ir;
1041 struct timeval tv;
1042 int count, pulse, oldpulse, gap;
1043 u32 ircode = 0, not_code = 0;
1044 int ngap = 0;
1045
1046 if (!data) {
1047 printk(KERN_ERR "saa713x/ir: Can't recover dev struct\n");
1048 /* GPIO will be kept disabled */
1049 return;
1050 }
1051
1052 ir = dev->remote;
1053
1054 /* rising SAA7134_GPIO_GPRESCAN reads the status */
1055 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1056 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1057
1058 oldpulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
1059 pulse = oldpulse;
1060
1061 do_gettimeofday(&tv);
1062 ir->base_time = tv;
1063
1064 /* Decode NEC pulsecode. This code can take up to 76.5 ms to run.
1065 Unfortunately, using IRQ to decode pulse didn't work, since it uses
1066 a pulse train of 38KHz. This means one pulse on each 52 us
1067 */
1068 do {
1069 /* Wait until the end of pulse/space or 5 ms */
1070 for (count = 0; count < 500; count++) {
1071 udelay(10);
1072 /* rising SAA7134_GPIO_GPRESCAN reads the status */
1073 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1074 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1075 pulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2)
1076 & ir->mask_keydown;
1077 if (pulse != oldpulse)
1078 break;
1079 }
1080
1081 do_gettimeofday(&tv);
1082 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
1083 tv.tv_usec - ir->base_time.tv_usec;
1084
1085 if (!pulse) {
1086 /* Bit 0 has 560 us, while bit 1 has 1120 us.
1087 Do something only if bit == 1
1088 */
1089 if (ngap && (gap > 560 + 280)) {
1090 unsigned int shift = ngap - 1;
1091
1092 /* Address first, then command */
1093 if (shift < 8) {
1094 shift += 8;
1095 ircode |= 1 << shift;
1096 } else if (shift < 16) {
1097 not_code |= 1 << shift;
1098 } else if (shift < 24) {
1099 shift -= 16;
1100 ircode |= 1 << shift;
1101 } else {
1102 shift -= 24;
1103 not_code |= 1 << shift;
1104 }
1105 }
1106 ngap++;
1107 }
1108
1109
1110 ir->base_time = tv;
1111
1112 /* TIMEOUT - Long pulse */
1113 if (gap >= 5000)
1114 break;
1115 oldpulse = pulse;
1116 } while (ngap < 32);
1117
1118 if (ngap == 32) {
1119 /* FIXME: should check if not_code == ~ircode */
1120 ir->code = ir_extract_bits(ircode, ir->mask_keycode);
1121
1122 dprintk("scancode = 0x%02x (code = 0x%02x, notcode= 0x%02x)\n",
1123 ir->code, ircode, not_code);
1124
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -03001125 ir_input_keydown(ir->dev, &ir->ir, ir->code);
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -03001126 } else
1127 dprintk("Repeat last key\n");
1128
1129 /* Keep repeating the last key */
1130 mod_timer(&ir->timer_keyup, jiffies + msecs_to_jiffies(150));
1131
1132 saa_setl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
1133}
1134
1135static int saa7134_nec_irq(struct saa7134_dev *dev)
1136{
1137 struct card_ir *ir = dev->remote;
1138
1139 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
1140 tasklet_schedule(&ir->tlet);
1141
1142 return 1;
1143}