mbox series

[v2,00/10] Add support for SAMA5D2 touchscreen

Message ID 1522153963-1121-1-git-send-email-eugen.hristev@microchip.com
Headers show
Series Add support for SAMA5D2 touchscreen | expand

Message

Eugen Hristev March 27, 2018, 12:32 p.m. UTC
Hello,

This patch series is a rework of my previous series named:
[PATCH 00/14] iio: triggers: add consumer support

In few words, this is the implementation of splitting the functionality
of the IP block ADC device in SAMA5D2 SoC from ADC with touchscreen
support. In order to avoid having a MFD device, two separate
drivers that would work on same register base and split the IRQ,etc,
as advised on the mailing list, I created a consumer driver for the
channels, that will connect to the ADC as described in the device tree.

I have collected feedback from everyone and here is the result:
I have added a new generic resistive touchscreen driver, which acts
as a iio consumer for the given channels and will create an input
device and report the events. It uses a callback buffer to register
to the IIO device and waits for data to be pushed.
Inside the IIO device, I have kept a similar approach with the first version
of the series, except that now the driver can take multiple buffers, and
will configure the touchscreen part of the hardware device if the specific
channels are requested.

The SAMA5D2 ADC driver registers three new channels: two for the
position on the X and Y axis, and one for the touch pressure.
When channels are requested, it will check if the touchscreen channel mask
includes the requested channels (it is possible that the consumer driver
will not request pressure for example). If it's the case, it will work
in touchscreen mode, and will refuse to do usual analog-digital conversion,
because we have a single trigger and the touchscreen needs it.
When the scan mask will include only old channels, the driver will function
in the same way as before. If the scan mask somehow is a mix of the two (the
masks intersect), the driver will refuse to work whatsoever (cannot have both
in the same time).
The driver allows reading raw data for the new channels, if claim direct
mode works: no touchscreen driver requested anything. The new channels can
act like the old ones. However, when requesting these channels, the usual
trigger will not work and will not be enabled. The touchscreen channels
require special trigger and irq configuration: pen detect, no pen detect
and a periodic trigger to sample the touchscreen position and pressure.
If the user attempts to use another trigger while there is a buffer
that already requested the touchscreen channels (thus the trigger), the
driver will refuse to comply.

In order to have defines for the channel numbers, I added a bindings include
file that goes on a separate commit :
dt-bindings: iio: adc: at91-sama5d2_adc: add channel specific consumer info
This should go in the same tree with the following commits :
  ARM: dts: at91: sama5d2: add channel cells for ADC device
  ARM: dts: at91: sama5d2: Add resistive touch device

as build will break because these commits depend on the binding one
which creates the included header file.

After the discussions earlier this year on the mailing list, I hope this
rework of the patches is much better and fulfills all the requirements
for this implementation.

Eugen Hristev (10):
  MAINTAINERS: add generic resistive touchscreen adc
  iio: Add channel for Position Relative
  dt-bindings: input: touchscreen: touch_adc: create bindings
  iio: inkern: add module put/get on iio dev module when requesting
    channels
  iio: adc: at91-sama5d2_adc: fix channel configuration for differential
    channels
  iio: adc: at91-sama5d2_adc: add support for position and pressure
    channels
  input: touchscreen: touch_adc: add generic resistive ADC touchscreen
  dt-bindings: iio: adc: at91-sama5d2_adc: add channel specific consumer
    info
  ARM: dts: at91: sama5d2: add channel cells for ADC device
  ARM: dts: at91: sama5d2: Add resistive touch device

 Documentation/ABI/testing/sysfs-bus-iio            |  12 +
 .../bindings/iio/adc/at91-sama5d2_adc.txt          |   9 +
 .../bindings/input/touchscreen/touch_adc.txt       |  33 ++
 MAINTAINERS                                        |   6 +
 arch/arm/boot/dts/sama5d2.dtsi                     |  12 +
 drivers/iio/adc/at91-sama5d2_adc.c                 | 491 ++++++++++++++++++++-
 drivers/iio/industrialio-core.c                    |   1 +
 drivers/iio/inkern.c                               |   8 +-
 drivers/input/touchscreen/Kconfig                  |  13 +
 drivers/input/touchscreen/Makefile                 |   1 +
 drivers/input/touchscreen/touch_adc.c              | 199 +++++++++
 include/dt-bindings/iio/adc/at91-sama5d2_adc.h     |  16 +
 include/uapi/linux/iio/types.h                     |   1 +
 tools/iio/iio_event_monitor.c                      |   2 +
 14 files changed, 791 insertions(+), 13 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/input/touchscreen/touch_adc.txt
 create mode 100644 drivers/input/touchscreen/touch_adc.c
 create mode 100644 include/dt-bindings/iio/adc/at91-sama5d2_adc.h

Comments

Jonathan Cameron March 30, 2018, 12:17 p.m. UTC | #1
On Tue, 27 Mar 2018 15:32:38 +0300
Eugen Hristev <eugen.hristev@microchip.com> wrote:

> When iterating through the channels, the index in the array is not the
> scan index. Added an xlate function to translate to the proper index.
> This will be used also for devicetree channel xlate
> 
> Signed-off-by: Eugen Hristev <eugen.hristev@microchip.com>
So, this is a fix for an existing bug?

Please move this to the front of your set (to make it easier to backport).
Describe the result of the bug as well.

Fixes tag would be good.

Thanks,

Jonathan


> ---
>  drivers/iio/adc/at91-sama5d2_adc.c | 41 ++++++++++++++++++++++++++++++++++----
>  1 file changed, 37 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/iio/adc/at91-sama5d2_adc.c b/drivers/iio/adc/at91-sama5d2_adc.c
> index 4eff835..8729d65 100644
> --- a/drivers/iio/adc/at91-sama5d2_adc.c
> +++ b/drivers/iio/adc/at91-sama5d2_adc.c
> @@ -333,6 +333,27 @@ static const struct iio_chan_spec at91_adc_channels[] = {
>  				+ AT91_SAMA5D2_DIFF_CHAN_CNT + 1),
>  };
>  
> +static int at91_adc_chan_xlate(struct iio_dev *indio_dev, int chan)
> +{
> +	int i;
> +
> +	for (i = 0; i < indio_dev->num_channels; i++) {
> +		if (indio_dev->channels[i].scan_index == chan)
> +			return i;
> +	}
> +	return -EINVAL;
> +}
> +
> +static inline struct iio_chan_spec const *
> +at91_adc_chan_get(struct iio_dev *indio_dev, int chan)
> +{
> +	int index = at91_adc_chan_xlate(indio_dev, chan);
> +
> +	if (index < 0)
> +		return NULL;
> +	return indio_dev->channels + index;
> +}
> +
>  static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
>  {
>  	struct iio_dev *indio = iio_trigger_get_drvdata(trig);
> @@ -350,8 +371,10 @@ static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
>  	at91_adc_writel(st, AT91_SAMA5D2_TRGR, status);
>  
>  	for_each_set_bit(bit, indio->active_scan_mask, indio->num_channels) {
> -		struct iio_chan_spec const *chan = indio->channels + bit;
> +		struct iio_chan_spec const *chan = at91_adc_chan_get(indio, bit);
>  
> +		if (!chan)
> +			continue;
>  		if (state) {
>  			at91_adc_writel(st, AT91_SAMA5D2_CHER,
>  					BIT(chan->channel));
> @@ -448,7 +471,11 @@ static int at91_adc_dma_start(struct iio_dev *indio_dev)
>  
>  	for_each_set_bit(bit, indio_dev->active_scan_mask,
>  			 indio_dev->num_channels) {
> -		struct iio_chan_spec const *chan = indio_dev->channels + bit;
> +		struct iio_chan_spec const *chan =
> +					 at91_adc_chan_get(indio_dev, bit);
> +
> +		if (!chan)
> +			continue;
>  
>  		st->dma_st.rx_buf_sz += chan->scan_type.storagebits / 8;
>  	}
> @@ -526,8 +553,11 @@ static int at91_adc_buffer_predisable(struct iio_dev *indio_dev)
>  	 */
>  	for_each_set_bit(bit, indio_dev->active_scan_mask,
>  			 indio_dev->num_channels) {
> -		struct iio_chan_spec const *chan = indio_dev->channels + bit;
> +		struct iio_chan_spec const *chan =
> +					at91_adc_chan_get(indio_dev, bit);
>  
> +		if (!chan)
> +			continue;
>  		if (st->dma_st.dma_chan)
>  			at91_adc_readl(st, chan->address);
>  	}
> @@ -587,8 +617,11 @@ static void at91_adc_trigger_handler_nodma(struct iio_dev *indio_dev,
>  
>  	for_each_set_bit(bit, indio_dev->active_scan_mask,
>  			 indio_dev->num_channels) {
> -		struct iio_chan_spec const *chan = indio_dev->channels + bit;
> +		struct iio_chan_spec const *chan =
> +					at91_adc_chan_get(indio_dev, bit);
>  
> +		if (!chan)
> +			continue;
>  		st->buffer[i] = at91_adc_readl(st, chan->address);
>  		i++;
>  	}

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Jonathan Cameron March 30, 2018, 12:47 p.m. UTC | #2
On Tue, 27 Mar 2018 15:32:39 +0300
Eugen Hristev <eugen.hristev@microchip.com> wrote:

> This implements the support for position and pressure for the included
> touchscreen support in the SAMA5D2 SOC ADC block.
> Two position channels are added and one for pressure.
> They can be read in raw format, or through a buffer.
> A normal use case is for a consumer driver to register a callback buffer
> for these channels.
> When the touchscreen channels are in the active scan mask,
> the driver will start the touchscreen sampling and push the data to the
> buffer.
> 
> Some parts of this patch are based on initial original work by
> Mohamed Jamsheeth Hajanajubudeen and Bandaru Venkateswara Swamy
> 
> Signed-off-by: Eugen Hristev <eugen.hristev@microchip.com>
Looks pretty good to me.  A few minor comments inline.

Jonathan

> ---
> Changes in v2:
>  - the support is now based on callback buffer.
> 
>  drivers/iio/adc/at91-sama5d2_adc.c | 452 ++++++++++++++++++++++++++++++++++++-
>  1 file changed, 443 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/iio/adc/at91-sama5d2_adc.c b/drivers/iio/adc/at91-sama5d2_adc.c
> index 8729d65..29a7fb9 100644
> --- a/drivers/iio/adc/at91-sama5d2_adc.c
> +++ b/drivers/iio/adc/at91-sama5d2_adc.c
> @@ -102,14 +102,26 @@
>  #define AT91_SAMA5D2_LCDR	0x20
>  /* Interrupt Enable Register */
>  #define AT91_SAMA5D2_IER	0x24
> +/* Interrupt Enable Register - TS X measurement ready */
> +#define AT91_SAMA5D2_IER_XRDY   BIT(20)
> +/* Interrupt Enable Register - TS Y measurement ready */
> +#define AT91_SAMA5D2_IER_YRDY   BIT(21)
> +/* Interrupt Enable Register - TS pressure measurement ready */
> +#define AT91_SAMA5D2_IER_PRDY   BIT(22)
>  /* Interrupt Enable Register - general overrun error */
>  #define AT91_SAMA5D2_IER_GOVRE BIT(25)
> +/* Interrupt Enable Register - Pen detect */
> +#define AT91_SAMA5D2_IER_PEN    BIT(29)
> +/* Interrupt Enable Register - No pen detect */
> +#define AT91_SAMA5D2_IER_NOPEN  BIT(30)
>  /* Interrupt Disable Register */
>  #define AT91_SAMA5D2_IDR	0x28
>  /* Interrupt Mask Register */
>  #define AT91_SAMA5D2_IMR	0x2c
>  /* Interrupt Status Register */
>  #define AT91_SAMA5D2_ISR	0x30
> +/* Interrupt Status Register - Pen touching sense status */
> +#define AT91_SAMA5D2_ISR_PENS   BIT(31)
>  /* Last Channel Trigger Mode Register */
>  #define AT91_SAMA5D2_LCTMR	0x34
>  /* Last Channel Compare Window Register */
> @@ -131,8 +143,38 @@
>  #define AT91_SAMA5D2_CDR0	0x50
>  /* Analog Control Register */
>  #define AT91_SAMA5D2_ACR	0x94
> +/* Analog Control Register - Pen detect sensitivity mask */
> +#define AT91_SAMA5D2_ACR_PENDETSENS_MASK        GENMASK(1, 0)
> +
>  /* Touchscreen Mode Register */
>  #define AT91_SAMA5D2_TSMR	0xb0
> +/* Touchscreen Mode Register - No touch mode */
> +#define AT91_SAMA5D2_TSMR_TSMODE_NONE           0
> +/* Touchscreen Mode Register - 4 wire screen, no pressure measurement */
> +#define AT91_SAMA5D2_TSMR_TSMODE_4WIRE_NO_PRESS 1
> +/* Touchscreen Mode Register - 4 wire screen, pressure measurement */
> +#define AT91_SAMA5D2_TSMR_TSMODE_4WIRE_PRESS    2
> +/* Touchscreen Mode Register - 5 wire screen */
> +#define AT91_SAMA5D2_TSMR_TSMODE_5WIRE          3
> +/* Touchscreen Mode Register - Average samples mask */
> +#define AT91_SAMA5D2_TSMR_TSAV_MASK             GENMASK(5, 4)
> +/* Touchscreen Mode Register - Average samples */
> +#define AT91_SAMA5D2_TSMR_TSAV(x)               ((x) << 4)
> +/* Touchscreen Mode Register - Touch/trigger frequency ratio mask */
> +#define AT91_SAMA5D2_TSMR_TSFREQ_MASK           GENMASK(11, 8)
> +/* Touchscreen Mode Register - Touch/trigger frequency ratio */
> +#define AT91_SAMA5D2_TSMR_TSFREQ(x)             ((x) << 8)
> +/* Touchscreen Mode Register - Pen Debounce Time mask */
> +#define AT91_SAMA5D2_TSMR_PENDBC_MASK           GENMASK(31, 28)
> +/* Touchscreen Mode Register - Pen Debounce Time */
> +#define AT91_SAMA5D2_TSMR_PENDBC(x)            ((x) << 28)
> +/* Touchscreen Mode Register - No DMA for touch measurements */
> +#define AT91_SAMA5D2_TSMR_NOTSDMA               BIT(22)
> +/* Touchscreen Mode Register - Disable pen detection */
> +#define AT91_SAMA5D2_TSMR_PENDET_DIS            (0 << 24)
> +/* Touchscreen Mode Register - Enable pen detection */
> +#define AT91_SAMA5D2_TSMR_PENDET_ENA            BIT(24)
> +
>  /* Touchscreen X Position Register */
>  #define AT91_SAMA5D2_XPOSR	0xb4
>  /* Touchscreen Y Position Register */
> @@ -151,6 +193,12 @@
>  #define AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_FALL 2
>  /* Trigger Mode external trigger any edge */
>  #define AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_ANY 3
> +/* Trigger Mode internal periodic */
> +#define AT91_SAMA5D2_TRGR_TRGMOD_PERIODIC 5
> +/* Trigger Mode - trigger period mask */
> +#define AT91_SAMA5D2_TRGR_TRGPER_MASK           GENMASK(31, 16)
> +/* Trigger Mode - trigger period */
> +#define AT91_SAMA5D2_TRGR_TRGPER(x)             ((x) << 16)
>  
>  /* Correction Select Register */
>  #define AT91_SAMA5D2_COSR	0xd0
> @@ -169,6 +217,22 @@
>  #define AT91_SAMA5D2_SINGLE_CHAN_CNT 12
>  #define AT91_SAMA5D2_DIFF_CHAN_CNT 6
>  
> +#define AT91_SAMA5D2_TIMESTAMP_CHAN_IDX (AT91_SAMA5D2_SINGLE_CHAN_CNT + \
> +					 AT91_SAMA5D2_DIFF_CHAN_CNT + 1)
> +
> +#define AT91_SAMA5D2_TOUCH_X_CHAN_IDX (AT91_SAMA5D2_SINGLE_CHAN_CNT + \
> +					 AT91_SAMA5D2_DIFF_CHAN_CNT * 2)
> +#define AT91_SAMA5D2_TOUCH_Y_CHAN_IDX   (AT91_SAMA5D2_TOUCH_X_CHAN_IDX + 1)
> +#define AT91_SAMA5D2_TOUCH_P_CHAN_IDX   (AT91_SAMA5D2_TOUCH_Y_CHAN_IDX + 1)
> +#define AT91_SAMA5D2_MAX_CHAN_IDX	AT91_SAMA5D2_TOUCH_P_CHAN_IDX
> +
> +#define TOUCH_SAMPLE_PERIOD_US          2000    /* 2ms */
> +#define TOUCH_PEN_DETECT_DEBOUNCE_US    200
> +
> +#define XYZ_MASK			GENMASK(11, 0)
> +
> +#define MAX_POS_BITS			12
> +
Please prefix these defines as the chances of a clash with these generic names
is very high.

>  /*
>   * Maximum number of bytes to hold conversion from all channels
>   * without the timestamp.
> @@ -222,6 +286,37 @@
>  		.indexed = 1,						\
>  	}
>  
> +#define AT91_SAMA5D2_CHAN_TOUCH(num, name, mod)				\
> +	{								\
> +		.type = IIO_POSITIONRELATIVE,				\
> +		.modified = 1,						\
> +		.channel = num,						\
> +		.channel2 = mod,					\
> +		.scan_index = num,					\
> +		.scan_type = {						\
> +			.sign = 'u',					\
> +			.realbits = 12,					\
> +			.storagebits = 16,				\
> +		},							\
> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
> +		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\
> +		.datasheet_name = name,					\
> +	}
> +#define AT91_SAMA5D2_CHAN_PRESSURE(num, name)				\
> +	{								\
> +		.type = IIO_PRESSURE,					\
> +		.channel = num,						\
> +		.scan_index = num,					\
> +		.scan_type = {						\
> +			.sign = 'u',					\
> +			.realbits = 12,					\
> +			.storagebits = 16,				\
> +		},							\
> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
> +		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\
> +		.datasheet_name = name,					\
> +	}
> +
>  #define at91_adc_readl(st, reg)		readl_relaxed(st->base + reg)
>  #define at91_adc_writel(st, reg, val)	writel_relaxed(val, st->base + reg)
>  
> @@ -260,6 +355,22 @@ struct at91_adc_dma {
>  	s64				dma_ts;
>  };
>  
> +/**
> + * at91_adc_touch - at91-sama5d2 touchscreen information struct
> + * @sample_period_val:		the value for periodic trigger interval
> + * @touching:			is the pen touching the screen or not
> + * @x_pos:			temporary placeholder for pressure computation
> + * @channels_bitmask:		bitmask with the touchscreen channels enabled
> + * @workq:			workqueue for buffer data pushing
> + */
> +struct at91_adc_touch {
> +	u16				sample_period_val;
> +	bool				touching;
> +	u16				x_pos;
> +	unsigned long			channels_bitmask;
> +	struct work_struct		workq;
> +};
> +
>  struct at91_adc_state {
>  	void __iomem			*base;
>  	int				irq;
> @@ -267,6 +378,7 @@ struct at91_adc_state {
>  	struct regulator		*reg;
>  	struct regulator		*vref;
>  	int				vref_uv;
> +	unsigned int			current_sample_rate;
>  	struct iio_trigger		*trig;
>  	const struct at91_adc_trigger	*selected_trig;
>  	const struct iio_chan_spec	*chan;
> @@ -275,6 +387,7 @@ struct at91_adc_state {
>  	struct at91_adc_soc_info	soc_info;
>  	wait_queue_head_t		wq_data_available;
>  	struct at91_adc_dma		dma_st;
> +	struct at91_adc_touch		touch_st;
>  	u16				buffer[AT91_BUFFER_MAX_HWORDS];
>  	/*
>  	 * lock to prevent concurrent 'single conversion' requests through
> @@ -329,8 +442,10 @@ static const struct iio_chan_spec at91_adc_channels[] = {
>  	AT91_SAMA5D2_CHAN_DIFF(6, 7, 0x68),
>  	AT91_SAMA5D2_CHAN_DIFF(8, 9, 0x70),
>  	AT91_SAMA5D2_CHAN_DIFF(10, 11, 0x78),
> -	IIO_CHAN_SOFT_TIMESTAMP(AT91_SAMA5D2_SINGLE_CHAN_CNT
> -				+ AT91_SAMA5D2_DIFF_CHAN_CNT + 1),
> +	IIO_CHAN_SOFT_TIMESTAMP(AT91_SAMA5D2_TIMESTAMP_CHAN_IDX),
> +	AT91_SAMA5D2_CHAN_TOUCH(AT91_SAMA5D2_TOUCH_X_CHAN_IDX, "x", IIO_MOD_X),
> +	AT91_SAMA5D2_CHAN_TOUCH(AT91_SAMA5D2_TOUCH_Y_CHAN_IDX, "y", IIO_MOD_Y),
> +	AT91_SAMA5D2_CHAN_PRESSURE(AT91_SAMA5D2_TOUCH_P_CHAN_IDX, "pressure"),
>  };
>  
>  static int at91_adc_chan_xlate(struct iio_dev *indio_dev, int chan)
> @@ -354,6 +469,162 @@ at91_adc_chan_get(struct iio_dev *indio_dev, int chan)
>  	return indio_dev->channels + index;
>  }
>  
> +static inline int at91_adc_of_xlate(struct iio_dev *indio_dev,
> +				    const struct of_phandle_args *iiospec)
> +{
> +	return at91_adc_chan_xlate(indio_dev, iiospec->args[0]);
> +}
> +
> +static int at91_adc_configure_touch(struct at91_adc_state *st, bool state)
> +{
> +	u32 clk_khz = st->current_sample_rate / 1000;
> +	int i = 0;
> +	u16 pendbc;
> +	u32 tsmr, acr;
> +
> +	if (!state) {
> +		/* disabling touch IRQs and setting mode to no touch enabled */
> +		at91_adc_writel(st, AT91_SAMA5D2_IDR,
> +				AT91_SAMA5D2_IER_PEN | AT91_SAMA5D2_IER_NOPEN);
> +		at91_adc_writel(st, AT91_SAMA5D2_TSMR, 0);
> +		return 0;
> +	}
> +	/*
> +	 * debounce time is in microseconds, we need it in milliseconds to
> +	 * multiply with kilohertz, so, divide by 1000, but after the multiply.
> +	 * round up to make sure pendbc is at least 1
> +	 */
> +	pendbc = round_up(TOUCH_PEN_DETECT_DEBOUNCE_US * clk_khz / 1000, 1);
> +
> +	/* get the required exponent */
> +	while (pendbc >> i++)
> +		;
> +
> +	pendbc = i;
> +
> +	tsmr = AT91_SAMA5D2_TSMR_TSMODE_4WIRE_PRESS;
> +
> +	tsmr |= AT91_SAMA5D2_TSMR_TSAV(2) & AT91_SAMA5D2_TSMR_TSAV_MASK;
> +	tsmr |= AT91_SAMA5D2_TSMR_PENDBC(pendbc) &
> +		AT91_SAMA5D2_TSMR_PENDBC_MASK;
> +	tsmr |= AT91_SAMA5D2_TSMR_NOTSDMA;
> +	tsmr |= AT91_SAMA5D2_TSMR_PENDET_ENA;
> +	tsmr |= AT91_SAMA5D2_TSMR_TSFREQ(2) & AT91_SAMA5D2_TSMR_TSFREQ_MASK;
> +
> +	at91_adc_writel(st, AT91_SAMA5D2_TSMR, tsmr);
> +
> +	acr =  at91_adc_readl(st, AT91_SAMA5D2_ACR);
> +	acr &= ~AT91_SAMA5D2_ACR_PENDETSENS_MASK;
> +	acr |= 0x02 & AT91_SAMA5D2_ACR_PENDETSENS_MASK;
> +	at91_adc_writel(st, AT91_SAMA5D2_ACR, acr);
> +
> +	/* Sample Period Time = (TRGPER + 1) / ADCClock */
> +	st->touch_st.sample_period_val = round_up((TOUCH_SAMPLE_PERIOD_US *
> +					 clk_khz / 1000) - 1, 1);
> +	/* enable pen detect IRQ */
> +	at91_adc_writel(st, AT91_SAMA5D2_IER, AT91_SAMA5D2_IER_PEN);
> +
> +	return 0;
> +}
> +
> +static u16 at91_adc_touch_x_pos(struct at91_adc_state *st)
> +{
> +	u32 val;
> +	u32 xscale, x, xpos;
> +
> +	/* x position = (x / xscale) * max, max = 2^MAX_POS_BITS - 1 */
> +	val = at91_adc_readl(st, AT91_SAMA5D2_XPOSR);
> +	if (!val)
> +		dev_dbg(&iio_priv_to_dev(st)->dev, "x_pos is 0\n");
> +
> +	xpos = val & XYZ_MASK;
> +	x = (xpos << MAX_POS_BITS) - xpos;
> +	xscale = (val >> 16) & XYZ_MASK;
> +	if (xscale == 0) {
> +		dev_err(&iio_priv_to_dev(st)->dev, "xscale is 0\n");
> +		return 0;
> +	}
> +	x /= xscale;
> +	st->touch_st.x_pos = x;
> +
> +	return x;
> +}
> +
> +static u16 at91_adc_touch_y_pos(struct at91_adc_state *st)
> +{
> +	u32 val;
> +	u32 yscale, y, ypos;
> +
> +	/* y position = (y / yscale) * max, max = 2^MAX_POS_BITS - 1 */
> +	val = at91_adc_readl(st, AT91_SAMA5D2_YPOSR);
> +	if (!val)
> +		dev_dbg(&iio_priv_to_dev(st)->dev, "y_pos is 0\n");
> +
> +	ypos = val & XYZ_MASK;
> +	y = (ypos << MAX_POS_BITS) - ypos;
> +	yscale = (val >> 16) & XYZ_MASK;
> +
> +	if (yscale == 0) {
> +		dev_err(&iio_priv_to_dev(st)->dev, "yscale is 0\n");
> +		return 0;
> +	}
> +
> +	y /= yscale;
> +
> +	return y;
This is obviously very similar to the above and feels like you need
some wrappers around a single function?
Just the xpos store needs to be outside the shared function.

> +}
> +
> +static u16 at91_adc_touch_pressure(struct at91_adc_state *st)
> +{
> +	u32 val;
> +	u32 z1, z2;
> +	u32 pres;
> +	u32 rxp = 1;
> +	u32 factor = 1000;
> +
> +	/* calculate the pressure */
> +	val = at91_adc_readl(st, AT91_SAMA5D2_PRESSR);
> +	z1 = val & XYZ_MASK;
> +	z2 = (val >> 16) & XYZ_MASK;
> +
> +	if (z1 != 0)
> +		pres = rxp * (st->touch_st.x_pos * factor / 1024) *
> +			(z2 * factor / z1 - factor) /
> +			factor;
> +	else
> +		pres = 0xFFFF;       /* no pen contact */
Would a pressure value of 0 not make more sense?

> +
> +	return pres;
> +}
> +
> +static int at91_adc_read_position(struct at91_adc_state *st, int chan, u16 *val)
> +{
> +	*val = 0;
> +	if (!st->touch_st.touching)
> +		return -ENODATA;
> +	if (chan == AT91_SAMA5D2_TOUCH_X_CHAN_IDX)
> +		*val = at91_adc_touch_x_pos(st);
> +	else if (chan == AT91_SAMA5D2_TOUCH_Y_CHAN_IDX)
> +		*val = at91_adc_touch_y_pos(st);
> +	else
> +		return -ENODATA;
> +
> +	return IIO_VAL_INT;
> +}
> +
> +static int at91_adc_read_pressure(struct at91_adc_state *st, int chan, u16 *val)
> +{
> +	*val = 0;
> +	if (!st->touch_st.touching)
> +		return -ENODATA;
> +	if (chan == AT91_SAMA5D2_TOUCH_P_CHAN_IDX)
> +		*val = at91_adc_touch_pressure(st);
> +	else
> +		return -ENODATA;
> +
> +	return IIO_VAL_INT;
> +}
> +
>  static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
>  {
>  	struct iio_dev *indio = iio_trigger_get_drvdata(trig);
> @@ -373,7 +644,7 @@ static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
>  	for_each_set_bit(bit, indio->active_scan_mask, indio->num_channels) {
>  		struct iio_chan_spec const *chan = at91_adc_chan_get(indio, bit);
>  
> -		if (!chan)
> +		if (!chan || chan->type != IIO_VOLTAGE)
I think this would be more robust if we had it matching what it was rather
than what it wasn't.  Otherwise we'll get a temperature sensor on this
part at somepoint and it'll break.

>  			continue;
>  		if (state) {
>  			at91_adc_writel(st, AT91_SAMA5D2_CHER,
> @@ -520,7 +791,17 @@ static int at91_adc_dma_start(struct iio_dev *indio_dev)
>  static int at91_adc_buffer_postenable(struct iio_dev *indio_dev)
>  {
>  	int ret;
> +	struct at91_adc_state *st = iio_priv(indio_dev);
>  
> +	/* check if we are enabling triggered buffer or the touchscreen */
> +	if (bitmap_subset(indio_dev->active_scan_mask,
> +			  &st->touch_st.channels_bitmask,
> +			  AT91_SAMA5D2_MAX_CHAN_IDX + 1)) {
> +		/* touchscreen enabling */
> +		at91_adc_configure_touch(st, true);
Given you have that returning a value (be it always 0 I think)
better to pass that on here as well.

> +		return 0;
> +	}
> +	/* we continue with the triggered buffer */
>  	ret = at91_adc_dma_start(indio_dev);
>  	if (ret) {
>  		dev_err(&indio_dev->dev, "buffer postenable failed\n");
> @@ -536,6 +817,16 @@ static int at91_adc_buffer_predisable(struct iio_dev *indio_dev)
>  	int ret;
>  	u8 bit;
>  
> +	/* check if we are disabling triggered buffer or the touchscreen */
> +	if (bitmap_subset(indio_dev->active_scan_mask,
> +			  &st->touch_st.channels_bitmask,
> +			  AT91_SAMA5D2_MAX_CHAN_IDX + 1)) {
> +		/* touchscreen disable */
> +		at91_adc_configure_touch(st, false);
> +		return 0;
> +	}
> +
> +	/* continue with the triggered buffer */
>  	ret = iio_triggered_buffer_predisable(indio_dev);
>  	if (ret < 0)
>  		dev_err(&indio_dev->dev, "buffer predisable failed\n");
> @@ -556,7 +847,7 @@ static int at91_adc_buffer_predisable(struct iio_dev *indio_dev)
>  		struct iio_chan_spec const *chan =
>  					at91_adc_chan_get(indio_dev, bit);
>  
> -		if (!chan)
> +		if (!chan && chan->type != IIO_VOLTAGE)
>  			continue;
>  		if (st->dma_st.dma_chan)
>  			at91_adc_readl(st, chan->address);
> @@ -622,7 +913,10 @@ static void at91_adc_trigger_handler_nodma(struct iio_dev *indio_dev,
>  
>  		if (!chan)
>  			continue;
> -		st->buffer[i] = at91_adc_readl(st, chan->address);
> +		if (chan->type == IIO_VOLTAGE)
> +			st->buffer[i] = at91_adc_readl(st, chan->address);
> +		else
> +			st->buffer[i] = 0;
Please explain this one with a comment.

>  		i++;
>  	}
>  	iio_push_to_buffers_with_timestamp(indio_dev, st->buffer,
> @@ -736,6 +1030,7 @@ static void at91_adc_setup_samp_freq(struct at91_adc_state *st, unsigned freq)
>  
>  	dev_dbg(&indio_dev->dev, "freq: %u, startup: %u, prescal: %u\n",
>  		freq, startup, prescal);
> +	st->current_sample_rate = freq;
>  }
>  
>  static unsigned at91_adc_get_sample_freq(struct at91_adc_state *st)
> @@ -751,23 +1046,109 @@ static unsigned at91_adc_get_sample_freq(struct at91_adc_state *st)
If st->current_sample_rate is always valid, then
can we just use that for the get call?

>  	return f_adc;
>  }
>  
> +static void at91_adc_touch_data_handler(struct iio_dev *indio_dev)
> +{
> +	struct at91_adc_state *st = iio_priv(indio_dev);
> +	u8 bit;
> +	u16 val;
> +	int i = 0;
> +
> +	for_each_set_bit(bit, indio_dev->active_scan_mask,
> +			 AT91_SAMA5D2_MAX_CHAN_IDX + 1) {
> +		struct iio_chan_spec const *chan =
> +					 at91_adc_chan_get(indio_dev, bit);
> +
> +		if (chan->type == IIO_POSITIONRELATIVE)
> +			at91_adc_read_position(st, chan->channel, &val);
> +		else if (chan->type == IIO_PRESSURE)
> +			at91_adc_read_pressure(st, chan->channel, &val);
> +		else
> +			continue;
> +		st->buffer[i] = val;
> +		i++;
> +	}
> +	/* schedule work to push to buffers */
I would expand the comment to say why you don't just do it directly.

> +	schedule_work(&st->touch_st.workq);
> +}
> +
> +static void at91_adc_pen_detect_interrupt(struct at91_adc_state *st)
> +{
> +	at91_adc_writel(st, AT91_SAMA5D2_IDR, AT91_SAMA5D2_IER_PEN);
> +	at91_adc_writel(st, AT91_SAMA5D2_IER, AT91_SAMA5D2_IER_NOPEN |
> +			AT91_SAMA5D2_IER_XRDY | AT91_SAMA5D2_IER_YRDY |
> +			AT91_SAMA5D2_IER_PRDY);
> +	at91_adc_writel(st, AT91_SAMA5D2_TRGR,
> +			AT91_SAMA5D2_TRGR_TRGMOD_PERIODIC |
> +			AT91_SAMA5D2_TRGR_TRGPER(st->touch_st.sample_period_val));
> +	st->touch_st.touching = true;
> +}
> +
> +static void at91_adc_no_pen_detect_interrupt(struct at91_adc_state *st)
> +{
> +	struct iio_dev *indio_dev = iio_priv_to_dev(st);
> +
> +	at91_adc_writel(st, AT91_SAMA5D2_TRGR,
> +			AT91_SAMA5D2_TRGR_TRGMOD_NO_TRIGGER);
> +	at91_adc_writel(st, AT91_SAMA5D2_IDR, AT91_SAMA5D2_IER_NOPEN |
> +			AT91_SAMA5D2_IER_XRDY | AT91_SAMA5D2_IER_YRDY |
> +			AT91_SAMA5D2_IER_PRDY);
> +	st->touch_st.touching = false;
> +
> +	at91_adc_touch_data_handler(indio_dev);
> +
> +	at91_adc_writel(st, AT91_SAMA5D2_IER, AT91_SAMA5D2_IER_PEN);
> +}
> +
> +static void at91_adc_workq_handler(struct work_struct *workq)
> +{
> +	struct at91_adc_touch *touch_st = container_of(workq,
> +					struct at91_adc_touch, workq);
> +	struct at91_adc_state *st = container_of(touch_st,
> +					struct at91_adc_state, touch_st);
> +	struct iio_dev *indio_dev = iio_priv_to_dev(st);
> +
> +	iio_push_to_buffers(indio_dev, st->buffer);
> +}
> +
>  static irqreturn_t at91_adc_interrupt(int irq, void *private)
>  {
>  	struct iio_dev *indio = private;
>  	struct at91_adc_state *st = iio_priv(indio);
>  	u32 status = at91_adc_readl(st, AT91_SAMA5D2_ISR);
>  	u32 imr = at91_adc_readl(st, AT91_SAMA5D2_IMR);
> +	u32 rdy_mask = AT91_SAMA5D2_IER_XRDY | AT91_SAMA5D2_IER_YRDY |
> +			AT91_SAMA5D2_IER_PRDY;
>  
>  	if (!(status & imr))
>  		return IRQ_NONE;
> -
> -	if (iio_buffer_enabled(indio) && !st->dma_st.dma_chan) {
> +	if (status & AT91_SAMA5D2_IER_PEN) {
> +		/* pen detected IRQ */
> +		at91_adc_pen_detect_interrupt(st);
> +	} else if ((status & AT91_SAMA5D2_IER_NOPEN)) {
> +		/* nopen detected IRQ */
> +		at91_adc_no_pen_detect_interrupt(st);
> +	} else if ((status & AT91_SAMA5D2_ISR_PENS) &&
> +		   ((status & rdy_mask) == rdy_mask)) {
> +		/* periodic trigger IRQ - during pen sense */
> +		at91_adc_touch_data_handler(indio);
> +	} else if (status & AT91_SAMA5D2_ISR_PENS) {
> +		/*
> +		 * touching, but the measurements are not ready yet.
> +		 * read and ignore.
> +		 */
> +		status = at91_adc_readl(st, AT91_SAMA5D2_XPOSR);
> +		status = at91_adc_readl(st, AT91_SAMA5D2_YPOSR);
> +		status = at91_adc_readl(st, AT91_SAMA5D2_PRESSR);
> +	} else if (iio_buffer_enabled(indio) && !st->dma_st.dma_chan) {
> +		/* triggered buffer without DMA */
>  		disable_irq_nosync(irq);
>  		iio_trigger_poll(indio->trig);
>  	} else if (iio_buffer_enabled(indio) && st->dma_st.dma_chan) {
> +		/* triggered buffer with DMA - should not happen */
>  		disable_irq_nosync(irq);
>  		WARN(true, "Unexpected irq occurred\n");
>  	} else if (!iio_buffer_enabled(indio)) {
> +		/* software requested conversion */
>  		st->conversion_value = at91_adc_readl(st, st->chan->address);
>  		st->conversion_done = true;
>  		wake_up_interruptible(&st->wq_data_available);
> @@ -791,6 +1172,21 @@ static int at91_adc_read_raw(struct iio_dev *indio_dev,
>  			return ret;
>  		mutex_lock(&st->lock);
>  
> +		if (chan->type == IIO_POSITIONRELATIVE) {
> +			ret = at91_adc_read_position(st,
> +						     chan->channel, (u16 *)val);
Odd line breaking, I'd expect chan->channel on the first line..

These exit paths are a bit ugly. Perhaps it is time to pull the
INFO_RAW block out as a utility function. Then the indenting will 
be less allowing a simple single exit point with the unlocking all
in one place.

> +			mutex_unlock(&st->lock);
> +			iio_device_release_direct_mode(indio_dev);
> +			return ret;
> +		}
> +		if (chan->type == IIO_PRESSURE) {
> +			ret = at91_adc_read_pressure(st,
> +						     chan->channel, (u16 *)val);
> +			mutex_unlock(&st->lock);
> +			iio_device_release_direct_mode(indio_dev);
> +			return ret;
> +		}
> +
>  		st->chan = chan;
>  
>  		if (chan->differential)
> @@ -974,9 +1370,29 @@ static int at91_adc_set_watermark(struct iio_dev *indio_dev, unsigned int val)
>  	return 0;
>  }
>  
> +static int at91_adc_update_scan_mode(struct iio_dev *indio_dev,
> +				     const unsigned long *scan_mask)
> +{
> +	struct at91_adc_state *st = iio_priv(indio_dev);
> +
> +	if (bitmap_subset(scan_mask, &st->touch_st.channels_bitmask,
> +			  AT91_SAMA5D2_MAX_CHAN_IDX + 1))
> +		return 0;
> +	/*
> +	 * if the new bitmap is a combination of touchscreen and regular
> +	 * channels, then we are not fine
> +	 */
> +	if (bitmap_intersects(&st->touch_st.channels_bitmask, scan_mask,
> +			      AT91_SAMA5D2_MAX_CHAN_IDX + 1))
> +		return -EBUSY;
> +	return 0;
> +}
> +
>  static const struct iio_info at91_adc_info = {
>  	.read_raw = &at91_adc_read_raw,
>  	.write_raw = &at91_adc_write_raw,
> +	.update_scan_mode = &at91_adc_update_scan_mode,
> +	.of_xlate = &at91_adc_of_xlate,
>  	.hwfifo_set_watermark = &at91_adc_set_watermark,
>  };
>  
> @@ -1044,13 +1460,20 @@ static int at91_adc_probe(struct platform_device *pdev)
>  
>  	indio_dev->dev.parent = &pdev->dev;
>  	indio_dev->name = dev_name(&pdev->dev);
> -	indio_dev->modes = INDIO_DIRECT_MODE;
> +	indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
>  	indio_dev->info = &at91_adc_info;
>  	indio_dev->channels = at91_adc_channels;
>  	indio_dev->num_channels = ARRAY_SIZE(at91_adc_channels);
>  
>  	st = iio_priv(indio_dev);
>  
> +	bitmap_set(&st->touch_st.channels_bitmask,
> +		   AT91_SAMA5D2_TOUCH_X_CHAN_IDX, 1);
> +	bitmap_set(&st->touch_st.channels_bitmask,
> +		   AT91_SAMA5D2_TOUCH_Y_CHAN_IDX, 1);
> +	bitmap_set(&st->touch_st.channels_bitmask,
> +		   AT91_SAMA5D2_TOUCH_P_CHAN_IDX, 1);
> +
>  	ret = of_property_read_u32(pdev->dev.of_node,
>  				   "atmel,min-sample-rate-hz",
>  				   &st->soc_info.min_sample_rate);
> @@ -1100,6 +1523,7 @@ static int at91_adc_probe(struct platform_device *pdev)
>  
>  	init_waitqueue_head(&st->wq_data_available);
>  	mutex_init(&st->lock);
> +	INIT_WORK(&st->touch_st.workq, at91_adc_workq_handler);
>  
>  	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
>  	if (!res)
> @@ -1272,8 +1696,18 @@ static __maybe_unused int at91_adc_resume(struct device *dev)
>  	at91_adc_hw_init(st);
>  
>  	/* reconfiguring trigger hardware state */
> -	if (iio_buffer_enabled(indio_dev))
> +	if (!iio_buffer_enabled(indio_dev))
> +		return 0;
> +
> +	/* check if we are enabling triggered buffer or the touchscreen */
> +	if (bitmap_subset(indio_dev->active_scan_mask,
> +			  &st->touch_st.channels_bitmask,
> +			  AT91_SAMA5D2_MAX_CHAN_IDX + 1)) {
> +		/* touchscreen enabling */
> +		at91_adc_configure_touch(st, true);
> +	} else {
>  		at91_adc_configure_trigger(st->trig, true);
> +	}
>  
>  	return 0;
>  

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Jonathan Cameron March 30, 2018, 12:58 p.m. UTC | #3
On Tue, 27 Mar 2018 15:32:40 +0300
Eugen Hristev <eugen.hristev@microchip.com> wrote:

> This adds a generic resistive touchscreen (GRTS) driver, which is based
> on an IIO device (an ADC). It must be connected to the channels of an ADC
> to receive touch data. Then it will feed the data into the input subsystem
> where it registers an input device.
> It uses an IIO callback buffer to register to the IIO device
> 
> Some parts of this patch are based on initial original work by
> Mohamed Jamsheeth Hajanajubudeen and Bandaru Venkateswara Swamy
> 
> Signed-off-by: Eugen Hristev <eugen.hristev@microchip.com>
I like this a lot!

A few minor bits and bobs inline.  Over to Dmitry for the input
side of things.

Jonathan

> ---
> Changes in v2:
>  - this is now a generic resistive adc touchscreen driver
> 
>  drivers/input/touchscreen/Kconfig     |  13 +++
>  drivers/input/touchscreen/Makefile    |   1 +
>  drivers/input/touchscreen/touch_adc.c | 199 ++++++++++++++++++++++++++++++++++
>  3 files changed, 213 insertions(+)
>  create mode 100644 drivers/input/touchscreen/touch_adc.c
> 
> diff --git a/drivers/input/touchscreen/Kconfig b/drivers/input/touchscreen/Kconfig
> index 4f15496..afd879f 100644
> --- a/drivers/input/touchscreen/Kconfig
> +++ b/drivers/input/touchscreen/Kconfig
> @@ -92,6 +92,19 @@ config TOUCHSCREEN_AD7879_SPI
>  	  To compile this driver as a module, choose M here: the
>  	  module will be called ad7879-spi.
>  
> +config TOUCHSCREEN_ADC
> +	tristate "Generic ADC based resistive touchscreen"
> +	depends on IIO
> +	select IIO_BUFFER_CB
> +	help
> +	  Say Y here if you want to use the generic ADC
> +	  resistive touchscreen driver.
> +
> +	  If unsure, say N (but it's safe to say "Y").
> +
> +	  To compile this driver as a module, choose M here: the
> +	  module will be called touch_adc.ko.
> +
>  config TOUCHSCREEN_AR1021_I2C
>  	tristate "Microchip AR1020/1021 i2c touchscreen"
>  	depends on I2C && OF
> diff --git a/drivers/input/touchscreen/Makefile b/drivers/input/touchscreen/Makefile
> index dddae79..cbe6121 100644
> --- a/drivers/input/touchscreen/Makefile
> +++ b/drivers/input/touchscreen/Makefile
> @@ -13,6 +13,7 @@ obj-$(CONFIG_TOUCHSCREEN_AD7877)	+= ad7877.o
>  obj-$(CONFIG_TOUCHSCREEN_AD7879)	+= ad7879.o
>  obj-$(CONFIG_TOUCHSCREEN_AD7879_I2C)	+= ad7879-i2c.o
>  obj-$(CONFIG_TOUCHSCREEN_AD7879_SPI)	+= ad7879-spi.o
> +obj-$(CONFIG_TOUCHSCREEN_ADC)		+= touch_adc.o
>  obj-$(CONFIG_TOUCHSCREEN_ADS7846)	+= ads7846.o
>  obj-$(CONFIG_TOUCHSCREEN_AR1021_I2C)	+= ar1021_i2c.o
>  obj-$(CONFIG_TOUCHSCREEN_ATMEL_MXT)	+= atmel_mxt_ts.o
> diff --git a/drivers/input/touchscreen/touch_adc.c b/drivers/input/touchscreen/touch_adc.c
> new file mode 100644
> index 0000000..de4b929
> --- /dev/null
> +++ b/drivers/input/touchscreen/touch_adc.c
> @@ -0,0 +1,199 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * ADC generic resistive touchscreen (GRTS)
> + *
> + * Copyright (C) 2017,2018 Microchip Technology,
> + * Author: Eugen Hristev <eugen.hristev@microchip.com>
> + *
> + */
> +#include <linux/input.h>
> +#include <linux/of.h>
> +#include <linux/of_device.h>
> +#include <linux/platform_device.h>
> +#include <linux/iio/consumer.h>
> +#include <linux/iio/iio.h>
> +#include <linux/module.h>
> +
> +#define DRIVER_NAME					"touch_adc"
> +#define GRTS_DEFAULT_PRESSURE_THRESHOLD			10000
> +#define MAX_POS_MASK					GENMASK(11, 0)
> +
> +/**
> + * grts_state - generic resistive touch screen information struct
> + * @pressure_threshold:	number representing the threshold for the pressure
> + * @pressure:		are we getting pressure info or not
> + * @iio_chans:		list of channels acquired
> + * @iio_cb:		iio_callback buffer for the data
> + * @input:		the input device structure that we register
> + */
> +struct grts_state {
> +	u32			pressure_threshold;
> +	bool			pressure;
> +	struct iio_channel	*iio_chans;
> +	struct iio_cb_buffer	*iio_cb;
> +	struct input_dev	*input;
> +};
> +
> +static int grts_cb(const void *data, void *private)
> +{
> +	const u16 *touch_info = data;
> +	struct grts_state *st = private;
> +
> +	unsigned int x, y, press = 0xFFFF;
> +
> +	/* channel data coming in buffer in the order below */
> +	x = touch_info[0];
> +	y = touch_info[1];
> +	if (st->pressure)
> +		press = touch_info[2];
> +
> +	if ((!x && !y) || (st->pressure && (press > st->pressure_threshold))) {
Ah, clearly pressure is the other way around to I assumed and gets larger as
the pressure is reduced.  hmm.

> +		/* report end of touch */
> +		input_report_key(st->input, BTN_TOUCH, 0);
> +		input_sync(st->input);
> +		return 0;
> +	}
> +
> +	/* report proper touch to subsystem*/
> +	input_report_abs(st->input, ABS_X, x);
> +	input_report_abs(st->input, ABS_Y, y);
> +	if (st->pressure)
> +		input_report_abs(st->input, ABS_PRESSURE, press);
> +	input_report_key(st->input, BTN_TOUCH, 1);
> +	input_sync(st->input);
blank line preferred here.

> +	return 0;
> +}
> +
> +static int grts_open(struct input_dev *dev)
> +{
> +	int ret;
> +	struct grts_state *st = input_get_drvdata(dev);
> +
> +	ret = iio_channel_start_all_cb(st->iio_cb);
> +	if (ret) {
> +		dev_err(dev->dev.parent, "failed to start callback buffer.\n");
> +		return ret;
Drop the return ret out of the brackets.

> +	}
> +	return 0;
> +}
> +
> +static void grts_close(struct input_dev *dev)
> +{
> +	struct grts_state *st = input_get_drvdata(dev);
> +
> +	iio_channel_stop_all_cb(st->iio_cb);
> +}
> +
> +static int grts_probe(struct platform_device *pdev)
> +{
> +	struct grts_state *st;
> +	struct input_dev *input;
> +	struct device *dev = &pdev->dev;
> +	struct device_node *node = dev->of_node;
> +	struct iio_channel *chan;
> +	int ret = 0;
> +
> +	st = devm_kzalloc(dev, sizeof(struct grts_state), GFP_KERNEL);
> +	if (!st)
> +		return -ENOMEM;
> +
> +	ret = of_property_read_u32(node,
> +			"generic-adc-resistive-touchscreen,pressure-threshold",
> +			&st->pressure_threshold);
> +	if (ret < 0) {
> +		dev_dbg(dev, "can't get touchscreen pressure threshold property.\n");
> +		st->pressure_threshold = GRTS_DEFAULT_PRESSURE_THRESHOLD;
> +	}
> +
> +	/* get the channels from IIO device */
> +	st->iio_chans = devm_iio_channel_get_all(dev);
> +
> +	if (IS_ERR(st->iio_chans)) {
> +		if (PTR_ERR(st->iio_chans) != -EPROBE_DEFER)
> +			dev_err(dev, "can't get iio channels.\n");
> +		return PTR_ERR(st->iio_chans);
> +	}
> +
> +	chan = &st->iio_chans[0];
> +	st->pressure = false;
> +	while (chan && chan->indio_dev) {
> +		if (!strcmp(chan->channel->datasheet_name, "pressure"))
> +			st->pressure = true;
> +		chan++;
> +	}
> +
> +	input = devm_input_allocate_device(dev);
> +	if (!input) {
> +		dev_err(dev, "failed to allocate input device.\n");
> +		return -ENOMEM;
> +	}
> +
> +	input->name = DRIVER_NAME;
> +	input->id.bustype = BUS_HOST;
> +	input->dev.parent = dev;
> +	input->open = grts_open;
> +	input->close = grts_close;
> +
> +	input_set_abs_params(input, ABS_X, 0, MAX_POS_MASK - 1, 0, 0);
> +	input_set_abs_params(input, ABS_Y, 0, MAX_POS_MASK - 1, 0, 0);
> +	if (st->pressure)
> +		input_set_abs_params(input, ABS_PRESSURE, 0, 0xffffff, 0, 0);
> +
> +	input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
> +	input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
> +
> +	st->input = input;
> +	input_set_drvdata(input, st);
> +
> +	ret = input_register_device(input);
> +	if (ret) {
> +		dev_err(dev, "failed to register input device.");
> +		return ret;
> +	}
> +
> +	st->iio_cb = iio_channel_get_all_cb(&pdev->dev, grts_cb, st);
> +
> +	if (IS_ERR(st->iio_cb)) {
> +		dev_err(dev, "failed to allocate callback buffer.\n");
> +		ret =  PTR_ERR(st->iio_cb);
> +		return ret;
> +	}
> +
> +	platform_set_drvdata(pdev, st);
> +
> +	return ret;
> +}
> +
> +static int grts_remove(struct platform_device *pdev)
> +{
> +	struct grts_state *st = platform_get_drvdata(pdev);
> +
> +	iio_channel_release_all_cb(st->iio_cb);
> +	input_unregister_device(st->input);
blank line preferred before simple returns.

> +	return 0;
> +}
> +
> +static const struct of_device_id grts_of_match[] = {
> +	{
> +		.compatible = "generic-resistive-adc-touch",
> +	}, {
> +		/* sentinel */
> +	},
> +};
> +
> +MODULE_DEVICE_TABLE(of, grts_of_match);
> +
> +static struct platform_driver grts_driver = {
> +	.probe = grts_probe,
> +	.remove = grts_remove,
> +	.driver = {
> +		   .name = DRIVER_NAME,
> +		   .of_match_table = of_match_ptr(grts_of_match),
> +	},
> +};
> +
> +module_platform_driver(grts_driver);
> +
> +MODULE_AUTHOR("Eugen Hristev <eugen.hristev@microchip.com>");
> +MODULE_DESCRIPTION("Generic ADC Resistive Touch Driver");
> +MODULE_LICENSE("GPL v2");

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Jonathan Cameron March 30, 2018, 1:02 p.m. UTC | #4
On Tue, 27 Mar 2018 15:32:33 +0300
Eugen Hristev <eugen.hristev@microchip.com> wrote:

> Hello,
> 
> This patch series is a rework of my previous series named:
> [PATCH 00/14] iio: triggers: add consumer support
> 
> In few words, this is the implementation of splitting the functionality
> of the IP block ADC device in SAMA5D2 SoC from ADC with touchscreen
> support. In order to avoid having a MFD device, two separate
> drivers that would work on same register base and split the IRQ,etc,
> as advised on the mailing list, I created a consumer driver for the
> channels, that will connect to the ADC as described in the device tree.
> 
> I have collected feedback from everyone and here is the result:
> I have added a new generic resistive touchscreen driver, which acts
> as a iio consumer for the given channels and will create an input
> device and report the events. It uses a callback buffer to register
> to the IIO device and waits for data to be pushed.
> Inside the IIO device, I have kept a similar approach with the first version
> of the series, except that now the driver can take multiple buffers, and
> will configure the touchscreen part of the hardware device if the specific
> channels are requested.
> 
> The SAMA5D2 ADC driver registers three new channels: two for the
> position on the X and Y axis, and one for the touch pressure.
> When channels are requested, it will check if the touchscreen channel mask
> includes the requested channels (it is possible that the consumer driver
> will not request pressure for example). If it's the case, it will work
> in touchscreen mode, and will refuse to do usual analog-digital conversion,
> because we have a single trigger and the touchscreen needs it.
> When the scan mask will include only old channels, the driver will function
> in the same way as before. If the scan mask somehow is a mix of the two (the
> masks intersect), the driver will refuse to work whatsoever (cannot have both
> in the same time).
> The driver allows reading raw data for the new channels, if claim direct
> mode works: no touchscreen driver requested anything. The new channels can
> act like the old ones. However, when requesting these channels, the usual
> trigger will not work and will not be enabled. The touchscreen channels
> require special trigger and irq configuration: pen detect, no pen detect
> and a periodic trigger to sample the touchscreen position and pressure.
> If the user attempts to use another trigger while there is a buffer
> that already requested the touchscreen channels (thus the trigger), the
> driver will refuse to comply.
> 
> In order to have defines for the channel numbers, I added a bindings include
> file that goes on a separate commit :
> dt-bindings: iio: adc: at91-sama5d2_adc: add channel specific consumer info
> This should go in the same tree with the following commits :
>   ARM: dts: at91: sama5d2: add channel cells for ADC device
>   ARM: dts: at91: sama5d2: Add resistive touch device
> 
> as build will break because these commits depend on the binding one
> which creates the included header file.
> 
> After the discussions earlier this year on the mailing list, I hope this
> rework of the patches is much better and fulfills all the requirements
> for this implementation.
As I said in one of the later patches, I like this a lot.
It is a good blend of the moderately nasty handling needed in the ADC
driver with a lovely generic input driver.

Very nice!  Hope everyone else agrees ;)

Jonathan

> 
> Eugen Hristev (10):
>   MAINTAINERS: add generic resistive touchscreen adc
>   iio: Add channel for Position Relative
>   dt-bindings: input: touchscreen: touch_adc: create bindings
>   iio: inkern: add module put/get on iio dev module when requesting
>     channels
>   iio: adc: at91-sama5d2_adc: fix channel configuration for differential
>     channels
>   iio: adc: at91-sama5d2_adc: add support for position and pressure
>     channels
>   input: touchscreen: touch_adc: add generic resistive ADC touchscreen
>   dt-bindings: iio: adc: at91-sama5d2_adc: add channel specific consumer
>     info
>   ARM: dts: at91: sama5d2: add channel cells for ADC device
>   ARM: dts: at91: sama5d2: Add resistive touch device
> 
>  Documentation/ABI/testing/sysfs-bus-iio            |  12 +
>  .../bindings/iio/adc/at91-sama5d2_adc.txt          |   9 +
>  .../bindings/input/touchscreen/touch_adc.txt       |  33 ++
>  MAINTAINERS                                        |   6 +
>  arch/arm/boot/dts/sama5d2.dtsi                     |  12 +
>  drivers/iio/adc/at91-sama5d2_adc.c                 | 491 ++++++++++++++++++++-
>  drivers/iio/industrialio-core.c                    |   1 +
>  drivers/iio/inkern.c                               |   8 +-
>  drivers/input/touchscreen/Kconfig                  |  13 +
>  drivers/input/touchscreen/Makefile                 |   1 +
>  drivers/input/touchscreen/touch_adc.c              | 199 +++++++++
>  include/dt-bindings/iio/adc/at91-sama5d2_adc.h     |  16 +
>  include/uapi/linux/iio/types.h                     |   1 +
>  tools/iio/iio_event_monitor.c                      |   2 +
>  14 files changed, 791 insertions(+), 13 deletions(-)
>  create mode 100644 Documentation/devicetree/bindings/input/touchscreen/touch_adc.txt
>  create mode 100644 drivers/input/touchscreen/touch_adc.c
>  create mode 100644 include/dt-bindings/iio/adc/at91-sama5d2_adc.h
> 

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Dmitry Torokhov March 30, 2018, 6:09 p.m. UTC | #5
On Fri, Mar 30, 2018 at 01:58:35PM +0100, Jonathan Cameron wrote:
> On Tue, 27 Mar 2018 15:32:40 +0300
> Eugen Hristev <eugen.hristev@microchip.com> wrote:
> 
> > This adds a generic resistive touchscreen (GRTS) driver, which is based
> > on an IIO device (an ADC). It must be connected to the channels of an ADC
> > to receive touch data. Then it will feed the data into the input subsystem
> > where it registers an input device.
> > It uses an IIO callback buffer to register to the IIO device
> > 
> > Some parts of this patch are based on initial original work by
> > Mohamed Jamsheeth Hajanajubudeen and Bandaru Venkateswara Swamy
> > 
> > Signed-off-by: Eugen Hristev <eugen.hristev@microchip.com>
> I like this a lot!
> 
> A few minor bits and bobs inline.  Over to Dmitry for the input
> side of things.
> 
> Jonathan
> 
> > ---
> > Changes in v2:
> >  - this is now a generic resistive adc touchscreen driver
> > 
> >  drivers/input/touchscreen/Kconfig     |  13 +++
> >  drivers/input/touchscreen/Makefile    |   1 +
> >  drivers/input/touchscreen/touch_adc.c | 199 ++++++++++++++++++++++++++++++++++
> >  3 files changed, 213 insertions(+)
> >  create mode 100644 drivers/input/touchscreen/touch_adc.c
> > 
> > diff --git a/drivers/input/touchscreen/Kconfig b/drivers/input/touchscreen/Kconfig
> > index 4f15496..afd879f 100644
> > --- a/drivers/input/touchscreen/Kconfig
> > +++ b/drivers/input/touchscreen/Kconfig
> > @@ -92,6 +92,19 @@ config TOUCHSCREEN_AD7879_SPI
> >  	  To compile this driver as a module, choose M here: the
> >  	  module will be called ad7879-spi.
> >  
> > +config TOUCHSCREEN_ADC
> > +	tristate "Generic ADC based resistive touchscreen"
> > +	depends on IIO
> > +	select IIO_BUFFER_CB
> > +	help
> > +	  Say Y here if you want to use the generic ADC
> > +	  resistive touchscreen driver.
> > +
> > +	  If unsure, say N (but it's safe to say "Y").
> > +
> > +	  To compile this driver as a module, choose M here: the
> > +	  module will be called touch_adc.ko.
> > +
> >  config TOUCHSCREEN_AR1021_I2C
> >  	tristate "Microchip AR1020/1021 i2c touchscreen"
> >  	depends on I2C && OF
> > diff --git a/drivers/input/touchscreen/Makefile b/drivers/input/touchscreen/Makefile
> > index dddae79..cbe6121 100644
> > --- a/drivers/input/touchscreen/Makefile
> > +++ b/drivers/input/touchscreen/Makefile
> > @@ -13,6 +13,7 @@ obj-$(CONFIG_TOUCHSCREEN_AD7877)	+= ad7877.o
> >  obj-$(CONFIG_TOUCHSCREEN_AD7879)	+= ad7879.o
> >  obj-$(CONFIG_TOUCHSCREEN_AD7879_I2C)	+= ad7879-i2c.o
> >  obj-$(CONFIG_TOUCHSCREEN_AD7879_SPI)	+= ad7879-spi.o
> > +obj-$(CONFIG_TOUCHSCREEN_ADC)		+= touch_adc.o
> >  obj-$(CONFIG_TOUCHSCREEN_ADS7846)	+= ads7846.o
> >  obj-$(CONFIG_TOUCHSCREEN_AR1021_I2C)	+= ar1021_i2c.o
> >  obj-$(CONFIG_TOUCHSCREEN_ATMEL_MXT)	+= atmel_mxt_ts.o
> > diff --git a/drivers/input/touchscreen/touch_adc.c b/drivers/input/touchscreen/touch_adc.c
> > new file mode 100644
> > index 0000000..de4b929
> > --- /dev/null
> > +++ b/drivers/input/touchscreen/touch_adc.c
> > @@ -0,0 +1,199 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +/*
> > + * ADC generic resistive touchscreen (GRTS)
> > + *
> > + * Copyright (C) 2017,2018 Microchip Technology,
> > + * Author: Eugen Hristev <eugen.hristev@microchip.com>
> > + *
> > + */
> > +#include <linux/input.h>
> > +#include <linux/of.h>
> > +#include <linux/of_device.h>
> > +#include <linux/platform_device.h>
> > +#include <linux/iio/consumer.h>
> > +#include <linux/iio/iio.h>
> > +#include <linux/module.h>
> > +
> > +#define DRIVER_NAME					"touch_adc"
> > +#define GRTS_DEFAULT_PRESSURE_THRESHOLD			10000
> > +#define MAX_POS_MASK					GENMASK(11, 0)
> > +
> > +/**
> > + * grts_state - generic resistive touch screen information struct
> > + * @pressure_threshold:	number representing the threshold for the pressure
> > + * @pressure:		are we getting pressure info or not
> > + * @iio_chans:		list of channels acquired
> > + * @iio_cb:		iio_callback buffer for the data
> > + * @input:		the input device structure that we register
> > + */
> > +struct grts_state {
> > +	u32			pressure_threshold;
> > +	bool			pressure;
> > +	struct iio_channel	*iio_chans;
> > +	struct iio_cb_buffer	*iio_cb;
> > +	struct input_dev	*input;
> > +};
> > +
> > +static int grts_cb(const void *data, void *private)
> > +{
> > +	const u16 *touch_info = data;
> > +	struct grts_state *st = private;
> > +
> > +	unsigned int x, y, press = 0xFFFF;
> > +
> > +	/* channel data coming in buffer in the order below */
> > +	x = touch_info[0];
> > +	y = touch_info[1];
> > +	if (st->pressure)
> > +		press = touch_info[2];
> > +
> > +	if ((!x && !y) || (st->pressure && (press > st->pressure_threshold))) {
> Ah, clearly pressure is the other way around to I assumed and gets larger as
> the pressure is reduced.  hmm.

OK, in this case it should not be reported directly as ABS_PRESSURE, as
ABSP_RESSURE value is expected to grow the firmer the finger touches the
surface, and reduce to 0 when finger is about to be lifted.

> 
> > +		/* report end of touch */
> > +		input_report_key(st->input, BTN_TOUCH, 0);
> > +		input_sync(st->input);
> > +		return 0;
> > +	}
> > +
> > +	/* report proper touch to subsystem*/
> > +	input_report_abs(st->input, ABS_X, x);
> > +	input_report_abs(st->input, ABS_Y, y);
> > +	if (st->pressure)
> > +		input_report_abs(st->input, ABS_PRESSURE, press);
> > +	input_report_key(st->input, BTN_TOUCH, 1);
> > +	input_sync(st->input);
> blank line preferred here.
> 
> > +	return 0;
> > +}
> > +
> > +static int grts_open(struct input_dev *dev)
> > +{
> > +	int ret;
> > +	struct grts_state *st = input_get_drvdata(dev);
> > +
> > +	ret = iio_channel_start_all_cb(st->iio_cb);
> > +	if (ret) {
> > +		dev_err(dev->dev.parent, "failed to start callback buffer.\n");
> > +		return ret;
> Drop the return ret out of the brackets.

Actually I prefer explicit "success" return path. But I would
appreciate if we called this "ret" variable "error".

> 
> > +	}
> > +	return 0;
> > +}
> > +
> > +static void grts_close(struct input_dev *dev)
> > +{
> > +	struct grts_state *st = input_get_drvdata(dev);
> > +
> > +	iio_channel_stop_all_cb(st->iio_cb);
> > +}
> > +
> > +static int grts_probe(struct platform_device *pdev)
> > +{
> > +	struct grts_state *st;
> > +	struct input_dev *input;
> > +	struct device *dev = &pdev->dev;
> > +	struct device_node *node = dev->of_node;
> > +	struct iio_channel *chan;
> > +	int ret = 0;
> > +
> > +	st = devm_kzalloc(dev, sizeof(struct grts_state), GFP_KERNEL);
> > +	if (!st)
> > +		return -ENOMEM;
> > +
> > +	ret = of_property_read_u32(node,
> > +			"generic-adc-resistive-touchscreen,pressure-threshold",
> > +			&st->pressure_threshold);
> > +	if (ret < 0) {
> > +		dev_dbg(dev, "can't get touchscreen pressure threshold property.\n");
> > +		st->pressure_threshold = GRTS_DEFAULT_PRESSURE_THRESHOLD;
> > +	}
> > +
> > +	/* get the channels from IIO device */
> > +	st->iio_chans = devm_iio_channel_get_all(dev);
> > +
> > +	if (IS_ERR(st->iio_chans)) {
> > +		if (PTR_ERR(st->iio_chans) != -EPROBE_DEFER)
> > +			dev_err(dev, "can't get iio channels.\n");
> > +		return PTR_ERR(st->iio_chans);
> > +	}
> > +
> > +	chan = &st->iio_chans[0];
> > +	st->pressure = false;
> > +	while (chan && chan->indio_dev) {
> > +		if (!strcmp(chan->channel->datasheet_name, "pressure"))
> > +			st->pressure = true;
> > +		chan++;
> > +	}
> > +
> > +	input = devm_input_allocate_device(dev);
> > +	if (!input) {
> > +		dev_err(dev, "failed to allocate input device.\n");
> > +		return -ENOMEM;
> > +	}
> > +
> > +	input->name = DRIVER_NAME;
> > +	input->id.bustype = BUS_HOST;
> > +	input->dev.parent = dev;

This is already being set by devm_input_allocate_device().

> > +	input->open = grts_open;
> > +	input->close = grts_close;
> > +
> > +	input_set_abs_params(input, ABS_X, 0, MAX_POS_MASK - 1, 0, 0);
> > +	input_set_abs_params(input, ABS_Y, 0, MAX_POS_MASK - 1, 0, 0);
> > +	if (st->pressure)
> > +		input_set_abs_params(input, ABS_PRESSURE, 0, 0xffffff, 0, 0);

It would be good if you used the API from
include/linux/input/touchscreen.h to parse standard touchscreen
properties and implement sizing, flippign and rotation of the screen.

> > +
> > +	input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
> > +	input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
> > +
> > +	st->input = input;
> > +	input_set_drvdata(input, st);
> > +
> > +	ret = input_register_device(input);
> > +	if (ret) {
> > +		dev_err(dev, "failed to register input device.");
> > +		return ret;
> > +	}
> > +
> > +	st->iio_cb = iio_channel_get_all_cb(&pdev->dev, grts_cb, st);

Hmm, we don't have devm-variant for this? Then you could use
devm_add_action_or_reset() to add cleanup action and completely remove
grts_remove().

> > +
> > +	if (IS_ERR(st->iio_cb)) {
> > +		dev_err(dev, "failed to allocate callback buffer.\n");
> > +		ret =  PTR_ERR(st->iio_cb);
> > +		return ret;
> > +	}
> > +
> > +	platform_set_drvdata(pdev, st);
> > +
> > +	return ret;
> > +}
> > +
> > +static int grts_remove(struct platform_device *pdev)
> > +{
> > +	struct grts_state *st = platform_get_drvdata(pdev);
> > +
> > +	iio_channel_release_all_cb(st->iio_cb);
> > +	input_unregister_device(st->input);

This call is not needed since you are using managed input device. 

> blank line preferred before simple returns.
> 
> > +	return 0;
> > +}
> > +
> > +static const struct of_device_id grts_of_match[] = {
> > +	{
> > +		.compatible = "generic-resistive-adc-touch",
> > +	}, {
> > +		/* sentinel */
> > +	},
> > +};
> > +
> > +MODULE_DEVICE_TABLE(of, grts_of_match);
> > +
> > +static struct platform_driver grts_driver = {
> > +	.probe = grts_probe,
> > +	.remove = grts_remove,
> > +	.driver = {
> > +		   .name = DRIVER_NAME,
> > +		   .of_match_table = of_match_ptr(grts_of_match),
> > +	},
> > +};
> > +
> > +module_platform_driver(grts_driver);
> > +
> > +MODULE_AUTHOR("Eugen Hristev <eugen.hristev@microchip.com>");
> > +MODULE_DESCRIPTION("Generic ADC Resistive Touch Driver");
> > +MODULE_LICENSE("GPL v2");
> 

Thanks.
Jonathan Cameron April 6, 2018, 3:13 p.m. UTC | #6
> > > +
> > > +	input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
> > > +	input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
> > > +
> > > +	st->input = input;
> > > +	input_set_drvdata(input, st);
> > > +
> > > +	ret = input_register_device(input);
> > > +	if (ret) {
> > > +		dev_err(dev, "failed to register input device.");
> > > +		return ret;
> > > +	}
> > > +
> > > +	st->iio_cb = iio_channel_get_all_cb(&pdev->dev, grts_cb, st);  
> 
> Hmm, we don't have devm-variant for this? Then you could use
> devm_add_action_or_reset() to add cleanup action and completely remove
> grts_remove().
Not yet, but I'm not adverse to having one...

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Quentin Schulz April 10, 2018, 7:38 a.m. UTC | #7
Hi Jonathan and Eugen,

On Fri, Mar 30, 2018 at 02:02:12PM +0100, Jonathan Cameron wrote:
> On Tue, 27 Mar 2018 15:32:33 +0300
> Eugen Hristev <eugen.hristev@microchip.com> wrote:
> 
> > Hello,
> > 
> > This patch series is a rework of my previous series named:
> > [PATCH 00/14] iio: triggers: add consumer support
> > 
> > In few words, this is the implementation of splitting the functionality
> > of the IP block ADC device in SAMA5D2 SoC from ADC with touchscreen
> > support. In order to avoid having a MFD device, two separate
> > drivers that would work on same register base and split the IRQ,etc,
> > as advised on the mailing list, I created a consumer driver for the
> > channels, that will connect to the ADC as described in the device tree.
> > 
> > I have collected feedback from everyone and here is the result:
> > I have added a new generic resistive touchscreen driver, which acts
> > as a iio consumer for the given channels and will create an input
> > device and report the events. It uses a callback buffer to register
> > to the IIO device and waits for data to be pushed.
> > Inside the IIO device, I have kept a similar approach with the first version
> > of the series, except that now the driver can take multiple buffers, and
> > will configure the touchscreen part of the hardware device if the specific
> > channels are requested.
> > 
> > The SAMA5D2 ADC driver registers three new channels: two for the
> > position on the X and Y axis, and one for the touch pressure.
> > When channels are requested, it will check if the touchscreen channel mask
> > includes the requested channels (it is possible that the consumer driver
> > will not request pressure for example). If it's the case, it will work
> > in touchscreen mode, and will refuse to do usual analog-digital conversion,
> > because we have a single trigger and the touchscreen needs it.
> > When the scan mask will include only old channels, the driver will function
> > in the same way as before. If the scan mask somehow is a mix of the two (the
> > masks intersect), the driver will refuse to work whatsoever (cannot have both
> > in the same time).
> > The driver allows reading raw data for the new channels, if claim direct
> > mode works: no touchscreen driver requested anything. The new channels can
> > act like the old ones. However, when requesting these channels, the usual
> > trigger will not work and will not be enabled. The touchscreen channels
> > require special trigger and irq configuration: pen detect, no pen detect
> > and a periodic trigger to sample the touchscreen position and pressure.
> > If the user attempts to use another trigger while there is a buffer
> > that already requested the touchscreen channels (thus the trigger), the
> > driver will refuse to comply.
> > 
> > In order to have defines for the channel numbers, I added a bindings include
> > file that goes on a separate commit :
> > dt-bindings: iio: adc: at91-sama5d2_adc: add channel specific consumer info
> > This should go in the same tree with the following commits :
> >   ARM: dts: at91: sama5d2: add channel cells for ADC device
> >   ARM: dts: at91: sama5d2: Add resistive touch device
> > 
> > as build will break because these commits depend on the binding one
> > which creates the included header file.
> > 
> > After the discussions earlier this year on the mailing list, I hope this
> > rework of the patches is much better and fulfills all the requirements
> > for this implementation.
> As I said in one of the later patches, I like this a lot.
> It is a good blend of the moderately nasty handling needed in the ADC
> driver with a lovely generic input driver.
> 
> Very nice!  Hope everyone else agrees ;)
> 

I'd love to see a generic touchscreen driver being an iio consumer!

However, I've already a case that can't be handled unfortunately.

I posted ~2 years ago a patch series[1] for touchscreen support for
Allwinner SoCs that are using their ADC (also) for touchscreen purpose.

It's been a very long time, I'm trying the hardest I can with my
"IIRC-skills".

There are several problems:
  - Data is stored in one register as a queue following this scheme:
    X @t=0 coord, Y @t=0 coord, X @t=1 coord, Y @t=1 coord, X @t=2
    coord, Y @t=2 coord, etc...

    Thus, I suppose I've only one channel and not two for coordinates.

  - The first data stored after an up event is absolutely unreliable so
  I have to flush it, thus I need to use another API call to have the
  touchscreen driver do some logic with a whole queue only when it's
  after an up event (it doesn't really make sense to do so in the IIO
  driver, does it?).

  - This up event is an interrupt.. that is configurable from the same
  set of registers than for the ADC, so I need an mfd,

What are your thoughts (and maybe workarounds?) on those issues?

Thanks,
Quentin

[1] https://lkml.org/lkml/2016/7/20/156

> Jonathan
> 
> > 
> > Eugen Hristev (10):
> >   MAINTAINERS: add generic resistive touchscreen adc
> >   iio: Add channel for Position Relative
> >   dt-bindings: input: touchscreen: touch_adc: create bindings
> >   iio: inkern: add module put/get on iio dev module when requesting
> >     channels
> >   iio: adc: at91-sama5d2_adc: fix channel configuration for differential
> >     channels
> >   iio: adc: at91-sama5d2_adc: add support for position and pressure
> >     channels
> >   input: touchscreen: touch_adc: add generic resistive ADC touchscreen
> >   dt-bindings: iio: adc: at91-sama5d2_adc: add channel specific consumer
> >     info
> >   ARM: dts: at91: sama5d2: add channel cells for ADC device
> >   ARM: dts: at91: sama5d2: Add resistive touch device
> > 
> >  Documentation/ABI/testing/sysfs-bus-iio            |  12 +
> >  .../bindings/iio/adc/at91-sama5d2_adc.txt          |   9 +
> >  .../bindings/input/touchscreen/touch_adc.txt       |  33 ++
> >  MAINTAINERS                                        |   6 +
> >  arch/arm/boot/dts/sama5d2.dtsi                     |  12 +
> >  drivers/iio/adc/at91-sama5d2_adc.c                 | 491 ++++++++++++++++++++-
> >  drivers/iio/industrialio-core.c                    |   1 +
> >  drivers/iio/inkern.c                               |   8 +-
> >  drivers/input/touchscreen/Kconfig                  |  13 +
> >  drivers/input/touchscreen/Makefile                 |   1 +
> >  drivers/input/touchscreen/touch_adc.c              | 199 +++++++++
> >  include/dt-bindings/iio/adc/at91-sama5d2_adc.h     |  16 +
> >  include/uapi/linux/iio/types.h                     |   1 +
> >  tools/iio/iio_event_monitor.c                      |   2 +
> >  14 files changed, 791 insertions(+), 13 deletions(-)
> >  create mode 100644 Documentation/devicetree/bindings/input/touchscreen/touch_adc.txt
> >  create mode 100644 drivers/input/touchscreen/touch_adc.c
> >  create mode 100644 include/dt-bindings/iio/adc/at91-sama5d2_adc.h
> > 
> 
> 
> _______________________________________________
> linux-arm-kernel mailing list
> linux-arm-kernel@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Jonathan Cameron April 10, 2018, 1:47 p.m. UTC | #8
On Tue, 10 Apr 2018 09:38:12 +0200
Quentin Schulz <quentin.schulz@bootlin.com> wrote:

> Hi Jonathan and Eugen,
> 
> On Fri, Mar 30, 2018 at 02:02:12PM +0100, Jonathan Cameron wrote:
> > On Tue, 27 Mar 2018 15:32:33 +0300
> > Eugen Hristev <eugen.hristev@microchip.com> wrote:
> >   
> > > Hello,
> > > 
> > > This patch series is a rework of my previous series named:
> > > [PATCH 00/14] iio: triggers: add consumer support
> > > 
> > > In few words, this is the implementation of splitting the functionality
> > > of the IP block ADC device in SAMA5D2 SoC from ADC with touchscreen
> > > support. In order to avoid having a MFD device, two separate
> > > drivers that would work on same register base and split the IRQ,etc,
> > > as advised on the mailing list, I created a consumer driver for the
> > > channels, that will connect to the ADC as described in the device tree.
> > > 
> > > I have collected feedback from everyone and here is the result:
> > > I have added a new generic resistive touchscreen driver, which acts
> > > as a iio consumer for the given channels and will create an input
> > > device and report the events. It uses a callback buffer to register
> > > to the IIO device and waits for data to be pushed.
> > > Inside the IIO device, I have kept a similar approach with the first version
> > > of the series, except that now the driver can take multiple buffers, and
> > > will configure the touchscreen part of the hardware device if the specific
> > > channels are requested.
> > > 
> > > The SAMA5D2 ADC driver registers three new channels: two for the
> > > position on the X and Y axis, and one for the touch pressure.
> > > When channels are requested, it will check if the touchscreen channel mask
> > > includes the requested channels (it is possible that the consumer driver
> > > will not request pressure for example). If it's the case, it will work
> > > in touchscreen mode, and will refuse to do usual analog-digital conversion,
> > > because we have a single trigger and the touchscreen needs it.
> > > When the scan mask will include only old channels, the driver will function
> > > in the same way as before. If the scan mask somehow is a mix of the two (the
> > > masks intersect), the driver will refuse to work whatsoever (cannot have both
> > > in the same time).
> > > The driver allows reading raw data for the new channels, if claim direct
> > > mode works: no touchscreen driver requested anything. The new channels can
> > > act like the old ones. However, when requesting these channels, the usual
> > > trigger will not work and will not be enabled. The touchscreen channels
> > > require special trigger and irq configuration: pen detect, no pen detect
> > > and a periodic trigger to sample the touchscreen position and pressure.
> > > If the user attempts to use another trigger while there is a buffer
> > > that already requested the touchscreen channels (thus the trigger), the
> > > driver will refuse to comply.
> > > 
> > > In order to have defines for the channel numbers, I added a bindings include
> > > file that goes on a separate commit :
> > > dt-bindings: iio: adc: at91-sama5d2_adc: add channel specific consumer info
> > > This should go in the same tree with the following commits :
> > >   ARM: dts: at91: sama5d2: add channel cells for ADC device
> > >   ARM: dts: at91: sama5d2: Add resistive touch device
> > > 
> > > as build will break because these commits depend on the binding one
> > > which creates the included header file.
> > > 
> > > After the discussions earlier this year on the mailing list, I hope this
> > > rework of the patches is much better and fulfills all the requirements
> > > for this implementation.  
> > As I said in one of the later patches, I like this a lot.
> > It is a good blend of the moderately nasty handling needed in the ADC
> > driver with a lovely generic input driver.
> > 
> > Very nice!  Hope everyone else agrees ;)
> >   
> 
> I'd love to see a generic touchscreen driver being an iio consumer!
> 
> However, I've already a case that can't be handled unfortunately.
> 
> I posted ~2 years ago a patch series[1] for touchscreen support for
> Allwinner SoCs that are using their ADC (also) for touchscreen purpose.
> 
> It's been a very long time, I'm trying the hardest I can with my
> "IIRC-skills".
> 
> There are several problems:
>   - Data is stored in one register as a queue following this scheme:
>     X @t=0 coord, Y @t=0 coord, X @t=1 coord, Y @t=1 coord, X @t=2
>     coord, Y @t=2 coord, etc...
> 
>     Thus, I suppose I've only one channel and not two for coordinates.

As I read this, no you don't.  You have two channels going through a
very short on chip hw fifo.  They need to be reported as two channels
with data appropriate reformatted to reflect that before being pushed
to the IIO buffer interfaces.

> 
>   - The first data stored after an up event is absolutely unreliable so
>   I have to flush it, thus I need to use another API call to have the
>   touchscreen driver do some logic with a whole queue only when it's
>   after an up event (it doesn't really make sense to do so in the IIO
>   driver, does it?).

If it is garbage data I don't see any real problem with doing it in
the IIO driver.  But obviously I don't have details to hand so there
may be some complexity in this - I'm not sure the IIO driver knows
enough about what is going on to distinguish that this data is bad.

> 
>   - This up event is an interrupt.. that is configurable from the same
>   set of registers than for the ADC, so I need an mfd,
Hmm.  So there are various options here other than using an mfd.

1. Could use the event to 'filter' the trigger being used to drive the
data flow to the touch screen driver. So, as far as the touch screen
driver is concerned it gets good data only when a touch is in progress.
We present it as a 'magic' trigger that can be associated with the
device that happens to have this property.

2. Could actually write the (long missing) in kernel API for IIO events and
support the touch as a threshold falling event and add support for
handling to the touchscreen driver.
> 
> What are your thoughts (and maybe workarounds?) on those issues?
> 
Not trivial and at some point we'll be different enough from the generic
driver that it makes sense to perhaps have a couple of 'classes' of generic
touch screen to cover common options without making thing too complex.
That might be in one driver, and it might not.

> Thanks,
> Quentin
> 
> [1] https://lkml.org/lkml/2016/7/20/156
> 
> > Jonathan
> >   
> > > 
> > > Eugen Hristev (10):
> > >   MAINTAINERS: add generic resistive touchscreen adc
> > >   iio: Add channel for Position Relative
> > >   dt-bindings: input: touchscreen: touch_adc: create bindings
> > >   iio: inkern: add module put/get on iio dev module when requesting
> > >     channels
> > >   iio: adc: at91-sama5d2_adc: fix channel configuration for differential
> > >     channels
> > >   iio: adc: at91-sama5d2_adc: add support for position and pressure
> > >     channels
> > >   input: touchscreen: touch_adc: add generic resistive ADC touchscreen
> > >   dt-bindings: iio: adc: at91-sama5d2_adc: add channel specific consumer
> > >     info
> > >   ARM: dts: at91: sama5d2: add channel cells for ADC device
> > >   ARM: dts: at91: sama5d2: Add resistive touch device
> > > 
> > >  Documentation/ABI/testing/sysfs-bus-iio            |  12 +
> > >  .../bindings/iio/adc/at91-sama5d2_adc.txt          |   9 +
> > >  .../bindings/input/touchscreen/touch_adc.txt       |  33 ++
> > >  MAINTAINERS                                        |   6 +
> > >  arch/arm/boot/dts/sama5d2.dtsi                     |  12 +
> > >  drivers/iio/adc/at91-sama5d2_adc.c                 | 491 ++++++++++++++++++++-
> > >  drivers/iio/industrialio-core.c                    |   1 +
> > >  drivers/iio/inkern.c                               |   8 +-
> > >  drivers/input/touchscreen/Kconfig                  |  13 +
> > >  drivers/input/touchscreen/Makefile                 |   1 +
> > >  drivers/input/touchscreen/touch_adc.c              | 199 +++++++++
> > >  include/dt-bindings/iio/adc/at91-sama5d2_adc.h     |  16 +
> > >  include/uapi/linux/iio/types.h                     |   1 +
> > >  tools/iio/iio_event_monitor.c                      |   2 +
> > >  14 files changed, 791 insertions(+), 13 deletions(-)
> > >  create mode 100644 Documentation/devicetree/bindings/input/touchscreen/touch_adc.txt
> > >  create mode 100644 drivers/input/touchscreen/touch_adc.c
> > >  create mode 100644 include/dt-bindings/iio/adc/at91-sama5d2_adc.h
> > >   
> > 
> > 
> > _______________________________________________
> > linux-arm-kernel mailing list
> > linux-arm-kernel@lists.infradead.org
> > http://lists.infradead.org/mailman/listinfo/linux-arm-kernel  
> 

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html