Path: blob/master/drivers/input/touchscreen/stmpe-ts.c
15109 views
/* STMicroelectronics STMPE811 Touchscreen Driver1*2* (C) 2010 Luotao Fu <[email protected]>3* All rights reserved.4*5* This program is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License as published by the7* Free Software Foundation; either version 2 of the License, or (at your8* option) any later version.9*10*/1112#include <linux/kernel.h>13#include <linux/module.h>14#include <linux/sched.h>15#include <linux/interrupt.h>16#include <linux/init.h>17#include <linux/device.h>18#include <linux/platform_device.h>19#include <linux/input.h>20#include <linux/slab.h>21#include <linux/delay.h>22#include <linux/i2c.h>23#include <linux/workqueue.h>2425#include <linux/mfd/stmpe.h>2627/* Register layouts and functionalities are identical on all stmpexxx variants28* with touchscreen controller29*/30#define STMPE_REG_INT_STA 0x0B31#define STMPE_REG_ADC_CTRL1 0x2032#define STMPE_REG_ADC_CTRL2 0x2133#define STMPE_REG_TSC_CTRL 0x4034#define STMPE_REG_TSC_CFG 0x4135#define STMPE_REG_FIFO_TH 0x4A36#define STMPE_REG_FIFO_STA 0x4B37#define STMPE_REG_FIFO_SIZE 0x4C38#define STMPE_REG_TSC_DATA_XYZ 0x5239#define STMPE_REG_TSC_FRACTION_Z 0x5640#define STMPE_REG_TSC_I_DRIVE 0x584142#define OP_MOD_XYZ 04344#define STMPE_TSC_CTRL_TSC_EN (1<<0)4546#define STMPE_FIFO_STA_RESET (1<<0)4748#define STMPE_IRQ_TOUCH_DET 04950#define SAMPLE_TIME(x) ((x & 0xf) << 4)51#define MOD_12B(x) ((x & 0x1) << 3)52#define REF_SEL(x) ((x & 0x1) << 1)53#define ADC_FREQ(x) (x & 0x3)54#define AVE_CTRL(x) ((x & 0x3) << 6)55#define DET_DELAY(x) ((x & 0x7) << 3)56#define SETTLING(x) (x & 0x7)57#define FRACTION_Z(x) (x & 0x7)58#define I_DRIVE(x) (x & 0x1)59#define OP_MODE(x) ((x & 0x7) << 1)6061#define STMPE_TS_NAME "stmpe-ts"62#define XY_MASK 0xfff6364struct stmpe_touch {65struct stmpe *stmpe;66struct input_dev *idev;67struct delayed_work work;68struct device *dev;69u8 sample_time;70u8 mod_12b;71u8 ref_sel;72u8 adc_freq;73u8 ave_ctrl;74u8 touch_det_delay;75u8 settling;76u8 fraction_z;77u8 i_drive;78};7980static int __stmpe_reset_fifo(struct stmpe *stmpe)81{82int ret;8384ret = stmpe_set_bits(stmpe, STMPE_REG_FIFO_STA,85STMPE_FIFO_STA_RESET, STMPE_FIFO_STA_RESET);86if (ret)87return ret;8889return stmpe_set_bits(stmpe, STMPE_REG_FIFO_STA,90STMPE_FIFO_STA_RESET, 0);91}9293static void stmpe_work(struct work_struct *work)94{95int int_sta;96u32 timeout = 40;9798struct stmpe_touch *ts =99container_of(work, struct stmpe_touch, work.work);100101int_sta = stmpe_reg_read(ts->stmpe, STMPE_REG_INT_STA);102103/*104* touch_det sometimes get desasserted or just get stuck. This appears105* to be a silicon bug, We still have to clearify this with the106* manufacture. As a workaround We release the key anyway if the107* touch_det keeps coming in after 4ms, while the FIFO contains no value108* during the whole time.109*/110while ((int_sta & (1 << STMPE_IRQ_TOUCH_DET)) && (timeout > 0)) {111timeout--;112int_sta = stmpe_reg_read(ts->stmpe, STMPE_REG_INT_STA);113udelay(100);114}115116/* reset the FIFO before we report release event */117__stmpe_reset_fifo(ts->stmpe);118119input_report_abs(ts->idev, ABS_PRESSURE, 0);120input_sync(ts->idev);121}122123static irqreturn_t stmpe_ts_handler(int irq, void *data)124{125u8 data_set[4];126int x, y, z;127struct stmpe_touch *ts = data;128129/*130* Cancel scheduled polling for release if we have new value131* available. Wait if the polling is already running.132*/133cancel_delayed_work_sync(&ts->work);134135/*136* The FIFO sometimes just crashes and stops generating interrupts. This137* appears to be a silicon bug. We still have to clearify this with138* the manufacture. As a workaround we disable the TSC while we are139* collecting data and flush the FIFO after reading140*/141stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,142STMPE_TSC_CTRL_TSC_EN, 0);143144stmpe_block_read(ts->stmpe, STMPE_REG_TSC_DATA_XYZ, 4, data_set);145146x = (data_set[0] << 4) | (data_set[1] >> 4);147y = ((data_set[1] & 0xf) << 8) | data_set[2];148z = data_set[3];149150input_report_abs(ts->idev, ABS_X, x);151input_report_abs(ts->idev, ABS_Y, y);152input_report_abs(ts->idev, ABS_PRESSURE, z);153input_sync(ts->idev);154155/* flush the FIFO after we have read out our values. */156__stmpe_reset_fifo(ts->stmpe);157158/* reenable the tsc */159stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,160STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);161162/* start polling for touch_det to detect release */163schedule_delayed_work(&ts->work, HZ / 50);164165return IRQ_HANDLED;166}167168static int __devinit stmpe_init_hw(struct stmpe_touch *ts)169{170int ret;171u8 adc_ctrl1, adc_ctrl1_mask, tsc_cfg, tsc_cfg_mask;172struct stmpe *stmpe = ts->stmpe;173struct device *dev = ts->dev;174175ret = stmpe_enable(stmpe, STMPE_BLOCK_TOUCHSCREEN | STMPE_BLOCK_ADC);176if (ret) {177dev_err(dev, "Could not enable clock for ADC and TS\n");178return ret;179}180181adc_ctrl1 = SAMPLE_TIME(ts->sample_time) | MOD_12B(ts->mod_12b) |182REF_SEL(ts->ref_sel);183adc_ctrl1_mask = SAMPLE_TIME(0xff) | MOD_12B(0xff) | REF_SEL(0xff);184185ret = stmpe_set_bits(stmpe, STMPE_REG_ADC_CTRL1,186adc_ctrl1_mask, adc_ctrl1);187if (ret) {188dev_err(dev, "Could not setup ADC\n");189return ret;190}191192ret = stmpe_set_bits(stmpe, STMPE_REG_ADC_CTRL2,193ADC_FREQ(0xff), ADC_FREQ(ts->adc_freq));194if (ret) {195dev_err(dev, "Could not setup ADC\n");196return ret;197}198199tsc_cfg = AVE_CTRL(ts->ave_ctrl) | DET_DELAY(ts->touch_det_delay) |200SETTLING(ts->settling);201tsc_cfg_mask = AVE_CTRL(0xff) | DET_DELAY(0xff) | SETTLING(0xff);202203ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CFG, tsc_cfg_mask, tsc_cfg);204if (ret) {205dev_err(dev, "Could not config touch\n");206return ret;207}208209ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_FRACTION_Z,210FRACTION_Z(0xff), FRACTION_Z(ts->fraction_z));211if (ret) {212dev_err(dev, "Could not config touch\n");213return ret;214}215216ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_I_DRIVE,217I_DRIVE(0xff), I_DRIVE(ts->i_drive));218if (ret) {219dev_err(dev, "Could not config touch\n");220return ret;221}222223/* set FIFO to 1 for single point reading */224ret = stmpe_reg_write(stmpe, STMPE_REG_FIFO_TH, 1);225if (ret) {226dev_err(dev, "Could not set FIFO\n");227return ret;228}229230ret = stmpe_set_bits(stmpe, STMPE_REG_TSC_CTRL,231OP_MODE(0xff), OP_MODE(OP_MOD_XYZ));232if (ret) {233dev_err(dev, "Could not set mode\n");234return ret;235}236237return 0;238}239240static int stmpe_ts_open(struct input_dev *dev)241{242struct stmpe_touch *ts = input_get_drvdata(dev);243int ret = 0;244245ret = __stmpe_reset_fifo(ts->stmpe);246if (ret)247return ret;248249return stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,250STMPE_TSC_CTRL_TSC_EN, STMPE_TSC_CTRL_TSC_EN);251}252253static void stmpe_ts_close(struct input_dev *dev)254{255struct stmpe_touch *ts = input_get_drvdata(dev);256257cancel_delayed_work_sync(&ts->work);258259stmpe_set_bits(ts->stmpe, STMPE_REG_TSC_CTRL,260STMPE_TSC_CTRL_TSC_EN, 0);261}262263static int __devinit stmpe_input_probe(struct platform_device *pdev)264{265struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);266struct stmpe_platform_data *pdata = stmpe->pdata;267struct stmpe_touch *ts;268struct input_dev *idev;269struct stmpe_ts_platform_data *ts_pdata = NULL;270int ret;271int ts_irq;272273ts_irq = platform_get_irq_byname(pdev, "FIFO_TH");274if (ts_irq < 0)275return ts_irq;276277ts = kzalloc(sizeof(*ts), GFP_KERNEL);278if (!ts) {279ret = -ENOMEM;280goto err_out;281}282283idev = input_allocate_device();284if (!idev) {285ret = -ENOMEM;286goto err_free_ts;287}288289platform_set_drvdata(pdev, ts);290ts->stmpe = stmpe;291ts->idev = idev;292ts->dev = &pdev->dev;293294if (pdata)295ts_pdata = pdata->ts;296297if (ts_pdata) {298ts->sample_time = ts_pdata->sample_time;299ts->mod_12b = ts_pdata->mod_12b;300ts->ref_sel = ts_pdata->ref_sel;301ts->adc_freq = ts_pdata->adc_freq;302ts->ave_ctrl = ts_pdata->ave_ctrl;303ts->touch_det_delay = ts_pdata->touch_det_delay;304ts->settling = ts_pdata->settling;305ts->fraction_z = ts_pdata->fraction_z;306ts->i_drive = ts_pdata->i_drive;307}308309INIT_DELAYED_WORK(&ts->work, stmpe_work);310311ret = request_threaded_irq(ts_irq, NULL, stmpe_ts_handler,312IRQF_ONESHOT, STMPE_TS_NAME, ts);313if (ret) {314dev_err(&pdev->dev, "Failed to request IRQ %d\n", ts_irq);315goto err_free_input;316}317318ret = stmpe_init_hw(ts);319if (ret)320goto err_free_irq;321322idev->name = STMPE_TS_NAME;323idev->id.bustype = BUS_I2C;324idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);325idev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);326327idev->open = stmpe_ts_open;328idev->close = stmpe_ts_close;329330input_set_drvdata(idev, ts);331332input_set_abs_params(idev, ABS_X, 0, XY_MASK, 0, 0);333input_set_abs_params(idev, ABS_Y, 0, XY_MASK, 0, 0);334input_set_abs_params(idev, ABS_PRESSURE, 0x0, 0xff, 0, 0);335336ret = input_register_device(idev);337if (ret) {338dev_err(&pdev->dev, "Could not register input device\n");339goto err_free_irq;340}341342return ret;343344err_free_irq:345free_irq(ts_irq, ts);346err_free_input:347input_free_device(idev);348platform_set_drvdata(pdev, NULL);349err_free_ts:350kfree(ts);351err_out:352return ret;353}354355static int __devexit stmpe_ts_remove(struct platform_device *pdev)356{357struct stmpe_touch *ts = platform_get_drvdata(pdev);358unsigned int ts_irq = platform_get_irq_byname(pdev, "FIFO_TH");359360stmpe_disable(ts->stmpe, STMPE_BLOCK_TOUCHSCREEN);361362free_irq(ts_irq, ts);363364platform_set_drvdata(pdev, NULL);365366input_unregister_device(ts->idev);367368kfree(ts);369370return 0;371}372373static struct platform_driver stmpe_ts_driver = {374.driver = {375.name = STMPE_TS_NAME,376.owner = THIS_MODULE,377},378.probe = stmpe_input_probe,379.remove = __devexit_p(stmpe_ts_remove),380};381382static int __init stmpe_ts_init(void)383{384return platform_driver_register(&stmpe_ts_driver);385}386387module_init(stmpe_ts_init);388389static void __exit stmpe_ts_exit(void)390{391platform_driver_unregister(&stmpe_ts_driver);392}393394module_exit(stmpe_ts_exit);395396MODULE_AUTHOR("Luotao Fu <[email protected]>");397MODULE_DESCRIPTION("STMPEXXX touchscreen driver");398MODULE_LICENSE("GPL");399MODULE_ALIAS("platform:" STMPE_TS_NAME);400401402