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