Path: blob/master/drivers/input/touchscreen/ads7846.c
15111 views
/*1* ADS7846 based touchscreen and sensor driver2*3* Copyright (c) 2005 David Brownell4* Copyright (c) 2006 Nokia Corporation5* Various changes: Imre Deak <[email protected]>6*7* Using code from:8* - corgi_ts.c9* Copyright (C) 2004-2005 Richard Purdie10* - omap_ts.[hc], ads7846.h, ts_osk.c11* Copyright (C) 2002 MontaVista Software12* Copyright (C) 2004 Texas Instruments13* Copyright (C) 2005 Dirk Behme14*15* This program is free software; you can redistribute it and/or modify16* it under the terms of the GNU General Public License version 2 as17* published by the Free Software Foundation.18*/19#include <linux/types.h>20#include <linux/hwmon.h>21#include <linux/init.h>22#include <linux/err.h>23#include <linux/sched.h>24#include <linux/delay.h>25#include <linux/input.h>26#include <linux/interrupt.h>27#include <linux/slab.h>28#include <linux/pm.h>29#include <linux/gpio.h>30#include <linux/spi/spi.h>31#include <linux/spi/ads7846.h>32#include <linux/regulator/consumer.h>33#include <asm/irq.h>3435/*36* This code has been heavily tested on a Nokia 770, and lightly37* tested on other ads7846 devices (OSK/Mistral, Lubbock, Spitz).38* TSC2046 is just newer ads7846 silicon.39* Support for ads7843 tested on Atmel at91sam926x-EK.40* Support for ads7845 has only been stubbed in.41* Support for Analog Devices AD7873 and AD7843 tested.42*43* IRQ handling needs a workaround because of a shortcoming in handling44* edge triggered IRQs on some platforms like the OMAP1/2. These45* platforms don't handle the ARM lazy IRQ disabling properly, thus we46* have to maintain our own SW IRQ disabled status. This should be47* removed as soon as the affected platform's IRQ handling is fixed.48*49* App note sbaa036 talks in more detail about accurate sampling...50* that ought to help in situations like LCDs inducing noise (which51* can also be helped by using synch signals) and more generally.52* This driver tries to utilize the measures described in the app53* note. The strength of filtering can be set in the board-* specific54* files.55*/5657#define TS_POLL_DELAY 1 /* ms delay before the first sample */58#define TS_POLL_PERIOD 5 /* ms delay between samples */5960/* this driver doesn't aim at the peak continuous sample rate */61#define SAMPLE_BITS (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)6263struct ts_event {64/*65* For portability, we can't read 12 bit values using SPI (which66* would make the controller deliver them as native byte order u1667* with msbs zeroed). Instead, we read them as two 8-bit values,68* *** WHICH NEED BYTESWAPPING *** and range adjustment.69*/70u16 x;71u16 y;72u16 z1, z2;73bool ignore;74u8 x_buf[3];75u8 y_buf[3];76};7778/*79* We allocate this separately to avoid cache line sharing issues when80* driver is used with DMA-based SPI controllers (like atmel_spi) on81* systems where main memory is not DMA-coherent (most non-x86 boards).82*/83struct ads7846_packet {84u8 read_x, read_y, read_z1, read_z2, pwrdown;85u16 dummy; /* for the pwrdown read */86struct ts_event tc;87/* for ads7845 with mpc5121 psc spi we use 3-byte buffers */88u8 read_x_cmd[3], read_y_cmd[3], pwrdown_cmd[3];89};9091struct ads7846 {92struct input_dev *input;93char phys[32];94char name[32];9596struct spi_device *spi;97struct regulator *reg;9899#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)100struct attribute_group *attr_group;101struct device *hwmon;102#endif103104u16 model;105u16 vref_mv;106u16 vref_delay_usecs;107u16 x_plate_ohms;108u16 pressure_max;109110bool swap_xy;111bool use_internal;112113struct ads7846_packet *packet;114115struct spi_transfer xfer[18];116struct spi_message msg[5];117int msg_count;118wait_queue_head_t wait;119120bool pendown;121122int read_cnt;123int read_rep;124int last_read;125126u16 debounce_max;127u16 debounce_tol;128u16 debounce_rep;129130u16 penirq_recheck_delay_usecs;131132struct mutex lock;133bool stopped; /* P: lock */134bool disabled; /* P: lock */135bool suspended; /* P: lock */136137int (*filter)(void *data, int data_idx, int *val);138void *filter_data;139void (*filter_cleanup)(void *data);140int (*get_pendown_state)(void);141int gpio_pendown;142143void (*wait_for_sync)(void);144};145146/* leave chip selected when we're done, for quicker re-select? */147#if 0148#define CS_CHANGE(xfer) ((xfer).cs_change = 1)149#else150#define CS_CHANGE(xfer) ((xfer).cs_change = 0)151#endif152153/*--------------------------------------------------------------------------*/154155/* The ADS7846 has touchscreen and other sensors.156* Earlier ads784x chips are somewhat compatible.157*/158#define ADS_START (1 << 7)159#define ADS_A2A1A0_d_y (1 << 4) /* differential */160#define ADS_A2A1A0_d_z1 (3 << 4) /* differential */161#define ADS_A2A1A0_d_z2 (4 << 4) /* differential */162#define ADS_A2A1A0_d_x (5 << 4) /* differential */163#define ADS_A2A1A0_temp0 (0 << 4) /* non-differential */164#define ADS_A2A1A0_vbatt (2 << 4) /* non-differential */165#define ADS_A2A1A0_vaux (6 << 4) /* non-differential */166#define ADS_A2A1A0_temp1 (7 << 4) /* non-differential */167#define ADS_8_BIT (1 << 3)168#define ADS_12_BIT (0 << 3)169#define ADS_SER (1 << 2) /* non-differential */170#define ADS_DFR (0 << 2) /* differential */171#define ADS_PD10_PDOWN (0 << 0) /* low power mode + penirq */172#define ADS_PD10_ADC_ON (1 << 0) /* ADC on */173#define ADS_PD10_REF_ON (2 << 0) /* vREF on + penirq */174#define ADS_PD10_ALL_ON (3 << 0) /* ADC + vREF on */175176#define MAX_12BIT ((1<<12)-1)177178/* leave ADC powered up (disables penirq) between differential samples */179#define READ_12BIT_DFR(x, adc, vref) (ADS_START | ADS_A2A1A0_d_ ## x \180| ADS_12_BIT | ADS_DFR | \181(adc ? ADS_PD10_ADC_ON : 0) | (vref ? ADS_PD10_REF_ON : 0))182183#define READ_Y(vref) (READ_12BIT_DFR(y, 1, vref))184#define READ_Z1(vref) (READ_12BIT_DFR(z1, 1, vref))185#define READ_Z2(vref) (READ_12BIT_DFR(z2, 1, vref))186187#define READ_X(vref) (READ_12BIT_DFR(x, 1, vref))188#define PWRDOWN (READ_12BIT_DFR(y, 0, 0)) /* LAST */189190/* single-ended samples need to first power up reference voltage;191* we leave both ADC and VREF powered192*/193#define READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \194| ADS_12_BIT | ADS_SER)195196#define REF_ON (READ_12BIT_DFR(x, 1, 1))197#define REF_OFF (READ_12BIT_DFR(y, 0, 0))198199/* Must be called with ts->lock held */200static void ads7846_stop(struct ads7846 *ts)201{202if (!ts->disabled && !ts->suspended) {203/* Signal IRQ thread to stop polling and disable the handler. */204ts->stopped = true;205mb();206wake_up(&ts->wait);207disable_irq(ts->spi->irq);208}209}210211/* Must be called with ts->lock held */212static void ads7846_restart(struct ads7846 *ts)213{214if (!ts->disabled && !ts->suspended) {215/* Tell IRQ thread that it may poll the device. */216ts->stopped = false;217mb();218enable_irq(ts->spi->irq);219}220}221222/* Must be called with ts->lock held */223static void __ads7846_disable(struct ads7846 *ts)224{225ads7846_stop(ts);226regulator_disable(ts->reg);227228/*229* We know the chip's in low power mode since we always230* leave it that way after every request231*/232}233234/* Must be called with ts->lock held */235static void __ads7846_enable(struct ads7846 *ts)236{237regulator_enable(ts->reg);238ads7846_restart(ts);239}240241static void ads7846_disable(struct ads7846 *ts)242{243mutex_lock(&ts->lock);244245if (!ts->disabled) {246247if (!ts->suspended)248__ads7846_disable(ts);249250ts->disabled = true;251}252253mutex_unlock(&ts->lock);254}255256static void ads7846_enable(struct ads7846 *ts)257{258mutex_lock(&ts->lock);259260if (ts->disabled) {261262ts->disabled = false;263264if (!ts->suspended)265__ads7846_enable(ts);266}267268mutex_unlock(&ts->lock);269}270271/*--------------------------------------------------------------------------*/272273/*274* Non-touchscreen sensors only use single-ended conversions.275* The range is GND..vREF. The ads7843 and ads7835 must use external vREF;276* ads7846 lets that pin be unconnected, to use internal vREF.277*/278279struct ser_req {280u8 ref_on;281u8 command;282u8 ref_off;283u16 scratch;284struct spi_message msg;285struct spi_transfer xfer[6];286/*287* DMA (thus cache coherency maintenance) requires the288* transfer buffers to live in their own cache lines.289*/290__be16 sample ____cacheline_aligned;291};292293struct ads7845_ser_req {294u8 command[3];295struct spi_message msg;296struct spi_transfer xfer[2];297/*298* DMA (thus cache coherency maintenance) requires the299* transfer buffers to live in their own cache lines.300*/301u8 sample[3] ____cacheline_aligned;302};303304static int ads7846_read12_ser(struct device *dev, unsigned command)305{306struct spi_device *spi = to_spi_device(dev);307struct ads7846 *ts = dev_get_drvdata(dev);308struct ser_req *req;309int status;310311req = kzalloc(sizeof *req, GFP_KERNEL);312if (!req)313return -ENOMEM;314315spi_message_init(&req->msg);316317/* maybe turn on internal vREF, and let it settle */318if (ts->use_internal) {319req->ref_on = REF_ON;320req->xfer[0].tx_buf = &req->ref_on;321req->xfer[0].len = 1;322spi_message_add_tail(&req->xfer[0], &req->msg);323324req->xfer[1].rx_buf = &req->scratch;325req->xfer[1].len = 2;326327/* for 1uF, settle for 800 usec; no cap, 100 usec. */328req->xfer[1].delay_usecs = ts->vref_delay_usecs;329spi_message_add_tail(&req->xfer[1], &req->msg);330331/* Enable reference voltage */332command |= ADS_PD10_REF_ON;333}334335/* Enable ADC in every case */336command |= ADS_PD10_ADC_ON;337338/* take sample */339req->command = (u8) command;340req->xfer[2].tx_buf = &req->command;341req->xfer[2].len = 1;342spi_message_add_tail(&req->xfer[2], &req->msg);343344req->xfer[3].rx_buf = &req->sample;345req->xfer[3].len = 2;346spi_message_add_tail(&req->xfer[3], &req->msg);347348/* REVISIT: take a few more samples, and compare ... */349350/* converter in low power mode & enable PENIRQ */351req->ref_off = PWRDOWN;352req->xfer[4].tx_buf = &req->ref_off;353req->xfer[4].len = 1;354spi_message_add_tail(&req->xfer[4], &req->msg);355356req->xfer[5].rx_buf = &req->scratch;357req->xfer[5].len = 2;358CS_CHANGE(req->xfer[5]);359spi_message_add_tail(&req->xfer[5], &req->msg);360361mutex_lock(&ts->lock);362ads7846_stop(ts);363status = spi_sync(spi, &req->msg);364ads7846_restart(ts);365mutex_unlock(&ts->lock);366367if (status == 0) {368/* on-wire is a must-ignore bit, a BE12 value, then padding */369status = be16_to_cpu(req->sample);370status = status >> 3;371status &= 0x0fff;372}373374kfree(req);375return status;376}377378static int ads7845_read12_ser(struct device *dev, unsigned command)379{380struct spi_device *spi = to_spi_device(dev);381struct ads7846 *ts = dev_get_drvdata(dev);382struct ads7845_ser_req *req;383int status;384385req = kzalloc(sizeof *req, GFP_KERNEL);386if (!req)387return -ENOMEM;388389spi_message_init(&req->msg);390391req->command[0] = (u8) command;392req->xfer[0].tx_buf = req->command;393req->xfer[0].rx_buf = req->sample;394req->xfer[0].len = 3;395spi_message_add_tail(&req->xfer[0], &req->msg);396397mutex_lock(&ts->lock);398ads7846_stop(ts);399status = spi_sync(spi, &req->msg);400ads7846_restart(ts);401mutex_unlock(&ts->lock);402403if (status == 0) {404/* BE12 value, then padding */405status = be16_to_cpu(*((u16 *)&req->sample[1]));406status = status >> 3;407status &= 0x0fff;408}409410kfree(req);411return status;412}413414#if defined(CONFIG_HWMON) || defined(CONFIG_HWMON_MODULE)415416#define SHOW(name, var, adjust) static ssize_t \417name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \418{ \419struct ads7846 *ts = dev_get_drvdata(dev); \420ssize_t v = ads7846_read12_ser(dev, \421READ_12BIT_SER(var)); \422if (v < 0) \423return v; \424return sprintf(buf, "%u\n", adjust(ts, v)); \425} \426static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);427428429/* Sysfs conventions report temperatures in millidegrees Celsius.430* ADS7846 could use the low-accuracy two-sample scheme, but can't do the high431* accuracy scheme without calibration data. For now we won't try either;432* userspace sees raw sensor values, and must scale/calibrate appropriately.433*/434static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)435{436return v;437}438439SHOW(temp0, temp0, null_adjust) /* temp1_input */440SHOW(temp1, temp1, null_adjust) /* temp2_input */441442443/* sysfs conventions report voltages in millivolts. We can convert voltages444* if we know vREF. userspace may need to scale vAUX to match the board's445* external resistors; we assume that vBATT only uses the internal ones.446*/447static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)448{449unsigned retval = v;450451/* external resistors may scale vAUX into 0..vREF */452retval *= ts->vref_mv;453retval = retval >> 12;454455return retval;456}457458static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)459{460unsigned retval = vaux_adjust(ts, v);461462/* ads7846 has a resistor ladder to scale this signal down */463if (ts->model == 7846)464retval *= 4;465466return retval;467}468469SHOW(in0_input, vaux, vaux_adjust)470SHOW(in1_input, vbatt, vbatt_adjust)471472static struct attribute *ads7846_attributes[] = {473&dev_attr_temp0.attr,474&dev_attr_temp1.attr,475&dev_attr_in0_input.attr,476&dev_attr_in1_input.attr,477NULL,478};479480static struct attribute_group ads7846_attr_group = {481.attrs = ads7846_attributes,482};483484static struct attribute *ads7843_attributes[] = {485&dev_attr_in0_input.attr,486&dev_attr_in1_input.attr,487NULL,488};489490static struct attribute_group ads7843_attr_group = {491.attrs = ads7843_attributes,492};493494static struct attribute *ads7845_attributes[] = {495&dev_attr_in0_input.attr,496NULL,497};498499static struct attribute_group ads7845_attr_group = {500.attrs = ads7845_attributes,501};502503static int ads784x_hwmon_register(struct spi_device *spi, struct ads7846 *ts)504{505struct device *hwmon;506int err;507508/* hwmon sensors need a reference voltage */509switch (ts->model) {510case 7846:511if (!ts->vref_mv) {512dev_dbg(&spi->dev, "assuming 2.5V internal vREF\n");513ts->vref_mv = 2500;514ts->use_internal = true;515}516break;517case 7845:518case 7843:519if (!ts->vref_mv) {520dev_warn(&spi->dev,521"external vREF for ADS%d not specified\n",522ts->model);523return 0;524}525break;526}527528/* different chips have different sensor groups */529switch (ts->model) {530case 7846:531ts->attr_group = &ads7846_attr_group;532break;533case 7845:534ts->attr_group = &ads7845_attr_group;535break;536case 7843:537ts->attr_group = &ads7843_attr_group;538break;539default:540dev_dbg(&spi->dev, "ADS%d not recognized\n", ts->model);541return 0;542}543544err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);545if (err)546return err;547548hwmon = hwmon_device_register(&spi->dev);549if (IS_ERR(hwmon)) {550sysfs_remove_group(&spi->dev.kobj, ts->attr_group);551return PTR_ERR(hwmon);552}553554ts->hwmon = hwmon;555return 0;556}557558static void ads784x_hwmon_unregister(struct spi_device *spi,559struct ads7846 *ts)560{561if (ts->hwmon) {562sysfs_remove_group(&spi->dev.kobj, ts->attr_group);563hwmon_device_unregister(ts->hwmon);564}565}566567#else568static inline int ads784x_hwmon_register(struct spi_device *spi,569struct ads7846 *ts)570{571return 0;572}573574static inline void ads784x_hwmon_unregister(struct spi_device *spi,575struct ads7846 *ts)576{577}578#endif579580static ssize_t ads7846_pen_down_show(struct device *dev,581struct device_attribute *attr, char *buf)582{583struct ads7846 *ts = dev_get_drvdata(dev);584585return sprintf(buf, "%u\n", ts->pendown);586}587588static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);589590static ssize_t ads7846_disable_show(struct device *dev,591struct device_attribute *attr, char *buf)592{593struct ads7846 *ts = dev_get_drvdata(dev);594595return sprintf(buf, "%u\n", ts->disabled);596}597598static ssize_t ads7846_disable_store(struct device *dev,599struct device_attribute *attr,600const char *buf, size_t count)601{602struct ads7846 *ts = dev_get_drvdata(dev);603unsigned long i;604605if (strict_strtoul(buf, 10, &i))606return -EINVAL;607608if (i)609ads7846_disable(ts);610else611ads7846_enable(ts);612613return count;614}615616static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);617618static struct attribute *ads784x_attributes[] = {619&dev_attr_pen_down.attr,620&dev_attr_disable.attr,621NULL,622};623624static struct attribute_group ads784x_attr_group = {625.attrs = ads784x_attributes,626};627628/*--------------------------------------------------------------------------*/629630static int get_pendown_state(struct ads7846 *ts)631{632if (ts->get_pendown_state)633return ts->get_pendown_state();634635return !gpio_get_value(ts->gpio_pendown);636}637638static void null_wait_for_sync(void)639{640}641642static int ads7846_debounce_filter(void *ads, int data_idx, int *val)643{644struct ads7846 *ts = ads;645646if (!ts->read_cnt || (abs(ts->last_read - *val) > ts->debounce_tol)) {647/* Start over collecting consistent readings. */648ts->read_rep = 0;649/*650* Repeat it, if this was the first read or the read651* wasn't consistent enough.652*/653if (ts->read_cnt < ts->debounce_max) {654ts->last_read = *val;655ts->read_cnt++;656return ADS7846_FILTER_REPEAT;657} else {658/*659* Maximum number of debouncing reached and still660* not enough number of consistent readings. Abort661* the whole sample, repeat it in the next sampling662* period.663*/664ts->read_cnt = 0;665return ADS7846_FILTER_IGNORE;666}667} else {668if (++ts->read_rep > ts->debounce_rep) {669/*670* Got a good reading for this coordinate,671* go for the next one.672*/673ts->read_cnt = 0;674ts->read_rep = 0;675return ADS7846_FILTER_OK;676} else {677/* Read more values that are consistent. */678ts->read_cnt++;679return ADS7846_FILTER_REPEAT;680}681}682}683684static int ads7846_no_filter(void *ads, int data_idx, int *val)685{686return ADS7846_FILTER_OK;687}688689static int ads7846_get_value(struct ads7846 *ts, struct spi_message *m)690{691struct spi_transfer *t =692list_entry(m->transfers.prev, struct spi_transfer, transfer_list);693694if (ts->model == 7845) {695return be16_to_cpup((__be16 *)&(((char*)t->rx_buf)[1])) >> 3;696} else {697/*698* adjust: on-wire is a must-ignore bit, a BE12 value, then699* padding; built from two 8 bit values written msb-first.700*/701return be16_to_cpup((__be16 *)t->rx_buf) >> 3;702}703}704705static void ads7846_update_value(struct spi_message *m, int val)706{707struct spi_transfer *t =708list_entry(m->transfers.prev, struct spi_transfer, transfer_list);709710*(u16 *)t->rx_buf = val;711}712713static void ads7846_read_state(struct ads7846 *ts)714{715struct ads7846_packet *packet = ts->packet;716struct spi_message *m;717int msg_idx = 0;718int val;719int action;720int error;721722while (msg_idx < ts->msg_count) {723724ts->wait_for_sync();725726m = &ts->msg[msg_idx];727error = spi_sync(ts->spi, m);728if (error) {729dev_err(&ts->spi->dev, "spi_async --> %d\n", error);730packet->tc.ignore = true;731return;732}733734/*735* Last message is power down request, no need to convert736* or filter the value.737*/738if (msg_idx < ts->msg_count - 1) {739740val = ads7846_get_value(ts, m);741742action = ts->filter(ts->filter_data, msg_idx, &val);743switch (action) {744case ADS7846_FILTER_REPEAT:745continue;746747case ADS7846_FILTER_IGNORE:748packet->tc.ignore = true;749msg_idx = ts->msg_count - 1;750continue;751752case ADS7846_FILTER_OK:753ads7846_update_value(m, val);754packet->tc.ignore = false;755msg_idx++;756break;757758default:759BUG();760}761} else {762msg_idx++;763}764}765}766767static void ads7846_report_state(struct ads7846 *ts)768{769struct ads7846_packet *packet = ts->packet;770unsigned int Rt;771u16 x, y, z1, z2;772773/*774* ads7846_get_value() does in-place conversion (including byte swap)775* from on-the-wire format as part of debouncing to get stable776* readings.777*/778if (ts->model == 7845) {779x = *(u16 *)packet->tc.x_buf;780y = *(u16 *)packet->tc.y_buf;781z1 = 0;782z2 = 0;783} else {784x = packet->tc.x;785y = packet->tc.y;786z1 = packet->tc.z1;787z2 = packet->tc.z2;788}789790/* range filtering */791if (x == MAX_12BIT)792x = 0;793794if (ts->model == 7843) {795Rt = ts->pressure_max / 2;796} else if (ts->model == 7845) {797if (get_pendown_state(ts))798Rt = ts->pressure_max / 2;799else800Rt = 0;801dev_vdbg(&ts->spi->dev, "x/y: %d/%d, PD %d\n", x, y, Rt);802} else if (likely(x && z1)) {803/* compute touch pressure resistance using equation #2 */804Rt = z2;805Rt -= z1;806Rt *= x;807Rt *= ts->x_plate_ohms;808Rt /= z1;809Rt = (Rt + 2047) >> 12;810} else {811Rt = 0;812}813814/*815* Sample found inconsistent by debouncing or pressure is beyond816* the maximum. Don't report it to user space, repeat at least817* once more the measurement818*/819if (packet->tc.ignore || Rt > ts->pressure_max) {820dev_vdbg(&ts->spi->dev, "ignored %d pressure %d\n",821packet->tc.ignore, Rt);822return;823}824825/*826* Maybe check the pendown state before reporting. This discards827* false readings when the pen is lifted.828*/829if (ts->penirq_recheck_delay_usecs) {830udelay(ts->penirq_recheck_delay_usecs);831if (!get_pendown_state(ts))832Rt = 0;833}834835/*836* NOTE: We can't rely on the pressure to determine the pen down837* state, even this controller has a pressure sensor. The pressure838* value can fluctuate for quite a while after lifting the pen and839* in some cases may not even settle at the expected value.840*841* The only safe way to check for the pen up condition is in the842* timer by reading the pen signal state (it's a GPIO _and_ IRQ).843*/844if (Rt) {845struct input_dev *input = ts->input;846847if (ts->swap_xy)848swap(x, y);849850if (!ts->pendown) {851input_report_key(input, BTN_TOUCH, 1);852ts->pendown = true;853dev_vdbg(&ts->spi->dev, "DOWN\n");854}855856input_report_abs(input, ABS_X, x);857input_report_abs(input, ABS_Y, y);858input_report_abs(input, ABS_PRESSURE, ts->pressure_max - Rt);859860input_sync(input);861dev_vdbg(&ts->spi->dev, "%4d/%4d/%4d\n", x, y, Rt);862}863}864865static irqreturn_t ads7846_hard_irq(int irq, void *handle)866{867struct ads7846 *ts = handle;868869return get_pendown_state(ts) ? IRQ_WAKE_THREAD : IRQ_HANDLED;870}871872873static irqreturn_t ads7846_irq(int irq, void *handle)874{875struct ads7846 *ts = handle;876877/* Start with a small delay before checking pendown state */878msleep(TS_POLL_DELAY);879880while (!ts->stopped && get_pendown_state(ts)) {881882/* pen is down, continue with the measurement */883ads7846_read_state(ts);884885if (!ts->stopped)886ads7846_report_state(ts);887888wait_event_timeout(ts->wait, ts->stopped,889msecs_to_jiffies(TS_POLL_PERIOD));890}891892if (ts->pendown) {893struct input_dev *input = ts->input;894895input_report_key(input, BTN_TOUCH, 0);896input_report_abs(input, ABS_PRESSURE, 0);897input_sync(input);898899ts->pendown = false;900dev_vdbg(&ts->spi->dev, "UP\n");901}902903return IRQ_HANDLED;904}905906#ifdef CONFIG_PM_SLEEP907static int ads7846_suspend(struct device *dev)908{909struct ads7846 *ts = dev_get_drvdata(dev);910911mutex_lock(&ts->lock);912913if (!ts->suspended) {914915if (!ts->disabled)916__ads7846_disable(ts);917918if (device_may_wakeup(&ts->spi->dev))919enable_irq_wake(ts->spi->irq);920921ts->suspended = true;922}923924mutex_unlock(&ts->lock);925926return 0;927}928929static int ads7846_resume(struct device *dev)930{931struct ads7846 *ts = dev_get_drvdata(dev);932933mutex_lock(&ts->lock);934935if (ts->suspended) {936937ts->suspended = false;938939if (device_may_wakeup(&ts->spi->dev))940disable_irq_wake(ts->spi->irq);941942if (!ts->disabled)943__ads7846_enable(ts);944}945946mutex_unlock(&ts->lock);947948return 0;949}950#endif951952static SIMPLE_DEV_PM_OPS(ads7846_pm, ads7846_suspend, ads7846_resume);953954static int __devinit ads7846_setup_pendown(struct spi_device *spi, struct ads7846 *ts)955{956struct ads7846_platform_data *pdata = spi->dev.platform_data;957int err;958959/*960* REVISIT when the irq can be triggered active-low, or if for some961* reason the touchscreen isn't hooked up, we don't need to access962* the pendown state.963*/964965if (pdata->get_pendown_state) {966ts->get_pendown_state = pdata->get_pendown_state;967} else if (gpio_is_valid(pdata->gpio_pendown)) {968969err = gpio_request(pdata->gpio_pendown, "ads7846_pendown");970if (err) {971dev_err(&spi->dev, "failed to request pendown GPIO%d\n",972pdata->gpio_pendown);973return err;974}975err = gpio_direction_input(pdata->gpio_pendown);976if (err) {977dev_err(&spi->dev, "failed to setup pendown GPIO%d\n",978pdata->gpio_pendown);979gpio_free(pdata->gpio_pendown);980return err;981}982983ts->gpio_pendown = pdata->gpio_pendown;984985} else {986dev_err(&spi->dev, "no get_pendown_state nor gpio_pendown?\n");987return -EINVAL;988}989990return 0;991}992993/*994* Set up the transfers to read touchscreen state; this assumes we995* use formula #2 for pressure, not #3.996*/997static void __devinit ads7846_setup_spi_msg(struct ads7846 *ts,998const struct ads7846_platform_data *pdata)999{1000struct spi_message *m = &ts->msg[0];1001struct spi_transfer *x = ts->xfer;1002struct ads7846_packet *packet = ts->packet;1003int vref = pdata->keep_vref_on;10041005if (ts->model == 7873) {1006/*1007* The AD7873 is almost identical to the ADS78461008* keep VREF off during differential/ratiometric1009* conversion modes.1010*/1011ts->model = 7846;1012vref = 0;1013}10141015ts->msg_count = 1;1016spi_message_init(m);1017m->context = ts;10181019if (ts->model == 7845) {1020packet->read_y_cmd[0] = READ_Y(vref);1021packet->read_y_cmd[1] = 0;1022packet->read_y_cmd[2] = 0;1023x->tx_buf = &packet->read_y_cmd[0];1024x->rx_buf = &packet->tc.y_buf[0];1025x->len = 3;1026spi_message_add_tail(x, m);1027} else {1028/* y- still on; turn on only y+ (and ADC) */1029packet->read_y = READ_Y(vref);1030x->tx_buf = &packet->read_y;1031x->len = 1;1032spi_message_add_tail(x, m);10331034x++;1035x->rx_buf = &packet->tc.y;1036x->len = 2;1037spi_message_add_tail(x, m);1038}10391040/*1041* The first sample after switching drivers can be low quality;1042* optionally discard it, using a second one after the signals1043* have had enough time to stabilize.1044*/1045if (pdata->settle_delay_usecs) {1046x->delay_usecs = pdata->settle_delay_usecs;10471048x++;1049x->tx_buf = &packet->read_y;1050x->len = 1;1051spi_message_add_tail(x, m);10521053x++;1054x->rx_buf = &packet->tc.y;1055x->len = 2;1056spi_message_add_tail(x, m);1057}10581059ts->msg_count++;1060m++;1061spi_message_init(m);1062m->context = ts;10631064if (ts->model == 7845) {1065x++;1066packet->read_x_cmd[0] = READ_X(vref);1067packet->read_x_cmd[1] = 0;1068packet->read_x_cmd[2] = 0;1069x->tx_buf = &packet->read_x_cmd[0];1070x->rx_buf = &packet->tc.x_buf[0];1071x->len = 3;1072spi_message_add_tail(x, m);1073} else {1074/* turn y- off, x+ on, then leave in lowpower */1075x++;1076packet->read_x = READ_X(vref);1077x->tx_buf = &packet->read_x;1078x->len = 1;1079spi_message_add_tail(x, m);10801081x++;1082x->rx_buf = &packet->tc.x;1083x->len = 2;1084spi_message_add_tail(x, m);1085}10861087/* ... maybe discard first sample ... */1088if (pdata->settle_delay_usecs) {1089x->delay_usecs = pdata->settle_delay_usecs;10901091x++;1092x->tx_buf = &packet->read_x;1093x->len = 1;1094spi_message_add_tail(x, m);10951096x++;1097x->rx_buf = &packet->tc.x;1098x->len = 2;1099spi_message_add_tail(x, m);1100}11011102/* turn y+ off, x- on; we'll use formula #2 */1103if (ts->model == 7846) {1104ts->msg_count++;1105m++;1106spi_message_init(m);1107m->context = ts;11081109x++;1110packet->read_z1 = READ_Z1(vref);1111x->tx_buf = &packet->read_z1;1112x->len = 1;1113spi_message_add_tail(x, m);11141115x++;1116x->rx_buf = &packet->tc.z1;1117x->len = 2;1118spi_message_add_tail(x, m);11191120/* ... maybe discard first sample ... */1121if (pdata->settle_delay_usecs) {1122x->delay_usecs = pdata->settle_delay_usecs;11231124x++;1125x->tx_buf = &packet->read_z1;1126x->len = 1;1127spi_message_add_tail(x, m);11281129x++;1130x->rx_buf = &packet->tc.z1;1131x->len = 2;1132spi_message_add_tail(x, m);1133}11341135ts->msg_count++;1136m++;1137spi_message_init(m);1138m->context = ts;11391140x++;1141packet->read_z2 = READ_Z2(vref);1142x->tx_buf = &packet->read_z2;1143x->len = 1;1144spi_message_add_tail(x, m);11451146x++;1147x->rx_buf = &packet->tc.z2;1148x->len = 2;1149spi_message_add_tail(x, m);11501151/* ... maybe discard first sample ... */1152if (pdata->settle_delay_usecs) {1153x->delay_usecs = pdata->settle_delay_usecs;11541155x++;1156x->tx_buf = &packet->read_z2;1157x->len = 1;1158spi_message_add_tail(x, m);11591160x++;1161x->rx_buf = &packet->tc.z2;1162x->len = 2;1163spi_message_add_tail(x, m);1164}1165}11661167/* power down */1168ts->msg_count++;1169m++;1170spi_message_init(m);1171m->context = ts;11721173if (ts->model == 7845) {1174x++;1175packet->pwrdown_cmd[0] = PWRDOWN;1176packet->pwrdown_cmd[1] = 0;1177packet->pwrdown_cmd[2] = 0;1178x->tx_buf = &packet->pwrdown_cmd[0];1179x->len = 3;1180} else {1181x++;1182packet->pwrdown = PWRDOWN;1183x->tx_buf = &packet->pwrdown;1184x->len = 1;1185spi_message_add_tail(x, m);11861187x++;1188x->rx_buf = &packet->dummy;1189x->len = 2;1190}11911192CS_CHANGE(*x);1193spi_message_add_tail(x, m);1194}11951196static int __devinit ads7846_probe(struct spi_device *spi)1197{1198struct ads7846 *ts;1199struct ads7846_packet *packet;1200struct input_dev *input_dev;1201struct ads7846_platform_data *pdata = spi->dev.platform_data;1202unsigned long irq_flags;1203int err;12041205if (!spi->irq) {1206dev_dbg(&spi->dev, "no IRQ?\n");1207return -ENODEV;1208}12091210if (!pdata) {1211dev_dbg(&spi->dev, "no platform data?\n");1212return -ENODEV;1213}12141215/* don't exceed max specified sample rate */1216if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {1217dev_dbg(&spi->dev, "f(sample) %d KHz?\n",1218(spi->max_speed_hz/SAMPLE_BITS)/1000);1219return -EINVAL;1220}12211222/* We'd set TX word size 8 bits and RX word size to 13 bits ... except1223* that even if the hardware can do that, the SPI controller driver1224* may not. So we stick to very-portable 8 bit words, both RX and TX.1225*/1226spi->bits_per_word = 8;1227spi->mode = SPI_MODE_0;1228err = spi_setup(spi);1229if (err < 0)1230return err;12311232ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);1233packet = kzalloc(sizeof(struct ads7846_packet), GFP_KERNEL);1234input_dev = input_allocate_device();1235if (!ts || !packet || !input_dev) {1236err = -ENOMEM;1237goto err_free_mem;1238}12391240dev_set_drvdata(&spi->dev, ts);12411242ts->packet = packet;1243ts->spi = spi;1244ts->input = input_dev;1245ts->vref_mv = pdata->vref_mv;1246ts->swap_xy = pdata->swap_xy;12471248mutex_init(&ts->lock);1249init_waitqueue_head(&ts->wait);12501251ts->model = pdata->model ? : 7846;1252ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;1253ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;1254ts->pressure_max = pdata->pressure_max ? : ~0;12551256if (pdata->filter != NULL) {1257if (pdata->filter_init != NULL) {1258err = pdata->filter_init(pdata, &ts->filter_data);1259if (err < 0)1260goto err_free_mem;1261}1262ts->filter = pdata->filter;1263ts->filter_cleanup = pdata->filter_cleanup;1264} else if (pdata->debounce_max) {1265ts->debounce_max = pdata->debounce_max;1266if (ts->debounce_max < 2)1267ts->debounce_max = 2;1268ts->debounce_tol = pdata->debounce_tol;1269ts->debounce_rep = pdata->debounce_rep;1270ts->filter = ads7846_debounce_filter;1271ts->filter_data = ts;1272} else {1273ts->filter = ads7846_no_filter;1274}12751276err = ads7846_setup_pendown(spi, ts);1277if (err)1278goto err_cleanup_filter;12791280if (pdata->penirq_recheck_delay_usecs)1281ts->penirq_recheck_delay_usecs =1282pdata->penirq_recheck_delay_usecs;12831284ts->wait_for_sync = pdata->wait_for_sync ? : null_wait_for_sync;12851286snprintf(ts->phys, sizeof(ts->phys), "%s/input0", dev_name(&spi->dev));1287snprintf(ts->name, sizeof(ts->name), "ADS%d Touchscreen", ts->model);12881289input_dev->name = ts->name;1290input_dev->phys = ts->phys;1291input_dev->dev.parent = &spi->dev;12921293input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);1294input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);1295input_set_abs_params(input_dev, ABS_X,1296pdata->x_min ? : 0,1297pdata->x_max ? : MAX_12BIT,12980, 0);1299input_set_abs_params(input_dev, ABS_Y,1300pdata->y_min ? : 0,1301pdata->y_max ? : MAX_12BIT,13020, 0);1303input_set_abs_params(input_dev, ABS_PRESSURE,1304pdata->pressure_min, pdata->pressure_max, 0, 0);13051306ads7846_setup_spi_msg(ts, pdata);13071308ts->reg = regulator_get(&spi->dev, "vcc");1309if (IS_ERR(ts->reg)) {1310err = PTR_ERR(ts->reg);1311dev_err(&spi->dev, "unable to get regulator: %d\n", err);1312goto err_free_gpio;1313}13141315err = regulator_enable(ts->reg);1316if (err) {1317dev_err(&spi->dev, "unable to enable regulator: %d\n", err);1318goto err_put_regulator;1319}13201321irq_flags = pdata->irq_flags ? : IRQF_TRIGGER_FALLING;1322irq_flags |= IRQF_ONESHOT;13231324err = request_threaded_irq(spi->irq, ads7846_hard_irq, ads7846_irq,1325irq_flags, spi->dev.driver->name, ts);1326if (err && !pdata->irq_flags) {1327dev_info(&spi->dev,1328"trying pin change workaround on irq %d\n", spi->irq);1329irq_flags |= IRQF_TRIGGER_RISING;1330err = request_threaded_irq(spi->irq,1331ads7846_hard_irq, ads7846_irq,1332irq_flags, spi->dev.driver->name, ts);1333}13341335if (err) {1336dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);1337goto err_disable_regulator;1338}13391340err = ads784x_hwmon_register(spi, ts);1341if (err)1342goto err_free_irq;13431344dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);13451346/*1347* Take a first sample, leaving nPENIRQ active and vREF off; avoid1348* the touchscreen, in case it's not connected.1349*/1350if (ts->model == 7845)1351ads7845_read12_ser(&spi->dev, PWRDOWN);1352else1353(void) ads7846_read12_ser(&spi->dev, READ_12BIT_SER(vaux));13541355err = sysfs_create_group(&spi->dev.kobj, &ads784x_attr_group);1356if (err)1357goto err_remove_hwmon;13581359err = input_register_device(input_dev);1360if (err)1361goto err_remove_attr_group;13621363device_init_wakeup(&spi->dev, pdata->wakeup);13641365return 0;13661367err_remove_attr_group:1368sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);1369err_remove_hwmon:1370ads784x_hwmon_unregister(spi, ts);1371err_free_irq:1372free_irq(spi->irq, ts);1373err_disable_regulator:1374regulator_disable(ts->reg);1375err_put_regulator:1376regulator_put(ts->reg);1377err_free_gpio:1378if (!ts->get_pendown_state)1379gpio_free(ts->gpio_pendown);1380err_cleanup_filter:1381if (ts->filter_cleanup)1382ts->filter_cleanup(ts->filter_data);1383err_free_mem:1384input_free_device(input_dev);1385kfree(packet);1386kfree(ts);1387return err;1388}13891390static int __devexit ads7846_remove(struct spi_device *spi)1391{1392struct ads7846 *ts = dev_get_drvdata(&spi->dev);13931394device_init_wakeup(&spi->dev, false);13951396sysfs_remove_group(&spi->dev.kobj, &ads784x_attr_group);13971398ads7846_disable(ts);1399free_irq(ts->spi->irq, ts);14001401input_unregister_device(ts->input);14021403ads784x_hwmon_unregister(spi, ts);14041405regulator_disable(ts->reg);1406regulator_put(ts->reg);14071408if (!ts->get_pendown_state) {1409/*1410* If we are not using specialized pendown method we must1411* have been relying on gpio we set up ourselves.1412*/1413gpio_free(ts->gpio_pendown);1414}14151416if (ts->filter_cleanup)1417ts->filter_cleanup(ts->filter_data);14181419kfree(ts->packet);1420kfree(ts);14211422dev_dbg(&spi->dev, "unregistered touchscreen\n");14231424return 0;1425}14261427static struct spi_driver ads7846_driver = {1428.driver = {1429.name = "ads7846",1430.bus = &spi_bus_type,1431.owner = THIS_MODULE,1432.pm = &ads7846_pm,1433},1434.probe = ads7846_probe,1435.remove = __devexit_p(ads7846_remove),1436};14371438static int __init ads7846_init(void)1439{1440return spi_register_driver(&ads7846_driver);1441}1442module_init(ads7846_init);14431444static void __exit ads7846_exit(void)1445{1446spi_unregister_driver(&ads7846_driver);1447}1448module_exit(ads7846_exit);14491450MODULE_DESCRIPTION("ADS7846 TouchScreen Driver");1451MODULE_LICENSE("GPL");1452MODULE_ALIAS("spi:ads7846");145314541455