blob: 55639e0cee82c0d2708dabf1da455f23a25ce333 [file] [log] [blame]
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -05001/*
2 * drivers/input/touchscreen/tsc2007.c
3 *
4 * Copyright (c) 2008 MtekVision Co., Ltd.
5 * Kwangwoo Lee <kwlee@mtekvision.com>
6 *
7 * Using code from:
8 * - ads7846.c
9 * Copyright (c) 2005 David Brownell
10 * Copyright (c) 2006 Nokia Corporation
11 * - corgi_ts.c
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
17 *
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.
21 */
22
23#include <linux/module.h>
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -050024#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>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070029#include <linux/pm.h>
30
31#if defined(CONFIG_HAS_EARLYSUSPEND)
32#include <linux/earlysuspend.h>
33#define TSC2007_SUSPEND_LEVEL 1
34#endif
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -050035
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -050036#define TSC2007_MEASURE_TEMP0 (0x0 << 4)
37#define TSC2007_MEASURE_AUX (0x2 << 4)
38#define TSC2007_MEASURE_TEMP1 (0x4 << 4)
39#define TSC2007_ACTIVATE_XN (0x8 << 4)
40#define TSC2007_ACTIVATE_YN (0x9 << 4)
41#define TSC2007_ACTIVATE_YP_XN (0xa << 4)
42#define TSC2007_SETUP (0xb << 4)
43#define TSC2007_MEASURE_X (0xc << 4)
44#define TSC2007_MEASURE_Y (0xd << 4)
45#define TSC2007_MEASURE_Z1 (0xe << 4)
46#define TSC2007_MEASURE_Z2 (0xf << 4)
47
48#define TSC2007_POWER_OFF_IRQ_EN (0x0 << 2)
49#define TSC2007_ADC_ON_IRQ_DIS0 (0x1 << 2)
50#define TSC2007_ADC_OFF_IRQ_EN (0x2 << 2)
51#define TSC2007_ADC_ON_IRQ_DIS1 (0x3 << 2)
52
53#define TSC2007_12BIT (0x0 << 1)
54#define TSC2007_8BIT (0x1 << 1)
55
56#define MAX_12BIT ((1 << 12) - 1)
57
58#define ADC_ON_12BIT (TSC2007_12BIT | TSC2007_ADC_ON_IRQ_DIS0)
59
60#define READ_Y (ADC_ON_12BIT | TSC2007_MEASURE_Y)
61#define READ_Z1 (ADC_ON_12BIT | TSC2007_MEASURE_Z1)
62#define READ_Z2 (ADC_ON_12BIT | TSC2007_MEASURE_Z2)
63#define READ_X (ADC_ON_12BIT | TSC2007_MEASURE_X)
64#define PWRDOWN (TSC2007_12BIT | TSC2007_POWER_OFF_IRQ_EN)
65
66struct ts_event {
67 u16 x;
68 u16 y;
69 u16 z1, z2;
70};
71
72struct tsc2007 {
73 struct input_dev *input;
74 char phys[32];
Richard Röjfors75fba3b2009-07-24 22:01:39 -070075 struct delayed_work work;
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -050076
77 struct i2c_client *client;
78
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -050079 u16 model;
80 u16 x_plate_ohms;
Thierry Reding84005eb2011-05-17 09:31:01 -070081 u16 max_rt;
Thierry Reding2d137c7e2011-05-17 09:31:33 -070082 unsigned long poll_delay;
Thierry Reding1af38ea2011-05-17 09:32:02 -070083 unsigned long poll_period;
Anirudh Ghayal482cccc2012-01-25 12:19:19 +053084 u16 min_x;
85 u16 max_x;
86 u16 min_y;
87 u16 max_y;
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -050088
Dmitry Torokhov141586bc2009-07-24 23:14:16 -070089 bool pendown;
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -050090 int irq;
91
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070092 bool invert_x;
93 bool invert_y;
94 bool invert_z1;
95 bool invert_z2;
Dmitry Torokhov377dc552011-08-25 00:25:12 -070096
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -050097 int (*get_pendown_state)(void);
98 void (*clear_penirq)(void);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099 int (*power_shutdown)(bool);
100#if defined(CONFIG_HAS_EARLYSUSPEND)
101 struct early_suspend early_suspend;
102#endif
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500103};
104
105static inline int tsc2007_xfer(struct tsc2007 *tsc, u8 cmd)
106{
107 s32 data;
108 u16 val;
109
110 data = i2c_smbus_read_word_data(tsc->client, cmd);
111 if (data < 0) {
112 dev_err(&tsc->client->dev, "i2c io error: %d\n", data);
113 return data;
114 }
115
116 /* The protocol and raw data format from i2c interface:
117 * S Addr Wr [A] Comm [A] S Addr Rd [A] [DataLow] A [DataHigh] NA P
118 * Where DataLow has [D11-D4], DataHigh has [D3-D0 << 4 | Dummy 4bit].
119 */
120 val = swab16(data) >> 4;
121
122 dev_dbg(&tsc->client->dev, "data: 0x%x, val: 0x%x\n", data, val);
123
124 return val;
125}
126
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700127static void tsc2007_read_values(struct tsc2007 *tsc, struct ts_event *tc)
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500128{
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700129 /* y- still on; turn on only y+ (and ADC) */
130 tc->y = tsc2007_xfer(tsc, READ_Y);
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500131
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700132 /* turn y- off, x+ on, then leave in lowpower */
133 tc->x = tsc2007_xfer(tsc, READ_X);
134
135 /* turn y+ off, x- on; we'll use formula #1 */
136 tc->z1 = tsc2007_xfer(tsc, READ_Z1);
137 tc->z2 = tsc2007_xfer(tsc, READ_Z2);
138
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700139 if (tsc->invert_x == true)
140 tc->x = MAX_12BIT - tc->x;
141
142 if (tsc->invert_y == true)
143 tc->y = MAX_12BIT - tc->y;
144
145 if (tsc->invert_z1 == true)
146 tc->z1 = MAX_12BIT - tc->z1;
147
148 if (tsc->invert_z2 == true)
149 tc->z2 = MAX_12BIT - tc->z2;
150
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700151 /* Prepare for next touch reading - power down ADC, enable PENIRQ */
152 tsc2007_xfer(tsc, PWRDOWN);
153}
154
155static u32 tsc2007_calculate_pressure(struct tsc2007 *tsc, struct ts_event *tc)
156{
157 u32 rt = 0;
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500158
159 /* range filtering */
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700160 if (tc->x == MAX_12BIT)
161 tc->x = 0;
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500162
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700163 if (likely(tc->x && tc->z1)) {
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500164 /* compute touch pressure resistance using equation #1 */
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700165 rt = tc->z2 - tc->z1;
166 rt *= tc->x;
167 rt *= tsc->x_plate_ohms;
168 rt /= tc->z1;
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500169 rt = (rt + 2047) >> 12;
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500170 }
171
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700172 return rt;
173}
174
175static void tsc2007_send_up_event(struct tsc2007 *tsc)
176{
177 struct input_dev *input = tsc->input;
178
179 dev_dbg(&tsc->client->dev, "UP\n");
180
181 input_report_key(input, BTN_TOUCH, 0);
182 input_report_abs(input, ABS_PRESSURE, 0);
183 input_sync(input);
184}
185
186static void tsc2007_work(struct work_struct *work)
187{
188 struct tsc2007 *ts =
189 container_of(to_delayed_work(work), struct tsc2007, work);
Thierry Reding4a1a42a2011-05-17 09:29:45 -0700190 bool debounced = false;
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700191 struct ts_event tc;
192 u32 rt;
193
Dmitry Torokhov141586bc2009-07-24 23:14:16 -0700194 /*
195 * NOTE: We can't rely on the pressure to determine the pen down
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700196 * state, even though this controller has a pressure sensor.
197 * The pressure value can fluctuate for quite a while after
198 * lifting the pen and in some cases may not even settle at the
199 * expected value.
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500200 *
201 * The only safe way to check for the pen up condition is in the
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700202 * work function by reading the pen signal state (it's a GPIO
203 * and IRQ). Unfortunately such callback is not always available,
204 * in that case we have rely on the pressure anyway.
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500205 */
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700206 if (ts->get_pendown_state) {
207 if (unlikely(!ts->get_pendown_state())) {
208 tsc2007_send_up_event(ts);
209 ts->pendown = false;
210 goto out;
Dmitry Torokhov377dc552011-08-25 00:25:12 -0700211 }
212
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700213 dev_dbg(&ts->client->dev, "pen is still down\n");
Dmitry Torokhov377dc552011-08-25 00:25:12 -0700214 }
215
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700216 tsc2007_read_values(ts, &tc);
Dmitry Torokhov377dc552011-08-25 00:25:12 -0700217
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700218 rt = tsc2007_calculate_pressure(ts, &tc);
Thierry Reding84005eb2011-05-17 09:31:01 -0700219 if (rt > ts->max_rt) {
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700220 /*
221 * Sample found inconsistent by debouncing or pressure is
222 * beyond the maximum. Don't report it to user space,
223 * repeat at least once more the measurement.
224 */
225 dev_dbg(&ts->client->dev, "ignored pressure %d\n", rt);
Thierry Reding4a1a42a2011-05-17 09:29:45 -0700226 debounced = true;
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700227 goto out;
Dmitry Torokhov377dc552011-08-25 00:25:12 -0700228
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700229 }
Dmitry Torokhov377dc552011-08-25 00:25:12 -0700230
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500231 if (rt) {
232 struct input_dev *input = ts->input;
233
234 if (!ts->pendown) {
235 dev_dbg(&ts->client->dev, "DOWN\n");
236
237 input_report_key(input, BTN_TOUCH, 1);
Dmitry Torokhov141586bc2009-07-24 23:14:16 -0700238 ts->pendown = true;
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500239 }
240
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700241 input_report_abs(input, ABS_X, tc.x);
242 input_report_abs(input, ABS_Y, tc.y);
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500243 input_report_abs(input, ABS_PRESSURE, rt);
244
245 input_sync(input);
246
247 dev_dbg(&ts->client->dev, "point(%4d,%4d), pressure (%4u)\n",
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700248 tc.x, tc.y, rt);
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500249
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700250 } else if (!ts->get_pendown_state && ts->pendown) {
251 /*
252 * We don't have callback to check pendown state, so we
253 * have to assume that since pressure reported is 0 the
254 * pen was lifted up.
255 */
256 tsc2007_send_up_event(ts);
Dmitry Torokhov141586bc2009-07-24 23:14:16 -0700257 ts->pendown = false;
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700258 }
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500259
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700260 out:
Thierry Reding4a1a42a2011-05-17 09:29:45 -0700261 if (ts->pendown || debounced)
Dmitry Torokhov141586bc2009-07-24 23:14:16 -0700262 schedule_delayed_work(&ts->work,
Thierry Reding1af38ea2011-05-17 09:32:02 -0700263 msecs_to_jiffies(ts->poll_period));
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700264 else
265 enable_irq(ts->irq);
Dmitry Torokhov377dc552011-08-25 00:25:12 -0700266}
267
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500268static irqreturn_t tsc2007_irq(int irq, void *handle)
269{
270 struct tsc2007 *ts = handle;
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500271
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700272 if (!ts->get_pendown_state || likely(ts->get_pendown_state())) {
Ben Nizette29fa98b2009-04-17 20:35:57 -0700273 disable_irq_nosync(ts->irq);
Dmitry Torokhov141586bc2009-07-24 23:14:16 -0700274 schedule_delayed_work(&ts->work,
Thierry Reding2d137c7e2011-05-17 09:31:33 -0700275 msecs_to_jiffies(ts->poll_delay));
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500276 }
277
278 if (ts->clear_penirq)
279 ts->clear_penirq();
280
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500281 return IRQ_HANDLED;
282}
283
Dmitry Torokhov141586bc2009-07-24 23:14:16 -0700284static void tsc2007_free_irq(struct tsc2007 *ts)
285{
286 free_irq(ts->irq, ts);
287 if (cancel_delayed_work_sync(&ts->work)) {
288 /*
289 * Work was pending, therefore we need to enable
290 * IRQ here to balance the disable_irq() done in the
291 * interrupt handler.
292 */
293 enable_irq(ts->irq);
294 }
Dmitry Torokhovd3654d72011-08-25 01:05:46 -0700295}
296
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700297#ifdef CONFIG_PM
298static int tsc2007_suspend(struct device *dev)
Dmitry Torokhovd3654d72011-08-25 01:05:46 -0700299{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700300 int rc;
301 struct tsc2007 *ts = dev_get_drvdata(dev);
Dmitry Torokhovd3654d72011-08-25 01:05:46 -0700302
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700303 disable_irq(ts->irq);
Dmitry Torokhovd3654d72011-08-25 01:05:46 -0700304
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700305 if (cancel_delayed_work_sync(&ts->work))
306 enable_irq(ts->irq);
Dmitry Torokhovd3654d72011-08-25 01:05:46 -0700307
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700308 if (ts->power_shutdown) {
309 rc = ts->power_shutdown(true);
310 if (rc) {
311 pr_err("%s: Power off failed, suspend failed (%d)\n",
312 __func__, rc);
313 return rc;
314 }
Dmitry Torokhovd3654d72011-08-25 01:05:46 -0700315 }
316
317 return 0;
318}
319
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700320static int tsc2007_resume(struct device *dev)
Dmitry Torokhovd3654d72011-08-25 01:05:46 -0700321{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700322 int rc;
323 struct tsc2007 *ts = dev_get_drvdata(dev);
Dmitry Torokhovd3654d72011-08-25 01:05:46 -0700324
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700325 if (ts->power_shutdown) {
326 rc = ts->power_shutdown(false);
327 if (rc) {
328 pr_err("%s: Power on failed, resume failed (%d)\n",
329 __func__, rc);
330 return rc;
331 }
332 }
333
334 enable_irq(ts->irq);
335
336 return 0;
Dmitry Torokhov141586bc2009-07-24 23:14:16 -0700337}
338
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700339#ifdef CONFIG_HAS_EARLYSUSPEND
340static void tsc2007_early_suspend(struct early_suspend *h)
341{
342 struct tsc2007 *ts = container_of(h, struct tsc2007, early_suspend);
343
344 tsc2007_suspend(&ts->client->dev);
345}
346
347static void tsc2007_late_resume(struct early_suspend *h)
348{
349 struct tsc2007 *ts = container_of(h, struct tsc2007, early_suspend);
350
351 tsc2007_resume(&ts->client->dev);
352}
353#endif
354
355static const struct dev_pm_ops tsc2007_pm_ops = {
356#ifndef CONFIG_HAS_EARLYSUSPEND
357 .suspend = tsc2007_suspend,
358 .resume = tsc2007_resume,
359#endif
360};
361#endif
362
Dmitry Torokhov141586bc2009-07-24 23:14:16 -0700363static int __devinit tsc2007_probe(struct i2c_client *client,
364 const struct i2c_device_id *id)
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500365{
366 struct tsc2007 *ts;
Axel Linb496acb2010-09-02 19:52:41 -0700367 struct tsc2007_platform_data *pdata = client->dev.platform_data;
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500368 struct input_dev *input_dev;
369 int err;
370
Dmitry Torokhovd570e9e2009-08-04 22:07:26 -0700371 if (!pdata) {
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500372 dev_err(&client->dev, "platform data is required!\n");
373 return -EINVAL;
374 }
375
376 if (!i2c_check_functionality(client->adapter,
377 I2C_FUNC_SMBUS_READ_WORD_DATA))
378 return -EIO;
379
380 ts = kzalloc(sizeof(struct tsc2007), GFP_KERNEL);
381 input_dev = input_allocate_device();
382 if (!ts || !input_dev) {
383 err = -ENOMEM;
384 goto err_free_mem;
385 }
386
387 ts->client = client;
Dmitry Torokhov141586bc2009-07-24 23:14:16 -0700388 ts->irq = client->irq;
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500389 ts->input = input_dev;
Dmitry Torokhov141586bc2009-07-24 23:14:16 -0700390 INIT_DELAYED_WORK(&ts->work, tsc2007_work);
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500391
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500392 ts->model = pdata->model;
393 ts->x_plate_ohms = pdata->x_plate_ohms;
Thierry Reding84005eb2011-05-17 09:31:01 -0700394 ts->max_rt = pdata->max_rt ? : MAX_12BIT;
Thierry Reding2d137c7e2011-05-17 09:31:33 -0700395 ts->poll_delay = pdata->poll_delay ? : 1;
Thierry Reding1af38ea2011-05-17 09:32:02 -0700396 ts->poll_period = pdata->poll_period ? : 1;
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500397 ts->get_pendown_state = pdata->get_pendown_state;
398 ts->clear_penirq = pdata->clear_penirq;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700399 ts->invert_x = pdata->invert_x;
400 ts->invert_y = pdata->invert_y;
401 ts->invert_z1 = pdata->invert_z1;
402 ts->invert_z2 = pdata->invert_z2;
Anirudh Ghayal482cccc2012-01-25 12:19:19 +0530403 ts->min_x = pdata->min_x ? pdata->min_x : 0;
404 ts->max_x = pdata->max_x ? pdata->max_x : MAX_12BIT;
405 ts->min_y = pdata->min_y ? pdata->min_y : 0;
406 ts->max_y = pdata->max_y ? pdata->max_y : MAX_12BIT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700407 ts->power_shutdown = pdata->power_shutdown;
Philip Rakity341deef2011-10-11 20:54:55 -0700408
Kay Sievers4e8718a2009-01-29 22:56:08 -0800409 snprintf(ts->phys, sizeof(ts->phys),
410 "%s/input0", dev_name(&client->dev));
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500411
412 input_dev->name = "TSC2007 Touchscreen";
413 input_dev->phys = ts->phys;
414 input_dev->id.bustype = BUS_I2C;
415
416 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
417 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
Amy Maloche08f1cc12012-07-25 11:32:49 -0700418 __set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500419
Anirudh Ghayal482cccc2012-01-25 12:19:19 +0530420 input_set_abs_params(input_dev, ABS_X, ts->min_x,
421 ts->max_x, pdata->fuzzx, 0);
422 input_set_abs_params(input_dev, ABS_Y, ts->min_y,
423 ts->max_y, pdata->fuzzy, 0);
Thierry Reding891e3762011-05-17 09:32:29 -0700424 input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT,
425 pdata->fuzzz, 0);
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500426
Richard Röjforscad065f2009-07-24 23:14:17 -0700427 if (pdata->init_platform_hw)
428 pdata->init_platform_hw();
Richard Röjfors75fba3b2009-07-24 22:01:39 -0700429
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700430 err = request_irq(ts->irq, tsc2007_irq, pdata->irq_flags,
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500431 client->dev.driver->name, ts);
432 if (err < 0) {
433 dev_err(&client->dev, "irq %d busy?\n", ts->irq);
434 goto err_free_mem;
435 }
436
Richard Röjforscf5f4392009-08-04 22:34:10 -0700437 /* Prepare for touch readings - power down ADC and enable PENIRQ */
438 err = tsc2007_xfer(ts, PWRDOWN);
439 if (err < 0)
440 goto err_free_irq;
441
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500442 err = input_register_device(input_dev);
443 if (err)
444 goto err_free_irq;
445
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700446#ifdef CONFIG_HAS_EARLYSUSPEND
447 ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
448 TSC2007_SUSPEND_LEVEL;
449 ts->early_suspend.suspend = tsc2007_early_suspend;
450 ts->early_suspend.resume = tsc2007_late_resume;
451 register_early_suspend(&ts->early_suspend);
452#endif
453
Dmitry Torokhov141586bc2009-07-24 23:14:16 -0700454 i2c_set_clientdata(client, ts);
455
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500456 return 0;
457
458 err_free_irq:
Dmitry Torokhov141586bc2009-07-24 23:14:16 -0700459 tsc2007_free_irq(ts);
Richard Röjforscad065f2009-07-24 23:14:17 -0700460 if (pdata->exit_platform_hw)
461 pdata->exit_platform_hw();
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500462 err_free_mem:
463 input_free_device(input_dev);
464 kfree(ts);
465 return err;
466}
467
Dmitry Torokhov141586bc2009-07-24 23:14:16 -0700468static int __devexit tsc2007_remove(struct i2c_client *client)
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500469{
470 struct tsc2007 *ts = i2c_get_clientdata(client);
Dmitry Torokhov141586bc2009-07-24 23:14:16 -0700471 struct tsc2007_platform_data *pdata = client->dev.platform_data;
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500472
Dmitry Torokhov141586bc2009-07-24 23:14:16 -0700473 tsc2007_free_irq(ts);
Richard Röjfors75fba3b2009-07-24 22:01:39 -0700474
Richard Röjforscad065f2009-07-24 23:14:17 -0700475 if (pdata->exit_platform_hw)
476 pdata->exit_platform_hw();
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500477
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478#ifdef CONFIG_HAS_EARLYSUSPEND
479 unregister_early_suspend(&ts->early_suspend);
480#endif
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500481 input_unregister_device(ts->input);
482 kfree(ts);
483
484 return 0;
485}
486
Márton Némethce7b39a2010-01-09 23:23:02 -0800487static const struct i2c_device_id tsc2007_idtable[] = {
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500488 { "tsc2007", 0 },
489 { }
490};
491
492MODULE_DEVICE_TABLE(i2c, tsc2007_idtable);
493
494static struct i2c_driver tsc2007_driver = {
495 .driver = {
496 .owner = THIS_MODULE,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497 .name = "tsc2007",
498#ifdef CONFIG_PM
499 .pm = &tsc2007_pm_ops,
500#endif
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500501 },
502 .id_table = tsc2007_idtable,
503 .probe = tsc2007_probe,
Dmitry Torokhov141586bc2009-07-24 23:14:16 -0700504 .remove = __devexit_p(tsc2007_remove),
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500505};
506
Axel Lin1b92c1c2012-03-16 23:05:41 -0700507module_i2c_driver(tsc2007_driver);
Kwangwoo Lee50b6f1f2008-12-20 04:26:01 -0500508
509MODULE_AUTHOR("Kwangwoo Lee <kwlee@mtekvision.com>");
510MODULE_DESCRIPTION("TSC2007 TouchScreen Driver");
511MODULE_LICENSE("GPL");