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