11
11
* Copyright 2024 BayLibre, SAS
12
12
*/
13
13
14
+ #include <linux/align.h>
14
15
#include <linux/bitfield.h>
15
16
#include <linux/bits.h>
16
17
#include <linux/compiler.h>
17
18
#include <linux/delay.h>
18
19
#include <linux/device.h>
19
20
#include <linux/err.h>
20
21
#include <linux/gpio/consumer.h>
22
+ #include <linux/iio/buffer.h>
21
23
#include <linux/iio/iio.h>
24
+ #include <linux/iio/triggered_buffer.h>
25
+ #include <linux/iio/trigger_consumer.h>
22
26
#include <linux/property.h>
23
27
#include <linux/regmap.h>
24
28
#include <linux/regulator/consumer.h>
61
65
#define AD4695_REG_GPIO_CTRL 0x0026
62
66
#define AD4695_REG_GP_MODE 0x0027
63
67
#define AD4695_REG_TEMP_CTRL 0x0029
68
+ #define AD4695_REG_TEMP_CTRL_TEMP_EN BIT(0)
64
69
#define AD4695_REG_CONFIG_IN (n ) (0x0030 | (n))
65
70
#define AD4695_REG_CONFIG_IN_MODE BIT(6)
66
71
#define AD4695_REG_CONFIG_IN_PAIR GENMASK(5, 4)
84
89
#define AD4695_T_WAKEUP_HW_MS 3
85
90
#define AD4695_T_WAKEUP_SW_MS 3
86
91
#define AD4695_T_REFBUF_MS 100
92
+ #define AD4695_T_REGCONFIG_NS 20
87
93
#define AD4695_REG_ACCESS_SCLK_HZ (10 * MEGA)
88
94
95
+ /* Max number of voltage input channels. */
89
96
#define AD4695_MAX_CHANNELS 16
97
+ /* Max size of 1 raw sample in bytes. */
98
+ #define AD4695_MAX_CHANNEL_SIZE 2
90
99
91
100
enum ad4695_in_pair {
92
101
AD4695_IN_PAIR_REFGND ,
@@ -97,6 +106,7 @@ enum ad4695_in_pair {
97
106
struct ad4695_chip_info {
98
107
const char * name ;
99
108
int max_sample_rate ;
109
+ u32 t_acq_ns ;
100
110
u8 num_voltage_inputs ;
101
111
};
102
112
@@ -112,15 +122,21 @@ struct ad4695_state {
112
122
struct spi_device * spi ;
113
123
struct regmap * regmap ;
114
124
struct gpio_desc * reset_gpio ;
115
- struct iio_chan_spec iio_chan [AD4695_MAX_CHANNELS + 1 ];
125
+ /* voltages channels plus temperature and timestamp */
126
+ struct iio_chan_spec iio_chan [AD4695_MAX_CHANNELS + 2 ];
116
127
struct ad4695_channel_config channels_cfg [AD4695_MAX_CHANNELS ];
117
128
const struct ad4695_chip_info * chip_info ;
118
129
/* Reference voltage. */
119
130
unsigned int vref_mv ;
120
131
/* Common mode input pin voltage. */
121
132
unsigned int com_mv ;
133
+ /* 1 per voltage and temperature chan plus 1 xfer to trigger 1st CNV */
134
+ struct spi_transfer buf_read_xfer [AD4695_MAX_CHANNELS + 2 ];
135
+ struct spi_message buf_read_msg ;
122
136
/* Raw conversion data received. */
123
- u16 raw_data __aligned (IIO_DMA_MINALIGN );
137
+ u8 buf [ALIGN ((AD4695_MAX_CHANNELS + 2 ) * AD4695_MAX_CHANNEL_SIZE ,
138
+ sizeof (s64 )) + sizeof (s64 )] __aligned (IIO_DMA_MINALIGN );
139
+ u16 raw_data ;
124
140
/* Commands to send for single conversion. */
125
141
u16 cnv_cmd ;
126
142
u8 cnv_cmd2 ;
@@ -193,31 +209,38 @@ static const struct iio_chan_spec ad4695_temp_channel_template = {
193
209
},
194
210
};
195
211
212
+ static const struct iio_chan_spec ad4695_soft_timestamp_channel_template =
213
+ IIO_CHAN_SOFT_TIMESTAMP (0 );
214
+
196
215
static const char * const ad4695_power_supplies [] = {
197
216
"avdd" , "vio"
198
217
};
199
218
200
219
static const struct ad4695_chip_info ad4695_chip_info = {
201
220
.name = "ad4695" ,
202
221
.max_sample_rate = 500 * KILO ,
222
+ .t_acq_ns = 1715 ,
203
223
.num_voltage_inputs = 16 ,
204
224
};
205
225
206
226
static const struct ad4695_chip_info ad4696_chip_info = {
207
227
.name = "ad4696" ,
208
228
.max_sample_rate = 1 * MEGA ,
229
+ .t_acq_ns = 715 ,
209
230
.num_voltage_inputs = 16 ,
210
231
};
211
232
212
233
static const struct ad4695_chip_info ad4697_chip_info = {
213
234
.name = "ad4697" ,
214
235
.max_sample_rate = 500 * KILO ,
236
+ .t_acq_ns = 1715 ,
215
237
.num_voltage_inputs = 8 ,
216
238
};
217
239
218
240
static const struct ad4695_chip_info ad4698_chip_info = {
219
241
.name = "ad4698" ,
220
242
.max_sample_rate = 1 * MEGA ,
243
+ .t_acq_ns = 715 ,
221
244
.num_voltage_inputs = 8 ,
222
245
};
223
246
@@ -254,6 +277,61 @@ static int ad4695_set_single_cycle_mode(struct ad4695_state *st,
254
277
AD4695_REG_SETUP_SPI_CYC_CTRL );
255
278
}
256
279
280
+ /**
281
+ * ad4695_enter_advanced_sequencer_mode - Put the ADC in advanced sequencer mode
282
+ * @st: The driver state
283
+ * @n: The number of slots to use - must be >= 2, <= 128
284
+ *
285
+ * As per the datasheet, to enable advanced sequencer, we need to set
286
+ * STD_SEQ_EN=0, NUM_SLOTS_AS=n-1 and CYC_CTRL=0 (Table 15). Setting SPI_MODE=1
287
+ * triggers the first conversion using the channel in AS_SLOT0.
288
+ *
289
+ * Return: 0 on success, a negative error code on failure
290
+ */
291
+ static int ad4695_enter_advanced_sequencer_mode (struct ad4695_state * st , u32 n )
292
+ {
293
+ int ret ;
294
+
295
+ ret = regmap_update_bits (st -> regmap , AD4695_REG_SEQ_CTRL ,
296
+ AD4695_REG_SEQ_CTRL_STD_SEQ_EN |
297
+ AD4695_REG_SEQ_CTRL_NUM_SLOTS_AS ,
298
+ FIELD_PREP (AD4695_REG_SEQ_CTRL_STD_SEQ_EN , 0 ) |
299
+ FIELD_PREP (AD4695_REG_SEQ_CTRL_NUM_SLOTS_AS , n - 1 ));
300
+ if (ret )
301
+ return ret ;
302
+
303
+ return regmap_update_bits (st -> regmap , AD4695_REG_SETUP ,
304
+ AD4695_REG_SETUP_SPI_MODE | AD4695_REG_SETUP_SPI_CYC_CTRL ,
305
+ FIELD_PREP (AD4695_REG_SETUP_SPI_MODE , 1 ) |
306
+ FIELD_PREP (AD4695_REG_SETUP_SPI_CYC_CTRL , 0 ));
307
+ }
308
+
309
+ /**
310
+ * ad4695_exit_conversion_mode - Exit conversion mode
311
+ * @st: The AD4695 state
312
+ *
313
+ * Sends SPI command to exit conversion mode.
314
+ *
315
+ * Return: 0 on success, a negative error code on failure
316
+ */
317
+ static int ad4695_exit_conversion_mode (struct ad4695_state * st )
318
+ {
319
+ struct spi_transfer xfer = {
320
+ .tx_buf = & st -> cnv_cmd2 ,
321
+ .len = 1 ,
322
+ .delay .value = AD4695_T_REGCONFIG_NS ,
323
+ .delay .unit = SPI_DELAY_UNIT_NSECS ,
324
+ };
325
+
326
+ /*
327
+ * Technically, could do a 5-bit transfer, but shifting to start of
328
+ * 8 bits instead for better SPI controller support.
329
+ */
330
+ st -> cnv_cmd2 = AD4695_CMD_EXIT_CNV_MODE << 3 ;
331
+
332
+ return spi_sync_transfer (st -> spi , & xfer , 1 );
333
+ }
334
+
257
335
static int ad4695_set_ref_voltage (struct ad4695_state * st , int vref_mv )
258
336
{
259
337
u8 val ;
@@ -296,6 +374,161 @@ static int ad4695_write_chn_cfg(struct ad4695_state *st,
296
374
mask , val );
297
375
}
298
376
377
+ static int ad4695_buffer_preenable (struct iio_dev * indio_dev )
378
+ {
379
+ struct ad4695_state * st = iio_priv (indio_dev );
380
+ struct spi_transfer * xfer ;
381
+ u8 temp_chan_bit = st -> chip_info -> num_voltage_inputs ;
382
+ u32 bit , num_xfer , num_slots ;
383
+ u32 temp_en = 0 ;
384
+ int ret ;
385
+
386
+ /*
387
+ * We are using the advanced sequencer since it is the only way to read
388
+ * multiple channels that allows individual configuration of each
389
+ * voltage input channel. Slot 0 in the advanced sequencer is used to
390
+ * account for the gap between trigger polls - we don't read data from
391
+ * this slot. Each enabled voltage channel is assigned a slot starting
392
+ * with slot 1.
393
+ */
394
+ num_slots = 1 ;
395
+
396
+ memset (st -> buf_read_xfer , 0 , sizeof (st -> buf_read_xfer ));
397
+
398
+ /* First xfer is only to trigger conversion of slot 1, so no rx. */
399
+ xfer = & st -> buf_read_xfer [0 ];
400
+ xfer -> cs_change = 1 ;
401
+ xfer -> delay .value = st -> chip_info -> t_acq_ns ;
402
+ xfer -> delay .unit = SPI_DELAY_UNIT_NSECS ;
403
+ xfer -> cs_change_delay .value = AD4695_T_CONVERT_NS ;
404
+ xfer -> cs_change_delay .unit = SPI_DELAY_UNIT_NSECS ;
405
+ num_xfer = 1 ;
406
+
407
+ iio_for_each_active_channel (indio_dev , bit ) {
408
+ xfer = & st -> buf_read_xfer [num_xfer ];
409
+ xfer -> bits_per_word = 16 ;
410
+ xfer -> rx_buf = & st -> buf [(num_xfer - 1 ) * 2 ];
411
+ xfer -> len = 2 ;
412
+ xfer -> cs_change = 1 ;
413
+ xfer -> cs_change_delay .value = AD4695_T_CONVERT_NS ;
414
+ xfer -> cs_change_delay .unit = SPI_DELAY_UNIT_NSECS ;
415
+
416
+ if (bit == temp_chan_bit ) {
417
+ temp_en = 1 ;
418
+ } else {
419
+ ret = regmap_write (st -> regmap ,
420
+ AD4695_REG_AS_SLOT (num_slots ),
421
+ FIELD_PREP (AD4695_REG_AS_SLOT_INX , bit ));
422
+ if (ret )
423
+ return ret ;
424
+
425
+ num_slots ++ ;
426
+ }
427
+
428
+ num_xfer ++ ;
429
+ }
430
+
431
+ /*
432
+ * The advanced sequencer requires that at least 2 slots are enabled.
433
+ * Since slot 0 is always used for other purposes, we need only 1
434
+ * enabled voltage channel to meet this requirement. If the temperature
435
+ * channel is the only enabled channel, we need to add one more slot
436
+ * in the sequence but not read from it.
437
+ */
438
+ if (num_slots < 2 ) {
439
+ /* move last xfer so we can insert one more xfer before it */
440
+ st -> buf_read_xfer [num_xfer ] = * xfer ;
441
+ num_xfer ++ ;
442
+
443
+ /* modify 2nd to last xfer for extra slot */
444
+ memset (xfer , 0 , sizeof (* xfer ));
445
+ xfer -> cs_change = 1 ;
446
+ xfer -> delay .value = st -> chip_info -> t_acq_ns ;
447
+ xfer -> delay .unit = SPI_DELAY_UNIT_NSECS ;
448
+ xfer -> cs_change_delay .value = AD4695_T_CONVERT_NS ;
449
+ xfer -> cs_change_delay .unit = SPI_DELAY_UNIT_NSECS ;
450
+ xfer ++ ;
451
+
452
+ /* and add the extra slot in the sequencer */
453
+ ret = regmap_write (st -> regmap ,
454
+ AD4695_REG_AS_SLOT (num_slots ),
455
+ FIELD_PREP (AD4695_REG_AS_SLOT_INX , 0 ));
456
+ if (ret )
457
+ return ret ;
458
+
459
+ num_slots ++ ;
460
+ }
461
+
462
+ /*
463
+ * Don't keep CS asserted after last xfer. Also triggers conversion of
464
+ * slot 0.
465
+ */
466
+ xfer -> cs_change = 0 ;
467
+
468
+ /*
469
+ * Temperature channel isn't included in the sequence, but rather
470
+ * controlled by setting a bit in the TEMP_CTRL register.
471
+ */
472
+
473
+ ret = regmap_update_bits (st -> regmap , AD4695_REG_TEMP_CTRL ,
474
+ AD4695_REG_TEMP_CTRL_TEMP_EN ,
475
+ FIELD_PREP (AD4695_REG_TEMP_CTRL_TEMP_EN , temp_en ));
476
+ if (ret )
477
+ return ret ;
478
+
479
+ spi_message_init_with_transfers (& st -> buf_read_msg , st -> buf_read_xfer ,
480
+ num_xfer );
481
+
482
+ ret = spi_optimize_message (st -> spi , & st -> buf_read_msg );
483
+ if (ret )
484
+ return ret ;
485
+
486
+ /* This triggers conversion of slot 0. */
487
+ ret = ad4695_enter_advanced_sequencer_mode (st , num_slots );
488
+ if (ret )
489
+ spi_unoptimize_message (& st -> buf_read_msg );
490
+
491
+ return ret ;
492
+ }
493
+
494
+ static int ad4695_buffer_postdisable (struct iio_dev * indio_dev )
495
+ {
496
+ struct ad4695_state * st = iio_priv (indio_dev );
497
+ int ret ;
498
+
499
+ ret = ad4695_exit_conversion_mode (st );
500
+ if (ret )
501
+ return ret ;
502
+
503
+ spi_unoptimize_message (& st -> buf_read_msg );
504
+
505
+ return 0 ;
506
+ }
507
+
508
+ static const struct iio_buffer_setup_ops ad4695_buffer_setup_ops = {
509
+ .preenable = ad4695_buffer_preenable ,
510
+ .postdisable = ad4695_buffer_postdisable ,
511
+ };
512
+
513
+ static irqreturn_t ad4695_trigger_handler (int irq , void * p )
514
+ {
515
+ struct iio_poll_func * pf = p ;
516
+ struct iio_dev * indio_dev = pf -> indio_dev ;
517
+ struct ad4695_state * st = iio_priv (indio_dev );
518
+ int ret ;
519
+
520
+ ret = spi_sync (st -> spi , & st -> buf_read_msg );
521
+ if (ret )
522
+ goto out ;
523
+
524
+ iio_push_to_buffers_with_timestamp (indio_dev , st -> buf , pf -> timestamp );
525
+
526
+ out :
527
+ iio_trigger_notify_done (indio_dev -> trig );
528
+
529
+ return IRQ_HANDLED ;
530
+ }
531
+
299
532
/**
300
533
* ad4695_read_one_sample - Read a single sample using single-cycle mode
301
534
* @st: The AD4695 state
@@ -527,6 +760,10 @@ static int ad4695_parse_channel_cfg(struct ad4695_state *st)
527
760
/* Temperature channel must be next scan index after voltage channels. */
528
761
st -> iio_chan [i ] = ad4695_temp_channel_template ;
529
762
st -> iio_chan [i ].scan_index = i ;
763
+ i ++ ;
764
+
765
+ st -> iio_chan [i ] = ad4695_soft_timestamp_channel_template ;
766
+ st -> iio_chan [i ].scan_index = i ;
530
767
531
768
return 0 ;
532
769
}
@@ -695,7 +932,14 @@ static int ad4695_probe(struct spi_device *spi)
695
932
indio_dev -> info = & ad4695_info ;
696
933
indio_dev -> modes = INDIO_DIRECT_MODE ;
697
934
indio_dev -> channels = st -> iio_chan ;
698
- indio_dev -> num_channels = st -> chip_info -> num_voltage_inputs + 1 ;
935
+ indio_dev -> num_channels = st -> chip_info -> num_voltage_inputs + 2 ;
936
+
937
+ ret = devm_iio_triggered_buffer_setup (dev , indio_dev ,
938
+ iio_pollfunc_store_time ,
939
+ ad4695_trigger_handler ,
940
+ & ad4695_buffer_setup_ops );
941
+ if (ret )
942
+ return ret ;
699
943
700
944
return devm_iio_device_register (dev , indio_dev );
701
945
}
0 commit comments