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_WAKE_TYPE 0x0c 31 #define NVT_TS_PARAMS_CHIP_ID 0x0e 32 #define NVT_TS_PARAMS_SIZE 0x0f 33 34 #define NVT_TS_MAX_TOUCHES 10 35 #define NVT_TS_MAX_SIZE 4096 36 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 43 44 static const int nvt_ts_irq_type[4] = { 45 IRQF_TRIGGER_RISING, 46 IRQF_TRIGGER_FALLING, 47 IRQF_TRIGGER_LOW, 48 IRQF_TRIGGER_HIGH 49 }; 50 51 struct nvt_ts_i2c_chip_data { 52 u8 wake_type; 53 u8 chip_id; 54 }; 55 56 struct nvt_ts_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; 62 int max_touches; 63 u8 buf[NVT_TS_TOUCH_SIZE * NVT_TS_MAX_TOUCHES]; 64 }; 65 66 static int nvt_ts_read_data(struct i2c_client *client, u8 reg, u8 *data, int count) 67 { 68 struct i2c_msg msg[2] = { 69 { 70 .addr = client->addr, 71 .len = 1, 72 .buf = ®, 73 }, 74 { 75 .addr = client->addr, 76 .flags = I2C_M_RD, 77 .len = count, 78 .buf = data, 79 } 80 }; 81 int ret; 82 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; 87 } 88 89 return 0; 90 } 91 92 static irqreturn_t nvt_ts_irq(int irq, void *dev_id) 93 { 94 struct nvt_ts_data *data = dev_id; 95 struct device *dev = &data->client->dev; 96 int i, error, slot, x, y; 97 bool active; 98 u8 *touch; 99 100 error = nvt_ts_read_data(data->client, NVT_TS_TOUCH_START, data->buf, 101 data->max_touches * NVT_TS_TOUCH_SIZE); 102 if (error) 103 return IRQ_HANDLED; 104 105 for (i = 0; i < data->max_touches; i++) { 106 touch = &data->buf[i * NVT_TS_TOUCH_SIZE]; 107 108 if (touch[0] == NVT_TS_TOUCH_INVALID) 109 continue; 110 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); 114 continue; 115 } 116 117 switch (touch[0] & NVT_TS_TOUCH_TYPE_MASK) { 118 case NVT_TS_TOUCH_NEW: 119 case NVT_TS_TOUCH_UPDATE: 120 active = true; 121 break; 122 case NVT_TS_TOUCH_RELEASE: 123 active = false; 124 break; 125 default: 126 dev_warn(dev, "slot %d unknown state %d\n", slot, touch[0] & 7); 127 continue; 128 } 129 130 slot--; 131 x = (touch[1] << 4) | (touch[3] >> 4); 132 y = (touch[2] << 4) | (touch[3] & 0x0f); 133 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); 137 } 138 139 input_mt_sync_frame(data->input); 140 input_sync(data->input); 141 142 return IRQ_HANDLED; 143 } 144 145 static int nvt_ts_start(struct input_dev *dev) 146 { 147 struct nvt_ts_data *data = input_get_drvdata(dev); 148 int error; 149 150 error = regulator_bulk_enable(ARRAY_SIZE(data->regulators), data->regulators); 151 if (error) { 152 dev_err(&data->client->dev, "failed to enable regulators\n"); 153 return error; 154 } 155 156 enable_irq(data->client->irq); 157 gpiod_set_value_cansleep(data->reset_gpio, 0); 158 159 return 0; 160 } 161 162 static void nvt_ts_stop(struct input_dev *dev) 163 { 164 struct nvt_ts_data *data = input_get_drvdata(dev); 165 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); 169 } 170 171 static int nvt_ts_suspend(struct device *dev) 172 { 173 struct nvt_ts_data *data = i2c_get_clientdata(to_i2c_client(dev)); 174 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); 179 180 return 0; 181 } 182 183 static int nvt_ts_resume(struct device *dev) 184 { 185 struct nvt_ts_data *data = i2c_get_clientdata(to_i2c_client(dev)); 186 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); 191 192 return 0; 193 } 194 195 static DEFINE_SIMPLE_DEV_PM_OPS(nvt_ts_pm_ops, nvt_ts_suspend, nvt_ts_resume); 196 197 static int nvt_ts_probe(struct i2c_client *client) 198 { 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; 204 205 if (!client->irq) { 206 dev_err(dev, "Error no irq specified\n"); 207 return -EINVAL; 208 } 209 210 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 211 if (!data) 212 return -ENOMEM; 213 214 chip = device_get_match_data(&client->dev); 215 if (!chip) 216 return -EINVAL; 217 218 data->client = client; 219 i2c_set_clientdata(client, data); 220 221 /* 222 * VCC is the analog voltage supply 223 * IOVCC is the digital voltage supply 224 */ 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); 228 if (error) { 229 dev_err(dev, "cannot get regulators: %d\n", error); 230 return error; 231 } 232 233 error = regulator_bulk_enable(ARRAY_SIZE(data->regulators), data->regulators); 234 if (error) { 235 dev_err(dev, "failed to enable regulators: %d\n", error); 236 return error; 237 } 238 239 data->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW); 240 error = PTR_ERR_OR_ZERO(data->reset_gpio); 241 if (error) { 242 regulator_bulk_disable(ARRAY_SIZE(data->regulators), data->regulators); 243 dev_err(dev, "failed to request reset GPIO: %d\n", error); 244 return error; 245 } 246 247 /* Wait for controller to come out of reset before params read */ 248 msleep(100); 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); 253 if (error) 254 return error; 255 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]; 260 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); 268 return -EIO; 269 } 270 271 dev_dbg(dev, "Detected %dx%d touchscreen with %d max touches\n", 272 width, height, data->max_touches); 273 274 if (data->buf[NVT_TS_PARAMS_MAX_BUTTONS]) 275 dev_warn(dev, "Touchscreen buttons are not supported\n"); 276 277 input = devm_input_allocate_device(dev); 278 if (!input) 279 return -ENOMEM; 280 281 input->name = client->name; 282 input->id.bustype = BUS_I2C; 283 input->open = nvt_ts_start; 284 input->close = nvt_ts_stop; 285 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); 289 290 error = input_mt_init_slots(input, data->max_touches, 291 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); 292 if (error) 293 return error; 294 295 data->input = input; 296 input_set_drvdata(input, data); 297 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], 301 client->name, data); 302 if (error) { 303 dev_err(dev, "failed to request irq: %d\n", error); 304 return error; 305 } 306 307 error = input_register_device(input); 308 if (error) { 309 dev_err(dev, "failed to register input device: %d\n", error); 310 return error; 311 } 312 313 return 0; 314 } 315 316 static const struct nvt_ts_i2c_chip_data nvt_nt11205_ts_data = { 317 .wake_type = 0x05, 318 .chip_id = 0x05, 319 }; 320 321 static const struct nvt_ts_i2c_chip_data nvt_nt36672a_ts_data = { 322 .wake_type = 0x01, 323 .chip_id = 0x08, 324 }; 325 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 }, 329 { } 330 }; 331 MODULE_DEVICE_TABLE(of, nvt_ts_of_match); 332 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 }, 336 { } 337 }; 338 MODULE_DEVICE_TABLE(i2c, nvt_ts_i2c_id); 339 340 static struct i2c_driver nvt_ts_driver = { 341 .driver = { 342 .name = "novatek-nvt-ts", 343 .pm = pm_sleep_ptr(&nvt_ts_pm_ops), 344 .of_match_table = nvt_ts_of_match, 345 }, 346 .probe = nvt_ts_probe, 347 .id_table = nvt_ts_i2c_id, 348 }; 349 350 module_i2c_driver(nvt_ts_driver); 351 352 MODULE_DESCRIPTION("Novatek NT11205 touchscreen driver"); 353 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>"); 354 MODULE_LICENSE("GPL"); 355