xref: /linux/drivers/iio/pressure/zpa2326.c (revision fe2392c67db9730d46f11fc4fadfa7bffa8843fa)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
203b262f2SGregor Boirie /*
303b262f2SGregor Boirie  * Murata ZPA2326 pressure and temperature sensor IIO driver
403b262f2SGregor Boirie  *
503b262f2SGregor Boirie  * Copyright (c) 2016 Parrot S.A.
603b262f2SGregor Boirie  *
703b262f2SGregor Boirie  * Author: Gregor Boirie <gregor.boirie@parrot.com>
803b262f2SGregor Boirie  */
903b262f2SGregor Boirie 
1003b262f2SGregor Boirie /**
1103b262f2SGregor Boirie  * DOC: ZPA2326 theory of operations
1203b262f2SGregor Boirie  *
1303b262f2SGregor Boirie  * This driver supports %INDIO_DIRECT_MODE and %INDIO_BUFFER_TRIGGERED IIO
1403b262f2SGregor Boirie  * modes.
1503b262f2SGregor Boirie  * A internal hardware trigger is also implemented to dispatch registered IIO
1603b262f2SGregor Boirie  * trigger consumers upon "sample ready" interrupts.
1703b262f2SGregor Boirie  *
1803b262f2SGregor Boirie  * ZPA2326 hardware supports 2 sampling mode: one shot and continuous.
1903b262f2SGregor Boirie  *
2003b262f2SGregor Boirie  * A complete one shot sampling cycle gets device out of low power mode,
2103b262f2SGregor Boirie  * performs pressure and temperature measurements, then automatically switches
2203b262f2SGregor Boirie  * back to low power mode. It is meant for on demand sampling with optimal power
2303b262f2SGregor Boirie  * saving at the cost of lower sampling rate and higher software overhead.
2403b262f2SGregor Boirie  * This is a natural candidate for IIO read_raw hook implementation
2503b262f2SGregor Boirie  * (%INDIO_DIRECT_MODE). It is also used for triggered buffering support to
2603b262f2SGregor Boirie  * ensure explicit synchronization with external trigger events
2703b262f2SGregor Boirie  * (%INDIO_BUFFER_TRIGGERED).
2803b262f2SGregor Boirie  *
2903b262f2SGregor Boirie  * The continuous mode works according to a periodic hardware measurement
3003b262f2SGregor Boirie  * process continuously pushing samples into an internal hardware FIFO (for
3103b262f2SGregor Boirie  * pressure samples only). Measurement cycle completion may be signaled by a
3203b262f2SGregor Boirie  * "sample ready" interrupt.
3303b262f2SGregor Boirie  * Typical software sequence of operations :
3403b262f2SGregor Boirie  * - get device out of low power mode,
3503b262f2SGregor Boirie  * - setup hardware sampling period,
3603b262f2SGregor Boirie  * - at end of period, upon data ready interrupt: pop pressure samples out of
3703b262f2SGregor Boirie  *   hardware FIFO and fetch temperature sample
3803b262f2SGregor Boirie  * - when no longer needed, stop sampling process by putting device into
3903b262f2SGregor Boirie  *   low power mode.
4003b262f2SGregor Boirie  * This mode is used to implement %INDIO_BUFFER_TRIGGERED mode if device tree
4103b262f2SGregor Boirie  * declares a valid interrupt line. In this case, the internal hardware trigger
4203b262f2SGregor Boirie  * drives acquisition.
4303b262f2SGregor Boirie  *
4403b262f2SGregor Boirie  * Note that hardware sampling frequency is taken into account only when
4503b262f2SGregor Boirie  * internal hardware trigger is attached as the highest sampling rate seems to
4603b262f2SGregor Boirie  * be the most energy efficient.
4703b262f2SGregor Boirie  *
4803b262f2SGregor Boirie  * TODO:
4903b262f2SGregor Boirie  *   preset pressure threshold crossing / IIO events ;
5003b262f2SGregor Boirie  *   differential pressure sampling ;
5103b262f2SGregor Boirie  *   hardware samples averaging.
5203b262f2SGregor Boirie  */
5303b262f2SGregor Boirie 
5403b262f2SGregor Boirie #include <linux/module.h>
5503b262f2SGregor Boirie #include <linux/kernel.h>
5603b262f2SGregor Boirie #include <linux/delay.h>
5703b262f2SGregor Boirie #include <linux/interrupt.h>
5803b262f2SGregor Boirie #include <linux/regulator/consumer.h>
5903b262f2SGregor Boirie #include <linux/pm_runtime.h>
6003b262f2SGregor Boirie #include <linux/regmap.h>
6103b262f2SGregor Boirie #include <linux/iio/iio.h>
6203b262f2SGregor Boirie #include <linux/iio/sysfs.h>
6303b262f2SGregor Boirie #include <linux/iio/buffer.h>
6403b262f2SGregor Boirie #include <linux/iio/trigger.h>
6503b262f2SGregor Boirie #include <linux/iio/trigger_consumer.h>
6603b262f2SGregor Boirie #include <linux/iio/triggered_buffer.h>
6703b262f2SGregor Boirie #include "zpa2326.h"
6803b262f2SGregor Boirie 
6903b262f2SGregor Boirie /* 200 ms should be enough for the longest conversion time in one-shot mode. */
7003b262f2SGregor Boirie #define ZPA2326_CONVERSION_JIFFIES (HZ / 5)
7103b262f2SGregor Boirie 
7203b262f2SGregor Boirie /* There should be a 1 ms delay (Tpup) after getting out of reset. */
7303b262f2SGregor Boirie #define ZPA2326_TPUP_USEC_MIN      (1000)
7403b262f2SGregor Boirie #define ZPA2326_TPUP_USEC_MAX      (2000)
7503b262f2SGregor Boirie 
7603b262f2SGregor Boirie /**
7703b262f2SGregor Boirie  * struct zpa2326_frequency - Hardware sampling frequency descriptor
7803b262f2SGregor Boirie  * @hz : Frequency in Hertz.
7903b262f2SGregor Boirie  * @odr: Output Data Rate word as expected by %ZPA2326_CTRL_REG3_REG.
8003b262f2SGregor Boirie  */
8103b262f2SGregor Boirie struct zpa2326_frequency {
8203b262f2SGregor Boirie 	int hz;
8303b262f2SGregor Boirie 	u16 odr;
8403b262f2SGregor Boirie };
8503b262f2SGregor Boirie 
8603b262f2SGregor Boirie /*
8703b262f2SGregor Boirie  * Keep these in strict ascending order: last array entry is expected to
8803b262f2SGregor Boirie  * correspond to the highest sampling frequency.
8903b262f2SGregor Boirie  */
9003b262f2SGregor Boirie static const struct zpa2326_frequency zpa2326_sampling_frequencies[] = {
9103b262f2SGregor Boirie 	{ .hz = 1,  .odr = 1 << ZPA2326_CTRL_REG3_ODR_SHIFT },
9203b262f2SGregor Boirie 	{ .hz = 5,  .odr = 5 << ZPA2326_CTRL_REG3_ODR_SHIFT },
9303b262f2SGregor Boirie 	{ .hz = 11, .odr = 6 << ZPA2326_CTRL_REG3_ODR_SHIFT },
9403b262f2SGregor Boirie 	{ .hz = 23, .odr = 7 << ZPA2326_CTRL_REG3_ODR_SHIFT },
9503b262f2SGregor Boirie };
9603b262f2SGregor Boirie 
9703b262f2SGregor Boirie /* Return the highest hardware sampling frequency available. */
9803b262f2SGregor Boirie static const struct zpa2326_frequency *zpa2326_highest_frequency(void)
9903b262f2SGregor Boirie {
10003b262f2SGregor Boirie 	return &zpa2326_sampling_frequencies[
10103b262f2SGregor Boirie 		ARRAY_SIZE(zpa2326_sampling_frequencies) - 1];
10203b262f2SGregor Boirie }
10303b262f2SGregor Boirie 
10403b262f2SGregor Boirie /**
10503b262f2SGregor Boirie  * struct zpa_private - Per-device internal private state
10603b262f2SGregor Boirie  * @timestamp:  Buffered samples ready datum.
10703b262f2SGregor Boirie  * @regmap:     Underlying I2C / SPI bus adapter used to abstract slave register
10803b262f2SGregor Boirie  *              accesses.
10903b262f2SGregor Boirie  * @result:     Allows sampling logic to get completion status of operations
11003b262f2SGregor Boirie  *              that interrupt handlers perform asynchronously.
11103b262f2SGregor Boirie  * @data_ready: Interrupt handler uses this to wake user context up at sampling
11203b262f2SGregor Boirie  *              operation completion.
11303b262f2SGregor Boirie  * @trigger:    Optional hardware / interrupt driven trigger used to notify
11403b262f2SGregor Boirie  *              external devices a new sample is ready.
11503b262f2SGregor Boirie  * @waken:      Flag indicating whether or not device has just been powered on.
11603b262f2SGregor Boirie  * @irq:        Optional interrupt line: negative or zero if not declared into
11703b262f2SGregor Boirie  *              DT, in which case sampling logic keeps polling status register
11803b262f2SGregor Boirie  *              to detect completion.
11903b262f2SGregor Boirie  * @frequency:  Current hardware sampling frequency.
12003b262f2SGregor Boirie  * @vref:       Power / voltage reference.
12103b262f2SGregor Boirie  * @vdd:        Power supply.
12203b262f2SGregor Boirie  */
12303b262f2SGregor Boirie struct zpa2326_private {
12403b262f2SGregor Boirie 	s64                             timestamp;
12503b262f2SGregor Boirie 	struct regmap                  *regmap;
12603b262f2SGregor Boirie 	int                             result;
12703b262f2SGregor Boirie 	struct completion               data_ready;
12803b262f2SGregor Boirie 	struct iio_trigger             *trigger;
12903b262f2SGregor Boirie 	bool                            waken;
13003b262f2SGregor Boirie 	int                             irq;
13103b262f2SGregor Boirie 	const struct zpa2326_frequency *frequency;
13203b262f2SGregor Boirie 	struct regulator               *vref;
13303b262f2SGregor Boirie 	struct regulator               *vdd;
13403b262f2SGregor Boirie };
13503b262f2SGregor Boirie 
136eed3089fSJoe Perches #define zpa2326_err(idev, fmt, ...)					\
137eed3089fSJoe Perches 	dev_err(idev->dev.parent, fmt "\n", ##__VA_ARGS__)
13803b262f2SGregor Boirie 
139eed3089fSJoe Perches #define zpa2326_warn(idev, fmt, ...)					\
140eed3089fSJoe Perches 	dev_warn(idev->dev.parent, fmt "\n", ##__VA_ARGS__)
14103b262f2SGregor Boirie 
142eed3089fSJoe Perches #define zpa2326_dbg(idev, fmt, ...)					\
143eed3089fSJoe Perches 	dev_dbg(idev->dev.parent, fmt "\n", ##__VA_ARGS__)
14403b262f2SGregor Boirie 
14503b262f2SGregor Boirie bool zpa2326_isreg_writeable(struct device *dev, unsigned int reg)
14603b262f2SGregor Boirie {
14703b262f2SGregor Boirie 	switch (reg) {
14803b262f2SGregor Boirie 	case ZPA2326_REF_P_XL_REG:
14903b262f2SGregor Boirie 	case ZPA2326_REF_P_L_REG:
15003b262f2SGregor Boirie 	case ZPA2326_REF_P_H_REG:
15103b262f2SGregor Boirie 	case ZPA2326_RES_CONF_REG:
15203b262f2SGregor Boirie 	case ZPA2326_CTRL_REG0_REG:
15303b262f2SGregor Boirie 	case ZPA2326_CTRL_REG1_REG:
15403b262f2SGregor Boirie 	case ZPA2326_CTRL_REG2_REG:
15503b262f2SGregor Boirie 	case ZPA2326_CTRL_REG3_REG:
15603b262f2SGregor Boirie 	case ZPA2326_THS_P_LOW_REG:
15703b262f2SGregor Boirie 	case ZPA2326_THS_P_HIGH_REG:
15803b262f2SGregor Boirie 		return true;
15903b262f2SGregor Boirie 
16003b262f2SGregor Boirie 	default:
16103b262f2SGregor Boirie 		return false;
16203b262f2SGregor Boirie 	}
16303b262f2SGregor Boirie }
16403b262f2SGregor Boirie EXPORT_SYMBOL_GPL(zpa2326_isreg_writeable);
16503b262f2SGregor Boirie 
16603b262f2SGregor Boirie bool zpa2326_isreg_readable(struct device *dev, unsigned int reg)
16703b262f2SGregor Boirie {
16803b262f2SGregor Boirie 	switch (reg) {
16903b262f2SGregor Boirie 	case ZPA2326_REF_P_XL_REG:
17003b262f2SGregor Boirie 	case ZPA2326_REF_P_L_REG:
17103b262f2SGregor Boirie 	case ZPA2326_REF_P_H_REG:
17203b262f2SGregor Boirie 	case ZPA2326_DEVICE_ID_REG:
17303b262f2SGregor Boirie 	case ZPA2326_RES_CONF_REG:
17403b262f2SGregor Boirie 	case ZPA2326_CTRL_REG0_REG:
17503b262f2SGregor Boirie 	case ZPA2326_CTRL_REG1_REG:
17603b262f2SGregor Boirie 	case ZPA2326_CTRL_REG2_REG:
17703b262f2SGregor Boirie 	case ZPA2326_CTRL_REG3_REG:
17803b262f2SGregor Boirie 	case ZPA2326_INT_SOURCE_REG:
17903b262f2SGregor Boirie 	case ZPA2326_THS_P_LOW_REG:
18003b262f2SGregor Boirie 	case ZPA2326_THS_P_HIGH_REG:
18103b262f2SGregor Boirie 	case ZPA2326_STATUS_REG:
18203b262f2SGregor Boirie 	case ZPA2326_PRESS_OUT_XL_REG:
18303b262f2SGregor Boirie 	case ZPA2326_PRESS_OUT_L_REG:
18403b262f2SGregor Boirie 	case ZPA2326_PRESS_OUT_H_REG:
18503b262f2SGregor Boirie 	case ZPA2326_TEMP_OUT_L_REG:
18603b262f2SGregor Boirie 	case ZPA2326_TEMP_OUT_H_REG:
18703b262f2SGregor Boirie 		return true;
18803b262f2SGregor Boirie 
18903b262f2SGregor Boirie 	default:
19003b262f2SGregor Boirie 		return false;
19103b262f2SGregor Boirie 	}
19203b262f2SGregor Boirie }
19303b262f2SGregor Boirie EXPORT_SYMBOL_GPL(zpa2326_isreg_readable);
19403b262f2SGregor Boirie 
19503b262f2SGregor Boirie bool zpa2326_isreg_precious(struct device *dev, unsigned int reg)
19603b262f2SGregor Boirie {
19703b262f2SGregor Boirie 	switch (reg) {
19803b262f2SGregor Boirie 	case ZPA2326_INT_SOURCE_REG:
19903b262f2SGregor Boirie 	case ZPA2326_PRESS_OUT_H_REG:
20003b262f2SGregor Boirie 		return true;
20103b262f2SGregor Boirie 
20203b262f2SGregor Boirie 	default:
20303b262f2SGregor Boirie 		return false;
20403b262f2SGregor Boirie 	}
20503b262f2SGregor Boirie }
20603b262f2SGregor Boirie EXPORT_SYMBOL_GPL(zpa2326_isreg_precious);
20703b262f2SGregor Boirie 
20803b262f2SGregor Boirie /**
20903b262f2SGregor Boirie  * zpa2326_enable_device() - Enable device, i.e. get out of low power mode.
21003b262f2SGregor Boirie  * @indio_dev: The IIO device associated with the hardware to enable.
21103b262f2SGregor Boirie  *
21203b262f2SGregor Boirie  * Required to access complete register space and to perform any sampling
21303b262f2SGregor Boirie  * or control operations.
21403b262f2SGregor Boirie  *
21503b262f2SGregor Boirie  * Return: Zero when successful, a negative error code otherwise.
21603b262f2SGregor Boirie  */
21703b262f2SGregor Boirie static int zpa2326_enable_device(const struct iio_dev *indio_dev)
21803b262f2SGregor Boirie {
21903b262f2SGregor Boirie 	int err;
22003b262f2SGregor Boirie 
22103b262f2SGregor Boirie 	err = regmap_write(((struct zpa2326_private *)
22203b262f2SGregor Boirie 			    iio_priv(indio_dev))->regmap,
22303b262f2SGregor Boirie 			    ZPA2326_CTRL_REG0_REG, ZPA2326_CTRL_REG0_ENABLE);
22403b262f2SGregor Boirie 	if (err) {
22503b262f2SGregor Boirie 		zpa2326_err(indio_dev, "failed to enable device (%d)", err);
22603b262f2SGregor Boirie 		return err;
22703b262f2SGregor Boirie 	}
22803b262f2SGregor Boirie 
22903b262f2SGregor Boirie 	zpa2326_dbg(indio_dev, "enabled");
23003b262f2SGregor Boirie 
23103b262f2SGregor Boirie 	return 0;
23203b262f2SGregor Boirie }
23303b262f2SGregor Boirie 
23403b262f2SGregor Boirie /**
23503b262f2SGregor Boirie  * zpa2326_sleep() - Disable device, i.e. switch to low power mode.
23603b262f2SGregor Boirie  * @indio_dev: The IIO device associated with the hardware to disable.
23703b262f2SGregor Boirie  *
23803b262f2SGregor Boirie  * Only %ZPA2326_DEVICE_ID_REG and %ZPA2326_CTRL_REG0_REG registers may be
23903b262f2SGregor Boirie  * accessed once device is in the disabled state.
24003b262f2SGregor Boirie  *
24103b262f2SGregor Boirie  * Return: Zero when successful, a negative error code otherwise.
24203b262f2SGregor Boirie  */
24303b262f2SGregor Boirie static int zpa2326_sleep(const struct iio_dev *indio_dev)
24403b262f2SGregor Boirie {
24503b262f2SGregor Boirie 	int err;
24603b262f2SGregor Boirie 
24703b262f2SGregor Boirie 	err = regmap_write(((struct zpa2326_private *)
24803b262f2SGregor Boirie 			    iio_priv(indio_dev))->regmap,
24903b262f2SGregor Boirie 			    ZPA2326_CTRL_REG0_REG, 0);
25003b262f2SGregor Boirie 	if (err) {
25103b262f2SGregor Boirie 		zpa2326_err(indio_dev, "failed to sleep (%d)", err);
25203b262f2SGregor Boirie 		return err;
25303b262f2SGregor Boirie 	}
25403b262f2SGregor Boirie 
25503b262f2SGregor Boirie 	zpa2326_dbg(indio_dev, "sleeping");
25603b262f2SGregor Boirie 
25703b262f2SGregor Boirie 	return 0;
25803b262f2SGregor Boirie }
25903b262f2SGregor Boirie 
26003b262f2SGregor Boirie /**
26103b262f2SGregor Boirie  * zpa2326_reset_device() - Reset device to default hardware state.
26203b262f2SGregor Boirie  * @indio_dev: The IIO device associated with the hardware to reset.
26303b262f2SGregor Boirie  *
26403b262f2SGregor Boirie  * Disable sampling and empty hardware FIFO.
26503b262f2SGregor Boirie  * Device must be enabled before reset, i.e. not in low power mode.
26603b262f2SGregor Boirie  *
26703b262f2SGregor Boirie  * Return: Zero when successful, a negative error code otherwise.
26803b262f2SGregor Boirie  */
26903b262f2SGregor Boirie static int zpa2326_reset_device(const struct iio_dev *indio_dev)
27003b262f2SGregor Boirie {
27103b262f2SGregor Boirie 	int err;
27203b262f2SGregor Boirie 
27303b262f2SGregor Boirie 	err = regmap_write(((struct zpa2326_private *)
27403b262f2SGregor Boirie 			    iio_priv(indio_dev))->regmap,
27503b262f2SGregor Boirie 			    ZPA2326_CTRL_REG2_REG, ZPA2326_CTRL_REG2_SWRESET);
27603b262f2SGregor Boirie 	if (err) {
27703b262f2SGregor Boirie 		zpa2326_err(indio_dev, "failed to reset device (%d)", err);
27803b262f2SGregor Boirie 		return err;
27903b262f2SGregor Boirie 	}
28003b262f2SGregor Boirie 
28103b262f2SGregor Boirie 	usleep_range(ZPA2326_TPUP_USEC_MIN, ZPA2326_TPUP_USEC_MAX);
28203b262f2SGregor Boirie 
28303b262f2SGregor Boirie 	zpa2326_dbg(indio_dev, "reset");
28403b262f2SGregor Boirie 
28503b262f2SGregor Boirie 	return 0;
28603b262f2SGregor Boirie }
28703b262f2SGregor Boirie 
28803b262f2SGregor Boirie /**
28903b262f2SGregor Boirie  * zpa2326_start_oneshot() - Start a single sampling cycle, i.e. in one shot
29003b262f2SGregor Boirie  *                           mode.
29103b262f2SGregor Boirie  * @indio_dev: The IIO device associated with the sampling hardware.
29203b262f2SGregor Boirie  *
29303b262f2SGregor Boirie  * Device must have been previously enabled and configured for one shot mode.
29403b262f2SGregor Boirie  * Device will be switched back to low power mode at end of cycle.
29503b262f2SGregor Boirie  *
29603b262f2SGregor Boirie  * Return: Zero when successful, a negative error code otherwise.
29703b262f2SGregor Boirie  */
29803b262f2SGregor Boirie static int zpa2326_start_oneshot(const struct iio_dev *indio_dev)
29903b262f2SGregor Boirie {
30003b262f2SGregor Boirie 	int err;
30103b262f2SGregor Boirie 
30203b262f2SGregor Boirie 	err = regmap_write(((struct zpa2326_private *)
30303b262f2SGregor Boirie 			    iio_priv(indio_dev))->regmap,
30403b262f2SGregor Boirie 			    ZPA2326_CTRL_REG0_REG,
30503b262f2SGregor Boirie 			    ZPA2326_CTRL_REG0_ENABLE |
30603b262f2SGregor Boirie 			    ZPA2326_CTRL_REG0_ONE_SHOT);
30703b262f2SGregor Boirie 	if (err) {
30803b262f2SGregor Boirie 		zpa2326_err(indio_dev, "failed to start one shot cycle (%d)",
30903b262f2SGregor Boirie 			    err);
31003b262f2SGregor Boirie 		return err;
31103b262f2SGregor Boirie 	}
31203b262f2SGregor Boirie 
31303b262f2SGregor Boirie 	zpa2326_dbg(indio_dev, "one shot cycle started");
31403b262f2SGregor Boirie 
31503b262f2SGregor Boirie 	return 0;
31603b262f2SGregor Boirie }
31703b262f2SGregor Boirie 
31803b262f2SGregor Boirie /**
31903b262f2SGregor Boirie  * zpa2326_power_on() - Power on device to allow subsequent configuration.
32003b262f2SGregor Boirie  * @indio_dev: The IIO device associated with the sampling hardware.
32103b262f2SGregor Boirie  * @private:   Internal private state related to @indio_dev.
32203b262f2SGregor Boirie  *
32303b262f2SGregor Boirie  * Sampling will be disabled, preventing strange things from happening in our
32403b262f2SGregor Boirie  * back. Hardware FIFO content will be cleared.
32503b262f2SGregor Boirie  * When successful, device will be left in the enabled state to allow further
32603b262f2SGregor Boirie  * configuration.
32703b262f2SGregor Boirie  *
32803b262f2SGregor Boirie  * Return: Zero when successful, a negative error code otherwise.
32903b262f2SGregor Boirie  */
33003b262f2SGregor Boirie static int zpa2326_power_on(const struct iio_dev         *indio_dev,
33103b262f2SGregor Boirie 			    const struct zpa2326_private *private)
33203b262f2SGregor Boirie {
33303b262f2SGregor Boirie 	int err;
33403b262f2SGregor Boirie 
33503b262f2SGregor Boirie 	err = regulator_enable(private->vref);
33603b262f2SGregor Boirie 	if (err)
33703b262f2SGregor Boirie 		return err;
33803b262f2SGregor Boirie 
33903b262f2SGregor Boirie 	err = regulator_enable(private->vdd);
34003b262f2SGregor Boirie 	if (err)
34103b262f2SGregor Boirie 		goto vref;
34203b262f2SGregor Boirie 
34303b262f2SGregor Boirie 	zpa2326_dbg(indio_dev, "powered on");
34403b262f2SGregor Boirie 
34503b262f2SGregor Boirie 	err = zpa2326_enable_device(indio_dev);
34603b262f2SGregor Boirie 	if (err)
34703b262f2SGregor Boirie 		goto vdd;
34803b262f2SGregor Boirie 
34903b262f2SGregor Boirie 	err = zpa2326_reset_device(indio_dev);
35003b262f2SGregor Boirie 	if (err)
35103b262f2SGregor Boirie 		goto sleep;
35203b262f2SGregor Boirie 
35303b262f2SGregor Boirie 	return 0;
35403b262f2SGregor Boirie 
35503b262f2SGregor Boirie sleep:
35603b262f2SGregor Boirie 	zpa2326_sleep(indio_dev);
35703b262f2SGregor Boirie vdd:
35803b262f2SGregor Boirie 	regulator_disable(private->vdd);
35903b262f2SGregor Boirie vref:
36003b262f2SGregor Boirie 	regulator_disable(private->vref);
36103b262f2SGregor Boirie 
36203b262f2SGregor Boirie 	zpa2326_dbg(indio_dev, "powered off");
36303b262f2SGregor Boirie 
36403b262f2SGregor Boirie 	return err;
36503b262f2SGregor Boirie }
36603b262f2SGregor Boirie 
36703b262f2SGregor Boirie /**
36803b262f2SGregor Boirie  * zpa2326_power_off() - Power off device, i.e. disable attached power
36903b262f2SGregor Boirie  *                       regulators.
37003b262f2SGregor Boirie  * @indio_dev: The IIO device associated with the sampling hardware.
37103b262f2SGregor Boirie  * @private:   Internal private state related to @indio_dev.
37203b262f2SGregor Boirie  *
37303b262f2SGregor Boirie  * Return: Zero when successful, a negative error code otherwise.
37403b262f2SGregor Boirie  */
37503b262f2SGregor Boirie static void zpa2326_power_off(const struct iio_dev         *indio_dev,
37603b262f2SGregor Boirie 			      const struct zpa2326_private *private)
37703b262f2SGregor Boirie {
37803b262f2SGregor Boirie 	regulator_disable(private->vdd);
37903b262f2SGregor Boirie 	regulator_disable(private->vref);
38003b262f2SGregor Boirie 
38103b262f2SGregor Boirie 	zpa2326_dbg(indio_dev, "powered off");
38203b262f2SGregor Boirie }
38303b262f2SGregor Boirie 
38403b262f2SGregor Boirie /**
38503b262f2SGregor Boirie  * zpa2326_config_oneshot() - Setup device for one shot / on demand mode.
38603b262f2SGregor Boirie  * @indio_dev: The IIO device associated with the sampling hardware.
38703b262f2SGregor Boirie  * @irq:       Optional interrupt line the hardware uses to notify new data
38803b262f2SGregor Boirie  *             samples are ready. Negative or zero values indicate no interrupts
38903b262f2SGregor Boirie  *             are available, meaning polling is required.
39003b262f2SGregor Boirie  *
39103b262f2SGregor Boirie  * Output Data Rate is configured for the highest possible rate so that
39203b262f2SGregor Boirie  * conversion time and power consumption are reduced to a minimum.
39303b262f2SGregor Boirie  * Note that hardware internal averaging machinery (not implemented in this
39403b262f2SGregor Boirie  * driver) is not applicable in this mode.
39503b262f2SGregor Boirie  *
39603b262f2SGregor Boirie  * Device must have been previously enabled before calling
39703b262f2SGregor Boirie  * zpa2326_config_oneshot().
39803b262f2SGregor Boirie  *
39903b262f2SGregor Boirie  * Return: Zero when successful, a negative error code otherwise.
40003b262f2SGregor Boirie  */
40103b262f2SGregor Boirie static int zpa2326_config_oneshot(const struct iio_dev *indio_dev,
40203b262f2SGregor Boirie 				  int                   irq)
40303b262f2SGregor Boirie {
40403b262f2SGregor Boirie 	struct regmap                  *regs = ((struct zpa2326_private *)
40503b262f2SGregor Boirie 						iio_priv(indio_dev))->regmap;
40603b262f2SGregor Boirie 	const struct zpa2326_frequency *freq = zpa2326_highest_frequency();
40703b262f2SGregor Boirie 	int                             err;
40803b262f2SGregor Boirie 
40903b262f2SGregor Boirie 	/* Setup highest available Output Data Rate for one shot mode. */
41003b262f2SGregor Boirie 	err = regmap_write(regs, ZPA2326_CTRL_REG3_REG, freq->odr);
41103b262f2SGregor Boirie 	if (err)
41203b262f2SGregor Boirie 		return err;
41303b262f2SGregor Boirie 
41403b262f2SGregor Boirie 	if (irq > 0) {
41503b262f2SGregor Boirie 		/* Request interrupt when new sample is available. */
41603b262f2SGregor Boirie 		err = regmap_write(regs, ZPA2326_CTRL_REG1_REG,
41703b262f2SGregor Boirie 				   (u8)~ZPA2326_CTRL_REG1_MASK_DATA_READY);
41803b262f2SGregor Boirie 
41903b262f2SGregor Boirie 		if (err) {
42003b262f2SGregor Boirie 			dev_err(indio_dev->dev.parent,
42103b262f2SGregor Boirie 				"failed to setup one shot mode (%d)", err);
42203b262f2SGregor Boirie 			return err;
42303b262f2SGregor Boirie 		}
42403b262f2SGregor Boirie 	}
42503b262f2SGregor Boirie 
42603b262f2SGregor Boirie 	zpa2326_dbg(indio_dev, "one shot mode setup @%dHz", freq->hz);
42703b262f2SGregor Boirie 
42803b262f2SGregor Boirie 	return 0;
42903b262f2SGregor Boirie }
43003b262f2SGregor Boirie 
43103b262f2SGregor Boirie /**
43203b262f2SGregor Boirie  * zpa2326_clear_fifo() - Clear remaining entries in hardware FIFO.
43303b262f2SGregor Boirie  * @indio_dev: The IIO device associated with the sampling hardware.
43403b262f2SGregor Boirie  * @min_count: Number of samples present within hardware FIFO.
43503b262f2SGregor Boirie  *
43603b262f2SGregor Boirie  * @min_count argument is a hint corresponding to the known minimum number of
43703b262f2SGregor Boirie  * samples currently living in the FIFO. This allows to reduce the number of bus
43803b262f2SGregor Boirie  * accesses by skipping status register read operation as long as we know for
43903b262f2SGregor Boirie  * sure there are still entries left.
44003b262f2SGregor Boirie  *
44103b262f2SGregor Boirie  * Return: Zero when successful, a negative error code otherwise.
44203b262f2SGregor Boirie  */
44303b262f2SGregor Boirie static int zpa2326_clear_fifo(const struct iio_dev *indio_dev,
44403b262f2SGregor Boirie 			      unsigned int          min_count)
44503b262f2SGregor Boirie {
44603b262f2SGregor Boirie 	struct regmap *regs = ((struct zpa2326_private *)
44703b262f2SGregor Boirie 			       iio_priv(indio_dev))->regmap;
44803b262f2SGregor Boirie 	int            err;
44903b262f2SGregor Boirie 	unsigned int   val;
45003b262f2SGregor Boirie 
45103b262f2SGregor Boirie 	if (!min_count) {
45203b262f2SGregor Boirie 		/*
45303b262f2SGregor Boirie 		 * No hint: read status register to determine whether FIFO is
45403b262f2SGregor Boirie 		 * empty or not.
45503b262f2SGregor Boirie 		 */
45603b262f2SGregor Boirie 		err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
45703b262f2SGregor Boirie 
45803b262f2SGregor Boirie 		if (err < 0)
45903b262f2SGregor Boirie 			goto err;
46003b262f2SGregor Boirie 
46103b262f2SGregor Boirie 		if (val & ZPA2326_STATUS_FIFO_E)
46203b262f2SGregor Boirie 			/* Fifo is empty: nothing to trash. */
46303b262f2SGregor Boirie 			return 0;
46403b262f2SGregor Boirie 	}
46503b262f2SGregor Boirie 
46603b262f2SGregor Boirie 	/* Clear FIFO. */
46703b262f2SGregor Boirie 	do {
46803b262f2SGregor Boirie 		/*
46903b262f2SGregor Boirie 		 * A single fetch from pressure MSB register is enough to pop
47003b262f2SGregor Boirie 		 * values out of FIFO.
47103b262f2SGregor Boirie 		 */
47203b262f2SGregor Boirie 		err = regmap_read(regs, ZPA2326_PRESS_OUT_H_REG, &val);
47303b262f2SGregor Boirie 		if (err < 0)
47403b262f2SGregor Boirie 			goto err;
47503b262f2SGregor Boirie 
47603b262f2SGregor Boirie 		if (min_count) {
47703b262f2SGregor Boirie 			/*
47803b262f2SGregor Boirie 			 * We know for sure there are at least min_count entries
47903b262f2SGregor Boirie 			 * left in FIFO. Skip status register read.
48003b262f2SGregor Boirie 			 */
48103b262f2SGregor Boirie 			min_count--;
48203b262f2SGregor Boirie 			continue;
48303b262f2SGregor Boirie 		}
48403b262f2SGregor Boirie 
48503b262f2SGregor Boirie 		err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
48603b262f2SGregor Boirie 		if (err < 0)
48703b262f2SGregor Boirie 			goto err;
48803b262f2SGregor Boirie 
48903b262f2SGregor Boirie 	} while (!(val & ZPA2326_STATUS_FIFO_E));
49003b262f2SGregor Boirie 
49103b262f2SGregor Boirie 	zpa2326_dbg(indio_dev, "FIFO cleared");
49203b262f2SGregor Boirie 
49303b262f2SGregor Boirie 	return 0;
49403b262f2SGregor Boirie 
49503b262f2SGregor Boirie err:
49603b262f2SGregor Boirie 	zpa2326_err(indio_dev, "failed to clear FIFO (%d)", err);
49703b262f2SGregor Boirie 
49803b262f2SGregor Boirie 	return err;
49903b262f2SGregor Boirie }
50003b262f2SGregor Boirie 
50103b262f2SGregor Boirie /**
50203b262f2SGregor Boirie  * zpa2326_dequeue_pressure() - Retrieve the most recent pressure sample from
50303b262f2SGregor Boirie  *                              hardware FIFO.
50403b262f2SGregor Boirie  * @indio_dev: The IIO device associated with the sampling hardware.
50503b262f2SGregor Boirie  * @pressure:  Sampled pressure output.
50603b262f2SGregor Boirie  *
50703b262f2SGregor Boirie  * Note that ZPA2326 hardware FIFO stores pressure samples only.
50803b262f2SGregor Boirie  *
50903b262f2SGregor Boirie  * Return: Zero when successful, a negative error code otherwise.
51003b262f2SGregor Boirie  */
51103b262f2SGregor Boirie static int zpa2326_dequeue_pressure(const struct iio_dev *indio_dev,
51203b262f2SGregor Boirie 				    u32                  *pressure)
51303b262f2SGregor Boirie {
51403b262f2SGregor Boirie 	struct regmap *regs = ((struct zpa2326_private *)
51503b262f2SGregor Boirie 			       iio_priv(indio_dev))->regmap;
51603b262f2SGregor Boirie 	unsigned int   val;
51703b262f2SGregor Boirie 	int            err;
51803b262f2SGregor Boirie 	int            cleared = -1;
51903b262f2SGregor Boirie 
52003b262f2SGregor Boirie 	err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
52103b262f2SGregor Boirie 	if (err < 0)
52203b262f2SGregor Boirie 		return err;
52303b262f2SGregor Boirie 
52403b262f2SGregor Boirie 	*pressure = 0;
52503b262f2SGregor Boirie 
52603b262f2SGregor Boirie 	if (val & ZPA2326_STATUS_P_OR) {
52703b262f2SGregor Boirie 		/*
52803b262f2SGregor Boirie 		 * Fifo overrun : first sample dequeued from FIFO is the
52903b262f2SGregor Boirie 		 * newest.
53003b262f2SGregor Boirie 		 */
53103b262f2SGregor Boirie 		zpa2326_warn(indio_dev, "FIFO overflow");
53203b262f2SGregor Boirie 
53303b262f2SGregor Boirie 		err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, pressure,
53403b262f2SGregor Boirie 				       3);
53503b262f2SGregor Boirie 		if (err)
53603b262f2SGregor Boirie 			return err;
53703b262f2SGregor Boirie 
53803b262f2SGregor Boirie #define ZPA2326_FIFO_DEPTH (16U)
53903b262f2SGregor Boirie 		/* Hardware FIFO may hold no more than 16 pressure samples. */
54003b262f2SGregor Boirie 		return zpa2326_clear_fifo(indio_dev, ZPA2326_FIFO_DEPTH - 1);
54103b262f2SGregor Boirie 	}
54203b262f2SGregor Boirie 
54303b262f2SGregor Boirie 	/*
54403b262f2SGregor Boirie 	 * Fifo has not overflown : retrieve newest sample. We need to pop
54503b262f2SGregor Boirie 	 * values out until FIFO is empty : last fetched pressure is the newest.
54603b262f2SGregor Boirie 	 * In nominal cases, we should find a single queued sample only.
54703b262f2SGregor Boirie 	 */
54803b262f2SGregor Boirie 	do {
54903b262f2SGregor Boirie 		err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, pressure,
55003b262f2SGregor Boirie 				       3);
55103b262f2SGregor Boirie 		if (err)
55203b262f2SGregor Boirie 			return err;
55303b262f2SGregor Boirie 
55403b262f2SGregor Boirie 		err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
55503b262f2SGregor Boirie 		if (err < 0)
55603b262f2SGregor Boirie 			return err;
55703b262f2SGregor Boirie 
55803b262f2SGregor Boirie 		cleared++;
55903b262f2SGregor Boirie 	} while (!(val & ZPA2326_STATUS_FIFO_E));
56003b262f2SGregor Boirie 
56103b262f2SGregor Boirie 	if (cleared)
56203b262f2SGregor Boirie 		/*
56303b262f2SGregor Boirie 		 * Samples were pushed by hardware during previous rounds but we
56403b262f2SGregor Boirie 		 * didn't consume them fast enough: inform user.
56503b262f2SGregor Boirie 		 */
56603b262f2SGregor Boirie 		zpa2326_dbg(indio_dev, "cleared %d FIFO entries", cleared);
56703b262f2SGregor Boirie 
56803b262f2SGregor Boirie 	return 0;
56903b262f2SGregor Boirie }
57003b262f2SGregor Boirie 
57103b262f2SGregor Boirie /**
57203b262f2SGregor Boirie  * zpa2326_fill_sample_buffer() - Enqueue new channel samples to IIO buffer.
57303b262f2SGregor Boirie  * @indio_dev: The IIO device associated with the sampling hardware.
57403b262f2SGregor Boirie  * @private:   Internal private state related to @indio_dev.
57503b262f2SGregor Boirie  *
57603b262f2SGregor Boirie  * Return: Zero when successful, a negative error code otherwise.
57703b262f2SGregor Boirie  */
57803b262f2SGregor Boirie static int zpa2326_fill_sample_buffer(struct iio_dev               *indio_dev,
57903b262f2SGregor Boirie 				      const struct zpa2326_private *private)
58003b262f2SGregor Boirie {
58103b262f2SGregor Boirie 	struct {
58203b262f2SGregor Boirie 		u32 pressure;
58303b262f2SGregor Boirie 		u16 temperature;
58403b262f2SGregor Boirie 		u64 timestamp;
58503b262f2SGregor Boirie 	}   sample;
58603b262f2SGregor Boirie 	int err;
58703b262f2SGregor Boirie 
58803b262f2SGregor Boirie 	if (test_bit(0, indio_dev->active_scan_mask)) {
58903b262f2SGregor Boirie 		/* Get current pressure from hardware FIFO. */
59003b262f2SGregor Boirie 		err = zpa2326_dequeue_pressure(indio_dev, &sample.pressure);
59103b262f2SGregor Boirie 		if (err) {
59203b262f2SGregor Boirie 			zpa2326_warn(indio_dev, "failed to fetch pressure (%d)",
59303b262f2SGregor Boirie 				     err);
59403b262f2SGregor Boirie 			return err;
59503b262f2SGregor Boirie 		}
59603b262f2SGregor Boirie 	}
59703b262f2SGregor Boirie 
59803b262f2SGregor Boirie 	if (test_bit(1, indio_dev->active_scan_mask)) {
59903b262f2SGregor Boirie 		/* Get current temperature. */
60003b262f2SGregor Boirie 		err = regmap_bulk_read(private->regmap, ZPA2326_TEMP_OUT_L_REG,
60103b262f2SGregor Boirie 				       &sample.temperature, 2);
60203b262f2SGregor Boirie 		if (err) {
60303b262f2SGregor Boirie 			zpa2326_warn(indio_dev,
60403b262f2SGregor Boirie 				     "failed to fetch temperature (%d)", err);
60503b262f2SGregor Boirie 			return err;
60603b262f2SGregor Boirie 		}
60703b262f2SGregor Boirie 	}
60803b262f2SGregor Boirie 
60903b262f2SGregor Boirie 	/*
61003b262f2SGregor Boirie 	 * Now push samples using timestamp stored either :
61103b262f2SGregor Boirie 	 *   - by hardware interrupt handler if interrupt is available: see
61203b262f2SGregor Boirie 	 *     zpa2326_handle_irq(),
61303b262f2SGregor Boirie 	 *   - or oneshot completion polling machinery : see
61403b262f2SGregor Boirie 	 *     zpa2326_trigger_handler().
61503b262f2SGregor Boirie 	 */
61603b262f2SGregor Boirie 	zpa2326_dbg(indio_dev, "filling raw samples buffer");
61703b262f2SGregor Boirie 
61803b262f2SGregor Boirie 	iio_push_to_buffers_with_timestamp(indio_dev, &sample,
61903b262f2SGregor Boirie 					   private->timestamp);
62003b262f2SGregor Boirie 
62103b262f2SGregor Boirie 	return 0;
62203b262f2SGregor Boirie }
62303b262f2SGregor Boirie 
62403b262f2SGregor Boirie #ifdef CONFIG_PM
62503b262f2SGregor Boirie static int zpa2326_runtime_suspend(struct device *parent)
62603b262f2SGregor Boirie {
62703b262f2SGregor Boirie 	const struct iio_dev *indio_dev = dev_get_drvdata(parent);
62803b262f2SGregor Boirie 
62903b262f2SGregor Boirie 	if (pm_runtime_autosuspend_expiration(parent))
63003b262f2SGregor Boirie 		/* Userspace changed autosuspend delay. */
63103b262f2SGregor Boirie 		return -EAGAIN;
63203b262f2SGregor Boirie 
63303b262f2SGregor Boirie 	zpa2326_power_off(indio_dev, iio_priv(indio_dev));
63403b262f2SGregor Boirie 
63503b262f2SGregor Boirie 	return 0;
63603b262f2SGregor Boirie }
63703b262f2SGregor Boirie 
63803b262f2SGregor Boirie static int zpa2326_runtime_resume(struct device *parent)
63903b262f2SGregor Boirie {
64003b262f2SGregor Boirie 	const struct iio_dev *indio_dev = dev_get_drvdata(parent);
64103b262f2SGregor Boirie 
64203b262f2SGregor Boirie 	return zpa2326_power_on(indio_dev, iio_priv(indio_dev));
64303b262f2SGregor Boirie }
64403b262f2SGregor Boirie 
64503b262f2SGregor Boirie const struct dev_pm_ops zpa2326_pm_ops = {
64603b262f2SGregor Boirie 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
64703b262f2SGregor Boirie 				pm_runtime_force_resume)
64803b262f2SGregor Boirie 	SET_RUNTIME_PM_OPS(zpa2326_runtime_suspend, zpa2326_runtime_resume,
64903b262f2SGregor Boirie 			   NULL)
65003b262f2SGregor Boirie };
65103b262f2SGregor Boirie EXPORT_SYMBOL_GPL(zpa2326_pm_ops);
65203b262f2SGregor Boirie 
65303b262f2SGregor Boirie /**
65403b262f2SGregor Boirie  * zpa2326_resume() - Request the PM layer to power supply the device.
65503b262f2SGregor Boirie  * @indio_dev: The IIO device associated with the sampling hardware.
65603b262f2SGregor Boirie  *
65703b262f2SGregor Boirie  * Return:
65803b262f2SGregor Boirie  *  < 0 - a negative error code meaning failure ;
65903b262f2SGregor Boirie  *    0 - success, device has just been powered up ;
66003b262f2SGregor Boirie  *    1 - success, device was already powered.
66103b262f2SGregor Boirie  */
66203b262f2SGregor Boirie static int zpa2326_resume(const struct iio_dev *indio_dev)
66303b262f2SGregor Boirie {
66403b262f2SGregor Boirie 	int err;
66503b262f2SGregor Boirie 
66603b262f2SGregor Boirie 	err = pm_runtime_get_sync(indio_dev->dev.parent);
66703b262f2SGregor Boirie 	if (err < 0)
66803b262f2SGregor Boirie 		return err;
66903b262f2SGregor Boirie 
67003b262f2SGregor Boirie 	if (err > 0) {
67103b262f2SGregor Boirie 		/*
67203b262f2SGregor Boirie 		 * Device was already power supplied: get it out of low power
67303b262f2SGregor Boirie 		 * mode and inform caller.
67403b262f2SGregor Boirie 		 */
67503b262f2SGregor Boirie 		zpa2326_enable_device(indio_dev);
67603b262f2SGregor Boirie 		return 1;
67703b262f2SGregor Boirie 	}
67803b262f2SGregor Boirie 
67903b262f2SGregor Boirie 	/* Inform caller device has just been brought back to life. */
68003b262f2SGregor Boirie 	return 0;
68103b262f2SGregor Boirie }
68203b262f2SGregor Boirie 
68303b262f2SGregor Boirie /**
68403b262f2SGregor Boirie  * zpa2326_suspend() - Schedule a power down using autosuspend feature of PM
68503b262f2SGregor Boirie  *                     layer.
68603b262f2SGregor Boirie  * @indio_dev: The IIO device associated with the sampling hardware.
68703b262f2SGregor Boirie  *
68803b262f2SGregor Boirie  * Device is switched to low power mode at first to save power even when
68903b262f2SGregor Boirie  * attached regulator is a "dummy" one.
69003b262f2SGregor Boirie  */
69103b262f2SGregor Boirie static void zpa2326_suspend(struct iio_dev *indio_dev)
69203b262f2SGregor Boirie {
69303b262f2SGregor Boirie 	struct device *parent = indio_dev->dev.parent;
69403b262f2SGregor Boirie 
69503b262f2SGregor Boirie 	zpa2326_sleep(indio_dev);
69603b262f2SGregor Boirie 
69703b262f2SGregor Boirie 	pm_runtime_mark_last_busy(parent);
69803b262f2SGregor Boirie 	pm_runtime_put_autosuspend(parent);
69903b262f2SGregor Boirie }
70003b262f2SGregor Boirie 
70103b262f2SGregor Boirie static void zpa2326_init_runtime(struct device *parent)
70203b262f2SGregor Boirie {
70303b262f2SGregor Boirie 	pm_runtime_get_noresume(parent);
70403b262f2SGregor Boirie 	pm_runtime_set_active(parent);
70503b262f2SGregor Boirie 	pm_runtime_enable(parent);
70603b262f2SGregor Boirie 	pm_runtime_set_autosuspend_delay(parent, 1000);
70703b262f2SGregor Boirie 	pm_runtime_use_autosuspend(parent);
70803b262f2SGregor Boirie 	pm_runtime_mark_last_busy(parent);
70903b262f2SGregor Boirie 	pm_runtime_put_autosuspend(parent);
71003b262f2SGregor Boirie }
71103b262f2SGregor Boirie 
71203b262f2SGregor Boirie static void zpa2326_fini_runtime(struct device *parent)
71303b262f2SGregor Boirie {
71403b262f2SGregor Boirie 	pm_runtime_disable(parent);
71503b262f2SGregor Boirie 	pm_runtime_set_suspended(parent);
71603b262f2SGregor Boirie }
71703b262f2SGregor Boirie #else /* !CONFIG_PM */
71803b262f2SGregor Boirie static int zpa2326_resume(const struct iio_dev *indio_dev)
71903b262f2SGregor Boirie {
72003b262f2SGregor Boirie 	zpa2326_enable_device(indio_dev);
72103b262f2SGregor Boirie 
72203b262f2SGregor Boirie 	return 0;
72303b262f2SGregor Boirie }
72403b262f2SGregor Boirie 
72503b262f2SGregor Boirie static void zpa2326_suspend(struct iio_dev *indio_dev)
72603b262f2SGregor Boirie {
72703b262f2SGregor Boirie 	zpa2326_sleep(indio_dev);
72803b262f2SGregor Boirie }
72903b262f2SGregor Boirie 
73003b262f2SGregor Boirie #define zpa2326_init_runtime(_parent)
73103b262f2SGregor Boirie #define zpa2326_fini_runtime(_parent)
73203b262f2SGregor Boirie #endif /* !CONFIG_PM */
73303b262f2SGregor Boirie 
73403b262f2SGregor Boirie /**
73503b262f2SGregor Boirie  * zpa2326_handle_irq() - Process hardware interrupts.
73603b262f2SGregor Boirie  * @irq:  Interrupt line the hardware uses to notify new data has arrived.
73703b262f2SGregor Boirie  * @data: The IIO device associated with the sampling hardware.
73803b262f2SGregor Boirie  *
73903b262f2SGregor Boirie  * Timestamp buffered samples as soon as possible then schedule threaded bottom
74003b262f2SGregor Boirie  * half.
74103b262f2SGregor Boirie  *
74203b262f2SGregor Boirie  * Return: Always successful.
74303b262f2SGregor Boirie  */
74403b262f2SGregor Boirie static irqreturn_t zpa2326_handle_irq(int irq, void *data)
74503b262f2SGregor Boirie {
746af873b0dSsimran singhal 	struct iio_dev *indio_dev = data;
74703b262f2SGregor Boirie 
74803b262f2SGregor Boirie 	if (iio_buffer_enabled(indio_dev)) {
74903b262f2SGregor Boirie 		/* Timestamping needed for buffered sampling only. */
75003b262f2SGregor Boirie 		((struct zpa2326_private *)
75103b262f2SGregor Boirie 		 iio_priv(indio_dev))->timestamp = iio_get_time_ns(indio_dev);
75203b262f2SGregor Boirie 	}
75303b262f2SGregor Boirie 
75403b262f2SGregor Boirie 	return IRQ_WAKE_THREAD;
75503b262f2SGregor Boirie }
75603b262f2SGregor Boirie 
75703b262f2SGregor Boirie /**
75803b262f2SGregor Boirie  * zpa2326_handle_threaded_irq() - Interrupt bottom-half handler.
75903b262f2SGregor Boirie  * @irq:  Interrupt line the hardware uses to notify new data has arrived.
76003b262f2SGregor Boirie  * @data: The IIO device associated with the sampling hardware.
76103b262f2SGregor Boirie  *
76203b262f2SGregor Boirie  * Mainly ensures interrupt is caused by a real "new sample available"
76303b262f2SGregor Boirie  * condition. This relies upon the ability to perform blocking / sleeping bus
76403b262f2SGregor Boirie  * accesses to slave's registers. This is why zpa2326_handle_threaded_irq() is
76503b262f2SGregor Boirie  * called from within a thread, i.e. not called from hard interrupt context.
76603b262f2SGregor Boirie  *
76703b262f2SGregor Boirie  * When device is using its own internal hardware trigger in continuous sampling
76803b262f2SGregor Boirie  * mode, data are available into hardware FIFO once interrupt has occurred. All
76903b262f2SGregor Boirie  * we have to do is to dispatch the trigger, which in turn will fetch data and
77003b262f2SGregor Boirie  * fill IIO buffer.
77103b262f2SGregor Boirie  *
77203b262f2SGregor Boirie  * When not using its own internal hardware trigger, the device has been
77303b262f2SGregor Boirie  * configured in one-shot mode either by an external trigger or the IIO read_raw
77403b262f2SGregor Boirie  * hook. This means one of the latter is currently waiting for sampling
77503b262f2SGregor Boirie  * completion, in which case we must simply wake it up.
77603b262f2SGregor Boirie  *
77703b262f2SGregor Boirie  * See zpa2326_trigger_handler().
77803b262f2SGregor Boirie  *
77903b262f2SGregor Boirie  * Return:
78003b262f2SGregor Boirie  *   %IRQ_NONE - no consistent interrupt happened ;
78103b262f2SGregor Boirie  *   %IRQ_HANDLED - there was new samples available.
78203b262f2SGregor Boirie  */
78303b262f2SGregor Boirie static irqreturn_t zpa2326_handle_threaded_irq(int irq, void *data)
78403b262f2SGregor Boirie {
785af873b0dSsimran singhal 	struct iio_dev         *indio_dev = data;
78603b262f2SGregor Boirie 	struct zpa2326_private *priv = iio_priv(indio_dev);
78703b262f2SGregor Boirie 	unsigned int            val;
78803b262f2SGregor Boirie 	bool                    cont;
78903b262f2SGregor Boirie 	irqreturn_t             ret = IRQ_NONE;
79003b262f2SGregor Boirie 
79103b262f2SGregor Boirie 	/*
79203b262f2SGregor Boirie 	 * Are we using our own internal trigger in triggered buffer mode, i.e.,
79303b262f2SGregor Boirie 	 * currently working in continuous sampling mode ?
79403b262f2SGregor Boirie 	 */
79503b262f2SGregor Boirie 	cont = (iio_buffer_enabled(indio_dev) &&
79603b262f2SGregor Boirie 		iio_trigger_using_own(indio_dev));
79703b262f2SGregor Boirie 
79803b262f2SGregor Boirie 	/*
79903b262f2SGregor Boirie 	 * Device works according to a level interrupt scheme: reading interrupt
80003b262f2SGregor Boirie 	 * status de-asserts interrupt line.
80103b262f2SGregor Boirie 	 */
80203b262f2SGregor Boirie 	priv->result = regmap_read(priv->regmap, ZPA2326_INT_SOURCE_REG, &val);
80303b262f2SGregor Boirie 	if (priv->result < 0) {
80403b262f2SGregor Boirie 		if (cont)
80503b262f2SGregor Boirie 			return IRQ_NONE;
80603b262f2SGregor Boirie 
80703b262f2SGregor Boirie 		goto complete;
80803b262f2SGregor Boirie 	}
80903b262f2SGregor Boirie 
81003b262f2SGregor Boirie 	/* Data ready is the only interrupt source we requested. */
81103b262f2SGregor Boirie 	if (!(val & ZPA2326_INT_SOURCE_DATA_READY)) {
81203b262f2SGregor Boirie 		/*
81303b262f2SGregor Boirie 		 * Interrupt happened but no new sample available: likely caused
81403b262f2SGregor Boirie 		 * by spurious interrupts, in which case, returning IRQ_NONE
81503b262f2SGregor Boirie 		 * allows to benefit from the generic spurious interrupts
81603b262f2SGregor Boirie 		 * handling.
81703b262f2SGregor Boirie 		 */
81803b262f2SGregor Boirie 		zpa2326_warn(indio_dev, "unexpected interrupt status %02x",
81903b262f2SGregor Boirie 			     val);
82003b262f2SGregor Boirie 
82103b262f2SGregor Boirie 		if (cont)
82203b262f2SGregor Boirie 			return IRQ_NONE;
82303b262f2SGregor Boirie 
82403b262f2SGregor Boirie 		priv->result = -ENODATA;
82503b262f2SGregor Boirie 		goto complete;
82603b262f2SGregor Boirie 	}
82703b262f2SGregor Boirie 
82803b262f2SGregor Boirie 	/* New sample available: dispatch internal trigger consumers. */
82903b262f2SGregor Boirie 	iio_trigger_poll_chained(priv->trigger);
83003b262f2SGregor Boirie 
83103b262f2SGregor Boirie 	if (cont)
83203b262f2SGregor Boirie 		/*
83303b262f2SGregor Boirie 		 * Internal hardware trigger has been scheduled above : it will
83403b262f2SGregor Boirie 		 * fetch data on its own.
83503b262f2SGregor Boirie 		 */
83603b262f2SGregor Boirie 		return IRQ_HANDLED;
83703b262f2SGregor Boirie 
83803b262f2SGregor Boirie 	ret = IRQ_HANDLED;
83903b262f2SGregor Boirie 
84003b262f2SGregor Boirie complete:
84103b262f2SGregor Boirie 	/*
84203b262f2SGregor Boirie 	 * Wake up direct or externaly triggered buffer mode waiters: see
84303b262f2SGregor Boirie 	 * zpa2326_sample_oneshot() and zpa2326_trigger_handler().
84403b262f2SGregor Boirie 	 */
84503b262f2SGregor Boirie 	complete(&priv->data_ready);
84603b262f2SGregor Boirie 
84703b262f2SGregor Boirie 	return ret;
84803b262f2SGregor Boirie }
84903b262f2SGregor Boirie 
85003b262f2SGregor Boirie /**
85103b262f2SGregor Boirie  * zpa2326_wait_oneshot_completion() - Wait for oneshot data ready interrupt.
85203b262f2SGregor Boirie  * @indio_dev: The IIO device associated with the sampling hardware.
85303b262f2SGregor Boirie  * @private:   Internal private state related to @indio_dev.
85403b262f2SGregor Boirie  *
85503b262f2SGregor Boirie  * Return: Zero when successful, a negative error code otherwise.
85603b262f2SGregor Boirie  */
85703b262f2SGregor Boirie static int zpa2326_wait_oneshot_completion(const struct iio_dev   *indio_dev,
85803b262f2SGregor Boirie 					   struct zpa2326_private *private)
85903b262f2SGregor Boirie {
86003b262f2SGregor Boirie 	unsigned int val;
861e7215fe4SNicholas Mc Guire 	long     timeout;
86203b262f2SGregor Boirie 
86303b262f2SGregor Boirie 	zpa2326_dbg(indio_dev, "waiting for one shot completion interrupt");
86403b262f2SGregor Boirie 
865e7215fe4SNicholas Mc Guire 	timeout = wait_for_completion_interruptible_timeout(
86603b262f2SGregor Boirie 		&private->data_ready, ZPA2326_CONVERSION_JIFFIES);
867e7215fe4SNicholas Mc Guire 	if (timeout > 0)
86803b262f2SGregor Boirie 		/*
86903b262f2SGregor Boirie 		 * Interrupt handler completed before timeout: return operation
87003b262f2SGregor Boirie 		 * status.
87103b262f2SGregor Boirie 		 */
87203b262f2SGregor Boirie 		return private->result;
87303b262f2SGregor Boirie 
87403b262f2SGregor Boirie 	/* Clear all interrupts just to be sure. */
87503b262f2SGregor Boirie 	regmap_read(private->regmap, ZPA2326_INT_SOURCE_REG, &val);
87603b262f2SGregor Boirie 
877e7215fe4SNicholas Mc Guire 	if (!timeout) {
87803b262f2SGregor Boirie 		/* Timed out. */
879e7215fe4SNicholas Mc Guire 		zpa2326_warn(indio_dev, "no one shot interrupt occurred (%ld)",
880e7215fe4SNicholas Mc Guire 			     timeout);
881f61dfff2SGeert Uytterhoeven 		return -ETIME;
882e7215fe4SNicholas Mc Guire 	}
88303b262f2SGregor Boirie 
884f61dfff2SGeert Uytterhoeven 	zpa2326_warn(indio_dev, "wait for one shot interrupt cancelled");
885f61dfff2SGeert Uytterhoeven 	return -ERESTARTSYS;
88603b262f2SGregor Boirie }
88703b262f2SGregor Boirie 
88803b262f2SGregor Boirie static int zpa2326_init_managed_irq(struct device          *parent,
88903b262f2SGregor Boirie 				    struct iio_dev         *indio_dev,
89003b262f2SGregor Boirie 				    struct zpa2326_private *private,
89103b262f2SGregor Boirie 				    int                     irq)
89203b262f2SGregor Boirie {
89303b262f2SGregor Boirie 	int err;
89403b262f2SGregor Boirie 
89503b262f2SGregor Boirie 	private->irq = irq;
89603b262f2SGregor Boirie 
89703b262f2SGregor Boirie 	if (irq <= 0) {
89803b262f2SGregor Boirie 		/*
89903b262f2SGregor Boirie 		 * Platform declared no interrupt line: device will be polled
90003b262f2SGregor Boirie 		 * for data availability.
90103b262f2SGregor Boirie 		 */
90203b262f2SGregor Boirie 		dev_info(parent, "no interrupt found, running in polling mode");
90303b262f2SGregor Boirie 		return 0;
90403b262f2SGregor Boirie 	}
90503b262f2SGregor Boirie 
90603b262f2SGregor Boirie 	init_completion(&private->data_ready);
90703b262f2SGregor Boirie 
90803b262f2SGregor Boirie 	/* Request handler to be scheduled into threaded interrupt context. */
90903b262f2SGregor Boirie 	err = devm_request_threaded_irq(parent, irq, zpa2326_handle_irq,
91003b262f2SGregor Boirie 					zpa2326_handle_threaded_irq,
91103b262f2SGregor Boirie 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
91203b262f2SGregor Boirie 					dev_name(parent), indio_dev);
91303b262f2SGregor Boirie 	if (err) {
91403b262f2SGregor Boirie 		dev_err(parent, "failed to request interrupt %d (%d)", irq,
91503b262f2SGregor Boirie 			err);
91603b262f2SGregor Boirie 		return err;
91703b262f2SGregor Boirie 	}
91803b262f2SGregor Boirie 
91903b262f2SGregor Boirie 	dev_info(parent, "using interrupt %d", irq);
92003b262f2SGregor Boirie 
92103b262f2SGregor Boirie 	return 0;
92203b262f2SGregor Boirie }
92303b262f2SGregor Boirie 
92403b262f2SGregor Boirie /**
92503b262f2SGregor Boirie  * zpa2326_poll_oneshot_completion() - Actively poll for one shot data ready.
92603b262f2SGregor Boirie  * @indio_dev: The IIO device associated with the sampling hardware.
92703b262f2SGregor Boirie  *
92803b262f2SGregor Boirie  * Loop over registers content to detect end of sampling cycle. Used when DT
92903b262f2SGregor Boirie  * declared no valid interrupt lines.
93003b262f2SGregor Boirie  *
93103b262f2SGregor Boirie  * Return: Zero when successful, a negative error code otherwise.
93203b262f2SGregor Boirie  */
93303b262f2SGregor Boirie static int zpa2326_poll_oneshot_completion(const struct iio_dev *indio_dev)
93403b262f2SGregor Boirie {
93503b262f2SGregor Boirie 	unsigned long  tmout = jiffies + ZPA2326_CONVERSION_JIFFIES;
93603b262f2SGregor Boirie 	struct regmap *regs = ((struct zpa2326_private *)
93703b262f2SGregor Boirie 			       iio_priv(indio_dev))->regmap;
93803b262f2SGregor Boirie 	unsigned int   val;
93903b262f2SGregor Boirie 	int            err;
94003b262f2SGregor Boirie 
94103b262f2SGregor Boirie 	zpa2326_dbg(indio_dev, "polling for one shot completion");
94203b262f2SGregor Boirie 
94303b262f2SGregor Boirie 	/*
94403b262f2SGregor Boirie 	 * At least, 100 ms is needed for the device to complete its one-shot
94503b262f2SGregor Boirie 	 * cycle.
94603b262f2SGregor Boirie 	 */
94703b262f2SGregor Boirie 	if (msleep_interruptible(100))
94803b262f2SGregor Boirie 		return -ERESTARTSYS;
94903b262f2SGregor Boirie 
95003b262f2SGregor Boirie 	/* Poll for conversion completion in hardware. */
95103b262f2SGregor Boirie 	while (true) {
95203b262f2SGregor Boirie 		err = regmap_read(regs, ZPA2326_CTRL_REG0_REG, &val);
95303b262f2SGregor Boirie 		if (err < 0)
95403b262f2SGregor Boirie 			goto err;
95503b262f2SGregor Boirie 
95603b262f2SGregor Boirie 		if (!(val & ZPA2326_CTRL_REG0_ONE_SHOT))
95703b262f2SGregor Boirie 			/* One-shot bit self clears at conversion end. */
95803b262f2SGregor Boirie 			break;
95903b262f2SGregor Boirie 
96003b262f2SGregor Boirie 		if (time_after(jiffies, tmout)) {
96103b262f2SGregor Boirie 			/* Prevent from waiting forever : let's time out. */
96203b262f2SGregor Boirie 			err = -ETIME;
96303b262f2SGregor Boirie 			goto err;
96403b262f2SGregor Boirie 		}
96503b262f2SGregor Boirie 
96603b262f2SGregor Boirie 		usleep_range(10000, 20000);
96703b262f2SGregor Boirie 	}
96803b262f2SGregor Boirie 
96903b262f2SGregor Boirie 	/*
97003b262f2SGregor Boirie 	 * In oneshot mode, pressure sample availability guarantees that
97103b262f2SGregor Boirie 	 * temperature conversion has also completed : just check pressure
97203b262f2SGregor Boirie 	 * status bit to keep things simple.
97303b262f2SGregor Boirie 	 */
97403b262f2SGregor Boirie 	err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
97503b262f2SGregor Boirie 	if (err < 0)
97603b262f2SGregor Boirie 		goto err;
97703b262f2SGregor Boirie 
97803b262f2SGregor Boirie 	if (!(val & ZPA2326_STATUS_P_DA)) {
97903b262f2SGregor Boirie 		/* No sample available. */
98003b262f2SGregor Boirie 		err = -ENODATA;
98103b262f2SGregor Boirie 		goto err;
98203b262f2SGregor Boirie 	}
98303b262f2SGregor Boirie 
98403b262f2SGregor Boirie 	return 0;
98503b262f2SGregor Boirie 
98603b262f2SGregor Boirie err:
98703b262f2SGregor Boirie 	zpa2326_warn(indio_dev, "failed to poll one shot completion (%d)", err);
98803b262f2SGregor Boirie 
98903b262f2SGregor Boirie 	return err;
99003b262f2SGregor Boirie }
99103b262f2SGregor Boirie 
99203b262f2SGregor Boirie /**
99303b262f2SGregor Boirie  * zpa2326_fetch_raw_sample() - Retrieve a raw sample and convert it to CPU
99403b262f2SGregor Boirie  *                              endianness.
99503b262f2SGregor Boirie  * @indio_dev: The IIO device associated with the sampling hardware.
99603b262f2SGregor Boirie  * @type:      Type of measurement / channel to fetch from.
99703b262f2SGregor Boirie  * @value:     Sample output.
99803b262f2SGregor Boirie  *
99903b262f2SGregor Boirie  * Return: Zero when successful, a negative error code otherwise.
100003b262f2SGregor Boirie  */
100103b262f2SGregor Boirie static int zpa2326_fetch_raw_sample(const struct iio_dev *indio_dev,
100203b262f2SGregor Boirie 				    enum iio_chan_type    type,
100303b262f2SGregor Boirie 				    int                  *value)
100403b262f2SGregor Boirie {
100503b262f2SGregor Boirie 	struct regmap *regs = ((struct zpa2326_private *)
100603b262f2SGregor Boirie 			       iio_priv(indio_dev))->regmap;
100703b262f2SGregor Boirie 	int            err;
100803b262f2SGregor Boirie 
100903b262f2SGregor Boirie 	switch (type) {
101003b262f2SGregor Boirie 	case IIO_PRESSURE:
101103b262f2SGregor Boirie 		zpa2326_dbg(indio_dev, "fetching raw pressure sample");
101203b262f2SGregor Boirie 
101303b262f2SGregor Boirie 		err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, value,
101403b262f2SGregor Boirie 				       3);
101503b262f2SGregor Boirie 		if (err) {
101603b262f2SGregor Boirie 			zpa2326_warn(indio_dev, "failed to fetch pressure (%d)",
101703b262f2SGregor Boirie 				     err);
101803b262f2SGregor Boirie 			return err;
101903b262f2SGregor Boirie 		}
102003b262f2SGregor Boirie 
102103b262f2SGregor Boirie 		/* Pressure is a 24 bits wide little-endian unsigned int. */
102203b262f2SGregor Boirie 		*value = (((u8 *)value)[2] << 16) | (((u8 *)value)[1] << 8) |
102303b262f2SGregor Boirie 			 ((u8 *)value)[0];
102403b262f2SGregor Boirie 
102503b262f2SGregor Boirie 		return IIO_VAL_INT;
102603b262f2SGregor Boirie 
102703b262f2SGregor Boirie 	case IIO_TEMP:
102803b262f2SGregor Boirie 		zpa2326_dbg(indio_dev, "fetching raw temperature sample");
102903b262f2SGregor Boirie 
103003b262f2SGregor Boirie 		err = regmap_bulk_read(regs, ZPA2326_TEMP_OUT_L_REG, value, 2);
103103b262f2SGregor Boirie 		if (err) {
103203b262f2SGregor Boirie 			zpa2326_warn(indio_dev,
103303b262f2SGregor Boirie 				     "failed to fetch temperature (%d)", err);
103403b262f2SGregor Boirie 			return err;
103503b262f2SGregor Boirie 		}
103603b262f2SGregor Boirie 
103703b262f2SGregor Boirie 		/* Temperature is a 16 bits wide little-endian signed int. */
103803b262f2SGregor Boirie 		*value = (int)le16_to_cpup((__le16 *)value);
103903b262f2SGregor Boirie 
104003b262f2SGregor Boirie 		return IIO_VAL_INT;
104103b262f2SGregor Boirie 
104203b262f2SGregor Boirie 	default:
104303b262f2SGregor Boirie 		return -EINVAL;
104403b262f2SGregor Boirie 	}
104503b262f2SGregor Boirie }
104603b262f2SGregor Boirie 
104703b262f2SGregor Boirie /**
104803b262f2SGregor Boirie  * zpa2326_sample_oneshot() - Perform a complete one shot sampling cycle.
104903b262f2SGregor Boirie  * @indio_dev: The IIO device associated with the sampling hardware.
105003b262f2SGregor Boirie  * @type:      Type of measurement / channel to fetch from.
105103b262f2SGregor Boirie  * @value:     Sample output.
105203b262f2SGregor Boirie  *
105303b262f2SGregor Boirie  * Return: Zero when successful, a negative error code otherwise.
105403b262f2SGregor Boirie  */
105503b262f2SGregor Boirie static int zpa2326_sample_oneshot(struct iio_dev     *indio_dev,
105603b262f2SGregor Boirie 				  enum iio_chan_type  type,
105703b262f2SGregor Boirie 				  int                *value)
105803b262f2SGregor Boirie {
105903b262f2SGregor Boirie 	int                     ret;
106003b262f2SGregor Boirie 	struct zpa2326_private *priv;
106103b262f2SGregor Boirie 
106203b262f2SGregor Boirie 	ret = iio_device_claim_direct_mode(indio_dev);
106303b262f2SGregor Boirie 	if (ret)
106403b262f2SGregor Boirie 		return ret;
106503b262f2SGregor Boirie 
106603b262f2SGregor Boirie 	ret = zpa2326_resume(indio_dev);
106703b262f2SGregor Boirie 	if (ret < 0)
106803b262f2SGregor Boirie 		goto release;
106903b262f2SGregor Boirie 
107003b262f2SGregor Boirie 	priv = iio_priv(indio_dev);
107103b262f2SGregor Boirie 
107203b262f2SGregor Boirie 	if (ret > 0) {
107303b262f2SGregor Boirie 		/*
107403b262f2SGregor Boirie 		 * We were already power supplied. Just clear hardware FIFO to
107503b262f2SGregor Boirie 		 * get rid of samples acquired during previous rounds (if any).
107603b262f2SGregor Boirie 		 * Sampling operation always generates both temperature and
107703b262f2SGregor Boirie 		 * pressure samples. The latter are always enqueued into
107803b262f2SGregor Boirie 		 * hardware FIFO. This may lead to situations were pressure
107903b262f2SGregor Boirie 		 * samples still sit into FIFO when previous cycle(s) fetched
108003b262f2SGregor Boirie 		 * temperature data only.
108103b262f2SGregor Boirie 		 * Hence, we need to clear hardware FIFO content to prevent from
108203b262f2SGregor Boirie 		 * getting outdated values at the end of current cycle.
108303b262f2SGregor Boirie 		 */
108403b262f2SGregor Boirie 		if (type == IIO_PRESSURE) {
108503b262f2SGregor Boirie 			ret = zpa2326_clear_fifo(indio_dev, 0);
108603b262f2SGregor Boirie 			if (ret)
108703b262f2SGregor Boirie 				goto suspend;
108803b262f2SGregor Boirie 		}
108903b262f2SGregor Boirie 	} else {
109003b262f2SGregor Boirie 		/*
109103b262f2SGregor Boirie 		 * We have just been power supplied, i.e. device is in default
109203b262f2SGregor Boirie 		 * "out of reset" state, meaning we need to reconfigure it
109303b262f2SGregor Boirie 		 * entirely.
109403b262f2SGregor Boirie 		 */
109503b262f2SGregor Boirie 		ret = zpa2326_config_oneshot(indio_dev, priv->irq);
109603b262f2SGregor Boirie 		if (ret)
109703b262f2SGregor Boirie 			goto suspend;
109803b262f2SGregor Boirie 	}
109903b262f2SGregor Boirie 
110003b262f2SGregor Boirie 	/* Start a sampling cycle in oneshot mode. */
110103b262f2SGregor Boirie 	ret = zpa2326_start_oneshot(indio_dev);
110203b262f2SGregor Boirie 	if (ret)
110303b262f2SGregor Boirie 		goto suspend;
110403b262f2SGregor Boirie 
110503b262f2SGregor Boirie 	/* Wait for sampling cycle to complete. */
110603b262f2SGregor Boirie 	if (priv->irq > 0)
110703b262f2SGregor Boirie 		ret = zpa2326_wait_oneshot_completion(indio_dev, priv);
110803b262f2SGregor Boirie 	else
110903b262f2SGregor Boirie 		ret = zpa2326_poll_oneshot_completion(indio_dev);
111003b262f2SGregor Boirie 
111103b262f2SGregor Boirie 	if (ret)
111203b262f2SGregor Boirie 		goto suspend;
111303b262f2SGregor Boirie 
111403b262f2SGregor Boirie 	/* Retrieve raw sample value and convert it to CPU endianness. */
111503b262f2SGregor Boirie 	ret = zpa2326_fetch_raw_sample(indio_dev, type, value);
111603b262f2SGregor Boirie 
111703b262f2SGregor Boirie suspend:
111803b262f2SGregor Boirie 	zpa2326_suspend(indio_dev);
111903b262f2SGregor Boirie release:
112003b262f2SGregor Boirie 	iio_device_release_direct_mode(indio_dev);
112103b262f2SGregor Boirie 
112203b262f2SGregor Boirie 	return ret;
112303b262f2SGregor Boirie }
112403b262f2SGregor Boirie 
112503b262f2SGregor Boirie /**
112603b262f2SGregor Boirie  * zpa2326_trigger_handler() - Perform an IIO buffered sampling round in one
112703b262f2SGregor Boirie  *                             shot mode.
112803b262f2SGregor Boirie  * @irq:  The software interrupt assigned to @data
112903b262f2SGregor Boirie  * @data: The IIO poll function dispatched by external trigger our device is
113003b262f2SGregor Boirie  *        attached to.
113103b262f2SGregor Boirie  *
113203b262f2SGregor Boirie  * Bottom-half handler called by the IIO trigger to which our device is
113303b262f2SGregor Boirie  * currently attached. Allows us to synchronize this device buffered sampling
113403b262f2SGregor Boirie  * either with external events (such as timer expiration, external device sample
113503b262f2SGregor Boirie  * ready, etc...) or with its own interrupt (internal hardware trigger).
113603b262f2SGregor Boirie  *
113703b262f2SGregor Boirie  * When using an external trigger, basically run the same sequence of operations
113803b262f2SGregor Boirie  * as for zpa2326_sample_oneshot() with the following hereafter. Hardware FIFO
113903b262f2SGregor Boirie  * is not cleared since already done at buffering enable time and samples
114003b262f2SGregor Boirie  * dequeueing always retrieves the most recent value.
114103b262f2SGregor Boirie  *
114203b262f2SGregor Boirie  * Otherwise, when internal hardware trigger has dispatched us, just fetch data
114303b262f2SGregor Boirie  * from hardware FIFO.
114403b262f2SGregor Boirie  *
114503b262f2SGregor Boirie  * Fetched data will pushed unprocessed to IIO buffer since samples conversion
114603b262f2SGregor Boirie  * is delegated to userspace in buffered mode (endianness, etc...).
114703b262f2SGregor Boirie  *
114803b262f2SGregor Boirie  * Return:
114903b262f2SGregor Boirie  *   %IRQ_NONE - no consistent interrupt happened ;
115003b262f2SGregor Boirie  *   %IRQ_HANDLED - there was new samples available.
115103b262f2SGregor Boirie  */
115203b262f2SGregor Boirie static irqreturn_t zpa2326_trigger_handler(int irq, void *data)
115303b262f2SGregor Boirie {
115403b262f2SGregor Boirie 	struct iio_dev         *indio_dev = ((struct iio_poll_func *)
115503b262f2SGregor Boirie 					     data)->indio_dev;
115603b262f2SGregor Boirie 	struct zpa2326_private *priv = iio_priv(indio_dev);
115703b262f2SGregor Boirie 	bool                    cont;
115803b262f2SGregor Boirie 
115903b262f2SGregor Boirie 	/*
116003b262f2SGregor Boirie 	 * We have been dispatched, meaning we are in triggered buffer mode.
116103b262f2SGregor Boirie 	 * Using our own internal trigger implies we are currently in continuous
116203b262f2SGregor Boirie 	 * hardware sampling mode.
116303b262f2SGregor Boirie 	 */
116403b262f2SGregor Boirie 	cont = iio_trigger_using_own(indio_dev);
116503b262f2SGregor Boirie 
116603b262f2SGregor Boirie 	if (!cont) {
116703b262f2SGregor Boirie 		/* On demand sampling : start a one shot cycle. */
116803b262f2SGregor Boirie 		if (zpa2326_start_oneshot(indio_dev))
116903b262f2SGregor Boirie 			goto out;
117003b262f2SGregor Boirie 
117103b262f2SGregor Boirie 		/* Wait for sampling cycle to complete. */
117203b262f2SGregor Boirie 		if (priv->irq <= 0) {
117303b262f2SGregor Boirie 			/* No interrupt available: poll for completion. */
117403b262f2SGregor Boirie 			if (zpa2326_poll_oneshot_completion(indio_dev))
117503b262f2SGregor Boirie 				goto out;
117603b262f2SGregor Boirie 
117703b262f2SGregor Boirie 			/* Only timestamp sample once it is ready. */
117803b262f2SGregor Boirie 			priv->timestamp = iio_get_time_ns(indio_dev);
117903b262f2SGregor Boirie 		} else {
118003b262f2SGregor Boirie 			/* Interrupt handlers will timestamp for us. */
118103b262f2SGregor Boirie 			if (zpa2326_wait_oneshot_completion(indio_dev, priv))
118203b262f2SGregor Boirie 				goto out;
118303b262f2SGregor Boirie 		}
118403b262f2SGregor Boirie 	}
118503b262f2SGregor Boirie 
118603b262f2SGregor Boirie 	/* Enqueue to IIO buffer / userspace. */
118703b262f2SGregor Boirie 	zpa2326_fill_sample_buffer(indio_dev, priv);
118803b262f2SGregor Boirie 
118903b262f2SGregor Boirie out:
119003b262f2SGregor Boirie 	if (!cont)
119103b262f2SGregor Boirie 		/* Don't switch to low power if sampling continuously. */
119203b262f2SGregor Boirie 		zpa2326_sleep(indio_dev);
119303b262f2SGregor Boirie 
119403b262f2SGregor Boirie 	/* Inform attached trigger we are done. */
119503b262f2SGregor Boirie 	iio_trigger_notify_done(indio_dev->trig);
119603b262f2SGregor Boirie 
119703b262f2SGregor Boirie 	return IRQ_HANDLED;
119803b262f2SGregor Boirie }
119903b262f2SGregor Boirie 
120003b262f2SGregor Boirie /**
120103b262f2SGregor Boirie  * zpa2326_preenable_buffer() - Prepare device for configuring triggered
120203b262f2SGregor Boirie  *                              sampling
120303b262f2SGregor Boirie  * modes.
120403b262f2SGregor Boirie  * @indio_dev: The IIO device associated with the sampling hardware.
120503b262f2SGregor Boirie  *
120603b262f2SGregor Boirie  * Basically power up device.
120703b262f2SGregor Boirie  * Called with IIO device's lock held.
120803b262f2SGregor Boirie  *
120903b262f2SGregor Boirie  * Return: Zero when successful, a negative error code otherwise.
121003b262f2SGregor Boirie  */
121103b262f2SGregor Boirie static int zpa2326_preenable_buffer(struct iio_dev *indio_dev)
121203b262f2SGregor Boirie {
121303b262f2SGregor Boirie 	int ret = zpa2326_resume(indio_dev);
121403b262f2SGregor Boirie 
121503b262f2SGregor Boirie 	if (ret < 0)
121603b262f2SGregor Boirie 		return ret;
121703b262f2SGregor Boirie 
121803b262f2SGregor Boirie 	/* Tell zpa2326_postenable_buffer() if we have just been powered on. */
121903b262f2SGregor Boirie 	((struct zpa2326_private *)
122003b262f2SGregor Boirie 	 iio_priv(indio_dev))->waken = iio_priv(indio_dev);
122103b262f2SGregor Boirie 
122203b262f2SGregor Boirie 	return 0;
122303b262f2SGregor Boirie }
122403b262f2SGregor Boirie 
122503b262f2SGregor Boirie /**
122603b262f2SGregor Boirie  * zpa2326_postenable_buffer() - Configure device for triggered sampling.
122703b262f2SGregor Boirie  * @indio_dev: The IIO device associated with the sampling hardware.
122803b262f2SGregor Boirie  *
122903b262f2SGregor Boirie  * Basically setup one-shot mode if plugging external trigger.
123003b262f2SGregor Boirie  * Otherwise, let internal trigger configure continuous sampling :
123103b262f2SGregor Boirie  * see zpa2326_set_trigger_state().
123203b262f2SGregor Boirie  *
123303b262f2SGregor Boirie  * If an error is returned, IIO layer will call our postdisable hook for us,
123403b262f2SGregor Boirie  * i.e. no need to explicitly power device off here.
123503b262f2SGregor Boirie  * Called with IIO device's lock held.
123603b262f2SGregor Boirie  *
123703b262f2SGregor Boirie  * Called with IIO device's lock held.
123803b262f2SGregor Boirie  *
123903b262f2SGregor Boirie  * Return: Zero when successful, a negative error code otherwise.
124003b262f2SGregor Boirie  */
124103b262f2SGregor Boirie static int zpa2326_postenable_buffer(struct iio_dev *indio_dev)
124203b262f2SGregor Boirie {
124303b262f2SGregor Boirie 	const struct zpa2326_private *priv = iio_priv(indio_dev);
124403b262f2SGregor Boirie 	int                           err;
124503b262f2SGregor Boirie 
1246*fe2392c6SAlexandru Ardelean 	/* Plug our own trigger event handler. */
1247*fe2392c6SAlexandru Ardelean 	err = iio_triggered_buffer_postenable(indio_dev);
1248*fe2392c6SAlexandru Ardelean 	if (err)
1249*fe2392c6SAlexandru Ardelean 		goto err;
1250*fe2392c6SAlexandru Ardelean 
125103b262f2SGregor Boirie 	if (!priv->waken) {
125203b262f2SGregor Boirie 		/*
125303b262f2SGregor Boirie 		 * We were already power supplied. Just clear hardware FIFO to
125403b262f2SGregor Boirie 		 * get rid of samples acquired during previous rounds (if any).
125503b262f2SGregor Boirie 		 */
125603b262f2SGregor Boirie 		err = zpa2326_clear_fifo(indio_dev, 0);
125703b262f2SGregor Boirie 		if (err)
1258*fe2392c6SAlexandru Ardelean 			goto err_buffer_predisable;
125903b262f2SGregor Boirie 	}
126003b262f2SGregor Boirie 
126103b262f2SGregor Boirie 	if (!iio_trigger_using_own(indio_dev) && priv->waken) {
126203b262f2SGregor Boirie 		/*
126303b262f2SGregor Boirie 		 * We are using an external trigger and we have just been
126403b262f2SGregor Boirie 		 * powered up: reconfigure one-shot mode.
126503b262f2SGregor Boirie 		 */
126603b262f2SGregor Boirie 		err = zpa2326_config_oneshot(indio_dev, priv->irq);
126703b262f2SGregor Boirie 		if (err)
1268*fe2392c6SAlexandru Ardelean 			goto err_buffer_predisable;
126903b262f2SGregor Boirie 	}
127003b262f2SGregor Boirie 
127103b262f2SGregor Boirie 	return 0;
127203b262f2SGregor Boirie 
1273*fe2392c6SAlexandru Ardelean err_buffer_predisable:
1274*fe2392c6SAlexandru Ardelean 	iio_triggered_buffer_predisable(indio_dev);
127503b262f2SGregor Boirie err:
127603b262f2SGregor Boirie 	zpa2326_err(indio_dev, "failed to enable buffering (%d)", err);
127703b262f2SGregor Boirie 
127803b262f2SGregor Boirie 	return err;
127903b262f2SGregor Boirie }
128003b262f2SGregor Boirie 
128103b262f2SGregor Boirie static int zpa2326_postdisable_buffer(struct iio_dev *indio_dev)
128203b262f2SGregor Boirie {
128303b262f2SGregor Boirie 	zpa2326_suspend(indio_dev);
128403b262f2SGregor Boirie 
128503b262f2SGregor Boirie 	return 0;
128603b262f2SGregor Boirie }
128703b262f2SGregor Boirie 
128803b262f2SGregor Boirie static const struct iio_buffer_setup_ops zpa2326_buffer_setup_ops = {
128903b262f2SGregor Boirie 	.preenable   = zpa2326_preenable_buffer,
129003b262f2SGregor Boirie 	.postenable  = zpa2326_postenable_buffer,
129103b262f2SGregor Boirie 	.predisable  = iio_triggered_buffer_predisable,
129203b262f2SGregor Boirie 	.postdisable = zpa2326_postdisable_buffer
129303b262f2SGregor Boirie };
129403b262f2SGregor Boirie 
129503b262f2SGregor Boirie /**
129603b262f2SGregor Boirie  * zpa2326_set_trigger_state() - Start / stop continuous sampling.
129703b262f2SGregor Boirie  * @trig:  The trigger being attached to IIO device associated with the sampling
129803b262f2SGregor Boirie  *         hardware.
129903b262f2SGregor Boirie  * @state: Tell whether to start (true) or stop (false)
130003b262f2SGregor Boirie  *
130103b262f2SGregor Boirie  * Basically enable / disable hardware continuous sampling mode.
130203b262f2SGregor Boirie  *
130303b262f2SGregor Boirie  * Called with IIO device's lock held at postenable() or predisable() time.
130403b262f2SGregor Boirie  *
130503b262f2SGregor Boirie  * Return: Zero when successful, a negative error code otherwise.
130603b262f2SGregor Boirie  */
130703b262f2SGregor Boirie static int zpa2326_set_trigger_state(struct iio_trigger *trig, bool state)
130803b262f2SGregor Boirie {
130903b262f2SGregor Boirie 	const struct iio_dev         *indio_dev = dev_get_drvdata(
131003b262f2SGregor Boirie 							trig->dev.parent);
131103b262f2SGregor Boirie 	const struct zpa2326_private *priv = iio_priv(indio_dev);
131203b262f2SGregor Boirie 	int                           err;
131303b262f2SGregor Boirie 
131403b262f2SGregor Boirie 	if (!state) {
131503b262f2SGregor Boirie 		/*
131603b262f2SGregor Boirie 		 * Switch trigger off : in case of failure, interrupt is left
131703b262f2SGregor Boirie 		 * disabled in order to prevent handler from accessing released
131803b262f2SGregor Boirie 		 * resources.
131903b262f2SGregor Boirie 		 */
132003b262f2SGregor Boirie 		unsigned int val;
132103b262f2SGregor Boirie 
132203b262f2SGregor Boirie 		/*
132303b262f2SGregor Boirie 		 * As device is working in continuous mode, handlers may be
132403b262f2SGregor Boirie 		 * accessing resources we are currently freeing...
132503b262f2SGregor Boirie 		 * Prevent this by disabling interrupt handlers and ensure
132603b262f2SGregor Boirie 		 * the device will generate no more interrupts unless explicitly
132703b262f2SGregor Boirie 		 * required to, i.e. by restoring back to default one shot mode.
132803b262f2SGregor Boirie 		 */
132903b262f2SGregor Boirie 		disable_irq(priv->irq);
133003b262f2SGregor Boirie 
133103b262f2SGregor Boirie 		/*
133203b262f2SGregor Boirie 		 * Disable continuous sampling mode to restore settings for
133303b262f2SGregor Boirie 		 * one shot / direct sampling operations.
133403b262f2SGregor Boirie 		 */
133503b262f2SGregor Boirie 		err = regmap_write(priv->regmap, ZPA2326_CTRL_REG3_REG,
133603b262f2SGregor Boirie 				   zpa2326_highest_frequency()->odr);
133703b262f2SGregor Boirie 		if (err)
133803b262f2SGregor Boirie 			return err;
133903b262f2SGregor Boirie 
134003b262f2SGregor Boirie 		/*
134103b262f2SGregor Boirie 		 * Now that device won't generate interrupts on its own,
134203b262f2SGregor Boirie 		 * acknowledge any currently active interrupts (may happen on
134303b262f2SGregor Boirie 		 * rare occasions while stopping continuous mode).
134403b262f2SGregor Boirie 		 */
134503b262f2SGregor Boirie 		err = regmap_read(priv->regmap, ZPA2326_INT_SOURCE_REG, &val);
134603b262f2SGregor Boirie 		if (err < 0)
134703b262f2SGregor Boirie 			return err;
134803b262f2SGregor Boirie 
134903b262f2SGregor Boirie 		/*
135003b262f2SGregor Boirie 		 * Re-enable interrupts only if we can guarantee the device will
135103b262f2SGregor Boirie 		 * generate no more interrupts to prevent handlers from
135203b262f2SGregor Boirie 		 * accessing released resources.
135303b262f2SGregor Boirie 		 */
135403b262f2SGregor Boirie 		enable_irq(priv->irq);
135503b262f2SGregor Boirie 
135603b262f2SGregor Boirie 		zpa2326_dbg(indio_dev, "continuous mode stopped");
135703b262f2SGregor Boirie 	} else {
135803b262f2SGregor Boirie 		/*
135903b262f2SGregor Boirie 		 * Switch trigger on : start continuous sampling at required
136003b262f2SGregor Boirie 		 * frequency.
136103b262f2SGregor Boirie 		 */
136203b262f2SGregor Boirie 
136303b262f2SGregor Boirie 		if (priv->waken) {
136403b262f2SGregor Boirie 			/* Enable interrupt if getting out of reset. */
136503b262f2SGregor Boirie 			err = regmap_write(priv->regmap, ZPA2326_CTRL_REG1_REG,
136603b262f2SGregor Boirie 					   (u8)
136703b262f2SGregor Boirie 					   ~ZPA2326_CTRL_REG1_MASK_DATA_READY);
136803b262f2SGregor Boirie 			if (err)
136903b262f2SGregor Boirie 				return err;
137003b262f2SGregor Boirie 		}
137103b262f2SGregor Boirie 
137203b262f2SGregor Boirie 		/* Enable continuous sampling at specified frequency. */
137303b262f2SGregor Boirie 		err = regmap_write(priv->regmap, ZPA2326_CTRL_REG3_REG,
137403b262f2SGregor Boirie 				   ZPA2326_CTRL_REG3_ENABLE_MEAS |
137503b262f2SGregor Boirie 				   priv->frequency->odr);
137603b262f2SGregor Boirie 		if (err)
137703b262f2SGregor Boirie 			return err;
137803b262f2SGregor Boirie 
137903b262f2SGregor Boirie 		zpa2326_dbg(indio_dev, "continuous mode setup @%dHz",
138003b262f2SGregor Boirie 			    priv->frequency->hz);
138103b262f2SGregor Boirie 	}
138203b262f2SGregor Boirie 
138303b262f2SGregor Boirie 	return 0;
138403b262f2SGregor Boirie }
138503b262f2SGregor Boirie 
138603b262f2SGregor Boirie static const struct iio_trigger_ops zpa2326_trigger_ops = {
138703b262f2SGregor Boirie 	.set_trigger_state = zpa2326_set_trigger_state,
138803b262f2SGregor Boirie };
138903b262f2SGregor Boirie 
139003b262f2SGregor Boirie /**
139103b262f2SGregor Boirie  * zpa2326_init_trigger() - Create an interrupt driven / hardware trigger
139203b262f2SGregor Boirie  *                          allowing to notify external devices a new sample is
139303b262f2SGregor Boirie  *                          ready.
139403b262f2SGregor Boirie  * @parent:    Hardware sampling device @indio_dev is a child of.
139503b262f2SGregor Boirie  * @indio_dev: The IIO device associated with the sampling hardware.
139603b262f2SGregor Boirie  * @private:   Internal private state related to @indio_dev.
139703b262f2SGregor Boirie  * @irq:       Optional interrupt line the hardware uses to notify new data
139803b262f2SGregor Boirie  *             samples are ready. Negative or zero values indicate no interrupts
139903b262f2SGregor Boirie  *             are available, meaning polling is required.
140003b262f2SGregor Boirie  *
140103b262f2SGregor Boirie  * Only relevant when DT declares a valid interrupt line.
140203b262f2SGregor Boirie  *
140303b262f2SGregor Boirie  * Return: Zero when successful, a negative error code otherwise.
140403b262f2SGregor Boirie  */
140503b262f2SGregor Boirie static int zpa2326_init_managed_trigger(struct device          *parent,
140603b262f2SGregor Boirie 					struct iio_dev         *indio_dev,
140703b262f2SGregor Boirie 					struct zpa2326_private *private,
140803b262f2SGregor Boirie 					int                     irq)
140903b262f2SGregor Boirie {
141003b262f2SGregor Boirie 	struct iio_trigger *trigger;
141103b262f2SGregor Boirie 	int                 ret;
141203b262f2SGregor Boirie 
141303b262f2SGregor Boirie 	if (irq <= 0)
141403b262f2SGregor Boirie 		return 0;
141503b262f2SGregor Boirie 
141603b262f2SGregor Boirie 	trigger = devm_iio_trigger_alloc(parent, "%s-dev%d",
141703b262f2SGregor Boirie 					 indio_dev->name, indio_dev->id);
141803b262f2SGregor Boirie 	if (!trigger)
141903b262f2SGregor Boirie 		return -ENOMEM;
142003b262f2SGregor Boirie 
142103b262f2SGregor Boirie 	/* Basic setup. */
142203b262f2SGregor Boirie 	trigger->dev.parent = parent;
142303b262f2SGregor Boirie 	trigger->ops = &zpa2326_trigger_ops;
142403b262f2SGregor Boirie 
142503b262f2SGregor Boirie 	private->trigger = trigger;
142603b262f2SGregor Boirie 
142703b262f2SGregor Boirie 	/* Register to triggers space. */
142803b262f2SGregor Boirie 	ret = devm_iio_trigger_register(parent, trigger);
142903b262f2SGregor Boirie 	if (ret)
143003b262f2SGregor Boirie 		dev_err(parent, "failed to register hardware trigger (%d)",
143103b262f2SGregor Boirie 			ret);
143203b262f2SGregor Boirie 
143303b262f2SGregor Boirie 	return ret;
143403b262f2SGregor Boirie }
143503b262f2SGregor Boirie 
143603b262f2SGregor Boirie static int zpa2326_get_frequency(const struct iio_dev *indio_dev)
143703b262f2SGregor Boirie {
143803b262f2SGregor Boirie 	return ((struct zpa2326_private *)iio_priv(indio_dev))->frequency->hz;
143903b262f2SGregor Boirie }
144003b262f2SGregor Boirie 
144103b262f2SGregor Boirie static int zpa2326_set_frequency(struct iio_dev *indio_dev, int hz)
144203b262f2SGregor Boirie {
144303b262f2SGregor Boirie 	struct zpa2326_private *priv = iio_priv(indio_dev);
144403b262f2SGregor Boirie 	int                     freq;
144503b262f2SGregor Boirie 	int                     err;
144603b262f2SGregor Boirie 
144703b262f2SGregor Boirie 	/* Check if requested frequency is supported. */
144803b262f2SGregor Boirie 	for (freq = 0; freq < ARRAY_SIZE(zpa2326_sampling_frequencies); freq++)
144903b262f2SGregor Boirie 		if (zpa2326_sampling_frequencies[freq].hz == hz)
145003b262f2SGregor Boirie 			break;
145103b262f2SGregor Boirie 	if (freq == ARRAY_SIZE(zpa2326_sampling_frequencies))
145203b262f2SGregor Boirie 		return -EINVAL;
145303b262f2SGregor Boirie 
145403b262f2SGregor Boirie 	/* Don't allow changing frequency if buffered sampling is ongoing. */
145503b262f2SGregor Boirie 	err = iio_device_claim_direct_mode(indio_dev);
145603b262f2SGregor Boirie 	if (err)
145703b262f2SGregor Boirie 		return err;
145803b262f2SGregor Boirie 
145903b262f2SGregor Boirie 	priv->frequency = &zpa2326_sampling_frequencies[freq];
146003b262f2SGregor Boirie 
146103b262f2SGregor Boirie 	iio_device_release_direct_mode(indio_dev);
146203b262f2SGregor Boirie 
146303b262f2SGregor Boirie 	return 0;
146403b262f2SGregor Boirie }
146503b262f2SGregor Boirie 
146603b262f2SGregor Boirie /* Expose supported hardware sampling frequencies (Hz) through sysfs. */
146703b262f2SGregor Boirie static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("1 5 11 23");
146803b262f2SGregor Boirie 
146903b262f2SGregor Boirie static struct attribute *zpa2326_attributes[] = {
147003b262f2SGregor Boirie 	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
147103b262f2SGregor Boirie 	NULL
147203b262f2SGregor Boirie };
147303b262f2SGregor Boirie 
147403b262f2SGregor Boirie static const struct attribute_group zpa2326_attribute_group = {
147503b262f2SGregor Boirie 	.attrs = zpa2326_attributes,
147603b262f2SGregor Boirie };
147703b262f2SGregor Boirie 
147803b262f2SGregor Boirie static int zpa2326_read_raw(struct iio_dev             *indio_dev,
147903b262f2SGregor Boirie 			    struct iio_chan_spec const *chan,
148003b262f2SGregor Boirie 			    int                        *val,
148103b262f2SGregor Boirie 			    int                        *val2,
148203b262f2SGregor Boirie 			    long                        mask)
148303b262f2SGregor Boirie {
148403b262f2SGregor Boirie 	switch (mask) {
148503b262f2SGregor Boirie 	case IIO_CHAN_INFO_RAW:
148603b262f2SGregor Boirie 		return zpa2326_sample_oneshot(indio_dev, chan->type, val);
148703b262f2SGregor Boirie 
148803b262f2SGregor Boirie 	case IIO_CHAN_INFO_SCALE:
148903b262f2SGregor Boirie 		switch (chan->type) {
149003b262f2SGregor Boirie 		case IIO_PRESSURE:
149103b262f2SGregor Boirie 			/*
149203b262f2SGregor Boirie 			 * Pressure resolution is 1/64 Pascal. Scale to kPascal
149303b262f2SGregor Boirie 			 * as required by IIO ABI.
149403b262f2SGregor Boirie 			 */
149503b262f2SGregor Boirie 			*val = 1;
149603b262f2SGregor Boirie 			*val2 = 64000;
149703b262f2SGregor Boirie 			return IIO_VAL_FRACTIONAL;
149803b262f2SGregor Boirie 
149903b262f2SGregor Boirie 		case IIO_TEMP:
150003b262f2SGregor Boirie 			/*
150103b262f2SGregor Boirie 			 * Temperature follows the equation:
150203b262f2SGregor Boirie 			 *     Temp[degC] = Tempcode * 0.00649 - 176.83
150303b262f2SGregor Boirie 			 * where:
150403b262f2SGregor Boirie 			 *     Tempcode is composed the raw sampled 16 bits.
150503b262f2SGregor Boirie 			 *
150603b262f2SGregor Boirie 			 * Hence, to produce a temperature in milli-degrees
150703b262f2SGregor Boirie 			 * Celsius according to IIO ABI, we need to apply the
150803b262f2SGregor Boirie 			 * following equation to raw samples:
150903b262f2SGregor Boirie 			 *     Temp[milli degC] = (Tempcode + Offset) * Scale
151003b262f2SGregor Boirie 			 * where:
151103b262f2SGregor Boirie 			 *     Offset = -176.83 / 0.00649
151203b262f2SGregor Boirie 			 *     Scale = 0.00649 * 1000
151303b262f2SGregor Boirie 			 */
151403b262f2SGregor Boirie 			*val = 6;
151503b262f2SGregor Boirie 			*val2 = 490000;
151603b262f2SGregor Boirie 			return IIO_VAL_INT_PLUS_MICRO;
151703b262f2SGregor Boirie 
151803b262f2SGregor Boirie 		default:
151903b262f2SGregor Boirie 			return -EINVAL;
152003b262f2SGregor Boirie 		}
152103b262f2SGregor Boirie 
152203b262f2SGregor Boirie 	case IIO_CHAN_INFO_OFFSET:
152303b262f2SGregor Boirie 		switch (chan->type) {
152403b262f2SGregor Boirie 		case IIO_TEMP:
152503b262f2SGregor Boirie 			*val = -17683000;
152603b262f2SGregor Boirie 			*val2 = 649;
152703b262f2SGregor Boirie 			return IIO_VAL_FRACTIONAL;
152803b262f2SGregor Boirie 
152903b262f2SGregor Boirie 		default:
153003b262f2SGregor Boirie 			return -EINVAL;
153103b262f2SGregor Boirie 		}
153203b262f2SGregor Boirie 
153303b262f2SGregor Boirie 	case IIO_CHAN_INFO_SAMP_FREQ:
153403b262f2SGregor Boirie 		*val = zpa2326_get_frequency(indio_dev);
153503b262f2SGregor Boirie 		return IIO_VAL_INT;
153603b262f2SGregor Boirie 
153703b262f2SGregor Boirie 	default:
153803b262f2SGregor Boirie 		return -EINVAL;
153903b262f2SGregor Boirie 	}
154003b262f2SGregor Boirie }
154103b262f2SGregor Boirie 
154203b262f2SGregor Boirie static int zpa2326_write_raw(struct iio_dev             *indio_dev,
154303b262f2SGregor Boirie 			     const struct iio_chan_spec *chan,
154403b262f2SGregor Boirie 			     int                         val,
154503b262f2SGregor Boirie 			     int                         val2,
154603b262f2SGregor Boirie 			     long                        mask)
154703b262f2SGregor Boirie {
154803b262f2SGregor Boirie 	if ((mask != IIO_CHAN_INFO_SAMP_FREQ) || val2)
154903b262f2SGregor Boirie 		return -EINVAL;
155003b262f2SGregor Boirie 
155103b262f2SGregor Boirie 	return zpa2326_set_frequency(indio_dev, val);
155203b262f2SGregor Boirie }
155303b262f2SGregor Boirie 
155403b262f2SGregor Boirie static const struct iio_chan_spec zpa2326_channels[] = {
155503b262f2SGregor Boirie 	[0] = {
155603b262f2SGregor Boirie 		.type                    = IIO_PRESSURE,
155703b262f2SGregor Boirie 		.scan_index              = 0,
155803b262f2SGregor Boirie 		.scan_type               = {
155903b262f2SGregor Boirie 			.sign                   = 'u',
156003b262f2SGregor Boirie 			.realbits               = 24,
156103b262f2SGregor Boirie 			.storagebits            = 32,
156203b262f2SGregor Boirie 			.endianness             = IIO_LE,
156303b262f2SGregor Boirie 		},
156403b262f2SGregor Boirie 		.info_mask_separate      = BIT(IIO_CHAN_INFO_RAW) |
156503b262f2SGregor Boirie 					   BIT(IIO_CHAN_INFO_SCALE),
156603b262f2SGregor Boirie 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
156703b262f2SGregor Boirie 	},
156803b262f2SGregor Boirie 	[1] = {
156903b262f2SGregor Boirie 		.type                    = IIO_TEMP,
157003b262f2SGregor Boirie 		.scan_index              = 1,
157103b262f2SGregor Boirie 		.scan_type               = {
157203b262f2SGregor Boirie 			.sign                   = 's',
157303b262f2SGregor Boirie 			.realbits               = 16,
157403b262f2SGregor Boirie 			.storagebits            = 16,
157503b262f2SGregor Boirie 			.endianness             = IIO_LE,
157603b262f2SGregor Boirie 		},
157703b262f2SGregor Boirie 		.info_mask_separate      = BIT(IIO_CHAN_INFO_RAW) |
157803b262f2SGregor Boirie 					   BIT(IIO_CHAN_INFO_SCALE) |
157903b262f2SGregor Boirie 					   BIT(IIO_CHAN_INFO_OFFSET),
158003b262f2SGregor Boirie 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
158103b262f2SGregor Boirie 	},
158203b262f2SGregor Boirie 	[2] = IIO_CHAN_SOFT_TIMESTAMP(2),
158303b262f2SGregor Boirie };
158403b262f2SGregor Boirie 
158503b262f2SGregor Boirie static const struct iio_info zpa2326_info = {
158603b262f2SGregor Boirie 	.attrs         = &zpa2326_attribute_group,
158703b262f2SGregor Boirie 	.read_raw      = zpa2326_read_raw,
158803b262f2SGregor Boirie 	.write_raw     = zpa2326_write_raw,
158903b262f2SGregor Boirie };
159003b262f2SGregor Boirie 
159103b262f2SGregor Boirie static struct iio_dev *zpa2326_create_managed_iiodev(struct device *device,
159203b262f2SGregor Boirie 						     const char    *name,
159303b262f2SGregor Boirie 						     struct regmap *regmap)
159403b262f2SGregor Boirie {
159503b262f2SGregor Boirie 	struct iio_dev *indio_dev;
159603b262f2SGregor Boirie 
159703b262f2SGregor Boirie 	/* Allocate space to hold IIO device internal state. */
159803b262f2SGregor Boirie 	indio_dev = devm_iio_device_alloc(device,
159903b262f2SGregor Boirie 					  sizeof(struct zpa2326_private));
160003b262f2SGregor Boirie 	if (!indio_dev)
160103b262f2SGregor Boirie 		return NULL;
160203b262f2SGregor Boirie 
160303b262f2SGregor Boirie 	/* Setup for userspace synchronous on demand sampling. */
160403b262f2SGregor Boirie 	indio_dev->modes = INDIO_DIRECT_MODE;
160503b262f2SGregor Boirie 	indio_dev->dev.parent = device;
160603b262f2SGregor Boirie 	indio_dev->channels = zpa2326_channels;
160703b262f2SGregor Boirie 	indio_dev->num_channels = ARRAY_SIZE(zpa2326_channels);
160803b262f2SGregor Boirie 	indio_dev->name = name;
160903b262f2SGregor Boirie 	indio_dev->info = &zpa2326_info;
161003b262f2SGregor Boirie 
161103b262f2SGregor Boirie 	return indio_dev;
161203b262f2SGregor Boirie }
161303b262f2SGregor Boirie 
161403b262f2SGregor Boirie int zpa2326_probe(struct device *parent,
161503b262f2SGregor Boirie 		  const char    *name,
161603b262f2SGregor Boirie 		  int            irq,
161703b262f2SGregor Boirie 		  unsigned int   hwid,
161803b262f2SGregor Boirie 		  struct regmap *regmap)
161903b262f2SGregor Boirie {
162003b262f2SGregor Boirie 	struct iio_dev         *indio_dev;
162103b262f2SGregor Boirie 	struct zpa2326_private *priv;
162203b262f2SGregor Boirie 	int                     err;
162303b262f2SGregor Boirie 	unsigned int            id;
162403b262f2SGregor Boirie 
162503b262f2SGregor Boirie 	indio_dev = zpa2326_create_managed_iiodev(parent, name, regmap);
162603b262f2SGregor Boirie 	if (!indio_dev)
162703b262f2SGregor Boirie 		return -ENOMEM;
162803b262f2SGregor Boirie 
162903b262f2SGregor Boirie 	priv = iio_priv(indio_dev);
163003b262f2SGregor Boirie 
163103b262f2SGregor Boirie 	priv->vref = devm_regulator_get(parent, "vref");
163203b262f2SGregor Boirie 	if (IS_ERR(priv->vref))
163303b262f2SGregor Boirie 		return PTR_ERR(priv->vref);
163403b262f2SGregor Boirie 
163503b262f2SGregor Boirie 	priv->vdd = devm_regulator_get(parent, "vdd");
163603b262f2SGregor Boirie 	if (IS_ERR(priv->vdd))
163703b262f2SGregor Boirie 		return PTR_ERR(priv->vdd);
163803b262f2SGregor Boirie 
163903b262f2SGregor Boirie 	/* Set default hardware sampling frequency to highest rate supported. */
164003b262f2SGregor Boirie 	priv->frequency = zpa2326_highest_frequency();
164103b262f2SGregor Boirie 
164203b262f2SGregor Boirie 	/*
164303b262f2SGregor Boirie 	 * Plug device's underlying bus abstraction : this MUST be set before
164403b262f2SGregor Boirie 	 * registering interrupt handlers since an interrupt might happen if
164503b262f2SGregor Boirie 	 * power up sequence is not properly applied.
164603b262f2SGregor Boirie 	 */
164703b262f2SGregor Boirie 	priv->regmap = regmap;
164803b262f2SGregor Boirie 
164903b262f2SGregor Boirie 	err = devm_iio_triggered_buffer_setup(parent, indio_dev, NULL,
165003b262f2SGregor Boirie 					      zpa2326_trigger_handler,
165103b262f2SGregor Boirie 					      &zpa2326_buffer_setup_ops);
165203b262f2SGregor Boirie 	if (err)
165303b262f2SGregor Boirie 		return err;
165403b262f2SGregor Boirie 
165503b262f2SGregor Boirie 	err = zpa2326_init_managed_trigger(parent, indio_dev, priv, irq);
165603b262f2SGregor Boirie 	if (err)
165703b262f2SGregor Boirie 		return err;
165803b262f2SGregor Boirie 
165903b262f2SGregor Boirie 	err = zpa2326_init_managed_irq(parent, indio_dev, priv, irq);
166003b262f2SGregor Boirie 	if (err)
166103b262f2SGregor Boirie 		return err;
166203b262f2SGregor Boirie 
166303b262f2SGregor Boirie 	/* Power up to check device ID and perform initial hardware setup. */
166403b262f2SGregor Boirie 	err = zpa2326_power_on(indio_dev, priv);
166503b262f2SGregor Boirie 	if (err)
166603b262f2SGregor Boirie 		return err;
166703b262f2SGregor Boirie 
166803b262f2SGregor Boirie 	/* Read id register to check we are talking to the right slave. */
166903b262f2SGregor Boirie 	err = regmap_read(regmap, ZPA2326_DEVICE_ID_REG, &id);
167003b262f2SGregor Boirie 	if (err)
167103b262f2SGregor Boirie 		goto sleep;
167203b262f2SGregor Boirie 
167303b262f2SGregor Boirie 	if (id != hwid) {
167403b262f2SGregor Boirie 		dev_err(parent, "found device with unexpected id %02x", id);
167503b262f2SGregor Boirie 		err = -ENODEV;
167603b262f2SGregor Boirie 		goto sleep;
167703b262f2SGregor Boirie 	}
167803b262f2SGregor Boirie 
167903b262f2SGregor Boirie 	err = zpa2326_config_oneshot(indio_dev, irq);
168003b262f2SGregor Boirie 	if (err)
168103b262f2SGregor Boirie 		goto sleep;
168203b262f2SGregor Boirie 
168303b262f2SGregor Boirie 	/* Setup done : go sleeping. Device will be awaken upon user request. */
168403b262f2SGregor Boirie 	err = zpa2326_sleep(indio_dev);
168503b262f2SGregor Boirie 	if (err)
168603b262f2SGregor Boirie 		goto poweroff;
168703b262f2SGregor Boirie 
168803b262f2SGregor Boirie 	dev_set_drvdata(parent, indio_dev);
168903b262f2SGregor Boirie 
169003b262f2SGregor Boirie 	zpa2326_init_runtime(parent);
169103b262f2SGregor Boirie 
169203b262f2SGregor Boirie 	err = iio_device_register(indio_dev);
169303b262f2SGregor Boirie 	if (err) {
169403b262f2SGregor Boirie 		zpa2326_fini_runtime(parent);
169503b262f2SGregor Boirie 		goto poweroff;
169603b262f2SGregor Boirie 	}
169703b262f2SGregor Boirie 
169803b262f2SGregor Boirie 	return 0;
169903b262f2SGregor Boirie 
170003b262f2SGregor Boirie sleep:
170103b262f2SGregor Boirie 	/* Put to sleep just in case power regulators are "dummy" ones. */
170203b262f2SGregor Boirie 	zpa2326_sleep(indio_dev);
170303b262f2SGregor Boirie poweroff:
170403b262f2SGregor Boirie 	zpa2326_power_off(indio_dev, priv);
170503b262f2SGregor Boirie 
170603b262f2SGregor Boirie 	return err;
170703b262f2SGregor Boirie }
170803b262f2SGregor Boirie EXPORT_SYMBOL_GPL(zpa2326_probe);
170903b262f2SGregor Boirie 
171003b262f2SGregor Boirie void zpa2326_remove(const struct device *parent)
171103b262f2SGregor Boirie {
171203b262f2SGregor Boirie 	struct iio_dev *indio_dev = dev_get_drvdata(parent);
171303b262f2SGregor Boirie 
171403b262f2SGregor Boirie 	iio_device_unregister(indio_dev);
171503b262f2SGregor Boirie 	zpa2326_fini_runtime(indio_dev->dev.parent);
171603b262f2SGregor Boirie 	zpa2326_sleep(indio_dev);
171703b262f2SGregor Boirie 	zpa2326_power_off(indio_dev, iio_priv(indio_dev));
171803b262f2SGregor Boirie }
171903b262f2SGregor Boirie EXPORT_SYMBOL_GPL(zpa2326_remove);
172003b262f2SGregor Boirie 
172103b262f2SGregor Boirie MODULE_AUTHOR("Gregor Boirie <gregor.boirie@parrot.com>");
172203b262f2SGregor Boirie MODULE_DESCRIPTION("Core driver for Murata ZPA2326 pressure sensor");
172303b262f2SGregor Boirie MODULE_LICENSE("GPL v2");
1724