Path: blob/master/drivers/input/touchscreen/tps6507x-ts.c
15111 views
/*1* drivers/input/touchscreen/tps6507x_ts.c2*3* Touchscreen driver for the tps6507x chip.4*5* Copyright (c) 2009 RidgeRun ([email protected])6*7* Credits:8*9* Using code from tsc2007, MtekVision Co., Ltd.10*11* For licencing details see kernel-base/COPYING12*13* TPS65070, TPS65073, TPS650731, and TPS650732 support14* 10 bit touch screen interface.15*/1617#include <linux/module.h>18#include <linux/workqueue.h>19#include <linux/slab.h>20#include <linux/input.h>21#include <linux/platform_device.h>22#include <linux/mfd/tps6507x.h>23#include <linux/input/tps6507x-ts.h>24#include <linux/delay.h>2526#define TSC_DEFAULT_POLL_PERIOD 30 /* ms */27#define TPS_DEFAULT_MIN_PRESSURE 0x3028#define MAX_10BIT ((1 << 10) - 1)2930#define TPS6507X_ADCONFIG_CONVERT_TS (TPS6507X_ADCONFIG_AD_ENABLE | \31TPS6507X_ADCONFIG_START_CONVERSION | \32TPS6507X_ADCONFIG_INPUT_REAL_TSC)33#define TPS6507X_ADCONFIG_POWER_DOWN_TS (TPS6507X_ADCONFIG_INPUT_REAL_TSC)3435struct ts_event {36u16 x;37u16 y;38u16 pressure;39};4041struct tps6507x_ts {42struct input_dev *input_dev;43struct device *dev;44char phys[32];45struct delayed_work work;46unsigned polling; /* polling is active */47struct ts_event tc;48struct tps6507x_dev *mfd;49u16 model;50unsigned pendown;51int irq;52void (*clear_penirq)(void);53unsigned long poll_period; /* ms */54u16 min_pressure;55int vref; /* non-zero to leave vref on */56};5758static int tps6507x_read_u8(struct tps6507x_ts *tsc, u8 reg, u8 *data)59{60int err;6162err = tsc->mfd->read_dev(tsc->mfd, reg, 1, data);6364if (err)65return err;6667return 0;68}6970static int tps6507x_write_u8(struct tps6507x_ts *tsc, u8 reg, u8 data)71{72return tsc->mfd->write_dev(tsc->mfd, reg, 1, &data);73}7475static s32 tps6507x_adc_conversion(struct tps6507x_ts *tsc,76u8 tsc_mode, u16 *value)77{78s32 ret;79u8 adc_status;80u8 result;8182/* Route input signal to A/D converter */8384ret = tps6507x_write_u8(tsc, TPS6507X_REG_TSCMODE, tsc_mode);85if (ret) {86dev_err(tsc->dev, "TSC mode read failed\n");87goto err;88}8990/* Start A/D conversion */9192ret = tps6507x_write_u8(tsc, TPS6507X_REG_ADCONFIG,93TPS6507X_ADCONFIG_CONVERT_TS);94if (ret) {95dev_err(tsc->dev, "ADC config write failed\n");96return ret;97}9899do {100ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADCONFIG,101&adc_status);102if (ret) {103dev_err(tsc->dev, "ADC config read failed\n");104goto err;105}106} while (adc_status & TPS6507X_ADCONFIG_START_CONVERSION);107108ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADRESULT_2, &result);109if (ret) {110dev_err(tsc->dev, "ADC result 2 read failed\n");111goto err;112}113114*value = (result & TPS6507X_REG_ADRESULT_2_MASK) << 8;115116ret = tps6507x_read_u8(tsc, TPS6507X_REG_ADRESULT_1, &result);117if (ret) {118dev_err(tsc->dev, "ADC result 1 read failed\n");119goto err;120}121122*value |= result;123124dev_dbg(tsc->dev, "TSC channel %d = 0x%X\n", tsc_mode, *value);125126err:127return ret;128}129130/* Need to call tps6507x_adc_standby() after using A/D converter for the131* touch screen interrupt to work properly.132*/133134static s32 tps6507x_adc_standby(struct tps6507x_ts *tsc)135{136s32 ret;137s32 loops = 0;138u8 val;139140ret = tps6507x_write_u8(tsc, TPS6507X_REG_ADCONFIG,141TPS6507X_ADCONFIG_INPUT_TSC);142if (ret)143return ret;144145ret = tps6507x_write_u8(tsc, TPS6507X_REG_TSCMODE,146TPS6507X_TSCMODE_STANDBY);147if (ret)148return ret;149150ret = tps6507x_read_u8(tsc, TPS6507X_REG_INT, &val);151if (ret)152return ret;153154while (val & TPS6507X_REG_TSC_INT) {155mdelay(10);156ret = tps6507x_read_u8(tsc, TPS6507X_REG_INT, &val);157if (ret)158return ret;159loops++;160}161162return ret;163}164165static void tps6507x_ts_handler(struct work_struct *work)166{167struct tps6507x_ts *tsc = container_of(work,168struct tps6507x_ts, work.work);169struct input_dev *input_dev = tsc->input_dev;170int pendown;171int schd;172int poll = 0;173s32 ret;174175ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_PRESSURE,176&tsc->tc.pressure);177if (ret)178goto done;179180pendown = tsc->tc.pressure > tsc->min_pressure;181182if (unlikely(!pendown && tsc->pendown)) {183dev_dbg(tsc->dev, "UP\n");184input_report_key(input_dev, BTN_TOUCH, 0);185input_report_abs(input_dev, ABS_PRESSURE, 0);186input_sync(input_dev);187tsc->pendown = 0;188}189190if (pendown) {191192if (!tsc->pendown) {193dev_dbg(tsc->dev, "DOWN\n");194input_report_key(input_dev, BTN_TOUCH, 1);195} else196dev_dbg(tsc->dev, "still down\n");197198ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_X_POSITION,199&tsc->tc.x);200if (ret)201goto done;202203ret = tps6507x_adc_conversion(tsc, TPS6507X_TSCMODE_Y_POSITION,204&tsc->tc.y);205if (ret)206goto done;207208input_report_abs(input_dev, ABS_X, tsc->tc.x);209input_report_abs(input_dev, ABS_Y, tsc->tc.y);210input_report_abs(input_dev, ABS_PRESSURE, tsc->tc.pressure);211input_sync(input_dev);212tsc->pendown = 1;213poll = 1;214}215216done:217/* always poll if not using interrupts */218poll = 1;219220if (poll) {221schd = schedule_delayed_work(&tsc->work,222msecs_to_jiffies(tsc->poll_period));223if (schd)224tsc->polling = 1;225else {226tsc->polling = 0;227dev_err(tsc->dev, "re-schedule failed");228}229} else230tsc->polling = 0;231232ret = tps6507x_adc_standby(tsc);233}234235static int tps6507x_ts_probe(struct platform_device *pdev)236{237int error;238struct tps6507x_ts *tsc;239struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent);240struct touchscreen_init_data *init_data;241struct input_dev *input_dev;242struct tps6507x_board *tps_board;243int schd;244245/**246* tps_board points to pmic related constants247* coming from the board-evm file.248*/249250tps_board = (struct tps6507x_board *)tps6507x_dev->dev->platform_data;251252if (!tps_board) {253dev_err(tps6507x_dev->dev,254"Could not find tps6507x platform data\n");255return -EIO;256}257258/**259* init_data points to array of regulator_init structures260* coming from the board-evm file.261*/262263init_data = tps_board->tps6507x_ts_init_data;264265tsc = kzalloc(sizeof(struct tps6507x_ts), GFP_KERNEL);266if (!tsc) {267dev_err(tps6507x_dev->dev, "failed to allocate driver data\n");268error = -ENOMEM;269goto err0;270}271272tps6507x_dev->ts = tsc;273tsc->mfd = tps6507x_dev;274tsc->dev = tps6507x_dev->dev;275input_dev = input_allocate_device();276if (!input_dev) {277dev_err(tsc->dev, "Failed to allocate input device.\n");278error = -ENOMEM;279goto err1;280}281282input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);283input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);284285input_set_abs_params(input_dev, ABS_X, 0, MAX_10BIT, 0, 0);286input_set_abs_params(input_dev, ABS_Y, 0, MAX_10BIT, 0, 0);287input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_10BIT, 0, 0);288289input_dev->name = "TPS6507x Touchscreen";290input_dev->id.bustype = BUS_I2C;291input_dev->dev.parent = tsc->dev;292293snprintf(tsc->phys, sizeof(tsc->phys),294"%s/input0", dev_name(tsc->dev));295input_dev->phys = tsc->phys;296297dev_dbg(tsc->dev, "device: %s\n", input_dev->phys);298299input_set_drvdata(input_dev, tsc);300301tsc->input_dev = input_dev;302303INIT_DELAYED_WORK(&tsc->work, tps6507x_ts_handler);304305if (init_data) {306tsc->poll_period = init_data->poll_period;307tsc->vref = init_data->vref;308tsc->min_pressure = init_data->min_pressure;309input_dev->id.vendor = init_data->vendor;310input_dev->id.product = init_data->product;311input_dev->id.version = init_data->version;312} else {313tsc->poll_period = TSC_DEFAULT_POLL_PERIOD;314tsc->min_pressure = TPS_DEFAULT_MIN_PRESSURE;315}316317error = tps6507x_adc_standby(tsc);318if (error)319goto err2;320321error = input_register_device(input_dev);322if (error)323goto err2;324325schd = schedule_delayed_work(&tsc->work,326msecs_to_jiffies(tsc->poll_period));327328if (schd)329tsc->polling = 1;330else {331tsc->polling = 0;332dev_err(tsc->dev, "schedule failed");333goto err2;334}335platform_set_drvdata(pdev, tps6507x_dev);336337return 0;338339err2:340cancel_delayed_work_sync(&tsc->work);341input_free_device(input_dev);342err1:343kfree(tsc);344tps6507x_dev->ts = NULL;345err0:346return error;347}348349static int __devexit tps6507x_ts_remove(struct platform_device *pdev)350{351struct tps6507x_dev *tps6507x_dev = platform_get_drvdata(pdev);352struct tps6507x_ts *tsc = tps6507x_dev->ts;353struct input_dev *input_dev = tsc->input_dev;354355cancel_delayed_work_sync(&tsc->work);356357input_unregister_device(input_dev);358359tps6507x_dev->ts = NULL;360kfree(tsc);361362return 0;363}364365static struct platform_driver tps6507x_ts_driver = {366.driver = {367.name = "tps6507x-ts",368.owner = THIS_MODULE,369},370.probe = tps6507x_ts_probe,371.remove = __devexit_p(tps6507x_ts_remove),372};373374static int __init tps6507x_ts_init(void)375{376return platform_driver_register(&tps6507x_ts_driver);377}378module_init(tps6507x_ts_init);379380static void __exit tps6507x_ts_exit(void)381{382platform_driver_unregister(&tps6507x_ts_driver);383}384module_exit(tps6507x_ts_exit);385386MODULE_AUTHOR("Todd Fischer <[email protected]>");387MODULE_DESCRIPTION("TPS6507x - TouchScreen driver");388MODULE_LICENSE("GPL v2");389MODULE_ALIAS("platform:tps6507x-tsc");390391392