1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Driver for Novatek NT11205 i2c touchscreen controller as found 4 * on the Acer Iconia One 7 B1-750 tablet. 5 * 6 * Copyright (c) 2023 Hans de Goede <hdegoede@redhat.com> 7 */ 8 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> 17 18 #include <linux/unaligned.h> 19 20 #define NVT_TS_TOUCH_START 0x00 21 #define NVT_TS_TOUCH_SIZE 6 22 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_CHIP_ID 0x0e 31 #define NVT_TS_PARAMS_SIZE 0x0f 32 33 #define NVT_TS_MAX_TOUCHES 10 34 #define NVT_TS_MAX_SIZE 4096 35 36 #define NVT_TS_TOUCH_INVALID 0xff 37 #define NVT_TS_TOUCH_SLOT_SHIFT 3 38 #define NVT_TS_TOUCH_TYPE_MASK GENMASK(2, 0) 39 #define NVT_TS_TOUCH_NEW 1 40 #define NVT_TS_TOUCH_UPDATE 2 41 #define NVT_TS_TOUCH_RELEASE 3 42 43 static const int nvt_ts_irq_type[4] = { 44 IRQF_TRIGGER_RISING, 45 IRQF_TRIGGER_FALLING, 46 IRQF_TRIGGER_LOW, 47 IRQF_TRIGGER_HIGH 48 }; 49 50 struct nvt_ts_i2c_chip_data { 51 u8 chip_id; 52 }; 53 54 struct nvt_ts_data { 55 struct i2c_client *client; 56 struct input_dev *input; 57 struct gpio_desc *reset_gpio; 58 struct regulator_bulk_data regulators[2]; 59 struct touchscreen_properties prop; 60 int max_touches; 61 u8 buf[NVT_TS_TOUCH_SIZE * NVT_TS_MAX_TOUCHES]; 62 }; 63 64 static int nvt_ts_read_data(struct i2c_client *client, u8 reg, u8 *data, int count) 65 { 66 struct i2c_msg msg[2] = { 67 { 68 .addr = client->addr, 69 .len = 1, 70 .buf = ®, 71 }, 72 { 73 .addr = client->addr, 74 .flags = I2C_M_RD, 75 .len = count, 76 .buf = data, 77 } 78 }; 79 int ret; 80 81 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); 82 if (ret != ARRAY_SIZE(msg)) { 83 dev_err(&client->dev, "Error reading from 0x%02x: %d\n", reg, ret); 84 return (ret < 0) ? ret : -EIO; 85 } 86 87 return 0; 88 } 89 90 static irqreturn_t nvt_ts_irq(int irq, void *dev_id) 91 { 92 struct nvt_ts_data *data = dev_id; 93 struct device *dev = &data->client->dev; 94 int i, error, slot, x, y; 95 bool active; 96 u8 *touch; 97 98 error = nvt_ts_read_data(data->client, NVT_TS_TOUCH_START, data->buf, 99 data->max_touches * NVT_TS_TOUCH_SIZE); 100 if (error) 101 return IRQ_HANDLED; 102 103 for (i = 0; i < data->max_touches; i++) { 104 touch = &data->buf[i * NVT_TS_TOUCH_SIZE]; 105 106 if (touch[0] == NVT_TS_TOUCH_INVALID) 107 continue; 108 109 slot = touch[0] >> NVT_TS_TOUCH_SLOT_SHIFT; 110 if (slot < 1 || slot > data->max_touches) { 111 dev_warn(dev, "slot %d out of range, ignoring\n", slot); 112 continue; 113 } 114 115 switch (touch[0] & NVT_TS_TOUCH_TYPE_MASK) { 116 case NVT_TS_TOUCH_NEW: 117 case NVT_TS_TOUCH_UPDATE: 118 active = true; 119 break; 120 case NVT_TS_TOUCH_RELEASE: 121 active = false; 122 break; 123 default: 124 dev_warn(dev, "slot %d unknown state %d\n", slot, touch[0] & 7); 125 continue; 126 } 127 128 slot--; 129 x = (touch[1] << 4) | (touch[3] >> 4); 130 y = (touch[2] << 4) | (touch[3] & 0x0f); 131 132 input_mt_slot(data->input, slot); 133 input_mt_report_slot_state(data->input, MT_TOOL_FINGER, active); 134 touchscreen_report_pos(data->input, &data->prop, x, y, true); 135 } 136 137 input_mt_sync_frame(data->input); 138 input_sync(data->input); 139 140 return IRQ_HANDLED; 141 } 142 143 static int nvt_ts_start(struct input_dev *dev) 144 { 145 struct nvt_ts_data *data = input_get_drvdata(dev); 146 int error; 147 148 error = regulator_bulk_enable(ARRAY_SIZE(data->regulators), data->regulators); 149 if (error) { 150 dev_err(&data->client->dev, "failed to enable regulators\n"); 151 return error; 152 } 153 154 enable_irq(data->client->irq); 155 gpiod_set_value_cansleep(data->reset_gpio, 0); 156 157 return 0; 158 } 159 160 static void nvt_ts_stop(struct input_dev *dev) 161 { 162 struct nvt_ts_data *data = input_get_drvdata(dev); 163 164 disable_irq(data->client->irq); 165 gpiod_set_value_cansleep(data->reset_gpio, 1); 166 regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators); 167 } 168 169 static int nvt_ts_suspend(struct device *dev) 170 { 171 struct nvt_ts_data *data = i2c_get_clientdata(to_i2c_client(dev)); 172 173 mutex_lock(&data->input->mutex); 174 if (input_device_enabled(data->input)) 175 nvt_ts_stop(data->input); 176 mutex_unlock(&data->input->mutex); 177 178 return 0; 179 } 180 181 static int nvt_ts_resume(struct device *dev) 182 { 183 struct nvt_ts_data *data = i2c_get_clientdata(to_i2c_client(dev)); 184 185 mutex_lock(&data->input->mutex); 186 if (input_device_enabled(data->input)) 187 nvt_ts_start(data->input); 188 mutex_unlock(&data->input->mutex); 189 190 return 0; 191 } 192 193 static DEFINE_SIMPLE_DEV_PM_OPS(nvt_ts_pm_ops, nvt_ts_suspend, nvt_ts_resume); 194 195 static int nvt_ts_probe(struct i2c_client *client) 196 { 197 struct device *dev = &client->dev; 198 int error, width, height, irq_type; 199 struct nvt_ts_data *data; 200 const struct nvt_ts_i2c_chip_data *chip; 201 struct input_dev *input; 202 203 if (!client->irq) { 204 dev_err(dev, "Error no irq specified\n"); 205 return -EINVAL; 206 } 207 208 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 209 if (!data) 210 return -ENOMEM; 211 212 chip = device_get_match_data(&client->dev); 213 if (!chip) 214 return -EINVAL; 215 216 data->client = client; 217 i2c_set_clientdata(client, data); 218 219 /* 220 * VCC is the analog voltage supply 221 * IOVCC is the digital voltage supply 222 */ 223 data->regulators[0].supply = "vcc"; 224 data->regulators[1].supply = "iovcc"; 225 error = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->regulators), data->regulators); 226 if (error) { 227 dev_err(dev, "cannot get regulators: %d\n", error); 228 return error; 229 } 230 231 error = regulator_bulk_enable(ARRAY_SIZE(data->regulators), data->regulators); 232 if (error) { 233 dev_err(dev, "failed to enable regulators: %d\n", error); 234 return error; 235 } 236 237 data->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW); 238 error = PTR_ERR_OR_ZERO(data->reset_gpio); 239 if (error) { 240 regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators); 241 dev_err(dev, "failed to request reset GPIO: %d\n", error); 242 return error; 243 } 244 245 /* Wait for controller to come out of reset before params read */ 246 msleep(100); 247 error = nvt_ts_read_data(data->client, NVT_TS_PARAMETERS_START, 248 data->buf, NVT_TS_PARAMS_SIZE); 249 gpiod_set_value_cansleep(data->reset_gpio, 1); /* Put back in reset */ 250 regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators); 251 if (error) 252 return error; 253 254 width = get_unaligned_be16(&data->buf[NVT_TS_PARAMS_WIDTH]); 255 height = get_unaligned_be16(&data->buf[NVT_TS_PARAMS_HEIGHT]); 256 data->max_touches = data->buf[NVT_TS_PARAMS_MAX_TOUCH]; 257 irq_type = data->buf[NVT_TS_PARAMS_IRQ_TYPE]; 258 259 if (width > NVT_TS_MAX_SIZE || height >= NVT_TS_MAX_SIZE || 260 data->max_touches > NVT_TS_MAX_TOUCHES || 261 irq_type >= ARRAY_SIZE(nvt_ts_irq_type) || 262 data->buf[NVT_TS_PARAMS_CHIP_ID] != chip->chip_id) { 263 dev_err(dev, "Unsupported touchscreen parameters: %*ph\n", 264 NVT_TS_PARAMS_SIZE, data->buf); 265 return -EIO; 266 } 267 268 dev_dbg(dev, "Detected %dx%d touchscreen with %d max touches\n", 269 width, height, data->max_touches); 270 271 if (data->buf[NVT_TS_PARAMS_MAX_BUTTONS]) 272 dev_warn(dev, "Touchscreen buttons are not supported\n"); 273 274 input = devm_input_allocate_device(dev); 275 if (!input) 276 return -ENOMEM; 277 278 input->name = client->name; 279 input->id.bustype = BUS_I2C; 280 input->open = nvt_ts_start; 281 input->close = nvt_ts_stop; 282 283 input_set_abs_params(input, ABS_MT_POSITION_X, 0, width - 1, 0, 0); 284 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, height - 1, 0, 0); 285 touchscreen_parse_properties(input, true, &data->prop); 286 287 error = input_mt_init_slots(input, data->max_touches, 288 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); 289 if (error) 290 return error; 291 292 data->input = input; 293 input_set_drvdata(input, data); 294 295 error = devm_request_threaded_irq(dev, client->irq, NULL, nvt_ts_irq, 296 IRQF_ONESHOT | IRQF_NO_AUTOEN | 297 nvt_ts_irq_type[irq_type], 298 client->name, data); 299 if (error) { 300 dev_err(dev, "failed to request irq: %d\n", error); 301 return error; 302 } 303 304 error = input_register_device(input); 305 if (error) { 306 dev_err(dev, "failed to register input device: %d\n", error); 307 return error; 308 } 309 310 return 0; 311 } 312 313 static const struct nvt_ts_i2c_chip_data nvt_nt11205_ts_data = { 314 .chip_id = 0x05, 315 }; 316 317 static const struct nvt_ts_i2c_chip_data nvt_nt36672a_ts_data = { 318 .chip_id = 0x08, 319 }; 320 321 static const struct of_device_id nvt_ts_of_match[] = { 322 { .compatible = "novatek,nt11205-ts", .data = &nvt_nt11205_ts_data }, 323 { .compatible = "novatek,nt36672a-ts", .data = &nvt_nt36672a_ts_data }, 324 { } 325 }; 326 MODULE_DEVICE_TABLE(of, nvt_ts_of_match); 327 328 static const struct i2c_device_id nvt_ts_i2c_id[] = { 329 { "nt11205-ts", (unsigned long) &nvt_nt11205_ts_data }, 330 { "nt36672a-ts", (unsigned long) &nvt_nt36672a_ts_data }, 331 { } 332 }; 333 MODULE_DEVICE_TABLE(i2c, nvt_ts_i2c_id); 334 335 static struct i2c_driver nvt_ts_driver = { 336 .driver = { 337 .name = "novatek-nvt-ts", 338 .pm = pm_sleep_ptr(&nvt_ts_pm_ops), 339 .of_match_table = nvt_ts_of_match, 340 }, 341 .probe = nvt_ts_probe, 342 .id_table = nvt_ts_i2c_id, 343 }; 344 345 module_i2c_driver(nvt_ts_driver); 346 347 MODULE_DESCRIPTION("Novatek NT11205 touchscreen driver"); 348 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>"); 349 MODULE_LICENSE("GPL"); 350