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