blob: 672911293987097840ec06bc64f4d90825a2909a [file] [log] [blame]
Jonathan Cameron1637db42009-08-18 18:06:26 +01001/* 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>
Jonathan Cameron1637db42009-08-18 18:06:26 +010011#include <linux/idr.h>
12#include <linux/err.h>
13#include <linux/device.h>
14#include <linux/interrupt.h>
15#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Jonathan Cameron1637db42009-08-18 18:06:26 +010017
Jonathan Cameron06458e22012-04-25 15:54:58 +010018#include <linux/iio/iio.h>
19#include <linux/iio/trigger.h>
Jonathan Camerondf9c1c42011-08-12 17:56:03 +010020#include "iio_core.h"
Jonathan Cameron6aea1c32011-08-24 17:28:38 +010021#include "iio_core_trigger.h"
Jonathan Cameron06458e22012-04-25 15:54:58 +010022#include <linux/iio/trigger_consumer.h>
Jonathan Cameron1637db42009-08-18 18:06:26 +010023
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
Jonathan Cameron47c24fd2011-08-30 12:41:07 +010035static DEFINE_IDA(iio_trigger_ida);
Jonathan Cameron1637db42009-08-18 18:06:26 +010036
37/* Single list of all available triggers */
38static LIST_HEAD(iio_trigger_list);
39static DEFINE_MUTEX(iio_trigger_list_lock);
40
41/**
Jonathan Cameron59c85e82011-05-18 14:42:22 +010042 * iio_trigger_read_name() - retrieve useful identifying name
Cristina Opriceana8e563b02015-08-06 14:56:02 +030043 * @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 */
Jonathan Cameron59c85e82011-05-18 14:42:22 +010051static ssize_t iio_trigger_read_name(struct device *dev,
52 struct device_attribute *attr,
53 char *buf)
54{
Lars-Peter Clausen971ff1d2012-06-21 19:11:00 +020055 struct iio_trigger *trig = to_iio_trigger(dev);
Jonathan Cameron59c85e82011-05-18 14:42:22 +010056 return sprintf(buf, "%s\n", trig->name);
57}
58
59static DEVICE_ATTR(name, S_IRUGO, iio_trigger_read_name, NULL);
60
Lars-Peter Clausen6d459aa2012-07-05 10:57:06 +020061static struct attribute *iio_trig_dev_attrs[] = {
62 &dev_attr_name.attr,
63 NULL,
64};
Axel Linf59c2572013-12-02 02:57:00 +000065ATTRIBUTE_GROUPS(iio_trig_dev);
Jonathan Cameron1637db42009-08-18 18:06:26 +010066
Jonathan Cameron1637db42009-08-18 18:06:26 +010067int iio_trigger_register(struct iio_trigger *trig_info)
68{
69 int ret;
70
Jonathan Cameronef2d71d2016-05-14 18:42:08 +010071 /* trig_info->ops is required for the module member */
72 if (!trig_info->ops)
73 return -EINVAL;
74
Jonathan Cameron47c24fd2011-08-30 12:41:07 +010075 trig_info->id = ida_simple_get(&iio_trigger_ida, 0, 0, GFP_KERNEL);
Hartmut Knaack92825ff2014-02-16 11:53:00 +000076 if (trig_info->id < 0)
77 return trig_info->id;
78
Jonathan Cameron1637db42009-08-18 18:06:26 +010079 /* 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
Jonathan Cameron1637db42009-08-18 18:06:26 +010087 /* 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
Jonathan Cameron1637db42009-08-18 18:06:26 +010094error_unregister_id:
Jonathan Cameron47c24fd2011-08-30 12:41:07 +010095 ida_simple_remove(&iio_trigger_ida, trig_info->id);
Jonathan Cameron1637db42009-08-18 18:06:26 +010096 return ret;
97}
98EXPORT_SYMBOL(iio_trigger_register);
99
100void iio_trigger_unregister(struct iio_trigger *trig_info)
101{
Jonathan Cameron1637db42009-08-18 18:06:26 +0100102 mutex_lock(&iio_trigger_list_lock);
Jonathan Cameron582e5482011-04-15 18:55:54 +0100103 list_del(&trig_info->list);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100104 mutex_unlock(&iio_trigger_list_lock);
105
Jonathan Cameron47c24fd2011-08-30 12:41:07 +0100106 ida_simple_remove(&iio_trigger_ida, trig_info->id);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100107 /* Possible issue in here */
Jonathan Cameron8bade402013-06-22 12:00:04 +0100108 device_del(&trig_info->dev);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100109}
110EXPORT_SYMBOL(iio_trigger_unregister);
111
Jonathan Cameronf6517f22011-04-15 18:55:53 +0100112static struct iio_trigger *iio_trigger_find_by_name(const char *name,
113 size_t len)
Jonathan Cameron1637db42009-08-18 18:06:26 +0100114{
Jonathan Cameronf6517f22011-04-15 18:55:53 +0100115 struct iio_trigger *trig = NULL, *iter;
Michael Hennerich7c327852010-04-26 10:49:10 +0200116
Jonathan Cameron1637db42009-08-18 18:06:26 +0100117 mutex_lock(&iio_trigger_list_lock);
Jonathan Cameronf6517f22011-04-15 18:55:53 +0100118 list_for_each_entry(iter, &iio_trigger_list, list)
119 if (sysfs_streq(iter->name, name)) {
120 trig = iter;
Jonathan Cameron1637db42009-08-18 18:06:26 +0100121 break;
122 }
Jonathan Cameron1637db42009-08-18 18:06:26 +0100123 mutex_unlock(&iio_trigger_list_lock);
124
Jonathan Cameronf6517f22011-04-15 18:55:53 +0100125 return trig;
Jonathan Cameron5f874042010-05-04 22:20:54 +0100126}
Jonathan Cameron1637db42009-08-18 18:06:26 +0100127
Peter Meerwald398fd222014-12-06 06:46:00 +0000128void iio_trigger_poll(struct iio_trigger *trig)
Jonathan Cameron1637db42009-08-18 18:06:26 +0100129{
Jonathan Camerond96d1332011-05-18 14:41:18 +0100130 int i;
Lars-Peter Clausena1a8e1d2013-07-16 15:28:00 +0100131
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)
Jonathan Camerond96d1332011-05-18 14:41:18 +0100137 generic_handle_irq(trig->subirq_base + i);
Lars-Peter Clausena1a8e1d2013-07-16 15:28:00 +0100138 else
139 iio_trigger_notify_done(trig);
140 }
141 }
Jonathan Cameron1637db42009-08-18 18:06:26 +0100142}
143EXPORT_SYMBOL(iio_trigger_poll);
144
Jonathan Cameron8384d952011-05-18 14:41:21 +0100145irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private)
146{
Peter Meerwald398fd222014-12-06 06:46:00 +0000147 iio_trigger_poll(private);
Jonathan Cameron8384d952011-05-18 14:41:21 +0100148 return IRQ_HANDLED;
149}
150EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
151
Peter Meerwald398fd222014-12-06 06:46:00 +0000152void iio_trigger_poll_chained(struct iio_trigger *trig)
Jonathan Cameron1f785682011-05-18 14:42:11 +0100153{
154 int i;
Lars-Peter Clausena1a8e1d2013-07-16 15:28:00 +0100155
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)
Jonathan Cameron1f785682011-05-18 14:42:11 +0100161 handle_nested_irq(trig->subirq_base + i);
Lars-Peter Clausena1a8e1d2013-07-16 15:28:00 +0100162 else
163 iio_trigger_notify_done(trig);
164 }
165 }
Jonathan Cameron1f785682011-05-18 14:42:11 +0100166}
167EXPORT_SYMBOL(iio_trigger_poll_chained);
168
Jonathan Cameron1637db42009-08-18 18:06:26 +0100169void iio_trigger_notify_done(struct iio_trigger *trig)
170{
Jonathan Cameronef2d71d2016-05-14 18:42:08 +0100171 if (atomic_dec_and_test(&trig->use_count) && trig->ops->try_reenable)
Jonathan Camerone69616b2011-10-26 17:27:39 +0100172 if (trig->ops->try_reenable(trig))
Peter Meerwald860c9c52013-02-04 11:36:00 +0000173 /* Missed an interrupt so launch new poll now */
Peter Meerwald398fd222014-12-06 06:46:00 +0000174 iio_trigger_poll(trig);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100175}
176EXPORT_SYMBOL(iio_trigger_notify_done);
177
Jonathan Cameron1637db42009-08-18 18:06:26 +0100178/* Trigger Consumer related functions */
Jonathan Cameron208b8132011-08-24 17:28:41 +0100179static 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
193static 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}
Jonathan Cameron1637db42009-08-18 18:06:26 +0100199
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 */
Peter Meerwald860c9c52013-02-04 11:36:00 +0000206/* Worth protecting against double additions? */
Jonathan Cameron208b8132011-08-24 17:28:41 +0100207static int iio_trigger_attach_poll_func(struct iio_trigger *trig,
208 struct iio_poll_func *pf)
Jonathan Cameron1637db42009-08-18 18:06:26 +0100209{
210 int ret = 0;
Jonathan Cameron51c060a2011-05-18 14:41:42 +0100211 bool notinuse
212 = bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100213
Peter Meerwald860c9c52013-02-04 11:36:00 +0000214 /* Prevent the module from being removed whilst attached to a trigger */
Jonathan Cameronf60c4a02011-08-24 17:28:37 +0100215 __module_get(pf->indio_dev->info->driver_module);
Jonathan Cameron51c060a2011-05-18 14:41:42 +0100216 pf->irq = iio_trigger_get_irq(trig);
217 ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
218 pf->type, pf->name,
219 pf);
Jonathan Cameron5dd72ec2011-10-26 17:27:41 +0100220 if (ret < 0) {
221 module_put(pf->indio_dev->info->driver_module);
222 return ret;
223 }
224
Jonathan Cameronef2d71d2016-05-14 18:42:08 +0100225 if (trig->ops->set_trigger_state && notinuse) {
Jonathan Camerond29f73d2011-08-12 17:08:38 +0100226 ret = trig->ops->set_trigger_state(trig, true);
Jonathan Cameron5dd72ec2011-10-26 17:27:41 +0100227 if (ret < 0)
228 module_put(pf->indio_dev->info->driver_module);
229 }
Jonathan Camerond96d1332011-05-18 14:41:18 +0100230
Jonathan Cameron1637db42009-08-18 18:06:26 +0100231 return ret;
232}
Jonathan Cameron1637db42009-08-18 18:06:26 +0100233
Peter Meerwald034bd7b2012-07-02 23:43:47 +0200234static int iio_trigger_detach_poll_func(struct iio_trigger *trig,
Jonathan Cameron208b8132011-08-24 17:28:41 +0100235 struct iio_poll_func *pf)
Jonathan Cameron1637db42009-08-18 18:06:26 +0100236{
Jonathan Cameron51c060a2011-05-18 14:41:42 +0100237 int ret = 0;
238 bool no_other_users
239 = (bitmap_weight(trig->pool,
240 CONFIG_IIO_CONSUMERS_PER_TRIGGER)
241 == 1);
Jonathan Cameronef2d71d2016-05-14 18:42:08 +0100242 if (trig->ops->set_trigger_state && no_other_users) {
Jonathan Camerond29f73d2011-08-12 17:08:38 +0100243 ret = trig->ops->set_trigger_state(trig, false);
Jonathan Cameron51c060a2011-05-18 14:41:42 +0100244 if (ret)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000245 return ret;
Jonathan Cameron1637db42009-08-18 18:06:26 +0100246 }
Jonathan Cameron51c060a2011-05-18 14:41:42 +0100247 iio_trigger_put_irq(trig, pf->irq);
248 free_irq(pf->irq, pf);
Jonathan Cameronf60c4a02011-08-24 17:28:37 +0100249 module_put(pf->indio_dev->info->driver_module);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100250
Jonathan Cameron1637db42009-08-18 18:06:26 +0100251 return ret;
252}
Jonathan Cameron1637db42009-08-18 18:06:26 +0100253
Jonathan Camerond96d1332011-05-18 14:41:18 +0100254irqreturn_t iio_pollfunc_store_time(int irq, void *p)
255{
256 struct iio_poll_func *pf = p;
257 pf->timestamp = iio_get_time_ns();
258 return IRQ_WAKE_THREAD;
259}
260EXPORT_SYMBOL(iio_pollfunc_store_time);
261
Jonathan Cameron21b185f2011-05-18 14:42:32 +0100262struct iio_poll_func
263*iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p),
264 irqreturn_t (*thread)(int irq, void *p),
265 int type,
Jonathan Camerone65bc6a2011-08-24 17:28:36 +0100266 struct iio_dev *indio_dev,
Jonathan Cameron21b185f2011-05-18 14:42:32 +0100267 const char *fmt,
268 ...)
269{
270 va_list vargs;
271 struct iio_poll_func *pf;
272
273 pf = kmalloc(sizeof *pf, GFP_KERNEL);
274 if (pf == NULL)
275 return NULL;
276 va_start(vargs, fmt);
277 pf->name = kvasprintf(GFP_KERNEL, fmt, vargs);
278 va_end(vargs);
279 if (pf->name == NULL) {
280 kfree(pf);
281 return NULL;
282 }
283 pf->h = h;
284 pf->thread = thread;
285 pf->type = type;
Jonathan Camerone65bc6a2011-08-24 17:28:36 +0100286 pf->indio_dev = indio_dev;
Jonathan Cameron21b185f2011-05-18 14:42:32 +0100287
288 return pf;
289}
290EXPORT_SYMBOL_GPL(iio_alloc_pollfunc);
291
292void iio_dealloc_pollfunc(struct iio_poll_func *pf)
293{
294 kfree(pf->name);
295 kfree(pf);
296}
297EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc);
298
Jonathan Cameron1637db42009-08-18 18:06:26 +0100299/**
Peter Meerwald860c9c52013-02-04 11:36:00 +0000300 * iio_trigger_read_current() - trigger consumer sysfs query current trigger
Cristina Opriceana8e563b02015-08-06 14:56:02 +0300301 * @dev: device associated with an industrial I/O device
302 * @attr: pointer to the device_attribute structure that
303 * is being processed
304 * @buf: buffer where the current trigger name will be printed into
Jonathan Cameron1637db42009-08-18 18:06:26 +0100305 *
306 * For trigger consumers the current_trigger interface allows the trigger
307 * used by the device to be queried.
Cristina Opriceana8e563b02015-08-06 14:56:02 +0300308 *
309 * Return: a negative number on failure, the number of characters written
310 * on success or 0 if no trigger is available
311 */
Jonathan Cameron1637db42009-08-18 18:06:26 +0100312static ssize_t iio_trigger_read_current(struct device *dev,
313 struct device_attribute *attr,
314 char *buf)
315{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200316 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameroncb6c89a2011-08-24 17:28:40 +0100317
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100318 if (indio_dev->trig)
319 return sprintf(buf, "%s\n", indio_dev->trig->name);
Jonathan Cameroncb6c89a2011-08-24 17:28:40 +0100320 return 0;
Jonathan Cameron1637db42009-08-18 18:06:26 +0100321}
322
323/**
Peter Meerwald860c9c52013-02-04 11:36:00 +0000324 * iio_trigger_write_current() - trigger consumer sysfs set current trigger
Cristina Opriceana8e563b02015-08-06 14:56:02 +0300325 * @dev: device associated with an industrial I/O device
326 * @attr: device attribute that is being processed
327 * @buf: string buffer that holds the name of the trigger
328 * @len: length of the trigger name held by buf
Jonathan Cameron1637db42009-08-18 18:06:26 +0100329 *
330 * For trigger consumers the current_trigger interface allows the trigger
Peter Meerwald17666ef2013-11-12 21:49:00 +0000331 * used for this device to be specified at run time based on the trigger's
Jonathan Cameron1637db42009-08-18 18:06:26 +0100332 * name.
Cristina Opriceana8e563b02015-08-06 14:56:02 +0300333 *
334 * Return: negative error code on failure or length of the buffer
335 * on success
336 */
Jonathan Cameron1637db42009-08-18 18:06:26 +0100337static ssize_t iio_trigger_write_current(struct device *dev,
338 struct device_attribute *attr,
339 const char *buf,
340 size_t len)
341{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200342 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100343 struct iio_trigger *oldtrig = indio_dev->trig;
Michael Hennerich43a43602011-06-27 13:07:09 +0100344 struct iio_trigger *trig;
345 int ret;
346
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100347 mutex_lock(&indio_dev->mlock);
348 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
349 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100350 return -EBUSY;
351 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100352 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100353
Michael Hennerich43a43602011-06-27 13:07:09 +0100354 trig = iio_trigger_find_by_name(buf, len);
Jonathan Cameron5dd72ec2011-10-26 17:27:41 +0100355 if (oldtrig == trig)
356 return len;
Michael Hennerich43a43602011-06-27 13:07:09 +0100357
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100358 if (trig && indio_dev->info->validate_trigger) {
359 ret = indio_dev->info->validate_trigger(indio_dev, trig);
Michael Hennerich43a43602011-06-27 13:07:09 +0100360 if (ret)
361 return ret;
362 }
363
Jonathan Cameronef2d71d2016-05-14 18:42:08 +0100364 if (trig && trig->ops->validate_device) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100365 ret = trig->ops->validate_device(trig, indio_dev);
Michael Hennerich43a43602011-06-27 13:07:09 +0100366 if (ret)
367 return ret;
368 }
369
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100370 indio_dev->trig = trig;
Michael Hennerich43a43602011-06-27 13:07:09 +0100371
Vladimir Barinov735ad072015-08-20 22:37:39 +0300372 if (oldtrig) {
373 if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
374 iio_trigger_detach_poll_func(oldtrig,
375 indio_dev->pollfunc_event);
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200376 iio_trigger_put(oldtrig);
Vladimir Barinov735ad072015-08-20 22:37:39 +0300377 }
378 if (indio_dev->trig) {
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200379 iio_trigger_get(indio_dev->trig);
Vladimir Barinov735ad072015-08-20 22:37:39 +0300380 if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
381 iio_trigger_attach_poll_func(indio_dev->trig,
382 indio_dev->pollfunc_event);
383 }
Jonathan Cameron1637db42009-08-18 18:06:26 +0100384
385 return len;
386}
387
Greg Kroah-Hartman74112d32010-05-04 22:34:00 -0700388static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
389 iio_trigger_read_current,
390 iio_trigger_write_current);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100391
392static struct attribute *iio_trigger_consumer_attrs[] = {
393 &dev_attr_current_trigger.attr,
394 NULL,
395};
396
397static const struct attribute_group iio_trigger_consumer_attr_group = {
398 .name = "trigger",
399 .attrs = iio_trigger_consumer_attrs,
400};
401
402static void iio_trig_release(struct device *device)
403{
404 struct iio_trigger *trig = to_iio_trigger(device);
Jonathan Camerond96d1332011-05-18 14:41:18 +0100405 int i;
406
407 if (trig->subirq_base) {
408 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
409 irq_modify_status(trig->subirq_base + i,
410 IRQ_NOAUTOEN,
411 IRQ_NOREQUEST | IRQ_NOPROBE);
412 irq_set_chip(trig->subirq_base + i,
413 NULL);
414 irq_set_handler(trig->subirq_base + i,
415 NULL);
416 }
417
418 irq_free_descs(trig->subirq_base,
419 CONFIG_IIO_CONSUMERS_PER_TRIGGER);
420 }
Jonathan Cameron59c85e82011-05-18 14:42:22 +0100421 kfree(trig->name);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100422 kfree(trig);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100423}
424
425static struct device_type iio_trig_type = {
426 .release = iio_trig_release,
Axel Linf59c2572013-12-02 02:57:00 +0000427 .groups = iio_trig_dev_groups,
Jonathan Cameron1637db42009-08-18 18:06:26 +0100428};
429
Jonathan Camerond96d1332011-05-18 14:41:18 +0100430static void iio_trig_subirqmask(struct irq_data *d)
431{
432 struct irq_chip *chip = irq_data_get_irq_chip(d);
433 struct iio_trigger *trig
434 = container_of(chip,
435 struct iio_trigger, subirq_chip);
436 trig->subirqs[d->irq - trig->subirq_base].enabled = false;
437}
438
439static void iio_trig_subirqunmask(struct irq_data *d)
440{
441 struct irq_chip *chip = irq_data_get_irq_chip(d);
442 struct iio_trigger *trig
443 = container_of(chip,
444 struct iio_trigger, subirq_chip);
445 trig->subirqs[d->irq - trig->subirq_base].enabled = true;
446}
447
Jacek Anaszewskid5363212013-08-16 14:11:00 +0100448static struct iio_trigger *viio_trigger_alloc(const char *fmt, va_list vargs)
Jonathan Cameron1637db42009-08-18 18:06:26 +0100449{
450 struct iio_trigger *trig;
451 trig = kzalloc(sizeof *trig, GFP_KERNEL);
452 if (trig) {
Jonathan Camerond96d1332011-05-18 14:41:18 +0100453 int i;
Jonathan Cameron1637db42009-08-18 18:06:26 +0100454 trig->dev.type = &iio_trig_type;
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100455 trig->dev.bus = &iio_bus_type;
Jonathan Cameron1637db42009-08-18 18:06:26 +0100456 device_initialize(&trig->dev);
Jonathan Camerond96d1332011-05-18 14:41:18 +0100457
Jonathan Cameron59c85e82011-05-18 14:42:22 +0100458 mutex_init(&trig->pool_lock);
459 trig->subirq_base
460 = irq_alloc_descs(-1, 0,
461 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
462 0);
463 if (trig->subirq_base < 0) {
464 kfree(trig);
465 return NULL;
466 }
Jacek Anaszewskid5363212013-08-16 14:11:00 +0100467
Jonathan Cameron59c85e82011-05-18 14:42:22 +0100468 trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
Jonathan Cameron59c85e82011-05-18 14:42:22 +0100469 if (trig->name == NULL) {
470 irq_free_descs(trig->subirq_base,
471 CONFIG_IIO_CONSUMERS_PER_TRIGGER);
472 kfree(trig);
473 return NULL;
474 }
475 trig->subirq_chip.name = trig->name;
476 trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
477 trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask;
478 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
479 irq_set_chip(trig->subirq_base + i,
480 &trig->subirq_chip);
481 irq_set_handler(trig->subirq_base + i,
482 &handle_simple_irq);
483 irq_modify_status(trig->subirq_base + i,
484 IRQ_NOREQUEST | IRQ_NOAUTOEN,
485 IRQ_NOPROBE);
Jonathan Camerond96d1332011-05-18 14:41:18 +0100486 }
Jonathan Cameron5f9c0352011-08-24 17:28:33 +0100487 get_device(&trig->dev);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100488 }
Jacek Anaszewskid5363212013-08-16 14:11:00 +0100489
490 return trig;
491}
492
493struct iio_trigger *iio_trigger_alloc(const char *fmt, ...)
494{
495 struct iio_trigger *trig;
496 va_list vargs;
497
498 va_start(vargs, fmt);
499 trig = viio_trigger_alloc(fmt, vargs);
500 va_end(vargs);
501
Jonathan Cameron1637db42009-08-18 18:06:26 +0100502 return trig;
503}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200504EXPORT_SYMBOL(iio_trigger_alloc);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100505
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200506void iio_trigger_free(struct iio_trigger *trig)
Jonathan Cameron1637db42009-08-18 18:06:26 +0100507{
508 if (trig)
509 put_device(&trig->dev);
510}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200511EXPORT_SYMBOL(iio_trigger_free);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100512
Jacek Anaszewskid5363212013-08-16 14:11:00 +0100513static void devm_iio_trigger_release(struct device *dev, void *res)
514{
515 iio_trigger_free(*(struct iio_trigger **)res);
516}
517
518static int devm_iio_trigger_match(struct device *dev, void *res, void *data)
519{
520 struct iio_trigger **r = res;
521
522 if (!r || !*r) {
523 WARN_ON(!r || !*r);
524 return 0;
525 }
526
527 return *r == data;
528}
529
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000530/**
531 * devm_iio_trigger_alloc - Resource-managed iio_trigger_alloc()
532 * @dev: Device to allocate iio_trigger for
533 * @fmt: trigger name format. If it includes format
534 * specifiers, the additional arguments following
535 * format are formatted and inserted in the resulting
536 * string replacing their respective specifiers.
537 *
538 * Managed iio_trigger_alloc. iio_trigger allocated with this function is
539 * automatically freed on driver detach.
540 *
541 * If an iio_trigger allocated with this function needs to be freed separately,
542 * devm_iio_trigger_free() must be used.
543 *
544 * RETURNS:
545 * Pointer to allocated iio_trigger on success, NULL on failure.
546 */
Jacek Anaszewskid5363212013-08-16 14:11:00 +0100547struct iio_trigger *devm_iio_trigger_alloc(struct device *dev,
548 const char *fmt, ...)
549{
550 struct iio_trigger **ptr, *trig;
551 va_list vargs;
552
553 ptr = devres_alloc(devm_iio_trigger_release, sizeof(*ptr),
554 GFP_KERNEL);
555 if (!ptr)
556 return NULL;
557
558 /* use raw alloc_dr for kmalloc caller tracing */
559 va_start(vargs, fmt);
560 trig = viio_trigger_alloc(fmt, vargs);
561 va_end(vargs);
562 if (trig) {
563 *ptr = trig;
564 devres_add(dev, ptr);
565 } else {
566 devres_free(ptr);
567 }
568
569 return trig;
570}
571EXPORT_SYMBOL_GPL(devm_iio_trigger_alloc);
572
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000573/**
574 * devm_iio_trigger_free - Resource-managed iio_trigger_free()
575 * @dev: Device this iio_dev belongs to
576 * @iio_trig: the iio_trigger associated with the device
577 *
578 * Free iio_trigger allocated with devm_iio_trigger_alloc().
579 */
Jacek Anaszewskid5363212013-08-16 14:11:00 +0100580void devm_iio_trigger_free(struct device *dev, struct iio_trigger *iio_trig)
581{
582 int rc;
583
584 rc = devres_release(dev, devm_iio_trigger_release,
585 devm_iio_trigger_match, iio_trig);
586 WARN_ON(rc);
587}
588EXPORT_SYMBOL_GPL(devm_iio_trigger_free);
589
Jonathan Cameron67be8e32011-10-26 17:27:38 +0100590void iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
Jonathan Cameron1637db42009-08-18 18:06:26 +0100591{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100592 indio_dev->groups[indio_dev->groupcounter++] =
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100593 &iio_trigger_consumer_attr_group;
Jonathan Cameron1637db42009-08-18 18:06:26 +0100594}
Jonathan Cameron1637db42009-08-18 18:06:26 +0100595
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100596void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev)
Jonathan Cameron1637db42009-08-18 18:06:26 +0100597{
Peter Meerwald860c9c52013-02-04 11:36:00 +0000598 /* Clean up an associated but not attached trigger reference */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100599 if (indio_dev->trig)
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200600 iio_trigger_put(indio_dev->trig);
Jonathan Cameron1637db42009-08-18 18:06:26 +0100601}
Jonathan Cameron1637db42009-08-18 18:06:26 +0100602
Jonathan Cameron3b99fb72011-09-21 11:15:53 +0100603int iio_triggered_buffer_postenable(struct iio_dev *indio_dev)
Jonathan Cameronc3db00c2010-07-11 16:39:10 +0100604{
Jonathan Cameron67be8e32011-10-26 17:27:38 +0100605 return iio_trigger_attach_poll_func(indio_dev->trig,
606 indio_dev->pollfunc);
Jonathan Cameronc3db00c2010-07-11 16:39:10 +0100607}
Jonathan Cameron3b99fb72011-09-21 11:15:53 +0100608EXPORT_SYMBOL(iio_triggered_buffer_postenable);
Jonathan Cameronc3db00c2010-07-11 16:39:10 +0100609
Jonathan Cameron3b99fb72011-09-21 11:15:53 +0100610int iio_triggered_buffer_predisable(struct iio_dev *indio_dev)
Jonathan Cameronc3db00c2010-07-11 16:39:10 +0100611{
Peter Meerwald034bd7b2012-07-02 23:43:47 +0200612 return iio_trigger_detach_poll_func(indio_dev->trig,
Jonathan Cameron67be8e32011-10-26 17:27:38 +0100613 indio_dev->pollfunc);
Jonathan Cameronc3db00c2010-07-11 16:39:10 +0100614}
Jonathan Cameron3b99fb72011-09-21 11:15:53 +0100615EXPORT_SYMBOL(iio_triggered_buffer_predisable);