Path: blob/master/drivers/input/touchscreen/eeti_ts.c
15109 views
/*1* Touch Screen driver for EETI's I2C connected touch screen panels2* Copyright (c) 2009 Daniel Mack <[email protected]>3*4* See EETI's software guide for the protocol specification:5* http://home.eeti.com.tw/web20/eg/guide.htm6*7* Based on migor_ts.c8* Copyright (c) 2008 Magnus Damm9* Copyright (c) 2007 Ujjwal Pande <[email protected]>10*11* This file is free software; you can redistribute it and/or12* modify it under the terms of the GNU General Public13* License as published by the Free Software Foundation; either14* version 2 of the License, or (at your option) any later version.15*16* This file is distributed in the hope that it will be useful,17* but WITHOUT ANY WARRANTY; without even the implied warranty of18* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU19* General Public License for more details.20*21* You should have received a copy of the GNU General Public22* License along with this library; if not, write to the Free Software23* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA24*/2526#include <linux/module.h>27#include <linux/moduleparam.h>28#include <linux/kernel.h>29#include <linux/input.h>30#include <linux/interrupt.h>31#include <linux/i2c.h>32#include <linux/timer.h>33#include <linux/gpio.h>34#include <linux/input/eeti_ts.h>35#include <linux/slab.h>3637static int flip_x;38module_param(flip_x, bool, 0644);39MODULE_PARM_DESC(flip_x, "flip x coordinate");4041static int flip_y;42module_param(flip_y, bool, 0644);43MODULE_PARM_DESC(flip_y, "flip y coordinate");4445struct eeti_ts_priv {46struct i2c_client *client;47struct input_dev *input;48struct work_struct work;49struct mutex mutex;50int irq, irq_active_high;51};5253#define EETI_TS_BITDEPTH (11)54#define EETI_MAXVAL ((1 << (EETI_TS_BITDEPTH + 1)) - 1)5556#define REPORT_BIT_PRESSED (1 << 0)57#define REPORT_BIT_AD0 (1 << 1)58#define REPORT_BIT_AD1 (1 << 2)59#define REPORT_BIT_HAS_PRESSURE (1 << 6)60#define REPORT_RES_BITS(v) (((v) >> 1) + EETI_TS_BITDEPTH)6162static inline int eeti_ts_irq_active(struct eeti_ts_priv *priv)63{64return gpio_get_value(irq_to_gpio(priv->irq)) == priv->irq_active_high;65}6667static void eeti_ts_read(struct work_struct *work)68{69char buf[6];70unsigned int x, y, res, pressed, to = 100;71struct eeti_ts_priv *priv =72container_of(work, struct eeti_ts_priv, work);7374mutex_lock(&priv->mutex);7576while (eeti_ts_irq_active(priv) && --to)77i2c_master_recv(priv->client, buf, sizeof(buf));7879if (!to) {80dev_err(&priv->client->dev,81"unable to clear IRQ - line stuck?\n");82goto out;83}8485/* drop non-report packets */86if (!(buf[0] & 0x80))87goto out;8889pressed = buf[0] & REPORT_BIT_PRESSED;90res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));91x = buf[2] | (buf[1] << 8);92y = buf[4] | (buf[3] << 8);9394/* fix the range to 11 bits */95x >>= res - EETI_TS_BITDEPTH;96y >>= res - EETI_TS_BITDEPTH;9798if (flip_x)99x = EETI_MAXVAL - x;100101if (flip_y)102y = EETI_MAXVAL - y;103104if (buf[0] & REPORT_BIT_HAS_PRESSURE)105input_report_abs(priv->input, ABS_PRESSURE, buf[5]);106107input_report_abs(priv->input, ABS_X, x);108input_report_abs(priv->input, ABS_Y, y);109input_report_key(priv->input, BTN_TOUCH, !!pressed);110input_sync(priv->input);111112out:113mutex_unlock(&priv->mutex);114}115116static irqreturn_t eeti_ts_isr(int irq, void *dev_id)117{118struct eeti_ts_priv *priv = dev_id;119120/* postpone I2C transactions as we are atomic */121schedule_work(&priv->work);122123return IRQ_HANDLED;124}125126static void eeti_ts_start(struct eeti_ts_priv *priv)127{128enable_irq(priv->irq);129130/* Read the events once to arm the IRQ */131eeti_ts_read(&priv->work);132}133134static void eeti_ts_stop(struct eeti_ts_priv *priv)135{136disable_irq(priv->irq);137cancel_work_sync(&priv->work);138}139140static int eeti_ts_open(struct input_dev *dev)141{142struct eeti_ts_priv *priv = input_get_drvdata(dev);143144eeti_ts_start(priv);145146return 0;147}148149static void eeti_ts_close(struct input_dev *dev)150{151struct eeti_ts_priv *priv = input_get_drvdata(dev);152153eeti_ts_stop(priv);154}155156static int __devinit eeti_ts_probe(struct i2c_client *client,157const struct i2c_device_id *idp)158{159struct eeti_ts_platform_data *pdata;160struct eeti_ts_priv *priv;161struct input_dev *input;162unsigned int irq_flags;163int err = -ENOMEM;164165/*166* In contrast to what's described in the datasheet, there seems167* to be no way of probing the presence of that device using I2C168* commands. So we need to blindly believe it is there, and wait169* for interrupts to occur.170*/171172priv = kzalloc(sizeof(*priv), GFP_KERNEL);173if (!priv) {174dev_err(&client->dev, "failed to allocate driver data\n");175goto err0;176}177178mutex_init(&priv->mutex);179input = input_allocate_device();180181if (!input) {182dev_err(&client->dev, "Failed to allocate input device.\n");183goto err1;184}185186input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);187input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);188189input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);190input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);191input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);192193input->name = client->name;194input->id.bustype = BUS_I2C;195input->dev.parent = &client->dev;196input->open = eeti_ts_open;197input->close = eeti_ts_close;198199priv->client = client;200priv->input = input;201priv->irq = client->irq;202203pdata = client->dev.platform_data;204205if (pdata)206priv->irq_active_high = pdata->irq_active_high;207208irq_flags = priv->irq_active_high ?209IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;210211INIT_WORK(&priv->work, eeti_ts_read);212i2c_set_clientdata(client, priv);213input_set_drvdata(input, priv);214215err = input_register_device(input);216if (err)217goto err1;218219err = request_irq(priv->irq, eeti_ts_isr, irq_flags,220client->name, priv);221if (err) {222dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");223goto err2;224}225226/*227* Disable the device for now. It will be enabled once the228* input device is opened.229*/230eeti_ts_stop(priv);231232device_init_wakeup(&client->dev, 0);233return 0;234235err2:236input_unregister_device(input);237input = NULL; /* so we dont try to free it below */238err1:239input_free_device(input);240kfree(priv);241err0:242return err;243}244245static int __devexit eeti_ts_remove(struct i2c_client *client)246{247struct eeti_ts_priv *priv = i2c_get_clientdata(client);248249free_irq(priv->irq, priv);250/*251* eeti_ts_stop() leaves IRQ disabled. We need to re-enable it252* so that device still works if we reload the driver.253*/254enable_irq(priv->irq);255256input_unregister_device(priv->input);257kfree(priv);258259return 0;260}261262#ifdef CONFIG_PM263static int eeti_ts_suspend(struct device *dev)264{265struct i2c_client *client = to_i2c_client(dev);266struct eeti_ts_priv *priv = i2c_get_clientdata(client);267struct input_dev *input_dev = priv->input;268269mutex_lock(&input_dev->mutex);270271if (input_dev->users)272eeti_ts_stop(priv);273274mutex_unlock(&input_dev->mutex);275276if (device_may_wakeup(&client->dev))277enable_irq_wake(priv->irq);278279return 0;280}281282static int eeti_ts_resume(struct device *dev)283{284struct i2c_client *client = to_i2c_client(dev);285struct eeti_ts_priv *priv = i2c_get_clientdata(client);286struct input_dev *input_dev = priv->input;287288if (device_may_wakeup(&client->dev))289disable_irq_wake(priv->irq);290291mutex_lock(&input_dev->mutex);292293if (input_dev->users)294eeti_ts_start(priv);295296mutex_unlock(&input_dev->mutex);297298return 0;299}300301static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume);302#endif303304static const struct i2c_device_id eeti_ts_id[] = {305{ "eeti_ts", 0 },306{ }307};308MODULE_DEVICE_TABLE(i2c, eeti_ts_id);309310static struct i2c_driver eeti_ts_driver = {311.driver = {312.name = "eeti_ts",313#ifdef CONFIG_PM314.pm = &eeti_ts_pm,315#endif316},317.probe = eeti_ts_probe,318.remove = __devexit_p(eeti_ts_remove),319.id_table = eeti_ts_id,320};321322static int __init eeti_ts_init(void)323{324return i2c_add_driver(&eeti_ts_driver);325}326327static void __exit eeti_ts_exit(void)328{329i2c_del_driver(&eeti_ts_driver);330}331332MODULE_DESCRIPTION("EETI Touchscreen driver");333MODULE_AUTHOR("Daniel Mack <[email protected]>");334MODULE_LICENSE("GPL");335336module_init(eeti_ts_init);337module_exit(eeti_ts_exit);338339340341