91
91
#define AD4695_T_WAKEUP_SW_MS 3
92
92
#define AD4695_T_REFBUF_MS 100
93
93
#define AD4695_T_REGCONFIG_NS 20
94
+ #define AD4695_T_SCK_CNV_DELAY_NS 80
94
95
#define AD4695_REG_ACCESS_SCLK_HZ (10 * MEGA)
95
96
96
97
/* Max number of voltage input channels. */
@@ -132,8 +133,13 @@ struct ad4695_state {
132
133
unsigned int vref_mv ;
133
134
/* Common mode input pin voltage. */
134
135
unsigned int com_mv ;
135
- /* 1 per voltage and temperature chan plus 1 xfer to trigger 1st CNV */
136
- struct spi_transfer buf_read_xfer [AD4695_MAX_CHANNELS + 2 ];
136
+ /*
137
+ * 2 per voltage and temperature chan plus 1 xfer to trigger 1st
138
+ * CNV. Excluding the trigger xfer, every 2nd xfer only serves
139
+ * to control CS and add a delay between the last SCLK and next
140
+ * CNV rising edges.
141
+ */
142
+ struct spi_transfer buf_read_xfer [AD4695_MAX_CHANNELS * 2 + 3 ];
137
143
struct spi_message buf_read_msg ;
138
144
/* Raw conversion data received. */
139
145
u8 buf [ALIGN ((AD4695_MAX_CHANNELS + 2 ) * AD4695_MAX_CHANNEL_SIZE ,
@@ -423,7 +429,7 @@ static int ad4695_buffer_preenable(struct iio_dev *indio_dev)
423
429
u8 temp_chan_bit = st -> chip_info -> num_voltage_inputs ;
424
430
u32 bit , num_xfer , num_slots ;
425
431
u32 temp_en = 0 ;
426
- int ret ;
432
+ int ret , rx_buf_offset = 0 ;
427
433
428
434
/*
429
435
* We are using the advanced sequencer since it is the only way to read
@@ -449,11 +455,9 @@ static int ad4695_buffer_preenable(struct iio_dev *indio_dev)
449
455
iio_for_each_active_channel (indio_dev , bit ) {
450
456
xfer = & st -> buf_read_xfer [num_xfer ];
451
457
xfer -> bits_per_word = 16 ;
452
- xfer -> rx_buf = & st -> buf [( num_xfer - 1 ) * 2 ];
458
+ xfer -> rx_buf = & st -> buf [rx_buf_offset ];
453
459
xfer -> len = 2 ;
454
- xfer -> cs_change = 1 ;
455
- xfer -> cs_change_delay .value = AD4695_T_CONVERT_NS ;
456
- xfer -> cs_change_delay .unit = SPI_DELAY_UNIT_NSECS ;
460
+ rx_buf_offset += xfer -> len ;
457
461
458
462
if (bit == temp_chan_bit ) {
459
463
temp_en = 1 ;
@@ -468,21 +472,44 @@ static int ad4695_buffer_preenable(struct iio_dev *indio_dev)
468
472
}
469
473
470
474
num_xfer ++ ;
475
+
476
+ /*
477
+ * We need to add a blank xfer in data reads, to meet the timing
478
+ * requirement of a minimum delay between the last SCLK rising
479
+ * edge and the CS deassert.
480
+ */
481
+ xfer = & st -> buf_read_xfer [num_xfer ];
482
+ xfer -> delay .value = AD4695_T_SCK_CNV_DELAY_NS ;
483
+ xfer -> delay .unit = SPI_DELAY_UNIT_NSECS ;
484
+ xfer -> cs_change = 1 ;
485
+ xfer -> cs_change_delay .value = AD4695_T_CONVERT_NS ;
486
+ xfer -> cs_change_delay .unit = SPI_DELAY_UNIT_NSECS ;
487
+
488
+ num_xfer ++ ;
471
489
}
472
490
473
491
/*
474
492
* The advanced sequencer requires that at least 2 slots are enabled.
475
493
* Since slot 0 is always used for other purposes, we need only 1
476
- * enabled voltage channel to meet this requirement. If the temperature
477
- * channel is the only enabled channel, we need to add one more slot
478
- * in the sequence but not read from it.
494
+ * enabled voltage channel to meet this requirement. If the temperature
495
+ * channel is the only enabled channel, we need to add one more slot in
496
+ * the sequence but not read from it. This is because the temperature
497
+ * sensor is sampled at the end of the channel sequence in advanced
498
+ * sequencer mode (see datasheet page 38).
499
+ *
500
+ * From the iio_for_each_active_channel() block above, we now have an
501
+ * xfer with data followed by a blank xfer to allow us to meet the
502
+ * timing spec, so move both of those up before adding an extra to
503
+ * handle the temperature-only case.
479
504
*/
480
505
if (num_slots < 2 ) {
481
- /* move last xfer so we can insert one more xfer before it */
482
- st -> buf_read_xfer [num_xfer ] = * xfer ;
506
+ /* Move last two xfers */
507
+ st -> buf_read_xfer [num_xfer ] = st -> buf_read_xfer [num_xfer - 1 ];
508
+ st -> buf_read_xfer [num_xfer - 1 ] = st -> buf_read_xfer [num_xfer - 2 ];
483
509
num_xfer ++ ;
484
510
485
- /* modify 2nd to last xfer for extra slot */
511
+ /* Modify inserted xfer for extra slot. */
512
+ xfer = & st -> buf_read_xfer [num_xfer - 3 ];
486
513
memset (xfer , 0 , sizeof (* xfer ));
487
514
xfer -> cs_change = 1 ;
488
515
xfer -> delay .value = st -> chip_info -> t_acq_ns ;
@@ -499,6 +526,12 @@ static int ad4695_buffer_preenable(struct iio_dev *indio_dev)
499
526
return ret ;
500
527
501
528
num_slots ++ ;
529
+
530
+ /*
531
+ * We still want to point at the last xfer when finished, so
532
+ * update the pointer.
533
+ */
534
+ xfer = & st -> buf_read_xfer [num_xfer - 1 ];
502
535
}
503
536
504
537
/*
@@ -583,38 +616,43 @@ static irqreturn_t ad4695_trigger_handler(int irq, void *p)
583
616
*/
584
617
static int ad4695_read_one_sample (struct ad4695_state * st , unsigned int address )
585
618
{
586
- struct spi_transfer xfer [2 ] = { };
587
- int ret , i = 0 ;
619
+ struct spi_transfer xfers [2 ] = {
620
+ {
621
+ .speed_hz = AD4695_REG_ACCESS_SCLK_HZ ,
622
+ .bits_per_word = 16 ,
623
+ .tx_buf = & st -> cnv_cmd ,
624
+ .len = 2 ,
625
+ },
626
+ {
627
+ /* Required delay between last SCLK and CNV/CS */
628
+ .delay .value = AD4695_T_SCK_CNV_DELAY_NS ,
629
+ .delay .unit = SPI_DELAY_UNIT_NSECS ,
630
+ }
631
+ };
632
+ int ret ;
588
633
589
634
ret = ad4695_set_single_cycle_mode (st , address );
590
635
if (ret )
591
636
return ret ;
592
637
593
638
/*
594
639
* Setting the first channel to the temperature channel isn't supported
595
- * in single-cycle mode, so we have to do an extra xfer to read the
596
- * temperature.
640
+ * in single-cycle mode, so we have to do an extra conversion to read
641
+ * the temperature.
597
642
*/
598
643
if (address == AD4695_CMD_TEMP_CHAN ) {
599
- /* We aren't reading, so we can make this a short xfer. */
600
- st -> cnv_cmd2 = AD4695_CMD_TEMP_CHAN << 3 ;
601
- xfer [0 ].tx_buf = & st -> cnv_cmd2 ;
602
- xfer [0 ].len = 1 ;
603
- xfer [0 ].cs_change = 1 ;
604
- xfer [0 ].cs_change_delay .value = AD4695_T_CONVERT_NS ;
605
- xfer [0 ].cs_change_delay .unit = SPI_DELAY_UNIT_NSECS ;
606
-
607
- i = 1 ;
644
+ st -> cnv_cmd = AD4695_CMD_TEMP_CHAN << 11 ;
645
+
646
+ ret = spi_sync_transfer (st -> spi , xfers , ARRAY_SIZE (xfers ));
647
+ if (ret )
648
+ return ret ;
608
649
}
609
650
610
651
/* Then read the result and exit conversion mode. */
611
652
st -> cnv_cmd = AD4695_CMD_EXIT_CNV_MODE << 11 ;
612
- xfer [i ].bits_per_word = 16 ;
613
- xfer [i ].tx_buf = & st -> cnv_cmd ;
614
- xfer [i ].rx_buf = & st -> raw_data ;
615
- xfer [i ].len = 2 ;
653
+ xfers [0 ].rx_buf = & st -> raw_data ;
616
654
617
- return spi_sync_transfer (st -> spi , xfer , i + 1 );
655
+ return spi_sync_transfer (st -> spi , xfers , ARRAY_SIZE ( xfers ) );
618
656
}
619
657
620
658
static int ad4695_read_raw (struct iio_dev * indio_dev ,
0 commit comments