1 // SPDX-License-Identifier: GPL-2.0-only
2 /* The industrial I/O core, trigger handling functions
3 *
4 * Copyright (c) 2008 Jonathan Cameron
5 */
6
7 #include <linux/kernel.h>
8 #include <linux/idr.h>
9 #include <linux/err.h>
10 #include <linux/device.h>
11 #include <linux/interrupt.h>
12 #include <linux/list.h>
13 #include <linux/slab.h>
14
15 #include <linux/iio/iio.h>
16 #include <linux/iio/trigger.h>
17 #include "iio_core.h"
18 #include "iio_core_trigger.h"
19 #include <linux/iio/trigger_consumer.h>
20
21 /* RFC - Question of approach
22 * Make the common case (single sensor single trigger)
23 * simple by starting trigger capture from when first sensors
24 * is added.
25 *
26 * Complex simultaneous start requires use of 'hold' functionality
27 * of the trigger. (not implemented)
28 *
29 * Any other suggestions?
30 */
31
32 static DEFINE_IDA(iio_trigger_ida);
33
34 /* Single list of all available triggers */
35 static LIST_HEAD(iio_trigger_list);
36 static DEFINE_MUTEX(iio_trigger_list_lock);
37
38 /**
39 * iio_trigger_read_name() - retrieve useful identifying name
40 * @dev: device associated with the iio_trigger
41 * @attr: pointer to the device_attribute structure that is
42 * being processed
43 * @buf: buffer to print the name into
44 *
45 * Return: a negative number on failure or the number of written
46 * characters on success.
47 */
iio_trigger_read_name(struct device * dev,struct device_attribute * attr,char * buf)48 static ssize_t iio_trigger_read_name(struct device *dev,
49 struct device_attribute *attr,
50 char *buf)
51 {
52 struct iio_trigger *trig = to_iio_trigger(dev);
53 return sysfs_emit(buf, "%s\n", trig->name);
54 }
55
56 static DEVICE_ATTR(name, S_IRUGO, iio_trigger_read_name, NULL);
57
58 static struct attribute *iio_trig_dev_attrs[] = {
59 &dev_attr_name.attr,
60 NULL,
61 };
62 ATTRIBUTE_GROUPS(iio_trig_dev);
63
64 static struct iio_trigger *__iio_trigger_find_by_name(const char *name);
65
__iio_trigger_register(struct iio_trigger * trig_info,struct module * this_mod)66 int __iio_trigger_register(struct iio_trigger *trig_info,
67 struct module *this_mod)
68 {
69 int ret;
70
71 trig_info->owner = this_mod;
72
73 trig_info->id = ida_simple_get(&iio_trigger_ida, 0, 0, GFP_KERNEL);
74 if (trig_info->id < 0)
75 return trig_info->id;
76
77 /* Set the name used for the sysfs directory etc */
78 dev_set_name(&trig_info->dev, "trigger%d", trig_info->id);
79
80 ret = device_add(&trig_info->dev);
81 if (ret)
82 goto error_unregister_id;
83
84 /* Add to list of available triggers held by the IIO core */
85 mutex_lock(&iio_trigger_list_lock);
86 if (__iio_trigger_find_by_name(trig_info->name)) {
87 pr_err("Duplicate trigger name '%s'\n", trig_info->name);
88 ret = -EEXIST;
89 goto error_device_del;
90 }
91 list_add_tail(&trig_info->list, &iio_trigger_list);
92 mutex_unlock(&iio_trigger_list_lock);
93
94 return 0;
95
96 error_device_del:
97 mutex_unlock(&iio_trigger_list_lock);
98 device_del(&trig_info->dev);
99 error_unregister_id:
100 ida_simple_remove(&iio_trigger_ida, trig_info->id);
101 return ret;
102 }
103 EXPORT_SYMBOL(__iio_trigger_register);
104
iio_trigger_unregister(struct iio_trigger * trig_info)105 void iio_trigger_unregister(struct iio_trigger *trig_info)
106 {
107 mutex_lock(&iio_trigger_list_lock);
108 list_del(&trig_info->list);
109 mutex_unlock(&iio_trigger_list_lock);
110
111 ida_simple_remove(&iio_trigger_ida, trig_info->id);
112 /* Possible issue in here */
113 device_del(&trig_info->dev);
114 }
115 EXPORT_SYMBOL(iio_trigger_unregister);
116
iio_trigger_set_immutable(struct iio_dev * indio_dev,struct iio_trigger * trig)117 int iio_trigger_set_immutable(struct iio_dev *indio_dev, struct iio_trigger *trig)
118 {
119 if (!indio_dev || !trig)
120 return -EINVAL;
121
122 mutex_lock(&indio_dev->mlock);
123 WARN_ON(indio_dev->trig_readonly);
124
125 indio_dev->trig = iio_trigger_get(trig);
126 indio_dev->trig_readonly = true;
127 mutex_unlock(&indio_dev->mlock);
128
129 return 0;
130 }
131 EXPORT_SYMBOL(iio_trigger_set_immutable);
132
133 /* Search for trigger by name, assuming iio_trigger_list_lock held */
__iio_trigger_find_by_name(const char * name)134 static struct iio_trigger *__iio_trigger_find_by_name(const char *name)
135 {
136 struct iio_trigger *iter;
137
138 list_for_each_entry(iter, &iio_trigger_list, list)
139 if (!strcmp(iter->name, name))
140 return iter;
141
142 return NULL;
143 }
144
iio_trigger_acquire_by_name(const char * name)145 static struct iio_trigger *iio_trigger_acquire_by_name(const char *name)
146 {
147 struct iio_trigger *trig = NULL, *iter;
148
149 mutex_lock(&iio_trigger_list_lock);
150 list_for_each_entry(iter, &iio_trigger_list, list)
151 if (sysfs_streq(iter->name, name)) {
152 trig = iter;
153 iio_trigger_get(trig);
154 break;
155 }
156 mutex_unlock(&iio_trigger_list_lock);
157
158 return trig;
159 }
160
iio_trigger_poll(struct iio_trigger * trig)161 void iio_trigger_poll(struct iio_trigger *trig)
162 {
163 int i;
164
165 if (!atomic_read(&trig->use_count)) {
166 atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
167
168 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
169 if (trig->subirqs[i].enabled)
170 generic_handle_irq(trig->subirq_base + i);
171 else
172 iio_trigger_notify_done(trig);
173 }
174 }
175 }
176 EXPORT_SYMBOL(iio_trigger_poll);
177
iio_trigger_generic_data_rdy_poll(int irq,void * private)178 irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private)
179 {
180 iio_trigger_poll(private);
181 return IRQ_HANDLED;
182 }
183 EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
184
iio_trigger_poll_chained(struct iio_trigger * trig)185 void iio_trigger_poll_chained(struct iio_trigger *trig)
186 {
187 int i;
188
189 if (!atomic_read(&trig->use_count)) {
190 atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
191
192 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
193 if (trig->subirqs[i].enabled)
194 handle_nested_irq(trig->subirq_base + i);
195 else
196 iio_trigger_notify_done(trig);
197 }
198 }
199 }
200 EXPORT_SYMBOL(iio_trigger_poll_chained);
201
iio_trigger_notify_done(struct iio_trigger * trig)202 void iio_trigger_notify_done(struct iio_trigger *trig)
203 {
204 if (atomic_dec_and_test(&trig->use_count) && trig->ops &&
205 trig->ops->reenable)
206 trig->ops->reenable(trig);
207 }
208 EXPORT_SYMBOL(iio_trigger_notify_done);
209
210 /* Trigger Consumer related functions */
iio_trigger_get_irq(struct iio_trigger * trig)211 static int iio_trigger_get_irq(struct iio_trigger *trig)
212 {
213 int ret;
214
215 mutex_lock(&trig->pool_lock);
216 ret = bitmap_find_free_region(trig->pool,
217 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
218 ilog2(1));
219 mutex_unlock(&trig->pool_lock);
220 if (ret >= 0)
221 ret += trig->subirq_base;
222
223 return ret;
224 }
225
iio_trigger_put_irq(struct iio_trigger * trig,int irq)226 static void iio_trigger_put_irq(struct iio_trigger *trig, int irq)
227 {
228 mutex_lock(&trig->pool_lock);
229 clear_bit(irq - trig->subirq_base, trig->pool);
230 mutex_unlock(&trig->pool_lock);
231 }
232
233 /* Complexity in here. With certain triggers (datardy) an acknowledgement
234 * may be needed if the pollfuncs do not include the data read for the
235 * triggering device.
236 * This is not currently handled. Alternative of not enabling trigger unless
237 * the relevant function is in there may be the best option.
238 */
239 /* Worth protecting against double additions? */
iio_trigger_attach_poll_func(struct iio_trigger * trig,struct iio_poll_func * pf)240 int iio_trigger_attach_poll_func(struct iio_trigger *trig,
241 struct iio_poll_func *pf)
242 {
243 bool notinuse =
244 bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
245 int ret = 0;
246
247 /* Prevent the module from being removed whilst attached to a trigger */
248 __module_get(pf->indio_dev->driver_module);
249
250 /* Get irq number */
251 pf->irq = iio_trigger_get_irq(trig);
252 if (pf->irq < 0) {
253 pr_err("Could not find an available irq for trigger %s, CONFIG_IIO_CONSUMERS_PER_TRIGGER=%d limit might be exceeded\n",
254 trig->name, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
255 goto out_put_module;
256 }
257
258 /* Request irq */
259 ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
260 pf->type, pf->name,
261 pf);
262 if (ret < 0)
263 goto out_put_irq;
264
265 /* Enable trigger in driver */
266 if (trig->ops && trig->ops->set_trigger_state && notinuse) {
267 ret = trig->ops->set_trigger_state(trig, true);
268 if (ret < 0)
269 goto out_free_irq;
270 }
271
272 /*
273 * Check if we just registered to our own trigger: we determine that
274 * this is the case if the IIO device and the trigger device share the
275 * same parent device.
276 */
277 if (pf->indio_dev->dev.parent == trig->dev.parent)
278 trig->attached_own_device = true;
279
280 return ret;
281
282 out_free_irq:
283 free_irq(pf->irq, pf);
284 out_put_irq:
285 iio_trigger_put_irq(trig, pf->irq);
286 out_put_module:
287 module_put(pf->indio_dev->driver_module);
288 return ret;
289 }
290
iio_trigger_detach_poll_func(struct iio_trigger * trig,struct iio_poll_func * pf)291 int iio_trigger_detach_poll_func(struct iio_trigger *trig,
292 struct iio_poll_func *pf)
293 {
294 bool no_other_users =
295 bitmap_weight(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER) == 1;
296 int ret = 0;
297
298 if (trig->ops && trig->ops->set_trigger_state && no_other_users) {
299 ret = trig->ops->set_trigger_state(trig, false);
300 if (ret)
301 return ret;
302 }
303 if (pf->indio_dev->dev.parent == trig->dev.parent)
304 trig->attached_own_device = false;
305 iio_trigger_put_irq(trig, pf->irq);
306 free_irq(pf->irq, pf);
307 module_put(pf->indio_dev->driver_module);
308
309 return ret;
310 }
311
iio_pollfunc_store_time(int irq,void * p)312 irqreturn_t iio_pollfunc_store_time(int irq, void *p)
313 {
314 struct iio_poll_func *pf = p;
315
316 pf->timestamp = iio_get_time_ns(pf->indio_dev);
317 return IRQ_WAKE_THREAD;
318 }
319 EXPORT_SYMBOL(iio_pollfunc_store_time);
320
321 struct iio_poll_func
iio_alloc_pollfunc(irqreturn_t (* h)(int irq,void * p),irqreturn_t (* thread)(int irq,void * p),int type,struct iio_dev * indio_dev,const char * fmt,...)322 *iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p),
323 irqreturn_t (*thread)(int irq, void *p),
324 int type,
325 struct iio_dev *indio_dev,
326 const char *fmt,
327 ...)
328 {
329 va_list vargs;
330 struct iio_poll_func *pf;
331
332 pf = kmalloc(sizeof *pf, GFP_KERNEL);
333 if (pf == NULL)
334 return NULL;
335 va_start(vargs, fmt);
336 pf->name = kvasprintf(GFP_KERNEL, fmt, vargs);
337 va_end(vargs);
338 if (pf->name == NULL) {
339 kfree(pf);
340 return NULL;
341 }
342 pf->h = h;
343 pf->thread = thread;
344 pf->type = type;
345 pf->indio_dev = indio_dev;
346
347 return pf;
348 }
349 EXPORT_SYMBOL_GPL(iio_alloc_pollfunc);
350
iio_dealloc_pollfunc(struct iio_poll_func * pf)351 void iio_dealloc_pollfunc(struct iio_poll_func *pf)
352 {
353 kfree(pf->name);
354 kfree(pf);
355 }
356 EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc);
357
358 /**
359 * iio_trigger_read_current() - trigger consumer sysfs query current trigger
360 * @dev: device associated with an industrial I/O device
361 * @attr: pointer to the device_attribute structure that
362 * is being processed
363 * @buf: buffer where the current trigger name will be printed into
364 *
365 * For trigger consumers the current_trigger interface allows the trigger
366 * used by the device to be queried.
367 *
368 * Return: a negative number on failure, the number of characters written
369 * on success or 0 if no trigger is available
370 */
iio_trigger_read_current(struct device * dev,struct device_attribute * attr,char * buf)371 static ssize_t iio_trigger_read_current(struct device *dev,
372 struct device_attribute *attr,
373 char *buf)
374 {
375 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
376
377 if (indio_dev->trig)
378 return sysfs_emit(buf, "%s\n", indio_dev->trig->name);
379 return 0;
380 }
381
382 /**
383 * iio_trigger_write_current() - trigger consumer sysfs set current trigger
384 * @dev: device associated with an industrial I/O device
385 * @attr: device attribute that is being processed
386 * @buf: string buffer that holds the name of the trigger
387 * @len: length of the trigger name held by buf
388 *
389 * For trigger consumers the current_trigger interface allows the trigger
390 * used for this device to be specified at run time based on the trigger's
391 * name.
392 *
393 * Return: negative error code on failure or length of the buffer
394 * on success
395 */
iio_trigger_write_current(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)396 static ssize_t iio_trigger_write_current(struct device *dev,
397 struct device_attribute *attr,
398 const char *buf,
399 size_t len)
400 {
401 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
402 struct iio_trigger *oldtrig = indio_dev->trig;
403 struct iio_trigger *trig;
404 int ret;
405
406 mutex_lock(&indio_dev->mlock);
407 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
408 mutex_unlock(&indio_dev->mlock);
409 return -EBUSY;
410 }
411 if (indio_dev->trig_readonly) {
412 mutex_unlock(&indio_dev->mlock);
413 return -EPERM;
414 }
415 mutex_unlock(&indio_dev->mlock);
416
417 trig = iio_trigger_acquire_by_name(buf);
418 if (oldtrig == trig) {
419 ret = len;
420 goto out_trigger_put;
421 }
422
423 if (trig && indio_dev->info->validate_trigger) {
424 ret = indio_dev->info->validate_trigger(indio_dev, trig);
425 if (ret)
426 goto out_trigger_put;
427 }
428
429 if (trig && trig->ops && trig->ops->validate_device) {
430 ret = trig->ops->validate_device(trig, indio_dev);
431 if (ret)
432 goto out_trigger_put;
433 }
434
435 indio_dev->trig = trig;
436
437 if (oldtrig) {
438 if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
439 iio_trigger_detach_poll_func(oldtrig,
440 indio_dev->pollfunc_event);
441 iio_trigger_put(oldtrig);
442 }
443 if (indio_dev->trig) {
444 if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
445 iio_trigger_attach_poll_func(indio_dev->trig,
446 indio_dev->pollfunc_event);
447 }
448
449 return len;
450
451 out_trigger_put:
452 if (trig)
453 iio_trigger_put(trig);
454 return ret;
455 }
456
457 static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
458 iio_trigger_read_current,
459 iio_trigger_write_current);
460
461 static struct attribute *iio_trigger_consumer_attrs[] = {
462 &dev_attr_current_trigger.attr,
463 NULL,
464 };
465
466 static const struct attribute_group iio_trigger_consumer_attr_group = {
467 .name = "trigger",
468 .attrs = iio_trigger_consumer_attrs,
469 };
470
iio_trig_release(struct device * device)471 static void iio_trig_release(struct device *device)
472 {
473 struct iio_trigger *trig = to_iio_trigger(device);
474 int i;
475
476 if (trig->subirq_base) {
477 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
478 irq_modify_status(trig->subirq_base + i,
479 IRQ_NOAUTOEN,
480 IRQ_NOREQUEST | IRQ_NOPROBE);
481 irq_set_chip(trig->subirq_base + i,
482 NULL);
483 irq_set_handler(trig->subirq_base + i,
484 NULL);
485 }
486
487 irq_free_descs(trig->subirq_base,
488 CONFIG_IIO_CONSUMERS_PER_TRIGGER);
489 }
490 kfree(trig->name);
491 kfree(trig);
492 }
493
494 static const struct device_type iio_trig_type = {
495 .release = iio_trig_release,
496 .groups = iio_trig_dev_groups,
497 };
498
iio_trig_subirqmask(struct irq_data * d)499 static void iio_trig_subirqmask(struct irq_data *d)
500 {
501 struct irq_chip *chip = irq_data_get_irq_chip(d);
502 struct iio_trigger *trig = container_of(chip, struct iio_trigger, subirq_chip);
503
504 trig->subirqs[d->irq - trig->subirq_base].enabled = false;
505 }
506
iio_trig_subirqunmask(struct irq_data * d)507 static void iio_trig_subirqunmask(struct irq_data *d)
508 {
509 struct irq_chip *chip = irq_data_get_irq_chip(d);
510 struct iio_trigger *trig = container_of(chip, struct iio_trigger, subirq_chip);
511
512 trig->subirqs[d->irq - trig->subirq_base].enabled = true;
513 }
514
515 static __printf(2, 0)
viio_trigger_alloc(struct device * parent,const char * fmt,va_list vargs)516 struct iio_trigger *viio_trigger_alloc(struct device *parent,
517 const char *fmt,
518 va_list vargs)
519 {
520 struct iio_trigger *trig;
521 int i;
522
523 trig = kzalloc(sizeof *trig, GFP_KERNEL);
524 if (!trig)
525 return NULL;
526
527 trig->dev.parent = parent;
528 trig->dev.type = &iio_trig_type;
529 trig->dev.bus = &iio_bus_type;
530 device_initialize(&trig->dev);
531
532 mutex_init(&trig->pool_lock);
533 trig->subirq_base = irq_alloc_descs(-1, 0,
534 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
535 0);
536 if (trig->subirq_base < 0)
537 goto free_trig;
538
539 trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
540 if (trig->name == NULL)
541 goto free_descs;
542
543 trig->subirq_chip.name = trig->name;
544 trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
545 trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask;
546 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
547 irq_set_chip(trig->subirq_base + i, &trig->subirq_chip);
548 irq_set_handler(trig->subirq_base + i, &handle_simple_irq);
549 irq_modify_status(trig->subirq_base + i,
550 IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE);
551 }
552 get_device(&trig->dev);
553
554 return trig;
555
556 free_descs:
557 irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
558 free_trig:
559 kfree(trig);
560 return NULL;
561 }
562
563 /**
564 * iio_trigger_alloc - Allocate a trigger
565 * @parent: Device to allocate iio_trigger for
566 * @fmt: trigger name format. If it includes format
567 * specifiers, the additional arguments following
568 * format are formatted and inserted in the resulting
569 * string replacing their respective specifiers.
570 * RETURNS:
571 * Pointer to allocated iio_trigger on success, NULL on failure.
572 */
iio_trigger_alloc(struct device * parent,const char * fmt,...)573 struct iio_trigger *iio_trigger_alloc(struct device *parent, const char *fmt, ...)
574 {
575 struct iio_trigger *trig;
576 va_list vargs;
577
578 va_start(vargs, fmt);
579 trig = viio_trigger_alloc(parent, fmt, vargs);
580 va_end(vargs);
581
582 return trig;
583 }
584 EXPORT_SYMBOL(iio_trigger_alloc);
585
iio_trigger_free(struct iio_trigger * trig)586 void iio_trigger_free(struct iio_trigger *trig)
587 {
588 if (trig)
589 put_device(&trig->dev);
590 }
591 EXPORT_SYMBOL(iio_trigger_free);
592
devm_iio_trigger_release(struct device * dev,void * res)593 static void devm_iio_trigger_release(struct device *dev, void *res)
594 {
595 iio_trigger_free(*(struct iio_trigger **)res);
596 }
597
598 /**
599 * devm_iio_trigger_alloc - Resource-managed iio_trigger_alloc()
600 * Managed iio_trigger_alloc. iio_trigger allocated with this function is
601 * automatically freed on driver detach.
602 * @parent: Device to allocate iio_trigger for
603 * @fmt: trigger name format. If it includes format
604 * specifiers, the additional arguments following
605 * format are formatted and inserted in the resulting
606 * string replacing their respective specifiers.
607 *
608 *
609 * RETURNS:
610 * Pointer to allocated iio_trigger on success, NULL on failure.
611 */
devm_iio_trigger_alloc(struct device * parent,const char * fmt,...)612 struct iio_trigger *devm_iio_trigger_alloc(struct device *parent, const char *fmt, ...)
613 {
614 struct iio_trigger **ptr, *trig;
615 va_list vargs;
616
617 ptr = devres_alloc(devm_iio_trigger_release, sizeof(*ptr),
618 GFP_KERNEL);
619 if (!ptr)
620 return NULL;
621
622 /* use raw alloc_dr for kmalloc caller tracing */
623 va_start(vargs, fmt);
624 trig = viio_trigger_alloc(parent, fmt, vargs);
625 va_end(vargs);
626 if (trig) {
627 *ptr = trig;
628 devres_add(parent, ptr);
629 } else {
630 devres_free(ptr);
631 }
632
633 return trig;
634 }
635 EXPORT_SYMBOL_GPL(devm_iio_trigger_alloc);
636
devm_iio_trigger_unreg(struct device * dev,void * res)637 static void devm_iio_trigger_unreg(struct device *dev, void *res)
638 {
639 iio_trigger_unregister(*(struct iio_trigger **)res);
640 }
641
642 /**
643 * __devm_iio_trigger_register - Resource-managed iio_trigger_register()
644 * @dev: device this trigger was allocated for
645 * @trig_info: trigger to register
646 * @this_mod: module registering the trigger
647 *
648 * Managed iio_trigger_register(). The IIO trigger registered with this
649 * function is automatically unregistered on driver detach. This function
650 * calls iio_trigger_register() internally. Refer to that function for more
651 * information.
652 *
653 * RETURNS:
654 * 0 on success, negative error number on failure.
655 */
__devm_iio_trigger_register(struct device * dev,struct iio_trigger * trig_info,struct module * this_mod)656 int __devm_iio_trigger_register(struct device *dev,
657 struct iio_trigger *trig_info,
658 struct module *this_mod)
659 {
660 struct iio_trigger **ptr;
661 int ret;
662
663 ptr = devres_alloc(devm_iio_trigger_unreg, sizeof(*ptr), GFP_KERNEL);
664 if (!ptr)
665 return -ENOMEM;
666
667 *ptr = trig_info;
668 ret = __iio_trigger_register(trig_info, this_mod);
669 if (!ret)
670 devres_add(dev, ptr);
671 else
672 devres_free(ptr);
673
674 return ret;
675 }
676 EXPORT_SYMBOL_GPL(__devm_iio_trigger_register);
677
iio_trigger_using_own(struct iio_dev * indio_dev)678 bool iio_trigger_using_own(struct iio_dev *indio_dev)
679 {
680 return indio_dev->trig->attached_own_device;
681 }
682 EXPORT_SYMBOL(iio_trigger_using_own);
683
684 /**
685 * iio_trigger_validate_own_device - Check if a trigger and IIO device belong to
686 * the same device
687 * @trig: The IIO trigger to check
688 * @indio_dev: the IIO device to check
689 *
690 * This function can be used as the validate_device callback for triggers that
691 * can only be attached to their own device.
692 *
693 * Return: 0 if both the trigger and the IIO device belong to the same
694 * device, -EINVAL otherwise.
695 */
iio_trigger_validate_own_device(struct iio_trigger * trig,struct iio_dev * indio_dev)696 int iio_trigger_validate_own_device(struct iio_trigger *trig,
697 struct iio_dev *indio_dev)
698 {
699 if (indio_dev->dev.parent != trig->dev.parent)
700 return -EINVAL;
701 return 0;
702 }
703 EXPORT_SYMBOL(iio_trigger_validate_own_device);
704
iio_device_register_trigger_consumer(struct iio_dev * indio_dev)705 int iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
706 {
707 return iio_device_register_sysfs_group(indio_dev,
708 &iio_trigger_consumer_attr_group);
709 }
710
iio_device_unregister_trigger_consumer(struct iio_dev * indio_dev)711 void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev)
712 {
713 /* Clean up an associated but not attached trigger reference */
714 if (indio_dev->trig)
715 iio_trigger_put(indio_dev->trig);
716 }
717