@@ -165,18 +165,19 @@ static const int hmc5883l_regval_to_input_field_mga[] = {
165
165
* 6 | 50 | 75
166
166
* 7 | Not used | Not used
167
167
*/
168
- static const char * const hmc5843_regval_to_sample_freq [ ] = {
169
- "0.5" , "1" , "2" , "5" , "10" , "20" , "50" ,
168
+ static const int hmc5843_regval_to_samp_freq [ 7 ][ 2 ] = {
169
+ { 0 , 500000 }, { 1 , 0 }, { 2 , 0 }, { 5 , 0 }, { 10 , 0 }, { 20 , 0 }, { 50 , 0 }
170
170
};
171
171
172
- static const char * const hmc5883_regval_to_sample_freq [] = {
173
- "0.75" , "1.5" , "3" , "7.5" , "15" , "30" , "75" ,
172
+ static const int hmc5883_regval_to_samp_freq [7 ][2 ] = {
173
+ {0 , 750000 }, {1 , 500000 }, {3 , 0 }, {7 , 500000 }, {15 , 0 }, {30 , 0 },
174
+ {75 , 0 }
174
175
};
175
176
176
177
/* Describe chip variants */
177
178
struct hmc5843_chip_info {
178
179
const struct iio_chan_spec * channels ;
179
- const char * const * regval_to_sample_freq ;
180
+ const int ( * regval_to_samp_freq )[ 2 ] ;
180
181
const int * regval_to_input_field_mga ;
181
182
const int * regval_to_nanoscale ;
182
183
};
@@ -370,17 +371,17 @@ static IIO_DEVICE_ATTR(meas_conf,
370
371
hmc5843_set_measurement_configuration ,
371
372
0 ) ;
372
373
373
- static ssize_t hmc5843_show_sampling_frequencies_available (struct device * dev ,
374
- struct device_attribute * attr ,
375
- char * buf )
374
+ static ssize_t hmc5843_show_samp_freq_avail (struct device * dev ,
375
+ struct device_attribute * attr , char * buf )
376
376
{
377
- struct iio_dev * indio_dev = dev_to_iio_dev (dev );
378
- struct hmc5843_data * data = iio_priv (indio_dev );
377
+ struct hmc5843_data * data = iio_priv (dev_to_iio_dev (dev ));
379
378
ssize_t total_n = 0 ;
380
379
int i ;
381
380
382
381
for (i = 0 ; i < HMC5843_RATE_NOT_USED ; i ++ ) {
383
- ssize_t n = sprintf (buf , "%s " , data -> variant -> regval_to_sample_freq [i ]);
382
+ ssize_t n = sprintf (buf , "%d.%d " ,
383
+ data -> variant -> regval_to_samp_freq [i ][0 ],
384
+ data -> variant -> regval_to_samp_freq [i ][1 ]);
384
385
buf += n ;
385
386
total_n += n ;
386
387
}
@@ -390,87 +391,30 @@ static ssize_t hmc5843_show_sampling_frequencies_available(struct device *dev,
390
391
return total_n ;
391
392
}
392
393
393
- static IIO_DEV_ATTR_SAMP_FREQ_AVAIL (hmc5843_show_sampling_frequencies_available );
394
+ static IIO_DEV_ATTR_SAMP_FREQ_AVAIL (hmc5843_show_samp_freq_avail );
394
395
395
396
static s32 hmc5843_set_rate (struct hmc5843_data * data , u8 rate )
396
397
{
397
- u8 reg_val ;
398
-
399
- if (rate >= HMC5843_RATE_NOT_USED ) {
400
- dev_err (& data -> client -> dev ,
401
- "data output rate is not supported\n" );
402
- return - EINVAL ;
403
- }
398
+ u8 reg_val = data -> meas_conf | (rate << HMC5843_RATE_OFFSET );
404
399
405
- reg_val = data -> meas_conf | (rate << HMC5843_RATE_OFFSET );
406
400
return i2c_smbus_write_byte_data (data -> client , HMC5843_CONFIG_REG_A ,
407
401
reg_val );
408
402
}
409
403
410
- static int hmc5843_check_sampling_frequency (struct hmc5843_data * data ,
411
- const char * buf )
404
+ static int hmc5843_check_samp_freq (struct hmc5843_data * data ,
405
+ int val , int val2 )
412
406
{
413
- const char * const * samp_freq = data -> variant -> regval_to_sample_freq ;
414
407
int i ;
415
408
416
409
for (i = 0 ; i < HMC5843_RATE_NOT_USED ; i ++ ) {
417
- if (sysfs_streq (buf , samp_freq [i ]))
410
+ if (val == data -> variant -> regval_to_samp_freq [i ][0 ] &&
411
+ val2 == data -> variant -> regval_to_samp_freq [i ][1 ])
418
412
return i ;
419
413
}
420
414
421
415
return - EINVAL ;
422
416
}
423
417
424
- static ssize_t hmc5843_set_sampling_frequency (struct device * dev ,
425
- struct device_attribute * attr ,
426
- const char * buf , size_t count )
427
- {
428
-
429
- struct iio_dev * indio_dev = dev_to_iio_dev (dev );
430
- struct hmc5843_data * data = iio_priv (indio_dev );
431
- int rate ;
432
-
433
- rate = hmc5843_check_sampling_frequency (data , buf );
434
- if (rate < 0 ) {
435
- dev_err (& data -> client -> dev ,
436
- "sampling frequency is not supported\n" );
437
- return rate ;
438
- }
439
-
440
- mutex_lock (& data -> lock );
441
- dev_dbg (dev , "set rate to %d\n" , rate );
442
- if (hmc5843_set_rate (data , rate )) {
443
- count = - EINVAL ;
444
- goto exit ;
445
- }
446
- data -> rate = rate ;
447
-
448
- exit :
449
- mutex_unlock (& data -> lock );
450
- return count ;
451
- }
452
-
453
- static ssize_t hmc5843_show_sampling_frequency (struct device * dev ,
454
- struct device_attribute * attr , char * buf )
455
- {
456
- struct iio_dev * indio_dev = dev_to_iio_dev (dev );
457
- struct iio_dev_attr * this_attr = to_iio_dev_attr (attr );
458
- struct hmc5843_data * data = iio_priv (indio_dev );
459
- s32 rate ;
460
-
461
- rate = i2c_smbus_read_byte_data (data -> client , this_attr -> address );
462
- if (rate < 0 )
463
- return rate ;
464
- rate = (rate & HMC5843_RATE_BITMASK ) >> HMC5843_RATE_OFFSET ;
465
- return sprintf (buf , "%s\n" , data -> variant -> regval_to_sample_freq [rate ]);
466
- }
467
-
468
- static IIO_DEVICE_ATTR (sampling_frequency ,
469
- S_IWUSR | S_IRUGO ,
470
- hmc5843_show_sampling_frequency ,
471
- hmc5843_set_sampling_frequency ,
472
- HMC5843_CONFIG_REG_A ) ;
473
-
474
418
static ssize_t hmc5843_show_range_gain (struct device * dev ,
475
419
struct device_attribute * attr ,
476
420
char * buf )
@@ -525,8 +469,7 @@ static IIO_DEVICE_ATTR(in_magn_range,
525
469
526
470
static int hmc5843_read_raw (struct iio_dev * indio_dev ,
527
471
struct iio_chan_spec const * chan ,
528
- int * val , int * val2 ,
529
- long mask )
472
+ int * val , int * val2 , long mask )
530
473
{
531
474
struct hmc5843_data * data = iio_priv (indio_dev );
532
475
@@ -537,17 +480,47 @@ static int hmc5843_read_raw(struct iio_dev *indio_dev,
537
480
* val = 0 ;
538
481
* val2 = data -> variant -> regval_to_nanoscale [data -> range ];
539
482
return IIO_VAL_INT_PLUS_NANO ;
483
+ case IIO_CHAN_INFO_SAMP_FREQ :
484
+ * val = data -> variant -> regval_to_samp_freq [data -> rate ][0 ];
485
+ * val2 = data -> variant -> regval_to_samp_freq [data -> rate ][1 ];
486
+ return IIO_VAL_INT_PLUS_MICRO ;
540
487
}
541
488
return - EINVAL ;
542
489
}
543
490
491
+ static int hmc5843_write_raw (struct iio_dev * indio_dev ,
492
+ struct iio_chan_spec const * chan ,
493
+ int val , int val2 , long mask )
494
+ {
495
+ struct hmc5843_data * data = iio_priv (indio_dev );
496
+ int ret , rate ;
497
+
498
+ switch (mask ) {
499
+ case IIO_CHAN_INFO_SAMP_FREQ :
500
+ rate = hmc5843_check_samp_freq (data , val , val2 );
501
+ if (rate < 0 )
502
+ return - EINVAL ;
503
+
504
+ mutex_lock (& data -> lock );
505
+ ret = hmc5843_set_rate (data , rate );
506
+ if (ret >= 0 )
507
+ data -> rate = rate ;
508
+ mutex_unlock (& data -> lock );
509
+
510
+ return ret ;
511
+ default :
512
+ return - EINVAL ;
513
+ }
514
+ }
515
+
544
516
#define HMC5843_CHANNEL (axis , addr ) \
545
517
{ \
546
518
.type = IIO_MAGN, \
547
519
.modified = 1, \
548
520
.channel2 = IIO_MOD_##axis, \
549
521
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
550
- .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
522
+ .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
523
+ BIT(IIO_CHAN_INFO_SAMP_FREQ), \
551
524
.address = addr \
552
525
}
553
526
@@ -566,7 +539,6 @@ static const struct iio_chan_spec hmc5883_channels[] = {
566
539
static struct attribute * hmc5843_attributes [] = {
567
540
& iio_dev_attr_meas_conf .dev_attr .attr ,
568
541
& iio_dev_attr_operating_mode .dev_attr .attr ,
569
- & iio_dev_attr_sampling_frequency .dev_attr .attr ,
570
542
& iio_dev_attr_in_magn_range .dev_attr .attr ,
571
543
& iio_dev_attr_sampling_frequency_available .dev_attr .attr ,
572
544
NULL
@@ -579,21 +551,21 @@ static const struct attribute_group hmc5843_group = {
579
551
static const struct hmc5843_chip_info hmc5843_chip_info_tbl [] = {
580
552
[HMC5843_ID ] = {
581
553
.channels = hmc5843_channels ,
582
- .regval_to_sample_freq = hmc5843_regval_to_sample_freq ,
554
+ .regval_to_samp_freq = hmc5843_regval_to_samp_freq ,
583
555
.regval_to_input_field_mga =
584
556
hmc5843_regval_to_input_field_mga ,
585
557
.regval_to_nanoscale = hmc5843_regval_to_nanoscale ,
586
558
},
587
559
[HMC5883_ID ] = {
588
560
.channels = hmc5883_channels ,
589
- .regval_to_sample_freq = hmc5883_regval_to_sample_freq ,
561
+ .regval_to_samp_freq = hmc5883_regval_to_samp_freq ,
590
562
.regval_to_input_field_mga =
591
563
hmc5883_regval_to_input_field_mga ,
592
564
.regval_to_nanoscale = hmc5883_regval_to_nanoscale ,
593
565
},
594
566
[HMC5883L_ID ] = {
595
567
.channels = hmc5883_channels ,
596
- .regval_to_sample_freq = hmc5883_regval_to_sample_freq ,
568
+ .regval_to_samp_freq = hmc5883_regval_to_samp_freq ,
597
569
.regval_to_input_field_mga =
598
570
hmc5883l_regval_to_input_field_mga ,
599
571
.regval_to_nanoscale = hmc5883l_regval_to_nanoscale ,
@@ -612,6 +584,7 @@ static void hmc5843_init(struct hmc5843_data *data)
612
584
static const struct iio_info hmc5843_info = {
613
585
.attrs = & hmc5843_group ,
614
586
.read_raw = & hmc5843_read_raw ,
587
+ .write_raw = & hmc5843_write_raw ,
615
588
.driver_module = THIS_MODULE ,
616
589
};
617
590
0 commit comments