saa7134-input.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. *
  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. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/input.h>
  25. #include "saa7134-reg.h"
  26. #include "saa7134.h"
  27. static unsigned int disable_ir = 0;
  28. module_param(disable_ir, int, 0444);
  29. MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
  30. static unsigned int ir_debug = 0;
  31. module_param(ir_debug, int, 0644);
  32. MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
  33. static int pinnacle_remote = 0;
  34. module_param(pinnacle_remote, int, 0644); /* Choose Pinnacle PCTV remote */
  35. MODULE_PARM_DESC(pinnacle_remote, "Specify Pinnacle PCTV remote: 0=coloured, 1=grey (defaults to 0)");
  36. static int ir_rc5_remote_gap = 885;
  37. module_param(ir_rc5_remote_gap, int, 0644);
  38. static int ir_rc5_key_timeout = 115;
  39. module_param(ir_rc5_key_timeout, int, 0644);
  40. static int repeat_delay = 500;
  41. module_param(repeat_delay, int, 0644);
  42. MODULE_PARM_DESC(repeat_delay, "delay before key repeat started");
  43. static int repeat_period = 33;
  44. module_param(repeat_period, int, 0644);
  45. MODULE_PARM_DESC(repeat_period, "repeat period between "
  46. "keypresses when key is down");
  47. #define dprintk(fmt, arg...) if (ir_debug) \
  48. printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
  49. #define i2cdprintk(fmt, arg...) if (ir_debug) \
  50. printk(KERN_DEBUG "%s/ir: " fmt, ir->c.name , ## arg)
  51. /** rc5 functions */
  52. static int saa7134_rc5_irq(struct saa7134_dev *dev);
  53. /* -------------------- GPIO generic keycode builder -------------------- */
  54. static int build_key(struct saa7134_dev *dev)
  55. {
  56. struct card_ir *ir = dev->remote;
  57. u32 gpio, data;
  58. /* here comes the additional handshake steps for some cards */
  59. switch (dev->board) {
  60. case SAA7134_BOARD_GOTVIEW_7135:
  61. saa_setb(SAA7134_GPIO_GPSTATUS1, 0x80);
  62. saa_clearb(SAA7134_GPIO_GPSTATUS1, 0x80);
  63. break;
  64. }
  65. /* rising SAA7134_GPIO_GPRESCAN reads the status */
  66. saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
  67. saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
  68. gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
  69. if (ir->polling) {
  70. if (ir->last_gpio == gpio)
  71. return 0;
  72. ir->last_gpio = gpio;
  73. }
  74. data = ir_extract_bits(gpio, ir->mask_keycode);
  75. dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
  76. gpio, ir->mask_keycode, data);
  77. if (ir->polling) {
  78. if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
  79. (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
  80. ir_input_keydown(ir->dev, &ir->ir, data, data);
  81. } else {
  82. ir_input_nokey(ir->dev, &ir->ir);
  83. }
  84. }
  85. else { /* IRQ driven mode - handle key press and release in one go */
  86. if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
  87. (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
  88. ir_input_keydown(ir->dev, &ir->ir, data, data);
  89. ir_input_nokey(ir->dev, &ir->ir);
  90. }
  91. }
  92. return 0;
  93. }
  94. /* --------------------- Chip specific I2C key builders ----------------- */
  95. static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  96. {
  97. unsigned char b;
  98. /* poll IR chip */
  99. if (1 != i2c_master_recv(&ir->c,&b,1)) {
  100. i2cdprintk("read error\n");
  101. return -EIO;
  102. }
  103. /* no button press */
  104. if (b==0)
  105. return 0;
  106. /* repeating */
  107. if (b & 0x80)
  108. return 1;
  109. *ir_key = b;
  110. *ir_raw = b;
  111. return 1;
  112. }
  113. static int get_key_hvr1110(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
  114. {
  115. unsigned char buf[5], cod4, code3, code4;
  116. /* poll IR chip */
  117. if (5 != i2c_master_recv(&ir->c,buf,5))
  118. return -EIO;
  119. cod4 = buf[4];
  120. code4 = (cod4 >> 2);
  121. code3 = buf[3];
  122. if (code3 == 0)
  123. /* no key pressed */
  124. return 0;
  125. /* return key */
  126. *ir_key = code4;
  127. *ir_raw = code4;
  128. return 1;
  129. }
  130. void saa7134_input_irq(struct saa7134_dev *dev)
  131. {
  132. struct card_ir *ir = dev->remote;
  133. if (!ir->polling && !ir->rc5_gpio) {
  134. build_key(dev);
  135. } else if (ir->rc5_gpio) {
  136. saa7134_rc5_irq(dev);
  137. }
  138. }
  139. static void saa7134_input_timer(unsigned long data)
  140. {
  141. struct saa7134_dev *dev = (struct saa7134_dev *)data;
  142. struct card_ir *ir = dev->remote;
  143. build_key(dev);
  144. mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
  145. }
  146. void saa7134_ir_start(struct saa7134_dev *dev, struct card_ir *ir)
  147. {
  148. if (ir->polling) {
  149. setup_timer(&ir->timer, saa7134_input_timer,
  150. (unsigned long)dev);
  151. ir->timer.expires = jiffies + HZ;
  152. add_timer(&ir->timer);
  153. } else if (ir->rc5_gpio) {
  154. /* set timer_end for code completion */
  155. init_timer(&ir->timer_end);
  156. ir->timer_end.function = ir_rc5_timer_end;
  157. ir->timer_end.data = (unsigned long)ir;
  158. init_timer(&ir->timer_keyup);
  159. ir->timer_keyup.function = ir_rc5_timer_keyup;
  160. ir->timer_keyup.data = (unsigned long)ir;
  161. ir->shift_by = 2;
  162. ir->start = 0x2;
  163. ir->addr = 0x17;
  164. ir->rc5_key_timeout = ir_rc5_key_timeout;
  165. ir->rc5_remote_gap = ir_rc5_remote_gap;
  166. }
  167. }
  168. void saa7134_ir_stop(struct saa7134_dev *dev)
  169. {
  170. if (dev->remote->polling)
  171. del_timer_sync(&dev->remote->timer);
  172. }
  173. int saa7134_input_init1(struct saa7134_dev *dev)
  174. {
  175. struct card_ir *ir;
  176. struct input_dev *input_dev;
  177. IR_KEYTAB_TYPE *ir_codes = NULL;
  178. u32 mask_keycode = 0;
  179. u32 mask_keydown = 0;
  180. u32 mask_keyup = 0;
  181. int polling = 0;
  182. int rc5_gpio = 0;
  183. int ir_type = IR_TYPE_OTHER;
  184. int err;
  185. if (dev->has_remote != SAA7134_REMOTE_GPIO)
  186. return -ENODEV;
  187. if (disable_ir)
  188. return -ENODEV;
  189. /* detect & configure */
  190. switch (dev->board) {
  191. case SAA7134_BOARD_FLYVIDEO2000:
  192. case SAA7134_BOARD_FLYVIDEO3000:
  193. case SAA7134_BOARD_FLYTVPLATINUM_FM:
  194. case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
  195. ir_codes = ir_codes_flyvideo;
  196. mask_keycode = 0xEC00000;
  197. mask_keydown = 0x0040000;
  198. break;
  199. case SAA7134_BOARD_CINERGY400:
  200. case SAA7134_BOARD_CINERGY600:
  201. case SAA7134_BOARD_CINERGY600_MK3:
  202. ir_codes = ir_codes_cinergy;
  203. mask_keycode = 0x00003f;
  204. mask_keyup = 0x040000;
  205. break;
  206. case SAA7134_BOARD_ECS_TVP3XP:
  207. case SAA7134_BOARD_ECS_TVP3XP_4CB5:
  208. ir_codes = ir_codes_eztv;
  209. mask_keycode = 0x00017c;
  210. mask_keyup = 0x000002;
  211. polling = 50; // ms
  212. break;
  213. case SAA7134_BOARD_KWORLD_XPERT:
  214. case SAA7134_BOARD_AVACSSMARTTV:
  215. ir_codes = ir_codes_pixelview;
  216. mask_keycode = 0x00001F;
  217. mask_keyup = 0x000020;
  218. polling = 50; // ms
  219. break;
  220. case SAA7134_BOARD_MD2819:
  221. case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
  222. case SAA7134_BOARD_AVERMEDIA_305:
  223. case SAA7134_BOARD_AVERMEDIA_307:
  224. case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
  225. case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
  226. case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
  227. case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
  228. case SAA7134_BOARD_AVERMEDIA_M102:
  229. ir_codes = ir_codes_avermedia;
  230. mask_keycode = 0x0007C8;
  231. mask_keydown = 0x000010;
  232. polling = 50; // ms
  233. /* Set GPIO pin2 to high to enable the IR controller */
  234. saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
  235. saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
  236. break;
  237. case SAA7134_BOARD_AVERMEDIA_777:
  238. case SAA7134_BOARD_AVERMEDIA_A16AR:
  239. ir_codes = ir_codes_avermedia;
  240. mask_keycode = 0x02F200;
  241. mask_keydown = 0x000400;
  242. polling = 50; // ms
  243. /* Without this we won't receive key up events */
  244. saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
  245. saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
  246. break;
  247. case SAA7134_BOARD_KWORLD_TERMINATOR:
  248. ir_codes = ir_codes_pixelview;
  249. mask_keycode = 0x00001f;
  250. mask_keyup = 0x000060;
  251. polling = 50; // ms
  252. break;
  253. case SAA7134_BOARD_MANLI_MTV001:
  254. case SAA7134_BOARD_MANLI_MTV002:
  255. case SAA7134_BOARD_BEHOLD_409FM:
  256. ir_codes = ir_codes_manli;
  257. mask_keycode = 0x001f00;
  258. mask_keyup = 0x004000;
  259. polling = 50; // ms
  260. break;
  261. case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
  262. ir_codes = ir_codes_pctv_sedna;
  263. mask_keycode = 0x001f00;
  264. mask_keyup = 0x004000;
  265. polling = 50; // ms
  266. break;
  267. case SAA7134_BOARD_GOTVIEW_7135:
  268. ir_codes = ir_codes_gotview7135;
  269. mask_keycode = 0x0003CC;
  270. mask_keydown = 0x000010;
  271. polling = 5; /* ms */
  272. saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
  273. break;
  274. case SAA7134_BOARD_VIDEOMATE_TV_PVR:
  275. case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
  276. case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
  277. ir_codes = ir_codes_videomate_tv_pvr;
  278. mask_keycode = 0x00003F;
  279. mask_keyup = 0x400000;
  280. polling = 50; // ms
  281. break;
  282. case SAA7134_BOARD_PROTEUS_2309:
  283. ir_codes = ir_codes_proteus_2309;
  284. mask_keycode = 0x00007F;
  285. mask_keyup = 0x000080;
  286. polling = 50; // ms
  287. break;
  288. case SAA7134_BOARD_VIDEOMATE_DVBT_300:
  289. case SAA7134_BOARD_VIDEOMATE_DVBT_200:
  290. ir_codes = ir_codes_videomate_tv_pvr;
  291. mask_keycode = 0x003F00;
  292. mask_keyup = 0x040000;
  293. break;
  294. case SAA7134_BOARD_FLYDVBS_LR300:
  295. case SAA7134_BOARD_FLYDVBT_LR301:
  296. case SAA7134_BOARD_FLYDVBTDUO:
  297. ir_codes = ir_codes_flydvb;
  298. mask_keycode = 0x0001F00;
  299. mask_keydown = 0x0040000;
  300. break;
  301. case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
  302. case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
  303. ir_codes = ir_codes_asus_pc39;
  304. mask_keydown = 0x0040000;
  305. rc5_gpio = 1;
  306. break;
  307. case SAA7134_BOARD_ENCORE_ENLTV:
  308. case SAA7134_BOARD_ENCORE_ENLTV_FM:
  309. ir_codes = ir_codes_encore_enltv;
  310. mask_keycode = 0x00007f;
  311. mask_keyup = 0x040000;
  312. polling = 50; // ms
  313. break;
  314. case SAA7134_BOARD_10MOONSTVMASTER3:
  315. ir_codes = ir_codes_encore_enltv;
  316. mask_keycode = 0x5f80000;
  317. mask_keyup = 0x8000000;
  318. polling = 50; //ms
  319. break;
  320. }
  321. if (NULL == ir_codes) {
  322. printk("%s: Oops: IR config error [card=%d]\n",
  323. dev->name, dev->board);
  324. return -ENODEV;
  325. }
  326. ir = kzalloc(sizeof(*ir), GFP_KERNEL);
  327. input_dev = input_allocate_device();
  328. if (!ir || !input_dev) {
  329. err = -ENOMEM;
  330. goto err_out_free;
  331. }
  332. ir->dev = input_dev;
  333. /* init hardware-specific stuff */
  334. ir->mask_keycode = mask_keycode;
  335. ir->mask_keydown = mask_keydown;
  336. ir->mask_keyup = mask_keyup;
  337. ir->polling = polling;
  338. ir->rc5_gpio = rc5_gpio;
  339. /* init input device */
  340. snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
  341. saa7134_boards[dev->board].name);
  342. snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
  343. pci_name(dev->pci));
  344. ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
  345. input_dev->name = ir->name;
  346. input_dev->phys = ir->phys;
  347. input_dev->id.bustype = BUS_PCI;
  348. input_dev->id.version = 1;
  349. if (dev->pci->subsystem_vendor) {
  350. input_dev->id.vendor = dev->pci->subsystem_vendor;
  351. input_dev->id.product = dev->pci->subsystem_device;
  352. } else {
  353. input_dev->id.vendor = dev->pci->vendor;
  354. input_dev->id.product = dev->pci->device;
  355. }
  356. input_dev->dev.parent = &dev->pci->dev;
  357. dev->remote = ir;
  358. saa7134_ir_start(dev, ir);
  359. err = input_register_device(ir->dev);
  360. if (err)
  361. goto err_out_stop;
  362. /* the remote isn't as bouncy as a keyboard */
  363. ir->dev->rep[REP_DELAY] = repeat_delay;
  364. ir->dev->rep[REP_PERIOD] = repeat_period;
  365. return 0;
  366. err_out_stop:
  367. saa7134_ir_stop(dev);
  368. dev->remote = NULL;
  369. err_out_free:
  370. input_free_device(input_dev);
  371. kfree(ir);
  372. return err;
  373. }
  374. void saa7134_input_fini(struct saa7134_dev *dev)
  375. {
  376. if (NULL == dev->remote)
  377. return;
  378. saa7134_ir_stop(dev);
  379. input_unregister_device(dev->remote->dev);
  380. kfree(dev->remote);
  381. dev->remote = NULL;
  382. }
  383. void saa7134_set_i2c_ir(struct saa7134_dev *dev, struct IR_i2c *ir)
  384. {
  385. if (disable_ir) {
  386. dprintk("Found supported i2c remote, but IR has been disabled\n");
  387. ir->get_key=NULL;
  388. return;
  389. }
  390. switch (dev->board) {
  391. case SAA7134_BOARD_PINNACLE_PCTV_110i:
  392. case SAA7134_BOARD_PINNACLE_PCTV_310i:
  393. snprintf(ir->c.name, sizeof(ir->c.name), "Pinnacle PCTV");
  394. if (pinnacle_remote == 0) {
  395. ir->get_key = get_key_pinnacle_color;
  396. ir->ir_codes = ir_codes_pinnacle_color;
  397. } else {
  398. ir->get_key = get_key_pinnacle_grey;
  399. ir->ir_codes = ir_codes_pinnacle_grey;
  400. }
  401. break;
  402. case SAA7134_BOARD_UPMOST_PURPLE_TV:
  403. snprintf(ir->c.name, sizeof(ir->c.name), "Purple TV");
  404. ir->get_key = get_key_purpletv;
  405. ir->ir_codes = ir_codes_purpletv;
  406. break;
  407. case SAA7134_BOARD_HAUPPAUGE_HVR1110:
  408. snprintf(ir->c.name, sizeof(ir->c.name), "HVR 1110");
  409. ir->get_key = get_key_hvr1110;
  410. ir->ir_codes = ir_codes_hauppauge_new;
  411. break;
  412. default:
  413. dprintk("Shouldn't get here: Unknown board %x for I2C IR?\n",dev->board);
  414. break;
  415. }
  416. }
  417. static int saa7134_rc5_irq(struct saa7134_dev *dev)
  418. {
  419. struct card_ir *ir = dev->remote;
  420. struct timeval tv;
  421. u32 gap;
  422. unsigned long current_jiffies, timeout;
  423. /* get time of bit */
  424. current_jiffies = jiffies;
  425. do_gettimeofday(&tv);
  426. /* avoid overflow with gap >1s */
  427. if (tv.tv_sec - ir->base_time.tv_sec > 1) {
  428. gap = 200000;
  429. } else {
  430. gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
  431. tv.tv_usec - ir->base_time.tv_usec;
  432. }
  433. /* active code => add bit */
  434. if (ir->active) {
  435. /* only if in the code (otherwise spurious IRQ or timer
  436. late) */
  437. if (ir->last_bit < 28) {
  438. ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
  439. ir_rc5_remote_gap;
  440. ir->code |= 1 << ir->last_bit;
  441. }
  442. /* starting new code */
  443. } else {
  444. ir->active = 1;
  445. ir->code = 0;
  446. ir->base_time = tv;
  447. ir->last_bit = 0;
  448. timeout = current_jiffies + (500 + 30 * HZ) / 1000;
  449. mod_timer(&ir->timer_end, timeout);
  450. }
  451. return 1;
  452. }
  453. /* ----------------------------------------------------------------------
  454. * Local variables:
  455. * c-basic-offset: 8
  456. * End:
  457. */