saa7134-input.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. void saa7134_input_irq(struct saa7134_dev *dev)
  96. {
  97. struct saa7134_ir *ir = dev->remote;
  98. if (!ir->polling)
  99. build_key(dev);
  100. }
  101. static void saa7134_input_timer(unsigned long data)
  102. {
  103. struct saa7134_dev *dev = (struct saa7134_dev*)data;
  104. struct saa7134_ir *ir = dev->remote;
  105. unsigned long timeout;
  106. build_key(dev);
  107. timeout = jiffies + (ir->polling * HZ / 1000);
  108. mod_timer(&ir->timer, timeout);
  109. }
  110. int saa7134_input_init1(struct saa7134_dev *dev)
  111. {
  112. struct saa7134_ir *ir;
  113. struct input_dev *input_dev;
  114. IR_KEYTAB_TYPE *ir_codes = NULL;
  115. u32 mask_keycode = 0;
  116. u32 mask_keydown = 0;
  117. u32 mask_keyup = 0;
  118. int polling = 0;
  119. int ir_type = IR_TYPE_OTHER;
  120. if (dev->has_remote != SAA7134_REMOTE_GPIO)
  121. return -ENODEV;
  122. if (disable_ir)
  123. return -ENODEV;
  124. /* detect & configure */
  125. switch (dev->board) {
  126. case SAA7134_BOARD_FLYVIDEO2000:
  127. case SAA7134_BOARD_FLYVIDEO3000:
  128. case SAA7134_BOARD_FLYTVPLATINUM_FM:
  129. case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
  130. ir_codes = ir_codes_flyvideo;
  131. mask_keycode = 0xEC00000;
  132. mask_keydown = 0x0040000;
  133. break;
  134. case SAA7134_BOARD_CINERGY400:
  135. case SAA7134_BOARD_CINERGY600:
  136. case SAA7134_BOARD_CINERGY600_MK3:
  137. ir_codes = ir_codes_cinergy;
  138. mask_keycode = 0x00003f;
  139. mask_keyup = 0x040000;
  140. break;
  141. case SAA7134_BOARD_ECS_TVP3XP:
  142. case SAA7134_BOARD_ECS_TVP3XP_4CB5:
  143. ir_codes = ir_codes_eztv;
  144. mask_keycode = 0x00017c;
  145. mask_keyup = 0x000002;
  146. polling = 50; // ms
  147. break;
  148. case SAA7134_BOARD_KWORLD_XPERT:
  149. case SAA7134_BOARD_AVACSSMARTTV:
  150. ir_codes = ir_codes_pixelview;
  151. mask_keycode = 0x00001F;
  152. mask_keyup = 0x000020;
  153. polling = 50; // ms
  154. break;
  155. case SAA7134_BOARD_MD2819:
  156. case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
  157. case SAA7134_BOARD_AVERMEDIA_305:
  158. case SAA7134_BOARD_AVERMEDIA_307:
  159. case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
  160. case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
  161. case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
  162. ir_codes = ir_codes_avermedia;
  163. mask_keycode = 0x0007C8;
  164. mask_keydown = 0x000010;
  165. polling = 50; // ms
  166. /* Set GPIO pin2 to high to enable the IR controller */
  167. saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
  168. saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
  169. break;
  170. case SAA7134_BOARD_AVERMEDIA_777:
  171. case SAA7134_BOARD_AVERMEDIA_A16AR:
  172. ir_codes = ir_codes_avermedia;
  173. mask_keycode = 0x02F200;
  174. mask_keydown = 0x000400;
  175. polling = 50; // ms
  176. /* Without this we won't receive key up events */
  177. saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
  178. saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
  179. break;
  180. case SAA7134_BOARD_KWORLD_TERMINATOR:
  181. ir_codes = ir_codes_pixelview;
  182. mask_keycode = 0x00001f;
  183. mask_keyup = 0x000060;
  184. polling = 50; // ms
  185. break;
  186. case SAA7134_BOARD_MANLI_MTV001:
  187. case SAA7134_BOARD_MANLI_MTV002:
  188. case SAA7134_BOARD_BEHOLD_409FM:
  189. ir_codes = ir_codes_manli;
  190. mask_keycode = 0x001f00;
  191. mask_keyup = 0x004000;
  192. polling = 50; // ms
  193. break;
  194. case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
  195. ir_codes = ir_codes_pctv_sedna;
  196. mask_keycode = 0x001f00;
  197. mask_keyup = 0x004000;
  198. polling = 50; // ms
  199. break;
  200. case SAA7134_BOARD_GOTVIEW_7135:
  201. ir_codes = ir_codes_gotview7135;
  202. mask_keycode = 0x0003EC;
  203. mask_keyup = 0x008000;
  204. mask_keydown = 0x000010;
  205. polling = 50; // ms
  206. break;
  207. case SAA7134_BOARD_VIDEOMATE_TV_PVR:
  208. case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
  209. case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
  210. ir_codes = ir_codes_videomate_tv_pvr;
  211. mask_keycode = 0x00003F;
  212. mask_keyup = 0x400000;
  213. polling = 50; // ms
  214. break;
  215. case SAA7134_BOARD_PROTEUS_2309:
  216. ir_codes = ir_codes_proteus_2309;
  217. mask_keycode = 0x00007F;
  218. mask_keyup = 0x000080;
  219. polling = 50; // ms
  220. break;
  221. case SAA7134_BOARD_VIDEOMATE_DVBT_300:
  222. case SAA7134_BOARD_VIDEOMATE_DVBT_200:
  223. ir_codes = ir_codes_videomate_tv_pvr;
  224. mask_keycode = 0x003F00;
  225. mask_keyup = 0x040000;
  226. break;
  227. case SAA7134_BOARD_FLYDVBT_LR301:
  228. case SAA7134_BOARD_FLYDVBTDUO:
  229. ir_codes = ir_codes_flydvb;
  230. mask_keycode = 0x0001F00;
  231. mask_keydown = 0x0040000;
  232. break;
  233. }
  234. if (NULL == ir_codes) {
  235. printk("%s: Oops: IR config error [card=%d]\n",
  236. dev->name, dev->board);
  237. return -ENODEV;
  238. }
  239. ir = kzalloc(sizeof(*ir), GFP_KERNEL);
  240. input_dev = input_allocate_device();
  241. if (!ir || !input_dev) {
  242. kfree(ir);
  243. input_free_device(input_dev);
  244. return -ENOMEM;
  245. }
  246. ir->dev = input_dev;
  247. /* init hardware-specific stuff */
  248. ir->mask_keycode = mask_keycode;
  249. ir->mask_keydown = mask_keydown;
  250. ir->mask_keyup = mask_keyup;
  251. ir->polling = polling;
  252. /* init input device */
  253. snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
  254. saa7134_boards[dev->board].name);
  255. snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
  256. pci_name(dev->pci));
  257. ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
  258. input_dev->name = ir->name;
  259. input_dev->phys = ir->phys;
  260. input_dev->id.bustype = BUS_PCI;
  261. input_dev->id.version = 1;
  262. if (dev->pci->subsystem_vendor) {
  263. input_dev->id.vendor = dev->pci->subsystem_vendor;
  264. input_dev->id.product = dev->pci->subsystem_device;
  265. } else {
  266. input_dev->id.vendor = dev->pci->vendor;
  267. input_dev->id.product = dev->pci->device;
  268. }
  269. input_dev->cdev.dev = &dev->pci->dev;
  270. /* all done */
  271. dev->remote = ir;
  272. if (ir->polling) {
  273. init_timer(&ir->timer);
  274. ir->timer.function = saa7134_input_timer;
  275. ir->timer.data = (unsigned long)dev;
  276. ir->timer.expires = jiffies + HZ;
  277. add_timer(&ir->timer);
  278. }
  279. input_register_device(ir->dev);
  280. return 0;
  281. }
  282. void saa7134_input_fini(struct saa7134_dev *dev)
  283. {
  284. if (NULL == dev->remote)
  285. return;
  286. if (dev->remote->polling)
  287. del_timer_sync(&dev->remote->timer);
  288. input_unregister_device(dev->remote->dev);
  289. kfree(dev->remote);
  290. dev->remote = NULL;
  291. }
  292. void saa7134_set_i2c_ir(struct saa7134_dev *dev, struct IR_i2c *ir)
  293. {
  294. if (disable_ir) {
  295. dprintk("Found supported i2c remote, but IR has been disabled\n");
  296. ir->get_key=NULL;
  297. return;
  298. }
  299. switch (dev->board) {
  300. case SAA7134_BOARD_PINNACLE_PCTV_110i:
  301. snprintf(ir->c.name, sizeof(ir->c.name), "Pinnacle PCTV");
  302. if (pinnacle_remote == 0) {
  303. ir->get_key = get_key_pinnacle_color;
  304. ir->ir_codes = ir_codes_pinnacle_color;
  305. } else {
  306. ir->get_key = get_key_pinnacle_grey;
  307. ir->ir_codes = ir_codes_pinnacle_grey;
  308. }
  309. break;
  310. case SAA7134_BOARD_UPMOST_PURPLE_TV:
  311. snprintf(ir->c.name, sizeof(ir->c.name), "Purple TV");
  312. ir->get_key = get_key_purpletv;
  313. ir->ir_codes = ir_codes_purpletv;
  314. break;
  315. default:
  316. dprintk("Shouldn't get here: Unknown board %x for I2C IR?\n",dev->board);
  317. break;
  318. }
  319. }
  320. /* ----------------------------------------------------------------------
  321. * Local variables:
  322. * c-basic-offset: 8
  323. * End:
  324. */