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; |
Anirudh Ghayal | 482cccc | 2012-01-25 12:19:19 +0530 | [diff] [blame] | 84 | u16 min_x; |
| 85 | u16 max_x; |
| 86 | u16 min_y; |
| 87 | u16 max_y; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 88 | |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 89 | bool pendown; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 90 | int irq; |
| 91 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 92 | bool invert_x; |
| 93 | bool invert_y; |
| 94 | bool invert_z1; |
| 95 | bool invert_z2; |
Dmitry Torokhov | 377dc55 | 2011-08-25 00:25:12 -0700 | [diff] [blame] | 96 | |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 97 | int (*get_pendown_state)(void); |
| 98 | void (*clear_penirq)(void); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 99 | int (*power_shutdown)(bool); |
| 100 | #if defined(CONFIG_HAS_EARLYSUSPEND) |
| 101 | struct early_suspend early_suspend; |
| 102 | #endif |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | static 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 Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 127 | static void tsc2007_read_values(struct tsc2007 *tsc, struct ts_event *tc) |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 128 | { |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 129 | /* y- still on; turn on only y+ (and ADC) */ |
| 130 | tc->y = tsc2007_xfer(tsc, READ_Y); |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 131 | |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 132 | /* 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 139 | 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 Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 151 | /* Prepare for next touch reading - power down ADC, enable PENIRQ */ |
| 152 | tsc2007_xfer(tsc, PWRDOWN); |
| 153 | } |
| 154 | |
| 155 | static u32 tsc2007_calculate_pressure(struct tsc2007 *tsc, struct ts_event *tc) |
| 156 | { |
| 157 | u32 rt = 0; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 158 | |
| 159 | /* range filtering */ |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 160 | if (tc->x == MAX_12BIT) |
| 161 | tc->x = 0; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 162 | |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 163 | if (likely(tc->x && tc->z1)) { |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 164 | /* compute touch pressure resistance using equation #1 */ |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 165 | rt = tc->z2 - tc->z1; |
| 166 | rt *= tc->x; |
| 167 | rt *= tsc->x_plate_ohms; |
| 168 | rt /= tc->z1; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 169 | rt = (rt + 2047) >> 12; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 170 | } |
| 171 | |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 172 | return rt; |
| 173 | } |
| 174 | |
| 175 | static 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 | |
| 186 | static void tsc2007_work(struct work_struct *work) |
| 187 | { |
| 188 | struct tsc2007 *ts = |
| 189 | container_of(to_delayed_work(work), struct tsc2007, work); |
Thierry Reding | 4a1a42a | 2011-05-17 09:29:45 -0700 | [diff] [blame] | 190 | bool debounced = false; |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 191 | struct ts_event tc; |
| 192 | u32 rt; |
| 193 | |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 194 | /* |
| 195 | * 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] | 196 | * 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 Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 200 | * |
| 201 | * 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] | 202 | * 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 Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 205 | */ |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 206 | 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 Torokhov | 377dc55 | 2011-08-25 00:25:12 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 213 | dev_dbg(&ts->client->dev, "pen is still down\n"); |
Dmitry Torokhov | 377dc55 | 2011-08-25 00:25:12 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 216 | tsc2007_read_values(ts, &tc); |
Dmitry Torokhov | 377dc55 | 2011-08-25 00:25:12 -0700 | [diff] [blame] | 217 | |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 218 | rt = tsc2007_calculate_pressure(ts, &tc); |
Thierry Reding | 84005eb | 2011-05-17 09:31:01 -0700 | [diff] [blame] | 219 | if (rt > ts->max_rt) { |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 220 | /* |
| 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 Reding | 4a1a42a | 2011-05-17 09:29:45 -0700 | [diff] [blame] | 226 | debounced = true; |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 227 | goto out; |
Dmitry Torokhov | 377dc55 | 2011-08-25 00:25:12 -0700 | [diff] [blame] | 228 | |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 229 | } |
Dmitry Torokhov | 377dc55 | 2011-08-25 00:25:12 -0700 | [diff] [blame] | 230 | |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 231 | 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 Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 238 | ts->pendown = true; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 239 | } |
| 240 | |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 241 | input_report_abs(input, ABS_X, tc.x); |
| 242 | input_report_abs(input, ABS_Y, tc.y); |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 243 | 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 Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 248 | tc.x, tc.y, rt); |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 249 | |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 250 | } 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 Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 257 | ts->pendown = false; |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 258 | } |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 259 | |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 260 | out: |
Thierry Reding | 4a1a42a | 2011-05-17 09:29:45 -0700 | [diff] [blame] | 261 | if (ts->pendown || debounced) |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 262 | schedule_delayed_work(&ts->work, |
Thierry Reding | 1af38ea | 2011-05-17 09:32:02 -0700 | [diff] [blame] | 263 | msecs_to_jiffies(ts->poll_period)); |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 264 | else |
| 265 | enable_irq(ts->irq); |
Dmitry Torokhov | 377dc55 | 2011-08-25 00:25:12 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 268 | static irqreturn_t tsc2007_irq(int irq, void *handle) |
| 269 | { |
| 270 | struct tsc2007 *ts = handle; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 271 | |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 272 | if (!ts->get_pendown_state || likely(ts->get_pendown_state())) { |
Ben Nizette | 29fa98b | 2009-04-17 20:35:57 -0700 | [diff] [blame] | 273 | disable_irq_nosync(ts->irq); |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 274 | schedule_delayed_work(&ts->work, |
Thierry Reding | 2d137c7e | 2011-05-17 09:31:33 -0700 | [diff] [blame] | 275 | msecs_to_jiffies(ts->poll_delay)); |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | if (ts->clear_penirq) |
| 279 | ts->clear_penirq(); |
| 280 | |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 281 | return IRQ_HANDLED; |
| 282 | } |
| 283 | |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 284 | static 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 Torokhov | d3654d7 | 2011-08-25 01:05:46 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 297 | #ifdef CONFIG_PM |
| 298 | static int tsc2007_suspend(struct device *dev) |
Dmitry Torokhov | d3654d7 | 2011-08-25 01:05:46 -0700 | [diff] [blame] | 299 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 300 | int rc; |
| 301 | struct tsc2007 *ts = dev_get_drvdata(dev); |
Dmitry Torokhov | d3654d7 | 2011-08-25 01:05:46 -0700 | [diff] [blame] | 302 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 303 | disable_irq(ts->irq); |
Dmitry Torokhov | d3654d7 | 2011-08-25 01:05:46 -0700 | [diff] [blame] | 304 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 305 | if (cancel_delayed_work_sync(&ts->work)) |
| 306 | enable_irq(ts->irq); |
Dmitry Torokhov | d3654d7 | 2011-08-25 01:05:46 -0700 | [diff] [blame] | 307 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 308 | 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 Torokhov | d3654d7 | 2011-08-25 01:05:46 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 320 | static int tsc2007_resume(struct device *dev) |
Dmitry Torokhov | d3654d7 | 2011-08-25 01:05:46 -0700 | [diff] [blame] | 321 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 322 | int rc; |
| 323 | struct tsc2007 *ts = dev_get_drvdata(dev); |
Dmitry Torokhov | d3654d7 | 2011-08-25 01:05:46 -0700 | [diff] [blame] | 324 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 325 | 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 Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 339 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 340 | static 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 | |
| 347 | static 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 | |
| 355 | static 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 Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 363 | static int __devinit tsc2007_probe(struct i2c_client *client, |
| 364 | const struct i2c_device_id *id) |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 365 | { |
| 366 | struct tsc2007 *ts; |
Axel Lin | b496acb | 2010-09-02 19:52:41 -0700 | [diff] [blame] | 367 | struct tsc2007_platform_data *pdata = client->dev.platform_data; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 368 | struct input_dev *input_dev; |
| 369 | int err; |
| 370 | |
Dmitry Torokhov | d570e9e | 2009-08-04 22:07:26 -0700 | [diff] [blame] | 371 | if (!pdata) { |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 372 | 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 Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 388 | ts->irq = client->irq; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 389 | ts->input = input_dev; |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 390 | INIT_DELAYED_WORK(&ts->work, tsc2007_work); |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 391 | |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 392 | ts->model = pdata->model; |
| 393 | ts->x_plate_ohms = pdata->x_plate_ohms; |
Thierry Reding | 84005eb | 2011-05-17 09:31:01 -0700 | [diff] [blame] | 394 | ts->max_rt = pdata->max_rt ? : MAX_12BIT; |
Thierry Reding | 2d137c7e | 2011-05-17 09:31:33 -0700 | [diff] [blame] | 395 | ts->poll_delay = pdata->poll_delay ? : 1; |
Thierry Reding | 1af38ea | 2011-05-17 09:32:02 -0700 | [diff] [blame] | 396 | ts->poll_period = pdata->poll_period ? : 1; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 397 | ts->get_pendown_state = pdata->get_pendown_state; |
| 398 | ts->clear_penirq = pdata->clear_penirq; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 399 | 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 Ghayal | 482cccc | 2012-01-25 12:19:19 +0530 | [diff] [blame] | 403 | 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 407 | ts->power_shutdown = pdata->power_shutdown; |
Philip Rakity | 341deef | 2011-10-11 20:54:55 -0700 | [diff] [blame] | 408 | |
Kay Sievers | 4e8718a | 2009-01-29 22:56:08 -0800 | [diff] [blame] | 409 | snprintf(ts->phys, sizeof(ts->phys), |
| 410 | "%s/input0", dev_name(&client->dev)); |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 411 | |
| 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 Maloche | 08f1cc1 | 2012-07-25 11:32:49 -0700 | [diff] [blame] | 418 | __set_bit(INPUT_PROP_DIRECT, input_dev->propbit); |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 419 | |
Anirudh Ghayal | 482cccc | 2012-01-25 12:19:19 +0530 | [diff] [blame] | 420 | 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 Reding | 891e376 | 2011-05-17 09:32:29 -0700 | [diff] [blame] | 424 | input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT, |
| 425 | pdata->fuzzz, 0); |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 426 | |
Richard Röjfors | cad065f | 2009-07-24 23:14:17 -0700 | [diff] [blame] | 427 | if (pdata->init_platform_hw) |
| 428 | pdata->init_platform_hw(); |
Richard Röjfors | 75fba3b | 2009-07-24 22:01:39 -0700 | [diff] [blame] | 429 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 430 | err = request_irq(ts->irq, tsc2007_irq, pdata->irq_flags, |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 431 | 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öjfors | cf5f439 | 2009-08-04 22:34:10 -0700 | [diff] [blame] | 437 | /* 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 Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 442 | err = input_register_device(input_dev); |
| 443 | if (err) |
| 444 | goto err_free_irq; |
| 445 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 446 | #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 Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 454 | i2c_set_clientdata(client, ts); |
| 455 | |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 456 | return 0; |
| 457 | |
| 458 | err_free_irq: |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 459 | tsc2007_free_irq(ts); |
Richard Röjfors | cad065f | 2009-07-24 23:14:17 -0700 | [diff] [blame] | 460 | if (pdata->exit_platform_hw) |
| 461 | pdata->exit_platform_hw(); |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 462 | err_free_mem: |
| 463 | input_free_device(input_dev); |
| 464 | kfree(ts); |
| 465 | return err; |
| 466 | } |
| 467 | |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 468 | static int __devexit tsc2007_remove(struct i2c_client *client) |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 469 | { |
| 470 | struct tsc2007 *ts = i2c_get_clientdata(client); |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 471 | struct tsc2007_platform_data *pdata = client->dev.platform_data; |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 472 | |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 473 | tsc2007_free_irq(ts); |
Richard Röjfors | 75fba3b | 2009-07-24 22:01:39 -0700 | [diff] [blame] | 474 | |
Richard Röjfors | cad065f | 2009-07-24 23:14:17 -0700 | [diff] [blame] | 475 | if (pdata->exit_platform_hw) |
| 476 | pdata->exit_platform_hw(); |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 477 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 478 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 479 | unregister_early_suspend(&ts->early_suspend); |
| 480 | #endif |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 481 | input_unregister_device(ts->input); |
| 482 | kfree(ts); |
| 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | |
Márton Németh | ce7b39a | 2010-01-09 23:23:02 -0800 | [diff] [blame] | 487 | static const struct i2c_device_id tsc2007_idtable[] = { |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 488 | { "tsc2007", 0 }, |
| 489 | { } |
| 490 | }; |
| 491 | |
| 492 | MODULE_DEVICE_TABLE(i2c, tsc2007_idtable); |
| 493 | |
| 494 | static struct i2c_driver tsc2007_driver = { |
| 495 | .driver = { |
| 496 | .owner = THIS_MODULE, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 497 | .name = "tsc2007", |
| 498 | #ifdef CONFIG_PM |
| 499 | .pm = &tsc2007_pm_ops, |
| 500 | #endif |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 501 | }, |
| 502 | .id_table = tsc2007_idtable, |
| 503 | .probe = tsc2007_probe, |
Dmitry Torokhov | 141586bc | 2009-07-24 23:14:16 -0700 | [diff] [blame] | 504 | .remove = __devexit_p(tsc2007_remove), |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 505 | }; |
| 506 | |
Axel Lin | 1b92c1c | 2012-03-16 23:05:41 -0700 | [diff] [blame] | 507 | module_i2c_driver(tsc2007_driver); |
Kwangwoo Lee | 50b6f1f | 2008-12-20 04:26:01 -0500 | [diff] [blame] | 508 | |
| 509 | MODULE_AUTHOR("Kwangwoo Lee <kwlee@mtekvision.com>"); |
| 510 | MODULE_DESCRIPTION("TSC2007 TouchScreen Driver"); |
| 511 | MODULE_LICENSE("GPL"); |