-
Notifications
You must be signed in to change notification settings - Fork 845
/
ad_pulsar.c
951 lines (806 loc) · 24.3 KB
/
ad_pulsar.c
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
// SPDX-License-Identifier: GPL-2.0+
/*
* Analog Devices PulSAR ADC family driver
*
* Copyright 2022 Analog Devices Inc.
*/
#include <asm/unaligned.h>
#include <linux/bitfield.h>
#include <linux/bitops.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/dmaengine.h>
#include <linux/err.h>
#include <linux/jiffies.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/property.h>
#include <linux/pwm.h>
#include <linux/spi/spi.h>
#include <linux/spi/spi-engine.h>
#include <linux/regulator/consumer.h>
#include <linux/iio/buffer.h>
#include <linux/iio/buffer-dma.h>
#include <linux/iio/buffer-dmaengine.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#define AD_PULSAR_REG_CONFIG 0x00
#define AD7682_NUM_TEMP_CHANNELS 1
#define AD7682_REF_INT_2_5V 0x0
#define AD7682_REF_INT_4V 0x1
#define AD7682_REF_EXT_1 0x2
#define AD7682_REF_EXT_2 0x3
#define AD7682_REF_EXT_3 0x6
#define AD7682_REF_EXT_4 0x7
#define AD7682_REG_CONFIG AD_PULSAR_REG_CONFIG
#define AD7682_CONFIG_RESET GENMASK(15, 0)
#define AD7682_CFG_MSK BIT(13)
#define AD7682_POLARITY_MSK BIT(12)
#define AD7682_PAIR_MSK BIT(11)
#define AD7682_REF_MSK BIT(10)
#define AD7682_SEL_MSK GENMASK(9, 7)
#define AD7682_BW_MSK BIT(6)
#define AD7682_REFBUF_MSK GENMASK(5, 3)
#define AD7682_SEQ_MSK GENMASK(2, 1)
#define AD7682_READBACK_MSK BIT(0)
#define AD7682_UPDATE_CFG AD7682_CFG_MSK
#define AD7682_NO_READBACK AD7682_READBACK_MSK
#define AD7682_CH_POLARITY(x) FIELD_PREP(AD7682_POLARITY_MSK, x)
#define AD7682_CH_TYPE(x) FIELD_PREP(AD7682_PAIR_MSK, x)
#define AD7682_CH_REF(x) FIELD_PREP(AD7682_REF_MSK, x)
#define AD7682_SEQ_SCAN(x) FIELD_PREP(AD7682_SEQ_MSK, x)
#define AD7682_REFBUF_SEL(x) FIELD_PREP(AD7682_REFBUF_MSK, x)
#define AD7682_BW_SEL(x) FIELD_PREP(AD7682_BW_MSK, x)
#define AD7682_SEL_CH(x) FIELD_PREP(AD7682_SEL_MSK, x)
#define AD7682_GET_BW(x) FIELD_GET(AD7682_BW_MSK, x)
#define AD7682_CH_TEMP_SENSOR (AD7682_REFBUF_SEL(AD7682_REF_INT_4V) |\
AD7682_UPDATE_CFG | AD7682_CH_TYPE(SINGLE_ENDED) | AD7682_CH_REF(GND) |\
AD7682_CH_POLARITY(BIPOLAR))
#define AD7682_SEQ_EN_CHANNEL(i) (AD7682_UPDATE_CFG | \
AD7682_CH_POLARITY(UNIPOLAR) | AD7682_CH_TYPE(SINGLE_ENDED) | \
AD7682_CH_REF(COM) | AD7682_SEL_CH(i) | AD7682_BW_SEL(FULL_BW) | \
AD7682_REFBUF_SEL(AD7682_REF_INT_4V) | AD7682_SEQ_SCAN(DISABLED) | \
AD7682_NO_READBACK)
#define AD7682_DISABLE_SEQ (AD7682_UPDATE_CFG | \
AD7682_CH_POLARITY(UNIPOLAR) | AD7682_CH_TYPE(SINGLE_ENDED) | \
AD7682_CH_REF(GND) | AD7682_SEL_CH(7) | AD7682_BW_SEL(FULL_BW) | \
AD7682_REFBUF_SEL(AD7682_REF_EXT_1) | AD7682_SEQ_SCAN(DISABLED) | \
AD7682_NO_READBACK)
enum ad_pulsar_input_type {
DIFFERENTIAL,
SINGLE_ENDED,
};
enum ad_pulsar_input_polarity {
BIPOLAR,
UNIPOLAR
};
enum ad_pulsar_ch_ref {
COM,
GND
};
enum ad_pulsar_sequencer_scan {
DISABLED,
};
enum ad_pulsar_filter_bw {
QUARTER_BW,
FULL_BW
};
static unsigned int ad_pulsar_filter_freq[] = {
[QUARTER_BW] = 425000,
[FULL_BW] = 1700000
};
struct ad_pulsar_chip_info {
enum ad_pulsar_input_type input_type;
const char *name;
int num_channels;
int resolution;
int sclk_rate;
int max_rate;
bool has_filter:1;
bool cfg_register:1;
};
static const struct ad_pulsar_chip_info ad7988_5_chip_info = {
.name = "ad7988-5",
.input_type = SINGLE_ENDED,
.max_rate = 500000,
.resolution = 16,
.num_channels = 1,
.sclk_rate = 80000000
};
static const struct ad_pulsar_chip_info ad7988_1_chip_info = {
.name = "ad7988-1",
.input_type = SINGLE_ENDED,
.max_rate = 100000,
.resolution = 16,
.num_channels = 1,
.sclk_rate = 80000000
};
static const struct ad_pulsar_chip_info ad7984_chip_info = {
.name = "ad7984",
.input_type = DIFFERENTIAL,
.max_rate = 1333333,
.resolution = 18,
.num_channels = 1,
.sclk_rate = 80000000
};
static const struct ad_pulsar_chip_info ad7983_chip_info = {
.name = "ad7983",
.input_type = SINGLE_ENDED,
.max_rate = 1333333,
.resolution = 16,
.num_channels = 1,
.sclk_rate = 80000000
};
static const struct ad_pulsar_chip_info ad7982_chip_info = {
.name = "ad7982",
.input_type = DIFFERENTIAL,
.max_rate = 1000000,
.resolution = 18,
.num_channels = 1,
.sclk_rate = 80000000
};
static const struct ad_pulsar_chip_info ad7980_chip_info = {
.name = "ad7980",
.input_type = SINGLE_ENDED,
.max_rate = 1000000,
.resolution = 16,
.num_channels = 1,
.sclk_rate = 80000000
};
static const struct ad_pulsar_chip_info ad7949_chip_info = {
.name = "ad7949",
.input_type = SINGLE_ENDED,
.max_rate = 250000,
.resolution = 14,
.num_channels = 8 + AD7682_NUM_TEMP_CHANNELS,
.sclk_rate = 40000000,
.has_filter = true,
.cfg_register = true
};
static const struct ad_pulsar_chip_info ad7946_chip_info = {
.name = "ad7946",
.input_type = SINGLE_ENDED,
.max_rate = 500000,
.resolution = 14,
.num_channels = 1,
.sclk_rate = 40000000
};
static const struct ad_pulsar_chip_info ad7942_chip_info = {
.name = "ad7942",
.input_type = SINGLE_ENDED,
.max_rate = 250000,
.resolution = 14,
.num_channels = 1,
.sclk_rate = 40000000
};
static const struct ad_pulsar_chip_info ad7699_chip_info = {
.name = "ad7699",
.input_type = SINGLE_ENDED,
.max_rate = 500000,
.resolution = 16,
.num_channels = 8 + AD7682_NUM_TEMP_CHANNELS,
.sclk_rate = 40000000,
.has_filter = true,
.cfg_register = true
};
static const struct ad_pulsar_chip_info ad7693_chip_info = {
.name = "ad7693",
.input_type = DIFFERENTIAL,
.max_rate = 500000,
.resolution = 16,
.num_channels = 1,
.sclk_rate = 40000000
};
static const struct ad_pulsar_chip_info ad7691_chip_info = {
.name = "ad7691",
.input_type = DIFFERENTIAL,
.max_rate = 250000,
.resolution = 18,
.num_channels = 1,
.sclk_rate = 40000000
};
static const struct ad_pulsar_chip_info ad7690_chip_info = {
.name = "ad7690",
.input_type = DIFFERENTIAL,
.max_rate = 400000,
.resolution = 18,
.num_channels = 1,
.sclk_rate = 40000000
};
static const struct ad_pulsar_chip_info ad7689_chip_info = {
.name = "ad7689",
.input_type = SINGLE_ENDED,
.max_rate = 250000,
.resolution = 16,
.num_channels = 8 + AD7682_NUM_TEMP_CHANNELS,
.sclk_rate = 40000000,
.has_filter = true,
.cfg_register = true
};
static const struct ad_pulsar_chip_info ad7688_chip_info = {
.name = "ad7688",
.input_type = DIFFERENTIAL,
.max_rate = 500000,
.resolution = 16,
.num_channels = 1,
.sclk_rate = 40000000
};
static const struct ad_pulsar_chip_info ad7687_chip_info = {
.name = "ad7687",
.input_type = DIFFERENTIAL,
.max_rate = 250000,
.resolution = 16,
.num_channels = 1,
.sclk_rate = 40000000
};
static const struct ad_pulsar_chip_info ad7686_chip_info = {
.name = "ad7686",
.input_type = SINGLE_ENDED,
.max_rate = 500000,
.resolution = 16,
.num_channels = 1,
.sclk_rate = 40000000
};
static const struct ad_pulsar_chip_info ad7685_chip_info = {
.name = "ad7685",
.input_type = SINGLE_ENDED,
.max_rate = 250000,
.resolution = 16,
.num_channels = 1,
.sclk_rate = 40000000
};
static const struct ad_pulsar_chip_info ad7682_chip_info = {
.name = "ad7682",
.input_type = SINGLE_ENDED,
.max_rate = 250000,
.resolution = 16,
.num_channels = 4 + AD7682_NUM_TEMP_CHANNELS,
.sclk_rate = 40000000,
.has_filter = true,
.cfg_register = true
};
struct ad_pulsar_adc {
const struct ad_pulsar_chip_info *info;
struct iio_chan_spec *channels;
struct spi_transfer *seq_xfer;
unsigned int cfg;
unsigned long ref_clk_rate;
struct pwm_device *cnv;
struct spi_device *spi;
struct regulator *vref;
unsigned int *cfg_reg;
int spi_speed_hz;
int samp_freq;
int device_id;
/*
* DMA (thus cache coherency maintenance) requires the
* transfer buffers to live in their own cache lines.
*/
u8 spi_rx_data[4] __aligned(IIO_DMA_MINALIGN);
u8 spi_tx_data[4];
};
static const struct iio_chan_spec ad_pulsar_chan_template = {
.type = IIO_VOLTAGE,
.indexed = 1,
.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
BIT(IIO_CHAN_INFO_SCALE),
.scan_type.sign = 'u',
.scan_type.storagebits = 32,
};
static const struct iio_chan_spec ad_pulsar_chan_template_bw = {
.type = IIO_VOLTAGE,
.indexed = 1,
.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
BIT(IIO_CHAN_INFO_SCALE) |
BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
.info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
.scan_type.sign = 'u',
.scan_type.storagebits = 32,
};
static int ad_pulsar_reg_write(struct ad_pulsar_adc *adc, unsigned int reg,
unsigned int val)
{
struct spi_transfer xfer = {
.bits_per_word = adc->info->resolution,
.speed_hz = adc->info->sclk_rate,
.len = 4,
};
adc->cfg = val;
put_unaligned_be16(val << 2, adc->spi_tx_data);
xfer.tx_buf = adc->spi_tx_data;
return spi_sync_transfer(adc->spi, &xfer, 1);
}
static int ad_pulsar_read_channel(struct ad_pulsar_adc *adc, unsigned int reg,
unsigned int *val)
{
struct spi_transfer xfer = {
.bits_per_word = adc->info->resolution,
.speed_hz = adc->info->sclk_rate,
.len = 4,
};
int ret;
adc->cfg = reg;
put_unaligned_be16(reg << 2, adc->spi_tx_data);
if (adc->info->cfg_register)
xfer.tx_buf = adc->spi_tx_data;
xfer.rx_buf = adc->spi_rx_data;
ret = spi_sync_transfer(adc->spi, &xfer, 1);
if (ret)
return ret;
*val = get_unaligned_le32(adc->spi_rx_data);
return ret;
}
static int ad_pulsar_reg_access(struct iio_dev *indio_dev, unsigned int reg,
unsigned int writeval, unsigned int *readval)
{
struct ad_pulsar_adc *adc = iio_priv(indio_dev);
if (readval) {
*readval = adc->cfg;
return 0;
}
return ad_pulsar_reg_write(adc, reg, writeval);
}
static int ad_pulsar_set_samp_freq(struct ad_pulsar_adc *adc, int freq)
{
unsigned long long target, ref_clk_period_ps;
struct pwm_state cnv_state;
int ret;
freq = clamp(freq, 0, adc->info->max_rate);
target = DIV_ROUND_CLOSEST_ULL(adc->ref_clk_rate, freq);
ref_clk_period_ps = DIV_ROUND_CLOSEST_ULL(1000000000000,
adc->ref_clk_rate);
cnv_state.period = ref_clk_period_ps * target;
cnv_state.duty_cycle = ref_clk_period_ps;
cnv_state.phase = ref_clk_period_ps;
cnv_state.time_unit = PWM_UNIT_PSEC;
cnv_state.enabled = true;
ret = pwm_apply_state(adc->cnv, &cnv_state);
if (ret)
return ret;
adc->samp_freq = DIV_ROUND_CLOSEST_ULL(adc->ref_clk_rate, target);
return ret;
}
static int ad_pulsar_set_lpf(struct ad_pulsar_adc *adc, int index,
unsigned int val)
{
enum ad_pulsar_filter_bw filter;
if (val == ad_pulsar_filter_freq[QUARTER_BW])
filter = QUARTER_BW;
else if (val == ad_pulsar_filter_freq[FULL_BW])
filter = FULL_BW;
else
return -EINVAL;
if (adc->info->has_filter) {
adc->cfg_reg[index] &= ~AD7682_BW_MSK;
adc->cfg_reg[index] |= AD7682_BW_SEL(filter);
}
return 0;
}
static int ad_pulsar_get_lpf(struct ad_pulsar_adc *adc, int index, int *val)
{
if (!adc->info->has_filter)
return -EINVAL;
*val = ad_pulsar_filter_freq[AD7682_GET_BW(adc->cfg_reg[index])];
return 0;
}
static int ad_pulsar_read_raw(struct iio_dev *indio_dev,
const struct iio_chan_spec *chan,
int *val, int *val2, long info)
{
struct ad_pulsar_adc *adc = iio_priv(indio_dev);
int ret;
switch (info) {
case IIO_CHAN_INFO_RAW:
/*
* Conversion requires 2 acquisitions for some ADCs (AD7682
* Datasheet page 31).
*/
if (adc->info->cfg_register) {
ret = ad_pulsar_read_channel(adc, chan->address, val);
if (ret)
return ret;
ret = ad_pulsar_read_channel(adc, chan->address, val);
if (ret)
return ret;
}
ret = ad_pulsar_read_channel(adc, chan->address, val);
if (ret)
return ret;
if (chan->differential)
*val = sign_extend32(*val, adc->info->resolution - 1);
return IIO_VAL_INT;
case IIO_CHAN_INFO_SAMP_FREQ:
*val = adc->samp_freq;
return IIO_VAL_INT;
case IIO_CHAN_INFO_SCALE:
switch (chan->type) {
case IIO_VOLTAGE:
ret = regulator_get_voltage(adc->vref);
if (ret < 0)
return ret;
*val = ret / 1000;
*val2 = adc->info->resolution;
return IIO_VAL_FRACTIONAL_LOG2;
case IIO_TEMP:
*val = 8933;
*val2 = 1000;
return IIO_VAL_FRACTIONAL;
default:
return -EINVAL;
}
case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
ret = ad_pulsar_get_lpf(adc, chan->scan_index, val);
if (ret)
return ret;
return IIO_VAL_INT;
default:
return -EINVAL;
}
}
static int ad_pulsar_write_raw(struct iio_dev *indio_dev,
const struct iio_chan_spec *chan,
int val, int val2, long info)
{
struct ad_pulsar_adc *adc = iio_priv(indio_dev);
int ret;
switch (info) {
case IIO_CHAN_INFO_SAMP_FREQ:
return ad_pulsar_set_samp_freq(adc, val);
case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
ret = ad_pulsar_set_lpf(adc, chan->scan_index, val);
if (ret)
return ret;
return ad_pulsar_reg_write(adc, AD7682_REG_CONFIG,
adc->cfg_reg[chan->scan_index]);
default:
return -EINVAL;
}
}
static int ad_pulsar_read_avail(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
const int **vals, int *type, int *length,
long info)
{
switch (info) {
case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
*vals = ad_pulsar_filter_freq;
*length = ARRAY_SIZE(ad_pulsar_filter_freq);
*type = IIO_VAL_INT;
return IIO_AVAIL_LIST;
default:
return -EINVAL;
}
}
static int ad_pulsar_buffer(struct iio_dev *indio_dev,
struct spi_message *msg)
{
struct ad_pulsar_adc *adc = iio_priv(indio_dev);
u8 ch, next_active_ch, first, second, last, active_ch[8];
unsigned int num_en_ch;
u8 next_in_sequ = 1;
u8 i = 0;
int ret;
num_en_ch = bitmap_weight(indio_dev->active_scan_mask,
adc->info->num_channels);
last = find_last_bit(indio_dev->active_scan_mask,
indio_dev->masklength);
first = find_first_bit(indio_dev->active_scan_mask,
indio_dev->masklength);
if (num_en_ch > 1) {
second = find_next_bit(indio_dev->active_scan_mask,
indio_dev->masklength,
first + 1);
}
spi_message_init(msg);
for_each_set_bit(ch, indio_dev->active_scan_mask,
indio_dev->masklength) {
active_ch[i] = ch;
i++;
}
/*
* Conversion requires 2 acquisitions for some ADCs (AD7682
* Datasheet page 31)
*/
for (i = 0; i < num_en_ch; i++) {
ch = active_ch[i];
/*
* Configurations need to be sent prior to the acquisition.
*/
if (num_en_ch > 1) {
if (next_in_sequ >= num_en_ch)
next_in_sequ = 0;
next_active_ch = active_ch[next_in_sequ];
adc->seq_xfer[ch].tx_buf = &adc->cfg_reg[next_active_ch];
next_in_sequ++;
}
adc->seq_xfer[ch].cs_change = 1;
adc->seq_xfer[ch].word_delay.value = 2;
adc->seq_xfer[ch].word_delay.unit = SPI_DELAY_UNIT_USECS;
if (ch == last)
adc->seq_xfer[ch].cs_change = 0;
spi_message_add_tail(&adc->seq_xfer[ch], msg);
}
/*
* The first two configurations need to be sent before populating the
* buffer.
*/
ret = ad_pulsar_reg_write(adc, AD7682_REG_CONFIG,
adc->cfg_reg[first]);
if (ret)
return ret;
if (num_en_ch > 1)
return ad_pulsar_reg_write(adc, AD7682_REG_CONFIG,
adc->cfg_reg[second]);
return ad_pulsar_reg_write(adc, AD7682_REG_CONFIG,
adc->cfg_reg[first]);
}
static int ad_pulsar_buffer_preenable(struct iio_dev *indio_dev)
{
struct ad_pulsar_adc *adc = iio_priv(indio_dev);
struct spi_message msg;
int ret;
ret = ad_pulsar_buffer(indio_dev, &msg);
if (ret)
return ret;
spi_bus_lock(adc->spi->master);
ret = spi_engine_offload_load_msg(adc->spi, &msg);
if (ret)
return ret;
spi_engine_offload_enable(adc->spi, true);
return 0;
}
static int ad_pulsar_buffer_postdisable(struct iio_dev *indio_dev)
{
struct ad_pulsar_adc *adc = iio_priv(indio_dev);
int ret;
spi_engine_offload_enable(adc->spi, false);
spi_bus_unlock(adc->spi->master);
ret = ad_pulsar_reg_write(adc, AD7682_REG_CONFIG, AD7682_DISABLE_SEQ);
if (ret)
return ret;
return ad_pulsar_reg_write(adc, AD7682_REG_CONFIG, AD7682_DISABLE_SEQ);
}
static const struct iio_buffer_setup_ops ad_pulsar_buffer_ops = {
.preenable = &ad_pulsar_buffer_preenable,
.postdisable = &ad_pulsar_buffer_postdisable,
};
static const struct iio_info ad_pulsar_iio_info = {
.read_raw = &ad_pulsar_read_raw,
.write_raw = &ad_pulsar_write_raw,
.read_avail = &ad_pulsar_read_avail,
.debugfs_reg_access = &ad_pulsar_reg_access,
};
static int ad_pulsar_setup_channel(struct ad_pulsar_adc *adc,
struct fwnode_handle *child)
{
unsigned int dummy, in[2] = {};
u32 chan_index;
int ret;
ret = fwnode_property_read_u32(child, "reg", &chan_index);
if (ret)
return ret;
if (fwnode_property_present(child, "diff-channels")) {
ret = fwnode_property_read_u32_array(child,
"diff-channels",
in, 2);
if (ret)
return ret;
if (in[0] > 7 || in[1] > 7 || in[0] % 2 != 0 ||
(in[0] % 2 == 0 && in[1] != in[0] + 1))
return -EINVAL;
adc->cfg_reg[chan_index] = AD7682_SEQ_EN_CHANNEL(in[0]);
adc->cfg_reg[chan_index] &= ~AD7682_PAIR_MSK;
adc->cfg_reg[chan_index] |= AD7682_CH_TYPE(DIFFERENTIAL);
adc->channels[chan_index].differential = 1;
adc->channels[chan_index].scan_type.sign = 's';
adc->channels[chan_index].channel2 = in[1];
} else if (fwnode_property_read_bool(child, "adi,temp-sensor")) {
adc->cfg_reg[chan_index] = AD7682_CH_TEMP_SENSOR;
adc->channels[chan_index].type = IIO_TEMP;
adc->channels[chan_index].indexed = 0;
} else {
ret = fwnode_property_read_u32(child,
"adi,single-channel",
&in[0]);
if (ret)
return ret;
adc->cfg_reg[chan_index] = AD7682_SEQ_EN_CHANNEL(in[0]);
if (in[0] > adc->info->num_channels)
return -EINVAL;
}
if (fwnode_property_read_bool(child, "bipolar")) {
adc->channels[chan_index].scan_type.sign = 's';
adc->cfg_reg[chan_index] &= ~AD7682_POLARITY_MSK;
adc->cfg_reg[chan_index] |= AD7682_CH_POLARITY(BIPOLAR);
}
adc->channels[chan_index].channel = in[0];
adc->channels[chan_index].scan_index = chan_index;
adc->channels[chan_index].address = adc->cfg_reg[chan_index];
if (adc->info->cfg_register)
adc->seq_xfer[chan_index].tx_buf = &adc->cfg_reg[chan_index];
adc->seq_xfer[chan_index].rx_buf = &dummy;
adc->seq_xfer[chan_index].len = 1;
adc->seq_xfer[chan_index].bits_per_word = adc->info->resolution;
adc->seq_xfer[chan_index].speed_hz = adc->info->sclk_rate;
return 0;
}
static int ad_pulsar_parse_channels(struct iio_dev *indio_dev)
{
struct ad_pulsar_adc *adc = iio_priv(indio_dev);
struct device *dev = indio_dev->dev.parent;
struct fwnode_handle *child;
int num_ch, ret, i;
num_ch = device_get_child_node_count(dev);
if (!num_ch || num_ch > adc->info->num_channels)
return -EINVAL;
adc->channels = devm_kcalloc(dev, num_ch, sizeof(struct iio_chan_spec),
GFP_KERNEL);
if (!adc->channels)
return -ENOMEM;
for (i = 0; i < num_ch; i++) {
if (adc->info->has_filter)
adc->channels[i] = ad_pulsar_chan_template_bw;
else
adc->channels[i] = ad_pulsar_chan_template;
adc->channels[i].channel = i;
adc->channels[i].scan_index = i;
adc->channels[i].scan_type.realbits = adc->info->resolution;
}
indio_dev->channels = adc->channels;
indio_dev->num_channels = num_ch;
adc->cfg_reg = devm_kcalloc(dev, num_ch, sizeof(unsigned short),
GFP_KERNEL);
if (!adc->cfg_reg)
return -ENOMEM;
adc->seq_xfer = devm_kcalloc(dev, num_ch, sizeof(struct spi_transfer),
GFP_KERNEL);
if (!adc->seq_xfer)
return -ENOMEM;
device_for_each_child_node(dev, child) {
ret = ad_pulsar_setup_channel(adc, child);
if (ret) {
fwnode_handle_put(child);
return ret;
}
}
return 0;
}
static void ad_pulsar_reg_disable(void *data)
{
regulator_disable(data);
}
static void ad_pulsar_pwm_diasble(void *data)
{
pwm_disable(data);
}
static void ad_pulsar_clk_disable(void *data)
{
clk_disable_unprepare(data);
}
static int ad_pulsar_probe(struct spi_device *spi)
{
struct ad_pulsar_adc *adc;
struct iio_dev *indio_dev;
struct clk *ref_clk;
int ret;
indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
if (!indio_dev)
return -ENOMEM;
adc = iio_priv(indio_dev);
adc->spi = spi;
adc->vref = devm_regulator_get(&spi->dev, "vref");
if (IS_ERR(adc->vref))
return PTR_ERR(adc->vref);
ret = regulator_enable(adc->vref);
if (ret) {
dev_err(&spi->dev, "Failed to enable VREF regulator");
return ret;
}
ret = devm_add_action_or_reset(&spi->dev, ad_pulsar_reg_disable,
adc->vref);
if (ret)
return ret;
ref_clk = devm_clk_get(&spi->dev, "ref_clk");
if (IS_ERR(ref_clk))
return PTR_ERR(ref_clk);
ret = clk_prepare_enable(ref_clk);
if (ret)
return ret;
ret = devm_add_action_or_reset(&spi->dev, ad_pulsar_clk_disable,
ref_clk);
if (ret)
return ret;
adc->ref_clk_rate = clk_get_rate(ref_clk);
adc->cnv = devm_pwm_get(&spi->dev, "cnv");
if (IS_ERR(adc->cnv))
return PTR_ERR(adc->cnv);
ret = devm_add_action_or_reset(&spi->dev, ad_pulsar_pwm_diasble,
adc->cnv);
if (ret)
return ret;
adc->info = device_get_match_data(&spi->dev);
if (!adc->info) {
adc->info = (struct ad_pulsar_chip_info *)
spi_get_device_id(spi)->driver_data;
if (!adc->info)
return -EINVAL;
}
ret = ad_pulsar_parse_channels(indio_dev);
if (ret)
return ret;
indio_dev->name = adc->info->name;
indio_dev->info = &ad_pulsar_iio_info;
indio_dev->modes = INDIO_BUFFER_HARDWARE | INDIO_DIRECT_MODE;
indio_dev->setup_ops = &ad_pulsar_buffer_ops;
ret = devm_iio_dmaengine_buffer_setup(indio_dev->dev.parent,
indio_dev, "rx",
IIO_BUFFER_DIRECTION_IN);
if (ret)
return ret;
ret = ad_pulsar_set_samp_freq(adc, adc->info->max_rate);
if (ret)
return ret;
return devm_iio_device_register(&spi->dev, indio_dev);
}
static const struct of_device_id ad_pulsar_of_match[] = {
{ .compatible = "adi,pulsar,ad7988-5", .data = &ad7988_5_chip_info },
{ .compatible = "adi,pulsar,ad7988-1", .data = &ad7988_1_chip_info },
{ .compatible = "adi,pulsar,ad7984", .data = &ad7984_chip_info },
{ .compatible = "adi,pulsar,ad7983", .data = &ad7983_chip_info },
{ .compatible = "adi,pulsar,ad7982", .data = &ad7982_chip_info },
{ .compatible = "adi,pulsar,ad7980", .data = &ad7980_chip_info },
{ .compatible = "adi,pulsar,ad7949", .data = &ad7949_chip_info },
{ .compatible = "adi,pulsar,ad7946", .data = &ad7946_chip_info },
{ .compatible = "adi,pulsar,ad7942", .data = &ad7942_chip_info },
{ .compatible = "adi,pulsar,ad7699", .data = &ad7699_chip_info },
{ .compatible = "adi,pulsar,ad7693", .data = &ad7693_chip_info },
{ .compatible = "adi,pulsar,ad7691", .data = &ad7691_chip_info },
{ .compatible = "adi,pulsar,ad7690", .data = &ad7690_chip_info },
{ .compatible = "adi,pulsar,ad7689", .data = &ad7689_chip_info },
{ .compatible = "adi,pulsar,ad7688", .data = &ad7688_chip_info },
{ .compatible = "adi,pulsar,ad7687", .data = &ad7687_chip_info },
{ .compatible = "adi,pulsar,ad7686", .data = &ad7686_chip_info },
{ .compatible = "adi,pulsar,ad7685", .data = &ad7685_chip_info },
{ .compatible = "adi,pulsar,ad7682", .data = &ad7682_chip_info },
{ },
};
MODULE_DEVICE_TABLE(of, ad_pulsar_of_match);
static const struct spi_device_id ad_pulsar_spi_id[] = {
{ "adi,pulsar,ad7988-5", (kernel_ulong_t)&ad7988_5_chip_info },
{ "adi,pulsar,ad7988-1", (kernel_ulong_t)&ad7988_1_chip_info },
{ "adi,pulsar,ad7984", (kernel_ulong_t)&ad7984_chip_info },
{ "adi,pulsar,ad7983", (kernel_ulong_t)&ad7983_chip_info },
{ "adi,pulsar,ad7982", (kernel_ulong_t)&ad7982_chip_info },
{ "adi,pulsar,ad7980", (kernel_ulong_t)&ad7980_chip_info },
{ "adi,pulsar,ad7949", (kernel_ulong_t)&ad7949_chip_info },
{ "adi,pulsar,ad7946", (kernel_ulong_t)&ad7946_chip_info },
{ "adi,pulsar,ad7942", (kernel_ulong_t)&ad7942_chip_info },
{ "adi,pulsar,ad7699", (kernel_ulong_t)&ad7699_chip_info },
{ "adi,pulsar,ad7693", (kernel_ulong_t)&ad7693_chip_info },
{ "adi,pulsar,ad7691", (kernel_ulong_t)&ad7691_chip_info },
{ "adi,pulsar,ad7690", (kernel_ulong_t)&ad7690_chip_info },
{ "adi,pulsar,ad7689", (kernel_ulong_t)&ad7689_chip_info },
{ "adi,pulsar,ad7688", (kernel_ulong_t)&ad7688_chip_info },
{ "adi,pulsar,ad7687", (kernel_ulong_t)&ad7687_chip_info },
{ "adi,pulsar,ad7686", (kernel_ulong_t)&ad7686_chip_info },
{ "adi,pulsar,ad7685", (kernel_ulong_t)&ad7685_chip_info },
{ "adi,pulsar,ad7682", (kernel_ulong_t)&ad7682_chip_info },
{ }
};
MODULE_DEVICE_TABLE(spi, ad_pulsar_spi_id);
static struct spi_driver ad_pulsar_driver = {
.driver = {
.name = "pulsar_adc",
.of_match_table = ad_pulsar_of_match,
},
.probe = ad_pulsar_probe,
.id_table = ad_pulsar_spi_id,
};
module_spi_driver(ad_pulsar_driver);
MODULE_AUTHOR("Sergiu Cuciurean <[email protected]>");
MODULE_DESCRIPTION("Analog Devices PulSAR ADC family driver");
MODULE_LICENSE("GPL");