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.
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>
22 #include "trigger_consumer.h"
24 /* RFC - Question of approach
25 * Make the common case (single sensor single trigger)
26 * simple by starting trigger capture from when first sensors
29 * Complex simultaneous start requires use of 'hold' functionality
30 * of the trigger. (not implemented)
32 * Any other suggestions?
35 static DEFINE_IDR(iio_trigger_idr);
36 static DEFINE_SPINLOCK(iio_trigger_idr_lock);
38 /* Single list of all available triggers */
39 static LIST_HEAD(iio_trigger_list);
40 static DEFINE_MUTEX(iio_trigger_list_lock);
43 * iio_trigger_read_name() - retrieve useful identifying name
45 static ssize_t iio_trigger_read_name(struct device *dev,
46 struct device_attribute *attr,
49 struct iio_trigger *trig = dev_get_drvdata(dev);
50 return sprintf(buf, "%s\n", trig->name);
53 static DEVICE_ATTR(name, S_IRUGO, iio_trigger_read_name, NULL);
56 * iio_trigger_register_sysfs() - create a device for this trigger
57 * @trig_info: the trigger
59 * Also adds any control attribute registered by the trigger driver
61 static int iio_trigger_register_sysfs(struct iio_trigger *trig_info)
63 return sysfs_add_file_to_group(&trig_info->dev.kobj,
68 static void iio_trigger_unregister_sysfs(struct iio_trigger *trig_info)
70 sysfs_remove_file_from_group(&trig_info->dev.kobj,
77 * iio_trigger_register_id() - get a unique id for this trigger
78 * @trig_info: the trigger
80 static int iio_trigger_register_id(struct iio_trigger *trig_info)
85 if (unlikely(idr_pre_get(&iio_trigger_idr, GFP_KERNEL) == 0))
88 spin_lock(&iio_trigger_idr_lock);
89 ret = idr_get_new(&iio_trigger_idr, NULL, &trig_info->id);
90 spin_unlock(&iio_trigger_idr_lock);
91 if (unlikely(ret == -EAGAIN))
93 else if (likely(!ret))
94 trig_info->id = trig_info->id & MAX_ID_MASK;
100 * iio_trigger_unregister_id() - free up unique id for use by another trigger
101 * @trig_info: the trigger
103 static void iio_trigger_unregister_id(struct iio_trigger *trig_info)
105 spin_lock(&iio_trigger_idr_lock);
106 idr_remove(&iio_trigger_idr, trig_info->id);
107 spin_unlock(&iio_trigger_idr_lock);
110 int iio_trigger_register(struct iio_trigger *trig_info)
114 ret = iio_trigger_register_id(trig_info);
117 /* Set the name used for the sysfs directory etc */
118 dev_set_name(&trig_info->dev, "trigger%ld",
119 (unsigned long) trig_info->id);
121 ret = device_add(&trig_info->dev);
123 goto error_unregister_id;
125 ret = iio_trigger_register_sysfs(trig_info);
127 goto error_device_del;
129 /* Add to list of available triggers held by the IIO core */
130 mutex_lock(&iio_trigger_list_lock);
131 list_add_tail(&trig_info->list, &iio_trigger_list);
132 mutex_unlock(&iio_trigger_list_lock);
137 device_del(&trig_info->dev);
139 iio_trigger_unregister_id(trig_info);
143 EXPORT_SYMBOL(iio_trigger_register);
145 void iio_trigger_unregister(struct iio_trigger *trig_info)
147 mutex_lock(&iio_trigger_list_lock);
148 list_del(&trig_info->list);
149 mutex_unlock(&iio_trigger_list_lock);
151 iio_trigger_unregister_sysfs(trig_info);
152 iio_trigger_unregister_id(trig_info);
153 /* Possible issue in here */
154 device_unregister(&trig_info->dev);
156 EXPORT_SYMBOL(iio_trigger_unregister);
158 static struct iio_trigger *iio_trigger_find_by_name(const char *name,
161 struct iio_trigger *trig = NULL, *iter;
163 mutex_lock(&iio_trigger_list_lock);
164 list_for_each_entry(iter, &iio_trigger_list, list)
165 if (sysfs_streq(iter->name, name)) {
169 mutex_unlock(&iio_trigger_list_lock);
174 void iio_trigger_poll(struct iio_trigger *trig, s64 time)
177 if (!trig->use_count) {
178 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++)
179 if (trig->subirqs[i].enabled) {
181 generic_handle_irq(trig->subirq_base + i);
185 EXPORT_SYMBOL(iio_trigger_poll);
187 irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private)
189 iio_trigger_poll(private, iio_get_time_ns());
192 EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
194 void iio_trigger_poll_chained(struct iio_trigger *trig, s64 time)
197 if (!trig->use_count) {
198 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++)
199 if (trig->subirqs[i].enabled) {
201 handle_nested_irq(trig->subirq_base + i);
205 EXPORT_SYMBOL(iio_trigger_poll_chained);
207 void iio_trigger_notify_done(struct iio_trigger *trig)
210 if (trig->use_count == 0 && trig->ops && trig->ops->try_reenable)
211 if (trig->ops->try_reenable(trig)) {
212 /* Missed and interrupt so launch new poll now */
213 iio_trigger_poll(trig, 0);
216 EXPORT_SYMBOL(iio_trigger_notify_done);
218 /* Trigger Consumer related functions */
220 /* Complexity in here. With certain triggers (datardy) an acknowledgement
221 * may be needed if the pollfuncs do not include the data read for the
223 * This is not currently handled. Alternative of not enabling trigger unless
224 * the relevant function is in there may be the best option.
226 /* Worth protecting against double additions?*/
227 int iio_trigger_attach_poll_func(struct iio_trigger *trig,
228 struct iio_poll_func *pf)
232 = bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
234 pf->irq = iio_trigger_get_irq(trig);
235 ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
238 if (trig->ops && trig->ops->set_trigger_state && notinuse)
239 ret = trig->ops->set_trigger_state(trig, true);
243 EXPORT_SYMBOL(iio_trigger_attach_poll_func);
245 int iio_trigger_dettach_poll_func(struct iio_trigger *trig,
246 struct iio_poll_func *pf)
250 = (bitmap_weight(trig->pool,
251 CONFIG_IIO_CONSUMERS_PER_TRIGGER)
253 if (trig->ops && trig->ops->set_trigger_state && no_other_users) {
254 ret = trig->ops->set_trigger_state(trig, false);
258 iio_trigger_put_irq(trig, pf->irq);
259 free_irq(pf->irq, pf);
264 EXPORT_SYMBOL(iio_trigger_dettach_poll_func);
266 irqreturn_t iio_pollfunc_store_time(int irq, void *p)
268 struct iio_poll_func *pf = p;
269 pf->timestamp = iio_get_time_ns();
270 return IRQ_WAKE_THREAD;
272 EXPORT_SYMBOL(iio_pollfunc_store_time);
275 *iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p),
276 irqreturn_t (*thread)(int irq, void *p),
283 struct iio_poll_func *pf;
285 pf = kmalloc(sizeof *pf, GFP_KERNEL);
288 va_start(vargs, fmt);
289 pf->name = kvasprintf(GFP_KERNEL, fmt, vargs);
291 if (pf->name == NULL) {
298 pf->private_data = private;
302 EXPORT_SYMBOL_GPL(iio_alloc_pollfunc);
304 void iio_dealloc_pollfunc(struct iio_poll_func *pf)
309 EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc);
312 * iio_trigger_read_currrent() - trigger consumer sysfs query which trigger
314 * For trigger consumers the current_trigger interface allows the trigger
315 * used by the device to be queried.
317 static ssize_t iio_trigger_read_current(struct device *dev,
318 struct device_attribute *attr,
321 struct iio_dev *dev_info = dev_get_drvdata(dev);
326 dev_info->trig->name);
331 * iio_trigger_write_current() trigger consumer sysfs set current trigger
333 * For trigger consumers the current_trigger interface allows the trigger
334 * used for this device to be specified at run time based on the triggers
337 static ssize_t iio_trigger_write_current(struct device *dev,
338 struct device_attribute *attr,
342 struct iio_dev *dev_info = dev_get_drvdata(dev);
343 struct iio_trigger *oldtrig = dev_info->trig;
344 struct iio_trigger *trig;
347 mutex_lock(&dev_info->mlock);
348 if (dev_info->currentmode == INDIO_RING_TRIGGERED) {
349 mutex_unlock(&dev_info->mlock);
352 mutex_unlock(&dev_info->mlock);
354 trig = iio_trigger_find_by_name(buf, len);
356 if (trig && dev_info->info->validate_trigger) {
357 ret = dev_info->info->validate_trigger(dev_info, trig);
362 if (trig && trig->ops && trig->ops->validate_device) {
363 ret = trig->ops->validate_device(trig, dev_info);
368 dev_info->trig = trig;
370 if (oldtrig && dev_info->trig != oldtrig)
371 iio_put_trigger(oldtrig);
373 iio_get_trigger(dev_info->trig);
378 static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
379 iio_trigger_read_current,
380 iio_trigger_write_current);
382 static struct attribute *iio_trigger_consumer_attrs[] = {
383 &dev_attr_current_trigger.attr,
387 static const struct attribute_group iio_trigger_consumer_attr_group = {
389 .attrs = iio_trigger_consumer_attrs,
392 static void iio_trig_release(struct device *device)
394 struct iio_trigger *trig = to_iio_trigger(device);
397 if (trig->subirq_base) {
398 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
399 irq_modify_status(trig->subirq_base + i,
401 IRQ_NOREQUEST | IRQ_NOPROBE);
402 irq_set_chip(trig->subirq_base + i,
404 irq_set_handler(trig->subirq_base + i,
408 irq_free_descs(trig->subirq_base,
409 CONFIG_IIO_CONSUMERS_PER_TRIGGER);
416 static struct device_type iio_trig_type = {
417 .release = iio_trig_release,
420 static void iio_trig_subirqmask(struct irq_data *d)
422 struct irq_chip *chip = irq_data_get_irq_chip(d);
423 struct iio_trigger *trig
425 struct iio_trigger, subirq_chip);
426 trig->subirqs[d->irq - trig->subirq_base].enabled = false;
429 static void iio_trig_subirqunmask(struct irq_data *d)
431 struct irq_chip *chip = irq_data_get_irq_chip(d);
432 struct iio_trigger *trig
434 struct iio_trigger, subirq_chip);
435 trig->subirqs[d->irq - trig->subirq_base].enabled = true;
438 struct iio_trigger *iio_allocate_trigger(const char *fmt, ...)
441 struct iio_trigger *trig;
442 trig = kzalloc(sizeof *trig, GFP_KERNEL);
445 trig->dev.type = &iio_trig_type;
446 trig->dev.bus = &iio_bus_type;
447 device_initialize(&trig->dev);
448 dev_set_drvdata(&trig->dev, (void *)trig);
450 mutex_init(&trig->pool_lock);
452 = irq_alloc_descs(-1, 0,
453 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
455 if (trig->subirq_base < 0) {
459 va_start(vargs, fmt);
460 trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
462 if (trig->name == NULL) {
463 irq_free_descs(trig->subirq_base,
464 CONFIG_IIO_CONSUMERS_PER_TRIGGER);
468 trig->subirq_chip.name = trig->name;
469 trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
470 trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask;
471 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
472 irq_set_chip(trig->subirq_base + i,
474 irq_set_handler(trig->subirq_base + i,
476 irq_modify_status(trig->subirq_base + i,
477 IRQ_NOREQUEST | IRQ_NOAUTOEN,
484 EXPORT_SYMBOL(iio_allocate_trigger);
486 void iio_free_trigger(struct iio_trigger *trig)
489 put_device(&trig->dev);
491 EXPORT_SYMBOL(iio_free_trigger);
493 int iio_device_register_trigger_consumer(struct iio_dev *dev_info)
496 ret = sysfs_create_group(&dev_info->dev.kobj,
497 &iio_trigger_consumer_attr_group);
500 EXPORT_SYMBOL(iio_device_register_trigger_consumer);
502 int iio_device_unregister_trigger_consumer(struct iio_dev *dev_info)
504 sysfs_remove_group(&dev_info->dev.kobj,
505 &iio_trigger_consumer_attr_group);
508 EXPORT_SYMBOL(iio_device_unregister_trigger_consumer);
510 int iio_triggered_ring_postenable(struct iio_dev *indio_dev)
512 return indio_dev->trig
513 ? iio_trigger_attach_poll_func(indio_dev->trig,
517 EXPORT_SYMBOL(iio_triggered_ring_postenable);
519 int iio_triggered_ring_predisable(struct iio_dev *indio_dev)
521 return indio_dev->trig
522 ? iio_trigger_dettach_poll_func(indio_dev->trig,
526 EXPORT_SYMBOL(iio_triggered_ring_predisable);