1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Touchkey driver for Freescale MPR121 Controllor 4 * 5 * Copyright (C) 2011 Freescale Semiconductor, Inc. 6 * Author: Zhang Jiejing <jiejing.zhang@freescale.com> 7 * 8 * Based on mcs_touchkey.c 9 */ 10 11 #include <linux/bitops.h> 12 #include <linux/delay.h> 13 #include <linux/i2c.h> 14 #include <linux/input.h> 15 #include <linux/interrupt.h> 16 #include <linux/module.h> 17 #include <linux/of.h> 18 #include <linux/property.h> 19 #include <linux/regulator/consumer.h> 20 #include <linux/slab.h> 21 22 /* Register definitions */ 23 #define ELE_TOUCH_STATUS_0_ADDR 0x0 24 #define ELE_TOUCH_STATUS_1_ADDR 0X1 25 #define MHD_RISING_ADDR 0x2b 26 #define NHD_RISING_ADDR 0x2c 27 #define NCL_RISING_ADDR 0x2d 28 #define FDL_RISING_ADDR 0x2e 29 #define MHD_FALLING_ADDR 0x2f 30 #define NHD_FALLING_ADDR 0x30 31 #define NCL_FALLING_ADDR 0x31 32 #define FDL_FALLING_ADDR 0x32 33 #define ELE0_TOUCH_THRESHOLD_ADDR 0x41 34 #define ELE0_RELEASE_THRESHOLD_ADDR 0x42 35 #define AFE_CONF_ADDR 0x5c 36 #define FILTER_CONF_ADDR 0x5d 37 38 /* 39 * ELECTRODE_CONF_ADDR: This register configures the number of 40 * enabled capacitance sensing inputs and its run/suspend mode. 41 */ 42 #define ELECTRODE_CONF_ADDR 0x5e 43 #define ELECTRODE_CONF_QUICK_CHARGE 0x80 44 #define AUTO_CONFIG_CTRL_ADDR 0x7b 45 #define AUTO_CONFIG_USL_ADDR 0x7d 46 #define AUTO_CONFIG_LSL_ADDR 0x7e 47 #define AUTO_CONFIG_TL_ADDR 0x7f 48 49 /* Threshold of touch/release trigger */ 50 #define TOUCH_THRESHOLD 0x08 51 #define RELEASE_THRESHOLD 0x05 52 /* Masks for touch and release triggers */ 53 #define TOUCH_STATUS_MASK 0xfff 54 /* MPR121 has 12 keys */ 55 #define MPR121_MAX_KEY_COUNT 12 56 57 #define MPR121_MIN_POLL_INTERVAL 10 58 #define MPR121_MAX_POLL_INTERVAL 200 59 60 struct mpr121_touchkey { 61 struct i2c_client *client; 62 struct input_dev *input_dev; 63 unsigned int statusbits; 64 unsigned int keycount; 65 u32 keycodes[MPR121_MAX_KEY_COUNT]; 66 }; 67 68 struct mpr121_init_register { 69 int addr; 70 u8 val; 71 }; 72 73 static const struct mpr121_init_register init_reg_table[] = { 74 { MHD_RISING_ADDR, 0x1 }, 75 { NHD_RISING_ADDR, 0x1 }, 76 { MHD_FALLING_ADDR, 0x1 }, 77 { NHD_FALLING_ADDR, 0x1 }, 78 { NCL_FALLING_ADDR, 0xff }, 79 { FDL_FALLING_ADDR, 0x02 }, 80 { FILTER_CONF_ADDR, 0x04 }, 81 { AFE_CONF_ADDR, 0x0b }, 82 { AUTO_CONFIG_CTRL_ADDR, 0x0b }, 83 }; 84 85 static void mpr121_vdd_supply_disable(void *data) 86 { 87 struct regulator *vdd_supply = data; 88 89 regulator_disable(vdd_supply); 90 } 91 92 static struct regulator *mpr121_vdd_supply_init(struct device *dev) 93 { 94 struct regulator *vdd_supply; 95 int err; 96 97 vdd_supply = devm_regulator_get(dev, "vdd"); 98 if (IS_ERR(vdd_supply)) { 99 dev_err(dev, "failed to get vdd regulator: %ld\n", 100 PTR_ERR(vdd_supply)); 101 return vdd_supply; 102 } 103 104 err = regulator_enable(vdd_supply); 105 if (err) { 106 dev_err(dev, "failed to enable vdd regulator: %d\n", err); 107 return ERR_PTR(err); 108 } 109 110 err = devm_add_action(dev, mpr121_vdd_supply_disable, vdd_supply); 111 if (err) { 112 regulator_disable(vdd_supply); 113 dev_err(dev, "failed to add disable regulator action: %d\n", 114 err); 115 return ERR_PTR(err); 116 } 117 118 return vdd_supply; 119 } 120 121 static void mpr_touchkey_report(struct input_dev *dev) 122 { 123 struct mpr121_touchkey *mpr121 = input_get_drvdata(dev); 124 struct input_dev *input = mpr121->input_dev; 125 struct i2c_client *client = mpr121->client; 126 unsigned long bit_changed; 127 unsigned int key_num; 128 int reg; 129 130 reg = i2c_smbus_read_byte_data(client, ELE_TOUCH_STATUS_1_ADDR); 131 if (reg < 0) { 132 dev_err(&client->dev, "i2c read error [%d]\n", reg); 133 return; 134 } 135 136 reg <<= 8; 137 reg |= i2c_smbus_read_byte_data(client, ELE_TOUCH_STATUS_0_ADDR); 138 if (reg < 0) { 139 dev_err(&client->dev, "i2c read error [%d]\n", reg); 140 return; 141 } 142 143 reg &= TOUCH_STATUS_MASK; 144 /* use old press bit to figure out which bit changed */ 145 bit_changed = reg ^ mpr121->statusbits; 146 mpr121->statusbits = reg; 147 for_each_set_bit(key_num, &bit_changed, mpr121->keycount) { 148 unsigned int key_val, pressed; 149 150 pressed = reg & BIT(key_num); 151 key_val = mpr121->keycodes[key_num]; 152 153 input_event(input, EV_MSC, MSC_SCAN, key_num); 154 input_report_key(input, key_val, pressed); 155 156 dev_dbg(&client->dev, "key %d %d %s\n", key_num, key_val, 157 pressed ? "pressed" : "released"); 158 159 } 160 input_sync(input); 161 } 162 163 static irqreturn_t mpr_touchkey_interrupt(int irq, void *dev_id) 164 { 165 struct mpr121_touchkey *mpr121 = dev_id; 166 167 mpr_touchkey_report(mpr121->input_dev); 168 169 return IRQ_HANDLED; 170 } 171 172 static int mpr121_phys_init(struct mpr121_touchkey *mpr121, 173 struct i2c_client *client, int vdd_uv) 174 { 175 const struct mpr121_init_register *reg; 176 unsigned char usl, lsl, tl, eleconf; 177 int i, t, vdd, ret; 178 179 /* Set up touch/release threshold for ele0-ele11 */ 180 for (i = 0; i <= MPR121_MAX_KEY_COUNT; i++) { 181 t = ELE0_TOUCH_THRESHOLD_ADDR + (i * 2); 182 ret = i2c_smbus_write_byte_data(client, t, TOUCH_THRESHOLD); 183 if (ret < 0) 184 goto err_i2c_write; 185 ret = i2c_smbus_write_byte_data(client, t + 1, 186 RELEASE_THRESHOLD); 187 if (ret < 0) 188 goto err_i2c_write; 189 } 190 191 /* Set up init register */ 192 for (i = 0; i < ARRAY_SIZE(init_reg_table); i++) { 193 reg = &init_reg_table[i]; 194 ret = i2c_smbus_write_byte_data(client, reg->addr, reg->val); 195 if (ret < 0) 196 goto err_i2c_write; 197 } 198 199 200 /* 201 * Capacitance on sensing input varies and needs to be compensated. 202 * The internal MPR121-auto-configuration can do this if it's 203 * registers are set properly (based on vdd_uv). 204 */ 205 vdd = vdd_uv / 1000; 206 usl = ((vdd - 700) * 256) / vdd; 207 lsl = (usl * 65) / 100; 208 tl = (usl * 90) / 100; 209 ret = i2c_smbus_write_byte_data(client, AUTO_CONFIG_USL_ADDR, usl); 210 ret |= i2c_smbus_write_byte_data(client, AUTO_CONFIG_LSL_ADDR, lsl); 211 ret |= i2c_smbus_write_byte_data(client, AUTO_CONFIG_TL_ADDR, tl); 212 213 /* 214 * Quick charge bit will let the capacitive charge to ready 215 * state quickly, or the buttons may not function after system 216 * boot. 217 */ 218 eleconf = mpr121->keycount | ELECTRODE_CONF_QUICK_CHARGE; 219 ret |= i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR, 220 eleconf); 221 if (ret != 0) 222 goto err_i2c_write; 223 224 dev_dbg(&client->dev, "set up with %x keys.\n", mpr121->keycount); 225 226 return 0; 227 228 err_i2c_write: 229 dev_err(&client->dev, "i2c write error: %d\n", ret); 230 return ret; 231 } 232 233 static int mpr_touchkey_probe(struct i2c_client *client, 234 const struct i2c_device_id *id) 235 { 236 struct device *dev = &client->dev; 237 struct regulator *vdd_supply; 238 int vdd_uv; 239 struct mpr121_touchkey *mpr121; 240 struct input_dev *input_dev; 241 u32 poll_interval = 0; 242 int error; 243 int i; 244 245 vdd_supply = mpr121_vdd_supply_init(dev); 246 if (IS_ERR(vdd_supply)) 247 return PTR_ERR(vdd_supply); 248 249 vdd_uv = regulator_get_voltage(vdd_supply); 250 251 mpr121 = devm_kzalloc(dev, sizeof(*mpr121), GFP_KERNEL); 252 if (!mpr121) 253 return -ENOMEM; 254 255 input_dev = devm_input_allocate_device(dev); 256 if (!input_dev) 257 return -ENOMEM; 258 259 mpr121->client = client; 260 mpr121->input_dev = input_dev; 261 mpr121->keycount = device_property_count_u32(dev, "linux,keycodes"); 262 if (mpr121->keycount > MPR121_MAX_KEY_COUNT) { 263 dev_err(dev, "too many keys defined (%d)\n", mpr121->keycount); 264 return -EINVAL; 265 } 266 267 error = device_property_read_u32_array(dev, "linux,keycodes", 268 mpr121->keycodes, 269 mpr121->keycount); 270 if (error) { 271 dev_err(dev, 272 "failed to read linux,keycode property: %d\n", error); 273 return error; 274 } 275 276 input_dev->name = "Freescale MPR121 Touchkey"; 277 input_dev->id.bustype = BUS_I2C; 278 input_dev->dev.parent = dev; 279 if (device_property_read_bool(dev, "autorepeat")) 280 __set_bit(EV_REP, input_dev->evbit); 281 input_set_capability(input_dev, EV_MSC, MSC_SCAN); 282 input_set_drvdata(input_dev, mpr121); 283 284 input_dev->keycode = mpr121->keycodes; 285 input_dev->keycodesize = sizeof(mpr121->keycodes[0]); 286 input_dev->keycodemax = mpr121->keycount; 287 288 for (i = 0; i < mpr121->keycount; i++) 289 input_set_capability(input_dev, EV_KEY, mpr121->keycodes[i]); 290 291 error = mpr121_phys_init(mpr121, client, vdd_uv); 292 if (error) { 293 dev_err(dev, "Failed to init register\n"); 294 return error; 295 } 296 297 device_property_read_u32(dev, "poll-interval", &poll_interval); 298 299 if (client->irq) { 300 error = devm_request_threaded_irq(dev, client->irq, NULL, 301 mpr_touchkey_interrupt, 302 IRQF_TRIGGER_FALLING | 303 IRQF_ONESHOT, 304 dev->driver->name, mpr121); 305 if (error) { 306 dev_err(dev, "Failed to register interrupt\n"); 307 return error; 308 } 309 } else if (poll_interval) { 310 if (poll_interval < MPR121_MIN_POLL_INTERVAL) 311 return -EINVAL; 312 313 if (poll_interval > MPR121_MAX_POLL_INTERVAL) 314 return -EINVAL; 315 316 error = input_setup_polling(input_dev, mpr_touchkey_report); 317 if (error) { 318 dev_err(dev, "Failed to setup polling\n"); 319 return error; 320 } 321 322 input_set_poll_interval(input_dev, poll_interval); 323 input_set_min_poll_interval(input_dev, 324 MPR121_MIN_POLL_INTERVAL); 325 input_set_max_poll_interval(input_dev, 326 MPR121_MAX_POLL_INTERVAL); 327 } else { 328 dev_err(dev, 329 "invalid IRQ number and polling not configured\n"); 330 return -EINVAL; 331 } 332 333 error = input_register_device(input_dev); 334 if (error) 335 return error; 336 337 i2c_set_clientdata(client, mpr121); 338 device_init_wakeup(dev, 339 device_property_read_bool(dev, "wakeup-source")); 340 341 return 0; 342 } 343 344 static int __maybe_unused mpr_suspend(struct device *dev) 345 { 346 struct i2c_client *client = to_i2c_client(dev); 347 348 if (device_may_wakeup(&client->dev)) 349 enable_irq_wake(client->irq); 350 351 i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR, 0x00); 352 353 return 0; 354 } 355 356 static int __maybe_unused mpr_resume(struct device *dev) 357 { 358 struct i2c_client *client = to_i2c_client(dev); 359 struct mpr121_touchkey *mpr121 = i2c_get_clientdata(client); 360 361 if (device_may_wakeup(&client->dev)) 362 disable_irq_wake(client->irq); 363 364 i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR, 365 mpr121->keycount); 366 367 return 0; 368 } 369 370 static SIMPLE_DEV_PM_OPS(mpr121_touchkey_pm_ops, mpr_suspend, mpr_resume); 371 372 static const struct i2c_device_id mpr121_id[] = { 373 { "mpr121_touchkey", 0 }, 374 { } 375 }; 376 MODULE_DEVICE_TABLE(i2c, mpr121_id); 377 378 #ifdef CONFIG_OF 379 static const struct of_device_id mpr121_touchkey_dt_match_table[] = { 380 { .compatible = "fsl,mpr121-touchkey" }, 381 { }, 382 }; 383 MODULE_DEVICE_TABLE(of, mpr121_touchkey_dt_match_table); 384 #endif 385 386 static struct i2c_driver mpr_touchkey_driver = { 387 .driver = { 388 .name = "mpr121", 389 .pm = &mpr121_touchkey_pm_ops, 390 .of_match_table = of_match_ptr(mpr121_touchkey_dt_match_table), 391 }, 392 .id_table = mpr121_id, 393 .probe = mpr_touchkey_probe, 394 }; 395 396 module_i2c_driver(mpr_touchkey_driver); 397 398 MODULE_LICENSE("GPL"); 399 MODULE_AUTHOR("Zhang Jiejing <jiejing.zhang@freescale.com>"); 400 MODULE_DESCRIPTION("Touch Key driver for Freescale MPR121 Chip"); 401