0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/kernel.h>
0012 #include <linux/slab.h>
0013 #include <linux/hrtimer.h>
0014
0015 #include <linux/iio/iio.h>
0016 #include <linux/iio/trigger.h>
0017 #include <linux/iio/sw_trigger.h>
0018
0019
0020 #define PSEC_PER_SEC 1000000000000LL
0021
0022
0023 #define HRTIMER_DEFAULT_SAMPLING_FREQUENCY 100
0024
0025 struct iio_hrtimer_info {
0026 struct iio_sw_trigger swt;
0027 struct hrtimer timer;
0028 int sampling_frequency[2];
0029 ktime_t period;
0030 };
0031
0032 static const struct config_item_type iio_hrtimer_type = {
0033 .ct_owner = THIS_MODULE,
0034 };
0035
0036 static
0037 ssize_t iio_hrtimer_show_sampling_frequency(struct device *dev,
0038 struct device_attribute *attr,
0039 char *buf)
0040 {
0041 struct iio_trigger *trig = to_iio_trigger(dev);
0042 struct iio_hrtimer_info *info = iio_trigger_get_drvdata(trig);
0043
0044 return iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO,
0045 ARRAY_SIZE(info->sampling_frequency),
0046 info->sampling_frequency);
0047 }
0048
0049 static
0050 ssize_t iio_hrtimer_store_sampling_frequency(struct device *dev,
0051 struct device_attribute *attr,
0052 const char *buf, size_t len)
0053 {
0054 struct iio_trigger *trig = to_iio_trigger(dev);
0055 struct iio_hrtimer_info *info = iio_trigger_get_drvdata(trig);
0056 unsigned long long val;
0057 u64 period;
0058 int integer, fract, ret;
0059
0060 ret = iio_str_to_fixpoint(buf, 100, &integer, &fract);
0061 if (ret)
0062 return ret;
0063 if (integer < 0 || fract < 0)
0064 return -ERANGE;
0065
0066 val = fract + 1000ULL * integer;
0067
0068 if (!val || val > UINT_MAX)
0069 return -EINVAL;
0070
0071 info->sampling_frequency[0] = integer;
0072 info->sampling_frequency[1] = fract * 1000;
0073 period = PSEC_PER_SEC;
0074 do_div(period, val);
0075 info->period = period;
0076
0077 return len;
0078 }
0079
0080 static DEVICE_ATTR(sampling_frequency, S_IRUGO | S_IWUSR,
0081 iio_hrtimer_show_sampling_frequency,
0082 iio_hrtimer_store_sampling_frequency);
0083
0084 static struct attribute *iio_hrtimer_attrs[] = {
0085 &dev_attr_sampling_frequency.attr,
0086 NULL
0087 };
0088
0089 static const struct attribute_group iio_hrtimer_attr_group = {
0090 .attrs = iio_hrtimer_attrs,
0091 };
0092
0093 static const struct attribute_group *iio_hrtimer_attr_groups[] = {
0094 &iio_hrtimer_attr_group,
0095 NULL
0096 };
0097
0098 static enum hrtimer_restart iio_hrtimer_trig_handler(struct hrtimer *timer)
0099 {
0100 struct iio_hrtimer_info *info;
0101
0102 info = container_of(timer, struct iio_hrtimer_info, timer);
0103
0104 hrtimer_forward_now(timer, info->period);
0105 iio_trigger_poll(info->swt.trigger);
0106
0107 return HRTIMER_RESTART;
0108 }
0109
0110 static int iio_trig_hrtimer_set_state(struct iio_trigger *trig, bool state)
0111 {
0112 struct iio_hrtimer_info *trig_info;
0113
0114 trig_info = iio_trigger_get_drvdata(trig);
0115
0116 if (state)
0117 hrtimer_start(&trig_info->timer, trig_info->period,
0118 HRTIMER_MODE_REL_HARD);
0119 else
0120 hrtimer_cancel(&trig_info->timer);
0121
0122 return 0;
0123 }
0124
0125 static const struct iio_trigger_ops iio_hrtimer_trigger_ops = {
0126 .set_trigger_state = iio_trig_hrtimer_set_state,
0127 };
0128
0129 static struct iio_sw_trigger *iio_trig_hrtimer_probe(const char *name)
0130 {
0131 struct iio_hrtimer_info *trig_info;
0132 int ret;
0133
0134 trig_info = kzalloc(sizeof(*trig_info), GFP_KERNEL);
0135 if (!trig_info)
0136 return ERR_PTR(-ENOMEM);
0137
0138 trig_info->swt.trigger = iio_trigger_alloc(NULL, "%s", name);
0139 if (!trig_info->swt.trigger) {
0140 ret = -ENOMEM;
0141 goto err_free_trig_info;
0142 }
0143
0144 iio_trigger_set_drvdata(trig_info->swt.trigger, trig_info);
0145 trig_info->swt.trigger->ops = &iio_hrtimer_trigger_ops;
0146 trig_info->swt.trigger->dev.groups = iio_hrtimer_attr_groups;
0147
0148 hrtimer_init(&trig_info->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
0149 trig_info->timer.function = iio_hrtimer_trig_handler;
0150
0151 trig_info->sampling_frequency[0] = HRTIMER_DEFAULT_SAMPLING_FREQUENCY;
0152 trig_info->period = NSEC_PER_SEC / trig_info->sampling_frequency[0];
0153
0154 ret = iio_trigger_register(trig_info->swt.trigger);
0155 if (ret)
0156 goto err_free_trigger;
0157
0158 iio_swt_group_init_type_name(&trig_info->swt, name, &iio_hrtimer_type);
0159 return &trig_info->swt;
0160 err_free_trigger:
0161 iio_trigger_free(trig_info->swt.trigger);
0162 err_free_trig_info:
0163 kfree(trig_info);
0164
0165 return ERR_PTR(ret);
0166 }
0167
0168 static int iio_trig_hrtimer_remove(struct iio_sw_trigger *swt)
0169 {
0170 struct iio_hrtimer_info *trig_info;
0171
0172 trig_info = iio_trigger_get_drvdata(swt->trigger);
0173
0174 iio_trigger_unregister(swt->trigger);
0175
0176
0177 hrtimer_cancel(&trig_info->timer);
0178 iio_trigger_free(swt->trigger);
0179 kfree(trig_info);
0180
0181 return 0;
0182 }
0183
0184 static const struct iio_sw_trigger_ops iio_trig_hrtimer_ops = {
0185 .probe = iio_trig_hrtimer_probe,
0186 .remove = iio_trig_hrtimer_remove,
0187 };
0188
0189 static struct iio_sw_trigger_type iio_trig_hrtimer = {
0190 .name = "hrtimer",
0191 .owner = THIS_MODULE,
0192 .ops = &iio_trig_hrtimer_ops,
0193 };
0194
0195 module_iio_sw_trigger_driver(iio_trig_hrtimer);
0196
0197 MODULE_AUTHOR("Marten Svanfeldt <marten@intuitiveaerial.com>");
0198 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
0199 MODULE_DESCRIPTION("Periodic hrtimer trigger for the IIO subsystem");
0200 MODULE_LICENSE("GPL v2");