^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Murata ZPA2326 pressure and temperature sensor IIO driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2016 Parrot S.A.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Author: Gregor Boirie <gregor.boirie@parrot.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * DOC: ZPA2326 theory of operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * This driver supports %INDIO_DIRECT_MODE and %INDIO_BUFFER_TRIGGERED IIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * modes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * A internal hardware trigger is also implemented to dispatch registered IIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * trigger consumers upon "sample ready" interrupts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * ZPA2326 hardware supports 2 sampling mode: one shot and continuous.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * A complete one shot sampling cycle gets device out of low power mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * performs pressure and temperature measurements, then automatically switches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * back to low power mode. It is meant for on demand sampling with optimal power
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * saving at the cost of lower sampling rate and higher software overhead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * This is a natural candidate for IIO read_raw hook implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * (%INDIO_DIRECT_MODE). It is also used for triggered buffering support to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * ensure explicit synchronization with external trigger events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * (%INDIO_BUFFER_TRIGGERED).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * The continuous mode works according to a periodic hardware measurement
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * process continuously pushing samples into an internal hardware FIFO (for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * pressure samples only). Measurement cycle completion may be signaled by a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * "sample ready" interrupt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * Typical software sequence of operations :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * - get device out of low power mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * - setup hardware sampling period,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * - at end of period, upon data ready interrupt: pop pressure samples out of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * hardware FIFO and fetch temperature sample
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * - when no longer needed, stop sampling process by putting device into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * low power mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * This mode is used to implement %INDIO_BUFFER_TRIGGERED mode if device tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * declares a valid interrupt line. In this case, the internal hardware trigger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * drives acquisition.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * Note that hardware sampling frequency is taken into account only when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * internal hardware trigger is attached as the highest sampling rate seems to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * be the most energy efficient.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * TODO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * preset pressure threshold crossing / IIO events ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * differential pressure sampling ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * hardware samples averaging.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #include <linux/regulator/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #include <linux/iio/iio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #include <linux/iio/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #include <linux/iio/buffer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #include <linux/iio/trigger.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #include <linux/iio/trigger_consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #include <linux/iio/triggered_buffer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #include "zpa2326.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /* 200 ms should be enough for the longest conversion time in one-shot mode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #define ZPA2326_CONVERSION_JIFFIES (HZ / 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /* There should be a 1 ms delay (Tpup) after getting out of reset. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #define ZPA2326_TPUP_USEC_MIN (1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #define ZPA2326_TPUP_USEC_MAX (2000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * struct zpa2326_frequency - Hardware sampling frequency descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * @hz : Frequency in Hertz.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * @odr: Output Data Rate word as expected by %ZPA2326_CTRL_REG3_REG.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct zpa2326_frequency {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) int hz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) u16 odr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * Keep these in strict ascending order: last array entry is expected to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * correspond to the highest sampling frequency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) static const struct zpa2326_frequency zpa2326_sampling_frequencies[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) { .hz = 1, .odr = 1 << ZPA2326_CTRL_REG3_ODR_SHIFT },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) { .hz = 5, .odr = 5 << ZPA2326_CTRL_REG3_ODR_SHIFT },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) { .hz = 11, .odr = 6 << ZPA2326_CTRL_REG3_ODR_SHIFT },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) { .hz = 23, .odr = 7 << ZPA2326_CTRL_REG3_ODR_SHIFT },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /* Return the highest hardware sampling frequency available. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) static const struct zpa2326_frequency *zpa2326_highest_frequency(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return &zpa2326_sampling_frequencies[
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) ARRAY_SIZE(zpa2326_sampling_frequencies) - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * struct zpa_private - Per-device internal private state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * @timestamp: Buffered samples ready datum.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * @regmap: Underlying I2C / SPI bus adapter used to abstract slave register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * accesses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * @result: Allows sampling logic to get completion status of operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * that interrupt handlers perform asynchronously.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * @data_ready: Interrupt handler uses this to wake user context up at sampling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * operation completion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * @trigger: Optional hardware / interrupt driven trigger used to notify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * external devices a new sample is ready.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * @waken: Flag indicating whether or not device has just been powered on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * @irq: Optional interrupt line: negative or zero if not declared into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * DT, in which case sampling logic keeps polling status register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * to detect completion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * @frequency: Current hardware sampling frequency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * @vref: Power / voltage reference.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * @vdd: Power supply.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct zpa2326_private {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) s64 timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct completion data_ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct iio_trigger *trigger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) bool waken;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) const struct zpa2326_frequency *frequency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct regulator *vref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct regulator *vdd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) #define zpa2326_err(idev, fmt, ...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) dev_err(idev->dev.parent, fmt "\n", ##__VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) #define zpa2326_warn(idev, fmt, ...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) dev_warn(idev->dev.parent, fmt "\n", ##__VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) #define zpa2326_dbg(idev, fmt, ...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) dev_dbg(idev->dev.parent, fmt "\n", ##__VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) bool zpa2326_isreg_writeable(struct device *dev, unsigned int reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) switch (reg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) case ZPA2326_REF_P_XL_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) case ZPA2326_REF_P_L_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) case ZPA2326_REF_P_H_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) case ZPA2326_RES_CONF_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) case ZPA2326_CTRL_REG0_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) case ZPA2326_CTRL_REG1_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) case ZPA2326_CTRL_REG2_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) case ZPA2326_CTRL_REG3_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) case ZPA2326_THS_P_LOW_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) case ZPA2326_THS_P_HIGH_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) EXPORT_SYMBOL_GPL(zpa2326_isreg_writeable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) bool zpa2326_isreg_readable(struct device *dev, unsigned int reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) switch (reg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) case ZPA2326_REF_P_XL_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) case ZPA2326_REF_P_L_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) case ZPA2326_REF_P_H_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) case ZPA2326_DEVICE_ID_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) case ZPA2326_RES_CONF_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) case ZPA2326_CTRL_REG0_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) case ZPA2326_CTRL_REG1_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) case ZPA2326_CTRL_REG2_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) case ZPA2326_CTRL_REG3_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) case ZPA2326_INT_SOURCE_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) case ZPA2326_THS_P_LOW_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) case ZPA2326_THS_P_HIGH_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) case ZPA2326_STATUS_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) case ZPA2326_PRESS_OUT_XL_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) case ZPA2326_PRESS_OUT_L_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) case ZPA2326_PRESS_OUT_H_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) case ZPA2326_TEMP_OUT_L_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) case ZPA2326_TEMP_OUT_H_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) EXPORT_SYMBOL_GPL(zpa2326_isreg_readable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) bool zpa2326_isreg_precious(struct device *dev, unsigned int reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) switch (reg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) case ZPA2326_INT_SOURCE_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) case ZPA2326_PRESS_OUT_H_REG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) EXPORT_SYMBOL_GPL(zpa2326_isreg_precious);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * zpa2326_enable_device() - Enable device, i.e. get out of low power mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * @indio_dev: The IIO device associated with the hardware to enable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * Required to access complete register space and to perform any sampling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * or control operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * Return: Zero when successful, a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static int zpa2326_enable_device(const struct iio_dev *indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) err = regmap_write(((struct zpa2326_private *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) iio_priv(indio_dev))->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) ZPA2326_CTRL_REG0_REG, ZPA2326_CTRL_REG0_ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) zpa2326_err(indio_dev, "failed to enable device (%d)", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) zpa2326_dbg(indio_dev, "enabled");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) * zpa2326_sleep() - Disable device, i.e. switch to low power mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * @indio_dev: The IIO device associated with the hardware to disable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * Only %ZPA2326_DEVICE_ID_REG and %ZPA2326_CTRL_REG0_REG registers may be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * accessed once device is in the disabled state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * Return: Zero when successful, a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) static int zpa2326_sleep(const struct iio_dev *indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) err = regmap_write(((struct zpa2326_private *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) iio_priv(indio_dev))->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) ZPA2326_CTRL_REG0_REG, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) zpa2326_err(indio_dev, "failed to sleep (%d)", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) zpa2326_dbg(indio_dev, "sleeping");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * zpa2326_reset_device() - Reset device to default hardware state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * @indio_dev: The IIO device associated with the hardware to reset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * Disable sampling and empty hardware FIFO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * Device must be enabled before reset, i.e. not in low power mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * Return: Zero when successful, a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static int zpa2326_reset_device(const struct iio_dev *indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) err = regmap_write(((struct zpa2326_private *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) iio_priv(indio_dev))->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) ZPA2326_CTRL_REG2_REG, ZPA2326_CTRL_REG2_SWRESET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) zpa2326_err(indio_dev, "failed to reset device (%d)", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) usleep_range(ZPA2326_TPUP_USEC_MIN, ZPA2326_TPUP_USEC_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) zpa2326_dbg(indio_dev, "reset");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * zpa2326_start_oneshot() - Start a single sampling cycle, i.e. in one shot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * @indio_dev: The IIO device associated with the sampling hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * Device must have been previously enabled and configured for one shot mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * Device will be switched back to low power mode at end of cycle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * Return: Zero when successful, a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static int zpa2326_start_oneshot(const struct iio_dev *indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) err = regmap_write(((struct zpa2326_private *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) iio_priv(indio_dev))->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) ZPA2326_CTRL_REG0_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) ZPA2326_CTRL_REG0_ENABLE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) ZPA2326_CTRL_REG0_ONE_SHOT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) zpa2326_err(indio_dev, "failed to start one shot cycle (%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) zpa2326_dbg(indio_dev, "one shot cycle started");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * zpa2326_power_on() - Power on device to allow subsequent configuration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * @indio_dev: The IIO device associated with the sampling hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * @private: Internal private state related to @indio_dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * Sampling will be disabled, preventing strange things from happening in our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * back. Hardware FIFO content will be cleared.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * When successful, device will be left in the enabled state to allow further
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * configuration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * Return: Zero when successful, a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) static int zpa2326_power_on(const struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) const struct zpa2326_private *private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) err = regulator_enable(private->vref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) err = regulator_enable(private->vdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) goto vref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) zpa2326_dbg(indio_dev, "powered on");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) err = zpa2326_enable_device(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) goto vdd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) err = zpa2326_reset_device(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) goto sleep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) sleep:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) zpa2326_sleep(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) vdd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) regulator_disable(private->vdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) vref:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) regulator_disable(private->vref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) zpa2326_dbg(indio_dev, "powered off");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) * zpa2326_power_off() - Power off device, i.e. disable attached power
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) * regulators.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) * @indio_dev: The IIO device associated with the sampling hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) * @private: Internal private state related to @indio_dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) * Return: Zero when successful, a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) static void zpa2326_power_off(const struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) const struct zpa2326_private *private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) regulator_disable(private->vdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) regulator_disable(private->vref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) zpa2326_dbg(indio_dev, "powered off");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * zpa2326_config_oneshot() - Setup device for one shot / on demand mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * @indio_dev: The IIO device associated with the sampling hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * @irq: Optional interrupt line the hardware uses to notify new data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) * samples are ready. Negative or zero values indicate no interrupts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) * are available, meaning polling is required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * Output Data Rate is configured for the highest possible rate so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * conversion time and power consumption are reduced to a minimum.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) * Note that hardware internal averaging machinery (not implemented in this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * driver) is not applicable in this mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) * Device must have been previously enabled before calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) * zpa2326_config_oneshot().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * Return: Zero when successful, a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) static int zpa2326_config_oneshot(const struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) int irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) struct regmap *regs = ((struct zpa2326_private *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) iio_priv(indio_dev))->regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) const struct zpa2326_frequency *freq = zpa2326_highest_frequency();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) /* Setup highest available Output Data Rate for one shot mode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) err = regmap_write(regs, ZPA2326_CTRL_REG3_REG, freq->odr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (irq > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) /* Request interrupt when new sample is available. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) err = regmap_write(regs, ZPA2326_CTRL_REG1_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) (u8)~ZPA2326_CTRL_REG1_MASK_DATA_READY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) dev_err(indio_dev->dev.parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) "failed to setup one shot mode (%d)", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) zpa2326_dbg(indio_dev, "one shot mode setup @%dHz", freq->hz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) * zpa2326_clear_fifo() - Clear remaining entries in hardware FIFO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) * @indio_dev: The IIO device associated with the sampling hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) * @min_count: Number of samples present within hardware FIFO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) * @min_count argument is a hint corresponding to the known minimum number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) * samples currently living in the FIFO. This allows to reduce the number of bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) * accesses by skipping status register read operation as long as we know for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) * sure there are still entries left.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) * Return: Zero when successful, a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) static int zpa2326_clear_fifo(const struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) unsigned int min_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) struct regmap *regs = ((struct zpa2326_private *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) iio_priv(indio_dev))->regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) unsigned int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) if (!min_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) * No hint: read status register to determine whether FIFO is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) * empty or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) if (val & ZPA2326_STATUS_FIFO_E)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) /* Fifo is empty: nothing to trash. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) /* Clear FIFO. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) * A single fetch from pressure MSB register is enough to pop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) * values out of FIFO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) err = regmap_read(regs, ZPA2326_PRESS_OUT_H_REG, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) if (min_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) * We know for sure there are at least min_count entries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) * left in FIFO. Skip status register read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) min_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) } while (!(val & ZPA2326_STATUS_FIFO_E));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) zpa2326_dbg(indio_dev, "FIFO cleared");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) zpa2326_err(indio_dev, "failed to clear FIFO (%d)", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) * zpa2326_dequeue_pressure() - Retrieve the most recent pressure sample from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) * hardware FIFO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) * @indio_dev: The IIO device associated with the sampling hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) * @pressure: Sampled pressure output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) * Note that ZPA2326 hardware FIFO stores pressure samples only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) * Return: Zero when successful, a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) static int zpa2326_dequeue_pressure(const struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) u32 *pressure)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) struct regmap *regs = ((struct zpa2326_private *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) iio_priv(indio_dev))->regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) unsigned int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) int cleared = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) *pressure = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) if (val & ZPA2326_STATUS_P_OR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) * Fifo overrun : first sample dequeued from FIFO is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) * newest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) zpa2326_warn(indio_dev, "FIFO overflow");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, pressure,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) #define ZPA2326_FIFO_DEPTH (16U)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) /* Hardware FIFO may hold no more than 16 pressure samples. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) return zpa2326_clear_fifo(indio_dev, ZPA2326_FIFO_DEPTH - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) * Fifo has not overflown : retrieve newest sample. We need to pop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) * values out until FIFO is empty : last fetched pressure is the newest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) * In nominal cases, we should find a single queued sample only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, pressure,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) cleared++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) } while (!(val & ZPA2326_STATUS_FIFO_E));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) if (cleared)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) * Samples were pushed by hardware during previous rounds but we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) * didn't consume them fast enough: inform user.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) zpa2326_dbg(indio_dev, "cleared %d FIFO entries", cleared);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) * zpa2326_fill_sample_buffer() - Enqueue new channel samples to IIO buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) * @indio_dev: The IIO device associated with the sampling hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) * @private: Internal private state related to @indio_dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) * Return: Zero when successful, a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) static int zpa2326_fill_sample_buffer(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) const struct zpa2326_private *private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) u32 pressure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) u16 temperature;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) u64 timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) } sample;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) if (test_bit(0, indio_dev->active_scan_mask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) /* Get current pressure from hardware FIFO. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) err = zpa2326_dequeue_pressure(indio_dev, &sample.pressure);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) zpa2326_warn(indio_dev, "failed to fetch pressure (%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) if (test_bit(1, indio_dev->active_scan_mask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) /* Get current temperature. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) err = regmap_bulk_read(private->regmap, ZPA2326_TEMP_OUT_L_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) &sample.temperature, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) zpa2326_warn(indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) "failed to fetch temperature (%d)", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) * Now push samples using timestamp stored either :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) * - by hardware interrupt handler if interrupt is available: see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) * zpa2326_handle_irq(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) * - or oneshot completion polling machinery : see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) * zpa2326_trigger_handler().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) zpa2326_dbg(indio_dev, "filling raw samples buffer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) iio_push_to_buffers_with_timestamp(indio_dev, &sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) private->timestamp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) static int zpa2326_runtime_suspend(struct device *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) const struct iio_dev *indio_dev = dev_get_drvdata(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) if (pm_runtime_autosuspend_expiration(parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) /* Userspace changed autosuspend delay. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) zpa2326_power_off(indio_dev, iio_priv(indio_dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) static int zpa2326_runtime_resume(struct device *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) const struct iio_dev *indio_dev = dev_get_drvdata(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) return zpa2326_power_on(indio_dev, iio_priv(indio_dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) const struct dev_pm_ops zpa2326_pm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) pm_runtime_force_resume)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) SET_RUNTIME_PM_OPS(zpa2326_runtime_suspend, zpa2326_runtime_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) EXPORT_SYMBOL_GPL(zpa2326_pm_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) * zpa2326_resume() - Request the PM layer to power supply the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) * @indio_dev: The IIO device associated with the sampling hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) * < 0 - a negative error code meaning failure ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) * 0 - success, device has just been powered up ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) * 1 - success, device was already powered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) static int zpa2326_resume(const struct iio_dev *indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) err = pm_runtime_get_sync(indio_dev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) pm_runtime_put(indio_dev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) if (err > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) * Device was already power supplied: get it out of low power
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) * mode and inform caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) zpa2326_enable_device(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) /* Inform caller device has just been brought back to life. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) * zpa2326_suspend() - Schedule a power down using autosuspend feature of PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) * layer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) * @indio_dev: The IIO device associated with the sampling hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) * Device is switched to low power mode at first to save power even when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) * attached regulator is a "dummy" one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) static void zpa2326_suspend(struct iio_dev *indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) struct device *parent = indio_dev->dev.parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) zpa2326_sleep(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) pm_runtime_mark_last_busy(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) pm_runtime_put_autosuspend(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) static void zpa2326_init_runtime(struct device *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) pm_runtime_get_noresume(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) pm_runtime_set_active(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) pm_runtime_enable(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) pm_runtime_set_autosuspend_delay(parent, 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) pm_runtime_use_autosuspend(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) pm_runtime_mark_last_busy(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) pm_runtime_put_autosuspend(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) static void zpa2326_fini_runtime(struct device *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) pm_runtime_disable(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) pm_runtime_set_suspended(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) #else /* !CONFIG_PM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) static int zpa2326_resume(const struct iio_dev *indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) zpa2326_enable_device(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) static void zpa2326_suspend(struct iio_dev *indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) zpa2326_sleep(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) #define zpa2326_init_runtime(_parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) #define zpa2326_fini_runtime(_parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) #endif /* !CONFIG_PM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) * zpa2326_handle_irq() - Process hardware interrupts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) * @irq: Interrupt line the hardware uses to notify new data has arrived.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) * @data: The IIO device associated with the sampling hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) * Timestamp buffered samples as soon as possible then schedule threaded bottom
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) * half.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) * Return: Always successful.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) static irqreturn_t zpa2326_handle_irq(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) struct iio_dev *indio_dev = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) if (iio_buffer_enabled(indio_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) /* Timestamping needed for buffered sampling only. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) ((struct zpa2326_private *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) iio_priv(indio_dev))->timestamp = iio_get_time_ns(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) return IRQ_WAKE_THREAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) * zpa2326_handle_threaded_irq() - Interrupt bottom-half handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) * @irq: Interrupt line the hardware uses to notify new data has arrived.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) * @data: The IIO device associated with the sampling hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) * Mainly ensures interrupt is caused by a real "new sample available"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) * condition. This relies upon the ability to perform blocking / sleeping bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) * accesses to slave's registers. This is why zpa2326_handle_threaded_irq() is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) * called from within a thread, i.e. not called from hard interrupt context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) * When device is using its own internal hardware trigger in continuous sampling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) * mode, data are available into hardware FIFO once interrupt has occurred. All
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) * we have to do is to dispatch the trigger, which in turn will fetch data and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) * fill IIO buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) * When not using its own internal hardware trigger, the device has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) * configured in one-shot mode either by an external trigger or the IIO read_raw
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) * hook. This means one of the latter is currently waiting for sampling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) * completion, in which case we must simply wake it up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) * See zpa2326_trigger_handler().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) * %IRQ_NONE - no consistent interrupt happened ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) * %IRQ_HANDLED - there was new samples available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) static irqreturn_t zpa2326_handle_threaded_irq(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) struct iio_dev *indio_dev = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) struct zpa2326_private *priv = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) unsigned int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) bool cont;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) irqreturn_t ret = IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) * Are we using our own internal trigger in triggered buffer mode, i.e.,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) * currently working in continuous sampling mode ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) cont = (iio_buffer_enabled(indio_dev) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) iio_trigger_using_own(indio_dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) * Device works according to a level interrupt scheme: reading interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) * status de-asserts interrupt line.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) priv->result = regmap_read(priv->regmap, ZPA2326_INT_SOURCE_REG, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) if (priv->result < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) if (cont)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) goto complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) /* Data ready is the only interrupt source we requested. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) if (!(val & ZPA2326_INT_SOURCE_DATA_READY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) * Interrupt happened but no new sample available: likely caused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) * by spurious interrupts, in which case, returning IRQ_NONE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) * allows to benefit from the generic spurious interrupts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) * handling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) zpa2326_warn(indio_dev, "unexpected interrupt status %02x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) if (cont)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) priv->result = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) goto complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) /* New sample available: dispatch internal trigger consumers. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) iio_trigger_poll_chained(priv->trigger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) if (cont)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) * Internal hardware trigger has been scheduled above : it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) * fetch data on its own.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) ret = IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) complete:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) * Wake up direct or externaly triggered buffer mode waiters: see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) * zpa2326_sample_oneshot() and zpa2326_trigger_handler().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) complete(&priv->data_ready);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) * zpa2326_wait_oneshot_completion() - Wait for oneshot data ready interrupt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) * @indio_dev: The IIO device associated with the sampling hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) * @private: Internal private state related to @indio_dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) * Return: Zero when successful, a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) static int zpa2326_wait_oneshot_completion(const struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) struct zpa2326_private *private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) unsigned int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) long timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) zpa2326_dbg(indio_dev, "waiting for one shot completion interrupt");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) timeout = wait_for_completion_interruptible_timeout(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) &private->data_ready, ZPA2326_CONVERSION_JIFFIES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) if (timeout > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) * Interrupt handler completed before timeout: return operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) * status.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) return private->result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) /* Clear all interrupts just to be sure. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) regmap_read(private->regmap, ZPA2326_INT_SOURCE_REG, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) if (!timeout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) /* Timed out. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) zpa2326_warn(indio_dev, "no one shot interrupt occurred (%ld)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) return -ETIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) zpa2326_warn(indio_dev, "wait for one shot interrupt cancelled");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) static int zpa2326_init_managed_irq(struct device *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) struct zpa2326_private *private,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) int irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) private->irq = irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) if (irq <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) * Platform declared no interrupt line: device will be polled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) * for data availability.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) dev_info(parent, "no interrupt found, running in polling mode");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) init_completion(&private->data_ready);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) /* Request handler to be scheduled into threaded interrupt context. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) err = devm_request_threaded_irq(parent, irq, zpa2326_handle_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) zpa2326_handle_threaded_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) IRQF_TRIGGER_RISING | IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) dev_name(parent), indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) dev_err(parent, "failed to request interrupt %d (%d)", irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) dev_info(parent, "using interrupt %d", irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) * zpa2326_poll_oneshot_completion() - Actively poll for one shot data ready.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) * @indio_dev: The IIO device associated with the sampling hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) * Loop over registers content to detect end of sampling cycle. Used when DT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) * declared no valid interrupt lines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) * Return: Zero when successful, a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) static int zpa2326_poll_oneshot_completion(const struct iio_dev *indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) unsigned long tmout = jiffies + ZPA2326_CONVERSION_JIFFIES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) struct regmap *regs = ((struct zpa2326_private *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) iio_priv(indio_dev))->regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) unsigned int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) zpa2326_dbg(indio_dev, "polling for one shot completion");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) * At least, 100 ms is needed for the device to complete its one-shot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) * cycle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) if (msleep_interruptible(100))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) /* Poll for conversion completion in hardware. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) while (true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) err = regmap_read(regs, ZPA2326_CTRL_REG0_REG, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) if (!(val & ZPA2326_CTRL_REG0_ONE_SHOT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) /* One-shot bit self clears at conversion end. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) if (time_after(jiffies, tmout)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) /* Prevent from waiting forever : let's time out. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) err = -ETIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) usleep_range(10000, 20000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) * In oneshot mode, pressure sample availability guarantees that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) * temperature conversion has also completed : just check pressure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) * status bit to keep things simple.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) if (!(val & ZPA2326_STATUS_P_DA)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) /* No sample available. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) err = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) zpa2326_warn(indio_dev, "failed to poll one shot completion (%d)", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) * zpa2326_fetch_raw_sample() - Retrieve a raw sample and convert it to CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) * endianness.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) * @indio_dev: The IIO device associated with the sampling hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) * @type: Type of measurement / channel to fetch from.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) * @value: Sample output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) * Return: Zero when successful, a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) static int zpa2326_fetch_raw_sample(const struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) enum iio_chan_type type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) int *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) struct regmap *regs = ((struct zpa2326_private *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) iio_priv(indio_dev))->regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) u8 v[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) case IIO_PRESSURE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) zpa2326_dbg(indio_dev, "fetching raw pressure sample");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, v, sizeof(v));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) zpa2326_warn(indio_dev, "failed to fetch pressure (%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) *value = get_unaligned_le24(&v[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) return IIO_VAL_INT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) case IIO_TEMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) zpa2326_dbg(indio_dev, "fetching raw temperature sample");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) err = regmap_bulk_read(regs, ZPA2326_TEMP_OUT_L_REG, value, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) zpa2326_warn(indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) "failed to fetch temperature (%d)", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) /* Temperature is a 16 bits wide little-endian signed int. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) *value = (int)le16_to_cpup((__le16 *)value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) return IIO_VAL_INT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) * zpa2326_sample_oneshot() - Perform a complete one shot sampling cycle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) * @indio_dev: The IIO device associated with the sampling hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) * @type: Type of measurement / channel to fetch from.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) * @value: Sample output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) * Return: Zero when successful, a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) static int zpa2326_sample_oneshot(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) enum iio_chan_type type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) int *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) struct zpa2326_private *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) ret = iio_device_claim_direct_mode(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) ret = zpa2326_resume(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) goto release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) priv = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) * We were already power supplied. Just clear hardware FIFO to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) * get rid of samples acquired during previous rounds (if any).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) * Sampling operation always generates both temperature and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) * pressure samples. The latter are always enqueued into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) * hardware FIFO. This may lead to situations were pressure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) * samples still sit into FIFO when previous cycle(s) fetched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) * temperature data only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) * Hence, we need to clear hardware FIFO content to prevent from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) * getting outdated values at the end of current cycle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) if (type == IIO_PRESSURE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) ret = zpa2326_clear_fifo(indio_dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) goto suspend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) * We have just been power supplied, i.e. device is in default
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) * "out of reset" state, meaning we need to reconfigure it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) * entirely.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) ret = zpa2326_config_oneshot(indio_dev, priv->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) goto suspend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) /* Start a sampling cycle in oneshot mode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) ret = zpa2326_start_oneshot(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) goto suspend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) /* Wait for sampling cycle to complete. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) if (priv->irq > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) ret = zpa2326_wait_oneshot_completion(indio_dev, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) ret = zpa2326_poll_oneshot_completion(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) goto suspend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) /* Retrieve raw sample value and convert it to CPU endianness. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) ret = zpa2326_fetch_raw_sample(indio_dev, type, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) suspend:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) zpa2326_suspend(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) release:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) iio_device_release_direct_mode(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) * zpa2326_trigger_handler() - Perform an IIO buffered sampling round in one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) * shot mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) * @irq: The software interrupt assigned to @data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) * @data: The IIO poll function dispatched by external trigger our device is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) * attached to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) * Bottom-half handler called by the IIO trigger to which our device is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) * currently attached. Allows us to synchronize this device buffered sampling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) * either with external events (such as timer expiration, external device sample
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) * ready, etc...) or with its own interrupt (internal hardware trigger).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) * When using an external trigger, basically run the same sequence of operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) * as for zpa2326_sample_oneshot() with the following hereafter. Hardware FIFO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) * is not cleared since already done at buffering enable time and samples
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) * dequeueing always retrieves the most recent value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) * Otherwise, when internal hardware trigger has dispatched us, just fetch data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) * from hardware FIFO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) * Fetched data will pushed unprocessed to IIO buffer since samples conversion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) * is delegated to userspace in buffered mode (endianness, etc...).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) * %IRQ_NONE - no consistent interrupt happened ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) * %IRQ_HANDLED - there was new samples available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) static irqreturn_t zpa2326_trigger_handler(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) struct iio_dev *indio_dev = ((struct iio_poll_func *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) data)->indio_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) struct zpa2326_private *priv = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) bool cont;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) * We have been dispatched, meaning we are in triggered buffer mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) * Using our own internal trigger implies we are currently in continuous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) * hardware sampling mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) cont = iio_trigger_using_own(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) if (!cont) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) /* On demand sampling : start a one shot cycle. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) if (zpa2326_start_oneshot(indio_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) /* Wait for sampling cycle to complete. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) if (priv->irq <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) /* No interrupt available: poll for completion. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) if (zpa2326_poll_oneshot_completion(indio_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) /* Only timestamp sample once it is ready. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) priv->timestamp = iio_get_time_ns(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) /* Interrupt handlers will timestamp for us. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) if (zpa2326_wait_oneshot_completion(indio_dev, priv))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) /* Enqueue to IIO buffer / userspace. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) zpa2326_fill_sample_buffer(indio_dev, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) if (!cont)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) /* Don't switch to low power if sampling continuously. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) zpa2326_sleep(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) /* Inform attached trigger we are done. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) iio_trigger_notify_done(indio_dev->trig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) * zpa2326_preenable_buffer() - Prepare device for configuring triggered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) * sampling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) * modes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) * @indio_dev: The IIO device associated with the sampling hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) * Basically power up device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) * Called with IIO device's lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) * Return: Zero when successful, a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) static int zpa2326_preenable_buffer(struct iio_dev *indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) int ret = zpa2326_resume(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) /* Tell zpa2326_postenable_buffer() if we have just been powered on. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) ((struct zpa2326_private *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) iio_priv(indio_dev))->waken = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) * zpa2326_postenable_buffer() - Configure device for triggered sampling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) * @indio_dev: The IIO device associated with the sampling hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) * Basically setup one-shot mode if plugging external trigger.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) * Otherwise, let internal trigger configure continuous sampling :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) * see zpa2326_set_trigger_state().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) * If an error is returned, IIO layer will call our postdisable hook for us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) * i.e. no need to explicitly power device off here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) * Called with IIO device's lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) * Called with IIO device's lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) * Return: Zero when successful, a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) static int zpa2326_postenable_buffer(struct iio_dev *indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) const struct zpa2326_private *priv = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) if (!priv->waken) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) * We were already power supplied. Just clear hardware FIFO to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) * get rid of samples acquired during previous rounds (if any).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) err = zpa2326_clear_fifo(indio_dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) zpa2326_err(indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) "failed to enable buffering (%d)", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) if (!iio_trigger_using_own(indio_dev) && priv->waken) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) * We are using an external trigger and we have just been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) * powered up: reconfigure one-shot mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) err = zpa2326_config_oneshot(indio_dev, priv->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) zpa2326_err(indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) "failed to enable buffering (%d)", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) static int zpa2326_postdisable_buffer(struct iio_dev *indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) zpa2326_suspend(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) static const struct iio_buffer_setup_ops zpa2326_buffer_setup_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) .preenable = zpa2326_preenable_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) .postenable = zpa2326_postenable_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) .postdisable = zpa2326_postdisable_buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) * zpa2326_set_trigger_state() - Start / stop continuous sampling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) * @trig: The trigger being attached to IIO device associated with the sampling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) * hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) * @state: Tell whether to start (true) or stop (false)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) * Basically enable / disable hardware continuous sampling mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) * Called with IIO device's lock held at postenable() or predisable() time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) * Return: Zero when successful, a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) static int zpa2326_set_trigger_state(struct iio_trigger *trig, bool state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) const struct iio_dev *indio_dev = dev_get_drvdata(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) trig->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) const struct zpa2326_private *priv = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) if (!state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) * Switch trigger off : in case of failure, interrupt is left
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) * disabled in order to prevent handler from accessing released
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) * resources.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) unsigned int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) * As device is working in continuous mode, handlers may be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) * accessing resources we are currently freeing...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) * Prevent this by disabling interrupt handlers and ensure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) * the device will generate no more interrupts unless explicitly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) * required to, i.e. by restoring back to default one shot mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) disable_irq(priv->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) * Disable continuous sampling mode to restore settings for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) * one shot / direct sampling operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) err = regmap_write(priv->regmap, ZPA2326_CTRL_REG3_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) zpa2326_highest_frequency()->odr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) * Now that device won't generate interrupts on its own,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) * acknowledge any currently active interrupts (may happen on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) * rare occasions while stopping continuous mode).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) err = regmap_read(priv->regmap, ZPA2326_INT_SOURCE_REG, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) * Re-enable interrupts only if we can guarantee the device will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) * generate no more interrupts to prevent handlers from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) * accessing released resources.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) enable_irq(priv->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) zpa2326_dbg(indio_dev, "continuous mode stopped");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) * Switch trigger on : start continuous sampling at required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) * frequency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) if (priv->waken) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) /* Enable interrupt if getting out of reset. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) err = regmap_write(priv->regmap, ZPA2326_CTRL_REG1_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) (u8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) ~ZPA2326_CTRL_REG1_MASK_DATA_READY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) /* Enable continuous sampling at specified frequency. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) err = regmap_write(priv->regmap, ZPA2326_CTRL_REG3_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) ZPA2326_CTRL_REG3_ENABLE_MEAS |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) priv->frequency->odr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) zpa2326_dbg(indio_dev, "continuous mode setup @%dHz",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) priv->frequency->hz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) static const struct iio_trigger_ops zpa2326_trigger_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) .set_trigger_state = zpa2326_set_trigger_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) * zpa2326_init_trigger() - Create an interrupt driven / hardware trigger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) * allowing to notify external devices a new sample is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) * ready.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) * @parent: Hardware sampling device @indio_dev is a child of.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) * @indio_dev: The IIO device associated with the sampling hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) * @private: Internal private state related to @indio_dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) * @irq: Optional interrupt line the hardware uses to notify new data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) * samples are ready. Negative or zero values indicate no interrupts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) * are available, meaning polling is required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) * Only relevant when DT declares a valid interrupt line.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) * Return: Zero when successful, a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) static int zpa2326_init_managed_trigger(struct device *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) struct zpa2326_private *private,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) int irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) struct iio_trigger *trigger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) if (irq <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) trigger = devm_iio_trigger_alloc(parent, "%s-dev%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) indio_dev->name, indio_dev->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) if (!trigger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) /* Basic setup. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) trigger->dev.parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) trigger->ops = &zpa2326_trigger_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) private->trigger = trigger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) /* Register to triggers space. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) ret = devm_iio_trigger_register(parent, trigger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) dev_err(parent, "failed to register hardware trigger (%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) static int zpa2326_get_frequency(const struct iio_dev *indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) return ((struct zpa2326_private *)iio_priv(indio_dev))->frequency->hz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) static int zpa2326_set_frequency(struct iio_dev *indio_dev, int hz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) struct zpa2326_private *priv = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) int freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) /* Check if requested frequency is supported. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) for (freq = 0; freq < ARRAY_SIZE(zpa2326_sampling_frequencies); freq++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) if (zpa2326_sampling_frequencies[freq].hz == hz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) if (freq == ARRAY_SIZE(zpa2326_sampling_frequencies))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) /* Don't allow changing frequency if buffered sampling is ongoing. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) err = iio_device_claim_direct_mode(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) priv->frequency = &zpa2326_sampling_frequencies[freq];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) iio_device_release_direct_mode(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) /* Expose supported hardware sampling frequencies (Hz) through sysfs. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("1 5 11 23");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) static struct attribute *zpa2326_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) &iio_const_attr_sampling_frequency_available.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) static const struct attribute_group zpa2326_attribute_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) .attrs = zpa2326_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) static int zpa2326_read_raw(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) struct iio_chan_spec const *chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) int *val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) int *val2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) switch (mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) case IIO_CHAN_INFO_RAW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) return zpa2326_sample_oneshot(indio_dev, chan->type, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) case IIO_CHAN_INFO_SCALE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) switch (chan->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) case IIO_PRESSURE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) * Pressure resolution is 1/64 Pascal. Scale to kPascal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) * as required by IIO ABI.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) *val = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) *val2 = 64000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) return IIO_VAL_FRACTIONAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) case IIO_TEMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) * Temperature follows the equation:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) * Temp[degC] = Tempcode * 0.00649 - 176.83
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) * where:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) * Tempcode is composed the raw sampled 16 bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) * Hence, to produce a temperature in milli-degrees
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) * Celsius according to IIO ABI, we need to apply the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) * following equation to raw samples:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) * Temp[milli degC] = (Tempcode + Offset) * Scale
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) * where:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) * Offset = -176.83 / 0.00649
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) * Scale = 0.00649 * 1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) *val = 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) *val2 = 490000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) return IIO_VAL_INT_PLUS_MICRO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) case IIO_CHAN_INFO_OFFSET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) switch (chan->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) case IIO_TEMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) *val = -17683000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) *val2 = 649;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) return IIO_VAL_FRACTIONAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) case IIO_CHAN_INFO_SAMP_FREQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) *val = zpa2326_get_frequency(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) return IIO_VAL_INT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) static int zpa2326_write_raw(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) const struct iio_chan_spec *chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) int val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) int val2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) if ((mask != IIO_CHAN_INFO_SAMP_FREQ) || val2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) return zpa2326_set_frequency(indio_dev, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) static const struct iio_chan_spec zpa2326_channels[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) [0] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) .type = IIO_PRESSURE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) .scan_index = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) .scan_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) .sign = 'u',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) .realbits = 24,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) .storagebits = 32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) .endianness = IIO_LE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) BIT(IIO_CHAN_INFO_SCALE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) [1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) .type = IIO_TEMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) .scan_index = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) .scan_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) .sign = 's',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) .realbits = 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) .storagebits = 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) .endianness = IIO_LE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) BIT(IIO_CHAN_INFO_SCALE) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) BIT(IIO_CHAN_INFO_OFFSET),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) [2] = IIO_CHAN_SOFT_TIMESTAMP(2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) static const struct iio_info zpa2326_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) .attrs = &zpa2326_attribute_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) .read_raw = zpa2326_read_raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) .write_raw = zpa2326_write_raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) static struct iio_dev *zpa2326_create_managed_iiodev(struct device *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) struct regmap *regmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) struct iio_dev *indio_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) /* Allocate space to hold IIO device internal state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) indio_dev = devm_iio_device_alloc(device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) sizeof(struct zpa2326_private));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) if (!indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) /* Setup for userspace synchronous on demand sampling. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) indio_dev->modes = INDIO_DIRECT_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) indio_dev->channels = zpa2326_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) indio_dev->num_channels = ARRAY_SIZE(zpa2326_channels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) indio_dev->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) indio_dev->info = &zpa2326_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) return indio_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) int zpa2326_probe(struct device *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) int irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) unsigned int hwid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) struct regmap *regmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) struct iio_dev *indio_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) struct zpa2326_private *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) unsigned int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) indio_dev = zpa2326_create_managed_iiodev(parent, name, regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) if (!indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) priv = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) priv->vref = devm_regulator_get(parent, "vref");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) if (IS_ERR(priv->vref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) return PTR_ERR(priv->vref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) priv->vdd = devm_regulator_get(parent, "vdd");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) if (IS_ERR(priv->vdd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) return PTR_ERR(priv->vdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) /* Set default hardware sampling frequency to highest rate supported. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) priv->frequency = zpa2326_highest_frequency();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) * Plug device's underlying bus abstraction : this MUST be set before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) * registering interrupt handlers since an interrupt might happen if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) * power up sequence is not properly applied.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) priv->regmap = regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) err = devm_iio_triggered_buffer_setup(parent, indio_dev, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) zpa2326_trigger_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) &zpa2326_buffer_setup_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) err = zpa2326_init_managed_trigger(parent, indio_dev, priv, irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) err = zpa2326_init_managed_irq(parent, indio_dev, priv, irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) /* Power up to check device ID and perform initial hardware setup. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) err = zpa2326_power_on(indio_dev, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) /* Read id register to check we are talking to the right slave. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) err = regmap_read(regmap, ZPA2326_DEVICE_ID_REG, &id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) goto sleep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) if (id != hwid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) dev_err(parent, "found device with unexpected id %02x", id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) goto sleep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) err = zpa2326_config_oneshot(indio_dev, irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) goto sleep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) /* Setup done : go sleeping. Device will be awaken upon user request. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) err = zpa2326_sleep(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) goto poweroff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) dev_set_drvdata(parent, indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) zpa2326_init_runtime(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) err = iio_device_register(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) zpa2326_fini_runtime(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) goto poweroff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) sleep:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) /* Put to sleep just in case power regulators are "dummy" ones. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) zpa2326_sleep(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) poweroff:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) zpa2326_power_off(indio_dev, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) EXPORT_SYMBOL_GPL(zpa2326_probe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) void zpa2326_remove(const struct device *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) struct iio_dev *indio_dev = dev_get_drvdata(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) iio_device_unregister(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) zpa2326_fini_runtime(indio_dev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) zpa2326_sleep(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) zpa2326_power_off(indio_dev, iio_priv(indio_dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) EXPORT_SYMBOL_GPL(zpa2326_remove);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) MODULE_AUTHOR("Gregor Boirie <gregor.boirie@parrot.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) MODULE_DESCRIPTION("Core driver for Murata ZPA2326 pressure sensor");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) MODULE_LICENSE("GPL v2");