blob: 884a74d8a7edf50b26f9e62a4ce731d77d9f2c8e [file] [log] [blame]
Zhang Jiejing36682b82011-05-11 14:32:01 -07001/*
2 * Touchkey driver for Freescale MPR121 Controllor
3 *
4 * Copyright (C) 2011 Freescale Semiconductor, Inc.
5 * Author: Zhang Jiejing <jiejing.zhang@freescale.com>
6 *
7 * Based on mcs_touchkey.c
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 */
14
Zhang Jiejing36682b82011-05-11 14:32:01 -070015#include <linux/bitops.h>
Akinobu Mitade901cc2017-01-15 14:51:12 -080016#include <linux/delay.h>
17#include <linux/i2c.h>
18#include <linux/input.h>
Zhang Jiejing36682b82011-05-11 14:32:01 -070019#include <linux/interrupt.h>
Akinobu Mitade901cc2017-01-15 14:51:12 -080020#include <linux/module.h>
21#include <linux/of.h>
22#include <linux/property.h>
23#include <linux/regulator/consumer.h>
24#include <linux/slab.h>
Zhang Jiejing36682b82011-05-11 14:32:01 -070025
26/* Register definitions */
27#define ELE_TOUCH_STATUS_0_ADDR 0x0
28#define ELE_TOUCH_STATUS_1_ADDR 0X1
29#define MHD_RISING_ADDR 0x2b
30#define NHD_RISING_ADDR 0x2c
31#define NCL_RISING_ADDR 0x2d
32#define FDL_RISING_ADDR 0x2e
33#define MHD_FALLING_ADDR 0x2f
34#define NHD_FALLING_ADDR 0x30
35#define NCL_FALLING_ADDR 0x31
36#define FDL_FALLING_ADDR 0x32
37#define ELE0_TOUCH_THRESHOLD_ADDR 0x41
38#define ELE0_RELEASE_THRESHOLD_ADDR 0x42
39#define AFE_CONF_ADDR 0x5c
40#define FILTER_CONF_ADDR 0x5d
41
42/*
43 * ELECTRODE_CONF_ADDR: This register configures the number of
44 * enabled capacitance sensing inputs and its run/suspend mode.
45 */
46#define ELECTRODE_CONF_ADDR 0x5e
Jiejing Zhangc7b4a5d2011-06-29 01:28:21 -070047#define ELECTRODE_CONF_QUICK_CHARGE 0x80
Zhang Jiejing36682b82011-05-11 14:32:01 -070048#define AUTO_CONFIG_CTRL_ADDR 0x7b
49#define AUTO_CONFIG_USL_ADDR 0x7d
50#define AUTO_CONFIG_LSL_ADDR 0x7e
51#define AUTO_CONFIG_TL_ADDR 0x7f
52
53/* Threshold of touch/release trigger */
Jiejing Zhangc7b4a5d2011-06-29 01:28:21 -070054#define TOUCH_THRESHOLD 0x08
55#define RELEASE_THRESHOLD 0x05
Zhang Jiejing36682b82011-05-11 14:32:01 -070056/* Masks for touch and release triggers */
57#define TOUCH_STATUS_MASK 0xfff
58/* MPR121 has 12 keys */
59#define MPR121_MAX_KEY_COUNT 12
60
61struct mpr121_touchkey {
62 struct i2c_client *client;
63 struct input_dev *input_dev;
Zhang Jiejing36682b82011-05-11 14:32:01 -070064 unsigned int statusbits;
65 unsigned int keycount;
Akinobu Mitade901cc2017-01-15 14:51:12 -080066 u32 keycodes[MPR121_MAX_KEY_COUNT];
Zhang Jiejing36682b82011-05-11 14:32:01 -070067};
68
69struct mpr121_init_register {
70 int addr;
71 u8 val;
72};
73
Bill Pemberton78f50c22012-11-23 21:31:00 -080074static const struct mpr121_init_register init_reg_table[] = {
Zhang Jiejing36682b82011-05-11 14:32:01 -070075 { MHD_RISING_ADDR, 0x1 },
76 { NHD_RISING_ADDR, 0x1 },
77 { MHD_FALLING_ADDR, 0x1 },
78 { NHD_FALLING_ADDR, 0x1 },
79 { NCL_FALLING_ADDR, 0xff },
80 { FDL_FALLING_ADDR, 0x02 },
81 { FILTER_CONF_ADDR, 0x04 },
82 { AFE_CONF_ADDR, 0x0b },
83 { AUTO_CONFIG_CTRL_ADDR, 0x0b },
84};
85
Akinobu Mitade901cc2017-01-15 14:51:12 -080086static void mpr121_vdd_supply_disable(void *data)
87{
88 struct regulator *vdd_supply = data;
89
90 regulator_disable(vdd_supply);
91}
92
93static struct regulator *mpr121_vdd_supply_init(struct device *dev)
94{
95 struct regulator *vdd_supply;
96 int err;
97
98 vdd_supply = devm_regulator_get(dev, "vdd");
99 if (IS_ERR(vdd_supply)) {
100 dev_err(dev, "failed to get vdd regulator: %ld\n",
101 PTR_ERR(vdd_supply));
102 return vdd_supply;
103 }
104
105 err = regulator_enable(vdd_supply);
106 if (err) {
107 dev_err(dev, "failed to enable vdd regulator: %d\n", err);
108 return ERR_PTR(err);
109 }
110
111 err = devm_add_action(dev, mpr121_vdd_supply_disable, vdd_supply);
112 if (err) {
113 regulator_disable(vdd_supply);
114 dev_err(dev, "failed to add disable regulator action: %d\n",
115 err);
116 return ERR_PTR(err);
117 }
118
119 return vdd_supply;
120}
121
Zhang Jiejing36682b82011-05-11 14:32:01 -0700122static irqreturn_t mpr_touchkey_interrupt(int irq, void *dev_id)
123{
124 struct mpr121_touchkey *mpr121 = dev_id;
125 struct i2c_client *client = mpr121->client;
126 struct input_dev *input = mpr121->input_dev;
Akinobu Mita08fea552017-01-15 14:44:30 -0800127 unsigned long bit_changed;
128 unsigned int key_num;
Zhang Jiejing36682b82011-05-11 14:32:01 -0700129 int reg;
130
131 reg = i2c_smbus_read_byte_data(client, ELE_TOUCH_STATUS_1_ADDR);
132 if (reg < 0) {
133 dev_err(&client->dev, "i2c read error [%d]\n", reg);
134 goto out;
135 }
136
137 reg <<= 8;
138 reg |= i2c_smbus_read_byte_data(client, ELE_TOUCH_STATUS_0_ADDR);
139 if (reg < 0) {
140 dev_err(&client->dev, "i2c read error [%d]\n", reg);
141 goto out;
142 }
143
144 reg &= TOUCH_STATUS_MASK;
145 /* use old press bit to figure out which bit changed */
Akinobu Mita08fea552017-01-15 14:44:30 -0800146 bit_changed = reg ^ mpr121->statusbits;
Zhang Jiejing36682b82011-05-11 14:32:01 -0700147 mpr121->statusbits = reg;
Akinobu Mita08fea552017-01-15 14:44:30 -0800148 for_each_set_bit(key_num, &bit_changed, mpr121->keycount) {
149 unsigned int key_val, pressed;
Zhang Jiejing36682b82011-05-11 14:32:01 -0700150
Akinobu Mita08fea552017-01-15 14:44:30 -0800151 pressed = reg & BIT(key_num);
152 key_val = mpr121->keycodes[key_num];
Zhang Jiejing36682b82011-05-11 14:32:01 -0700153
Akinobu Mita08fea552017-01-15 14:44:30 -0800154 input_event(input, EV_MSC, MSC_SCAN, key_num);
155 input_report_key(input, key_val, pressed);
156
157 dev_dbg(&client->dev, "key %d %d %s\n", key_num, key_val,
158 pressed ? "pressed" : "released");
159
160 }
Zhang Jiejing36682b82011-05-11 14:32:01 -0700161 input_sync(input);
162
Zhang Jiejing36682b82011-05-11 14:32:01 -0700163out:
164 return IRQ_HANDLED;
165}
166
Akinobu Mitade901cc2017-01-15 14:51:12 -0800167static int mpr121_phys_init(struct mpr121_touchkey *mpr121,
168 struct i2c_client *client, int vdd_uv)
Zhang Jiejing36682b82011-05-11 14:32:01 -0700169{
170 const struct mpr121_init_register *reg;
Jiejing Zhangc7b4a5d2011-06-29 01:28:21 -0700171 unsigned char usl, lsl, tl, eleconf;
Zhang Jiejing36682b82011-05-11 14:32:01 -0700172 int i, t, vdd, ret;
173
174 /* Set up touch/release threshold for ele0-ele11 */
175 for (i = 0; i <= MPR121_MAX_KEY_COUNT; i++) {
176 t = ELE0_TOUCH_THRESHOLD_ADDR + (i * 2);
177 ret = i2c_smbus_write_byte_data(client, t, TOUCH_THRESHOLD);
178 if (ret < 0)
179 goto err_i2c_write;
180 ret = i2c_smbus_write_byte_data(client, t + 1,
181 RELEASE_THRESHOLD);
182 if (ret < 0)
183 goto err_i2c_write;
184 }
185
186 /* Set up init register */
187 for (i = 0; i < ARRAY_SIZE(init_reg_table); i++) {
188 reg = &init_reg_table[i];
189 ret = i2c_smbus_write_byte_data(client, reg->addr, reg->val);
190 if (ret < 0)
191 goto err_i2c_write;
192 }
193
194
195 /*
196 * Capacitance on sensing input varies and needs to be compensated.
197 * The internal MPR121-auto-configuration can do this if it's
Akinobu Mitade901cc2017-01-15 14:51:12 -0800198 * registers are set properly (based on vdd_uv).
Zhang Jiejing36682b82011-05-11 14:32:01 -0700199 */
Akinobu Mitade901cc2017-01-15 14:51:12 -0800200 vdd = vdd_uv / 1000;
Zhang Jiejing36682b82011-05-11 14:32:01 -0700201 usl = ((vdd - 700) * 256) / vdd;
202 lsl = (usl * 65) / 100;
203 tl = (usl * 90) / 100;
204 ret = i2c_smbus_write_byte_data(client, AUTO_CONFIG_USL_ADDR, usl);
205 ret |= i2c_smbus_write_byte_data(client, AUTO_CONFIG_LSL_ADDR, lsl);
206 ret |= i2c_smbus_write_byte_data(client, AUTO_CONFIG_TL_ADDR, tl);
Jiejing Zhangc7b4a5d2011-06-29 01:28:21 -0700207
208 /*
209 * Quick charge bit will let the capacitive charge to ready
210 * state quickly, or the buttons may not function after system
211 * boot.
212 */
213 eleconf = mpr121->keycount | ELECTRODE_CONF_QUICK_CHARGE;
Zhang Jiejing36682b82011-05-11 14:32:01 -0700214 ret |= i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR,
Jiejing Zhangc7b4a5d2011-06-29 01:28:21 -0700215 eleconf);
Zhang Jiejing36682b82011-05-11 14:32:01 -0700216 if (ret != 0)
217 goto err_i2c_write;
218
219 dev_dbg(&client->dev, "set up with %x keys.\n", mpr121->keycount);
220
221 return 0;
222
223err_i2c_write:
224 dev_err(&client->dev, "i2c write error: %d\n", ret);
225 return ret;
226}
227
Bill Pemberton5298cc42012-11-23 21:38:25 -0800228static int mpr_touchkey_probe(struct i2c_client *client,
229 const struct i2c_device_id *id)
Zhang Jiejing36682b82011-05-11 14:32:01 -0700230{
Akinobu Mitade901cc2017-01-15 14:51:12 -0800231 struct device *dev = &client->dev;
232 struct regulator *vdd_supply;
233 int vdd_uv;
Zhang Jiejing36682b82011-05-11 14:32:01 -0700234 struct mpr121_touchkey *mpr121;
235 struct input_dev *input_dev;
236 int error;
237 int i;
238
Zhang Jiejing36682b82011-05-11 14:32:01 -0700239 if (!client->irq) {
Guenter Roeckb4e66e72017-01-21 23:40:45 -0800240 dev_err(dev, "irq number should not be zero\n");
Zhang Jiejing36682b82011-05-11 14:32:01 -0700241 return -EINVAL;
242 }
243
Akinobu Mitade901cc2017-01-15 14:51:12 -0800244 vdd_supply = mpr121_vdd_supply_init(dev);
245 if (IS_ERR(vdd_supply))
246 return PTR_ERR(vdd_supply);
247
248 vdd_uv = regulator_get_voltage(vdd_supply);
249
Guenter Roeckb4e66e72017-01-21 23:40:45 -0800250 mpr121 = devm_kzalloc(dev, sizeof(*mpr121), GFP_KERNEL);
Pramod Gurav254af0a2014-10-08 11:14:10 -0700251 if (!mpr121)
252 return -ENOMEM;
253
Guenter Roeckb4e66e72017-01-21 23:40:45 -0800254 input_dev = devm_input_allocate_device(dev);
Pramod Gurav254af0a2014-10-08 11:14:10 -0700255 if (!input_dev)
256 return -ENOMEM;
Zhang Jiejing36682b82011-05-11 14:32:01 -0700257
258 mpr121->client = client;
259 mpr121->input_dev = input_dev;
Akinobu Mitade901cc2017-01-15 14:51:12 -0800260 mpr121->keycount = device_property_read_u32_array(dev, "linux,keycodes",
261 NULL, 0);
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 }
Zhang Jiejing36682b82011-05-11 14:32:01 -0700275
276 input_dev->name = "Freescale MPR121 Touchkey";
277 input_dev->id.bustype = BUS_I2C;
Guenter Roeckb4e66e72017-01-21 23:40:45 -0800278 input_dev->dev.parent = dev;
Akinobu Mitade901cc2017-01-15 14:51:12 -0800279 if (device_property_read_bool(dev, "autorepeat"))
280 __set_bit(EV_REP, input_dev->evbit);
Akinobu Mita9723ddc2017-01-15 14:44:05 -0800281 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
Zhang Jiejing36682b82011-05-11 14:32:01 -0700282
283 input_dev->keycode = mpr121->keycodes;
284 input_dev->keycodesize = sizeof(mpr121->keycodes[0]);
285 input_dev->keycodemax = mpr121->keycount;
286
Akinobu Mitade901cc2017-01-15 14:51:12 -0800287 for (i = 0; i < mpr121->keycount; i++)
288 input_set_capability(input_dev, EV_KEY, mpr121->keycodes[i]);
Zhang Jiejing36682b82011-05-11 14:32:01 -0700289
Akinobu Mitade901cc2017-01-15 14:51:12 -0800290 error = mpr121_phys_init(mpr121, client, vdd_uv);
Zhang Jiejing36682b82011-05-11 14:32:01 -0700291 if (error) {
Guenter Roeckb4e66e72017-01-21 23:40:45 -0800292 dev_err(dev, "Failed to init register\n");
Pramod Gurav254af0a2014-10-08 11:14:10 -0700293 return error;
Zhang Jiejing36682b82011-05-11 14:32:01 -0700294 }
295
Guenter Roeckb4e66e72017-01-21 23:40:45 -0800296 error = devm_request_threaded_irq(dev, client->irq, NULL,
297 mpr_touchkey_interrupt,
Akinobu Mitade901cc2017-01-15 14:51:12 -0800298 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
Guenter Roeckb4e66e72017-01-21 23:40:45 -0800299 dev->driver->name, mpr121);
Zhang Jiejing36682b82011-05-11 14:32:01 -0700300 if (error) {
Guenter Roeckb4e66e72017-01-21 23:40:45 -0800301 dev_err(dev, "Failed to register interrupt\n");
Pramod Gurav254af0a2014-10-08 11:14:10 -0700302 return error;
Zhang Jiejing36682b82011-05-11 14:32:01 -0700303 }
304
305 error = input_register_device(input_dev);
306 if (error)
Pramod Gurav254af0a2014-10-08 11:14:10 -0700307 return error;
Zhang Jiejing36682b82011-05-11 14:32:01 -0700308
309 i2c_set_clientdata(client, mpr121);
Akinobu Mitade901cc2017-01-15 14:51:12 -0800310 device_init_wakeup(dev,
311 device_property_read_bool(dev, "wakeup-source"));
Zhang Jiejing36682b82011-05-11 14:32:01 -0700312
313 return 0;
Zhang Jiejing36682b82011-05-11 14:32:01 -0700314}
315
Akinobu Mita890fa162017-01-15 14:43:15 -0800316static int __maybe_unused mpr_suspend(struct device *dev)
Zhang Jiejing36682b82011-05-11 14:32:01 -0700317{
318 struct i2c_client *client = to_i2c_client(dev);
319
320 if (device_may_wakeup(&client->dev))
321 enable_irq_wake(client->irq);
322
323 i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR, 0x00);
324
325 return 0;
326}
327
Akinobu Mita890fa162017-01-15 14:43:15 -0800328static int __maybe_unused mpr_resume(struct device *dev)
Zhang Jiejing36682b82011-05-11 14:32:01 -0700329{
330 struct i2c_client *client = to_i2c_client(dev);
331 struct mpr121_touchkey *mpr121 = i2c_get_clientdata(client);
332
333 if (device_may_wakeup(&client->dev))
334 disable_irq_wake(client->irq);
335
336 i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR,
337 mpr121->keycount);
338
339 return 0;
340}
Zhang Jiejing36682b82011-05-11 14:32:01 -0700341
342static SIMPLE_DEV_PM_OPS(mpr121_touchkey_pm_ops, mpr_suspend, mpr_resume);
343
344static const struct i2c_device_id mpr121_id[] = {
345 { "mpr121_touchkey", 0 },
346 { }
347};
348MODULE_DEVICE_TABLE(i2c, mpr121_id);
349
Akinobu Mitade901cc2017-01-15 14:51:12 -0800350#ifdef CONFIG_OF
351static const struct of_device_id mpr121_touchkey_dt_match_table[] = {
352 { .compatible = "fsl,mpr121-touchkey" },
353 { },
354};
355MODULE_DEVICE_TABLE(of, mpr121_touchkey_dt_match_table);
356#endif
357
Zhang Jiejing36682b82011-05-11 14:32:01 -0700358static struct i2c_driver mpr_touchkey_driver = {
359 .driver = {
360 .name = "mpr121",
Zhang Jiejing36682b82011-05-11 14:32:01 -0700361 .pm = &mpr121_touchkey_pm_ops,
Akinobu Mitade901cc2017-01-15 14:51:12 -0800362 .of_match_table = of_match_ptr(mpr121_touchkey_dt_match_table),
Zhang Jiejing36682b82011-05-11 14:32:01 -0700363 },
364 .id_table = mpr121_id,
365 .probe = mpr_touchkey_probe,
Zhang Jiejing36682b82011-05-11 14:32:01 -0700366};
367
Axel Lin1b92c1c2012-03-16 23:05:41 -0700368module_i2c_driver(mpr_touchkey_driver);
Zhang Jiejing36682b82011-05-11 14:32:01 -0700369
370MODULE_LICENSE("GPL");
371MODULE_AUTHOR("Zhang Jiejing <jiejing.zhang@freescale.com>");
372MODULE_DESCRIPTION("Touch Key driver for Freescale MPR121 Chip");