Version:  2.0.40 2.2.26 2.4.37 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17

Linux/drivers/staging/iio/trigger/iio-trig-periodic-rtc.c

  1 /* The industrial I/O periodic RTC trigger driver
  2  *
  3  * Copyright (c) 2008 Jonathan Cameron
  4  *
  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.
  8  *
  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.
 12  */
 13 
 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 <linux/iio/iio.h>
 20 #include <linux/iio/trigger.h>
 21 
 22 static LIST_HEAD(iio_prtc_trigger_list);
 23 static DEFINE_MUTEX(iio_prtc_trigger_list_lock);
 24 
 25 struct iio_prtc_trigger_info {
 26         struct rtc_device *rtc;
 27         int frequency;
 28         struct rtc_task task;
 29         bool state;
 30 };
 31 
 32 static int iio_trig_periodic_rtc_set_state(struct iio_trigger *trig, bool state)
 33 {
 34         struct iio_prtc_trigger_info *trig_info = iio_trigger_get_drvdata(trig);
 35         int ret;
 36         if (trig_info->frequency == 0 && state)
 37                 return -EINVAL;
 38         dev_dbg(&trig_info->rtc->dev, "trigger frequency is %d\n",
 39                         trig_info->frequency);
 40         ret = rtc_irq_set_state(trig_info->rtc, &trig_info->task, state);
 41         if (ret == 0)
 42                 trig_info->state = state;
 43 
 44         return ret;
 45 }
 46 
 47 static ssize_t iio_trig_periodic_read_freq(struct device *dev,
 48                                            struct device_attribute *attr,
 49                                            char *buf)
 50 {
 51         struct iio_trigger *trig = to_iio_trigger(dev);
 52         struct iio_prtc_trigger_info *trig_info = iio_trigger_get_drvdata(trig);
 53         return sprintf(buf, "%u\n", trig_info->frequency);
 54 }
 55 
 56 static ssize_t iio_trig_periodic_write_freq(struct device *dev,
 57                                             struct device_attribute *attr,
 58                                             const char *buf,
 59                                             size_t len)
 60 {
 61         struct iio_trigger *trig = to_iio_trigger(dev);
 62         struct iio_prtc_trigger_info *trig_info = iio_trigger_get_drvdata(trig);
 63         int val;
 64         int ret;
 65 
 66         ret = kstrtoint(buf, 10, &val);
 67         if (ret)
 68                 goto error_ret;
 69 
 70         if (val > 0) {
 71                 ret = rtc_irq_set_freq(trig_info->rtc, &trig_info->task, val);
 72                 if (ret == 0 && trig_info->state && trig_info->frequency == 0)
 73                         ret = rtc_irq_set_state(trig_info->rtc, &trig_info->task, 1);
 74         } else if (val == 0) {
 75                 ret = rtc_irq_set_state(trig_info->rtc, &trig_info->task, 0);
 76         } else
 77                 ret = -EINVAL;
 78         if (ret)
 79                 goto error_ret;
 80 
 81         trig_info->frequency = val;
 82 
 83         return len;
 84 
 85 error_ret:
 86         return ret;
 87 }
 88 
 89 static DEVICE_ATTR(frequency, S_IRUGO | S_IWUSR,
 90             iio_trig_periodic_read_freq,
 91             iio_trig_periodic_write_freq);
 92 
 93 static struct attribute *iio_trig_prtc_attrs[] = {
 94         &dev_attr_frequency.attr,
 95         NULL,
 96 };
 97 
 98 static const struct attribute_group iio_trig_prtc_attr_group = {
 99         .attrs = iio_trig_prtc_attrs,
100 };
101 
102 static const struct attribute_group *iio_trig_prtc_attr_groups[] = {
103         &iio_trig_prtc_attr_group,
104         NULL
105 };
106 
107 static void iio_prtc_trigger_poll(void *private_data)
108 {
109         iio_trigger_poll(private_data);
110 }
111 
112 static const struct iio_trigger_ops iio_prtc_trigger_ops = {
113         .owner = THIS_MODULE,
114         .set_trigger_state = &iio_trig_periodic_rtc_set_state,
115 };
116 
117 static int iio_trig_periodic_rtc_probe(struct platform_device *dev)
118 {
119         char **pdata = dev->dev.platform_data;
120         struct iio_prtc_trigger_info *trig_info;
121         struct iio_trigger *trig, *trig2;
122 
123         int i, ret;
124 
125         for (i = 0;; i++) {
126                 if (pdata[i] == NULL)
127                         break;
128                 trig = iio_trigger_alloc("periodic%s", pdata[i]);
129                 if (!trig) {
130                         ret = -ENOMEM;
131                         goto error_free_completed_registrations;
132                 }
133                 list_add(&trig->alloc_list, &iio_prtc_trigger_list);
134 
135                 trig_info = kzalloc(sizeof(*trig_info), GFP_KERNEL);
136                 if (!trig_info) {
137                         ret = -ENOMEM;
138                         goto error_put_trigger_and_remove_from_list;
139                 }
140                 iio_trigger_set_drvdata(trig, trig_info);
141                 trig->ops = &iio_prtc_trigger_ops;
142                 /* RTC access */
143                 trig_info->rtc = rtc_class_open(pdata[i]);
144                 if (trig_info->rtc == NULL) {
145                         ret = -EINVAL;
146                         goto error_free_trig_info;
147                 }
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->dev.groups = iio_trig_prtc_attr_groups;
154                 ret = iio_trigger_register(trig);
155                 if (ret)
156                         goto error_unregister_rtc_irq;
157         }
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_trig_info:
164         kfree(trig_info);
165 error_put_trigger_and_remove_from_list:
166         list_del(&trig->alloc_list);
167         iio_trigger_put(trig);
168 error_free_completed_registrations:
169         list_for_each_entry_safe(trig,
170                                  trig2,
171                                  &iio_prtc_trigger_list,
172                                  alloc_list) {
173                 trig_info = iio_trigger_get_drvdata(trig);
174                 rtc_irq_unregister(trig_info->rtc, &trig_info->task);
175                 rtc_class_close(trig_info->rtc);
176                 kfree(trig_info);
177                 iio_trigger_unregister(trig);
178         }
179         return ret;
180 }
181 
182 static int iio_trig_periodic_rtc_remove(struct platform_device *dev)
183 {
184         struct iio_trigger *trig, *trig2;
185         struct iio_prtc_trigger_info *trig_info;
186         mutex_lock(&iio_prtc_trigger_list_lock);
187         list_for_each_entry_safe(trig,
188                                  trig2,
189                                  &iio_prtc_trigger_list,
190                                  alloc_list) {
191                 trig_info = iio_trigger_get_drvdata(trig);
192                 rtc_irq_unregister(trig_info->rtc, &trig_info->task);
193                 rtc_class_close(trig_info->rtc);
194                 kfree(trig_info);
195                 iio_trigger_unregister(trig);
196         }
197         mutex_unlock(&iio_prtc_trigger_list_lock);
198         return 0;
199 }
200 
201 static struct platform_driver iio_trig_periodic_rtc_driver = {
202         .probe = iio_trig_periodic_rtc_probe,
203         .remove = iio_trig_periodic_rtc_remove,
204         .driver = {
205                 .name = "iio_prtc_trigger",
206                 .owner = THIS_MODULE,
207         },
208 };
209 
210 module_platform_driver(iio_trig_periodic_rtc_driver);
211 
212 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
213 MODULE_DESCRIPTION("Periodic realtime clock trigger for the iio subsystem");
214 MODULE_LICENSE("GPL v2");
215 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us