st-keyscan.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * STMicroelectronics Key Scanning driver
  3. *
  4. * Copyright (c) 2014 STMicroelectonics Ltd.
  5. * Author: Stuart Menefy <stuart.menefy@st.com>
  6. *
  7. * Based on sh_keysc.c, copyright 2008 Magnus Damm
  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. #include <linux/module.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/clk.h>
  17. #include <linux/io.h>
  18. #include <linux/input/matrix_keypad.h>
  19. #define ST_KEYSCAN_MAXKEYS 16
  20. #define KEYSCAN_CONFIG_OFF 0x0
  21. #define KEYSCAN_CONFIG_ENABLE 0x1
  22. #define KEYSCAN_DEBOUNCE_TIME_OFF 0x4
  23. #define KEYSCAN_MATRIX_STATE_OFF 0x8
  24. #define KEYSCAN_MATRIX_DIM_OFF 0xc
  25. #define KEYSCAN_MATRIX_DIM_X_SHIFT 0x0
  26. #define KEYSCAN_MATRIX_DIM_Y_SHIFT 0x2
  27. struct st_keyscan {
  28. void __iomem *base;
  29. int irq;
  30. struct clk *clk;
  31. struct input_dev *input_dev;
  32. unsigned long last_state;
  33. unsigned int n_rows;
  34. unsigned int n_cols;
  35. unsigned int debounce_us;
  36. };
  37. static irqreturn_t keyscan_isr(int irq, void *dev_id)
  38. {
  39. struct st_keyscan *keypad = dev_id;
  40. unsigned short *keycode = keypad->input_dev->keycode;
  41. unsigned long state, change;
  42. int bit_nr;
  43. state = readl(keypad->base + KEYSCAN_MATRIX_STATE_OFF) & 0xffff;
  44. change = keypad->last_state ^ state;
  45. keypad->last_state = state;
  46. for_each_set_bit(bit_nr, &change, BITS_PER_LONG)
  47. input_report_key(keypad->input_dev,
  48. keycode[bit_nr], state & BIT(bit_nr));
  49. input_sync(keypad->input_dev);
  50. return IRQ_HANDLED;
  51. }
  52. static int keyscan_start(struct st_keyscan *keypad)
  53. {
  54. int error;
  55. error = clk_enable(keypad->clk);
  56. if (error)
  57. return error;
  58. writel(keypad->debounce_us * (clk_get_rate(keypad->clk) / 1000000),
  59. keypad->base + KEYSCAN_DEBOUNCE_TIME_OFF);
  60. writel(((keypad->n_cols - 1) << KEYSCAN_MATRIX_DIM_X_SHIFT) |
  61. ((keypad->n_rows - 1) << KEYSCAN_MATRIX_DIM_Y_SHIFT),
  62. keypad->base + KEYSCAN_MATRIX_DIM_OFF);
  63. writel(KEYSCAN_CONFIG_ENABLE, keypad->base + KEYSCAN_CONFIG_OFF);
  64. return 0;
  65. }
  66. static void keyscan_stop(struct st_keyscan *keypad)
  67. {
  68. writel(0, keypad->base + KEYSCAN_CONFIG_OFF);
  69. clk_disable(keypad->clk);
  70. }
  71. static int keyscan_open(struct input_dev *dev)
  72. {
  73. struct st_keyscan *keypad = input_get_drvdata(dev);
  74. return keyscan_start(keypad);
  75. }
  76. static void keyscan_close(struct input_dev *dev)
  77. {
  78. struct st_keyscan *keypad = input_get_drvdata(dev);
  79. keyscan_stop(keypad);
  80. }
  81. static int keypad_matrix_key_parse_dt(struct st_keyscan *keypad_data)
  82. {
  83. struct device *dev = keypad_data->input_dev->dev.parent;
  84. struct device_node *np = dev->of_node;
  85. int error;
  86. error = matrix_keypad_parse_of_params(dev, &keypad_data->n_rows,
  87. &keypad_data->n_cols);
  88. if (error) {
  89. dev_err(dev, "failed to parse keypad params\n");
  90. return error;
  91. }
  92. of_property_read_u32(np, "st,debounce-us", &keypad_data->debounce_us);
  93. dev_dbg(dev, "n_rows=%d n_col=%d debounce=%d\n",
  94. keypad_data->n_rows, keypad_data->n_cols,
  95. keypad_data->debounce_us);
  96. return 0;
  97. }
  98. static int keyscan_probe(struct platform_device *pdev)
  99. {
  100. struct st_keyscan *keypad_data;
  101. struct input_dev *input_dev;
  102. struct resource *res;
  103. int error;
  104. if (!pdev->dev.of_node) {
  105. dev_err(&pdev->dev, "no DT data present\n");
  106. return -EINVAL;
  107. }
  108. keypad_data = devm_kzalloc(&pdev->dev, sizeof(*keypad_data),
  109. GFP_KERNEL);
  110. if (!keypad_data)
  111. return -ENOMEM;
  112. input_dev = devm_input_allocate_device(&pdev->dev);
  113. if (!input_dev) {
  114. dev_err(&pdev->dev, "failed to allocate the input device\n");
  115. return -ENOMEM;
  116. }
  117. input_dev->name = pdev->name;
  118. input_dev->phys = "keyscan-keys/input0";
  119. input_dev->dev.parent = &pdev->dev;
  120. input_dev->open = keyscan_open;
  121. input_dev->close = keyscan_close;
  122. input_dev->id.bustype = BUS_HOST;
  123. keypad_data->input_dev = input_dev;
  124. error = keypad_matrix_key_parse_dt(keypad_data);
  125. if (error)
  126. return error;
  127. error = matrix_keypad_build_keymap(NULL, NULL,
  128. keypad_data->n_rows,
  129. keypad_data->n_cols,
  130. NULL, input_dev);
  131. if (error) {
  132. dev_err(&pdev->dev, "failed to build keymap\n");
  133. return error;
  134. }
  135. input_set_drvdata(input_dev, keypad_data);
  136. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  137. keypad_data->base = devm_ioremap_resource(&pdev->dev, res);
  138. if (IS_ERR(keypad_data->base))
  139. return PTR_ERR(keypad_data->base);
  140. keypad_data->clk = devm_clk_get(&pdev->dev, NULL);
  141. if (IS_ERR(keypad_data->clk)) {
  142. dev_err(&pdev->dev, "cannot get clock\n");
  143. return PTR_ERR(keypad_data->clk);
  144. }
  145. error = clk_enable(keypad_data->clk);
  146. if (error) {
  147. dev_err(&pdev->dev, "failed to enable clock\n");
  148. return error;
  149. }
  150. keyscan_stop(keypad_data);
  151. keypad_data->irq = platform_get_irq(pdev, 0);
  152. if (keypad_data->irq < 0) {
  153. dev_err(&pdev->dev, "no IRQ specified\n");
  154. return -EINVAL;
  155. }
  156. error = devm_request_irq(&pdev->dev, keypad_data->irq, keyscan_isr, 0,
  157. pdev->name, keypad_data);
  158. if (error) {
  159. dev_err(&pdev->dev, "failed to request IRQ\n");
  160. return error;
  161. }
  162. error = input_register_device(input_dev);
  163. if (error) {
  164. dev_err(&pdev->dev, "failed to register input device\n");
  165. return error;
  166. }
  167. platform_set_drvdata(pdev, keypad_data);
  168. device_set_wakeup_capable(&pdev->dev, 1);
  169. return 0;
  170. }
  171. #ifdef CONFIG_PM_SLEEP
  172. static int keyscan_suspend(struct device *dev)
  173. {
  174. struct platform_device *pdev = to_platform_device(dev);
  175. struct st_keyscan *keypad = platform_get_drvdata(pdev);
  176. struct input_dev *input = keypad->input_dev;
  177. mutex_lock(&input->mutex);
  178. if (device_may_wakeup(dev))
  179. enable_irq_wake(keypad->irq);
  180. else if (input->users)
  181. keyscan_stop(keypad);
  182. mutex_unlock(&input->mutex);
  183. return 0;
  184. }
  185. static int keyscan_resume(struct device *dev)
  186. {
  187. struct platform_device *pdev = to_platform_device(dev);
  188. struct st_keyscan *keypad = platform_get_drvdata(pdev);
  189. struct input_dev *input = keypad->input_dev;
  190. int retval = 0;
  191. mutex_lock(&input->mutex);
  192. if (device_may_wakeup(dev))
  193. disable_irq_wake(keypad->irq);
  194. else if (input->users)
  195. retval = keyscan_start(keypad);
  196. mutex_unlock(&input->mutex);
  197. return retval;
  198. }
  199. #endif
  200. static SIMPLE_DEV_PM_OPS(keyscan_dev_pm_ops, keyscan_suspend, keyscan_resume);
  201. static const struct of_device_id keyscan_of_match[] = {
  202. { .compatible = "st,sti-keyscan" },
  203. { },
  204. };
  205. MODULE_DEVICE_TABLE(of, keyscan_of_match);
  206. static struct platform_driver keyscan_device_driver = {
  207. .probe = keyscan_probe,
  208. .driver = {
  209. .name = "st-keyscan",
  210. .pm = &keyscan_dev_pm_ops,
  211. .of_match_table = of_match_ptr(keyscan_of_match),
  212. }
  213. };
  214. module_platform_driver(keyscan_device_driver);
  215. MODULE_AUTHOR("Stuart Menefy <stuart.menefy@st.com>");
  216. MODULE_DESCRIPTION("STMicroelectronics keyscan device driver");
  217. MODULE_LICENSE("GPL");