staging:iio:ad7780 trivial unused header cleanup.
[deliverable/linux.git] / drivers / staging / iio / industrialio-trigger.c
1 /* The industrial I/O core, trigger handling functions
2 *
3 * Copyright (c) 2008 Jonathan Cameron
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/idr.h>
13 #include <linux/err.h>
14 #include <linux/device.h>
15 #include <linux/interrupt.h>
16 #include <linux/list.h>
17 #include <linux/slab.h>
18
19 #include "iio.h"
20 #include "trigger.h"
21 #include "trigger_consumer.h"
22
23 /* RFC - Question of approach
24 * Make the common case (single sensor single trigger)
25 * simple by starting trigger capture from when first sensors
26 * is added.
27 *
28 * Complex simultaneous start requires use of 'hold' functionality
29 * of the trigger. (not implemented)
30 *
31 * Any other suggestions?
32 */
33
34 static DEFINE_IDR(iio_trigger_idr);
35 static DEFINE_SPINLOCK(iio_trigger_idr_lock);
36
37 /* Single list of all available triggers */
38 static LIST_HEAD(iio_trigger_list);
39 static DEFINE_MUTEX(iio_trigger_list_lock);
40
41 /**
42 * iio_trigger_read_name() - retrieve useful identifying name
43 **/
44 static ssize_t iio_trigger_read_name(struct device *dev,
45 struct device_attribute *attr,
46 char *buf)
47 {
48 struct iio_trigger *trig = dev_get_drvdata(dev);
49 return sprintf(buf, "%s\n", trig->name);
50 }
51
52 static DEVICE_ATTR(name, S_IRUGO, iio_trigger_read_name, NULL);
53
54 /**
55 * iio_trigger_register_sysfs() - create a device for this trigger
56 * @trig_info: the trigger
57 *
58 * Also adds any control attribute registered by the trigger driver
59 **/
60 static int iio_trigger_register_sysfs(struct iio_trigger *trig_info)
61 {
62 return sysfs_add_file_to_group(&trig_info->dev.kobj,
63 &dev_attr_name.attr,
64 NULL);
65 }
66
67 static void iio_trigger_unregister_sysfs(struct iio_trigger *trig_info)
68 {
69 sysfs_remove_file_from_group(&trig_info->dev.kobj,
70 &dev_attr_name.attr,
71 NULL);
72 }
73
74
75 /**
76 * iio_trigger_register_id() - get a unique id for this trigger
77 * @trig_info: the trigger
78 **/
79 static int iio_trigger_register_id(struct iio_trigger *trig_info)
80 {
81 int ret = 0;
82
83 idr_again:
84 if (unlikely(idr_pre_get(&iio_trigger_idr, GFP_KERNEL) == 0))
85 return -ENOMEM;
86
87 spin_lock(&iio_trigger_idr_lock);
88 ret = idr_get_new(&iio_trigger_idr, NULL, &trig_info->id);
89 spin_unlock(&iio_trigger_idr_lock);
90 if (unlikely(ret == -EAGAIN))
91 goto idr_again;
92 else if (likely(!ret))
93 trig_info->id = trig_info->id & MAX_ID_MASK;
94
95 return ret;
96 }
97
98 /**
99 * iio_trigger_unregister_id() - free up unique id for use by another trigger
100 * @trig_info: the trigger
101 **/
102 static void iio_trigger_unregister_id(struct iio_trigger *trig_info)
103 {
104 spin_lock(&iio_trigger_idr_lock);
105 idr_remove(&iio_trigger_idr, trig_info->id);
106 spin_unlock(&iio_trigger_idr_lock);
107 }
108
109 int iio_trigger_register(struct iio_trigger *trig_info)
110 {
111 int ret;
112
113 ret = iio_trigger_register_id(trig_info);
114 if (ret)
115 goto error_ret;
116 /* Set the name used for the sysfs directory etc */
117 dev_set_name(&trig_info->dev, "trigger%ld",
118 (unsigned long) trig_info->id);
119
120 ret = device_add(&trig_info->dev);
121 if (ret)
122 goto error_unregister_id;
123
124 ret = iio_trigger_register_sysfs(trig_info);
125 if (ret)
126 goto error_device_del;
127
128 /* Add to list of available triggers held by the IIO core */
129 mutex_lock(&iio_trigger_list_lock);
130 list_add_tail(&trig_info->list, &iio_trigger_list);
131 mutex_unlock(&iio_trigger_list_lock);
132
133 return 0;
134
135 error_device_del:
136 device_del(&trig_info->dev);
137 error_unregister_id:
138 iio_trigger_unregister_id(trig_info);
139 error_ret:
140 return ret;
141 }
142 EXPORT_SYMBOL(iio_trigger_register);
143
144 void iio_trigger_unregister(struct iio_trigger *trig_info)
145 {
146 mutex_lock(&iio_trigger_list_lock);
147 list_del(&trig_info->list);
148 mutex_unlock(&iio_trigger_list_lock);
149
150 iio_trigger_unregister_sysfs(trig_info);
151 iio_trigger_unregister_id(trig_info);
152 /* Possible issue in here */
153 device_unregister(&trig_info->dev);
154 }
155 EXPORT_SYMBOL(iio_trigger_unregister);
156
157 static struct iio_trigger *iio_trigger_find_by_name(const char *name,
158 size_t len)
159 {
160 struct iio_trigger *trig = NULL, *iter;
161
162 mutex_lock(&iio_trigger_list_lock);
163 list_for_each_entry(iter, &iio_trigger_list, list)
164 if (sysfs_streq(iter->name, name)) {
165 trig = iter;
166 break;
167 }
168 mutex_unlock(&iio_trigger_list_lock);
169
170 return trig;
171 }
172
173 void iio_trigger_poll(struct iio_trigger *trig, s64 time)
174 {
175 int i;
176 if (!trig->use_count) {
177 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++)
178 if (trig->subirqs[i].enabled) {
179 trig->use_count++;
180 generic_handle_irq(trig->subirq_base + i);
181 }
182 }
183 }
184 EXPORT_SYMBOL(iio_trigger_poll);
185
186 irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private)
187 {
188 iio_trigger_poll(private, iio_get_time_ns());
189 return IRQ_HANDLED;
190 }
191 EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
192
193 void iio_trigger_poll_chained(struct iio_trigger *trig, s64 time)
194 {
195 int i;
196 if (!trig->use_count) {
197 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++)
198 if (trig->subirqs[i].enabled) {
199 trig->use_count++;
200 handle_nested_irq(trig->subirq_base + i);
201 }
202 }
203 }
204 EXPORT_SYMBOL(iio_trigger_poll_chained);
205
206 void iio_trigger_notify_done(struct iio_trigger *trig)
207 {
208 trig->use_count--;
209 if (trig->use_count == 0 && trig->try_reenable)
210 if (trig->try_reenable(trig)) {
211 /* Missed and interrupt so launch new poll now */
212 iio_trigger_poll(trig, 0);
213 }
214 }
215 EXPORT_SYMBOL(iio_trigger_notify_done);
216
217 /* Trigger Consumer related functions */
218
219 /* Complexity in here. With certain triggers (datardy) an acknowledgement
220 * may be needed if the pollfuncs do not include the data read for the
221 * triggering device.
222 * This is not currently handled. Alternative of not enabling trigger unless
223 * the relevant function is in there may be the best option.
224 */
225 /* Worth protecting against double additions?*/
226 int iio_trigger_attach_poll_func(struct iio_trigger *trig,
227 struct iio_poll_func *pf)
228 {
229 int ret = 0;
230 bool notinuse
231 = bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
232
233 pf->irq = iio_trigger_get_irq(trig);
234 ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
235 pf->type, pf->name,
236 pf);
237 if (trig->set_trigger_state && notinuse)
238 ret = trig->set_trigger_state(trig, true);
239
240 return ret;
241 }
242 EXPORT_SYMBOL(iio_trigger_attach_poll_func);
243
244 int iio_trigger_dettach_poll_func(struct iio_trigger *trig,
245 struct iio_poll_func *pf)
246 {
247 int ret = 0;
248 bool no_other_users
249 = (bitmap_weight(trig->pool,
250 CONFIG_IIO_CONSUMERS_PER_TRIGGER)
251 == 1);
252 if (trig->set_trigger_state && no_other_users) {
253 ret = trig->set_trigger_state(trig, false);
254 if (ret)
255 goto error_ret;
256 }
257 iio_trigger_put_irq(trig, pf->irq);
258 free_irq(pf->irq, pf);
259
260 error_ret:
261 return ret;
262 }
263 EXPORT_SYMBOL(iio_trigger_dettach_poll_func);
264
265 irqreturn_t iio_pollfunc_store_time(int irq, void *p)
266 {
267 struct iio_poll_func *pf = p;
268 pf->timestamp = iio_get_time_ns();
269 return IRQ_WAKE_THREAD;
270 }
271 EXPORT_SYMBOL(iio_pollfunc_store_time);
272
273 /**
274 * iio_trigger_read_currrent() - trigger consumer sysfs query which trigger
275 *
276 * For trigger consumers the current_trigger interface allows the trigger
277 * used by the device to be queried.
278 **/
279 static ssize_t iio_trigger_read_current(struct device *dev,
280 struct device_attribute *attr,
281 char *buf)
282 {
283 struct iio_dev *dev_info = dev_get_drvdata(dev);
284 int len = 0;
285 if (dev_info->trig)
286 len = sprintf(buf,
287 "%s\n",
288 dev_info->trig->name);
289 return len;
290 }
291
292 /**
293 * iio_trigger_write_current() trigger consumer sysfs set current trigger
294 *
295 * For trigger consumers the current_trigger interface allows the trigger
296 * used for this device to be specified at run time based on the triggers
297 * name.
298 **/
299 static ssize_t iio_trigger_write_current(struct device *dev,
300 struct device_attribute *attr,
301 const char *buf,
302 size_t len)
303 {
304 struct iio_dev *dev_info = dev_get_drvdata(dev);
305 struct iio_trigger *oldtrig = dev_info->trig;
306 mutex_lock(&dev_info->mlock);
307 if (dev_info->currentmode == INDIO_RING_TRIGGERED) {
308 mutex_unlock(&dev_info->mlock);
309 return -EBUSY;
310 }
311 mutex_unlock(&dev_info->mlock);
312
313 dev_info->trig = iio_trigger_find_by_name(buf, len);
314 if (oldtrig && dev_info->trig != oldtrig)
315 iio_put_trigger(oldtrig);
316 if (dev_info->trig)
317 iio_get_trigger(dev_info->trig);
318
319 return len;
320 }
321
322 static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
323 iio_trigger_read_current,
324 iio_trigger_write_current);
325
326 static struct attribute *iio_trigger_consumer_attrs[] = {
327 &dev_attr_current_trigger.attr,
328 NULL,
329 };
330
331 static const struct attribute_group iio_trigger_consumer_attr_group = {
332 .name = "trigger",
333 .attrs = iio_trigger_consumer_attrs,
334 };
335
336 static void iio_trig_release(struct device *device)
337 {
338 struct iio_trigger *trig = to_iio_trigger(device);
339 int i;
340
341 if (trig->subirq_base) {
342 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
343 irq_modify_status(trig->subirq_base + i,
344 IRQ_NOAUTOEN,
345 IRQ_NOREQUEST | IRQ_NOPROBE);
346 irq_set_chip(trig->subirq_base + i,
347 NULL);
348 irq_set_handler(trig->subirq_base + i,
349 NULL);
350 }
351
352 irq_free_descs(trig->subirq_base,
353 CONFIG_IIO_CONSUMERS_PER_TRIGGER);
354 }
355 kfree(trig->name);
356 kfree(trig);
357 iio_put();
358 }
359
360 static struct device_type iio_trig_type = {
361 .release = iio_trig_release,
362 };
363
364 static void iio_trig_subirqmask(struct irq_data *d)
365 {
366 struct irq_chip *chip = irq_data_get_irq_chip(d);
367 struct iio_trigger *trig
368 = container_of(chip,
369 struct iio_trigger, subirq_chip);
370 trig->subirqs[d->irq - trig->subirq_base].enabled = false;
371 }
372
373 static void iio_trig_subirqunmask(struct irq_data *d)
374 {
375 struct irq_chip *chip = irq_data_get_irq_chip(d);
376 struct iio_trigger *trig
377 = container_of(chip,
378 struct iio_trigger, subirq_chip);
379 trig->subirqs[d->irq - trig->subirq_base].enabled = true;
380 }
381
382 struct iio_trigger *iio_allocate_trigger(const char *fmt, ...)
383 {
384 va_list vargs;
385 struct iio_trigger *trig;
386 trig = kzalloc(sizeof *trig, GFP_KERNEL);
387 if (trig) {
388 int i;
389 trig->dev.type = &iio_trig_type;
390 trig->dev.bus = &iio_bus_type;
391 device_initialize(&trig->dev);
392 dev_set_drvdata(&trig->dev, (void *)trig);
393
394 mutex_init(&trig->pool_lock);
395 trig->subirq_base
396 = irq_alloc_descs(-1, 0,
397 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
398 0);
399 if (trig->subirq_base < 0) {
400 kfree(trig);
401 return NULL;
402 }
403 va_start(vargs, fmt);
404 trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
405 va_end(vargs);
406 if (trig->name == NULL) {
407 irq_free_descs(trig->subirq_base,
408 CONFIG_IIO_CONSUMERS_PER_TRIGGER);
409 kfree(trig);
410 return NULL;
411 }
412 trig->subirq_chip.name = trig->name;
413 trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
414 trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask;
415 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
416 irq_set_chip(trig->subirq_base + i,
417 &trig->subirq_chip);
418 irq_set_handler(trig->subirq_base + i,
419 &handle_simple_irq);
420 irq_modify_status(trig->subirq_base + i,
421 IRQ_NOREQUEST | IRQ_NOAUTOEN,
422 IRQ_NOPROBE);
423 }
424 iio_get();
425 }
426 return trig;
427 }
428 EXPORT_SYMBOL(iio_allocate_trigger);
429
430 void iio_free_trigger(struct iio_trigger *trig)
431 {
432 if (trig)
433 put_device(&trig->dev);
434 }
435 EXPORT_SYMBOL(iio_free_trigger);
436
437 int iio_device_register_trigger_consumer(struct iio_dev *dev_info)
438 {
439 int ret;
440 ret = sysfs_create_group(&dev_info->dev.kobj,
441 &iio_trigger_consumer_attr_group);
442 return ret;
443 }
444 EXPORT_SYMBOL(iio_device_register_trigger_consumer);
445
446 int iio_device_unregister_trigger_consumer(struct iio_dev *dev_info)
447 {
448 sysfs_remove_group(&dev_info->dev.kobj,
449 &iio_trigger_consumer_attr_group);
450 return 0;
451 }
452 EXPORT_SYMBOL(iio_device_unregister_trigger_consumer);
453
454 int iio_triggered_ring_postenable(struct iio_dev *indio_dev)
455 {
456 return indio_dev->trig
457 ? iio_trigger_attach_poll_func(indio_dev->trig,
458 indio_dev->pollfunc)
459 : 0;
460 }
461 EXPORT_SYMBOL(iio_triggered_ring_postenable);
462
463 int iio_triggered_ring_predisable(struct iio_dev *indio_dev)
464 {
465 return indio_dev->trig
466 ? iio_trigger_dettach_poll_func(indio_dev->trig,
467 indio_dev->pollfunc)
468 : 0;
469 }
470 EXPORT_SYMBOL(iio_triggered_ring_predisable);
This page took 0.212971 seconds and 6 git commands to generate.