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 |
| 5 | * |
| 6 | * Using code from: |
| 7 | * - corgi_ts.c |
| 8 | * Copyright (C) 2004-2005 Richard Purdie |
| 9 | * - omap_ts.[hc], ads7846.h, ts_osk.c |
| 10 | * Copyright (C) 2002 MontaVista Software |
| 11 | * Copyright (C) 2004 Texas Instruments |
| 12 | * Copyright (C) 2005 Dirk Behme |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License version 2 as |
| 16 | * published by the Free Software Foundation. |
| 17 | */ |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/input.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/spi/spi.h> |
| 25 | #include <linux/spi/ads7846.h> |
Andrew Morton | 3ac8bf0 | 2006-03-26 01:37:33 -0800 | [diff] [blame] | 26 | #include <asm/irq.h> |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 27 | |
| 28 | #ifdef CONFIG_ARM |
| 29 | #include <asm/mach-types.h> |
| 30 | #ifdef CONFIG_ARCH_OMAP |
| 31 | #include <asm/arch/gpio.h> |
| 32 | #endif |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 33 | #endif |
| 34 | |
| 35 | |
| 36 | /* |
| 37 | * This code has been lightly tested on an ads7846. |
| 38 | * Support for ads7843 and ads7845 has only been stubbed in. |
| 39 | * |
| 40 | * Not yet done: investigate the values reported. Are x/y/pressure |
| 41 | * event values sane enough for X11? How accurate are the temperature |
| 42 | * and voltage readings? (System-specific calibration should support |
| 43 | * accuracy of 0.3 degrees C; otherwise it's 2.0 degrees.) |
| 44 | * |
| 45 | * app note sbaa036 talks in more detail about accurate sampling... |
| 46 | * that ought to help in situations like LCDs inducing noise (which |
| 47 | * can also be helped by using synch signals) and more generally. |
| 48 | */ |
| 49 | |
| 50 | #define TS_POLL_PERIOD msecs_to_jiffies(10) |
| 51 | |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 52 | /* this driver doesn't aim at the peak continuous sample rate */ |
| 53 | #define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */) |
| 54 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 55 | struct ts_event { |
| 56 | /* For portability, we can't read 12 bit values using SPI (which |
| 57 | * would make the controller deliver them as native byteorder u16 |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 58 | * with msbs zeroed). Instead, we read them as two 8-bit values, |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 59 | * which need byteswapping then range adjustment. |
| 60 | */ |
| 61 | __be16 x; |
| 62 | __be16 y; |
| 63 | __be16 z1, z2; |
| 64 | }; |
| 65 | |
| 66 | struct ads7846 { |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 67 | struct input_dev *input; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 68 | char phys[32]; |
| 69 | |
| 70 | struct spi_device *spi; |
| 71 | u16 model; |
| 72 | u16 vref_delay_usecs; |
| 73 | u16 x_plate_ohms; |
| 74 | |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 75 | u8 read_x, read_y, read_z1, read_z2; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 76 | struct ts_event tc; |
| 77 | |
| 78 | struct spi_transfer xfer[8]; |
| 79 | struct spi_message msg; |
| 80 | |
| 81 | spinlock_t lock; |
| 82 | struct timer_list timer; /* P: lock */ |
| 83 | unsigned pendown:1; /* P: lock */ |
| 84 | unsigned pending:1; /* P: lock */ |
| 85 | // FIXME remove "irq_disabled" |
| 86 | unsigned irq_disabled:1; /* P: lock */ |
| 87 | }; |
| 88 | |
| 89 | /* leave chip selected when we're done, for quicker re-select? */ |
| 90 | #if 0 |
| 91 | #define CS_CHANGE(xfer) ((xfer).cs_change = 1) |
| 92 | #else |
| 93 | #define CS_CHANGE(xfer) ((xfer).cs_change = 0) |
| 94 | #endif |
| 95 | |
| 96 | /*--------------------------------------------------------------------------*/ |
| 97 | |
| 98 | /* The ADS7846 has touchscreen and other sensors. |
| 99 | * Earlier ads784x chips are somewhat compatible. |
| 100 | */ |
| 101 | #define ADS_START (1 << 7) |
| 102 | #define ADS_A2A1A0_d_y (1 << 4) /* differential */ |
| 103 | #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */ |
| 104 | #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */ |
| 105 | #define ADS_A2A1A0_d_x (5 << 4) /* differential */ |
| 106 | #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */ |
| 107 | #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */ |
| 108 | #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */ |
| 109 | #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */ |
| 110 | #define ADS_8_BIT (1 << 3) |
| 111 | #define ADS_12_BIT (0 << 3) |
| 112 | #define ADS_SER (1 << 2) /* non-differential */ |
| 113 | #define ADS_DFR (0 << 2) /* differential */ |
| 114 | #define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */ |
| 115 | #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */ |
| 116 | #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */ |
| 117 | #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */ |
| 118 | |
| 119 | #define MAX_12BIT ((1<<12)-1) |
| 120 | |
| 121 | /* leave ADC powered up (disables penirq) between differential samples */ |
| 122 | #define READ_12BIT_DFR(x) (ADS_START | ADS_A2A1A0_d_ ## x \ |
| 123 | | ADS_12_BIT | ADS_DFR) |
| 124 | |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 125 | #define READ_Y (READ_12BIT_DFR(y) | ADS_PD10_ADC_ON) |
| 126 | #define READ_Z1 (READ_12BIT_DFR(z1) | ADS_PD10_ADC_ON) |
| 127 | #define READ_Z2 (READ_12BIT_DFR(z2) | ADS_PD10_ADC_ON) |
| 128 | #define READ_X (READ_12BIT_DFR(x) | ADS_PD10_PDOWN) /* LAST */ |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 129 | |
| 130 | /* single-ended samples need to first power up reference voltage; |
| 131 | * we leave both ADC and VREF powered |
| 132 | */ |
| 133 | #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \ |
| 134 | | ADS_12_BIT | ADS_SER) |
| 135 | |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 136 | #define REF_ON (READ_12BIT_DFR(x) | ADS_PD10_ALL_ON) |
| 137 | #define REF_OFF (READ_12BIT_DFR(y) | ADS_PD10_PDOWN) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 138 | |
| 139 | /*--------------------------------------------------------------------------*/ |
| 140 | |
| 141 | /* |
| 142 | * Non-touchscreen sensors only use single-ended conversions. |
| 143 | */ |
| 144 | |
| 145 | struct ser_req { |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 146 | u8 ref_on; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 147 | u8 command; |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 148 | u8 ref_off; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 149 | u16 scratch; |
| 150 | __be16 sample; |
| 151 | struct spi_message msg; |
| 152 | struct spi_transfer xfer[6]; |
| 153 | }; |
| 154 | |
| 155 | static int ads7846_read12_ser(struct device *dev, unsigned command) |
| 156 | { |
| 157 | struct spi_device *spi = to_spi_device(dev); |
| 158 | struct ads7846 *ts = dev_get_drvdata(dev); |
| 159 | struct ser_req *req = kzalloc(sizeof *req, SLAB_KERNEL); |
| 160 | int status; |
| 161 | int sample; |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 162 | int i; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 163 | |
| 164 | if (!req) |
| 165 | return -ENOMEM; |
| 166 | |
Vitaly Wool | 8275c64 | 2006-01-08 13:34:28 -0800 | [diff] [blame] | 167 | INIT_LIST_HEAD(&req->msg.transfers); |
| 168 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 169 | /* activate reference, so it has time to settle; */ |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 170 | req->ref_on = REF_ON; |
| 171 | req->xfer[0].tx_buf = &req->ref_on; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 172 | req->xfer[0].len = 1; |
| 173 | req->xfer[1].rx_buf = &req->scratch; |
| 174 | req->xfer[1].len = 2; |
| 175 | |
| 176 | /* |
| 177 | * for external VREF, 0 usec (and assume it's always on); |
| 178 | * for 1uF, use 800 usec; |
| 179 | * no cap, 100 usec. |
| 180 | */ |
| 181 | req->xfer[1].delay_usecs = ts->vref_delay_usecs; |
| 182 | |
| 183 | /* take sample */ |
| 184 | req->command = (u8) command; |
| 185 | req->xfer[2].tx_buf = &req->command; |
| 186 | req->xfer[2].len = 1; |
| 187 | req->xfer[3].rx_buf = &req->sample; |
| 188 | req->xfer[3].len = 2; |
| 189 | |
| 190 | /* REVISIT: take a few more samples, and compare ... */ |
| 191 | |
| 192 | /* turn off reference */ |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 193 | req->ref_off = REF_OFF; |
| 194 | req->xfer[4].tx_buf = &req->ref_off; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 195 | req->xfer[4].len = 1; |
| 196 | req->xfer[5].rx_buf = &req->scratch; |
| 197 | req->xfer[5].len = 2; |
| 198 | |
| 199 | CS_CHANGE(req->xfer[5]); |
| 200 | |
| 201 | /* group all the transfers together, so we can't interfere with |
| 202 | * reading touchscreen state; disable penirq while sampling |
| 203 | */ |
Vitaly Wool | 8275c64 | 2006-01-08 13:34:28 -0800 | [diff] [blame] | 204 | for (i = 0; i < 6; i++) |
| 205 | spi_message_add_tail(&req->xfer[i], &req->msg); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 206 | |
| 207 | disable_irq(spi->irq); |
| 208 | status = spi_sync(spi, &req->msg); |
| 209 | enable_irq(spi->irq); |
| 210 | |
| 211 | if (req->msg.status) |
| 212 | status = req->msg.status; |
| 213 | sample = be16_to_cpu(req->sample); |
| 214 | sample = sample >> 4; |
| 215 | kfree(req); |
| 216 | |
| 217 | return status ? status : sample; |
| 218 | } |
| 219 | |
| 220 | #define SHOW(name) static ssize_t \ |
| 221 | name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \ |
| 222 | { \ |
| 223 | ssize_t v = ads7846_read12_ser(dev, \ |
| 224 | READ_12BIT_SER(name) | ADS_PD10_ALL_ON); \ |
| 225 | if (v < 0) \ |
| 226 | return v; \ |
| 227 | return sprintf(buf, "%u\n", (unsigned) v); \ |
| 228 | } \ |
| 229 | static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL); |
| 230 | |
| 231 | SHOW(temp0) |
| 232 | SHOW(temp1) |
| 233 | SHOW(vaux) |
| 234 | SHOW(vbatt) |
| 235 | |
Imre Deak | 438f2a7 | 2006-04-11 23:41:32 -0400 | [diff] [blame^] | 236 | static int is_pen_down(struct device *dev) |
| 237 | { |
| 238 | struct ads7846 *ts = dev_get_drvdata(dev); |
| 239 | |
| 240 | return ts->pendown; |
| 241 | } |
| 242 | |
| 243 | static ssize_t ads7846_pen_down_show(struct device *dev, |
| 244 | struct device_attribute *attr, char *buf) |
| 245 | { |
| 246 | return sprintf(buf, "%u\n", is_pen_down(dev)); |
| 247 | } |
| 248 | |
| 249 | static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL); |
| 250 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 251 | /*--------------------------------------------------------------------------*/ |
| 252 | |
| 253 | /* |
| 254 | * PENIRQ only kicks the timer. The timer only reissues the SPI transfer, |
| 255 | * to retrieve touchscreen status. |
| 256 | * |
| 257 | * The SPI transfer completion callback does the real work. It reports |
| 258 | * touchscreen events and reactivates the timer (or IRQ) as appropriate. |
| 259 | */ |
| 260 | |
| 261 | static void ads7846_rx(void *ads) |
| 262 | { |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 263 | struct ads7846 *ts = ads; |
| 264 | struct input_dev *input_dev = ts->input; |
| 265 | unsigned Rt; |
| 266 | unsigned sync = 0; |
| 267 | u16 x, y, z1, z2; |
| 268 | unsigned long flags; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 269 | |
| 270 | /* adjust: 12 bit samples (left aligned), built from |
| 271 | * two 8 bit values writen msb-first. |
| 272 | */ |
| 273 | x = be16_to_cpu(ts->tc.x) >> 4; |
| 274 | y = be16_to_cpu(ts->tc.y) >> 4; |
| 275 | z1 = be16_to_cpu(ts->tc.z1) >> 4; |
| 276 | z2 = be16_to_cpu(ts->tc.z2) >> 4; |
| 277 | |
| 278 | /* range filtering */ |
| 279 | if (x == MAX_12BIT) |
| 280 | x = 0; |
| 281 | |
| 282 | if (x && z1 && ts->spi->dev.power.power_state.event == PM_EVENT_ON) { |
| 283 | /* compute touch pressure resistance using equation #2 */ |
| 284 | Rt = z2; |
| 285 | Rt -= z1; |
| 286 | Rt *= x; |
| 287 | Rt *= ts->x_plate_ohms; |
| 288 | Rt /= z1; |
| 289 | Rt = (Rt + 2047) >> 12; |
| 290 | } else |
| 291 | Rt = 0; |
| 292 | |
| 293 | /* NOTE: "pendown" is inferred from pressure; we don't rely on |
| 294 | * being able to check nPENIRQ status, or "friendly" trigger modes |
| 295 | * (both-edges is much better than just-falling or low-level). |
| 296 | * |
| 297 | * REVISIT: some boards may require reading nPENIRQ; it's |
| 298 | * needed on 7843. and 7845 reads pressure differently... |
| 299 | * |
| 300 | * REVISIT: the touchscreen might not be connected; this code |
| 301 | * won't notice that, even if nPENIRQ never fires ... |
| 302 | */ |
| 303 | if (!ts->pendown && Rt != 0) { |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 304 | input_report_key(input_dev, BTN_TOUCH, 1); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 305 | sync = 1; |
| 306 | } else if (ts->pendown && Rt == 0) { |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 307 | input_report_key(input_dev, BTN_TOUCH, 0); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 308 | sync = 1; |
| 309 | } |
| 310 | |
| 311 | if (Rt) { |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 312 | input_report_abs(input_dev, ABS_X, x); |
| 313 | input_report_abs(input_dev, ABS_Y, y); |
| 314 | input_report_abs(input_dev, ABS_PRESSURE, Rt); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 315 | sync = 1; |
| 316 | } |
| 317 | if (sync) |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 318 | input_sync(input_dev); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 319 | |
| 320 | #ifdef VERBOSE |
| 321 | if (Rt || ts->pendown) |
| 322 | pr_debug("%s: %d/%d/%d%s\n", ts->spi->dev.bus_id, |
| 323 | x, y, Rt, Rt ? "" : " UP"); |
| 324 | #endif |
| 325 | |
| 326 | /* don't retrigger while we're suspended */ |
| 327 | spin_lock_irqsave(&ts->lock, flags); |
| 328 | |
| 329 | ts->pendown = (Rt != 0); |
| 330 | ts->pending = 0; |
| 331 | |
| 332 | if (ts->spi->dev.power.power_state.event == PM_EVENT_ON) { |
| 333 | if (ts->pendown) |
| 334 | mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD); |
| 335 | else if (ts->irq_disabled) { |
| 336 | ts->irq_disabled = 0; |
| 337 | enable_irq(ts->spi->irq); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | spin_unlock_irqrestore(&ts->lock, flags); |
| 342 | } |
| 343 | |
| 344 | static void ads7846_timer(unsigned long handle) |
| 345 | { |
| 346 | struct ads7846 *ts = (void *)handle; |
| 347 | int status = 0; |
| 348 | unsigned long flags; |
| 349 | |
| 350 | spin_lock_irqsave(&ts->lock, flags); |
| 351 | if (!ts->pending) { |
| 352 | ts->pending = 1; |
| 353 | if (!ts->irq_disabled) { |
| 354 | ts->irq_disabled = 1; |
| 355 | disable_irq(ts->spi->irq); |
| 356 | } |
| 357 | status = spi_async(ts->spi, &ts->msg); |
| 358 | if (status) |
| 359 | dev_err(&ts->spi->dev, "spi_async --> %d\n", |
| 360 | status); |
| 361 | } |
| 362 | spin_unlock_irqrestore(&ts->lock, flags); |
| 363 | } |
| 364 | |
| 365 | static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs) |
| 366 | { |
| 367 | ads7846_timer((unsigned long) handle); |
| 368 | return IRQ_HANDLED; |
| 369 | } |
| 370 | |
| 371 | /*--------------------------------------------------------------------------*/ |
| 372 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 373 | static int |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 374 | ads7846_suspend(struct spi_device *spi, pm_message_t message) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 375 | { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 376 | struct ads7846 *ts = dev_get_drvdata(&spi->dev); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 377 | unsigned long flags; |
| 378 | |
| 379 | spin_lock_irqsave(&ts->lock, flags); |
| 380 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 381 | spi->dev.power.power_state = message; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 382 | |
| 383 | /* are we waiting for IRQ, or polling? */ |
| 384 | if (!ts->pendown) { |
| 385 | if (!ts->irq_disabled) { |
| 386 | ts->irq_disabled = 1; |
| 387 | disable_irq(ts->spi->irq); |
| 388 | } |
| 389 | } else { |
| 390 | /* polling; force a final SPI completion; |
| 391 | * that will clean things up neatly |
| 392 | */ |
| 393 | if (!ts->pending) |
| 394 | mod_timer(&ts->timer, jiffies); |
| 395 | |
| 396 | while (ts->pendown || ts->pending) { |
| 397 | spin_unlock_irqrestore(&ts->lock, flags); |
| 398 | udelay(10); |
| 399 | spin_lock_irqsave(&ts->lock, flags); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | /* we know the chip's in lowpower mode since we always |
| 404 | * leave it that way after every request |
| 405 | */ |
| 406 | |
| 407 | spin_unlock_irqrestore(&ts->lock, flags); |
| 408 | return 0; |
| 409 | } |
| 410 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 411 | static int ads7846_resume(struct spi_device *spi) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 412 | { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 413 | struct ads7846 *ts = dev_get_drvdata(&spi->dev); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 414 | |
| 415 | ts->irq_disabled = 0; |
| 416 | enable_irq(ts->spi->irq); |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 417 | spi->dev.power.power_state = PMSG_ON; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 418 | return 0; |
| 419 | } |
| 420 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 421 | static int __devinit ads7846_probe(struct spi_device *spi) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 422 | { |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 423 | struct ads7846 *ts; |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 424 | struct input_dev *input_dev; |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 425 | struct ads7846_platform_data *pdata = spi->dev.platform_data; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 426 | struct spi_transfer *x; |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 427 | int err; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 428 | |
| 429 | if (!spi->irq) { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 430 | dev_dbg(&spi->dev, "no IRQ?\n"); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 431 | return -ENODEV; |
| 432 | } |
| 433 | |
| 434 | if (!pdata) { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 435 | dev_dbg(&spi->dev, "no platform data?\n"); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 436 | return -ENODEV; |
| 437 | } |
| 438 | |
| 439 | /* don't exceed max specified sample rate */ |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 440 | if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 441 | dev_dbg(&spi->dev, "f(sample) %d KHz?\n", |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 442 | (spi->max_speed_hz/SAMPLE_BITS)/1000); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 443 | return -EINVAL; |
| 444 | } |
| 445 | |
| 446 | /* We'd set the wordsize to 12 bits ... except that some controllers |
| 447 | * will then treat the 8 bit command words as 12 bits (and drop the |
| 448 | * four MSBs of the 12 bit result). Result: inputs must be shifted |
| 449 | * to discard the four garbage LSBs. |
| 450 | */ |
| 451 | |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 452 | ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL); |
| 453 | input_dev = input_allocate_device(); |
| 454 | if (!ts || !input_dev) { |
| 455 | err = -ENOMEM; |
| 456 | goto err_free_mem; |
| 457 | } |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 458 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 459 | dev_set_drvdata(&spi->dev, ts); |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 460 | spi->dev.power.power_state = PMSG_ON; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 461 | |
| 462 | ts->spi = spi; |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 463 | ts->input = input_dev; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 464 | |
| 465 | init_timer(&ts->timer); |
| 466 | ts->timer.data = (unsigned long) ts; |
| 467 | ts->timer.function = ads7846_timer; |
| 468 | |
| 469 | ts->model = pdata->model ? : 7846; |
| 470 | ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100; |
| 471 | ts->x_plate_ohms = pdata->x_plate_ohms ? : 400; |
| 472 | |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 473 | snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 474 | |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 475 | input_dev->name = "ADS784x Touchscreen"; |
| 476 | input_dev->phys = ts->phys; |
| 477 | input_dev->cdev.dev = &spi->dev; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 478 | |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 479 | input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS); |
| 480 | input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH); |
| 481 | input_set_abs_params(input_dev, ABS_X, |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 482 | pdata->x_min ? : 0, |
| 483 | pdata->x_max ? : MAX_12BIT, |
| 484 | 0, 0); |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 485 | input_set_abs_params(input_dev, ABS_Y, |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 486 | pdata->y_min ? : 0, |
| 487 | pdata->y_max ? : MAX_12BIT, |
| 488 | 0, 0); |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 489 | input_set_abs_params(input_dev, ABS_PRESSURE, |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 490 | pdata->pressure_min, pdata->pressure_max, 0, 0); |
| 491 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 492 | /* set up the transfers to read touchscreen state; this assumes we |
| 493 | * use formula #2 for pressure, not #3. |
| 494 | */ |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 495 | INIT_LIST_HEAD(&ts->msg.transfers); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 496 | x = ts->xfer; |
| 497 | |
| 498 | /* y- still on; turn on only y+ (and ADC) */ |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 499 | ts->read_y = READ_Y; |
| 500 | x->tx_buf = &ts->read_y; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 501 | x->len = 1; |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 502 | spi_message_add_tail(x, &ts->msg); |
| 503 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 504 | x++; |
| 505 | x->rx_buf = &ts->tc.y; |
| 506 | x->len = 2; |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 507 | spi_message_add_tail(x, &ts->msg); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 508 | |
| 509 | /* turn y+ off, x- on; we'll use formula #2 */ |
| 510 | if (ts->model == 7846) { |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 511 | x++; |
| 512 | ts->read_z1 = READ_Z1; |
| 513 | x->tx_buf = &ts->read_z1; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 514 | x->len = 1; |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 515 | spi_message_add_tail(x, &ts->msg); |
| 516 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 517 | x++; |
| 518 | x->rx_buf = &ts->tc.z1; |
| 519 | x->len = 2; |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 520 | spi_message_add_tail(x, &ts->msg); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 521 | |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 522 | x++; |
| 523 | ts->read_z2 = READ_Z2; |
| 524 | x->tx_buf = &ts->read_z2; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 525 | x->len = 1; |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 526 | spi_message_add_tail(x, &ts->msg); |
| 527 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 528 | x++; |
| 529 | x->rx_buf = &ts->tc.z2; |
| 530 | x->len = 2; |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 531 | spi_message_add_tail(x, &ts->msg); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /* turn y- off, x+ on, then leave in lowpower */ |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 535 | x++; |
| 536 | ts->read_x = READ_X; |
| 537 | x->tx_buf = &ts->read_x; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 538 | x->len = 1; |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 539 | spi_message_add_tail(x, &ts->msg); |
| 540 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 541 | x++; |
| 542 | x->rx_buf = &ts->tc.x; |
| 543 | x->len = 2; |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 544 | CS_CHANGE(*x); |
| 545 | spi_message_add_tail(x, &ts->msg); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 546 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 547 | ts->msg.complete = ads7846_rx; |
| 548 | ts->msg.context = ts; |
| 549 | |
Russell King | f43aaba | 2006-01-19 12:26:57 +0000 | [diff] [blame] | 550 | if (request_irq(spi->irq, ads7846_irq, |
| 551 | SA_SAMPLE_RANDOM | SA_TRIGGER_FALLING, |
| 552 | spi->dev.bus_id, ts)) { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 553 | dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq); |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 554 | err = -EBUSY; |
| 555 | goto err_free_mem; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 556 | } |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 557 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 558 | dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 559 | |
| 560 | /* take a first sample, leaving nPENIRQ active; avoid |
| 561 | * the touchscreen, in case it's not connected. |
| 562 | */ |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 563 | (void) ads7846_read12_ser(&spi->dev, |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 564 | READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON); |
| 565 | |
| 566 | /* ads7843/7845 don't have temperature sensors, and |
| 567 | * use the other sensors a bit differently too |
| 568 | */ |
| 569 | if (ts->model == 7846) { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 570 | device_create_file(&spi->dev, &dev_attr_temp0); |
| 571 | device_create_file(&spi->dev, &dev_attr_temp1); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 572 | } |
| 573 | if (ts->model != 7845) |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 574 | device_create_file(&spi->dev, &dev_attr_vbatt); |
| 575 | device_create_file(&spi->dev, &dev_attr_vaux); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 576 | |
Imre Deak | 438f2a7 | 2006-04-11 23:41:32 -0400 | [diff] [blame^] | 577 | device_create_file(&spi->dev, &dev_attr_pen_down); |
| 578 | |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 579 | err = input_register_device(input_dev); |
| 580 | if (err) |
| 581 | goto err_free_irq; |
| 582 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 583 | return 0; |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 584 | |
| 585 | err_free_irq: |
| 586 | free_irq(spi->irq, ts); |
| 587 | err_free_mem: |
| 588 | input_free_device(input_dev); |
| 589 | kfree(ts); |
| 590 | return err; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 591 | } |
| 592 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 593 | static int __devexit ads7846_remove(struct spi_device *spi) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 594 | { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 595 | struct ads7846 *ts = dev_get_drvdata(&spi->dev); |
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 | ads7846_suspend(spi, PMSG_SUSPEND); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 598 | free_irq(ts->spi->irq, ts); |
| 599 | if (ts->irq_disabled) |
| 600 | enable_irq(ts->spi->irq); |
| 601 | |
Imre Deak | 438f2a7 | 2006-04-11 23:41:32 -0400 | [diff] [blame^] | 602 | device_remove_file(&spi->dev, &dev_attr_pen_down); |
| 603 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 604 | if (ts->model == 7846) { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 605 | device_remove_file(&spi->dev, &dev_attr_temp0); |
| 606 | device_remove_file(&spi->dev, &dev_attr_temp1); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 607 | } |
| 608 | if (ts->model != 7845) |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 609 | device_remove_file(&spi->dev, &dev_attr_vbatt); |
| 610 | device_remove_file(&spi->dev, &dev_attr_vaux); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 611 | |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 612 | input_unregister_device(ts->input); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 613 | kfree(ts); |
| 614 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 615 | dev_dbg(&spi->dev, "unregistered touchscreen\n"); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 616 | return 0; |
| 617 | } |
| 618 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 619 | static struct spi_driver ads7846_driver = { |
| 620 | .driver = { |
| 621 | .name = "ads7846", |
| 622 | .bus = &spi_bus_type, |
| 623 | .owner = THIS_MODULE, |
| 624 | }, |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 625 | .probe = ads7846_probe, |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 626 | .remove = __devexit_p(ads7846_remove), |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 627 | .suspend = ads7846_suspend, |
| 628 | .resume = ads7846_resume, |
| 629 | }; |
| 630 | |
| 631 | static int __init ads7846_init(void) |
| 632 | { |
| 633 | /* grr, board-specific init should stay out of drivers!! */ |
| 634 | |
| 635 | #ifdef CONFIG_ARCH_OMAP |
| 636 | if (machine_is_omap_osk()) { |
| 637 | /* GPIO4 = PENIRQ; GPIO6 = BUSY */ |
| 638 | omap_request_gpio(4); |
| 639 | omap_set_gpio_direction(4, 1); |
| 640 | omap_request_gpio(6); |
| 641 | omap_set_gpio_direction(6, 1); |
| 642 | } |
| 643 | // also TI 1510 Innovator, bitbanging through FPGA |
| 644 | // also Nokia 770 |
| 645 | // also Palm Tungsten T2 |
| 646 | #endif |
| 647 | |
| 648 | // PXA: |
| 649 | // also Dell Axim X50 |
| 650 | // also HP iPaq H191x/H192x/H415x/H435x |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 651 | // also Intel Lubbock (additional to UCB1400; as temperature sensor) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 652 | // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky) |
| 653 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 654 | // Atmel at91sam9261-EK uses ads7843 |
| 655 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 656 | // also various AMD Au1x00 devel boards |
| 657 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 658 | return spi_register_driver(&ads7846_driver); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 659 | } |
| 660 | module_init(ads7846_init); |
| 661 | |
| 662 | static void __exit ads7846_exit(void) |
| 663 | { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 664 | spi_unregister_driver(&ads7846_driver); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 665 | |
| 666 | #ifdef CONFIG_ARCH_OMAP |
| 667 | if (machine_is_omap_osk()) { |
| 668 | omap_free_gpio(4); |
| 669 | omap_free_gpio(6); |
| 670 | } |
| 671 | #endif |
| 672 | |
| 673 | } |
| 674 | module_exit(ads7846_exit); |
| 675 | |
| 676 | MODULE_DESCRIPTION("ADS7846 TouchScreen Driver"); |
| 677 | MODULE_LICENSE("GPL"); |