歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> OK6410矩陣鍵盤驅動 linux2.6.36

OK6410矩陣鍵盤驅動 linux2.6.36

日期:2017/3/1 10:10:47   编辑:Linux編程

實現了4*4的矩陣鍵盤驅動,

參考文檔 http://www.linuxidc.com/Linux/2012-09/69904.htm

需要注明一點的是:

void samsung_keypad_cfg_gpio(unsigned int rows, unsigned int cols)函數的位置:

arch/arm/mach-s3c64xx/setup-keypad.c 修改方法如下:

  1. voidsamsung_keypad_cfg_gpio(unsigned int rows, unsigned int cols)
  2. {
  3. unsignedint gpio;
  4. unsignedint end;
  5. /*Set all the necessary GPK pins to special-function 3: KP_ROW[x] */
  6. end= S3C64XX_GPK(8 + rows);
  7. for(gpio = S3C64XX_GPK(8); gpio < end; gpio++) {
  8. s3c_gpio_cfgpin(gpio,S3C_GPIO_SFN(3));
  9. s3c_gpio_setpull(gpio,S3C_GPIO_PULL_NONE);
  10. }
  11. /*Set all the necessary GPL pins to special-function 3: KP_COL[x] */
  12. end= S3C64XX_GPL(0 + cols);
  13. for(gpio = S3C64XX_GPL(0); gpio < end; gpio++) {
  14. s3c_gpio_cfgpin(gpio,S3C_GPIO_SFN(3));
  15. s3c_gpio_setpull(gpio,S3C_GPIO_PULL_NONE);
  16. }
  17. }
  18. 其中S3C_GPIO_PULL_NONE需要改成S3C_GPIO_PULL_UP

此外,還需要修改一點,代碼原先為2*8的矩陣鍵盤,需修改為4*4的矩陣鍵盤

在arch/arm/mach-s3c64xx/mach-smdk6410.c中,在380行中修改為如下:

  1. static struct samsung_keypad_platdata smdk6410_keypad_data _initdata={
  2. .keymap_data = & smdk6410_keymap_data,
  3. .rows = 4,
  4. .cols = 4,
  5. };

整個代碼如下:

samsung-keypad,c :

  1. /*
  2. * Samsung keypad driver
  3. *
  4. * Copyright (C) 2010 Samsung Electronics Co.Ltd
  5. * Author: Joonyoung Shim <jy0922.shim@samsung.com>
  6. * Author: Donghwa Lee <dh09.lee@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/init.h>
  17. #include <linux/input.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #include <linux/sched.h>
  24. #include <plat/keypad.h>
  25. #define SAMSUNG_KEYIFCON 0x00
  26. #define SAMSUNG_KEYIFSTSCLR 0x04
  27. #define SAMSUNG_KEYIFCOL 0x08
  28. #define SAMSUNG_KEYIFROW 0x0c
  29. #define SAMSUNG_KEYIFFC 0x10
  30. /* SAMSUNG_KEYIFCON */
  31. #define SAMSUNG_KEYIFCON_INT_F_EN (1 << 0)
  32. #define SAMSUNG_KEYIFCON_INT_R_EN (1 << 1)
  33. #define SAMSUNG_KEYIFCON_DF_EN (1 << 2)
  34. #define SAMSUNG_KEYIFCON_FC_EN (1 << 3)
  35. #define SAMSUNG_KEYIFCON_WAKEUPEN (1 << 4)
  36. /* SAMSUNG_KEYIFSTSCLR */
  37. #define SAMSUNG_KEYIFSTSCLR_P_INT_MASK (0xff << 0)
  38. #define SAMSUNG_KEYIFSTSCLR_R_INT_MASK (0xff << 8)
  39. #define SAMSUNG_KEYIFSTSCLR_R_INT_OFFSET 8
  40. #define S5PV210_KEYIFSTSCLR_P_INT_MASK (0x3fff << 0)
  41. #define S5PV210_KEYIFSTSCLR_R_INT_MASK (0x3fff << 16)
  42. #define S5PV210_KEYIFSTSCLR_R_INT_OFFSET 16
  43. /* SAMSUNG_KEYIFCOL */
  44. #define SAMSUNG_KEYIFCOL_MASK (0xff << 0)
  45. #define S5PV210_KEYIFCOLEN_MASK (0xff << 8)
  46. unsigned int keypad_keycode[] = {'1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G'};
  47. /* SAMSUNG_KEYIFROW */
  48. #define SAMSUNG_KEYIFROW_MASK (0xff << 0)
  49. #define S5PV210_KEYIFROW_MASK (0x3fff << 0)
  50. /* SAMSUNG_KEYIFFC */
  51. #define SAMSUNG_KEYIFFC_MASK (0x3ff << 0)
  52. enum samsung_keypad_type {
  53. KEYPAD_TYPE_SAMSUNG,
  54. KEYPAD_TYPE_S5PV210,
  55. };
  56. struct samsung_keypad {
  57. struct input_dev *input_dev;
  58. struct clk *clk;
  59. void __iomem *base;
  60. wait_queue_head_t wait;
  61. bool stopped;
  62. int irq;
  63. unsigned int row_shift;
  64. unsigned int rows;
  65. unsigned int cols;
  66. unsigned int row_state[SAMSUNG_MAX_COLS];
  67. unsigned short keycodes[];
  68. };
  69. static int samsung_keypad_is_s5pv210(struct device *dev)
  70. {
  71. struct platform_device *pdev = to_platform_device(dev);
  72. enum samsung_keypad_type type =
  73. platform_get_device_id(pdev)->driver_data;
  74. return type == KEYPAD_TYPE_S5PV210;
  75. }
  76. static void samsung_keypad_scan(struct samsung_keypad *keypad,
  77. unsigned int *row_state)
  78. {
  79. // struct device *dev = keypad->input_dev->dev.parent;
  80. unsigned int col;
  81. unsigned int val;
  82. // printk("<0>samsung_keypad_scan!\n");
  83. for (col = 0; col < keypad->cols; col++) {
  84. /*if (samsung_keypad_is_s5pv210(dev)) {
  85. val = S5PV210_KEYIFCOLEN_MASK;
  86. val &= ~(1 << col) << 8;
  87. } else {*/
  88. val = SAMSUNG_KEYIFCOL_MASK;
  89. val &= ~(1 << col);
  90. //}
  91. writel(val, keypad->base + SAMSUNG_KEYIFCOL);
  92. mdelay(1);
  93. val = readl(keypad->base + SAMSUNG_KEYIFROW);
  94. row_state[col] = ~val & ((1 << keypad->rows) - 1);
  95. }
  96. /* KEYIFCOL reg clear */
  97. writel(0, keypad->base + SAMSUNG_KEYIFCOL);
  98. }
  99. static bool samsung_keypad_report(struct samsung_keypad *keypad,
  100. unsigned int *row_state)
  101. {
  102. struct input_dev *input_dev = keypad->input_dev;
  103. unsigned int changed;
  104. unsigned int pressed;
  105. unsigned int key_down = 0;
  106. unsigned int val;
  107. unsigned int col, row;
  108. for (col = 0; col < keypad->cols; col++) {
  109. changed = row_state[col] ^ keypad->row_state[col];
  110. key_down |= row_state[col];
  111. if (!changed)
  112. continue;
  113. for (row = 0; row < keypad->rows; row++) {
  114. if (!(changed & (1 << row)))
  115. continue;
  116. pressed = row_state[col] & (1 << row);
  117. printk("\n<0>samsung_keypad key %s,row:%d,col:%d\n"
  118. ,(pressed ? "pressed":"released"),row,col );
  119. dev_dbg(&keypad->input_dev->dev,
  120. "key %s, row: %d, col: %d\n",
  121. pressed ? "pressed" : "released", row, col);
  122. val = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
  123. input_event(input_dev, EV_MSC, MSC_SCAN, val);
  124. input_report_key(input_dev,
  125. keypad->keycodes[val], pressed);
  126. }
  127. input_sync(keypad->input_dev);
  128. }
  129. memcpy(keypad->row_state, row_state, sizeof(keypad->row_state));
  130. return key_down;
  131. }
  132. static irqreturn_t samsung_keypad_irq(int irq, void *dev_id)
  133. {
  134. struct samsung_keypad *keypad = dev_id;
  135. unsigned int row_state[SAMSUNG_MAX_COLS];
  136. unsigned int val;
  137. bool key_down;
  138. do {
  139. val = readl(keypad->base + SAMSUNG_KEYIFSTSCLR);
  140. /* Clear interrupt. */
  141. writel(~0x0, keypad->base + SAMSUNG_KEYIFSTSCLR);
  142. samsung_keypad_scan(keypad, row_state);
  143. key_down = samsung_keypad_report(keypad, row_state);
  144. if (key_down)
  145. wait_event_timeout(keypad->wait, keypad->stopped,
  146. msecs_to_jiffies(50));
  147. } while (key_down && !keypad->stopped);
  148. return IRQ_HANDLED;
  149. }
  150. static void samsung_keypad_start(struct samsung_keypad *keypad)
  151. {
  152. unsigned int val;
  153. /* Tell IRQ thread that it may poll the device. */
  154. keypad->stopped = false;
  155. clk_enable(keypad->clk);
  156. /* Enable interrupt bits. */
  157. val = readl(keypad->base + SAMSUNG_KEYIFCON);
  158. val |= SAMSUNG_KEYIFCON_INT_F_EN | SAMSUNG_KEYIFCON_INT_R_EN;
  159. writel(val, keypad->base + SAMSUNG_KEYIFCON);
  160. /* KEYIFCOL reg clear. */
  161. writel(0, keypad->base + SAMSUNG_KEYIFCOL);
  162. }
  163. static void samsung_keypad_stop(struct samsung_keypad *keypad)
  164. {
  165. unsigned int val;
  166. /* Signal IRQ thread to stop polling and disable the handler. */
  167. keypad->stopped = true;
  168. wake_up(&keypad->wait);
  169. disable_irq(keypad->irq);
  170. /* Clear interrupt. */
  171. writel(~0x0, keypad->base + SAMSUNG_KEYIFSTSCLR);
  172. /* Disable interrupt bits. */
  173. val = readl(keypad->base + SAMSUNG_KEYIFCON);
  174. val &= ~(SAMSUNG_KEYIFCON_INT_F_EN | SAMSUNG_KEYIFCON_INT_R_EN);
  175. writel(val, keypad->base + SAMSUNG_KEYIFCON);
  176. clk_disable(keypad->clk);
  177. /*
  178. * Now that chip should not generate interrupts we can safely
  179. * re-enable the handler.
  180. */
  181. enable_irq(keypad->irq);
  182. }
  183. static int samsung_keypad_open(struct input_dev *input_dev)
  184. {
  185. printk("<0>samsung_keypad_open!\n");
  186. struct samsung_keypad *keypad = input_get_drvdata(input_dev);
  187. samsung_keypad_start(keypad);
  188. return 0;
  189. }
  190. static void samsung_keypad_close(struct input_dev *input_dev)
  191. {
  192. struct samsung_keypad *keypad = input_get_drvdata(input_dev);
  193. samsung_keypad_stop(keypad);
  194. }
  195. static int __devinit samsung_keypad_probe(struct platform_device *pdev)
  196. {
  197. const struct samsung_keypad_platdata *pdata;
  198. const struct matrix_keymap_data *keymap_data;
  199. struct samsung_keypad *keypad;
  200. struct resource *res;
  201. struct input_dev *input_dev;
  202. unsigned int row_shift;
  203. unsigned int keymap_size;
  204. int error;
  205. unsigned int key=0;
  206. unsigned int keycodes_size=sizeof(keypad_keycode)/sizeof(keypad_keycode[0]);
  207. printk("<0>samsung_keypad_probe!\n");
  208. pdata = pdev->dev.platform_data;
  209. if (!pdata) {
  210. dev_err(&pdev->dev, "no platform data defined\n");
  211. return -EINVAL;
  212. }
  213. keymap_data = pdata->keymap_data;
  214. if (!keymap_data) {
  215. dev_err(&pdev->dev, "no keymap data defined\n");
  216. return -EINVAL;
  217. }
  218. if (!pdata->rows || pdata->rows > SAMSUNG_MAX_ROWS)
  219. return -EINVAL;
  220. if (!pdata->cols || pdata->cols > SAMSUNG_MAX_COLS)
  221. return -EINVAL;
  222. /* initialize the gpio */
  223. if (pdata->cfg_gpio)
  224. pdata->cfg_gpio(pdata->rows, pdata->cols);
  225. row_shift = get_count_order(pdata->cols);
  226. keymap_size = (pdata->rows << row_shift) * sizeof(keypad->keycodes[0]);
  227. keypad = kzalloc(sizeof(*keypad) + keymap_size, GFP_KERNEL);
  228. input_dev = input_allocate_device();
  229. if (!keypad || !input_dev) {
  230. error = -ENOMEM;
  231. goto err_free_mem;
  232. }
  233. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  234. if (!res) {
  235. error = -ENODEV;
  236. goto err_free_mem;
  237. }
  238. keypad->base = ioremap(res->start, resource_size(res));
  239. if (!keypad->base) {
  240. error = -EBUSY;
  241. goto err_free_mem;
  242. }
  243. keypad->clk = clk_get(&pdev->dev, "keypad");
  244. if (IS_ERR(keypad->clk)) {
  245. dev_err(&pdev->dev, "failed to get keypad clk\n");
  246. error = PTR_ERR(keypad->clk);
  247. goto err_unmap_base;
  248. }
  249. keypad->input_dev = input_dev;
  250. keypad->row_shift = row_shift;
  251. keypad->rows = pdata->rows;
  252. keypad->cols = pdata->cols;
  253. init_waitqueue_head(&keypad->wait);
  254. input_dev->name = pdev->name;
  255. input_dev->id.bustype = BUS_HOST;
  256. input_dev->dev.parent = &pdev->dev;
  257. input_set_drvdata(input_dev, keypad);
  258. input_dev->open = samsung_keypad_open;
  259. input_dev->close = samsung_keypad_close;
  260. input_dev->evbit[0] = BIT_MASK(EV_KEY);
  261. if (!pdata->no_autorepeat)
  262. input_dev->evbit[0] |= BIT_MASK(EV_REP);
  263. input_set_capability(input_dev, EV_MSC, MSC_SCAN);
  264. input_dev->keycode = keypad->keycodes;
  265. input_dev->keycodesize = sizeof(keypad->keycodes[0]);
  266. input_dev->keycodemax = pdata->rows << row_shift;
  267. /*matrix_keypad_build_keymap(keymap_data, row_shift,
  268. input_dev->keycode, input_dev->keybit);*/
  269. for(key=0;key<keycodes_size;key++){
  270. int code=keypad->keycodes[key]=keypad_keycode[key];
  271. if(code<=0)
  272. continue;
  273. __set_bit(code,input_dev->keybit);
  274. }
  275. __clear_bit(0,input_dev->keybit);
  276. keypad->irq = platform_get_irq(pdev, 0);
  277. if (keypad->irq < 0) {
  278. error = keypad->irq;
  279. goto err_put_clk;
  280. }
  281. error = request_threaded_irq(keypad->irq, NULL, samsung_keypad_irq,
  282. IRQF_ONESHOT, dev_name(&pdev->dev), keypad);
  283. if (error) {
  284. dev_err(&pdev->dev, "failed to register keypad interrupt\n");
  285. goto err_put_clk;
  286. }
  287. error = input_register_device(keypad->input_dev);
  288. if (error)
  289. goto err_free_irq;
  290. device_init_wakeup(&pdev->dev, pdata->wakeup);
  291. platform_set_drvdata(pdev, keypad);
  292. return 0;
  293. err_free_irq:
  294. free_irq(keypad->irq, keypad);
  295. err_put_clk:
  296. clk_put(keypad->clk);
  297. err_unmap_base:
  298. iounmap(keypad->base);
  299. err_free_mem:
  300. input_free_device(input_dev);
  301. kfree(keypad);
  302. return error;
  303. }
  304. static int __devexit samsung_keypad_remove(struct platform_device *pdev)
  305. {
  306. struct samsung_keypad *keypad = platform_get_drvdata(pdev);
  307. device_init_wakeup(&pdev->dev, 0);
  308. platform_set_drvdata(pdev, NULL);
  309. input_unregister_device(keypad->input_dev);
  310. /*
  311. * It is safe to free IRQ after unregistering device because
  312. * samsung_keypad_close will shut off interrupts.
  313. */
  314. free_irq(keypad->irq, keypad);
  315. clk_put(keypad->clk);
  316. iounmap(keypad->base);
  317. kfree(keypad);
  318. return 0;
  319. }
  320. #ifdef CONFIG_PM
  321. static void samsung_keypad_toggle_wakeup(struct samsung_keypad *keypad,
  322. bool enable)
  323. {
  324. struct device *dev = keypad->input_dev->dev.parent;
  325. unsigned int val;
  326. clk_enable(keypad->clk);
  327. val = readl(keypad->base + SAMSUNG_KEYIFCON);
  328. if (enable) {
  329. val |= SAMSUNG_KEYIFCON_WAKEUPEN;
  330. if (device_may_wakeup(dev))
  331. enable_irq_wake(keypad->irq);
  332. } else {
  333. val &= ~SAMSUNG_KEYIFCON_WAKEUPEN;
  334. if (device_may_wakeup(dev))
  335. disable_irq_wake(keypad->irq);
  336. }
  337. writel(val, keypad->base + SAMSUNG_KEYIFCON);
  338. clk_disable(keypad->clk);
  339. }
  340. static int samsung_keypad_suspend(struct device *dev)
  341. {
  342. struct platform_device *pdev = to_platform_device(dev);
  343. struct samsung_keypad *keypad = platform_get_drvdata(pdev);
  344. struct input_dev *input_dev = keypad->input_dev;
  345. mutex_lock(&input_dev->mutex);
  346. if (input_dev->users)
  347. samsung_keypad_stop(keypad);
  348. samsung_keypad_toggle_wakeup(keypad, true);
  349. mutex_unlock(&input_dev->mutex);
  350. return 0;
  351. }
  352. static int samsung_keypad_resume(struct device *dev)
  353. {
  354. struct platform_device *pdev = to_platform_device(dev);
  355. struct samsung_keypad *keypad = platform_get_drvdata(pdev);
  356. struct input_dev *input_dev = keypad->input_dev;
  357. mutex_lock(&input_dev->mutex);
  358. samsung_keypad_toggle_wakeup(keypad, false);
  359. if (input_dev->users)
  360. samsung_keypad_start(keypad);
  361. mutex_unlock(&input_dev->mutex);
  362. return 0;
  363. }
  364. static const struct dev_pm_ops samsung_keypad_pm_ops = {
  365. .suspend = samsung_keypad_suspend,
  366. .resume = samsung_keypad_resume,
  367. };
  368. #endif
  369. static struct platform_device_id samsung_keypad_driver_ids[] = {
  370. {
  371. .name = "samsung-keypad",
  372. .driver_data = KEYPAD_TYPE_SAMSUNG,
  373. }, {
  374. .name = "s5pv210-keypad",
  375. .driver_data = KEYPAD_TYPE_S5PV210,
  376. },
  377. { },
  378. };
  379. MODULE_DEVICE_TABLE(platform, samsung_keypad_driver_ids);
  380. static struct platform_driver samsung_keypad_driver = {
  381. .probe = samsung_keypad_probe,
  382. .remove = __devexit_p(samsung_keypad_remove),
  383. .driver = {
  384. .name = "samsung-keypad",
  385. .owner = THIS_MODULE,
  386. #ifdef CONFIG_PM
  387. .pm = &samsung_keypad_pm_ops,
  388. #endif
  389. },
  390. .id_table = samsung_keypad_driver_ids,
  391. };
  392. static int __init samsung_keypad_init(void)
  393. {
  394. printk("<0>adc_drv_open and adc_init()\n");
  395. return platform_driver_register(&samsung_keypad_driver);
  396. }
  397. module_init(samsung_keypad_init);
  398. static void __exit samsung_keypad_exit(void)
  399. {
  400. platform_driver_unregister(&samsung_keypad_driver);
  401. }
  402. module_exit(samsung_keypad_exit);
  403. MODULE_DESCRIPTION("Samsung keypad driver");
  404. MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
  405. MODULE_AUTHOR("Donghwa Lee <dh09.lee@samsung.com>");
  406. MODULE_LICENSE("GPL");
  407. MODULE_ALIAS("platform:samsung-keypad");
Copyright © Linux教程網 All Rights Reserved