[go: nahoru, domu]

industrialio-trigger.c revision 17666ef3c765afa9f70de1f3c733a2f7ea2f6ca3
11637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron/* The industrial I/O core, trigger handling functions
21637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron *
31637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron * Copyright (c) 2008 Jonathan Cameron
41637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron *
51637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron * This program is free software; you can redistribute it and/or modify it
61637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron * under the terms of the GNU General Public License version 2 as published by
71637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron * the Free Software Foundation.
81637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron */
91637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
101637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron#include <linux/kernel.h>
111637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron#include <linux/idr.h>
121637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron#include <linux/err.h>
131637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron#include <linux/device.h>
141637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron#include <linux/interrupt.h>
151637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron#include <linux/list.h>
165a0e3ad6af8660be21ca98a971cd00f331318c05Tejun Heo#include <linux/slab.h>
171637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
1806458e277eac2b8761b0a04d3c808d57be281a2eJonathan Cameron#include <linux/iio/iio.h>
1906458e277eac2b8761b0a04d3c808d57be281a2eJonathan Cameron#include <linux/iio/trigger.h>
20df9c1c42c26f9a516dd44c956cff301741a0884eJonathan Cameron#include "iio_core.h"
216aea1c364cde5b28b551844b7b8925f523310a18Jonathan Cameron#include "iio_core_trigger.h"
2206458e277eac2b8761b0a04d3c808d57be281a2eJonathan Cameron#include <linux/iio/trigger_consumer.h>
231637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
241637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron/* RFC - Question of approach
251637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron * Make the common case (single sensor single trigger)
261637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron * simple by starting trigger capture from when first sensors
271637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron * is added.
281637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron *
291637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron * Complex simultaneous start requires use of 'hold' functionality
301637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron * of the trigger. (not implemented)
311637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron *
321637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron * Any other suggestions?
331637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron */
341637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
3547c24fdd4253a2c8d730b978a186923b1af5e879Jonathan Cameronstatic DEFINE_IDA(iio_trigger_ida);
361637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
371637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron/* Single list of all available triggers */
381637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameronstatic LIST_HEAD(iio_trigger_list);
391637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameronstatic DEFINE_MUTEX(iio_trigger_list_lock);
401637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
411637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron/**
4259c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron * iio_trigger_read_name() - retrieve useful identifying name
4359c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron **/
4459c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameronstatic ssize_t iio_trigger_read_name(struct device *dev,
4559c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron				     struct device_attribute *attr,
4659c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron				     char *buf)
4759c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron{
48971ff1db41b0f16f34cfdeb9e0460d7dc598e68eLars-Peter Clausen	struct iio_trigger *trig = to_iio_trigger(dev);
4959c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron	return sprintf(buf, "%s\n", trig->name);
5059c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron}
5159c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron
5259c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameronstatic DEVICE_ATTR(name, S_IRUGO, iio_trigger_read_name, NULL);
5359c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron
546d459aa011cb087ed1f5c17836b032fcc670e306Lars-Peter Clausenstatic struct attribute *iio_trig_dev_attrs[] = {
556d459aa011cb087ed1f5c17836b032fcc670e306Lars-Peter Clausen	&dev_attr_name.attr,
566d459aa011cb087ed1f5c17836b032fcc670e306Lars-Peter Clausen	NULL,
576d459aa011cb087ed1f5c17836b032fcc670e306Lars-Peter Clausen};
581637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
596d459aa011cb087ed1f5c17836b032fcc670e306Lars-Peter Clausenstatic struct attribute_group iio_trig_attr_group = {
606d459aa011cb087ed1f5c17836b032fcc670e306Lars-Peter Clausen	.attrs	= iio_trig_dev_attrs,
616d459aa011cb087ed1f5c17836b032fcc670e306Lars-Peter Clausen};
626d459aa011cb087ed1f5c17836b032fcc670e306Lars-Peter Clausen
636d459aa011cb087ed1f5c17836b032fcc670e306Lars-Peter Clausenstatic const struct attribute_group *iio_trig_attr_groups[] = {
646d459aa011cb087ed1f5c17836b032fcc670e306Lars-Peter Clausen	&iio_trig_attr_group,
656d459aa011cb087ed1f5c17836b032fcc670e306Lars-Peter Clausen	NULL
666d459aa011cb087ed1f5c17836b032fcc670e306Lars-Peter Clausen};
671637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
681637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameronint iio_trigger_register(struct iio_trigger *trig_info)
691637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron{
701637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	int ret;
711637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
7247c24fdd4253a2c8d730b978a186923b1af5e879Jonathan Cameron	trig_info->id = ida_simple_get(&iio_trigger_ida, 0, 0, GFP_KERNEL);
7347c24fdd4253a2c8d730b978a186923b1af5e879Jonathan Cameron	if (trig_info->id < 0) {
7447c24fdd4253a2c8d730b978a186923b1af5e879Jonathan Cameron		ret = trig_info->id;
751637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron		goto error_ret;
7647c24fdd4253a2c8d730b978a186923b1af5e879Jonathan Cameron	}
771637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	/* Set the name used for the sysfs directory etc */
781637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	dev_set_name(&trig_info->dev, "trigger%ld",
791637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron		     (unsigned long) trig_info->id);
801637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
811637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	ret = device_add(&trig_info->dev);
821637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	if (ret)
831637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron		goto error_unregister_id;
841637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
851637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	/* Add to list of available triggers held by the IIO core */
861637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	mutex_lock(&iio_trigger_list_lock);
871637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	list_add_tail(&trig_info->list, &iio_trigger_list);
881637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	mutex_unlock(&iio_trigger_list_lock);
891637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
901637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	return 0;
911637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
921637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameronerror_unregister_id:
9347c24fdd4253a2c8d730b978a186923b1af5e879Jonathan Cameron	ida_simple_remove(&iio_trigger_ida, trig_info->id);
941637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameronerror_ret:
951637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	return ret;
961637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron}
971637db44413e32a0f100dd4bd164644e641842f7Jonathan CameronEXPORT_SYMBOL(iio_trigger_register);
981637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
991637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameronvoid iio_trigger_unregister(struct iio_trigger *trig_info)
1001637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron{
1011637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	mutex_lock(&iio_trigger_list_lock);
102582e548908729dd2f3e183fda5a8b7c2e2ca0b0eJonathan Cameron	list_del(&trig_info->list);
1031637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	mutex_unlock(&iio_trigger_list_lock);
1041637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
10547c24fdd4253a2c8d730b978a186923b1af5e879Jonathan Cameron	ida_simple_remove(&iio_trigger_ida, trig_info->id);
1061637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	/* Possible issue in here */
1078bade406649245292d6fcd1947cd7ad2ad8c80c1Jonathan Cameron	device_del(&trig_info->dev);
1081637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron}
1091637db44413e32a0f100dd4bd164644e641842f7Jonathan CameronEXPORT_SYMBOL(iio_trigger_unregister);
1101637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
111f6517f22d4f6336ce37dc6b72aae4704ff872ba3Jonathan Cameronstatic struct iio_trigger *iio_trigger_find_by_name(const char *name,
112f6517f22d4f6336ce37dc6b72aae4704ff872ba3Jonathan Cameron						    size_t len)
1131637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron{
114f6517f22d4f6336ce37dc6b72aae4704ff872ba3Jonathan Cameron	struct iio_trigger *trig = NULL, *iter;
1157c327857016116eba1595c67c25d0314d1385e85Michael Hennerich
1161637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	mutex_lock(&iio_trigger_list_lock);
117f6517f22d4f6336ce37dc6b72aae4704ff872ba3Jonathan Cameron	list_for_each_entry(iter, &iio_trigger_list, list)
118f6517f22d4f6336ce37dc6b72aae4704ff872ba3Jonathan Cameron		if (sysfs_streq(iter->name, name)) {
119f6517f22d4f6336ce37dc6b72aae4704ff872ba3Jonathan Cameron			trig = iter;
1201637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron			break;
1211637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron		}
1221637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	mutex_unlock(&iio_trigger_list_lock);
1231637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
124f6517f22d4f6336ce37dc6b72aae4704ff872ba3Jonathan Cameron	return trig;
1255f87404dfce14b2a194d085b7bac4b8c3672ab00Jonathan Cameron}
1261637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
1277b2c33b11ef003d83c87a58201ff42313e13eff4Jonathan Cameronvoid iio_trigger_poll(struct iio_trigger *trig, s64 time)
1281637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron{
129d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron	int i;
130a1a8e1dc111d6f05e7164e851e58219d428359e1Lars-Peter Clausen
131a1a8e1dc111d6f05e7164e851e58219d428359e1Lars-Peter Clausen	if (!atomic_read(&trig->use_count)) {
132a1a8e1dc111d6f05e7164e851e58219d428359e1Lars-Peter Clausen		atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
133a1a8e1dc111d6f05e7164e851e58219d428359e1Lars-Peter Clausen
134a1a8e1dc111d6f05e7164e851e58219d428359e1Lars-Peter Clausen		for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
135a1a8e1dc111d6f05e7164e851e58219d428359e1Lars-Peter Clausen			if (trig->subirqs[i].enabled)
136d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron				generic_handle_irq(trig->subirq_base + i);
137a1a8e1dc111d6f05e7164e851e58219d428359e1Lars-Peter Clausen			else
138a1a8e1dc111d6f05e7164e851e58219d428359e1Lars-Peter Clausen				iio_trigger_notify_done(trig);
139a1a8e1dc111d6f05e7164e851e58219d428359e1Lars-Peter Clausen		}
140a1a8e1dc111d6f05e7164e851e58219d428359e1Lars-Peter Clausen	}
1411637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron}
1421637db44413e32a0f100dd4bd164644e641842f7Jonathan CameronEXPORT_SYMBOL(iio_trigger_poll);
1438384d9573e937949eba96eaa1323253c2687aa5cJonathan Cameron
1448384d9573e937949eba96eaa1323253c2687aa5cJonathan Cameronirqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private)
1458384d9573e937949eba96eaa1323253c2687aa5cJonathan Cameron{
1468384d9573e937949eba96eaa1323253c2687aa5cJonathan Cameron	iio_trigger_poll(private, iio_get_time_ns());
1478384d9573e937949eba96eaa1323253c2687aa5cJonathan Cameron	return IRQ_HANDLED;
1488384d9573e937949eba96eaa1323253c2687aa5cJonathan Cameron}
1498384d9573e937949eba96eaa1323253c2687aa5cJonathan CameronEXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
1501637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
1511f785681a87068f123d3e23da13b2c55ab4f93acJonathan Cameronvoid iio_trigger_poll_chained(struct iio_trigger *trig, s64 time)
1521f785681a87068f123d3e23da13b2c55ab4f93acJonathan Cameron{
1531f785681a87068f123d3e23da13b2c55ab4f93acJonathan Cameron	int i;
154a1a8e1dc111d6f05e7164e851e58219d428359e1Lars-Peter Clausen
155a1a8e1dc111d6f05e7164e851e58219d428359e1Lars-Peter Clausen	if (!atomic_read(&trig->use_count)) {
156a1a8e1dc111d6f05e7164e851e58219d428359e1Lars-Peter Clausen		atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
157a1a8e1dc111d6f05e7164e851e58219d428359e1Lars-Peter Clausen
158a1a8e1dc111d6f05e7164e851e58219d428359e1Lars-Peter Clausen		for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
159a1a8e1dc111d6f05e7164e851e58219d428359e1Lars-Peter Clausen			if (trig->subirqs[i].enabled)
1601f785681a87068f123d3e23da13b2c55ab4f93acJonathan Cameron				handle_nested_irq(trig->subirq_base + i);
161a1a8e1dc111d6f05e7164e851e58219d428359e1Lars-Peter Clausen			else
162a1a8e1dc111d6f05e7164e851e58219d428359e1Lars-Peter Clausen				iio_trigger_notify_done(trig);
163a1a8e1dc111d6f05e7164e851e58219d428359e1Lars-Peter Clausen		}
164a1a8e1dc111d6f05e7164e851e58219d428359e1Lars-Peter Clausen	}
1651f785681a87068f123d3e23da13b2c55ab4f93acJonathan Cameron}
1661f785681a87068f123d3e23da13b2c55ab4f93acJonathan CameronEXPORT_SYMBOL(iio_trigger_poll_chained);
1671f785681a87068f123d3e23da13b2c55ab4f93acJonathan Cameron
1681637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameronvoid iio_trigger_notify_done(struct iio_trigger *trig)
1691637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron{
170a1a8e1dc111d6f05e7164e851e58219d428359e1Lars-Peter Clausen	if (atomic_dec_and_test(&trig->use_count) && trig->ops &&
171a1a8e1dc111d6f05e7164e851e58219d428359e1Lars-Peter Clausen		trig->ops->try_reenable)
172e69616b1e65cb9d3dcf34399e8fac331911abfe5Jonathan Cameron		if (trig->ops->try_reenable(trig))
173860c9c54272deaab43b40dbe416becb34abd344fPeter Meerwald			/* Missed an interrupt so launch new poll now */
1747b2c33b11ef003d83c87a58201ff42313e13eff4Jonathan Cameron			iio_trigger_poll(trig, 0);
1751637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron}
1761637db44413e32a0f100dd4bd164644e641842f7Jonathan CameronEXPORT_SYMBOL(iio_trigger_notify_done);
1771637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
1781637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron/* Trigger Consumer related functions */
179208b813c04ef7e628783cc62eeb1a140ae25bd19Jonathan Cameronstatic int iio_trigger_get_irq(struct iio_trigger *trig)
180208b813c04ef7e628783cc62eeb1a140ae25bd19Jonathan Cameron{
181208b813c04ef7e628783cc62eeb1a140ae25bd19Jonathan Cameron	int ret;
182208b813c04ef7e628783cc62eeb1a140ae25bd19Jonathan Cameron	mutex_lock(&trig->pool_lock);
183208b813c04ef7e628783cc62eeb1a140ae25bd19Jonathan Cameron	ret = bitmap_find_free_region(trig->pool,
184208b813c04ef7e628783cc62eeb1a140ae25bd19Jonathan Cameron				      CONFIG_IIO_CONSUMERS_PER_TRIGGER,
185208b813c04ef7e628783cc62eeb1a140ae25bd19Jonathan Cameron				      ilog2(1));
186208b813c04ef7e628783cc62eeb1a140ae25bd19Jonathan Cameron	mutex_unlock(&trig->pool_lock);
187208b813c04ef7e628783cc62eeb1a140ae25bd19Jonathan Cameron	if (ret >= 0)
188208b813c04ef7e628783cc62eeb1a140ae25bd19Jonathan Cameron		ret += trig->subirq_base;
189208b813c04ef7e628783cc62eeb1a140ae25bd19Jonathan Cameron
190208b813c04ef7e628783cc62eeb1a140ae25bd19Jonathan Cameron	return ret;
191208b813c04ef7e628783cc62eeb1a140ae25bd19Jonathan Cameron}
192208b813c04ef7e628783cc62eeb1a140ae25bd19Jonathan Cameron
193208b813c04ef7e628783cc62eeb1a140ae25bd19Jonathan Cameronstatic void iio_trigger_put_irq(struct iio_trigger *trig, int irq)
194208b813c04ef7e628783cc62eeb1a140ae25bd19Jonathan Cameron{
195208b813c04ef7e628783cc62eeb1a140ae25bd19Jonathan Cameron	mutex_lock(&trig->pool_lock);
196208b813c04ef7e628783cc62eeb1a140ae25bd19Jonathan Cameron	clear_bit(irq - trig->subirq_base, trig->pool);
197208b813c04ef7e628783cc62eeb1a140ae25bd19Jonathan Cameron	mutex_unlock(&trig->pool_lock);
198208b813c04ef7e628783cc62eeb1a140ae25bd19Jonathan Cameron}
1991637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
2001637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron/* Complexity in here.  With certain triggers (datardy) an acknowledgement
2011637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron * may be needed if the pollfuncs do not include the data read for the
2021637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron * triggering device.
2031637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron * This is not currently handled.  Alternative of not enabling trigger unless
2041637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron * the relevant function is in there may be the best option.
2051637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron */
206860c9c54272deaab43b40dbe416becb34abd344fPeter Meerwald/* Worth protecting against double additions? */
207208b813c04ef7e628783cc62eeb1a140ae25bd19Jonathan Cameronstatic int iio_trigger_attach_poll_func(struct iio_trigger *trig,
208208b813c04ef7e628783cc62eeb1a140ae25bd19Jonathan Cameron					struct iio_poll_func *pf)
2091637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron{
2101637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	int ret = 0;
21151c060a069376385bc0c2f8010121303d3b7bd57Jonathan Cameron	bool notinuse
21251c060a069376385bc0c2f8010121303d3b7bd57Jonathan Cameron		= bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
21351c060a069376385bc0c2f8010121303d3b7bd57Jonathan Cameron
214860c9c54272deaab43b40dbe416becb34abd344fPeter Meerwald	/* Prevent the module from being removed whilst attached to a trigger */
215f60c4a02aa05817f00408ecefdf221f44781e08aJonathan Cameron	__module_get(pf->indio_dev->info->driver_module);
21651c060a069376385bc0c2f8010121303d3b7bd57Jonathan Cameron	pf->irq = iio_trigger_get_irq(trig);
21751c060a069376385bc0c2f8010121303d3b7bd57Jonathan Cameron	ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
21851c060a069376385bc0c2f8010121303d3b7bd57Jonathan Cameron				   pf->type, pf->name,
21951c060a069376385bc0c2f8010121303d3b7bd57Jonathan Cameron				   pf);
2205dd72ecb0166498852705939163f375d693d37f3Jonathan Cameron	if (ret < 0) {
2215dd72ecb0166498852705939163f375d693d37f3Jonathan Cameron		module_put(pf->indio_dev->info->driver_module);
2225dd72ecb0166498852705939163f375d693d37f3Jonathan Cameron		return ret;
2235dd72ecb0166498852705939163f375d693d37f3Jonathan Cameron	}
2245dd72ecb0166498852705939163f375d693d37f3Jonathan Cameron
2255dd72ecb0166498852705939163f375d693d37f3Jonathan Cameron	if (trig->ops && trig->ops->set_trigger_state && notinuse) {
226d29f73db791098179af90e6a5b1df41f941b32cdJonathan Cameron		ret = trig->ops->set_trigger_state(trig, true);
2275dd72ecb0166498852705939163f375d693d37f3Jonathan Cameron		if (ret < 0)
2285dd72ecb0166498852705939163f375d693d37f3Jonathan Cameron			module_put(pf->indio_dev->info->driver_module);
2295dd72ecb0166498852705939163f375d693d37f3Jonathan Cameron	}
230d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron
2311637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	return ret;
2321637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron}
2331637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
234034bd7b5d926816285deb71c41a230b912524f8bPeter Meerwaldstatic int iio_trigger_detach_poll_func(struct iio_trigger *trig,
235208b813c04ef7e628783cc62eeb1a140ae25bd19Jonathan Cameron					 struct iio_poll_func *pf)
2361637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron{
23751c060a069376385bc0c2f8010121303d3b7bd57Jonathan Cameron	int ret = 0;
23851c060a069376385bc0c2f8010121303d3b7bd57Jonathan Cameron	bool no_other_users
23951c060a069376385bc0c2f8010121303d3b7bd57Jonathan Cameron		= (bitmap_weight(trig->pool,
24051c060a069376385bc0c2f8010121303d3b7bd57Jonathan Cameron				 CONFIG_IIO_CONSUMERS_PER_TRIGGER)
24151c060a069376385bc0c2f8010121303d3b7bd57Jonathan Cameron		   == 1);
242d29f73db791098179af90e6a5b1df41f941b32cdJonathan Cameron	if (trig->ops && trig->ops->set_trigger_state && no_other_users) {
243d29f73db791098179af90e6a5b1df41f941b32cdJonathan Cameron		ret = trig->ops->set_trigger_state(trig, false);
24451c060a069376385bc0c2f8010121303d3b7bd57Jonathan Cameron		if (ret)
24551c060a069376385bc0c2f8010121303d3b7bd57Jonathan Cameron			goto error_ret;
2461637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	}
24751c060a069376385bc0c2f8010121303d3b7bd57Jonathan Cameron	iio_trigger_put_irq(trig, pf->irq);
24851c060a069376385bc0c2f8010121303d3b7bd57Jonathan Cameron	free_irq(pf->irq, pf);
249f60c4a02aa05817f00408ecefdf221f44781e08aJonathan Cameron	module_put(pf->indio_dev->info->driver_module);
2501637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
2511637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameronerror_ret:
2521637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	return ret;
2531637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron}
2541637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
255d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameronirqreturn_t iio_pollfunc_store_time(int irq, void *p)
256d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron{
257d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron	struct iio_poll_func *pf = p;
258d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron	pf->timestamp = iio_get_time_ns();
259d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron	return IRQ_WAKE_THREAD;
260d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron}
261d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan CameronEXPORT_SYMBOL(iio_pollfunc_store_time);
262d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron
26321b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameronstruct iio_poll_func
26421b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron*iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p),
26521b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron		    irqreturn_t (*thread)(int irq, void *p),
26621b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron		    int type,
267e65bc6ac6fa54959ac0b3712b0f35bbf073c073eJonathan Cameron		    struct iio_dev *indio_dev,
26821b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron		    const char *fmt,
26921b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron		    ...)
27021b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron{
27121b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron	va_list vargs;
27221b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron	struct iio_poll_func *pf;
27321b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron
27421b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron	pf = kmalloc(sizeof *pf, GFP_KERNEL);
27521b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron	if (pf == NULL)
27621b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron		return NULL;
27721b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron	va_start(vargs, fmt);
27821b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron	pf->name = kvasprintf(GFP_KERNEL, fmt, vargs);
27921b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron	va_end(vargs);
28021b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron	if (pf->name == NULL) {
28121b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron		kfree(pf);
28221b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron		return NULL;
28321b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron	}
28421b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron	pf->h = h;
28521b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron	pf->thread = thread;
28621b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron	pf->type = type;
287e65bc6ac6fa54959ac0b3712b0f35bbf073c073eJonathan Cameron	pf->indio_dev = indio_dev;
28821b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron
28921b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron	return pf;
29021b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron}
29121b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan CameronEXPORT_SYMBOL_GPL(iio_alloc_pollfunc);
29221b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron
29321b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameronvoid iio_dealloc_pollfunc(struct iio_poll_func *pf)
29421b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron{
29521b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron	kfree(pf->name);
29621b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron	kfree(pf);
29721b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron}
29821b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan CameronEXPORT_SYMBOL_GPL(iio_dealloc_pollfunc);
29921b185f8d5951ebbf76d61f431fb541dce5113d1Jonathan Cameron
3001637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron/**
301860c9c54272deaab43b40dbe416becb34abd344fPeter Meerwald * iio_trigger_read_current() - trigger consumer sysfs query current trigger
3021637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron *
3031637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron * For trigger consumers the current_trigger interface allows the trigger
3041637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron * used by the device to be queried.
3051637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron **/
3061637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameronstatic ssize_t iio_trigger_read_current(struct device *dev,
3071637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron					struct device_attribute *attr,
3081637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron					char *buf)
3091637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron{
310e53f5ac52ec19c8c606aeacdd13fed2a03d7365aLars-Peter Clausen	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
311cb6c89a094df3823a1797b880e5dfda2fbfcd584Jonathan Cameron
312f8c6f4e9a40d47ce86a641eb20fb7c5a59f06ff0Jonathan Cameron	if (indio_dev->trig)
313f8c6f4e9a40d47ce86a641eb20fb7c5a59f06ff0Jonathan Cameron		return sprintf(buf, "%s\n", indio_dev->trig->name);
314cb6c89a094df3823a1797b880e5dfda2fbfcd584Jonathan Cameron	return 0;
3151637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron}
3161637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
3171637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron/**
318860c9c54272deaab43b40dbe416becb34abd344fPeter Meerwald * iio_trigger_write_current() - trigger consumer sysfs set current trigger
3191637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron *
3201637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron * For trigger consumers the current_trigger interface allows the trigger
32117666ef3c765afa9f70de1f3c733a2f7ea2f6ca3Peter Meerwald * used for this device to be specified at run time based on the trigger's
3221637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron * name.
3231637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron **/
3241637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameronstatic ssize_t iio_trigger_write_current(struct device *dev,
3251637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron					 struct device_attribute *attr,
3261637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron					 const char *buf,
3271637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron					 size_t len)
3281637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron{
329e53f5ac52ec19c8c606aeacdd13fed2a03d7365aLars-Peter Clausen	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
330f8c6f4e9a40d47ce86a641eb20fb7c5a59f06ff0Jonathan Cameron	struct iio_trigger *oldtrig = indio_dev->trig;
33143a4360ea8f075540cb7c1efc8d643b4261eb901Michael Hennerich	struct iio_trigger *trig;
33243a4360ea8f075540cb7c1efc8d643b4261eb901Michael Hennerich	int ret;
33343a4360ea8f075540cb7c1efc8d643b4261eb901Michael Hennerich
334f8c6f4e9a40d47ce86a641eb20fb7c5a59f06ff0Jonathan Cameron	mutex_lock(&indio_dev->mlock);
335f8c6f4e9a40d47ce86a641eb20fb7c5a59f06ff0Jonathan Cameron	if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
336f8c6f4e9a40d47ce86a641eb20fb7c5a59f06ff0Jonathan Cameron		mutex_unlock(&indio_dev->mlock);
3371637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron		return -EBUSY;
3381637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	}
339f8c6f4e9a40d47ce86a641eb20fb7c5a59f06ff0Jonathan Cameron	mutex_unlock(&indio_dev->mlock);
3401637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
34143a4360ea8f075540cb7c1efc8d643b4261eb901Michael Hennerich	trig = iio_trigger_find_by_name(buf, len);
3425dd72ecb0166498852705939163f375d693d37f3Jonathan Cameron	if (oldtrig == trig)
3435dd72ecb0166498852705939163f375d693d37f3Jonathan Cameron		return len;
34443a4360ea8f075540cb7c1efc8d643b4261eb901Michael Hennerich
345f8c6f4e9a40d47ce86a641eb20fb7c5a59f06ff0Jonathan Cameron	if (trig && indio_dev->info->validate_trigger) {
346f8c6f4e9a40d47ce86a641eb20fb7c5a59f06ff0Jonathan Cameron		ret = indio_dev->info->validate_trigger(indio_dev, trig);
34743a4360ea8f075540cb7c1efc8d643b4261eb901Michael Hennerich		if (ret)
34843a4360ea8f075540cb7c1efc8d643b4261eb901Michael Hennerich			return ret;
34943a4360ea8f075540cb7c1efc8d643b4261eb901Michael Hennerich	}
35043a4360ea8f075540cb7c1efc8d643b4261eb901Michael Hennerich
351d29f73db791098179af90e6a5b1df41f941b32cdJonathan Cameron	if (trig && trig->ops && trig->ops->validate_device) {
352f8c6f4e9a40d47ce86a641eb20fb7c5a59f06ff0Jonathan Cameron		ret = trig->ops->validate_device(trig, indio_dev);
35343a4360ea8f075540cb7c1efc8d643b4261eb901Michael Hennerich		if (ret)
35443a4360ea8f075540cb7c1efc8d643b4261eb901Michael Hennerich			return ret;
35543a4360ea8f075540cb7c1efc8d643b4261eb901Michael Hennerich	}
35643a4360ea8f075540cb7c1efc8d643b4261eb901Michael Hennerich
357f8c6f4e9a40d47ce86a641eb20fb7c5a59f06ff0Jonathan Cameron	indio_dev->trig = trig;
35843a4360ea8f075540cb7c1efc8d643b4261eb901Michael Hennerich
359f8c6f4e9a40d47ce86a641eb20fb7c5a59f06ff0Jonathan Cameron	if (oldtrig && indio_dev->trig != oldtrig)
3607cbb753701d11f3c71e8543e1ae0fc0772edac06Lars-Peter Clausen		iio_trigger_put(oldtrig);
361f8c6f4e9a40d47ce86a641eb20fb7c5a59f06ff0Jonathan Cameron	if (indio_dev->trig)
3627cbb753701d11f3c71e8543e1ae0fc0772edac06Lars-Peter Clausen		iio_trigger_get(indio_dev->trig);
3631637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
3641637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	return len;
3651637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron}
3661637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
36774112d3f3a64afb98179169cf8af691ccc63434dGreg Kroah-Hartmanstatic DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
36874112d3f3a64afb98179169cf8af691ccc63434dGreg Kroah-Hartman		   iio_trigger_read_current,
36974112d3f3a64afb98179169cf8af691ccc63434dGreg Kroah-Hartman		   iio_trigger_write_current);
3701637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
3711637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameronstatic struct attribute *iio_trigger_consumer_attrs[] = {
3721637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	&dev_attr_current_trigger.attr,
3731637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	NULL,
3741637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron};
3751637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
3761637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameronstatic const struct attribute_group iio_trigger_consumer_attr_group = {
3771637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	.name = "trigger",
3781637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	.attrs = iio_trigger_consumer_attrs,
3791637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron};
3801637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
3811637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameronstatic void iio_trig_release(struct device *device)
3821637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron{
3831637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	struct iio_trigger *trig = to_iio_trigger(device);
384d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron	int i;
385d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron
386d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron	if (trig->subirq_base) {
387d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron		for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
388d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron			irq_modify_status(trig->subirq_base + i,
389d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron					  IRQ_NOAUTOEN,
390d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron					  IRQ_NOREQUEST | IRQ_NOPROBE);
391d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron			irq_set_chip(trig->subirq_base + i,
392d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron				     NULL);
393d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron			irq_set_handler(trig->subirq_base + i,
394d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron					NULL);
395d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron		}
396d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron
397d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron		irq_free_descs(trig->subirq_base,
398d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron			       CONFIG_IIO_CONSUMERS_PER_TRIGGER);
399d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron	}
40059c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron	kfree(trig->name);
4011637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	kfree(trig);
4021637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron}
4031637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
4041637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameronstatic struct device_type iio_trig_type = {
4051637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	.release = iio_trig_release,
4066d459aa011cb087ed1f5c17836b032fcc670e306Lars-Peter Clausen	.groups = iio_trig_attr_groups,
4071637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron};
4081637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
409d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameronstatic void iio_trig_subirqmask(struct irq_data *d)
410d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron{
411d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron	struct irq_chip *chip = irq_data_get_irq_chip(d);
412d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron	struct iio_trigger *trig
413d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron		= container_of(chip,
414d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron			       struct iio_trigger, subirq_chip);
415d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron	trig->subirqs[d->irq - trig->subirq_base].enabled = false;
416d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron}
417d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron
418d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameronstatic void iio_trig_subirqunmask(struct irq_data *d)
419d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron{
420d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron	struct irq_chip *chip = irq_data_get_irq_chip(d);
421d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron	struct iio_trigger *trig
422d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron		= container_of(chip,
423d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron			       struct iio_trigger, subirq_chip);
424d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron	trig->subirqs[d->irq - trig->subirq_base].enabled = true;
425d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron}
426d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron
427d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewskistatic struct iio_trigger *viio_trigger_alloc(const char *fmt, va_list vargs)
4281637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron{
4291637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	struct iio_trigger *trig;
4301637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	trig = kzalloc(sizeof *trig, GFP_KERNEL);
4311637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	if (trig) {
432d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron		int i;
4331637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron		trig->dev.type = &iio_trig_type;
4345aaaeba82e00958ecb2c890b4953a249bbde9426Jonathan Cameron		trig->dev.bus = &iio_bus_type;
4351637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron		device_initialize(&trig->dev);
436d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron
43759c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron		mutex_init(&trig->pool_lock);
43859c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron		trig->subirq_base
43959c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron			= irq_alloc_descs(-1, 0,
44059c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron					  CONFIG_IIO_CONSUMERS_PER_TRIGGER,
44159c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron					  0);
44259c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron		if (trig->subirq_base < 0) {
44359c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron			kfree(trig);
44459c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron			return NULL;
44559c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron		}
446d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski
44759c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron		trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
44859c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron		if (trig->name == NULL) {
44959c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron			irq_free_descs(trig->subirq_base,
45059c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron				       CONFIG_IIO_CONSUMERS_PER_TRIGGER);
45159c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron			kfree(trig);
45259c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron			return NULL;
45359c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron		}
45459c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron		trig->subirq_chip.name = trig->name;
45559c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron		trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
45659c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron		trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask;
45759c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron		for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
45859c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron			irq_set_chip(trig->subirq_base + i,
45959c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron				     &trig->subirq_chip);
46059c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron			irq_set_handler(trig->subirq_base + i,
46159c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron					&handle_simple_irq);
46259c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron			irq_modify_status(trig->subirq_base + i,
46359c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron					  IRQ_NOREQUEST | IRQ_NOAUTOEN,
46459c85e82c2e7a672cb4342dc5ccf9df8a3a14f73Jonathan Cameron					  IRQ_NOPROBE);
465d96d1337e339521a2bd56dc9d51fef140c1a49eeJonathan Cameron		}
4665f9c035cae18be30b70efc88c3c88b34283db62fJonathan Cameron		get_device(&trig->dev);
4671637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	}
468d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski
469d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	return trig;
470d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski}
471d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski
472d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewskistruct iio_trigger *iio_trigger_alloc(const char *fmt, ...)
473d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski{
474d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	struct iio_trigger *trig;
475d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	va_list vargs;
476d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski
477d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	va_start(vargs, fmt);
478d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	trig = viio_trigger_alloc(fmt, vargs);
479d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	va_end(vargs);
480d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski
4811637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	return trig;
4821637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron}
4837cbb753701d11f3c71e8543e1ae0fc0772edac06Lars-Peter ClausenEXPORT_SYMBOL(iio_trigger_alloc);
4841637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
4857cbb753701d11f3c71e8543e1ae0fc0772edac06Lars-Peter Clausenvoid iio_trigger_free(struct iio_trigger *trig)
4861637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron{
4871637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron	if (trig)
4881637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron		put_device(&trig->dev);
4891637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron}
4907cbb753701d11f3c71e8543e1ae0fc0772edac06Lars-Peter ClausenEXPORT_SYMBOL(iio_trigger_free);
4911637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
492d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewskistatic void devm_iio_trigger_release(struct device *dev, void *res)
493d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski{
494d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	iio_trigger_free(*(struct iio_trigger **)res);
495d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski}
496d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski
497d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewskistatic int devm_iio_trigger_match(struct device *dev, void *res, void *data)
498d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski{
499d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	struct iio_trigger **r = res;
500d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski
501d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	if (!r || !*r) {
502d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski		WARN_ON(!r || !*r);
503d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski		return 0;
504d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	}
505d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski
506d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	return *r == data;
507d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski}
508d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski
509a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat/**
510a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat * devm_iio_trigger_alloc - Resource-managed iio_trigger_alloc()
511a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat * @dev:		Device to allocate iio_trigger for
512a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat * @fmt:		trigger name format. If it includes format
513a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat *			specifiers, the additional arguments following
514a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat *			format are formatted and inserted in the resulting
515a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat *			string replacing their respective specifiers.
516a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat *
517a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat * Managed iio_trigger_alloc.  iio_trigger allocated with this function is
518a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat * automatically freed on driver detach.
519a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat *
520a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat * If an iio_trigger allocated with this function needs to be freed separately,
521a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat * devm_iio_trigger_free() must be used.
522a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat *
523a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat * RETURNS:
524a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat * Pointer to allocated iio_trigger on success, NULL on failure.
525a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat */
526d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewskistruct iio_trigger *devm_iio_trigger_alloc(struct device *dev,
527d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski						const char *fmt, ...)
528d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski{
529d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	struct iio_trigger **ptr, *trig;
530d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	va_list vargs;
531d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski
532d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	ptr = devres_alloc(devm_iio_trigger_release, sizeof(*ptr),
533d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski			   GFP_KERNEL);
534d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	if (!ptr)
535d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski		return NULL;
536d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski
537d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	/* use raw alloc_dr for kmalloc caller tracing */
538d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	va_start(vargs, fmt);
539d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	trig = viio_trigger_alloc(fmt, vargs);
540d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	va_end(vargs);
541d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	if (trig) {
542d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski		*ptr = trig;
543d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski		devres_add(dev, ptr);
544d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	} else {
545d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski		devres_free(ptr);
546d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	}
547d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski
548d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	return trig;
549d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski}
550d536321df392ee0db6a5e92d98f0303020315f40Jacek AnaszewskiEXPORT_SYMBOL_GPL(devm_iio_trigger_alloc);
551d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski
552a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat/**
553a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat * devm_iio_trigger_free - Resource-managed iio_trigger_free()
554a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat * @dev:		Device this iio_dev belongs to
555a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat * @iio_trig:		the iio_trigger associated with the device
556a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat *
557a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat * Free iio_trigger allocated with devm_iio_trigger_alloc().
558a7e57dce02ceb8212f106191fdfbc0fcb61ce9e0Sachin Kamat */
559d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewskivoid devm_iio_trigger_free(struct device *dev, struct iio_trigger *iio_trig)
560d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski{
561d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	int rc;
562d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski
563d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	rc = devres_release(dev, devm_iio_trigger_release,
564d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski			    devm_iio_trigger_match, iio_trig);
565d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski	WARN_ON(rc);
566d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski}
567d536321df392ee0db6a5e92d98f0303020315f40Jacek AnaszewskiEXPORT_SYMBOL_GPL(devm_iio_trigger_free);
568d536321df392ee0db6a5e92d98f0303020315f40Jacek Anaszewski
56967be8e32c9f98453faeb9a992a47bd5910c9fb2eJonathan Cameronvoid iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
5701637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron{
571f8c6f4e9a40d47ce86a641eb20fb7c5a59f06ff0Jonathan Cameron	indio_dev->groups[indio_dev->groupcounter++] =
57226d25ae3f0d8ffe350aacc75b71198d6b35bd1f4Jonathan Cameron		&iio_trigger_consumer_attr_group;
5731637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron}
5741637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
575f8c6f4e9a40d47ce86a641eb20fb7c5a59f06ff0Jonathan Cameronvoid iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev)
5761637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron{
577860c9c54272deaab43b40dbe416becb34abd344fPeter Meerwald	/* Clean up an associated but not attached trigger reference */
578f8c6f4e9a40d47ce86a641eb20fb7c5a59f06ff0Jonathan Cameron	if (indio_dev->trig)
5797cbb753701d11f3c71e8543e1ae0fc0772edac06Lars-Peter Clausen		iio_trigger_put(indio_dev->trig);
5801637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron}
5811637db44413e32a0f100dd4bd164644e641842f7Jonathan Cameron
5823b99fb7656a73d483b00fd4777646c00db16b040Jonathan Cameronint iio_triggered_buffer_postenable(struct iio_dev *indio_dev)
583c3db00cc80cf01f9a2033c3f3eb03345d0024761Jonathan Cameron{
58467be8e32c9f98453faeb9a992a47bd5910c9fb2eJonathan Cameron	return iio_trigger_attach_poll_func(indio_dev->trig,
58567be8e32c9f98453faeb9a992a47bd5910c9fb2eJonathan Cameron					    indio_dev->pollfunc);
586c3db00cc80cf01f9a2033c3f3eb03345d0024761Jonathan Cameron}
5873b99fb7656a73d483b00fd4777646c00db16b040Jonathan CameronEXPORT_SYMBOL(iio_triggered_buffer_postenable);
588c3db00cc80cf01f9a2033c3f3eb03345d0024761Jonathan Cameron
5893b99fb7656a73d483b00fd4777646c00db16b040Jonathan Cameronint iio_triggered_buffer_predisable(struct iio_dev *indio_dev)
590c3db00cc80cf01f9a2033c3f3eb03345d0024761Jonathan Cameron{
591034bd7b5d926816285deb71c41a230b912524f8bPeter Meerwald	return iio_trigger_detach_poll_func(indio_dev->trig,
59267be8e32c9f98453faeb9a992a47bd5910c9fb2eJonathan Cameron					     indio_dev->pollfunc);
593c3db00cc80cf01f9a2033c3f3eb03345d0024761Jonathan Cameron}
5943b99fb7656a73d483b00fd4777646c00db16b040Jonathan CameronEXPORT_SYMBOL(iio_triggered_buffer_predisable);
595