1 // SPDX-License-Identifier: GPL-2.0+
3 * Azoteq IQS620A/621/622/624/625 Keys and Switches
5 * Copyright (C) 2019 Jeff LaBundy <jeff@labundy.com>
8 #include <linux/device.h>
9 #include <linux/input.h>
10 #include <linux/kernel.h>
11 #include <linux/mfd/iqs62x.h>
12 #include <linux/module.h>
13 #include <linux/notifier.h>
14 #include <linux/platform_device.h>
15 #include <linux/property.h>
16 #include <linux/regmap.h>
17 #include <linux/slab.h>
24 static const char * const iqs62x_switch_names[] = {
25 [IQS62X_SW_HALL_N] = "hall-switch-north",
26 [IQS62X_SW_HALL_S] = "hall-switch-south",
29 struct iqs62x_switch_desc {
30 enum iqs62x_event_flag flag;
35 struct iqs62x_keys_private {
36 struct iqs62x_core *iqs62x;
37 struct input_dev *input;
38 struct notifier_block notifier;
39 struct iqs62x_switch_desc switches[ARRAY_SIZE(iqs62x_switch_names)];
40 unsigned int keycode[IQS62X_NUM_KEYS];
41 unsigned int keycodemax;
45 static int iqs62x_keys_parse_prop(struct platform_device *pdev,
46 struct iqs62x_keys_private *iqs62x_keys)
48 struct fwnode_handle *child;
52 ret = device_property_count_u32(&pdev->dev, "linux,keycodes");
53 if (ret > IQS62X_NUM_KEYS) {
54 dev_err(&pdev->dev, "Too many keycodes present\n");
57 dev_err(&pdev->dev, "Failed to count keycodes: %d\n", ret);
60 iqs62x_keys->keycodemax = ret;
62 ret = device_property_read_u32_array(&pdev->dev, "linux,keycodes",
64 iqs62x_keys->keycodemax);
66 dev_err(&pdev->dev, "Failed to read keycodes: %d\n", ret);
70 for (i = 0; i < ARRAY_SIZE(iqs62x_keys->switches); i++) {
71 child = device_get_named_child_node(&pdev->dev,
72 iqs62x_switch_names[i]);
76 ret = fwnode_property_read_u32(child, "linux,code", &val);
78 dev_err(&pdev->dev, "Failed to read switch code: %d\n",
80 fwnode_handle_put(child);
83 iqs62x_keys->switches[i].code = val;
84 iqs62x_keys->switches[i].enabled = true;
86 if (fwnode_property_present(child, "azoteq,use-prox"))
87 iqs62x_keys->switches[i].flag = (i == IQS62X_SW_HALL_N ?
88 IQS62X_EVENT_HALL_N_P :
89 IQS62X_EVENT_HALL_S_P);
91 iqs62x_keys->switches[i].flag = (i == IQS62X_SW_HALL_N ?
92 IQS62X_EVENT_HALL_N_T :
93 IQS62X_EVENT_HALL_S_T);
95 fwnode_handle_put(child);
101 static int iqs62x_keys_init(struct iqs62x_keys_private *iqs62x_keys)
103 struct iqs62x_core *iqs62x = iqs62x_keys->iqs62x;
104 enum iqs62x_event_flag flag;
105 unsigned int event_reg, val;
106 unsigned int event_mask = 0;
109 switch (iqs62x->dev_desc->prod_num) {
110 case IQS620_PROD_NUM:
111 case IQS621_PROD_NUM:
112 case IQS622_PROD_NUM:
113 event_reg = IQS620_GLBL_EVENT_MASK;
116 * Discreet button, hysteresis and SAR UI flags represent keys
117 * and are unmasked if mapped to a valid keycode.
119 for (i = 0; i < iqs62x_keys->keycodemax; i++) {
120 if (iqs62x_keys->keycode[i] == KEY_RESERVED)
123 if (iqs62x_events[i].reg == IQS62X_EVENT_PROX)
124 event_mask |= iqs62x->dev_desc->prox_mask;
125 else if (iqs62x_events[i].reg == IQS62X_EVENT_HYST)
126 event_mask |= (iqs62x->dev_desc->hyst_mask |
127 iqs62x->dev_desc->sar_mask);
130 ret = regmap_read(iqs62x->regmap, iqs62x->dev_desc->hall_flags,
136 * Hall UI flags represent switches and are unmasked if their
137 * corresponding child nodes are present.
139 for (i = 0; i < ARRAY_SIZE(iqs62x_keys->switches); i++) {
140 if (!(iqs62x_keys->switches[i].enabled))
143 flag = iqs62x_keys->switches[i].flag;
145 if (iqs62x_events[flag].reg != IQS62X_EVENT_HALL)
148 event_mask |= iqs62x->dev_desc->hall_mask;
150 input_report_switch(iqs62x_keys->input,
151 iqs62x_keys->switches[i].code,
152 (val & iqs62x_events[flag].mask) ==
153 iqs62x_events[flag].val);
156 input_sync(iqs62x_keys->input);
159 case IQS624_PROD_NUM:
160 event_reg = IQS624_HALL_UI;
163 * Interval change events represent keys and are unmasked if
164 * either wheel movement flag is mapped to a valid keycode.
166 if (iqs62x_keys->keycode[IQS62X_EVENT_WHEEL_UP] != KEY_RESERVED)
167 event_mask |= IQS624_HALL_UI_INT_EVENT;
169 if (iqs62x_keys->keycode[IQS62X_EVENT_WHEEL_DN] != KEY_RESERVED)
170 event_mask |= IQS624_HALL_UI_INT_EVENT;
172 ret = regmap_read(iqs62x->regmap, iqs62x->dev_desc->interval,
177 iqs62x_keys->interval = val;
184 return regmap_update_bits(iqs62x->regmap, event_reg, event_mask, 0);
187 static int iqs62x_keys_notifier(struct notifier_block *notifier,
188 unsigned long event_flags, void *context)
190 struct iqs62x_event_data *event_data = context;
191 struct iqs62x_keys_private *iqs62x_keys;
194 iqs62x_keys = container_of(notifier, struct iqs62x_keys_private,
197 if (event_flags & BIT(IQS62X_EVENT_SYS_RESET)) {
198 ret = iqs62x_keys_init(iqs62x_keys);
200 dev_err(iqs62x_keys->input->dev.parent,
201 "Failed to re-initialize device: %d\n", ret);
208 for (i = 0; i < iqs62x_keys->keycodemax; i++) {
209 if (iqs62x_events[i].reg == IQS62X_EVENT_WHEEL &&
210 event_data->interval == iqs62x_keys->interval)
213 input_report_key(iqs62x_keys->input, iqs62x_keys->keycode[i],
214 event_flags & BIT(i));
217 for (i = 0; i < ARRAY_SIZE(iqs62x_keys->switches); i++)
218 if (iqs62x_keys->switches[i].enabled)
219 input_report_switch(iqs62x_keys->input,
220 iqs62x_keys->switches[i].code,
222 BIT(iqs62x_keys->switches[i].flag));
224 input_sync(iqs62x_keys->input);
226 if (event_data->interval == iqs62x_keys->interval)
230 * Each frame contains at most one wheel event (up or down), in which
231 * case a complementary release cycle is emulated.
233 if (event_flags & BIT(IQS62X_EVENT_WHEEL_UP)) {
234 input_report_key(iqs62x_keys->input,
235 iqs62x_keys->keycode[IQS62X_EVENT_WHEEL_UP],
237 input_sync(iqs62x_keys->input);
238 } else if (event_flags & BIT(IQS62X_EVENT_WHEEL_DN)) {
239 input_report_key(iqs62x_keys->input,
240 iqs62x_keys->keycode[IQS62X_EVENT_WHEEL_DN],
242 input_sync(iqs62x_keys->input);
245 iqs62x_keys->interval = event_data->interval;
250 static int iqs62x_keys_probe(struct platform_device *pdev)
252 struct iqs62x_core *iqs62x = dev_get_drvdata(pdev->dev.parent);
253 struct iqs62x_keys_private *iqs62x_keys;
254 struct input_dev *input;
257 iqs62x_keys = devm_kzalloc(&pdev->dev, sizeof(*iqs62x_keys),
262 platform_set_drvdata(pdev, iqs62x_keys);
264 ret = iqs62x_keys_parse_prop(pdev, iqs62x_keys);
268 input = devm_input_allocate_device(&pdev->dev);
272 input->keycodemax = iqs62x_keys->keycodemax;
273 input->keycode = iqs62x_keys->keycode;
274 input->keycodesize = sizeof(*iqs62x_keys->keycode);
276 input->name = iqs62x->dev_desc->dev_name;
277 input->id.bustype = BUS_I2C;
279 for (i = 0; i < iqs62x_keys->keycodemax; i++)
280 if (iqs62x_keys->keycode[i] != KEY_RESERVED)
281 input_set_capability(input, EV_KEY,
282 iqs62x_keys->keycode[i]);
284 for (i = 0; i < ARRAY_SIZE(iqs62x_keys->switches); i++)
285 if (iqs62x_keys->switches[i].enabled)
286 input_set_capability(input, EV_SW,
287 iqs62x_keys->switches[i].code);
289 iqs62x_keys->iqs62x = iqs62x;
290 iqs62x_keys->input = input;
292 ret = iqs62x_keys_init(iqs62x_keys);
294 dev_err(&pdev->dev, "Failed to initialize device: %d\n", ret);
298 ret = input_register_device(iqs62x_keys->input);
300 dev_err(&pdev->dev, "Failed to register device: %d\n", ret);
304 iqs62x_keys->notifier.notifier_call = iqs62x_keys_notifier;
305 ret = blocking_notifier_chain_register(&iqs62x_keys->iqs62x->nh,
306 &iqs62x_keys->notifier);
308 dev_err(&pdev->dev, "Failed to register notifier: %d\n", ret);
313 static void iqs62x_keys_remove(struct platform_device *pdev)
315 struct iqs62x_keys_private *iqs62x_keys = platform_get_drvdata(pdev);
318 ret = blocking_notifier_chain_unregister(&iqs62x_keys->iqs62x->nh,
319 &iqs62x_keys->notifier);
321 dev_err(&pdev->dev, "Failed to unregister notifier: %d\n", ret);
324 static struct platform_driver iqs62x_keys_platform_driver = {
326 .name = "iqs62x-keys",
328 .probe = iqs62x_keys_probe,
329 .remove_new = iqs62x_keys_remove,
331 module_platform_driver(iqs62x_keys_platform_driver);
333 MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
334 MODULE_DESCRIPTION("Azoteq IQS620A/621/622/624/625 Keys and Switches");
335 MODULE_LICENSE("GPL");
336 MODULE_ALIAS("platform:iqs62x-keys");