| 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 <[email protected]> |
| 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 mpr_touchkey_report(struct input_dev *dev) |
| 86 |
{ |
| 87 |
struct mpr121_touchkey *mpr121 = input_get_drvdata(dev); |
| 88 |
struct input_dev *input = mpr121->input_dev; |
| 89 |
struct i2c_client *client = mpr121->client; |
| 90 |
unsigned long bit_changed; |
| 91 |
unsigned int key_num; |
| 92 |
int reg; |
| 93 |
|
| 94 |
reg = i2c_smbus_read_byte_data(client, ELE_TOUCH_STATUS_1_ADDR); |
| 95 |
if (reg < 0) { |
| 96 |
dev_err(&client->dev, "i2c read error [%d]\n", reg); |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
reg <<= 8; |
| 101 |
reg |= i2c_smbus_read_byte_data(client, ELE_TOUCH_STATUS_0_ADDR); |
| 102 |
if (reg < 0) { |
| 103 |
dev_err(&client->dev, "i2c read error [%d]\n", reg); |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
reg &= TOUCH_STATUS_MASK; |
| 108 |
/* use old press bit to figure out which bit changed */ |
| 109 |
bit_changed = reg ^ mpr121->statusbits; |
| 110 |
mpr121->statusbits = reg; |
| 111 |
for_each_set_bit(key_num, &bit_changed, mpr121->keycount) { |
| 112 |
unsigned int key_val, pressed; |
| 113 |
|
| 114 |
pressed = reg & BIT(key_num); |
| 115 |
key_val = mpr121->keycodes[key_num]; |
| 116 |
|
| 117 |
input_event(input, EV_MSC, MSC_SCAN, key_num); |
| 118 |
input_report_key(input, key_val, pressed); |
| 119 |
|
| 120 |
dev_dbg(&client->dev, "key %d %d %s\n", key_num, key_val, |
| 121 |
pressed ? "pressed" : "released"); |
| 122 |
|
| 123 |
} |
| 124 |
input_sync(input); |
| 125 |
} |
| 126 |
|
| 127 |
static irqreturn_t mpr_touchkey_interrupt(int irq, void *dev_id) |
| 128 |
{ |
| 129 |
struct mpr121_touchkey *mpr121 = dev_id; |
| 130 |
|
| 131 |
mpr_touchkey_report(mpr121->input_dev); |
| 132 |
|
| 133 |
return IRQ_HANDLED; |
| 134 |
} |
| 135 |
|
| 136 |
static int mpr121_phys_init(struct mpr121_touchkey *mpr121, |
| 137 |
struct i2c_client *client, int vdd_uv) |
| 138 |
{ |
| 139 |
const struct mpr121_init_register *reg; |
| 140 |
unsigned char usl, lsl, tl, eleconf; |
| 141 |
int i, t, vdd, ret; |
| 142 |
|
| 143 |
/* Set up touch/release threshold for ele0-ele11 */ |
| 144 |
for (i = 0; i <= MPR121_MAX_KEY_COUNT; i++) { |
| 145 |
t = ELE0_TOUCH_THRESHOLD_ADDR + (i * 2); |
| 146 |
ret = i2c_smbus_write_byte_data(client, t, TOUCH_THRESHOLD); |
| 147 |
if (ret < 0) |
| 148 |
goto err_i2c_write; |
| 149 |
ret = i2c_smbus_write_byte_data(client, t + 1, |
| 150 |
RELEASE_THRESHOLD); |
| 151 |
if (ret < 0) |
| 152 |
goto err_i2c_write; |
| 153 |
} |
| 154 |
|
| 155 |
/* Set up init register */ |
| 156 |
for (i = 0; i < ARRAY_SIZE(init_reg_table); i++) { |
| 157 |
reg = &init_reg_table[i]; |
| 158 |
ret = i2c_smbus_write_byte_data(client, reg->addr, reg->val); |
| 159 |
if (ret < 0) |
| 160 |
goto err_i2c_write; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
/* |
| 165 |
* Capacitance on sensing input varies and needs to be compensated. |
| 166 |
* The internal MPR121-auto-configuration can do this if it's |
| 167 |
* registers are set properly (based on vdd_uv). |
| 168 |
*/ |
| 169 |
vdd = vdd_uv / 1000; |
| 170 |
usl = ((vdd - 700) * 256) / vdd; |
| 171 |
lsl = (usl * 65) / 100; |
| 172 |
tl = (usl * 90) / 100; |
| 173 |
ret = i2c_smbus_write_byte_data(client, AUTO_CONFIG_USL_ADDR, usl); |
| 174 |
ret |= i2c_smbus_write_byte_data(client, AUTO_CONFIG_LSL_ADDR, lsl); |
| 175 |
ret |= i2c_smbus_write_byte_data(client, AUTO_CONFIG_TL_ADDR, tl); |
| 176 |
|
| 177 |
/* |
| 178 |
* Quick charge bit will let the capacitive charge to ready |
| 179 |
* state quickly, or the buttons may not function after system |
| 180 |
* boot. |
| 181 |
*/ |
| 182 |
eleconf = mpr121->keycount | ELECTRODE_CONF_QUICK_CHARGE; |
| 183 |
ret |= i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR, |
| 184 |
eleconf); |
| 185 |
if (ret != 0) |
| 186 |
goto err_i2c_write; |
| 187 |
|
| 188 |
dev_dbg(&client->dev, "set up with %x keys.\n", mpr121->keycount); |
| 189 |
|
| 190 |
return 0; |
| 191 |
|
| 192 |
err_i2c_write: |
| 193 |
dev_err(&client->dev, "i2c write error: %d\n", ret); |
| 194 |
return ret; |
| 195 |
} |
| 196 |
|
| 197 |
static int mpr_touchkey_probe(struct i2c_client *client) |
| 198 |
{ |
| 199 |
struct device *dev = &client->dev; |
| 200 |
int vdd_uv; |
| 201 |
struct mpr121_touchkey *mpr121; |
| 202 |
struct input_dev *input_dev; |
| 203 |
u32 poll_interval = 0; |
| 204 |
int error; |
| 205 |
int i; |
| 206 |
|
| 207 |
vdd_uv = devm_regulator_get_enable_read_voltage(dev, "vdd"); |
| 208 |
if (vdd_uv < 0) |
| 209 |
return dev_err_probe(dev, vdd_uv, "failed to get vdd voltage\n"); |
| 210 |
|
| 211 |
mpr121 = devm_kzalloc(dev, sizeof(*mpr121), GFP_KERNEL); |
| 212 |
if (!mpr121) |
| 213 |
return -ENOMEM; |
| 214 |
|
| 215 |
input_dev = devm_input_allocate_device(dev); |
| 216 |
if (!input_dev) |
| 217 |
return -ENOMEM; |
| 218 |
|
| 219 |
mpr121->client = client; |
| 220 |
mpr121->input_dev = input_dev; |
| 221 |
mpr121->keycount = device_property_count_u32(dev, "linux,keycodes"); |
| 222 |
if (mpr121->keycount > MPR121_MAX_KEY_COUNT) { |
| 223 |
dev_err(dev, "too many keys defined (%d)\n", mpr121->keycount); |
| 224 |
return -EINVAL; |
| 225 |
} |
| 226 |
|
| 227 |
error = device_property_read_u32_array(dev, "linux,keycodes", |
| 228 |
mpr121->keycodes, |
| 229 |
mpr121->keycount); |
| 230 |
if (error) { |
| 231 |
dev_err(dev, |
| 232 |
"failed to read linux,keycode property: %d\n", error); |
| 233 |
return error; |
| 234 |
} |
| 235 |
|
| 236 |
input_dev->name = "Freescale MPR121 Touchkey"; |
| 237 |
input_dev->id.bustype = BUS_I2C; |
| 238 |
input_dev->dev.parent = dev; |
| 239 |
if (device_property_read_bool(dev, "autorepeat")) |
| 240 |
__set_bit(EV_REP, input_dev->evbit); |
| 241 |
input_set_capability(input_dev, EV_MSC, MSC_SCAN); |
| 242 |
input_set_drvdata(input_dev, mpr121); |
| 243 |
|
| 244 |
input_dev->keycode = mpr121->keycodes; |
| 245 |
input_dev->keycodesize = sizeof(mpr121->keycodes[0]); |
| 246 |
input_dev->keycodemax = mpr121->keycount; |
| 247 |
|
| 248 |
for (i = 0; i < mpr121->keycount; i++) |
| 249 |
input_set_capability(input_dev, EV_KEY, mpr121->keycodes[i]); |
| 250 |
|
| 251 |
error = mpr121_phys_init(mpr121, client, vdd_uv); |
| 252 |
if (error) { |
| 253 |
dev_err(dev, "Failed to init register\n"); |
| 254 |
return error; |
| 255 |
} |
| 256 |
|
| 257 |
device_property_read_u32(dev, "poll-interval", &poll_interval); |
| 258 |
|
| 259 |
if (client->irq) { |
| 260 |
error = devm_request_threaded_irq(dev, client->irq, NULL, |
| 261 |
mpr_touchkey_interrupt, |
| 262 |
IRQF_TRIGGER_FALLING | |
| 263 |
IRQF_ONESHOT, |
| 264 |
dev->driver->name, mpr121); |
| 265 |
if (error) { |
| 266 |
dev_err(dev, "Failed to register interrupt\n"); |
| 267 |
return error; |
| 268 |
} |
| 269 |
} else if (poll_interval) { |
| 270 |
if (poll_interval < MPR121_MIN_POLL_INTERVAL) |
| 271 |
return -EINVAL; |
| 272 |
|
| 273 |
if (poll_interval > MPR121_MAX_POLL_INTERVAL) |
| 274 |
return -EINVAL; |
| 275 |
|
| 276 |
error = input_setup_polling(input_dev, mpr_touchkey_report); |
| 277 |
if (error) { |
| 278 |
dev_err(dev, "Failed to setup polling\n"); |
| 279 |
return error; |
| 280 |
} |
| 281 |
|
| 282 |
input_set_poll_interval(input_dev, poll_interval); |
| 283 |
input_set_min_poll_interval(input_dev, |
| 284 |
MPR121_MIN_POLL_INTERVAL); |
| 285 |
input_set_max_poll_interval(input_dev, |
| 286 |
MPR121_MAX_POLL_INTERVAL); |
| 287 |
} else { |
| 288 |
dev_err(dev, |
| 289 |
"invalid IRQ number and polling not configured\n"); |
| 290 |
return -EINVAL; |
| 291 |
} |
| 292 |
|
| 293 |
error = input_register_device(input_dev); |
| 294 |
if (error) |
| 295 |
return error; |
| 296 |
|
| 297 |
i2c_set_clientdata(client, mpr121); |
| 298 |
device_init_wakeup(dev, |
| 299 |
device_property_read_bool(dev, "wakeup-source")); |
| 300 |
|
| 301 |
return 0; |
| 302 |
} |
| 303 |
|
| 304 |
static int mpr_suspend(struct device *dev) |
| 305 |
{ |
| 306 |
struct i2c_client *client = to_i2c_client(dev); |
| 307 |
|
| 308 |
if (device_may_wakeup(&client->dev)) |
| 309 |
enable_irq_wake(client->irq); |
| 310 |
|
| 311 |
i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR, 0x00); |
| 312 |
|
| 313 |
return 0; |
| 314 |
} |
| 315 |
|
| 316 |
static int mpr_resume(struct device *dev) |
| 317 |
{ |
| 318 |
struct i2c_client *client = to_i2c_client(dev); |
| 319 |
struct mpr121_touchkey *mpr121 = i2c_get_clientdata(client); |
| 320 |
|
| 321 |
if (device_may_wakeup(&client->dev)) |
| 322 |
disable_irq_wake(client->irq); |
| 323 |
|
| 324 |
i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR, |
| 325 |
mpr121->keycount); |
| 326 |
|
| 327 |
return 0; |
| 328 |
} |
| 329 |
|
| 330 |
static DEFINE_SIMPLE_DEV_PM_OPS(mpr121_touchkey_pm_ops, mpr_suspend, mpr_resume); |
| 331 |
|
| 332 |
static const struct i2c_device_id mpr121_id[] = { |
| 333 |
{ "mpr121_touchkey" }, |
| 334 |
{ } |
| 335 |
}; |
| 336 |
MODULE_DEVICE_TABLE(i2c, mpr121_id); |
| 337 |
|
| 338 |
#ifdef CONFIG_OF |
| 339 |
static const struct of_device_id mpr121_touchkey_dt_match_table[] = { |
| 340 |
{ .compatible = "fsl,mpr121-touchkey" }, |
| 341 |
{ }, |
| 342 |
}; |
| 343 |
MODULE_DEVICE_TABLE(of, mpr121_touchkey_dt_match_table); |
| 344 |
#endif |
| 345 |
|
| 346 |
static struct i2c_driver mpr_touchkey_driver = { |
| 347 |
.driver = { |
| 348 |
.name = "mpr121", |
| 349 |
.pm = pm_sleep_ptr(&mpr121_touchkey_pm_ops), |
| 350 |
.of_match_table = of_match_ptr(mpr121_touchkey_dt_match_table), |
| 351 |
}, |
| 352 |
.id_table = mpr121_id, |
| 353 |
.probe = mpr_touchkey_probe, |
| 354 |
}; |
| 355 |
|
| 356 |
module_i2c_driver(mpr_touchkey_driver); |
| 357 |
|
| 358 |
MODULE_LICENSE("GPL"); |
| 359 |
MODULE_AUTHOR("Zhang Jiejing <[email protected]>"); |
| 360 |
MODULE_DESCRIPTION("Touch Key driver for Freescale MPR121 Chip"); |