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