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.
6 * Copyright (c) 2023 Hans de Goede <hdegoede@redhat.com>
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 <linux/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_MAX_TOUCHES 10
35 #define NVT_TS_MAX_SIZE 4096
37 #define NVT_TS_TOUCH_INVALID 0xff
38 #define NVT_TS_TOUCH_SLOT_SHIFT 3
39 #define NVT_TS_TOUCH_TYPE_MASK GENMASK(2, 0)
40 #define NVT_TS_TOUCH_NEW 1
41 #define NVT_TS_TOUCH_UPDATE 2
42 #define NVT_TS_TOUCH_RELEASE 3
44 static const int nvt_ts_irq_type
[4] = {
51 struct nvt_ts_i2c_chip_data
{
57 struct i2c_client
*client
;
58 struct input_dev
*input
;
59 struct gpio_desc
*reset_gpio
;
60 struct regulator_bulk_data regulators
[2];
61 struct touchscreen_properties prop
;
63 u8 buf
[NVT_TS_TOUCH_SIZE
* NVT_TS_MAX_TOUCHES
];
66 static int nvt_ts_read_data(struct i2c_client
*client
, u8 reg
, u8
*data
, int count
)
68 struct i2c_msg msg
[2] = {
83 ret
= i2c_transfer(client
->adapter
, msg
, ARRAY_SIZE(msg
));
84 if (ret
!= ARRAY_SIZE(msg
)) {
85 dev_err(&client
->dev
, "Error reading from 0x%02x: %d\n", reg
, ret
);
86 return (ret
< 0) ? ret
: -EIO
;
92 static irqreturn_t
nvt_ts_irq(int irq
, void *dev_id
)
94 struct nvt_ts_data
*data
= dev_id
;
95 struct device
*dev
= &data
->client
->dev
;
96 int i
, error
, slot
, x
, y
;
100 error
= nvt_ts_read_data(data
->client
, NVT_TS_TOUCH_START
, data
->buf
,
101 data
->max_touches
* NVT_TS_TOUCH_SIZE
);
105 for (i
= 0; i
< data
->max_touches
; i
++) {
106 touch
= &data
->buf
[i
* NVT_TS_TOUCH_SIZE
];
108 if (touch
[0] == NVT_TS_TOUCH_INVALID
)
111 slot
= touch
[0] >> NVT_TS_TOUCH_SLOT_SHIFT
;
112 if (slot
< 1 || slot
> data
->max_touches
) {
113 dev_warn(dev
, "slot %d out of range, ignoring\n", slot
);
117 switch (touch
[0] & NVT_TS_TOUCH_TYPE_MASK
) {
118 case NVT_TS_TOUCH_NEW
:
119 case NVT_TS_TOUCH_UPDATE
:
122 case NVT_TS_TOUCH_RELEASE
:
126 dev_warn(dev
, "slot %d unknown state %d\n", slot
, touch
[0] & 7);
131 x
= (touch
[1] << 4) | (touch
[3] >> 4);
132 y
= (touch
[2] << 4) | (touch
[3] & 0x0f);
134 input_mt_slot(data
->input
, slot
);
135 input_mt_report_slot_state(data
->input
, MT_TOOL_FINGER
, active
);
136 touchscreen_report_pos(data
->input
, &data
->prop
, x
, y
, true);
139 input_mt_sync_frame(data
->input
);
140 input_sync(data
->input
);
145 static int nvt_ts_start(struct input_dev
*dev
)
147 struct nvt_ts_data
*data
= input_get_drvdata(dev
);
150 error
= regulator_bulk_enable(ARRAY_SIZE(data
->regulators
), data
->regulators
);
152 dev_err(&data
->client
->dev
, "failed to enable regulators\n");
156 enable_irq(data
->client
->irq
);
157 gpiod_set_value_cansleep(data
->reset_gpio
, 0);
162 static void nvt_ts_stop(struct input_dev
*dev
)
164 struct nvt_ts_data
*data
= input_get_drvdata(dev
);
166 disable_irq(data
->client
->irq
);
167 gpiod_set_value_cansleep(data
->reset_gpio
, 1);
168 regulator_bulk_disable(ARRAY_SIZE(data
->regulators
), data
->regulators
);
171 static int nvt_ts_suspend(struct device
*dev
)
173 struct nvt_ts_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
175 mutex_lock(&data
->input
->mutex
);
176 if (input_device_enabled(data
->input
))
177 nvt_ts_stop(data
->input
);
178 mutex_unlock(&data
->input
->mutex
);
183 static int nvt_ts_resume(struct device
*dev
)
185 struct nvt_ts_data
*data
= i2c_get_clientdata(to_i2c_client(dev
));
187 mutex_lock(&data
->input
->mutex
);
188 if (input_device_enabled(data
->input
))
189 nvt_ts_start(data
->input
);
190 mutex_unlock(&data
->input
->mutex
);
195 static DEFINE_SIMPLE_DEV_PM_OPS(nvt_ts_pm_ops
, nvt_ts_suspend
, nvt_ts_resume
);
197 static int nvt_ts_probe(struct i2c_client
*client
)
199 struct device
*dev
= &client
->dev
;
200 int error
, width
, height
, irq_type
;
201 struct nvt_ts_data
*data
;
202 const struct nvt_ts_i2c_chip_data
*chip
;
203 struct input_dev
*input
;
206 dev_err(dev
, "Error no irq specified\n");
210 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
214 chip
= device_get_match_data(&client
->dev
);
218 data
->client
= client
;
219 i2c_set_clientdata(client
, data
);
222 * VCC is the analog voltage supply
223 * IOVCC is the digital voltage supply
225 data
->regulators
[0].supply
= "vcc";
226 data
->regulators
[1].supply
= "iovcc";
227 error
= devm_regulator_bulk_get(dev
, ARRAY_SIZE(data
->regulators
), data
->regulators
);
229 dev_err(dev
, "cannot get regulators: %d\n", error
);
233 error
= regulator_bulk_enable(ARRAY_SIZE(data
->regulators
), data
->regulators
);
235 dev_err(dev
, "failed to enable regulators: %d\n", error
);
239 data
->reset_gpio
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_LOW
);
240 error
= PTR_ERR_OR_ZERO(data
->reset_gpio
);
242 regulator_bulk_disable(ARRAY_SIZE(data
->regulators
), data
->regulators
);
243 dev_err(dev
, "failed to request reset GPIO: %d\n", error
);
247 /* Wait for controller to come out of reset before params read */
249 error
= nvt_ts_read_data(data
->client
, NVT_TS_PARAMETERS_START
,
250 data
->buf
, NVT_TS_PARAMS_SIZE
);
251 gpiod_set_value_cansleep(data
->reset_gpio
, 1); /* Put back in reset */
252 regulator_bulk_disable(ARRAY_SIZE(data
->regulators
), data
->regulators
);
256 width
= get_unaligned_be16(&data
->buf
[NVT_TS_PARAMS_WIDTH
]);
257 height
= get_unaligned_be16(&data
->buf
[NVT_TS_PARAMS_HEIGHT
]);
258 data
->max_touches
= data
->buf
[NVT_TS_PARAMS_MAX_TOUCH
];
259 irq_type
= data
->buf
[NVT_TS_PARAMS_IRQ_TYPE
];
261 if (width
> NVT_TS_MAX_SIZE
|| height
>= NVT_TS_MAX_SIZE
||
262 data
->max_touches
> NVT_TS_MAX_TOUCHES
||
263 irq_type
>= ARRAY_SIZE(nvt_ts_irq_type
) ||
264 data
->buf
[NVT_TS_PARAMS_WAKE_TYPE
] != chip
->wake_type
||
265 data
->buf
[NVT_TS_PARAMS_CHIP_ID
] != chip
->chip_id
) {
266 dev_err(dev
, "Unsupported touchscreen parameters: %*ph\n",
267 NVT_TS_PARAMS_SIZE
, data
->buf
);
271 dev_dbg(dev
, "Detected %dx%d touchscreen with %d max touches\n",
272 width
, height
, data
->max_touches
);
274 if (data
->buf
[NVT_TS_PARAMS_MAX_BUTTONS
])
275 dev_warn(dev
, "Touchscreen buttons are not supported\n");
277 input
= devm_input_allocate_device(dev
);
281 input
->name
= client
->name
;
282 input
->id
.bustype
= BUS_I2C
;
283 input
->open
= nvt_ts_start
;
284 input
->close
= nvt_ts_stop
;
286 input_set_abs_params(input
, ABS_MT_POSITION_X
, 0, width
- 1, 0, 0);
287 input_set_abs_params(input
, ABS_MT_POSITION_Y
, 0, height
- 1, 0, 0);
288 touchscreen_parse_properties(input
, true, &data
->prop
);
290 error
= input_mt_init_slots(input
, data
->max_touches
,
291 INPUT_MT_DIRECT
| INPUT_MT_DROP_UNUSED
);
296 input_set_drvdata(input
, data
);
298 error
= devm_request_threaded_irq(dev
, client
->irq
, NULL
, nvt_ts_irq
,
299 IRQF_ONESHOT
| IRQF_NO_AUTOEN
|
300 nvt_ts_irq_type
[irq_type
],
303 dev_err(dev
, "failed to request irq: %d\n", error
);
307 error
= input_register_device(input
);
309 dev_err(dev
, "failed to register input device: %d\n", error
);
316 static const struct nvt_ts_i2c_chip_data nvt_nt11205_ts_data
= {
321 static const struct nvt_ts_i2c_chip_data nvt_nt36672a_ts_data
= {
326 static const struct of_device_id nvt_ts_of_match
[] = {
327 { .compatible
= "novatek,nt11205-ts", .data
= &nvt_nt11205_ts_data
},
328 { .compatible
= "novatek,nt36672a-ts", .data
= &nvt_nt36672a_ts_data
},
331 MODULE_DEVICE_TABLE(of
, nvt_ts_of_match
);
333 static const struct i2c_device_id nvt_ts_i2c_id
[] = {
334 { "nt11205-ts", (unsigned long) &nvt_nt11205_ts_data
},
335 { "nt36672a-ts", (unsigned long) &nvt_nt36672a_ts_data
},
338 MODULE_DEVICE_TABLE(i2c
, nvt_ts_i2c_id
);
340 static struct i2c_driver nvt_ts_driver
= {
342 .name
= "novatek-nvt-ts",
343 .pm
= pm_sleep_ptr(&nvt_ts_pm_ops
),
344 .of_match_table
= nvt_ts_of_match
,
346 .probe
= nvt_ts_probe
,
347 .id_table
= nvt_ts_i2c_id
,
350 module_i2c_driver(nvt_ts_driver
);
352 MODULE_DESCRIPTION("Novatek NT11205 touchscreen driver");
353 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
354 MODULE_LICENSE("GPL");