Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 1 | /* |
| 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 Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 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> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame^] | 29 | #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 Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 35 | |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 36 | #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 | |
| 66 | struct ts_event { |
| 67 | u16 x; |
| 68 | u16 y; |
| 69 | u16 z1, z2; |
| 70 | }; |
| 71 | |
| 72 | struct tsc2007 { |
| 73 | struct input_dev *input; |
| 74 | char phys[32]; |
Richard Röjfors | 75fba3b | 2009-07-24 22:01:39 -0700 | [diff] [blame] | 75 | struct delayed_work work; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 76 | |
| 77 | struct i2c_client *client; |
| 78 | |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 79 | u16 model; |
| 80 | u16 x_plate_ohms; |
Thierry Reding | 84005eb | 2011-05-17 09:31:01 -0700 | [diff] [blame] | 81 | u16 max_rt; |
Thierry Reding | 2d137c7e | 2011-05-17 09:31:33 -0700 | [diff] [blame] | 82 | unsigned long poll_delay; |
Thierry Reding | 1af38ea | 2011-05-17 09:32:02 -0700 | [diff] [blame] | 83 | unsigned long poll_period; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 84 | |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 85 | bool pendown; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 86 | int irq; |
| 87 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame^] | 88 | bool invert_x; |
| 89 | bool invert_y; |
| 90 | bool invert_z1; |
| 91 | bool invert_z2; |
| 92 | |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 93 | int (*get_pendown_state)(void); |
| 94 | void (*clear_penirq)(void); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame^] | 95 | int (*power_shutdown)(bool); |
| 96 | #if defined(CONFIG_HAS_EARLYSUSPEND) |
| 97 | struct early_suspend early_suspend; |
| 98 | #endif |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | static inline int tsc2007_xfer(struct tsc2007 *tsc, u8 cmd) |
| 102 | { |
| 103 | s32 data; |
| 104 | u16 val; |
| 105 | |
| 106 | data = i2c_smbus_read_word_data(tsc->client, cmd); |
| 107 | if (data < 0) { |
| 108 | dev_err(&tsc->client->dev, "i2c io error: %d\n", data); |
| 109 | return data; |
| 110 | } |
| 111 | |
| 112 | /* The protocol and raw data format from i2c interface: |
| 113 | * S Addr Wr [A] Comm [A] S Addr Rd [A] [DataLow] A [DataHigh] NA P |
| 114 | * Where DataLow has [D11-D4], DataHigh has [D3-D0 << 4 | Dummy 4bit]. |
| 115 | */ |
| 116 | val = swab16(data) >> 4; |
| 117 | |
| 118 | dev_dbg(&tsc->client->dev, "data: 0x%x, val: 0x%x\n", data, val); |
| 119 | |
| 120 | return val; |
| 121 | } |
| 122 | |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 123 | static void tsc2007_read_values(struct tsc2007 *tsc, struct ts_event *tc) |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 124 | { |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 125 | /* y- still on; turn on only y+ (and ADC) */ |
| 126 | tc->y = tsc2007_xfer(tsc, READ_Y); |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 127 | |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 128 | /* turn y- off, x+ on, then leave in lowpower */ |
| 129 | tc->x = tsc2007_xfer(tsc, READ_X); |
| 130 | |
| 131 | /* turn y+ off, x- on; we'll use formula #1 */ |
| 132 | tc->z1 = tsc2007_xfer(tsc, READ_Z1); |
| 133 | tc->z2 = tsc2007_xfer(tsc, READ_Z2); |
| 134 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame^] | 135 | if (tsc->invert_x == true) |
| 136 | tc->x = MAX_12BIT - tc->x; |
| 137 | |
| 138 | if (tsc->invert_y == true) |
| 139 | tc->y = MAX_12BIT - tc->y; |
| 140 | |
| 141 | if (tsc->invert_z1 == true) |
| 142 | tc->z1 = MAX_12BIT - tc->z1; |
| 143 | |
| 144 | if (tsc->invert_z2 == true) |
| 145 | tc->z2 = MAX_12BIT - tc->z2; |
| 146 | |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 147 | /* Prepare for next touch reading - power down ADC, enable PENIRQ */ |
| 148 | tsc2007_xfer(tsc, PWRDOWN); |
| 149 | } |
| 150 | |
| 151 | static u32 tsc2007_calculate_pressure(struct tsc2007 *tsc, struct ts_event *tc) |
| 152 | { |
| 153 | u32 rt = 0; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 154 | |
| 155 | /* range filtering */ |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 156 | if (tc->x == MAX_12BIT) |
| 157 | tc->x = 0; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 158 | |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 159 | if (likely(tc->x && tc->z1)) { |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 160 | /* compute touch pressure resistance using equation #1 */ |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 161 | rt = tc->z2 - tc->z1; |
| 162 | rt *= tc->x; |
| 163 | rt *= tsc->x_plate_ohms; |
| 164 | rt /= tc->z1; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 165 | rt = (rt + 2047) >> 12; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 166 | } |
| 167 | |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 168 | return rt; |
| 169 | } |
| 170 | |
| 171 | static void tsc2007_send_up_event(struct tsc2007 *tsc) |
| 172 | { |
| 173 | struct input_dev *input = tsc->input; |
| 174 | |
| 175 | dev_dbg(&tsc->client->dev, "UP\n"); |
| 176 | |
| 177 | input_report_key(input, BTN_TOUCH, 0); |
| 178 | input_report_abs(input, ABS_PRESSURE, 0); |
| 179 | input_sync(input); |
| 180 | } |
| 181 | |
| 182 | static void tsc2007_work(struct work_struct *work) |
| 183 | { |
| 184 | struct tsc2007 *ts = |
| 185 | container_of(to_delayed_work(work), struct tsc2007, work); |
Thierry Reding | 4a1a42a | 2011-05-17 09:29:45 -0700 | [diff] [blame] | 186 | bool debounced = false; |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 187 | struct ts_event tc; |
| 188 | u32 rt; |
| 189 | |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 190 | /* |
| 191 | * NOTE: We can't rely on the pressure to determine the pen down |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 192 | * state, even though this controller has a pressure sensor. |
| 193 | * The pressure value can fluctuate for quite a while after |
| 194 | * lifting the pen and in some cases may not even settle at the |
| 195 | * expected value. |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 196 | * |
| 197 | * The only safe way to check for the pen up condition is in the |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 198 | * work function by reading the pen signal state (it's a GPIO |
| 199 | * and IRQ). Unfortunately such callback is not always available, |
| 200 | * in that case we have rely on the pressure anyway. |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 201 | */ |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 202 | if (ts->get_pendown_state) { |
| 203 | if (unlikely(!ts->get_pendown_state())) { |
| 204 | tsc2007_send_up_event(ts); |
| 205 | ts->pendown = false; |
| 206 | goto out; |
| 207 | } |
| 208 | |
| 209 | dev_dbg(&ts->client->dev, "pen is still down\n"); |
| 210 | } |
| 211 | |
| 212 | tsc2007_read_values(ts, &tc); |
| 213 | |
| 214 | rt = tsc2007_calculate_pressure(ts, &tc); |
Thierry Reding | 84005eb | 2011-05-17 09:31:01 -0700 | [diff] [blame] | 215 | if (rt > ts->max_rt) { |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 216 | /* |
| 217 | * Sample found inconsistent by debouncing or pressure is |
| 218 | * beyond the maximum. Don't report it to user space, |
| 219 | * repeat at least once more the measurement. |
| 220 | */ |
| 221 | dev_dbg(&ts->client->dev, "ignored pressure %d\n", rt); |
Thierry Reding | 4a1a42a | 2011-05-17 09:29:45 -0700 | [diff] [blame] | 222 | debounced = true; |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 223 | goto out; |
| 224 | |
| 225 | } |
| 226 | |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 227 | if (rt) { |
| 228 | struct input_dev *input = ts->input; |
| 229 | |
| 230 | if (!ts->pendown) { |
| 231 | dev_dbg(&ts->client->dev, "DOWN\n"); |
| 232 | |
| 233 | input_report_key(input, BTN_TOUCH, 1); |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 234 | ts->pendown = true; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 235 | } |
| 236 | |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 237 | input_report_abs(input, ABS_X, tc.x); |
| 238 | input_report_abs(input, ABS_Y, tc.y); |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 239 | input_report_abs(input, ABS_PRESSURE, rt); |
| 240 | |
| 241 | input_sync(input); |
| 242 | |
| 243 | dev_dbg(&ts->client->dev, "point(%4d,%4d), pressure (%4u)\n", |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 244 | tc.x, tc.y, rt); |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 245 | |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 246 | } else if (!ts->get_pendown_state && ts->pendown) { |
| 247 | /* |
| 248 | * We don't have callback to check pendown state, so we |
| 249 | * have to assume that since pressure reported is 0 the |
| 250 | * pen was lifted up. |
| 251 | */ |
| 252 | tsc2007_send_up_event(ts); |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 253 | ts->pendown = false; |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 254 | } |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 255 | |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 256 | out: |
Thierry Reding | 4a1a42a | 2011-05-17 09:29:45 -0700 | [diff] [blame] | 257 | if (ts->pendown || debounced) |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 258 | schedule_delayed_work(&ts->work, |
Thierry Reding | 1af38ea | 2011-05-17 09:32:02 -0700 | [diff] [blame] | 259 | msecs_to_jiffies(ts->poll_period)); |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 260 | else |
| 261 | enable_irq(ts->irq); |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | static irqreturn_t tsc2007_irq(int irq, void *handle) |
| 265 | { |
| 266 | struct tsc2007 *ts = handle; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 267 | |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 268 | if (!ts->get_pendown_state || likely(ts->get_pendown_state())) { |
Ben Nizette | 29fa98b | 2009-04-17 20:35:57 -0700 | [diff] [blame] | 269 | disable_irq_nosync(ts->irq); |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 270 | schedule_delayed_work(&ts->work, |
Thierry Reding | 2d137c7e | 2011-05-17 09:31:33 -0700 | [diff] [blame] | 271 | msecs_to_jiffies(ts->poll_delay)); |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | if (ts->clear_penirq) |
| 275 | ts->clear_penirq(); |
| 276 | |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 277 | return IRQ_HANDLED; |
| 278 | } |
| 279 | |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 280 | static void tsc2007_free_irq(struct tsc2007 *ts) |
| 281 | { |
| 282 | free_irq(ts->irq, ts); |
| 283 | if (cancel_delayed_work_sync(&ts->work)) { |
| 284 | /* |
| 285 | * Work was pending, therefore we need to enable |
| 286 | * IRQ here to balance the disable_irq() done in the |
| 287 | * interrupt handler. |
| 288 | */ |
| 289 | enable_irq(ts->irq); |
| 290 | } |
| 291 | } |
| 292 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame^] | 293 | #ifdef CONFIG_PM |
| 294 | static int tsc2007_suspend(struct device *dev) |
| 295 | { |
| 296 | int rc; |
| 297 | struct tsc2007 *ts = dev_get_drvdata(dev); |
| 298 | |
| 299 | disable_irq(ts->irq); |
| 300 | |
| 301 | if (cancel_delayed_work_sync(&ts->work)) |
| 302 | enable_irq(ts->irq); |
| 303 | |
| 304 | if (ts->power_shutdown) { |
| 305 | rc = ts->power_shutdown(true); |
| 306 | if (rc) { |
| 307 | pr_err("%s: Power off failed, suspend failed (%d)\n", |
| 308 | __func__, rc); |
| 309 | return rc; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | static int tsc2007_resume(struct device *dev) |
| 317 | { |
| 318 | int rc; |
| 319 | struct tsc2007 *ts = dev_get_drvdata(dev); |
| 320 | |
| 321 | if (ts->power_shutdown) { |
| 322 | rc = ts->power_shutdown(false); |
| 323 | if (rc) { |
| 324 | pr_err("%s: Power on failed, resume failed (%d)\n", |
| 325 | __func__, rc); |
| 326 | return rc; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | enable_irq(ts->irq); |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 336 | static void tsc2007_early_suspend(struct early_suspend *h) |
| 337 | { |
| 338 | struct tsc2007 *ts = container_of(h, struct tsc2007, early_suspend); |
| 339 | |
| 340 | tsc2007_suspend(&ts->client->dev); |
| 341 | } |
| 342 | |
| 343 | static void tsc2007_late_resume(struct early_suspend *h) |
| 344 | { |
| 345 | struct tsc2007 *ts = container_of(h, struct tsc2007, early_suspend); |
| 346 | |
| 347 | tsc2007_resume(&ts->client->dev); |
| 348 | } |
| 349 | #endif |
| 350 | |
| 351 | static const struct dev_pm_ops tsc2007_pm_ops = { |
| 352 | #ifndef CONFIG_HAS_EARLYSUSPEND |
| 353 | .suspend = tsc2007_suspend, |
| 354 | .resume = tsc2007_resume, |
| 355 | #endif |
| 356 | }; |
| 357 | #endif |
| 358 | |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 359 | static int __devinit tsc2007_probe(struct i2c_client *client, |
| 360 | const struct i2c_device_id *id) |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 361 | { |
| 362 | struct tsc2007 *ts; |
Axel Lin | b496acb | 2010-09-02 19:52:41 -0700 | [diff] [blame] | 363 | struct tsc2007_platform_data *pdata = client->dev.platform_data; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 364 | struct input_dev *input_dev; |
| 365 | int err; |
| 366 | |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 367 | if (!pdata) { |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 368 | dev_err(&client->dev, "platform data is required!\n"); |
| 369 | return -EINVAL; |
| 370 | } |
| 371 | |
| 372 | if (!i2c_check_functionality(client->adapter, |
| 373 | I2C_FUNC_SMBUS_READ_WORD_DATA)) |
| 374 | return -EIO; |
| 375 | |
| 376 | ts = kzalloc(sizeof(struct tsc2007), GFP_KERNEL); |
| 377 | input_dev = input_allocate_device(); |
| 378 | if (!ts || !input_dev) { |
| 379 | err = -ENOMEM; |
| 380 | goto err_free_mem; |
| 381 | } |
| 382 | |
| 383 | ts->client = client; |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 384 | ts->irq = client->irq; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 385 | ts->input = input_dev; |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 386 | INIT_DELAYED_WORK(&ts->work, tsc2007_work); |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 387 | |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 388 | ts->model = pdata->model; |
| 389 | ts->x_plate_ohms = pdata->x_plate_ohms; |
Thierry Reding | 84005eb | 2011-05-17 09:31:01 -0700 | [diff] [blame] | 390 | ts->max_rt = pdata->max_rt ? : MAX_12BIT; |
Thierry Reding | 2d137c7e | 2011-05-17 09:31:33 -0700 | [diff] [blame] | 391 | ts->poll_delay = pdata->poll_delay ? : 1; |
Thierry Reding | 1af38ea | 2011-05-17 09:32:02 -0700 | [diff] [blame] | 392 | ts->poll_period = pdata->poll_period ? : 1; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 393 | ts->get_pendown_state = pdata->get_pendown_state; |
| 394 | ts->clear_penirq = pdata->clear_penirq; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame^] | 395 | ts->invert_x = pdata->invert_x; |
| 396 | ts->invert_y = pdata->invert_y; |
| 397 | ts->invert_z1 = pdata->invert_z1; |
| 398 | ts->invert_z2 = pdata->invert_z2; |
| 399 | ts->power_shutdown = pdata->power_shutdown; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 400 | |
Kay Sievers | 4e8718a | 2009-01-29 22:56:08 -0800 | [diff] [blame] | 401 | snprintf(ts->phys, sizeof(ts->phys), |
| 402 | "%s/input0", dev_name(&client->dev)); |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 403 | |
| 404 | input_dev->name = "TSC2007 Touchscreen"; |
| 405 | input_dev->phys = ts->phys; |
| 406 | input_dev->id.bustype = BUS_I2C; |
| 407 | |
| 408 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); |
| 409 | input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH); |
| 410 | |
Thierry Reding | 891e376 | 2011-05-17 09:32:29 -0700 | [diff] [blame] | 411 | input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, pdata->fuzzx, 0); |
| 412 | input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, pdata->fuzzy, 0); |
| 413 | input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT, |
| 414 | pdata->fuzzz, 0); |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 415 | |
Richard Röjfors | cad065f | 2009-07-24 23:14:17 -0700 | [diff] [blame] | 416 | if (pdata->init_platform_hw) |
| 417 | pdata->init_platform_hw(); |
Richard Röjfors | 75fba3b | 2009-07-24 22:01:39 -0700 | [diff] [blame] | 418 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame^] | 419 | err = request_irq(ts->irq, tsc2007_irq, pdata->irq_flags, |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 420 | client->dev.driver->name, ts); |
| 421 | if (err < 0) { |
| 422 | dev_err(&client->dev, "irq %d busy?\n", ts->irq); |
| 423 | goto err_free_mem; |
| 424 | } |
| 425 | |
Richard Röjfors | cf5f439 | 2009-08-04 22:34:10 -0700 | [diff] [blame] | 426 | /* Prepare for touch readings - power down ADC and enable PENIRQ */ |
| 427 | err = tsc2007_xfer(ts, PWRDOWN); |
| 428 | if (err < 0) |
| 429 | goto err_free_irq; |
| 430 | |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 431 | err = input_register_device(input_dev); |
| 432 | if (err) |
| 433 | goto err_free_irq; |
| 434 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame^] | 435 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 436 | ts->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + |
| 437 | TSC2007_SUSPEND_LEVEL; |
| 438 | ts->early_suspend.suspend = tsc2007_early_suspend; |
| 439 | ts->early_suspend.resume = tsc2007_late_resume; |
| 440 | register_early_suspend(&ts->early_suspend); |
| 441 | #endif |
| 442 | |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 443 | i2c_set_clientdata(client, ts); |
| 444 | |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 445 | return 0; |
| 446 | |
| 447 | err_free_irq: |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 448 | tsc2007_free_irq(ts); |
Richard Röjfors | cad065f | 2009-07-24 23:14:17 -0700 | [diff] [blame] | 449 | if (pdata->exit_platform_hw) |
| 450 | pdata->exit_platform_hw(); |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 451 | err_free_mem: |
| 452 | input_free_device(input_dev); |
| 453 | kfree(ts); |
| 454 | return err; |
| 455 | } |
| 456 | |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 457 | static int __devexit tsc2007_remove(struct i2c_client *client) |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 458 | { |
| 459 | struct tsc2007 *ts = i2c_get_clientdata(client); |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 460 | struct tsc2007_platform_data *pdata = client->dev.platform_data; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 461 | |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 462 | tsc2007_free_irq(ts); |
Richard Röjfors | 75fba3b | 2009-07-24 22:01:39 -0700 | [diff] [blame] | 463 | |
Richard Röjfors | cad065f | 2009-07-24 23:14:17 -0700 | [diff] [blame] | 464 | if (pdata->exit_platform_hw) |
| 465 | pdata->exit_platform_hw(); |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 466 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame^] | 467 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 468 | unregister_early_suspend(&ts->early_suspend); |
| 469 | #endif |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 470 | input_unregister_device(ts->input); |
| 471 | kfree(ts); |
| 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | |
Márton Németh | ce7b39a | 2010-01-09 23:23:02 -0800 | [diff] [blame] | 476 | static const struct i2c_device_id tsc2007_idtable[] = { |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 477 | { "tsc2007", 0 }, |
| 478 | { } |
| 479 | }; |
| 480 | |
| 481 | MODULE_DEVICE_TABLE(i2c, tsc2007_idtable); |
| 482 | |
| 483 | static struct i2c_driver tsc2007_driver = { |
| 484 | .driver = { |
| 485 | .owner = THIS_MODULE, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame^] | 486 | .name = "tsc2007", |
| 487 | #ifdef CONFIG_PM |
| 488 | .pm = &tsc2007_pm_ops, |
| 489 | #endif |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 490 | }, |
| 491 | .id_table = tsc2007_idtable, |
| 492 | .probe = tsc2007_probe, |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 493 | .remove = __devexit_p(tsc2007_remove), |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 494 | }; |
| 495 | |
| 496 | static int __init tsc2007_init(void) |
| 497 | { |
| 498 | return i2c_add_driver(&tsc2007_driver); |
| 499 | } |
| 500 | |
| 501 | static void __exit tsc2007_exit(void) |
| 502 | { |
| 503 | i2c_del_driver(&tsc2007_driver); |
| 504 | } |
| 505 | |
| 506 | module_init(tsc2007_init); |
| 507 | module_exit(tsc2007_exit); |
| 508 | |
| 509 | MODULE_AUTHOR("Kwangwoo Lee <kwlee@mtekvision.com>"); |
| 510 | MODULE_DESCRIPTION("TSC2007 TouchScreen Driver"); |
| 511 | MODULE_LICENSE("GPL"); |