Skip to content

Commit b9ee3d4

Browse files
dlechProject516
authored andcommitted
iio: trigger: add in-kernel function for setting hrtimer sampling frequency
1 parent ce2e3a6 commit b9ee3d4

File tree

2 files changed

+50
-12
lines changed

2 files changed

+50
-12
lines changed

drivers/iio/trigger/iio-trig-hrtimer.c

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,50 @@ static const struct config_item_type iio_hrtimer_type = {
3333
.ct_owner = THIS_MODULE,
3434
};
3535

36+
static struct iio_sw_trigger_type iio_trig_hrtimer;
37+
38+
static int __iio_hrtimer_set_sampling_frequency(struct iio_hrtimer_info *info,
39+
int integer, int fract)
40+
{
41+
u64 val, period;
42+
43+
val = fract + 1000ULL * integer; /* mHz */
44+
45+
if (!val || val > UINT_MAX)
46+
return -EINVAL;
47+
48+
info->sampling_frequency[0] = integer; /* Hz */
49+
info->sampling_frequency[1] = fract * 1000; /* uHz */
50+
period = PSEC_PER_SEC;
51+
do_div(period, val);
52+
info->period = period; /* nS */
53+
54+
return 0;
55+
}
56+
57+
/**
58+
* iio_hrtimer_set_sampling_frequency - set the sampling frequency
59+
* @sw_trigger: a hrtimer trigger
60+
* @freq: the sampling rate in Hz
61+
*
62+
* Returns 0 on success or -EINVAL if the trigger is not an hrtimer trigger
63+
* or the frequency is out of range.
64+
*/
65+
int iio_hrtimer_set_sampling_frequency(struct iio_sw_trigger *sw_trigger,
66+
unsigned long freq)
67+
{
68+
struct iio_hrtimer_info *info;
69+
70+
71+
if (sw_trigger->trigger_type != &iio_trig_hrtimer)
72+
return -EINVAL;
73+
74+
info = iio_trigger_get_drvdata(sw_trigger->trigger);
75+
76+
return __iio_hrtimer_set_sampling_frequency(info, freq, 0);
77+
}
78+
EXPORT_SYMBOL_GPL(iio_hrtimer_set_sampling_frequency);
79+
3680
static
3781
ssize_t iio_hrtimer_show_sampling_frequency(struct device *dev,
3882
struct device_attribute *attr,
@@ -53,8 +97,6 @@ ssize_t iio_hrtimer_store_sampling_frequency(struct device *dev,
5397
{
5498
struct iio_trigger *trig = to_iio_trigger(dev);
5599
struct iio_hrtimer_info *info = iio_trigger_get_drvdata(trig);
56-
unsigned long long val;
57-
u64 period;
58100
int integer, fract, ret;
59101

60102
ret = iio_str_to_fixpoint(buf, 100, &integer, &fract);
@@ -63,16 +105,9 @@ ssize_t iio_hrtimer_store_sampling_frequency(struct device *dev,
63105
if (integer < 0 || fract < 0)
64106
return -ERANGE;
65107

66-
val = fract + 1000ULL * integer; /* mHz */
67-
68-
if (!val || val > UINT_MAX)
69-
return -EINVAL;
70-
71-
info->sampling_frequency[0] = integer; /* Hz */
72-
info->sampling_frequency[1] = fract * 1000; /* uHz */
73-
period = PSEC_PER_SEC;
74-
do_div(period, val);
75-
info->period = period; /* nS */
108+
ret = __iio_hrtimer_set_sampling_frequency(info, integer, fract);
109+
if (ret)
110+
return ret;
76111

77112
return len;
78113
}

include/linux/iio/sw_trigger.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,7 @@ void iio_swt_group_init_type_name(struct iio_sw_trigger *t,
6161
#endif
6262
}
6363

64+
int iio_hrtimer_set_sampling_frequency(struct iio_sw_trigger *sw_trigger,
65+
unsigned long freq);
66+
6467
#endif /* __IIO_SW_TRIGGER */

0 commit comments

Comments
 (0)