Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / staging / iio / industrialio-trigger.c
blobf1ece8619e8ddcf51282785dc0f0487fbd1784e3
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"
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
27 * is added.
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);
42 /**
43 * iio_trigger_read_name() - retrieve useful identifying name
44 **/
45 static ssize_t iio_trigger_read_name(struct device *dev,
46 struct device_attribute *attr,
47 char *buf)
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);
55 /**
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
60 **/
61 static int iio_trigger_register_sysfs(struct iio_trigger *trig_info)
63 return sysfs_add_file_to_group(&trig_info->dev.kobj,
64 &dev_attr_name.attr,
65 NULL);
68 static void iio_trigger_unregister_sysfs(struct iio_trigger *trig_info)
70 sysfs_remove_file_from_group(&trig_info->dev.kobj,
71 &dev_attr_name.attr,
72 NULL);
76 /**
77 * iio_trigger_register_id() - get a unique id for this trigger
78 * @trig_info: the trigger
79 **/
80 static int iio_trigger_register_id(struct iio_trigger *trig_info)
82 int ret = 0;
84 idr_again:
85 if (unlikely(idr_pre_get(&iio_trigger_idr, GFP_KERNEL) == 0))
86 return -ENOMEM;
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))
92 goto idr_again;
93 else if (likely(!ret))
94 trig_info->id = trig_info->id & MAX_ID_MASK;
96 return ret;
99 /**
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)
112 int ret;
114 ret = iio_trigger_register_id(trig_info);
115 if (ret)
116 goto error_ret;
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);
122 if (ret)
123 goto error_unregister_id;
125 ret = iio_trigger_register_sysfs(trig_info);
126 if (ret)
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);
134 return 0;
136 error_device_del:
137 device_del(&trig_info->dev);
138 error_unregister_id:
139 iio_trigger_unregister_id(trig_info);
140 error_ret:
141 return ret;
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,
159 size_t len)
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)) {
166 trig = iter;
167 break;
169 mutex_unlock(&iio_trigger_list_lock);
171 return trig;
174 void iio_trigger_poll(struct iio_trigger *trig, s64 time)
176 int i;
177 if (!trig->use_count)
178 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++)
179 if (trig->subirqs[i].enabled) {
180 trig->use_count++;
181 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 */
218 static int iio_trigger_get_irq(struct iio_trigger *trig)
220 int ret;
221 mutex_lock(&trig->pool_lock);
222 ret = bitmap_find_free_region(trig->pool,
223 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
224 ilog2(1));
225 mutex_unlock(&trig->pool_lock);
226 if (ret >= 0)
227 ret += trig->subirq_base;
229 return ret;
232 static void iio_trigger_put_irq(struct iio_trigger *trig, int irq)
234 mutex_lock(&trig->pool_lock);
235 clear_bit(irq - trig->subirq_base, trig->pool);
236 mutex_unlock(&trig->pool_lock);
239 /* Complexity in here. With certain triggers (datardy) an acknowledgement
240 * may be needed if the pollfuncs do not include the data read for the
241 * triggering device.
242 * This is not currently handled. Alternative of not enabling trigger unless
243 * the relevant function is in there may be the best option.
245 /* Worth protecting against double additions?*/
246 static int iio_trigger_attach_poll_func(struct iio_trigger *trig,
247 struct iio_poll_func *pf)
249 int ret = 0;
250 bool notinuse
251 = bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
253 /* Prevent the module being removed whilst attached to a trigger */
254 __module_get(pf->indio_dev->info->driver_module);
255 pf->irq = iio_trigger_get_irq(trig);
256 ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
257 pf->type, pf->name,
258 pf);
259 if (trig->ops && trig->ops->set_trigger_state && notinuse)
260 ret = trig->ops->set_trigger_state(trig, true);
262 return ret;
265 static int iio_trigger_dettach_poll_func(struct iio_trigger *trig,
266 struct iio_poll_func *pf)
268 int ret = 0;
269 bool no_other_users
270 = (bitmap_weight(trig->pool,
271 CONFIG_IIO_CONSUMERS_PER_TRIGGER)
272 == 1);
273 if (trig->ops && trig->ops->set_trigger_state && no_other_users) {
274 ret = trig->ops->set_trigger_state(trig, false);
275 if (ret)
276 goto error_ret;
278 iio_trigger_put_irq(trig, pf->irq);
279 free_irq(pf->irq, pf);
280 module_put(pf->indio_dev->info->driver_module);
282 error_ret:
283 return ret;
286 irqreturn_t iio_pollfunc_store_time(int irq, void *p)
288 struct iio_poll_func *pf = p;
289 pf->timestamp = iio_get_time_ns();
290 return IRQ_WAKE_THREAD;
292 EXPORT_SYMBOL(iio_pollfunc_store_time);
294 struct iio_poll_func
295 *iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p),
296 irqreturn_t (*thread)(int irq, void *p),
297 int type,
298 struct iio_dev *indio_dev,
299 const char *fmt,
300 ...)
302 va_list vargs;
303 struct iio_poll_func *pf;
305 pf = kmalloc(sizeof *pf, GFP_KERNEL);
306 if (pf == NULL)
307 return NULL;
308 va_start(vargs, fmt);
309 pf->name = kvasprintf(GFP_KERNEL, fmt, vargs);
310 va_end(vargs);
311 if (pf->name == NULL) {
312 kfree(pf);
313 return NULL;
315 pf->h = h;
316 pf->thread = thread;
317 pf->type = type;
318 pf->indio_dev = indio_dev;
320 return pf;
322 EXPORT_SYMBOL_GPL(iio_alloc_pollfunc);
324 void iio_dealloc_pollfunc(struct iio_poll_func *pf)
326 kfree(pf->name);
327 kfree(pf);
329 EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc);
332 * iio_trigger_read_currrent() - trigger consumer sysfs query which trigger
334 * For trigger consumers the current_trigger interface allows the trigger
335 * used by the device to be queried.
337 static ssize_t iio_trigger_read_current(struct device *dev,
338 struct device_attribute *attr,
339 char *buf)
341 struct iio_dev *dev_info = dev_get_drvdata(dev);
343 if (dev_info->trig)
344 return sprintf(buf, "%s\n", dev_info->trig->name);
345 return 0;
349 * iio_trigger_write_current() trigger consumer sysfs set current trigger
351 * For trigger consumers the current_trigger interface allows the trigger
352 * used for this device to be specified at run time based on the triggers
353 * name.
355 static ssize_t iio_trigger_write_current(struct device *dev,
356 struct device_attribute *attr,
357 const char *buf,
358 size_t len)
360 struct iio_dev *dev_info = dev_get_drvdata(dev);
361 struct iio_trigger *oldtrig = dev_info->trig;
362 struct iio_trigger *trig;
363 int ret;
365 mutex_lock(&dev_info->mlock);
366 if (dev_info->currentmode == INDIO_RING_TRIGGERED) {
367 mutex_unlock(&dev_info->mlock);
368 return -EBUSY;
370 mutex_unlock(&dev_info->mlock);
372 trig = iio_trigger_find_by_name(buf, len);
374 if (trig && dev_info->info->validate_trigger) {
375 ret = dev_info->info->validate_trigger(dev_info, trig);
376 if (ret)
377 return ret;
380 if (trig && trig->ops && trig->ops->validate_device) {
381 ret = trig->ops->validate_device(trig, dev_info);
382 if (ret)
383 return ret;
386 dev_info->trig = trig;
388 if (oldtrig && dev_info->trig != oldtrig)
389 iio_put_trigger(oldtrig);
390 if (dev_info->trig)
391 iio_get_trigger(dev_info->trig);
393 return len;
396 static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
397 iio_trigger_read_current,
398 iio_trigger_write_current);
400 static struct attribute *iio_trigger_consumer_attrs[] = {
401 &dev_attr_current_trigger.attr,
402 NULL,
405 static const struct attribute_group iio_trigger_consumer_attr_group = {
406 .name = "trigger",
407 .attrs = iio_trigger_consumer_attrs,
410 static void iio_trig_release(struct device *device)
412 struct iio_trigger *trig = to_iio_trigger(device);
413 int i;
415 if (trig->subirq_base) {
416 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
417 irq_modify_status(trig->subirq_base + i,
418 IRQ_NOAUTOEN,
419 IRQ_NOREQUEST | IRQ_NOPROBE);
420 irq_set_chip(trig->subirq_base + i,
421 NULL);
422 irq_set_handler(trig->subirq_base + i,
423 NULL);
426 irq_free_descs(trig->subirq_base,
427 CONFIG_IIO_CONSUMERS_PER_TRIGGER);
429 kfree(trig->name);
430 kfree(trig);
431 iio_put();
434 static struct device_type iio_trig_type = {
435 .release = iio_trig_release,
438 static void iio_trig_subirqmask(struct irq_data *d)
440 struct irq_chip *chip = irq_data_get_irq_chip(d);
441 struct iio_trigger *trig
442 = container_of(chip,
443 struct iio_trigger, subirq_chip);
444 trig->subirqs[d->irq - trig->subirq_base].enabled = false;
447 static void iio_trig_subirqunmask(struct irq_data *d)
449 struct irq_chip *chip = irq_data_get_irq_chip(d);
450 struct iio_trigger *trig
451 = container_of(chip,
452 struct iio_trigger, subirq_chip);
453 trig->subirqs[d->irq - trig->subirq_base].enabled = true;
456 struct iio_trigger *iio_allocate_trigger(const char *fmt, ...)
458 va_list vargs;
459 struct iio_trigger *trig;
460 trig = kzalloc(sizeof *trig, GFP_KERNEL);
461 if (trig) {
462 int i;
463 trig->dev.type = &iio_trig_type;
464 trig->dev.bus = &iio_bus_type;
465 device_initialize(&trig->dev);
466 dev_set_drvdata(&trig->dev, (void *)trig);
468 mutex_init(&trig->pool_lock);
469 trig->subirq_base
470 = irq_alloc_descs(-1, 0,
471 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
473 if (trig->subirq_base < 0) {
474 kfree(trig);
475 return NULL;
477 va_start(vargs, fmt);
478 trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
479 va_end(vargs);
480 if (trig->name == NULL) {
481 irq_free_descs(trig->subirq_base,
482 CONFIG_IIO_CONSUMERS_PER_TRIGGER);
483 kfree(trig);
484 return NULL;
486 trig->subirq_chip.name = trig->name;
487 trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
488 trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask;
489 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
490 irq_set_chip(trig->subirq_base + i,
491 &trig->subirq_chip);
492 irq_set_handler(trig->subirq_base + i,
493 &handle_simple_irq);
494 irq_modify_status(trig->subirq_base + i,
495 IRQ_NOREQUEST | IRQ_NOAUTOEN,
496 IRQ_NOPROBE);
498 iio_get();
499 get_device(&trig->dev);
501 return trig;
503 EXPORT_SYMBOL(iio_allocate_trigger);
505 void iio_free_trigger(struct iio_trigger *trig)
507 if (trig)
508 put_device(&trig->dev);
510 EXPORT_SYMBOL(iio_free_trigger);
512 int iio_device_register_trigger_consumer(struct iio_dev *dev_info)
514 return sysfs_create_group(&dev_info->dev.kobj,
515 &iio_trigger_consumer_attr_group);
518 void iio_device_unregister_trigger_consumer(struct iio_dev *dev_info)
520 /* Clean up and associated but not attached triggers references */
521 if (dev_info->trig)
522 iio_put_trigger(dev_info->trig);
523 sysfs_remove_group(&dev_info->dev.kobj,
524 &iio_trigger_consumer_attr_group);
527 int iio_triggered_ring_postenable(struct iio_dev *indio_dev)
529 return indio_dev->trig
530 ? iio_trigger_attach_poll_func(indio_dev->trig,
531 indio_dev->pollfunc)
532 : 0;
534 EXPORT_SYMBOL(iio_triggered_ring_postenable);
536 int iio_triggered_ring_predisable(struct iio_dev *indio_dev)
538 return indio_dev->trig
539 ? iio_trigger_dettach_poll_func(indio_dev->trig,
540 indio_dev->pollfunc)
541 : 0;
543 EXPORT_SYMBOL(iio_triggered_ring_predisable);