Path: blob/master/drivers/input/touchscreen/s3c2410_ts.c
15111 views
/*1* Samsung S3C24XX touchscreen driver2*3* This program is free software; you can redistribute it and/or modify4* it under the term of the GNU General Public License as published by5* the Free Software Foundation; either version 2 of the License, or6* (at your option) any later version.7*8* This program is distributed in the hope that it will be useful,9* but WITHOUT ANY WARRANTY; without even the implied warranty of10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the11* GNU General Public License for more details.12*13* You should have received a copy of the GNU General Public License14* along with this program; if not, write to the Free Software15* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA16*17* Copyright 2004 Arnaud Patard <[email protected]>18* Copyright 2008 Ben Dooks <[email protected]>19* Copyright 2009 Simtec Electronics <[email protected]>20*21* Additional work by Herbert Pƶtzl <[email protected]> and22* Harald Welte <[email protected]>23*/2425#include <linux/errno.h>26#include <linux/kernel.h>27#include <linux/module.h>28#include <linux/gpio.h>29#include <linux/input.h>30#include <linux/init.h>31#include <linux/delay.h>32#include <linux/interrupt.h>33#include <linux/platform_device.h>34#include <linux/clk.h>35#include <linux/io.h>3637#include <plat/adc.h>38#include <plat/regs-adc.h>39#include <plat/ts.h>4041#define TSC_SLEEP (S3C2410_ADCTSC_PULL_UP_DISABLE | S3C2410_ADCTSC_XY_PST(0))4243#define INT_DOWN (0)44#define INT_UP (1 << 8)4546#define WAIT4INT (S3C2410_ADCTSC_YM_SEN | \47S3C2410_ADCTSC_YP_SEN | \48S3C2410_ADCTSC_XP_SEN | \49S3C2410_ADCTSC_XY_PST(3))5051#define AUTOPST (S3C2410_ADCTSC_YM_SEN | \52S3C2410_ADCTSC_YP_SEN | \53S3C2410_ADCTSC_XP_SEN | \54S3C2410_ADCTSC_AUTO_PST | \55S3C2410_ADCTSC_XY_PST(0))5657#define FEAT_PEN_IRQ (1 << 0) /* HAS ADCCLRINTPNDNUP */5859/* Per-touchscreen data. */6061/**62* struct s3c2410ts - driver touchscreen state.63* @client: The ADC client we registered with the core driver.64* @dev: The device we are bound to.65* @input: The input device we registered with the input subsystem.66* @clock: The clock for the adc.67* @io: Pointer to the IO base.68* @xp: The accumulated X position data.69* @yp: The accumulated Y position data.70* @irq_tc: The interrupt number for pen up/down interrupt71* @count: The number of samples collected.72* @shift: The log2 of the maximum count to read in one go.73* @features: The features supported by the TSADC MOdule.74*/75struct s3c2410ts {76struct s3c_adc_client *client;77struct device *dev;78struct input_dev *input;79struct clk *clock;80void __iomem *io;81unsigned long xp;82unsigned long yp;83int irq_tc;84int count;85int shift;86int features;87};8889static struct s3c2410ts ts;9091/**92* get_down - return the down state of the pen93* @data0: The data read from ADCDAT0 register.94* @data1: The data read from ADCDAT1 register.95*96* Return non-zero if both readings show that the pen is down.97*/98static inline bool get_down(unsigned long data0, unsigned long data1)99{100/* returns true if both data values show stylus down */101return (!(data0 & S3C2410_ADCDAT0_UPDOWN) &&102!(data1 & S3C2410_ADCDAT0_UPDOWN));103}104105static void touch_timer_fire(unsigned long data)106{107unsigned long data0;108unsigned long data1;109bool down;110111data0 = readl(ts.io + S3C2410_ADCDAT0);112data1 = readl(ts.io + S3C2410_ADCDAT1);113114down = get_down(data0, data1);115116if (down) {117if (ts.count == (1 << ts.shift)) {118ts.xp >>= ts.shift;119ts.yp >>= ts.shift;120121dev_dbg(ts.dev, "%s: X=%lu, Y=%lu, count=%d\n",122__func__, ts.xp, ts.yp, ts.count);123124input_report_abs(ts.input, ABS_X, ts.xp);125input_report_abs(ts.input, ABS_Y, ts.yp);126127input_report_key(ts.input, BTN_TOUCH, 1);128input_sync(ts.input);129130ts.xp = 0;131ts.yp = 0;132ts.count = 0;133}134135s3c_adc_start(ts.client, 0, 1 << ts.shift);136} else {137ts.xp = 0;138ts.yp = 0;139ts.count = 0;140141input_report_key(ts.input, BTN_TOUCH, 0);142input_sync(ts.input);143144writel(WAIT4INT | INT_DOWN, ts.io + S3C2410_ADCTSC);145}146}147148static DEFINE_TIMER(touch_timer, touch_timer_fire, 0, 0);149150/**151* stylus_irq - touchscreen stylus event interrupt152* @irq: The interrupt number153* @dev_id: The device ID.154*155* Called when the IRQ_TC is fired for a pen up or down event.156*/157static irqreturn_t stylus_irq(int irq, void *dev_id)158{159unsigned long data0;160unsigned long data1;161bool down;162163data0 = readl(ts.io + S3C2410_ADCDAT0);164data1 = readl(ts.io + S3C2410_ADCDAT1);165166down = get_down(data0, data1);167168/* TODO we should never get an interrupt with down set while169* the timer is running, but maybe we ought to verify that the170* timer isn't running anyways. */171172if (down)173s3c_adc_start(ts.client, 0, 1 << ts.shift);174else175dev_dbg(ts.dev, "%s: count=%d\n", __func__, ts.count);176177if (ts.features & FEAT_PEN_IRQ) {178/* Clear pen down/up interrupt */179writel(0x0, ts.io + S3C64XX_ADCCLRINTPNDNUP);180}181182return IRQ_HANDLED;183}184185/**186* s3c24xx_ts_conversion - ADC conversion callback187* @client: The client that was registered with the ADC core.188* @data0: The reading from ADCDAT0.189* @data1: The reading from ADCDAT1.190* @left: The number of samples left.191*192* Called when a conversion has finished.193*/194static void s3c24xx_ts_conversion(struct s3c_adc_client *client,195unsigned data0, unsigned data1,196unsigned *left)197{198dev_dbg(ts.dev, "%s: %d,%d\n", __func__, data0, data1);199200ts.xp += data0;201ts.yp += data1;202203ts.count++;204205/* From tests, it seems that it is unlikely to get a pen-up206* event during the conversion process which means we can207* ignore any pen-up events with less than the requisite208* count done.209*210* In several thousand conversions, no pen-ups where detected211* before count completed.212*/213}214215/**216* s3c24xx_ts_select - ADC selection callback.217* @client: The client that was registered with the ADC core.218* @select: The reason for select.219*220* Called when the ADC core selects (or deslects) us as a client.221*/222static void s3c24xx_ts_select(struct s3c_adc_client *client, unsigned select)223{224if (select) {225writel(S3C2410_ADCTSC_PULL_UP_DISABLE | AUTOPST,226ts.io + S3C2410_ADCTSC);227} else {228mod_timer(&touch_timer, jiffies+1);229writel(WAIT4INT | INT_UP, ts.io + S3C2410_ADCTSC);230}231}232233/**234* s3c2410ts_probe - device core probe entry point235* @pdev: The device we are being bound to.236*237* Initialise, find and allocate any resources we need to run and then238* register with the ADC and input systems.239*/240static int __devinit s3c2410ts_probe(struct platform_device *pdev)241{242struct s3c2410_ts_mach_info *info;243struct device *dev = &pdev->dev;244struct input_dev *input_dev;245struct resource *res;246int ret = -EINVAL;247248/* Initialise input stuff */249memset(&ts, 0, sizeof(struct s3c2410ts));250251ts.dev = dev;252253info = pdev->dev.platform_data;254if (!info) {255dev_err(dev, "no platform data, cannot attach\n");256return -EINVAL;257}258259dev_dbg(dev, "initialising touchscreen\n");260261ts.clock = clk_get(dev, "adc");262if (IS_ERR(ts.clock)) {263dev_err(dev, "cannot get adc clock source\n");264return -ENOENT;265}266267clk_enable(ts.clock);268dev_dbg(dev, "got and enabled clocks\n");269270ts.irq_tc = ret = platform_get_irq(pdev, 0);271if (ret < 0) {272dev_err(dev, "no resource for interrupt\n");273goto err_clk;274}275276res = platform_get_resource(pdev, IORESOURCE_MEM, 0);277if (!res) {278dev_err(dev, "no resource for registers\n");279ret = -ENOENT;280goto err_clk;281}282283ts.io = ioremap(res->start, resource_size(res));284if (ts.io == NULL) {285dev_err(dev, "cannot map registers\n");286ret = -ENOMEM;287goto err_clk;288}289290/* inititalise the gpio */291if (info->cfg_gpio)292info->cfg_gpio(to_platform_device(ts.dev));293294ts.client = s3c_adc_register(pdev, s3c24xx_ts_select,295s3c24xx_ts_conversion, 1);296if (IS_ERR(ts.client)) {297dev_err(dev, "failed to register adc client\n");298ret = PTR_ERR(ts.client);299goto err_iomap;300}301302/* Initialise registers */303if ((info->delay & 0xffff) > 0)304writel(info->delay & 0xffff, ts.io + S3C2410_ADCDLY);305306writel(WAIT4INT | INT_DOWN, ts.io + S3C2410_ADCTSC);307308input_dev = input_allocate_device();309if (!input_dev) {310dev_err(dev, "Unable to allocate the input device !!\n");311ret = -ENOMEM;312goto err_iomap;313}314315ts.input = input_dev;316ts.input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);317ts.input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);318input_set_abs_params(ts.input, ABS_X, 0, 0x3FF, 0, 0);319input_set_abs_params(ts.input, ABS_Y, 0, 0x3FF, 0, 0);320321ts.input->name = "S3C24XX TouchScreen";322ts.input->id.bustype = BUS_HOST;323ts.input->id.vendor = 0xDEAD;324ts.input->id.product = 0xBEEF;325ts.input->id.version = 0x0102;326327ts.shift = info->oversampling_shift;328ts.features = platform_get_device_id(pdev)->driver_data;329330ret = request_irq(ts.irq_tc, stylus_irq, IRQF_DISABLED,331"s3c2410_ts_pen", ts.input);332if (ret) {333dev_err(dev, "cannot get TC interrupt\n");334goto err_inputdev;335}336337dev_info(dev, "driver attached, registering input device\n");338339/* All went ok, so register to the input system */340ret = input_register_device(ts.input);341if (ret < 0) {342dev_err(dev, "failed to register input device\n");343ret = -EIO;344goto err_tcirq;345}346347return 0;348349err_tcirq:350free_irq(ts.irq_tc, ts.input);351err_inputdev:352input_free_device(ts.input);353err_iomap:354iounmap(ts.io);355err_clk:356del_timer_sync(&touch_timer);357clk_put(ts.clock);358return ret;359}360361/**362* s3c2410ts_remove - device core removal entry point363* @pdev: The device we are being removed from.364*365* Free up our state ready to be removed.366*/367static int __devexit s3c2410ts_remove(struct platform_device *pdev)368{369free_irq(ts.irq_tc, ts.input);370del_timer_sync(&touch_timer);371372clk_disable(ts.clock);373clk_put(ts.clock);374375input_unregister_device(ts.input);376iounmap(ts.io);377378return 0;379}380381#ifdef CONFIG_PM382static int s3c2410ts_suspend(struct device *dev)383{384writel(TSC_SLEEP, ts.io + S3C2410_ADCTSC);385disable_irq(ts.irq_tc);386clk_disable(ts.clock);387388return 0;389}390391static int s3c2410ts_resume(struct device *dev)392{393struct platform_device *pdev = to_platform_device(dev);394struct s3c2410_ts_mach_info *info = pdev->dev.platform_data;395396clk_enable(ts.clock);397enable_irq(ts.irq_tc);398399/* Initialise registers */400if ((info->delay & 0xffff) > 0)401writel(info->delay & 0xffff, ts.io + S3C2410_ADCDLY);402403writel(WAIT4INT | INT_DOWN, ts.io + S3C2410_ADCTSC);404405return 0;406}407408static struct dev_pm_ops s3c_ts_pmops = {409.suspend = s3c2410ts_suspend,410.resume = s3c2410ts_resume,411};412#endif413414static struct platform_device_id s3cts_driver_ids[] = {415{ "s3c2410-ts", 0 },416{ "s3c2440-ts", 0 },417{ "s3c64xx-ts", FEAT_PEN_IRQ },418{ }419};420MODULE_DEVICE_TABLE(platform, s3cts_driver_ids);421422static struct platform_driver s3c_ts_driver = {423.driver = {424.name = "samsung-ts",425.owner = THIS_MODULE,426#ifdef CONFIG_PM427.pm = &s3c_ts_pmops,428#endif429},430.id_table = s3cts_driver_ids,431.probe = s3c2410ts_probe,432.remove = __devexit_p(s3c2410ts_remove),433};434435static int __init s3c2410ts_init(void)436{437return platform_driver_register(&s3c_ts_driver);438}439440static void __exit s3c2410ts_exit(void)441{442platform_driver_unregister(&s3c_ts_driver);443}444445module_init(s3c2410ts_init);446module_exit(s3c2410ts_exit);447448MODULE_AUTHOR("Arnaud Patard <[email protected]>, "449"Ben Dooks <[email protected]>, "450"Simtec Electronics <[email protected]>");451MODULE_DESCRIPTION("S3C24XX Touchscreen driver");452MODULE_LICENSE("GPL v2");453454455