sw_remote_rc5.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * linux/drivers/input/irremote/sw_remote_rc5.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. work_mode =0x32
  30. reg_control =0x8574
  31. reg_base_gen =0
  32. tw_bit0 =0x00398x035a
  33. bit_count =6
  34. repeat_enable =0
  35. release_delay =180
  36. debug_enable =0
  37. *********************************/
  38. #include <linux/module.h>
  39. #include <linux/init.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/types.h>
  42. #include <linux/input.h>
  43. #include <linux/kernel.h>
  44. #include <linux/delay.h>
  45. #include <linux/platform_device.h>
  46. #include <linux/mutex.h>
  47. #include <linux/errno.h>
  48. #include <asm/irq.h>
  49. #include <asm/io.h>
  50. #include <mach/am_regs.h>
  51. #include "am_remote.h"
  52. //unit T=888.889us
  53. #define RC5_UNIT_T 889
  54. #define RC5_STATUS_START 201
  55. #define RC5_STATUS_INDICATION 202
  56. #define RC5_STATUS_CONTROL 203
  57. #define RC5_STATUS_SYSTEM 204
  58. #define RC5_STATUS_COMMAND 205
  59. #define RC5_STATUS_SYNC 206
  60. extern char *remote_log_buf;
  61. typedef struct {
  62. unsigned short time;
  63. unsigned char level; //0-low level 1-high level
  64. } win_time_t;
  65. static win_time_t left_window = {.time = 0, .level = 1 };
  66. static int sub_status;
  67. static int bit_0_1;
  68. static int system_field;
  69. static int dbg_printk(const char *fmt, ...)
  70. {
  71. char buf[100];
  72. va_list args;
  73. va_start(args, fmt);
  74. vscnprintf(buf, 100, fmt, args);
  75. if (strlen(remote_log_buf) + (strlen(buf) + 64) > REMOTE_LOG_BUF_LEN) {
  76. remote_log_buf[0] = '\0';
  77. }
  78. strcat(remote_log_buf, buf);
  79. va_end(args);
  80. return 0;
  81. }
  82. //clear sub status and set new status
  83. static inline void goto_status(unsigned long data, int status)
  84. {
  85. struct remote *remote_data = (struct remote *)data;
  86. sub_status = 0;
  87. remote_data->step = status;
  88. }
  89. static inline int rc5_bit1_high(win_time_t * win)
  90. {
  91. if (win->level == 1 && win->time == 1) {
  92. left_window.time = 0;
  93. return 0;
  94. } else {
  95. return -1;
  96. }
  97. }
  98. static inline int rc5_bit1_low(win_time_t * win)
  99. {
  100. if (win->level == 0 && win->time >= 1) {
  101. left_window.time = win->time - 1;
  102. return 0;
  103. } else {
  104. return -1;
  105. }
  106. }
  107. static inline int rc5_bit0_low(win_time_t * win)
  108. {
  109. if (win->level == 0 && win->time == 1) {
  110. left_window.time = 0;
  111. return 0;
  112. } else {
  113. return -1;
  114. }
  115. }
  116. static inline int rc5_bit0_high(win_time_t * win)
  117. {
  118. if (win->level == 1 && win->time >= 1) {
  119. left_window.time = win->time - 1;
  120. return 0;
  121. } else {
  122. return -1;
  123. }
  124. }
  125. /*
  126. check the pulse is the first half of 0 bit or 1 bit
  127. return: 0-----0 bit
  128. 1-----1 bit
  129. -1----invalid pulse
  130. */
  131. static inline int check_start_level(win_time_t * win)
  132. {
  133. if (rc5_bit0_low(win) == 0) {
  134. return 0;
  135. }
  136. if (rc5_bit1_high(win) == 0) {
  137. return 1;
  138. }
  139. return -1;
  140. }
  141. //return width which base rate is RC5_UNIT_T
  142. static inline int get_pulse_width(unsigned long data)
  143. {
  144. struct remote *remote_data = (struct remote *)data;
  145. int pulse_width;
  146. const char *state;
  147. int num;
  148. int ret;
  149. unsigned int range[2] =
  150. { remote_data->time_window[2], remote_data->time_window[3] };
  151. pulse_width = (am_remote_read_reg(AM_IR_DEC_REG1) & 0x1FFF0000) >> 16;
  152. num = pulse_width / RC5_UNIT_T;
  153. if (pulse_width > num * range[0] && pulse_width < num * range[1]) {
  154. ret = num;
  155. } else if (pulse_width > (num + 1) * range[0]
  156. && pulse_width < (num + 1) * range[1]) {
  157. ret = num + 1;
  158. } else {
  159. ret = 0;
  160. }
  161. state = remote_data->step == REMOTE_STATUS_WAIT ? "wait" :
  162. remote_data->step == RC5_STATUS_START ? "start" :
  163. remote_data->step == RC5_STATUS_INDICATION ? "indication" :
  164. remote_data->step == RC5_STATUS_CONTROL ? "control" :
  165. remote_data->step == RC5_STATUS_SYSTEM ? "system" :
  166. remote_data->step == RC5_STATUS_COMMAND ? "command" :
  167. remote_data->step == RC5_STATUS_SYNC ? "sync" : NULL;
  168. dbg_printk("-----------------------pulse_wdith:%d ret:%d state:%s \n",
  169. pulse_width, ret, state);
  170. return ret;
  171. }
  172. static inline void kbd_rc5_wait(unsigned long data, win_time_t * window)
  173. {
  174. //unsigned short pulse_width;
  175. struct remote *remote_data = (struct remote *)data;
  176. //pulse_width = get_pulse_width(data);//ignore first window
  177. remote_data->step = RC5_STATUS_START;
  178. remote_data->cur_keycode = 0;
  179. remote_data->bit_num = remote_data->bit_count;
  180. left_window.level = 1;
  181. left_window.time = 0;
  182. sub_status = 0;
  183. system_field = 0;
  184. }
  185. /*
  186. start bit 1
  187. */
  188. static inline void kbd_rc5_start(unsigned long data, win_time_t * window)
  189. {
  190. if (window->time == 1) {
  191. left_window.time = 0;
  192. goto_status(data, RC5_STATUS_INDICATION);
  193. } else {
  194. goto_status(data, REMOTE_STATUS_WAIT);
  195. }
  196. }
  197. /*
  198. indication bit 1
  199. sub_status: 0----->high 1T level, 1----->low 1T level
  200. */
  201. static inline void kbd_rc5_indication(unsigned long data, win_time_t * window)
  202. {
  203. if (rc5_bit1_high(window) == 0) {
  204. sub_status = 1;
  205. } else if (rc5_bit1_low(window) == 0 && sub_status == 1) {
  206. goto_status(data, RC5_STATUS_CONTROL);
  207. } else {
  208. goto_status(data, REMOTE_STATUS_WAIT);
  209. }
  210. }
  211. /*
  212. control bit: 1 bit, 0 or 1
  213. sub_status: 0---->low or high 1T level, 1------>high or low 1T level
  214. */
  215. static inline void kbd_rc5_control(unsigned long data, win_time_t * window)
  216. {
  217. if (sub_status == 0) {
  218. bit_0_1 = check_start_level(window);
  219. if (bit_0_1 == -1) {
  220. goto_status(data, REMOTE_STATUS_WAIT);
  221. return;
  222. }
  223. sub_status++;
  224. } else if (sub_status == 1) {
  225. if ((bit_0_1 == 0 && rc5_bit0_high(window) == 0)
  226. || (bit_0_1 == 1 && rc5_bit1_low(window) == 0)) {
  227. goto_status(data, RC5_STATUS_SYSTEM);
  228. } else {
  229. goto_status(data, REMOTE_STATUS_WAIT);
  230. }
  231. } else {
  232. goto_status(data, REMOTE_STATUS_WAIT);
  233. }
  234. }
  235. /*
  236. system bits: 5 bit
  237. sub_status: 0,1----->1 bit, 2,3---->2 bit
  238. 4,5----->3 bit, 6,7---->4 bit
  239. 8,9----->5 bit
  240. */
  241. static inline void kbd_rc5_system(unsigned long data, win_time_t * window)
  242. {
  243. if (sub_status % 2 == 0) {
  244. bit_0_1 = check_start_level(window);
  245. if (bit_0_1 == -1) {
  246. goto_status(data, REMOTE_STATUS_WAIT);
  247. return;
  248. }
  249. } else {
  250. if (bit_0_1 == 0 && rc5_bit0_high(window) == 0) {
  251. //do nothing
  252. } else if (bit_0_1 == 1 && rc5_bit1_low(window) == 0) {
  253. system_field |= (1 << (sub_status >> 1));
  254. } else {
  255. goto_status(data, REMOTE_STATUS_WAIT);
  256. return;
  257. }
  258. }
  259. sub_status++;
  260. if (sub_status == 10) {
  261. dbg_printk("system value: 0x%x\n", system_field);
  262. goto_status(data, RC5_STATUS_COMMAND);
  263. }
  264. }
  265. /*
  266. command bits: 6 bit
  267. sub_status: 0,1----->1 bit, 2,3---->2 bit
  268. 4,5----->3 bit, 6,7---->4 bit
  269. 8,9----->5 bit, 10,11-->6 bit
  270. */
  271. static inline void kbd_rc5_command(unsigned long data, win_time_t * window)
  272. {
  273. struct remote *remote_data = (struct remote *)data;
  274. if (sub_status % 2 == 0) {
  275. bit_0_1 = check_start_level(window);
  276. if (bit_0_1 == -1) {
  277. goto_status(data, REMOTE_STATUS_WAIT);
  278. return;
  279. }
  280. } else {
  281. if (bit_0_1 == 0 && rc5_bit0_high(window) == 0) {
  282. remote_data->bit_num--;
  283. } else if (bit_0_1 == 1 && rc5_bit1_low(window) == 0) {
  284. remote_data->cur_keycode |=
  285. 1 << (remote_data->bit_count - remote_data->bit_num);
  286. remote_data->bit_num--;
  287. } else {
  288. goto_status(data, REMOTE_STATUS_WAIT);
  289. return;
  290. }
  291. }
  292. if (remote_data->bit_num == 0) { //send the key
  293. dbg_printk("send key cur_keycode[0x%x]\n", remote_data->cur_keycode);
  294. remote_data->send_data = 1;
  295. fiq_bridge_pulse_trigger(&remote_data->fiq_handle_item);
  296. } else if (remote_data->bit_num == 1) { //the last bit is 0, so the high level can not be captured
  297. if (sub_status == 10 && bit_0_1 == 0 && left_window.time == 0) {
  298. dbg_printk("11send key cur_keycode[0x%x]\n", remote_data->cur_keycode);
  299. remote_data->send_data = 1;
  300. fiq_bridge_pulse_trigger(&remote_data->fiq_handle_item);
  301. }
  302. }
  303. sub_status++;
  304. if (sub_status == 12) {
  305. goto_status(data, RC5_STATUS_SYNC);
  306. }
  307. }
  308. static inline void kbd_rc5_sync(unsigned long data, win_time_t * window)
  309. {
  310. struct remote *remote_data = (struct remote *)data;
  311. if (window->time > 0) {
  312. window->time = 0;
  313. }
  314. remote_data->step = RC5_STATUS_SYNC;
  315. fiq_bridge_pulse_trigger(&remote_data->fiq_handle_item);
  316. }
  317. static void kbd_handle_sm(unsigned long data, win_time_t * window)
  318. {
  319. struct remote *remote_data = (struct remote *)data;
  320. switch (remote_data->step) {
  321. case REMOTE_STATUS_WAIT:
  322. kbd_rc5_wait(data, window);
  323. break;
  324. case RC5_STATUS_START:
  325. kbd_rc5_start(data, window);
  326. break;
  327. case RC5_STATUS_INDICATION:
  328. kbd_rc5_indication(data, window);
  329. break;
  330. case RC5_STATUS_CONTROL:
  331. kbd_rc5_control(data, window);
  332. break;
  333. case RC5_STATUS_SYSTEM:
  334. kbd_rc5_system(data, window);
  335. break;
  336. case RC5_STATUS_COMMAND:
  337. kbd_rc5_command(data, window);
  338. break;
  339. case RC5_STATUS_SYNC:
  340. kbd_rc5_sync(data, window);
  341. break;
  342. default:
  343. break;
  344. }
  345. if (left_window.time > 0) {
  346. kbd_handle_sm(data, &left_window);
  347. }
  348. }
  349. void remote_rc5_reprot_key(unsigned long data)
  350. {
  351. struct remote *remote_data = (struct remote *)data;
  352. int current_jiffies = jiffies;
  353. if (((current_jiffies - remote_data->last_jiffies) > 20)
  354. && (remote_data->step <= RC5_STATUS_SYNC)) {
  355. remote_data->step = REMOTE_STATUS_WAIT;
  356. }
  357. remote_data->last_jiffies = current_jiffies; //ignore a little msecs
  358. left_window.time = get_pulse_width(data);
  359. left_window.level = (left_window.level == 0) ? 1 : 0;
  360. kbd_handle_sm(data, &left_window);
  361. }
  362. irqreturn_t remote_rc5_bridge_isr(int irq, void *dev_id)
  363. {
  364. struct remote *remote_data = (struct remote *)dev_id;
  365. if (remote_data->send_data) { //report key
  366. remote_data->step = RC5_STATUS_SYNC;
  367. remote_send_key(remote_data->input, remote_data->cur_keycode, 1);
  368. remote_data->send_data = 0;
  369. }
  370. remote_data->timer.data = (unsigned long)remote_data;
  371. mod_timer(&remote_data->timer,
  372. jiffies + msecs_to_jiffies(remote_data->release_delay));
  373. return IRQ_HANDLED;
  374. }