mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Oskar Schirmer" <oskar@scara.com>
To: sameo@linux.intel.com
Cc: dmitry.torokhov@gmail.com, kernel@pengutronix.de,
	u.kleine-koenig@pengutronix.de, philippe.retornaz@epfl.ch,
	michael.thalmeier@hale.at, linux-kernel@vger.kernel.org,
	"Oskar Schirmer" <oskar@scara.com>
Subject: [PATCH 6/7] input/touchscreen: add calibration functionality with
Date: 14 Feb 2012 10:34:31 +0000	[thread overview]
Message-ID: <1329215672-15706-7-git-send-email-oskar@scara.com> (raw)
In-Reply-To: <1329215672-15706-1-git-send-email-oskar@scara.com>

Currently mc13xxx_ts does provide uncalibrated data only.
Add means to calibrate touchscreen through simple matrix calculus,
parameters accessible via sysfs.

Signed-off-by: Oskar Schirmer <oskar@scara.com>
---
 drivers/input/touchscreen/Kconfig      |    1 +
 drivers/input/touchscreen/mc13xxx_ts.c |   87 +++++++++++++++++++++++++++++---
 2 files changed, 81 insertions(+), 7 deletions(-)

diff --git a/drivers/input/touchscreen/Kconfig b/drivers/input/touchscreen/Kconfig
index 51d19cb..9e315d7 100644
--- a/drivers/input/touchscreen/Kconfig
+++ b/drivers/input/touchscreen/Kconfig
@@ -587,6 +587,7 @@ config TOUCHSCREEN_USB_COMPOSITE
 config TOUCHSCREEN_MC13XXX
 	tristate "Freescale MC13XXX touchscreen input driver"
 	depends on MFD_MC13XXX
+	select SYSFS
 	help
 	  Say Y here if you have an Freescale MC13XXX PMIC on your
 	  board and want to use its touchscreen
diff --git a/drivers/input/touchscreen/mc13xxx_ts.c b/drivers/input/touchscreen/mc13xxx_ts.c
index 802228e..02f61a7 100644
--- a/drivers/input/touchscreen/mc13xxx_ts.c
+++ b/drivers/input/touchscreen/mc13xxx_ts.c
@@ -48,6 +48,7 @@ struct mc13xxx_ts_priv {
 	struct delayed_work work;
 	struct workqueue_struct *workq;
 	unsigned int sample[4];
+	int calibration[7];
 };
 
 static irqreturn_t mc13xxx_ts_handler(int irq, void *data)
@@ -76,6 +77,26 @@ static irqreturn_t mc13xxx_ts_handler(int irq, void *data)
 			swap(a0, a1);					\
 		})
 
+static void mc13xxx_report_abs_calibrated(struct mc13xxx_ts_priv *priv,
+			struct input_dev *idev, int x, int y)
+{
+	int *calib = priv->calibration;
+	int xc, yc;
+	if (calib[6]) {
+		xc = (calib[0] * x + calib[1] * y + calib[2]) / calib[6];
+		if (xc < 0)
+			xc = 0;
+		yc = (calib[3] * x + calib[4] * y + calib[5]) / calib[6];
+		if (yc < 0)
+			yc = 0;
+	} else {
+		xc = x;
+		yc = y;
+	}
+	input_report_abs(idev, ABS_X, xc);
+	input_report_abs(idev, ABS_Y, yc);
+}
+
 static void mc13783_ts_report_sample(struct mc13xxx_ts_priv *priv)
 {
 	struct input_dev *idev = priv->idev;
@@ -109,9 +130,7 @@ static void mc13783_ts_report_sample(struct mc13xxx_ts_priv *priv)
 			 y2 - y0 < sample_tolerance)) {
 		/* report the median coordinate and average pressure */
 		if (cr0) {
-			input_report_abs(idev, ABS_X, x1);
-			input_report_abs(idev, ABS_Y, y1);
-
+			mc13xxx_report_abs_calibrated(priv, idev, x1, y1);
 			dev_dbg(&idev->dev, "report (%d, %d, %d)\n",
 					x1, y1, 0x1000 - cr0);
 			queue_delayed_work(priv->workq, &priv->work, HZ / 50);
@@ -163,9 +182,7 @@ static void mc13892_ts_report_sample(struct mc13xxx_ts_priv *priv)
 		if (cr0) {
 			x0 = (x0 + x1) / 2;
 			y0 = (y0 + y1) / 2;
-			input_report_abs(idev, ABS_X, x0);
-			input_report_abs(idev, ABS_Y, y0);
-
+			mc13xxx_report_abs_calibrated(priv, idev, x0, y0);
 			dev_dbg(&idev->dev, "report (%d, %d, %d)\n",
 					x0, y0, cr0);
 			queue_delayed_work(priv->workq, &priv->work, HZ / 50);
@@ -239,6 +256,53 @@ static void mc13xxx_ts_close(struct input_dev *dev)
 	cancel_delayed_work_sync(&priv->work);
 }
 
+static ssize_t mc13xxx_calibration_show(struct device *d,
+		struct device_attribute *attr, char *buf)
+{
+	struct mc13xxx_ts_priv *priv = dev_get_drvdata(d);
+	int i, p, r;
+
+	p = 0;
+	for (i = 0; i < 7; i++) {
+		r = sprintf(&buf[p], i == 6 ? "%d\n" : "%d ",
+				priv->calibration[i]);
+		if (r < 0)
+			return r;
+		p += r;
+	}
+	return p;
+}
+
+static ssize_t mc13xxx_calibration_store(struct device *d,
+		struct device_attribute *attr, const char *buf, size_t count)
+{
+	struct mc13xxx_ts_priv *priv = dev_get_drvdata(d);
+	int i, p, r;
+	int c[7];
+
+	p = 0;
+	for (i = 0; i < 7; i++) {
+		if (sscanf(&buf[p], "%d%n", &c[i], &r) != 1)
+			return -EINVAL;
+		p += r;
+	}
+	memcpy(priv->calibration, c, sizeof(priv->calibration));
+	return count;
+}
+
+static DEVICE_ATTR(calibration, S_IWUSR | S_IRUGO,
+		mc13xxx_calibration_show, mc13xxx_calibration_store);
+
+static int mc13xxx_init_sysfs(struct device *d)
+{
+	return device_create_file(d, &dev_attr_calibration);
+}
+
+static void mc13xxx_free_sysfs(struct device *d)
+{
+	device_remove_file(d, &dev_attr_calibration);
+}
+
 static int __init mc13xxx_ts_probe(struct platform_device *pdev)
 {
 	struct mc13xxx_ts_priv *priv;
@@ -286,16 +350,24 @@ static int __init mc13xxx_ts_probe(struct platform_device *pdev)
 
 	input_set_drvdata(idev, priv);
 
+	ret = mc13xxx_init_sysfs(&pdev->dev);
+	if (ret) {
+		dev_err(&pdev->dev, "failed to init sysfs.\n");
+		goto err_destroy_wq;
+	}
+
 	ret = input_register_device(priv->idev);
 	if (ret) {
 		dev_err(&pdev->dev,
 			"register input device failed with %d\n", ret);
-		goto err_destroy_wq;
+		goto err_free_sysfs;
 	}
 
 	platform_set_drvdata(pdev, priv);
 	return 0;
 
+err_free_sysfs:
+	mc13xxx_free_sysfs(&pdev->dev);
 err_destroy_wq:
 	destroy_workqueue(priv->workq);
 err_free_mem:
@@ -310,6 +382,7 @@ static int __devexit mc13xxx_ts_remove(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, NULL);
 
+	mc13xxx_free_sysfs(&pdev->dev);
 	destroy_workqueue(priv->workq);
 	input_unregister_device(priv->idev);
 	kfree(priv);
-- 
1.7.5.4


  parent reply	other threads:[~2012-02-14 11:05 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-14 10:34 [PATCH 0/7] add support for mc13892 I2C based touch panel for mx35_3ds Oskar Schirmer
2012-02-14 10:34 ` [PATCH 1/7] mfd: move mc13xxx global declarations to header file Oskar Schirmer
2012-02-14 10:34 ` [PATCH 2/7] mfd: split off mc13xxx SPI specifica from core Oskar Schirmer
2012-02-14 10:34 ` [PATCH 3/7] mfd: add I2C handling to mc13xxx driver Oskar Schirmer
2012-02-16 16:40   ` Mark Brown
2012-02-14 10:34 ` [PATCH 4/7] input/touchscreen: rename mc13783_ts.c Oskar Schirmer
2012-02-14 10:34 ` [PATCH 5/7] input/touchscreen: add support for mc13892 Oskar Schirmer
2012-02-14 10:34 ` Oskar Schirmer [this message]
2012-02-14 10:34 ` [PATCH 7/7] arm/imx: add mc13892 I2C based touch panel support for Oskar Schirmer
2012-02-14 11:49 ` [PATCH 0/7] add support for mc13892 I2C based touch panel for mx35_3ds Uwe Kleine-König
2012-02-14 14:27   ` Oskar Schirmer
2012-02-23 15:37   ` Samuel Ortiz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1329215672-15706-7-git-send-email-oskar@scara.com \
    --to=oskar@scara.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=kernel@pengutronix.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michael.thalmeier@hale.at \
    --cc=philippe.retornaz@epfl.ch \
    --cc=sameo@linux.intel.com \
    --cc=u.kleine-koenig@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®