Skip to content

Commit dd28e48

Browse files
pmeerwjic23
authored andcommitted
staging:iio:hmc5843: Introduce helper functions to show/check list of int pairs
Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent 9728fe3 commit dd28e48

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

drivers/staging/iio/magnetometer/hmc5843.c

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -356,24 +356,42 @@ static IIO_DEVICE_ATTR(meas_conf,
356356
hmc5843_set_measurement_configuration,
357357
0);
358358

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)
361361
{
362-
struct hmc5843_data *data = iio_priv(dev_to_iio_dev(dev));
363-
ssize_t total_n = 0;
362+
size_t len = 0;
364363
int i;
365364

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+
373369
/* replace trailing space by newline */
374-
buf[-1] = '\n';
370+
buf[len - 1] = '\n';
371+
372+
return len;
373+
}
375374

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);
377395
}
378396

379397
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)
389407
static int hmc5843_check_samp_freq(struct hmc5843_data *data,
390408
int val, int val2)
391409
{
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);
401413
}
402414

403415
static ssize_t hmc5843_show_range_gain(struct device *dev,

0 commit comments

Comments
 (0)