Path: blob/master/drivers/input/touchscreen/tnetv107x-ts.c
15111 views
/*1* Texas Instruments TNETV107X Touchscreen Driver2*3* Copyright (C) 2010 Texas Instruments4*5* This program is free software; you can redistribute it and/or6* modify it under the terms of the GNU General Public License as7* published by the Free Software Foundation version 2.8*9* This program is distributed "as is" WITHOUT ANY WARRANTY of any10* kind, whether express or implied; without even the implied warranty11* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12* GNU General Public License for more details.13*/1415#include <linux/kernel.h>16#include <linux/err.h>17#include <linux/errno.h>18#include <linux/input.h>19#include <linux/platform_device.h>20#include <linux/interrupt.h>21#include <linux/slab.h>22#include <linux/delay.h>23#include <linux/ctype.h>24#include <linux/io.h>25#include <linux/clk.h>2627#include <mach/tnetv107x.h>2829#define TSC_PENUP_POLL (HZ / 5)30#define IDLE_TIMEOUT 100 /* msec */3132/*33* The first and last samples of a touch interval are usually garbage and need34* to be filtered out with these devices. The following definitions control35* the number of samples skipped.36*/37#define TSC_HEAD_SKIP 138#define TSC_TAIL_SKIP 139#define TSC_SKIP (TSC_HEAD_SKIP + TSC_TAIL_SKIP + 1)40#define TSC_SAMPLES (TSC_SKIP + 1)4142/* Register Offsets */43struct tsc_regs {44u32 rev;45u32 tscm;46u32 bwcm;47u32 swc;48u32 adcchnl;49u32 adcdata;50u32 chval[4];51};5253/* TSC Mode Configuration Register (tscm) bits */54#define WMODE BIT(0)55#define TSKIND BIT(1)56#define ZMEASURE_EN BIT(2)57#define IDLE BIT(3)58#define TSC_EN BIT(4)59#define STOP BIT(5)60#define ONE_SHOT BIT(6)61#define SINGLE BIT(7)62#define AVG BIT(8)63#define AVGNUM(x) (((x) & 0x03) << 9)64#define PVSTC(x) (((x) & 0x07) << 11)65#define PON BIT(14)66#define PONBG BIT(15)67#define AFERST BIT(16)6869/* ADC DATA Capture Register bits */70#define DATA_VALID BIT(16)7172/* Register Access Macros */73#define tsc_read(ts, reg) __raw_readl(&(ts)->regs->reg)74#define tsc_write(ts, reg, val) __raw_writel(val, &(ts)->regs->reg);75#define tsc_set_bits(ts, reg, val) \76tsc_write(ts, reg, tsc_read(ts, reg) | (val))77#define tsc_clr_bits(ts, reg, val) \78tsc_write(ts, reg, tsc_read(ts, reg) & ~(val))7980struct sample {81int x, y, p;82};8384struct tsc_data {85struct input_dev *input_dev;86struct resource *res;87struct tsc_regs __iomem *regs;88struct timer_list timer;89spinlock_t lock;90struct clk *clk;91struct device *dev;92int sample_count;93struct sample samples[TSC_SAMPLES];94int tsc_irq;95};9697static int tsc_read_sample(struct tsc_data *ts, struct sample* sample)98{99int x, y, z1, z2, t, p = 0;100u32 val;101102val = tsc_read(ts, chval[0]);103if (val & DATA_VALID)104x = val & 0xffff;105else106return -EINVAL;107108y = tsc_read(ts, chval[1]) & 0xffff;109z1 = tsc_read(ts, chval[2]) & 0xffff;110z2 = tsc_read(ts, chval[3]) & 0xffff;111112if (z1) {113t = ((600 * x) * (z2 - z1));114p = t / (u32) (z1 << 12);115if (p < 0)116p = 0;117}118119sample->x = x;120sample->y = y;121sample->p = p;122123return 0;124}125126static void tsc_poll(unsigned long data)127{128struct tsc_data *ts = (struct tsc_data *)data;129unsigned long flags;130int i, val, x, y, p;131132spin_lock_irqsave(&ts->lock, flags);133134if (ts->sample_count >= TSC_SKIP) {135input_report_abs(ts->input_dev, ABS_PRESSURE, 0);136input_report_key(ts->input_dev, BTN_TOUCH, 0);137input_sync(ts->input_dev);138} else if (ts->sample_count > 0) {139/*140* A touch event lasted less than our skip count. Salvage and141* report anyway.142*/143for (i = 0, val = 0; i < ts->sample_count; i++)144val += ts->samples[i].x;145x = val / ts->sample_count;146147for (i = 0, val = 0; i < ts->sample_count; i++)148val += ts->samples[i].y;149y = val / ts->sample_count;150151for (i = 0, val = 0; i < ts->sample_count; i++)152val += ts->samples[i].p;153p = val / ts->sample_count;154155input_report_abs(ts->input_dev, ABS_X, x);156input_report_abs(ts->input_dev, ABS_Y, y);157input_report_abs(ts->input_dev, ABS_PRESSURE, p);158input_report_key(ts->input_dev, BTN_TOUCH, 1);159input_sync(ts->input_dev);160}161162ts->sample_count = 0;163164spin_unlock_irqrestore(&ts->lock, flags);165}166167static irqreturn_t tsc_irq(int irq, void *dev_id)168{169struct tsc_data *ts = (struct tsc_data *)dev_id;170struct sample *sample;171int index;172173spin_lock(&ts->lock);174175index = ts->sample_count % TSC_SAMPLES;176sample = &ts->samples[index];177if (tsc_read_sample(ts, sample) < 0)178goto out;179180if (++ts->sample_count >= TSC_SKIP) {181index = (ts->sample_count - TSC_TAIL_SKIP - 1) % TSC_SAMPLES;182sample = &ts->samples[index];183184input_report_abs(ts->input_dev, ABS_X, sample->x);185input_report_abs(ts->input_dev, ABS_Y, sample->y);186input_report_abs(ts->input_dev, ABS_PRESSURE, sample->p);187if (ts->sample_count == TSC_SKIP)188input_report_key(ts->input_dev, BTN_TOUCH, 1);189input_sync(ts->input_dev);190}191mod_timer(&ts->timer, jiffies + TSC_PENUP_POLL);192out:193spin_unlock(&ts->lock);194return IRQ_HANDLED;195}196197static int tsc_start(struct input_dev *dev)198{199struct tsc_data *ts = input_get_drvdata(dev);200unsigned long timeout = jiffies + msecs_to_jiffies(IDLE_TIMEOUT);201u32 val;202203clk_enable(ts->clk);204205/* Go to idle mode, before any initialization */206while (time_after(timeout, jiffies)) {207if (tsc_read(ts, tscm) & IDLE)208break;209}210211if (time_before(timeout, jiffies)) {212dev_warn(ts->dev, "timeout waiting for idle\n");213clk_disable(ts->clk);214return -EIO;215}216217/* Configure TSC Control register*/218val = (PONBG | PON | PVSTC(4) | ONE_SHOT | ZMEASURE_EN);219tsc_write(ts, tscm, val);220221/* Bring TSC out of reset: Clear AFE reset bit */222val &= ~(AFERST);223tsc_write(ts, tscm, val);224225/* Configure all pins for hardware control*/226tsc_write(ts, bwcm, 0);227228/* Finally enable the TSC */229tsc_set_bits(ts, tscm, TSC_EN);230231return 0;232}233234static void tsc_stop(struct input_dev *dev)235{236struct tsc_data *ts = input_get_drvdata(dev);237238tsc_clr_bits(ts, tscm, TSC_EN);239synchronize_irq(ts->tsc_irq);240del_timer_sync(&ts->timer);241clk_disable(ts->clk);242}243244static int __devinit tsc_probe(struct platform_device *pdev)245{246struct device *dev = &pdev->dev;247struct tsc_data *ts;248int error = 0;249u32 rev = 0;250251ts = kzalloc(sizeof(struct tsc_data), GFP_KERNEL);252if (!ts) {253dev_err(dev, "cannot allocate device info\n");254return -ENOMEM;255}256257ts->dev = dev;258spin_lock_init(&ts->lock);259setup_timer(&ts->timer, tsc_poll, (unsigned long)ts);260platform_set_drvdata(pdev, ts);261262ts->tsc_irq = platform_get_irq(pdev, 0);263if (ts->tsc_irq < 0) {264dev_err(dev, "cannot determine device interrupt\n");265error = -ENODEV;266goto error_res;267}268269ts->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);270if (!ts->res) {271dev_err(dev, "cannot determine register area\n");272error = -ENODEV;273goto error_res;274}275276if (!request_mem_region(ts->res->start, resource_size(ts->res),277pdev->name)) {278dev_err(dev, "cannot claim register memory\n");279ts->res = NULL;280error = -EINVAL;281goto error_res;282}283284ts->regs = ioremap(ts->res->start, resource_size(ts->res));285if (!ts->regs) {286dev_err(dev, "cannot map register memory\n");287error = -ENOMEM;288goto error_map;289}290291ts->clk = clk_get(dev, NULL);292if (IS_ERR(ts->clk)) {293dev_err(dev, "cannot claim device clock\n");294error = PTR_ERR(ts->clk);295goto error_clk;296}297298error = request_threaded_irq(ts->tsc_irq, NULL, tsc_irq, 0,299dev_name(dev), ts);300if (error < 0) {301dev_err(ts->dev, "Could not allocate ts irq\n");302goto error_irq;303}304305ts->input_dev = input_allocate_device();306if (!ts->input_dev) {307dev_err(dev, "cannot allocate input device\n");308error = -ENOMEM;309goto error_input;310}311input_set_drvdata(ts->input_dev, ts);312313ts->input_dev->name = pdev->name;314ts->input_dev->id.bustype = BUS_HOST;315ts->input_dev->dev.parent = &pdev->dev;316ts->input_dev->open = tsc_start;317ts->input_dev->close = tsc_stop;318319clk_enable(ts->clk);320rev = tsc_read(ts, rev);321ts->input_dev->id.product = ((rev >> 8) & 0x07);322ts->input_dev->id.version = ((rev >> 16) & 0xfff);323clk_disable(ts->clk);324325__set_bit(EV_KEY, ts->input_dev->evbit);326__set_bit(EV_ABS, ts->input_dev->evbit);327__set_bit(BTN_TOUCH, ts->input_dev->keybit);328329input_set_abs_params(ts->input_dev, ABS_X, 0, 0xffff, 5, 0);330input_set_abs_params(ts->input_dev, ABS_Y, 0, 0xffff, 5, 0);331input_set_abs_params(ts->input_dev, ABS_PRESSURE, 0, 4095, 128, 0);332333error = input_register_device(ts->input_dev);334if (error < 0) {335dev_err(dev, "failed input device registration\n");336goto error_reg;337}338339return 0;340341error_reg:342input_free_device(ts->input_dev);343error_input:344free_irq(ts->tsc_irq, ts);345error_irq:346clk_put(ts->clk);347error_clk:348iounmap(ts->regs);349error_map:350release_mem_region(ts->res->start, resource_size(ts->res));351error_res:352platform_set_drvdata(pdev, NULL);353kfree(ts);354355return error;356}357358static int __devexit tsc_remove(struct platform_device *pdev)359{360struct tsc_data *ts = platform_get_drvdata(pdev);361362input_unregister_device(ts->input_dev);363free_irq(ts->tsc_irq, ts);364clk_put(ts->clk);365iounmap(ts->regs);366release_mem_region(ts->res->start, resource_size(ts->res));367platform_set_drvdata(pdev, NULL);368kfree(ts);369370return 0;371}372373static struct platform_driver tsc_driver = {374.probe = tsc_probe,375.remove = __devexit_p(tsc_remove),376.driver.name = "tnetv107x-ts",377.driver.owner = THIS_MODULE,378};379380static int __init tsc_init(void)381{382return platform_driver_register(&tsc_driver);383}384385static void __exit tsc_exit(void)386{387platform_driver_unregister(&tsc_driver);388}389390module_init(tsc_init);391module_exit(tsc_exit);392393MODULE_AUTHOR("Cyril Chemparathy");394MODULE_DESCRIPTION("TNETV107X Touchscreen Driver");395MODULE_ALIAS("platform: tnetv107x-ts");396MODULE_LICENSE("GPL");397398399