1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Touchkey driver for Freescale MPR121 Controllor
4  *
5  * Copyright (C) 2011 Freescale Semiconductor, Inc.
6  * Author: Zhang Jiejing <jiejing.zhang@freescale.com>
7  *
8  * Based on mcs_touchkey.c
9  */
10 
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>
17 #include <linux/of.h>
18 #include <linux/property.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/slab.h>
21 
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
37 
38 /*
39  * ELECTRODE_CONF_ADDR: This register configures the number of
40  * enabled capacitance sensing inputs and its run/suspend mode.
41  */
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
48 
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
56 
57 #define MPR121_MIN_POLL_INTERVAL	10
58 #define MPR121_MAX_POLL_INTERVAL	200
59 
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];
66 };
67 
68 struct mpr121_init_register {
69 	int addr;
70 	u8 val;
71 };
72 
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 },
83 };
84 
85 static void mpr121_vdd_supply_disable(void *data)
86 {
87 	struct regulator *vdd_supply = data;
88 
89 	regulator_disable(vdd_supply);
90 }
91 
92 static struct regulator *mpr121_vdd_supply_init(struct device *dev)
93 {
94 	struct regulator *vdd_supply;
95 	int err;
96 
97 	vdd_supply = devm_regulator_get(dev, "vdd");
98 	if (IS_ERR(vdd_supply)) {
99 		dev_err(dev, "failed to get vdd regulator: %ld\n",
100 			PTR_ERR(vdd_supply));
101 		return vdd_supply;
102 	}
103 
104 	err = regulator_enable(vdd_supply);
105 	if (err) {
106 		dev_err(dev, "failed to enable vdd regulator: %d\n", err);
107 		return ERR_PTR(err);
108 	}
109 
110 	err = devm_add_action_or_reset(dev, mpr121_vdd_supply_disable,
111 				       vdd_supply);
112 	if (err) {
113 		dev_err(dev, "failed to add disable regulator action: %d\n",
114 			err);
115 		return ERR_PTR(err);
116 	}
117 
118 	return vdd_supply;
119 }
120 
121 static void mpr_touchkey_report(struct input_dev *dev)
122 {
123 	struct mpr121_touchkey *mpr121 = input_get_drvdata(dev);
124 	struct input_dev *input = mpr121->input_dev;
125 	struct i2c_client *client = mpr121->client;
126 	unsigned long bit_changed;
127 	unsigned int key_num;
128 	int reg;
129 
130 	reg = i2c_smbus_read_byte_data(client, ELE_TOUCH_STATUS_1_ADDR);
131 	if (reg < 0) {
132 		dev_err(&client->dev, "i2c read error [%d]\n", reg);
133 		return;
134 	}
135 
136 	reg <<= 8;
137 	reg |= i2c_smbus_read_byte_data(client, ELE_TOUCH_STATUS_0_ADDR);
138 	if (reg < 0) {
139 		dev_err(&client->dev, "i2c read error [%d]\n", reg);
140 		return;
141 	}
142 
143 	reg &= TOUCH_STATUS_MASK;
144 	/* use old press bit to figure out which bit changed */
145 	bit_changed = reg ^ mpr121->statusbits;
146 	mpr121->statusbits = reg;
147 	for_each_set_bit(key_num, &bit_changed, mpr121->keycount) {
148 		unsigned int key_val, pressed;
149 
150 		pressed = reg & BIT(key_num);
151 		key_val = mpr121->keycodes[key_num];
152 
153 		input_event(input, EV_MSC, MSC_SCAN, key_num);
154 		input_report_key(input, key_val, pressed);
155 
156 		dev_dbg(&client->dev, "key %d %d %s\n", key_num, key_val,
157 			pressed ? "pressed" : "released");
158 
159 	}
160 	input_sync(input);
161 }
162 
163 static irqreturn_t mpr_touchkey_interrupt(int irq, void *dev_id)
164 {
165 	struct mpr121_touchkey *mpr121 = dev_id;
166 
167 	mpr_touchkey_report(mpr121->input_dev);
168 
169 	return IRQ_HANDLED;
170 }
171 
172 static int mpr121_phys_init(struct mpr121_touchkey *mpr121,
173 			    struct i2c_client *client, int vdd_uv)
174 {
175 	const struct mpr121_init_register *reg;
176 	unsigned char usl, lsl, tl, eleconf;
177 	int i, t, vdd, ret;
178 
179 	/* Set up touch/release threshold for ele0-ele11 */
180 	for (i = 0; i <= MPR121_MAX_KEY_COUNT; i++) {
181 		t = ELE0_TOUCH_THRESHOLD_ADDR + (i * 2);
182 		ret = i2c_smbus_write_byte_data(client, t, TOUCH_THRESHOLD);
183 		if (ret < 0)
184 			goto err_i2c_write;
185 		ret = i2c_smbus_write_byte_data(client, t + 1,
186 						RELEASE_THRESHOLD);
187 		if (ret < 0)
188 			goto err_i2c_write;
189 	}
190 
191 	/* Set up init register */
192 	for (i = 0; i < ARRAY_SIZE(init_reg_table); i++) {
193 		reg = &init_reg_table[i];
194 		ret = i2c_smbus_write_byte_data(client, reg->addr, reg->val);
195 		if (ret < 0)
196 			goto err_i2c_write;
197 	}
198 
199 
200 	/*
201 	 * Capacitance on sensing input varies and needs to be compensated.
202 	 * The internal MPR121-auto-configuration can do this if it's
203 	 * registers are set properly (based on vdd_uv).
204 	 */
205 	vdd = vdd_uv / 1000;
206 	usl = ((vdd - 700) * 256) / vdd;
207 	lsl = (usl * 65) / 100;
208 	tl = (usl * 90) / 100;
209 	ret = i2c_smbus_write_byte_data(client, AUTO_CONFIG_USL_ADDR, usl);
210 	ret |= i2c_smbus_write_byte_data(client, AUTO_CONFIG_LSL_ADDR, lsl);
211 	ret |= i2c_smbus_write_byte_data(client, AUTO_CONFIG_TL_ADDR, tl);
212 
213 	/*
214 	 * Quick charge bit will let the capacitive charge to ready
215 	 * state quickly, or the buttons may not function after system
216 	 * boot.
217 	 */
218 	eleconf = mpr121->keycount | ELECTRODE_CONF_QUICK_CHARGE;
219 	ret |= i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR,
220 					 eleconf);
221 	if (ret != 0)
222 		goto err_i2c_write;
223 
224 	dev_dbg(&client->dev, "set up with %x keys.\n", mpr121->keycount);
225 
226 	return 0;
227 
228 err_i2c_write:
229 	dev_err(&client->dev, "i2c write error: %d\n", ret);
230 	return ret;
231 }
232 
233 static int mpr_touchkey_probe(struct i2c_client *client)
234 {
235 	struct device *dev = &client->dev;
236 	struct regulator *vdd_supply;
237 	int vdd_uv;
238 	struct mpr121_touchkey *mpr121;
239 	struct input_dev *input_dev;
240 	u32 poll_interval = 0;
241 	int error;
242 	int i;
243 
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 
250 	mpr121 = devm_kzalloc(dev, sizeof(*mpr121), GFP_KERNEL);
251 	if (!mpr121)
252 		return -ENOMEM;
253 
254 	input_dev = devm_input_allocate_device(dev);
255 	if (!input_dev)
256 		return -ENOMEM;
257 
258 	mpr121->client = client;
259 	mpr121->input_dev = input_dev;
260 	mpr121->keycount = device_property_count_u32(dev, "linux,keycodes");
261 	if (mpr121->keycount > MPR121_MAX_KEY_COUNT) {
262 		dev_err(dev, "too many keys defined (%d)\n", mpr121->keycount);
263 		return -EINVAL;
264 	}
265 
266 	error = device_property_read_u32_array(dev, "linux,keycodes",
267 					       mpr121->keycodes,
268 					       mpr121->keycount);
269 	if (error) {
270 		dev_err(dev,
271 			"failed to read linux,keycode property: %d\n", error);
272 		return error;
273 	}
274 
275 	input_dev->name = "Freescale MPR121 Touchkey";
276 	input_dev->id.bustype = BUS_I2C;
277 	input_dev->dev.parent = dev;
278 	if (device_property_read_bool(dev, "autorepeat"))
279 		__set_bit(EV_REP, input_dev->evbit);
280 	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
281 	input_set_drvdata(input_dev, mpr121);
282 
283 	input_dev->keycode = mpr121->keycodes;
284 	input_dev->keycodesize = sizeof(mpr121->keycodes[0]);
285 	input_dev->keycodemax = mpr121->keycount;
286 
287 	for (i = 0; i < mpr121->keycount; i++)
288 		input_set_capability(input_dev, EV_KEY, mpr121->keycodes[i]);
289 
290 	error = mpr121_phys_init(mpr121, client, vdd_uv);
291 	if (error) {
292 		dev_err(dev, "Failed to init register\n");
293 		return error;
294 	}
295 
296 	device_property_read_u32(dev, "poll-interval", &poll_interval);
297 
298 	if (client->irq) {
299 		error = devm_request_threaded_irq(dev, client->irq, NULL,
300 						  mpr_touchkey_interrupt,
301 						  IRQF_TRIGGER_FALLING |
302 						  IRQF_ONESHOT,
303 						  dev->driver->name, mpr121);
304 		if (error) {
305 			dev_err(dev, "Failed to register interrupt\n");
306 			return error;
307 		}
308 	} else if (poll_interval) {
309 		if (poll_interval < MPR121_MIN_POLL_INTERVAL)
310 			return -EINVAL;
311 
312 		if (poll_interval > MPR121_MAX_POLL_INTERVAL)
313 			return -EINVAL;
314 
315 		error = input_setup_polling(input_dev, mpr_touchkey_report);
316 		if (error) {
317 			dev_err(dev, "Failed to setup polling\n");
318 			return error;
319 		}
320 
321 		input_set_poll_interval(input_dev, poll_interval);
322 		input_set_min_poll_interval(input_dev,
323 					    MPR121_MIN_POLL_INTERVAL);
324 		input_set_max_poll_interval(input_dev,
325 					    MPR121_MAX_POLL_INTERVAL);
326 	} else {
327 		dev_err(dev,
328 			"invalid IRQ number and polling not configured\n");
329 		return -EINVAL;
330 	}
331 
332 	error = input_register_device(input_dev);
333 	if (error)
334 		return error;
335 
336 	i2c_set_clientdata(client, mpr121);
337 	device_init_wakeup(dev,
338 			device_property_read_bool(dev, "wakeup-source"));
339 
340 	return 0;
341 }
342 
343 static int mpr_suspend(struct device *dev)
344 {
345 	struct i2c_client *client = to_i2c_client(dev);
346 
347 	if (device_may_wakeup(&client->dev))
348 		enable_irq_wake(client->irq);
349 
350 	i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR, 0x00);
351 
352 	return 0;
353 }
354 
355 static int mpr_resume(struct device *dev)
356 {
357 	struct i2c_client *client = to_i2c_client(dev);
358 	struct mpr121_touchkey *mpr121 = i2c_get_clientdata(client);
359 
360 	if (device_may_wakeup(&client->dev))
361 		disable_irq_wake(client->irq);
362 
363 	i2c_smbus_write_byte_data(client, ELECTRODE_CONF_ADDR,
364 				  mpr121->keycount);
365 
366 	return 0;
367 }
368 
369 static DEFINE_SIMPLE_DEV_PM_OPS(mpr121_touchkey_pm_ops, mpr_suspend, mpr_resume);
370 
371 static const struct i2c_device_id mpr121_id[] = {
372 	{ "mpr121_touchkey", 0 },
373 	{ }
374 };
375 MODULE_DEVICE_TABLE(i2c, mpr121_id);
376 
377 #ifdef CONFIG_OF
378 static const struct of_device_id mpr121_touchkey_dt_match_table[] = {
379 	{ .compatible = "fsl,mpr121-touchkey" },
380 	{ },
381 };
382 MODULE_DEVICE_TABLE(of, mpr121_touchkey_dt_match_table);
383 #endif
384 
385 static struct i2c_driver mpr_touchkey_driver = {
386 	.driver = {
387 		.name	= "mpr121",
388 		.pm	= pm_sleep_ptr(&mpr121_touchkey_pm_ops),
389 		.of_match_table = of_match_ptr(mpr121_touchkey_dt_match_table),
390 	},
391 	.id_table	= mpr121_id,
392 	.probe_new	= mpr_touchkey_probe,
393 };
394 
395 module_i2c_driver(mpr_touchkey_driver);
396 
397 MODULE_LICENSE("GPL");
398 MODULE_AUTHOR("Zhang Jiejing <jiejing.zhang@freescale.com>");
399 MODULE_DESCRIPTION("Touch Key driver for Freescale MPR121 Chip");
400