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