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