mt6779-keypad.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2022 MediaTek Inc.
  4. * Author Fengping Yu <fengping.yu@mediatek.com>
  5. */
  6. #include <linux/bitops.h>
  7. #include <linux/clk.h>
  8. #include <linux/input.h>
  9. #include <linux/input/matrix_keypad.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/module.h>
  12. #include <linux/property.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regmap.h>
  15. #define MTK_KPD_NAME "mt6779-keypad"
  16. #define MTK_KPD_MEM 0x0004
  17. #define MTK_KPD_DEBOUNCE 0x0018
  18. #define MTK_KPD_DEBOUNCE_MASK GENMASK(13, 0)
  19. #define MTK_KPD_DEBOUNCE_MAX_MS 256
  20. #define MTK_KPD_SEL 0x0020
  21. #define MTK_KPD_SEL_DOUBLE_KP_MODE BIT(0)
  22. #define MTK_KPD_SEL_COL GENMASK(15, 10)
  23. #define MTK_KPD_SEL_ROW GENMASK(9, 4)
  24. #define MTK_KPD_SEL_COLMASK(c) GENMASK((c) + 9, 10)
  25. #define MTK_KPD_SEL_ROWMASK(r) GENMASK((r) + 3, 4)
  26. #define MTK_KPD_NUM_MEMS 5
  27. #define MTK_KPD_NUM_BITS 136 /* 4*32+8 MEM5 only use 8 BITS */
  28. struct mt6779_keypad {
  29. struct regmap *regmap;
  30. struct input_dev *input_dev;
  31. struct clk *clk;
  32. u32 n_rows;
  33. u32 n_cols;
  34. void (*calc_row_col)(unsigned int key,
  35. unsigned int *row, unsigned int *col);
  36. DECLARE_BITMAP(keymap_state, MTK_KPD_NUM_BITS);
  37. };
  38. static const struct regmap_config mt6779_keypad_regmap_cfg = {
  39. .reg_bits = 32,
  40. .val_bits = 32,
  41. .reg_stride = sizeof(u32),
  42. .max_register = 36,
  43. };
  44. static irqreturn_t mt6779_keypad_irq_handler(int irq, void *dev_id)
  45. {
  46. struct mt6779_keypad *keypad = dev_id;
  47. const unsigned short *keycode = keypad->input_dev->keycode;
  48. DECLARE_BITMAP(new_state, MTK_KPD_NUM_BITS);
  49. DECLARE_BITMAP(change, MTK_KPD_NUM_BITS);
  50. unsigned int bit_nr, key;
  51. unsigned int row, col;
  52. unsigned int scancode;
  53. unsigned int row_shift = get_count_order(keypad->n_cols);
  54. bool pressed;
  55. regmap_bulk_read(keypad->regmap, MTK_KPD_MEM,
  56. new_state, MTK_KPD_NUM_MEMS);
  57. bitmap_xor(change, new_state, keypad->keymap_state, MTK_KPD_NUM_BITS);
  58. for_each_set_bit(bit_nr, change, MTK_KPD_NUM_BITS) {
  59. /*
  60. * Registers are 32bits, but only bits [15:0] are used to
  61. * indicate key status.
  62. */
  63. if (bit_nr % 32 >= 16)
  64. continue;
  65. key = bit_nr / 32 * 16 + bit_nr % 32;
  66. keypad->calc_row_col(key, &row, &col);
  67. scancode = MATRIX_SCAN_CODE(row, col, row_shift);
  68. /* 1: not pressed, 0: pressed */
  69. pressed = !test_bit(bit_nr, new_state);
  70. dev_dbg(&keypad->input_dev->dev, "%s",
  71. pressed ? "pressed" : "released");
  72. input_event(keypad->input_dev, EV_MSC, MSC_SCAN, scancode);
  73. input_report_key(keypad->input_dev, keycode[scancode], pressed);
  74. input_sync(keypad->input_dev);
  75. dev_dbg(&keypad->input_dev->dev,
  76. "report Linux keycode = %d\n", keycode[scancode]);
  77. }
  78. bitmap_copy(keypad->keymap_state, new_state, MTK_KPD_NUM_BITS);
  79. return IRQ_HANDLED;
  80. }
  81. static void mt6779_keypad_calc_row_col_single(unsigned int key,
  82. unsigned int *row,
  83. unsigned int *col)
  84. {
  85. *row = key / 9;
  86. *col = key % 9;
  87. }
  88. static void mt6779_keypad_calc_row_col_double(unsigned int key,
  89. unsigned int *row,
  90. unsigned int *col)
  91. {
  92. *row = key / 13;
  93. *col = (key % 13) / 2;
  94. }
  95. static int mt6779_keypad_pdrv_probe(struct platform_device *pdev)
  96. {
  97. struct mt6779_keypad *keypad;
  98. void __iomem *base;
  99. int irq;
  100. u32 debounce;
  101. u32 keys_per_group;
  102. bool wakeup;
  103. int error;
  104. keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad), GFP_KERNEL);
  105. if (!keypad)
  106. return -ENOMEM;
  107. base = devm_platform_ioremap_resource(pdev, 0);
  108. if (IS_ERR(base))
  109. return PTR_ERR(base);
  110. keypad->regmap = devm_regmap_init_mmio(&pdev->dev, base,
  111. &mt6779_keypad_regmap_cfg);
  112. if (IS_ERR(keypad->regmap)) {
  113. dev_err(&pdev->dev,
  114. "regmap init failed:%pe\n", keypad->regmap);
  115. return PTR_ERR(keypad->regmap);
  116. }
  117. bitmap_fill(keypad->keymap_state, MTK_KPD_NUM_BITS);
  118. keypad->input_dev = devm_input_allocate_device(&pdev->dev);
  119. if (!keypad->input_dev) {
  120. dev_err(&pdev->dev, "Failed to allocate input dev\n");
  121. return -ENOMEM;
  122. }
  123. keypad->input_dev->name = MTK_KPD_NAME;
  124. keypad->input_dev->id.bustype = BUS_HOST;
  125. error = matrix_keypad_parse_properties(&pdev->dev, &keypad->n_rows,
  126. &keypad->n_cols);
  127. if (error) {
  128. dev_err(&pdev->dev, "Failed to parse keypad params\n");
  129. return error;
  130. }
  131. if (device_property_read_u32(&pdev->dev, "debounce-delay-ms",
  132. &debounce))
  133. debounce = 16;
  134. if (debounce > MTK_KPD_DEBOUNCE_MAX_MS) {
  135. dev_err(&pdev->dev,
  136. "Debounce time exceeds the maximum allowed time %dms\n",
  137. MTK_KPD_DEBOUNCE_MAX_MS);
  138. return -EINVAL;
  139. }
  140. if (device_property_read_u32(&pdev->dev, "mediatek,keys-per-group",
  141. &keys_per_group))
  142. keys_per_group = 1;
  143. switch (keys_per_group) {
  144. case 1:
  145. keypad->calc_row_col = mt6779_keypad_calc_row_col_single;
  146. break;
  147. case 2:
  148. keypad->calc_row_col = mt6779_keypad_calc_row_col_double;
  149. break;
  150. default:
  151. dev_err(&pdev->dev,
  152. "Invalid keys-per-group: %d\n", keys_per_group);
  153. return -EINVAL;
  154. }
  155. wakeup = device_property_read_bool(&pdev->dev, "wakeup-source");
  156. dev_dbg(&pdev->dev, "n_row=%d n_col=%d debounce=%d\n",
  157. keypad->n_rows, keypad->n_cols, debounce);
  158. error = matrix_keypad_build_keymap(NULL, NULL,
  159. keypad->n_rows, keypad->n_cols,
  160. NULL, keypad->input_dev);
  161. if (error) {
  162. dev_err(&pdev->dev, "Failed to build keymap\n");
  163. return error;
  164. }
  165. input_set_capability(keypad->input_dev, EV_MSC, MSC_SCAN);
  166. regmap_write(keypad->regmap, MTK_KPD_DEBOUNCE,
  167. (debounce * (1 << 5)) & MTK_KPD_DEBOUNCE_MASK);
  168. if (keys_per_group == 2)
  169. regmap_update_bits(keypad->regmap, MTK_KPD_SEL,
  170. MTK_KPD_SEL_DOUBLE_KP_MODE,
  171. MTK_KPD_SEL_DOUBLE_KP_MODE);
  172. regmap_update_bits(keypad->regmap, MTK_KPD_SEL, MTK_KPD_SEL_ROW,
  173. MTK_KPD_SEL_ROWMASK(keypad->n_rows));
  174. regmap_update_bits(keypad->regmap, MTK_KPD_SEL, MTK_KPD_SEL_COL,
  175. MTK_KPD_SEL_COLMASK(keypad->n_cols));
  176. keypad->clk = devm_clk_get_enabled(&pdev->dev, "kpd");
  177. if (IS_ERR(keypad->clk))
  178. return PTR_ERR(keypad->clk);
  179. irq = platform_get_irq(pdev, 0);
  180. if (irq < 0)
  181. return irq;
  182. error = devm_request_threaded_irq(&pdev->dev, irq,
  183. NULL, mt6779_keypad_irq_handler,
  184. IRQF_ONESHOT, MTK_KPD_NAME, keypad);
  185. if (error) {
  186. dev_err(&pdev->dev, "Failed to request IRQ#%d: %d\n",
  187. irq, error);
  188. return error;
  189. }
  190. error = input_register_device(keypad->input_dev);
  191. if (error) {
  192. dev_err(&pdev->dev, "Failed to register device\n");
  193. return error;
  194. }
  195. error = device_init_wakeup(&pdev->dev, wakeup);
  196. if (error)
  197. dev_warn(&pdev->dev, "device_init_wakeup() failed: %d\n",
  198. error);
  199. return 0;
  200. }
  201. static const struct of_device_id mt6779_keypad_of_match[] = {
  202. { .compatible = "mediatek,mt6779-keypad" },
  203. { .compatible = "mediatek,mt6873-keypad" },
  204. { /* sentinel */ }
  205. };
  206. MODULE_DEVICE_TABLE(of, mt6779_keypad_of_match);
  207. static struct platform_driver mt6779_keypad_pdrv = {
  208. .probe = mt6779_keypad_pdrv_probe,
  209. .driver = {
  210. .name = MTK_KPD_NAME,
  211. .of_match_table = mt6779_keypad_of_match,
  212. },
  213. };
  214. module_platform_driver(mt6779_keypad_pdrv);
  215. MODULE_AUTHOR("Mediatek Corporation");
  216. MODULE_DESCRIPTION("MTK Keypad (KPD) Driver");
  217. MODULE_LICENSE("GPL");