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