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