Merge 4.7-rc4 into staging-next
[linux/fpc-iii.git] / drivers / iio / industrialio-trigger.c
blob7c15c3092d5d1a53eba70614f5add64c7ae164f8
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 <linux/iio/iio.h>
19 #include <linux/iio/trigger.h>
20 #include "iio_core.h"
21 #include "iio_core_trigger.h"
22 #include <linux/iio/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_IDA(iio_trigger_ida);
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 * @dev: device associated with the iio_trigger
44 * @attr: pointer to the device_attribute structure that is
45 * being processed
46 * @buf: buffer to print the name into
48 * Return: a negative number on failure or the number of written
49 * characters on success.
51 static ssize_t iio_trigger_read_name(struct device *dev,
52 struct device_attribute *attr,
53 char *buf)
55 struct iio_trigger *trig = to_iio_trigger(dev);
56 return sprintf(buf, "%s\n", trig->name);
59 static DEVICE_ATTR(name, S_IRUGO, iio_trigger_read_name, NULL);
61 static struct attribute *iio_trig_dev_attrs[] = {
62 &dev_attr_name.attr,
63 NULL,
65 ATTRIBUTE_GROUPS(iio_trig_dev);
67 int iio_trigger_register(struct iio_trigger *trig_info)
69 int ret;
71 /* trig_info->ops is required for the module member */
72 if (!trig_info->ops)
73 return -EINVAL;
75 trig_info->id = ida_simple_get(&iio_trigger_ida, 0, 0, GFP_KERNEL);
76 if (trig_info->id < 0)
77 return trig_info->id;
79 /* Set the name used for the sysfs directory etc */
80 dev_set_name(&trig_info->dev, "trigger%ld",
81 (unsigned long) trig_info->id);
83 ret = device_add(&trig_info->dev);
84 if (ret)
85 goto error_unregister_id;
87 /* Add to list of available triggers held by the IIO core */
88 mutex_lock(&iio_trigger_list_lock);
89 list_add_tail(&trig_info->list, &iio_trigger_list);
90 mutex_unlock(&iio_trigger_list_lock);
92 return 0;
94 error_unregister_id:
95 ida_simple_remove(&iio_trigger_ida, trig_info->id);
96 return ret;
98 EXPORT_SYMBOL(iio_trigger_register);
100 void iio_trigger_unregister(struct iio_trigger *trig_info)
102 mutex_lock(&iio_trigger_list_lock);
103 list_del(&trig_info->list);
104 mutex_unlock(&iio_trigger_list_lock);
106 ida_simple_remove(&iio_trigger_ida, trig_info->id);
107 /* Possible issue in here */
108 device_del(&trig_info->dev);
110 EXPORT_SYMBOL(iio_trigger_unregister);
112 static struct iio_trigger *iio_trigger_find_by_name(const char *name,
113 size_t len)
115 struct iio_trigger *trig = NULL, *iter;
117 mutex_lock(&iio_trigger_list_lock);
118 list_for_each_entry(iter, &iio_trigger_list, list)
119 if (sysfs_streq(iter->name, name)) {
120 trig = iter;
121 break;
123 mutex_unlock(&iio_trigger_list_lock);
125 return trig;
128 void iio_trigger_poll(struct iio_trigger *trig)
130 int i;
132 if (!atomic_read(&trig->use_count)) {
133 atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
135 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
136 if (trig->subirqs[i].enabled)
137 generic_handle_irq(trig->subirq_base + i);
138 else
139 iio_trigger_notify_done(trig);
143 EXPORT_SYMBOL(iio_trigger_poll);
145 irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private)
147 iio_trigger_poll(private);
148 return IRQ_HANDLED;
150 EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
152 void iio_trigger_poll_chained(struct iio_trigger *trig)
154 int i;
156 if (!atomic_read(&trig->use_count)) {
157 atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
159 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
160 if (trig->subirqs[i].enabled)
161 handle_nested_irq(trig->subirq_base + i);
162 else
163 iio_trigger_notify_done(trig);
167 EXPORT_SYMBOL(iio_trigger_poll_chained);
169 void iio_trigger_notify_done(struct iio_trigger *trig)
171 if (atomic_dec_and_test(&trig->use_count) && trig->ops->try_reenable)
172 if (trig->ops->try_reenable(trig))
173 /* Missed an interrupt so launch new poll now */
174 iio_trigger_poll(trig);
176 EXPORT_SYMBOL(iio_trigger_notify_done);
178 /* Trigger Consumer related functions */
179 static int iio_trigger_get_irq(struct iio_trigger *trig)
181 int ret;
182 mutex_lock(&trig->pool_lock);
183 ret = bitmap_find_free_region(trig->pool,
184 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
185 ilog2(1));
186 mutex_unlock(&trig->pool_lock);
187 if (ret >= 0)
188 ret += trig->subirq_base;
190 return ret;
193 static void iio_trigger_put_irq(struct iio_trigger *trig, int irq)
195 mutex_lock(&trig->pool_lock);
196 clear_bit(irq - trig->subirq_base, trig->pool);
197 mutex_unlock(&trig->pool_lock);
200 /* Complexity in here. With certain triggers (datardy) an acknowledgement
201 * may be needed if the pollfuncs do not include the data read for the
202 * triggering device.
203 * This is not currently handled. Alternative of not enabling trigger unless
204 * the relevant function is in there may be the best option.
206 /* Worth protecting against double additions? */
207 static int iio_trigger_attach_poll_func(struct iio_trigger *trig,
208 struct iio_poll_func *pf)
210 int ret = 0;
211 bool notinuse
212 = bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
214 /* Prevent the module from being removed whilst attached to a trigger */
215 __module_get(pf->indio_dev->info->driver_module);
217 /* Get irq number */
218 pf->irq = iio_trigger_get_irq(trig);
219 if (pf->irq < 0)
220 goto out_put_module;
222 /* Request irq */
223 ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
224 pf->type, pf->name,
225 pf);
226 if (ret < 0)
227 goto out_put_irq;
229 /* Enable trigger in driver */
230 if (trig->ops->set_trigger_state && notinuse) {
231 ret = trig->ops->set_trigger_state(trig, true);
232 if (ret < 0)
233 goto out_free_irq;
236 return ret;
238 out_free_irq:
239 free_irq(pf->irq, pf);
240 out_put_irq:
241 iio_trigger_put_irq(trig, pf->irq);
242 out_put_module:
243 module_put(pf->indio_dev->info->driver_module);
244 return ret;
247 static int iio_trigger_detach_poll_func(struct iio_trigger *trig,
248 struct iio_poll_func *pf)
250 int ret = 0;
251 bool no_other_users
252 = (bitmap_weight(trig->pool,
253 CONFIG_IIO_CONSUMERS_PER_TRIGGER)
254 == 1);
255 if (trig->ops->set_trigger_state && no_other_users) {
256 ret = trig->ops->set_trigger_state(trig, false);
257 if (ret)
258 return ret;
260 iio_trigger_put_irq(trig, pf->irq);
261 free_irq(pf->irq, pf);
262 module_put(pf->indio_dev->info->driver_module);
264 return ret;
267 irqreturn_t iio_pollfunc_store_time(int irq, void *p)
269 struct iio_poll_func *pf = p;
270 pf->timestamp = iio_get_time_ns();
271 return IRQ_WAKE_THREAD;
273 EXPORT_SYMBOL(iio_pollfunc_store_time);
275 struct iio_poll_func
276 *iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p),
277 irqreturn_t (*thread)(int irq, void *p),
278 int type,
279 struct iio_dev *indio_dev,
280 const char *fmt,
281 ...)
283 va_list vargs;
284 struct iio_poll_func *pf;
286 pf = kmalloc(sizeof *pf, GFP_KERNEL);
287 if (pf == NULL)
288 return NULL;
289 va_start(vargs, fmt);
290 pf->name = kvasprintf(GFP_KERNEL, fmt, vargs);
291 va_end(vargs);
292 if (pf->name == NULL) {
293 kfree(pf);
294 return NULL;
296 pf->h = h;
297 pf->thread = thread;
298 pf->type = type;
299 pf->indio_dev = indio_dev;
301 return pf;
303 EXPORT_SYMBOL_GPL(iio_alloc_pollfunc);
305 void iio_dealloc_pollfunc(struct iio_poll_func *pf)
307 kfree(pf->name);
308 kfree(pf);
310 EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc);
313 * iio_trigger_read_current() - trigger consumer sysfs query current trigger
314 * @dev: device associated with an industrial I/O device
315 * @attr: pointer to the device_attribute structure that
316 * is being processed
317 * @buf: buffer where the current trigger name will be printed into
319 * For trigger consumers the current_trigger interface allows the trigger
320 * used by the device to be queried.
322 * Return: a negative number on failure, the number of characters written
323 * on success or 0 if no trigger is available
325 static ssize_t iio_trigger_read_current(struct device *dev,
326 struct device_attribute *attr,
327 char *buf)
329 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
331 if (indio_dev->trig)
332 return sprintf(buf, "%s\n", indio_dev->trig->name);
333 return 0;
337 * iio_trigger_write_current() - trigger consumer sysfs set current trigger
338 * @dev: device associated with an industrial I/O device
339 * @attr: device attribute that is being processed
340 * @buf: string buffer that holds the name of the trigger
341 * @len: length of the trigger name held by buf
343 * For trigger consumers the current_trigger interface allows the trigger
344 * used for this device to be specified at run time based on the trigger's
345 * name.
347 * Return: negative error code on failure or length of the buffer
348 * on success
350 static ssize_t iio_trigger_write_current(struct device *dev,
351 struct device_attribute *attr,
352 const char *buf,
353 size_t len)
355 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
356 struct iio_trigger *oldtrig = indio_dev->trig;
357 struct iio_trigger *trig;
358 int ret;
360 mutex_lock(&indio_dev->mlock);
361 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
362 mutex_unlock(&indio_dev->mlock);
363 return -EBUSY;
365 mutex_unlock(&indio_dev->mlock);
367 trig = iio_trigger_find_by_name(buf, len);
368 if (oldtrig == trig)
369 return len;
371 if (trig && indio_dev->info->validate_trigger) {
372 ret = indio_dev->info->validate_trigger(indio_dev, trig);
373 if (ret)
374 return ret;
377 if (trig && trig->ops->validate_device) {
378 ret = trig->ops->validate_device(trig, indio_dev);
379 if (ret)
380 return ret;
383 indio_dev->trig = trig;
385 if (oldtrig) {
386 if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
387 iio_trigger_detach_poll_func(oldtrig,
388 indio_dev->pollfunc_event);
389 iio_trigger_put(oldtrig);
391 if (indio_dev->trig) {
392 iio_trigger_get(indio_dev->trig);
393 if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
394 iio_trigger_attach_poll_func(indio_dev->trig,
395 indio_dev->pollfunc_event);
398 return len;
401 static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
402 iio_trigger_read_current,
403 iio_trigger_write_current);
405 static struct attribute *iio_trigger_consumer_attrs[] = {
406 &dev_attr_current_trigger.attr,
407 NULL,
410 static const struct attribute_group iio_trigger_consumer_attr_group = {
411 .name = "trigger",
412 .attrs = iio_trigger_consumer_attrs,
415 static void iio_trig_release(struct device *device)
417 struct iio_trigger *trig = to_iio_trigger(device);
418 int i;
420 if (trig->subirq_base) {
421 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
422 irq_modify_status(trig->subirq_base + i,
423 IRQ_NOAUTOEN,
424 IRQ_NOREQUEST | IRQ_NOPROBE);
425 irq_set_chip(trig->subirq_base + i,
426 NULL);
427 irq_set_handler(trig->subirq_base + i,
428 NULL);
431 irq_free_descs(trig->subirq_base,
432 CONFIG_IIO_CONSUMERS_PER_TRIGGER);
434 kfree(trig->name);
435 kfree(trig);
438 static struct device_type iio_trig_type = {
439 .release = iio_trig_release,
440 .groups = iio_trig_dev_groups,
443 static void iio_trig_subirqmask(struct irq_data *d)
445 struct irq_chip *chip = irq_data_get_irq_chip(d);
446 struct iio_trigger *trig
447 = container_of(chip,
448 struct iio_trigger, subirq_chip);
449 trig->subirqs[d->irq - trig->subirq_base].enabled = false;
452 static void iio_trig_subirqunmask(struct irq_data *d)
454 struct irq_chip *chip = irq_data_get_irq_chip(d);
455 struct iio_trigger *trig
456 = container_of(chip,
457 struct iio_trigger, subirq_chip);
458 trig->subirqs[d->irq - trig->subirq_base].enabled = true;
461 static struct iio_trigger *viio_trigger_alloc(const char *fmt, va_list vargs)
463 struct iio_trigger *trig;
464 trig = kzalloc(sizeof *trig, GFP_KERNEL);
465 if (trig) {
466 int i;
467 trig->dev.type = &iio_trig_type;
468 trig->dev.bus = &iio_bus_type;
469 device_initialize(&trig->dev);
471 mutex_init(&trig->pool_lock);
472 trig->subirq_base
473 = irq_alloc_descs(-1, 0,
474 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
476 if (trig->subirq_base < 0) {
477 kfree(trig);
478 return NULL;
481 trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
482 if (trig->name == NULL) {
483 irq_free_descs(trig->subirq_base,
484 CONFIG_IIO_CONSUMERS_PER_TRIGGER);
485 kfree(trig);
486 return NULL;
488 trig->subirq_chip.name = trig->name;
489 trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
490 trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask;
491 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
492 irq_set_chip(trig->subirq_base + i,
493 &trig->subirq_chip);
494 irq_set_handler(trig->subirq_base + i,
495 &handle_simple_irq);
496 irq_modify_status(trig->subirq_base + i,
497 IRQ_NOREQUEST | IRQ_NOAUTOEN,
498 IRQ_NOPROBE);
500 get_device(&trig->dev);
503 return trig;
506 struct iio_trigger *iio_trigger_alloc(const char *fmt, ...)
508 struct iio_trigger *trig;
509 va_list vargs;
511 va_start(vargs, fmt);
512 trig = viio_trigger_alloc(fmt, vargs);
513 va_end(vargs);
515 return trig;
517 EXPORT_SYMBOL(iio_trigger_alloc);
519 void iio_trigger_free(struct iio_trigger *trig)
521 if (trig)
522 put_device(&trig->dev);
524 EXPORT_SYMBOL(iio_trigger_free);
526 static void devm_iio_trigger_release(struct device *dev, void *res)
528 iio_trigger_free(*(struct iio_trigger **)res);
531 static int devm_iio_trigger_match(struct device *dev, void *res, void *data)
533 struct iio_trigger **r = res;
535 if (!r || !*r) {
536 WARN_ON(!r || !*r);
537 return 0;
540 return *r == data;
544 * devm_iio_trigger_alloc - Resource-managed iio_trigger_alloc()
545 * @dev: Device to allocate iio_trigger for
546 * @fmt: trigger name format. If it includes format
547 * specifiers, the additional arguments following
548 * format are formatted and inserted in the resulting
549 * string replacing their respective specifiers.
551 * Managed iio_trigger_alloc. iio_trigger allocated with this function is
552 * automatically freed on driver detach.
554 * If an iio_trigger allocated with this function needs to be freed separately,
555 * devm_iio_trigger_free() must be used.
557 * RETURNS:
558 * Pointer to allocated iio_trigger on success, NULL on failure.
560 struct iio_trigger *devm_iio_trigger_alloc(struct device *dev,
561 const char *fmt, ...)
563 struct iio_trigger **ptr, *trig;
564 va_list vargs;
566 ptr = devres_alloc(devm_iio_trigger_release, sizeof(*ptr),
567 GFP_KERNEL);
568 if (!ptr)
569 return NULL;
571 /* use raw alloc_dr for kmalloc caller tracing */
572 va_start(vargs, fmt);
573 trig = viio_trigger_alloc(fmt, vargs);
574 va_end(vargs);
575 if (trig) {
576 *ptr = trig;
577 devres_add(dev, ptr);
578 } else {
579 devres_free(ptr);
582 return trig;
584 EXPORT_SYMBOL_GPL(devm_iio_trigger_alloc);
587 * devm_iio_trigger_free - Resource-managed iio_trigger_free()
588 * @dev: Device this iio_dev belongs to
589 * @iio_trig: the iio_trigger associated with the device
591 * Free iio_trigger allocated with devm_iio_trigger_alloc().
593 void devm_iio_trigger_free(struct device *dev, struct iio_trigger *iio_trig)
595 int rc;
597 rc = devres_release(dev, devm_iio_trigger_release,
598 devm_iio_trigger_match, iio_trig);
599 WARN_ON(rc);
601 EXPORT_SYMBOL_GPL(devm_iio_trigger_free);
603 void iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
605 indio_dev->groups[indio_dev->groupcounter++] =
606 &iio_trigger_consumer_attr_group;
609 void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev)
611 /* Clean up an associated but not attached trigger reference */
612 if (indio_dev->trig)
613 iio_trigger_put(indio_dev->trig);
616 int iio_triggered_buffer_postenable(struct iio_dev *indio_dev)
618 return iio_trigger_attach_poll_func(indio_dev->trig,
619 indio_dev->pollfunc);
621 EXPORT_SYMBOL(iio_triggered_buffer_postenable);
623 int iio_triggered_buffer_predisable(struct iio_dev *indio_dev)
625 return iio_trigger_detach_poll_func(indio_dev->trig,
626 indio_dev->pollfunc);
628 EXPORT_SYMBOL(iio_triggered_buffer_predisable);