struct timer_list penup_timer;
unsigned int esd_timeout;
- struct timer_list esd_timer;
- struct work_struct esd_work;
+ struct delayed_work esd_work;
+ unsigned long last_valid_interrupt;
unsigned int x_plate_ohm;
- bool disabled;
- unsigned int disable_depth;
+ bool opened;
+ bool suspended;
bool pen_down;
void (*set_reset)(bool enable);
};
-static void tsc2005_cmd(struct tsc2005 *ts, u8 cmd)
+static int tsc2005_cmd(struct tsc2005 *ts, u8 cmd)
{
u8 tx = TSC2005_CMD | TSC2005_CMD_12BIT | cmd;
struct spi_transfer xfer = {
.bits_per_word = 8,
};
struct spi_message msg;
+ int error;
spi_message_init(&msg);
spi_message_add_tail(&xfer, &msg);
- spi_sync(ts->spi, &msg);
+
+ error = spi_sync(ts->spi, &msg);
+ if (error) {
+ dev_err(&ts->spi->dev, "%s: failed, command: %x, error: %d\n",
+ __func__, cmd, error);
+ return error;
+ }
+
+ return 0;
}
-static void tsc2005_write(struct tsc2005 *ts, u8 reg, u16 value)
+static int tsc2005_write(struct tsc2005 *ts, u8 reg, u16 value)
{
u32 tx = ((reg | TSC2005_REG_PND0) << 16) | value;
struct spi_transfer xfer = {
.bits_per_word = 24,
};
struct spi_message msg;
+ int error;
spi_message_init(&msg);
spi_message_add_tail(&xfer, &msg);
- spi_sync(ts->spi, &msg);
+
+ error = spi_sync(ts->spi, &msg);
+ if (error) {
+ dev_err(&ts->spi->dev,
+ "%s: failed, register: %x, value: %x, error: %d\n",
+ __func__, reg, value, error);
+ return error;
+ }
+
+ return 0;
}
static void tsc2005_setup_read(struct tsc2005_spi_rd *rd, u8 reg, bool last)
rd->spi_xfer.cs_change = !last;
}
-static void tsc2005_read(struct tsc2005 *ts, u8 reg, u16 *value)
+static int tsc2005_read(struct tsc2005 *ts, u8 reg, u16 *value)
{
struct tsc2005_spi_rd spi_rd;
struct spi_message msg;
+ int error;
tsc2005_setup_read(&spi_rd, reg, true);
spi_message_init(&msg);
spi_message_add_tail(&spi_rd.spi_xfer, &msg);
- spi_sync(ts->spi, &msg);
+
+ error = spi_sync(ts->spi, &msg);
+ if (error)
+ return error;
*value = spi_rd.spi_rx;
+ return 0;
}
static void tsc2005_update_pen_state(struct tsc2005 *ts,
unsigned int pressure;
u32 x, y;
u32 z1, z2;
+ int error;
- mutex_lock(&ts->mutex);
-
- if (unlikely(ts->disable_depth))
+ /* read the coordinates */
+ error = spi_sync(ts->spi, &ts->spi_read_msg);
+ if (unlikely(error))
goto out;
- /* read the coordinates */
- spi_sync(ts->spi, &ts->spi_read_msg);
x = ts->spi_x.spi_rx;
y = ts->spi_y.spi_rx;
z1 = ts->spi_z1.spi_rx;
spin_lock_irqsave(&ts->lock, flags);
tsc2005_update_pen_state(ts, x, y, pressure);
-
- /* set the penup timer */
mod_timer(&ts->penup_timer,
jiffies + msecs_to_jiffies(TSC2005_PENUP_TIME_MS));
- if (ts->esd_timeout && ts->set_reset) {
- /* update the watchdog timer */
- mod_timer(&ts->esd_timer, round_jiffies(jiffies +
- msecs_to_jiffies(ts->esd_timeout)));
- }
-
spin_unlock_irqrestore(&ts->lock, flags);
+ ts->last_valid_interrupt = jiffies;
out:
- mutex_unlock(&ts->mutex);
return IRQ_HANDLED;
}
tsc2005_cmd(ts, TSC2005_CMD_STOP);
}
-/* must be called with mutex held */
-static void tsc2005_disable(struct tsc2005 *ts)
+/* must be called with ts->mutex held */
+static void __tsc2005_disable(struct tsc2005 *ts)
{
- if (ts->disable_depth++ != 0)
- return;
- disable_irq(ts->spi->irq);
- if (ts->esd_timeout)
- del_timer_sync(&ts->esd_timer);
- del_timer_sync(&ts->penup_timer);
tsc2005_stop_scan(ts);
-}
-/* must be called with mutex held */
-static void tsc2005_enable(struct tsc2005 *ts)
-{
- if (--ts->disable_depth != 0)
- return;
- tsc2005_start_scan(ts);
- enable_irq(ts->spi->irq);
- if (!ts->esd_timeout)
- return;
- mod_timer(&ts->esd_timer,
- round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout)));
-}
+ disable_irq(ts->spi->irq);
+ del_timer_sync(&ts->penup_timer);
-static ssize_t tsc2005_disable_show(struct device *dev,
- struct device_attribute *attr, char *buf)
-{
- struct spi_device *spi = to_spi_device(dev);
- struct tsc2005 *ts = spi_get_drvdata(spi);
+ cancel_delayed_work_sync(&ts->esd_work);
- return sprintf(buf, "%u\n", ts->disabled);
+ enable_irq(ts->spi->irq);
}
-static ssize_t tsc2005_disable_store(struct device *dev,
- struct device_attribute *attr,
- const char *buf, size_t count)
+/* must be called with ts->mutex held */
+static void __tsc2005_enable(struct tsc2005 *ts)
{
- struct spi_device *spi = to_spi_device(dev);
- struct tsc2005 *ts = spi_get_drvdata(spi);
- unsigned long res;
- int i;
+ tsc2005_start_scan(ts);
- if (strict_strtoul(buf, 10, &res) < 0)
- return -EINVAL;
- i = res ? 1 : 0;
+ if (ts->esd_timeout && ts->set_reset) {
+ ts->last_valid_interrupt = jiffies;
+ schedule_delayed_work(&ts->esd_work,
+ round_jiffies_relative(
+ msecs_to_jiffies(ts->esd_timeout)));
+ }
- mutex_lock(&ts->mutex);
- if (i == ts->disabled)
- goto out;
- ts->disabled = i;
- if (i)
- tsc2005_disable(ts);
- else
- tsc2005_enable(ts);
-out:
- mutex_unlock(&ts->mutex);
- return count;
}
-static DEVICE_ATTR(disable, 0664, tsc2005_disable_show, tsc2005_disable_store);
static ssize_t tsc2005_selftest_show(struct device *dev,
struct device_attribute *attr,
u16 temp_high;
u16 temp_high_orig;
u16 temp_high_test;
- unsigned int result;
+ bool success = true;
+ int error;
mutex_lock(&ts->mutex);
/*
* Test TSC2005 communications via temp high register.
*/
- tsc2005_disable(ts);
- result = 1;
- tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high_orig);
+ __tsc2005_disable(ts);
+
+ error = tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high_orig);
+ if (error) {
+ dev_warn(dev, "selftest failed: read error %d\n", error);
+ success = false;
+ goto out;
+ }
+
temp_high_test = (temp_high_orig - 1) & MAX_12BIT;
- tsc2005_write(ts, TSC2005_REG_TEMP_HIGH, temp_high_test);
- tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high);
+
+ error = tsc2005_write(ts, TSC2005_REG_TEMP_HIGH, temp_high_test);
+ if (error) {
+ dev_warn(dev, "selftest failed: write error %d\n", error);
+ success = false;
+ goto out;
+ }
+
+ error = tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high);
+ if (error) {
+ dev_warn(dev, "selftest failed: read error %d after write\n",
+ error);
+ success = false;
+ goto out;
+ }
+
if (temp_high != temp_high_test) {
dev_warn(dev, "selftest failed: %d != %d\n",
temp_high, temp_high_test);
- result = 0;
+ success = false;
}
/* hardware reset */
ts->set_reset(false);
usleep_range(100, 500); /* only 10us required */
ts->set_reset(true);
- tsc2005_enable(ts);
+
+ if (!success)
+ goto out;
/* test that the reset really happened */
- tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high);
+ error = tsc2005_read(ts, TSC2005_REG_TEMP_HIGH, &temp_high);
+ if (error) {
+ dev_warn(dev, "selftest failed: read error %d after reset\n",
+ error);
+ success = false;
+ goto out;
+ }
+
if (temp_high != temp_high_orig) {
dev_warn(dev, "selftest failed after reset: %d != %d\n",
temp_high, temp_high_orig);
- result = 0;
+ success = false;
}
+out:
+ __tsc2005_enable(ts);
mutex_unlock(&ts->mutex);
- return sprintf(buf, "%u\n", result);
+ return sprintf(buf, "%d\n", success);
}
static DEVICE_ATTR(selftest, S_IRUGO, tsc2005_selftest_show, NULL);
static struct attribute *tsc2005_attrs[] = {
- &dev_attr_disable.attr,
&dev_attr_selftest.attr,
NULL
};
.attrs = tsc2005_attrs,
};
-static void tsc2005_esd_timer(unsigned long data)
-{
- struct tsc2005 *ts = (struct tsc2005 *)data;
-
- schedule_work(&ts->esd_work);
-}
-
static void tsc2005_esd_work(struct work_struct *work)
{
- struct tsc2005 *ts = container_of(work, struct tsc2005, esd_work);
+ struct tsc2005 *ts = container_of(work, struct tsc2005, esd_work.work);
+ int error;
u16 r;
- mutex_lock(&ts->mutex);
+ if (!mutex_trylock(&ts->mutex)) {
+ /*
+ * If the mutex is taken, it means that disable or enable is in
+ * progress. In that case just reschedule the work. If the work
+ * is not needed, it will be canceled by disable.
+ */
+ goto reschedule;
+ }
- if (ts->disable_depth)
+ if (time_is_after_jiffies(ts->last_valid_interrupt +
+ msecs_to_jiffies(ts->esd_timeout)))
goto out;
+ /* We should be able to read register without disabling interrupts. */
+ error = tsc2005_read(ts, TSC2005_REG_CFR0, &r);
+ if (!error &&
+ !((r ^ TSC2005_CFR0_INITVALUE) & TSC2005_CFR0_RW_MASK)) {
+ goto out;
+ }
+
/*
- * If we cannot read our known value from configuration register 0 then
- * reset the controller as if from power-up and start scanning again.
+ * If we could not read our known value from configuration register 0
+ * then we should reset the controller as if from power-up and start
+ * scanning again.
*/
- tsc2005_read(ts, TSC2005_REG_CFR0, &r);
- if ((r ^ TSC2005_CFR0_INITVALUE) & TSC2005_CFR0_RW_MASK) {
- dev_info(&ts->spi->dev, "TSC2005 not responding - resetting\n");
- ts->set_reset(false);
- tsc2005_update_pen_state(ts, 0, 0, 0);
- usleep_range(100, 500); /* only 10us required */
- ts->set_reset(true);
- tsc2005_start_scan(ts);
- }
+ dev_info(&ts->spi->dev, "TSC2005 not responding - resetting\n");
- /* re-arm the watchdog */
- mod_timer(&ts->esd_timer,
- round_jiffies(jiffies + msecs_to_jiffies(ts->esd_timeout)));
+ disable_irq(ts->spi->irq);
+ del_timer_sync(&ts->penup_timer);
+
+ tsc2005_update_pen_state(ts, 0, 0, 0);
+
+ ts->set_reset(false);
+ usleep_range(100, 500); /* only 10us required */
+ ts->set_reset(true);
+
+ enable_irq(ts->spi->irq);
+ tsc2005_start_scan(ts);
out:
+ mutex_unlock(&ts->mutex);
+reschedule:
+ /* re-arm the watchdog */
+ schedule_delayed_work(&ts->esd_work,
+ round_jiffies_relative(
+ msecs_to_jiffies(ts->esd_timeout)));
+}
+
+static int tsc2005_open(struct input_dev *input)
+{
+ struct tsc2005 *ts = input_get_drvdata(input);
+
+ mutex_lock(&ts->mutex);
+
+ if (!ts->suspended)
+ __tsc2005_enable(ts);
+
+ ts->opened = true;
+
+ mutex_unlock(&ts->mutex);
+
+ return 0;
+}
+
+static void tsc2005_close(struct input_dev *input)
+{
+ struct tsc2005 *ts = input_get_drvdata(input);
+
+ mutex_lock(&ts->mutex);
+
+ if (!ts->suspended)
+ __tsc2005_disable(ts);
+
+ ts->opened = false;
+
mutex_unlock(&ts->mutex);
}
spin_lock_init(&ts->lock);
setup_timer(&ts->penup_timer, tsc2005_penup_timer, (unsigned long)ts);
- setup_timer(&ts->esd_timer, tsc2005_esd_timer, (unsigned long)ts);
- INIT_WORK(&ts->esd_work, tsc2005_esd_work);
+ INIT_DELAYED_WORK(&ts->esd_work, tsc2005_esd_work);
tsc2005_setup_spi_xfer(ts);
input_set_abs_params(input_dev, ABS_Y, 0, max_y, fudge_y, 0);
input_set_abs_params(input_dev, ABS_PRESSURE, 0, max_p, fudge_p, 0);
+ input_dev->open = tsc2005_open;
+ input_dev->close = tsc2005_close;
+
+ input_set_drvdata(input_dev, ts);
+
+ /* Ensure the touchscreen is off */
+ tsc2005_stop_scan(ts);
+
error = request_threaded_irq(spi->irq, NULL, tsc2005_irq_thread,
IRQF_TRIGGER_RISING, "tsc2005", ts);
if (error) {
goto err_remove_sysfs;
}
- tsc2005_start_scan(ts);
-
- if (ts->esd_timeout && ts->set_reset) {
- /* start the optional ESD watchdog */
- mod_timer(&ts->esd_timer, round_jiffies(jiffies +
- msecs_to_jiffies(ts->esd_timeout)));
- }
-
- set_irq_wake(spi->irq, 1);
+ irq_set_irq_wake(spi->irq, 1);
return 0;
err_remove_sysfs:
sysfs_remove_group(&ts->spi->dev.kobj, &tsc2005_attr_group);
- mutex_lock(&ts->mutex);
- tsc2005_disable(ts);
- mutex_unlock(&ts->mutex);
-
- if (ts->esd_timeout)
- del_timer_sync(&ts->esd_timer);
- del_timer_sync(&ts->penup_timer);
-
- flush_work(&ts->esd_work);
-
free_irq(ts->spi->irq, ts);
input_unregister_device(ts->idev);
kfree(ts);
struct tsc2005 *ts = spi_get_drvdata(spi);
mutex_lock(&ts->mutex);
- tsc2005_disable(ts);
+
+ if (!ts->suspended && ts->opened)
+ __tsc2005_disable(ts);
+
+ ts->suspended = true;
+
mutex_unlock(&ts->mutex);
return 0;
struct tsc2005 *ts = spi_get_drvdata(spi);
mutex_lock(&ts->mutex);
- tsc2005_enable(ts);
+
+ if (ts->suspended && ts->opened)
+ __tsc2005_enable(ts);
+
+ ts->suspended = false;
+
mutex_unlock(&ts->mutex);
return 0;