blob: 374370ec73ac948e34cde9424a5bcc97ba853fd0 [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>
Mark Brownfccdd7c2011-01-20 22:47:47 -080044#include <linux/pm.h>
Michael Hennerich331b78e2009-03-09 20:12:52 -070045#include <linux/slab.h>
46#include <linux/spi/spi.h>
47#include <linux/spi/ad7877.h>
48#include <asm/irq.h>
49
Michael Hennerich4eb6f912010-03-09 20:38:47 -080050#define TS_PEN_UP_TIMEOUT msecs_to_jiffies(100)
Michael Hennerich331b78e2009-03-09 20:12:52 -070051
52#define MAX_SPI_FREQ_HZ 20000000
53#define MAX_12BIT ((1<<12)-1)
54
55#define AD7877_REG_ZEROS 0
56#define AD7877_REG_CTRL1 1
57#define AD7877_REG_CTRL2 2
58#define AD7877_REG_ALERT 3
59#define AD7877_REG_AUX1HIGH 4
60#define AD7877_REG_AUX1LOW 5
61#define AD7877_REG_BAT1HIGH 6
62#define AD7877_REG_BAT1LOW 7
63#define AD7877_REG_BAT2HIGH 8
64#define AD7877_REG_BAT2LOW 9
65#define AD7877_REG_TEMP1HIGH 10
66#define AD7877_REG_TEMP1LOW 11
67#define AD7877_REG_SEQ0 12
68#define AD7877_REG_SEQ1 13
69#define AD7877_REG_DAC 14
70#define AD7877_REG_NONE1 15
71#define AD7877_REG_EXTWRITE 15
72#define AD7877_REG_XPLUS 16
73#define AD7877_REG_YPLUS 17
74#define AD7877_REG_Z2 18
75#define AD7877_REG_aux1 19
76#define AD7877_REG_aux2 20
77#define AD7877_REG_aux3 21
78#define AD7877_REG_bat1 22
79#define AD7877_REG_bat2 23
80#define AD7877_REG_temp1 24
81#define AD7877_REG_temp2 25
82#define AD7877_REG_Z1 26
83#define AD7877_REG_GPIOCTRL1 27
84#define AD7877_REG_GPIOCTRL2 28
85#define AD7877_REG_GPIODATA 29
86#define AD7877_REG_NONE2 30
87#define AD7877_REG_NONE3 31
88
89#define AD7877_SEQ_YPLUS_BIT (1<<11)
90#define AD7877_SEQ_XPLUS_BIT (1<<10)
91#define AD7877_SEQ_Z2_BIT (1<<9)
92#define AD7877_SEQ_AUX1_BIT (1<<8)
93#define AD7877_SEQ_AUX2_BIT (1<<7)
94#define AD7877_SEQ_AUX3_BIT (1<<6)
95#define AD7877_SEQ_BAT1_BIT (1<<5)
96#define AD7877_SEQ_BAT2_BIT (1<<4)
97#define AD7877_SEQ_TEMP1_BIT (1<<3)
98#define AD7877_SEQ_TEMP2_BIT (1<<2)
99#define AD7877_SEQ_Z1_BIT (1<<1)
100
101enum {
102 AD7877_SEQ_YPOS = 0,
103 AD7877_SEQ_XPOS = 1,
104 AD7877_SEQ_Z2 = 2,
105 AD7877_SEQ_AUX1 = 3,
106 AD7877_SEQ_AUX2 = 4,
107 AD7877_SEQ_AUX3 = 5,
108 AD7877_SEQ_BAT1 = 6,
109 AD7877_SEQ_BAT2 = 7,
110 AD7877_SEQ_TEMP1 = 8,
111 AD7877_SEQ_TEMP2 = 9,
112 AD7877_SEQ_Z1 = 10,
113 AD7877_NR_SENSE = 11,
114};
115
116/* DAC Register Default RANGE 0 to Vcc, Volatge Mode, DAC On */
117#define AD7877_DAC_CONF 0x1
118
119/* If gpio3 is set AUX3/GPIO3 acts as GPIO Output */
120#define AD7877_EXTW_GPIO_3_CONF 0x1C4
121#define AD7877_EXTW_GPIO_DATA 0x200
122
123/* Control REG 2 */
124#define AD7877_TMR(x) ((x & 0x3) << 0)
125#define AD7877_REF(x) ((x & 0x1) << 2)
126#define AD7877_POL(x) ((x & 0x1) << 3)
127#define AD7877_FCD(x) ((x & 0x3) << 4)
128#define AD7877_PM(x) ((x & 0x3) << 6)
129#define AD7877_ACQ(x) ((x & 0x3) << 8)
130#define AD7877_AVG(x) ((x & 0x3) << 10)
131
132/* Control REG 1 */
133#define AD7877_SER (1 << 11) /* non-differential */
134#define AD7877_DFR (0 << 11) /* differential */
135
136#define AD7877_MODE_NOC (0) /* Do not convert */
137#define AD7877_MODE_SCC (1) /* Single channel conversion */
138#define AD7877_MODE_SEQ0 (2) /* Sequence 0 in Slave Mode */
139#define AD7877_MODE_SEQ1 (3) /* Sequence 1 in Master Mode */
140
141#define AD7877_CHANADD(x) ((x&0xF)<<7)
142#define AD7877_READADD(x) ((x)<<2)
143#define AD7877_WRITEADD(x) ((x)<<12)
144
145#define AD7877_READ_CHAN(x) (AD7877_WRITEADD(AD7877_REG_CTRL1) | AD7877_SER | \
146 AD7877_MODE_SCC | AD7877_CHANADD(AD7877_REG_ ## x) | \
147 AD7877_READADD(AD7877_REG_ ## x))
148
149#define AD7877_MM_SEQUENCE (AD7877_SEQ_YPLUS_BIT | AD7877_SEQ_XPLUS_BIT | \
150 AD7877_SEQ_Z2_BIT | AD7877_SEQ_Z1_BIT)
151
152/*
153 * Non-touchscreen sensors only use single-ended conversions.
154 */
155
156struct ser_req {
157 u16 reset;
158 u16 ref_on;
159 u16 command;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700160 struct spi_message msg;
161 struct spi_transfer xfer[6];
Oskar Schirmer38433842010-05-13 00:42:23 -0700162
163 /*
164 * DMA (thus cache coherency maintenance) requires the
165 * transfer buffers to live in their own cache lines.
166 */
167 u16 sample ____cacheline_aligned;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700168};
169
170struct ad7877 {
171 struct input_dev *input;
172 char phys[32];
173
174 struct spi_device *spi;
175 u16 model;
176 u16 vref_delay_usecs;
177 u16 x_plate_ohms;
178 u16 pressure_max;
179
180 u16 cmd_crtl1;
181 u16 cmd_crtl2;
182 u16 cmd_dummy;
183 u16 dac;
184
185 u8 stopacq_polarity;
186 u8 first_conversion_delay;
187 u8 acquisition_time;
188 u8 averaging;
189 u8 pen_down_acc_interval;
190
Michael Hennerich331b78e2009-03-09 20:12:52 -0700191 struct spi_transfer xfer[AD7877_NR_SENSE + 2];
192 struct spi_message msg;
193
194 struct mutex mutex;
Dmitry Torokhovb5344222010-10-18 09:24:22 -0700195 bool disabled; /* P: mutex */
196 bool gpio3; /* P: mutex */
197 bool gpio4; /* P: mutex */
Michael Hennerich331b78e2009-03-09 20:12:52 -0700198
199 spinlock_t lock;
200 struct timer_list timer; /* 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
Dmitry Torokhov2fd18ab2010-10-18 09:22:38 -0700209static bool gpio3;
210module_param(gpio3, bool, 0);
Michael Hennerich331b78e2009-03-09 20:12:52 -0700211MODULE_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
Dmitry Torokhovb5344222010-10-18 09:24:22 -0700336static int ad7877_process_data(struct ad7877 *ts)
Michael Hennerich331b78e2009-03-09 20:12:52 -0700337{
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
Michael Hennerich1d02ad42010-10-15 09:49:08 -0700363 /*
364 * Sample found inconsistent, pressure is beyond
365 * the maximum. Don't report it to user space.
366 */
367 if (Rt > ts->pressure_max)
368 return -EINVAL;
369
Michael Hennerich47026b22010-10-15 09:49:07 -0700370 if (!timer_pending(&ts->timer))
371 input_report_key(input_dev, BTN_TOUCH, 1);
372
Michael Hennerich331b78e2009-03-09 20:12:52 -0700373 input_report_abs(input_dev, ABS_X, x);
374 input_report_abs(input_dev, ABS_Y, y);
375 input_report_abs(input_dev, ABS_PRESSURE, Rt);
376 input_sync(input_dev);
Dmitry Torokhovb5344222010-10-18 09:24:22 -0700377
Michael Hennerich47026b22010-10-15 09:49:07 -0700378 return 0;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700379 }
Michael Hennerich47026b22010-10-15 09:49:07 -0700380
381 return -EINVAL;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700382}
383
384static inline void ad7877_ts_event_release(struct ad7877 *ts)
385{
386 struct input_dev *input_dev = ts->input;
387
388 input_report_abs(input_dev, ABS_PRESSURE, 0);
Michael Hennerich47026b22010-10-15 09:49:07 -0700389 input_report_key(input_dev, BTN_TOUCH, 0);
Michael Hennerich331b78e2009-03-09 20:12:52 -0700390 input_sync(input_dev);
391}
392
393static void ad7877_timer(unsigned long handle)
394{
395 struct ad7877 *ts = (void *)handle;
Dmitry Torokhovb5344222010-10-18 09:24:22 -0700396 unsigned long flags;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700397
Dmitry Torokhovb5344222010-10-18 09:24:22 -0700398 spin_lock_irqsave(&ts->lock, flags);
Michael Hennerich331b78e2009-03-09 20:12:52 -0700399 ad7877_ts_event_release(ts);
Dmitry Torokhovb5344222010-10-18 09:24:22 -0700400 spin_unlock_irqrestore(&ts->lock, flags);
Michael Hennerich331b78e2009-03-09 20:12:52 -0700401}
402
403static irqreturn_t ad7877_irq(int irq, void *handle)
404{
405 struct ad7877 *ts = handle;
406 unsigned long flags;
Dmitry Torokhovb5344222010-10-18 09:24:22 -0700407 int error;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700408
Dmitry Torokhovb5344222010-10-18 09:24:22 -0700409 error = spi_sync(ts->spi, &ts->msg);
410 if (error) {
411 dev_err(&ts->spi->dev, "spi_sync --> %d\n", error);
412 goto out;
413 }
Michael Hennerich331b78e2009-03-09 20:12:52 -0700414
415 spin_lock_irqsave(&ts->lock, flags);
Dmitry Torokhovb5344222010-10-18 09:24:22 -0700416 error = ad7877_process_data(ts);
417 if (!error)
418 mod_timer(&ts->timer, jiffies + TS_PEN_UP_TIMEOUT);
Michael Hennerich331b78e2009-03-09 20:12:52 -0700419 spin_unlock_irqrestore(&ts->lock, flags);
420
Dmitry Torokhovb5344222010-10-18 09:24:22 -0700421out:
Michael Hennerich331b78e2009-03-09 20:12:52 -0700422 return IRQ_HANDLED;
423}
424
Michael Hennerich331b78e2009-03-09 20:12:52 -0700425static void ad7877_disable(struct ad7877 *ts)
426{
427 mutex_lock(&ts->mutex);
428
429 if (!ts->disabled) {
Dmitry Torokhovb5344222010-10-18 09:24:22 -0700430 ts->disabled = true;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700431 disable_irq(ts->spi->irq);
432
Michael Hennerich331b78e2009-03-09 20:12:52 -0700433 if (del_timer_sync(&ts->timer))
434 ad7877_ts_event_release(ts);
435 }
436
Dmitry Torokhovb5344222010-10-18 09:24:22 -0700437 /*
438 * We know the chip's in lowpower mode since we always
Michael Hennerich331b78e2009-03-09 20:12:52 -0700439 * leave it that way after every request
440 */
441
442 mutex_unlock(&ts->mutex);
443}
444
445static void ad7877_enable(struct ad7877 *ts)
446{
447 mutex_lock(&ts->mutex);
448
449 if (ts->disabled) {
Dmitry Torokhovb5344222010-10-18 09:24:22 -0700450 ts->disabled = false;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700451 enable_irq(ts->spi->irq);
452 }
453
454 mutex_unlock(&ts->mutex);
455}
456
457#define SHOW(name) static ssize_t \
458name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
459{ \
Dmitry Torokhov2fd18ab2010-10-18 09:22:38 -0700460 struct ad7877 *ts = dev_get_drvdata(dev); \
Michael Hennerich331b78e2009-03-09 20:12:52 -0700461 ssize_t v = ad7877_read_adc(ts->spi, \
462 AD7877_READ_CHAN(name)); \
463 if (v < 0) \
464 return v; \
465 return sprintf(buf, "%u\n", (unsigned) v); \
466} \
467static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
468
469SHOW(aux1)
470SHOW(aux2)
471SHOW(aux3)
472SHOW(bat1)
473SHOW(bat2)
474SHOW(temp1)
475SHOW(temp2)
476
477static ssize_t ad7877_disable_show(struct device *dev,
478 struct device_attribute *attr, char *buf)
479{
Dmitry Torokhov2fd18ab2010-10-18 09:22:38 -0700480 struct ad7877 *ts = dev_get_drvdata(dev);
Michael Hennerich331b78e2009-03-09 20:12:52 -0700481
482 return sprintf(buf, "%u\n", ts->disabled);
483}
484
485static ssize_t ad7877_disable_store(struct device *dev,
486 struct device_attribute *attr,
487 const char *buf, size_t count)
488{
489 struct ad7877 *ts = dev_get_drvdata(dev);
JJ Ding76496e72011-11-09 10:20:14 -0800490 unsigned int val;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700491 int error;
492
JJ Ding76496e72011-11-09 10:20:14 -0800493 error = kstrtouint(buf, 10, &val);
Michael Hennerich331b78e2009-03-09 20:12:52 -0700494 if (error)
495 return error;
496
497 if (val)
498 ad7877_disable(ts);
499 else
500 ad7877_enable(ts);
501
502 return count;
503}
504
505static DEVICE_ATTR(disable, 0664, ad7877_disable_show, ad7877_disable_store);
506
507static ssize_t ad7877_dac_show(struct device *dev,
508 struct device_attribute *attr, char *buf)
509{
Dmitry Torokhov2fd18ab2010-10-18 09:22:38 -0700510 struct ad7877 *ts = dev_get_drvdata(dev);
Michael Hennerich331b78e2009-03-09 20:12:52 -0700511
512 return sprintf(buf, "%u\n", ts->dac);
513}
514
515static ssize_t ad7877_dac_store(struct device *dev,
516 struct device_attribute *attr,
517 const char *buf, size_t count)
518{
519 struct ad7877 *ts = dev_get_drvdata(dev);
JJ Ding76496e72011-11-09 10:20:14 -0800520 unsigned int val;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700521 int error;
522
JJ Ding76496e72011-11-09 10:20:14 -0800523 error = kstrtouint(buf, 10, &val);
Michael Hennerich331b78e2009-03-09 20:12:52 -0700524 if (error)
525 return error;
526
527 mutex_lock(&ts->mutex);
528 ts->dac = val & 0xFF;
529 ad7877_write(ts->spi, AD7877_REG_DAC, (ts->dac << 4) | AD7877_DAC_CONF);
530 mutex_unlock(&ts->mutex);
531
532 return count;
533}
534
535static DEVICE_ATTR(dac, 0664, ad7877_dac_show, ad7877_dac_store);
536
537static ssize_t ad7877_gpio3_show(struct device *dev,
538 struct device_attribute *attr, char *buf)
539{
Dmitry Torokhov2fd18ab2010-10-18 09:22:38 -0700540 struct ad7877 *ts = dev_get_drvdata(dev);
Michael Hennerich331b78e2009-03-09 20:12:52 -0700541
542 return sprintf(buf, "%u\n", ts->gpio3);
543}
544
545static ssize_t ad7877_gpio3_store(struct device *dev,
546 struct device_attribute *attr,
547 const char *buf, size_t count)
548{
549 struct ad7877 *ts = dev_get_drvdata(dev);
JJ Ding76496e72011-11-09 10:20:14 -0800550 unsigned int val;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700551 int error;
552
JJ Ding76496e72011-11-09 10:20:14 -0800553 error = kstrtouint(buf, 10, &val);
Michael Hennerich331b78e2009-03-09 20:12:52 -0700554 if (error)
555 return error;
556
557 mutex_lock(&ts->mutex);
558 ts->gpio3 = !!val;
559 ad7877_write(ts->spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_DATA |
560 (ts->gpio4 << 4) | (ts->gpio3 << 5));
561 mutex_unlock(&ts->mutex);
562
563 return count;
564}
565
566static DEVICE_ATTR(gpio3, 0664, ad7877_gpio3_show, ad7877_gpio3_store);
567
568static ssize_t ad7877_gpio4_show(struct device *dev,
569 struct device_attribute *attr, char *buf)
570{
Dmitry Torokhov2fd18ab2010-10-18 09:22:38 -0700571 struct ad7877 *ts = dev_get_drvdata(dev);
Michael Hennerich331b78e2009-03-09 20:12:52 -0700572
573 return sprintf(buf, "%u\n", ts->gpio4);
574}
575
576static ssize_t ad7877_gpio4_store(struct device *dev,
577 struct device_attribute *attr,
578 const char *buf, size_t count)
579{
580 struct ad7877 *ts = dev_get_drvdata(dev);
JJ Ding76496e72011-11-09 10:20:14 -0800581 unsigned int val;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700582 int error;
583
JJ Ding76496e72011-11-09 10:20:14 -0800584 error = kstrtouint(buf, 10, &val);
Michael Hennerich331b78e2009-03-09 20:12:52 -0700585 if (error)
586 return error;
587
588 mutex_lock(&ts->mutex);
589 ts->gpio4 = !!val;
590 ad7877_write(ts->spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_DATA |
591 (ts->gpio4 << 4) | (ts->gpio3 << 5));
592 mutex_unlock(&ts->mutex);
593
594 return count;
595}
596
597static DEVICE_ATTR(gpio4, 0664, ad7877_gpio4_show, ad7877_gpio4_store);
598
599static struct attribute *ad7877_attributes[] = {
600 &dev_attr_temp1.attr,
601 &dev_attr_temp2.attr,
602 &dev_attr_aux1.attr,
603 &dev_attr_aux2.attr,
Dmitry Torokhov2fd18ab2010-10-18 09:22:38 -0700604 &dev_attr_aux3.attr,
Michael Hennerich331b78e2009-03-09 20:12:52 -0700605 &dev_attr_bat1.attr,
606 &dev_attr_bat2.attr,
607 &dev_attr_disable.attr,
608 &dev_attr_dac.attr,
Dmitry Torokhov2fd18ab2010-10-18 09:22:38 -0700609 &dev_attr_gpio3.attr,
Michael Hennerich331b78e2009-03-09 20:12:52 -0700610 &dev_attr_gpio4.attr,
611 NULL
612};
613
Dmitry Torokhov2fd18ab2010-10-18 09:22:38 -0700614static mode_t ad7877_attr_is_visible(struct kobject *kobj,
615 struct attribute *attr, int n)
616{
617 mode_t mode = attr->mode;
618
619 if (attr == &dev_attr_aux3.attr) {
620 if (gpio3)
621 mode = 0;
622 } else if (attr == &dev_attr_gpio3.attr) {
623 if (!gpio3)
624 mode = 0;
625 }
626
627 return mode;
628}
629
Michael Hennerich331b78e2009-03-09 20:12:52 -0700630static const struct attribute_group ad7877_attr_group = {
Dmitry Torokhov2fd18ab2010-10-18 09:22:38 -0700631 .is_visible = ad7877_attr_is_visible,
632 .attrs = ad7877_attributes,
Michael Hennerich331b78e2009-03-09 20:12:52 -0700633};
634
635static void ad7877_setup_ts_def_msg(struct spi_device *spi, struct ad7877 *ts)
636{
637 struct spi_message *m;
638 int i;
639
640 ts->cmd_crtl2 = AD7877_WRITEADD(AD7877_REG_CTRL2) |
641 AD7877_POL(ts->stopacq_polarity) |
642 AD7877_AVG(ts->averaging) | AD7877_PM(1) |
643 AD7877_TMR(ts->pen_down_acc_interval) |
644 AD7877_ACQ(ts->acquisition_time) |
645 AD7877_FCD(ts->first_conversion_delay);
646
647 ad7877_write(spi, AD7877_REG_CTRL2, ts->cmd_crtl2);
648
649 ts->cmd_crtl1 = AD7877_WRITEADD(AD7877_REG_CTRL1) |
650 AD7877_READADD(AD7877_REG_XPLUS-1) |
651 AD7877_MODE_SEQ1 | AD7877_DFR;
652
653 ad7877_write(spi, AD7877_REG_CTRL1, ts->cmd_crtl1);
654
655 ts->cmd_dummy = 0;
656
657 m = &ts->msg;
658
659 spi_message_init(m);
660
Michael Hennerich331b78e2009-03-09 20:12:52 -0700661 m->context = ts;
662
663 ts->xfer[0].tx_buf = &ts->cmd_crtl1;
664 ts->xfer[0].len = 2;
Michael Henneriche92c27f2010-10-15 09:48:10 -0700665 ts->xfer[0].cs_change = 1;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700666
667 spi_message_add_tail(&ts->xfer[0], m);
668
669 ts->xfer[1].tx_buf = &ts->cmd_dummy; /* Send ZERO */
670 ts->xfer[1].len = 2;
Michael Henneriche92c27f2010-10-15 09:48:10 -0700671 ts->xfer[1].cs_change = 1;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700672
673 spi_message_add_tail(&ts->xfer[1], m);
674
Michael Henneriche92c27f2010-10-15 09:48:10 -0700675 for (i = 0; i < AD7877_NR_SENSE; i++) {
Michael Hennerich331b78e2009-03-09 20:12:52 -0700676 ts->xfer[i + 2].rx_buf = &ts->conversion_data[AD7877_SEQ_YPOS + i];
677 ts->xfer[i + 2].len = 2;
Michael Henneriche92c27f2010-10-15 09:48:10 -0700678 if (i < (AD7877_NR_SENSE - 1))
679 ts->xfer[i + 2].cs_change = 1;
Michael Hennerich331b78e2009-03-09 20:12:52 -0700680 spi_message_add_tail(&ts->xfer[i + 2], m);
681 }
682}
683
684static int __devinit ad7877_probe(struct spi_device *spi)
685{
686 struct ad7877 *ts;
687 struct input_dev *input_dev;
688 struct ad7877_platform_data *pdata = spi->dev.platform_data;
689 int err;
690 u16 verify;
691
692 if (!spi->irq) {
693 dev_dbg(&spi->dev, "no IRQ?\n");
694 return -ENODEV;
695 }
696
697 if (!pdata) {
698 dev_dbg(&spi->dev, "no platform data?\n");
699 return -ENODEV;
700 }
701
702 /* don't exceed max specified SPI CLK frequency */
703 if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
704 dev_dbg(&spi->dev, "SPI CLK %d Hz?\n",spi->max_speed_hz);
705 return -EINVAL;
706 }
707
Oskar Schirmercd9b6fd2010-06-08 01:12:22 -0700708 spi->bits_per_word = 16;
709 err = spi_setup(spi);
710 if (err) {
711 dev_dbg(&spi->dev, "spi master doesn't support 16 bits/word\n");
712 return err;
713 }
714
Michael Hennerich331b78e2009-03-09 20:12:52 -0700715 ts = kzalloc(sizeof(struct ad7877), GFP_KERNEL);
716 input_dev = input_allocate_device();
717 if (!ts || !input_dev) {
718 err = -ENOMEM;
719 goto err_free_mem;
720 }
721
722 dev_set_drvdata(&spi->dev, ts);
723 ts->spi = spi;
724 ts->input = input_dev;
725
726 setup_timer(&ts->timer, ad7877_timer, (unsigned long) ts);
727 mutex_init(&ts->mutex);
728 spin_lock_init(&ts->lock);
729
730 ts->model = pdata->model ? : 7877;
731 ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
732 ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
733 ts->pressure_max = pdata->pressure_max ? : ~0;
734
735 ts->stopacq_polarity = pdata->stopacq_polarity;
736 ts->first_conversion_delay = pdata->first_conversion_delay;
737 ts->acquisition_time = pdata->acquisition_time;
738 ts->averaging = pdata->averaging;
739 ts->pen_down_acc_interval = pdata->pen_down_acc_interval;
740
741 snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));
742
743 input_dev->name = "AD7877 Touchscreen";
744 input_dev->phys = ts->phys;
745 input_dev->dev.parent = &spi->dev;
746
Michael Hennerich47026b22010-10-15 09:49:07 -0700747 __set_bit(EV_KEY, input_dev->evbit);
748 __set_bit(BTN_TOUCH, input_dev->keybit);
Michael Hennerich331b78e2009-03-09 20:12:52 -0700749 __set_bit(EV_ABS, input_dev->evbit);
750 __set_bit(ABS_X, input_dev->absbit);
751 __set_bit(ABS_Y, input_dev->absbit);
752 __set_bit(ABS_PRESSURE, input_dev->absbit);
753
754 input_set_abs_params(input_dev, ABS_X,
755 pdata->x_min ? : 0,
756 pdata->x_max ? : MAX_12BIT,
757 0, 0);
758 input_set_abs_params(input_dev, ABS_Y,
759 pdata->y_min ? : 0,
760 pdata->y_max ? : MAX_12BIT,
761 0, 0);
762 input_set_abs_params(input_dev, ABS_PRESSURE,
763 pdata->pressure_min, pdata->pressure_max, 0, 0);
764
765 ad7877_write(spi, AD7877_REG_SEQ1, AD7877_MM_SEQUENCE);
766
767 verify = ad7877_read(spi, AD7877_REG_SEQ1);
768
769 if (verify != AD7877_MM_SEQUENCE){
770 dev_err(&spi->dev, "%s: Failed to probe %s\n",
771 dev_name(&spi->dev), input_dev->name);
772 err = -ENODEV;
773 goto err_free_mem;
774 }
775
776 if (gpio3)
777 ad7877_write(spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_3_CONF);
778
779 ad7877_setup_ts_def_msg(spi, ts);
780
781 /* Request AD7877 /DAV GPIO interrupt */
782
Dmitry Torokhovb5344222010-10-18 09:24:22 -0700783 err = request_threaded_irq(spi->irq, NULL, ad7877_irq,
784 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
785 spi->dev.driver->name, ts);
Michael Hennerich331b78e2009-03-09 20:12:52 -0700786 if (err) {
787 dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
788 goto err_free_mem;
789 }
790
791 err = sysfs_create_group(&spi->dev.kobj, &ad7877_attr_group);
792 if (err)
793 goto err_free_irq;
794
Dmitry Torokhov2fd18ab2010-10-18 09:22:38 -0700795 err = input_register_device(input_dev);
Michael Hennerich331b78e2009-03-09 20:12:52 -0700796 if (err)
797 goto err_remove_attr_group;
798
Michael Hennerich331b78e2009-03-09 20:12:52 -0700799 return 0;
800
Michael Hennerich331b78e2009-03-09 20:12:52 -0700801err_remove_attr_group:
802 sysfs_remove_group(&spi->dev.kobj, &ad7877_attr_group);
803err_free_irq:
804 free_irq(spi->irq, ts);
805err_free_mem:
806 input_free_device(input_dev);
807 kfree(ts);
808 dev_set_drvdata(&spi->dev, NULL);
809 return err;
810}
811
812static int __devexit ad7877_remove(struct spi_device *spi)
813{
Dmitry Torokhov2fd18ab2010-10-18 09:22:38 -0700814 struct ad7877 *ts = dev_get_drvdata(&spi->dev);
Michael Hennerich331b78e2009-03-09 20:12:52 -0700815
816 sysfs_remove_group(&spi->dev.kobj, &ad7877_attr_group);
Michael Hennerich331b78e2009-03-09 20:12:52 -0700817
818 ad7877_disable(ts);
819 free_irq(ts->spi->irq, ts);
820
821 input_unregister_device(ts->input);
822 kfree(ts);
823
824 dev_dbg(&spi->dev, "unregistered touchscreen\n");
825 dev_set_drvdata(&spi->dev, NULL);
826
827 return 0;
828}
829
Mark Brownfccdd7c2011-01-20 22:47:47 -0800830#ifdef CONFIG_PM_SLEEP
831static int ad7877_suspend(struct device *dev)
Michael Hennerich331b78e2009-03-09 20:12:52 -0700832{
Mark Brownfccdd7c2011-01-20 22:47:47 -0800833 struct ad7877 *ts = dev_get_drvdata(dev);
Michael Hennerich331b78e2009-03-09 20:12:52 -0700834
835 ad7877_disable(ts);
836
837 return 0;
838}
839
Mark Brownfccdd7c2011-01-20 22:47:47 -0800840static int ad7877_resume(struct device *dev)
Michael Hennerich331b78e2009-03-09 20:12:52 -0700841{
Mark Brownfccdd7c2011-01-20 22:47:47 -0800842 struct ad7877 *ts = dev_get_drvdata(dev);
Michael Hennerich331b78e2009-03-09 20:12:52 -0700843
844 ad7877_enable(ts);
845
846 return 0;
847}
Michael Hennerich331b78e2009-03-09 20:12:52 -0700848#endif
849
Mark Brownfccdd7c2011-01-20 22:47:47 -0800850static SIMPLE_DEV_PM_OPS(ad7877_pm, ad7877_suspend, ad7877_resume);
851
Michael Hennerich331b78e2009-03-09 20:12:52 -0700852static struct spi_driver ad7877_driver = {
853 .driver = {
854 .name = "ad7877",
855 .bus = &spi_bus_type,
856 .owner = THIS_MODULE,
Mark Brownfccdd7c2011-01-20 22:47:47 -0800857 .pm = &ad7877_pm,
Michael Hennerich331b78e2009-03-09 20:12:52 -0700858 },
859 .probe = ad7877_probe,
860 .remove = __devexit_p(ad7877_remove),
Michael Hennerich331b78e2009-03-09 20:12:52 -0700861};
862
863static int __init ad7877_init(void)
864{
865 return spi_register_driver(&ad7877_driver);
866}
867module_init(ad7877_init);
868
869static void __exit ad7877_exit(void)
870{
871 spi_unregister_driver(&ad7877_driver);
872}
873module_exit(ad7877_exit);
874
875MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
876MODULE_DESCRIPTION("AD7877 touchscreen Driver");
877MODULE_LICENSE("GPL");
Anton Vorontsove0626e32009-09-22 16:46:08 -0700878MODULE_ALIAS("spi:ad7877");