staging:iio:trigger handle name attr in core, remove old alloc and register any contr...
[pohmelfs.git] / drivers / staging / iio / trigger / iio-trig-periodic-rtc.c
blob01cf7e20b515bf45922e306d263070aedc695444
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>
19 #include "../iio.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;
27 int frequency;
28 struct rtc_task task;
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)
35 return -EINVAL;
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,
42 char *buf)
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,
51 const char *buf,
52 size_t len)
54 struct iio_trigger *trig = dev_get_drvdata(dev);
55 struct iio_prtc_trigger_info *trig_info = trig->private_data;
56 unsigned long val;
57 int ret;
59 ret = strict_strtoul(buf, 10, &val);
60 if (ret)
61 goto error_ret;
63 ret = rtc_irq_set_freq(trig_info->rtc, &trig_info->task, val);
64 if (ret)
65 goto error_ret;
67 trig_info->frequency = val;
69 return len;
71 error_ret:
72 return ret;
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,
81 NULL,
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,
90 NULL
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 int iio_trig_periodic_rtc_probe(struct platform_device *dev)
101 char **pdata = dev->dev.platform_data;
102 struct iio_prtc_trigger_info *trig_info;
103 struct iio_trigger *trig, *trig2;
105 int i, ret;
107 for (i = 0;; i++) {
108 if (pdata[i] == NULL)
109 break;
110 trig = iio_allocate_trigger("periodic%s", pdata[i]);
111 if (!trig) {
112 ret = -ENOMEM;
113 goto error_free_completed_registrations;
115 list_add(&trig->alloc_list, &iio_prtc_trigger_list);
117 trig_info = kzalloc(sizeof(*trig_info), GFP_KERNEL);
118 if (!trig_info) {
119 ret = -ENOMEM;
120 goto error_put_trigger_and_remove_from_list;
122 trig->private_data = trig_info;
123 trig->owner = THIS_MODULE;
124 trig->set_trigger_state = &iio_trig_periodic_rtc_set_state;
125 /* RTC access */
126 trig_info->rtc
127 = rtc_class_open(pdata[i]);
128 if (trig_info->rtc == NULL) {
129 ret = -EINVAL;
130 goto error_free_trig_info;
132 trig_info->task.func = iio_prtc_trigger_poll;
133 trig_info->task.private_data = trig;
134 ret = rtc_irq_register(trig_info->rtc, &trig_info->task);
135 if (ret)
136 goto error_close_rtc;
137 trig->dev.groups = iio_trig_prtc_attr_groups;
138 ret = iio_trigger_register(trig);
139 if (ret)
140 goto error_unregister_rtc_irq;
142 return 0;
143 error_unregister_rtc_irq:
144 rtc_irq_unregister(trig_info->rtc, &trig_info->task);
145 error_close_rtc:
146 rtc_class_close(trig_info->rtc);
147 error_free_trig_info:
148 kfree(trig_info);
149 error_put_trigger_and_remove_from_list:
150 list_del(&trig->alloc_list);
151 iio_put_trigger(trig);
152 error_free_completed_registrations:
153 list_for_each_entry_safe(trig,
154 trig2,
155 &iio_prtc_trigger_list,
156 alloc_list) {
157 trig_info = trig->private_data;
158 rtc_irq_unregister(trig_info->rtc, &trig_info->task);
159 rtc_class_close(trig_info->rtc);
160 kfree(trig_info);
161 iio_trigger_unregister(trig);
163 return ret;
166 static int iio_trig_periodic_rtc_remove(struct platform_device *dev)
168 struct iio_trigger *trig, *trig2;
169 struct iio_prtc_trigger_info *trig_info;
170 mutex_lock(&iio_prtc_trigger_list_lock);
171 list_for_each_entry_safe(trig,
172 trig2,
173 &iio_prtc_trigger_list,
174 alloc_list) {
175 trig_info = trig->private_data;
176 rtc_irq_unregister(trig_info->rtc, &trig_info->task);
177 rtc_class_close(trig_info->rtc);
178 kfree(trig_info);
179 iio_trigger_unregister(trig);
181 mutex_unlock(&iio_prtc_trigger_list_lock);
182 return 0;
185 static struct platform_driver iio_trig_periodic_rtc_driver = {
186 .probe = iio_trig_periodic_rtc_probe,
187 .remove = iio_trig_periodic_rtc_remove,
188 .driver = {
189 .name = "iio_prtc_trigger",
190 .owner = THIS_MODULE,
194 static int __init iio_trig_periodic_rtc_init(void)
196 return platform_driver_register(&iio_trig_periodic_rtc_driver);
199 static void __exit iio_trig_periodic_rtc_exit(void)
201 return platform_driver_unregister(&iio_trig_periodic_rtc_driver);
204 module_init(iio_trig_periodic_rtc_init);
205 module_exit(iio_trig_periodic_rtc_exit);
206 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
207 MODULE_DESCRIPTION("Periodic realtime clock trigger for the iio subsystem");
208 MODULE_LICENSE("GPL v2");