6
6
* Copyright 2024 BayLibre, SAS
7
7
*/
8
8
9
+ #include <linux/align.h>
9
10
#include <linux/bitfield.h>
10
11
#include <linux/bitops.h>
11
12
#include <linux/delay.h>
@@ -53,6 +54,7 @@ struct ad7944_adc {
53
54
enum ad7944_spi_mode spi_mode ;
54
55
struct spi_transfer xfers [3 ];
55
56
struct spi_message msg ;
57
+ void * chain_mode_buf ;
56
58
/* Chip-specific timing specifications. */
57
59
const struct ad7944_timing_spec * timing_spec ;
58
60
/* GPIO connected to CNV pin. */
@@ -214,6 +216,46 @@ static int ad7944_4wire_mode_init_msg(struct device *dev, struct ad7944_adc *adc
214
216
return devm_add_action_or_reset (dev , ad7944_unoptimize_msg , & adc -> msg );
215
217
}
216
218
219
+ static int ad7944_chain_mode_init_msg (struct device * dev , struct ad7944_adc * adc ,
220
+ const struct iio_chan_spec * chan ,
221
+ u32 n_chain_dev )
222
+ {
223
+ struct spi_transfer * xfers = adc -> xfers ;
224
+ int ret ;
225
+
226
+ /*
227
+ * NB: SCLK has to be low before we toggle CS to avoid triggering the
228
+ * busy indication.
229
+ */
230
+ if (adc -> spi -> mode & SPI_CPOL )
231
+ return dev_err_probe (dev , - EINVAL ,
232
+ "chain mode requires ~SPI_CPOL\n" );
233
+
234
+ /*
235
+ * We only support CNV connected to CS in chain mode and we need CNV
236
+ * to be high during the transfer to trigger the conversion.
237
+ */
238
+ if (!(adc -> spi -> mode & SPI_CS_HIGH ))
239
+ return dev_err_probe (dev , - EINVAL ,
240
+ "chain mode requires SPI_CS_HIGH\n" );
241
+
242
+ /* CNV has to be high for full conversion time before reading data. */
243
+ xfers [0 ].delay .value = adc -> timing_spec -> conv_ns ;
244
+ xfers [0 ].delay .unit = SPI_DELAY_UNIT_NSECS ;
245
+
246
+ xfers [1 ].rx_buf = adc -> chain_mode_buf ;
247
+ xfers [1 ].len = BITS_TO_BYTES (chan -> scan_type .storagebits ) * n_chain_dev ;
248
+ xfers [1 ].bits_per_word = chan -> scan_type .realbits ;
249
+
250
+ spi_message_init_with_transfers (& adc -> msg , xfers , 2 );
251
+
252
+ ret = spi_optimize_message (adc -> spi , & adc -> msg );
253
+ if (ret )
254
+ return ret ;
255
+
256
+ return devm_add_action_or_reset (dev , ad7944_unoptimize_msg , & adc -> msg );
257
+ }
258
+
217
259
/**
218
260
* ad7944_convert_and_acquire - Perform a single conversion and acquisition
219
261
* @adc: The ADC device structure
@@ -223,7 +265,8 @@ static int ad7944_4wire_mode_init_msg(struct device *dev, struct ad7944_adc *adc
223
265
* Perform a conversion and acquisition of a single sample using the
224
266
* pre-optimized adc->msg.
225
267
*
226
- * Upon successful return adc->sample.raw will contain the conversion result.
268
+ * Upon successful return adc->sample.raw will contain the conversion result
269
+ * (or adc->chain_mode_buf if the device is using chain mode).
227
270
*/
228
271
static int ad7944_convert_and_acquire (struct ad7944_adc * adc ,
229
272
const struct iio_chan_spec * chan )
@@ -252,10 +295,17 @@ static int ad7944_single_conversion(struct ad7944_adc *adc,
252
295
if (ret )
253
296
return ret ;
254
297
255
- if (chan -> scan_type .storagebits > 16 )
256
- * val = adc -> sample .raw .u32 ;
257
- else
258
- * val = adc -> sample .raw .u16 ;
298
+ if (adc -> spi_mode == AD7944_SPI_MODE_CHAIN ) {
299
+ if (chan -> scan_type .storagebits > 16 )
300
+ * val = ((u32 * )adc -> chain_mode_buf )[chan -> scan_index ];
301
+ else
302
+ * val = ((u16 * )adc -> chain_mode_buf )[chan -> scan_index ];
303
+ } else {
304
+ if (chan -> scan_type .storagebits > 16 )
305
+ * val = adc -> sample .raw .u32 ;
306
+ else
307
+ * val = adc -> sample .raw .u16 ;
308
+ }
259
309
260
310
if (chan -> scan_type .sign == 's' )
261
311
* val = sign_extend32 (* val , chan -> scan_type .realbits - 1 );
@@ -315,15 +365,103 @@ static irqreturn_t ad7944_trigger_handler(int irq, void *p)
315
365
if (ret )
316
366
goto out ;
317
367
318
- iio_push_to_buffers_with_timestamp (indio_dev , & adc -> sample .raw ,
319
- pf -> timestamp );
368
+ if (adc -> spi_mode == AD7944_SPI_MODE_CHAIN )
369
+ iio_push_to_buffers_with_timestamp (indio_dev , adc -> chain_mode_buf ,
370
+ pf -> timestamp );
371
+ else
372
+ iio_push_to_buffers_with_timestamp (indio_dev , & adc -> sample .raw ,
373
+ pf -> timestamp );
320
374
321
375
out :
322
376
iio_trigger_notify_done (indio_dev -> trig );
323
377
324
378
return IRQ_HANDLED ;
325
379
}
326
380
381
+ /**
382
+ * ad7944_chain_mode_alloc - allocate and initialize channel specs and buffers
383
+ * for daisy-chained devices
384
+ * @dev: The device for devm_ functions
385
+ * @chan_template: The channel template for the devices (array of 2 channels
386
+ * voltage and timestamp)
387
+ * @n_chain_dev: The number of devices in the chain
388
+ * @chain_chan: Pointer to receive the allocated channel specs
389
+ * @chain_mode_buf: Pointer to receive the allocated rx buffer
390
+ * @chain_scan_masks: Pointer to receive the allocated scan masks
391
+ * Return: 0 on success, a negative error code on failure
392
+ */
393
+ static int ad7944_chain_mode_alloc (struct device * dev ,
394
+ const struct iio_chan_spec * chan_template ,
395
+ u32 n_chain_dev ,
396
+ struct iio_chan_spec * * chain_chan ,
397
+ void * * chain_mode_buf ,
398
+ unsigned long * * chain_scan_masks )
399
+ {
400
+ struct iio_chan_spec * chan ;
401
+ size_t chain_mode_buf_size ;
402
+ unsigned long * scan_masks ;
403
+ void * buf ;
404
+ int i ;
405
+
406
+ /* 1 channel for each device in chain plus 1 for soft timestamp */
407
+
408
+ chan = devm_kcalloc (dev , n_chain_dev + 1 , sizeof (* chan ), GFP_KERNEL );
409
+ if (!chan )
410
+ return - ENOMEM ;
411
+
412
+ for (i = 0 ; i < n_chain_dev ; i ++ ) {
413
+ chan [i ] = chan_template [0 ];
414
+
415
+ if (chan_template [0 ].differential ) {
416
+ chan [i ].channel = 2 * i ;
417
+ chan [i ].channel2 = 2 * i + 1 ;
418
+ } else {
419
+ chan [i ].channel = i ;
420
+ }
421
+
422
+ chan [i ].scan_index = i ;
423
+ }
424
+
425
+ /* soft timestamp */
426
+ chan [i ] = chan_template [1 ];
427
+ chan [i ].scan_index = i ;
428
+
429
+ * chain_chan = chan ;
430
+
431
+ /* 1 word for each voltage channel + aligned u64 for timestamp */
432
+
433
+ chain_mode_buf_size = ALIGN (n_chain_dev *
434
+ BITS_TO_BYTES (chan [0 ].scan_type .storagebits ), sizeof (u64 ))
435
+ + sizeof (u64 );
436
+ buf = devm_kzalloc (dev , chain_mode_buf_size , GFP_KERNEL );
437
+ if (!buf )
438
+ return - ENOMEM ;
439
+
440
+ * chain_mode_buf = buf ;
441
+
442
+ /*
443
+ * Have to limit n_chain_dev due to current implementation of
444
+ * available_scan_masks.
445
+ */
446
+ if (n_chain_dev > BITS_PER_LONG )
447
+ return dev_err_probe (dev , - EINVAL ,
448
+ "chain is limited to 32 devices\n" );
449
+
450
+ scan_masks = devm_kcalloc (dev , 2 , sizeof (* scan_masks ), GFP_KERNEL );
451
+ if (!scan_masks )
452
+ return - ENOMEM ;
453
+
454
+ /*
455
+ * Scan mask is needed since we always have to read all devices in the
456
+ * chain in one SPI transfer.
457
+ */
458
+ scan_masks [0 ] = GENMASK (n_chain_dev - 1 , 0 );
459
+
460
+ * chain_scan_masks = scan_masks ;
461
+
462
+ return 0 ;
463
+ }
464
+
327
465
static const char * const ad7944_power_supplies [] = {
328
466
"avdd" , "dvdd" , "bvdd" , "vio"
329
467
};
@@ -341,6 +479,9 @@ static int ad7944_probe(struct spi_device *spi)
341
479
struct ad7944_adc * adc ;
342
480
bool have_refin = false;
343
481
struct regulator * ref ;
482
+ struct iio_chan_spec * chain_chan ;
483
+ unsigned long * chain_scan_masks ;
484
+ u32 n_chain_dev ;
344
485
int ret ;
345
486
346
487
indio_dev = devm_iio_device_alloc (dev , sizeof (* adc ));
@@ -474,14 +615,39 @@ static int ad7944_probe(struct spi_device *spi)
474
615
475
616
break ;
476
617
case AD7944_SPI_MODE_CHAIN :
477
- return dev_err_probe (dev , - EINVAL , "chain mode is not implemented\n" );
618
+ ret = device_property_read_u32 (dev , "#daisy-chained-devices" ,
619
+ & n_chain_dev );
620
+ if (ret )
621
+ return dev_err_probe (dev , ret ,
622
+ "failed to get #daisy-chained-devices\n" );
623
+
624
+ ret = ad7944_chain_mode_alloc (dev , chip_info -> channels ,
625
+ n_chain_dev , & chain_chan ,
626
+ & adc -> chain_mode_buf ,
627
+ & chain_scan_masks );
628
+ if (ret )
629
+ return ret ;
630
+
631
+ ret = ad7944_chain_mode_init_msg (dev , adc , & chain_chan [0 ],
632
+ n_chain_dev );
633
+ if (ret )
634
+ return ret ;
635
+
636
+ break ;
478
637
}
479
638
480
639
indio_dev -> name = chip_info -> name ;
481
640
indio_dev -> modes = INDIO_DIRECT_MODE ;
482
641
indio_dev -> info = & ad7944_iio_info ;
483
- indio_dev -> channels = chip_info -> channels ;
484
- indio_dev -> num_channels = ARRAY_SIZE (chip_info -> channels );
642
+
643
+ if (adc -> spi_mode == AD7944_SPI_MODE_CHAIN ) {
644
+ indio_dev -> available_scan_masks = chain_scan_masks ;
645
+ indio_dev -> channels = chain_chan ;
646
+ indio_dev -> num_channels = n_chain_dev + 1 ;
647
+ } else {
648
+ indio_dev -> channels = chip_info -> channels ;
649
+ indio_dev -> num_channels = ARRAY_SIZE (chip_info -> channels );
650
+ }
485
651
486
652
ret = devm_iio_triggered_buffer_setup (dev , indio_dev ,
487
653
iio_pollfunc_store_time ,
0 commit comments