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