treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / iio / pressure / zpa2326.c
blob99dfe33ee402fce63e908fa84602a5dae4716d25
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Murata ZPA2326 pressure and temperature sensor IIO driver
5 * Copyright (c) 2016 Parrot S.A.
7 * Author: Gregor Boirie <gregor.boirie@parrot.com>
8 */
10 /**
11 * DOC: ZPA2326 theory of operations
13 * This driver supports %INDIO_DIRECT_MODE and %INDIO_BUFFER_TRIGGERED IIO
14 * modes.
15 * A internal hardware trigger is also implemented to dispatch registered IIO
16 * trigger consumers upon "sample ready" interrupts.
18 * ZPA2326 hardware supports 2 sampling mode: one shot and continuous.
20 * A complete one shot sampling cycle gets device out of low power mode,
21 * performs pressure and temperature measurements, then automatically switches
22 * back to low power mode. It is meant for on demand sampling with optimal power
23 * saving at the cost of lower sampling rate and higher software overhead.
24 * This is a natural candidate for IIO read_raw hook implementation
25 * (%INDIO_DIRECT_MODE). It is also used for triggered buffering support to
26 * ensure explicit synchronization with external trigger events
27 * (%INDIO_BUFFER_TRIGGERED).
29 * The continuous mode works according to a periodic hardware measurement
30 * process continuously pushing samples into an internal hardware FIFO (for
31 * pressure samples only). Measurement cycle completion may be signaled by a
32 * "sample ready" interrupt.
33 * Typical software sequence of operations :
34 * - get device out of low power mode,
35 * - setup hardware sampling period,
36 * - at end of period, upon data ready interrupt: pop pressure samples out of
37 * hardware FIFO and fetch temperature sample
38 * - when no longer needed, stop sampling process by putting device into
39 * low power mode.
40 * This mode is used to implement %INDIO_BUFFER_TRIGGERED mode if device tree
41 * declares a valid interrupt line. In this case, the internal hardware trigger
42 * drives acquisition.
44 * Note that hardware sampling frequency is taken into account only when
45 * internal hardware trigger is attached as the highest sampling rate seems to
46 * be the most energy efficient.
48 * TODO:
49 * preset pressure threshold crossing / IIO events ;
50 * differential pressure sampling ;
51 * hardware samples averaging.
54 #include <linux/module.h>
55 #include <linux/kernel.h>
56 #include <linux/delay.h>
57 #include <linux/interrupt.h>
58 #include <linux/regulator/consumer.h>
59 #include <linux/pm_runtime.h>
60 #include <linux/regmap.h>
61 #include <linux/iio/iio.h>
62 #include <linux/iio/sysfs.h>
63 #include <linux/iio/buffer.h>
64 #include <linux/iio/trigger.h>
65 #include <linux/iio/trigger_consumer.h>
66 #include <linux/iio/triggered_buffer.h>
67 #include "zpa2326.h"
69 /* 200 ms should be enough for the longest conversion time in one-shot mode. */
70 #define ZPA2326_CONVERSION_JIFFIES (HZ / 5)
72 /* There should be a 1 ms delay (Tpup) after getting out of reset. */
73 #define ZPA2326_TPUP_USEC_MIN (1000)
74 #define ZPA2326_TPUP_USEC_MAX (2000)
76 /**
77 * struct zpa2326_frequency - Hardware sampling frequency descriptor
78 * @hz : Frequency in Hertz.
79 * @odr: Output Data Rate word as expected by %ZPA2326_CTRL_REG3_REG.
81 struct zpa2326_frequency {
82 int hz;
83 u16 odr;
87 * Keep these in strict ascending order: last array entry is expected to
88 * correspond to the highest sampling frequency.
90 static const struct zpa2326_frequency zpa2326_sampling_frequencies[] = {
91 { .hz = 1, .odr = 1 << ZPA2326_CTRL_REG3_ODR_SHIFT },
92 { .hz = 5, .odr = 5 << ZPA2326_CTRL_REG3_ODR_SHIFT },
93 { .hz = 11, .odr = 6 << ZPA2326_CTRL_REG3_ODR_SHIFT },
94 { .hz = 23, .odr = 7 << ZPA2326_CTRL_REG3_ODR_SHIFT },
97 /* Return the highest hardware sampling frequency available. */
98 static const struct zpa2326_frequency *zpa2326_highest_frequency(void)
100 return &zpa2326_sampling_frequencies[
101 ARRAY_SIZE(zpa2326_sampling_frequencies) - 1];
105 * struct zpa_private - Per-device internal private state
106 * @timestamp: Buffered samples ready datum.
107 * @regmap: Underlying I2C / SPI bus adapter used to abstract slave register
108 * accesses.
109 * @result: Allows sampling logic to get completion status of operations
110 * that interrupt handlers perform asynchronously.
111 * @data_ready: Interrupt handler uses this to wake user context up at sampling
112 * operation completion.
113 * @trigger: Optional hardware / interrupt driven trigger used to notify
114 * external devices a new sample is ready.
115 * @waken: Flag indicating whether or not device has just been powered on.
116 * @irq: Optional interrupt line: negative or zero if not declared into
117 * DT, in which case sampling logic keeps polling status register
118 * to detect completion.
119 * @frequency: Current hardware sampling frequency.
120 * @vref: Power / voltage reference.
121 * @vdd: Power supply.
123 struct zpa2326_private {
124 s64 timestamp;
125 struct regmap *regmap;
126 int result;
127 struct completion data_ready;
128 struct iio_trigger *trigger;
129 bool waken;
130 int irq;
131 const struct zpa2326_frequency *frequency;
132 struct regulator *vref;
133 struct regulator *vdd;
136 #define zpa2326_err(idev, fmt, ...) \
137 dev_err(idev->dev.parent, fmt "\n", ##__VA_ARGS__)
139 #define zpa2326_warn(idev, fmt, ...) \
140 dev_warn(idev->dev.parent, fmt "\n", ##__VA_ARGS__)
142 #define zpa2326_dbg(idev, fmt, ...) \
143 dev_dbg(idev->dev.parent, fmt "\n", ##__VA_ARGS__)
145 bool zpa2326_isreg_writeable(struct device *dev, unsigned int reg)
147 switch (reg) {
148 case ZPA2326_REF_P_XL_REG:
149 case ZPA2326_REF_P_L_REG:
150 case ZPA2326_REF_P_H_REG:
151 case ZPA2326_RES_CONF_REG:
152 case ZPA2326_CTRL_REG0_REG:
153 case ZPA2326_CTRL_REG1_REG:
154 case ZPA2326_CTRL_REG2_REG:
155 case ZPA2326_CTRL_REG3_REG:
156 case ZPA2326_THS_P_LOW_REG:
157 case ZPA2326_THS_P_HIGH_REG:
158 return true;
160 default:
161 return false;
164 EXPORT_SYMBOL_GPL(zpa2326_isreg_writeable);
166 bool zpa2326_isreg_readable(struct device *dev, unsigned int reg)
168 switch (reg) {
169 case ZPA2326_REF_P_XL_REG:
170 case ZPA2326_REF_P_L_REG:
171 case ZPA2326_REF_P_H_REG:
172 case ZPA2326_DEVICE_ID_REG:
173 case ZPA2326_RES_CONF_REG:
174 case ZPA2326_CTRL_REG0_REG:
175 case ZPA2326_CTRL_REG1_REG:
176 case ZPA2326_CTRL_REG2_REG:
177 case ZPA2326_CTRL_REG3_REG:
178 case ZPA2326_INT_SOURCE_REG:
179 case ZPA2326_THS_P_LOW_REG:
180 case ZPA2326_THS_P_HIGH_REG:
181 case ZPA2326_STATUS_REG:
182 case ZPA2326_PRESS_OUT_XL_REG:
183 case ZPA2326_PRESS_OUT_L_REG:
184 case ZPA2326_PRESS_OUT_H_REG:
185 case ZPA2326_TEMP_OUT_L_REG:
186 case ZPA2326_TEMP_OUT_H_REG:
187 return true;
189 default:
190 return false;
193 EXPORT_SYMBOL_GPL(zpa2326_isreg_readable);
195 bool zpa2326_isreg_precious(struct device *dev, unsigned int reg)
197 switch (reg) {
198 case ZPA2326_INT_SOURCE_REG:
199 case ZPA2326_PRESS_OUT_H_REG:
200 return true;
202 default:
203 return false;
206 EXPORT_SYMBOL_GPL(zpa2326_isreg_precious);
209 * zpa2326_enable_device() - Enable device, i.e. get out of low power mode.
210 * @indio_dev: The IIO device associated with the hardware to enable.
212 * Required to access complete register space and to perform any sampling
213 * or control operations.
215 * Return: Zero when successful, a negative error code otherwise.
217 static int zpa2326_enable_device(const struct iio_dev *indio_dev)
219 int err;
221 err = regmap_write(((struct zpa2326_private *)
222 iio_priv(indio_dev))->regmap,
223 ZPA2326_CTRL_REG0_REG, ZPA2326_CTRL_REG0_ENABLE);
224 if (err) {
225 zpa2326_err(indio_dev, "failed to enable device (%d)", err);
226 return err;
229 zpa2326_dbg(indio_dev, "enabled");
231 return 0;
235 * zpa2326_sleep() - Disable device, i.e. switch to low power mode.
236 * @indio_dev: The IIO device associated with the hardware to disable.
238 * Only %ZPA2326_DEVICE_ID_REG and %ZPA2326_CTRL_REG0_REG registers may be
239 * accessed once device is in the disabled state.
241 * Return: Zero when successful, a negative error code otherwise.
243 static int zpa2326_sleep(const struct iio_dev *indio_dev)
245 int err;
247 err = regmap_write(((struct zpa2326_private *)
248 iio_priv(indio_dev))->regmap,
249 ZPA2326_CTRL_REG0_REG, 0);
250 if (err) {
251 zpa2326_err(indio_dev, "failed to sleep (%d)", err);
252 return err;
255 zpa2326_dbg(indio_dev, "sleeping");
257 return 0;
261 * zpa2326_reset_device() - Reset device to default hardware state.
262 * @indio_dev: The IIO device associated with the hardware to reset.
264 * Disable sampling and empty hardware FIFO.
265 * Device must be enabled before reset, i.e. not in low power mode.
267 * Return: Zero when successful, a negative error code otherwise.
269 static int zpa2326_reset_device(const struct iio_dev *indio_dev)
271 int err;
273 err = regmap_write(((struct zpa2326_private *)
274 iio_priv(indio_dev))->regmap,
275 ZPA2326_CTRL_REG2_REG, ZPA2326_CTRL_REG2_SWRESET);
276 if (err) {
277 zpa2326_err(indio_dev, "failed to reset device (%d)", err);
278 return err;
281 usleep_range(ZPA2326_TPUP_USEC_MIN, ZPA2326_TPUP_USEC_MAX);
283 zpa2326_dbg(indio_dev, "reset");
285 return 0;
289 * zpa2326_start_oneshot() - Start a single sampling cycle, i.e. in one shot
290 * mode.
291 * @indio_dev: The IIO device associated with the sampling hardware.
293 * Device must have been previously enabled and configured for one shot mode.
294 * Device will be switched back to low power mode at end of cycle.
296 * Return: Zero when successful, a negative error code otherwise.
298 static int zpa2326_start_oneshot(const struct iio_dev *indio_dev)
300 int err;
302 err = regmap_write(((struct zpa2326_private *)
303 iio_priv(indio_dev))->regmap,
304 ZPA2326_CTRL_REG0_REG,
305 ZPA2326_CTRL_REG0_ENABLE |
306 ZPA2326_CTRL_REG0_ONE_SHOT);
307 if (err) {
308 zpa2326_err(indio_dev, "failed to start one shot cycle (%d)",
309 err);
310 return err;
313 zpa2326_dbg(indio_dev, "one shot cycle started");
315 return 0;
319 * zpa2326_power_on() - Power on device to allow subsequent configuration.
320 * @indio_dev: The IIO device associated with the sampling hardware.
321 * @private: Internal private state related to @indio_dev.
323 * Sampling will be disabled, preventing strange things from happening in our
324 * back. Hardware FIFO content will be cleared.
325 * When successful, device will be left in the enabled state to allow further
326 * configuration.
328 * Return: Zero when successful, a negative error code otherwise.
330 static int zpa2326_power_on(const struct iio_dev *indio_dev,
331 const struct zpa2326_private *private)
333 int err;
335 err = regulator_enable(private->vref);
336 if (err)
337 return err;
339 err = regulator_enable(private->vdd);
340 if (err)
341 goto vref;
343 zpa2326_dbg(indio_dev, "powered on");
345 err = zpa2326_enable_device(indio_dev);
346 if (err)
347 goto vdd;
349 err = zpa2326_reset_device(indio_dev);
350 if (err)
351 goto sleep;
353 return 0;
355 sleep:
356 zpa2326_sleep(indio_dev);
357 vdd:
358 regulator_disable(private->vdd);
359 vref:
360 regulator_disable(private->vref);
362 zpa2326_dbg(indio_dev, "powered off");
364 return err;
368 * zpa2326_power_off() - Power off device, i.e. disable attached power
369 * regulators.
370 * @indio_dev: The IIO device associated with the sampling hardware.
371 * @private: Internal private state related to @indio_dev.
373 * Return: Zero when successful, a negative error code otherwise.
375 static void zpa2326_power_off(const struct iio_dev *indio_dev,
376 const struct zpa2326_private *private)
378 regulator_disable(private->vdd);
379 regulator_disable(private->vref);
381 zpa2326_dbg(indio_dev, "powered off");
385 * zpa2326_config_oneshot() - Setup device for one shot / on demand mode.
386 * @indio_dev: The IIO device associated with the sampling hardware.
387 * @irq: Optional interrupt line the hardware uses to notify new data
388 * samples are ready. Negative or zero values indicate no interrupts
389 * are available, meaning polling is required.
391 * Output Data Rate is configured for the highest possible rate so that
392 * conversion time and power consumption are reduced to a minimum.
393 * Note that hardware internal averaging machinery (not implemented in this
394 * driver) is not applicable in this mode.
396 * Device must have been previously enabled before calling
397 * zpa2326_config_oneshot().
399 * Return: Zero when successful, a negative error code otherwise.
401 static int zpa2326_config_oneshot(const struct iio_dev *indio_dev,
402 int irq)
404 struct regmap *regs = ((struct zpa2326_private *)
405 iio_priv(indio_dev))->regmap;
406 const struct zpa2326_frequency *freq = zpa2326_highest_frequency();
407 int err;
409 /* Setup highest available Output Data Rate for one shot mode. */
410 err = regmap_write(regs, ZPA2326_CTRL_REG3_REG, freq->odr);
411 if (err)
412 return err;
414 if (irq > 0) {
415 /* Request interrupt when new sample is available. */
416 err = regmap_write(regs, ZPA2326_CTRL_REG1_REG,
417 (u8)~ZPA2326_CTRL_REG1_MASK_DATA_READY);
419 if (err) {
420 dev_err(indio_dev->dev.parent,
421 "failed to setup one shot mode (%d)", err);
422 return err;
426 zpa2326_dbg(indio_dev, "one shot mode setup @%dHz", freq->hz);
428 return 0;
432 * zpa2326_clear_fifo() - Clear remaining entries in hardware FIFO.
433 * @indio_dev: The IIO device associated with the sampling hardware.
434 * @min_count: Number of samples present within hardware FIFO.
436 * @min_count argument is a hint corresponding to the known minimum number of
437 * samples currently living in the FIFO. This allows to reduce the number of bus
438 * accesses by skipping status register read operation as long as we know for
439 * sure there are still entries left.
441 * Return: Zero when successful, a negative error code otherwise.
443 static int zpa2326_clear_fifo(const struct iio_dev *indio_dev,
444 unsigned int min_count)
446 struct regmap *regs = ((struct zpa2326_private *)
447 iio_priv(indio_dev))->regmap;
448 int err;
449 unsigned int val;
451 if (!min_count) {
453 * No hint: read status register to determine whether FIFO is
454 * empty or not.
456 err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
458 if (err < 0)
459 goto err;
461 if (val & ZPA2326_STATUS_FIFO_E)
462 /* Fifo is empty: nothing to trash. */
463 return 0;
466 /* Clear FIFO. */
467 do {
469 * A single fetch from pressure MSB register is enough to pop
470 * values out of FIFO.
472 err = regmap_read(regs, ZPA2326_PRESS_OUT_H_REG, &val);
473 if (err < 0)
474 goto err;
476 if (min_count) {
478 * We know for sure there are at least min_count entries
479 * left in FIFO. Skip status register read.
481 min_count--;
482 continue;
485 err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
486 if (err < 0)
487 goto err;
489 } while (!(val & ZPA2326_STATUS_FIFO_E));
491 zpa2326_dbg(indio_dev, "FIFO cleared");
493 return 0;
495 err:
496 zpa2326_err(indio_dev, "failed to clear FIFO (%d)", err);
498 return err;
502 * zpa2326_dequeue_pressure() - Retrieve the most recent pressure sample from
503 * hardware FIFO.
504 * @indio_dev: The IIO device associated with the sampling hardware.
505 * @pressure: Sampled pressure output.
507 * Note that ZPA2326 hardware FIFO stores pressure samples only.
509 * Return: Zero when successful, a negative error code otherwise.
511 static int zpa2326_dequeue_pressure(const struct iio_dev *indio_dev,
512 u32 *pressure)
514 struct regmap *regs = ((struct zpa2326_private *)
515 iio_priv(indio_dev))->regmap;
516 unsigned int val;
517 int err;
518 int cleared = -1;
520 err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
521 if (err < 0)
522 return err;
524 *pressure = 0;
526 if (val & ZPA2326_STATUS_P_OR) {
528 * Fifo overrun : first sample dequeued from FIFO is the
529 * newest.
531 zpa2326_warn(indio_dev, "FIFO overflow");
533 err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, pressure,
535 if (err)
536 return err;
538 #define ZPA2326_FIFO_DEPTH (16U)
539 /* Hardware FIFO may hold no more than 16 pressure samples. */
540 return zpa2326_clear_fifo(indio_dev, ZPA2326_FIFO_DEPTH - 1);
544 * Fifo has not overflown : retrieve newest sample. We need to pop
545 * values out until FIFO is empty : last fetched pressure is the newest.
546 * In nominal cases, we should find a single queued sample only.
548 do {
549 err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, pressure,
551 if (err)
552 return err;
554 err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
555 if (err < 0)
556 return err;
558 cleared++;
559 } while (!(val & ZPA2326_STATUS_FIFO_E));
561 if (cleared)
563 * Samples were pushed by hardware during previous rounds but we
564 * didn't consume them fast enough: inform user.
566 zpa2326_dbg(indio_dev, "cleared %d FIFO entries", cleared);
568 return 0;
572 * zpa2326_fill_sample_buffer() - Enqueue new channel samples to IIO buffer.
573 * @indio_dev: The IIO device associated with the sampling hardware.
574 * @private: Internal private state related to @indio_dev.
576 * Return: Zero when successful, a negative error code otherwise.
578 static int zpa2326_fill_sample_buffer(struct iio_dev *indio_dev,
579 const struct zpa2326_private *private)
581 struct {
582 u32 pressure;
583 u16 temperature;
584 u64 timestamp;
585 } sample;
586 int err;
588 if (test_bit(0, indio_dev->active_scan_mask)) {
589 /* Get current pressure from hardware FIFO. */
590 err = zpa2326_dequeue_pressure(indio_dev, &sample.pressure);
591 if (err) {
592 zpa2326_warn(indio_dev, "failed to fetch pressure (%d)",
593 err);
594 return err;
598 if (test_bit(1, indio_dev->active_scan_mask)) {
599 /* Get current temperature. */
600 err = regmap_bulk_read(private->regmap, ZPA2326_TEMP_OUT_L_REG,
601 &sample.temperature, 2);
602 if (err) {
603 zpa2326_warn(indio_dev,
604 "failed to fetch temperature (%d)", err);
605 return err;
610 * Now push samples using timestamp stored either :
611 * - by hardware interrupt handler if interrupt is available: see
612 * zpa2326_handle_irq(),
613 * - or oneshot completion polling machinery : see
614 * zpa2326_trigger_handler().
616 zpa2326_dbg(indio_dev, "filling raw samples buffer");
618 iio_push_to_buffers_with_timestamp(indio_dev, &sample,
619 private->timestamp);
621 return 0;
624 #ifdef CONFIG_PM
625 static int zpa2326_runtime_suspend(struct device *parent)
627 const struct iio_dev *indio_dev = dev_get_drvdata(parent);
629 if (pm_runtime_autosuspend_expiration(parent))
630 /* Userspace changed autosuspend delay. */
631 return -EAGAIN;
633 zpa2326_power_off(indio_dev, iio_priv(indio_dev));
635 return 0;
638 static int zpa2326_runtime_resume(struct device *parent)
640 const struct iio_dev *indio_dev = dev_get_drvdata(parent);
642 return zpa2326_power_on(indio_dev, iio_priv(indio_dev));
645 const struct dev_pm_ops zpa2326_pm_ops = {
646 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
647 pm_runtime_force_resume)
648 SET_RUNTIME_PM_OPS(zpa2326_runtime_suspend, zpa2326_runtime_resume,
649 NULL)
651 EXPORT_SYMBOL_GPL(zpa2326_pm_ops);
654 * zpa2326_resume() - Request the PM layer to power supply the device.
655 * @indio_dev: The IIO device associated with the sampling hardware.
657 * Return:
658 * < 0 - a negative error code meaning failure ;
659 * 0 - success, device has just been powered up ;
660 * 1 - success, device was already powered.
662 static int zpa2326_resume(const struct iio_dev *indio_dev)
664 int err;
666 err = pm_runtime_get_sync(indio_dev->dev.parent);
667 if (err < 0)
668 return err;
670 if (err > 0) {
672 * Device was already power supplied: get it out of low power
673 * mode and inform caller.
675 zpa2326_enable_device(indio_dev);
676 return 1;
679 /* Inform caller device has just been brought back to life. */
680 return 0;
684 * zpa2326_suspend() - Schedule a power down using autosuspend feature of PM
685 * layer.
686 * @indio_dev: The IIO device associated with the sampling hardware.
688 * Device is switched to low power mode at first to save power even when
689 * attached regulator is a "dummy" one.
691 static void zpa2326_suspend(struct iio_dev *indio_dev)
693 struct device *parent = indio_dev->dev.parent;
695 zpa2326_sleep(indio_dev);
697 pm_runtime_mark_last_busy(parent);
698 pm_runtime_put_autosuspend(parent);
701 static void zpa2326_init_runtime(struct device *parent)
703 pm_runtime_get_noresume(parent);
704 pm_runtime_set_active(parent);
705 pm_runtime_enable(parent);
706 pm_runtime_set_autosuspend_delay(parent, 1000);
707 pm_runtime_use_autosuspend(parent);
708 pm_runtime_mark_last_busy(parent);
709 pm_runtime_put_autosuspend(parent);
712 static void zpa2326_fini_runtime(struct device *parent)
714 pm_runtime_disable(parent);
715 pm_runtime_set_suspended(parent);
717 #else /* !CONFIG_PM */
718 static int zpa2326_resume(const struct iio_dev *indio_dev)
720 zpa2326_enable_device(indio_dev);
722 return 0;
725 static void zpa2326_suspend(struct iio_dev *indio_dev)
727 zpa2326_sleep(indio_dev);
730 #define zpa2326_init_runtime(_parent)
731 #define zpa2326_fini_runtime(_parent)
732 #endif /* !CONFIG_PM */
735 * zpa2326_handle_irq() - Process hardware interrupts.
736 * @irq: Interrupt line the hardware uses to notify new data has arrived.
737 * @data: The IIO device associated with the sampling hardware.
739 * Timestamp buffered samples as soon as possible then schedule threaded bottom
740 * half.
742 * Return: Always successful.
744 static irqreturn_t zpa2326_handle_irq(int irq, void *data)
746 struct iio_dev *indio_dev = data;
748 if (iio_buffer_enabled(indio_dev)) {
749 /* Timestamping needed for buffered sampling only. */
750 ((struct zpa2326_private *)
751 iio_priv(indio_dev))->timestamp = iio_get_time_ns(indio_dev);
754 return IRQ_WAKE_THREAD;
758 * zpa2326_handle_threaded_irq() - Interrupt bottom-half handler.
759 * @irq: Interrupt line the hardware uses to notify new data has arrived.
760 * @data: The IIO device associated with the sampling hardware.
762 * Mainly ensures interrupt is caused by a real "new sample available"
763 * condition. This relies upon the ability to perform blocking / sleeping bus
764 * accesses to slave's registers. This is why zpa2326_handle_threaded_irq() is
765 * called from within a thread, i.e. not called from hard interrupt context.
767 * When device is using its own internal hardware trigger in continuous sampling
768 * mode, data are available into hardware FIFO once interrupt has occurred. All
769 * we have to do is to dispatch the trigger, which in turn will fetch data and
770 * fill IIO buffer.
772 * When not using its own internal hardware trigger, the device has been
773 * configured in one-shot mode either by an external trigger or the IIO read_raw
774 * hook. This means one of the latter is currently waiting for sampling
775 * completion, in which case we must simply wake it up.
777 * See zpa2326_trigger_handler().
779 * Return:
780 * %IRQ_NONE - no consistent interrupt happened ;
781 * %IRQ_HANDLED - there was new samples available.
783 static irqreturn_t zpa2326_handle_threaded_irq(int irq, void *data)
785 struct iio_dev *indio_dev = data;
786 struct zpa2326_private *priv = iio_priv(indio_dev);
787 unsigned int val;
788 bool cont;
789 irqreturn_t ret = IRQ_NONE;
792 * Are we using our own internal trigger in triggered buffer mode, i.e.,
793 * currently working in continuous sampling mode ?
795 cont = (iio_buffer_enabled(indio_dev) &&
796 iio_trigger_using_own(indio_dev));
799 * Device works according to a level interrupt scheme: reading interrupt
800 * status de-asserts interrupt line.
802 priv->result = regmap_read(priv->regmap, ZPA2326_INT_SOURCE_REG, &val);
803 if (priv->result < 0) {
804 if (cont)
805 return IRQ_NONE;
807 goto complete;
810 /* Data ready is the only interrupt source we requested. */
811 if (!(val & ZPA2326_INT_SOURCE_DATA_READY)) {
813 * Interrupt happened but no new sample available: likely caused
814 * by spurious interrupts, in which case, returning IRQ_NONE
815 * allows to benefit from the generic spurious interrupts
816 * handling.
818 zpa2326_warn(indio_dev, "unexpected interrupt status %02x",
819 val);
821 if (cont)
822 return IRQ_NONE;
824 priv->result = -ENODATA;
825 goto complete;
828 /* New sample available: dispatch internal trigger consumers. */
829 iio_trigger_poll_chained(priv->trigger);
831 if (cont)
833 * Internal hardware trigger has been scheduled above : it will
834 * fetch data on its own.
836 return IRQ_HANDLED;
838 ret = IRQ_HANDLED;
840 complete:
842 * Wake up direct or externaly triggered buffer mode waiters: see
843 * zpa2326_sample_oneshot() and zpa2326_trigger_handler().
845 complete(&priv->data_ready);
847 return ret;
851 * zpa2326_wait_oneshot_completion() - Wait for oneshot data ready interrupt.
852 * @indio_dev: The IIO device associated with the sampling hardware.
853 * @private: Internal private state related to @indio_dev.
855 * Return: Zero when successful, a negative error code otherwise.
857 static int zpa2326_wait_oneshot_completion(const struct iio_dev *indio_dev,
858 struct zpa2326_private *private)
860 unsigned int val;
861 long timeout;
863 zpa2326_dbg(indio_dev, "waiting for one shot completion interrupt");
865 timeout = wait_for_completion_interruptible_timeout(
866 &private->data_ready, ZPA2326_CONVERSION_JIFFIES);
867 if (timeout > 0)
869 * Interrupt handler completed before timeout: return operation
870 * status.
872 return private->result;
874 /* Clear all interrupts just to be sure. */
875 regmap_read(private->regmap, ZPA2326_INT_SOURCE_REG, &val);
877 if (!timeout) {
878 /* Timed out. */
879 zpa2326_warn(indio_dev, "no one shot interrupt occurred (%ld)",
880 timeout);
881 return -ETIME;
884 zpa2326_warn(indio_dev, "wait for one shot interrupt cancelled");
885 return -ERESTARTSYS;
888 static int zpa2326_init_managed_irq(struct device *parent,
889 struct iio_dev *indio_dev,
890 struct zpa2326_private *private,
891 int irq)
893 int err;
895 private->irq = irq;
897 if (irq <= 0) {
899 * Platform declared no interrupt line: device will be polled
900 * for data availability.
902 dev_info(parent, "no interrupt found, running in polling mode");
903 return 0;
906 init_completion(&private->data_ready);
908 /* Request handler to be scheduled into threaded interrupt context. */
909 err = devm_request_threaded_irq(parent, irq, zpa2326_handle_irq,
910 zpa2326_handle_threaded_irq,
911 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
912 dev_name(parent), indio_dev);
913 if (err) {
914 dev_err(parent, "failed to request interrupt %d (%d)", irq,
915 err);
916 return err;
919 dev_info(parent, "using interrupt %d", irq);
921 return 0;
925 * zpa2326_poll_oneshot_completion() - Actively poll for one shot data ready.
926 * @indio_dev: The IIO device associated with the sampling hardware.
928 * Loop over registers content to detect end of sampling cycle. Used when DT
929 * declared no valid interrupt lines.
931 * Return: Zero when successful, a negative error code otherwise.
933 static int zpa2326_poll_oneshot_completion(const struct iio_dev *indio_dev)
935 unsigned long tmout = jiffies + ZPA2326_CONVERSION_JIFFIES;
936 struct regmap *regs = ((struct zpa2326_private *)
937 iio_priv(indio_dev))->regmap;
938 unsigned int val;
939 int err;
941 zpa2326_dbg(indio_dev, "polling for one shot completion");
944 * At least, 100 ms is needed for the device to complete its one-shot
945 * cycle.
947 if (msleep_interruptible(100))
948 return -ERESTARTSYS;
950 /* Poll for conversion completion in hardware. */
951 while (true) {
952 err = regmap_read(regs, ZPA2326_CTRL_REG0_REG, &val);
953 if (err < 0)
954 goto err;
956 if (!(val & ZPA2326_CTRL_REG0_ONE_SHOT))
957 /* One-shot bit self clears at conversion end. */
958 break;
960 if (time_after(jiffies, tmout)) {
961 /* Prevent from waiting forever : let's time out. */
962 err = -ETIME;
963 goto err;
966 usleep_range(10000, 20000);
970 * In oneshot mode, pressure sample availability guarantees that
971 * temperature conversion has also completed : just check pressure
972 * status bit to keep things simple.
974 err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
975 if (err < 0)
976 goto err;
978 if (!(val & ZPA2326_STATUS_P_DA)) {
979 /* No sample available. */
980 err = -ENODATA;
981 goto err;
984 return 0;
986 err:
987 zpa2326_warn(indio_dev, "failed to poll one shot completion (%d)", err);
989 return err;
993 * zpa2326_fetch_raw_sample() - Retrieve a raw sample and convert it to CPU
994 * endianness.
995 * @indio_dev: The IIO device associated with the sampling hardware.
996 * @type: Type of measurement / channel to fetch from.
997 * @value: Sample output.
999 * Return: Zero when successful, a negative error code otherwise.
1001 static int zpa2326_fetch_raw_sample(const struct iio_dev *indio_dev,
1002 enum iio_chan_type type,
1003 int *value)
1005 struct regmap *regs = ((struct zpa2326_private *)
1006 iio_priv(indio_dev))->regmap;
1007 int err;
1009 switch (type) {
1010 case IIO_PRESSURE:
1011 zpa2326_dbg(indio_dev, "fetching raw pressure sample");
1013 err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, value,
1015 if (err) {
1016 zpa2326_warn(indio_dev, "failed to fetch pressure (%d)",
1017 err);
1018 return err;
1021 /* Pressure is a 24 bits wide little-endian unsigned int. */
1022 *value = (((u8 *)value)[2] << 16) | (((u8 *)value)[1] << 8) |
1023 ((u8 *)value)[0];
1025 return IIO_VAL_INT;
1027 case IIO_TEMP:
1028 zpa2326_dbg(indio_dev, "fetching raw temperature sample");
1030 err = regmap_bulk_read(regs, ZPA2326_TEMP_OUT_L_REG, value, 2);
1031 if (err) {
1032 zpa2326_warn(indio_dev,
1033 "failed to fetch temperature (%d)", err);
1034 return err;
1037 /* Temperature is a 16 bits wide little-endian signed int. */
1038 *value = (int)le16_to_cpup((__le16 *)value);
1040 return IIO_VAL_INT;
1042 default:
1043 return -EINVAL;
1048 * zpa2326_sample_oneshot() - Perform a complete one shot sampling cycle.
1049 * @indio_dev: The IIO device associated with the sampling hardware.
1050 * @type: Type of measurement / channel to fetch from.
1051 * @value: Sample output.
1053 * Return: Zero when successful, a negative error code otherwise.
1055 static int zpa2326_sample_oneshot(struct iio_dev *indio_dev,
1056 enum iio_chan_type type,
1057 int *value)
1059 int ret;
1060 struct zpa2326_private *priv;
1062 ret = iio_device_claim_direct_mode(indio_dev);
1063 if (ret)
1064 return ret;
1066 ret = zpa2326_resume(indio_dev);
1067 if (ret < 0)
1068 goto release;
1070 priv = iio_priv(indio_dev);
1072 if (ret > 0) {
1074 * We were already power supplied. Just clear hardware FIFO to
1075 * get rid of samples acquired during previous rounds (if any).
1076 * Sampling operation always generates both temperature and
1077 * pressure samples. The latter are always enqueued into
1078 * hardware FIFO. This may lead to situations were pressure
1079 * samples still sit into FIFO when previous cycle(s) fetched
1080 * temperature data only.
1081 * Hence, we need to clear hardware FIFO content to prevent from
1082 * getting outdated values at the end of current cycle.
1084 if (type == IIO_PRESSURE) {
1085 ret = zpa2326_clear_fifo(indio_dev, 0);
1086 if (ret)
1087 goto suspend;
1089 } else {
1091 * We have just been power supplied, i.e. device is in default
1092 * "out of reset" state, meaning we need to reconfigure it
1093 * entirely.
1095 ret = zpa2326_config_oneshot(indio_dev, priv->irq);
1096 if (ret)
1097 goto suspend;
1100 /* Start a sampling cycle in oneshot mode. */
1101 ret = zpa2326_start_oneshot(indio_dev);
1102 if (ret)
1103 goto suspend;
1105 /* Wait for sampling cycle to complete. */
1106 if (priv->irq > 0)
1107 ret = zpa2326_wait_oneshot_completion(indio_dev, priv);
1108 else
1109 ret = zpa2326_poll_oneshot_completion(indio_dev);
1111 if (ret)
1112 goto suspend;
1114 /* Retrieve raw sample value and convert it to CPU endianness. */
1115 ret = zpa2326_fetch_raw_sample(indio_dev, type, value);
1117 suspend:
1118 zpa2326_suspend(indio_dev);
1119 release:
1120 iio_device_release_direct_mode(indio_dev);
1122 return ret;
1126 * zpa2326_trigger_handler() - Perform an IIO buffered sampling round in one
1127 * shot mode.
1128 * @irq: The software interrupt assigned to @data
1129 * @data: The IIO poll function dispatched by external trigger our device is
1130 * attached to.
1132 * Bottom-half handler called by the IIO trigger to which our device is
1133 * currently attached. Allows us to synchronize this device buffered sampling
1134 * either with external events (such as timer expiration, external device sample
1135 * ready, etc...) or with its own interrupt (internal hardware trigger).
1137 * When using an external trigger, basically run the same sequence of operations
1138 * as for zpa2326_sample_oneshot() with the following hereafter. Hardware FIFO
1139 * is not cleared since already done at buffering enable time and samples
1140 * dequeueing always retrieves the most recent value.
1142 * Otherwise, when internal hardware trigger has dispatched us, just fetch data
1143 * from hardware FIFO.
1145 * Fetched data will pushed unprocessed to IIO buffer since samples conversion
1146 * is delegated to userspace in buffered mode (endianness, etc...).
1148 * Return:
1149 * %IRQ_NONE - no consistent interrupt happened ;
1150 * %IRQ_HANDLED - there was new samples available.
1152 static irqreturn_t zpa2326_trigger_handler(int irq, void *data)
1154 struct iio_dev *indio_dev = ((struct iio_poll_func *)
1155 data)->indio_dev;
1156 struct zpa2326_private *priv = iio_priv(indio_dev);
1157 bool cont;
1160 * We have been dispatched, meaning we are in triggered buffer mode.
1161 * Using our own internal trigger implies we are currently in continuous
1162 * hardware sampling mode.
1164 cont = iio_trigger_using_own(indio_dev);
1166 if (!cont) {
1167 /* On demand sampling : start a one shot cycle. */
1168 if (zpa2326_start_oneshot(indio_dev))
1169 goto out;
1171 /* Wait for sampling cycle to complete. */
1172 if (priv->irq <= 0) {
1173 /* No interrupt available: poll for completion. */
1174 if (zpa2326_poll_oneshot_completion(indio_dev))
1175 goto out;
1177 /* Only timestamp sample once it is ready. */
1178 priv->timestamp = iio_get_time_ns(indio_dev);
1179 } else {
1180 /* Interrupt handlers will timestamp for us. */
1181 if (zpa2326_wait_oneshot_completion(indio_dev, priv))
1182 goto out;
1186 /* Enqueue to IIO buffer / userspace. */
1187 zpa2326_fill_sample_buffer(indio_dev, priv);
1189 out:
1190 if (!cont)
1191 /* Don't switch to low power if sampling continuously. */
1192 zpa2326_sleep(indio_dev);
1194 /* Inform attached trigger we are done. */
1195 iio_trigger_notify_done(indio_dev->trig);
1197 return IRQ_HANDLED;
1201 * zpa2326_preenable_buffer() - Prepare device for configuring triggered
1202 * sampling
1203 * modes.
1204 * @indio_dev: The IIO device associated with the sampling hardware.
1206 * Basically power up device.
1207 * Called with IIO device's lock held.
1209 * Return: Zero when successful, a negative error code otherwise.
1211 static int zpa2326_preenable_buffer(struct iio_dev *indio_dev)
1213 int ret = zpa2326_resume(indio_dev);
1215 if (ret < 0)
1216 return ret;
1218 /* Tell zpa2326_postenable_buffer() if we have just been powered on. */
1219 ((struct zpa2326_private *)
1220 iio_priv(indio_dev))->waken = iio_priv(indio_dev);
1222 return 0;
1226 * zpa2326_postenable_buffer() - Configure device for triggered sampling.
1227 * @indio_dev: The IIO device associated with the sampling hardware.
1229 * Basically setup one-shot mode if plugging external trigger.
1230 * Otherwise, let internal trigger configure continuous sampling :
1231 * see zpa2326_set_trigger_state().
1233 * If an error is returned, IIO layer will call our postdisable hook for us,
1234 * i.e. no need to explicitly power device off here.
1235 * Called with IIO device's lock held.
1237 * Called with IIO device's lock held.
1239 * Return: Zero when successful, a negative error code otherwise.
1241 static int zpa2326_postenable_buffer(struct iio_dev *indio_dev)
1243 const struct zpa2326_private *priv = iio_priv(indio_dev);
1244 int err;
1246 /* Plug our own trigger event handler. */
1247 err = iio_triggered_buffer_postenable(indio_dev);
1248 if (err)
1249 goto err;
1251 if (!priv->waken) {
1253 * We were already power supplied. Just clear hardware FIFO to
1254 * get rid of samples acquired during previous rounds (if any).
1256 err = zpa2326_clear_fifo(indio_dev, 0);
1257 if (err)
1258 goto err_buffer_predisable;
1261 if (!iio_trigger_using_own(indio_dev) && priv->waken) {
1263 * We are using an external trigger and we have just been
1264 * powered up: reconfigure one-shot mode.
1266 err = zpa2326_config_oneshot(indio_dev, priv->irq);
1267 if (err)
1268 goto err_buffer_predisable;
1271 return 0;
1273 err_buffer_predisable:
1274 iio_triggered_buffer_predisable(indio_dev);
1275 err:
1276 zpa2326_err(indio_dev, "failed to enable buffering (%d)", err);
1278 return err;
1281 static int zpa2326_postdisable_buffer(struct iio_dev *indio_dev)
1283 zpa2326_suspend(indio_dev);
1285 return 0;
1288 static const struct iio_buffer_setup_ops zpa2326_buffer_setup_ops = {
1289 .preenable = zpa2326_preenable_buffer,
1290 .postenable = zpa2326_postenable_buffer,
1291 .predisable = iio_triggered_buffer_predisable,
1292 .postdisable = zpa2326_postdisable_buffer
1296 * zpa2326_set_trigger_state() - Start / stop continuous sampling.
1297 * @trig: The trigger being attached to IIO device associated with the sampling
1298 * hardware.
1299 * @state: Tell whether to start (true) or stop (false)
1301 * Basically enable / disable hardware continuous sampling mode.
1303 * Called with IIO device's lock held at postenable() or predisable() time.
1305 * Return: Zero when successful, a negative error code otherwise.
1307 static int zpa2326_set_trigger_state(struct iio_trigger *trig, bool state)
1309 const struct iio_dev *indio_dev = dev_get_drvdata(
1310 trig->dev.parent);
1311 const struct zpa2326_private *priv = iio_priv(indio_dev);
1312 int err;
1314 if (!state) {
1316 * Switch trigger off : in case of failure, interrupt is left
1317 * disabled in order to prevent handler from accessing released
1318 * resources.
1320 unsigned int val;
1323 * As device is working in continuous mode, handlers may be
1324 * accessing resources we are currently freeing...
1325 * Prevent this by disabling interrupt handlers and ensure
1326 * the device will generate no more interrupts unless explicitly
1327 * required to, i.e. by restoring back to default one shot mode.
1329 disable_irq(priv->irq);
1332 * Disable continuous sampling mode to restore settings for
1333 * one shot / direct sampling operations.
1335 err = regmap_write(priv->regmap, ZPA2326_CTRL_REG3_REG,
1336 zpa2326_highest_frequency()->odr);
1337 if (err)
1338 return err;
1341 * Now that device won't generate interrupts on its own,
1342 * acknowledge any currently active interrupts (may happen on
1343 * rare occasions while stopping continuous mode).
1345 err = regmap_read(priv->regmap, ZPA2326_INT_SOURCE_REG, &val);
1346 if (err < 0)
1347 return err;
1350 * Re-enable interrupts only if we can guarantee the device will
1351 * generate no more interrupts to prevent handlers from
1352 * accessing released resources.
1354 enable_irq(priv->irq);
1356 zpa2326_dbg(indio_dev, "continuous mode stopped");
1357 } else {
1359 * Switch trigger on : start continuous sampling at required
1360 * frequency.
1363 if (priv->waken) {
1364 /* Enable interrupt if getting out of reset. */
1365 err = regmap_write(priv->regmap, ZPA2326_CTRL_REG1_REG,
1366 (u8)
1367 ~ZPA2326_CTRL_REG1_MASK_DATA_READY);
1368 if (err)
1369 return err;
1372 /* Enable continuous sampling at specified frequency. */
1373 err = regmap_write(priv->regmap, ZPA2326_CTRL_REG3_REG,
1374 ZPA2326_CTRL_REG3_ENABLE_MEAS |
1375 priv->frequency->odr);
1376 if (err)
1377 return err;
1379 zpa2326_dbg(indio_dev, "continuous mode setup @%dHz",
1380 priv->frequency->hz);
1383 return 0;
1386 static const struct iio_trigger_ops zpa2326_trigger_ops = {
1387 .set_trigger_state = zpa2326_set_trigger_state,
1391 * zpa2326_init_trigger() - Create an interrupt driven / hardware trigger
1392 * allowing to notify external devices a new sample is
1393 * ready.
1394 * @parent: Hardware sampling device @indio_dev is a child of.
1395 * @indio_dev: The IIO device associated with the sampling hardware.
1396 * @private: Internal private state related to @indio_dev.
1397 * @irq: Optional interrupt line the hardware uses to notify new data
1398 * samples are ready. Negative or zero values indicate no interrupts
1399 * are available, meaning polling is required.
1401 * Only relevant when DT declares a valid interrupt line.
1403 * Return: Zero when successful, a negative error code otherwise.
1405 static int zpa2326_init_managed_trigger(struct device *parent,
1406 struct iio_dev *indio_dev,
1407 struct zpa2326_private *private,
1408 int irq)
1410 struct iio_trigger *trigger;
1411 int ret;
1413 if (irq <= 0)
1414 return 0;
1416 trigger = devm_iio_trigger_alloc(parent, "%s-dev%d",
1417 indio_dev->name, indio_dev->id);
1418 if (!trigger)
1419 return -ENOMEM;
1421 /* Basic setup. */
1422 trigger->dev.parent = parent;
1423 trigger->ops = &zpa2326_trigger_ops;
1425 private->trigger = trigger;
1427 /* Register to triggers space. */
1428 ret = devm_iio_trigger_register(parent, trigger);
1429 if (ret)
1430 dev_err(parent, "failed to register hardware trigger (%d)",
1431 ret);
1433 return ret;
1436 static int zpa2326_get_frequency(const struct iio_dev *indio_dev)
1438 return ((struct zpa2326_private *)iio_priv(indio_dev))->frequency->hz;
1441 static int zpa2326_set_frequency(struct iio_dev *indio_dev, int hz)
1443 struct zpa2326_private *priv = iio_priv(indio_dev);
1444 int freq;
1445 int err;
1447 /* Check if requested frequency is supported. */
1448 for (freq = 0; freq < ARRAY_SIZE(zpa2326_sampling_frequencies); freq++)
1449 if (zpa2326_sampling_frequencies[freq].hz == hz)
1450 break;
1451 if (freq == ARRAY_SIZE(zpa2326_sampling_frequencies))
1452 return -EINVAL;
1454 /* Don't allow changing frequency if buffered sampling is ongoing. */
1455 err = iio_device_claim_direct_mode(indio_dev);
1456 if (err)
1457 return err;
1459 priv->frequency = &zpa2326_sampling_frequencies[freq];
1461 iio_device_release_direct_mode(indio_dev);
1463 return 0;
1466 /* Expose supported hardware sampling frequencies (Hz) through sysfs. */
1467 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("1 5 11 23");
1469 static struct attribute *zpa2326_attributes[] = {
1470 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
1471 NULL
1474 static const struct attribute_group zpa2326_attribute_group = {
1475 .attrs = zpa2326_attributes,
1478 static int zpa2326_read_raw(struct iio_dev *indio_dev,
1479 struct iio_chan_spec const *chan,
1480 int *val,
1481 int *val2,
1482 long mask)
1484 switch (mask) {
1485 case IIO_CHAN_INFO_RAW:
1486 return zpa2326_sample_oneshot(indio_dev, chan->type, val);
1488 case IIO_CHAN_INFO_SCALE:
1489 switch (chan->type) {
1490 case IIO_PRESSURE:
1492 * Pressure resolution is 1/64 Pascal. Scale to kPascal
1493 * as required by IIO ABI.
1495 *val = 1;
1496 *val2 = 64000;
1497 return IIO_VAL_FRACTIONAL;
1499 case IIO_TEMP:
1501 * Temperature follows the equation:
1502 * Temp[degC] = Tempcode * 0.00649 - 176.83
1503 * where:
1504 * Tempcode is composed the raw sampled 16 bits.
1506 * Hence, to produce a temperature in milli-degrees
1507 * Celsius according to IIO ABI, we need to apply the
1508 * following equation to raw samples:
1509 * Temp[milli degC] = (Tempcode + Offset) * Scale
1510 * where:
1511 * Offset = -176.83 / 0.00649
1512 * Scale = 0.00649 * 1000
1514 *val = 6;
1515 *val2 = 490000;
1516 return IIO_VAL_INT_PLUS_MICRO;
1518 default:
1519 return -EINVAL;
1522 case IIO_CHAN_INFO_OFFSET:
1523 switch (chan->type) {
1524 case IIO_TEMP:
1525 *val = -17683000;
1526 *val2 = 649;
1527 return IIO_VAL_FRACTIONAL;
1529 default:
1530 return -EINVAL;
1533 case IIO_CHAN_INFO_SAMP_FREQ:
1534 *val = zpa2326_get_frequency(indio_dev);
1535 return IIO_VAL_INT;
1537 default:
1538 return -EINVAL;
1542 static int zpa2326_write_raw(struct iio_dev *indio_dev,
1543 const struct iio_chan_spec *chan,
1544 int val,
1545 int val2,
1546 long mask)
1548 if ((mask != IIO_CHAN_INFO_SAMP_FREQ) || val2)
1549 return -EINVAL;
1551 return zpa2326_set_frequency(indio_dev, val);
1554 static const struct iio_chan_spec zpa2326_channels[] = {
1555 [0] = {
1556 .type = IIO_PRESSURE,
1557 .scan_index = 0,
1558 .scan_type = {
1559 .sign = 'u',
1560 .realbits = 24,
1561 .storagebits = 32,
1562 .endianness = IIO_LE,
1564 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1565 BIT(IIO_CHAN_INFO_SCALE),
1566 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
1568 [1] = {
1569 .type = IIO_TEMP,
1570 .scan_index = 1,
1571 .scan_type = {
1572 .sign = 's',
1573 .realbits = 16,
1574 .storagebits = 16,
1575 .endianness = IIO_LE,
1577 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1578 BIT(IIO_CHAN_INFO_SCALE) |
1579 BIT(IIO_CHAN_INFO_OFFSET),
1580 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
1582 [2] = IIO_CHAN_SOFT_TIMESTAMP(2),
1585 static const struct iio_info zpa2326_info = {
1586 .attrs = &zpa2326_attribute_group,
1587 .read_raw = zpa2326_read_raw,
1588 .write_raw = zpa2326_write_raw,
1591 static struct iio_dev *zpa2326_create_managed_iiodev(struct device *device,
1592 const char *name,
1593 struct regmap *regmap)
1595 struct iio_dev *indio_dev;
1597 /* Allocate space to hold IIO device internal state. */
1598 indio_dev = devm_iio_device_alloc(device,
1599 sizeof(struct zpa2326_private));
1600 if (!indio_dev)
1601 return NULL;
1603 /* Setup for userspace synchronous on demand sampling. */
1604 indio_dev->modes = INDIO_DIRECT_MODE;
1605 indio_dev->dev.parent = device;
1606 indio_dev->channels = zpa2326_channels;
1607 indio_dev->num_channels = ARRAY_SIZE(zpa2326_channels);
1608 indio_dev->name = name;
1609 indio_dev->info = &zpa2326_info;
1611 return indio_dev;
1614 int zpa2326_probe(struct device *parent,
1615 const char *name,
1616 int irq,
1617 unsigned int hwid,
1618 struct regmap *regmap)
1620 struct iio_dev *indio_dev;
1621 struct zpa2326_private *priv;
1622 int err;
1623 unsigned int id;
1625 indio_dev = zpa2326_create_managed_iiodev(parent, name, regmap);
1626 if (!indio_dev)
1627 return -ENOMEM;
1629 priv = iio_priv(indio_dev);
1631 priv->vref = devm_regulator_get(parent, "vref");
1632 if (IS_ERR(priv->vref))
1633 return PTR_ERR(priv->vref);
1635 priv->vdd = devm_regulator_get(parent, "vdd");
1636 if (IS_ERR(priv->vdd))
1637 return PTR_ERR(priv->vdd);
1639 /* Set default hardware sampling frequency to highest rate supported. */
1640 priv->frequency = zpa2326_highest_frequency();
1643 * Plug device's underlying bus abstraction : this MUST be set before
1644 * registering interrupt handlers since an interrupt might happen if
1645 * power up sequence is not properly applied.
1647 priv->regmap = regmap;
1649 err = devm_iio_triggered_buffer_setup(parent, indio_dev, NULL,
1650 zpa2326_trigger_handler,
1651 &zpa2326_buffer_setup_ops);
1652 if (err)
1653 return err;
1655 err = zpa2326_init_managed_trigger(parent, indio_dev, priv, irq);
1656 if (err)
1657 return err;
1659 err = zpa2326_init_managed_irq(parent, indio_dev, priv, irq);
1660 if (err)
1661 return err;
1663 /* Power up to check device ID and perform initial hardware setup. */
1664 err = zpa2326_power_on(indio_dev, priv);
1665 if (err)
1666 return err;
1668 /* Read id register to check we are talking to the right slave. */
1669 err = regmap_read(regmap, ZPA2326_DEVICE_ID_REG, &id);
1670 if (err)
1671 goto sleep;
1673 if (id != hwid) {
1674 dev_err(parent, "found device with unexpected id %02x", id);
1675 err = -ENODEV;
1676 goto sleep;
1679 err = zpa2326_config_oneshot(indio_dev, irq);
1680 if (err)
1681 goto sleep;
1683 /* Setup done : go sleeping. Device will be awaken upon user request. */
1684 err = zpa2326_sleep(indio_dev);
1685 if (err)
1686 goto poweroff;
1688 dev_set_drvdata(parent, indio_dev);
1690 zpa2326_init_runtime(parent);
1692 err = iio_device_register(indio_dev);
1693 if (err) {
1694 zpa2326_fini_runtime(parent);
1695 goto poweroff;
1698 return 0;
1700 sleep:
1701 /* Put to sleep just in case power regulators are "dummy" ones. */
1702 zpa2326_sleep(indio_dev);
1703 poweroff:
1704 zpa2326_power_off(indio_dev, priv);
1706 return err;
1708 EXPORT_SYMBOL_GPL(zpa2326_probe);
1710 void zpa2326_remove(const struct device *parent)
1712 struct iio_dev *indio_dev = dev_get_drvdata(parent);
1714 iio_device_unregister(indio_dev);
1715 zpa2326_fini_runtime(indio_dev->dev.parent);
1716 zpa2326_sleep(indio_dev);
1717 zpa2326_power_off(indio_dev, iio_priv(indio_dev));
1719 EXPORT_SYMBOL_GPL(zpa2326_remove);
1721 MODULE_AUTHOR("Gregor Boirie <gregor.boirie@parrot.com>");
1722 MODULE_DESCRIPTION("Core driver for Murata ZPA2326 pressure sensor");
1723 MODULE_LICENSE("GPL v2");