···939939 To compile this driver as a module, choose M here: the940940 module will be called tsc40.941941942942+config TOUCHSCREEN_TSC200X_CORE943943+ tristate944944+945945+config TOUCHSCREEN_TSC2004946946+ tristate "TSC2004 based touchscreens"947947+ depends on I2C948948+ select REGMAP_I2C949949+ select TOUCHSCREEN_TSC200X_CORE950950+ help951951+ Say Y here if you have a TSC2004 based touchscreen.952952+953953+ If unsure, say N.954954+955955+ To compile this driver as a module, choose M here: the956956+ module will be called tsc2004.957957+942958config TOUCHSCREEN_TSC2005943959 tristate "TSC2005 based touchscreens"944960 depends on SPI_MASTER945961 select REGMAP_SPI962962+ select TOUCHSCREEN_TSC200X_CORE946963 help947964 Say Y here if you have a TSC2005 based touchscreen.948965
···11+/*22+ * TSC2004 touchscreen driver33+ *44+ * Copyright (C) 2015 QWERTY Embedded Design55+ * Copyright (C) 2015 EMAC Inc.66+ *77+ * This program is free software; you can redistribute it and/or modify88+ * it under the terms of the GNU General Public License as published by99+ * the Free Software Foundation; either version 2 of the License, or1010+ * (at your option) any later version.1111+ *1212+ * This program is distributed in the hope that it will be useful,1313+ * but WITHOUT ANY WARRANTY; without even the implied warranty of1414+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the1515+ * GNU General Public License for more details.1616+ */1717+1818+#include <linux/module.h>1919+#include <linux/input.h>2020+#include <linux/of.h>2121+#include <linux/i2c.h>2222+#include <linux/regmap.h>2323+#include "tsc200x-core.h"2424+2525+static int tsc2004_cmd(struct device *dev, u8 cmd)2626+{2727+ u8 tx = TSC200X_CMD | TSC200X_CMD_12BIT | cmd;2828+ s32 data;2929+ struct i2c_client *i2c = to_i2c_client(dev);3030+3131+ data = i2c_smbus_write_byte(i2c, tx);3232+ if (data < 0) {3333+ dev_err(dev, "%s: failed, command: %x i2c error: %d\n",3434+ __func__, cmd, data);3535+ return data;3636+ }3737+3838+ return 0;3939+}4040+4141+static int tsc2004_probe(struct i2c_client *i2c,4242+ const struct i2c_device_id *id)4343+4444+{4545+ return tsc200x_probe(&i2c->dev, i2c->irq, BUS_I2C,4646+ devm_regmap_init_i2c(i2c, &tsc200x_regmap_config),4747+ tsc2004_cmd);4848+}4949+5050+static int tsc2004_remove(struct i2c_client *i2c)5151+{5252+ return tsc200x_remove(&i2c->dev);5353+}5454+5555+static const struct i2c_device_id tsc2004_idtable[] = {5656+ { "tsc2004", 0 },5757+ { }5858+};5959+MODULE_DEVICE_TABLE(i2c, tsc2004_idtable);6060+6161+#ifdef CONFIG_OF6262+static const struct of_device_id tsc2004_of_match[] = {6363+ { .compatible = "ti,tsc2004" },6464+ { /* sentinel */ }6565+};6666+MODULE_DEVICE_TABLE(of, tsc2004_of_match);6767+#endif6868+6969+static struct i2c_driver tsc2004_driver = {7070+ .driver = {7171+ .name = "tsc2004",7272+ .of_match_table = of_match_ptr(tsc2004_of_match),7373+ .pm = &tsc200x_pm_ops,7474+ },7575+ .id_table = tsc2004_idtable,7676+ .probe = tsc2004_probe,7777+ .remove = tsc2004_remove,7878+};7979+module_i2c_driver(tsc2004_driver);8080+8181+MODULE_AUTHOR("Michael Welling <mwelling@ieee.org>");8282+MODULE_DESCRIPTION("TSC2004 Touchscreen Driver");8383+MODULE_LICENSE("GPL");
+18-696
drivers/input/touchscreen/tsc2005.c
···22 * TSC2005 touchscreen driver33 *44 * Copyright (C) 2006-2010 Nokia Corporation55+ * Copyright (C) 2015 QWERTY Embedded Design66+ * Copyright (C) 2015 EMAC Inc.57 *66- * Author: Lauri Leukkunen <lauri.leukkunen@nokia.com>77- * based on TSC2301 driver by Klaus K. Pedersen <klaus.k.pedersen@nokia.com>88+ * Based on original tsc2005.c by Lauri Leukkunen <lauri.leukkunen@nokia.com>89 *910 * This program is free software; you can redistribute it and/or modify1011 * it under the terms of the GNU General Public License as published by···1615 * but WITHOUT ANY WARRANTY; without even the implied warranty of1716 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the1817 * GNU General Public License for more details.1919- *2020- * You should have received a copy of the GNU General Public License2121- * along with this program; if not, write to the Free Software2222- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA2323- *2418 */25192626-#include <linux/kernel.h>2720#include <linux/module.h>2821#include <linux/input.h>2929-#include <linux/input/touchscreen.h>3030-#include <linux/interrupt.h>3131-#include <linux/delay.h>3232-#include <linux/pm.h>3333-#include <linux/of.h>3422#include <linux/spi/spi.h>3535-#include <linux/spi/tsc2005.h>3636-#include <linux/regulator/consumer.h>3723#include <linux/regmap.h>3838-#include <linux/gpio/consumer.h>2424+#include "tsc200x-core.h"39254040-/*4141- * The touchscreen interface operates as follows:4242- *4343- * 1) Pen is pressed against the touchscreen.4444- * 2) TSC2005 performs AD conversion.4545- * 3) After the conversion is done TSC2005 drives DAV line down.4646- * 4) GPIO IRQ is received and tsc2005_irq_thread() is scheduled.4747- * 5) tsc2005_irq_thread() queues up an spi transfer to fetch the x, y, z1, z24848- * values.4949- * 6) tsc2005_irq_thread() reports coordinates to input layer and sets up5050- * tsc2005_penup_timer() to be called after TSC2005_PENUP_TIME_MS (40ms).5151- * 7) When the penup timer expires, there have not been touch or DAV interrupts5252- * during the last 40ms which means the pen has been lifted.5353- *5454- * ESD recovery via a hardware reset is done if the TSC2005 doesn't respond5555- * after a configurable period (in ms) of activity. If esd_timeout is 0, the5656- * watchdog is disabled.5757- */5858-5959-/* control byte 1 */6060-#define TSC2005_CMD 0x806161-#define TSC2005_CMD_NORMAL 0x006262-#define TSC2005_CMD_STOP 0x016363-#define TSC2005_CMD_12BIT 0x046464-6565-/* control byte 0 */6666-#define TSC2005_REG_READ 0x01 /* R/W access */6767-#define TSC2005_REG_PND0 0x02 /* Power Not Down Control */6868-#define TSC2005_REG_X (0x0 << 3)6969-#define TSC2005_REG_Y (0x1 << 3)7070-#define TSC2005_REG_Z1 (0x2 << 3)7171-#define TSC2005_REG_Z2 (0x3 << 3)7272-#define TSC2005_REG_AUX (0x4 << 3)7373-#define TSC2005_REG_TEMP1 (0x5 << 3)7474-#define TSC2005_REG_TEMP2 (0x6 << 3)7575-#define TSC2005_REG_STATUS (0x7 << 3)7676-#define TSC2005_REG_AUX_HIGH (0x8 << 3)7777-#define TSC2005_REG_AUX_LOW (0x9 << 3)7878-#define TSC2005_REG_TEMP_HIGH (0xA << 3)7979-#define TSC2005_REG_TEMP_LOW (0xB << 3)8080-#define TSC2005_REG_CFR0 (0xC << 3)8181-#define TSC2005_REG_CFR1 (0xD << 3)8282-#define TSC2005_REG_CFR2 (0xE << 3)8383-#define TSC2005_REG_CONV_FUNC (0xF << 3)8484-8585-/* configuration register 0 */8686-#define TSC2005_CFR0_PRECHARGE_276US 0x00408787-#define TSC2005_CFR0_STABTIME_1MS 0x03008888-#define TSC2005_CFR0_CLOCK_1MHZ 0x10008989-#define TSC2005_CFR0_RESOLUTION12 0x20009090-#define TSC2005_CFR0_PENMODE 0x80009191-#define TSC2005_CFR0_INITVALUE (TSC2005_CFR0_STABTIME_1MS | \9292- TSC2005_CFR0_CLOCK_1MHZ | \9393- TSC2005_CFR0_RESOLUTION12 | \9494- TSC2005_CFR0_PRECHARGE_276US | \9595- TSC2005_CFR0_PENMODE)9696-9797-/* bits common to both read and write of configuration register 0 */9898-#define TSC2005_CFR0_RW_MASK 0x3fff9999-100100-/* configuration register 1 */101101-#define TSC2005_CFR1_BATCHDELAY_4MS 0x0003102102-#define TSC2005_CFR1_INITVALUE TSC2005_CFR1_BATCHDELAY_4MS103103-104104-/* configuration register 2 */105105-#define TSC2005_CFR2_MAVE_Z 0x0004106106-#define TSC2005_CFR2_MAVE_Y 0x0008107107-#define TSC2005_CFR2_MAVE_X 0x0010108108-#define TSC2005_CFR2_AVG_7 0x0800109109-#define TSC2005_CFR2_MEDIUM_15 0x3000110110-#define TSC2005_CFR2_INITVALUE (TSC2005_CFR2_MAVE_X | \111111- TSC2005_CFR2_MAVE_Y | \112112- TSC2005_CFR2_MAVE_Z | \113113- TSC2005_CFR2_MEDIUM_15 | \114114- TSC2005_CFR2_AVG_7)115115-116116-#define MAX_12BIT 0xfff117117-#define TSC2005_DEF_X_FUZZ 4118118-#define TSC2005_DEF_Y_FUZZ 8119119-#define TSC2005_DEF_P_FUZZ 2120120-#define TSC2005_DEF_RESISTOR 280121121-122122-#define TSC2005_SPI_MAX_SPEED_HZ 10000000123123-#define TSC2005_PENUP_TIME_MS 40124124-125125-static const struct regmap_range tsc2005_writable_ranges[] = {126126- regmap_reg_range(TSC2005_REG_AUX_HIGH, TSC2005_REG_CFR2),127127-};128128-129129-static const struct regmap_access_table tsc2005_writable_table = {130130- .yes_ranges = tsc2005_writable_ranges,131131- .n_yes_ranges = ARRAY_SIZE(tsc2005_writable_ranges),132132-};133133-134134-static struct regmap_config tsc2005_regmap_config = {135135- .reg_bits = 8,136136- .val_bits = 16,137137- .reg_stride = 0x08,138138- .max_register = 0x78,139139- .read_flag_mask = TSC2005_REG_READ,140140- .write_flag_mask = TSC2005_REG_PND0,141141- .wr_table = &tsc2005_writable_table,142142- .use_single_rw = true,143143-};144144-145145-struct tsc2005_data {146146- u16 x;147147- u16 y;148148- u16 z1;149149- u16 z2;150150-} __packed;151151-#define TSC2005_DATA_REGS 4152152-153153-struct tsc2005 {154154- struct spi_device *spi;155155- struct regmap *regmap;156156-157157- struct input_dev *idev;158158- char phys[32];159159-160160- struct mutex mutex;161161-162162- /* raw copy of previous x,y,z */163163- int in_x;164164- int in_y;165165- int in_z1;166166- int in_z2;167167-168168- spinlock_t lock;169169- struct timer_list penup_timer;170170-171171- unsigned int esd_timeout;172172- struct delayed_work esd_work;173173- unsigned long last_valid_interrupt;174174-175175- unsigned int x_plate_ohm;176176-177177- bool opened;178178- bool suspended;179179-180180- bool pen_down;181181-182182- struct regulator *vio;183183-184184- struct gpio_desc *reset_gpio;185185- void (*set_reset)(bool enable);186186-};187187-188188-static int tsc2005_cmd(struct tsc2005 *ts, u8 cmd)2626+static int tsc2005_cmd(struct device *dev, u8 cmd)18927{190190- u8 tx = TSC2005_CMD | TSC2005_CMD_12BIT | cmd;2828+ u8 tx = TSC200X_CMD | TSC200X_CMD_12BIT | cmd;19129 struct spi_transfer xfer = {192192- .tx_buf = &tx,193193- .len = 1,194194- .bits_per_word = 8,3030+ .tx_buf = &tx,3131+ .len = 1,3232+ .bits_per_word = 8,19533 };19634 struct spi_message msg;3535+ struct spi_device *spi = to_spi_device(dev);19736 int error;1983719938 spi_message_init(&msg);20039 spi_message_add_tail(&xfer, &msg);20140202202- error = spi_sync(ts->spi, &msg);4141+ error = spi_sync(spi, &msg);20342 if (error) {204204- dev_err(&ts->spi->dev, "%s: failed, command: %x, error: %d\n",4343+ dev_err(dev, "%s: failed, command: %x, spi error: %d\n",20544 __func__, cmd, error);20645 return error;20746 }···49208 return 0;50209}512105252-static void tsc2005_update_pen_state(struct tsc2005 *ts,5353- int x, int y, int pressure)5454-{5555- if (pressure) {5656- input_report_abs(ts->idev, ABS_X, x);5757- input_report_abs(ts->idev, ABS_Y, y);5858- input_report_abs(ts->idev, ABS_PRESSURE, pressure);5959- if (!ts->pen_down) {6060- input_report_key(ts->idev, BTN_TOUCH, !!pressure);6161- ts->pen_down = true;6262- }6363- } else {6464- input_report_abs(ts->idev, ABS_PRESSURE, 0);6565- if (ts->pen_down) {6666- input_report_key(ts->idev, BTN_TOUCH, 0);6767- ts->pen_down = false;6868- }6969- }7070- input_sync(ts->idev);7171- dev_dbg(&ts->spi->dev, "point(%4d,%4d), pressure (%4d)\n", x, y,7272- pressure);7373-}7474-7575-static irqreturn_t tsc2005_irq_thread(int irq, void *_ts)7676-{7777- struct tsc2005 *ts = _ts;7878- unsigned long flags;7979- unsigned int pressure;8080- struct tsc2005_data tsdata;8181- int error;8282-8383- /* read the coordinates */8484- error = regmap_bulk_read(ts->regmap, TSC2005_REG_X, &tsdata,8585- TSC2005_DATA_REGS);8686- if (unlikely(error))8787- goto out;8888-8989- /* validate position */9090- if (unlikely(tsdata.x > MAX_12BIT || tsdata.y > MAX_12BIT))9191- goto out;9292-9393- /* Skip reading if the pressure components are out of range */9494- if (unlikely(tsdata.z1 == 0 || tsdata.z2 > MAX_12BIT))9595- goto out;9696- if (unlikely(tsdata.z1 >= tsdata.z2))9797- goto out;9898-9999- /*100100- * Skip point if this is a pen down with the exact same values as101101- * the value before pen-up - that implies SPI fed us stale data102102- */103103- if (!ts->pen_down &&104104- ts->in_x == tsdata.x && ts->in_y == tsdata.y &&105105- ts->in_z1 == tsdata.z1 && ts->in_z2 == tsdata.z2) {106106- goto out;107107- }108108-109109- /*110110- * At this point we are happy we have a valid and useful reading.111111- * Remember it for later comparisons. We may now begin downsampling.112112- */113113- ts->in_x = tsdata.x;114114- ts->in_y = tsdata.y;115115- ts->in_z1 = tsdata.z1;116116- ts->in_z2 = tsdata.z2;117117-118118- /* Compute touch pressure resistance using equation #1 */119119- pressure = tsdata.x * (tsdata.z2 - tsdata.z1) / tsdata.z1;120120- pressure = pressure * ts->x_plate_ohm / 4096;121121- if (unlikely(pressure > MAX_12BIT))122122- goto out;123123-124124- spin_lock_irqsave(&ts->lock, flags);125125-126126- tsc2005_update_pen_state(ts, tsdata.x, tsdata.y, pressure);127127- mod_timer(&ts->penup_timer,128128- jiffies + msecs_to_jiffies(TSC2005_PENUP_TIME_MS));129129-130130- spin_unlock_irqrestore(&ts->lock, flags);131131-132132- ts->last_valid_interrupt = jiffies;133133-out:134134- return IRQ_HANDLED;135135-}136136-137137-static void tsc2005_penup_timer(unsigned long data)138138-{139139- struct tsc2005 *ts = (struct tsc2005 *)data;140140- unsigned long flags;141141-142142- spin_lock_irqsave(&ts->lock, flags);143143- tsc2005_update_pen_state(ts, 0, 0, 0);144144- spin_unlock_irqrestore(&ts->lock, flags);145145-}146146-147147-static void tsc2005_start_scan(struct tsc2005 *ts)148148-{149149- regmap_write(ts->regmap, TSC2005_REG_CFR0, TSC2005_CFR0_INITVALUE);150150- regmap_write(ts->regmap, TSC2005_REG_CFR1, TSC2005_CFR1_INITVALUE);151151- regmap_write(ts->regmap, TSC2005_REG_CFR2, TSC2005_CFR2_INITVALUE);152152- tsc2005_cmd(ts, TSC2005_CMD_NORMAL);153153-}154154-155155-static void tsc2005_stop_scan(struct tsc2005 *ts)156156-{157157- tsc2005_cmd(ts, TSC2005_CMD_STOP);158158-}159159-160160-static void tsc2005_set_reset(struct tsc2005 *ts, bool enable)161161-{162162- if (ts->reset_gpio)163163- gpiod_set_value_cansleep(ts->reset_gpio, enable);164164- else if (ts->set_reset)165165- ts->set_reset(enable);166166-}167167-168168-/* must be called with ts->mutex held */169169-static void __tsc2005_disable(struct tsc2005 *ts)170170-{171171- tsc2005_stop_scan(ts);172172-173173- disable_irq(ts->spi->irq);174174- del_timer_sync(&ts->penup_timer);175175-176176- cancel_delayed_work_sync(&ts->esd_work);177177-178178- enable_irq(ts->spi->irq);179179-}180180-181181-/* must be called with ts->mutex held */182182-static void __tsc2005_enable(struct tsc2005 *ts)183183-{184184- tsc2005_start_scan(ts);185185-186186- if (ts->esd_timeout && (ts->set_reset || ts->reset_gpio)) {187187- ts->last_valid_interrupt = jiffies;188188- schedule_delayed_work(&ts->esd_work,189189- round_jiffies_relative(190190- msecs_to_jiffies(ts->esd_timeout)));191191- }192192-193193-}194194-195195-static ssize_t tsc2005_selftest_show(struct device *dev,196196- struct device_attribute *attr,197197- char *buf)198198-{199199- struct tsc2005 *ts = dev_get_drvdata(dev);200200- unsigned int temp_high;201201- unsigned int temp_high_orig;202202- unsigned int temp_high_test;203203- bool success = true;204204- int error;205205-206206- mutex_lock(&ts->mutex);207207-208208- /*209209- * Test TSC2005 communications via temp high register.210210- */211211- __tsc2005_disable(ts);212212-213213- error = regmap_read(ts->regmap, TSC2005_REG_TEMP_HIGH, &temp_high_orig);214214- if (error) {215215- dev_warn(dev, "selftest failed: read error %d\n", error);216216- success = false;217217- goto out;218218- }219219-220220- temp_high_test = (temp_high_orig - 1) & MAX_12BIT;221221-222222- error = regmap_write(ts->regmap, TSC2005_REG_TEMP_HIGH, temp_high_test);223223- if (error) {224224- dev_warn(dev, "selftest failed: write error %d\n", error);225225- success = false;226226- goto out;227227- }228228-229229- error = regmap_read(ts->regmap, TSC2005_REG_TEMP_HIGH, &temp_high);230230- if (error) {231231- dev_warn(dev, "selftest failed: read error %d after write\n",232232- error);233233- success = false;234234- goto out;235235- }236236-237237- if (temp_high != temp_high_test) {238238- dev_warn(dev, "selftest failed: %d != %d\n",239239- temp_high, temp_high_test);240240- success = false;241241- }242242-243243- /* hardware reset */244244- tsc2005_set_reset(ts, false);245245- usleep_range(100, 500); /* only 10us required */246246- tsc2005_set_reset(ts, true);247247-248248- if (!success)249249- goto out;250250-251251- /* test that the reset really happened */252252- error = regmap_read(ts->regmap, TSC2005_REG_TEMP_HIGH, &temp_high);253253- if (error) {254254- dev_warn(dev, "selftest failed: read error %d after reset\n",255255- error);256256- success = false;257257- goto out;258258- }259259-260260- if (temp_high != temp_high_orig) {261261- dev_warn(dev, "selftest failed after reset: %d != %d\n",262262- temp_high, temp_high_orig);263263- success = false;264264- }265265-266266-out:267267- __tsc2005_enable(ts);268268- mutex_unlock(&ts->mutex);269269-270270- return sprintf(buf, "%d\n", success);271271-}272272-273273-static DEVICE_ATTR(selftest, S_IRUGO, tsc2005_selftest_show, NULL);274274-275275-static struct attribute *tsc2005_attrs[] = {276276- &dev_attr_selftest.attr,277277- NULL278278-};279279-280280-static umode_t tsc2005_attr_is_visible(struct kobject *kobj,281281- struct attribute *attr, int n)282282-{283283- struct device *dev = container_of(kobj, struct device, kobj);284284- struct tsc2005 *ts = dev_get_drvdata(dev);285285- umode_t mode = attr->mode;286286-287287- if (attr == &dev_attr_selftest.attr) {288288- if (!ts->set_reset && !ts->reset_gpio)289289- mode = 0;290290- }291291-292292- return mode;293293-}294294-295295-static const struct attribute_group tsc2005_attr_group = {296296- .is_visible = tsc2005_attr_is_visible,297297- .attrs = tsc2005_attrs,298298-};299299-300300-static void tsc2005_esd_work(struct work_struct *work)301301-{302302- struct tsc2005 *ts = container_of(work, struct tsc2005, esd_work.work);303303- int error;304304- unsigned int r;305305-306306- if (!mutex_trylock(&ts->mutex)) {307307- /*308308- * If the mutex is taken, it means that disable or enable is in309309- * progress. In that case just reschedule the work. If the work310310- * is not needed, it will be canceled by disable.311311- */312312- goto reschedule;313313- }314314-315315- if (time_is_after_jiffies(ts->last_valid_interrupt +316316- msecs_to_jiffies(ts->esd_timeout)))317317- goto out;318318-319319- /* We should be able to read register without disabling interrupts. */320320- error = regmap_read(ts->regmap, TSC2005_REG_CFR0, &r);321321- if (!error &&322322- !((r ^ TSC2005_CFR0_INITVALUE) & TSC2005_CFR0_RW_MASK)) {323323- goto out;324324- }325325-326326- /*327327- * If we could not read our known value from configuration register 0328328- * then we should reset the controller as if from power-up and start329329- * scanning again.330330- */331331- dev_info(&ts->spi->dev, "TSC2005 not responding - resetting\n");332332-333333- disable_irq(ts->spi->irq);334334- del_timer_sync(&ts->penup_timer);335335-336336- tsc2005_update_pen_state(ts, 0, 0, 0);337337-338338- tsc2005_set_reset(ts, false);339339- usleep_range(100, 500); /* only 10us required */340340- tsc2005_set_reset(ts, true);341341-342342- enable_irq(ts->spi->irq);343343- tsc2005_start_scan(ts);344344-345345-out:346346- mutex_unlock(&ts->mutex);347347-reschedule:348348- /* re-arm the watchdog */349349- schedule_delayed_work(&ts->esd_work,350350- round_jiffies_relative(351351- msecs_to_jiffies(ts->esd_timeout)));352352-}353353-354354-static int tsc2005_open(struct input_dev *input)355355-{356356- struct tsc2005 *ts = input_get_drvdata(input);357357-358358- mutex_lock(&ts->mutex);359359-360360- if (!ts->suspended)361361- __tsc2005_enable(ts);362362-363363- ts->opened = true;364364-365365- mutex_unlock(&ts->mutex);366366-367367- return 0;368368-}369369-370370-static void tsc2005_close(struct input_dev *input)371371-{372372- struct tsc2005 *ts = input_get_drvdata(input);373373-374374- mutex_lock(&ts->mutex);375375-376376- if (!ts->suspended)377377- __tsc2005_disable(ts);378378-379379- ts->opened = false;380380-381381- mutex_unlock(&ts->mutex);382382-}383383-384211static int tsc2005_probe(struct spi_device *spi)385212{386386- const struct tsc2005_platform_data *pdata = dev_get_platdata(&spi->dev);387387- struct device_node *np = spi->dev.of_node;388388-389389- struct tsc2005 *ts;390390- struct input_dev *input_dev;391391- unsigned int max_x = MAX_12BIT;392392- unsigned int max_y = MAX_12BIT;393393- unsigned int max_p = MAX_12BIT;394394- unsigned int fudge_x = TSC2005_DEF_X_FUZZ;395395- unsigned int fudge_y = TSC2005_DEF_Y_FUZZ;396396- unsigned int fudge_p = TSC2005_DEF_P_FUZZ;397397- unsigned int x_plate_ohm = TSC2005_DEF_RESISTOR;398398- unsigned int esd_timeout;399213 int error;400400-401401- if (!np && !pdata) {402402- dev_err(&spi->dev, "no platform data\n");403403- return -ENODEV;404404- }405405-406406- if (spi->irq <= 0) {407407- dev_err(&spi->dev, "no irq\n");408408- return -ENODEV;409409- }410410-411411- if (pdata) {412412- fudge_x = pdata->ts_x_fudge;413413- fudge_y = pdata->ts_y_fudge;414414- fudge_p = pdata->ts_pressure_fudge;415415- max_x = pdata->ts_x_max;416416- max_y = pdata->ts_y_max;417417- max_p = pdata->ts_pressure_max;418418- x_plate_ohm = pdata->ts_x_plate_ohm;419419- esd_timeout = pdata->esd_timeout_ms;420420- } else {421421- x_plate_ohm = TSC2005_DEF_RESISTOR;422422- of_property_read_u32(np, "ti,x-plate-ohms", &x_plate_ohm);423423- esd_timeout = 0;424424- of_property_read_u32(np, "ti,esd-recovery-timeout-ms",425425- &esd_timeout);426426- }427214428215 spi->mode = SPI_MODE_0;429216 spi->bits_per_word = 8;···62593 if (error)63594 return error;645956565- ts = devm_kzalloc(&spi->dev, sizeof(*ts), GFP_KERNEL);6666- if (!ts)6767- return -ENOMEM;6868-6969- input_dev = devm_input_allocate_device(&spi->dev);7070- if (!input_dev)7171- return -ENOMEM;7272-7373- ts->spi = spi;7474- ts->idev = input_dev;7575-7676- ts->regmap = devm_regmap_init_spi(spi, &tsc2005_regmap_config);7777- if (IS_ERR(ts->regmap))7878- return PTR_ERR(ts->regmap);7979-8080- ts->x_plate_ohm = x_plate_ohm;8181- ts->esd_timeout = esd_timeout;8282-8383- ts->reset_gpio = devm_gpiod_get_optional(&spi->dev, "reset",8484- GPIOD_OUT_HIGH);8585- if (IS_ERR(ts->reset_gpio)) {8686- error = PTR_ERR(ts->reset_gpio);8787- dev_err(&spi->dev, "error acquiring reset gpio: %d\n", error);8888- return error;8989- }9090-9191- ts->vio = devm_regulator_get_optional(&spi->dev, "vio");9292- if (IS_ERR(ts->vio)) {9393- error = PTR_ERR(ts->vio);9494- dev_err(&spi->dev, "vio regulator missing (%d)", error);9595- return error;9696- }9797-9898- if (!ts->reset_gpio && pdata)9999- ts->set_reset = pdata->set_reset;100100-101101- mutex_init(&ts->mutex);102102-103103- spin_lock_init(&ts->lock);104104- setup_timer(&ts->penup_timer, tsc2005_penup_timer, (unsigned long)ts);105105-106106- INIT_DELAYED_WORK(&ts->esd_work, tsc2005_esd_work);107107-108108- snprintf(ts->phys, sizeof(ts->phys),109109- "%s/input-ts", dev_name(&spi->dev));110110-111111- input_dev->name = "TSC2005 touchscreen";112112- input_dev->phys = ts->phys;113113- input_dev->id.bustype = BUS_SPI;114114- input_dev->dev.parent = &spi->dev;115115- input_dev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);116116- input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);117117-118118- input_set_abs_params(input_dev, ABS_X, 0, max_x, fudge_x, 0);119119- input_set_abs_params(input_dev, ABS_Y, 0, max_y, fudge_y, 0);120120- input_set_abs_params(input_dev, ABS_PRESSURE, 0, max_p, fudge_p, 0);121121-122122- if (np)123123- touchscreen_parse_properties(input_dev, false);124124-125125- input_dev->open = tsc2005_open;126126- input_dev->close = tsc2005_close;127127-128128- input_set_drvdata(input_dev, ts);129129-130130- /* Ensure the touchscreen is off */131131- tsc2005_stop_scan(ts);132132-133133- error = devm_request_threaded_irq(&spi->dev, spi->irq, NULL,134134- tsc2005_irq_thread,135135- IRQF_TRIGGER_RISING | IRQF_ONESHOT,136136- "tsc2005", ts);137137- if (error) {138138- dev_err(&spi->dev, "Failed to request irq, err: %d\n", error);139139- return error;140140- }141141-142142- /* enable regulator for DT */143143- if (ts->vio) {144144- error = regulator_enable(ts->vio);145145- if (error)146146- return error;147147- }148148-149149- dev_set_drvdata(&spi->dev, ts);150150- error = sysfs_create_group(&spi->dev.kobj, &tsc2005_attr_group);151151- if (error) {152152- dev_err(&spi->dev,153153- "Failed to create sysfs attributes, err: %d\n", error);154154- goto disable_regulator;155155- }156156-157157- error = input_register_device(ts->idev);158158- if (error) {159159- dev_err(&spi->dev,160160- "Failed to register input device, err: %d\n", error);161161- goto err_remove_sysfs;162162- }163163-164164- irq_set_irq_wake(spi->irq, 1);165165- return 0;166166-167167-err_remove_sysfs:168168- sysfs_remove_group(&spi->dev.kobj, &tsc2005_attr_group);169169-disable_regulator:170170- if (ts->vio)171171- regulator_disable(ts->vio);172172- return error;596596+ return tsc200x_probe(&spi->dev, spi->irq, BUS_SPI,597597+ devm_regmap_init_spi(spi, &tsc200x_regmap_config),598598+ tsc2005_cmd);173599}174600175601static int tsc2005_remove(struct spi_device *spi)176602{177177- struct tsc2005 *ts = dev_get_drvdata(&spi->dev);178178-179179- sysfs_remove_group(&spi->dev.kobj, &tsc2005_attr_group);180180-181181- if (ts->vio)182182- regulator_disable(ts->vio);183183-184184- return 0;603603+ return tsc200x_remove(&spi->dev);185604}186186-187187-static int __maybe_unused tsc2005_suspend(struct device *dev)188188-{189189- struct tsc2005 *ts = dev_get_drvdata(dev);190190-191191- mutex_lock(&ts->mutex);192192-193193- if (!ts->suspended && ts->opened)194194- __tsc2005_disable(ts);195195-196196- ts->suspended = true;197197-198198- mutex_unlock(&ts->mutex);199199-200200- return 0;201201-}202202-203203-static int __maybe_unused tsc2005_resume(struct device *dev)204204-{205205- struct tsc2005 *ts = dev_get_drvdata(dev);206206-207207- mutex_lock(&ts->mutex);208208-209209- if (ts->suspended && ts->opened)210210- __tsc2005_enable(ts);211211-212212- ts->suspended = false;213213-214214- mutex_unlock(&ts->mutex);215215-216216- return 0;217217-}218218-219219-static SIMPLE_DEV_PM_OPS(tsc2005_pm_ops, tsc2005_suspend, tsc2005_resume);220605221606static struct spi_driver tsc2005_driver = {222607 .driver = {223608 .name = "tsc2005",224609 .owner = THIS_MODULE,225225- .pm = &tsc2005_pm_ops,610610+ .pm = &tsc200x_pm_ops,226611 },227612 .probe = tsc2005_probe,228613 .remove = tsc2005_remove,229614};230230-231615module_spi_driver(tsc2005_driver);232616233233-MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>");617617+MODULE_AUTHOR("Michael Welling <mwelling@ieee.org>");234618MODULE_DESCRIPTION("TSC2005 Touchscreen Driver");235619MODULE_LICENSE("GPL");236620MODULE_ALIAS("spi:tsc2005");
+665
drivers/input/touchscreen/tsc200x-core.c
···11+/*22+ * TSC2004/TSC2005 touchscreen driver core33+ *44+ * Copyright (C) 2006-2010 Nokia Corporation55+ * Copyright (C) 2015 QWERTY Embedded Design66+ * Copyright (C) 2015 EMAC Inc.77+ *88+ * Author: Lauri Leukkunen <lauri.leukkunen@nokia.com>99+ * based on TSC2301 driver by Klaus K. Pedersen <klaus.k.pedersen@nokia.com>1010+ *1111+ * This program is free software; you can redistribute it and/or modify1212+ * it under the terms of the GNU General Public License as published by1313+ * the Free Software Foundation; either version 2 of the License, or1414+ * (at your option) any later version.1515+ *1616+ * This program is distributed in the hope that it will be useful,1717+ * but WITHOUT ANY WARRANTY; without even the implied warranty of1818+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the1919+ * GNU General Public License for more details.2020+ */2121+2222+#include <linux/kernel.h>2323+#include <linux/module.h>2424+#include <linux/input.h>2525+#include <linux/input/touchscreen.h>2626+#include <linux/interrupt.h>2727+#include <linux/delay.h>2828+#include <linux/pm.h>2929+#include <linux/of.h>3030+#include <linux/spi/tsc2005.h>3131+#include <linux/regulator/consumer.h>3232+#include <linux/regmap.h>3333+#include <linux/gpio/consumer.h>3434+#include "tsc200x-core.h"3535+3636+/*3737+ * The touchscreen interface operates as follows:3838+ *3939+ * 1) Pen is pressed against the touchscreen.4040+ * 2) TSC200X performs AD conversion.4141+ * 3) After the conversion is done TSC200X drives DAV line down.4242+ * 4) GPIO IRQ is received and tsc200x_irq_thread() is scheduled.4343+ * 5) tsc200x_irq_thread() queues up a transfer to fetch the x, y, z1, z24444+ * values.4545+ * 6) tsc200x_irq_thread() reports coordinates to input layer and sets up4646+ * tsc200x_penup_timer() to be called after TSC200X_PENUP_TIME_MS (40ms).4747+ * 7) When the penup timer expires, there have not been touch or DAV interrupts4848+ * during the last 40ms which means the pen has been lifted.4949+ *5050+ * ESD recovery via a hardware reset is done if the TSC200X doesn't respond5151+ * after a configurable period (in ms) of activity. If esd_timeout is 0, the5252+ * watchdog is disabled.5353+ */5454+5555+static const struct regmap_range tsc200x_writable_ranges[] = {5656+ regmap_reg_range(TSC200X_REG_AUX_HIGH, TSC200X_REG_CFR2),5757+};5858+5959+static const struct regmap_access_table tsc200x_writable_table = {6060+ .yes_ranges = tsc200x_writable_ranges,6161+ .n_yes_ranges = ARRAY_SIZE(tsc200x_writable_ranges),6262+};6363+6464+const struct regmap_config tsc200x_regmap_config = {6565+ .reg_bits = 8,6666+ .val_bits = 16,6767+ .reg_stride = 0x08,6868+ .max_register = 0x78,6969+ .read_flag_mask = TSC200X_REG_READ,7070+ .write_flag_mask = TSC200X_REG_PND0,7171+ .wr_table = &tsc200x_writable_table,7272+ .use_single_rw = true,7373+};7474+EXPORT_SYMBOL_GPL(tsc200x_regmap_config);7575+7676+struct tsc200x_data {7777+ u16 x;7878+ u16 y;7979+ u16 z1;8080+ u16 z2;8181+} __packed;8282+#define TSC200X_DATA_REGS 48383+8484+struct tsc200x {8585+ struct device *dev;8686+ struct regmap *regmap;8787+ __u16 bustype;8888+8989+ struct input_dev *idev;9090+ char phys[32];9191+9292+ struct mutex mutex;9393+9494+ /* raw copy of previous x,y,z */9595+ int in_x;9696+ int in_y;9797+ int in_z1;9898+ int in_z2;9999+100100+ spinlock_t lock;101101+ struct timer_list penup_timer;102102+103103+ unsigned int esd_timeout;104104+ struct delayed_work esd_work;105105+ unsigned long last_valid_interrupt;106106+107107+ unsigned int x_plate_ohm;108108+109109+ bool opened;110110+ bool suspended;111111+112112+ bool pen_down;113113+114114+ struct regulator *vio;115115+116116+ struct gpio_desc *reset_gpio;117117+ void (*set_reset)(bool enable);118118+ int (*tsc200x_cmd)(struct device *dev, u8 cmd);119119+ int irq;120120+};121121+122122+static void tsc200x_update_pen_state(struct tsc200x *ts,123123+ int x, int y, int pressure)124124+{125125+ if (pressure) {126126+ input_report_abs(ts->idev, ABS_X, x);127127+ input_report_abs(ts->idev, ABS_Y, y);128128+ input_report_abs(ts->idev, ABS_PRESSURE, pressure);129129+ if (!ts->pen_down) {130130+ input_report_key(ts->idev, BTN_TOUCH, !!pressure);131131+ ts->pen_down = true;132132+ }133133+ } else {134134+ input_report_abs(ts->idev, ABS_PRESSURE, 0);135135+ if (ts->pen_down) {136136+ input_report_key(ts->idev, BTN_TOUCH, 0);137137+ ts->pen_down = false;138138+ }139139+ }140140+ input_sync(ts->idev);141141+ dev_dbg(ts->dev, "point(%4d,%4d), pressure (%4d)\n", x, y,142142+ pressure);143143+}144144+145145+static irqreturn_t tsc200x_irq_thread(int irq, void *_ts)146146+{147147+ struct tsc200x *ts = _ts;148148+ unsigned long flags;149149+ unsigned int pressure;150150+ struct tsc200x_data tsdata;151151+ int error;152152+153153+ /* read the coordinates */154154+ error = regmap_bulk_read(ts->regmap, TSC200X_REG_X, &tsdata,155155+ TSC200X_DATA_REGS);156156+ if (unlikely(error))157157+ goto out;158158+159159+ /* validate position */160160+ if (unlikely(tsdata.x > MAX_12BIT || tsdata.y > MAX_12BIT))161161+ goto out;162162+163163+ /* Skip reading if the pressure components are out of range */164164+ if (unlikely(tsdata.z1 == 0 || tsdata.z2 > MAX_12BIT))165165+ goto out;166166+ if (unlikely(tsdata.z1 >= tsdata.z2))167167+ goto out;168168+169169+ /*170170+ * Skip point if this is a pen down with the exact same values as171171+ * the value before pen-up - that implies SPI fed us stale data172172+ */173173+ if (!ts->pen_down &&174174+ ts->in_x == tsdata.x && ts->in_y == tsdata.y &&175175+ ts->in_z1 == tsdata.z1 && ts->in_z2 == tsdata.z2) {176176+ goto out;177177+ }178178+179179+ /*180180+ * At this point we are happy we have a valid and useful reading.181181+ * Remember it for later comparisons. We may now begin downsampling.182182+ */183183+ ts->in_x = tsdata.x;184184+ ts->in_y = tsdata.y;185185+ ts->in_z1 = tsdata.z1;186186+ ts->in_z2 = tsdata.z2;187187+188188+ /* Compute touch pressure resistance using equation #1 */189189+ pressure = tsdata.x * (tsdata.z2 - tsdata.z1) / tsdata.z1;190190+ pressure = pressure * ts->x_plate_ohm / 4096;191191+ if (unlikely(pressure > MAX_12BIT))192192+ goto out;193193+194194+ spin_lock_irqsave(&ts->lock, flags);195195+196196+ tsc200x_update_pen_state(ts, tsdata.x, tsdata.y, pressure);197197+ mod_timer(&ts->penup_timer,198198+ jiffies + msecs_to_jiffies(TSC200X_PENUP_TIME_MS));199199+200200+ spin_unlock_irqrestore(&ts->lock, flags);201201+202202+ ts->last_valid_interrupt = jiffies;203203+out:204204+ return IRQ_HANDLED;205205+}206206+207207+static void tsc200x_penup_timer(unsigned long data)208208+{209209+ struct tsc200x *ts = (struct tsc200x *)data;210210+ unsigned long flags;211211+212212+ spin_lock_irqsave(&ts->lock, flags);213213+ tsc200x_update_pen_state(ts, 0, 0, 0);214214+ spin_unlock_irqrestore(&ts->lock, flags);215215+}216216+217217+static void tsc200x_start_scan(struct tsc200x *ts)218218+{219219+ regmap_write(ts->regmap, TSC200X_REG_CFR0, TSC200X_CFR0_INITVALUE);220220+ regmap_write(ts->regmap, TSC200X_REG_CFR1, TSC200X_CFR1_INITVALUE);221221+ regmap_write(ts->regmap, TSC200X_REG_CFR2, TSC200X_CFR2_INITVALUE);222222+ ts->tsc200x_cmd(ts->dev, TSC200X_CMD_NORMAL);223223+}224224+225225+static void tsc200x_stop_scan(struct tsc200x *ts)226226+{227227+ ts->tsc200x_cmd(ts->dev, TSC200X_CMD_STOP);228228+}229229+230230+static void tsc200x_set_reset(struct tsc200x *ts, bool enable)231231+{232232+ if (ts->reset_gpio)233233+ gpiod_set_value_cansleep(ts->reset_gpio, enable);234234+ else if (ts->set_reset)235235+ ts->set_reset(enable);236236+}237237+238238+/* must be called with ts->mutex held */239239+static void __tsc200x_disable(struct tsc200x *ts)240240+{241241+ tsc200x_stop_scan(ts);242242+243243+ disable_irq(ts->irq);244244+ del_timer_sync(&ts->penup_timer);245245+246246+ cancel_delayed_work_sync(&ts->esd_work);247247+248248+ enable_irq(ts->irq);249249+}250250+251251+/* must be called with ts->mutex held */252252+static void __tsc200x_enable(struct tsc200x *ts)253253+{254254+ tsc200x_start_scan(ts);255255+256256+ if (ts->esd_timeout && (ts->set_reset || ts->reset_gpio)) {257257+ ts->last_valid_interrupt = jiffies;258258+ schedule_delayed_work(&ts->esd_work,259259+ round_jiffies_relative(260260+ msecs_to_jiffies(ts->esd_timeout)));261261+ }262262+}263263+264264+static ssize_t tsc200x_selftest_show(struct device *dev,265265+ struct device_attribute *attr,266266+ char *buf)267267+{268268+ struct tsc200x *ts = dev_get_drvdata(dev);269269+ unsigned int temp_high;270270+ unsigned int temp_high_orig;271271+ unsigned int temp_high_test;272272+ bool success = true;273273+ int error;274274+275275+ mutex_lock(&ts->mutex);276276+277277+ /*278278+ * Test TSC200X communications via temp high register.279279+ */280280+ __tsc200x_disable(ts);281281+282282+ error = regmap_read(ts->regmap, TSC200X_REG_TEMP_HIGH, &temp_high_orig);283283+ if (error) {284284+ dev_warn(dev, "selftest failed: read error %d\n", error);285285+ success = false;286286+ goto out;287287+ }288288+289289+ temp_high_test = (temp_high_orig - 1) & MAX_12BIT;290290+291291+ error = regmap_write(ts->regmap, TSC200X_REG_TEMP_HIGH, temp_high_test);292292+ if (error) {293293+ dev_warn(dev, "selftest failed: write error %d\n", error);294294+ success = false;295295+ goto out;296296+ }297297+298298+ error = regmap_read(ts->regmap, TSC200X_REG_TEMP_HIGH, &temp_high);299299+ if (error) {300300+ dev_warn(dev, "selftest failed: read error %d after write\n",301301+ error);302302+ success = false;303303+ goto out;304304+ }305305+306306+ if (temp_high != temp_high_test) {307307+ dev_warn(dev, "selftest failed: %d != %d\n",308308+ temp_high, temp_high_test);309309+ success = false;310310+ }311311+312312+ /* hardware reset */313313+ tsc200x_set_reset(ts, false);314314+ usleep_range(100, 500); /* only 10us required */315315+ tsc200x_set_reset(ts, true);316316+317317+ if (!success)318318+ goto out;319319+320320+ /* test that the reset really happened */321321+ error = regmap_read(ts->regmap, TSC200X_REG_TEMP_HIGH, &temp_high);322322+ if (error) {323323+ dev_warn(dev, "selftest failed: read error %d after reset\n",324324+ error);325325+ success = false;326326+ goto out;327327+ }328328+329329+ if (temp_high != temp_high_orig) {330330+ dev_warn(dev, "selftest failed after reset: %d != %d\n",331331+ temp_high, temp_high_orig);332332+ success = false;333333+ }334334+335335+out:336336+ __tsc200x_enable(ts);337337+ mutex_unlock(&ts->mutex);338338+339339+ return sprintf(buf, "%d\n", success);340340+}341341+342342+static DEVICE_ATTR(selftest, S_IRUGO, tsc200x_selftest_show, NULL);343343+344344+static struct attribute *tsc200x_attrs[] = {345345+ &dev_attr_selftest.attr,346346+ NULL347347+};348348+349349+static umode_t tsc200x_attr_is_visible(struct kobject *kobj,350350+ struct attribute *attr, int n)351351+{352352+ struct device *dev = container_of(kobj, struct device, kobj);353353+ struct tsc200x *ts = dev_get_drvdata(dev);354354+ umode_t mode = attr->mode;355355+356356+ if (attr == &dev_attr_selftest.attr) {357357+ if (!ts->set_reset && !ts->reset_gpio)358358+ mode = 0;359359+ }360360+361361+ return mode;362362+}363363+364364+static const struct attribute_group tsc200x_attr_group = {365365+ .is_visible = tsc200x_attr_is_visible,366366+ .attrs = tsc200x_attrs,367367+};368368+369369+static void tsc200x_esd_work(struct work_struct *work)370370+{371371+ struct tsc200x *ts = container_of(work, struct tsc200x, esd_work.work);372372+ int error;373373+ unsigned int r;374374+375375+ if (!mutex_trylock(&ts->mutex)) {376376+ /*377377+ * If the mutex is taken, it means that disable or enable is in378378+ * progress. In that case just reschedule the work. If the work379379+ * is not needed, it will be canceled by disable.380380+ */381381+ goto reschedule;382382+ }383383+384384+ if (time_is_after_jiffies(ts->last_valid_interrupt +385385+ msecs_to_jiffies(ts->esd_timeout)))386386+ goto out;387387+388388+ /* We should be able to read register without disabling interrupts. */389389+ error = regmap_read(ts->regmap, TSC200X_REG_CFR0, &r);390390+ if (!error &&391391+ !((r ^ TSC200X_CFR0_INITVALUE) & TSC200X_CFR0_RW_MASK)) {392392+ goto out;393393+ }394394+395395+ /*396396+ * If we could not read our known value from configuration register 0397397+ * then we should reset the controller as if from power-up and start398398+ * scanning again.399399+ */400400+ dev_info(ts->dev, "TSC200X not responding - resetting\n");401401+402402+ disable_irq(ts->irq);403403+ del_timer_sync(&ts->penup_timer);404404+405405+ tsc200x_update_pen_state(ts, 0, 0, 0);406406+407407+ tsc200x_set_reset(ts, false);408408+ usleep_range(100, 500); /* only 10us required */409409+ tsc200x_set_reset(ts, true);410410+411411+ enable_irq(ts->irq);412412+ tsc200x_start_scan(ts);413413+414414+out:415415+ mutex_unlock(&ts->mutex);416416+reschedule:417417+ /* re-arm the watchdog */418418+ schedule_delayed_work(&ts->esd_work,419419+ round_jiffies_relative(420420+ msecs_to_jiffies(ts->esd_timeout)));421421+}422422+423423+static int tsc200x_open(struct input_dev *input)424424+{425425+ struct tsc200x *ts = input_get_drvdata(input);426426+427427+ mutex_lock(&ts->mutex);428428+429429+ if (!ts->suspended)430430+ __tsc200x_enable(ts);431431+432432+ ts->opened = true;433433+434434+ mutex_unlock(&ts->mutex);435435+436436+ return 0;437437+}438438+439439+static void tsc200x_close(struct input_dev *input)440440+{441441+ struct tsc200x *ts = input_get_drvdata(input);442442+443443+ mutex_lock(&ts->mutex);444444+445445+ if (!ts->suspended)446446+ __tsc200x_disable(ts);447447+448448+ ts->opened = false;449449+450450+ mutex_unlock(&ts->mutex);451451+}452452+453453+int tsc200x_probe(struct device *dev, int irq, __u16 bustype,454454+ struct regmap *regmap,455455+ int (*tsc200x_cmd)(struct device *dev, u8 cmd))456456+{457457+ const struct tsc2005_platform_data *pdata = dev_get_platdata(dev);458458+ struct device_node *np = dev->of_node;459459+460460+ struct tsc200x *ts;461461+ struct input_dev *input_dev;462462+ unsigned int max_x = MAX_12BIT;463463+ unsigned int max_y = MAX_12BIT;464464+ unsigned int max_p = MAX_12BIT;465465+ unsigned int fudge_x = TSC200X_DEF_X_FUZZ;466466+ unsigned int fudge_y = TSC200X_DEF_Y_FUZZ;467467+ unsigned int fudge_p = TSC200X_DEF_P_FUZZ;468468+ unsigned int x_plate_ohm = TSC200X_DEF_RESISTOR;469469+ unsigned int esd_timeout;470470+ int error;471471+472472+ if (!np && !pdata) {473473+ dev_err(dev, "no platform data\n");474474+ return -ENODEV;475475+ }476476+477477+ if (irq <= 0) {478478+ dev_err(dev, "no irq\n");479479+ return -ENODEV;480480+ }481481+482482+ if (IS_ERR(regmap))483483+ return PTR_ERR(regmap);484484+485485+ if (!tsc200x_cmd) {486486+ dev_err(dev, "no cmd function\n");487487+ return -ENODEV;488488+ }489489+490490+ if (pdata) {491491+ fudge_x = pdata->ts_x_fudge;492492+ fudge_y = pdata->ts_y_fudge;493493+ fudge_p = pdata->ts_pressure_fudge;494494+ max_x = pdata->ts_x_max;495495+ max_y = pdata->ts_y_max;496496+ max_p = pdata->ts_pressure_max;497497+ x_plate_ohm = pdata->ts_x_plate_ohm;498498+ esd_timeout = pdata->esd_timeout_ms;499499+ } else {500500+ x_plate_ohm = TSC200X_DEF_RESISTOR;501501+ of_property_read_u32(np, "ti,x-plate-ohms", &x_plate_ohm);502502+ esd_timeout = 0;503503+ of_property_read_u32(np, "ti,esd-recovery-timeout-ms",504504+ &esd_timeout);505505+ }506506+507507+ ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL);508508+ if (!ts)509509+ return -ENOMEM;510510+511511+ input_dev = devm_input_allocate_device(dev);512512+ if (!input_dev)513513+ return -ENOMEM;514514+515515+ ts->irq = irq;516516+ ts->dev = dev;517517+ ts->idev = input_dev;518518+ ts->regmap = regmap;519519+ ts->tsc200x_cmd = tsc200x_cmd;520520+ ts->x_plate_ohm = x_plate_ohm;521521+ ts->esd_timeout = esd_timeout;522522+523523+ ts->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);524524+ if (IS_ERR(ts->reset_gpio)) {525525+ error = PTR_ERR(ts->reset_gpio);526526+ dev_err(dev, "error acquiring reset gpio: %d\n", error);527527+ return error;528528+ }529529+530530+ ts->vio = devm_regulator_get_optional(dev, "vio");531531+ if (IS_ERR(ts->vio)) {532532+ error = PTR_ERR(ts->vio);533533+ dev_err(dev, "vio regulator missing (%d)", error);534534+ return error;535535+ }536536+537537+ if (!ts->reset_gpio && pdata)538538+ ts->set_reset = pdata->set_reset;539539+540540+ mutex_init(&ts->mutex);541541+542542+ spin_lock_init(&ts->lock);543543+ setup_timer(&ts->penup_timer, tsc200x_penup_timer, (unsigned long)ts);544544+545545+ INIT_DELAYED_WORK(&ts->esd_work, tsc200x_esd_work);546546+547547+ snprintf(ts->phys, sizeof(ts->phys),548548+ "%s/input-ts", dev_name(dev));549549+550550+ input_dev->name = "TSC200X touchscreen";551551+ input_dev->phys = ts->phys;552552+ input_dev->id.bustype = bustype;553553+ input_dev->dev.parent = dev;554554+ input_dev->evbit[0] = BIT(EV_ABS) | BIT(EV_KEY);555555+ input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);556556+557557+ input_set_abs_params(input_dev, ABS_X, 0, max_x, fudge_x, 0);558558+ input_set_abs_params(input_dev, ABS_Y, 0, max_y, fudge_y, 0);559559+ input_set_abs_params(input_dev, ABS_PRESSURE, 0, max_p, fudge_p, 0);560560+561561+ if (np)562562+ touchscreen_parse_properties(input_dev, false);563563+564564+ input_dev->open = tsc200x_open;565565+ input_dev->close = tsc200x_close;566566+567567+ input_set_drvdata(input_dev, ts);568568+569569+ /* Ensure the touchscreen is off */570570+ tsc200x_stop_scan(ts);571571+572572+ error = devm_request_threaded_irq(dev, irq, NULL,573573+ tsc200x_irq_thread,574574+ IRQF_TRIGGER_RISING | IRQF_ONESHOT,575575+ "tsc200x", ts);576576+ if (error) {577577+ dev_err(dev, "Failed to request irq, err: %d\n", error);578578+ return error;579579+ }580580+581581+ /* enable regulator for DT */582582+ if (ts->vio) {583583+ error = regulator_enable(ts->vio);584584+ if (error)585585+ return error;586586+ }587587+588588+ dev_set_drvdata(dev, ts);589589+ error = sysfs_create_group(&dev->kobj, &tsc200x_attr_group);590590+ if (error) {591591+ dev_err(dev,592592+ "Failed to create sysfs attributes, err: %d\n", error);593593+ goto disable_regulator;594594+ }595595+596596+ error = input_register_device(ts->idev);597597+ if (error) {598598+ dev_err(dev,599599+ "Failed to register input device, err: %d\n", error);600600+ goto err_remove_sysfs;601601+ }602602+603603+ irq_set_irq_wake(irq, 1);604604+ return 0;605605+606606+err_remove_sysfs:607607+ sysfs_remove_group(&dev->kobj, &tsc200x_attr_group);608608+disable_regulator:609609+ if (ts->vio)610610+ regulator_disable(ts->vio);611611+ return error;612612+}613613+EXPORT_SYMBOL_GPL(tsc200x_probe);614614+615615+int tsc200x_remove(struct device *dev)616616+{617617+ struct tsc200x *ts = dev_get_drvdata(dev);618618+619619+ sysfs_remove_group(&dev->kobj, &tsc200x_attr_group);620620+621621+ if (ts->vio)622622+ regulator_disable(ts->vio);623623+624624+ return 0;625625+}626626+EXPORT_SYMBOL_GPL(tsc200x_remove);627627+628628+static int __maybe_unused tsc200x_suspend(struct device *dev)629629+{630630+ struct tsc200x *ts = dev_get_drvdata(dev);631631+632632+ mutex_lock(&ts->mutex);633633+634634+ if (!ts->suspended && ts->opened)635635+ __tsc200x_disable(ts);636636+637637+ ts->suspended = true;638638+639639+ mutex_unlock(&ts->mutex);640640+641641+ return 0;642642+}643643+644644+static int __maybe_unused tsc200x_resume(struct device *dev)645645+{646646+ struct tsc200x *ts = dev_get_drvdata(dev);647647+648648+ mutex_lock(&ts->mutex);649649+650650+ if (ts->suspended && ts->opened)651651+ __tsc200x_enable(ts);652652+653653+ ts->suspended = false;654654+655655+ mutex_unlock(&ts->mutex);656656+657657+ return 0;658658+}659659+660660+SIMPLE_DEV_PM_OPS(tsc200x_pm_ops, tsc200x_suspend, tsc200x_resume);661661+EXPORT_SYMBOL_GPL(tsc200x_pm_ops);662662+663663+MODULE_AUTHOR("Lauri Leukkunen <lauri.leukkunen@nokia.com>");664664+MODULE_DESCRIPTION("TSC200x Touchscreen Driver Core");665665+MODULE_LICENSE("GPL");