irreceiver.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * linux/drivers/input/irremote/virtual_remote.c
  3. *
  4. * Virtual Keypad Driver
  5. *
  6. * Copyright (C) 2010 Amlogic Corporation
  7. *
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. * author : geng.li
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/types.h>
  28. #include <linux/input.h>
  29. #include <linux/kernel.h>
  30. #include <linux/delay.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/mutex.h>
  33. #include <linux/errno.h>
  34. #include <asm/irq.h>
  35. #include <asm/io.h>
  36. #include <mach/am_regs.h>
  37. #include <linux/slab.h>
  38. #include <linux/major.h>
  39. #include <asm/uaccess.h>
  40. #include <linux/cdev.h>
  41. #include <mach/pinmux.h>
  42. #include <mach/gpio.h>
  43. #include <linux/irq.h>
  44. #include "irreceiver.h"
  45. #define DEBUG
  46. //#ifdef DEBUG
  47. #define dbg(fmt, args...) printk(KERN_INFO "ir_receiver: " fmt, ##args)
  48. //#else
  49. //#define dbg(fmt, args...)
  50. //#endif
  51. #define DEVICE_NAME "irreceiver"
  52. #define DEIVE_COUNT 32
  53. #define BUFFER_SIZE 4096
  54. static dev_t irreceiver_id;
  55. static struct class *irreceiver_class;
  56. static struct device *irreceiver_dev;
  57. static struct cdev irreceiver_device;
  58. static int pwm_level = 24000000/38000/2; // 24M/38K/2
  59. static int pwmSwitch = 0;
  60. static struct ir_window rec_win, send_win;
  61. static int rec_idx, send_idx;
  62. static unsigned int last_jiffies;
  63. static unsigned long int time_count = 0; //10us time base
  64. static unsigned long int shot_time = 0;
  65. static char logbuf[4096];
  66. static ssize_t dbg_operate(struct device * dev, struct device_attribute *attr, const char * buf, size_t count)
  67. {
  68. dbg("Enter dbg mode ...\n");
  69. if(!strncmp(buf, "v", 1)) {
  70. printk("Test printk\n");
  71. }
  72. else if(!strncmp(buf, "t", 1)) {
  73. printk("Test printk!!!!\n");
  74. }
  75. return 16;
  76. }
  77. static ssize_t switch_write(struct device * dev, struct device_attribute *attr, const char * buf, size_t count)
  78. {
  79. int val = simple_strtoul(buf, NULL, 0);
  80. dbg("switch write [%d]\n", val);
  81. if(val == 0)//off
  82. {
  83. CLEAR_CBUS_REG_MASK(PWM_MISC_REG_CD, (1 << 1));
  84. }
  85. else //on
  86. {
  87. SET_CBUS_REG_MASK(PWM_MISC_REG_CD, (1 << 1));
  88. }
  89. return count;
  90. }
  91. static void init_pwm_d(void)
  92. {
  93. CLEAR_CBUS_REG_MASK(PWM_MISC_REG_CD, (1 << 1));//close PWM_D by default
  94. msleep(100);
  95. CLEAR_CBUS_REG_MASK(PERIPHS_PIN_MUX_11, (1<<23));
  96. CLEAR_CBUS_REG_MASK(PERIPHS_PIN_MUX_5, (1<<23));
  97. CLEAR_CBUS_REG_MASK(PERIPHS_PIN_MUX_6, (1<<11));
  98. CLEAR_CBUS_REG_MASK(PERIPHS_PIN_MUX_8, (1<<12));
  99. CLEAR_CBUS_REG_MASK(PERIPHS_PIN_MUX_8, (1<<13));
  100. CLEAR_CBUS_REG_MASK(PERIPHS_PIN_MUX_8, (1<<14));
  101. SET_CBUS_REG_MASK(PERIPHS_PIN_MUX_7, (1<<20));
  102. msleep(100);
  103. WRITE_CBUS_REG_BITS(PWM_PWM_D, pwm_level, 0, 16); //low
  104. WRITE_CBUS_REG_BITS(PWM_PWM_D, pwm_level, 16, 16); //hi
  105. }
  106. static void set_timer_b_event(unsigned long t)
  107. {
  108. shot_time = t;
  109. time_count = 0;
  110. }
  111. static void oneshot_event(void)
  112. {
  113. shot_time = 0;
  114. if(pwmSwitch == 0)
  115. {
  116. strcat(logbuf, "open\n");
  117. pwmSwitch = 1;
  118. SET_CBUS_REG_MASK(PWM_MISC_REG_CD, (1 << 1));//on
  119. }
  120. else
  121. {
  122. strcat(logbuf, "close\n");
  123. pwmSwitch = 0;
  124. CLEAR_CBUS_REG_MASK(PWM_MISC_REG_CD, (1 << 1));//off
  125. }
  126. //set next window timer
  127. send_idx++;
  128. if(send_idx < send_win.winNum)
  129. {
  130. //dbg("delay time[%d] and trgger next event\n", send_win.winArray[send_idx]);
  131. set_timer_b_event(send_win.winArray[send_idx]);
  132. }
  133. else //pwm should be off
  134. {
  135. //dbg("off pwm\n");
  136. CLEAR_CBUS_REG_MASK(PWM_MISC_REG_CD, (1 << 1));
  137. pwmSwitch = 0;
  138. }
  139. }
  140. static void timer_b_interrupt(void)
  141. {
  142. char tmp[128];
  143. time_count++;
  144. if(shot_time !=0 && (time_count*10) >= shot_time)
  145. {
  146. //sprintf(tmp, "[%lu][%lu]\n", shot_time, time_count*10);
  147. strcat(logbuf, tmp);
  148. oneshot_event();
  149. }
  150. }
  151. static void init_timer_b(void)
  152. {
  153. printk(KERN_INFO "init_timer_b\n");
  154. CLEAR_CBUS_REG_MASK(ISA_TIMER_MUX, TIMER_B_INPUT_MASK);
  155. SET_CBUS_REG_MASK(ISA_TIMER_MUX, TIMER_UNIT_10us << TIMER_B_INPUT_BIT);
  156. /* Set up the fiq handler */
  157. request_fiq(INT_TIMER_B, &timer_b_interrupt);
  158. }
  159. static ssize_t show_key_value(struct device * dev, struct device_attribute *attr, char * buf)
  160. {
  161. int i = 0;
  162. char tmp[10];
  163. memset(buf, 0, PAGE_SIZE);
  164. sprintf(tmp, "num=%d\n", rec_win.winNum);
  165. strcat(buf, tmp);
  166. for(i=0; i<rec_win.winNum; i++)
  167. {
  168. sprintf(tmp, "[%d]", rec_win.winArray[i]);
  169. strcat(buf, tmp);
  170. }
  171. strcat(buf, "\n");
  172. return strlen(buf);
  173. }
  174. static ssize_t show_log(struct device * dev, struct device_attribute *attr, char * buf)
  175. {
  176. memset(buf, 0, PAGE_SIZE);
  177. strcpy(buf, logbuf);
  178. return strlen(buf);
  179. }
  180. static DEVICE_ATTR(debug, S_IWUSR | S_IRUGO, NULL, dbg_operate);
  181. static DEVICE_ATTR(switch, S_IWUSR | S_IRUGO, NULL, switch_write);
  182. static DEVICE_ATTR(keyvalue, S_IWUSR | S_IRUGO, show_key_value, NULL);
  183. static DEVICE_ATTR(log, S_IWUSR | S_IRUGO, show_log, NULL);
  184. static int aml_ir_receiver_open(struct inode *inode, struct file *file)
  185. {
  186. dbg("aml_ir_receiver_open()\n");
  187. return 0;
  188. }
  189. static int aml_ir_receiver_ioctl(struct inode *inode, struct file *filp,
  190. unsigned int cmd, unsigned long args)
  191. {
  192. int i;
  193. s32 r = 0;
  194. unsigned long flags;
  195. void __user *argp = (void __user *)args;
  196. dbg("aml_ir_receiver_ioctl()\n");
  197. switch(cmd)
  198. {
  199. case IRRECEIVER_IOC_SEND:
  200. if (copy_from_user(&send_win, argp, sizeof(struct ir_window)))
  201. return -EFAULT;
  202. for(i=0; i<send_win.winNum; i++)
  203. dbg("idx[%d]:[%d]\n", i, send_win.winArray[i]);
  204. logbuf[0] = 0;
  205. send_idx = 0;
  206. pwmSwitch = 1;
  207. SET_CBUS_REG_MASK(PWM_MISC_REG_CD, (1 << 1));
  208. if(send_idx < send_win.winNum)
  209. {
  210. local_irq_save(flags);
  211. set_timer_b_event(send_win.winArray[send_idx]);
  212. local_irq_restore(flags);
  213. }
  214. break;
  215. case IRRECEIVER_IOC_RECV:
  216. dbg("recv win [%d]\n", rec_win.winNum);
  217. if(copy_to_user(argp, &rec_win, sizeof(struct ir_window)))
  218. return -EFAULT;
  219. break;
  220. case IRRECEIVER_IOC_STUDY_S:
  221. dbg("IRRECEIVER_IOC_STUDY_S\n");
  222. rec_win.winNum = 0;
  223. break;
  224. case IRRECEIVER_IOC_STUDY_E:
  225. dbg("IRRECEIVER_IOC_STUDY_E\n");
  226. break;
  227. default:
  228. r = -ENOIOCTLCMD;
  229. break;
  230. }
  231. return r;
  232. }
  233. static int aml_ir_receiver_release(struct inode *inode, struct file *file)
  234. {
  235. dbg("aml_ir_receiver_release()\n");
  236. file->private_data = NULL;
  237. return 0;
  238. }
  239. static const struct file_operations aml_ir_receiver_fops = {
  240. .owner = THIS_MODULE,
  241. .open = aml_ir_receiver_open,
  242. .ioctl = aml_ir_receiver_ioctl,
  243. .release = aml_ir_receiver_release,
  244. };
  245. static void ir_fiq_interrupt(void)
  246. {
  247. int pulse_width;
  248. unsigned int current_jiffies = jiffies;
  249. if(current_jiffies - last_jiffies > 10)//means a new key
  250. {
  251. //dbg("it is a new ir key\n");
  252. rec_idx = 0;
  253. rec_win.winNum = 0;
  254. last_jiffies = current_jiffies;
  255. return;//ignore first falling or rising edge
  256. }
  257. last_jiffies = current_jiffies;
  258. pulse_width = ( (am_remote_read_reg(AM_IR_DEC_REG1)) & 0x1FFF0000 ) >> 16 ;
  259. rec_idx++;
  260. if(rec_idx >= MAX_PLUSE)
  261. return;
  262. rec_win.winNum = rec_idx;
  263. rec_win.winArray[rec_idx-1] = (pulse_width<<1);
  264. //dbg("idx[%d]window width[%d]\n", rec_idx-1, pulse_width);
  265. }
  266. static void ir_hardware_init(void)
  267. {
  268. unsigned int control_value;
  269. rec_idx = 0;
  270. last_jiffies = 0xffffffff;
  271. //mask--mux gpio_e21 to remote
  272. set_mio_mux(5, 1<<31);
  273. //max frame time is 80ms, base rate is 2us
  274. control_value = 3<<28|(0x9c40 << 12)|0x1;
  275. am_remote_write_reg(AM_IR_DEC_REG0, control_value);
  276. /*[3-2]rising or falling edge detected
  277. [8-7]Measure mode
  278. */
  279. control_value = 0x8574;
  280. am_remote_write_reg(AM_IR_DEC_REG1, control_value);
  281. request_fiq(INT_REMOTE, &ir_fiq_interrupt);
  282. }
  283. static int __init aml_ir_receiver_probe(struct platform_device *pdev)
  284. {
  285. int r;
  286. dbg("ir receiver probe\n");
  287. r = alloc_chrdev_region(&irreceiver_id, 0, DEIVE_COUNT, DEVICE_NAME);
  288. if (r < 0) {
  289. printk(KERN_ERR "Can't register major for ir receiver device\n");
  290. return r;
  291. }
  292. irreceiver_class = class_create(THIS_MODULE, DEVICE_NAME);
  293. if (IS_ERR(irreceiver_class)) {
  294. unregister_chrdev_region(irreceiver_id, DEIVE_COUNT);
  295. printk(KERN_ERR "Can't create class for ir receiver device\n");
  296. return -1;
  297. }
  298. cdev_init(&irreceiver_device, &aml_ir_receiver_fops);
  299. irreceiver_device.owner = THIS_MODULE;
  300. cdev_add(&(irreceiver_device), irreceiver_id, DEIVE_COUNT);
  301. irreceiver_dev = device_create(irreceiver_class, NULL, irreceiver_id, NULL, "irreceiver%d", 0); //kernel>=2.6.27
  302. if (irreceiver_dev == NULL) {
  303. dbg("irreceiver_dev create error\n");
  304. class_destroy(irreceiver_class);
  305. return -EEXIST;
  306. }
  307. device_create_file(irreceiver_dev, &dev_attr_debug);
  308. device_create_file(irreceiver_dev, &dev_attr_switch);
  309. device_create_file(irreceiver_dev, &dev_attr_keyvalue);
  310. device_create_file(irreceiver_dev, &dev_attr_log);
  311. ir_hardware_init();
  312. init_pwm_d();
  313. init_timer_b();
  314. return 0;
  315. }
  316. static int aml_ir_receiver_remove(struct platform_device *pdev)
  317. {
  318. dbg("remove IR Receiver\n");
  319. /* unregister everything */
  320. free_fiq(INT_REMOTE, &ir_fiq_interrupt);
  321. free_fiq(INT_TIMER_B, &timer_b_interrupt);
  322. /* Remove the cdev */
  323. device_remove_file(irreceiver_dev, &dev_attr_debug);
  324. device_remove_file(irreceiver_dev, &dev_attr_switch);
  325. device_remove_file(irreceiver_dev, &dev_attr_keyvalue);
  326. device_remove_file(irreceiver_dev, &dev_attr_log);
  327. cdev_del(&irreceiver_device);
  328. device_destroy(irreceiver_class, irreceiver_id);
  329. class_destroy(irreceiver_class);
  330. unregister_chrdev_region(irreceiver_id, DEIVE_COUNT);
  331. return 0;
  332. }
  333. static struct platform_driver aml_ir_receiver_driver = {
  334. .probe = aml_ir_receiver_probe,
  335. .remove = aml_ir_receiver_remove,
  336. .suspend = NULL,
  337. .resume = NULL,
  338. .driver = {
  339. .name = DEVICE_NAME,
  340. .owner = THIS_MODULE,
  341. },
  342. };
  343. static struct platform_device* aml_ir_receiver_device = NULL;
  344. static int __devinit aml_ir_receiver_init(void)
  345. {
  346. dbg("IR Receiver Driver for Hisense\n");
  347. aml_ir_receiver_device = platform_device_alloc(DEVICE_NAME,0);
  348. if (!aml_ir_receiver_device) {
  349. dbg("failed to alloc aml_ir_receiver_device\n");
  350. return -ENOMEM;
  351. }
  352. if(platform_device_add(aml_ir_receiver_device)){
  353. platform_device_put(aml_ir_receiver_device);
  354. dbg("failed to add aml_ir_receiver_device\n");
  355. return -ENODEV;
  356. }
  357. if (platform_driver_register(&aml_ir_receiver_driver)) {
  358. dbg("failed to register aml_ir_receiver_driver module\n");
  359. platform_device_del(aml_ir_receiver_device);
  360. platform_device_put(aml_ir_receiver_device);
  361. return -ENODEV;
  362. }
  363. return 0;
  364. }
  365. static void __exit aml_ir_receiver_exit(void)
  366. {
  367. dbg("IR Receiver exit \n");
  368. platform_driver_unregister(&aml_ir_receiver_driver);
  369. platform_device_unregister(aml_ir_receiver_device);
  370. aml_ir_receiver_device = NULL;
  371. }
  372. module_init(aml_ir_receiver_init);
  373. module_exit(aml_ir_receiver_exit);
  374. MODULE_AUTHOR("geng.li");
  375. MODULE_DESCRIPTION("IR Receiver Driver");
  376. MODULE_LICENSE("GPL");