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 
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 */
29 
30 struct mt6779_keypad {
31 	struct regmap *regmap;
32 	struct input_dev *input_dev;
33 	struct clk *clk;
34 	u32 n_rows;
35 	u32 n_cols;
36 	void (*calc_row_col)(unsigned int key,
37 			     unsigned int *row, unsigned int *col);
38 	DECLARE_BITMAP(keymap_state, MTK_KPD_NUM_BITS);
39 };
40 
41 static const struct regmap_config mt6779_keypad_regmap_cfg = {
42 	.reg_bits = 32,
43 	.val_bits = 32,
44 	.reg_stride = sizeof(u32),
45 	.max_register = 36,
46 };
47 
mt6779_keypad_irq_handler(int irq,void * dev_id)48 static irqreturn_t mt6779_keypad_irq_handler(int irq, void *dev_id)
49 {
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);
58 	bool pressed;
59 
60 	regmap_bulk_read(keypad->regmap, MTK_KPD_MEM,
61 			 new_state, MTK_KPD_NUM_MEMS);
62 
63 	bitmap_xor(change, new_state, keypad->keymap_state, MTK_KPD_NUM_BITS);
64 
65 	for_each_set_bit(bit_nr, change, MTK_KPD_NUM_BITS) {
66 		/*
67 		 * Registers are 32bits, but only bits [15:0] are used to
68 		 * indicate key status.
69 		 */
70 		if (bit_nr % 32 >= 16)
71 			continue;
72 
73 		key = bit_nr / 32 * 16 + bit_nr % 32;
74 		keypad->calc_row_col(key, &row, &col);
75 
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");
81 
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);
85 
86 		dev_dbg(&keypad->input_dev->dev,
87 			"report Linux keycode = %d\n", keycode[scancode]);
88 	}
89 
90 	bitmap_copy(keypad->keymap_state, new_state, MTK_KPD_NUM_BITS);
91 
92 	return IRQ_HANDLED;
93 }
94 
mt6779_keypad_calc_row_col_single(unsigned int key,unsigned int * row,unsigned int * col)95 static void mt6779_keypad_calc_row_col_single(unsigned int key,
96 					      unsigned int *row,
97 					      unsigned int *col)
98 {
99 	*row = key / 9;
100 	*col = key % 9;
101 }
102 
mt6779_keypad_calc_row_col_double(unsigned int key,unsigned int * row,unsigned int * col)103 static void mt6779_keypad_calc_row_col_double(unsigned int key,
104 					      unsigned int *row,
105 					      unsigned int *col)
106 {
107 	*row = key / 13;
108 	*col = (key % 13) / 2;
109 }
110 
mt6779_keypad_pdrv_probe(struct platform_device * pdev)111 static int mt6779_keypad_pdrv_probe(struct platform_device *pdev)
112 {
113 	struct mt6779_keypad *keypad;
114 	void __iomem *base;
115 	int irq;
116 	u32 debounce;
117 	u32 keys_per_group;
118 	bool wakeup;
119 	int error;
120 
121 	keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad), GFP_KERNEL);
122 	if (!keypad)
123 		return -ENOMEM;
124 
125 	base = devm_platform_ioremap_resource(pdev, 0);
126 	if (IS_ERR(base))
127 		return PTR_ERR(base);
128 
129 	keypad->regmap = devm_regmap_init_mmio(&pdev->dev, base,
130 					       &mt6779_keypad_regmap_cfg);
131 	if (IS_ERR(keypad->regmap)) {
132 		dev_err(&pdev->dev,
133 			"regmap init failed:%pe\n", keypad->regmap);
134 		return PTR_ERR(keypad->regmap);
135 	}
136 
137 	bitmap_fill(keypad->keymap_state, MTK_KPD_NUM_BITS);
138 
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");
142 		return -ENOMEM;
143 	}
144 
145 	keypad->input_dev->name = MTK_KPD_NAME;
146 	keypad->input_dev->id.bustype = BUS_HOST;
147 
148 	error = matrix_keypad_parse_properties(&pdev->dev, &keypad->n_rows,
149 					       &keypad->n_cols);
150 	if (error) {
151 		dev_err(&pdev->dev, "Failed to parse keypad params\n");
152 		return error;
153 	}
154 
155 	if (device_property_read_u32(&pdev->dev, "debounce-delay-ms",
156 				     &debounce))
157 		debounce = 16;
158 
159 	if (debounce > MTK_KPD_DEBOUNCE_MAX_MS) {
160 		dev_err(&pdev->dev,
161 			"Debounce time exceeds the maximum allowed time %dms\n",
162 			MTK_KPD_DEBOUNCE_MAX_MS);
163 		return -EINVAL;
164 	}
165 
166 	if (device_property_read_u32(&pdev->dev, "mediatek,keys-per-group",
167 				     &keys_per_group))
168 		keys_per_group = 1;
169 
170 	switch (keys_per_group) {
171 	case 1:
172 		keypad->calc_row_col = mt6779_keypad_calc_row_col_single;
173 		break;
174 	case 2:
175 		keypad->calc_row_col = mt6779_keypad_calc_row_col_double;
176 		break;
177 	default:
178 		dev_err(&pdev->dev,
179 			"Invalid keys-per-group: %d\n", keys_per_group);
180 		return -EINVAL;
181 	}
182 
183 	wakeup = device_property_read_bool(&pdev->dev, "wakeup-source");
184 
185 	dev_dbg(&pdev->dev, "n_row=%d n_col=%d debounce=%d\n",
186 		keypad->n_rows, keypad->n_cols, debounce);
187 
188 	error = matrix_keypad_build_keymap(NULL, NULL,
189 					   keypad->n_rows, keypad->n_cols,
190 					   NULL, keypad->input_dev);
191 	if (error) {
192 		dev_err(&pdev->dev, "Failed to build keymap\n");
193 		return error;
194 	}
195 
196 	input_set_capability(keypad->input_dev, EV_MSC, MSC_SCAN);
197 
198 	regmap_write(keypad->regmap, MTK_KPD_DEBOUNCE,
199 		     (debounce * (1 << 5)) & MTK_KPD_DEBOUNCE_MASK);
200 
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);
205 
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));
210 
211 	keypad->clk = devm_clk_get_enabled(&pdev->dev, "kpd");
212 	if (IS_ERR(keypad->clk))
213 		return PTR_ERR(keypad->clk);
214 
215 	irq = platform_get_irq(pdev, 0);
216 	if (irq < 0)
217 		return irq;
218 
219 	error = devm_request_threaded_irq(&pdev->dev, irq,
220 					  NULL, mt6779_keypad_irq_handler,
221 					  IRQF_ONESHOT, MTK_KPD_NAME, keypad);
222 	if (error) {
223 		dev_err(&pdev->dev, "Failed to request IRQ#%d: %d\n",
224 			irq, error);
225 		return error;
226 	}
227 
228 	error = input_register_device(keypad->input_dev);
229 	if (error) {
230 		dev_err(&pdev->dev, "Failed to register device\n");
231 		return error;
232 	}
233 
234 	error = device_init_wakeup(&pdev->dev, wakeup);
235 	if (error)
236 		dev_warn(&pdev->dev, "device_init_wakeup() failed: %d\n",
237 			 error);
238 
239 	return 0;
240 }
241 
242 static const struct of_device_id mt6779_keypad_of_match[] = {
243 	{ .compatible = "mediatek,mt6779-keypad" },
244 	{ .compatible = "mediatek,mt6873-keypad" },
245 	{ /* sentinel */ }
246 };
247 MODULE_DEVICE_TABLE(of, mt6779_keypad_of_match);
248 
249 static struct platform_driver mt6779_keypad_pdrv = {
250 	.probe = mt6779_keypad_pdrv_probe,
251 	.driver = {
252 		   .name = MTK_KPD_NAME,
253 		   .of_match_table = mt6779_keypad_of_match,
254 	},
255 };
256 module_platform_driver(mt6779_keypad_pdrv);
257 
258 MODULE_AUTHOR("Mediatek Corporation");
259 MODULE_DESCRIPTION("MTK Keypad (KPD) Driver");
260 MODULE_LICENSE("GPL");
261