blob: 5a1fe08bdd76179bbd32870811becabbeafb5999 [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
Jaechul Lee72d1f232017-01-18 14:35:42 -080031#define TM2_TOUCHKEY_CMD_LED_ON 0x10
32#define TM2_TOUCHKEY_CMD_LED_OFF 0x20
33#define TM2_TOUCHKEY_BIT_PRESS_EV BIT(3)
34#define TM2_TOUCHKEY_BIT_KEYCODE GENMASK(2, 0)
35#define TM2_TOUCHKEY_LED_VOLTAGE_MIN 2500000
36#define TM2_TOUCHKEY_LED_VOLTAGE_MAX 3300000
37
38enum {
39 TM2_TOUCHKEY_KEY_MENU = 0x1,
40 TM2_TOUCHKEY_KEY_BACK,
41};
42
Simon Shieldsd6f66f62019-01-07 11:09:26 -080043struct touchkey_variant {
44 u8 keycode_reg;
45 u8 base_reg;
46};
47
Jaechul Lee72d1f232017-01-18 14:35:42 -080048struct tm2_touchkey_data {
49 struct i2c_client *client;
50 struct input_dev *input_dev;
51 struct led_classdev led_dev;
52 struct regulator *vdd;
53 struct regulator_bulk_data regulators[2];
Simon Shieldsd6f66f62019-01-07 11:09:26 -080054 const struct touchkey_variant *variant;
55};
56
57static const struct touchkey_variant tm2_touchkey_variant = {
58 .keycode_reg = 0x03,
59 .base_reg = 0x00,
60};
61
62static const struct touchkey_variant midas_touchkey_variant = {
63 .keycode_reg = 0x00,
64 .base_reg = 0x00,
Jaechul Lee72d1f232017-01-18 14:35:42 -080065};
66
67static void tm2_touchkey_led_brightness_set(struct led_classdev *led_dev,
68 enum led_brightness brightness)
69{
70 struct tm2_touchkey_data *touchkey =
71 container_of(led_dev, struct tm2_touchkey_data, led_dev);
72 u32 volt;
73 u8 data;
74
75 if (brightness == LED_OFF) {
76 volt = TM2_TOUCHKEY_LED_VOLTAGE_MIN;
77 data = TM2_TOUCHKEY_CMD_LED_OFF;
78 } else {
79 volt = TM2_TOUCHKEY_LED_VOLTAGE_MAX;
80 data = TM2_TOUCHKEY_CMD_LED_ON;
81 }
82
83 regulator_set_voltage(touchkey->vdd, volt, volt);
84 i2c_smbus_write_byte_data(touchkey->client,
Simon Shieldsd6f66f62019-01-07 11:09:26 -080085 touchkey->variant->base_reg, data);
Jaechul Lee72d1f232017-01-18 14:35:42 -080086}
87
88static int tm2_touchkey_power_enable(struct tm2_touchkey_data *touchkey)
89{
90 int error;
91
92 error = regulator_bulk_enable(ARRAY_SIZE(touchkey->regulators),
93 touchkey->regulators);
94 if (error)
95 return error;
96
97 /* waiting for device initialization, at least 150ms */
98 msleep(150);
99
100 return 0;
101}
102
103static void tm2_touchkey_power_disable(void *data)
104{
105 struct tm2_touchkey_data *touchkey = data;
106
107 regulator_bulk_disable(ARRAY_SIZE(touchkey->regulators),
108 touchkey->regulators);
109}
110
111static irqreturn_t tm2_touchkey_irq_handler(int irq, void *devid)
112{
113 struct tm2_touchkey_data *touchkey = devid;
114 int data;
115 int key;
116
117 data = i2c_smbus_read_byte_data(touchkey->client,
Simon Shieldsd6f66f62019-01-07 11:09:26 -0800118 touchkey->variant->keycode_reg);
Jaechul Lee72d1f232017-01-18 14:35:42 -0800119 if (data < 0) {
120 dev_err(&touchkey->client->dev,
121 "failed to read i2c data: %d\n", data);
122 goto out;
123 }
124
125 switch (data & TM2_TOUCHKEY_BIT_KEYCODE) {
126 case TM2_TOUCHKEY_KEY_MENU:
127 key = KEY_PHONE;
128 break;
129
130 case TM2_TOUCHKEY_KEY_BACK:
131 key = KEY_BACK;
132 break;
133
134 default:
135 dev_warn(&touchkey->client->dev,
136 "unhandled keycode, data %#02x\n", data);
137 goto out;
138 }
139
140 if (data & TM2_TOUCHKEY_BIT_PRESS_EV) {
141 input_report_key(touchkey->input_dev, KEY_PHONE, 0);
142 input_report_key(touchkey->input_dev, KEY_BACK, 0);
143 } else {
144 input_report_key(touchkey->input_dev, key, 1);
145 }
146
147 input_sync(touchkey->input_dev);
148
149out:
150 return IRQ_HANDLED;
151}
152
153static int tm2_touchkey_probe(struct i2c_client *client,
154 const struct i2c_device_id *id)
155{
156 struct tm2_touchkey_data *touchkey;
157 int error;
158
159 if (!i2c_check_functionality(client->adapter,
160 I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA)) {
161 dev_err(&client->dev, "incompatible I2C adapter\n");
162 return -EIO;
163 }
164
165 touchkey = devm_kzalloc(&client->dev, sizeof(*touchkey), GFP_KERNEL);
166 if (!touchkey)
167 return -ENOMEM;
168
169 touchkey->client = client;
170 i2c_set_clientdata(client, touchkey);
171
Simon Shieldsd6f66f62019-01-07 11:09:26 -0800172 touchkey->variant = of_device_get_match_data(&client->dev);
173
Jaechul Lee72d1f232017-01-18 14:35:42 -0800174 touchkey->regulators[0].supply = "vcc";
175 touchkey->regulators[1].supply = "vdd";
176 error = devm_regulator_bulk_get(&client->dev,
177 ARRAY_SIZE(touchkey->regulators),
178 touchkey->regulators);
179 if (error) {
180 dev_err(&client->dev, "failed to get regulators: %d\n", error);
181 return error;
182 }
183
184 /* Save VDD for easy access */
185 touchkey->vdd = touchkey->regulators[1].consumer;
186
187 error = tm2_touchkey_power_enable(touchkey);
188 if (error) {
189 dev_err(&client->dev, "failed to power up device: %d\n", error);
190 return error;
191 }
192
193 error = devm_add_action_or_reset(&client->dev,
194 tm2_touchkey_power_disable, touchkey);
195 if (error) {
196 dev_err(&client->dev,
197 "failed to install poweroff handler: %d\n", error);
198 return error;
199 }
200
201 /* input device */
202 touchkey->input_dev = devm_input_allocate_device(&client->dev);
203 if (!touchkey->input_dev) {
204 dev_err(&client->dev, "failed to allocate input device\n");
205 return -ENOMEM;
206 }
207
208 touchkey->input_dev->name = TM2_TOUCHKEY_DEV_NAME;
209 touchkey->input_dev->id.bustype = BUS_I2C;
210
211 input_set_capability(touchkey->input_dev, EV_KEY, KEY_PHONE);
212 input_set_capability(touchkey->input_dev, EV_KEY, KEY_BACK);
213
Jaechul Lee72d1f232017-01-18 14:35:42 -0800214 error = input_register_device(touchkey->input_dev);
215 if (error) {
216 dev_err(&client->dev,
217 "failed to register input device: %d\n", error);
218 return error;
219 }
220
221 error = devm_request_threaded_irq(&client->dev, client->irq,
222 NULL, tm2_touchkey_irq_handler,
223 IRQF_ONESHOT,
224 TM2_TOUCHKEY_DEV_NAME, touchkey);
225 if (error) {
226 dev_err(&client->dev,
227 "failed to request threaded irq: %d\n", error);
228 return error;
229 }
230
231 /* led device */
232 touchkey->led_dev.name = TM2_TOUCHKEY_DEV_NAME;
233 touchkey->led_dev.brightness = LED_FULL;
Andi Shytic4beedb2017-06-01 22:05:40 -0700234 touchkey->led_dev.max_brightness = LED_ON;
Jaechul Lee72d1f232017-01-18 14:35:42 -0800235 touchkey->led_dev.brightness_set = tm2_touchkey_led_brightness_set;
236
237 error = devm_led_classdev_register(&client->dev, &touchkey->led_dev);
238 if (error) {
239 dev_err(&client->dev,
240 "failed to register touchkey led: %d\n", error);
241 return error;
242 }
243
244 return 0;
245}
246
247static int __maybe_unused tm2_touchkey_suspend(struct device *dev)
248{
249 struct i2c_client *client = to_i2c_client(dev);
250 struct tm2_touchkey_data *touchkey = i2c_get_clientdata(client);
251
252 disable_irq(client->irq);
253 tm2_touchkey_power_disable(touchkey);
254
255 return 0;
256}
257
258static int __maybe_unused tm2_touchkey_resume(struct device *dev)
259{
260 struct i2c_client *client = to_i2c_client(dev);
261 struct tm2_touchkey_data *touchkey = i2c_get_clientdata(client);
262 int ret;
263
264 enable_irq(client->irq);
265
266 ret = tm2_touchkey_power_enable(touchkey);
267 if (ret)
268 dev_err(dev, "failed to enable power: %d\n", ret);
269
270 return ret;
271}
272
273static SIMPLE_DEV_PM_OPS(tm2_touchkey_pm_ops,
274 tm2_touchkey_suspend, tm2_touchkey_resume);
275
276static const struct i2c_device_id tm2_touchkey_id_table[] = {
277 { TM2_TOUCHKEY_DEV_NAME, 0 },
278 { },
279};
280MODULE_DEVICE_TABLE(i2c, tm2_touchkey_id_table);
281
282static const struct of_device_id tm2_touchkey_of_match[] = {
Simon Shieldsd6f66f62019-01-07 11:09:26 -0800283 {
284 .compatible = "cypress,tm2-touchkey",
285 .data = &tm2_touchkey_variant,
286 }, {
287 .compatible = "cypress,midas-touchkey",
288 .data = &midas_touchkey_variant,
289 },
Jaechul Lee72d1f232017-01-18 14:35:42 -0800290 { },
291};
292MODULE_DEVICE_TABLE(of, tm2_touchkey_of_match);
293
294static struct i2c_driver tm2_touchkey_driver = {
295 .driver = {
296 .name = TM2_TOUCHKEY_DEV_NAME,
297 .pm = &tm2_touchkey_pm_ops,
298 .of_match_table = of_match_ptr(tm2_touchkey_of_match),
299 },
300 .probe = tm2_touchkey_probe,
301 .id_table = tm2_touchkey_id_table,
302};
303module_i2c_driver(tm2_touchkey_driver);
304
305MODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>");
306MODULE_AUTHOR("Jaechul Lee <jcsing.lee@samsung.com>");
307MODULE_DESCRIPTION("Samsung touchkey driver");
308MODULE_LICENSE("GPL v2");