David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * ADS7846 based touchscreen and sensor driver |
| 3 | * |
| 4 | * Copyright (c) 2005 David Brownell |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 5 | * Copyright (c) 2006 Nokia Corporation |
| 6 | * Various changes: Imre Deak <imre.deak@nokia.com> |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 7 | * |
| 8 | * Using code from: |
| 9 | * - corgi_ts.c |
| 10 | * Copyright (C) 2004-2005 Richard Purdie |
| 11 | * - omap_ts.[hc], ads7846.h, ts_osk.c |
| 12 | * Copyright (C) 2002 MontaVista Software |
| 13 | * Copyright (C) 2004 Texas Instruments |
| 14 | * Copyright (C) 2005 Dirk Behme |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or modify |
| 17 | * it under the terms of the GNU General Public License version 2 as |
| 18 | * published by the Free Software Foundation. |
| 19 | */ |
| 20 | #include <linux/device.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/input.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/spi/spi.h> |
| 27 | #include <linux/spi/ads7846.h> |
Andrew Morton | 3ac8bf0 | 2006-03-26 01:37:33 -0800 | [diff] [blame] | 28 | #include <asm/irq.h> |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 29 | |
| 30 | #ifdef CONFIG_ARM |
| 31 | #include <asm/mach-types.h> |
| 32 | #ifdef CONFIG_ARCH_OMAP |
| 33 | #include <asm/arch/gpio.h> |
| 34 | #endif |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 35 | #endif |
| 36 | |
| 37 | |
| 38 | /* |
David Brownell | 9084533 | 2006-05-25 18:44:20 -0700 | [diff] [blame] | 39 | * This code has been heavily tested on a Nokia 770, and lightly |
| 40 | * tested on other ads7846 devices (OSK/Mistral, Lubbock). |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 41 | * Support for ads7843 and ads7845 has only been stubbed in. |
| 42 | * |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 43 | * IRQ handling needs a workaround because of a shortcoming in handling |
| 44 | * edge triggered IRQs on some platforms like the OMAP1/2. These |
| 45 | * platforms don't handle the ARM lazy IRQ disabling properly, thus we |
| 46 | * have to maintain our own SW IRQ disabled status. This should be |
| 47 | * removed as soon as the affected platform's IRQ handling is fixed. |
| 48 | * |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 49 | * app note sbaa036 talks in more detail about accurate sampling... |
| 50 | * that ought to help in situations like LCDs inducing noise (which |
| 51 | * can also be helped by using synch signals) and more generally. |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 52 | * This driver tries to utilize the measures described in the app |
| 53 | * note. The strength of filtering can be set in the board-* specific |
| 54 | * files. |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 55 | */ |
| 56 | |
| 57 | #define TS_POLL_PERIOD msecs_to_jiffies(10) |
| 58 | |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 59 | /* this driver doesn't aim at the peak continuous sample rate */ |
| 60 | #define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */) |
| 61 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 62 | struct ts_event { |
| 63 | /* For portability, we can't read 12 bit values using SPI (which |
| 64 | * would make the controller deliver them as native byteorder u16 |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 65 | * with msbs zeroed). Instead, we read them as two 8-bit values, |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 66 | * which need byteswapping then range adjustment. |
| 67 | */ |
| 68 | __be16 x; |
| 69 | __be16 y; |
| 70 | __be16 z1, z2; |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 71 | int ignore; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | struct ads7846 { |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 75 | struct input_dev *input; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 76 | char phys[32]; |
| 77 | |
| 78 | struct spi_device *spi; |
| 79 | u16 model; |
| 80 | u16 vref_delay_usecs; |
| 81 | u16 x_plate_ohms; |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 82 | u16 pressure_max; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 83 | |
Imre Deak | 53a0ef89 | 2006-04-11 23:41:49 -0400 | [diff] [blame] | 84 | u8 read_x, read_y, read_z1, read_z2, pwrdown; |
| 85 | u16 dummy; /* for the pwrdown read */ |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 86 | struct ts_event tc; |
| 87 | |
Imre Deak | 53a0ef89 | 2006-04-11 23:41:49 -0400 | [diff] [blame] | 88 | struct spi_transfer xfer[10]; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 89 | struct spi_message msg[5]; |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 90 | struct spi_message *last_msg; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 91 | int msg_idx; |
| 92 | int read_cnt; |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 93 | int read_rep; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 94 | int last_read; |
| 95 | |
| 96 | u16 debounce_max; |
| 97 | u16 debounce_tol; |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 98 | u16 debounce_rep; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 99 | |
| 100 | spinlock_t lock; |
| 101 | struct timer_list timer; /* P: lock */ |
| 102 | unsigned pendown:1; /* P: lock */ |
| 103 | unsigned pending:1; /* P: lock */ |
| 104 | // FIXME remove "irq_disabled" |
| 105 | unsigned irq_disabled:1; /* P: lock */ |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 106 | unsigned disabled:1; |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 107 | |
| 108 | int (*get_pendown_state)(void); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | /* leave chip selected when we're done, for quicker re-select? */ |
| 112 | #if 0 |
| 113 | #define CS_CHANGE(xfer) ((xfer).cs_change = 1) |
| 114 | #else |
| 115 | #define CS_CHANGE(xfer) ((xfer).cs_change = 0) |
| 116 | #endif |
| 117 | |
| 118 | /*--------------------------------------------------------------------------*/ |
| 119 | |
| 120 | /* The ADS7846 has touchscreen and other sensors. |
| 121 | * Earlier ads784x chips are somewhat compatible. |
| 122 | */ |
| 123 | #define ADS_START (1 << 7) |
| 124 | #define ADS_A2A1A0_d_y (1 << 4) /* differential */ |
| 125 | #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */ |
| 126 | #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */ |
| 127 | #define ADS_A2A1A0_d_x (5 << 4) /* differential */ |
| 128 | #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */ |
| 129 | #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */ |
| 130 | #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */ |
| 131 | #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */ |
| 132 | #define ADS_8_BIT (1 << 3) |
| 133 | #define ADS_12_BIT (0 << 3) |
| 134 | #define ADS_SER (1 << 2) /* non-differential */ |
| 135 | #define ADS_DFR (0 << 2) /* differential */ |
| 136 | #define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */ |
| 137 | #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */ |
| 138 | #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */ |
| 139 | #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */ |
| 140 | |
| 141 | #define MAX_12BIT ((1<<12)-1) |
| 142 | |
| 143 | /* leave ADC powered up (disables penirq) between differential samples */ |
| 144 | #define READ_12BIT_DFR(x) (ADS_START | ADS_A2A1A0_d_ ## x \ |
| 145 | | ADS_12_BIT | ADS_DFR) |
| 146 | |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 147 | #define READ_Y (READ_12BIT_DFR(y) | ADS_PD10_ADC_ON) |
| 148 | #define READ_Z1 (READ_12BIT_DFR(z1) | ADS_PD10_ADC_ON) |
| 149 | #define READ_Z2 (READ_12BIT_DFR(z2) | ADS_PD10_ADC_ON) |
Imre Deak | 53a0ef89 | 2006-04-11 23:41:49 -0400 | [diff] [blame] | 150 | |
| 151 | #define READ_X (READ_12BIT_DFR(x) | ADS_PD10_ADC_ON) |
| 152 | #define PWRDOWN (READ_12BIT_DFR(y) | ADS_PD10_PDOWN) /* LAST */ |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 153 | |
| 154 | /* single-ended samples need to first power up reference voltage; |
| 155 | * we leave both ADC and VREF powered |
| 156 | */ |
| 157 | #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \ |
| 158 | | ADS_12_BIT | ADS_SER) |
| 159 | |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 160 | #define REF_ON (READ_12BIT_DFR(x) | ADS_PD10_ALL_ON) |
| 161 | #define REF_OFF (READ_12BIT_DFR(y) | ADS_PD10_PDOWN) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 162 | |
| 163 | /*--------------------------------------------------------------------------*/ |
| 164 | |
| 165 | /* |
| 166 | * Non-touchscreen sensors only use single-ended conversions. |
| 167 | */ |
| 168 | |
| 169 | struct ser_req { |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 170 | u8 ref_on; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 171 | u8 command; |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 172 | u8 ref_off; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 173 | u16 scratch; |
| 174 | __be16 sample; |
| 175 | struct spi_message msg; |
| 176 | struct spi_transfer xfer[6]; |
| 177 | }; |
| 178 | |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 179 | static void ads7846_enable(struct ads7846 *ts); |
| 180 | static void ads7846_disable(struct ads7846 *ts); |
| 181 | |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 182 | static int device_suspended(struct device *dev) |
| 183 | { |
| 184 | struct ads7846 *ts = dev_get_drvdata(dev); |
| 185 | return dev->power.power_state.event != PM_EVENT_ON || ts->disabled; |
| 186 | } |
| 187 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 188 | static int ads7846_read12_ser(struct device *dev, unsigned command) |
| 189 | { |
| 190 | struct spi_device *spi = to_spi_device(dev); |
| 191 | struct ads7846 *ts = dev_get_drvdata(dev); |
| 192 | struct ser_req *req = kzalloc(sizeof *req, SLAB_KERNEL); |
| 193 | int status; |
| 194 | int sample; |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 195 | int i; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 196 | |
| 197 | if (!req) |
| 198 | return -ENOMEM; |
| 199 | |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 200 | spi_message_init(&req->msg); |
Vitaly Wool | 8275c64 | 2006-01-08 13:34:28 -0800 | [diff] [blame] | 201 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 202 | /* activate reference, so it has time to settle; */ |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 203 | req->ref_on = REF_ON; |
| 204 | req->xfer[0].tx_buf = &req->ref_on; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 205 | req->xfer[0].len = 1; |
| 206 | req->xfer[1].rx_buf = &req->scratch; |
| 207 | req->xfer[1].len = 2; |
| 208 | |
| 209 | /* |
| 210 | * for external VREF, 0 usec (and assume it's always on); |
| 211 | * for 1uF, use 800 usec; |
| 212 | * no cap, 100 usec. |
| 213 | */ |
| 214 | req->xfer[1].delay_usecs = ts->vref_delay_usecs; |
| 215 | |
| 216 | /* take sample */ |
| 217 | req->command = (u8) command; |
| 218 | req->xfer[2].tx_buf = &req->command; |
| 219 | req->xfer[2].len = 1; |
| 220 | req->xfer[3].rx_buf = &req->sample; |
| 221 | req->xfer[3].len = 2; |
| 222 | |
| 223 | /* REVISIT: take a few more samples, and compare ... */ |
| 224 | |
| 225 | /* turn off reference */ |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 226 | req->ref_off = REF_OFF; |
| 227 | req->xfer[4].tx_buf = &req->ref_off; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 228 | req->xfer[4].len = 1; |
| 229 | req->xfer[5].rx_buf = &req->scratch; |
| 230 | req->xfer[5].len = 2; |
| 231 | |
| 232 | CS_CHANGE(req->xfer[5]); |
| 233 | |
| 234 | /* group all the transfers together, so we can't interfere with |
| 235 | * reading touchscreen state; disable penirq while sampling |
| 236 | */ |
Vitaly Wool | 8275c64 | 2006-01-08 13:34:28 -0800 | [diff] [blame] | 237 | for (i = 0; i < 6; i++) |
| 238 | spi_message_add_tail(&req->xfer[i], &req->msg); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 239 | |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 240 | ts->irq_disabled = 1; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 241 | disable_irq(spi->irq); |
| 242 | status = spi_sync(spi, &req->msg); |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 243 | ts->irq_disabled = 0; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 244 | enable_irq(spi->irq); |
| 245 | |
| 246 | if (req->msg.status) |
| 247 | status = req->msg.status; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 248 | |
David Brownell | 9084533 | 2006-05-25 18:44:20 -0700 | [diff] [blame] | 249 | /* on-wire is a must-ignore bit, a BE12 value, then padding */ |
| 250 | sample = be16_to_cpu(req->sample); |
| 251 | sample = sample >> 3; |
| 252 | sample &= 0x0fff; |
| 253 | |
| 254 | kfree(req); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 255 | return status ? status : sample; |
| 256 | } |
| 257 | |
| 258 | #define SHOW(name) static ssize_t \ |
| 259 | name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \ |
| 260 | { \ |
| 261 | ssize_t v = ads7846_read12_ser(dev, \ |
| 262 | READ_12BIT_SER(name) | ADS_PD10_ALL_ON); \ |
| 263 | if (v < 0) \ |
| 264 | return v; \ |
| 265 | return sprintf(buf, "%u\n", (unsigned) v); \ |
| 266 | } \ |
| 267 | static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL); |
| 268 | |
| 269 | SHOW(temp0) |
| 270 | SHOW(temp1) |
| 271 | SHOW(vaux) |
| 272 | SHOW(vbatt) |
| 273 | |
Imre Deak | 438f2a7 | 2006-04-11 23:41:32 -0400 | [diff] [blame] | 274 | static int is_pen_down(struct device *dev) |
| 275 | { |
| 276 | struct ads7846 *ts = dev_get_drvdata(dev); |
| 277 | |
| 278 | return ts->pendown; |
| 279 | } |
| 280 | |
| 281 | static ssize_t ads7846_pen_down_show(struct device *dev, |
| 282 | struct device_attribute *attr, char *buf) |
| 283 | { |
| 284 | return sprintf(buf, "%u\n", is_pen_down(dev)); |
| 285 | } |
| 286 | |
| 287 | static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL); |
| 288 | |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 289 | static ssize_t ads7846_disable_show(struct device *dev, |
| 290 | struct device_attribute *attr, char *buf) |
| 291 | { |
| 292 | struct ads7846 *ts = dev_get_drvdata(dev); |
| 293 | |
| 294 | return sprintf(buf, "%u\n", ts->disabled); |
| 295 | } |
| 296 | |
| 297 | static ssize_t ads7846_disable_store(struct device *dev, |
| 298 | struct device_attribute *attr, |
| 299 | const char *buf, size_t count) |
| 300 | { |
| 301 | struct ads7846 *ts = dev_get_drvdata(dev); |
| 302 | char *endp; |
| 303 | int i; |
| 304 | |
| 305 | i = simple_strtoul(buf, &endp, 10); |
| 306 | spin_lock_irq(&ts->lock); |
| 307 | |
| 308 | if (i) |
| 309 | ads7846_disable(ts); |
| 310 | else |
| 311 | ads7846_enable(ts); |
| 312 | |
| 313 | spin_unlock_irq(&ts->lock); |
| 314 | |
| 315 | return count; |
| 316 | } |
| 317 | |
| 318 | static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store); |
| 319 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 320 | /*--------------------------------------------------------------------------*/ |
| 321 | |
| 322 | /* |
| 323 | * PENIRQ only kicks the timer. The timer only reissues the SPI transfer, |
| 324 | * to retrieve touchscreen status. |
| 325 | * |
| 326 | * The SPI transfer completion callback does the real work. It reports |
| 327 | * touchscreen events and reactivates the timer (or IRQ) as appropriate. |
| 328 | */ |
| 329 | |
| 330 | static void ads7846_rx(void *ads) |
| 331 | { |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 332 | struct ads7846 *ts = ads; |
| 333 | struct input_dev *input_dev = ts->input; |
| 334 | unsigned Rt; |
| 335 | unsigned sync = 0; |
| 336 | u16 x, y, z1, z2; |
| 337 | unsigned long flags; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 338 | |
David Brownell | 9084533 | 2006-05-25 18:44:20 -0700 | [diff] [blame] | 339 | /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding; |
| 340 | * built from two 8 bit values written msb-first. |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 341 | */ |
David Brownell | 9084533 | 2006-05-25 18:44:20 -0700 | [diff] [blame] | 342 | x = (be16_to_cpu(ts->tc.x) >> 3) & 0x0fff; |
| 343 | y = (be16_to_cpu(ts->tc.y) >> 3) & 0x0fff; |
| 344 | z1 = (be16_to_cpu(ts->tc.z1) >> 3) & 0x0fff; |
| 345 | z2 = (be16_to_cpu(ts->tc.z2) >> 3) & 0x0fff; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 346 | |
| 347 | /* range filtering */ |
| 348 | if (x == MAX_12BIT) |
| 349 | x = 0; |
| 350 | |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 351 | if (likely(x && z1 && !device_suspended(&ts->spi->dev))) { |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 352 | /* compute touch pressure resistance using equation #2 */ |
| 353 | Rt = z2; |
| 354 | Rt -= z1; |
| 355 | Rt *= x; |
| 356 | Rt *= ts->x_plate_ohms; |
| 357 | Rt /= z1; |
| 358 | Rt = (Rt + 2047) >> 12; |
| 359 | } else |
| 360 | Rt = 0; |
| 361 | |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 362 | /* Sample found inconsistent by debouncing or pressure is beyond |
| 363 | * the maximum. Don't report it to user space, repeat at least |
| 364 | * once more the measurement */ |
| 365 | if (ts->tc.ignore || Rt > ts->pressure_max) { |
| 366 | mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD); |
| 367 | return; |
| 368 | } |
| 369 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 370 | /* NOTE: "pendown" is inferred from pressure; we don't rely on |
| 371 | * being able to check nPENIRQ status, or "friendly" trigger modes |
| 372 | * (both-edges is much better than just-falling or low-level). |
| 373 | * |
| 374 | * REVISIT: some boards may require reading nPENIRQ; it's |
| 375 | * needed on 7843. and 7845 reads pressure differently... |
| 376 | * |
| 377 | * REVISIT: the touchscreen might not be connected; this code |
| 378 | * won't notice that, even if nPENIRQ never fires ... |
| 379 | */ |
| 380 | if (!ts->pendown && Rt != 0) { |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 381 | input_report_key(input_dev, BTN_TOUCH, 1); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 382 | sync = 1; |
| 383 | } else if (ts->pendown && Rt == 0) { |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 384 | input_report_key(input_dev, BTN_TOUCH, 0); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 385 | sync = 1; |
| 386 | } |
| 387 | |
| 388 | if (Rt) { |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 389 | input_report_abs(input_dev, ABS_X, x); |
| 390 | input_report_abs(input_dev, ABS_Y, y); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 391 | sync = 1; |
| 392 | } |
Imre Deak | ae82d5a | 2006-04-26 00:12:14 -0400 | [diff] [blame] | 393 | |
| 394 | if (sync) { |
| 395 | input_report_abs(input_dev, ABS_PRESSURE, Rt); |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 396 | input_sync(input_dev); |
Imre Deak | ae82d5a | 2006-04-26 00:12:14 -0400 | [diff] [blame] | 397 | } |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 398 | |
| 399 | #ifdef VERBOSE |
| 400 | if (Rt || ts->pendown) |
| 401 | pr_debug("%s: %d/%d/%d%s\n", ts->spi->dev.bus_id, |
| 402 | x, y, Rt, Rt ? "" : " UP"); |
| 403 | #endif |
| 404 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 405 | spin_lock_irqsave(&ts->lock, flags); |
| 406 | |
| 407 | ts->pendown = (Rt != 0); |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 408 | mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 409 | |
| 410 | spin_unlock_irqrestore(&ts->lock, flags); |
| 411 | } |
| 412 | |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 413 | static void ads7846_debounce(void *ads) |
| 414 | { |
| 415 | struct ads7846 *ts = ads; |
| 416 | struct spi_message *m; |
| 417 | struct spi_transfer *t; |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 418 | int val; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 419 | int status; |
| 420 | |
| 421 | m = &ts->msg[ts->msg_idx]; |
| 422 | t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list); |
David Brownell | 9084533 | 2006-05-25 18:44:20 -0700 | [diff] [blame] | 423 | val = (be16_to_cpu(*(__be16 *)t->rx_buf) >> 3) & 0x0fff; |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 424 | if (!ts->read_cnt || (abs(ts->last_read - val) > ts->debounce_tol)) { |
| 425 | /* Repeat it, if this was the first read or the read |
| 426 | * wasn't consistent enough. */ |
| 427 | if (ts->read_cnt < ts->debounce_max) { |
| 428 | ts->last_read = val; |
| 429 | ts->read_cnt++; |
| 430 | } else { |
| 431 | /* Maximum number of debouncing reached and still |
| 432 | * not enough number of consistent readings. Abort |
| 433 | * the whole sample, repeat it in the next sampling |
| 434 | * period. |
| 435 | */ |
| 436 | ts->tc.ignore = 1; |
| 437 | ts->read_cnt = 0; |
| 438 | /* Last message will contain ads7846_rx() as the |
| 439 | * completion function. |
| 440 | */ |
| 441 | m = ts->last_msg; |
| 442 | } |
| 443 | /* Start over collecting consistent readings. */ |
| 444 | ts->read_rep = 0; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 445 | } else { |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 446 | if (++ts->read_rep > ts->debounce_rep) { |
| 447 | /* Got a good reading for this coordinate, |
| 448 | * go for the next one. */ |
| 449 | ts->tc.ignore = 0; |
| 450 | ts->msg_idx++; |
| 451 | ts->read_cnt = 0; |
| 452 | ts->read_rep = 0; |
| 453 | m++; |
| 454 | } else |
| 455 | /* Read more values that are consistent. */ |
| 456 | ts->read_cnt++; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 457 | } |
| 458 | status = spi_async(ts->spi, m); |
| 459 | if (status) |
| 460 | dev_err(&ts->spi->dev, "spi_async --> %d\n", |
| 461 | status); |
| 462 | } |
| 463 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 464 | static void ads7846_timer(unsigned long handle) |
| 465 | { |
| 466 | struct ads7846 *ts = (void *)handle; |
| 467 | int status = 0; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 468 | |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 469 | spin_lock_irq(&ts->lock); |
| 470 | |
| 471 | if (unlikely(ts->msg_idx && !ts->pendown)) { |
David Brownell | 9084533 | 2006-05-25 18:44:20 -0700 | [diff] [blame] | 472 | /* measurement cycle ended */ |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 473 | if (!device_suspended(&ts->spi->dev)) { |
| 474 | ts->irq_disabled = 0; |
| 475 | enable_irq(ts->spi->irq); |
| 476 | } |
| 477 | ts->pending = 0; |
| 478 | ts->msg_idx = 0; |
| 479 | } else { |
| 480 | /* pen is still down, continue with the measurement */ |
| 481 | ts->msg_idx = 0; |
| 482 | status = spi_async(ts->spi, &ts->msg[0]); |
| 483 | if (status) |
| 484 | dev_err(&ts->spi->dev, "spi_async --> %d\n", status); |
| 485 | } |
| 486 | |
| 487 | spin_unlock_irq(&ts->lock); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs) |
| 491 | { |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 492 | struct ads7846 *ts = handle; |
| 493 | unsigned long flags; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 494 | |
| 495 | spin_lock_irqsave(&ts->lock, flags); |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 496 | if (likely(ts->get_pendown_state())) { |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 497 | if (!ts->irq_disabled) { |
David Brownell | 9084533 | 2006-05-25 18:44:20 -0700 | [diff] [blame] | 498 | /* The ARM do_simple_IRQ() dispatcher doesn't act |
| 499 | * like the other dispatchers: it will report IRQs |
| 500 | * even after they've been disabled. We work around |
| 501 | * that here. (The "generic irq" framework may help...) |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 502 | */ |
| 503 | ts->irq_disabled = 1; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 504 | disable_irq(ts->spi->irq); |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 505 | ts->pending = 1; |
| 506 | mod_timer(&ts->timer, jiffies); |
| 507 | } |
| 508 | } |
| 509 | spin_unlock_irqrestore(&ts->lock, flags); |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 510 | |
| 511 | return IRQ_HANDLED; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | /*--------------------------------------------------------------------------*/ |
| 515 | |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 516 | /* Must be called with ts->lock held */ |
| 517 | static void ads7846_disable(struct ads7846 *ts) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 518 | { |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 519 | if (ts->disabled) |
| 520 | return; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 521 | |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 522 | ts->disabled = 1; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 523 | |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 524 | /* are we waiting for IRQ, or polling? */ |
| 525 | if (!ts->pending) { |
| 526 | ts->irq_disabled = 1; |
| 527 | disable_irq(ts->spi->irq); |
| 528 | } else { |
| 529 | /* the timer will run at least once more, and |
| 530 | * leave everything in a clean state, IRQ disabled |
| 531 | */ |
| 532 | while (ts->pending) { |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 533 | spin_unlock_irq(&ts->lock); |
Juha Yrjola | c4febb9 | 2006-04-11 23:42:25 -0400 | [diff] [blame] | 534 | msleep(1); |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 535 | spin_lock_irq(&ts->lock); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
| 539 | /* we know the chip's in lowpower mode since we always |
| 540 | * leave it that way after every request |
| 541 | */ |
| 542 | |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /* Must be called with ts->lock held */ |
| 546 | static void ads7846_enable(struct ads7846 *ts) |
| 547 | { |
| 548 | if (!ts->disabled) |
| 549 | return; |
| 550 | |
| 551 | ts->disabled = 0; |
| 552 | ts->irq_disabled = 0; |
| 553 | enable_irq(ts->spi->irq); |
| 554 | } |
| 555 | |
| 556 | static int ads7846_suspend(struct spi_device *spi, pm_message_t message) |
| 557 | { |
| 558 | struct ads7846 *ts = dev_get_drvdata(&spi->dev); |
| 559 | |
| 560 | spin_lock_irq(&ts->lock); |
| 561 | |
| 562 | spi->dev.power.power_state = message; |
| 563 | ads7846_disable(ts); |
| 564 | |
| 565 | spin_unlock_irq(&ts->lock); |
| 566 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 567 | return 0; |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 568 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 569 | } |
| 570 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 571 | static int ads7846_resume(struct spi_device *spi) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 572 | { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 573 | struct ads7846 *ts = dev_get_drvdata(&spi->dev); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 574 | |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 575 | spin_lock_irq(&ts->lock); |
| 576 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 577 | spi->dev.power.power_state = PMSG_ON; |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 578 | ads7846_enable(ts); |
| 579 | |
| 580 | spin_unlock_irq(&ts->lock); |
| 581 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 582 | return 0; |
| 583 | } |
| 584 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 585 | static int __devinit ads7846_probe(struct spi_device *spi) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 586 | { |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 587 | struct ads7846 *ts; |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 588 | struct input_dev *input_dev; |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 589 | struct ads7846_platform_data *pdata = spi->dev.platform_data; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 590 | struct spi_message *m; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 591 | struct spi_transfer *x; |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 592 | int err; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 593 | |
| 594 | if (!spi->irq) { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 595 | dev_dbg(&spi->dev, "no IRQ?\n"); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 596 | return -ENODEV; |
| 597 | } |
| 598 | |
| 599 | if (!pdata) { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 600 | dev_dbg(&spi->dev, "no platform data?\n"); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 601 | return -ENODEV; |
| 602 | } |
| 603 | |
| 604 | /* don't exceed max specified sample rate */ |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 605 | if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 606 | dev_dbg(&spi->dev, "f(sample) %d KHz?\n", |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 607 | (spi->max_speed_hz/SAMPLE_BITS)/1000); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 608 | return -EINVAL; |
| 609 | } |
| 610 | |
David Brownell | 9084533 | 2006-05-25 18:44:20 -0700 | [diff] [blame] | 611 | /* REVISIT when the irq can be triggered active-low, or if for some |
| 612 | * reason the touchscreen isn't hooked up, we don't need to access |
| 613 | * the pendown state. |
| 614 | */ |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 615 | if (pdata->get_pendown_state == NULL) { |
| 616 | dev_dbg(&spi->dev, "no get_pendown_state function?\n"); |
| 617 | return -EINVAL; |
| 618 | } |
| 619 | |
David Brownell | 9084533 | 2006-05-25 18:44:20 -0700 | [diff] [blame] | 620 | /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except |
| 621 | * that even if the hardware can do that, the SPI controller driver |
| 622 | * may not. So we stick to very-portable 8 bit words, both RX and TX. |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 623 | */ |
David Brownell | 9084533 | 2006-05-25 18:44:20 -0700 | [diff] [blame] | 624 | spi->bits_per_word = 8; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 625 | |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 626 | ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL); |
| 627 | input_dev = input_allocate_device(); |
| 628 | if (!ts || !input_dev) { |
| 629 | err = -ENOMEM; |
| 630 | goto err_free_mem; |
| 631 | } |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 632 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 633 | dev_set_drvdata(&spi->dev, ts); |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 634 | spi->dev.power.power_state = PMSG_ON; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 635 | |
| 636 | ts->spi = spi; |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 637 | ts->input = input_dev; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 638 | |
| 639 | init_timer(&ts->timer); |
| 640 | ts->timer.data = (unsigned long) ts; |
| 641 | ts->timer.function = ads7846_timer; |
| 642 | |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 643 | spin_lock_init(&ts->lock); |
| 644 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 645 | ts->model = pdata->model ? : 7846; |
| 646 | ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100; |
| 647 | ts->x_plate_ohms = pdata->x_plate_ohms ? : 400; |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 648 | ts->pressure_max = pdata->pressure_max ? : ~0; |
| 649 | if (pdata->debounce_max) { |
| 650 | ts->debounce_max = pdata->debounce_max; |
| 651 | ts->debounce_tol = pdata->debounce_tol; |
| 652 | ts->debounce_rep = pdata->debounce_rep; |
| 653 | if (ts->debounce_rep > ts->debounce_max + 1) |
| 654 | ts->debounce_rep = ts->debounce_max - 1; |
| 655 | } else |
| 656 | ts->debounce_tol = ~0; |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 657 | ts->get_pendown_state = pdata->get_pendown_state; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 658 | |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 659 | snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 660 | |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 661 | input_dev->name = "ADS784x Touchscreen"; |
| 662 | input_dev->phys = ts->phys; |
| 663 | input_dev->cdev.dev = &spi->dev; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 664 | |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 665 | input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS); |
| 666 | input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH); |
| 667 | input_set_abs_params(input_dev, ABS_X, |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 668 | pdata->x_min ? : 0, |
| 669 | pdata->x_max ? : MAX_12BIT, |
| 670 | 0, 0); |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 671 | input_set_abs_params(input_dev, ABS_Y, |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 672 | pdata->y_min ? : 0, |
| 673 | pdata->y_max ? : MAX_12BIT, |
| 674 | 0, 0); |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 675 | input_set_abs_params(input_dev, ABS_PRESSURE, |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 676 | pdata->pressure_min, pdata->pressure_max, 0, 0); |
| 677 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 678 | /* set up the transfers to read touchscreen state; this assumes we |
| 679 | * use formula #2 for pressure, not #3. |
| 680 | */ |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 681 | m = &ts->msg[0]; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 682 | x = ts->xfer; |
| 683 | |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 684 | spi_message_init(m); |
| 685 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 686 | /* y- still on; turn on only y+ (and ADC) */ |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 687 | ts->read_y = READ_Y; |
| 688 | x->tx_buf = &ts->read_y; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 689 | x->len = 1; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 690 | spi_message_add_tail(x, m); |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 691 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 692 | x++; |
| 693 | x->rx_buf = &ts->tc.y; |
| 694 | x->len = 2; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 695 | spi_message_add_tail(x, m); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 696 | |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 697 | m->complete = ads7846_debounce; |
| 698 | m->context = ts; |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 699 | |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 700 | m++; |
| 701 | spi_message_init(m); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 702 | |
| 703 | /* turn y- off, x+ on, then leave in lowpower */ |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 704 | x++; |
| 705 | ts->read_x = READ_X; |
| 706 | x->tx_buf = &ts->read_x; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 707 | x->len = 1; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 708 | spi_message_add_tail(x, m); |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 709 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 710 | x++; |
| 711 | x->rx_buf = &ts->tc.x; |
| 712 | x->len = 2; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 713 | spi_message_add_tail(x, m); |
| 714 | |
| 715 | m->complete = ads7846_debounce; |
| 716 | m->context = ts; |
| 717 | |
| 718 | /* turn y+ off, x- on; we'll use formula #2 */ |
| 719 | if (ts->model == 7846) { |
| 720 | m++; |
| 721 | spi_message_init(m); |
| 722 | |
| 723 | x++; |
| 724 | ts->read_z1 = READ_Z1; |
| 725 | x->tx_buf = &ts->read_z1; |
| 726 | x->len = 1; |
| 727 | spi_message_add_tail(x, m); |
| 728 | |
| 729 | x++; |
| 730 | x->rx_buf = &ts->tc.z1; |
| 731 | x->len = 2; |
| 732 | spi_message_add_tail(x, m); |
| 733 | |
| 734 | m->complete = ads7846_debounce; |
| 735 | m->context = ts; |
| 736 | |
| 737 | m++; |
| 738 | spi_message_init(m); |
| 739 | |
| 740 | x++; |
| 741 | ts->read_z2 = READ_Z2; |
| 742 | x->tx_buf = &ts->read_z2; |
| 743 | x->len = 1; |
| 744 | spi_message_add_tail(x, m); |
| 745 | |
| 746 | x++; |
| 747 | x->rx_buf = &ts->tc.z2; |
| 748 | x->len = 2; |
| 749 | spi_message_add_tail(x, m); |
| 750 | |
| 751 | m->complete = ads7846_debounce; |
| 752 | m->context = ts; |
| 753 | } |
Imre Deak | 53a0ef89 | 2006-04-11 23:41:49 -0400 | [diff] [blame] | 754 | |
| 755 | /* power down */ |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 756 | m++; |
| 757 | spi_message_init(m); |
| 758 | |
Imre Deak | 53a0ef89 | 2006-04-11 23:41:49 -0400 | [diff] [blame] | 759 | x++; |
| 760 | ts->pwrdown = PWRDOWN; |
| 761 | x->tx_buf = &ts->pwrdown; |
| 762 | x->len = 1; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 763 | spi_message_add_tail(x, m); |
Imre Deak | 53a0ef89 | 2006-04-11 23:41:49 -0400 | [diff] [blame] | 764 | |
| 765 | x++; |
| 766 | x->rx_buf = &ts->dummy; |
| 767 | x->len = 2; |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 768 | CS_CHANGE(*x); |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 769 | spi_message_add_tail(x, m); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 770 | |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 771 | m->complete = ads7846_rx; |
| 772 | m->context = ts; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 773 | |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 774 | ts->last_msg = m; |
| 775 | |
Thomas Gleixner | dace145 | 2006-07-01 19:29:38 -0700 | [diff] [blame] | 776 | if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING, |
David Brownell | 9084533 | 2006-05-25 18:44:20 -0700 | [diff] [blame] | 777 | spi->dev.driver->name, ts)) { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 778 | dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq); |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 779 | err = -EBUSY; |
| 780 | goto err_free_mem; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 781 | } |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 782 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 783 | dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 784 | |
| 785 | /* take a first sample, leaving nPENIRQ active; avoid |
| 786 | * the touchscreen, in case it's not connected. |
| 787 | */ |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 788 | (void) ads7846_read12_ser(&spi->dev, |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 789 | READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON); |
| 790 | |
| 791 | /* ads7843/7845 don't have temperature sensors, and |
| 792 | * use the other sensors a bit differently too |
| 793 | */ |
| 794 | if (ts->model == 7846) { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 795 | device_create_file(&spi->dev, &dev_attr_temp0); |
| 796 | device_create_file(&spi->dev, &dev_attr_temp1); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 797 | } |
| 798 | if (ts->model != 7845) |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 799 | device_create_file(&spi->dev, &dev_attr_vbatt); |
| 800 | device_create_file(&spi->dev, &dev_attr_vaux); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 801 | |
Imre Deak | 438f2a7 | 2006-04-11 23:41:32 -0400 | [diff] [blame] | 802 | device_create_file(&spi->dev, &dev_attr_pen_down); |
| 803 | |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 804 | device_create_file(&spi->dev, &dev_attr_disable); |
| 805 | |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 806 | err = input_register_device(input_dev); |
| 807 | if (err) |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 808 | goto err_remove_attr; |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 809 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 810 | return 0; |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 811 | |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 812 | err_remove_attr: |
| 813 | device_remove_file(&spi->dev, &dev_attr_disable); |
| 814 | device_remove_file(&spi->dev, &dev_attr_pen_down); |
| 815 | if (ts->model == 7846) { |
| 816 | device_remove_file(&spi->dev, &dev_attr_temp1); |
| 817 | device_remove_file(&spi->dev, &dev_attr_temp0); |
| 818 | } |
| 819 | if (ts->model != 7845) |
| 820 | device_remove_file(&spi->dev, &dev_attr_vbatt); |
| 821 | device_remove_file(&spi->dev, &dev_attr_vaux); |
| 822 | |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 823 | free_irq(spi->irq, ts); |
| 824 | err_free_mem: |
| 825 | input_free_device(input_dev); |
| 826 | kfree(ts); |
| 827 | return err; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 828 | } |
| 829 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 830 | static int __devexit ads7846_remove(struct spi_device *spi) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 831 | { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 832 | struct ads7846 *ts = dev_get_drvdata(&spi->dev); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 833 | |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 834 | input_unregister_device(ts->input); |
| 835 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 836 | ads7846_suspend(spi, PMSG_SUSPEND); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 837 | |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 838 | device_remove_file(&spi->dev, &dev_attr_disable); |
Imre Deak | 438f2a7 | 2006-04-11 23:41:32 -0400 | [diff] [blame] | 839 | device_remove_file(&spi->dev, &dev_attr_pen_down); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 840 | if (ts->model == 7846) { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 841 | device_remove_file(&spi->dev, &dev_attr_temp1); |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 842 | device_remove_file(&spi->dev, &dev_attr_temp0); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 843 | } |
| 844 | if (ts->model != 7845) |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 845 | device_remove_file(&spi->dev, &dev_attr_vbatt); |
| 846 | device_remove_file(&spi->dev, &dev_attr_vaux); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 847 | |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 848 | free_irq(ts->spi->irq, ts); |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 849 | /* suspend left the IRQ disabled */ |
| 850 | enable_irq(ts->spi->irq); |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 851 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 852 | kfree(ts); |
| 853 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 854 | dev_dbg(&spi->dev, "unregistered touchscreen\n"); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 855 | return 0; |
| 856 | } |
| 857 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 858 | static struct spi_driver ads7846_driver = { |
| 859 | .driver = { |
| 860 | .name = "ads7846", |
| 861 | .bus = &spi_bus_type, |
| 862 | .owner = THIS_MODULE, |
| 863 | }, |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 864 | .probe = ads7846_probe, |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 865 | .remove = __devexit_p(ads7846_remove), |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 866 | .suspend = ads7846_suspend, |
| 867 | .resume = ads7846_resume, |
| 868 | }; |
| 869 | |
| 870 | static int __init ads7846_init(void) |
| 871 | { |
| 872 | /* grr, board-specific init should stay out of drivers!! */ |
| 873 | |
| 874 | #ifdef CONFIG_ARCH_OMAP |
| 875 | if (machine_is_omap_osk()) { |
| 876 | /* GPIO4 = PENIRQ; GPIO6 = BUSY */ |
| 877 | omap_request_gpio(4); |
| 878 | omap_set_gpio_direction(4, 1); |
| 879 | omap_request_gpio(6); |
| 880 | omap_set_gpio_direction(6, 1); |
| 881 | } |
| 882 | // also TI 1510 Innovator, bitbanging through FPGA |
| 883 | // also Nokia 770 |
| 884 | // also Palm Tungsten T2 |
| 885 | #endif |
| 886 | |
| 887 | // PXA: |
| 888 | // also Dell Axim X50 |
| 889 | // also HP iPaq H191x/H192x/H415x/H435x |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 890 | // also Intel Lubbock (additional to UCB1400; as temperature sensor) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 891 | // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky) |
| 892 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 893 | // Atmel at91sam9261-EK uses ads7843 |
| 894 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 895 | // also various AMD Au1x00 devel boards |
| 896 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 897 | return spi_register_driver(&ads7846_driver); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 898 | } |
| 899 | module_init(ads7846_init); |
| 900 | |
| 901 | static void __exit ads7846_exit(void) |
| 902 | { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 903 | spi_unregister_driver(&ads7846_driver); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 904 | |
| 905 | #ifdef CONFIG_ARCH_OMAP |
| 906 | if (machine_is_omap_osk()) { |
| 907 | omap_free_gpio(4); |
| 908 | omap_free_gpio(6); |
| 909 | } |
| 910 | #endif |
| 911 | |
| 912 | } |
| 913 | module_exit(ads7846_exit); |
| 914 | |
| 915 | MODULE_DESCRIPTION("ADS7846 TouchScreen Driver"); |
| 916 | MODULE_LICENSE("GPL"); |