Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1 | /* The industrial I/O core |
| 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 | * Based on elements of hwmon and input subsystems. |
| 10 | */ |
| 11 | |
Sachin Kamat | 3176dd5 | 2013-10-24 12:53:00 +0100 | [diff] [blame] | 12 | #define pr_fmt(fmt) "iio-core: " fmt |
| 13 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/idr.h> |
| 17 | #include <linux/kdev_t.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/device.h> |
| 20 | #include <linux/fs.h> |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 21 | #include <linux/poll.h> |
Jonathan Cameron | ffc18af | 2009-10-12 19:18:09 +0100 | [diff] [blame] | 22 | #include <linux/sched.h> |
Jeff Mahoney | 4439c93 | 2009-10-12 17:10:34 -0400 | [diff] [blame] | 23 | #include <linux/wait.h> |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 24 | #include <linux/cdev.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Jonathan Cameron | 8e7d967 | 2011-08-30 12:32:45 +0100 | [diff] [blame] | 26 | #include <linux/anon_inodes.h> |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 27 | #include <linux/debugfs.h> |
Alison Schofield | 08a3380 | 2016-03-09 11:30:12 -0800 | [diff] [blame] | 28 | #include <linux/mutex.h> |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 29 | #include <linux/iio/iio.h> |
Jonathan Cameron | df9c1c4 | 2011-08-12 17:56:03 +0100 | [diff] [blame] | 30 | #include "iio_core.h" |
Jonathan Cameron | 6aea1c3 | 2011-08-24 17:28:38 +0100 | [diff] [blame] | 31 | #include "iio_core_trigger.h" |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 32 | #include <linux/iio/sysfs.h> |
| 33 | #include <linux/iio/events.h> |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 34 | #include <linux/iio/buffer.h> |
Jonathan Cameron | 9dd1cb3 | 2011-08-30 12:41:15 +0100 | [diff] [blame] | 35 | |
Peter Meerwald | 99698b4 | 2012-08-26 13:43:00 +0100 | [diff] [blame] | 36 | /* IDA to assign each registered device a unique id */ |
Jonathan Cameron | b156cf7 | 2010-09-04 17:54:43 +0100 | [diff] [blame] | 37 | static DEFINE_IDA(iio_ida); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 38 | |
Jonathan Cameron | f625cb9 | 2011-08-30 12:32:48 +0100 | [diff] [blame] | 39 | static dev_t iio_devt; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 40 | |
| 41 | #define IIO_DEV_MAX 256 |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 42 | struct bus_type iio_bus_type = { |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 43 | .name = "iio", |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 44 | }; |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 45 | EXPORT_SYMBOL(iio_bus_type); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 46 | |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 47 | static struct dentry *iio_debugfs_dentry; |
| 48 | |
Jonathan Cameron | c6fc806 | 2011-09-02 17:14:34 +0100 | [diff] [blame] | 49 | static const char * const iio_direction[] = { |
| 50 | [0] = "in", |
| 51 | [1] = "out", |
| 52 | }; |
| 53 | |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 54 | static const char * const iio_chan_type_name_spec[] = { |
Jonathan Cameron | c6fc806 | 2011-09-02 17:14:34 +0100 | [diff] [blame] | 55 | [IIO_VOLTAGE] = "voltage", |
Michael Hennerich | faf290e | 2011-05-18 14:42:02 +0100 | [diff] [blame] | 56 | [IIO_CURRENT] = "current", |
| 57 | [IIO_POWER] = "power", |
Bryan Freed | 9bff02f | 2011-07-07 12:01:54 -0700 | [diff] [blame] | 58 | [IIO_ACCEL] = "accel", |
Jonathan Cameron | 41ea040 | 2011-10-05 15:27:59 +0100 | [diff] [blame] | 59 | [IIO_ANGL_VEL] = "anglvel", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 60 | [IIO_MAGN] = "magn", |
Bryan Freed | 9bff02f | 2011-07-07 12:01:54 -0700 | [diff] [blame] | 61 | [IIO_LIGHT] = "illuminance", |
| 62 | [IIO_INTENSITY] = "intensity", |
Bryan Freed | f09f2c8 | 2011-07-07 12:01:55 -0700 | [diff] [blame] | 63 | [IIO_PROXIMITY] = "proximity", |
Bryan Freed | 9bff02f | 2011-07-07 12:01:54 -0700 | [diff] [blame] | 64 | [IIO_TEMP] = "temp", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 65 | [IIO_INCLI] = "incli", |
| 66 | [IIO_ROT] = "rot", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 67 | [IIO_ANGL] = "angl", |
Bryan Freed | 9bff02f | 2011-07-07 12:01:54 -0700 | [diff] [blame] | 68 | [IIO_TIMESTAMP] = "timestamp", |
Jonathan Cameron | 66dbe70 | 2011-09-02 17:14:43 +0100 | [diff] [blame] | 69 | [IIO_CAPACITANCE] = "capacitance", |
Michael Hennerich | a6b1285 | 2012-04-27 10:58:34 +0200 | [diff] [blame] | 70 | [IIO_ALTVOLTAGE] = "altvoltage", |
Jon Brenner | 21cd1fa | 2012-05-16 10:46:42 -0500 | [diff] [blame] | 71 | [IIO_CCT] = "cct", |
Lars-Peter Clausen | c4f0c69 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 72 | [IIO_PRESSURE] = "pressure", |
Harald Geyer | ac216aa | 2013-12-01 15:08:00 +0000 | [diff] [blame] | 73 | [IIO_HUMIDITYRELATIVE] = "humidityrelative", |
Daniel Baluta | 55aebeb | 2014-11-10 14:45:30 +0200 | [diff] [blame] | 74 | [IIO_ACTIVITY] = "activity", |
Irina Tirdea | a88bfe7 | 2014-11-10 14:45:32 +0200 | [diff] [blame] | 75 | [IIO_STEPS] = "steps", |
Irina Tirdea | 72c6664 | 2015-01-11 21:10:07 +0200 | [diff] [blame] | 76 | [IIO_ENERGY] = "energy", |
Irina Tirdea | cc3c9ee | 2015-01-11 21:10:08 +0200 | [diff] [blame] | 77 | [IIO_DISTANCE] = "distance", |
Irina Tirdea | 5a1a932 | 2015-01-11 21:10:09 +0200 | [diff] [blame] | 78 | [IIO_VELOCITY] = "velocity", |
Matt Ranostay | 8ff6b3b | 2015-09-13 20:26:11 -0700 | [diff] [blame] | 79 | [IIO_CONCENTRATION] = "concentration", |
Matt Ranostay | d38d546 | 2015-09-13 20:26:12 -0700 | [diff] [blame] | 80 | [IIO_RESISTANCE] = "resistance", |
Matt Ranostay | ecb3a7c | 2016-01-26 18:34:30 -0800 | [diff] [blame] | 81 | [IIO_PH] = "ph", |
Peter Meerwald-Stadler | d409404 | 2016-03-20 16:20:23 +0100 | [diff] [blame] | 82 | [IIO_UVINDEX] = "uvindex", |
Matt Ranostay | 4b9d209 | 2016-05-24 21:29:19 -0700 | [diff] [blame] | 83 | [IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 84 | }; |
| 85 | |
Jonathan Cameron | 330c6c5 | 2011-09-02 17:14:39 +0100 | [diff] [blame] | 86 | static const char * const iio_modifier_names[] = { |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 87 | [IIO_MOD_X] = "x", |
| 88 | [IIO_MOD_Y] = "y", |
| 89 | [IIO_MOD_Z] = "z", |
Peter Meerwald | 4b8d801 | 2015-06-20 23:52:30 +0200 | [diff] [blame] | 90 | [IIO_MOD_X_AND_Y] = "x&y", |
| 91 | [IIO_MOD_X_AND_Z] = "x&z", |
| 92 | [IIO_MOD_Y_AND_Z] = "y&z", |
| 93 | [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z", |
| 94 | [IIO_MOD_X_OR_Y] = "x|y", |
| 95 | [IIO_MOD_X_OR_Z] = "x|z", |
| 96 | [IIO_MOD_Y_OR_Z] = "y|z", |
| 97 | [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z", |
Jonathan Cameron | 8f5879b | 2012-05-05 10:39:22 +0100 | [diff] [blame] | 98 | [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)", |
Jonathan Cameron | cf82cb8 | 2012-05-05 10:56:41 +0100 | [diff] [blame] | 99 | [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2", |
Jonathan Cameron | 330c6c5 | 2011-09-02 17:14:39 +0100 | [diff] [blame] | 100 | [IIO_MOD_LIGHT_BOTH] = "both", |
| 101 | [IIO_MOD_LIGHT_IR] = "ir", |
Jon Brenner | 21cd1fa | 2012-05-16 10:46:42 -0500 | [diff] [blame] | 102 | [IIO_MOD_LIGHT_CLEAR] = "clear", |
| 103 | [IIO_MOD_LIGHT_RED] = "red", |
| 104 | [IIO_MOD_LIGHT_GREEN] = "green", |
| 105 | [IIO_MOD_LIGHT_BLUE] = "blue", |
Peter Meerwald-Stadler | 2c5ff1f | 2016-03-20 16:20:22 +0100 | [diff] [blame] | 106 | [IIO_MOD_LIGHT_UV] = "uv", |
Srinivas Pandruvada | 5082f40 | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 107 | [IIO_MOD_QUATERNION] = "quaternion", |
Peter Meerwald | 638b43b | 2014-02-05 16:57:00 +0000 | [diff] [blame] | 108 | [IIO_MOD_TEMP_AMBIENT] = "ambient", |
| 109 | [IIO_MOD_TEMP_OBJECT] = "object", |
Reyad Attiyat | 11b8dda | 2014-07-17 19:18:00 +0100 | [diff] [blame] | 110 | [IIO_MOD_NORTH_MAGN] = "from_north_magnetic", |
| 111 | [IIO_MOD_NORTH_TRUE] = "from_north_true", |
| 112 | [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp", |
| 113 | [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp", |
Daniel Baluta | 55aebeb | 2014-11-10 14:45:30 +0200 | [diff] [blame] | 114 | [IIO_MOD_RUNNING] = "running", |
| 115 | [IIO_MOD_JOGGING] = "jogging", |
| 116 | [IIO_MOD_WALKING] = "walking", |
| 117 | [IIO_MOD_STILL] = "still", |
Irina Tirdea | 5a1a932 | 2015-01-11 21:10:09 +0200 | [diff] [blame] | 118 | [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)", |
Lars-Peter Clausen | 1ce87f2 | 2015-05-22 18:17:38 +0200 | [diff] [blame] | 119 | [IIO_MOD_I] = "i", |
| 120 | [IIO_MOD_Q] = "q", |
Matt Ranostay | 8ff6b3b | 2015-09-13 20:26:11 -0700 | [diff] [blame] | 121 | [IIO_MOD_CO2] = "co2", |
| 122 | [IIO_MOD_VOC] = "voc", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | /* relies on pairs of these shared then separate */ |
| 126 | static const char * const iio_chan_info_postfix[] = { |
Jonathan Cameron | 75a973c | 2012-04-15 17:41:30 +0100 | [diff] [blame] | 127 | [IIO_CHAN_INFO_RAW] = "raw", |
| 128 | [IIO_CHAN_INFO_PROCESSED] = "input", |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 129 | [IIO_CHAN_INFO_SCALE] = "scale", |
| 130 | [IIO_CHAN_INFO_OFFSET] = "offset", |
| 131 | [IIO_CHAN_INFO_CALIBSCALE] = "calibscale", |
| 132 | [IIO_CHAN_INFO_CALIBBIAS] = "calibbias", |
| 133 | [IIO_CHAN_INFO_PEAK] = "peak_raw", |
| 134 | [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale", |
| 135 | [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw", |
| 136 | [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw", |
Jonathan Cameron | df94aba | 2011-11-27 11:39:12 +0000 | [diff] [blame] | 137 | [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY] |
| 138 | = "filter_low_pass_3db_frequency", |
Martin Fuzzey | 3f7f642 | 2015-05-13 12:26:42 +0200 | [diff] [blame] | 139 | [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY] |
| 140 | = "filter_high_pass_3db_frequency", |
Laxman Dewangan | ce85a1c | 2012-04-13 16:03:31 +0530 | [diff] [blame] | 141 | [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency", |
Michael Hennerich | a6b1285 | 2012-04-27 10:58:34 +0200 | [diff] [blame] | 142 | [IIO_CHAN_INFO_FREQUENCY] = "frequency", |
| 143 | [IIO_CHAN_INFO_PHASE] = "phase", |
Michael Hennerich | b65d621 | 2012-05-11 11:36:53 +0200 | [diff] [blame] | 144 | [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain", |
srinivas pandruvada | 7c9ab03 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 145 | [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis", |
Peter Meerwald | 899d90b | 2013-09-08 16:20:00 +0100 | [diff] [blame] | 146 | [IIO_CHAN_INFO_INT_TIME] = "integration_time", |
Irina Tirdea | a88bfe7 | 2014-11-10 14:45:32 +0200 | [diff] [blame] | 147 | [IIO_CHAN_INFO_ENABLE] = "en", |
Irina Tirdea | bcdf28f | 2014-11-10 14:45:33 +0200 | [diff] [blame] | 148 | [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight", |
Irina Tirdea | d37f683 | 2015-01-11 21:10:10 +0200 | [diff] [blame] | 149 | [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight", |
Irina Tirdea | 2f0ecb7 | 2015-01-27 20:41:52 +0200 | [diff] [blame] | 150 | [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count", |
| 151 | [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time", |
Vianney le Clément de Saint-Marcq | c8a8585 | 2015-03-30 10:34:58 +0200 | [diff] [blame] | 152 | [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity", |
Irina Tirdea | faaa449 | 2015-04-29 21:16:39 +0300 | [diff] [blame] | 153 | [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 154 | }; |
| 155 | |
Sachin Kamat | a7e57dc | 2013-10-29 11:39:00 +0000 | [diff] [blame] | 156 | /** |
| 157 | * iio_find_channel_from_si() - get channel from its scan index |
| 158 | * @indio_dev: device |
| 159 | * @si: scan index to match |
| 160 | */ |
Jonathan Cameron | 5fb21c8 | 2011-12-05 21:37:10 +0000 | [diff] [blame] | 161 | const struct iio_chan_spec |
| 162 | *iio_find_channel_from_si(struct iio_dev *indio_dev, int si) |
| 163 | { |
| 164 | int i; |
| 165 | |
| 166 | for (i = 0; i < indio_dev->num_channels; i++) |
| 167 | if (indio_dev->channels[i].scan_index == si) |
| 168 | return &indio_dev->channels[i]; |
| 169 | return NULL; |
| 170 | } |
| 171 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 172 | /* This turns up an awful lot */ |
| 173 | ssize_t iio_read_const_attr(struct device *dev, |
| 174 | struct device_attribute *attr, |
| 175 | char *buf) |
| 176 | { |
| 177 | return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string); |
| 178 | } |
| 179 | EXPORT_SYMBOL(iio_read_const_attr); |
| 180 | |
Gregor Boirie | bc2b7da | 2016-03-09 19:05:49 +0100 | [diff] [blame] | 181 | static int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id) |
| 182 | { |
| 183 | int ret; |
| 184 | const struct iio_event_interface *ev_int = indio_dev->event_interface; |
| 185 | |
| 186 | ret = mutex_lock_interruptible(&indio_dev->mlock); |
| 187 | if (ret) |
| 188 | return ret; |
| 189 | if ((ev_int && iio_event_enabled(ev_int)) || |
| 190 | iio_buffer_enabled(indio_dev)) { |
| 191 | mutex_unlock(&indio_dev->mlock); |
| 192 | return -EBUSY; |
| 193 | } |
| 194 | indio_dev->clock_id = clock_id; |
| 195 | mutex_unlock(&indio_dev->mlock); |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * iio_get_time_ns() - utility function to get a time stamp for events etc |
| 202 | * @indio_dev: device |
| 203 | */ |
| 204 | s64 iio_get_time_ns(const struct iio_dev *indio_dev) |
| 205 | { |
| 206 | struct timespec tp; |
| 207 | |
| 208 | switch (iio_device_get_clock(indio_dev)) { |
| 209 | case CLOCK_REALTIME: |
| 210 | ktime_get_real_ts(&tp); |
| 211 | break; |
| 212 | case CLOCK_MONOTONIC: |
| 213 | ktime_get_ts(&tp); |
| 214 | break; |
| 215 | case CLOCK_MONOTONIC_RAW: |
| 216 | getrawmonotonic(&tp); |
| 217 | break; |
| 218 | case CLOCK_REALTIME_COARSE: |
| 219 | tp = current_kernel_time(); |
| 220 | break; |
| 221 | case CLOCK_MONOTONIC_COARSE: |
| 222 | tp = get_monotonic_coarse(); |
| 223 | break; |
| 224 | case CLOCK_BOOTTIME: |
| 225 | get_monotonic_boottime(&tp); |
| 226 | break; |
| 227 | case CLOCK_TAI: |
| 228 | timekeeping_clocktai(&tp); |
| 229 | break; |
| 230 | default: |
| 231 | BUG(); |
| 232 | } |
| 233 | |
| 234 | return timespec_to_ns(&tp); |
| 235 | } |
| 236 | EXPORT_SYMBOL(iio_get_time_ns); |
| 237 | |
| 238 | /** |
| 239 | * iio_get_time_res() - utility function to get time stamp clock resolution in |
| 240 | * nano seconds. |
| 241 | * @indio_dev: device |
| 242 | */ |
| 243 | unsigned int iio_get_time_res(const struct iio_dev *indio_dev) |
| 244 | { |
| 245 | switch (iio_device_get_clock(indio_dev)) { |
| 246 | case CLOCK_REALTIME: |
| 247 | case CLOCK_MONOTONIC: |
| 248 | case CLOCK_MONOTONIC_RAW: |
| 249 | case CLOCK_BOOTTIME: |
| 250 | case CLOCK_TAI: |
| 251 | return hrtimer_resolution; |
| 252 | case CLOCK_REALTIME_COARSE: |
| 253 | case CLOCK_MONOTONIC_COARSE: |
| 254 | return LOW_RES_NSEC; |
| 255 | default: |
| 256 | BUG(); |
| 257 | } |
| 258 | } |
| 259 | EXPORT_SYMBOL(iio_get_time_res); |
| 260 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 261 | static int __init iio_init(void) |
| 262 | { |
| 263 | int ret; |
| 264 | |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 265 | /* Register sysfs bus */ |
| 266 | ret = bus_register(&iio_bus_type); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 267 | if (ret < 0) { |
Sachin Kamat | 3176dd5 | 2013-10-24 12:53:00 +0100 | [diff] [blame] | 268 | pr_err("could not register bus type\n"); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 269 | goto error_nothing; |
| 270 | } |
| 271 | |
Jonathan Cameron | 9aa1a16 | 2011-08-12 17:08:50 +0100 | [diff] [blame] | 272 | ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio"); |
| 273 | if (ret < 0) { |
Sachin Kamat | 3176dd5 | 2013-10-24 12:53:00 +0100 | [diff] [blame] | 274 | pr_err("failed to allocate char dev region\n"); |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 275 | goto error_unregister_bus_type; |
Jonathan Cameron | 9aa1a16 | 2011-08-12 17:08:50 +0100 | [diff] [blame] | 276 | } |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 277 | |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 278 | iio_debugfs_dentry = debugfs_create_dir("iio", NULL); |
| 279 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 280 | return 0; |
| 281 | |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 282 | error_unregister_bus_type: |
| 283 | bus_unregister(&iio_bus_type); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 284 | error_nothing: |
| 285 | return ret; |
| 286 | } |
| 287 | |
| 288 | static void __exit iio_exit(void) |
| 289 | { |
Jonathan Cameron | 9aa1a16 | 2011-08-12 17:08:50 +0100 | [diff] [blame] | 290 | if (iio_devt) |
| 291 | unregister_chrdev_region(iio_devt, IIO_DEV_MAX); |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 292 | bus_unregister(&iio_bus_type); |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 293 | debugfs_remove(iio_debugfs_dentry); |
| 294 | } |
| 295 | |
| 296 | #if defined(CONFIG_DEBUG_FS) |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 297 | static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf, |
| 298 | size_t count, loff_t *ppos) |
| 299 | { |
| 300 | struct iio_dev *indio_dev = file->private_data; |
| 301 | char buf[20]; |
| 302 | unsigned val = 0; |
| 303 | ssize_t len; |
| 304 | int ret; |
| 305 | |
| 306 | ret = indio_dev->info->debugfs_reg_access(indio_dev, |
| 307 | indio_dev->cached_reg_addr, |
| 308 | 0, &val); |
| 309 | if (ret) |
| 310 | dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__); |
| 311 | |
| 312 | len = snprintf(buf, sizeof(buf), "0x%X\n", val); |
| 313 | |
| 314 | return simple_read_from_buffer(userbuf, count, ppos, buf, len); |
| 315 | } |
| 316 | |
| 317 | static ssize_t iio_debugfs_write_reg(struct file *file, |
| 318 | const char __user *userbuf, size_t count, loff_t *ppos) |
| 319 | { |
| 320 | struct iio_dev *indio_dev = file->private_data; |
| 321 | unsigned reg, val; |
| 322 | char buf[80]; |
| 323 | int ret; |
| 324 | |
| 325 | count = min_t(size_t, count, (sizeof(buf)-1)); |
| 326 | if (copy_from_user(buf, userbuf, count)) |
| 327 | return -EFAULT; |
| 328 | |
| 329 | buf[count] = 0; |
| 330 | |
| 331 | ret = sscanf(buf, "%i %i", ®, &val); |
| 332 | |
| 333 | switch (ret) { |
| 334 | case 1: |
| 335 | indio_dev->cached_reg_addr = reg; |
| 336 | break; |
| 337 | case 2: |
| 338 | indio_dev->cached_reg_addr = reg; |
| 339 | ret = indio_dev->info->debugfs_reg_access(indio_dev, reg, |
| 340 | val, NULL); |
| 341 | if (ret) { |
| 342 | dev_err(indio_dev->dev.parent, "%s: write failed\n", |
| 343 | __func__); |
| 344 | return ret; |
| 345 | } |
| 346 | break; |
| 347 | default: |
| 348 | return -EINVAL; |
| 349 | } |
| 350 | |
| 351 | return count; |
| 352 | } |
| 353 | |
| 354 | static const struct file_operations iio_debugfs_reg_fops = { |
Axel Lin | 5a28c87 | 2012-05-03 09:50:51 +0800 | [diff] [blame] | 355 | .open = simple_open, |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 356 | .read = iio_debugfs_read_reg, |
| 357 | .write = iio_debugfs_write_reg, |
| 358 | }; |
| 359 | |
| 360 | static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) |
| 361 | { |
| 362 | debugfs_remove_recursive(indio_dev->debugfs_dentry); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 363 | } |
| 364 | |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 365 | static int iio_device_register_debugfs(struct iio_dev *indio_dev) |
| 366 | { |
| 367 | struct dentry *d; |
| 368 | |
| 369 | if (indio_dev->info->debugfs_reg_access == NULL) |
| 370 | return 0; |
| 371 | |
Axel Lin | abd5a2f | 2012-05-03 22:56:58 +0800 | [diff] [blame] | 372 | if (!iio_debugfs_dentry) |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 373 | return 0; |
| 374 | |
| 375 | indio_dev->debugfs_dentry = |
| 376 | debugfs_create_dir(dev_name(&indio_dev->dev), |
| 377 | iio_debugfs_dentry); |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 378 | if (indio_dev->debugfs_dentry == NULL) { |
| 379 | dev_warn(indio_dev->dev.parent, |
| 380 | "Failed to create debugfs directory\n"); |
| 381 | return -EFAULT; |
| 382 | } |
| 383 | |
| 384 | d = debugfs_create_file("direct_reg_access", 0644, |
| 385 | indio_dev->debugfs_dentry, |
| 386 | indio_dev, &iio_debugfs_reg_fops); |
| 387 | if (!d) { |
| 388 | iio_device_unregister_debugfs(indio_dev); |
| 389 | return -ENOMEM; |
| 390 | } |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | #else |
| 395 | static int iio_device_register_debugfs(struct iio_dev *indio_dev) |
| 396 | { |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) |
| 401 | { |
| 402 | } |
| 403 | #endif /* CONFIG_DEBUG_FS */ |
| 404 | |
Lars-Peter Clausen | 4fee7e1 | 2012-03-06 20:43:45 +0100 | [diff] [blame] | 405 | static ssize_t iio_read_channel_ext_info(struct device *dev, |
| 406 | struct device_attribute *attr, |
| 407 | char *buf) |
| 408 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 409 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Lars-Peter Clausen | 4fee7e1 | 2012-03-06 20:43:45 +0100 | [diff] [blame] | 410 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 411 | const struct iio_chan_spec_ext_info *ext_info; |
| 412 | |
| 413 | ext_info = &this_attr->c->ext_info[this_attr->address]; |
| 414 | |
Michael Hennerich | fc6d113 | 2012-04-27 10:58:36 +0200 | [diff] [blame] | 415 | return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf); |
Lars-Peter Clausen | 4fee7e1 | 2012-03-06 20:43:45 +0100 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static ssize_t iio_write_channel_ext_info(struct device *dev, |
| 419 | struct device_attribute *attr, |
| 420 | const char *buf, |
| 421 | size_t len) |
| 422 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 423 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Lars-Peter Clausen | 4fee7e1 | 2012-03-06 20:43:45 +0100 | [diff] [blame] | 424 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 425 | const struct iio_chan_spec_ext_info *ext_info; |
| 426 | |
| 427 | ext_info = &this_attr->c->ext_info[this_attr->address]; |
| 428 | |
Michael Hennerich | fc6d113 | 2012-04-27 10:58:36 +0200 | [diff] [blame] | 429 | return ext_info->write(indio_dev, ext_info->private, |
| 430 | this_attr->c, buf, len); |
Lars-Peter Clausen | 4fee7e1 | 2012-03-06 20:43:45 +0100 | [diff] [blame] | 431 | } |
| 432 | |
Lars-Peter Clausen | 5212cc8 | 2012-06-04 11:36:11 +0200 | [diff] [blame] | 433 | ssize_t iio_enum_available_read(struct iio_dev *indio_dev, |
| 434 | uintptr_t priv, const struct iio_chan_spec *chan, char *buf) |
| 435 | { |
| 436 | const struct iio_enum *e = (const struct iio_enum *)priv; |
| 437 | unsigned int i; |
| 438 | size_t len = 0; |
| 439 | |
| 440 | if (!e->num_items) |
| 441 | return 0; |
| 442 | |
| 443 | for (i = 0; i < e->num_items; ++i) |
Lars-Peter Clausen | 74dcd43 | 2012-06-05 18:24:12 +0200 | [diff] [blame] | 444 | len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]); |
Lars-Peter Clausen | 5212cc8 | 2012-06-04 11:36:11 +0200 | [diff] [blame] | 445 | |
| 446 | /* replace last space with a newline */ |
| 447 | buf[len - 1] = '\n'; |
| 448 | |
| 449 | return len; |
| 450 | } |
| 451 | EXPORT_SYMBOL_GPL(iio_enum_available_read); |
| 452 | |
| 453 | ssize_t iio_enum_read(struct iio_dev *indio_dev, |
| 454 | uintptr_t priv, const struct iio_chan_spec *chan, char *buf) |
| 455 | { |
| 456 | const struct iio_enum *e = (const struct iio_enum *)priv; |
| 457 | int i; |
| 458 | |
| 459 | if (!e->get) |
| 460 | return -EINVAL; |
| 461 | |
| 462 | i = e->get(indio_dev, chan); |
| 463 | if (i < 0) |
| 464 | return i; |
| 465 | else if (i >= e->num_items) |
| 466 | return -EINVAL; |
| 467 | |
Kees Cook | 598db58 | 2014-03-13 16:46:00 +0000 | [diff] [blame] | 468 | return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]); |
Lars-Peter Clausen | 5212cc8 | 2012-06-04 11:36:11 +0200 | [diff] [blame] | 469 | } |
| 470 | EXPORT_SYMBOL_GPL(iio_enum_read); |
| 471 | |
| 472 | ssize_t iio_enum_write(struct iio_dev *indio_dev, |
| 473 | uintptr_t priv, const struct iio_chan_spec *chan, const char *buf, |
| 474 | size_t len) |
| 475 | { |
| 476 | const struct iio_enum *e = (const struct iio_enum *)priv; |
| 477 | unsigned int i; |
| 478 | int ret; |
| 479 | |
| 480 | if (!e->set) |
| 481 | return -EINVAL; |
| 482 | |
| 483 | for (i = 0; i < e->num_items; i++) { |
| 484 | if (sysfs_streq(buf, e->items[i])) |
| 485 | break; |
| 486 | } |
| 487 | |
| 488 | if (i == e->num_items) |
| 489 | return -EINVAL; |
| 490 | |
| 491 | ret = e->set(indio_dev, chan, i); |
| 492 | return ret ? ret : len; |
| 493 | } |
| 494 | EXPORT_SYMBOL_GPL(iio_enum_write); |
| 495 | |
Gregor Boirie | dfc5773 | 2016-04-20 19:23:43 +0200 | [diff] [blame] | 496 | static const struct iio_mount_matrix iio_mount_idmatrix = { |
| 497 | .rotation = { |
| 498 | "1", "0", "0", |
| 499 | "0", "1", "0", |
| 500 | "0", "0", "1" |
| 501 | } |
| 502 | }; |
| 503 | |
| 504 | static int iio_setup_mount_idmatrix(const struct device *dev, |
| 505 | struct iio_mount_matrix *matrix) |
| 506 | { |
| 507 | *matrix = iio_mount_idmatrix; |
| 508 | dev_info(dev, "mounting matrix not found: using identity...\n"); |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv, |
| 513 | const struct iio_chan_spec *chan, char *buf) |
| 514 | { |
| 515 | const struct iio_mount_matrix *mtx = ((iio_get_mount_matrix_t *) |
| 516 | priv)(indio_dev, chan); |
| 517 | |
| 518 | if (IS_ERR(mtx)) |
| 519 | return PTR_ERR(mtx); |
| 520 | |
| 521 | if (!mtx) |
| 522 | mtx = &iio_mount_idmatrix; |
| 523 | |
| 524 | return snprintf(buf, PAGE_SIZE, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n", |
| 525 | mtx->rotation[0], mtx->rotation[1], mtx->rotation[2], |
| 526 | mtx->rotation[3], mtx->rotation[4], mtx->rotation[5], |
| 527 | mtx->rotation[6], mtx->rotation[7], mtx->rotation[8]); |
| 528 | } |
| 529 | EXPORT_SYMBOL_GPL(iio_show_mount_matrix); |
| 530 | |
| 531 | /** |
| 532 | * of_iio_read_mount_matrix() - retrieve iio device mounting matrix from |
| 533 | * device-tree "mount-matrix" property |
| 534 | * @dev: device the mounting matrix property is assigned to |
| 535 | * @propname: device specific mounting matrix property name |
| 536 | * @matrix: where to store retrieved matrix |
| 537 | * |
| 538 | * If device is assigned no mounting matrix property, a default 3x3 identity |
| 539 | * matrix will be filled in. |
| 540 | * |
| 541 | * Return: 0 if success, or a negative error code on failure. |
| 542 | */ |
| 543 | #ifdef CONFIG_OF |
| 544 | int of_iio_read_mount_matrix(const struct device *dev, |
| 545 | const char *propname, |
| 546 | struct iio_mount_matrix *matrix) |
| 547 | { |
| 548 | if (dev->of_node) { |
| 549 | int err = of_property_read_string_array(dev->of_node, |
| 550 | propname, matrix->rotation, |
| 551 | ARRAY_SIZE(iio_mount_idmatrix.rotation)); |
| 552 | |
| 553 | if (err == ARRAY_SIZE(iio_mount_idmatrix.rotation)) |
| 554 | return 0; |
| 555 | |
| 556 | if (err >= 0) |
| 557 | /* Invalid number of matrix entries. */ |
| 558 | return -EINVAL; |
| 559 | |
| 560 | if (err != -EINVAL) |
| 561 | /* Invalid matrix declaration format. */ |
| 562 | return err; |
| 563 | } |
| 564 | |
| 565 | /* Matrix was not declared at all: fallback to identity. */ |
| 566 | return iio_setup_mount_idmatrix(dev, matrix); |
| 567 | } |
| 568 | #else |
| 569 | int of_iio_read_mount_matrix(const struct device *dev, |
| 570 | const char *propname, |
| 571 | struct iio_mount_matrix *matrix) |
| 572 | { |
| 573 | return iio_setup_mount_idmatrix(dev, matrix); |
| 574 | } |
| 575 | #endif |
| 576 | EXPORT_SYMBOL(of_iio_read_mount_matrix); |
| 577 | |
Lars-Peter Clausen | 3661f3f | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 578 | /** |
| 579 | * iio_format_value() - Formats a IIO value into its string representation |
Cristina Opriceana | 2498dcf | 2015-07-24 16:16:19 +0300 | [diff] [blame] | 580 | * @buf: The buffer to which the formatted value gets written |
| 581 | * @type: One of the IIO_VAL_... constants. This decides how the val |
| 582 | * and val2 parameters are formatted. |
| 583 | * @size: Number of IIO value entries contained in vals |
| 584 | * @vals: Pointer to the values, exact meaning depends on the |
| 585 | * type parameter. |
| 586 | * |
| 587 | * Return: 0 by default, a negative number on failure or the |
| 588 | * total number of characters written for a type that belongs |
| 589 | * to the IIO_VAL_... constant. |
Lars-Peter Clausen | 3661f3f | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 590 | */ |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 591 | ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 592 | { |
Lars-Peter Clausen | 7985e7c | 2012-09-14 16:21:00 +0100 | [diff] [blame] | 593 | unsigned long long tmp; |
Michael Hennerich | 67eedba | 2012-05-11 11:36:52 +0200 | [diff] [blame] | 594 | bool scale_db = false; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 595 | |
Lars-Peter Clausen | 3661f3f | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 596 | switch (type) { |
Michael Hennerich | 67eedba | 2012-05-11 11:36:52 +0200 | [diff] [blame] | 597 | case IIO_VAL_INT: |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 598 | return sprintf(buf, "%d\n", vals[0]); |
Michael Hennerich | 67eedba | 2012-05-11 11:36:52 +0200 | [diff] [blame] | 599 | case IIO_VAL_INT_PLUS_MICRO_DB: |
| 600 | scale_db = true; |
| 601 | case IIO_VAL_INT_PLUS_MICRO: |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 602 | if (vals[1] < 0) |
Michal Nazarewicz | 8f57e4d | 2016-01-15 16:57:58 -0800 | [diff] [blame] | 603 | return sprintf(buf, "-%d.%06u%s\n", abs(vals[0]), |
| 604 | -vals[1], scale_db ? " dB" : ""); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 605 | else |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 606 | return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1], |
Michael Hennerich | 67eedba | 2012-05-11 11:36:52 +0200 | [diff] [blame] | 607 | scale_db ? " dB" : ""); |
| 608 | case IIO_VAL_INT_PLUS_NANO: |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 609 | if (vals[1] < 0) |
Michal Nazarewicz | 8f57e4d | 2016-01-15 16:57:58 -0800 | [diff] [blame] | 610 | return sprintf(buf, "-%d.%09u\n", abs(vals[0]), |
| 611 | -vals[1]); |
Michael Hennerich | 71646e2 | 2011-06-27 13:07:07 +0100 | [diff] [blame] | 612 | else |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 613 | return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); |
Lars-Peter Clausen | 7985e7c | 2012-09-14 16:21:00 +0100 | [diff] [blame] | 614 | case IIO_VAL_FRACTIONAL: |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 615 | tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]); |
Gregor Boirie | 171c009 | 2016-09-02 20:27:46 +0200 | [diff] [blame] | 616 | vals[0] = (int)div_s64_rem(tmp, 1000000000, &vals[1]); |
| 617 | return sprintf(buf, "%d.%09u\n", vals[0], abs(vals[1])); |
Lars-Peter Clausen | 103d9fb | 2012-10-16 17:29:00 +0100 | [diff] [blame] | 618 | case IIO_VAL_FRACTIONAL_LOG2: |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 619 | tmp = (s64)vals[0] * 1000000000LL >> vals[1]; |
| 620 | vals[1] = do_div(tmp, 1000000000LL); |
| 621 | vals[0] = tmp; |
| 622 | return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); |
| 623 | case IIO_VAL_INT_MULTIPLE: |
| 624 | { |
| 625 | int i; |
| 626 | int len = 0; |
| 627 | |
| 628 | for (i = 0; i < size; ++i) |
| 629 | len += snprintf(&buf[len], PAGE_SIZE - len, "%d ", |
| 630 | vals[i]); |
| 631 | len += snprintf(&buf[len], PAGE_SIZE - len, "\n"); |
| 632 | return len; |
| 633 | } |
Michael Hennerich | 67eedba | 2012-05-11 11:36:52 +0200 | [diff] [blame] | 634 | default: |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 635 | return 0; |
Michael Hennerich | 67eedba | 2012-05-11 11:36:52 +0200 | [diff] [blame] | 636 | } |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 637 | } |
Andrew F. Davis | 7d2c2aca | 2015-12-14 16:35:57 -0600 | [diff] [blame] | 638 | EXPORT_SYMBOL_GPL(iio_format_value); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 639 | |
Lars-Peter Clausen | 3661f3f | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 640 | static ssize_t iio_read_channel_info(struct device *dev, |
| 641 | struct device_attribute *attr, |
| 642 | char *buf) |
| 643 | { |
| 644 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 645 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 646 | int vals[INDIO_MAX_RAW_ELEMENTS]; |
| 647 | int ret; |
| 648 | int val_len = 2; |
| 649 | |
| 650 | if (indio_dev->info->read_raw_multi) |
| 651 | ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c, |
| 652 | INDIO_MAX_RAW_ELEMENTS, |
| 653 | vals, &val_len, |
| 654 | this_attr->address); |
| 655 | else |
| 656 | ret = indio_dev->info->read_raw(indio_dev, this_attr->c, |
| 657 | &vals[0], &vals[1], this_attr->address); |
Lars-Peter Clausen | 3661f3f | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 658 | |
| 659 | if (ret < 0) |
| 660 | return ret; |
| 661 | |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 662 | return iio_format_value(buf, ret, val_len, vals); |
Lars-Peter Clausen | 3661f3f | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 663 | } |
| 664 | |
Lars-Peter Clausen | 6807d72 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 665 | /** |
| 666 | * iio_str_to_fixpoint() - Parse a fixed-point number from a string |
| 667 | * @str: The string to parse |
| 668 | * @fract_mult: Multiplier for the first decimal place, should be a power of 10 |
| 669 | * @integer: The integer part of the number |
| 670 | * @fract: The fractional part of the number |
| 671 | * |
| 672 | * Returns 0 on success, or a negative error code if the string could not be |
| 673 | * parsed. |
| 674 | */ |
| 675 | int iio_str_to_fixpoint(const char *str, int fract_mult, |
| 676 | int *integer, int *fract) |
| 677 | { |
| 678 | int i = 0, f = 0; |
| 679 | bool integer_part = true, negative = false; |
| 680 | |
Sean Nyekjaer | f47dff3 | 2015-11-09 13:55:34 +0100 | [diff] [blame] | 681 | if (fract_mult == 0) { |
| 682 | *fract = 0; |
| 683 | |
| 684 | return kstrtoint(str, 0, integer); |
| 685 | } |
| 686 | |
Lars-Peter Clausen | 6807d72 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 687 | if (str[0] == '-') { |
| 688 | negative = true; |
| 689 | str++; |
| 690 | } else if (str[0] == '+') { |
| 691 | str++; |
| 692 | } |
| 693 | |
| 694 | while (*str) { |
| 695 | if ('0' <= *str && *str <= '9') { |
| 696 | if (integer_part) { |
| 697 | i = i * 10 + *str - '0'; |
| 698 | } else { |
| 699 | f += fract_mult * (*str - '0'); |
| 700 | fract_mult /= 10; |
| 701 | } |
| 702 | } else if (*str == '\n') { |
| 703 | if (*(str + 1) == '\0') |
| 704 | break; |
| 705 | else |
| 706 | return -EINVAL; |
| 707 | } else if (*str == '.' && integer_part) { |
| 708 | integer_part = false; |
| 709 | } else { |
| 710 | return -EINVAL; |
| 711 | } |
| 712 | str++; |
| 713 | } |
| 714 | |
| 715 | if (negative) { |
| 716 | if (i) |
| 717 | i = -i; |
| 718 | else |
| 719 | f = -f; |
| 720 | } |
| 721 | |
| 722 | *integer = i; |
| 723 | *fract = f; |
| 724 | |
| 725 | return 0; |
| 726 | } |
| 727 | EXPORT_SYMBOL_GPL(iio_str_to_fixpoint); |
| 728 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 729 | static ssize_t iio_write_channel_info(struct device *dev, |
| 730 | struct device_attribute *attr, |
| 731 | const char *buf, |
| 732 | size_t len) |
| 733 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 734 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 735 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
Lars-Peter Clausen | 6807d72 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 736 | int ret, fract_mult = 100000; |
| 737 | int integer, fract; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 738 | |
| 739 | /* Assumes decimal - precision based on number of digits */ |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 740 | if (!indio_dev->info->write_raw) |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 741 | return -EINVAL; |
Michael Hennerich | 5c04af0 | 2011-06-27 13:07:10 +0100 | [diff] [blame] | 742 | |
| 743 | if (indio_dev->info->write_raw_get_fmt) |
| 744 | switch (indio_dev->info->write_raw_get_fmt(indio_dev, |
| 745 | this_attr->c, this_attr->address)) { |
Sean Nyekjaer | f47dff3 | 2015-11-09 13:55:34 +0100 | [diff] [blame] | 746 | case IIO_VAL_INT: |
| 747 | fract_mult = 0; |
| 748 | break; |
Michael Hennerich | 5c04af0 | 2011-06-27 13:07:10 +0100 | [diff] [blame] | 749 | case IIO_VAL_INT_PLUS_MICRO: |
| 750 | fract_mult = 100000; |
| 751 | break; |
| 752 | case IIO_VAL_INT_PLUS_NANO: |
| 753 | fract_mult = 100000000; |
| 754 | break; |
| 755 | default: |
| 756 | return -EINVAL; |
| 757 | } |
| 758 | |
Lars-Peter Clausen | 6807d72 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 759 | ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract); |
| 760 | if (ret) |
| 761 | return ret; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 762 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 763 | ret = indio_dev->info->write_raw(indio_dev, this_attr->c, |
Michael Hennerich | 5c04af0 | 2011-06-27 13:07:10 +0100 | [diff] [blame] | 764 | integer, fract, this_attr->address); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 765 | if (ret) |
| 766 | return ret; |
| 767 | |
| 768 | return len; |
| 769 | } |
| 770 | |
Jonathan Cameron | df9c1c4 | 2011-08-12 17:56:03 +0100 | [diff] [blame] | 771 | static |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 772 | int __iio_device_attr_init(struct device_attribute *dev_attr, |
| 773 | const char *postfix, |
| 774 | struct iio_chan_spec const *chan, |
| 775 | ssize_t (*readfunc)(struct device *dev, |
| 776 | struct device_attribute *attr, |
| 777 | char *buf), |
| 778 | ssize_t (*writefunc)(struct device *dev, |
| 779 | struct device_attribute *attr, |
| 780 | const char *buf, |
| 781 | size_t len), |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 782 | enum iio_shared_by shared_by) |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 783 | { |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 784 | int ret = 0; |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 785 | char *name = NULL; |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 786 | char *full_postfix; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 787 | sysfs_attr_init(&dev_attr->attr); |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 788 | |
| 789 | /* Build up postfix of <extend_name>_<modifier>_postfix */ |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 790 | if (chan->modified && (shared_by == IIO_SEPARATE)) { |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 791 | if (chan->extend_name) |
| 792 | full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", |
| 793 | iio_modifier_names[chan |
| 794 | ->channel2], |
| 795 | chan->extend_name, |
| 796 | postfix); |
| 797 | else |
| 798 | full_postfix = kasprintf(GFP_KERNEL, "%s_%s", |
| 799 | iio_modifier_names[chan |
| 800 | ->channel2], |
| 801 | postfix); |
| 802 | } else { |
Lars-Peter Clausen | 77bfa8b | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 803 | if (chan->extend_name == NULL || shared_by != IIO_SEPARATE) |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 804 | full_postfix = kstrdup(postfix, GFP_KERNEL); |
| 805 | else |
| 806 | full_postfix = kasprintf(GFP_KERNEL, |
| 807 | "%s_%s", |
| 808 | chan->extend_name, |
| 809 | postfix); |
| 810 | } |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 811 | if (full_postfix == NULL) |
| 812 | return -ENOMEM; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 813 | |
Justin P. Mattock | 4abf6f8 | 2012-02-29 22:00:38 -0800 | [diff] [blame] | 814 | if (chan->differential) { /* Differential can not have modifier */ |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 815 | switch (shared_by) { |
Jonathan Cameron | c006ec8 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 816 | case IIO_SHARED_BY_ALL: |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 817 | name = kasprintf(GFP_KERNEL, "%s", full_postfix); |
Jonathan Cameron | c006ec8 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 818 | break; |
| 819 | case IIO_SHARED_BY_DIR: |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 820 | name = kasprintf(GFP_KERNEL, "%s_%s", |
Jonathan Cameron | c006ec8 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 821 | iio_direction[chan->output], |
| 822 | full_postfix); |
| 823 | break; |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 824 | case IIO_SHARED_BY_TYPE: |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 825 | name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s", |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 826 | iio_direction[chan->output], |
| 827 | iio_chan_type_name_spec[chan->type], |
| 828 | iio_chan_type_name_spec[chan->type], |
| 829 | full_postfix); |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 830 | break; |
| 831 | case IIO_SEPARATE: |
| 832 | if (!chan->indexed) { |
Dan Carpenter | 231bfe5 | 2015-11-21 13:33:00 +0300 | [diff] [blame] | 833 | WARN(1, "Differential channels must be indexed\n"); |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 834 | ret = -EINVAL; |
| 835 | goto error_free_full_postfix; |
| 836 | } |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 837 | name = kasprintf(GFP_KERNEL, |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 838 | "%s_%s%d-%s%d_%s", |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 839 | iio_direction[chan->output], |
| 840 | iio_chan_type_name_spec[chan->type], |
| 841 | chan->channel, |
| 842 | iio_chan_type_name_spec[chan->type], |
| 843 | chan->channel2, |
| 844 | full_postfix); |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 845 | break; |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 846 | } |
| 847 | } else { /* Single ended */ |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 848 | switch (shared_by) { |
Jonathan Cameron | c006ec8 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 849 | case IIO_SHARED_BY_ALL: |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 850 | name = kasprintf(GFP_KERNEL, "%s", full_postfix); |
Jonathan Cameron | c006ec8 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 851 | break; |
| 852 | case IIO_SHARED_BY_DIR: |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 853 | name = kasprintf(GFP_KERNEL, "%s_%s", |
Jonathan Cameron | c006ec8 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 854 | iio_direction[chan->output], |
| 855 | full_postfix); |
| 856 | break; |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 857 | case IIO_SHARED_BY_TYPE: |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 858 | name = kasprintf(GFP_KERNEL, "%s_%s_%s", |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 859 | iio_direction[chan->output], |
| 860 | iio_chan_type_name_spec[chan->type], |
| 861 | full_postfix); |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 862 | break; |
| 863 | |
| 864 | case IIO_SEPARATE: |
| 865 | if (chan->indexed) |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 866 | name = kasprintf(GFP_KERNEL, "%s_%s%d_%s", |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 867 | iio_direction[chan->output], |
| 868 | iio_chan_type_name_spec[chan->type], |
| 869 | chan->channel, |
| 870 | full_postfix); |
| 871 | else |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 872 | name = kasprintf(GFP_KERNEL, "%s_%s_%s", |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 873 | iio_direction[chan->output], |
| 874 | iio_chan_type_name_spec[chan->type], |
| 875 | full_postfix); |
| 876 | break; |
| 877 | } |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 878 | } |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 879 | if (name == NULL) { |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 880 | ret = -ENOMEM; |
| 881 | goto error_free_full_postfix; |
| 882 | } |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 883 | dev_attr->attr.name = name; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 884 | |
| 885 | if (readfunc) { |
| 886 | dev_attr->attr.mode |= S_IRUGO; |
| 887 | dev_attr->show = readfunc; |
| 888 | } |
| 889 | |
| 890 | if (writefunc) { |
| 891 | dev_attr->attr.mode |= S_IWUSR; |
| 892 | dev_attr->store = writefunc; |
| 893 | } |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 894 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 895 | error_free_full_postfix: |
| 896 | kfree(full_postfix); |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 897 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 898 | return ret; |
| 899 | } |
| 900 | |
Jonathan Cameron | df9c1c4 | 2011-08-12 17:56:03 +0100 | [diff] [blame] | 901 | static void __iio_device_attr_deinit(struct device_attribute *dev_attr) |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 902 | { |
| 903 | kfree(dev_attr->attr.name); |
| 904 | } |
| 905 | |
| 906 | int __iio_add_chan_devattr(const char *postfix, |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 907 | struct iio_chan_spec const *chan, |
| 908 | ssize_t (*readfunc)(struct device *dev, |
| 909 | struct device_attribute *attr, |
| 910 | char *buf), |
| 911 | ssize_t (*writefunc)(struct device *dev, |
| 912 | struct device_attribute *attr, |
| 913 | const char *buf, |
| 914 | size_t len), |
Jonathan Cameron | e614a54 | 2011-09-02 17:14:41 +0100 | [diff] [blame] | 915 | u64 mask, |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 916 | enum iio_shared_by shared_by, |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 917 | struct device *dev, |
| 918 | struct list_head *attr_list) |
| 919 | { |
| 920 | int ret; |
| 921 | struct iio_dev_attr *iio_attr, *t; |
| 922 | |
Sachin Kamat | 670c110 | 2013-10-24 12:53:00 +0100 | [diff] [blame] | 923 | iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL); |
Hartmut Knaack | 92825ff | 2014-02-16 11:53:00 +0000 | [diff] [blame] | 924 | if (iio_attr == NULL) |
| 925 | return -ENOMEM; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 926 | ret = __iio_device_attr_init(&iio_attr->dev_attr, |
| 927 | postfix, chan, |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 928 | readfunc, writefunc, shared_by); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 929 | if (ret) |
| 930 | goto error_iio_dev_attr_free; |
| 931 | iio_attr->c = chan; |
| 932 | iio_attr->address = mask; |
| 933 | list_for_each_entry(t, attr_list, l) |
| 934 | if (strcmp(t->dev_attr.attr.name, |
| 935 | iio_attr->dev_attr.attr.name) == 0) { |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 936 | if (shared_by == IIO_SEPARATE) |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 937 | dev_err(dev, "tried to double register : %s\n", |
| 938 | t->dev_attr.attr.name); |
| 939 | ret = -EBUSY; |
| 940 | goto error_device_attr_deinit; |
| 941 | } |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 942 | list_add(&iio_attr->l, attr_list); |
| 943 | |
| 944 | return 0; |
| 945 | |
| 946 | error_device_attr_deinit: |
| 947 | __iio_device_attr_deinit(&iio_attr->dev_attr); |
| 948 | error_iio_dev_attr_free: |
| 949 | kfree(iio_attr); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 950 | return ret; |
| 951 | } |
| 952 | |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 953 | static int iio_device_add_info_mask_type(struct iio_dev *indio_dev, |
| 954 | struct iio_chan_spec const *chan, |
| 955 | enum iio_shared_by shared_by, |
| 956 | const long *infomask) |
| 957 | { |
| 958 | int i, ret, attrcount = 0; |
| 959 | |
| 960 | for_each_set_bit(i, infomask, sizeof(infomask)*8) { |
Jonathan Cameron | ef4b485 | 2014-01-03 22:24:00 +0000 | [diff] [blame] | 961 | if (i >= ARRAY_SIZE(iio_chan_info_postfix)) |
| 962 | return -EINVAL; |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 963 | ret = __iio_add_chan_devattr(iio_chan_info_postfix[i], |
| 964 | chan, |
| 965 | &iio_read_channel_info, |
| 966 | &iio_write_channel_info, |
| 967 | i, |
| 968 | shared_by, |
| 969 | &indio_dev->dev, |
| 970 | &indio_dev->channel_attr_list); |
| 971 | if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) |
| 972 | continue; |
| 973 | else if (ret < 0) |
| 974 | return ret; |
| 975 | attrcount++; |
| 976 | } |
| 977 | |
| 978 | return attrcount; |
| 979 | } |
| 980 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 981 | static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev, |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 982 | struct iio_chan_spec const *chan) |
| 983 | { |
Jonathan Cameron | 5ccb3ad | 2012-04-15 17:41:16 +0100 | [diff] [blame] | 984 | int ret, attrcount = 0; |
Lars-Peter Clausen | 5f420b4 | 2012-02-21 18:38:12 +0100 | [diff] [blame] | 985 | const struct iio_chan_spec_ext_info *ext_info; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 986 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 987 | if (chan->channel < 0) |
| 988 | return 0; |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 989 | ret = iio_device_add_info_mask_type(indio_dev, chan, |
| 990 | IIO_SEPARATE, |
| 991 | &chan->info_mask_separate); |
| 992 | if (ret < 0) |
| 993 | return ret; |
| 994 | attrcount += ret; |
| 995 | |
| 996 | ret = iio_device_add_info_mask_type(indio_dev, chan, |
| 997 | IIO_SHARED_BY_TYPE, |
| 998 | &chan->info_mask_shared_by_type); |
| 999 | if (ret < 0) |
| 1000 | return ret; |
| 1001 | attrcount += ret; |
Lars-Peter Clausen | 5f420b4 | 2012-02-21 18:38:12 +0100 | [diff] [blame] | 1002 | |
Jonathan Cameron | c006ec8 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 1003 | ret = iio_device_add_info_mask_type(indio_dev, chan, |
| 1004 | IIO_SHARED_BY_DIR, |
| 1005 | &chan->info_mask_shared_by_dir); |
| 1006 | if (ret < 0) |
| 1007 | return ret; |
| 1008 | attrcount += ret; |
| 1009 | |
| 1010 | ret = iio_device_add_info_mask_type(indio_dev, chan, |
| 1011 | IIO_SHARED_BY_ALL, |
| 1012 | &chan->info_mask_shared_by_all); |
| 1013 | if (ret < 0) |
| 1014 | return ret; |
| 1015 | attrcount += ret; |
| 1016 | |
Lars-Peter Clausen | 5f420b4 | 2012-02-21 18:38:12 +0100 | [diff] [blame] | 1017 | if (chan->ext_info) { |
| 1018 | unsigned int i = 0; |
| 1019 | for (ext_info = chan->ext_info; ext_info->name; ext_info++) { |
| 1020 | ret = __iio_add_chan_devattr(ext_info->name, |
| 1021 | chan, |
| 1022 | ext_info->read ? |
| 1023 | &iio_read_channel_ext_info : NULL, |
| 1024 | ext_info->write ? |
| 1025 | &iio_write_channel_ext_info : NULL, |
| 1026 | i, |
| 1027 | ext_info->shared, |
| 1028 | &indio_dev->dev, |
| 1029 | &indio_dev->channel_attr_list); |
| 1030 | i++; |
| 1031 | if (ret == -EBUSY && ext_info->shared) |
| 1032 | continue; |
| 1033 | |
| 1034 | if (ret) |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 1035 | return ret; |
Lars-Peter Clausen | 5f420b4 | 2012-02-21 18:38:12 +0100 | [diff] [blame] | 1036 | |
| 1037 | attrcount++; |
| 1038 | } |
| 1039 | } |
| 1040 | |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 1041 | return attrcount; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1042 | } |
| 1043 | |
Lars-Peter Clausen | 84088eb | 2013-10-07 12:50:00 +0100 | [diff] [blame] | 1044 | /** |
| 1045 | * iio_free_chan_devattr_list() - Free a list of IIO device attributes |
| 1046 | * @attr_list: List of IIO device attributes |
| 1047 | * |
| 1048 | * This function frees the memory allocated for each of the IIO device |
Martin Fuzzey | c1b03ab | 2015-02-19 15:17:44 +0100 | [diff] [blame] | 1049 | * attributes in the list. |
Lars-Peter Clausen | 84088eb | 2013-10-07 12:50:00 +0100 | [diff] [blame] | 1050 | */ |
| 1051 | void iio_free_chan_devattr_list(struct list_head *attr_list) |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1052 | { |
Lars-Peter Clausen | 84088eb | 2013-10-07 12:50:00 +0100 | [diff] [blame] | 1053 | struct iio_dev_attr *p, *n; |
| 1054 | |
| 1055 | list_for_each_entry_safe(p, n, attr_list, l) { |
| 1056 | kfree(p->dev_attr.attr.name); |
Martin Fuzzey | c1b03ab | 2015-02-19 15:17:44 +0100 | [diff] [blame] | 1057 | list_del(&p->l); |
Lars-Peter Clausen | 84088eb | 2013-10-07 12:50:00 +0100 | [diff] [blame] | 1058 | kfree(p); |
| 1059 | } |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1060 | } |
| 1061 | |
Jonathan Cameron | 1b73288 | 2011-05-18 14:41:43 +0100 | [diff] [blame] | 1062 | static ssize_t iio_show_dev_name(struct device *dev, |
| 1063 | struct device_attribute *attr, |
| 1064 | char *buf) |
| 1065 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 1066 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Kees Cook | 598db58 | 2014-03-13 16:46:00 +0000 | [diff] [blame] | 1067 | return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name); |
Jonathan Cameron | 1b73288 | 2011-05-18 14:41:43 +0100 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL); |
| 1071 | |
Gregor Boirie | bc2b7da | 2016-03-09 19:05:49 +0100 | [diff] [blame] | 1072 | static ssize_t iio_show_timestamp_clock(struct device *dev, |
| 1073 | struct device_attribute *attr, |
| 1074 | char *buf) |
| 1075 | { |
| 1076 | const struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 1077 | const clockid_t clk = iio_device_get_clock(indio_dev); |
| 1078 | const char *name; |
| 1079 | ssize_t sz; |
| 1080 | |
| 1081 | switch (clk) { |
| 1082 | case CLOCK_REALTIME: |
| 1083 | name = "realtime\n"; |
| 1084 | sz = sizeof("realtime\n"); |
| 1085 | break; |
| 1086 | case CLOCK_MONOTONIC: |
| 1087 | name = "monotonic\n"; |
| 1088 | sz = sizeof("monotonic\n"); |
| 1089 | break; |
| 1090 | case CLOCK_MONOTONIC_RAW: |
| 1091 | name = "monotonic_raw\n"; |
| 1092 | sz = sizeof("monotonic_raw\n"); |
| 1093 | break; |
| 1094 | case CLOCK_REALTIME_COARSE: |
| 1095 | name = "realtime_coarse\n"; |
| 1096 | sz = sizeof("realtime_coarse\n"); |
| 1097 | break; |
| 1098 | case CLOCK_MONOTONIC_COARSE: |
| 1099 | name = "monotonic_coarse\n"; |
| 1100 | sz = sizeof("monotonic_coarse\n"); |
| 1101 | break; |
| 1102 | case CLOCK_BOOTTIME: |
| 1103 | name = "boottime\n"; |
| 1104 | sz = sizeof("boottime\n"); |
| 1105 | break; |
| 1106 | case CLOCK_TAI: |
| 1107 | name = "tai\n"; |
| 1108 | sz = sizeof("tai\n"); |
| 1109 | break; |
| 1110 | default: |
| 1111 | BUG(); |
| 1112 | } |
| 1113 | |
| 1114 | memcpy(buf, name, sz); |
| 1115 | return sz; |
| 1116 | } |
| 1117 | |
| 1118 | static ssize_t iio_store_timestamp_clock(struct device *dev, |
| 1119 | struct device_attribute *attr, |
| 1120 | const char *buf, size_t len) |
| 1121 | { |
| 1122 | clockid_t clk; |
| 1123 | int ret; |
| 1124 | |
| 1125 | if (sysfs_streq(buf, "realtime")) |
| 1126 | clk = CLOCK_REALTIME; |
| 1127 | else if (sysfs_streq(buf, "monotonic")) |
| 1128 | clk = CLOCK_MONOTONIC; |
| 1129 | else if (sysfs_streq(buf, "monotonic_raw")) |
| 1130 | clk = CLOCK_MONOTONIC_RAW; |
| 1131 | else if (sysfs_streq(buf, "realtime_coarse")) |
| 1132 | clk = CLOCK_REALTIME_COARSE; |
| 1133 | else if (sysfs_streq(buf, "monotonic_coarse")) |
| 1134 | clk = CLOCK_MONOTONIC_COARSE; |
| 1135 | else if (sysfs_streq(buf, "boottime")) |
| 1136 | clk = CLOCK_BOOTTIME; |
| 1137 | else if (sysfs_streq(buf, "tai")) |
| 1138 | clk = CLOCK_TAI; |
| 1139 | else |
| 1140 | return -EINVAL; |
| 1141 | |
| 1142 | ret = iio_device_set_clock(dev_to_iio_dev(dev), clk); |
| 1143 | if (ret) |
| 1144 | return ret; |
| 1145 | |
| 1146 | return len; |
| 1147 | } |
| 1148 | |
| 1149 | static DEVICE_ATTR(current_timestamp_clock, S_IRUGO | S_IWUSR, |
| 1150 | iio_show_timestamp_clock, iio_store_timestamp_clock); |
| 1151 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1152 | static int iio_device_register_sysfs(struct iio_dev *indio_dev) |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1153 | { |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1154 | int i, ret = 0, attrcount, attrn, attrcount_orig = 0; |
Lars-Peter Clausen | 84088eb | 2013-10-07 12:50:00 +0100 | [diff] [blame] | 1155 | struct iio_dev_attr *p; |
Gregor Boirie | bc2b7da | 2016-03-09 19:05:49 +0100 | [diff] [blame] | 1156 | struct attribute **attr, *clk = NULL; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1157 | |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1158 | /* First count elements in any existing group */ |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1159 | if (indio_dev->info->attrs) { |
| 1160 | attr = indio_dev->info->attrs->attrs; |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1161 | while (*attr++ != NULL) |
| 1162 | attrcount_orig++; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1163 | } |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1164 | attrcount = attrcount_orig; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1165 | /* |
| 1166 | * New channel registration method - relies on the fact a group does |
Peter Meerwald | d25b380 | 2012-08-26 13:43:00 +0100 | [diff] [blame] | 1167 | * not need to be initialized if its name is NULL. |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1168 | */ |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1169 | if (indio_dev->channels) |
| 1170 | for (i = 0; i < indio_dev->num_channels; i++) { |
Gregor Boirie | bc2b7da | 2016-03-09 19:05:49 +0100 | [diff] [blame] | 1171 | const struct iio_chan_spec *chan = |
| 1172 | &indio_dev->channels[i]; |
| 1173 | |
| 1174 | if (chan->type == IIO_TIMESTAMP) |
| 1175 | clk = &dev_attr_current_timestamp_clock.attr; |
| 1176 | |
| 1177 | ret = iio_device_add_channel_sysfs(indio_dev, chan); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1178 | if (ret < 0) |
| 1179 | goto error_clear_attrs; |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1180 | attrcount += ret; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1181 | } |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1182 | |
Gregor Boirie | bc2b7da | 2016-03-09 19:05:49 +0100 | [diff] [blame] | 1183 | if (indio_dev->event_interface) |
| 1184 | clk = &dev_attr_current_timestamp_clock.attr; |
| 1185 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1186 | if (indio_dev->name) |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1187 | attrcount++; |
Gregor Boirie | bc2b7da | 2016-03-09 19:05:49 +0100 | [diff] [blame] | 1188 | if (clk) |
| 1189 | attrcount++; |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1190 | |
Thomas Meyer | d83fb18 | 2011-11-29 22:08:00 +0100 | [diff] [blame] | 1191 | indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1, |
| 1192 | sizeof(indio_dev->chan_attr_group.attrs[0]), |
| 1193 | GFP_KERNEL); |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1194 | if (indio_dev->chan_attr_group.attrs == NULL) { |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1195 | ret = -ENOMEM; |
| 1196 | goto error_clear_attrs; |
Jonathan Cameron | 1b73288 | 2011-05-18 14:41:43 +0100 | [diff] [blame] | 1197 | } |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1198 | /* Copy across original attributes */ |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1199 | if (indio_dev->info->attrs) |
| 1200 | memcpy(indio_dev->chan_attr_group.attrs, |
| 1201 | indio_dev->info->attrs->attrs, |
| 1202 | sizeof(indio_dev->chan_attr_group.attrs[0]) |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1203 | *attrcount_orig); |
| 1204 | attrn = attrcount_orig; |
| 1205 | /* Add all elements from the list. */ |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1206 | list_for_each_entry(p, &indio_dev->channel_attr_list, l) |
| 1207 | indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr; |
| 1208 | if (indio_dev->name) |
| 1209 | indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr; |
Gregor Boirie | bc2b7da | 2016-03-09 19:05:49 +0100 | [diff] [blame] | 1210 | if (clk) |
| 1211 | indio_dev->chan_attr_group.attrs[attrn++] = clk; |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1212 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1213 | indio_dev->groups[indio_dev->groupcounter++] = |
| 1214 | &indio_dev->chan_attr_group; |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1215 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1216 | return 0; |
Jonathan Cameron | 1b73288 | 2011-05-18 14:41:43 +0100 | [diff] [blame] | 1217 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1218 | error_clear_attrs: |
Lars-Peter Clausen | 84088eb | 2013-10-07 12:50:00 +0100 | [diff] [blame] | 1219 | iio_free_chan_devattr_list(&indio_dev->channel_attr_list); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1220 | |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1221 | return ret; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1222 | } |
| 1223 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1224 | static void iio_device_unregister_sysfs(struct iio_dev *indio_dev) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1225 | { |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 1226 | |
Lars-Peter Clausen | 84088eb | 2013-10-07 12:50:00 +0100 | [diff] [blame] | 1227 | iio_free_chan_devattr_list(&indio_dev->channel_attr_list); |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1228 | kfree(indio_dev->chan_attr_group.attrs); |
Martin Fuzzey | c1b03ab | 2015-02-19 15:17:44 +0100 | [diff] [blame] | 1229 | indio_dev->chan_attr_group.attrs = NULL; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1230 | } |
| 1231 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1232 | static void iio_dev_release(struct device *device) |
| 1233 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 1234 | struct iio_dev *indio_dev = dev_to_iio_dev(device); |
Vladimir Barinov | 735ad07 | 2015-08-20 22:37:39 +0300 | [diff] [blame] | 1235 | if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED)) |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1236 | iio_device_unregister_trigger_consumer(indio_dev); |
| 1237 | iio_device_unregister_eventset(indio_dev); |
| 1238 | iio_device_unregister_sysfs(indio_dev); |
Lars-Peter Clausen | e407fd6 | 2012-06-04 10:41:42 +0200 | [diff] [blame] | 1239 | |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 1240 | iio_buffer_put(indio_dev->buffer); |
| 1241 | |
Lars-Peter Clausen | e407fd6 | 2012-06-04 10:41:42 +0200 | [diff] [blame] | 1242 | ida_simple_remove(&iio_ida, indio_dev->id); |
| 1243 | kfree(indio_dev); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1244 | } |
| 1245 | |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 1246 | struct device_type iio_device_type = { |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1247 | .name = "iio_device", |
| 1248 | .release = iio_dev_release, |
| 1249 | }; |
| 1250 | |
Sachin Kamat | a7e57dc | 2013-10-29 11:39:00 +0000 | [diff] [blame] | 1251 | /** |
| 1252 | * iio_device_alloc() - allocate an iio_dev from a driver |
| 1253 | * @sizeof_priv: Space to allocate for private structure. |
| 1254 | **/ |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 1255 | struct iio_dev *iio_device_alloc(int sizeof_priv) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1256 | { |
Jonathan Cameron | 6f7c8ee | 2011-04-15 18:55:56 +0100 | [diff] [blame] | 1257 | struct iio_dev *dev; |
| 1258 | size_t alloc_size; |
| 1259 | |
| 1260 | alloc_size = sizeof(struct iio_dev); |
| 1261 | if (sizeof_priv) { |
| 1262 | alloc_size = ALIGN(alloc_size, IIO_ALIGN); |
| 1263 | alloc_size += sizeof_priv; |
| 1264 | } |
| 1265 | /* ensure 32-byte alignment of whole construct ? */ |
| 1266 | alloc_size += IIO_ALIGN - 1; |
| 1267 | |
| 1268 | dev = kzalloc(alloc_size, GFP_KERNEL); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1269 | |
| 1270 | if (dev) { |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1271 | dev->dev.groups = dev->groups; |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 1272 | dev->dev.type = &iio_device_type; |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 1273 | dev->dev.bus = &iio_bus_type; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1274 | device_initialize(&dev->dev); |
| 1275 | dev_set_drvdata(&dev->dev, (void *)dev); |
| 1276 | mutex_init(&dev->mlock); |
Jonathan Cameron | ac917a8 | 2012-02-15 19:48:00 +0000 | [diff] [blame] | 1277 | mutex_init(&dev->info_exist_lock); |
Lars-Peter Clausen | e407fd6 | 2012-06-04 10:41:42 +0200 | [diff] [blame] | 1278 | INIT_LIST_HEAD(&dev->channel_attr_list); |
Jonathan Cameron | a9e39f9 | 2011-09-14 13:01:25 +0100 | [diff] [blame] | 1279 | |
| 1280 | dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL); |
| 1281 | if (dev->id < 0) { |
| 1282 | /* cannot use a dev_err as the name isn't available */ |
Sachin Kamat | 3176dd5 | 2013-10-24 12:53:00 +0100 | [diff] [blame] | 1283 | pr_err("failed to get device id\n"); |
Jonathan Cameron | a9e39f9 | 2011-09-14 13:01:25 +0100 | [diff] [blame] | 1284 | kfree(dev); |
| 1285 | return NULL; |
| 1286 | } |
| 1287 | dev_set_name(&dev->dev, "iio:device%d", dev->id); |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 1288 | INIT_LIST_HEAD(&dev->buffer_list); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | return dev; |
| 1292 | } |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 1293 | EXPORT_SYMBOL(iio_device_alloc); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1294 | |
Sachin Kamat | a7e57dc | 2013-10-29 11:39:00 +0000 | [diff] [blame] | 1295 | /** |
| 1296 | * iio_device_free() - free an iio_dev from a driver |
| 1297 | * @dev: the iio_dev associated with the device |
| 1298 | **/ |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 1299 | void iio_device_free(struct iio_dev *dev) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1300 | { |
Lars-Peter Clausen | e407fd6 | 2012-06-04 10:41:42 +0200 | [diff] [blame] | 1301 | if (dev) |
| 1302 | put_device(&dev->dev); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1303 | } |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 1304 | EXPORT_SYMBOL(iio_device_free); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1305 | |
Grygorii Strashko | 9dabaf5 | 2013-07-18 11:19:00 +0100 | [diff] [blame] | 1306 | static void devm_iio_device_release(struct device *dev, void *res) |
| 1307 | { |
| 1308 | iio_device_free(*(struct iio_dev **)res); |
| 1309 | } |
| 1310 | |
| 1311 | static int devm_iio_device_match(struct device *dev, void *res, void *data) |
| 1312 | { |
| 1313 | struct iio_dev **r = res; |
| 1314 | if (!r || !*r) { |
| 1315 | WARN_ON(!r || !*r); |
| 1316 | return 0; |
| 1317 | } |
| 1318 | return *r == data; |
| 1319 | } |
| 1320 | |
Sachin Kamat | a7e57dc | 2013-10-29 11:39:00 +0000 | [diff] [blame] | 1321 | /** |
| 1322 | * devm_iio_device_alloc - Resource-managed iio_device_alloc() |
| 1323 | * @dev: Device to allocate iio_dev for |
| 1324 | * @sizeof_priv: Space to allocate for private structure. |
| 1325 | * |
| 1326 | * Managed iio_device_alloc. iio_dev allocated with this function is |
| 1327 | * automatically freed on driver detach. |
| 1328 | * |
| 1329 | * If an iio_dev allocated with this function needs to be freed separately, |
| 1330 | * devm_iio_device_free() must be used. |
| 1331 | * |
| 1332 | * RETURNS: |
| 1333 | * Pointer to allocated iio_dev on success, NULL on failure. |
| 1334 | */ |
Grygorii Strashko | 9dabaf5 | 2013-07-18 11:19:00 +0100 | [diff] [blame] | 1335 | struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv) |
| 1336 | { |
| 1337 | struct iio_dev **ptr, *iio_dev; |
| 1338 | |
| 1339 | ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr), |
| 1340 | GFP_KERNEL); |
| 1341 | if (!ptr) |
| 1342 | return NULL; |
| 1343 | |
Grygorii Strashko | 9dabaf5 | 2013-07-18 11:19:00 +0100 | [diff] [blame] | 1344 | iio_dev = iio_device_alloc(sizeof_priv); |
| 1345 | if (iio_dev) { |
| 1346 | *ptr = iio_dev; |
| 1347 | devres_add(dev, ptr); |
| 1348 | } else { |
| 1349 | devres_free(ptr); |
| 1350 | } |
| 1351 | |
| 1352 | return iio_dev; |
| 1353 | } |
| 1354 | EXPORT_SYMBOL_GPL(devm_iio_device_alloc); |
| 1355 | |
Sachin Kamat | a7e57dc | 2013-10-29 11:39:00 +0000 | [diff] [blame] | 1356 | /** |
| 1357 | * devm_iio_device_free - Resource-managed iio_device_free() |
| 1358 | * @dev: Device this iio_dev belongs to |
| 1359 | * @iio_dev: the iio_dev associated with the device |
| 1360 | * |
| 1361 | * Free iio_dev allocated with devm_iio_device_alloc(). |
| 1362 | */ |
Grygorii Strashko | 9dabaf5 | 2013-07-18 11:19:00 +0100 | [diff] [blame] | 1363 | void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev) |
| 1364 | { |
| 1365 | int rc; |
| 1366 | |
| 1367 | rc = devres_release(dev, devm_iio_device_release, |
| 1368 | devm_iio_device_match, iio_dev); |
| 1369 | WARN_ON(rc); |
| 1370 | } |
| 1371 | EXPORT_SYMBOL_GPL(devm_iio_device_free); |
| 1372 | |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1373 | /** |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 1374 | * iio_chrdev_open() - chrdev file open for buffer access and ioctls |
Cristina Opriceana | 2498dcf | 2015-07-24 16:16:19 +0300 | [diff] [blame] | 1375 | * @inode: Inode structure for identifying the device in the file system |
| 1376 | * @filp: File structure for iio device used to keep and later access |
| 1377 | * private data |
| 1378 | * |
| 1379 | * Return: 0 on success or -EBUSY if the device is already opened |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1380 | **/ |
| 1381 | static int iio_chrdev_open(struct inode *inode, struct file *filp) |
| 1382 | { |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1383 | struct iio_dev *indio_dev = container_of(inode->i_cdev, |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1384 | struct iio_dev, chrdev); |
Lars-Peter Clausen | bb01443 | 2011-12-19 15:23:45 +0100 | [diff] [blame] | 1385 | |
| 1386 | if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags)) |
| 1387 | return -EBUSY; |
| 1388 | |
Lars-Peter Clausen | cadc212 | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1389 | iio_device_get(indio_dev); |
| 1390 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1391 | filp->private_data = indio_dev; |
Jonathan Cameron | 30eb82f | 2011-09-21 11:16:02 +0100 | [diff] [blame] | 1392 | |
Lars-Peter Clausen | 7933514 | 2011-12-19 15:23:49 +0100 | [diff] [blame] | 1393 | return 0; |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | /** |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 1397 | * iio_chrdev_release() - chrdev file close buffer access and ioctls |
Cristina Opriceana | 2498dcf | 2015-07-24 16:16:19 +0300 | [diff] [blame] | 1398 | * @inode: Inode structure pointer for the char device |
| 1399 | * @filp: File structure pointer for the char device |
| 1400 | * |
| 1401 | * Return: 0 for successful release |
| 1402 | */ |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1403 | static int iio_chrdev_release(struct inode *inode, struct file *filp) |
| 1404 | { |
Lars-Peter Clausen | bb01443 | 2011-12-19 15:23:45 +0100 | [diff] [blame] | 1405 | struct iio_dev *indio_dev = container_of(inode->i_cdev, |
| 1406 | struct iio_dev, chrdev); |
Lars-Peter Clausen | bb01443 | 2011-12-19 15:23:45 +0100 | [diff] [blame] | 1407 | clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags); |
Lars-Peter Clausen | cadc212 | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1408 | iio_device_put(indio_dev); |
| 1409 | |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1410 | return 0; |
| 1411 | } |
| 1412 | |
| 1413 | /* Somewhat of a cross file organization violation - ioctls here are actually |
| 1414 | * event related */ |
| 1415 | static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 1416 | { |
| 1417 | struct iio_dev *indio_dev = filp->private_data; |
| 1418 | int __user *ip = (int __user *)arg; |
| 1419 | int fd; |
| 1420 | |
Lars-Peter Clausen | f18e7a0 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 1421 | if (!indio_dev->info) |
| 1422 | return -ENODEV; |
| 1423 | |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1424 | if (cmd == IIO_GET_EVENT_FD_IOCTL) { |
| 1425 | fd = iio_event_getfd(indio_dev); |
Linus Walleij | 3f9059b | 2015-08-11 11:56:40 +0200 | [diff] [blame] | 1426 | if (fd < 0) |
| 1427 | return fd; |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1428 | if (copy_to_user(ip, &fd, sizeof(fd))) |
| 1429 | return -EFAULT; |
| 1430 | return 0; |
| 1431 | } |
| 1432 | return -EINVAL; |
| 1433 | } |
| 1434 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 1435 | static const struct file_operations iio_buffer_fileops = { |
| 1436 | .read = iio_buffer_read_first_n_outer_addr, |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1437 | .release = iio_chrdev_release, |
| 1438 | .open = iio_chrdev_open, |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 1439 | .poll = iio_buffer_poll_addr, |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1440 | .owner = THIS_MODULE, |
| 1441 | .llseek = noop_llseek, |
| 1442 | .unlocked_ioctl = iio_ioctl, |
| 1443 | .compat_ioctl = iio_ioctl, |
| 1444 | }; |
| 1445 | |
Vlad Dogaru | 8f5d872 | 2014-12-29 11:37:48 +0200 | [diff] [blame] | 1446 | static int iio_check_unique_scan_index(struct iio_dev *indio_dev) |
| 1447 | { |
| 1448 | int i, j; |
| 1449 | const struct iio_chan_spec *channels = indio_dev->channels; |
| 1450 | |
| 1451 | if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES)) |
| 1452 | return 0; |
| 1453 | |
| 1454 | for (i = 0; i < indio_dev->num_channels - 1; i++) { |
| 1455 | if (channels[i].scan_index < 0) |
| 1456 | continue; |
| 1457 | for (j = i + 1; j < indio_dev->num_channels; j++) |
| 1458 | if (channels[i].scan_index == channels[j].scan_index) { |
| 1459 | dev_err(&indio_dev->dev, |
| 1460 | "Duplicate scan index %d\n", |
| 1461 | channels[i].scan_index); |
| 1462 | return -EINVAL; |
| 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | return 0; |
| 1467 | } |
| 1468 | |
Michael Hennerich | 0f1acee | 2012-03-01 10:51:04 +0100 | [diff] [blame] | 1469 | static const struct iio_buffer_setup_ops noop_ring_setup_ops; |
| 1470 | |
Sachin Kamat | a7e57dc | 2013-10-29 11:39:00 +0000 | [diff] [blame] | 1471 | /** |
| 1472 | * iio_device_register() - register a device with the IIO subsystem |
| 1473 | * @indio_dev: Device structure filled by the device driver |
| 1474 | **/ |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1475 | int iio_device_register(struct iio_dev *indio_dev) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1476 | { |
| 1477 | int ret; |
| 1478 | |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 1479 | /* If the calling driver did not initialize of_node, do it here */ |
| 1480 | if (!indio_dev->dev.of_node && indio_dev->dev.parent) |
| 1481 | indio_dev->dev.of_node = indio_dev->dev.parent->of_node; |
| 1482 | |
Vlad Dogaru | 8f5d872 | 2014-12-29 11:37:48 +0200 | [diff] [blame] | 1483 | ret = iio_check_unique_scan_index(indio_dev); |
| 1484 | if (ret < 0) |
| 1485 | return ret; |
| 1486 | |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1487 | /* configure elements for the chrdev */ |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1488 | indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1489 | |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 1490 | ret = iio_device_register_debugfs(indio_dev); |
| 1491 | if (ret) { |
| 1492 | dev_err(indio_dev->dev.parent, |
| 1493 | "Failed to register debugfs interfaces\n"); |
Hartmut Knaack | 92825ff | 2014-02-16 11:53:00 +0000 | [diff] [blame] | 1494 | return ret; |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 1495 | } |
Lars-Peter Clausen | 3e1b6c9 | 2014-11-26 18:55:12 +0100 | [diff] [blame] | 1496 | |
| 1497 | ret = iio_buffer_alloc_sysfs_and_mask(indio_dev); |
| 1498 | if (ret) { |
| 1499 | dev_err(indio_dev->dev.parent, |
| 1500 | "Failed to create buffer sysfs interfaces\n"); |
| 1501 | goto error_unreg_debugfs; |
| 1502 | } |
| 1503 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1504 | ret = iio_device_register_sysfs(indio_dev); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1505 | if (ret) { |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1506 | dev_err(indio_dev->dev.parent, |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1507 | "Failed to register sysfs interfaces\n"); |
Lars-Peter Clausen | 3e1b6c9 | 2014-11-26 18:55:12 +0100 | [diff] [blame] | 1508 | goto error_buffer_free_sysfs; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1509 | } |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1510 | ret = iio_device_register_eventset(indio_dev); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1511 | if (ret) { |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1512 | dev_err(indio_dev->dev.parent, |
Roel Van Nyen | c849d25 | 2010-04-29 19:27:31 +0200 | [diff] [blame] | 1513 | "Failed to register event set\n"); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1514 | goto error_free_sysfs; |
| 1515 | } |
Vladimir Barinov | 735ad07 | 2015-08-20 22:37:39 +0300 | [diff] [blame] | 1516 | if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED)) |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1517 | iio_device_register_trigger_consumer(indio_dev); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1518 | |
Michael Hennerich | 0f1acee | 2012-03-01 10:51:04 +0100 | [diff] [blame] | 1519 | if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) && |
| 1520 | indio_dev->setup_ops == NULL) |
| 1521 | indio_dev->setup_ops = &noop_ring_setup_ops; |
| 1522 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1523 | cdev_init(&indio_dev->chrdev, &iio_buffer_fileops); |
| 1524 | indio_dev->chrdev.owner = indio_dev->info->driver_module; |
Lars-Peter Clausen | 0d5b7da | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1525 | indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj; |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1526 | ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1); |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1527 | if (ret < 0) |
Lars-Peter Clausen | 0d5b7da | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1528 | goto error_unreg_eventset; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1529 | |
Lars-Peter Clausen | 0d5b7da | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1530 | ret = device_add(&indio_dev->dev); |
| 1531 | if (ret < 0) |
| 1532 | goto error_cdev_del; |
| 1533 | |
| 1534 | return 0; |
| 1535 | error_cdev_del: |
| 1536 | cdev_del(&indio_dev->chrdev); |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1537 | error_unreg_eventset: |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1538 | iio_device_unregister_eventset(indio_dev); |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1539 | error_free_sysfs: |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1540 | iio_device_unregister_sysfs(indio_dev); |
Lars-Peter Clausen | 3e1b6c9 | 2014-11-26 18:55:12 +0100 | [diff] [blame] | 1541 | error_buffer_free_sysfs: |
| 1542 | iio_buffer_free_sysfs_and_mask(indio_dev); |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 1543 | error_unreg_debugfs: |
| 1544 | iio_device_unregister_debugfs(indio_dev); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1545 | return ret; |
| 1546 | } |
| 1547 | EXPORT_SYMBOL(iio_device_register); |
| 1548 | |
Sachin Kamat | a7e57dc | 2013-10-29 11:39:00 +0000 | [diff] [blame] | 1549 | /** |
| 1550 | * iio_device_unregister() - unregister a device from the IIO subsystem |
| 1551 | * @indio_dev: Device structure representing the device. |
| 1552 | **/ |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1553 | void iio_device_unregister(struct iio_dev *indio_dev) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1554 | { |
Jonathan Cameron | ac917a8 | 2012-02-15 19:48:00 +0000 | [diff] [blame] | 1555 | mutex_lock(&indio_dev->info_exist_lock); |
Lars-Peter Clausen | a87c82e | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1556 | |
| 1557 | device_del(&indio_dev->dev); |
| 1558 | |
Lars-Peter Clausen | 0d5b7da | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1559 | if (indio_dev->chrdev.dev) |
| 1560 | cdev_del(&indio_dev->chrdev); |
Lars-Peter Clausen | bc4c961 | 2013-09-21 16:21:00 +0100 | [diff] [blame] | 1561 | iio_device_unregister_debugfs(indio_dev); |
Lars-Peter Clausen | 0d5b7da | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1562 | |
Lars-Peter Clausen | a87c82e | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1563 | iio_disable_all_buffers(indio_dev); |
| 1564 | |
Jonathan Cameron | ac917a8 | 2012-02-15 19:48:00 +0000 | [diff] [blame] | 1565 | indio_dev->info = NULL; |
Lars-Peter Clausen | d2f0a48 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 1566 | |
| 1567 | iio_device_wakeup_eventset(indio_dev); |
| 1568 | iio_buffer_wakeup_poll(indio_dev); |
| 1569 | |
Jonathan Cameron | ac917a8 | 2012-02-15 19:48:00 +0000 | [diff] [blame] | 1570 | mutex_unlock(&indio_dev->info_exist_lock); |
Lars-Peter Clausen | 3e1b6c9 | 2014-11-26 18:55:12 +0100 | [diff] [blame] | 1571 | |
| 1572 | iio_buffer_free_sysfs_and_mask(indio_dev); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1573 | } |
| 1574 | EXPORT_SYMBOL(iio_device_unregister); |
Sachin Kamat | 8caa07c | 2013-10-29 11:39:00 +0000 | [diff] [blame] | 1575 | |
| 1576 | static void devm_iio_device_unreg(struct device *dev, void *res) |
| 1577 | { |
| 1578 | iio_device_unregister(*(struct iio_dev **)res); |
| 1579 | } |
| 1580 | |
| 1581 | /** |
| 1582 | * devm_iio_device_register - Resource-managed iio_device_register() |
| 1583 | * @dev: Device to allocate iio_dev for |
| 1584 | * @indio_dev: Device structure filled by the device driver |
| 1585 | * |
| 1586 | * Managed iio_device_register. The IIO device registered with this |
| 1587 | * function is automatically unregistered on driver detach. This function |
| 1588 | * calls iio_device_register() internally. Refer to that function for more |
| 1589 | * information. |
| 1590 | * |
| 1591 | * If an iio_dev registered with this function needs to be unregistered |
| 1592 | * separately, devm_iio_device_unregister() must be used. |
| 1593 | * |
| 1594 | * RETURNS: |
| 1595 | * 0 on success, negative error number on failure. |
| 1596 | */ |
| 1597 | int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev) |
| 1598 | { |
| 1599 | struct iio_dev **ptr; |
| 1600 | int ret; |
| 1601 | |
| 1602 | ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL); |
| 1603 | if (!ptr) |
| 1604 | return -ENOMEM; |
| 1605 | |
| 1606 | *ptr = indio_dev; |
| 1607 | ret = iio_device_register(indio_dev); |
| 1608 | if (!ret) |
| 1609 | devres_add(dev, ptr); |
| 1610 | else |
| 1611 | devres_free(ptr); |
| 1612 | |
| 1613 | return ret; |
| 1614 | } |
| 1615 | EXPORT_SYMBOL_GPL(devm_iio_device_register); |
| 1616 | |
| 1617 | /** |
| 1618 | * devm_iio_device_unregister - Resource-managed iio_device_unregister() |
| 1619 | * @dev: Device this iio_dev belongs to |
| 1620 | * @indio_dev: the iio_dev associated with the device |
| 1621 | * |
| 1622 | * Unregister iio_dev registered with devm_iio_device_register(). |
| 1623 | */ |
| 1624 | void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev) |
| 1625 | { |
| 1626 | int rc; |
| 1627 | |
| 1628 | rc = devres_release(dev, devm_iio_device_unreg, |
| 1629 | devm_iio_device_match, indio_dev); |
| 1630 | WARN_ON(rc); |
| 1631 | } |
| 1632 | EXPORT_SYMBOL_GPL(devm_iio_device_unregister); |
| 1633 | |
Alison Schofield | 08a3380 | 2016-03-09 11:30:12 -0800 | [diff] [blame] | 1634 | /** |
| 1635 | * iio_device_claim_direct_mode - Keep device in direct mode |
| 1636 | * @indio_dev: the iio_dev associated with the device |
| 1637 | * |
| 1638 | * If the device is in direct mode it is guaranteed to stay |
| 1639 | * that way until iio_device_release_direct_mode() is called. |
| 1640 | * |
| 1641 | * Use with iio_device_release_direct_mode() |
| 1642 | * |
| 1643 | * Returns: 0 on success, -EBUSY on failure |
| 1644 | */ |
| 1645 | int iio_device_claim_direct_mode(struct iio_dev *indio_dev) |
| 1646 | { |
| 1647 | mutex_lock(&indio_dev->mlock); |
| 1648 | |
| 1649 | if (iio_buffer_enabled(indio_dev)) { |
| 1650 | mutex_unlock(&indio_dev->mlock); |
| 1651 | return -EBUSY; |
| 1652 | } |
| 1653 | return 0; |
| 1654 | } |
| 1655 | EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode); |
| 1656 | |
| 1657 | /** |
| 1658 | * iio_device_release_direct_mode - releases claim on direct mode |
| 1659 | * @indio_dev: the iio_dev associated with the device |
| 1660 | * |
| 1661 | * Release the claim. Device is no longer guaranteed to stay |
| 1662 | * in direct mode. |
| 1663 | * |
| 1664 | * Use with iio_device_claim_direct_mode() |
| 1665 | */ |
| 1666 | void iio_device_release_direct_mode(struct iio_dev *indio_dev) |
| 1667 | { |
| 1668 | mutex_unlock(&indio_dev->mlock); |
| 1669 | } |
| 1670 | EXPORT_SYMBOL_GPL(iio_device_release_direct_mode); |
| 1671 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1672 | subsys_initcall(iio_init); |
| 1673 | module_exit(iio_exit); |
| 1674 | |
Jonathan Cameron | c8b9595 | 2012-09-02 21:27:56 +0100 | [diff] [blame] | 1675 | MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1676 | MODULE_DESCRIPTION("Industrial I/O core"); |
| 1677 | MODULE_LICENSE("GPL"); |