Version:  2.0.40 2.2.26 2.4.37 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 3.18 3.19

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

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