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