1 /* The industrial I/O periodic RTC trigger driver
3 * Copyright (c) 2008 Jonathan Cameron
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * This is a heavily rewritten version of the periodic timer system in
10 * earlier version of industrialio. It supplies the same functionality
11 * but via a trigger rather than a specific periodic timer system.
14 #include <linux/platform_device.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/rtc.h>
20 #include "../trigger.h"
22 static LIST_HEAD(iio_prtc_trigger_list
);
23 static DEFINE_MUTEX(iio_prtc_trigger_list_lock
);
25 struct iio_prtc_trigger_info
{
26 struct rtc_device
*rtc
;
31 static int iio_trig_periodic_rtc_set_state(struct iio_trigger
*trig
, bool state
)
33 struct iio_prtc_trigger_info
*trig_info
= trig
->private_data
;
34 if (trig_info
->frequency
== 0)
36 printk(KERN_INFO
"trigger frequency is %d\n", trig_info
->frequency
);
37 return rtc_irq_set_state(trig_info
->rtc
, &trig_info
->task
, state
);
40 static ssize_t
iio_trig_periodic_read_freq(struct device
*dev
,
41 struct device_attribute
*attr
,
44 struct iio_trigger
*trig
= dev_get_drvdata(dev
);
45 struct iio_prtc_trigger_info
*trig_info
= trig
->private_data
;
46 return sprintf(buf
, "%u\n", trig_info
->frequency
);
49 static ssize_t
iio_trig_periodic_write_freq(struct device
*dev
,
50 struct device_attribute
*attr
,
54 struct iio_trigger
*trig
= dev_get_drvdata(dev
);
55 struct iio_prtc_trigger_info
*trig_info
= trig
->private_data
;
59 ret
= strict_strtoul(buf
, 10, &val
);
63 ret
= rtc_irq_set_freq(trig_info
->rtc
, &trig_info
->task
, val
);
67 trig_info
->frequency
= val
;
75 static DEVICE_ATTR(frequency
, S_IRUGO
| S_IWUSR
,
76 iio_trig_periodic_read_freq
,
77 iio_trig_periodic_write_freq
);
79 static struct attribute
*iio_trig_prtc_attrs
[] = {
80 &dev_attr_frequency
.attr
,
84 static const struct attribute_group iio_trig_prtc_attr_group
= {
85 .attrs
= iio_trig_prtc_attrs
,
88 static const struct attribute_group
*iio_trig_prtc_attr_groups
[] = {
89 &iio_trig_prtc_attr_group
,
93 static void iio_prtc_trigger_poll(void *private_data
)
95 /* Timestamp is not provided currently */
96 iio_trigger_poll(private_data
, 0);
99 static const struct iio_trigger_ops iio_prtc_trigger_ops
= {
100 .owner
= THIS_MODULE
,
101 .set_trigger_state
= &iio_trig_periodic_rtc_set_state
,
104 static int iio_trig_periodic_rtc_probe(struct platform_device
*dev
)
106 char **pdata
= dev
->dev
.platform_data
;
107 struct iio_prtc_trigger_info
*trig_info
;
108 struct iio_trigger
*trig
, *trig2
;
113 if (pdata
[i
] == NULL
)
115 trig
= iio_allocate_trigger("periodic%s", pdata
[i
]);
118 goto error_free_completed_registrations
;
120 list_add(&trig
->alloc_list
, &iio_prtc_trigger_list
);
122 trig_info
= kzalloc(sizeof(*trig_info
), GFP_KERNEL
);
125 goto error_put_trigger_and_remove_from_list
;
127 trig
->private_data
= trig_info
;
128 trig
->owner
= THIS_MODULE
;
129 trig
->ops
= &iio_prtc_trigger_ops
;
132 = rtc_class_open(pdata
[i
]);
133 if (trig_info
->rtc
== NULL
) {
135 goto error_free_trig_info
;
137 trig_info
->task
.func
= iio_prtc_trigger_poll
;
138 trig_info
->task
.private_data
= trig
;
139 ret
= rtc_irq_register(trig_info
->rtc
, &trig_info
->task
);
141 goto error_close_rtc
;
142 trig
->dev
.groups
= iio_trig_prtc_attr_groups
;
143 ret
= iio_trigger_register(trig
);
145 goto error_unregister_rtc_irq
;
148 error_unregister_rtc_irq
:
149 rtc_irq_unregister(trig_info
->rtc
, &trig_info
->task
);
151 rtc_class_close(trig_info
->rtc
);
152 error_free_trig_info
:
154 error_put_trigger_and_remove_from_list
:
155 list_del(&trig
->alloc_list
);
156 iio_put_trigger(trig
);
157 error_free_completed_registrations
:
158 list_for_each_entry_safe(trig
,
160 &iio_prtc_trigger_list
,
162 trig_info
= trig
->private_data
;
163 rtc_irq_unregister(trig_info
->rtc
, &trig_info
->task
);
164 rtc_class_close(trig_info
->rtc
);
166 iio_trigger_unregister(trig
);
171 static int iio_trig_periodic_rtc_remove(struct platform_device
*dev
)
173 struct iio_trigger
*trig
, *trig2
;
174 struct iio_prtc_trigger_info
*trig_info
;
175 mutex_lock(&iio_prtc_trigger_list_lock
);
176 list_for_each_entry_safe(trig
,
178 &iio_prtc_trigger_list
,
180 trig_info
= trig
->private_data
;
181 rtc_irq_unregister(trig_info
->rtc
, &trig_info
->task
);
182 rtc_class_close(trig_info
->rtc
);
184 iio_trigger_unregister(trig
);
186 mutex_unlock(&iio_prtc_trigger_list_lock
);
190 static struct platform_driver iio_trig_periodic_rtc_driver
= {
191 .probe
= iio_trig_periodic_rtc_probe
,
192 .remove
= iio_trig_periodic_rtc_remove
,
194 .name
= "iio_prtc_trigger",
195 .owner
= THIS_MODULE
,
199 static int __init
iio_trig_periodic_rtc_init(void)
201 return platform_driver_register(&iio_trig_periodic_rtc_driver
);
204 static void __exit
iio_trig_periodic_rtc_exit(void)
206 return platform_driver_unregister(&iio_trig_periodic_rtc_driver
);
209 module_init(iio_trig_periodic_rtc_init
);
210 module_exit(iio_trig_periodic_rtc_exit
);
211 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
212 MODULE_DESCRIPTION("Periodic realtime clock trigger for the iio subsystem");
213 MODULE_LICENSE("GPL v2");