| 1 | |
| 2 | |
| 3 | * ST1232 Touchscreen Controller Driver
|
| 4 | *
|
| 5 | * Copyright (C) 2010 Renesas Solutions Corp.
|
| 6 | * Tony SIM <[email protected]>
|
| 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/input/mt.h>
|
| 18 | #include <linux/input/touch-overlay.h>
|
| 19 | #include <linux/input/touchscreen.h>
|
| 20 | #include <linux/interrupt.h>
|
| 21 | #include <linux/module.h>
|
| 22 | #include <linux/of.h>
|
| 23 | #include <linux/pm_qos.h>
|
| 24 | #include <linux/slab.h>
|
| 25 | #include <linux/types.h>
|
| 26 | #include <asm/byteorder.h>
|
| 27 |
|
| 28 | #define ST1232_TS_NAME "st1232-ts"
|
| 29 | #define ST1633_TS_NAME "st1633-ts"
|
| 30 |
|
| 31 | #define REG_FIRMWARE_VERSION 0x00
|
| 32 | #define REG_FIRMWARE_REVISION_3 0x0C
|
| 33 |
|
| 34 | #define REG_STATUS 0x01
|
| 35 |
|
| 36 | #define STATUS_NORMAL 0x00
|
| 37 | #define STATUS_INIT 0x01
|
| 38 | #define STATUS_ERROR 0x02
|
| 39 | #define STATUS_AUTO_TUNING 0x03
|
| 40 | #define STATUS_IDLE 0x04
|
| 41 | #define STATUS_POWER_DOWN 0x05
|
| 42 |
|
| 43 | #define ERROR_NONE 0x00
|
| 44 | #define ERROR_INVALID_ADDRESS 0x10
|
| 45 | #define ERROR_INVALID_VALUE 0x20
|
| 46 | #define ERROR_INVALID_PLATFORM 0x30
|
| 47 |
|
| 48 | #define REG_XY_RESOLUTION 0x04
|
| 49 | #define REG_XY_COORDINATES 0x12
|
| 50 | #define ST_TS_MAX_FINGERS 10
|
| 51 |
|
| 52 | struct st_chip_info {
|
| 53 | bool have_z;
|
| 54 | u16 max_area;
|
| 55 | u16 max_fingers;
|
| 56 | };
|
| 57 |
|
| 58 | struct st1232_ts_data {
|
| 59 | struct i2c_client *client;
|
| 60 | struct input_dev *input_dev;
|
| 61 | struct touchscreen_properties prop;
|
| 62 | struct dev_pm_qos_request low_latency_req;
|
| 63 | struct gpio_desc *reset_gpio;
|
| 64 | const struct st_chip_info *chip_info;
|
| 65 | struct list_head touch_overlay_list;
|
| 66 | int read_buf_len;
|
| 67 | u8 *read_buf;
|
| 68 | u8 fw_version;
|
| 69 | u32 fw_revision;
|
| 70 | };
|
| 71 |
|
| 72 | static ssize_t fw_version_show(struct device *dev,
|
| 73 | struct device_attribute *attr, char *buf)
|
| 74 | {
|
| 75 | struct i2c_client *client = to_i2c_client(dev);
|
| 76 | struct st1232_ts_data *st1232_ts = i2c_get_clientdata(client);
|
| 77 |
|
| 78 | return sysfs_emit(buf, "%u\n", st1232_ts->fw_version);
|
| 79 | }
|
| 80 |
|
| 81 | static ssize_t fw_revision_show(struct device *dev,
|
| 82 | struct device_attribute *attr, char *buf)
|
| 83 | {
|
| 84 | struct i2c_client *client = to_i2c_client(dev);
|
| 85 | struct st1232_ts_data *st1232_ts = i2c_get_clientdata(client);
|
| 86 |
|
| 87 | return sysfs_emit(buf, "%08x\n", st1232_ts->fw_revision);
|
| 88 | }
|
| 89 |
|
| 90 | static DEVICE_ATTR_RO(fw_version);
|
| 91 | static DEVICE_ATTR_RO(fw_revision);
|
| 92 |
|
| 93 | static struct attribute *st1232_attrs[] = {
|
| 94 | &dev_attr_fw_version.attr,
|
| 95 | &dev_attr_fw_revision.attr,
|
| 96 | NULL,
|
| 97 | };
|
| 98 | ATTRIBUTE_GROUPS(st1232);
|
| 99 |
|
| 100 | static int st1232_ts_read_data(struct st1232_ts_data *ts, u8 reg,
|
| 101 | unsigned int n)
|
| 102 | {
|
| 103 | struct i2c_client *client = ts->client;
|
| 104 | struct i2c_msg msg[] = {
|
| 105 | {
|
| 106 | .addr = client->addr,
|
| 107 | .len = sizeof(reg),
|
| 108 | .buf = ®,
|
| 109 | },
|
| 110 | {
|
| 111 | .addr = client->addr,
|
| 112 | .flags = I2C_M_RD | I2C_M_DMA_SAFE,
|
| 113 | .len = n,
|
| 114 | .buf = ts->read_buf,
|
| 115 | }
|
| 116 | };
|
| 117 | int ret;
|
| 118 |
|
| 119 | ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
|
| 120 | if (ret != ARRAY_SIZE(msg))
|
| 121 | return ret < 0 ? ret : -EIO;
|
| 122 |
|
| 123 | return 0;
|
| 124 | }
|
| 125 |
|
| 126 | static int st1232_ts_wait_ready(struct st1232_ts_data *ts)
|
| 127 | {
|
| 128 | unsigned int retries;
|
| 129 | int error;
|
| 130 |
|
| 131 | for (retries = 100; retries; retries--) {
|
| 132 | error = st1232_ts_read_data(ts, REG_STATUS, 1);
|
| 133 | if (!error) {
|
| 134 | switch (ts->read_buf[0]) {
|
| 135 | case STATUS_NORMAL | ERROR_NONE:
|
| 136 | case STATUS_IDLE | ERROR_NONE:
|
| 137 | return 0;
|
| 138 | }
|
| 139 | }
|
| 140 |
|
| 141 | usleep_range(1000, 2000);
|
| 142 | }
|
| 143 |
|
| 144 | return -ENXIO;
|
| 145 | }
|
| 146 |
|
| 147 | static int st1232_ts_read_fw_version(struct st1232_ts_data *ts,
|
| 148 | u8 *fw_version, u32 *fw_revision)
|
| 149 | {
|
| 150 | int error;
|
| 151 |
|
| 152 |
|
| 153 | error = st1232_ts_read_data(ts, REG_FIRMWARE_VERSION, 1);
|
| 154 | if (error)
|
| 155 | return error;
|
| 156 | *fw_version = ts->read_buf[0];
|
| 157 |
|
| 158 |
|
| 159 | error = st1232_ts_read_data(ts, REG_FIRMWARE_REVISION_3, 4);
|
| 160 | if (error)
|
| 161 | return error;
|
| 162 | *fw_revision = le32_to_cpup((__le32 *)ts->read_buf);
|
| 163 |
|
| 164 | return 0;
|
| 165 | }
|
| 166 |
|
| 167 | static int st1232_ts_read_resolution(struct st1232_ts_data *ts, u16 *max_x,
|
| 168 | u16 *max_y)
|
| 169 | {
|
| 170 | u8 *buf;
|
| 171 | int error;
|
| 172 |
|
| 173 |
|
| 174 | error = st1232_ts_read_data(ts, REG_XY_RESOLUTION, 3);
|
| 175 | if (error)
|
| 176 | return error;
|
| 177 |
|
| 178 | buf = ts->read_buf;
|
| 179 |
|
| 180 | *max_x = (((buf[0] & 0x0070) << 4) | buf[1]) - 1;
|
| 181 | *max_y = (((buf[0] & 0x0007) << 8) | buf[2]) - 1;
|
| 182 |
|
| 183 | return 0;
|
| 184 | }
|
| 185 |
|
| 186 | static int st1232_ts_parse_and_report(struct st1232_ts_data *ts)
|
| 187 | {
|
| 188 | struct input_dev *input = ts->input_dev;
|
| 189 | struct input_mt_pos pos[ST_TS_MAX_FINGERS];
|
| 190 | u8 z[ST_TS_MAX_FINGERS];
|
| 191 | int slots[ST_TS_MAX_FINGERS];
|
| 192 | int n_contacts = 0;
|
| 193 | int i;
|
| 194 |
|
| 195 | for (i = 0; i < ts->chip_info->max_fingers; i++) {
|
| 196 | u8 *buf = &ts->read_buf[i * 4];
|
| 197 |
|
| 198 | if (buf[0] & BIT(7)) {
|
| 199 | unsigned int x = ((buf[0] & 0x70) << 4) | buf[1];
|
| 200 | unsigned int y = ((buf[0] & 0x07) << 8) | buf[2];
|
| 201 |
|
| 202 | touchscreen_set_mt_pos(&pos[n_contacts],
|
| 203 | &ts->prop, x, y);
|
| 204 |
|
| 205 |
|
| 206 | if (ts->chip_info->have_z)
|
| 207 | z[n_contacts] = ts->read_buf[i + 6];
|
| 208 |
|
| 209 | n_contacts++;
|
| 210 | }
|
| 211 | }
|
| 212 |
|
| 213 | input_mt_assign_slots(input, slots, pos, n_contacts, 0);
|
| 214 | for (i = 0; i < n_contacts; i++) {
|
| 215 | if (touch_overlay_process_contact(&ts->touch_overlay_list,
|
| 216 | input, &pos[i], slots[i]))
|
| 217 | continue;
|
| 218 |
|
| 219 | input_mt_slot(input, slots[i]);
|
| 220 | input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
|
| 221 | input_report_abs(input, ABS_MT_POSITION_X, pos[i].x);
|
| 222 | input_report_abs(input, ABS_MT_POSITION_Y, pos[i].y);
|
| 223 | if (ts->chip_info->have_z)
|
| 224 | input_report_abs(input, ABS_MT_TOUCH_MAJOR, z[i]);
|
| 225 | }
|
| 226 |
|
| 227 | touch_overlay_sync_frame(&ts->touch_overlay_list, input);
|
| 228 | input_mt_sync_frame(input);
|
| 229 | input_sync(input);
|
| 230 |
|
| 231 | return n_contacts;
|
| 232 | }
|
| 233 |
|
| 234 | static irqreturn_t st1232_ts_irq_handler(int irq, void *dev_id)
|
| 235 | {
|
| 236 | struct st1232_ts_data *ts = dev_id;
|
| 237 | int count;
|
| 238 | int error;
|
| 239 |
|
| 240 | error = st1232_ts_read_data(ts, REG_XY_COORDINATES, ts->read_buf_len);
|
| 241 | if (error)
|
| 242 | goto out;
|
| 243 |
|
| 244 | count = st1232_ts_parse_and_report(ts);
|
| 245 | if (!count) {
|
| 246 | if (ts->low_latency_req.dev) {
|
| 247 | dev_pm_qos_remove_request(&ts->low_latency_req);
|
| 248 | ts->low_latency_req.dev = NULL;
|
| 249 | }
|
| 250 | } else if (!ts->low_latency_req.dev) {
|
| 251 |
|
| 252 | dev_pm_qos_add_ancestor_request(&ts->client->dev,
|
| 253 | &ts->low_latency_req,
|
| 254 | DEV_PM_QOS_RESUME_LATENCY, 100);
|
| 255 | }
|
| 256 |
|
| 257 | out:
|
| 258 | return IRQ_HANDLED;
|
| 259 | }
|
| 260 |
|
| 261 | static void st1232_ts_power(struct st1232_ts_data *ts, bool poweron)
|
| 262 | {
|
| 263 | if (ts->reset_gpio)
|
| 264 | gpiod_set_value_cansleep(ts->reset_gpio, !poweron);
|
| 265 | }
|
| 266 |
|
| 267 | static void st1232_ts_power_off(void *data)
|
| 268 | {
|
| 269 | st1232_ts_power(data, false);
|
| 270 | }
|
| 271 |
|
| 272 | static const struct st_chip_info st1232_chip_info = {
|
| 273 | .have_z = true,
|
| 274 | .max_area = 0xff,
|
| 275 | .max_fingers = 2,
|
| 276 | };
|
| 277 |
|
| 278 | static const struct st_chip_info st1633_chip_info = {
|
| 279 | .have_z = false,
|
| 280 | .max_area = 0x00,
|
| 281 | .max_fingers = 5,
|
| 282 | };
|
| 283 |
|
| 284 | static int st1232_ts_probe(struct i2c_client *client)
|
| 285 | {
|
| 286 | const struct i2c_device_id *id = i2c_client_get_device_id(client);
|
| 287 | const struct st_chip_info *match;
|
| 288 | struct st1232_ts_data *ts;
|
| 289 | struct input_dev *input_dev;
|
| 290 | u16 max_x, max_y;
|
| 291 | int error;
|
| 292 |
|
| 293 | match = device_get_match_data(&client->dev);
|
| 294 | if (!match && id)
|
| 295 | match = (const void *)id->driver_data;
|
| 296 | if (!match) {
|
| 297 | dev_err(&client->dev, "unknown device model\n");
|
| 298 | return -ENODEV;
|
| 299 | }
|
| 300 |
|
| 301 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
|
| 302 | dev_err(&client->dev, "need I2C_FUNC_I2C\n");
|
| 303 | return -EIO;
|
| 304 | }
|
| 305 |
|
| 306 | if (!client->irq) {
|
| 307 | dev_err(&client->dev, "no IRQ?\n");
|
| 308 | return -EINVAL;
|
| 309 | }
|
| 310 |
|
| 311 | ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
|
| 312 | if (!ts)
|
| 313 | return -ENOMEM;
|
| 314 |
|
| 315 | ts->chip_info = match;
|
| 316 |
|
| 317 |
|
| 318 | ts->read_buf_len = ts->chip_info->max_fingers * 4;
|
| 319 | ts->read_buf = devm_kzalloc(&client->dev, ts->read_buf_len, GFP_KERNEL);
|
| 320 | if (!ts->read_buf)
|
| 321 | return -ENOMEM;
|
| 322 |
|
| 323 | input_dev = devm_input_allocate_device(&client->dev);
|
| 324 | if (!input_dev)
|
| 325 | return -ENOMEM;
|
| 326 |
|
| 327 | ts->client = client;
|
| 328 | ts->input_dev = input_dev;
|
| 329 |
|
| 330 | ts->reset_gpio = devm_gpiod_get_optional(&client->dev, NULL,
|
| 331 | GPIOD_OUT_HIGH);
|
| 332 | if (IS_ERR(ts->reset_gpio)) {
|
| 333 | error = PTR_ERR(ts->reset_gpio);
|
| 334 | dev_err(&client->dev, "Unable to request GPIO pin: %d.\n",
|
| 335 | error);
|
| 336 | return error;
|
| 337 | }
|
| 338 |
|
| 339 | st1232_ts_power(ts, true);
|
| 340 |
|
| 341 | error = devm_add_action_or_reset(&client->dev, st1232_ts_power_off, ts);
|
| 342 | if (error) {
|
| 343 | dev_err(&client->dev,
|
| 344 | "Failed to install power off action: %d\n", error);
|
| 345 | return error;
|
| 346 | }
|
| 347 |
|
| 348 | input_dev->name = "st1232-touchscreen";
|
| 349 | input_dev->id.bustype = BUS_I2C;
|
| 350 |
|
| 351 |
|
| 352 | error = st1232_ts_wait_ready(ts);
|
| 353 | if (error)
|
| 354 | return error;
|
| 355 |
|
| 356 |
|
| 357 | error = st1232_ts_read_fw_version(ts, &ts->fw_version, &ts->fw_revision);
|
| 358 | if (error) {
|
| 359 | dev_err(&client->dev,
|
| 360 | "Failed to read firmware version: %d\n", error);
|
| 361 | return error;
|
| 362 | }
|
| 363 | dev_dbg(&client->dev, "Detected firmware version %u, rev %08x\n",
|
| 364 | ts->fw_version, ts->fw_revision);
|
| 365 |
|
| 366 | if (ts->chip_info->have_z)
|
| 367 | input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0,
|
| 368 | ts->chip_info->max_area, 0, 0);
|
| 369 |
|
| 370 |
|
| 371 | INIT_LIST_HEAD(&ts->touch_overlay_list);
|
| 372 | error = touch_overlay_map(&ts->touch_overlay_list, input_dev);
|
| 373 | if (error)
|
| 374 | return error;
|
| 375 |
|
| 376 | if (touch_overlay_mapped_touchscreen(&ts->touch_overlay_list)) {
|
| 377 |
|
| 378 | touch_overlay_get_touchscreen_abs(&ts->touch_overlay_list,
|
| 379 | &max_x, &max_y);
|
| 380 | } else {
|
| 381 |
|
| 382 | error = st1232_ts_read_resolution(ts, &max_x, &max_y);
|
| 383 | if (error) {
|
| 384 | dev_err(&client->dev,
|
| 385 | "Failed to read resolution: %d\n", error);
|
| 386 | return error;
|
| 387 | }
|
| 388 | }
|
| 389 |
|
| 390 | input_set_abs_params(input_dev, ABS_MT_POSITION_X,
|
| 391 | 0, max_x, 0, 0);
|
| 392 | input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
|
| 393 | 0, max_y, 0, 0);
|
| 394 |
|
| 395 | touchscreen_parse_properties(input_dev, true, &ts->prop);
|
| 396 |
|
| 397 | error = input_mt_init_slots(input_dev, ts->chip_info->max_fingers,
|
| 398 | INPUT_MT_DIRECT | INPUT_MT_TRACK |
|
| 399 | INPUT_MT_DROP_UNUSED);
|
| 400 | if (error) {
|
| 401 | dev_err(&client->dev, "failed to initialize MT slots\n");
|
| 402 | return error;
|
| 403 | }
|
| 404 |
|
| 405 | error = devm_request_threaded_irq(&client->dev, client->irq,
|
| 406 | NULL, st1232_ts_irq_handler,
|
| 407 | IRQF_ONESHOT,
|
| 408 | client->name, ts);
|
| 409 | if (error) {
|
| 410 | dev_err(&client->dev, "Failed to register interrupt\n");
|
| 411 | return error;
|
| 412 | }
|
| 413 |
|
| 414 | error = input_register_device(ts->input_dev);
|
| 415 | if (error) {
|
| 416 | dev_err(&client->dev, "Unable to register %s input device\n",
|
| 417 | input_dev->name);
|
| 418 | return error;
|
| 419 | }
|
| 420 |
|
| 421 | i2c_set_clientdata(client, ts);
|
| 422 |
|
| 423 | return 0;
|
| 424 | }
|
| 425 |
|
| 426 | static int st1232_ts_suspend(struct device *dev)
|
| 427 | {
|
| 428 | struct i2c_client *client = to_i2c_client(dev);
|
| 429 | struct st1232_ts_data *ts = i2c_get_clientdata(client);
|
| 430 |
|
| 431 | disable_irq(client->irq);
|
| 432 |
|
| 433 | if (!device_may_wakeup(&client->dev))
|
| 434 | st1232_ts_power(ts, false);
|
| 435 |
|
| 436 | return 0;
|
| 437 | }
|
| 438 |
|
| 439 | static int st1232_ts_resume(struct device *dev)
|
| 440 | {
|
| 441 | struct i2c_client *client = to_i2c_client(dev);
|
| 442 | struct st1232_ts_data *ts = i2c_get_clientdata(client);
|
| 443 |
|
| 444 | if (!device_may_wakeup(&client->dev))
|
| 445 | st1232_ts_power(ts, true);
|
| 446 |
|
| 447 | enable_irq(client->irq);
|
| 448 |
|
| 449 | return 0;
|
| 450 | }
|
| 451 |
|
| 452 | static DEFINE_SIMPLE_DEV_PM_OPS(st1232_ts_pm_ops,
|
| 453 | st1232_ts_suspend, st1232_ts_resume);
|
| 454 |
|
| 455 | static const struct i2c_device_id st1232_ts_id[] = {
|
| 456 | { .name = ST1232_TS_NAME, .driver_data = (unsigned long)&st1232_chip_info },
|
| 457 | { .name = ST1633_TS_NAME, .driver_data = (unsigned long)&st1633_chip_info },
|
| 458 | { }
|
| 459 | };
|
| 460 | MODULE_DEVICE_TABLE(i2c, st1232_ts_id);
|
| 461 |
|
| 462 | static const struct of_device_id st1232_ts_dt_ids[] = {
|
| 463 | { .compatible = "sitronix,st1232", .data = &st1232_chip_info },
|
| 464 | { .compatible = "sitronix,st1633", .data = &st1633_chip_info },
|
| 465 | { }
|
| 466 | };
|
| 467 | MODULE_DEVICE_TABLE(of, st1232_ts_dt_ids);
|
| 468 |
|
| 469 | static struct i2c_driver st1232_ts_driver = {
|
| 470 | .probe = st1232_ts_probe,
|
| 471 | .id_table = st1232_ts_id,
|
| 472 | .driver = {
|
| 473 | .name = ST1232_TS_NAME,
|
| 474 | .of_match_table = st1232_ts_dt_ids,
|
| 475 | .dev_groups = st1232_groups,
|
| 476 | .probe_type = PROBE_PREFER_ASYNCHRONOUS,
|
| 477 | .pm = pm_sleep_ptr(&st1232_ts_pm_ops),
|
| 478 | },
|
| 479 | };
|
| 480 |
|
| 481 | module_i2c_driver(st1232_ts_driver);
|
| 482 |
|
| 483 | MODULE_AUTHOR("Tony SIM <[email protected]>");
|
| 484 | MODULE_AUTHOR("Martin Kepplinger <[email protected]>");
|
| 485 | MODULE_DESCRIPTION("SITRONIX ST1232 Touchscreen Controller Driver");
|
| 486 | MODULE_LICENSE("GPL v2");
|