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