am_remote.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. /*
  2. * linux/drivers/input/irremote/remote_kbd.c
  3. *
  4. * Remote Driver
  5. *
  6. * Copyright (C) 2009 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 : jianfeng_wang
  23. */
  24. /*
  25. * !!caution: if you use remote ,you should disable card1 used for ata_enable pin.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/irq.h>
  31. #include <linux/types.h>
  32. #include <linux/input.h>
  33. #include <linux/kernel.h>
  34. #include <linux/delay.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/mutex.h>
  37. #include <linux/errno.h>
  38. #include <asm/irq.h>
  39. #include <asm/io.h>
  40. #include <mach/am_regs.h>
  41. #include <mach/pinmux.h>
  42. #include <linux/major.h>
  43. #include <linux/slab.h>
  44. #include <asm/uaccess.h>
  45. #include "plat/remote.h"
  46. #include "am_remote.h"
  47. #undef NEW_BOARD_LEARNING_MODE
  48. #define IR_CONTROL_HOLD_LAST_KEY (1<<6)
  49. #define IR_CONTROL_DECODER_MODE (3<<7)
  50. #define IR_CONTROL_SKIP_HEADER (1<<7)
  51. #define IR_CONTROL_RESET (1<<0)
  52. #define KEY_RELEASE_DELAY 200
  53. type_printk input_dbg;
  54. static DEFINE_MUTEX(remote_enable_mutex);
  55. static DEFINE_MUTEX(remote_file_mutex);
  56. static void remote_tasklet(unsigned long);
  57. static int remote_enable;
  58. static int NEC_REMOTE_IRQ_NO = INT_REMOTE;
  59. //static unsigned int g_remote_base;
  60. DECLARE_TASKLET_DISABLED(tasklet, remote_tasklet, 0);
  61. static struct remote *gp_remote = NULL;
  62. char *remote_log_buf;
  63. static __u16 key_map[512];
  64. static __u16 mouse_map[6]; /*Left Right Up Down + middlewheel up &down */
  65. int remote_printk(const char *fmt, ...)
  66. {
  67. va_list args;
  68. int r;
  69. if (gp_remote->debug_enable == 0) {
  70. return 0;
  71. }
  72. va_start(args, fmt);
  73. r = vprintk(fmt, args);
  74. va_end(args);
  75. return r;
  76. }
  77. static int remote_mouse_event(struct input_dev *dev, unsigned int scancode, unsigned int type)
  78. {
  79. __u16 mouse_code = REL_X;
  80. __s32 mouse_value = 0;
  81. static unsigned int repeat_count = 0;
  82. __s32 move_accelerate[] = { 0, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9 };
  83. unsigned int i;
  84. for (i = 0; i < ARRAY_SIZE(mouse_map); i++)
  85. if (mouse_map[i] == scancode) {
  86. break;
  87. }
  88. if (i >= ARRAY_SIZE(mouse_map)) {
  89. return -1;
  90. }
  91. switch (type) {
  92. case 1: //press
  93. repeat_count = 0;
  94. break;
  95. case 2: //repeat
  96. if (repeat_count >= ARRAY_SIZE(move_accelerate) - 1) {
  97. repeat_count = ARRAY_SIZE(move_accelerate) - 1;
  98. } else {
  99. repeat_count++;
  100. }
  101. }
  102. switch (i) {
  103. case 0:
  104. mouse_code = REL_X;
  105. mouse_value = -(1 + move_accelerate[repeat_count]);
  106. break;
  107. case 1:
  108. mouse_code = REL_X;
  109. mouse_value = 1 + move_accelerate[repeat_count];
  110. break;
  111. case 2:
  112. mouse_code = REL_Y;
  113. mouse_value = -(1 + move_accelerate[repeat_count]);
  114. break;
  115. case 3:
  116. mouse_code = REL_Y;
  117. mouse_value = 1 + move_accelerate[repeat_count];
  118. break;
  119. case 4: //up
  120. mouse_code = REL_WHEEL;
  121. mouse_value = 0x1;
  122. break;
  123. case 5:
  124. mouse_code = REL_WHEEL;
  125. mouse_value = 0xffffffff;
  126. break;
  127. }
  128. if (type) {
  129. input_event(dev, EV_REL, mouse_code, mouse_value);
  130. input_sync(dev);
  131. switch (mouse_code) {
  132. case REL_X:
  133. case REL_Y:
  134. input_dbg("mouse be %s moved %d.\n",
  135. mouse_code == REL_X ? "horizontal" : "vertical", mouse_value);
  136. break;
  137. case REL_WHEEL:
  138. input_dbg("mouse wheel move %s .\n", mouse_value == 0x1 ? "up" : "down");
  139. break;
  140. }
  141. }
  142. return 0;
  143. }
  144. void remote_send_key(struct input_dev *dev, unsigned int scancode, unsigned int type)
  145. {
  146. if (remote_mouse_event(dev, scancode, type)) {
  147. if (scancode > ARRAY_SIZE(key_map)) {
  148. input_dbg("scancode is 0x%04x, out of key mapping.\n", scancode);
  149. return;
  150. }
  151. if ((key_map[scancode] >= KEY_MAX)
  152. || (key_map[scancode] == KEY_RESERVED)) {
  153. input_dbg("scancode is 0x%04x, invalid key is 0x%04x.\n", scancode, key_map[scancode]);
  154. return;
  155. }
  156. input_event(dev, EV_KEY, key_map[scancode], type);
  157. input_sync(dev);
  158. switch (type) {
  159. case 0:
  160. input_dbg("release ircode = 0x%02x, scancode = 0x%04x\n", scancode, key_map[scancode]);
  161. break;
  162. case 1:
  163. input_dbg("press ircode = 0x%02x, scancode = 0x%04x\n", scancode, key_map[scancode]);
  164. break;
  165. case 2:
  166. input_dbg("repeat ircode = 0x%02x, scancode = 0x%04x\n", scancode, key_map[scancode]);
  167. break;
  168. }
  169. }
  170. }
  171. static void disable_remote_irq(void)
  172. {
  173. if ((!(gp_remote->work_mode && REMOTE_WORK_MODE_FIQ))
  174. && (!(gp_remote->work_mode && REMOTE_WORK_MODE_FIQ_RCMM))) {
  175. disable_irq(NEC_REMOTE_IRQ_NO);
  176. }
  177. }
  178. static void enable_remote_irq(void)
  179. {
  180. if ((!(gp_remote->work_mode && REMOTE_WORK_MODE_FIQ))
  181. && (!(gp_remote->work_mode && REMOTE_WORK_MODE_FIQ_RCMM))) {
  182. enable_irq(NEC_REMOTE_IRQ_NO);
  183. }
  184. }
  185. static void remote_repeat_sr(unsigned long data)
  186. {
  187. struct remote *remote_data = (struct remote *)data;
  188. u32 status;
  189. u32 timer_period;
  190. status = am_remote_read_reg(AM_IR_DEC_STATUS);
  191. switch (status & REMOTE_HW_DECODER_STATUS_MASK) {
  192. case REMOTE_HW_DECODER_STATUS_OK:
  193. remote_send_key(remote_data->input, (remote_data->cur_keycode >> 16) & 0xff, 0);
  194. break;
  195. default:
  196. am_remote_set_mask(AM_IR_DEC_REG1, 1); //reset ir deocoder
  197. am_remote_clear_mask(AM_IR_DEC_REG1, 1);
  198. if (remote_data->repeat_tick != 0) { //new key coming in.
  199. timer_period = jiffies + 10; //timer peroid waiting for a stable state.
  200. } else { //repeat key check
  201. if (remote_data->repeat_enable) {
  202. remote_send_key(remote_data->input, (remote_data->cur_keycode >> 16) & 0xff, 2);
  203. }
  204. timer_period = jiffies + msecs_to_jiffies(remote_data->repeat_peroid);
  205. }
  206. mod_timer(&remote_data->repeat_timer, timer_period);
  207. remote_data->repeat_tick = 0;
  208. break;
  209. }
  210. }
  211. static void remote_timer_sr(unsigned long data)
  212. {
  213. struct remote *remote_data = (struct remote *)data;
  214. if (remote_data->work_mode == REMOTE_WORK_MODE_FIQ_RCMM) {
  215. if (remote_data->bit_count == 32) {
  216. remote_send_key(remote_data->input, 0x100 | (remote_data->cur_keycode >> (remote_data->bit_count - 8)), 1);
  217. } else {
  218. remote_send_key(remote_data->input, remote_data->cur_keycode >> (remote_data->bit_count - 8), 0);
  219. }
  220. } else if (remote_data->work_mode == REMOTE_WORK_MODE_RC5 || remote_data->work_mode == REMOTE_WORK_MODE_RC6) {
  221. remote_send_key(remote_data->input, remote_data->cur_keycode, 0);
  222. } else {
  223. remote_send_key(remote_data->input, (remote_data->cur_keycode >> 16) & 0xff, 0);
  224. }
  225. if (!(remote_data->work_mode & REMOTE_WORK_MODE_HW)) {
  226. remote_data->step = REMOTE_STATUS_WAIT;
  227. }
  228. }
  229. static irqreturn_t remote_interrupt(int irq, void *dev_id)
  230. {
  231. /* disable keyboard interrupt and schedule for handling */
  232. // input_dbg("===trigger one remoteads interupt \r\n");
  233. tasklet_schedule(&tasklet);
  234. return IRQ_HANDLED;
  235. }
  236. static void remote_fiq_interrupt(void)
  237. {
  238. if (gp_remote->work_mode == REMOTE_WORK_MODE_RC6) {
  239. remote_rc6_reprot_key((unsigned long)gp_remote);
  240. } else if (gp_remote->work_mode == REMOTE_WORK_MODE_RC5) {
  241. remote_rc5_reprot_key((unsigned long)gp_remote);
  242. } else {
  243. remote_sw_reprot_key((unsigned long)gp_remote);
  244. }
  245. WRITE_MPEG_REG(IRQ_CLR_REG(NEC_REMOTE_IRQ_NO), 1 << IRQ_BIT(NEC_REMOTE_IRQ_NO));
  246. }
  247. static inline int remote_hw_reprot_key(struct remote *remote_data)
  248. {
  249. int key_index;
  250. unsigned int status, scan_code;
  251. static int last_scan_code, key_hold;
  252. static int last_custom_code;
  253. // 1 get scan code
  254. scan_code = am_remote_read_reg(AM_IR_DEC_FRAME);
  255. status = am_remote_read_reg(AM_IR_DEC_STATUS);
  256. key_index = 0;
  257. key_hold = -1;
  258. if (scan_code) { //key first press
  259. last_custom_code = scan_code & 0xffff;
  260. if (remote_data->custom_code != last_custom_code) {
  261. input_dbg("Wrong custom code is 0x%08x\n", scan_code);
  262. return -1;
  263. }
  264. //add for skyworth remote.
  265. if (remote_data->work_mode == REMOTE_TOSHIBA_HW) { //we start repeat timer for check repeat.
  266. if (remote_data->repeat_timer.expires > jiffies) { //release last key.
  267. remote_send_key(remote_data->input, (remote_data->cur_keycode >> 16) & 0xff, 0);
  268. }
  269. remote_send_key(remote_data->input, (scan_code >> 16) & 0xff, 1);
  270. last_scan_code = scan_code;
  271. remote_data->cur_keycode = last_scan_code;
  272. remote_data->repeat_timer.data = (unsigned long)remote_data;
  273. //here repeat delay is time interval from the first frame end to first repeat end.
  274. remote_data->repeat_tick = jiffies;
  275. mod_timer(&remote_data->repeat_timer, jiffies + msecs_to_jiffies(remote_data->repeat_delay));
  276. return 0;
  277. } else {
  278. if (remote_data->timer.expires > jiffies) {
  279. remote_send_key(remote_data->input, (remote_data->cur_keycode >> 16) & 0xff, 0);
  280. }
  281. remote_send_key(remote_data->input, (scan_code >> 16) & 0xff, 1);
  282. if (remote_data->repeat_enable) {
  283. remote_data->repeat_tick = jiffies + msecs_to_jiffies(remote_data->input->rep[REP_DELAY]);
  284. }
  285. }
  286. } else if (scan_code == 0 && status & 0x1) { //repeate key
  287. scan_code = last_scan_code;
  288. if (remote_data->custom_code != last_custom_code) {
  289. return -1;
  290. }
  291. if (remote_data->repeat_enable) {
  292. if (remote_data->repeat_tick < jiffies) {
  293. remote_send_key(remote_data->input, (scan_code >> 16) & 0xff, 2);
  294. remote_data->repeat_tick += msecs_to_jiffies(remote_data->input->rep[REP_PERIOD]);
  295. }
  296. } else {
  297. if (remote_data->timer.expires > jiffies) {
  298. mod_timer(&remote_data->timer, jiffies + msecs_to_jiffies(remote_data->release_delay));
  299. }
  300. return -1;
  301. }
  302. }
  303. last_scan_code = scan_code;
  304. remote_data->cur_keycode = last_scan_code;
  305. remote_data->timer.data = (unsigned long)remote_data;
  306. mod_timer(&remote_data->timer, jiffies + msecs_to_jiffies(remote_data->release_delay));
  307. return 0;
  308. }
  309. static void remote_tasklet(unsigned long data)
  310. {
  311. struct remote *remote_data = (struct remote *)data;
  312. if (remote_data->work_mode & REMOTE_WORK_MODE_HW) {
  313. remote_hw_reprot_key(remote_data);
  314. } else {
  315. remote_sw_reprot_key(data);
  316. }
  317. }
  318. static ssize_t remote_log_buffer_show(struct device *dev, struct device_attribute *attr, char *buf)
  319. {
  320. int ret = 0;
  321. ret = sprintf(buf, "%s\n", remote_log_buf);
  322. //printk(remote_log_buf);
  323. remote_log_buf[0] = '\0';
  324. return ret;
  325. }
  326. static ssize_t remote_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
  327. {
  328. return sprintf(buf, "%u\n", remote_enable);
  329. }
  330. //control var by sysfs .
  331. static ssize_t remote_enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  332. {
  333. int state;
  334. if (sscanf(buf, "%u", &state) != 1) {
  335. return -EINVAL;
  336. }
  337. if ((state != 1) && (state != 0)) {
  338. return -EINVAL;
  339. }
  340. mutex_lock(&remote_enable_mutex);
  341. if (state != remote_enable) {
  342. if (state) {
  343. enable_remote_irq();
  344. } else {
  345. disable_remote_irq();
  346. }
  347. remote_enable = state;
  348. }
  349. mutex_unlock(&remote_enable_mutex);
  350. return strnlen(buf, count);
  351. }
  352. static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, remote_enable_show, remote_enable_store);
  353. static DEVICE_ATTR(log_buffer, S_IRUGO | S_IWUSR, remote_log_buffer_show, NULL);
  354. /*****************************************************************
  355. **
  356. ** func : hardware init
  357. ** in this function will do pin configuration and and initialize for hardware
  358. ** decoder mode .
  359. **
  360. ********************************************************************/
  361. static int hardware_init(struct platform_device *pdev)
  362. {
  363. struct aml_remote_platdata *remote_pdata;
  364. unsigned int control_value, status, data_value;
  365. //step 0: set pinmux to remote
  366. remote_pdata = (struct aml_remote_platdata *)pdev->dev.platform_data;
  367. if (!remote_pdata) {
  368. printk("Remote platform_data not found.\n");
  369. return -1;
  370. }
  371. if (remote_pdata->pinmux_setup)
  372. remote_pdata->pinmux_setup();
  373. g_remote_base = remote_pdata->ao_baseaddr;
  374. printk("Remote platform_data g_remote_base=%x\n",g_remote_base);
  375. //step 1 :set reg IR_DEC_CONTROL
  376. control_value = 3 << 28 | (0xFA0 << 12) | 0x13;
  377. am_remote_write_reg(AM_IR_DEC_REG0, control_value);
  378. control_value = am_remote_read_reg(AM_IR_DEC_REG1);
  379. am_remote_write_reg(AM_IR_DEC_REG1, control_value | IR_CONTROL_HOLD_LAST_KEY);
  380. status = am_remote_read_reg(AM_IR_DEC_STATUS);
  381. data_value = am_remote_read_reg(AM_IR_DEC_FRAME);
  382. printk("Remote date_valye======%x,status == %x\n",data_value,status);
  383. //step 2 : request nec_remote irq & enable it
  384. return request_irq(NEC_REMOTE_IRQ_NO, remote_interrupt, IRQF_SHARED, "keypad", (void *)remote_interrupt);
  385. }
  386. static int work_mode_config(unsigned int cur_mode)
  387. {
  388. unsigned int control_value;
  389. static unsigned int last_mode = REMOTE_WORK_MODE_INV;
  390. struct irq_desc *desc = irq_to_desc(NEC_REMOTE_IRQ_NO);
  391. int ret;
  392. if (last_mode == cur_mode) {
  393. return -1;
  394. }
  395. if (cur_mode & REMOTE_WORK_MODE_HW) {
  396. control_value = 0xbe40; //ignore custom code .
  397. am_remote_write_reg(AM_IR_DEC_REG1, control_value | IR_CONTROL_HOLD_LAST_KEY);
  398. } else {
  399. control_value = 0x8578;
  400. am_remote_write_reg(AM_IR_DEC_REG1, control_value);
  401. }
  402. switch (cur_mode & REMOTE_WORK_MODE_MASK) {
  403. case REMOTE_WORK_MODE_HW:
  404. case REMOTE_WORK_MODE_SW:
  405. if ((last_mode == REMOTE_WORK_MODE_FIQ)
  406. || (last_mode == REMOTE_WORK_MODE_FIQ_RCMM)) {
  407. //disable fiq and enable common irq
  408. unregister_fiq_bridge_handle(&gp_remote->fiq_handle_item);
  409. free_fiq(NEC_REMOTE_IRQ_NO, &remote_fiq_interrupt);
  410. ret = request_irq(NEC_REMOTE_IRQ_NO, remote_interrupt, IRQF_SHARED, "keypad", (void *)remote_interrupt);
  411. if (ret < 0) {
  412. printk(KERN_ERR "Remote: request_irq failed, ret=%d.\n", ret);
  413. return ret;
  414. }
  415. }
  416. break;
  417. case REMOTE_WORK_MODE_FIQ:
  418. case REMOTE_WORK_MODE_FIQ_RCMM:
  419. if ((last_mode == REMOTE_WORK_MODE_FIQ)
  420. || (last_mode == REMOTE_WORK_MODE_FIQ_RCMM)) {
  421. break;
  422. }
  423. //disable common irq and enable fiq.
  424. free_irq(NEC_REMOTE_IRQ_NO, remote_interrupt);
  425. if (cur_mode == REMOTE_WORK_MODE_RC6) {
  426. gp_remote->fiq_handle_item.handle = remote_rc6_bridge_isr;
  427. } else if (cur_mode == REMOTE_WORK_MODE_RC5) {
  428. gp_remote->fiq_handle_item.handle = remote_rc5_bridge_isr;
  429. } else {
  430. gp_remote->fiq_handle_item.handle = remote_bridge_isr;
  431. }
  432. gp_remote->fiq_handle_item.key = (u32) gp_remote;
  433. gp_remote->fiq_handle_item.name = "remote_bridge";
  434. register_fiq_bridge_handle(&gp_remote->fiq_handle_item);
  435. //workaround to fix fiq mechanism bug.
  436. desc->depth++;
  437. request_fiq(NEC_REMOTE_IRQ_NO, &remote_fiq_interrupt);
  438. break;
  439. }
  440. last_mode = cur_mode;
  441. //add for skyworth remote
  442. if (cur_mode == REMOTE_TOSHIBA_HW) {
  443. setup_timer(&gp_remote->repeat_timer, remote_repeat_sr, 0);
  444. } else {
  445. del_timer(&gp_remote->repeat_timer);
  446. }
  447. return 0;
  448. }
  449. static int remote_config_open(struct inode *inode, struct file *file)
  450. {
  451. file->private_data = gp_remote;
  452. return 0;
  453. }
  454. static long remote_config_ioctl(struct file *filp, unsigned int cmd, unsigned long args)
  455. {
  456. struct remote *remote = (struct remote *)filp->private_data;
  457. void __user *argp = (void __user *)args;
  458. unsigned int val, i;
  459. unsigned int ret;
  460. if (args) {
  461. ret = copy_from_user(&val, argp, sizeof(unsigned long));
  462. }
  463. mutex_lock(&remote_file_mutex);
  464. //cmd input
  465. switch (cmd) {
  466. // 1 set part
  467. case REMOTE_IOC_RESET_KEY_MAPPING:
  468. for (i = 0; i < ARRAY_SIZE(key_map); i++) {
  469. key_map[i] = KEY_RESERVED;
  470. }
  471. for (i = 0; i < ARRAY_SIZE(mouse_map); i++) {
  472. mouse_map[i] = 0xffff;
  473. }
  474. break;
  475. case REMOTE_IOC_SET_KEY_MAPPING:
  476. if ((val >> 16) >= ARRAY_SIZE(key_map)) {
  477. mutex_unlock(&remote_file_mutex);
  478. return -1;
  479. }
  480. key_map[val >> 16] = val & 0xffff;
  481. break;
  482. case REMOTE_IOC_SET_MOUSE_MAPPING:
  483. if ((val >> 16) >= ARRAY_SIZE(mouse_map)) {
  484. mutex_unlock(&remote_file_mutex);
  485. return -1;
  486. }
  487. mouse_map[val >> 16] = val & 0xff;
  488. break;
  489. case REMOTE_IOC_SET_REPEAT_DELAY:
  490. ret = copy_from_user(&remote->repeat_delay, argp, sizeof(long));
  491. break;
  492. case REMOTE_IOC_SET_REPEAT_PERIOD:
  493. ret = copy_from_user(&remote->repeat_peroid, argp, sizeof(long));
  494. break;
  495. case REMOTE_IOC_SET_REPEAT_ENABLE:
  496. ret = copy_from_user(&remote->repeat_enable, argp, sizeof(long));
  497. break;
  498. case REMOTE_IOC_SET_DEBUG_ENABLE:
  499. ret = copy_from_user(&remote->debug_enable, argp, sizeof(long));
  500. break;
  501. case REMOTE_IOC_SET_MODE:
  502. ret = copy_from_user(&remote->work_mode, argp, sizeof(long));
  503. break;
  504. case REMOTE_IOC_SET_BIT_COUNT:
  505. ret = copy_from_user(&remote->bit_count, argp, sizeof(long));
  506. if (remote->bit_count > 32) {
  507. remote->bit_count = 32;
  508. }
  509. break;
  510. case REMOTE_IOC_SET_CUSTOMCODE:
  511. ret = copy_from_user(&remote->custom_code, argp, sizeof(long));
  512. break;
  513. case REMOTE_IOC_SET_REG_BASE_GEN:
  514. am_remote_write_reg(AM_IR_DEC_REG0, val);
  515. break;
  516. case REMOTE_IOC_SET_REG_CONTROL:
  517. am_remote_write_reg(AM_IR_DEC_REG1, val);
  518. break;
  519. case REMOTE_IOC_SET_REG_LEADER_ACT:
  520. am_remote_write_reg(AM_IR_DEC_LDR_ACTIVE, val);
  521. break;
  522. case REMOTE_IOC_SET_REG_LEADER_IDLE:
  523. am_remote_write_reg(AM_IR_DEC_LDR_IDLE, val);
  524. break;
  525. case REMOTE_IOC_SET_REG_REPEAT_LEADER:
  526. am_remote_write_reg(AM_IR_DEC_LDR_REPEAT, val);
  527. break;
  528. case REMOTE_IOC_SET_REG_BIT0_TIME:
  529. am_remote_write_reg(AM_IR_DEC_BIT_0, val);
  530. break;
  531. case REMOTE_IOC_SET_RELEASE_DELAY:
  532. ret = copy_from_user(&remote->release_delay, argp, sizeof(long));
  533. break;
  534. //SW
  535. case REMOTE_IOC_SET_TW_LEADER_ACT:
  536. remote->time_window[0] = val & 0xffff;
  537. remote->time_window[1] = (val >> 16) & 0xffff;
  538. break;
  539. case REMOTE_IOC_SET_TW_BIT0_TIME:
  540. remote->time_window[2] = val & 0xffff;
  541. remote->time_window[3] = (val >> 16) & 0xffff;
  542. break;
  543. case REMOTE_IOC_SET_TW_BIT1_TIME:
  544. remote->time_window[4] = val & 0xffff;
  545. remote->time_window[5] = (val >> 16) & 0xffff;
  546. break;
  547. case REMOTE_IOC_SET_TW_REPEATE_LEADER:
  548. remote->time_window[6] = val & 0xffff;
  549. remote->time_window[7] = (val >> 16) & 0xffff;
  550. break;
  551. case REMOTE_IOC_SET_TW_BIT2_TIME:
  552. remote->time_window[8] = val & 0xffff;
  553. remote->time_window[9] = (val >> 16) & 0xffff;
  554. break;
  555. case REMOTE_IOC_SET_TW_BIT3_TIME:
  556. remote->time_window[10] = val & 0xffff;
  557. remote->time_window[11] = (val >> 16) & 0xffff;
  558. break;
  559. // 2 get part
  560. case REMOTE_IOC_GET_REG_BASE_GEN:
  561. val = am_remote_read_reg(AM_IR_DEC_REG0);
  562. break;
  563. case REMOTE_IOC_GET_REG_CONTROL:
  564. val = am_remote_read_reg(AM_IR_DEC_REG1);
  565. break;
  566. case REMOTE_IOC_GET_REG_LEADER_ACT:
  567. val = am_remote_read_reg(AM_IR_DEC_LDR_ACTIVE);
  568. break;
  569. case REMOTE_IOC_GET_REG_LEADER_IDLE:
  570. val = am_remote_read_reg(AM_IR_DEC_LDR_IDLE);
  571. break;
  572. case REMOTE_IOC_GET_REG_REPEAT_LEADER:
  573. val = am_remote_read_reg(AM_IR_DEC_LDR_REPEAT);
  574. break;
  575. case REMOTE_IOC_GET_REG_BIT0_TIME:
  576. val = am_remote_read_reg(AM_IR_DEC_BIT_0);
  577. break;
  578. case REMOTE_IOC_GET_REG_FRAME_DATA:
  579. val = am_remote_read_reg(AM_IR_DEC_FRAME);
  580. break;
  581. case REMOTE_IOC_GET_REG_FRAME_STATUS:
  582. val = am_remote_read_reg(AM_IR_DEC_STATUS);
  583. break;
  584. //sw
  585. case REMOTE_IOC_GET_TW_LEADER_ACT:
  586. val = remote->time_window[0] | (remote->time_window[1] << 16);
  587. break;
  588. case REMOTE_IOC_GET_TW_BIT0_TIME:
  589. val = remote->time_window[2] | (remote->time_window[3] << 16);
  590. break;
  591. case REMOTE_IOC_GET_TW_BIT1_TIME:
  592. val = remote->time_window[4] | (remote->time_window[5] << 16);
  593. break;
  594. case REMOTE_IOC_GET_TW_REPEATE_LEADER:
  595. val = remote->time_window[6] | (remote->time_window[7] << 16);
  596. break;
  597. }
  598. //output result
  599. switch (cmd) {
  600. case REMOTE_IOC_SET_REPEAT_ENABLE:
  601. if (remote->repeat_enable) {
  602. remote->input->rep[REP_DELAY] = remote->repeat_delay;
  603. remote->input->rep[REP_PERIOD] = remote->repeat_peroid;
  604. } else {
  605. remote->input->rep[REP_DELAY] = 0xffffffff;
  606. remote->input->rep[REP_PERIOD] = 0xffffffff;
  607. }
  608. break;
  609. case REMOTE_IOC_SET_MODE:
  610. work_mode_config(remote->work_mode);
  611. break;
  612. case REMOTE_IOC_GET_REG_BASE_GEN:
  613. case REMOTE_IOC_GET_REG_CONTROL:
  614. case REMOTE_IOC_GET_REG_LEADER_ACT:
  615. case REMOTE_IOC_GET_REG_LEADER_IDLE:
  616. case REMOTE_IOC_GET_REG_REPEAT_LEADER:
  617. case REMOTE_IOC_GET_REG_BIT0_TIME:
  618. case REMOTE_IOC_GET_REG_FRAME_DATA:
  619. case REMOTE_IOC_GET_REG_FRAME_STATUS:
  620. case REMOTE_IOC_GET_TW_LEADER_ACT:
  621. case REMOTE_IOC_GET_TW_BIT0_TIME:
  622. case REMOTE_IOC_GET_TW_BIT1_TIME:
  623. case REMOTE_IOC_GET_TW_REPEATE_LEADER:
  624. ret = copy_to_user(argp, &val, sizeof(long));
  625. break;
  626. }
  627. mutex_unlock(&remote_file_mutex);
  628. return 0;
  629. }
  630. static int remote_config_release(struct inode *inode, struct file *file)
  631. {
  632. file->private_data = NULL;
  633. return 0;
  634. }
  635. static const struct file_operations remote_fops = {
  636. .owner = THIS_MODULE,
  637. .open = remote_config_open,
  638. .unlocked_ioctl = remote_config_ioctl,
  639. .release = remote_config_release,
  640. };
  641. static int register_remote_dev(struct remote *remote)
  642. {
  643. int ret = 0;
  644. strcpy(remote->config_name, "amremote");
  645. ret = register_chrdev(0, remote->config_name, &remote_fops);
  646. if (ret <= 0) {
  647. printk("register char dev tv error\r\n");
  648. return ret;
  649. }
  650. remote->config_major = ret;
  651. printk("remote config major:%d\r\n", ret);
  652. remote->config_class = class_create(THIS_MODULE, remote->config_name);
  653. remote->config_dev = device_create(remote->config_class, NULL, MKDEV(remote->config_major, 0), NULL, remote->config_name);
  654. return ret;
  655. }
  656. static int __init remote_probe(struct platform_device *pdev)
  657. {
  658. struct remote *remote;
  659. struct input_dev *input_dev;
  660. int i, ret;
  661. remote_enable = 1;
  662. remote = kzalloc(sizeof(struct remote), GFP_KERNEL);
  663. input_dev = input_allocate_device();
  664. if (!remote || !input_dev) {
  665. kfree(remote);
  666. input_free_device(input_dev);
  667. return -ENOMEM;
  668. }
  669. gp_remote = remote;
  670. remote->debug_enable = 0;
  671. input_dbg = remote_printk;
  672. platform_set_drvdata(pdev, remote);
  673. remote->work_mode = REMOTE_NEC_HW;
  674. remote->input = input_dev;
  675. remote->release_delay = KEY_RELEASE_DELAY;
  676. remote->custom_code = 0xff00;
  677. remote->bit_count = 32; //default 32bit for sw mode.
  678. remote->last_jiffies = 0xffffffff;
  679. remote->last_pulse_width = 0;
  680. remote->step = REMOTE_STATUS_WAIT;
  681. remote->time_window[0] = 0x1;
  682. remote->time_window[1] = 0x1;
  683. remote->time_window[2] = 0x1;
  684. remote->time_window[3] = 0x1;
  685. remote->time_window[4] = 0x1;
  686. remote->time_window[5] = 0x1;
  687. remote->time_window[6] = 0x1;
  688. remote->time_window[7] = 0x1;
  689. /* Disable the interrupt for the MPUIO keyboard */
  690. for (i = 0; i < ARRAY_SIZE(key_map); i++) {
  691. key_map[i] = KEY_RESERVED;
  692. }
  693. for (i = 0; i < ARRAY_SIZE(mouse_map); i++) {
  694. mouse_map[i] = 0xffff;
  695. }
  696. remote->repeat_delay = 250;
  697. remote->repeat_peroid = 33;
  698. /* get the irq and init timer */
  699. input_dbg("set drvdata completed\r\n");
  700. tasklet_enable(&tasklet);
  701. tasklet.data = (unsigned long)remote;
  702. setup_timer(&remote->timer, remote_timer_sr, 0);
  703. ret = device_create_file(&pdev->dev, &dev_attr_enable);
  704. if (ret < 0) {
  705. goto err1;
  706. }
  707. ret = device_create_file(&pdev->dev, &dev_attr_log_buffer);
  708. if (ret < 0) {
  709. device_remove_file(&pdev->dev, &dev_attr_enable);
  710. goto err1;
  711. }
  712. input_dbg("device_create_file completed \r\n");
  713. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  714. input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
  715. input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y) | BIT_MASK(REL_WHEEL);
  716. input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_SIDE) | BIT_MASK(BTN_EXTRA);
  717. for (i = 0; i < KEY_MAX; i++) {
  718. set_bit(i, input_dev->keybit);
  719. }
  720. //clear_bit(0,input_dev->keybit);
  721. input_dev->name = "aml_keypad";
  722. input_dev->phys = "keypad/input0";
  723. //input_dev->cdev.dev = &pdev->dev;
  724. //input_dev->private = remote;
  725. input_dev->dev.parent = &pdev->dev;
  726. input_dev->id.bustype = BUS_ISA;
  727. input_dev->id.vendor = 0x0001;
  728. input_dev->id.product = 0x0001;
  729. input_dev->id.version = 0x0100;
  730. remote->repeat_enable = 0;
  731. input_dev->rep[REP_DELAY] = 0xffffffff;
  732. input_dev->rep[REP_PERIOD] = 0xffffffff;
  733. input_dev->keycodesize = sizeof(unsigned short);
  734. input_dev->keycodemax = 0x1ff;
  735. ret = input_register_device(remote->input);
  736. if (ret < 0) {
  737. printk(KERN_ERR "Unable to register keypad input device\n");
  738. goto err2;
  739. }
  740. input_dbg("input_register_device completed \r\n");
  741. if (hardware_init(pdev)) {
  742. goto err3;
  743. }
  744. register_remote_dev(gp_remote);
  745. remote_log_buf = (char *)__get_free_pages(GFP_KERNEL, REMOTE_LOG_BUF_ORDER);
  746. remote_log_buf[0] = '\0';
  747. printk("physical address:0x%x\n", (unsigned int)virt_to_phys(remote_log_buf));
  748. return 0;
  749. err3:
  750. // free_irq(NEC_REMOTE_IRQ_NO,remote_interrupt);
  751. input_unregister_device(remote->input);
  752. input_dev = NULL;
  753. err2:
  754. device_remove_file(&pdev->dev, &dev_attr_enable);
  755. device_remove_file(&pdev->dev, &dev_attr_log_buffer);
  756. err1:
  757. kfree(remote);
  758. input_free_device(input_dev);
  759. return -EINVAL;
  760. }
  761. static int remote_remove(struct platform_device *pdev)
  762. {
  763. struct remote *remote = platform_get_drvdata(pdev);
  764. /* disable keypad interrupt handling */
  765. input_dbg("remove remoteads \r\n");
  766. tasklet_disable(&tasklet);
  767. tasklet_kill(&tasklet);
  768. /* unregister everything */
  769. input_unregister_device(remote->input);
  770. free_pages((unsigned long)remote_log_buf, REMOTE_LOG_BUF_ORDER);
  771. device_remove_file(&pdev->dev, &dev_attr_enable);
  772. device_remove_file(&pdev->dev, &dev_attr_log_buffer);
  773. if ((remote->work_mode & REMOTE_WORK_MODE_FIQ)
  774. || (remote->work_mode & REMOTE_WORK_MODE_FIQ_RCMM)) {
  775. free_fiq(NEC_REMOTE_IRQ_NO, &remote_fiq_interrupt);
  776. free_irq(BRIDGE_IRQ, gp_remote);
  777. } else {
  778. free_irq(NEC_REMOTE_IRQ_NO, remote_interrupt);
  779. }
  780. input_free_device(remote->input);
  781. unregister_chrdev(remote->config_major, remote->config_name);
  782. if (remote->config_class) {
  783. if (remote->config_dev) {
  784. device_destroy(remote->config_class, MKDEV(remote->config_major, 0));
  785. }
  786. class_destroy(remote->config_class);
  787. }
  788. kfree(remote);
  789. gp_remote = NULL;
  790. return 0;
  791. }
  792. static struct platform_driver remote_driver = {
  793. .probe = remote_probe,
  794. .remove = remote_remove,
  795. .suspend = NULL,
  796. .resume = NULL,
  797. .driver = {
  798. .name = "meson-remote",
  799. },
  800. };
  801. static int __devinit remote_init(void)
  802. {
  803. printk(KERN_INFO "Remote Driver\n");
  804. return platform_driver_register(&remote_driver);
  805. }
  806. static void __exit remote_exit(void)
  807. {
  808. printk(KERN_INFO "Remote exit \n");
  809. platform_driver_unregister(&remote_driver);
  810. }
  811. module_init(remote_init);
  812. module_exit(remote_exit);
  813. MODULE_AUTHOR("jianfeng_wang");
  814. MODULE_DESCRIPTION("Remote Driver");
  815. MODULE_LICENSE("GPL");