1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2022 MediaTek Inc.
4 * Author Fengping Yu <fengping.yu@mediatek.com>
6 #include <linux/bitops.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>
16 #define MTK_KPD_NAME "mt6779-keypad"
17 #define MTK_KPD_MEM 0x0004
18 #define MTK_KPD_DEBOUNCE 0x0018
19 #define MTK_KPD_DEBOUNCE_MASK GENMASK(13, 0)
20 #define MTK_KPD_DEBOUNCE_MAX_MS 256
21 #define MTK_KPD_SEL 0x0020
22 #define MTK_KPD_SEL_DOUBLE_KP_MODE BIT(0)
23 #define MTK_KPD_SEL_COL GENMASK(15, 10)
24 #define MTK_KPD_SEL_ROW GENMASK(9, 4)
25 #define MTK_KPD_SEL_COLMASK(c) GENMASK((c) + 9, 10)
26 #define MTK_KPD_SEL_ROWMASK(r) GENMASK((r) + 3, 4)
27 #define MTK_KPD_NUM_MEMS 5
28 #define MTK_KPD_NUM_BITS 136 /* 4*32+8 MEM5 only use 8 BITS */
30 struct mt6779_keypad
{
31 struct regmap
*regmap
;
32 struct input_dev
*input_dev
;
36 void (*calc_row_col
)(unsigned int key
,
37 unsigned int *row
, unsigned int *col
);
38 DECLARE_BITMAP(keymap_state
, MTK_KPD_NUM_BITS
);
41 static const struct regmap_config mt6779_keypad_regmap_cfg
= {
44 .reg_stride
= sizeof(u32
),
48 static irqreturn_t
mt6779_keypad_irq_handler(int irq
, void *dev_id
)
50 struct mt6779_keypad
*keypad
= dev_id
;
51 const unsigned short *keycode
= keypad
->input_dev
->keycode
;
52 DECLARE_BITMAP(new_state
, MTK_KPD_NUM_BITS
);
53 DECLARE_BITMAP(change
, MTK_KPD_NUM_BITS
);
54 unsigned int bit_nr
, key
;
55 unsigned int row
, col
;
56 unsigned int scancode
;
57 unsigned int row_shift
= get_count_order(keypad
->n_cols
);
60 regmap_bulk_read(keypad
->regmap
, MTK_KPD_MEM
,
61 new_state
, MTK_KPD_NUM_MEMS
);
63 bitmap_xor(change
, new_state
, keypad
->keymap_state
, MTK_KPD_NUM_BITS
);
65 for_each_set_bit(bit_nr
, change
, MTK_KPD_NUM_BITS
) {
67 * Registers are 32bits, but only bits [15:0] are used to
68 * indicate key status.
70 if (bit_nr
% 32 >= 16)
73 key
= bit_nr
/ 32 * 16 + bit_nr
% 32;
74 keypad
->calc_row_col(key
, &row
, &col
);
76 scancode
= MATRIX_SCAN_CODE(row
, col
, row_shift
);
77 /* 1: not pressed, 0: pressed */
78 pressed
= !test_bit(bit_nr
, new_state
);
79 dev_dbg(&keypad
->input_dev
->dev
, "%s",
80 pressed
? "pressed" : "released");
82 input_event(keypad
->input_dev
, EV_MSC
, MSC_SCAN
, scancode
);
83 input_report_key(keypad
->input_dev
, keycode
[scancode
], pressed
);
84 input_sync(keypad
->input_dev
);
86 dev_dbg(&keypad
->input_dev
->dev
,
87 "report Linux keycode = %d\n", keycode
[scancode
]);
90 bitmap_copy(keypad
->keymap_state
, new_state
, MTK_KPD_NUM_BITS
);
95 static void mt6779_keypad_calc_row_col_single(unsigned int key
,
103 static void mt6779_keypad_calc_row_col_double(unsigned int key
,
108 *col
= (key
% 13) / 2;
111 static int mt6779_keypad_pdrv_probe(struct platform_device
*pdev
)
113 struct mt6779_keypad
*keypad
;
121 keypad
= devm_kzalloc(&pdev
->dev
, sizeof(*keypad
), GFP_KERNEL
);
125 base
= devm_platform_ioremap_resource(pdev
, 0);
127 return PTR_ERR(base
);
129 keypad
->regmap
= devm_regmap_init_mmio(&pdev
->dev
, base
,
130 &mt6779_keypad_regmap_cfg
);
131 if (IS_ERR(keypad
->regmap
)) {
133 "regmap init failed:%pe\n", keypad
->regmap
);
134 return PTR_ERR(keypad
->regmap
);
137 bitmap_fill(keypad
->keymap_state
, MTK_KPD_NUM_BITS
);
139 keypad
->input_dev
= devm_input_allocate_device(&pdev
->dev
);
140 if (!keypad
->input_dev
) {
141 dev_err(&pdev
->dev
, "Failed to allocate input dev\n");
145 keypad
->input_dev
->name
= MTK_KPD_NAME
;
146 keypad
->input_dev
->id
.bustype
= BUS_HOST
;
148 error
= matrix_keypad_parse_properties(&pdev
->dev
, &keypad
->n_rows
,
151 dev_err(&pdev
->dev
, "Failed to parse keypad params\n");
155 if (device_property_read_u32(&pdev
->dev
, "debounce-delay-ms",
159 if (debounce
> MTK_KPD_DEBOUNCE_MAX_MS
) {
161 "Debounce time exceeds the maximum allowed time %dms\n",
162 MTK_KPD_DEBOUNCE_MAX_MS
);
166 if (device_property_read_u32(&pdev
->dev
, "mediatek,keys-per-group",
170 switch (keys_per_group
) {
172 keypad
->calc_row_col
= mt6779_keypad_calc_row_col_single
;
175 keypad
->calc_row_col
= mt6779_keypad_calc_row_col_double
;
179 "Invalid keys-per-group: %d\n", keys_per_group
);
183 wakeup
= device_property_read_bool(&pdev
->dev
, "wakeup-source");
185 dev_dbg(&pdev
->dev
, "n_row=%d n_col=%d debounce=%d\n",
186 keypad
->n_rows
, keypad
->n_cols
, debounce
);
188 error
= matrix_keypad_build_keymap(NULL
, NULL
,
189 keypad
->n_rows
, keypad
->n_cols
,
190 NULL
, keypad
->input_dev
);
192 dev_err(&pdev
->dev
, "Failed to build keymap\n");
196 input_set_capability(keypad
->input_dev
, EV_MSC
, MSC_SCAN
);
198 regmap_write(keypad
->regmap
, MTK_KPD_DEBOUNCE
,
199 (debounce
* (1 << 5)) & MTK_KPD_DEBOUNCE_MASK
);
201 if (keys_per_group
== 2)
202 regmap_update_bits(keypad
->regmap
, MTK_KPD_SEL
,
203 MTK_KPD_SEL_DOUBLE_KP_MODE
,
204 MTK_KPD_SEL_DOUBLE_KP_MODE
);
206 regmap_update_bits(keypad
->regmap
, MTK_KPD_SEL
, MTK_KPD_SEL_ROW
,
207 MTK_KPD_SEL_ROWMASK(keypad
->n_rows
));
208 regmap_update_bits(keypad
->regmap
, MTK_KPD_SEL
, MTK_KPD_SEL_COL
,
209 MTK_KPD_SEL_COLMASK(keypad
->n_cols
));
211 keypad
->clk
= devm_clk_get_enabled(&pdev
->dev
, "kpd");
212 if (IS_ERR(keypad
->clk
))
213 return PTR_ERR(keypad
->clk
);
215 irq
= platform_get_irq(pdev
, 0);
219 error
= devm_request_threaded_irq(&pdev
->dev
, irq
,
220 NULL
, mt6779_keypad_irq_handler
,
221 IRQF_ONESHOT
, MTK_KPD_NAME
, keypad
);
223 dev_err(&pdev
->dev
, "Failed to request IRQ#%d: %d\n",
228 error
= input_register_device(keypad
->input_dev
);
230 dev_err(&pdev
->dev
, "Failed to register device\n");
234 error
= device_init_wakeup(&pdev
->dev
, wakeup
);
236 dev_warn(&pdev
->dev
, "device_init_wakeup() failed: %d\n",
242 static const struct of_device_id mt6779_keypad_of_match
[] = {
243 { .compatible
= "mediatek,mt6779-keypad" },
244 { .compatible
= "mediatek,mt6873-keypad" },
247 MODULE_DEVICE_TABLE(of
, mt6779_keypad_of_match
);
249 static struct platform_driver mt6779_keypad_pdrv
= {
250 .probe
= mt6779_keypad_pdrv_probe
,
252 .name
= MTK_KPD_NAME
,
253 .of_match_table
= mt6779_keypad_of_match
,
256 module_platform_driver(mt6779_keypad_pdrv
);
258 MODULE_AUTHOR("Mediatek Corporation");
259 MODULE_DESCRIPTION("MTK Keypad (KPD) Driver");
260 MODULE_LICENSE("GPL");