On Tue, Nov 06, 2007 at 02:33:53AM -0800, akpm@linux-foundation.org wrote:
[mmotm.git] / drivers / staging / iio / trigger / iio-trig-periodic-rtc.c
blobe310dc0098559c424d5100d03401109d2976d21c
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>
18 #include "../iio.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;
26 int frequency;
27 char *name;
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 ssize_t iio_trig_periodic_read_name(struct device *dev,
76 struct device_attribute *attr,
77 char *buf)
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,
86 NULL);
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,
93 &dev_attr_name.attr,
94 NULL,
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;
111 int i, ret;
113 for (i = 0;; i++) {
114 if (pdata[i] == NULL)
115 break;
116 trig = iio_allocate_trigger();
117 if (!trig) {
118 ret = -ENOMEM;
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);
124 if (!trig_info) {
125 ret = -ENOMEM;
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) {
133 ret = -ENOMEM;
134 goto error_free_trig_info;
136 snprintf((char *)trig->name,
137 IIO_TRIGGER_NAME_LENGTH,
138 "periodic%s",
139 pdata[i]);
140 trig_info->name = (char *)trig->name;
141 /* RTC access */
142 trig_info->rtc
143 = rtc_class_open(pdata[i]);
144 if (trig_info->rtc == NULL) {
145 ret = -EINVAL;
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);
151 if (ret)
152 goto error_close_rtc;
153 trig->control_attrs = &iio_trig_prtc_attr_group;
154 ret = iio_trigger_register(trig);
155 if (ret)
156 goto error_unregister_rtc_irq;
158 return 0;
159 error_unregister_rtc_irq:
160 rtc_irq_unregister(trig_info->rtc, &trig_info->task);
161 error_close_rtc:
162 rtc_class_close(trig_info->rtc);
163 error_free_name:
164 kfree(trig->name);
165 error_free_trig_info:
166 kfree(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,
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->name);
179 kfree(trig_info);
180 iio_trigger_unregister(trig);
182 return ret;
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,
191 trig2,
192 &iio_prtc_trigger_list,
193 alloc_list) {
194 trig_info = trig->private_data;
195 rtc_irq_unregister(trig_info->rtc, &trig_info->task);
196 rtc_class_close(trig_info->rtc);
197 kfree(trig->name);
198 kfree(trig_info);
199 iio_trigger_unregister(trig);
201 mutex_unlock(&iio_prtc_trigger_list_lock);
202 return 0;
205 static struct platform_driver iio_trig_periodic_rtc_driver = {
206 .probe = iio_trig_periodic_rtc_probe,
207 .remove = iio_trig_periodic_rtc_remove,
208 .driver = {
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");