1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2a980e046SJonathan Cameron /* The industrial I/O core 3a980e046SJonathan Cameron * 4a980e046SJonathan Cameron * Copyright (c) 2008 Jonathan Cameron 5a980e046SJonathan Cameron * 6a980e046SJonathan Cameron * Based on elements of hwmon and input subsystems. 7a980e046SJonathan Cameron */ 8a980e046SJonathan Cameron 93176dd5dSSachin Kamat #define pr_fmt(fmt) "iio-core: " fmt 103176dd5dSSachin Kamat 11a980e046SJonathan Cameron #include <linux/kernel.h> 12a980e046SJonathan Cameron #include <linux/module.h> 13a980e046SJonathan Cameron #include <linux/idr.h> 14a980e046SJonathan Cameron #include <linux/kdev_t.h> 15a980e046SJonathan Cameron #include <linux/err.h> 16a980e046SJonathan Cameron #include <linux/device.h> 17a980e046SJonathan Cameron #include <linux/fs.h> 18a980e046SJonathan Cameron #include <linux/poll.h> 19fb158971SAndy Shevchenko #include <linux/property.h> 20a980e046SJonathan Cameron #include <linux/sched.h> 21a980e046SJonathan Cameron #include <linux/wait.h> 22a980e046SJonathan Cameron #include <linux/cdev.h> 23a980e046SJonathan Cameron #include <linux/slab.h> 24a980e046SJonathan Cameron #include <linux/anon_inodes.h> 25a980e046SJonathan Cameron #include <linux/debugfs.h> 2608a33805SAlison Schofield #include <linux/mutex.h> 27a980e046SJonathan Cameron #include <linux/iio/iio.h> 286d4ebd56SAlexandru Ardelean #include <linux/iio/iio-opaque.h> 29a980e046SJonathan Cameron #include "iio_core.h" 30a980e046SJonathan Cameron #include "iio_core_trigger.h" 31a980e046SJonathan Cameron #include <linux/iio/sysfs.h> 32a980e046SJonathan Cameron #include <linux/iio/events.h> 339e69c935SLars-Peter Clausen #include <linux/iio/buffer.h> 3433dd94cbSJonathan Cameron #include <linux/iio/buffer_impl.h> 35a980e046SJonathan Cameron 36a980e046SJonathan Cameron /* IDA to assign each registered device a unique id */ 37a980e046SJonathan Cameron static DEFINE_IDA(iio_ida); 38a980e046SJonathan Cameron 39a980e046SJonathan Cameron static dev_t iio_devt; 40a980e046SJonathan Cameron 41a980e046SJonathan Cameron #define IIO_DEV_MAX 256 42a980e046SJonathan Cameron struct bus_type iio_bus_type = { 43a980e046SJonathan Cameron .name = "iio", 44a980e046SJonathan Cameron }; 45a980e046SJonathan Cameron EXPORT_SYMBOL(iio_bus_type); 46a980e046SJonathan Cameron 47a980e046SJonathan Cameron static struct dentry *iio_debugfs_dentry; 48a980e046SJonathan Cameron 49a980e046SJonathan Cameron static const char * const iio_direction[] = { 50a980e046SJonathan Cameron [0] = "in", 51a980e046SJonathan Cameron [1] = "out", 52a980e046SJonathan Cameron }; 53a980e046SJonathan Cameron 54a980e046SJonathan Cameron static const char * const iio_chan_type_name_spec[] = { 55a980e046SJonathan Cameron [IIO_VOLTAGE] = "voltage", 56a980e046SJonathan Cameron [IIO_CURRENT] = "current", 57a980e046SJonathan Cameron [IIO_POWER] = "power", 58a980e046SJonathan Cameron [IIO_ACCEL] = "accel", 59a980e046SJonathan Cameron [IIO_ANGL_VEL] = "anglvel", 60a980e046SJonathan Cameron [IIO_MAGN] = "magn", 61a980e046SJonathan Cameron [IIO_LIGHT] = "illuminance", 62a980e046SJonathan Cameron [IIO_INTENSITY] = "intensity", 63a980e046SJonathan Cameron [IIO_PROXIMITY] = "proximity", 64a980e046SJonathan Cameron [IIO_TEMP] = "temp", 65a980e046SJonathan Cameron [IIO_INCLI] = "incli", 66a980e046SJonathan Cameron [IIO_ROT] = "rot", 67a980e046SJonathan Cameron [IIO_ANGL] = "angl", 68a980e046SJonathan Cameron [IIO_TIMESTAMP] = "timestamp", 69a980e046SJonathan Cameron [IIO_CAPACITANCE] = "capacitance", 70a6b12855SMichael Hennerich [IIO_ALTVOLTAGE] = "altvoltage", 7121cd1fabSJon Brenner [IIO_CCT] = "cct", 72c4f0c693SLars-Peter Clausen [IIO_PRESSURE] = "pressure", 73ac216aa2SHarald Geyer [IIO_HUMIDITYRELATIVE] = "humidityrelative", 7455aebeb9SDaniel Baluta [IIO_ACTIVITY] = "activity", 75a88bfe78SIrina Tirdea [IIO_STEPS] = "steps", 7672c66644SIrina Tirdea [IIO_ENERGY] = "energy", 77cc3c9eecSIrina Tirdea [IIO_DISTANCE] = "distance", 785a1a9329SIrina Tirdea [IIO_VELOCITY] = "velocity", 798ff6b3bcSMatt Ranostay [IIO_CONCENTRATION] = "concentration", 80d38d5469SMatt Ranostay [IIO_RESISTANCE] = "resistance", 81ecb3a7ccSMatt Ranostay [IIO_PH] = "ph", 82d409404cSPeter Meerwald-Stadler [IIO_UVINDEX] = "uvindex", 834b9d2090SMatt Ranostay [IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity", 841a8f324aSWilliam Breathitt Gray [IIO_COUNT] = "count", 851a8f324aSWilliam Breathitt Gray [IIO_INDEX] = "index", 86571299d0SSong Hongyan [IIO_GRAVITY] = "gravity", 873055a6cfSEugen Hristev [IIO_POSITIONRELATIVE] = "positionrelative", 88c73314e6SMathieu Othacehe [IIO_PHASE] = "phase", 8917abc9ecSTomasz Duszynski [IIO_MASSCONCENTRATION] = "massconcentration", 90a980e046SJonathan Cameron }; 91a980e046SJonathan Cameron 92a980e046SJonathan Cameron static const char * const iio_modifier_names[] = { 93a980e046SJonathan Cameron [IIO_MOD_X] = "x", 94a980e046SJonathan Cameron [IIO_MOD_Y] = "y", 95a980e046SJonathan Cameron [IIO_MOD_Z] = "z", 964b8d8015SPeter Meerwald [IIO_MOD_X_AND_Y] = "x&y", 974b8d8015SPeter Meerwald [IIO_MOD_X_AND_Z] = "x&z", 984b8d8015SPeter Meerwald [IIO_MOD_Y_AND_Z] = "y&z", 994b8d8015SPeter Meerwald [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z", 1004b8d8015SPeter Meerwald [IIO_MOD_X_OR_Y] = "x|y", 1014b8d8015SPeter Meerwald [IIO_MOD_X_OR_Z] = "x|z", 1024b8d8015SPeter Meerwald [IIO_MOD_Y_OR_Z] = "y|z", 1034b8d8015SPeter Meerwald [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z", 1048f5879b2SJonathan Cameron [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)", 105cf82cb81SJonathan Cameron [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2", 106a980e046SJonathan Cameron [IIO_MOD_LIGHT_BOTH] = "both", 107a980e046SJonathan Cameron [IIO_MOD_LIGHT_IR] = "ir", 10821cd1fabSJon Brenner [IIO_MOD_LIGHT_CLEAR] = "clear", 10921cd1fabSJon Brenner [IIO_MOD_LIGHT_RED] = "red", 11021cd1fabSJon Brenner [IIO_MOD_LIGHT_GREEN] = "green", 11121cd1fabSJon Brenner [IIO_MOD_LIGHT_BLUE] = "blue", 1122c5ff1f9SPeter Meerwald-Stadler [IIO_MOD_LIGHT_UV] = "uv", 113c0e4e0fdSMaxime Roussin-Bélanger [IIO_MOD_LIGHT_DUV] = "duv", 1145082f405SSrinivas Pandruvada [IIO_MOD_QUATERNION] = "quaternion", 115638b43b3SPeter Meerwald [IIO_MOD_TEMP_AMBIENT] = "ambient", 116638b43b3SPeter Meerwald [IIO_MOD_TEMP_OBJECT] = "object", 11711b8ddabSReyad Attiyat [IIO_MOD_NORTH_MAGN] = "from_north_magnetic", 11811b8ddabSReyad Attiyat [IIO_MOD_NORTH_TRUE] = "from_north_true", 11911b8ddabSReyad Attiyat [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp", 12011b8ddabSReyad Attiyat [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp", 12155aebeb9SDaniel Baluta [IIO_MOD_RUNNING] = "running", 12255aebeb9SDaniel Baluta [IIO_MOD_JOGGING] = "jogging", 12355aebeb9SDaniel Baluta [IIO_MOD_WALKING] = "walking", 12455aebeb9SDaniel Baluta [IIO_MOD_STILL] = "still", 1255a1a9329SIrina Tirdea [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)", 1261ce87f21SLars-Peter Clausen [IIO_MOD_I] = "i", 1271ce87f21SLars-Peter Clausen [IIO_MOD_Q] = "q", 1288ff6b3bcSMatt Ranostay [IIO_MOD_CO2] = "co2", 1298ff6b3bcSMatt Ranostay [IIO_MOD_VOC] = "voc", 13017abc9ecSTomasz Duszynski [IIO_MOD_PM1] = "pm1", 13117abc9ecSTomasz Duszynski [IIO_MOD_PM2P5] = "pm2p5", 13217abc9ecSTomasz Duszynski [IIO_MOD_PM4] = "pm4", 13317abc9ecSTomasz Duszynski [IIO_MOD_PM10] = "pm10", 13425f02d32SMatt Ranostay [IIO_MOD_ETHANOL] = "ethanol", 13525f02d32SMatt Ranostay [IIO_MOD_H2] = "h2", 1364ffa22fdSMatt Ranostay [IIO_MOD_O2] = "o2", 137bcc57a48SAndrea Merello [IIO_MOD_LINEAR_X] = "linear_x", 138bcc57a48SAndrea Merello [IIO_MOD_LINEAR_Y] = "linear_y", 139bcc57a48SAndrea Merello [IIO_MOD_LINEAR_Z] = "linear_z", 140dcedf145SAndrea Merello [IIO_MOD_PITCH] = "pitch", 141dcedf145SAndrea Merello [IIO_MOD_YAW] = "yaw", 142dcedf145SAndrea Merello [IIO_MOD_ROLL] = "roll", 143a980e046SJonathan Cameron }; 144a980e046SJonathan Cameron 145a980e046SJonathan Cameron /* relies on pairs of these shared then separate */ 146a980e046SJonathan Cameron static const char * const iio_chan_info_postfix[] = { 147a980e046SJonathan Cameron [IIO_CHAN_INFO_RAW] = "raw", 148a980e046SJonathan Cameron [IIO_CHAN_INFO_PROCESSED] = "input", 149a980e046SJonathan Cameron [IIO_CHAN_INFO_SCALE] = "scale", 150a980e046SJonathan Cameron [IIO_CHAN_INFO_OFFSET] = "offset", 151a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBSCALE] = "calibscale", 152a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBBIAS] = "calibbias", 153a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK] = "peak_raw", 154a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale", 155a980e046SJonathan Cameron [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw", 156a980e046SJonathan Cameron [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw", 157a980e046SJonathan Cameron [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY] 158a980e046SJonathan Cameron = "filter_low_pass_3db_frequency", 1593f7f642bSMartin Fuzzey [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY] 1603f7f642bSMartin Fuzzey = "filter_high_pass_3db_frequency", 161a980e046SJonathan Cameron [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency", 162a6b12855SMichael Hennerich [IIO_CHAN_INFO_FREQUENCY] = "frequency", 163a6b12855SMichael Hennerich [IIO_CHAN_INFO_PHASE] = "phase", 164b65d6212SMichael Hennerich [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain", 1657c9ab035Ssrinivas pandruvada [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis", 1661c71a286SYe Xiang [IIO_CHAN_INFO_HYSTERESIS_RELATIVE] = "hysteresis_relative", 167899d90bdSPeter Meerwald [IIO_CHAN_INFO_INT_TIME] = "integration_time", 168a88bfe78SIrina Tirdea [IIO_CHAN_INFO_ENABLE] = "en", 169bcdf28fbSIrina Tirdea [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight", 170d37f6836SIrina Tirdea [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight", 1712f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count", 1722f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time", 173c8a85854SVianney le Clément de Saint-Marcq [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity", 174faaa4495SIrina Tirdea [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio", 175ddfb97d8SAndrea Merello [IIO_CHAN_INFO_THERMOCOUPLE_TYPE] = "thermocouple_type", 176dde8ceecSCrt Mori [IIO_CHAN_INFO_CALIBAMBIENT] = "calibambient", 1775c64990bSJonathan Cameron [IIO_CHAN_INFO_ZEROPOINT] = "zeropoint", 178a980e046SJonathan Cameron }; 17915ea2878SJonathan Cameron /** 18015ea2878SJonathan Cameron * iio_device_id() - query the unique ID for the device 18115ea2878SJonathan Cameron * @indio_dev: Device structure whose ID is being queried 18215ea2878SJonathan Cameron * 18315ea2878SJonathan Cameron * The IIO device ID is a unique index used for example for the naming 18415ea2878SJonathan Cameron * of the character device /dev/iio\:device[ID] 18515ea2878SJonathan Cameron */ 18615ea2878SJonathan Cameron int iio_device_id(struct iio_dev *indio_dev) 18715ea2878SJonathan Cameron { 18815ea2878SJonathan Cameron struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 18915ea2878SJonathan Cameron 19015ea2878SJonathan Cameron return iio_dev_opaque->id; 19115ea2878SJonathan Cameron } 19215ea2878SJonathan Cameron EXPORT_SYMBOL_GPL(iio_device_id); 193a980e046SJonathan Cameron 194d9a0e73cSLars-Peter Clausen /** 1952f53b4adSMiquel Raynal * iio_buffer_enabled() - helper function to test if the buffer is enabled 1962f53b4adSMiquel Raynal * @indio_dev: IIO device structure for device 1972f53b4adSMiquel Raynal */ 1982f53b4adSMiquel Raynal bool iio_buffer_enabled(struct iio_dev *indio_dev) 1992f53b4adSMiquel Raynal { 20051570c9dSMiquel Raynal struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 20151570c9dSMiquel Raynal 20251570c9dSMiquel Raynal return iio_dev_opaque->currentmode 2032f53b4adSMiquel Raynal & (INDIO_BUFFER_TRIGGERED | INDIO_BUFFER_HARDWARE | 2042f53b4adSMiquel Raynal INDIO_BUFFER_SOFTWARE); 2052f53b4adSMiquel Raynal } 2062f53b4adSMiquel Raynal EXPORT_SYMBOL_GPL(iio_buffer_enabled); 2072f53b4adSMiquel Raynal 20896fb1b67SAlexandru Ardelean #if defined(CONFIG_DEBUG_FS) 209c9561fd2SJonathan Cameron /* 21096fb1b67SAlexandru Ardelean * There's also a CONFIG_DEBUG_FS guard in include/linux/iio/iio.h for 21196fb1b67SAlexandru Ardelean * iio_get_debugfs_dentry() to make it inline if CONFIG_DEBUG_FS is undefined 21296fb1b67SAlexandru Ardelean */ 21396fb1b67SAlexandru Ardelean struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev) 21496fb1b67SAlexandru Ardelean { 21596fb1b67SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 2169d9ec8d0SJoe Simmons-Talbott 21796fb1b67SAlexandru Ardelean return iio_dev_opaque->debugfs_dentry; 21896fb1b67SAlexandru Ardelean } 21996fb1b67SAlexandru Ardelean EXPORT_SYMBOL_GPL(iio_get_debugfs_dentry); 22096fb1b67SAlexandru Ardelean #endif 22196fb1b67SAlexandru Ardelean 222a7e57dceSSachin Kamat /** 223a7e57dceSSachin Kamat * iio_find_channel_from_si() - get channel from its scan index 224a7e57dceSSachin Kamat * @indio_dev: device 225a7e57dceSSachin Kamat * @si: scan index to match 226a7e57dceSSachin Kamat */ 227a980e046SJonathan Cameron const struct iio_chan_spec 228a980e046SJonathan Cameron *iio_find_channel_from_si(struct iio_dev *indio_dev, int si) 229a980e046SJonathan Cameron { 230a980e046SJonathan Cameron int i; 231a980e046SJonathan Cameron 232a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) 233a980e046SJonathan Cameron if (indio_dev->channels[i].scan_index == si) 234a980e046SJonathan Cameron return &indio_dev->channels[i]; 235a980e046SJonathan Cameron return NULL; 236a980e046SJonathan Cameron } 237a980e046SJonathan Cameron 238a980e046SJonathan Cameron /* This turns up an awful lot */ 239a980e046SJonathan Cameron ssize_t iio_read_const_attr(struct device *dev, 240a980e046SJonathan Cameron struct device_attribute *attr, 241a980e046SJonathan Cameron char *buf) 242a980e046SJonathan Cameron { 24383ca56b6SLars-Peter Clausen return sysfs_emit(buf, "%s\n", to_iio_const_attr(attr)->string); 244a980e046SJonathan Cameron } 245a980e046SJonathan Cameron EXPORT_SYMBOL(iio_read_const_attr); 246a980e046SJonathan Cameron 24769f0793eSGwendal Grignou /** 24869f0793eSGwendal Grignou * iio_device_set_clock() - Set current timestamping clock for the device 24969f0793eSGwendal Grignou * @indio_dev: IIO device structure containing the device 25069f0793eSGwendal Grignou * @clock_id: timestamping clock posix identifier to set. 25169f0793eSGwendal Grignou */ 25269f0793eSGwendal Grignou int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id) 253bc2b7dabSGregor Boirie { 254bc2b7dabSGregor Boirie int ret; 255fa83c3baSAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 256fa83c3baSAlexandru Ardelean const struct iio_event_interface *ev_int = iio_dev_opaque->event_interface; 257bc2b7dabSGregor Boirie 25816afe125SNuno Sá ret = mutex_lock_interruptible(&iio_dev_opaque->mlock); 259bc2b7dabSGregor Boirie if (ret) 260bc2b7dabSGregor Boirie return ret; 261bc2b7dabSGregor Boirie if ((ev_int && iio_event_enabled(ev_int)) || 262bc2b7dabSGregor Boirie iio_buffer_enabled(indio_dev)) { 26316afe125SNuno Sá mutex_unlock(&iio_dev_opaque->mlock); 264bc2b7dabSGregor Boirie return -EBUSY; 265bc2b7dabSGregor Boirie } 26662a486c4SJonathan Cameron iio_dev_opaque->clock_id = clock_id; 26716afe125SNuno Sá mutex_unlock(&iio_dev_opaque->mlock); 268bc2b7dabSGregor Boirie 269bc2b7dabSGregor Boirie return 0; 270bc2b7dabSGregor Boirie } 27169f0793eSGwendal Grignou EXPORT_SYMBOL(iio_device_set_clock); 272bc2b7dabSGregor Boirie 273bc2b7dabSGregor Boirie /** 27462a486c4SJonathan Cameron * iio_device_get_clock() - Retrieve current timestamping clock for the device 27562a486c4SJonathan Cameron * @indio_dev: IIO device structure containing the device 27662a486c4SJonathan Cameron */ 27762a486c4SJonathan Cameron clockid_t iio_device_get_clock(const struct iio_dev *indio_dev) 27862a486c4SJonathan Cameron { 27962a486c4SJonathan Cameron struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 28062a486c4SJonathan Cameron 28162a486c4SJonathan Cameron return iio_dev_opaque->clock_id; 28262a486c4SJonathan Cameron } 28362a486c4SJonathan Cameron EXPORT_SYMBOL(iio_device_get_clock); 28462a486c4SJonathan Cameron 28562a486c4SJonathan Cameron /** 286bc2b7dabSGregor Boirie * iio_get_time_ns() - utility function to get a time stamp for events etc 287bc2b7dabSGregor Boirie * @indio_dev: device 288bc2b7dabSGregor Boirie */ 289bc2b7dabSGregor Boirie s64 iio_get_time_ns(const struct iio_dev *indio_dev) 290bc2b7dabSGregor Boirie { 29145e7f5d2SArnd Bergmann struct timespec64 tp; 292bc2b7dabSGregor Boirie 293bc2b7dabSGregor Boirie switch (iio_device_get_clock(indio_dev)) { 294bc2b7dabSGregor Boirie case CLOCK_REALTIME: 29545e7f5d2SArnd Bergmann return ktime_get_real_ns(); 296bc2b7dabSGregor Boirie case CLOCK_MONOTONIC: 29745e7f5d2SArnd Bergmann return ktime_get_ns(); 298bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_RAW: 29945e7f5d2SArnd Bergmann return ktime_get_raw_ns(); 300bc2b7dabSGregor Boirie case CLOCK_REALTIME_COARSE: 30145e7f5d2SArnd Bergmann return ktime_to_ns(ktime_get_coarse_real()); 302bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_COARSE: 30345e7f5d2SArnd Bergmann ktime_get_coarse_ts64(&tp); 30445e7f5d2SArnd Bergmann return timespec64_to_ns(&tp); 305bc2b7dabSGregor Boirie case CLOCK_BOOTTIME: 3069285ec4cSJason A. Donenfeld return ktime_get_boottime_ns(); 307bc2b7dabSGregor Boirie case CLOCK_TAI: 3089285ec4cSJason A. Donenfeld return ktime_get_clocktai_ns(); 309bc2b7dabSGregor Boirie default: 310bc2b7dabSGregor Boirie BUG(); 311bc2b7dabSGregor Boirie } 312bc2b7dabSGregor Boirie } 313bc2b7dabSGregor Boirie EXPORT_SYMBOL(iio_get_time_ns); 314bc2b7dabSGregor Boirie 315a980e046SJonathan Cameron static int __init iio_init(void) 316a980e046SJonathan Cameron { 317a980e046SJonathan Cameron int ret; 318a980e046SJonathan Cameron 319a980e046SJonathan Cameron /* Register sysfs bus */ 320a980e046SJonathan Cameron ret = bus_register(&iio_bus_type); 321a980e046SJonathan Cameron if (ret < 0) { 3223176dd5dSSachin Kamat pr_err("could not register bus type\n"); 323a980e046SJonathan Cameron goto error_nothing; 324a980e046SJonathan Cameron } 325a980e046SJonathan Cameron 326a980e046SJonathan Cameron ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio"); 327a980e046SJonathan Cameron if (ret < 0) { 3283176dd5dSSachin Kamat pr_err("failed to allocate char dev region\n"); 329a980e046SJonathan Cameron goto error_unregister_bus_type; 330a980e046SJonathan Cameron } 331a980e046SJonathan Cameron 332a980e046SJonathan Cameron iio_debugfs_dentry = debugfs_create_dir("iio", NULL); 333a980e046SJonathan Cameron 334a980e046SJonathan Cameron return 0; 335a980e046SJonathan Cameron 336a980e046SJonathan Cameron error_unregister_bus_type: 337a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 338a980e046SJonathan Cameron error_nothing: 339a980e046SJonathan Cameron return ret; 340a980e046SJonathan Cameron } 341a980e046SJonathan Cameron 342a980e046SJonathan Cameron static void __exit iio_exit(void) 343a980e046SJonathan Cameron { 344a980e046SJonathan Cameron if (iio_devt) 345a980e046SJonathan Cameron unregister_chrdev_region(iio_devt, IIO_DEV_MAX); 346a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 347a980e046SJonathan Cameron debugfs_remove(iio_debugfs_dentry); 348a980e046SJonathan Cameron } 349a980e046SJonathan Cameron 350a980e046SJonathan Cameron #if defined(CONFIG_DEBUG_FS) 351a980e046SJonathan Cameron static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf, 352a980e046SJonathan Cameron size_t count, loff_t *ppos) 353a980e046SJonathan Cameron { 354a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 35596fb1b67SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 356abeba59fSJoe Simmons-Talbott unsigned int val = 0; 357a980e046SJonathan Cameron int ret; 358a980e046SJonathan Cameron 3592ddc982aSAlexandru Tachici if (*ppos > 0) 3602ddc982aSAlexandru Tachici return simple_read_from_buffer(userbuf, count, ppos, 36196fb1b67SAlexandru Ardelean iio_dev_opaque->read_buf, 36296fb1b67SAlexandru Ardelean iio_dev_opaque->read_buf_len); 3632ddc982aSAlexandru Tachici 364a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, 36596fb1b67SAlexandru Ardelean iio_dev_opaque->cached_reg_addr, 366a980e046SJonathan Cameron 0, &val); 3673d62c78aSMatt Fornero if (ret) { 368a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__); 3693d62c78aSMatt Fornero return ret; 3703d62c78aSMatt Fornero } 371a980e046SJonathan Cameron 37296fb1b67SAlexandru Ardelean iio_dev_opaque->read_buf_len = snprintf(iio_dev_opaque->read_buf, 37396fb1b67SAlexandru Ardelean sizeof(iio_dev_opaque->read_buf), 3742ddc982aSAlexandru Tachici "0x%X\n", val); 375a980e046SJonathan Cameron 3762ddc982aSAlexandru Tachici return simple_read_from_buffer(userbuf, count, ppos, 37796fb1b67SAlexandru Ardelean iio_dev_opaque->read_buf, 37896fb1b67SAlexandru Ardelean iio_dev_opaque->read_buf_len); 379a980e046SJonathan Cameron } 380a980e046SJonathan Cameron 381a980e046SJonathan Cameron static ssize_t iio_debugfs_write_reg(struct file *file, 382a980e046SJonathan Cameron const char __user *userbuf, size_t count, loff_t *ppos) 383a980e046SJonathan Cameron { 384a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 38596fb1b67SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 386abeba59fSJoe Simmons-Talbott unsigned int reg, val; 387a980e046SJonathan Cameron char buf[80]; 388a980e046SJonathan Cameron int ret; 389a980e046SJonathan Cameron 390a980e046SJonathan Cameron count = min_t(size_t, count, (sizeof(buf)-1)); 391a980e046SJonathan Cameron if (copy_from_user(buf, userbuf, count)) 392a980e046SJonathan Cameron return -EFAULT; 393a980e046SJonathan Cameron 394a980e046SJonathan Cameron buf[count] = 0; 395a980e046SJonathan Cameron 396a980e046SJonathan Cameron ret = sscanf(buf, "%i %i", ®, &val); 397a980e046SJonathan Cameron 398a980e046SJonathan Cameron switch (ret) { 399a980e046SJonathan Cameron case 1: 40096fb1b67SAlexandru Ardelean iio_dev_opaque->cached_reg_addr = reg; 401a980e046SJonathan Cameron break; 402a980e046SJonathan Cameron case 2: 40396fb1b67SAlexandru Ardelean iio_dev_opaque->cached_reg_addr = reg; 404a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, reg, 405a980e046SJonathan Cameron val, NULL); 406a980e046SJonathan Cameron if (ret) { 407a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: write failed\n", 408a980e046SJonathan Cameron __func__); 409a980e046SJonathan Cameron return ret; 410a980e046SJonathan Cameron } 411a980e046SJonathan Cameron break; 412a980e046SJonathan Cameron default: 413a980e046SJonathan Cameron return -EINVAL; 414a980e046SJonathan Cameron } 415a980e046SJonathan Cameron 416a980e046SJonathan Cameron return count; 417a980e046SJonathan Cameron } 418a980e046SJonathan Cameron 419a980e046SJonathan Cameron static const struct file_operations iio_debugfs_reg_fops = { 4205a28c873SAxel Lin .open = simple_open, 421a980e046SJonathan Cameron .read = iio_debugfs_read_reg, 422a980e046SJonathan Cameron .write = iio_debugfs_write_reg, 423a980e046SJonathan Cameron }; 424a980e046SJonathan Cameron 425a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 426a980e046SJonathan Cameron { 42796fb1b67SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 4289d9ec8d0SJoe Simmons-Talbott 42996fb1b67SAlexandru Ardelean debugfs_remove_recursive(iio_dev_opaque->debugfs_dentry); 430a980e046SJonathan Cameron } 431a980e046SJonathan Cameron 4328915aacaSGreg Kroah-Hartman static void iio_device_register_debugfs(struct iio_dev *indio_dev) 433a980e046SJonathan Cameron { 43496fb1b67SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque; 43596fb1b67SAlexandru Ardelean 436a980e046SJonathan Cameron if (indio_dev->info->debugfs_reg_access == NULL) 4378915aacaSGreg Kroah-Hartman return; 438a980e046SJonathan Cameron 439abd5a2fbSAxel Lin if (!iio_debugfs_dentry) 4408915aacaSGreg Kroah-Hartman return; 441a980e046SJonathan Cameron 44296fb1b67SAlexandru Ardelean iio_dev_opaque = to_iio_dev_opaque(indio_dev); 44396fb1b67SAlexandru Ardelean 44496fb1b67SAlexandru Ardelean iio_dev_opaque->debugfs_dentry = 445a980e046SJonathan Cameron debugfs_create_dir(dev_name(&indio_dev->dev), 446a980e046SJonathan Cameron iio_debugfs_dentry); 447a980e046SJonathan Cameron 4488915aacaSGreg Kroah-Hartman debugfs_create_file("direct_reg_access", 0644, 44996fb1b67SAlexandru Ardelean iio_dev_opaque->debugfs_dentry, indio_dev, 4508915aacaSGreg Kroah-Hartman &iio_debugfs_reg_fops); 451a980e046SJonathan Cameron } 452a980e046SJonathan Cameron #else 4538915aacaSGreg Kroah-Hartman static void iio_device_register_debugfs(struct iio_dev *indio_dev) 454a980e046SJonathan Cameron { 455a980e046SJonathan Cameron } 456a980e046SJonathan Cameron 457a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 458a980e046SJonathan Cameron { 459a980e046SJonathan Cameron } 460a980e046SJonathan Cameron #endif /* CONFIG_DEBUG_FS */ 461a980e046SJonathan Cameron 462a980e046SJonathan Cameron static ssize_t iio_read_channel_ext_info(struct device *dev, 463a980e046SJonathan Cameron struct device_attribute *attr, 464a980e046SJonathan Cameron char *buf) 465a980e046SJonathan Cameron { 466e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 467a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 468a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 469a980e046SJonathan Cameron 470a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 471a980e046SJonathan Cameron 472fc6d1139SMichael Hennerich return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf); 473a980e046SJonathan Cameron } 474a980e046SJonathan Cameron 475a980e046SJonathan Cameron static ssize_t iio_write_channel_ext_info(struct device *dev, 476a980e046SJonathan Cameron struct device_attribute *attr, 477a980e046SJonathan Cameron const char *buf, 478a980e046SJonathan Cameron size_t len) 479a980e046SJonathan Cameron { 480e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 481a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 482a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 483a980e046SJonathan Cameron 484a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 485a980e046SJonathan Cameron 486fc6d1139SMichael Hennerich return ext_info->write(indio_dev, ext_info->private, 487fc6d1139SMichael Hennerich this_attr->c, buf, len); 488a980e046SJonathan Cameron } 489a980e046SJonathan Cameron 4905212cc8aSLars-Peter Clausen ssize_t iio_enum_available_read(struct iio_dev *indio_dev, 4915212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 4925212cc8aSLars-Peter Clausen { 4935212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 4945212cc8aSLars-Peter Clausen unsigned int i; 4955212cc8aSLars-Peter Clausen size_t len = 0; 4965212cc8aSLars-Peter Clausen 4975212cc8aSLars-Peter Clausen if (!e->num_items) 4985212cc8aSLars-Peter Clausen return 0; 4995212cc8aSLars-Peter Clausen 500d9a0e73cSLars-Peter Clausen for (i = 0; i < e->num_items; ++i) { 501d9a0e73cSLars-Peter Clausen if (!e->items[i]) 502d9a0e73cSLars-Peter Clausen continue; 5030207483bSLars-Peter Clausen len += sysfs_emit_at(buf, len, "%s ", e->items[i]); 504d9a0e73cSLars-Peter Clausen } 5055212cc8aSLars-Peter Clausen 5065212cc8aSLars-Peter Clausen /* replace last space with a newline */ 5075212cc8aSLars-Peter Clausen buf[len - 1] = '\n'; 5085212cc8aSLars-Peter Clausen 5095212cc8aSLars-Peter Clausen return len; 5105212cc8aSLars-Peter Clausen } 5115212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_available_read); 5125212cc8aSLars-Peter Clausen 5135212cc8aSLars-Peter Clausen ssize_t iio_enum_read(struct iio_dev *indio_dev, 5145212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 5155212cc8aSLars-Peter Clausen { 5165212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 5175212cc8aSLars-Peter Clausen int i; 5185212cc8aSLars-Peter Clausen 5195212cc8aSLars-Peter Clausen if (!e->get) 5205212cc8aSLars-Peter Clausen return -EINVAL; 5215212cc8aSLars-Peter Clausen 5225212cc8aSLars-Peter Clausen i = e->get(indio_dev, chan); 5235212cc8aSLars-Peter Clausen if (i < 0) 5245212cc8aSLars-Peter Clausen return i; 525d9a0e73cSLars-Peter Clausen else if (i >= e->num_items || !e->items[i]) 5265212cc8aSLars-Peter Clausen return -EINVAL; 5275212cc8aSLars-Peter Clausen 52883ca56b6SLars-Peter Clausen return sysfs_emit(buf, "%s\n", e->items[i]); 5295212cc8aSLars-Peter Clausen } 5305212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_read); 5315212cc8aSLars-Peter Clausen 5325212cc8aSLars-Peter Clausen ssize_t iio_enum_write(struct iio_dev *indio_dev, 5335212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, const char *buf, 5345212cc8aSLars-Peter Clausen size_t len) 5355212cc8aSLars-Peter Clausen { 5365212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 5375212cc8aSLars-Peter Clausen int ret; 5385212cc8aSLars-Peter Clausen 5395212cc8aSLars-Peter Clausen if (!e->set) 5405212cc8aSLars-Peter Clausen return -EINVAL; 5415212cc8aSLars-Peter Clausen 542*122fb873SAndy Shevchenko ret = __sysfs_match_string(e->items, e->num_items, buf); 54302e9a0ffSAndy Shevchenko if (ret < 0) 54402e9a0ffSAndy Shevchenko return ret; 5455212cc8aSLars-Peter Clausen 54602e9a0ffSAndy Shevchenko ret = e->set(indio_dev, chan, ret); 5475212cc8aSLars-Peter Clausen return ret ? ret : len; 5485212cc8aSLars-Peter Clausen } 5495212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_write); 5505212cc8aSLars-Peter Clausen 551dfc57732SGregor Boirie static const struct iio_mount_matrix iio_mount_idmatrix = { 552dfc57732SGregor Boirie .rotation = { 553dfc57732SGregor Boirie "1", "0", "0", 554dfc57732SGregor Boirie "0", "1", "0", 555dfc57732SGregor Boirie "0", "0", "1" 556dfc57732SGregor Boirie } 557dfc57732SGregor Boirie }; 558dfc57732SGregor Boirie 559dfc57732SGregor Boirie static int iio_setup_mount_idmatrix(const struct device *dev, 560dfc57732SGregor Boirie struct iio_mount_matrix *matrix) 561dfc57732SGregor Boirie { 562dfc57732SGregor Boirie *matrix = iio_mount_idmatrix; 563dfc57732SGregor Boirie dev_info(dev, "mounting matrix not found: using identity...\n"); 564dfc57732SGregor Boirie return 0; 565dfc57732SGregor Boirie } 566dfc57732SGregor Boirie 567dfc57732SGregor Boirie ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv, 568dfc57732SGregor Boirie const struct iio_chan_spec *chan, char *buf) 569dfc57732SGregor Boirie { 570dfc57732SGregor Boirie const struct iio_mount_matrix *mtx = ((iio_get_mount_matrix_t *) 571dfc57732SGregor Boirie priv)(indio_dev, chan); 572dfc57732SGregor Boirie 573dfc57732SGregor Boirie if (IS_ERR(mtx)) 574dfc57732SGregor Boirie return PTR_ERR(mtx); 575dfc57732SGregor Boirie 576dfc57732SGregor Boirie if (!mtx) 577dfc57732SGregor Boirie mtx = &iio_mount_idmatrix; 578dfc57732SGregor Boirie 57983ca56b6SLars-Peter Clausen return sysfs_emit(buf, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n", 580dfc57732SGregor Boirie mtx->rotation[0], mtx->rotation[1], mtx->rotation[2], 581dfc57732SGregor Boirie mtx->rotation[3], mtx->rotation[4], mtx->rotation[5], 582dfc57732SGregor Boirie mtx->rotation[6], mtx->rotation[7], mtx->rotation[8]); 583dfc57732SGregor Boirie } 584dfc57732SGregor Boirie EXPORT_SYMBOL_GPL(iio_show_mount_matrix); 585dfc57732SGregor Boirie 586dfc57732SGregor Boirie /** 587fb158971SAndy Shevchenko * iio_read_mount_matrix() - retrieve iio device mounting matrix from 588fb158971SAndy Shevchenko * device "mount-matrix" property 589dfc57732SGregor Boirie * @dev: device the mounting matrix property is assigned to 590dfc57732SGregor Boirie * @matrix: where to store retrieved matrix 591dfc57732SGregor Boirie * 592dfc57732SGregor Boirie * If device is assigned no mounting matrix property, a default 3x3 identity 593dfc57732SGregor Boirie * matrix will be filled in. 594dfc57732SGregor Boirie * 595dfc57732SGregor Boirie * Return: 0 if success, or a negative error code on failure. 596dfc57732SGregor Boirie */ 597b892770aSAndy Shevchenko int iio_read_mount_matrix(struct device *dev, struct iio_mount_matrix *matrix) 598dfc57732SGregor Boirie { 599fb158971SAndy Shevchenko size_t len = ARRAY_SIZE(iio_mount_idmatrix.rotation); 600fb158971SAndy Shevchenko int err; 601dfc57732SGregor Boirie 602b892770aSAndy Shevchenko err = device_property_read_string_array(dev, "mount-matrix", matrix->rotation, len); 603fb158971SAndy Shevchenko if (err == len) 604dfc57732SGregor Boirie return 0; 605dfc57732SGregor Boirie 606dfc57732SGregor Boirie if (err >= 0) 607dfc57732SGregor Boirie /* Invalid number of matrix entries. */ 608dfc57732SGregor Boirie return -EINVAL; 609dfc57732SGregor Boirie 610dfc57732SGregor Boirie if (err != -EINVAL) 611dfc57732SGregor Boirie /* Invalid matrix declaration format. */ 612dfc57732SGregor Boirie return err; 613dfc57732SGregor Boirie 614dfc57732SGregor Boirie /* Matrix was not declared at all: fallback to identity. */ 615dfc57732SGregor Boirie return iio_setup_mount_idmatrix(dev, matrix); 616dfc57732SGregor Boirie } 617fb158971SAndy Shevchenko EXPORT_SYMBOL(iio_read_mount_matrix); 618dfc57732SGregor Boirie 6196b92ba0aSLars-Peter Clausen static ssize_t __iio_format_value(char *buf, size_t offset, unsigned int type, 62051239600SJonathan Cameron int size, const int *vals) 62151239600SJonathan Cameron { 62251239600SJonathan Cameron int tmp0, tmp1; 6230e7a3978SAnand Ashok Dumbre s64 tmp2; 62451239600SJonathan Cameron bool scale_db = false; 62551239600SJonathan Cameron 62651239600SJonathan Cameron switch (type) { 62751239600SJonathan Cameron case IIO_VAL_INT: 6286b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "%d", vals[0]); 62951239600SJonathan Cameron case IIO_VAL_INT_PLUS_MICRO_DB: 63051239600SJonathan Cameron scale_db = true; 631df561f66SGustavo A. R. Silva fallthrough; 63251239600SJonathan Cameron case IIO_VAL_INT_PLUS_MICRO: 63351239600SJonathan Cameron if (vals[1] < 0) 6346b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "-%d.%06u%s", 6356b92ba0aSLars-Peter Clausen abs(vals[0]), -vals[1], 63651239600SJonathan Cameron scale_db ? " dB" : ""); 6376b92ba0aSLars-Peter Clausen else 6386b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "%d.%06u%s", vals[0], 6396b92ba0aSLars-Peter Clausen vals[1], scale_db ? " dB" : ""); 64051239600SJonathan Cameron case IIO_VAL_INT_PLUS_NANO: 64151239600SJonathan Cameron if (vals[1] < 0) 6426b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "-%d.%09u", 6436b92ba0aSLars-Peter Clausen abs(vals[0]), -vals[1]); 64451239600SJonathan Cameron else 6456b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "%d.%09u", vals[0], 6466b92ba0aSLars-Peter Clausen vals[1]); 64751239600SJonathan Cameron case IIO_VAL_FRACTIONAL: 6480e7a3978SAnand Ashok Dumbre tmp2 = div_s64((s64)vals[0] * 1000000000LL, vals[1]); 64951239600SJonathan Cameron tmp1 = vals[1]; 6500e7a3978SAnand Ashok Dumbre tmp0 = (int)div_s64_rem(tmp2, 1000000000, &tmp1); 6510e7a3978SAnand Ashok Dumbre if ((tmp2 < 0) && (tmp0 == 0)) 6526b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "-0.%09u", abs(tmp1)); 6530e7a3978SAnand Ashok Dumbre else 6546b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "%d.%09u", tmp0, 6556b92ba0aSLars-Peter Clausen abs(tmp1)); 65651239600SJonathan Cameron case IIO_VAL_FRACTIONAL_LOG2: 6572646a95dSLars-Peter Clausen tmp2 = shift_right((s64)vals[0] * 1000000000LL, vals[1]); 6582646a95dSLars-Peter Clausen tmp0 = (int)div_s64_rem(tmp2, 1000000000LL, &tmp1); 65938a52cdeSLars-Peter Clausen if (tmp0 == 0 && tmp2 < 0) 6606b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "-0.%09u", abs(tmp1)); 66138a52cdeSLars-Peter Clausen else 6626b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "%d.%09u", tmp0, 6636b92ba0aSLars-Peter Clausen abs(tmp1)); 66451239600SJonathan Cameron case IIO_VAL_INT_MULTIPLE: 66551239600SJonathan Cameron { 66651239600SJonathan Cameron int i; 66751239600SJonathan Cameron int l = 0; 66851239600SJonathan Cameron 6696b92ba0aSLars-Peter Clausen for (i = 0; i < size; ++i) 6706b92ba0aSLars-Peter Clausen l += sysfs_emit_at(buf, offset + l, "%d ", vals[i]); 67151239600SJonathan Cameron return l; 67251239600SJonathan Cameron } 6738cb34036SAndrea Merello case IIO_VAL_CHAR: 6746b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "%c", (char)vals[0]); 6756bb835f3SAndriy Tryshnivskyy case IIO_VAL_INT_64: 6766bb835f3SAndriy Tryshnivskyy tmp2 = (s64)((((u64)vals[1]) << 32) | (u32)vals[0]); 6776bb835f3SAndriy Tryshnivskyy return sysfs_emit_at(buf, offset, "%lld", tmp2); 67851239600SJonathan Cameron default: 67951239600SJonathan Cameron return 0; 68051239600SJonathan Cameron } 68151239600SJonathan Cameron } 68251239600SJonathan Cameron 6833661f3f5SLars-Peter Clausen /** 6843661f3f5SLars-Peter Clausen * iio_format_value() - Formats a IIO value into its string representation 6852498dcf6SCristina Opriceana * @buf: The buffer to which the formatted value gets written 68651239600SJonathan Cameron * which is assumed to be big enough (i.e. PAGE_SIZE). 687c175cb7cSRandy Dunlap * @type: One of the IIO_VAL_* constants. This decides how the val 6882498dcf6SCristina Opriceana * and val2 parameters are formatted. 6892498dcf6SCristina Opriceana * @size: Number of IIO value entries contained in vals 6902498dcf6SCristina Opriceana * @vals: Pointer to the values, exact meaning depends on the 6912498dcf6SCristina Opriceana * type parameter. 6922498dcf6SCristina Opriceana * 6932498dcf6SCristina Opriceana * Return: 0 by default, a negative number on failure or the 6942498dcf6SCristina Opriceana * total number of characters written for a type that belongs 695c175cb7cSRandy Dunlap * to the IIO_VAL_* constant. 6963661f3f5SLars-Peter Clausen */ 6979fbfb4b3SSrinivas Pandruvada ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals) 698a980e046SJonathan Cameron { 69951239600SJonathan Cameron ssize_t len; 700a980e046SJonathan Cameron 7016b92ba0aSLars-Peter Clausen len = __iio_format_value(buf, 0, type, size, vals); 70251239600SJonathan Cameron if (len >= PAGE_SIZE - 1) 70351239600SJonathan Cameron return -EFBIG; 7049fbfb4b3SSrinivas Pandruvada 7056b92ba0aSLars-Peter Clausen return len + sysfs_emit_at(buf, len, "\n"); 70667eedba3SMichael Hennerich } 7077d2c2acaSAndrew F. Davis EXPORT_SYMBOL_GPL(iio_format_value); 708a980e046SJonathan Cameron 7091d4ef9b3SCristian Pop static ssize_t iio_read_channel_label(struct device *dev, 7101d4ef9b3SCristian Pop struct device_attribute *attr, 7111d4ef9b3SCristian Pop char *buf) 7121d4ef9b3SCristian Pop { 7131d4ef9b3SCristian Pop struct iio_dev *indio_dev = dev_to_iio_dev(dev); 7141d4ef9b3SCristian Pop struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 7151d4ef9b3SCristian Pop 71613efdc3dSPaul Cercueil if (indio_dev->info->read_label) 7171d4ef9b3SCristian Pop return indio_dev->info->read_label(indio_dev, this_attr->c, buf); 71813efdc3dSPaul Cercueil 71913efdc3dSPaul Cercueil if (this_attr->c->extend_name) 720c3154defSLars-Peter Clausen return sysfs_emit(buf, "%s\n", this_attr->c->extend_name); 72113efdc3dSPaul Cercueil 72213efdc3dSPaul Cercueil return -EINVAL; 7231d4ef9b3SCristian Pop } 7241d4ef9b3SCristian Pop 7253661f3f5SLars-Peter Clausen static ssize_t iio_read_channel_info(struct device *dev, 7263661f3f5SLars-Peter Clausen struct device_attribute *attr, 7273661f3f5SLars-Peter Clausen char *buf) 7283661f3f5SLars-Peter Clausen { 7293661f3f5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 7303661f3f5SLars-Peter Clausen struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 7319fbfb4b3SSrinivas Pandruvada int vals[INDIO_MAX_RAW_ELEMENTS]; 7329fbfb4b3SSrinivas Pandruvada int ret; 7339fbfb4b3SSrinivas Pandruvada int val_len = 2; 7349fbfb4b3SSrinivas Pandruvada 7359fbfb4b3SSrinivas Pandruvada if (indio_dev->info->read_raw_multi) 7369fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c, 7379fbfb4b3SSrinivas Pandruvada INDIO_MAX_RAW_ELEMENTS, 7389fbfb4b3SSrinivas Pandruvada vals, &val_len, 7399fbfb4b3SSrinivas Pandruvada this_attr->address); 7409fbfb4b3SSrinivas Pandruvada else 7419fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw(indio_dev, this_attr->c, 7429fbfb4b3SSrinivas Pandruvada &vals[0], &vals[1], this_attr->address); 7433661f3f5SLars-Peter Clausen 7443661f3f5SLars-Peter Clausen if (ret < 0) 7453661f3f5SLars-Peter Clausen return ret; 7463661f3f5SLars-Peter Clausen 7479fbfb4b3SSrinivas Pandruvada return iio_format_value(buf, ret, val_len, vals); 7483661f3f5SLars-Peter Clausen } 7493661f3f5SLars-Peter Clausen 750eda20ba1SLars-Peter Clausen static ssize_t iio_format_list(char *buf, const int *vals, int type, int length, 751eda20ba1SLars-Peter Clausen const char *prefix, const char *suffix) 75251239600SJonathan Cameron { 753eda20ba1SLars-Peter Clausen ssize_t len; 754e08b60d3SLars-Peter Clausen int stride; 75551239600SJonathan Cameron int i; 756eda20ba1SLars-Peter Clausen 75751239600SJonathan Cameron switch (type) { 75851239600SJonathan Cameron case IIO_VAL_INT: 759e08b60d3SLars-Peter Clausen stride = 1; 76051239600SJonathan Cameron break; 76151239600SJonathan Cameron default: 762e08b60d3SLars-Peter Clausen stride = 2; 763e08b60d3SLars-Peter Clausen break; 764e08b60d3SLars-Peter Clausen } 765e08b60d3SLars-Peter Clausen 7666b92ba0aSLars-Peter Clausen len = sysfs_emit(buf, prefix); 767e08b60d3SLars-Peter Clausen 768e08b60d3SLars-Peter Clausen for (i = 0; i <= length - stride; i += stride) { 769e08b60d3SLars-Peter Clausen if (i != 0) { 7706b92ba0aSLars-Peter Clausen len += sysfs_emit_at(buf, len, " "); 77151239600SJonathan Cameron if (len >= PAGE_SIZE) 77251239600SJonathan Cameron return -EFBIG; 77351239600SJonathan Cameron } 774e08b60d3SLars-Peter Clausen 7756b92ba0aSLars-Peter Clausen len += __iio_format_value(buf, len, type, stride, &vals[i]); 776e08b60d3SLars-Peter Clausen if (len >= PAGE_SIZE) 777e08b60d3SLars-Peter Clausen return -EFBIG; 77851239600SJonathan Cameron } 77951239600SJonathan Cameron 7806b92ba0aSLars-Peter Clausen len += sysfs_emit_at(buf, len, "%s\n", suffix); 781e08b60d3SLars-Peter Clausen 78251239600SJonathan Cameron return len; 78351239600SJonathan Cameron } 78451239600SJonathan Cameron 785eda20ba1SLars-Peter Clausen static ssize_t iio_format_avail_list(char *buf, const int *vals, 786eda20ba1SLars-Peter Clausen int type, int length) 787eda20ba1SLars-Peter Clausen { 788eda20ba1SLars-Peter Clausen 789eda20ba1SLars-Peter Clausen return iio_format_list(buf, vals, type, length, "", ""); 790eda20ba1SLars-Peter Clausen } 791eda20ba1SLars-Peter Clausen 79251239600SJonathan Cameron static ssize_t iio_format_avail_range(char *buf, const int *vals, int type) 79351239600SJonathan Cameron { 7945e1f9185SFawzi Khaber int length; 7955e1f9185SFawzi Khaber 7965e1f9185SFawzi Khaber /* 7975e1f9185SFawzi Khaber * length refers to the array size , not the number of elements. 7985e1f9185SFawzi Khaber * The purpose is to print the range [min , step ,max] so length should 7995e1f9185SFawzi Khaber * be 3 in case of int, and 6 for other types. 8005e1f9185SFawzi Khaber */ 8015e1f9185SFawzi Khaber switch (type) { 8025e1f9185SFawzi Khaber case IIO_VAL_INT: 8035e1f9185SFawzi Khaber length = 3; 8045e1f9185SFawzi Khaber break; 8055e1f9185SFawzi Khaber default: 8065e1f9185SFawzi Khaber length = 6; 8075e1f9185SFawzi Khaber break; 8085e1f9185SFawzi Khaber } 8095e1f9185SFawzi Khaber 8105e1f9185SFawzi Khaber return iio_format_list(buf, vals, type, length, "[", "]"); 81151239600SJonathan Cameron } 81251239600SJonathan Cameron 81351239600SJonathan Cameron static ssize_t iio_read_channel_info_avail(struct device *dev, 81451239600SJonathan Cameron struct device_attribute *attr, 81551239600SJonathan Cameron char *buf) 81651239600SJonathan Cameron { 81751239600SJonathan Cameron struct iio_dev *indio_dev = dev_to_iio_dev(dev); 81851239600SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 81951239600SJonathan Cameron const int *vals; 82051239600SJonathan Cameron int ret; 82151239600SJonathan Cameron int length; 82251239600SJonathan Cameron int type; 82351239600SJonathan Cameron 82451239600SJonathan Cameron ret = indio_dev->info->read_avail(indio_dev, this_attr->c, 82551239600SJonathan Cameron &vals, &type, &length, 82651239600SJonathan Cameron this_attr->address); 82751239600SJonathan Cameron 82851239600SJonathan Cameron if (ret < 0) 82951239600SJonathan Cameron return ret; 83051239600SJonathan Cameron switch (ret) { 83151239600SJonathan Cameron case IIO_AVAIL_LIST: 83251239600SJonathan Cameron return iio_format_avail_list(buf, vals, type, length); 83351239600SJonathan Cameron case IIO_AVAIL_RANGE: 83451239600SJonathan Cameron return iio_format_avail_range(buf, vals, type); 83551239600SJonathan Cameron default: 83651239600SJonathan Cameron return -EINVAL; 83751239600SJonathan Cameron } 83851239600SJonathan Cameron } 83951239600SJonathan Cameron 8406807d721SLars-Peter Clausen /** 841b8528224SBeniamin Bia * __iio_str_to_fixpoint() - Parse a fixed-point number from a string 8426807d721SLars-Peter Clausen * @str: The string to parse 8436807d721SLars-Peter Clausen * @fract_mult: Multiplier for the first decimal place, should be a power of 10 8446807d721SLars-Peter Clausen * @integer: The integer part of the number 8456807d721SLars-Peter Clausen * @fract: The fractional part of the number 846b8528224SBeniamin Bia * @scale_db: True if this should parse as dB 8476807d721SLars-Peter Clausen * 8486807d721SLars-Peter Clausen * Returns 0 on success, or a negative error code if the string could not be 8496807d721SLars-Peter Clausen * parsed. 8506807d721SLars-Peter Clausen */ 851b8528224SBeniamin Bia static int __iio_str_to_fixpoint(const char *str, int fract_mult, 852b8528224SBeniamin Bia int *integer, int *fract, bool scale_db) 8536807d721SLars-Peter Clausen { 8546807d721SLars-Peter Clausen int i = 0, f = 0; 8556807d721SLars-Peter Clausen bool integer_part = true, negative = false; 8566807d721SLars-Peter Clausen 857f47dff32SSean Nyekjaer if (fract_mult == 0) { 858f47dff32SSean Nyekjaer *fract = 0; 859f47dff32SSean Nyekjaer 860f47dff32SSean Nyekjaer return kstrtoint(str, 0, integer); 861f47dff32SSean Nyekjaer } 862f47dff32SSean Nyekjaer 8636807d721SLars-Peter Clausen if (str[0] == '-') { 8646807d721SLars-Peter Clausen negative = true; 8656807d721SLars-Peter Clausen str++; 8666807d721SLars-Peter Clausen } else if (str[0] == '+') { 8676807d721SLars-Peter Clausen str++; 8686807d721SLars-Peter Clausen } 8696807d721SLars-Peter Clausen 8706807d721SLars-Peter Clausen while (*str) { 8716807d721SLars-Peter Clausen if ('0' <= *str && *str <= '9') { 8726807d721SLars-Peter Clausen if (integer_part) { 8736807d721SLars-Peter Clausen i = i * 10 + *str - '0'; 8746807d721SLars-Peter Clausen } else { 8756807d721SLars-Peter Clausen f += fract_mult * (*str - '0'); 8766807d721SLars-Peter Clausen fract_mult /= 10; 8776807d721SLars-Peter Clausen } 8786807d721SLars-Peter Clausen } else if (*str == '\n') { 8796807d721SLars-Peter Clausen if (*(str + 1) == '\0') 8806807d721SLars-Peter Clausen break; 8816807d721SLars-Peter Clausen return -EINVAL; 882b8528224SBeniamin Bia } else if (!strncmp(str, " dB", sizeof(" dB") - 1) && scale_db) { 883b8528224SBeniamin Bia /* Ignore the dB suffix */ 884b8528224SBeniamin Bia str += sizeof(" dB") - 1; 885b8528224SBeniamin Bia continue; 886b8528224SBeniamin Bia } else if (!strncmp(str, "dB", sizeof("dB") - 1) && scale_db) { 887b8528224SBeniamin Bia /* Ignore the dB suffix */ 888b8528224SBeniamin Bia str += sizeof("dB") - 1; 889b8528224SBeniamin Bia continue; 8906807d721SLars-Peter Clausen } else if (*str == '.' && integer_part) { 8916807d721SLars-Peter Clausen integer_part = false; 8926807d721SLars-Peter Clausen } else { 8936807d721SLars-Peter Clausen return -EINVAL; 8946807d721SLars-Peter Clausen } 8956807d721SLars-Peter Clausen str++; 8966807d721SLars-Peter Clausen } 8976807d721SLars-Peter Clausen 8986807d721SLars-Peter Clausen if (negative) { 8996807d721SLars-Peter Clausen if (i) 9006807d721SLars-Peter Clausen i = -i; 9016807d721SLars-Peter Clausen else 9026807d721SLars-Peter Clausen f = -f; 9036807d721SLars-Peter Clausen } 9046807d721SLars-Peter Clausen 9056807d721SLars-Peter Clausen *integer = i; 9066807d721SLars-Peter Clausen *fract = f; 9076807d721SLars-Peter Clausen 9086807d721SLars-Peter Clausen return 0; 9096807d721SLars-Peter Clausen } 910b8528224SBeniamin Bia 911b8528224SBeniamin Bia /** 912b8528224SBeniamin Bia * iio_str_to_fixpoint() - Parse a fixed-point number from a string 913b8528224SBeniamin Bia * @str: The string to parse 914b8528224SBeniamin Bia * @fract_mult: Multiplier for the first decimal place, should be a power of 10 915b8528224SBeniamin Bia * @integer: The integer part of the number 916b8528224SBeniamin Bia * @fract: The fractional part of the number 917b8528224SBeniamin Bia * 918b8528224SBeniamin Bia * Returns 0 on success, or a negative error code if the string could not be 919b8528224SBeniamin Bia * parsed. 920b8528224SBeniamin Bia */ 921b8528224SBeniamin Bia int iio_str_to_fixpoint(const char *str, int fract_mult, 922b8528224SBeniamin Bia int *integer, int *fract) 923b8528224SBeniamin Bia { 924b8528224SBeniamin Bia return __iio_str_to_fixpoint(str, fract_mult, integer, fract, false); 925b8528224SBeniamin Bia } 9266807d721SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_str_to_fixpoint); 9276807d721SLars-Peter Clausen 928a980e046SJonathan Cameron static ssize_t iio_write_channel_info(struct device *dev, 929a980e046SJonathan Cameron struct device_attribute *attr, 930a980e046SJonathan Cameron const char *buf, 931a980e046SJonathan Cameron size_t len) 932a980e046SJonathan Cameron { 933e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 934a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 9356807d721SLars-Peter Clausen int ret, fract_mult = 100000; 9368cb34036SAndrea Merello int integer, fract = 0; 9378cb34036SAndrea Merello bool is_char = false; 938b8528224SBeniamin Bia bool scale_db = false; 939a980e046SJonathan Cameron 940a980e046SJonathan Cameron /* Assumes decimal - precision based on number of digits */ 941a980e046SJonathan Cameron if (!indio_dev->info->write_raw) 942a980e046SJonathan Cameron return -EINVAL; 943a980e046SJonathan Cameron 944a980e046SJonathan Cameron if (indio_dev->info->write_raw_get_fmt) 945a980e046SJonathan Cameron switch (indio_dev->info->write_raw_get_fmt(indio_dev, 946a980e046SJonathan Cameron this_attr->c, this_attr->address)) { 947f47dff32SSean Nyekjaer case IIO_VAL_INT: 948f47dff32SSean Nyekjaer fract_mult = 0; 949f47dff32SSean Nyekjaer break; 950b8528224SBeniamin Bia case IIO_VAL_INT_PLUS_MICRO_DB: 951b8528224SBeniamin Bia scale_db = true; 952df561f66SGustavo A. R. Silva fallthrough; 953a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_MICRO: 954a980e046SJonathan Cameron fract_mult = 100000; 955a980e046SJonathan Cameron break; 956a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_NANO: 957a980e046SJonathan Cameron fract_mult = 100000000; 958a980e046SJonathan Cameron break; 9598cb34036SAndrea Merello case IIO_VAL_CHAR: 9608cb34036SAndrea Merello is_char = true; 9618cb34036SAndrea Merello break; 962a980e046SJonathan Cameron default: 963a980e046SJonathan Cameron return -EINVAL; 964a980e046SJonathan Cameron } 965a980e046SJonathan Cameron 9668cb34036SAndrea Merello if (is_char) { 9678cb34036SAndrea Merello char ch; 9688cb34036SAndrea Merello 9698cb34036SAndrea Merello if (sscanf(buf, "%c", &ch) != 1) 9708cb34036SAndrea Merello return -EINVAL; 9718cb34036SAndrea Merello integer = ch; 9728cb34036SAndrea Merello } else { 973b8528224SBeniamin Bia ret = __iio_str_to_fixpoint(buf, fract_mult, &integer, &fract, 974b8528224SBeniamin Bia scale_db); 975b8528224SBeniamin Bia if (ret) 976b8528224SBeniamin Bia return ret; 9775403f280SMircea Caprioru } 978a980e046SJonathan Cameron 979a980e046SJonathan Cameron ret = indio_dev->info->write_raw(indio_dev, this_attr->c, 980a980e046SJonathan Cameron integer, fract, this_attr->address); 981a980e046SJonathan Cameron if (ret) 982a980e046SJonathan Cameron return ret; 983a980e046SJonathan Cameron 984a980e046SJonathan Cameron return len; 985a980e046SJonathan Cameron } 986a980e046SJonathan Cameron 987a980e046SJonathan Cameron static 988a980e046SJonathan Cameron int __iio_device_attr_init(struct device_attribute *dev_attr, 989a980e046SJonathan Cameron const char *postfix, 990a980e046SJonathan Cameron struct iio_chan_spec const *chan, 991a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 992a980e046SJonathan Cameron struct device_attribute *attr, 993a980e046SJonathan Cameron char *buf), 994a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 995a980e046SJonathan Cameron struct device_attribute *attr, 996a980e046SJonathan Cameron const char *buf, 997a980e046SJonathan Cameron size_t len), 9983704432fSJonathan Cameron enum iio_shared_by shared_by) 999a980e046SJonathan Cameron { 10003704432fSJonathan Cameron int ret = 0; 10017bbcf7e1SLars-Peter Clausen char *name = NULL; 10023704432fSJonathan Cameron char *full_postfix; 10039d9ec8d0SJoe Simmons-Talbott 1004a980e046SJonathan Cameron sysfs_attr_init(&dev_attr->attr); 1005a980e046SJonathan Cameron 1006a980e046SJonathan Cameron /* Build up postfix of <extend_name>_<modifier>_postfix */ 10073704432fSJonathan Cameron if (chan->modified && (shared_by == IIO_SEPARATE)) { 1008a980e046SJonathan Cameron if (chan->extend_name) 1009a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", 1010a980e046SJonathan Cameron iio_modifier_names[chan 1011a980e046SJonathan Cameron ->channel2], 1012a980e046SJonathan Cameron chan->extend_name, 1013a980e046SJonathan Cameron postfix); 1014a980e046SJonathan Cameron else 1015a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s", 1016a980e046SJonathan Cameron iio_modifier_names[chan 1017a980e046SJonathan Cameron ->channel2], 1018a980e046SJonathan Cameron postfix); 1019a980e046SJonathan Cameron } else { 102077bfa8baSLars-Peter Clausen if (chan->extend_name == NULL || shared_by != IIO_SEPARATE) 1021a980e046SJonathan Cameron full_postfix = kstrdup(postfix, GFP_KERNEL); 1022a980e046SJonathan Cameron else 1023a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, 1024a980e046SJonathan Cameron "%s_%s", 1025a980e046SJonathan Cameron chan->extend_name, 1026a980e046SJonathan Cameron postfix); 1027a980e046SJonathan Cameron } 10283704432fSJonathan Cameron if (full_postfix == NULL) 10293704432fSJonathan Cameron return -ENOMEM; 1030a980e046SJonathan Cameron 1031a980e046SJonathan Cameron if (chan->differential) { /* Differential can not have modifier */ 10323704432fSJonathan Cameron switch (shared_by) { 1033c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 10347bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 1035c006ec83SJonathan Cameron break; 1036c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 10377bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 1038c006ec83SJonathan Cameron iio_direction[chan->output], 1039c006ec83SJonathan Cameron full_postfix); 1040c006ec83SJonathan Cameron break; 10413704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 10427bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s", 1043a980e046SJonathan Cameron iio_direction[chan->output], 1044a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1045a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1046a980e046SJonathan Cameron full_postfix); 10473704432fSJonathan Cameron break; 10483704432fSJonathan Cameron case IIO_SEPARATE: 10493704432fSJonathan Cameron if (!chan->indexed) { 1050231bfe53SDan Carpenter WARN(1, "Differential channels must be indexed\n"); 10513704432fSJonathan Cameron ret = -EINVAL; 10523704432fSJonathan Cameron goto error_free_full_postfix; 10533704432fSJonathan Cameron } 10547bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, 10553704432fSJonathan Cameron "%s_%s%d-%s%d_%s", 1056a980e046SJonathan Cameron iio_direction[chan->output], 1057a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1058a980e046SJonathan Cameron chan->channel, 1059a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1060a980e046SJonathan Cameron chan->channel2, 1061a980e046SJonathan Cameron full_postfix); 10623704432fSJonathan Cameron break; 1063a980e046SJonathan Cameron } 1064a980e046SJonathan Cameron } else { /* Single ended */ 10653704432fSJonathan Cameron switch (shared_by) { 1066c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 10677bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 1068c006ec83SJonathan Cameron break; 1069c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 10707bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 1071c006ec83SJonathan Cameron iio_direction[chan->output], 1072c006ec83SJonathan Cameron full_postfix); 1073c006ec83SJonathan Cameron break; 10743704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 10757bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 1076a980e046SJonathan Cameron iio_direction[chan->output], 1077a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1078a980e046SJonathan Cameron full_postfix); 10793704432fSJonathan Cameron break; 10803704432fSJonathan Cameron 10813704432fSJonathan Cameron case IIO_SEPARATE: 10823704432fSJonathan Cameron if (chan->indexed) 10837bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s%d_%s", 1084a980e046SJonathan Cameron iio_direction[chan->output], 1085a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1086a980e046SJonathan Cameron chan->channel, 1087a980e046SJonathan Cameron full_postfix); 1088a980e046SJonathan Cameron else 10897bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 1090a980e046SJonathan Cameron iio_direction[chan->output], 1091a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1092a980e046SJonathan Cameron full_postfix); 10933704432fSJonathan Cameron break; 10943704432fSJonathan Cameron } 1095a980e046SJonathan Cameron } 10967bbcf7e1SLars-Peter Clausen if (name == NULL) { 1097a980e046SJonathan Cameron ret = -ENOMEM; 1098a980e046SJonathan Cameron goto error_free_full_postfix; 1099a980e046SJonathan Cameron } 11007bbcf7e1SLars-Peter Clausen dev_attr->attr.name = name; 1101a980e046SJonathan Cameron 1102a980e046SJonathan Cameron if (readfunc) { 11039cf0b618SJoe Simmons-Talbott dev_attr->attr.mode |= 0444; 1104a980e046SJonathan Cameron dev_attr->show = readfunc; 1105a980e046SJonathan Cameron } 1106a980e046SJonathan Cameron 1107a980e046SJonathan Cameron if (writefunc) { 11089cf0b618SJoe Simmons-Talbott dev_attr->attr.mode |= 0200; 1109a980e046SJonathan Cameron dev_attr->store = writefunc; 1110a980e046SJonathan Cameron } 11117bbcf7e1SLars-Peter Clausen 1112a980e046SJonathan Cameron error_free_full_postfix: 1113a980e046SJonathan Cameron kfree(full_postfix); 11143704432fSJonathan Cameron 1115a980e046SJonathan Cameron return ret; 1116a980e046SJonathan Cameron } 1117a980e046SJonathan Cameron 1118a980e046SJonathan Cameron static void __iio_device_attr_deinit(struct device_attribute *dev_attr) 1119a980e046SJonathan Cameron { 1120a980e046SJonathan Cameron kfree(dev_attr->attr.name); 1121a980e046SJonathan Cameron } 1122a980e046SJonathan Cameron 1123a980e046SJonathan Cameron int __iio_add_chan_devattr(const char *postfix, 1124a980e046SJonathan Cameron struct iio_chan_spec const *chan, 1125a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 1126a980e046SJonathan Cameron struct device_attribute *attr, 1127a980e046SJonathan Cameron char *buf), 1128a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 1129a980e046SJonathan Cameron struct device_attribute *attr, 1130a980e046SJonathan Cameron const char *buf, 1131a980e046SJonathan Cameron size_t len), 1132a980e046SJonathan Cameron u64 mask, 11333704432fSJonathan Cameron enum iio_shared_by shared_by, 1134a980e046SJonathan Cameron struct device *dev, 11353e3d11b2SAlexandru Ardelean struct iio_buffer *buffer, 1136a980e046SJonathan Cameron struct list_head *attr_list) 1137a980e046SJonathan Cameron { 1138a980e046SJonathan Cameron int ret; 1139a980e046SJonathan Cameron struct iio_dev_attr *iio_attr, *t; 1140a980e046SJonathan Cameron 1141670c1103SSachin Kamat iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL); 114292825ff9SHartmut Knaack if (iio_attr == NULL) 114392825ff9SHartmut Knaack return -ENOMEM; 1144a980e046SJonathan Cameron ret = __iio_device_attr_init(&iio_attr->dev_attr, 1145a980e046SJonathan Cameron postfix, chan, 11463704432fSJonathan Cameron readfunc, writefunc, shared_by); 1147a980e046SJonathan Cameron if (ret) 1148a980e046SJonathan Cameron goto error_iio_dev_attr_free; 1149a980e046SJonathan Cameron iio_attr->c = chan; 1150a980e046SJonathan Cameron iio_attr->address = mask; 11513e3d11b2SAlexandru Ardelean iio_attr->buffer = buffer; 1152a980e046SJonathan Cameron list_for_each_entry(t, attr_list, l) 1153a980e046SJonathan Cameron if (strcmp(t->dev_attr.attr.name, 1154a980e046SJonathan Cameron iio_attr->dev_attr.attr.name) == 0) { 11553704432fSJonathan Cameron if (shared_by == IIO_SEPARATE) 1156a980e046SJonathan Cameron dev_err(dev, "tried to double register : %s\n", 1157a980e046SJonathan Cameron t->dev_attr.attr.name); 1158a980e046SJonathan Cameron ret = -EBUSY; 1159a980e046SJonathan Cameron goto error_device_attr_deinit; 1160a980e046SJonathan Cameron } 1161a980e046SJonathan Cameron list_add(&iio_attr->l, attr_list); 1162a980e046SJonathan Cameron 1163a980e046SJonathan Cameron return 0; 1164a980e046SJonathan Cameron 1165a980e046SJonathan Cameron error_device_attr_deinit: 1166a980e046SJonathan Cameron __iio_device_attr_deinit(&iio_attr->dev_attr); 1167a980e046SJonathan Cameron error_iio_dev_attr_free: 1168a980e046SJonathan Cameron kfree(iio_attr); 1169a980e046SJonathan Cameron return ret; 1170a980e046SJonathan Cameron } 1171a980e046SJonathan Cameron 11721d4ef9b3SCristian Pop static int iio_device_add_channel_label(struct iio_dev *indio_dev, 11731d4ef9b3SCristian Pop struct iio_chan_spec const *chan) 11741d4ef9b3SCristian Pop { 11751d4ef9b3SCristian Pop struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 11761d4ef9b3SCristian Pop int ret; 11771d4ef9b3SCristian Pop 117813efdc3dSPaul Cercueil if (!indio_dev->info->read_label && !chan->extend_name) 11791d4ef9b3SCristian Pop return 0; 11801d4ef9b3SCristian Pop 11811d4ef9b3SCristian Pop ret = __iio_add_chan_devattr("label", 11821d4ef9b3SCristian Pop chan, 11831d4ef9b3SCristian Pop &iio_read_channel_label, 11841d4ef9b3SCristian Pop NULL, 11851d4ef9b3SCristian Pop 0, 11861d4ef9b3SCristian Pop IIO_SEPARATE, 11871d4ef9b3SCristian Pop &indio_dev->dev, 11883e3d11b2SAlexandru Ardelean NULL, 11891d4ef9b3SCristian Pop &iio_dev_opaque->channel_attr_list); 11901d4ef9b3SCristian Pop if (ret < 0) 11911d4ef9b3SCristian Pop return ret; 11921d4ef9b3SCristian Pop 11931d4ef9b3SCristian Pop return 1; 11941d4ef9b3SCristian Pop } 11951d4ef9b3SCristian Pop 11963704432fSJonathan Cameron static int iio_device_add_info_mask_type(struct iio_dev *indio_dev, 11973704432fSJonathan Cameron struct iio_chan_spec const *chan, 11983704432fSJonathan Cameron enum iio_shared_by shared_by, 11993704432fSJonathan Cameron const long *infomask) 12003704432fSJonathan Cameron { 1201207c2d27SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 12023704432fSJonathan Cameron int i, ret, attrcount = 0; 12033704432fSJonathan Cameron 12041016d567SOrson Zhai for_each_set_bit(i, infomask, sizeof(*infomask)*8) { 1205ef4b4856SJonathan Cameron if (i >= ARRAY_SIZE(iio_chan_info_postfix)) 1206ef4b4856SJonathan Cameron return -EINVAL; 12073704432fSJonathan Cameron ret = __iio_add_chan_devattr(iio_chan_info_postfix[i], 12083704432fSJonathan Cameron chan, 12093704432fSJonathan Cameron &iio_read_channel_info, 12103704432fSJonathan Cameron &iio_write_channel_info, 12113704432fSJonathan Cameron i, 12123704432fSJonathan Cameron shared_by, 12133704432fSJonathan Cameron &indio_dev->dev, 12143e3d11b2SAlexandru Ardelean NULL, 1215207c2d27SAlexandru Ardelean &iio_dev_opaque->channel_attr_list); 12163704432fSJonathan Cameron if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) 12173704432fSJonathan Cameron continue; 12183704432fSJonathan Cameron else if (ret < 0) 12193704432fSJonathan Cameron return ret; 12203704432fSJonathan Cameron attrcount++; 12213704432fSJonathan Cameron } 12223704432fSJonathan Cameron 12233704432fSJonathan Cameron return attrcount; 12243704432fSJonathan Cameron } 12253704432fSJonathan Cameron 122651239600SJonathan Cameron static int iio_device_add_info_mask_type_avail(struct iio_dev *indio_dev, 122751239600SJonathan Cameron struct iio_chan_spec const *chan, 122851239600SJonathan Cameron enum iio_shared_by shared_by, 122951239600SJonathan Cameron const long *infomask) 123051239600SJonathan Cameron { 1231207c2d27SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 123251239600SJonathan Cameron int i, ret, attrcount = 0; 123351239600SJonathan Cameron char *avail_postfix; 123451239600SJonathan Cameron 12351016d567SOrson Zhai for_each_set_bit(i, infomask, sizeof(*infomask) * 8) { 1236936d3e53SYoung Xiao if (i >= ARRAY_SIZE(iio_chan_info_postfix)) 1237936d3e53SYoung Xiao return -EINVAL; 123851239600SJonathan Cameron avail_postfix = kasprintf(GFP_KERNEL, 123951239600SJonathan Cameron "%s_available", 124051239600SJonathan Cameron iio_chan_info_postfix[i]); 124151239600SJonathan Cameron if (!avail_postfix) 124251239600SJonathan Cameron return -ENOMEM; 124351239600SJonathan Cameron 124451239600SJonathan Cameron ret = __iio_add_chan_devattr(avail_postfix, 124551239600SJonathan Cameron chan, 124651239600SJonathan Cameron &iio_read_channel_info_avail, 124751239600SJonathan Cameron NULL, 124851239600SJonathan Cameron i, 124951239600SJonathan Cameron shared_by, 125051239600SJonathan Cameron &indio_dev->dev, 12513e3d11b2SAlexandru Ardelean NULL, 1252207c2d27SAlexandru Ardelean &iio_dev_opaque->channel_attr_list); 125351239600SJonathan Cameron kfree(avail_postfix); 125451239600SJonathan Cameron if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) 125551239600SJonathan Cameron continue; 125651239600SJonathan Cameron else if (ret < 0) 125751239600SJonathan Cameron return ret; 125851239600SJonathan Cameron attrcount++; 125951239600SJonathan Cameron } 126051239600SJonathan Cameron 126151239600SJonathan Cameron return attrcount; 126251239600SJonathan Cameron } 126351239600SJonathan Cameron 1264a980e046SJonathan Cameron static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev, 1265a980e046SJonathan Cameron struct iio_chan_spec const *chan) 1266a980e046SJonathan Cameron { 1267207c2d27SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1268a980e046SJonathan Cameron int ret, attrcount = 0; 1269a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 1270a980e046SJonathan Cameron 1271a980e046SJonathan Cameron if (chan->channel < 0) 1272a980e046SJonathan Cameron return 0; 12733704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 12743704432fSJonathan Cameron IIO_SEPARATE, 12753704432fSJonathan Cameron &chan->info_mask_separate); 12768655cc49SJonathan Cameron if (ret < 0) 12773704432fSJonathan Cameron return ret; 12783704432fSJonathan Cameron attrcount += ret; 12793704432fSJonathan Cameron 128051239600SJonathan Cameron ret = iio_device_add_info_mask_type_avail(indio_dev, chan, 128151239600SJonathan Cameron IIO_SEPARATE, 1282682ca76bSJoe Simmons-Talbott &chan->info_mask_separate_available); 128351239600SJonathan Cameron if (ret < 0) 128451239600SJonathan Cameron return ret; 128551239600SJonathan Cameron attrcount += ret; 128651239600SJonathan Cameron 12873704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 12883704432fSJonathan Cameron IIO_SHARED_BY_TYPE, 12893704432fSJonathan Cameron &chan->info_mask_shared_by_type); 12903704432fSJonathan Cameron if (ret < 0) 12913704432fSJonathan Cameron return ret; 12923704432fSJonathan Cameron attrcount += ret; 1293a980e046SJonathan Cameron 129451239600SJonathan Cameron ret = iio_device_add_info_mask_type_avail(indio_dev, chan, 129551239600SJonathan Cameron IIO_SHARED_BY_TYPE, 1296682ca76bSJoe Simmons-Talbott &chan->info_mask_shared_by_type_available); 129751239600SJonathan Cameron if (ret < 0) 129851239600SJonathan Cameron return ret; 129951239600SJonathan Cameron attrcount += ret; 130051239600SJonathan Cameron 1301c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 1302c006ec83SJonathan Cameron IIO_SHARED_BY_DIR, 1303c006ec83SJonathan Cameron &chan->info_mask_shared_by_dir); 1304c006ec83SJonathan Cameron if (ret < 0) 1305c006ec83SJonathan Cameron return ret; 1306c006ec83SJonathan Cameron attrcount += ret; 1307c006ec83SJonathan Cameron 130851239600SJonathan Cameron ret = iio_device_add_info_mask_type_avail(indio_dev, chan, 130951239600SJonathan Cameron IIO_SHARED_BY_DIR, 131051239600SJonathan Cameron &chan->info_mask_shared_by_dir_available); 131151239600SJonathan Cameron if (ret < 0) 131251239600SJonathan Cameron return ret; 131351239600SJonathan Cameron attrcount += ret; 131451239600SJonathan Cameron 1315c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 1316c006ec83SJonathan Cameron IIO_SHARED_BY_ALL, 1317c006ec83SJonathan Cameron &chan->info_mask_shared_by_all); 1318c006ec83SJonathan Cameron if (ret < 0) 1319c006ec83SJonathan Cameron return ret; 1320c006ec83SJonathan Cameron attrcount += ret; 1321c006ec83SJonathan Cameron 132251239600SJonathan Cameron ret = iio_device_add_info_mask_type_avail(indio_dev, chan, 132351239600SJonathan Cameron IIO_SHARED_BY_ALL, 132451239600SJonathan Cameron &chan->info_mask_shared_by_all_available); 132551239600SJonathan Cameron if (ret < 0) 132651239600SJonathan Cameron return ret; 132751239600SJonathan Cameron attrcount += ret; 132851239600SJonathan Cameron 13291d4ef9b3SCristian Pop ret = iio_device_add_channel_label(indio_dev, chan); 13301d4ef9b3SCristian Pop if (ret < 0) 13311d4ef9b3SCristian Pop return ret; 13321d4ef9b3SCristian Pop attrcount += ret; 13331d4ef9b3SCristian Pop 1334a980e046SJonathan Cameron if (chan->ext_info) { 1335a980e046SJonathan Cameron unsigned int i = 0; 13369d9ec8d0SJoe Simmons-Talbott 1337a980e046SJonathan Cameron for (ext_info = chan->ext_info; ext_info->name; ext_info++) { 1338a980e046SJonathan Cameron ret = __iio_add_chan_devattr(ext_info->name, 1339a980e046SJonathan Cameron chan, 1340a980e046SJonathan Cameron ext_info->read ? 1341a980e046SJonathan Cameron &iio_read_channel_ext_info : NULL, 1342a980e046SJonathan Cameron ext_info->write ? 1343a980e046SJonathan Cameron &iio_write_channel_ext_info : NULL, 1344a980e046SJonathan Cameron i, 1345a980e046SJonathan Cameron ext_info->shared, 1346a980e046SJonathan Cameron &indio_dev->dev, 13473e3d11b2SAlexandru Ardelean NULL, 1348207c2d27SAlexandru Ardelean &iio_dev_opaque->channel_attr_list); 1349a980e046SJonathan Cameron i++; 1350a980e046SJonathan Cameron if (ret == -EBUSY && ext_info->shared) 1351a980e046SJonathan Cameron continue; 1352a980e046SJonathan Cameron 1353a980e046SJonathan Cameron if (ret) 13543704432fSJonathan Cameron return ret; 1355a980e046SJonathan Cameron 1356a980e046SJonathan Cameron attrcount++; 1357a980e046SJonathan Cameron } 1358a980e046SJonathan Cameron } 1359a980e046SJonathan Cameron 13603704432fSJonathan Cameron return attrcount; 1361a980e046SJonathan Cameron } 1362a980e046SJonathan Cameron 136384088ebdSLars-Peter Clausen /** 136484088ebdSLars-Peter Clausen * iio_free_chan_devattr_list() - Free a list of IIO device attributes 136584088ebdSLars-Peter Clausen * @attr_list: List of IIO device attributes 136684088ebdSLars-Peter Clausen * 136784088ebdSLars-Peter Clausen * This function frees the memory allocated for each of the IIO device 1368c1b03ab5SMartin Fuzzey * attributes in the list. 136984088ebdSLars-Peter Clausen */ 137084088ebdSLars-Peter Clausen void iio_free_chan_devattr_list(struct list_head *attr_list) 1371a980e046SJonathan Cameron { 137284088ebdSLars-Peter Clausen struct iio_dev_attr *p, *n; 137384088ebdSLars-Peter Clausen 137484088ebdSLars-Peter Clausen list_for_each_entry_safe(p, n, attr_list, l) { 137515097c7aSAlexandru Ardelean kfree_const(p->dev_attr.attr.name); 1376c1b03ab5SMartin Fuzzey list_del(&p->l); 1377a980e046SJonathan Cameron kfree(p); 1378a980e046SJonathan Cameron } 137984088ebdSLars-Peter Clausen } 1380a980e046SJonathan Cameron 13819cf0b618SJoe Simmons-Talbott static ssize_t name_show(struct device *dev, struct device_attribute *attr, 1382a980e046SJonathan Cameron char *buf) 1383a980e046SJonathan Cameron { 1384e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 13859d9ec8d0SJoe Simmons-Talbott 138683ca56b6SLars-Peter Clausen return sysfs_emit(buf, "%s\n", indio_dev->name); 1387a980e046SJonathan Cameron } 1388a980e046SJonathan Cameron 13899cf0b618SJoe Simmons-Talbott static DEVICE_ATTR_RO(name); 1390a980e046SJonathan Cameron 13919cf0b618SJoe Simmons-Talbott static ssize_t label_show(struct device *dev, struct device_attribute *attr, 13922c3d0c9fSPhil Reid char *buf) 13932c3d0c9fSPhil Reid { 13942c3d0c9fSPhil Reid struct iio_dev *indio_dev = dev_to_iio_dev(dev); 13959d9ec8d0SJoe Simmons-Talbott 139683ca56b6SLars-Peter Clausen return sysfs_emit(buf, "%s\n", indio_dev->label); 13972c3d0c9fSPhil Reid } 13982c3d0c9fSPhil Reid 13999cf0b618SJoe Simmons-Talbott static DEVICE_ATTR_RO(label); 14002c3d0c9fSPhil Reid 14019cf0b618SJoe Simmons-Talbott static ssize_t current_timestamp_clock_show(struct device *dev, 1402bc2b7dabSGregor Boirie struct device_attribute *attr, 1403bc2b7dabSGregor Boirie char *buf) 1404bc2b7dabSGregor Boirie { 1405bc2b7dabSGregor Boirie const struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1406bc2b7dabSGregor Boirie const clockid_t clk = iio_device_get_clock(indio_dev); 1407bc2b7dabSGregor Boirie const char *name; 1408bc2b7dabSGregor Boirie ssize_t sz; 1409bc2b7dabSGregor Boirie 1410bc2b7dabSGregor Boirie switch (clk) { 1411bc2b7dabSGregor Boirie case CLOCK_REALTIME: 1412bc2b7dabSGregor Boirie name = "realtime\n"; 1413bc2b7dabSGregor Boirie sz = sizeof("realtime\n"); 1414bc2b7dabSGregor Boirie break; 1415bc2b7dabSGregor Boirie case CLOCK_MONOTONIC: 1416bc2b7dabSGregor Boirie name = "monotonic\n"; 1417bc2b7dabSGregor Boirie sz = sizeof("monotonic\n"); 1418bc2b7dabSGregor Boirie break; 1419bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_RAW: 1420bc2b7dabSGregor Boirie name = "monotonic_raw\n"; 1421bc2b7dabSGregor Boirie sz = sizeof("monotonic_raw\n"); 1422bc2b7dabSGregor Boirie break; 1423bc2b7dabSGregor Boirie case CLOCK_REALTIME_COARSE: 1424bc2b7dabSGregor Boirie name = "realtime_coarse\n"; 1425bc2b7dabSGregor Boirie sz = sizeof("realtime_coarse\n"); 1426bc2b7dabSGregor Boirie break; 1427bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_COARSE: 1428bc2b7dabSGregor Boirie name = "monotonic_coarse\n"; 1429bc2b7dabSGregor Boirie sz = sizeof("monotonic_coarse\n"); 1430bc2b7dabSGregor Boirie break; 1431bc2b7dabSGregor Boirie case CLOCK_BOOTTIME: 1432bc2b7dabSGregor Boirie name = "boottime\n"; 1433bc2b7dabSGregor Boirie sz = sizeof("boottime\n"); 1434bc2b7dabSGregor Boirie break; 1435bc2b7dabSGregor Boirie case CLOCK_TAI: 1436bc2b7dabSGregor Boirie name = "tai\n"; 1437bc2b7dabSGregor Boirie sz = sizeof("tai\n"); 1438bc2b7dabSGregor Boirie break; 1439bc2b7dabSGregor Boirie default: 1440bc2b7dabSGregor Boirie BUG(); 1441bc2b7dabSGregor Boirie } 1442bc2b7dabSGregor Boirie 1443bc2b7dabSGregor Boirie memcpy(buf, name, sz); 1444bc2b7dabSGregor Boirie return sz; 1445bc2b7dabSGregor Boirie } 1446bc2b7dabSGregor Boirie 14479cf0b618SJoe Simmons-Talbott static ssize_t current_timestamp_clock_store(struct device *dev, 1448bc2b7dabSGregor Boirie struct device_attribute *attr, 1449bc2b7dabSGregor Boirie const char *buf, size_t len) 1450bc2b7dabSGregor Boirie { 1451bc2b7dabSGregor Boirie clockid_t clk; 1452bc2b7dabSGregor Boirie int ret; 1453bc2b7dabSGregor Boirie 1454bc2b7dabSGregor Boirie if (sysfs_streq(buf, "realtime")) 1455bc2b7dabSGregor Boirie clk = CLOCK_REALTIME; 1456bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "monotonic")) 1457bc2b7dabSGregor Boirie clk = CLOCK_MONOTONIC; 1458bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "monotonic_raw")) 1459bc2b7dabSGregor Boirie clk = CLOCK_MONOTONIC_RAW; 1460bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "realtime_coarse")) 1461bc2b7dabSGregor Boirie clk = CLOCK_REALTIME_COARSE; 1462bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "monotonic_coarse")) 1463bc2b7dabSGregor Boirie clk = CLOCK_MONOTONIC_COARSE; 1464bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "boottime")) 1465bc2b7dabSGregor Boirie clk = CLOCK_BOOTTIME; 1466bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "tai")) 1467bc2b7dabSGregor Boirie clk = CLOCK_TAI; 1468bc2b7dabSGregor Boirie else 1469bc2b7dabSGregor Boirie return -EINVAL; 1470bc2b7dabSGregor Boirie 1471bc2b7dabSGregor Boirie ret = iio_device_set_clock(dev_to_iio_dev(dev), clk); 1472bc2b7dabSGregor Boirie if (ret) 1473bc2b7dabSGregor Boirie return ret; 1474bc2b7dabSGregor Boirie 1475bc2b7dabSGregor Boirie return len; 1476bc2b7dabSGregor Boirie } 1477bc2b7dabSGregor Boirie 147832f17172SAlexandru Ardelean int iio_device_register_sysfs_group(struct iio_dev *indio_dev, 147932f17172SAlexandru Ardelean const struct attribute_group *group) 148032f17172SAlexandru Ardelean { 148132f17172SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 148232f17172SAlexandru Ardelean const struct attribute_group **new, **old = iio_dev_opaque->groups; 148332f17172SAlexandru Ardelean unsigned int cnt = iio_dev_opaque->groupcounter; 148432f17172SAlexandru Ardelean 148532f17172SAlexandru Ardelean new = krealloc(old, sizeof(*new) * (cnt + 2), GFP_KERNEL); 148632f17172SAlexandru Ardelean if (!new) 148732f17172SAlexandru Ardelean return -ENOMEM; 148832f17172SAlexandru Ardelean 148932f17172SAlexandru Ardelean new[iio_dev_opaque->groupcounter++] = group; 149032f17172SAlexandru Ardelean new[iio_dev_opaque->groupcounter] = NULL; 149132f17172SAlexandru Ardelean 149232f17172SAlexandru Ardelean iio_dev_opaque->groups = new; 149332f17172SAlexandru Ardelean 149432f17172SAlexandru Ardelean return 0; 149532f17172SAlexandru Ardelean } 149632f17172SAlexandru Ardelean 14979cf0b618SJoe Simmons-Talbott static DEVICE_ATTR_RW(current_timestamp_clock); 1498bc2b7dabSGregor Boirie 1499a980e046SJonathan Cameron static int iio_device_register_sysfs(struct iio_dev *indio_dev) 1500a980e046SJonathan Cameron { 1501207c2d27SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1502a980e046SJonathan Cameron int i, ret = 0, attrcount, attrn, attrcount_orig = 0; 150384088ebdSLars-Peter Clausen struct iio_dev_attr *p; 1504bc2b7dabSGregor Boirie struct attribute **attr, *clk = NULL; 1505a980e046SJonathan Cameron 1506a980e046SJonathan Cameron /* First count elements in any existing group */ 1507a980e046SJonathan Cameron if (indio_dev->info->attrs) { 1508a980e046SJonathan Cameron attr = indio_dev->info->attrs->attrs; 1509a980e046SJonathan Cameron while (*attr++ != NULL) 1510a980e046SJonathan Cameron attrcount_orig++; 1511a980e046SJonathan Cameron } 1512a980e046SJonathan Cameron attrcount = attrcount_orig; 1513a980e046SJonathan Cameron /* 1514a980e046SJonathan Cameron * New channel registration method - relies on the fact a group does 1515d25b3808SPeter Meerwald * not need to be initialized if its name is NULL. 1516a980e046SJonathan Cameron */ 1517a980e046SJonathan Cameron if (indio_dev->channels) 1518a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) { 1519bc2b7dabSGregor Boirie const struct iio_chan_spec *chan = 1520bc2b7dabSGregor Boirie &indio_dev->channels[i]; 1521bc2b7dabSGregor Boirie 1522bc2b7dabSGregor Boirie if (chan->type == IIO_TIMESTAMP) 1523bc2b7dabSGregor Boirie clk = &dev_attr_current_timestamp_clock.attr; 1524bc2b7dabSGregor Boirie 1525bc2b7dabSGregor Boirie ret = iio_device_add_channel_sysfs(indio_dev, chan); 1526a980e046SJonathan Cameron if (ret < 0) 1527a980e046SJonathan Cameron goto error_clear_attrs; 1528a980e046SJonathan Cameron attrcount += ret; 1529a980e046SJonathan Cameron } 1530a980e046SJonathan Cameron 1531fa83c3baSAlexandru Ardelean if (iio_dev_opaque->event_interface) 1532bc2b7dabSGregor Boirie clk = &dev_attr_current_timestamp_clock.attr; 1533bc2b7dabSGregor Boirie 1534a980e046SJonathan Cameron if (indio_dev->name) 1535a980e046SJonathan Cameron attrcount++; 15362c3d0c9fSPhil Reid if (indio_dev->label) 15372c3d0c9fSPhil Reid attrcount++; 1538bc2b7dabSGregor Boirie if (clk) 1539bc2b7dabSGregor Boirie attrcount++; 1540a980e046SJonathan Cameron 1541207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs = 1542207c2d27SAlexandru Ardelean kcalloc(attrcount + 1, 1543207c2d27SAlexandru Ardelean sizeof(iio_dev_opaque->chan_attr_group.attrs[0]), 1544a980e046SJonathan Cameron GFP_KERNEL); 1545207c2d27SAlexandru Ardelean if (iio_dev_opaque->chan_attr_group.attrs == NULL) { 1546a980e046SJonathan Cameron ret = -ENOMEM; 1547a980e046SJonathan Cameron goto error_clear_attrs; 1548a980e046SJonathan Cameron } 1549e0d27ea3SAndrea Merello /* Copy across original attributes, and point to original binary attributes */ 155018b4c9cdSHans de Goede if (indio_dev->info->attrs) { 1551207c2d27SAlexandru Ardelean memcpy(iio_dev_opaque->chan_attr_group.attrs, 1552a980e046SJonathan Cameron indio_dev->info->attrs->attrs, 1553207c2d27SAlexandru Ardelean sizeof(iio_dev_opaque->chan_attr_group.attrs[0]) 1554a980e046SJonathan Cameron *attrcount_orig); 155518b4c9cdSHans de Goede iio_dev_opaque->chan_attr_group.is_visible = 155618b4c9cdSHans de Goede indio_dev->info->attrs->is_visible; 1557e0d27ea3SAndrea Merello iio_dev_opaque->chan_attr_group.bin_attrs = 1558e0d27ea3SAndrea Merello indio_dev->info->attrs->bin_attrs; 155918b4c9cdSHans de Goede } 1560a980e046SJonathan Cameron attrn = attrcount_orig; 1561a980e046SJonathan Cameron /* Add all elements from the list. */ 1562207c2d27SAlexandru Ardelean list_for_each_entry(p, &iio_dev_opaque->channel_attr_list, l) 1563207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr; 1564a980e046SJonathan Cameron if (indio_dev->name) 1565207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr; 15662c3d0c9fSPhil Reid if (indio_dev->label) 1567207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_label.attr; 1568bc2b7dabSGregor Boirie if (clk) 1569207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs[attrn++] = clk; 1570a980e046SJonathan Cameron 157132f17172SAlexandru Ardelean ret = iio_device_register_sysfs_group(indio_dev, 157232f17172SAlexandru Ardelean &iio_dev_opaque->chan_attr_group); 157332f17172SAlexandru Ardelean if (ret) 157432f17172SAlexandru Ardelean goto error_clear_attrs; 1575a980e046SJonathan Cameron 1576a980e046SJonathan Cameron return 0; 1577a980e046SJonathan Cameron 1578a980e046SJonathan Cameron error_clear_attrs: 1579207c2d27SAlexandru Ardelean iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list); 1580a980e046SJonathan Cameron 1581a980e046SJonathan Cameron return ret; 1582a980e046SJonathan Cameron } 1583a980e046SJonathan Cameron 1584a980e046SJonathan Cameron static void iio_device_unregister_sysfs(struct iio_dev *indio_dev) 1585a980e046SJonathan Cameron { 1586207c2d27SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1587a980e046SJonathan Cameron 1588207c2d27SAlexandru Ardelean iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list); 1589207c2d27SAlexandru Ardelean kfree(iio_dev_opaque->chan_attr_group.attrs); 1590207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs = NULL; 159132f17172SAlexandru Ardelean kfree(iio_dev_opaque->groups); 159219833c40SYang Yingliang iio_dev_opaque->groups = NULL; 1593a980e046SJonathan Cameron } 1594a980e046SJonathan Cameron 1595a980e046SJonathan Cameron static void iio_dev_release(struct device *device) 1596a980e046SJonathan Cameron { 1597e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(device); 15986d4ebd56SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 15996d4ebd56SAlexandru Ardelean 1600d89e119aSBenjamin Gaignard if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES) 1601a980e046SJonathan Cameron iio_device_unregister_trigger_consumer(indio_dev); 1602a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 1603a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 1604e407fd65SLars-Peter Clausen 1605218bc53dSAlexandru Ardelean iio_device_detach_buffers(indio_dev); 16069e69c935SLars-Peter Clausen 16072bc9cd66SVincent Whitchurch lockdep_unregister_key(&iio_dev_opaque->mlock_key); 16082bc9cd66SVincent Whitchurch 1609319dbcd8Skeliu ida_free(&iio_ida, iio_dev_opaque->id); 16106d4ebd56SAlexandru Ardelean kfree(iio_dev_opaque); 1611a980e046SJonathan Cameron } 1612a980e046SJonathan Cameron 16133c33b7b8SLars-Peter Clausen const struct device_type iio_device_type = { 1614a980e046SJonathan Cameron .name = "iio_device", 1615a980e046SJonathan Cameron .release = iio_dev_release, 1616a980e046SJonathan Cameron }; 1617a980e046SJonathan Cameron 1618a7e57dceSSachin Kamat /** 1619a7e57dceSSachin Kamat * iio_device_alloc() - allocate an iio_dev from a driver 1620c9561fd2SJonathan Cameron * @parent: Parent device. 1621a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 1622a7e57dceSSachin Kamat **/ 162378289b4aSAlexandru Ardelean struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv) 1624a980e046SJonathan Cameron { 16256d4ebd56SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque; 16260d596bb2SAlexandru Ardelean struct iio_dev *indio_dev; 1627a980e046SJonathan Cameron size_t alloc_size; 1628a980e046SJonathan Cameron 16296d4ebd56SAlexandru Ardelean alloc_size = sizeof(struct iio_dev_opaque); 1630a980e046SJonathan Cameron if (sizeof_priv) { 163112c4efe3SJonathan Cameron alloc_size = ALIGN(alloc_size, IIO_DMA_MINALIGN); 1632a980e046SJonathan Cameron alloc_size += sizeof_priv; 1633a980e046SJonathan Cameron } 1634a980e046SJonathan Cameron 16356d4ebd56SAlexandru Ardelean iio_dev_opaque = kzalloc(alloc_size, GFP_KERNEL); 16366d4ebd56SAlexandru Ardelean if (!iio_dev_opaque) 16370d4b2184SJonathan Cameron return NULL; 1638a980e046SJonathan Cameron 16390d596bb2SAlexandru Ardelean indio_dev = &iio_dev_opaque->indio_dev; 16400d596bb2SAlexandru Ardelean indio_dev->priv = (char *)iio_dev_opaque + 164112c4efe3SJonathan Cameron ALIGN(sizeof(struct iio_dev_opaque), IIO_DMA_MINALIGN); 16426d4ebd56SAlexandru Ardelean 16430d596bb2SAlexandru Ardelean indio_dev->dev.parent = parent; 16440d596bb2SAlexandru Ardelean indio_dev->dev.type = &iio_device_type; 16450d596bb2SAlexandru Ardelean indio_dev->dev.bus = &iio_bus_type; 16460d596bb2SAlexandru Ardelean device_initialize(&indio_dev->dev); 164716afe125SNuno Sá mutex_init(&iio_dev_opaque->mlock); 1648b804e2b7SJonathan Cameron mutex_init(&iio_dev_opaque->info_exist_lock); 1649207c2d27SAlexandru Ardelean INIT_LIST_HEAD(&iio_dev_opaque->channel_attr_list); 1650a980e046SJonathan Cameron 1651319dbcd8Skeliu iio_dev_opaque->id = ida_alloc(&iio_ida, GFP_KERNEL); 165215ea2878SJonathan Cameron if (iio_dev_opaque->id < 0) { 1653a980e046SJonathan Cameron /* cannot use a dev_err as the name isn't available */ 16543176dd5dSSachin Kamat pr_err("failed to get device id\n"); 16556d4ebd56SAlexandru Ardelean kfree(iio_dev_opaque); 1656a980e046SJonathan Cameron return NULL; 1657a980e046SJonathan Cameron } 1658fe6f45f6SYang Yingliang 1659fe6f45f6SYang Yingliang if (dev_set_name(&indio_dev->dev, "iio:device%d", iio_dev_opaque->id)) { 1660319dbcd8Skeliu ida_free(&iio_ida, iio_dev_opaque->id); 1661fe6f45f6SYang Yingliang kfree(iio_dev_opaque); 1662fe6f45f6SYang Yingliang return NULL; 1663fe6f45f6SYang Yingliang } 1664fe6f45f6SYang Yingliang 16656a8c6b26SAlexandru Ardelean INIT_LIST_HEAD(&iio_dev_opaque->buffer_list); 16668dedcc3eSAlexandru Ardelean INIT_LIST_HEAD(&iio_dev_opaque->ioctl_handlers); 1667a980e046SJonathan Cameron 16682bc9cd66SVincent Whitchurch lockdep_register_key(&iio_dev_opaque->mlock_key); 166916afe125SNuno Sá lockdep_set_class(&iio_dev_opaque->mlock, &iio_dev_opaque->mlock_key); 16702bc9cd66SVincent Whitchurch 16710d596bb2SAlexandru Ardelean return indio_dev; 1672a980e046SJonathan Cameron } 16737cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_alloc); 1674a980e046SJonathan Cameron 1675a7e57dceSSachin Kamat /** 1676a7e57dceSSachin Kamat * iio_device_free() - free an iio_dev from a driver 1677a7e57dceSSachin Kamat * @dev: the iio_dev associated with the device 1678a7e57dceSSachin Kamat **/ 16797cbb7537SLars-Peter Clausen void iio_device_free(struct iio_dev *dev) 1680a980e046SJonathan Cameron { 1681e407fd65SLars-Peter Clausen if (dev) 1682e407fd65SLars-Peter Clausen put_device(&dev->dev); 1683a980e046SJonathan Cameron } 16847cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_free); 1685a980e046SJonathan Cameron 1686cf5724e9SYicong Yang static void devm_iio_device_release(void *iio_dev) 16879dabaf5eSGrygorii Strashko { 1688cf5724e9SYicong Yang iio_device_free(iio_dev); 16899dabaf5eSGrygorii Strashko } 16909dabaf5eSGrygorii Strashko 1691a7e57dceSSachin Kamat /** 1692a7e57dceSSachin Kamat * devm_iio_device_alloc - Resource-managed iio_device_alloc() 169378289b4aSAlexandru Ardelean * @parent: Device to allocate iio_dev for, and parent for this IIO device 1694a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 1695a7e57dceSSachin Kamat * 1696a7e57dceSSachin Kamat * Managed iio_device_alloc. iio_dev allocated with this function is 1697a7e57dceSSachin Kamat * automatically freed on driver detach. 1698a7e57dceSSachin Kamat * 1699a7e57dceSSachin Kamat * RETURNS: 1700a7e57dceSSachin Kamat * Pointer to allocated iio_dev on success, NULL on failure. 1701a7e57dceSSachin Kamat */ 170278289b4aSAlexandru Ardelean struct iio_dev *devm_iio_device_alloc(struct device *parent, int sizeof_priv) 17039dabaf5eSGrygorii Strashko { 1704cf5724e9SYicong Yang struct iio_dev *iio_dev; 1705cf5724e9SYicong Yang int ret; 17069dabaf5eSGrygorii Strashko 170778289b4aSAlexandru Ardelean iio_dev = iio_device_alloc(parent, sizeof_priv); 1708cf5724e9SYicong Yang if (!iio_dev) 1709cf5724e9SYicong Yang return NULL; 1710cf5724e9SYicong Yang 1711cf5724e9SYicong Yang ret = devm_add_action_or_reset(parent, devm_iio_device_release, 1712cf5724e9SYicong Yang iio_dev); 1713cf5724e9SYicong Yang if (ret) 171443fa7394SDan Carpenter return NULL; 17159dabaf5eSGrygorii Strashko 17169dabaf5eSGrygorii Strashko return iio_dev; 17179dabaf5eSGrygorii Strashko } 17189dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_alloc); 17199dabaf5eSGrygorii Strashko 1720a7e57dceSSachin Kamat /** 1721a980e046SJonathan Cameron * iio_chrdev_open() - chrdev file open for buffer access and ioctls 17222498dcf6SCristina Opriceana * @inode: Inode structure for identifying the device in the file system 17232498dcf6SCristina Opriceana * @filp: File structure for iio device used to keep and later access 17242498dcf6SCristina Opriceana * private data 17252498dcf6SCristina Opriceana * 17262498dcf6SCristina Opriceana * Return: 0 on success or -EBUSY if the device is already opened 1727a980e046SJonathan Cameron **/ 1728a980e046SJonathan Cameron static int iio_chrdev_open(struct inode *inode, struct file *filp) 1729a980e046SJonathan Cameron { 1730396f7234SJonathan Cameron struct iio_dev_opaque *iio_dev_opaque = 1731396f7234SJonathan Cameron container_of(inode->i_cdev, struct iio_dev_opaque, chrdev); 1732396f7234SJonathan Cameron struct iio_dev *indio_dev = &iio_dev_opaque->indio_dev; 1733be24dcb1SAlexandru Ardelean struct iio_dev_buffer_pair *ib; 1734a980e046SJonathan Cameron 17358b1c82cbSJonathan Cameron if (test_and_set_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags)) 1736a980e046SJonathan Cameron return -EBUSY; 1737a980e046SJonathan Cameron 1738cadc2125SLars-Peter Clausen iio_device_get(indio_dev); 1739cadc2125SLars-Peter Clausen 1740be24dcb1SAlexandru Ardelean ib = kmalloc(sizeof(*ib), GFP_KERNEL); 1741be24dcb1SAlexandru Ardelean if (!ib) { 1742be24dcb1SAlexandru Ardelean iio_device_put(indio_dev); 17438b1c82cbSJonathan Cameron clear_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags); 1744be24dcb1SAlexandru Ardelean return -ENOMEM; 1745be24dcb1SAlexandru Ardelean } 1746be24dcb1SAlexandru Ardelean 1747be24dcb1SAlexandru Ardelean ib->indio_dev = indio_dev; 1748be24dcb1SAlexandru Ardelean ib->buffer = indio_dev->buffer; 1749be24dcb1SAlexandru Ardelean 1750be24dcb1SAlexandru Ardelean filp->private_data = ib; 1751a980e046SJonathan Cameron 1752a980e046SJonathan Cameron return 0; 1753a980e046SJonathan Cameron } 1754a980e046SJonathan Cameron 1755a980e046SJonathan Cameron /** 1756a980e046SJonathan Cameron * iio_chrdev_release() - chrdev file close buffer access and ioctls 17572498dcf6SCristina Opriceana * @inode: Inode structure pointer for the char device 17582498dcf6SCristina Opriceana * @filp: File structure pointer for the char device 17592498dcf6SCristina Opriceana * 17602498dcf6SCristina Opriceana * Return: 0 for successful release 17612498dcf6SCristina Opriceana */ 1762a980e046SJonathan Cameron static int iio_chrdev_release(struct inode *inode, struct file *filp) 1763a980e046SJonathan Cameron { 1764be24dcb1SAlexandru Ardelean struct iio_dev_buffer_pair *ib = filp->private_data; 1765396f7234SJonathan Cameron struct iio_dev_opaque *iio_dev_opaque = 1766396f7234SJonathan Cameron container_of(inode->i_cdev, struct iio_dev_opaque, chrdev); 1767396f7234SJonathan Cameron struct iio_dev *indio_dev = &iio_dev_opaque->indio_dev; 17689d9ec8d0SJoe Simmons-Talbott 1769be24dcb1SAlexandru Ardelean kfree(ib); 17708b1c82cbSJonathan Cameron clear_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags); 1771cadc2125SLars-Peter Clausen iio_device_put(indio_dev); 1772cadc2125SLars-Peter Clausen 1773a980e046SJonathan Cameron return 0; 1774a980e046SJonathan Cameron } 1775a980e046SJonathan Cameron 17768dedcc3eSAlexandru Ardelean void iio_device_ioctl_handler_register(struct iio_dev *indio_dev, 17778dedcc3eSAlexandru Ardelean struct iio_ioctl_handler *h) 17788dedcc3eSAlexandru Ardelean { 17798dedcc3eSAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 17808dedcc3eSAlexandru Ardelean 17818dedcc3eSAlexandru Ardelean list_add_tail(&h->entry, &iio_dev_opaque->ioctl_handlers); 17828dedcc3eSAlexandru Ardelean } 17838dedcc3eSAlexandru Ardelean 17848dedcc3eSAlexandru Ardelean void iio_device_ioctl_handler_unregister(struct iio_ioctl_handler *h) 17858dedcc3eSAlexandru Ardelean { 17868dedcc3eSAlexandru Ardelean list_del(&h->entry); 17878dedcc3eSAlexandru Ardelean } 17888dedcc3eSAlexandru Ardelean 1789a980e046SJonathan Cameron static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 1790a980e046SJonathan Cameron { 1791be24dcb1SAlexandru Ardelean struct iio_dev_buffer_pair *ib = filp->private_data; 1792be24dcb1SAlexandru Ardelean struct iio_dev *indio_dev = ib->indio_dev; 17938dedcc3eSAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 17948dedcc3eSAlexandru Ardelean struct iio_ioctl_handler *h; 17958dedcc3eSAlexandru Ardelean int ret = -ENODEV; 1796a980e046SJonathan Cameron 1797b804e2b7SJonathan Cameron mutex_lock(&iio_dev_opaque->info_exist_lock); 17988dedcc3eSAlexandru Ardelean 17998dedcc3eSAlexandru Ardelean /** 18008dedcc3eSAlexandru Ardelean * The NULL check here is required to prevent crashing when a device 18018dedcc3eSAlexandru Ardelean * is being removed while userspace would still have open file handles 18028dedcc3eSAlexandru Ardelean * to try to access this device. 18038dedcc3eSAlexandru Ardelean */ 1804f18e7a06SLars-Peter Clausen if (!indio_dev->info) 18058dedcc3eSAlexandru Ardelean goto out_unlock; 1806f18e7a06SLars-Peter Clausen 18078dedcc3eSAlexandru Ardelean list_for_each_entry(h, &iio_dev_opaque->ioctl_handlers, entry) { 18088dedcc3eSAlexandru Ardelean ret = h->ioctl(indio_dev, filp, cmd, arg); 18098dedcc3eSAlexandru Ardelean if (ret != IIO_IOCTL_UNHANDLED) 18108dedcc3eSAlexandru Ardelean break; 1811a980e046SJonathan Cameron } 18128dedcc3eSAlexandru Ardelean 181374d826daSAlexandru Ardelean if (ret == IIO_IOCTL_UNHANDLED) 1814af0670b0SAlexandru Ardelean ret = -ENODEV; 181574d826daSAlexandru Ardelean 18168dedcc3eSAlexandru Ardelean out_unlock: 1817b804e2b7SJonathan Cameron mutex_unlock(&iio_dev_opaque->info_exist_lock); 18188dedcc3eSAlexandru Ardelean 18198dedcc3eSAlexandru Ardelean return ret; 1820a980e046SJonathan Cameron } 1821a980e046SJonathan Cameron 1822a980e046SJonathan Cameron static const struct file_operations iio_buffer_fileops = { 1823a980e046SJonathan Cameron .owner = THIS_MODULE, 1824a980e046SJonathan Cameron .llseek = noop_llseek, 1825ee8caea0SAlexandru Ardelean .read = iio_buffer_read_outer_addr, 18269eeee3b0SMihail Chindris .write = iio_buffer_write_outer_addr, 1827ee8caea0SAlexandru Ardelean .poll = iio_buffer_poll_addr, 1828a980e046SJonathan Cameron .unlocked_ioctl = iio_ioctl, 18291832f2d8SArnd Bergmann .compat_ioctl = compat_ptr_ioctl, 1830ee8caea0SAlexandru Ardelean .open = iio_chrdev_open, 1831ee8caea0SAlexandru Ardelean .release = iio_chrdev_release, 1832a980e046SJonathan Cameron }; 1833a980e046SJonathan Cameron 18348ebaa3ffSAlexandru Ardelean static const struct file_operations iio_event_fileops = { 18358ebaa3ffSAlexandru Ardelean .owner = THIS_MODULE, 18368ebaa3ffSAlexandru Ardelean .llseek = noop_llseek, 18378ebaa3ffSAlexandru Ardelean .unlocked_ioctl = iio_ioctl, 18388ebaa3ffSAlexandru Ardelean .compat_ioctl = compat_ptr_ioctl, 18398ebaa3ffSAlexandru Ardelean .open = iio_chrdev_open, 18408ebaa3ffSAlexandru Ardelean .release = iio_chrdev_release, 18418ebaa3ffSAlexandru Ardelean }; 18428ebaa3ffSAlexandru Ardelean 18438f5d8727SVlad Dogaru static int iio_check_unique_scan_index(struct iio_dev *indio_dev) 18448f5d8727SVlad Dogaru { 18458f5d8727SVlad Dogaru int i, j; 18468f5d8727SVlad Dogaru const struct iio_chan_spec *channels = indio_dev->channels; 18478f5d8727SVlad Dogaru 18488f5d8727SVlad Dogaru if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES)) 18498f5d8727SVlad Dogaru return 0; 18508f5d8727SVlad Dogaru 18518f5d8727SVlad Dogaru for (i = 0; i < indio_dev->num_channels - 1; i++) { 18528f5d8727SVlad Dogaru if (channels[i].scan_index < 0) 18538f5d8727SVlad Dogaru continue; 18548f5d8727SVlad Dogaru for (j = i + 1; j < indio_dev->num_channels; j++) 18558f5d8727SVlad Dogaru if (channels[i].scan_index == channels[j].scan_index) { 18568f5d8727SVlad Dogaru dev_err(&indio_dev->dev, 18578f5d8727SVlad Dogaru "Duplicate scan index %d\n", 18588f5d8727SVlad Dogaru channels[i].scan_index); 18598f5d8727SVlad Dogaru return -EINVAL; 18608f5d8727SVlad Dogaru } 18618f5d8727SVlad Dogaru } 18628f5d8727SVlad Dogaru 18638f5d8727SVlad Dogaru return 0; 18648f5d8727SVlad Dogaru } 18658f5d8727SVlad Dogaru 18660e0761f8SPaul Cercueil static int iio_check_extended_name(const struct iio_dev *indio_dev) 18670e0761f8SPaul Cercueil { 18680e0761f8SPaul Cercueil unsigned int i; 18690e0761f8SPaul Cercueil 18700e0761f8SPaul Cercueil if (!indio_dev->info->read_label) 18710e0761f8SPaul Cercueil return 0; 18720e0761f8SPaul Cercueil 18730e0761f8SPaul Cercueil for (i = 0; i < indio_dev->num_channels; i++) { 18740e0761f8SPaul Cercueil if (indio_dev->channels[i].extend_name) { 18750e0761f8SPaul Cercueil dev_err(&indio_dev->dev, 18760e0761f8SPaul Cercueil "Cannot use labels and extend_name at the same time\n"); 18770e0761f8SPaul Cercueil return -EINVAL; 18780e0761f8SPaul Cercueil } 18790e0761f8SPaul Cercueil } 18800e0761f8SPaul Cercueil 18810e0761f8SPaul Cercueil return 0; 18820e0761f8SPaul Cercueil } 18830e0761f8SPaul Cercueil 1884a980e046SJonathan Cameron static const struct iio_buffer_setup_ops noop_ring_setup_ops; 1885a980e046SJonathan Cameron 188663b19547SJonathan Cameron int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod) 1887a980e046SJonathan Cameron { 18888ebaa3ffSAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 188981366556SAndy Shevchenko struct fwnode_handle *fwnode; 1890a980e046SJonathan Cameron int ret; 1891a980e046SJonathan Cameron 1892a17cb784SAlexandru Ardelean if (!indio_dev->info) 1893a17cb784SAlexandru Ardelean return -EINVAL; 1894a17cb784SAlexandru Ardelean 18956eaf9f6aSJonathan Cameron iio_dev_opaque->driver_module = this_mod; 189617d82b47SGuenter Roeck 189781366556SAndy Shevchenko /* If the calling driver did not initialize firmware node, do it here */ 189881366556SAndy Shevchenko if (dev_fwnode(&indio_dev->dev)) 189981366556SAndy Shevchenko fwnode = dev_fwnode(&indio_dev->dev); 190081366556SAndy Shevchenko else 190181366556SAndy Shevchenko fwnode = dev_fwnode(indio_dev->dev.parent); 190281366556SAndy Shevchenko device_set_node(&indio_dev->dev, fwnode); 190381366556SAndy Shevchenko 190481366556SAndy Shevchenko fwnode_property_read_string(fwnode, "label", &indio_dev->label); 19052c3d0c9fSPhil Reid 19068f5d8727SVlad Dogaru ret = iio_check_unique_scan_index(indio_dev); 19078f5d8727SVlad Dogaru if (ret < 0) 19088f5d8727SVlad Dogaru return ret; 19098f5d8727SVlad Dogaru 19100e0761f8SPaul Cercueil ret = iio_check_extended_name(indio_dev); 19110e0761f8SPaul Cercueil if (ret < 0) 19120e0761f8SPaul Cercueil return ret; 19130e0761f8SPaul Cercueil 19148915aacaSGreg Kroah-Hartman iio_device_register_debugfs(indio_dev); 19153e1b6c95SLars-Peter Clausen 1916ee708e6bSAlexandru Ardelean ret = iio_buffers_alloc_sysfs_and_mask(indio_dev); 19173e1b6c95SLars-Peter Clausen if (ret) { 19183e1b6c95SLars-Peter Clausen dev_err(indio_dev->dev.parent, 19193e1b6c95SLars-Peter Clausen "Failed to create buffer sysfs interfaces\n"); 19203e1b6c95SLars-Peter Clausen goto error_unreg_debugfs; 19213e1b6c95SLars-Peter Clausen } 19223e1b6c95SLars-Peter Clausen 1923a980e046SJonathan Cameron ret = iio_device_register_sysfs(indio_dev); 1924a980e046SJonathan Cameron if (ret) { 1925a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1926a980e046SJonathan Cameron "Failed to register sysfs interfaces\n"); 19273e1b6c95SLars-Peter Clausen goto error_buffer_free_sysfs; 1928a980e046SJonathan Cameron } 1929a980e046SJonathan Cameron ret = iio_device_register_eventset(indio_dev); 1930a980e046SJonathan Cameron if (ret) { 1931a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1932a980e046SJonathan Cameron "Failed to register event set\n"); 1933a980e046SJonathan Cameron goto error_free_sysfs; 1934a980e046SJonathan Cameron } 1935d89e119aSBenjamin Gaignard if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES) 1936a980e046SJonathan Cameron iio_device_register_trigger_consumer(indio_dev); 1937a980e046SJonathan Cameron 1938a980e046SJonathan Cameron if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) && 1939a980e046SJonathan Cameron indio_dev->setup_ops == NULL) 1940a980e046SJonathan Cameron indio_dev->setup_ops = &noop_ring_setup_ops; 1941a980e046SJonathan Cameron 1942ee708e6bSAlexandru Ardelean if (iio_dev_opaque->attached_buffers_cnt) 1943396f7234SJonathan Cameron cdev_init(&iio_dev_opaque->chrdev, &iio_buffer_fileops); 19448ebaa3ffSAlexandru Ardelean else if (iio_dev_opaque->event_interface) 1945396f7234SJonathan Cameron cdev_init(&iio_dev_opaque->chrdev, &iio_event_fileops); 194663b19547SJonathan Cameron 1947ee708e6bSAlexandru Ardelean if (iio_dev_opaque->attached_buffers_cnt || iio_dev_opaque->event_interface) { 194815ea2878SJonathan Cameron indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), iio_dev_opaque->id); 1949396f7234SJonathan Cameron iio_dev_opaque->chrdev.owner = this_mod; 19508ebaa3ffSAlexandru Ardelean } 195138923911SLogan Gunthorpe 195232f17172SAlexandru Ardelean /* assign device groups now; they should be all registered now */ 195332f17172SAlexandru Ardelean indio_dev->dev.groups = iio_dev_opaque->groups; 195432f17172SAlexandru Ardelean 1955396f7234SJonathan Cameron ret = cdev_device_add(&iio_dev_opaque->chrdev, &indio_dev->dev); 1956a980e046SJonathan Cameron if (ret < 0) 19570d5b7daeSLars-Peter Clausen goto error_unreg_eventset; 1958a980e046SJonathan Cameron 19590d5b7daeSLars-Peter Clausen return 0; 196038923911SLogan Gunthorpe 1961a980e046SJonathan Cameron error_unreg_eventset: 1962a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 1963a980e046SJonathan Cameron error_free_sysfs: 1964a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 19653e1b6c95SLars-Peter Clausen error_buffer_free_sysfs: 1966ee708e6bSAlexandru Ardelean iio_buffers_free_sysfs_and_mask(indio_dev); 1967a980e046SJonathan Cameron error_unreg_debugfs: 1968a980e046SJonathan Cameron iio_device_unregister_debugfs(indio_dev); 1969a980e046SJonathan Cameron return ret; 1970a980e046SJonathan Cameron } 197163b19547SJonathan Cameron EXPORT_SYMBOL(__iio_device_register); 1972a980e046SJonathan Cameron 1973a7e57dceSSachin Kamat /** 1974a7e57dceSSachin Kamat * iio_device_unregister() - unregister a device from the IIO subsystem 1975a7e57dceSSachin Kamat * @indio_dev: Device structure representing the device. 1976a7e57dceSSachin Kamat **/ 1977a980e046SJonathan Cameron void iio_device_unregister(struct iio_dev *indio_dev) 1978a980e046SJonathan Cameron { 1979396f7234SJonathan Cameron struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1980396f7234SJonathan Cameron 1981396f7234SJonathan Cameron cdev_device_del(&iio_dev_opaque->chrdev, &indio_dev->dev); 1982a87c82e4SLars-Peter Clausen 1983b804e2b7SJonathan Cameron mutex_lock(&iio_dev_opaque->info_exist_lock); 19847f75591fSFabrice Gasnier 1985bc4c9612SLars-Peter Clausen iio_device_unregister_debugfs(indio_dev); 19860d5b7daeSLars-Peter Clausen 1987a87c82e4SLars-Peter Clausen iio_disable_all_buffers(indio_dev); 1988a87c82e4SLars-Peter Clausen 1989a980e046SJonathan Cameron indio_dev->info = NULL; 1990d2f0a48fSLars-Peter Clausen 1991d2f0a48fSLars-Peter Clausen iio_device_wakeup_eventset(indio_dev); 1992d2f0a48fSLars-Peter Clausen iio_buffer_wakeup_poll(indio_dev); 1993d2f0a48fSLars-Peter Clausen 1994b804e2b7SJonathan Cameron mutex_unlock(&iio_dev_opaque->info_exist_lock); 19953e1b6c95SLars-Peter Clausen 1996ee708e6bSAlexandru Ardelean iio_buffers_free_sysfs_and_mask(indio_dev); 1997a980e046SJonathan Cameron } 1998a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_unregister); 19998caa07c0SSachin Kamat 2000cf5724e9SYicong Yang static void devm_iio_device_unreg(void *indio_dev) 20018caa07c0SSachin Kamat { 2002cf5724e9SYicong Yang iio_device_unregister(indio_dev); 20038caa07c0SSachin Kamat } 20048caa07c0SSachin Kamat 200563b19547SJonathan Cameron int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev, 200663b19547SJonathan Cameron struct module *this_mod) 20078caa07c0SSachin Kamat { 20088caa07c0SSachin Kamat int ret; 20098caa07c0SSachin Kamat 201063b19547SJonathan Cameron ret = __iio_device_register(indio_dev, this_mod); 2011cf5724e9SYicong Yang if (ret) 20128caa07c0SSachin Kamat return ret; 2013cf5724e9SYicong Yang 2014cf5724e9SYicong Yang return devm_add_action_or_reset(dev, devm_iio_device_unreg, indio_dev); 20158caa07c0SSachin Kamat } 201663b19547SJonathan Cameron EXPORT_SYMBOL_GPL(__devm_iio_device_register); 20178caa07c0SSachin Kamat 20188caa07c0SSachin Kamat /** 201908a33805SAlison Schofield * iio_device_claim_direct_mode - Keep device in direct mode 202008a33805SAlison Schofield * @indio_dev: the iio_dev associated with the device 202108a33805SAlison Schofield * 202208a33805SAlison Schofield * If the device is in direct mode it is guaranteed to stay 202308a33805SAlison Schofield * that way until iio_device_release_direct_mode() is called. 202408a33805SAlison Schofield * 202508a33805SAlison Schofield * Use with iio_device_release_direct_mode() 202608a33805SAlison Schofield * 202708a33805SAlison Schofield * Returns: 0 on success, -EBUSY on failure 202808a33805SAlison Schofield */ 202908a33805SAlison Schofield int iio_device_claim_direct_mode(struct iio_dev *indio_dev) 203008a33805SAlison Schofield { 203116afe125SNuno Sá struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 203216afe125SNuno Sá 203316afe125SNuno Sá mutex_lock(&iio_dev_opaque->mlock); 203408a33805SAlison Schofield 203508a33805SAlison Schofield if (iio_buffer_enabled(indio_dev)) { 203616afe125SNuno Sá mutex_unlock(&iio_dev_opaque->mlock); 203708a33805SAlison Schofield return -EBUSY; 203808a33805SAlison Schofield } 203908a33805SAlison Schofield return 0; 204008a33805SAlison Schofield } 204108a33805SAlison Schofield EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode); 204208a33805SAlison Schofield 204308a33805SAlison Schofield /** 204408a33805SAlison Schofield * iio_device_release_direct_mode - releases claim on direct mode 204508a33805SAlison Schofield * @indio_dev: the iio_dev associated with the device 204608a33805SAlison Schofield * 204708a33805SAlison Schofield * Release the claim. Device is no longer guaranteed to stay 204808a33805SAlison Schofield * in direct mode. 204908a33805SAlison Schofield * 205008a33805SAlison Schofield * Use with iio_device_claim_direct_mode() 205108a33805SAlison Schofield */ 205208a33805SAlison Schofield void iio_device_release_direct_mode(struct iio_dev *indio_dev) 205308a33805SAlison Schofield { 205416afe125SNuno Sá mutex_unlock(&to_iio_dev_opaque(indio_dev)->mlock); 205508a33805SAlison Schofield } 205608a33805SAlison Schofield EXPORT_SYMBOL_GPL(iio_device_release_direct_mode); 205708a33805SAlison Schofield 20588c576f87SMiquel Raynal /** 20590a856542SNuno Sá * iio_device_claim_buffer_mode - Keep device in buffer mode 20600a856542SNuno Sá * @indio_dev: the iio_dev associated with the device 20610a856542SNuno Sá * 20620a856542SNuno Sá * If the device is in buffer mode it is guaranteed to stay 20630a856542SNuno Sá * that way until iio_device_release_buffer_mode() is called. 20640a856542SNuno Sá * 20650a856542SNuno Sá * Use with iio_device_release_buffer_mode(). 20660a856542SNuno Sá * 20670a856542SNuno Sá * Returns: 0 on success, -EBUSY on failure. 20680a856542SNuno Sá */ 20690a856542SNuno Sá int iio_device_claim_buffer_mode(struct iio_dev *indio_dev) 20700a856542SNuno Sá { 207116afe125SNuno Sá struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 207216afe125SNuno Sá 207316afe125SNuno Sá mutex_lock(&iio_dev_opaque->mlock); 20740a856542SNuno Sá 20750a856542SNuno Sá if (iio_buffer_enabled(indio_dev)) 20760a856542SNuno Sá return 0; 20770a856542SNuno Sá 207816afe125SNuno Sá mutex_unlock(&iio_dev_opaque->mlock); 20790a856542SNuno Sá return -EBUSY; 20800a856542SNuno Sá } 20810a856542SNuno Sá EXPORT_SYMBOL_GPL(iio_device_claim_buffer_mode); 20820a856542SNuno Sá 20830a856542SNuno Sá /** 20840a856542SNuno Sá * iio_device_release_buffer_mode - releases claim on buffer mode 20850a856542SNuno Sá * @indio_dev: the iio_dev associated with the device 20860a856542SNuno Sá * 20870a856542SNuno Sá * Release the claim. Device is no longer guaranteed to stay 20880a856542SNuno Sá * in buffer mode. 20890a856542SNuno Sá * 20900a856542SNuno Sá * Use with iio_device_claim_buffer_mode(). 20910a856542SNuno Sá */ 20920a856542SNuno Sá void iio_device_release_buffer_mode(struct iio_dev *indio_dev) 20930a856542SNuno Sá { 209416afe125SNuno Sá mutex_unlock(&to_iio_dev_opaque(indio_dev)->mlock); 20950a856542SNuno Sá } 20960a856542SNuno Sá EXPORT_SYMBOL_GPL(iio_device_release_buffer_mode); 20970a856542SNuno Sá 20980a856542SNuno Sá /** 20998c576f87SMiquel Raynal * iio_device_get_current_mode() - helper function providing read-only access to 210051570c9dSMiquel Raynal * the opaque @currentmode variable 21018c576f87SMiquel Raynal * @indio_dev: IIO device structure for device 21028c576f87SMiquel Raynal */ 21038c576f87SMiquel Raynal int iio_device_get_current_mode(struct iio_dev *indio_dev) 21048c576f87SMiquel Raynal { 210551570c9dSMiquel Raynal struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 210651570c9dSMiquel Raynal 210751570c9dSMiquel Raynal return iio_dev_opaque->currentmode; 21088c576f87SMiquel Raynal } 21098c576f87SMiquel Raynal EXPORT_SYMBOL_GPL(iio_device_get_current_mode); 21108c576f87SMiquel Raynal 2111a980e046SJonathan Cameron subsys_initcall(iio_init); 2112a980e046SJonathan Cameron module_exit(iio_exit); 2113a980e046SJonathan Cameron 2114c8b95952SJonathan Cameron MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); 2115a980e046SJonathan Cameron MODULE_DESCRIPTION("Industrial I/O core"); 2116a980e046SJonathan Cameron MODULE_LICENSE("GPL"); 2117