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