]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/input/keyboard/mpr121_touchkey.c
x86/msr-index: Cleanup bit defines
[mirror_ubuntu-bionic-kernel.git] / drivers / input / keyboard / mpr121_touchkey.c
CommitLineData
36682b81
ZJ
1/*
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
36682b81 15#include <linux/bitops.h>
de901cc3
AM
16#include <linux/delay.h>
17#include <linux/i2c.h>
18#include <linux/input.h>
36682b81 19#include <linux/interrupt.h>
de901cc3
AM
20#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>
36682b81
ZJ
25
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
c7b4a5d5 47#define ELECTRODE_CONF_QUICK_CHARGE 0x80
36682b81
ZJ
48#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 */
c7b4a5d5
JZ
54#define TOUCH_THRESHOLD 0x08
55#define RELEASE_THRESHOLD 0x05
36682b81
ZJ
56/* 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;
36682b81
ZJ
64 unsigned int statusbits;
65 unsigned int keycount;
de901cc3 66 u32 keycodes[MPR121_MAX_KEY_COUNT];
36682b81
ZJ
67};
68
69struct mpr121_init_register {
70 int addr;
71 u8 val;
72};
73
78f50c24 74static const struct mpr121_init_register init_reg_table[] = {
36682b81
ZJ
75 { 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
de901cc3
AM
86static 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
36682b81
ZJ
122static 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;
08fea55e
AM
127 unsigned long bit_changed;
128 unsigned int key_num;
36682b81
ZJ
129 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 */
08fea55e 146 bit_changed = reg ^ mpr121->statusbits;
36682b81 147 mpr121->statusbits = reg;
08fea55e
AM
148 for_each_set_bit(key_num, &bit_changed, mpr121->keycount) {
149 unsigned int key_val, pressed;
36682b81 150
08fea55e
AM
151 pressed = reg & BIT(key_num);
152 key_val = mpr121->keycodes[key_num];
36682b81 153
08fea55e
AM
154 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");
36682b81 159
08fea55e
AM
160 }
161 input_sync(input);
36682b81
ZJ
162
163out:
164 return IRQ_HANDLED;
165}
166
de901cc3
AM
167static int mpr121_phys_init(struct mpr121_touchkey *mpr121,
168 struct i2c_client *client, int vdd_uv)
36682b81
ZJ
169{
170 const struct mpr121_init_register *reg;
c7b4a5d5 171 unsigned char usl, lsl, tl, eleconf;
36682b81
ZJ
172 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
de901cc3 198 * registers are set properly (based on vdd_uv).
36682b81 199 */
de901cc3 200 vdd = vdd_uv / 1000;
36682b81
ZJ
201 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);
c7b4a5d5
JZ
207
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;
36682b81 214 ret |= i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR,
c7b4a5d5 215 eleconf);
36682b81
ZJ
216 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
5298cc4c
BP
228static int mpr_touchkey_probe(struct i2c_client *client,
229 const struct i2c_device_id *id)
36682b81 230{
de901cc3
AM
231 struct device *dev = &client->dev;
232 struct regulator *vdd_supply;
233 int vdd_uv;
36682b81
ZJ
234 struct mpr121_touchkey *mpr121;
235 struct input_dev *input_dev;
236 int error;
237 int i;
238
36682b81 239 if (!client->irq) {
b4e66e7d 240 dev_err(dev, "irq number should not be zero\n");
36682b81
ZJ
241 return -EINVAL;
242 }
243
de901cc3
AM
244 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
b4e66e7d 250 mpr121 = devm_kzalloc(dev, sizeof(*mpr121), GFP_KERNEL);
254af0a3
PG
251 if (!mpr121)
252 return -ENOMEM;
253
b4e66e7d 254 input_dev = devm_input_allocate_device(dev);
254af0a3
PG
255 if (!input_dev)
256 return -ENOMEM;
36682b81
ZJ
257
258 mpr121->client = client;
259 mpr121->input_dev = input_dev;
de901cc3
AM
260 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 }
36682b81
ZJ
275
276 input_dev->name = "Freescale MPR121 Touchkey";
277 input_dev->id.bustype = BUS_I2C;
b4e66e7d 278 input_dev->dev.parent = dev;
de901cc3
AM
279 if (device_property_read_bool(dev, "autorepeat"))
280 __set_bit(EV_REP, input_dev->evbit);
9723ddc8 281 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
36682b81
ZJ
282
283 input_dev->keycode = mpr121->keycodes;
284 input_dev->keycodesize = sizeof(mpr121->keycodes[0]);
285 input_dev->keycodemax = mpr121->keycount;
286
de901cc3
AM
287 for (i = 0; i < mpr121->keycount; i++)
288 input_set_capability(input_dev, EV_KEY, mpr121->keycodes[i]);
36682b81 289
de901cc3 290 error = mpr121_phys_init(mpr121, client, vdd_uv);
36682b81 291 if (error) {
b4e66e7d 292 dev_err(dev, "Failed to init register\n");
254af0a3 293 return error;
36682b81
ZJ
294 }
295
b4e66e7d
GR
296 error = devm_request_threaded_irq(dev, client->irq, NULL,
297 mpr_touchkey_interrupt,
de901cc3 298 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
b4e66e7d 299 dev->driver->name, mpr121);
36682b81 300 if (error) {
b4e66e7d 301 dev_err(dev, "Failed to register interrupt\n");
254af0a3 302 return error;
36682b81
ZJ
303 }
304
305 error = input_register_device(input_dev);
306 if (error)
254af0a3 307 return error;
36682b81
ZJ
308
309 i2c_set_clientdata(client, mpr121);
de901cc3
AM
310 device_init_wakeup(dev,
311 device_property_read_bool(dev, "wakeup-source"));
36682b81 312
36682b81
ZJ
313 return 0;
314}
315
890fa167 316static int __maybe_unused mpr_suspend(struct device *dev)
36682b81
ZJ
317{
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
890fa167 328static int __maybe_unused mpr_resume(struct device *dev)
36682b81
ZJ
329{
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}
36682b81
ZJ
341
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
de901cc3
AM
350#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
36682b81
ZJ
358static struct i2c_driver mpr_touchkey_driver = {
359 .driver = {
360 .name = "mpr121",
36682b81 361 .pm = &mpr121_touchkey_pm_ops,
de901cc3 362 .of_match_table = of_match_ptr(mpr121_touchkey_dt_match_table),
36682b81
ZJ
363 },
364 .id_table = mpr121_id,
365 .probe = mpr_touchkey_probe,
36682b81
ZJ
366};
367
1b92c1cf 368module_i2c_driver(mpr_touchkey_driver);
36682b81
ZJ
369
370MODULE_LICENSE("GPL");
371MODULE_AUTHOR("Zhang Jiejing <jiejing.zhang@freescale.com>");
372MODULE_DESCRIPTION("Touch Key driver for Freescale MPR121 Chip");