saa7134-input.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. ir_codes = ir_codes_avermedia;
  229. mask_keycode = 0x0007C8;
  230. mask_keydown = 0x000010;
  231. polling = 50; // ms
  232. /* Set GPIO pin2 to high to enable the IR controller */
  233. saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
  234. saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
  235. break;
  236. case SAA7134_BOARD_AVERMEDIA_777:
  237. case SAA7134_BOARD_AVERMEDIA_A16AR:
  238. ir_codes = ir_codes_avermedia;
  239. mask_keycode = 0x02F200;
  240. mask_keydown = 0x000400;
  241. polling = 50; // ms
  242. /* Without this we won't receive key up events */
  243. saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
  244. saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
  245. break;
  246. case SAA7134_BOARD_KWORLD_TERMINATOR:
  247. ir_codes = ir_codes_pixelview;
  248. mask_keycode = 0x00001f;
  249. mask_keyup = 0x000060;
  250. polling = 50; // ms
  251. break;
  252. case SAA7134_BOARD_MANLI_MTV001:
  253. case SAA7134_BOARD_MANLI_MTV002:
  254. case SAA7134_BOARD_BEHOLD_409FM:
  255. ir_codes = ir_codes_manli;
  256. mask_keycode = 0x001f00;
  257. mask_keyup = 0x004000;
  258. polling = 50; // ms
  259. break;
  260. case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
  261. ir_codes = ir_codes_pctv_sedna;
  262. mask_keycode = 0x001f00;
  263. mask_keyup = 0x004000;
  264. polling = 50; // ms
  265. break;
  266. case SAA7134_BOARD_GOTVIEW_7135:
  267. ir_codes = ir_codes_gotview7135;
  268. mask_keycode = 0x0003CC;
  269. mask_keydown = 0x000010;
  270. polling = 5; /* ms */
  271. saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
  272. break;
  273. case SAA7134_BOARD_VIDEOMATE_TV_PVR:
  274. case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
  275. case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
  276. ir_codes = ir_codes_videomate_tv_pvr;
  277. mask_keycode = 0x00003F;
  278. mask_keyup = 0x400000;
  279. polling = 50; // ms
  280. break;
  281. case SAA7134_BOARD_PROTEUS_2309:
  282. ir_codes = ir_codes_proteus_2309;
  283. mask_keycode = 0x00007F;
  284. mask_keyup = 0x000080;
  285. polling = 50; // ms
  286. break;
  287. case SAA7134_BOARD_VIDEOMATE_DVBT_300:
  288. case SAA7134_BOARD_VIDEOMATE_DVBT_200:
  289. ir_codes = ir_codes_videomate_tv_pvr;
  290. mask_keycode = 0x003F00;
  291. mask_keyup = 0x040000;
  292. break;
  293. case SAA7134_BOARD_FLYDVBS_LR300:
  294. case SAA7134_BOARD_FLYDVBT_LR301:
  295. case SAA7134_BOARD_FLYDVBTDUO:
  296. ir_codes = ir_codes_flydvb;
  297. mask_keycode = 0x0001F00;
  298. mask_keydown = 0x0040000;
  299. break;
  300. case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
  301. case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
  302. ir_codes = ir_codes_asus_pc39;
  303. mask_keydown = 0x0040000;
  304. rc5_gpio = 1;
  305. break;
  306. case SAA7134_BOARD_ENCORE_ENLTV:
  307. case SAA7134_BOARD_ENCORE_ENLTV_FM:
  308. ir_codes = ir_codes_encore_enltv;
  309. mask_keycode = 0x00007f;
  310. mask_keyup = 0x040000;
  311. polling = 50; // ms
  312. break;
  313. case SAA7134_BOARD_10MOONSTVMASTER3:
  314. ir_codes = ir_codes_encore_enltv;
  315. mask_keycode = 0x5f80000;
  316. mask_keyup = 0x8000000;
  317. polling = 50; //ms
  318. break;
  319. }
  320. if (NULL == ir_codes) {
  321. printk("%s: Oops: IR config error [card=%d]\n",
  322. dev->name, dev->board);
  323. return -ENODEV;
  324. }
  325. ir = kzalloc(sizeof(*ir), GFP_KERNEL);
  326. input_dev = input_allocate_device();
  327. if (!ir || !input_dev) {
  328. err = -ENOMEM;
  329. goto err_out_free;
  330. }
  331. ir->dev = input_dev;
  332. /* init hardware-specific stuff */
  333. ir->mask_keycode = mask_keycode;
  334. ir->mask_keydown = mask_keydown;
  335. ir->mask_keyup = mask_keyup;
  336. ir->polling = polling;
  337. ir->rc5_gpio = rc5_gpio;
  338. /* init input device */
  339. snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
  340. saa7134_boards[dev->board].name);
  341. snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
  342. pci_name(dev->pci));
  343. ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
  344. input_dev->name = ir->name;
  345. input_dev->phys = ir->phys;
  346. input_dev->id.bustype = BUS_PCI;
  347. input_dev->id.version = 1;
  348. if (dev->pci->subsystem_vendor) {
  349. input_dev->id.vendor = dev->pci->subsystem_vendor;
  350. input_dev->id.product = dev->pci->subsystem_device;
  351. } else {
  352. input_dev->id.vendor = dev->pci->vendor;
  353. input_dev->id.product = dev->pci->device;
  354. }
  355. input_dev->dev.parent = &dev->pci->dev;
  356. dev->remote = ir;
  357. saa7134_ir_start(dev, ir);
  358. err = input_register_device(ir->dev);
  359. if (err)
  360. goto err_out_stop;
  361. /* the remote isn't as bouncy as a keyboard */
  362. ir->dev->rep[REP_DELAY] = repeat_delay;
  363. ir->dev->rep[REP_PERIOD] = repeat_period;
  364. return 0;
  365. err_out_stop:
  366. saa7134_ir_stop(dev);
  367. dev->remote = NULL;
  368. err_out_free:
  369. input_free_device(input_dev);
  370. kfree(ir);
  371. return err;
  372. }
  373. void saa7134_input_fini(struct saa7134_dev *dev)
  374. {
  375. if (NULL == dev->remote)
  376. return;
  377. saa7134_ir_stop(dev);
  378. input_unregister_device(dev->remote->dev);
  379. kfree(dev->remote);
  380. dev->remote = NULL;
  381. }
  382. void saa7134_set_i2c_ir(struct saa7134_dev *dev, struct IR_i2c *ir)
  383. {
  384. if (disable_ir) {
  385. dprintk("Found supported i2c remote, but IR has been disabled\n");
  386. ir->get_key=NULL;
  387. return;
  388. }
  389. switch (dev->board) {
  390. case SAA7134_BOARD_PINNACLE_PCTV_110i:
  391. case SAA7134_BOARD_PINNACLE_PCTV_310i:
  392. snprintf(ir->c.name, sizeof(ir->c.name), "Pinnacle PCTV");
  393. if (pinnacle_remote == 0) {
  394. ir->get_key = get_key_pinnacle_color;
  395. ir->ir_codes = ir_codes_pinnacle_color;
  396. } else {
  397. ir->get_key = get_key_pinnacle_grey;
  398. ir->ir_codes = ir_codes_pinnacle_grey;
  399. }
  400. break;
  401. case SAA7134_BOARD_UPMOST_PURPLE_TV:
  402. snprintf(ir->c.name, sizeof(ir->c.name), "Purple TV");
  403. ir->get_key = get_key_purpletv;
  404. ir->ir_codes = ir_codes_purpletv;
  405. break;
  406. case SAA7134_BOARD_HAUPPAUGE_HVR1110:
  407. snprintf(ir->c.name, sizeof(ir->c.name), "HVR 1110");
  408. ir->get_key = get_key_hvr1110;
  409. ir->ir_codes = ir_codes_hauppauge_new;
  410. break;
  411. default:
  412. dprintk("Shouldn't get here: Unknown board %x for I2C IR?\n",dev->board);
  413. break;
  414. }
  415. }
  416. static int saa7134_rc5_irq(struct saa7134_dev *dev)
  417. {
  418. struct card_ir *ir = dev->remote;
  419. struct timeval tv;
  420. u32 gap;
  421. unsigned long current_jiffies, timeout;
  422. /* get time of bit */
  423. current_jiffies = jiffies;
  424. do_gettimeofday(&tv);
  425. /* avoid overflow with gap >1s */
  426. if (tv.tv_sec - ir->base_time.tv_sec > 1) {
  427. gap = 200000;
  428. } else {
  429. gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
  430. tv.tv_usec - ir->base_time.tv_usec;
  431. }
  432. /* active code => add bit */
  433. if (ir->active) {
  434. /* only if in the code (otherwise spurious IRQ or timer
  435. late) */
  436. if (ir->last_bit < 28) {
  437. ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
  438. ir_rc5_remote_gap;
  439. ir->code |= 1 << ir->last_bit;
  440. }
  441. /* starting new code */
  442. } else {
  443. ir->active = 1;
  444. ir->code = 0;
  445. ir->base_time = tv;
  446. ir->last_bit = 0;
  447. timeout = current_jiffies + (500 + 30 * HZ) / 1000;
  448. mod_timer(&ir->timer_end, timeout);
  449. }
  450. return 1;
  451. }
  452. /* ----------------------------------------------------------------------
  453. * Local variables:
  454. * c-basic-offset: 8
  455. * End:
  456. */