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 | */ |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 20 | #include <linux/types.h> |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 21 | #include <linux/hwmon.h> |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 22 | #include <linux/init.h> |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 23 | #include <linux/err.h> |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 24 | #include <linux/sched.h> |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 25 | #include <linux/delay.h> |
| 26 | #include <linux/input.h> |
| 27 | #include <linux/interrupt.h> |
| 28 | #include <linux/slab.h> |
Mark Brown | 3c36e71 | 2011-01-20 22:51:26 -0800 | [diff] [blame] | 29 | #include <linux/pm.h> |
Daniel Mack | a608026 | 2013-06-27 23:42:17 -0700 | [diff] [blame] | 30 | #include <linux/of.h> |
| 31 | #include <linux/of_gpio.h> |
| 32 | #include <linux/of_device.h> |
Eric Miao | 4d5975e | 2008-09-10 12:06:15 -0400 | [diff] [blame] | 33 | #include <linux/gpio.h> |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 34 | #include <linux/spi/spi.h> |
| 35 | #include <linux/spi/ads7846.h> |
Grazvydas Ignotas | 9114337 | 2010-02-25 02:04:56 -0800 | [diff] [blame] | 36 | #include <linux/regulator/consumer.h> |
Paul Gortmaker | d2d8442 | 2011-07-03 13:53:48 -0400 | [diff] [blame] | 37 | #include <linux/module.h> |
Andrew Morton | 3ac8bf0 | 2006-03-26 01:37:33 -0800 | [diff] [blame] | 38 | #include <asm/irq.h> |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 39 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 40 | /* |
David Brownell | 9084533 | 2006-05-25 18:44:20 -0700 | [diff] [blame] | 41 | * This code has been heavily tested on a Nokia 770, and lightly |
Pavel Machek | 52ce4eaa | 2009-11-23 08:25:17 -0800 | [diff] [blame] | 42 | * tested on other ads7846 devices (OSK/Mistral, Lubbock, Spitz). |
David Brownell | bff0de5 | 2007-05-22 23:28:40 -0400 | [diff] [blame] | 43 | * TSC2046 is just newer ads7846 silicon. |
Nicolas Ferre | 969111e | 2007-02-28 23:51:03 -0500 | [diff] [blame] | 44 | * Support for ads7843 tested on Atmel at91sam926x-EK. |
| 45 | * Support for ads7845 has only been stubbed in. |
Michael Hennerich | 06a0912 | 2010-03-09 20:38:45 -0800 | [diff] [blame] | 46 | * Support for Analog Devices AD7873 and AD7843 tested. |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 47 | * |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 48 | * IRQ handling needs a workaround because of a shortcoming in handling |
| 49 | * edge triggered IRQs on some platforms like the OMAP1/2. These |
| 50 | * platforms don't handle the ARM lazy IRQ disabling properly, thus we |
| 51 | * have to maintain our own SW IRQ disabled status. This should be |
| 52 | * removed as soon as the affected platform's IRQ handling is fixed. |
| 53 | * |
Pavel Machek | 52ce4eaa | 2009-11-23 08:25:17 -0800 | [diff] [blame] | 54 | * App note sbaa036 talks in more detail about accurate sampling... |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 55 | * that ought to help in situations like LCDs inducing noise (which |
| 56 | * can also be helped by using synch signals) and more generally. |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 57 | * This driver tries to utilize the measures described in the app |
| 58 | * note. The strength of filtering can be set in the board-* specific |
| 59 | * files. |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 60 | */ |
| 61 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 62 | #define TS_POLL_DELAY 1 /* ms delay before the first sample */ |
| 63 | #define TS_POLL_PERIOD 5 /* ms delay between samples */ |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 64 | |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 65 | /* this driver doesn't aim at the peak continuous sample rate */ |
| 66 | #define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */) |
| 67 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 68 | struct ts_event { |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 69 | /* |
| 70 | * For portability, we can't read 12 bit values using SPI (which |
| 71 | * would make the controller deliver them as native byte order u16 |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 72 | * with msbs zeroed). Instead, we read them as two 8-bit values, |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 73 | * *** WHICH NEED BYTESWAPPING *** and range adjustment. |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 74 | */ |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 75 | u16 x; |
| 76 | u16 y; |
| 77 | u16 z1, z2; |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 78 | bool ignore; |
Anatolij Gustschin | 3eac5c7 | 2010-07-01 09:01:56 -0700 | [diff] [blame] | 79 | u8 x_buf[3]; |
| 80 | u8 y_buf[3]; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
Dmitry Torokhov | e8f462d | 2008-10-09 00:52:23 -0400 | [diff] [blame] | 83 | /* |
| 84 | * We allocate this separately to avoid cache line sharing issues when |
| 85 | * driver is used with DMA-based SPI controllers (like atmel_spi) on |
| 86 | * systems where main memory is not DMA-coherent (most non-x86 boards). |
| 87 | */ |
| 88 | struct ads7846_packet { |
| 89 | u8 read_x, read_y, read_z1, read_z2, pwrdown; |
| 90 | u16 dummy; /* for the pwrdown read */ |
| 91 | struct ts_event tc; |
Anatolij Gustschin | 3eac5c7 | 2010-07-01 09:01:56 -0700 | [diff] [blame] | 92 | /* for ads7845 with mpc5121 psc spi we use 3-byte buffers */ |
| 93 | u8 read_x_cmd[3], read_y_cmd[3], pwrdown_cmd[3]; |
Dmitry Torokhov | e8f462d | 2008-10-09 00:52:23 -0400 | [diff] [blame] | 94 | }; |
| 95 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 96 | struct ads7846 { |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 97 | struct input_dev *input; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 98 | char phys[32]; |
Michael Roth | b58895f | 2009-05-18 16:05:12 -0700 | [diff] [blame] | 99 | char name[32]; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 100 | |
| 101 | struct spi_device *spi; |
Grazvydas Ignotas | 9114337 | 2010-02-25 02:04:56 -0800 | [diff] [blame] | 102 | struct regulator *reg; |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 103 | |
Fabio Estevam | c52b4fc | 2013-12-01 22:11:26 -0800 | [diff] [blame^] | 104 | #if IS_ENABLED(CONFIG_HWMON) |
Dmitry Torokhov | 8dd5165 | 2006-11-02 23:34:09 -0500 | [diff] [blame] | 105 | struct attribute_group *attr_group; |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 106 | struct device *hwmon; |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 107 | #endif |
| 108 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 109 | u16 model; |
David Brownell | 7c6d0ee | 2008-04-02 00:43:01 -0400 | [diff] [blame] | 110 | u16 vref_mv; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 111 | u16 vref_delay_usecs; |
| 112 | u16 x_plate_ohms; |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 113 | u16 pressure_max; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 114 | |
Michael Roth | 86579a4 | 2009-05-18 16:04:44 -0700 | [diff] [blame] | 115 | bool swap_xy; |
Alexander Stein | ebcaaad | 2011-05-11 16:24:08 -0700 | [diff] [blame] | 116 | bool use_internal; |
Michael Roth | 86579a4 | 2009-05-18 16:04:44 -0700 | [diff] [blame] | 117 | |
Dmitry Torokhov | e8f462d | 2008-10-09 00:52:23 -0400 | [diff] [blame] | 118 | struct ads7846_packet *packet; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 119 | |
Semih Hazar | e4f4886 | 2007-07-18 00:35:56 -0400 | [diff] [blame] | 120 | struct spi_transfer xfer[18]; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 121 | struct spi_message msg[5]; |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 122 | int msg_count; |
| 123 | wait_queue_head_t wait; |
| 124 | |
| 125 | bool pendown; |
| 126 | |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 127 | int read_cnt; |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 128 | int read_rep; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 129 | int last_read; |
| 130 | |
| 131 | u16 debounce_max; |
| 132 | u16 debounce_tol; |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 133 | u16 debounce_rep; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 134 | |
Semih Hazar | 1d25891 | 2007-07-18 00:36:04 -0400 | [diff] [blame] | 135 | u16 penirq_recheck_delay_usecs; |
| 136 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 137 | struct mutex lock; |
| 138 | bool stopped; /* P: lock */ |
| 139 | bool disabled; /* P: lock */ |
| 140 | bool suspended; /* P: lock */ |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 141 | |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 142 | int (*filter)(void *data, int data_idx, int *val); |
| 143 | void *filter_data; |
| 144 | void (*filter_cleanup)(void *data); |
Imre Deak | c9e617a | 2006-04-11 23:44:05 -0400 | [diff] [blame] | 145 | int (*get_pendown_state)(void); |
Eric Miao | 4d5975e | 2008-09-10 12:06:15 -0400 | [diff] [blame] | 146 | int gpio_pendown; |
Eric Miao | fd746d5 | 2009-04-11 16:54:59 -0700 | [diff] [blame] | 147 | |
| 148 | void (*wait_for_sync)(void); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | /* leave chip selected when we're done, for quicker re-select? */ |
| 152 | #if 0 |
| 153 | #define CS_CHANGE(xfer) ((xfer).cs_change = 1) |
| 154 | #else |
| 155 | #define CS_CHANGE(xfer) ((xfer).cs_change = 0) |
| 156 | #endif |
| 157 | |
| 158 | /*--------------------------------------------------------------------------*/ |
| 159 | |
| 160 | /* The ADS7846 has touchscreen and other sensors. |
| 161 | * Earlier ads784x chips are somewhat compatible. |
| 162 | */ |
| 163 | #define ADS_START (1 << 7) |
| 164 | #define ADS_A2A1A0_d_y (1 << 4) /* differential */ |
| 165 | #define ADS_A2A1A0_d_z1 (3 << 4) /* differential */ |
| 166 | #define ADS_A2A1A0_d_z2 (4 << 4) /* differential */ |
| 167 | #define ADS_A2A1A0_d_x (5 << 4) /* differential */ |
| 168 | #define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */ |
| 169 | #define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */ |
| 170 | #define ADS_A2A1A0_vaux (6 << 4) /* non-differential */ |
| 171 | #define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */ |
| 172 | #define ADS_8_BIT (1 << 3) |
| 173 | #define ADS_12_BIT (0 << 3) |
| 174 | #define ADS_SER (1 << 2) /* non-differential */ |
| 175 | #define ADS_DFR (0 << 2) /* differential */ |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 176 | #define ADS_PD10_PDOWN (0 << 0) /* low power mode + penirq */ |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 177 | #define ADS_PD10_ADC_ON (1 << 0) /* ADC on */ |
| 178 | #define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */ |
| 179 | #define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */ |
| 180 | |
| 181 | #define MAX_12BIT ((1<<12)-1) |
| 182 | |
| 183 | /* leave ADC powered up (disables penirq) between differential samples */ |
Imre Deak | de2defd | 2007-01-18 00:45:21 -0500 | [diff] [blame] | 184 | #define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \ |
| 185 | | ADS_12_BIT | ADS_DFR | \ |
| 186 | (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0)) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 187 | |
Imre Deak | de2defd | 2007-01-18 00:45:21 -0500 | [diff] [blame] | 188 | #define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref)) |
| 189 | #define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref)) |
| 190 | #define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref)) |
Imre Deak | 53a0ef89 | 2006-04-11 23:41:49 -0400 | [diff] [blame] | 191 | |
Imre Deak | de2defd | 2007-01-18 00:45:21 -0500 | [diff] [blame] | 192 | #define READ_X(vref) (READ_12BIT_DFR(x, 1, vref)) |
| 193 | #define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */ |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 194 | |
| 195 | /* single-ended samples need to first power up reference voltage; |
| 196 | * we leave both ADC and VREF powered |
| 197 | */ |
| 198 | #define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \ |
| 199 | | ADS_12_BIT | ADS_SER) |
| 200 | |
Imre Deak | de2defd | 2007-01-18 00:45:21 -0500 | [diff] [blame] | 201 | #define REF_ON (READ_12BIT_DFR(x, 1, 1)) |
| 202 | #define REF_OFF (READ_12BIT_DFR(y, 0, 0)) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 203 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 204 | /* Must be called with ts->lock held */ |
| 205 | static void ads7846_stop(struct ads7846 *ts) |
| 206 | { |
| 207 | if (!ts->disabled && !ts->suspended) { |
| 208 | /* Signal IRQ thread to stop polling and disable the handler. */ |
| 209 | ts->stopped = true; |
| 210 | mb(); |
| 211 | wake_up(&ts->wait); |
| 212 | disable_irq(ts->spi->irq); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /* Must be called with ts->lock held */ |
| 217 | static void ads7846_restart(struct ads7846 *ts) |
| 218 | { |
| 219 | if (!ts->disabled && !ts->suspended) { |
| 220 | /* Tell IRQ thread that it may poll the device. */ |
| 221 | ts->stopped = false; |
| 222 | mb(); |
| 223 | enable_irq(ts->spi->irq); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /* Must be called with ts->lock held */ |
| 228 | static void __ads7846_disable(struct ads7846 *ts) |
| 229 | { |
| 230 | ads7846_stop(ts); |
| 231 | regulator_disable(ts->reg); |
| 232 | |
| 233 | /* |
| 234 | * We know the chip's in low power mode since we always |
| 235 | * leave it that way after every request |
| 236 | */ |
| 237 | } |
| 238 | |
| 239 | /* Must be called with ts->lock held */ |
| 240 | static void __ads7846_enable(struct ads7846 *ts) |
| 241 | { |
Mark Brown | f94352f | 2013-03-03 20:19:07 -0800 | [diff] [blame] | 242 | int error; |
| 243 | |
| 244 | error = regulator_enable(ts->reg); |
| 245 | if (error != 0) |
| 246 | dev_err(&ts->spi->dev, "Failed to enable supply: %d\n", error); |
| 247 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 248 | ads7846_restart(ts); |
| 249 | } |
| 250 | |
| 251 | static void ads7846_disable(struct ads7846 *ts) |
| 252 | { |
| 253 | mutex_lock(&ts->lock); |
| 254 | |
| 255 | if (!ts->disabled) { |
| 256 | |
| 257 | if (!ts->suspended) |
| 258 | __ads7846_disable(ts); |
| 259 | |
| 260 | ts->disabled = true; |
| 261 | } |
| 262 | |
| 263 | mutex_unlock(&ts->lock); |
| 264 | } |
| 265 | |
| 266 | static void ads7846_enable(struct ads7846 *ts) |
| 267 | { |
| 268 | mutex_lock(&ts->lock); |
| 269 | |
| 270 | if (ts->disabled) { |
| 271 | |
| 272 | ts->disabled = false; |
| 273 | |
| 274 | if (!ts->suspended) |
| 275 | __ads7846_enable(ts); |
| 276 | } |
| 277 | |
| 278 | mutex_unlock(&ts->lock); |
| 279 | } |
| 280 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 281 | /*--------------------------------------------------------------------------*/ |
| 282 | |
| 283 | /* |
| 284 | * Non-touchscreen sensors only use single-ended conversions. |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 285 | * The range is GND..vREF. The ads7843 and ads7835 must use external vREF; |
| 286 | * ads7846 lets that pin be unconnected, to use internal vREF. |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 287 | */ |
| 288 | |
| 289 | struct ser_req { |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 290 | u8 ref_on; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 291 | u8 command; |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 292 | u8 ref_off; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 293 | u16 scratch; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 294 | struct spi_message msg; |
| 295 | struct spi_transfer xfer[6]; |
Alexander Stein | 1dbe7da | 2011-05-05 08:40:14 -0700 | [diff] [blame] | 296 | /* |
| 297 | * DMA (thus cache coherency maintenance) requires the |
| 298 | * transfer buffers to live in their own cache lines. |
| 299 | */ |
| 300 | __be16 sample ____cacheline_aligned; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 301 | }; |
| 302 | |
Anatolij Gustschin | 3eac5c7 | 2010-07-01 09:01:56 -0700 | [diff] [blame] | 303 | struct ads7845_ser_req { |
| 304 | u8 command[3]; |
Anatolij Gustschin | 3eac5c7 | 2010-07-01 09:01:56 -0700 | [diff] [blame] | 305 | struct spi_message msg; |
| 306 | struct spi_transfer xfer[2]; |
Alexander Stein | 1dbe7da | 2011-05-05 08:40:14 -0700 | [diff] [blame] | 307 | /* |
| 308 | * DMA (thus cache coherency maintenance) requires the |
| 309 | * transfer buffers to live in their own cache lines. |
| 310 | */ |
| 311 | u8 sample[3] ____cacheline_aligned; |
Anatolij Gustschin | 3eac5c7 | 2010-07-01 09:01:56 -0700 | [diff] [blame] | 312 | }; |
| 313 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 314 | static int ads7846_read12_ser(struct device *dev, unsigned command) |
| 315 | { |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 316 | struct spi_device *spi = to_spi_device(dev); |
| 317 | struct ads7846 *ts = dev_get_drvdata(dev); |
| 318 | struct ser_req *req; |
| 319 | int status; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 320 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 321 | req = kzalloc(sizeof *req, GFP_KERNEL); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 322 | if (!req) |
| 323 | return -ENOMEM; |
| 324 | |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 325 | spi_message_init(&req->msg); |
Vitaly Wool | 8275c64 | 2006-01-08 13:34:28 -0800 | [diff] [blame] | 326 | |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 327 | /* maybe turn on internal vREF, and let it settle */ |
Alexander Stein | ebcaaad | 2011-05-11 16:24:08 -0700 | [diff] [blame] | 328 | if (ts->use_internal) { |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 329 | req->ref_on = REF_ON; |
| 330 | req->xfer[0].tx_buf = &req->ref_on; |
| 331 | req->xfer[0].len = 1; |
| 332 | spi_message_add_tail(&req->xfer[0], &req->msg); |
| 333 | |
| 334 | req->xfer[1].rx_buf = &req->scratch; |
| 335 | req->xfer[1].len = 2; |
| 336 | |
| 337 | /* for 1uF, settle for 800 usec; no cap, 100 usec. */ |
| 338 | req->xfer[1].delay_usecs = ts->vref_delay_usecs; |
| 339 | spi_message_add_tail(&req->xfer[1], &req->msg); |
Alexander Stein | ebcaaad | 2011-05-11 16:24:08 -0700 | [diff] [blame] | 340 | |
| 341 | /* Enable reference voltage */ |
| 342 | command |= ADS_PD10_REF_ON; |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 343 | } |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 344 | |
Alexander Stein | ebcaaad | 2011-05-11 16:24:08 -0700 | [diff] [blame] | 345 | /* Enable ADC in every case */ |
| 346 | command |= ADS_PD10_ADC_ON; |
| 347 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 348 | /* take sample */ |
| 349 | req->command = (u8) command; |
| 350 | req->xfer[2].tx_buf = &req->command; |
| 351 | req->xfer[2].len = 1; |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 352 | spi_message_add_tail(&req->xfer[2], &req->msg); |
| 353 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 354 | req->xfer[3].rx_buf = &req->sample; |
| 355 | req->xfer[3].len = 2; |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 356 | spi_message_add_tail(&req->xfer[3], &req->msg); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 357 | |
| 358 | /* REVISIT: take a few more samples, and compare ... */ |
| 359 | |
Nicolas Ferre | 969111e | 2007-02-28 23:51:03 -0500 | [diff] [blame] | 360 | /* converter in low power mode & enable PENIRQ */ |
| 361 | req->ref_off = PWRDOWN; |
| 362 | req->xfer[4].tx_buf = &req->ref_off; |
| 363 | req->xfer[4].len = 1; |
| 364 | spi_message_add_tail(&req->xfer[4], &req->msg); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 365 | |
Nicolas Ferre | 969111e | 2007-02-28 23:51:03 -0500 | [diff] [blame] | 366 | req->xfer[5].rx_buf = &req->scratch; |
| 367 | req->xfer[5].len = 2; |
| 368 | CS_CHANGE(req->xfer[5]); |
| 369 | spi_message_add_tail(&req->xfer[5], &req->msg); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 370 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 371 | mutex_lock(&ts->lock); |
| 372 | ads7846_stop(ts); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 373 | status = spi_sync(spi, &req->msg); |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 374 | ads7846_restart(ts); |
| 375 | mutex_unlock(&ts->lock); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 376 | |
Marc Pignat | c24b260 | 2007-12-04 23:45:11 -0800 | [diff] [blame] | 377 | if (status == 0) { |
| 378 | /* on-wire is a must-ignore bit, a BE12 value, then padding */ |
David Brownell | 7c6d0ee | 2008-04-02 00:43:01 -0400 | [diff] [blame] | 379 | status = be16_to_cpu(req->sample); |
| 380 | status = status >> 3; |
| 381 | status &= 0x0fff; |
Marc Pignat | c24b260 | 2007-12-04 23:45:11 -0800 | [diff] [blame] | 382 | } |
David Brownell | 9084533 | 2006-05-25 18:44:20 -0700 | [diff] [blame] | 383 | |
| 384 | kfree(req); |
David Brownell | 7c6d0ee | 2008-04-02 00:43:01 -0400 | [diff] [blame] | 385 | return status; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Anatolij Gustschin | 3eac5c7 | 2010-07-01 09:01:56 -0700 | [diff] [blame] | 388 | static int ads7845_read12_ser(struct device *dev, unsigned command) |
| 389 | { |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 390 | struct spi_device *spi = to_spi_device(dev); |
| 391 | struct ads7846 *ts = dev_get_drvdata(dev); |
| 392 | struct ads7845_ser_req *req; |
| 393 | int status; |
Anatolij Gustschin | 3eac5c7 | 2010-07-01 09:01:56 -0700 | [diff] [blame] | 394 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 395 | req = kzalloc(sizeof *req, GFP_KERNEL); |
Anatolij Gustschin | 3eac5c7 | 2010-07-01 09:01:56 -0700 | [diff] [blame] | 396 | if (!req) |
| 397 | return -ENOMEM; |
| 398 | |
| 399 | spi_message_init(&req->msg); |
| 400 | |
| 401 | req->command[0] = (u8) command; |
| 402 | req->xfer[0].tx_buf = req->command; |
| 403 | req->xfer[0].rx_buf = req->sample; |
| 404 | req->xfer[0].len = 3; |
| 405 | spi_message_add_tail(&req->xfer[0], &req->msg); |
| 406 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 407 | mutex_lock(&ts->lock); |
| 408 | ads7846_stop(ts); |
Anatolij Gustschin | 3eac5c7 | 2010-07-01 09:01:56 -0700 | [diff] [blame] | 409 | status = spi_sync(spi, &req->msg); |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 410 | ads7846_restart(ts); |
| 411 | mutex_unlock(&ts->lock); |
Anatolij Gustschin | 3eac5c7 | 2010-07-01 09:01:56 -0700 | [diff] [blame] | 412 | |
| 413 | if (status == 0) { |
| 414 | /* BE12 value, then padding */ |
| 415 | status = be16_to_cpu(*((u16 *)&req->sample[1])); |
| 416 | status = status >> 3; |
| 417 | status &= 0x0fff; |
| 418 | } |
| 419 | |
| 420 | kfree(req); |
| 421 | return status; |
| 422 | } |
| 423 | |
Fabio Estevam | c52b4fc | 2013-12-01 22:11:26 -0800 | [diff] [blame^] | 424 | #if IS_ENABLED(CONFIG_HWMON) |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 425 | |
| 426 | #define SHOW(name, var, adjust) static ssize_t \ |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 427 | name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \ |
| 428 | { \ |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 429 | struct ads7846 *ts = dev_get_drvdata(dev); \ |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 430 | ssize_t v = ads7846_read12_ser(dev, \ |
Alexander Stein | ebcaaad | 2011-05-11 16:24:08 -0700 | [diff] [blame] | 431 | READ_12BIT_SER(var)); \ |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 432 | if (v < 0) \ |
| 433 | return v; \ |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 434 | return sprintf(buf, "%u\n", adjust(ts, v)); \ |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 435 | } \ |
| 436 | static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL); |
| 437 | |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 438 | |
Adam Buchbinder | b731d7b | 2009-03-13 12:15:26 -0400 | [diff] [blame] | 439 | /* Sysfs conventions report temperatures in millidegrees Celsius. |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 440 | * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high |
| 441 | * accuracy scheme without calibration data. For now we won't try either; |
| 442 | * userspace sees raw sensor values, and must scale/calibrate appropriately. |
| 443 | */ |
| 444 | static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v) |
| 445 | { |
| 446 | return v; |
| 447 | } |
| 448 | |
| 449 | SHOW(temp0, temp0, null_adjust) /* temp1_input */ |
| 450 | SHOW(temp1, temp1, null_adjust) /* temp2_input */ |
| 451 | |
| 452 | |
| 453 | /* sysfs conventions report voltages in millivolts. We can convert voltages |
| 454 | * if we know vREF. userspace may need to scale vAUX to match the board's |
| 455 | * external resistors; we assume that vBATT only uses the internal ones. |
| 456 | */ |
| 457 | static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v) |
| 458 | { |
| 459 | unsigned retval = v; |
| 460 | |
| 461 | /* external resistors may scale vAUX into 0..vREF */ |
David Brownell | 7c6d0ee | 2008-04-02 00:43:01 -0400 | [diff] [blame] | 462 | retval *= ts->vref_mv; |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 463 | retval = retval >> 12; |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 464 | |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 465 | return retval; |
| 466 | } |
| 467 | |
| 468 | static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v) |
| 469 | { |
| 470 | unsigned retval = vaux_adjust(ts, v); |
| 471 | |
| 472 | /* ads7846 has a resistor ladder to scale this signal down */ |
| 473 | if (ts->model == 7846) |
| 474 | retval *= 4; |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 475 | |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 476 | return retval; |
| 477 | } |
| 478 | |
| 479 | SHOW(in0_input, vaux, vaux_adjust) |
| 480 | SHOW(in1_input, vbatt, vbatt_adjust) |
| 481 | |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 482 | static struct attribute *ads7846_attributes[] = { |
| 483 | &dev_attr_temp0.attr, |
| 484 | &dev_attr_temp1.attr, |
| 485 | &dev_attr_in0_input.attr, |
| 486 | &dev_attr_in1_input.attr, |
| 487 | NULL, |
| 488 | }; |
| 489 | |
| 490 | static struct attribute_group ads7846_attr_group = { |
| 491 | .attrs = ads7846_attributes, |
| 492 | }; |
| 493 | |
| 494 | static struct attribute *ads7843_attributes[] = { |
| 495 | &dev_attr_in0_input.attr, |
| 496 | &dev_attr_in1_input.attr, |
| 497 | NULL, |
| 498 | }; |
| 499 | |
| 500 | static struct attribute_group ads7843_attr_group = { |
| 501 | .attrs = ads7843_attributes, |
| 502 | }; |
| 503 | |
| 504 | static struct attribute *ads7845_attributes[] = { |
| 505 | &dev_attr_in0_input.attr, |
| 506 | NULL, |
| 507 | }; |
| 508 | |
| 509 | static struct attribute_group ads7845_attr_group = { |
| 510 | .attrs = ads7845_attributes, |
| 511 | }; |
| 512 | |
| 513 | static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts) |
| 514 | { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 515 | struct device *hwmon; |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 516 | int err; |
| 517 | |
| 518 | /* hwmon sensors need a reference voltage */ |
| 519 | switch (ts->model) { |
| 520 | case 7846: |
David Brownell | 7c6d0ee | 2008-04-02 00:43:01 -0400 | [diff] [blame] | 521 | if (!ts->vref_mv) { |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 522 | dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n"); |
David Brownell | 7c6d0ee | 2008-04-02 00:43:01 -0400 | [diff] [blame] | 523 | ts->vref_mv = 2500; |
Alexander Stein | ebcaaad | 2011-05-11 16:24:08 -0700 | [diff] [blame] | 524 | ts->use_internal = true; |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 525 | } |
| 526 | break; |
| 527 | case 7845: |
| 528 | case 7843: |
David Brownell | 7c6d0ee | 2008-04-02 00:43:01 -0400 | [diff] [blame] | 529 | if (!ts->vref_mv) { |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 530 | dev_warn(&spi->dev, |
| 531 | "external vREF for ADS%d not specified\n", |
| 532 | ts->model); |
| 533 | return 0; |
| 534 | } |
| 535 | break; |
| 536 | } |
| 537 | |
| 538 | /* different chips have different sensor groups */ |
| 539 | switch (ts->model) { |
| 540 | case 7846: |
| 541 | ts->attr_group = &ads7846_attr_group; |
| 542 | break; |
| 543 | case 7845: |
| 544 | ts->attr_group = &ads7845_attr_group; |
| 545 | break; |
| 546 | case 7843: |
| 547 | ts->attr_group = &ads7843_attr_group; |
| 548 | break; |
| 549 | default: |
| 550 | dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model); |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | err = sysfs_create_group(&spi->dev.kobj, ts->attr_group); |
| 555 | if (err) |
| 556 | return err; |
| 557 | |
| 558 | hwmon = hwmon_device_register(&spi->dev); |
| 559 | if (IS_ERR(hwmon)) { |
| 560 | sysfs_remove_group(&spi->dev.kobj, ts->attr_group); |
| 561 | return PTR_ERR(hwmon); |
| 562 | } |
| 563 | |
| 564 | ts->hwmon = hwmon; |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | static void ads784x_hwmon_unregister(struct spi_device *spi, |
| 569 | struct ads7846 *ts) |
| 570 | { |
| 571 | if (ts->hwmon) { |
| 572 | sysfs_remove_group(&spi->dev.kobj, ts->attr_group); |
| 573 | hwmon_device_unregister(ts->hwmon); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | #else |
| 578 | static inline int ads784x_hwmon_register(struct spi_device *spi, |
| 579 | struct ads7846 *ts) |
| 580 | { |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | static inline void ads784x_hwmon_unregister(struct spi_device *spi, |
| 585 | struct ads7846 *ts) |
| 586 | { |
| 587 | } |
| 588 | #endif |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 589 | |
Imre Deak | 438f2a7 | 2006-04-11 23:41:32 -0400 | [diff] [blame] | 590 | static ssize_t ads7846_pen_down_show(struct device *dev, |
| 591 | struct device_attribute *attr, char *buf) |
| 592 | { |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 593 | struct ads7846 *ts = dev_get_drvdata(dev); |
| 594 | |
| 595 | return sprintf(buf, "%u\n", ts->pendown); |
Imre Deak | 438f2a7 | 2006-04-11 23:41:32 -0400 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL); |
| 599 | |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 600 | static ssize_t ads7846_disable_show(struct device *dev, |
| 601 | struct device_attribute *attr, char *buf) |
| 602 | { |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 603 | struct ads7846 *ts = dev_get_drvdata(dev); |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 604 | |
| 605 | return sprintf(buf, "%u\n", ts->disabled); |
| 606 | } |
| 607 | |
| 608 | static ssize_t ads7846_disable_store(struct device *dev, |
| 609 | struct device_attribute *attr, |
| 610 | const char *buf, size_t count) |
| 611 | { |
| 612 | struct ads7846 *ts = dev_get_drvdata(dev); |
JJ Ding | 76496e7 | 2011-11-09 10:20:14 -0800 | [diff] [blame] | 613 | unsigned int i; |
| 614 | int err; |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 615 | |
JJ Ding | 76496e7 | 2011-11-09 10:20:14 -0800 | [diff] [blame] | 616 | err = kstrtouint(buf, 10, &i); |
| 617 | if (err) |
| 618 | return err; |
Joe Rouvier | 160f1fe | 2008-08-10 00:29:25 -0400 | [diff] [blame] | 619 | |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 620 | if (i) |
| 621 | ads7846_disable(ts); |
| 622 | else |
| 623 | ads7846_enable(ts); |
| 624 | |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 625 | return count; |
| 626 | } |
| 627 | |
| 628 | static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store); |
| 629 | |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 630 | static struct attribute *ads784x_attributes[] = { |
Dmitry Torokhov | 8dd5165 | 2006-11-02 23:34:09 -0500 | [diff] [blame] | 631 | &dev_attr_pen_down.attr, |
| 632 | &dev_attr_disable.attr, |
| 633 | NULL, |
| 634 | }; |
| 635 | |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 636 | static struct attribute_group ads784x_attr_group = { |
| 637 | .attrs = ads784x_attributes, |
Dmitry Torokhov | 8dd5165 | 2006-11-02 23:34:09 -0500 | [diff] [blame] | 638 | }; |
| 639 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 640 | /*--------------------------------------------------------------------------*/ |
| 641 | |
Eric Miao | 4d5975e | 2008-09-10 12:06:15 -0400 | [diff] [blame] | 642 | static int get_pendown_state(struct ads7846 *ts) |
| 643 | { |
| 644 | if (ts->get_pendown_state) |
| 645 | return ts->get_pendown_state(); |
| 646 | |
| 647 | return !gpio_get_value(ts->gpio_pendown); |
| 648 | } |
| 649 | |
Eric Miao | fd746d5 | 2009-04-11 16:54:59 -0700 | [diff] [blame] | 650 | static void null_wait_for_sync(void) |
| 651 | { |
| 652 | } |
| 653 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 654 | static int ads7846_debounce_filter(void *ads, int data_idx, int *val) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 655 | { |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 656 | struct ads7846 *ts = ads; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 657 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 658 | if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) { |
| 659 | /* Start over collecting consistent readings. */ |
| 660 | ts->read_rep = 0; |
| 661 | /* |
| 662 | * Repeat it, if this was the first read or the read |
| 663 | * wasn't consistent enough. |
| 664 | */ |
| 665 | if (ts->read_cnt < ts->debounce_max) { |
| 666 | ts->last_read = *val; |
| 667 | ts->read_cnt++; |
| 668 | return ADS7846_FILTER_REPEAT; |
| 669 | } else { |
| 670 | /* |
| 671 | * Maximum number of debouncing reached and still |
| 672 | * not enough number of consistent readings. Abort |
| 673 | * the whole sample, repeat it in the next sampling |
| 674 | * period. |
| 675 | */ |
| 676 | ts->read_cnt = 0; |
| 677 | return ADS7846_FILTER_IGNORE; |
| 678 | } |
| 679 | } else { |
| 680 | if (++ts->read_rep > ts->debounce_rep) { |
| 681 | /* |
| 682 | * Got a good reading for this coordinate, |
| 683 | * go for the next one. |
| 684 | */ |
| 685 | ts->read_cnt = 0; |
| 686 | ts->read_rep = 0; |
| 687 | return ADS7846_FILTER_OK; |
| 688 | } else { |
| 689 | /* Read more values that are consistent. */ |
| 690 | ts->read_cnt++; |
| 691 | return ADS7846_FILTER_REPEAT; |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | static int ads7846_no_filter(void *ads, int data_idx, int *val) |
| 697 | { |
| 698 | return ADS7846_FILTER_OK; |
| 699 | } |
| 700 | |
| 701 | static int ads7846_get_value(struct ads7846 *ts, struct spi_message *m) |
| 702 | { |
| 703 | struct spi_transfer *t = |
| 704 | list_entry(m->transfers.prev, struct spi_transfer, transfer_list); |
| 705 | |
| 706 | if (ts->model == 7845) { |
| 707 | return be16_to_cpup((__be16 *)&(((char*)t->rx_buf)[1])) >> 3; |
| 708 | } else { |
| 709 | /* |
| 710 | * adjust: on-wire is a must-ignore bit, a BE12 value, then |
| 711 | * padding; built from two 8 bit values written msb-first. |
| 712 | */ |
| 713 | return be16_to_cpup((__be16 *)t->rx_buf) >> 3; |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | static void ads7846_update_value(struct spi_message *m, int val) |
| 718 | { |
| 719 | struct spi_transfer *t = |
| 720 | list_entry(m->transfers.prev, struct spi_transfer, transfer_list); |
| 721 | |
| 722 | *(u16 *)t->rx_buf = val; |
| 723 | } |
| 724 | |
| 725 | static void ads7846_read_state(struct ads7846 *ts) |
| 726 | { |
| 727 | struct ads7846_packet *packet = ts->packet; |
| 728 | struct spi_message *m; |
| 729 | int msg_idx = 0; |
| 730 | int val; |
| 731 | int action; |
| 732 | int error; |
| 733 | |
| 734 | while (msg_idx < ts->msg_count) { |
| 735 | |
| 736 | ts->wait_for_sync(); |
| 737 | |
| 738 | m = &ts->msg[msg_idx]; |
| 739 | error = spi_sync(ts->spi, m); |
| 740 | if (error) { |
| 741 | dev_err(&ts->spi->dev, "spi_async --> %d\n", error); |
| 742 | packet->tc.ignore = true; |
| 743 | return; |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | * Last message is power down request, no need to convert |
| 748 | * or filter the value. |
| 749 | */ |
| 750 | if (msg_idx < ts->msg_count - 1) { |
| 751 | |
| 752 | val = ads7846_get_value(ts, m); |
| 753 | |
| 754 | action = ts->filter(ts->filter_data, msg_idx, &val); |
| 755 | switch (action) { |
| 756 | case ADS7846_FILTER_REPEAT: |
| 757 | continue; |
| 758 | |
| 759 | case ADS7846_FILTER_IGNORE: |
| 760 | packet->tc.ignore = true; |
| 761 | msg_idx = ts->msg_count - 1; |
| 762 | continue; |
| 763 | |
| 764 | case ADS7846_FILTER_OK: |
| 765 | ads7846_update_value(m, val); |
| 766 | packet->tc.ignore = false; |
| 767 | msg_idx++; |
| 768 | break; |
| 769 | |
| 770 | default: |
| 771 | BUG(); |
| 772 | } |
| 773 | } else { |
| 774 | msg_idx++; |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | static void ads7846_report_state(struct ads7846 *ts) |
| 780 | { |
| 781 | struct ads7846_packet *packet = ts->packet; |
| 782 | unsigned int Rt; |
| 783 | u16 x, y, z1, z2; |
| 784 | |
| 785 | /* |
| 786 | * ads7846_get_value() does in-place conversion (including byte swap) |
| 787 | * from on-the-wire format as part of debouncing to get stable |
| 788 | * readings. |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 789 | */ |
Anatolij Gustschin | 3eac5c7 | 2010-07-01 09:01:56 -0700 | [diff] [blame] | 790 | if (ts->model == 7845) { |
| 791 | x = *(u16 *)packet->tc.x_buf; |
| 792 | y = *(u16 *)packet->tc.y_buf; |
| 793 | z1 = 0; |
| 794 | z2 = 0; |
| 795 | } else { |
| 796 | x = packet->tc.x; |
| 797 | y = packet->tc.y; |
| 798 | z1 = packet->tc.z1; |
| 799 | z2 = packet->tc.z2; |
| 800 | } |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 801 | |
| 802 | /* range filtering */ |
| 803 | if (x == MAX_12BIT) |
| 804 | x = 0; |
| 805 | |
Hans-Christian Egtvedt | 9460b65 | 2008-07-23 14:38:27 -0400 | [diff] [blame] | 806 | if (ts->model == 7843) { |
| 807 | Rt = ts->pressure_max / 2; |
Anatolij Gustschin | 3eac5c7 | 2010-07-01 09:01:56 -0700 | [diff] [blame] | 808 | } else if (ts->model == 7845) { |
| 809 | if (get_pendown_state(ts)) |
| 810 | Rt = ts->pressure_max / 2; |
| 811 | else |
| 812 | Rt = 0; |
| 813 | dev_vdbg(&ts->spi->dev, "x/y: %d/%d, PD %d\n", x, y, Rt); |
Hans-Christian Egtvedt | 9460b65 | 2008-07-23 14:38:27 -0400 | [diff] [blame] | 814 | } else if (likely(x && z1)) { |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 815 | /* compute touch pressure resistance using equation #2 */ |
| 816 | Rt = z2; |
| 817 | Rt -= z1; |
| 818 | Rt *= x; |
| 819 | Rt *= ts->x_plate_ohms; |
| 820 | Rt /= z1; |
| 821 | Rt = (Rt + 2047) >> 12; |
Hans-Christian Egtvedt | 9460b65 | 2008-07-23 14:38:27 -0400 | [diff] [blame] | 822 | } else { |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 823 | Rt = 0; |
Hans-Christian Egtvedt | 9460b65 | 2008-07-23 14:38:27 -0400 | [diff] [blame] | 824 | } |
Nicolas Ferre | 969111e | 2007-02-28 23:51:03 -0500 | [diff] [blame] | 825 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 826 | /* |
| 827 | * Sample found inconsistent by debouncing or pressure is beyond |
Imre Deak | 1936d59 | 2007-01-18 00:45:31 -0500 | [diff] [blame] | 828 | * the maximum. Don't report it to user space, repeat at least |
| 829 | * once more the measurement |
| 830 | */ |
Dmitry Torokhov | e8f462d | 2008-10-09 00:52:23 -0400 | [diff] [blame] | 831 | if (packet->tc.ignore || Rt > ts->pressure_max) { |
Pavel Machek | 52ce4eaa | 2009-11-23 08:25:17 -0800 | [diff] [blame] | 832 | dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n", |
| 833 | packet->tc.ignore, Rt); |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 834 | return; |
| 835 | } |
| 836 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 837 | /* |
| 838 | * Maybe check the pendown state before reporting. This discards |
Semih Hazar | 1d25891 | 2007-07-18 00:36:04 -0400 | [diff] [blame] | 839 | * false readings when the pen is lifted. |
| 840 | */ |
| 841 | if (ts->penirq_recheck_delay_usecs) { |
| 842 | udelay(ts->penirq_recheck_delay_usecs); |
Eric Miao | 4d5975e | 2008-09-10 12:06:15 -0400 | [diff] [blame] | 843 | if (!get_pendown_state(ts)) |
Semih Hazar | 1d25891 | 2007-07-18 00:36:04 -0400 | [diff] [blame] | 844 | Rt = 0; |
| 845 | } |
| 846 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 847 | /* |
| 848 | * NOTE: We can't rely on the pressure to determine the pen down |
| 849 | * state, even this controller has a pressure sensor. The pressure |
Imre Deak | 15e3589 | 2007-01-18 00:45:43 -0500 | [diff] [blame] | 850 | * value can fluctuate for quite a while after lifting the pen and |
| 851 | * in some cases may not even settle at the expected value. |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 852 | * |
Imre Deak | 15e3589 | 2007-01-18 00:45:43 -0500 | [diff] [blame] | 853 | * The only safe way to check for the pen up condition is in the |
| 854 | * 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] | 855 | */ |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 856 | if (Rt) { |
Imre Deak | 15e3589 | 2007-01-18 00:45:43 -0500 | [diff] [blame] | 857 | struct input_dev *input = ts->input; |
Imre Deak | ae82d5a | 2006-04-26 00:12:14 -0400 | [diff] [blame] | 858 | |
Michael Roth | 86579a4 | 2009-05-18 16:04:44 -0700 | [diff] [blame] | 859 | if (ts->swap_xy) |
| 860 | swap(x, y); |
| 861 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 862 | if (!ts->pendown) { |
| 863 | input_report_key(input, BTN_TOUCH, 1); |
| 864 | ts->pendown = true; |
| 865 | dev_vdbg(&ts->spi->dev, "DOWN\n"); |
| 866 | } |
| 867 | |
Imre Deak | 15e3589 | 2007-01-18 00:45:43 -0500 | [diff] [blame] | 868 | input_report_abs(input, ABS_X, x); |
| 869 | input_report_abs(input, ABS_Y, y); |
Pavel Machek | 30ad7ba | 2009-11-23 08:17:38 -0800 | [diff] [blame] | 870 | input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 871 | |
Imre Deak | 15e3589 | 2007-01-18 00:45:43 -0500 | [diff] [blame] | 872 | input_sync(input); |
Pavel Machek | 52ce4eaa | 2009-11-23 08:25:17 -0800 | [diff] [blame] | 873 | dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt); |
Imre Deak | 15e3589 | 2007-01-18 00:45:43 -0500 | [diff] [blame] | 874 | } |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 875 | } |
| 876 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 877 | static irqreturn_t ads7846_hard_irq(int irq, void *handle) |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 878 | { |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 879 | struct ads7846 *ts = handle; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 880 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 881 | return get_pendown_state(ts) ? IRQ_WAKE_THREAD : IRQ_HANDLED; |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 882 | } |
| 883 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 884 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 885 | static irqreturn_t ads7846_irq(int irq, void *handle) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 886 | { |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 887 | struct ads7846 *ts = handle; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 888 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 889 | /* Start with a small delay before checking pendown state */ |
| 890 | msleep(TS_POLL_DELAY); |
| 891 | |
| 892 | while (!ts->stopped && get_pendown_state(ts)) { |
| 893 | |
| 894 | /* pen is down, continue with the measurement */ |
| 895 | ads7846_read_state(ts); |
| 896 | |
| 897 | if (!ts->stopped) |
| 898 | ads7846_report_state(ts); |
| 899 | |
| 900 | wait_event_timeout(ts->wait, ts->stopped, |
| 901 | msecs_to_jiffies(TS_POLL_PERIOD)); |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 902 | } |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 903 | |
| 904 | if (ts->pendown) { |
| 905 | struct input_dev *input = ts->input; |
| 906 | |
| 907 | input_report_key(input, BTN_TOUCH, 0); |
| 908 | input_report_abs(input, ABS_PRESSURE, 0); |
| 909 | input_sync(input); |
| 910 | |
| 911 | ts->pendown = false; |
| 912 | dev_vdbg(&ts->spi->dev, "UP\n"); |
| 913 | } |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 914 | |
| 915 | return IRQ_HANDLED; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 916 | } |
| 917 | |
Mark Brown | 3c36e71 | 2011-01-20 22:51:26 -0800 | [diff] [blame] | 918 | #ifdef CONFIG_PM_SLEEP |
| 919 | static int ads7846_suspend(struct device *dev) |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 920 | { |
Mark Brown | 3c36e71 | 2011-01-20 22:51:26 -0800 | [diff] [blame] | 921 | struct ads7846 *ts = dev_get_drvdata(dev); |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 922 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 923 | mutex_lock(&ts->lock); |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 924 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 925 | if (!ts->suspended) { |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 926 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 927 | if (!ts->disabled) |
| 928 | __ads7846_disable(ts); |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 929 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 930 | if (device_may_wakeup(&ts->spi->dev)) |
| 931 | enable_irq_wake(ts->spi->irq); |
| 932 | |
| 933 | ts->suspended = true; |
| 934 | } |
| 935 | |
| 936 | mutex_unlock(&ts->lock); |
Ranjith Lohithakshan | fdba2bb | 2010-03-10 23:41:22 -0800 | [diff] [blame] | 937 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 938 | return 0; |
| 939 | } |
| 940 | |
Mark Brown | 3c36e71 | 2011-01-20 22:51:26 -0800 | [diff] [blame] | 941 | static int ads7846_resume(struct device *dev) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 942 | { |
Mark Brown | 3c36e71 | 2011-01-20 22:51:26 -0800 | [diff] [blame] | 943 | struct ads7846 *ts = dev_get_drvdata(dev); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 944 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 945 | mutex_lock(&ts->lock); |
Ranjith Lohithakshan | fdba2bb | 2010-03-10 23:41:22 -0800 | [diff] [blame] | 946 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 947 | if (ts->suspended) { |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 948 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 949 | ts->suspended = false; |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 950 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 951 | if (device_may_wakeup(&ts->spi->dev)) |
| 952 | disable_irq_wake(ts->spi->irq); |
| 953 | |
| 954 | if (!ts->disabled) |
| 955 | __ads7846_enable(ts); |
| 956 | } |
| 957 | |
| 958 | mutex_unlock(&ts->lock); |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 959 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 960 | return 0; |
| 961 | } |
Mark Brown | 3c36e71 | 2011-01-20 22:51:26 -0800 | [diff] [blame] | 962 | #endif |
| 963 | |
| 964 | static SIMPLE_DEV_PM_OPS(ads7846_pm, ads7846_suspend, ads7846_resume); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 965 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 966 | static int ads7846_setup_pendown(struct spi_device *spi, |
Dmitry Torokhov | 57691a1 | 2013-06-27 23:29:25 -0700 | [diff] [blame] | 967 | struct ads7846 *ts, |
| 968 | const struct ads7846_platform_data *pdata) |
Eric Miao | 4d5975e | 2008-09-10 12:06:15 -0400 | [diff] [blame] | 969 | { |
Eric Miao | 4d5975e | 2008-09-10 12:06:15 -0400 | [diff] [blame] | 970 | int err; |
| 971 | |
Dmitry Torokhov | 0fbc9fd | 2011-02-04 00:37:26 -0800 | [diff] [blame] | 972 | /* |
| 973 | * REVISIT when the irq can be triggered active-low, or if for some |
Eric Miao | 4d5975e | 2008-09-10 12:06:15 -0400 | [diff] [blame] | 974 | * reason the touchscreen isn't hooked up, we don't need to access |
| 975 | * the pendown state. |
| 976 | */ |
Eric Miao | 4d5975e | 2008-09-10 12:06:15 -0400 | [diff] [blame] | 977 | |
| 978 | if (pdata->get_pendown_state) { |
| 979 | ts->get_pendown_state = pdata->get_pendown_state; |
Dmitry Torokhov | 0fbc9fd | 2011-02-04 00:37:26 -0800 | [diff] [blame] | 980 | } else if (gpio_is_valid(pdata->gpio_pendown)) { |
Eric Miao | 4d5975e | 2008-09-10 12:06:15 -0400 | [diff] [blame] | 981 | |
Igor Grinberg | 58c2440 | 2011-06-27 13:06:27 -0700 | [diff] [blame] | 982 | err = gpio_request_one(pdata->gpio_pendown, GPIOF_IN, |
| 983 | "ads7846_pendown"); |
Dmitry Torokhov | 0fbc9fd | 2011-02-04 00:37:26 -0800 | [diff] [blame] | 984 | if (err) { |
Igor Grinberg | 58c2440 | 2011-06-27 13:06:27 -0700 | [diff] [blame] | 985 | dev_err(&spi->dev, |
| 986 | "failed to request/setup pendown GPIO%d: %d\n", |
| 987 | pdata->gpio_pendown, err); |
Igor Grinberg | 1201e7e | 2011-05-11 15:45:05 -0700 | [diff] [blame] | 988 | return err; |
| 989 | } |
Eric Miao | 4d5975e | 2008-09-10 12:06:15 -0400 | [diff] [blame] | 990 | |
Dmitry Torokhov | 0fbc9fd | 2011-02-04 00:37:26 -0800 | [diff] [blame] | 991 | ts->gpio_pendown = pdata->gpio_pendown; |
| 992 | |
Igor Grinberg | c4f4925 | 2012-11-20 23:00:10 -0800 | [diff] [blame] | 993 | if (pdata->gpio_pendown_debounce) |
| 994 | gpio_set_debounce(pdata->gpio_pendown, |
| 995 | pdata->gpio_pendown_debounce); |
Dmitry Torokhov | 0fbc9fd | 2011-02-04 00:37:26 -0800 | [diff] [blame] | 996 | } else { |
| 997 | dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n"); |
| 998 | return -EINVAL; |
| 999 | } |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1000 | |
Eric Miao | 4d5975e | 2008-09-10 12:06:15 -0400 | [diff] [blame] | 1001 | return 0; |
| 1002 | } |
| 1003 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1004 | /* |
| 1005 | * Set up the transfers to read touchscreen state; this assumes we |
| 1006 | * use formula #2 for pressure, not #3. |
| 1007 | */ |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 1008 | static void ads7846_setup_spi_msg(struct ads7846 *ts, |
Dmitry Torokhov | 57691a1 | 2013-06-27 23:29:25 -0700 | [diff] [blame] | 1009 | const struct ads7846_platform_data *pdata) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1010 | { |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1011 | struct spi_message *m = &ts->msg[0]; |
| 1012 | struct spi_transfer *x = ts->xfer; |
| 1013 | struct ads7846_packet *packet = ts->packet; |
| 1014 | int vref = pdata->keep_vref_on; |
Imre Deak | de2defd | 2007-01-18 00:45:21 -0500 | [diff] [blame] | 1015 | |
Michael Hennerich | 06a0912 | 2010-03-09 20:38:45 -0800 | [diff] [blame] | 1016 | if (ts->model == 7873) { |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1017 | /* |
| 1018 | * The AD7873 is almost identical to the ADS7846 |
Michael Hennerich | 06a0912 | 2010-03-09 20:38:45 -0800 | [diff] [blame] | 1019 | * keep VREF off during differential/ratiometric |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1020 | * conversion modes. |
Michael Hennerich | 06a0912 | 2010-03-09 20:38:45 -0800 | [diff] [blame] | 1021 | */ |
| 1022 | ts->model = 7846; |
| 1023 | vref = 0; |
| 1024 | } |
| 1025 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1026 | ts->msg_count = 1; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 1027 | spi_message_init(m); |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1028 | m->context = ts; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 1029 | |
Anatolij Gustschin | 3eac5c7 | 2010-07-01 09:01:56 -0700 | [diff] [blame] | 1030 | if (ts->model == 7845) { |
| 1031 | packet->read_y_cmd[0] = READ_Y(vref); |
| 1032 | packet->read_y_cmd[1] = 0; |
| 1033 | packet->read_y_cmd[2] = 0; |
| 1034 | x->tx_buf = &packet->read_y_cmd[0]; |
| 1035 | x->rx_buf = &packet->tc.y_buf[0]; |
| 1036 | x->len = 3; |
| 1037 | spi_message_add_tail(x, m); |
| 1038 | } else { |
| 1039 | /* y- still on; turn on only y+ (and ADC) */ |
| 1040 | packet->read_y = READ_Y(vref); |
| 1041 | x->tx_buf = &packet->read_y; |
| 1042 | x->len = 1; |
| 1043 | spi_message_add_tail(x, m); |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 1044 | |
Anatolij Gustschin | 3eac5c7 | 2010-07-01 09:01:56 -0700 | [diff] [blame] | 1045 | x++; |
| 1046 | x->rx_buf = &packet->tc.y; |
| 1047 | x->len = 2; |
| 1048 | spi_message_add_tail(x, m); |
| 1049 | } |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1050 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1051 | /* |
| 1052 | * The first sample after switching drivers can be low quality; |
Semih Hazar | e4f4886 | 2007-07-18 00:35:56 -0400 | [diff] [blame] | 1053 | * optionally discard it, using a second one after the signals |
| 1054 | * have had enough time to stabilize. |
| 1055 | */ |
| 1056 | if (pdata->settle_delay_usecs) { |
| 1057 | x->delay_usecs = pdata->settle_delay_usecs; |
| 1058 | |
| 1059 | x++; |
Dmitry Torokhov | e8f462d | 2008-10-09 00:52:23 -0400 | [diff] [blame] | 1060 | x->tx_buf = &packet->read_y; |
Semih Hazar | e4f4886 | 2007-07-18 00:35:56 -0400 | [diff] [blame] | 1061 | x->len = 1; |
| 1062 | spi_message_add_tail(x, m); |
| 1063 | |
| 1064 | x++; |
Dmitry Torokhov | e8f462d | 2008-10-09 00:52:23 -0400 | [diff] [blame] | 1065 | x->rx_buf = &packet->tc.y; |
Semih Hazar | e4f4886 | 2007-07-18 00:35:56 -0400 | [diff] [blame] | 1066 | x->len = 2; |
| 1067 | spi_message_add_tail(x, m); |
| 1068 | } |
| 1069 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1070 | ts->msg_count++; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 1071 | m++; |
| 1072 | spi_message_init(m); |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1073 | m->context = ts; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1074 | |
Anatolij Gustschin | 3eac5c7 | 2010-07-01 09:01:56 -0700 | [diff] [blame] | 1075 | if (ts->model == 7845) { |
| 1076 | x++; |
| 1077 | packet->read_x_cmd[0] = READ_X(vref); |
| 1078 | packet->read_x_cmd[1] = 0; |
| 1079 | packet->read_x_cmd[2] = 0; |
| 1080 | x->tx_buf = &packet->read_x_cmd[0]; |
| 1081 | x->rx_buf = &packet->tc.x_buf[0]; |
| 1082 | x->len = 3; |
| 1083 | spi_message_add_tail(x, m); |
| 1084 | } else { |
| 1085 | /* turn y- off, x+ on, then leave in lowpower */ |
| 1086 | x++; |
| 1087 | packet->read_x = READ_X(vref); |
| 1088 | x->tx_buf = &packet->read_x; |
| 1089 | x->len = 1; |
| 1090 | spi_message_add_tail(x, m); |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 1091 | |
Anatolij Gustschin | 3eac5c7 | 2010-07-01 09:01:56 -0700 | [diff] [blame] | 1092 | x++; |
| 1093 | x->rx_buf = &packet->tc.x; |
| 1094 | x->len = 2; |
| 1095 | spi_message_add_tail(x, m); |
| 1096 | } |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 1097 | |
Semih Hazar | e4f4886 | 2007-07-18 00:35:56 -0400 | [diff] [blame] | 1098 | /* ... maybe discard first sample ... */ |
| 1099 | if (pdata->settle_delay_usecs) { |
| 1100 | x->delay_usecs = pdata->settle_delay_usecs; |
| 1101 | |
| 1102 | x++; |
Dmitry Torokhov | e8f462d | 2008-10-09 00:52:23 -0400 | [diff] [blame] | 1103 | x->tx_buf = &packet->read_x; |
Semih Hazar | e4f4886 | 2007-07-18 00:35:56 -0400 | [diff] [blame] | 1104 | x->len = 1; |
| 1105 | spi_message_add_tail(x, m); |
| 1106 | |
| 1107 | x++; |
Dmitry Torokhov | e8f462d | 2008-10-09 00:52:23 -0400 | [diff] [blame] | 1108 | x->rx_buf = &packet->tc.x; |
Semih Hazar | e4f4886 | 2007-07-18 00:35:56 -0400 | [diff] [blame] | 1109 | x->len = 2; |
| 1110 | spi_message_add_tail(x, m); |
| 1111 | } |
| 1112 | |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 1113 | /* turn y+ off, x- on; we'll use formula #2 */ |
| 1114 | if (ts->model == 7846) { |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1115 | ts->msg_count++; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 1116 | m++; |
| 1117 | spi_message_init(m); |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1118 | m->context = ts; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 1119 | |
| 1120 | x++; |
Dmitry Torokhov | e8f462d | 2008-10-09 00:52:23 -0400 | [diff] [blame] | 1121 | packet->read_z1 = READ_Z1(vref); |
| 1122 | x->tx_buf = &packet->read_z1; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 1123 | x->len = 1; |
| 1124 | spi_message_add_tail(x, m); |
| 1125 | |
| 1126 | x++; |
Dmitry Torokhov | e8f462d | 2008-10-09 00:52:23 -0400 | [diff] [blame] | 1127 | x->rx_buf = &packet->tc.z1; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 1128 | x->len = 2; |
| 1129 | spi_message_add_tail(x, m); |
| 1130 | |
Semih Hazar | e4f4886 | 2007-07-18 00:35:56 -0400 | [diff] [blame] | 1131 | /* ... maybe discard first sample ... */ |
| 1132 | if (pdata->settle_delay_usecs) { |
| 1133 | x->delay_usecs = pdata->settle_delay_usecs; |
| 1134 | |
| 1135 | x++; |
Dmitry Torokhov | e8f462d | 2008-10-09 00:52:23 -0400 | [diff] [blame] | 1136 | x->tx_buf = &packet->read_z1; |
Semih Hazar | e4f4886 | 2007-07-18 00:35:56 -0400 | [diff] [blame] | 1137 | x->len = 1; |
| 1138 | spi_message_add_tail(x, m); |
| 1139 | |
| 1140 | x++; |
Dmitry Torokhov | e8f462d | 2008-10-09 00:52:23 -0400 | [diff] [blame] | 1141 | x->rx_buf = &packet->tc.z1; |
Semih Hazar | e4f4886 | 2007-07-18 00:35:56 -0400 | [diff] [blame] | 1142 | x->len = 2; |
| 1143 | spi_message_add_tail(x, m); |
| 1144 | } |
| 1145 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1146 | ts->msg_count++; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 1147 | m++; |
| 1148 | spi_message_init(m); |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1149 | m->context = ts; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 1150 | |
| 1151 | x++; |
Dmitry Torokhov | e8f462d | 2008-10-09 00:52:23 -0400 | [diff] [blame] | 1152 | packet->read_z2 = READ_Z2(vref); |
| 1153 | x->tx_buf = &packet->read_z2; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 1154 | x->len = 1; |
| 1155 | spi_message_add_tail(x, m); |
| 1156 | |
| 1157 | x++; |
Dmitry Torokhov | e8f462d | 2008-10-09 00:52:23 -0400 | [diff] [blame] | 1158 | x->rx_buf = &packet->tc.z2; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 1159 | x->len = 2; |
| 1160 | spi_message_add_tail(x, m); |
| 1161 | |
Semih Hazar | e4f4886 | 2007-07-18 00:35:56 -0400 | [diff] [blame] | 1162 | /* ... maybe discard first sample ... */ |
| 1163 | if (pdata->settle_delay_usecs) { |
| 1164 | x->delay_usecs = pdata->settle_delay_usecs; |
| 1165 | |
| 1166 | x++; |
Dmitry Torokhov | e8f462d | 2008-10-09 00:52:23 -0400 | [diff] [blame] | 1167 | x->tx_buf = &packet->read_z2; |
Semih Hazar | e4f4886 | 2007-07-18 00:35:56 -0400 | [diff] [blame] | 1168 | x->len = 1; |
| 1169 | spi_message_add_tail(x, m); |
| 1170 | |
| 1171 | x++; |
Dmitry Torokhov | e8f462d | 2008-10-09 00:52:23 -0400 | [diff] [blame] | 1172 | x->rx_buf = &packet->tc.z2; |
Semih Hazar | e4f4886 | 2007-07-18 00:35:56 -0400 | [diff] [blame] | 1173 | x->len = 2; |
| 1174 | spi_message_add_tail(x, m); |
| 1175 | } |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 1176 | } |
Imre Deak | 53a0ef89 | 2006-04-11 23:41:49 -0400 | [diff] [blame] | 1177 | |
| 1178 | /* power down */ |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1179 | ts->msg_count++; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 1180 | m++; |
| 1181 | spi_message_init(m); |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1182 | m->context = ts; |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 1183 | |
Anatolij Gustschin | 3eac5c7 | 2010-07-01 09:01:56 -0700 | [diff] [blame] | 1184 | if (ts->model == 7845) { |
| 1185 | x++; |
| 1186 | packet->pwrdown_cmd[0] = PWRDOWN; |
| 1187 | packet->pwrdown_cmd[1] = 0; |
| 1188 | packet->pwrdown_cmd[2] = 0; |
| 1189 | x->tx_buf = &packet->pwrdown_cmd[0]; |
| 1190 | x->len = 3; |
| 1191 | } else { |
| 1192 | x++; |
| 1193 | packet->pwrdown = PWRDOWN; |
| 1194 | x->tx_buf = &packet->pwrdown; |
| 1195 | x->len = 1; |
| 1196 | spi_message_add_tail(x, m); |
Imre Deak | 53a0ef89 | 2006-04-11 23:41:49 -0400 | [diff] [blame] | 1197 | |
Anatolij Gustschin | 3eac5c7 | 2010-07-01 09:01:56 -0700 | [diff] [blame] | 1198 | x++; |
| 1199 | x->rx_buf = &packet->dummy; |
| 1200 | x->len = 2; |
| 1201 | } |
| 1202 | |
David Brownell | d93f70b | 2006-02-15 00:49:35 -0500 | [diff] [blame] | 1203 | CS_CHANGE(*x); |
Imre Deak | 0b7018a | 2006-04-11 23:42:03 -0400 | [diff] [blame] | 1204 | spi_message_add_tail(x, m); |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1205 | } |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1206 | |
Daniel Mack | a608026 | 2013-06-27 23:42:17 -0700 | [diff] [blame] | 1207 | #ifdef CONFIG_OF |
| 1208 | static const struct of_device_id ads7846_dt_ids[] = { |
| 1209 | { .compatible = "ti,tsc2046", .data = (void *) 7846 }, |
| 1210 | { .compatible = "ti,ads7843", .data = (void *) 7843 }, |
| 1211 | { .compatible = "ti,ads7845", .data = (void *) 7845 }, |
| 1212 | { .compatible = "ti,ads7846", .data = (void *) 7846 }, |
| 1213 | { .compatible = "ti,ads7873", .data = (void *) 7873 }, |
| 1214 | { } |
| 1215 | }; |
| 1216 | MODULE_DEVICE_TABLE(of, ads7846_dt_ids); |
| 1217 | |
| 1218 | static const struct ads7846_platform_data *ads7846_probe_dt(struct device *dev) |
| 1219 | { |
| 1220 | struct ads7846_platform_data *pdata; |
| 1221 | struct device_node *node = dev->of_node; |
| 1222 | const struct of_device_id *match; |
| 1223 | |
| 1224 | if (!node) { |
| 1225 | dev_err(dev, "Device does not have associated DT data\n"); |
| 1226 | return ERR_PTR(-EINVAL); |
| 1227 | } |
| 1228 | |
| 1229 | match = of_match_device(ads7846_dt_ids, dev); |
| 1230 | if (!match) { |
| 1231 | dev_err(dev, "Unknown device model\n"); |
| 1232 | return ERR_PTR(-EINVAL); |
| 1233 | } |
| 1234 | |
| 1235 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 1236 | if (!pdata) |
| 1237 | return ERR_PTR(-ENOMEM); |
| 1238 | |
| 1239 | pdata->model = (unsigned long)match->data; |
| 1240 | |
| 1241 | of_property_read_u16(node, "ti,vref-delay-usecs", |
| 1242 | &pdata->vref_delay_usecs); |
| 1243 | of_property_read_u16(node, "ti,vref-mv", &pdata->vref_mv); |
| 1244 | pdata->keep_vref_on = of_property_read_bool(node, "ti,keep-vref-on"); |
| 1245 | |
| 1246 | pdata->swap_xy = of_property_read_bool(node, "ti,swap-xy"); |
| 1247 | |
| 1248 | of_property_read_u16(node, "ti,settle-delay-usec", |
| 1249 | &pdata->settle_delay_usecs); |
| 1250 | of_property_read_u16(node, "ti,penirq-recheck-delay-usecs", |
| 1251 | &pdata->penirq_recheck_delay_usecs); |
| 1252 | |
| 1253 | of_property_read_u16(node, "ti,x-plate-ohms", &pdata->x_plate_ohms); |
| 1254 | of_property_read_u16(node, "ti,y-plate-ohms", &pdata->y_plate_ohms); |
| 1255 | |
| 1256 | of_property_read_u16(node, "ti,x-min", &pdata->x_min); |
| 1257 | of_property_read_u16(node, "ti,y-min", &pdata->y_min); |
| 1258 | of_property_read_u16(node, "ti,x-max", &pdata->x_max); |
| 1259 | of_property_read_u16(node, "ti,y-max", &pdata->y_max); |
| 1260 | |
| 1261 | of_property_read_u16(node, "ti,pressure-min", &pdata->pressure_min); |
| 1262 | of_property_read_u16(node, "ti,pressure-max", &pdata->pressure_max); |
| 1263 | |
| 1264 | of_property_read_u16(node, "ti,debounce-max", &pdata->debounce_max); |
| 1265 | of_property_read_u16(node, "ti,debounce-tol", &pdata->debounce_tol); |
| 1266 | of_property_read_u16(node, "ti,debounce-rep", &pdata->debounce_rep); |
| 1267 | |
| 1268 | of_property_read_u32(node, "ti,pendown-gpio-debounce", |
| 1269 | &pdata->gpio_pendown_debounce); |
| 1270 | |
| 1271 | pdata->wakeup = of_property_read_bool(node, "linux,wakeup"); |
| 1272 | |
| 1273 | pdata->gpio_pendown = of_get_named_gpio(dev->of_node, "pendown-gpio", 0); |
| 1274 | |
| 1275 | return pdata; |
| 1276 | } |
| 1277 | #else |
| 1278 | static const struct ads7846_platform_data *ads7846_probe_dt(struct device *dev) |
| 1279 | { |
| 1280 | dev_err(dev, "no platform data defined\n"); |
| 1281 | return ERR_PTR(-EINVAL); |
| 1282 | } |
| 1283 | #endif |
| 1284 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 1285 | static int ads7846_probe(struct spi_device *spi) |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1286 | { |
Daniel Mack | a608026 | 2013-06-27 23:42:17 -0700 | [diff] [blame] | 1287 | const struct ads7846_platform_data *pdata; |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1288 | struct ads7846 *ts; |
| 1289 | struct ads7846_packet *packet; |
| 1290 | struct input_dev *input_dev; |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1291 | unsigned long irq_flags; |
| 1292 | int err; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1293 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1294 | if (!spi->irq) { |
| 1295 | dev_dbg(&spi->dev, "no IRQ?\n"); |
Daniel Mack | a608026 | 2013-06-27 23:42:17 -0700 | [diff] [blame] | 1296 | return -EINVAL; |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | /* don't exceed max specified sample rate */ |
| 1300 | if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) { |
Daniel Mack | a608026 | 2013-06-27 23:42:17 -0700 | [diff] [blame] | 1301 | dev_err(&spi->dev, "f(sample) %d KHz?\n", |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1302 | (spi->max_speed_hz/SAMPLE_BITS)/1000); |
| 1303 | return -EINVAL; |
| 1304 | } |
| 1305 | |
Daniel Mack | a608026 | 2013-06-27 23:42:17 -0700 | [diff] [blame] | 1306 | /* |
| 1307 | * We'd set TX word size 8 bits and RX word size to 13 bits ... except |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1308 | * that even if the hardware can do that, the SPI controller driver |
| 1309 | * may not. So we stick to very-portable 8 bit words, both RX and TX. |
| 1310 | */ |
| 1311 | spi->bits_per_word = 8; |
| 1312 | spi->mode = SPI_MODE_0; |
| 1313 | err = spi_setup(spi); |
| 1314 | if (err < 0) |
| 1315 | return err; |
| 1316 | |
| 1317 | ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL); |
| 1318 | packet = kzalloc(sizeof(struct ads7846_packet), GFP_KERNEL); |
| 1319 | input_dev = input_allocate_device(); |
| 1320 | if (!ts || !packet || !input_dev) { |
| 1321 | err = -ENOMEM; |
| 1322 | goto err_free_mem; |
| 1323 | } |
| 1324 | |
Jingoo Han | c12454f | 2013-04-07 20:52:07 -0700 | [diff] [blame] | 1325 | spi_set_drvdata(spi, ts); |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1326 | |
| 1327 | ts->packet = packet; |
| 1328 | ts->spi = spi; |
| 1329 | ts->input = input_dev; |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1330 | |
| 1331 | mutex_init(&ts->lock); |
| 1332 | init_waitqueue_head(&ts->wait); |
| 1333 | |
Daniel Mack | a608026 | 2013-06-27 23:42:17 -0700 | [diff] [blame] | 1334 | pdata = dev_get_platdata(&spi->dev); |
| 1335 | if (!pdata) { |
| 1336 | pdata = ads7846_probe_dt(&spi->dev); |
| 1337 | if (IS_ERR(pdata)) |
| 1338 | return PTR_ERR(pdata); |
| 1339 | } |
| 1340 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1341 | ts->model = pdata->model ? : 7846; |
| 1342 | ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100; |
| 1343 | ts->x_plate_ohms = pdata->x_plate_ohms ? : 400; |
| 1344 | ts->pressure_max = pdata->pressure_max ? : ~0; |
| 1345 | |
Daniel Mack | a608026 | 2013-06-27 23:42:17 -0700 | [diff] [blame] | 1346 | ts->vref_mv = pdata->vref_mv; |
| 1347 | ts->swap_xy = pdata->swap_xy; |
| 1348 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1349 | if (pdata->filter != NULL) { |
| 1350 | if (pdata->filter_init != NULL) { |
| 1351 | err = pdata->filter_init(pdata, &ts->filter_data); |
| 1352 | if (err < 0) |
| 1353 | goto err_free_mem; |
| 1354 | } |
| 1355 | ts->filter = pdata->filter; |
| 1356 | ts->filter_cleanup = pdata->filter_cleanup; |
| 1357 | } else if (pdata->debounce_max) { |
| 1358 | ts->debounce_max = pdata->debounce_max; |
| 1359 | if (ts->debounce_max < 2) |
| 1360 | ts->debounce_max = 2; |
| 1361 | ts->debounce_tol = pdata->debounce_tol; |
| 1362 | ts->debounce_rep = pdata->debounce_rep; |
| 1363 | ts->filter = ads7846_debounce_filter; |
| 1364 | ts->filter_data = ts; |
| 1365 | } else { |
| 1366 | ts->filter = ads7846_no_filter; |
| 1367 | } |
| 1368 | |
Dmitry Torokhov | 57691a1 | 2013-06-27 23:29:25 -0700 | [diff] [blame] | 1369 | err = ads7846_setup_pendown(spi, ts, pdata); |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1370 | if (err) |
| 1371 | goto err_cleanup_filter; |
| 1372 | |
| 1373 | if (pdata->penirq_recheck_delay_usecs) |
| 1374 | ts->penirq_recheck_delay_usecs = |
| 1375 | pdata->penirq_recheck_delay_usecs; |
| 1376 | |
| 1377 | ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync; |
| 1378 | |
| 1379 | snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev)); |
| 1380 | snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model); |
| 1381 | |
| 1382 | input_dev->name = ts->name; |
| 1383 | input_dev->phys = ts->phys; |
| 1384 | input_dev->dev.parent = &spi->dev; |
| 1385 | |
| 1386 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); |
| 1387 | input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH); |
| 1388 | input_set_abs_params(input_dev, ABS_X, |
| 1389 | pdata->x_min ? : 0, |
| 1390 | pdata->x_max ? : MAX_12BIT, |
| 1391 | 0, 0); |
| 1392 | input_set_abs_params(input_dev, ABS_Y, |
| 1393 | pdata->y_min ? : 0, |
| 1394 | pdata->y_max ? : MAX_12BIT, |
| 1395 | 0, 0); |
| 1396 | input_set_abs_params(input_dev, ABS_PRESSURE, |
| 1397 | pdata->pressure_min, pdata->pressure_max, 0, 0); |
| 1398 | |
| 1399 | ads7846_setup_spi_msg(ts, pdata); |
Imre Deak | d5b415c | 2006-04-26 00:13:18 -0400 | [diff] [blame] | 1400 | |
Grazvydas Ignotas | 9114337 | 2010-02-25 02:04:56 -0800 | [diff] [blame] | 1401 | ts->reg = regulator_get(&spi->dev, "vcc"); |
| 1402 | if (IS_ERR(ts->reg)) { |
Kevin Hilman | 067fb2f | 2010-05-26 23:30:55 -0700 | [diff] [blame] | 1403 | err = PTR_ERR(ts->reg); |
Dmitry Torokhov | 56960b3 | 2010-06-02 00:40:06 -0700 | [diff] [blame] | 1404 | dev_err(&spi->dev, "unable to get regulator: %d\n", err); |
Grazvydas Ignotas | 9114337 | 2010-02-25 02:04:56 -0800 | [diff] [blame] | 1405 | goto err_free_gpio; |
| 1406 | } |
| 1407 | |
| 1408 | err = regulator_enable(ts->reg); |
| 1409 | if (err) { |
| 1410 | dev_err(&spi->dev, "unable to enable regulator: %d\n", err); |
| 1411 | goto err_put_regulator; |
| 1412 | } |
| 1413 | |
Dmitry Torokhov | 0f622bf | 2010-07-01 09:01:50 -0700 | [diff] [blame] | 1414 | irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING; |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1415 | irq_flags |= IRQF_ONESHOT; |
Anatolij Gustschin | 7804302 | 2010-06-28 01:25:19 -0700 | [diff] [blame] | 1416 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1417 | err = request_threaded_irq(spi->irq, ads7846_hard_irq, ads7846_irq, |
| 1418 | irq_flags, spi->dev.driver->name, ts); |
Dmitry Torokhov | 0f622bf | 2010-07-01 09:01:50 -0700 | [diff] [blame] | 1419 | if (err && !pdata->irq_flags) { |
Michael Roth | c57c0a2 | 2009-06-10 23:30:55 -0700 | [diff] [blame] | 1420 | dev_info(&spi->dev, |
| 1421 | "trying pin change workaround on irq %d\n", spi->irq); |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1422 | irq_flags |= IRQF_TRIGGER_RISING; |
| 1423 | err = request_threaded_irq(spi->irq, |
| 1424 | ads7846_hard_irq, ads7846_irq, |
| 1425 | irq_flags, spi->dev.driver->name, ts); |
Dmitry Torokhov | 0f622bf | 2010-07-01 09:01:50 -0700 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | if (err) { |
| 1429 | dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq); |
| 1430 | goto err_disable_regulator; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1431 | } |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1432 | |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 1433 | err = ads784x_hwmon_register(spi, ts); |
| 1434 | if (err) |
| 1435 | goto err_free_irq; |
| 1436 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1437 | dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1438 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1439 | /* |
| 1440 | * Take a first sample, leaving nPENIRQ active and vREF off; avoid |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1441 | * the touchscreen, in case it's not connected. |
| 1442 | */ |
Anatolij Gustschin | 3eac5c7 | 2010-07-01 09:01:56 -0700 | [diff] [blame] | 1443 | if (ts->model == 7845) |
| 1444 | ads7845_read12_ser(&spi->dev, PWRDOWN); |
| 1445 | else |
Alexander Stein | ebcaaad | 2011-05-11 16:24:08 -0700 | [diff] [blame] | 1446 | (void) ads7846_read12_ser(&spi->dev, READ_12BIT_SER(vaux)); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1447 | |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 1448 | err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group); |
Dmitry Torokhov | 8dd5165 | 2006-11-02 23:34:09 -0500 | [diff] [blame] | 1449 | if (err) |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 1450 | goto err_remove_hwmon; |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 1451 | |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 1452 | err = input_register_device(input_dev); |
| 1453 | if (err) |
Dmitry Torokhov | 8dd5165 | 2006-11-02 23:34:09 -0500 | [diff] [blame] | 1454 | goto err_remove_attr_group; |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 1455 | |
Ranjith Lohithakshan | fdba2bb | 2010-03-10 23:41:22 -0800 | [diff] [blame] | 1456 | device_init_wakeup(&spi->dev, pdata->wakeup); |
| 1457 | |
Daniel Mack | a608026 | 2013-06-27 23:42:17 -0700 | [diff] [blame] | 1458 | /* |
| 1459 | * If device does not carry platform data we must have allocated it |
| 1460 | * when parsing DT data. |
| 1461 | */ |
| 1462 | if (!dev_get_platdata(&spi->dev)) |
| 1463 | devm_kfree(&spi->dev, (void *)pdata); |
| 1464 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1465 | return 0; |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 1466 | |
Dmitry Torokhov | 8dd5165 | 2006-11-02 23:34:09 -0500 | [diff] [blame] | 1467 | err_remove_attr_group: |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 1468 | sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group); |
| 1469 | err_remove_hwmon: |
| 1470 | ads784x_hwmon_unregister(spi, ts); |
Dmitry Torokhov | 8dd5165 | 2006-11-02 23:34:09 -0500 | [diff] [blame] | 1471 | err_free_irq: |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 1472 | free_irq(spi->irq, ts); |
Grazvydas Ignotas | 9114337 | 2010-02-25 02:04:56 -0800 | [diff] [blame] | 1473 | err_disable_regulator: |
| 1474 | regulator_disable(ts->reg); |
| 1475 | err_put_regulator: |
| 1476 | regulator_put(ts->reg); |
Eric Miao | 4d5975e | 2008-09-10 12:06:15 -0400 | [diff] [blame] | 1477 | err_free_gpio: |
Dmitry Torokhov | 0fbc9fd | 2011-02-04 00:37:26 -0800 | [diff] [blame] | 1478 | if (!ts->get_pendown_state) |
Eric Miao | 4d5975e | 2008-09-10 12:06:15 -0400 | [diff] [blame] | 1479 | gpio_free(ts->gpio_pendown); |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 1480 | err_cleanup_filter: |
| 1481 | if (ts->filter_cleanup) |
| 1482 | ts->filter_cleanup(ts->filter_data); |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 1483 | err_free_mem: |
| 1484 | input_free_device(input_dev); |
Dmitry Torokhov | e8f462d | 2008-10-09 00:52:23 -0400 | [diff] [blame] | 1485 | kfree(packet); |
Dmitry Torokhov | a90f7e9 | 2006-02-15 00:49:22 -0500 | [diff] [blame] | 1486 | kfree(ts); |
| 1487 | return err; |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1488 | } |
| 1489 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 1490 | static int ads7846_remove(struct spi_device *spi) |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1491 | { |
Jingoo Han | c12454f | 2013-04-07 20:52:07 -0700 | [diff] [blame] | 1492 | struct ads7846 *ts = spi_get_drvdata(spi); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1493 | |
Ranjith Lohithakshan | fdba2bb | 2010-03-10 23:41:22 -0800 | [diff] [blame] | 1494 | device_init_wakeup(&spi->dev, false); |
| 1495 | |
David Brownell | 2c8dc07 | 2007-01-18 00:45:48 -0500 | [diff] [blame] | 1496 | sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1497 | |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1498 | ads7846_disable(ts); |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 1499 | free_irq(ts->spi->irq, ts); |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1500 | |
| 1501 | input_unregister_device(ts->input); |
| 1502 | |
| 1503 | ads784x_hwmon_unregister(spi, ts); |
Imre Deak | 7de90a8 | 2006-04-11 23:43:55 -0400 | [diff] [blame] | 1504 | |
Grazvydas Ignotas | 9114337 | 2010-02-25 02:04:56 -0800 | [diff] [blame] | 1505 | regulator_disable(ts->reg); |
| 1506 | regulator_put(ts->reg); |
| 1507 | |
Dmitry Torokhov | 0fbc9fd | 2011-02-04 00:37:26 -0800 | [diff] [blame] | 1508 | if (!ts->get_pendown_state) { |
| 1509 | /* |
| 1510 | * If we are not using specialized pendown method we must |
| 1511 | * have been relying on gpio we set up ourselves. |
| 1512 | */ |
Eric Miao | 4d5975e | 2008-09-10 12:06:15 -0400 | [diff] [blame] | 1513 | gpio_free(ts->gpio_pendown); |
Dmitry Torokhov | 0fbc9fd | 2011-02-04 00:37:26 -0800 | [diff] [blame] | 1514 | } |
Eric Miao | 4d5975e | 2008-09-10 12:06:15 -0400 | [diff] [blame] | 1515 | |
Imre Deak | da970e6 | 2007-01-18 00:44:41 -0500 | [diff] [blame] | 1516 | if (ts->filter_cleanup) |
| 1517 | ts->filter_cleanup(ts->filter_data); |
| 1518 | |
Dmitry Torokhov | e8f462d | 2008-10-09 00:52:23 -0400 | [diff] [blame] | 1519 | kfree(ts->packet); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1520 | kfree(ts); |
| 1521 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1522 | dev_dbg(&spi->dev, "unregistered touchscreen\n"); |
Jason Wang | 2991a1c | 2010-10-13 11:35:40 -0700 | [diff] [blame] | 1523 | |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1524 | return 0; |
| 1525 | } |
| 1526 | |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1527 | static struct spi_driver ads7846_driver = { |
| 1528 | .driver = { |
| 1529 | .name = "ads7846", |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1530 | .owner = THIS_MODULE, |
Mark Brown | 3c36e71 | 2011-01-20 22:51:26 -0800 | [diff] [blame] | 1531 | .pm = &ads7846_pm, |
Daniel Mack | a608026 | 2013-06-27 23:42:17 -0700 | [diff] [blame] | 1532 | .of_match_table = of_match_ptr(ads7846_dt_ids), |
David Brownell | 2e5a7bd | 2006-01-08 13:34:25 -0800 | [diff] [blame] | 1533 | }, |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1534 | .probe = ads7846_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 1535 | .remove = ads7846_remove, |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1536 | }; |
| 1537 | |
Axel Lin | ca83922 | 2012-03-16 23:05:26 -0700 | [diff] [blame] | 1538 | module_spi_driver(ads7846_driver); |
David Brownell | ffa458c | 2006-01-08 13:34:21 -0800 | [diff] [blame] | 1539 | |
| 1540 | MODULE_DESCRIPTION("ADS7846 TouchScreen Driver"); |
| 1541 | MODULE_LICENSE("GPL"); |
Anton Vorontsov | e0626e3 | 2009-09-22 16:46:08 -0700 | [diff] [blame] | 1542 | MODULE_ALIAS("spi:ads7846"); |