7c15c3092d5d1a53eba70614f5add64c7ae164f8
[cascardo/linux.git] / drivers / iio / industrialio-trigger.c
1 /* The industrial I/O core, trigger handling functions
2  *
3  * Copyright (c) 2008 Jonathan Cameron
4  *
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  */
9
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>
17
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>
23
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.
28  *
29  * Complex simultaneous start requires use of 'hold' functionality
30  * of the trigger. (not implemented)
31  *
32  * Any other suggestions?
33  */
34
35 static DEFINE_IDA(iio_trigger_ida);
36
37 /* Single list of all available triggers */
38 static LIST_HEAD(iio_trigger_list);
39 static DEFINE_MUTEX(iio_trigger_list_lock);
40
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
47  *
48  * Return: a negative number on failure or the number of written
49  *         characters on success.
50  */
51 static ssize_t iio_trigger_read_name(struct device *dev,
52                                      struct device_attribute *attr,
53                                      char *buf)
54 {
55         struct iio_trigger *trig = to_iio_trigger(dev);
56         return sprintf(buf, "%s\n", trig->name);
57 }
58
59 static DEVICE_ATTR(name, S_IRUGO, iio_trigger_read_name, NULL);
60
61 static struct attribute *iio_trig_dev_attrs[] = {
62         &dev_attr_name.attr,
63         NULL,
64 };
65 ATTRIBUTE_GROUPS(iio_trig_dev);
66
67 int iio_trigger_register(struct iio_trigger *trig_info)
68 {
69         int ret;
70
71         /* trig_info->ops is required for the module member */
72         if (!trig_info->ops)
73                 return -EINVAL;
74
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;
78
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);
82
83         ret = device_add(&trig_info->dev);
84         if (ret)
85                 goto error_unregister_id;
86
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);
91
92         return 0;
93
94 error_unregister_id:
95         ida_simple_remove(&iio_trigger_ida, trig_info->id);
96         return ret;
97 }
98 EXPORT_SYMBOL(iio_trigger_register);
99
100 void iio_trigger_unregister(struct iio_trigger *trig_info)
101 {
102         mutex_lock(&iio_trigger_list_lock);
103         list_del(&trig_info->list);
104         mutex_unlock(&iio_trigger_list_lock);
105
106         ida_simple_remove(&iio_trigger_ida, trig_info->id);
107         /* Possible issue in here */
108         device_del(&trig_info->dev);
109 }
110 EXPORT_SYMBOL(iio_trigger_unregister);
111
112 static struct iio_trigger *iio_trigger_find_by_name(const char *name,
113                                                     size_t len)
114 {
115         struct iio_trigger *trig = NULL, *iter;
116
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;
122                 }
123         mutex_unlock(&iio_trigger_list_lock);
124
125         return trig;
126 }
127
128 void iio_trigger_poll(struct iio_trigger *trig)
129 {
130         int i;
131
132         if (!atomic_read(&trig->use_count)) {
133                 atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
134
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);
140                 }
141         }
142 }
143 EXPORT_SYMBOL(iio_trigger_poll);
144
145 irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private)
146 {
147         iio_trigger_poll(private);
148         return IRQ_HANDLED;
149 }
150 EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
151
152 void iio_trigger_poll_chained(struct iio_trigger *trig)
153 {
154         int i;
155
156         if (!atomic_read(&trig->use_count)) {
157                 atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
158
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);
164                 }
165         }
166 }
167 EXPORT_SYMBOL(iio_trigger_poll_chained);
168
169 void iio_trigger_notify_done(struct iio_trigger *trig)
170 {
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);
175 }
176 EXPORT_SYMBOL(iio_trigger_notify_done);
177
178 /* Trigger Consumer related functions */
179 static int iio_trigger_get_irq(struct iio_trigger *trig)
180 {
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;
189
190         return ret;
191 }
192
193 static void iio_trigger_put_irq(struct iio_trigger *trig, int irq)
194 {
195         mutex_lock(&trig->pool_lock);
196         clear_bit(irq - trig->subirq_base, trig->pool);
197         mutex_unlock(&trig->pool_lock);
198 }
199
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.
205  */
206 /* Worth protecting against double additions? */
207 static int iio_trigger_attach_poll_func(struct iio_trigger *trig,
208                                         struct iio_poll_func *pf)
209 {
210         int ret = 0;
211         bool notinuse
212                 = bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
213
214         /* Prevent the module from being removed whilst attached to a trigger */
215         __module_get(pf->indio_dev->info->driver_module);
216
217         /* Get irq number */
218         pf->irq = iio_trigger_get_irq(trig);
219         if (pf->irq < 0)
220                 goto out_put_module;
221
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;
228
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;
234         }
235
236         return ret;
237
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;
245 }
246
247 static int iio_trigger_detach_poll_func(struct iio_trigger *trig,
248                                          struct iio_poll_func *pf)
249 {
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;
259         }
260         iio_trigger_put_irq(trig, pf->irq);
261         free_irq(pf->irq, pf);
262         module_put(pf->indio_dev->info->driver_module);
263
264         return ret;
265 }
266
267 irqreturn_t iio_pollfunc_store_time(int irq, void *p)
268 {
269         struct iio_poll_func *pf = p;
270         pf->timestamp = iio_get_time_ns();
271         return IRQ_WAKE_THREAD;
272 }
273 EXPORT_SYMBOL(iio_pollfunc_store_time);
274
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                     ...)
282 {
283         va_list vargs;
284         struct iio_poll_func *pf;
285
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;
295         }
296         pf->h = h;
297         pf->thread = thread;
298         pf->type = type;
299         pf->indio_dev = indio_dev;
300
301         return pf;
302 }
303 EXPORT_SYMBOL_GPL(iio_alloc_pollfunc);
304
305 void iio_dealloc_pollfunc(struct iio_poll_func *pf)
306 {
307         kfree(pf->name);
308         kfree(pf);
309 }
310 EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc);
311
312 /**
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
318  *
319  * For trigger consumers the current_trigger interface allows the trigger
320  * used by the device to be queried.
321  *
322  * Return: a negative number on failure, the number of characters written
323  *         on success or 0 if no trigger is available
324  */
325 static ssize_t iio_trigger_read_current(struct device *dev,
326                                         struct device_attribute *attr,
327                                         char *buf)
328 {
329         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
330
331         if (indio_dev->trig)
332                 return sprintf(buf, "%s\n", indio_dev->trig->name);
333         return 0;
334 }
335
336 /**
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
342  *
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.
346  *
347  * Return: negative error code on failure or length of the buffer
348  *         on success
349  */
350 static ssize_t iio_trigger_write_current(struct device *dev,
351                                          struct device_attribute *attr,
352                                          const char *buf,
353                                          size_t len)
354 {
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;
359
360         mutex_lock(&indio_dev->mlock);
361         if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
362                 mutex_unlock(&indio_dev->mlock);
363                 return -EBUSY;
364         }
365         mutex_unlock(&indio_dev->mlock);
366
367         trig = iio_trigger_find_by_name(buf, len);
368         if (oldtrig == trig)
369                 return len;
370
371         if (trig && indio_dev->info->validate_trigger) {
372                 ret = indio_dev->info->validate_trigger(indio_dev, trig);
373                 if (ret)
374                         return ret;
375         }
376
377         if (trig && trig->ops->validate_device) {
378                 ret = trig->ops->validate_device(trig, indio_dev);
379                 if (ret)
380                         return ret;
381         }
382
383         indio_dev->trig = trig;
384
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);
390         }
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);
396         }
397
398         return len;
399 }
400
401 static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
402                    iio_trigger_read_current,
403                    iio_trigger_write_current);
404
405 static struct attribute *iio_trigger_consumer_attrs[] = {
406         &dev_attr_current_trigger.attr,
407         NULL,
408 };
409
410 static const struct attribute_group iio_trigger_consumer_attr_group = {
411         .name = "trigger",
412         .attrs = iio_trigger_consumer_attrs,
413 };
414
415 static void iio_trig_release(struct device *device)
416 {
417         struct iio_trigger *trig = to_iio_trigger(device);
418         int i;
419
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);
429                 }
430
431                 irq_free_descs(trig->subirq_base,
432                                CONFIG_IIO_CONSUMERS_PER_TRIGGER);
433         }
434         kfree(trig->name);
435         kfree(trig);
436 }
437
438 static struct device_type iio_trig_type = {
439         .release = iio_trig_release,
440         .groups = iio_trig_dev_groups,
441 };
442
443 static void iio_trig_subirqmask(struct irq_data *d)
444 {
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;
450 }
451
452 static void iio_trig_subirqunmask(struct irq_data *d)
453 {
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;
459 }
460
461 static struct iio_trigger *viio_trigger_alloc(const char *fmt, va_list vargs)
462 {
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);
470
471                 mutex_init(&trig->pool_lock);
472                 trig->subirq_base
473                         = irq_alloc_descs(-1, 0,
474                                           CONFIG_IIO_CONSUMERS_PER_TRIGGER,
475                                           0);
476                 if (trig->subirq_base < 0) {
477                         kfree(trig);
478                         return NULL;
479                 }
480
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;
487                 }
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);
499                 }
500                 get_device(&trig->dev);
501         }
502
503         return trig;
504 }
505
506 struct iio_trigger *iio_trigger_alloc(const char *fmt, ...)
507 {
508         struct iio_trigger *trig;
509         va_list vargs;
510
511         va_start(vargs, fmt);
512         trig = viio_trigger_alloc(fmt, vargs);
513         va_end(vargs);
514
515         return trig;
516 }
517 EXPORT_SYMBOL(iio_trigger_alloc);
518
519 void iio_trigger_free(struct iio_trigger *trig)
520 {
521         if (trig)
522                 put_device(&trig->dev);
523 }
524 EXPORT_SYMBOL(iio_trigger_free);
525
526 static void devm_iio_trigger_release(struct device *dev, void *res)
527 {
528         iio_trigger_free(*(struct iio_trigger **)res);
529 }
530
531 static int devm_iio_trigger_match(struct device *dev, void *res, void *data)
532 {
533         struct iio_trigger **r = res;
534
535         if (!r || !*r) {
536                 WARN_ON(!r || !*r);
537                 return 0;
538         }
539
540         return *r == data;
541 }
542
543 /**
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.
550  *
551  * Managed iio_trigger_alloc.  iio_trigger allocated with this function is
552  * automatically freed on driver detach.
553  *
554  * If an iio_trigger allocated with this function needs to be freed separately,
555  * devm_iio_trigger_free() must be used.
556  *
557  * RETURNS:
558  * Pointer to allocated iio_trigger on success, NULL on failure.
559  */
560 struct iio_trigger *devm_iio_trigger_alloc(struct device *dev,
561                                                 const char *fmt, ...)
562 {
563         struct iio_trigger **ptr, *trig;
564         va_list vargs;
565
566         ptr = devres_alloc(devm_iio_trigger_release, sizeof(*ptr),
567                            GFP_KERNEL);
568         if (!ptr)
569                 return NULL;
570
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);
580         }
581
582         return trig;
583 }
584 EXPORT_SYMBOL_GPL(devm_iio_trigger_alloc);
585
586 /**
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
590  *
591  * Free iio_trigger allocated with devm_iio_trigger_alloc().
592  */
593 void devm_iio_trigger_free(struct device *dev, struct iio_trigger *iio_trig)
594 {
595         int rc;
596
597         rc = devres_release(dev, devm_iio_trigger_release,
598                             devm_iio_trigger_match, iio_trig);
599         WARN_ON(rc);
600 }
601 EXPORT_SYMBOL_GPL(devm_iio_trigger_free);
602
603 void iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
604 {
605         indio_dev->groups[indio_dev->groupcounter++] =
606                 &iio_trigger_consumer_attr_group;
607 }
608
609 void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev)
610 {
611         /* Clean up an associated but not attached trigger reference */
612         if (indio_dev->trig)
613                 iio_trigger_put(indio_dev->trig);
614 }
615
616 int iio_triggered_buffer_postenable(struct iio_dev *indio_dev)
617 {
618         return iio_trigger_attach_poll_func(indio_dev->trig,
619                                             indio_dev->pollfunc);
620 }
621 EXPORT_SYMBOL(iio_triggered_buffer_postenable);
622
623 int iio_triggered_buffer_predisable(struct iio_dev *indio_dev)
624 {
625         return iio_trigger_detach_poll_func(indio_dev->trig,
626                                              indio_dev->pollfunc);
627 }
628 EXPORT_SYMBOL(iio_triggered_buffer_predisable);