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