saa7134-input.c 13 KB

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