saa7134-input.c 10 KB

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