0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/bitops.h>
0012 #include <linux/delay.h>
0013 #include <linux/i2c.h>
0014 #include <linux/input.h>
0015 #include <linux/interrupt.h>
0016 #include <linux/module.h>
0017 #include <linux/of.h>
0018 #include <linux/property.h>
0019 #include <linux/regulator/consumer.h>
0020 #include <linux/slab.h>
0021
0022
0023 #define ELE_TOUCH_STATUS_0_ADDR 0x0
0024 #define ELE_TOUCH_STATUS_1_ADDR 0X1
0025 #define MHD_RISING_ADDR 0x2b
0026 #define NHD_RISING_ADDR 0x2c
0027 #define NCL_RISING_ADDR 0x2d
0028 #define FDL_RISING_ADDR 0x2e
0029 #define MHD_FALLING_ADDR 0x2f
0030 #define NHD_FALLING_ADDR 0x30
0031 #define NCL_FALLING_ADDR 0x31
0032 #define FDL_FALLING_ADDR 0x32
0033 #define ELE0_TOUCH_THRESHOLD_ADDR 0x41
0034 #define ELE0_RELEASE_THRESHOLD_ADDR 0x42
0035 #define AFE_CONF_ADDR 0x5c
0036 #define FILTER_CONF_ADDR 0x5d
0037
0038
0039
0040
0041
0042 #define ELECTRODE_CONF_ADDR 0x5e
0043 #define ELECTRODE_CONF_QUICK_CHARGE 0x80
0044 #define AUTO_CONFIG_CTRL_ADDR 0x7b
0045 #define AUTO_CONFIG_USL_ADDR 0x7d
0046 #define AUTO_CONFIG_LSL_ADDR 0x7e
0047 #define AUTO_CONFIG_TL_ADDR 0x7f
0048
0049
0050 #define TOUCH_THRESHOLD 0x08
0051 #define RELEASE_THRESHOLD 0x05
0052
0053 #define TOUCH_STATUS_MASK 0xfff
0054
0055 #define MPR121_MAX_KEY_COUNT 12
0056
0057 #define MPR121_MIN_POLL_INTERVAL 10
0058 #define MPR121_MAX_POLL_INTERVAL 200
0059
0060 struct mpr121_touchkey {
0061 struct i2c_client *client;
0062 struct input_dev *input_dev;
0063 unsigned int statusbits;
0064 unsigned int keycount;
0065 u32 keycodes[MPR121_MAX_KEY_COUNT];
0066 };
0067
0068 struct mpr121_init_register {
0069 int addr;
0070 u8 val;
0071 };
0072
0073 static const struct mpr121_init_register init_reg_table[] = {
0074 { MHD_RISING_ADDR, 0x1 },
0075 { NHD_RISING_ADDR, 0x1 },
0076 { MHD_FALLING_ADDR, 0x1 },
0077 { NHD_FALLING_ADDR, 0x1 },
0078 { NCL_FALLING_ADDR, 0xff },
0079 { FDL_FALLING_ADDR, 0x02 },
0080 { FILTER_CONF_ADDR, 0x04 },
0081 { AFE_CONF_ADDR, 0x0b },
0082 { AUTO_CONFIG_CTRL_ADDR, 0x0b },
0083 };
0084
0085 static void mpr121_vdd_supply_disable(void *data)
0086 {
0087 struct regulator *vdd_supply = data;
0088
0089 regulator_disable(vdd_supply);
0090 }
0091
0092 static struct regulator *mpr121_vdd_supply_init(struct device *dev)
0093 {
0094 struct regulator *vdd_supply;
0095 int err;
0096
0097 vdd_supply = devm_regulator_get(dev, "vdd");
0098 if (IS_ERR(vdd_supply)) {
0099 dev_err(dev, "failed to get vdd regulator: %ld\n",
0100 PTR_ERR(vdd_supply));
0101 return vdd_supply;
0102 }
0103
0104 err = regulator_enable(vdd_supply);
0105 if (err) {
0106 dev_err(dev, "failed to enable vdd regulator: %d\n", err);
0107 return ERR_PTR(err);
0108 }
0109
0110 err = devm_add_action_or_reset(dev, mpr121_vdd_supply_disable,
0111 vdd_supply);
0112 if (err) {
0113 dev_err(dev, "failed to add disable regulator action: %d\n",
0114 err);
0115 return ERR_PTR(err);
0116 }
0117
0118 return vdd_supply;
0119 }
0120
0121 static void mpr_touchkey_report(struct input_dev *dev)
0122 {
0123 struct mpr121_touchkey *mpr121 = input_get_drvdata(dev);
0124 struct input_dev *input = mpr121->input_dev;
0125 struct i2c_client *client = mpr121->client;
0126 unsigned long bit_changed;
0127 unsigned int key_num;
0128 int reg;
0129
0130 reg = i2c_smbus_read_byte_data(client, ELE_TOUCH_STATUS_1_ADDR);
0131 if (reg < 0) {
0132 dev_err(&client->dev, "i2c read error [%d]\n", reg);
0133 return;
0134 }
0135
0136 reg <<= 8;
0137 reg |= i2c_smbus_read_byte_data(client, ELE_TOUCH_STATUS_0_ADDR);
0138 if (reg < 0) {
0139 dev_err(&client->dev, "i2c read error [%d]\n", reg);
0140 return;
0141 }
0142
0143 reg &= TOUCH_STATUS_MASK;
0144
0145 bit_changed = reg ^ mpr121->statusbits;
0146 mpr121->statusbits = reg;
0147 for_each_set_bit(key_num, &bit_changed, mpr121->keycount) {
0148 unsigned int key_val, pressed;
0149
0150 pressed = reg & BIT(key_num);
0151 key_val = mpr121->keycodes[key_num];
0152
0153 input_event(input, EV_MSC, MSC_SCAN, key_num);
0154 input_report_key(input, key_val, pressed);
0155
0156 dev_dbg(&client->dev, "key %d %d %s\n", key_num, key_val,
0157 pressed ? "pressed" : "released");
0158
0159 }
0160 input_sync(input);
0161 }
0162
0163 static irqreturn_t mpr_touchkey_interrupt(int irq, void *dev_id)
0164 {
0165 struct mpr121_touchkey *mpr121 = dev_id;
0166
0167 mpr_touchkey_report(mpr121->input_dev);
0168
0169 return IRQ_HANDLED;
0170 }
0171
0172 static int mpr121_phys_init(struct mpr121_touchkey *mpr121,
0173 struct i2c_client *client, int vdd_uv)
0174 {
0175 const struct mpr121_init_register *reg;
0176 unsigned char usl, lsl, tl, eleconf;
0177 int i, t, vdd, ret;
0178
0179
0180 for (i = 0; i <= MPR121_MAX_KEY_COUNT; i++) {
0181 t = ELE0_TOUCH_THRESHOLD_ADDR + (i * 2);
0182 ret = i2c_smbus_write_byte_data(client, t, TOUCH_THRESHOLD);
0183 if (ret < 0)
0184 goto err_i2c_write;
0185 ret = i2c_smbus_write_byte_data(client, t + 1,
0186 RELEASE_THRESHOLD);
0187 if (ret < 0)
0188 goto err_i2c_write;
0189 }
0190
0191
0192 for (i = 0; i < ARRAY_SIZE(init_reg_table); i++) {
0193 reg = &init_reg_table[i];
0194 ret = i2c_smbus_write_byte_data(client, reg->addr, reg->val);
0195 if (ret < 0)
0196 goto err_i2c_write;
0197 }
0198
0199
0200
0201
0202
0203
0204
0205 vdd = vdd_uv / 1000;
0206 usl = ((vdd - 700) * 256) / vdd;
0207 lsl = (usl * 65) / 100;
0208 tl = (usl * 90) / 100;
0209 ret = i2c_smbus_write_byte_data(client, AUTO_CONFIG_USL_ADDR, usl);
0210 ret |= i2c_smbus_write_byte_data(client, AUTO_CONFIG_LSL_ADDR, lsl);
0211 ret |= i2c_smbus_write_byte_data(client, AUTO_CONFIG_TL_ADDR, tl);
0212
0213
0214
0215
0216
0217
0218 eleconf = mpr121->keycount | ELECTRODE_CONF_QUICK_CHARGE;
0219 ret |= i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR,
0220 eleconf);
0221 if (ret != 0)
0222 goto err_i2c_write;
0223
0224 dev_dbg(&client->dev, "set up with %x keys.\n", mpr121->keycount);
0225
0226 return 0;
0227
0228 err_i2c_write:
0229 dev_err(&client->dev, "i2c write error: %d\n", ret);
0230 return ret;
0231 }
0232
0233 static int mpr_touchkey_probe(struct i2c_client *client,
0234 const struct i2c_device_id *id)
0235 {
0236 struct device *dev = &client->dev;
0237 struct regulator *vdd_supply;
0238 int vdd_uv;
0239 struct mpr121_touchkey *mpr121;
0240 struct input_dev *input_dev;
0241 u32 poll_interval = 0;
0242 int error;
0243 int i;
0244
0245 vdd_supply = mpr121_vdd_supply_init(dev);
0246 if (IS_ERR(vdd_supply))
0247 return PTR_ERR(vdd_supply);
0248
0249 vdd_uv = regulator_get_voltage(vdd_supply);
0250
0251 mpr121 = devm_kzalloc(dev, sizeof(*mpr121), GFP_KERNEL);
0252 if (!mpr121)
0253 return -ENOMEM;
0254
0255 input_dev = devm_input_allocate_device(dev);
0256 if (!input_dev)
0257 return -ENOMEM;
0258
0259 mpr121->client = client;
0260 mpr121->input_dev = input_dev;
0261 mpr121->keycount = device_property_count_u32(dev, "linux,keycodes");
0262 if (mpr121->keycount > MPR121_MAX_KEY_COUNT) {
0263 dev_err(dev, "too many keys defined (%d)\n", mpr121->keycount);
0264 return -EINVAL;
0265 }
0266
0267 error = device_property_read_u32_array(dev, "linux,keycodes",
0268 mpr121->keycodes,
0269 mpr121->keycount);
0270 if (error) {
0271 dev_err(dev,
0272 "failed to read linux,keycode property: %d\n", error);
0273 return error;
0274 }
0275
0276 input_dev->name = "Freescale MPR121 Touchkey";
0277 input_dev->id.bustype = BUS_I2C;
0278 input_dev->dev.parent = dev;
0279 if (device_property_read_bool(dev, "autorepeat"))
0280 __set_bit(EV_REP, input_dev->evbit);
0281 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
0282 input_set_drvdata(input_dev, mpr121);
0283
0284 input_dev->keycode = mpr121->keycodes;
0285 input_dev->keycodesize = sizeof(mpr121->keycodes[0]);
0286 input_dev->keycodemax = mpr121->keycount;
0287
0288 for (i = 0; i < mpr121->keycount; i++)
0289 input_set_capability(input_dev, EV_KEY, mpr121->keycodes[i]);
0290
0291 error = mpr121_phys_init(mpr121, client, vdd_uv);
0292 if (error) {
0293 dev_err(dev, "Failed to init register\n");
0294 return error;
0295 }
0296
0297 device_property_read_u32(dev, "poll-interval", &poll_interval);
0298
0299 if (client->irq) {
0300 error = devm_request_threaded_irq(dev, client->irq, NULL,
0301 mpr_touchkey_interrupt,
0302 IRQF_TRIGGER_FALLING |
0303 IRQF_ONESHOT,
0304 dev->driver->name, mpr121);
0305 if (error) {
0306 dev_err(dev, "Failed to register interrupt\n");
0307 return error;
0308 }
0309 } else if (poll_interval) {
0310 if (poll_interval < MPR121_MIN_POLL_INTERVAL)
0311 return -EINVAL;
0312
0313 if (poll_interval > MPR121_MAX_POLL_INTERVAL)
0314 return -EINVAL;
0315
0316 error = input_setup_polling(input_dev, mpr_touchkey_report);
0317 if (error) {
0318 dev_err(dev, "Failed to setup polling\n");
0319 return error;
0320 }
0321
0322 input_set_poll_interval(input_dev, poll_interval);
0323 input_set_min_poll_interval(input_dev,
0324 MPR121_MIN_POLL_INTERVAL);
0325 input_set_max_poll_interval(input_dev,
0326 MPR121_MAX_POLL_INTERVAL);
0327 } else {
0328 dev_err(dev,
0329 "invalid IRQ number and polling not configured\n");
0330 return -EINVAL;
0331 }
0332
0333 error = input_register_device(input_dev);
0334 if (error)
0335 return error;
0336
0337 i2c_set_clientdata(client, mpr121);
0338 device_init_wakeup(dev,
0339 device_property_read_bool(dev, "wakeup-source"));
0340
0341 return 0;
0342 }
0343
0344 static int __maybe_unused mpr_suspend(struct device *dev)
0345 {
0346 struct i2c_client *client = to_i2c_client(dev);
0347
0348 if (device_may_wakeup(&client->dev))
0349 enable_irq_wake(client->irq);
0350
0351 i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR, 0x00);
0352
0353 return 0;
0354 }
0355
0356 static int __maybe_unused mpr_resume(struct device *dev)
0357 {
0358 struct i2c_client *client = to_i2c_client(dev);
0359 struct mpr121_touchkey *mpr121 = i2c_get_clientdata(client);
0360
0361 if (device_may_wakeup(&client->dev))
0362 disable_irq_wake(client->irq);
0363
0364 i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR,
0365 mpr121->keycount);
0366
0367 return 0;
0368 }
0369
0370 static SIMPLE_DEV_PM_OPS(mpr121_touchkey_pm_ops, mpr_suspend, mpr_resume);
0371
0372 static const struct i2c_device_id mpr121_id[] = {
0373 { "mpr121_touchkey", 0 },
0374 { }
0375 };
0376 MODULE_DEVICE_TABLE(i2c, mpr121_id);
0377
0378 #ifdef CONFIG_OF
0379 static const struct of_device_id mpr121_touchkey_dt_match_table[] = {
0380 { .compatible = "fsl,mpr121-touchkey" },
0381 { },
0382 };
0383 MODULE_DEVICE_TABLE(of, mpr121_touchkey_dt_match_table);
0384 #endif
0385
0386 static struct i2c_driver mpr_touchkey_driver = {
0387 .driver = {
0388 .name = "mpr121",
0389 .pm = &mpr121_touchkey_pm_ops,
0390 .of_match_table = of_match_ptr(mpr121_touchkey_dt_match_table),
0391 },
0392 .id_table = mpr121_id,
0393 .probe = mpr_touchkey_probe,
0394 };
0395
0396 module_i2c_driver(mpr_touchkey_driver);
0397
0398 MODULE_LICENSE("GPL");
0399 MODULE_AUTHOR("Zhang Jiejing <jiejing.zhang@freescale.com>");
0400 MODULE_DESCRIPTION("Touch Key driver for Freescale MPR121 Chip");