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