blob: 58a0cdc8414a36a654e8d2e067fd0821e89624ec [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
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030031static unsigned int disable_ir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032module_param(disable_ir, int, 0444);
33MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
34
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030035static unsigned int ir_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036module_param(ir_debug, int, 0644);
37MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
38
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030039static int pinnacle_remote;
Sylvain Pascheb93eedb2006-03-25 23:14:42 -030040module_param(pinnacle_remote, int, 0644); /* Choose Pinnacle PCTV remote */
41MODULE_PARM_DESC(pinnacle_remote, "Specify Pinnacle PCTV remote: 0=coloured, 1=grey (defaults to 0)");
42
Adrian Bunkc408a6f2006-12-28 12:47:47 -030043static int ir_rc5_remote_gap = 885;
Hermann Pitton91607232006-12-07 21:45:28 -030044module_param(ir_rc5_remote_gap, int, 0644);
Adrian Bunkc408a6f2006-12-28 12:47:47 -030045static int ir_rc5_key_timeout = 115;
Hermann Pitton91607232006-12-07 21:45:28 -030046module_param(ir_rc5_key_timeout, int, 0644);
47
Pedro0938e312007-10-17 17:58:40 -030048static int repeat_delay = 500;
49module_param(repeat_delay, int, 0644);
50MODULE_PARM_DESC(repeat_delay, "delay before key repeat started");
51static int repeat_period = 33;
52module_param(repeat_period, int, 0644);
Joe Perchesac9bb7f2007-11-20 09:00:35 -030053MODULE_PARM_DESC(repeat_period, "repeat period between "
Pedro0938e312007-10-17 17:58:40 -030054 "keypresses when key is down");
55
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -030056static unsigned int disable_other_ir;
57module_param(disable_other_ir, int, 0644);
58MODULE_PARM_DESC(disable_other_ir, "disable full codes of "
59 "alternative remotes from other manufacturers");
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define dprintk(fmt, arg...) if (ir_debug) \
62 printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -080063#define i2cdprintk(fmt, arg...) if (ir_debug) \
Jean Delvare1df8e982009-05-13 16:48:07 -030064 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -030066/* Helper functions for RC5 and NEC decoding at GPIO16 or GPIO18 */
Hermann Pitton91607232006-12-07 21:45:28 -030067static int saa7134_rc5_irq(struct saa7134_dev *dev);
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -030068static int saa7134_nec_irq(struct saa7134_dev *dev);
69static void nec_task(unsigned long data);
70static void saa7134_nec_timer(unsigned long data);
Hermann Pitton91607232006-12-07 21:45:28 -030071
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -080072/* -------------------- GPIO generic keycode builder -------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static int build_key(struct saa7134_dev *dev)
75{
Hermann Pitton91607232006-12-07 21:45:28 -030076 struct card_ir *ir = dev->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 u32 gpio, data;
78
Pedro0938e312007-10-17 17:58:40 -030079 /* here comes the additional handshake steps for some cards */
80 switch (dev->board) {
81 case SAA7134_BOARD_GOTVIEW_7135:
82 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x80);
83 saa_clearb(SAA7134_GPIO_GPSTATUS1, 0x80);
84 break;
85 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 /* rising SAA7134_GPIO_GPRESCAN reads the status */
87 saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
88 saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
89
90 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080091 if (ir->polling) {
92 if (ir->last_gpio == gpio)
93 return 0;
94 ir->last_gpio = gpio;
95 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080097 data = ir_extract_bits(gpio, ir->mask_keycode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
99 gpio, ir->mask_keycode, data);
100
Mauro Carvalho Chehab26d5f3a2008-12-07 13:19:29 -0300101 switch (dev->board) {
102 case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
103 if (data == ir->mask_keycode)
104 ir_input_nokey(ir->dev, &ir->ir);
105 else
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -0300106 ir_input_keydown(ir->dev, &ir->ir, data);
Mauro Carvalho Chehab26d5f3a2008-12-07 13:19:29 -0300107 return 0;
108 }
109
Peter Missel0602fbb2006-01-09 18:21:23 -0200110 if (ir->polling) {
111 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
112 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -0300113 ir_input_keydown(ir->dev, &ir->ir, data);
Peter Missel0602fbb2006-01-09 18:21:23 -0200114 } else {
115 ir_input_nokey(ir->dev, &ir->ir);
116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
Peter Missel0602fbb2006-01-09 18:21:23 -0200118 else { /* IRQ driven mode - handle key press and release in one go */
119 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
120 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -0300121 ir_input_keydown(ir->dev, &ir->ir, data);
Peter Missel0602fbb2006-01-09 18:21:23 -0200122 ir_input_nokey(ir->dev, &ir->ir);
123 }
124 }
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return 0;
127}
128
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800129/* --------------------- Chip specific I2C key builders ----------------- */
130
Lukas Karasd995a182009-11-24 12:06:52 -0300131static int get_key_flydvb_trio(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
132{
133 int gpio;
134 int attempt = 0;
135 unsigned char b;
136
137 /* We need this to access GPI Used by the saa_readl macro. */
138 struct saa7134_dev *dev = ir->c->adapter->algo_data;
139
140 if (dev == NULL) {
141 dprintk("get_key_flydvb_trio: "
142 "gir->c->adapter->algo_data is NULL!\n");
143 return -EIO;
144 }
145
146 /* rising SAA7134_GPIGPRESCAN reads the status */
147 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
148 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
149
150 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
151
152 if (0x40000 & ~gpio)
153 return 0; /* No button press */
154
155 /* No button press - only before first key pressed */
156 if (b == 0xFF)
157 return 0;
158
159 /* poll IR chip */
160 /* weak up the IR chip */
161 b = 0;
162
163 while (1 != i2c_master_send(ir->c, &b, 1)) {
164 if ((attempt++) < 10) {
165 /*
166 * wait a bit for next attempt -
167 * I don't know how make it better
168 */
169 msleep(10);
170 continue;
171 }
172 i2cdprintk("send wake up byte to pic16C505 (IR chip)"
173 "failed %dx\n", attempt);
174 return -EIO;
175 }
176 if (1 != i2c_master_recv(ir->c, &b, 1)) {
177 i2cdprintk("read error\n");
178 return -EIO;
179 }
180
181 *ir_key = b;
182 *ir_raw = b;
183 return 1;
184}
185
Brian Rogersba340b42008-10-13 08:37:06 -0300186static int get_key_msi_tvanywhere_plus(struct IR_i2c *ir, u32 *ir_key,
187 u32 *ir_raw)
188{
189 unsigned char b;
190 int gpio;
191
192 /* <dev> is needed to access GPIO. Used by the saa_readl macro. */
Jean Delvarec668f322009-05-13 16:48:50 -0300193 struct saa7134_dev *dev = ir->c->adapter->algo_data;
Brian Rogersba340b42008-10-13 08:37:06 -0300194 if (dev == NULL) {
195 dprintk("get_key_msi_tvanywhere_plus: "
Jean Delvarec668f322009-05-13 16:48:50 -0300196 "gir->c->adapter->algo_data is NULL!\n");
Brian Rogersba340b42008-10-13 08:37:06 -0300197 return -EIO;
198 }
199
200 /* rising SAA7134_GPIO_GPRESCAN reads the status */
201
202 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
203 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
204
205 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
206
207 /* GPIO&0x40 is pulsed low when a button is pressed. Don't do
208 I2C receive if gpio&0x40 is not low. */
209
210 if (gpio & 0x40)
211 return 0; /* No button press */
212
213 /* GPIO says there is a button press. Get it. */
214
Jean Delvarec668f322009-05-13 16:48:50 -0300215 if (1 != i2c_master_recv(ir->c, &b, 1)) {
Brian Rogersba340b42008-10-13 08:37:06 -0300216 i2cdprintk("read error\n");
217 return -EIO;
218 }
219
220 /* No button press */
221
222 if (b == 0xff)
223 return 0;
224
225 /* Button pressed */
226
227 dprintk("get_key_msi_tvanywhere_plus: Key = 0x%02X\n", b);
228 *ir_key = b;
229 *ir_raw = b;
230 return 1;
231}
232
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800233static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
234{
235 unsigned char b;
236
237 /* poll IR chip */
Jean Delvarec668f322009-05-13 16:48:50 -0300238 if (1 != i2c_master_recv(ir->c, &b, 1)) {
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800239 i2cdprintk("read error\n");
240 return -EIO;
241 }
242
243 /* no button press */
244 if (b==0)
245 return 0;
246
247 /* repeating */
248 if (b & 0x80)
249 return 1;
250
251 *ir_key = b;
252 *ir_raw = b;
253 return 1;
254}
255
Thomas Genty177aaaf2006-11-29 21:57:24 -0300256static int get_key_hvr1110(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
257{
258 unsigned char buf[5], cod4, code3, code4;
259
260 /* poll IR chip */
Jean Delvarec668f322009-05-13 16:48:50 -0300261 if (5 != i2c_master_recv(ir->c, buf, 5))
Thomas Genty177aaaf2006-11-29 21:57:24 -0300262 return -EIO;
263
264 cod4 = buf[4];
265 code4 = (cod4 >> 2);
266 code3 = buf[3];
267 if (code3 == 0)
268 /* no key pressed */
269 return 0;
270
271 /* return key */
272 *ir_key = code4;
273 *ir_raw = code4;
274 return 1;
275}
276
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300277
278static int get_key_beholdm6xx(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
279{
280 unsigned char data[12];
281 u32 gpio;
282
Jean Delvarec668f322009-05-13 16:48:50 -0300283 struct saa7134_dev *dev = ir->c->adapter->algo_data;
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300284
285 /* rising SAA7134_GPIO_GPRESCAN reads the status */
286 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
287 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
288
289 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
290
Mauro Carvalho Chehab616f8872008-01-07 05:18:36 -0300291 if (0x400000 & ~gpio)
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300292 return 0; /* No button press */
293
Jean Delvarec668f322009-05-13 16:48:50 -0300294 ir->c->addr = 0x5a >> 1;
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300295
Jean Delvarec668f322009-05-13 16:48:50 -0300296 if (12 != i2c_master_recv(ir->c, data, 12)) {
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300297 i2cdprintk("read error\n");
298 return -EIO;
299 }
300 /* IR of this card normally decode signals NEC-standard from
301 * - Sven IHOO MT 5.1R remote. xxyye718
302 * - Sven DVD HD-10xx remote. xxyyf708
303 * - BBK ...
304 * - mayby others
305 * So, skip not our, if disable full codes mode.
306 */
307 if (data[10] != 0x6b && data[11] != 0x86 && disable_other_ir)
308 return 0;
309
Dmitri Belimov2d21ffe2009-09-07 20:36:05 -0300310 /* Wrong data decode fix */
311 if (data[9] != (unsigned char)(~data[8]))
312 return 0;
313
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300314 *ir_key = data[9];
315 *ir_raw = data[9];
316
317 return 1;
318}
319
Mauro Carvalho Chehab1c22dad2008-07-17 22:31:29 -0300320/* Common (grey or coloured) pinnacle PCTV remote handling
321 *
322 */
323static int get_key_pinnacle(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw,
324 int parity_offset, int marker, int code_modulo)
325{
326 unsigned char b[4];
327 unsigned int start = 0,parity = 0,code = 0;
328
329 /* poll IR chip */
Jean Delvarec668f322009-05-13 16:48:50 -0300330 if (4 != i2c_master_recv(ir->c, b, 4)) {
Mauro Carvalho Chehab1c22dad2008-07-17 22:31:29 -0300331 i2cdprintk("read error\n");
332 return -EIO;
333 }
334
335 for (start = 0; start < ARRAY_SIZE(b); start++) {
336 if (b[start] == marker) {
337 code=b[(start+parity_offset + 1) % 4];
338 parity=b[(start+parity_offset) % 4];
339 }
340 }
341
342 /* Empty Request */
343 if (parity == 0)
344 return 0;
345
346 /* Repeating... */
347 if (ir->old == parity)
348 return 0;
349
350 ir->old = parity;
351
352 /* drop special codes when a key is held down a long time for the grey controller
353 In this case, the second bit of the code is asserted */
354 if (marker == 0xfe && (code & 0x40))
355 return 0;
356
357 code %= code_modulo;
358
359 *ir_raw = code;
360 *ir_key = code;
361
362 i2cdprintk("Pinnacle PCTV key %02x\n", code);
363
364 return 1;
365}
366
367/* The grey pinnacle PCTV remote
368 *
369 * There are one issue with this remote:
370 * - I2c packet does not change when the same key is pressed quickly. The workaround
371 * is to hold down each key for about half a second, so that another code is generated
372 * in the i2c packet, and the function can distinguish key presses.
373 *
374 * Sylvain Pasche <sylvain.pasche@gmail.com>
375 */
376static int get_key_pinnacle_grey(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
377{
378
379 return get_key_pinnacle(ir, ir_key, ir_raw, 1, 0xfe, 0xff);
380}
381
382
383/* The new pinnacle PCTV remote (with the colored buttons)
384 *
385 * Ricardo Cerqueira <v4l@cerqueira.org>
386 */
387static int get_key_pinnacle_color(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
388{
389 /* code_modulo parameter (0x88) is used to reduce code value to fit inside IR_KEYTAB_SIZE
390 *
391 * this is the only value that results in 42 unique
392 * codes < 128
393 */
394
395 return get_key_pinnacle(ir, ir_key, ir_raw, 2, 0x80, 0x88);
396}
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398void saa7134_input_irq(struct saa7134_dev *dev)
399{
Hermann Pitton91607232006-12-07 21:45:28 -0300400 struct card_ir *ir = dev->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -0300402 if (ir->nec_gpio) {
403 saa7134_nec_irq(dev);
404 } else if (!ir->polling && !ir->rc5_gpio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 build_key(dev);
Hermann Pitton91607232006-12-07 21:45:28 -0300406 } else if (ir->rc5_gpio) {
407 saa7134_rc5_irq(dev);
408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
411static void saa7134_input_timer(unsigned long data)
412{
Dmitry Torokhovb4ba7882007-05-21 11:41:02 -0300413 struct saa7134_dev *dev = (struct saa7134_dev *)data;
Hermann Pitton91607232006-12-07 21:45:28 -0300414 struct card_ir *ir = dev->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 build_key(dev);
Dmitry Torokhovb4ba7882007-05-21 11:41:02 -0300417 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Maxim Levitskyc4584732007-10-12 00:57:15 -0300420void saa7134_ir_start(struct saa7134_dev *dev, struct card_ir *ir)
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300421{
422 if (ir->polling) {
Dmitry Torokhovb4ba7882007-05-21 11:41:02 -0300423 setup_timer(&ir->timer, saa7134_input_timer,
424 (unsigned long)dev);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300425 ir->timer.expires = jiffies + HZ;
426 add_timer(&ir->timer);
Hermann Pitton91607232006-12-07 21:45:28 -0300427 } else if (ir->rc5_gpio) {
428 /* set timer_end for code completion */
429 init_timer(&ir->timer_end);
430 ir->timer_end.function = ir_rc5_timer_end;
431 ir->timer_end.data = (unsigned long)ir;
432 init_timer(&ir->timer_keyup);
433 ir->timer_keyup.function = ir_rc5_timer_keyup;
434 ir->timer_keyup.data = (unsigned long)ir;
435 ir->shift_by = 2;
436 ir->start = 0x2;
437 ir->addr = 0x17;
438 ir->rc5_key_timeout = ir_rc5_key_timeout;
439 ir->rc5_remote_gap = ir_rc5_remote_gap;
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -0300440 } else if (ir->nec_gpio) {
441 setup_timer(&ir->timer_keyup, saa7134_nec_timer,
442 (unsigned long)dev);
443 tasklet_init(&ir->tlet, nec_task, (unsigned long)dev);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300444 }
445}
446
Maxim Levitskyc4584732007-10-12 00:57:15 -0300447void saa7134_ir_stop(struct saa7134_dev *dev)
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300448{
449 if (dev->remote->polling)
450 del_timer_sync(&dev->remote->timer);
451}
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453int saa7134_input_init1(struct saa7134_dev *dev)
454{
Hermann Pitton91607232006-12-07 21:45:28 -0300455 struct card_ir *ir;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500456 struct input_dev *input_dev;
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300457 struct ir_scancode_table *ir_codes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 u32 mask_keycode = 0;
459 u32 mask_keydown = 0;
460 u32 mask_keyup = 0;
461 int polling = 0;
Hermann Pitton91607232006-12-07 21:45:28 -0300462 int rc5_gpio = 0;
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -0300463 int nec_gpio = 0;
Mauro Carvalho Chehab971e8292009-12-14 13:53:37 -0300464 u64 ir_type = IR_TYPE_OTHER;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300465 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Ricardo Cerqueiracb2444d2005-11-08 21:38:47 -0800467 if (dev->has_remote != SAA7134_REMOTE_GPIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return -ENODEV;
469 if (disable_ir)
470 return -ENODEV;
471
472 /* detect & configure */
473 switch (dev->board) {
474 case SAA7134_BOARD_FLYVIDEO2000:
475 case SAA7134_BOARD_FLYVIDEO3000:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800476 case SAA7134_BOARD_FLYTVPLATINUM_FM:
Arnaud Patard6af90ab2005-11-08 21:36:55 -0800477 case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
Eugene Yudin23389b82009-08-29 09:32:11 -0300478 case SAA7134_BOARD_ROVERMEDIA_LINK_PRO_FM:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300479 ir_codes = &ir_codes_flyvideo_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 mask_keycode = 0xEC00000;
481 mask_keydown = 0x0040000;
482 break;
483 case SAA7134_BOARD_CINERGY400:
484 case SAA7134_BOARD_CINERGY600:
485 case SAA7134_BOARD_CINERGY600_MK3:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300486 ir_codes = &ir_codes_cinergy_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 mask_keycode = 0x00003f;
488 mask_keyup = 0x040000;
489 break;
490 case SAA7134_BOARD_ECS_TVP3XP:
491 case SAA7134_BOARD_ECS_TVP3XP_4CB5:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300492 ir_codes = &ir_codes_eztv_table;
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700493 mask_keycode = 0x00017c;
494 mask_keyup = 0x000002;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 polling = 50; // ms
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700496 break;
497 case SAA7134_BOARD_KWORLD_XPERT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 case SAA7134_BOARD_AVACSSMARTTV:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300499 ir_codes = &ir_codes_pixelview_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 mask_keycode = 0x00001F;
501 mask_keyup = 0x000020;
502 polling = 50; // ms
503 break;
504 case SAA7134_BOARD_MD2819:
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700505 case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 case SAA7134_BOARD_AVERMEDIA_305:
507 case SAA7134_BOARD_AVERMEDIA_307:
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700508 case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
Vasiliy Temnikov5a5e1da2009-08-26 22:10:55 -0300509 case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700510 case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
Mikhail Fedotov3ac706d2006-10-06 20:23:47 -0300511 case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
Andy Shevchenkodf0dbbe2009-04-08 14:01:19 -0300512 case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700513 case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
Albert Grahamd2761f22007-12-09 09:44:38 -0300514 case SAA7134_BOARD_AVERMEDIA_M102:
Pham Thanh Nam6a2d8022008-12-30 23:26:09 -0300515 case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300516 ir_codes = &ir_codes_avermedia_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 mask_keycode = 0x0007C8;
518 mask_keydown = 0x000010;
519 polling = 50; // ms
520 /* Set GPIO pin2 to high to enable the IR controller */
521 saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
522 saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
523 break;
Mauro Carvalho Chehab36f6bb92008-06-26 17:03:00 -0300524 case SAA7134_BOARD_AVERMEDIA_M135A:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300525 ir_codes = &ir_codes_avermedia_m135a_table;
Mauro Carvalho Chehab36f6bb92008-06-26 17:03:00 -0300526 mask_keydown = 0x0040000;
527 mask_keycode = 0x00013f;
528 nec_gpio = 1;
529 break;
pasky@ucw.cz450efcf2006-11-12 14:22:32 -0300530 case SAA7134_BOARD_AVERMEDIA_777:
pasky@ucw.cz29e0f1a2006-11-12 14:23:32 -0300531 case SAA7134_BOARD_AVERMEDIA_A16AR:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300532 ir_codes = &ir_codes_avermedia_table;
pasky@ucw.cz450efcf2006-11-12 14:22:32 -0300533 mask_keycode = 0x02F200;
534 mask_keydown = 0x000400;
535 polling = 50; // ms
536 /* Without this we won't receive key up events */
537 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
538 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
Linus Torvalds4dd74062006-11-13 09:50:11 -0800539 break;
Tim Farrington6e501a32008-06-15 13:33:42 -0300540 case SAA7134_BOARD_AVERMEDIA_A16D:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300541 ir_codes = &ir_codes_avermedia_a16d_table;
Tim Farrington6e501a32008-06-15 13:33:42 -0300542 mask_keycode = 0x02F200;
543 mask_keydown = 0x000400;
544 polling = 50; /* ms */
545 /* Without this we won't receive key up events */
546 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
547 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
548 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800549 case SAA7134_BOARD_KWORLD_TERMINATOR:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300550 ir_codes = &ir_codes_pixelview_table;
James R. Webbdc2286c2005-11-08 21:37:00 -0800551 mask_keycode = 0x00001f;
552 mask_keyup = 0x000060;
553 polling = 50; // ms
554 break;
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700555 case SAA7134_BOARD_MANLI_MTV001:
556 case SAA7134_BOARD_MANLI_MTV002:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300557 ir_codes = &ir_codes_manli_table;
Dmitry Belimovb34dddb2008-04-23 14:09:08 -0300558 mask_keycode = 0x001f00;
559 mask_keyup = 0x004000;
560 polling = 50; /* ms */
561 break;
Nickolay V. Shmyreva8ff4172005-11-08 21:36:16 -0800562 case SAA7134_BOARD_BEHOLD_409FM:
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300563 case SAA7134_BOARD_BEHOLD_401:
564 case SAA7134_BOARD_BEHOLD_403:
565 case SAA7134_BOARD_BEHOLD_403FM:
566 case SAA7134_BOARD_BEHOLD_405:
567 case SAA7134_BOARD_BEHOLD_405FM:
568 case SAA7134_BOARD_BEHOLD_407:
569 case SAA7134_BOARD_BEHOLD_407FM:
570 case SAA7134_BOARD_BEHOLD_409:
571 case SAA7134_BOARD_BEHOLD_505FM:
Dmitri Belimov9c6f97a2009-12-21 02:00:38 -0300572 case SAA7134_BOARD_BEHOLD_505RDS_MK5:
573 case SAA7134_BOARD_BEHOLD_505RDS_MK3:
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300574 case SAA7134_BOARD_BEHOLD_507_9FM:
Dmitri Belimov84d728c2009-04-23 02:32:49 -0300575 case SAA7134_BOARD_BEHOLD_507RDS_MK3:
576 case SAA7134_BOARD_BEHOLD_507RDS_MK5:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300577 ir_codes = &ir_codes_manli_table;
Dmitry Belimovb34dddb2008-04-23 14:09:08 -0300578 mask_keycode = 0x003f00;
579 mask_keyup = 0x004000;
580 polling = 50; /* ms */
581 break;
582 case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300583 ir_codes = &ir_codes_behold_columbus_table;
Dmitry Belimovb34dddb2008-04-23 14:09:08 -0300584 mask_keycode = 0x003f00;
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700585 mask_keyup = 0x004000;
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700586 polling = 50; // ms
587 break;
Ricardo Cerqueira17ce1ff2005-11-08 21:38:47 -0800588 case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300589 ir_codes = &ir_codes_pctv_sedna_table;
Pavel Mihaylovc3d93192005-11-08 21:38:43 -0800590 mask_keycode = 0x001f00;
591 mask_keyup = 0x004000;
592 polling = 50; // ms
593 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800594 case SAA7134_BOARD_GOTVIEW_7135:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300595 ir_codes = &ir_codes_gotview7135_table;
Pedro0938e312007-10-17 17:58:40 -0300596 mask_keycode = 0x0003CC;
Nickolay V. Shmyrev6b961442005-11-08 21:36:22 -0800597 mask_keydown = 0x000010;
Pedro0938e312007-10-17 17:58:40 -0300598 polling = 5; /* ms */
599 saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
Nickolay V. Shmyrev6b961442005-11-08 21:36:22 -0800600 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 case SAA7134_BOARD_VIDEOMATE_TV_PVR:
Nickolay V. Shmyrev2a9a9a82006-01-09 15:25:33 -0200602 case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700603 case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300604 ir_codes = &ir_codes_videomate_tv_pvr_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 mask_keycode = 0x00003F;
606 mask_keyup = 0x400000;
607 polling = 50; // ms
608 break;
Michal Majchrowiczb04c1ba2006-09-13 16:42:42 -0300609 case SAA7134_BOARD_PROTEUS_2309:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300610 ir_codes = &ir_codes_proteus_2309_table;
Michal Majchrowiczb04c1ba2006-09-13 16:42:42 -0300611 mask_keycode = 0x00007F;
612 mask_keyup = 0x000080;
613 polling = 50; // ms
614 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800615 case SAA7134_BOARD_VIDEOMATE_DVBT_300:
616 case SAA7134_BOARD_VIDEOMATE_DVBT_200:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300617 ir_codes = &ir_codes_videomate_tv_pvr_table;
Nickolay V. Shmyrevfea095f2005-11-08 21:36:47 -0800618 mask_keycode = 0x003F00;
619 mask_keyup = 0x040000;
620 break;
James Le Cuirotf5c965a2007-07-02 12:53:25 -0300621 case SAA7134_BOARD_FLYDVBS_LR300:
Giampiero Giancipoli3d8466e2006-02-07 06:49:09 -0200622 case SAA7134_BOARD_FLYDVBT_LR301:
Rudo Thomasa8029172006-02-27 00:08:46 -0300623 case SAA7134_BOARD_FLYDVBTDUO:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300624 ir_codes = &ir_codes_flydvb_table;
Giampiero Giancipoli3d8466e2006-02-07 06:49:09 -0200625 mask_keycode = 0x0001F00;
626 mask_keydown = 0x0040000;
627 break;
Hermann Pitton91607232006-12-07 21:45:28 -0300628 case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
Ed Vipas904ab882007-03-29 18:32:49 -0300629 case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
Mauro Carvalho Chehabdfb4ba12009-08-20 10:13:40 -0300630 case SAA7134_BOARD_ASUSTeK_P7131_ANALOG:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300631 ir_codes = &ir_codes_asus_pc39_table;
Hermann Pitton91607232006-12-07 21:45:28 -0300632 mask_keydown = 0x0040000;
633 rc5_gpio = 1;
634 break;
Juan Pablo Sormanic36c4592006-12-27 12:46:36 -0300635 case SAA7134_BOARD_ENCORE_ENLTV:
636 case SAA7134_BOARD_ENCORE_ENLTV_FM:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300637 ir_codes = &ir_codes_encore_enltv_table;
Juan Pablo Sormanic36c4592006-12-27 12:46:36 -0300638 mask_keycode = 0x00007f;
639 mask_keyup = 0x040000;
640 polling = 50; // ms
641 break;
Mauro Carvalho Chehabbf1ece62008-06-26 17:03:00 -0300642 case SAA7134_BOARD_ENCORE_ENLTV_FM53:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300643 ir_codes = &ir_codes_encore_enltv_fm53_table;
Mauro Carvalho Chehabbf1ece62008-06-26 17:03:00 -0300644 mask_keydown = 0x0040000;
645 mask_keycode = 0x00007f;
646 nec_gpio = 1;
647 break;
Tony Wan480f75a2007-05-11 11:33:50 -0300648 case SAA7134_BOARD_10MOONSTVMASTER3:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300649 ir_codes = &ir_codes_encore_enltv_table;
Tony Wan480f75a2007-05-11 11:33:50 -0300650 mask_keycode = 0x5f80000;
651 mask_keyup = 0x8000000;
652 polling = 50; //ms
653 break;
Adrian Pardinif0ba3562008-02-11 12:40:53 -0300654 case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300655 ir_codes = &ir_codes_genius_tvgo_a11mce_table;
Adrian Pardinif0ba3562008-02-11 12:40:53 -0300656 mask_keycode = 0xff;
657 mask_keydown = 0xf00000;
658 polling = 50; /* ms */
659 break;
Mauro Carvalho Chehab9b000192008-06-26 17:03:00 -0300660 case SAA7134_BOARD_REAL_ANGEL_220:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300661 ir_codes = &ir_codes_real_audio_220_32_keys_table;
Mauro Carvalho Chehab9b000192008-06-26 17:03:00 -0300662 mask_keycode = 0x3f00;
663 mask_keyup = 0x4000;
664 polling = 50; /* ms */
665 break;
Mauro Carvalho Chehab26d5f3a2008-12-07 13:19:29 -0300666 case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300667 ir_codes = &ir_codes_kworld_plus_tv_analog_table;
Mauro Carvalho Chehab26d5f3a2008-12-07 13:19:29 -0300668 mask_keycode = 0x7f;
669 polling = 40; /* ms */
670 break;
Igor M. Liplianinecfcfec2009-08-13 21:42:21 -0300671 case SAA7134_BOARD_VIDEOMATE_S350:
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -0300672 ir_codes = &ir_codes_videomate_s350_table;
Igor M. Liplianinecfcfec2009-08-13 21:42:21 -0300673 mask_keycode = 0x003f00;
674 mask_keydown = 0x040000;
675 break;
Michael Obst0700ade2009-10-31 14:05:42 -0300676 case SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S:
677 ir_codes = &ir_codes_winfast_table;
678 mask_keycode = 0x5f00;
679 mask_keyup = 0x020000;
Michael Krufky117e1342009-11-01 11:16:10 -0300680 polling = 50; /* ms */
Michael Obst0700ade2009-10-31 14:05:42 -0300681 break;
Lukas Karasd995a182009-11-24 12:06:52 -0300682 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684 if (NULL == ir_codes) {
685 printk("%s: Oops: IR config error [card=%d]\n",
686 dev->name, dev->board);
687 return -ENODEV;
688 }
689
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500690 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
691 input_dev = input_allocate_device();
692 if (!ir || !input_dev) {
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300693 err = -ENOMEM;
694 goto err_out_free;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Dmitry Torokhovd271d1c2005-11-20 00:56:54 -0500697 ir->dev = input_dev;
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 /* init hardware-specific stuff */
700 ir->mask_keycode = mask_keycode;
701 ir->mask_keydown = mask_keydown;
702 ir->mask_keyup = mask_keyup;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800703 ir->polling = polling;
Hermann Pitton91607232006-12-07 21:45:28 -0300704 ir->rc5_gpio = rc5_gpio;
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -0300705 ir->nec_gpio = nec_gpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 /* init input device */
708 snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
709 saa7134_boards[dev->board].name);
710 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
711 pci_name(dev->pci));
712
Mauro Carvalho Chehab579e7d62009-12-11 11:20:59 -0300713 err = ir_input_init(input_dev, &ir->ir, ir_type);
Mauro Carvalho Chehab055cd552009-11-29 08:19:59 -0300714 if (err < 0)
715 goto err_out_free;
716
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500717 input_dev->name = ir->name;
718 input_dev->phys = ir->phys;
719 input_dev->id.bustype = BUS_PCI;
720 input_dev->id.version = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (dev->pci->subsystem_vendor) {
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500722 input_dev->id.vendor = dev->pci->subsystem_vendor;
723 input_dev->id.product = dev->pci->subsystem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 } else {
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500725 input_dev->id.vendor = dev->pci->vendor;
726 input_dev->id.product = dev->pci->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
Dmitry Torokhov2c8a3a32007-07-16 09:28:15 -0300728 input_dev->dev.parent = &dev->pci->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 dev->remote = ir;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300731 saa7134_ir_start(dev, ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Mauro Carvalho Chehabe93854d2009-12-14 00:16:55 -0300733 err = ir_input_register(ir->dev, ir_codes, NULL);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300734 if (err)
735 goto err_out_stop;
736
Pedro0938e312007-10-17 17:58:40 -0300737 /* the remote isn't as bouncy as a keyboard */
738 ir->dev->rep[REP_DELAY] = repeat_delay;
739 ir->dev->rep[REP_PERIOD] = repeat_period;
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return 0;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300742
743 err_out_stop:
744 saa7134_ir_stop(dev);
745 dev->remote = NULL;
746 err_out_free:
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300747 kfree(ir);
748 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
751void saa7134_input_fini(struct saa7134_dev *dev)
752{
753 if (NULL == dev->remote)
754 return;
755
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300756 saa7134_ir_stop(dev);
Mauro Carvalho Chehab38ef6aa2009-12-11 09:47:42 -0300757 ir_input_unregister(dev->remote->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 kfree(dev->remote);
759 dev->remote = NULL;
760}
761
Jean Delvarec668f322009-05-13 16:48:50 -0300762void saa7134_probe_i2c_ir(struct saa7134_dev *dev)
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800763{
Jean Delvare43e16ea2009-10-02 05:47:08 -0300764 struct i2c_board_info info;
Jean Delvarec668f322009-05-13 16:48:50 -0300765
Jean Delvarec668f322009-05-13 16:48:50 -0300766 struct i2c_msg msg_msi = {
767 .addr = 0x50,
768 .flags = I2C_M_RD,
769 .len = 0,
770 .buf = NULL,
771 };
772
Jean Delvarec668f322009-05-13 16:48:50 -0300773 int rc;
774
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800775 if (disable_ir) {
Jean Delvarec668f322009-05-13 16:48:50 -0300776 dprintk("IR has been disabled, not probing for i2c remote\n");
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800777 return;
778 }
779
Jean Delvare43e16ea2009-10-02 05:47:08 -0300780 memset(&info, 0, sizeof(struct i2c_board_info));
Mauro Carvalho Chehab7aedd5e2009-09-07 02:22:01 -0300781 memset(&dev->init_data, 0, sizeof(dev->init_data));
Jean Delvare43e16ea2009-10-02 05:47:08 -0300782 strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300783
784 switch (dev->board) {
785 case SAA7134_BOARD_PINNACLE_PCTV_110i:
786 case SAA7134_BOARD_PINNACLE_PCTV_310i:
Mauro Carvalho Chehab7aedd5e2009-09-07 02:22:01 -0300787 dev->init_data.name = "Pinnacle PCTV";
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300788 if (pinnacle_remote == 0) {
Mauro Carvalho Chehab7aedd5e2009-09-07 02:22:01 -0300789 dev->init_data.get_key = get_key_pinnacle_color;
790 dev->init_data.ir_codes = &ir_codes_pinnacle_color_table;
Jean Delvare43e16ea2009-10-02 05:47:08 -0300791 info.addr = 0x47;
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300792 } else {
Mauro Carvalho Chehab7aedd5e2009-09-07 02:22:01 -0300793 dev->init_data.get_key = get_key_pinnacle_grey;
794 dev->init_data.ir_codes = &ir_codes_pinnacle_grey_table;
Jean Delvare43e16ea2009-10-02 05:47:08 -0300795 info.addr = 0x47;
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300796 }
797 break;
798 case SAA7134_BOARD_UPMOST_PURPLE_TV:
Mauro Carvalho Chehab7aedd5e2009-09-07 02:22:01 -0300799 dev->init_data.name = "Purple TV";
800 dev->init_data.get_key = get_key_purpletv;
801 dev->init_data.ir_codes = &ir_codes_purpletv_table;
Jean Delvareaef02aa2009-10-02 08:47:22 -0300802 info.addr = 0x7a;
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300803 break;
804 case SAA7134_BOARD_MSI_TVATANYWHERE_PLUS:
Mauro Carvalho Chehab7aedd5e2009-09-07 02:22:01 -0300805 dev->init_data.name = "MSI TV@nywhere Plus";
806 dev->init_data.get_key = get_key_msi_tvanywhere_plus;
807 dev->init_data.ir_codes = &ir_codes_msi_tvanywhere_plus_table;
Jean Delvare43e16ea2009-10-02 05:47:08 -0300808 info.addr = 0x30;
Jean Delvareec218a42009-05-13 16:51:46 -0300809 /* MSI TV@nywhere Plus controller doesn't seem to
810 respond to probes unless we read something from
811 an existing device. Weird...
812 REVISIT: might no longer be needed */
813 rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
814 dprintk(KERN_DEBUG "probe 0x%02x @ %s: %s\n",
815 msg_msi.addr, dev->i2c_adap.name,
816 (1 == rc) ? "yes" : "no");
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300817 break;
818 case SAA7134_BOARD_HAUPPAUGE_HVR1110:
Mauro Carvalho Chehab7aedd5e2009-09-07 02:22:01 -0300819 dev->init_data.name = "HVR 1110";
820 dev->init_data.get_key = get_key_hvr1110;
821 dev->init_data.ir_codes = &ir_codes_hauppauge_new_table;
Jean Delvare30093e82009-10-02 09:48:04 -0300822 info.addr = 0x71;
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300823 break;
824 case SAA7134_BOARD_BEHOLD_607FM_MK3:
825 case SAA7134_BOARD_BEHOLD_607FM_MK5:
826 case SAA7134_BOARD_BEHOLD_609FM_MK3:
827 case SAA7134_BOARD_BEHOLD_609FM_MK5:
828 case SAA7134_BOARD_BEHOLD_607RDS_MK3:
829 case SAA7134_BOARD_BEHOLD_607RDS_MK5:
830 case SAA7134_BOARD_BEHOLD_609RDS_MK3:
831 case SAA7134_BOARD_BEHOLD_609RDS_MK5:
832 case SAA7134_BOARD_BEHOLD_M6:
833 case SAA7134_BOARD_BEHOLD_M63:
834 case SAA7134_BOARD_BEHOLD_M6_EXTRA:
835 case SAA7134_BOARD_BEHOLD_H6:
Dmitri Belimov2012c872009-08-26 01:01:12 -0300836 case SAA7134_BOARD_BEHOLD_X7:
Mauro Carvalho Chehab7aedd5e2009-09-07 02:22:01 -0300837 dev->init_data.name = "BeholdTV";
838 dev->init_data.get_key = get_key_beholdm6xx;
839 dev->init_data.ir_codes = &ir_codes_behold_table;
Jean Delvare30093e82009-10-02 09:48:04 -0300840 info.addr = 0x2d;
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300841 break;
Jean Delvared9a88e62009-05-13 16:52:44 -0300842 case SAA7134_BOARD_AVERMEDIA_CARDBUS_501:
843 case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
Jean Delvare43e16ea2009-10-02 05:47:08 -0300844 info.addr = 0x40;
Jean Delvared9a88e62009-05-13 16:52:44 -0300845 break;
Lukas Karasd995a182009-11-24 12:06:52 -0300846 case SAA7134_BOARD_FLYDVB_TRIO:
847 dev->init_data.name = "FlyDVB Trio";
848 dev->init_data.get_key = get_key_flydvb_trio;
849 dev->init_data.ir_codes = &ir_codes_flydvb_table;
850 info.addr = 0x0b;
851 break;
Jean Delvare30093e82009-10-02 09:48:04 -0300852 default:
853 dprintk("No I2C IR support for board %x\n", dev->board);
854 return;
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300855 }
856
Mauro Carvalho Chehab7aedd5e2009-09-07 02:22:01 -0300857 if (dev->init_data.name)
Jean Delvare43e16ea2009-10-02 05:47:08 -0300858 info.platform_data = &dev->init_data;
Jean Delvare30093e82009-10-02 09:48:04 -0300859 i2c_new_device(&dev->i2c_adap, &info);
Jean Delvarec668f322009-05-13 16:48:50 -0300860}
861
Hermann Pitton91607232006-12-07 21:45:28 -0300862static int saa7134_rc5_irq(struct saa7134_dev *dev)
863{
864 struct card_ir *ir = dev->remote;
865 struct timeval tv;
866 u32 gap;
867 unsigned long current_jiffies, timeout;
868
869 /* get time of bit */
870 current_jiffies = jiffies;
871 do_gettimeofday(&tv);
872
873 /* avoid overflow with gap >1s */
874 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
875 gap = 200000;
876 } else {
877 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
878 tv.tv_usec - ir->base_time.tv_usec;
879 }
880
881 /* active code => add bit */
882 if (ir->active) {
883 /* only if in the code (otherwise spurious IRQ or timer
884 late) */
885 if (ir->last_bit < 28) {
886 ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
887 ir_rc5_remote_gap;
888 ir->code |= 1 << ir->last_bit;
889 }
890 /* starting new code */
891 } else {
892 ir->active = 1;
893 ir->code = 0;
894 ir->base_time = tv;
895 ir->last_bit = 0;
896
897 timeout = current_jiffies + (500 + 30 * HZ) / 1000;
898 mod_timer(&ir->timer_end, timeout);
899 }
900
901 return 1;
902}
903
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -0300904
905/* On NEC protocol, One has 2.25 ms, and zero has 1.125 ms
906 The first pulse (start) has 9 + 4.5 ms
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 */
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -0300908
909static void saa7134_nec_timer(unsigned long data)
910{
911 struct saa7134_dev *dev = (struct saa7134_dev *) data;
912 struct card_ir *ir = dev->remote;
913
914 dprintk("Cancel key repeat\n");
915
916 ir_input_nokey(ir->dev, &ir->ir);
917}
918
919static void nec_task(unsigned long data)
920{
921 struct saa7134_dev *dev = (struct saa7134_dev *) data;
922 struct card_ir *ir;
923 struct timeval tv;
924 int count, pulse, oldpulse, gap;
925 u32 ircode = 0, not_code = 0;
926 int ngap = 0;
927
928 if (!data) {
929 printk(KERN_ERR "saa713x/ir: Can't recover dev struct\n");
930 /* GPIO will be kept disabled */
931 return;
932 }
933
934 ir = dev->remote;
935
936 /* rising SAA7134_GPIO_GPRESCAN reads the status */
937 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
938 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
939
940 oldpulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
941 pulse = oldpulse;
942
943 do_gettimeofday(&tv);
944 ir->base_time = tv;
945
946 /* Decode NEC pulsecode. This code can take up to 76.5 ms to run.
947 Unfortunately, using IRQ to decode pulse didn't work, since it uses
948 a pulse train of 38KHz. This means one pulse on each 52 us
949 */
950 do {
951 /* Wait until the end of pulse/space or 5 ms */
952 for (count = 0; count < 500; count++) {
953 udelay(10);
954 /* rising SAA7134_GPIO_GPRESCAN reads the status */
955 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
956 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
957 pulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2)
958 & ir->mask_keydown;
959 if (pulse != oldpulse)
960 break;
961 }
962
963 do_gettimeofday(&tv);
964 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
965 tv.tv_usec - ir->base_time.tv_usec;
966
967 if (!pulse) {
968 /* Bit 0 has 560 us, while bit 1 has 1120 us.
969 Do something only if bit == 1
970 */
971 if (ngap && (gap > 560 + 280)) {
972 unsigned int shift = ngap - 1;
973
974 /* Address first, then command */
975 if (shift < 8) {
976 shift += 8;
977 ircode |= 1 << shift;
978 } else if (shift < 16) {
979 not_code |= 1 << shift;
980 } else if (shift < 24) {
981 shift -= 16;
982 ircode |= 1 << shift;
983 } else {
984 shift -= 24;
985 not_code |= 1 << shift;
986 }
987 }
988 ngap++;
989 }
990
991
992 ir->base_time = tv;
993
994 /* TIMEOUT - Long pulse */
995 if (gap >= 5000)
996 break;
997 oldpulse = pulse;
998 } while (ngap < 32);
999
1000 if (ngap == 32) {
1001 /* FIXME: should check if not_code == ~ircode */
1002 ir->code = ir_extract_bits(ircode, ir->mask_keycode);
1003
1004 dprintk("scancode = 0x%02x (code = 0x%02x, notcode= 0x%02x)\n",
1005 ir->code, ircode, not_code);
1006
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -03001007 ir_input_keydown(ir->dev, &ir->ir, ir->code);
Mauro Carvalho Chehab622ecb32008-08-05 10:03:17 -03001008 } else
1009 dprintk("Repeat last key\n");
1010
1011 /* Keep repeating the last key */
1012 mod_timer(&ir->timer_keyup, jiffies + msecs_to_jiffies(150));
1013
1014 saa_setl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
1015}
1016
1017static int saa7134_nec_irq(struct saa7134_dev *dev)
1018{
1019 struct card_ir *ir = dev->remote;
1020
1021 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
1022 tasklet_schedule(&ir->tlet);
1023
1024 return 1;
1025}