linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/5] add resistive touchscreen support for new Allwinner SoCs' GPADC's driver
@ 2016-07-20  8:29 Quentin Schulz
  2016-07-20  8:29 ` [PATCH 1/5] mfd: sunxi-gpadc-mfd: add TP_UP_PENDING irq Quentin Schulz
                   ` (4 more replies)
  0 siblings, 5 replies; 31+ messages in thread
From: Quentin Schulz @ 2016-07-20  8:29 UTC (permalink / raw)
  To: linux-arm-kernel

The Allwinner SoCs all have an ADC that can also act as a touchscreen
controller and a thermal sensor. The first four channels can be used either
for the ADC or the touchscreen and the fifth channel is used for the
thermal sensor. We currently have a driver for the two latter functions in
drivers/input/touchscreen/sun4i-ts.c but we don't have access to the ADC
feature at all.

The temperature sensor returns valid values only when the GPADC is in
touchscreen mode.

This patch series is based on the patch series named "add support for
Allwinner SoCs ADC": https://lkml.org/lkml/2016/6/28/226

This adds the TP_UP_PENDING VIRQ in the MFD which occurs when the thing
(stylus or finger) touching the screen releases the touch. This VIRQ is
then handled in the touchscreen driver to notify the input framework of an
up event.
The MFD probes the touchscreen driver if the property
"allwinner,ts-attached" is set in rtp node of the DT.

The touchscreen driver needs data from the IIO ADC driver to retrieve touch
coordinates. The ADC driver exposes its four ADC channels to the
touchscreen driver. The touchscreen driver retrieves the four ADC channels,
associates a buffer with these channels and registers a callback to handle
buffer data. When the touchscreen driver is open, it starts the buffering
in the ADC driver and activates the callback. The ADC driver will be
notified a buffer is started and will then activate the FIFO_DATA_PENDING
interrupt which occurs when there is data to read in the FIFO. It also
selects the right mode (touchscreen or ADC). Then the FIFO_DATA_PENDING
VIRQ handler will continuously fill in a buffer with all data from the
hardware FIFO and send it to the touchscreen driver via the callback the
touchscreen driver registered previously when the VIRQ has been handled.
When in touchscreen mode, the hardware FIFO is filled in following this
pattern: X then Y coordinates of the first event, X and Y coordinates of
the second event, etc. This VIRQ is disabled only when the ADC driver is
notified a buffer is stopped. The touchscreen driver gets this buffer in
its defined callback and notifies the input framework of all events'
coordinates. When the touchscreen driver is closed, it stops the buffering
which deactivates the callback.

The hardware FIFO contains a maximum of 32 u32 values. Locations of the
first buffer retrieved after an up event are unreliable and are thus
dropped.

This patch replaces drivers/input/touchscreen/sun4i-ts.c by
drivers/input/touchscreen/sunxi-gpadc-ts.c, adding the ADC feature to
Allwinner SoCs' GPADC.

Quentin Schulz (5):
  mfd: sunxi-gpadc-mfd: add TP_UP_PENDING irq
  mfd: sunxi-gpadc-mfd: add buffer structure
  iio: adc: sunxi-gpadc-iio: enable iio_buffers
  input: touchscreen: support Allwinner SoCs' touchscreen
  mfd: sunxi-gpadc-mfd: probe sunxi-gpadc-ts driver

 drivers/iio/adc/Kconfig                    |   1 +
 drivers/iio/adc/sunxi-gpadc-iio.c          | 153 +++++++++--
 drivers/input/touchscreen/Kconfig          |  11 +-
 drivers/input/touchscreen/Makefile         |   2 +-
 drivers/input/touchscreen/sun4i-ts.c       | 419 -----------------------------
 drivers/input/touchscreen/sunxi-gpadc-ts.c | 195 ++++++++++++++
 drivers/mfd/sunxi-gpadc-mfd.c              | 127 ++++++---
 include/linux/mfd/sunxi-gpadc-mfd.h        |   5 +
 8 files changed, 433 insertions(+), 480 deletions(-)
 delete mode 100644 drivers/input/touchscreen/sun4i-ts.c
 create mode 100644 drivers/input/touchscreen/sunxi-gpadc-ts.c

-- 
2.5.0

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 1/5] mfd: sunxi-gpadc-mfd: add TP_UP_PENDING irq
  2016-07-20  8:29 [PATCH 0/5] add resistive touchscreen support for new Allwinner SoCs' GPADC's driver Quentin Schulz
@ 2016-07-20  8:29 ` Quentin Schulz
  2016-07-20  8:29 ` [PATCH 2/5] mfd: sunxi-gpadc-mfd: add buffer structure Quentin Schulz
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 31+ messages in thread
From: Quentin Schulz @ 2016-07-20  8:29 UTC (permalink / raw)
  To: linux-arm-kernel

This adds support for TP_UP_PENDING irq in Allwinner SoCs' GPADC's MFD.

This interrupt occurs when a touchscreen is attached and the thing (stylus,
finger) currently touching the touchscreen releases the touch.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
---
 drivers/mfd/sunxi-gpadc-mfd.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/drivers/mfd/sunxi-gpadc-mfd.c b/drivers/mfd/sunxi-gpadc-mfd.c
index f0005a6..05a000b 100644
--- a/drivers/mfd/sunxi-gpadc-mfd.c
+++ b/drivers/mfd/sunxi-gpadc-mfd.c
@@ -19,6 +19,8 @@
 
 #define SUNXI_IRQ_FIFO_DATA	0
 #define SUNXI_IRQ_TEMP_DATA	1
+#define SUNXI_IRQ_TP_UP		2
+
 
 static struct resource adc_resources[] = {
 	{
@@ -34,9 +36,19 @@ static struct resource adc_resources[] = {
 	},
 };
 
+static struct resource ts_resources[] = {
+	{
+		.name	= "TP_UP_PENDING",
+		.start	= SUNXI_IRQ_TP_UP,
+		.end	= SUNXI_IRQ_TP_UP,
+		.flags	= IORESOURCE_IRQ,
+	},
+};
+
 static const struct regmap_irq sunxi_gpadc_mfd_regmap_irq[] = {
 	REGMAP_IRQ_REG(SUNXI_IRQ_FIFO_DATA, 0, BIT(16)),
 	REGMAP_IRQ_REG(SUNXI_IRQ_TEMP_DATA, 0, BIT(18)),
+	REGMAP_IRQ_REG(SUNXI_IRQ_TP_UP, 0, BIT(1)),
 };
 
 static const struct regmap_irq_chip sunxi_gpadc_mfd_regmap_irq_chip = {
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 2/5] mfd: sunxi-gpadc-mfd: add buffer structure
  2016-07-20  8:29 [PATCH 0/5] add resistive touchscreen support for new Allwinner SoCs' GPADC's driver Quentin Schulz
  2016-07-20  8:29 ` [PATCH 1/5] mfd: sunxi-gpadc-mfd: add TP_UP_PENDING irq Quentin Schulz
@ 2016-07-20  8:29 ` Quentin Schulz
  2016-07-24 10:32   ` Jonathan Cameron
  2016-07-20  8:29 ` [PATCH 3/5] iio: adc: sunxi-gpadc-iio: enable iio_buffers Quentin Schulz
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 31+ messages in thread
From: Quentin Schulz @ 2016-07-20  8:29 UTC (permalink / raw)
  To: linux-arm-kernel

This adds a buffer structure for files including the sunxi-gpadc-mfd
header. This structure has a buffer of 32 u32 values to store data from the
FIFO of the GPADC of Allwinner SoCs. A buff_size is provided in case the
buffer is not full.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
---
 include/linux/mfd/sunxi-gpadc-mfd.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/include/linux/mfd/sunxi-gpadc-mfd.h b/include/linux/mfd/sunxi-gpadc-mfd.h
index 7155845..f658299 100644
--- a/include/linux/mfd/sunxi-gpadc-mfd.h
+++ b/include/linux/mfd/sunxi-gpadc-mfd.h
@@ -20,4 +20,9 @@ struct sunxi_gpadc_mfd_dev {
 	void __iomem			*regs;
 };
 
+struct sunxi_gpadc_buffer {
+	u32				buffer[32];
+	unsigned int			buff_size;
+};
+
 #endif
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 3/5] iio: adc: sunxi-gpadc-iio: enable iio_buffers
  2016-07-20  8:29 [PATCH 0/5] add resistive touchscreen support for new Allwinner SoCs' GPADC's driver Quentin Schulz
  2016-07-20  8:29 ` [PATCH 1/5] mfd: sunxi-gpadc-mfd: add TP_UP_PENDING irq Quentin Schulz
  2016-07-20  8:29 ` [PATCH 2/5] mfd: sunxi-gpadc-mfd: add buffer structure Quentin Schulz
@ 2016-07-20  8:29 ` Quentin Schulz
  2016-07-20  8:38   ` Peter Meerwald-Stadler
  2016-07-24 11:03   ` Jonathan Cameron
  2016-07-20  8:29 ` [PATCH 4/5] input: touchscreen: support Allwinner SoCs' touchscreen Quentin Schulz
  2016-07-20  8:29 ` [PATCH 5/5] mfd: sunxi-gpadc-mfd: probe sunxi-gpadc-ts driver Quentin Schulz
  4 siblings, 2 replies; 31+ messages in thread
From: Quentin Schulz @ 2016-07-20  8:29 UTC (permalink / raw)
  To: linux-arm-kernel

This enables the use of buffers on ADC channels of sunxi-gpadc-iio driver.
It also prepares the code which will be used by the touchscreen driver
named sunxi-gpadc-ts.

The GPADC on Allwinner SoCs (A10, A13 and A31) has a 12 bits register for
conversion's data. The GPADC uses the same ADC channels for the ADC and the
touchscreen therefore exposes these channels to the sunxi-gpadc-ts iio
consumer which will be in charge of reading data from these channels for
the input framework.

The temperature can only be read when in touchscreen mode. This means if
the buffers are being used for the ADC, the temperature sensor cannot be
read.

When a FIFO_DATA_PENDING irq occurs, its handler will read the entire FIFO
and fill a buffer before sending it to the consumers which registered in
IIO for the ADC channels.

When a consumer starts buffering ADC channels,
sunxi_gpadc_buffer_postenable is called and will enable FIFO_DATA_PENDING
irq and select the mode in which the GPADC should run (ADC or touchscreen)
depending on a property of the DT ("allwinner,ts-attached").
When the consumer stops buffering, it disables the same irq.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
---
 drivers/iio/adc/Kconfig           |   1 +
 drivers/iio/adc/sunxi-gpadc-iio.c | 153 ++++++++++++++++++++++++++++++++++----
 2 files changed, 138 insertions(+), 16 deletions(-)

diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index 184856f..15e3b08 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -342,6 +342,7 @@ config SUNXI_ADC
 	tristate "ADC driver for sunxi platforms"
 	depends on IIO
 	depends on MFD_SUNXI_ADC
+	depends on IIO_BUFFER_CB
 	help
 	  Say yes here to build support for Allwinner (A10, A13 and A31) SoCs
 	  ADC. This ADC provides 4 channels which can be used as an ADC or as a
diff --git a/drivers/iio/adc/sunxi-gpadc-iio.c b/drivers/iio/adc/sunxi-gpadc-iio.c
index 87cc913..2e44ca7 100644
--- a/drivers/iio/adc/sunxi-gpadc-iio.c
+++ b/drivers/iio/adc/sunxi-gpadc-iio.c
@@ -16,8 +16,9 @@
 #include <linux/platform_device.h>
 #include <linux/regmap.h>
 
-#include <linux/iio/iio.h>
+#include <linux/iio/buffer.h>
 #include <linux/iio/driver.h>
+#include <linux/iio/iio.h>
 #include <linux/iio/machine.h>
 #include <linux/mfd/sunxi-gpadc-mfd.h>
 
@@ -71,6 +72,7 @@
 #define SUNXI_GPADC_TP_DATA_XY_CHANGE		BIT(13)
 #define SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(x)	((x) << 8)  /* 5 bits */
 #define SUNXI_GPADC_TP_DATA_DRQ_EN		BIT(7)
+/* Be careful, flushing FIFO spawns SUNXI_GPADC_FIFO_DATA_PENDING interrupts */
 #define SUNXI_GPADC_TP_FIFO_FLUSH		BIT(4)
 #define SUNXI_GPADC_TP_UP_IRQ_EN		BIT(1)
 #define SUNXI_GPADC_TP_DOWN_IRQ_EN		BIT(0)
@@ -79,6 +81,7 @@
 #define SUNXI_GPADC_TEMP_DATA_PENDING		BIT(18)
 #define SUNXI_GPADC_FIFO_OVERRUN_PENDING	BIT(17)
 #define SUNXI_GPADC_FIFO_DATA_PENDING		BIT(16)
+#define SUNXI_GPADC_RXA_CNT			GENMASK(12, 8)
 #define SUNXI_GPADC_TP_IDLE_FLG			BIT(2)
 #define SUNXI_GPADC_TP_UP_PENDING		BIT(1)
 #define SUNXI_GPADC_TP_DOWN_PENDING		BIT(0)
@@ -101,19 +104,43 @@ struct sunxi_gpadc_dev {
 	unsigned int			fifo_data_irq;
 	unsigned int			temp_data_irq;
 	unsigned int			flags;
+	struct iio_dev			*indio_dev;
+	struct sunxi_gpadc_buffer	buffer;
+	bool				ts_attached;
+	bool				buffered;
 };
 
-#define SUNXI_GPADC_ADC_CHANNEL(_channel, _name) {		\
+#define SUNXI_GPADC_ADC_CHANNEL(_channel, _name, _index) {	\
 	.type = IIO_VOLTAGE,					\
 	.indexed = 1,						\
 	.channel = _channel,					\
 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
 	.datasheet_name = _name,				\
+	.scan_index = _index,					\
+	.scan_type = {						\
+		.sign = 'u',					\
+		.realbits = 12,					\
+		.storagebits = 16,				\
+		.shift = 0,					\
+		.endianness = IIO_LE,				\
+	},							\
 }
 
 static struct iio_map sunxi_gpadc_hwmon_maps[] = {
 	{
+		.adc_channel_label = "adc_chan0",
+		.consumer_dev_name = "sunxi-gpadc-ts.0",
+	}, {
+		.adc_channel_label = "adc_chan1",
+		.consumer_dev_name = "sunxi-gpadc-ts.0",
+	}, {
+		.adc_channel_label = "adc_chan2",
+		.consumer_dev_name = "sunxi-gpadc-ts.0",
+	}, {
+		.adc_channel_label = "adc_chan3",
+		.consumer_dev_name = "sunxi-gpadc-ts.0",
+	}, {
 		.adc_channel_label = "temp_adc",
 		.consumer_dev_name = "iio_hwmon.0",
 	},
@@ -121,28 +148,33 @@ static struct iio_map sunxi_gpadc_hwmon_maps[] = {
 };
 
 static const struct iio_chan_spec sunxi_gpadc_channels[] = {
-	SUNXI_GPADC_ADC_CHANNEL(0, "adc_chan0"),
-	SUNXI_GPADC_ADC_CHANNEL(1, "adc_chan1"),
-	SUNXI_GPADC_ADC_CHANNEL(2, "adc_chan2"),
-	SUNXI_GPADC_ADC_CHANNEL(3, "adc_chan3"),
+	SUNXI_GPADC_ADC_CHANNEL(0, "adc_chan0", 1),
+	SUNXI_GPADC_ADC_CHANNEL(1, "adc_chan1", 2),
+	SUNXI_GPADC_ADC_CHANNEL(2, "adc_chan2", 3),
+	SUNXI_GPADC_ADC_CHANNEL(3, "adc_chan3", 4),
 	{
 		.type = IIO_TEMP,
 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
 		.datasheet_name = "temp_adc",
 		.extend_name = "SoC temperature",
 	},
-	{ /* sentinel */ },
 };
 
 static int sunxi_gpadc_adc_read(struct iio_dev *indio_dev, int channel,
 				int *val)
 {
 	struct sunxi_gpadc_dev *info = iio_priv(indio_dev);
+	bool buffered = info->buffered;
 	int ret = 0;
+	unsigned int reg;
 
 	mutex_lock(&indio_dev->mlock);
 
 	reinit_completion(&info->completion);
+
+	reg = SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(1) | SUNXI_GPADC_TP_FIFO_FLUSH;
+	regmap_update_bits(info->regmap, SUNXI_GPADC_TP_INT_FIFOC, reg, reg);
+
 	if (info->flags & SUNXI_GPADC_ARCH_SUN6I)
 		regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL1,
 			     SUNXI_GPADC_SUN6I_TP_MODE_EN |
@@ -153,9 +185,9 @@ static int sunxi_gpadc_adc_read(struct iio_dev *indio_dev, int channel,
 			     SUNXI_GPADC_TP_MODE_EN |
 			     SUNXI_GPADC_TP_ADC_SELECT |
 			     SUNXI_GPADC_ADC_CHAN_SELECT(channel));
-	regmap_write(info->regmap, SUNXI_GPADC_TP_INT_FIFOC,
-		     SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(1) |
-		     SUNXI_GPADC_TP_FIFO_FLUSH);
+
+	info->buffered = false;
+
 	enable_irq(info->fifo_data_irq);
 
 	if (!wait_for_completion_timeout(&info->completion,
@@ -169,6 +201,7 @@ static int sunxi_gpadc_adc_read(struct iio_dev *indio_dev, int channel,
 out:
 	disable_irq(info->fifo_data_irq);
 	mutex_unlock(&indio_dev->mlock);
+	info->buffered = buffered;
 
 	return ret;
 }
@@ -177,20 +210,22 @@ static int sunxi_gpadc_temp_read(struct iio_dev *indio_dev, int *val)
 {
 	struct sunxi_gpadc_dev *info = iio_priv(indio_dev);
 	int ret = 0;
+	unsigned int reg;
 
 	mutex_lock(&indio_dev->mlock);
 
 	reinit_completion(&info->completion);
 
-	regmap_write(info->regmap, SUNXI_GPADC_TP_INT_FIFOC,
-		     SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(1) |
-		     SUNXI_GPADC_TP_FIFO_FLUSH);
+	reg = SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(1) | SUNXI_GPADC_TP_FIFO_FLUSH;
+	regmap_update_bits(info->regmap, SUNXI_GPADC_TP_INT_FIFOC, reg, reg);
+
 	if (info->flags & SUNXI_GPADC_ARCH_SUN6I)
 		regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL1,
 			     SUNXI_GPADC_SUN6I_TP_MODE_EN);
 	else
 		regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL1,
 			     SUNXI_GPADC_TP_MODE_EN);
+
 	enable_irq(info->temp_data_irq);
 
 	if (!wait_for_completion_timeout(&info->completion,
@@ -211,7 +246,6 @@ out:
 	mutex_unlock(&indio_dev->mlock);
 
 	return ret;
-
 }
 
 static int sunxi_gpadc_read_raw(struct iio_dev *indio_dev,
@@ -219,15 +253,22 @@ static int sunxi_gpadc_read_raw(struct iio_dev *indio_dev,
 				int *val, int *val2, long mask)
 {
 	int ret;
+	struct sunxi_gpadc_dev *info = iio_priv(indio_dev);
 
 	switch (mask) {
 	case IIO_CHAN_INFO_PROCESSED:
+		if (info->buffered && !info->ts_attached)
+			return -EBUSY;
+
 		ret = sunxi_gpadc_temp_read(indio_dev, val);
 		if (ret)
 			return ret;
 
 		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_RAW:
+		if (info->buffered)
+			return -EBUSY;
+
 		ret = sunxi_gpadc_adc_read(indio_dev, chan->channel, val);
 		if (ret)
 			return ret;
@@ -261,7 +302,29 @@ static irqreturn_t sunxi_gpadc_temp_data_irq_handler(int irq, void *dev_id)
 static irqreturn_t sunxi_gpadc_fifo_data_irq_handler(int irq, void *dev_id)
 {
 	struct sunxi_gpadc_dev *info = dev_id;
-	int ret;
+	int ret, reg, i, fifo_count;
+
+	if (info->buffered) {
+		if (regmap_read(info->regmap, SUNXI_GPADC_TP_INT_FIFOS, &reg))
+			return IRQ_HANDLED;
+
+		fifo_count = (reg & SUNXI_GPADC_RXA_CNT) >> 8;
+		/* Sometimes, the interrupt occurs when the FIFO is empty. */
+		if (!fifo_count)
+			return IRQ_HANDLED;
+
+		for (i = 0; i < fifo_count; i++) {
+			if (regmap_read(info->regmap, SUNXI_GPADC_TP_DATA,
+					&info->buffer.buffer[i]))
+				return IRQ_HANDLED;
+		}
+
+		info->buffer.buff_size = i;
+
+		iio_push_to_buffers(info->indio_dev, &info->buffer);
+
+		return IRQ_HANDLED;
+	}
 
 	ret = regmap_read(info->regmap, SUNXI_GPADC_TP_DATA, &info->adc_data);
 	if (ret == 0)
@@ -270,6 +333,58 @@ static irqreturn_t sunxi_gpadc_fifo_data_irq_handler(int irq, void *dev_id)
 	return IRQ_HANDLED;
 }
 
+static int sunxi_gpadc_buffer_postenable(struct iio_dev *indio_dev)
+{
+	struct sunxi_gpadc_dev *info = iio_priv(indio_dev);
+	unsigned int reg;
+	int ret;
+
+	reg = SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(1) | SUNXI_GPADC_TP_FIFO_FLUSH;
+	regmap_update_bits(info->regmap, SUNXI_GPADC_TP_INT_FIFOC, reg, reg);
+
+	if (info->ts_attached) {
+		reg = SUNXI_GPADC_STYLUS_UP_DEBOUNCE(5) |
+		      SUNXI_GPADC_STYLUS_UP_DEBOUNCE_EN;
+
+		if (info->flags & SUNXI_GPADC_ARCH_SUN6I)
+			reg |= SUNXI_GPADC_SUN6I_TP_MODE_EN;
+		else
+			reg |= SUNXI_GPADC_TP_MODE_EN;
+	} else {
+		if (info->flags & SUNXI_GPADC_ARCH_SUN6I)
+			reg = SUNXI_GPADC_SUN6I_TP_MODE_EN |
+			      SUNXI_GPADC_SUN6I_TP_ADC_SELECT;
+		else
+			reg = SUNXI_GPADC_TP_MODE_EN |
+			      SUNXI_GPADC_TP_ADC_SELECT;
+	}
+
+	if (regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL1, reg))
+		return ret;
+
+	info->buffered = true;
+
+	enable_irq(info->fifo_data_irq);
+
+	return 0;
+}
+
+static int sunxi_gpadc_buffer_predisable(struct iio_dev *indio_dev)
+{
+	struct sunxi_gpadc_dev *info = iio_priv(indio_dev);
+
+	disable_irq(info->fifo_data_irq);
+
+	info->buffered = false;
+
+	return 0;
+}
+
+static const struct iio_buffer_setup_ops sunxi_gpadc_buffer_setup_ops = {
+	.postenable = sunxi_gpadc_buffer_postenable,
+	.predisable = sunxi_gpadc_buffer_predisable,
+};
+
 static int sunxi_gpadc_probe(struct platform_device *pdev)
 {
 	struct sunxi_gpadc_dev *info;
@@ -286,16 +401,20 @@ static int sunxi_gpadc_probe(struct platform_device *pdev)
 	info = iio_priv(indio_dev);
 
 	info->regmap = sunxi_gpadc_mfd_dev->regmap;
+	info->indio_dev = indio_dev;
 	init_completion(&info->completion);
 	indio_dev->name = dev_name(&pdev->dev);
 	indio_dev->dev.parent = &pdev->dev;
 	indio_dev->dev.of_node = pdev->dev.of_node;
 	indio_dev->info = &sunxi_gpadc_iio_info;
-	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
 	indio_dev->num_channels = ARRAY_SIZE(sunxi_gpadc_channels);
 	indio_dev->channels = sunxi_gpadc_channels;
+	indio_dev->setup_ops = &sunxi_gpadc_buffer_setup_ops;
 
 	info->flags = platform_get_device_id(pdev)->driver_data;
+	info->ts_attached = of_property_read_bool(pdev->dev.parent->of_node,
+						  "allwinner,ts-attached");
 
 	regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL0, SUNXI_GPADC_FS_DIV(7) |
 		     SUNXI_GPADC_ADC_CLK_DIVIDER(2) | SUNXI_GPADC_T_ACQ(63));
@@ -305,6 +424,8 @@ static int sunxi_gpadc_probe(struct platform_device *pdev)
 	else
 		regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL1,
 			     SUNXI_GPADC_TP_MODE_EN);
+	regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL2,
+		     SUNXI_GPADC_TP_SENSITIVE_ADJUST(15));
 	regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL3, SUNXI_GPADC_FILTER_EN |
 		     SUNXI_GPADC_FILTER_TYPE(1));
 	regmap_write(info->regmap, SUNXI_GPADC_TP_TPR,
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 4/5] input: touchscreen: support Allwinner SoCs' touchscreen
  2016-07-20  8:29 [PATCH 0/5] add resistive touchscreen support for new Allwinner SoCs' GPADC's driver Quentin Schulz
                   ` (2 preceding siblings ...)
  2016-07-20  8:29 ` [PATCH 3/5] iio: adc: sunxi-gpadc-iio: enable iio_buffers Quentin Schulz
@ 2016-07-20  8:29 ` Quentin Schulz
  2016-07-20 17:25   ` Dmitry Torokhov
                     ` (2 more replies)
  2016-07-20  8:29 ` [PATCH 5/5] mfd: sunxi-gpadc-mfd: probe sunxi-gpadc-ts driver Quentin Schulz
  4 siblings, 3 replies; 31+ messages in thread
From: Quentin Schulz @ 2016-07-20  8:29 UTC (permalink / raw)
  To: linux-arm-kernel

This adds support for Allwinner SoCs' (A10, A13 and A31) resistive
touchscreen. This driver is probed by the MFD sunxi-gpadc-mfd.

This driver uses ADC channels exposed through the IIO framework by
sunxi-gpadc-iio to get its data. When opening this input device, it will
start buffering in the ADC driver and enable a TP_UP_PENDING irq. The ADC
driver will fill in a buffer with all data and call the callback the input
device associated with this buffer. The input device will then read the
buffer two by two and send X and Y coordinates to the input framework based
on what it received from the ADC's buffer. When closing this input device,
the buffering is stopped.

Note that locations in the first received buffer after an TP_UP_PENDING irq
occurred are unreliable, thus dropped.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
---
 drivers/input/touchscreen/Kconfig          |  11 +-
 drivers/input/touchscreen/Makefile         |   2 +-
 drivers/input/touchscreen/sun4i-ts.c       | 419 -----------------------------
 drivers/input/touchscreen/sunxi-gpadc-ts.c | 195 ++++++++++++++
 4 files changed, 201 insertions(+), 426 deletions(-)
 delete mode 100644 drivers/input/touchscreen/sun4i-ts.c
 create mode 100644 drivers/input/touchscreen/sunxi-gpadc-ts.c

diff --git a/drivers/input/touchscreen/Kconfig b/drivers/input/touchscreen/Kconfig
index 8ecdc38..f16ce36 100644
--- a/drivers/input/touchscreen/Kconfig
+++ b/drivers/input/touchscreen/Kconfig
@@ -1072,14 +1072,13 @@ config TOUCHSCREEN_STMPE
 config TOUCHSCREEN_SUN4I
 	tristate "Allwinner sun4i resistive touchscreen controller support"
 	depends on ARCH_SUNXI || COMPILE_TEST
-	depends on HWMON
-	depends on THERMAL || !THERMAL_OF
+	depends on SUNXI_ADC
 	help
-	  This selects support for the resistive touchscreen controller
-	  found on Allwinner sunxi SoCs.
+	  This selects support for the resistive touchscreen controller found
+	  on Allwinner SoCs (A10, A13, A31).
 
-	  To compile this driver as a module, choose M here: the
-	  module will be called sun4i-ts.
+	  To compile this driver as a module, choose M here: the module will be
+	  called sunxi-gpadc-ts.
 
 config TOUCHSCREEN_SUR40
 	tristate "Samsung SUR40 (Surface 2.0/PixelSense) touchscreen"
diff --git a/drivers/input/touchscreen/Makefile b/drivers/input/touchscreen/Makefile
index f42975e..bdc1889 100644
--- a/drivers/input/touchscreen/Makefile
+++ b/drivers/input/touchscreen/Makefile
@@ -65,7 +65,7 @@ obj-$(CONFIG_TOUCHSCREEN_PIXCIR)	+= pixcir_i2c_ts.o
 obj-$(CONFIG_TOUCHSCREEN_S3C2410)	+= s3c2410_ts.o
 obj-$(CONFIG_TOUCHSCREEN_ST1232)	+= st1232.o
 obj-$(CONFIG_TOUCHSCREEN_STMPE)		+= stmpe-ts.o
-obj-$(CONFIG_TOUCHSCREEN_SUN4I)		+= sun4i-ts.o
+obj-$(CONFIG_TOUCHSCREEN_SUN4I)		+= sunxi-gpadc-ts.o
 obj-$(CONFIG_TOUCHSCREEN_SUR40)		+= sur40.o
 obj-$(CONFIG_TOUCHSCREEN_TI_AM335X_TSC)	+= ti_am335x_tsc.o
 obj-$(CONFIG_TOUCHSCREEN_TOUCHIT213)	+= touchit213.o
diff --git a/drivers/input/touchscreen/sun4i-ts.c b/drivers/input/touchscreen/sun4i-ts.c
deleted file mode 100644
index d07dd29..0000000
--- a/drivers/input/touchscreen/sun4i-ts.c
+++ /dev/null
@@ -1,419 +0,0 @@
-/*
- * Allwinner sunxi resistive touchscreen controller driver
- *
- * Copyright (C) 2013 - 2014 Hans de Goede <hdegoede@redhat.com>
- *
- * The hwmon parts are based on work by Corentin LABBE which is:
- * Copyright (C) 2013 Corentin LABBE <clabbe.montjoie@gmail.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- */
-
-/*
- * The sun4i-ts controller is capable of detecting a second touch, but when a
- * second touch is present then the accuracy becomes so bad the reported touch
- * location is not useable.
- *
- * The original android driver contains some complicated heuristics using the
- * aprox. distance between the 2 touches to see if the user is making a pinch
- * open / close movement, and then reports emulated multi-touch events around
- * the last touch coordinate (as the dual-touch coordinates are worthless).
- *
- * These kinds of heuristics are just asking for trouble (and don't belong
- * in the kernel). So this driver offers straight forward, reliable single
- * touch functionality only.
- *
- * s.a. A20 User Manual "1.15 TP" (Documentation/arm/sunxi/README)
- * (looks like the description in the A20 User Manual v1.3 is better
- * than the one in the A10 User Manual v.1.5)
- */
-
-#include <linux/err.h>
-#include <linux/hwmon.h>
-#include <linux/thermal.h>
-#include <linux/init.h>
-#include <linux/input.h>
-#include <linux/interrupt.h>
-#include <linux/io.h>
-#include <linux/module.h>
-#include <linux/of_platform.h>
-#include <linux/platform_device.h>
-#include <linux/slab.h>
-
-#define TP_CTRL0		0x00
-#define TP_CTRL1		0x04
-#define TP_CTRL2		0x08
-#define TP_CTRL3		0x0c
-#define TP_INT_FIFOC		0x10
-#define TP_INT_FIFOS		0x14
-#define TP_TPR			0x18
-#define TP_CDAT			0x1c
-#define TEMP_DATA		0x20
-#define TP_DATA			0x24
-
-/* TP_CTRL0 bits */
-#define ADC_FIRST_DLY(x)	((x) << 24) /* 8 bits */
-#define ADC_FIRST_DLY_MODE(x)	((x) << 23)
-#define ADC_CLK_SEL(x)		((x) << 22)
-#define ADC_CLK_DIV(x)		((x) << 20) /* 3 bits */
-#define FS_DIV(x)		((x) << 16) /* 4 bits */
-#define T_ACQ(x)		((x) << 0) /* 16 bits */
-
-/* TP_CTRL1 bits */
-#define STYLUS_UP_DEBOUN(x)	((x) << 12) /* 8 bits */
-#define STYLUS_UP_DEBOUN_EN(x)	((x) << 9)
-#define TOUCH_PAN_CALI_EN(x)	((x) << 6)
-#define TP_DUAL_EN(x)		((x) << 5)
-#define TP_MODE_EN(x)		((x) << 4)
-#define TP_ADC_SELECT(x)	((x) << 3)
-#define ADC_CHAN_SELECT(x)	((x) << 0)  /* 3 bits */
-
-/* on sun6i, bits 3~6 are left shifted by 1 to 4~7 */
-#define SUN6I_TP_MODE_EN(x)	((x) << 5)
-
-/* TP_CTRL2 bits */
-#define TP_SENSITIVE_ADJUST(x)	((x) << 28) /* 4 bits */
-#define TP_MODE_SELECT(x)	((x) << 26) /* 2 bits */
-#define PRE_MEA_EN(x)		((x) << 24)
-#define PRE_MEA_THRE_CNT(x)	((x) << 0) /* 24 bits */
-
-/* TP_CTRL3 bits */
-#define FILTER_EN(x)		((x) << 2)
-#define FILTER_TYPE(x)		((x) << 0)  /* 2 bits */
-
-/* TP_INT_FIFOC irq and fifo mask / control bits */
-#define TEMP_IRQ_EN(x)		((x) << 18)
-#define OVERRUN_IRQ_EN(x)	((x) << 17)
-#define DATA_IRQ_EN(x)		((x) << 16)
-#define TP_DATA_XY_CHANGE(x)	((x) << 13)
-#define FIFO_TRIG(x)		((x) << 8)  /* 5 bits */
-#define DATA_DRQ_EN(x)		((x) << 7)
-#define FIFO_FLUSH(x)		((x) << 4)
-#define TP_UP_IRQ_EN(x)		((x) << 1)
-#define TP_DOWN_IRQ_EN(x)	((x) << 0)
-
-/* TP_INT_FIFOS irq and fifo status bits */
-#define TEMP_DATA_PENDING	BIT(18)
-#define FIFO_OVERRUN_PENDING	BIT(17)
-#define FIFO_DATA_PENDING	BIT(16)
-#define TP_IDLE_FLG		BIT(2)
-#define TP_UP_PENDING		BIT(1)
-#define TP_DOWN_PENDING		BIT(0)
-
-/* TP_TPR bits */
-#define TEMP_ENABLE(x)		((x) << 16)
-#define TEMP_PERIOD(x)		((x) << 0)  /* t = x * 256 * 16 / clkin */
-
-struct sun4i_ts_data {
-	struct device *dev;
-	struct input_dev *input;
-	void __iomem *base;
-	unsigned int irq;
-	bool ignore_fifo_data;
-	int temp_data;
-	int temp_offset;
-	int temp_step;
-};
-
-static void sun4i_ts_irq_handle_input(struct sun4i_ts_data *ts, u32 reg_val)
-{
-	u32 x, y;
-
-	if (reg_val & FIFO_DATA_PENDING) {
-		x = readl(ts->base + TP_DATA);
-		y = readl(ts->base + TP_DATA);
-		/* The 1st location reported after an up event is unreliable */
-		if (!ts->ignore_fifo_data) {
-			input_report_abs(ts->input, ABS_X, x);
-			input_report_abs(ts->input, ABS_Y, y);
-			/*
-			 * The hardware has a separate down status bit, but
-			 * that gets set before we get the first location,
-			 * resulting in reporting a click on the old location.
-			 */
-			input_report_key(ts->input, BTN_TOUCH, 1);
-			input_sync(ts->input);
-		} else {
-			ts->ignore_fifo_data = false;
-		}
-	}
-
-	if (reg_val & TP_UP_PENDING) {
-		ts->ignore_fifo_data = true;
-		input_report_key(ts->input, BTN_TOUCH, 0);
-		input_sync(ts->input);
-	}
-}
-
-static irqreturn_t sun4i_ts_irq(int irq, void *dev_id)
-{
-	struct sun4i_ts_data *ts = dev_id;
-	u32 reg_val;
-
-	reg_val  = readl(ts->base + TP_INT_FIFOS);
-
-	if (reg_val & TEMP_DATA_PENDING)
-		ts->temp_data = readl(ts->base + TEMP_DATA);
-
-	if (ts->input)
-		sun4i_ts_irq_handle_input(ts, reg_val);
-
-	writel(reg_val, ts->base + TP_INT_FIFOS);
-
-	return IRQ_HANDLED;
-}
-
-static int sun4i_ts_open(struct input_dev *dev)
-{
-	struct sun4i_ts_data *ts = input_get_drvdata(dev);
-
-	/* Flush, set trig level to 1, enable temp, data and up irqs */
-	writel(TEMP_IRQ_EN(1) | DATA_IRQ_EN(1) | FIFO_TRIG(1) | FIFO_FLUSH(1) |
-		TP_UP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
-
-	return 0;
-}
-
-static void sun4i_ts_close(struct input_dev *dev)
-{
-	struct sun4i_ts_data *ts = input_get_drvdata(dev);
-
-	/* Deactivate all input IRQs */
-	writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
-}
-
-static int sun4i_get_temp(const struct sun4i_ts_data *ts, int *temp)
-{
-	/* No temp_data until the first irq */
-	if (ts->temp_data == -1)
-		return -EAGAIN;
-
-	*temp = ts->temp_data * ts->temp_step - ts->temp_offset;
-
-	return 0;
-}
-
-static int sun4i_get_tz_temp(void *data, int *temp)
-{
-	return sun4i_get_temp(data, temp);
-}
-
-static struct thermal_zone_of_device_ops sun4i_ts_tz_ops = {
-	.get_temp = sun4i_get_tz_temp,
-};
-
-static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
-			 char *buf)
-{
-	struct sun4i_ts_data *ts = dev_get_drvdata(dev);
-	int temp;
-	int error;
-
-	error = sun4i_get_temp(ts, &temp);
-	if (error)
-		return error;
-
-	return sprintf(buf, "%d\n", temp);
-}
-
-static ssize_t show_temp_label(struct device *dev,
-			      struct device_attribute *devattr, char *buf)
-{
-	return sprintf(buf, "SoC temperature\n");
-}
-
-static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
-static DEVICE_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL);
-
-static struct attribute *sun4i_ts_attrs[] = {
-	&dev_attr_temp1_input.attr,
-	&dev_attr_temp1_label.attr,
-	NULL
-};
-ATTRIBUTE_GROUPS(sun4i_ts);
-
-static int sun4i_ts_probe(struct platform_device *pdev)
-{
-	struct sun4i_ts_data *ts;
-	struct device *dev = &pdev->dev;
-	struct device_node *np = dev->of_node;
-	struct device *hwmon;
-	int error;
-	u32 reg;
-	bool ts_attached;
-	u32 tp_sensitive_adjust = 15;
-	u32 filter_type = 1;
-
-	ts = devm_kzalloc(dev, sizeof(struct sun4i_ts_data), GFP_KERNEL);
-	if (!ts)
-		return -ENOMEM;
-
-	ts->dev = dev;
-	ts->ignore_fifo_data = true;
-	ts->temp_data = -1;
-	if (of_device_is_compatible(np, "allwinner,sun6i-a31-ts")) {
-		/* Allwinner SDK has temperature (C) = (value / 6) - 271 */
-		ts->temp_offset = 271000;
-		ts->temp_step = 167;
-	} else if (of_device_is_compatible(np, "allwinner,sun4i-a10-ts")) {
-		/*
-		 * The A10 temperature sensor has quite a wide spread, these
-		 * parameters are based on the averaging of the calibration
-		 * results of 4 completely different boards, with a spread of
-		 * temp_step from 0.096 - 0.170 and temp_offset from 176 - 331.
-		 */
-		ts->temp_offset = 257000;
-		ts->temp_step = 133;
-	} else {
-		/*
-		 * The user manuals do not contain the formula for calculating
-		 * the temperature. The formula used here is from the AXP209,
-		 * which is designed by X-Powers, an affiliate of Allwinner:
-		 *
-		 *     temperature (C) = (value * 0.1) - 144.7
-		 *
-		 * Allwinner does not have any documentation whatsoever for
-		 * this hardware. Moreover, it is claimed that the sensor
-		 * is inaccurate and cannot work properly.
-		 */
-		ts->temp_offset = 144700;
-		ts->temp_step = 100;
-	}
-
-	ts_attached = of_property_read_bool(np, "allwinner,ts-attached");
-	if (ts_attached) {
-		ts->input = devm_input_allocate_device(dev);
-		if (!ts->input)
-			return -ENOMEM;
-
-		ts->input->name = pdev->name;
-		ts->input->phys = "sun4i_ts/input0";
-		ts->input->open = sun4i_ts_open;
-		ts->input->close = sun4i_ts_close;
-		ts->input->id.bustype = BUS_HOST;
-		ts->input->id.vendor = 0x0001;
-		ts->input->id.product = 0x0001;
-		ts->input->id.version = 0x0100;
-		ts->input->evbit[0] =  BIT(EV_SYN) | BIT(EV_KEY) | BIT(EV_ABS);
-		__set_bit(BTN_TOUCH, ts->input->keybit);
-		input_set_abs_params(ts->input, ABS_X, 0, 4095, 0, 0);
-		input_set_abs_params(ts->input, ABS_Y, 0, 4095, 0, 0);
-		input_set_drvdata(ts->input, ts);
-	}
-
-	ts->base = devm_ioremap_resource(dev,
-			      platform_get_resource(pdev, IORESOURCE_MEM, 0));
-	if (IS_ERR(ts->base))
-		return PTR_ERR(ts->base);
-
-	ts->irq = platform_get_irq(pdev, 0);
-	error = devm_request_irq(dev, ts->irq, sun4i_ts_irq, 0, "sun4i-ts", ts);
-	if (error)
-		return error;
-
-	/*
-	 * Select HOSC clk, clkin = clk / 6, adc samplefreq = clkin / 8192,
-	 * t_acq = clkin / (16 * 64)
-	 */
-	writel(ADC_CLK_SEL(0) | ADC_CLK_DIV(2) | FS_DIV(7) | T_ACQ(63),
-	       ts->base + TP_CTRL0);
-
-	/*
-	 * tp_sensitive_adjust is an optional property
-	 * tp_mode = 0 : only x and y coordinates, as we don't use dual touch
-	 */
-	of_property_read_u32(np, "allwinner,tp-sensitive-adjust",
-			     &tp_sensitive_adjust);
-	writel(TP_SENSITIVE_ADJUST(tp_sensitive_adjust) | TP_MODE_SELECT(0),
-	       ts->base + TP_CTRL2);
-
-	/*
-	 * Enable median and averaging filter, optional property for
-	 * filter type.
-	 */
-	of_property_read_u32(np, "allwinner,filter-type", &filter_type);
-	writel(FILTER_EN(1) | FILTER_TYPE(filter_type), ts->base + TP_CTRL3);
-
-	/* Enable temperature measurement, period 1953 (2 seconds) */
-	writel(TEMP_ENABLE(1) | TEMP_PERIOD(1953), ts->base + TP_TPR);
-
-	/*
-	 * Set stylus up debounce to aprox 10 ms, enable debounce, and
-	 * finally enable tp mode.
-	 */
-	reg = STYLUS_UP_DEBOUN(5) | STYLUS_UP_DEBOUN_EN(1);
-	if (of_device_is_compatible(np, "allwinner,sun6i-a31-ts"))
-		reg |= SUN6I_TP_MODE_EN(1);
-	else
-		reg |= TP_MODE_EN(1);
-	writel(reg, ts->base + TP_CTRL1);
-
-	/*
-	 * The thermal core does not register hwmon devices for DT-based
-	 * thermal zone sensors, such as this one.
-	 */
-	hwmon = devm_hwmon_device_register_with_groups(ts->dev, "sun4i_ts",
-						       ts, sun4i_ts_groups);
-	if (IS_ERR(hwmon))
-		return PTR_ERR(hwmon);
-
-	devm_thermal_zone_of_sensor_register(ts->dev, 0, ts, &sun4i_ts_tz_ops);
-
-	writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
-
-	if (ts_attached) {
-		error = input_register_device(ts->input);
-		if (error) {
-			writel(0, ts->base + TP_INT_FIFOC);
-			return error;
-		}
-	}
-
-	platform_set_drvdata(pdev, ts);
-	return 0;
-}
-
-static int sun4i_ts_remove(struct platform_device *pdev)
-{
-	struct sun4i_ts_data *ts = platform_get_drvdata(pdev);
-
-	/* Explicit unregister to avoid open/close changing the imask later */
-	if (ts->input)
-		input_unregister_device(ts->input);
-
-	/* Deactivate all IRQs */
-	writel(0, ts->base + TP_INT_FIFOC);
-
-	return 0;
-}
-
-static const struct of_device_id sun4i_ts_of_match[] = {
-	{ .compatible = "allwinner,sun4i-a10-ts", },
-	{ .compatible = "allwinner,sun5i-a13-ts", },
-	{ .compatible = "allwinner,sun6i-a31-ts", },
-	{ /* sentinel */ }
-};
-MODULE_DEVICE_TABLE(of, sun4i_ts_of_match);
-
-static struct platform_driver sun4i_ts_driver = {
-	.driver = {
-		.name	= "sun4i-ts",
-		.of_match_table = of_match_ptr(sun4i_ts_of_match),
-	},
-	.probe	= sun4i_ts_probe,
-	.remove	= sun4i_ts_remove,
-};
-
-module_platform_driver(sun4i_ts_driver);
-
-MODULE_DESCRIPTION("Allwinner sun4i resistive touchscreen controller driver");
-MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
-MODULE_LICENSE("GPL");
diff --git a/drivers/input/touchscreen/sunxi-gpadc-ts.c b/drivers/input/touchscreen/sunxi-gpadc-ts.c
new file mode 100644
index 0000000..410d14f
--- /dev/null
+++ b/drivers/input/touchscreen/sunxi-gpadc-ts.c
@@ -0,0 +1,195 @@
+/* Touchscreen driver for Allwinner SoCs (A10, A13, A31) GPADC
+ *
+ * Copyright (c) 2016 Quentin Schulz <quentin.schulz@free-electrons>
+ *
+ * This program is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License version 2 as published by the
+ * Free Software Foundation.
+ */
+
+#include <linux/init.h>
+#include <linux/input.h>
+#include <linux/interrupt.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+
+#include <linux/iio/consumer.h>
+#include <linux/mfd/sunxi-gpadc-mfd.h>
+
+struct sunxi_gpadc_ts {
+	struct iio_cb_buffer	*buffer;
+	struct input_dev	*input;
+	struct platform_device	*pdev;
+	unsigned int		tp_up_irq;
+	bool			ignore_fifo_data;
+};
+
+static int sunxi_gpadc_ts_open(struct input_dev *dev)
+{
+	struct sunxi_gpadc_ts *info = input_get_drvdata(dev);
+	int ret;
+
+	ret = iio_channel_start_all_cb(info->buffer);
+	if (ret) {
+		dev_err(dev->dev.parent,
+			"failed to start iio channels with callback\n");
+		return ret;
+	}
+
+	enable_irq(info->tp_up_irq);
+
+	return 0;
+}
+
+static void sunxi_gpadc_ts_close(struct input_dev *dev)
+{
+	struct sunxi_gpadc_ts *info = input_get_drvdata(dev);
+
+	iio_channel_stop_all_cb(info->buffer);
+
+	disable_irq(info->tp_up_irq);
+}
+
+/*
+ * This function will be called by iio_push_to_buffers from another driver
+ * (namely sunxi-gpadc-iio). It will be passed the buffer filled with input
+ * values (X value then Y value) and the sunxi_gpadc_ts structure representing
+ * the device.
+ */
+static int sunxi_gpadc_ts_callback(const void *data, void *private)
+{
+	const struct sunxi_gpadc_buffer *buffer = data;
+	struct sunxi_gpadc_ts *info = private;
+	int i = 0;
+
+	/* Locations in the first buffer after an up event are unreliable */
+	if (info->ignore_fifo_data) {
+		info->ignore_fifo_data = false;
+		return 0;
+	}
+
+	while (i + 1 < buffer->buff_size) {
+		input_event(info->input, EV_ABS, ABS_X, buffer->buffer[i++]);
+		input_event(info->input, EV_ABS, ABS_Y, buffer->buffer[i++]);
+		input_event(info->input, EV_KEY, BTN_TOUCH, 1);
+		input_sync(info->input);
+	}
+
+	return 0;
+}
+
+static irqreturn_t sunxi_gpadc_tp_up_irq_handler(int irq, void *dev_id)
+{
+	struct sunxi_gpadc_ts *info = dev_id;
+
+	info->ignore_fifo_data = true;
+
+	input_event(info->input, EV_KEY, BTN_TOUCH, 0);
+	input_sync(info->input);
+
+	return IRQ_HANDLED;
+}
+
+static int sunxi_gpadc_ts_probe(struct platform_device *pdev)
+{
+	struct input_dev *input;
+	struct sunxi_gpadc_ts *info;
+	int ret, irq;
+	struct sunxi_gpadc_mfd_dev *sunxi_gpadc_mfd_dev;
+
+	input = devm_input_allocate_device(&pdev->dev);
+	if (!input)
+		return -ENOMEM;
+
+	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
+	if (!info)
+		return -ENOMEM;
+
+	info->buffer = iio_channel_get_all_cb(&pdev->dev,
+					      &sunxi_gpadc_ts_callback,
+					      (void *)info);
+	if (IS_ERR(info->buffer)) {
+		if (PTR_ERR(info->buffer) == -ENODEV)
+			return -EPROBE_DEFER;
+		return PTR_ERR(info->buffer);
+	}
+
+	info->pdev = pdev;
+	info->input = input;
+	info->ignore_fifo_data = false;
+	platform_set_drvdata(pdev, info);
+
+	input->dev.parent = &pdev->dev;
+	input->name = "sunxi-gpadc-ts";
+	input->id.bustype = BUS_HOST;
+	input->open = sunxi_gpadc_ts_open;
+	input->close = sunxi_gpadc_ts_close;
+	__set_bit(EV_SYN, input->evbit);
+	input_set_capability(input, EV_KEY, BTN_TOUCH);
+	input_set_abs_params(input, ABS_X, 0, 4095, 0, 0);
+	input_set_abs_params(input, ABS_Y, 0, 4095, 0, 0);
+	input_set_drvdata(input, info);
+
+	irq = platform_get_irq_byname(pdev, "TP_UP_PENDING");
+	if (irq < 0) {
+		dev_err(&pdev->dev, "no TP_UP_PENDING interrupt registered\n");
+		ret = irq;
+		goto err;
+	}
+
+	sunxi_gpadc_mfd_dev = dev_get_drvdata(pdev->dev.parent);
+
+	irq = regmap_irq_get_virq(sunxi_gpadc_mfd_dev->regmap_irqc, irq);
+	ret = devm_request_any_context_irq(&pdev->dev, irq,
+					   sunxi_gpadc_tp_up_irq_handler, 0,
+					   "tp_up", info);
+	if (ret < 0) {
+		dev_err(&pdev->dev,
+			"could not request TP_UP_PENDING interrupt: %d\n", ret);
+		goto err;
+	}
+
+	info->tp_up_irq = irq;
+	disable_irq(irq);
+
+	ret = input_register_device(input);
+	if (ret) {
+		dev_err(&pdev->dev, "failed to register input device\n");
+		goto err;
+	}
+
+	return 0;
+
+err:
+	iio_channel_release_all_cb(info->buffer);
+
+	return ret;
+}
+
+static int sunxi_gpadc_ts_remove(struct platform_device *pdev)
+{
+	struct sunxi_gpadc_ts *info = platform_get_drvdata(pdev);
+
+	iio_channel_stop_all_cb(info->buffer);
+	iio_channel_release_all_cb(info->buffer);
+
+	disable_irq(info->tp_up_irq);
+
+	return 0;
+}
+
+static struct platform_driver sunxi_gpadc_ts_driver = {
+	.driver = {
+		.name = "sunxi-gpadc-ts",
+	},
+	.probe = sunxi_gpadc_ts_probe,
+	.remove = sunxi_gpadc_ts_remove,
+};
+
+module_platform_driver(sunxi_gpadc_ts_driver);
+
+MODULE_DESCRIPTION("Touchscreen driver for Allwinner SoCs (A10, A13, A31) GPADC");
+MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
+MODULE_LICENSE("GPL v2");
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 5/5] mfd: sunxi-gpadc-mfd: probe sunxi-gpadc-ts driver
  2016-07-20  8:29 [PATCH 0/5] add resistive touchscreen support for new Allwinner SoCs' GPADC's driver Quentin Schulz
                   ` (3 preceding siblings ...)
  2016-07-20  8:29 ` [PATCH 4/5] input: touchscreen: support Allwinner SoCs' touchscreen Quentin Schulz
@ 2016-07-20  8:29 ` Quentin Schulz
  2016-07-21  6:08   ` Maxime Ripard
  4 siblings, 1 reply; 31+ messages in thread
From: Quentin Schulz @ 2016-07-20  8:29 UTC (permalink / raw)
  To: linux-arm-kernel

This probes the touchscreen driver for Allwinner SoCs (A10, A13 and A31)
when the property "allwinner,ts-attached" is set in the GPADC (rtp) node of
the DT.

Some comestic modifications done to shorten and increase readability of the
code.

Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
---
 drivers/mfd/sunxi-gpadc-mfd.c | 115 ++++++++++++++++++++++++++++--------------
 1 file changed, 77 insertions(+), 38 deletions(-)

diff --git a/drivers/mfd/sunxi-gpadc-mfd.c b/drivers/mfd/sunxi-gpadc-mfd.c
index 05a000b..9b9ed0b 100644
--- a/drivers/mfd/sunxi-gpadc-mfd.c
+++ b/drivers/mfd/sunxi-gpadc-mfd.c
@@ -21,6 +21,11 @@
 #define SUNXI_IRQ_TEMP_DATA	1
 #define SUNXI_IRQ_TP_UP		2
 
+#define SUNXI_GPADC_MFD_CELL(_name, _resources, _num_resources) {	\
+	.name = _name,							\
+	.resources = _resources,					\
+	.num_resources = _num_resources					\
+}
 
 static struct resource adc_resources[] = {
 	{
@@ -64,33 +69,45 @@ static const struct regmap_irq_chip sunxi_gpadc_mfd_regmap_irq_chip = {
 };
 
 static struct mfd_cell sun4i_gpadc_mfd_cells[] = {
-	{
-		.name	= "sun4i-a10-gpadc-iio",
-		.resources = adc_resources,
-		.num_resources = ARRAY_SIZE(adc_resources),
-	}, {
-		.name = "iio_hwmon",
-	}
+	SUNXI_GPADC_MFD_CELL("sun4i-a10-gpadc-iio", adc_resources,
+			     ARRAY_SIZE(adc_resources)),
+	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
 };
 
 static struct mfd_cell sun5i_gpadc_mfd_cells[] = {
-	{
-		.name	= "sun5i-a13-gpadc-iio",
-		.resources = adc_resources,
-		.num_resources = ARRAY_SIZE(adc_resources),
-	}, {
-		.name = "iio_hwmon",
-	},
+	SUNXI_GPADC_MFD_CELL("sun5i-a13-gpadc-iio", adc_resources,
+			     ARRAY_SIZE(adc_resources)),
+	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
 };
 
 static struct mfd_cell sun6i_gpadc_mfd_cells[] = {
-	{
-		.name	= "sun6i-a31-gpadc-iio",
-		.resources = adc_resources,
-		.num_resources = ARRAY_SIZE(adc_resources),
-	}, {
-		.name = "iio_hwmon",
-	},
+	SUNXI_GPADC_MFD_CELL("sun6i-a31-gpadc-iio", adc_resources,
+			     ARRAY_SIZE(adc_resources)),
+	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
+};
+
+static struct mfd_cell sun4i_gpadc_mfd_cells_ts[] = {
+	SUNXI_GPADC_MFD_CELL("sun6i-a31-gpadc-iio", adc_resources,
+			     ARRAY_SIZE(adc_resources)),
+	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
+	SUNXI_GPADC_MFD_CELL("sunxi-gpadc-ts", ts_resources,
+			     ARRAY_SIZE(ts_resources)),
+};
+
+static struct mfd_cell sun5i_gpadc_mfd_cells_ts[] = {
+	SUNXI_GPADC_MFD_CELL("sun5i-a13-gpadc-iio", adc_resources,
+			     ARRAY_SIZE(adc_resources)),
+	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
+	SUNXI_GPADC_MFD_CELL("sunxi-gpadc-ts", ts_resources,
+			     ARRAY_SIZE(ts_resources)),
+};
+
+static struct mfd_cell sun6i_gpadc_mfd_cells_ts[] = {
+	SUNXI_GPADC_MFD_CELL("sun6i-a31-gpadc-iio", adc_resources,
+			     ARRAY_SIZE(adc_resources)),
+	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
+	SUNXI_GPADC_MFD_CELL("sunxi-gpadc-ts", ts_resources,
+			     ARRAY_SIZE(ts_resources)),
 };
 
 static const struct regmap_config sunxi_gpadc_mfd_regmap_config = {
@@ -142,23 +159,45 @@ static int sunxi_gpadc_mfd_probe(struct platform_device *pdev)
 	}
 
 	if (of_device_is_compatible(pdev->dev.of_node,
-				    "allwinner,sun4i-a10-ts"))
-		ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
-				      sun4i_gpadc_mfd_cells,
-				      ARRAY_SIZE(sun4i_gpadc_mfd_cells), NULL,
-				      0, NULL);
-	else if (of_device_is_compatible(pdev->dev.of_node,
-					 "allwinner,sun5i-a13-ts"))
-		ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
-				      sun5i_gpadc_mfd_cells,
-				      ARRAY_SIZE(sun5i_gpadc_mfd_cells), NULL,
-				      0, NULL);
-	else if (of_device_is_compatible(pdev->dev.of_node,
-					 "allwinner,sun6i-a31-ts"))
-		ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
-				      sun6i_gpadc_mfd_cells,
-				      ARRAY_SIZE(sun6i_gpadc_mfd_cells), NULL,
-				      0, NULL);
+				    "allwinner,sun4i-a10-ts")) {
+		if (of_property_read_bool(pdev->dev.of_node,
+					  "allwinner,ts-attached"))
+			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
+					      sun4i_gpadc_mfd_cells_ts,
+					      ARRAY_SIZE(sun4i_gpadc_mfd_cells_ts),
+					      NULL, 0, NULL);
+		else
+			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
+					      sun4i_gpadc_mfd_cells,
+					      ARRAY_SIZE(sun4i_gpadc_mfd_cells),
+					      NULL, 0, NULL);
+	} else if (of_device_is_compatible(pdev->dev.of_node,
+					 "allwinner,sun5i-a13-ts")) {
+		if (of_property_read_bool(pdev->dev.of_node,
+					  "allwinner,ts-attached"))
+			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
+					      sun5i_gpadc_mfd_cells_ts,
+					      ARRAY_SIZE(sun5i_gpadc_mfd_cells_ts),
+					      NULL, 0, NULL);
+		else
+			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
+					      sun5i_gpadc_mfd_cells,
+					      ARRAY_SIZE(sun5i_gpadc_mfd_cells),
+					      NULL, 0, NULL);
+	} else if (of_device_is_compatible(pdev->dev.of_node,
+					 "allwinner,sun6i-a31-ts")) {
+		if (of_property_read_bool(pdev->dev.of_node,
+					  "allwinner,ts-attached"))
+			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
+					      sun6i_gpadc_mfd_cells_ts,
+					      ARRAY_SIZE(sun6i_gpadc_mfd_cells_ts),
+					      NULL, 0, NULL);
+		else
+			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
+					      sun6i_gpadc_mfd_cells,
+					      ARRAY_SIZE(sun6i_gpadc_mfd_cells),
+					      NULL, 0, NULL);
+	}
 
 	if (ret) {
 		dev_err(&pdev->dev, "failed to add MFD devices: %d\n", ret);
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 31+ messages in thread

* [PATCH 3/5] iio: adc: sunxi-gpadc-iio: enable iio_buffers
  2016-07-20  8:29 ` [PATCH 3/5] iio: adc: sunxi-gpadc-iio: enable iio_buffers Quentin Schulz
@ 2016-07-20  8:38   ` Peter Meerwald-Stadler
  2016-07-20  8:57     ` Quentin Schulz
  2016-07-24 11:03   ` Jonathan Cameron
  1 sibling, 1 reply; 31+ messages in thread
From: Peter Meerwald-Stadler @ 2016-07-20  8:38 UTC (permalink / raw)
  To: linux-arm-kernel


> This enables the use of buffers on ADC channels of sunxi-gpadc-iio driver.
> It also prepares the code which will be used by the touchscreen driver
> named sunxi-gpadc-ts.
> 
> The GPADC on Allwinner SoCs (A10, A13 and A31) has a 12 bits register for
> conversion's data. The GPADC uses the same ADC channels for the ADC and the
> touchscreen therefore exposes these channels to the sunxi-gpadc-ts iio
> consumer which will be in charge of reading data from these channels for
> the input framework.
> 
> The temperature can only be read when in touchscreen mode. This means if
> the buffers are being used for the ADC, the temperature sensor cannot be
> read.
> 
> When a FIFO_DATA_PENDING irq occurs, its handler will read the entire FIFO
> and fill a buffer before sending it to the consumers which registered in
> IIO for the ADC channels.
> 
> When a consumer starts buffering ADC channels,
> sunxi_gpadc_buffer_postenable is called and will enable FIFO_DATA_PENDING
> irq and select the mode in which the GPADC should run (ADC or touchscreen)
> depending on a property of the DT ("allwinner,ts-attached").
> When the consumer stops buffering, it disables the same irq.

comments below
 
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> ---
>  drivers/iio/adc/Kconfig           |   1 +
>  drivers/iio/adc/sunxi-gpadc-iio.c | 153 ++++++++++++++++++++++++++++++++++----
>  2 files changed, 138 insertions(+), 16 deletions(-)
> 
> diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
> index 184856f..15e3b08 100644
> --- a/drivers/iio/adc/Kconfig
> +++ b/drivers/iio/adc/Kconfig
> @@ -342,6 +342,7 @@ config SUNXI_ADC
>  	tristate "ADC driver for sunxi platforms"
>  	depends on IIO
>  	depends on MFD_SUNXI_ADC
> +	depends on IIO_BUFFER_CB
>  	help
>  	  Say yes here to build support for Allwinner (A10, A13 and A31) SoCs
>  	  ADC. This ADC provides 4 channels which can be used as an ADC or as a
> diff --git a/drivers/iio/adc/sunxi-gpadc-iio.c b/drivers/iio/adc/sunxi-gpadc-iio.c
> index 87cc913..2e44ca7 100644
> --- a/drivers/iio/adc/sunxi-gpadc-iio.c
> +++ b/drivers/iio/adc/sunxi-gpadc-iio.c
> @@ -16,8 +16,9 @@
>  #include <linux/platform_device.h>
>  #include <linux/regmap.h>
>  
> -#include <linux/iio/iio.h>
> +#include <linux/iio/buffer.h>
>  #include <linux/iio/driver.h>
> +#include <linux/iio/iio.h>
>  #include <linux/iio/machine.h>
>  #include <linux/mfd/sunxi-gpadc-mfd.h>
>  
> @@ -71,6 +72,7 @@
>  #define SUNXI_GPADC_TP_DATA_XY_CHANGE		BIT(13)
>  #define SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(x)	((x) << 8)  /* 5 bits */
>  #define SUNXI_GPADC_TP_DATA_DRQ_EN		BIT(7)
> +/* Be careful, flushing FIFO spawns SUNXI_GPADC_FIFO_DATA_PENDING interrupts */
>  #define SUNXI_GPADC_TP_FIFO_FLUSH		BIT(4)
>  #define SUNXI_GPADC_TP_UP_IRQ_EN		BIT(1)
>  #define SUNXI_GPADC_TP_DOWN_IRQ_EN		BIT(0)
> @@ -79,6 +81,7 @@
>  #define SUNXI_GPADC_TEMP_DATA_PENDING		BIT(18)
>  #define SUNXI_GPADC_FIFO_OVERRUN_PENDING	BIT(17)
>  #define SUNXI_GPADC_FIFO_DATA_PENDING		BIT(16)
> +#define SUNXI_GPADC_RXA_CNT			GENMASK(12, 8)
>  #define SUNXI_GPADC_TP_IDLE_FLG			BIT(2)
>  #define SUNXI_GPADC_TP_UP_PENDING		BIT(1)
>  #define SUNXI_GPADC_TP_DOWN_PENDING		BIT(0)
> @@ -101,19 +104,43 @@ struct sunxi_gpadc_dev {
>  	unsigned int			fifo_data_irq;
>  	unsigned int			temp_data_irq;
>  	unsigned int			flags;
> +	struct iio_dev			*indio_dev;
> +	struct sunxi_gpadc_buffer	buffer;
> +	bool				ts_attached;
> +	bool				buffered;

why add buffered, duplicate state and not query iio_buffer_enabled()?

>  };
>  
> -#define SUNXI_GPADC_ADC_CHANNEL(_channel, _name) {		\
> +#define SUNXI_GPADC_ADC_CHANNEL(_channel, _name, _index) {	\
>  	.type = IIO_VOLTAGE,					\
>  	.indexed = 1,						\
>  	.channel = _channel,					\
>  	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
>  	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
>  	.datasheet_name = _name,				\
> +	.scan_index = _index,					\
> +	.scan_type = {						\
> +		.sign = 'u',					\
> +		.realbits = 12,					\
> +		.storagebits = 16,				\
> +		.shift = 0,					\

shift not strictly needed

> +		.endianness = IIO_LE,				\
> +	},							\
>  }
>  
>  static struct iio_map sunxi_gpadc_hwmon_maps[] = {
>  	{
> +		.adc_channel_label = "adc_chan0",
> +		.consumer_dev_name = "sunxi-gpadc-ts.0",
> +	}, {
> +		.adc_channel_label = "adc_chan1",
> +		.consumer_dev_name = "sunxi-gpadc-ts.0",
> +	}, {
> +		.adc_channel_label = "adc_chan2",
> +		.consumer_dev_name = "sunxi-gpadc-ts.0",
> +	}, {
> +		.adc_channel_label = "adc_chan3",
> +		.consumer_dev_name = "sunxi-gpadc-ts.0",
> +	}, {
>  		.adc_channel_label = "temp_adc",
>  		.consumer_dev_name = "iio_hwmon.0",
>  	},
> @@ -121,28 +148,33 @@ static struct iio_map sunxi_gpadc_hwmon_maps[] = {
>  };
>  
>  static const struct iio_chan_spec sunxi_gpadc_channels[] = {
> -	SUNXI_GPADC_ADC_CHANNEL(0, "adc_chan0"),
> -	SUNXI_GPADC_ADC_CHANNEL(1, "adc_chan1"),
> -	SUNXI_GPADC_ADC_CHANNEL(2, "adc_chan2"),
> -	SUNXI_GPADC_ADC_CHANNEL(3, "adc_chan3"),
> +	SUNXI_GPADC_ADC_CHANNEL(0, "adc_chan0", 1),
> +	SUNXI_GPADC_ADC_CHANNEL(1, "adc_chan1", 2),
> +	SUNXI_GPADC_ADC_CHANNEL(2, "adc_chan2", 3),
> +	SUNXI_GPADC_ADC_CHANNEL(3, "adc_chan3", 4),
>  	{
>  		.type = IIO_TEMP,
>  		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
>  		.datasheet_name = "temp_adc",
>  		.extend_name = "SoC temperature",
>  	},
> -	{ /* sentinel */ },
>  };
>  
>  static int sunxi_gpadc_adc_read(struct iio_dev *indio_dev, int channel,
>  				int *val)
>  {
>  	struct sunxi_gpadc_dev *info = iio_priv(indio_dev);
> +	bool buffered = info->buffered;
>  	int ret = 0;
> +	unsigned int reg;
>  
>  	mutex_lock(&indio_dev->mlock);
>  
>  	reinit_completion(&info->completion);
> +
> +	reg = SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(1) | SUNXI_GPADC_TP_FIFO_FLUSH;
> +	regmap_update_bits(info->regmap, SUNXI_GPADC_TP_INT_FIFOC, reg, reg);
> +
>  	if (info->flags & SUNXI_GPADC_ARCH_SUN6I)
>  		regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL1,
>  			     SUNXI_GPADC_SUN6I_TP_MODE_EN |
> @@ -153,9 +185,9 @@ static int sunxi_gpadc_adc_read(struct iio_dev *indio_dev, int channel,
>  			     SUNXI_GPADC_TP_MODE_EN |
>  			     SUNXI_GPADC_TP_ADC_SELECT |
>  			     SUNXI_GPADC_ADC_CHAN_SELECT(channel));
> -	regmap_write(info->regmap, SUNXI_GPADC_TP_INT_FIFOC,
> -		     SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(1) |
> -		     SUNXI_GPADC_TP_FIFO_FLUSH);
> +
> +	info->buffered = false;
> +
>  	enable_irq(info->fifo_data_irq);
>  
>  	if (!wait_for_completion_timeout(&info->completion,
> @@ -169,6 +201,7 @@ static int sunxi_gpadc_adc_read(struct iio_dev *indio_dev, int channel,
>  out:
>  	disable_irq(info->fifo_data_irq);
>  	mutex_unlock(&indio_dev->mlock);
> +	info->buffered = buffered;
>  
>  	return ret;
>  }
> @@ -177,20 +210,22 @@ static int sunxi_gpadc_temp_read(struct iio_dev *indio_dev, int *val)
>  {
>  	struct sunxi_gpadc_dev *info = iio_priv(indio_dev);
>  	int ret = 0;
> +	unsigned int reg;
>  
>  	mutex_lock(&indio_dev->mlock);
>  
>  	reinit_completion(&info->completion);
>  
> -	regmap_write(info->regmap, SUNXI_GPADC_TP_INT_FIFOC,
> -		     SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(1) |
> -		     SUNXI_GPADC_TP_FIFO_FLUSH);
> +	reg = SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(1) | SUNXI_GPADC_TP_FIFO_FLUSH;
> +	regmap_update_bits(info->regmap, SUNXI_GPADC_TP_INT_FIFOC, reg, reg);
> +
>  	if (info->flags & SUNXI_GPADC_ARCH_SUN6I)
>  		regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL1,
>  			     SUNXI_GPADC_SUN6I_TP_MODE_EN);
>  	else
>  		regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL1,
>  			     SUNXI_GPADC_TP_MODE_EN);
> +
>  	enable_irq(info->temp_data_irq);
>  
>  	if (!wait_for_completion_timeout(&info->completion,
> @@ -211,7 +246,6 @@ out:
>  	mutex_unlock(&indio_dev->mlock);
>  
>  	return ret;
> -
>  }
>  
>  static int sunxi_gpadc_read_raw(struct iio_dev *indio_dev,
> @@ -219,15 +253,22 @@ static int sunxi_gpadc_read_raw(struct iio_dev *indio_dev,
>  				int *val, int *val2, long mask)
>  {
>  	int ret;
> +	struct sunxi_gpadc_dev *info = iio_priv(indio_dev);
>  
>  	switch (mask) {
>  	case IIO_CHAN_INFO_PROCESSED:
> +		if (info->buffered && !info->ts_attached)
> +			return -EBUSY;

there would be iio_device_claim_direct_mode()

> +
>  		ret = sunxi_gpadc_temp_read(indio_dev, val);
>  		if (ret)
>  			return ret;
>  
>  		return IIO_VAL_INT;
>  	case IIO_CHAN_INFO_RAW:
> +		if (info->buffered)
> +			return -EBUSY;
> +
>  		ret = sunxi_gpadc_adc_read(indio_dev, chan->channel, val);
>  		if (ret)
>  			return ret;
> @@ -261,7 +302,29 @@ static irqreturn_t sunxi_gpadc_temp_data_irq_handler(int irq, void *dev_id)
>  static irqreturn_t sunxi_gpadc_fifo_data_irq_handler(int irq, void *dev_id)
>  {
>  	struct sunxi_gpadc_dev *info = dev_id;
> -	int ret;
> +	int ret, reg, i, fifo_count;
> +
> +	if (info->buffered) {
> +		if (regmap_read(info->regmap, SUNXI_GPADC_TP_INT_FIFOS, &reg))
> +			return IRQ_HANDLED;
> +
> +		fifo_count = (reg & SUNXI_GPADC_RXA_CNT) >> 8;
> +		/* Sometimes, the interrupt occurs when the FIFO is empty. */
> +		if (!fifo_count)
> +			return IRQ_HANDLED;
> +
> +		for (i = 0; i < fifo_count; i++) {
> +			if (regmap_read(info->regmap, SUNXI_GPADC_TP_DATA,
> +					&info->buffer.buffer[i]))
> +				return IRQ_HANDLED;
> +		}
> +
> +		info->buffer.buff_size = i;
> +
> +		iio_push_to_buffers(info->indio_dev, &info->buffer);
> +
> +		return IRQ_HANDLED;
> +	}
>  
>  	ret = regmap_read(info->regmap, SUNXI_GPADC_TP_DATA, &info->adc_data);
>  	if (ret == 0)
> @@ -270,6 +333,58 @@ static irqreturn_t sunxi_gpadc_fifo_data_irq_handler(int irq, void *dev_id)
>  	return IRQ_HANDLED;
>  }
>  
> +static int sunxi_gpadc_buffer_postenable(struct iio_dev *indio_dev)
> +{
> +	struct sunxi_gpadc_dev *info = iio_priv(indio_dev);
> +	unsigned int reg;
> +	int ret;
> +
> +	reg = SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(1) | SUNXI_GPADC_TP_FIFO_FLUSH;
> +	regmap_update_bits(info->regmap, SUNXI_GPADC_TP_INT_FIFOC, reg, reg);
> +
> +	if (info->ts_attached) {
> +		reg = SUNXI_GPADC_STYLUS_UP_DEBOUNCE(5) |
> +		      SUNXI_GPADC_STYLUS_UP_DEBOUNCE_EN;
> +
> +		if (info->flags & SUNXI_GPADC_ARCH_SUN6I)
> +			reg |= SUNXI_GPADC_SUN6I_TP_MODE_EN;
> +		else
> +			reg |= SUNXI_GPADC_TP_MODE_EN;
> +	} else {
> +		if (info->flags & SUNXI_GPADC_ARCH_SUN6I)
> +			reg = SUNXI_GPADC_SUN6I_TP_MODE_EN |
> +			      SUNXI_GPADC_SUN6I_TP_ADC_SELECT;
> +		else
> +			reg = SUNXI_GPADC_TP_MODE_EN |
> +			      SUNXI_GPADC_TP_ADC_SELECT;
> +	}
> +
> +	if (regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL1, reg))
> +		return ret;
> +
> +	info->buffered = true;
> +
> +	enable_irq(info->fifo_data_irq);
> +
> +	return 0;
> +}
> +
> +static int sunxi_gpadc_buffer_predisable(struct iio_dev *indio_dev)
> +{
> +	struct sunxi_gpadc_dev *info = iio_priv(indio_dev);
> +
> +	disable_irq(info->fifo_data_irq);
> +
> +	info->buffered = false;
> +
> +	return 0;
> +}
> +
> +static const struct iio_buffer_setup_ops sunxi_gpadc_buffer_setup_ops = {
> +	.postenable = sunxi_gpadc_buffer_postenable,
> +	.predisable = sunxi_gpadc_buffer_predisable,
> +};
> +
>  static int sunxi_gpadc_probe(struct platform_device *pdev)
>  {
>  	struct sunxi_gpadc_dev *info;
> @@ -286,16 +401,20 @@ static int sunxi_gpadc_probe(struct platform_device *pdev)
>  	info = iio_priv(indio_dev);
>  
>  	info->regmap = sunxi_gpadc_mfd_dev->regmap;
> +	info->indio_dev = indio_dev;
>  	init_completion(&info->completion);
>  	indio_dev->name = dev_name(&pdev->dev);
>  	indio_dev->dev.parent = &pdev->dev;
>  	indio_dev->dev.of_node = pdev->dev.of_node;
>  	indio_dev->info = &sunxi_gpadc_iio_info;
> -	indio_dev->modes = INDIO_DIRECT_MODE;
> +	indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
>  	indio_dev->num_channels = ARRAY_SIZE(sunxi_gpadc_channels);
>  	indio_dev->channels = sunxi_gpadc_channels;
> +	indio_dev->setup_ops = &sunxi_gpadc_buffer_setup_ops;
>  
>  	info->flags = platform_get_device_id(pdev)->driver_data;
> +	info->ts_attached = of_property_read_bool(pdev->dev.parent->of_node,
> +						  "allwinner,ts-attached");
>  
>  	regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL0, SUNXI_GPADC_FS_DIV(7) |
>  		     SUNXI_GPADC_ADC_CLK_DIVIDER(2) | SUNXI_GPADC_T_ACQ(63));
> @@ -305,6 +424,8 @@ static int sunxi_gpadc_probe(struct platform_device *pdev)
>  	else
>  		regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL1,
>  			     SUNXI_GPADC_TP_MODE_EN);
> +	regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL2,
> +		     SUNXI_GPADC_TP_SENSITIVE_ADJUST(15));
>  	regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL3, SUNXI_GPADC_FILTER_EN |
>  		     SUNXI_GPADC_FILTER_TYPE(1));
>  	regmap_write(info->regmap, SUNXI_GPADC_TP_TPR,
> 

-- 

Peter Meerwald-Stadler
+43-664-2444418 (mobile)

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 3/5] iio: adc: sunxi-gpadc-iio: enable iio_buffers
  2016-07-20  8:38   ` Peter Meerwald-Stadler
@ 2016-07-20  8:57     ` Quentin Schulz
  0 siblings, 0 replies; 31+ messages in thread
From: Quentin Schulz @ 2016-07-20  8:57 UTC (permalink / raw)
  To: linux-arm-kernel

On 20/07/2016 10:38, Peter Meerwald-Stadler wrote:
> 
>> This enables the use of buffers on ADC channels of sunxi-gpadc-iio driver.
>> It also prepares the code which will be used by the touchscreen driver
>> named sunxi-gpadc-ts.
>>
>> The GPADC on Allwinner SoCs (A10, A13 and A31) has a 12 bits register for
>> conversion's data. The GPADC uses the same ADC channels for the ADC and the
>> touchscreen therefore exposes these channels to the sunxi-gpadc-ts iio
>> consumer which will be in charge of reading data from these channels for
>> the input framework.
>>
>> The temperature can only be read when in touchscreen mode. This means if
>> the buffers are being used for the ADC, the temperature sensor cannot be
>> read.
>>
>> When a FIFO_DATA_PENDING irq occurs, its handler will read the entire FIFO
>> and fill a buffer before sending it to the consumers which registered in
>> IIO for the ADC channels.
>>
>> When a consumer starts buffering ADC channels,
>> sunxi_gpadc_buffer_postenable is called and will enable FIFO_DATA_PENDING
>> irq and select the mode in which the GPADC should run (ADC or touchscreen)
>> depending on a property of the DT ("allwinner,ts-attached").
>> When the consumer stops buffering, it disables the same irq.
> 
> comments below
>  
[...]
>> @@ -101,19 +104,43 @@ struct sunxi_gpadc_dev {
>>  	unsigned int			fifo_data_irq;
>>  	unsigned int			temp_data_irq;
>>  	unsigned int			flags;
>> +	struct iio_dev			*indio_dev;
>> +	struct sunxi_gpadc_buffer	buffer;
>> +	bool				ts_attached;
>> +	bool				buffered;
> 
> why add buffered, duplicate state and not query iio_buffer_enabled()?
> 
>>  };
>>  
>> -#define SUNXI_GPADC_ADC_CHANNEL(_channel, _name) {		\
>> +#define SUNXI_GPADC_ADC_CHANNEL(_channel, _name, _index) {	\
>>  	.type = IIO_VOLTAGE,					\
>>  	.indexed = 1,						\
>>  	.channel = _channel,					\
>>  	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
>>  	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
>>  	.datasheet_name = _name,				\
>> +	.scan_index = _index,					\
>> +	.scan_type = {						\
>> +		.sign = 'u',					\
>> +		.realbits = 12,					\
>> +		.storagebits = 16,				\
>> +		.shift = 0,					\
> 
> shift not strictly needed
> 

ACK.

[...]
>>  static int sunxi_gpadc_read_raw(struct iio_dev *indio_dev,
>> @@ -219,15 +253,22 @@ static int sunxi_gpadc_read_raw(struct iio_dev *indio_dev,
>>  				int *val, int *val2, long mask)
>>  {
>>  	int ret;
>> +	struct sunxi_gpadc_dev *info = iio_priv(indio_dev);
>>  
>>  	switch (mask) {
>>  	case IIO_CHAN_INFO_PROCESSED:
>> +		if (info->buffered && !info->ts_attached)
>> +			return -EBUSY;
> 
> there would be iio_device_claim_direct_mode()
> 

OK, iio_device_claim_direct_mode() and iio_device_release_direct_mode()
are new functions which are not yet in the Linux Cross Reference
(http://lxr.free-electrons.com/), I didn't know they existed. I'll use
that, iio_buffer_enabled() when needed and get rid of the buffered
boolean variable.

[...]
Thanks.

Quentin

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 4/5] input: touchscreen: support Allwinner SoCs' touchscreen
  2016-07-20  8:29 ` [PATCH 4/5] input: touchscreen: support Allwinner SoCs' touchscreen Quentin Schulz
@ 2016-07-20 17:25   ` Dmitry Torokhov
  2016-07-20 20:13     ` Jonathan Cameron
  2016-09-24 18:26     ` Quentin Schulz
  2016-07-21  6:29   ` Maxime Ripard
  2016-07-24 11:24   ` Jonathan Cameron
  2 siblings, 2 replies; 31+ messages in thread
From: Dmitry Torokhov @ 2016-07-20 17:25 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Quentin,

On Wed, Jul 20, 2016 at 10:29:10AM +0200, Quentin Schulz wrote:
> This adds support for Allwinner SoCs' (A10, A13 and A31) resistive
> touchscreen. This driver is probed by the MFD sunxi-gpadc-mfd.
> 
> This driver uses ADC channels exposed through the IIO framework by
> sunxi-gpadc-iio to get its data. When opening this input device, it will
> start buffering in the ADC driver and enable a TP_UP_PENDING irq. The ADC
> driver will fill in a buffer with all data and call the callback the input
> device associated with this buffer. The input device will then read the
> buffer two by two and send X and Y coordinates to the input framework based
> on what it received from the ADC's buffer. When closing this input device,
> the buffering is stopped.
> 
> Note that locations in the first received buffer after an TP_UP_PENDING irq
> occurred are unreliable, thus dropped.
> 
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> ---
>  drivers/input/touchscreen/Kconfig          |  11 +-
>  drivers/input/touchscreen/Makefile         |   2 +-
>  drivers/input/touchscreen/sun4i-ts.c       | 419 -----------------------------
>  drivers/input/touchscreen/sunxi-gpadc-ts.c | 195 ++++++++++++++
>  4 files changed, 201 insertions(+), 426 deletions(-)
>  delete mode 100644 drivers/input/touchscreen/sun4i-ts.c
>  create mode 100644 drivers/input/touchscreen/sunxi-gpadc-ts.c
> 
> diff --git a/drivers/input/touchscreen/Kconfig b/drivers/input/touchscreen/Kconfig
> index 8ecdc38..f16ce36 100644
> --- a/drivers/input/touchscreen/Kconfig
> +++ b/drivers/input/touchscreen/Kconfig
> @@ -1072,14 +1072,13 @@ config TOUCHSCREEN_STMPE
>  config TOUCHSCREEN_SUN4I
>  	tristate "Allwinner sun4i resistive touchscreen controller support"
>  	depends on ARCH_SUNXI || COMPILE_TEST
> -	depends on HWMON
> -	depends on THERMAL || !THERMAL_OF
> +	depends on SUNXI_ADC
>  	help
> -	  This selects support for the resistive touchscreen controller
> -	  found on Allwinner sunxi SoCs.
> +	  This selects support for the resistive touchscreen controller found
> +	  on Allwinner SoCs (A10, A13, A31).
>  
> -	  To compile this driver as a module, choose M here: the
> -	  module will be called sun4i-ts.
> +	  To compile this driver as a module, choose M here: the module will be
> +	  called sunxi-gpadc-ts.
>  
>  config TOUCHSCREEN_SUR40
>  	tristate "Samsung SUR40 (Surface 2.0/PixelSense) touchscreen"
> diff --git a/drivers/input/touchscreen/Makefile b/drivers/input/touchscreen/Makefile
> index f42975e..bdc1889 100644
> --- a/drivers/input/touchscreen/Makefile
> +++ b/drivers/input/touchscreen/Makefile
> @@ -65,7 +65,7 @@ obj-$(CONFIG_TOUCHSCREEN_PIXCIR)	+= pixcir_i2c_ts.o
>  obj-$(CONFIG_TOUCHSCREEN_S3C2410)	+= s3c2410_ts.o
>  obj-$(CONFIG_TOUCHSCREEN_ST1232)	+= st1232.o
>  obj-$(CONFIG_TOUCHSCREEN_STMPE)		+= stmpe-ts.o
> -obj-$(CONFIG_TOUCHSCREEN_SUN4I)		+= sun4i-ts.o
> +obj-$(CONFIG_TOUCHSCREEN_SUN4I)		+= sunxi-gpadc-ts.o
>  obj-$(CONFIG_TOUCHSCREEN_SUR40)		+= sur40.o
>  obj-$(CONFIG_TOUCHSCREEN_TI_AM335X_TSC)	+= ti_am335x_tsc.o
>  obj-$(CONFIG_TOUCHSCREEN_TOUCHIT213)	+= touchit213.o
> diff --git a/drivers/input/touchscreen/sun4i-ts.c b/drivers/input/touchscreen/sun4i-ts.c
> deleted file mode 100644
> index d07dd29..0000000
...
> diff --git a/drivers/input/touchscreen/sunxi-gpadc-ts.c b/drivers/input/touchscreen/sunxi-gpadc-ts.c
> new file mode 100644
> index 0000000..410d14f
> --- /dev/null
> +++ b/drivers/input/touchscreen/sunxi-gpadc-ts.c
> @@ -0,0 +1,195 @@
> +/* Touchscreen driver for Allwinner SoCs (A10, A13, A31) GPADC
> + *
> + * Copyright (c) 2016 Quentin Schulz <quentin.schulz@free-electrons>
> + *
> + * This program is free software; you can redistribute it and/or modify it under
> + * the terms of the GNU General Public License version 2 as published by the
> + * Free Software Foundation.
> + */
> +
> +#include <linux/init.h>
> +#include <linux/input.h>
> +#include <linux/interrupt.h>
> +#include <linux/module.h>
> +#include <linux/of.h>
> +#include <linux/platform_device.h>
> +#include <linux/regmap.h>
> +
> +#include <linux/iio/consumer.h>
> +#include <linux/mfd/sunxi-gpadc-mfd.h>
> +
> +struct sunxi_gpadc_ts {
> +	struct iio_cb_buffer	*buffer;
> +	struct input_dev	*input;
> +	struct platform_device	*pdev;
> +	unsigned int		tp_up_irq;
> +	bool			ignore_fifo_data;
> +};
> +
> +static int sunxi_gpadc_ts_open(struct input_dev *dev)
> +{
> +	struct sunxi_gpadc_ts *info = input_get_drvdata(dev);
> +	int ret;
> +
> +	ret = iio_channel_start_all_cb(info->buffer);
> +	if (ret) {
> +		dev_err(dev->dev.parent,
> +			"failed to start iio channels with callback\n");
> +		return ret;
> +	}
> +
> +	enable_irq(info->tp_up_irq);
> +
> +	return 0;
> +}
> +
> +static void sunxi_gpadc_ts_close(struct input_dev *dev)
> +{
> +	struct sunxi_gpadc_ts *info = input_get_drvdata(dev);
> +
> +	iio_channel_stop_all_cb(info->buffer);
> +
> +	disable_irq(info->tp_up_irq);
> +}
> +
> +/*
> + * This function will be called by iio_push_to_buffers from another driver
> + * (namely sunxi-gpadc-iio). It will be passed the buffer filled with input
> + * values (X value then Y value) and the sunxi_gpadc_ts structure representing
> + * the device.
> + */
> +static int sunxi_gpadc_ts_callback(const void *data, void *private)
> +{
> +	const struct sunxi_gpadc_buffer *buffer = data;
> +	struct sunxi_gpadc_ts *info = private;
> +	int i = 0;
> +
> +	/* Locations in the first buffer after an up event are unreliable */
> +	if (info->ignore_fifo_data) {
> +		info->ignore_fifo_data = false;
> +		return 0;
> +	}
> +
> +	while (i + 1 < buffer->buff_size) {
> +		input_event(info->input, EV_ABS, ABS_X, buffer->buffer[i++]);
> +		input_event(info->input, EV_ABS, ABS_Y, buffer->buffer[i++]);
> +		input_event(info->input, EV_KEY, BTN_TOUCH, 1);
> +		input_sync(info->input);
> +	}
> +
> +	return 0;
> +}
> +
> +static irqreturn_t sunxi_gpadc_tp_up_irq_handler(int irq, void *dev_id)
> +{
> +	struct sunxi_gpadc_ts *info = dev_id;
> +
> +	info->ignore_fifo_data = true;
> +
> +	input_event(info->input, EV_KEY, BTN_TOUCH, 0);
> +	input_sync(info->input);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static int sunxi_gpadc_ts_probe(struct platform_device *pdev)
> +{
> +	struct input_dev *input;
> +	struct sunxi_gpadc_ts *info;
> +	int ret, irq;
> +	struct sunxi_gpadc_mfd_dev *sunxi_gpadc_mfd_dev;
> +
> +	input = devm_input_allocate_device(&pdev->dev);
> +	if (!input)
> +		return -ENOMEM;
> +
> +	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
> +	if (!info)
> +		return -ENOMEM;
> +
> +	info->buffer = iio_channel_get_all_cb(&pdev->dev,
> +					      &sunxi_gpadc_ts_callback,
> +					      (void *)info);

Any chance we could introduce devm-variant here? If you do not want to
wait for IIO to add it you can temporarily add call
devm_add_action_or_reset() after getting channels and remove it when IIO
API catches up.

> +	if (IS_ERR(info->buffer)) {
> +		if (PTR_ERR(info->buffer) == -ENODEV)
> +			return -EPROBE_DEFER;
> +		return PTR_ERR(info->buffer);
> +	}
> +
> +	info->pdev = pdev;
> +	info->input = input;
> +	info->ignore_fifo_data = false;
> +	platform_set_drvdata(pdev, info);
> +
> +	input->dev.parent = &pdev->dev;

Not needed for input devices allocated with devm.

> +	input->name = "sunxi-gpadc-ts";
> +	input->id.bustype = BUS_HOST;
> +	input->open = sunxi_gpadc_ts_open;
> +	input->close = sunxi_gpadc_ts_close;
> +	__set_bit(EV_SYN, input->evbit);

This not needed.

> +	input_set_capability(input, EV_KEY, BTN_TOUCH);
> +	input_set_abs_params(input, ABS_X, 0, 4095, 0, 0);
> +	input_set_abs_params(input, ABS_Y, 0, 4095, 0, 0);
> +	input_set_drvdata(input, info);
> +
> +	irq = platform_get_irq_byname(pdev, "TP_UP_PENDING");
> +	if (irq < 0) {
> +		dev_err(&pdev->dev, "no TP_UP_PENDING interrupt registered\n");
> +		ret = irq;
> +		goto err;
> +	}
> +
> +	sunxi_gpadc_mfd_dev = dev_get_drvdata(pdev->dev.parent);
> +
> +	irq = regmap_irq_get_virq(sunxi_gpadc_mfd_dev->regmap_irqc, irq);
> +	ret = devm_request_any_context_irq(&pdev->dev, irq,
> +					   sunxi_gpadc_tp_up_irq_handler, 0,
> +					   "tp_up", info);
> +	if (ret < 0) {
> +		dev_err(&pdev->dev,
> +			"could not request TP_UP_PENDING interrupt: %d\n", ret);
> +		goto err;
> +	}
> +
> +	info->tp_up_irq = irq;
> +	disable_irq(irq);
> +
> +	ret = input_register_device(input);
> +	if (ret) {
> +		dev_err(&pdev->dev, "failed to register input device\n");
> +		goto err;
> +	}
> +
> +	return 0;
> +
> +err:
> +	iio_channel_release_all_cb(info->buffer);
> +
> +	return ret;
> +}
> +
> +static int sunxi_gpadc_ts_remove(struct platform_device *pdev)
> +{
> +	struct sunxi_gpadc_ts *info = platform_get_drvdata(pdev);
> +
> +	iio_channel_stop_all_cb(info->buffer);
> +	iio_channel_release_all_cb(info->buffer);
> +
> +	disable_irq(info->tp_up_irq);

You are mixing devm and non-devm so your unwind order is completely out
of wack. If input device is opened while you are unloading (or
unbinding) the dirver, then you'll release channels, then input device's
close() will be called, which will try to stop the IIO channels again
and disable IRQ yet again.

> +
> +	return 0;
> +}
> +
> +static struct platform_driver sunxi_gpadc_ts_driver = {
> +	.driver = {
> +		.name = "sunxi-gpadc-ts",
> +	},
> +	.probe = sunxi_gpadc_ts_probe,
> +	.remove = sunxi_gpadc_ts_remove,
> +};
> +
> +module_platform_driver(sunxi_gpadc_ts_driver);
> +
> +MODULE_DESCRIPTION("Touchscreen driver for Allwinner SoCs (A10, A13, A31) GPADC");
> +MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
> +MODULE_LICENSE("GPL v2");
> -- 
> 2.5.0
> 

Thanks.

-- 
Dmitry

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 4/5] input: touchscreen: support Allwinner SoCs' touchscreen
  2016-07-20 17:25   ` Dmitry Torokhov
@ 2016-07-20 20:13     ` Jonathan Cameron
  2016-09-24 18:26     ` Quentin Schulz
  1 sibling, 0 replies; 31+ messages in thread
From: Jonathan Cameron @ 2016-07-20 20:13 UTC (permalink / raw)
  To: linux-arm-kernel

On 20/07/16 18:25, Dmitry Torokhov wrote:
> Hi Quentin,
> 
> On Wed, Jul 20, 2016 at 10:29:10AM +0200, Quentin Schulz wrote:
>> This adds support for Allwinner SoCs' (A10, A13 and A31) resistive
>> touchscreen. This driver is probed by the MFD sunxi-gpadc-mfd.
>>
>> This driver uses ADC channels exposed through the IIO framework by
>> sunxi-gpadc-iio to get its data. When opening this input device, it will
>> start buffering in the ADC driver and enable a TP_UP_PENDING irq. The ADC
>> driver will fill in a buffer with all data and call the callback the input
>> device associated with this buffer. The input device will then read the
>> buffer two by two and send X and Y coordinates to the input framework based
>> on what it received from the ADC's buffer. When closing this input device,
>> the buffering is stopped.
>>
>> Note that locations in the first received buffer after an TP_UP_PENDING irq
>> occurred are unreliable, thus dropped.
>>
>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>> ---
>>  drivers/input/touchscreen/Kconfig          |  11 +-
>>  drivers/input/touchscreen/Makefile         |   2 +-
>>  drivers/input/touchscreen/sun4i-ts.c       | 419 -----------------------------
>>  drivers/input/touchscreen/sunxi-gpadc-ts.c | 195 ++++++++++++++
>>  4 files changed, 201 insertions(+), 426 deletions(-)
>>  delete mode 100644 drivers/input/touchscreen/sun4i-ts.c
>>  create mode 100644 drivers/input/touchscreen/sunxi-gpadc-ts.c
>>
>> diff --git a/drivers/input/touchscreen/Kconfig b/drivers/input/touchscreen/Kconfig
>> index 8ecdc38..f16ce36 100644
>> --- a/drivers/input/touchscreen/Kconfig
>> +++ b/drivers/input/touchscreen/Kconfig
>> @@ -1072,14 +1072,13 @@ config TOUCHSCREEN_STMPE
>>  config TOUCHSCREEN_SUN4I
>>  	tristate "Allwinner sun4i resistive touchscreen controller support"
>>  	depends on ARCH_SUNXI || COMPILE_TEST
>> -	depends on HWMON
>> -	depends on THERMAL || !THERMAL_OF
>> +	depends on SUNXI_ADC
>>  	help
>> -	  This selects support for the resistive touchscreen controller
>> -	  found on Allwinner sunxi SoCs.
>> +	  This selects support for the resistive touchscreen controller found
>> +	  on Allwinner SoCs (A10, A13, A31).
>>  
>> -	  To compile this driver as a module, choose M here: the
>> -	  module will be called sun4i-ts.
>> +	  To compile this driver as a module, choose M here: the module will be
>> +	  called sunxi-gpadc-ts.
>>  
>>  config TOUCHSCREEN_SUR40
>>  	tristate "Samsung SUR40 (Surface 2.0/PixelSense) touchscreen"
>> diff --git a/drivers/input/touchscreen/Makefile b/drivers/input/touchscreen/Makefile
>> index f42975e..bdc1889 100644
>> --- a/drivers/input/touchscreen/Makefile
>> +++ b/drivers/input/touchscreen/Makefile
>> @@ -65,7 +65,7 @@ obj-$(CONFIG_TOUCHSCREEN_PIXCIR)	+= pixcir_i2c_ts.o
>>  obj-$(CONFIG_TOUCHSCREEN_S3C2410)	+= s3c2410_ts.o
>>  obj-$(CONFIG_TOUCHSCREEN_ST1232)	+= st1232.o
>>  obj-$(CONFIG_TOUCHSCREEN_STMPE)		+= stmpe-ts.o
>> -obj-$(CONFIG_TOUCHSCREEN_SUN4I)		+= sun4i-ts.o
>> +obj-$(CONFIG_TOUCHSCREEN_SUN4I)		+= sunxi-gpadc-ts.o
>>  obj-$(CONFIG_TOUCHSCREEN_SUR40)		+= sur40.o
>>  obj-$(CONFIG_TOUCHSCREEN_TI_AM335X_TSC)	+= ti_am335x_tsc.o
>>  obj-$(CONFIG_TOUCHSCREEN_TOUCHIT213)	+= touchit213.o
>> diff --git a/drivers/input/touchscreen/sun4i-ts.c b/drivers/input/touchscreen/sun4i-ts.c
>> deleted file mode 100644
>> index d07dd29..0000000
> ...
>> diff --git a/drivers/input/touchscreen/sunxi-gpadc-ts.c b/drivers/input/touchscreen/sunxi-gpadc-ts.c
>> new file mode 100644
>> index 0000000..410d14f
>> --- /dev/null
>> +++ b/drivers/input/touchscreen/sunxi-gpadc-ts.c
>> @@ -0,0 +1,195 @@
>> +/* Touchscreen driver for Allwinner SoCs (A10, A13, A31) GPADC
>> + *
>> + * Copyright (c) 2016 Quentin Schulz <quentin.schulz@free-electrons>
>> + *
>> + * This program is free software; you can redistribute it and/or modify it under
>> + * the terms of the GNU General Public License version 2 as published by the
>> + * Free Software Foundation.
>> + */
>> +
>> +#include <linux/init.h>
>> +#include <linux/input.h>
>> +#include <linux/interrupt.h>
>> +#include <linux/module.h>
>> +#include <linux/of.h>
>> +#include <linux/platform_device.h>
>> +#include <linux/regmap.h>
>> +
>> +#include <linux/iio/consumer.h>
>> +#include <linux/mfd/sunxi-gpadc-mfd.h>
>> +
>> +struct sunxi_gpadc_ts {
>> +	struct iio_cb_buffer	*buffer;
>> +	struct input_dev	*input;
>> +	struct platform_device	*pdev;
>> +	unsigned int		tp_up_irq;
>> +	bool			ignore_fifo_data;
>> +};
>> +
>> +static int sunxi_gpadc_ts_open(struct input_dev *dev)
>> +{
>> +	struct sunxi_gpadc_ts *info = input_get_drvdata(dev);
>> +	int ret;
>> +
>> +	ret = iio_channel_start_all_cb(info->buffer);
>> +	if (ret) {
>> +		dev_err(dev->dev.parent,
>> +			"failed to start iio channels with callback\n");
>> +		return ret;
>> +	}
>> +
>> +	enable_irq(info->tp_up_irq);
>> +
>> +	return 0;
>> +}
>> +
>> +static void sunxi_gpadc_ts_close(struct input_dev *dev)
>> +{
>> +	struct sunxi_gpadc_ts *info = input_get_drvdata(dev);
>> +
>> +	iio_channel_stop_all_cb(info->buffer);
>> +
>> +	disable_irq(info->tp_up_irq);
>> +}
>> +
>> +/*
>> + * This function will be called by iio_push_to_buffers from another driver
>> + * (namely sunxi-gpadc-iio). It will be passed the buffer filled with input
>> + * values (X value then Y value) and the sunxi_gpadc_ts structure representing
>> + * the device.
>> + */
>> +static int sunxi_gpadc_ts_callback(const void *data, void *private)
>> +{
>> +	const struct sunxi_gpadc_buffer *buffer = data;
>> +	struct sunxi_gpadc_ts *info = private;
>> +	int i = 0;
>> +
>> +	/* Locations in the first buffer after an up event are unreliable */
>> +	if (info->ignore_fifo_data) {
>> +		info->ignore_fifo_data = false;
>> +		return 0;
>> +	}
>> +
>> +	while (i + 1 < buffer->buff_size) {
>> +		input_event(info->input, EV_ABS, ABS_X, buffer->buffer[i++]);
>> +		input_event(info->input, EV_ABS, ABS_Y, buffer->buffer[i++]);
>> +		input_event(info->input, EV_KEY, BTN_TOUCH, 1);
>> +		input_sync(info->input);
>> +	}
>> +
>> +	return 0;
>> +}
>> +
>> +static irqreturn_t sunxi_gpadc_tp_up_irq_handler(int irq, void *dev_id)
>> +{
>> +	struct sunxi_gpadc_ts *info = dev_id;
>> +
>> +	info->ignore_fifo_data = true;
>> +
>> +	input_event(info->input, EV_KEY, BTN_TOUCH, 0);
>> +	input_sync(info->input);
>> +
>> +	return IRQ_HANDLED;
>> +}
>> +
>> +static int sunxi_gpadc_ts_probe(struct platform_device *pdev)
>> +{
>> +	struct input_dev *input;
>> +	struct sunxi_gpadc_ts *info;
>> +	int ret, irq;
>> +	struct sunxi_gpadc_mfd_dev *sunxi_gpadc_mfd_dev;
>> +
>> +	input = devm_input_allocate_device(&pdev->dev);
>> +	if (!input)
>> +		return -ENOMEM;
>> +
>> +	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
>> +	if (!info)
>> +		return -ENOMEM;
>> +
>> +	info->buffer = iio_channel_get_all_cb(&pdev->dev,
>> +					      &sunxi_gpadc_ts_callback,
>> +					      (void *)info);
> 
> Any chance we could introduce devm-variant here? If you do not want to
> wait for IIO to add it you can temporarily add call
> devm_add_action_or_reset() after getting channels and remove it when IIO
> API catches up.
Absolutely.  I think this may be the first mainline driver to actually
use this interface at all. (which is great btw!)
So lack of devm interface is due to lack or prior need.
I'm happy enough to have such a patch in this series as not likely to
cause any merge issues.

We are still somewhat lacking in IIO devm interfaces unfortunately.
> 
>> +	if (IS_ERR(info->buffer)) {
>> +		if (PTR_ERR(info->buffer) == -ENODEV)
>> +			return -EPROBE_DEFER;
>> +		return PTR_ERR(info->buffer);
>> +	}
>> +
>> +	info->pdev = pdev;
>> +	info->input = input;
>> +	info->ignore_fifo_data = false;
>> +	platform_set_drvdata(pdev, info);
>> +
>> +	input->dev.parent = &pdev->dev;
> 
> Not needed for input devices allocated with devm.
> 
>> +	input->name = "sunxi-gpadc-ts";
>> +	input->id.bustype = BUS_HOST;
>> +	input->open = sunxi_gpadc_ts_open;
>> +	input->close = sunxi_gpadc_ts_close;
>> +	__set_bit(EV_SYN, input->evbit);
> 
> This not needed.
> 
>> +	input_set_capability(input, EV_KEY, BTN_TOUCH);
>> +	input_set_abs_params(input, ABS_X, 0, 4095, 0, 0);
>> +	input_set_abs_params(input, ABS_Y, 0, 4095, 0, 0);
>> +	input_set_drvdata(input, info);
>> +
>> +	irq = platform_get_irq_byname(pdev, "TP_UP_PENDING");
>> +	if (irq < 0) {
>> +		dev_err(&pdev->dev, "no TP_UP_PENDING interrupt registered\n");
>> +		ret = irq;
>> +		goto err;
>> +	}
>> +
>> +	sunxi_gpadc_mfd_dev = dev_get_drvdata(pdev->dev.parent);
>> +
>> +	irq = regmap_irq_get_virq(sunxi_gpadc_mfd_dev->regmap_irqc, irq);
>> +	ret = devm_request_any_context_irq(&pdev->dev, irq,
>> +					   sunxi_gpadc_tp_up_irq_handler, 0,
>> +					   "tp_up", info);
>> +	if (ret < 0) {
>> +		dev_err(&pdev->dev,
>> +			"could not request TP_UP_PENDING interrupt: %d\n", ret);
>> +		goto err;
>> +	}
>> +
>> +	info->tp_up_irq = irq;
>> +	disable_irq(irq);
>> +
>> +	ret = input_register_device(input);
>> +	if (ret) {
>> +		dev_err(&pdev->dev, "failed to register input device\n");
>> +		goto err;
>> +	}
>> +
>> +	return 0;
>> +
>> +err:
>> +	iio_channel_release_all_cb(info->buffer);
>> +
>> +	return ret;
>> +}
>> +
>> +static int sunxi_gpadc_ts_remove(struct platform_device *pdev)
>> +{
>> +	struct sunxi_gpadc_ts *info = platform_get_drvdata(pdev);
>> +
>> +	iio_channel_stop_all_cb(info->buffer);
>> +	iio_channel_release_all_cb(info->buffer);
>> +
>> +	disable_irq(info->tp_up_irq);
> 
> You are mixing devm and non-devm so your unwind order is completely out
> of wack. If input device is opened while you are unloading (or
> unbinding) the dirver, then you'll release channels, then input device's
> close() will be called, which will try to stop the IIO channels again
> and disable IRQ yet again.
> 
>> +
>> +	return 0;
>> +}
>> +
>> +static struct platform_driver sunxi_gpadc_ts_driver = {
>> +	.driver = {
>> +		.name = "sunxi-gpadc-ts",
>> +	},
>> +	.probe = sunxi_gpadc_ts_probe,
>> +	.remove = sunxi_gpadc_ts_remove,
>> +};
>> +
>> +module_platform_driver(sunxi_gpadc_ts_driver);
>> +
>> +MODULE_DESCRIPTION("Touchscreen driver for Allwinner SoCs (A10, A13, A31) GPADC");
>> +MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
>> +MODULE_LICENSE("GPL v2");
>> -- 
>> 2.5.0
>>
> 
> Thanks.
> 

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 5/5] mfd: sunxi-gpadc-mfd: probe sunxi-gpadc-ts driver
  2016-07-20  8:29 ` [PATCH 5/5] mfd: sunxi-gpadc-mfd: probe sunxi-gpadc-ts driver Quentin Schulz
@ 2016-07-21  6:08   ` Maxime Ripard
  2016-07-24 11:26     ` Jonathan Cameron
  0 siblings, 1 reply; 31+ messages in thread
From: Maxime Ripard @ 2016-07-21  6:08 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Quentin,

On Wed, Jul 20, 2016 at 10:29:11AM +0200, Quentin Schulz wrote:
> This probes the touchscreen driver for Allwinner SoCs (A10, A13 and A31)
> when the property "allwinner,ts-attached" is set in the GPADC (rtp) node of
> the DT.
> 
> Some comestic modifications done to shorten and increase readability of the
> code.
> 
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> ---
>  drivers/mfd/sunxi-gpadc-mfd.c | 115 ++++++++++++++++++++++++++++--------------
>  1 file changed, 77 insertions(+), 38 deletions(-)
> 
> diff --git a/drivers/mfd/sunxi-gpadc-mfd.c b/drivers/mfd/sunxi-gpadc-mfd.c
> index 05a000b..9b9ed0b 100644
> --- a/drivers/mfd/sunxi-gpadc-mfd.c
> +++ b/drivers/mfd/sunxi-gpadc-mfd.c
> @@ -21,6 +21,11 @@
>  #define SUNXI_IRQ_TEMP_DATA	1
>  #define SUNXI_IRQ_TP_UP		2
>  
> +#define SUNXI_GPADC_MFD_CELL(_name, _resources, _num_resources) {	\
> +	.name = _name,							\
> +	.resources = _resources,					\
> +	.num_resources = _num_resources					\
> +}
>  
>  static struct resource adc_resources[] = {
>  	{
> @@ -64,33 +69,45 @@ static const struct regmap_irq_chip sunxi_gpadc_mfd_regmap_irq_chip = {
>  };
>  
>  static struct mfd_cell sun4i_gpadc_mfd_cells[] = {
> -	{
> -		.name	= "sun4i-a10-gpadc-iio",
> -		.resources = adc_resources,
> -		.num_resources = ARRAY_SIZE(adc_resources),
> -	}, {
> -		.name = "iio_hwmon",
> -	}
> +	SUNXI_GPADC_MFD_CELL("sun4i-a10-gpadc-iio", adc_resources,
> +			     ARRAY_SIZE(adc_resources)),
> +	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
>  };
>  
>  static struct mfd_cell sun5i_gpadc_mfd_cells[] = {
> -	{
> -		.name	= "sun5i-a13-gpadc-iio",
> -		.resources = adc_resources,
> -		.num_resources = ARRAY_SIZE(adc_resources),
> -	}, {
> -		.name = "iio_hwmon",
> -	},
> +	SUNXI_GPADC_MFD_CELL("sun5i-a13-gpadc-iio", adc_resources,
> +			     ARRAY_SIZE(adc_resources)),
> +	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
>  };
>  
>  static struct mfd_cell sun6i_gpadc_mfd_cells[] = {
> -	{
> -		.name	= "sun6i-a31-gpadc-iio",
> -		.resources = adc_resources,
> -		.num_resources = ARRAY_SIZE(adc_resources),
> -	}, {
> -		.name = "iio_hwmon",
> -	},
> +	SUNXI_GPADC_MFD_CELL("sun6i-a31-gpadc-iio", adc_resources,
> +			     ARRAY_SIZE(adc_resources)),
> +	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
> +};

This should be part of a separate patch.

> +
> +static struct mfd_cell sun4i_gpadc_mfd_cells_ts[] = {
> +	SUNXI_GPADC_MFD_CELL("sun6i-a31-gpadc-iio", adc_resources,
> +			     ARRAY_SIZE(adc_resources)),
> +	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
> +	SUNXI_GPADC_MFD_CELL("sunxi-gpadc-ts", ts_resources,
> +			     ARRAY_SIZE(ts_resources)),
> +};
> +
> +static struct mfd_cell sun5i_gpadc_mfd_cells_ts[] = {
> +	SUNXI_GPADC_MFD_CELL("sun5i-a13-gpadc-iio", adc_resources,
> +			     ARRAY_SIZE(adc_resources)),
> +	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
> +	SUNXI_GPADC_MFD_CELL("sunxi-gpadc-ts", ts_resources,
> +			     ARRAY_SIZE(ts_resources)),
> +};
> +
> +static struct mfd_cell sun6i_gpadc_mfd_cells_ts[] = {
> +	SUNXI_GPADC_MFD_CELL("sun6i-a31-gpadc-iio", adc_resources,
> +			     ARRAY_SIZE(adc_resources)),
> +	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
> +	SUNXI_GPADC_MFD_CELL("sunxi-gpadc-ts", ts_resources,
> +			     ARRAY_SIZE(ts_resources)),
>  };
>  
>  static const struct regmap_config sunxi_gpadc_mfd_regmap_config = {
> @@ -142,23 +159,45 @@ static int sunxi_gpadc_mfd_probe(struct platform_device *pdev)
>  	}
>  
>  	if (of_device_is_compatible(pdev->dev.of_node,
> -				    "allwinner,sun4i-a10-ts"))
> -		ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> -				      sun4i_gpadc_mfd_cells,
> -				      ARRAY_SIZE(sun4i_gpadc_mfd_cells), NULL,
> -				      0, NULL);
> -	else if (of_device_is_compatible(pdev->dev.of_node,
> -					 "allwinner,sun5i-a13-ts"))
> -		ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> -				      sun5i_gpadc_mfd_cells,
> -				      ARRAY_SIZE(sun5i_gpadc_mfd_cells), NULL,
> -				      0, NULL);
> -	else if (of_device_is_compatible(pdev->dev.of_node,
> -					 "allwinner,sun6i-a31-ts"))
> -		ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> -				      sun6i_gpadc_mfd_cells,
> -				      ARRAY_SIZE(sun6i_gpadc_mfd_cells), NULL,
> -				      0, NULL);
> +				    "allwinner,sun4i-a10-ts")) {
> +		if (of_property_read_bool(pdev->dev.of_node,
> +					  "allwinner,ts-attached"))
> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> +					      sun4i_gpadc_mfd_cells_ts,
> +					      ARRAY_SIZE(sun4i_gpadc_mfd_cells_ts),
> +					      NULL, 0, NULL);
> +		else
> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> +					      sun4i_gpadc_mfd_cells,
> +					      ARRAY_SIZE(sun4i_gpadc_mfd_cells),
> +					      NULL, 0, NULL);
> +	} else if (of_device_is_compatible(pdev->dev.of_node,
> +					 "allwinner,sun5i-a13-ts")) {
> +		if (of_property_read_bool(pdev->dev.of_node,
> +					  "allwinner,ts-attached"))
> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> +					      sun5i_gpadc_mfd_cells_ts,
> +					      ARRAY_SIZE(sun5i_gpadc_mfd_cells_ts),
> +					      NULL, 0, NULL);
> +		else
> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> +					      sun5i_gpadc_mfd_cells,
> +					      ARRAY_SIZE(sun5i_gpadc_mfd_cells),
> +					      NULL, 0, NULL);
> +	} else if (of_device_is_compatible(pdev->dev.of_node,
> +					 "allwinner,sun6i-a31-ts")) {
> +		if (of_property_read_bool(pdev->dev.of_node,
> +					  "allwinner,ts-attached"))
> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> +					      sun6i_gpadc_mfd_cells_ts,
> +					      ARRAY_SIZE(sun6i_gpadc_mfd_cells_ts),
> +					      NULL, 0, NULL);
> +		else
> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> +					      sun6i_gpadc_mfd_cells,
> +					      ARRAY_SIZE(sun6i_gpadc_mfd_cells),
> +					      NULL, 0, NULL);
> +	}

Please don't use any of_device_is_compatible.

Thanks,
Maxime

-- 
Maxime Ripard, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20160721/2b13f92a/attachment-0001.sig>

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 4/5] input: touchscreen: support Allwinner SoCs' touchscreen
  2016-07-20  8:29 ` [PATCH 4/5] input: touchscreen: support Allwinner SoCs' touchscreen Quentin Schulz
  2016-07-20 17:25   ` Dmitry Torokhov
@ 2016-07-21  6:29   ` Maxime Ripard
  2016-07-21  6:41     ` Dmitry Torokhov
  2016-07-24 11:24   ` Jonathan Cameron
  2 siblings, 1 reply; 31+ messages in thread
From: Maxime Ripard @ 2016-07-21  6:29 UTC (permalink / raw)
  To: linux-arm-kernel

On Wed, Jul 20, 2016 at 10:29:10AM +0200, Quentin Schulz wrote:
> This adds support for Allwinner SoCs' (A10, A13 and A31) resistive
> touchscreen. This driver is probed by the MFD sunxi-gpadc-mfd.
> 
> This driver uses ADC channels exposed through the IIO framework by
> sunxi-gpadc-iio to get its data. When opening this input device, it will
> start buffering in the ADC driver and enable a TP_UP_PENDING irq. The ADC
> driver will fill in a buffer with all data and call the callback the input
> device associated with this buffer. The input device will then read the
> buffer two by two and send X and Y coordinates to the input framework based
> on what it received from the ADC's buffer. When closing this input device,
> the buffering is stopped.
> 
> Note that locations in the first received buffer after an TP_UP_PENDING irq
> occurred are unreliable, thus dropped.
> 
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> ---
>  drivers/input/touchscreen/Kconfig          |  11 +-
>  drivers/input/touchscreen/Makefile         |   2 +-
>  drivers/input/touchscreen/sun4i-ts.c       | 419 -----------------------------
>  drivers/input/touchscreen/sunxi-gpadc-ts.c | 195 ++++++++++++++

This new driver and the removal of the old one should be in two
separate patches, the removal being in the last one. You break
bisectability here, since the new driver won't be loaded, and the old
one will not be there anymore.

>  4 files changed, 201 insertions(+), 426 deletions(-)
>  delete mode 100644 drivers/input/touchscreen/sun4i-ts.c
>  create mode 100644 drivers/input/touchscreen/sunxi-gpadc-ts.c
> 
> diff --git a/drivers/input/touchscreen/Kconfig b/drivers/input/touchscreen/Kconfig
> index 8ecdc38..f16ce36 100644
> --- a/drivers/input/touchscreen/Kconfig
> +++ b/drivers/input/touchscreen/Kconfig
> @@ -1072,14 +1072,13 @@ config TOUCHSCREEN_STMPE
>  config TOUCHSCREEN_SUN4I
>  	tristate "Allwinner sun4i resistive touchscreen controller support"
>  	depends on ARCH_SUNXI || COMPILE_TEST
> -	depends on HWMON
> -	depends on THERMAL || !THERMAL_OF
> +	depends on SUNXI_ADC
>  	help
> -	  This selects support for the resistive touchscreen controller
> -	  found on Allwinner sunxi SoCs.
> +	  This selects support for the resistive touchscreen controller found
> +	  on Allwinner SoCs (A10, A13, A31).
>  
> -	  To compile this driver as a module, choose M here: the
> -	  module will be called sun4i-ts.
> +	  To compile this driver as a module, choose M here: the module will be
> +	  called sunxi-gpadc-ts.
>  
>  config TOUCHSCREEN_SUR40
>  	tristate "Samsung SUR40 (Surface 2.0/PixelSense) touchscreen"
> diff --git a/drivers/input/touchscreen/Makefile b/drivers/input/touchscreen/Makefile
> index f42975e..bdc1889 100644
> --- a/drivers/input/touchscreen/Makefile
> +++ b/drivers/input/touchscreen/Makefile
> @@ -65,7 +65,7 @@ obj-$(CONFIG_TOUCHSCREEN_PIXCIR)	+= pixcir_i2c_ts.o
>  obj-$(CONFIG_TOUCHSCREEN_S3C2410)	+= s3c2410_ts.o
>  obj-$(CONFIG_TOUCHSCREEN_ST1232)	+= st1232.o
>  obj-$(CONFIG_TOUCHSCREEN_STMPE)		+= stmpe-ts.o
> -obj-$(CONFIG_TOUCHSCREEN_SUN4I)		+= sun4i-ts.o
> +obj-$(CONFIG_TOUCHSCREEN_SUN4I)		+= sunxi-gpadc-ts.o
>  obj-$(CONFIG_TOUCHSCREEN_SUR40)		+= sur40.o
>  obj-$(CONFIG_TOUCHSCREEN_TI_AM335X_TSC)	+= ti_am335x_tsc.o
>  obj-$(CONFIG_TOUCHSCREEN_TOUCHIT213)	+= touchit213.o
> diff --git a/drivers/input/touchscreen/sun4i-ts.c b/drivers/input/touchscreen/sun4i-ts.c
> deleted file mode 100644
> index d07dd29..0000000
> --- a/drivers/input/touchscreen/sun4i-ts.c
> +++ /dev/null
> @@ -1,419 +0,0 @@
> -/*
> - * Allwinner sunxi resistive touchscreen controller driver
> - *
> - * Copyright (C) 2013 - 2014 Hans de Goede <hdegoede@redhat.com>
> - *
> - * The hwmon parts are based on work by Corentin LABBE which is:
> - * Copyright (C) 2013 Corentin LABBE <clabbe.montjoie@gmail.com>
> - *
> - * This program is free software; you can redistribute it and/or modify
> - * it under the terms of the GNU General Public License as published by
> - * the Free Software Foundation; either version 2 of the License, or
> - * (at your option) any later version.
> - *
> - * This program is distributed in the hope that it will be useful,
> - * but WITHOUT ANY WARRANTY; without even the implied warranty of
> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> - * GNU General Public License for more details.
> - */
> -
> -/*
> - * The sun4i-ts controller is capable of detecting a second touch, but when a
> - * second touch is present then the accuracy becomes so bad the reported touch
> - * location is not useable.
> - *
> - * The original android driver contains some complicated heuristics using the
> - * aprox. distance between the 2 touches to see if the user is making a pinch
> - * open / close movement, and then reports emulated multi-touch events around
> - * the last touch coordinate (as the dual-touch coordinates are worthless).
> - *
> - * These kinds of heuristics are just asking for trouble (and don't belong
> - * in the kernel). So this driver offers straight forward, reliable single
> - * touch functionality only.
> - *
> - * s.a. A20 User Manual "1.15 TP" (Documentation/arm/sunxi/README)
> - * (looks like the description in the A20 User Manual v1.3 is better
> - * than the one in the A10 User Manual v.1.5)
> - */
> -
> -#include <linux/err.h>
> -#include <linux/hwmon.h>
> -#include <linux/thermal.h>
> -#include <linux/init.h>
> -#include <linux/input.h>
> -#include <linux/interrupt.h>
> -#include <linux/io.h>
> -#include <linux/module.h>
> -#include <linux/of_platform.h>
> -#include <linux/platform_device.h>
> -#include <linux/slab.h>
> -
> -#define TP_CTRL0		0x00
> -#define TP_CTRL1		0x04
> -#define TP_CTRL2		0x08
> -#define TP_CTRL3		0x0c
> -#define TP_INT_FIFOC		0x10
> -#define TP_INT_FIFOS		0x14
> -#define TP_TPR			0x18
> -#define TP_CDAT			0x1c
> -#define TEMP_DATA		0x20
> -#define TP_DATA			0x24
> -
> -/* TP_CTRL0 bits */
> -#define ADC_FIRST_DLY(x)	((x) << 24) /* 8 bits */
> -#define ADC_FIRST_DLY_MODE(x)	((x) << 23)
> -#define ADC_CLK_SEL(x)		((x) << 22)
> -#define ADC_CLK_DIV(x)		((x) << 20) /* 3 bits */
> -#define FS_DIV(x)		((x) << 16) /* 4 bits */
> -#define T_ACQ(x)		((x) << 0) /* 16 bits */
> -
> -/* TP_CTRL1 bits */
> -#define STYLUS_UP_DEBOUN(x)	((x) << 12) /* 8 bits */
> -#define STYLUS_UP_DEBOUN_EN(x)	((x) << 9)
> -#define TOUCH_PAN_CALI_EN(x)	((x) << 6)
> -#define TP_DUAL_EN(x)		((x) << 5)
> -#define TP_MODE_EN(x)		((x) << 4)
> -#define TP_ADC_SELECT(x)	((x) << 3)
> -#define ADC_CHAN_SELECT(x)	((x) << 0)  /* 3 bits */
> -
> -/* on sun6i, bits 3~6 are left shifted by 1 to 4~7 */
> -#define SUN6I_TP_MODE_EN(x)	((x) << 5)
> -
> -/* TP_CTRL2 bits */
> -#define TP_SENSITIVE_ADJUST(x)	((x) << 28) /* 4 bits */
> -#define TP_MODE_SELECT(x)	((x) << 26) /* 2 bits */
> -#define PRE_MEA_EN(x)		((x) << 24)
> -#define PRE_MEA_THRE_CNT(x)	((x) << 0) /* 24 bits */
> -
> -/* TP_CTRL3 bits */
> -#define FILTER_EN(x)		((x) << 2)
> -#define FILTER_TYPE(x)		((x) << 0)  /* 2 bits */
> -
> -/* TP_INT_FIFOC irq and fifo mask / control bits */
> -#define TEMP_IRQ_EN(x)		((x) << 18)
> -#define OVERRUN_IRQ_EN(x)	((x) << 17)
> -#define DATA_IRQ_EN(x)		((x) << 16)
> -#define TP_DATA_XY_CHANGE(x)	((x) << 13)
> -#define FIFO_TRIG(x)		((x) << 8)  /* 5 bits */
> -#define DATA_DRQ_EN(x)		((x) << 7)
> -#define FIFO_FLUSH(x)		((x) << 4)
> -#define TP_UP_IRQ_EN(x)		((x) << 1)
> -#define TP_DOWN_IRQ_EN(x)	((x) << 0)
> -
> -/* TP_INT_FIFOS irq and fifo status bits */
> -#define TEMP_DATA_PENDING	BIT(18)
> -#define FIFO_OVERRUN_PENDING	BIT(17)
> -#define FIFO_DATA_PENDING	BIT(16)
> -#define TP_IDLE_FLG		BIT(2)
> -#define TP_UP_PENDING		BIT(1)
> -#define TP_DOWN_PENDING		BIT(0)
> -
> -/* TP_TPR bits */
> -#define TEMP_ENABLE(x)		((x) << 16)
> -#define TEMP_PERIOD(x)		((x) << 0)  /* t = x * 256 * 16 / clkin */
> -
> -struct sun4i_ts_data {
> -	struct device *dev;
> -	struct input_dev *input;
> -	void __iomem *base;
> -	unsigned int irq;
> -	bool ignore_fifo_data;
> -	int temp_data;
> -	int temp_offset;
> -	int temp_step;
> -};
> -
> -static void sun4i_ts_irq_handle_input(struct sun4i_ts_data *ts, u32 reg_val)
> -{
> -	u32 x, y;
> -
> -	if (reg_val & FIFO_DATA_PENDING) {
> -		x = readl(ts->base + TP_DATA);
> -		y = readl(ts->base + TP_DATA);
> -		/* The 1st location reported after an up event is unreliable */
> -		if (!ts->ignore_fifo_data) {
> -			input_report_abs(ts->input, ABS_X, x);
> -			input_report_abs(ts->input, ABS_Y, y);
> -			/*
> -			 * The hardware has a separate down status bit, but
> -			 * that gets set before we get the first location,
> -			 * resulting in reporting a click on the old location.
> -			 */
> -			input_report_key(ts->input, BTN_TOUCH, 1);
> -			input_sync(ts->input);
> -		} else {
> -			ts->ignore_fifo_data = false;
> -		}
> -	}
> -
> -	if (reg_val & TP_UP_PENDING) {
> -		ts->ignore_fifo_data = true;
> -		input_report_key(ts->input, BTN_TOUCH, 0);
> -		input_sync(ts->input);
> -	}
> -}
> -
> -static irqreturn_t sun4i_ts_irq(int irq, void *dev_id)
> -{
> -	struct sun4i_ts_data *ts = dev_id;
> -	u32 reg_val;
> -
> -	reg_val  = readl(ts->base + TP_INT_FIFOS);
> -
> -	if (reg_val & TEMP_DATA_PENDING)
> -		ts->temp_data = readl(ts->base + TEMP_DATA);
> -
> -	if (ts->input)
> -		sun4i_ts_irq_handle_input(ts, reg_val);
> -
> -	writel(reg_val, ts->base + TP_INT_FIFOS);
> -
> -	return IRQ_HANDLED;
> -}
> -
> -static int sun4i_ts_open(struct input_dev *dev)
> -{
> -	struct sun4i_ts_data *ts = input_get_drvdata(dev);
> -
> -	/* Flush, set trig level to 1, enable temp, data and up irqs */
> -	writel(TEMP_IRQ_EN(1) | DATA_IRQ_EN(1) | FIFO_TRIG(1) | FIFO_FLUSH(1) |
> -		TP_UP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
> -
> -	return 0;
> -}
> -
> -static void sun4i_ts_close(struct input_dev *dev)
> -{
> -	struct sun4i_ts_data *ts = input_get_drvdata(dev);
> -
> -	/* Deactivate all input IRQs */
> -	writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
> -}
> -
> -static int sun4i_get_temp(const struct sun4i_ts_data *ts, int *temp)
> -{
> -	/* No temp_data until the first irq */
> -	if (ts->temp_data == -1)
> -		return -EAGAIN;
> -
> -	*temp = ts->temp_data * ts->temp_step - ts->temp_offset;
> -
> -	return 0;
> -}
> -
> -static int sun4i_get_tz_temp(void *data, int *temp)
> -{
> -	return sun4i_get_temp(data, temp);
> -}
> -
> -static struct thermal_zone_of_device_ops sun4i_ts_tz_ops = {
> -	.get_temp = sun4i_get_tz_temp,
> -};
> -
> -static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
> -			 char *buf)
> -{
> -	struct sun4i_ts_data *ts = dev_get_drvdata(dev);
> -	int temp;
> -	int error;
> -
> -	error = sun4i_get_temp(ts, &temp);
> -	if (error)
> -		return error;
> -
> -	return sprintf(buf, "%d\n", temp);
> -}
> -
> -static ssize_t show_temp_label(struct device *dev,
> -			      struct device_attribute *devattr, char *buf)
> -{
> -	return sprintf(buf, "SoC temperature\n");
> -}
> -
> -static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
> -static DEVICE_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL);
> -
> -static struct attribute *sun4i_ts_attrs[] = {
> -	&dev_attr_temp1_input.attr,
> -	&dev_attr_temp1_label.attr,
> -	NULL
> -};
> -ATTRIBUTE_GROUPS(sun4i_ts);
> -
> -static int sun4i_ts_probe(struct platform_device *pdev)
> -{
> -	struct sun4i_ts_data *ts;
> -	struct device *dev = &pdev->dev;
> -	struct device_node *np = dev->of_node;
> -	struct device *hwmon;
> -	int error;
> -	u32 reg;
> -	bool ts_attached;
> -	u32 tp_sensitive_adjust = 15;
> -	u32 filter_type = 1;
> -
> -	ts = devm_kzalloc(dev, sizeof(struct sun4i_ts_data), GFP_KERNEL);
> -	if (!ts)
> -		return -ENOMEM;
> -
> -	ts->dev = dev;
> -	ts->ignore_fifo_data = true;
> -	ts->temp_data = -1;
> -	if (of_device_is_compatible(np, "allwinner,sun6i-a31-ts")) {
> -		/* Allwinner SDK has temperature (C) = (value / 6) - 271 */
> -		ts->temp_offset = 271000;
> -		ts->temp_step = 167;
> -	} else if (of_device_is_compatible(np, "allwinner,sun4i-a10-ts")) {
> -		/*
> -		 * The A10 temperature sensor has quite a wide spread, these
> -		 * parameters are based on the averaging of the calibration
> -		 * results of 4 completely different boards, with a spread of
> -		 * temp_step from 0.096 - 0.170 and temp_offset from 176 - 331.
> -		 */
> -		ts->temp_offset = 257000;
> -		ts->temp_step = 133;
> -	} else {
> -		/*
> -		 * The user manuals do not contain the formula for calculating
> -		 * the temperature. The formula used here is from the AXP209,
> -		 * which is designed by X-Powers, an affiliate of Allwinner:
> -		 *
> -		 *     temperature (C) = (value * 0.1) - 144.7
> -		 *
> -		 * Allwinner does not have any documentation whatsoever for
> -		 * this hardware. Moreover, it is claimed that the sensor
> -		 * is inaccurate and cannot work properly.
> -		 */
> -		ts->temp_offset = 144700;
> -		ts->temp_step = 100;
> -	}
> -
> -	ts_attached = of_property_read_bool(np, "allwinner,ts-attached");
> -	if (ts_attached) {
> -		ts->input = devm_input_allocate_device(dev);
> -		if (!ts->input)
> -			return -ENOMEM;
> -
> -		ts->input->name = pdev->name;
> -		ts->input->phys = "sun4i_ts/input0";
> -		ts->input->open = sun4i_ts_open;
> -		ts->input->close = sun4i_ts_close;
> -		ts->input->id.bustype = BUS_HOST;
> -		ts->input->id.vendor = 0x0001;
> -		ts->input->id.product = 0x0001;
> -		ts->input->id.version = 0x0100;
> -		ts->input->evbit[0] =  BIT(EV_SYN) | BIT(EV_KEY) | BIT(EV_ABS);
> -		__set_bit(BTN_TOUCH, ts->input->keybit);
> -		input_set_abs_params(ts->input, ABS_X, 0, 4095, 0, 0);
> -		input_set_abs_params(ts->input, ABS_Y, 0, 4095, 0, 0);
> -		input_set_drvdata(ts->input, ts);
> -	}
> -
> -	ts->base = devm_ioremap_resource(dev,
> -			      platform_get_resource(pdev, IORESOURCE_MEM, 0));
> -	if (IS_ERR(ts->base))
> -		return PTR_ERR(ts->base);
> -
> -	ts->irq = platform_get_irq(pdev, 0);
> -	error = devm_request_irq(dev, ts->irq, sun4i_ts_irq, 0, "sun4i-ts", ts);
> -	if (error)
> -		return error;
> -
> -	/*
> -	 * Select HOSC clk, clkin = clk / 6, adc samplefreq = clkin / 8192,
> -	 * t_acq = clkin / (16 * 64)
> -	 */
> -	writel(ADC_CLK_SEL(0) | ADC_CLK_DIV(2) | FS_DIV(7) | T_ACQ(63),
> -	       ts->base + TP_CTRL0);
> -
> -	/*
> -	 * tp_sensitive_adjust is an optional property
> -	 * tp_mode = 0 : only x and y coordinates, as we don't use dual touch
> -	 */
> -	of_property_read_u32(np, "allwinner,tp-sensitive-adjust",
> -			     &tp_sensitive_adjust);
> -	writel(TP_SENSITIVE_ADJUST(tp_sensitive_adjust) | TP_MODE_SELECT(0),
> -	       ts->base + TP_CTRL2);
> -
> -	/*
> -	 * Enable median and averaging filter, optional property for
> -	 * filter type.
> -	 */
> -	of_property_read_u32(np, "allwinner,filter-type", &filter_type);
> -	writel(FILTER_EN(1) | FILTER_TYPE(filter_type), ts->base + TP_CTRL3);
> -
> -	/* Enable temperature measurement, period 1953 (2 seconds) */
> -	writel(TEMP_ENABLE(1) | TEMP_PERIOD(1953), ts->base + TP_TPR);
> -
> -	/*
> -	 * Set stylus up debounce to aprox 10 ms, enable debounce, and
> -	 * finally enable tp mode.
> -	 */
> -	reg = STYLUS_UP_DEBOUN(5) | STYLUS_UP_DEBOUN_EN(1);
> -	if (of_device_is_compatible(np, "allwinner,sun6i-a31-ts"))
> -		reg |= SUN6I_TP_MODE_EN(1);
> -	else
> -		reg |= TP_MODE_EN(1);
> -	writel(reg, ts->base + TP_CTRL1);
> -
> -	/*
> -	 * The thermal core does not register hwmon devices for DT-based
> -	 * thermal zone sensors, such as this one.
> -	 */
> -	hwmon = devm_hwmon_device_register_with_groups(ts->dev, "sun4i_ts",
> -						       ts, sun4i_ts_groups);
> -	if (IS_ERR(hwmon))
> -		return PTR_ERR(hwmon);
> -
> -	devm_thermal_zone_of_sensor_register(ts->dev, 0, ts, &sun4i_ts_tz_ops);
> -
> -	writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
> -
> -	if (ts_attached) {
> -		error = input_register_device(ts->input);
> -		if (error) {
> -			writel(0, ts->base + TP_INT_FIFOC);
> -			return error;
> -		}
> -	}
> -
> -	platform_set_drvdata(pdev, ts);
> -	return 0;
> -}
> -
> -static int sun4i_ts_remove(struct platform_device *pdev)
> -{
> -	struct sun4i_ts_data *ts = platform_get_drvdata(pdev);
> -
> -	/* Explicit unregister to avoid open/close changing the imask later */
> -	if (ts->input)
> -		input_unregister_device(ts->input);
> -
> -	/* Deactivate all IRQs */
> -	writel(0, ts->base + TP_INT_FIFOC);
> -
> -	return 0;
> -}
> -
> -static const struct of_device_id sun4i_ts_of_match[] = {
> -	{ .compatible = "allwinner,sun4i-a10-ts", },
> -	{ .compatible = "allwinner,sun5i-a13-ts", },
> -	{ .compatible = "allwinner,sun6i-a31-ts", },
> -	{ /* sentinel */ }
> -};
> -MODULE_DEVICE_TABLE(of, sun4i_ts_of_match);
> -
> -static struct platform_driver sun4i_ts_driver = {
> -	.driver = {
> -		.name	= "sun4i-ts",
> -		.of_match_table = of_match_ptr(sun4i_ts_of_match),
> -	},
> -	.probe	= sun4i_ts_probe,
> -	.remove	= sun4i_ts_remove,
> -};
> -
> -module_platform_driver(sun4i_ts_driver);
> -
> -MODULE_DESCRIPTION("Allwinner sun4i resistive touchscreen controller driver");
> -MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
> -MODULE_LICENSE("GPL");
> diff --git a/drivers/input/touchscreen/sunxi-gpadc-ts.c b/drivers/input/touchscreen/sunxi-gpadc-ts.c
> new file mode 100644
> index 0000000..410d14f
> --- /dev/null
> +++ b/drivers/input/touchscreen/sunxi-gpadc-ts.c
> @@ -0,0 +1,195 @@
> +/* Touchscreen driver for Allwinner SoCs (A10, A13, A31) GPADC
> + *
> + * Copyright (c) 2016 Quentin Schulz <quentin.schulz@free-electrons>
> + *
> + * This program is free software; you can redistribute it and/or modify it under
> + * the terms of the GNU General Public License version 2 as published by the
> + * Free Software Foundation.
> + */
> +
> +#include <linux/init.h>
> +#include <linux/input.h>
> +#include <linux/interrupt.h>
> +#include <linux/module.h>
> +#include <linux/of.h>
> +#include <linux/platform_device.h>
> +#include <linux/regmap.h>
> +
> +#include <linux/iio/consumer.h>
> +#include <linux/mfd/sunxi-gpadc-mfd.h>
> +
> +struct sunxi_gpadc_ts {
> +	struct iio_cb_buffer	*buffer;
> +	struct input_dev	*input;
> +	struct platform_device	*pdev;
> +	unsigned int		tp_up_irq;
> +	bool			ignore_fifo_data;
> +};
> +
> +static int sunxi_gpadc_ts_open(struct input_dev *dev)
> +{
> +	struct sunxi_gpadc_ts *info = input_get_drvdata(dev);
> +	int ret;
> +
> +	ret = iio_channel_start_all_cb(info->buffer);
> +	if (ret) {
> +		dev_err(dev->dev.parent,
> +			"failed to start iio channels with callback\n");
> +		return ret;
> +	}
> +
> +	enable_irq(info->tp_up_irq);
> +
> +	return 0;
> +}
> +
> +static void sunxi_gpadc_ts_close(struct input_dev *dev)
> +{
> +	struct sunxi_gpadc_ts *info = input_get_drvdata(dev);
> +
> +	iio_channel_stop_all_cb(info->buffer);
> +
> +	disable_irq(info->tp_up_irq);
> +}
> +
> +/*
> + * This function will be called by iio_push_to_buffers from another driver
> + * (namely sunxi-gpadc-iio).

This can change in the future, please just mention the ADC driver.

> It will be passed the buffer filled with input
> + * values (X value then Y value) and the sunxi_gpadc_ts structure representing
> + * the device.
> + */
> +static int sunxi_gpadc_ts_callback(const void *data, void *private)
> +{
> +	const struct sunxi_gpadc_buffer *buffer = data;
> +	struct sunxi_gpadc_ts *info = private;
> +	int i = 0;
> +
> +	/* Locations in the first buffer after an up event are unreliable */
> +	if (info->ignore_fifo_data) {
> +		info->ignore_fifo_data = false;
> +		return 0;
> +	}
> +
> +	while (i + 1 < buffer->buff_size) {

So.... for (i = 0; i < buffer->buff_size; i += 2) { ?

> +		input_event(info->input, EV_ABS, ABS_X, buffer->buffer[i++]);
> +		input_event(info->input, EV_ABS, ABS_Y, buffer->buffer[i++]);

Using i and i + 1 as indinces here.

> +		input_event(info->input, EV_KEY, BTN_TOUCH, 1);
> +		input_sync(info->input);
> +	}

Why do you need an additional buffer compared to IIO's ?

This intertwined layout between X and Y would is rather complicated,
and would be better expressed with a structure with both
fields. Especially since X and Y are sampled at the same time.

> +
> +	return 0;
> +}
> +
> +static irqreturn_t sunxi_gpadc_tp_up_irq_handler(int irq, void *dev_id)
> +{
> +	struct sunxi_gpadc_ts *info = dev_id;
> +
> +	info->ignore_fifo_data = true;
> +
> +	input_event(info->input, EV_KEY, BTN_TOUCH, 0);
> +	input_sync(info->input);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static int sunxi_gpadc_ts_probe(struct platform_device *pdev)
> +{
> +	struct input_dev *input;
> +	struct sunxi_gpadc_ts *info;
> +	int ret, irq;
> +	struct sunxi_gpadc_mfd_dev *sunxi_gpadc_mfd_dev;
> +
> +	input = devm_input_allocate_device(&pdev->dev);
> +	if (!input)
> +		return -ENOMEM;
> +
> +	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
> +	if (!info)
> +		return -ENOMEM;
> +
> +	info->buffer = iio_channel_get_all_cb(&pdev->dev,
> +					      &sunxi_gpadc_ts_callback,
> +					      (void *)info);
> +	if (IS_ERR(info->buffer)) {
> +		if (PTR_ERR(info->buffer) == -ENODEV)
> +			return -EPROBE_DEFER;
> +		return PTR_ERR(info->buffer);
> +	}
> +
> +	info->pdev = pdev;
> +	info->input = input;
> +	info->ignore_fifo_data = false;
> +	platform_set_drvdata(pdev, info);
> +
> +	input->dev.parent = &pdev->dev;
> +	input->name = "sunxi-gpadc-ts";
> +	input->id.bustype = BUS_HOST;
> +	input->open = sunxi_gpadc_ts_open;
> +	input->close = sunxi_gpadc_ts_close;
> +	__set_bit(EV_SYN, input->evbit);
> +	input_set_capability(input, EV_KEY, BTN_TOUCH);
> +	input_set_abs_params(input, ABS_X, 0, 4095, 0, 0);
> +	input_set_abs_params(input, ABS_Y, 0, 4095, 0, 0);

Shouldn't the max be the screen pixel size here?

> +	input_set_drvdata(input, info);
> +
> +	irq = platform_get_irq_byname(pdev, "TP_UP_PENDING");
> +	if (irq < 0) {
> +		dev_err(&pdev->dev, "no TP_UP_PENDING interrupt registered\n");
> +		ret = irq;
> +		goto err;
> +	}
> +
> +	sunxi_gpadc_mfd_dev = dev_get_drvdata(pdev->dev.parent);
> +
> +	irq = regmap_irq_get_virq(sunxi_gpadc_mfd_dev->regmap_irqc, irq);
> +	ret = devm_request_any_context_irq(&pdev->dev, irq,
> +					   sunxi_gpadc_tp_up_irq_handler, 0,
> +					   "tp_up", info);
> +	if (ret < 0) {
> +		dev_err(&pdev->dev,
> +			"could not request TP_UP_PENDING interrupt: %d\n", ret);
> +		goto err;
> +	}

You enable the interrupts...

> +	info->tp_up_irq = irq;
> +	disable_irq(irq);
> +
> +	ret = input_register_device(input);
> +	if (ret) {
> +		dev_err(&pdev->dev, "failed to register input device\n");
> +		goto err;
> +	}

... but your driver isn't registered yet. How does input_report and
input_sync behave in such a case?

> +	return 0;
> +
> +err:
> +	iio_channel_release_all_cb(info->buffer);
> +
> +	return ret;
> +}
> +
> +static int sunxi_gpadc_ts_remove(struct platform_device *pdev)
> +{
> +	struct sunxi_gpadc_ts *info = platform_get_drvdata(pdev);
> +
> +	iio_channel_stop_all_cb(info->buffer);
> +	iio_channel_release_all_cb(info->buffer);
> +
> +	disable_irq(info->tp_up_irq);
> +
> +	return 0;
> +}

I would expect your close function to have been called already when
you're there. Which would lead to two calls to iio_channel_stop_all_cb
and disable_irq.

Maxime

-- 
Maxime Ripard, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20160721/063f8b09/attachment-0001.sig>

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 4/5] input: touchscreen: support Allwinner SoCs' touchscreen
  2016-07-21  6:29   ` Maxime Ripard
@ 2016-07-21  6:41     ` Dmitry Torokhov
  2016-07-25  9:45       ` maxime.ripard at free-electrons.com
  0 siblings, 1 reply; 31+ messages in thread
From: Dmitry Torokhov @ 2016-07-21  6:41 UTC (permalink / raw)
  To: linux-arm-kernel

On Thu, Jul 21, 2016 at 08:29:50AM +0200, Maxime Ripard wrote:
> On Wed, Jul 20, 2016 at 10:29:10AM +0200, Quentin Schulz wrote:
> > +	irq = regmap_irq_get_virq(sunxi_gpadc_mfd_dev->regmap_irqc, irq);
> > +	ret = devm_request_any_context_irq(&pdev->dev, irq,
> > +					   sunxi_gpadc_tp_up_irq_handler, 0,
> > +					   "tp_up", info);
> > +	if (ret < 0) {
> > +		dev_err(&pdev->dev,
> > +			"could not request TP_UP_PENDING interrupt: %d\n", ret);
> > +		goto err;
> > +	}
> 
> You enable the interrupts...
> 
> > +	info->tp_up_irq = irq;
> > +	disable_irq(irq);
> > +
> > +	ret = input_register_device(input);
> > +	if (ret) {
> > +		dev_err(&pdev->dev, "failed to register input device\n");
> > +		goto err;
> > +	}
> 
> ... but your driver isn't registered yet. How does input_report and
> input_sync behave in such a case?

This is explicitly allowed:

"
...
 * NOTE: input_event() may be safely used right after input device was
 * allocated with input_allocate_device(), even before it is registered
 * with input_register_device(), but the event will not reach any of the
 * input handlers. Such early invocation of input_event() may be used
 * to 'seed' initial state of a switch or initial position of absolute
 * axis, etc.
 */
"

Thanks.


-- 
Dmitry

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 2/5] mfd: sunxi-gpadc-mfd: add buffer structure
  2016-07-20  8:29 ` [PATCH 2/5] mfd: sunxi-gpadc-mfd: add buffer structure Quentin Schulz
@ 2016-07-24 10:32   ` Jonathan Cameron
  0 siblings, 0 replies; 31+ messages in thread
From: Jonathan Cameron @ 2016-07-24 10:32 UTC (permalink / raw)
  To: linux-arm-kernel

On 20/07/16 09:29, Quentin Schulz wrote:
> This adds a buffer structure for files including the sunxi-gpadc-mfd
> header. This structure has a buffer of 32 u32 values to store data from the
> FIFO of the GPADC of Allwinner SoCs. A buff_size is provided in case the
> buffer is not full.
> 
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
I'd roll this into the first patch that uses it.
Easier to see what is for when reviewing then!

Jonathan
> ---
>  include/linux/mfd/sunxi-gpadc-mfd.h | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/include/linux/mfd/sunxi-gpadc-mfd.h b/include/linux/mfd/sunxi-gpadc-mfd.h
> index 7155845..f658299 100644
> --- a/include/linux/mfd/sunxi-gpadc-mfd.h
> +++ b/include/linux/mfd/sunxi-gpadc-mfd.h
> @@ -20,4 +20,9 @@ struct sunxi_gpadc_mfd_dev {
>  	void __iomem			*regs;
>  };
>  
> +struct sunxi_gpadc_buffer {
> +	u32				buffer[32];
> +	unsigned int			buff_size;
> +};
> +
>  #endif
> 

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 3/5] iio: adc: sunxi-gpadc-iio: enable iio_buffers
  2016-07-20  8:29 ` [PATCH 3/5] iio: adc: sunxi-gpadc-iio: enable iio_buffers Quentin Schulz
  2016-07-20  8:38   ` Peter Meerwald-Stadler
@ 2016-07-24 11:03   ` Jonathan Cameron
  2016-09-24 17:40     ` Quentin Schulz
  1 sibling, 1 reply; 31+ messages in thread
From: Jonathan Cameron @ 2016-07-24 11:03 UTC (permalink / raw)
  To: linux-arm-kernel

On 20/07/16 09:29, Quentin Schulz wrote:
> This enables the use of buffers on ADC channels of sunxi-gpadc-iio driver.
> It also prepares the code which will be used by the touchscreen driver
> named sunxi-gpadc-ts.
> 
> The GPADC on Allwinner SoCs (A10, A13 and A31) has a 12 bits register for
> conversion's data. The GPADC uses the same ADC channels for the ADC and the
> touchscreen therefore exposes these channels to the sunxi-gpadc-ts iio
> consumer which will be in charge of reading data from these channels for
> the input framework.
> 
> The temperature can only be read when in touchscreen mode. This means if
> the buffers are being used for the ADC, the temperature sensor cannot be
> read.
That may be the bizarest hardware restriction I've heard of in a while! :)
> 
> When a FIFO_DATA_PENDING irq occurs, its handler will read the entire FIFO
> and fill a buffer before sending it to the consumers which registered in
> IIO for the ADC channels.
> 
> When a consumer starts buffering ADC channels,
> sunxi_gpadc_buffer_postenable is called and will enable FIFO_DATA_PENDING
> irq and select the mode in which the GPADC should run (ADC or touchscreen)
> depending on a property of the DT ("allwinner,ts-attached").
> When the consumer stops buffering, it disables the same irq.
Hmm. Might be possible to distinguish which consumer caused the start.
Thus, if the touchscreen is there we would know purely based on the
driver being the requester that we need to be in touchscreen mode.

> 
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
You are moving fast on this - I'd have been tempted to do a mega
series with the updated version of the basic support and this on top
rather than a new unconnected series.

(I'd forgotten that was still under review so got confused when I
went to look something up in the files you are modifying!).
> ---
>  drivers/iio/adc/Kconfig           |   1 +
>  drivers/iio/adc/sunxi-gpadc-iio.c | 153 ++++++++++++++++++++++++++++++++++----
>  2 files changed, 138 insertions(+), 16 deletions(-)
> 
> diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
> index 184856f..15e3b08 100644
> --- a/drivers/iio/adc/Kconfig
> +++ b/drivers/iio/adc/Kconfig
> @@ -342,6 +342,7 @@ config SUNXI_ADC
>  	tristate "ADC driver for sunxi platforms"
>  	depends on IIO
>  	depends on MFD_SUNXI_ADC
> +	depends on IIO_BUFFER_CB
>  	help
>  	  Say yes here to build support for Allwinner (A10, A13 and A31) SoCs
>  	  ADC. This ADC provides 4 channels which can be used as an ADC or as a
> diff --git a/drivers/iio/adc/sunxi-gpadc-iio.c b/drivers/iio/adc/sunxi-gpadc-iio.c
> index 87cc913..2e44ca7 100644
> --- a/drivers/iio/adc/sunxi-gpadc-iio.c
> +++ b/drivers/iio/adc/sunxi-gpadc-iio.c
> @@ -16,8 +16,9 @@
>  #include <linux/platform_device.h>
>  #include <linux/regmap.h>
>  
> -#include <linux/iio/iio.h>
> +#include <linux/iio/buffer.h>
>  #include <linux/iio/driver.h>
> +#include <linux/iio/iio.h>
Can't say I'm a particular fan of reordering headers to be in alphabetical
order, but I suppose it doesn't really matter if you want to do it.
(to my mind there is a tree structure implicit in these headers with iio.h
at the top for generic support, then the various sub elements below).

>  #include <linux/iio/machine.h>
>  #include <linux/mfd/sunxi-gpadc-mfd.h>
>  
> @@ -71,6 +72,7 @@
>  #define SUNXI_GPADC_TP_DATA_XY_CHANGE		BIT(13)
>  #define SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(x)	((x) << 8)  /* 5 bits */
>  #define SUNXI_GPADC_TP_DATA_DRQ_EN		BIT(7)
> +/* Be careful, flushing FIFO spawns SUNXI_GPADC_FIFO_DATA_PENDING interrupts */
Sounds like you learned that one the hard way ;)
>  #define SUNXI_GPADC_TP_FIFO_FLUSH		BIT(4)
>  #define SUNXI_GPADC_TP_UP_IRQ_EN		BIT(1)
>  #define SUNXI_GPADC_TP_DOWN_IRQ_EN		BIT(0)
> @@ -79,6 +81,7 @@
>  #define SUNXI_GPADC_TEMP_DATA_PENDING		BIT(18)
>  #define SUNXI_GPADC_FIFO_OVERRUN_PENDING	BIT(17)
>  #define SUNXI_GPADC_FIFO_DATA_PENDING		BIT(16)
> +#define SUNXI_GPADC_RXA_CNT			GENMASK(12, 8)
>  #define SUNXI_GPADC_TP_IDLE_FLG			BIT(2)
>  #define SUNXI_GPADC_TP_UP_PENDING		BIT(1)
>  #define SUNXI_GPADC_TP_DOWN_PENDING		BIT(0)
> @@ -101,19 +104,43 @@ struct sunxi_gpadc_dev {
>  	unsigned int			fifo_data_irq;
>  	unsigned int			temp_data_irq;
>  	unsigned int			flags;
> +	struct iio_dev			*indio_dev;
I was suprised to see this as normally it is cleaner to structure
the whole code to go in one direction through the structures (which is
why we don't provide a generic iio_device_from_priv bit of pointer magic).

Anyhow, don't htink you are actually using it ;)

> +	struct sunxi_gpadc_buffer	buffer;
> +	bool				ts_attached;
> +	bool				buffered;
>  };
>  
> -#define SUNXI_GPADC_ADC_CHANNEL(_channel, _name) {		\
> +#define SUNXI_GPADC_ADC_CHANNEL(_channel, _name, _index) {	\
>  	.type = IIO_VOLTAGE,					\
>  	.indexed = 1,						\
>  	.channel = _channel,					\
>  	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
>  	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
>  	.datasheet_name = _name,				\
> +	.scan_index = _index,					\
> +	.scan_type = {						\
> +		.sign = 'u',					\
> +		.realbits = 12,					\
> +		.storagebits = 16,				\
> +		.shift = 0,					\
No need to specify shift.  C(99?) guarantees that any non set elements
are 0 initialized and 0 is the obvious default here.
> +		.endianness = IIO_LE,				\
> +	},							\
>  }
>  
>  static struct iio_map sunxi_gpadc_hwmon_maps[] = {
>  	{
> +		.adc_channel_label = "adc_chan0",
> +		.consumer_dev_name = "sunxi-gpadc-ts.0",
> +	}, {
> +		.adc_channel_label = "adc_chan1",
> +		.consumer_dev_name = "sunxi-gpadc-ts.0",
> +	}, {
> +		.adc_channel_label = "adc_chan2",
> +		.consumer_dev_name = "sunxi-gpadc-ts.0",
> +	}, {
> +		.adc_channel_label = "adc_chan3",
> +		.consumer_dev_name = "sunxi-gpadc-ts.0",
> +	}, {
>  		.adc_channel_label = "temp_adc",
>  		.consumer_dev_name = "iio_hwmon.0",
>  	},
> @@ -121,28 +148,33 @@ static struct iio_map sunxi_gpadc_hwmon_maps[] = {
>  };
>  
>  static const struct iio_chan_spec sunxi_gpadc_channels[] = {
> -	SUNXI_GPADC_ADC_CHANNEL(0, "adc_chan0"),
> -	SUNXI_GPADC_ADC_CHANNEL(1, "adc_chan1"),
> -	SUNXI_GPADC_ADC_CHANNEL(2, "adc_chan2"),
> -	SUNXI_GPADC_ADC_CHANNEL(3, "adc_chan3"),
Why not index from 0? The scan indexes have to be monotonic, but 0 is just
fine.
> +	SUNXI_GPADC_ADC_CHANNEL(0, "adc_chan0", 1),
> +	SUNXI_GPADC_ADC_CHANNEL(1, "adc_chan1", 2),
> +	SUNXI_GPADC_ADC_CHANNEL(2, "adc_chan2", 3),
> +	SUNXI_GPADC_ADC_CHANNEL(3, "adc_chan3", 4),
>  	{
>  		.type = IIO_TEMP,
>  		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
>  		.datasheet_name = "temp_adc",
>  		.extend_name = "SoC temperature",
>  	},
> -	{ /* sentinel */ },
This should never have been there - explicit size is used, not a null
element.  Fix that in the prior patches please.
>  };
>  
>  static int sunxi_gpadc_adc_read(struct iio_dev *indio_dev, int channel,
>  				int *val)
>  {
>  	struct sunxi_gpadc_dev *info = iio_priv(indio_dev);
> +	bool buffered = info->buffered;
Not worth the local version...
>  	int ret = 0;
> +	unsigned int reg;
>  
>  	mutex_lock(&indio_dev->mlock);
>  
>  	reinit_completion(&info->completion);
> +
> +	reg = SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(1) | SUNXI_GPADC_TP_FIFO_FLUSH;
> +	regmap_update_bits(info->regmap, SUNXI_GPADC_TP_INT_FIFOC, reg, reg);
I'd put it in directly rahter than having a reg local variable.  To mind
mind that would be slightly easier to understand.
> +
>  	if (info->flags & SUNXI_GPADC_ARCH_SUN6I)
>  		regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL1,
>  			     SUNXI_GPADC_SUN6I_TP_MODE_EN |
> @@ -153,9 +185,9 @@ static int sunxi_gpadc_adc_read(struct iio_dev *indio_dev, int channel,
>  			     SUNXI_GPADC_TP_MODE_EN |
>  			     SUNXI_GPADC_TP_ADC_SELECT |
>  			     SUNXI_GPADC_ADC_CHAN_SELECT(channel));
> -	regmap_write(info->regmap, SUNXI_GPADC_TP_INT_FIFOC,
> -		     SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(1) |
> -		     SUNXI_GPADC_TP_FIFO_FLUSH);
Whole load of infrastructure in place to lock buffered mode out and
revent transitions when we can't have them.

iio_claim_direct_mode etc.  I think you can just use that here?
If you need to do extra checks on it being enabled that should be
fine too.

As a general rule, it makes sense to simply disable polled reads
if in buffered mode.  Leads to much simpler code and generally
the data is already known to userspace anyway.

I have been meaning to do it a bit better when we have multiple
in kernel consumers, some expecting polled readings and some
pushed.  There some core caching magic will make sense to
keep the polled channels as available as possible when running
the  buffers.

A bit fiddly to implement + might have some slightly suprising
results on delays on channels when say a sysfs trigger is
being used... (not a problem here as you have a fifo and hence
aren't using triggers).

Anyhow, not really relevant here :)

> +
> +	info->buffered = false;
> +
>  	enable_irq(info->fifo_data_irq);
>  
>  	if (!wait_for_completion_timeout(&info->completion,
> @@ -169,6 +201,7 @@ static int sunxi_gpadc_adc_read(struct iio_dev *indio_dev, int channel,
>  out:
>  	disable_irq(info->fifo_data_irq);
>  	mutex_unlock(&indio_dev->mlock);
> +	info->buffered = buffered;
>  
>  	return ret;
>  }
> @@ -177,20 +210,22 @@ static int sunxi_gpadc_temp_read(struct iio_dev *indio_dev, int *val)
>  {
>  	struct sunxi_gpadc_dev *info = iio_priv(indio_dev);
>  	int ret = 0;
> +	unsigned int reg;
>  
>  	mutex_lock(&indio_dev->mlock);
>  
>  	reinit_completion(&info->completion);
>  
> -	regmap_write(info->regmap, SUNXI_GPADC_TP_INT_FIFOC,
> -		     SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(1) |
> -		     SUNXI_GPADC_TP_FIFO_FLUSH);
> +	reg = SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(1) | SUNXI_GPADC_TP_FIFO_FLUSH;
> +	regmap_update_bits(info->regmap, SUNXI_GPADC_TP_INT_FIFOC, reg, reg);
> +
Again, I'd drop the local variable reg and put it directly in the update_bits
call.
>  	if (info->flags & SUNXI_GPADC_ARCH_SUN6I)
>  		regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL1,
>  			     SUNXI_GPADC_SUN6I_TP_MODE_EN);
>  	else
>  		regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL1,
>  			     SUNXI_GPADC_TP_MODE_EN);
> +
>  	enable_irq(info->temp_data_irq);
>  
>  	if (!wait_for_completion_timeout(&info->completion,
> @@ -211,7 +246,6 @@ out:
>  	mutex_unlock(&indio_dev->mlock);
>  
>  	return ret;
> -
>  }
>  
>  static int sunxi_gpadc_read_raw(struct iio_dev *indio_dev,
> @@ -219,15 +253,22 @@ static int sunxi_gpadc_read_raw(struct iio_dev *indio_dev,
>  				int *val, int *val2, long mask)
>  {
>  	int ret;
> +	struct sunxi_gpadc_dev *info = iio_priv(indio_dev);
>  
>  	switch (mask) {
>  	case IIO_CHAN_INFO_PROCESSED:
> +		if (info->buffered && !info->ts_attached)
> +			return -EBUSY;
> +
>  		ret = sunxi_gpadc_temp_read(indio_dev, val);
>  		if (ret)
>  			return ret;
>  
>  		return IIO_VAL_INT;
>  	case IIO_CHAN_INFO_RAW:
Definitely use the iio_claim_direct_mode stuff here to avoid possible races
with the buffer being enabled whilst this read is in flight.
> +		if (info->buffered)
> +			return -EBUSY;
> +
>  		ret = sunxi_gpadc_adc_read(indio_dev, chan->channel, val);
>  		if (ret)
>  			return ret;
> @@ -261,7 +302,29 @@ static irqreturn_t sunxi_gpadc_temp_data_irq_handler(int irq, void *dev_id)
>  static irqreturn_t sunxi_gpadc_fifo_data_irq_handler(int irq, void *dev_id)
>  {
>  	struct sunxi_gpadc_dev *info = dev_id;
> -	int ret;
> +	int ret, reg, i, fifo_count;
> +
> +	if (info->buffered) {
> +		if (regmap_read(info->regmap, SUNXI_GPADC_TP_INT_FIFOS, &reg))
> +			return IRQ_HANDLED;
> +
> +		fifo_count = (reg & SUNXI_GPADC_RXA_CNT) >> 8;
> +		/* Sometimes, the interrupt occurs when the FIFO is empty. */
> +		if (!fifo_count)
> +			return IRQ_HANDLED;
> +
> +		for (i = 0; i < fifo_count; i++) {
> +			if (regmap_read(info->regmap, SUNXI_GPADC_TP_DATA,
> +					&info->buffer.buffer[i]))
> +				return IRQ_HANDLED;
> +		}
> +
> +		info->buffer.buff_size = i;
> +
> +		iio_push_to_buffers(info->indio_dev, &info->buffer);
This is expecting a single 'scan' - e.g. set of channels read at one
time.  Here I think we could have repeated sets of channels?
(at least that would be what is normally meant by a fifo in such
a device).

If so you need to read 'whole' scans and push them one at a time.
We don't yet have a bulk iio_push_to_buffers, though we can add
one if it makes sense.  Care will be needed though as we'd need
handle the case of different consumers either supporting or
not supporting this new functionality.  Not particularly hard though
if it is worth doing.
> +
> +		return IRQ_HANDLED;
> +	}
>  
>  	ret = regmap_read(info->regmap, SUNXI_GPADC_TP_DATA, &info->adc_data);
>  	if (ret == 0)
> @@ -270,6 +333,58 @@ static irqreturn_t sunxi_gpadc_fifo_data_irq_handler(int irq, void *dev_id)
>  	return IRQ_HANDLED;
>  }
>  
> +static int sunxi_gpadc_buffer_postenable(struct iio_dev *indio_dev)
> +{
> +	struct sunxi_gpadc_dev *info = iio_priv(indio_dev);
> +	unsigned int reg;
> +	int ret;
> +
> +	reg = SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(1) | SUNXI_GPADC_TP_FIFO_FLUSH;
> +	regmap_update_bits(info->regmap, SUNXI_GPADC_TP_INT_FIFOC, reg, reg);
Check for errors and return them...
> +
> +	if (info->ts_attached) {
> +		reg = SUNXI_GPADC_STYLUS_UP_DEBOUNCE(5) |
> +		      SUNXI_GPADC_STYLUS_UP_DEBOUNCE_EN;
> +
> +		if (info->flags & SUNXI_GPADC_ARCH_SUN6I)
> +			reg |= SUNXI_GPADC_SUN6I_TP_MODE_EN;
> +		else
> +			reg |= SUNXI_GPADC_TP_MODE_EN;
> +	} else {
> +		if (info->flags & SUNXI_GPADC_ARCH_SUN6I)
> +			reg = SUNXI_GPADC_SUN6I_TP_MODE_EN |
> +			      SUNXI_GPADC_SUN6I_TP_ADC_SELECT;
> +		else
> +			reg = SUNXI_GPADC_TP_MODE_EN |
> +			      SUNXI_GPADC_TP_ADC_SELECT;
> +	}
> +
> +	if (regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL1, reg))
> +		return ret;
ret hasn't been initialized and anwyay it should be the return of
regmap_write.
> +
> +	info->buffered = true;
> +
> +	enable_irq(info->fifo_data_irq);
Needs a comment to explain why this is needed.  Until the above enables
IRQs I'd normally expect them to simply not occur.  Hence it's
unusual to have to explicitly mask them in such a driver as this.
> +
> +	return 0;
> +}
> +
> +static int sunxi_gpadc_buffer_predisable(struct iio_dev *indio_dev)
> +{
> +	struct sunxi_gpadc_dev *info = iio_priv(indio_dev);
> +
> +	disable_irq(info->fifo_data_irq);
I would undo the various enables above to get back to the state
prior to postenable above.
> +
> +	info->buffered = false;
> +
> +	return 0;
> +}
> +
> +static const struct iio_buffer_setup_ops sunxi_gpadc_buffer_setup_ops = {
> +	.postenable = sunxi_gpadc_buffer_postenable,
> +	.predisable = sunxi_gpadc_buffer_predisable,
> +};
> +
>  static int sunxi_gpadc_probe(struct platform_device *pdev)
>  {
>  	struct sunxi_gpadc_dev *info;
> @@ -286,16 +401,20 @@ static int sunxi_gpadc_probe(struct platform_device *pdev)
>  	info = iio_priv(indio_dev);
>  
>  	info->regmap = sunxi_gpadc_mfd_dev->regmap;
> +	info->indio_dev = indio_dev;
Not used as far as I can see.
>  	init_completion(&info->completion);
>  	indio_dev->name = dev_name(&pdev->dev);
>  	indio_dev->dev.parent = &pdev->dev;
>  	indio_dev->dev.of_node = pdev->dev.of_node;
>  	indio_dev->info = &sunxi_gpadc_iio_info;
> -	indio_dev->modes = INDIO_DIRECT_MODE;
> +	indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
>  	indio_dev->num_channels = ARRAY_SIZE(sunxi_gpadc_channels);
>  	indio_dev->channels = sunxi_gpadc_channels;
> +	indio_dev->setup_ops = &sunxi_gpadc_buffer_setup_ops;
>  
>  	info->flags = platform_get_device_id(pdev)->driver_data;
> +	info->ts_attached = of_property_read_bool(pdev->dev.parent->of_node,
> +						  "allwinner,ts-attached");
>  
>  	regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL0, SUNXI_GPADC_FS_DIV(7) |
>  		     SUNXI_GPADC_ADC_CLK_DIVIDER(2) | SUNXI_GPADC_T_ACQ(63));
> @@ -305,6 +424,8 @@ static int sunxi_gpadc_probe(struct platform_device *pdev)
>  	else
>  		regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL1,
>  			     SUNXI_GPADC_TP_MODE_EN);
> +	regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL2,
> +		     SUNXI_GPADC_TP_SENSITIVE_ADJUST(15));
>  	regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL3, SUNXI_GPADC_FILTER_EN |
>  		     SUNXI_GPADC_FILTER_TYPE(1));
>  	regmap_write(info->regmap, SUNXI_GPADC_TP_TPR,
> 

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 4/5] input: touchscreen: support Allwinner SoCs' touchscreen
  2016-07-20  8:29 ` [PATCH 4/5] input: touchscreen: support Allwinner SoCs' touchscreen Quentin Schulz
  2016-07-20 17:25   ` Dmitry Torokhov
  2016-07-21  6:29   ` Maxime Ripard
@ 2016-07-24 11:24   ` Jonathan Cameron
  2016-09-25 19:44     ` Quentin Schulz
  2 siblings, 1 reply; 31+ messages in thread
From: Jonathan Cameron @ 2016-07-24 11:24 UTC (permalink / raw)
  To: linux-arm-kernel

On 20/07/16 09:29, Quentin Schulz wrote:
> This adds support for Allwinner SoCs' (A10, A13 and A31) resistive
> touchscreen. This driver is probed by the MFD sunxi-gpadc-mfd.
> 
> This driver uses ADC channels exposed through the IIO framework by
> sunxi-gpadc-iio to get its data. When opening this input device, it will
> start buffering in the ADC driver and enable a TP_UP_PENDING irq. The ADC
> driver will fill in a buffer with all data and call the callback the input
> device associated with this buffer. The input device will then read the
> buffer two by two and send X and Y coordinates to the input framework based
> on what it received from the ADC's buffer. When closing this input device,
> the buffering is stopped.
> 
> Note that locations in the first received buffer after an TP_UP_PENDING irq
> occurred are unreliable, thus dropped.
>
I think I now understand what you are doing.

The channel grab is grabbing 4 channels, when there are only two real
ones (x and y) then you are abusing the callback interface from IIO.
That transmits only one scan (e.g. here (x,y)) per call. Because you
have added a sideband route for the buffer size what you have wil work.

However, it is not how the interface should be used.  Please fix that.
Using it correctly is not a big issue.

On the channels front, I'd be tempted to do this as follows:

6 Channels, not 4.

First 4 are standard ADC channels
Next 2 are the touch screen channels with appropriate descriptions
(guessing these are actually differential channels across the various
wires of the first two?)

Then you set the map up to apply to the last two channels only.
By setting available_scan_masks to the relevant options in the IIO driver
you'll be able to automatically lock the device when ever the
touch screeen driver is loaded.

That map will be something like (in binary
000000
000011
000100
001000
010000
100000
001100
010100
011000
100100
101000
110000
011100
101100
110100
111000
111100

thus any combination of the ADC channels, but always all or none of the
touchscreen (none when ever the ADC channels are on) the order above
ensures we always turn on the minimum possible for a given requirement.

Hence whenever the touch screen is there it'll lock out the ADC usage.
Your postenable can look at what is there and put it in the right mode.

After that, break your fifo read up into individual pairs of readings
and push those out.

That way we end up using the interface in the standard fashion.

You'll also need fix the usage of the fifo for ADC mode which suffers
from the same problem.  There all sorts of nasty crashes might occur
or you might just loose data.

Jonathan
> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> ---
>  drivers/input/touchscreen/Kconfig          |  11 +-
>  drivers/input/touchscreen/Makefile         |   2 +-
>  drivers/input/touchscreen/sun4i-ts.c       | 419 -----------------------------
>  drivers/input/touchscreen/sunxi-gpadc-ts.c | 195 ++++++++++++++
>  4 files changed, 201 insertions(+), 426 deletions(-)
>  delete mode 100644 drivers/input/touchscreen/sun4i-ts.c
>  create mode 100644 drivers/input/touchscreen/sunxi-gpadc-ts.c
> 
> diff --git a/drivers/input/touchscreen/Kconfig b/drivers/input/touchscreen/Kconfig
> index 8ecdc38..f16ce36 100644
> --- a/drivers/input/touchscreen/Kconfig
> +++ b/drivers/input/touchscreen/Kconfig
> @@ -1072,14 +1072,13 @@ config TOUCHSCREEN_STMPE
>  config TOUCHSCREEN_SUN4I
>  	tristate "Allwinner sun4i resistive touchscreen controller support"
>  	depends on ARCH_SUNXI || COMPILE_TEST
> -	depends on HWMON
> -	depends on THERMAL || !THERMAL_OF
> +	depends on SUNXI_ADC
>  	help
> -	  This selects support for the resistive touchscreen controller
> -	  found on Allwinner sunxi SoCs.
> +	  This selects support for the resistive touchscreen controller found
> +	  on Allwinner SoCs (A10, A13, A31).
>  
> -	  To compile this driver as a module, choose M here: the
> -	  module will be called sun4i-ts.
> +	  To compile this driver as a module, choose M here: the module will be
> +	  called sunxi-gpadc-ts.
>  
>  config TOUCHSCREEN_SUR40
>  	tristate "Samsung SUR40 (Surface 2.0/PixelSense) touchscreen"
> diff --git a/drivers/input/touchscreen/Makefile b/drivers/input/touchscreen/Makefile
> index f42975e..bdc1889 100644
> --- a/drivers/input/touchscreen/Makefile
> +++ b/drivers/input/touchscreen/Makefile
> @@ -65,7 +65,7 @@ obj-$(CONFIG_TOUCHSCREEN_PIXCIR)	+= pixcir_i2c_ts.o
>  obj-$(CONFIG_TOUCHSCREEN_S3C2410)	+= s3c2410_ts.o
>  obj-$(CONFIG_TOUCHSCREEN_ST1232)	+= st1232.o
>  obj-$(CONFIG_TOUCHSCREEN_STMPE)		+= stmpe-ts.o
> -obj-$(CONFIG_TOUCHSCREEN_SUN4I)		+= sun4i-ts.o
> +obj-$(CONFIG_TOUCHSCREEN_SUN4I)		+= sunxi-gpadc-ts.o
>  obj-$(CONFIG_TOUCHSCREEN_SUR40)		+= sur40.o
>  obj-$(CONFIG_TOUCHSCREEN_TI_AM335X_TSC)	+= ti_am335x_tsc.o
>  obj-$(CONFIG_TOUCHSCREEN_TOUCHIT213)	+= touchit213.o
> diff --git a/drivers/input/touchscreen/sun4i-ts.c b/drivers/input/touchscreen/sun4i-ts.c
> deleted file mode 100644
> index d07dd29..0000000
> --- a/drivers/input/touchscreen/sun4i-ts.c
> +++ /dev/null
> @@ -1,419 +0,0 @@
> -/*
> - * Allwinner sunxi resistive touchscreen controller driver
> - *
> - * Copyright (C) 2013 - 2014 Hans de Goede <hdegoede@redhat.com>
> - *
> - * The hwmon parts are based on work by Corentin LABBE which is:
> - * Copyright (C) 2013 Corentin LABBE <clabbe.montjoie@gmail.com>
> - *
> - * This program is free software; you can redistribute it and/or modify
> - * it under the terms of the GNU General Public License as published by
> - * the Free Software Foundation; either version 2 of the License, or
> - * (at your option) any later version.
> - *
> - * This program is distributed in the hope that it will be useful,
> - * but WITHOUT ANY WARRANTY; without even the implied warranty of
> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> - * GNU General Public License for more details.
> - */
> -
> -/*
> - * The sun4i-ts controller is capable of detecting a second touch, but when a
> - * second touch is present then the accuracy becomes so bad the reported touch
> - * location is not useable.
> - *
> - * The original android driver contains some complicated heuristics using the
> - * aprox. distance between the 2 touches to see if the user is making a pinch
> - * open / close movement, and then reports emulated multi-touch events around
> - * the last touch coordinate (as the dual-touch coordinates are worthless).
> - *
> - * These kinds of heuristics are just asking for trouble (and don't belong
> - * in the kernel). So this driver offers straight forward, reliable single
> - * touch functionality only.
> - *
> - * s.a. A20 User Manual "1.15 TP" (Documentation/arm/sunxi/README)
> - * (looks like the description in the A20 User Manual v1.3 is better
> - * than the one in the A10 User Manual v.1.5)
> - */
> -
> -#include <linux/err.h>
> -#include <linux/hwmon.h>
> -#include <linux/thermal.h>
> -#include <linux/init.h>
> -#include <linux/input.h>
> -#include <linux/interrupt.h>
> -#include <linux/io.h>
> -#include <linux/module.h>
> -#include <linux/of_platform.h>
> -#include <linux/platform_device.h>
> -#include <linux/slab.h>
> -
> -#define TP_CTRL0		0x00
> -#define TP_CTRL1		0x04
> -#define TP_CTRL2		0x08
> -#define TP_CTRL3		0x0c
> -#define TP_INT_FIFOC		0x10
> -#define TP_INT_FIFOS		0x14
> -#define TP_TPR			0x18
> -#define TP_CDAT			0x1c
> -#define TEMP_DATA		0x20
> -#define TP_DATA			0x24
> -
> -/* TP_CTRL0 bits */
> -#define ADC_FIRST_DLY(x)	((x) << 24) /* 8 bits */
> -#define ADC_FIRST_DLY_MODE(x)	((x) << 23)
> -#define ADC_CLK_SEL(x)		((x) << 22)
> -#define ADC_CLK_DIV(x)		((x) << 20) /* 3 bits */
> -#define FS_DIV(x)		((x) << 16) /* 4 bits */
> -#define T_ACQ(x)		((x) << 0) /* 16 bits */
> -
> -/* TP_CTRL1 bits */
> -#define STYLUS_UP_DEBOUN(x)	((x) << 12) /* 8 bits */
> -#define STYLUS_UP_DEBOUN_EN(x)	((x) << 9)
> -#define TOUCH_PAN_CALI_EN(x)	((x) << 6)
> -#define TP_DUAL_EN(x)		((x) << 5)
> -#define TP_MODE_EN(x)		((x) << 4)
> -#define TP_ADC_SELECT(x)	((x) << 3)
> -#define ADC_CHAN_SELECT(x)	((x) << 0)  /* 3 bits */
> -
> -/* on sun6i, bits 3~6 are left shifted by 1 to 4~7 */
> -#define SUN6I_TP_MODE_EN(x)	((x) << 5)
> -
> -/* TP_CTRL2 bits */
> -#define TP_SENSITIVE_ADJUST(x)	((x) << 28) /* 4 bits */
> -#define TP_MODE_SELECT(x)	((x) << 26) /* 2 bits */
> -#define PRE_MEA_EN(x)		((x) << 24)
> -#define PRE_MEA_THRE_CNT(x)	((x) << 0) /* 24 bits */
> -
> -/* TP_CTRL3 bits */
> -#define FILTER_EN(x)		((x) << 2)
> -#define FILTER_TYPE(x)		((x) << 0)  /* 2 bits */
> -
> -/* TP_INT_FIFOC irq and fifo mask / control bits */
> -#define TEMP_IRQ_EN(x)		((x) << 18)
> -#define OVERRUN_IRQ_EN(x)	((x) << 17)
> -#define DATA_IRQ_EN(x)		((x) << 16)
> -#define TP_DATA_XY_CHANGE(x)	((x) << 13)
> -#define FIFO_TRIG(x)		((x) << 8)  /* 5 bits */
> -#define DATA_DRQ_EN(x)		((x) << 7)
> -#define FIFO_FLUSH(x)		((x) << 4)
> -#define TP_UP_IRQ_EN(x)		((x) << 1)
> -#define TP_DOWN_IRQ_EN(x)	((x) << 0)
> -
> -/* TP_INT_FIFOS irq and fifo status bits */
> -#define TEMP_DATA_PENDING	BIT(18)
> -#define FIFO_OVERRUN_PENDING	BIT(17)
> -#define FIFO_DATA_PENDING	BIT(16)
> -#define TP_IDLE_FLG		BIT(2)
> -#define TP_UP_PENDING		BIT(1)
> -#define TP_DOWN_PENDING		BIT(0)
> -
> -/* TP_TPR bits */
> -#define TEMP_ENABLE(x)		((x) << 16)
> -#define TEMP_PERIOD(x)		((x) << 0)  /* t = x * 256 * 16 / clkin */
> -
> -struct sun4i_ts_data {
> -	struct device *dev;
> -	struct input_dev *input;
> -	void __iomem *base;
> -	unsigned int irq;
> -	bool ignore_fifo_data;
> -	int temp_data;
> -	int temp_offset;
> -	int temp_step;
> -};
> -
> -static void sun4i_ts_irq_handle_input(struct sun4i_ts_data *ts, u32 reg_val)
> -{
> -	u32 x, y;
> -
> -	if (reg_val & FIFO_DATA_PENDING) {
> -		x = readl(ts->base + TP_DATA);
> -		y = readl(ts->base + TP_DATA);
> -		/* The 1st location reported after an up event is unreliable */
> -		if (!ts->ignore_fifo_data) {
> -			input_report_abs(ts->input, ABS_X, x);
> -			input_report_abs(ts->input, ABS_Y, y);
> -			/*
> -			 * The hardware has a separate down status bit, but
> -			 * that gets set before we get the first location,
> -			 * resulting in reporting a click on the old location.
> -			 */
> -			input_report_key(ts->input, BTN_TOUCH, 1);
> -			input_sync(ts->input);
> -		} else {
> -			ts->ignore_fifo_data = false;
> -		}
> -	}
> -
> -	if (reg_val & TP_UP_PENDING) {
> -		ts->ignore_fifo_data = true;
> -		input_report_key(ts->input, BTN_TOUCH, 0);
> -		input_sync(ts->input);
> -	}
> -}
> -
> -static irqreturn_t sun4i_ts_irq(int irq, void *dev_id)
> -{
> -	struct sun4i_ts_data *ts = dev_id;
> -	u32 reg_val;
> -
> -	reg_val  = readl(ts->base + TP_INT_FIFOS);
> -
> -	if (reg_val & TEMP_DATA_PENDING)
> -		ts->temp_data = readl(ts->base + TEMP_DATA);
> -
> -	if (ts->input)
> -		sun4i_ts_irq_handle_input(ts, reg_val);
> -
> -	writel(reg_val, ts->base + TP_INT_FIFOS);
> -
> -	return IRQ_HANDLED;
> -}
> -
> -static int sun4i_ts_open(struct input_dev *dev)
> -{
> -	struct sun4i_ts_data *ts = input_get_drvdata(dev);
> -
> -	/* Flush, set trig level to 1, enable temp, data and up irqs */
> -	writel(TEMP_IRQ_EN(1) | DATA_IRQ_EN(1) | FIFO_TRIG(1) | FIFO_FLUSH(1) |
> -		TP_UP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
> -
> -	return 0;
> -}
> -
> -static void sun4i_ts_close(struct input_dev *dev)
> -{
> -	struct sun4i_ts_data *ts = input_get_drvdata(dev);
> -
> -	/* Deactivate all input IRQs */
> -	writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
> -}
> -
> -static int sun4i_get_temp(const struct sun4i_ts_data *ts, int *temp)
> -{
> -	/* No temp_data until the first irq */
> -	if (ts->temp_data == -1)
> -		return -EAGAIN;
> -
> -	*temp = ts->temp_data * ts->temp_step - ts->temp_offset;
> -
> -	return 0;
> -}
> -
> -static int sun4i_get_tz_temp(void *data, int *temp)
> -{
> -	return sun4i_get_temp(data, temp);
> -}
> -
> -static struct thermal_zone_of_device_ops sun4i_ts_tz_ops = {
> -	.get_temp = sun4i_get_tz_temp,
> -};
> -
> -static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
> -			 char *buf)
> -{
> -	struct sun4i_ts_data *ts = dev_get_drvdata(dev);
> -	int temp;
> -	int error;
> -
> -	error = sun4i_get_temp(ts, &temp);
> -	if (error)
> -		return error;
> -
> -	return sprintf(buf, "%d\n", temp);
> -}
> -
> -static ssize_t show_temp_label(struct device *dev,
> -			      struct device_attribute *devattr, char *buf)
> -{
> -	return sprintf(buf, "SoC temperature\n");
> -}
> -
> -static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
> -static DEVICE_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL);
> -
> -static struct attribute *sun4i_ts_attrs[] = {
> -	&dev_attr_temp1_input.attr,
> -	&dev_attr_temp1_label.attr,
> -	NULL
> -};
> -ATTRIBUTE_GROUPS(sun4i_ts);
> -
> -static int sun4i_ts_probe(struct platform_device *pdev)
> -{
> -	struct sun4i_ts_data *ts;
> -	struct device *dev = &pdev->dev;
> -	struct device_node *np = dev->of_node;
> -	struct device *hwmon;
> -	int error;
> -	u32 reg;
> -	bool ts_attached;
> -	u32 tp_sensitive_adjust = 15;
> -	u32 filter_type = 1;
> -
> -	ts = devm_kzalloc(dev, sizeof(struct sun4i_ts_data), GFP_KERNEL);
> -	if (!ts)
> -		return -ENOMEM;
> -
> -	ts->dev = dev;
> -	ts->ignore_fifo_data = true;
> -	ts->temp_data = -1;
> -	if (of_device_is_compatible(np, "allwinner,sun6i-a31-ts")) {
> -		/* Allwinner SDK has temperature (C) = (value / 6) - 271 */
> -		ts->temp_offset = 271000;
> -		ts->temp_step = 167;
> -	} else if (of_device_is_compatible(np, "allwinner,sun4i-a10-ts")) {
> -		/*
> -		 * The A10 temperature sensor has quite a wide spread, these
> -		 * parameters are based on the averaging of the calibration
> -		 * results of 4 completely different boards, with a spread of
> -		 * temp_step from 0.096 - 0.170 and temp_offset from 176 - 331.
> -		 */
> -		ts->temp_offset = 257000;
> -		ts->temp_step = 133;
> -	} else {
> -		/*
> -		 * The user manuals do not contain the formula for calculating
> -		 * the temperature. The formula used here is from the AXP209,
> -		 * which is designed by X-Powers, an affiliate of Allwinner:
> -		 *
> -		 *     temperature (C) = (value * 0.1) - 144.7
> -		 *
> -		 * Allwinner does not have any documentation whatsoever for
> -		 * this hardware. Moreover, it is claimed that the sensor
> -		 * is inaccurate and cannot work properly.
> -		 */
> -		ts->temp_offset = 144700;
> -		ts->temp_step = 100;
> -	}
> -
> -	ts_attached = of_property_read_bool(np, "allwinner,ts-attached");
> -	if (ts_attached) {
> -		ts->input = devm_input_allocate_device(dev);
> -		if (!ts->input)
> -			return -ENOMEM;
> -
> -		ts->input->name = pdev->name;
> -		ts->input->phys = "sun4i_ts/input0";
> -		ts->input->open = sun4i_ts_open;
> -		ts->input->close = sun4i_ts_close;
> -		ts->input->id.bustype = BUS_HOST;
> -		ts->input->id.vendor = 0x0001;
> -		ts->input->id.product = 0x0001;
> -		ts->input->id.version = 0x0100;
> -		ts->input->evbit[0] =  BIT(EV_SYN) | BIT(EV_KEY) | BIT(EV_ABS);
> -		__set_bit(BTN_TOUCH, ts->input->keybit);
> -		input_set_abs_params(ts->input, ABS_X, 0, 4095, 0, 0);
> -		input_set_abs_params(ts->input, ABS_Y, 0, 4095, 0, 0);
> -		input_set_drvdata(ts->input, ts);
> -	}
> -
> -	ts->base = devm_ioremap_resource(dev,
> -			      platform_get_resource(pdev, IORESOURCE_MEM, 0));
> -	if (IS_ERR(ts->base))
> -		return PTR_ERR(ts->base);
> -
> -	ts->irq = platform_get_irq(pdev, 0);
> -	error = devm_request_irq(dev, ts->irq, sun4i_ts_irq, 0, "sun4i-ts", ts);
> -	if (error)
> -		return error;
> -
> -	/*
> -	 * Select HOSC clk, clkin = clk / 6, adc samplefreq = clkin / 8192,
> -	 * t_acq = clkin / (16 * 64)
> -	 */
> -	writel(ADC_CLK_SEL(0) | ADC_CLK_DIV(2) | FS_DIV(7) | T_ACQ(63),
> -	       ts->base + TP_CTRL0);
> -
> -	/*
> -	 * tp_sensitive_adjust is an optional property
> -	 * tp_mode = 0 : only x and y coordinates, as we don't use dual touch
> -	 */
> -	of_property_read_u32(np, "allwinner,tp-sensitive-adjust",
> -			     &tp_sensitive_adjust);
> -	writel(TP_SENSITIVE_ADJUST(tp_sensitive_adjust) | TP_MODE_SELECT(0),
> -	       ts->base + TP_CTRL2);
> -
> -	/*
> -	 * Enable median and averaging filter, optional property for
> -	 * filter type.
> -	 */
> -	of_property_read_u32(np, "allwinner,filter-type", &filter_type);
> -	writel(FILTER_EN(1) | FILTER_TYPE(filter_type), ts->base + TP_CTRL3);
> -
> -	/* Enable temperature measurement, period 1953 (2 seconds) */
> -	writel(TEMP_ENABLE(1) | TEMP_PERIOD(1953), ts->base + TP_TPR);
> -
> -	/*
> -	 * Set stylus up debounce to aprox 10 ms, enable debounce, and
> -	 * finally enable tp mode.
> -	 */
> -	reg = STYLUS_UP_DEBOUN(5) | STYLUS_UP_DEBOUN_EN(1);
> -	if (of_device_is_compatible(np, "allwinner,sun6i-a31-ts"))
> -		reg |= SUN6I_TP_MODE_EN(1);
> -	else
> -		reg |= TP_MODE_EN(1);
> -	writel(reg, ts->base + TP_CTRL1);
> -
> -	/*
> -	 * The thermal core does not register hwmon devices for DT-based
> -	 * thermal zone sensors, such as this one.
> -	 */
> -	hwmon = devm_hwmon_device_register_with_groups(ts->dev, "sun4i_ts",
> -						       ts, sun4i_ts_groups);
> -	if (IS_ERR(hwmon))
> -		return PTR_ERR(hwmon);
> -
> -	devm_thermal_zone_of_sensor_register(ts->dev, 0, ts, &sun4i_ts_tz_ops);
> -
> -	writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
> -
> -	if (ts_attached) {
> -		error = input_register_device(ts->input);
> -		if (error) {
> -			writel(0, ts->base + TP_INT_FIFOC);
> -			return error;
> -		}
> -	}
> -
> -	platform_set_drvdata(pdev, ts);
> -	return 0;
> -}
> -
> -static int sun4i_ts_remove(struct platform_device *pdev)
> -{
> -	struct sun4i_ts_data *ts = platform_get_drvdata(pdev);
> -
> -	/* Explicit unregister to avoid open/close changing the imask later */
> -	if (ts->input)
> -		input_unregister_device(ts->input);
> -
> -	/* Deactivate all IRQs */
> -	writel(0, ts->base + TP_INT_FIFOC);
> -
> -	return 0;
> -}
> -
> -static const struct of_device_id sun4i_ts_of_match[] = {
> -	{ .compatible = "allwinner,sun4i-a10-ts", },
> -	{ .compatible = "allwinner,sun5i-a13-ts", },
> -	{ .compatible = "allwinner,sun6i-a31-ts", },
> -	{ /* sentinel */ }
> -};
> -MODULE_DEVICE_TABLE(of, sun4i_ts_of_match);
> -
> -static struct platform_driver sun4i_ts_driver = {
> -	.driver = {
> -		.name	= "sun4i-ts",
> -		.of_match_table = of_match_ptr(sun4i_ts_of_match),
> -	},
> -	.probe	= sun4i_ts_probe,
> -	.remove	= sun4i_ts_remove,
> -};
> -
> -module_platform_driver(sun4i_ts_driver);
> -
> -MODULE_DESCRIPTION("Allwinner sun4i resistive touchscreen controller driver");
> -MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
> -MODULE_LICENSE("GPL");
> diff --git a/drivers/input/touchscreen/sunxi-gpadc-ts.c b/drivers/input/touchscreen/sunxi-gpadc-ts.c
> new file mode 100644
> index 0000000..410d14f
> --- /dev/null
> +++ b/drivers/input/touchscreen/sunxi-gpadc-ts.c
> @@ -0,0 +1,195 @@
> +/* Touchscreen driver for Allwinner SoCs (A10, A13, A31) GPADC
> + *
> + * Copyright (c) 2016 Quentin Schulz <quentin.schulz@free-electrons>
> + *
> + * This program is free software; you can redistribute it and/or modify it under
> + * the terms of the GNU General Public License version 2 as published by the
> + * Free Software Foundation.
> + */
> +
> +#include <linux/init.h>
> +#include <linux/input.h>
> +#include <linux/interrupt.h>
> +#include <linux/module.h>
> +#include <linux/of.h>
> +#include <linux/platform_device.h>
> +#include <linux/regmap.h>
> +
> +#include <linux/iio/consumer.h>
> +#include <linux/mfd/sunxi-gpadc-mfd.h>
> +
> +struct sunxi_gpadc_ts {
> +	struct iio_cb_buffer	*buffer;
> +	struct input_dev	*input;
> +	struct platform_device	*pdev;
> +	unsigned int		tp_up_irq;
> +	bool			ignore_fifo_data;
> +};
> +
> +static int sunxi_gpadc_ts_open(struct input_dev *dev)
> +{
> +	struct sunxi_gpadc_ts *info = input_get_drvdata(dev);
> +	int ret;
> +
> +	ret = iio_channel_start_all_cb(info->buffer);
> +	if (ret) {
> +		dev_err(dev->dev.parent,
> +			"failed to start iio channels with callback\n");
> +		return ret;
> +	}
> +
> +	enable_irq(info->tp_up_irq);
> +
> +	return 0;
> +}
> +
> +static void sunxi_gpadc_ts_close(struct input_dev *dev)
> +{
> +	struct sunxi_gpadc_ts *info = input_get_drvdata(dev);
> +
> +	iio_channel_stop_all_cb(info->buffer);
> +
> +	disable_irq(info->tp_up_irq);
> +}
> +
> +/*
> + * This function will be called by iio_push_to_buffers from another driver
> + * (namely sunxi-gpadc-iio). It will be passed the buffer filled with input
> + * values (X value then Y value) and the sunxi_gpadc_ts structure representing
> + * the device.
> + */
> +static int sunxi_gpadc_ts_callback(const void *data, void *private)
> +{
> +	const struct sunxi_gpadc_buffer *buffer = data;
> +	struct sunxi_gpadc_ts *info = private;
> +	int i = 0;
> +
> +	/* Locations in the first buffer after an up event are unreliable */
> +	if (info->ignore_fifo_data) {
> +		info->ignore_fifo_data = false;
> +		return 0;
> +	}
> +
It doesn't work like this at all. You'll get one scan only on each call of
this function.  As I said in the previous driver, if there is a true reason
to do this (and I'm unconvinced as yet) then we need to add support in the
iio core etc for this (and emulating it when multiple scan passing isn't
happening).

I guess this will work, as you are passing the buffer size as a side
band, but it is definitely not how that ABI is meant to be used.

Also, you grab 4 channels, and only two are used here.  Please explain...

> +	while (i + 1 < buffer->buff_size) {
> +		input_event(info->input, EV_ABS, ABS_X, buffer->buffer[i++]);
> +		input_event(info->input, EV_ABS, ABS_Y, buffer->buffer[i++]);
> +		input_event(info->input, EV_KEY, BTN_TOUCH, 1);
> +		input_sync(info->input);
> +	}
> +
> +	return 0;
> +}
> +
> +static irqreturn_t sunxi_gpadc_tp_up_irq_handler(int irq, void *dev_id)
> +{
> +	struct sunxi_gpadc_ts *info = dev_id;
> +
> +	info->ignore_fifo_data = true;
> +
> +	input_event(info->input, EV_KEY, BTN_TOUCH, 0);
> +	input_sync(info->input);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static int sunxi_gpadc_ts_probe(struct platform_device *pdev)
> +{
> +	struct input_dev *input;
> +	struct sunxi_gpadc_ts *info;
> +	int ret, irq;
> +	struct sunxi_gpadc_mfd_dev *sunxi_gpadc_mfd_dev;
> +
> +	input = devm_input_allocate_device(&pdev->dev);
> +	if (!input)
> +		return -ENOMEM;
> +
> +	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
> +	if (!info)
> +		return -ENOMEM;
> +
> +	info->buffer = iio_channel_get_all_cb(&pdev->dev,
> +					      &sunxi_gpadc_ts_callback,
> +					      (void *)info);
> +	if (IS_ERR(info->buffer)) {
> +		if (PTR_ERR(info->buffer) == -ENODEV)
> +			return -EPROBE_DEFER;
> +		return PTR_ERR(info->buffer);
> +	}
> +
> +	info->pdev = pdev;
> +	info->input = input;
> +	info->ignore_fifo_data = false;
> +	platform_set_drvdata(pdev, info);
> +
> +	input->dev.parent = &pdev->dev;
> +	input->name = "sunxi-gpadc-ts";
> +	input->id.bustype = BUS_HOST;
> +	input->open = sunxi_gpadc_ts_open;
> +	input->close = sunxi_gpadc_ts_close;
> +	__set_bit(EV_SYN, input->evbit);
> +	input_set_capability(input, EV_KEY, BTN_TOUCH);
> +	input_set_abs_params(input, ABS_X, 0, 4095, 0, 0);
> +	input_set_abs_params(input, ABS_Y, 0, 4095, 0, 0);
> +	input_set_drvdata(input, info);
> +
> +	irq = platform_get_irq_byname(pdev, "TP_UP_PENDING");
> +	if (irq < 0) {
> +		dev_err(&pdev->dev, "no TP_UP_PENDING interrupt registered\n");
> +		ret = irq;
> +		goto err;
> +	}
> +
> +	sunxi_gpadc_mfd_dev = dev_get_drvdata(pdev->dev.parent);
> +
> +	irq = regmap_irq_get_virq(sunxi_gpadc_mfd_dev->regmap_irqc, irq);
> +	ret = devm_request_any_context_irq(&pdev->dev, irq,
> +					   sunxi_gpadc_tp_up_irq_handler, 0,
> +					   "tp_up", info);
> +	if (ret < 0) {
> +		dev_err(&pdev->dev,
> +			"could not request TP_UP_PENDING interrupt: %d\n", ret);
> +		goto err;
> +	}
> +
> +	info->tp_up_irq = irq;
> +	disable_irq(irq);
Any time I see an explicit irq disable in a driver, I want to know why...
Comment please as I'd expect the generation of the interrupt to only be
enabled when we want it, rather than squishing it at destination.



> +
> +	ret = input_register_device(input);
> +	if (ret) {
> +		dev_err(&pdev->dev, "failed to register input device\n");
> +		goto err;
> +	}
> +
> +	return 0;
> +
> +err:
> +	iio_channel_release_all_cb(info->buffer);
> +
> +	return ret;
> +}
> +
> +static int sunxi_gpadc_ts_remove(struct platform_device *pdev)
> +{
> +	struct sunxi_gpadc_ts *info = platform_get_drvdata(pdev);
> +
> +	iio_channel_stop_all_cb(info->buffer);
Necessary? I'd kind of expect the close to have occurred before
here.
> +	iio_channel_release_all_cb(info->buffer);
> +
> +	disable_irq(info->tp_up_irq);
> +
> +	return 0;
> +}
> +
> +static struct platform_driver sunxi_gpadc_ts_driver = {
> +	.driver = {
> +		.name = "sunxi-gpadc-ts",
> +	},
> +	.probe = sunxi_gpadc_ts_probe,
> +	.remove = sunxi_gpadc_ts_remove,
> +};
> +
> +module_platform_driver(sunxi_gpadc_ts_driver);
> +
> +MODULE_DESCRIPTION("Touchscreen driver for Allwinner SoCs (A10, A13, A31) GPADC");
> +MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
> +MODULE_LICENSE("GPL v2");
> 

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 5/5] mfd: sunxi-gpadc-mfd: probe sunxi-gpadc-ts driver
  2016-07-21  6:08   ` Maxime Ripard
@ 2016-07-24 11:26     ` Jonathan Cameron
  2016-07-25  9:51       ` Maxime Ripard
  2016-07-25 10:21       ` Lee Jones
  0 siblings, 2 replies; 31+ messages in thread
From: Jonathan Cameron @ 2016-07-24 11:26 UTC (permalink / raw)
  To: linux-arm-kernel

On 21/07/16 07:08, Maxime Ripard wrote:
> Hi Quentin,
> 
> On Wed, Jul 20, 2016 at 10:29:11AM +0200, Quentin Schulz wrote:
>> This probes the touchscreen driver for Allwinner SoCs (A10, A13 and A31)
>> when the property "allwinner,ts-attached" is set in the GPADC (rtp) node of
>> the DT.
>>
>> Some comestic modifications done to shorten and increase readability of the
>> code.
>>
>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>> ---
>>  drivers/mfd/sunxi-gpadc-mfd.c | 115 ++++++++++++++++++++++++++++--------------
>>  1 file changed, 77 insertions(+), 38 deletions(-)
>>
>> diff --git a/drivers/mfd/sunxi-gpadc-mfd.c b/drivers/mfd/sunxi-gpadc-mfd.c
>> index 05a000b..9b9ed0b 100644
>> --- a/drivers/mfd/sunxi-gpadc-mfd.c
>> +++ b/drivers/mfd/sunxi-gpadc-mfd.c
>> @@ -21,6 +21,11 @@
>>  #define SUNXI_IRQ_TEMP_DATA	1
>>  #define SUNXI_IRQ_TP_UP		2
>>  
>> +#define SUNXI_GPADC_MFD_CELL(_name, _resources, _num_resources) {	\
>> +	.name = _name,							\
>> +	.resources = _resources,					\
>> +	.num_resources = _num_resources					\
>> +}
>>  
>>  static struct resource adc_resources[] = {
>>  	{
>> @@ -64,33 +69,45 @@ static const struct regmap_irq_chip sunxi_gpadc_mfd_regmap_irq_chip = {
>>  };
>>  
>>  static struct mfd_cell sun4i_gpadc_mfd_cells[] = {
>> -	{
>> -		.name	= "sun4i-a10-gpadc-iio",
>> -		.resources = adc_resources,
>> -		.num_resources = ARRAY_SIZE(adc_resources),
>> -	}, {
>> -		.name = "iio_hwmon",
>> -	}
>> +	SUNXI_GPADC_MFD_CELL("sun4i-a10-gpadc-iio", adc_resources,
>> +			     ARRAY_SIZE(adc_resources)),
>> +	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
>>  };
>>  
>>  static struct mfd_cell sun5i_gpadc_mfd_cells[] = {
>> -	{
>> -		.name	= "sun5i-a13-gpadc-iio",
>> -		.resources = adc_resources,
>> -		.num_resources = ARRAY_SIZE(adc_resources),
>> -	}, {
>> -		.name = "iio_hwmon",
>> -	},
>> +	SUNXI_GPADC_MFD_CELL("sun5i-a13-gpadc-iio", adc_resources,
>> +			     ARRAY_SIZE(adc_resources)),
>> +	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
>>  };
>>  
>>  static struct mfd_cell sun6i_gpadc_mfd_cells[] = {
>> -	{
>> -		.name	= "sun6i-a31-gpadc-iio",
>> -		.resources = adc_resources,
>> -		.num_resources = ARRAY_SIZE(adc_resources),
>> -	}, {
>> -		.name = "iio_hwmon",
>> -	},
>> +	SUNXI_GPADC_MFD_CELL("sun6i-a31-gpadc-iio", adc_resources,
>> +			     ARRAY_SIZE(adc_resources)),
>> +	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
>> +};
> 
> This should be part of a separate patch.
> 
>> +
>> +static struct mfd_cell sun4i_gpadc_mfd_cells_ts[] = {
>> +	SUNXI_GPADC_MFD_CELL("sun6i-a31-gpadc-iio", adc_resources,
>> +			     ARRAY_SIZE(adc_resources)),
>> +	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
>> +	SUNXI_GPADC_MFD_CELL("sunxi-gpadc-ts", ts_resources,
>> +			     ARRAY_SIZE(ts_resources)),
>> +};
>> +
>> +static struct mfd_cell sun5i_gpadc_mfd_cells_ts[] = {
>> +	SUNXI_GPADC_MFD_CELL("sun5i-a13-gpadc-iio", adc_resources,
>> +			     ARRAY_SIZE(adc_resources)),
>> +	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
>> +	SUNXI_GPADC_MFD_CELL("sunxi-gpadc-ts", ts_resources,
>> +			     ARRAY_SIZE(ts_resources)),
>> +};
>> +
>> +static struct mfd_cell sun6i_gpadc_mfd_cells_ts[] = {
>> +	SUNXI_GPADC_MFD_CELL("sun6i-a31-gpadc-iio", adc_resources,
>> +			     ARRAY_SIZE(adc_resources)),
>> +	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
>> +	SUNXI_GPADC_MFD_CELL("sunxi-gpadc-ts", ts_resources,
>> +			     ARRAY_SIZE(ts_resources)),
>>  };
>>  
>>  static const struct regmap_config sunxi_gpadc_mfd_regmap_config = {
>> @@ -142,23 +159,45 @@ static int sunxi_gpadc_mfd_probe(struct platform_device *pdev)
>>  	}
>>  
>>  	if (of_device_is_compatible(pdev->dev.of_node,
>> -				    "allwinner,sun4i-a10-ts"))
>> -		ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
>> -				      sun4i_gpadc_mfd_cells,
>> -				      ARRAY_SIZE(sun4i_gpadc_mfd_cells), NULL,
>> -				      0, NULL);
>> -	else if (of_device_is_compatible(pdev->dev.of_node,
>> -					 "allwinner,sun5i-a13-ts"))
>> -		ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
>> -				      sun5i_gpadc_mfd_cells,
>> -				      ARRAY_SIZE(sun5i_gpadc_mfd_cells), NULL,
>> -				      0, NULL);
>> -	else if (of_device_is_compatible(pdev->dev.of_node,
>> -					 "allwinner,sun6i-a31-ts"))
>> -		ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
>> -				      sun6i_gpadc_mfd_cells,
>> -				      ARRAY_SIZE(sun6i_gpadc_mfd_cells), NULL,
>> -				      0, NULL);
>> +				    "allwinner,sun4i-a10-ts")) {
>> +		if (of_property_read_bool(pdev->dev.of_node,
>> +					  "allwinner,ts-attached"))
>> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
>> +					      sun4i_gpadc_mfd_cells_ts,
>> +					      ARRAY_SIZE(sun4i_gpadc_mfd_cells_ts),
>> +					      NULL, 0, NULL);
>> +		else
>> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
>> +					      sun4i_gpadc_mfd_cells,
>> +					      ARRAY_SIZE(sun4i_gpadc_mfd_cells),
>> +					      NULL, 0, NULL);
>> +	} else if (of_device_is_compatible(pdev->dev.of_node,
>> +					 "allwinner,sun5i-a13-ts")) {
>> +		if (of_property_read_bool(pdev->dev.of_node,
>> +					  "allwinner,ts-attached"))
>> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
>> +					      sun5i_gpadc_mfd_cells_ts,
>> +					      ARRAY_SIZE(sun5i_gpadc_mfd_cells_ts),
>> +					      NULL, 0, NULL);
>> +		else
>> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
>> +					      sun5i_gpadc_mfd_cells,
>> +					      ARRAY_SIZE(sun5i_gpadc_mfd_cells),
>> +					      NULL, 0, NULL);
>> +	} else if (of_device_is_compatible(pdev->dev.of_node,
>> +					 "allwinner,sun6i-a31-ts")) {
>> +		if (of_property_read_bool(pdev->dev.of_node,
>> +					  "allwinner,ts-attached"))
>> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
>> +					      sun6i_gpadc_mfd_cells_ts,
>> +					      ARRAY_SIZE(sun6i_gpadc_mfd_cells_ts),
>> +					      NULL, 0, NULL);
>> +		else
>> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
>> +					      sun6i_gpadc_mfd_cells,
>> +					      ARRAY_SIZE(sun6i_gpadc_mfd_cells),
>> +					      NULL, 0, NULL);
>> +	}
> 
> Please don't use any of_device_is_compatible.
Hi Maxime,

Why?  (Just curious...)

Jonathan
> 
> Thanks,
> Maxime
> 

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 4/5] input: touchscreen: support Allwinner SoCs' touchscreen
  2016-07-21  6:41     ` Dmitry Torokhov
@ 2016-07-25  9:45       ` maxime.ripard at free-electrons.com
  2016-07-25 17:08         ` Dmitry Torokhov
  0 siblings, 1 reply; 31+ messages in thread
From: maxime.ripard at free-electrons.com @ 2016-07-25  9:45 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Dmitry,

On Wed, Jul 20, 2016 at 11:41:40PM -0700, Dmitry Torokhov wrote:
> On Thu, Jul 21, 2016 at 08:29:50AM +0200, Maxime Ripard wrote:
> > On Wed, Jul 20, 2016 at 10:29:10AM +0200, Quentin Schulz wrote:
> > > +	irq = regmap_irq_get_virq(sunxi_gpadc_mfd_dev->regmap_irqc, irq);
> > > +	ret = devm_request_any_context_irq(&pdev->dev, irq,
> > > +					   sunxi_gpadc_tp_up_irq_handler, 0,
> > > +					   "tp_up", info);
> > > +	if (ret < 0) {
> > > +		dev_err(&pdev->dev,
> > > +			"could not request TP_UP_PENDING interrupt: %d\n", ret);
> > > +		goto err;
> > > +	}
> > 
> > You enable the interrupts...
> > 
> > > +	info->tp_up_irq = irq;
> > > +	disable_irq(irq);
> > > +
> > > +	ret = input_register_device(input);
> > > +	if (ret) {
> > > +		dev_err(&pdev->dev, "failed to register input device\n");
> > > +		goto err;
> > > +	}
> > 
> > ... but your driver isn't registered yet. How does input_report and
> > input_sync behave in such a case?
> 
> This is explicitly allowed:
> 
> "
> ...
>  * NOTE: input_event() may be safely used right after input device was
>  * allocated with input_allocate_device(), even before it is registered
>  * with input_register_device(), but the event will not reach any of the
>  * input handlers. Such early invocation of input_event() may be used
>  * to 'seed' initial state of a switch or initial position of absolute
>  * axis, etc.
>  */
> "

Good to know. Still, it feels like it should be handled explicitly,
instead of relying on the fact that we only call input_event in our
handler and that it works that way.

Thanks,
Maxime

-- 
Maxime Ripard, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20160725/12bb33b2/attachment.sig>

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 5/5] mfd: sunxi-gpadc-mfd: probe sunxi-gpadc-ts driver
  2016-07-24 11:26     ` Jonathan Cameron
@ 2016-07-25  9:51       ` Maxime Ripard
  2016-07-25 10:08         ` Jonathan Cameron
  2016-07-25 10:21       ` Lee Jones
  1 sibling, 1 reply; 31+ messages in thread
From: Maxime Ripard @ 2016-07-25  9:51 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Jonathan,

On Sun, Jul 24, 2016 at 12:26:56PM +0100, Jonathan Cameron wrote:
> >> @@ -142,23 +159,45 @@ static int sunxi_gpadc_mfd_probe(struct platform_device *pdev)
> >>  	}
> >>  
> >>  	if (of_device_is_compatible(pdev->dev.of_node,
> >> -				    "allwinner,sun4i-a10-ts"))
> >> -		ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> >> -				      sun4i_gpadc_mfd_cells,
> >> -				      ARRAY_SIZE(sun4i_gpadc_mfd_cells), NULL,
> >> -				      0, NULL);
> >> -	else if (of_device_is_compatible(pdev->dev.of_node,
> >> -					 "allwinner,sun5i-a13-ts"))
> >> -		ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> >> -				      sun5i_gpadc_mfd_cells,
> >> -				      ARRAY_SIZE(sun5i_gpadc_mfd_cells), NULL,
> >> -				      0, NULL);
> >> -	else if (of_device_is_compatible(pdev->dev.of_node,
> >> -					 "allwinner,sun6i-a31-ts"))
> >> -		ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> >> -				      sun6i_gpadc_mfd_cells,
> >> -				      ARRAY_SIZE(sun6i_gpadc_mfd_cells), NULL,
> >> -				      0, NULL);
> >> +				    "allwinner,sun4i-a10-ts")) {
> >> +		if (of_property_read_bool(pdev->dev.of_node,
> >> +					  "allwinner,ts-attached"))
> >> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> >> +					      sun4i_gpadc_mfd_cells_ts,
> >> +					      ARRAY_SIZE(sun4i_gpadc_mfd_cells_ts),
> >> +					      NULL, 0, NULL);
> >> +		else
> >> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> >> +					      sun4i_gpadc_mfd_cells,
> >> +					      ARRAY_SIZE(sun4i_gpadc_mfd_cells),
> >> +					      NULL, 0, NULL);
> >> +	} else if (of_device_is_compatible(pdev->dev.of_node,
> >> +					 "allwinner,sun5i-a13-ts")) {
> >> +		if (of_property_read_bool(pdev->dev.of_node,
> >> +					  "allwinner,ts-attached"))
> >> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> >> +					      sun5i_gpadc_mfd_cells_ts,
> >> +					      ARRAY_SIZE(sun5i_gpadc_mfd_cells_ts),
> >> +					      NULL, 0, NULL);
> >> +		else
> >> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> >> +					      sun5i_gpadc_mfd_cells,
> >> +					      ARRAY_SIZE(sun5i_gpadc_mfd_cells),
> >> +					      NULL, 0, NULL);
> >> +	} else if (of_device_is_compatible(pdev->dev.of_node,
> >> +					 "allwinner,sun6i-a31-ts")) {
> >> +		if (of_property_read_bool(pdev->dev.of_node,
> >> +					  "allwinner,ts-attached"))
> >> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> >> +					      sun6i_gpadc_mfd_cells_ts,
> >> +					      ARRAY_SIZE(sun6i_gpadc_mfd_cells_ts),
> >> +					      NULL, 0, NULL);
> >> +		else
> >> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> >> +					      sun6i_gpadc_mfd_cells,
> >> +					      ARRAY_SIZE(sun6i_gpadc_mfd_cells),
> >> +					      NULL, 0, NULL);
> >> +	}
> > 
> > Please don't use any of_device_is_compatible.
> Hi Maxime,
> 
> Why?  (Just curious...)

This is completely redundant. The compatible has already been looked
up once to match the driver, and you can associate a void * pointer to
any compatible you register in the of_device_id array.

So you can just retrieve the compatible that probed you in the first
place, and use it's private data pointer to store whatever you want,
without the numerous (and expensive) calls to of_device_is_compatible.

It's also easier to maintain in the long term, since you can simply
add a new field to the structure you would register there, instead of
keeping adding more conditions.

Maxime

-- 
Maxime Ripard, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20160725/6c7dc35d/attachment.sig>

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 5/5] mfd: sunxi-gpadc-mfd: probe sunxi-gpadc-ts driver
  2016-07-25  9:51       ` Maxime Ripard
@ 2016-07-25 10:08         ` Jonathan Cameron
  0 siblings, 0 replies; 31+ messages in thread
From: Jonathan Cameron @ 2016-07-25 10:08 UTC (permalink / raw)
  To: linux-arm-kernel



On 25 July 2016 10:51:13 BST, Maxime Ripard <maxime.ripard@free-electrons.com> wrote:
>Hi Jonathan,
>
>On Sun, Jul 24, 2016 at 12:26:56PM +0100, Jonathan Cameron wrote:
>> >> @@ -142,23 +159,45 @@ static int sunxi_gpadc_mfd_probe(struct
>platform_device *pdev)
>> >>  	}
>> >>  
>> >>  	if (of_device_is_compatible(pdev->dev.of_node,
>> >> -				    "allwinner,sun4i-a10-ts"))
>> >> -		ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
>> >> -				      sun4i_gpadc_mfd_cells,
>> >> -				      ARRAY_SIZE(sun4i_gpadc_mfd_cells), NULL,
>> >> -				      0, NULL);
>> >> -	else if (of_device_is_compatible(pdev->dev.of_node,
>> >> -					 "allwinner,sun5i-a13-ts"))
>> >> -		ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
>> >> -				      sun5i_gpadc_mfd_cells,
>> >> -				      ARRAY_SIZE(sun5i_gpadc_mfd_cells), NULL,
>> >> -				      0, NULL);
>> >> -	else if (of_device_is_compatible(pdev->dev.of_node,
>> >> -					 "allwinner,sun6i-a31-ts"))
>> >> -		ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
>> >> -				      sun6i_gpadc_mfd_cells,
>> >> -				      ARRAY_SIZE(sun6i_gpadc_mfd_cells), NULL,
>> >> -				      0, NULL);
>> >> +				    "allwinner,sun4i-a10-ts")) {
>> >> +		if (of_property_read_bool(pdev->dev.of_node,
>> >> +					  "allwinner,ts-attached"))
>> >> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
>> >> +					      sun4i_gpadc_mfd_cells_ts,
>> >> +					      ARRAY_SIZE(sun4i_gpadc_mfd_cells_ts),
>> >> +					      NULL, 0, NULL);
>> >> +		else
>> >> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
>> >> +					      sun4i_gpadc_mfd_cells,
>> >> +					      ARRAY_SIZE(sun4i_gpadc_mfd_cells),
>> >> +					      NULL, 0, NULL);
>> >> +	} else if (of_device_is_compatible(pdev->dev.of_node,
>> >> +					 "allwinner,sun5i-a13-ts")) {
>> >> +		if (of_property_read_bool(pdev->dev.of_node,
>> >> +					  "allwinner,ts-attached"))
>> >> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
>> >> +					      sun5i_gpadc_mfd_cells_ts,
>> >> +					      ARRAY_SIZE(sun5i_gpadc_mfd_cells_ts),
>> >> +					      NULL, 0, NULL);
>> >> +		else
>> >> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
>> >> +					      sun5i_gpadc_mfd_cells,
>> >> +					      ARRAY_SIZE(sun5i_gpadc_mfd_cells),
>> >> +					      NULL, 0, NULL);
>> >> +	} else if (of_device_is_compatible(pdev->dev.of_node,
>> >> +					 "allwinner,sun6i-a31-ts")) {
>> >> +		if (of_property_read_bool(pdev->dev.of_node,
>> >> +					  "allwinner,ts-attached"))
>> >> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
>> >> +					      sun6i_gpadc_mfd_cells_ts,
>> >> +					      ARRAY_SIZE(sun6i_gpadc_mfd_cells_ts),
>> >> +					      NULL, 0, NULL);
>> >> +		else
>> >> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
>> >> +					      sun6i_gpadc_mfd_cells,
>> >> +					      ARRAY_SIZE(sun6i_gpadc_mfd_cells),
>> >> +					      NULL, 0, NULL);
>> >> +	}
>> > 
>> > Please don't use any of_device_is_compatible.
>> Hi Maxime,
>> 
>> Why?  (Just curious...)
>
>This is completely redundant. The compatible has already been looked
>up once to match the driver, and you can associate a void * pointer to
>any compatible you register in the of_device_id array.
>
>So you can just retrieve the compatible that probed you in the first
>place, and use it's private data pointer to store whatever you want,
>without the numerous (and expensive) calls to of_device_is_compatible.
>
>It's also easier to maintain in the long term, since you can simply
>add a new field to the structure you would register there, instead of
>keeping adding more conditions.
Fair enough, now I get what you mean.

Thanks,

Jonathan
>
>Maxime

-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 5/5] mfd: sunxi-gpadc-mfd: probe sunxi-gpadc-ts driver
  2016-07-24 11:26     ` Jonathan Cameron
  2016-07-25  9:51       ` Maxime Ripard
@ 2016-07-25 10:21       ` Lee Jones
  1 sibling, 0 replies; 31+ messages in thread
From: Lee Jones @ 2016-07-25 10:21 UTC (permalink / raw)
  To: linux-arm-kernel

On Sun, 24 Jul 2016, Jonathan Cameron wrote:

> On 21/07/16 07:08, Maxime Ripard wrote:
> > Hi Quentin,
> > 
> > On Wed, Jul 20, 2016 at 10:29:11AM +0200, Quentin Schulz wrote:
> >> This probes the touchscreen driver for Allwinner SoCs (A10, A13 and A31)
> >> when the property "allwinner,ts-attached" is set in the GPADC (rtp) node of
> >> the DT.
> >>
> >> Some comestic modifications done to shorten and increase readability of the
> >> code.
> >>
> >> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> >> ---
> >>  drivers/mfd/sunxi-gpadc-mfd.c | 115 ++++++++++++++++++++++++++++--------------
> >>  1 file changed, 77 insertions(+), 38 deletions(-)
> >>
> >> diff --git a/drivers/mfd/sunxi-gpadc-mfd.c b/drivers/mfd/sunxi-gpadc-mfd.c
> >> index 05a000b..9b9ed0b 100644
> >> --- a/drivers/mfd/sunxi-gpadc-mfd.c
> >> +++ b/drivers/mfd/sunxi-gpadc-mfd.c
> >> @@ -21,6 +21,11 @@
> >>  #define SUNXI_IRQ_TEMP_DATA	1
> >>  #define SUNXI_IRQ_TP_UP		2
> >>  
> >> +#define SUNXI_GPADC_MFD_CELL(_name, _resources, _num_resources) {	\
> >> +	.name = _name,							\
> >> +	.resources = _resources,					\
> >> +	.num_resources = _num_resources					\
> >> +}
> >>  
> >>  static struct resource adc_resources[] = {
> >>  	{
> >> @@ -64,33 +69,45 @@ static const struct regmap_irq_chip sunxi_gpadc_mfd_regmap_irq_chip = {
> >>  };
> >>  
> >>  static struct mfd_cell sun4i_gpadc_mfd_cells[] = {
> >> -	{
> >> -		.name	= "sun4i-a10-gpadc-iio",
> >> -		.resources = adc_resources,
> >> -		.num_resources = ARRAY_SIZE(adc_resources),
> >> -	}, {
> >> -		.name = "iio_hwmon",
> >> -	}
> >> +	SUNXI_GPADC_MFD_CELL("sun4i-a10-gpadc-iio", adc_resources,
> >> +			     ARRAY_SIZE(adc_resources)),
> >> +	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
> >>  };
> >>  
> >>  static struct mfd_cell sun5i_gpadc_mfd_cells[] = {
> >> -	{
> >> -		.name	= "sun5i-a13-gpadc-iio",
> >> -		.resources = adc_resources,
> >> -		.num_resources = ARRAY_SIZE(adc_resources),
> >> -	}, {
> >> -		.name = "iio_hwmon",
> >> -	},
> >> +	SUNXI_GPADC_MFD_CELL("sun5i-a13-gpadc-iio", adc_resources,
> >> +			     ARRAY_SIZE(adc_resources)),
> >> +	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
> >>  };
> >>  
> >>  static struct mfd_cell sun6i_gpadc_mfd_cells[] = {
> >> -	{
> >> -		.name	= "sun6i-a31-gpadc-iio",
> >> -		.resources = adc_resources,
> >> -		.num_resources = ARRAY_SIZE(adc_resources),
> >> -	}, {
> >> -		.name = "iio_hwmon",
> >> -	},
> >> +	SUNXI_GPADC_MFD_CELL("sun6i-a31-gpadc-iio", adc_resources,
> >> +			     ARRAY_SIZE(adc_resources)),
> >> +	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
> >> +};
> > 
> > This should be part of a separate patch.
> > 
> >> +
> >> +static struct mfd_cell sun4i_gpadc_mfd_cells_ts[] = {
> >> +	SUNXI_GPADC_MFD_CELL("sun6i-a31-gpadc-iio", adc_resources,
> >> +			     ARRAY_SIZE(adc_resources)),
> >> +	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
> >> +	SUNXI_GPADC_MFD_CELL("sunxi-gpadc-ts", ts_resources,
> >> +			     ARRAY_SIZE(ts_resources)),
> >> +};
> >> +
> >> +static struct mfd_cell sun5i_gpadc_mfd_cells_ts[] = {
> >> +	SUNXI_GPADC_MFD_CELL("sun5i-a13-gpadc-iio", adc_resources,
> >> +			     ARRAY_SIZE(adc_resources)),
> >> +	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
> >> +	SUNXI_GPADC_MFD_CELL("sunxi-gpadc-ts", ts_resources,
> >> +			     ARRAY_SIZE(ts_resources)),
> >> +};
> >> +
> >> +static struct mfd_cell sun6i_gpadc_mfd_cells_ts[] = {
> >> +	SUNXI_GPADC_MFD_CELL("sun6i-a31-gpadc-iio", adc_resources,
> >> +			     ARRAY_SIZE(adc_resources)),
> >> +	SUNXI_GPADC_MFD_CELL("iio_hwmon", NULL, 0),
> >> +	SUNXI_GPADC_MFD_CELL("sunxi-gpadc-ts", ts_resources,
> >> +			     ARRAY_SIZE(ts_resources)),
> >>  };
> >>  
> >>  static const struct regmap_config sunxi_gpadc_mfd_regmap_config = {
> >> @@ -142,23 +159,45 @@ static int sunxi_gpadc_mfd_probe(struct platform_device *pdev)
> >>  	}
> >>  
> >>  	if (of_device_is_compatible(pdev->dev.of_node,
> >> -				    "allwinner,sun4i-a10-ts"))
> >> -		ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> >> -				      sun4i_gpadc_mfd_cells,
> >> -				      ARRAY_SIZE(sun4i_gpadc_mfd_cells), NULL,
> >> -				      0, NULL);
> >> -	else if (of_device_is_compatible(pdev->dev.of_node,
> >> -					 "allwinner,sun5i-a13-ts"))
> >> -		ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> >> -				      sun5i_gpadc_mfd_cells,
> >> -				      ARRAY_SIZE(sun5i_gpadc_mfd_cells), NULL,
> >> -				      0, NULL);
> >> -	else if (of_device_is_compatible(pdev->dev.of_node,
> >> -					 "allwinner,sun6i-a31-ts"))
> >> -		ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> >> -				      sun6i_gpadc_mfd_cells,
> >> -				      ARRAY_SIZE(sun6i_gpadc_mfd_cells), NULL,
> >> -				      0, NULL);
> >> +				    "allwinner,sun4i-a10-ts")) {
> >> +		if (of_property_read_bool(pdev->dev.of_node,
> >> +					  "allwinner,ts-attached"))
> >> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> >> +					      sun4i_gpadc_mfd_cells_ts,
> >> +					      ARRAY_SIZE(sun4i_gpadc_mfd_cells_ts),
> >> +					      NULL, 0, NULL);
> >> +		else
> >> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> >> +					      sun4i_gpadc_mfd_cells,
> >> +					      ARRAY_SIZE(sun4i_gpadc_mfd_cells),
> >> +					      NULL, 0, NULL);
> >> +	} else if (of_device_is_compatible(pdev->dev.of_node,
> >> +					 "allwinner,sun5i-a13-ts")) {
> >> +		if (of_property_read_bool(pdev->dev.of_node,
> >> +					  "allwinner,ts-attached"))
> >> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> >> +					      sun5i_gpadc_mfd_cells_ts,
> >> +					      ARRAY_SIZE(sun5i_gpadc_mfd_cells_ts),
> >> +					      NULL, 0, NULL);
> >> +		else
> >> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> >> +					      sun5i_gpadc_mfd_cells,
> >> +					      ARRAY_SIZE(sun5i_gpadc_mfd_cells),
> >> +					      NULL, 0, NULL);
> >> +	} else if (of_device_is_compatible(pdev->dev.of_node,
> >> +					 "allwinner,sun6i-a31-ts")) {
> >> +		if (of_property_read_bool(pdev->dev.of_node,
> >> +					  "allwinner,ts-attached"))
> >> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> >> +					      sun6i_gpadc_mfd_cells_ts,
> >> +					      ARRAY_SIZE(sun6i_gpadc_mfd_cells_ts),
> >> +					      NULL, 0, NULL);
> >> +		else
> >> +			ret = mfd_add_devices(sunxi_gpadc_mfd_dev->dev, 0,
> >> +					      sun6i_gpadc_mfd_cells,
> >> +					      ARRAY_SIZE(sun6i_gpadc_mfd_cells),
> >> +					      NULL, 0, NULL);
> >> +	}
> > 
> > Please don't use any of_device_is_compatible.
> Hi Maxime,
> 
> Why?  (Just curious...)

'cos they make for ugly code.

Use of_match_device() instead.

-- 
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org ? Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 4/5] input: touchscreen: support Allwinner SoCs' touchscreen
  2016-07-25  9:45       ` maxime.ripard at free-electrons.com
@ 2016-07-25 17:08         ` Dmitry Torokhov
  2016-07-26 15:13           ` Maxime Ripard
  0 siblings, 1 reply; 31+ messages in thread
From: Dmitry Torokhov @ 2016-07-25 17:08 UTC (permalink / raw)
  To: linux-arm-kernel

On Mon, Jul 25, 2016 at 11:45:42AM +0200, maxime.ripard at free-electrons.com wrote:
> Hi Dmitry,
> 
> On Wed, Jul 20, 2016 at 11:41:40PM -0700, Dmitry Torokhov wrote:
> > On Thu, Jul 21, 2016 at 08:29:50AM +0200, Maxime Ripard wrote:
> > > On Wed, Jul 20, 2016 at 10:29:10AM +0200, Quentin Schulz wrote:
> > > > +	irq = regmap_irq_get_virq(sunxi_gpadc_mfd_dev->regmap_irqc, irq);
> > > > +	ret = devm_request_any_context_irq(&pdev->dev, irq,
> > > > +					   sunxi_gpadc_tp_up_irq_handler, 0,
> > > > +					   "tp_up", info);
> > > > +	if (ret < 0) {
> > > > +		dev_err(&pdev->dev,
> > > > +			"could not request TP_UP_PENDING interrupt: %d\n", ret);
> > > > +		goto err;
> > > > +	}
> > > 
> > > You enable the interrupts...
> > > 
> > > > +	info->tp_up_irq = irq;
> > > > +	disable_irq(irq);
> > > > +
> > > > +	ret = input_register_device(input);
> > > > +	if (ret) {
> > > > +		dev_err(&pdev->dev, "failed to register input device\n");
> > > > +		goto err;
> > > > +	}
> > > 
> > > ... but your driver isn't registered yet. How does input_report and
> > > input_sync behave in such a case?
> > 
> > This is explicitly allowed:
> > 
> > "
> > ...
> >  * NOTE: input_event() may be safely used right after input device was
> >  * allocated with input_allocate_device(), even before it is registered
> >  * with input_register_device(), but the event will not reach any of the
> >  * input handlers. Such early invocation of input_event() may be used
> >  * to 'seed' initial state of a switch or initial position of absolute
> >  * axis, etc.
> >  */
> > "
> 
> Good to know. Still, it feels like it should be handled explicitly,
> instead of relying on the fact that we only call input_event in our
> handler and that it works that way.

Why? It is the documented property of input API and it is done so
precisely so that you can register interrupt before registering input
device. That is done because once registered input device is supposed to
be fully-functional.  It does not matter for this driver but there are
drivers out there that require interrupt handling as part of their
"open" method.

Thanks.

-- 
Dmitry

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 4/5] input: touchscreen: support Allwinner SoCs' touchscreen
  2016-07-25 17:08         ` Dmitry Torokhov
@ 2016-07-26 15:13           ` Maxime Ripard
  0 siblings, 0 replies; 31+ messages in thread
From: Maxime Ripard @ 2016-07-26 15:13 UTC (permalink / raw)
  To: linux-arm-kernel

Hi,

On Mon, Jul 25, 2016 at 10:08:39AM -0700, Dmitry Torokhov wrote:
> > > > ... but your driver isn't registered yet. How does input_report and
> > > > input_sync behave in such a case?
> > > 
> > > This is explicitly allowed:
> > > 
> > > "
> > > ...
> > >  * NOTE: input_event() may be safely used right after input device was
> > >  * allocated with input_allocate_device(), even before it is registered
> > >  * with input_register_device(), but the event will not reach any of the
> > >  * input handlers. Such early invocation of input_event() may be used
> > >  * to 'seed' initial state of a switch or initial position of absolute
> > >  * axis, etc.
> > >  */
> > > "
> > 
> > Good to know. Still, it feels like it should be handled explicitly,
> > instead of relying on the fact that we only call input_event in our
> > handler and that it works that way.
> 
> Why? It is the documented property of input API and it is done so
> precisely so that you can register interrupt before registering input
> device. That is done because once registered input device is supposed to
> be fully-functional.  It does not matter for this driver but there are
> drivers out there that require interrupt handling as part of their
> "open" method.

I'm not questionning the input framework itself, and it works right
now because we all looked at that issue and found that it would work
even in the event of an early interrupt.

What I'm afraid of is that in a few weeks /monthes / years / decades,
someone will add something new in the driver without taking care of
that, and we will miss it because we won't have enough context.

Maxime

-- 
Maxime Ripard, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20160726/e9555d8b/attachment.sig>

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 3/5] iio: adc: sunxi-gpadc-iio: enable iio_buffers
  2016-07-24 11:03   ` Jonathan Cameron
@ 2016-09-24 17:40     ` Quentin Schulz
  2016-09-25  9:10       ` Jonathan Cameron
  0 siblings, 1 reply; 31+ messages in thread
From: Quentin Schulz @ 2016-09-24 17:40 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Jonathan,

Sorry for the (long) delay, I did not have time to work on it. I'll
mainly work in my free time now.

Keep in mind this patch was proposed based on the v2 of the ADC patches.
Since then, substantial changes have been made and I'm working on
rebasing this series of patches on the v6, so comments here might
include references to code parts added later in the ADC patch series.

On 24/07/2016 13:03, Jonathan Cameron wrote:
> On 20/07/16 09:29, Quentin Schulz wrote:
>> This enables the use of buffers on ADC channels of sunxi-gpadc-iio driver.
>> It also prepares the code which will be used by the touchscreen driver
>> named sunxi-gpadc-ts.
>>
>> The GPADC on Allwinner SoCs (A10, A13 and A31) has a 12 bits register for
>> conversion's data. The GPADC uses the same ADC channels for the ADC and the
>> touchscreen therefore exposes these channels to the sunxi-gpadc-ts iio
>> consumer which will be in charge of reading data from these channels for
>> the input framework.
>>
>> The temperature can only be read when in touchscreen mode. This means if
>> the buffers are being used for the ADC, the temperature sensor cannot be
>> read.
> That may be the bizarest hardware restriction I've heard of in a while! :)
>>
>> When a FIFO_DATA_PENDING irq occurs, its handler will read the entire FIFO
>> and fill a buffer before sending it to the consumers which registered in
>> IIO for the ADC channels.
>>
>> When a consumer starts buffering ADC channels,
>> sunxi_gpadc_buffer_postenable is called and will enable FIFO_DATA_PENDING
>> irq and select the mode in which the GPADC should run (ADC or touchscreen)
>> depending on a property of the DT ("allwinner,ts-attached").
>> When the consumer stops buffering, it disables the same irq.
> Hmm. Might be possible to distinguish which consumer caused the start.
> Thus, if the touchscreen is there we would know purely based on the
> driver being the requester that we need to be in touchscreen mode.
> 

As of yet, can't see in which way I can retrieve the consumer in
provider code. Maybe I'm missing something, I don't know?

>>
>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> You are moving fast on this - I'd have been tempted to do a mega
> series with the updated version of the basic support and this on top
> rather than a new unconnected series.
> 
> (I'd forgotten that was still under review so got confused when I
> went to look something up in the files you are modifying!).
>> ---
>>  drivers/iio/adc/Kconfig           |   1 +
>>  drivers/iio/adc/sunxi-gpadc-iio.c | 153 ++++++++++++++++++++++++++++++++++----
>>  2 files changed, 138 insertions(+), 16 deletions(-)
>>
>> diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
>> index 184856f..15e3b08 100644
>> --- a/drivers/iio/adc/Kconfig
>> +++ b/drivers/iio/adc/Kconfig
>> @@ -342,6 +342,7 @@ config SUNXI_ADC
>>  	tristate "ADC driver for sunxi platforms"
>>  	depends on IIO
>>  	depends on MFD_SUNXI_ADC
>> +	depends on IIO_BUFFER_CB
>>  	help
>>  	  Say yes here to build support for Allwinner (A10, A13 and A31) SoCs
>>  	  ADC. This ADC provides 4 channels which can be used as an ADC or as a
>> diff --git a/drivers/iio/adc/sunxi-gpadc-iio.c b/drivers/iio/adc/sunxi-gpadc-iio.c
>> index 87cc913..2e44ca7 100644
>> --- a/drivers/iio/adc/sunxi-gpadc-iio.c
>> +++ b/drivers/iio/adc/sunxi-gpadc-iio.c
>> @@ -16,8 +16,9 @@
>>  #include <linux/platform_device.h>
>>  #include <linux/regmap.h>
>>  
>> -#include <linux/iio/iio.h>
>> +#include <linux/iio/buffer.h>
>>  #include <linux/iio/driver.h>
>> +#include <linux/iio/iio.h>
> Can't say I'm a particular fan of reordering headers to be in alphabetical
> order, but I suppose it doesn't really matter if you want to do it.
> (to my mind there is a tree structure implicit in these headers with iio.h
> at the top for generic support, then the various sub elements below).
> 
>>  #include <linux/iio/machine.h>
>>  #include <linux/mfd/sunxi-gpadc-mfd.h>
>>  
>> @@ -71,6 +72,7 @@
>>  #define SUNXI_GPADC_TP_DATA_XY_CHANGE		BIT(13)
>>  #define SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(x)	((x) << 8)  /* 5 bits */
>>  #define SUNXI_GPADC_TP_DATA_DRQ_EN		BIT(7)
>> +/* Be careful, flushing FIFO spawns SUNXI_GPADC_FIFO_DATA_PENDING interrupts */
> Sounds like you learned that one the hard way ;)
>>  #define SUNXI_GPADC_TP_FIFO_FLUSH		BIT(4)
>>  #define SUNXI_GPADC_TP_UP_IRQ_EN		BIT(1)
>>  #define SUNXI_GPADC_TP_DOWN_IRQ_EN		BIT(0)
>> @@ -79,6 +81,7 @@
>>  #define SUNXI_GPADC_TEMP_DATA_PENDING		BIT(18)
>>  #define SUNXI_GPADC_FIFO_OVERRUN_PENDING	BIT(17)
>>  #define SUNXI_GPADC_FIFO_DATA_PENDING		BIT(16)
>> +#define SUNXI_GPADC_RXA_CNT			GENMASK(12, 8)
>>  #define SUNXI_GPADC_TP_IDLE_FLG			BIT(2)
>>  #define SUNXI_GPADC_TP_UP_PENDING		BIT(1)
>>  #define SUNXI_GPADC_TP_DOWN_PENDING		BIT(0)
>> @@ -101,19 +104,43 @@ struct sunxi_gpadc_dev {
>>  	unsigned int			fifo_data_irq;
>>  	unsigned int			temp_data_irq;
>>  	unsigned int			flags;
>> +	struct iio_dev			*indio_dev;
> I was suprised to see this as normally it is cleaner to structure
> the whole code to go in one direction through the structures (which is
> why we don't provide a generic iio_device_from_priv bit of pointer magic).
> 
> Anyhow, don't htink you are actually using it ;)
> 

I'm using to push to buffers from the irq handler since I pass the local
structure (sunxi_gpadc_dev) to the irq handler when registering it. But
I guess I can pass the iio_dev instead and remove this from the local
structure.

[...]
>>  static int sunxi_gpadc_adc_read(struct iio_dev *indio_dev, int channel,
>>  				int *val)
>>  {
>>  	struct sunxi_gpadc_dev *info = iio_priv(indio_dev);
>> +	bool buffered = info->buffered;
> Not worth the local version...
>>  	int ret = 0;
>> +	unsigned int reg;
>>  
>>  	mutex_lock(&indio_dev->mlock);
>>  
>>  	reinit_completion(&info->completion);
>> +
>> +	reg = SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(1) | SUNXI_GPADC_TP_FIFO_FLUSH;
>> +	regmap_update_bits(info->regmap, SUNXI_GPADC_TP_INT_FIFOC, reg, reg);
> I'd put it in directly rahter than having a reg local variable.  To mind
> mind that would be slightly easier to understand.
>> +
>>  	if (info->flags & SUNXI_GPADC_ARCH_SUN6I)
>>  		regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL1,
>>  			     SUNXI_GPADC_SUN6I_TP_MODE_EN |
>> @@ -153,9 +185,9 @@ static int sunxi_gpadc_adc_read(struct iio_dev *indio_dev, int channel,
>>  			     SUNXI_GPADC_TP_MODE_EN |
>>  			     SUNXI_GPADC_TP_ADC_SELECT |
>>  			     SUNXI_GPADC_ADC_CHAN_SELECT(channel));
>> -	regmap_write(info->regmap, SUNXI_GPADC_TP_INT_FIFOC,
>> -		     SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(1) |
>> -		     SUNXI_GPADC_TP_FIFO_FLUSH);
> Whole load of infrastructure in place to lock buffered mode out and
> revent transitions when we can't have them.
> 
> iio_claim_direct_mode etc.  I think you can just use that here?
> If you need to do extra checks on it being enabled that should be
> fine too.
> 

Yes, way better with iio_device_claim_direct_mode and iio_buffer_enabled!

> As a general rule, it makes sense to simply disable polled reads
> if in buffered mode.  Leads to much simpler code and generally
> the data is already known to userspace anyway.
> 

That's what I try to do.
However, I think the temperature of the SoC is an interesting feature to
have. Since it ("hardwarely") works while the ADC is read in touchscreen
mode (even in buffer mode), I guess it could be a good idea to allow it
in the driver. If we don't do that, boards with a touchscreen connected
to the ADC of the SoC will not get SoC temperatures and can't have
proper thermal management. We already have one board in that case: the
PocketCHIP.

Therefore, I also need to know if when the buffer is enabled, if it's
for buffering ADC data or touchscreen data. If it's for ADC data, then I
should disable temperature readings since it will return senseless
values (from memory, always 0 which means something like -144?C).

> I have been meaning to do it a bit better when we have multiple
> in kernel consumers, some expecting polled readings and some
> pushed.  There some core caching magic will make sense to
> keep the polled channels as available as possible when running
> the  buffers.
> 
> A bit fiddly to implement + might have some slightly suprising
> results on delays on channels when say a sysfs trigger is
> being used... (not a problem here as you have a fifo and hence
> aren't using triggers).
> 
> Anyhow, not really relevant here :)
> 
[...]
>>  	case IIO_CHAN_INFO_RAW:
> Definitely use the iio_claim_direct_mode stuff here to avoid possible races
> with the buffer being enabled whilst this read is in flight.

Indeed.

>> +		if (info->buffered)
>> +			return -EBUSY;
>> +
>>  		ret = sunxi_gpadc_adc_read(indio_dev, chan->channel, val);
>>  		if (ret)
>>  			return ret;
>> @@ -261,7 +302,29 @@ static irqreturn_t sunxi_gpadc_temp_data_irq_handler(int irq, void *dev_id)
>>  static irqreturn_t sunxi_gpadc_fifo_data_irq_handler(int irq, void *dev_id)
>>  {
>>  	struct sunxi_gpadc_dev *info = dev_id;
>> -	int ret;
>> +	int ret, reg, i, fifo_count;
>> +
>> +	if (info->buffered) {
>> +		if (regmap_read(info->regmap, SUNXI_GPADC_TP_INT_FIFOS, &reg))
>> +			return IRQ_HANDLED;
>> +
>> +		fifo_count = (reg & SUNXI_GPADC_RXA_CNT) >> 8;
>> +		/* Sometimes, the interrupt occurs when the FIFO is empty. */
>> +		if (!fifo_count)
>> +			return IRQ_HANDLED;
>> +
>> +		for (i = 0; i < fifo_count; i++) {
>> +			if (regmap_read(info->regmap, SUNXI_GPADC_TP_DATA,
>> +					&info->buffer.buffer[i]))
>> +				return IRQ_HANDLED;
>> +		}
>> +
>> +		info->buffer.buff_size = i;
>> +
>> +		iio_push_to_buffers(info->indio_dev, &info->buffer);
> This is expecting a single 'scan' - e.g. set of channels read at one
> time.  Here I think we could have repeated sets of channels?
> (at least that would be what is normally meant by a fifo in such
> a device).
> 
> If so you need to read 'whole' scans and push them one at a time.
> We don't yet have a bulk iio_push_to_buffers, though we can add
> one if it makes sense.  Care will be needed though as we'd need
> handle the case of different consumers either supporting or
> not supporting this new functionality.  Not particularly hard though
> if it is worth doing.

I didn't know it was meant for only one scan. Then I need a bulk
iio_push_to_buffers.

I have a rather big problem. The whole first FIFO at each touch is
unusable so I have to drop it. I can detect the beginning of a touch
when the TP_UP irq occurs, then I know the next full FIFO the consumer
receives by callback is to be dropped. If I use push_to_buffers to send
coordinates by coordinates, the consumer has no mean to know when the
second FIFO (the first to be valid) starts and can be used. Either we
can find a way to notify the consumer of the start of a new FIFO or I
have to use a bulk iio_push_to_buffers.

The workaround would be to register the TP_UP irq in the provider (the
ADC driver) and do not send the first FIFO to the consumer. But then, we
need a way to know which consumer requests buffering to know when to
enable this irq and do all touchscreen-only logic (dropping first
frame). And I guess we don't have something like that yet. Or I could
only code a buffering in touchscreen mode and add the ADC buffering
later? But it doesn't feel right to do what I think should be handled
(TP_UP irq handler and first FIFO dropping) in the consumer, in the
provider.

So it's quiet a dead-end yet if I can't use iio_push_to_buffers with a
whole FIFO (which you told is not how it is meant to be used).

[...]
Thanks,

Quentin

-- 
Quentin Schulz, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 4/5] input: touchscreen: support Allwinner SoCs' touchscreen
  2016-07-20 17:25   ` Dmitry Torokhov
  2016-07-20 20:13     ` Jonathan Cameron
@ 2016-09-24 18:26     ` Quentin Schulz
  2016-09-24 18:39       ` Dmitry Torokhov
  1 sibling, 1 reply; 31+ messages in thread
From: Quentin Schulz @ 2016-09-24 18:26 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Dimitry,

Sorry for the (long) delay, I did not have time to work on it. I'll
mainly work in my free time now.

On 20/07/2016 19:25, Dmitry Torokhov wrote:
> Hi Quentin,
> 
> On Wed, Jul 20, 2016 at 10:29:10AM +0200, Quentin Schulz wrote:
>> This adds support for Allwinner SoCs' (A10, A13 and A31) resistive
>> touchscreen. This driver is probed by the MFD sunxi-gpadc-mfd.
>>
>> This driver uses ADC channels exposed through the IIO framework by
>> sunxi-gpadc-iio to get its data. When opening this input device, it will
>> start buffering in the ADC driver and enable a TP_UP_PENDING irq. The ADC
>> driver will fill in a buffer with all data and call the callback the input
>> device associated with this buffer. The input device will then read the
>> buffer two by two and send X and Y coordinates to the input framework based
>> on what it received from the ADC's buffer. When closing this input device,
>> the buffering is stopped.
>>
>> Note that locations in the first received buffer after an TP_UP_PENDING irq
>> occurred are unreliable, thus dropped.
>>
>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>> ---
[...]
>> +	info->buffer = iio_channel_get_all_cb(&pdev->dev,
>> +					      &sunxi_gpadc_ts_callback,
>> +					      (void *)info);
> 
> Any chance we could introduce devm-variant here? If you do not want to
> wait for IIO to add it you can temporarily add call
> devm_add_action_or_reset() after getting channels and remove it when IIO
> API catches up.
> 

Something like:

release_iio_channels(void* data)
{
	struct sunxi_gpadc_ts *info = data;
	iio_channel_release_all_cb(info->buffer);
}

[...]
	info->buffer = iio_channel_get_all_cb(&pdev->dev,
					      &sunxi_gpadc_ts_callback,
					      (void *)info);
	ret = devm_add_action_or_reset(&pdev->dev,
				       release_iio_channels,
				       (void *)info);
	if (ret)
		return ret;

?

May I know why you prefer that way instead of explicit removing in
remove function of the platform device? I understand for devm-variant
already in the framework but I am curious for this one.

[...]
>> +static int sunxi_gpadc_ts_remove(struct platform_device *pdev)
>> +{
>> +	struct sunxi_gpadc_ts *info = platform_get_drvdata(pdev);
>> +
>> +	iio_channel_stop_all_cb(info->buffer);
>> +	iio_channel_release_all_cb(info->buffer);
>> +
>> +	disable_irq(info->tp_up_irq);
> 
> You are mixing devm and non-devm so your unwind order is completely out
> of wack. If input device is opened while you are unloading (or
> unbinding) the dirver, then you'll release channels, then input device's
> close() will be called, which will try to stop the IIO channels again
> and disable IRQ yet again.
> 

Do you mean that I should be using exclusively devm or non-devm functions?
Do you mean input device's close will always be called when
unloading/unbinding the driver?

[...]
Thanks,
Quentin

-- 
Quentin Schulz, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 4/5] input: touchscreen: support Allwinner SoCs' touchscreen
  2016-09-24 18:26     ` Quentin Schulz
@ 2016-09-24 18:39       ` Dmitry Torokhov
  0 siblings, 0 replies; 31+ messages in thread
From: Dmitry Torokhov @ 2016-09-24 18:39 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Quentin,

On Sat, Sep 24, 2016 at 08:26:08PM +0200, Quentin Schulz wrote:
> Hi Dimitry,
> 
> Sorry for the (long) delay, I did not have time to work on it. I'll
> mainly work in my free time now.
> 
> On 20/07/2016 19:25, Dmitry Torokhov wrote:
> > Hi Quentin,
> > 
> > On Wed, Jul 20, 2016 at 10:29:10AM +0200, Quentin Schulz wrote:
> >> This adds support for Allwinner SoCs' (A10, A13 and A31) resistive
> >> touchscreen. This driver is probed by the MFD sunxi-gpadc-mfd.
> >>
> >> This driver uses ADC channels exposed through the IIO framework by
> >> sunxi-gpadc-iio to get its data. When opening this input device, it will
> >> start buffering in the ADC driver and enable a TP_UP_PENDING irq. The ADC
> >> driver will fill in a buffer with all data and call the callback the input
> >> device associated with this buffer. The input device will then read the
> >> buffer two by two and send X and Y coordinates to the input framework based
> >> on what it received from the ADC's buffer. When closing this input device,
> >> the buffering is stopped.
> >>
> >> Note that locations in the first received buffer after an TP_UP_PENDING irq
> >> occurred are unreliable, thus dropped.
> >>
> >> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
> >> ---
> [...]
> >> +	info->buffer = iio_channel_get_all_cb(&pdev->dev,
> >> +					      &sunxi_gpadc_ts_callback,
> >> +					      (void *)info);
> > 
> > Any chance we could introduce devm-variant here? If you do not want to
> > wait for IIO to add it you can temporarily add call
> > devm_add_action_or_reset() after getting channels and remove it when IIO
> > API catches up.
> > 
> 
> Something like:
> 
> release_iio_channels(void* data)
> {
> 	struct sunxi_gpadc_ts *info = data;
> 	iio_channel_release_all_cb(info->buffer);
> }
> 
> [...]
> 	info->buffer = iio_channel_get_all_cb(&pdev->dev,
> 					      &sunxi_gpadc_ts_callback,
> 					      (void *)info);
> 	ret = devm_add_action_or_reset(&pdev->dev,
> 				       release_iio_channels,
> 				       (void *)info);
> 	if (ret)
> 		return ret;
> 
> ?
> 
> May I know why you prefer that way instead of explicit removing in
> remove function of the platform device? I understand for devm-variant
> already in the framework but I am curious for this one.

So that you release all resources in the same order they were allocated.
When mixing devm and non-devm allocation/release order is often
incorrect.

> 
> [...]
> >> +static int sunxi_gpadc_ts_remove(struct platform_device *pdev)
> >> +{
> >> +	struct sunxi_gpadc_ts *info = platform_get_drvdata(pdev);
> >> +
> >> +	iio_channel_stop_all_cb(info->buffer);
> >> +	iio_channel_release_all_cb(info->buffer);
> >> +
> >> +	disable_irq(info->tp_up_irq);
> > 
> > You are mixing devm and non-devm so your unwind order is completely out
> > of wack. If input device is opened while you are unloading (or
> > unbinding) the dirver, then you'll release channels, then input device's
> > close() will be called, which will try to stop the IIO channels again
> > and disable IRQ yet again.
> > 
> 
> Do you mean that I should be using exclusively devm or non-devm functions?

Yes. Sometimes you can get away with mixing style (you have all devm
resources allocated first, then non-devm), but it is much clearer and
safer if you use one style or another exclusively.

> Do you mean input device's close will always be called when
> unloading/unbinding the driver?

If ->open() has been called() then input core will ensure that ->close()
is called as part of input_unregister_device(). If ->open() has not been
called, then ->close() will not be called either.

Thanks.

-- 
Dmitry

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 3/5] iio: adc: sunxi-gpadc-iio: enable iio_buffers
  2016-09-24 17:40     ` Quentin Schulz
@ 2016-09-25  9:10       ` Jonathan Cameron
  2016-09-25 19:57         ` Quentin Schulz
  0 siblings, 1 reply; 31+ messages in thread
From: Jonathan Cameron @ 2016-09-25  9:10 UTC (permalink / raw)
  To: linux-arm-kernel

On 24/09/16 18:40, Quentin Schulz wrote:
> Hi Jonathan,
> 
> Sorry for the (long) delay, I did not have time to work on it. I'll
> mainly work in my free time now.
> 
> Keep in mind this patch was proposed based on the v2 of the ADC patches.
> Since then, substantial changes have been made and I'm working on
> rebasing this series of patches on the v6, so comments here might
> include references to code parts added later in the ADC patch series.
> 
> On 24/07/2016 13:03, Jonathan Cameron wrote:
>> On 20/07/16 09:29, Quentin Schulz wrote:
>>> This enables the use of buffers on ADC channels of sunxi-gpadc-iio driver.
>>> It also prepares the code which will be used by the touchscreen driver
>>> named sunxi-gpadc-ts.
>>>
>>> The GPADC on Allwinner SoCs (A10, A13 and A31) has a 12 bits register for
>>> conversion's data. The GPADC uses the same ADC channels for the ADC and the
>>> touchscreen therefore exposes these channels to the sunxi-gpadc-ts iio
>>> consumer which will be in charge of reading data from these channels for
>>> the input framework.
>>>
>>> The temperature can only be read when in touchscreen mode. This means if
>>> the buffers are being used for the ADC, the temperature sensor cannot be
>>> read.
>> That may be the bizarest hardware restriction I've heard of in a while! :)
>>>
>>> When a FIFO_DATA_PENDING irq occurs, its handler will read the entire FIFO
>>> and fill a buffer before sending it to the consumers which registered in
>>> IIO for the ADC channels.
>>>
>>> When a consumer starts buffering ADC channels,
>>> sunxi_gpadc_buffer_postenable is called and will enable FIFO_DATA_PENDING
>>> irq and select the mode in which the GPADC should run (ADC or touchscreen)
>>> depending on a property of the DT ("allwinner,ts-attached").
>>> When the consumer stops buffering, it disables the same irq.
>> Hmm. Might be possible to distinguish which consumer caused the start.
>> Thus, if the touchscreen is there we would know purely based on the
>> driver being the requester that we need to be in touchscreen mode.
>>
> 
> As of yet, can't see in which way I can retrieve the consumer in
> provider code. Maybe I'm missing something, I don't know?
I don't think we have a current way of doing this... Might be possible
to add one, but it would be a rather odd bit of reverse looking up.
> 
>>>
>>> Signed-off-by: Quentin Schulz <quentin.schulz@free-electrons.com>
>> You are moving fast on this - I'd have been tempted to do a mega
>> series with the updated version of the basic support and this on top
>> rather than a new unconnected series.
>>
>> (I'd forgotten that was still under review so got confused when I
>> went to look something up in the files you are modifying!).
>>> ---
>>>  drivers/iio/adc/Kconfig           |   1 +
>>>  drivers/iio/adc/sunxi-gpadc-iio.c | 153 ++++++++++++++++++++++++++++++++++----
>>>  2 files changed, 138 insertions(+), 16 deletions(-)
>>>
>>> diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
>>> index 184856f..15e3b08 100644
>>> --- a/drivers/iio/adc/Kconfig
>>> +++ b/drivers/iio/adc/Kconfig
>>> @@ -342,6 +342,7 @@ config SUNXI_ADC
>>>  	tristate "ADC driver for sunxi platforms"
>>>  	depends on IIO
>>>  	depends on MFD_SUNXI_ADC
>>> +	depends on IIO_BUFFER_CB
>>>  	help
>>>  	  Say yes here to build support for Allwinner (A10, A13 and A31) SoCs
>>>  	  ADC. This ADC provides 4 channels which can be used as an ADC or as a
>>> diff --git a/drivers/iio/adc/sunxi-gpadc-iio.c b/drivers/iio/adc/sunxi-gpadc-iio.c
>>> index 87cc913..2e44ca7 100644
>>> --- a/drivers/iio/adc/sunxi-gpadc-iio.c
>>> +++ b/drivers/iio/adc/sunxi-gpadc-iio.c
>>> @@ -16,8 +16,9 @@
>>>  #include <linux/platform_device.h>
>>>  #include <linux/regmap.h>
>>>  
>>> -#include <linux/iio/iio.h>
>>> +#include <linux/iio/buffer.h>
>>>  #include <linux/iio/driver.h>
>>> +#include <linux/iio/iio.h>
>> Can't say I'm a particular fan of reordering headers to be in alphabetical
>> order, but I suppose it doesn't really matter if you want to do it.
>> (to my mind there is a tree structure implicit in these headers with iio.h
>> at the top for generic support, then the various sub elements below).
>>
>>>  #include <linux/iio/machine.h>
>>>  #include <linux/mfd/sunxi-gpadc-mfd.h>
>>>  
>>> @@ -71,6 +72,7 @@
>>>  #define SUNXI_GPADC_TP_DATA_XY_CHANGE		BIT(13)
>>>  #define SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(x)	((x) << 8)  /* 5 bits */
>>>  #define SUNXI_GPADC_TP_DATA_DRQ_EN		BIT(7)
>>> +/* Be careful, flushing FIFO spawns SUNXI_GPADC_FIFO_DATA_PENDING interrupts */
>> Sounds like you learned that one the hard way ;)
>>>  #define SUNXI_GPADC_TP_FIFO_FLUSH		BIT(4)
>>>  #define SUNXI_GPADC_TP_UP_IRQ_EN		BIT(1)
>>>  #define SUNXI_GPADC_TP_DOWN_IRQ_EN		BIT(0)
>>> @@ -79,6 +81,7 @@
>>>  #define SUNXI_GPADC_TEMP_DATA_PENDING		BIT(18)
>>>  #define SUNXI_GPADC_FIFO_OVERRUN_PENDING	BIT(17)
>>>  #define SUNXI_GPADC_FIFO_DATA_PENDING		BIT(16)
>>> +#define SUNXI_GPADC_RXA_CNT			GENMASK(12, 8)
>>>  #define SUNXI_GPADC_TP_IDLE_FLG			BIT(2)
>>>  #define SUNXI_GPADC_TP_UP_PENDING		BIT(1)
>>>  #define SUNXI_GPADC_TP_DOWN_PENDING		BIT(0)
>>> @@ -101,19 +104,43 @@ struct sunxi_gpadc_dev {
>>>  	unsigned int			fifo_data_irq;
>>>  	unsigned int			temp_data_irq;
>>>  	unsigned int			flags;
>>> +	struct iio_dev			*indio_dev;
>> I was suprised to see this as normally it is cleaner to structure
>> the whole code to go in one direction through the structures (which is
>> why we don't provide a generic iio_device_from_priv bit of pointer magic).
>>
>> Anyhow, don't htink you are actually using it ;)
>>
> 
> I'm using to push to buffers from the irq handler since I pass the local
> structure (sunxi_gpadc_dev) to the irq handler when registering it. But
> I guess I can pass the iio_dev instead and remove this from the local
> structure.
I'd prefer passing the iio_dev and keep all lookups in one direction.
> 
> [...]
>>>  static int sunxi_gpadc_adc_read(struct iio_dev *indio_dev, int channel,
>>>  				int *val)
>>>  {
>>>  	struct sunxi_gpadc_dev *info = iio_priv(indio_dev);
>>> +	bool buffered = info->buffered;
>> Not worth the local version...
>>>  	int ret = 0;
>>> +	unsigned int reg;
>>>  
>>>  	mutex_lock(&indio_dev->mlock);
>>>  
>>>  	reinit_completion(&info->completion);
>>> +
>>> +	reg = SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(1) | SUNXI_GPADC_TP_FIFO_FLUSH;
>>> +	regmap_update_bits(info->regmap, SUNXI_GPADC_TP_INT_FIFOC, reg, reg);
>> I'd put it in directly rahter than having a reg local variable.  To mind
>> mind that would be slightly easier to understand.
>>> +
>>>  	if (info->flags & SUNXI_GPADC_ARCH_SUN6I)
>>>  		regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL1,
>>>  			     SUNXI_GPADC_SUN6I_TP_MODE_EN |
>>> @@ -153,9 +185,9 @@ static int sunxi_gpadc_adc_read(struct iio_dev *indio_dev, int channel,
>>>  			     SUNXI_GPADC_TP_MODE_EN |
>>>  			     SUNXI_GPADC_TP_ADC_SELECT |
>>>  			     SUNXI_GPADC_ADC_CHAN_SELECT(channel));
>>> -	regmap_write(info->regmap, SUNXI_GPADC_TP_INT_FIFOC,
>>> -		     SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(1) |
>>> -		     SUNXI_GPADC_TP_FIFO_FLUSH);
>> Whole load of infrastructure in place to lock buffered mode out and
>> revent transitions when we can't have them.
>>
>> iio_claim_direct_mode etc.  I think you can just use that here?
>> If you need to do extra checks on it being enabled that should be
>> fine too.
>>
> 
> Yes, way better with iio_device_claim_direct_mode and iio_buffer_enabled!
> 
>> As a general rule, it makes sense to simply disable polled reads
>> if in buffered mode.  Leads to much simpler code and generally
>> the data is already known to userspace anyway.
>>
> 
> That's what I try to do.
> However, I think the temperature of the SoC is an interesting feature to
> have. Since it ("hardwarely") works while the ADC is read in touchscreen
> mode (even in buffer mode), I guess it could be a good idea to allow it
> in the driver. If we don't do that, boards with a touchscreen connected
> to the ADC of the SoC will not get SoC temperatures and can't have
> proper thermal management. We already have one board in that case: the
> PocketCHIP.
> 
> Therefore, I also need to know if when the buffer is enabled, if it's
> for buffering ADC data or touchscreen data. If it's for ADC data, then I
> should disable temperature readings since it will return senseless
> values (from memory, always 0 which means something like -144?C).
Nice so no thermal management if we don't have a touch screen :)
> 
>> I have been meaning to do it a bit better when we have multiple
>> in kernel consumers, some expecting polled readings and some
>> pushed.  There some core caching magic will make sense to
>> keep the polled channels as available as possible when running
>> the  buffers.
>>
>> A bit fiddly to implement + might have some slightly suprising
>> results on delays on channels when say a sysfs trigger is
>> being used... (not a problem here as you have a fifo and hence
>> aren't using triggers).
>>
>> Anyhow, not really relevant here :)
>>
> [...]
>>>  	case IIO_CHAN_INFO_RAW:
>> Definitely use the iio_claim_direct_mode stuff here to avoid possible races
>> with the buffer being enabled whilst this read is in flight.
> 
> Indeed.
> 
>>> +		if (info->buffered)
>>> +			return -EBUSY;
>>> +
>>>  		ret = sunxi_gpadc_adc_read(indio_dev, chan->channel, val);
>>>  		if (ret)
>>>  			return ret;
>>> @@ -261,7 +302,29 @@ static irqreturn_t sunxi_gpadc_temp_data_irq_handler(int irq, void *dev_id)
>>>  static irqreturn_t sunxi_gpadc_fifo_data_irq_handler(int irq, void *dev_id)
>>>  {
>>>  	struct sunxi_gpadc_dev *info = dev_id;
>>> -	int ret;
>>> +	int ret, reg, i, fifo_count;
>>> +
>>> +	if (info->buffered) {
>>> +		if (regmap_read(info->regmap, SUNXI_GPADC_TP_INT_FIFOS, &reg))
>>> +			return IRQ_HANDLED;
>>> +
>>> +		fifo_count = (reg & SUNXI_GPADC_RXA_CNT) >> 8;
>>> +		/* Sometimes, the interrupt occurs when the FIFO is empty. */
>>> +		if (!fifo_count)
>>> +			return IRQ_HANDLED;
>>> +
>>> +		for (i = 0; i < fifo_count; i++) {
>>> +			if (regmap_read(info->regmap, SUNXI_GPADC_TP_DATA,
>>> +					&info->buffer.buffer[i]))
>>> +				return IRQ_HANDLED;
>>> +		}
>>> +
>>> +		info->buffer.buff_size = i;
>>> +
>>> +		iio_push_to_buffers(info->indio_dev, &info->buffer);
>> This is expecting a single 'scan' - e.g. set of channels read at one
>> time.  Here I think we could have repeated sets of channels?
>> (at least that would be what is normally meant by a fifo in such
>> a device).
>>
>> If so you need to read 'whole' scans and push them one at a time.
>> We don't yet have a bulk iio_push_to_buffers, though we can add
>> one if it makes sense.  Care will be needed though as we'd need
>> handle the case of different consumers either supporting or
>> not supporting this new functionality.  Not particularly hard though
>> if it is worth doing.
> 
> I didn't know it was meant for only one scan. Then I need a bulk
> iio_push_to_buffers.
We've had a few cases where that would be handy recently. 
The bit that makes it complex is if we are doing any demux of the channels
to multiple consumers.  Could be done by falling back to separating
the scan's out and pushing them one by one through the demux though.
Not sure there is a better way to do it though...
> 
> I have a rather big problem. The whole first FIFO at each touch is
> unusable so I have to drop it. I can detect the beginning of a touch
> when the TP_UP irq occurs, then I know the next full FIFO the consumer
> receives by callback is to be dropped. If I use push_to_buffers to send
> coordinates by coordinates, the consumer has no mean to know when the
> second FIFO (the first to be valid) starts and can be used. Either we
> can find a way to notify the consumer of the start of a new FIFO or I
> have to use a bulk iio_push_to_buffers.
Nasty indeed.
> 
> The workaround would be to register the TP_UP irq in the provider (the
> ADC driver) and do not send the first FIFO to the consumer. But then, we
> need a way to know which consumer requests buffering to know when to
> enable this irq and do all touchscreen-only logic (dropping first
> frame). And I guess we don't have something like that yet. Or I could
> only code a buffering in touchscreen mode and add the ADC buffering
> later? But it doesn't feel right to do what I think should be handled
> (TP_UP irq handler and first FIFO dropping) in the consumer, in the
> provider.
Would indeed by the nicer way of doing it, but we are ultimately working
around a hardware issue (to my mind it should never return rubbish!)
so I'd not worry too much about where the fix is.
> 
> So it's quiet a dead-end yet if I can't use iio_push_to_buffers with a
> whole FIFO (which you told is not how it is meant to be used).
It only worked here because you had control of both ends of the link.

First thought is that we should add a bulk push to buffers, but
that a little bit of fiddly code would be needed to unwind the
data in the demux if needed.  Probably not too hard to do.  It would then
need to repackage the data up as a bulk buffer data block to send onwards.

To do this I think you'd need to:
1) Add core support to have a bulk push with the right magic around to call
   the demux code in a loop over all the elements before pushing on.
2) Bulk handling in the callback buffer.
3) Kfifo bulk handling (mostly to allow us to test the demux code).

Actually, short of stuff I haven't thought of, doesn't look too tricky
and useful feature to have in general.

Jonathan
> 
> [...]
> Thanks,
> 
> Quentin
> 

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 4/5] input: touchscreen: support Allwinner SoCs' touchscreen
  2016-07-24 11:24   ` Jonathan Cameron
@ 2016-09-25 19:44     ` Quentin Schulz
  2016-09-27 19:59       ` Jonathan Cameron
  0 siblings, 1 reply; 31+ messages in thread
From: Quentin Schulz @ 2016-09-25 19:44 UTC (permalink / raw)
  To: linux-arm-kernel

On 24/07/2016 13:24, Jonathan Cameron wrote:
> On 20/07/16 09:29, Quentin Schulz wrote:
>> This adds support for Allwinner SoCs' (A10, A13 and A31) resistive
>> touchscreen. This driver is probed by the MFD sunxi-gpadc-mfd.
>>
>> This driver uses ADC channels exposed through the IIO framework by
>> sunxi-gpadc-iio to get its data. When opening this input device, it will
>> start buffering in the ADC driver and enable a TP_UP_PENDING irq. The ADC
>> driver will fill in a buffer with all data and call the callback the input
>> device associated with this buffer. The input device will then read the
>> buffer two by two and send X and Y coordinates to the input framework based
>> on what it received from the ADC's buffer. When closing this input device,
>> the buffering is stopped.
>>
>> Note that locations in the first received buffer after an TP_UP_PENDING irq
>> occurred are unreliable, thus dropped.
>>
> I think I now understand what you are doing.
> 
> The channel grab is grabbing 4 channels, when there are only two real
> ones (x and y) then you are abusing the callback interface from IIO.

Actually, I need an IIO channel only for registering the callback from
the consumer but I never use the IIO channel in an other way from
consumer side (everything's done in the provider by reading the FIFO
register and sending data to the callback via iio_buffer).

> That transmits only one scan (e.g. here (x,y)) per call. Because you
> have added a sideband route for the buffer size what you have wil work.
> 
> However, it is not how the interface should be used.  Please fix that.
> Using it correctly is not a big issue.
> 
> On the channels front, I'd be tempted to do this as follows:
> 
> 6 Channels, not 4.
> 
> First 4 are standard ADC channels
> Next 2 are the touch screen channels with appropriate descriptions
> (guessing these are actually differential channels across the various
> wires of the first two?)
> 

Yes they are differential channels.

However, I think there is actually no sense in using several channels
for the touchscreen as everything is handled by the hardware. You only
select the touchscreen mode by setting a register and then X and Y
coordinates will be added to the FIFO register when touching the
touchscreen. I would not mind not having IIO channel at all since we do
not read from the consumer. But I need a way to register a callback to
get data from the provider. I don't know if I make myself clear enough?

> Then you set the map up to apply to the last two channels only.
> By setting available_scan_masks to the relevant options in the IIO driver
> you'll be able to automatically lock the device when ever the
> touch screeen driver is loaded.
> 
> That map will be something like (in binary
> 000000
> 000011
> 000100
> 001000
> 010000
> 100000
> 001100
> 010100
> 011000
> 100100
> 101000
> 110000
> 011100
> 101100
> 110100
> 111000
> 111100
> 
> thus any combination of the ADC channels, but always all or none of the
> touchscreen (none when ever the ADC channels are on) the order above
> ensures we always turn on the minimum possible for a given requirement.
> 
> Hence whenever the touch screen is there it'll lock out the ADC usage.
> Your postenable can look at what is there and put it in the right mode.
> 

I'll dive more into that.

> After that, break your fifo read up into individual pairs of readings
> and push those out.
> 
> That way we end up using the interface in the standard fashion.
> 
> You'll also need fix the usage of the fifo for ADC mode which suffers
> from the same problem.  There all sorts of nasty crashes might occur
> or you might just loose data
> 
[...]
>> +/*
>> + * This function will be called by iio_push_to_buffers from another driver
>> + * (namely sunxi-gpadc-iio). It will be passed the buffer filled with input
>> + * values (X value then Y value) and the sunxi_gpadc_ts structure representing
>> + * the device.
>> + */
>> +static int sunxi_gpadc_ts_callback(const void *data, void *private)
>> +{
>> +	const struct sunxi_gpadc_buffer *buffer = data;
>> +	struct sunxi_gpadc_ts *info = private;
>> +	int i = 0;
>> +
>> +	/* Locations in the first buffer after an up event are unreliable */
>> +	if (info->ignore_fifo_data) {
>> +		info->ignore_fifo_data = false;
>> +		return 0;
>> +	}
>> +
> It doesn't work like this at all. You'll get one scan only on each call of
> this function.  As I said in the previous driver, if there is a true reason
> to do this (and I'm unconvinced as yet) then we need to add support in the
> iio core etc for this (and emulating it when multiple scan passing isn't
> happening).
> 
> I guess this will work, as you are passing the buffer size as a side
> band, but it is definitely not how that ABI is meant to be used.
> 
> Also, you grab 4 channels, and only two are used here.  Please explain...
> 

Hum. Actually, that's a mistake. I'm quiet confused about how I should
do it while I never read channels from the consumer. I still "need" one
for "linking" the consumer and the provider but in the meantime, I'm
never using that channel.

Thanks,
Quentin

-- 
Quentin Schulz, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 3/5] iio: adc: sunxi-gpadc-iio: enable iio_buffers
  2016-09-25  9:10       ` Jonathan Cameron
@ 2016-09-25 19:57         ` Quentin Schulz
  2016-09-27 19:38           ` Jonathan Cameron
  0 siblings, 1 reply; 31+ messages in thread
From: Quentin Schulz @ 2016-09-25 19:57 UTC (permalink / raw)
  To: linux-arm-kernel

On 25/09/2016 11:10, Jonathan Cameron wrote:
> On 24/09/16 18:40, Quentin Schulz wrote:
>> Hi Jonathan,
>>
>> Sorry for the (long) delay, I did not have time to work on it. I'll
>> mainly work in my free time now.
>>
>> Keep in mind this patch was proposed based on the v2 of the ADC patches.
>> Since then, substantial changes have been made and I'm working on
>> rebasing this series of patches on the v6, so comments here might
>> include references to code parts added later in the ADC patch series.
>>
>> On 24/07/2016 13:03, Jonathan Cameron wrote:
>>> On 20/07/16 09:29, Quentin Schulz wrote:
>>>> This enables the use of buffers on ADC channels of sunxi-gpadc-iio driver.
>>>> It also prepares the code which will be used by the touchscreen driver
>>>> named sunxi-gpadc-ts.
>>>>
>>>> The GPADC on Allwinner SoCs (A10, A13 and A31) has a 12 bits register for
>>>> conversion's data. The GPADC uses the same ADC channels for the ADC and the
>>>> touchscreen therefore exposes these channels to the sunxi-gpadc-ts iio
>>>> consumer which will be in charge of reading data from these channels for
>>>> the input framework.
>>>>
>>>> The temperature can only be read when in touchscreen mode. This means if
>>>> the buffers are being used for the ADC, the temperature sensor cannot be
>>>> read.
>>> That may be the bizarest hardware restriction I've heard of in a while! :)
>>>>
>>>> When a FIFO_DATA_PENDING irq occurs, its handler will read the entire FIFO
>>>> and fill a buffer before sending it to the consumers which registered in
>>>> IIO for the ADC channels.
>>>>
>>>> When a consumer starts buffering ADC channels,
>>>> sunxi_gpadc_buffer_postenable is called and will enable FIFO_DATA_PENDING
>>>> irq and select the mode in which the GPADC should run (ADC or touchscreen)
>>>> depending on a property of the DT ("allwinner,ts-attached").
>>>> When the consumer stops buffering, it disables the same irq.
>>> Hmm. Might be possible to distinguish which consumer caused the start.
>>> Thus, if the touchscreen is there we would know purely based on the
>>> driver being the requester that we need to be in touchscreen mode.
>>>
>>
>> As of yet, can't see in which way I can retrieve the consumer in
>> provider code. Maybe I'm missing something, I don't know?
> I don't think we have a current way of doing this... Might be possible
> to add one, but it would be a rather odd bit of reverse looking up.
[...]
>>>> @@ -101,19 +104,43 @@ struct sunxi_gpadc_dev {
>>>>  	unsigned int			fifo_data_irq;
>>>>  	unsigned int			temp_data_irq;
>>>>  	unsigned int			flags;
>>>> +	struct iio_dev			*indio_dev;
>>> I was suprised to see this as normally it is cleaner to structure
>>> the whole code to go in one direction through the structures (which is
>>> why we don't provide a generic iio_device_from_priv bit of pointer magic).
>>>
>>> Anyhow, don't htink you are actually using it ;)
>>>
>>
>> I'm using to push to buffers from the irq handler since I pass the local
>> structure (sunxi_gpadc_dev) to the irq handler when registering it. But
>> I guess I can pass the iio_dev instead and remove this from the local
>> structure.
> I'd prefer passing the iio_dev and keep all lookups in one direction.

ACK.

>>
>> [...]
>>>>  static int sunxi_gpadc_adc_read(struct iio_dev *indio_dev, int channel,
>>>>  				int *val)
>>>>  {
>>>>  	struct sunxi_gpadc_dev *info = iio_priv(indio_dev);
>>>> +	bool buffered = info->buffered;
>>> Not worth the local version...
>>>>  	int ret = 0;
>>>> +	unsigned int reg;
>>>>  
>>>>  	mutex_lock(&indio_dev->mlock);
>>>>  
>>>>  	reinit_completion(&info->completion);
>>>> +
>>>> +	reg = SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(1) | SUNXI_GPADC_TP_FIFO_FLUSH;
>>>> +	regmap_update_bits(info->regmap, SUNXI_GPADC_TP_INT_FIFOC, reg, reg);
>>> I'd put it in directly rahter than having a reg local variable.  To mind
>>> mind that would be slightly easier to understand.
>>>> +
>>>>  	if (info->flags & SUNXI_GPADC_ARCH_SUN6I)
>>>>  		regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL1,
>>>>  			     SUNXI_GPADC_SUN6I_TP_MODE_EN |
>>>> @@ -153,9 +185,9 @@ static int sunxi_gpadc_adc_read(struct iio_dev *indio_dev, int channel,
>>>>  			     SUNXI_GPADC_TP_MODE_EN |
>>>>  			     SUNXI_GPADC_TP_ADC_SELECT |
>>>>  			     SUNXI_GPADC_ADC_CHAN_SELECT(channel));
>>>> -	regmap_write(info->regmap, SUNXI_GPADC_TP_INT_FIFOC,
>>>> -		     SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(1) |
>>>> -		     SUNXI_GPADC_TP_FIFO_FLUSH);
>>> Whole load of infrastructure in place to lock buffered mode out and
>>> revent transitions when we can't have them.
>>>
>>> iio_claim_direct_mode etc.  I think you can just use that here?
>>> If you need to do extra checks on it being enabled that should be
>>> fine too.
>>>
>>
>> Yes, way better with iio_device_claim_direct_mode and iio_buffer_enabled!
>>
>>> As a general rule, it makes sense to simply disable polled reads
>>> if in buffered mode.  Leads to much simpler code and generally
>>> the data is already known to userspace anyway.
>>>
>>
>> That's what I try to do.
>> However, I think the temperature of the SoC is an interesting feature to
>> have. Since it ("hardwarely") works while the ADC is read in touchscreen
>> mode (even in buffer mode), I guess it could be a good idea to allow it
>> in the driver. If we don't do that, boards with a touchscreen connected
>> to the ADC of the SoC will not get SoC temperatures and can't have
>> proper thermal management. We already have one board in that case: the
>> PocketCHIP.
>>
>> Therefore, I also need to know if when the buffer is enabled, if it's
>> for buffering ADC data or touchscreen data. If it's for ADC data, then I
>> should disable temperature readings since it will return senseless
>> values (from memory, always 0 which means something like -144?C).
> Nice so no thermal management if we don't have a touch screen :)

I think that's a big constraint. I think we should be able to read the
temperature when the ADC is in either touchscreen or ADC mode but ONLY
in DIRECT_MODE. While in BUFFER_MODE, we should enable temperature
reading only when the touchscreen is present.

That might induce a problem with the thermal framework I think. I've had
some problem with temperature reading's timeout so I'm reading a
manually "cached" temperature which is updated when the temperature
reading is done. However, it will never be updated when the ADC is in
buffered ADC mode. I don't know if in that case it is better to
unregister the thermal device, to give outdated values or to notify the
thermal framework the temperature readings timed out (which is verbose).
[...]
>>>> @@ -261,7 +302,29 @@ static irqreturn_t sunxi_gpadc_temp_data_irq_handler(int irq, void *dev_id)
>>>>  static irqreturn_t sunxi_gpadc_fifo_data_irq_handler(int irq, void *dev_id)
>>>>  {
>>>>  	struct sunxi_gpadc_dev *info = dev_id;
>>>> -	int ret;
>>>> +	int ret, reg, i, fifo_count;
>>>> +
>>>> +	if (info->buffered) {
>>>> +		if (regmap_read(info->regmap, SUNXI_GPADC_TP_INT_FIFOS, &reg))
>>>> +			return IRQ_HANDLED;
>>>> +
>>>> +		fifo_count = (reg & SUNXI_GPADC_RXA_CNT) >> 8;
>>>> +		/* Sometimes, the interrupt occurs when the FIFO is empty. */
>>>> +		if (!fifo_count)
>>>> +			return IRQ_HANDLED;
>>>> +
>>>> +		for (i = 0; i < fifo_count; i++) {
>>>> +			if (regmap_read(info->regmap, SUNXI_GPADC_TP_DATA,
>>>> +					&info->buffer.buffer[i]))
>>>> +				return IRQ_HANDLED;
>>>> +		}
>>>> +
>>>> +		info->buffer.buff_size = i;
>>>> +
>>>> +		iio_push_to_buffers(info->indio_dev, &info->buffer);
>>> This is expecting a single 'scan' - e.g. set of channels read at one
>>> time.  Here I think we could have repeated sets of channels?
>>> (at least that would be what is normally meant by a fifo in such
>>> a device).
>>>
>>> If so you need to read 'whole' scans and push them one at a time.
>>> We don't yet have a bulk iio_push_to_buffers, though we can add
>>> one if it makes sense.  Care will be needed though as we'd need
>>> handle the case of different consumers either supporting or
>>> not supporting this new functionality.  Not particularly hard though
>>> if it is worth doing.
>>
>> I didn't know it was meant for only one scan. Then I need a bulk
>> iio_push_to_buffers.
> We've had a few cases where that would be handy recently. 
> The bit that makes it complex is if we are doing any demux of the channels
> to multiple consumers.  Could be done by falling back to separating
> the scan's out and pushing them one by one through the demux though.
> Not sure there is a better way to do it though...
>>
>> I have a rather big problem. The whole first FIFO at each touch is
>> unusable so I have to drop it. I can detect the beginning of a touch
>> when the TP_UP irq occurs, then I know the next full FIFO the consumer
>> receives by callback is to be dropped. If I use push_to_buffers to send
>> coordinates by coordinates, the consumer has no mean to know when the
>> second FIFO (the first to be valid) starts and can be used. Either we
>> can find a way to notify the consumer of the start of a new FIFO or I
>> have to use a bulk iio_push_to_buffers.
> Nasty indeed.
>>
>> The workaround would be to register the TP_UP irq in the provider (the
>> ADC driver) and do not send the first FIFO to the consumer. But then, we
>> need a way to know which consumer requests buffering to know when to
>> enable this irq and do all touchscreen-only logic (dropping first
>> frame). And I guess we don't have something like that yet. Or I could
>> only code a buffering in touchscreen mode and add the ADC buffering
>> later? But it doesn't feel right to do what I think should be handled
>> (TP_UP irq handler and first FIFO dropping) in the consumer, in the
>> provider.
> Would indeed by the nicer way of doing it, but we are ultimately working
> around a hardware issue (to my mind it should never return rubbish!)
> so I'd not worry too much about where the fix is.
>>
>> So it's quiet a dead-end yet if I can't use iio_push_to_buffers with a
>> whole FIFO (which you told is not how it is meant to be used).
> It only worked here because you had control of both ends of the link.
> 
> First thought is that we should add a bulk push to buffers, but
> that a little bit of fiddly code would be needed to unwind the
> data in the demux if needed.  Probably not too hard to do.  It would then
> need to repackage the data up as a bulk buffer data block to send onwards.
> 
> To do this I think you'd need to:
> 1) Add core support to have a bulk push with the right magic around to call
>    the demux code in a loop over all the elements before pushing on.
> 2) Bulk handling in the callback buffer.
> 3) Kfifo bulk handling (mostly to allow us to test the demux code).
> 
> Actually, short of stuff I haven't thought of, doesn't look too tricky
> and useful feature to have in general.
> 

Then I guess we'll have to do it :) I might need a lot of guiding though
but maybe that's more of an IRC chat or non-lkml conversation?

Thanks,
Quentin

-- 
Quentin Schulz, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 3/5] iio: adc: sunxi-gpadc-iio: enable iio_buffers
  2016-09-25 19:57         ` Quentin Schulz
@ 2016-09-27 19:38           ` Jonathan Cameron
  0 siblings, 0 replies; 31+ messages in thread
From: Jonathan Cameron @ 2016-09-27 19:38 UTC (permalink / raw)
  To: linux-arm-kernel

On 25/09/16 20:57, Quentin Schulz wrote:
> On 25/09/2016 11:10, Jonathan Cameron wrote:
>> On 24/09/16 18:40, Quentin Schulz wrote:
>>> Hi Jonathan,
>>>
>>> Sorry for the (long) delay, I did not have time to work on it. I'll
>>> mainly work in my free time now.
>>>
>>> Keep in mind this patch was proposed based on the v2 of the ADC patches.
>>> Since then, substantial changes have been made and I'm working on
>>> rebasing this series of patches on the v6, so comments here might
>>> include references to code parts added later in the ADC patch series.
>>>
>>> On 24/07/2016 13:03, Jonathan Cameron wrote:
>>>> On 20/07/16 09:29, Quentin Schulz wrote:
>>>>> This enables the use of buffers on ADC channels of sunxi-gpadc-iio driver.
>>>>> It also prepares the code which will be used by the touchscreen driver
>>>>> named sunxi-gpadc-ts.
>>>>>
>>>>> The GPADC on Allwinner SoCs (A10, A13 and A31) has a 12 bits register for
>>>>> conversion's data. The GPADC uses the same ADC channels for the ADC and the
>>>>> touchscreen therefore exposes these channels to the sunxi-gpadc-ts iio
>>>>> consumer which will be in charge of reading data from these channels for
>>>>> the input framework.
>>>>>
>>>>> The temperature can only be read when in touchscreen mode. This means if
>>>>> the buffers are being used for the ADC, the temperature sensor cannot be
>>>>> read.
>>>> That may be the bizarest hardware restriction I've heard of in a while! :)
>>>>>
>>>>> When a FIFO_DATA_PENDING irq occurs, its handler will read the entire FIFO
>>>>> and fill a buffer before sending it to the consumers which registered in
>>>>> IIO for the ADC channels.
>>>>>
>>>>> When a consumer starts buffering ADC channels,
>>>>> sunxi_gpadc_buffer_postenable is called and will enable FIFO_DATA_PENDING
>>>>> irq and select the mode in which the GPADC should run (ADC or touchscreen)
>>>>> depending on a property of the DT ("allwinner,ts-attached").
>>>>> When the consumer stops buffering, it disables the same irq.
>>>> Hmm. Might be possible to distinguish which consumer caused the start.
>>>> Thus, if the touchscreen is there we would know purely based on the
>>>> driver being the requester that we need to be in touchscreen mode.
>>>>
>>>
>>> As of yet, can't see in which way I can retrieve the consumer in
>>> provider code. Maybe I'm missing something, I don't know?
>> I don't think we have a current way of doing this... Might be possible
>> to add one, but it would be a rather odd bit of reverse looking up.
> [...]
>>>>> @@ -101,19 +104,43 @@ struct sunxi_gpadc_dev {
>>>>>  	unsigned int			fifo_data_irq;
>>>>>  	unsigned int			temp_data_irq;
>>>>>  	unsigned int			flags;
>>>>> +	struct iio_dev			*indio_dev;
>>>> I was suprised to see this as normally it is cleaner to structure
>>>> the whole code to go in one direction through the structures (which is
>>>> why we don't provide a generic iio_device_from_priv bit of pointer magic).
>>>>
>>>> Anyhow, don't htink you are actually using it ;)
>>>>
>>>
>>> I'm using to push to buffers from the irq handler since I pass the local
>>> structure (sunxi_gpadc_dev) to the irq handler when registering it. But
>>> I guess I can pass the iio_dev instead and remove this from the local
>>> structure.
>> I'd prefer passing the iio_dev and keep all lookups in one direction.
> 
> ACK.
> 
>>>
>>> [...]
>>>>>  static int sunxi_gpadc_adc_read(struct iio_dev *indio_dev, int channel,
>>>>>  				int *val)
>>>>>  {
>>>>>  	struct sunxi_gpadc_dev *info = iio_priv(indio_dev);
>>>>> +	bool buffered = info->buffered;
>>>> Not worth the local version...
>>>>>  	int ret = 0;
>>>>> +	unsigned int reg;
>>>>>  
>>>>>  	mutex_lock(&indio_dev->mlock);
>>>>>  
>>>>>  	reinit_completion(&info->completion);
>>>>> +
>>>>> +	reg = SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(1) | SUNXI_GPADC_TP_FIFO_FLUSH;
>>>>> +	regmap_update_bits(info->regmap, SUNXI_GPADC_TP_INT_FIFOC, reg, reg);
>>>> I'd put it in directly rahter than having a reg local variable.  To mind
>>>> mind that would be slightly easier to understand.
>>>>> +
>>>>>  	if (info->flags & SUNXI_GPADC_ARCH_SUN6I)
>>>>>  		regmap_write(info->regmap, SUNXI_GPADC_TP_CTRL1,
>>>>>  			     SUNXI_GPADC_SUN6I_TP_MODE_EN |
>>>>> @@ -153,9 +185,9 @@ static int sunxi_gpadc_adc_read(struct iio_dev *indio_dev, int channel,
>>>>>  			     SUNXI_GPADC_TP_MODE_EN |
>>>>>  			     SUNXI_GPADC_TP_ADC_SELECT |
>>>>>  			     SUNXI_GPADC_ADC_CHAN_SELECT(channel));
>>>>> -	regmap_write(info->regmap, SUNXI_GPADC_TP_INT_FIFOC,
>>>>> -		     SUNXI_GPADC_TP_FIFO_TRIG_LEVEL(1) |
>>>>> -		     SUNXI_GPADC_TP_FIFO_FLUSH);
>>>> Whole load of infrastructure in place to lock buffered mode out and
>>>> revent transitions when we can't have them.
>>>>
>>>> iio_claim_direct_mode etc.  I think you can just use that here?
>>>> If you need to do extra checks on it being enabled that should be
>>>> fine too.
>>>>
>>>
>>> Yes, way better with iio_device_claim_direct_mode and iio_buffer_enabled!
>>>
>>>> As a general rule, it makes sense to simply disable polled reads
>>>> if in buffered mode.  Leads to much simpler code and generally
>>>> the data is already known to userspace anyway.
>>>>
>>>
>>> That's what I try to do.
>>> However, I think the temperature of the SoC is an interesting feature to
>>> have. Since it ("hardwarely") works while the ADC is read in touchscreen
>>> mode (even in buffer mode), I guess it could be a good idea to allow it
>>> in the driver. If we don't do that, boards with a touchscreen connected
>>> to the ADC of the SoC will not get SoC temperatures and can't have
>>> proper thermal management. We already have one board in that case: the
>>> PocketCHIP.
>>>
>>> Therefore, I also need to know if when the buffer is enabled, if it's
>>> for buffering ADC data or touchscreen data. If it's for ADC data, then I
>>> should disable temperature readings since it will return senseless
>>> values (from memory, always 0 which means something like -144?C).
>> Nice so no thermal management if we don't have a touch screen :)
> 
> I think that's a big constraint. I think we should be able to read the
> temperature when the ADC is in either touchscreen or ADC mode but ONLY
> in DIRECT_MODE. While in BUFFER_MODE, we should enable temperature
> reading only when the touchscreen is present.
It would certainly be preferable to do so.
> 
> That might induce a problem with the thermal framework I think. I've had
> some problem with temperature reading's timeout so I'm reading a
> manually "cached" temperature which is updated when the temperature
> reading is done. However, it will never be updated when the ADC is in
> buffered ADC mode. I don't know if in that case it is better to
> unregister the thermal device, to give outdated values or to notify the
> thermal framework the temperature readings timed out (which is verbose).
> [...]
Definitely need some input from the thermal guys on this.  Perhaps
they need to have a way of saying -EBUSY.
>>>>> @@ -261,7 +302,29 @@ static irqreturn_t sunxi_gpadc_temp_data_irq_handler(int irq, void *dev_id)
>>>>>  static irqreturn_t sunxi_gpadc_fifo_data_irq_handler(int irq, void *dev_id)
>>>>>  {
>>>>>  	struct sunxi_gpadc_dev *info = dev_id;
>>>>> -	int ret;
>>>>> +	int ret, reg, i, fifo_count;
>>>>> +
>>>>> +	if (info->buffered) {
>>>>> +		if (regmap_read(info->regmap, SUNXI_GPADC_TP_INT_FIFOS, &reg))
>>>>> +			return IRQ_HANDLED;
>>>>> +
>>>>> +		fifo_count = (reg & SUNXI_GPADC_RXA_CNT) >> 8;
>>>>> +		/* Sometimes, the interrupt occurs when the FIFO is empty. */
>>>>> +		if (!fifo_count)
>>>>> +			return IRQ_HANDLED;
>>>>> +
>>>>> +		for (i = 0; i < fifo_count; i++) {
>>>>> +			if (regmap_read(info->regmap, SUNXI_GPADC_TP_DATA,
>>>>> +					&info->buffer.buffer[i]))
>>>>> +				return IRQ_HANDLED;
>>>>> +		}
>>>>> +
>>>>> +		info->buffer.buff_size = i;
>>>>> +
>>>>> +		iio_push_to_buffers(info->indio_dev, &info->buffer);
>>>> This is expecting a single 'scan' - e.g. set of channels read at one
>>>> time.  Here I think we could have repeated sets of channels?
>>>> (at least that would be what is normally meant by a fifo in such
>>>> a device).
>>>>
>>>> If so you need to read 'whole' scans and push them one at a time.
>>>> We don't yet have a bulk iio_push_to_buffers, though we can add
>>>> one if it makes sense.  Care will be needed though as we'd need
>>>> handle the case of different consumers either supporting or
>>>> not supporting this new functionality.  Not particularly hard though
>>>> if it is worth doing.
>>>
>>> I didn't know it was meant for only one scan. Then I need a bulk
>>> iio_push_to_buffers.
>> We've had a few cases where that would be handy recently. 
>> The bit that makes it complex is if we are doing any demux of the channels
>> to multiple consumers.  Could be done by falling back to separating
>> the scan's out and pushing them one by one through the demux though.
>> Not sure there is a better way to do it though...
>>>
>>> I have a rather big problem. The whole first FIFO at each touch is
>>> unusable so I have to drop it. I can detect the beginning of a touch
>>> when the TP_UP irq occurs, then I know the next full FIFO the consumer
>>> receives by callback is to be dropped. If I use push_to_buffers to send
>>> coordinates by coordinates, the consumer has no mean to know when the
>>> second FIFO (the first to be valid) starts and can be used. Either we
>>> can find a way to notify the consumer of the start of a new FIFO or I
>>> have to use a bulk iio_push_to_buffers.
>> Nasty indeed.
>>>
>>> The workaround would be to register the TP_UP irq in the provider (the
>>> ADC driver) and do not send the first FIFO to the consumer. But then, we
>>> need a way to know which consumer requests buffering to know when to
>>> enable this irq and do all touchscreen-only logic (dropping first
>>> frame). And I guess we don't have something like that yet. Or I could
>>> only code a buffering in touchscreen mode and add the ADC buffering
>>> later? But it doesn't feel right to do what I think should be handled
>>> (TP_UP irq handler and first FIFO dropping) in the consumer, in the
>>> provider.
>> Would indeed by the nicer way of doing it, but we are ultimately working
>> around a hardware issue (to my mind it should never return rubbish!)
>> so I'd not worry too much about where the fix is.
>>>
>>> So it's quiet a dead-end yet if I can't use iio_push_to_buffers with a
>>> whole FIFO (which you told is not how it is meant to be used).
>> It only worked here because you had control of both ends of the link.
>>
>> First thought is that we should add a bulk push to buffers, but
>> that a little bit of fiddly code would be needed to unwind the
>> data in the demux if needed.  Probably not too hard to do.  It would then
>> need to repackage the data up as a bulk buffer data block to send onwards.
>>
>> To do this I think you'd need to:
>> 1) Add core support to have a bulk push with the right magic around to call
>>    the demux code in a loop over all the elements before pushing on.
>> 2) Bulk handling in the callback buffer.
>> 3) Kfifo bulk handling (mostly to allow us to test the demux code).
>>
>> Actually, short of stuff I haven't thought of, doesn't look too tricky
>> and useful feature to have in general.
>>
> 
> Then I guess we'll have to do it :) I might need a lot of guiding though
> but maybe that's more of an IRC chat or non-lkml conversation?
Sure, funnily enough I'm updating the sca3000 driver to finally 
lift it out of staging and a bulk write would help there as well.
(tends to be copying 32 scans + at a time due to a hardware fifo).

Looking at the new kionix parts which have 8K hardware fifos this gets
even more interesting (though perhaps we should handle as pure
hardware fifos without the front end kfifo.)
> 
> Thanks,
> Quentin
> 

^ permalink raw reply	[flat|nested] 31+ messages in thread

* [PATCH 4/5] input: touchscreen: support Allwinner SoCs' touchscreen
  2016-09-25 19:44     ` Quentin Schulz
@ 2016-09-27 19:59       ` Jonathan Cameron
  0 siblings, 0 replies; 31+ messages in thread
From: Jonathan Cameron @ 2016-09-27 19:59 UTC (permalink / raw)
  To: linux-arm-kernel

On 25/09/16 20:44, Quentin Schulz wrote:
> On 24/07/2016 13:24, Jonathan Cameron wrote:
>> On 20/07/16 09:29, Quentin Schulz wrote:
>>> This adds support for Allwinner SoCs' (A10, A13 and A31) resistive
>>> touchscreen. This driver is probed by the MFD sunxi-gpadc-mfd.
>>>
>>> This driver uses ADC channels exposed through the IIO framework by
>>> sunxi-gpadc-iio to get its data. When opening this input device, it will
>>> start buffering in the ADC driver and enable a TP_UP_PENDING irq. The ADC
>>> driver will fill in a buffer with all data and call the callback the input
>>> device associated with this buffer. The input device will then read the
>>> buffer two by two and send X and Y coordinates to the input framework based
>>> on what it received from the ADC's buffer. When closing this input device,
>>> the buffering is stopped.
>>>
>>> Note that locations in the first received buffer after an TP_UP_PENDING irq
>>> occurred are unreliable, thus dropped.
>>>
>> I think I now understand what you are doing.
>>
>> The channel grab is grabbing 4 channels, when there are only two real
>> ones (x and y) then you are abusing the callback interface from IIO.
> 
> Actually, I need an IIO channel only for registering the callback from
> the consumer but I never use the IIO channel in an other way from
> consumer side (everything's done in the provider by reading the FIFO
> register and sending data to the callback via iio_buffer).
Whilst it 'would work' to just hook in and deal with the data if it
came through a single channel with enough space, that would be an
abuse of the interface which is very much designed to ship 'scans'
(e.g. sets of data taken at the same time ish).

There is a demux unit in the path which is designed to pull out
only the channels wanted by the consumer.  If the combination
of available_scan_masks in the provider and the channels requested
by the consumer (via the map) are right then you'll only 'get'
the data you want and the rest will disappear into thin air ;)

> 
>> That transmits only one scan (e.g. here (x,y)) per call. Because you
>> have added a sideband route for the buffer size what you have wil work.
>>
>> However, it is not how the interface should be used.  Please fix that.
>> Using it correctly is not a big issue.
>>
>> On the channels front, I'd be tempted to do this as follows:
>>
>> 6 Channels, not 4.
>>
>> First 4 are standard ADC channels
>> Next 2 are the touch screen channels with appropriate descriptions
>> (guessing these are actually differential channels across the various
>> wires of the first two?)
>>
> 
> Yes they are differential channels.
> 
> However, I think there is actually no sense in using several channels
> for the touchscreen as everything is handled by the hardware. You only
> select the touchscreen mode by setting a register and then X and Y
> coordinates will be added to the FIFO register when touching the
> touchscreen. I would not mind not having IIO channel at all since we do
> not read from the consumer. But I need a way to register a callback to
> get data from the provider. I don't know if I make myself clear enough?
If you want to do it another way (I'd rather you didn't!) then you'll
need to create an mfd style base driver and register the touch screen
on that with custom callback registration etc.
> 
>> Then you set the map up to apply to the last two channels only.
>> By setting available_scan_masks to the relevant options in the IIO driver
>> you'll be able to automatically lock the device when ever the
>> touch screeen driver is loaded.
>>
>> That map will be something like (in binary
>> 000000
>> 000011
>> 000100
>> 001000
>> 010000
>> 100000
>> 001100
>> 010100
>> 011000
>> 100100
>> 101000
>> 110000
>> 011100
>> 101100
>> 110100
>> 111000
>> 111100
>>
>> thus any combination of the ADC channels, but always all or none of the
>> touchscreen (none when ever the ADC channels are on) the order above
>> ensures we always turn on the minimum possible for a given requirement.
>>
>> Hence whenever the touch screen is there it'll lock out the ADC usage.
>> Your postenable can look at what is there and put it in the right mode.
>>
> 
> I'll dive more into that.
> 
>> After that, break your fifo read up into individual pairs of readings
>> and push those out.
>>
>> That way we end up using the interface in the standard fashion.
>>
>> You'll also need fix the usage of the fifo for ADC mode which suffers
>> from the same problem.  There all sorts of nasty crashes might occur
>> or you might just loose data
>>
> [...]
>>> +/*
>>> + * This function will be called by iio_push_to_buffers from another driver
>>> + * (namely sunxi-gpadc-iio). It will be passed the buffer filled with input
>>> + * values (X value then Y value) and the sunxi_gpadc_ts structure representing
>>> + * the device.
>>> + */
>>> +static int sunxi_gpadc_ts_callback(const void *data, void *private)
>>> +{
>>> +	const struct sunxi_gpadc_buffer *buffer = data;
>>> +	struct sunxi_gpadc_ts *info = private;
>>> +	int i = 0;
>>> +
>>> +	/* Locations in the first buffer after an up event are unreliable */
>>> +	if (info->ignore_fifo_data) {
>>> +		info->ignore_fifo_data = false;
>>> +		return 0;
>>> +	}
>>> +
>> It doesn't work like this at all. You'll get one scan only on each call of
>> this function.  As I said in the previous driver, if there is a true reason
>> to do this (and I'm unconvinced as yet) then we need to add support in the
>> iio core etc for this (and emulating it when multiple scan passing isn't
>> happening).
>>
>> I guess this will work, as you are passing the buffer size as a side
>> band, but it is definitely not how that ABI is meant to be used.
>>
>> Also, you grab 4 channels, and only two are used here.  Please explain...
>>
> 
> Hum. Actually, that's a mistake. I'm quiet confused about how I should
> do it while I never read channels from the consumer. I still "need" one
> for "linking" the consumer and the provider but in the meantime, I'm
> never using that channel.
You are, because the demux is only sending you the channels you
are registered for.  Thus the provider should push whatever it
gets to the buffers and by the time it hits the consumer it should
get only what it needs.

It's not so interesting with this hardware because you can't do
general purpose reads at the same time as the touchscreen stuff is
running (annoyingly).  With hardware that can do any combination of
channels you end up with for example:

Provider - pushing out a superset of all the channels anyone cares
about.

Consumer 1 (touch screen) gets just the x and y coordinates when pen
is down.

Consumer 2 (temperature - thermal) gets just the temperature.

Consumer 3 (e.g. battery monitory) gets just the charger voltages etc.

Consumer 4 (stretching here - analog accelerometer) gets just
accelerations for the input bridge I never get round to finally
submitting.

Jonathan

^ permalink raw reply	[flat|nested] 31+ messages in thread

end of thread, other threads:[~2016-09-27 19:59 UTC | newest]

Thread overview: 31+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-20  8:29 [PATCH 0/5] add resistive touchscreen support for new Allwinner SoCs' GPADC's driver Quentin Schulz
2016-07-20  8:29 ` [PATCH 1/5] mfd: sunxi-gpadc-mfd: add TP_UP_PENDING irq Quentin Schulz
2016-07-20  8:29 ` [PATCH 2/5] mfd: sunxi-gpadc-mfd: add buffer structure Quentin Schulz
2016-07-24 10:32   ` Jonathan Cameron
2016-07-20  8:29 ` [PATCH 3/5] iio: adc: sunxi-gpadc-iio: enable iio_buffers Quentin Schulz
2016-07-20  8:38   ` Peter Meerwald-Stadler
2016-07-20  8:57     ` Quentin Schulz
2016-07-24 11:03   ` Jonathan Cameron
2016-09-24 17:40     ` Quentin Schulz
2016-09-25  9:10       ` Jonathan Cameron
2016-09-25 19:57         ` Quentin Schulz
2016-09-27 19:38           ` Jonathan Cameron
2016-07-20  8:29 ` [PATCH 4/5] input: touchscreen: support Allwinner SoCs' touchscreen Quentin Schulz
2016-07-20 17:25   ` Dmitry Torokhov
2016-07-20 20:13     ` Jonathan Cameron
2016-09-24 18:26     ` Quentin Schulz
2016-09-24 18:39       ` Dmitry Torokhov
2016-07-21  6:29   ` Maxime Ripard
2016-07-21  6:41     ` Dmitry Torokhov
2016-07-25  9:45       ` maxime.ripard at free-electrons.com
2016-07-25 17:08         ` Dmitry Torokhov
2016-07-26 15:13           ` Maxime Ripard
2016-07-24 11:24   ` Jonathan Cameron
2016-09-25 19:44     ` Quentin Schulz
2016-09-27 19:59       ` Jonathan Cameron
2016-07-20  8:29 ` [PATCH 5/5] mfd: sunxi-gpadc-mfd: probe sunxi-gpadc-ts driver Quentin Schulz
2016-07-21  6:08   ` Maxime Ripard
2016-07-24 11:26     ` Jonathan Cameron
2016-07-25  9:51       ` Maxime Ripard
2016-07-25 10:08         ` Jonathan Cameron
2016-07-25 10:21       ` Lee Jones

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).