| 1 | |
| 2 | |
| 3 | * drivers/input/touchscreen/tsc2007.c
|
| 4 | *
|
| 5 | * Copyright (c) 2008 MtekVision Co., Ltd.
|
| 6 | * Kwangwoo Lee <[email protected]>
|
| 7 | *
|
| 8 | * Using code from:
|
| 9 | * - ads7846.c
|
| 10 | * Copyright (c) 2005 David Brownell
|
| 11 | * Copyright (c) 2006 Nokia Corporation
|
| 12 | * - corgi_ts.c
|
| 13 | * Copyright (C) 2004-2005 Richard Purdie
|
| 14 | * - omap_ts.[hc], ads7846.h, ts_osk.c
|
| 15 | * Copyright (C) 2002 MontaVista Software
|
| 16 | * Copyright (C) 2004 Texas Instruments
|
| 17 | * Copyright (C) 2005 Dirk Behme
|
| 18 |
|
| 19 |
|
| 20 | #include <linux/module.h>
|
| 21 | #include <linux/slab.h>
|
| 22 | #include <linux/gpio/consumer.h>
|
| 23 | #include <linux/input.h>
|
| 24 | #include <linux/interrupt.h>
|
| 25 | #include <linux/i2c.h>
|
| 26 | #include <linux/math64.h>
|
| 27 | #include <linux/property.h>
|
| 28 | #include <linux/platform_data/tsc2007.h>
|
| 29 | #include "tsc2007.h"
|
| 30 |
|
| 31 | int tsc2007_xfer(struct tsc2007 *tsc, u8 cmd)
|
| 32 | {
|
| 33 | s32 data;
|
| 34 | u16 val;
|
| 35 |
|
| 36 | data = i2c_smbus_read_word_data(tsc->client, cmd);
|
| 37 | if (data < 0) {
|
| 38 | dev_err(&tsc->client->dev, "i2c io error: %d\n", data);
|
| 39 | return data;
|
| 40 | }
|
| 41 |
|
| 42 | |
| 43 | * S Addr Wr [A] Comm [A] S Addr Rd [A] [DataLow] A [DataHigh] NA P
|
| 44 | * Where DataLow has [D11-D4], DataHigh has [D3-D0 << 4 | Dummy 4bit].
|
| 45 |
|
| 46 | val = swab16(data) >> 4;
|
| 47 |
|
| 48 | dev_dbg(&tsc->client->dev, "data: 0x%x, val: 0x%x\n", data, val);
|
| 49 |
|
| 50 | return val;
|
| 51 | }
|
| 52 |
|
| 53 | static void tsc2007_read_values(struct tsc2007 *tsc, struct ts_event *tc)
|
| 54 | {
|
| 55 |
|
| 56 | tc->y = tsc2007_xfer(tsc, READ_Y);
|
| 57 |
|
| 58 |
|
| 59 | tc->x = tsc2007_xfer(tsc, READ_X);
|
| 60 |
|
| 61 |
|
| 62 | tc->z1 = tsc2007_xfer(tsc, READ_Z1);
|
| 63 |
|
| 64 | tc->z2 = tsc2007_xfer(tsc, (TSC2007_POWER_OFF_IRQ_EN | TSC2007_MEASURE_Z2));
|
| 65 | }
|
| 66 |
|
| 67 | u32 tsc2007_calculate_resistance(struct tsc2007 *tsc, struct ts_event *tc)
|
| 68 | {
|
| 69 | u64 rt = 0;
|
| 70 |
|
| 71 |
|
| 72 | if (tc->x == MAX_12BIT)
|
| 73 | tc->x = 0;
|
| 74 |
|
| 75 | if (likely(tc->x && tc->z1)) {
|
| 76 |
|
| 77 | rt = tc->z2 - tc->z1;
|
| 78 | rt *= tc->x;
|
| 79 | rt *= tsc->x_plate_ohms;
|
| 80 | rt = div_u64(rt, tc->z1);
|
| 81 | rt = (rt + 2047) >> 12;
|
| 82 | }
|
| 83 |
|
| 84 | if (rt > U32_MAX)
|
| 85 | return U32_MAX;
|
| 86 | return (u32) rt;
|
| 87 | }
|
| 88 |
|
| 89 | bool tsc2007_is_pen_down(struct tsc2007 *ts)
|
| 90 | {
|
| 91 | |
| 92 | * NOTE: We can't rely on the pressure to determine the pen down
|
| 93 | * state, even though this controller has a pressure sensor.
|
| 94 | * The pressure value can fluctuate for quite a while after
|
| 95 | * lifting the pen and in some cases may not even settle at the
|
| 96 | * expected value.
|
| 97 | *
|
| 98 | * The only safe way to check for the pen up condition is in the
|
| 99 | * work function by reading the pen signal state (it's a GPIO
|
| 100 | * and IRQ). Unfortunately such callback is not always available,
|
| 101 | * in that case we assume that the pen is down and expect caller
|
| 102 | * to fall back on the pressure reading.
|
| 103 |
|
| 104 |
|
| 105 | if (!ts->get_pendown_state)
|
| 106 | return true;
|
| 107 |
|
| 108 | return ts->get_pendown_state(&ts->client->dev);
|
| 109 | }
|
| 110 |
|
| 111 | static irqreturn_t tsc2007_soft_irq(int irq, void *handle)
|
| 112 | {
|
| 113 | struct tsc2007 *ts = handle;
|
| 114 | struct input_dev *input = ts->input;
|
| 115 | struct ts_event tc;
|
| 116 | u32 rt;
|
| 117 |
|
| 118 | while (!ts->stopped && tsc2007_is_pen_down(ts)) {
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 | scoped_guard(mutex, &ts->mlock) {
|
| 124 | tsc2007_read_values(ts, &tc);
|
| 125 | }
|
| 126 |
|
| 127 | rt = tsc2007_calculate_resistance(ts, &tc);
|
| 128 |
|
| 129 | if (!rt && !ts->get_pendown_state) {
|
| 130 | |
| 131 | * If pressure reported is 0 and we don't have
|
| 132 | * callback to check pendown state, we have to
|
| 133 | * assume that pen was lifted up.
|
| 134 |
|
| 135 | break;
|
| 136 | }
|
| 137 |
|
| 138 | if (rt <= ts->max_rt) {
|
| 139 | dev_dbg(&ts->client->dev,
|
| 140 | "DOWN point(%4d,%4d), resistance (%4u)\n",
|
| 141 | tc.x, tc.y, rt);
|
| 142 |
|
| 143 | rt = ts->max_rt - rt;
|
| 144 |
|
| 145 | input_report_key(input, BTN_TOUCH, 1);
|
| 146 | touchscreen_report_pos(input, &ts->prop, tc.x, tc.y, false);
|
| 147 | input_report_abs(input, ABS_PRESSURE, rt);
|
| 148 |
|
| 149 | input_sync(input);
|
| 150 |
|
| 151 | } else {
|
| 152 | |
| 153 | * Sample found inconsistent by debouncing or pressure is
|
| 154 | * beyond the maximum. Don't report it to user space,
|
| 155 | * repeat at least once more the measurement.
|
| 156 |
|
| 157 | dev_dbg(&ts->client->dev, "ignored pressure %d\n", rt);
|
| 158 | }
|
| 159 |
|
| 160 | wait_event_timeout(ts->wait, ts->stopped, ts->poll_period);
|
| 161 | }
|
| 162 |
|
| 163 | dev_dbg(&ts->client->dev, "UP\n");
|
| 164 |
|
| 165 | input_report_key(input, BTN_TOUCH, 0);
|
| 166 | input_report_abs(input, ABS_PRESSURE, 0);
|
| 167 | input_sync(input);
|
| 168 |
|
| 169 | if (ts->clear_penirq)
|
| 170 | ts->clear_penirq();
|
| 171 |
|
| 172 | return IRQ_HANDLED;
|
| 173 | }
|
| 174 |
|
| 175 | static void tsc2007_stop(struct tsc2007 *ts)
|
| 176 | {
|
| 177 | ts->stopped = true;
|
| 178 | mb();
|
| 179 | wake_up(&ts->wait);
|
| 180 |
|
| 181 | if (ts->irq)
|
| 182 | disable_irq(ts->irq);
|
| 183 | }
|
| 184 |
|
| 185 | static int tsc2007_open(struct input_dev *input_dev)
|
| 186 | {
|
| 187 | struct tsc2007 *ts = input_get_drvdata(input_dev);
|
| 188 | int err;
|
| 189 |
|
| 190 | ts->stopped = false;
|
| 191 | mb();
|
| 192 |
|
| 193 | if (ts->irq)
|
| 194 | enable_irq(ts->irq);
|
| 195 |
|
| 196 |
|
| 197 | err = tsc2007_xfer(ts, PWRDOWN);
|
| 198 | if (err < 0) {
|
| 199 | tsc2007_stop(ts);
|
| 200 | return err;
|
| 201 | }
|
| 202 |
|
| 203 | return 0;
|
| 204 | }
|
| 205 |
|
| 206 | static void tsc2007_close(struct input_dev *input_dev)
|
| 207 | {
|
| 208 | struct tsc2007 *ts = input_get_drvdata(input_dev);
|
| 209 |
|
| 210 | tsc2007_stop(ts);
|
| 211 | }
|
| 212 |
|
| 213 | static int tsc2007_get_pendown_state_gpio(struct device *dev)
|
| 214 | {
|
| 215 | struct i2c_client *client = to_i2c_client(dev);
|
| 216 | struct tsc2007 *ts = i2c_get_clientdata(client);
|
| 217 |
|
| 218 | return gpiod_get_value_cansleep(ts->gpiod);
|
| 219 | }
|
| 220 |
|
| 221 | static int tsc2007_probe_properties(struct device *dev, struct tsc2007 *ts)
|
| 222 | {
|
| 223 | u32 val32;
|
| 224 |
|
| 225 | if (!device_property_read_u32(dev, "ti,max-rt", &val32))
|
| 226 | ts->max_rt = val32;
|
| 227 | else
|
| 228 | ts->max_rt = MAX_12BIT;
|
| 229 |
|
| 230 | if (!device_property_read_u32(dev, "ti,fuzzx", &val32))
|
| 231 | ts->fuzzx = val32;
|
| 232 |
|
| 233 | if (!device_property_read_u32(dev, "ti,fuzzy", &val32))
|
| 234 | ts->fuzzy = val32;
|
| 235 |
|
| 236 | if (!device_property_read_u32(dev, "ti,fuzzz", &val32))
|
| 237 | ts->fuzzz = val32;
|
| 238 |
|
| 239 | if (!device_property_read_u32(dev, "ti,poll-period", &val32))
|
| 240 | ts->poll_period = msecs_to_jiffies(val32);
|
| 241 | else
|
| 242 | ts->poll_period = msecs_to_jiffies(1);
|
| 243 |
|
| 244 | if (!device_property_read_u32(dev, "ti,x-plate-ohms", &val32)) {
|
| 245 | ts->x_plate_ohms = val32;
|
| 246 | } else {
|
| 247 | dev_err(dev, "Missing ti,x-plate-ohms device property\n");
|
| 248 | return -EINVAL;
|
| 249 | }
|
| 250 |
|
| 251 | ts->gpiod = devm_gpiod_get_optional(dev, NULL, GPIOD_IN);
|
| 252 | if (IS_ERR(ts->gpiod))
|
| 253 | return PTR_ERR(ts->gpiod);
|
| 254 |
|
| 255 | if (ts->gpiod)
|
| 256 | ts->get_pendown_state = tsc2007_get_pendown_state_gpio;
|
| 257 | else
|
| 258 | dev_dbg(dev, "Pen down GPIO is not specified in properties\n");
|
| 259 |
|
| 260 | return 0;
|
| 261 | }
|
| 262 |
|
| 263 | static int tsc2007_probe_pdev(struct device *dev, struct tsc2007 *ts,
|
| 264 | const struct tsc2007_platform_data *pdata,
|
| 265 | const struct i2c_device_id *id)
|
| 266 | {
|
| 267 | ts->model = pdata->model;
|
| 268 | ts->x_plate_ohms = pdata->x_plate_ohms;
|
| 269 | ts->max_rt = pdata->max_rt ? : MAX_12BIT;
|
| 270 | ts->poll_period = msecs_to_jiffies(pdata->poll_period ? : 1);
|
| 271 | ts->get_pendown_state = pdata->get_pendown_state;
|
| 272 | ts->clear_penirq = pdata->clear_penirq;
|
| 273 | ts->fuzzx = pdata->fuzzx;
|
| 274 | ts->fuzzy = pdata->fuzzy;
|
| 275 | ts->fuzzz = pdata->fuzzz;
|
| 276 |
|
| 277 | if (pdata->x_plate_ohms == 0) {
|
| 278 | dev_err(dev, "x_plate_ohms is not set up in platform data\n");
|
| 279 | return -EINVAL;
|
| 280 | }
|
| 281 |
|
| 282 | return 0;
|
| 283 | }
|
| 284 |
|
| 285 | static void tsc2007_call_exit_platform_hw(void *data)
|
| 286 | {
|
| 287 | struct device *dev = data;
|
| 288 | const struct tsc2007_platform_data *pdata = dev_get_platdata(dev);
|
| 289 |
|
| 290 | pdata->exit_platform_hw();
|
| 291 | }
|
| 292 |
|
| 293 | static int tsc2007_probe(struct i2c_client *client)
|
| 294 | {
|
| 295 | const struct i2c_device_id *id = i2c_client_get_device_id(client);
|
| 296 | const struct tsc2007_platform_data *pdata =
|
| 297 | dev_get_platdata(&client->dev);
|
| 298 | struct tsc2007 *ts;
|
| 299 | struct input_dev *input_dev;
|
| 300 | int err;
|
| 301 |
|
| 302 | if (!i2c_check_functionality(client->adapter,
|
| 303 | I2C_FUNC_SMBUS_READ_WORD_DATA))
|
| 304 | return -EIO;
|
| 305 |
|
| 306 | ts = devm_kzalloc(&client->dev, sizeof(struct tsc2007), GFP_KERNEL);
|
| 307 | if (!ts)
|
| 308 | return -ENOMEM;
|
| 309 |
|
| 310 | if (pdata)
|
| 311 | err = tsc2007_probe_pdev(&client->dev, ts, pdata, id);
|
| 312 | else
|
| 313 | err = tsc2007_probe_properties(&client->dev, ts);
|
| 314 | if (err)
|
| 315 | return err;
|
| 316 |
|
| 317 | input_dev = devm_input_allocate_device(&client->dev);
|
| 318 | if (!input_dev)
|
| 319 | return -ENOMEM;
|
| 320 |
|
| 321 | i2c_set_clientdata(client, ts);
|
| 322 |
|
| 323 | ts->client = client;
|
| 324 | ts->irq = client->irq;
|
| 325 | ts->input = input_dev;
|
| 326 |
|
| 327 | init_waitqueue_head(&ts->wait);
|
| 328 | mutex_init(&ts->mlock);
|
| 329 |
|
| 330 | snprintf(ts->phys, sizeof(ts->phys),
|
| 331 | "%s/input0", dev_name(&client->dev));
|
| 332 |
|
| 333 | input_dev->name = "TSC2007 Touchscreen";
|
| 334 | input_dev->phys = ts->phys;
|
| 335 | input_dev->id.bustype = BUS_I2C;
|
| 336 |
|
| 337 | input_dev->open = tsc2007_open;
|
| 338 | input_dev->close = tsc2007_close;
|
| 339 |
|
| 340 | input_set_drvdata(input_dev, ts);
|
| 341 |
|
| 342 | input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
|
| 343 | input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, ts->fuzzx, 0);
|
| 344 | input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, ts->fuzzy, 0);
|
| 345 | touchscreen_parse_properties(input_dev, false, &ts->prop);
|
| 346 | input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT,
|
| 347 | ts->fuzzz, 0);
|
| 348 |
|
| 349 | if (pdata) {
|
| 350 | if (pdata->exit_platform_hw) {
|
| 351 | err = devm_add_action(&client->dev,
|
| 352 | tsc2007_call_exit_platform_hw,
|
| 353 | &client->dev);
|
| 354 | if (err) {
|
| 355 | dev_err(&client->dev,
|
| 356 | "Failed to register exit_platform_hw action, %d\n",
|
| 357 | err);
|
| 358 | return err;
|
| 359 | }
|
| 360 | }
|
| 361 |
|
| 362 | if (pdata->init_platform_hw)
|
| 363 | pdata->init_platform_hw();
|
| 364 | }
|
| 365 |
|
| 366 | if (ts->irq) {
|
| 367 | err = devm_request_threaded_irq(&client->dev, ts->irq,
|
| 368 | NULL, tsc2007_soft_irq,
|
| 369 | IRQF_ONESHOT,
|
| 370 | client->dev.driver->name, ts);
|
| 371 | if (err) {
|
| 372 | dev_err(&client->dev, "Failed to request irq %d: %d\n",
|
| 373 | ts->irq, err);
|
| 374 | return err;
|
| 375 | }
|
| 376 |
|
| 377 | tsc2007_stop(ts);
|
| 378 | }
|
| 379 |
|
| 380 |
|
| 381 | err = tsc2007_xfer(ts, PWRDOWN);
|
| 382 | if (err < 0) {
|
| 383 | dev_err(&client->dev,
|
| 384 | "Failed to setup chip: %d\n", err);
|
| 385 | return err;
|
| 386 | }
|
| 387 |
|
| 388 | err = input_register_device(input_dev);
|
| 389 | if (err) {
|
| 390 | dev_err(&client->dev,
|
| 391 | "Failed to register input device: %d\n", err);
|
| 392 | return err;
|
| 393 | }
|
| 394 |
|
| 395 | err = tsc2007_iio_configure(ts);
|
| 396 | if (err) {
|
| 397 | dev_err(&client->dev,
|
| 398 | "Failed to register with IIO: %d\n", err);
|
| 399 | return err;
|
| 400 | }
|
| 401 |
|
| 402 | return 0;
|
| 403 | }
|
| 404 |
|
| 405 | static const struct i2c_device_id tsc2007_idtable[] = {
|
| 406 | { .name = "tsc2007" },
|
| 407 | { }
|
| 408 | };
|
| 409 |
|
| 410 | MODULE_DEVICE_TABLE(i2c, tsc2007_idtable);
|
| 411 |
|
| 412 | static const struct of_device_id tsc2007_of_match[] = {
|
| 413 | { .compatible = "ti,tsc2007" },
|
| 414 | { }
|
| 415 | };
|
| 416 | MODULE_DEVICE_TABLE(of, tsc2007_of_match);
|
| 417 |
|
| 418 | static struct i2c_driver tsc2007_driver = {
|
| 419 | .driver = {
|
| 420 | .name = "tsc2007",
|
| 421 | .of_match_table = tsc2007_of_match,
|
| 422 | },
|
| 423 | .id_table = tsc2007_idtable,
|
| 424 | .probe = tsc2007_probe,
|
| 425 | };
|
| 426 |
|
| 427 | module_i2c_driver(tsc2007_driver);
|
| 428 |
|
| 429 | MODULE_AUTHOR("Kwangwoo Lee <[email protected]>");
|
| 430 | MODULE_DESCRIPTION("TSC2007 TouchScreen Driver");
|
| 431 | MODULE_LICENSE("GPL");
|