1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Novatek NT11205 i2c touchscreen controller as found
4 * on the Acer Iconia One 7 B1-750 tablet.
9 #include <linux/delay.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/interrupt.h>
12 #include <linux/i2c.h>
13 #include <linux/input.h>
14 #include <linux/input/mt.h>
15 #include <linux/input/touchscreen.h>
16 #include <linux/module.h>
18 #include <asm/unaligned.h>
20 #define NVT_TS_TOUCH_START 0x00
21 #define NVT_TS_TOUCH_SIZE 6
23 #define NVT_TS_PARAMETERS_START 0x78
24 /* These are offsets from NVT_TS_PARAMETERS_START */
25 #define NVT_TS_PARAMS_WIDTH 0x04
26 #define NVT_TS_PARAMS_HEIGHT 0x06
27 #define NVT_TS_PARAMS_MAX_TOUCH 0x09
28 #define NVT_TS_PARAMS_MAX_BUTTONS 0x0a
29 #define NVT_TS_PARAMS_IRQ_TYPE 0x0b
30 #define NVT_TS_PARAMS_WAKE_TYPE 0x0c
31 #define NVT_TS_PARAMS_CHIP_ID 0x0e
32 #define NVT_TS_PARAMS_SIZE 0x0f
34 #define NVT_TS_SUPPORTED_WAKE_TYPE 0x05
35 #define NVT_TS_SUPPORTED_CHIP_ID 0x05
37 #define NVT_TS_MAX_TOUCHES 10
38 #define NVT_TS_MAX_SIZE 4096
40 #define NVT_TS_TOUCH_INVALID 0xff
41 #define NVT_TS_TOUCH_SLOT_SHIFT 3
42 #define NVT_TS_TOUCH_TYPE_MASK GENMASK(2, 0)
43 #define NVT_TS_TOUCH_NEW 1
44 #define NVT_TS_TOUCH_UPDATE 2
45 #define NVT_TS_TOUCH_RELEASE 3
47 static const int nvt_ts_irq_type[4] = {
55 struct i2c_client *client;
56 struct input_dev *input;
57 struct gpio_desc *reset_gpio;
58 struct touchscreen_properties prop;
60 u8 buf[NVT_TS_TOUCH_SIZE * NVT_TS_MAX_TOUCHES];
63 static int nvt_ts_read_data(struct i2c_client *client, u8 reg, u8 *data, int count)
65 struct i2c_msg msg[2] = {
80 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
81 if (ret != ARRAY_SIZE(msg)) {
82 dev_err(&client->dev, "Error reading from 0x%02x: %d\n", reg, ret);
83 return (ret < 0) ? ret : -EIO;
89 static irqreturn_t nvt_ts_irq(int irq, void *dev_id)
91 struct nvt_ts_data *data = dev_id;
92 struct device *dev = &data->client->dev;
93 int i, error, slot, x, y;
97 error = nvt_ts_read_data(data->client, NVT_TS_TOUCH_START, data->buf,
98 data->max_touches * NVT_TS_TOUCH_SIZE);
102 for (i = 0; i < data->max_touches; i++) {
103 touch = &data->buf[i * NVT_TS_TOUCH_SIZE];
105 if (touch[0] == NVT_TS_TOUCH_INVALID)
108 slot = touch[0] >> NVT_TS_TOUCH_SLOT_SHIFT;
109 if (slot < 1 || slot > data->max_touches) {
110 dev_warn(dev, "slot %d out of range, ignoring\n", slot);
114 switch (touch[0] & NVT_TS_TOUCH_TYPE_MASK) {
115 case NVT_TS_TOUCH_NEW:
116 case NVT_TS_TOUCH_UPDATE:
119 case NVT_TS_TOUCH_RELEASE:
123 dev_warn(dev, "slot %d unknown state %d\n", slot, touch[0] & 7);
128 x = (touch[1] << 4) | (touch[3] >> 4);
129 y = (touch[2] << 4) | (touch[3] & 0x0f);
131 input_mt_slot(data->input, slot);
132 input_mt_report_slot_state(data->input, MT_TOOL_FINGER, active);
133 touchscreen_report_pos(data->input, &data->prop, x, y, true);
136 input_mt_sync_frame(data->input);
137 input_sync(data->input);
142 static int nvt_ts_start(struct input_dev *dev)
144 struct nvt_ts_data *data = input_get_drvdata(dev);
146 enable_irq(data->client->irq);
147 gpiod_set_value_cansleep(data->reset_gpio, 0);
152 static void nvt_ts_stop(struct input_dev *dev)
154 struct nvt_ts_data *data = input_get_drvdata(dev);
156 disable_irq(data->client->irq);
157 gpiod_set_value_cansleep(data->reset_gpio, 1);
160 static int nvt_ts_suspend(struct device *dev)
162 struct nvt_ts_data *data = i2c_get_clientdata(to_i2c_client(dev));
164 mutex_lock(&data->input->mutex);
165 if (input_device_enabled(data->input))
166 nvt_ts_stop(data->input);
167 mutex_unlock(&data->input->mutex);
172 static int nvt_ts_resume(struct device *dev)
174 struct nvt_ts_data *data = i2c_get_clientdata(to_i2c_client(dev));
176 mutex_lock(&data->input->mutex);
177 if (input_device_enabled(data->input))
178 nvt_ts_start(data->input);
179 mutex_unlock(&data->input->mutex);
184 static DEFINE_SIMPLE_DEV_PM_OPS(nvt_ts_pm_ops, nvt_ts_suspend, nvt_ts_resume);
186 static int nvt_ts_probe(struct i2c_client *client)
188 struct device *dev = &client->dev;
189 int error, width, height, irq_type;
190 struct nvt_ts_data *data;
191 struct input_dev *input;
194 dev_err(dev, "Error no irq specified\n");
198 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
202 data->client = client;
203 i2c_set_clientdata(client, data);
205 data->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
206 error = PTR_ERR_OR_ZERO(data->reset_gpio);
208 dev_err(dev, "failed to request reset GPIO: %d\n", error);
212 /* Wait for controller to come out of reset before params read */
214 error = nvt_ts_read_data(data->client, NVT_TS_PARAMETERS_START,
215 data->buf, NVT_TS_PARAMS_SIZE);
216 gpiod_set_value_cansleep(data->reset_gpio, 1); /* Put back in reset */
220 width = get_unaligned_be16(&data->buf[NVT_TS_PARAMS_WIDTH]);
221 height = get_unaligned_be16(&data->buf[NVT_TS_PARAMS_HEIGHT]);
222 data->max_touches = data->buf[NVT_TS_PARAMS_MAX_TOUCH];
223 irq_type = data->buf[NVT_TS_PARAMS_IRQ_TYPE];
225 if (width > NVT_TS_MAX_SIZE || height >= NVT_TS_MAX_SIZE ||
226 data->max_touches > NVT_TS_MAX_TOUCHES ||
227 irq_type >= ARRAY_SIZE(nvt_ts_irq_type) ||
228 data->buf[NVT_TS_PARAMS_WAKE_TYPE] != NVT_TS_SUPPORTED_WAKE_TYPE ||
229 data->buf[NVT_TS_PARAMS_CHIP_ID] != NVT_TS_SUPPORTED_CHIP_ID) {
230 dev_err(dev, "Unsupported touchscreen parameters: %*ph\n",
231 NVT_TS_PARAMS_SIZE, data->buf);
235 dev_dbg(dev, "Detected %dx%d touchscreen with %d max touches\n",
236 width, height, data->max_touches);
238 if (data->buf[NVT_TS_PARAMS_MAX_BUTTONS])
239 dev_warn(dev, "Touchscreen buttons are not supported\n");
241 input = devm_input_allocate_device(dev);
245 input->name = client->name;
246 input->id.bustype = BUS_I2C;
247 input->open = nvt_ts_start;
248 input->close = nvt_ts_stop;
250 input_set_abs_params(input, ABS_MT_POSITION_X, 0, width - 1, 0, 0);
251 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, height - 1, 0, 0);
252 touchscreen_parse_properties(input, true, &data->prop);
254 error = input_mt_init_slots(input, data->max_touches,
255 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
260 input_set_drvdata(input, data);
262 error = devm_request_threaded_irq(dev, client->irq, NULL, nvt_ts_irq,
263 IRQF_ONESHOT | IRQF_NO_AUTOEN |
264 nvt_ts_irq_type[irq_type],
267 dev_err(dev, "failed to request irq: %d\n", error);
271 error = input_register_device(input);
273 dev_err(dev, "failed to register input device: %d\n", error);
280 static const struct i2c_device_id nvt_ts_i2c_id[] = {
284 MODULE_DEVICE_TABLE(i2c, nvt_ts_i2c_id);
286 static struct i2c_driver nvt_ts_driver = {
288 .name = "novatek-nvt-ts",
289 .pm = pm_sleep_ptr(&nvt_ts_pm_ops),
291 .probe = nvt_ts_probe,
292 .id_table = nvt_ts_i2c_id,
295 module_i2c_driver(nvt_ts_driver);
297 MODULE_DESCRIPTION("Novatek NT11205 touchscreen driver");
299 MODULE_LICENSE("GPL");