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 | */ |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 20 | #include <linux/hwmon.h> |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 21 | #include <linux/init.h> |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 22 | #include <linux/err.h> |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 23 | #include <linux/delay.h> |
| 24 | #include <linux/input.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/spi/spi.h> |
| 28 | #include <linux/spi/ads7846.h> |
Andrew Morton | 3ac8bf0 | 2006-03-26 01:37:33 -0800 | [diff] [blame] | 29 | #include <asm/irq.h> |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 30 | |
| 31 | #ifdef CONFIG_ARM |
| 32 | #include <asm/mach-types.h> |
| 33 | #ifdef CONFIG_ARCH_OMAP |
| 34 | #include <asm/arch/gpio.h> |
| 35 | #endif |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 36 | #endif |
| 37 | |
| 38 | |
| 39 | /* |
David Brownell | 9084533 | 2006-05-25 18:44:20 -0700 | [diff] [blame] | 40 | * This code has been heavily tested on a Nokia 770, and lightly |
| 41 | * tested on other ads7846 devices (OSK/Mistral, Lubbock). |
Nicolas Ferre | 969111e | 2007-02-28 23:51:03 -0500 | [diff] [blame] | 42 | * Support for ads7843 tested on Atmel at91sam926x-EK. |
| 43 | * Support for ads7845 has only been stubbed in. |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 44 | * |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 45 | * IRQ handling needs a workaround because of a shortcoming in handling |
| 46 | * edge triggered IRQs on some platforms like the OMAP1/2. These |
| 47 | * platforms don't handle the ARM lazy IRQ disabling properly, thus we |
| 48 | * have to maintain our own SW IRQ disabled status. This should be |
| 49 | * removed as soon as the affected platform's IRQ handling is fixed. |
| 50 | * |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 51 | * app note sbaa036 talks in more detail about accurate sampling... |
| 52 | * that ought to help in situations like LCDs inducing noise (which |
| 53 | * can also be helped by using synch signals) and more generally. |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 54 | * This driver tries to utilize the measures described in the app |
| 55 | * note. The strength of filtering can be set in the board-* specific |
| 56 | * files. |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 57 | */ |
| 58 | |
Imre Deak | 1936d59 | 2007-01-18 00:45:31 -0500 | [diff] [blame] | 59 | #define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */ |
| 60 | #define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */ |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 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, |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 69 | * *** WHICH NEED BYTESWAPPING *** and range adjustment. |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 70 | */ |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 71 | u16 x; |
| 72 | u16 y; |
| 73 | u16 z1, z2; |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 74 | int ignore; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | struct ads7846 { |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 78 | struct input_dev *input; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 79 | char phys[32]; |
| 80 | |
| 81 | struct spi_device *spi; |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 82 | |
| 83 | #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE) |
Dmitry Torokhov | 8dd5165 | 2006-11-02 23:34:09 -0500 | [diff] [blame] | 84 | struct attribute_group *attr_group; |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 85 | struct class_device *hwmon; |
| 86 | #endif |
| 87 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 88 | u16 model; |
| 89 | u16 vref_delay_usecs; |
| 90 | u16 x_plate_ohms; |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 91 | u16 pressure_max; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 92 | |
Imre Deak | 53a0ef89 | 2006-04-11 23:41:49 -0400 | [diff] [blame] | 93 | u8 read_x, read_y, read_z1, read_z2, pwrdown; |
| 94 | u16 dummy; /* for the pwrdown read */ |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 95 | struct ts_event tc; |
| 96 | |
Imre Deak | 53a0ef89 | 2006-04-11 23:41:49 -0400 | [diff] [blame] | 97 | struct spi_transfer xfer[10]; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 98 | struct spi_message msg[5]; |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 99 | struct spi_message *last_msg; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 100 | int msg_idx; |
| 101 | int read_cnt; |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 102 | int read_rep; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 103 | int last_read; |
| 104 | |
| 105 | u16 debounce_max; |
| 106 | u16 debounce_tol; |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 107 | u16 debounce_rep; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 108 | |
| 109 | spinlock_t lock; |
Imre Deak | 1936d59 | 2007-01-18 00:45:31 -0500 | [diff] [blame] | 110 | struct hrtimer timer; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 111 | unsigned pendown:1; /* P: lock */ |
| 112 | unsigned pending:1; /* P: lock */ |
| 113 | // FIXME remove "irq_disabled" |
| 114 | unsigned irq_disabled:1; /* P: lock */ |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 115 | unsigned disabled:1; |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 116 | |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 117 | int (*filter)(void *data, int data_idx, int *val); |
| 118 | void *filter_data; |
| 119 | void (*filter_cleanup)(void *data); |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 120 | int (*get_pendown_state)(void); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | /* leave chip selected when we're done, for quicker re-select? */ |
| 124 | #if 0 |
| 125 | #define CS_CHANGE(xfer) ((xfer).cs_change = 1) |
| 126 | #else |
| 127 | #define CS_CHANGE(xfer) ((xfer).cs_change = 0) |
| 128 | #endif |
| 129 | |
| 130 | /*--------------------------------------------------------------------------*/ |
| 131 | |
| 132 | /* The ADS7846 has touchscreen and other sensors. |
| 133 | * Earlier ads784x chips are somewhat compatible. |
| 134 | */ |
| 135 | #define ADS_START (1 << 7) |
| 136 | #define ADS_A2A1A0_d_y (1 << 4) /* differential */ |
| 137 | #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */ |
| 138 | #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */ |
| 139 | #define ADS_A2A1A0_d_x (5 << 4) /* differential */ |
| 140 | #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */ |
| 141 | #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */ |
| 142 | #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */ |
| 143 | #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */ |
| 144 | #define ADS_8_BIT (1 << 3) |
| 145 | #define ADS_12_BIT (0 << 3) |
| 146 | #define ADS_SER (1 << 2) /* non-differential */ |
| 147 | #define ADS_DFR (0 << 2) /* differential */ |
| 148 | #define ADS_PD10_PDOWN (0 << 0) /* lowpower mode + penirq */ |
| 149 | #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */ |
| 150 | #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */ |
| 151 | #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */ |
| 152 | |
| 153 | #define MAX_12BIT ((1<<12)-1) |
| 154 | |
| 155 | /* leave ADC powered up (disables penirq) between differential samples */ |
Imre Deak | de2defd | 2007-01-18 00:45:21 -0500 | [diff] [blame] | 156 | #define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \ |
| 157 | | ADS_12_BIT | ADS_DFR | \ |
| 158 | (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0)) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 159 | |
Imre Deak | de2defd | 2007-01-18 00:45:21 -0500 | [diff] [blame] | 160 | #define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref)) |
| 161 | #define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref)) |
| 162 | #define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref)) |
Imre Deak | 53a0ef89 | 2006-04-11 23:41:49 -0400 | [diff] [blame] | 163 | |
Imre Deak | de2defd | 2007-01-18 00:45:21 -0500 | [diff] [blame] | 164 | #define READ_X(vref) (READ_12BIT_DFR(x, 1, vref)) |
| 165 | #define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */ |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 166 | |
| 167 | /* single-ended samples need to first power up reference voltage; |
| 168 | * we leave both ADC and VREF powered |
| 169 | */ |
| 170 | #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \ |
| 171 | | ADS_12_BIT | ADS_SER) |
| 172 | |
Imre Deak | de2defd | 2007-01-18 00:45:21 -0500 | [diff] [blame] | 173 | #define REF_ON (READ_12BIT_DFR(x, 1, 1)) |
| 174 | #define REF_OFF (READ_12BIT_DFR(y, 0, 0)) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 175 | |
| 176 | /*--------------------------------------------------------------------------*/ |
| 177 | |
| 178 | /* |
| 179 | * Non-touchscreen sensors only use single-ended conversions. |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 180 | * The range is GND..vREF. The ads7843 and ads7835 must use external vREF; |
| 181 | * ads7846 lets that pin be unconnected, to use internal vREF. |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 182 | */ |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 183 | static unsigned vREF_mV; |
| 184 | module_param(vREF_mV, uint, 0); |
| 185 | MODULE_PARM_DESC(vREF_mV, "external vREF voltage, in milliVolts"); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 186 | |
| 187 | struct ser_req { |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 188 | u8 ref_on; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 189 | u8 command; |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 190 | u8 ref_off; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 191 | u16 scratch; |
| 192 | __be16 sample; |
| 193 | struct spi_message msg; |
| 194 | struct spi_transfer xfer[6]; |
| 195 | }; |
| 196 | |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 197 | static void ads7846_enable(struct ads7846 *ts); |
| 198 | static void ads7846_disable(struct ads7846 *ts); |
| 199 | |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 200 | static int device_suspended(struct device *dev) |
| 201 | { |
| 202 | struct ads7846 *ts = dev_get_drvdata(dev); |
| 203 | return dev->power.power_state.event != PM_EVENT_ON || ts->disabled; |
| 204 | } |
| 205 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 206 | static int ads7846_read12_ser(struct device *dev, unsigned command) |
| 207 | { |
| 208 | struct spi_device *spi = to_spi_device(dev); |
| 209 | struct ads7846 *ts = dev_get_drvdata(dev); |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 210 | struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 211 | int status; |
| 212 | int sample; |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 213 | int use_internal; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 214 | |
| 215 | if (!req) |
| 216 | return -ENOMEM; |
| 217 | |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 218 | spi_message_init(&req->msg); |
Vitaly Wool | 8275c64 | 2006-01-08 13:34:28 -0800 | [diff] [blame] | 219 | |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 220 | /* FIXME boards with ads7846 might use external vref instead ... */ |
| 221 | use_internal = (ts->model == 7846); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 222 | |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 223 | /* maybe turn on internal vREF, and let it settle */ |
| 224 | if (use_internal) { |
| 225 | req->ref_on = REF_ON; |
| 226 | req->xfer[0].tx_buf = &req->ref_on; |
| 227 | req->xfer[0].len = 1; |
| 228 | spi_message_add_tail(&req->xfer[0], &req->msg); |
| 229 | |
| 230 | req->xfer[1].rx_buf = &req->scratch; |
| 231 | req->xfer[1].len = 2; |
| 232 | |
| 233 | /* for 1uF, settle for 800 usec; no cap, 100 usec. */ |
| 234 | req->xfer[1].delay_usecs = ts->vref_delay_usecs; |
| 235 | spi_message_add_tail(&req->xfer[1], &req->msg); |
| 236 | } |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 237 | |
| 238 | /* take sample */ |
| 239 | req->command = (u8) command; |
| 240 | req->xfer[2].tx_buf = &req->command; |
| 241 | req->xfer[2].len = 1; |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 242 | spi_message_add_tail(&req->xfer[2], &req->msg); |
| 243 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 244 | req->xfer[3].rx_buf = &req->sample; |
| 245 | req->xfer[3].len = 2; |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 246 | spi_message_add_tail(&req->xfer[3], &req->msg); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 247 | |
| 248 | /* REVISIT: take a few more samples, and compare ... */ |
| 249 | |
Nicolas Ferre | 969111e | 2007-02-28 23:51:03 -0500 | [diff] [blame] | 250 | /* converter in low power mode & enable PENIRQ */ |
| 251 | req->ref_off = PWRDOWN; |
| 252 | req->xfer[4].tx_buf = &req->ref_off; |
| 253 | req->xfer[4].len = 1; |
| 254 | spi_message_add_tail(&req->xfer[4], &req->msg); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 255 | |
Nicolas Ferre | 969111e | 2007-02-28 23:51:03 -0500 | [diff] [blame] | 256 | req->xfer[5].rx_buf = &req->scratch; |
| 257 | req->xfer[5].len = 2; |
| 258 | CS_CHANGE(req->xfer[5]); |
| 259 | spi_message_add_tail(&req->xfer[5], &req->msg); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 260 | |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 261 | ts->irq_disabled = 1; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 262 | disable_irq(spi->irq); |
| 263 | status = spi_sync(spi, &req->msg); |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 264 | ts->irq_disabled = 0; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 265 | enable_irq(spi->irq); |
| 266 | |
| 267 | if (req->msg.status) |
| 268 | status = req->msg.status; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 269 | |
David Brownell | 9084533 | 2006-05-25 18:44:20 -0700 | [diff] [blame] | 270 | /* on-wire is a must-ignore bit, a BE12 value, then padding */ |
| 271 | sample = be16_to_cpu(req->sample); |
| 272 | sample = sample >> 3; |
| 273 | sample &= 0x0fff; |
| 274 | |
| 275 | kfree(req); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 276 | return status ? status : sample; |
| 277 | } |
| 278 | |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 279 | #if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE) |
| 280 | |
| 281 | #define SHOW(name, var, adjust) static ssize_t \ |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 282 | name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \ |
| 283 | { \ |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 284 | struct ads7846 *ts = dev_get_drvdata(dev); \ |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 285 | ssize_t v = ads7846_read12_ser(dev, \ |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 286 | READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \ |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 287 | if (v < 0) \ |
| 288 | return v; \ |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 289 | return sprintf(buf, "%u\n", adjust(ts, v)); \ |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 290 | } \ |
| 291 | static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL); |
| 292 | |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 293 | |
| 294 | /* Sysfs conventions report temperatures in millidegrees Celcius. |
| 295 | * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high |
| 296 | * accuracy scheme without calibration data. For now we won't try either; |
| 297 | * userspace sees raw sensor values, and must scale/calibrate appropriately. |
| 298 | */ |
| 299 | static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v) |
| 300 | { |
| 301 | return v; |
| 302 | } |
| 303 | |
| 304 | SHOW(temp0, temp0, null_adjust) /* temp1_input */ |
| 305 | SHOW(temp1, temp1, null_adjust) /* temp2_input */ |
| 306 | |
| 307 | |
| 308 | /* sysfs conventions report voltages in millivolts. We can convert voltages |
| 309 | * if we know vREF. userspace may need to scale vAUX to match the board's |
| 310 | * external resistors; we assume that vBATT only uses the internal ones. |
| 311 | */ |
| 312 | static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v) |
| 313 | { |
| 314 | unsigned retval = v; |
| 315 | |
| 316 | /* external resistors may scale vAUX into 0..vREF */ |
| 317 | retval *= vREF_mV; |
| 318 | retval = retval >> 12; |
| 319 | return retval; |
| 320 | } |
| 321 | |
| 322 | static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v) |
| 323 | { |
| 324 | unsigned retval = vaux_adjust(ts, v); |
| 325 | |
| 326 | /* ads7846 has a resistor ladder to scale this signal down */ |
| 327 | if (ts->model == 7846) |
| 328 | retval *= 4; |
| 329 | return retval; |
| 330 | } |
| 331 | |
| 332 | SHOW(in0_input, vaux, vaux_adjust) |
| 333 | SHOW(in1_input, vbatt, vbatt_adjust) |
| 334 | |
| 335 | |
| 336 | static struct attribute *ads7846_attributes[] = { |
| 337 | &dev_attr_temp0.attr, |
| 338 | &dev_attr_temp1.attr, |
| 339 | &dev_attr_in0_input.attr, |
| 340 | &dev_attr_in1_input.attr, |
| 341 | NULL, |
| 342 | }; |
| 343 | |
| 344 | static struct attribute_group ads7846_attr_group = { |
| 345 | .attrs = ads7846_attributes, |
| 346 | }; |
| 347 | |
| 348 | static struct attribute *ads7843_attributes[] = { |
| 349 | &dev_attr_in0_input.attr, |
| 350 | &dev_attr_in1_input.attr, |
| 351 | NULL, |
| 352 | }; |
| 353 | |
| 354 | static struct attribute_group ads7843_attr_group = { |
| 355 | .attrs = ads7843_attributes, |
| 356 | }; |
| 357 | |
| 358 | static struct attribute *ads7845_attributes[] = { |
| 359 | &dev_attr_in0_input.attr, |
| 360 | NULL, |
| 361 | }; |
| 362 | |
| 363 | static struct attribute_group ads7845_attr_group = { |
| 364 | .attrs = ads7845_attributes, |
| 365 | }; |
| 366 | |
| 367 | static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts) |
| 368 | { |
| 369 | struct class_device *hwmon; |
| 370 | int err; |
| 371 | |
| 372 | /* hwmon sensors need a reference voltage */ |
| 373 | switch (ts->model) { |
| 374 | case 7846: |
| 375 | if (!vREF_mV) { |
| 376 | dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n"); |
| 377 | vREF_mV = 2500; |
| 378 | } |
| 379 | break; |
| 380 | case 7845: |
| 381 | case 7843: |
| 382 | if (!vREF_mV) { |
| 383 | dev_warn(&spi->dev, |
| 384 | "external vREF for ADS%d not specified\n", |
| 385 | ts->model); |
| 386 | return 0; |
| 387 | } |
| 388 | break; |
| 389 | } |
| 390 | |
| 391 | /* different chips have different sensor groups */ |
| 392 | switch (ts->model) { |
| 393 | case 7846: |
| 394 | ts->attr_group = &ads7846_attr_group; |
| 395 | break; |
| 396 | case 7845: |
| 397 | ts->attr_group = &ads7845_attr_group; |
| 398 | break; |
| 399 | case 7843: |
| 400 | ts->attr_group = &ads7843_attr_group; |
| 401 | break; |
| 402 | default: |
| 403 | dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model); |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | err = sysfs_create_group(&spi->dev.kobj, ts->attr_group); |
| 408 | if (err) |
| 409 | return err; |
| 410 | |
| 411 | hwmon = hwmon_device_register(&spi->dev); |
| 412 | if (IS_ERR(hwmon)) { |
| 413 | sysfs_remove_group(&spi->dev.kobj, ts->attr_group); |
| 414 | return PTR_ERR(hwmon); |
| 415 | } |
| 416 | |
| 417 | ts->hwmon = hwmon; |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | static void ads784x_hwmon_unregister(struct spi_device *spi, |
| 422 | struct ads7846 *ts) |
| 423 | { |
| 424 | if (ts->hwmon) { |
| 425 | sysfs_remove_group(&spi->dev.kobj, ts->attr_group); |
| 426 | hwmon_device_unregister(ts->hwmon); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | #else |
| 431 | static inline int ads784x_hwmon_register(struct spi_device *spi, |
| 432 | struct ads7846 *ts) |
| 433 | { |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | static inline void ads784x_hwmon_unregister(struct spi_device *spi, |
| 438 | struct ads7846 *ts) |
| 439 | { |
| 440 | } |
| 441 | #endif |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 442 | |
Imre Deak | 438f2a7 | 2006-04-11 23:41:32 -0400 | [diff] [blame] | 443 | static int is_pen_down(struct device *dev) |
| 444 | { |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 445 | struct ads7846 *ts = dev_get_drvdata(dev); |
Imre Deak | 438f2a7 | 2006-04-11 23:41:32 -0400 | [diff] [blame] | 446 | |
| 447 | return ts->pendown; |
| 448 | } |
| 449 | |
| 450 | static ssize_t ads7846_pen_down_show(struct device *dev, |
| 451 | struct device_attribute *attr, char *buf) |
| 452 | { |
| 453 | return sprintf(buf, "%u\n", is_pen_down(dev)); |
| 454 | } |
| 455 | |
| 456 | static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL); |
| 457 | |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 458 | static ssize_t ads7846_disable_show(struct device *dev, |
| 459 | struct device_attribute *attr, char *buf) |
| 460 | { |
| 461 | struct ads7846 *ts = dev_get_drvdata(dev); |
| 462 | |
| 463 | return sprintf(buf, "%u\n", ts->disabled); |
| 464 | } |
| 465 | |
| 466 | static ssize_t ads7846_disable_store(struct device *dev, |
| 467 | struct device_attribute *attr, |
| 468 | const char *buf, size_t count) |
| 469 | { |
| 470 | struct ads7846 *ts = dev_get_drvdata(dev); |
| 471 | char *endp; |
| 472 | int i; |
| 473 | |
| 474 | i = simple_strtoul(buf, &endp, 10); |
| 475 | spin_lock_irq(&ts->lock); |
| 476 | |
| 477 | if (i) |
| 478 | ads7846_disable(ts); |
| 479 | else |
| 480 | ads7846_enable(ts); |
| 481 | |
| 482 | spin_unlock_irq(&ts->lock); |
| 483 | |
| 484 | return count; |
| 485 | } |
| 486 | |
| 487 | static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store); |
| 488 | |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 489 | static struct attribute *ads784x_attributes[] = { |
Dmitry Torokhov | 8dd5165 | 2006-11-02 23:34:09 -0500 | [diff] [blame] | 490 | &dev_attr_pen_down.attr, |
| 491 | &dev_attr_disable.attr, |
| 492 | NULL, |
| 493 | }; |
| 494 | |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 495 | static struct attribute_group ads784x_attr_group = { |
| 496 | .attrs = ads784x_attributes, |
Dmitry Torokhov | 8dd5165 | 2006-11-02 23:34:09 -0500 | [diff] [blame] | 497 | }; |
| 498 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 499 | /*--------------------------------------------------------------------------*/ |
| 500 | |
| 501 | /* |
| 502 | * PENIRQ only kicks the timer. The timer only reissues the SPI transfer, |
| 503 | * to retrieve touchscreen status. |
| 504 | * |
| 505 | * The SPI transfer completion callback does the real work. It reports |
| 506 | * touchscreen events and reactivates the timer (or IRQ) as appropriate. |
| 507 | */ |
| 508 | |
| 509 | static void ads7846_rx(void *ads) |
| 510 | { |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 511 | struct ads7846 *ts = ads; |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 512 | unsigned Rt; |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 513 | u16 x, y, z1, z2; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 514 | |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 515 | /* ads7846_rx_val() did in-place conversion (including byteswap) from |
| 516 | * on-the-wire format as part of debouncing to get stable readings. |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 517 | */ |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 518 | x = ts->tc.x; |
| 519 | y = ts->tc.y; |
| 520 | z1 = ts->tc.z1; |
| 521 | z2 = ts->tc.z2; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 522 | |
| 523 | /* range filtering */ |
| 524 | if (x == MAX_12BIT) |
| 525 | x = 0; |
| 526 | |
Imre Deak | 15e3589 | 2007-01-18 00:45:43 -0500 | [diff] [blame] | 527 | if (likely(x && z1)) { |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 528 | /* compute touch pressure resistance using equation #2 */ |
| 529 | Rt = z2; |
| 530 | Rt -= z1; |
| 531 | Rt *= x; |
| 532 | Rt *= ts->x_plate_ohms; |
| 533 | Rt /= z1; |
| 534 | Rt = (Rt + 2047) >> 12; |
| 535 | } else |
| 536 | Rt = 0; |
| 537 | |
Nicolas Ferre | 969111e | 2007-02-28 23:51:03 -0500 | [diff] [blame] | 538 | if (ts->model == 7843) |
| 539 | Rt = ts->pressure_max / 2; |
| 540 | |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 541 | /* Sample found inconsistent by debouncing or pressure is beyond |
Imre Deak | 1936d59 | 2007-01-18 00:45:31 -0500 | [diff] [blame] | 542 | * the maximum. Don't report it to user space, repeat at least |
| 543 | * once more the measurement |
| 544 | */ |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 545 | if (ts->tc.ignore || Rt > ts->pressure_max) { |
Imre Deak | 15e3589 | 2007-01-18 00:45:43 -0500 | [diff] [blame] | 546 | #ifdef VERBOSE |
| 547 | pr_debug("%s: ignored %d pressure %d\n", |
| 548 | ts->spi->dev.bus_id, ts->tc.ignore, Rt); |
| 549 | #endif |
Imre Deak | 1936d59 | 2007-01-18 00:45:31 -0500 | [diff] [blame] | 550 | hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD), |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 551 | HRTIMER_MODE_REL); |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 552 | return; |
| 553 | } |
| 554 | |
Imre Deak | 15e3589 | 2007-01-18 00:45:43 -0500 | [diff] [blame] | 555 | /* NOTE: We can't rely on the pressure to determine the pen down |
| 556 | * state, even this controller has a pressure sensor. The pressure |
| 557 | * value can fluctuate for quite a while after lifting the pen and |
| 558 | * in some cases may not even settle at the expected value. |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 559 | * |
Imre Deak | 15e3589 | 2007-01-18 00:45:43 -0500 | [diff] [blame] | 560 | * The only safe way to check for the pen up condition is in the |
| 561 | * timer by reading the pen signal state (it's a GPIO _and_ IRQ). |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 562 | */ |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 563 | if (Rt) { |
Imre Deak | 15e3589 | 2007-01-18 00:45:43 -0500 | [diff] [blame] | 564 | struct input_dev *input = ts->input; |
Imre Deak | ae82d5a | 2006-04-26 00:12:14 -0400 | [diff] [blame] | 565 | |
Imre Deak | 15e3589 | 2007-01-18 00:45:43 -0500 | [diff] [blame] | 566 | if (!ts->pendown) { |
| 567 | input_report_key(input, BTN_TOUCH, 1); |
| 568 | ts->pendown = 1; |
| 569 | #ifdef VERBOSE |
| 570 | dev_dbg(&ts->spi->dev, "DOWN\n"); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 571 | #endif |
Imre Deak | 15e3589 | 2007-01-18 00:45:43 -0500 | [diff] [blame] | 572 | } |
| 573 | input_report_abs(input, ABS_X, x); |
| 574 | input_report_abs(input, ABS_Y, y); |
| 575 | input_report_abs(input, ABS_PRESSURE, Rt); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 576 | |
Imre Deak | 15e3589 | 2007-01-18 00:45:43 -0500 | [diff] [blame] | 577 | input_sync(input); |
| 578 | #ifdef VERBOSE |
| 579 | dev_dbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt); |
| 580 | #endif |
| 581 | } |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 582 | |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 583 | hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD), |
| 584 | HRTIMER_MODE_REL); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 585 | } |
| 586 | |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 587 | static int ads7846_debounce(void *ads, int data_idx, int *val) |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 588 | { |
| 589 | struct ads7846 *ts = ads; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 590 | |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 591 | if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) { |
| 592 | /* Start over collecting consistent readings. */ |
| 593 | ts->read_rep = 0; |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 594 | /* Repeat it, if this was the first read or the read |
| 595 | * wasn't consistent enough. */ |
| 596 | if (ts->read_cnt < ts->debounce_max) { |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 597 | ts->last_read = *val; |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 598 | ts->read_cnt++; |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 599 | return ADS7846_FILTER_REPEAT; |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 600 | } else { |
| 601 | /* Maximum number of debouncing reached and still |
| 602 | * not enough number of consistent readings. Abort |
| 603 | * the whole sample, repeat it in the next sampling |
| 604 | * period. |
| 605 | */ |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 606 | ts->read_cnt = 0; |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 607 | return ADS7846_FILTER_IGNORE; |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 608 | } |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 609 | } else { |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 610 | if (++ts->read_rep > ts->debounce_rep) { |
| 611 | /* Got a good reading for this coordinate, |
| 612 | * go for the next one. */ |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 613 | ts->read_cnt = 0; |
| 614 | ts->read_rep = 0; |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 615 | return ADS7846_FILTER_OK; |
| 616 | } else { |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 617 | /* Read more values that are consistent. */ |
| 618 | ts->read_cnt++; |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 619 | return ADS7846_FILTER_REPEAT; |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | static int ads7846_no_filter(void *ads, int data_idx, int *val) |
| 625 | { |
| 626 | return ADS7846_FILTER_OK; |
| 627 | } |
| 628 | |
| 629 | static void ads7846_rx_val(void *ads) |
| 630 | { |
| 631 | struct ads7846 *ts = ads; |
| 632 | struct spi_message *m; |
| 633 | struct spi_transfer *t; |
| 634 | u16 *rx_val; |
| 635 | int val; |
| 636 | int action; |
| 637 | int status; |
| 638 | |
| 639 | m = &ts->msg[ts->msg_idx]; |
| 640 | t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list); |
| 641 | rx_val = t->rx_buf; |
| 642 | |
| 643 | /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding; |
| 644 | * built from two 8 bit values written msb-first. |
| 645 | */ |
| 646 | val = be16_to_cpu(*rx_val) >> 3; |
| 647 | |
| 648 | action = ts->filter(ts->filter_data, ts->msg_idx, &val); |
| 649 | switch (action) { |
| 650 | case ADS7846_FILTER_REPEAT: |
| 651 | break; |
| 652 | case ADS7846_FILTER_IGNORE: |
| 653 | ts->tc.ignore = 1; |
| 654 | /* Last message will contain ads7846_rx() as the |
| 655 | * completion function. |
| 656 | */ |
| 657 | m = ts->last_msg; |
| 658 | break; |
| 659 | case ADS7846_FILTER_OK: |
| 660 | *rx_val = val; |
| 661 | ts->tc.ignore = 0; |
| 662 | m = &ts->msg[++ts->msg_idx]; |
| 663 | break; |
| 664 | default: |
| 665 | BUG(); |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 666 | } |
| 667 | status = spi_async(ts->spi, m); |
| 668 | if (status) |
| 669 | dev_err(&ts->spi->dev, "spi_async --> %d\n", |
| 670 | status); |
| 671 | } |
| 672 | |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 673 | static enum hrtimer_restart ads7846_timer(struct hrtimer *handle) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 674 | { |
Imre Deak | 1936d59 | 2007-01-18 00:45:31 -0500 | [diff] [blame] | 675 | struct ads7846 *ts = container_of(handle, struct ads7846, timer); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 676 | int status = 0; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 677 | |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 678 | spin_lock_irq(&ts->lock); |
| 679 | |
Imre Deak | 15e3589 | 2007-01-18 00:45:43 -0500 | [diff] [blame] | 680 | if (unlikely(!ts->get_pendown_state() || |
| 681 | device_suspended(&ts->spi->dev))) { |
| 682 | if (ts->pendown) { |
| 683 | struct input_dev *input = ts->input; |
| 684 | |
| 685 | input_report_key(input, BTN_TOUCH, 0); |
| 686 | input_report_abs(input, ABS_PRESSURE, 0); |
| 687 | input_sync(input); |
| 688 | |
| 689 | ts->pendown = 0; |
| 690 | #ifdef VERBOSE |
| 691 | dev_dbg(&ts->spi->dev, "UP\n"); |
| 692 | #endif |
| 693 | } |
| 694 | |
David Brownell | 9084533 | 2006-05-25 18:44:20 -0700 | [diff] [blame] | 695 | /* measurement cycle ended */ |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 696 | if (!device_suspended(&ts->spi->dev)) { |
| 697 | ts->irq_disabled = 0; |
| 698 | enable_irq(ts->spi->irq); |
| 699 | } |
| 700 | ts->pending = 0; |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 701 | } else { |
| 702 | /* pen is still down, continue with the measurement */ |
| 703 | ts->msg_idx = 0; |
| 704 | status = spi_async(ts->spi, &ts->msg[0]); |
| 705 | if (status) |
| 706 | dev_err(&ts->spi->dev, "spi_async --> %d\n", status); |
| 707 | } |
| 708 | |
| 709 | spin_unlock_irq(&ts->lock); |
Imre Deak | 1936d59 | 2007-01-18 00:45:31 -0500 | [diff] [blame] | 710 | return HRTIMER_NORESTART; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 711 | } |
| 712 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 713 | static irqreturn_t ads7846_irq(int irq, void *handle) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 714 | { |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 715 | struct ads7846 *ts = handle; |
| 716 | unsigned long flags; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 717 | |
| 718 | spin_lock_irqsave(&ts->lock, flags); |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 719 | if (likely(ts->get_pendown_state())) { |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 720 | if (!ts->irq_disabled) { |
David Brownell | 9084533 | 2006-05-25 18:44:20 -0700 | [diff] [blame] | 721 | /* The ARM do_simple_IRQ() dispatcher doesn't act |
| 722 | * like the other dispatchers: it will report IRQs |
| 723 | * even after they've been disabled. We work around |
| 724 | * that here. (The "generic irq" framework may help...) |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 725 | */ |
| 726 | ts->irq_disabled = 1; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 727 | disable_irq(ts->spi->irq); |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 728 | ts->pending = 1; |
Imre Deak | 1936d59 | 2007-01-18 00:45:31 -0500 | [diff] [blame] | 729 | hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY), |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 730 | HRTIMER_MODE_REL); |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | spin_unlock_irqrestore(&ts->lock, flags); |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 734 | |
| 735 | return IRQ_HANDLED; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | /*--------------------------------------------------------------------------*/ |
| 739 | |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 740 | /* Must be called with ts->lock held */ |
| 741 | static void ads7846_disable(struct ads7846 *ts) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 742 | { |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 743 | if (ts->disabled) |
| 744 | return; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 745 | |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 746 | ts->disabled = 1; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 747 | |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 748 | /* are we waiting for IRQ, or polling? */ |
| 749 | if (!ts->pending) { |
| 750 | ts->irq_disabled = 1; |
| 751 | disable_irq(ts->spi->irq); |
| 752 | } else { |
| 753 | /* the timer will run at least once more, and |
| 754 | * leave everything in a clean state, IRQ disabled |
| 755 | */ |
| 756 | while (ts->pending) { |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 757 | spin_unlock_irq(&ts->lock); |
Juha Yrjola | c4febb9 | 2006-04-11 23:42:25 -0400 | [diff] [blame] | 758 | msleep(1); |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 759 | spin_lock_irq(&ts->lock); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
| 763 | /* we know the chip's in lowpower mode since we always |
| 764 | * leave it that way after every request |
| 765 | */ |
| 766 | |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | /* Must be called with ts->lock held */ |
| 770 | static void ads7846_enable(struct ads7846 *ts) |
| 771 | { |
| 772 | if (!ts->disabled) |
| 773 | return; |
| 774 | |
| 775 | ts->disabled = 0; |
| 776 | ts->irq_disabled = 0; |
| 777 | enable_irq(ts->spi->irq); |
| 778 | } |
| 779 | |
| 780 | static int ads7846_suspend(struct spi_device *spi, pm_message_t message) |
| 781 | { |
| 782 | struct ads7846 *ts = dev_get_drvdata(&spi->dev); |
| 783 | |
| 784 | spin_lock_irq(&ts->lock); |
| 785 | |
| 786 | spi->dev.power.power_state = message; |
| 787 | ads7846_disable(ts); |
| 788 | |
| 789 | spin_unlock_irq(&ts->lock); |
| 790 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 791 | return 0; |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 792 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 793 | } |
| 794 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 795 | static int ads7846_resume(struct spi_device *spi) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 796 | { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 797 | struct ads7846 *ts = dev_get_drvdata(&spi->dev); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 798 | |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 799 | spin_lock_irq(&ts->lock); |
| 800 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 801 | spi->dev.power.power_state = PMSG_ON; |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 802 | ads7846_enable(ts); |
| 803 | |
| 804 | spin_unlock_irq(&ts->lock); |
| 805 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 806 | return 0; |
| 807 | } |
| 808 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 809 | static int __devinit ads7846_probe(struct spi_device *spi) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 810 | { |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 811 | struct ads7846 *ts; |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 812 | struct input_dev *input_dev; |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 813 | struct ads7846_platform_data *pdata = spi->dev.platform_data; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 814 | struct spi_message *m; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 815 | struct spi_transfer *x; |
Imre Deak | de2defd | 2007-01-18 00:45:21 -0500 | [diff] [blame] | 816 | int vref; |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 817 | int err; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 818 | |
| 819 | if (!spi->irq) { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 820 | dev_dbg(&spi->dev, "no IRQ?\n"); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 821 | return -ENODEV; |
| 822 | } |
| 823 | |
| 824 | if (!pdata) { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 825 | dev_dbg(&spi->dev, "no platform data?\n"); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 826 | return -ENODEV; |
| 827 | } |
| 828 | |
| 829 | /* don't exceed max specified sample rate */ |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 830 | if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 831 | dev_dbg(&spi->dev, "f(sample) %d KHz?\n", |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 832 | (spi->max_speed_hz/SAMPLE_BITS)/1000); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 833 | return -EINVAL; |
| 834 | } |
| 835 | |
David Brownell | 9084533 | 2006-05-25 18:44:20 -0700 | [diff] [blame] | 836 | /* REVISIT when the irq can be triggered active-low, or if for some |
| 837 | * reason the touchscreen isn't hooked up, we don't need to access |
| 838 | * the pendown state. |
| 839 | */ |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 840 | if (pdata->get_pendown_state == NULL) { |
| 841 | dev_dbg(&spi->dev, "no get_pendown_state function?\n"); |
| 842 | return -EINVAL; |
| 843 | } |
| 844 | |
David Brownell | 9084533 | 2006-05-25 18:44:20 -0700 | [diff] [blame] | 845 | /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except |
| 846 | * that even if the hardware can do that, the SPI controller driver |
| 847 | * 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] | 848 | */ |
David Brownell | 9084533 | 2006-05-25 18:44:20 -0700 | [diff] [blame] | 849 | spi->bits_per_word = 8; |
Imre Deak | 7937e86 | 2007-01-18 00:45:38 -0500 | [diff] [blame] | 850 | spi->mode = SPI_MODE_1; |
| 851 | err = spi_setup(spi); |
| 852 | if (err < 0) |
| 853 | return err; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 854 | |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 855 | ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL); |
| 856 | input_dev = input_allocate_device(); |
| 857 | if (!ts || !input_dev) { |
| 858 | err = -ENOMEM; |
| 859 | goto err_free_mem; |
| 860 | } |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 861 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 862 | dev_set_drvdata(&spi->dev, ts); |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 863 | spi->dev.power.power_state = PMSG_ON; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 864 | |
| 865 | ts->spi = spi; |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 866 | ts->input = input_dev; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 867 | |
Thomas Gleixner | c9cb2e3 | 2007-02-16 01:27:49 -0800 | [diff] [blame] | 868 | hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 869 | ts->timer.function = ads7846_timer; |
| 870 | |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 871 | spin_lock_init(&ts->lock); |
| 872 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 873 | ts->model = pdata->model ? : 7846; |
| 874 | ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100; |
| 875 | ts->x_plate_ohms = pdata->x_plate_ohms ? : 400; |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 876 | ts->pressure_max = pdata->pressure_max ? : ~0; |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 877 | |
| 878 | if (pdata->filter != NULL) { |
| 879 | if (pdata->filter_init != NULL) { |
| 880 | err = pdata->filter_init(pdata, &ts->filter_data); |
| 881 | if (err < 0) |
| 882 | goto err_free_mem; |
| 883 | } |
| 884 | ts->filter = pdata->filter; |
| 885 | ts->filter_cleanup = pdata->filter_cleanup; |
| 886 | } else if (pdata->debounce_max) { |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 887 | ts->debounce_max = pdata->debounce_max; |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 888 | if (ts->debounce_max < 2) |
| 889 | ts->debounce_max = 2; |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 890 | ts->debounce_tol = pdata->debounce_tol; |
| 891 | ts->debounce_rep = pdata->debounce_rep; |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 892 | ts->filter = ads7846_debounce; |
| 893 | ts->filter_data = ts; |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 894 | } else |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 895 | ts->filter = ads7846_no_filter; |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 896 | ts->get_pendown_state = pdata->get_pendown_state; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 897 | |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 898 | snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 899 | |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 900 | input_dev->name = "ADS784x Touchscreen"; |
| 901 | input_dev->phys = ts->phys; |
Dmitry Torokhov | a5394fb0 | 2007-04-12 01:35:14 -0400 | [diff] [blame] | 902 | input_dev->dev.parent = &spi->dev; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 903 | |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 904 | input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS); |
| 905 | input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH); |
| 906 | input_set_abs_params(input_dev, ABS_X, |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 907 | pdata->x_min ? : 0, |
| 908 | pdata->x_max ? : MAX_12BIT, |
| 909 | 0, 0); |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 910 | input_set_abs_params(input_dev, ABS_Y, |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 911 | pdata->y_min ? : 0, |
| 912 | pdata->y_max ? : MAX_12BIT, |
| 913 | 0, 0); |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 914 | input_set_abs_params(input_dev, ABS_PRESSURE, |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 915 | pdata->pressure_min, pdata->pressure_max, 0, 0); |
| 916 | |
Imre Deak | de2defd | 2007-01-18 00:45:21 -0500 | [diff] [blame] | 917 | vref = pdata->keep_vref_on; |
| 918 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 919 | /* set up the transfers to read touchscreen state; this assumes we |
| 920 | * use formula #2 for pressure, not #3. |
| 921 | */ |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 922 | m = &ts->msg[0]; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 923 | x = ts->xfer; |
| 924 | |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 925 | spi_message_init(m); |
| 926 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 927 | /* y- still on; turn on only y+ (and ADC) */ |
Imre Deak | de2defd | 2007-01-18 00:45:21 -0500 | [diff] [blame] | 928 | ts->read_y = READ_Y(vref); |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 929 | x->tx_buf = &ts->read_y; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 930 | x->len = 1; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 931 | spi_message_add_tail(x, m); |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 932 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 933 | x++; |
| 934 | x->rx_buf = &ts->tc.y; |
| 935 | x->len = 2; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 936 | spi_message_add_tail(x, m); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 937 | |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 938 | m->complete = ads7846_rx_val; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 939 | m->context = ts; |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 940 | |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 941 | m++; |
| 942 | spi_message_init(m); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 943 | |
| 944 | /* turn y- off, x+ on, then leave in lowpower */ |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 945 | x++; |
Imre Deak | de2defd | 2007-01-18 00:45:21 -0500 | [diff] [blame] | 946 | ts->read_x = READ_X(vref); |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 947 | x->tx_buf = &ts->read_x; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 948 | x->len = 1; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 949 | spi_message_add_tail(x, m); |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 950 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 951 | x++; |
| 952 | x->rx_buf = &ts->tc.x; |
| 953 | x->len = 2; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 954 | spi_message_add_tail(x, m); |
| 955 | |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 956 | m->complete = ads7846_rx_val; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 957 | m->context = ts; |
| 958 | |
| 959 | /* turn y+ off, x- on; we'll use formula #2 */ |
| 960 | if (ts->model == 7846) { |
| 961 | m++; |
| 962 | spi_message_init(m); |
| 963 | |
| 964 | x++; |
Imre Deak | de2defd | 2007-01-18 00:45:21 -0500 | [diff] [blame] | 965 | ts->read_z1 = READ_Z1(vref); |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 966 | x->tx_buf = &ts->read_z1; |
| 967 | x->len = 1; |
| 968 | spi_message_add_tail(x, m); |
| 969 | |
| 970 | x++; |
| 971 | x->rx_buf = &ts->tc.z1; |
| 972 | x->len = 2; |
| 973 | spi_message_add_tail(x, m); |
| 974 | |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 975 | m->complete = ads7846_rx_val; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 976 | m->context = ts; |
| 977 | |
| 978 | m++; |
| 979 | spi_message_init(m); |
| 980 | |
| 981 | x++; |
Imre Deak | de2defd | 2007-01-18 00:45:21 -0500 | [diff] [blame] | 982 | ts->read_z2 = READ_Z2(vref); |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 983 | x->tx_buf = &ts->read_z2; |
| 984 | x->len = 1; |
| 985 | spi_message_add_tail(x, m); |
| 986 | |
| 987 | x++; |
| 988 | x->rx_buf = &ts->tc.z2; |
| 989 | x->len = 2; |
| 990 | spi_message_add_tail(x, m); |
| 991 | |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 992 | m->complete = ads7846_rx_val; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 993 | m->context = ts; |
| 994 | } |
Imre Deak | 53a0ef89 | 2006-04-11 23:41:49 -0400 | [diff] [blame] | 995 | |
| 996 | /* power down */ |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 997 | m++; |
| 998 | spi_message_init(m); |
| 999 | |
Imre Deak | 53a0ef89 | 2006-04-11 23:41:49 -0400 | [diff] [blame] | 1000 | x++; |
| 1001 | ts->pwrdown = PWRDOWN; |
| 1002 | x->tx_buf = &ts->pwrdown; |
| 1003 | x->len = 1; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 1004 | spi_message_add_tail(x, m); |
Imre Deak | 53a0ef89 | 2006-04-11 23:41:49 -0400 | [diff] [blame] | 1005 | |
| 1006 | x++; |
| 1007 | x->rx_buf = &ts->dummy; |
| 1008 | x->len = 2; |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 1009 | CS_CHANGE(*x); |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 1010 | spi_message_add_tail(x, m); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1011 | |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 1012 | m->complete = ads7846_rx; |
| 1013 | m->context = ts; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1014 | |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 1015 | ts->last_msg = m; |
| 1016 | |
Thomas Gleixner | dace145 | 2006-07-01 19:29:38 -0700 | [diff] [blame] | 1017 | if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING, |
David Brownell | 9084533 | 2006-05-25 18:44:20 -0700 | [diff] [blame] | 1018 | spi->dev.driver->name, ts)) { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1019 | dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq); |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 1020 | err = -EBUSY; |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 1021 | goto err_cleanup_filter; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1022 | } |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1023 | |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 1024 | err = ads784x_hwmon_register(spi, ts); |
| 1025 | if (err) |
| 1026 | goto err_free_irq; |
| 1027 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1028 | dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1029 | |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 1030 | /* take a first sample, leaving nPENIRQ active and vREF off; avoid |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1031 | * the touchscreen, in case it's not connected. |
| 1032 | */ |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1033 | (void) ads7846_read12_ser(&spi->dev, |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1034 | READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON); |
| 1035 | |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 1036 | err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group); |
Dmitry Torokhov | 8dd5165 | 2006-11-02 23:34:09 -0500 | [diff] [blame] | 1037 | if (err) |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 1038 | goto err_remove_hwmon; |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 1039 | |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 1040 | err = input_register_device(input_dev); |
| 1041 | if (err) |
Dmitry Torokhov | 8dd5165 | 2006-11-02 23:34:09 -0500 | [diff] [blame] | 1042 | goto err_remove_attr_group; |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 1043 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1044 | return 0; |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 1045 | |
Dmitry Torokhov | 8dd5165 | 2006-11-02 23:34:09 -0500 | [diff] [blame] | 1046 | err_remove_attr_group: |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 1047 | sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group); |
| 1048 | err_remove_hwmon: |
| 1049 | ads784x_hwmon_unregister(spi, ts); |
Dmitry Torokhov | 8dd5165 | 2006-11-02 23:34:09 -0500 | [diff] [blame] | 1050 | err_free_irq: |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 1051 | free_irq(spi->irq, ts); |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 1052 | err_cleanup_filter: |
| 1053 | if (ts->filter_cleanup) |
| 1054 | ts->filter_cleanup(ts->filter_data); |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 1055 | err_free_mem: |
| 1056 | input_free_device(input_dev); |
| 1057 | kfree(ts); |
| 1058 | return err; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1059 | } |
| 1060 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1061 | static int __devexit ads7846_remove(struct spi_device *spi) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1062 | { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1063 | struct ads7846 *ts = dev_get_drvdata(&spi->dev); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1064 | |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 1065 | ads784x_hwmon_unregister(spi, ts); |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 1066 | input_unregister_device(ts->input); |
| 1067 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1068 | ads7846_suspend(spi, PMSG_SUSPEND); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1069 | |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 1070 | sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1071 | |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 1072 | free_irq(ts->spi->irq, ts); |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 1073 | /* suspend left the IRQ disabled */ |
| 1074 | enable_irq(ts->spi->irq); |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 1075 | |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 1076 | if (ts->filter_cleanup) |
| 1077 | ts->filter_cleanup(ts->filter_data); |
| 1078 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1079 | kfree(ts); |
| 1080 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1081 | dev_dbg(&spi->dev, "unregistered touchscreen\n"); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1082 | return 0; |
| 1083 | } |
| 1084 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1085 | static struct spi_driver ads7846_driver = { |
| 1086 | .driver = { |
| 1087 | .name = "ads7846", |
| 1088 | .bus = &spi_bus_type, |
| 1089 | .owner = THIS_MODULE, |
| 1090 | }, |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1091 | .probe = ads7846_probe, |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1092 | .remove = __devexit_p(ads7846_remove), |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1093 | .suspend = ads7846_suspend, |
| 1094 | .resume = ads7846_resume, |
| 1095 | }; |
| 1096 | |
| 1097 | static int __init ads7846_init(void) |
| 1098 | { |
| 1099 | /* grr, board-specific init should stay out of drivers!! */ |
| 1100 | |
| 1101 | #ifdef CONFIG_ARCH_OMAP |
| 1102 | if (machine_is_omap_osk()) { |
| 1103 | /* GPIO4 = PENIRQ; GPIO6 = BUSY */ |
| 1104 | omap_request_gpio(4); |
| 1105 | omap_set_gpio_direction(4, 1); |
| 1106 | omap_request_gpio(6); |
| 1107 | omap_set_gpio_direction(6, 1); |
| 1108 | } |
| 1109 | // also TI 1510 Innovator, bitbanging through FPGA |
| 1110 | // also Nokia 770 |
| 1111 | // also Palm Tungsten T2 |
| 1112 | #endif |
| 1113 | |
| 1114 | // PXA: |
| 1115 | // also Dell Axim X50 |
| 1116 | // also HP iPaq H191x/H192x/H415x/H435x |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1117 | // also Intel Lubbock (additional to UCB1400; as temperature sensor) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1118 | // also Sharp Zaurus C7xx, C8xx (corgi/sheperd/husky) |
| 1119 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1120 | // Atmel at91sam9261-EK uses ads7843 |
| 1121 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1122 | // also various AMD Au1x00 devel boards |
| 1123 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1124 | return spi_register_driver(&ads7846_driver); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1125 | } |
| 1126 | module_init(ads7846_init); |
| 1127 | |
| 1128 | static void __exit ads7846_exit(void) |
| 1129 | { |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1130 | spi_unregister_driver(&ads7846_driver); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1131 | |
| 1132 | #ifdef CONFIG_ARCH_OMAP |
| 1133 | if (machine_is_omap_osk()) { |
| 1134 | omap_free_gpio(4); |
| 1135 | omap_free_gpio(6); |
| 1136 | } |
| 1137 | #endif |
| 1138 | |
| 1139 | } |
| 1140 | module_exit(ads7846_exit); |
| 1141 | |
| 1142 | MODULE_DESCRIPTION("ADS7846 TouchScreen Driver"); |
| 1143 | MODULE_LICENSE("GPL"); |