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/rtc.h>
19 #include "../trigger.h"
21 LIST_HEAD(iio_prtc_trigger_list
);
22 DEFINE_MUTEX(iio_prtc_trigger_list_lock
);
24 struct iio_prtc_trigger_info
{
25 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 ssize_t
iio_trig_periodic_read_name(struct device
*dev
,
76 struct device_attribute
*attr
,
79 struct iio_trigger
*trig
= dev_get_drvdata(dev
);
80 struct iio_prtc_trigger_info
*trig_info
= trig
->private_data
;
81 return sprintf(buf
, "%s\n", trig_info
->name
);
84 static DEVICE_ATTR(name
, S_IRUGO
,
85 iio_trig_periodic_read_name
,
87 static DEVICE_ATTR(frequency
, S_IRUGO
| S_IWUSR
,
88 iio_trig_periodic_read_freq
,
89 iio_trig_periodic_write_freq
);
91 static struct attribute
*iio_trig_prtc_attrs
[] = {
92 &dev_attr_frequency
.attr
,
96 static const struct attribute_group iio_trig_prtc_attr_group
= {
97 .attrs
= iio_trig_prtc_attrs
,
100 static void iio_prtc_trigger_poll(void *private_data
)
102 iio_trigger_poll(private_data
);
105 static int iio_trig_periodic_rtc_probe(struct platform_device
*dev
)
107 char **pdata
= dev
->dev
.platform_data
;
108 struct iio_prtc_trigger_info
*trig_info
;
109 struct iio_trigger
*trig
, *trig2
;
114 if (pdata
[i
] == NULL
)
116 trig
= iio_allocate_trigger();
119 goto error_free_completed_registrations
;
121 list_add(&trig
->alloc_list
, &iio_prtc_trigger_list
);
123 trig_info
= kzalloc(sizeof(*trig_info
), GFP_KERNEL
);
126 goto error_put_trigger_and_remove_from_list
;
128 trig
->private_data
= trig_info
;
129 trig
->owner
= THIS_MODULE
;
130 trig
->set_trigger_state
= &iio_trig_periodic_rtc_set_state
;
131 trig
->name
= kmalloc(IIO_TRIGGER_NAME_LENGTH
, GFP_KERNEL
);
132 if (trig
->name
== NULL
) {
134 goto error_free_trig_info
;
136 snprintf((char *)trig
->name
,
137 IIO_TRIGGER_NAME_LENGTH
,
140 trig_info
->name
= (char *)trig
->name
;
143 = rtc_class_open(pdata
[i
]);
144 if (trig_info
->rtc
== NULL
) {
146 goto error_free_name
;
148 trig_info
->task
.func
= iio_prtc_trigger_poll
;
149 trig_info
->task
.private_data
= trig
;
150 ret
= rtc_irq_register(trig_info
->rtc
, &trig_info
->task
);
152 goto error_close_rtc
;
153 trig
->control_attrs
= &iio_trig_prtc_attr_group
;
154 ret
= iio_trigger_register(trig
);
156 goto error_unregister_rtc_irq
;
159 error_unregister_rtc_irq
:
160 rtc_irq_unregister(trig_info
->rtc
, &trig_info
->task
);
162 rtc_class_close(trig_info
->rtc
);
165 error_free_trig_info
:
167 error_put_trigger_and_remove_from_list
:
168 list_del(&trig
->alloc_list
);
169 iio_put_trigger(trig
);
170 error_free_completed_registrations
:
171 list_for_each_entry_safe(trig
,
173 &iio_prtc_trigger_list
,
175 trig_info
= trig
->private_data
;
176 rtc_irq_unregister(trig_info
->rtc
, &trig_info
->task
);
177 rtc_class_close(trig_info
->rtc
);
180 iio_trigger_unregister(trig
);
185 static int iio_trig_periodic_rtc_remove(struct platform_device
*dev
)
187 struct iio_trigger
*trig
, *trig2
;
188 struct iio_prtc_trigger_info
*trig_info
;
189 mutex_lock(&iio_prtc_trigger_list_lock
);
190 list_for_each_entry_safe(trig
,
192 &iio_prtc_trigger_list
,
194 trig_info
= trig
->private_data
;
195 rtc_irq_unregister(trig_info
->rtc
, &trig_info
->task
);
196 rtc_class_close(trig_info
->rtc
);
199 iio_trigger_unregister(trig
);
201 mutex_unlock(&iio_prtc_trigger_list_lock
);
205 static struct platform_driver iio_trig_periodic_rtc_driver
= {
206 .probe
= iio_trig_periodic_rtc_probe
,
207 .remove
= iio_trig_periodic_rtc_remove
,
209 .name
= "iio_prtc_trigger",
210 .owner
= THIS_MODULE
,
214 static int __init
iio_trig_periodic_rtc_init(void)
216 return platform_driver_register(&iio_trig_periodic_rtc_driver
);
219 static void __exit
iio_trig_periodic_rtc_exit(void)
221 return platform_driver_unregister(&iio_trig_periodic_rtc_driver
);
224 module_init(iio_trig_periodic_rtc_init
);
225 module_exit(iio_trig_periodic_rtc_exit
);
226 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
227 MODULE_DESCRIPTION("Periodic realtime clock trigger for the iio subsystem");
228 MODULE_LICENSE("GPL v2");