staging:iio:rename trigger_consumer.h to indicate it is core only.
[linux-2.6/next.git] / drivers / staging / iio / industrialio-trigger.c
blob8fafeb8a8b8ac5d1a5325e3c297bec8fb857c89c
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/idr.h>
12 #include <linux/err.h>
13 #include <linux/device.h>
14 #include <linux/interrupt.h>
15 #include <linux/list.h>
16 #include <linux/slab.h>
18 #include "iio.h"
19 #include "trigger.h"
20 #include "iio_core.h"
21 #include "iio_core_trigger.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_read_name() - retrieve useful identifying name
43 **/
44 static ssize_t iio_trigger_read_name(struct device *dev,
45 struct device_attribute *attr,
46 char *buf)
48 struct iio_trigger *trig = dev_get_drvdata(dev);
49 return sprintf(buf, "%s\n", trig->name);
52 static DEVICE_ATTR(name, S_IRUGO, iio_trigger_read_name, NULL);
54 /**
55 * iio_trigger_register_sysfs() - create a device for this trigger
56 * @trig_info: the trigger
58 * Also adds any control attribute registered by the trigger driver
59 **/
60 static int iio_trigger_register_sysfs(struct iio_trigger *trig_info)
62 return sysfs_add_file_to_group(&trig_info->dev.kobj,
63 &dev_attr_name.attr,
64 NULL);
67 static void iio_trigger_unregister_sysfs(struct iio_trigger *trig_info)
69 sysfs_remove_file_from_group(&trig_info->dev.kobj,
70 &dev_attr_name.attr,
71 NULL);
75 /**
76 * iio_trigger_register_id() - get a unique id for this trigger
77 * @trig_info: the trigger
78 **/
79 static int iio_trigger_register_id(struct iio_trigger *trig_info)
81 int ret = 0;
83 idr_again:
84 if (unlikely(idr_pre_get(&iio_trigger_idr, GFP_KERNEL) == 0))
85 return -ENOMEM;
87 spin_lock(&iio_trigger_idr_lock);
88 ret = idr_get_new(&iio_trigger_idr, NULL, &trig_info->id);
89 spin_unlock(&iio_trigger_idr_lock);
90 if (unlikely(ret == -EAGAIN))
91 goto idr_again;
92 else if (likely(!ret))
93 trig_info->id = trig_info->id & MAX_ID_MASK;
95 return ret;
98 /**
99 * iio_trigger_unregister_id() - free up unique id for use by another trigger
100 * @trig_info: the trigger
102 static void iio_trigger_unregister_id(struct iio_trigger *trig_info)
104 spin_lock(&iio_trigger_idr_lock);
105 idr_remove(&iio_trigger_idr, trig_info->id);
106 spin_unlock(&iio_trigger_idr_lock);
109 int iio_trigger_register(struct iio_trigger *trig_info)
111 int ret;
113 ret = iio_trigger_register_id(trig_info);
114 if (ret)
115 goto error_ret;
116 /* Set the name used for the sysfs directory etc */
117 dev_set_name(&trig_info->dev, "trigger%ld",
118 (unsigned long) trig_info->id);
120 ret = device_add(&trig_info->dev);
121 if (ret)
122 goto error_unregister_id;
124 ret = iio_trigger_register_sysfs(trig_info);
125 if (ret)
126 goto error_device_del;
128 /* Add to list of available triggers held by the IIO core */
129 mutex_lock(&iio_trigger_list_lock);
130 list_add_tail(&trig_info->list, &iio_trigger_list);
131 mutex_unlock(&iio_trigger_list_lock);
133 return 0;
135 error_device_del:
136 device_del(&trig_info->dev);
137 error_unregister_id:
138 iio_trigger_unregister_id(trig_info);
139 error_ret:
140 return ret;
142 EXPORT_SYMBOL(iio_trigger_register);
144 void iio_trigger_unregister(struct iio_trigger *trig_info)
146 mutex_lock(&iio_trigger_list_lock);
147 list_del(&trig_info->list);
148 mutex_unlock(&iio_trigger_list_lock);
150 iio_trigger_unregister_sysfs(trig_info);
151 iio_trigger_unregister_id(trig_info);
152 /* Possible issue in here */
153 device_unregister(&trig_info->dev);
155 EXPORT_SYMBOL(iio_trigger_unregister);
157 static struct iio_trigger *iio_trigger_find_by_name(const char *name,
158 size_t len)
160 struct iio_trigger *trig = NULL, *iter;
162 mutex_lock(&iio_trigger_list_lock);
163 list_for_each_entry(iter, &iio_trigger_list, list)
164 if (sysfs_streq(iter->name, name)) {
165 trig = iter;
166 break;
168 mutex_unlock(&iio_trigger_list_lock);
170 return trig;
173 void iio_trigger_poll(struct iio_trigger *trig, s64 time)
175 int i;
176 if (!trig->use_count) {
177 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++)
178 if (trig->subirqs[i].enabled) {
179 trig->use_count++;
180 generic_handle_irq(trig->subirq_base + i);
184 EXPORT_SYMBOL(iio_trigger_poll);
186 irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private)
188 iio_trigger_poll(private, iio_get_time_ns());
189 return IRQ_HANDLED;
191 EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
193 void iio_trigger_poll_chained(struct iio_trigger *trig, s64 time)
195 int i;
196 if (!trig->use_count) {
197 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++)
198 if (trig->subirqs[i].enabled) {
199 trig->use_count++;
200 handle_nested_irq(trig->subirq_base + i);
204 EXPORT_SYMBOL(iio_trigger_poll_chained);
206 void iio_trigger_notify_done(struct iio_trigger *trig)
208 trig->use_count--;
209 if (trig->use_count == 0 && trig->ops && trig->ops->try_reenable)
210 if (trig->ops->try_reenable(trig)) {
211 /* Missed and interrupt so launch new poll now */
212 iio_trigger_poll(trig, 0);
215 EXPORT_SYMBOL(iio_trigger_notify_done);
217 /* Trigger Consumer related functions */
219 /* Complexity in here. With certain triggers (datardy) an acknowledgement
220 * may be needed if the pollfuncs do not include the data read for the
221 * triggering device.
222 * This is not currently handled. Alternative of not enabling trigger unless
223 * the relevant function is in there may be the best option.
225 /* Worth protecting against double additions?*/
226 int iio_trigger_attach_poll_func(struct iio_trigger *trig,
227 struct iio_poll_func *pf)
229 int ret = 0;
230 bool notinuse
231 = bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
233 /* Prevent the module being removed whilst attached to a trigger */
234 __module_get(pf->indio_dev->info->driver_module);
235 pf->irq = iio_trigger_get_irq(trig);
236 ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
237 pf->type, pf->name,
238 pf);
239 if (trig->ops && trig->ops->set_trigger_state && notinuse)
240 ret = trig->ops->set_trigger_state(trig, true);
242 return ret;
244 EXPORT_SYMBOL(iio_trigger_attach_poll_func);
246 int iio_trigger_dettach_poll_func(struct iio_trigger *trig,
247 struct iio_poll_func *pf)
249 int ret = 0;
250 bool no_other_users
251 = (bitmap_weight(trig->pool,
252 CONFIG_IIO_CONSUMERS_PER_TRIGGER)
253 == 1);
254 if (trig->ops && trig->ops->set_trigger_state && no_other_users) {
255 ret = trig->ops->set_trigger_state(trig, false);
256 if (ret)
257 goto error_ret;
259 iio_trigger_put_irq(trig, pf->irq);
260 free_irq(pf->irq, pf);
261 module_put(pf->indio_dev->info->driver_module);
263 error_ret:
264 return ret;
266 EXPORT_SYMBOL(iio_trigger_dettach_poll_func);
268 irqreturn_t iio_pollfunc_store_time(int irq, void *p)
270 struct iio_poll_func *pf = p;
271 pf->timestamp = iio_get_time_ns();
272 return IRQ_WAKE_THREAD;
274 EXPORT_SYMBOL(iio_pollfunc_store_time);
276 struct iio_poll_func
277 *iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p),
278 irqreturn_t (*thread)(int irq, void *p),
279 int type,
280 struct iio_dev *indio_dev,
281 const char *fmt,
282 ...)
284 va_list vargs;
285 struct iio_poll_func *pf;
287 pf = kmalloc(sizeof *pf, GFP_KERNEL);
288 if (pf == NULL)
289 return NULL;
290 va_start(vargs, fmt);
291 pf->name = kvasprintf(GFP_KERNEL, fmt, vargs);
292 va_end(vargs);
293 if (pf->name == NULL) {
294 kfree(pf);
295 return NULL;
297 pf->h = h;
298 pf->thread = thread;
299 pf->type = type;
300 pf->indio_dev = indio_dev;
302 return pf;
304 EXPORT_SYMBOL_GPL(iio_alloc_pollfunc);
306 void iio_dealloc_pollfunc(struct iio_poll_func *pf)
308 kfree(pf->name);
309 kfree(pf);
311 EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc);
314 * iio_trigger_read_currrent() - trigger consumer sysfs query which trigger
316 * For trigger consumers the current_trigger interface allows the trigger
317 * used by the device to be queried.
319 static ssize_t iio_trigger_read_current(struct device *dev,
320 struct device_attribute *attr,
321 char *buf)
323 struct iio_dev *dev_info = dev_get_drvdata(dev);
324 int len = 0;
325 if (dev_info->trig)
326 len = sprintf(buf,
327 "%s\n",
328 dev_info->trig->name);
329 return len;
333 * iio_trigger_write_current() trigger consumer sysfs set current trigger
335 * For trigger consumers the current_trigger interface allows the trigger
336 * used for this device to be specified at run time based on the triggers
337 * name.
339 static ssize_t iio_trigger_write_current(struct device *dev,
340 struct device_attribute *attr,
341 const char *buf,
342 size_t len)
344 struct iio_dev *dev_info = dev_get_drvdata(dev);
345 struct iio_trigger *oldtrig = dev_info->trig;
346 struct iio_trigger *trig;
347 int ret;
349 mutex_lock(&dev_info->mlock);
350 if (dev_info->currentmode == INDIO_RING_TRIGGERED) {
351 mutex_unlock(&dev_info->mlock);
352 return -EBUSY;
354 mutex_unlock(&dev_info->mlock);
356 trig = iio_trigger_find_by_name(buf, len);
358 if (trig && dev_info->info->validate_trigger) {
359 ret = dev_info->info->validate_trigger(dev_info, trig);
360 if (ret)
361 return ret;
364 if (trig && trig->ops && trig->ops->validate_device) {
365 ret = trig->ops->validate_device(trig, dev_info);
366 if (ret)
367 return ret;
370 dev_info->trig = trig;
372 if (oldtrig && dev_info->trig != oldtrig)
373 iio_put_trigger(oldtrig);
374 if (dev_info->trig)
375 iio_get_trigger(dev_info->trig);
377 return len;
380 static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
381 iio_trigger_read_current,
382 iio_trigger_write_current);
384 static struct attribute *iio_trigger_consumer_attrs[] = {
385 &dev_attr_current_trigger.attr,
386 NULL,
389 static const struct attribute_group iio_trigger_consumer_attr_group = {
390 .name = "trigger",
391 .attrs = iio_trigger_consumer_attrs,
394 static void iio_trig_release(struct device *device)
396 struct iio_trigger *trig = to_iio_trigger(device);
397 int i;
399 if (trig->subirq_base) {
400 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
401 irq_modify_status(trig->subirq_base + i,
402 IRQ_NOAUTOEN,
403 IRQ_NOREQUEST | IRQ_NOPROBE);
404 irq_set_chip(trig->subirq_base + i,
405 NULL);
406 irq_set_handler(trig->subirq_base + i,
407 NULL);
410 irq_free_descs(trig->subirq_base,
411 CONFIG_IIO_CONSUMERS_PER_TRIGGER);
413 kfree(trig->name);
414 kfree(trig);
415 iio_put();
418 static struct device_type iio_trig_type = {
419 .release = iio_trig_release,
422 static void iio_trig_subirqmask(struct irq_data *d)
424 struct irq_chip *chip = irq_data_get_irq_chip(d);
425 struct iio_trigger *trig
426 = container_of(chip,
427 struct iio_trigger, subirq_chip);
428 trig->subirqs[d->irq - trig->subirq_base].enabled = false;
431 static void iio_trig_subirqunmask(struct irq_data *d)
433 struct irq_chip *chip = irq_data_get_irq_chip(d);
434 struct iio_trigger *trig
435 = container_of(chip,
436 struct iio_trigger, subirq_chip);
437 trig->subirqs[d->irq - trig->subirq_base].enabled = true;
440 struct iio_trigger *iio_allocate_trigger(const char *fmt, ...)
442 va_list vargs;
443 struct iio_trigger *trig;
444 trig = kzalloc(sizeof *trig, GFP_KERNEL);
445 if (trig) {
446 int i;
447 trig->dev.type = &iio_trig_type;
448 trig->dev.bus = &iio_bus_type;
449 device_initialize(&trig->dev);
450 dev_set_drvdata(&trig->dev, (void *)trig);
452 mutex_init(&trig->pool_lock);
453 trig->subirq_base
454 = irq_alloc_descs(-1, 0,
455 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
457 if (trig->subirq_base < 0) {
458 kfree(trig);
459 return NULL;
461 va_start(vargs, fmt);
462 trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
463 va_end(vargs);
464 if (trig->name == NULL) {
465 irq_free_descs(trig->subirq_base,
466 CONFIG_IIO_CONSUMERS_PER_TRIGGER);
467 kfree(trig);
468 return NULL;
470 trig->subirq_chip.name = trig->name;
471 trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
472 trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask;
473 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
474 irq_set_chip(trig->subirq_base + i,
475 &trig->subirq_chip);
476 irq_set_handler(trig->subirq_base + i,
477 &handle_simple_irq);
478 irq_modify_status(trig->subirq_base + i,
479 IRQ_NOREQUEST | IRQ_NOAUTOEN,
480 IRQ_NOPROBE);
482 iio_get();
483 get_device(&trig->dev);
485 return trig;
487 EXPORT_SYMBOL(iio_allocate_trigger);
489 void iio_free_trigger(struct iio_trigger *trig)
491 if (trig)
492 put_device(&trig->dev);
494 EXPORT_SYMBOL(iio_free_trigger);
496 int iio_device_register_trigger_consumer(struct iio_dev *dev_info)
498 int ret;
499 ret = sysfs_create_group(&dev_info->dev.kobj,
500 &iio_trigger_consumer_attr_group);
501 return ret;
503 EXPORT_SYMBOL(iio_device_register_trigger_consumer);
505 int iio_device_unregister_trigger_consumer(struct iio_dev *dev_info)
507 /* Clean up and associated but not attached triggers references */
508 if (dev_info->trig)
509 iio_put_trigger(dev_info->trig);
510 sysfs_remove_group(&dev_info->dev.kobj,
511 &iio_trigger_consumer_attr_group);
512 return 0;
514 EXPORT_SYMBOL(iio_device_unregister_trigger_consumer);
516 int iio_triggered_ring_postenable(struct iio_dev *indio_dev)
518 return indio_dev->trig
519 ? iio_trigger_attach_poll_func(indio_dev->trig,
520 indio_dev->pollfunc)
521 : 0;
523 EXPORT_SYMBOL(iio_triggered_ring_postenable);
525 int iio_triggered_ring_predisable(struct iio_dev *indio_dev)
527 return indio_dev->trig
528 ? iio_trigger_dettach_poll_func(indio_dev->trig,
529 indio_dev->pollfunc)
530 : 0;
532 EXPORT_SYMBOL(iio_triggered_ring_predisable);