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