1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * TM2 touchkey device driver
4  *
5  * Copyright 2005 Phil Blundell
6  * Copyright 2016 Samsung Electronics Co., Ltd.
7  *
8  * Author: Beomho Seo <beomho.seo@samsung.com>
9  * Author: Jaechul Lee <jcsing.lee@samsung.com>
10  */
11 
12 #include <linux/bitops.h>
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/i2c.h>
16 #include <linux/input.h>
17 #include <linux/interrupt.h>
18 #include <linux/irq.h>
19 #include <linux/leds.h>
20 #include <linux/module.h>
21 #include <linux/of.h>
22 #include <linux/of_device.h>
23 #include <linux/pm.h>
24 #include <linux/regulator/consumer.h>
25 
26 #define TM2_TOUCHKEY_DEV_NAME		"tm2-touchkey"
27 
28 #define ARIES_TOUCHKEY_CMD_LED_ON	0x1
29 #define ARIES_TOUCHKEY_CMD_LED_OFF	0x2
30 #define TM2_TOUCHKEY_CMD_LED_ON		0x10
31 #define TM2_TOUCHKEY_CMD_LED_OFF	0x20
32 #define TM2_TOUCHKEY_BIT_PRESS_EV	BIT(3)
33 #define TM2_TOUCHKEY_BIT_KEYCODE	GENMASK(2, 0)
34 #define TM2_TOUCHKEY_LED_VOLTAGE_MIN	2500000
35 #define TM2_TOUCHKEY_LED_VOLTAGE_MAX	3300000
36 
37 struct touchkey_variant {
38 	u8 keycode_reg;
39 	u8 base_reg;
40 	u8 cmd_led_on;
41 	u8 cmd_led_off;
42 	bool no_reg;
43 	bool fixed_regulator;
44 };
45 
46 struct tm2_touchkey_data {
47 	struct i2c_client *client;
48 	struct input_dev *input_dev;
49 	struct led_classdev led_dev;
50 	struct regulator *vdd;
51 	struct regulator_bulk_data regulators[3];
52 	const struct touchkey_variant *variant;
53 	u32 keycodes[4];
54 	int num_keycodes;
55 };
56 
57 static const struct touchkey_variant tm2_touchkey_variant = {
58 	.keycode_reg = 0x03,
59 	.base_reg = 0x00,
60 	.cmd_led_on = TM2_TOUCHKEY_CMD_LED_ON,
61 	.cmd_led_off = TM2_TOUCHKEY_CMD_LED_OFF,
62 };
63 
64 static const struct touchkey_variant midas_touchkey_variant = {
65 	.keycode_reg = 0x00,
66 	.base_reg = 0x00,
67 	.cmd_led_on = TM2_TOUCHKEY_CMD_LED_ON,
68 	.cmd_led_off = TM2_TOUCHKEY_CMD_LED_OFF,
69 };
70 
71 static struct touchkey_variant aries_touchkey_variant = {
72 	.no_reg = true,
73 	.fixed_regulator = true,
74 	.cmd_led_on = ARIES_TOUCHKEY_CMD_LED_ON,
75 	.cmd_led_off = ARIES_TOUCHKEY_CMD_LED_OFF,
76 };
77 
78 static const struct touchkey_variant tc360_touchkey_variant = {
79 	.keycode_reg = 0x00,
80 	.base_reg = 0x00,
81 	.fixed_regulator = true,
82 	.cmd_led_on = TM2_TOUCHKEY_CMD_LED_ON,
83 	.cmd_led_off = TM2_TOUCHKEY_CMD_LED_OFF,
84 };
85 
86 static int tm2_touchkey_led_brightness_set(struct led_classdev *led_dev,
87 					    enum led_brightness brightness)
88 {
89 	struct tm2_touchkey_data *touchkey =
90 		container_of(led_dev, struct tm2_touchkey_data, led_dev);
91 	u32 volt;
92 	u8 data;
93 
94 	if (brightness == LED_OFF) {
95 		volt = TM2_TOUCHKEY_LED_VOLTAGE_MIN;
96 		data = touchkey->variant->cmd_led_off;
97 	} else {
98 		volt = TM2_TOUCHKEY_LED_VOLTAGE_MAX;
99 		data = touchkey->variant->cmd_led_on;
100 	}
101 
102 	if (!touchkey->variant->fixed_regulator)
103 		regulator_set_voltage(touchkey->vdd, volt, volt);
104 
105 	return touchkey->variant->no_reg ?
106 		i2c_smbus_write_byte(touchkey->client, data) :
107 		i2c_smbus_write_byte_data(touchkey->client,
108 					  touchkey->variant->base_reg, data);
109 }
110 
111 static int tm2_touchkey_power_enable(struct tm2_touchkey_data *touchkey)
112 {
113 	int error;
114 
115 	error = regulator_bulk_enable(ARRAY_SIZE(touchkey->regulators),
116 				      touchkey->regulators);
117 	if (error)
118 		return error;
119 
120 	/* waiting for device initialization, at least 150ms */
121 	msleep(150);
122 
123 	return 0;
124 }
125 
126 static void tm2_touchkey_power_disable(void *data)
127 {
128 	struct tm2_touchkey_data *touchkey = data;
129 
130 	regulator_bulk_disable(ARRAY_SIZE(touchkey->regulators),
131 			       touchkey->regulators);
132 }
133 
134 static irqreturn_t tm2_touchkey_irq_handler(int irq, void *devid)
135 {
136 	struct tm2_touchkey_data *touchkey = devid;
137 	int data;
138 	int index;
139 	int i;
140 
141 	if (touchkey->variant->no_reg)
142 		data = i2c_smbus_read_byte(touchkey->client);
143 	else
144 		data = i2c_smbus_read_byte_data(touchkey->client,
145 						touchkey->variant->keycode_reg);
146 	if (data < 0) {
147 		dev_err(&touchkey->client->dev,
148 			"failed to read i2c data: %d\n", data);
149 		goto out;
150 	}
151 
152 	index = (data & TM2_TOUCHKEY_BIT_KEYCODE) - 1;
153 	if (index < 0 || index >= touchkey->num_keycodes) {
154 		dev_warn(&touchkey->client->dev,
155 			 "invalid keycode index %d\n", index);
156 		goto out;
157 	}
158 
159 	input_event(touchkey->input_dev, EV_MSC, MSC_SCAN, index);
160 
161 	if (data & TM2_TOUCHKEY_BIT_PRESS_EV) {
162 		for (i = 0; i < touchkey->num_keycodes; i++)
163 			input_report_key(touchkey->input_dev,
164 					 touchkey->keycodes[i], 0);
165 	} else {
166 		input_report_key(touchkey->input_dev,
167 				 touchkey->keycodes[index], 1);
168 	}
169 
170 	input_sync(touchkey->input_dev);
171 
172 out:
173 	if (touchkey->variant->fixed_regulator &&
174 				data & TM2_TOUCHKEY_BIT_PRESS_EV) {
175 		/* touch turns backlight on, so make sure we're in sync */
176 		if (touchkey->led_dev.brightness == LED_OFF)
177 			tm2_touchkey_led_brightness_set(&touchkey->led_dev,
178 							LED_OFF);
179 	}
180 
181 	return IRQ_HANDLED;
182 }
183 
184 static int tm2_touchkey_probe(struct i2c_client *client,
185 			      const struct i2c_device_id *id)
186 {
187 	struct device_node *np = client->dev.of_node;
188 	struct tm2_touchkey_data *touchkey;
189 	int error;
190 	int i;
191 
192 	if (!i2c_check_functionality(client->adapter,
193 			I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA)) {
194 		dev_err(&client->dev, "incompatible I2C adapter\n");
195 		return -EIO;
196 	}
197 
198 	touchkey = devm_kzalloc(&client->dev, sizeof(*touchkey), GFP_KERNEL);
199 	if (!touchkey)
200 		return -ENOMEM;
201 
202 	touchkey->client = client;
203 	i2c_set_clientdata(client, touchkey);
204 
205 	touchkey->variant = of_device_get_match_data(&client->dev);
206 
207 	touchkey->regulators[0].supply = "vcc";
208 	touchkey->regulators[1].supply = "vdd";
209 	touchkey->regulators[2].supply = "vddio";
210 	error = devm_regulator_bulk_get(&client->dev,
211 					ARRAY_SIZE(touchkey->regulators),
212 					touchkey->regulators);
213 	if (error) {
214 		dev_err(&client->dev, "failed to get regulators: %d\n", error);
215 		return error;
216 	}
217 
218 	/* Save VDD for easy access */
219 	touchkey->vdd = touchkey->regulators[1].consumer;
220 
221 	touchkey->num_keycodes = of_property_read_variable_u32_array(np,
222 					"linux,keycodes", touchkey->keycodes, 0,
223 					ARRAY_SIZE(touchkey->keycodes));
224 	if (touchkey->num_keycodes <= 0) {
225 		/* default keycodes */
226 		touchkey->keycodes[0] = KEY_PHONE;
227 		touchkey->keycodes[1] = KEY_BACK;
228 		touchkey->num_keycodes = 2;
229 	}
230 
231 	error = tm2_touchkey_power_enable(touchkey);
232 	if (error) {
233 		dev_err(&client->dev, "failed to power up device: %d\n", error);
234 		return error;
235 	}
236 
237 	error = devm_add_action_or_reset(&client->dev,
238 					 tm2_touchkey_power_disable, touchkey);
239 	if (error) {
240 		dev_err(&client->dev,
241 			"failed to install poweroff handler: %d\n", error);
242 		return error;
243 	}
244 
245 	/* input device */
246 	touchkey->input_dev = devm_input_allocate_device(&client->dev);
247 	if (!touchkey->input_dev) {
248 		dev_err(&client->dev, "failed to allocate input device\n");
249 		return -ENOMEM;
250 	}
251 
252 	touchkey->input_dev->name = TM2_TOUCHKEY_DEV_NAME;
253 	touchkey->input_dev->id.bustype = BUS_I2C;
254 
255 	touchkey->input_dev->keycode = touchkey->keycodes;
256 	touchkey->input_dev->keycodemax = touchkey->num_keycodes;
257 	touchkey->input_dev->keycodesize = sizeof(touchkey->keycodes[0]);
258 
259 	input_set_capability(touchkey->input_dev, EV_MSC, MSC_SCAN);
260 	for (i = 0; i < touchkey->num_keycodes; i++)
261 		input_set_capability(touchkey->input_dev, EV_KEY,
262 				     touchkey->keycodes[i]);
263 
264 	error = input_register_device(touchkey->input_dev);
265 	if (error) {
266 		dev_err(&client->dev,
267 			"failed to register input device: %d\n", error);
268 		return error;
269 	}
270 
271 	error = devm_request_threaded_irq(&client->dev, client->irq,
272 					  NULL, tm2_touchkey_irq_handler,
273 					  IRQF_ONESHOT,
274 					  TM2_TOUCHKEY_DEV_NAME, touchkey);
275 	if (error) {
276 		dev_err(&client->dev,
277 			"failed to request threaded irq: %d\n", error);
278 		return error;
279 	}
280 
281 	/* led device */
282 	touchkey->led_dev.name = TM2_TOUCHKEY_DEV_NAME;
283 	touchkey->led_dev.brightness = LED_ON;
284 	touchkey->led_dev.max_brightness = LED_ON;
285 	touchkey->led_dev.brightness_set_blocking =
286 					tm2_touchkey_led_brightness_set;
287 
288 	error = devm_led_classdev_register(&client->dev, &touchkey->led_dev);
289 	if (error) {
290 		dev_err(&client->dev,
291 			"failed to register touchkey led: %d\n", error);
292 		return error;
293 	}
294 
295 	if (touchkey->variant->fixed_regulator)
296 		tm2_touchkey_led_brightness_set(&touchkey->led_dev, LED_ON);
297 
298 	return 0;
299 }
300 
301 static int __maybe_unused tm2_touchkey_suspend(struct device *dev)
302 {
303 	struct i2c_client *client = to_i2c_client(dev);
304 	struct tm2_touchkey_data *touchkey = i2c_get_clientdata(client);
305 
306 	disable_irq(client->irq);
307 	tm2_touchkey_power_disable(touchkey);
308 
309 	return 0;
310 }
311 
312 static int __maybe_unused tm2_touchkey_resume(struct device *dev)
313 {
314 	struct i2c_client *client = to_i2c_client(dev);
315 	struct tm2_touchkey_data *touchkey = i2c_get_clientdata(client);
316 	int ret;
317 
318 	enable_irq(client->irq);
319 
320 	ret = tm2_touchkey_power_enable(touchkey);
321 	if (ret)
322 		dev_err(dev, "failed to enable power: %d\n", ret);
323 
324 	return ret;
325 }
326 
327 static SIMPLE_DEV_PM_OPS(tm2_touchkey_pm_ops,
328 			 tm2_touchkey_suspend, tm2_touchkey_resume);
329 
330 static const struct i2c_device_id tm2_touchkey_id_table[] = {
331 	{ TM2_TOUCHKEY_DEV_NAME, 0 },
332 	{ },
333 };
334 MODULE_DEVICE_TABLE(i2c, tm2_touchkey_id_table);
335 
336 static const struct of_device_id tm2_touchkey_of_match[] = {
337 	{
338 		.compatible = "cypress,tm2-touchkey",
339 		.data = &tm2_touchkey_variant,
340 	}, {
341 		.compatible = "cypress,midas-touchkey",
342 		.data = &midas_touchkey_variant,
343 	}, {
344 		.compatible = "cypress,aries-touchkey",
345 		.data = &aries_touchkey_variant,
346 	}, {
347 		.compatible = "coreriver,tc360-touchkey",
348 		.data = &tc360_touchkey_variant,
349 	},
350 	{ },
351 };
352 MODULE_DEVICE_TABLE(of, tm2_touchkey_of_match);
353 
354 static struct i2c_driver tm2_touchkey_driver = {
355 	.driver = {
356 		.name = TM2_TOUCHKEY_DEV_NAME,
357 		.pm = &tm2_touchkey_pm_ops,
358 		.of_match_table = of_match_ptr(tm2_touchkey_of_match),
359 	},
360 	.probe = tm2_touchkey_probe,
361 	.id_table = tm2_touchkey_id_table,
362 };
363 module_i2c_driver(tm2_touchkey_driver);
364 
365 MODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>");
366 MODULE_AUTHOR("Jaechul Lee <jcsing.lee@samsung.com>");
367 MODULE_DESCRIPTION("Samsung touchkey driver");
368 MODULE_LICENSE("GPL v2");
369