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