aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/touchscreen/ads7846.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/input/touchscreen/ads7846.c')
-rw-r--r--drivers/input/touchscreen/ads7846.c581
1 files changed, 389 insertions, 192 deletions
diff --git a/drivers/input/touchscreen/ads7846.c b/drivers/input/touchscreen/ads7846.c
index c6164b6f476a..cd251efda410 100644
--- a/drivers/input/touchscreen/ads7846.c
+++ b/drivers/input/touchscreen/ads7846.c
@@ -17,8 +17,9 @@
17 * it under the terms of the GNU General Public License version 2 as 17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation. 18 * published by the Free Software Foundation.
19 */ 19 */
20#include <linux/device.h> 20#include <linux/hwmon.h>
21#include <linux/init.h> 21#include <linux/init.h>
22#include <linux/err.h>
22#include <linux/delay.h> 23#include <linux/delay.h>
23#include <linux/input.h> 24#include <linux/input.h>
24#include <linux/interrupt.h> 25#include <linux/interrupt.h>
@@ -54,7 +55,8 @@
54 * files. 55 * files.
55 */ 56 */
56 57
57#define TS_POLL_PERIOD msecs_to_jiffies(10) 58#define TS_POLL_DELAY (1 * 1000000) /* ns delay before the first sample */
59#define TS_POLL_PERIOD (5 * 1000000) /* ns delay between samples */
58 60
59/* this driver doesn't aim at the peak continuous sample rate */ 61/* this driver doesn't aim at the peak continuous sample rate */
60#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */) 62#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
@@ -63,12 +65,12 @@ struct ts_event {
63 /* For portability, we can't read 12 bit values using SPI (which 65 /* For portability, we can't read 12 bit values using SPI (which
64 * would make the controller deliver them as native byteorder u16 66 * would make the controller deliver them as native byteorder u16
65 * with msbs zeroed). Instead, we read them as two 8-bit values, 67 * with msbs zeroed). Instead, we read them as two 8-bit values,
66 * which need byteswapping then range adjustment. 68 * *** WHICH NEED BYTESWAPPING *** and range adjustment.
67 */ 69 */
68 __be16 x; 70 u16 x;
69 __be16 y; 71 u16 y;
70 __be16 z1, z2; 72 u16 z1, z2;
71 int ignore; 73 int ignore;
72}; 74};
73 75
74struct ads7846 { 76struct ads7846 {
@@ -76,7 +78,12 @@ struct ads7846 {
76 char phys[32]; 78 char phys[32];
77 79
78 struct spi_device *spi; 80 struct spi_device *spi;
81
82#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
79 struct attribute_group *attr_group; 83 struct attribute_group *attr_group;
84 struct class_device *hwmon;
85#endif
86
80 u16 model; 87 u16 model;
81 u16 vref_delay_usecs; 88 u16 vref_delay_usecs;
82 u16 x_plate_ohms; 89 u16 x_plate_ohms;
@@ -99,13 +106,16 @@ struct ads7846 {
99 u16 debounce_rep; 106 u16 debounce_rep;
100 107
101 spinlock_t lock; 108 spinlock_t lock;
102 struct timer_list timer; /* P: lock */ 109 struct hrtimer timer;
103 unsigned pendown:1; /* P: lock */ 110 unsigned pendown:1; /* P: lock */
104 unsigned pending:1; /* P: lock */ 111 unsigned pending:1; /* P: lock */
105// FIXME remove "irq_disabled" 112// FIXME remove "irq_disabled"
106 unsigned irq_disabled:1; /* P: lock */ 113 unsigned irq_disabled:1; /* P: lock */
107 unsigned disabled:1; 114 unsigned disabled:1;
108 115
116 int (*filter)(void *data, int data_idx, int *val);
117 void *filter_data;
118 void (*filter_cleanup)(void *data);
109 int (*get_pendown_state)(void); 119 int (*get_pendown_state)(void);
110}; 120};
111 121
@@ -142,15 +152,16 @@ struct ads7846 {
142#define MAX_12BIT ((1<<12)-1) 152#define MAX_12BIT ((1<<12)-1)
143 153
144/* leave ADC powered up (disables penirq) between differential samples */ 154/* leave ADC powered up (disables penirq) between differential samples */
145#define READ_12BIT_DFR(x) (ADS_START | ADS_A2A1A0_d_ ## x \ 155#define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \
146 | ADS_12_BIT | ADS_DFR) 156 | ADS_12_BIT | ADS_DFR | \
157 (adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))
147 158
148#define READ_Y (READ_12BIT_DFR(y) | ADS_PD10_ADC_ON) 159#define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))
149#define READ_Z1 (READ_12BIT_DFR(z1) | ADS_PD10_ADC_ON) 160#define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))
150#define READ_Z2 (READ_12BIT_DFR(z2) | ADS_PD10_ADC_ON) 161#define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))
151 162
152#define READ_X (READ_12BIT_DFR(x) | ADS_PD10_ADC_ON) 163#define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))
153#define PWRDOWN (READ_12BIT_DFR(y) | ADS_PD10_PDOWN) /* LAST */ 164#define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */
154 165
155/* single-ended samples need to first power up reference voltage; 166/* single-ended samples need to first power up reference voltage;
156 * we leave both ADC and VREF powered 167 * we leave both ADC and VREF powered
@@ -158,14 +169,19 @@ struct ads7846 {
158#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \ 169#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
159 | ADS_12_BIT | ADS_SER) 170 | ADS_12_BIT | ADS_SER)
160 171
161#define REF_ON (READ_12BIT_DFR(x) | ADS_PD10_ALL_ON) 172#define REF_ON (READ_12BIT_DFR(x, 1, 1))
162#define REF_OFF (READ_12BIT_DFR(y) | ADS_PD10_PDOWN) 173#define REF_OFF (READ_12BIT_DFR(y, 0, 0))
163 174
164/*--------------------------------------------------------------------------*/ 175/*--------------------------------------------------------------------------*/
165 176
166/* 177/*
167 * Non-touchscreen sensors only use single-ended conversions. 178 * Non-touchscreen sensors only use single-ended conversions.
179 * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
180 * ads7846 lets that pin be unconnected, to use internal vREF.
168 */ 181 */
182static unsigned vREF_mV;
183module_param(vREF_mV, uint, 0);
184MODULE_PARM_DESC(vREF_mV, "external vREF voltage, in milliVolts");
169 185
170struct ser_req { 186struct ser_req {
171 u8 ref_on; 187 u8 ref_on;
@@ -193,50 +209,55 @@ static int ads7846_read12_ser(struct device *dev, unsigned command)
193 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL); 209 struct ser_req *req = kzalloc(sizeof *req, GFP_KERNEL);
194 int status; 210 int status;
195 int sample; 211 int sample;
196 int i; 212 int use_internal;
197 213
198 if (!req) 214 if (!req)
199 return -ENOMEM; 215 return -ENOMEM;
200 216
201 spi_message_init(&req->msg); 217 spi_message_init(&req->msg);
202 218
203 /* activate reference, so it has time to settle; */ 219 /* FIXME boards with ads7846 might use external vref instead ... */
204 req->ref_on = REF_ON; 220 use_internal = (ts->model == 7846);
205 req->xfer[0].tx_buf = &req->ref_on; 221
206 req->xfer[0].len = 1; 222 /* maybe turn on internal vREF, and let it settle */
207 req->xfer[1].rx_buf = &req->scratch; 223 if (use_internal) {
208 req->xfer[1].len = 2; 224 req->ref_on = REF_ON;
209 225 req->xfer[0].tx_buf = &req->ref_on;
210 /* 226 req->xfer[0].len = 1;
211 * for external VREF, 0 usec (and assume it's always on); 227 spi_message_add_tail(&req->xfer[0], &req->msg);
212 * for 1uF, use 800 usec; 228
213 * no cap, 100 usec. 229 req->xfer[1].rx_buf = &req->scratch;
214 */ 230 req->xfer[1].len = 2;
215 req->xfer[1].delay_usecs = ts->vref_delay_usecs; 231
232 /* for 1uF, settle for 800 usec; no cap, 100 usec. */
233 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
234 spi_message_add_tail(&req->xfer[1], &req->msg);
235 }
216 236
217 /* take sample */ 237 /* take sample */
218 req->command = (u8) command; 238 req->command = (u8) command;
219 req->xfer[2].tx_buf = &req->command; 239 req->xfer[2].tx_buf = &req->command;
220 req->xfer[2].len = 1; 240 req->xfer[2].len = 1;
241 spi_message_add_tail(&req->xfer[2], &req->msg);
242
221 req->xfer[3].rx_buf = &req->sample; 243 req->xfer[3].rx_buf = &req->sample;
222 req->xfer[3].len = 2; 244 req->xfer[3].len = 2;
245 spi_message_add_tail(&req->xfer[3], &req->msg);
223 246
224 /* REVISIT: take a few more samples, and compare ... */ 247 /* REVISIT: take a few more samples, and compare ... */
225 248
226 /* turn off reference */ 249 /* maybe off internal vREF */
227 req->ref_off = REF_OFF; 250 if (use_internal) {
228 req->xfer[4].tx_buf = &req->ref_off; 251 req->ref_off = REF_OFF;
229 req->xfer[4].len = 1; 252 req->xfer[4].tx_buf = &req->ref_off;
230 req->xfer[5].rx_buf = &req->scratch; 253 req->xfer[4].len = 1;
231 req->xfer[5].len = 2; 254 spi_message_add_tail(&req->xfer[4], &req->msg);
232 255
233 CS_CHANGE(req->xfer[5]); 256 req->xfer[5].rx_buf = &req->scratch;
234 257 req->xfer[5].len = 2;
235 /* group all the transfers together, so we can't interfere with 258 CS_CHANGE(req->xfer[5]);
236 * reading touchscreen state; disable penirq while sampling 259 spi_message_add_tail(&req->xfer[5], &req->msg);
237 */ 260 }
238 for (i = 0; i < 6; i++)
239 spi_message_add_tail(&req->xfer[i], &req->msg);
240 261
241 ts->irq_disabled = 1; 262 ts->irq_disabled = 1;
242 disable_irq(spi->irq); 263 disable_irq(spi->irq);
@@ -256,25 +277,173 @@ static int ads7846_read12_ser(struct device *dev, unsigned command)
256 return status ? status : sample; 277 return status ? status : sample;
257} 278}
258 279
259#define SHOW(name) static ssize_t \ 280#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)
281
282#define SHOW(name, var, adjust) static ssize_t \
260name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \ 283name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
261{ \ 284{ \
285 struct ads7846 *ts = dev_get_drvdata(dev); \
262 ssize_t v = ads7846_read12_ser(dev, \ 286 ssize_t v = ads7846_read12_ser(dev, \
263 READ_12BIT_SER(name) | ADS_PD10_ALL_ON); \ 287 READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
264 if (v < 0) \ 288 if (v < 0) \
265 return v; \ 289 return v; \
266 return sprintf(buf, "%u\n", (unsigned) v); \ 290 return sprintf(buf, "%u\n", adjust(ts, v)); \
267} \ 291} \
268static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL); 292static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
269 293
270SHOW(temp0) 294
271SHOW(temp1) 295/* Sysfs conventions report temperatures in millidegrees Celcius.
272SHOW(vaux) 296 * ADS7846 could use the low-accuracy two-sample scheme, but can't do the high
273SHOW(vbatt) 297 * accuracy scheme without calibration data. For now we won't try either;
298 * userspace sees raw sensor values, and must scale/calibrate appropriately.
299 */
300static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
301{
302 return v;
303}
304
305SHOW(temp0, temp0, null_adjust) /* temp1_input */
306SHOW(temp1, temp1, null_adjust) /* temp2_input */
307
308
309/* sysfs conventions report voltages in millivolts. We can convert voltages
310 * if we know vREF. userspace may need to scale vAUX to match the board's
311 * external resistors; we assume that vBATT only uses the internal ones.
312 */
313static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
314{
315 unsigned retval = v;
316
317 /* external resistors may scale vAUX into 0..vREF */
318 retval *= vREF_mV;
319 retval = retval >> 12;
320 return retval;
321}
322
323static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
324{
325 unsigned retval = vaux_adjust(ts, v);
326
327 /* ads7846 has a resistor ladder to scale this signal down */
328 if (ts->model == 7846)
329 retval *= 4;
330 return retval;
331}
332
333SHOW(in0_input, vaux, vaux_adjust)
334SHOW(in1_input, vbatt, vbatt_adjust)
335
336
337static struct attribute *ads7846_attributes[] = {
338 &dev_attr_temp0.attr,
339 &dev_attr_temp1.attr,
340 &dev_attr_in0_input.attr,
341 &dev_attr_in1_input.attr,
342 NULL,
343};
344
345static struct attribute_group ads7846_attr_group = {
346 .attrs = ads7846_attributes,
347};
348
349static struct attribute *ads7843_attributes[] = {
350 &dev_attr_in0_input.attr,
351 &dev_attr_in1_input.attr,
352 NULL,
353};
354
355static struct attribute_group ads7843_attr_group = {
356 .attrs = ads7843_attributes,
357};
358
359static struct attribute *ads7845_attributes[] = {
360 &dev_attr_in0_input.attr,
361 NULL,
362};
363
364static struct attribute_group ads7845_attr_group = {
365 .attrs = ads7845_attributes,
366};
367
368static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)
369{
370 struct class_device *hwmon;
371 int err;
372
373 /* hwmon sensors need a reference voltage */
374 switch (ts->model) {
375 case 7846:
376 if (!vREF_mV) {
377 dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");
378 vREF_mV = 2500;
379 }
380 break;
381 case 7845:
382 case 7843:
383 if (!vREF_mV) {
384 dev_warn(&spi->dev,
385 "external vREF for ADS%d not specified\n",
386 ts->model);
387 return 0;
388 }
389 break;
390 }
391
392 /* different chips have different sensor groups */
393 switch (ts->model) {
394 case 7846:
395 ts->attr_group = &ads7846_attr_group;
396 break;
397 case 7845:
398 ts->attr_group = &ads7845_attr_group;
399 break;
400 case 7843:
401 ts->attr_group = &ads7843_attr_group;
402 break;
403 default:
404 dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);
405 return 0;
406 }
407
408 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
409 if (err)
410 return err;
411
412 hwmon = hwmon_device_register(&spi->dev);
413 if (IS_ERR(hwmon)) {
414 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
415 return PTR_ERR(hwmon);
416 }
417
418 ts->hwmon = hwmon;
419 return 0;
420}
421
422static void ads784x_hwmon_unregister(struct spi_device *spi,
423 struct ads7846 *ts)
424{
425 if (ts->hwmon) {
426 sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
427 hwmon_device_unregister(ts->hwmon);
428 }
429}
430
431#else
432static inline int ads784x_hwmon_register(struct spi_device *spi,
433 struct ads7846 *ts)
434{
435 return 0;
436}
437
438static inline void ads784x_hwmon_unregister(struct spi_device *spi,
439 struct ads7846 *ts)
440{
441}
442#endif
274 443
275static int is_pen_down(struct device *dev) 444static int is_pen_down(struct device *dev)
276{ 445{
277 struct ads7846 *ts = dev_get_drvdata(dev); 446 struct ads7846 *ts = dev_get_drvdata(dev);
278 447
279 return ts->pendown; 448 return ts->pendown;
280} 449}
@@ -318,46 +487,14 @@ static ssize_t ads7846_disable_store(struct device *dev,
318 487
319static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store); 488static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
320 489
321static struct attribute *ads7846_attributes[] = { 490static struct attribute *ads784x_attributes[] = {
322 &dev_attr_temp0.attr,
323 &dev_attr_temp1.attr,
324 &dev_attr_vbatt.attr,
325 &dev_attr_vaux.attr,
326 &dev_attr_pen_down.attr,
327 &dev_attr_disable.attr,
328 NULL,
329};
330
331static struct attribute_group ads7846_attr_group = {
332 .attrs = ads7846_attributes,
333};
334
335/*
336 * ads7843/7845 don't have temperature sensors, and
337 * use the other sensors a bit differently too
338 */
339
340static struct attribute *ads7843_attributes[] = {
341 &dev_attr_vbatt.attr,
342 &dev_attr_vaux.attr,
343 &dev_attr_pen_down.attr, 491 &dev_attr_pen_down.attr,
344 &dev_attr_disable.attr, 492 &dev_attr_disable.attr,
345 NULL, 493 NULL,
346}; 494};
347 495
348static struct attribute_group ads7843_attr_group = { 496static struct attribute_group ads784x_attr_group = {
349 .attrs = ads7843_attributes, 497 .attrs = ads784x_attributes,
350};
351
352static struct attribute *ads7845_attributes[] = {
353 &dev_attr_vaux.attr,
354 &dev_attr_pen_down.attr,
355 &dev_attr_disable.attr,
356 NULL,
357};
358
359static struct attribute_group ads7845_attr_group = {
360 .attrs = ads7845_attributes,
361}; 498};
362 499
363/*--------------------------------------------------------------------------*/ 500/*--------------------------------------------------------------------------*/
@@ -373,25 +510,22 @@ static struct attribute_group ads7845_attr_group = {
373static void ads7846_rx(void *ads) 510static void ads7846_rx(void *ads)
374{ 511{
375 struct ads7846 *ts = ads; 512 struct ads7846 *ts = ads;
376 struct input_dev *input_dev = ts->input;
377 unsigned Rt; 513 unsigned Rt;
378 unsigned sync = 0;
379 u16 x, y, z1, z2; 514 u16 x, y, z1, z2;
380 unsigned long flags;
381 515
382 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding; 516 /* ads7846_rx_val() did in-place conversion (including byteswap) from
383 * built from two 8 bit values written msb-first. 517 * on-the-wire format as part of debouncing to get stable readings.
384 */ 518 */
385 x = (be16_to_cpu(ts->tc.x) >> 3) & 0x0fff; 519 x = ts->tc.x;
386 y = (be16_to_cpu(ts->tc.y) >> 3) & 0x0fff; 520 y = ts->tc.y;
387 z1 = (be16_to_cpu(ts->tc.z1) >> 3) & 0x0fff; 521 z1 = ts->tc.z1;
388 z2 = (be16_to_cpu(ts->tc.z2) >> 3) & 0x0fff; 522 z2 = ts->tc.z2;
389 523
390 /* range filtering */ 524 /* range filtering */
391 if (x == MAX_12BIT) 525 if (x == MAX_12BIT)
392 x = 0; 526 x = 0;
393 527
394 if (likely(x && z1 && !device_suspended(&ts->spi->dev))) { 528 if (likely(x && z1)) {
395 /* compute touch pressure resistance using equation #2 */ 529 /* compute touch pressure resistance using equation #2 */
396 Rt = z2; 530 Rt = z2;
397 Rt -= z1; 531 Rt -= z1;
@@ -403,100 +537,129 @@ static void ads7846_rx(void *ads)
403 Rt = 0; 537 Rt = 0;
404 538
405 /* Sample found inconsistent by debouncing or pressure is beyond 539 /* Sample found inconsistent by debouncing or pressure is beyond
406 * the maximum. Don't report it to user space, repeat at least 540 * the maximum. Don't report it to user space, repeat at least
407 * once more the measurement */ 541 * once more the measurement
542 */
408 if (ts->tc.ignore || Rt > ts->pressure_max) { 543 if (ts->tc.ignore || Rt > ts->pressure_max) {
409 mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD); 544#ifdef VERBOSE
545 pr_debug("%s: ignored %d pressure %d\n",
546 ts->spi->dev.bus_id, ts->tc.ignore, Rt);
547#endif
548 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD),
549 HRTIMER_REL);
410 return; 550 return;
411 } 551 }
412 552
413 /* NOTE: "pendown" is inferred from pressure; we don't rely on 553 /* NOTE: We can't rely on the pressure to determine the pen down
414 * being able to check nPENIRQ status, or "friendly" trigger modes 554 * state, even this controller has a pressure sensor. The pressure
415 * (both-edges is much better than just-falling or low-level). 555 * value can fluctuate for quite a while after lifting the pen and
556 * in some cases may not even settle at the expected value.
416 * 557 *
417 * REVISIT: some boards may require reading nPENIRQ; it's 558 * The only safe way to check for the pen up condition is in the
418 * needed on 7843. and 7845 reads pressure differently... 559 * timer by reading the pen signal state (it's a GPIO _and_ IRQ).
419 *
420 * REVISIT: the touchscreen might not be connected; this code
421 * won't notice that, even if nPENIRQ never fires ...
422 */ 560 */
423 if (!ts->pendown && Rt != 0) {
424 input_report_key(input_dev, BTN_TOUCH, 1);
425 sync = 1;
426 } else if (ts->pendown && Rt == 0) {
427 input_report_key(input_dev, BTN_TOUCH, 0);
428 sync = 1;
429 }
430
431 if (Rt) { 561 if (Rt) {
432 input_report_abs(input_dev, ABS_X, x); 562 struct input_dev *input = ts->input;
433 input_report_abs(input_dev, ABS_Y, y);
434 sync = 1;
435 }
436
437 if (sync) {
438 input_report_abs(input_dev, ABS_PRESSURE, Rt);
439 input_sync(input_dev);
440 }
441 563
442#ifdef VERBOSE 564 if (!ts->pendown) {
443 if (Rt || ts->pendown) 565 input_report_key(input, BTN_TOUCH, 1);
444 pr_debug("%s: %d/%d/%d%s\n", ts->spi->dev.bus_id, 566 ts->pendown = 1;
445 x, y, Rt, Rt ? "" : " UP"); 567#ifdef VERBOSE
568 dev_dbg(&ts->spi->dev, "DOWN\n");
446#endif 569#endif
570 }
571 input_report_abs(input, ABS_X, x);
572 input_report_abs(input, ABS_Y, y);
573 input_report_abs(input, ABS_PRESSURE, Rt);
447 574
448 spin_lock_irqsave(&ts->lock, flags); 575 input_sync(input);
449 576#ifdef VERBOSE
450 ts->pendown = (Rt != 0); 577 dev_dbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);
451 mod_timer(&ts->timer, jiffies + TS_POLL_PERIOD); 578#endif
579 }
452 580
453 spin_unlock_irqrestore(&ts->lock, flags); 581 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_PERIOD), HRTIMER_REL);
454} 582}
455 583
456static void ads7846_debounce(void *ads) 584static int ads7846_debounce(void *ads, int data_idx, int *val)
457{ 585{
458 struct ads7846 *ts = ads; 586 struct ads7846 *ts = ads;
459 struct spi_message *m;
460 struct spi_transfer *t;
461 int val;
462 int status;
463 587
464 m = &ts->msg[ts->msg_idx]; 588 if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {
465 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list); 589 /* Start over collecting consistent readings. */
466 val = (be16_to_cpu(*(__be16 *)t->rx_buf) >> 3) & 0x0fff; 590 ts->read_rep = 0;
467 if (!ts->read_cnt || (abs(ts->last_read - val) > ts->debounce_tol)) {
468 /* Repeat it, if this was the first read or the read 591 /* Repeat it, if this was the first read or the read
469 * wasn't consistent enough. */ 592 * wasn't consistent enough. */
470 if (ts->read_cnt < ts->debounce_max) { 593 if (ts->read_cnt < ts->debounce_max) {
471 ts->last_read = val; 594 ts->last_read = *val;
472 ts->read_cnt++; 595 ts->read_cnt++;
596 return ADS7846_FILTER_REPEAT;
473 } else { 597 } else {
474 /* Maximum number of debouncing reached and still 598 /* Maximum number of debouncing reached and still
475 * not enough number of consistent readings. Abort 599 * not enough number of consistent readings. Abort
476 * the whole sample, repeat it in the next sampling 600 * the whole sample, repeat it in the next sampling
477 * period. 601 * period.
478 */ 602 */
479 ts->tc.ignore = 1;
480 ts->read_cnt = 0; 603 ts->read_cnt = 0;
481 /* Last message will contain ads7846_rx() as the 604 return ADS7846_FILTER_IGNORE;
482 * completion function.
483 */
484 m = ts->last_msg;
485 } 605 }
486 /* Start over collecting consistent readings. */
487 ts->read_rep = 0;
488 } else { 606 } else {
489 if (++ts->read_rep > ts->debounce_rep) { 607 if (++ts->read_rep > ts->debounce_rep) {
490 /* Got a good reading for this coordinate, 608 /* Got a good reading for this coordinate,
491 * go for the next one. */ 609 * go for the next one. */
492 ts->tc.ignore = 0;
493 ts->msg_idx++;
494 ts->read_cnt = 0; 610 ts->read_cnt = 0;
495 ts->read_rep = 0; 611 ts->read_rep = 0;
496 m++; 612 return ADS7846_FILTER_OK;
497 } else 613 } else {
498 /* Read more values that are consistent. */ 614 /* Read more values that are consistent. */
499 ts->read_cnt++; 615 ts->read_cnt++;
616 return ADS7846_FILTER_REPEAT;
617 }
618 }
619}
620
621static int ads7846_no_filter(void *ads, int data_idx, int *val)
622{
623 return ADS7846_FILTER_OK;
624}
625
626static void ads7846_rx_val(void *ads)
627{
628 struct ads7846 *ts = ads;
629 struct spi_message *m;
630 struct spi_transfer *t;
631 u16 *rx_val;
632 int val;
633 int action;
634 int status;
635
636 m = &ts->msg[ts->msg_idx];
637 t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
638 rx_val = t->rx_buf;
639
640 /* adjust: on-wire is a must-ignore bit, a BE12 value, then padding;
641 * built from two 8 bit values written msb-first.
642 */
643 val = be16_to_cpu(*rx_val) >> 3;
644
645 action = ts->filter(ts->filter_data, ts->msg_idx, &val);
646 switch (action) {
647 case ADS7846_FILTER_REPEAT:
648 break;
649 case ADS7846_FILTER_IGNORE:
650 ts->tc.ignore = 1;
651 /* Last message will contain ads7846_rx() as the
652 * completion function.
653 */
654 m = ts->last_msg;
655 break;
656 case ADS7846_FILTER_OK:
657 *rx_val = val;
658 ts->tc.ignore = 0;
659 m = &ts->msg[++ts->msg_idx];
660 break;
661 default:
662 BUG();
500 } 663 }
501 status = spi_async(ts->spi, m); 664 status = spi_async(ts->spi, m);
502 if (status) 665 if (status)
@@ -504,21 +667,34 @@ static void ads7846_debounce(void *ads)
504 status); 667 status);
505} 668}
506 669
507static void ads7846_timer(unsigned long handle) 670static int ads7846_timer(struct hrtimer *handle)
508{ 671{
509 struct ads7846 *ts = (void *)handle; 672 struct ads7846 *ts = container_of(handle, struct ads7846, timer);
510 int status = 0; 673 int status = 0;
511 674
512 spin_lock_irq(&ts->lock); 675 spin_lock_irq(&ts->lock);
513 676
514 if (unlikely(ts->msg_idx && !ts->pendown)) { 677 if (unlikely(!ts->get_pendown_state() ||
678 device_suspended(&ts->spi->dev))) {
679 if (ts->pendown) {
680 struct input_dev *input = ts->input;
681
682 input_report_key(input, BTN_TOUCH, 0);
683 input_report_abs(input, ABS_PRESSURE, 0);
684 input_sync(input);
685
686 ts->pendown = 0;
687#ifdef VERBOSE
688 dev_dbg(&ts->spi->dev, "UP\n");
689#endif
690 }
691
515 /* measurement cycle ended */ 692 /* measurement cycle ended */
516 if (!device_suspended(&ts->spi->dev)) { 693 if (!device_suspended(&ts->spi->dev)) {
517 ts->irq_disabled = 0; 694 ts->irq_disabled = 0;
518 enable_irq(ts->spi->irq); 695 enable_irq(ts->spi->irq);
519 } 696 }
520 ts->pending = 0; 697 ts->pending = 0;
521 ts->msg_idx = 0;
522 } else { 698 } else {
523 /* pen is still down, continue with the measurement */ 699 /* pen is still down, continue with the measurement */
524 ts->msg_idx = 0; 700 ts->msg_idx = 0;
@@ -528,6 +704,7 @@ static void ads7846_timer(unsigned long handle)
528 } 704 }
529 705
530 spin_unlock_irq(&ts->lock); 706 spin_unlock_irq(&ts->lock);
707 return HRTIMER_NORESTART;
531} 708}
532 709
533static irqreturn_t ads7846_irq(int irq, void *handle) 710static irqreturn_t ads7846_irq(int irq, void *handle)
@@ -546,7 +723,8 @@ static irqreturn_t ads7846_irq(int irq, void *handle)
546 ts->irq_disabled = 1; 723 ts->irq_disabled = 1;
547 disable_irq(ts->spi->irq); 724 disable_irq(ts->spi->irq);
548 ts->pending = 1; 725 ts->pending = 1;
549 mod_timer(&ts->timer, jiffies); 726 hrtimer_start(&ts->timer, ktime_set(0, TS_POLL_DELAY),
727 HRTIMER_REL);
550 } 728 }
551 } 729 }
552 spin_unlock_irqrestore(&ts->lock, flags); 730 spin_unlock_irqrestore(&ts->lock, flags);
@@ -632,6 +810,7 @@ static int __devinit ads7846_probe(struct spi_device *spi)
632 struct ads7846_platform_data *pdata = spi->dev.platform_data; 810 struct ads7846_platform_data *pdata = spi->dev.platform_data;
633 struct spi_message *m; 811 struct spi_message *m;
634 struct spi_transfer *x; 812 struct spi_transfer *x;
813 int vref;
635 int err; 814 int err;
636 815
637 if (!spi->irq) { 816 if (!spi->irq) {
@@ -665,6 +844,10 @@ static int __devinit ads7846_probe(struct spi_device *spi)
665 * may not. So we stick to very-portable 8 bit words, both RX and TX. 844 * may not. So we stick to very-portable 8 bit words, both RX and TX.
666 */ 845 */
667 spi->bits_per_word = 8; 846 spi->bits_per_word = 8;
847 spi->mode = SPI_MODE_1;
848 err = spi_setup(spi);
849 if (err < 0)
850 return err;
668 851
669 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL); 852 ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
670 input_dev = input_allocate_device(); 853 input_dev = input_allocate_device();
@@ -679,8 +862,7 @@ static int __devinit ads7846_probe(struct spi_device *spi)
679 ts->spi = spi; 862 ts->spi = spi;
680 ts->input = input_dev; 863 ts->input = input_dev;
681 864
682 init_timer(&ts->timer); 865 hrtimer_init(&ts->timer, CLOCK_MONOTONIC, HRTIMER_REL);
683 ts->timer.data = (unsigned long) ts;
684 ts->timer.function = ads7846_timer; 866 ts->timer.function = ads7846_timer;
685 867
686 spin_lock_init(&ts->lock); 868 spin_lock_init(&ts->lock);
@@ -689,14 +871,25 @@ static int __devinit ads7846_probe(struct spi_device *spi)
689 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100; 871 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
690 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400; 872 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
691 ts->pressure_max = pdata->pressure_max ? : ~0; 873 ts->pressure_max = pdata->pressure_max ? : ~0;
692 if (pdata->debounce_max) { 874
875 if (pdata->filter != NULL) {
876 if (pdata->filter_init != NULL) {
877 err = pdata->filter_init(pdata, &ts->filter_data);
878 if (err < 0)
879 goto err_free_mem;
880 }
881 ts->filter = pdata->filter;
882 ts->filter_cleanup = pdata->filter_cleanup;
883 } else if (pdata->debounce_max) {
693 ts->debounce_max = pdata->debounce_max; 884 ts->debounce_max = pdata->debounce_max;
885 if (ts->debounce_max < 2)
886 ts->debounce_max = 2;
694 ts->debounce_tol = pdata->debounce_tol; 887 ts->debounce_tol = pdata->debounce_tol;
695 ts->debounce_rep = pdata->debounce_rep; 888 ts->debounce_rep = pdata->debounce_rep;
696 if (ts->debounce_rep > ts->debounce_max + 1) 889 ts->filter = ads7846_debounce;
697 ts->debounce_rep = ts->debounce_max - 1; 890 ts->filter_data = ts;
698 } else 891 } else
699 ts->debounce_tol = ~0; 892 ts->filter = ads7846_no_filter;
700 ts->get_pendown_state = pdata->get_pendown_state; 893 ts->get_pendown_state = pdata->get_pendown_state;
701 894
702 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id); 895 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
@@ -718,6 +911,8 @@ static int __devinit ads7846_probe(struct spi_device *spi)
718 input_set_abs_params(input_dev, ABS_PRESSURE, 911 input_set_abs_params(input_dev, ABS_PRESSURE,
719 pdata->pressure_min, pdata->pressure_max, 0, 0); 912 pdata->pressure_min, pdata->pressure_max, 0, 0);
720 913
914 vref = pdata->keep_vref_on;
915
721 /* set up the transfers to read touchscreen state; this assumes we 916 /* set up the transfers to read touchscreen state; this assumes we
722 * use formula #2 for pressure, not #3. 917 * use formula #2 for pressure, not #3.
723 */ 918 */
@@ -727,7 +922,7 @@ static int __devinit ads7846_probe(struct spi_device *spi)
727 spi_message_init(m); 922 spi_message_init(m);
728 923
729 /* y- still on; turn on only y+ (and ADC) */ 924 /* y- still on; turn on only y+ (and ADC) */
730 ts->read_y = READ_Y; 925 ts->read_y = READ_Y(vref);
731 x->tx_buf = &ts->read_y; 926 x->tx_buf = &ts->read_y;
732 x->len = 1; 927 x->len = 1;
733 spi_message_add_tail(x, m); 928 spi_message_add_tail(x, m);
@@ -737,7 +932,7 @@ static int __devinit ads7846_probe(struct spi_device *spi)
737 x->len = 2; 932 x->len = 2;
738 spi_message_add_tail(x, m); 933 spi_message_add_tail(x, m);
739 934
740 m->complete = ads7846_debounce; 935 m->complete = ads7846_rx_val;
741 m->context = ts; 936 m->context = ts;
742 937
743 m++; 938 m++;
@@ -745,7 +940,7 @@ static int __devinit ads7846_probe(struct spi_device *spi)
745 940
746 /* turn y- off, x+ on, then leave in lowpower */ 941 /* turn y- off, x+ on, then leave in lowpower */
747 x++; 942 x++;
748 ts->read_x = READ_X; 943 ts->read_x = READ_X(vref);
749 x->tx_buf = &ts->read_x; 944 x->tx_buf = &ts->read_x;
750 x->len = 1; 945 x->len = 1;
751 spi_message_add_tail(x, m); 946 spi_message_add_tail(x, m);
@@ -755,7 +950,7 @@ static int __devinit ads7846_probe(struct spi_device *spi)
755 x->len = 2; 950 x->len = 2;
756 spi_message_add_tail(x, m); 951 spi_message_add_tail(x, m);
757 952
758 m->complete = ads7846_debounce; 953 m->complete = ads7846_rx_val;
759 m->context = ts; 954 m->context = ts;
760 955
761 /* turn y+ off, x- on; we'll use formula #2 */ 956 /* turn y+ off, x- on; we'll use formula #2 */
@@ -764,7 +959,7 @@ static int __devinit ads7846_probe(struct spi_device *spi)
764 spi_message_init(m); 959 spi_message_init(m);
765 960
766 x++; 961 x++;
767 ts->read_z1 = READ_Z1; 962 ts->read_z1 = READ_Z1(vref);
768 x->tx_buf = &ts->read_z1; 963 x->tx_buf = &ts->read_z1;
769 x->len = 1; 964 x->len = 1;
770 spi_message_add_tail(x, m); 965 spi_message_add_tail(x, m);
@@ -774,14 +969,14 @@ static int __devinit ads7846_probe(struct spi_device *spi)
774 x->len = 2; 969 x->len = 2;
775 spi_message_add_tail(x, m); 970 spi_message_add_tail(x, m);
776 971
777 m->complete = ads7846_debounce; 972 m->complete = ads7846_rx_val;
778 m->context = ts; 973 m->context = ts;
779 974
780 m++; 975 m++;
781 spi_message_init(m); 976 spi_message_init(m);
782 977
783 x++; 978 x++;
784 ts->read_z2 = READ_Z2; 979 ts->read_z2 = READ_Z2(vref);
785 x->tx_buf = &ts->read_z2; 980 x->tx_buf = &ts->read_z2;
786 x->len = 1; 981 x->len = 1;
787 spi_message_add_tail(x, m); 982 spi_message_add_tail(x, m);
@@ -791,7 +986,7 @@ static int __devinit ads7846_probe(struct spi_device *spi)
791 x->len = 2; 986 x->len = 2;
792 spi_message_add_tail(x, m); 987 spi_message_add_tail(x, m);
793 988
794 m->complete = ads7846_debounce; 989 m->complete = ads7846_rx_val;
795 m->context = ts; 990 m->context = ts;
796 } 991 }
797 992
@@ -820,31 +1015,24 @@ static int __devinit ads7846_probe(struct spi_device *spi)
820 spi->dev.driver->name, ts)) { 1015 spi->dev.driver->name, ts)) {
821 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq); 1016 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
822 err = -EBUSY; 1017 err = -EBUSY;
823 goto err_free_mem; 1018 goto err_cleanup_filter;
824 } 1019 }
825 1020
1021 err = ads784x_hwmon_register(spi, ts);
1022 if (err)
1023 goto err_free_irq;
1024
826 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq); 1025 dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
827 1026
828 /* take a first sample, leaving nPENIRQ active; avoid 1027 /* take a first sample, leaving nPENIRQ active and vREF off; avoid
829 * the touchscreen, in case it's not connected. 1028 * the touchscreen, in case it's not connected.
830 */ 1029 */
831 (void) ads7846_read12_ser(&spi->dev, 1030 (void) ads7846_read12_ser(&spi->dev,
832 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON); 1031 READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
833 1032
834 switch (ts->model) { 1033 err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);
835 case 7846:
836 ts->attr_group = &ads7846_attr_group;
837 break;
838 case 7845:
839 ts->attr_group = &ads7845_attr_group;
840 break;
841 default:
842 ts->attr_group = &ads7843_attr_group;
843 break;
844 }
845 err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
846 if (err) 1034 if (err)
847 goto err_free_irq; 1035 goto err_remove_hwmon;
848 1036
849 err = input_register_device(input_dev); 1037 err = input_register_device(input_dev);
850 if (err) 1038 if (err)
@@ -853,9 +1041,14 @@ static int __devinit ads7846_probe(struct spi_device *spi)
853 return 0; 1041 return 0;
854 1042
855 err_remove_attr_group: 1043 err_remove_attr_group:
856 sysfs_remove_group(&spi->dev.kobj, ts->attr_group); 1044 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
1045 err_remove_hwmon:
1046 ads784x_hwmon_unregister(spi, ts);
857 err_free_irq: 1047 err_free_irq:
858 free_irq(spi->irq, ts); 1048 free_irq(spi->irq, ts);
1049 err_cleanup_filter:
1050 if (ts->filter_cleanup)
1051 ts->filter_cleanup(ts->filter_data);
859 err_free_mem: 1052 err_free_mem:
860 input_free_device(input_dev); 1053 input_free_device(input_dev);
861 kfree(ts); 1054 kfree(ts);
@@ -866,16 +1059,20 @@ static int __devexit ads7846_remove(struct spi_device *spi)
866{ 1059{
867 struct ads7846 *ts = dev_get_drvdata(&spi->dev); 1060 struct ads7846 *ts = dev_get_drvdata(&spi->dev);
868 1061
1062 ads784x_hwmon_unregister(spi, ts);
869 input_unregister_device(ts->input); 1063 input_unregister_device(ts->input);
870 1064
871 ads7846_suspend(spi, PMSG_SUSPEND); 1065 ads7846_suspend(spi, PMSG_SUSPEND);
872 1066
873 sysfs_remove_group(&spi->dev.kobj, ts->attr_group); 1067 sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);
874 1068
875 free_irq(ts->spi->irq, ts); 1069 free_irq(ts->spi->irq, ts);
876 /* suspend left the IRQ disabled */ 1070 /* suspend left the IRQ disabled */
877 enable_irq(ts->spi->irq); 1071 enable_irq(ts->spi->irq);
878 1072
1073 if (ts->filter_cleanup)
1074 ts->filter_cleanup(ts->filter_data);
1075
879 kfree(ts); 1076 kfree(ts);
880 1077
881 dev_dbg(&spi->dev, "unregistered touchscreen\n"); 1078 dev_dbg(&spi->dev, "unregistered touchscreen\n");