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