1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Touchkey driver for MELFAS MCS5000/5080 controller
5 * Copyright (C) 2010 Samsung Electronics Co.Ltd
6 * Author: HeungJun Kim <riverful.kim@samsung.com>
7 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
10 #include <linux/module.h>
11 #include <linux/i2c.h>
12 #include <linux/interrupt.h>
13 #include <linux/input.h>
14 #include <linux/irq.h>
15 #include <linux/slab.h>
16 #include <linux/platform_data/mcs.h>
19 /* MCS5000 Touchkey */
20 #define MCS5000_TOUCHKEY_STATUS 0x04
21 #define MCS5000_TOUCHKEY_STATUS_PRESS 7
22 #define MCS5000_TOUCHKEY_FW 0x0a
23 #define MCS5000_TOUCHKEY_BASE_VAL 0x61
25 /* MCS5080 Touchkey */
26 #define MCS5080_TOUCHKEY_STATUS 0x00
27 #define MCS5080_TOUCHKEY_STATUS_PRESS 3
28 #define MCS5080_TOUCHKEY_FW 0x01
29 #define MCS5080_TOUCHKEY_BASE_VAL 0x1
31 enum mcs_touchkey_type {
36 struct mcs_touchkey_chip {
37 unsigned int status_reg;
38 unsigned int pressbit;
39 unsigned int press_invert;
43 struct mcs_touchkey_data {
44 void (*poweron)(bool);
46 struct i2c_client *client;
47 struct input_dev *input_dev;
48 struct mcs_touchkey_chip chip;
49 unsigned int key_code;
51 unsigned short keycodes[];
54 static irqreturn_t mcs_touchkey_interrupt(int irq, void *dev_id)
56 struct mcs_touchkey_data *data = dev_id;
57 struct mcs_touchkey_chip *chip = &data->chip;
58 struct i2c_client *client = data->client;
59 struct input_dev *input = data->input_dev;
64 val = i2c_smbus_read_byte_data(client, chip->status_reg);
66 dev_err(&client->dev, "i2c read error [%d]\n", val);
70 pressed = (val & (1 << chip->pressbit)) >> chip->pressbit;
71 if (chip->press_invert)
72 pressed ^= chip->press_invert;
74 /* key_val is 0 when released, so we should use key_val of press. */
76 key_val = val & (0xff >> (8 - chip->pressbit));
79 key_val -= chip->baseval;
80 data->key_code = data->keycodes[key_val];
81 data->key_val = key_val;
84 input_event(input, EV_MSC, MSC_SCAN, data->key_val);
85 input_report_key(input, data->key_code, pressed);
88 dev_dbg(&client->dev, "key %d %d %s\n", data->key_val, data->key_code,
89 pressed ? "pressed" : "released");
95 static void mcs_touchkey_poweroff(void *data)
97 struct mcs_touchkey_data *touchkey = data;
99 touchkey->poweron(false);
102 static int mcs_touchkey_probe(struct i2c_client *client)
104 const struct i2c_device_id *id = i2c_client_get_device_id(client);
105 const struct mcs_platform_data *pdata;
106 struct mcs_touchkey_data *data;
107 struct input_dev *input_dev;
113 pdata = dev_get_platdata(&client->dev);
115 dev_err(&client->dev, "no platform data defined\n");
119 data = devm_kzalloc(&client->dev,
120 struct_size(data, keycodes, pdata->key_maxval + 1),
125 input_dev = devm_input_allocate_device(&client->dev);
127 dev_err(&client->dev, "Failed to allocate input device\n");
131 data->client = client;
132 data->input_dev = input_dev;
134 if (id->driver_data == MCS5000_TOUCHKEY) {
135 data->chip.status_reg = MCS5000_TOUCHKEY_STATUS;
136 data->chip.pressbit = MCS5000_TOUCHKEY_STATUS_PRESS;
137 data->chip.baseval = MCS5000_TOUCHKEY_BASE_VAL;
138 fw_reg = MCS5000_TOUCHKEY_FW;
140 data->chip.status_reg = MCS5080_TOUCHKEY_STATUS;
141 data->chip.pressbit = MCS5080_TOUCHKEY_STATUS_PRESS;
142 data->chip.press_invert = 1;
143 data->chip.baseval = MCS5080_TOUCHKEY_BASE_VAL;
144 fw_reg = MCS5080_TOUCHKEY_FW;
147 fw_ver = i2c_smbus_read_byte_data(client, fw_reg);
149 dev_err(&client->dev, "i2c read error[%d]\n", fw_ver);
152 dev_info(&client->dev, "Firmware version: %d\n", fw_ver);
154 input_dev->name = "MELFAS MCS Touchkey";
155 input_dev->id.bustype = BUS_I2C;
156 input_dev->evbit[0] = BIT_MASK(EV_KEY);
157 if (!pdata->no_autorepeat)
158 input_dev->evbit[0] |= BIT_MASK(EV_REP);
159 input_dev->keycode = data->keycodes;
160 input_dev->keycodesize = sizeof(data->keycodes[0]);
161 input_dev->keycodemax = pdata->key_maxval + 1;
163 for (i = 0; i < pdata->keymap_size; i++) {
164 unsigned int val = MCS_KEY_VAL(pdata->keymap[i]);
165 unsigned int code = MCS_KEY_CODE(pdata->keymap[i]);
167 data->keycodes[val] = code;
168 __set_bit(code, input_dev->keybit);
171 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
172 input_set_drvdata(input_dev, data);
177 if (pdata->poweron) {
178 data->poweron = pdata->poweron;
181 error = devm_add_action_or_reset(&client->dev,
182 mcs_touchkey_poweroff, data);
187 error = devm_request_threaded_irq(&client->dev, client->irq,
188 NULL, mcs_touchkey_interrupt,
189 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
190 client->dev.driver->name, data);
192 dev_err(&client->dev, "Failed to register interrupt\n");
196 error = input_register_device(input_dev);
200 i2c_set_clientdata(client, data);
204 static void mcs_touchkey_shutdown(struct i2c_client *client)
206 struct mcs_touchkey_data *data = i2c_get_clientdata(client);
209 data->poweron(false);
212 static int mcs_touchkey_suspend(struct device *dev)
214 struct mcs_touchkey_data *data = dev_get_drvdata(dev);
215 struct i2c_client *client = data->client;
217 /* Disable the work */
218 disable_irq(client->irq);
220 /* Finally turn off the power */
222 data->poweron(false);
227 static int mcs_touchkey_resume(struct device *dev)
229 struct mcs_touchkey_data *data = dev_get_drvdata(dev);
230 struct i2c_client *client = data->client;
232 /* Enable the device first */
236 /* Enable irq again */
237 enable_irq(client->irq);
242 static DEFINE_SIMPLE_DEV_PM_OPS(mcs_touchkey_pm_ops,
243 mcs_touchkey_suspend, mcs_touchkey_resume);
245 static const struct i2c_device_id mcs_touchkey_id[] = {
246 { "mcs5000_touchkey", MCS5000_TOUCHKEY },
247 { "mcs5080_touchkey", MCS5080_TOUCHKEY },
250 MODULE_DEVICE_TABLE(i2c, mcs_touchkey_id);
252 static struct i2c_driver mcs_touchkey_driver = {
254 .name = "mcs_touchkey",
255 .pm = pm_sleep_ptr(&mcs_touchkey_pm_ops),
257 .probe = mcs_touchkey_probe,
258 .shutdown = mcs_touchkey_shutdown,
259 .id_table = mcs_touchkey_id,
262 module_i2c_driver(mcs_touchkey_driver);
264 /* Module information */
265 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
266 MODULE_AUTHOR("HeungJun Kim <riverful.kim@samsung.com>");
267 MODULE_DESCRIPTION("Touchkey driver for MELFAS MCS5000/5080 controller");
268 MODULE_LICENSE("GPL");