1331b78edSMichael Hennerich /* 2331b78edSMichael Hennerich * Copyright (C) 2006-2008 Michael Hennerich, Analog Devices Inc. 3331b78edSMichael Hennerich * 4331b78edSMichael Hennerich * Description: AD7877 based touchscreen, sensor (ADCs), DAC and GPIO driver 5331b78edSMichael Hennerich * Based on: ads7846.c 6331b78edSMichael Hennerich * 7331b78edSMichael Hennerich * Bugs: Enter bugs at http://blackfin.uclinux.org/ 8331b78edSMichael Hennerich * 9331b78edSMichael Hennerich * This program is free software; you can redistribute it and/or modify 10331b78edSMichael Hennerich * it under the terms of the GNU General Public License as published by 11331b78edSMichael Hennerich * the Free Software Foundation; either version 2 of the License, or 12331b78edSMichael Hennerich * (at your option) any later version. 13331b78edSMichael Hennerich * 14331b78edSMichael Hennerich * This program is distributed in the hope that it will be useful, 15331b78edSMichael Hennerich * but WITHOUT ANY WARRANTY; without even the implied warranty of 16331b78edSMichael Hennerich * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17331b78edSMichael Hennerich * GNU General Public License for more details. 18331b78edSMichael Hennerich * 19331b78edSMichael Hennerich * You should have received a copy of the GNU General Public License 20331b78edSMichael Hennerich * along with this program; if not, see the file COPYING, or write 21331b78edSMichael Hennerich * to the Free Software Foundation, Inc., 22331b78edSMichael Hennerich * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 23331b78edSMichael Hennerich * 24331b78edSMichael Hennerich * History: 25331b78edSMichael Hennerich * Copyright (c) 2005 David Brownell 26331b78edSMichael Hennerich * Copyright (c) 2006 Nokia Corporation 27331b78edSMichael Hennerich * Various changes: Imre Deak <imre.deak@nokia.com> 28331b78edSMichael Hennerich * 29331b78edSMichael Hennerich * Using code from: 30331b78edSMichael Hennerich * - corgi_ts.c 31331b78edSMichael Hennerich * Copyright (C) 2004-2005 Richard Purdie 32331b78edSMichael Hennerich * - omap_ts.[hc], ads7846.h, ts_osk.c 33331b78edSMichael Hennerich * Copyright (C) 2002 MontaVista Software 34331b78edSMichael Hennerich * Copyright (C) 2004 Texas Instruments 35331b78edSMichael Hennerich * Copyright (C) 2005 Dirk Behme 36331b78edSMichael Hennerich */ 37331b78edSMichael Hennerich 38331b78edSMichael Hennerich 39331b78edSMichael Hennerich #include <linux/device.h> 40331b78edSMichael Hennerich #include <linux/init.h> 41331b78edSMichael Hennerich #include <linux/delay.h> 42331b78edSMichael Hennerich #include <linux/input.h> 43331b78edSMichael Hennerich #include <linux/interrupt.h> 44331b78edSMichael Hennerich #include <linux/slab.h> 45331b78edSMichael Hennerich #include <linux/spi/spi.h> 46331b78edSMichael Hennerich #include <linux/spi/ad7877.h> 47331b78edSMichael Hennerich #include <asm/irq.h> 48331b78edSMichael Hennerich 49331b78edSMichael Hennerich #define TS_PEN_UP_TIMEOUT msecs_to_jiffies(50) 50331b78edSMichael Hennerich 51331b78edSMichael Hennerich #define MAX_SPI_FREQ_HZ 20000000 52331b78edSMichael Hennerich #define MAX_12BIT ((1<<12)-1) 53331b78edSMichael Hennerich 54331b78edSMichael Hennerich #define AD7877_REG_ZEROS 0 55331b78edSMichael Hennerich #define AD7877_REG_CTRL1 1 56331b78edSMichael Hennerich #define AD7877_REG_CTRL2 2 57331b78edSMichael Hennerich #define AD7877_REG_ALERT 3 58331b78edSMichael Hennerich #define AD7877_REG_AUX1HIGH 4 59331b78edSMichael Hennerich #define AD7877_REG_AUX1LOW 5 60331b78edSMichael Hennerich #define AD7877_REG_BAT1HIGH 6 61331b78edSMichael Hennerich #define AD7877_REG_BAT1LOW 7 62331b78edSMichael Hennerich #define AD7877_REG_BAT2HIGH 8 63331b78edSMichael Hennerich #define AD7877_REG_BAT2LOW 9 64331b78edSMichael Hennerich #define AD7877_REG_TEMP1HIGH 10 65331b78edSMichael Hennerich #define AD7877_REG_TEMP1LOW 11 66331b78edSMichael Hennerich #define AD7877_REG_SEQ0 12 67331b78edSMichael Hennerich #define AD7877_REG_SEQ1 13 68331b78edSMichael Hennerich #define AD7877_REG_DAC 14 69331b78edSMichael Hennerich #define AD7877_REG_NONE1 15 70331b78edSMichael Hennerich #define AD7877_REG_EXTWRITE 15 71331b78edSMichael Hennerich #define AD7877_REG_XPLUS 16 72331b78edSMichael Hennerich #define AD7877_REG_YPLUS 17 73331b78edSMichael Hennerich #define AD7877_REG_Z2 18 74331b78edSMichael Hennerich #define AD7877_REG_aux1 19 75331b78edSMichael Hennerich #define AD7877_REG_aux2 20 76331b78edSMichael Hennerich #define AD7877_REG_aux3 21 77331b78edSMichael Hennerich #define AD7877_REG_bat1 22 78331b78edSMichael Hennerich #define AD7877_REG_bat2 23 79331b78edSMichael Hennerich #define AD7877_REG_temp1 24 80331b78edSMichael Hennerich #define AD7877_REG_temp2 25 81331b78edSMichael Hennerich #define AD7877_REG_Z1 26 82331b78edSMichael Hennerich #define AD7877_REG_GPIOCTRL1 27 83331b78edSMichael Hennerich #define AD7877_REG_GPIOCTRL2 28 84331b78edSMichael Hennerich #define AD7877_REG_GPIODATA 29 85331b78edSMichael Hennerich #define AD7877_REG_NONE2 30 86331b78edSMichael Hennerich #define AD7877_REG_NONE3 31 87331b78edSMichael Hennerich 88331b78edSMichael Hennerich #define AD7877_SEQ_YPLUS_BIT (1<<11) 89331b78edSMichael Hennerich #define AD7877_SEQ_XPLUS_BIT (1<<10) 90331b78edSMichael Hennerich #define AD7877_SEQ_Z2_BIT (1<<9) 91331b78edSMichael Hennerich #define AD7877_SEQ_AUX1_BIT (1<<8) 92331b78edSMichael Hennerich #define AD7877_SEQ_AUX2_BIT (1<<7) 93331b78edSMichael Hennerich #define AD7877_SEQ_AUX3_BIT (1<<6) 94331b78edSMichael Hennerich #define AD7877_SEQ_BAT1_BIT (1<<5) 95331b78edSMichael Hennerich #define AD7877_SEQ_BAT2_BIT (1<<4) 96331b78edSMichael Hennerich #define AD7877_SEQ_TEMP1_BIT (1<<3) 97331b78edSMichael Hennerich #define AD7877_SEQ_TEMP2_BIT (1<<2) 98331b78edSMichael Hennerich #define AD7877_SEQ_Z1_BIT (1<<1) 99331b78edSMichael Hennerich 100331b78edSMichael Hennerich enum { 101331b78edSMichael Hennerich AD7877_SEQ_YPOS = 0, 102331b78edSMichael Hennerich AD7877_SEQ_XPOS = 1, 103331b78edSMichael Hennerich AD7877_SEQ_Z2 = 2, 104331b78edSMichael Hennerich AD7877_SEQ_AUX1 = 3, 105331b78edSMichael Hennerich AD7877_SEQ_AUX2 = 4, 106331b78edSMichael Hennerich AD7877_SEQ_AUX3 = 5, 107331b78edSMichael Hennerich AD7877_SEQ_BAT1 = 6, 108331b78edSMichael Hennerich AD7877_SEQ_BAT2 = 7, 109331b78edSMichael Hennerich AD7877_SEQ_TEMP1 = 8, 110331b78edSMichael Hennerich AD7877_SEQ_TEMP2 = 9, 111331b78edSMichael Hennerich AD7877_SEQ_Z1 = 10, 112331b78edSMichael Hennerich AD7877_NR_SENSE = 11, 113331b78edSMichael Hennerich }; 114331b78edSMichael Hennerich 115331b78edSMichael Hennerich /* DAC Register Default RANGE 0 to Vcc, Volatge Mode, DAC On */ 116331b78edSMichael Hennerich #define AD7877_DAC_CONF 0x1 117331b78edSMichael Hennerich 118331b78edSMichael Hennerich /* If gpio3 is set AUX3/GPIO3 acts as GPIO Output */ 119331b78edSMichael Hennerich #define AD7877_EXTW_GPIO_3_CONF 0x1C4 120331b78edSMichael Hennerich #define AD7877_EXTW_GPIO_DATA 0x200 121331b78edSMichael Hennerich 122331b78edSMichael Hennerich /* Control REG 2 */ 123331b78edSMichael Hennerich #define AD7877_TMR(x) ((x & 0x3) << 0) 124331b78edSMichael Hennerich #define AD7877_REF(x) ((x & 0x1) << 2) 125331b78edSMichael Hennerich #define AD7877_POL(x) ((x & 0x1) << 3) 126331b78edSMichael Hennerich #define AD7877_FCD(x) ((x & 0x3) << 4) 127331b78edSMichael Hennerich #define AD7877_PM(x) ((x & 0x3) << 6) 128331b78edSMichael Hennerich #define AD7877_ACQ(x) ((x & 0x3) << 8) 129331b78edSMichael Hennerich #define AD7877_AVG(x) ((x & 0x3) << 10) 130331b78edSMichael Hennerich 131331b78edSMichael Hennerich /* Control REG 1 */ 132331b78edSMichael Hennerich #define AD7877_SER (1 << 11) /* non-differential */ 133331b78edSMichael Hennerich #define AD7877_DFR (0 << 11) /* differential */ 134331b78edSMichael Hennerich 135331b78edSMichael Hennerich #define AD7877_MODE_NOC (0) /* Do not convert */ 136331b78edSMichael Hennerich #define AD7877_MODE_SCC (1) /* Single channel conversion */ 137331b78edSMichael Hennerich #define AD7877_MODE_SEQ0 (2) /* Sequence 0 in Slave Mode */ 138331b78edSMichael Hennerich #define AD7877_MODE_SEQ1 (3) /* Sequence 1 in Master Mode */ 139331b78edSMichael Hennerich 140331b78edSMichael Hennerich #define AD7877_CHANADD(x) ((x&0xF)<<7) 141331b78edSMichael Hennerich #define AD7877_READADD(x) ((x)<<2) 142331b78edSMichael Hennerich #define AD7877_WRITEADD(x) ((x)<<12) 143331b78edSMichael Hennerich 144331b78edSMichael Hennerich #define AD7877_READ_CHAN(x) (AD7877_WRITEADD(AD7877_REG_CTRL1) | AD7877_SER | \ 145331b78edSMichael Hennerich AD7877_MODE_SCC | AD7877_CHANADD(AD7877_REG_ ## x) | \ 146331b78edSMichael Hennerich AD7877_READADD(AD7877_REG_ ## x)) 147331b78edSMichael Hennerich 148331b78edSMichael Hennerich #define AD7877_MM_SEQUENCE (AD7877_SEQ_YPLUS_BIT | AD7877_SEQ_XPLUS_BIT | \ 149331b78edSMichael Hennerich AD7877_SEQ_Z2_BIT | AD7877_SEQ_Z1_BIT) 150331b78edSMichael Hennerich 151331b78edSMichael Hennerich /* 152331b78edSMichael Hennerich * Non-touchscreen sensors only use single-ended conversions. 153331b78edSMichael Hennerich */ 154331b78edSMichael Hennerich 155331b78edSMichael Hennerich struct ser_req { 156331b78edSMichael Hennerich u16 reset; 157331b78edSMichael Hennerich u16 ref_on; 158331b78edSMichael Hennerich u16 command; 159331b78edSMichael Hennerich u16 sample; 160331b78edSMichael Hennerich struct spi_message msg; 161331b78edSMichael Hennerich struct spi_transfer xfer[6]; 162331b78edSMichael Hennerich }; 163331b78edSMichael Hennerich 164331b78edSMichael Hennerich struct ad7877 { 165331b78edSMichael Hennerich struct input_dev *input; 166331b78edSMichael Hennerich char phys[32]; 167331b78edSMichael Hennerich 168331b78edSMichael Hennerich struct spi_device *spi; 169331b78edSMichael Hennerich u16 model; 170331b78edSMichael Hennerich u16 vref_delay_usecs; 171331b78edSMichael Hennerich u16 x_plate_ohms; 172331b78edSMichael Hennerich u16 pressure_max; 173331b78edSMichael Hennerich 174331b78edSMichael Hennerich u16 cmd_crtl1; 175331b78edSMichael Hennerich u16 cmd_crtl2; 176331b78edSMichael Hennerich u16 cmd_dummy; 177331b78edSMichael Hennerich u16 dac; 178331b78edSMichael Hennerich 179331b78edSMichael Hennerich u8 stopacq_polarity; 180331b78edSMichael Hennerich u8 first_conversion_delay; 181331b78edSMichael Hennerich u8 acquisition_time; 182331b78edSMichael Hennerich u8 averaging; 183331b78edSMichael Hennerich u8 pen_down_acc_interval; 184331b78edSMichael Hennerich 185331b78edSMichael Hennerich u16 conversion_data[AD7877_NR_SENSE]; 186331b78edSMichael Hennerich 187331b78edSMichael Hennerich struct spi_transfer xfer[AD7877_NR_SENSE + 2]; 188331b78edSMichael Hennerich struct spi_message msg; 189331b78edSMichael Hennerich 190331b78edSMichael Hennerich struct mutex mutex; 191331b78edSMichael Hennerich unsigned disabled:1; /* P: mutex */ 192331b78edSMichael Hennerich unsigned gpio3:1; /* P: mutex */ 193331b78edSMichael Hennerich unsigned gpio4:1; /* P: mutex */ 194331b78edSMichael Hennerich 195331b78edSMichael Hennerich spinlock_t lock; 196331b78edSMichael Hennerich struct timer_list timer; /* P: lock */ 197331b78edSMichael Hennerich unsigned pending:1; /* P: lock */ 198331b78edSMichael Hennerich }; 199331b78edSMichael Hennerich 200331b78edSMichael Hennerich static int gpio3; 201331b78edSMichael Hennerich module_param(gpio3, int, 0); 202331b78edSMichael Hennerich MODULE_PARM_DESC(gpio3, "If gpio3 is set to 1 AUX3 acts as GPIO3"); 203331b78edSMichael Hennerich 204331b78edSMichael Hennerich /* 205331b78edSMichael Hennerich * ad7877_read/write are only used for initial setup and for sysfs controls. 206331b78edSMichael Hennerich * The main traffic is done using spi_async() in the interrupt handler. 207331b78edSMichael Hennerich */ 208331b78edSMichael Hennerich 209331b78edSMichael Hennerich static int ad7877_read(struct spi_device *spi, u16 reg) 210331b78edSMichael Hennerich { 211331b78edSMichael Hennerich struct ser_req *req; 212331b78edSMichael Hennerich int status, ret; 213331b78edSMichael Hennerich 214331b78edSMichael Hennerich req = kzalloc(sizeof *req, GFP_KERNEL); 215331b78edSMichael Hennerich if (!req) 216331b78edSMichael Hennerich return -ENOMEM; 217331b78edSMichael Hennerich 218331b78edSMichael Hennerich spi_message_init(&req->msg); 219331b78edSMichael Hennerich 220331b78edSMichael Hennerich req->command = (u16) (AD7877_WRITEADD(AD7877_REG_CTRL1) | 221331b78edSMichael Hennerich AD7877_READADD(reg)); 222331b78edSMichael Hennerich req->xfer[0].tx_buf = &req->command; 223331b78edSMichael Hennerich req->xfer[0].len = 2; 224331b78edSMichael Hennerich 225331b78edSMichael Hennerich req->xfer[1].rx_buf = &req->sample; 226331b78edSMichael Hennerich req->xfer[1].len = 2; 227331b78edSMichael Hennerich 228331b78edSMichael Hennerich spi_message_add_tail(&req->xfer[0], &req->msg); 229331b78edSMichael Hennerich spi_message_add_tail(&req->xfer[1], &req->msg); 230331b78edSMichael Hennerich 231331b78edSMichael Hennerich status = spi_sync(spi, &req->msg); 232331b78edSMichael Hennerich ret = status ? : req->sample; 233331b78edSMichael Hennerich 234331b78edSMichael Hennerich kfree(req); 235331b78edSMichael Hennerich 236331b78edSMichael Hennerich return ret; 237331b78edSMichael Hennerich } 238331b78edSMichael Hennerich 239331b78edSMichael Hennerich static int ad7877_write(struct spi_device *spi, u16 reg, u16 val) 240331b78edSMichael Hennerich { 241331b78edSMichael Hennerich struct ser_req *req; 242331b78edSMichael Hennerich int status; 243331b78edSMichael Hennerich 244331b78edSMichael Hennerich req = kzalloc(sizeof *req, GFP_KERNEL); 245331b78edSMichael Hennerich if (!req) 246331b78edSMichael Hennerich return -ENOMEM; 247331b78edSMichael Hennerich 248331b78edSMichael Hennerich spi_message_init(&req->msg); 249331b78edSMichael Hennerich 250331b78edSMichael Hennerich req->command = (u16) (AD7877_WRITEADD(reg) | (val & MAX_12BIT)); 251331b78edSMichael Hennerich req->xfer[0].tx_buf = &req->command; 252331b78edSMichael Hennerich req->xfer[0].len = 2; 253331b78edSMichael Hennerich 254331b78edSMichael Hennerich spi_message_add_tail(&req->xfer[0], &req->msg); 255331b78edSMichael Hennerich 256331b78edSMichael Hennerich status = spi_sync(spi, &req->msg); 257331b78edSMichael Hennerich 258331b78edSMichael Hennerich kfree(req); 259331b78edSMichael Hennerich 260331b78edSMichael Hennerich return status; 261331b78edSMichael Hennerich } 262331b78edSMichael Hennerich 263331b78edSMichael Hennerich static int ad7877_read_adc(struct spi_device *spi, unsigned command) 264331b78edSMichael Hennerich { 265331b78edSMichael Hennerich struct ad7877 *ts = dev_get_drvdata(&spi->dev); 266331b78edSMichael Hennerich struct ser_req *req; 267331b78edSMichael Hennerich int status; 268331b78edSMichael Hennerich int sample; 269331b78edSMichael Hennerich int i; 270331b78edSMichael Hennerich 271331b78edSMichael Hennerich req = kzalloc(sizeof *req, GFP_KERNEL); 272331b78edSMichael Hennerich if (!req) 273331b78edSMichael Hennerich return -ENOMEM; 274331b78edSMichael Hennerich 275331b78edSMichael Hennerich spi_message_init(&req->msg); 276331b78edSMichael Hennerich 277331b78edSMichael Hennerich /* activate reference, so it has time to settle; */ 278331b78edSMichael Hennerich req->ref_on = AD7877_WRITEADD(AD7877_REG_CTRL2) | 279331b78edSMichael Hennerich AD7877_POL(ts->stopacq_polarity) | 280331b78edSMichael Hennerich AD7877_AVG(0) | AD7877_PM(2) | AD7877_TMR(0) | 281331b78edSMichael Hennerich AD7877_ACQ(ts->acquisition_time) | AD7877_FCD(0); 282331b78edSMichael Hennerich 283331b78edSMichael Hennerich req->reset = AD7877_WRITEADD(AD7877_REG_CTRL1) | AD7877_MODE_NOC; 284331b78edSMichael Hennerich 285331b78edSMichael Hennerich req->command = (u16) command; 286331b78edSMichael Hennerich 287331b78edSMichael Hennerich req->xfer[0].tx_buf = &req->reset; 288331b78edSMichael Hennerich req->xfer[0].len = 2; 289331b78edSMichael Hennerich 290331b78edSMichael Hennerich req->xfer[1].tx_buf = &req->ref_on; 291331b78edSMichael Hennerich req->xfer[1].len = 2; 292331b78edSMichael Hennerich req->xfer[1].delay_usecs = ts->vref_delay_usecs; 293331b78edSMichael Hennerich 294331b78edSMichael Hennerich req->xfer[2].tx_buf = &req->command; 295331b78edSMichael Hennerich req->xfer[2].len = 2; 296331b78edSMichael Hennerich req->xfer[2].delay_usecs = ts->vref_delay_usecs; 297331b78edSMichael Hennerich 298331b78edSMichael Hennerich req->xfer[3].rx_buf = &req->sample; 299331b78edSMichael Hennerich req->xfer[3].len = 2; 300331b78edSMichael Hennerich 301331b78edSMichael Hennerich req->xfer[4].tx_buf = &ts->cmd_crtl2; /*REF OFF*/ 302331b78edSMichael Hennerich req->xfer[4].len = 2; 303331b78edSMichael Hennerich 304331b78edSMichael Hennerich req->xfer[5].tx_buf = &ts->cmd_crtl1; /*DEFAULT*/ 305331b78edSMichael Hennerich req->xfer[5].len = 2; 306331b78edSMichael Hennerich 307331b78edSMichael Hennerich /* group all the transfers together, so we can't interfere with 308331b78edSMichael Hennerich * reading touchscreen state; disable penirq while sampling 309331b78edSMichael Hennerich */ 310331b78edSMichael Hennerich for (i = 0; i < 6; i++) 311331b78edSMichael Hennerich spi_message_add_tail(&req->xfer[i], &req->msg); 312331b78edSMichael Hennerich 313331b78edSMichael Hennerich status = spi_sync(spi, &req->msg); 314331b78edSMichael Hennerich sample = req->sample; 315331b78edSMichael Hennerich 316331b78edSMichael Hennerich kfree(req); 317331b78edSMichael Hennerich 318331b78edSMichael Hennerich return status ? : sample; 319331b78edSMichael Hennerich } 320331b78edSMichael Hennerich 321331b78edSMichael Hennerich static void ad7877_rx(struct ad7877 *ts) 322331b78edSMichael Hennerich { 323331b78edSMichael Hennerich struct input_dev *input_dev = ts->input; 324331b78edSMichael Hennerich unsigned Rt; 325331b78edSMichael Hennerich u16 x, y, z1, z2; 326331b78edSMichael Hennerich 327331b78edSMichael Hennerich x = ts->conversion_data[AD7877_SEQ_XPOS] & MAX_12BIT; 328331b78edSMichael Hennerich y = ts->conversion_data[AD7877_SEQ_YPOS] & MAX_12BIT; 329331b78edSMichael Hennerich z1 = ts->conversion_data[AD7877_SEQ_Z1] & MAX_12BIT; 330331b78edSMichael Hennerich z2 = ts->conversion_data[AD7877_SEQ_Z2] & MAX_12BIT; 331331b78edSMichael Hennerich 332331b78edSMichael Hennerich /* 333331b78edSMichael Hennerich * The samples processed here are already preprocessed by the AD7877. 334331b78edSMichael Hennerich * The preprocessing function consists of an averaging filter. 335331b78edSMichael Hennerich * The combination of 'first conversion delay' and averaging provides a robust solution, 336331b78edSMichael Hennerich * discarding the spurious noise in the signal and keeping only the data of interest. 337331b78edSMichael Hennerich * The size of the averaging filter is programmable. (dev.platform_data, see linux/spi/ad7877.h) 338331b78edSMichael Hennerich * Other user-programmable conversion controls include variable acquisition time, 339331b78edSMichael Hennerich * and first conversion delay. Up to 16 averages can be taken per conversion. 340331b78edSMichael Hennerich */ 341331b78edSMichael Hennerich 342331b78edSMichael Hennerich if (likely(x && z1)) { 343331b78edSMichael Hennerich /* compute touch pressure resistance using equation #1 */ 344331b78edSMichael Hennerich Rt = (z2 - z1) * x * ts->x_plate_ohms; 345331b78edSMichael Hennerich Rt /= z1; 346331b78edSMichael Hennerich Rt = (Rt + 2047) >> 12; 347331b78edSMichael Hennerich 348331b78edSMichael Hennerich input_report_abs(input_dev, ABS_X, x); 349331b78edSMichael Hennerich input_report_abs(input_dev, ABS_Y, y); 350331b78edSMichael Hennerich input_report_abs(input_dev, ABS_PRESSURE, Rt); 351331b78edSMichael Hennerich input_sync(input_dev); 352331b78edSMichael Hennerich } 353331b78edSMichael Hennerich } 354331b78edSMichael Hennerich 355331b78edSMichael Hennerich static inline void ad7877_ts_event_release(struct ad7877 *ts) 356331b78edSMichael Hennerich { 357331b78edSMichael Hennerich struct input_dev *input_dev = ts->input; 358331b78edSMichael Hennerich 359331b78edSMichael Hennerich input_report_abs(input_dev, ABS_PRESSURE, 0); 360331b78edSMichael Hennerich input_sync(input_dev); 361331b78edSMichael Hennerich } 362331b78edSMichael Hennerich 363331b78edSMichael Hennerich static void ad7877_timer(unsigned long handle) 364331b78edSMichael Hennerich { 365331b78edSMichael Hennerich struct ad7877 *ts = (void *)handle; 366331b78edSMichael Hennerich 367331b78edSMichael Hennerich ad7877_ts_event_release(ts); 368331b78edSMichael Hennerich } 369331b78edSMichael Hennerich 370331b78edSMichael Hennerich static irqreturn_t ad7877_irq(int irq, void *handle) 371331b78edSMichael Hennerich { 372331b78edSMichael Hennerich struct ad7877 *ts = handle; 373331b78edSMichael Hennerich unsigned long flags; 374331b78edSMichael Hennerich int status; 375331b78edSMichael Hennerich 376331b78edSMichael Hennerich /* 377331b78edSMichael Hennerich * The repeated conversion sequencer controlled by TMR kicked off 378331b78edSMichael Hennerich * too fast. We ignore the last and process the sample sequence 379331b78edSMichael Hennerich * currently in the queue. It can't be older than 9.4ms, and we 380331b78edSMichael Hennerich * need to avoid that ts->msg doesn't get issued twice while in work. 381331b78edSMichael Hennerich */ 382331b78edSMichael Hennerich 383331b78edSMichael Hennerich spin_lock_irqsave(&ts->lock, flags); 384331b78edSMichael Hennerich if (!ts->pending) { 385331b78edSMichael Hennerich ts->pending = 1; 386331b78edSMichael Hennerich 387331b78edSMichael Hennerich status = spi_async(ts->spi, &ts->msg); 388331b78edSMichael Hennerich if (status) 389331b78edSMichael Hennerich dev_err(&ts->spi->dev, "spi_sync --> %d\n", status); 390331b78edSMichael Hennerich } 391331b78edSMichael Hennerich spin_unlock_irqrestore(&ts->lock, flags); 392331b78edSMichael Hennerich 393331b78edSMichael Hennerich return IRQ_HANDLED; 394331b78edSMichael Hennerich } 395331b78edSMichael Hennerich 396331b78edSMichael Hennerich static void ad7877_callback(void *_ts) 397331b78edSMichael Hennerich { 398331b78edSMichael Hennerich struct ad7877 *ts = _ts; 399331b78edSMichael Hennerich 400331b78edSMichael Hennerich spin_lock_irq(&ts->lock); 401331b78edSMichael Hennerich 402331b78edSMichael Hennerich ad7877_rx(ts); 403331b78edSMichael Hennerich ts->pending = 0; 404331b78edSMichael Hennerich mod_timer(&ts->timer, jiffies + TS_PEN_UP_TIMEOUT); 405331b78edSMichael Hennerich 406331b78edSMichael Hennerich spin_unlock_irq(&ts->lock); 407331b78edSMichael Hennerich } 408331b78edSMichael Hennerich 409331b78edSMichael Hennerich static void ad7877_disable(struct ad7877 *ts) 410331b78edSMichael Hennerich { 411331b78edSMichael Hennerich mutex_lock(&ts->mutex); 412331b78edSMichael Hennerich 413331b78edSMichael Hennerich if (!ts->disabled) { 414331b78edSMichael Hennerich ts->disabled = 1; 415331b78edSMichael Hennerich disable_irq(ts->spi->irq); 416331b78edSMichael Hennerich 417331b78edSMichael Hennerich /* Wait for spi_async callback */ 418331b78edSMichael Hennerich while (ts->pending) 419331b78edSMichael Hennerich msleep(1); 420331b78edSMichael Hennerich 421331b78edSMichael Hennerich if (del_timer_sync(&ts->timer)) 422331b78edSMichael Hennerich ad7877_ts_event_release(ts); 423331b78edSMichael Hennerich } 424331b78edSMichael Hennerich 425331b78edSMichael Hennerich /* we know the chip's in lowpower mode since we always 426331b78edSMichael Hennerich * leave it that way after every request 427331b78edSMichael Hennerich */ 428331b78edSMichael Hennerich 429331b78edSMichael Hennerich mutex_unlock(&ts->mutex); 430331b78edSMichael Hennerich } 431331b78edSMichael Hennerich 432331b78edSMichael Hennerich static void ad7877_enable(struct ad7877 *ts) 433331b78edSMichael Hennerich { 434331b78edSMichael Hennerich mutex_lock(&ts->mutex); 435331b78edSMichael Hennerich 436331b78edSMichael Hennerich if (ts->disabled) { 437331b78edSMichael Hennerich ts->disabled = 0; 438331b78edSMichael Hennerich enable_irq(ts->spi->irq); 439331b78edSMichael Hennerich } 440331b78edSMichael Hennerich 441331b78edSMichael Hennerich mutex_unlock(&ts->mutex); 442331b78edSMichael Hennerich } 443331b78edSMichael Hennerich 444331b78edSMichael Hennerich #define SHOW(name) static ssize_t \ 445331b78edSMichael Hennerich name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \ 446331b78edSMichael Hennerich { \ 447331b78edSMichael Hennerich struct ad7877 *ts = dev_get_drvdata(dev); \ 448331b78edSMichael Hennerich ssize_t v = ad7877_read_adc(ts->spi, \ 449331b78edSMichael Hennerich AD7877_READ_CHAN(name)); \ 450331b78edSMichael Hennerich if (v < 0) \ 451331b78edSMichael Hennerich return v; \ 452331b78edSMichael Hennerich return sprintf(buf, "%u\n", (unsigned) v); \ 453331b78edSMichael Hennerich } \ 454331b78edSMichael Hennerich static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL); 455331b78edSMichael Hennerich 456331b78edSMichael Hennerich SHOW(aux1) 457331b78edSMichael Hennerich SHOW(aux2) 458331b78edSMichael Hennerich SHOW(aux3) 459331b78edSMichael Hennerich SHOW(bat1) 460331b78edSMichael Hennerich SHOW(bat2) 461331b78edSMichael Hennerich SHOW(temp1) 462331b78edSMichael Hennerich SHOW(temp2) 463331b78edSMichael Hennerich 464331b78edSMichael Hennerich static ssize_t ad7877_disable_show(struct device *dev, 465331b78edSMichael Hennerich struct device_attribute *attr, char *buf) 466331b78edSMichael Hennerich { 467331b78edSMichael Hennerich struct ad7877 *ts = dev_get_drvdata(dev); 468331b78edSMichael Hennerich 469331b78edSMichael Hennerich return sprintf(buf, "%u\n", ts->disabled); 470331b78edSMichael Hennerich } 471331b78edSMichael Hennerich 472331b78edSMichael Hennerich static ssize_t ad7877_disable_store(struct device *dev, 473331b78edSMichael Hennerich struct device_attribute *attr, 474331b78edSMichael Hennerich const char *buf, size_t count) 475331b78edSMichael Hennerich { 476331b78edSMichael Hennerich struct ad7877 *ts = dev_get_drvdata(dev); 477331b78edSMichael Hennerich unsigned long val; 478331b78edSMichael Hennerich int error; 479331b78edSMichael Hennerich 480331b78edSMichael Hennerich error = strict_strtoul(buf, 10, &val); 481331b78edSMichael Hennerich if (error) 482331b78edSMichael Hennerich return error; 483331b78edSMichael Hennerich 484331b78edSMichael Hennerich if (val) 485331b78edSMichael Hennerich ad7877_disable(ts); 486331b78edSMichael Hennerich else 487331b78edSMichael Hennerich ad7877_enable(ts); 488331b78edSMichael Hennerich 489331b78edSMichael Hennerich return count; 490331b78edSMichael Hennerich } 491331b78edSMichael Hennerich 492331b78edSMichael Hennerich static DEVICE_ATTR(disable, 0664, ad7877_disable_show, ad7877_disable_store); 493331b78edSMichael Hennerich 494331b78edSMichael Hennerich static ssize_t ad7877_dac_show(struct device *dev, 495331b78edSMichael Hennerich struct device_attribute *attr, char *buf) 496331b78edSMichael Hennerich { 497331b78edSMichael Hennerich struct ad7877 *ts = dev_get_drvdata(dev); 498331b78edSMichael Hennerich 499331b78edSMichael Hennerich return sprintf(buf, "%u\n", ts->dac); 500331b78edSMichael Hennerich } 501331b78edSMichael Hennerich 502331b78edSMichael Hennerich static ssize_t ad7877_dac_store(struct device *dev, 503331b78edSMichael Hennerich struct device_attribute *attr, 504331b78edSMichael Hennerich const char *buf, size_t count) 505331b78edSMichael Hennerich { 506331b78edSMichael Hennerich struct ad7877 *ts = dev_get_drvdata(dev); 507331b78edSMichael Hennerich unsigned long val; 508331b78edSMichael Hennerich int error; 509331b78edSMichael Hennerich 510331b78edSMichael Hennerich error = strict_strtoul(buf, 10, &val); 511331b78edSMichael Hennerich if (error) 512331b78edSMichael Hennerich return error; 513331b78edSMichael Hennerich 514331b78edSMichael Hennerich mutex_lock(&ts->mutex); 515331b78edSMichael Hennerich ts->dac = val & 0xFF; 516331b78edSMichael Hennerich ad7877_write(ts->spi, AD7877_REG_DAC, (ts->dac << 4) | AD7877_DAC_CONF); 517331b78edSMichael Hennerich mutex_unlock(&ts->mutex); 518331b78edSMichael Hennerich 519331b78edSMichael Hennerich return count; 520331b78edSMichael Hennerich } 521331b78edSMichael Hennerich 522331b78edSMichael Hennerich static DEVICE_ATTR(dac, 0664, ad7877_dac_show, ad7877_dac_store); 523331b78edSMichael Hennerich 524331b78edSMichael Hennerich static ssize_t ad7877_gpio3_show(struct device *dev, 525331b78edSMichael Hennerich struct device_attribute *attr, char *buf) 526331b78edSMichael Hennerich { 527331b78edSMichael Hennerich struct ad7877 *ts = dev_get_drvdata(dev); 528331b78edSMichael Hennerich 529331b78edSMichael Hennerich return sprintf(buf, "%u\n", ts->gpio3); 530331b78edSMichael Hennerich } 531331b78edSMichael Hennerich 532331b78edSMichael Hennerich static ssize_t ad7877_gpio3_store(struct device *dev, 533331b78edSMichael Hennerich struct device_attribute *attr, 534331b78edSMichael Hennerich const char *buf, size_t count) 535331b78edSMichael Hennerich { 536331b78edSMichael Hennerich struct ad7877 *ts = dev_get_drvdata(dev); 537331b78edSMichael Hennerich unsigned long val; 538331b78edSMichael Hennerich int error; 539331b78edSMichael Hennerich 540331b78edSMichael Hennerich error = strict_strtoul(buf, 10, &val); 541331b78edSMichael Hennerich if (error) 542331b78edSMichael Hennerich return error; 543331b78edSMichael Hennerich 544331b78edSMichael Hennerich mutex_lock(&ts->mutex); 545331b78edSMichael Hennerich ts->gpio3 = !!val; 546331b78edSMichael Hennerich ad7877_write(ts->spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_DATA | 547331b78edSMichael Hennerich (ts->gpio4 << 4) | (ts->gpio3 << 5)); 548331b78edSMichael Hennerich mutex_unlock(&ts->mutex); 549331b78edSMichael Hennerich 550331b78edSMichael Hennerich return count; 551331b78edSMichael Hennerich } 552331b78edSMichael Hennerich 553331b78edSMichael Hennerich static DEVICE_ATTR(gpio3, 0664, ad7877_gpio3_show, ad7877_gpio3_store); 554331b78edSMichael Hennerich 555331b78edSMichael Hennerich static ssize_t ad7877_gpio4_show(struct device *dev, 556331b78edSMichael Hennerich struct device_attribute *attr, char *buf) 557331b78edSMichael Hennerich { 558331b78edSMichael Hennerich struct ad7877 *ts = dev_get_drvdata(dev); 559331b78edSMichael Hennerich 560331b78edSMichael Hennerich return sprintf(buf, "%u\n", ts->gpio4); 561331b78edSMichael Hennerich } 562331b78edSMichael Hennerich 563331b78edSMichael Hennerich static ssize_t ad7877_gpio4_store(struct device *dev, 564331b78edSMichael Hennerich struct device_attribute *attr, 565331b78edSMichael Hennerich const char *buf, size_t count) 566331b78edSMichael Hennerich { 567331b78edSMichael Hennerich struct ad7877 *ts = dev_get_drvdata(dev); 568331b78edSMichael Hennerich unsigned long val; 569331b78edSMichael Hennerich int error; 570331b78edSMichael Hennerich 571331b78edSMichael Hennerich error = strict_strtoul(buf, 10, &val); 572331b78edSMichael Hennerich if (error) 573331b78edSMichael Hennerich return error; 574331b78edSMichael Hennerich 575331b78edSMichael Hennerich mutex_lock(&ts->mutex); 576331b78edSMichael Hennerich ts->gpio4 = !!val; 577331b78edSMichael Hennerich ad7877_write(ts->spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_DATA | 578331b78edSMichael Hennerich (ts->gpio4 << 4) | (ts->gpio3 << 5)); 579331b78edSMichael Hennerich mutex_unlock(&ts->mutex); 580331b78edSMichael Hennerich 581331b78edSMichael Hennerich return count; 582331b78edSMichael Hennerich } 583331b78edSMichael Hennerich 584331b78edSMichael Hennerich static DEVICE_ATTR(gpio4, 0664, ad7877_gpio4_show, ad7877_gpio4_store); 585331b78edSMichael Hennerich 586331b78edSMichael Hennerich static struct attribute *ad7877_attributes[] = { 587331b78edSMichael Hennerich &dev_attr_temp1.attr, 588331b78edSMichael Hennerich &dev_attr_temp2.attr, 589331b78edSMichael Hennerich &dev_attr_aux1.attr, 590331b78edSMichael Hennerich &dev_attr_aux2.attr, 591331b78edSMichael Hennerich &dev_attr_bat1.attr, 592331b78edSMichael Hennerich &dev_attr_bat2.attr, 593331b78edSMichael Hennerich &dev_attr_disable.attr, 594331b78edSMichael Hennerich &dev_attr_dac.attr, 595331b78edSMichael Hennerich &dev_attr_gpio4.attr, 596331b78edSMichael Hennerich NULL 597331b78edSMichael Hennerich }; 598331b78edSMichael Hennerich 599331b78edSMichael Hennerich static const struct attribute_group ad7877_attr_group = { 600331b78edSMichael Hennerich .attrs = ad7877_attributes, 601331b78edSMichael Hennerich }; 602331b78edSMichael Hennerich 603331b78edSMichael Hennerich static void ad7877_setup_ts_def_msg(struct spi_device *spi, struct ad7877 *ts) 604331b78edSMichael Hennerich { 605331b78edSMichael Hennerich struct spi_message *m; 606331b78edSMichael Hennerich int i; 607331b78edSMichael Hennerich 608331b78edSMichael Hennerich ts->cmd_crtl2 = AD7877_WRITEADD(AD7877_REG_CTRL2) | 609331b78edSMichael Hennerich AD7877_POL(ts->stopacq_polarity) | 610331b78edSMichael Hennerich AD7877_AVG(ts->averaging) | AD7877_PM(1) | 611331b78edSMichael Hennerich AD7877_TMR(ts->pen_down_acc_interval) | 612331b78edSMichael Hennerich AD7877_ACQ(ts->acquisition_time) | 613331b78edSMichael Hennerich AD7877_FCD(ts->first_conversion_delay); 614331b78edSMichael Hennerich 615331b78edSMichael Hennerich ad7877_write(spi, AD7877_REG_CTRL2, ts->cmd_crtl2); 616331b78edSMichael Hennerich 617331b78edSMichael Hennerich ts->cmd_crtl1 = AD7877_WRITEADD(AD7877_REG_CTRL1) | 618331b78edSMichael Hennerich AD7877_READADD(AD7877_REG_XPLUS-1) | 619331b78edSMichael Hennerich AD7877_MODE_SEQ1 | AD7877_DFR; 620331b78edSMichael Hennerich 621331b78edSMichael Hennerich ad7877_write(spi, AD7877_REG_CTRL1, ts->cmd_crtl1); 622331b78edSMichael Hennerich 623331b78edSMichael Hennerich ts->cmd_dummy = 0; 624331b78edSMichael Hennerich 625331b78edSMichael Hennerich m = &ts->msg; 626331b78edSMichael Hennerich 627331b78edSMichael Hennerich spi_message_init(m); 628331b78edSMichael Hennerich 629331b78edSMichael Hennerich m->complete = ad7877_callback; 630331b78edSMichael Hennerich m->context = ts; 631331b78edSMichael Hennerich 632331b78edSMichael Hennerich ts->xfer[0].tx_buf = &ts->cmd_crtl1; 633331b78edSMichael Hennerich ts->xfer[0].len = 2; 634331b78edSMichael Hennerich 635331b78edSMichael Hennerich spi_message_add_tail(&ts->xfer[0], m); 636331b78edSMichael Hennerich 637331b78edSMichael Hennerich ts->xfer[1].tx_buf = &ts->cmd_dummy; /* Send ZERO */ 638331b78edSMichael Hennerich ts->xfer[1].len = 2; 639331b78edSMichael Hennerich 640331b78edSMichael Hennerich spi_message_add_tail(&ts->xfer[1], m); 641331b78edSMichael Hennerich 642331b78edSMichael Hennerich for (i = 0; i < 11; i++) { 643331b78edSMichael Hennerich ts->xfer[i + 2].rx_buf = &ts->conversion_data[AD7877_SEQ_YPOS + i]; 644331b78edSMichael Hennerich ts->xfer[i + 2].len = 2; 645331b78edSMichael Hennerich spi_message_add_tail(&ts->xfer[i + 2], m); 646331b78edSMichael Hennerich } 647331b78edSMichael Hennerich } 648331b78edSMichael Hennerich 649331b78edSMichael Hennerich static int __devinit ad7877_probe(struct spi_device *spi) 650331b78edSMichael Hennerich { 651331b78edSMichael Hennerich struct ad7877 *ts; 652331b78edSMichael Hennerich struct input_dev *input_dev; 653331b78edSMichael Hennerich struct ad7877_platform_data *pdata = spi->dev.platform_data; 654331b78edSMichael Hennerich int err; 655331b78edSMichael Hennerich u16 verify; 656331b78edSMichael Hennerich 657331b78edSMichael Hennerich if (!spi->irq) { 658331b78edSMichael Hennerich dev_dbg(&spi->dev, "no IRQ?\n"); 659331b78edSMichael Hennerich return -ENODEV; 660331b78edSMichael Hennerich } 661331b78edSMichael Hennerich 662331b78edSMichael Hennerich if (!pdata) { 663331b78edSMichael Hennerich dev_dbg(&spi->dev, "no platform data?\n"); 664331b78edSMichael Hennerich return -ENODEV; 665331b78edSMichael Hennerich } 666331b78edSMichael Hennerich 667331b78edSMichael Hennerich /* don't exceed max specified SPI CLK frequency */ 668331b78edSMichael Hennerich if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) { 669331b78edSMichael Hennerich dev_dbg(&spi->dev, "SPI CLK %d Hz?\n",spi->max_speed_hz); 670331b78edSMichael Hennerich return -EINVAL; 671331b78edSMichael Hennerich } 672331b78edSMichael Hennerich 673331b78edSMichael Hennerich ts = kzalloc(sizeof(struct ad7877), GFP_KERNEL); 674331b78edSMichael Hennerich input_dev = input_allocate_device(); 675331b78edSMichael Hennerich if (!ts || !input_dev) { 676331b78edSMichael Hennerich err = -ENOMEM; 677331b78edSMichael Hennerich goto err_free_mem; 678331b78edSMichael Hennerich } 679331b78edSMichael Hennerich 680331b78edSMichael Hennerich dev_set_drvdata(&spi->dev, ts); 681331b78edSMichael Hennerich ts->spi = spi; 682331b78edSMichael Hennerich ts->input = input_dev; 683331b78edSMichael Hennerich 684331b78edSMichael Hennerich setup_timer(&ts->timer, ad7877_timer, (unsigned long) ts); 685331b78edSMichael Hennerich mutex_init(&ts->mutex); 686331b78edSMichael Hennerich spin_lock_init(&ts->lock); 687331b78edSMichael Hennerich 688331b78edSMichael Hennerich ts->model = pdata->model ? : 7877; 689331b78edSMichael Hennerich ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100; 690331b78edSMichael Hennerich ts->x_plate_ohms = pdata->x_plate_ohms ? : 400; 691331b78edSMichael Hennerich ts->pressure_max = pdata->pressure_max ? : ~0; 692331b78edSMichael Hennerich 693331b78edSMichael Hennerich ts->stopacq_polarity = pdata->stopacq_polarity; 694331b78edSMichael Hennerich ts->first_conversion_delay = pdata->first_conversion_delay; 695331b78edSMichael Hennerich ts->acquisition_time = pdata->acquisition_time; 696331b78edSMichael Hennerich ts->averaging = pdata->averaging; 697331b78edSMichael Hennerich ts->pen_down_acc_interval = pdata->pen_down_acc_interval; 698331b78edSMichael Hennerich 699331b78edSMichael Hennerich snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev)); 700331b78edSMichael Hennerich 701331b78edSMichael Hennerich input_dev->name = "AD7877 Touchscreen"; 702331b78edSMichael Hennerich input_dev->phys = ts->phys; 703331b78edSMichael Hennerich input_dev->dev.parent = &spi->dev; 704331b78edSMichael Hennerich 705331b78edSMichael Hennerich __set_bit(EV_ABS, input_dev->evbit); 706331b78edSMichael Hennerich __set_bit(ABS_X, input_dev->absbit); 707331b78edSMichael Hennerich __set_bit(ABS_Y, input_dev->absbit); 708331b78edSMichael Hennerich __set_bit(ABS_PRESSURE, input_dev->absbit); 709331b78edSMichael Hennerich 710331b78edSMichael Hennerich input_set_abs_params(input_dev, ABS_X, 711331b78edSMichael Hennerich pdata->x_min ? : 0, 712331b78edSMichael Hennerich pdata->x_max ? : MAX_12BIT, 713331b78edSMichael Hennerich 0, 0); 714331b78edSMichael Hennerich input_set_abs_params(input_dev, ABS_Y, 715331b78edSMichael Hennerich pdata->y_min ? : 0, 716331b78edSMichael Hennerich pdata->y_max ? : MAX_12BIT, 717331b78edSMichael Hennerich 0, 0); 718331b78edSMichael Hennerich input_set_abs_params(input_dev, ABS_PRESSURE, 719331b78edSMichael Hennerich pdata->pressure_min, pdata->pressure_max, 0, 0); 720331b78edSMichael Hennerich 721331b78edSMichael Hennerich ad7877_write(spi, AD7877_REG_SEQ1, AD7877_MM_SEQUENCE); 722331b78edSMichael Hennerich 723331b78edSMichael Hennerich verify = ad7877_read(spi, AD7877_REG_SEQ1); 724331b78edSMichael Hennerich 725331b78edSMichael Hennerich if (verify != AD7877_MM_SEQUENCE){ 726331b78edSMichael Hennerich dev_err(&spi->dev, "%s: Failed to probe %s\n", 727331b78edSMichael Hennerich dev_name(&spi->dev), input_dev->name); 728331b78edSMichael Hennerich err = -ENODEV; 729331b78edSMichael Hennerich goto err_free_mem; 730331b78edSMichael Hennerich } 731331b78edSMichael Hennerich 732331b78edSMichael Hennerich if (gpio3) 733331b78edSMichael Hennerich ad7877_write(spi, AD7877_REG_EXTWRITE, AD7877_EXTW_GPIO_3_CONF); 734331b78edSMichael Hennerich 735331b78edSMichael Hennerich ad7877_setup_ts_def_msg(spi, ts); 736331b78edSMichael Hennerich 737331b78edSMichael Hennerich /* Request AD7877 /DAV GPIO interrupt */ 738331b78edSMichael Hennerich 7390bc69ce6SMichael Hennerich err = request_irq(spi->irq, ad7877_irq, IRQF_TRIGGER_FALLING, 7400bc69ce6SMichael Hennerich spi->dev.driver->name, ts); 741331b78edSMichael Hennerich if (err) { 742331b78edSMichael Hennerich dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq); 743331b78edSMichael Hennerich goto err_free_mem; 744331b78edSMichael Hennerich } 745331b78edSMichael Hennerich 746331b78edSMichael Hennerich err = sysfs_create_group(&spi->dev.kobj, &ad7877_attr_group); 747331b78edSMichael Hennerich if (err) 748331b78edSMichael Hennerich goto err_free_irq; 749331b78edSMichael Hennerich 750331b78edSMichael Hennerich err = device_create_file(&spi->dev, 751331b78edSMichael Hennerich gpio3 ? &dev_attr_gpio3 : &dev_attr_aux3); 752331b78edSMichael Hennerich if (err) 753331b78edSMichael Hennerich goto err_remove_attr_group; 754331b78edSMichael Hennerich 755331b78edSMichael Hennerich err = input_register_device(input_dev); 756331b78edSMichael Hennerich if (err) 757331b78edSMichael Hennerich goto err_remove_attr; 758331b78edSMichael Hennerich 759331b78edSMichael Hennerich return 0; 760331b78edSMichael Hennerich 761331b78edSMichael Hennerich err_remove_attr: 762331b78edSMichael Hennerich device_remove_file(&spi->dev, 763331b78edSMichael Hennerich gpio3 ? &dev_attr_gpio3 : &dev_attr_aux3); 764331b78edSMichael Hennerich err_remove_attr_group: 765331b78edSMichael Hennerich sysfs_remove_group(&spi->dev.kobj, &ad7877_attr_group); 766331b78edSMichael Hennerich err_free_irq: 767331b78edSMichael Hennerich free_irq(spi->irq, ts); 768331b78edSMichael Hennerich err_free_mem: 769331b78edSMichael Hennerich input_free_device(input_dev); 770331b78edSMichael Hennerich kfree(ts); 771331b78edSMichael Hennerich dev_set_drvdata(&spi->dev, NULL); 772331b78edSMichael Hennerich return err; 773331b78edSMichael Hennerich } 774331b78edSMichael Hennerich 775331b78edSMichael Hennerich static int __devexit ad7877_remove(struct spi_device *spi) 776331b78edSMichael Hennerich { 777331b78edSMichael Hennerich struct ad7877 *ts = dev_get_drvdata(&spi->dev); 778331b78edSMichael Hennerich 779331b78edSMichael Hennerich sysfs_remove_group(&spi->dev.kobj, &ad7877_attr_group); 780331b78edSMichael Hennerich device_remove_file(&spi->dev, 781331b78edSMichael Hennerich gpio3 ? &dev_attr_gpio3 : &dev_attr_aux3); 782331b78edSMichael Hennerich 783331b78edSMichael Hennerich ad7877_disable(ts); 784331b78edSMichael Hennerich free_irq(ts->spi->irq, ts); 785331b78edSMichael Hennerich 786331b78edSMichael Hennerich input_unregister_device(ts->input); 787331b78edSMichael Hennerich kfree(ts); 788331b78edSMichael Hennerich 789331b78edSMichael Hennerich dev_dbg(&spi->dev, "unregistered touchscreen\n"); 790331b78edSMichael Hennerich dev_set_drvdata(&spi->dev, NULL); 791331b78edSMichael Hennerich 792331b78edSMichael Hennerich return 0; 793331b78edSMichael Hennerich } 794331b78edSMichael Hennerich 795331b78edSMichael Hennerich #ifdef CONFIG_PM 796331b78edSMichael Hennerich static int ad7877_suspend(struct spi_device *spi, pm_message_t message) 797331b78edSMichael Hennerich { 798331b78edSMichael Hennerich struct ad7877 *ts = dev_get_drvdata(&spi->dev); 799331b78edSMichael Hennerich 800331b78edSMichael Hennerich ad7877_disable(ts); 801331b78edSMichael Hennerich 802331b78edSMichael Hennerich return 0; 803331b78edSMichael Hennerich } 804331b78edSMichael Hennerich 805331b78edSMichael Hennerich static int ad7877_resume(struct spi_device *spi) 806331b78edSMichael Hennerich { 807331b78edSMichael Hennerich struct ad7877 *ts = dev_get_drvdata(&spi->dev); 808331b78edSMichael Hennerich 809331b78edSMichael Hennerich ad7877_enable(ts); 810331b78edSMichael Hennerich 811331b78edSMichael Hennerich return 0; 812331b78edSMichael Hennerich } 813331b78edSMichael Hennerich #else 814331b78edSMichael Hennerich #define ad7877_suspend NULL 815331b78edSMichael Hennerich #define ad7877_resume NULL 816331b78edSMichael Hennerich #endif 817331b78edSMichael Hennerich 818331b78edSMichael Hennerich static struct spi_driver ad7877_driver = { 819331b78edSMichael Hennerich .driver = { 820331b78edSMichael Hennerich .name = "ad7877", 821331b78edSMichael Hennerich .bus = &spi_bus_type, 822331b78edSMichael Hennerich .owner = THIS_MODULE, 823331b78edSMichael Hennerich }, 824331b78edSMichael Hennerich .probe = ad7877_probe, 825331b78edSMichael Hennerich .remove = __devexit_p(ad7877_remove), 826331b78edSMichael Hennerich .suspend = ad7877_suspend, 827331b78edSMichael Hennerich .resume = ad7877_resume, 828331b78edSMichael Hennerich }; 829331b78edSMichael Hennerich 830331b78edSMichael Hennerich static int __init ad7877_init(void) 831331b78edSMichael Hennerich { 832331b78edSMichael Hennerich return spi_register_driver(&ad7877_driver); 833331b78edSMichael Hennerich } 834331b78edSMichael Hennerich module_init(ad7877_init); 835331b78edSMichael Hennerich 836331b78edSMichael Hennerich static void __exit ad7877_exit(void) 837331b78edSMichael Hennerich { 838331b78edSMichael Hennerich spi_unregister_driver(&ad7877_driver); 839331b78edSMichael Hennerich } 840331b78edSMichael Hennerich module_exit(ad7877_exit); 841331b78edSMichael Hennerich 842331b78edSMichael Hennerich MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); 843331b78edSMichael Hennerich MODULE_DESCRIPTION("AD7877 touchscreen Driver"); 844331b78edSMichael Hennerich MODULE_LICENSE("GPL"); 845*e0626e38SAnton Vorontsov MODULE_ALIAS("spi:ad7877"); 846