Skip to content

Commit d4dc0a3

Browse files
pmeerwjic23
authored andcommitted
staging:iio:hmc5843: Use INFO_SAMP_FREQ
Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent 8e2e297 commit d4dc0a3

File tree

1 file changed

+54
-81
lines changed

1 file changed

+54
-81
lines changed

drivers/staging/iio/magnetometer/hmc5843.c

Lines changed: 54 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,19 @@ static const int hmc5883l_regval_to_input_field_mga[] = {
165165
* 6 | 50 | 75
166166
* 7 | Not used | Not used
167167
*/
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}
170170
};
171171

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}
174175
};
175176

176177
/* Describe chip variants */
177178
struct hmc5843_chip_info {
178179
const struct iio_chan_spec *channels;
179-
const char * const *regval_to_sample_freq;
180+
const int (*regval_to_samp_freq)[2];
180181
const int *regval_to_input_field_mga;
181182
const int *regval_to_nanoscale;
182183
};
@@ -370,17 +371,17 @@ static IIO_DEVICE_ATTR(meas_conf,
370371
hmc5843_set_measurement_configuration,
371372
0);
372373

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)
376376
{
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));
379378
ssize_t total_n = 0;
380379
int i;
381380

382381
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]);
384385
buf += n;
385386
total_n += n;
386387
}
@@ -390,87 +391,30 @@ static ssize_t hmc5843_show_sampling_frequencies_available(struct device *dev,
390391
return total_n;
391392
}
392393

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);
394395

395396
static s32 hmc5843_set_rate(struct hmc5843_data *data, u8 rate)
396397
{
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);
404399

405-
reg_val = data->meas_conf | (rate << HMC5843_RATE_OFFSET);
406400
return i2c_smbus_write_byte_data(data->client, HMC5843_CONFIG_REG_A,
407401
reg_val);
408402
}
409403

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)
412406
{
413-
const char * const *samp_freq = data->variant->regval_to_sample_freq;
414407
int i;
415408

416409
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])
418412
return i;
419413
}
420414

421415
return -EINVAL;
422416
}
423417

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-
474418
static ssize_t hmc5843_show_range_gain(struct device *dev,
475419
struct device_attribute *attr,
476420
char *buf)
@@ -525,8 +469,7 @@ static IIO_DEVICE_ATTR(in_magn_range,
525469

526470
static int hmc5843_read_raw(struct iio_dev *indio_dev,
527471
struct iio_chan_spec const *chan,
528-
int *val, int *val2,
529-
long mask)
472+
int *val, int *val2, long mask)
530473
{
531474
struct hmc5843_data *data = iio_priv(indio_dev);
532475

@@ -537,17 +480,47 @@ static int hmc5843_read_raw(struct iio_dev *indio_dev,
537480
*val = 0;
538481
*val2 = data->variant->regval_to_nanoscale[data->range];
539482
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;
540487
}
541488
return -EINVAL;
542489
}
543490

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+
544516
#define HMC5843_CHANNEL(axis, addr) \
545517
{ \
546518
.type = IIO_MAGN, \
547519
.modified = 1, \
548520
.channel2 = IIO_MOD_##axis, \
549521
.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), \
551524
.address = addr \
552525
}
553526

@@ -566,7 +539,6 @@ static const struct iio_chan_spec hmc5883_channels[] = {
566539
static struct attribute *hmc5843_attributes[] = {
567540
&iio_dev_attr_meas_conf.dev_attr.attr,
568541
&iio_dev_attr_operating_mode.dev_attr.attr,
569-
&iio_dev_attr_sampling_frequency.dev_attr.attr,
570542
&iio_dev_attr_in_magn_range.dev_attr.attr,
571543
&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
572544
NULL
@@ -579,21 +551,21 @@ static const struct attribute_group hmc5843_group = {
579551
static const struct hmc5843_chip_info hmc5843_chip_info_tbl[] = {
580552
[HMC5843_ID] = {
581553
.channels = hmc5843_channels,
582-
.regval_to_sample_freq = hmc5843_regval_to_sample_freq,
554+
.regval_to_samp_freq = hmc5843_regval_to_samp_freq,
583555
.regval_to_input_field_mga =
584556
hmc5843_regval_to_input_field_mga,
585557
.regval_to_nanoscale = hmc5843_regval_to_nanoscale,
586558
},
587559
[HMC5883_ID] = {
588560
.channels = hmc5883_channels,
589-
.regval_to_sample_freq = hmc5883_regval_to_sample_freq,
561+
.regval_to_samp_freq = hmc5883_regval_to_samp_freq,
590562
.regval_to_input_field_mga =
591563
hmc5883_regval_to_input_field_mga,
592564
.regval_to_nanoscale = hmc5883_regval_to_nanoscale,
593565
},
594566
[HMC5883L_ID] = {
595567
.channels = hmc5883_channels,
596-
.regval_to_sample_freq = hmc5883_regval_to_sample_freq,
568+
.regval_to_samp_freq = hmc5883_regval_to_samp_freq,
597569
.regval_to_input_field_mga =
598570
hmc5883l_regval_to_input_field_mga,
599571
.regval_to_nanoscale = hmc5883l_regval_to_nanoscale,
@@ -612,6 +584,7 @@ static void hmc5843_init(struct hmc5843_data *data)
612584
static const struct iio_info hmc5843_info = {
613585
.attrs = &hmc5843_group,
614586
.read_raw = &hmc5843_read_raw,
587+
.write_raw = &hmc5843_write_raw,
615588
.driver_module = THIS_MODULE,
616589
};
617590

0 commit comments

Comments
 (0)