tosakbd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * Keyboard driver for Sharp Tosa models (SL-6000x)
  3. *
  4. * Copyright (c) 2005 Dirk Opfer
  5. * Copyright (c) 2007 Dmitry Baryshkov
  6. *
  7. * Based on xtkbd.c/locomkbd.c/corgikbd.c
  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 version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/input.h>
  18. #include <linux/delay.h>
  19. #include <linux/interrupt.h>
  20. #include <asm/arch/gpio.h>
  21. #include <asm/arch/tosa.h>
  22. #define KB_ROWMASK(r) (1 << (r))
  23. #define SCANCODE(r, c) (((r)<<4) + (c) + 1)
  24. #define NR_SCANCODES SCANCODE(TOSA_KEY_SENSE_NUM - 1, TOSA_KEY_STROBE_NUM - 1) + 1
  25. #define SCAN_INTERVAL (HZ/10)
  26. #define KB_DISCHARGE_DELAY 10
  27. #define KB_ACTIVATE_DELAY 10
  28. static unsigned int tosakbd_keycode[NR_SCANCODES] = {
  29. 0,
  30. 0, KEY_W, 0, 0, 0, KEY_K, KEY_BACKSPACE, KEY_P,
  31. 0, 0, 0, 0, 0, 0, 0, 0,
  32. KEY_Q, KEY_E, KEY_T, KEY_Y, 0, KEY_O, KEY_I, KEY_COMMA,
  33. 0, 0, 0, 0, 0, 0, 0, 0,
  34. KEY_A, KEY_D, KEY_G, KEY_U, 0, KEY_L, KEY_ENTER, KEY_DOT,
  35. 0, 0, 0, 0, 0, 0, 0, 0,
  36. KEY_Z, KEY_C, KEY_V, KEY_J, TOSA_KEY_ADDRESSBOOK, TOSA_KEY_CANCEL, TOSA_KEY_CENTER, TOSA_KEY_OK,
  37. KEY_LEFTSHIFT, 0, 0, 0, 0, 0, 0, 0,
  38. KEY_S, KEY_R, KEY_B, KEY_N, TOSA_KEY_CALENDAR, TOSA_KEY_HOMEPAGE, KEY_LEFTCTRL, TOSA_KEY_LIGHT,
  39. 0, KEY_RIGHTSHIFT, 0, 0, 0, 0, 0, 0,
  40. KEY_TAB, KEY_SLASH, KEY_H, KEY_M, TOSA_KEY_MENU, 0, KEY_UP, 0,
  41. 0, 0, TOSA_KEY_FN, 0, 0, 0, 0, 0,
  42. KEY_X, KEY_F, KEY_SPACE, KEY_APOSTROPHE, TOSA_KEY_MAIL, KEY_LEFT, KEY_DOWN, KEY_RIGHT,
  43. 0, 0, 0,
  44. };
  45. struct tosakbd {
  46. unsigned int keycode[ARRAY_SIZE(tosakbd_keycode)];
  47. struct input_dev *input;
  48. spinlock_t lock; /* protect kbd scanning */
  49. struct timer_list timer;
  50. };
  51. /* Helper functions for reading the keyboard matrix
  52. * Note: We should really be using pxa_gpio_mode to alter GPDR but it
  53. * requires a function call per GPIO bit which is excessive
  54. * when we need to access 12 bits at once, multiple times.
  55. * These functions must be called within local_irq_save()/local_irq_restore()
  56. * or similar.
  57. */
  58. #define GET_ROWS_STATUS(c) ((GPLR2 & TOSA_GPIO_ALL_SENSE_BIT) >> TOSA_GPIO_ALL_SENSE_RSHIFT)
  59. static inline void tosakbd_discharge_all(void)
  60. {
  61. /* STROBE All HiZ */
  62. GPCR1 = TOSA_GPIO_HIGH_STROBE_BIT;
  63. GPDR1 &= ~TOSA_GPIO_HIGH_STROBE_BIT;
  64. GPCR2 = TOSA_GPIO_LOW_STROBE_BIT;
  65. GPDR2 &= ~TOSA_GPIO_LOW_STROBE_BIT;
  66. }
  67. static inline void tosakbd_activate_all(void)
  68. {
  69. /* STROBE ALL -> High */
  70. GPSR1 = TOSA_GPIO_HIGH_STROBE_BIT;
  71. GPDR1 |= TOSA_GPIO_HIGH_STROBE_BIT;
  72. GPSR2 = TOSA_GPIO_LOW_STROBE_BIT;
  73. GPDR2 |= TOSA_GPIO_LOW_STROBE_BIT;
  74. udelay(KB_DISCHARGE_DELAY);
  75. /* STATE CLEAR */
  76. GEDR2 |= TOSA_GPIO_ALL_SENSE_BIT;
  77. }
  78. static inline void tosakbd_activate_col(int col)
  79. {
  80. if (col <= 5) {
  81. /* STROBE col -> High, not col -> HiZ */
  82. GPSR1 = TOSA_GPIO_STROBE_BIT(col);
  83. GPDR1 = (GPDR1 & ~TOSA_GPIO_HIGH_STROBE_BIT) | TOSA_GPIO_STROBE_BIT(col);
  84. } else {
  85. /* STROBE col -> High, not col -> HiZ */
  86. GPSR2 = TOSA_GPIO_STROBE_BIT(col);
  87. GPDR2 = (GPDR2 & ~TOSA_GPIO_LOW_STROBE_BIT) | TOSA_GPIO_STROBE_BIT(col);
  88. }
  89. }
  90. static inline void tosakbd_reset_col(int col)
  91. {
  92. if (col <= 5) {
  93. /* STROBE col -> Low */
  94. GPCR1 = TOSA_GPIO_STROBE_BIT(col);
  95. /* STROBE col -> out, not col -> HiZ */
  96. GPDR1 = (GPDR1 & ~TOSA_GPIO_HIGH_STROBE_BIT) | TOSA_GPIO_STROBE_BIT(col);
  97. } else {
  98. /* STROBE col -> Low */
  99. GPCR2 = TOSA_GPIO_STROBE_BIT(col);
  100. /* STROBE col -> out, not col -> HiZ */
  101. GPDR2 = (GPDR2 & ~TOSA_GPIO_LOW_STROBE_BIT) | TOSA_GPIO_STROBE_BIT(col);
  102. }
  103. }
  104. /*
  105. * The tosa keyboard only generates interrupts when a key is pressed.
  106. * So when a key is pressed, we enable a timer. This timer scans the
  107. * keyboard, and this is how we detect when the key is released.
  108. */
  109. /* Scan the hardware keyboard and push any changes up through the input layer */
  110. static void tosakbd_scankeyboard(struct platform_device *dev)
  111. {
  112. struct tosakbd *tosakbd = platform_get_drvdata(dev);
  113. unsigned int row, col, rowd;
  114. unsigned long flags;
  115. unsigned int num_pressed = 0;
  116. spin_lock_irqsave(&tosakbd->lock, flags);
  117. for (col = 0; col < TOSA_KEY_STROBE_NUM; col++) {
  118. /*
  119. * Discharge the output driver capacitatance
  120. * in the keyboard matrix. (Yes it is significant..)
  121. */
  122. tosakbd_discharge_all();
  123. udelay(KB_DISCHARGE_DELAY);
  124. tosakbd_activate_col(col);
  125. udelay(KB_ACTIVATE_DELAY);
  126. rowd = GET_ROWS_STATUS(col);
  127. for (row = 0; row < TOSA_KEY_SENSE_NUM; row++) {
  128. unsigned int scancode, pressed;
  129. scancode = SCANCODE(row, col);
  130. pressed = rowd & KB_ROWMASK(row);
  131. if (pressed && !tosakbd->keycode[scancode])
  132. dev_warn(&dev->dev,
  133. "unhandled scancode: 0x%02x\n",
  134. scancode);
  135. input_report_key(tosakbd->input,
  136. tosakbd->keycode[scancode],
  137. pressed);
  138. if (pressed)
  139. num_pressed++;
  140. }
  141. tosakbd_reset_col(col);
  142. }
  143. tosakbd_activate_all();
  144. input_sync(tosakbd->input);
  145. /* if any keys are pressed, enable the timer */
  146. if (num_pressed)
  147. mod_timer(&tosakbd->timer, jiffies + SCAN_INTERVAL);
  148. spin_unlock_irqrestore(&tosakbd->lock, flags);
  149. }
  150. /*
  151. * tosa keyboard interrupt handler.
  152. */
  153. static irqreturn_t tosakbd_interrupt(int irq, void *__dev)
  154. {
  155. struct platform_device *dev = __dev;
  156. struct tosakbd *tosakbd = platform_get_drvdata(dev);
  157. if (!timer_pending(&tosakbd->timer)) {
  158. /** wait chattering delay **/
  159. udelay(20);
  160. tosakbd_scankeyboard(dev);
  161. }
  162. return IRQ_HANDLED;
  163. }
  164. /*
  165. * tosa timer checking for released keys
  166. */
  167. static void tosakbd_timer_callback(unsigned long __dev)
  168. {
  169. struct platform_device *dev = (struct platform_device *)__dev;
  170. tosakbd_scankeyboard(dev);
  171. }
  172. #ifdef CONFIG_PM
  173. static int tosakbd_suspend(struct platform_device *dev, pm_message_t state)
  174. {
  175. struct tosakbd *tosakbd = platform_get_drvdata(dev);
  176. del_timer_sync(&tosakbd->timer);
  177. return 0;
  178. }
  179. static int tosakbd_resume(struct platform_device *dev)
  180. {
  181. tosakbd_scankeyboard(dev);
  182. return 0;
  183. }
  184. #else
  185. #define tosakbd_suspend NULL
  186. #define tosakbd_resume NULL
  187. #endif
  188. static int __devinit tosakbd_probe(struct platform_device *pdev) {
  189. int i;
  190. struct tosakbd *tosakbd;
  191. struct input_dev *input_dev;
  192. int error;
  193. tosakbd = kzalloc(sizeof(struct tosakbd), GFP_KERNEL);
  194. if (!tosakbd)
  195. return -ENOMEM;
  196. input_dev = input_allocate_device();
  197. if (!input_dev) {
  198. kfree(tosakbd);
  199. return -ENOMEM;
  200. }
  201. platform_set_drvdata(pdev, tosakbd);
  202. spin_lock_init(&tosakbd->lock);
  203. /* Init Keyboard rescan timer */
  204. init_timer(&tosakbd->timer);
  205. tosakbd->timer.function = tosakbd_timer_callback;
  206. tosakbd->timer.data = (unsigned long) pdev;
  207. tosakbd->input = input_dev;
  208. input_set_drvdata(input_dev, tosakbd);
  209. input_dev->name = "Tosa Keyboard";
  210. input_dev->phys = "tosakbd/input0";
  211. input_dev->dev.parent = &pdev->dev;
  212. input_dev->id.bustype = BUS_HOST;
  213. input_dev->id.vendor = 0x0001;
  214. input_dev->id.product = 0x0001;
  215. input_dev->id.version = 0x0100;
  216. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
  217. input_dev->keycode = tosakbd->keycode;
  218. input_dev->keycodesize = sizeof(unsigned int);
  219. input_dev->keycodemax = ARRAY_SIZE(tosakbd_keycode);
  220. memcpy(tosakbd->keycode, tosakbd_keycode, sizeof(tosakbd_keycode));
  221. for (i = 0; i < ARRAY_SIZE(tosakbd_keycode); i++)
  222. __set_bit(tosakbd->keycode[i], input_dev->keybit);
  223. clear_bit(0, input_dev->keybit);
  224. /* Setup sense interrupts - RisingEdge Detect, sense lines as inputs */
  225. for (i = 0; i < TOSA_KEY_SENSE_NUM; i++) {
  226. int gpio = TOSA_GPIO_KEY_SENSE(i);
  227. int irq;
  228. error = gpio_request(gpio, "tosakbd");
  229. if (error < 0) {
  230. printk(KERN_ERR "tosakbd: failed to request GPIO %d, "
  231. " error %d\n", gpio, error);
  232. goto fail;
  233. }
  234. error = gpio_direction_input(TOSA_GPIO_KEY_SENSE(i));
  235. if (error < 0) {
  236. printk(KERN_ERR "tosakbd: failed to configure input"
  237. " direction for GPIO %d, error %d\n",
  238. gpio, error);
  239. gpio_free(gpio);
  240. goto fail;
  241. }
  242. irq = gpio_to_irq(gpio);
  243. if (irq < 0) {
  244. error = irq;
  245. printk(KERN_ERR "gpio-keys: Unable to get irq number"
  246. " for GPIO %d, error %d\n",
  247. gpio, error);
  248. gpio_free(gpio);
  249. goto fail;
  250. }
  251. error = request_irq(irq, tosakbd_interrupt,
  252. IRQF_DISABLED | IRQF_TRIGGER_RISING,
  253. "tosakbd", pdev);
  254. if (error) {
  255. printk("tosakbd: Can't get IRQ: %d: error %d!\n",
  256. irq, error);
  257. gpio_free(gpio);
  258. goto fail;
  259. }
  260. }
  261. /* Set Strobe lines as outputs - set high */
  262. for (i = 0; i < TOSA_KEY_STROBE_NUM; i++) {
  263. int gpio = TOSA_GPIO_KEY_STROBE(i);
  264. error = gpio_request(gpio, "tosakbd");
  265. if (error < 0) {
  266. printk(KERN_ERR "tosakbd: failed to request GPIO %d, "
  267. " error %d\n", gpio, error);
  268. goto fail2;
  269. }
  270. error = gpio_direction_output(gpio, 1);
  271. if (error < 0) {
  272. printk(KERN_ERR "tosakbd: failed to configure input"
  273. " direction for GPIO %d, error %d\n",
  274. gpio, error);
  275. gpio_free(gpio);
  276. goto fail;
  277. }
  278. }
  279. error = input_register_device(input_dev);
  280. if (error) {
  281. printk(KERN_ERR "tosakbd: Unable to register input device, "
  282. "error: %d\n", error);
  283. goto fail;
  284. }
  285. printk(KERN_INFO "input: Tosa Keyboard Registered\n");
  286. return 0;
  287. fail2:
  288. while (--i >= 0)
  289. gpio_free(TOSA_GPIO_KEY_STROBE(i));
  290. i = TOSA_KEY_SENSE_NUM;
  291. fail:
  292. while (--i >= 0) {
  293. free_irq(gpio_to_irq(TOSA_GPIO_KEY_SENSE(i)), pdev);
  294. gpio_free(TOSA_GPIO_KEY_SENSE(i));
  295. }
  296. platform_set_drvdata(pdev, NULL);
  297. input_free_device(input_dev);
  298. kfree(tosakbd);
  299. return error;
  300. }
  301. static int __devexit tosakbd_remove(struct platform_device *dev) {
  302. int i;
  303. struct tosakbd *tosakbd = platform_get_drvdata(dev);
  304. for (i = 0; i < TOSA_KEY_STROBE_NUM; i++)
  305. gpio_free(TOSA_GPIO_KEY_STROBE(i));
  306. for (i = 0; i < TOSA_KEY_SENSE_NUM; i++) {
  307. free_irq(gpio_to_irq(TOSA_GPIO_KEY_SENSE(i)), dev);
  308. gpio_free(TOSA_GPIO_KEY_SENSE(i));
  309. }
  310. del_timer_sync(&tosakbd->timer);
  311. input_unregister_device(tosakbd->input);
  312. kfree(tosakbd);
  313. return 0;
  314. }
  315. static struct platform_driver tosakbd_driver = {
  316. .probe = tosakbd_probe,
  317. .remove = __devexit_p(tosakbd_remove),
  318. .suspend = tosakbd_suspend,
  319. .resume = tosakbd_resume,
  320. .driver = {
  321. .name = "tosa-keyboard",
  322. },
  323. };
  324. static int __devinit tosakbd_init(void)
  325. {
  326. return platform_driver_register(&tosakbd_driver);
  327. }
  328. static void __exit tosakbd_exit(void)
  329. {
  330. platform_driver_unregister(&tosakbd_driver);
  331. }
  332. module_init(tosakbd_init);
  333. module_exit(tosakbd_exit);
  334. MODULE_AUTHOR("Dirk Opfer <Dirk@Opfer-Online.de>");
  335. MODULE_DESCRIPTION("Tosa Keyboard Driver");
  336. MODULE_LICENSE("GPL v2");