@@ -356,24 +356,42 @@ static IIO_DEVICE_ATTR(meas_conf,
356
356
hmc5843_set_measurement_configuration ,
357
357
0 ) ;
358
358
359
- static ssize_t hmc5843_show_samp_freq_avail ( struct device * dev ,
360
- struct device_attribute * attr , char * buf )
359
+ static ssize_t hmc5843_show_int_plus_micros ( char * buf ,
360
+ const int ( * vals )[ 2 ], int n )
361
361
{
362
- struct hmc5843_data * data = iio_priv (dev_to_iio_dev (dev ));
363
- ssize_t total_n = 0 ;
362
+ size_t len = 0 ;
364
363
int i ;
365
364
366
- for (i = 0 ; i < HMC5843_RATE_NOT_USED ; i ++ ) {
367
- ssize_t n = sprintf (buf , "%d.%d " ,
368
- data -> variant -> regval_to_samp_freq [i ][0 ],
369
- data -> variant -> regval_to_samp_freq [i ][1 ]);
370
- buf += n ;
371
- total_n += n ;
372
- }
365
+ for (i = 0 ; i < n ; i ++ )
366
+ len += scnprintf (buf + len , PAGE_SIZE - len ,
367
+ "%d.%d " , vals [i ][0 ], vals [i ][1 ]);
368
+
373
369
/* replace trailing space by newline */
374
- buf [-1 ] = '\n' ;
370
+ buf [len - 1 ] = '\n' ;
371
+
372
+ return len ;
373
+ }
375
374
376
- return total_n ;
375
+ static int hmc5843_check_int_plus_micros (const int (* vals )[2 ], int n ,
376
+ int val , int val2 )
377
+ {
378
+ int i ;
379
+
380
+ for (i = 0 ; i < n ; i ++ ) {
381
+ if (val == vals [i ][0 ] && val2 == vals [i ][1 ])
382
+ return i ;
383
+ }
384
+
385
+ return - EINVAL ;
386
+ }
387
+
388
+ static ssize_t hmc5843_show_samp_freq_avail (struct device * dev ,
389
+ struct device_attribute * attr , char * buf )
390
+ {
391
+ struct hmc5843_data * data = iio_priv (dev_to_iio_dev (dev ));
392
+
393
+ return hmc5843_show_int_plus_micros (buf ,
394
+ data -> variant -> regval_to_samp_freq , HMC5843_RATE_NOT_USED );
377
395
}
378
396
379
397
static IIO_DEV_ATTR_SAMP_FREQ_AVAIL (hmc5843_show_samp_freq_avail );
@@ -389,15 +407,9 @@ static s32 hmc5843_set_rate(struct hmc5843_data *data, u8 rate)
389
407
static int hmc5843_check_samp_freq (struct hmc5843_data * data ,
390
408
int val , int val2 )
391
409
{
392
- int i ;
393
-
394
- for (i = 0 ; i < HMC5843_RATE_NOT_USED ; i ++ ) {
395
- if (val == data -> variant -> regval_to_samp_freq [i ][0 ] &&
396
- val2 == data -> variant -> regval_to_samp_freq [i ][1 ])
397
- return i ;
398
- }
399
-
400
- return - EINVAL ;
410
+ return hmc5843_check_int_plus_micros (
411
+ data -> variant -> regval_to_samp_freq , HMC5843_RATE_NOT_USED ,
412
+ val , val2 );
401
413
}
402
414
403
415
static ssize_t hmc5843_show_range_gain (struct device * dev ,
0 commit comments