1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * ST1232 Touchscreen Controller Driver 4 * 5 * Copyright (C) 2010 Renesas Solutions Corp. 6 * Tony SIM <chinyeow.sim.xt@renesas.com> 7 * 8 * Using code from: 9 * - android.git.kernel.org: projects/kernel/common.git: synaptics_i2c_rmi.c 10 * Copyright (C) 2007 Google, Inc. 11 */ 12 13 #include <linux/delay.h> 14 #include <linux/gpio/consumer.h> 15 #include <linux/i2c.h> 16 #include <linux/input.h> 17 #include <linux/interrupt.h> 18 #include <linux/module.h> 19 #include <linux/of.h> 20 #include <linux/pm_qos.h> 21 #include <linux/slab.h> 22 #include <linux/types.h> 23 24 #define ST1232_TS_NAME "st1232-ts" 25 #define ST1633_TS_NAME "st1633-ts" 26 27 struct st1232_ts_finger { 28 u16 x; 29 u16 y; 30 u8 t; 31 bool is_valid; 32 }; 33 34 struct st_chip_info { 35 bool have_z; 36 u16 max_x; 37 u16 max_y; 38 u16 max_area; 39 u16 max_fingers; 40 u8 start_reg; 41 }; 42 43 struct st1232_ts_data { 44 struct i2c_client *client; 45 struct input_dev *input_dev; 46 struct dev_pm_qos_request low_latency_req; 47 struct gpio_desc *reset_gpio; 48 const struct st_chip_info *chip_info; 49 int read_buf_len; 50 u8 *read_buf; 51 struct st1232_ts_finger *finger; 52 }; 53 54 static int st1232_ts_read_data(struct st1232_ts_data *ts) 55 { 56 struct st1232_ts_finger *finger = ts->finger; 57 struct i2c_client *client = ts->client; 58 struct i2c_msg msg[2]; 59 int error; 60 int i, y; 61 u8 start_reg = ts->chip_info->start_reg; 62 u8 *buf = ts->read_buf; 63 64 /* read touchscreen data */ 65 msg[0].addr = client->addr; 66 msg[0].flags = 0; 67 msg[0].len = 1; 68 msg[0].buf = &start_reg; 69 70 msg[1].addr = ts->client->addr; 71 msg[1].flags = I2C_M_RD; 72 msg[1].len = ts->read_buf_len; 73 msg[1].buf = buf; 74 75 error = i2c_transfer(client->adapter, msg, 2); 76 if (error < 0) 77 return error; 78 79 for (i = 0, y = 0; i < ts->chip_info->max_fingers; i++, y += 3) { 80 finger[i].is_valid = buf[i + y] >> 7; 81 if (finger[i].is_valid) { 82 finger[i].x = ((buf[i + y] & 0x0070) << 4) | buf[i + 1]; 83 finger[i].y = ((buf[i + y] & 0x0007) << 8) | buf[i + 2]; 84 85 /* st1232 includes a z-axis / touch strength */ 86 if (ts->chip_info->have_z) 87 finger[i].t = buf[i + 6]; 88 } 89 } 90 91 return 0; 92 } 93 94 static irqreturn_t st1232_ts_irq_handler(int irq, void *dev_id) 95 { 96 struct st1232_ts_data *ts = dev_id; 97 struct st1232_ts_finger *finger = ts->finger; 98 struct input_dev *input_dev = ts->input_dev; 99 int count = 0; 100 int i, ret; 101 102 ret = st1232_ts_read_data(ts); 103 if (ret < 0) 104 goto end; 105 106 /* multi touch protocol */ 107 for (i = 0; i < ts->chip_info->max_fingers; i++) { 108 if (!finger[i].is_valid) 109 continue; 110 111 if (ts->chip_info->have_z) 112 input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, 113 finger[i].t); 114 115 input_report_abs(input_dev, ABS_MT_POSITION_X, finger[i].x); 116 input_report_abs(input_dev, ABS_MT_POSITION_Y, finger[i].y); 117 input_mt_sync(input_dev); 118 count++; 119 } 120 121 /* SYN_MT_REPORT only if no contact */ 122 if (!count) { 123 input_mt_sync(input_dev); 124 if (ts->low_latency_req.dev) { 125 dev_pm_qos_remove_request(&ts->low_latency_req); 126 ts->low_latency_req.dev = NULL; 127 } 128 } else if (!ts->low_latency_req.dev) { 129 /* First contact, request 100 us latency. */ 130 dev_pm_qos_add_ancestor_request(&ts->client->dev, 131 &ts->low_latency_req, 132 DEV_PM_QOS_RESUME_LATENCY, 100); 133 } 134 135 /* SYN_REPORT */ 136 input_sync(input_dev); 137 138 end: 139 return IRQ_HANDLED; 140 } 141 142 static void st1232_ts_power(struct st1232_ts_data *ts, bool poweron) 143 { 144 if (ts->reset_gpio) 145 gpiod_set_value_cansleep(ts->reset_gpio, !poweron); 146 } 147 148 static const struct st_chip_info st1232_chip_info = { 149 .have_z = true, 150 .max_x = 0x31f, /* 800 - 1 */ 151 .max_y = 0x1df, /* 480 -1 */ 152 .max_area = 0xff, 153 .max_fingers = 2, 154 .start_reg = 0x12, 155 }; 156 157 static const struct st_chip_info st1633_chip_info = { 158 .have_z = false, 159 .max_x = 0x13f, /* 320 - 1 */ 160 .max_y = 0x1df, /* 480 -1 */ 161 .max_area = 0x00, 162 .max_fingers = 5, 163 .start_reg = 0x12, 164 }; 165 166 static int st1232_ts_probe(struct i2c_client *client, 167 const struct i2c_device_id *id) 168 { 169 const struct st_chip_info *match; 170 struct st1232_ts_data *ts; 171 struct st1232_ts_finger *finger; 172 struct input_dev *input_dev; 173 int error; 174 175 match = device_get_match_data(&client->dev); 176 if (!match && id) 177 match = (const void *)id->driver_data; 178 if (!match) { 179 dev_err(&client->dev, "unknown device model\n"); 180 return -ENODEV; 181 } 182 183 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 184 dev_err(&client->dev, "need I2C_FUNC_I2C\n"); 185 return -EIO; 186 } 187 188 if (!client->irq) { 189 dev_err(&client->dev, "no IRQ?\n"); 190 return -EINVAL; 191 } 192 193 ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL); 194 if (!ts) 195 return -ENOMEM; 196 197 ts->chip_info = match; 198 ts->finger = devm_kcalloc(&client->dev, 199 ts->chip_info->max_fingers, sizeof(*finger), 200 GFP_KERNEL); 201 if (!ts->finger) 202 return -ENOMEM; 203 204 /* allocate a buffer according to the number of registers to read */ 205 ts->read_buf_len = ts->chip_info->max_fingers * 4; 206 ts->read_buf = devm_kzalloc(&client->dev, ts->read_buf_len, GFP_KERNEL); 207 if (!ts->read_buf) 208 return -ENOMEM; 209 210 input_dev = devm_input_allocate_device(&client->dev); 211 if (!input_dev) 212 return -ENOMEM; 213 214 ts->client = client; 215 ts->input_dev = input_dev; 216 217 ts->reset_gpio = devm_gpiod_get_optional(&client->dev, NULL, 218 GPIOD_OUT_HIGH); 219 if (IS_ERR(ts->reset_gpio)) { 220 error = PTR_ERR(ts->reset_gpio); 221 dev_err(&client->dev, "Unable to request GPIO pin: %d.\n", 222 error); 223 return error; 224 } 225 226 st1232_ts_power(ts, true); 227 228 input_dev->name = "st1232-touchscreen"; 229 input_dev->id.bustype = BUS_I2C; 230 input_dev->dev.parent = &client->dev; 231 232 __set_bit(INPUT_PROP_DIRECT, input_dev->propbit); 233 __set_bit(EV_SYN, input_dev->evbit); 234 __set_bit(EV_KEY, input_dev->evbit); 235 __set_bit(EV_ABS, input_dev->evbit); 236 237 if (ts->chip_info->have_z) 238 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0, 239 ts->chip_info->max_area, 0, 0); 240 241 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 242 0, ts->chip_info->max_x, 0, 0); 243 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 244 0, ts->chip_info->max_y, 0, 0); 245 246 error = devm_request_threaded_irq(&client->dev, client->irq, 247 NULL, st1232_ts_irq_handler, 248 IRQF_ONESHOT, 249 client->name, ts); 250 if (error) { 251 dev_err(&client->dev, "Failed to register interrupt\n"); 252 return error; 253 } 254 255 error = input_register_device(ts->input_dev); 256 if (error) { 257 dev_err(&client->dev, "Unable to register %s input device\n", 258 input_dev->name); 259 return error; 260 } 261 262 i2c_set_clientdata(client, ts); 263 device_init_wakeup(&client->dev, 1); 264 265 return 0; 266 } 267 268 static int st1232_ts_remove(struct i2c_client *client) 269 { 270 struct st1232_ts_data *ts = i2c_get_clientdata(client); 271 272 st1232_ts_power(ts, false); 273 274 return 0; 275 } 276 277 static int __maybe_unused st1232_ts_suspend(struct device *dev) 278 { 279 struct i2c_client *client = to_i2c_client(dev); 280 struct st1232_ts_data *ts = i2c_get_clientdata(client); 281 282 if (device_may_wakeup(&client->dev)) { 283 enable_irq_wake(client->irq); 284 } else { 285 disable_irq(client->irq); 286 st1232_ts_power(ts, false); 287 } 288 289 return 0; 290 } 291 292 static int __maybe_unused st1232_ts_resume(struct device *dev) 293 { 294 struct i2c_client *client = to_i2c_client(dev); 295 struct st1232_ts_data *ts = i2c_get_clientdata(client); 296 297 if (device_may_wakeup(&client->dev)) { 298 disable_irq_wake(client->irq); 299 } else { 300 st1232_ts_power(ts, true); 301 enable_irq(client->irq); 302 } 303 304 return 0; 305 } 306 307 static SIMPLE_DEV_PM_OPS(st1232_ts_pm_ops, 308 st1232_ts_suspend, st1232_ts_resume); 309 310 static const struct i2c_device_id st1232_ts_id[] = { 311 { ST1232_TS_NAME, (unsigned long)&st1232_chip_info }, 312 { ST1633_TS_NAME, (unsigned long)&st1633_chip_info }, 313 { } 314 }; 315 MODULE_DEVICE_TABLE(i2c, st1232_ts_id); 316 317 static const struct of_device_id st1232_ts_dt_ids[] = { 318 { .compatible = "sitronix,st1232", .data = &st1232_chip_info }, 319 { .compatible = "sitronix,st1633", .data = &st1633_chip_info }, 320 { } 321 }; 322 MODULE_DEVICE_TABLE(of, st1232_ts_dt_ids); 323 324 static struct i2c_driver st1232_ts_driver = { 325 .probe = st1232_ts_probe, 326 .remove = st1232_ts_remove, 327 .id_table = st1232_ts_id, 328 .driver = { 329 .name = ST1232_TS_NAME, 330 .of_match_table = st1232_ts_dt_ids, 331 .pm = &st1232_ts_pm_ops, 332 }, 333 }; 334 335 module_i2c_driver(st1232_ts_driver); 336 337 MODULE_AUTHOR("Tony SIM <chinyeow.sim.xt@renesas.com>"); 338 MODULE_AUTHOR("Martin Kepplinger <martin.kepplinger@ginzinger.com>"); 339 MODULE_DESCRIPTION("SITRONIX ST1232 Touchscreen Controller Driver"); 340 MODULE_LICENSE("GPL v2"); 341