corgikbd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Keyboard driver for Sharp Corgi models (SL-C7xx)
  3. *
  4. * Copyright (c) 2004-2005 Richard Purdie
  5. *
  6. * Based on xtkbd.c/locomkbd.c
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/device.h>
  15. #include <linux/init.h>
  16. #include <linux/input.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <asm/irq.h>
  21. #include <asm/arch/corgi.h>
  22. #include <asm/arch/hardware.h>
  23. #include <asm/arch/pxa-regs.h>
  24. #include <asm/hardware/scoop.h>
  25. #define KB_ROWS 8
  26. #define KB_COLS 12
  27. #define KB_ROWMASK(r) (1 << (r))
  28. #define SCANCODE(r,c) ( ((r)<<4) + (c) + 1 )
  29. /* zero code, 124 scancodes + 3 hinge combinations */
  30. #define NR_SCANCODES ( SCANCODE(KB_ROWS-1,KB_COLS-1) +1 +1 +3 )
  31. #define SCAN_INTERVAL (HZ/10)
  32. #define CORGIKBD_PRESSED 1
  33. #define HINGE_SCAN_INTERVAL (HZ/4)
  34. #define CORGI_KEY_CALENDER KEY_F1
  35. #define CORGI_KEY_ADDRESS KEY_F2
  36. #define CORGI_KEY_FN KEY_F3
  37. #define CORGI_KEY_OFF KEY_SUSPEND
  38. #define CORGI_KEY_EXOK KEY_F5
  39. #define CORGI_KEY_EXCANCEL KEY_F6
  40. #define CORGI_KEY_EXJOGDOWN KEY_F7
  41. #define CORGI_KEY_EXJOGUP KEY_F8
  42. #define CORGI_KEY_JAP1 KEY_LEFTCTRL
  43. #define CORGI_KEY_JAP2 KEY_LEFTALT
  44. #define CORGI_KEY_OK KEY_F11
  45. #define CORGI_KEY_MENU KEY_F12
  46. #define CORGI_HINGE_0 KEY_KP0
  47. #define CORGI_HINGE_1 KEY_KP1
  48. #define CORGI_HINGE_2 KEY_KP2
  49. static unsigned char corgikbd_keycode[NR_SCANCODES] = {
  50. 0, /* 0 */
  51. 0, KEY_1, KEY_3, KEY_5, KEY_6, KEY_7, KEY_9, KEY_0, KEY_BACKSPACE, 0, 0, 0, 0, 0, 0, 0, /* 1-16 */
  52. 0, KEY_2, KEY_4, KEY_R, KEY_Y, KEY_8, KEY_I, KEY_O, KEY_P, 0, 0, 0, 0, 0, 0, 0, /* 17-32 */
  53. KEY_TAB, KEY_Q, KEY_E, KEY_T, KEY_G, KEY_U, KEY_J, KEY_K, 0, 0, 0, 0, 0, 0, 0, 0, /* 33-48 */
  54. CORGI_KEY_CALENDER, KEY_W, KEY_S, KEY_F, KEY_V, KEY_H, KEY_M, KEY_L, 0, KEY_RIGHTSHIFT, 0, 0, 0, 0, 0, 0, /* 49-64 */
  55. CORGI_KEY_ADDRESS, KEY_A, KEY_D, KEY_C, KEY_B, KEY_N, KEY_DOT, 0, KEY_ENTER, 0, KEY_LEFTSHIFT, 0, 0, 0, 0, 0, /* 65-80 */
  56. KEY_MAIL, KEY_Z, KEY_X, KEY_MINUS, KEY_SPACE, KEY_COMMA, 0, KEY_UP, 0, 0, 0, CORGI_KEY_FN, 0, 0, 0, 0, /* 81-96 */
  57. KEY_SYSRQ, CORGI_KEY_JAP1, CORGI_KEY_JAP2, KEY_CANCEL, CORGI_KEY_OK, CORGI_KEY_MENU, KEY_LEFT, KEY_DOWN, KEY_RIGHT, 0, 0, 0, 0, 0, 0, 0, /* 97-112 */
  58. CORGI_KEY_OFF, CORGI_KEY_EXOK, CORGI_KEY_EXCANCEL, CORGI_KEY_EXJOGDOWN, CORGI_KEY_EXJOGUP, 0, 0, 0, 0, 0, 0, 0, /* 113-124 */
  59. CORGI_HINGE_0, CORGI_HINGE_1, CORGI_HINGE_2 /* 125-127 */
  60. };
  61. struct corgikbd {
  62. unsigned char keycode[ARRAY_SIZE(corgikbd_keycode)];
  63. struct input_dev input;
  64. char phys[32];
  65. unsigned char state[ARRAY_SIZE(corgikbd_keycode)];
  66. spinlock_t lock;
  67. struct timer_list timer;
  68. struct timer_list htimer;
  69. };
  70. static void handle_scancode(unsigned int pressed,unsigned int scancode, struct corgikbd *corgikbd_data)
  71. {
  72. if (pressed && !(corgikbd_data->state[scancode] & CORGIKBD_PRESSED)) {
  73. corgikbd_data->state[scancode] |= CORGIKBD_PRESSED;
  74. input_report_key(&corgikbd_data->input, corgikbd_data->keycode[scancode], 1);
  75. if (corgikbd_data->keycode[scancode] == CORGI_KEY_OFF)
  76. input_event(&corgikbd_data->input, EV_PWR, CORGI_KEY_OFF, 1);
  77. } else if (!pressed && corgikbd_data->state[scancode] & CORGIKBD_PRESSED) {
  78. corgikbd_data->state[scancode] &= ~CORGIKBD_PRESSED;
  79. input_report_key(&corgikbd_data->input, corgikbd_data->keycode[scancode], 0);
  80. }
  81. }
  82. #define KB_DISCHARGE_DELAY 10
  83. #define KB_ACTIVATE_DELAY 10
  84. /* Helper functions for reading the keyboard matrix
  85. * Note: We should really be using pxa_gpio_mode to alter GPDR but it
  86. * requires a function call per GPIO bit which is excessive
  87. * when we need to access 12 bits at once multiple times.
  88. * These functions must be called within local_irq_save()/local_irq_restore()
  89. * or similar.
  90. */
  91. static inline void corgikbd_discharge_all(void)
  92. {
  93. // STROBE All HiZ
  94. GPCR2 = CORGI_GPIO_ALL_STROBE_BIT;
  95. GPDR2 &= ~CORGI_GPIO_ALL_STROBE_BIT;
  96. }
  97. static inline void corgikbd_activate_all(void)
  98. {
  99. // STROBE ALL -> High
  100. GPSR2 = CORGI_GPIO_ALL_STROBE_BIT;
  101. GPDR2 |= CORGI_GPIO_ALL_STROBE_BIT;
  102. udelay(KB_DISCHARGE_DELAY);
  103. // Clear any interrupts we may have triggered when altering the GPIO lines
  104. GEDR1 = CORGI_GPIO_HIGH_SENSE_BIT;
  105. GEDR2 = CORGI_GPIO_LOW_SENSE_BIT;
  106. }
  107. static inline void corgikbd_activate_col(int col)
  108. {
  109. // STROBE col -> High, not col -> HiZ
  110. GPSR2 = CORGI_GPIO_STROBE_BIT(col);
  111. GPDR2 = (GPDR2 & ~CORGI_GPIO_ALL_STROBE_BIT) | CORGI_GPIO_STROBE_BIT(col);
  112. }
  113. static inline void corgikbd_reset_col(int col)
  114. {
  115. // STROBE col -> Low
  116. GPCR2 = CORGI_GPIO_STROBE_BIT(col);
  117. // STROBE col -> out, not col -> HiZ
  118. GPDR2 = (GPDR2 & ~CORGI_GPIO_ALL_STROBE_BIT) | CORGI_GPIO_STROBE_BIT(col);
  119. }
  120. #define GET_ROWS_STATUS(c) (((GPLR1 & CORGI_GPIO_HIGH_SENSE_BIT) >> CORGI_GPIO_HIGH_SENSE_RSHIFT) | ((GPLR2 & CORGI_GPIO_LOW_SENSE_BIT) << CORGI_GPIO_LOW_SENSE_LSHIFT))
  121. /*
  122. * The corgi keyboard only generates interrupts when a key is pressed.
  123. * When a key is pressed, we enable a timer which then scans the
  124. * keyboard to detect when the key is released.
  125. */
  126. /* Scan the hardware keyboard and push any changes up through the input layer */
  127. static void corgikbd_scankeyboard(struct corgikbd *corgikbd_data, struct pt_regs *regs)
  128. {
  129. unsigned int row, col, rowd, scancode;
  130. unsigned long flags;
  131. unsigned int num_pressed;
  132. spin_lock_irqsave(&corgikbd_data->lock, flags);
  133. if (regs)
  134. input_regs(&corgikbd_data->input, regs);
  135. num_pressed = 0;
  136. for (col = 0; col < KB_COLS; col++) {
  137. /*
  138. * Discharge the output driver capacitatance
  139. * in the keyboard matrix. (Yes it is significant..)
  140. */
  141. corgikbd_discharge_all();
  142. udelay(KB_DISCHARGE_DELAY);
  143. corgikbd_activate_col(col);
  144. udelay(KB_ACTIVATE_DELAY);
  145. rowd = GET_ROWS_STATUS(col);
  146. for (row = 0; row < KB_ROWS; row++) {
  147. scancode = SCANCODE(row, col);
  148. handle_scancode((rowd & KB_ROWMASK(row)), scancode, corgikbd_data);
  149. if (rowd & KB_ROWMASK(row))
  150. num_pressed++;
  151. }
  152. corgikbd_reset_col(col);
  153. }
  154. corgikbd_activate_all();
  155. input_sync(&corgikbd_data->input);
  156. /* if any keys are pressed, enable the timer */
  157. if (num_pressed)
  158. mod_timer(&corgikbd_data->timer, jiffies + SCAN_INTERVAL);
  159. spin_unlock_irqrestore(&corgikbd_data->lock, flags);
  160. }
  161. /*
  162. * corgi keyboard interrupt handler.
  163. */
  164. static irqreturn_t corgikbd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  165. {
  166. struct corgikbd *corgikbd_data = dev_id;
  167. if (!timer_pending(&corgikbd_data->timer)) {
  168. /** wait chattering delay **/
  169. udelay(20);
  170. corgikbd_scankeyboard(corgikbd_data, regs);
  171. }
  172. return IRQ_HANDLED;
  173. }
  174. /*
  175. * corgi timer checking for released keys
  176. */
  177. static void corgikbd_timer_callback(unsigned long data)
  178. {
  179. struct corgikbd *corgikbd_data = (struct corgikbd *) data;
  180. corgikbd_scankeyboard(corgikbd_data, NULL);
  181. }
  182. /*
  183. * The hinge switches generate no interrupt so they need to be
  184. * monitored by a timer.
  185. *
  186. * When we detect changes, we debounce it and then pass the three
  187. * positions the system can take as keypresses to the input system.
  188. */
  189. #define HINGE_STABLE_COUNT 2
  190. static int sharpsl_hinge_state;
  191. static int hinge_count;
  192. static void corgikbd_hinge_timer(unsigned long data)
  193. {
  194. struct corgikbd *corgikbd_data = (struct corgikbd *) data;
  195. unsigned long gprr;
  196. unsigned long flags;
  197. gprr = read_scoop_reg(SCOOP_GPRR) & (CORGI_SCP_SWA | CORGI_SCP_SWB);
  198. if (gprr != sharpsl_hinge_state) {
  199. hinge_count = 0;
  200. sharpsl_hinge_state = gprr;
  201. } else if (hinge_count < HINGE_STABLE_COUNT) {
  202. hinge_count++;
  203. if (hinge_count >= HINGE_STABLE_COUNT) {
  204. spin_lock_irqsave(&corgikbd_data->lock, flags);
  205. handle_scancode((sharpsl_hinge_state == 0x00), 125, corgikbd_data); /* Keyboard with Landscape Screen */
  206. handle_scancode((sharpsl_hinge_state == 0x08), 126, corgikbd_data); /* No Keyboard with Portrait Screen */
  207. handle_scancode((sharpsl_hinge_state == 0x0c), 127, corgikbd_data); /* Keyboard and Screen Closed */
  208. input_sync(&corgikbd_data->input);
  209. spin_unlock_irqrestore(&corgikbd_data->lock, flags);
  210. }
  211. }
  212. mod_timer(&corgikbd_data->htimer, jiffies + HINGE_SCAN_INTERVAL);
  213. }
  214. static int __init corgikbd_probe(struct device *dev)
  215. {
  216. int i;
  217. struct corgikbd *corgikbd;
  218. corgikbd = kcalloc(1, sizeof(struct corgikbd), GFP_KERNEL);
  219. if (!corgikbd)
  220. return -ENOMEM;
  221. dev_set_drvdata(dev,corgikbd);
  222. strcpy(corgikbd->phys, "corgikbd/input0");
  223. spin_lock_init(corgikbd->lock);
  224. /* Init Keyboard rescan timer */
  225. init_timer(&corgikbd->timer);
  226. corgikbd->timer.function = corgikbd_timer_callback;
  227. corgikbd->timer.data = (unsigned long) corgikbd;
  228. /* Init Hinge Timer */
  229. init_timer(&corgikbd->htimer);
  230. corgikbd->htimer.function = corgikbd_hinge_timer;
  231. corgikbd->htimer.data = (unsigned long) corgikbd;
  232. init_input_dev(&corgikbd->input);
  233. corgikbd->input.private = corgikbd;
  234. corgikbd->input.name = "Corgi Keyboard";
  235. corgikbd->input.dev = dev;
  236. corgikbd->input.phys = corgikbd->phys;
  237. corgikbd->input.id.bustype = BUS_HOST;
  238. corgikbd->input.id.vendor = 0x0001;
  239. corgikbd->input.id.product = 0x0001;
  240. corgikbd->input.id.version = 0x0100;
  241. corgikbd->input.evbit[0] = BIT(EV_KEY) | BIT(EV_REP) | BIT(EV_PWR);
  242. corgikbd->input.keycode = corgikbd->keycode;
  243. corgikbd->input.keycodesize = sizeof(unsigned char);
  244. corgikbd->input.keycodemax = ARRAY_SIZE(corgikbd_keycode);
  245. memcpy(corgikbd->keycode, corgikbd_keycode, sizeof(corgikbd->keycode));
  246. for (i = 0; i < ARRAY_SIZE(corgikbd_keycode); i++)
  247. set_bit(corgikbd->keycode[i], corgikbd->input.keybit);
  248. clear_bit(0, corgikbd->input.keybit);
  249. input_register_device(&corgikbd->input);
  250. mod_timer(&corgikbd->htimer, jiffies + HINGE_SCAN_INTERVAL);
  251. /* Setup sense interrupts - RisingEdge Detect, sense lines as inputs */
  252. for (i = 0; i < CORGI_KEY_SENSE_NUM; i++) {
  253. pxa_gpio_mode(CORGI_GPIO_KEY_SENSE(i) | GPIO_IN);
  254. if (request_irq(CORGI_IRQ_GPIO_KEY_SENSE(i), corgikbd_interrupt,
  255. SA_INTERRUPT, "corgikbd", corgikbd))
  256. printk(KERN_WARNING "corgikbd: Can't get IRQ: %d!\n", i);
  257. else
  258. set_irq_type(CORGI_IRQ_GPIO_KEY_SENSE(i),IRQT_RISING);
  259. }
  260. /* Set Strobe lines as outputs - set high */
  261. for (i = 0; i < CORGI_KEY_STROBE_NUM; i++)
  262. pxa_gpio_mode(CORGI_GPIO_KEY_STROBE(i) | GPIO_OUT | GPIO_DFLT_HIGH);
  263. printk(KERN_INFO "input: Corgi Keyboard Registered\n");
  264. return 0;
  265. }
  266. static int corgikbd_remove(struct device *dev)
  267. {
  268. int i;
  269. struct corgikbd *corgikbd = dev_get_drvdata(dev);
  270. for (i = 0; i < CORGI_KEY_SENSE_NUM; i++)
  271. free_irq(CORGI_IRQ_GPIO_KEY_SENSE(i), corgikbd);
  272. del_timer_sync(&corgikbd->htimer);
  273. del_timer_sync(&corgikbd->timer);
  274. input_unregister_device(&corgikbd->input);
  275. kfree(corgikbd);
  276. return 0;
  277. }
  278. static struct device_driver corgikbd_driver = {
  279. .name = "corgi-keyboard",
  280. .bus = &platform_bus_type,
  281. .probe = corgikbd_probe,
  282. .remove = corgikbd_remove,
  283. };
  284. static int __devinit corgikbd_init(void)
  285. {
  286. return driver_register(&corgikbd_driver);
  287. }
  288. static void __exit corgikbd_exit(void)
  289. {
  290. driver_unregister(&corgikbd_driver);
  291. }
  292. module_init(corgikbd_init);
  293. module_exit(corgikbd_exit);
  294. MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
  295. MODULE_DESCRIPTION("Corgi Keyboard Driver");
  296. MODULE_LICENSE("GPLv2");