bf54x-keys.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * File: drivers/input/keyboard/bf54x-keys.c
  3. * Based on:
  4. * Author: Michael Hennerich <hennerich@blackfin.uclinux.org>
  5. *
  6. * Created:
  7. * Description: keypad driver for Analog Devices Blackfin BF54x Processors
  8. *
  9. *
  10. * Modified:
  11. * Copyright 2007 Analog Devices Inc.
  12. *
  13. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, see the file COPYING, or write
  27. * to the Free Software Foundation, Inc.,
  28. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  29. */
  30. #include <linux/module.h>
  31. #include <linux/version.h>
  32. #include <linux/init.h>
  33. #include <linux/fs.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/irq.h>
  36. #include <linux/sched.h>
  37. #include <linux/pm.h>
  38. #include <linux/sysctl.h>
  39. #include <linux/proc_fs.h>
  40. #include <linux/delay.h>
  41. #include <linux/platform_device.h>
  42. #include <linux/input.h>
  43. #include <asm/portmux.h>
  44. #include <asm/mach/bf54x_keys.h>
  45. #define DRV_NAME "bf54x-keys"
  46. #define TIME_SCALE 100 /* 100 ns */
  47. #define MAX_MULT (0xFF * TIME_SCALE)
  48. #define MAX_RC 8 /* Max Row/Col */
  49. static const u16 per_rows[] = {
  50. P_KEY_ROW7,
  51. P_KEY_ROW6,
  52. P_KEY_ROW5,
  53. P_KEY_ROW4,
  54. P_KEY_ROW3,
  55. P_KEY_ROW2,
  56. P_KEY_ROW1,
  57. P_KEY_ROW0,
  58. 0
  59. };
  60. static const u16 per_cols[] = {
  61. P_KEY_COL7,
  62. P_KEY_COL6,
  63. P_KEY_COL5,
  64. P_KEY_COL4,
  65. P_KEY_COL3,
  66. P_KEY_COL2,
  67. P_KEY_COL1,
  68. P_KEY_COL0,
  69. 0
  70. };
  71. struct bf54x_kpad {
  72. struct input_dev *input;
  73. int irq;
  74. unsigned short lastkey;
  75. unsigned short *keycode;
  76. struct timer_list timer;
  77. unsigned int keyup_test_jiffies;
  78. };
  79. static inline int bfin_kpad_find_key(struct bf54x_kpad *bf54x_kpad,
  80. struct input_dev *input, u16 keyident)
  81. {
  82. u16 i;
  83. for (i = 0; i < input->keycodemax; i++)
  84. if (bf54x_kpad->keycode[i + input->keycodemax] == keyident)
  85. return bf54x_kpad->keycode[i];
  86. return -1;
  87. }
  88. static inline void bfin_keycodecpy(unsigned short *keycode,
  89. const unsigned int *pdata_kc,
  90. unsigned short keymapsize)
  91. {
  92. unsigned int i;
  93. for (i = 0; i < keymapsize; i++) {
  94. keycode[i] = pdata_kc[i] & 0xffff;
  95. keycode[i + keymapsize] = pdata_kc[i] >> 16;
  96. }
  97. }
  98. static inline u16 bfin_kpad_get_prescale(u32 timescale)
  99. {
  100. u32 sclk = get_sclk();
  101. return ((((sclk / 1000) * timescale) / 1024) - 1);
  102. }
  103. static inline u16 bfin_kpad_get_keypressed(struct bf54x_kpad *bf54x_kpad)
  104. {
  105. return (bfin_read_KPAD_STAT() & KPAD_PRESSED);
  106. }
  107. static inline void bfin_kpad_clear_irq(void)
  108. {
  109. bfin_write_KPAD_STAT(0xFFFF);
  110. bfin_write_KPAD_ROWCOL(0xFFFF);
  111. }
  112. static void bfin_kpad_timer(unsigned long data)
  113. {
  114. struct platform_device *pdev = (struct platform_device *) data;
  115. struct bf54x_kpad *bf54x_kpad = platform_get_drvdata(pdev);
  116. if (bfin_kpad_get_keypressed(bf54x_kpad)) {
  117. /* Try again later */
  118. mod_timer(&bf54x_kpad->timer,
  119. jiffies + bf54x_kpad->keyup_test_jiffies);
  120. return;
  121. }
  122. input_report_key(bf54x_kpad->input, bf54x_kpad->lastkey, 0);
  123. input_sync(bf54x_kpad->input);
  124. /* Clear IRQ Status */
  125. bfin_kpad_clear_irq();
  126. enable_irq(bf54x_kpad->irq);
  127. }
  128. static irqreturn_t bfin_kpad_isr(int irq, void *dev_id)
  129. {
  130. struct platform_device *pdev = dev_id;
  131. struct bf54x_kpad *bf54x_kpad = platform_get_drvdata(pdev);
  132. struct input_dev *input = bf54x_kpad->input;
  133. int key;
  134. u16 rowcol = bfin_read_KPAD_ROWCOL();
  135. key = bfin_kpad_find_key(bf54x_kpad, input, rowcol);
  136. input_report_key(input, key, 1);
  137. input_sync(input);
  138. if (bfin_kpad_get_keypressed(bf54x_kpad)) {
  139. disable_irq(bf54x_kpad->irq);
  140. bf54x_kpad->lastkey = key;
  141. mod_timer(&bf54x_kpad->timer,
  142. jiffies + bf54x_kpad->keyup_test_jiffies);
  143. } else {
  144. input_report_key(input, key, 0);
  145. input_sync(input);
  146. bfin_kpad_clear_irq();
  147. }
  148. return IRQ_HANDLED;
  149. }
  150. static int __devinit bfin_kpad_probe(struct platform_device *pdev)
  151. {
  152. struct bf54x_kpad *bf54x_kpad;
  153. struct bfin_kpad_platform_data *pdata = pdev->dev.platform_data;
  154. struct input_dev *input;
  155. int i, error;
  156. if (!pdata->rows || !pdata->cols || !pdata->keymap) {
  157. printk(KERN_ERR DRV_NAME
  158. ": No rows, cols or keymap from pdata\n");
  159. return -EINVAL;
  160. }
  161. if (!pdata->keymapsize ||
  162. pdata->keymapsize > (pdata->rows * pdata->cols)) {
  163. printk(KERN_ERR DRV_NAME ": Invalid keymapsize\n");
  164. return -EINVAL;
  165. }
  166. bf54x_kpad = kzalloc(sizeof(struct bf54x_kpad), GFP_KERNEL);
  167. if (!bf54x_kpad)
  168. return -ENOMEM;
  169. platform_set_drvdata(pdev, bf54x_kpad);
  170. /* Allocate memory for keymap followed by private LUT */
  171. bf54x_kpad->keycode = kmalloc(pdata->keymapsize *
  172. sizeof(unsigned short) * 2, GFP_KERNEL);
  173. if (!bf54x_kpad->keycode) {
  174. error = -ENOMEM;
  175. goto out;
  176. }
  177. if (!pdata->debounce_time || !pdata->debounce_time > MAX_MULT ||
  178. !pdata->coldrive_time || !pdata->coldrive_time > MAX_MULT) {
  179. printk(KERN_ERR DRV_NAME
  180. ": Invalid Debounce/Columdrive Time from pdata\n");
  181. bfin_write_KPAD_MSEL(0xFF0); /* Default MSEL */
  182. } else {
  183. bfin_write_KPAD_MSEL(
  184. ((pdata->debounce_time / TIME_SCALE)
  185. & DBON_SCALE) |
  186. (((pdata->coldrive_time / TIME_SCALE) << 8)
  187. & COLDRV_SCALE));
  188. }
  189. if (!pdata->keyup_test_interval)
  190. bf54x_kpad->keyup_test_jiffies = msecs_to_jiffies(50);
  191. else
  192. bf54x_kpad->keyup_test_jiffies =
  193. msecs_to_jiffies(pdata->keyup_test_interval);
  194. if (peripheral_request_list((u16 *)&per_rows[MAX_RC - pdata->rows],
  195. DRV_NAME)) {
  196. printk(KERN_ERR DRV_NAME
  197. ": Requesting Peripherals failed\n");
  198. error = -EFAULT;
  199. goto out0;
  200. }
  201. if (peripheral_request_list((u16 *)&per_cols[MAX_RC - pdata->cols],
  202. DRV_NAME)) {
  203. printk(KERN_ERR DRV_NAME
  204. ": Requesting Peripherals failed\n");
  205. error = -EFAULT;
  206. goto out1;
  207. }
  208. bf54x_kpad->irq = platform_get_irq(pdev, 0);
  209. if (bf54x_kpad->irq < 0) {
  210. error = -ENODEV;
  211. goto out2;
  212. }
  213. error = request_irq(bf54x_kpad->irq, bfin_kpad_isr,
  214. IRQF_SAMPLE_RANDOM, DRV_NAME, pdev);
  215. if (error) {
  216. printk(KERN_ERR DRV_NAME
  217. ": unable to claim irq %d; error %d\n",
  218. bf54x_kpad->irq, error);
  219. goto out2;
  220. }
  221. input = input_allocate_device();
  222. if (!input) {
  223. error = -ENOMEM;
  224. goto out3;
  225. }
  226. bf54x_kpad->input = input;
  227. input->name = pdev->name;
  228. input->phys = "bf54x-keys/input0";
  229. input->dev.parent = &pdev->dev;
  230. input_set_drvdata(input, bf54x_kpad);
  231. input->id.bustype = BUS_HOST;
  232. input->id.vendor = 0x0001;
  233. input->id.product = 0x0001;
  234. input->id.version = 0x0100;
  235. input->keycodesize = sizeof(unsigned short);
  236. input->keycodemax = pdata->keymapsize;
  237. input->keycode = bf54x_kpad->keycode;
  238. bfin_keycodecpy(bf54x_kpad->keycode, pdata->keymap, pdata->keymapsize);
  239. /* setup input device */
  240. __set_bit(EV_KEY, input->evbit);
  241. if (pdata->repeat)
  242. __set_bit(EV_REP, input->evbit);
  243. for (i = 0; i < input->keycodemax; i++)
  244. __set_bit(bf54x_kpad->keycode[i] & KEY_MAX, input->keybit);
  245. __clear_bit(KEY_RESERVED, input->keybit);
  246. error = input_register_device(input);
  247. if (error) {
  248. printk(KERN_ERR DRV_NAME
  249. ": Unable to register input device (%d)\n", error);
  250. goto out4;
  251. }
  252. /* Init Keypad Key Up/Release test timer */
  253. setup_timer(&bf54x_kpad->timer, bfin_kpad_timer, (unsigned long) pdev);
  254. bfin_write_KPAD_PRESCALE(bfin_kpad_get_prescale(TIME_SCALE));
  255. bfin_write_KPAD_CTL((((pdata->cols - 1) << 13) & KPAD_COLEN) |
  256. (((pdata->rows - 1) << 10) & KPAD_ROWEN) |
  257. (2 & KPAD_IRQMODE));
  258. bfin_write_KPAD_CTL(bfin_read_KPAD_CTL() | KPAD_EN);
  259. device_init_wakeup(&pdev->dev, 1);
  260. printk(KERN_ERR DRV_NAME
  261. ": Blackfin BF54x Keypad registered IRQ %d\n", bf54x_kpad->irq);
  262. return 0;
  263. out4:
  264. input_free_device(input);
  265. out3:
  266. free_irq(bf54x_kpad->irq, pdev);
  267. out2:
  268. peripheral_free_list((u16 *)&per_cols[MAX_RC - pdata->cols]);
  269. out1:
  270. peripheral_free_list((u16 *)&per_rows[MAX_RC - pdata->rows]);
  271. out0:
  272. kfree(bf54x_kpad->keycode);
  273. out:
  274. kfree(bf54x_kpad);
  275. platform_set_drvdata(pdev, NULL);
  276. return error;
  277. }
  278. static int __devexit bfin_kpad_remove(struct platform_device *pdev)
  279. {
  280. struct bfin_kpad_platform_data *pdata = pdev->dev.platform_data;
  281. struct bf54x_kpad *bf54x_kpad = platform_get_drvdata(pdev);
  282. del_timer_sync(&bf54x_kpad->timer);
  283. free_irq(bf54x_kpad->irq, pdev);
  284. input_unregister_device(bf54x_kpad->input);
  285. peripheral_free_list((u16 *)&per_rows[MAX_RC - pdata->rows]);
  286. peripheral_free_list((u16 *)&per_cols[MAX_RC - pdata->cols]);
  287. kfree(bf54x_kpad->keycode);
  288. kfree(bf54x_kpad);
  289. platform_set_drvdata(pdev, NULL);
  290. return 0;
  291. }
  292. #ifdef CONFIG_PM
  293. static int bfin_kpad_suspend(struct platform_device *pdev, pm_message_t state)
  294. {
  295. struct bf54x_kpad *bf54x_kpad = platform_get_drvdata(pdev);
  296. if (device_may_wakeup(&pdev->dev))
  297. enable_irq_wake(bf54x_kpad->irq);
  298. return 0;
  299. }
  300. static int bfin_kpad_resume(struct platform_device *pdev)
  301. {
  302. struct bf54x_kpad *bf54x_kpad = platform_get_drvdata(pdev);
  303. if (device_may_wakeup(&pdev->dev))
  304. disable_irq_wake(bf54x_kpad->irq);
  305. return 0;
  306. }
  307. #else
  308. # define bfin_kpad_suspend NULL
  309. # define bfin_kpad_resume NULL
  310. #endif
  311. struct platform_driver bfin_kpad_device_driver = {
  312. .driver = {
  313. .name = DRV_NAME,
  314. .owner = THIS_MODULE,
  315. },
  316. .probe = bfin_kpad_probe,
  317. .remove = __devexit_p(bfin_kpad_remove),
  318. .suspend = bfin_kpad_suspend,
  319. .resume = bfin_kpad_resume,
  320. };
  321. static int __init bfin_kpad_init(void)
  322. {
  323. return platform_driver_register(&bfin_kpad_device_driver);
  324. }
  325. static void __exit bfin_kpad_exit(void)
  326. {
  327. platform_driver_unregister(&bfin_kpad_device_driver);
  328. }
  329. module_init(bfin_kpad_init);
  330. module_exit(bfin_kpad_exit);
  331. MODULE_LICENSE("GPL");
  332. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  333. MODULE_DESCRIPTION("Keypad driver for BF54x Processors");
  334. MODULE_ALIAS("platform:bf54x-keys");