Merge branch 'for-6.9/amd-sfh' into for-linus
[sfrench/cifs-2.6.git] / drivers / input / keyboard / cap11xx.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Input driver for Microchip CAP11xx based capacitive touch sensors
4  *
5  * (c) 2014 Daniel Mack <linux@zonque.org>
6  */
7
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/interrupt.h>
11 #include <linux/input.h>
12 #include <linux/leds.h>
13 #include <linux/of.h>
14 #include <linux/regmap.h>
15 #include <linux/i2c.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/bitfield.h>
18
19 #define CAP11XX_REG_MAIN_CONTROL        0x00
20 #define CAP11XX_REG_MAIN_CONTROL_GAIN_SHIFT     (6)
21 #define CAP11XX_REG_MAIN_CONTROL_GAIN_MASK      (0xc0)
22 #define CAP11XX_REG_MAIN_CONTROL_DLSEEP         BIT(4)
23 #define CAP11XX_REG_GENERAL_STATUS      0x02
24 #define CAP11XX_REG_SENSOR_INPUT        0x03
25 #define CAP11XX_REG_NOISE_FLAG_STATUS   0x0a
26 #define CAP11XX_REG_SENOR_DELTA(X)      (0x10 + (X))
27 #define CAP11XX_REG_SENSITIVITY_CONTROL 0x1f
28 #define CAP11XX_REG_SENSITIVITY_CONTROL_DELTA_SENSE_MASK        0x70
29 #define CAP11XX_REG_CONFIG              0x20
30 #define CAP11XX_REG_SENSOR_ENABLE       0x21
31 #define CAP11XX_REG_SENSOR_CONFIG       0x22
32 #define CAP11XX_REG_SENSOR_CONFIG2      0x23
33 #define CAP11XX_REG_SAMPLING_CONFIG     0x24
34 #define CAP11XX_REG_CALIBRATION         0x26
35 #define CAP11XX_REG_INT_ENABLE          0x27
36 #define CAP11XX_REG_REPEAT_RATE         0x28
37 #define CAP11XX_REG_SIGNAL_GUARD_ENABLE 0x29
38 #define CAP11XX_REG_MT_CONFIG           0x2a
39 #define CAP11XX_REG_MT_PATTERN_CONFIG   0x2b
40 #define CAP11XX_REG_MT_PATTERN          0x2d
41 #define CAP11XX_REG_RECALIB_CONFIG      0x2f
42 #define CAP11XX_REG_SENSOR_THRESH(X)    (0x30 + (X))
43 #define CAP11XX_REG_SENSOR_NOISE_THRESH 0x38
44 #define CAP11XX_REG_STANDBY_CHANNEL     0x40
45 #define CAP11XX_REG_STANDBY_CONFIG      0x41
46 #define CAP11XX_REG_STANDBY_SENSITIVITY 0x42
47 #define CAP11XX_REG_STANDBY_THRESH      0x43
48 #define CAP11XX_REG_CONFIG2             0x44
49 #define CAP11XX_REG_CONFIG2_ALT_POL     BIT(6)
50 #define CAP11XX_REG_SENSOR_BASE_CNT(X)  (0x50 + (X))
51 #define CAP11XX_REG_LED_POLARITY        0x73
52 #define CAP11XX_REG_LED_OUTPUT_CONTROL  0x74
53 #define CAP11XX_REG_CALIB_SENSITIVITY_CONFIG    0x80
54 #define CAP11XX_REG_CALIB_SENSITIVITY_CONFIG2   0x81
55
56 #define CAP11XX_REG_LED_DUTY_CYCLE_1    0x90
57 #define CAP11XX_REG_LED_DUTY_CYCLE_2    0x91
58 #define CAP11XX_REG_LED_DUTY_CYCLE_3    0x92
59 #define CAP11XX_REG_LED_DUTY_CYCLE_4    0x93
60
61 #define CAP11XX_REG_LED_DUTY_MIN_MASK   (0x0f)
62 #define CAP11XX_REG_LED_DUTY_MIN_MASK_SHIFT     (0)
63 #define CAP11XX_REG_LED_DUTY_MAX_MASK   (0xf0)
64 #define CAP11XX_REG_LED_DUTY_MAX_MASK_SHIFT     (4)
65 #define CAP11XX_REG_LED_DUTY_MAX_VALUE  (15)
66
67 #define CAP11XX_REG_SENSOR_CALIB        (0xb1 + (X))
68 #define CAP11XX_REG_SENSOR_CALIB_LSB1   0xb9
69 #define CAP11XX_REG_SENSOR_CALIB_LSB2   0xba
70 #define CAP11XX_REG_PRODUCT_ID          0xfd
71 #define CAP11XX_REG_MANUFACTURER_ID     0xfe
72 #define CAP11XX_REG_REVISION            0xff
73
74 #define CAP11XX_MANUFACTURER_ID 0x5d
75
76 #ifdef CONFIG_LEDS_CLASS
77 struct cap11xx_led {
78         struct cap11xx_priv *priv;
79         struct led_classdev cdev;
80         u32 reg;
81 };
82 #endif
83
84 struct cap11xx_priv {
85         struct regmap *regmap;
86         struct device *dev;
87         struct input_dev *idev;
88         const struct cap11xx_hw_model *model;
89         u8 id;
90
91         struct cap11xx_led *leds;
92         int num_leds;
93
94         /* config */
95         u8 analog_gain;
96         u8 sensitivity_delta_sense;
97         u8 signal_guard_inputs_mask;
98         u32 thresholds[8];
99         u32 calib_sensitivities[8];
100         u32 keycodes[];
101 };
102
103 struct cap11xx_hw_model {
104         u8 product_id;
105         unsigned int num_channels;
106         unsigned int num_leds;
107         bool no_gain;
108 };
109
110 enum {
111         CAP1106,
112         CAP1126,
113         CAP1188,
114         CAP1203,
115         CAP1206,
116         CAP1293,
117         CAP1298
118 };
119
120 static const struct cap11xx_hw_model cap11xx_devices[] = {
121         [CAP1106] = { .product_id = 0x55, .num_channels = 6, .num_leds = 0, .no_gain = false },
122         [CAP1126] = { .product_id = 0x53, .num_channels = 6, .num_leds = 2, .no_gain = false },
123         [CAP1188] = { .product_id = 0x50, .num_channels = 8, .num_leds = 8, .no_gain = false },
124         [CAP1203] = { .product_id = 0x6d, .num_channels = 3, .num_leds = 0, .no_gain = true },
125         [CAP1206] = { .product_id = 0x67, .num_channels = 6, .num_leds = 0, .no_gain = true },
126         [CAP1293] = { .product_id = 0x6f, .num_channels = 3, .num_leds = 0, .no_gain = false },
127         [CAP1298] = { .product_id = 0x71, .num_channels = 8, .num_leds = 0, .no_gain = false },
128 };
129
130 static const struct reg_default cap11xx_reg_defaults[] = {
131         { CAP11XX_REG_MAIN_CONTROL,             0x00 },
132         { CAP11XX_REG_GENERAL_STATUS,           0x00 },
133         { CAP11XX_REG_SENSOR_INPUT,             0x00 },
134         { CAP11XX_REG_NOISE_FLAG_STATUS,        0x00 },
135         { CAP11XX_REG_SENSITIVITY_CONTROL,      0x2f },
136         { CAP11XX_REG_CONFIG,                   0x20 },
137         { CAP11XX_REG_SENSOR_ENABLE,            0x3f },
138         { CAP11XX_REG_SENSOR_CONFIG,            0xa4 },
139         { CAP11XX_REG_SENSOR_CONFIG2,           0x07 },
140         { CAP11XX_REG_SAMPLING_CONFIG,          0x39 },
141         { CAP11XX_REG_CALIBRATION,              0x00 },
142         { CAP11XX_REG_INT_ENABLE,               0x3f },
143         { CAP11XX_REG_REPEAT_RATE,              0x3f },
144         { CAP11XX_REG_MT_CONFIG,                0x80 },
145         { CAP11XX_REG_MT_PATTERN_CONFIG,        0x00 },
146         { CAP11XX_REG_MT_PATTERN,               0x3f },
147         { CAP11XX_REG_RECALIB_CONFIG,           0x8a },
148         { CAP11XX_REG_SENSOR_THRESH(0),         0x40 },
149         { CAP11XX_REG_SENSOR_THRESH(1),         0x40 },
150         { CAP11XX_REG_SENSOR_THRESH(2),         0x40 },
151         { CAP11XX_REG_SENSOR_THRESH(3),         0x40 },
152         { CAP11XX_REG_SENSOR_THRESH(4),         0x40 },
153         { CAP11XX_REG_SENSOR_THRESH(5),         0x40 },
154         { CAP11XX_REG_SENSOR_NOISE_THRESH,      0x01 },
155         { CAP11XX_REG_STANDBY_CHANNEL,          0x00 },
156         { CAP11XX_REG_STANDBY_CONFIG,           0x39 },
157         { CAP11XX_REG_STANDBY_SENSITIVITY,      0x02 },
158         { CAP11XX_REG_STANDBY_THRESH,           0x40 },
159         { CAP11XX_REG_CONFIG2,                  0x40 },
160         { CAP11XX_REG_LED_POLARITY,             0x00 },
161         { CAP11XX_REG_SENSOR_CALIB_LSB1,        0x00 },
162         { CAP11XX_REG_SENSOR_CALIB_LSB2,        0x00 },
163 };
164
165 static bool cap11xx_volatile_reg(struct device *dev, unsigned int reg)
166 {
167         switch (reg) {
168         case CAP11XX_REG_MAIN_CONTROL:
169         case CAP11XX_REG_SENSOR_INPUT:
170         case CAP11XX_REG_SENOR_DELTA(0):
171         case CAP11XX_REG_SENOR_DELTA(1):
172         case CAP11XX_REG_SENOR_DELTA(2):
173         case CAP11XX_REG_SENOR_DELTA(3):
174         case CAP11XX_REG_SENOR_DELTA(4):
175         case CAP11XX_REG_SENOR_DELTA(5):
176                 return true;
177         }
178
179         return false;
180 }
181
182 static const struct regmap_config cap11xx_regmap_config = {
183         .reg_bits = 8,
184         .val_bits = 8,
185
186         .max_register = CAP11XX_REG_REVISION,
187         .reg_defaults = cap11xx_reg_defaults,
188
189         .num_reg_defaults = ARRAY_SIZE(cap11xx_reg_defaults),
190         .cache_type = REGCACHE_MAPLE,
191         .volatile_reg = cap11xx_volatile_reg,
192 };
193
194 static int cap11xx_write_calib_sens_config_1(struct cap11xx_priv *priv)
195 {
196         return regmap_write(priv->regmap,
197                             CAP11XX_REG_CALIB_SENSITIVITY_CONFIG,
198                             (priv->calib_sensitivities[3] << 6) |
199                             (priv->calib_sensitivities[2] << 4) |
200                             (priv->calib_sensitivities[1] << 2) |
201                             priv->calib_sensitivities[0]);
202 }
203
204 static int cap11xx_write_calib_sens_config_2(struct cap11xx_priv *priv)
205 {
206         return regmap_write(priv->regmap,
207                             CAP11XX_REG_CALIB_SENSITIVITY_CONFIG2,
208                             (priv->calib_sensitivities[7] << 6) |
209                             (priv->calib_sensitivities[6] << 4) |
210                             (priv->calib_sensitivities[5] << 2) |
211                             priv->calib_sensitivities[4]);
212 }
213
214 static int cap11xx_init_keys(struct cap11xx_priv *priv)
215 {
216         struct device_node *node = priv->dev->of_node;
217         struct device *dev = priv->dev;
218         int i, error;
219         u32 u32_val;
220
221         if (!node) {
222                 dev_err(dev, "Corresponding DT entry is not available\n");
223                 return -ENODEV;
224         }
225
226         if (!of_property_read_u32(node, "microchip,sensor-gain", &u32_val)) {
227                 if (priv->model->no_gain) {
228                         dev_warn(dev,
229                                  "This model doesn't support 'sensor-gain'\n");
230                 } else if (is_power_of_2(u32_val) && u32_val <= 8) {
231                         priv->analog_gain = (u8)ilog2(u32_val);
232
233                         error = regmap_update_bits(priv->regmap,
234                                 CAP11XX_REG_MAIN_CONTROL,
235                                 CAP11XX_REG_MAIN_CONTROL_GAIN_MASK,
236                                 priv->analog_gain << CAP11XX_REG_MAIN_CONTROL_GAIN_SHIFT);
237                         if (error)
238                                 return error;
239                 } else {
240                         dev_err(dev, "Invalid sensor-gain value %u\n", u32_val);
241                         return -EINVAL;
242                 }
243         }
244
245         if (of_property_read_bool(node, "microchip,irq-active-high")) {
246                 if (priv->id == CAP1106 ||
247                     priv->id == CAP1126 ||
248                     priv->id == CAP1188) {
249                         error = regmap_update_bits(priv->regmap,
250                                                    CAP11XX_REG_CONFIG2,
251                                                    CAP11XX_REG_CONFIG2_ALT_POL,
252                                                    0);
253                         if (error)
254                                 return error;
255                 } else {
256                         dev_warn(dev,
257                                  "This model doesn't support 'irq-active-high'\n");
258                 }
259         }
260
261         if (!of_property_read_u32(node, "microchip,sensitivity-delta-sense", &u32_val)) {
262                 if (!is_power_of_2(u32_val) || u32_val > 128) {
263                         dev_err(dev, "Invalid sensitivity-delta-sense value %u\n", u32_val);
264                         return -EINVAL;
265                 }
266
267                 priv->sensitivity_delta_sense = (u8)ilog2(u32_val);
268                 u32_val = ~(FIELD_PREP(CAP11XX_REG_SENSITIVITY_CONTROL_DELTA_SENSE_MASK,
269                                         priv->sensitivity_delta_sense));
270
271                 error = regmap_update_bits(priv->regmap,
272                                            CAP11XX_REG_SENSITIVITY_CONTROL,
273                                            CAP11XX_REG_SENSITIVITY_CONTROL_DELTA_SENSE_MASK,
274                                            u32_val);
275                 if (error)
276                         return error;
277         }
278
279         if (!of_property_read_u32_array(node, "microchip,input-threshold",
280                                         priv->thresholds, priv->model->num_channels)) {
281                 for (i = 0; i < priv->model->num_channels; i++) {
282                         if (priv->thresholds[i] > 127) {
283                                 dev_err(dev, "Invalid input-threshold value %u\n",
284                                         priv->thresholds[i]);
285                                 return -EINVAL;
286                         }
287
288                         error = regmap_write(priv->regmap,
289                                              CAP11XX_REG_SENSOR_THRESH(i),
290                                              priv->thresholds[i]);
291                         if (error)
292                                 return error;
293                 }
294         }
295
296         if (!of_property_read_u32_array(node, "microchip,calib-sensitivity",
297                                         priv->calib_sensitivities,
298                                         priv->model->num_channels)) {
299                 if (priv->id == CAP1293 || priv->id == CAP1298) {
300                         for (i = 0; i < priv->model->num_channels; i++) {
301                                 if (!is_power_of_2(priv->calib_sensitivities[i]) ||
302                                     priv->calib_sensitivities[i] > 4) {
303                                         dev_err(dev, "Invalid calib-sensitivity value %u\n",
304                                                 priv->calib_sensitivities[i]);
305                                         return -EINVAL;
306                                 }
307                                 priv->calib_sensitivities[i] = ilog2(priv->calib_sensitivities[i]);
308                         }
309
310                         error = cap11xx_write_calib_sens_config_1(priv);
311                         if (error)
312                                 return error;
313
314                         if (priv->id == CAP1298) {
315                                 error = cap11xx_write_calib_sens_config_2(priv);
316                                 if (error)
317                                         return error;
318                         }
319                 } else {
320                         dev_warn(dev,
321                                  "This model doesn't support 'calib-sensitivity'\n");
322                 }
323         }
324
325         for (i = 0; i < priv->model->num_channels; i++) {
326                 if (!of_property_read_u32_index(node, "microchip,signal-guard",
327                                                 i, &u32_val)) {
328                         if (u32_val > 1)
329                                 return -EINVAL;
330                         if (u32_val)
331                                 priv->signal_guard_inputs_mask |= 0x01 << i;
332                 }
333         }
334
335         if (priv->signal_guard_inputs_mask) {
336                 if (priv->id == CAP1293 || priv->id == CAP1298) {
337                         error = regmap_write(priv->regmap,
338                                              CAP11XX_REG_SIGNAL_GUARD_ENABLE,
339                                              priv->signal_guard_inputs_mask);
340                         if (error)
341                                 return error;
342                 } else {
343                         dev_warn(dev,
344                                  "This model doesn't support 'signal-guard'\n");
345                 }
346         }
347
348         /* Provide some useful defaults */
349         for (i = 0; i < priv->model->num_channels; i++)
350                 priv->keycodes[i] = KEY_A + i;
351
352         of_property_read_u32_array(node, "linux,keycodes",
353                                    priv->keycodes, priv->model->num_channels);
354
355         /* Disable autorepeat. The Linux input system has its own handling. */
356         error = regmap_write(priv->regmap, CAP11XX_REG_REPEAT_RATE, 0);
357         if (error)
358                 return error;
359
360         return 0;
361 }
362
363 static irqreturn_t cap11xx_thread_func(int irq_num, void *data)
364 {
365         struct cap11xx_priv *priv = data;
366         unsigned int status;
367         int ret, i;
368
369         /*
370          * Deassert interrupt. This needs to be done before reading the status
371          * registers, which will not carry valid values otherwise.
372          */
373         ret = regmap_update_bits(priv->regmap, CAP11XX_REG_MAIN_CONTROL, 1, 0);
374         if (ret < 0)
375                 goto out;
376
377         ret = regmap_read(priv->regmap, CAP11XX_REG_SENSOR_INPUT, &status);
378         if (ret < 0)
379                 goto out;
380
381         for (i = 0; i < priv->idev->keycodemax; i++)
382                 input_report_key(priv->idev, priv->keycodes[i],
383                                  status & (1 << i));
384
385         input_sync(priv->idev);
386
387 out:
388         return IRQ_HANDLED;
389 }
390
391 static int cap11xx_set_sleep(struct cap11xx_priv *priv, bool sleep)
392 {
393         /*
394          * DLSEEP mode will turn off all LEDS, prevent this
395          */
396         if (IS_ENABLED(CONFIG_LEDS_CLASS) && priv->num_leds)
397                 return 0;
398
399         return regmap_update_bits(priv->regmap, CAP11XX_REG_MAIN_CONTROL,
400                                   CAP11XX_REG_MAIN_CONTROL_DLSEEP,
401                                   sleep ? CAP11XX_REG_MAIN_CONTROL_DLSEEP : 0);
402 }
403
404 static int cap11xx_input_open(struct input_dev *idev)
405 {
406         struct cap11xx_priv *priv = input_get_drvdata(idev);
407
408         return cap11xx_set_sleep(priv, false);
409 }
410
411 static void cap11xx_input_close(struct input_dev *idev)
412 {
413         struct cap11xx_priv *priv = input_get_drvdata(idev);
414
415         cap11xx_set_sleep(priv, true);
416 }
417
418 #ifdef CONFIG_LEDS_CLASS
419 static int cap11xx_led_set(struct led_classdev *cdev,
420                             enum led_brightness value)
421 {
422         struct cap11xx_led *led = container_of(cdev, struct cap11xx_led, cdev);
423         struct cap11xx_priv *priv = led->priv;
424
425         /*
426          * All LEDs share the same duty cycle as this is a HW
427          * limitation. Brightness levels per LED are either
428          * 0 (OFF) and 1 (ON).
429          */
430         return regmap_update_bits(priv->regmap,
431                                   CAP11XX_REG_LED_OUTPUT_CONTROL,
432                                   BIT(led->reg),
433                                   value ? BIT(led->reg) : 0);
434 }
435
436 static int cap11xx_init_leds(struct device *dev,
437                              struct cap11xx_priv *priv, int num_leds)
438 {
439         struct device_node *node = dev->of_node, *child;
440         struct cap11xx_led *led;
441         int cnt = of_get_child_count(node);
442         int error;
443
444         if (!num_leds || !cnt)
445                 return 0;
446
447         if (cnt > num_leds)
448                 return -EINVAL;
449
450         led = devm_kcalloc(dev, cnt, sizeof(struct cap11xx_led), GFP_KERNEL);
451         if (!led)
452                 return -ENOMEM;
453
454         priv->leds = led;
455
456         error = regmap_update_bits(priv->regmap,
457                                 CAP11XX_REG_LED_OUTPUT_CONTROL, 0xff, 0);
458         if (error)
459                 return error;
460
461         error = regmap_update_bits(priv->regmap, CAP11XX_REG_LED_DUTY_CYCLE_4,
462                                 CAP11XX_REG_LED_DUTY_MAX_MASK,
463                                 CAP11XX_REG_LED_DUTY_MAX_VALUE <<
464                                 CAP11XX_REG_LED_DUTY_MAX_MASK_SHIFT);
465         if (error)
466                 return error;
467
468         for_each_child_of_node(node, child) {
469                 u32 reg;
470
471                 led->cdev.name =
472                         of_get_property(child, "label", NULL) ? : child->name;
473                 led->cdev.default_trigger =
474                         of_get_property(child, "linux,default-trigger", NULL);
475                 led->cdev.flags = 0;
476                 led->cdev.brightness_set_blocking = cap11xx_led_set;
477                 led->cdev.max_brightness = 1;
478                 led->cdev.brightness = LED_OFF;
479
480                 error = of_property_read_u32(child, "reg", &reg);
481                 if (error != 0 || reg >= num_leds) {
482                         of_node_put(child);
483                         return -EINVAL;
484                 }
485
486                 led->reg = reg;
487                 led->priv = priv;
488
489                 error = devm_led_classdev_register(dev, &led->cdev);
490                 if (error) {
491                         of_node_put(child);
492                         return error;
493                 }
494
495                 priv->num_leds++;
496                 led++;
497         }
498
499         return 0;
500 }
501 #else
502 static int cap11xx_init_leds(struct device *dev,
503                              struct cap11xx_priv *priv, int num_leds)
504 {
505         return 0;
506 }
507 #endif
508
509 static int cap11xx_i2c_probe(struct i2c_client *i2c_client)
510 {
511         const struct i2c_device_id *id = i2c_client_get_device_id(i2c_client);
512         struct device *dev = &i2c_client->dev;
513         struct cap11xx_priv *priv;
514         const struct cap11xx_hw_model *cap;
515         int i, error;
516         unsigned int val, rev;
517
518         if (id->driver_data >= ARRAY_SIZE(cap11xx_devices)) {
519                 dev_err(dev, "Invalid device ID %lu\n", id->driver_data);
520                 return -EINVAL;
521         }
522
523         cap = &cap11xx_devices[id->driver_data];
524         if (!cap || !cap->num_channels) {
525                 dev_err(dev, "Invalid device configuration\n");
526                 return -EINVAL;
527         }
528
529         priv = devm_kzalloc(dev,
530                             struct_size(priv, keycodes, cap->num_channels),
531                             GFP_KERNEL);
532         if (!priv)
533                 return -ENOMEM;
534
535         priv->dev = dev;
536
537         priv->regmap = devm_regmap_init_i2c(i2c_client, &cap11xx_regmap_config);
538         if (IS_ERR(priv->regmap))
539                 return PTR_ERR(priv->regmap);
540
541         error = regmap_read(priv->regmap, CAP11XX_REG_PRODUCT_ID, &val);
542         if (error)
543                 return error;
544
545         if (val != cap->product_id) {
546                 dev_err(dev, "Product ID: Got 0x%02x, expected 0x%02x\n",
547                         val, cap->product_id);
548                 return -ENXIO;
549         }
550
551         error = regmap_read(priv->regmap, CAP11XX_REG_MANUFACTURER_ID, &val);
552         if (error)
553                 return error;
554
555         if (val != CAP11XX_MANUFACTURER_ID) {
556                 dev_err(dev, "Manufacturer ID: Got 0x%02x, expected 0x%02x\n",
557                         val, CAP11XX_MANUFACTURER_ID);
558                 return -ENXIO;
559         }
560
561         error = regmap_read(priv->regmap, CAP11XX_REG_REVISION, &rev);
562         if (error < 0)
563                 return error;
564
565         dev_info(dev, "CAP11XX detected, model %s, revision 0x%02x\n",
566                          id->name, rev);
567
568         priv->model = cap;
569         priv->id = id->driver_data;
570
571         dev_info(dev, "CAP11XX device detected, model %s, revision 0x%02x\n",
572                  id->name, rev);
573
574         error = cap11xx_init_keys(priv);
575         if (error)
576                 return error;
577
578         priv->idev = devm_input_allocate_device(dev);
579         if (!priv->idev)
580                 return -ENOMEM;
581
582         priv->idev->name = "CAP11XX capacitive touch sensor";
583         priv->idev->id.bustype = BUS_I2C;
584         priv->idev->evbit[0] = BIT_MASK(EV_KEY);
585
586         if (of_property_read_bool(dev->of_node, "autorepeat"))
587                 __set_bit(EV_REP, priv->idev->evbit);
588
589         for (i = 0; i < cap->num_channels; i++)
590                 __set_bit(priv->keycodes[i], priv->idev->keybit);
591
592         __clear_bit(KEY_RESERVED, priv->idev->keybit);
593
594         priv->idev->keycode = priv->keycodes;
595         priv->idev->keycodesize = sizeof(priv->keycodes[0]);
596         priv->idev->keycodemax = cap->num_channels;
597
598         priv->idev->id.vendor = CAP11XX_MANUFACTURER_ID;
599         priv->idev->id.product = cap->product_id;
600         priv->idev->id.version = rev;
601
602         priv->idev->open = cap11xx_input_open;
603         priv->idev->close = cap11xx_input_close;
604
605         error = cap11xx_init_leds(dev, priv, cap->num_leds);
606         if (error)
607                 return error;
608
609         input_set_drvdata(priv->idev, priv);
610
611         /*
612          * Put the device in deep sleep mode for now.
613          * ->open() will bring it back once the it is actually needed.
614          */
615         cap11xx_set_sleep(priv, true);
616
617         error = input_register_device(priv->idev);
618         if (error)
619                 return error;
620
621         error = devm_request_threaded_irq(dev, i2c_client->irq,
622                                           NULL, cap11xx_thread_func,
623                                           IRQF_ONESHOT, dev_name(dev), priv);
624         if (error)
625                 return error;
626
627         return 0;
628 }
629
630 static const struct of_device_id cap11xx_dt_ids[] = {
631         { .compatible = "microchip,cap1106", },
632         { .compatible = "microchip,cap1126", },
633         { .compatible = "microchip,cap1188", },
634         { .compatible = "microchip,cap1203", },
635         { .compatible = "microchip,cap1206", },
636         { .compatible = "microchip,cap1293", },
637         { .compatible = "microchip,cap1298", },
638         {}
639 };
640 MODULE_DEVICE_TABLE(of, cap11xx_dt_ids);
641
642 static const struct i2c_device_id cap11xx_i2c_ids[] = {
643         { "cap1106", CAP1106 },
644         { "cap1126", CAP1126 },
645         { "cap1188", CAP1188 },
646         { "cap1203", CAP1203 },
647         { "cap1206", CAP1206 },
648         { "cap1293", CAP1293 },
649         { "cap1298", CAP1298 },
650         {}
651 };
652 MODULE_DEVICE_TABLE(i2c, cap11xx_i2c_ids);
653
654 static struct i2c_driver cap11xx_i2c_driver = {
655         .driver = {
656                 .name   = "cap11xx",
657                 .of_match_table = cap11xx_dt_ids,
658         },
659         .id_table       = cap11xx_i2c_ids,
660         .probe          = cap11xx_i2c_probe,
661 };
662
663 module_i2c_driver(cap11xx_i2c_driver);
664
665 MODULE_DESCRIPTION("Microchip CAP11XX driver");
666 MODULE_AUTHOR("Daniel Mack <linux@zonque.org>");
667 MODULE_LICENSE("GPL v2");