bf54x-keys.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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-2008 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/fs.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/irq.h>
  34. #include <linux/slab.h>
  35. #include <linux/sched.h>
  36. #include <linux/pm.h>
  37. #include <linux/sysctl.h>
  38. #include <linux/proc_fs.h>
  39. #include <linux/delay.h>
  40. #include <linux/platform_device.h>
  41. #include <linux/input.h>
  42. #include <asm/portmux.h>
  43. #include <mach/bf54x_keys.h>
  44. #define DRV_NAME "bf54x-keys"
  45. #define TIME_SCALE 100 /* 100 ns */
  46. #define MAX_MULT (0xFF * TIME_SCALE)
  47. #define MAX_RC 8 /* Max Row/Col */
  48. static const u16 per_rows[] = {
  49. P_KEY_ROW7,
  50. P_KEY_ROW6,
  51. P_KEY_ROW5,
  52. P_KEY_ROW4,
  53. P_KEY_ROW3,
  54. P_KEY_ROW2,
  55. P_KEY_ROW1,
  56. P_KEY_ROW0,
  57. 0
  58. };
  59. static const u16 per_cols[] = {
  60. P_KEY_COL7,
  61. P_KEY_COL6,
  62. P_KEY_COL5,
  63. P_KEY_COL4,
  64. P_KEY_COL3,
  65. P_KEY_COL2,
  66. P_KEY_COL1,
  67. P_KEY_COL0,
  68. 0
  69. };
  70. struct bf54x_kpad {
  71. struct input_dev *input;
  72. int irq;
  73. unsigned short lastkey;
  74. unsigned short *keycode;
  75. struct timer_list timer;
  76. unsigned int keyup_test_jiffies;
  77. unsigned short kpad_msel;
  78. unsigned short kpad_prescale;
  79. unsigned short kpad_ctl;
  80. };
  81. static inline int bfin_kpad_find_key(struct bf54x_kpad *bf54x_kpad,
  82. struct input_dev *input, u16 keyident)
  83. {
  84. u16 i;
  85. for (i = 0; i < input->keycodemax; i++)
  86. if (bf54x_kpad->keycode[i + input->keycodemax] == keyident)
  87. return bf54x_kpad->keycode[i];
  88. return -1;
  89. }
  90. static inline void bfin_keycodecpy(unsigned short *keycode,
  91. const unsigned int *pdata_kc,
  92. unsigned short keymapsize)
  93. {
  94. unsigned int i;
  95. for (i = 0; i < keymapsize; i++) {
  96. keycode[i] = pdata_kc[i] & 0xffff;
  97. keycode[i + keymapsize] = pdata_kc[i] >> 16;
  98. }
  99. }
  100. static inline u16 bfin_kpad_get_prescale(u32 timescale)
  101. {
  102. u32 sclk = get_sclk();
  103. return ((((sclk / 1000) * timescale) / 1024) - 1);
  104. }
  105. static inline u16 bfin_kpad_get_keypressed(struct bf54x_kpad *bf54x_kpad)
  106. {
  107. return (bfin_read_KPAD_STAT() & KPAD_PRESSED);
  108. }
  109. static inline void bfin_kpad_clear_irq(void)
  110. {
  111. bfin_write_KPAD_STAT(0xFFFF);
  112. bfin_write_KPAD_ROWCOL(0xFFFF);
  113. }
  114. static void bfin_kpad_timer(unsigned long data)
  115. {
  116. struct platform_device *pdev = (struct platform_device *) data;
  117. struct bf54x_kpad *bf54x_kpad = platform_get_drvdata(pdev);
  118. if (bfin_kpad_get_keypressed(bf54x_kpad)) {
  119. /* Try again later */
  120. mod_timer(&bf54x_kpad->timer,
  121. jiffies + bf54x_kpad->keyup_test_jiffies);
  122. return;
  123. }
  124. input_report_key(bf54x_kpad->input, bf54x_kpad->lastkey, 0);
  125. input_sync(bf54x_kpad->input);
  126. /* Clear IRQ Status */
  127. bfin_kpad_clear_irq();
  128. enable_irq(bf54x_kpad->irq);
  129. }
  130. static irqreturn_t bfin_kpad_isr(int irq, void *dev_id)
  131. {
  132. struct platform_device *pdev = dev_id;
  133. struct bf54x_kpad *bf54x_kpad = platform_get_drvdata(pdev);
  134. struct input_dev *input = bf54x_kpad->input;
  135. int key;
  136. u16 rowcol = bfin_read_KPAD_ROWCOL();
  137. key = bfin_kpad_find_key(bf54x_kpad, input, rowcol);
  138. input_report_key(input, key, 1);
  139. input_sync(input);
  140. if (bfin_kpad_get_keypressed(bf54x_kpad)) {
  141. disable_irq_nosync(bf54x_kpad->irq);
  142. bf54x_kpad->lastkey = key;
  143. mod_timer(&bf54x_kpad->timer,
  144. jiffies + bf54x_kpad->keyup_test_jiffies);
  145. } else {
  146. input_report_key(input, key, 0);
  147. input_sync(input);
  148. bfin_kpad_clear_irq();
  149. }
  150. return IRQ_HANDLED;
  151. }
  152. static int bfin_kpad_probe(struct platform_device *pdev)
  153. {
  154. struct bf54x_kpad *bf54x_kpad;
  155. struct bfin_kpad_platform_data *pdata = dev_get_platdata(&pdev->dev);
  156. struct input_dev *input;
  157. int i, error;
  158. if (!pdata->rows || !pdata->cols || !pdata->keymap) {
  159. dev_err(&pdev->dev, "no rows, cols or keymap from pdata\n");
  160. return -EINVAL;
  161. }
  162. if (!pdata->keymapsize ||
  163. pdata->keymapsize > (pdata->rows * pdata->cols)) {
  164. dev_err(&pdev->dev, "invalid keymapsize\n");
  165. return -EINVAL;
  166. }
  167. bf54x_kpad = kzalloc(sizeof(struct bf54x_kpad), GFP_KERNEL);
  168. if (!bf54x_kpad)
  169. return -ENOMEM;
  170. platform_set_drvdata(pdev, bf54x_kpad);
  171. /* Allocate memory for keymap followed by private LUT */
  172. bf54x_kpad->keycode = kmalloc(pdata->keymapsize *
  173. sizeof(unsigned short) * 2, GFP_KERNEL);
  174. if (!bf54x_kpad->keycode) {
  175. error = -ENOMEM;
  176. goto out;
  177. }
  178. if (!pdata->debounce_time || pdata->debounce_time > MAX_MULT ||
  179. !pdata->coldrive_time || pdata->coldrive_time > MAX_MULT) {
  180. dev_warn(&pdev->dev,
  181. "invalid platform debounce/columndrive time\n");
  182. bfin_write_KPAD_MSEL(0xFF0); /* Default MSEL */
  183. } else {
  184. bfin_write_KPAD_MSEL(
  185. ((pdata->debounce_time / TIME_SCALE)
  186. & DBON_SCALE) |
  187. (((pdata->coldrive_time / TIME_SCALE) << 8)
  188. & COLDRV_SCALE));
  189. }
  190. if (!pdata->keyup_test_interval)
  191. bf54x_kpad->keyup_test_jiffies = msecs_to_jiffies(50);
  192. else
  193. bf54x_kpad->keyup_test_jiffies =
  194. msecs_to_jiffies(pdata->keyup_test_interval);
  195. if (peripheral_request_list((u16 *)&per_rows[MAX_RC - pdata->rows],
  196. DRV_NAME)) {
  197. dev_err(&pdev->dev, "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. dev_err(&pdev->dev, "requesting peripherals failed\n");
  204. error = -EFAULT;
  205. goto out1;
  206. }
  207. bf54x_kpad->irq = platform_get_irq(pdev, 0);
  208. if (bf54x_kpad->irq < 0) {
  209. error = -ENODEV;
  210. goto out2;
  211. }
  212. error = request_irq(bf54x_kpad->irq, bfin_kpad_isr,
  213. 0, DRV_NAME, pdev);
  214. if (error) {
  215. dev_err(&pdev->dev, "unable to claim irq %d\n",
  216. bf54x_kpad->irq);
  217. goto out2;
  218. }
  219. input = input_allocate_device();
  220. if (!input) {
  221. error = -ENOMEM;
  222. goto out3;
  223. }
  224. bf54x_kpad->input = input;
  225. input->name = pdev->name;
  226. input->phys = "bf54x-keys/input0";
  227. input->dev.parent = &pdev->dev;
  228. input_set_drvdata(input, bf54x_kpad);
  229. input->id.bustype = BUS_HOST;
  230. input->id.vendor = 0x0001;
  231. input->id.product = 0x0001;
  232. input->id.version = 0x0100;
  233. input->keycodesize = sizeof(unsigned short);
  234. input->keycodemax = pdata->keymapsize;
  235. input->keycode = bf54x_kpad->keycode;
  236. bfin_keycodecpy(bf54x_kpad->keycode, pdata->keymap, pdata->keymapsize);
  237. /* setup input device */
  238. __set_bit(EV_KEY, input->evbit);
  239. if (pdata->repeat)
  240. __set_bit(EV_REP, input->evbit);
  241. for (i = 0; i < input->keycodemax; i++)
  242. if (bf54x_kpad->keycode[i] <= KEY_MAX)
  243. __set_bit(bf54x_kpad->keycode[i], input->keybit);
  244. __clear_bit(KEY_RESERVED, input->keybit);
  245. error = input_register_device(input);
  246. if (error) {
  247. dev_err(&pdev->dev, "unable to register input device\n");
  248. goto out4;
  249. }
  250. /* Init Keypad Key Up/Release test timer */
  251. setup_timer(&bf54x_kpad->timer, bfin_kpad_timer, (unsigned long) pdev);
  252. bfin_write_KPAD_PRESCALE(bfin_kpad_get_prescale(TIME_SCALE));
  253. bfin_write_KPAD_CTL((((pdata->cols - 1) << 13) & KPAD_COLEN) |
  254. (((pdata->rows - 1) << 10) & KPAD_ROWEN) |
  255. (2 & KPAD_IRQMODE));
  256. bfin_write_KPAD_CTL(bfin_read_KPAD_CTL() | KPAD_EN);
  257. device_init_wakeup(&pdev->dev, 1);
  258. return 0;
  259. out4:
  260. input_free_device(input);
  261. out3:
  262. free_irq(bf54x_kpad->irq, pdev);
  263. out2:
  264. peripheral_free_list((u16 *)&per_cols[MAX_RC - pdata->cols]);
  265. out1:
  266. peripheral_free_list((u16 *)&per_rows[MAX_RC - pdata->rows]);
  267. out0:
  268. kfree(bf54x_kpad->keycode);
  269. out:
  270. kfree(bf54x_kpad);
  271. return error;
  272. }
  273. static int bfin_kpad_remove(struct platform_device *pdev)
  274. {
  275. struct bfin_kpad_platform_data *pdata = dev_get_platdata(&pdev->dev);
  276. struct bf54x_kpad *bf54x_kpad = platform_get_drvdata(pdev);
  277. del_timer_sync(&bf54x_kpad->timer);
  278. free_irq(bf54x_kpad->irq, pdev);
  279. input_unregister_device(bf54x_kpad->input);
  280. peripheral_free_list((u16 *)&per_rows[MAX_RC - pdata->rows]);
  281. peripheral_free_list((u16 *)&per_cols[MAX_RC - pdata->cols]);
  282. kfree(bf54x_kpad->keycode);
  283. kfree(bf54x_kpad);
  284. return 0;
  285. }
  286. #ifdef CONFIG_PM
  287. static int bfin_kpad_suspend(struct platform_device *pdev, pm_message_t state)
  288. {
  289. struct bf54x_kpad *bf54x_kpad = platform_get_drvdata(pdev);
  290. bf54x_kpad->kpad_msel = bfin_read_KPAD_MSEL();
  291. bf54x_kpad->kpad_prescale = bfin_read_KPAD_PRESCALE();
  292. bf54x_kpad->kpad_ctl = bfin_read_KPAD_CTL();
  293. if (device_may_wakeup(&pdev->dev))
  294. enable_irq_wake(bf54x_kpad->irq);
  295. return 0;
  296. }
  297. static int bfin_kpad_resume(struct platform_device *pdev)
  298. {
  299. struct bf54x_kpad *bf54x_kpad = platform_get_drvdata(pdev);
  300. bfin_write_KPAD_MSEL(bf54x_kpad->kpad_msel);
  301. bfin_write_KPAD_PRESCALE(bf54x_kpad->kpad_prescale);
  302. bfin_write_KPAD_CTL(bf54x_kpad->kpad_ctl);
  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. static struct platform_driver bfin_kpad_device_driver = {
  312. .driver = {
  313. .name = DRV_NAME,
  314. },
  315. .probe = bfin_kpad_probe,
  316. .remove = bfin_kpad_remove,
  317. .suspend = bfin_kpad_suspend,
  318. .resume = bfin_kpad_resume,
  319. };
  320. module_platform_driver(bfin_kpad_device_driver);
  321. MODULE_LICENSE("GPL");
  322. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  323. MODULE_DESCRIPTION("Keypad driver for BF54x Processors");
  324. MODULE_ALIAS("platform:bf54x-keys");