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