This source file includes following definitions.
- zpa2326_highest_frequency
- zpa2326_isreg_writeable
- zpa2326_isreg_readable
- zpa2326_isreg_precious
- zpa2326_enable_device
- zpa2326_sleep
- zpa2326_reset_device
- zpa2326_start_oneshot
- zpa2326_power_on
- zpa2326_power_off
- zpa2326_config_oneshot
- zpa2326_clear_fifo
- zpa2326_dequeue_pressure
- zpa2326_fill_sample_buffer
- zpa2326_runtime_suspend
- zpa2326_runtime_resume
- zpa2326_resume
- zpa2326_suspend
- zpa2326_init_runtime
- zpa2326_fini_runtime
- zpa2326_resume
- zpa2326_suspend
- zpa2326_handle_irq
- zpa2326_handle_threaded_irq
- zpa2326_wait_oneshot_completion
- zpa2326_init_managed_irq
- zpa2326_poll_oneshot_completion
- zpa2326_fetch_raw_sample
- zpa2326_sample_oneshot
- zpa2326_trigger_handler
- zpa2326_preenable_buffer
- zpa2326_postenable_buffer
- zpa2326_postdisable_buffer
- zpa2326_set_trigger_state
- zpa2326_init_managed_trigger
- zpa2326_get_frequency
- zpa2326_set_frequency
- zpa2326_read_raw
- zpa2326_write_raw
- zpa2326_create_managed_iiodev
- zpa2326_probe
- zpa2326_remove
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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"
68
69
70 #define ZPA2326_CONVERSION_JIFFIES (HZ / 5)
71
72
73 #define ZPA2326_TPUP_USEC_MIN (1000)
74 #define ZPA2326_TPUP_USEC_MAX (2000)
75
76
77
78
79
80
81 struct zpa2326_frequency {
82 int hz;
83 u16 odr;
84 };
85
86
87
88
89
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 },
95 };
96
97
98 static const struct zpa2326_frequency *zpa2326_highest_frequency(void)
99 {
100 return &zpa2326_sampling_frequencies[
101 ARRAY_SIZE(zpa2326_sampling_frequencies) - 1];
102 }
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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;
134 };
135
136 #define zpa2326_err(idev, fmt, ...) \
137 dev_err(idev->dev.parent, fmt "\n", ##__VA_ARGS__)
138
139 #define zpa2326_warn(idev, fmt, ...) \
140 dev_warn(idev->dev.parent, fmt "\n", ##__VA_ARGS__)
141
142 #define zpa2326_dbg(idev, fmt, ...) \
143 dev_dbg(idev->dev.parent, fmt "\n", ##__VA_ARGS__)
144
145 bool zpa2326_isreg_writeable(struct device *dev, unsigned int reg)
146 {
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;
159
160 default:
161 return false;
162 }
163 }
164 EXPORT_SYMBOL_GPL(zpa2326_isreg_writeable);
165
166 bool zpa2326_isreg_readable(struct device *dev, unsigned int reg)
167 {
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;
188
189 default:
190 return false;
191 }
192 }
193 EXPORT_SYMBOL_GPL(zpa2326_isreg_readable);
194
195 bool zpa2326_isreg_precious(struct device *dev, unsigned int reg)
196 {
197 switch (reg) {
198 case ZPA2326_INT_SOURCE_REG:
199 case ZPA2326_PRESS_OUT_H_REG:
200 return true;
201
202 default:
203 return false;
204 }
205 }
206 EXPORT_SYMBOL_GPL(zpa2326_isreg_precious);
207
208
209
210
211
212
213
214
215
216
217 static int zpa2326_enable_device(const struct iio_dev *indio_dev)
218 {
219 int err;
220
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;
227 }
228
229 zpa2326_dbg(indio_dev, "enabled");
230
231 return 0;
232 }
233
234
235
236
237
238
239
240
241
242
243 static int zpa2326_sleep(const struct iio_dev *indio_dev)
244 {
245 int err;
246
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;
253 }
254
255 zpa2326_dbg(indio_dev, "sleeping");
256
257 return 0;
258 }
259
260
261
262
263
264
265
266
267
268
269 static int zpa2326_reset_device(const struct iio_dev *indio_dev)
270 {
271 int err;
272
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;
279 }
280
281 usleep_range(ZPA2326_TPUP_USEC_MIN, ZPA2326_TPUP_USEC_MAX);
282
283 zpa2326_dbg(indio_dev, "reset");
284
285 return 0;
286 }
287
288
289
290
291
292
293
294
295
296
297
298 static int zpa2326_start_oneshot(const struct iio_dev *indio_dev)
299 {
300 int err;
301
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;
311 }
312
313 zpa2326_dbg(indio_dev, "one shot cycle started");
314
315 return 0;
316 }
317
318
319
320
321
322
323
324
325
326
327
328
329
330 static int zpa2326_power_on(const struct iio_dev *indio_dev,
331 const struct zpa2326_private *private)
332 {
333 int err;
334
335 err = regulator_enable(private->vref);
336 if (err)
337 return err;
338
339 err = regulator_enable(private->vdd);
340 if (err)
341 goto vref;
342
343 zpa2326_dbg(indio_dev, "powered on");
344
345 err = zpa2326_enable_device(indio_dev);
346 if (err)
347 goto vdd;
348
349 err = zpa2326_reset_device(indio_dev);
350 if (err)
351 goto sleep;
352
353 return 0;
354
355 sleep:
356 zpa2326_sleep(indio_dev);
357 vdd:
358 regulator_disable(private->vdd);
359 vref:
360 regulator_disable(private->vref);
361
362 zpa2326_dbg(indio_dev, "powered off");
363
364 return err;
365 }
366
367
368
369
370
371
372
373
374
375 static void zpa2326_power_off(const struct iio_dev *indio_dev,
376 const struct zpa2326_private *private)
377 {
378 regulator_disable(private->vdd);
379 regulator_disable(private->vref);
380
381 zpa2326_dbg(indio_dev, "powered off");
382 }
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401 static int zpa2326_config_oneshot(const struct iio_dev *indio_dev,
402 int irq)
403 {
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;
408
409
410 err = regmap_write(regs, ZPA2326_CTRL_REG3_REG, freq->odr);
411 if (err)
412 return err;
413
414 if (irq > 0) {
415
416 err = regmap_write(regs, ZPA2326_CTRL_REG1_REG,
417 (u8)~ZPA2326_CTRL_REG1_MASK_DATA_READY);
418
419 if (err) {
420 dev_err(indio_dev->dev.parent,
421 "failed to setup one shot mode (%d)", err);
422 return err;
423 }
424 }
425
426 zpa2326_dbg(indio_dev, "one shot mode setup @%dHz", freq->hz);
427
428 return 0;
429 }
430
431
432
433
434
435
436
437
438
439
440
441
442
443 static int zpa2326_clear_fifo(const struct iio_dev *indio_dev,
444 unsigned int min_count)
445 {
446 struct regmap *regs = ((struct zpa2326_private *)
447 iio_priv(indio_dev))->regmap;
448 int err;
449 unsigned int val;
450
451 if (!min_count) {
452
453
454
455
456 err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
457
458 if (err < 0)
459 goto err;
460
461 if (val & ZPA2326_STATUS_FIFO_E)
462
463 return 0;
464 }
465
466
467 do {
468
469
470
471
472 err = regmap_read(regs, ZPA2326_PRESS_OUT_H_REG, &val);
473 if (err < 0)
474 goto err;
475
476 if (min_count) {
477
478
479
480
481 min_count--;
482 continue;
483 }
484
485 err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
486 if (err < 0)
487 goto err;
488
489 } while (!(val & ZPA2326_STATUS_FIFO_E));
490
491 zpa2326_dbg(indio_dev, "FIFO cleared");
492
493 return 0;
494
495 err:
496 zpa2326_err(indio_dev, "failed to clear FIFO (%d)", err);
497
498 return err;
499 }
500
501
502
503
504
505
506
507
508
509
510
511 static int zpa2326_dequeue_pressure(const struct iio_dev *indio_dev,
512 u32 *pressure)
513 {
514 struct regmap *regs = ((struct zpa2326_private *)
515 iio_priv(indio_dev))->regmap;
516 unsigned int val;
517 int err;
518 int cleared = -1;
519
520 err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
521 if (err < 0)
522 return err;
523
524 *pressure = 0;
525
526 if (val & ZPA2326_STATUS_P_OR) {
527
528
529
530
531 zpa2326_warn(indio_dev, "FIFO overflow");
532
533 err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, pressure,
534 3);
535 if (err)
536 return err;
537
538 #define ZPA2326_FIFO_DEPTH (16U)
539
540 return zpa2326_clear_fifo(indio_dev, ZPA2326_FIFO_DEPTH - 1);
541 }
542
543
544
545
546
547
548 do {
549 err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, pressure,
550 3);
551 if (err)
552 return err;
553
554 err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
555 if (err < 0)
556 return err;
557
558 cleared++;
559 } while (!(val & ZPA2326_STATUS_FIFO_E));
560
561 if (cleared)
562
563
564
565
566 zpa2326_dbg(indio_dev, "cleared %d FIFO entries", cleared);
567
568 return 0;
569 }
570
571
572
573
574
575
576
577
578 static int zpa2326_fill_sample_buffer(struct iio_dev *indio_dev,
579 const struct zpa2326_private *private)
580 {
581 struct {
582 u32 pressure;
583 u16 temperature;
584 u64 timestamp;
585 } sample;
586 int err;
587
588 if (test_bit(0, indio_dev->active_scan_mask)) {
589
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;
595 }
596 }
597
598 if (test_bit(1, indio_dev->active_scan_mask)) {
599
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;
606 }
607 }
608
609
610
611
612
613
614
615
616 zpa2326_dbg(indio_dev, "filling raw samples buffer");
617
618 iio_push_to_buffers_with_timestamp(indio_dev, &sample,
619 private->timestamp);
620
621 return 0;
622 }
623
624 #ifdef CONFIG_PM
625 static int zpa2326_runtime_suspend(struct device *parent)
626 {
627 const struct iio_dev *indio_dev = dev_get_drvdata(parent);
628
629 if (pm_runtime_autosuspend_expiration(parent))
630
631 return -EAGAIN;
632
633 zpa2326_power_off(indio_dev, iio_priv(indio_dev));
634
635 return 0;
636 }
637
638 static int zpa2326_runtime_resume(struct device *parent)
639 {
640 const struct iio_dev *indio_dev = dev_get_drvdata(parent);
641
642 return zpa2326_power_on(indio_dev, iio_priv(indio_dev));
643 }
644
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)
650 };
651 EXPORT_SYMBOL_GPL(zpa2326_pm_ops);
652
653
654
655
656
657
658
659
660
661
662 static int zpa2326_resume(const struct iio_dev *indio_dev)
663 {
664 int err;
665
666 err = pm_runtime_get_sync(indio_dev->dev.parent);
667 if (err < 0)
668 return err;
669
670 if (err > 0) {
671
672
673
674
675 zpa2326_enable_device(indio_dev);
676 return 1;
677 }
678
679
680 return 0;
681 }
682
683
684
685
686
687
688
689
690
691 static void zpa2326_suspend(struct iio_dev *indio_dev)
692 {
693 struct device *parent = indio_dev->dev.parent;
694
695 zpa2326_sleep(indio_dev);
696
697 pm_runtime_mark_last_busy(parent);
698 pm_runtime_put_autosuspend(parent);
699 }
700
701 static void zpa2326_init_runtime(struct device *parent)
702 {
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);
710 }
711
712 static void zpa2326_fini_runtime(struct device *parent)
713 {
714 pm_runtime_disable(parent);
715 pm_runtime_set_suspended(parent);
716 }
717 #else
718 static int zpa2326_resume(const struct iio_dev *indio_dev)
719 {
720 zpa2326_enable_device(indio_dev);
721
722 return 0;
723 }
724
725 static void zpa2326_suspend(struct iio_dev *indio_dev)
726 {
727 zpa2326_sleep(indio_dev);
728 }
729
730 #define zpa2326_init_runtime(_parent)
731 #define zpa2326_fini_runtime(_parent)
732 #endif
733
734
735
736
737
738
739
740
741
742
743
744 static irqreturn_t zpa2326_handle_irq(int irq, void *data)
745 {
746 struct iio_dev *indio_dev = data;
747
748 if (iio_buffer_enabled(indio_dev)) {
749
750 ((struct zpa2326_private *)
751 iio_priv(indio_dev))->timestamp = iio_get_time_ns(indio_dev);
752 }
753
754 return IRQ_WAKE_THREAD;
755 }
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783 static irqreturn_t zpa2326_handle_threaded_irq(int irq, void *data)
784 {
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;
790
791
792
793
794
795 cont = (iio_buffer_enabled(indio_dev) &&
796 iio_trigger_using_own(indio_dev));
797
798
799
800
801
802 priv->result = regmap_read(priv->regmap, ZPA2326_INT_SOURCE_REG, &val);
803 if (priv->result < 0) {
804 if (cont)
805 return IRQ_NONE;
806
807 goto complete;
808 }
809
810
811 if (!(val & ZPA2326_INT_SOURCE_DATA_READY)) {
812
813
814
815
816
817
818 zpa2326_warn(indio_dev, "unexpected interrupt status %02x",
819 val);
820
821 if (cont)
822 return IRQ_NONE;
823
824 priv->result = -ENODATA;
825 goto complete;
826 }
827
828
829 iio_trigger_poll_chained(priv->trigger);
830
831 if (cont)
832
833
834
835
836 return IRQ_HANDLED;
837
838 ret = IRQ_HANDLED;
839
840 complete:
841
842
843
844
845 complete(&priv->data_ready);
846
847 return ret;
848 }
849
850
851
852
853
854
855
856
857 static int zpa2326_wait_oneshot_completion(const struct iio_dev *indio_dev,
858 struct zpa2326_private *private)
859 {
860 unsigned int val;
861 long timeout;
862
863 zpa2326_dbg(indio_dev, "waiting for one shot completion interrupt");
864
865 timeout = wait_for_completion_interruptible_timeout(
866 &private->data_ready, ZPA2326_CONVERSION_JIFFIES);
867 if (timeout > 0)
868
869
870
871
872 return private->result;
873
874
875 regmap_read(private->regmap, ZPA2326_INT_SOURCE_REG, &val);
876
877 if (!timeout) {
878
879 zpa2326_warn(indio_dev, "no one shot interrupt occurred (%ld)",
880 timeout);
881 return -ETIME;
882 }
883
884 zpa2326_warn(indio_dev, "wait for one shot interrupt cancelled");
885 return -ERESTARTSYS;
886 }
887
888 static int zpa2326_init_managed_irq(struct device *parent,
889 struct iio_dev *indio_dev,
890 struct zpa2326_private *private,
891 int irq)
892 {
893 int err;
894
895 private->irq = irq;
896
897 if (irq <= 0) {
898
899
900
901
902 dev_info(parent, "no interrupt found, running in polling mode");
903 return 0;
904 }
905
906 init_completion(&private->data_ready);
907
908
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;
917 }
918
919 dev_info(parent, "using interrupt %d", irq);
920
921 return 0;
922 }
923
924
925
926
927
928
929
930
931
932
933 static int zpa2326_poll_oneshot_completion(const struct iio_dev *indio_dev)
934 {
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;
940
941 zpa2326_dbg(indio_dev, "polling for one shot completion");
942
943
944
945
946
947 if (msleep_interruptible(100))
948 return -ERESTARTSYS;
949
950
951 while (true) {
952 err = regmap_read(regs, ZPA2326_CTRL_REG0_REG, &val);
953 if (err < 0)
954 goto err;
955
956 if (!(val & ZPA2326_CTRL_REG0_ONE_SHOT))
957
958 break;
959
960 if (time_after(jiffies, tmout)) {
961
962 err = -ETIME;
963 goto err;
964 }
965
966 usleep_range(10000, 20000);
967 }
968
969
970
971
972
973
974 err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
975 if (err < 0)
976 goto err;
977
978 if (!(val & ZPA2326_STATUS_P_DA)) {
979
980 err = -ENODATA;
981 goto err;
982 }
983
984 return 0;
985
986 err:
987 zpa2326_warn(indio_dev, "failed to poll one shot completion (%d)", err);
988
989 return err;
990 }
991
992
993
994
995
996
997
998
999
1000
1001 static int zpa2326_fetch_raw_sample(const struct iio_dev *indio_dev,
1002 enum iio_chan_type type,
1003 int *value)
1004 {
1005 struct regmap *regs = ((struct zpa2326_private *)
1006 iio_priv(indio_dev))->regmap;
1007 int err;
1008
1009 switch (type) {
1010 case IIO_PRESSURE:
1011 zpa2326_dbg(indio_dev, "fetching raw pressure sample");
1012
1013 err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, value,
1014 3);
1015 if (err) {
1016 zpa2326_warn(indio_dev, "failed to fetch pressure (%d)",
1017 err);
1018 return err;
1019 }
1020
1021
1022 *value = (((u8 *)value)[2] << 16) | (((u8 *)value)[1] << 8) |
1023 ((u8 *)value)[0];
1024
1025 return IIO_VAL_INT;
1026
1027 case IIO_TEMP:
1028 zpa2326_dbg(indio_dev, "fetching raw temperature sample");
1029
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;
1035 }
1036
1037
1038 *value = (int)le16_to_cpup((__le16 *)value);
1039
1040 return IIO_VAL_INT;
1041
1042 default:
1043 return -EINVAL;
1044 }
1045 }
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055 static int zpa2326_sample_oneshot(struct iio_dev *indio_dev,
1056 enum iio_chan_type type,
1057 int *value)
1058 {
1059 int ret;
1060 struct zpa2326_private *priv;
1061
1062 ret = iio_device_claim_direct_mode(indio_dev);
1063 if (ret)
1064 return ret;
1065
1066 ret = zpa2326_resume(indio_dev);
1067 if (ret < 0)
1068 goto release;
1069
1070 priv = iio_priv(indio_dev);
1071
1072 if (ret > 0) {
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084 if (type == IIO_PRESSURE) {
1085 ret = zpa2326_clear_fifo(indio_dev, 0);
1086 if (ret)
1087 goto suspend;
1088 }
1089 } else {
1090
1091
1092
1093
1094
1095 ret = zpa2326_config_oneshot(indio_dev, priv->irq);
1096 if (ret)
1097 goto suspend;
1098 }
1099
1100
1101 ret = zpa2326_start_oneshot(indio_dev);
1102 if (ret)
1103 goto suspend;
1104
1105
1106 if (priv->irq > 0)
1107 ret = zpa2326_wait_oneshot_completion(indio_dev, priv);
1108 else
1109 ret = zpa2326_poll_oneshot_completion(indio_dev);
1110
1111 if (ret)
1112 goto suspend;
1113
1114
1115 ret = zpa2326_fetch_raw_sample(indio_dev, type, value);
1116
1117 suspend:
1118 zpa2326_suspend(indio_dev);
1119 release:
1120 iio_device_release_direct_mode(indio_dev);
1121
1122 return ret;
1123 }
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152 static irqreturn_t zpa2326_trigger_handler(int irq, void *data)
1153 {
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;
1158
1159
1160
1161
1162
1163
1164 cont = iio_trigger_using_own(indio_dev);
1165
1166 if (!cont) {
1167
1168 if (zpa2326_start_oneshot(indio_dev))
1169 goto out;
1170
1171
1172 if (priv->irq <= 0) {
1173
1174 if (zpa2326_poll_oneshot_completion(indio_dev))
1175 goto out;
1176
1177
1178 priv->timestamp = iio_get_time_ns(indio_dev);
1179 } else {
1180
1181 if (zpa2326_wait_oneshot_completion(indio_dev, priv))
1182 goto out;
1183 }
1184 }
1185
1186
1187 zpa2326_fill_sample_buffer(indio_dev, priv);
1188
1189 out:
1190 if (!cont)
1191
1192 zpa2326_sleep(indio_dev);
1193
1194
1195 iio_trigger_notify_done(indio_dev->trig);
1196
1197 return IRQ_HANDLED;
1198 }
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211 static int zpa2326_preenable_buffer(struct iio_dev *indio_dev)
1212 {
1213 int ret = zpa2326_resume(indio_dev);
1214
1215 if (ret < 0)
1216 return ret;
1217
1218
1219 ((struct zpa2326_private *)
1220 iio_priv(indio_dev))->waken = iio_priv(indio_dev);
1221
1222 return 0;
1223 }
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241 static int zpa2326_postenable_buffer(struct iio_dev *indio_dev)
1242 {
1243 const struct zpa2326_private *priv = iio_priv(indio_dev);
1244 int err;
1245
1246 if (!priv->waken) {
1247
1248
1249
1250
1251 err = zpa2326_clear_fifo(indio_dev, 0);
1252 if (err)
1253 goto err;
1254 }
1255
1256 if (!iio_trigger_using_own(indio_dev) && priv->waken) {
1257
1258
1259
1260
1261 err = zpa2326_config_oneshot(indio_dev, priv->irq);
1262 if (err)
1263 goto err;
1264 }
1265
1266
1267 err = iio_triggered_buffer_postenable(indio_dev);
1268 if (err)
1269 goto err;
1270
1271 return 0;
1272
1273 err:
1274 zpa2326_err(indio_dev, "failed to enable buffering (%d)", err);
1275
1276 return err;
1277 }
1278
1279 static int zpa2326_postdisable_buffer(struct iio_dev *indio_dev)
1280 {
1281 zpa2326_suspend(indio_dev);
1282
1283 return 0;
1284 }
1285
1286 static const struct iio_buffer_setup_ops zpa2326_buffer_setup_ops = {
1287 .preenable = zpa2326_preenable_buffer,
1288 .postenable = zpa2326_postenable_buffer,
1289 .predisable = iio_triggered_buffer_predisable,
1290 .postdisable = zpa2326_postdisable_buffer
1291 };
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305 static int zpa2326_set_trigger_state(struct iio_trigger *trig, bool state)
1306 {
1307 const struct iio_dev *indio_dev = dev_get_drvdata(
1308 trig->dev.parent);
1309 const struct zpa2326_private *priv = iio_priv(indio_dev);
1310 int err;
1311
1312 if (!state) {
1313
1314
1315
1316
1317
1318 unsigned int val;
1319
1320
1321
1322
1323
1324
1325
1326
1327 disable_irq(priv->irq);
1328
1329
1330
1331
1332
1333 err = regmap_write(priv->regmap, ZPA2326_CTRL_REG3_REG,
1334 zpa2326_highest_frequency()->odr);
1335 if (err)
1336 return err;
1337
1338
1339
1340
1341
1342
1343 err = regmap_read(priv->regmap, ZPA2326_INT_SOURCE_REG, &val);
1344 if (err < 0)
1345 return err;
1346
1347
1348
1349
1350
1351
1352 enable_irq(priv->irq);
1353
1354 zpa2326_dbg(indio_dev, "continuous mode stopped");
1355 } else {
1356
1357
1358
1359
1360
1361 if (priv->waken) {
1362
1363 err = regmap_write(priv->regmap, ZPA2326_CTRL_REG1_REG,
1364 (u8)
1365 ~ZPA2326_CTRL_REG1_MASK_DATA_READY);
1366 if (err)
1367 return err;
1368 }
1369
1370
1371 err = regmap_write(priv->regmap, ZPA2326_CTRL_REG3_REG,
1372 ZPA2326_CTRL_REG3_ENABLE_MEAS |
1373 priv->frequency->odr);
1374 if (err)
1375 return err;
1376
1377 zpa2326_dbg(indio_dev, "continuous mode setup @%dHz",
1378 priv->frequency->hz);
1379 }
1380
1381 return 0;
1382 }
1383
1384 static const struct iio_trigger_ops zpa2326_trigger_ops = {
1385 .set_trigger_state = zpa2326_set_trigger_state,
1386 };
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403 static int zpa2326_init_managed_trigger(struct device *parent,
1404 struct iio_dev *indio_dev,
1405 struct zpa2326_private *private,
1406 int irq)
1407 {
1408 struct iio_trigger *trigger;
1409 int ret;
1410
1411 if (irq <= 0)
1412 return 0;
1413
1414 trigger = devm_iio_trigger_alloc(parent, "%s-dev%d",
1415 indio_dev->name, indio_dev->id);
1416 if (!trigger)
1417 return -ENOMEM;
1418
1419
1420 trigger->dev.parent = parent;
1421 trigger->ops = &zpa2326_trigger_ops;
1422
1423 private->trigger = trigger;
1424
1425
1426 ret = devm_iio_trigger_register(parent, trigger);
1427 if (ret)
1428 dev_err(parent, "failed to register hardware trigger (%d)",
1429 ret);
1430
1431 return ret;
1432 }
1433
1434 static int zpa2326_get_frequency(const struct iio_dev *indio_dev)
1435 {
1436 return ((struct zpa2326_private *)iio_priv(indio_dev))->frequency->hz;
1437 }
1438
1439 static int zpa2326_set_frequency(struct iio_dev *indio_dev, int hz)
1440 {
1441 struct zpa2326_private *priv = iio_priv(indio_dev);
1442 int freq;
1443 int err;
1444
1445
1446 for (freq = 0; freq < ARRAY_SIZE(zpa2326_sampling_frequencies); freq++)
1447 if (zpa2326_sampling_frequencies[freq].hz == hz)
1448 break;
1449 if (freq == ARRAY_SIZE(zpa2326_sampling_frequencies))
1450 return -EINVAL;
1451
1452
1453 err = iio_device_claim_direct_mode(indio_dev);
1454 if (err)
1455 return err;
1456
1457 priv->frequency = &zpa2326_sampling_frequencies[freq];
1458
1459 iio_device_release_direct_mode(indio_dev);
1460
1461 return 0;
1462 }
1463
1464
1465 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("1 5 11 23");
1466
1467 static struct attribute *zpa2326_attributes[] = {
1468 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
1469 NULL
1470 };
1471
1472 static const struct attribute_group zpa2326_attribute_group = {
1473 .attrs = zpa2326_attributes,
1474 };
1475
1476 static int zpa2326_read_raw(struct iio_dev *indio_dev,
1477 struct iio_chan_spec const *chan,
1478 int *val,
1479 int *val2,
1480 long mask)
1481 {
1482 switch (mask) {
1483 case IIO_CHAN_INFO_RAW:
1484 return zpa2326_sample_oneshot(indio_dev, chan->type, val);
1485
1486 case IIO_CHAN_INFO_SCALE:
1487 switch (chan->type) {
1488 case IIO_PRESSURE:
1489
1490
1491
1492
1493 *val = 1;
1494 *val2 = 64000;
1495 return IIO_VAL_FRACTIONAL;
1496
1497 case IIO_TEMP:
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512 *val = 6;
1513 *val2 = 490000;
1514 return IIO_VAL_INT_PLUS_MICRO;
1515
1516 default:
1517 return -EINVAL;
1518 }
1519
1520 case IIO_CHAN_INFO_OFFSET:
1521 switch (chan->type) {
1522 case IIO_TEMP:
1523 *val = -17683000;
1524 *val2 = 649;
1525 return IIO_VAL_FRACTIONAL;
1526
1527 default:
1528 return -EINVAL;
1529 }
1530
1531 case IIO_CHAN_INFO_SAMP_FREQ:
1532 *val = zpa2326_get_frequency(indio_dev);
1533 return IIO_VAL_INT;
1534
1535 default:
1536 return -EINVAL;
1537 }
1538 }
1539
1540 static int zpa2326_write_raw(struct iio_dev *indio_dev,
1541 const struct iio_chan_spec *chan,
1542 int val,
1543 int val2,
1544 long mask)
1545 {
1546 if ((mask != IIO_CHAN_INFO_SAMP_FREQ) || val2)
1547 return -EINVAL;
1548
1549 return zpa2326_set_frequency(indio_dev, val);
1550 }
1551
1552 static const struct iio_chan_spec zpa2326_channels[] = {
1553 [0] = {
1554 .type = IIO_PRESSURE,
1555 .scan_index = 0,
1556 .scan_type = {
1557 .sign = 'u',
1558 .realbits = 24,
1559 .storagebits = 32,
1560 .endianness = IIO_LE,
1561 },
1562 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1563 BIT(IIO_CHAN_INFO_SCALE),
1564 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
1565 },
1566 [1] = {
1567 .type = IIO_TEMP,
1568 .scan_index = 1,
1569 .scan_type = {
1570 .sign = 's',
1571 .realbits = 16,
1572 .storagebits = 16,
1573 .endianness = IIO_LE,
1574 },
1575 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1576 BIT(IIO_CHAN_INFO_SCALE) |
1577 BIT(IIO_CHAN_INFO_OFFSET),
1578 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
1579 },
1580 [2] = IIO_CHAN_SOFT_TIMESTAMP(2),
1581 };
1582
1583 static const struct iio_info zpa2326_info = {
1584 .attrs = &zpa2326_attribute_group,
1585 .read_raw = zpa2326_read_raw,
1586 .write_raw = zpa2326_write_raw,
1587 };
1588
1589 static struct iio_dev *zpa2326_create_managed_iiodev(struct device *device,
1590 const char *name,
1591 struct regmap *regmap)
1592 {
1593 struct iio_dev *indio_dev;
1594
1595
1596 indio_dev = devm_iio_device_alloc(device,
1597 sizeof(struct zpa2326_private));
1598 if (!indio_dev)
1599 return NULL;
1600
1601
1602 indio_dev->modes = INDIO_DIRECT_MODE;
1603 indio_dev->dev.parent = device;
1604 indio_dev->channels = zpa2326_channels;
1605 indio_dev->num_channels = ARRAY_SIZE(zpa2326_channels);
1606 indio_dev->name = name;
1607 indio_dev->info = &zpa2326_info;
1608
1609 return indio_dev;
1610 }
1611
1612 int zpa2326_probe(struct device *parent,
1613 const char *name,
1614 int irq,
1615 unsigned int hwid,
1616 struct regmap *regmap)
1617 {
1618 struct iio_dev *indio_dev;
1619 struct zpa2326_private *priv;
1620 int err;
1621 unsigned int id;
1622
1623 indio_dev = zpa2326_create_managed_iiodev(parent, name, regmap);
1624 if (!indio_dev)
1625 return -ENOMEM;
1626
1627 priv = iio_priv(indio_dev);
1628
1629 priv->vref = devm_regulator_get(parent, "vref");
1630 if (IS_ERR(priv->vref))
1631 return PTR_ERR(priv->vref);
1632
1633 priv->vdd = devm_regulator_get(parent, "vdd");
1634 if (IS_ERR(priv->vdd))
1635 return PTR_ERR(priv->vdd);
1636
1637
1638 priv->frequency = zpa2326_highest_frequency();
1639
1640
1641
1642
1643
1644
1645 priv->regmap = regmap;
1646
1647 err = devm_iio_triggered_buffer_setup(parent, indio_dev, NULL,
1648 zpa2326_trigger_handler,
1649 &zpa2326_buffer_setup_ops);
1650 if (err)
1651 return err;
1652
1653 err = zpa2326_init_managed_trigger(parent, indio_dev, priv, irq);
1654 if (err)
1655 return err;
1656
1657 err = zpa2326_init_managed_irq(parent, indio_dev, priv, irq);
1658 if (err)
1659 return err;
1660
1661
1662 err = zpa2326_power_on(indio_dev, priv);
1663 if (err)
1664 return err;
1665
1666
1667 err = regmap_read(regmap, ZPA2326_DEVICE_ID_REG, &id);
1668 if (err)
1669 goto sleep;
1670
1671 if (id != hwid) {
1672 dev_err(parent, "found device with unexpected id %02x", id);
1673 err = -ENODEV;
1674 goto sleep;
1675 }
1676
1677 err = zpa2326_config_oneshot(indio_dev, irq);
1678 if (err)
1679 goto sleep;
1680
1681
1682 err = zpa2326_sleep(indio_dev);
1683 if (err)
1684 goto poweroff;
1685
1686 dev_set_drvdata(parent, indio_dev);
1687
1688 zpa2326_init_runtime(parent);
1689
1690 err = iio_device_register(indio_dev);
1691 if (err) {
1692 zpa2326_fini_runtime(parent);
1693 goto poweroff;
1694 }
1695
1696 return 0;
1697
1698 sleep:
1699
1700 zpa2326_sleep(indio_dev);
1701 poweroff:
1702 zpa2326_power_off(indio_dev, priv);
1703
1704 return err;
1705 }
1706 EXPORT_SYMBOL_GPL(zpa2326_probe);
1707
1708 void zpa2326_remove(const struct device *parent)
1709 {
1710 struct iio_dev *indio_dev = dev_get_drvdata(parent);
1711
1712 iio_device_unregister(indio_dev);
1713 zpa2326_fini_runtime(indio_dev->dev.parent);
1714 zpa2326_sleep(indio_dev);
1715 zpa2326_power_off(indio_dev, iio_priv(indio_dev));
1716 }
1717 EXPORT_SYMBOL_GPL(zpa2326_remove);
1718
1719 MODULE_AUTHOR("Gregor Boirie <gregor.boirie@parrot.com>");
1720 MODULE_DESCRIPTION("Core driver for Murata ZPA2326 pressure sensor");
1721 MODULE_LICENSE("GPL v2");