blob: 53f4d79ee5307583ba76ab7b4ea0718d01012951 [file] [log] [blame]
Michael Hennerich331b78e2009-03-09 20:12:52 -07001/*
2 * Copyright (C) 2006-2008 Michael Hennerich, Analog Devices Inc.
3 *
4 * Description: AD7877 based touchscreen, sensor (ADCs), DAC and GPIO driver
5 * Based on: ads7846.c
6 *
7 * Bugs: Enter bugs at http://blackfin.uclinux.org/
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see the file COPYING, or write
21 * to the Free Software Foundation, Inc.,
22 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 *
24 * History:
25 * Copyright (c) 2005 David Brownell
26 * Copyright (c) 2006 Nokia Corporation
27 * Various changes: Imre Deak <imre.deak@nokia.com>
28 *
29 * Using code from:
30 * - corgi_ts.c
31 * Copyright (C) 2004-2005 Richard Purdie
32 * - omap_ts.[hc], ads7846.h, ts_osk.c
33 * Copyright (C) 2002 MontaVista Software
34 * Copyright (C) 2004 Texas Instruments
35 * Copyright (C) 2005 Dirk Behme
36 */
37
38
39#include <linux/device.h>
40#include <linux/init.h>
41#include <linux/delay.h>
42#include <linux/input.h>
43#include <linux/interrupt.h>
44#include <linux/slab.h>
45#include <linux/spi/spi.h>
46#include <linux/spi/ad7877.h>
47#include <asm/irq.h>
48
Michael Hennerich4eb6f912010-03-09 20:38:47 -080049#define TS_PEN_UP_TIMEOUT msecs_to_jiffies(100)
Michael Hennerich331b78e2009-03-09 20:12:52 -070050
51#define MAX_SPI_FREQ_HZ 20000000
52#define MAX_12BIT ((1<<12)-1)
53
54#define AD7877_REG_ZEROS 0
55#define AD7877_REG_CTRL1 1
56#define AD7877_REG_CTRL2 2
57#define AD7877_REG_ALERT 3
58#define AD7877_REG_AUX1HIGH 4
59#define AD7877_REG_AUX1LOW 5
60#define AD7877_REG_BAT1HIGH 6
61#define AD7877_REG_BAT1LOW 7
62#define AD7877_REG_BAT2HIGH 8
63#define AD7877_REG_BAT2LOW 9
64#define AD7877_REG_TEMP1HIGH 10
65#define AD7877_REG_TEMP1LOW 11
66#define AD7877_REG_SEQ0 12
67#define AD7877_REG_SEQ1 13
68#define AD7877_REG_DAC 14
69#define AD7877_REG_NONE1 15
70#define AD7877_REG_EXTWRITE 15
71#define AD7877_REG_XPLUS 16
72#define AD7877_REG_YPLUS 17
73#define AD7877_REG_Z2 18
74#define AD7877_REG_aux1 19
75#define AD7877_REG_aux2 20
76#define AD7877_REG_aux3 21
77#define AD7877_REG_bat1 22
78#define AD7877_REG_bat2 23
79#define AD7877_REG_temp1 24
80#define AD7877_REG_temp2 25
81#define AD7877_REG_Z1 26
82#define AD7877_REG_GPIOCTRL1 27
83#define AD7877_REG_GPIOCTRL2 28
84#define AD7877_REG_GPIODATA 29
85#define AD7877_REG_NONE2 30
86#define AD7877_REG_NONE3 31
87
88#define AD7877_SEQ_YPLUS_BIT (1<<11)
89#define AD7877_SEQ_XPLUS_BIT (1<<10)
90#define AD7877_SEQ_Z2_BIT (1<<9)
91#define AD7877_SEQ_AUX1_BIT (1<<8)
92#define AD7877_SEQ_AUX2_BIT (1<<7)
93#define AD7877_SEQ_AUX3_BIT (1<<6)
94#define AD7877_SEQ_BAT1_BIT (1<<5)
95#define AD7877_SEQ_BAT2_BIT (1<<4)
96#define AD7877_SEQ_TEMP1_BIT (1<<3)
97#define AD7877_SEQ_TEMP2_BIT (1<<2)
98#define AD7877_SEQ_Z1_BIT (1<<1)
99
100enum {
101 AD7877_SEQ_YPOS = 0,
102 AD7877_SEQ_XPOS = 1,
103 AD7877_SEQ_Z2 = 2,
104 AD7877_SEQ_AUX1 = 3,
105 AD7877_SEQ_AUX2 = 4,
106 AD7877_SEQ_AUX3 = 5,
107 AD7877_SEQ_BAT1 = 6,
108 AD7877_SEQ_BAT2 = 7,
109 AD7877_SEQ_TEMP1 = 8,
110 AD7877_SEQ_TEMP2 = 9,
111 AD7877_SEQ_Z1 = 10,
112 AD7877_NR_SENSE = 11,
113};
114
115/* DAC Register Default RANGE 0 to Vcc, Volatge Mode, DAC On */
116#define AD7877_DAC_CONF 0x1
117
118/* If gpio3 is set AUX3/GPIO3 acts as GPIO Output */
119#define AD7877_EXTW_GPIO_3_CONF 0x1C4
120#define AD7877_EXTW_GPIO_DATA 0x200
121
122/* Control REG 2 */
123#define AD7877_TMR(x) ((x & 0x3) << 0)
124#define AD7877_REF(x) ((x & 0x1) << 2)
125#define AD7877_POL(x) ((x & 0x1) << 3)
126#define AD7877_FCD(x) ((x & 0x3) << 4)
127#define AD7877_PM(x) ((x & 0x3) << 6)
128#define AD7877_ACQ(x) ((x & 0x3) << 8)
129#define AD7877_AVG(x) ((x & 0x3) << 10)
130
131/* Control REG 1 */
132#define AD7877_SER (1 << 11) /* non-differential */
133#define AD7877_DFR (0 << 11) /* differential */
134
135#define AD7877_MODE_NOC (0) /* Do not convert */
136#define AD7877_MODE_SCC (1) /* Single channel conversion */
137#define AD7877_MODE_SEQ0 (2) /* Sequence 0 in Slave Mode */
138#define AD7877_MODE_SEQ1 (3) /* Sequence 1 in Master Mode */
139
140#define AD7877_CHANADD(x) ((x&0xF)<<7)
141#define AD7877_READADD(x) ((x)<<2)
142#define AD7877_WRITEADD(x) ((x)<<12)
143
144#define AD7877_READ_CHAN(x) (AD7877_WRITEADD(AD7877_REG_CTRL1) | AD7877_SER | \
145 AD7877_MODE_SCC | AD7877_CHANADD(AD7877_REG_ ## x) | \
146 AD7877_READADD(AD7877_REG_ ## x))
147
148#define AD7877_MM_SEQUENCE (AD7877_SEQ_YPLUS_BIT | AD7877_SEQ_XPLUS_BIT | \
149 AD7877_SEQ_Z2_BIT | AD7877_SEQ_Z1_BIT)
150
151/*
152 * Non-touchscreen sensors only use single-ended conversions.
153 */
154
155struct ser_req {
156 u16 reset;
157 u16 ref_on;
158 u16 command;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700159 struct spi_message msg;
160 struct spi_transfer xfer[6];
Oskar Schirmer38433842010-05-13 00:42:23 -0700161
162 /*
163 * DMA (thus cache coherency maintenance) requires the
164 * transfer buffers to live in their own cache lines.
165 */
166 u16 sample ____cacheline_aligned;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700167};
168
169struct ad7877 {
170 struct input_dev *input;
171 char phys[32];
172
173 struct spi_device *spi;
174 u16 model;
175 u16 vref_delay_usecs;
176 u16 x_plate_ohms;
177 u16 pressure_max;
178
179 u16 cmd_crtl1;
180 u16 cmd_crtl2;
181 u16 cmd_dummy;
182 u16 dac;
183
184 u8 stopacq_polarity;
185 u8 first_conversion_delay;
186 u8 acquisition_time;
187 u8 averaging;
188 u8 pen_down_acc_interval;
189
Michael Hennerich331b78e2009-03-09 20:12:52 -0700190 struct spi_transfer xfer[AD7877_NR_SENSE + 2];
191 struct spi_message msg;
192
193 struct mutex mutex;
194 unsigned disabled:1; /* P: mutex */
195 unsigned gpio3:1; /* P: mutex */
196 unsigned gpio4:1; /* P: mutex */
197
198 spinlock_t lock;
199 struct timer_list timer; /* P: lock */
200 unsigned pending:1; /* P: lock */
Oskar Schirmer38433842010-05-13 00:42:23 -0700201
202 /*
203 * DMA (thus cache coherency maintenance) requires the
204 * transfer buffers to live in their own cache lines.
205 */
206 u16 conversion_data[AD7877_NR_SENSE] ____cacheline_aligned;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700207};
208
209static int gpio3;
210module_param(gpio3, int, 0);
211MODULE_PARM_DESC(gpio3, "If gpio3 is set to 1 AUX3 acts as GPIO3");
212
213/*
214 * ad7877_read/write are only used for initial setup and for sysfs controls.
215 * The main traffic is done using spi_async() in the interrupt handler.
216 */
217
218static int ad7877_read(struct spi_device *spi, u16 reg)
219{
220 struct ser_req *req;
221 int status, ret;
222
223 req = kzalloc(sizeof *req, GFP_KERNEL);
224 if (!req)
225 return -ENOMEM;
226
227 spi_message_init(&req->msg);
228
229 req->command = (u16) (AD7877_WRITEADD(AD7877_REG_CTRL1) |
230 AD7877_READADD(reg));
231 req->xfer[0].tx_buf = &req->command;
232 req->xfer[0].len = 2;
Michael Henneriche92c27f2010-10-15 09:48:10 -0700233 req->xfer[0].cs_change = 1;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700234
235 req->xfer[1].rx_buf = &req->sample;
236 req->xfer[1].len = 2;
237
238 spi_message_add_tail(&req->xfer[0], &req->msg);
239 spi_message_add_tail(&req->xfer[1], &req->msg);
240
241 status = spi_sync(spi, &req->msg);
242 ret = status ? : req->sample;
243
244 kfree(req);
245
246 return ret;
247}
248
249static int ad7877_write(struct spi_device *spi, u16 reg, u16 val)
250{
251 struct ser_req *req;
252 int status;
253
254 req = kzalloc(sizeof *req, GFP_KERNEL);
255 if (!req)
256 return -ENOMEM;
257
258 spi_message_init(&req->msg);
259
260 req->command = (u16) (AD7877_WRITEADD(reg) | (val & MAX_12BIT));
261 req->xfer[0].tx_buf = &req->command;
262 req->xfer[0].len = 2;
263
264 spi_message_add_tail(&req->xfer[0], &req->msg);
265
266 status = spi_sync(spi, &req->msg);
267
268 kfree(req);
269
270 return status;
271}
272
273static int ad7877_read_adc(struct spi_device *spi, unsigned command)
274{
275 struct ad7877 *ts = dev_get_drvdata(&spi->dev);
276 struct ser_req *req;
277 int status;
278 int sample;
279 int i;
280
281 req = kzalloc(sizeof *req, GFP_KERNEL);
282 if (!req)
283 return -ENOMEM;
284
285 spi_message_init(&req->msg);
286
287 /* activate reference, so it has time to settle; */
288 req->ref_on = AD7877_WRITEADD(AD7877_REG_CTRL2) |
289 AD7877_POL(ts->stopacq_polarity) |
290 AD7877_AVG(0) | AD7877_PM(2) | AD7877_TMR(0) |
291 AD7877_ACQ(ts->acquisition_time) | AD7877_FCD(0);
292
293 req->reset = AD7877_WRITEADD(AD7877_REG_CTRL1) | AD7877_MODE_NOC;
294
295 req->command = (u16) command;
296
297 req->xfer[0].tx_buf = &req->reset;
298 req->xfer[0].len = 2;
Michael Henneriche92c27f2010-10-15 09:48:10 -0700299 req->xfer[0].cs_change = 1;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700300
301 req->xfer[1].tx_buf = &req->ref_on;
302 req->xfer[1].len = 2;
303 req->xfer[1].delay_usecs = ts->vref_delay_usecs;
Michael Henneriche92c27f2010-10-15 09:48:10 -0700304 req->xfer[1].cs_change = 1;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700305
306 req->xfer[2].tx_buf = &req->command;
307 req->xfer[2].len = 2;
308 req->xfer[2].delay_usecs = ts->vref_delay_usecs;
Michael Henneriche92c27f2010-10-15 09:48:10 -0700309 req->xfer[2].cs_change = 1;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700310
311 req->xfer[3].rx_buf = &req->sample;
312 req->xfer[3].len = 2;
Michael Henneriche92c27f2010-10-15 09:48:10 -0700313 req->xfer[3].cs_change = 1;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700314
315 req->xfer[4].tx_buf = &ts->cmd_crtl2; /*REF OFF*/
316 req->xfer[4].len = 2;
Michael Henneriche92c27f2010-10-15 09:48:10 -0700317 req->xfer[4].cs_change = 1;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700318
319 req->xfer[5].tx_buf = &ts->cmd_crtl1; /*DEFAULT*/
320 req->xfer[5].len = 2;
321
322 /* group all the transfers together, so we can't interfere with
323 * reading touchscreen state; disable penirq while sampling
324 */
325 for (i = 0; i < 6; i++)
326 spi_message_add_tail(&req->xfer[i], &req->msg);
327
328 status = spi_sync(spi, &req->msg);
329 sample = req->sample;
330
331 kfree(req);
332
333 return status ? : sample;
334}
335
336static void ad7877_rx(struct ad7877 *ts)
337{
338 struct input_dev *input_dev = ts->input;
339 unsigned Rt;
340 u16 x, y, z1, z2;
341
342 x = ts->conversion_data[AD7877_SEQ_XPOS] & MAX_12BIT;
343 y = ts->conversion_data[AD7877_SEQ_YPOS] & MAX_12BIT;
344 z1 = ts->conversion_data[AD7877_SEQ_Z1] & MAX_12BIT;
345 z2 = ts->conversion_data[AD7877_SEQ_Z2] & MAX_12BIT;
346
347 /*
348 * The samples processed here are already preprocessed by the AD7877.
349 * The preprocessing function consists of an averaging filter.
350 * The combination of 'first conversion delay' and averaging provides a robust solution,
351 * discarding the spurious noise in the signal and keeping only the data of interest.
352 * The size of the averaging filter is programmable. (dev.platform_data, see linux/spi/ad7877.h)
353 * Other user-programmable conversion controls include variable acquisition time,
354 * and first conversion delay. Up to 16 averages can be taken per conversion.
355 */
356
357 if (likely(x && z1)) {
358 /* compute touch pressure resistance using equation #1 */
359 Rt = (z2 - z1) * x * ts->x_plate_ohms;
360 Rt /= z1;
361 Rt = (Rt + 2047) >> 12;
362
363 input_report_abs(input_dev, ABS_X, x);
364 input_report_abs(input_dev, ABS_Y, y);
365 input_report_abs(input_dev, ABS_PRESSURE, Rt);
366 input_sync(input_dev);
367 }
368}
369
370static inline void ad7877_ts_event_release(struct ad7877 *ts)
371{
372 struct input_dev *input_dev = ts->input;
373
374 input_report_abs(input_dev, ABS_PRESSURE, 0);
375 input_sync(input_dev);
376}
377
378static void ad7877_timer(unsigned long handle)
379{
380 struct ad7877 *ts = (void *)handle;
381
382 ad7877_ts_event_release(ts);
383}
384
385static irqreturn_t ad7877_irq(int irq, void *handle)
386{
387 struct ad7877 *ts = handle;
388 unsigned long flags;
389 int status;
390
391 /*
392 * The repeated conversion sequencer controlled by TMR kicked off
393 * too fast. We ignore the last and process the sample sequence
394 * currently in the queue. It can't be older than 9.4ms, and we
395 * need to avoid that ts->msg doesn't get issued twice while in work.
396 */
397
398 spin_lock_irqsave(&ts->lock, flags);
399 if (!ts->pending) {
400 ts->pending = 1;
401
402 status = spi_async(ts->spi, &ts->msg);
403 if (status)
404 dev_err(&ts->spi->dev, "spi_sync --> %d\n", status);
405 }
406 spin_unlock_irqrestore(&ts->lock, flags);
407
408 return IRQ_HANDLED;
409}
410
411static void ad7877_callback(void *_ts)
412{
413 struct ad7877 *ts = _ts;
414
415 spin_lock_irq(&ts->lock);
416
417 ad7877_rx(ts);
418 ts->pending = 0;
419 mod_timer(&ts->timer, jiffies + TS_PEN_UP_TIMEOUT);
420
421 spin_unlock_irq(&ts->lock);
422}
423
424static void ad7877_disable(struct ad7877 *ts)
425{
426 mutex_lock(&ts->mutex);
427
428 if (!ts->disabled) {
429 ts->disabled = 1;
430 disable_irq(ts->spi->irq);
431
432 /* Wait for spi_async callback */
433 while (ts->pending)
434 msleep(1);
435
436 if (del_timer_sync(&ts->timer))
437 ad7877_ts_event_release(ts);
438 }
439
440 /* we know the chip's in lowpower mode since we always
441 * leave it that way after every request
442 */
443
444 mutex_unlock(&ts->mutex);
445}
446
447static void ad7877_enable(struct ad7877 *ts)
448{
449 mutex_lock(&ts->mutex);
450
451 if (ts->disabled) {
452 ts->disabled = 0;
453 enable_irq(ts->spi->irq);
454 }
455
456 mutex_unlock(&ts->mutex);
457}
458
459#define SHOW(name) static ssize_t \
460name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
461{ \
462 struct ad7877 *ts = dev_get_drvdata(dev); \
463 ssize_t v = ad7877_read_adc(ts->spi, \
464 AD7877_READ_CHAN(name)); \
465 if (v < 0) \
466 return v; \
467 return sprintf(buf, "%u\n", (unsigned) v); \
468} \
469static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
470
471SHOW(aux1)
472SHOW(aux2)
473SHOW(aux3)
474SHOW(bat1)
475SHOW(bat2)
476SHOW(temp1)
477SHOW(temp2)
478
479static ssize_t ad7877_disable_show(struct device *dev,
480 struct device_attribute *attr, char *buf)
481{
482 struct ad7877 *ts = dev_get_drvdata(dev);
483
484 return sprintf(buf, "%u\n", ts->disabled);
485}
486
487static ssize_t ad7877_disable_store(struct device *dev,
488 struct device_attribute *attr,
489 const char *buf, size_t count)
490{
491 struct ad7877 *ts = dev_get_drvdata(dev);
492 unsigned long val;
493 int error;
494
495 error = strict_strtoul(buf, 10, &val);
496 if (error)
497 return error;
498
499 if (val)
500 ad7877_disable(ts);
501 else
502 ad7877_enable(ts);
503
504 return count;
505}
506
507static DEVICE_ATTR(disable, 0664, ad7877_disable_show, ad7877_disable_store);
508
509static ssize_t ad7877_dac_show(struct device *dev,
510 struct device_attribute *attr, char *buf)
511{
512 struct ad7877 *ts = dev_get_drvdata(dev);
513
514 return sprintf(buf, "%u\n", ts->dac);
515}
516
517static ssize_t ad7877_dac_store(struct device *dev,
518 struct device_attribute *attr,
519 const char *buf, size_t count)
520{
521 struct ad7877 *ts = dev_get_drvdata(dev);
522 unsigned long val;
523 int error;
524
525 error = strict_strtoul(buf, 10, &val);
526 if (error)
527 return error;
528
529 mutex_lock(&ts->mutex);
530 ts->dac = val & 0xFF;
531 ad7877_write(ts->spi, AD7877_REG_DAC, (ts->dac << 4) | AD7877_DAC_CONF);
532 mutex_unlock(&ts->mutex);
533
534 return count;
535}
536
537static DEVICE_ATTR(dac, 0664, ad7877_dac_show, ad7877_dac_store);
538
539static ssize_t ad7877_gpio3_show(struct device *dev,
540 struct device_attribute *attr, char *buf)
541{
542 struct ad7877 *ts = dev_get_drvdata(dev);
543
544 return sprintf(buf, "%u\n", ts->gpio3);
545}
546
547static ssize_t ad7877_gpio3_store(struct device *dev,
548 struct device_attribute *attr,
549 const char *buf, size_t count)
550{
551 struct ad7877 *ts = dev_get_drvdata(dev);
552 unsigned long val;
553 int error;
554
555 error = strict_strtoul(buf, 10, &val);
556 if (error)
557 return error;
558
559 mutex_lock(&ts->mutex);
560 ts->gpio3 = !!val;
561 ad7877_write(ts->spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_DATA |
562 (ts->gpio4 << 4) | (ts->gpio3 << 5));
563 mutex_unlock(&ts->mutex);
564
565 return count;
566}
567
568static DEVICE_ATTR(gpio3, 0664, ad7877_gpio3_show, ad7877_gpio3_store);
569
570static ssize_t ad7877_gpio4_show(struct device *dev,
571 struct device_attribute *attr, char *buf)
572{
573 struct ad7877 *ts = dev_get_drvdata(dev);
574
575 return sprintf(buf, "%u\n", ts->gpio4);
576}
577
578static ssize_t ad7877_gpio4_store(struct device *dev,
579 struct device_attribute *attr,
580 const char *buf, size_t count)
581{
582 struct ad7877 *ts = dev_get_drvdata(dev);
583 unsigned long val;
584 int error;
585
586 error = strict_strtoul(buf, 10, &val);
587 if (error)
588 return error;
589
590 mutex_lock(&ts->mutex);
591 ts->gpio4 = !!val;
592 ad7877_write(ts->spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_DATA |
593 (ts->gpio4 << 4) | (ts->gpio3 << 5));
594 mutex_unlock(&ts->mutex);
595
596 return count;
597}
598
599static DEVICE_ATTR(gpio4, 0664, ad7877_gpio4_show, ad7877_gpio4_store);
600
601static struct attribute *ad7877_attributes[] = {
602 &dev_attr_temp1.attr,
603 &dev_attr_temp2.attr,
604 &dev_attr_aux1.attr,
605 &dev_attr_aux2.attr,
606 &dev_attr_bat1.attr,
607 &dev_attr_bat2.attr,
608 &dev_attr_disable.attr,
609 &dev_attr_dac.attr,
610 &dev_attr_gpio4.attr,
611 NULL
612};
613
614static const struct attribute_group ad7877_attr_group = {
615 .attrs = ad7877_attributes,
616};
617
618static void ad7877_setup_ts_def_msg(struct spi_device *spi, struct ad7877 *ts)
619{
620 struct spi_message *m;
621 int i;
622
623 ts->cmd_crtl2 = AD7877_WRITEADD(AD7877_REG_CTRL2) |
624 AD7877_POL(ts->stopacq_polarity) |
625 AD7877_AVG(ts->averaging) | AD7877_PM(1) |
626 AD7877_TMR(ts->pen_down_acc_interval) |
627 AD7877_ACQ(ts->acquisition_time) |
628 AD7877_FCD(ts->first_conversion_delay);
629
630 ad7877_write(spi, AD7877_REG_CTRL2, ts->cmd_crtl2);
631
632 ts->cmd_crtl1 = AD7877_WRITEADD(AD7877_REG_CTRL1) |
633 AD7877_READADD(AD7877_REG_XPLUS-1) |
634 AD7877_MODE_SEQ1 | AD7877_DFR;
635
636 ad7877_write(spi, AD7877_REG_CTRL1, ts->cmd_crtl1);
637
638 ts->cmd_dummy = 0;
639
640 m = &ts->msg;
641
642 spi_message_init(m);
643
644 m->complete = ad7877_callback;
645 m->context = ts;
646
647 ts->xfer[0].tx_buf = &ts->cmd_crtl1;
648 ts->xfer[0].len = 2;
Michael Henneriche92c27f2010-10-15 09:48:10 -0700649 ts->xfer[0].cs_change = 1;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700650
651 spi_message_add_tail(&ts->xfer[0], m);
652
653 ts->xfer[1].tx_buf = &ts->cmd_dummy; /* Send ZERO */
654 ts->xfer[1].len = 2;
Michael Henneriche92c27f2010-10-15 09:48:10 -0700655 ts->xfer[1].cs_change = 1;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700656
657 spi_message_add_tail(&ts->xfer[1], m);
658
Michael Henneriche92c27f2010-10-15 09:48:10 -0700659 for (i = 0; i < AD7877_NR_SENSE; i++) {
Michael Hennerich331b78e2009-03-09 20:12:52 -0700660 ts->xfer[i + 2].rx_buf = &ts->conversion_data[AD7877_SEQ_YPOS + i];
661 ts->xfer[i + 2].len = 2;
Michael Henneriche92c27f2010-10-15 09:48:10 -0700662 if (i < (AD7877_NR_SENSE - 1))
663 ts->xfer[i + 2].cs_change = 1;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700664 spi_message_add_tail(&ts->xfer[i + 2], m);
665 }
666}
667
668static int __devinit ad7877_probe(struct spi_device *spi)
669{
670 struct ad7877 *ts;
671 struct input_dev *input_dev;
672 struct ad7877_platform_data *pdata = spi->dev.platform_data;
673 int err;
674 u16 verify;
675
676 if (!spi->irq) {
677 dev_dbg(&spi->dev, "no IRQ?\n");
678 return -ENODEV;
679 }
680
681 if (!pdata) {
682 dev_dbg(&spi->dev, "no platform data?\n");
683 return -ENODEV;
684 }
685
686 /* don't exceed max specified SPI CLK frequency */
687 if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
688 dev_dbg(&spi->dev, "SPI CLK %d Hz?\n",spi->max_speed_hz);
689 return -EINVAL;
690 }
691
Oskar Schirmercd9b6fd2010-06-08 01:12:22 -0700692 spi->bits_per_word = 16;
693 err = spi_setup(spi);
694 if (err) {
695 dev_dbg(&spi->dev, "spi master doesn't support 16 bits/word\n");
696 return err;
697 }
698
Michael Hennerich331b78e2009-03-09 20:12:52 -0700699 ts = kzalloc(sizeof(struct ad7877), GFP_KERNEL);
700 input_dev = input_allocate_device();
701 if (!ts || !input_dev) {
702 err = -ENOMEM;
703 goto err_free_mem;
704 }
705
706 dev_set_drvdata(&spi->dev, ts);
707 ts->spi = spi;
708 ts->input = input_dev;
709
710 setup_timer(&ts->timer, ad7877_timer, (unsigned long) ts);
711 mutex_init(&ts->mutex);
712 spin_lock_init(&ts->lock);
713
714 ts->model = pdata->model ? : 7877;
715 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
716 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
717 ts->pressure_max = pdata->pressure_max ? : ~0;
718
719 ts->stopacq_polarity = pdata->stopacq_polarity;
720 ts->first_conversion_delay = pdata->first_conversion_delay;
721 ts->acquisition_time = pdata->acquisition_time;
722 ts->averaging = pdata->averaging;
723 ts->pen_down_acc_interval = pdata->pen_down_acc_interval;
724
725 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
726
727 input_dev->name = "AD7877 Touchscreen";
728 input_dev->phys = ts->phys;
729 input_dev->dev.parent = &spi->dev;
730
731 __set_bit(EV_ABS, input_dev->evbit);
732 __set_bit(ABS_X, input_dev->absbit);
733 __set_bit(ABS_Y, input_dev->absbit);
734 __set_bit(ABS_PRESSURE, input_dev->absbit);
735
736 input_set_abs_params(input_dev, ABS_X,
737 pdata->x_min ? : 0,
738 pdata->x_max ? : MAX_12BIT,
739 0, 0);
740 input_set_abs_params(input_dev, ABS_Y,
741 pdata->y_min ? : 0,
742 pdata->y_max ? : MAX_12BIT,
743 0, 0);
744 input_set_abs_params(input_dev, ABS_PRESSURE,
745 pdata->pressure_min, pdata->pressure_max, 0, 0);
746
747 ad7877_write(spi, AD7877_REG_SEQ1, AD7877_MM_SEQUENCE);
748
749 verify = ad7877_read(spi, AD7877_REG_SEQ1);
750
751 if (verify != AD7877_MM_SEQUENCE){
752 dev_err(&spi->dev, "%s: Failed to probe %s\n",
753 dev_name(&spi->dev), input_dev->name);
754 err = -ENODEV;
755 goto err_free_mem;
756 }
757
758 if (gpio3)
759 ad7877_write(spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_3_CONF);
760
761 ad7877_setup_ts_def_msg(spi, ts);
762
763 /* Request AD7877 /DAV GPIO interrupt */
764
Michael Hennerich0bc69ce2009-04-14 10:38:36 -0700765 err = request_irq(spi->irq, ad7877_irq, IRQF_TRIGGER_FALLING,
766 spi->dev.driver->name, ts);
Michael Hennerich331b78e2009-03-09 20:12:52 -0700767 if (err) {
768 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
769 goto err_free_mem;
770 }
771
772 err = sysfs_create_group(&spi->dev.kobj, &ad7877_attr_group);
773 if (err)
774 goto err_free_irq;
775
776 err = device_create_file(&spi->dev,
777 gpio3 ? &dev_attr_gpio3 : &dev_attr_aux3);
778 if (err)
779 goto err_remove_attr_group;
780
781 err = input_register_device(input_dev);
782 if (err)
783 goto err_remove_attr;
784
785 return 0;
786
787err_remove_attr:
788 device_remove_file(&spi->dev,
789 gpio3 ? &dev_attr_gpio3 : &dev_attr_aux3);
790err_remove_attr_group:
791 sysfs_remove_group(&spi->dev.kobj, &ad7877_attr_group);
792err_free_irq:
793 free_irq(spi->irq, ts);
794err_free_mem:
795 input_free_device(input_dev);
796 kfree(ts);
797 dev_set_drvdata(&spi->dev, NULL);
798 return err;
799}
800
801static int __devexit ad7877_remove(struct spi_device *spi)
802{
803 struct ad7877 *ts = dev_get_drvdata(&spi->dev);
804
805 sysfs_remove_group(&spi->dev.kobj, &ad7877_attr_group);
806 device_remove_file(&spi->dev,
807 gpio3 ? &dev_attr_gpio3 : &dev_attr_aux3);
808
809 ad7877_disable(ts);
810 free_irq(ts->spi->irq, ts);
811
812 input_unregister_device(ts->input);
813 kfree(ts);
814
815 dev_dbg(&spi->dev, "unregistered touchscreen\n");
816 dev_set_drvdata(&spi->dev, NULL);
817
818 return 0;
819}
820
821#ifdef CONFIG_PM
822static int ad7877_suspend(struct spi_device *spi, pm_message_t message)
823{
824 struct ad7877 *ts = dev_get_drvdata(&spi->dev);
825
826 ad7877_disable(ts);
827
828 return 0;
829}
830
831static int ad7877_resume(struct spi_device *spi)
832{
833 struct ad7877 *ts = dev_get_drvdata(&spi->dev);
834
835 ad7877_enable(ts);
836
837 return 0;
838}
839#else
840#define ad7877_suspend NULL
841#define ad7877_resume NULL
842#endif
843
844static struct spi_driver ad7877_driver = {
845 .driver = {
846 .name = "ad7877",
847 .bus = &spi_bus_type,
848 .owner = THIS_MODULE,
849 },
850 .probe = ad7877_probe,
851 .remove = __devexit_p(ad7877_remove),
852 .suspend = ad7877_suspend,
853 .resume = ad7877_resume,
854};
855
856static int __init ad7877_init(void)
857{
858 return spi_register_driver(&ad7877_driver);
859}
860module_init(ad7877_init);
861
862static void __exit ad7877_exit(void)
863{
864 spi_unregister_driver(&ad7877_driver);
865}
866module_exit(ad7877_exit);
867
868MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
869MODULE_DESCRIPTION("AD7877 touchscreen Driver");
870MODULE_LICENSE("GPL");
Anton Vorontsove0626e32009-09-22 16:46:08 -0700871MODULE_ALIAS("spi:ad7877");