1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Azoteq IQS620A/621/622/624/625 Keys and Switches
*
* Copyright (C) 2019 Jeff LaBundy <jeff@labundy.com>
*/
#include <linux/device.h>
#include <linux/input.h>
#include <linux/kernel.h>
#include <linux/mfd/iqs62x.h>
#include <linux/module.h>
#include <linux/notifier.h>
#include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/regmap.h>
#include <linux/slab.h>
enum {
IQS62X_SW_HALL_N,
IQS62X_SW_HALL_S,
};
static const char * const iqs62x_switch_names[] = {
[IQS62X_SW_HALL_N] = "hall-switch-north",
[IQS62X_SW_HALL_S] = "hall-switch-south",
};
struct iqs62x_switch_desc {
enum iqs62x_event_flag flag;
unsigned int code;
bool enabled;
};
struct iqs62x_keys_private {
struct iqs62x_core *iqs62x;
struct input_dev *input;
struct notifier_block notifier;
struct iqs62x_switch_desc switches[ARRAY_SIZE(iqs62x_switch_names)];
unsigned int keycode[IQS62X_NUM_KEYS];
unsigned int keycodemax;
u8 interval;
};
static int iqs62x_keys_parse_prop(struct platform_device *pdev,
struct iqs62x_keys_private *iqs62x_keys)
{
struct fwnode_handle *child;
unsigned int val;
int ret, i;
ret = device_property_count_u32(&pdev->dev, "linux,keycodes");
if (ret > IQS62X_NUM_KEYS) {
dev_err(&pdev->dev, "Too many keycodes present\n");
return -EINVAL;
} else if (ret < 0) {
dev_err(&pdev->dev, "Failed to count keycodes: %d\n", ret);
return ret;
}
iqs62x_keys->keycodemax = ret;
ret = device_property_read_u32_array(&pdev->dev, "linux,keycodes",
iqs62x_keys->keycode,
iqs62x_keys->keycodemax);
if (ret) {
dev_err(&pdev->dev, "Failed to read keycodes: %d\n", ret);
return ret;
}
for (i = 0; i < ARRAY_SIZE(iqs62x_keys->switches); i++) {
child = device_get_named_child_node(&pdev->dev,
iqs62x_switch_names[i]);
if (!child)
continue;
ret = fwnode_property_read_u32(child, "linux,code", &val);
if (ret) {
dev_err(&pdev->dev, "Failed to read switch code: %d\n",
ret);
fwnode_handle_put(child);
return ret;
}
iqs62x_keys->switches[i].code = val;
iqs62x_keys->switches[i].enabled = true;
if (fwnode_property_present(child, "azoteq,use-prox"))
iqs62x_keys->switches[i].flag = (i == IQS62X_SW_HALL_N ?
IQS62X_EVENT_HALL_N_P :
IQS62X_EVENT_HALL_S_P);
else
iqs62x_keys->switches[i].flag = (i == IQS62X_SW_HALL_N ?
IQS62X_EVENT_HALL_N_T :
IQS62X_EVENT_HALL_S_T);
fwnode_handle_put(child);
}
return 0;
}
static int iqs62x_keys_init(struct iqs62x_keys_private *iqs62x_keys)
{
struct iqs62x_core *iqs62x = iqs62x_keys->iqs62x;
enum iqs62x_event_flag flag;
unsigned int event_reg, val;
unsigned int event_mask = 0;
int ret, i;
switch (iqs62x->dev_desc->prod_num) {
case IQS620_PROD_NUM:
case IQS621_PROD_NUM:
case IQS622_PROD_NUM:
event_reg = IQS620_GLBL_EVENT_MASK;
/*
* Discreet button, hysteresis and SAR UI flags represent keys
* and are unmasked if mapped to a valid keycode.
*/
for (i = 0; i < iqs62x_keys->keycodemax; i++) {
if (iqs62x_keys->keycode[i] == KEY_RESERVED)
continue;
if (iqs62x_events[i].reg == IQS62X_EVENT_PROX)
event_mask |= iqs62x->dev_desc->prox_mask;
else if (iqs62x_events[i].reg == IQS62X_EVENT_HYST)
event_mask |= (iqs62x->dev_desc->hyst_mask |
iqs62x->dev_desc->sar_mask);
}
ret = regmap_read(iqs62x->regmap, iqs62x->dev_desc->hall_flags,
&val);
if (ret)
return ret;
/*
* Hall UI flags represent switches and are unmasked if their
* corresponding child nodes are present.
*/
for (i = 0; i < ARRAY_SIZE(iqs62x_keys->switches); i++) {
if (!(iqs62x_keys->switches[i].enabled))
continue;
flag = iqs62x_keys->switches[i].flag;
if (iqs62x_events[flag].reg != IQS62X_EVENT_HALL)
continue;
event_mask |= iqs62x->dev_desc->hall_mask;
input_report_switch(iqs62x_keys->input,
iqs62x_keys->switches[i].code,
(val & iqs62x_events[flag].mask) ==
iqs62x_events[flag].val);
}
input_sync(iqs62x_keys->input);
break;
case IQS624_PROD_NUM:
event_reg = IQS624_HALL_UI;
/*
* Interval change events represent keys and are unmasked if
* either wheel movement flag is mapped to a valid keycode.
*/
if (iqs62x_keys->keycode[IQS62X_EVENT_WHEEL_UP] != KEY_RESERVED)
event_mask |= IQS624_HALL_UI_INT_EVENT;
if (iqs62x_keys->keycode[IQS62X_EVENT_WHEEL_DN] != KEY_RESERVED)
event_mask |= IQS624_HALL_UI_INT_EVENT;
ret = regmap_read(iqs62x->regmap, iqs62x->dev_desc->interval,
&val);
if (ret)
return ret;
iqs62x_keys->interval = val;
break;
default:
return 0;
}
return regmap_update_bits(iqs62x->regmap, event_reg, event_mask, 0);
}
static int iqs62x_keys_notifier(struct notifier_block *notifier,
unsigned long event_flags, void *context)
{
struct iqs62x_event_data *event_data = context;
struct iqs62x_keys_private *iqs62x_keys;
int ret, i;
iqs62x_keys = container_of(notifier, struct iqs62x_keys_private,
notifier);
if (event_flags & BIT(IQS62X_EVENT_SYS_RESET)) {
ret = iqs62x_keys_init(iqs62x_keys);
if (ret) {
dev_err(iqs62x_keys->input->dev.parent,
"Failed to re-initialize device: %d\n", ret);
return NOTIFY_BAD;
}
return NOTIFY_OK;
}
for (i = 0; i < iqs62x_keys->keycodemax; i++) {
if (iqs62x_events[i].reg == IQS62X_EVENT_WHEEL &&
event_data->interval == iqs62x_keys->interval)
continue;
input_report_key(iqs62x_keys->input, iqs62x_keys->keycode[i],
event_flags & BIT(i));
}
for (i = 0; i < ARRAY_SIZE(iqs62x_keys->switches); i++)
if (iqs62x_keys->switches[i].enabled)
input_report_switch(iqs62x_keys->input,
iqs62x_keys->switches[i].code,
event_flags &
BIT(iqs62x_keys->switches[i].flag));
input_sync(iqs62x_keys->input);
if (event_data->interval == iqs62x_keys->interval)
return NOTIFY_OK;
/*
* Each frame contains at most one wheel event (up or down), in which
* case a complementary release cycle is emulated.
*/
if (event_flags & BIT(IQS62X_EVENT_WHEEL_UP)) {
input_report_key(iqs62x_keys->input,
iqs62x_keys->keycode[IQS62X_EVENT_WHEEL_UP],
0);
input_sync(iqs62x_keys->input);
} else if (event_flags & BIT(IQS62X_EVENT_WHEEL_DN)) {
input_report_key(iqs62x_keys->input,
iqs62x_keys->keycode[IQS62X_EVENT_WHEEL_DN],
0);
input_sync(iqs62x_keys->input);
}
iqs62x_keys->interval = event_data->interval;
return NOTIFY_OK;
}
static int iqs62x_keys_probe(struct platform_device *pdev)
{
struct iqs62x_core *iqs62x = dev_get_drvdata(pdev->dev.parent);
struct iqs62x_keys_private *iqs62x_keys;
struct input_dev *input;
int ret, i;
iqs62x_keys = devm_kzalloc(&pdev->dev, sizeof(*iqs62x_keys),
GFP_KERNEL);
if (!iqs62x_keys)
return -ENOMEM;
platform_set_drvdata(pdev, iqs62x_keys);
ret = iqs62x_keys_parse_prop(pdev, iqs62x_keys);
if (ret)
return ret;
input = devm_input_allocate_device(&pdev->dev);
if (!input)
return -ENOMEM;
input->keycodemax = iqs62x_keys->keycodemax;
input->keycode = iqs62x_keys->keycode;
input->keycodesize = sizeof(*iqs62x_keys->keycode);
input->name = iqs62x->dev_desc->dev_name;
input->id.bustype = BUS_I2C;
for (i = 0; i < iqs62x_keys->keycodemax; i++)
if (iqs62x_keys->keycode[i] != KEY_RESERVED)
input_set_capability(input, EV_KEY,
iqs62x_keys->keycode[i]);
for (i = 0; i < ARRAY_SIZE(iqs62x_keys->switches); i++)
if (iqs62x_keys->switches[i].enabled)
input_set_capability(input, EV_SW,
iqs62x_keys->switches[i].code);
iqs62x_keys->iqs62x = iqs62x;
iqs62x_keys->input = input;
ret = iqs62x_keys_init(iqs62x_keys);
if (ret) {
dev_err(&pdev->dev, "Failed to initialize device: %d\n", ret);
return ret;
}
ret = input_register_device(iqs62x_keys->input);
if (ret) {
dev_err(&pdev->dev, "Failed to register device: %d\n", ret);
return ret;
}
iqs62x_keys->notifier.notifier_call = iqs62x_keys_notifier;
ret = blocking_notifier_chain_register(&iqs62x_keys->iqs62x->nh,
&iqs62x_keys->notifier);
if (ret)
dev_err(&pdev->dev, "Failed to register notifier: %d\n", ret);
return ret;
}
static void iqs62x_keys_remove(struct platform_device *pdev)
{
struct iqs62x_keys_private *iqs62x_keys = platform_get_drvdata(pdev);
int ret;
ret = blocking_notifier_chain_unregister(&iqs62x_keys->iqs62x->nh,
&iqs62x_keys->notifier);
if (ret)
dev_err(&pdev->dev, "Failed to unregister notifier: %d\n", ret);
}
static struct platform_driver iqs62x_keys_platform_driver = {
.driver = {
.name = "iqs62x-keys",
},
.probe = iqs62x_keys_probe,
.remove_new = iqs62x_keys_remove,
};
module_platform_driver(iqs62x_keys_platform_driver);
MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
MODULE_DESCRIPTION("Azoteq IQS620A/621/622/624/625 Keys and Switches");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:iqs62x-keys");
|