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