Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 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> |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 11 | #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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 17 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 18 | #include <linux/iio/iio.h> |
| 19 | #include <linux/iio/trigger.h> |
Jonathan Cameron | df9c1c4 | 2011-08-12 17:56:03 +0100 | [diff] [blame] | 20 | #include "iio_core.h" |
Jonathan Cameron | 6aea1c3 | 2011-08-24 17:28:38 +0100 | [diff] [blame] | 21 | #include "iio_core_trigger.h" |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 22 | #include <linux/iio/trigger_consumer.h> |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 23 | |
| 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 Cameron | 47c24fd | 2011-08-30 12:41:07 +0100 | [diff] [blame] | 35 | static DEFINE_IDA(iio_trigger_ida); |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 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 | /** |
Jonathan Cameron | 59c85e8 | 2011-05-18 14:42:22 +0100 | [diff] [blame] | 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 | { |
Lars-Peter Clausen | 971ff1d | 2012-06-21 19:11:00 +0200 | [diff] [blame] | 48 | struct iio_trigger *trig = to_iio_trigger(dev); |
Jonathan Cameron | 59c85e8 | 2011-05-18 14:42:22 +0100 | [diff] [blame] | 49 | return sprintf(buf, "%s\n", trig->name); |
| 50 | } |
| 51 | |
| 52 | static DEVICE_ATTR(name, S_IRUGO, iio_trigger_read_name, NULL); |
| 53 | |
Lars-Peter Clausen | 6d459aa | 2012-07-05 10:57:06 +0200 | [diff] [blame] | 54 | static struct attribute *iio_trig_dev_attrs[] = { |
| 55 | &dev_attr_name.attr, |
| 56 | NULL, |
| 57 | }; |
Axel Lin | f59c257 | 2013-12-02 02:57:00 +0000 | [diff] [blame] | 58 | ATTRIBUTE_GROUPS(iio_trig_dev); |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 59 | |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 60 | int iio_trigger_register(struct iio_trigger *trig_info) |
| 61 | { |
| 62 | int ret; |
| 63 | |
Jonathan Cameron | 47c24fd | 2011-08-30 12:41:07 +0100 | [diff] [blame] | 64 | trig_info->id = ida_simple_get(&iio_trigger_ida, 0, 0, GFP_KERNEL); |
| 65 | if (trig_info->id < 0) { |
| 66 | ret = trig_info->id; |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 67 | goto error_ret; |
Jonathan Cameron | 47c24fd | 2011-08-30 12:41:07 +0100 | [diff] [blame] | 68 | } |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 69 | /* Set the name used for the sysfs directory etc */ |
| 70 | dev_set_name(&trig_info->dev, "trigger%ld", |
| 71 | (unsigned long) trig_info->id); |
| 72 | |
| 73 | ret = device_add(&trig_info->dev); |
| 74 | if (ret) |
| 75 | goto error_unregister_id; |
| 76 | |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 77 | /* Add to list of available triggers held by the IIO core */ |
| 78 | mutex_lock(&iio_trigger_list_lock); |
| 79 | list_add_tail(&trig_info->list, &iio_trigger_list); |
| 80 | mutex_unlock(&iio_trigger_list_lock); |
| 81 | |
| 82 | return 0; |
| 83 | |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 84 | error_unregister_id: |
Jonathan Cameron | 47c24fd | 2011-08-30 12:41:07 +0100 | [diff] [blame] | 85 | ida_simple_remove(&iio_trigger_ida, trig_info->id); |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 86 | error_ret: |
| 87 | return ret; |
| 88 | } |
| 89 | EXPORT_SYMBOL(iio_trigger_register); |
| 90 | |
| 91 | void iio_trigger_unregister(struct iio_trigger *trig_info) |
| 92 | { |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 93 | mutex_lock(&iio_trigger_list_lock); |
Jonathan Cameron | 582e548 | 2011-04-15 18:55:54 +0100 | [diff] [blame] | 94 | list_del(&trig_info->list); |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 95 | mutex_unlock(&iio_trigger_list_lock); |
| 96 | |
Jonathan Cameron | 47c24fd | 2011-08-30 12:41:07 +0100 | [diff] [blame] | 97 | ida_simple_remove(&iio_trigger_ida, trig_info->id); |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 98 | /* Possible issue in here */ |
Jonathan Cameron | 8bade40 | 2013-06-22 12:00:04 +0100 | [diff] [blame] | 99 | device_del(&trig_info->dev); |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 100 | } |
| 101 | EXPORT_SYMBOL(iio_trigger_unregister); |
| 102 | |
Jonathan Cameron | f6517f2 | 2011-04-15 18:55:53 +0100 | [diff] [blame] | 103 | static struct iio_trigger *iio_trigger_find_by_name(const char *name, |
| 104 | size_t len) |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 105 | { |
Jonathan Cameron | f6517f2 | 2011-04-15 18:55:53 +0100 | [diff] [blame] | 106 | struct iio_trigger *trig = NULL, *iter; |
Michael Hennerich | 7c32785 | 2010-04-26 10:49:10 +0200 | [diff] [blame] | 107 | |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 108 | mutex_lock(&iio_trigger_list_lock); |
Jonathan Cameron | f6517f2 | 2011-04-15 18:55:53 +0100 | [diff] [blame] | 109 | list_for_each_entry(iter, &iio_trigger_list, list) |
| 110 | if (sysfs_streq(iter->name, name)) { |
| 111 | trig = iter; |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 112 | break; |
| 113 | } |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 114 | mutex_unlock(&iio_trigger_list_lock); |
| 115 | |
Jonathan Cameron | f6517f2 | 2011-04-15 18:55:53 +0100 | [diff] [blame] | 116 | return trig; |
Jonathan Cameron | 5f87404 | 2010-05-04 22:20:54 +0100 | [diff] [blame] | 117 | } |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 118 | |
Jonathan Cameron | 7b2c33b | 2010-07-11 16:39:11 +0100 | [diff] [blame] | 119 | void iio_trigger_poll(struct iio_trigger *trig, s64 time) |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 120 | { |
Jonathan Cameron | d96d133 | 2011-05-18 14:41:18 +0100 | [diff] [blame] | 121 | int i; |
Lars-Peter Clausen | a1a8e1d | 2013-07-16 15:28:00 +0100 | [diff] [blame] | 122 | |
| 123 | if (!atomic_read(&trig->use_count)) { |
| 124 | atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER); |
| 125 | |
| 126 | for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) { |
| 127 | if (trig->subirqs[i].enabled) |
Jonathan Cameron | d96d133 | 2011-05-18 14:41:18 +0100 | [diff] [blame] | 128 | generic_handle_irq(trig->subirq_base + i); |
Lars-Peter Clausen | a1a8e1d | 2013-07-16 15:28:00 +0100 | [diff] [blame] | 129 | else |
| 130 | iio_trigger_notify_done(trig); |
| 131 | } |
| 132 | } |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 133 | } |
| 134 | EXPORT_SYMBOL(iio_trigger_poll); |
| 135 | |
Jonathan Cameron | 8384d95 | 2011-05-18 14:41:21 +0100 | [diff] [blame] | 136 | irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private) |
| 137 | { |
| 138 | iio_trigger_poll(private, iio_get_time_ns()); |
| 139 | return IRQ_HANDLED; |
| 140 | } |
| 141 | EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll); |
| 142 | |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 143 | void iio_trigger_poll_chained(struct iio_trigger *trig, s64 time) |
| 144 | { |
| 145 | int i; |
Lars-Peter Clausen | a1a8e1d | 2013-07-16 15:28:00 +0100 | [diff] [blame] | 146 | |
| 147 | if (!atomic_read(&trig->use_count)) { |
| 148 | atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER); |
| 149 | |
| 150 | for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) { |
| 151 | if (trig->subirqs[i].enabled) |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 152 | handle_nested_irq(trig->subirq_base + i); |
Lars-Peter Clausen | a1a8e1d | 2013-07-16 15:28:00 +0100 | [diff] [blame] | 153 | else |
| 154 | iio_trigger_notify_done(trig); |
| 155 | } |
| 156 | } |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 157 | } |
| 158 | EXPORT_SYMBOL(iio_trigger_poll_chained); |
| 159 | |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 160 | void iio_trigger_notify_done(struct iio_trigger *trig) |
| 161 | { |
Lars-Peter Clausen | a1a8e1d | 2013-07-16 15:28:00 +0100 | [diff] [blame] | 162 | if (atomic_dec_and_test(&trig->use_count) && trig->ops && |
| 163 | trig->ops->try_reenable) |
Jonathan Cameron | e69616b | 2011-10-26 17:27:39 +0100 | [diff] [blame] | 164 | if (trig->ops->try_reenable(trig)) |
Peter Meerwald | 860c9c5 | 2013-02-04 11:36:00 +0000 | [diff] [blame] | 165 | /* Missed an interrupt so launch new poll now */ |
Jonathan Cameron | 7b2c33b | 2010-07-11 16:39:11 +0100 | [diff] [blame] | 166 | iio_trigger_poll(trig, 0); |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 167 | } |
| 168 | EXPORT_SYMBOL(iio_trigger_notify_done); |
| 169 | |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 170 | /* Trigger Consumer related functions */ |
Jonathan Cameron | 208b813 | 2011-08-24 17:28:41 +0100 | [diff] [blame] | 171 | static int iio_trigger_get_irq(struct iio_trigger *trig) |
| 172 | { |
| 173 | int ret; |
| 174 | mutex_lock(&trig->pool_lock); |
| 175 | ret = bitmap_find_free_region(trig->pool, |
| 176 | CONFIG_IIO_CONSUMERS_PER_TRIGGER, |
| 177 | ilog2(1)); |
| 178 | mutex_unlock(&trig->pool_lock); |
| 179 | if (ret >= 0) |
| 180 | ret += trig->subirq_base; |
| 181 | |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | static void iio_trigger_put_irq(struct iio_trigger *trig, int irq) |
| 186 | { |
| 187 | mutex_lock(&trig->pool_lock); |
| 188 | clear_bit(irq - trig->subirq_base, trig->pool); |
| 189 | mutex_unlock(&trig->pool_lock); |
| 190 | } |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 191 | |
| 192 | /* Complexity in here. With certain triggers (datardy) an acknowledgement |
| 193 | * may be needed if the pollfuncs do not include the data read for the |
| 194 | * triggering device. |
| 195 | * This is not currently handled. Alternative of not enabling trigger unless |
| 196 | * the relevant function is in there may be the best option. |
| 197 | */ |
Peter Meerwald | 860c9c5 | 2013-02-04 11:36:00 +0000 | [diff] [blame] | 198 | /* Worth protecting against double additions? */ |
Jonathan Cameron | 208b813 | 2011-08-24 17:28:41 +0100 | [diff] [blame] | 199 | static int iio_trigger_attach_poll_func(struct iio_trigger *trig, |
| 200 | struct iio_poll_func *pf) |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 201 | { |
| 202 | int ret = 0; |
Jonathan Cameron | 51c060a | 2011-05-18 14:41:42 +0100 | [diff] [blame] | 203 | bool notinuse |
| 204 | = bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER); |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 205 | |
Peter Meerwald | 860c9c5 | 2013-02-04 11:36:00 +0000 | [diff] [blame] | 206 | /* Prevent the module from being removed whilst attached to a trigger */ |
Jonathan Cameron | f60c4a0 | 2011-08-24 17:28:37 +0100 | [diff] [blame] | 207 | __module_get(pf->indio_dev->info->driver_module); |
Jonathan Cameron | 51c060a | 2011-05-18 14:41:42 +0100 | [diff] [blame] | 208 | pf->irq = iio_trigger_get_irq(trig); |
| 209 | ret = request_threaded_irq(pf->irq, pf->h, pf->thread, |
| 210 | pf->type, pf->name, |
| 211 | pf); |
Jonathan Cameron | 5dd72ec | 2011-10-26 17:27:41 +0100 | [diff] [blame] | 212 | if (ret < 0) { |
| 213 | module_put(pf->indio_dev->info->driver_module); |
| 214 | return ret; |
| 215 | } |
| 216 | |
| 217 | if (trig->ops && trig->ops->set_trigger_state && notinuse) { |
Jonathan Cameron | d29f73d | 2011-08-12 17:08:38 +0100 | [diff] [blame] | 218 | ret = trig->ops->set_trigger_state(trig, true); |
Jonathan Cameron | 5dd72ec | 2011-10-26 17:27:41 +0100 | [diff] [blame] | 219 | if (ret < 0) |
| 220 | module_put(pf->indio_dev->info->driver_module); |
| 221 | } |
Jonathan Cameron | d96d133 | 2011-05-18 14:41:18 +0100 | [diff] [blame] | 222 | |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 223 | return ret; |
| 224 | } |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 225 | |
Peter Meerwald | 034bd7b | 2012-07-02 23:43:47 +0200 | [diff] [blame] | 226 | static int iio_trigger_detach_poll_func(struct iio_trigger *trig, |
Jonathan Cameron | 208b813 | 2011-08-24 17:28:41 +0100 | [diff] [blame] | 227 | struct iio_poll_func *pf) |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 228 | { |
Jonathan Cameron | 51c060a | 2011-05-18 14:41:42 +0100 | [diff] [blame] | 229 | int ret = 0; |
| 230 | bool no_other_users |
| 231 | = (bitmap_weight(trig->pool, |
| 232 | CONFIG_IIO_CONSUMERS_PER_TRIGGER) |
| 233 | == 1); |
Jonathan Cameron | d29f73d | 2011-08-12 17:08:38 +0100 | [diff] [blame] | 234 | if (trig->ops && trig->ops->set_trigger_state && no_other_users) { |
| 235 | ret = trig->ops->set_trigger_state(trig, false); |
Jonathan Cameron | 51c060a | 2011-05-18 14:41:42 +0100 | [diff] [blame] | 236 | if (ret) |
| 237 | goto error_ret; |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 238 | } |
Jonathan Cameron | 51c060a | 2011-05-18 14:41:42 +0100 | [diff] [blame] | 239 | iio_trigger_put_irq(trig, pf->irq); |
| 240 | free_irq(pf->irq, pf); |
Jonathan Cameron | f60c4a0 | 2011-08-24 17:28:37 +0100 | [diff] [blame] | 241 | module_put(pf->indio_dev->info->driver_module); |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 242 | |
| 243 | error_ret: |
| 244 | return ret; |
| 245 | } |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 246 | |
Jonathan Cameron | d96d133 | 2011-05-18 14:41:18 +0100 | [diff] [blame] | 247 | irqreturn_t iio_pollfunc_store_time(int irq, void *p) |
| 248 | { |
| 249 | struct iio_poll_func *pf = p; |
| 250 | pf->timestamp = iio_get_time_ns(); |
| 251 | return IRQ_WAKE_THREAD; |
| 252 | } |
| 253 | EXPORT_SYMBOL(iio_pollfunc_store_time); |
| 254 | |
Jonathan Cameron | 21b185f | 2011-05-18 14:42:32 +0100 | [diff] [blame] | 255 | struct iio_poll_func |
| 256 | *iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p), |
| 257 | irqreturn_t (*thread)(int irq, void *p), |
| 258 | int type, |
Jonathan Cameron | e65bc6a | 2011-08-24 17:28:36 +0100 | [diff] [blame] | 259 | struct iio_dev *indio_dev, |
Jonathan Cameron | 21b185f | 2011-05-18 14:42:32 +0100 | [diff] [blame] | 260 | const char *fmt, |
| 261 | ...) |
| 262 | { |
| 263 | va_list vargs; |
| 264 | struct iio_poll_func *pf; |
| 265 | |
| 266 | pf = kmalloc(sizeof *pf, GFP_KERNEL); |
| 267 | if (pf == NULL) |
| 268 | return NULL; |
| 269 | va_start(vargs, fmt); |
| 270 | pf->name = kvasprintf(GFP_KERNEL, fmt, vargs); |
| 271 | va_end(vargs); |
| 272 | if (pf->name == NULL) { |
| 273 | kfree(pf); |
| 274 | return NULL; |
| 275 | } |
| 276 | pf->h = h; |
| 277 | pf->thread = thread; |
| 278 | pf->type = type; |
Jonathan Cameron | e65bc6a | 2011-08-24 17:28:36 +0100 | [diff] [blame] | 279 | pf->indio_dev = indio_dev; |
Jonathan Cameron | 21b185f | 2011-05-18 14:42:32 +0100 | [diff] [blame] | 280 | |
| 281 | return pf; |
| 282 | } |
| 283 | EXPORT_SYMBOL_GPL(iio_alloc_pollfunc); |
| 284 | |
| 285 | void iio_dealloc_pollfunc(struct iio_poll_func *pf) |
| 286 | { |
| 287 | kfree(pf->name); |
| 288 | kfree(pf); |
| 289 | } |
| 290 | EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc); |
| 291 | |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 292 | /** |
Peter Meerwald | 860c9c5 | 2013-02-04 11:36:00 +0000 | [diff] [blame] | 293 | * iio_trigger_read_current() - trigger consumer sysfs query current trigger |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 294 | * |
| 295 | * For trigger consumers the current_trigger interface allows the trigger |
| 296 | * used by the device to be queried. |
| 297 | **/ |
| 298 | static ssize_t iio_trigger_read_current(struct device *dev, |
| 299 | struct device_attribute *attr, |
| 300 | char *buf) |
| 301 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 302 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | cb6c89a | 2011-08-24 17:28:40 +0100 | [diff] [blame] | 303 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 304 | if (indio_dev->trig) |
| 305 | return sprintf(buf, "%s\n", indio_dev->trig->name); |
Jonathan Cameron | cb6c89a | 2011-08-24 17:28:40 +0100 | [diff] [blame] | 306 | return 0; |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /** |
Peter Meerwald | 860c9c5 | 2013-02-04 11:36:00 +0000 | [diff] [blame] | 310 | * iio_trigger_write_current() - trigger consumer sysfs set current trigger |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 311 | * |
| 312 | * For trigger consumers the current_trigger interface allows the trigger |
Peter Meerwald | 17666ef | 2013-11-12 21:49:00 +0000 | [diff] [blame] | 313 | * used for this device to be specified at run time based on the trigger's |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 314 | * name. |
| 315 | **/ |
| 316 | static ssize_t iio_trigger_write_current(struct device *dev, |
| 317 | struct device_attribute *attr, |
| 318 | const char *buf, |
| 319 | size_t len) |
| 320 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 321 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 322 | struct iio_trigger *oldtrig = indio_dev->trig; |
Michael Hennerich | 43a4360 | 2011-06-27 13:07:09 +0100 | [diff] [blame] | 323 | struct iio_trigger *trig; |
| 324 | int ret; |
| 325 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 326 | mutex_lock(&indio_dev->mlock); |
| 327 | if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) { |
| 328 | mutex_unlock(&indio_dev->mlock); |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 329 | return -EBUSY; |
| 330 | } |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 331 | mutex_unlock(&indio_dev->mlock); |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 332 | |
Michael Hennerich | 43a4360 | 2011-06-27 13:07:09 +0100 | [diff] [blame] | 333 | trig = iio_trigger_find_by_name(buf, len); |
Jonathan Cameron | 5dd72ec | 2011-10-26 17:27:41 +0100 | [diff] [blame] | 334 | if (oldtrig == trig) |
| 335 | return len; |
Michael Hennerich | 43a4360 | 2011-06-27 13:07:09 +0100 | [diff] [blame] | 336 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 337 | if (trig && indio_dev->info->validate_trigger) { |
| 338 | ret = indio_dev->info->validate_trigger(indio_dev, trig); |
Michael Hennerich | 43a4360 | 2011-06-27 13:07:09 +0100 | [diff] [blame] | 339 | if (ret) |
| 340 | return ret; |
| 341 | } |
| 342 | |
Jonathan Cameron | d29f73d | 2011-08-12 17:08:38 +0100 | [diff] [blame] | 343 | if (trig && trig->ops && trig->ops->validate_device) { |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 344 | ret = trig->ops->validate_device(trig, indio_dev); |
Michael Hennerich | 43a4360 | 2011-06-27 13:07:09 +0100 | [diff] [blame] | 345 | if (ret) |
| 346 | return ret; |
| 347 | } |
| 348 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 349 | indio_dev->trig = trig; |
Michael Hennerich | 43a4360 | 2011-06-27 13:07:09 +0100 | [diff] [blame] | 350 | |
Peter Meerwald | a35e1fd | 2013-11-12 21:49:00 +0000 | [diff] [blame] | 351 | if (oldtrig) |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 352 | iio_trigger_put(oldtrig); |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 353 | if (indio_dev->trig) |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 354 | iio_trigger_get(indio_dev->trig); |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 355 | |
| 356 | return len; |
| 357 | } |
| 358 | |
Greg Kroah-Hartman | 74112d3 | 2010-05-04 22:34:00 -0700 | [diff] [blame] | 359 | static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR, |
| 360 | iio_trigger_read_current, |
| 361 | iio_trigger_write_current); |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 362 | |
| 363 | static struct attribute *iio_trigger_consumer_attrs[] = { |
| 364 | &dev_attr_current_trigger.attr, |
| 365 | NULL, |
| 366 | }; |
| 367 | |
| 368 | static const struct attribute_group iio_trigger_consumer_attr_group = { |
| 369 | .name = "trigger", |
| 370 | .attrs = iio_trigger_consumer_attrs, |
| 371 | }; |
| 372 | |
| 373 | static void iio_trig_release(struct device *device) |
| 374 | { |
| 375 | struct iio_trigger *trig = to_iio_trigger(device); |
Jonathan Cameron | d96d133 | 2011-05-18 14:41:18 +0100 | [diff] [blame] | 376 | int i; |
| 377 | |
| 378 | if (trig->subirq_base) { |
| 379 | for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) { |
| 380 | irq_modify_status(trig->subirq_base + i, |
| 381 | IRQ_NOAUTOEN, |
| 382 | IRQ_NOREQUEST | IRQ_NOPROBE); |
| 383 | irq_set_chip(trig->subirq_base + i, |
| 384 | NULL); |
| 385 | irq_set_handler(trig->subirq_base + i, |
| 386 | NULL); |
| 387 | } |
| 388 | |
| 389 | irq_free_descs(trig->subirq_base, |
| 390 | CONFIG_IIO_CONSUMERS_PER_TRIGGER); |
| 391 | } |
Jonathan Cameron | 59c85e8 | 2011-05-18 14:42:22 +0100 | [diff] [blame] | 392 | kfree(trig->name); |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 393 | kfree(trig); |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | static struct device_type iio_trig_type = { |
| 397 | .release = iio_trig_release, |
Axel Lin | f59c257 | 2013-12-02 02:57:00 +0000 | [diff] [blame] | 398 | .groups = iio_trig_dev_groups, |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 399 | }; |
| 400 | |
Jonathan Cameron | d96d133 | 2011-05-18 14:41:18 +0100 | [diff] [blame] | 401 | static void iio_trig_subirqmask(struct irq_data *d) |
| 402 | { |
| 403 | struct irq_chip *chip = irq_data_get_irq_chip(d); |
| 404 | struct iio_trigger *trig |
| 405 | = container_of(chip, |
| 406 | struct iio_trigger, subirq_chip); |
| 407 | trig->subirqs[d->irq - trig->subirq_base].enabled = false; |
| 408 | } |
| 409 | |
| 410 | static void iio_trig_subirqunmask(struct irq_data *d) |
| 411 | { |
| 412 | struct irq_chip *chip = irq_data_get_irq_chip(d); |
| 413 | struct iio_trigger *trig |
| 414 | = container_of(chip, |
| 415 | struct iio_trigger, subirq_chip); |
| 416 | trig->subirqs[d->irq - trig->subirq_base].enabled = true; |
| 417 | } |
| 418 | |
Jacek Anaszewski | d536321 | 2013-08-16 14:11:00 +0100 | [diff] [blame] | 419 | static struct iio_trigger *viio_trigger_alloc(const char *fmt, va_list vargs) |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 420 | { |
| 421 | struct iio_trigger *trig; |
| 422 | trig = kzalloc(sizeof *trig, GFP_KERNEL); |
| 423 | if (trig) { |
Jonathan Cameron | d96d133 | 2011-05-18 14:41:18 +0100 | [diff] [blame] | 424 | int i; |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 425 | trig->dev.type = &iio_trig_type; |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 426 | trig->dev.bus = &iio_bus_type; |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 427 | device_initialize(&trig->dev); |
Jonathan Cameron | d96d133 | 2011-05-18 14:41:18 +0100 | [diff] [blame] | 428 | |
Jonathan Cameron | 59c85e8 | 2011-05-18 14:42:22 +0100 | [diff] [blame] | 429 | mutex_init(&trig->pool_lock); |
| 430 | trig->subirq_base |
| 431 | = irq_alloc_descs(-1, 0, |
| 432 | CONFIG_IIO_CONSUMERS_PER_TRIGGER, |
| 433 | 0); |
| 434 | if (trig->subirq_base < 0) { |
| 435 | kfree(trig); |
| 436 | return NULL; |
| 437 | } |
Jacek Anaszewski | d536321 | 2013-08-16 14:11:00 +0100 | [diff] [blame] | 438 | |
Jonathan Cameron | 59c85e8 | 2011-05-18 14:42:22 +0100 | [diff] [blame] | 439 | trig->name = kvasprintf(GFP_KERNEL, fmt, vargs); |
Jonathan Cameron | 59c85e8 | 2011-05-18 14:42:22 +0100 | [diff] [blame] | 440 | if (trig->name == NULL) { |
| 441 | irq_free_descs(trig->subirq_base, |
| 442 | CONFIG_IIO_CONSUMERS_PER_TRIGGER); |
| 443 | kfree(trig); |
| 444 | return NULL; |
| 445 | } |
| 446 | trig->subirq_chip.name = trig->name; |
| 447 | trig->subirq_chip.irq_mask = &iio_trig_subirqmask; |
| 448 | trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask; |
| 449 | for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) { |
| 450 | irq_set_chip(trig->subirq_base + i, |
| 451 | &trig->subirq_chip); |
| 452 | irq_set_handler(trig->subirq_base + i, |
| 453 | &handle_simple_irq); |
| 454 | irq_modify_status(trig->subirq_base + i, |
| 455 | IRQ_NOREQUEST | IRQ_NOAUTOEN, |
| 456 | IRQ_NOPROBE); |
Jonathan Cameron | d96d133 | 2011-05-18 14:41:18 +0100 | [diff] [blame] | 457 | } |
Jonathan Cameron | 5f9c035 | 2011-08-24 17:28:33 +0100 | [diff] [blame] | 458 | get_device(&trig->dev); |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 459 | } |
Jacek Anaszewski | d536321 | 2013-08-16 14:11:00 +0100 | [diff] [blame] | 460 | |
| 461 | return trig; |
| 462 | } |
| 463 | |
| 464 | struct iio_trigger *iio_trigger_alloc(const char *fmt, ...) |
| 465 | { |
| 466 | struct iio_trigger *trig; |
| 467 | va_list vargs; |
| 468 | |
| 469 | va_start(vargs, fmt); |
| 470 | trig = viio_trigger_alloc(fmt, vargs); |
| 471 | va_end(vargs); |
| 472 | |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 473 | return trig; |
| 474 | } |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 475 | EXPORT_SYMBOL(iio_trigger_alloc); |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 476 | |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 477 | void iio_trigger_free(struct iio_trigger *trig) |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 478 | { |
| 479 | if (trig) |
| 480 | put_device(&trig->dev); |
| 481 | } |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 482 | EXPORT_SYMBOL(iio_trigger_free); |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 483 | |
Jacek Anaszewski | d536321 | 2013-08-16 14:11:00 +0100 | [diff] [blame] | 484 | static void devm_iio_trigger_release(struct device *dev, void *res) |
| 485 | { |
| 486 | iio_trigger_free(*(struct iio_trigger **)res); |
| 487 | } |
| 488 | |
| 489 | static int devm_iio_trigger_match(struct device *dev, void *res, void *data) |
| 490 | { |
| 491 | struct iio_trigger **r = res; |
| 492 | |
| 493 | if (!r || !*r) { |
| 494 | WARN_ON(!r || !*r); |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | return *r == data; |
| 499 | } |
| 500 | |
Sachin Kamat | a7e57dc | 2013-10-29 11:39:00 +0000 | [diff] [blame] | 501 | /** |
| 502 | * devm_iio_trigger_alloc - Resource-managed iio_trigger_alloc() |
| 503 | * @dev: Device to allocate iio_trigger for |
| 504 | * @fmt: trigger name format. If it includes format |
| 505 | * specifiers, the additional arguments following |
| 506 | * format are formatted and inserted in the resulting |
| 507 | * string replacing their respective specifiers. |
| 508 | * |
| 509 | * Managed iio_trigger_alloc. iio_trigger allocated with this function is |
| 510 | * automatically freed on driver detach. |
| 511 | * |
| 512 | * If an iio_trigger allocated with this function needs to be freed separately, |
| 513 | * devm_iio_trigger_free() must be used. |
| 514 | * |
| 515 | * RETURNS: |
| 516 | * Pointer to allocated iio_trigger on success, NULL on failure. |
| 517 | */ |
Jacek Anaszewski | d536321 | 2013-08-16 14:11:00 +0100 | [diff] [blame] | 518 | struct iio_trigger *devm_iio_trigger_alloc(struct device *dev, |
| 519 | const char *fmt, ...) |
| 520 | { |
| 521 | struct iio_trigger **ptr, *trig; |
| 522 | va_list vargs; |
| 523 | |
| 524 | ptr = devres_alloc(devm_iio_trigger_release, sizeof(*ptr), |
| 525 | GFP_KERNEL); |
| 526 | if (!ptr) |
| 527 | return NULL; |
| 528 | |
| 529 | /* use raw alloc_dr for kmalloc caller tracing */ |
| 530 | va_start(vargs, fmt); |
| 531 | trig = viio_trigger_alloc(fmt, vargs); |
| 532 | va_end(vargs); |
| 533 | if (trig) { |
| 534 | *ptr = trig; |
| 535 | devres_add(dev, ptr); |
| 536 | } else { |
| 537 | devres_free(ptr); |
| 538 | } |
| 539 | |
| 540 | return trig; |
| 541 | } |
| 542 | EXPORT_SYMBOL_GPL(devm_iio_trigger_alloc); |
| 543 | |
Sachin Kamat | a7e57dc | 2013-10-29 11:39:00 +0000 | [diff] [blame] | 544 | /** |
| 545 | * devm_iio_trigger_free - Resource-managed iio_trigger_free() |
| 546 | * @dev: Device this iio_dev belongs to |
| 547 | * @iio_trig: the iio_trigger associated with the device |
| 548 | * |
| 549 | * Free iio_trigger allocated with devm_iio_trigger_alloc(). |
| 550 | */ |
Jacek Anaszewski | d536321 | 2013-08-16 14:11:00 +0100 | [diff] [blame] | 551 | void devm_iio_trigger_free(struct device *dev, struct iio_trigger *iio_trig) |
| 552 | { |
| 553 | int rc; |
| 554 | |
| 555 | rc = devres_release(dev, devm_iio_trigger_release, |
| 556 | devm_iio_trigger_match, iio_trig); |
| 557 | WARN_ON(rc); |
| 558 | } |
| 559 | EXPORT_SYMBOL_GPL(devm_iio_trigger_free); |
| 560 | |
Jonathan Cameron | 67be8e3 | 2011-10-26 17:27:38 +0100 | [diff] [blame] | 561 | void iio_device_register_trigger_consumer(struct iio_dev *indio_dev) |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 562 | { |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 563 | indio_dev->groups[indio_dev->groupcounter++] = |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 564 | &iio_trigger_consumer_attr_group; |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 565 | } |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 566 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 567 | void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev) |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 568 | { |
Peter Meerwald | 860c9c5 | 2013-02-04 11:36:00 +0000 | [diff] [blame] | 569 | /* Clean up an associated but not attached trigger reference */ |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 570 | if (indio_dev->trig) |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 571 | iio_trigger_put(indio_dev->trig); |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 572 | } |
Jonathan Cameron | 1637db4 | 2009-08-18 18:06:26 +0100 | [diff] [blame] | 573 | |
Jonathan Cameron | 3b99fb7 | 2011-09-21 11:15:53 +0100 | [diff] [blame] | 574 | int iio_triggered_buffer_postenable(struct iio_dev *indio_dev) |
Jonathan Cameron | c3db00c | 2010-07-11 16:39:10 +0100 | [diff] [blame] | 575 | { |
Jonathan Cameron | 67be8e3 | 2011-10-26 17:27:38 +0100 | [diff] [blame] | 576 | return iio_trigger_attach_poll_func(indio_dev->trig, |
| 577 | indio_dev->pollfunc); |
Jonathan Cameron | c3db00c | 2010-07-11 16:39:10 +0100 | [diff] [blame] | 578 | } |
Jonathan Cameron | 3b99fb7 | 2011-09-21 11:15:53 +0100 | [diff] [blame] | 579 | EXPORT_SYMBOL(iio_triggered_buffer_postenable); |
Jonathan Cameron | c3db00c | 2010-07-11 16:39:10 +0100 | [diff] [blame] | 580 | |
Jonathan Cameron | 3b99fb7 | 2011-09-21 11:15:53 +0100 | [diff] [blame] | 581 | int iio_triggered_buffer_predisable(struct iio_dev *indio_dev) |
Jonathan Cameron | c3db00c | 2010-07-11 16:39:10 +0100 | [diff] [blame] | 582 | { |
Peter Meerwald | 034bd7b | 2012-07-02 23:43:47 +0200 | [diff] [blame] | 583 | return iio_trigger_detach_poll_func(indio_dev->trig, |
Jonathan Cameron | 67be8e3 | 2011-10-26 17:27:38 +0100 | [diff] [blame] | 584 | indio_dev->pollfunc); |
Jonathan Cameron | c3db00c | 2010-07-11 16:39:10 +0100 | [diff] [blame] | 585 | } |
Jonathan Cameron | 3b99fb7 | 2011-09-21 11:15:53 +0100 | [diff] [blame] | 586 | EXPORT_SYMBOL(iio_triggered_buffer_predisable); |