tosakbd.c 11 KB

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