@@ -120,6 +120,7 @@ struct ad7944_chip_info {
120
120
const char * name ;
121
121
const struct ad7944_timing_spec * timing_spec ;
122
122
const struct iio_chan_spec channels [2 ];
123
+ const struct iio_chan_spec offload_channels [1 ];
123
124
};
124
125
125
126
/*
@@ -150,6 +151,23 @@ static const struct ad7944_chip_info _name##_chip_info = { \
150
151
}, \
151
152
IIO_CHAN_SOFT_TIMESTAMP(1), \
152
153
}, \
154
+ .offload_channels = { \
155
+ { \
156
+ .type = IIO_VOLTAGE, \
157
+ .indexed = 1, \
158
+ .differential = _diff, \
159
+ .channel = 0, \
160
+ .channel2 = _diff ? 1 : 0, \
161
+ .scan_index = 0, \
162
+ .scan_type.sign = _diff ? 's' : 'u', \
163
+ .scan_type.realbits = _bits, \
164
+ .scan_type.storagebits = 32, \
165
+ .scan_type.endianness = IIO_CPU, \
166
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \
167
+ | BIT(IIO_CHAN_INFO_SCALE) \
168
+ | BIT(IIO_CHAN_INFO_SAMP_FREQ), \
169
+ }, \
170
+ }, \
153
171
}
154
172
155
173
/* pseudo-differential with ground sense */
@@ -194,7 +212,7 @@ static int ad7944_3wire_cs_mode_init_msg(struct device *dev, struct ad7944_adc *
194
212
195
213
/* Then we can read the data during the acquisition phase */
196
214
xfers [2 ].rx_buf = & adc -> sample .raw ;
197
- xfers [2 ].len = BITS_TO_BYTES ( chan -> scan_type .storagebits ) ;
215
+ xfers [2 ].len = chan -> scan_type .realbits > 16 ? 4 : 2 ;
198
216
xfers [2 ].bits_per_word = chan -> scan_type .realbits ;
199
217
200
218
spi_message_init_with_transfers (& adc -> msg , xfers , 3 );
@@ -228,7 +246,7 @@ static int ad7944_3wire_cs_mode_init_turbo_msg(struct device *dev,
228
246
229
247
/* read sample data from previous conversion */
230
248
xfers [1 ].rx_buf = & adc -> sample .raw ;
231
- xfers [1 ].len = BITS_TO_BYTES ( chan -> scan_type .storagebits ) ;
249
+ xfers [1 ].len = chan -> scan_type .realbits > 16 ? 4 : 2 ;
232
250
xfers [1 ].bits_per_word = chan -> scan_type .realbits ;
233
251
/*
234
252
* CNV has to be high at end of conversion to avoid triggering the busy
@@ -279,7 +297,7 @@ static int ad7944_4wire_mode_init_msg(struct device *dev, struct ad7944_adc *adc
279
297
xfers [0 ].delay .unit = SPI_DELAY_UNIT_NSECS ;
280
298
281
299
xfers [1 ].rx_buf = & adc -> sample .raw ;
282
- xfers [1 ].len = BITS_TO_BYTES ( chan -> scan_type .storagebits ) ;
300
+ xfers [1 ].len = chan -> scan_type .realbits > 16 ? 4 : 2 ;
283
301
xfers [1 ].bits_per_word = chan -> scan_type .realbits ;
284
302
285
303
spi_message_init_with_transfers (& adc -> msg , xfers , 3 );
@@ -356,7 +374,7 @@ static int ad7944_single_conversion(struct ad7944_adc *adc,
356
374
return - EOPNOTSUPP ;
357
375
}
358
376
359
- if (chan -> scan_type .storagebits > 16 )
377
+ if (chan -> scan_type .realbits > 16 )
360
378
* val = adc -> sample .raw .u32 ;
361
379
else
362
380
* val = adc -> sample .raw .u16 ;
@@ -399,90 +417,60 @@ static int ad7944_read_raw(struct iio_dev *indio_dev,
399
417
return - EINVAL ;
400
418
}
401
419
420
+ case IIO_CHAN_INFO_SAMP_FREQ : {
421
+ /* TODO: get actual hw period */
422
+ u64 period_ns = pwm_get_period (adc -> pwm );
423
+
424
+ * val = DIV_ROUND_UP_ULL (NSEC_PER_SEC , period_ns );
425
+ return IIO_VAL_INT ;
426
+ }
427
+
402
428
default :
403
429
return - EINVAL ;
404
430
}
405
431
}
406
432
407
- static ssize_t ad7944_sampling_frequency_show (struct device * dev ,
408
- struct device_attribute * attr ,
409
- char * buf )
433
+ static int ad7944_write_raw (struct iio_dev * indio_dev ,
434
+ const struct iio_chan_spec * chan ,
435
+ int val , int val2 , long info )
410
436
{
411
- struct iio_dev * indio_dev = dev_to_iio_dev (dev );
412
437
struct ad7944_adc * adc = iio_priv (indio_dev );
413
- u64 period_ns ;
414
-
415
- if (!adc -> pwm )
416
- return - ENODEV ;
417
-
418
- period_ns = pwm_get_period (adc -> pwm );
419
438
420
- return sysfs_emit (buf , "%llu\n" , div64_u64 (NSEC_PER_SEC , period_ns ));
421
- }
422
-
423
- static ssize_t ad7944_sampling_frequency_store (struct device * dev ,
424
- struct device_attribute * attr ,
425
- const char * buf , size_t len )
426
- {
427
- struct iio_dev * indio_dev = dev_to_iio_dev (dev );
428
- struct ad7944_adc * adc = iio_priv (indio_dev );
429
- struct pwm_state sample_state ;
430
- u32 val ;
431
- int ret ;
439
+ switch (info ) {
440
+ case IIO_CHAN_INFO_SAMP_FREQ : {
441
+ struct pwm_state state ;
432
442
433
- if (! adc -> pwm )
434
- return - ENODEV ;
443
+ if (val < 0 )
444
+ return - EINVAL ;
435
445
436
- ret = kstrtouint (buf , 0 , & val );
437
- if (ret )
438
- return ret ;
446
+ pwm_init_state (adc -> pwm , & state );
447
+ state .period = DIV_ROUND_UP (NSEC_PER_SEC , val );
448
+ state .duty_cycle = AD7944_PWM_TRIGGER_DUTY_CYCLE_NS ;
449
+ state .enabled = true;
439
450
440
- if (val == 0 )
451
+ return pwm_apply_state (adc -> pwm , & state );
452
+ }
453
+ default :
441
454
return - EINVAL ;
442
-
443
- pwm_init_state (adc -> pwm , & sample_state );
444
- sample_state .period = div_u64 (NSEC_PER_SEC , val );
445
- sample_state .duty_cycle = AD7944_PWM_TRIGGER_DUTY_CYCLE_NS ;
446
- sample_state .enabled = true;
447
-
448
- ret = pwm_apply_state (adc -> pwm , & sample_state );
449
- if (ret )
450
- return ret ;
451
-
452
- return len ;
455
+ }
453
456
}
454
457
455
- static IIO_DEV_ATTR_SAMP_FREQ (0644 , ad7944_sampling_frequency_show ,
456
- ad7944_sampling_frequency_store ) ;
457
-
458
- static struct attribute * ad7944_attrs [] = {
459
- & iio_dev_attr_sampling_frequency .dev_attr .attr ,
460
- NULL
461
- };
462
-
463
- static umode_t ad7944_attrs_is_visible (struct kobject * kobj ,
464
- struct attribute * attr , int unused )
458
+ static int ad7944_write_raw_get_fmt (struct iio_dev * indio_dev ,
459
+ const struct iio_chan_spec * chan ,
460
+ long info )
465
461
{
466
- struct device * dev = kobj_to_dev (kobj );
467
- struct iio_dev * indio_dev = dev_to_iio_dev (dev );
468
- struct ad7944_adc * adc = iio_priv (indio_dev );
469
-
470
- /* hide sampling_frequency attribute when there is no pwm */
471
- if (attr == & iio_dev_attr_sampling_frequency .dev_attr .attr && !adc -> pwm )
472
- return 0 ;
473
-
474
- /* show all other attributes */
475
- return attr -> mode ;
462
+ switch (info ) {
463
+ case IIO_CHAN_INFO_SAMP_FREQ :
464
+ return IIO_VAL_INT ;
465
+ default :
466
+ return IIO_VAL_INT_PLUS_MICRO ;
467
+ }
476
468
}
477
469
478
- static const struct attribute_group ad7944_group = {
479
- .attrs = ad7944_attrs ,
480
- .is_visible = ad7944_attrs_is_visible ,
481
- };
482
-
483
470
static const struct iio_info ad7944_iio_info = {
484
471
.read_raw = & ad7944_read_raw ,
485
- .attrs = & ad7944_group ,
472
+ .write_raw = & ad7944_write_raw ,
473
+ .write_raw_get_fmt = & ad7944_write_raw_get_fmt ,
486
474
};
487
475
488
476
static int ad7944_offload_ex_buffer_preenable (struct iio_dev * indio_dev )
@@ -730,8 +718,6 @@ static int ad7944_probe(struct spi_device *spi)
730
718
indio_dev -> name = chip_info -> name ;
731
719
indio_dev -> modes = INDIO_DIRECT_MODE ;
732
720
indio_dev -> info = & ad7944_iio_info ;
733
- indio_dev -> channels = chip_info -> channels ;
734
- indio_dev -> num_channels = ARRAY_SIZE (chip_info -> channels );
735
721
736
722
if (spi_engine_ex_offload_supported (spi )) {
737
723
struct pwm_state state = {
@@ -740,6 +726,10 @@ static int ad7944_probe(struct spi_device *spi)
740
726
.enabled = true,
741
727
};
742
728
729
+ indio_dev -> channels = chip_info -> offload_channels ;
730
+ indio_dev -> num_channels = ARRAY_SIZE (chip_info -> offload_channels );
731
+ indio_dev -> setup_ops = & ad7944_offload_ex_buffer_setup_ops ;
732
+
743
733
adc -> pwm = devm_pwm_get (dev , NULL );
744
734
if (IS_ERR (adc -> pwm ))
745
735
return dev_err_probe (dev , PTR_ERR (adc -> pwm ),
@@ -754,12 +744,10 @@ static int ad7944_probe(struct spi_device *spi)
754
744
IIO_BUFFER_DIRECTION_IN );
755
745
if (ret )
756
746
return ret ;
757
-
758
- indio_dev -> setup_ops = & ad7944_offload_ex_buffer_setup_ops ;
759
-
760
- /* can't have soft timestamp with SPI offload */
761
- indio_dev -> num_channels -- ;
762
747
} else {
748
+ indio_dev -> channels = chip_info -> channels ;
749
+ indio_dev -> num_channels = ARRAY_SIZE (chip_info -> channels );
750
+
763
751
ret = devm_iio_triggered_buffer_setup (dev , indio_dev ,
764
752
iio_pollfunc_store_time ,
765
753
ad7944_trigger_handler ,
0 commit comments