sw_remote_rc6.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * linux/drivers/input/irremote/sw_remote_rc6.c
  3. *
  4. * Keypad 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 : jinlin xia
  23. */
  24. /*
  25. * !!caution: if you use remote ,you should disable card1 used for ata_enable pin.
  26. *
  27. */
  28. /********remote.conf**************
  29. factory_code =
  30. work_mode =0x22
  31. reg_control =0x8574
  32. tw_bit0 =
  33. bit_count =8
  34. repeat_enable =0
  35. release_delay =120
  36. *********************************/
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/types.h>
  41. #include <linux/input.h>
  42. #include <linux/kernel.h>
  43. #include <linux/delay.h>
  44. #include <linux/platform_device.h>
  45. #include <linux/mutex.h>
  46. #include <linux/errno.h>
  47. #include <asm/irq.h>
  48. #include <asm/io.h>
  49. #include <mach/am_regs.h>
  50. #include "am_remote.h"
  51. //unit T=444.44us, base rate generator is 20us, so 1T=444.44/20 us
  52. #define RC6_UNIT_T 22
  53. #define RC6_STATUS_WAIT 100
  54. #define RC6_STATUS_LEADER 101
  55. #define RC6_STATUS_START 102
  56. #define RC6_STATUS_MODE 103
  57. #define RC6_STATUS_TRAILER 104
  58. #define RC6_STATUS_CONTROL 105
  59. #define RC6_STATUS_INFO 106
  60. #define RC6_STATUS_FREE 107
  61. #define RC6_STATUS_SYNC 108
  62. extern char *remote_log_buf;
  63. typedef struct {
  64. unsigned short time;
  65. unsigned char level; //0-low level 1-high level
  66. } win_time_t;
  67. static win_time_t left_window = {.time = 0, .level = 1 };
  68. static int sub_status;
  69. static int trailer_level;
  70. static int bit_0_1;
  71. static int control_field;
  72. static int dbg_printk(const char *fmt, ...)
  73. {
  74. char buf[100];
  75. va_list args;
  76. va_start(args, fmt);
  77. vscnprintf(buf, 100, fmt, args);
  78. if (strlen(remote_log_buf) + (strlen(buf) + 64) > REMOTE_LOG_BUF_LEN) {
  79. remote_log_buf[0] = '\0';
  80. }
  81. strcat(remote_log_buf, buf);
  82. va_end(args);
  83. return 0;
  84. }
  85. //clear sub status and set new status
  86. static inline void goto_status(unsigned long data, int status)
  87. {
  88. struct remote *remote_data = (struct remote *)data;
  89. sub_status = 0;
  90. remote_data->step = status;
  91. }
  92. static inline int rc6_bit0_high(win_time_t * win)
  93. {
  94. if (win->level == 1 && win->time == 1) {
  95. left_window.time = 0;
  96. return 0;
  97. } else {
  98. return -1;
  99. }
  100. }
  101. static inline int rc6_bit0_low(win_time_t * win)
  102. {
  103. if (win->level == 0 && win->time >= 1) {
  104. left_window.time = win->time - 1;
  105. return 0;
  106. } else {
  107. return -1;
  108. }
  109. }
  110. static inline int rc6_bit1_low(win_time_t * win)
  111. {
  112. if (win->level == 0 && win->time == 1) {
  113. left_window.time = 0;
  114. return 0;
  115. } else {
  116. return -1;
  117. }
  118. }
  119. static inline int rc6_bit1_high(win_time_t * win)
  120. {
  121. if (win->level == 1 && win->time >= 1) {
  122. left_window.time = win->time - 1;
  123. return 0;
  124. } else {
  125. return -1;
  126. }
  127. }
  128. /*
  129. check the pulse is the first half of 0 bit or 1 bit
  130. return: 0-----0 bit
  131. 1-----1 bit
  132. -1----invalid pulse
  133. */
  134. static inline int check_start_level(win_time_t * win)
  135. {
  136. if (rc6_bit0_high(win) == 0) {
  137. return 0;
  138. }
  139. if (rc6_bit1_low(win) == 0) {
  140. return 1;
  141. }
  142. return -1;
  143. }
  144. //return width which base rate is RC6_UNIT_T
  145. static inline int get_pulse_width(unsigned long data)
  146. {
  147. struct remote *remote_data = (struct remote *)data;
  148. int pulse_width;
  149. const char *state;
  150. int num;
  151. int ret;
  152. unsigned int range[2] =
  153. { remote_data->time_window[2] >> 1, remote_data->time_window[3] >> 1 };
  154. pulse_width = (am_remote_read_reg(AM_IR_DEC_REG1) & 0x1FFF0000) >> 16;
  155. num = pulse_width / RC6_UNIT_T;
  156. if (pulse_width > num * range[0] && pulse_width < num * range[1]) {
  157. ret = num;
  158. } else if (pulse_width > (num + 1) * range[0]
  159. && pulse_width < (num + 1) * range[1]) {
  160. ret = num + 1;
  161. } else {
  162. ret = 0;
  163. }
  164. state = remote_data->step == RC6_STATUS_WAIT ? "wait" :
  165. remote_data->step == RC6_STATUS_LEADER ? "leader" :
  166. remote_data->step == RC6_STATUS_START ? "start" :
  167. remote_data->step == RC6_STATUS_MODE ? "mode" :
  168. remote_data->step == RC6_STATUS_TRAILER ? "trailer" :
  169. remote_data->step == RC6_STATUS_CONTROL ? "control" :
  170. remote_data->step == RC6_STATUS_INFO ? "information" :
  171. remote_data->step == RC6_STATUS_FREE ? "free" : NULL;
  172. dbg_printk("%02d:pulse_wdith:%d==>%s\r\n",
  173. remote_data->bit_count - remote_data->bit_num, ret, state);
  174. return ret;
  175. }
  176. static inline void kbd_rc6_wait(unsigned long data, win_time_t * window)
  177. {
  178. //unsigned short pulse_width;
  179. struct remote *remote_data = (struct remote *)data;
  180. //pulse_width = get_pulse_width(data);//ignore first window
  181. remote_data->step = RC6_STATUS_LEADER;
  182. remote_data->cur_keycode = 0;
  183. remote_data->bit_num = remote_data->bit_count;
  184. left_window.level = 1;
  185. left_window.time = 0;
  186. sub_status = 0;
  187. control_field = 0;
  188. }
  189. /*
  190. sub_status: 0----->low 6T level, 1---->high 2T level
  191. */
  192. static inline void kbd_rc6_leader(unsigned long data, win_time_t * window)
  193. {
  194. if (window->time == 6 && window->level == 0 && sub_status == 0) {
  195. sub_status = 1;
  196. left_window.time = 0;
  197. } else if (window->time == 2 && window->level == 1 && sub_status == 1) {
  198. goto_status(data, RC6_STATUS_START);
  199. left_window.time = 0;
  200. } else {
  201. goto_status(data, RC6_STATUS_WAIT);
  202. }
  203. }
  204. /*
  205. start bit 1
  206. sub_status: 0----->low 1T level, 1----->high 1T level
  207. */
  208. static inline void kbd_rc6_start(unsigned long data, win_time_t * window)
  209. {
  210. if (rc6_bit1_low(window) == 0 && sub_status == 0) {
  211. sub_status = 1;
  212. } else if (rc6_bit1_high(window) == 1 && sub_status == 1) {
  213. goto_status(data, RC6_STATUS_MODE);
  214. } else {
  215. goto_status(data, RC6_STATUS_WAIT);
  216. }
  217. }
  218. /*
  219. mode bits: 3bit, we only handle 000 currently
  220. sub_status: 0----->high 1T level, 1----->low 1T level
  221. 2----->high 1T level, 3----->low 1T level
  222. 4----->high 1T level, 5----->low 1T level
  223. */
  224. static inline void kbd_rc6_mode(unsigned long data, win_time_t * window)
  225. {
  226. switch (sub_status) {
  227. case 0:
  228. if (rc6_bit0_high(window) == 0) {
  229. sub_status = 1;
  230. return;
  231. }
  232. break;
  233. case 1:
  234. if (rc6_bit0_low(window) == 0) {
  235. sub_status = 2;
  236. return;
  237. }
  238. break;
  239. case 2:
  240. if (rc6_bit0_high(window) == 0) {
  241. sub_status = 3;
  242. return;
  243. }
  244. break;
  245. case 3:
  246. if (rc6_bit0_low(window) == 0) {
  247. sub_status = 4;
  248. return;
  249. }
  250. break;
  251. case 4:
  252. if (rc6_bit0_high(window) == 0) {
  253. sub_status = 5;
  254. return;
  255. }
  256. break;
  257. case 5:
  258. if (rc6_bit0_low(window) == 0) {
  259. goto_status(data, RC6_STATUS_TRAILER);
  260. return;
  261. }
  262. break;
  263. }
  264. goto_status(data, RC6_STATUS_WAIT);
  265. }
  266. /*
  267. trailer mode
  268. sub_status: 0---->low or high 2T level, 1------>high or low 2T level
  269. */
  270. static inline void kbd_rc6_trailer(unsigned long data, win_time_t * window)
  271. {
  272. if (sub_status == 0 && window->time == 2) {
  273. sub_status = 1;
  274. trailer_level = window->level;
  275. left_window.time = 0;
  276. } else if (sub_status == 1 && window->time >= 2
  277. && trailer_level != window->level) {
  278. left_window.time = window->time - 2;
  279. goto_status(data, RC6_STATUS_CONTROL);
  280. } else {
  281. goto_status(data, RC6_STATUS_WAIT);
  282. }
  283. }
  284. /*
  285. control field: 8 bit
  286. sub_status: 0,1----->1 bit, 2,3---->2 bit
  287. 4,5----->3 bit, 6,7---->4 bit
  288. 8,9----->5 bit, 10,11---->6 bit
  289. 12,13----->7 bit, 14,15---->8 bit
  290. */
  291. static inline void kbd_rc6_control(unsigned long data, win_time_t * window)
  292. {
  293. if (sub_status % 2 == 0) {
  294. bit_0_1 = check_start_level(window);
  295. if (bit_0_1 == -1) {
  296. goto_status(data, RC6_STATUS_WAIT);
  297. return;
  298. }
  299. } else {
  300. if (bit_0_1 == 0 && rc6_bit0_low(window) == 0) {
  301. //do nothing
  302. } else if (bit_0_1 == 1 && rc6_bit1_high(window) == 0) {
  303. control_field |= (1 << (sub_status >> 1));
  304. } else {
  305. goto_status(data, RC6_STATUS_WAIT);
  306. return;
  307. }
  308. }
  309. sub_status++;
  310. if (sub_status == 16) {
  311. goto_status(data, RC6_STATUS_INFO);
  312. }
  313. }
  314. /*
  315. information bits: 8 bit
  316. sub_status: 0,1----->1 bit, 2,3---->2 bit
  317. 4,5----->3 bit, 6,7---->4 bit
  318. 8,9----->5 bit, 10,11---->6 bit
  319. 12,13----->7 bit, 14,15---->8 bit
  320. */
  321. static inline void kbd_rc6_info(unsigned long data, win_time_t * window)
  322. {
  323. struct remote *remote_data = (struct remote *)data;
  324. if (sub_status % 2 == 0) {
  325. bit_0_1 = check_start_level(window);
  326. if (bit_0_1 == -1) {
  327. goto_status(data, RC6_STATUS_WAIT);
  328. return;
  329. }
  330. } else {
  331. if (bit_0_1 == 1 && rc6_bit0_low(window) == 0) {
  332. remote_data->bit_num--;
  333. } else if (bit_0_1 == 1 && rc6_bit1_high(window) == 0) {
  334. remote_data->cur_keycode |=
  335. 1 << (remote_data->bit_count - remote_data->bit_num);
  336. remote_data->bit_num--;
  337. } else {
  338. goto_status(data, RC6_STATUS_WAIT);
  339. return;
  340. }
  341. }
  342. if (remote_data->bit_num == 0) { //send the key
  343. printk(KERN_INFO "send key cur_keycode[0x%x]\n",
  344. remote_data->cur_keycode);
  345. remote_data->send_data = 1;
  346. fiq_bridge_pulse_trigger(&remote_data->fiq_handle_item);
  347. }
  348. sub_status++;
  349. if (sub_status == 16) {
  350. goto_status(data, RC6_STATUS_FREE);
  351. }
  352. }
  353. /*
  354. free bits: 6T high level pulse
  355. */
  356. static inline void kbd_rc6_free(unsigned long data, win_time_t * window)
  357. {
  358. }
  359. static void kbd_handle_sm(unsigned long data, win_time_t * window)
  360. {
  361. struct remote *remote_data = (struct remote *)data;
  362. switch (remote_data->step) {
  363. case RC6_STATUS_WAIT:
  364. kbd_rc6_wait(data, window);
  365. break;
  366. case RC6_STATUS_LEADER:
  367. kbd_rc6_leader(data, window);
  368. break;
  369. case RC6_STATUS_START:
  370. kbd_rc6_start(data, window);
  371. break;
  372. case RC6_STATUS_MODE:
  373. kbd_rc6_mode(data, window);
  374. break;
  375. case RC6_STATUS_TRAILER:
  376. kbd_rc6_trailer(data, window);
  377. break;
  378. case RC6_STATUS_CONTROL:
  379. kbd_rc6_control(data, window);
  380. break;
  381. case RC6_STATUS_INFO:
  382. kbd_rc6_info(data, window);
  383. break;
  384. case RC6_STATUS_FREE:
  385. kbd_rc6_free(data, window);
  386. break;
  387. default:
  388. break;
  389. }
  390. if (left_window.time > 0) {
  391. kbd_handle_sm(data, &left_window);
  392. }
  393. }
  394. void remote_rc6_reprot_key(unsigned long data)
  395. {
  396. struct remote *remote_data = (struct remote *)data;
  397. int current_jiffies = jiffies;
  398. if (((current_jiffies - remote_data->last_jiffies) > 20)
  399. && (remote_data->step <= RC6_STATUS_FREE)) {
  400. remote_data->step = RC6_STATUS_WAIT;
  401. }
  402. remote_data->last_jiffies = current_jiffies; //ignore a little msecs
  403. left_window.time = get_pulse_width(data);
  404. left_window.level = (left_window.level == 0) ? 1 : 0;
  405. kbd_handle_sm(data, &left_window);
  406. }
  407. irqreturn_t remote_rc6_bridge_isr(int irq, void *dev_id)
  408. {
  409. struct remote *remote_data = (struct remote *)dev_id;
  410. printk(KERN_INFO "===remote_rc6_bridge_isr\n");
  411. if (remote_data->send_data) { //report key
  412. remote_data->step = RC6_STATUS_SYNC;
  413. remote_send_key(remote_data->input, remote_data->cur_keycode, 1);
  414. remote_data->send_data = 0;
  415. }
  416. remote_data->timer.data = (unsigned long)remote_data;
  417. mod_timer(&remote_data->timer,
  418. jiffies + msecs_to_jiffies(remote_data->release_delay));
  419. return IRQ_HANDLED;
  420. }