1 // SPDX-License-Identifier: GPL-2.0-only
3 * Touchkey driver for Freescale MPR121 Controllor
5 * Copyright (C) 2011 Freescale Semiconductor, Inc.
6 * Author: Zhang Jiejing <jiejing.zhang@freescale.com>
8 * Based on mcs_touchkey.c
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>
18 #include <linux/property.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/slab.h>
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
39 * ELECTRODE_CONF_ADDR: This register configures the number of
40 * enabled capacitance sensing inputs and its run/suspend mode.
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
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
57 struct mpr121_touchkey
{
58 struct i2c_client
*client
;
59 struct input_dev
*input_dev
;
60 unsigned int statusbits
;
61 unsigned int keycount
;
62 u32 keycodes
[MPR121_MAX_KEY_COUNT
];
65 struct mpr121_init_register
{
70 static const struct mpr121_init_register init_reg_table
[] = {
71 { MHD_RISING_ADDR
, 0x1 },
72 { NHD_RISING_ADDR
, 0x1 },
73 { MHD_FALLING_ADDR
, 0x1 },
74 { NHD_FALLING_ADDR
, 0x1 },
75 { NCL_FALLING_ADDR
, 0xff },
76 { FDL_FALLING_ADDR
, 0x02 },
77 { FILTER_CONF_ADDR
, 0x04 },
78 { AFE_CONF_ADDR
, 0x0b },
79 { AUTO_CONFIG_CTRL_ADDR
, 0x0b },
82 static void mpr121_vdd_supply_disable(void *data
)
84 struct regulator
*vdd_supply
= data
;
86 regulator_disable(vdd_supply
);
89 static struct regulator
*mpr121_vdd_supply_init(struct device
*dev
)
91 struct regulator
*vdd_supply
;
94 vdd_supply
= devm_regulator_get(dev
, "vdd");
95 if (IS_ERR(vdd_supply
)) {
96 dev_err(dev
, "failed to get vdd regulator: %ld\n",
101 err
= regulator_enable(vdd_supply
);
103 dev_err(dev
, "failed to enable vdd regulator: %d\n", err
);
107 err
= devm_add_action(dev
, mpr121_vdd_supply_disable
, vdd_supply
);
109 regulator_disable(vdd_supply
);
110 dev_err(dev
, "failed to add disable regulator action: %d\n",
118 static irqreturn_t
mpr_touchkey_interrupt(int irq
, void *dev_id
)
120 struct mpr121_touchkey
*mpr121
= dev_id
;
121 struct i2c_client
*client
= mpr121
->client
;
122 struct input_dev
*input
= mpr121
->input_dev
;
123 unsigned long bit_changed
;
124 unsigned int key_num
;
127 reg
= i2c_smbus_read_byte_data(client
, ELE_TOUCH_STATUS_1_ADDR
);
129 dev_err(&client
->dev
, "i2c read error [%d]\n", reg
);
134 reg
|= i2c_smbus_read_byte_data(client
, ELE_TOUCH_STATUS_0_ADDR
);
136 dev_err(&client
->dev
, "i2c read error [%d]\n", reg
);
140 reg
&= TOUCH_STATUS_MASK
;
141 /* use old press bit to figure out which bit changed */
142 bit_changed
= reg
^ mpr121
->statusbits
;
143 mpr121
->statusbits
= reg
;
144 for_each_set_bit(key_num
, &bit_changed
, mpr121
->keycount
) {
145 unsigned int key_val
, pressed
;
147 pressed
= reg
& BIT(key_num
);
148 key_val
= mpr121
->keycodes
[key_num
];
150 input_event(input
, EV_MSC
, MSC_SCAN
, key_num
);
151 input_report_key(input
, key_val
, pressed
);
153 dev_dbg(&client
->dev
, "key %d %d %s\n", key_num
, key_val
,
154 pressed
? "pressed" : "released");
163 static int mpr121_phys_init(struct mpr121_touchkey
*mpr121
,
164 struct i2c_client
*client
, int vdd_uv
)
166 const struct mpr121_init_register
*reg
;
167 unsigned char usl
, lsl
, tl
, eleconf
;
170 /* Set up touch/release threshold for ele0-ele11 */
171 for (i
= 0; i
<= MPR121_MAX_KEY_COUNT
; i
++) {
172 t
= ELE0_TOUCH_THRESHOLD_ADDR
+ (i
* 2);
173 ret
= i2c_smbus_write_byte_data(client
, t
, TOUCH_THRESHOLD
);
176 ret
= i2c_smbus_write_byte_data(client
, t
+ 1,
182 /* Set up init register */
183 for (i
= 0; i
< ARRAY_SIZE(init_reg_table
); i
++) {
184 reg
= &init_reg_table
[i
];
185 ret
= i2c_smbus_write_byte_data(client
, reg
->addr
, reg
->val
);
192 * Capacitance on sensing input varies and needs to be compensated.
193 * The internal MPR121-auto-configuration can do this if it's
194 * registers are set properly (based on vdd_uv).
197 usl
= ((vdd
- 700) * 256) / vdd
;
198 lsl
= (usl
* 65) / 100;
199 tl
= (usl
* 90) / 100;
200 ret
= i2c_smbus_write_byte_data(client
, AUTO_CONFIG_USL_ADDR
, usl
);
201 ret
|= i2c_smbus_write_byte_data(client
, AUTO_CONFIG_LSL_ADDR
, lsl
);
202 ret
|= i2c_smbus_write_byte_data(client
, AUTO_CONFIG_TL_ADDR
, tl
);
205 * Quick charge bit will let the capacitive charge to ready
206 * state quickly, or the buttons may not function after system
209 eleconf
= mpr121
->keycount
| ELECTRODE_CONF_QUICK_CHARGE
;
210 ret
|= i2c_smbus_write_byte_data(client
, ELECTRODE_CONF_ADDR
,
215 dev_dbg(&client
->dev
, "set up with %x keys.\n", mpr121
->keycount
);
220 dev_err(&client
->dev
, "i2c write error: %d\n", ret
);
224 static int mpr_touchkey_probe(struct i2c_client
*client
,
225 const struct i2c_device_id
*id
)
227 struct device
*dev
= &client
->dev
;
228 struct regulator
*vdd_supply
;
230 struct mpr121_touchkey
*mpr121
;
231 struct input_dev
*input_dev
;
236 dev_err(dev
, "irq number should not be zero\n");
240 vdd_supply
= mpr121_vdd_supply_init(dev
);
241 if (IS_ERR(vdd_supply
))
242 return PTR_ERR(vdd_supply
);
244 vdd_uv
= regulator_get_voltage(vdd_supply
);
246 mpr121
= devm_kzalloc(dev
, sizeof(*mpr121
), GFP_KERNEL
);
250 input_dev
= devm_input_allocate_device(dev
);
254 mpr121
->client
= client
;
255 mpr121
->input_dev
= input_dev
;
256 mpr121
->keycount
= device_property_read_u32_array(dev
, "linux,keycodes",
258 if (mpr121
->keycount
> MPR121_MAX_KEY_COUNT
) {
259 dev_err(dev
, "too many keys defined (%d)\n", mpr121
->keycount
);
263 error
= device_property_read_u32_array(dev
, "linux,keycodes",
268 "failed to read linux,keycode property: %d\n", error
);
272 input_dev
->name
= "Freescale MPR121 Touchkey";
273 input_dev
->id
.bustype
= BUS_I2C
;
274 input_dev
->dev
.parent
= dev
;
275 if (device_property_read_bool(dev
, "autorepeat"))
276 __set_bit(EV_REP
, input_dev
->evbit
);
277 input_set_capability(input_dev
, EV_MSC
, MSC_SCAN
);
279 input_dev
->keycode
= mpr121
->keycodes
;
280 input_dev
->keycodesize
= sizeof(mpr121
->keycodes
[0]);
281 input_dev
->keycodemax
= mpr121
->keycount
;
283 for (i
= 0; i
< mpr121
->keycount
; i
++)
284 input_set_capability(input_dev
, EV_KEY
, mpr121
->keycodes
[i
]);
286 error
= mpr121_phys_init(mpr121
, client
, vdd_uv
);
288 dev_err(dev
, "Failed to init register\n");
292 error
= devm_request_threaded_irq(dev
, client
->irq
, NULL
,
293 mpr_touchkey_interrupt
,
294 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
295 dev
->driver
->name
, mpr121
);
297 dev_err(dev
, "Failed to register interrupt\n");
301 error
= input_register_device(input_dev
);
305 i2c_set_clientdata(client
, mpr121
);
306 device_init_wakeup(dev
,
307 device_property_read_bool(dev
, "wakeup-source"));
312 static int __maybe_unused
mpr_suspend(struct device
*dev
)
314 struct i2c_client
*client
= to_i2c_client(dev
);
316 if (device_may_wakeup(&client
->dev
))
317 enable_irq_wake(client
->irq
);
319 i2c_smbus_write_byte_data(client
, ELECTRODE_CONF_ADDR
, 0x00);
324 static int __maybe_unused
mpr_resume(struct device
*dev
)
326 struct i2c_client
*client
= to_i2c_client(dev
);
327 struct mpr121_touchkey
*mpr121
= i2c_get_clientdata(client
);
329 if (device_may_wakeup(&client
->dev
))
330 disable_irq_wake(client
->irq
);
332 i2c_smbus_write_byte_data(client
, ELECTRODE_CONF_ADDR
,
338 static SIMPLE_DEV_PM_OPS(mpr121_touchkey_pm_ops
, mpr_suspend
, mpr_resume
);
340 static const struct i2c_device_id mpr121_id
[] = {
341 { "mpr121_touchkey", 0 },
344 MODULE_DEVICE_TABLE(i2c
, mpr121_id
);
347 static const struct of_device_id mpr121_touchkey_dt_match_table
[] = {
348 { .compatible
= "fsl,mpr121-touchkey" },
351 MODULE_DEVICE_TABLE(of
, mpr121_touchkey_dt_match_table
);
354 static struct i2c_driver mpr_touchkey_driver
= {
357 .pm
= &mpr121_touchkey_pm_ops
,
358 .of_match_table
= of_match_ptr(mpr121_touchkey_dt_match_table
),
360 .id_table
= mpr121_id
,
361 .probe
= mpr_touchkey_probe
,
364 module_i2c_driver(mpr_touchkey_driver
);
366 MODULE_LICENSE("GPL");
367 MODULE_AUTHOR("Zhang Jiejing <jiejing.zhang@freescale.com>");
368 MODULE_DESCRIPTION("Touch Key driver for Freescale MPR121 Chip");