2 * drivers/input/touchscreen/tsc2007.c
4 * Copyright (c) 2008 MtekVision Co., Ltd.
5 * Kwangwoo Lee <kwlee@mtekvision.com>
9 * Copyright (c) 2005 David Brownell
10 * Copyright (c) 2006 Nokia Corporation
12 * Copyright (C) 2004-2005 Richard Purdie
13 * - omap_ts.[hc], ads7846.h, ts_osk.c
14 * Copyright (C) 2002 MontaVista Software
15 * Copyright (C) 2004 Texas Instruments
16 * Copyright (C) 2005 Dirk Behme
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License version 2 as
20 * published by the Free Software Foundation.
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/input.h>
26 #include <linux/interrupt.h>
27 #include <linux/i2c.h>
28 #include <linux/i2c/tsc2007.h>
29 #include <linux/of_device.h>
31 #include <linux/of_gpio.h>
33 #define TSC2007_MEASURE_TEMP0 (0x0 << 4)
34 #define TSC2007_MEASURE_AUX (0x2 << 4)
35 #define TSC2007_MEASURE_TEMP1 (0x4 << 4)
36 #define TSC2007_ACTIVATE_XN (0x8 << 4)
37 #define TSC2007_ACTIVATE_YN (0x9 << 4)
38 #define TSC2007_ACTIVATE_YP_XN (0xa << 4)
39 #define TSC2007_SETUP (0xb << 4)
40 #define TSC2007_MEASURE_X (0xc << 4)
41 #define TSC2007_MEASURE_Y (0xd << 4)
42 #define TSC2007_MEASURE_Z1 (0xe << 4)
43 #define TSC2007_MEASURE_Z2 (0xf << 4)
45 #define TSC2007_POWER_OFF_IRQ_EN (0x0 << 2)
46 #define TSC2007_ADC_ON_IRQ_DIS0 (0x1 << 2)
47 #define TSC2007_ADC_OFF_IRQ_EN (0x2 << 2)
48 #define TSC2007_ADC_ON_IRQ_DIS1 (0x3 << 2)
50 #define TSC2007_12BIT (0x0 << 1)
51 #define TSC2007_8BIT (0x1 << 1)
53 #define MAX_12BIT ((1 << 12) - 1)
55 #define ADC_ON_12BIT (TSC2007_12BIT | TSC2007_ADC_ON_IRQ_DIS0)
57 #define READ_Y (ADC_ON_12BIT | TSC2007_MEASURE_Y)
58 #define READ_Z1 (ADC_ON_12BIT | TSC2007_MEASURE_Z1)
59 #define READ_Z2 (ADC_ON_12BIT | TSC2007_MEASURE_Z2)
60 #define READ_X (ADC_ON_12BIT | TSC2007_MEASURE_X)
61 #define PWRDOWN (TSC2007_12BIT | TSC2007_POWER_OFF_IRQ_EN)
70 struct input_dev *input;
73 struct i2c_client *client;
78 unsigned long poll_delay;
79 unsigned long poll_period;
87 wait_queue_head_t wait;
90 int (*get_pendown_state)(struct device *);
91 void (*clear_penirq)(void);
94 static inline int tsc2007_xfer(struct tsc2007 *tsc, u8 cmd)
99 data = i2c_smbus_read_word_data(tsc->client, cmd);
101 dev_err(&tsc->client->dev, "i2c io error: %d\n", data);
105 /* The protocol and raw data format from i2c interface:
106 * S Addr Wr [A] Comm [A] S Addr Rd [A] [DataLow] A [DataHigh] NA P
107 * Where DataLow has [D11-D4], DataHigh has [D3-D0 << 4 | Dummy 4bit].
109 val = swab16(data) >> 4;
111 dev_dbg(&tsc->client->dev, "data: 0x%x, val: 0x%x\n", data, val);
116 static void tsc2007_read_values(struct tsc2007 *tsc, struct ts_event *tc)
118 /* y- still on; turn on only y+ (and ADC) */
119 tc->y = tsc2007_xfer(tsc, READ_Y);
121 /* turn y- off, x+ on, then leave in lowpower */
122 tc->x = tsc2007_xfer(tsc, READ_X);
124 /* turn y+ off, x- on; we'll use formula #1 */
125 tc->z1 = tsc2007_xfer(tsc, READ_Z1);
126 tc->z2 = tsc2007_xfer(tsc, READ_Z2);
128 /* Prepare for next touch reading - power down ADC, enable PENIRQ */
129 tsc2007_xfer(tsc, PWRDOWN);
132 static u32 tsc2007_calculate_pressure(struct tsc2007 *tsc, struct ts_event *tc)
136 /* range filtering */
137 if (tc->x == MAX_12BIT)
140 if (likely(tc->x && tc->z1)) {
141 /* compute touch pressure resistance using equation #1 */
142 rt = tc->z2 - tc->z1;
144 rt *= tsc->x_plate_ohms;
146 rt = (rt + 2047) >> 12;
152 static bool tsc2007_is_pen_down(struct tsc2007 *ts)
155 * NOTE: We can't rely on the pressure to determine the pen down
156 * state, even though this controller has a pressure sensor.
157 * The pressure value can fluctuate for quite a while after
158 * lifting the pen and in some cases may not even settle at the
161 * The only safe way to check for the pen up condition is in the
162 * work function by reading the pen signal state (it's a GPIO
163 * and IRQ). Unfortunately such callback is not always available,
164 * in that case we assume that the pen is down and expect caller
165 * to fall back on the pressure reading.
168 if (!ts->get_pendown_state)
171 return ts->get_pendown_state(&ts->client->dev);
174 static irqreturn_t tsc2007_soft_irq(int irq, void *handle)
176 struct tsc2007 *ts = handle;
177 struct input_dev *input = ts->input;
181 while (!ts->stopped && tsc2007_is_pen_down(ts)) {
183 /* pen is down, continue with the measurement */
184 tsc2007_read_values(ts, &tc);
186 rt = tsc2007_calculate_pressure(ts, &tc);
188 if (!rt && !ts->get_pendown_state) {
190 * If pressure reported is 0 and we don't have
191 * callback to check pendown state, we have to
192 * assume that pen was lifted up.
197 if (rt <= ts->max_rt) {
198 dev_dbg(&ts->client->dev,
199 "DOWN point(%4d,%4d), pressure (%4u)\n",
202 input_report_key(input, BTN_TOUCH, 1);
203 input_report_abs(input, ABS_X, tc.x);
204 input_report_abs(input, ABS_Y, tc.y);
205 input_report_abs(input, ABS_PRESSURE, rt);
211 * Sample found inconsistent by debouncing or pressure is
212 * beyond the maximum. Don't report it to user space,
213 * repeat at least once more the measurement.
215 dev_dbg(&ts->client->dev, "ignored pressure %d\n", rt);
218 wait_event_timeout(ts->wait, ts->stopped,
219 msecs_to_jiffies(ts->poll_period));
222 dev_dbg(&ts->client->dev, "UP\n");
224 input_report_key(input, BTN_TOUCH, 0);
225 input_report_abs(input, ABS_PRESSURE, 0);
228 if (ts->clear_penirq)
234 static irqreturn_t tsc2007_hard_irq(int irq, void *handle)
236 struct tsc2007 *ts = handle;
238 if (tsc2007_is_pen_down(ts))
239 return IRQ_WAKE_THREAD;
241 if (ts->clear_penirq)
247 static void tsc2007_stop(struct tsc2007 *ts)
253 disable_irq(ts->irq);
256 static int tsc2007_open(struct input_dev *input_dev)
258 struct tsc2007 *ts = input_get_drvdata(input_dev);
266 /* Prepare for touch readings - power down ADC and enable PENIRQ */
267 err = tsc2007_xfer(ts, PWRDOWN);
276 static void tsc2007_close(struct input_dev *input_dev)
278 struct tsc2007 *ts = input_get_drvdata(input_dev);
284 static int tsc2007_get_pendown_state_gpio(struct device *dev)
286 struct i2c_client *client = to_i2c_client(dev);
287 struct tsc2007 *ts = i2c_get_clientdata(client);
289 return !gpio_get_value(ts->gpio);
292 static int tsc2007_probe_dt(struct i2c_client *client, struct tsc2007 *ts)
294 struct device_node *np = client->dev.of_node;
299 dev_err(&client->dev, "missing device tree data\n");
303 if (!of_property_read_u32(np, "ti,max-rt", &val32))
306 ts->max_rt = MAX_12BIT;
308 if (!of_property_read_u32(np, "ti,fuzzx", &val32))
311 if (!of_property_read_u32(np, "ti,fuzzy", &val32))
314 if (!of_property_read_u32(np, "ti,fuzzz", &val32))
317 if (!of_property_read_u64(np, "ti,poll-period", &val64))
318 ts->poll_period = val64;
322 if (!of_property_read_u32(np, "ti,x-plate-ohms", &val32)) {
323 ts->x_plate_ohms = val32;
325 dev_err(&client->dev, "missing ti,x-plate-ohms devicetree property.");
329 ts->gpio = of_get_gpio(np, 0);
330 if (gpio_is_valid(ts->gpio))
331 ts->get_pendown_state = tsc2007_get_pendown_state_gpio;
333 dev_warn(&client->dev,
334 "GPIO not specified in DT (of_get_gpio returned %d)\n",
340 static int tsc2007_probe_dt(struct i2c_client *client, struct tsc2007 *ts)
342 dev_err(&client->dev, "platform data is required!\n");
347 static int tsc2007_probe_pdev(struct i2c_client *client, struct tsc2007 *ts,
348 const struct tsc2007_platform_data *pdata,
349 const struct i2c_device_id *id)
351 ts->model = pdata->model;
352 ts->x_plate_ohms = pdata->x_plate_ohms;
353 ts->max_rt = pdata->max_rt ? : MAX_12BIT;
354 ts->poll_delay = pdata->poll_delay ? : 1;
355 ts->poll_period = pdata->poll_period ? : 1;
356 ts->get_pendown_state = pdata->get_pendown_state;
357 ts->clear_penirq = pdata->clear_penirq;
358 ts->fuzzx = pdata->fuzzx;
359 ts->fuzzy = pdata->fuzzy;
360 ts->fuzzz = pdata->fuzzz;
362 if (pdata->x_plate_ohms == 0) {
363 dev_err(&client->dev, "x_plate_ohms is not set up in platform data");
370 static int tsc2007_probe(struct i2c_client *client,
371 const struct i2c_device_id *id)
373 const struct tsc2007_platform_data *pdata = dev_get_platdata(&client->dev);
375 struct input_dev *input_dev;
378 if (!i2c_check_functionality(client->adapter,
379 I2C_FUNC_SMBUS_READ_WORD_DATA))
382 ts = devm_kzalloc(&client->dev, sizeof(struct tsc2007), GFP_KERNEL);
387 err = tsc2007_probe_pdev(client, ts, pdata, id);
389 err = tsc2007_probe_dt(client, ts);
393 input_dev = input_allocate_device();
399 i2c_set_clientdata(client, ts);
402 ts->irq = client->irq;
403 ts->input = input_dev;
404 init_waitqueue_head(&ts->wait);
406 snprintf(ts->phys, sizeof(ts->phys),
407 "%s/input0", dev_name(&client->dev));
409 input_dev->name = "TSC2007 Touchscreen";
410 input_dev->phys = ts->phys;
411 input_dev->id.bustype = BUS_I2C;
413 input_dev->open = tsc2007_open;
414 input_dev->close = tsc2007_close;
416 input_set_drvdata(input_dev, ts);
418 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
419 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
421 input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, ts->fuzzx, 0);
422 input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, ts->fuzzy, 0);
423 input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT,
426 if (pdata && pdata->init_platform_hw)
427 pdata->init_platform_hw();
429 err = request_threaded_irq(ts->irq, tsc2007_hard_irq, tsc2007_soft_irq,
430 IRQF_ONESHOT, client->dev.driver->name, ts);
432 dev_err(&client->dev, "irq %d busy?\n", ts->irq);
438 err = input_register_device(input_dev);
445 free_irq(ts->irq, ts);
446 if (pdata && pdata->exit_platform_hw)
447 pdata->exit_platform_hw();
449 input_free_device(input_dev);
453 static int tsc2007_remove(struct i2c_client *client)
455 const struct tsc2007_platform_data *pdata = dev_get_platdata(&client->dev);
456 struct tsc2007 *ts = i2c_get_clientdata(client);
458 free_irq(ts->irq, ts);
460 if (pdata && pdata->exit_platform_hw)
461 pdata->exit_platform_hw();
463 input_unregister_device(ts->input);
469 static const struct i2c_device_id tsc2007_idtable[] = {
474 MODULE_DEVICE_TABLE(i2c, tsc2007_idtable);
477 static const struct of_device_id tsc2007_of_match[] = {
478 { .compatible = "ti,tsc2007" },
481 MODULE_DEVICE_TABLE(of, tsc2007_of_match);
484 static struct i2c_driver tsc2007_driver = {
486 .owner = THIS_MODULE,
488 .of_match_table = of_match_ptr(tsc2007_of_match),
490 .id_table = tsc2007_idtable,
491 .probe = tsc2007_probe,
492 .remove = tsc2007_remove,
495 module_i2c_driver(tsc2007_driver);
497 MODULE_AUTHOR("Kwangwoo Lee <kwlee@mtekvision.com>");
498 MODULE_DESCRIPTION("TSC2007 TouchScreen Driver");
499 MODULE_LICENSE("GPL");