xref: /openbmc/linux/drivers/input/keyboard/cap11xx.c (revision c819e2cf)
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/of_irq.h>
16 #include <linux/regmap.h>
17 #include <linux/i2c.h>
18 #include <linux/gpio/consumer.h>
19 
20 #define CAP11XX_REG_MAIN_CONTROL	0x00
21 #define CAP11XX_REG_MAIN_CONTROL_GAIN_SHIFT	(6)
22 #define CAP11XX_REG_MAIN_CONTROL_GAIN_MASK	(0xc0)
23 #define CAP11XX_REG_MAIN_CONTROL_DLSEEP		BIT(4)
24 #define CAP11XX_REG_GENERAL_STATUS	0x02
25 #define CAP11XX_REG_SENSOR_INPUT	0x03
26 #define CAP11XX_REG_NOISE_FLAG_STATUS	0x0a
27 #define CAP11XX_REG_SENOR_DELTA(X)	(0x10 + (X))
28 #define CAP11XX_REG_SENSITIVITY_CONTROL	0x1f
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_MT_CONFIG		0x2a
38 #define CAP11XX_REG_MT_PATTERN_CONFIG	0x2b
39 #define CAP11XX_REG_MT_PATTERN		0x2d
40 #define CAP11XX_REG_RECALIB_CONFIG	0x2f
41 #define CAP11XX_REG_SENSOR_THRESH(X)	(0x30 + (X))
42 #define CAP11XX_REG_SENSOR_NOISE_THRESH	0x38
43 #define CAP11XX_REG_STANDBY_CHANNEL	0x40
44 #define CAP11XX_REG_STANDBY_CONFIG	0x41
45 #define CAP11XX_REG_STANDBY_SENSITIVITY	0x42
46 #define CAP11XX_REG_STANDBY_THRESH	0x43
47 #define CAP11XX_REG_CONFIG2		0x44
48 #define CAP11XX_REG_CONFIG2_ALT_POL	BIT(6)
49 #define CAP11XX_REG_SENSOR_BASE_CNT(X)	(0x50 + (X))
50 #define CAP11XX_REG_SENSOR_CALIB	(0xb1 + (X))
51 #define CAP11XX_REG_SENSOR_CALIB_LSB1	0xb9
52 #define CAP11XX_REG_SENSOR_CALIB_LSB2	0xba
53 #define CAP11XX_REG_PRODUCT_ID		0xfd
54 #define CAP11XX_REG_MANUFACTURER_ID	0xfe
55 #define CAP11XX_REG_REVISION		0xff
56 
57 #define CAP11XX_MANUFACTURER_ID	0x5d
58 
59 struct cap11xx_priv {
60 	struct regmap *regmap;
61 	struct input_dev *idev;
62 
63 	/* config */
64 	u32 keycodes[];
65 };
66 
67 struct cap11xx_hw_model {
68 	u8 product_id;
69 	unsigned int num_channels;
70 };
71 
72 enum {
73 	CAP1106,
74 	CAP1126,
75 	CAP1188,
76 };
77 
78 static const struct cap11xx_hw_model cap11xx_devices[] = {
79 	[CAP1106] = { .product_id = 0x55, .num_channels = 6 },
80 	[CAP1126] = { .product_id = 0x53, .num_channels = 6 },
81 	[CAP1188] = { .product_id = 0x50, .num_channels = 8 },
82 };
83 
84 static const struct reg_default cap11xx_reg_defaults[] = {
85 	{ CAP11XX_REG_MAIN_CONTROL,		0x00 },
86 	{ CAP11XX_REG_GENERAL_STATUS,		0x00 },
87 	{ CAP11XX_REG_SENSOR_INPUT,		0x00 },
88 	{ CAP11XX_REG_NOISE_FLAG_STATUS,	0x00 },
89 	{ CAP11XX_REG_SENSITIVITY_CONTROL,	0x2f },
90 	{ CAP11XX_REG_CONFIG,			0x20 },
91 	{ CAP11XX_REG_SENSOR_ENABLE,		0x3f },
92 	{ CAP11XX_REG_SENSOR_CONFIG,		0xa4 },
93 	{ CAP11XX_REG_SENSOR_CONFIG2,		0x07 },
94 	{ CAP11XX_REG_SAMPLING_CONFIG,		0x39 },
95 	{ CAP11XX_REG_CALIBRATION,		0x00 },
96 	{ CAP11XX_REG_INT_ENABLE,		0x3f },
97 	{ CAP11XX_REG_REPEAT_RATE,		0x3f },
98 	{ CAP11XX_REG_MT_CONFIG,		0x80 },
99 	{ CAP11XX_REG_MT_PATTERN_CONFIG,	0x00 },
100 	{ CAP11XX_REG_MT_PATTERN,		0x3f },
101 	{ CAP11XX_REG_RECALIB_CONFIG,		0x8a },
102 	{ CAP11XX_REG_SENSOR_THRESH(0),		0x40 },
103 	{ CAP11XX_REG_SENSOR_THRESH(1),		0x40 },
104 	{ CAP11XX_REG_SENSOR_THRESH(2),		0x40 },
105 	{ CAP11XX_REG_SENSOR_THRESH(3),		0x40 },
106 	{ CAP11XX_REG_SENSOR_THRESH(4),		0x40 },
107 	{ CAP11XX_REG_SENSOR_THRESH(5),		0x40 },
108 	{ CAP11XX_REG_SENSOR_NOISE_THRESH,	0x01 },
109 	{ CAP11XX_REG_STANDBY_CHANNEL,		0x00 },
110 	{ CAP11XX_REG_STANDBY_CONFIG,		0x39 },
111 	{ CAP11XX_REG_STANDBY_SENSITIVITY,	0x02 },
112 	{ CAP11XX_REG_STANDBY_THRESH,		0x40 },
113 	{ CAP11XX_REG_CONFIG2,			0x40 },
114 	{ CAP11XX_REG_SENSOR_CALIB_LSB1,	0x00 },
115 	{ CAP11XX_REG_SENSOR_CALIB_LSB2,	0x00 },
116 };
117 
118 static bool cap11xx_volatile_reg(struct device *dev, unsigned int reg)
119 {
120 	switch (reg) {
121 	case CAP11XX_REG_MAIN_CONTROL:
122 	case CAP11XX_REG_SENSOR_INPUT:
123 	case CAP11XX_REG_SENOR_DELTA(0):
124 	case CAP11XX_REG_SENOR_DELTA(1):
125 	case CAP11XX_REG_SENOR_DELTA(2):
126 	case CAP11XX_REG_SENOR_DELTA(3):
127 	case CAP11XX_REG_SENOR_DELTA(4):
128 	case CAP11XX_REG_SENOR_DELTA(5):
129 	case CAP11XX_REG_PRODUCT_ID:
130 	case CAP11XX_REG_MANUFACTURER_ID:
131 	case CAP11XX_REG_REVISION:
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_RBTREE,
147 	.volatile_reg = cap11xx_volatile_reg,
148 };
149 
150 static irqreturn_t cap11xx_thread_func(int irq_num, void *data)
151 {
152 	struct cap11xx_priv *priv = data;
153 	unsigned int status;
154 	int ret, i;
155 
156 	/*
157 	 * Deassert interrupt. This needs to be done before reading the status
158 	 * registers, which will not carry valid values otherwise.
159 	 */
160 	ret = regmap_update_bits(priv->regmap, CAP11XX_REG_MAIN_CONTROL, 1, 0);
161 	if (ret < 0)
162 		goto out;
163 
164 	ret = regmap_read(priv->regmap, CAP11XX_REG_SENSOR_INPUT, &status);
165 	if (ret < 0)
166 		goto out;
167 
168 	for (i = 0; i < priv->idev->keycodemax; i++)
169 		input_report_key(priv->idev, priv->keycodes[i],
170 				 status & (1 << i));
171 
172 	input_sync(priv->idev);
173 
174 out:
175 	return IRQ_HANDLED;
176 }
177 
178 static int cap11xx_set_sleep(struct cap11xx_priv *priv, bool sleep)
179 {
180 	return regmap_update_bits(priv->regmap, CAP11XX_REG_MAIN_CONTROL,
181 				  CAP11XX_REG_MAIN_CONTROL_DLSEEP,
182 				  sleep ? CAP11XX_REG_MAIN_CONTROL_DLSEEP : 0);
183 }
184 
185 static int cap11xx_input_open(struct input_dev *idev)
186 {
187 	struct cap11xx_priv *priv = input_get_drvdata(idev);
188 
189 	return cap11xx_set_sleep(priv, false);
190 }
191 
192 static void cap11xx_input_close(struct input_dev *idev)
193 {
194 	struct cap11xx_priv *priv = input_get_drvdata(idev);
195 
196 	cap11xx_set_sleep(priv, true);
197 }
198 
199 static int cap11xx_i2c_probe(struct i2c_client *i2c_client,
200 			     const struct i2c_device_id *id)
201 {
202 	struct device *dev = &i2c_client->dev;
203 	struct cap11xx_priv *priv;
204 	struct device_node *node;
205 	const struct cap11xx_hw_model *cap;
206 	int i, error, irq, gain = 0;
207 	unsigned int val, rev;
208 	u32 gain32;
209 
210 	if (id->driver_data >= ARRAY_SIZE(cap11xx_devices)) {
211 		dev_err(dev, "Invalid device ID %lu\n", id->driver_data);
212 		return -EINVAL;
213 	}
214 
215 	cap = &cap11xx_devices[id->driver_data];
216 	if (!cap || !cap->num_channels) {
217 		dev_err(dev, "Invalid device configuration\n");
218 		return -EINVAL;
219 	}
220 
221 	priv = devm_kzalloc(dev,
222 			    sizeof(*priv) +
223 				cap->num_channels * sizeof(priv->keycodes[0]),
224 			    GFP_KERNEL);
225 	if (!priv)
226 		return -ENOMEM;
227 
228 	priv->regmap = devm_regmap_init_i2c(i2c_client, &cap11xx_regmap_config);
229 	if (IS_ERR(priv->regmap))
230 		return PTR_ERR(priv->regmap);
231 
232 	error = regmap_read(priv->regmap, CAP11XX_REG_PRODUCT_ID, &val);
233 	if (error)
234 		return error;
235 
236 	if (val != cap->product_id) {
237 		dev_err(dev, "Product ID: Got 0x%02x, expected 0x%02x\n",
238 			val, cap->product_id);
239 		return -ENXIO;
240 	}
241 
242 	error = regmap_read(priv->regmap, CAP11XX_REG_MANUFACTURER_ID, &val);
243 	if (error)
244 		return error;
245 
246 	if (val != CAP11XX_MANUFACTURER_ID) {
247 		dev_err(dev, "Manufacturer ID: Got 0x%02x, expected 0x%02x\n",
248 			val, CAP11XX_MANUFACTURER_ID);
249 		return -ENXIO;
250 	}
251 
252 	error = regmap_read(priv->regmap, CAP11XX_REG_REVISION, &rev);
253 	if (error < 0)
254 		return error;
255 
256 	dev_info(dev, "CAP11XX detected, revision 0x%02x\n", rev);
257 	i2c_set_clientdata(i2c_client, priv);
258 	node = dev->of_node;
259 
260 	if (!of_property_read_u32(node, "microchip,sensor-gain", &gain32)) {
261 		if (is_power_of_2(gain32) && gain32 <= 8)
262 			gain = ilog2(gain32);
263 		else
264 			dev_err(dev, "Invalid sensor-gain value %d\n", gain32);
265 	}
266 
267 	if (of_property_read_bool(node, "microchip,irq-active-high")) {
268 		error = regmap_update_bits(priv->regmap, CAP11XX_REG_CONFIG2,
269 					   CAP11XX_REG_CONFIG2_ALT_POL, 0);
270 		if (error)
271 			return error;
272 	}
273 
274 	/* Provide some useful defaults */
275 	for (i = 0; i < cap->num_channels; i++)
276 		priv->keycodes[i] = KEY_A + i;
277 
278 	of_property_read_u32_array(node, "linux,keycodes",
279 				   priv->keycodes, cap->num_channels);
280 
281 	error = regmap_update_bits(priv->regmap, CAP11XX_REG_MAIN_CONTROL,
282 				   CAP11XX_REG_MAIN_CONTROL_GAIN_MASK,
283 				   gain << CAP11XX_REG_MAIN_CONTROL_GAIN_SHIFT);
284 	if (error)
285 		return error;
286 
287 	/* Disable autorepeat. The Linux input system has its own handling. */
288 	error = regmap_write(priv->regmap, CAP11XX_REG_REPEAT_RATE, 0);
289 	if (error)
290 		return error;
291 
292 	priv->idev = devm_input_allocate_device(dev);
293 	if (!priv->idev)
294 		return -ENOMEM;
295 
296 	priv->idev->name = "CAP11XX capacitive touch sensor";
297 	priv->idev->id.bustype = BUS_I2C;
298 	priv->idev->evbit[0] = BIT_MASK(EV_KEY);
299 
300 	if (of_property_read_bool(node, "autorepeat"))
301 		__set_bit(EV_REP, priv->idev->evbit);
302 
303 	for (i = 0; i < cap->num_channels; i++)
304 		__set_bit(priv->keycodes[i], priv->idev->keybit);
305 
306 	__clear_bit(KEY_RESERVED, priv->idev->keybit);
307 
308 	priv->idev->keycode = priv->keycodes;
309 	priv->idev->keycodesize = sizeof(priv->keycodes[0]);
310 	priv->idev->keycodemax = cap->num_channels;
311 
312 	priv->idev->id.vendor = CAP11XX_MANUFACTURER_ID;
313 	priv->idev->id.product = cap->product_id;
314 	priv->idev->id.version = rev;
315 
316 	priv->idev->open = cap11xx_input_open;
317 	priv->idev->close = cap11xx_input_close;
318 
319 	input_set_drvdata(priv->idev, priv);
320 
321 	/*
322 	 * Put the device in deep sleep mode for now.
323 	 * ->open() will bring it back once the it is actually needed.
324 	 */
325 	cap11xx_set_sleep(priv, true);
326 
327 	error = input_register_device(priv->idev);
328 	if (error)
329 		return error;
330 
331 	irq = irq_of_parse_and_map(node, 0);
332 	if (!irq) {
333 		dev_err(dev, "Unable to parse or map IRQ\n");
334 		return -ENXIO;
335 	}
336 
337 	error = devm_request_threaded_irq(dev, irq, NULL, cap11xx_thread_func,
338 					  IRQF_ONESHOT, dev_name(dev), priv);
339 	if (error)
340 		return error;
341 
342 	return 0;
343 }
344 
345 static const struct of_device_id cap11xx_dt_ids[] = {
346 	{ .compatible = "microchip,cap1106", },
347 	{ .compatible = "microchip,cap1126", },
348 	{ .compatible = "microchip,cap1188", },
349 	{}
350 };
351 MODULE_DEVICE_TABLE(of, cap11xx_dt_ids);
352 
353 static const struct i2c_device_id cap11xx_i2c_ids[] = {
354 	{ "cap1106", CAP1106 },
355 	{ "cap1126", CAP1126 },
356 	{ "cap1188", CAP1188 },
357 	{}
358 };
359 MODULE_DEVICE_TABLE(i2c, cap11xx_i2c_ids);
360 
361 static struct i2c_driver cap11xx_i2c_driver = {
362 	.driver = {
363 		.name	= "cap11xx",
364 		.owner	= THIS_MODULE,
365 		.of_match_table = cap11xx_dt_ids,
366 	},
367 	.id_table	= cap11xx_i2c_ids,
368 	.probe		= cap11xx_i2c_probe,
369 };
370 
371 module_i2c_driver(cap11xx_i2c_driver);
372 
373 MODULE_ALIAS("platform:cap11xx");
374 MODULE_DESCRIPTION("Microchip CAP11XX driver");
375 MODULE_AUTHOR("Daniel Mack <linux@zonque.org>");
376 MODULE_LICENSE("GPL v2");
377