saa7134-input.c 11 KB

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