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

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

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