1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2a980e046SJonathan Cameron /* The industrial I/O core 3a980e046SJonathan Cameron * 4a980e046SJonathan Cameron * Copyright (c) 2008 Jonathan Cameron 5a980e046SJonathan Cameron * 6a980e046SJonathan Cameron * Based on elements of hwmon and input subsystems. 7a980e046SJonathan Cameron */ 8a980e046SJonathan Cameron 93176dd5dSSachin Kamat #define pr_fmt(fmt) "iio-core: " fmt 103176dd5dSSachin Kamat 11a980e046SJonathan Cameron #include <linux/kernel.h> 12a980e046SJonathan Cameron #include <linux/module.h> 13a980e046SJonathan Cameron #include <linux/idr.h> 14a980e046SJonathan Cameron #include <linux/kdev_t.h> 15a980e046SJonathan Cameron #include <linux/err.h> 16a980e046SJonathan Cameron #include <linux/device.h> 17a980e046SJonathan Cameron #include <linux/fs.h> 18a980e046SJonathan Cameron #include <linux/poll.h> 19fb158971SAndy Shevchenko #include <linux/property.h> 20a980e046SJonathan Cameron #include <linux/sched.h> 21a980e046SJonathan Cameron #include <linux/wait.h> 22a980e046SJonathan Cameron #include <linux/cdev.h> 23a980e046SJonathan Cameron #include <linux/slab.h> 24a980e046SJonathan Cameron #include <linux/anon_inodes.h> 25a980e046SJonathan Cameron #include <linux/debugfs.h> 2608a33805SAlison Schofield #include <linux/mutex.h> 27a980e046SJonathan Cameron #include <linux/iio/iio.h> 286d4ebd56SAlexandru Ardelean #include <linux/iio/iio-opaque.h> 29a980e046SJonathan Cameron #include "iio_core.h" 30a980e046SJonathan Cameron #include "iio_core_trigger.h" 31a980e046SJonathan Cameron #include <linux/iio/sysfs.h> 32a980e046SJonathan Cameron #include <linux/iio/events.h> 339e69c935SLars-Peter Clausen #include <linux/iio/buffer.h> 3433dd94cbSJonathan Cameron #include <linux/iio/buffer_impl.h> 35a980e046SJonathan Cameron 36a980e046SJonathan Cameron /* IDA to assign each registered device a unique id */ 37a980e046SJonathan Cameron static DEFINE_IDA(iio_ida); 38a980e046SJonathan Cameron 39a980e046SJonathan Cameron static dev_t iio_devt; 40a980e046SJonathan Cameron 41a980e046SJonathan Cameron #define IIO_DEV_MAX 256 42a980e046SJonathan Cameron struct bus_type iio_bus_type = { 43a980e046SJonathan Cameron .name = "iio", 44a980e046SJonathan Cameron }; 45a980e046SJonathan Cameron EXPORT_SYMBOL(iio_bus_type); 46a980e046SJonathan Cameron 47a980e046SJonathan Cameron static struct dentry *iio_debugfs_dentry; 48a980e046SJonathan Cameron 49a980e046SJonathan Cameron static const char * const iio_direction[] = { 50a980e046SJonathan Cameron [0] = "in", 51a980e046SJonathan Cameron [1] = "out", 52a980e046SJonathan Cameron }; 53a980e046SJonathan Cameron 54a980e046SJonathan Cameron static const char * const iio_chan_type_name_spec[] = { 55a980e046SJonathan Cameron [IIO_VOLTAGE] = "voltage", 56a980e046SJonathan Cameron [IIO_CURRENT] = "current", 57a980e046SJonathan Cameron [IIO_POWER] = "power", 58a980e046SJonathan Cameron [IIO_ACCEL] = "accel", 59a980e046SJonathan Cameron [IIO_ANGL_VEL] = "anglvel", 60a980e046SJonathan Cameron [IIO_MAGN] = "magn", 61a980e046SJonathan Cameron [IIO_LIGHT] = "illuminance", 62a980e046SJonathan Cameron [IIO_INTENSITY] = "intensity", 63a980e046SJonathan Cameron [IIO_PROXIMITY] = "proximity", 64a980e046SJonathan Cameron [IIO_TEMP] = "temp", 65a980e046SJonathan Cameron [IIO_INCLI] = "incli", 66a980e046SJonathan Cameron [IIO_ROT] = "rot", 67a980e046SJonathan Cameron [IIO_ANGL] = "angl", 68a980e046SJonathan Cameron [IIO_TIMESTAMP] = "timestamp", 69a980e046SJonathan Cameron [IIO_CAPACITANCE] = "capacitance", 70a6b12855SMichael Hennerich [IIO_ALTVOLTAGE] = "altvoltage", 7121cd1fabSJon Brenner [IIO_CCT] = "cct", 72c4f0c693SLars-Peter Clausen [IIO_PRESSURE] = "pressure", 73ac216aa2SHarald Geyer [IIO_HUMIDITYRELATIVE] = "humidityrelative", 7455aebeb9SDaniel Baluta [IIO_ACTIVITY] = "activity", 75a88bfe78SIrina Tirdea [IIO_STEPS] = "steps", 7672c66644SIrina Tirdea [IIO_ENERGY] = "energy", 77cc3c9eecSIrina Tirdea [IIO_DISTANCE] = "distance", 785a1a9329SIrina Tirdea [IIO_VELOCITY] = "velocity", 798ff6b3bcSMatt Ranostay [IIO_CONCENTRATION] = "concentration", 80d38d5469SMatt Ranostay [IIO_RESISTANCE] = "resistance", 81ecb3a7ccSMatt Ranostay [IIO_PH] = "ph", 82d409404cSPeter Meerwald-Stadler [IIO_UVINDEX] = "uvindex", 834b9d2090SMatt Ranostay [IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity", 841a8f324aSWilliam Breathitt Gray [IIO_COUNT] = "count", 851a8f324aSWilliam Breathitt Gray [IIO_INDEX] = "index", 86571299d0SSong Hongyan [IIO_GRAVITY] = "gravity", 873055a6cfSEugen Hristev [IIO_POSITIONRELATIVE] = "positionrelative", 88c73314e6SMathieu Othacehe [IIO_PHASE] = "phase", 8917abc9ecSTomasz Duszynski [IIO_MASSCONCENTRATION] = "massconcentration", 90a980e046SJonathan Cameron }; 91a980e046SJonathan Cameron 92a980e046SJonathan Cameron static const char * const iio_modifier_names[] = { 93a980e046SJonathan Cameron [IIO_MOD_X] = "x", 94a980e046SJonathan Cameron [IIO_MOD_Y] = "y", 95a980e046SJonathan Cameron [IIO_MOD_Z] = "z", 964b8d8015SPeter Meerwald [IIO_MOD_X_AND_Y] = "x&y", 974b8d8015SPeter Meerwald [IIO_MOD_X_AND_Z] = "x&z", 984b8d8015SPeter Meerwald [IIO_MOD_Y_AND_Z] = "y&z", 994b8d8015SPeter Meerwald [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z", 1004b8d8015SPeter Meerwald [IIO_MOD_X_OR_Y] = "x|y", 1014b8d8015SPeter Meerwald [IIO_MOD_X_OR_Z] = "x|z", 1024b8d8015SPeter Meerwald [IIO_MOD_Y_OR_Z] = "y|z", 1034b8d8015SPeter Meerwald [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z", 1048f5879b2SJonathan Cameron [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)", 105cf82cb81SJonathan Cameron [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2", 106a980e046SJonathan Cameron [IIO_MOD_LIGHT_BOTH] = "both", 107a980e046SJonathan Cameron [IIO_MOD_LIGHT_IR] = "ir", 10821cd1fabSJon Brenner [IIO_MOD_LIGHT_CLEAR] = "clear", 10921cd1fabSJon Brenner [IIO_MOD_LIGHT_RED] = "red", 11021cd1fabSJon Brenner [IIO_MOD_LIGHT_GREEN] = "green", 11121cd1fabSJon Brenner [IIO_MOD_LIGHT_BLUE] = "blue", 1122c5ff1f9SPeter Meerwald-Stadler [IIO_MOD_LIGHT_UV] = "uv", 113c0e4e0fdSMaxime Roussin-Bélanger [IIO_MOD_LIGHT_DUV] = "duv", 1145082f405SSrinivas Pandruvada [IIO_MOD_QUATERNION] = "quaternion", 115638b43b3SPeter Meerwald [IIO_MOD_TEMP_AMBIENT] = "ambient", 116638b43b3SPeter Meerwald [IIO_MOD_TEMP_OBJECT] = "object", 11711b8ddabSReyad Attiyat [IIO_MOD_NORTH_MAGN] = "from_north_magnetic", 11811b8ddabSReyad Attiyat [IIO_MOD_NORTH_TRUE] = "from_north_true", 11911b8ddabSReyad Attiyat [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp", 12011b8ddabSReyad Attiyat [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp", 12155aebeb9SDaniel Baluta [IIO_MOD_RUNNING] = "running", 12255aebeb9SDaniel Baluta [IIO_MOD_JOGGING] = "jogging", 12355aebeb9SDaniel Baluta [IIO_MOD_WALKING] = "walking", 12455aebeb9SDaniel Baluta [IIO_MOD_STILL] = "still", 1255a1a9329SIrina Tirdea [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)", 1261ce87f21SLars-Peter Clausen [IIO_MOD_I] = "i", 1271ce87f21SLars-Peter Clausen [IIO_MOD_Q] = "q", 1288ff6b3bcSMatt Ranostay [IIO_MOD_CO2] = "co2", 1298ff6b3bcSMatt Ranostay [IIO_MOD_VOC] = "voc", 13017abc9ecSTomasz Duszynski [IIO_MOD_PM1] = "pm1", 13117abc9ecSTomasz Duszynski [IIO_MOD_PM2P5] = "pm2p5", 13217abc9ecSTomasz Duszynski [IIO_MOD_PM4] = "pm4", 13317abc9ecSTomasz Duszynski [IIO_MOD_PM10] = "pm10", 13425f02d32SMatt Ranostay [IIO_MOD_ETHANOL] = "ethanol", 13525f02d32SMatt Ranostay [IIO_MOD_H2] = "h2", 1364ffa22fdSMatt Ranostay [IIO_MOD_O2] = "o2", 137a980e046SJonathan Cameron }; 138a980e046SJonathan Cameron 139a980e046SJonathan Cameron /* relies on pairs of these shared then separate */ 140a980e046SJonathan Cameron static const char * const iio_chan_info_postfix[] = { 141a980e046SJonathan Cameron [IIO_CHAN_INFO_RAW] = "raw", 142a980e046SJonathan Cameron [IIO_CHAN_INFO_PROCESSED] = "input", 143a980e046SJonathan Cameron [IIO_CHAN_INFO_SCALE] = "scale", 144a980e046SJonathan Cameron [IIO_CHAN_INFO_OFFSET] = "offset", 145a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBSCALE] = "calibscale", 146a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBBIAS] = "calibbias", 147a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK] = "peak_raw", 148a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale", 149a980e046SJonathan Cameron [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw", 150a980e046SJonathan Cameron [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw", 151a980e046SJonathan Cameron [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY] 152a980e046SJonathan Cameron = "filter_low_pass_3db_frequency", 1533f7f642bSMartin Fuzzey [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY] 1543f7f642bSMartin Fuzzey = "filter_high_pass_3db_frequency", 155a980e046SJonathan Cameron [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency", 156a6b12855SMichael Hennerich [IIO_CHAN_INFO_FREQUENCY] = "frequency", 157a6b12855SMichael Hennerich [IIO_CHAN_INFO_PHASE] = "phase", 158b65d6212SMichael Hennerich [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain", 1597c9ab035Ssrinivas pandruvada [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis", 160899d90bdSPeter Meerwald [IIO_CHAN_INFO_INT_TIME] = "integration_time", 161a88bfe78SIrina Tirdea [IIO_CHAN_INFO_ENABLE] = "en", 162bcdf28fbSIrina Tirdea [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight", 163d37f6836SIrina Tirdea [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight", 1642f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count", 1652f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time", 166c8a85854SVianney le Clément de Saint-Marcq [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity", 167faaa4495SIrina Tirdea [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio", 168ddfb97d8SAndrea Merello [IIO_CHAN_INFO_THERMOCOUPLE_TYPE] = "thermocouple_type", 169dde8ceecSCrt Mori [IIO_CHAN_INFO_CALIBAMBIENT] = "calibambient", 170a980e046SJonathan Cameron }; 171a980e046SJonathan Cameron 17296fb1b67SAlexandru Ardelean #if defined(CONFIG_DEBUG_FS) 173c9561fd2SJonathan Cameron /* 17496fb1b67SAlexandru Ardelean * There's also a CONFIG_DEBUG_FS guard in include/linux/iio/iio.h for 17596fb1b67SAlexandru Ardelean * iio_get_debugfs_dentry() to make it inline if CONFIG_DEBUG_FS is undefined 17696fb1b67SAlexandru Ardelean */ 17796fb1b67SAlexandru Ardelean struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev) 17896fb1b67SAlexandru Ardelean { 17996fb1b67SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 18096fb1b67SAlexandru Ardelean return iio_dev_opaque->debugfs_dentry; 18196fb1b67SAlexandru Ardelean } 18296fb1b67SAlexandru Ardelean EXPORT_SYMBOL_GPL(iio_get_debugfs_dentry); 18396fb1b67SAlexandru Ardelean #endif 18496fb1b67SAlexandru Ardelean 185a7e57dceSSachin Kamat /** 186a7e57dceSSachin Kamat * iio_find_channel_from_si() - get channel from its scan index 187a7e57dceSSachin Kamat * @indio_dev: device 188a7e57dceSSachin Kamat * @si: scan index to match 189a7e57dceSSachin Kamat */ 190a980e046SJonathan Cameron const struct iio_chan_spec 191a980e046SJonathan Cameron *iio_find_channel_from_si(struct iio_dev *indio_dev, int si) 192a980e046SJonathan Cameron { 193a980e046SJonathan Cameron int i; 194a980e046SJonathan Cameron 195a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) 196a980e046SJonathan Cameron if (indio_dev->channels[i].scan_index == si) 197a980e046SJonathan Cameron return &indio_dev->channels[i]; 198a980e046SJonathan Cameron return NULL; 199a980e046SJonathan Cameron } 200a980e046SJonathan Cameron 201a980e046SJonathan Cameron /* This turns up an awful lot */ 202a980e046SJonathan Cameron ssize_t iio_read_const_attr(struct device *dev, 203a980e046SJonathan Cameron struct device_attribute *attr, 204a980e046SJonathan Cameron char *buf) 205a980e046SJonathan Cameron { 206a980e046SJonathan Cameron return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string); 207a980e046SJonathan Cameron } 208a980e046SJonathan Cameron EXPORT_SYMBOL(iio_read_const_attr); 209a980e046SJonathan Cameron 21069f0793eSGwendal Grignou /** 21169f0793eSGwendal Grignou * iio_device_set_clock() - Set current timestamping clock for the device 21269f0793eSGwendal Grignou * @indio_dev: IIO device structure containing the device 21369f0793eSGwendal Grignou * @clock_id: timestamping clock posix identifier to set. 21469f0793eSGwendal Grignou */ 21569f0793eSGwendal Grignou int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id) 216bc2b7dabSGregor Boirie { 217bc2b7dabSGregor Boirie int ret; 218fa83c3baSAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 219fa83c3baSAlexandru Ardelean const struct iio_event_interface *ev_int = iio_dev_opaque->event_interface; 220bc2b7dabSGregor Boirie 221bc2b7dabSGregor Boirie ret = mutex_lock_interruptible(&indio_dev->mlock); 222bc2b7dabSGregor Boirie if (ret) 223bc2b7dabSGregor Boirie return ret; 224bc2b7dabSGregor Boirie if ((ev_int && iio_event_enabled(ev_int)) || 225bc2b7dabSGregor Boirie iio_buffer_enabled(indio_dev)) { 226bc2b7dabSGregor Boirie mutex_unlock(&indio_dev->mlock); 227bc2b7dabSGregor Boirie return -EBUSY; 228bc2b7dabSGregor Boirie } 229bc2b7dabSGregor Boirie indio_dev->clock_id = clock_id; 230bc2b7dabSGregor Boirie mutex_unlock(&indio_dev->mlock); 231bc2b7dabSGregor Boirie 232bc2b7dabSGregor Boirie return 0; 233bc2b7dabSGregor Boirie } 23469f0793eSGwendal Grignou EXPORT_SYMBOL(iio_device_set_clock); 235bc2b7dabSGregor Boirie 236bc2b7dabSGregor Boirie /** 237bc2b7dabSGregor Boirie * iio_get_time_ns() - utility function to get a time stamp for events etc 238bc2b7dabSGregor Boirie * @indio_dev: device 239bc2b7dabSGregor Boirie */ 240bc2b7dabSGregor Boirie s64 iio_get_time_ns(const struct iio_dev *indio_dev) 241bc2b7dabSGregor Boirie { 24245e7f5d2SArnd Bergmann struct timespec64 tp; 243bc2b7dabSGregor Boirie 244bc2b7dabSGregor Boirie switch (iio_device_get_clock(indio_dev)) { 245bc2b7dabSGregor Boirie case CLOCK_REALTIME: 24645e7f5d2SArnd Bergmann return ktime_get_real_ns(); 247bc2b7dabSGregor Boirie case CLOCK_MONOTONIC: 24845e7f5d2SArnd Bergmann return ktime_get_ns(); 249bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_RAW: 25045e7f5d2SArnd Bergmann return ktime_get_raw_ns(); 251bc2b7dabSGregor Boirie case CLOCK_REALTIME_COARSE: 25245e7f5d2SArnd Bergmann return ktime_to_ns(ktime_get_coarse_real()); 253bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_COARSE: 25445e7f5d2SArnd Bergmann ktime_get_coarse_ts64(&tp); 25545e7f5d2SArnd Bergmann return timespec64_to_ns(&tp); 256bc2b7dabSGregor Boirie case CLOCK_BOOTTIME: 2579285ec4cSJason A. Donenfeld return ktime_get_boottime_ns(); 258bc2b7dabSGregor Boirie case CLOCK_TAI: 2599285ec4cSJason A. Donenfeld return ktime_get_clocktai_ns(); 260bc2b7dabSGregor Boirie default: 261bc2b7dabSGregor Boirie BUG(); 262bc2b7dabSGregor Boirie } 263bc2b7dabSGregor Boirie } 264bc2b7dabSGregor Boirie EXPORT_SYMBOL(iio_get_time_ns); 265bc2b7dabSGregor Boirie 266bc2b7dabSGregor Boirie /** 267bc2b7dabSGregor Boirie * iio_get_time_res() - utility function to get time stamp clock resolution in 268bc2b7dabSGregor Boirie * nano seconds. 269bc2b7dabSGregor Boirie * @indio_dev: device 270bc2b7dabSGregor Boirie */ 271bc2b7dabSGregor Boirie unsigned int iio_get_time_res(const struct iio_dev *indio_dev) 272bc2b7dabSGregor Boirie { 273bc2b7dabSGregor Boirie switch (iio_device_get_clock(indio_dev)) { 274bc2b7dabSGregor Boirie case CLOCK_REALTIME: 275bc2b7dabSGregor Boirie case CLOCK_MONOTONIC: 276bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_RAW: 277bc2b7dabSGregor Boirie case CLOCK_BOOTTIME: 278bc2b7dabSGregor Boirie case CLOCK_TAI: 279bc2b7dabSGregor Boirie return hrtimer_resolution; 280bc2b7dabSGregor Boirie case CLOCK_REALTIME_COARSE: 281bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_COARSE: 282bc2b7dabSGregor Boirie return LOW_RES_NSEC; 283bc2b7dabSGregor Boirie default: 284bc2b7dabSGregor Boirie BUG(); 285bc2b7dabSGregor Boirie } 286bc2b7dabSGregor Boirie } 287bc2b7dabSGregor Boirie EXPORT_SYMBOL(iio_get_time_res); 288bc2b7dabSGregor Boirie 289a980e046SJonathan Cameron static int __init iio_init(void) 290a980e046SJonathan Cameron { 291a980e046SJonathan Cameron int ret; 292a980e046SJonathan Cameron 293a980e046SJonathan Cameron /* Register sysfs bus */ 294a980e046SJonathan Cameron ret = bus_register(&iio_bus_type); 295a980e046SJonathan Cameron if (ret < 0) { 2963176dd5dSSachin Kamat pr_err("could not register bus type\n"); 297a980e046SJonathan Cameron goto error_nothing; 298a980e046SJonathan Cameron } 299a980e046SJonathan Cameron 300a980e046SJonathan Cameron ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio"); 301a980e046SJonathan Cameron if (ret < 0) { 3023176dd5dSSachin Kamat pr_err("failed to allocate char dev region\n"); 303a980e046SJonathan Cameron goto error_unregister_bus_type; 304a980e046SJonathan Cameron } 305a980e046SJonathan Cameron 306a980e046SJonathan Cameron iio_debugfs_dentry = debugfs_create_dir("iio", NULL); 307a980e046SJonathan Cameron 308a980e046SJonathan Cameron return 0; 309a980e046SJonathan Cameron 310a980e046SJonathan Cameron error_unregister_bus_type: 311a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 312a980e046SJonathan Cameron error_nothing: 313a980e046SJonathan Cameron return ret; 314a980e046SJonathan Cameron } 315a980e046SJonathan Cameron 316a980e046SJonathan Cameron static void __exit iio_exit(void) 317a980e046SJonathan Cameron { 318a980e046SJonathan Cameron if (iio_devt) 319a980e046SJonathan Cameron unregister_chrdev_region(iio_devt, IIO_DEV_MAX); 320a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 321a980e046SJonathan Cameron debugfs_remove(iio_debugfs_dentry); 322a980e046SJonathan Cameron } 323a980e046SJonathan Cameron 324a980e046SJonathan Cameron #if defined(CONFIG_DEBUG_FS) 325a980e046SJonathan Cameron static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf, 326a980e046SJonathan Cameron size_t count, loff_t *ppos) 327a980e046SJonathan Cameron { 328a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 32996fb1b67SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 330a980e046SJonathan Cameron unsigned val = 0; 331a980e046SJonathan Cameron int ret; 332a980e046SJonathan Cameron 3332ddc982aSAlexandru Tachici if (*ppos > 0) 3342ddc982aSAlexandru Tachici return simple_read_from_buffer(userbuf, count, ppos, 33596fb1b67SAlexandru Ardelean iio_dev_opaque->read_buf, 33696fb1b67SAlexandru Ardelean iio_dev_opaque->read_buf_len); 3372ddc982aSAlexandru Tachici 338a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, 33996fb1b67SAlexandru Ardelean iio_dev_opaque->cached_reg_addr, 340a980e046SJonathan Cameron 0, &val); 3413d62c78aSMatt Fornero if (ret) { 342a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__); 3433d62c78aSMatt Fornero return ret; 3443d62c78aSMatt Fornero } 345a980e046SJonathan Cameron 34696fb1b67SAlexandru Ardelean iio_dev_opaque->read_buf_len = snprintf(iio_dev_opaque->read_buf, 34796fb1b67SAlexandru Ardelean sizeof(iio_dev_opaque->read_buf), 3482ddc982aSAlexandru Tachici "0x%X\n", val); 349a980e046SJonathan Cameron 3502ddc982aSAlexandru Tachici return simple_read_from_buffer(userbuf, count, ppos, 35196fb1b67SAlexandru Ardelean iio_dev_opaque->read_buf, 35296fb1b67SAlexandru Ardelean iio_dev_opaque->read_buf_len); 353a980e046SJonathan Cameron } 354a980e046SJonathan Cameron 355a980e046SJonathan Cameron static ssize_t iio_debugfs_write_reg(struct file *file, 356a980e046SJonathan Cameron const char __user *userbuf, size_t count, loff_t *ppos) 357a980e046SJonathan Cameron { 358a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 35996fb1b67SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 360a980e046SJonathan Cameron unsigned reg, val; 361a980e046SJonathan Cameron char buf[80]; 362a980e046SJonathan Cameron int ret; 363a980e046SJonathan Cameron 364a980e046SJonathan Cameron count = min_t(size_t, count, (sizeof(buf)-1)); 365a980e046SJonathan Cameron if (copy_from_user(buf, userbuf, count)) 366a980e046SJonathan Cameron return -EFAULT; 367a980e046SJonathan Cameron 368a980e046SJonathan Cameron buf[count] = 0; 369a980e046SJonathan Cameron 370a980e046SJonathan Cameron ret = sscanf(buf, "%i %i", ®, &val); 371a980e046SJonathan Cameron 372a980e046SJonathan Cameron switch (ret) { 373a980e046SJonathan Cameron case 1: 37496fb1b67SAlexandru Ardelean iio_dev_opaque->cached_reg_addr = reg; 375a980e046SJonathan Cameron break; 376a980e046SJonathan Cameron case 2: 37796fb1b67SAlexandru Ardelean iio_dev_opaque->cached_reg_addr = reg; 378a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, reg, 379a980e046SJonathan Cameron val, NULL); 380a980e046SJonathan Cameron if (ret) { 381a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: write failed\n", 382a980e046SJonathan Cameron __func__); 383a980e046SJonathan Cameron return ret; 384a980e046SJonathan Cameron } 385a980e046SJonathan Cameron break; 386a980e046SJonathan Cameron default: 387a980e046SJonathan Cameron return -EINVAL; 388a980e046SJonathan Cameron } 389a980e046SJonathan Cameron 390a980e046SJonathan Cameron return count; 391a980e046SJonathan Cameron } 392a980e046SJonathan Cameron 393a980e046SJonathan Cameron static const struct file_operations iio_debugfs_reg_fops = { 3945a28c873SAxel Lin .open = simple_open, 395a980e046SJonathan Cameron .read = iio_debugfs_read_reg, 396a980e046SJonathan Cameron .write = iio_debugfs_write_reg, 397a980e046SJonathan Cameron }; 398a980e046SJonathan Cameron 399a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 400a980e046SJonathan Cameron { 40196fb1b67SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 40296fb1b67SAlexandru Ardelean debugfs_remove_recursive(iio_dev_opaque->debugfs_dentry); 403a980e046SJonathan Cameron } 404a980e046SJonathan Cameron 4058915aacaSGreg Kroah-Hartman static void iio_device_register_debugfs(struct iio_dev *indio_dev) 406a980e046SJonathan Cameron { 40796fb1b67SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque; 40896fb1b67SAlexandru Ardelean 409a980e046SJonathan Cameron if (indio_dev->info->debugfs_reg_access == NULL) 4108915aacaSGreg Kroah-Hartman return; 411a980e046SJonathan Cameron 412abd5a2fbSAxel Lin if (!iio_debugfs_dentry) 4138915aacaSGreg Kroah-Hartman return; 414a980e046SJonathan Cameron 41596fb1b67SAlexandru Ardelean iio_dev_opaque = to_iio_dev_opaque(indio_dev); 41696fb1b67SAlexandru Ardelean 41796fb1b67SAlexandru Ardelean iio_dev_opaque->debugfs_dentry = 418a980e046SJonathan Cameron debugfs_create_dir(dev_name(&indio_dev->dev), 419a980e046SJonathan Cameron iio_debugfs_dentry); 420a980e046SJonathan Cameron 4218915aacaSGreg Kroah-Hartman debugfs_create_file("direct_reg_access", 0644, 42296fb1b67SAlexandru Ardelean iio_dev_opaque->debugfs_dentry, indio_dev, 4238915aacaSGreg Kroah-Hartman &iio_debugfs_reg_fops); 424a980e046SJonathan Cameron } 425a980e046SJonathan Cameron #else 4268915aacaSGreg Kroah-Hartman static void iio_device_register_debugfs(struct iio_dev *indio_dev) 427a980e046SJonathan Cameron { 428a980e046SJonathan Cameron } 429a980e046SJonathan Cameron 430a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 431a980e046SJonathan Cameron { 432a980e046SJonathan Cameron } 433a980e046SJonathan Cameron #endif /* CONFIG_DEBUG_FS */ 434a980e046SJonathan Cameron 435a980e046SJonathan Cameron static ssize_t iio_read_channel_ext_info(struct device *dev, 436a980e046SJonathan Cameron struct device_attribute *attr, 437a980e046SJonathan Cameron char *buf) 438a980e046SJonathan Cameron { 439e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 440a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 441a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 442a980e046SJonathan Cameron 443a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 444a980e046SJonathan Cameron 445fc6d1139SMichael Hennerich return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf); 446a980e046SJonathan Cameron } 447a980e046SJonathan Cameron 448a980e046SJonathan Cameron static ssize_t iio_write_channel_ext_info(struct device *dev, 449a980e046SJonathan Cameron struct device_attribute *attr, 450a980e046SJonathan Cameron const char *buf, 451a980e046SJonathan Cameron size_t len) 452a980e046SJonathan Cameron { 453e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 454a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 455a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 456a980e046SJonathan Cameron 457a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 458a980e046SJonathan Cameron 459fc6d1139SMichael Hennerich return ext_info->write(indio_dev, ext_info->private, 460fc6d1139SMichael Hennerich this_attr->c, buf, len); 461a980e046SJonathan Cameron } 462a980e046SJonathan Cameron 4635212cc8aSLars-Peter Clausen ssize_t iio_enum_available_read(struct iio_dev *indio_dev, 4645212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 4655212cc8aSLars-Peter Clausen { 4665212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 4675212cc8aSLars-Peter Clausen unsigned int i; 4685212cc8aSLars-Peter Clausen size_t len = 0; 4695212cc8aSLars-Peter Clausen 4705212cc8aSLars-Peter Clausen if (!e->num_items) 4715212cc8aSLars-Peter Clausen return 0; 4725212cc8aSLars-Peter Clausen 4735212cc8aSLars-Peter Clausen for (i = 0; i < e->num_items; ++i) 47474dcd439SLars-Peter Clausen len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]); 4755212cc8aSLars-Peter Clausen 4765212cc8aSLars-Peter Clausen /* replace last space with a newline */ 4775212cc8aSLars-Peter Clausen buf[len - 1] = '\n'; 4785212cc8aSLars-Peter Clausen 4795212cc8aSLars-Peter Clausen return len; 4805212cc8aSLars-Peter Clausen } 4815212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_available_read); 4825212cc8aSLars-Peter Clausen 4835212cc8aSLars-Peter Clausen ssize_t iio_enum_read(struct iio_dev *indio_dev, 4845212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 4855212cc8aSLars-Peter Clausen { 4865212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 4875212cc8aSLars-Peter Clausen int i; 4885212cc8aSLars-Peter Clausen 4895212cc8aSLars-Peter Clausen if (!e->get) 4905212cc8aSLars-Peter Clausen return -EINVAL; 4915212cc8aSLars-Peter Clausen 4925212cc8aSLars-Peter Clausen i = e->get(indio_dev, chan); 4935212cc8aSLars-Peter Clausen if (i < 0) 4945212cc8aSLars-Peter Clausen return i; 4955212cc8aSLars-Peter Clausen else if (i >= e->num_items) 4965212cc8aSLars-Peter Clausen return -EINVAL; 4975212cc8aSLars-Peter Clausen 498598db581SKees Cook return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]); 4995212cc8aSLars-Peter Clausen } 5005212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_read); 5015212cc8aSLars-Peter Clausen 5025212cc8aSLars-Peter Clausen ssize_t iio_enum_write(struct iio_dev *indio_dev, 5035212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, const char *buf, 5045212cc8aSLars-Peter Clausen size_t len) 5055212cc8aSLars-Peter Clausen { 5065212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 5075212cc8aSLars-Peter Clausen int ret; 5085212cc8aSLars-Peter Clausen 5095212cc8aSLars-Peter Clausen if (!e->set) 5105212cc8aSLars-Peter Clausen return -EINVAL; 5115212cc8aSLars-Peter Clausen 51202e9a0ffSAndy Shevchenko ret = __sysfs_match_string(e->items, e->num_items, buf); 51302e9a0ffSAndy Shevchenko if (ret < 0) 51402e9a0ffSAndy Shevchenko return ret; 5155212cc8aSLars-Peter Clausen 51602e9a0ffSAndy Shevchenko ret = e->set(indio_dev, chan, ret); 5175212cc8aSLars-Peter Clausen return ret ? ret : len; 5185212cc8aSLars-Peter Clausen } 5195212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_write); 5205212cc8aSLars-Peter Clausen 521dfc57732SGregor Boirie static const struct iio_mount_matrix iio_mount_idmatrix = { 522dfc57732SGregor Boirie .rotation = { 523dfc57732SGregor Boirie "1", "0", "0", 524dfc57732SGregor Boirie "0", "1", "0", 525dfc57732SGregor Boirie "0", "0", "1" 526dfc57732SGregor Boirie } 527dfc57732SGregor Boirie }; 528dfc57732SGregor Boirie 529dfc57732SGregor Boirie static int iio_setup_mount_idmatrix(const struct device *dev, 530dfc57732SGregor Boirie struct iio_mount_matrix *matrix) 531dfc57732SGregor Boirie { 532dfc57732SGregor Boirie *matrix = iio_mount_idmatrix; 533dfc57732SGregor Boirie dev_info(dev, "mounting matrix not found: using identity...\n"); 534dfc57732SGregor Boirie return 0; 535dfc57732SGregor Boirie } 536dfc57732SGregor Boirie 537dfc57732SGregor Boirie ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv, 538dfc57732SGregor Boirie const struct iio_chan_spec *chan, char *buf) 539dfc57732SGregor Boirie { 540dfc57732SGregor Boirie const struct iio_mount_matrix *mtx = ((iio_get_mount_matrix_t *) 541dfc57732SGregor Boirie priv)(indio_dev, chan); 542dfc57732SGregor Boirie 543dfc57732SGregor Boirie if (IS_ERR(mtx)) 544dfc57732SGregor Boirie return PTR_ERR(mtx); 545dfc57732SGregor Boirie 546dfc57732SGregor Boirie if (!mtx) 547dfc57732SGregor Boirie mtx = &iio_mount_idmatrix; 548dfc57732SGregor Boirie 549dfc57732SGregor Boirie return snprintf(buf, PAGE_SIZE, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n", 550dfc57732SGregor Boirie mtx->rotation[0], mtx->rotation[1], mtx->rotation[2], 551dfc57732SGregor Boirie mtx->rotation[3], mtx->rotation[4], mtx->rotation[5], 552dfc57732SGregor Boirie mtx->rotation[6], mtx->rotation[7], mtx->rotation[8]); 553dfc57732SGregor Boirie } 554dfc57732SGregor Boirie EXPORT_SYMBOL_GPL(iio_show_mount_matrix); 555dfc57732SGregor Boirie 556dfc57732SGregor Boirie /** 557fb158971SAndy Shevchenko * iio_read_mount_matrix() - retrieve iio device mounting matrix from 558fb158971SAndy Shevchenko * device "mount-matrix" property 559dfc57732SGregor Boirie * @dev: device the mounting matrix property is assigned to 560dfc57732SGregor Boirie * @propname: device specific mounting matrix property name 561dfc57732SGregor Boirie * @matrix: where to store retrieved matrix 562dfc57732SGregor Boirie * 563dfc57732SGregor Boirie * If device is assigned no mounting matrix property, a default 3x3 identity 564dfc57732SGregor Boirie * matrix will be filled in. 565dfc57732SGregor Boirie * 566dfc57732SGregor Boirie * Return: 0 if success, or a negative error code on failure. 567dfc57732SGregor Boirie */ 568fb158971SAndy Shevchenko int iio_read_mount_matrix(struct device *dev, const char *propname, 569dfc57732SGregor Boirie struct iio_mount_matrix *matrix) 570dfc57732SGregor Boirie { 571fb158971SAndy Shevchenko size_t len = ARRAY_SIZE(iio_mount_idmatrix.rotation); 572fb158971SAndy Shevchenko int err; 573dfc57732SGregor Boirie 574fb158971SAndy Shevchenko err = device_property_read_string_array(dev, propname, 575fb158971SAndy Shevchenko matrix->rotation, len); 576fb158971SAndy Shevchenko if (err == len) 577dfc57732SGregor Boirie return 0; 578dfc57732SGregor Boirie 579dfc57732SGregor Boirie if (err >= 0) 580dfc57732SGregor Boirie /* Invalid number of matrix entries. */ 581dfc57732SGregor Boirie return -EINVAL; 582dfc57732SGregor Boirie 583dfc57732SGregor Boirie if (err != -EINVAL) 584dfc57732SGregor Boirie /* Invalid matrix declaration format. */ 585dfc57732SGregor Boirie return err; 586dfc57732SGregor Boirie 587dfc57732SGregor Boirie /* Matrix was not declared at all: fallback to identity. */ 588dfc57732SGregor Boirie return iio_setup_mount_idmatrix(dev, matrix); 589dfc57732SGregor Boirie } 590fb158971SAndy Shevchenko EXPORT_SYMBOL(iio_read_mount_matrix); 591dfc57732SGregor Boirie 59251239600SJonathan Cameron static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type, 59351239600SJonathan Cameron int size, const int *vals) 59451239600SJonathan Cameron { 59551239600SJonathan Cameron unsigned long long tmp; 59651239600SJonathan Cameron int tmp0, tmp1; 597*0e7a3978SAnand Ashok Dumbre s64 tmp2; 59851239600SJonathan Cameron bool scale_db = false; 59951239600SJonathan Cameron 60051239600SJonathan Cameron switch (type) { 60151239600SJonathan Cameron case IIO_VAL_INT: 60235a4eeb0STakashi Iwai return scnprintf(buf, len, "%d", vals[0]); 60351239600SJonathan Cameron case IIO_VAL_INT_PLUS_MICRO_DB: 60451239600SJonathan Cameron scale_db = true; 605df561f66SGustavo A. R. Silva fallthrough; 60651239600SJonathan Cameron case IIO_VAL_INT_PLUS_MICRO: 60751239600SJonathan Cameron if (vals[1] < 0) 60835a4eeb0STakashi Iwai return scnprintf(buf, len, "-%d.%06u%s", abs(vals[0]), 60951239600SJonathan Cameron -vals[1], scale_db ? " dB" : ""); 61051239600SJonathan Cameron else 61135a4eeb0STakashi Iwai return scnprintf(buf, len, "%d.%06u%s", vals[0], vals[1], 61251239600SJonathan Cameron scale_db ? " dB" : ""); 61351239600SJonathan Cameron case IIO_VAL_INT_PLUS_NANO: 61451239600SJonathan Cameron if (vals[1] < 0) 61535a4eeb0STakashi Iwai return scnprintf(buf, len, "-%d.%09u", abs(vals[0]), 61651239600SJonathan Cameron -vals[1]); 61751239600SJonathan Cameron else 61835a4eeb0STakashi Iwai return scnprintf(buf, len, "%d.%09u", vals[0], vals[1]); 61951239600SJonathan Cameron case IIO_VAL_FRACTIONAL: 620*0e7a3978SAnand Ashok Dumbre tmp2 = div_s64((s64)vals[0] * 1000000000LL, vals[1]); 62151239600SJonathan Cameron tmp1 = vals[1]; 622*0e7a3978SAnand Ashok Dumbre tmp0 = (int)div_s64_rem(tmp2, 1000000000, &tmp1); 623*0e7a3978SAnand Ashok Dumbre if ((tmp2 < 0) && (tmp0 == 0)) 624*0e7a3978SAnand Ashok Dumbre return snprintf(buf, len, "-0.%09u", abs(tmp1)); 625*0e7a3978SAnand Ashok Dumbre else 626*0e7a3978SAnand Ashok Dumbre return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1)); 62751239600SJonathan Cameron case IIO_VAL_FRACTIONAL_LOG2: 6287fd6592dSNikolaus Schulz tmp = shift_right((s64)vals[0] * 1000000000LL, vals[1]); 6297fd6592dSNikolaus Schulz tmp0 = (int)div_s64_rem(tmp, 1000000000LL, &tmp1); 63035a4eeb0STakashi Iwai return scnprintf(buf, len, "%d.%09u", tmp0, abs(tmp1)); 63151239600SJonathan Cameron case IIO_VAL_INT_MULTIPLE: 63251239600SJonathan Cameron { 63351239600SJonathan Cameron int i; 63451239600SJonathan Cameron int l = 0; 63551239600SJonathan Cameron 63651239600SJonathan Cameron for (i = 0; i < size; ++i) { 63735a4eeb0STakashi Iwai l += scnprintf(&buf[l], len - l, "%d ", vals[i]); 63851239600SJonathan Cameron if (l >= len) 63951239600SJonathan Cameron break; 64051239600SJonathan Cameron } 64151239600SJonathan Cameron return l; 64251239600SJonathan Cameron } 6438cb34036SAndrea Merello case IIO_VAL_CHAR: 64435a4eeb0STakashi Iwai return scnprintf(buf, len, "%c", (char)vals[0]); 64551239600SJonathan Cameron default: 64651239600SJonathan Cameron return 0; 64751239600SJonathan Cameron } 64851239600SJonathan Cameron } 64951239600SJonathan Cameron 6503661f3f5SLars-Peter Clausen /** 6513661f3f5SLars-Peter Clausen * iio_format_value() - Formats a IIO value into its string representation 6522498dcf6SCristina Opriceana * @buf: The buffer to which the formatted value gets written 65351239600SJonathan Cameron * which is assumed to be big enough (i.e. PAGE_SIZE). 654c175cb7cSRandy Dunlap * @type: One of the IIO_VAL_* constants. This decides how the val 6552498dcf6SCristina Opriceana * and val2 parameters are formatted. 6562498dcf6SCristina Opriceana * @size: Number of IIO value entries contained in vals 6572498dcf6SCristina Opriceana * @vals: Pointer to the values, exact meaning depends on the 6582498dcf6SCristina Opriceana * type parameter. 6592498dcf6SCristina Opriceana * 6602498dcf6SCristina Opriceana * Return: 0 by default, a negative number on failure or the 6612498dcf6SCristina Opriceana * total number of characters written for a type that belongs 662c175cb7cSRandy Dunlap * to the IIO_VAL_* constant. 6633661f3f5SLars-Peter Clausen */ 6649fbfb4b3SSrinivas Pandruvada ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals) 665a980e046SJonathan Cameron { 66651239600SJonathan Cameron ssize_t len; 667a980e046SJonathan Cameron 66851239600SJonathan Cameron len = __iio_format_value(buf, PAGE_SIZE, type, size, vals); 66951239600SJonathan Cameron if (len >= PAGE_SIZE - 1) 67051239600SJonathan Cameron return -EFBIG; 6719fbfb4b3SSrinivas Pandruvada 67251239600SJonathan Cameron return len + sprintf(buf + len, "\n"); 67367eedba3SMichael Hennerich } 6747d2c2acaSAndrew F. Davis EXPORT_SYMBOL_GPL(iio_format_value); 675a980e046SJonathan Cameron 6763661f3f5SLars-Peter Clausen static ssize_t iio_read_channel_info(struct device *dev, 6773661f3f5SLars-Peter Clausen struct device_attribute *attr, 6783661f3f5SLars-Peter Clausen char *buf) 6793661f3f5SLars-Peter Clausen { 6803661f3f5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 6813661f3f5SLars-Peter Clausen struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 6829fbfb4b3SSrinivas Pandruvada int vals[INDIO_MAX_RAW_ELEMENTS]; 6839fbfb4b3SSrinivas Pandruvada int ret; 6849fbfb4b3SSrinivas Pandruvada int val_len = 2; 6859fbfb4b3SSrinivas Pandruvada 6869fbfb4b3SSrinivas Pandruvada if (indio_dev->info->read_raw_multi) 6879fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c, 6889fbfb4b3SSrinivas Pandruvada INDIO_MAX_RAW_ELEMENTS, 6899fbfb4b3SSrinivas Pandruvada vals, &val_len, 6909fbfb4b3SSrinivas Pandruvada this_attr->address); 6919fbfb4b3SSrinivas Pandruvada else 6929fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw(indio_dev, this_attr->c, 6939fbfb4b3SSrinivas Pandruvada &vals[0], &vals[1], this_attr->address); 6943661f3f5SLars-Peter Clausen 6953661f3f5SLars-Peter Clausen if (ret < 0) 6963661f3f5SLars-Peter Clausen return ret; 6973661f3f5SLars-Peter Clausen 6989fbfb4b3SSrinivas Pandruvada return iio_format_value(buf, ret, val_len, vals); 6993661f3f5SLars-Peter Clausen } 7003661f3f5SLars-Peter Clausen 70151239600SJonathan Cameron static ssize_t iio_format_avail_list(char *buf, const int *vals, 70251239600SJonathan Cameron int type, int length) 70351239600SJonathan Cameron { 70451239600SJonathan Cameron int i; 70551239600SJonathan Cameron ssize_t len = 0; 70651239600SJonathan Cameron 70751239600SJonathan Cameron switch (type) { 70851239600SJonathan Cameron case IIO_VAL_INT: 70951239600SJonathan Cameron for (i = 0; i < length; i++) { 71051239600SJonathan Cameron len += __iio_format_value(buf + len, PAGE_SIZE - len, 71151239600SJonathan Cameron type, 1, &vals[i]); 71251239600SJonathan Cameron if (len >= PAGE_SIZE) 71351239600SJonathan Cameron return -EFBIG; 71451239600SJonathan Cameron if (i < length - 1) 71535a4eeb0STakashi Iwai len += scnprintf(buf + len, PAGE_SIZE - len, 71651239600SJonathan Cameron " "); 71751239600SJonathan Cameron else 71835a4eeb0STakashi Iwai len += scnprintf(buf + len, PAGE_SIZE - len, 71951239600SJonathan Cameron "\n"); 72051239600SJonathan Cameron if (len >= PAGE_SIZE) 72151239600SJonathan Cameron return -EFBIG; 72251239600SJonathan Cameron } 72351239600SJonathan Cameron break; 72451239600SJonathan Cameron default: 72551239600SJonathan Cameron for (i = 0; i < length / 2; i++) { 72651239600SJonathan Cameron len += __iio_format_value(buf + len, PAGE_SIZE - len, 72751239600SJonathan Cameron type, 2, &vals[i * 2]); 72851239600SJonathan Cameron if (len >= PAGE_SIZE) 72951239600SJonathan Cameron return -EFBIG; 73051239600SJonathan Cameron if (i < length / 2 - 1) 73135a4eeb0STakashi Iwai len += scnprintf(buf + len, PAGE_SIZE - len, 73251239600SJonathan Cameron " "); 73351239600SJonathan Cameron else 73435a4eeb0STakashi Iwai len += scnprintf(buf + len, PAGE_SIZE - len, 73551239600SJonathan Cameron "\n"); 73651239600SJonathan Cameron if (len >= PAGE_SIZE) 73751239600SJonathan Cameron return -EFBIG; 73851239600SJonathan Cameron } 73951239600SJonathan Cameron } 74051239600SJonathan Cameron 74151239600SJonathan Cameron return len; 74251239600SJonathan Cameron } 74351239600SJonathan Cameron 74451239600SJonathan Cameron static ssize_t iio_format_avail_range(char *buf, const int *vals, int type) 74551239600SJonathan Cameron { 74651239600SJonathan Cameron int i; 74751239600SJonathan Cameron ssize_t len; 74851239600SJonathan Cameron 74951239600SJonathan Cameron len = snprintf(buf, PAGE_SIZE, "["); 75051239600SJonathan Cameron switch (type) { 75151239600SJonathan Cameron case IIO_VAL_INT: 75251239600SJonathan Cameron for (i = 0; i < 3; i++) { 75351239600SJonathan Cameron len += __iio_format_value(buf + len, PAGE_SIZE - len, 75451239600SJonathan Cameron type, 1, &vals[i]); 75551239600SJonathan Cameron if (len >= PAGE_SIZE) 75651239600SJonathan Cameron return -EFBIG; 75751239600SJonathan Cameron if (i < 2) 75835a4eeb0STakashi Iwai len += scnprintf(buf + len, PAGE_SIZE - len, 75951239600SJonathan Cameron " "); 76051239600SJonathan Cameron else 76135a4eeb0STakashi Iwai len += scnprintf(buf + len, PAGE_SIZE - len, 76251239600SJonathan Cameron "]\n"); 76351239600SJonathan Cameron if (len >= PAGE_SIZE) 76451239600SJonathan Cameron return -EFBIG; 76551239600SJonathan Cameron } 76651239600SJonathan Cameron break; 76751239600SJonathan Cameron default: 76851239600SJonathan Cameron for (i = 0; i < 3; i++) { 76951239600SJonathan Cameron len += __iio_format_value(buf + len, PAGE_SIZE - len, 77051239600SJonathan Cameron type, 2, &vals[i * 2]); 77151239600SJonathan Cameron if (len >= PAGE_SIZE) 77251239600SJonathan Cameron return -EFBIG; 77351239600SJonathan Cameron if (i < 2) 77435a4eeb0STakashi Iwai len += scnprintf(buf + len, PAGE_SIZE - len, 77551239600SJonathan Cameron " "); 77651239600SJonathan Cameron else 77735a4eeb0STakashi Iwai len += scnprintf(buf + len, PAGE_SIZE - len, 77851239600SJonathan Cameron "]\n"); 77951239600SJonathan Cameron if (len >= PAGE_SIZE) 78051239600SJonathan Cameron return -EFBIG; 78151239600SJonathan Cameron } 78251239600SJonathan Cameron } 78351239600SJonathan Cameron 78451239600SJonathan Cameron return len; 78551239600SJonathan Cameron } 78651239600SJonathan Cameron 78751239600SJonathan Cameron static ssize_t iio_read_channel_info_avail(struct device *dev, 78851239600SJonathan Cameron struct device_attribute *attr, 78951239600SJonathan Cameron char *buf) 79051239600SJonathan Cameron { 79151239600SJonathan Cameron struct iio_dev *indio_dev = dev_to_iio_dev(dev); 79251239600SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 79351239600SJonathan Cameron const int *vals; 79451239600SJonathan Cameron int ret; 79551239600SJonathan Cameron int length; 79651239600SJonathan Cameron int type; 79751239600SJonathan Cameron 79851239600SJonathan Cameron ret = indio_dev->info->read_avail(indio_dev, this_attr->c, 79951239600SJonathan Cameron &vals, &type, &length, 80051239600SJonathan Cameron this_attr->address); 80151239600SJonathan Cameron 80251239600SJonathan Cameron if (ret < 0) 80351239600SJonathan Cameron return ret; 80451239600SJonathan Cameron switch (ret) { 80551239600SJonathan Cameron case IIO_AVAIL_LIST: 80651239600SJonathan Cameron return iio_format_avail_list(buf, vals, type, length); 80751239600SJonathan Cameron case IIO_AVAIL_RANGE: 80851239600SJonathan Cameron return iio_format_avail_range(buf, vals, type); 80951239600SJonathan Cameron default: 81051239600SJonathan Cameron return -EINVAL; 81151239600SJonathan Cameron } 81251239600SJonathan Cameron } 81351239600SJonathan Cameron 8146807d721SLars-Peter Clausen /** 815b8528224SBeniamin Bia * __iio_str_to_fixpoint() - Parse a fixed-point number from a string 8166807d721SLars-Peter Clausen * @str: The string to parse 8176807d721SLars-Peter Clausen * @fract_mult: Multiplier for the first decimal place, should be a power of 10 8186807d721SLars-Peter Clausen * @integer: The integer part of the number 8196807d721SLars-Peter Clausen * @fract: The fractional part of the number 820b8528224SBeniamin Bia * @scale_db: True if this should parse as dB 8216807d721SLars-Peter Clausen * 8226807d721SLars-Peter Clausen * Returns 0 on success, or a negative error code if the string could not be 8236807d721SLars-Peter Clausen * parsed. 8246807d721SLars-Peter Clausen */ 825b8528224SBeniamin Bia static int __iio_str_to_fixpoint(const char *str, int fract_mult, 826b8528224SBeniamin Bia int *integer, int *fract, bool scale_db) 8276807d721SLars-Peter Clausen { 8286807d721SLars-Peter Clausen int i = 0, f = 0; 8296807d721SLars-Peter Clausen bool integer_part = true, negative = false; 8306807d721SLars-Peter Clausen 831f47dff32SSean Nyekjaer if (fract_mult == 0) { 832f47dff32SSean Nyekjaer *fract = 0; 833f47dff32SSean Nyekjaer 834f47dff32SSean Nyekjaer return kstrtoint(str, 0, integer); 835f47dff32SSean Nyekjaer } 836f47dff32SSean Nyekjaer 8376807d721SLars-Peter Clausen if (str[0] == '-') { 8386807d721SLars-Peter Clausen negative = true; 8396807d721SLars-Peter Clausen str++; 8406807d721SLars-Peter Clausen } else if (str[0] == '+') { 8416807d721SLars-Peter Clausen str++; 8426807d721SLars-Peter Clausen } 8436807d721SLars-Peter Clausen 8446807d721SLars-Peter Clausen while (*str) { 8456807d721SLars-Peter Clausen if ('0' <= *str && *str <= '9') { 8466807d721SLars-Peter Clausen if (integer_part) { 8476807d721SLars-Peter Clausen i = i * 10 + *str - '0'; 8486807d721SLars-Peter Clausen } else { 8496807d721SLars-Peter Clausen f += fract_mult * (*str - '0'); 8506807d721SLars-Peter Clausen fract_mult /= 10; 8516807d721SLars-Peter Clausen } 8526807d721SLars-Peter Clausen } else if (*str == '\n') { 8536807d721SLars-Peter Clausen if (*(str + 1) == '\0') 8546807d721SLars-Peter Clausen break; 8556807d721SLars-Peter Clausen else 8566807d721SLars-Peter Clausen return -EINVAL; 857b8528224SBeniamin Bia } else if (!strncmp(str, " dB", sizeof(" dB") - 1) && scale_db) { 858b8528224SBeniamin Bia /* Ignore the dB suffix */ 859b8528224SBeniamin Bia str += sizeof(" dB") - 1; 860b8528224SBeniamin Bia continue; 861b8528224SBeniamin Bia } else if (!strncmp(str, "dB", sizeof("dB") - 1) && scale_db) { 862b8528224SBeniamin Bia /* Ignore the dB suffix */ 863b8528224SBeniamin Bia str += sizeof("dB") - 1; 864b8528224SBeniamin Bia continue; 8656807d721SLars-Peter Clausen } else if (*str == '.' && integer_part) { 8666807d721SLars-Peter Clausen integer_part = false; 8676807d721SLars-Peter Clausen } else { 8686807d721SLars-Peter Clausen return -EINVAL; 8696807d721SLars-Peter Clausen } 8706807d721SLars-Peter Clausen str++; 8716807d721SLars-Peter Clausen } 8726807d721SLars-Peter Clausen 8736807d721SLars-Peter Clausen if (negative) { 8746807d721SLars-Peter Clausen if (i) 8756807d721SLars-Peter Clausen i = -i; 8766807d721SLars-Peter Clausen else 8776807d721SLars-Peter Clausen f = -f; 8786807d721SLars-Peter Clausen } 8796807d721SLars-Peter Clausen 8806807d721SLars-Peter Clausen *integer = i; 8816807d721SLars-Peter Clausen *fract = f; 8826807d721SLars-Peter Clausen 8836807d721SLars-Peter Clausen return 0; 8846807d721SLars-Peter Clausen } 885b8528224SBeniamin Bia 886b8528224SBeniamin Bia /** 887b8528224SBeniamin Bia * iio_str_to_fixpoint() - Parse a fixed-point number from a string 888b8528224SBeniamin Bia * @str: The string to parse 889b8528224SBeniamin Bia * @fract_mult: Multiplier for the first decimal place, should be a power of 10 890b8528224SBeniamin Bia * @integer: The integer part of the number 891b8528224SBeniamin Bia * @fract: The fractional part of the number 892b8528224SBeniamin Bia * 893b8528224SBeniamin Bia * Returns 0 on success, or a negative error code if the string could not be 894b8528224SBeniamin Bia * parsed. 895b8528224SBeniamin Bia */ 896b8528224SBeniamin Bia int iio_str_to_fixpoint(const char *str, int fract_mult, 897b8528224SBeniamin Bia int *integer, int *fract) 898b8528224SBeniamin Bia { 899b8528224SBeniamin Bia return __iio_str_to_fixpoint(str, fract_mult, integer, fract, false); 900b8528224SBeniamin Bia } 9016807d721SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_str_to_fixpoint); 9026807d721SLars-Peter Clausen 903a980e046SJonathan Cameron static ssize_t iio_write_channel_info(struct device *dev, 904a980e046SJonathan Cameron struct device_attribute *attr, 905a980e046SJonathan Cameron const char *buf, 906a980e046SJonathan Cameron size_t len) 907a980e046SJonathan Cameron { 908e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 909a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 9106807d721SLars-Peter Clausen int ret, fract_mult = 100000; 9118cb34036SAndrea Merello int integer, fract = 0; 9128cb34036SAndrea Merello bool is_char = false; 913b8528224SBeniamin Bia bool scale_db = false; 914a980e046SJonathan Cameron 915a980e046SJonathan Cameron /* Assumes decimal - precision based on number of digits */ 916a980e046SJonathan Cameron if (!indio_dev->info->write_raw) 917a980e046SJonathan Cameron return -EINVAL; 918a980e046SJonathan Cameron 919a980e046SJonathan Cameron if (indio_dev->info->write_raw_get_fmt) 920a980e046SJonathan Cameron switch (indio_dev->info->write_raw_get_fmt(indio_dev, 921a980e046SJonathan Cameron this_attr->c, this_attr->address)) { 922f47dff32SSean Nyekjaer case IIO_VAL_INT: 923f47dff32SSean Nyekjaer fract_mult = 0; 924f47dff32SSean Nyekjaer break; 925b8528224SBeniamin Bia case IIO_VAL_INT_PLUS_MICRO_DB: 926b8528224SBeniamin Bia scale_db = true; 927df561f66SGustavo A. R. Silva fallthrough; 928a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_MICRO: 929a980e046SJonathan Cameron fract_mult = 100000; 930a980e046SJonathan Cameron break; 931a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_NANO: 932a980e046SJonathan Cameron fract_mult = 100000000; 933a980e046SJonathan Cameron break; 9348cb34036SAndrea Merello case IIO_VAL_CHAR: 9358cb34036SAndrea Merello is_char = true; 9368cb34036SAndrea Merello break; 937a980e046SJonathan Cameron default: 938a980e046SJonathan Cameron return -EINVAL; 939a980e046SJonathan Cameron } 940a980e046SJonathan Cameron 9418cb34036SAndrea Merello if (is_char) { 9428cb34036SAndrea Merello char ch; 9438cb34036SAndrea Merello 9448cb34036SAndrea Merello if (sscanf(buf, "%c", &ch) != 1) 9458cb34036SAndrea Merello return -EINVAL; 9468cb34036SAndrea Merello integer = ch; 9478cb34036SAndrea Merello } else { 948b8528224SBeniamin Bia ret = __iio_str_to_fixpoint(buf, fract_mult, &integer, &fract, 949b8528224SBeniamin Bia scale_db); 950b8528224SBeniamin Bia if (ret) 951b8528224SBeniamin Bia return ret; 9525403f280SMircea Caprioru } 953a980e046SJonathan Cameron 954a980e046SJonathan Cameron ret = indio_dev->info->write_raw(indio_dev, this_attr->c, 955a980e046SJonathan Cameron integer, fract, this_attr->address); 956a980e046SJonathan Cameron if (ret) 957a980e046SJonathan Cameron return ret; 958a980e046SJonathan Cameron 959a980e046SJonathan Cameron return len; 960a980e046SJonathan Cameron } 961a980e046SJonathan Cameron 962a980e046SJonathan Cameron static 963a980e046SJonathan Cameron int __iio_device_attr_init(struct device_attribute *dev_attr, 964a980e046SJonathan Cameron const char *postfix, 965a980e046SJonathan Cameron struct iio_chan_spec const *chan, 966a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 967a980e046SJonathan Cameron struct device_attribute *attr, 968a980e046SJonathan Cameron char *buf), 969a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 970a980e046SJonathan Cameron struct device_attribute *attr, 971a980e046SJonathan Cameron const char *buf, 972a980e046SJonathan Cameron size_t len), 9733704432fSJonathan Cameron enum iio_shared_by shared_by) 974a980e046SJonathan Cameron { 9753704432fSJonathan Cameron int ret = 0; 9767bbcf7e1SLars-Peter Clausen char *name = NULL; 9773704432fSJonathan Cameron char *full_postfix; 978a980e046SJonathan Cameron sysfs_attr_init(&dev_attr->attr); 979a980e046SJonathan Cameron 980a980e046SJonathan Cameron /* Build up postfix of <extend_name>_<modifier>_postfix */ 9813704432fSJonathan Cameron if (chan->modified && (shared_by == IIO_SEPARATE)) { 982a980e046SJonathan Cameron if (chan->extend_name) 983a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", 984a980e046SJonathan Cameron iio_modifier_names[chan 985a980e046SJonathan Cameron ->channel2], 986a980e046SJonathan Cameron chan->extend_name, 987a980e046SJonathan Cameron postfix); 988a980e046SJonathan Cameron else 989a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s", 990a980e046SJonathan Cameron iio_modifier_names[chan 991a980e046SJonathan Cameron ->channel2], 992a980e046SJonathan Cameron postfix); 993a980e046SJonathan Cameron } else { 99477bfa8baSLars-Peter Clausen if (chan->extend_name == NULL || shared_by != IIO_SEPARATE) 995a980e046SJonathan Cameron full_postfix = kstrdup(postfix, GFP_KERNEL); 996a980e046SJonathan Cameron else 997a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, 998a980e046SJonathan Cameron "%s_%s", 999a980e046SJonathan Cameron chan->extend_name, 1000a980e046SJonathan Cameron postfix); 1001a980e046SJonathan Cameron } 10023704432fSJonathan Cameron if (full_postfix == NULL) 10033704432fSJonathan Cameron return -ENOMEM; 1004a980e046SJonathan Cameron 1005a980e046SJonathan Cameron if (chan->differential) { /* Differential can not have modifier */ 10063704432fSJonathan Cameron switch (shared_by) { 1007c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 10087bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 1009c006ec83SJonathan Cameron break; 1010c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 10117bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 1012c006ec83SJonathan Cameron iio_direction[chan->output], 1013c006ec83SJonathan Cameron full_postfix); 1014c006ec83SJonathan Cameron break; 10153704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 10167bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s", 1017a980e046SJonathan Cameron iio_direction[chan->output], 1018a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1019a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1020a980e046SJonathan Cameron full_postfix); 10213704432fSJonathan Cameron break; 10223704432fSJonathan Cameron case IIO_SEPARATE: 10233704432fSJonathan Cameron if (!chan->indexed) { 1024231bfe53SDan Carpenter WARN(1, "Differential channels must be indexed\n"); 10253704432fSJonathan Cameron ret = -EINVAL; 10263704432fSJonathan Cameron goto error_free_full_postfix; 10273704432fSJonathan Cameron } 10287bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, 10293704432fSJonathan Cameron "%s_%s%d-%s%d_%s", 1030a980e046SJonathan Cameron iio_direction[chan->output], 1031a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1032a980e046SJonathan Cameron chan->channel, 1033a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1034a980e046SJonathan Cameron chan->channel2, 1035a980e046SJonathan Cameron full_postfix); 10363704432fSJonathan Cameron break; 1037a980e046SJonathan Cameron } 1038a980e046SJonathan Cameron } else { /* Single ended */ 10393704432fSJonathan Cameron switch (shared_by) { 1040c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 10417bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 1042c006ec83SJonathan Cameron break; 1043c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 10447bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 1045c006ec83SJonathan Cameron iio_direction[chan->output], 1046c006ec83SJonathan Cameron full_postfix); 1047c006ec83SJonathan Cameron break; 10483704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 10497bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 1050a980e046SJonathan Cameron iio_direction[chan->output], 1051a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1052a980e046SJonathan Cameron full_postfix); 10533704432fSJonathan Cameron break; 10543704432fSJonathan Cameron 10553704432fSJonathan Cameron case IIO_SEPARATE: 10563704432fSJonathan Cameron if (chan->indexed) 10577bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s%d_%s", 1058a980e046SJonathan Cameron iio_direction[chan->output], 1059a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1060a980e046SJonathan Cameron chan->channel, 1061a980e046SJonathan Cameron full_postfix); 1062a980e046SJonathan Cameron else 10637bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 1064a980e046SJonathan Cameron iio_direction[chan->output], 1065a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1066a980e046SJonathan Cameron full_postfix); 10673704432fSJonathan Cameron break; 10683704432fSJonathan Cameron } 1069a980e046SJonathan Cameron } 10707bbcf7e1SLars-Peter Clausen if (name == NULL) { 1071a980e046SJonathan Cameron ret = -ENOMEM; 1072a980e046SJonathan Cameron goto error_free_full_postfix; 1073a980e046SJonathan Cameron } 10747bbcf7e1SLars-Peter Clausen dev_attr->attr.name = name; 1075a980e046SJonathan Cameron 1076a980e046SJonathan Cameron if (readfunc) { 1077a980e046SJonathan Cameron dev_attr->attr.mode |= S_IRUGO; 1078a980e046SJonathan Cameron dev_attr->show = readfunc; 1079a980e046SJonathan Cameron } 1080a980e046SJonathan Cameron 1081a980e046SJonathan Cameron if (writefunc) { 1082a980e046SJonathan Cameron dev_attr->attr.mode |= S_IWUSR; 1083a980e046SJonathan Cameron dev_attr->store = writefunc; 1084a980e046SJonathan Cameron } 10857bbcf7e1SLars-Peter Clausen 1086a980e046SJonathan Cameron error_free_full_postfix: 1087a980e046SJonathan Cameron kfree(full_postfix); 10883704432fSJonathan Cameron 1089a980e046SJonathan Cameron return ret; 1090a980e046SJonathan Cameron } 1091a980e046SJonathan Cameron 1092a980e046SJonathan Cameron static void __iio_device_attr_deinit(struct device_attribute *dev_attr) 1093a980e046SJonathan Cameron { 1094a980e046SJonathan Cameron kfree(dev_attr->attr.name); 1095a980e046SJonathan Cameron } 1096a980e046SJonathan Cameron 1097a980e046SJonathan Cameron int __iio_add_chan_devattr(const char *postfix, 1098a980e046SJonathan Cameron struct iio_chan_spec const *chan, 1099a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 1100a980e046SJonathan Cameron struct device_attribute *attr, 1101a980e046SJonathan Cameron char *buf), 1102a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 1103a980e046SJonathan Cameron struct device_attribute *attr, 1104a980e046SJonathan Cameron const char *buf, 1105a980e046SJonathan Cameron size_t len), 1106a980e046SJonathan Cameron u64 mask, 11073704432fSJonathan Cameron enum iio_shared_by shared_by, 1108a980e046SJonathan Cameron struct device *dev, 1109a980e046SJonathan Cameron struct list_head *attr_list) 1110a980e046SJonathan Cameron { 1111a980e046SJonathan Cameron int ret; 1112a980e046SJonathan Cameron struct iio_dev_attr *iio_attr, *t; 1113a980e046SJonathan Cameron 1114670c1103SSachin Kamat iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL); 111592825ff9SHartmut Knaack if (iio_attr == NULL) 111692825ff9SHartmut Knaack return -ENOMEM; 1117a980e046SJonathan Cameron ret = __iio_device_attr_init(&iio_attr->dev_attr, 1118a980e046SJonathan Cameron postfix, chan, 11193704432fSJonathan Cameron readfunc, writefunc, shared_by); 1120a980e046SJonathan Cameron if (ret) 1121a980e046SJonathan Cameron goto error_iio_dev_attr_free; 1122a980e046SJonathan Cameron iio_attr->c = chan; 1123a980e046SJonathan Cameron iio_attr->address = mask; 1124a980e046SJonathan Cameron list_for_each_entry(t, attr_list, l) 1125a980e046SJonathan Cameron if (strcmp(t->dev_attr.attr.name, 1126a980e046SJonathan Cameron iio_attr->dev_attr.attr.name) == 0) { 11273704432fSJonathan Cameron if (shared_by == IIO_SEPARATE) 1128a980e046SJonathan Cameron dev_err(dev, "tried to double register : %s\n", 1129a980e046SJonathan Cameron t->dev_attr.attr.name); 1130a980e046SJonathan Cameron ret = -EBUSY; 1131a980e046SJonathan Cameron goto error_device_attr_deinit; 1132a980e046SJonathan Cameron } 1133a980e046SJonathan Cameron list_add(&iio_attr->l, attr_list); 1134a980e046SJonathan Cameron 1135a980e046SJonathan Cameron return 0; 1136a980e046SJonathan Cameron 1137a980e046SJonathan Cameron error_device_attr_deinit: 1138a980e046SJonathan Cameron __iio_device_attr_deinit(&iio_attr->dev_attr); 1139a980e046SJonathan Cameron error_iio_dev_attr_free: 1140a980e046SJonathan Cameron kfree(iio_attr); 1141a980e046SJonathan Cameron return ret; 1142a980e046SJonathan Cameron } 1143a980e046SJonathan Cameron 11443704432fSJonathan Cameron static int iio_device_add_info_mask_type(struct iio_dev *indio_dev, 11453704432fSJonathan Cameron struct iio_chan_spec const *chan, 11463704432fSJonathan Cameron enum iio_shared_by shared_by, 11473704432fSJonathan Cameron const long *infomask) 11483704432fSJonathan Cameron { 1149207c2d27SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 11503704432fSJonathan Cameron int i, ret, attrcount = 0; 11513704432fSJonathan Cameron 11521016d567SOrson Zhai for_each_set_bit(i, infomask, sizeof(*infomask)*8) { 1153ef4b4856SJonathan Cameron if (i >= ARRAY_SIZE(iio_chan_info_postfix)) 1154ef4b4856SJonathan Cameron return -EINVAL; 11553704432fSJonathan Cameron ret = __iio_add_chan_devattr(iio_chan_info_postfix[i], 11563704432fSJonathan Cameron chan, 11573704432fSJonathan Cameron &iio_read_channel_info, 11583704432fSJonathan Cameron &iio_write_channel_info, 11593704432fSJonathan Cameron i, 11603704432fSJonathan Cameron shared_by, 11613704432fSJonathan Cameron &indio_dev->dev, 1162207c2d27SAlexandru Ardelean &iio_dev_opaque->channel_attr_list); 11633704432fSJonathan Cameron if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) 11643704432fSJonathan Cameron continue; 11653704432fSJonathan Cameron else if (ret < 0) 11663704432fSJonathan Cameron return ret; 11673704432fSJonathan Cameron attrcount++; 11683704432fSJonathan Cameron } 11693704432fSJonathan Cameron 11703704432fSJonathan Cameron return attrcount; 11713704432fSJonathan Cameron } 11723704432fSJonathan Cameron 117351239600SJonathan Cameron static int iio_device_add_info_mask_type_avail(struct iio_dev *indio_dev, 117451239600SJonathan Cameron struct iio_chan_spec const *chan, 117551239600SJonathan Cameron enum iio_shared_by shared_by, 117651239600SJonathan Cameron const long *infomask) 117751239600SJonathan Cameron { 1178207c2d27SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 117951239600SJonathan Cameron int i, ret, attrcount = 0; 118051239600SJonathan Cameron char *avail_postfix; 118151239600SJonathan Cameron 11821016d567SOrson Zhai for_each_set_bit(i, infomask, sizeof(*infomask) * 8) { 1183936d3e53SYoung Xiao if (i >= ARRAY_SIZE(iio_chan_info_postfix)) 1184936d3e53SYoung Xiao return -EINVAL; 118551239600SJonathan Cameron avail_postfix = kasprintf(GFP_KERNEL, 118651239600SJonathan Cameron "%s_available", 118751239600SJonathan Cameron iio_chan_info_postfix[i]); 118851239600SJonathan Cameron if (!avail_postfix) 118951239600SJonathan Cameron return -ENOMEM; 119051239600SJonathan Cameron 119151239600SJonathan Cameron ret = __iio_add_chan_devattr(avail_postfix, 119251239600SJonathan Cameron chan, 119351239600SJonathan Cameron &iio_read_channel_info_avail, 119451239600SJonathan Cameron NULL, 119551239600SJonathan Cameron i, 119651239600SJonathan Cameron shared_by, 119751239600SJonathan Cameron &indio_dev->dev, 1198207c2d27SAlexandru Ardelean &iio_dev_opaque->channel_attr_list); 119951239600SJonathan Cameron kfree(avail_postfix); 120051239600SJonathan Cameron if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) 120151239600SJonathan Cameron continue; 120251239600SJonathan Cameron else if (ret < 0) 120351239600SJonathan Cameron return ret; 120451239600SJonathan Cameron attrcount++; 120551239600SJonathan Cameron } 120651239600SJonathan Cameron 120751239600SJonathan Cameron return attrcount; 120851239600SJonathan Cameron } 120951239600SJonathan Cameron 1210a980e046SJonathan Cameron static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev, 1211a980e046SJonathan Cameron struct iio_chan_spec const *chan) 1212a980e046SJonathan Cameron { 1213207c2d27SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1214a980e046SJonathan Cameron int ret, attrcount = 0; 1215a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 1216a980e046SJonathan Cameron 1217a980e046SJonathan Cameron if (chan->channel < 0) 1218a980e046SJonathan Cameron return 0; 12193704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 12203704432fSJonathan Cameron IIO_SEPARATE, 12213704432fSJonathan Cameron &chan->info_mask_separate); 12228655cc49SJonathan Cameron if (ret < 0) 12233704432fSJonathan Cameron return ret; 12243704432fSJonathan Cameron attrcount += ret; 12253704432fSJonathan Cameron 122651239600SJonathan Cameron ret = iio_device_add_info_mask_type_avail(indio_dev, chan, 122751239600SJonathan Cameron IIO_SEPARATE, 122851239600SJonathan Cameron &chan-> 122951239600SJonathan Cameron info_mask_separate_available); 123051239600SJonathan Cameron if (ret < 0) 123151239600SJonathan Cameron return ret; 123251239600SJonathan Cameron attrcount += ret; 123351239600SJonathan Cameron 12343704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 12353704432fSJonathan Cameron IIO_SHARED_BY_TYPE, 12363704432fSJonathan Cameron &chan->info_mask_shared_by_type); 12373704432fSJonathan Cameron if (ret < 0) 12383704432fSJonathan Cameron return ret; 12393704432fSJonathan Cameron attrcount += ret; 1240a980e046SJonathan Cameron 124151239600SJonathan Cameron ret = iio_device_add_info_mask_type_avail(indio_dev, chan, 124251239600SJonathan Cameron IIO_SHARED_BY_TYPE, 124351239600SJonathan Cameron &chan-> 124451239600SJonathan Cameron info_mask_shared_by_type_available); 124551239600SJonathan Cameron if (ret < 0) 124651239600SJonathan Cameron return ret; 124751239600SJonathan Cameron attrcount += ret; 124851239600SJonathan Cameron 1249c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 1250c006ec83SJonathan Cameron IIO_SHARED_BY_DIR, 1251c006ec83SJonathan Cameron &chan->info_mask_shared_by_dir); 1252c006ec83SJonathan Cameron if (ret < 0) 1253c006ec83SJonathan Cameron return ret; 1254c006ec83SJonathan Cameron attrcount += ret; 1255c006ec83SJonathan Cameron 125651239600SJonathan Cameron ret = iio_device_add_info_mask_type_avail(indio_dev, chan, 125751239600SJonathan Cameron IIO_SHARED_BY_DIR, 125851239600SJonathan Cameron &chan->info_mask_shared_by_dir_available); 125951239600SJonathan Cameron if (ret < 0) 126051239600SJonathan Cameron return ret; 126151239600SJonathan Cameron attrcount += ret; 126251239600SJonathan Cameron 1263c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 1264c006ec83SJonathan Cameron IIO_SHARED_BY_ALL, 1265c006ec83SJonathan Cameron &chan->info_mask_shared_by_all); 1266c006ec83SJonathan Cameron if (ret < 0) 1267c006ec83SJonathan Cameron return ret; 1268c006ec83SJonathan Cameron attrcount += ret; 1269c006ec83SJonathan Cameron 127051239600SJonathan Cameron ret = iio_device_add_info_mask_type_avail(indio_dev, chan, 127151239600SJonathan Cameron IIO_SHARED_BY_ALL, 127251239600SJonathan Cameron &chan->info_mask_shared_by_all_available); 127351239600SJonathan Cameron if (ret < 0) 127451239600SJonathan Cameron return ret; 127551239600SJonathan Cameron attrcount += ret; 127651239600SJonathan Cameron 1277a980e046SJonathan Cameron if (chan->ext_info) { 1278a980e046SJonathan Cameron unsigned int i = 0; 1279a980e046SJonathan Cameron for (ext_info = chan->ext_info; ext_info->name; ext_info++) { 1280a980e046SJonathan Cameron ret = __iio_add_chan_devattr(ext_info->name, 1281a980e046SJonathan Cameron chan, 1282a980e046SJonathan Cameron ext_info->read ? 1283a980e046SJonathan Cameron &iio_read_channel_ext_info : NULL, 1284a980e046SJonathan Cameron ext_info->write ? 1285a980e046SJonathan Cameron &iio_write_channel_ext_info : NULL, 1286a980e046SJonathan Cameron i, 1287a980e046SJonathan Cameron ext_info->shared, 1288a980e046SJonathan Cameron &indio_dev->dev, 1289207c2d27SAlexandru Ardelean &iio_dev_opaque->channel_attr_list); 1290a980e046SJonathan Cameron i++; 1291a980e046SJonathan Cameron if (ret == -EBUSY && ext_info->shared) 1292a980e046SJonathan Cameron continue; 1293a980e046SJonathan Cameron 1294a980e046SJonathan Cameron if (ret) 12953704432fSJonathan Cameron return ret; 1296a980e046SJonathan Cameron 1297a980e046SJonathan Cameron attrcount++; 1298a980e046SJonathan Cameron } 1299a980e046SJonathan Cameron } 1300a980e046SJonathan Cameron 13013704432fSJonathan Cameron return attrcount; 1302a980e046SJonathan Cameron } 1303a980e046SJonathan Cameron 130484088ebdSLars-Peter Clausen /** 130584088ebdSLars-Peter Clausen * iio_free_chan_devattr_list() - Free a list of IIO device attributes 130684088ebdSLars-Peter Clausen * @attr_list: List of IIO device attributes 130784088ebdSLars-Peter Clausen * 130884088ebdSLars-Peter Clausen * This function frees the memory allocated for each of the IIO device 1309c1b03ab5SMartin Fuzzey * attributes in the list. 131084088ebdSLars-Peter Clausen */ 131184088ebdSLars-Peter Clausen void iio_free_chan_devattr_list(struct list_head *attr_list) 1312a980e046SJonathan Cameron { 131384088ebdSLars-Peter Clausen struct iio_dev_attr *p, *n; 131484088ebdSLars-Peter Clausen 131584088ebdSLars-Peter Clausen list_for_each_entry_safe(p, n, attr_list, l) { 1316a980e046SJonathan Cameron kfree(p->dev_attr.attr.name); 1317c1b03ab5SMartin Fuzzey list_del(&p->l); 1318a980e046SJonathan Cameron kfree(p); 1319a980e046SJonathan Cameron } 132084088ebdSLars-Peter Clausen } 1321a980e046SJonathan Cameron 1322a980e046SJonathan Cameron static ssize_t iio_show_dev_name(struct device *dev, 1323a980e046SJonathan Cameron struct device_attribute *attr, 1324a980e046SJonathan Cameron char *buf) 1325a980e046SJonathan Cameron { 1326e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1327598db581SKees Cook return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name); 1328a980e046SJonathan Cameron } 1329a980e046SJonathan Cameron 1330a980e046SJonathan Cameron static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL); 1331a980e046SJonathan Cameron 13322c3d0c9fSPhil Reid static ssize_t iio_show_dev_label(struct device *dev, 13332c3d0c9fSPhil Reid struct device_attribute *attr, 13342c3d0c9fSPhil Reid char *buf) 13352c3d0c9fSPhil Reid { 13362c3d0c9fSPhil Reid struct iio_dev *indio_dev = dev_to_iio_dev(dev); 13372c3d0c9fSPhil Reid return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->label); 13382c3d0c9fSPhil Reid } 13392c3d0c9fSPhil Reid 13402c3d0c9fSPhil Reid static DEVICE_ATTR(label, S_IRUGO, iio_show_dev_label, NULL); 13412c3d0c9fSPhil Reid 1342bc2b7dabSGregor Boirie static ssize_t iio_show_timestamp_clock(struct device *dev, 1343bc2b7dabSGregor Boirie struct device_attribute *attr, 1344bc2b7dabSGregor Boirie char *buf) 1345bc2b7dabSGregor Boirie { 1346bc2b7dabSGregor Boirie const struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1347bc2b7dabSGregor Boirie const clockid_t clk = iio_device_get_clock(indio_dev); 1348bc2b7dabSGregor Boirie const char *name; 1349bc2b7dabSGregor Boirie ssize_t sz; 1350bc2b7dabSGregor Boirie 1351bc2b7dabSGregor Boirie switch (clk) { 1352bc2b7dabSGregor Boirie case CLOCK_REALTIME: 1353bc2b7dabSGregor Boirie name = "realtime\n"; 1354bc2b7dabSGregor Boirie sz = sizeof("realtime\n"); 1355bc2b7dabSGregor Boirie break; 1356bc2b7dabSGregor Boirie case CLOCK_MONOTONIC: 1357bc2b7dabSGregor Boirie name = "monotonic\n"; 1358bc2b7dabSGregor Boirie sz = sizeof("monotonic\n"); 1359bc2b7dabSGregor Boirie break; 1360bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_RAW: 1361bc2b7dabSGregor Boirie name = "monotonic_raw\n"; 1362bc2b7dabSGregor Boirie sz = sizeof("monotonic_raw\n"); 1363bc2b7dabSGregor Boirie break; 1364bc2b7dabSGregor Boirie case CLOCK_REALTIME_COARSE: 1365bc2b7dabSGregor Boirie name = "realtime_coarse\n"; 1366bc2b7dabSGregor Boirie sz = sizeof("realtime_coarse\n"); 1367bc2b7dabSGregor Boirie break; 1368bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_COARSE: 1369bc2b7dabSGregor Boirie name = "monotonic_coarse\n"; 1370bc2b7dabSGregor Boirie sz = sizeof("monotonic_coarse\n"); 1371bc2b7dabSGregor Boirie break; 1372bc2b7dabSGregor Boirie case CLOCK_BOOTTIME: 1373bc2b7dabSGregor Boirie name = "boottime\n"; 1374bc2b7dabSGregor Boirie sz = sizeof("boottime\n"); 1375bc2b7dabSGregor Boirie break; 1376bc2b7dabSGregor Boirie case CLOCK_TAI: 1377bc2b7dabSGregor Boirie name = "tai\n"; 1378bc2b7dabSGregor Boirie sz = sizeof("tai\n"); 1379bc2b7dabSGregor Boirie break; 1380bc2b7dabSGregor Boirie default: 1381bc2b7dabSGregor Boirie BUG(); 1382bc2b7dabSGregor Boirie } 1383bc2b7dabSGregor Boirie 1384bc2b7dabSGregor Boirie memcpy(buf, name, sz); 1385bc2b7dabSGregor Boirie return sz; 1386bc2b7dabSGregor Boirie } 1387bc2b7dabSGregor Boirie 1388bc2b7dabSGregor Boirie static ssize_t iio_store_timestamp_clock(struct device *dev, 1389bc2b7dabSGregor Boirie struct device_attribute *attr, 1390bc2b7dabSGregor Boirie const char *buf, size_t len) 1391bc2b7dabSGregor Boirie { 1392bc2b7dabSGregor Boirie clockid_t clk; 1393bc2b7dabSGregor Boirie int ret; 1394bc2b7dabSGregor Boirie 1395bc2b7dabSGregor Boirie if (sysfs_streq(buf, "realtime")) 1396bc2b7dabSGregor Boirie clk = CLOCK_REALTIME; 1397bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "monotonic")) 1398bc2b7dabSGregor Boirie clk = CLOCK_MONOTONIC; 1399bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "monotonic_raw")) 1400bc2b7dabSGregor Boirie clk = CLOCK_MONOTONIC_RAW; 1401bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "realtime_coarse")) 1402bc2b7dabSGregor Boirie clk = CLOCK_REALTIME_COARSE; 1403bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "monotonic_coarse")) 1404bc2b7dabSGregor Boirie clk = CLOCK_MONOTONIC_COARSE; 1405bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "boottime")) 1406bc2b7dabSGregor Boirie clk = CLOCK_BOOTTIME; 1407bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "tai")) 1408bc2b7dabSGregor Boirie clk = CLOCK_TAI; 1409bc2b7dabSGregor Boirie else 1410bc2b7dabSGregor Boirie return -EINVAL; 1411bc2b7dabSGregor Boirie 1412bc2b7dabSGregor Boirie ret = iio_device_set_clock(dev_to_iio_dev(dev), clk); 1413bc2b7dabSGregor Boirie if (ret) 1414bc2b7dabSGregor Boirie return ret; 1415bc2b7dabSGregor Boirie 1416bc2b7dabSGregor Boirie return len; 1417bc2b7dabSGregor Boirie } 1418bc2b7dabSGregor Boirie 1419bc2b7dabSGregor Boirie static DEVICE_ATTR(current_timestamp_clock, S_IRUGO | S_IWUSR, 1420bc2b7dabSGregor Boirie iio_show_timestamp_clock, iio_store_timestamp_clock); 1421bc2b7dabSGregor Boirie 1422a980e046SJonathan Cameron static int iio_device_register_sysfs(struct iio_dev *indio_dev) 1423a980e046SJonathan Cameron { 1424207c2d27SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1425a980e046SJonathan Cameron int i, ret = 0, attrcount, attrn, attrcount_orig = 0; 142684088ebdSLars-Peter Clausen struct iio_dev_attr *p; 1427bc2b7dabSGregor Boirie struct attribute **attr, *clk = NULL; 1428a980e046SJonathan Cameron 1429a980e046SJonathan Cameron /* First count elements in any existing group */ 1430a980e046SJonathan Cameron if (indio_dev->info->attrs) { 1431a980e046SJonathan Cameron attr = indio_dev->info->attrs->attrs; 1432a980e046SJonathan Cameron while (*attr++ != NULL) 1433a980e046SJonathan Cameron attrcount_orig++; 1434a980e046SJonathan Cameron } 1435a980e046SJonathan Cameron attrcount = attrcount_orig; 1436a980e046SJonathan Cameron /* 1437a980e046SJonathan Cameron * New channel registration method - relies on the fact a group does 1438d25b3808SPeter Meerwald * not need to be initialized if its name is NULL. 1439a980e046SJonathan Cameron */ 1440a980e046SJonathan Cameron if (indio_dev->channels) 1441a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) { 1442bc2b7dabSGregor Boirie const struct iio_chan_spec *chan = 1443bc2b7dabSGregor Boirie &indio_dev->channels[i]; 1444bc2b7dabSGregor Boirie 1445bc2b7dabSGregor Boirie if (chan->type == IIO_TIMESTAMP) 1446bc2b7dabSGregor Boirie clk = &dev_attr_current_timestamp_clock.attr; 1447bc2b7dabSGregor Boirie 1448bc2b7dabSGregor Boirie ret = iio_device_add_channel_sysfs(indio_dev, chan); 1449a980e046SJonathan Cameron if (ret < 0) 1450a980e046SJonathan Cameron goto error_clear_attrs; 1451a980e046SJonathan Cameron attrcount += ret; 1452a980e046SJonathan Cameron } 1453a980e046SJonathan Cameron 1454fa83c3baSAlexandru Ardelean if (iio_dev_opaque->event_interface) 1455bc2b7dabSGregor Boirie clk = &dev_attr_current_timestamp_clock.attr; 1456bc2b7dabSGregor Boirie 1457a980e046SJonathan Cameron if (indio_dev->name) 1458a980e046SJonathan Cameron attrcount++; 14592c3d0c9fSPhil Reid if (indio_dev->label) 14602c3d0c9fSPhil Reid attrcount++; 1461bc2b7dabSGregor Boirie if (clk) 1462bc2b7dabSGregor Boirie attrcount++; 1463a980e046SJonathan Cameron 1464207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs = 1465207c2d27SAlexandru Ardelean kcalloc(attrcount + 1, 1466207c2d27SAlexandru Ardelean sizeof(iio_dev_opaque->chan_attr_group.attrs[0]), 1467a980e046SJonathan Cameron GFP_KERNEL); 1468207c2d27SAlexandru Ardelean if (iio_dev_opaque->chan_attr_group.attrs == NULL) { 1469a980e046SJonathan Cameron ret = -ENOMEM; 1470a980e046SJonathan Cameron goto error_clear_attrs; 1471a980e046SJonathan Cameron } 1472a980e046SJonathan Cameron /* Copy across original attributes */ 1473a980e046SJonathan Cameron if (indio_dev->info->attrs) 1474207c2d27SAlexandru Ardelean memcpy(iio_dev_opaque->chan_attr_group.attrs, 1475a980e046SJonathan Cameron indio_dev->info->attrs->attrs, 1476207c2d27SAlexandru Ardelean sizeof(iio_dev_opaque->chan_attr_group.attrs[0]) 1477a980e046SJonathan Cameron *attrcount_orig); 1478a980e046SJonathan Cameron attrn = attrcount_orig; 1479a980e046SJonathan Cameron /* Add all elements from the list. */ 1480207c2d27SAlexandru Ardelean list_for_each_entry(p, &iio_dev_opaque->channel_attr_list, l) 1481207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr; 1482a980e046SJonathan Cameron if (indio_dev->name) 1483207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr; 14842c3d0c9fSPhil Reid if (indio_dev->label) 1485207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_label.attr; 1486bc2b7dabSGregor Boirie if (clk) 1487207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs[attrn++] = clk; 1488a980e046SJonathan Cameron 1489a980e046SJonathan Cameron indio_dev->groups[indio_dev->groupcounter++] = 1490207c2d27SAlexandru Ardelean &iio_dev_opaque->chan_attr_group; 1491a980e046SJonathan Cameron 1492a980e046SJonathan Cameron return 0; 1493a980e046SJonathan Cameron 1494a980e046SJonathan Cameron error_clear_attrs: 1495207c2d27SAlexandru Ardelean iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list); 1496a980e046SJonathan Cameron 1497a980e046SJonathan Cameron return ret; 1498a980e046SJonathan Cameron } 1499a980e046SJonathan Cameron 1500a980e046SJonathan Cameron static void iio_device_unregister_sysfs(struct iio_dev *indio_dev) 1501a980e046SJonathan Cameron { 1502207c2d27SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1503a980e046SJonathan Cameron 1504207c2d27SAlexandru Ardelean iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list); 1505207c2d27SAlexandru Ardelean kfree(iio_dev_opaque->chan_attr_group.attrs); 1506207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs = NULL; 1507a980e046SJonathan Cameron } 1508a980e046SJonathan Cameron 1509a980e046SJonathan Cameron static void iio_dev_release(struct device *device) 1510a980e046SJonathan Cameron { 1511e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(device); 15126d4ebd56SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 15136d4ebd56SAlexandru Ardelean 1514d89e119aSBenjamin Gaignard if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES) 1515a980e046SJonathan Cameron iio_device_unregister_trigger_consumer(indio_dev); 1516a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 1517a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 1518e407fd65SLars-Peter Clausen 15199e69c935SLars-Peter Clausen iio_buffer_put(indio_dev->buffer); 15209e69c935SLars-Peter Clausen 1521e407fd65SLars-Peter Clausen ida_simple_remove(&iio_ida, indio_dev->id); 15226d4ebd56SAlexandru Ardelean kfree(iio_dev_opaque); 1523a980e046SJonathan Cameron } 1524a980e046SJonathan Cameron 152517d82b47SGuenter Roeck struct device_type iio_device_type = { 1526a980e046SJonathan Cameron .name = "iio_device", 1527a980e046SJonathan Cameron .release = iio_dev_release, 1528a980e046SJonathan Cameron }; 1529a980e046SJonathan Cameron 1530a7e57dceSSachin Kamat /** 1531a7e57dceSSachin Kamat * iio_device_alloc() - allocate an iio_dev from a driver 1532c9561fd2SJonathan Cameron * @parent: Parent device. 1533a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 1534a7e57dceSSachin Kamat **/ 153578289b4aSAlexandru Ardelean struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv) 1536a980e046SJonathan Cameron { 15376d4ebd56SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque; 1538a980e046SJonathan Cameron struct iio_dev *dev; 1539a980e046SJonathan Cameron size_t alloc_size; 1540a980e046SJonathan Cameron 15416d4ebd56SAlexandru Ardelean alloc_size = sizeof(struct iio_dev_opaque); 1542a980e046SJonathan Cameron if (sizeof_priv) { 1543a980e046SJonathan Cameron alloc_size = ALIGN(alloc_size, IIO_ALIGN); 1544a980e046SJonathan Cameron alloc_size += sizeof_priv; 1545a980e046SJonathan Cameron } 1546a980e046SJonathan Cameron 15476d4ebd56SAlexandru Ardelean iio_dev_opaque = kzalloc(alloc_size, GFP_KERNEL); 15486d4ebd56SAlexandru Ardelean if (!iio_dev_opaque) 15490d4b2184SJonathan Cameron return NULL; 1550a980e046SJonathan Cameron 15516d4ebd56SAlexandru Ardelean dev = &iio_dev_opaque->indio_dev; 15526d4ebd56SAlexandru Ardelean dev->priv = (char *)iio_dev_opaque + 15536d4ebd56SAlexandru Ardelean ALIGN(sizeof(struct iio_dev_opaque), IIO_ALIGN); 15546d4ebd56SAlexandru Ardelean 15558525df47SAlexandru Ardelean dev->dev.parent = parent; 1556a980e046SJonathan Cameron dev->dev.groups = dev->groups; 155717d82b47SGuenter Roeck dev->dev.type = &iio_device_type; 1558a980e046SJonathan Cameron dev->dev.bus = &iio_bus_type; 1559a980e046SJonathan Cameron device_initialize(&dev->dev); 1560a980e046SJonathan Cameron dev_set_drvdata(&dev->dev, (void *)dev); 1561a980e046SJonathan Cameron mutex_init(&dev->mlock); 1562a980e046SJonathan Cameron mutex_init(&dev->info_exist_lock); 1563207c2d27SAlexandru Ardelean INIT_LIST_HEAD(&iio_dev_opaque->channel_attr_list); 1564a980e046SJonathan Cameron 1565a980e046SJonathan Cameron dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL); 1566a980e046SJonathan Cameron if (dev->id < 0) { 1567a980e046SJonathan Cameron /* cannot use a dev_err as the name isn't available */ 15683176dd5dSSachin Kamat pr_err("failed to get device id\n"); 15696d4ebd56SAlexandru Ardelean kfree(iio_dev_opaque); 1570a980e046SJonathan Cameron return NULL; 1571a980e046SJonathan Cameron } 1572a980e046SJonathan Cameron dev_set_name(&dev->dev, "iio:device%d", dev->id); 15736a8c6b26SAlexandru Ardelean INIT_LIST_HEAD(&iio_dev_opaque->buffer_list); 1574a980e046SJonathan Cameron 1575a980e046SJonathan Cameron return dev; 1576a980e046SJonathan Cameron } 15777cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_alloc); 1578a980e046SJonathan Cameron 1579a7e57dceSSachin Kamat /** 1580a7e57dceSSachin Kamat * iio_device_free() - free an iio_dev from a driver 1581a7e57dceSSachin Kamat * @dev: the iio_dev associated with the device 1582a7e57dceSSachin Kamat **/ 15837cbb7537SLars-Peter Clausen void iio_device_free(struct iio_dev *dev) 1584a980e046SJonathan Cameron { 1585e407fd65SLars-Peter Clausen if (dev) 1586e407fd65SLars-Peter Clausen put_device(&dev->dev); 1587a980e046SJonathan Cameron } 15887cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_free); 1589a980e046SJonathan Cameron 15909dabaf5eSGrygorii Strashko static void devm_iio_device_release(struct device *dev, void *res) 15919dabaf5eSGrygorii Strashko { 15929dabaf5eSGrygorii Strashko iio_device_free(*(struct iio_dev **)res); 15939dabaf5eSGrygorii Strashko } 15949dabaf5eSGrygorii Strashko 1595a7e57dceSSachin Kamat /** 1596a7e57dceSSachin Kamat * devm_iio_device_alloc - Resource-managed iio_device_alloc() 159778289b4aSAlexandru Ardelean * @parent: Device to allocate iio_dev for, and parent for this IIO device 1598a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 1599a7e57dceSSachin Kamat * 1600a7e57dceSSachin Kamat * Managed iio_device_alloc. iio_dev allocated with this function is 1601a7e57dceSSachin Kamat * automatically freed on driver detach. 1602a7e57dceSSachin Kamat * 1603a7e57dceSSachin Kamat * RETURNS: 1604a7e57dceSSachin Kamat * Pointer to allocated iio_dev on success, NULL on failure. 1605a7e57dceSSachin Kamat */ 160678289b4aSAlexandru Ardelean struct iio_dev *devm_iio_device_alloc(struct device *parent, int sizeof_priv) 16079dabaf5eSGrygorii Strashko { 16089dabaf5eSGrygorii Strashko struct iio_dev **ptr, *iio_dev; 16099dabaf5eSGrygorii Strashko 16109dabaf5eSGrygorii Strashko ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr), 16119dabaf5eSGrygorii Strashko GFP_KERNEL); 16129dabaf5eSGrygorii Strashko if (!ptr) 16139dabaf5eSGrygorii Strashko return NULL; 16149dabaf5eSGrygorii Strashko 161578289b4aSAlexandru Ardelean iio_dev = iio_device_alloc(parent, sizeof_priv); 16169dabaf5eSGrygorii Strashko if (iio_dev) { 16179dabaf5eSGrygorii Strashko *ptr = iio_dev; 161878289b4aSAlexandru Ardelean devres_add(parent, ptr); 16199dabaf5eSGrygorii Strashko } else { 16209dabaf5eSGrygorii Strashko devres_free(ptr); 16219dabaf5eSGrygorii Strashko } 16229dabaf5eSGrygorii Strashko 16239dabaf5eSGrygorii Strashko return iio_dev; 16249dabaf5eSGrygorii Strashko } 16259dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_alloc); 16269dabaf5eSGrygorii Strashko 1627a7e57dceSSachin Kamat /** 1628a980e046SJonathan Cameron * iio_chrdev_open() - chrdev file open for buffer access and ioctls 16292498dcf6SCristina Opriceana * @inode: Inode structure for identifying the device in the file system 16302498dcf6SCristina Opriceana * @filp: File structure for iio device used to keep and later access 16312498dcf6SCristina Opriceana * private data 16322498dcf6SCristina Opriceana * 16332498dcf6SCristina Opriceana * Return: 0 on success or -EBUSY if the device is already opened 1634a980e046SJonathan Cameron **/ 1635a980e046SJonathan Cameron static int iio_chrdev_open(struct inode *inode, struct file *filp) 1636a980e046SJonathan Cameron { 1637a980e046SJonathan Cameron struct iio_dev *indio_dev = container_of(inode->i_cdev, 1638a980e046SJonathan Cameron struct iio_dev, chrdev); 1639a980e046SJonathan Cameron 1640a980e046SJonathan Cameron if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags)) 1641a980e046SJonathan Cameron return -EBUSY; 1642a980e046SJonathan Cameron 1643cadc2125SLars-Peter Clausen iio_device_get(indio_dev); 1644cadc2125SLars-Peter Clausen 1645a980e046SJonathan Cameron filp->private_data = indio_dev; 1646a980e046SJonathan Cameron 1647a980e046SJonathan Cameron return 0; 1648a980e046SJonathan Cameron } 1649a980e046SJonathan Cameron 1650a980e046SJonathan Cameron /** 1651a980e046SJonathan Cameron * iio_chrdev_release() - chrdev file close buffer access and ioctls 16522498dcf6SCristina Opriceana * @inode: Inode structure pointer for the char device 16532498dcf6SCristina Opriceana * @filp: File structure pointer for the char device 16542498dcf6SCristina Opriceana * 16552498dcf6SCristina Opriceana * Return: 0 for successful release 16562498dcf6SCristina Opriceana */ 1657a980e046SJonathan Cameron static int iio_chrdev_release(struct inode *inode, struct file *filp) 1658a980e046SJonathan Cameron { 1659a980e046SJonathan Cameron struct iio_dev *indio_dev = container_of(inode->i_cdev, 1660a980e046SJonathan Cameron struct iio_dev, chrdev); 1661a980e046SJonathan Cameron clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags); 1662cadc2125SLars-Peter Clausen iio_device_put(indio_dev); 1663cadc2125SLars-Peter Clausen 1664a980e046SJonathan Cameron return 0; 1665a980e046SJonathan Cameron } 1666a980e046SJonathan Cameron 1667a980e046SJonathan Cameron /* Somewhat of a cross file organization violation - ioctls here are actually 1668a980e046SJonathan Cameron * event related */ 1669a980e046SJonathan Cameron static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 1670a980e046SJonathan Cameron { 1671a980e046SJonathan Cameron struct iio_dev *indio_dev = filp->private_data; 1672a980e046SJonathan Cameron int __user *ip = (int __user *)arg; 1673a980e046SJonathan Cameron int fd; 1674a980e046SJonathan Cameron 1675f18e7a06SLars-Peter Clausen if (!indio_dev->info) 1676f18e7a06SLars-Peter Clausen return -ENODEV; 1677f18e7a06SLars-Peter Clausen 1678a980e046SJonathan Cameron if (cmd == IIO_GET_EVENT_FD_IOCTL) { 1679a980e046SJonathan Cameron fd = iio_event_getfd(indio_dev); 16803f9059b7SLinus Walleij if (fd < 0) 16813f9059b7SLinus Walleij return fd; 1682a980e046SJonathan Cameron if (copy_to_user(ip, &fd, sizeof(fd))) 1683a980e046SJonathan Cameron return -EFAULT; 1684a980e046SJonathan Cameron return 0; 1685a980e046SJonathan Cameron } 1686a980e046SJonathan Cameron return -EINVAL; 1687a980e046SJonathan Cameron } 1688a980e046SJonathan Cameron 1689a980e046SJonathan Cameron static const struct file_operations iio_buffer_fileops = { 1690f6d4033dSLars-Peter Clausen .read = iio_buffer_read_outer_addr, 1691a980e046SJonathan Cameron .release = iio_chrdev_release, 1692a980e046SJonathan Cameron .open = iio_chrdev_open, 1693a980e046SJonathan Cameron .poll = iio_buffer_poll_addr, 1694a980e046SJonathan Cameron .owner = THIS_MODULE, 1695a980e046SJonathan Cameron .llseek = noop_llseek, 1696a980e046SJonathan Cameron .unlocked_ioctl = iio_ioctl, 16971832f2d8SArnd Bergmann .compat_ioctl = compat_ptr_ioctl, 1698a980e046SJonathan Cameron }; 1699a980e046SJonathan Cameron 17008f5d8727SVlad Dogaru static int iio_check_unique_scan_index(struct iio_dev *indio_dev) 17018f5d8727SVlad Dogaru { 17028f5d8727SVlad Dogaru int i, j; 17038f5d8727SVlad Dogaru const struct iio_chan_spec *channels = indio_dev->channels; 17048f5d8727SVlad Dogaru 17058f5d8727SVlad Dogaru if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES)) 17068f5d8727SVlad Dogaru return 0; 17078f5d8727SVlad Dogaru 17088f5d8727SVlad Dogaru for (i = 0; i < indio_dev->num_channels - 1; i++) { 17098f5d8727SVlad Dogaru if (channels[i].scan_index < 0) 17108f5d8727SVlad Dogaru continue; 17118f5d8727SVlad Dogaru for (j = i + 1; j < indio_dev->num_channels; j++) 17128f5d8727SVlad Dogaru if (channels[i].scan_index == channels[j].scan_index) { 17138f5d8727SVlad Dogaru dev_err(&indio_dev->dev, 17148f5d8727SVlad Dogaru "Duplicate scan index %d\n", 17158f5d8727SVlad Dogaru channels[i].scan_index); 17168f5d8727SVlad Dogaru return -EINVAL; 17178f5d8727SVlad Dogaru } 17188f5d8727SVlad Dogaru } 17198f5d8727SVlad Dogaru 17208f5d8727SVlad Dogaru return 0; 17218f5d8727SVlad Dogaru } 17228f5d8727SVlad Dogaru 1723a980e046SJonathan Cameron static const struct iio_buffer_setup_ops noop_ring_setup_ops; 1724a980e046SJonathan Cameron 172563b19547SJonathan Cameron int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod) 1726a980e046SJonathan Cameron { 1727a980e046SJonathan Cameron int ret; 1728a980e046SJonathan Cameron 1729a17cb784SAlexandru Ardelean if (!indio_dev->info) 1730a17cb784SAlexandru Ardelean return -EINVAL; 1731a17cb784SAlexandru Ardelean 173263b19547SJonathan Cameron indio_dev->driver_module = this_mod; 173317d82b47SGuenter Roeck /* If the calling driver did not initialize of_node, do it here */ 173417d82b47SGuenter Roeck if (!indio_dev->dev.of_node && indio_dev->dev.parent) 173517d82b47SGuenter Roeck indio_dev->dev.of_node = indio_dev->dev.parent->of_node; 173617d82b47SGuenter Roeck 17372c3d0c9fSPhil Reid indio_dev->label = of_get_property(indio_dev->dev.of_node, "label", 17382c3d0c9fSPhil Reid NULL); 17392c3d0c9fSPhil Reid 17408f5d8727SVlad Dogaru ret = iio_check_unique_scan_index(indio_dev); 17418f5d8727SVlad Dogaru if (ret < 0) 17428f5d8727SVlad Dogaru return ret; 17438f5d8727SVlad Dogaru 1744a980e046SJonathan Cameron /* configure elements for the chrdev */ 1745a980e046SJonathan Cameron indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id); 1746a980e046SJonathan Cameron 17478915aacaSGreg Kroah-Hartman iio_device_register_debugfs(indio_dev); 17483e1b6c95SLars-Peter Clausen 17493e1b6c95SLars-Peter Clausen ret = iio_buffer_alloc_sysfs_and_mask(indio_dev); 17503e1b6c95SLars-Peter Clausen if (ret) { 17513e1b6c95SLars-Peter Clausen dev_err(indio_dev->dev.parent, 17523e1b6c95SLars-Peter Clausen "Failed to create buffer sysfs interfaces\n"); 17533e1b6c95SLars-Peter Clausen goto error_unreg_debugfs; 17543e1b6c95SLars-Peter Clausen } 17553e1b6c95SLars-Peter Clausen 1756a980e046SJonathan Cameron ret = iio_device_register_sysfs(indio_dev); 1757a980e046SJonathan Cameron if (ret) { 1758a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1759a980e046SJonathan Cameron "Failed to register sysfs interfaces\n"); 17603e1b6c95SLars-Peter Clausen goto error_buffer_free_sysfs; 1761a980e046SJonathan Cameron } 1762a980e046SJonathan Cameron ret = iio_device_register_eventset(indio_dev); 1763a980e046SJonathan Cameron if (ret) { 1764a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1765a980e046SJonathan Cameron "Failed to register event set\n"); 1766a980e046SJonathan Cameron goto error_free_sysfs; 1767a980e046SJonathan Cameron } 1768d89e119aSBenjamin Gaignard if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES) 1769a980e046SJonathan Cameron iio_device_register_trigger_consumer(indio_dev); 1770a980e046SJonathan Cameron 1771a980e046SJonathan Cameron if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) && 1772a980e046SJonathan Cameron indio_dev->setup_ops == NULL) 1773a980e046SJonathan Cameron indio_dev->setup_ops = &noop_ring_setup_ops; 1774a980e046SJonathan Cameron 1775a980e046SJonathan Cameron cdev_init(&indio_dev->chrdev, &iio_buffer_fileops); 177663b19547SJonathan Cameron 177763b19547SJonathan Cameron indio_dev->chrdev.owner = this_mod; 177838923911SLogan Gunthorpe 177938923911SLogan Gunthorpe ret = cdev_device_add(&indio_dev->chrdev, &indio_dev->dev); 1780a980e046SJonathan Cameron if (ret < 0) 17810d5b7daeSLars-Peter Clausen goto error_unreg_eventset; 1782a980e046SJonathan Cameron 17830d5b7daeSLars-Peter Clausen return 0; 178438923911SLogan Gunthorpe 1785a980e046SJonathan Cameron error_unreg_eventset: 1786a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 1787a980e046SJonathan Cameron error_free_sysfs: 1788a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 17893e1b6c95SLars-Peter Clausen error_buffer_free_sysfs: 17903e1b6c95SLars-Peter Clausen iio_buffer_free_sysfs_and_mask(indio_dev); 1791a980e046SJonathan Cameron error_unreg_debugfs: 1792a980e046SJonathan Cameron iio_device_unregister_debugfs(indio_dev); 1793a980e046SJonathan Cameron return ret; 1794a980e046SJonathan Cameron } 179563b19547SJonathan Cameron EXPORT_SYMBOL(__iio_device_register); 1796a980e046SJonathan Cameron 1797a7e57dceSSachin Kamat /** 1798a7e57dceSSachin Kamat * iio_device_unregister() - unregister a device from the IIO subsystem 1799a7e57dceSSachin Kamat * @indio_dev: Device structure representing the device. 1800a7e57dceSSachin Kamat **/ 1801a980e046SJonathan Cameron void iio_device_unregister(struct iio_dev *indio_dev) 1802a980e046SJonathan Cameron { 180338923911SLogan Gunthorpe cdev_device_del(&indio_dev->chrdev, &indio_dev->dev); 1804a87c82e4SLars-Peter Clausen 18057f75591fSFabrice Gasnier mutex_lock(&indio_dev->info_exist_lock); 18067f75591fSFabrice Gasnier 1807bc4c9612SLars-Peter Clausen iio_device_unregister_debugfs(indio_dev); 18080d5b7daeSLars-Peter Clausen 1809a87c82e4SLars-Peter Clausen iio_disable_all_buffers(indio_dev); 1810a87c82e4SLars-Peter Clausen 1811a980e046SJonathan Cameron indio_dev->info = NULL; 1812d2f0a48fSLars-Peter Clausen 1813d2f0a48fSLars-Peter Clausen iio_device_wakeup_eventset(indio_dev); 1814d2f0a48fSLars-Peter Clausen iio_buffer_wakeup_poll(indio_dev); 1815d2f0a48fSLars-Peter Clausen 1816a980e046SJonathan Cameron mutex_unlock(&indio_dev->info_exist_lock); 18173e1b6c95SLars-Peter Clausen 18183e1b6c95SLars-Peter Clausen iio_buffer_free_sysfs_and_mask(indio_dev); 1819a980e046SJonathan Cameron } 1820a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_unregister); 18218caa07c0SSachin Kamat 18228caa07c0SSachin Kamat static void devm_iio_device_unreg(struct device *dev, void *res) 18238caa07c0SSachin Kamat { 18248caa07c0SSachin Kamat iio_device_unregister(*(struct iio_dev **)res); 18258caa07c0SSachin Kamat } 18268caa07c0SSachin Kamat 182763b19547SJonathan Cameron int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev, 182863b19547SJonathan Cameron struct module *this_mod) 18298caa07c0SSachin Kamat { 18308caa07c0SSachin Kamat struct iio_dev **ptr; 18318caa07c0SSachin Kamat int ret; 18328caa07c0SSachin Kamat 18338caa07c0SSachin Kamat ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL); 18348caa07c0SSachin Kamat if (!ptr) 18358caa07c0SSachin Kamat return -ENOMEM; 18368caa07c0SSachin Kamat 18378caa07c0SSachin Kamat *ptr = indio_dev; 183863b19547SJonathan Cameron ret = __iio_device_register(indio_dev, this_mod); 18398caa07c0SSachin Kamat if (!ret) 18408caa07c0SSachin Kamat devres_add(dev, ptr); 18418caa07c0SSachin Kamat else 18428caa07c0SSachin Kamat devres_free(ptr); 18438caa07c0SSachin Kamat 18448caa07c0SSachin Kamat return ret; 18458caa07c0SSachin Kamat } 184663b19547SJonathan Cameron EXPORT_SYMBOL_GPL(__devm_iio_device_register); 18478caa07c0SSachin Kamat 18488caa07c0SSachin Kamat /** 184908a33805SAlison Schofield * iio_device_claim_direct_mode - Keep device in direct mode 185008a33805SAlison Schofield * @indio_dev: the iio_dev associated with the device 185108a33805SAlison Schofield * 185208a33805SAlison Schofield * If the device is in direct mode it is guaranteed to stay 185308a33805SAlison Schofield * that way until iio_device_release_direct_mode() is called. 185408a33805SAlison Schofield * 185508a33805SAlison Schofield * Use with iio_device_release_direct_mode() 185608a33805SAlison Schofield * 185708a33805SAlison Schofield * Returns: 0 on success, -EBUSY on failure 185808a33805SAlison Schofield */ 185908a33805SAlison Schofield int iio_device_claim_direct_mode(struct iio_dev *indio_dev) 186008a33805SAlison Schofield { 186108a33805SAlison Schofield mutex_lock(&indio_dev->mlock); 186208a33805SAlison Schofield 186308a33805SAlison Schofield if (iio_buffer_enabled(indio_dev)) { 186408a33805SAlison Schofield mutex_unlock(&indio_dev->mlock); 186508a33805SAlison Schofield return -EBUSY; 186608a33805SAlison Schofield } 186708a33805SAlison Schofield return 0; 186808a33805SAlison Schofield } 186908a33805SAlison Schofield EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode); 187008a33805SAlison Schofield 187108a33805SAlison Schofield /** 187208a33805SAlison Schofield * iio_device_release_direct_mode - releases claim on direct mode 187308a33805SAlison Schofield * @indio_dev: the iio_dev associated with the device 187408a33805SAlison Schofield * 187508a33805SAlison Schofield * Release the claim. Device is no longer guaranteed to stay 187608a33805SAlison Schofield * in direct mode. 187708a33805SAlison Schofield * 187808a33805SAlison Schofield * Use with iio_device_claim_direct_mode() 187908a33805SAlison Schofield */ 188008a33805SAlison Schofield void iio_device_release_direct_mode(struct iio_dev *indio_dev) 188108a33805SAlison Schofield { 188208a33805SAlison Schofield mutex_unlock(&indio_dev->mlock); 188308a33805SAlison Schofield } 188408a33805SAlison Schofield EXPORT_SYMBOL_GPL(iio_device_release_direct_mode); 188508a33805SAlison Schofield 1886a980e046SJonathan Cameron subsys_initcall(iio_init); 1887a980e046SJonathan Cameron module_exit(iio_exit); 1888a980e046SJonathan Cameron 1889c8b95952SJonathan Cameron MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); 1890a980e046SJonathan Cameron MODULE_DESCRIPTION("Industrial I/O core"); 1891a980e046SJonathan Cameron MODULE_LICENSE("GPL"); 1892