blob: 7dbef96559d2bf6683bdf4de1e84db1542022e8b [file] [log] [blame]
Jaechul Lee72d1f232017-01-18 14:35:42 -08001/*
2 * TM2 touchkey device driver
3 *
4 * Copyright 2005 Phil Blundell
5 * Copyright 2016 Samsung Electronics Co., Ltd.
6 *
7 * Author: Beomho Seo <beomho.seo@samsung.com>
8 * Author: Jaechul Lee <jcsing.lee@samsung.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/bitops.h>
16#include <linux/delay.h>
17#include <linux/device.h>
18#include <linux/i2c.h>
19#include <linux/input.h>
20#include <linux/interrupt.h>
21#include <linux/irq.h>
22#include <linux/leds.h>
23#include <linux/module.h>
24#include <linux/of.h>
Simon Shieldsd6f66f62019-01-07 11:09:26 -080025#include <linux/of_device.h>
Jaechul Lee72d1f232017-01-18 14:35:42 -080026#include <linux/pm.h>
27#include <linux/regulator/consumer.h>
28
29#define TM2_TOUCHKEY_DEV_NAME "tm2-touchkey"
Simon Shieldsd6f66f62019-01-07 11:09:26 -080030
Jonathan Bakker1cdbd3e2019-01-07 11:21:16 -080031#define ARIES_TOUCHKEY_CMD_LED_ON 0x1
32#define ARIES_TOUCHKEY_CMD_LED_OFF 0x2
Jaechul Lee72d1f232017-01-18 14:35:42 -080033#define TM2_TOUCHKEY_CMD_LED_ON 0x10
34#define TM2_TOUCHKEY_CMD_LED_OFF 0x20
35#define TM2_TOUCHKEY_BIT_PRESS_EV BIT(3)
36#define TM2_TOUCHKEY_BIT_KEYCODE GENMASK(2, 0)
37#define TM2_TOUCHKEY_LED_VOLTAGE_MIN 2500000
38#define TM2_TOUCHKEY_LED_VOLTAGE_MAX 3300000
39
Simon Shieldsd6f66f62019-01-07 11:09:26 -080040struct touchkey_variant {
41 u8 keycode_reg;
42 u8 base_reg;
Jonathan Bakker1cdbd3e2019-01-07 11:21:16 -080043 u8 cmd_led_on;
44 u8 cmd_led_off;
45 bool no_reg;
46 bool fixed_regulator;
Simon Shieldsd6f66f62019-01-07 11:09:26 -080047};
48
Jaechul Lee72d1f232017-01-18 14:35:42 -080049struct tm2_touchkey_data {
50 struct i2c_client *client;
51 struct input_dev *input_dev;
52 struct led_classdev led_dev;
53 struct regulator *vdd;
54 struct regulator_bulk_data regulators[2];
Simon Shieldsd6f66f62019-01-07 11:09:26 -080055 const struct touchkey_variant *variant;
Jonathan Bakker07df1c52019-01-07 11:11:55 -080056 u32 keycodes[4];
57 int num_keycodes;
Simon Shieldsd6f66f62019-01-07 11:09:26 -080058};
59
60static const struct touchkey_variant tm2_touchkey_variant = {
61 .keycode_reg = 0x03,
62 .base_reg = 0x00,
Jonathan Bakker1cdbd3e2019-01-07 11:21:16 -080063 .cmd_led_on = TM2_TOUCHKEY_CMD_LED_ON,
64 .cmd_led_off = TM2_TOUCHKEY_CMD_LED_OFF,
Simon Shieldsd6f66f62019-01-07 11:09:26 -080065};
66
67static const struct touchkey_variant midas_touchkey_variant = {
68 .keycode_reg = 0x00,
69 .base_reg = 0x00,
Jonathan Bakker1cdbd3e2019-01-07 11:21:16 -080070 .cmd_led_on = TM2_TOUCHKEY_CMD_LED_ON,
71 .cmd_led_off = TM2_TOUCHKEY_CMD_LED_OFF,
72};
73
74static struct touchkey_variant aries_touchkey_variant = {
75 .no_reg = true,
76 .fixed_regulator = true,
77 .cmd_led_on = ARIES_TOUCHKEY_CMD_LED_ON,
78 .cmd_led_off = ARIES_TOUCHKEY_CMD_LED_OFF,
Jaechul Lee72d1f232017-01-18 14:35:42 -080079};
80
81static void tm2_touchkey_led_brightness_set(struct led_classdev *led_dev,
82 enum led_brightness brightness)
83{
84 struct tm2_touchkey_data *touchkey =
85 container_of(led_dev, struct tm2_touchkey_data, led_dev);
86 u32 volt;
87 u8 data;
88
89 if (brightness == LED_OFF) {
90 volt = TM2_TOUCHKEY_LED_VOLTAGE_MIN;
Jonathan Bakker1cdbd3e2019-01-07 11:21:16 -080091 data = touchkey->variant->cmd_led_off;
Jaechul Lee72d1f232017-01-18 14:35:42 -080092 } else {
93 volt = TM2_TOUCHKEY_LED_VOLTAGE_MAX;
Jonathan Bakker1cdbd3e2019-01-07 11:21:16 -080094 data = touchkey->variant->cmd_led_on;
Jaechul Lee72d1f232017-01-18 14:35:42 -080095 }
96
Jonathan Bakker1cdbd3e2019-01-07 11:21:16 -080097 if (!touchkey->variant->fixed_regulator)
98 regulator_set_voltage(touchkey->vdd, volt, volt);
99
100 if (touchkey->variant->no_reg)
101 i2c_smbus_write_byte(touchkey->client, data);
102 else
103 i2c_smbus_write_byte_data(touchkey->client,
104 touchkey->variant->base_reg, data);
Jaechul Lee72d1f232017-01-18 14:35:42 -0800105}
106
107static int tm2_touchkey_power_enable(struct tm2_touchkey_data *touchkey)
108{
109 int error;
110
111 error = regulator_bulk_enable(ARRAY_SIZE(touchkey->regulators),
112 touchkey->regulators);
113 if (error)
114 return error;
115
116 /* waiting for device initialization, at least 150ms */
117 msleep(150);
118
119 return 0;
120}
121
122static void tm2_touchkey_power_disable(void *data)
123{
124 struct tm2_touchkey_data *touchkey = data;
125
126 regulator_bulk_disable(ARRAY_SIZE(touchkey->regulators),
127 touchkey->regulators);
128}
129
130static irqreturn_t tm2_touchkey_irq_handler(int irq, void *devid)
131{
132 struct tm2_touchkey_data *touchkey = devid;
133 int data;
Jonathan Bakker07df1c52019-01-07 11:11:55 -0800134 int index;
135 int i;
Jaechul Lee72d1f232017-01-18 14:35:42 -0800136
Jonathan Bakker1cdbd3e2019-01-07 11:21:16 -0800137 if (touchkey->variant->no_reg)
138 data = i2c_smbus_read_byte(touchkey->client);
139 else
140 data = i2c_smbus_read_byte_data(touchkey->client,
141 touchkey->variant->keycode_reg);
Jaechul Lee72d1f232017-01-18 14:35:42 -0800142 if (data < 0) {
143 dev_err(&touchkey->client->dev,
144 "failed to read i2c data: %d\n", data);
145 goto out;
146 }
147
Jonathan Bakker07df1c52019-01-07 11:11:55 -0800148 index = (data & TM2_TOUCHKEY_BIT_KEYCODE) - 1;
149 if (index < 0 || index >= touchkey->num_keycodes) {
Jaechul Lee72d1f232017-01-18 14:35:42 -0800150 dev_warn(&touchkey->client->dev,
Jonathan Bakker07df1c52019-01-07 11:11:55 -0800151 "invalid keycode index %d\n", index);
Jaechul Lee72d1f232017-01-18 14:35:42 -0800152 goto out;
153 }
154
155 if (data & TM2_TOUCHKEY_BIT_PRESS_EV) {
Jonathan Bakker07df1c52019-01-07 11:11:55 -0800156 for (i = 0; i < touchkey->num_keycodes; i++)
157 input_report_key(touchkey->input_dev,
158 touchkey->keycodes[i], 0);
Jaechul Lee72d1f232017-01-18 14:35:42 -0800159 } else {
Jonathan Bakker07df1c52019-01-07 11:11:55 -0800160 input_report_key(touchkey->input_dev,
161 touchkey->keycodes[index], 1);
Jaechul Lee72d1f232017-01-18 14:35:42 -0800162 }
163
164 input_sync(touchkey->input_dev);
165
166out:
Jonathan Bakker1cdbd3e2019-01-07 11:21:16 -0800167 if (touchkey->variant->fixed_regulator &&
168 data & TM2_TOUCHKEY_BIT_PRESS_EV) {
169 /* touch turns backlight on, so make sure we're in sync */
170 if (touchkey->led_dev.brightness == LED_OFF)
171 tm2_touchkey_led_brightness_set(&touchkey->led_dev,
172 LED_OFF);
173 }
174
Jaechul Lee72d1f232017-01-18 14:35:42 -0800175 return IRQ_HANDLED;
176}
177
178static int tm2_touchkey_probe(struct i2c_client *client,
179 const struct i2c_device_id *id)
180{
Jonathan Bakker07df1c52019-01-07 11:11:55 -0800181 struct device_node *np = client->dev.of_node;
Jaechul Lee72d1f232017-01-18 14:35:42 -0800182 struct tm2_touchkey_data *touchkey;
183 int error;
Jonathan Bakker07df1c52019-01-07 11:11:55 -0800184 int i;
Jaechul Lee72d1f232017-01-18 14:35:42 -0800185
186 if (!i2c_check_functionality(client->adapter,
187 I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA)) {
188 dev_err(&client->dev, "incompatible I2C adapter\n");
189 return -EIO;
190 }
191
192 touchkey = devm_kzalloc(&client->dev, sizeof(*touchkey), GFP_KERNEL);
193 if (!touchkey)
194 return -ENOMEM;
195
196 touchkey->client = client;
197 i2c_set_clientdata(client, touchkey);
198
Simon Shieldsd6f66f62019-01-07 11:09:26 -0800199 touchkey->variant = of_device_get_match_data(&client->dev);
200
Jaechul Lee72d1f232017-01-18 14:35:42 -0800201 touchkey->regulators[0].supply = "vcc";
202 touchkey->regulators[1].supply = "vdd";
203 error = devm_regulator_bulk_get(&client->dev,
204 ARRAY_SIZE(touchkey->regulators),
205 touchkey->regulators);
206 if (error) {
207 dev_err(&client->dev, "failed to get regulators: %d\n", error);
208 return error;
209 }
210
211 /* Save VDD for easy access */
212 touchkey->vdd = touchkey->regulators[1].consumer;
213
Jonathan Bakker07df1c52019-01-07 11:11:55 -0800214 touchkey->num_keycodes = of_property_read_variable_u32_array(np,
215 "linux,keycodes", touchkey->keycodes, 0,
216 ARRAY_SIZE(touchkey->keycodes));
217 if (touchkey->num_keycodes <= 0) {
218 /* default keycodes */
219 touchkey->keycodes[0] = KEY_PHONE;
220 touchkey->keycodes[1] = KEY_BACK;
221 touchkey->num_keycodes = 2;
222 }
223
Jaechul Lee72d1f232017-01-18 14:35:42 -0800224 error = tm2_touchkey_power_enable(touchkey);
225 if (error) {
226 dev_err(&client->dev, "failed to power up device: %d\n", error);
227 return error;
228 }
229
230 error = devm_add_action_or_reset(&client->dev,
231 tm2_touchkey_power_disable, touchkey);
232 if (error) {
233 dev_err(&client->dev,
234 "failed to install poweroff handler: %d\n", error);
235 return error;
236 }
237
238 /* input device */
239 touchkey->input_dev = devm_input_allocate_device(&client->dev);
240 if (!touchkey->input_dev) {
241 dev_err(&client->dev, "failed to allocate input device\n");
242 return -ENOMEM;
243 }
244
245 touchkey->input_dev->name = TM2_TOUCHKEY_DEV_NAME;
246 touchkey->input_dev->id.bustype = BUS_I2C;
247
Jonathan Bakker07df1c52019-01-07 11:11:55 -0800248 for (i = 0; i < touchkey->num_keycodes; i++)
249 input_set_capability(touchkey->input_dev, EV_KEY,
250 touchkey->keycodes[i]);
Jaechul Lee72d1f232017-01-18 14:35:42 -0800251
Jaechul Lee72d1f232017-01-18 14:35:42 -0800252 error = input_register_device(touchkey->input_dev);
253 if (error) {
254 dev_err(&client->dev,
255 "failed to register input device: %d\n", error);
256 return error;
257 }
258
259 error = devm_request_threaded_irq(&client->dev, client->irq,
260 NULL, tm2_touchkey_irq_handler,
261 IRQF_ONESHOT,
262 TM2_TOUCHKEY_DEV_NAME, touchkey);
263 if (error) {
264 dev_err(&client->dev,
265 "failed to request threaded irq: %d\n", error);
266 return error;
267 }
268
269 /* led device */
270 touchkey->led_dev.name = TM2_TOUCHKEY_DEV_NAME;
Jonathan Bakkerd5a158c2019-01-07 11:11:04 -0800271 touchkey->led_dev.brightness = LED_ON;
Andi Shytic4beedb2017-06-01 22:05:40 -0700272 touchkey->led_dev.max_brightness = LED_ON;
Jaechul Lee72d1f232017-01-18 14:35:42 -0800273 touchkey->led_dev.brightness_set = tm2_touchkey_led_brightness_set;
274
275 error = devm_led_classdev_register(&client->dev, &touchkey->led_dev);
276 if (error) {
277 dev_err(&client->dev,
278 "failed to register touchkey led: %d\n", error);
279 return error;
280 }
281
Jonathan Bakker1cdbd3e2019-01-07 11:21:16 -0800282 if (touchkey->variant->fixed_regulator)
283 tm2_touchkey_led_brightness_set(&touchkey->led_dev, LED_ON);
284
Jaechul Lee72d1f232017-01-18 14:35:42 -0800285 return 0;
286}
287
288static int __maybe_unused tm2_touchkey_suspend(struct device *dev)
289{
290 struct i2c_client *client = to_i2c_client(dev);
291 struct tm2_touchkey_data *touchkey = i2c_get_clientdata(client);
292
293 disable_irq(client->irq);
294 tm2_touchkey_power_disable(touchkey);
295
296 return 0;
297}
298
299static int __maybe_unused tm2_touchkey_resume(struct device *dev)
300{
301 struct i2c_client *client = to_i2c_client(dev);
302 struct tm2_touchkey_data *touchkey = i2c_get_clientdata(client);
303 int ret;
304
305 enable_irq(client->irq);
306
307 ret = tm2_touchkey_power_enable(touchkey);
308 if (ret)
309 dev_err(dev, "failed to enable power: %d\n", ret);
310
311 return ret;
312}
313
314static SIMPLE_DEV_PM_OPS(tm2_touchkey_pm_ops,
315 tm2_touchkey_suspend, tm2_touchkey_resume);
316
317static const struct i2c_device_id tm2_touchkey_id_table[] = {
318 { TM2_TOUCHKEY_DEV_NAME, 0 },
319 { },
320};
321MODULE_DEVICE_TABLE(i2c, tm2_touchkey_id_table);
322
323static const struct of_device_id tm2_touchkey_of_match[] = {
Simon Shieldsd6f66f62019-01-07 11:09:26 -0800324 {
325 .compatible = "cypress,tm2-touchkey",
326 .data = &tm2_touchkey_variant,
327 }, {
328 .compatible = "cypress,midas-touchkey",
329 .data = &midas_touchkey_variant,
Jonathan Bakker1cdbd3e2019-01-07 11:21:16 -0800330 }, {
331 .compatible = "cypress,aries-touchkey",
332 .data = &aries_touchkey_variant,
Simon Shieldsd6f66f62019-01-07 11:09:26 -0800333 },
Jaechul Lee72d1f232017-01-18 14:35:42 -0800334 { },
335};
336MODULE_DEVICE_TABLE(of, tm2_touchkey_of_match);
337
338static struct i2c_driver tm2_touchkey_driver = {
339 .driver = {
340 .name = TM2_TOUCHKEY_DEV_NAME,
341 .pm = &tm2_touchkey_pm_ops,
342 .of_match_table = of_match_ptr(tm2_touchkey_of_match),
343 },
344 .probe = tm2_touchkey_probe,
345 .id_table = tm2_touchkey_id_table,
346};
347module_i2c_driver(tm2_touchkey_driver);
348
349MODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>");
350MODULE_AUTHOR("Jaechul Lee <jcsing.lee@samsung.com>");
351MODULE_DESCRIPTION("Samsung touchkey driver");
352MODULE_LICENSE("GPL v2");