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> 28*6d4ebd56SAlexandru 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", 134a980e046SJonathan Cameron }; 135a980e046SJonathan Cameron 136a980e046SJonathan Cameron /* relies on pairs of these shared then separate */ 137a980e046SJonathan Cameron static const char * const iio_chan_info_postfix[] = { 138a980e046SJonathan Cameron [IIO_CHAN_INFO_RAW] = "raw", 139a980e046SJonathan Cameron [IIO_CHAN_INFO_PROCESSED] = "input", 140a980e046SJonathan Cameron [IIO_CHAN_INFO_SCALE] = "scale", 141a980e046SJonathan Cameron [IIO_CHAN_INFO_OFFSET] = "offset", 142a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBSCALE] = "calibscale", 143a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBBIAS] = "calibbias", 144a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK] = "peak_raw", 145a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale", 146a980e046SJonathan Cameron [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw", 147a980e046SJonathan Cameron [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw", 148a980e046SJonathan Cameron [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY] 149a980e046SJonathan Cameron = "filter_low_pass_3db_frequency", 1503f7f642bSMartin Fuzzey [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY] 1513f7f642bSMartin Fuzzey = "filter_high_pass_3db_frequency", 152a980e046SJonathan Cameron [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency", 153a6b12855SMichael Hennerich [IIO_CHAN_INFO_FREQUENCY] = "frequency", 154a6b12855SMichael Hennerich [IIO_CHAN_INFO_PHASE] = "phase", 155b65d6212SMichael Hennerich [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain", 1567c9ab035Ssrinivas pandruvada [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis", 157899d90bdSPeter Meerwald [IIO_CHAN_INFO_INT_TIME] = "integration_time", 158a88bfe78SIrina Tirdea [IIO_CHAN_INFO_ENABLE] = "en", 159bcdf28fbSIrina Tirdea [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight", 160d37f6836SIrina Tirdea [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight", 1612f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count", 1622f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time", 163c8a85854SVianney le Clément de Saint-Marcq [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity", 164faaa4495SIrina Tirdea [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio", 165ddfb97d8SAndrea Merello [IIO_CHAN_INFO_THERMOCOUPLE_TYPE] = "thermocouple_type", 166a980e046SJonathan Cameron }; 167a980e046SJonathan Cameron 168a7e57dceSSachin Kamat /** 169a7e57dceSSachin Kamat * iio_find_channel_from_si() - get channel from its scan index 170a7e57dceSSachin Kamat * @indio_dev: device 171a7e57dceSSachin Kamat * @si: scan index to match 172a7e57dceSSachin Kamat */ 173a980e046SJonathan Cameron const struct iio_chan_spec 174a980e046SJonathan Cameron *iio_find_channel_from_si(struct iio_dev *indio_dev, int si) 175a980e046SJonathan Cameron { 176a980e046SJonathan Cameron int i; 177a980e046SJonathan Cameron 178a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) 179a980e046SJonathan Cameron if (indio_dev->channels[i].scan_index == si) 180a980e046SJonathan Cameron return &indio_dev->channels[i]; 181a980e046SJonathan Cameron return NULL; 182a980e046SJonathan Cameron } 183a980e046SJonathan Cameron 184a980e046SJonathan Cameron /* This turns up an awful lot */ 185a980e046SJonathan Cameron ssize_t iio_read_const_attr(struct device *dev, 186a980e046SJonathan Cameron struct device_attribute *attr, 187a980e046SJonathan Cameron char *buf) 188a980e046SJonathan Cameron { 189a980e046SJonathan Cameron return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string); 190a980e046SJonathan Cameron } 191a980e046SJonathan Cameron EXPORT_SYMBOL(iio_read_const_attr); 192a980e046SJonathan Cameron 19369f0793eSGwendal Grignou /** 19469f0793eSGwendal Grignou * iio_device_set_clock() - Set current timestamping clock for the device 19569f0793eSGwendal Grignou * @indio_dev: IIO device structure containing the device 19669f0793eSGwendal Grignou * @clock_id: timestamping clock posix identifier to set. 19769f0793eSGwendal Grignou */ 19869f0793eSGwendal Grignou int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id) 199bc2b7dabSGregor Boirie { 200bc2b7dabSGregor Boirie int ret; 201bc2b7dabSGregor Boirie const struct iio_event_interface *ev_int = indio_dev->event_interface; 202bc2b7dabSGregor Boirie 203bc2b7dabSGregor Boirie ret = mutex_lock_interruptible(&indio_dev->mlock); 204bc2b7dabSGregor Boirie if (ret) 205bc2b7dabSGregor Boirie return ret; 206bc2b7dabSGregor Boirie if ((ev_int && iio_event_enabled(ev_int)) || 207bc2b7dabSGregor Boirie iio_buffer_enabled(indio_dev)) { 208bc2b7dabSGregor Boirie mutex_unlock(&indio_dev->mlock); 209bc2b7dabSGregor Boirie return -EBUSY; 210bc2b7dabSGregor Boirie } 211bc2b7dabSGregor Boirie indio_dev->clock_id = clock_id; 212bc2b7dabSGregor Boirie mutex_unlock(&indio_dev->mlock); 213bc2b7dabSGregor Boirie 214bc2b7dabSGregor Boirie return 0; 215bc2b7dabSGregor Boirie } 21669f0793eSGwendal Grignou EXPORT_SYMBOL(iio_device_set_clock); 217bc2b7dabSGregor Boirie 218bc2b7dabSGregor Boirie /** 219bc2b7dabSGregor Boirie * iio_get_time_ns() - utility function to get a time stamp for events etc 220bc2b7dabSGregor Boirie * @indio_dev: device 221bc2b7dabSGregor Boirie */ 222bc2b7dabSGregor Boirie s64 iio_get_time_ns(const struct iio_dev *indio_dev) 223bc2b7dabSGregor Boirie { 22445e7f5d2SArnd Bergmann struct timespec64 tp; 225bc2b7dabSGregor Boirie 226bc2b7dabSGregor Boirie switch (iio_device_get_clock(indio_dev)) { 227bc2b7dabSGregor Boirie case CLOCK_REALTIME: 22845e7f5d2SArnd Bergmann return ktime_get_real_ns(); 229bc2b7dabSGregor Boirie case CLOCK_MONOTONIC: 23045e7f5d2SArnd Bergmann return ktime_get_ns(); 231bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_RAW: 23245e7f5d2SArnd Bergmann return ktime_get_raw_ns(); 233bc2b7dabSGregor Boirie case CLOCK_REALTIME_COARSE: 23445e7f5d2SArnd Bergmann return ktime_to_ns(ktime_get_coarse_real()); 235bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_COARSE: 23645e7f5d2SArnd Bergmann ktime_get_coarse_ts64(&tp); 23745e7f5d2SArnd Bergmann return timespec64_to_ns(&tp); 238bc2b7dabSGregor Boirie case CLOCK_BOOTTIME: 2399285ec4cSJason A. Donenfeld return ktime_get_boottime_ns(); 240bc2b7dabSGregor Boirie case CLOCK_TAI: 2419285ec4cSJason A. Donenfeld return ktime_get_clocktai_ns(); 242bc2b7dabSGregor Boirie default: 243bc2b7dabSGregor Boirie BUG(); 244bc2b7dabSGregor Boirie } 245bc2b7dabSGregor Boirie } 246bc2b7dabSGregor Boirie EXPORT_SYMBOL(iio_get_time_ns); 247bc2b7dabSGregor Boirie 248bc2b7dabSGregor Boirie /** 249bc2b7dabSGregor Boirie * iio_get_time_res() - utility function to get time stamp clock resolution in 250bc2b7dabSGregor Boirie * nano seconds. 251bc2b7dabSGregor Boirie * @indio_dev: device 252bc2b7dabSGregor Boirie */ 253bc2b7dabSGregor Boirie unsigned int iio_get_time_res(const struct iio_dev *indio_dev) 254bc2b7dabSGregor Boirie { 255bc2b7dabSGregor Boirie switch (iio_device_get_clock(indio_dev)) { 256bc2b7dabSGregor Boirie case CLOCK_REALTIME: 257bc2b7dabSGregor Boirie case CLOCK_MONOTONIC: 258bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_RAW: 259bc2b7dabSGregor Boirie case CLOCK_BOOTTIME: 260bc2b7dabSGregor Boirie case CLOCK_TAI: 261bc2b7dabSGregor Boirie return hrtimer_resolution; 262bc2b7dabSGregor Boirie case CLOCK_REALTIME_COARSE: 263bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_COARSE: 264bc2b7dabSGregor Boirie return LOW_RES_NSEC; 265bc2b7dabSGregor Boirie default: 266bc2b7dabSGregor Boirie BUG(); 267bc2b7dabSGregor Boirie } 268bc2b7dabSGregor Boirie } 269bc2b7dabSGregor Boirie EXPORT_SYMBOL(iio_get_time_res); 270bc2b7dabSGregor Boirie 271a980e046SJonathan Cameron static int __init iio_init(void) 272a980e046SJonathan Cameron { 273a980e046SJonathan Cameron int ret; 274a980e046SJonathan Cameron 275a980e046SJonathan Cameron /* Register sysfs bus */ 276a980e046SJonathan Cameron ret = bus_register(&iio_bus_type); 277a980e046SJonathan Cameron if (ret < 0) { 2783176dd5dSSachin Kamat pr_err("could not register bus type\n"); 279a980e046SJonathan Cameron goto error_nothing; 280a980e046SJonathan Cameron } 281a980e046SJonathan Cameron 282a980e046SJonathan Cameron ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio"); 283a980e046SJonathan Cameron if (ret < 0) { 2843176dd5dSSachin Kamat pr_err("failed to allocate char dev region\n"); 285a980e046SJonathan Cameron goto error_unregister_bus_type; 286a980e046SJonathan Cameron } 287a980e046SJonathan Cameron 288a980e046SJonathan Cameron iio_debugfs_dentry = debugfs_create_dir("iio", NULL); 289a980e046SJonathan Cameron 290a980e046SJonathan Cameron return 0; 291a980e046SJonathan Cameron 292a980e046SJonathan Cameron error_unregister_bus_type: 293a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 294a980e046SJonathan Cameron error_nothing: 295a980e046SJonathan Cameron return ret; 296a980e046SJonathan Cameron } 297a980e046SJonathan Cameron 298a980e046SJonathan Cameron static void __exit iio_exit(void) 299a980e046SJonathan Cameron { 300a980e046SJonathan Cameron if (iio_devt) 301a980e046SJonathan Cameron unregister_chrdev_region(iio_devt, IIO_DEV_MAX); 302a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 303a980e046SJonathan Cameron debugfs_remove(iio_debugfs_dentry); 304a980e046SJonathan Cameron } 305a980e046SJonathan Cameron 306a980e046SJonathan Cameron #if defined(CONFIG_DEBUG_FS) 307a980e046SJonathan Cameron static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf, 308a980e046SJonathan Cameron size_t count, loff_t *ppos) 309a980e046SJonathan Cameron { 310a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 311a980e046SJonathan Cameron unsigned val = 0; 312a980e046SJonathan Cameron int ret; 313a980e046SJonathan Cameron 3142ddc982aSAlexandru Tachici if (*ppos > 0) 3152ddc982aSAlexandru Tachici return simple_read_from_buffer(userbuf, count, ppos, 3162ddc982aSAlexandru Tachici indio_dev->read_buf, 3172ddc982aSAlexandru Tachici indio_dev->read_buf_len); 3182ddc982aSAlexandru Tachici 319a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, 320a980e046SJonathan Cameron indio_dev->cached_reg_addr, 321a980e046SJonathan Cameron 0, &val); 3223d62c78aSMatt Fornero if (ret) { 323a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__); 3243d62c78aSMatt Fornero return ret; 3253d62c78aSMatt Fornero } 326a980e046SJonathan Cameron 3272ddc982aSAlexandru Tachici indio_dev->read_buf_len = snprintf(indio_dev->read_buf, 3282ddc982aSAlexandru Tachici sizeof(indio_dev->read_buf), 3292ddc982aSAlexandru Tachici "0x%X\n", val); 330a980e046SJonathan Cameron 3312ddc982aSAlexandru Tachici return simple_read_from_buffer(userbuf, count, ppos, 3322ddc982aSAlexandru Tachici indio_dev->read_buf, 3332ddc982aSAlexandru Tachici indio_dev->read_buf_len); 334a980e046SJonathan Cameron } 335a980e046SJonathan Cameron 336a980e046SJonathan Cameron static ssize_t iio_debugfs_write_reg(struct file *file, 337a980e046SJonathan Cameron const char __user *userbuf, size_t count, loff_t *ppos) 338a980e046SJonathan Cameron { 339a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 340a980e046SJonathan Cameron unsigned reg, val; 341a980e046SJonathan Cameron char buf[80]; 342a980e046SJonathan Cameron int ret; 343a980e046SJonathan Cameron 344a980e046SJonathan Cameron count = min_t(size_t, count, (sizeof(buf)-1)); 345a980e046SJonathan Cameron if (copy_from_user(buf, userbuf, count)) 346a980e046SJonathan Cameron return -EFAULT; 347a980e046SJonathan Cameron 348a980e046SJonathan Cameron buf[count] = 0; 349a980e046SJonathan Cameron 350a980e046SJonathan Cameron ret = sscanf(buf, "%i %i", ®, &val); 351a980e046SJonathan Cameron 352a980e046SJonathan Cameron switch (ret) { 353a980e046SJonathan Cameron case 1: 354a980e046SJonathan Cameron indio_dev->cached_reg_addr = reg; 355a980e046SJonathan Cameron break; 356a980e046SJonathan Cameron case 2: 357a980e046SJonathan Cameron indio_dev->cached_reg_addr = reg; 358a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, reg, 359a980e046SJonathan Cameron val, NULL); 360a980e046SJonathan Cameron if (ret) { 361a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: write failed\n", 362a980e046SJonathan Cameron __func__); 363a980e046SJonathan Cameron return ret; 364a980e046SJonathan Cameron } 365a980e046SJonathan Cameron break; 366a980e046SJonathan Cameron default: 367a980e046SJonathan Cameron return -EINVAL; 368a980e046SJonathan Cameron } 369a980e046SJonathan Cameron 370a980e046SJonathan Cameron return count; 371a980e046SJonathan Cameron } 372a980e046SJonathan Cameron 373a980e046SJonathan Cameron static const struct file_operations iio_debugfs_reg_fops = { 3745a28c873SAxel Lin .open = simple_open, 375a980e046SJonathan Cameron .read = iio_debugfs_read_reg, 376a980e046SJonathan Cameron .write = iio_debugfs_write_reg, 377a980e046SJonathan Cameron }; 378a980e046SJonathan Cameron 379a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 380a980e046SJonathan Cameron { 381a980e046SJonathan Cameron debugfs_remove_recursive(indio_dev->debugfs_dentry); 382a980e046SJonathan Cameron } 383a980e046SJonathan Cameron 3848915aacaSGreg Kroah-Hartman static void iio_device_register_debugfs(struct iio_dev *indio_dev) 385a980e046SJonathan Cameron { 386a980e046SJonathan Cameron if (indio_dev->info->debugfs_reg_access == NULL) 3878915aacaSGreg Kroah-Hartman return; 388a980e046SJonathan Cameron 389abd5a2fbSAxel Lin if (!iio_debugfs_dentry) 3908915aacaSGreg Kroah-Hartman return; 391a980e046SJonathan Cameron 392a980e046SJonathan Cameron indio_dev->debugfs_dentry = 393a980e046SJonathan Cameron debugfs_create_dir(dev_name(&indio_dev->dev), 394a980e046SJonathan Cameron iio_debugfs_dentry); 395a980e046SJonathan Cameron 3968915aacaSGreg Kroah-Hartman debugfs_create_file("direct_reg_access", 0644, 3978915aacaSGreg Kroah-Hartman indio_dev->debugfs_dentry, indio_dev, 3988915aacaSGreg Kroah-Hartman &iio_debugfs_reg_fops); 399a980e046SJonathan Cameron } 400a980e046SJonathan Cameron #else 4018915aacaSGreg Kroah-Hartman static void iio_device_register_debugfs(struct iio_dev *indio_dev) 402a980e046SJonathan Cameron { 403a980e046SJonathan Cameron } 404a980e046SJonathan Cameron 405a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 406a980e046SJonathan Cameron { 407a980e046SJonathan Cameron } 408a980e046SJonathan Cameron #endif /* CONFIG_DEBUG_FS */ 409a980e046SJonathan Cameron 410a980e046SJonathan Cameron static ssize_t iio_read_channel_ext_info(struct device *dev, 411a980e046SJonathan Cameron struct device_attribute *attr, 412a980e046SJonathan Cameron char *buf) 413a980e046SJonathan Cameron { 414e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 415a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 416a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 417a980e046SJonathan Cameron 418a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 419a980e046SJonathan Cameron 420fc6d1139SMichael Hennerich return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf); 421a980e046SJonathan Cameron } 422a980e046SJonathan Cameron 423a980e046SJonathan Cameron static ssize_t iio_write_channel_ext_info(struct device *dev, 424a980e046SJonathan Cameron struct device_attribute *attr, 425a980e046SJonathan Cameron const char *buf, 426a980e046SJonathan Cameron size_t len) 427a980e046SJonathan Cameron { 428e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 429a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 430a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 431a980e046SJonathan Cameron 432a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 433a980e046SJonathan Cameron 434fc6d1139SMichael Hennerich return ext_info->write(indio_dev, ext_info->private, 435fc6d1139SMichael Hennerich this_attr->c, buf, len); 436a980e046SJonathan Cameron } 437a980e046SJonathan Cameron 4385212cc8aSLars-Peter Clausen ssize_t iio_enum_available_read(struct iio_dev *indio_dev, 4395212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 4405212cc8aSLars-Peter Clausen { 4415212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 4425212cc8aSLars-Peter Clausen unsigned int i; 4435212cc8aSLars-Peter Clausen size_t len = 0; 4445212cc8aSLars-Peter Clausen 4455212cc8aSLars-Peter Clausen if (!e->num_items) 4465212cc8aSLars-Peter Clausen return 0; 4475212cc8aSLars-Peter Clausen 4485212cc8aSLars-Peter Clausen for (i = 0; i < e->num_items; ++i) 44974dcd439SLars-Peter Clausen len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]); 4505212cc8aSLars-Peter Clausen 4515212cc8aSLars-Peter Clausen /* replace last space with a newline */ 4525212cc8aSLars-Peter Clausen buf[len - 1] = '\n'; 4535212cc8aSLars-Peter Clausen 4545212cc8aSLars-Peter Clausen return len; 4555212cc8aSLars-Peter Clausen } 4565212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_available_read); 4575212cc8aSLars-Peter Clausen 4585212cc8aSLars-Peter Clausen ssize_t iio_enum_read(struct iio_dev *indio_dev, 4595212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 4605212cc8aSLars-Peter Clausen { 4615212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 4625212cc8aSLars-Peter Clausen int i; 4635212cc8aSLars-Peter Clausen 4645212cc8aSLars-Peter Clausen if (!e->get) 4655212cc8aSLars-Peter Clausen return -EINVAL; 4665212cc8aSLars-Peter Clausen 4675212cc8aSLars-Peter Clausen i = e->get(indio_dev, chan); 4685212cc8aSLars-Peter Clausen if (i < 0) 4695212cc8aSLars-Peter Clausen return i; 4705212cc8aSLars-Peter Clausen else if (i >= e->num_items) 4715212cc8aSLars-Peter Clausen return -EINVAL; 4725212cc8aSLars-Peter Clausen 473598db581SKees Cook return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]); 4745212cc8aSLars-Peter Clausen } 4755212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_read); 4765212cc8aSLars-Peter Clausen 4775212cc8aSLars-Peter Clausen ssize_t iio_enum_write(struct iio_dev *indio_dev, 4785212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, const char *buf, 4795212cc8aSLars-Peter Clausen size_t len) 4805212cc8aSLars-Peter Clausen { 4815212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 4825212cc8aSLars-Peter Clausen int ret; 4835212cc8aSLars-Peter Clausen 4845212cc8aSLars-Peter Clausen if (!e->set) 4855212cc8aSLars-Peter Clausen return -EINVAL; 4865212cc8aSLars-Peter Clausen 48702e9a0ffSAndy Shevchenko ret = __sysfs_match_string(e->items, e->num_items, buf); 48802e9a0ffSAndy Shevchenko if (ret < 0) 48902e9a0ffSAndy Shevchenko return ret; 4905212cc8aSLars-Peter Clausen 49102e9a0ffSAndy Shevchenko ret = e->set(indio_dev, chan, ret); 4925212cc8aSLars-Peter Clausen return ret ? ret : len; 4935212cc8aSLars-Peter Clausen } 4945212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_write); 4955212cc8aSLars-Peter Clausen 496dfc57732SGregor Boirie static const struct iio_mount_matrix iio_mount_idmatrix = { 497dfc57732SGregor Boirie .rotation = { 498dfc57732SGregor Boirie "1", "0", "0", 499dfc57732SGregor Boirie "0", "1", "0", 500dfc57732SGregor Boirie "0", "0", "1" 501dfc57732SGregor Boirie } 502dfc57732SGregor Boirie }; 503dfc57732SGregor Boirie 504dfc57732SGregor Boirie static int iio_setup_mount_idmatrix(const struct device *dev, 505dfc57732SGregor Boirie struct iio_mount_matrix *matrix) 506dfc57732SGregor Boirie { 507dfc57732SGregor Boirie *matrix = iio_mount_idmatrix; 508dfc57732SGregor Boirie dev_info(dev, "mounting matrix not found: using identity...\n"); 509dfc57732SGregor Boirie return 0; 510dfc57732SGregor Boirie } 511dfc57732SGregor Boirie 512dfc57732SGregor Boirie ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv, 513dfc57732SGregor Boirie const struct iio_chan_spec *chan, char *buf) 514dfc57732SGregor Boirie { 515dfc57732SGregor Boirie const struct iio_mount_matrix *mtx = ((iio_get_mount_matrix_t *) 516dfc57732SGregor Boirie priv)(indio_dev, chan); 517dfc57732SGregor Boirie 518dfc57732SGregor Boirie if (IS_ERR(mtx)) 519dfc57732SGregor Boirie return PTR_ERR(mtx); 520dfc57732SGregor Boirie 521dfc57732SGregor Boirie if (!mtx) 522dfc57732SGregor Boirie mtx = &iio_mount_idmatrix; 523dfc57732SGregor Boirie 524dfc57732SGregor Boirie return snprintf(buf, PAGE_SIZE, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n", 525dfc57732SGregor Boirie mtx->rotation[0], mtx->rotation[1], mtx->rotation[2], 526dfc57732SGregor Boirie mtx->rotation[3], mtx->rotation[4], mtx->rotation[5], 527dfc57732SGregor Boirie mtx->rotation[6], mtx->rotation[7], mtx->rotation[8]); 528dfc57732SGregor Boirie } 529dfc57732SGregor Boirie EXPORT_SYMBOL_GPL(iio_show_mount_matrix); 530dfc57732SGregor Boirie 531dfc57732SGregor Boirie /** 532fb158971SAndy Shevchenko * iio_read_mount_matrix() - retrieve iio device mounting matrix from 533fb158971SAndy Shevchenko * device "mount-matrix" property 534dfc57732SGregor Boirie * @dev: device the mounting matrix property is assigned to 535dfc57732SGregor Boirie * @propname: device specific mounting matrix property name 536dfc57732SGregor Boirie * @matrix: where to store retrieved matrix 537dfc57732SGregor Boirie * 538dfc57732SGregor Boirie * If device is assigned no mounting matrix property, a default 3x3 identity 539dfc57732SGregor Boirie * matrix will be filled in. 540dfc57732SGregor Boirie * 541dfc57732SGregor Boirie * Return: 0 if success, or a negative error code on failure. 542dfc57732SGregor Boirie */ 543fb158971SAndy Shevchenko int iio_read_mount_matrix(struct device *dev, const char *propname, 544dfc57732SGregor Boirie struct iio_mount_matrix *matrix) 545dfc57732SGregor Boirie { 546fb158971SAndy Shevchenko size_t len = ARRAY_SIZE(iio_mount_idmatrix.rotation); 547fb158971SAndy Shevchenko int err; 548dfc57732SGregor Boirie 549fb158971SAndy Shevchenko err = device_property_read_string_array(dev, propname, 550fb158971SAndy Shevchenko matrix->rotation, len); 551fb158971SAndy Shevchenko if (err == len) 552dfc57732SGregor Boirie return 0; 553dfc57732SGregor Boirie 554dfc57732SGregor Boirie if (err >= 0) 555dfc57732SGregor Boirie /* Invalid number of matrix entries. */ 556dfc57732SGregor Boirie return -EINVAL; 557dfc57732SGregor Boirie 558dfc57732SGregor Boirie if (err != -EINVAL) 559dfc57732SGregor Boirie /* Invalid matrix declaration format. */ 560dfc57732SGregor Boirie return err; 561dfc57732SGregor Boirie 562dfc57732SGregor Boirie /* Matrix was not declared at all: fallback to identity. */ 563dfc57732SGregor Boirie return iio_setup_mount_idmatrix(dev, matrix); 564dfc57732SGregor Boirie } 565fb158971SAndy Shevchenko EXPORT_SYMBOL(iio_read_mount_matrix); 566dfc57732SGregor Boirie 56751239600SJonathan Cameron static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type, 56851239600SJonathan Cameron int size, const int *vals) 56951239600SJonathan Cameron { 57051239600SJonathan Cameron unsigned long long tmp; 57151239600SJonathan Cameron int tmp0, tmp1; 57251239600SJonathan Cameron bool scale_db = false; 57351239600SJonathan Cameron 57451239600SJonathan Cameron switch (type) { 57551239600SJonathan Cameron case IIO_VAL_INT: 57635a4eeb0STakashi Iwai return scnprintf(buf, len, "%d", vals[0]); 57751239600SJonathan Cameron case IIO_VAL_INT_PLUS_MICRO_DB: 57851239600SJonathan Cameron scale_db = true; 5799d793c1aSGustavo A. R. Silva /* fall through */ 58051239600SJonathan Cameron case IIO_VAL_INT_PLUS_MICRO: 58151239600SJonathan Cameron if (vals[1] < 0) 58235a4eeb0STakashi Iwai return scnprintf(buf, len, "-%d.%06u%s", abs(vals[0]), 58351239600SJonathan Cameron -vals[1], scale_db ? " dB" : ""); 58451239600SJonathan Cameron else 58535a4eeb0STakashi Iwai return scnprintf(buf, len, "%d.%06u%s", vals[0], vals[1], 58651239600SJonathan Cameron scale_db ? " dB" : ""); 58751239600SJonathan Cameron case IIO_VAL_INT_PLUS_NANO: 58851239600SJonathan Cameron if (vals[1] < 0) 58935a4eeb0STakashi Iwai return scnprintf(buf, len, "-%d.%09u", abs(vals[0]), 59051239600SJonathan Cameron -vals[1]); 59151239600SJonathan Cameron else 59235a4eeb0STakashi Iwai return scnprintf(buf, len, "%d.%09u", vals[0], vals[1]); 59351239600SJonathan Cameron case IIO_VAL_FRACTIONAL: 59451239600SJonathan Cameron tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]); 59551239600SJonathan Cameron tmp1 = vals[1]; 59651239600SJonathan Cameron tmp0 = (int)div_s64_rem(tmp, 1000000000, &tmp1); 59735a4eeb0STakashi Iwai return scnprintf(buf, len, "%d.%09u", tmp0, abs(tmp1)); 59851239600SJonathan Cameron case IIO_VAL_FRACTIONAL_LOG2: 5997fd6592dSNikolaus Schulz tmp = shift_right((s64)vals[0] * 1000000000LL, vals[1]); 6007fd6592dSNikolaus Schulz tmp0 = (int)div_s64_rem(tmp, 1000000000LL, &tmp1); 60135a4eeb0STakashi Iwai return scnprintf(buf, len, "%d.%09u", tmp0, abs(tmp1)); 60251239600SJonathan Cameron case IIO_VAL_INT_MULTIPLE: 60351239600SJonathan Cameron { 60451239600SJonathan Cameron int i; 60551239600SJonathan Cameron int l = 0; 60651239600SJonathan Cameron 60751239600SJonathan Cameron for (i = 0; i < size; ++i) { 60835a4eeb0STakashi Iwai l += scnprintf(&buf[l], len - l, "%d ", vals[i]); 60951239600SJonathan Cameron if (l >= len) 61051239600SJonathan Cameron break; 61151239600SJonathan Cameron } 61251239600SJonathan Cameron return l; 61351239600SJonathan Cameron } 6148cb34036SAndrea Merello case IIO_VAL_CHAR: 61535a4eeb0STakashi Iwai return scnprintf(buf, len, "%c", (char)vals[0]); 61651239600SJonathan Cameron default: 61751239600SJonathan Cameron return 0; 61851239600SJonathan Cameron } 61951239600SJonathan Cameron } 62051239600SJonathan Cameron 6213661f3f5SLars-Peter Clausen /** 6223661f3f5SLars-Peter Clausen * iio_format_value() - Formats a IIO value into its string representation 6232498dcf6SCristina Opriceana * @buf: The buffer to which the formatted value gets written 62451239600SJonathan Cameron * which is assumed to be big enough (i.e. PAGE_SIZE). 625c175cb7cSRandy Dunlap * @type: One of the IIO_VAL_* constants. This decides how the val 6262498dcf6SCristina Opriceana * and val2 parameters are formatted. 6272498dcf6SCristina Opriceana * @size: Number of IIO value entries contained in vals 6282498dcf6SCristina Opriceana * @vals: Pointer to the values, exact meaning depends on the 6292498dcf6SCristina Opriceana * type parameter. 6302498dcf6SCristina Opriceana * 6312498dcf6SCristina Opriceana * Return: 0 by default, a negative number on failure or the 6322498dcf6SCristina Opriceana * total number of characters written for a type that belongs 633c175cb7cSRandy Dunlap * to the IIO_VAL_* constant. 6343661f3f5SLars-Peter Clausen */ 6359fbfb4b3SSrinivas Pandruvada ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals) 636a980e046SJonathan Cameron { 63751239600SJonathan Cameron ssize_t len; 638a980e046SJonathan Cameron 63951239600SJonathan Cameron len = __iio_format_value(buf, PAGE_SIZE, type, size, vals); 64051239600SJonathan Cameron if (len >= PAGE_SIZE - 1) 64151239600SJonathan Cameron return -EFBIG; 6429fbfb4b3SSrinivas Pandruvada 64351239600SJonathan Cameron return len + sprintf(buf + len, "\n"); 64467eedba3SMichael Hennerich } 6457d2c2acaSAndrew F. Davis EXPORT_SYMBOL_GPL(iio_format_value); 646a980e046SJonathan Cameron 6473661f3f5SLars-Peter Clausen static ssize_t iio_read_channel_info(struct device *dev, 6483661f3f5SLars-Peter Clausen struct device_attribute *attr, 6493661f3f5SLars-Peter Clausen char *buf) 6503661f3f5SLars-Peter Clausen { 6513661f3f5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 6523661f3f5SLars-Peter Clausen struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 6539fbfb4b3SSrinivas Pandruvada int vals[INDIO_MAX_RAW_ELEMENTS]; 6549fbfb4b3SSrinivas Pandruvada int ret; 6559fbfb4b3SSrinivas Pandruvada int val_len = 2; 6569fbfb4b3SSrinivas Pandruvada 6579fbfb4b3SSrinivas Pandruvada if (indio_dev->info->read_raw_multi) 6589fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c, 6599fbfb4b3SSrinivas Pandruvada INDIO_MAX_RAW_ELEMENTS, 6609fbfb4b3SSrinivas Pandruvada vals, &val_len, 6619fbfb4b3SSrinivas Pandruvada this_attr->address); 6629fbfb4b3SSrinivas Pandruvada else 6639fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw(indio_dev, this_attr->c, 6649fbfb4b3SSrinivas Pandruvada &vals[0], &vals[1], this_attr->address); 6653661f3f5SLars-Peter Clausen 6663661f3f5SLars-Peter Clausen if (ret < 0) 6673661f3f5SLars-Peter Clausen return ret; 6683661f3f5SLars-Peter Clausen 6699fbfb4b3SSrinivas Pandruvada return iio_format_value(buf, ret, val_len, vals); 6703661f3f5SLars-Peter Clausen } 6713661f3f5SLars-Peter Clausen 67251239600SJonathan Cameron static ssize_t iio_format_avail_list(char *buf, const int *vals, 67351239600SJonathan Cameron int type, int length) 67451239600SJonathan Cameron { 67551239600SJonathan Cameron int i; 67651239600SJonathan Cameron ssize_t len = 0; 67751239600SJonathan Cameron 67851239600SJonathan Cameron switch (type) { 67951239600SJonathan Cameron case IIO_VAL_INT: 68051239600SJonathan Cameron for (i = 0; i < length; i++) { 68151239600SJonathan Cameron len += __iio_format_value(buf + len, PAGE_SIZE - len, 68251239600SJonathan Cameron type, 1, &vals[i]); 68351239600SJonathan Cameron if (len >= PAGE_SIZE) 68451239600SJonathan Cameron return -EFBIG; 68551239600SJonathan Cameron if (i < length - 1) 68635a4eeb0STakashi Iwai len += scnprintf(buf + len, PAGE_SIZE - len, 68751239600SJonathan Cameron " "); 68851239600SJonathan Cameron else 68935a4eeb0STakashi Iwai len += scnprintf(buf + len, PAGE_SIZE - len, 69051239600SJonathan Cameron "\n"); 69151239600SJonathan Cameron if (len >= PAGE_SIZE) 69251239600SJonathan Cameron return -EFBIG; 69351239600SJonathan Cameron } 69451239600SJonathan Cameron break; 69551239600SJonathan Cameron default: 69651239600SJonathan Cameron for (i = 0; i < length / 2; i++) { 69751239600SJonathan Cameron len += __iio_format_value(buf + len, PAGE_SIZE - len, 69851239600SJonathan Cameron type, 2, &vals[i * 2]); 69951239600SJonathan Cameron if (len >= PAGE_SIZE) 70051239600SJonathan Cameron return -EFBIG; 70151239600SJonathan Cameron if (i < length / 2 - 1) 70235a4eeb0STakashi Iwai len += scnprintf(buf + len, PAGE_SIZE - len, 70351239600SJonathan Cameron " "); 70451239600SJonathan Cameron else 70535a4eeb0STakashi Iwai len += scnprintf(buf + len, PAGE_SIZE - len, 70651239600SJonathan Cameron "\n"); 70751239600SJonathan Cameron if (len >= PAGE_SIZE) 70851239600SJonathan Cameron return -EFBIG; 70951239600SJonathan Cameron } 71051239600SJonathan Cameron } 71151239600SJonathan Cameron 71251239600SJonathan Cameron return len; 71351239600SJonathan Cameron } 71451239600SJonathan Cameron 71551239600SJonathan Cameron static ssize_t iio_format_avail_range(char *buf, const int *vals, int type) 71651239600SJonathan Cameron { 71751239600SJonathan Cameron int i; 71851239600SJonathan Cameron ssize_t len; 71951239600SJonathan Cameron 72051239600SJonathan Cameron len = snprintf(buf, PAGE_SIZE, "["); 72151239600SJonathan Cameron switch (type) { 72251239600SJonathan Cameron case IIO_VAL_INT: 72351239600SJonathan Cameron for (i = 0; i < 3; i++) { 72451239600SJonathan Cameron len += __iio_format_value(buf + len, PAGE_SIZE - len, 72551239600SJonathan Cameron type, 1, &vals[i]); 72651239600SJonathan Cameron if (len >= PAGE_SIZE) 72751239600SJonathan Cameron return -EFBIG; 72851239600SJonathan Cameron if (i < 2) 72935a4eeb0STakashi Iwai len += scnprintf(buf + len, PAGE_SIZE - len, 73051239600SJonathan Cameron " "); 73151239600SJonathan Cameron else 73235a4eeb0STakashi Iwai len += scnprintf(buf + len, PAGE_SIZE - len, 73351239600SJonathan Cameron "]\n"); 73451239600SJonathan Cameron if (len >= PAGE_SIZE) 73551239600SJonathan Cameron return -EFBIG; 73651239600SJonathan Cameron } 73751239600SJonathan Cameron break; 73851239600SJonathan Cameron default: 73951239600SJonathan Cameron for (i = 0; i < 3; i++) { 74051239600SJonathan Cameron len += __iio_format_value(buf + len, PAGE_SIZE - len, 74151239600SJonathan Cameron type, 2, &vals[i * 2]); 74251239600SJonathan Cameron if (len >= PAGE_SIZE) 74351239600SJonathan Cameron return -EFBIG; 74451239600SJonathan Cameron if (i < 2) 74535a4eeb0STakashi Iwai len += scnprintf(buf + len, PAGE_SIZE - len, 74651239600SJonathan Cameron " "); 74751239600SJonathan Cameron else 74835a4eeb0STakashi Iwai len += scnprintf(buf + len, PAGE_SIZE - len, 74951239600SJonathan Cameron "]\n"); 75051239600SJonathan Cameron if (len >= PAGE_SIZE) 75151239600SJonathan Cameron return -EFBIG; 75251239600SJonathan Cameron } 75351239600SJonathan Cameron } 75451239600SJonathan Cameron 75551239600SJonathan Cameron return len; 75651239600SJonathan Cameron } 75751239600SJonathan Cameron 75851239600SJonathan Cameron static ssize_t iio_read_channel_info_avail(struct device *dev, 75951239600SJonathan Cameron struct device_attribute *attr, 76051239600SJonathan Cameron char *buf) 76151239600SJonathan Cameron { 76251239600SJonathan Cameron struct iio_dev *indio_dev = dev_to_iio_dev(dev); 76351239600SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 76451239600SJonathan Cameron const int *vals; 76551239600SJonathan Cameron int ret; 76651239600SJonathan Cameron int length; 76751239600SJonathan Cameron int type; 76851239600SJonathan Cameron 76951239600SJonathan Cameron ret = indio_dev->info->read_avail(indio_dev, this_attr->c, 77051239600SJonathan Cameron &vals, &type, &length, 77151239600SJonathan Cameron this_attr->address); 77251239600SJonathan Cameron 77351239600SJonathan Cameron if (ret < 0) 77451239600SJonathan Cameron return ret; 77551239600SJonathan Cameron switch (ret) { 77651239600SJonathan Cameron case IIO_AVAIL_LIST: 77751239600SJonathan Cameron return iio_format_avail_list(buf, vals, type, length); 77851239600SJonathan Cameron case IIO_AVAIL_RANGE: 77951239600SJonathan Cameron return iio_format_avail_range(buf, vals, type); 78051239600SJonathan Cameron default: 78151239600SJonathan Cameron return -EINVAL; 78251239600SJonathan Cameron } 78351239600SJonathan Cameron } 78451239600SJonathan Cameron 7856807d721SLars-Peter Clausen /** 786b8528224SBeniamin Bia * __iio_str_to_fixpoint() - Parse a fixed-point number from a string 7876807d721SLars-Peter Clausen * @str: The string to parse 7886807d721SLars-Peter Clausen * @fract_mult: Multiplier for the first decimal place, should be a power of 10 7896807d721SLars-Peter Clausen * @integer: The integer part of the number 7906807d721SLars-Peter Clausen * @fract: The fractional part of the number 791b8528224SBeniamin Bia * @scale_db: True if this should parse as dB 7926807d721SLars-Peter Clausen * 7936807d721SLars-Peter Clausen * Returns 0 on success, or a negative error code if the string could not be 7946807d721SLars-Peter Clausen * parsed. 7956807d721SLars-Peter Clausen */ 796b8528224SBeniamin Bia static int __iio_str_to_fixpoint(const char *str, int fract_mult, 797b8528224SBeniamin Bia int *integer, int *fract, bool scale_db) 7986807d721SLars-Peter Clausen { 7996807d721SLars-Peter Clausen int i = 0, f = 0; 8006807d721SLars-Peter Clausen bool integer_part = true, negative = false; 8016807d721SLars-Peter Clausen 802f47dff32SSean Nyekjaer if (fract_mult == 0) { 803f47dff32SSean Nyekjaer *fract = 0; 804f47dff32SSean Nyekjaer 805f47dff32SSean Nyekjaer return kstrtoint(str, 0, integer); 806f47dff32SSean Nyekjaer } 807f47dff32SSean Nyekjaer 8086807d721SLars-Peter Clausen if (str[0] == '-') { 8096807d721SLars-Peter Clausen negative = true; 8106807d721SLars-Peter Clausen str++; 8116807d721SLars-Peter Clausen } else if (str[0] == '+') { 8126807d721SLars-Peter Clausen str++; 8136807d721SLars-Peter Clausen } 8146807d721SLars-Peter Clausen 8156807d721SLars-Peter Clausen while (*str) { 8166807d721SLars-Peter Clausen if ('0' <= *str && *str <= '9') { 8176807d721SLars-Peter Clausen if (integer_part) { 8186807d721SLars-Peter Clausen i = i * 10 + *str - '0'; 8196807d721SLars-Peter Clausen } else { 8206807d721SLars-Peter Clausen f += fract_mult * (*str - '0'); 8216807d721SLars-Peter Clausen fract_mult /= 10; 8226807d721SLars-Peter Clausen } 8236807d721SLars-Peter Clausen } else if (*str == '\n') { 8246807d721SLars-Peter Clausen if (*(str + 1) == '\0') 8256807d721SLars-Peter Clausen break; 8266807d721SLars-Peter Clausen else 8276807d721SLars-Peter Clausen return -EINVAL; 828b8528224SBeniamin Bia } else if (!strncmp(str, " dB", sizeof(" dB") - 1) && scale_db) { 829b8528224SBeniamin Bia /* Ignore the dB suffix */ 830b8528224SBeniamin Bia str += sizeof(" dB") - 1; 831b8528224SBeniamin Bia continue; 832b8528224SBeniamin Bia } else if (!strncmp(str, "dB", sizeof("dB") - 1) && scale_db) { 833b8528224SBeniamin Bia /* Ignore the dB suffix */ 834b8528224SBeniamin Bia str += sizeof("dB") - 1; 835b8528224SBeniamin Bia continue; 8366807d721SLars-Peter Clausen } else if (*str == '.' && integer_part) { 8376807d721SLars-Peter Clausen integer_part = false; 8386807d721SLars-Peter Clausen } else { 8396807d721SLars-Peter Clausen return -EINVAL; 8406807d721SLars-Peter Clausen } 8416807d721SLars-Peter Clausen str++; 8426807d721SLars-Peter Clausen } 8436807d721SLars-Peter Clausen 8446807d721SLars-Peter Clausen if (negative) { 8456807d721SLars-Peter Clausen if (i) 8466807d721SLars-Peter Clausen i = -i; 8476807d721SLars-Peter Clausen else 8486807d721SLars-Peter Clausen f = -f; 8496807d721SLars-Peter Clausen } 8506807d721SLars-Peter Clausen 8516807d721SLars-Peter Clausen *integer = i; 8526807d721SLars-Peter Clausen *fract = f; 8536807d721SLars-Peter Clausen 8546807d721SLars-Peter Clausen return 0; 8556807d721SLars-Peter Clausen } 856b8528224SBeniamin Bia 857b8528224SBeniamin Bia /** 858b8528224SBeniamin Bia * iio_str_to_fixpoint() - Parse a fixed-point number from a string 859b8528224SBeniamin Bia * @str: The string to parse 860b8528224SBeniamin Bia * @fract_mult: Multiplier for the first decimal place, should be a power of 10 861b8528224SBeniamin Bia * @integer: The integer part of the number 862b8528224SBeniamin Bia * @fract: The fractional part of the number 863b8528224SBeniamin Bia * 864b8528224SBeniamin Bia * Returns 0 on success, or a negative error code if the string could not be 865b8528224SBeniamin Bia * parsed. 866b8528224SBeniamin Bia */ 867b8528224SBeniamin Bia int iio_str_to_fixpoint(const char *str, int fract_mult, 868b8528224SBeniamin Bia int *integer, int *fract) 869b8528224SBeniamin Bia { 870b8528224SBeniamin Bia return __iio_str_to_fixpoint(str, fract_mult, integer, fract, false); 871b8528224SBeniamin Bia } 8726807d721SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_str_to_fixpoint); 8736807d721SLars-Peter Clausen 874a980e046SJonathan Cameron static ssize_t iio_write_channel_info(struct device *dev, 875a980e046SJonathan Cameron struct device_attribute *attr, 876a980e046SJonathan Cameron const char *buf, 877a980e046SJonathan Cameron size_t len) 878a980e046SJonathan Cameron { 879e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 880a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 8816807d721SLars-Peter Clausen int ret, fract_mult = 100000; 8828cb34036SAndrea Merello int integer, fract = 0; 8838cb34036SAndrea Merello bool is_char = false; 884b8528224SBeniamin Bia bool scale_db = false; 885a980e046SJonathan Cameron 886a980e046SJonathan Cameron /* Assumes decimal - precision based on number of digits */ 887a980e046SJonathan Cameron if (!indio_dev->info->write_raw) 888a980e046SJonathan Cameron return -EINVAL; 889a980e046SJonathan Cameron 890a980e046SJonathan Cameron if (indio_dev->info->write_raw_get_fmt) 891a980e046SJonathan Cameron switch (indio_dev->info->write_raw_get_fmt(indio_dev, 892a980e046SJonathan Cameron this_attr->c, this_attr->address)) { 893f47dff32SSean Nyekjaer case IIO_VAL_INT: 894f47dff32SSean Nyekjaer fract_mult = 0; 895f47dff32SSean Nyekjaer break; 896b8528224SBeniamin Bia case IIO_VAL_INT_PLUS_MICRO_DB: 897b8528224SBeniamin Bia scale_db = true; 898b8528224SBeniamin Bia /* fall through */ 899a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_MICRO: 900a980e046SJonathan Cameron fract_mult = 100000; 901a980e046SJonathan Cameron break; 902a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_NANO: 903a980e046SJonathan Cameron fract_mult = 100000000; 904a980e046SJonathan Cameron break; 9058cb34036SAndrea Merello case IIO_VAL_CHAR: 9068cb34036SAndrea Merello is_char = true; 9078cb34036SAndrea Merello break; 908a980e046SJonathan Cameron default: 909a980e046SJonathan Cameron return -EINVAL; 910a980e046SJonathan Cameron } 911a980e046SJonathan Cameron 9128cb34036SAndrea Merello if (is_char) { 9138cb34036SAndrea Merello char ch; 9148cb34036SAndrea Merello 9158cb34036SAndrea Merello if (sscanf(buf, "%c", &ch) != 1) 9168cb34036SAndrea Merello return -EINVAL; 9178cb34036SAndrea Merello integer = ch; 9188cb34036SAndrea Merello } else { 919b8528224SBeniamin Bia ret = __iio_str_to_fixpoint(buf, fract_mult, &integer, &fract, 920b8528224SBeniamin Bia scale_db); 921b8528224SBeniamin Bia if (ret) 922b8528224SBeniamin Bia return ret; 9235403f280SMircea Caprioru } 924a980e046SJonathan Cameron 925a980e046SJonathan Cameron ret = indio_dev->info->write_raw(indio_dev, this_attr->c, 926a980e046SJonathan Cameron integer, fract, this_attr->address); 927a980e046SJonathan Cameron if (ret) 928a980e046SJonathan Cameron return ret; 929a980e046SJonathan Cameron 930a980e046SJonathan Cameron return len; 931a980e046SJonathan Cameron } 932a980e046SJonathan Cameron 933a980e046SJonathan Cameron static 934a980e046SJonathan Cameron int __iio_device_attr_init(struct device_attribute *dev_attr, 935a980e046SJonathan Cameron const char *postfix, 936a980e046SJonathan Cameron struct iio_chan_spec const *chan, 937a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 938a980e046SJonathan Cameron struct device_attribute *attr, 939a980e046SJonathan Cameron char *buf), 940a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 941a980e046SJonathan Cameron struct device_attribute *attr, 942a980e046SJonathan Cameron const char *buf, 943a980e046SJonathan Cameron size_t len), 9443704432fSJonathan Cameron enum iio_shared_by shared_by) 945a980e046SJonathan Cameron { 9463704432fSJonathan Cameron int ret = 0; 9477bbcf7e1SLars-Peter Clausen char *name = NULL; 9483704432fSJonathan Cameron char *full_postfix; 949a980e046SJonathan Cameron sysfs_attr_init(&dev_attr->attr); 950a980e046SJonathan Cameron 951a980e046SJonathan Cameron /* Build up postfix of <extend_name>_<modifier>_postfix */ 9523704432fSJonathan Cameron if (chan->modified && (shared_by == IIO_SEPARATE)) { 953a980e046SJonathan Cameron if (chan->extend_name) 954a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", 955a980e046SJonathan Cameron iio_modifier_names[chan 956a980e046SJonathan Cameron ->channel2], 957a980e046SJonathan Cameron chan->extend_name, 958a980e046SJonathan Cameron postfix); 959a980e046SJonathan Cameron else 960a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s", 961a980e046SJonathan Cameron iio_modifier_names[chan 962a980e046SJonathan Cameron ->channel2], 963a980e046SJonathan Cameron postfix); 964a980e046SJonathan Cameron } else { 96577bfa8baSLars-Peter Clausen if (chan->extend_name == NULL || shared_by != IIO_SEPARATE) 966a980e046SJonathan Cameron full_postfix = kstrdup(postfix, GFP_KERNEL); 967a980e046SJonathan Cameron else 968a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, 969a980e046SJonathan Cameron "%s_%s", 970a980e046SJonathan Cameron chan->extend_name, 971a980e046SJonathan Cameron postfix); 972a980e046SJonathan Cameron } 9733704432fSJonathan Cameron if (full_postfix == NULL) 9743704432fSJonathan Cameron return -ENOMEM; 975a980e046SJonathan Cameron 976a980e046SJonathan Cameron if (chan->differential) { /* Differential can not have modifier */ 9773704432fSJonathan Cameron switch (shared_by) { 978c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 9797bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 980c006ec83SJonathan Cameron break; 981c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 9827bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 983c006ec83SJonathan Cameron iio_direction[chan->output], 984c006ec83SJonathan Cameron full_postfix); 985c006ec83SJonathan Cameron break; 9863704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 9877bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s", 988a980e046SJonathan Cameron iio_direction[chan->output], 989a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 990a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 991a980e046SJonathan Cameron full_postfix); 9923704432fSJonathan Cameron break; 9933704432fSJonathan Cameron case IIO_SEPARATE: 9943704432fSJonathan Cameron if (!chan->indexed) { 995231bfe53SDan Carpenter WARN(1, "Differential channels must be indexed\n"); 9963704432fSJonathan Cameron ret = -EINVAL; 9973704432fSJonathan Cameron goto error_free_full_postfix; 9983704432fSJonathan Cameron } 9997bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, 10003704432fSJonathan Cameron "%s_%s%d-%s%d_%s", 1001a980e046SJonathan Cameron iio_direction[chan->output], 1002a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1003a980e046SJonathan Cameron chan->channel, 1004a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1005a980e046SJonathan Cameron chan->channel2, 1006a980e046SJonathan Cameron full_postfix); 10073704432fSJonathan Cameron break; 1008a980e046SJonathan Cameron } 1009a980e046SJonathan Cameron } else { /* Single ended */ 10103704432fSJonathan Cameron switch (shared_by) { 1011c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 10127bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 1013c006ec83SJonathan Cameron break; 1014c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 10157bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 1016c006ec83SJonathan Cameron iio_direction[chan->output], 1017c006ec83SJonathan Cameron full_postfix); 1018c006ec83SJonathan Cameron break; 10193704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 10207bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 1021a980e046SJonathan Cameron iio_direction[chan->output], 1022a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1023a980e046SJonathan Cameron full_postfix); 10243704432fSJonathan Cameron break; 10253704432fSJonathan Cameron 10263704432fSJonathan Cameron case IIO_SEPARATE: 10273704432fSJonathan Cameron if (chan->indexed) 10287bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s%d_%s", 1029a980e046SJonathan Cameron iio_direction[chan->output], 1030a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1031a980e046SJonathan Cameron chan->channel, 1032a980e046SJonathan Cameron full_postfix); 1033a980e046SJonathan Cameron else 10347bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 1035a980e046SJonathan Cameron iio_direction[chan->output], 1036a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1037a980e046SJonathan Cameron full_postfix); 10383704432fSJonathan Cameron break; 10393704432fSJonathan Cameron } 1040a980e046SJonathan Cameron } 10417bbcf7e1SLars-Peter Clausen if (name == NULL) { 1042a980e046SJonathan Cameron ret = -ENOMEM; 1043a980e046SJonathan Cameron goto error_free_full_postfix; 1044a980e046SJonathan Cameron } 10457bbcf7e1SLars-Peter Clausen dev_attr->attr.name = name; 1046a980e046SJonathan Cameron 1047a980e046SJonathan Cameron if (readfunc) { 1048a980e046SJonathan Cameron dev_attr->attr.mode |= S_IRUGO; 1049a980e046SJonathan Cameron dev_attr->show = readfunc; 1050a980e046SJonathan Cameron } 1051a980e046SJonathan Cameron 1052a980e046SJonathan Cameron if (writefunc) { 1053a980e046SJonathan Cameron dev_attr->attr.mode |= S_IWUSR; 1054a980e046SJonathan Cameron dev_attr->store = writefunc; 1055a980e046SJonathan Cameron } 10567bbcf7e1SLars-Peter Clausen 1057a980e046SJonathan Cameron error_free_full_postfix: 1058a980e046SJonathan Cameron kfree(full_postfix); 10593704432fSJonathan Cameron 1060a980e046SJonathan Cameron return ret; 1061a980e046SJonathan Cameron } 1062a980e046SJonathan Cameron 1063a980e046SJonathan Cameron static void __iio_device_attr_deinit(struct device_attribute *dev_attr) 1064a980e046SJonathan Cameron { 1065a980e046SJonathan Cameron kfree(dev_attr->attr.name); 1066a980e046SJonathan Cameron } 1067a980e046SJonathan Cameron 1068a980e046SJonathan Cameron int __iio_add_chan_devattr(const char *postfix, 1069a980e046SJonathan Cameron struct iio_chan_spec const *chan, 1070a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 1071a980e046SJonathan Cameron struct device_attribute *attr, 1072a980e046SJonathan Cameron char *buf), 1073a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 1074a980e046SJonathan Cameron struct device_attribute *attr, 1075a980e046SJonathan Cameron const char *buf, 1076a980e046SJonathan Cameron size_t len), 1077a980e046SJonathan Cameron u64 mask, 10783704432fSJonathan Cameron enum iio_shared_by shared_by, 1079a980e046SJonathan Cameron struct device *dev, 1080a980e046SJonathan Cameron struct list_head *attr_list) 1081a980e046SJonathan Cameron { 1082a980e046SJonathan Cameron int ret; 1083a980e046SJonathan Cameron struct iio_dev_attr *iio_attr, *t; 1084a980e046SJonathan Cameron 1085670c1103SSachin Kamat iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL); 108692825ff9SHartmut Knaack if (iio_attr == NULL) 108792825ff9SHartmut Knaack return -ENOMEM; 1088a980e046SJonathan Cameron ret = __iio_device_attr_init(&iio_attr->dev_attr, 1089a980e046SJonathan Cameron postfix, chan, 10903704432fSJonathan Cameron readfunc, writefunc, shared_by); 1091a980e046SJonathan Cameron if (ret) 1092a980e046SJonathan Cameron goto error_iio_dev_attr_free; 1093a980e046SJonathan Cameron iio_attr->c = chan; 1094a980e046SJonathan Cameron iio_attr->address = mask; 1095a980e046SJonathan Cameron list_for_each_entry(t, attr_list, l) 1096a980e046SJonathan Cameron if (strcmp(t->dev_attr.attr.name, 1097a980e046SJonathan Cameron iio_attr->dev_attr.attr.name) == 0) { 10983704432fSJonathan Cameron if (shared_by == IIO_SEPARATE) 1099a980e046SJonathan Cameron dev_err(dev, "tried to double register : %s\n", 1100a980e046SJonathan Cameron t->dev_attr.attr.name); 1101a980e046SJonathan Cameron ret = -EBUSY; 1102a980e046SJonathan Cameron goto error_device_attr_deinit; 1103a980e046SJonathan Cameron } 1104a980e046SJonathan Cameron list_add(&iio_attr->l, attr_list); 1105a980e046SJonathan Cameron 1106a980e046SJonathan Cameron return 0; 1107a980e046SJonathan Cameron 1108a980e046SJonathan Cameron error_device_attr_deinit: 1109a980e046SJonathan Cameron __iio_device_attr_deinit(&iio_attr->dev_attr); 1110a980e046SJonathan Cameron error_iio_dev_attr_free: 1111a980e046SJonathan Cameron kfree(iio_attr); 1112a980e046SJonathan Cameron return ret; 1113a980e046SJonathan Cameron } 1114a980e046SJonathan Cameron 11153704432fSJonathan Cameron static int iio_device_add_info_mask_type(struct iio_dev *indio_dev, 11163704432fSJonathan Cameron struct iio_chan_spec const *chan, 11173704432fSJonathan Cameron enum iio_shared_by shared_by, 11183704432fSJonathan Cameron const long *infomask) 11193704432fSJonathan Cameron { 11203704432fSJonathan Cameron int i, ret, attrcount = 0; 11213704432fSJonathan Cameron 11221016d567SOrson Zhai for_each_set_bit(i, infomask, sizeof(*infomask)*8) { 1123ef4b4856SJonathan Cameron if (i >= ARRAY_SIZE(iio_chan_info_postfix)) 1124ef4b4856SJonathan Cameron return -EINVAL; 11253704432fSJonathan Cameron ret = __iio_add_chan_devattr(iio_chan_info_postfix[i], 11263704432fSJonathan Cameron chan, 11273704432fSJonathan Cameron &iio_read_channel_info, 11283704432fSJonathan Cameron &iio_write_channel_info, 11293704432fSJonathan Cameron i, 11303704432fSJonathan Cameron shared_by, 11313704432fSJonathan Cameron &indio_dev->dev, 11323704432fSJonathan Cameron &indio_dev->channel_attr_list); 11333704432fSJonathan Cameron if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) 11343704432fSJonathan Cameron continue; 11353704432fSJonathan Cameron else if (ret < 0) 11363704432fSJonathan Cameron return ret; 11373704432fSJonathan Cameron attrcount++; 11383704432fSJonathan Cameron } 11393704432fSJonathan Cameron 11403704432fSJonathan Cameron return attrcount; 11413704432fSJonathan Cameron } 11423704432fSJonathan Cameron 114351239600SJonathan Cameron static int iio_device_add_info_mask_type_avail(struct iio_dev *indio_dev, 114451239600SJonathan Cameron struct iio_chan_spec const *chan, 114551239600SJonathan Cameron enum iio_shared_by shared_by, 114651239600SJonathan Cameron const long *infomask) 114751239600SJonathan Cameron { 114851239600SJonathan Cameron int i, ret, attrcount = 0; 114951239600SJonathan Cameron char *avail_postfix; 115051239600SJonathan Cameron 11511016d567SOrson Zhai for_each_set_bit(i, infomask, sizeof(*infomask) * 8) { 1152936d3e53SYoung Xiao if (i >= ARRAY_SIZE(iio_chan_info_postfix)) 1153936d3e53SYoung Xiao return -EINVAL; 115451239600SJonathan Cameron avail_postfix = kasprintf(GFP_KERNEL, 115551239600SJonathan Cameron "%s_available", 115651239600SJonathan Cameron iio_chan_info_postfix[i]); 115751239600SJonathan Cameron if (!avail_postfix) 115851239600SJonathan Cameron return -ENOMEM; 115951239600SJonathan Cameron 116051239600SJonathan Cameron ret = __iio_add_chan_devattr(avail_postfix, 116151239600SJonathan Cameron chan, 116251239600SJonathan Cameron &iio_read_channel_info_avail, 116351239600SJonathan Cameron NULL, 116451239600SJonathan Cameron i, 116551239600SJonathan Cameron shared_by, 116651239600SJonathan Cameron &indio_dev->dev, 116751239600SJonathan Cameron &indio_dev->channel_attr_list); 116851239600SJonathan Cameron kfree(avail_postfix); 116951239600SJonathan Cameron if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) 117051239600SJonathan Cameron continue; 117151239600SJonathan Cameron else if (ret < 0) 117251239600SJonathan Cameron return ret; 117351239600SJonathan Cameron attrcount++; 117451239600SJonathan Cameron } 117551239600SJonathan Cameron 117651239600SJonathan Cameron return attrcount; 117751239600SJonathan Cameron } 117851239600SJonathan Cameron 1179a980e046SJonathan Cameron static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev, 1180a980e046SJonathan Cameron struct iio_chan_spec const *chan) 1181a980e046SJonathan Cameron { 1182a980e046SJonathan Cameron int ret, attrcount = 0; 1183a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 1184a980e046SJonathan Cameron 1185a980e046SJonathan Cameron if (chan->channel < 0) 1186a980e046SJonathan Cameron return 0; 11873704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 11883704432fSJonathan Cameron IIO_SEPARATE, 11893704432fSJonathan Cameron &chan->info_mask_separate); 11908655cc49SJonathan Cameron if (ret < 0) 11913704432fSJonathan Cameron return ret; 11923704432fSJonathan Cameron attrcount += ret; 11933704432fSJonathan Cameron 119451239600SJonathan Cameron ret = iio_device_add_info_mask_type_avail(indio_dev, chan, 119551239600SJonathan Cameron IIO_SEPARATE, 119651239600SJonathan Cameron &chan-> 119751239600SJonathan Cameron info_mask_separate_available); 119851239600SJonathan Cameron if (ret < 0) 119951239600SJonathan Cameron return ret; 120051239600SJonathan Cameron attrcount += ret; 120151239600SJonathan Cameron 12023704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 12033704432fSJonathan Cameron IIO_SHARED_BY_TYPE, 12043704432fSJonathan Cameron &chan->info_mask_shared_by_type); 12053704432fSJonathan Cameron if (ret < 0) 12063704432fSJonathan Cameron return ret; 12073704432fSJonathan Cameron attrcount += ret; 1208a980e046SJonathan Cameron 120951239600SJonathan Cameron ret = iio_device_add_info_mask_type_avail(indio_dev, chan, 121051239600SJonathan Cameron IIO_SHARED_BY_TYPE, 121151239600SJonathan Cameron &chan-> 121251239600SJonathan Cameron info_mask_shared_by_type_available); 121351239600SJonathan Cameron if (ret < 0) 121451239600SJonathan Cameron return ret; 121551239600SJonathan Cameron attrcount += ret; 121651239600SJonathan Cameron 1217c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 1218c006ec83SJonathan Cameron IIO_SHARED_BY_DIR, 1219c006ec83SJonathan Cameron &chan->info_mask_shared_by_dir); 1220c006ec83SJonathan Cameron if (ret < 0) 1221c006ec83SJonathan Cameron return ret; 1222c006ec83SJonathan Cameron attrcount += ret; 1223c006ec83SJonathan Cameron 122451239600SJonathan Cameron ret = iio_device_add_info_mask_type_avail(indio_dev, chan, 122551239600SJonathan Cameron IIO_SHARED_BY_DIR, 122651239600SJonathan Cameron &chan->info_mask_shared_by_dir_available); 122751239600SJonathan Cameron if (ret < 0) 122851239600SJonathan Cameron return ret; 122951239600SJonathan Cameron attrcount += ret; 123051239600SJonathan Cameron 1231c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 1232c006ec83SJonathan Cameron IIO_SHARED_BY_ALL, 1233c006ec83SJonathan Cameron &chan->info_mask_shared_by_all); 1234c006ec83SJonathan Cameron if (ret < 0) 1235c006ec83SJonathan Cameron return ret; 1236c006ec83SJonathan Cameron attrcount += ret; 1237c006ec83SJonathan Cameron 123851239600SJonathan Cameron ret = iio_device_add_info_mask_type_avail(indio_dev, chan, 123951239600SJonathan Cameron IIO_SHARED_BY_ALL, 124051239600SJonathan Cameron &chan->info_mask_shared_by_all_available); 124151239600SJonathan Cameron if (ret < 0) 124251239600SJonathan Cameron return ret; 124351239600SJonathan Cameron attrcount += ret; 124451239600SJonathan Cameron 1245a980e046SJonathan Cameron if (chan->ext_info) { 1246a980e046SJonathan Cameron unsigned int i = 0; 1247a980e046SJonathan Cameron for (ext_info = chan->ext_info; ext_info->name; ext_info++) { 1248a980e046SJonathan Cameron ret = __iio_add_chan_devattr(ext_info->name, 1249a980e046SJonathan Cameron chan, 1250a980e046SJonathan Cameron ext_info->read ? 1251a980e046SJonathan Cameron &iio_read_channel_ext_info : NULL, 1252a980e046SJonathan Cameron ext_info->write ? 1253a980e046SJonathan Cameron &iio_write_channel_ext_info : NULL, 1254a980e046SJonathan Cameron i, 1255a980e046SJonathan Cameron ext_info->shared, 1256a980e046SJonathan Cameron &indio_dev->dev, 1257a980e046SJonathan Cameron &indio_dev->channel_attr_list); 1258a980e046SJonathan Cameron i++; 1259a980e046SJonathan Cameron if (ret == -EBUSY && ext_info->shared) 1260a980e046SJonathan Cameron continue; 1261a980e046SJonathan Cameron 1262a980e046SJonathan Cameron if (ret) 12633704432fSJonathan Cameron return ret; 1264a980e046SJonathan Cameron 1265a980e046SJonathan Cameron attrcount++; 1266a980e046SJonathan Cameron } 1267a980e046SJonathan Cameron } 1268a980e046SJonathan Cameron 12693704432fSJonathan Cameron return attrcount; 1270a980e046SJonathan Cameron } 1271a980e046SJonathan Cameron 127284088ebdSLars-Peter Clausen /** 127384088ebdSLars-Peter Clausen * iio_free_chan_devattr_list() - Free a list of IIO device attributes 127484088ebdSLars-Peter Clausen * @attr_list: List of IIO device attributes 127584088ebdSLars-Peter Clausen * 127684088ebdSLars-Peter Clausen * This function frees the memory allocated for each of the IIO device 1277c1b03ab5SMartin Fuzzey * attributes in the list. 127884088ebdSLars-Peter Clausen */ 127984088ebdSLars-Peter Clausen void iio_free_chan_devattr_list(struct list_head *attr_list) 1280a980e046SJonathan Cameron { 128184088ebdSLars-Peter Clausen struct iio_dev_attr *p, *n; 128284088ebdSLars-Peter Clausen 128384088ebdSLars-Peter Clausen list_for_each_entry_safe(p, n, attr_list, l) { 1284a980e046SJonathan Cameron kfree(p->dev_attr.attr.name); 1285c1b03ab5SMartin Fuzzey list_del(&p->l); 1286a980e046SJonathan Cameron kfree(p); 1287a980e046SJonathan Cameron } 128884088ebdSLars-Peter Clausen } 1289a980e046SJonathan Cameron 1290a980e046SJonathan Cameron static ssize_t iio_show_dev_name(struct device *dev, 1291a980e046SJonathan Cameron struct device_attribute *attr, 1292a980e046SJonathan Cameron char *buf) 1293a980e046SJonathan Cameron { 1294e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1295598db581SKees Cook return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name); 1296a980e046SJonathan Cameron } 1297a980e046SJonathan Cameron 1298a980e046SJonathan Cameron static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL); 1299a980e046SJonathan Cameron 13002c3d0c9fSPhil Reid static ssize_t iio_show_dev_label(struct device *dev, 13012c3d0c9fSPhil Reid struct device_attribute *attr, 13022c3d0c9fSPhil Reid char *buf) 13032c3d0c9fSPhil Reid { 13042c3d0c9fSPhil Reid struct iio_dev *indio_dev = dev_to_iio_dev(dev); 13052c3d0c9fSPhil Reid return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->label); 13062c3d0c9fSPhil Reid } 13072c3d0c9fSPhil Reid 13082c3d0c9fSPhil Reid static DEVICE_ATTR(label, S_IRUGO, iio_show_dev_label, NULL); 13092c3d0c9fSPhil Reid 1310bc2b7dabSGregor Boirie static ssize_t iio_show_timestamp_clock(struct device *dev, 1311bc2b7dabSGregor Boirie struct device_attribute *attr, 1312bc2b7dabSGregor Boirie char *buf) 1313bc2b7dabSGregor Boirie { 1314bc2b7dabSGregor Boirie const struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1315bc2b7dabSGregor Boirie const clockid_t clk = iio_device_get_clock(indio_dev); 1316bc2b7dabSGregor Boirie const char *name; 1317bc2b7dabSGregor Boirie ssize_t sz; 1318bc2b7dabSGregor Boirie 1319bc2b7dabSGregor Boirie switch (clk) { 1320bc2b7dabSGregor Boirie case CLOCK_REALTIME: 1321bc2b7dabSGregor Boirie name = "realtime\n"; 1322bc2b7dabSGregor Boirie sz = sizeof("realtime\n"); 1323bc2b7dabSGregor Boirie break; 1324bc2b7dabSGregor Boirie case CLOCK_MONOTONIC: 1325bc2b7dabSGregor Boirie name = "monotonic\n"; 1326bc2b7dabSGregor Boirie sz = sizeof("monotonic\n"); 1327bc2b7dabSGregor Boirie break; 1328bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_RAW: 1329bc2b7dabSGregor Boirie name = "monotonic_raw\n"; 1330bc2b7dabSGregor Boirie sz = sizeof("monotonic_raw\n"); 1331bc2b7dabSGregor Boirie break; 1332bc2b7dabSGregor Boirie case CLOCK_REALTIME_COARSE: 1333bc2b7dabSGregor Boirie name = "realtime_coarse\n"; 1334bc2b7dabSGregor Boirie sz = sizeof("realtime_coarse\n"); 1335bc2b7dabSGregor Boirie break; 1336bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_COARSE: 1337bc2b7dabSGregor Boirie name = "monotonic_coarse\n"; 1338bc2b7dabSGregor Boirie sz = sizeof("monotonic_coarse\n"); 1339bc2b7dabSGregor Boirie break; 1340bc2b7dabSGregor Boirie case CLOCK_BOOTTIME: 1341bc2b7dabSGregor Boirie name = "boottime\n"; 1342bc2b7dabSGregor Boirie sz = sizeof("boottime\n"); 1343bc2b7dabSGregor Boirie break; 1344bc2b7dabSGregor Boirie case CLOCK_TAI: 1345bc2b7dabSGregor Boirie name = "tai\n"; 1346bc2b7dabSGregor Boirie sz = sizeof("tai\n"); 1347bc2b7dabSGregor Boirie break; 1348bc2b7dabSGregor Boirie default: 1349bc2b7dabSGregor Boirie BUG(); 1350bc2b7dabSGregor Boirie } 1351bc2b7dabSGregor Boirie 1352bc2b7dabSGregor Boirie memcpy(buf, name, sz); 1353bc2b7dabSGregor Boirie return sz; 1354bc2b7dabSGregor Boirie } 1355bc2b7dabSGregor Boirie 1356bc2b7dabSGregor Boirie static ssize_t iio_store_timestamp_clock(struct device *dev, 1357bc2b7dabSGregor Boirie struct device_attribute *attr, 1358bc2b7dabSGregor Boirie const char *buf, size_t len) 1359bc2b7dabSGregor Boirie { 1360bc2b7dabSGregor Boirie clockid_t clk; 1361bc2b7dabSGregor Boirie int ret; 1362bc2b7dabSGregor Boirie 1363bc2b7dabSGregor Boirie if (sysfs_streq(buf, "realtime")) 1364bc2b7dabSGregor Boirie clk = CLOCK_REALTIME; 1365bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "monotonic")) 1366bc2b7dabSGregor Boirie clk = CLOCK_MONOTONIC; 1367bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "monotonic_raw")) 1368bc2b7dabSGregor Boirie clk = CLOCK_MONOTONIC_RAW; 1369bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "realtime_coarse")) 1370bc2b7dabSGregor Boirie clk = CLOCK_REALTIME_COARSE; 1371bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "monotonic_coarse")) 1372bc2b7dabSGregor Boirie clk = CLOCK_MONOTONIC_COARSE; 1373bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "boottime")) 1374bc2b7dabSGregor Boirie clk = CLOCK_BOOTTIME; 1375bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "tai")) 1376bc2b7dabSGregor Boirie clk = CLOCK_TAI; 1377bc2b7dabSGregor Boirie else 1378bc2b7dabSGregor Boirie return -EINVAL; 1379bc2b7dabSGregor Boirie 1380bc2b7dabSGregor Boirie ret = iio_device_set_clock(dev_to_iio_dev(dev), clk); 1381bc2b7dabSGregor Boirie if (ret) 1382bc2b7dabSGregor Boirie return ret; 1383bc2b7dabSGregor Boirie 1384bc2b7dabSGregor Boirie return len; 1385bc2b7dabSGregor Boirie } 1386bc2b7dabSGregor Boirie 1387bc2b7dabSGregor Boirie static DEVICE_ATTR(current_timestamp_clock, S_IRUGO | S_IWUSR, 1388bc2b7dabSGregor Boirie iio_show_timestamp_clock, iio_store_timestamp_clock); 1389bc2b7dabSGregor Boirie 1390a980e046SJonathan Cameron static int iio_device_register_sysfs(struct iio_dev *indio_dev) 1391a980e046SJonathan Cameron { 1392a980e046SJonathan Cameron int i, ret = 0, attrcount, attrn, attrcount_orig = 0; 139384088ebdSLars-Peter Clausen struct iio_dev_attr *p; 1394bc2b7dabSGregor Boirie struct attribute **attr, *clk = NULL; 1395a980e046SJonathan Cameron 1396a980e046SJonathan Cameron /* First count elements in any existing group */ 1397a980e046SJonathan Cameron if (indio_dev->info->attrs) { 1398a980e046SJonathan Cameron attr = indio_dev->info->attrs->attrs; 1399a980e046SJonathan Cameron while (*attr++ != NULL) 1400a980e046SJonathan Cameron attrcount_orig++; 1401a980e046SJonathan Cameron } 1402a980e046SJonathan Cameron attrcount = attrcount_orig; 1403a980e046SJonathan Cameron /* 1404a980e046SJonathan Cameron * New channel registration method - relies on the fact a group does 1405d25b3808SPeter Meerwald * not need to be initialized if its name is NULL. 1406a980e046SJonathan Cameron */ 1407a980e046SJonathan Cameron if (indio_dev->channels) 1408a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) { 1409bc2b7dabSGregor Boirie const struct iio_chan_spec *chan = 1410bc2b7dabSGregor Boirie &indio_dev->channels[i]; 1411bc2b7dabSGregor Boirie 1412bc2b7dabSGregor Boirie if (chan->type == IIO_TIMESTAMP) 1413bc2b7dabSGregor Boirie clk = &dev_attr_current_timestamp_clock.attr; 1414bc2b7dabSGregor Boirie 1415bc2b7dabSGregor Boirie ret = iio_device_add_channel_sysfs(indio_dev, chan); 1416a980e046SJonathan Cameron if (ret < 0) 1417a980e046SJonathan Cameron goto error_clear_attrs; 1418a980e046SJonathan Cameron attrcount += ret; 1419a980e046SJonathan Cameron } 1420a980e046SJonathan Cameron 1421bc2b7dabSGregor Boirie if (indio_dev->event_interface) 1422bc2b7dabSGregor Boirie clk = &dev_attr_current_timestamp_clock.attr; 1423bc2b7dabSGregor Boirie 1424a980e046SJonathan Cameron if (indio_dev->name) 1425a980e046SJonathan Cameron attrcount++; 14262c3d0c9fSPhil Reid if (indio_dev->label) 14272c3d0c9fSPhil Reid attrcount++; 1428bc2b7dabSGregor Boirie if (clk) 1429bc2b7dabSGregor Boirie attrcount++; 1430a980e046SJonathan Cameron 1431a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1, 1432a980e046SJonathan Cameron sizeof(indio_dev->chan_attr_group.attrs[0]), 1433a980e046SJonathan Cameron GFP_KERNEL); 1434a980e046SJonathan Cameron if (indio_dev->chan_attr_group.attrs == NULL) { 1435a980e046SJonathan Cameron ret = -ENOMEM; 1436a980e046SJonathan Cameron goto error_clear_attrs; 1437a980e046SJonathan Cameron } 1438a980e046SJonathan Cameron /* Copy across original attributes */ 1439a980e046SJonathan Cameron if (indio_dev->info->attrs) 1440a980e046SJonathan Cameron memcpy(indio_dev->chan_attr_group.attrs, 1441a980e046SJonathan Cameron indio_dev->info->attrs->attrs, 1442a980e046SJonathan Cameron sizeof(indio_dev->chan_attr_group.attrs[0]) 1443a980e046SJonathan Cameron *attrcount_orig); 1444a980e046SJonathan Cameron attrn = attrcount_orig; 1445a980e046SJonathan Cameron /* Add all elements from the list. */ 1446a980e046SJonathan Cameron list_for_each_entry(p, &indio_dev->channel_attr_list, l) 1447a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr; 1448a980e046SJonathan Cameron if (indio_dev->name) 1449a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr; 14502c3d0c9fSPhil Reid if (indio_dev->label) 14512c3d0c9fSPhil Reid indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_label.attr; 1452bc2b7dabSGregor Boirie if (clk) 1453bc2b7dabSGregor Boirie indio_dev->chan_attr_group.attrs[attrn++] = clk; 1454a980e046SJonathan Cameron 1455a980e046SJonathan Cameron indio_dev->groups[indio_dev->groupcounter++] = 1456a980e046SJonathan Cameron &indio_dev->chan_attr_group; 1457a980e046SJonathan Cameron 1458a980e046SJonathan Cameron return 0; 1459a980e046SJonathan Cameron 1460a980e046SJonathan Cameron error_clear_attrs: 146184088ebdSLars-Peter Clausen iio_free_chan_devattr_list(&indio_dev->channel_attr_list); 1462a980e046SJonathan Cameron 1463a980e046SJonathan Cameron return ret; 1464a980e046SJonathan Cameron } 1465a980e046SJonathan Cameron 1466a980e046SJonathan Cameron static void iio_device_unregister_sysfs(struct iio_dev *indio_dev) 1467a980e046SJonathan Cameron { 1468a980e046SJonathan Cameron 146984088ebdSLars-Peter Clausen iio_free_chan_devattr_list(&indio_dev->channel_attr_list); 1470a980e046SJonathan Cameron kfree(indio_dev->chan_attr_group.attrs); 1471c1b03ab5SMartin Fuzzey indio_dev->chan_attr_group.attrs = NULL; 1472a980e046SJonathan Cameron } 1473a980e046SJonathan Cameron 1474a980e046SJonathan Cameron static void iio_dev_release(struct device *device) 1475a980e046SJonathan Cameron { 1476e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(device); 1477*6d4ebd56SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1478*6d4ebd56SAlexandru Ardelean 1479d89e119aSBenjamin Gaignard if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES) 1480a980e046SJonathan Cameron iio_device_unregister_trigger_consumer(indio_dev); 1481a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 1482a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 1483e407fd65SLars-Peter Clausen 14849e69c935SLars-Peter Clausen iio_buffer_put(indio_dev->buffer); 14859e69c935SLars-Peter Clausen 1486e407fd65SLars-Peter Clausen ida_simple_remove(&iio_ida, indio_dev->id); 1487*6d4ebd56SAlexandru Ardelean kfree(iio_dev_opaque); 1488a980e046SJonathan Cameron } 1489a980e046SJonathan Cameron 149017d82b47SGuenter Roeck struct device_type iio_device_type = { 1491a980e046SJonathan Cameron .name = "iio_device", 1492a980e046SJonathan Cameron .release = iio_dev_release, 1493a980e046SJonathan Cameron }; 1494a980e046SJonathan Cameron 1495a7e57dceSSachin Kamat /** 1496a7e57dceSSachin Kamat * iio_device_alloc() - allocate an iio_dev from a driver 1497a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 1498a7e57dceSSachin Kamat **/ 149978289b4aSAlexandru Ardelean struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv) 1500a980e046SJonathan Cameron { 1501*6d4ebd56SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque; 1502a980e046SJonathan Cameron struct iio_dev *dev; 1503a980e046SJonathan Cameron size_t alloc_size; 1504a980e046SJonathan Cameron 1505*6d4ebd56SAlexandru Ardelean alloc_size = sizeof(struct iio_dev_opaque); 1506a980e046SJonathan Cameron if (sizeof_priv) { 1507a980e046SJonathan Cameron alloc_size = ALIGN(alloc_size, IIO_ALIGN); 1508a980e046SJonathan Cameron alloc_size += sizeof_priv; 1509a980e046SJonathan Cameron } 1510a980e046SJonathan Cameron /* ensure 32-byte alignment of whole construct ? */ 1511a980e046SJonathan Cameron alloc_size += IIO_ALIGN - 1; 1512a980e046SJonathan Cameron 1513*6d4ebd56SAlexandru Ardelean iio_dev_opaque = kzalloc(alloc_size, GFP_KERNEL); 1514*6d4ebd56SAlexandru Ardelean if (!iio_dev_opaque) 15150d4b2184SJonathan Cameron return NULL; 1516a980e046SJonathan Cameron 1517*6d4ebd56SAlexandru Ardelean dev = &iio_dev_opaque->indio_dev; 1518*6d4ebd56SAlexandru Ardelean dev->priv = (char *)iio_dev_opaque + 1519*6d4ebd56SAlexandru Ardelean ALIGN(sizeof(struct iio_dev_opaque), IIO_ALIGN); 1520*6d4ebd56SAlexandru Ardelean 1521a980e046SJonathan Cameron dev->dev.groups = dev->groups; 152217d82b47SGuenter Roeck dev->dev.type = &iio_device_type; 1523a980e046SJonathan Cameron dev->dev.bus = &iio_bus_type; 1524a980e046SJonathan Cameron device_initialize(&dev->dev); 1525a980e046SJonathan Cameron dev_set_drvdata(&dev->dev, (void *)dev); 1526a980e046SJonathan Cameron mutex_init(&dev->mlock); 1527a980e046SJonathan Cameron mutex_init(&dev->info_exist_lock); 1528e407fd65SLars-Peter Clausen INIT_LIST_HEAD(&dev->channel_attr_list); 1529a980e046SJonathan Cameron 1530a980e046SJonathan Cameron dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL); 1531a980e046SJonathan Cameron if (dev->id < 0) { 1532a980e046SJonathan Cameron /* cannot use a dev_err as the name isn't available */ 15333176dd5dSSachin Kamat pr_err("failed to get device id\n"); 1534*6d4ebd56SAlexandru Ardelean kfree(iio_dev_opaque); 1535a980e046SJonathan Cameron return NULL; 1536a980e046SJonathan Cameron } 1537a980e046SJonathan Cameron dev_set_name(&dev->dev, "iio:device%d", dev->id); 153884b36ce5SJonathan Cameron INIT_LIST_HEAD(&dev->buffer_list); 1539a980e046SJonathan Cameron 1540a980e046SJonathan Cameron return dev; 1541a980e046SJonathan Cameron } 15427cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_alloc); 1543a980e046SJonathan Cameron 1544a7e57dceSSachin Kamat /** 1545a7e57dceSSachin Kamat * iio_device_free() - free an iio_dev from a driver 1546a7e57dceSSachin Kamat * @dev: the iio_dev associated with the device 1547a7e57dceSSachin Kamat **/ 15487cbb7537SLars-Peter Clausen void iio_device_free(struct iio_dev *dev) 1549a980e046SJonathan Cameron { 1550e407fd65SLars-Peter Clausen if (dev) 1551e407fd65SLars-Peter Clausen put_device(&dev->dev); 1552a980e046SJonathan Cameron } 15537cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_free); 1554a980e046SJonathan Cameron 15559dabaf5eSGrygorii Strashko static void devm_iio_device_release(struct device *dev, void *res) 15569dabaf5eSGrygorii Strashko { 15579dabaf5eSGrygorii Strashko iio_device_free(*(struct iio_dev **)res); 15589dabaf5eSGrygorii Strashko } 15599dabaf5eSGrygorii Strashko 1560a7e57dceSSachin Kamat /** 1561a7e57dceSSachin Kamat * devm_iio_device_alloc - Resource-managed iio_device_alloc() 156278289b4aSAlexandru Ardelean * @parent: Device to allocate iio_dev for, and parent for this IIO device 1563a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 1564a7e57dceSSachin Kamat * 1565a7e57dceSSachin Kamat * Managed iio_device_alloc. iio_dev allocated with this function is 1566a7e57dceSSachin Kamat * automatically freed on driver detach. 1567a7e57dceSSachin Kamat * 1568a7e57dceSSachin Kamat * RETURNS: 1569a7e57dceSSachin Kamat * Pointer to allocated iio_dev on success, NULL on failure. 1570a7e57dceSSachin Kamat */ 157178289b4aSAlexandru Ardelean struct iio_dev *devm_iio_device_alloc(struct device *parent, int sizeof_priv) 15729dabaf5eSGrygorii Strashko { 15739dabaf5eSGrygorii Strashko struct iio_dev **ptr, *iio_dev; 15749dabaf5eSGrygorii Strashko 15759dabaf5eSGrygorii Strashko ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr), 15769dabaf5eSGrygorii Strashko GFP_KERNEL); 15779dabaf5eSGrygorii Strashko if (!ptr) 15789dabaf5eSGrygorii Strashko return NULL; 15799dabaf5eSGrygorii Strashko 158078289b4aSAlexandru Ardelean iio_dev = iio_device_alloc(parent, sizeof_priv); 15819dabaf5eSGrygorii Strashko if (iio_dev) { 15829dabaf5eSGrygorii Strashko *ptr = iio_dev; 158378289b4aSAlexandru Ardelean devres_add(parent, ptr); 15849dabaf5eSGrygorii Strashko } else { 15859dabaf5eSGrygorii Strashko devres_free(ptr); 15869dabaf5eSGrygorii Strashko } 15879dabaf5eSGrygorii Strashko 15889dabaf5eSGrygorii Strashko return iio_dev; 15899dabaf5eSGrygorii Strashko } 15909dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_alloc); 15919dabaf5eSGrygorii Strashko 1592a7e57dceSSachin Kamat /** 1593a980e046SJonathan Cameron * iio_chrdev_open() - chrdev file open for buffer access and ioctls 15942498dcf6SCristina Opriceana * @inode: Inode structure for identifying the device in the file system 15952498dcf6SCristina Opriceana * @filp: File structure for iio device used to keep and later access 15962498dcf6SCristina Opriceana * private data 15972498dcf6SCristina Opriceana * 15982498dcf6SCristina Opriceana * Return: 0 on success or -EBUSY if the device is already opened 1599a980e046SJonathan Cameron **/ 1600a980e046SJonathan Cameron static int iio_chrdev_open(struct inode *inode, struct file *filp) 1601a980e046SJonathan Cameron { 1602a980e046SJonathan Cameron struct iio_dev *indio_dev = container_of(inode->i_cdev, 1603a980e046SJonathan Cameron struct iio_dev, chrdev); 1604a980e046SJonathan Cameron 1605a980e046SJonathan Cameron if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags)) 1606a980e046SJonathan Cameron return -EBUSY; 1607a980e046SJonathan Cameron 1608cadc2125SLars-Peter Clausen iio_device_get(indio_dev); 1609cadc2125SLars-Peter Clausen 1610a980e046SJonathan Cameron filp->private_data = indio_dev; 1611a980e046SJonathan Cameron 1612a980e046SJonathan Cameron return 0; 1613a980e046SJonathan Cameron } 1614a980e046SJonathan Cameron 1615a980e046SJonathan Cameron /** 1616a980e046SJonathan Cameron * iio_chrdev_release() - chrdev file close buffer access and ioctls 16172498dcf6SCristina Opriceana * @inode: Inode structure pointer for the char device 16182498dcf6SCristina Opriceana * @filp: File structure pointer for the char device 16192498dcf6SCristina Opriceana * 16202498dcf6SCristina Opriceana * Return: 0 for successful release 16212498dcf6SCristina Opriceana */ 1622a980e046SJonathan Cameron static int iio_chrdev_release(struct inode *inode, struct file *filp) 1623a980e046SJonathan Cameron { 1624a980e046SJonathan Cameron struct iio_dev *indio_dev = container_of(inode->i_cdev, 1625a980e046SJonathan Cameron struct iio_dev, chrdev); 1626a980e046SJonathan Cameron clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags); 1627cadc2125SLars-Peter Clausen iio_device_put(indio_dev); 1628cadc2125SLars-Peter Clausen 1629a980e046SJonathan Cameron return 0; 1630a980e046SJonathan Cameron } 1631a980e046SJonathan Cameron 1632a980e046SJonathan Cameron /* Somewhat of a cross file organization violation - ioctls here are actually 1633a980e046SJonathan Cameron * event related */ 1634a980e046SJonathan Cameron static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 1635a980e046SJonathan Cameron { 1636a980e046SJonathan Cameron struct iio_dev *indio_dev = filp->private_data; 1637a980e046SJonathan Cameron int __user *ip = (int __user *)arg; 1638a980e046SJonathan Cameron int fd; 1639a980e046SJonathan Cameron 1640f18e7a06SLars-Peter Clausen if (!indio_dev->info) 1641f18e7a06SLars-Peter Clausen return -ENODEV; 1642f18e7a06SLars-Peter Clausen 1643a980e046SJonathan Cameron if (cmd == IIO_GET_EVENT_FD_IOCTL) { 1644a980e046SJonathan Cameron fd = iio_event_getfd(indio_dev); 16453f9059b7SLinus Walleij if (fd < 0) 16463f9059b7SLinus Walleij return fd; 1647a980e046SJonathan Cameron if (copy_to_user(ip, &fd, sizeof(fd))) 1648a980e046SJonathan Cameron return -EFAULT; 1649a980e046SJonathan Cameron return 0; 1650a980e046SJonathan Cameron } 1651a980e046SJonathan Cameron return -EINVAL; 1652a980e046SJonathan Cameron } 1653a980e046SJonathan Cameron 1654a980e046SJonathan Cameron static const struct file_operations iio_buffer_fileops = { 1655f6d4033dSLars-Peter Clausen .read = iio_buffer_read_outer_addr, 1656a980e046SJonathan Cameron .release = iio_chrdev_release, 1657a980e046SJonathan Cameron .open = iio_chrdev_open, 1658a980e046SJonathan Cameron .poll = iio_buffer_poll_addr, 1659a980e046SJonathan Cameron .owner = THIS_MODULE, 1660a980e046SJonathan Cameron .llseek = noop_llseek, 1661a980e046SJonathan Cameron .unlocked_ioctl = iio_ioctl, 16621832f2d8SArnd Bergmann .compat_ioctl = compat_ptr_ioctl, 1663a980e046SJonathan Cameron }; 1664a980e046SJonathan Cameron 16658f5d8727SVlad Dogaru static int iio_check_unique_scan_index(struct iio_dev *indio_dev) 16668f5d8727SVlad Dogaru { 16678f5d8727SVlad Dogaru int i, j; 16688f5d8727SVlad Dogaru const struct iio_chan_spec *channels = indio_dev->channels; 16698f5d8727SVlad Dogaru 16708f5d8727SVlad Dogaru if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES)) 16718f5d8727SVlad Dogaru return 0; 16728f5d8727SVlad Dogaru 16738f5d8727SVlad Dogaru for (i = 0; i < indio_dev->num_channels - 1; i++) { 16748f5d8727SVlad Dogaru if (channels[i].scan_index < 0) 16758f5d8727SVlad Dogaru continue; 16768f5d8727SVlad Dogaru for (j = i + 1; j < indio_dev->num_channels; j++) 16778f5d8727SVlad Dogaru if (channels[i].scan_index == channels[j].scan_index) { 16788f5d8727SVlad Dogaru dev_err(&indio_dev->dev, 16798f5d8727SVlad Dogaru "Duplicate scan index %d\n", 16808f5d8727SVlad Dogaru channels[i].scan_index); 16818f5d8727SVlad Dogaru return -EINVAL; 16828f5d8727SVlad Dogaru } 16838f5d8727SVlad Dogaru } 16848f5d8727SVlad Dogaru 16858f5d8727SVlad Dogaru return 0; 16868f5d8727SVlad Dogaru } 16878f5d8727SVlad Dogaru 1688a980e046SJonathan Cameron static const struct iio_buffer_setup_ops noop_ring_setup_ops; 1689a980e046SJonathan Cameron 169063b19547SJonathan Cameron int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod) 1691a980e046SJonathan Cameron { 1692a980e046SJonathan Cameron int ret; 1693a980e046SJonathan Cameron 1694a17cb784SAlexandru Ardelean if (!indio_dev->info) 1695a17cb784SAlexandru Ardelean return -EINVAL; 1696a17cb784SAlexandru Ardelean 169763b19547SJonathan Cameron indio_dev->driver_module = this_mod; 169817d82b47SGuenter Roeck /* If the calling driver did not initialize of_node, do it here */ 169917d82b47SGuenter Roeck if (!indio_dev->dev.of_node && indio_dev->dev.parent) 170017d82b47SGuenter Roeck indio_dev->dev.of_node = indio_dev->dev.parent->of_node; 170117d82b47SGuenter Roeck 17022c3d0c9fSPhil Reid indio_dev->label = of_get_property(indio_dev->dev.of_node, "label", 17032c3d0c9fSPhil Reid NULL); 17042c3d0c9fSPhil Reid 17058f5d8727SVlad Dogaru ret = iio_check_unique_scan_index(indio_dev); 17068f5d8727SVlad Dogaru if (ret < 0) 17078f5d8727SVlad Dogaru return ret; 17088f5d8727SVlad Dogaru 1709a980e046SJonathan Cameron /* configure elements for the chrdev */ 1710a980e046SJonathan Cameron indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id); 1711a980e046SJonathan Cameron 17128915aacaSGreg Kroah-Hartman iio_device_register_debugfs(indio_dev); 17133e1b6c95SLars-Peter Clausen 17143e1b6c95SLars-Peter Clausen ret = iio_buffer_alloc_sysfs_and_mask(indio_dev); 17153e1b6c95SLars-Peter Clausen if (ret) { 17163e1b6c95SLars-Peter Clausen dev_err(indio_dev->dev.parent, 17173e1b6c95SLars-Peter Clausen "Failed to create buffer sysfs interfaces\n"); 17183e1b6c95SLars-Peter Clausen goto error_unreg_debugfs; 17193e1b6c95SLars-Peter Clausen } 17203e1b6c95SLars-Peter Clausen 1721a980e046SJonathan Cameron ret = iio_device_register_sysfs(indio_dev); 1722a980e046SJonathan Cameron if (ret) { 1723a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1724a980e046SJonathan Cameron "Failed to register sysfs interfaces\n"); 17253e1b6c95SLars-Peter Clausen goto error_buffer_free_sysfs; 1726a980e046SJonathan Cameron } 1727a980e046SJonathan Cameron ret = iio_device_register_eventset(indio_dev); 1728a980e046SJonathan Cameron if (ret) { 1729a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1730a980e046SJonathan Cameron "Failed to register event set\n"); 1731a980e046SJonathan Cameron goto error_free_sysfs; 1732a980e046SJonathan Cameron } 1733d89e119aSBenjamin Gaignard if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES) 1734a980e046SJonathan Cameron iio_device_register_trigger_consumer(indio_dev); 1735a980e046SJonathan Cameron 1736a980e046SJonathan Cameron if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) && 1737a980e046SJonathan Cameron indio_dev->setup_ops == NULL) 1738a980e046SJonathan Cameron indio_dev->setup_ops = &noop_ring_setup_ops; 1739a980e046SJonathan Cameron 1740a980e046SJonathan Cameron cdev_init(&indio_dev->chrdev, &iio_buffer_fileops); 174163b19547SJonathan Cameron 174263b19547SJonathan Cameron indio_dev->chrdev.owner = this_mod; 174338923911SLogan Gunthorpe 174438923911SLogan Gunthorpe ret = cdev_device_add(&indio_dev->chrdev, &indio_dev->dev); 1745a980e046SJonathan Cameron if (ret < 0) 17460d5b7daeSLars-Peter Clausen goto error_unreg_eventset; 1747a980e046SJonathan Cameron 17480d5b7daeSLars-Peter Clausen return 0; 174938923911SLogan Gunthorpe 1750a980e046SJonathan Cameron error_unreg_eventset: 1751a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 1752a980e046SJonathan Cameron error_free_sysfs: 1753a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 17543e1b6c95SLars-Peter Clausen error_buffer_free_sysfs: 17553e1b6c95SLars-Peter Clausen iio_buffer_free_sysfs_and_mask(indio_dev); 1756a980e046SJonathan Cameron error_unreg_debugfs: 1757a980e046SJonathan Cameron iio_device_unregister_debugfs(indio_dev); 1758a980e046SJonathan Cameron return ret; 1759a980e046SJonathan Cameron } 176063b19547SJonathan Cameron EXPORT_SYMBOL(__iio_device_register); 1761a980e046SJonathan Cameron 1762a7e57dceSSachin Kamat /** 1763a7e57dceSSachin Kamat * iio_device_unregister() - unregister a device from the IIO subsystem 1764a7e57dceSSachin Kamat * @indio_dev: Device structure representing the device. 1765a7e57dceSSachin Kamat **/ 1766a980e046SJonathan Cameron void iio_device_unregister(struct iio_dev *indio_dev) 1767a980e046SJonathan Cameron { 176838923911SLogan Gunthorpe cdev_device_del(&indio_dev->chrdev, &indio_dev->dev); 1769a87c82e4SLars-Peter Clausen 17707f75591fSFabrice Gasnier mutex_lock(&indio_dev->info_exist_lock); 17717f75591fSFabrice Gasnier 1772bc4c9612SLars-Peter Clausen iio_device_unregister_debugfs(indio_dev); 17730d5b7daeSLars-Peter Clausen 1774a87c82e4SLars-Peter Clausen iio_disable_all_buffers(indio_dev); 1775a87c82e4SLars-Peter Clausen 1776a980e046SJonathan Cameron indio_dev->info = NULL; 1777d2f0a48fSLars-Peter Clausen 1778d2f0a48fSLars-Peter Clausen iio_device_wakeup_eventset(indio_dev); 1779d2f0a48fSLars-Peter Clausen iio_buffer_wakeup_poll(indio_dev); 1780d2f0a48fSLars-Peter Clausen 1781a980e046SJonathan Cameron mutex_unlock(&indio_dev->info_exist_lock); 17823e1b6c95SLars-Peter Clausen 17833e1b6c95SLars-Peter Clausen iio_buffer_free_sysfs_and_mask(indio_dev); 1784a980e046SJonathan Cameron } 1785a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_unregister); 17868caa07c0SSachin Kamat 17878caa07c0SSachin Kamat static void devm_iio_device_unreg(struct device *dev, void *res) 17888caa07c0SSachin Kamat { 17898caa07c0SSachin Kamat iio_device_unregister(*(struct iio_dev **)res); 17908caa07c0SSachin Kamat } 17918caa07c0SSachin Kamat 179263b19547SJonathan Cameron int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev, 179363b19547SJonathan Cameron struct module *this_mod) 17948caa07c0SSachin Kamat { 17958caa07c0SSachin Kamat struct iio_dev **ptr; 17968caa07c0SSachin Kamat int ret; 17978caa07c0SSachin Kamat 17988caa07c0SSachin Kamat ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL); 17998caa07c0SSachin Kamat if (!ptr) 18008caa07c0SSachin Kamat return -ENOMEM; 18018caa07c0SSachin Kamat 18028caa07c0SSachin Kamat *ptr = indio_dev; 180363b19547SJonathan Cameron ret = __iio_device_register(indio_dev, this_mod); 18048caa07c0SSachin Kamat if (!ret) 18058caa07c0SSachin Kamat devres_add(dev, ptr); 18068caa07c0SSachin Kamat else 18078caa07c0SSachin Kamat devres_free(ptr); 18088caa07c0SSachin Kamat 18098caa07c0SSachin Kamat return ret; 18108caa07c0SSachin Kamat } 181163b19547SJonathan Cameron EXPORT_SYMBOL_GPL(__devm_iio_device_register); 18128caa07c0SSachin Kamat 18138caa07c0SSachin Kamat /** 181408a33805SAlison Schofield * iio_device_claim_direct_mode - Keep device in direct mode 181508a33805SAlison Schofield * @indio_dev: the iio_dev associated with the device 181608a33805SAlison Schofield * 181708a33805SAlison Schofield * If the device is in direct mode it is guaranteed to stay 181808a33805SAlison Schofield * that way until iio_device_release_direct_mode() is called. 181908a33805SAlison Schofield * 182008a33805SAlison Schofield * Use with iio_device_release_direct_mode() 182108a33805SAlison Schofield * 182208a33805SAlison Schofield * Returns: 0 on success, -EBUSY on failure 182308a33805SAlison Schofield */ 182408a33805SAlison Schofield int iio_device_claim_direct_mode(struct iio_dev *indio_dev) 182508a33805SAlison Schofield { 182608a33805SAlison Schofield mutex_lock(&indio_dev->mlock); 182708a33805SAlison Schofield 182808a33805SAlison Schofield if (iio_buffer_enabled(indio_dev)) { 182908a33805SAlison Schofield mutex_unlock(&indio_dev->mlock); 183008a33805SAlison Schofield return -EBUSY; 183108a33805SAlison Schofield } 183208a33805SAlison Schofield return 0; 183308a33805SAlison Schofield } 183408a33805SAlison Schofield EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode); 183508a33805SAlison Schofield 183608a33805SAlison Schofield /** 183708a33805SAlison Schofield * iio_device_release_direct_mode - releases claim on direct mode 183808a33805SAlison Schofield * @indio_dev: the iio_dev associated with the device 183908a33805SAlison Schofield * 184008a33805SAlison Schofield * Release the claim. Device is no longer guaranteed to stay 184108a33805SAlison Schofield * in direct mode. 184208a33805SAlison Schofield * 184308a33805SAlison Schofield * Use with iio_device_claim_direct_mode() 184408a33805SAlison Schofield */ 184508a33805SAlison Schofield void iio_device_release_direct_mode(struct iio_dev *indio_dev) 184608a33805SAlison Schofield { 184708a33805SAlison Schofield mutex_unlock(&indio_dev->mlock); 184808a33805SAlison Schofield } 184908a33805SAlison Schofield EXPORT_SYMBOL_GPL(iio_device_release_direct_mode); 185008a33805SAlison Schofield 1851a980e046SJonathan Cameron subsys_initcall(iio_init); 1852a980e046SJonathan Cameron module_exit(iio_exit); 1853a980e046SJonathan Cameron 1854c8b95952SJonathan Cameron MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); 1855a980e046SJonathan Cameron MODULE_DESCRIPTION("Industrial I/O core"); 1856a980e046SJonathan Cameron MODULE_LICENSE("GPL"); 1857