xref: /linux/drivers/input/keyboard/adp5585-keys.c (revision d2c9a99135da931377240942d44f3dea104cedb8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Analog Devices ADP5585 Keys driver
4  *
5  * Copyright (C) 2025 Analog Devices, Inc.
6  */
7 
8 #include <linux/bitmap.h>
9 #include <linux/container_of.h>
10 #include <linux/device.h>
11 #include <linux/find.h>
12 #include <linux/input.h>
13 #include <linux/input/matrix_keypad.h>
14 #include <linux/mfd/adp5585.h>
15 #include <linux/module.h>
16 #include <linux/notifier.h>
17 #include <linux/platform_device.h>
18 #include <linux/property.h>
19 #include <linux/regmap.h>
20 #include <linux/types.h>
21 
22 /* As needed for the matrix parsing code */
23 #define ADP5589_MAX_KEYMAPSIZE		123
24 
25 struct adp5585_kpad_chip {
26 	u8 key_ev_min;
27 	u8 key_ev_max;
28 	u8 max_rows;
29 	u8 max_cols;
30 };
31 
32 struct adp5585_kpad {
33 	const struct adp5585_kpad_chip *info;
34 	struct notifier_block nb;
35 	struct input_dev *input;
36 	unsigned short keycode[ADP5589_MAX_KEYMAPSIZE];
37 	struct device *dev;
38 	unsigned long keypad;
39 	int row_shift;
40 };
41 
adp5585_keys_validate_events(const struct adp5585_kpad * kpad,const u32 * events,u32 n_events)42 static int adp5585_keys_validate_events(const struct adp5585_kpad *kpad,
43 					const u32 *events, u32 n_events)
44 {
45 	unsigned int ev;
46 	u32 row, col;
47 
48 	for (ev = 0; ev < n_events; ev++) {
49 		if (events[ev] < kpad->info->key_ev_min ||
50 		    events[ev] > kpad->info->key_ev_max)
51 			continue;
52 
53 		/*
54 		 * if the event is to be generated by the keymap, we need to make
55 		 * sure that the pins are part of it!
56 		 */
57 		row = (events[ev] - 1) / kpad->info->max_cols;
58 		col = (events[ev] - 1) % kpad->info->max_cols;
59 
60 		if (test_bit(row, &kpad->keypad) &&
61 		    test_bit(col + kpad->info->max_rows, &kpad->keypad))
62 			continue;
63 
64 		return dev_err_probe(kpad->dev, -EINVAL,
65 				     "Invalid unlock/reset event(%u) not used in the keypad\n",
66 				     events[ev]);
67 	}
68 
69 	return 0;
70 }
71 
adp5585_keys_check_special_events(const struct adp5585_dev * adp5585,const struct adp5585_kpad * kpad)72 static int adp5585_keys_check_special_events(const struct adp5585_dev *adp5585,
73 					     const struct adp5585_kpad *kpad)
74 {
75 	int error;
76 
77 	error = adp5585_keys_validate_events(kpad, adp5585->unlock_keys,
78 					     adp5585->nkeys_unlock);
79 	if (error)
80 		return error;
81 
82 	error = adp5585_keys_validate_events(kpad, adp5585->reset1_keys,
83 					     adp5585->nkeys_reset1);
84 	if (error)
85 		return error;
86 
87 	return adp5585_keys_validate_events(kpad, adp5585->reset2_keys,
88 					    adp5585->nkeys_reset2);
89 }
90 
adp5585_keys_pins_free(void * data)91 static void adp5585_keys_pins_free(void *data)
92 {
93 	struct adp5585_kpad *kpad = data;
94 	struct adp5585_dev *adp5585 = dev_get_drvdata(kpad->dev->parent);
95 	unsigned int pin;
96 
97 	for_each_set_bit(pin, &kpad->keypad, adp5585->n_pins)
98 		clear_bit(pin, adp5585->pin_usage);
99 }
100 
adp5585_keys_parse_fw(const struct adp5585_dev * adp5585,struct adp5585_kpad * kpad)101 static int adp5585_keys_parse_fw(const struct adp5585_dev *adp5585,
102 				 struct adp5585_kpad *kpad)
103 {
104 	struct device *dev = kpad->dev;
105 	u32 cols = 0, rows = 0, pin;
106 	int error, n_pins;
107 
108 	/*
109 	 * We do not check for errors (or no value) since the input device is
110 	 * only added if this property is present in the first place.
111 	 */
112 	n_pins = device_property_count_u32(dev, "adi,keypad-pins");
113 	if (n_pins > adp5585->n_pins)
114 		return dev_err_probe(dev, -EINVAL,
115 				     "Too many keypad pins (%d) defined (max=%d)\n",
116 				     n_pins, adp5585->n_pins);
117 
118 	unsigned int *keypad_pins __free(kfree) = kcalloc(n_pins, sizeof(*keypad_pins),
119 							  GFP_KERNEL);
120 	if (!keypad_pins)
121 		return -ENOMEM;
122 
123 	error = device_property_read_u32_array(dev, "adi,keypad-pins",
124 					       keypad_pins, n_pins);
125 	if (error)
126 		return error;
127 
128 	/*
129 	 * We can add the action here since it makes the code easier and nothing
130 	 * "bad" will happen out of it. Worst case, it will be a no-op and no
131 	 * bit will set.
132 	 */
133 	error = devm_add_action_or_reset(dev, adp5585_keys_pins_free, kpad);
134 	if (error)
135 		return error;
136 
137 	for (pin = 0; pin < n_pins; pin++) {
138 		if (keypad_pins[pin] >= adp5585->n_pins)
139 			return dev_err_probe(dev, -EINVAL,
140 					     "Invalid keypad pin(%u) defined\n",
141 					     keypad_pins[pin]);
142 
143 		if (test_and_set_bit(keypad_pins[pin], adp5585->pin_usage))
144 			return dev_err_probe(dev, -EBUSY,
145 					     "Keypad pin(%u) already used\n",
146 					     keypad_pins[pin]);
147 
148 		__set_bit(keypad_pins[pin], &kpad->keypad);
149 	}
150 
151 	/*
152 	 * Note that given that we get a mask (and the HW allows it), we
153 	 * can have holes in our keypad (eg: row0, row1 and row7 enabled).
154 	 * However, for the matrix parsing functions we need to pass the
155 	 * number of rows/cols as the maximum row/col used plus 1. This
156 	 * pretty much means we will also have holes in our SW keypad.
157 	 */
158 
159 	rows = find_last_bit(&kpad->keypad, kpad->info->max_rows) + 1;
160 	if (rows == kpad->info->max_rows + 1)
161 		return dev_err_probe(dev, -EINVAL,
162 				     "Now rows defined in the keypad!\n");
163 
164 	cols = find_last_bit(&kpad->keypad, kpad->info->max_cols + kpad->info->max_rows);
165 	if (cols < kpad->info->max_rows)
166 		return dev_err_probe(dev, -EINVAL,
167 				     "No columns defined in the keypad!\n");
168 
169 	cols = cols + 1 - kpad->info->max_rows;
170 
171 	error = matrix_keypad_build_keymap(NULL, NULL, rows, cols,
172 					   kpad->keycode, kpad->input);
173 	if (error)
174 		return error;
175 
176 	kpad->row_shift = get_count_order(cols);
177 
178 	if (device_property_read_bool(kpad->dev, "autorepeat"))
179 		__set_bit(EV_REP, kpad->input->evbit);
180 
181 	error = adp5585_keys_check_special_events(adp5585, kpad);
182 	if (error)
183 		return error;
184 
185 	return 0;
186 }
187 
adp5585_keys_setup(const struct adp5585_dev * adp5585,struct adp5585_kpad * kpad)188 static int adp5585_keys_setup(const struct adp5585_dev *adp5585,
189 			      struct adp5585_kpad *kpad)
190 {
191 	unsigned long keys_bits, start = 0, nbits = kpad->info->max_rows;
192 	const struct adp5585_regs *regs = adp5585->regs;
193 	unsigned int i = 0, max_cols = kpad->info->max_cols;
194 	int error;
195 
196 	/*
197 	 * Take care as the below assumes max_rows is always less or equal than
198 	 * 8 which is true for the supported devices. If we happen to add
199 	 * another device we need to make sure this still holds true. Although
200 	 * adding a new device is very unlikely.
201 	 */
202 	do {
203 		keys_bits = bitmap_read(&kpad->keypad, start, nbits);
204 		if (keys_bits) {
205 			error = regmap_write(adp5585->regmap, regs->pin_cfg_a + i,
206 					     keys_bits);
207 			if (error)
208 				return error;
209 		}
210 
211 		start += nbits;
212 		if (max_cols > 8) {
213 			nbits = 8;
214 			max_cols -= nbits;
215 		} else {
216 			nbits = max_cols;
217 		}
218 
219 		i++;
220 	} while (start < kpad->info->max_rows + kpad->info->max_cols);
221 
222 	return 0;
223 }
224 
adp5585_keys_ev_handle(struct notifier_block * nb,unsigned long key,void * data)225 static int adp5585_keys_ev_handle(struct notifier_block *nb, unsigned long key,
226 				  void *data)
227 {
228 	struct adp5585_kpad *kpad = container_of(nb, struct adp5585_kpad, nb);
229 	unsigned long key_press = (unsigned long)data;
230 	unsigned int row, col, code;
231 
232 	/* make sure the event is for us */
233 	if (key < kpad->info->key_ev_min || key > kpad->info->key_ev_max)
234 		return NOTIFY_DONE;
235 
236 	/*
237 	 * Unlikely but lets be on the safe side! We do not return any error
238 	 * because the event was indeed for us but with some weird value. So,
239 	 * we still want the caller know that the right handler was called.
240 	 */
241 	if (!key)
242 		return NOTIFY_BAD;
243 
244 	row = (key - 1) / (kpad->info->max_cols);
245 	col = (key - 1) % (kpad->info->max_cols);
246 	code = MATRIX_SCAN_CODE(row, col, kpad->row_shift);
247 
248 	dev_dbg_ratelimited(kpad->dev, "report key(%lu) r(%d) c(%d) code(%d)\n",
249 			    key, row, col, kpad->keycode[code]);
250 
251 	input_report_key(kpad->input, kpad->keycode[code], key_press);
252 	input_sync(kpad->input);
253 
254 	return NOTIFY_STOP;
255 }
256 
adp5585_keys_unreg_notifier(void * data)257 static void adp5585_keys_unreg_notifier(void *data)
258 {
259 	struct adp5585_kpad *kpad = data;
260 	struct adp5585_dev *adp5585 = dev_get_drvdata(kpad->dev->parent);
261 
262 	blocking_notifier_chain_unregister(&adp5585->event_notifier,
263 					   &kpad->nb);
264 }
265 
adp5585_keys_probe(struct platform_device * pdev)266 static int adp5585_keys_probe(struct platform_device *pdev)
267 {
268 	const struct platform_device_id *id = platform_get_device_id(pdev);
269 	struct adp5585_dev *adp5585 = dev_get_drvdata(pdev->dev.parent);
270 	struct device *dev = &pdev->dev;
271 	struct adp5585_kpad *kpad;
272 	unsigned int revid;
273 	const char *phys;
274 	int error;
275 
276 	kpad = devm_kzalloc(dev, sizeof(*kpad), GFP_KERNEL);
277 	if (!kpad)
278 		return -ENOMEM;
279 
280 	if (!adp5585->irq)
281 		return dev_err_probe(dev, -EINVAL,
282 				     "IRQ is mandatory for the keypad\n");
283 
284 	kpad->dev = dev;
285 
286 	kpad->input = devm_input_allocate_device(dev);
287 	if (!kpad->input)
288 		return -ENOMEM;
289 
290 	kpad->info = (const struct adp5585_kpad_chip *)id->driver_data;
291 	if (!kpad->info)
292 		return -ENODEV;
293 
294 	error = regmap_read(adp5585->regmap, ADP5585_ID, &revid);
295 	if (error)
296 		return dev_err_probe(dev, error, "Failed to read device ID\n");
297 
298 	phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input0", pdev->name);
299 	if (!phys)
300 		return -ENOMEM;
301 
302 	kpad->input->name = pdev->name;
303 	kpad->input->phys = phys;
304 
305 	kpad->input->id.bustype = BUS_I2C;
306 	kpad->input->id.vendor = 0x0001;
307 	kpad->input->id.product = 0x0001;
308 	kpad->input->id.version = revid & ADP5585_REV_ID_MASK;
309 
310 	device_set_of_node_from_dev(dev, dev->parent);
311 
312 	error = adp5585_keys_parse_fw(adp5585, kpad);
313 	if (error)
314 		return error;
315 
316 	error = adp5585_keys_setup(adp5585, kpad);
317 	if (error)
318 		return error;
319 
320 	kpad->nb.notifier_call = adp5585_keys_ev_handle;
321 	error = blocking_notifier_chain_register(&adp5585->event_notifier,
322 						 &kpad->nb);
323 	if (error)
324 		return error;
325 
326 	error = devm_add_action_or_reset(dev, adp5585_keys_unreg_notifier, kpad);
327 	if (error)
328 		return error;
329 
330 	error = input_register_device(kpad->input);
331 	if (error)
332 		return dev_err_probe(dev, error,
333 				     "Failed to register input device\n");
334 
335 	return 0;
336 }
337 
338 static const struct adp5585_kpad_chip adp5585_kpad_chip_info = {
339 	.max_rows = 6,
340 	.max_cols = 5,
341 	.key_ev_min = ADP5585_ROW5_KEY_EVENT_START,
342 	.key_ev_max = ADP5585_ROW5_KEY_EVENT_END,
343 };
344 
345 static const struct adp5585_kpad_chip adp5589_kpad_chip_info = {
346 	.max_rows = 8,
347 	.max_cols = 11,
348 	.key_ev_min = ADP5589_KEY_EVENT_START,
349 	.key_ev_max = ADP5589_KEY_EVENT_END,
350 };
351 
352 static const struct platform_device_id adp5585_keys_id_table[] = {
353 	{ "adp5585-keys", (kernel_ulong_t)&adp5585_kpad_chip_info },
354 	{ "adp5589-keys", (kernel_ulong_t)&adp5589_kpad_chip_info },
355 	{ }
356 };
357 MODULE_DEVICE_TABLE(platform, adp5585_keys_id_table);
358 
359 static struct platform_driver adp5585_keys_driver = {
360 	.driver	= {
361 		.name = "adp5585-keys",
362 	},
363 	.probe = adp5585_keys_probe,
364 	.id_table = adp5585_keys_id_table,
365 };
366 module_platform_driver(adp5585_keys_driver);
367 
368 MODULE_AUTHOR("Nuno Sá <nuno.sa@analog.com>");
369 MODULE_DESCRIPTION("ADP5585 Keys Driver");
370 MODULE_LICENSE("GPL");
371