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