staging:iio:trigger remove legacy pollfunc elements.
[zen-stable.git] / drivers / staging / iio / industrialio-trigger.c
blobdd762f05385072d116fa5e68f68cdb0de50d9ef1
1 /* The industrial I/O core, trigger handling functions
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.
8 */
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/idr.h>
13 #include <linux/err.h>
14 #include <linux/device.h>
15 #include <linux/interrupt.h>
16 #include <linux/list.h>
17 #include <linux/slab.h>
19 #include "iio.h"
20 #include "trigger.h"
21 #include "trigger_consumer.h"
23 /* RFC - Question of approach
24 * Make the common case (single sensor single trigger)
25 * simple by starting trigger capture from when first sensors
26 * is added.
28 * Complex simultaneous start requires use of 'hold' functionality
29 * of the trigger. (not implemented)
31 * Any other suggestions?
34 static DEFINE_IDR(iio_trigger_idr);
35 static DEFINE_SPINLOCK(iio_trigger_idr_lock);
37 /* Single list of all available triggers */
38 static LIST_HEAD(iio_trigger_list);
39 static DEFINE_MUTEX(iio_trigger_list_lock);
41 /**
42 * iio_trigger_register_sysfs() - create a device for this trigger
43 * @trig_info: the trigger
45 * Also adds any control attribute registered by the trigger driver
46 **/
47 static int iio_trigger_register_sysfs(struct iio_trigger *trig_info)
49 int ret = 0;
51 if (trig_info->control_attrs)
52 ret = sysfs_create_group(&trig_info->dev.kobj,
53 trig_info->control_attrs);
55 return ret;
58 static void iio_trigger_unregister_sysfs(struct iio_trigger *trig_info)
60 if (trig_info->control_attrs)
61 sysfs_remove_group(&trig_info->dev.kobj,
62 trig_info->control_attrs);
66 /**
67 * iio_trigger_register_id() - get a unique id for this trigger
68 * @trig_info: the trigger
69 **/
70 static int iio_trigger_register_id(struct iio_trigger *trig_info)
72 int ret = 0;
74 idr_again:
75 if (unlikely(idr_pre_get(&iio_trigger_idr, GFP_KERNEL) == 0))
76 return -ENOMEM;
78 spin_lock(&iio_trigger_idr_lock);
79 ret = idr_get_new(&iio_trigger_idr, NULL, &trig_info->id);
80 spin_unlock(&iio_trigger_idr_lock);
81 if (unlikely(ret == -EAGAIN))
82 goto idr_again;
83 else if (likely(!ret))
84 trig_info->id = trig_info->id & MAX_ID_MASK;
86 return ret;
89 /**
90 * iio_trigger_unregister_id() - free up unique id for use by another trigger
91 * @trig_info: the trigger
92 **/
93 static void iio_trigger_unregister_id(struct iio_trigger *trig_info)
95 spin_lock(&iio_trigger_idr_lock);
96 idr_remove(&iio_trigger_idr, trig_info->id);
97 spin_unlock(&iio_trigger_idr_lock);
100 int iio_trigger_register(struct iio_trigger *trig_info)
102 int ret;
104 ret = iio_trigger_register_id(trig_info);
105 if (ret)
106 goto error_ret;
107 /* Set the name used for the sysfs directory etc */
108 dev_set_name(&trig_info->dev, "trigger%ld",
109 (unsigned long) trig_info->id);
111 ret = device_add(&trig_info->dev);
112 if (ret)
113 goto error_unregister_id;
115 ret = iio_trigger_register_sysfs(trig_info);
116 if (ret)
117 goto error_device_del;
119 /* Add to list of available triggers held by the IIO core */
120 mutex_lock(&iio_trigger_list_lock);
121 list_add_tail(&trig_info->list, &iio_trigger_list);
122 mutex_unlock(&iio_trigger_list_lock);
124 return 0;
126 error_device_del:
127 device_del(&trig_info->dev);
128 error_unregister_id:
129 iio_trigger_unregister_id(trig_info);
130 error_ret:
131 return ret;
133 EXPORT_SYMBOL(iio_trigger_register);
135 void iio_trigger_unregister(struct iio_trigger *trig_info)
137 mutex_lock(&iio_trigger_list_lock);
138 list_del(&trig_info->list);
139 mutex_unlock(&iio_trigger_list_lock);
141 iio_trigger_unregister_sysfs(trig_info);
142 iio_trigger_unregister_id(trig_info);
143 /* Possible issue in here */
144 device_unregister(&trig_info->dev);
146 EXPORT_SYMBOL(iio_trigger_unregister);
148 static struct iio_trigger *iio_trigger_find_by_name(const char *name,
149 size_t len)
151 struct iio_trigger *trig = NULL, *iter;
153 mutex_lock(&iio_trigger_list_lock);
154 list_for_each_entry(iter, &iio_trigger_list, list)
155 if (sysfs_streq(iter->name, name)) {
156 trig = iter;
157 break;
159 mutex_unlock(&iio_trigger_list_lock);
161 return trig;
164 void iio_trigger_poll(struct iio_trigger *trig, s64 time)
166 int i;
167 if (!trig->use_count) {
168 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++)
169 if (trig->subirqs[i].enabled) {
170 trig->use_count++;
171 generic_handle_irq(trig->subirq_base + i);
175 EXPORT_SYMBOL(iio_trigger_poll);
177 irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private)
179 iio_trigger_poll(private, iio_get_time_ns());
180 return IRQ_HANDLED;
182 EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
184 void iio_trigger_notify_done(struct iio_trigger *trig)
186 trig->use_count--;
187 if (trig->use_count == 0 && trig->try_reenable)
188 if (trig->try_reenable(trig)) {
189 /* Missed and interrupt so launch new poll now */
190 iio_trigger_poll(trig, 0);
193 EXPORT_SYMBOL(iio_trigger_notify_done);
196 * iio_trigger_read_name() - retrieve useful identifying name
198 ssize_t iio_trigger_read_name(struct device *dev,
199 struct device_attribute *attr,
200 char *buf)
202 struct iio_trigger *trig = dev_get_drvdata(dev);
203 return sprintf(buf, "%s\n", trig->name);
205 EXPORT_SYMBOL(iio_trigger_read_name);
207 /* Trigger Consumer related functions */
209 /* Complexity in here. With certain triggers (datardy) an acknowledgement
210 * may be needed if the pollfuncs do not include the data read for the
211 * triggering device.
212 * This is not currently handled. Alternative of not enabling trigger unless
213 * the relevant function is in there may be the best option.
215 /* Worth protecting against double additions?*/
216 int iio_trigger_attach_poll_func(struct iio_trigger *trig,
217 struct iio_poll_func *pf)
219 int ret = 0;
220 bool notinuse
221 = bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
223 pf->irq = iio_trigger_get_irq(trig);
224 ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
225 pf->type, pf->name,
226 pf);
227 if (trig->set_trigger_state && notinuse)
228 ret = trig->set_trigger_state(trig, true);
230 return ret;
232 EXPORT_SYMBOL(iio_trigger_attach_poll_func);
234 int iio_trigger_dettach_poll_func(struct iio_trigger *trig,
235 struct iio_poll_func *pf)
237 int ret = 0;
238 bool no_other_users
239 = (bitmap_weight(trig->pool,
240 CONFIG_IIO_CONSUMERS_PER_TRIGGER)
241 == 1);
242 if (trig->set_trigger_state && no_other_users) {
243 ret = trig->set_trigger_state(trig, false);
244 if (ret)
245 goto error_ret;
247 iio_trigger_put_irq(trig, pf->irq);
248 free_irq(pf->irq, pf);
250 error_ret:
251 return ret;
253 EXPORT_SYMBOL(iio_trigger_dettach_poll_func);
255 irqreturn_t iio_pollfunc_store_time(int irq, void *p)
257 struct iio_poll_func *pf = p;
258 pf->timestamp = iio_get_time_ns();
259 return IRQ_WAKE_THREAD;
261 EXPORT_SYMBOL(iio_pollfunc_store_time);
264 * iio_trigger_read_currrent() - trigger consumer sysfs query which trigger
266 * For trigger consumers the current_trigger interface allows the trigger
267 * used by the device to be queried.
269 static ssize_t iio_trigger_read_current(struct device *dev,
270 struct device_attribute *attr,
271 char *buf)
273 struct iio_dev *dev_info = dev_get_drvdata(dev);
274 int len = 0;
275 if (dev_info->trig)
276 len = sprintf(buf,
277 "%s\n",
278 dev_info->trig->name);
279 return len;
283 * iio_trigger_write_current() trigger consumer sysfs set current trigger
285 * For trigger consumers the current_trigger interface allows the trigger
286 * used for this device to be specified at run time based on the triggers
287 * name.
289 static ssize_t iio_trigger_write_current(struct device *dev,
290 struct device_attribute *attr,
291 const char *buf,
292 size_t len)
294 struct iio_dev *dev_info = dev_get_drvdata(dev);
295 struct iio_trigger *oldtrig = dev_info->trig;
296 mutex_lock(&dev_info->mlock);
297 if (dev_info->currentmode == INDIO_RING_TRIGGERED) {
298 mutex_unlock(&dev_info->mlock);
299 return -EBUSY;
301 mutex_unlock(&dev_info->mlock);
303 dev_info->trig = iio_trigger_find_by_name(buf, len);
304 if (oldtrig && dev_info->trig != oldtrig)
305 iio_put_trigger(oldtrig);
306 if (dev_info->trig)
307 iio_get_trigger(dev_info->trig);
309 return len;
312 static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
313 iio_trigger_read_current,
314 iio_trigger_write_current);
316 static struct attribute *iio_trigger_consumer_attrs[] = {
317 &dev_attr_current_trigger.attr,
318 NULL,
321 static const struct attribute_group iio_trigger_consumer_attr_group = {
322 .name = "trigger",
323 .attrs = iio_trigger_consumer_attrs,
326 static void iio_trig_release(struct device *device)
328 struct iio_trigger *trig = to_iio_trigger(device);
329 int i;
331 if (trig->subirq_base) {
332 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
333 irq_modify_status(trig->subirq_base + i,
334 IRQ_NOAUTOEN,
335 IRQ_NOREQUEST | IRQ_NOPROBE);
336 irq_set_chip(trig->subirq_base + i,
337 NULL);
338 irq_set_handler(trig->subirq_base + i,
339 NULL);
342 irq_free_descs(trig->subirq_base,
343 CONFIG_IIO_CONSUMERS_PER_TRIGGER);
345 kfree(trig);
346 iio_put();
349 static struct device_type iio_trig_type = {
350 .release = iio_trig_release,
353 static void iio_trig_subirqmask(struct irq_data *d)
355 struct irq_chip *chip = irq_data_get_irq_chip(d);
356 struct iio_trigger *trig
357 = container_of(chip,
358 struct iio_trigger, subirq_chip);
359 trig->subirqs[d->irq - trig->subirq_base].enabled = false;
362 static void iio_trig_subirqunmask(struct irq_data *d)
364 struct irq_chip *chip = irq_data_get_irq_chip(d);
365 struct iio_trigger *trig
366 = container_of(chip,
367 struct iio_trigger, subirq_chip);
368 trig->subirqs[d->irq - trig->subirq_base].enabled = true;
371 struct iio_trigger *iio_allocate_trigger_named(const char *name)
373 struct iio_trigger *trig;
374 trig = kzalloc(sizeof *trig, GFP_KERNEL);
375 if (trig) {
376 int i;
377 trig->dev.type = &iio_trig_type;
378 trig->dev.bus = &iio_bus_type;
379 device_initialize(&trig->dev);
380 dev_set_drvdata(&trig->dev, (void *)trig);
382 if (name) {
383 mutex_init(&trig->pool_lock);
384 trig->subirq_base
385 = irq_alloc_descs(-1, 0,
386 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
388 if (trig->subirq_base < 0) {
389 kfree(trig);
390 return NULL;
392 trig->name = name;
393 trig->subirq_chip.name = name;
394 trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
395 trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask;
396 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
397 irq_set_chip(trig->subirq_base + i,
398 &trig->subirq_chip);
399 irq_set_handler(trig->subirq_base + i,
400 &handle_simple_irq);
401 irq_modify_status(trig->subirq_base + i,
402 IRQ_NOREQUEST | IRQ_NOAUTOEN,
403 IRQ_NOPROBE);
406 iio_get();
408 return trig;
410 EXPORT_SYMBOL(iio_allocate_trigger_named);
412 struct iio_trigger *iio_allocate_trigger(void)
414 return iio_allocate_trigger_named(NULL);
416 EXPORT_SYMBOL(iio_allocate_trigger);
418 void iio_free_trigger(struct iio_trigger *trig)
420 if (trig)
421 put_device(&trig->dev);
423 EXPORT_SYMBOL(iio_free_trigger);
425 int iio_device_register_trigger_consumer(struct iio_dev *dev_info)
427 int ret;
428 ret = sysfs_create_group(&dev_info->dev.kobj,
429 &iio_trigger_consumer_attr_group);
430 return ret;
432 EXPORT_SYMBOL(iio_device_register_trigger_consumer);
434 int iio_device_unregister_trigger_consumer(struct iio_dev *dev_info)
436 sysfs_remove_group(&dev_info->dev.kobj,
437 &iio_trigger_consumer_attr_group);
438 return 0;
440 EXPORT_SYMBOL(iio_device_unregister_trigger_consumer);
442 int iio_triggered_ring_postenable(struct iio_dev *indio_dev)
444 return indio_dev->trig
445 ? iio_trigger_attach_poll_func(indio_dev->trig,
446 indio_dev->pollfunc)
447 : 0;
449 EXPORT_SYMBOL(iio_triggered_ring_postenable);
451 int iio_triggered_ring_predisable(struct iio_dev *indio_dev)
453 return indio_dev->trig
454 ? iio_trigger_dettach_poll_func(indio_dev->trig,
455 indio_dev->pollfunc)
456 : 0;
458 EXPORT_SYMBOL(iio_triggered_ring_predisable);