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 #define MPR121_MIN_POLL_INTERVAL 10
58 #define MPR121_MAX_POLL_INTERVAL 200
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
];
68 struct mpr121_init_register
{
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 },
85 static void mpr_touchkey_report(struct input_dev
*dev
)
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
;
94 reg
= i2c_smbus_read_byte_data(client
, ELE_TOUCH_STATUS_1_ADDR
);
96 dev_err(&client
->dev
, "i2c read error [%d]\n", reg
);
101 reg
|= i2c_smbus_read_byte_data(client
, ELE_TOUCH_STATUS_0_ADDR
);
103 dev_err(&client
->dev
, "i2c read error [%d]\n", reg
);
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
;
114 pressed
= reg
& BIT(key_num
);
115 key_val
= mpr121
->keycodes
[key_num
];
117 input_event(input
, EV_MSC
, MSC_SCAN
, key_num
);
118 input_report_key(input
, key_val
, pressed
);
120 dev_dbg(&client
->dev
, "key %d %d %s\n", key_num
, key_val
,
121 pressed
? "pressed" : "released");
127 static irqreturn_t
mpr_touchkey_interrupt(int irq
, void *dev_id
)
129 struct mpr121_touchkey
*mpr121
= dev_id
;
131 mpr_touchkey_report(mpr121
->input_dev
);
136 static int mpr121_phys_init(struct mpr121_touchkey
*mpr121
,
137 struct i2c_client
*client
, int vdd_uv
)
139 const struct mpr121_init_register
*reg
;
140 unsigned char usl
, lsl
, tl
, eleconf
;
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
);
149 ret
= i2c_smbus_write_byte_data(client
, t
+ 1,
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
);
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).
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
);
178 * Quick charge bit will let the capacitive charge to ready
179 * state quickly, or the buttons may not function after system
182 eleconf
= mpr121
->keycount
| ELECTRODE_CONF_QUICK_CHARGE
;
183 ret
|= i2c_smbus_write_byte_data(client
, ELECTRODE_CONF_ADDR
,
188 dev_dbg(&client
->dev
, "set up with %x keys.\n", mpr121
->keycount
);
193 dev_err(&client
->dev
, "i2c write error: %d\n", ret
);
197 static int mpr_touchkey_probe(struct i2c_client
*client
)
199 struct device
*dev
= &client
->dev
;
201 struct mpr121_touchkey
*mpr121
;
202 struct input_dev
*input_dev
;
203 u32 poll_interval
= 0;
207 vdd_uv
= devm_regulator_get_enable_read_voltage(dev
, "vdd");
209 return dev_err_probe(dev
, vdd_uv
, "failed to get vdd voltage\n");
211 mpr121
= devm_kzalloc(dev
, sizeof(*mpr121
), GFP_KERNEL
);
215 input_dev
= devm_input_allocate_device(dev
);
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
);
227 error
= device_property_read_u32_array(dev
, "linux,keycodes",
232 "failed to read linux,keycode property: %d\n", error
);
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
);
244 input_dev
->keycode
= mpr121
->keycodes
;
245 input_dev
->keycodesize
= sizeof(mpr121
->keycodes
[0]);
246 input_dev
->keycodemax
= mpr121
->keycount
;
248 for (i
= 0; i
< mpr121
->keycount
; i
++)
249 input_set_capability(input_dev
, EV_KEY
, mpr121
->keycodes
[i
]);
251 error
= mpr121_phys_init(mpr121
, client
, vdd_uv
);
253 dev_err(dev
, "Failed to init register\n");
257 device_property_read_u32(dev
, "poll-interval", &poll_interval
);
260 error
= devm_request_threaded_irq(dev
, client
->irq
, NULL
,
261 mpr_touchkey_interrupt
,
262 IRQF_TRIGGER_FALLING
|
264 dev
->driver
->name
, mpr121
);
266 dev_err(dev
, "Failed to register interrupt\n");
269 } else if (poll_interval
) {
270 if (poll_interval
< MPR121_MIN_POLL_INTERVAL
)
273 if (poll_interval
> MPR121_MAX_POLL_INTERVAL
)
276 error
= input_setup_polling(input_dev
, mpr_touchkey_report
);
278 dev_err(dev
, "Failed to setup polling\n");
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
);
289 "invalid IRQ number and polling not configured\n");
293 error
= input_register_device(input_dev
);
297 i2c_set_clientdata(client
, mpr121
);
298 device_init_wakeup(dev
,
299 device_property_read_bool(dev
, "wakeup-source"));
304 static int mpr_suspend(struct device
*dev
)
306 struct i2c_client
*client
= to_i2c_client(dev
);
308 if (device_may_wakeup(&client
->dev
))
309 enable_irq_wake(client
->irq
);
311 i2c_smbus_write_byte_data(client
, ELECTRODE_CONF_ADDR
, 0x00);
316 static int mpr_resume(struct device
*dev
)
318 struct i2c_client
*client
= to_i2c_client(dev
);
319 struct mpr121_touchkey
*mpr121
= i2c_get_clientdata(client
);
321 if (device_may_wakeup(&client
->dev
))
322 disable_irq_wake(client
->irq
);
324 i2c_smbus_write_byte_data(client
, ELECTRODE_CONF_ADDR
,
330 static DEFINE_SIMPLE_DEV_PM_OPS(mpr121_touchkey_pm_ops
, mpr_suspend
, mpr_resume
);
332 static const struct i2c_device_id mpr121_id
[] = {
333 { "mpr121_touchkey" },
336 MODULE_DEVICE_TABLE(i2c
, mpr121_id
);
339 static const struct of_device_id mpr121_touchkey_dt_match_table
[] = {
340 { .compatible
= "fsl,mpr121-touchkey" },
343 MODULE_DEVICE_TABLE(of
, mpr121_touchkey_dt_match_table
);
346 static struct i2c_driver mpr_touchkey_driver
= {
349 .pm
= pm_sleep_ptr(&mpr121_touchkey_pm_ops
),
350 .of_match_table
= of_match_ptr(mpr121_touchkey_dt_match_table
),
352 .id_table
= mpr121_id
,
353 .probe
= mpr_touchkey_probe
,
356 module_i2c_driver(mpr_touchkey_driver
);
358 MODULE_LICENSE("GPL");
359 MODULE_AUTHOR("Zhang Jiejing <jiejing.zhang@freescale.com>");
360 MODULE_DESCRIPTION("Touch Key driver for Freescale MPR121 Chip");