2 * Input driver for Microchip CAP1106, 6 channel capacitive touch sensor
4 * http://www.microchip.com/wwwproducts/Devices.aspx?product=CAP1106
6 * (c) 2014 Daniel Mack <linux@zonque.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/interrupt.h>
16 #include <linux/input.h>
17 #include <linux/of_irq.h>
18 #include <linux/regmap.h>
19 #include <linux/i2c.h>
20 #include <linux/gpio/consumer.h>
22 #define CAP1106_REG_MAIN_CONTROL 0x00
23 #define CAP1106_REG_MAIN_CONTROL_GAIN_SHIFT (6)
24 #define CAP1106_REG_MAIN_CONTROL_GAIN_MASK (0xc0)
25 #define CAP1106_REG_MAIN_CONTROL_DLSEEP BIT(4)
26 #define CAP1106_REG_GENERAL_STATUS 0x02
27 #define CAP1106_REG_SENSOR_INPUT 0x03
28 #define CAP1106_REG_NOISE_FLAG_STATUS 0x0a
29 #define CAP1106_REG_SENOR_DELTA(X) (0x10 + (X))
30 #define CAP1106_REG_SENSITIVITY_CONTROL 0x1f
31 #define CAP1106_REG_CONFIG 0x20
32 #define CAP1106_REG_SENSOR_ENABLE 0x21
33 #define CAP1106_REG_SENSOR_CONFIG 0x22
34 #define CAP1106_REG_SENSOR_CONFIG2 0x23
35 #define CAP1106_REG_SAMPLING_CONFIG 0x24
36 #define CAP1106_REG_CALIBRATION 0x26
37 #define CAP1106_REG_INT_ENABLE 0x27
38 #define CAP1106_REG_REPEAT_RATE 0x28
39 #define CAP1106_REG_MT_CONFIG 0x2a
40 #define CAP1106_REG_MT_PATTERN_CONFIG 0x2b
41 #define CAP1106_REG_MT_PATTERN 0x2d
42 #define CAP1106_REG_RECALIB_CONFIG 0x2f
43 #define CAP1106_REG_SENSOR_THRESH(X) (0x30 + (X))
44 #define CAP1106_REG_SENSOR_NOISE_THRESH 0x38
45 #define CAP1106_REG_STANDBY_CHANNEL 0x40
46 #define CAP1106_REG_STANDBY_CONFIG 0x41
47 #define CAP1106_REG_STANDBY_SENSITIVITY 0x42
48 #define CAP1106_REG_STANDBY_THRESH 0x43
49 #define CAP1106_REG_CONFIG2 0x44
50 #define CAP1106_REG_SENSOR_BASE_CNT(X) (0x50 + (X))
51 #define CAP1106_REG_SENSOR_CALIB (0xb1 + (X))
52 #define CAP1106_REG_SENSOR_CALIB_LSB1 0xb9
53 #define CAP1106_REG_SENSOR_CALIB_LSB2 0xba
54 #define CAP1106_REG_PRODUCT_ID 0xfd
55 #define CAP1106_REG_MANUFACTURER_ID 0xfe
56 #define CAP1106_REG_REVISION 0xff
58 #define CAP1106_NUM_CHN 6
59 #define CAP1106_PRODUCT_ID 0x55
60 #define CAP1106_MANUFACTURER_ID 0x5d
63 struct regmap *regmap;
64 struct input_dev *idev;
67 unsigned short keycodes[CAP1106_NUM_CHN];
70 static const struct reg_default cap1106_reg_defaults[] = {
71 { CAP1106_REG_MAIN_CONTROL, 0x00 },
72 { CAP1106_REG_GENERAL_STATUS, 0x00 },
73 { CAP1106_REG_SENSOR_INPUT, 0x00 },
74 { CAP1106_REG_NOISE_FLAG_STATUS, 0x00 },
75 { CAP1106_REG_SENSITIVITY_CONTROL, 0x2f },
76 { CAP1106_REG_CONFIG, 0x20 },
77 { CAP1106_REG_SENSOR_ENABLE, 0x3f },
78 { CAP1106_REG_SENSOR_CONFIG, 0xa4 },
79 { CAP1106_REG_SENSOR_CONFIG2, 0x07 },
80 { CAP1106_REG_SAMPLING_CONFIG, 0x39 },
81 { CAP1106_REG_CALIBRATION, 0x00 },
82 { CAP1106_REG_INT_ENABLE, 0x3f },
83 { CAP1106_REG_REPEAT_RATE, 0x3f },
84 { CAP1106_REG_MT_CONFIG, 0x80 },
85 { CAP1106_REG_MT_PATTERN_CONFIG, 0x00 },
86 { CAP1106_REG_MT_PATTERN, 0x3f },
87 { CAP1106_REG_RECALIB_CONFIG, 0x8a },
88 { CAP1106_REG_SENSOR_THRESH(0), 0x40 },
89 { CAP1106_REG_SENSOR_THRESH(1), 0x40 },
90 { CAP1106_REG_SENSOR_THRESH(2), 0x40 },
91 { CAP1106_REG_SENSOR_THRESH(3), 0x40 },
92 { CAP1106_REG_SENSOR_THRESH(4), 0x40 },
93 { CAP1106_REG_SENSOR_THRESH(5), 0x40 },
94 { CAP1106_REG_SENSOR_NOISE_THRESH, 0x01 },
95 { CAP1106_REG_STANDBY_CHANNEL, 0x00 },
96 { CAP1106_REG_STANDBY_CONFIG, 0x39 },
97 { CAP1106_REG_STANDBY_SENSITIVITY, 0x02 },
98 { CAP1106_REG_STANDBY_THRESH, 0x40 },
99 { CAP1106_REG_CONFIG2, 0x40 },
100 { CAP1106_REG_SENSOR_CALIB_LSB1, 0x00 },
101 { CAP1106_REG_SENSOR_CALIB_LSB2, 0x00 },
104 static bool cap1106_volatile_reg(struct device *dev, unsigned int reg)
107 case CAP1106_REG_MAIN_CONTROL:
108 case CAP1106_REG_SENSOR_INPUT:
109 case CAP1106_REG_SENOR_DELTA(0):
110 case CAP1106_REG_SENOR_DELTA(1):
111 case CAP1106_REG_SENOR_DELTA(2):
112 case CAP1106_REG_SENOR_DELTA(3):
113 case CAP1106_REG_SENOR_DELTA(4):
114 case CAP1106_REG_SENOR_DELTA(5):
115 case CAP1106_REG_PRODUCT_ID:
116 case CAP1106_REG_MANUFACTURER_ID:
117 case CAP1106_REG_REVISION:
124 static const struct regmap_config cap1106_regmap_config = {
128 .max_register = CAP1106_REG_REVISION,
129 .reg_defaults = cap1106_reg_defaults,
131 .num_reg_defaults = ARRAY_SIZE(cap1106_reg_defaults),
132 .cache_type = REGCACHE_RBTREE,
133 .volatile_reg = cap1106_volatile_reg,
136 static irqreturn_t cap1106_thread_func(int irq_num, void *data)
138 struct cap1106_priv *priv = data;
143 * Deassert interrupt. This needs to be done before reading the status
144 * registers, which will not carry valid values otherwise.
146 ret = regmap_update_bits(priv->regmap, CAP1106_REG_MAIN_CONTROL, 1, 0);
150 ret = regmap_read(priv->regmap, CAP1106_REG_SENSOR_INPUT, &status);
154 for (i = 0; i < CAP1106_NUM_CHN; i++)
155 input_report_key(priv->idev, priv->keycodes[i],
158 input_sync(priv->idev);
164 static int cap1106_set_sleep(struct cap1106_priv *priv, bool sleep)
166 return regmap_update_bits(priv->regmap, CAP1106_REG_MAIN_CONTROL,
167 CAP1106_REG_MAIN_CONTROL_DLSEEP,
168 sleep ? CAP1106_REG_MAIN_CONTROL_DLSEEP : 0);
171 static int cap1106_input_open(struct input_dev *idev)
173 struct cap1106_priv *priv = input_get_drvdata(idev);
175 return cap1106_set_sleep(priv, false);
178 static void cap1106_input_close(struct input_dev *idev)
180 struct cap1106_priv *priv = input_get_drvdata(idev);
182 cap1106_set_sleep(priv, true);
185 static int cap1106_i2c_probe(struct i2c_client *i2c_client,
186 const struct i2c_device_id *id)
188 struct device *dev = &i2c_client->dev;
189 struct cap1106_priv *priv;
190 struct device_node *node;
191 int i, error, irq, gain = 0;
192 unsigned int val, rev;
193 u32 gain32, keycodes[CAP1106_NUM_CHN];
195 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
199 priv->regmap = devm_regmap_init_i2c(i2c_client, &cap1106_regmap_config);
200 if (IS_ERR(priv->regmap))
201 return PTR_ERR(priv->regmap);
203 error = regmap_read(priv->regmap, CAP1106_REG_PRODUCT_ID, &val);
207 if (val != CAP1106_PRODUCT_ID) {
208 dev_err(dev, "Product ID: Got 0x%02x, expected 0x%02x\n",
209 val, CAP1106_PRODUCT_ID);
213 error = regmap_read(priv->regmap, CAP1106_REG_MANUFACTURER_ID, &val);
217 if (val != CAP1106_MANUFACTURER_ID) {
218 dev_err(dev, "Manufacturer ID: Got 0x%02x, expected 0x%02x\n",
219 val, CAP1106_MANUFACTURER_ID);
223 error = regmap_read(priv->regmap, CAP1106_REG_REVISION, &rev);
227 dev_info(dev, "CAP1106 detected, revision 0x%02x\n", rev);
228 i2c_set_clientdata(i2c_client, priv);
231 if (!of_property_read_u32(node, "microchip,sensor-gain", &gain32)) {
232 if (is_power_of_2(gain32) && gain32 <= 8)
233 gain = ilog2(gain32);
235 dev_err(dev, "Invalid sensor-gain value %d\n", gain32);
238 BUILD_BUG_ON(ARRAY_SIZE(keycodes) != ARRAY_SIZE(priv->keycodes));
240 /* Provide some useful defaults */
241 for (i = 0; i < ARRAY_SIZE(keycodes); i++)
242 keycodes[i] = KEY_A + i;
244 of_property_read_u32_array(node, "linux,keycodes",
245 keycodes, ARRAY_SIZE(keycodes));
247 for (i = 0; i < ARRAY_SIZE(keycodes); i++)
248 priv->keycodes[i] = keycodes[i];
250 error = regmap_update_bits(priv->regmap, CAP1106_REG_MAIN_CONTROL,
251 CAP1106_REG_MAIN_CONTROL_GAIN_MASK,
252 gain << CAP1106_REG_MAIN_CONTROL_GAIN_SHIFT);
256 /* Disable autorepeat. The Linux input system has its own handling. */
257 error = regmap_write(priv->regmap, CAP1106_REG_REPEAT_RATE, 0);
261 priv->idev = devm_input_allocate_device(dev);
265 priv->idev->name = "CAP1106 capacitive touch sensor";
266 priv->idev->id.bustype = BUS_I2C;
267 priv->idev->evbit[0] = BIT_MASK(EV_KEY);
269 if (of_property_read_bool(node, "autorepeat"))
270 __set_bit(EV_REP, priv->idev->evbit);
272 for (i = 0; i < CAP1106_NUM_CHN; i++)
273 __set_bit(priv->keycodes[i], priv->idev->keybit);
275 __clear_bit(KEY_RESERVED, priv->idev->keybit);
277 priv->idev->keycode = priv->keycodes;
278 priv->idev->keycodesize = sizeof(priv->keycodes[0]);
279 priv->idev->keycodemax = ARRAY_SIZE(priv->keycodes);
281 priv->idev->id.vendor = CAP1106_MANUFACTURER_ID;
282 priv->idev->id.product = CAP1106_PRODUCT_ID;
283 priv->idev->id.version = rev;
285 priv->idev->open = cap1106_input_open;
286 priv->idev->close = cap1106_input_close;
288 input_set_drvdata(priv->idev, priv);
291 * Put the device in deep sleep mode for now.
292 * ->open() will bring it back once the it is actually needed.
294 cap1106_set_sleep(priv, true);
296 error = input_register_device(priv->idev);
300 irq = irq_of_parse_and_map(node, 0);
302 dev_err(dev, "Unable to parse or map IRQ\n");
306 error = devm_request_threaded_irq(dev, irq, NULL, cap1106_thread_func,
307 IRQF_ONESHOT, dev_name(dev), priv);
314 static const struct of_device_id cap1106_dt_ids[] = {
315 { .compatible = "microchip,cap1106", },
318 MODULE_DEVICE_TABLE(of, cap1106_dt_ids);
320 static const struct i2c_device_id cap1106_i2c_ids[] = {
324 MODULE_DEVICE_TABLE(i2c, cap1106_i2c_ids);
326 static struct i2c_driver cap1106_i2c_driver = {
329 .owner = THIS_MODULE,
330 .of_match_table = cap1106_dt_ids,
332 .id_table = cap1106_i2c_ids,
333 .probe = cap1106_i2c_probe,
336 module_i2c_driver(cap1106_i2c_driver);
338 MODULE_ALIAS("platform:cap1106");
339 MODULE_DESCRIPTION("Microchip CAP1106 driver");
340 MODULE_AUTHOR("Daniel Mack <linux@zonque.org>");
341 MODULE_LICENSE("GPL v2");