blob: fbb2ff1710084b4550f6dd0e7d289841c0865fcd [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include "saa7134-reg.h"
28#include "saa7134.h"
29
Mauro Carvalho Chehabd3025482010-10-06 22:02:58 -030030#define MODULE_NAME "saa7134"
Mauro Carvalho Chehab727e6252010-03-12 21:18:14 -030031
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030032static unsigned int disable_ir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033module_param(disable_ir, int, 0444);
34MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
35
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030036static unsigned int ir_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037module_param(ir_debug, int, 0644);
38MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
39
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030040static int pinnacle_remote;
Sylvain Pascheb93eedb2006-03-25 23:14:42 -030041module_param(pinnacle_remote, int, 0644); /* Choose Pinnacle PCTV remote */
42MODULE_PARM_DESC(pinnacle_remote, "Specify Pinnacle PCTV remote: 0=coloured, 1=grey (defaults to 0)");
43
Adrian Bunkc408a6f2006-12-28 12:47:47 -030044static int ir_rc5_remote_gap = 885;
Hermann Pitton91607232006-12-07 21:45:28 -030045module_param(ir_rc5_remote_gap, int, 0644);
Hermann Pitton91607232006-12-07 21:45:28 -030046
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -030047static unsigned int disable_other_ir;
48module_param(disable_other_ir, int, 0644);
49MODULE_PARM_DESC(disable_other_ir, "disable full codes of "
50 "alternative remotes from other manufacturers");
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define dprintk(fmt, arg...) if (ir_debug) \
53 printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -080054#define i2cdprintk(fmt, arg...) if (ir_debug) \
Jean Delvare1df8e982009-05-13 16:48:07 -030055 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Mauro Carvalho Chehab8ea43d02010-11-13 16:49:10 -030057/* Helper functions for RC5 and NEC decoding at GPIO16 or GPIO18 */
58static int saa7134_rc5_irq(struct saa7134_dev *dev);
59static int saa7134_nec_irq(struct saa7134_dev *dev);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030060static int saa7134_raw_decode_irq(struct saa7134_dev *dev);
Mauro Carvalho Chehab8ea43d02010-11-13 16:49:10 -030061static void nec_task(unsigned long data);
Hermann Pitton91607232006-12-07 21:45:28 -030062
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -080063/* -------------------- GPIO generic keycode builder -------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65static int build_key(struct saa7134_dev *dev)
66{
Hermann Pitton91607232006-12-07 21:45:28 -030067 struct card_ir *ir = dev->remote;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 u32 gpio, data;
69
Pedro0938e312007-10-17 17:58:40 -030070 /* here comes the additional handshake steps for some cards */
71 switch (dev->board) {
72 case SAA7134_BOARD_GOTVIEW_7135:
73 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x80);
74 saa_clearb(SAA7134_GPIO_GPSTATUS1, 0x80);
75 break;
76 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 /* rising SAA7134_GPIO_GPRESCAN reads the status */
78 saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
79 saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
80
81 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080082 if (ir->polling) {
83 if (ir->last_gpio == gpio)
84 return 0;
85 ir->last_gpio = gpio;
86 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080088 data = ir_extract_bits(gpio, ir->mask_keycode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
90 gpio, ir->mask_keycode, data);
91
Mauro Carvalho Chehab26d5f3a2008-12-07 13:19:29 -030092 switch (dev->board) {
93 case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
94 if (data == ir->mask_keycode)
David Härdeman62c65032010-10-29 16:08:07 -030095 ir_keyup(ir->dev);
Mauro Carvalho Chehab26d5f3a2008-12-07 13:19:29 -030096 else
David Härdeman62c65032010-10-29 16:08:07 -030097 ir_keydown_notimeout(ir->dev, data, 0);
Mauro Carvalho Chehab26d5f3a2008-12-07 13:19:29 -030098 return 0;
99 }
100
Peter Missel0602fbb2006-01-09 18:21:23 -0200101 if (ir->polling) {
102 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
103 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
David Härdeman62c65032010-10-29 16:08:07 -0300104 ir_keydown_notimeout(ir->dev, data, 0);
Peter Missel0602fbb2006-01-09 18:21:23 -0200105 } else {
David Härdeman62c65032010-10-29 16:08:07 -0300106 ir_keyup(ir->dev);
Peter Missel0602fbb2006-01-09 18:21:23 -0200107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
Peter Missel0602fbb2006-01-09 18:21:23 -0200109 else { /* IRQ driven mode - handle key press and release in one go */
110 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
111 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
David Härdeman62c65032010-10-29 16:08:07 -0300112 ir_keydown_notimeout(ir->dev, data, 0);
113 ir_keyup(ir->dev);
Peter Missel0602fbb2006-01-09 18:21:23 -0200114 }
115 }
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return 0;
118}
119
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800120/* --------------------- Chip specific I2C key builders ----------------- */
121
Lukas Karasd995a182009-11-24 12:06:52 -0300122static int get_key_flydvb_trio(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
123{
124 int gpio;
125 int attempt = 0;
126 unsigned char b;
127
128 /* We need this to access GPI Used by the saa_readl macro. */
129 struct saa7134_dev *dev = ir->c->adapter->algo_data;
130
131 if (dev == NULL) {
Dan Carpenter129c34d2010-05-26 11:58:10 -0300132 i2cdprintk("get_key_flydvb_trio: "
133 "ir->c->adapter->algo_data is NULL!\n");
Lukas Karasd995a182009-11-24 12:06:52 -0300134 return -EIO;
135 }
136
137 /* rising SAA7134_GPIGPRESCAN reads the status */
138 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
139 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
140
141 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
142
143 if (0x40000 & ~gpio)
144 return 0; /* No button press */
145
146 /* No button press - only before first key pressed */
147 if (b == 0xFF)
148 return 0;
149
150 /* poll IR chip */
151 /* weak up the IR chip */
152 b = 0;
153
154 while (1 != i2c_master_send(ir->c, &b, 1)) {
155 if ((attempt++) < 10) {
156 /*
157 * wait a bit for next attempt -
158 * I don't know how make it better
159 */
160 msleep(10);
161 continue;
162 }
163 i2cdprintk("send wake up byte to pic16C505 (IR chip)"
164 "failed %dx\n", attempt);
165 return -EIO;
166 }
167 if (1 != i2c_master_recv(ir->c, &b, 1)) {
168 i2cdprintk("read error\n");
169 return -EIO;
170 }
171
172 *ir_key = b;
173 *ir_raw = b;
174 return 1;
175}
176
Brian Rogersba340b42008-10-13 08:37:06 -0300177static int get_key_msi_tvanywhere_plus(struct IR_i2c *ir, u32 *ir_key,
178 u32 *ir_raw)
179{
180 unsigned char b;
181 int gpio;
182
183 /* <dev> is needed to access GPIO. Used by the saa_readl macro. */
Jean Delvarec668f322009-05-13 16:48:50 -0300184 struct saa7134_dev *dev = ir->c->adapter->algo_data;
Brian Rogersba340b42008-10-13 08:37:06 -0300185 if (dev == NULL) {
Dan Carpenter129c34d2010-05-26 11:58:10 -0300186 i2cdprintk("get_key_msi_tvanywhere_plus: "
187 "ir->c->adapter->algo_data is NULL!\n");
Brian Rogersba340b42008-10-13 08:37:06 -0300188 return -EIO;
189 }
190
191 /* rising SAA7134_GPIO_GPRESCAN reads the status */
192
193 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
194 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
195
196 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
197
198 /* GPIO&0x40 is pulsed low when a button is pressed. Don't do
199 I2C receive if gpio&0x40 is not low. */
200
201 if (gpio & 0x40)
202 return 0; /* No button press */
203
204 /* GPIO says there is a button press. Get it. */
205
Jean Delvarec668f322009-05-13 16:48:50 -0300206 if (1 != i2c_master_recv(ir->c, &b, 1)) {
Brian Rogersba340b42008-10-13 08:37:06 -0300207 i2cdprintk("read error\n");
208 return -EIO;
209 }
210
211 /* No button press */
212
213 if (b == 0xff)
214 return 0;
215
216 /* Button pressed */
217
218 dprintk("get_key_msi_tvanywhere_plus: Key = 0x%02X\n", b);
219 *ir_key = b;
220 *ir_raw = b;
221 return 1;
222}
223
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800224static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
225{
226 unsigned char b;
227
228 /* poll IR chip */
Jean Delvarec668f322009-05-13 16:48:50 -0300229 if (1 != i2c_master_recv(ir->c, &b, 1)) {
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800230 i2cdprintk("read error\n");
231 return -EIO;
232 }
233
234 /* no button press */
235 if (b==0)
236 return 0;
237
238 /* repeating */
239 if (b & 0x80)
240 return 1;
241
242 *ir_key = b;
243 *ir_raw = b;
244 return 1;
245}
246
Thomas Genty177aaaf2006-11-29 21:57:24 -0300247static int get_key_hvr1110(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
248{
249 unsigned char buf[5], cod4, code3, code4;
250
251 /* poll IR chip */
Jean Delvarec668f322009-05-13 16:48:50 -0300252 if (5 != i2c_master_recv(ir->c, buf, 5))
Thomas Genty177aaaf2006-11-29 21:57:24 -0300253 return -EIO;
254
255 cod4 = buf[4];
256 code4 = (cod4 >> 2);
257 code3 = buf[3];
258 if (code3 == 0)
259 /* no key pressed */
260 return 0;
261
262 /* return key */
263 *ir_key = code4;
264 *ir_raw = code4;
265 return 1;
266}
267
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300268
269static int get_key_beholdm6xx(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
270{
271 unsigned char data[12];
272 u32 gpio;
273
Jean Delvarec668f322009-05-13 16:48:50 -0300274 struct saa7134_dev *dev = ir->c->adapter->algo_data;
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300275
276 /* rising SAA7134_GPIO_GPRESCAN reads the status */
277 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
278 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
279
280 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
281
Mauro Carvalho Chehab616f8872008-01-07 05:18:36 -0300282 if (0x400000 & ~gpio)
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300283 return 0; /* No button press */
284
Jean Delvarec668f322009-05-13 16:48:50 -0300285 ir->c->addr = 0x5a >> 1;
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300286
Jean Delvarec668f322009-05-13 16:48:50 -0300287 if (12 != i2c_master_recv(ir->c, data, 12)) {
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300288 i2cdprintk("read error\n");
289 return -EIO;
290 }
291 /* IR of this card normally decode signals NEC-standard from
292 * - Sven IHOO MT 5.1R remote. xxyye718
293 * - Sven DVD HD-10xx remote. xxyyf708
294 * - BBK ...
295 * - mayby others
296 * So, skip not our, if disable full codes mode.
297 */
298 if (data[10] != 0x6b && data[11] != 0x86 && disable_other_ir)
299 return 0;
300
Dmitri Belimov2d21ffe2009-09-07 20:36:05 -0300301 /* Wrong data decode fix */
302 if (data[9] != (unsigned char)(~data[8]))
303 return 0;
304
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300305 *ir_key = data[9];
306 *ir_raw = data[9];
307
308 return 1;
309}
310
Mauro Carvalho Chehab1c22dad2008-07-17 22:31:29 -0300311/* Common (grey or coloured) pinnacle PCTV remote handling
312 *
313 */
314static int get_key_pinnacle(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw,
315 int parity_offset, int marker, int code_modulo)
316{
317 unsigned char b[4];
318 unsigned int start = 0,parity = 0,code = 0;
319
320 /* poll IR chip */
Jean Delvarec668f322009-05-13 16:48:50 -0300321 if (4 != i2c_master_recv(ir->c, b, 4)) {
Mauro Carvalho Chehab1c22dad2008-07-17 22:31:29 -0300322 i2cdprintk("read error\n");
323 return -EIO;
324 }
325
326 for (start = 0; start < ARRAY_SIZE(b); start++) {
327 if (b[start] == marker) {
328 code=b[(start+parity_offset + 1) % 4];
329 parity=b[(start+parity_offset) % 4];
330 }
331 }
332
333 /* Empty Request */
334 if (parity == 0)
335 return 0;
336
337 /* Repeating... */
338 if (ir->old == parity)
339 return 0;
340
341 ir->old = parity;
342
343 /* drop special codes when a key is held down a long time for the grey controller
344 In this case, the second bit of the code is asserted */
345 if (marker == 0xfe && (code & 0x40))
346 return 0;
347
348 code %= code_modulo;
349
350 *ir_raw = code;
351 *ir_key = code;
352
353 i2cdprintk("Pinnacle PCTV key %02x\n", code);
354
355 return 1;
356}
357
358/* The grey pinnacle PCTV remote
359 *
360 * There are one issue with this remote:
361 * - I2c packet does not change when the same key is pressed quickly. The workaround
362 * is to hold down each key for about half a second, so that another code is generated
363 * in the i2c packet, and the function can distinguish key presses.
364 *
365 * Sylvain Pasche <sylvain.pasche@gmail.com>
366 */
367static int get_key_pinnacle_grey(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
368{
369
370 return get_key_pinnacle(ir, ir_key, ir_raw, 1, 0xfe, 0xff);
371}
372
373
374/* The new pinnacle PCTV remote (with the colored buttons)
375 *
376 * Ricardo Cerqueira <v4l@cerqueira.org>
377 */
378static int get_key_pinnacle_color(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
379{
380 /* code_modulo parameter (0x88) is used to reduce code value to fit inside IR_KEYTAB_SIZE
381 *
382 * this is the only value that results in 42 unique
383 * codes < 128
384 */
385
386 return get_key_pinnacle(ir, ir_key, ir_raw, 2, 0x80, 0x88);
387}
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389void saa7134_input_irq(struct saa7134_dev *dev)
390{
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300391 struct card_ir *ir;
392
393 if (!dev || !dev->remote)
394 return;
395
396 ir = dev->remote;
397 if (!ir->running)
398 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Mauro Carvalho Chehab8ea43d02010-11-13 16:49:10 -0300400 if (ir->nec_gpio) {
401 saa7134_nec_irq(dev);
402 } else if (!ir->polling && !ir->rc5_gpio && !ir->raw_decode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 build_key(dev);
Mauro Carvalho Chehab8ea43d02010-11-13 16:49:10 -0300404 } else if (ir->rc5_gpio) {
405 saa7134_rc5_irq(dev);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300406 } else if (ir->raw_decode) {
407 saa7134_raw_decode_irq(dev);
Hermann Pitton91607232006-12-07 21:45:28 -0300408 }
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
Mauro Carvalho Chehabe051f692010-09-29 13:46:23 -0300420static void ir_raw_decode_timer_end(unsigned long data)
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300421{
422 struct saa7134_dev *dev = (struct saa7134_dev *)data;
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -0300423 struct card_ir *ir = dev->remote;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300424
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300425 ir_raw_event_handle(dev->remote->dev);
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -0300426
427 ir->active = 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300428}
429
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300430static int __saa7134_ir_start(void *priv)
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300431{
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300432 struct saa7134_dev *dev = priv;
433 struct card_ir *ir;
434
435 if (!dev)
436 return -EINVAL;
437
438 ir = dev->remote;
439 if (!ir)
440 return -EINVAL;
441
Mauro Carvalho Chehab02108942010-03-20 00:25:37 -0300442 if (ir->running)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300443 return 0;
Mauro Carvalho Chehab02108942010-03-20 00:25:37 -0300444
445 ir->running = 1;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300446 if (ir->polling) {
Dmitry Torokhovb4ba7882007-05-21 11:41:02 -0300447 setup_timer(&ir->timer, saa7134_input_timer,
448 (unsigned long)dev);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300449 ir->timer.expires = jiffies + HZ;
450 add_timer(&ir->timer);
Mauro Carvalho Chehab8ea43d02010-11-13 16:49:10 -0300451 } else if (ir->rc5_gpio) {
452 /* set timer_end for code completion */
453 init_timer(&ir->timer_end);
454 ir->timer_end.function = ir_rc5_timer_end;
455 ir->timer_end.data = (unsigned long)ir;
456 ir->shift_by = 2;
457 ir->start = 0x2;
458 ir->addr = 0x17;
459 ir->rc5_remote_gap = ir_rc5_remote_gap;
460 } else if (ir->nec_gpio) {
461 tasklet_init(&ir->tlet, nec_task, (unsigned long)dev);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300462 } else if (ir->raw_decode) {
463 /* set timer_end for code completion */
464 init_timer(&ir->timer_end);
465 ir->timer_end.function = ir_raw_decode_timer_end;
466 ir->timer_end.data = (unsigned long)dev;
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -0300467 ir->active = 0;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300468 }
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300469
470 return 0;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300471}
472
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300473static void __saa7134_ir_stop(void *priv)
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300474{
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300475 struct saa7134_dev *dev = priv;
476 struct card_ir *ir;
477
478 if (!dev)
479 return;
480
481 ir = dev->remote;
482 if (!ir)
483 return;
Mauro Carvalho Chehab02108942010-03-20 00:25:37 -0300484
485 if (!ir->running)
486 return;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300487 if (dev->remote->polling)
488 del_timer_sync(&dev->remote->timer);
Mauro Carvalho Chehab8ea43d02010-11-13 16:49:10 -0300489 else if (ir->rc5_gpio)
490 del_timer_sync(&ir->timer_end);
491 else if (ir->nec_gpio)
492 tasklet_kill(&ir->tlet);
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -0300493 else if (ir->raw_decode) {
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300494 del_timer_sync(&ir->timer_end);
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -0300495 ir->active = 0;
496 }
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300497
Mauro Carvalho Chehab02108942010-03-20 00:25:37 -0300498 ir->running = 0;
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300499
500 return;
Mauro Carvalho Chehab02108942010-03-20 00:25:37 -0300501}
502
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300503int saa7134_ir_start(struct saa7134_dev *dev)
504{
505 if (dev->remote->users)
506 return __saa7134_ir_start(dev);
507
508 return 0;
509}
510
511void saa7134_ir_stop(struct saa7134_dev *dev)
512{
513 if (dev->remote->users)
514 __saa7134_ir_stop(dev);
515}
516
David Härdemand8b4b582010-10-29 16:08:23 -0300517static int saa7134_ir_open(struct rc_dev *rc)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300518{
David Härdemand8b4b582010-10-29 16:08:23 -0300519 struct saa7134_dev *dev = rc->priv;
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300520
521 dev->remote->users++;
522 return __saa7134_ir_start(dev);
523}
524
David Härdemand8b4b582010-10-29 16:08:23 -0300525static void saa7134_ir_close(struct rc_dev *rc)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300526{
David Härdemand8b4b582010-10-29 16:08:23 -0300527 struct saa7134_dev *dev = rc->priv;
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300528
529 dev->remote->users--;
530 if (!dev->remote->users)
531 __saa7134_ir_stop(dev);
532}
533
Mauro Carvalho Chehab8ea43d02010-11-13 16:49:10 -0300534
535static int saa7134_ir_change_protocol(struct rc_dev *rc, u64 ir_type)
536{
537 struct saa7134_dev *dev = rc->priv;
538 struct card_ir *ir = dev->remote;
539 u32 nec_gpio, rc5_gpio;
540
541 if (ir_type == IR_TYPE_RC5) {
542 dprintk("Changing protocol to RC5\n");
543 nec_gpio = 0;
544 rc5_gpio = 1;
545 } else if (ir_type == IR_TYPE_NEC) {
546 dprintk("Changing protocol to NEC\n");
547 nec_gpio = 1;
548 rc5_gpio = 0;
549 } else {
550 dprintk("IR protocol type %ud is not supported\n",
551 (unsigned)ir_type);
552 return -EINVAL;
553 }
554
555 if (ir->running) {
556 saa7134_ir_stop(dev);
557 ir->nec_gpio = nec_gpio;
558 ir->rc5_gpio = rc5_gpio;
559 saa7134_ir_start(dev);
560 } else {
561 ir->nec_gpio = nec_gpio;
562 ir->rc5_gpio = rc5_gpio;
563 }
564
565 return 0;
566}
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568int saa7134_input_init1(struct saa7134_dev *dev)
569{
Hermann Pitton91607232006-12-07 21:45:28 -0300570 struct card_ir *ir;
David Härdemand8b4b582010-10-29 16:08:23 -0300571 struct rc_dev *rc;
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300572 char *ir_codes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 u32 mask_keycode = 0;
574 u32 mask_keydown = 0;
575 u32 mask_keyup = 0;
576 int polling = 0;
Mauro Carvalho Chehab8ea43d02010-11-13 16:49:10 -0300577 int rc5_gpio = 0;
578 int nec_gpio = 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300579 int raw_decode = 0;
Mauro Carvalho Chehab8ea43d02010-11-13 16:49:10 -0300580 int allow_protocol_change = 0;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300581 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Ricardo Cerqueiracb2444d2005-11-08 21:38:47 -0800583 if (dev->has_remote != SAA7134_REMOTE_GPIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return -ENODEV;
585 if (disable_ir)
586 return -ENODEV;
587
588 /* detect & configure */
589 switch (dev->board) {
590 case SAA7134_BOARD_FLYVIDEO2000:
591 case SAA7134_BOARD_FLYVIDEO3000:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800592 case SAA7134_BOARD_FLYTVPLATINUM_FM:
Arnaud Patard6af90ab2005-11-08 21:36:55 -0800593 case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
Eugene Yudin23389b82009-08-29 09:32:11 -0300594 case SAA7134_BOARD_ROVERMEDIA_LINK_PRO_FM:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300595 ir_codes = RC_MAP_FLYVIDEO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 mask_keycode = 0xEC00000;
597 mask_keydown = 0x0040000;
598 break;
599 case SAA7134_BOARD_CINERGY400:
600 case SAA7134_BOARD_CINERGY600:
601 case SAA7134_BOARD_CINERGY600_MK3:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300602 ir_codes = RC_MAP_CINERGY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 mask_keycode = 0x00003f;
604 mask_keyup = 0x040000;
605 break;
606 case SAA7134_BOARD_ECS_TVP3XP:
607 case SAA7134_BOARD_ECS_TVP3XP_4CB5:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300608 ir_codes = RC_MAP_EZTV;
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700609 mask_keycode = 0x00017c;
610 mask_keyup = 0x000002;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 polling = 50; // ms
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700612 break;
613 case SAA7134_BOARD_KWORLD_XPERT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 case SAA7134_BOARD_AVACSSMARTTV:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300615 ir_codes = RC_MAP_PIXELVIEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 mask_keycode = 0x00001F;
617 mask_keyup = 0x000020;
618 polling = 50; // ms
619 break;
620 case SAA7134_BOARD_MD2819:
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700621 case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 case SAA7134_BOARD_AVERMEDIA_305:
623 case SAA7134_BOARD_AVERMEDIA_307:
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700624 case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
Vasiliy Temnikov5a5e1da2009-08-26 22:10:55 -0300625 case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700626 case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
Mikhail Fedotov3ac706d2006-10-06 20:23:47 -0300627 case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
Andy Shevchenkodf0dbbe2009-04-08 14:01:19 -0300628 case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700629 case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
Albert Grahamd2761f22007-12-09 09:44:38 -0300630 case SAA7134_BOARD_AVERMEDIA_M102:
Pham Thanh Nam6a2d8022008-12-30 23:26:09 -0300631 case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300632 ir_codes = RC_MAP_AVERMEDIA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 mask_keycode = 0x0007C8;
634 mask_keydown = 0x000010;
635 polling = 50; // ms
636 /* Set GPIO pin2 to high to enable the IR controller */
637 saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
638 saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
639 break;
Mauro Carvalho Chehab36f6bb92008-06-26 17:03:00 -0300640 case SAA7134_BOARD_AVERMEDIA_M135A:
Herton Ronaldo Krzesinski63fc31e2010-05-10 15:43:31 -0300641 ir_codes = RC_MAP_AVERMEDIA_M135A;
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300642 mask_keydown = 0x0040000; /* Enable GPIO18 line on both edges */
643 mask_keyup = 0x0040000;
Mauro Carvalho Chehabd152b8b2010-03-20 00:23:30 -0300644 mask_keycode = 0xffff;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300645 raw_decode = 1;
Mauro Carvalho Chehab36f6bb92008-06-26 17:03:00 -0300646 break;
Herton Ronaldo Krzesinski9e1d9e72010-05-08 02:23:37 -0300647 case SAA7134_BOARD_AVERMEDIA_M733A:
648 ir_codes = RC_MAP_AVERMEDIA_M733A_RM_K6;
649 mask_keydown = 0x0040000;
650 mask_keyup = 0x0040000;
651 mask_keycode = 0xffff;
652 raw_decode = 1;
653 break;
pasky@ucw.cz450efcf2006-11-12 14:22:32 -0300654 case SAA7134_BOARD_AVERMEDIA_777:
pasky@ucw.cz29e0f1a2006-11-12 14:23:32 -0300655 case SAA7134_BOARD_AVERMEDIA_A16AR:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300656 ir_codes = RC_MAP_AVERMEDIA;
pasky@ucw.cz450efcf2006-11-12 14:22:32 -0300657 mask_keycode = 0x02F200;
658 mask_keydown = 0x000400;
659 polling = 50; // ms
660 /* Without this we won't receive key up events */
661 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
662 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
Linus Torvalds4dd74062006-11-13 09:50:11 -0800663 break;
Tim Farrington6e501a32008-06-15 13:33:42 -0300664 case SAA7134_BOARD_AVERMEDIA_A16D:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300665 ir_codes = RC_MAP_AVERMEDIA_A16D;
Tim Farrington6e501a32008-06-15 13:33:42 -0300666 mask_keycode = 0x02F200;
667 mask_keydown = 0x000400;
668 polling = 50; /* ms */
669 /* Without this we won't receive key up events */
670 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
671 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
672 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800673 case SAA7134_BOARD_KWORLD_TERMINATOR:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300674 ir_codes = RC_MAP_PIXELVIEW;
James R. Webbdc2286c2005-11-08 21:37:00 -0800675 mask_keycode = 0x00001f;
676 mask_keyup = 0x000060;
677 polling = 50; // ms
678 break;
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700679 case SAA7134_BOARD_MANLI_MTV001:
680 case SAA7134_BOARD_MANLI_MTV002:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300681 ir_codes = RC_MAP_MANLI;
Dmitry Belimovb34dddb2008-04-23 14:09:08 -0300682 mask_keycode = 0x001f00;
683 mask_keyup = 0x004000;
684 polling = 50; /* ms */
685 break;
Nickolay V. Shmyreva8ff4172005-11-08 21:36:16 -0800686 case SAA7134_BOARD_BEHOLD_409FM:
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300687 case SAA7134_BOARD_BEHOLD_401:
688 case SAA7134_BOARD_BEHOLD_403:
689 case SAA7134_BOARD_BEHOLD_403FM:
690 case SAA7134_BOARD_BEHOLD_405:
691 case SAA7134_BOARD_BEHOLD_405FM:
692 case SAA7134_BOARD_BEHOLD_407:
693 case SAA7134_BOARD_BEHOLD_407FM:
694 case SAA7134_BOARD_BEHOLD_409:
695 case SAA7134_BOARD_BEHOLD_505FM:
Dmitri Belimov9c6f97a2009-12-21 02:00:38 -0300696 case SAA7134_BOARD_BEHOLD_505RDS_MK5:
697 case SAA7134_BOARD_BEHOLD_505RDS_MK3:
Andrey J. Melnikoff (TEMHOTA)e8018c92008-01-07 05:17:39 -0300698 case SAA7134_BOARD_BEHOLD_507_9FM:
Dmitri Belimov84d728c2009-04-23 02:32:49 -0300699 case SAA7134_BOARD_BEHOLD_507RDS_MK3:
700 case SAA7134_BOARD_BEHOLD_507RDS_MK5:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300701 ir_codes = RC_MAP_MANLI;
Dmitry Belimovb34dddb2008-04-23 14:09:08 -0300702 mask_keycode = 0x003f00;
703 mask_keyup = 0x004000;
704 polling = 50; /* ms */
705 break;
706 case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300707 ir_codes = RC_MAP_BEHOLD_COLUMBUS;
Dmitry Belimovb34dddb2008-04-23 14:09:08 -0300708 mask_keycode = 0x003f00;
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700709 mask_keyup = 0x004000;
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700710 polling = 50; // ms
711 break;
Ricardo Cerqueira17ce1ff2005-11-08 21:38:47 -0800712 case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300713 ir_codes = RC_MAP_PCTV_SEDNA;
Pavel Mihaylovc3d93192005-11-08 21:38:43 -0800714 mask_keycode = 0x001f00;
715 mask_keyup = 0x004000;
716 polling = 50; // ms
717 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800718 case SAA7134_BOARD_GOTVIEW_7135:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300719 ir_codes = RC_MAP_GOTVIEW7135;
Pedro0938e312007-10-17 17:58:40 -0300720 mask_keycode = 0x0003CC;
Nickolay V. Shmyrev6b961442005-11-08 21:36:22 -0800721 mask_keydown = 0x000010;
Pedro0938e312007-10-17 17:58:40 -0300722 polling = 5; /* ms */
723 saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
Nickolay V. Shmyrev6b961442005-11-08 21:36:22 -0800724 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 case SAA7134_BOARD_VIDEOMATE_TV_PVR:
Nickolay V. Shmyrev2a9a9a82006-01-09 15:25:33 -0200726 case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700727 case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300728 ir_codes = RC_MAP_VIDEOMATE_TV_PVR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 mask_keycode = 0x00003F;
730 mask_keyup = 0x400000;
731 polling = 50; // ms
732 break;
Michal Majchrowiczb04c1ba2006-09-13 16:42:42 -0300733 case SAA7134_BOARD_PROTEUS_2309:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300734 ir_codes = RC_MAP_PROTEUS_2309;
Michal Majchrowiczb04c1ba2006-09-13 16:42:42 -0300735 mask_keycode = 0x00007F;
736 mask_keyup = 0x000080;
737 polling = 50; // ms
738 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800739 case SAA7134_BOARD_VIDEOMATE_DVBT_300:
740 case SAA7134_BOARD_VIDEOMATE_DVBT_200:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300741 ir_codes = RC_MAP_VIDEOMATE_TV_PVR;
Nickolay V. Shmyrevfea095f2005-11-08 21:36:47 -0800742 mask_keycode = 0x003F00;
743 mask_keyup = 0x040000;
744 break;
James Le Cuirotf5c965a2007-07-02 12:53:25 -0300745 case SAA7134_BOARD_FLYDVBS_LR300:
Giampiero Giancipoli3d8466e2006-02-07 06:49:09 -0200746 case SAA7134_BOARD_FLYDVBT_LR301:
Rudo Thomasa8029172006-02-27 00:08:46 -0300747 case SAA7134_BOARD_FLYDVBTDUO:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300748 ir_codes = RC_MAP_FLYDVB;
Giampiero Giancipoli3d8466e2006-02-07 06:49:09 -0200749 mask_keycode = 0x0001F00;
750 mask_keydown = 0x0040000;
751 break;
Hermann Pitton91607232006-12-07 21:45:28 -0300752 case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
Ed Vipas904ab882007-03-29 18:32:49 -0300753 case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
Mauro Carvalho Chehabdfb4ba12009-08-20 10:13:40 -0300754 case SAA7134_BOARD_ASUSTeK_P7131_ANALOG:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300755 ir_codes = RC_MAP_ASUS_PC39;
Mauro Carvalho Chehabfe8b67132010-09-30 14:46:47 -0300756 mask_keydown = 0x0040000; /* Enable GPIO18 line on both edges */
757 mask_keyup = 0x0040000;
758 mask_keycode = 0xffff;
759 raw_decode = 1;
Hermann Pitton91607232006-12-07 21:45:28 -0300760 break;
Juan Pablo Sormanic36c4592006-12-27 12:46:36 -0300761 case SAA7134_BOARD_ENCORE_ENLTV:
762 case SAA7134_BOARD_ENCORE_ENLTV_FM:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300763 ir_codes = RC_MAP_ENCORE_ENLTV;
Juan Pablo Sormanic36c4592006-12-27 12:46:36 -0300764 mask_keycode = 0x00007f;
765 mask_keyup = 0x040000;
766 polling = 50; // ms
767 break;
Mauro Carvalho Chehabbf1ece62008-06-26 17:03:00 -0300768 case SAA7134_BOARD_ENCORE_ENLTV_FM53:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300769 ir_codes = RC_MAP_ENCORE_ENLTV_FM53;
Mauro Carvalho Chehab8ea43d02010-11-13 16:49:10 -0300770 mask_keydown = 0x0040000;
771 mask_keycode = 0x00007f;
772 nec_gpio = 1;
Mauro Carvalho Chehabbf1ece62008-06-26 17:03:00 -0300773 break;
Tony Wan480f75a2007-05-11 11:33:50 -0300774 case SAA7134_BOARD_10MOONSTVMASTER3:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300775 ir_codes = RC_MAP_ENCORE_ENLTV;
Tony Wan480f75a2007-05-11 11:33:50 -0300776 mask_keycode = 0x5f80000;
777 mask_keyup = 0x8000000;
778 polling = 50; //ms
779 break;
Adrian Pardinif0ba3562008-02-11 12:40:53 -0300780 case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300781 ir_codes = RC_MAP_GENIUS_TVGO_A11MCE;
Adrian Pardinif0ba3562008-02-11 12:40:53 -0300782 mask_keycode = 0xff;
783 mask_keydown = 0xf00000;
784 polling = 50; /* ms */
785 break;
Mauro Carvalho Chehab9b000192008-06-26 17:03:00 -0300786 case SAA7134_BOARD_REAL_ANGEL_220:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300787 ir_codes = RC_MAP_REAL_AUDIO_220_32_KEYS;
Mauro Carvalho Chehab9b000192008-06-26 17:03:00 -0300788 mask_keycode = 0x3f00;
789 mask_keyup = 0x4000;
790 polling = 50; /* ms */
791 break;
Mauro Carvalho Chehab26d5f3a2008-12-07 13:19:29 -0300792 case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300793 ir_codes = RC_MAP_KWORLD_PLUS_TV_ANALOG;
Mauro Carvalho Chehab26d5f3a2008-12-07 13:19:29 -0300794 mask_keycode = 0x7f;
795 polling = 40; /* ms */
796 break;
Igor M. Liplianinecfcfec2009-08-13 21:42:21 -0300797 case SAA7134_BOARD_VIDEOMATE_S350:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300798 ir_codes = RC_MAP_VIDEOMATE_S350;
Igor M. Liplianinecfcfec2009-08-13 21:42:21 -0300799 mask_keycode = 0x003f00;
800 mask_keydown = 0x040000;
801 break;
Michael Obst0700ade2009-10-31 14:05:42 -0300802 case SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S:
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300803 ir_codes = RC_MAP_WINFAST;
Michael Obst0700ade2009-10-31 14:05:42 -0300804 mask_keycode = 0x5f00;
805 mask_keyup = 0x020000;
Michael Krufky117e1342009-11-01 11:16:10 -0300806 polling = 50; /* ms */
Michael Obst0700ade2009-10-31 14:05:42 -0300807 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809 if (NULL == ir_codes) {
810 printk("%s: Oops: IR config error [card=%d]\n",
811 dev->name, dev->board);
812 return -ENODEV;
813 }
814
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500815 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
David Härdemand8b4b582010-10-29 16:08:23 -0300816 rc = rc_allocate_device();
817 if (!ir || !rc) {
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300818 err = -ENOMEM;
819 goto err_out_free;
Dmitry Torokhovb7df3912005-09-15 02:01:53 -0500820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
David Härdemand8b4b582010-10-29 16:08:23 -0300822 ir->dev = rc;
Mauro Carvalho Chehab02108942010-03-20 00:25:37 -0300823 dev->remote = ir;
824
825 ir->running = 0;
Dmitry Torokhovd271d1c2005-11-20 00:56:54 -0500826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 /* init hardware-specific stuff */
828 ir->mask_keycode = mask_keycode;
829 ir->mask_keydown = mask_keydown;
830 ir->mask_keyup = mask_keyup;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800831 ir->polling = polling;
Mauro Carvalho Chehab8ea43d02010-11-13 16:49:10 -0300832 ir->rc5_gpio = rc5_gpio;
833 ir->nec_gpio = nec_gpio;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300834 ir->raw_decode = raw_decode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 /* init input device */
837 snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
838 saa7134_boards[dev->board].name);
839 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
840 pci_name(dev->pci));
841
David Härdemand8b4b582010-10-29 16:08:23 -0300842 rc->priv = dev;
843 rc->open = saa7134_ir_open;
844 rc->close = saa7134_ir_close;
Mauro Carvalho Chehab626cf692010-04-06 23:21:46 -0300845 if (raw_decode)
David Härdemand8b4b582010-10-29 16:08:23 -0300846 rc->driver_type = RC_DRIVER_IR_RAW;
Mauro Carvalho Chehab626cf692010-04-06 23:21:46 -0300847
Mauro Carvalho Chehab8ea43d02010-11-13 16:49:10 -0300848 if (!raw_decode && allow_protocol_change) {
849 rc->allowed_protos = IR_TYPE_RC5 | IR_TYPE_NEC;
850 rc->change_protocol = saa7134_ir_change_protocol;
851 }
852
David Härdemand8b4b582010-10-29 16:08:23 -0300853 rc->input_name = ir->name;
854 rc->input_phys = ir->phys;
855 rc->input_id.bustype = BUS_PCI;
856 rc->input_id.version = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (dev->pci->subsystem_vendor) {
David Härdemand8b4b582010-10-29 16:08:23 -0300858 rc->input_id.vendor = dev->pci->subsystem_vendor;
859 rc->input_id.product = dev->pci->subsystem_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 } else {
David Härdemand8b4b582010-10-29 16:08:23 -0300861 rc->input_id.vendor = dev->pci->vendor;
862 rc->input_id.product = dev->pci->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
David Härdemand8b4b582010-10-29 16:08:23 -0300864 rc->dev.parent = &dev->pci->dev;
865 rc->map_name = ir_codes;
866 rc->driver_name = MODULE_NAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
David Härdemand8b4b582010-10-29 16:08:23 -0300868 err = rc_register_device(rc);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300869 if (err)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300870 goto err_out_free;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return 0;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300873
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300874err_out_free:
David Härdemand8b4b582010-10-29 16:08:23 -0300875 rc_free_device(rc);
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300876 dev->remote = NULL;
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300877 kfree(ir);
878 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
881void saa7134_input_fini(struct saa7134_dev *dev)
882{
883 if (NULL == dev->remote)
884 return;
885
Dmitry Torokhovb07b4782006-11-20 10:23:04 -0300886 saa7134_ir_stop(dev);
David Härdemand8b4b582010-10-29 16:08:23 -0300887 rc_unregister_device(dev->remote->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 kfree(dev->remote);
889 dev->remote = NULL;
890}
891
Jean Delvarec668f322009-05-13 16:48:50 -0300892void saa7134_probe_i2c_ir(struct saa7134_dev *dev)
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800893{
Jean Delvare43e16ea2009-10-02 05:47:08 -0300894 struct i2c_board_info info;
Jean Delvarec668f322009-05-13 16:48:50 -0300895
Jean Delvarec668f322009-05-13 16:48:50 -0300896 struct i2c_msg msg_msi = {
897 .addr = 0x50,
898 .flags = I2C_M_RD,
899 .len = 0,
900 .buf = NULL,
901 };
902
Jean Delvarec668f322009-05-13 16:48:50 -0300903 int rc;
904
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800905 if (disable_ir) {
Jean Delvarec668f322009-05-13 16:48:50 -0300906 dprintk("IR has been disabled, not probing for i2c remote\n");
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800907 return;
908 }
909
Jean Delvare43e16ea2009-10-02 05:47:08 -0300910 memset(&info, 0, sizeof(struct i2c_board_info));
Mauro Carvalho Chehab7aedd5ec2009-09-07 02:22:01 -0300911 memset(&dev->init_data, 0, sizeof(dev->init_data));
Jean Delvare43e16ea2009-10-02 05:47:08 -0300912 strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300913
914 switch (dev->board) {
915 case SAA7134_BOARD_PINNACLE_PCTV_110i:
916 case SAA7134_BOARD_PINNACLE_PCTV_310i:
Mauro Carvalho Chehab7aedd5ec2009-09-07 02:22:01 -0300917 dev->init_data.name = "Pinnacle PCTV";
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300918 if (pinnacle_remote == 0) {
Mauro Carvalho Chehab7aedd5ec2009-09-07 02:22:01 -0300919 dev->init_data.get_key = get_key_pinnacle_color;
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300920 dev->init_data.ir_codes = RC_MAP_PINNACLE_COLOR;
Jean Delvare43e16ea2009-10-02 05:47:08 -0300921 info.addr = 0x47;
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300922 } else {
Mauro Carvalho Chehab7aedd5ec2009-09-07 02:22:01 -0300923 dev->init_data.get_key = get_key_pinnacle_grey;
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300924 dev->init_data.ir_codes = RC_MAP_PINNACLE_GREY;
Jean Delvare43e16ea2009-10-02 05:47:08 -0300925 info.addr = 0x47;
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300926 }
927 break;
928 case SAA7134_BOARD_UPMOST_PURPLE_TV:
Mauro Carvalho Chehab7aedd5ec2009-09-07 02:22:01 -0300929 dev->init_data.name = "Purple TV";
930 dev->init_data.get_key = get_key_purpletv;
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300931 dev->init_data.ir_codes = RC_MAP_PURPLETV;
Jean Delvareaef02aa2009-10-02 08:47:22 -0300932 info.addr = 0x7a;
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300933 break;
934 case SAA7134_BOARD_MSI_TVATANYWHERE_PLUS:
Mauro Carvalho Chehab7aedd5ec2009-09-07 02:22:01 -0300935 dev->init_data.name = "MSI TV@nywhere Plus";
936 dev->init_data.get_key = get_key_msi_tvanywhere_plus;
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300937 dev->init_data.ir_codes = RC_MAP_MSI_TVANYWHERE_PLUS;
Mauro Carvalho Chehabc72ba8e2010-09-23 01:23:10 -0300938 /*
939 * MSI TV@nyware Plus requires more frequent polling
940 * otherwise it will miss some keypresses
941 */
942 dev->init_data.polling_interval = 50;
Jean Delvare43e16ea2009-10-02 05:47:08 -0300943 info.addr = 0x30;
Jean Delvareec218a42009-05-13 16:51:46 -0300944 /* MSI TV@nywhere Plus controller doesn't seem to
945 respond to probes unless we read something from
946 an existing device. Weird...
947 REVISIT: might no longer be needed */
948 rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
Joe Perchesc429e7b2010-10-25 23:44:25 -0300949 dprintk("probe 0x%02x @ %s: %s\n",
Jean Delvareec218a42009-05-13 16:51:46 -0300950 msg_msi.addr, dev->i2c_adap.name,
951 (1 == rc) ? "yes" : "no");
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300952 break;
953 case SAA7134_BOARD_HAUPPAUGE_HVR1110:
Mauro Carvalho Chehab7aedd5ec2009-09-07 02:22:01 -0300954 dev->init_data.name = "HVR 1110";
955 dev->init_data.get_key = get_key_hvr1110;
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300956 dev->init_data.ir_codes = RC_MAP_HAUPPAUGE_NEW;
Jean Delvare30093e82009-10-02 09:48:04 -0300957 info.addr = 0x71;
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300958 break;
959 case SAA7134_BOARD_BEHOLD_607FM_MK3:
960 case SAA7134_BOARD_BEHOLD_607FM_MK5:
961 case SAA7134_BOARD_BEHOLD_609FM_MK3:
962 case SAA7134_BOARD_BEHOLD_609FM_MK5:
963 case SAA7134_BOARD_BEHOLD_607RDS_MK3:
964 case SAA7134_BOARD_BEHOLD_607RDS_MK5:
965 case SAA7134_BOARD_BEHOLD_609RDS_MK3:
966 case SAA7134_BOARD_BEHOLD_609RDS_MK5:
967 case SAA7134_BOARD_BEHOLD_M6:
968 case SAA7134_BOARD_BEHOLD_M63:
969 case SAA7134_BOARD_BEHOLD_M6_EXTRA:
970 case SAA7134_BOARD_BEHOLD_H6:
Dmitri Belimov2012c87f2009-08-26 01:01:12 -0300971 case SAA7134_BOARD_BEHOLD_X7:
Dmitri Belimov0faa2ed2010-04-06 01:00:05 -0300972 case SAA7134_BOARD_BEHOLD_H7:
973 case SAA7134_BOARD_BEHOLD_A7:
Mauro Carvalho Chehab7aedd5ec2009-09-07 02:22:01 -0300974 dev->init_data.name = "BeholdTV";
975 dev->init_data.get_key = get_key_beholdm6xx;
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300976 dev->init_data.ir_codes = RC_MAP_BEHOLD;
Dmitri Belimov2ffbb642010-03-04 02:40:37 -0300977 dev->init_data.type = IR_TYPE_NEC;
Jean Delvare30093e82009-10-02 09:48:04 -0300978 info.addr = 0x2d;
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300979 break;
Jean Delvared9a88e62009-05-13 16:52:44 -0300980 case SAA7134_BOARD_AVERMEDIA_CARDBUS_501:
981 case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
Jean Delvare43e16ea2009-10-02 05:47:08 -0300982 info.addr = 0x40;
Jean Delvared9a88e62009-05-13 16:52:44 -0300983 break;
Lukas Karasd995a182009-11-24 12:06:52 -0300984 case SAA7134_BOARD_FLYDVB_TRIO:
985 dev->init_data.name = "FlyDVB Trio";
986 dev->init_data.get_key = get_key_flydvb_trio;
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300987 dev->init_data.ir_codes = RC_MAP_FLYDVB;
Lukas Karasd995a182009-11-24 12:06:52 -0300988 info.addr = 0x0b;
989 break;
Jean Delvare30093e82009-10-02 09:48:04 -0300990 default:
991 dprintk("No I2C IR support for board %x\n", dev->board);
992 return;
Jean Delvare4d7a2d62009-05-13 16:49:32 -0300993 }
994
Mauro Carvalho Chehab7aedd5ec2009-09-07 02:22:01 -0300995 if (dev->init_data.name)
Jean Delvare43e16ea2009-10-02 05:47:08 -0300996 info.platform_data = &dev->init_data;
Jean Delvare30093e82009-10-02 09:48:04 -0300997 i2c_new_device(&dev->i2c_adap, &info);
Jean Delvarec668f322009-05-13 16:48:50 -0300998}
999
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -03001000static int saa7134_raw_decode_irq(struct saa7134_dev *dev)
1001{
1002 struct card_ir *ir = dev->remote;
1003 unsigned long timeout;
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -03001004 int space;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -03001005
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -03001006 /* Generate initial event */
1007 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1008 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -03001009 space = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
David Härdeman724e2492010-04-08 13:10:00 -03001010 ir_raw_event_store_edge(dev->remote->dev, space ? IR_SPACE : IR_PULSE);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -03001011
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -03001012
1013 /*
1014 * Wait 15 ms from the start of the first IR event before processing
1015 * the event. This time is enough for NEC protocol. May need adjustments
1016 * to work with other protocols.
1017 */
1018 if (!ir->active) {
1019 timeout = jiffies + jiffies_to_msecs(15);
1020 mod_timer(&ir->timer_end, timeout);
1021 ir->active = 1;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -03001022 }
1023
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -03001024 return 1;
1025}
Mauro Carvalho Chehab8ea43d02010-11-13 16:49:10 -03001026
1027static int saa7134_rc5_irq(struct saa7134_dev *dev)
1028{
1029 struct card_ir *ir = dev->remote;
1030 struct timeval tv;
1031 u32 gap;
1032 unsigned long current_jiffies, timeout;
1033
1034 /* get time of bit */
1035 current_jiffies = jiffies;
1036 do_gettimeofday(&tv);
1037
1038 /* avoid overflow with gap >1s */
1039 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
1040 gap = 200000;
1041 } else {
1042 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
1043 tv.tv_usec - ir->base_time.tv_usec;
1044 }
1045
1046 /* active code => add bit */
1047 if (ir->active) {
1048 /* only if in the code (otherwise spurious IRQ or timer
1049 late) */
1050 if (ir->last_bit < 28) {
1051 ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
1052 ir_rc5_remote_gap;
1053 ir->code |= 1 << ir->last_bit;
1054 }
1055 /* starting new code */
1056 } else {
1057 ir->active = 1;
1058 ir->code = 0;
1059 ir->base_time = tv;
1060 ir->last_bit = 0;
1061
1062 timeout = current_jiffies + (500 + 30 * HZ) / 1000;
1063 mod_timer(&ir->timer_end, timeout);
1064 }
1065
1066 return 1;
1067}
1068
1069static void nec_task(unsigned long data)
1070{
1071 struct saa7134_dev *dev = (struct saa7134_dev *) data;
1072 struct card_ir *ir;
1073 struct timeval tv;
1074 int count, pulse, oldpulse, gap;
1075 u32 ircode = 0, not_code = 0;
1076 int ngap = 0;
1077
1078 if (!data) {
1079 printk(KERN_ERR "saa713x/ir: Can't recover dev struct\n");
1080 /* GPIO will be kept disabled */
1081 return;
1082 }
1083
1084 ir = dev->remote;
1085
1086 /* rising SAA7134_GPIO_GPRESCAN reads the status */
1087 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1088 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1089
1090 oldpulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
1091 pulse = oldpulse;
1092
1093 do_gettimeofday(&tv);
1094 ir->base_time = tv;
1095
1096 /* Decode NEC pulsecode. This code can take up to 76.5 ms to run.
1097 Unfortunately, using IRQ to decode pulse didn't work, since it uses
1098 a pulse train of 38KHz. This means one pulse on each 52 us
1099 */
1100 do {
1101 /* Wait until the end of pulse/space or 5 ms */
1102 for (count = 0; count < 500; count++) {
1103 udelay(10);
1104 /* rising SAA7134_GPIO_GPRESCAN reads the status */
1105 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1106 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1107 pulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2)
1108 & ir->mask_keydown;
1109 if (pulse != oldpulse)
1110 break;
1111 }
1112
1113 do_gettimeofday(&tv);
1114 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
1115 tv.tv_usec - ir->base_time.tv_usec;
1116
1117 if (!pulse) {
1118 /* Bit 0 has 560 us, while bit 1 has 1120 us.
1119 Do something only if bit == 1
1120 */
1121 if (ngap && (gap > 560 + 280)) {
1122 unsigned int shift = ngap - 1;
1123
1124 /* Address first, then command */
1125 if (shift < 8) {
1126 shift += 8;
1127 ircode |= 1 << shift;
1128 } else if (shift < 16) {
1129 not_code |= 1 << shift;
1130 } else if (shift < 24) {
1131 shift -= 16;
1132 ircode |= 1 << shift;
1133 } else {
1134 shift -= 24;
1135 not_code |= 1 << shift;
1136 }
1137 }
1138 ngap++;
1139 }
1140
1141
1142 ir->base_time = tv;
1143
1144 /* TIMEOUT - Long pulse */
1145 if (gap >= 5000)
1146 break;
1147 oldpulse = pulse;
1148 } while (ngap < 32);
1149
1150 if (ngap == 32) {
1151 /* FIXME: should check if not_code == ~ircode */
1152 ir->code = ir_extract_bits(ircode, ir->mask_keycode);
1153
1154 dprintk("scancode = 0x%02x (code = 0x%02x, notcode= 0x%02x)\n",
1155 ir->code, ircode, not_code);
1156
1157 ir_keydown(ir->dev, ir->code, 0);
1158 } else {
1159 dprintk("Repeat last key\n");
1160 ir_repeat(ir->dev);
1161 }
1162
1163 saa_setl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18_P);
1164}
1165
1166static int saa7134_nec_irq(struct saa7134_dev *dev)
1167{
1168 struct card_ir *ir = dev->remote;
1169
1170 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18_P);
1171 tasklet_schedule(&ir->tlet);
1172
1173 return 1;
1174}