1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2a980e046SJonathan Cameron /* The industrial I/O core 3a980e046SJonathan Cameron * 4a980e046SJonathan Cameron * Copyright (c) 2008 Jonathan Cameron 5a980e046SJonathan Cameron * 6a980e046SJonathan Cameron * Based on elements of hwmon and input subsystems. 7a980e046SJonathan Cameron */ 8a980e046SJonathan Cameron 93176dd5dSSachin Kamat #define pr_fmt(fmt) "iio-core: " fmt 103176dd5dSSachin Kamat 11a980e046SJonathan Cameron #include <linux/kernel.h> 12a980e046SJonathan Cameron #include <linux/module.h> 13a980e046SJonathan Cameron #include <linux/idr.h> 14a980e046SJonathan Cameron #include <linux/kdev_t.h> 15a980e046SJonathan Cameron #include <linux/err.h> 16a980e046SJonathan Cameron #include <linux/device.h> 17a980e046SJonathan Cameron #include <linux/fs.h> 18a980e046SJonathan Cameron #include <linux/poll.h> 19fb158971SAndy Shevchenko #include <linux/property.h> 20a980e046SJonathan Cameron #include <linux/sched.h> 21a980e046SJonathan Cameron #include <linux/wait.h> 22a980e046SJonathan Cameron #include <linux/cdev.h> 23a980e046SJonathan Cameron #include <linux/slab.h> 24a980e046SJonathan Cameron #include <linux/anon_inodes.h> 25a980e046SJonathan Cameron #include <linux/debugfs.h> 2608a33805SAlison Schofield #include <linux/mutex.h> 27a980e046SJonathan Cameron #include <linux/iio/iio.h> 286d4ebd56SAlexandru Ardelean #include <linux/iio/iio-opaque.h> 29a980e046SJonathan Cameron #include "iio_core.h" 30a980e046SJonathan Cameron #include "iio_core_trigger.h" 31a980e046SJonathan Cameron #include <linux/iio/sysfs.h> 32a980e046SJonathan Cameron #include <linux/iio/events.h> 339e69c935SLars-Peter Clausen #include <linux/iio/buffer.h> 3433dd94cbSJonathan Cameron #include <linux/iio/buffer_impl.h> 35a980e046SJonathan Cameron 36a980e046SJonathan Cameron /* IDA to assign each registered device a unique id */ 37a980e046SJonathan Cameron static DEFINE_IDA(iio_ida); 38a980e046SJonathan Cameron 39a980e046SJonathan Cameron static dev_t iio_devt; 40a980e046SJonathan Cameron 41a980e046SJonathan Cameron #define IIO_DEV_MAX 256 42a980e046SJonathan Cameron struct bus_type iio_bus_type = { 43a980e046SJonathan Cameron .name = "iio", 44a980e046SJonathan Cameron }; 45a980e046SJonathan Cameron EXPORT_SYMBOL(iio_bus_type); 46a980e046SJonathan Cameron 47a980e046SJonathan Cameron static struct dentry *iio_debugfs_dentry; 48a980e046SJonathan Cameron 49a980e046SJonathan Cameron static const char * const iio_direction[] = { 50a980e046SJonathan Cameron [0] = "in", 51a980e046SJonathan Cameron [1] = "out", 52a980e046SJonathan Cameron }; 53a980e046SJonathan Cameron 54a980e046SJonathan Cameron static const char * const iio_chan_type_name_spec[] = { 55a980e046SJonathan Cameron [IIO_VOLTAGE] = "voltage", 56a980e046SJonathan Cameron [IIO_CURRENT] = "current", 57a980e046SJonathan Cameron [IIO_POWER] = "power", 58a980e046SJonathan Cameron [IIO_ACCEL] = "accel", 59a980e046SJonathan Cameron [IIO_ANGL_VEL] = "anglvel", 60a980e046SJonathan Cameron [IIO_MAGN] = "magn", 61a980e046SJonathan Cameron [IIO_LIGHT] = "illuminance", 62a980e046SJonathan Cameron [IIO_INTENSITY] = "intensity", 63a980e046SJonathan Cameron [IIO_PROXIMITY] = "proximity", 64a980e046SJonathan Cameron [IIO_TEMP] = "temp", 65a980e046SJonathan Cameron [IIO_INCLI] = "incli", 66a980e046SJonathan Cameron [IIO_ROT] = "rot", 67a980e046SJonathan Cameron [IIO_ANGL] = "angl", 68a980e046SJonathan Cameron [IIO_TIMESTAMP] = "timestamp", 69a980e046SJonathan Cameron [IIO_CAPACITANCE] = "capacitance", 70a6b12855SMichael Hennerich [IIO_ALTVOLTAGE] = "altvoltage", 7121cd1fabSJon Brenner [IIO_CCT] = "cct", 72c4f0c693SLars-Peter Clausen [IIO_PRESSURE] = "pressure", 73ac216aa2SHarald Geyer [IIO_HUMIDITYRELATIVE] = "humidityrelative", 7455aebeb9SDaniel Baluta [IIO_ACTIVITY] = "activity", 75a88bfe78SIrina Tirdea [IIO_STEPS] = "steps", 7672c66644SIrina Tirdea [IIO_ENERGY] = "energy", 77cc3c9eecSIrina Tirdea [IIO_DISTANCE] = "distance", 785a1a9329SIrina Tirdea [IIO_VELOCITY] = "velocity", 798ff6b3bcSMatt Ranostay [IIO_CONCENTRATION] = "concentration", 80d38d5469SMatt Ranostay [IIO_RESISTANCE] = "resistance", 81ecb3a7ccSMatt Ranostay [IIO_PH] = "ph", 82d409404cSPeter Meerwald-Stadler [IIO_UVINDEX] = "uvindex", 834b9d2090SMatt Ranostay [IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity", 841a8f324aSWilliam Breathitt Gray [IIO_COUNT] = "count", 851a8f324aSWilliam Breathitt Gray [IIO_INDEX] = "index", 86571299d0SSong Hongyan [IIO_GRAVITY] = "gravity", 873055a6cfSEugen Hristev [IIO_POSITIONRELATIVE] = "positionrelative", 88c73314e6SMathieu Othacehe [IIO_PHASE] = "phase", 8917abc9ecSTomasz Duszynski [IIO_MASSCONCENTRATION] = "massconcentration", 90a980e046SJonathan Cameron }; 91a980e046SJonathan Cameron 92a980e046SJonathan Cameron static const char * const iio_modifier_names[] = { 93a980e046SJonathan Cameron [IIO_MOD_X] = "x", 94a980e046SJonathan Cameron [IIO_MOD_Y] = "y", 95a980e046SJonathan Cameron [IIO_MOD_Z] = "z", 964b8d8015SPeter Meerwald [IIO_MOD_X_AND_Y] = "x&y", 974b8d8015SPeter Meerwald [IIO_MOD_X_AND_Z] = "x&z", 984b8d8015SPeter Meerwald [IIO_MOD_Y_AND_Z] = "y&z", 994b8d8015SPeter Meerwald [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z", 1004b8d8015SPeter Meerwald [IIO_MOD_X_OR_Y] = "x|y", 1014b8d8015SPeter Meerwald [IIO_MOD_X_OR_Z] = "x|z", 1024b8d8015SPeter Meerwald [IIO_MOD_Y_OR_Z] = "y|z", 1034b8d8015SPeter Meerwald [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z", 1048f5879b2SJonathan Cameron [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)", 105cf82cb81SJonathan Cameron [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2", 106a980e046SJonathan Cameron [IIO_MOD_LIGHT_BOTH] = "both", 107a980e046SJonathan Cameron [IIO_MOD_LIGHT_IR] = "ir", 10821cd1fabSJon Brenner [IIO_MOD_LIGHT_CLEAR] = "clear", 10921cd1fabSJon Brenner [IIO_MOD_LIGHT_RED] = "red", 11021cd1fabSJon Brenner [IIO_MOD_LIGHT_GREEN] = "green", 11121cd1fabSJon Brenner [IIO_MOD_LIGHT_BLUE] = "blue", 1122c5ff1f9SPeter Meerwald-Stadler [IIO_MOD_LIGHT_UV] = "uv", 113c0e4e0fdSMaxime Roussin-Bélanger [IIO_MOD_LIGHT_DUV] = "duv", 1145082f405SSrinivas Pandruvada [IIO_MOD_QUATERNION] = "quaternion", 115638b43b3SPeter Meerwald [IIO_MOD_TEMP_AMBIENT] = "ambient", 116638b43b3SPeter Meerwald [IIO_MOD_TEMP_OBJECT] = "object", 11711b8ddabSReyad Attiyat [IIO_MOD_NORTH_MAGN] = "from_north_magnetic", 11811b8ddabSReyad Attiyat [IIO_MOD_NORTH_TRUE] = "from_north_true", 11911b8ddabSReyad Attiyat [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp", 12011b8ddabSReyad Attiyat [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp", 12155aebeb9SDaniel Baluta [IIO_MOD_RUNNING] = "running", 12255aebeb9SDaniel Baluta [IIO_MOD_JOGGING] = "jogging", 12355aebeb9SDaniel Baluta [IIO_MOD_WALKING] = "walking", 12455aebeb9SDaniel Baluta [IIO_MOD_STILL] = "still", 1255a1a9329SIrina Tirdea [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)", 1261ce87f21SLars-Peter Clausen [IIO_MOD_I] = "i", 1271ce87f21SLars-Peter Clausen [IIO_MOD_Q] = "q", 1288ff6b3bcSMatt Ranostay [IIO_MOD_CO2] = "co2", 1298ff6b3bcSMatt Ranostay [IIO_MOD_VOC] = "voc", 13017abc9ecSTomasz Duszynski [IIO_MOD_PM1] = "pm1", 13117abc9ecSTomasz Duszynski [IIO_MOD_PM2P5] = "pm2p5", 13217abc9ecSTomasz Duszynski [IIO_MOD_PM4] = "pm4", 13317abc9ecSTomasz Duszynski [IIO_MOD_PM10] = "pm10", 13425f02d32SMatt Ranostay [IIO_MOD_ETHANOL] = "ethanol", 13525f02d32SMatt Ranostay [IIO_MOD_H2] = "h2", 1364ffa22fdSMatt Ranostay [IIO_MOD_O2] = "o2", 137a980e046SJonathan Cameron }; 138a980e046SJonathan Cameron 139a980e046SJonathan Cameron /* relies on pairs of these shared then separate */ 140a980e046SJonathan Cameron static const char * const iio_chan_info_postfix[] = { 141a980e046SJonathan Cameron [IIO_CHAN_INFO_RAW] = "raw", 142a980e046SJonathan Cameron [IIO_CHAN_INFO_PROCESSED] = "input", 143a980e046SJonathan Cameron [IIO_CHAN_INFO_SCALE] = "scale", 144a980e046SJonathan Cameron [IIO_CHAN_INFO_OFFSET] = "offset", 145a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBSCALE] = "calibscale", 146a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBBIAS] = "calibbias", 147a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK] = "peak_raw", 148a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale", 149a980e046SJonathan Cameron [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw", 150a980e046SJonathan Cameron [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw", 151a980e046SJonathan Cameron [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY] 152a980e046SJonathan Cameron = "filter_low_pass_3db_frequency", 1533f7f642bSMartin Fuzzey [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY] 1543f7f642bSMartin Fuzzey = "filter_high_pass_3db_frequency", 155a980e046SJonathan Cameron [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency", 156a6b12855SMichael Hennerich [IIO_CHAN_INFO_FREQUENCY] = "frequency", 157a6b12855SMichael Hennerich [IIO_CHAN_INFO_PHASE] = "phase", 158b65d6212SMichael Hennerich [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain", 1597c9ab035Ssrinivas pandruvada [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis", 1601c71a286SYe Xiang [IIO_CHAN_INFO_HYSTERESIS_RELATIVE] = "hysteresis_relative", 161899d90bdSPeter Meerwald [IIO_CHAN_INFO_INT_TIME] = "integration_time", 162a88bfe78SIrina Tirdea [IIO_CHAN_INFO_ENABLE] = "en", 163bcdf28fbSIrina Tirdea [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight", 164d37f6836SIrina Tirdea [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight", 1652f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count", 1662f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time", 167c8a85854SVianney le Clément de Saint-Marcq [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity", 168faaa4495SIrina Tirdea [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio", 169ddfb97d8SAndrea Merello [IIO_CHAN_INFO_THERMOCOUPLE_TYPE] = "thermocouple_type", 170dde8ceecSCrt Mori [IIO_CHAN_INFO_CALIBAMBIENT] = "calibambient", 171a980e046SJonathan Cameron }; 17215ea2878SJonathan Cameron /** 17315ea2878SJonathan Cameron * iio_device_id() - query the unique ID for the device 17415ea2878SJonathan Cameron * @indio_dev: Device structure whose ID is being queried 17515ea2878SJonathan Cameron * 17615ea2878SJonathan Cameron * The IIO device ID is a unique index used for example for the naming 17715ea2878SJonathan Cameron * of the character device /dev/iio\:device[ID] 17815ea2878SJonathan Cameron */ 17915ea2878SJonathan Cameron int iio_device_id(struct iio_dev *indio_dev) 18015ea2878SJonathan Cameron { 18115ea2878SJonathan Cameron struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 18215ea2878SJonathan Cameron 18315ea2878SJonathan Cameron return iio_dev_opaque->id; 18415ea2878SJonathan Cameron } 18515ea2878SJonathan Cameron EXPORT_SYMBOL_GPL(iio_device_id); 186a980e046SJonathan Cameron 187d9a0e73cSLars-Peter Clausen /** 188d9a0e73cSLars-Peter Clausen * iio_sysfs_match_string_with_gaps - matches given string in an array with gaps 189d9a0e73cSLars-Peter Clausen * @array: array of strings 190d9a0e73cSLars-Peter Clausen * @n: number of strings in the array 191d9a0e73cSLars-Peter Clausen * @str: string to match with 192d9a0e73cSLars-Peter Clausen * 193d9a0e73cSLars-Peter Clausen * Returns index of @str in the @array or -EINVAL, similar to match_string(). 194d9a0e73cSLars-Peter Clausen * Uses sysfs_streq instead of strcmp for matching. 195d9a0e73cSLars-Peter Clausen * 196d9a0e73cSLars-Peter Clausen * This routine will look for a string in an array of strings. 197d9a0e73cSLars-Peter Clausen * The search will continue until the element is found or the n-th element 198d9a0e73cSLars-Peter Clausen * is reached, regardless of any NULL elements in the array. 199d9a0e73cSLars-Peter Clausen */ 200d9a0e73cSLars-Peter Clausen static int iio_sysfs_match_string_with_gaps(const char * const *array, size_t n, 201d9a0e73cSLars-Peter Clausen const char *str) 202d9a0e73cSLars-Peter Clausen { 203d9a0e73cSLars-Peter Clausen const char *item; 204d9a0e73cSLars-Peter Clausen int index; 205d9a0e73cSLars-Peter Clausen 206d9a0e73cSLars-Peter Clausen for (index = 0; index < n; index++) { 207d9a0e73cSLars-Peter Clausen item = array[index]; 208d9a0e73cSLars-Peter Clausen if (!item) 209d9a0e73cSLars-Peter Clausen continue; 210d9a0e73cSLars-Peter Clausen if (sysfs_streq(item, str)) 211d9a0e73cSLars-Peter Clausen return index; 212d9a0e73cSLars-Peter Clausen } 213d9a0e73cSLars-Peter Clausen 214d9a0e73cSLars-Peter Clausen return -EINVAL; 215d9a0e73cSLars-Peter Clausen } 216d9a0e73cSLars-Peter Clausen 21796fb1b67SAlexandru Ardelean #if defined(CONFIG_DEBUG_FS) 218c9561fd2SJonathan Cameron /* 21996fb1b67SAlexandru Ardelean * There's also a CONFIG_DEBUG_FS guard in include/linux/iio/iio.h for 22096fb1b67SAlexandru Ardelean * iio_get_debugfs_dentry() to make it inline if CONFIG_DEBUG_FS is undefined 22196fb1b67SAlexandru Ardelean */ 22296fb1b67SAlexandru Ardelean struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev) 22396fb1b67SAlexandru Ardelean { 22496fb1b67SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 22596fb1b67SAlexandru Ardelean return iio_dev_opaque->debugfs_dentry; 22696fb1b67SAlexandru Ardelean } 22796fb1b67SAlexandru Ardelean EXPORT_SYMBOL_GPL(iio_get_debugfs_dentry); 22896fb1b67SAlexandru Ardelean #endif 22996fb1b67SAlexandru Ardelean 230a7e57dceSSachin Kamat /** 231a7e57dceSSachin Kamat * iio_find_channel_from_si() - get channel from its scan index 232a7e57dceSSachin Kamat * @indio_dev: device 233a7e57dceSSachin Kamat * @si: scan index to match 234a7e57dceSSachin Kamat */ 235a980e046SJonathan Cameron const struct iio_chan_spec 236a980e046SJonathan Cameron *iio_find_channel_from_si(struct iio_dev *indio_dev, int si) 237a980e046SJonathan Cameron { 238a980e046SJonathan Cameron int i; 239a980e046SJonathan Cameron 240a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) 241a980e046SJonathan Cameron if (indio_dev->channels[i].scan_index == si) 242a980e046SJonathan Cameron return &indio_dev->channels[i]; 243a980e046SJonathan Cameron return NULL; 244a980e046SJonathan Cameron } 245a980e046SJonathan Cameron 246a980e046SJonathan Cameron /* This turns up an awful lot */ 247a980e046SJonathan Cameron ssize_t iio_read_const_attr(struct device *dev, 248a980e046SJonathan Cameron struct device_attribute *attr, 249a980e046SJonathan Cameron char *buf) 250a980e046SJonathan Cameron { 25183ca56b6SLars-Peter Clausen return sysfs_emit(buf, "%s\n", to_iio_const_attr(attr)->string); 252a980e046SJonathan Cameron } 253a980e046SJonathan Cameron EXPORT_SYMBOL(iio_read_const_attr); 254a980e046SJonathan Cameron 25569f0793eSGwendal Grignou /** 25669f0793eSGwendal Grignou * iio_device_set_clock() - Set current timestamping clock for the device 25769f0793eSGwendal Grignou * @indio_dev: IIO device structure containing the device 25869f0793eSGwendal Grignou * @clock_id: timestamping clock posix identifier to set. 25969f0793eSGwendal Grignou */ 26069f0793eSGwendal Grignou int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id) 261bc2b7dabSGregor Boirie { 262bc2b7dabSGregor Boirie int ret; 263fa83c3baSAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 264fa83c3baSAlexandru Ardelean const struct iio_event_interface *ev_int = iio_dev_opaque->event_interface; 265bc2b7dabSGregor Boirie 266bc2b7dabSGregor Boirie ret = mutex_lock_interruptible(&indio_dev->mlock); 267bc2b7dabSGregor Boirie if (ret) 268bc2b7dabSGregor Boirie return ret; 269bc2b7dabSGregor Boirie if ((ev_int && iio_event_enabled(ev_int)) || 270bc2b7dabSGregor Boirie iio_buffer_enabled(indio_dev)) { 271bc2b7dabSGregor Boirie mutex_unlock(&indio_dev->mlock); 272bc2b7dabSGregor Boirie return -EBUSY; 273bc2b7dabSGregor Boirie } 27462a486c4SJonathan Cameron iio_dev_opaque->clock_id = clock_id; 275bc2b7dabSGregor Boirie mutex_unlock(&indio_dev->mlock); 276bc2b7dabSGregor Boirie 277bc2b7dabSGregor Boirie return 0; 278bc2b7dabSGregor Boirie } 27969f0793eSGwendal Grignou EXPORT_SYMBOL(iio_device_set_clock); 280bc2b7dabSGregor Boirie 281bc2b7dabSGregor Boirie /** 28262a486c4SJonathan Cameron * iio_device_get_clock() - Retrieve current timestamping clock for the device 28362a486c4SJonathan Cameron * @indio_dev: IIO device structure containing the device 28462a486c4SJonathan Cameron */ 28562a486c4SJonathan Cameron clockid_t iio_device_get_clock(const struct iio_dev *indio_dev) 28662a486c4SJonathan Cameron { 28762a486c4SJonathan Cameron struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 28862a486c4SJonathan Cameron 28962a486c4SJonathan Cameron return iio_dev_opaque->clock_id; 29062a486c4SJonathan Cameron } 29162a486c4SJonathan Cameron EXPORT_SYMBOL(iio_device_get_clock); 29262a486c4SJonathan Cameron 29362a486c4SJonathan Cameron /** 294bc2b7dabSGregor Boirie * iio_get_time_ns() - utility function to get a time stamp for events etc 295bc2b7dabSGregor Boirie * @indio_dev: device 296bc2b7dabSGregor Boirie */ 297bc2b7dabSGregor Boirie s64 iio_get_time_ns(const struct iio_dev *indio_dev) 298bc2b7dabSGregor Boirie { 29945e7f5d2SArnd Bergmann struct timespec64 tp; 300bc2b7dabSGregor Boirie 301bc2b7dabSGregor Boirie switch (iio_device_get_clock(indio_dev)) { 302bc2b7dabSGregor Boirie case CLOCK_REALTIME: 30345e7f5d2SArnd Bergmann return ktime_get_real_ns(); 304bc2b7dabSGregor Boirie case CLOCK_MONOTONIC: 30545e7f5d2SArnd Bergmann return ktime_get_ns(); 306bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_RAW: 30745e7f5d2SArnd Bergmann return ktime_get_raw_ns(); 308bc2b7dabSGregor Boirie case CLOCK_REALTIME_COARSE: 30945e7f5d2SArnd Bergmann return ktime_to_ns(ktime_get_coarse_real()); 310bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_COARSE: 31145e7f5d2SArnd Bergmann ktime_get_coarse_ts64(&tp); 31245e7f5d2SArnd Bergmann return timespec64_to_ns(&tp); 313bc2b7dabSGregor Boirie case CLOCK_BOOTTIME: 3149285ec4cSJason A. Donenfeld return ktime_get_boottime_ns(); 315bc2b7dabSGregor Boirie case CLOCK_TAI: 3169285ec4cSJason A. Donenfeld return ktime_get_clocktai_ns(); 317bc2b7dabSGregor Boirie default: 318bc2b7dabSGregor Boirie BUG(); 319bc2b7dabSGregor Boirie } 320bc2b7dabSGregor Boirie } 321bc2b7dabSGregor Boirie EXPORT_SYMBOL(iio_get_time_ns); 322bc2b7dabSGregor Boirie 323bc2b7dabSGregor Boirie /** 324bc2b7dabSGregor Boirie * iio_get_time_res() - utility function to get time stamp clock resolution in 325bc2b7dabSGregor Boirie * nano seconds. 326bc2b7dabSGregor Boirie * @indio_dev: device 327bc2b7dabSGregor Boirie */ 328bc2b7dabSGregor Boirie unsigned int iio_get_time_res(const struct iio_dev *indio_dev) 329bc2b7dabSGregor Boirie { 330bc2b7dabSGregor Boirie switch (iio_device_get_clock(indio_dev)) { 331bc2b7dabSGregor Boirie case CLOCK_REALTIME: 332bc2b7dabSGregor Boirie case CLOCK_MONOTONIC: 333bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_RAW: 334bc2b7dabSGregor Boirie case CLOCK_BOOTTIME: 335bc2b7dabSGregor Boirie case CLOCK_TAI: 336bc2b7dabSGregor Boirie return hrtimer_resolution; 337bc2b7dabSGregor Boirie case CLOCK_REALTIME_COARSE: 338bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_COARSE: 339bc2b7dabSGregor Boirie return LOW_RES_NSEC; 340bc2b7dabSGregor Boirie default: 341bc2b7dabSGregor Boirie BUG(); 342bc2b7dabSGregor Boirie } 343bc2b7dabSGregor Boirie } 344bc2b7dabSGregor Boirie EXPORT_SYMBOL(iio_get_time_res); 345bc2b7dabSGregor Boirie 346a980e046SJonathan Cameron static int __init iio_init(void) 347a980e046SJonathan Cameron { 348a980e046SJonathan Cameron int ret; 349a980e046SJonathan Cameron 350a980e046SJonathan Cameron /* Register sysfs bus */ 351a980e046SJonathan Cameron ret = bus_register(&iio_bus_type); 352a980e046SJonathan Cameron if (ret < 0) { 3533176dd5dSSachin Kamat pr_err("could not register bus type\n"); 354a980e046SJonathan Cameron goto error_nothing; 355a980e046SJonathan Cameron } 356a980e046SJonathan Cameron 357a980e046SJonathan Cameron ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio"); 358a980e046SJonathan Cameron if (ret < 0) { 3593176dd5dSSachin Kamat pr_err("failed to allocate char dev region\n"); 360a980e046SJonathan Cameron goto error_unregister_bus_type; 361a980e046SJonathan Cameron } 362a980e046SJonathan Cameron 363a980e046SJonathan Cameron iio_debugfs_dentry = debugfs_create_dir("iio", NULL); 364a980e046SJonathan Cameron 365a980e046SJonathan Cameron return 0; 366a980e046SJonathan Cameron 367a980e046SJonathan Cameron error_unregister_bus_type: 368a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 369a980e046SJonathan Cameron error_nothing: 370a980e046SJonathan Cameron return ret; 371a980e046SJonathan Cameron } 372a980e046SJonathan Cameron 373a980e046SJonathan Cameron static void __exit iio_exit(void) 374a980e046SJonathan Cameron { 375a980e046SJonathan Cameron if (iio_devt) 376a980e046SJonathan Cameron unregister_chrdev_region(iio_devt, IIO_DEV_MAX); 377a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 378a980e046SJonathan Cameron debugfs_remove(iio_debugfs_dentry); 379a980e046SJonathan Cameron } 380a980e046SJonathan Cameron 381a980e046SJonathan Cameron #if defined(CONFIG_DEBUG_FS) 382a980e046SJonathan Cameron static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf, 383a980e046SJonathan Cameron size_t count, loff_t *ppos) 384a980e046SJonathan Cameron { 385a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 38696fb1b67SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 387a980e046SJonathan Cameron unsigned val = 0; 388a980e046SJonathan Cameron int ret; 389a980e046SJonathan Cameron 3902ddc982aSAlexandru Tachici if (*ppos > 0) 3912ddc982aSAlexandru Tachici return simple_read_from_buffer(userbuf, count, ppos, 39296fb1b67SAlexandru Ardelean iio_dev_opaque->read_buf, 39396fb1b67SAlexandru Ardelean iio_dev_opaque->read_buf_len); 3942ddc982aSAlexandru Tachici 395a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, 39696fb1b67SAlexandru Ardelean iio_dev_opaque->cached_reg_addr, 397a980e046SJonathan Cameron 0, &val); 3983d62c78aSMatt Fornero if (ret) { 399a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__); 4003d62c78aSMatt Fornero return ret; 4013d62c78aSMatt Fornero } 402a980e046SJonathan Cameron 40396fb1b67SAlexandru Ardelean iio_dev_opaque->read_buf_len = snprintf(iio_dev_opaque->read_buf, 40496fb1b67SAlexandru Ardelean sizeof(iio_dev_opaque->read_buf), 4052ddc982aSAlexandru Tachici "0x%X\n", val); 406a980e046SJonathan Cameron 4072ddc982aSAlexandru Tachici return simple_read_from_buffer(userbuf, count, ppos, 40896fb1b67SAlexandru Ardelean iio_dev_opaque->read_buf, 40996fb1b67SAlexandru Ardelean iio_dev_opaque->read_buf_len); 410a980e046SJonathan Cameron } 411a980e046SJonathan Cameron 412a980e046SJonathan Cameron static ssize_t iio_debugfs_write_reg(struct file *file, 413a980e046SJonathan Cameron const char __user *userbuf, size_t count, loff_t *ppos) 414a980e046SJonathan Cameron { 415a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 41696fb1b67SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 417a980e046SJonathan Cameron unsigned reg, val; 418a980e046SJonathan Cameron char buf[80]; 419a980e046SJonathan Cameron int ret; 420a980e046SJonathan Cameron 421a980e046SJonathan Cameron count = min_t(size_t, count, (sizeof(buf)-1)); 422a980e046SJonathan Cameron if (copy_from_user(buf, userbuf, count)) 423a980e046SJonathan Cameron return -EFAULT; 424a980e046SJonathan Cameron 425a980e046SJonathan Cameron buf[count] = 0; 426a980e046SJonathan Cameron 427a980e046SJonathan Cameron ret = sscanf(buf, "%i %i", ®, &val); 428a980e046SJonathan Cameron 429a980e046SJonathan Cameron switch (ret) { 430a980e046SJonathan Cameron case 1: 43196fb1b67SAlexandru Ardelean iio_dev_opaque->cached_reg_addr = reg; 432a980e046SJonathan Cameron break; 433a980e046SJonathan Cameron case 2: 43496fb1b67SAlexandru Ardelean iio_dev_opaque->cached_reg_addr = reg; 435a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, reg, 436a980e046SJonathan Cameron val, NULL); 437a980e046SJonathan Cameron if (ret) { 438a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: write failed\n", 439a980e046SJonathan Cameron __func__); 440a980e046SJonathan Cameron return ret; 441a980e046SJonathan Cameron } 442a980e046SJonathan Cameron break; 443a980e046SJonathan Cameron default: 444a980e046SJonathan Cameron return -EINVAL; 445a980e046SJonathan Cameron } 446a980e046SJonathan Cameron 447a980e046SJonathan Cameron return count; 448a980e046SJonathan Cameron } 449a980e046SJonathan Cameron 450a980e046SJonathan Cameron static const struct file_operations iio_debugfs_reg_fops = { 4515a28c873SAxel Lin .open = simple_open, 452a980e046SJonathan Cameron .read = iio_debugfs_read_reg, 453a980e046SJonathan Cameron .write = iio_debugfs_write_reg, 454a980e046SJonathan Cameron }; 455a980e046SJonathan Cameron 456a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 457a980e046SJonathan Cameron { 45896fb1b67SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 45996fb1b67SAlexandru Ardelean debugfs_remove_recursive(iio_dev_opaque->debugfs_dentry); 460a980e046SJonathan Cameron } 461a980e046SJonathan Cameron 4628915aacaSGreg Kroah-Hartman static void iio_device_register_debugfs(struct iio_dev *indio_dev) 463a980e046SJonathan Cameron { 46496fb1b67SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque; 46596fb1b67SAlexandru Ardelean 466a980e046SJonathan Cameron if (indio_dev->info->debugfs_reg_access == NULL) 4678915aacaSGreg Kroah-Hartman return; 468a980e046SJonathan Cameron 469abd5a2fbSAxel Lin if (!iio_debugfs_dentry) 4708915aacaSGreg Kroah-Hartman return; 471a980e046SJonathan Cameron 47296fb1b67SAlexandru Ardelean iio_dev_opaque = to_iio_dev_opaque(indio_dev); 47396fb1b67SAlexandru Ardelean 47496fb1b67SAlexandru Ardelean iio_dev_opaque->debugfs_dentry = 475a980e046SJonathan Cameron debugfs_create_dir(dev_name(&indio_dev->dev), 476a980e046SJonathan Cameron iio_debugfs_dentry); 477a980e046SJonathan Cameron 4788915aacaSGreg Kroah-Hartman debugfs_create_file("direct_reg_access", 0644, 47996fb1b67SAlexandru Ardelean iio_dev_opaque->debugfs_dentry, indio_dev, 4808915aacaSGreg Kroah-Hartman &iio_debugfs_reg_fops); 481a980e046SJonathan Cameron } 482a980e046SJonathan Cameron #else 4838915aacaSGreg Kroah-Hartman static void iio_device_register_debugfs(struct iio_dev *indio_dev) 484a980e046SJonathan Cameron { 485a980e046SJonathan Cameron } 486a980e046SJonathan Cameron 487a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 488a980e046SJonathan Cameron { 489a980e046SJonathan Cameron } 490a980e046SJonathan Cameron #endif /* CONFIG_DEBUG_FS */ 491a980e046SJonathan Cameron 492a980e046SJonathan Cameron static ssize_t iio_read_channel_ext_info(struct device *dev, 493a980e046SJonathan Cameron struct device_attribute *attr, 494a980e046SJonathan Cameron char *buf) 495a980e046SJonathan Cameron { 496e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 497a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 498a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 499a980e046SJonathan Cameron 500a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 501a980e046SJonathan Cameron 502fc6d1139SMichael Hennerich return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf); 503a980e046SJonathan Cameron } 504a980e046SJonathan Cameron 505a980e046SJonathan Cameron static ssize_t iio_write_channel_ext_info(struct device *dev, 506a980e046SJonathan Cameron struct device_attribute *attr, 507a980e046SJonathan Cameron const char *buf, 508a980e046SJonathan Cameron size_t len) 509a980e046SJonathan Cameron { 510e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 511a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 512a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 513a980e046SJonathan Cameron 514a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 515a980e046SJonathan Cameron 516fc6d1139SMichael Hennerich return ext_info->write(indio_dev, ext_info->private, 517fc6d1139SMichael Hennerich this_attr->c, buf, len); 518a980e046SJonathan Cameron } 519a980e046SJonathan Cameron 5205212cc8aSLars-Peter Clausen ssize_t iio_enum_available_read(struct iio_dev *indio_dev, 5215212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 5225212cc8aSLars-Peter Clausen { 5235212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 5245212cc8aSLars-Peter Clausen unsigned int i; 5255212cc8aSLars-Peter Clausen size_t len = 0; 5265212cc8aSLars-Peter Clausen 5275212cc8aSLars-Peter Clausen if (!e->num_items) 5285212cc8aSLars-Peter Clausen return 0; 5295212cc8aSLars-Peter Clausen 530d9a0e73cSLars-Peter Clausen for (i = 0; i < e->num_items; ++i) { 531d9a0e73cSLars-Peter Clausen if (!e->items[i]) 532d9a0e73cSLars-Peter Clausen continue; 5330207483bSLars-Peter Clausen len += sysfs_emit_at(buf, len, "%s ", e->items[i]); 534d9a0e73cSLars-Peter Clausen } 5355212cc8aSLars-Peter Clausen 5365212cc8aSLars-Peter Clausen /* replace last space with a newline */ 5375212cc8aSLars-Peter Clausen buf[len - 1] = '\n'; 5385212cc8aSLars-Peter Clausen 5395212cc8aSLars-Peter Clausen return len; 5405212cc8aSLars-Peter Clausen } 5415212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_available_read); 5425212cc8aSLars-Peter Clausen 5435212cc8aSLars-Peter Clausen ssize_t iio_enum_read(struct iio_dev *indio_dev, 5445212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 5455212cc8aSLars-Peter Clausen { 5465212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 5475212cc8aSLars-Peter Clausen int i; 5485212cc8aSLars-Peter Clausen 5495212cc8aSLars-Peter Clausen if (!e->get) 5505212cc8aSLars-Peter Clausen return -EINVAL; 5515212cc8aSLars-Peter Clausen 5525212cc8aSLars-Peter Clausen i = e->get(indio_dev, chan); 5535212cc8aSLars-Peter Clausen if (i < 0) 5545212cc8aSLars-Peter Clausen return i; 555d9a0e73cSLars-Peter Clausen else if (i >= e->num_items || !e->items[i]) 5565212cc8aSLars-Peter Clausen return -EINVAL; 5575212cc8aSLars-Peter Clausen 55883ca56b6SLars-Peter Clausen return sysfs_emit(buf, "%s\n", e->items[i]); 5595212cc8aSLars-Peter Clausen } 5605212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_read); 5615212cc8aSLars-Peter Clausen 5625212cc8aSLars-Peter Clausen ssize_t iio_enum_write(struct iio_dev *indio_dev, 5635212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, const char *buf, 5645212cc8aSLars-Peter Clausen size_t len) 5655212cc8aSLars-Peter Clausen { 5665212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 5675212cc8aSLars-Peter Clausen int ret; 5685212cc8aSLars-Peter Clausen 5695212cc8aSLars-Peter Clausen if (!e->set) 5705212cc8aSLars-Peter Clausen return -EINVAL; 5715212cc8aSLars-Peter Clausen 572d9a0e73cSLars-Peter Clausen ret = iio_sysfs_match_string_with_gaps(e->items, e->num_items, buf); 57302e9a0ffSAndy Shevchenko if (ret < 0) 57402e9a0ffSAndy Shevchenko return ret; 5755212cc8aSLars-Peter Clausen 57602e9a0ffSAndy Shevchenko ret = e->set(indio_dev, chan, ret); 5775212cc8aSLars-Peter Clausen return ret ? ret : len; 5785212cc8aSLars-Peter Clausen } 5795212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_write); 5805212cc8aSLars-Peter Clausen 581dfc57732SGregor Boirie static const struct iio_mount_matrix iio_mount_idmatrix = { 582dfc57732SGregor Boirie .rotation = { 583dfc57732SGregor Boirie "1", "0", "0", 584dfc57732SGregor Boirie "0", "1", "0", 585dfc57732SGregor Boirie "0", "0", "1" 586dfc57732SGregor Boirie } 587dfc57732SGregor Boirie }; 588dfc57732SGregor Boirie 589dfc57732SGregor Boirie static int iio_setup_mount_idmatrix(const struct device *dev, 590dfc57732SGregor Boirie struct iio_mount_matrix *matrix) 591dfc57732SGregor Boirie { 592dfc57732SGregor Boirie *matrix = iio_mount_idmatrix; 593dfc57732SGregor Boirie dev_info(dev, "mounting matrix not found: using identity...\n"); 594dfc57732SGregor Boirie return 0; 595dfc57732SGregor Boirie } 596dfc57732SGregor Boirie 597dfc57732SGregor Boirie ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv, 598dfc57732SGregor Boirie const struct iio_chan_spec *chan, char *buf) 599dfc57732SGregor Boirie { 600dfc57732SGregor Boirie const struct iio_mount_matrix *mtx = ((iio_get_mount_matrix_t *) 601dfc57732SGregor Boirie priv)(indio_dev, chan); 602dfc57732SGregor Boirie 603dfc57732SGregor Boirie if (IS_ERR(mtx)) 604dfc57732SGregor Boirie return PTR_ERR(mtx); 605dfc57732SGregor Boirie 606dfc57732SGregor Boirie if (!mtx) 607dfc57732SGregor Boirie mtx = &iio_mount_idmatrix; 608dfc57732SGregor Boirie 60983ca56b6SLars-Peter Clausen return sysfs_emit(buf, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n", 610dfc57732SGregor Boirie mtx->rotation[0], mtx->rotation[1], mtx->rotation[2], 611dfc57732SGregor Boirie mtx->rotation[3], mtx->rotation[4], mtx->rotation[5], 612dfc57732SGregor Boirie mtx->rotation[6], mtx->rotation[7], mtx->rotation[8]); 613dfc57732SGregor Boirie } 614dfc57732SGregor Boirie EXPORT_SYMBOL_GPL(iio_show_mount_matrix); 615dfc57732SGregor Boirie 616dfc57732SGregor Boirie /** 617fb158971SAndy Shevchenko * iio_read_mount_matrix() - retrieve iio device mounting matrix from 618fb158971SAndy Shevchenko * device "mount-matrix" property 619dfc57732SGregor Boirie * @dev: device the mounting matrix property is assigned to 620dfc57732SGregor Boirie * @matrix: where to store retrieved matrix 621dfc57732SGregor Boirie * 622dfc57732SGregor Boirie * If device is assigned no mounting matrix property, a default 3x3 identity 623dfc57732SGregor Boirie * matrix will be filled in. 624dfc57732SGregor Boirie * 625dfc57732SGregor Boirie * Return: 0 if success, or a negative error code on failure. 626dfc57732SGregor Boirie */ 627b892770aSAndy Shevchenko int iio_read_mount_matrix(struct device *dev, struct iio_mount_matrix *matrix) 628dfc57732SGregor Boirie { 629fb158971SAndy Shevchenko size_t len = ARRAY_SIZE(iio_mount_idmatrix.rotation); 630fb158971SAndy Shevchenko int err; 631dfc57732SGregor Boirie 632b892770aSAndy Shevchenko err = device_property_read_string_array(dev, "mount-matrix", matrix->rotation, len); 633fb158971SAndy Shevchenko if (err == len) 634dfc57732SGregor Boirie return 0; 635dfc57732SGregor Boirie 636dfc57732SGregor Boirie if (err >= 0) 637dfc57732SGregor Boirie /* Invalid number of matrix entries. */ 638dfc57732SGregor Boirie return -EINVAL; 639dfc57732SGregor Boirie 640dfc57732SGregor Boirie if (err != -EINVAL) 641dfc57732SGregor Boirie /* Invalid matrix declaration format. */ 642dfc57732SGregor Boirie return err; 643dfc57732SGregor Boirie 644dfc57732SGregor Boirie /* Matrix was not declared at all: fallback to identity. */ 645dfc57732SGregor Boirie return iio_setup_mount_idmatrix(dev, matrix); 646dfc57732SGregor Boirie } 647fb158971SAndy Shevchenko EXPORT_SYMBOL(iio_read_mount_matrix); 648dfc57732SGregor Boirie 6496b92ba0aSLars-Peter Clausen static ssize_t __iio_format_value(char *buf, size_t offset, unsigned int type, 65051239600SJonathan Cameron int size, const int *vals) 65151239600SJonathan Cameron { 65251239600SJonathan Cameron int tmp0, tmp1; 6530e7a3978SAnand Ashok Dumbre s64 tmp2; 65451239600SJonathan Cameron bool scale_db = false; 65551239600SJonathan Cameron 65651239600SJonathan Cameron switch (type) { 65751239600SJonathan Cameron case IIO_VAL_INT: 6586b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "%d", vals[0]); 65951239600SJonathan Cameron case IIO_VAL_INT_PLUS_MICRO_DB: 66051239600SJonathan Cameron scale_db = true; 661df561f66SGustavo A. R. Silva fallthrough; 66251239600SJonathan Cameron case IIO_VAL_INT_PLUS_MICRO: 66351239600SJonathan Cameron if (vals[1] < 0) 6646b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "-%d.%06u%s", 6656b92ba0aSLars-Peter Clausen abs(vals[0]), -vals[1], 66651239600SJonathan Cameron scale_db ? " dB" : ""); 6676b92ba0aSLars-Peter Clausen else 6686b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "%d.%06u%s", vals[0], 6696b92ba0aSLars-Peter Clausen vals[1], scale_db ? " dB" : ""); 67051239600SJonathan Cameron case IIO_VAL_INT_PLUS_NANO: 67151239600SJonathan Cameron if (vals[1] < 0) 6726b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "-%d.%09u", 6736b92ba0aSLars-Peter Clausen abs(vals[0]), -vals[1]); 67451239600SJonathan Cameron else 6756b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "%d.%09u", vals[0], 6766b92ba0aSLars-Peter Clausen vals[1]); 67751239600SJonathan Cameron case IIO_VAL_FRACTIONAL: 6780e7a3978SAnand Ashok Dumbre tmp2 = div_s64((s64)vals[0] * 1000000000LL, vals[1]); 67951239600SJonathan Cameron tmp1 = vals[1]; 6800e7a3978SAnand Ashok Dumbre tmp0 = (int)div_s64_rem(tmp2, 1000000000, &tmp1); 6810e7a3978SAnand Ashok Dumbre if ((tmp2 < 0) && (tmp0 == 0)) 6826b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "-0.%09u", abs(tmp1)); 6830e7a3978SAnand Ashok Dumbre else 6846b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "%d.%09u", tmp0, 6856b92ba0aSLars-Peter Clausen abs(tmp1)); 68651239600SJonathan Cameron case IIO_VAL_FRACTIONAL_LOG2: 6872646a95dSLars-Peter Clausen tmp2 = shift_right((s64)vals[0] * 1000000000LL, vals[1]); 6882646a95dSLars-Peter Clausen tmp0 = (int)div_s64_rem(tmp2, 1000000000LL, &tmp1); 68938a52cdeSLars-Peter Clausen if (tmp0 == 0 && tmp2 < 0) 6906b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "-0.%09u", abs(tmp1)); 69138a52cdeSLars-Peter Clausen else 6926b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "%d.%09u", tmp0, 6936b92ba0aSLars-Peter Clausen abs(tmp1)); 69451239600SJonathan Cameron case IIO_VAL_INT_MULTIPLE: 69551239600SJonathan Cameron { 69651239600SJonathan Cameron int i; 69751239600SJonathan Cameron int l = 0; 69851239600SJonathan Cameron 6996b92ba0aSLars-Peter Clausen for (i = 0; i < size; ++i) 7006b92ba0aSLars-Peter Clausen l += sysfs_emit_at(buf, offset + l, "%d ", vals[i]); 70151239600SJonathan Cameron return l; 70251239600SJonathan Cameron } 7038cb34036SAndrea Merello case IIO_VAL_CHAR: 7046b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "%c", (char)vals[0]); 70551239600SJonathan Cameron default: 70651239600SJonathan Cameron return 0; 70751239600SJonathan Cameron } 70851239600SJonathan Cameron } 70951239600SJonathan Cameron 7103661f3f5SLars-Peter Clausen /** 7113661f3f5SLars-Peter Clausen * iio_format_value() - Formats a IIO value into its string representation 7122498dcf6SCristina Opriceana * @buf: The buffer to which the formatted value gets written 71351239600SJonathan Cameron * which is assumed to be big enough (i.e. PAGE_SIZE). 714c175cb7cSRandy Dunlap * @type: One of the IIO_VAL_* constants. This decides how the val 7152498dcf6SCristina Opriceana * and val2 parameters are formatted. 7162498dcf6SCristina Opriceana * @size: Number of IIO value entries contained in vals 7172498dcf6SCristina Opriceana * @vals: Pointer to the values, exact meaning depends on the 7182498dcf6SCristina Opriceana * type parameter. 7192498dcf6SCristina Opriceana * 7202498dcf6SCristina Opriceana * Return: 0 by default, a negative number on failure or the 7212498dcf6SCristina Opriceana * total number of characters written for a type that belongs 722c175cb7cSRandy Dunlap * to the IIO_VAL_* constant. 7233661f3f5SLars-Peter Clausen */ 7249fbfb4b3SSrinivas Pandruvada ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals) 725a980e046SJonathan Cameron { 72651239600SJonathan Cameron ssize_t len; 727a980e046SJonathan Cameron 7286b92ba0aSLars-Peter Clausen len = __iio_format_value(buf, 0, type, size, vals); 72951239600SJonathan Cameron if (len >= PAGE_SIZE - 1) 73051239600SJonathan Cameron return -EFBIG; 7319fbfb4b3SSrinivas Pandruvada 7326b92ba0aSLars-Peter Clausen return len + sysfs_emit_at(buf, len, "\n"); 73367eedba3SMichael Hennerich } 7347d2c2acaSAndrew F. Davis EXPORT_SYMBOL_GPL(iio_format_value); 735a980e046SJonathan Cameron 7361d4ef9b3SCristian Pop static ssize_t iio_read_channel_label(struct device *dev, 7371d4ef9b3SCristian Pop struct device_attribute *attr, 7381d4ef9b3SCristian Pop char *buf) 7391d4ef9b3SCristian Pop { 7401d4ef9b3SCristian Pop struct iio_dev *indio_dev = dev_to_iio_dev(dev); 7411d4ef9b3SCristian Pop struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 7421d4ef9b3SCristian Pop 74313efdc3dSPaul Cercueil if (indio_dev->info->read_label) 7441d4ef9b3SCristian Pop return indio_dev->info->read_label(indio_dev, this_attr->c, buf); 74513efdc3dSPaul Cercueil 74613efdc3dSPaul Cercueil if (this_attr->c->extend_name) 74713efdc3dSPaul Cercueil return sprintf(buf, "%s\n", this_attr->c->extend_name); 74813efdc3dSPaul Cercueil 74913efdc3dSPaul Cercueil return -EINVAL; 7501d4ef9b3SCristian Pop } 7511d4ef9b3SCristian Pop 7523661f3f5SLars-Peter Clausen static ssize_t iio_read_channel_info(struct device *dev, 7533661f3f5SLars-Peter Clausen struct device_attribute *attr, 7543661f3f5SLars-Peter Clausen char *buf) 7553661f3f5SLars-Peter Clausen { 7563661f3f5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 7573661f3f5SLars-Peter Clausen struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 7589fbfb4b3SSrinivas Pandruvada int vals[INDIO_MAX_RAW_ELEMENTS]; 7599fbfb4b3SSrinivas Pandruvada int ret; 7609fbfb4b3SSrinivas Pandruvada int val_len = 2; 7619fbfb4b3SSrinivas Pandruvada 7629fbfb4b3SSrinivas Pandruvada if (indio_dev->info->read_raw_multi) 7639fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c, 7649fbfb4b3SSrinivas Pandruvada INDIO_MAX_RAW_ELEMENTS, 7659fbfb4b3SSrinivas Pandruvada vals, &val_len, 7669fbfb4b3SSrinivas Pandruvada this_attr->address); 7679fbfb4b3SSrinivas Pandruvada else 7689fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw(indio_dev, this_attr->c, 7699fbfb4b3SSrinivas Pandruvada &vals[0], &vals[1], this_attr->address); 7703661f3f5SLars-Peter Clausen 7713661f3f5SLars-Peter Clausen if (ret < 0) 7723661f3f5SLars-Peter Clausen return ret; 7733661f3f5SLars-Peter Clausen 7749fbfb4b3SSrinivas Pandruvada return iio_format_value(buf, ret, val_len, vals); 7753661f3f5SLars-Peter Clausen } 7763661f3f5SLars-Peter Clausen 777eda20ba1SLars-Peter Clausen static ssize_t iio_format_list(char *buf, const int *vals, int type, int length, 778eda20ba1SLars-Peter Clausen const char *prefix, const char *suffix) 77951239600SJonathan Cameron { 780eda20ba1SLars-Peter Clausen ssize_t len; 781e08b60d3SLars-Peter Clausen int stride; 78251239600SJonathan Cameron int i; 783eda20ba1SLars-Peter Clausen 78451239600SJonathan Cameron switch (type) { 78551239600SJonathan Cameron case IIO_VAL_INT: 786e08b60d3SLars-Peter Clausen stride = 1; 78751239600SJonathan Cameron break; 78851239600SJonathan Cameron default: 789e08b60d3SLars-Peter Clausen stride = 2; 790e08b60d3SLars-Peter Clausen break; 791e08b60d3SLars-Peter Clausen } 792e08b60d3SLars-Peter Clausen 7936b92ba0aSLars-Peter Clausen len = sysfs_emit(buf, prefix); 794e08b60d3SLars-Peter Clausen 795e08b60d3SLars-Peter Clausen for (i = 0; i <= length - stride; i += stride) { 796e08b60d3SLars-Peter Clausen if (i != 0) { 7976b92ba0aSLars-Peter Clausen len += sysfs_emit_at(buf, len, " "); 79851239600SJonathan Cameron if (len >= PAGE_SIZE) 79951239600SJonathan Cameron return -EFBIG; 80051239600SJonathan Cameron } 801e08b60d3SLars-Peter Clausen 8026b92ba0aSLars-Peter Clausen len += __iio_format_value(buf, len, type, stride, &vals[i]); 803e08b60d3SLars-Peter Clausen if (len >= PAGE_SIZE) 804e08b60d3SLars-Peter Clausen return -EFBIG; 80551239600SJonathan Cameron } 80651239600SJonathan Cameron 8076b92ba0aSLars-Peter Clausen len += sysfs_emit_at(buf, len, "%s\n", suffix); 808e08b60d3SLars-Peter Clausen 80951239600SJonathan Cameron return len; 81051239600SJonathan Cameron } 81151239600SJonathan Cameron 812eda20ba1SLars-Peter Clausen static ssize_t iio_format_avail_list(char *buf, const int *vals, 813eda20ba1SLars-Peter Clausen int type, int length) 814eda20ba1SLars-Peter Clausen { 815eda20ba1SLars-Peter Clausen 816eda20ba1SLars-Peter Clausen return iio_format_list(buf, vals, type, length, "", ""); 817eda20ba1SLars-Peter Clausen } 818eda20ba1SLars-Peter Clausen 81951239600SJonathan Cameron static ssize_t iio_format_avail_range(char *buf, const int *vals, int type) 82051239600SJonathan Cameron { 821eda20ba1SLars-Peter Clausen return iio_format_list(buf, vals, type, 3, "[", "]"); 82251239600SJonathan Cameron } 82351239600SJonathan Cameron 82451239600SJonathan Cameron static ssize_t iio_read_channel_info_avail(struct device *dev, 82551239600SJonathan Cameron struct device_attribute *attr, 82651239600SJonathan Cameron char *buf) 82751239600SJonathan Cameron { 82851239600SJonathan Cameron struct iio_dev *indio_dev = dev_to_iio_dev(dev); 82951239600SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 83051239600SJonathan Cameron const int *vals; 83151239600SJonathan Cameron int ret; 83251239600SJonathan Cameron int length; 83351239600SJonathan Cameron int type; 83451239600SJonathan Cameron 83551239600SJonathan Cameron ret = indio_dev->info->read_avail(indio_dev, this_attr->c, 83651239600SJonathan Cameron &vals, &type, &length, 83751239600SJonathan Cameron this_attr->address); 83851239600SJonathan Cameron 83951239600SJonathan Cameron if (ret < 0) 84051239600SJonathan Cameron return ret; 84151239600SJonathan Cameron switch (ret) { 84251239600SJonathan Cameron case IIO_AVAIL_LIST: 84351239600SJonathan Cameron return iio_format_avail_list(buf, vals, type, length); 84451239600SJonathan Cameron case IIO_AVAIL_RANGE: 84551239600SJonathan Cameron return iio_format_avail_range(buf, vals, type); 84651239600SJonathan Cameron default: 84751239600SJonathan Cameron return -EINVAL; 84851239600SJonathan Cameron } 84951239600SJonathan Cameron } 85051239600SJonathan Cameron 8516807d721SLars-Peter Clausen /** 852b8528224SBeniamin Bia * __iio_str_to_fixpoint() - Parse a fixed-point number from a string 8536807d721SLars-Peter Clausen * @str: The string to parse 8546807d721SLars-Peter Clausen * @fract_mult: Multiplier for the first decimal place, should be a power of 10 8556807d721SLars-Peter Clausen * @integer: The integer part of the number 8566807d721SLars-Peter Clausen * @fract: The fractional part of the number 857b8528224SBeniamin Bia * @scale_db: True if this should parse as dB 8586807d721SLars-Peter Clausen * 8596807d721SLars-Peter Clausen * Returns 0 on success, or a negative error code if the string could not be 8606807d721SLars-Peter Clausen * parsed. 8616807d721SLars-Peter Clausen */ 862b8528224SBeniamin Bia static int __iio_str_to_fixpoint(const char *str, int fract_mult, 863b8528224SBeniamin Bia int *integer, int *fract, bool scale_db) 8646807d721SLars-Peter Clausen { 8656807d721SLars-Peter Clausen int i = 0, f = 0; 8666807d721SLars-Peter Clausen bool integer_part = true, negative = false; 8676807d721SLars-Peter Clausen 868f47dff32SSean Nyekjaer if (fract_mult == 0) { 869f47dff32SSean Nyekjaer *fract = 0; 870f47dff32SSean Nyekjaer 871f47dff32SSean Nyekjaer return kstrtoint(str, 0, integer); 872f47dff32SSean Nyekjaer } 873f47dff32SSean Nyekjaer 8746807d721SLars-Peter Clausen if (str[0] == '-') { 8756807d721SLars-Peter Clausen negative = true; 8766807d721SLars-Peter Clausen str++; 8776807d721SLars-Peter Clausen } else if (str[0] == '+') { 8786807d721SLars-Peter Clausen str++; 8796807d721SLars-Peter Clausen } 8806807d721SLars-Peter Clausen 8816807d721SLars-Peter Clausen while (*str) { 8826807d721SLars-Peter Clausen if ('0' <= *str && *str <= '9') { 8836807d721SLars-Peter Clausen if (integer_part) { 8846807d721SLars-Peter Clausen i = i * 10 + *str - '0'; 8856807d721SLars-Peter Clausen } else { 8866807d721SLars-Peter Clausen f += fract_mult * (*str - '0'); 8876807d721SLars-Peter Clausen fract_mult /= 10; 8886807d721SLars-Peter Clausen } 8896807d721SLars-Peter Clausen } else if (*str == '\n') { 8906807d721SLars-Peter Clausen if (*(str + 1) == '\0') 8916807d721SLars-Peter Clausen break; 8926807d721SLars-Peter Clausen else 8936807d721SLars-Peter Clausen return -EINVAL; 894b8528224SBeniamin Bia } else if (!strncmp(str, " dB", sizeof(" dB") - 1) && scale_db) { 895b8528224SBeniamin Bia /* Ignore the dB suffix */ 896b8528224SBeniamin Bia str += sizeof(" dB") - 1; 897b8528224SBeniamin Bia continue; 898b8528224SBeniamin Bia } else if (!strncmp(str, "dB", sizeof("dB") - 1) && scale_db) { 899b8528224SBeniamin Bia /* Ignore the dB suffix */ 900b8528224SBeniamin Bia str += sizeof("dB") - 1; 901b8528224SBeniamin Bia continue; 9026807d721SLars-Peter Clausen } else if (*str == '.' && integer_part) { 9036807d721SLars-Peter Clausen integer_part = false; 9046807d721SLars-Peter Clausen } else { 9056807d721SLars-Peter Clausen return -EINVAL; 9066807d721SLars-Peter Clausen } 9076807d721SLars-Peter Clausen str++; 9086807d721SLars-Peter Clausen } 9096807d721SLars-Peter Clausen 9106807d721SLars-Peter Clausen if (negative) { 9116807d721SLars-Peter Clausen if (i) 9126807d721SLars-Peter Clausen i = -i; 9136807d721SLars-Peter Clausen else 9146807d721SLars-Peter Clausen f = -f; 9156807d721SLars-Peter Clausen } 9166807d721SLars-Peter Clausen 9176807d721SLars-Peter Clausen *integer = i; 9186807d721SLars-Peter Clausen *fract = f; 9196807d721SLars-Peter Clausen 9206807d721SLars-Peter Clausen return 0; 9216807d721SLars-Peter Clausen } 922b8528224SBeniamin Bia 923b8528224SBeniamin Bia /** 924b8528224SBeniamin Bia * iio_str_to_fixpoint() - Parse a fixed-point number from a string 925b8528224SBeniamin Bia * @str: The string to parse 926b8528224SBeniamin Bia * @fract_mult: Multiplier for the first decimal place, should be a power of 10 927b8528224SBeniamin Bia * @integer: The integer part of the number 928b8528224SBeniamin Bia * @fract: The fractional part of the number 929b8528224SBeniamin Bia * 930b8528224SBeniamin Bia * Returns 0 on success, or a negative error code if the string could not be 931b8528224SBeniamin Bia * parsed. 932b8528224SBeniamin Bia */ 933b8528224SBeniamin Bia int iio_str_to_fixpoint(const char *str, int fract_mult, 934b8528224SBeniamin Bia int *integer, int *fract) 935b8528224SBeniamin Bia { 936b8528224SBeniamin Bia return __iio_str_to_fixpoint(str, fract_mult, integer, fract, false); 937b8528224SBeniamin Bia } 9386807d721SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_str_to_fixpoint); 9396807d721SLars-Peter Clausen 940a980e046SJonathan Cameron static ssize_t iio_write_channel_info(struct device *dev, 941a980e046SJonathan Cameron struct device_attribute *attr, 942a980e046SJonathan Cameron const char *buf, 943a980e046SJonathan Cameron size_t len) 944a980e046SJonathan Cameron { 945e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 946a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 9476807d721SLars-Peter Clausen int ret, fract_mult = 100000; 9488cb34036SAndrea Merello int integer, fract = 0; 9498cb34036SAndrea Merello bool is_char = false; 950b8528224SBeniamin Bia bool scale_db = false; 951a980e046SJonathan Cameron 952a980e046SJonathan Cameron /* Assumes decimal - precision based on number of digits */ 953a980e046SJonathan Cameron if (!indio_dev->info->write_raw) 954a980e046SJonathan Cameron return -EINVAL; 955a980e046SJonathan Cameron 956a980e046SJonathan Cameron if (indio_dev->info->write_raw_get_fmt) 957a980e046SJonathan Cameron switch (indio_dev->info->write_raw_get_fmt(indio_dev, 958a980e046SJonathan Cameron this_attr->c, this_attr->address)) { 959f47dff32SSean Nyekjaer case IIO_VAL_INT: 960f47dff32SSean Nyekjaer fract_mult = 0; 961f47dff32SSean Nyekjaer break; 962b8528224SBeniamin Bia case IIO_VAL_INT_PLUS_MICRO_DB: 963b8528224SBeniamin Bia scale_db = true; 964df561f66SGustavo A. R. Silva fallthrough; 965a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_MICRO: 966a980e046SJonathan Cameron fract_mult = 100000; 967a980e046SJonathan Cameron break; 968a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_NANO: 969a980e046SJonathan Cameron fract_mult = 100000000; 970a980e046SJonathan Cameron break; 9718cb34036SAndrea Merello case IIO_VAL_CHAR: 9728cb34036SAndrea Merello is_char = true; 9738cb34036SAndrea Merello break; 974a980e046SJonathan Cameron default: 975a980e046SJonathan Cameron return -EINVAL; 976a980e046SJonathan Cameron } 977a980e046SJonathan Cameron 9788cb34036SAndrea Merello if (is_char) { 9798cb34036SAndrea Merello char ch; 9808cb34036SAndrea Merello 9818cb34036SAndrea Merello if (sscanf(buf, "%c", &ch) != 1) 9828cb34036SAndrea Merello return -EINVAL; 9838cb34036SAndrea Merello integer = ch; 9848cb34036SAndrea Merello } else { 985b8528224SBeniamin Bia ret = __iio_str_to_fixpoint(buf, fract_mult, &integer, &fract, 986b8528224SBeniamin Bia scale_db); 987b8528224SBeniamin Bia if (ret) 988b8528224SBeniamin Bia return ret; 9895403f280SMircea Caprioru } 990a980e046SJonathan Cameron 991a980e046SJonathan Cameron ret = indio_dev->info->write_raw(indio_dev, this_attr->c, 992a980e046SJonathan Cameron integer, fract, this_attr->address); 993a980e046SJonathan Cameron if (ret) 994a980e046SJonathan Cameron return ret; 995a980e046SJonathan Cameron 996a980e046SJonathan Cameron return len; 997a980e046SJonathan Cameron } 998a980e046SJonathan Cameron 999a980e046SJonathan Cameron static 1000a980e046SJonathan Cameron int __iio_device_attr_init(struct device_attribute *dev_attr, 1001a980e046SJonathan Cameron const char *postfix, 1002a980e046SJonathan Cameron struct iio_chan_spec const *chan, 1003a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 1004a980e046SJonathan Cameron struct device_attribute *attr, 1005a980e046SJonathan Cameron char *buf), 1006a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 1007a980e046SJonathan Cameron struct device_attribute *attr, 1008a980e046SJonathan Cameron const char *buf, 1009a980e046SJonathan Cameron size_t len), 10103704432fSJonathan Cameron enum iio_shared_by shared_by) 1011a980e046SJonathan Cameron { 10123704432fSJonathan Cameron int ret = 0; 10137bbcf7e1SLars-Peter Clausen char *name = NULL; 10143704432fSJonathan Cameron char *full_postfix; 1015a980e046SJonathan Cameron sysfs_attr_init(&dev_attr->attr); 1016a980e046SJonathan Cameron 1017a980e046SJonathan Cameron /* Build up postfix of <extend_name>_<modifier>_postfix */ 10183704432fSJonathan Cameron if (chan->modified && (shared_by == IIO_SEPARATE)) { 1019a980e046SJonathan Cameron if (chan->extend_name) 1020a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", 1021a980e046SJonathan Cameron iio_modifier_names[chan 1022a980e046SJonathan Cameron ->channel2], 1023a980e046SJonathan Cameron chan->extend_name, 1024a980e046SJonathan Cameron postfix); 1025a980e046SJonathan Cameron else 1026a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s", 1027a980e046SJonathan Cameron iio_modifier_names[chan 1028a980e046SJonathan Cameron ->channel2], 1029a980e046SJonathan Cameron postfix); 1030a980e046SJonathan Cameron } else { 103177bfa8baSLars-Peter Clausen if (chan->extend_name == NULL || shared_by != IIO_SEPARATE) 1032a980e046SJonathan Cameron full_postfix = kstrdup(postfix, GFP_KERNEL); 1033a980e046SJonathan Cameron else 1034a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, 1035a980e046SJonathan Cameron "%s_%s", 1036a980e046SJonathan Cameron chan->extend_name, 1037a980e046SJonathan Cameron postfix); 1038a980e046SJonathan Cameron } 10393704432fSJonathan Cameron if (full_postfix == NULL) 10403704432fSJonathan Cameron return -ENOMEM; 1041a980e046SJonathan Cameron 1042a980e046SJonathan Cameron if (chan->differential) { /* Differential can not have modifier */ 10433704432fSJonathan Cameron switch (shared_by) { 1044c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 10457bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 1046c006ec83SJonathan Cameron break; 1047c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 10487bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 1049c006ec83SJonathan Cameron iio_direction[chan->output], 1050c006ec83SJonathan Cameron full_postfix); 1051c006ec83SJonathan Cameron break; 10523704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 10537bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s", 1054a980e046SJonathan Cameron iio_direction[chan->output], 1055a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1056a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1057a980e046SJonathan Cameron full_postfix); 10583704432fSJonathan Cameron break; 10593704432fSJonathan Cameron case IIO_SEPARATE: 10603704432fSJonathan Cameron if (!chan->indexed) { 1061231bfe53SDan Carpenter WARN(1, "Differential channels must be indexed\n"); 10623704432fSJonathan Cameron ret = -EINVAL; 10633704432fSJonathan Cameron goto error_free_full_postfix; 10643704432fSJonathan Cameron } 10657bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, 10663704432fSJonathan Cameron "%s_%s%d-%s%d_%s", 1067a980e046SJonathan Cameron iio_direction[chan->output], 1068a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1069a980e046SJonathan Cameron chan->channel, 1070a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1071a980e046SJonathan Cameron chan->channel2, 1072a980e046SJonathan Cameron full_postfix); 10733704432fSJonathan Cameron break; 1074a980e046SJonathan Cameron } 1075a980e046SJonathan Cameron } else { /* Single ended */ 10763704432fSJonathan Cameron switch (shared_by) { 1077c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 10787bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 1079c006ec83SJonathan Cameron break; 1080c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 10817bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 1082c006ec83SJonathan Cameron iio_direction[chan->output], 1083c006ec83SJonathan Cameron full_postfix); 1084c006ec83SJonathan Cameron break; 10853704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 10867bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 1087a980e046SJonathan Cameron iio_direction[chan->output], 1088a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1089a980e046SJonathan Cameron full_postfix); 10903704432fSJonathan Cameron break; 10913704432fSJonathan Cameron 10923704432fSJonathan Cameron case IIO_SEPARATE: 10933704432fSJonathan Cameron if (chan->indexed) 10947bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s%d_%s", 1095a980e046SJonathan Cameron iio_direction[chan->output], 1096a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1097a980e046SJonathan Cameron chan->channel, 1098a980e046SJonathan Cameron full_postfix); 1099a980e046SJonathan Cameron else 11007bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 1101a980e046SJonathan Cameron iio_direction[chan->output], 1102a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1103a980e046SJonathan Cameron full_postfix); 11043704432fSJonathan Cameron break; 11053704432fSJonathan Cameron } 1106a980e046SJonathan Cameron } 11077bbcf7e1SLars-Peter Clausen if (name == NULL) { 1108a980e046SJonathan Cameron ret = -ENOMEM; 1109a980e046SJonathan Cameron goto error_free_full_postfix; 1110a980e046SJonathan Cameron } 11117bbcf7e1SLars-Peter Clausen dev_attr->attr.name = name; 1112a980e046SJonathan Cameron 1113a980e046SJonathan Cameron if (readfunc) { 1114a980e046SJonathan Cameron dev_attr->attr.mode |= S_IRUGO; 1115a980e046SJonathan Cameron dev_attr->show = readfunc; 1116a980e046SJonathan Cameron } 1117a980e046SJonathan Cameron 1118a980e046SJonathan Cameron if (writefunc) { 1119a980e046SJonathan Cameron dev_attr->attr.mode |= S_IWUSR; 1120a980e046SJonathan Cameron dev_attr->store = writefunc; 1121a980e046SJonathan Cameron } 11227bbcf7e1SLars-Peter Clausen 1123a980e046SJonathan Cameron error_free_full_postfix: 1124a980e046SJonathan Cameron kfree(full_postfix); 11253704432fSJonathan Cameron 1126a980e046SJonathan Cameron return ret; 1127a980e046SJonathan Cameron } 1128a980e046SJonathan Cameron 1129a980e046SJonathan Cameron static void __iio_device_attr_deinit(struct device_attribute *dev_attr) 1130a980e046SJonathan Cameron { 1131a980e046SJonathan Cameron kfree(dev_attr->attr.name); 1132a980e046SJonathan Cameron } 1133a980e046SJonathan Cameron 1134a980e046SJonathan Cameron int __iio_add_chan_devattr(const char *postfix, 1135a980e046SJonathan Cameron struct iio_chan_spec const *chan, 1136a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 1137a980e046SJonathan Cameron struct device_attribute *attr, 1138a980e046SJonathan Cameron char *buf), 1139a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 1140a980e046SJonathan Cameron struct device_attribute *attr, 1141a980e046SJonathan Cameron const char *buf, 1142a980e046SJonathan Cameron size_t len), 1143a980e046SJonathan Cameron u64 mask, 11443704432fSJonathan Cameron enum iio_shared_by shared_by, 1145a980e046SJonathan Cameron struct device *dev, 11463e3d11b2SAlexandru Ardelean struct iio_buffer *buffer, 1147a980e046SJonathan Cameron struct list_head *attr_list) 1148a980e046SJonathan Cameron { 1149a980e046SJonathan Cameron int ret; 1150a980e046SJonathan Cameron struct iio_dev_attr *iio_attr, *t; 1151a980e046SJonathan Cameron 1152670c1103SSachin Kamat iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL); 115392825ff9SHartmut Knaack if (iio_attr == NULL) 115492825ff9SHartmut Knaack return -ENOMEM; 1155a980e046SJonathan Cameron ret = __iio_device_attr_init(&iio_attr->dev_attr, 1156a980e046SJonathan Cameron postfix, chan, 11573704432fSJonathan Cameron readfunc, writefunc, shared_by); 1158a980e046SJonathan Cameron if (ret) 1159a980e046SJonathan Cameron goto error_iio_dev_attr_free; 1160a980e046SJonathan Cameron iio_attr->c = chan; 1161a980e046SJonathan Cameron iio_attr->address = mask; 11623e3d11b2SAlexandru Ardelean iio_attr->buffer = buffer; 1163a980e046SJonathan Cameron list_for_each_entry(t, attr_list, l) 1164a980e046SJonathan Cameron if (strcmp(t->dev_attr.attr.name, 1165a980e046SJonathan Cameron iio_attr->dev_attr.attr.name) == 0) { 11663704432fSJonathan Cameron if (shared_by == IIO_SEPARATE) 1167a980e046SJonathan Cameron dev_err(dev, "tried to double register : %s\n", 1168a980e046SJonathan Cameron t->dev_attr.attr.name); 1169a980e046SJonathan Cameron ret = -EBUSY; 1170a980e046SJonathan Cameron goto error_device_attr_deinit; 1171a980e046SJonathan Cameron } 1172a980e046SJonathan Cameron list_add(&iio_attr->l, attr_list); 1173a980e046SJonathan Cameron 1174a980e046SJonathan Cameron return 0; 1175a980e046SJonathan Cameron 1176a980e046SJonathan Cameron error_device_attr_deinit: 1177a980e046SJonathan Cameron __iio_device_attr_deinit(&iio_attr->dev_attr); 1178a980e046SJonathan Cameron error_iio_dev_attr_free: 1179a980e046SJonathan Cameron kfree(iio_attr); 1180a980e046SJonathan Cameron return ret; 1181a980e046SJonathan Cameron } 1182a980e046SJonathan Cameron 11831d4ef9b3SCristian Pop static int iio_device_add_channel_label(struct iio_dev *indio_dev, 11841d4ef9b3SCristian Pop struct iio_chan_spec const *chan) 11851d4ef9b3SCristian Pop { 11861d4ef9b3SCristian Pop struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 11871d4ef9b3SCristian Pop int ret; 11881d4ef9b3SCristian Pop 118913efdc3dSPaul Cercueil if (!indio_dev->info->read_label && !chan->extend_name) 11901d4ef9b3SCristian Pop return 0; 11911d4ef9b3SCristian Pop 11921d4ef9b3SCristian Pop ret = __iio_add_chan_devattr("label", 11931d4ef9b3SCristian Pop chan, 11941d4ef9b3SCristian Pop &iio_read_channel_label, 11951d4ef9b3SCristian Pop NULL, 11961d4ef9b3SCristian Pop 0, 11971d4ef9b3SCristian Pop IIO_SEPARATE, 11981d4ef9b3SCristian Pop &indio_dev->dev, 11993e3d11b2SAlexandru Ardelean NULL, 12001d4ef9b3SCristian Pop &iio_dev_opaque->channel_attr_list); 12011d4ef9b3SCristian Pop if (ret < 0) 12021d4ef9b3SCristian Pop return ret; 12031d4ef9b3SCristian Pop 12041d4ef9b3SCristian Pop return 1; 12051d4ef9b3SCristian Pop } 12061d4ef9b3SCristian Pop 12073704432fSJonathan Cameron static int iio_device_add_info_mask_type(struct iio_dev *indio_dev, 12083704432fSJonathan Cameron struct iio_chan_spec const *chan, 12093704432fSJonathan Cameron enum iio_shared_by shared_by, 12103704432fSJonathan Cameron const long *infomask) 12113704432fSJonathan Cameron { 1212207c2d27SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 12133704432fSJonathan Cameron int i, ret, attrcount = 0; 12143704432fSJonathan Cameron 12151016d567SOrson Zhai for_each_set_bit(i, infomask, sizeof(*infomask)*8) { 1216ef4b4856SJonathan Cameron if (i >= ARRAY_SIZE(iio_chan_info_postfix)) 1217ef4b4856SJonathan Cameron return -EINVAL; 12183704432fSJonathan Cameron ret = __iio_add_chan_devattr(iio_chan_info_postfix[i], 12193704432fSJonathan Cameron chan, 12203704432fSJonathan Cameron &iio_read_channel_info, 12213704432fSJonathan Cameron &iio_write_channel_info, 12223704432fSJonathan Cameron i, 12233704432fSJonathan Cameron shared_by, 12243704432fSJonathan Cameron &indio_dev->dev, 12253e3d11b2SAlexandru Ardelean NULL, 1226207c2d27SAlexandru Ardelean &iio_dev_opaque->channel_attr_list); 12273704432fSJonathan Cameron if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) 12283704432fSJonathan Cameron continue; 12293704432fSJonathan Cameron else if (ret < 0) 12303704432fSJonathan Cameron return ret; 12313704432fSJonathan Cameron attrcount++; 12323704432fSJonathan Cameron } 12333704432fSJonathan Cameron 12343704432fSJonathan Cameron return attrcount; 12353704432fSJonathan Cameron } 12363704432fSJonathan Cameron 123751239600SJonathan Cameron static int iio_device_add_info_mask_type_avail(struct iio_dev *indio_dev, 123851239600SJonathan Cameron struct iio_chan_spec const *chan, 123951239600SJonathan Cameron enum iio_shared_by shared_by, 124051239600SJonathan Cameron const long *infomask) 124151239600SJonathan Cameron { 1242207c2d27SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 124351239600SJonathan Cameron int i, ret, attrcount = 0; 124451239600SJonathan Cameron char *avail_postfix; 124551239600SJonathan Cameron 12461016d567SOrson Zhai for_each_set_bit(i, infomask, sizeof(*infomask) * 8) { 1247936d3e53SYoung Xiao if (i >= ARRAY_SIZE(iio_chan_info_postfix)) 1248936d3e53SYoung Xiao return -EINVAL; 124951239600SJonathan Cameron avail_postfix = kasprintf(GFP_KERNEL, 125051239600SJonathan Cameron "%s_available", 125151239600SJonathan Cameron iio_chan_info_postfix[i]); 125251239600SJonathan Cameron if (!avail_postfix) 125351239600SJonathan Cameron return -ENOMEM; 125451239600SJonathan Cameron 125551239600SJonathan Cameron ret = __iio_add_chan_devattr(avail_postfix, 125651239600SJonathan Cameron chan, 125751239600SJonathan Cameron &iio_read_channel_info_avail, 125851239600SJonathan Cameron NULL, 125951239600SJonathan Cameron i, 126051239600SJonathan Cameron shared_by, 126151239600SJonathan Cameron &indio_dev->dev, 12623e3d11b2SAlexandru Ardelean NULL, 1263207c2d27SAlexandru Ardelean &iio_dev_opaque->channel_attr_list); 126451239600SJonathan Cameron kfree(avail_postfix); 126551239600SJonathan Cameron if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) 126651239600SJonathan Cameron continue; 126751239600SJonathan Cameron else if (ret < 0) 126851239600SJonathan Cameron return ret; 126951239600SJonathan Cameron attrcount++; 127051239600SJonathan Cameron } 127151239600SJonathan Cameron 127251239600SJonathan Cameron return attrcount; 127351239600SJonathan Cameron } 127451239600SJonathan Cameron 1275a980e046SJonathan Cameron static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev, 1276a980e046SJonathan Cameron struct iio_chan_spec const *chan) 1277a980e046SJonathan Cameron { 1278207c2d27SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1279a980e046SJonathan Cameron int ret, attrcount = 0; 1280a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 1281a980e046SJonathan Cameron 1282a980e046SJonathan Cameron if (chan->channel < 0) 1283a980e046SJonathan Cameron return 0; 12843704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 12853704432fSJonathan Cameron IIO_SEPARATE, 12863704432fSJonathan Cameron &chan->info_mask_separate); 12878655cc49SJonathan Cameron if (ret < 0) 12883704432fSJonathan Cameron return ret; 12893704432fSJonathan Cameron attrcount += ret; 12903704432fSJonathan Cameron 129151239600SJonathan Cameron ret = iio_device_add_info_mask_type_avail(indio_dev, chan, 129251239600SJonathan Cameron IIO_SEPARATE, 129351239600SJonathan Cameron &chan-> 129451239600SJonathan Cameron info_mask_separate_available); 129551239600SJonathan Cameron if (ret < 0) 129651239600SJonathan Cameron return ret; 129751239600SJonathan Cameron attrcount += ret; 129851239600SJonathan Cameron 12993704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 13003704432fSJonathan Cameron IIO_SHARED_BY_TYPE, 13013704432fSJonathan Cameron &chan->info_mask_shared_by_type); 13023704432fSJonathan Cameron if (ret < 0) 13033704432fSJonathan Cameron return ret; 13043704432fSJonathan Cameron attrcount += ret; 1305a980e046SJonathan Cameron 130651239600SJonathan Cameron ret = iio_device_add_info_mask_type_avail(indio_dev, chan, 130751239600SJonathan Cameron IIO_SHARED_BY_TYPE, 130851239600SJonathan Cameron &chan-> 130951239600SJonathan Cameron info_mask_shared_by_type_available); 131051239600SJonathan Cameron if (ret < 0) 131151239600SJonathan Cameron return ret; 131251239600SJonathan Cameron attrcount += ret; 131351239600SJonathan Cameron 1314c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 1315c006ec83SJonathan Cameron IIO_SHARED_BY_DIR, 1316c006ec83SJonathan Cameron &chan->info_mask_shared_by_dir); 1317c006ec83SJonathan Cameron if (ret < 0) 1318c006ec83SJonathan Cameron return ret; 1319c006ec83SJonathan Cameron attrcount += ret; 1320c006ec83SJonathan Cameron 132151239600SJonathan Cameron ret = iio_device_add_info_mask_type_avail(indio_dev, chan, 132251239600SJonathan Cameron IIO_SHARED_BY_DIR, 132351239600SJonathan Cameron &chan->info_mask_shared_by_dir_available); 132451239600SJonathan Cameron if (ret < 0) 132551239600SJonathan Cameron return ret; 132651239600SJonathan Cameron attrcount += ret; 132751239600SJonathan Cameron 1328c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 1329c006ec83SJonathan Cameron IIO_SHARED_BY_ALL, 1330c006ec83SJonathan Cameron &chan->info_mask_shared_by_all); 1331c006ec83SJonathan Cameron if (ret < 0) 1332c006ec83SJonathan Cameron return ret; 1333c006ec83SJonathan Cameron attrcount += ret; 1334c006ec83SJonathan Cameron 133551239600SJonathan Cameron ret = iio_device_add_info_mask_type_avail(indio_dev, chan, 133651239600SJonathan Cameron IIO_SHARED_BY_ALL, 133751239600SJonathan Cameron &chan->info_mask_shared_by_all_available); 133851239600SJonathan Cameron if (ret < 0) 133951239600SJonathan Cameron return ret; 134051239600SJonathan Cameron attrcount += ret; 134151239600SJonathan Cameron 13421d4ef9b3SCristian Pop ret = iio_device_add_channel_label(indio_dev, chan); 13431d4ef9b3SCristian Pop if (ret < 0) 13441d4ef9b3SCristian Pop return ret; 13451d4ef9b3SCristian Pop attrcount += ret; 13461d4ef9b3SCristian Pop 1347a980e046SJonathan Cameron if (chan->ext_info) { 1348a980e046SJonathan Cameron unsigned int i = 0; 1349a980e046SJonathan Cameron for (ext_info = chan->ext_info; ext_info->name; ext_info++) { 1350a980e046SJonathan Cameron ret = __iio_add_chan_devattr(ext_info->name, 1351a980e046SJonathan Cameron chan, 1352a980e046SJonathan Cameron ext_info->read ? 1353a980e046SJonathan Cameron &iio_read_channel_ext_info : NULL, 1354a980e046SJonathan Cameron ext_info->write ? 1355a980e046SJonathan Cameron &iio_write_channel_ext_info : NULL, 1356a980e046SJonathan Cameron i, 1357a980e046SJonathan Cameron ext_info->shared, 1358a980e046SJonathan Cameron &indio_dev->dev, 13593e3d11b2SAlexandru Ardelean NULL, 1360207c2d27SAlexandru Ardelean &iio_dev_opaque->channel_attr_list); 1361a980e046SJonathan Cameron i++; 1362a980e046SJonathan Cameron if (ret == -EBUSY && ext_info->shared) 1363a980e046SJonathan Cameron continue; 1364a980e046SJonathan Cameron 1365a980e046SJonathan Cameron if (ret) 13663704432fSJonathan Cameron return ret; 1367a980e046SJonathan Cameron 1368a980e046SJonathan Cameron attrcount++; 1369a980e046SJonathan Cameron } 1370a980e046SJonathan Cameron } 1371a980e046SJonathan Cameron 13723704432fSJonathan Cameron return attrcount; 1373a980e046SJonathan Cameron } 1374a980e046SJonathan Cameron 137584088ebdSLars-Peter Clausen /** 137684088ebdSLars-Peter Clausen * iio_free_chan_devattr_list() - Free a list of IIO device attributes 137784088ebdSLars-Peter Clausen * @attr_list: List of IIO device attributes 137884088ebdSLars-Peter Clausen * 137984088ebdSLars-Peter Clausen * This function frees the memory allocated for each of the IIO device 1380c1b03ab5SMartin Fuzzey * attributes in the list. 138184088ebdSLars-Peter Clausen */ 138284088ebdSLars-Peter Clausen void iio_free_chan_devattr_list(struct list_head *attr_list) 1383a980e046SJonathan Cameron { 138484088ebdSLars-Peter Clausen struct iio_dev_attr *p, *n; 138584088ebdSLars-Peter Clausen 138684088ebdSLars-Peter Clausen list_for_each_entry_safe(p, n, attr_list, l) { 138715097c7aSAlexandru Ardelean kfree_const(p->dev_attr.attr.name); 1388c1b03ab5SMartin Fuzzey list_del(&p->l); 1389a980e046SJonathan Cameron kfree(p); 1390a980e046SJonathan Cameron } 139184088ebdSLars-Peter Clausen } 1392a980e046SJonathan Cameron 1393a980e046SJonathan Cameron static ssize_t iio_show_dev_name(struct device *dev, 1394a980e046SJonathan Cameron struct device_attribute *attr, 1395a980e046SJonathan Cameron char *buf) 1396a980e046SJonathan Cameron { 1397e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 139883ca56b6SLars-Peter Clausen return sysfs_emit(buf, "%s\n", indio_dev->name); 1399a980e046SJonathan Cameron } 1400a980e046SJonathan Cameron 1401a980e046SJonathan Cameron static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL); 1402a980e046SJonathan Cameron 14032c3d0c9fSPhil Reid static ssize_t iio_show_dev_label(struct device *dev, 14042c3d0c9fSPhil Reid struct device_attribute *attr, 14052c3d0c9fSPhil Reid char *buf) 14062c3d0c9fSPhil Reid { 14072c3d0c9fSPhil Reid struct iio_dev *indio_dev = dev_to_iio_dev(dev); 140883ca56b6SLars-Peter Clausen return sysfs_emit(buf, "%s\n", indio_dev->label); 14092c3d0c9fSPhil Reid } 14102c3d0c9fSPhil Reid 14112c3d0c9fSPhil Reid static DEVICE_ATTR(label, S_IRUGO, iio_show_dev_label, NULL); 14122c3d0c9fSPhil Reid 1413bc2b7dabSGregor Boirie static ssize_t iio_show_timestamp_clock(struct device *dev, 1414bc2b7dabSGregor Boirie struct device_attribute *attr, 1415bc2b7dabSGregor Boirie char *buf) 1416bc2b7dabSGregor Boirie { 1417bc2b7dabSGregor Boirie const struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1418bc2b7dabSGregor Boirie const clockid_t clk = iio_device_get_clock(indio_dev); 1419bc2b7dabSGregor Boirie const char *name; 1420bc2b7dabSGregor Boirie ssize_t sz; 1421bc2b7dabSGregor Boirie 1422bc2b7dabSGregor Boirie switch (clk) { 1423bc2b7dabSGregor Boirie case CLOCK_REALTIME: 1424bc2b7dabSGregor Boirie name = "realtime\n"; 1425bc2b7dabSGregor Boirie sz = sizeof("realtime\n"); 1426bc2b7dabSGregor Boirie break; 1427bc2b7dabSGregor Boirie case CLOCK_MONOTONIC: 1428bc2b7dabSGregor Boirie name = "monotonic\n"; 1429bc2b7dabSGregor Boirie sz = sizeof("monotonic\n"); 1430bc2b7dabSGregor Boirie break; 1431bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_RAW: 1432bc2b7dabSGregor Boirie name = "monotonic_raw\n"; 1433bc2b7dabSGregor Boirie sz = sizeof("monotonic_raw\n"); 1434bc2b7dabSGregor Boirie break; 1435bc2b7dabSGregor Boirie case CLOCK_REALTIME_COARSE: 1436bc2b7dabSGregor Boirie name = "realtime_coarse\n"; 1437bc2b7dabSGregor Boirie sz = sizeof("realtime_coarse\n"); 1438bc2b7dabSGregor Boirie break; 1439bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_COARSE: 1440bc2b7dabSGregor Boirie name = "monotonic_coarse\n"; 1441bc2b7dabSGregor Boirie sz = sizeof("monotonic_coarse\n"); 1442bc2b7dabSGregor Boirie break; 1443bc2b7dabSGregor Boirie case CLOCK_BOOTTIME: 1444bc2b7dabSGregor Boirie name = "boottime\n"; 1445bc2b7dabSGregor Boirie sz = sizeof("boottime\n"); 1446bc2b7dabSGregor Boirie break; 1447bc2b7dabSGregor Boirie case CLOCK_TAI: 1448bc2b7dabSGregor Boirie name = "tai\n"; 1449bc2b7dabSGregor Boirie sz = sizeof("tai\n"); 1450bc2b7dabSGregor Boirie break; 1451bc2b7dabSGregor Boirie default: 1452bc2b7dabSGregor Boirie BUG(); 1453bc2b7dabSGregor Boirie } 1454bc2b7dabSGregor Boirie 1455bc2b7dabSGregor Boirie memcpy(buf, name, sz); 1456bc2b7dabSGregor Boirie return sz; 1457bc2b7dabSGregor Boirie } 1458bc2b7dabSGregor Boirie 1459bc2b7dabSGregor Boirie static ssize_t iio_store_timestamp_clock(struct device *dev, 1460bc2b7dabSGregor Boirie struct device_attribute *attr, 1461bc2b7dabSGregor Boirie const char *buf, size_t len) 1462bc2b7dabSGregor Boirie { 1463bc2b7dabSGregor Boirie clockid_t clk; 1464bc2b7dabSGregor Boirie int ret; 1465bc2b7dabSGregor Boirie 1466bc2b7dabSGregor Boirie if (sysfs_streq(buf, "realtime")) 1467bc2b7dabSGregor Boirie clk = CLOCK_REALTIME; 1468bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "monotonic")) 1469bc2b7dabSGregor Boirie clk = CLOCK_MONOTONIC; 1470bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "monotonic_raw")) 1471bc2b7dabSGregor Boirie clk = CLOCK_MONOTONIC_RAW; 1472bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "realtime_coarse")) 1473bc2b7dabSGregor Boirie clk = CLOCK_REALTIME_COARSE; 1474bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "monotonic_coarse")) 1475bc2b7dabSGregor Boirie clk = CLOCK_MONOTONIC_COARSE; 1476bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "boottime")) 1477bc2b7dabSGregor Boirie clk = CLOCK_BOOTTIME; 1478bc2b7dabSGregor Boirie else if (sysfs_streq(buf, "tai")) 1479bc2b7dabSGregor Boirie clk = CLOCK_TAI; 1480bc2b7dabSGregor Boirie else 1481bc2b7dabSGregor Boirie return -EINVAL; 1482bc2b7dabSGregor Boirie 1483bc2b7dabSGregor Boirie ret = iio_device_set_clock(dev_to_iio_dev(dev), clk); 1484bc2b7dabSGregor Boirie if (ret) 1485bc2b7dabSGregor Boirie return ret; 1486bc2b7dabSGregor Boirie 1487bc2b7dabSGregor Boirie return len; 1488bc2b7dabSGregor Boirie } 1489bc2b7dabSGregor Boirie 149032f17172SAlexandru Ardelean int iio_device_register_sysfs_group(struct iio_dev *indio_dev, 149132f17172SAlexandru Ardelean const struct attribute_group *group) 149232f17172SAlexandru Ardelean { 149332f17172SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 149432f17172SAlexandru Ardelean const struct attribute_group **new, **old = iio_dev_opaque->groups; 149532f17172SAlexandru Ardelean unsigned int cnt = iio_dev_opaque->groupcounter; 149632f17172SAlexandru Ardelean 149732f17172SAlexandru Ardelean new = krealloc(old, sizeof(*new) * (cnt + 2), GFP_KERNEL); 149832f17172SAlexandru Ardelean if (!new) 149932f17172SAlexandru Ardelean return -ENOMEM; 150032f17172SAlexandru Ardelean 150132f17172SAlexandru Ardelean new[iio_dev_opaque->groupcounter++] = group; 150232f17172SAlexandru Ardelean new[iio_dev_opaque->groupcounter] = NULL; 150332f17172SAlexandru Ardelean 150432f17172SAlexandru Ardelean iio_dev_opaque->groups = new; 150532f17172SAlexandru Ardelean 150632f17172SAlexandru Ardelean return 0; 150732f17172SAlexandru Ardelean } 150832f17172SAlexandru Ardelean 1509bc2b7dabSGregor Boirie static DEVICE_ATTR(current_timestamp_clock, S_IRUGO | S_IWUSR, 1510bc2b7dabSGregor Boirie iio_show_timestamp_clock, iio_store_timestamp_clock); 1511bc2b7dabSGregor Boirie 1512a980e046SJonathan Cameron static int iio_device_register_sysfs(struct iio_dev *indio_dev) 1513a980e046SJonathan Cameron { 1514207c2d27SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1515a980e046SJonathan Cameron int i, ret = 0, attrcount, attrn, attrcount_orig = 0; 151684088ebdSLars-Peter Clausen struct iio_dev_attr *p; 1517bc2b7dabSGregor Boirie struct attribute **attr, *clk = NULL; 1518a980e046SJonathan Cameron 1519a980e046SJonathan Cameron /* First count elements in any existing group */ 1520a980e046SJonathan Cameron if (indio_dev->info->attrs) { 1521a980e046SJonathan Cameron attr = indio_dev->info->attrs->attrs; 1522a980e046SJonathan Cameron while (*attr++ != NULL) 1523a980e046SJonathan Cameron attrcount_orig++; 1524a980e046SJonathan Cameron } 1525a980e046SJonathan Cameron attrcount = attrcount_orig; 1526a980e046SJonathan Cameron /* 1527a980e046SJonathan Cameron * New channel registration method - relies on the fact a group does 1528d25b3808SPeter Meerwald * not need to be initialized if its name is NULL. 1529a980e046SJonathan Cameron */ 1530a980e046SJonathan Cameron if (indio_dev->channels) 1531a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) { 1532bc2b7dabSGregor Boirie const struct iio_chan_spec *chan = 1533bc2b7dabSGregor Boirie &indio_dev->channels[i]; 1534bc2b7dabSGregor Boirie 1535bc2b7dabSGregor Boirie if (chan->type == IIO_TIMESTAMP) 1536bc2b7dabSGregor Boirie clk = &dev_attr_current_timestamp_clock.attr; 1537bc2b7dabSGregor Boirie 1538bc2b7dabSGregor Boirie ret = iio_device_add_channel_sysfs(indio_dev, chan); 1539a980e046SJonathan Cameron if (ret < 0) 1540a980e046SJonathan Cameron goto error_clear_attrs; 1541a980e046SJonathan Cameron attrcount += ret; 1542a980e046SJonathan Cameron } 1543a980e046SJonathan Cameron 1544fa83c3baSAlexandru Ardelean if (iio_dev_opaque->event_interface) 1545bc2b7dabSGregor Boirie clk = &dev_attr_current_timestamp_clock.attr; 1546bc2b7dabSGregor Boirie 1547a980e046SJonathan Cameron if (indio_dev->name) 1548a980e046SJonathan Cameron attrcount++; 15492c3d0c9fSPhil Reid if (indio_dev->label) 15502c3d0c9fSPhil Reid attrcount++; 1551bc2b7dabSGregor Boirie if (clk) 1552bc2b7dabSGregor Boirie attrcount++; 1553a980e046SJonathan Cameron 1554207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs = 1555207c2d27SAlexandru Ardelean kcalloc(attrcount + 1, 1556207c2d27SAlexandru Ardelean sizeof(iio_dev_opaque->chan_attr_group.attrs[0]), 1557a980e046SJonathan Cameron GFP_KERNEL); 1558207c2d27SAlexandru Ardelean if (iio_dev_opaque->chan_attr_group.attrs == NULL) { 1559a980e046SJonathan Cameron ret = -ENOMEM; 1560a980e046SJonathan Cameron goto error_clear_attrs; 1561a980e046SJonathan Cameron } 1562a980e046SJonathan Cameron /* Copy across original attributes */ 156318b4c9cdSHans de Goede if (indio_dev->info->attrs) { 1564207c2d27SAlexandru Ardelean memcpy(iio_dev_opaque->chan_attr_group.attrs, 1565a980e046SJonathan Cameron indio_dev->info->attrs->attrs, 1566207c2d27SAlexandru Ardelean sizeof(iio_dev_opaque->chan_attr_group.attrs[0]) 1567a980e046SJonathan Cameron *attrcount_orig); 156818b4c9cdSHans de Goede iio_dev_opaque->chan_attr_group.is_visible = 156918b4c9cdSHans de Goede indio_dev->info->attrs->is_visible; 157018b4c9cdSHans de Goede } 1571a980e046SJonathan Cameron attrn = attrcount_orig; 1572a980e046SJonathan Cameron /* Add all elements from the list. */ 1573207c2d27SAlexandru Ardelean list_for_each_entry(p, &iio_dev_opaque->channel_attr_list, l) 1574207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr; 1575a980e046SJonathan Cameron if (indio_dev->name) 1576207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr; 15772c3d0c9fSPhil Reid if (indio_dev->label) 1578207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_label.attr; 1579bc2b7dabSGregor Boirie if (clk) 1580207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs[attrn++] = clk; 1581a980e046SJonathan Cameron 158232f17172SAlexandru Ardelean ret = iio_device_register_sysfs_group(indio_dev, 158332f17172SAlexandru Ardelean &iio_dev_opaque->chan_attr_group); 158432f17172SAlexandru Ardelean if (ret) 158532f17172SAlexandru Ardelean goto error_clear_attrs; 1586a980e046SJonathan Cameron 1587a980e046SJonathan Cameron return 0; 1588a980e046SJonathan Cameron 1589a980e046SJonathan Cameron error_clear_attrs: 1590207c2d27SAlexandru Ardelean iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list); 1591a980e046SJonathan Cameron 1592a980e046SJonathan Cameron return ret; 1593a980e046SJonathan Cameron } 1594a980e046SJonathan Cameron 1595a980e046SJonathan Cameron static void iio_device_unregister_sysfs(struct iio_dev *indio_dev) 1596a980e046SJonathan Cameron { 1597207c2d27SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1598a980e046SJonathan Cameron 1599207c2d27SAlexandru Ardelean iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list); 1600207c2d27SAlexandru Ardelean kfree(iio_dev_opaque->chan_attr_group.attrs); 1601207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs = NULL; 160232f17172SAlexandru Ardelean kfree(iio_dev_opaque->groups); 1603a980e046SJonathan Cameron } 1604a980e046SJonathan Cameron 1605a980e046SJonathan Cameron static void iio_dev_release(struct device *device) 1606a980e046SJonathan Cameron { 1607e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(device); 16086d4ebd56SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 16096d4ebd56SAlexandru Ardelean 1610d89e119aSBenjamin Gaignard if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES) 1611a980e046SJonathan Cameron iio_device_unregister_trigger_consumer(indio_dev); 1612a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 1613a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 1614e407fd65SLars-Peter Clausen 1615218bc53dSAlexandru Ardelean iio_device_detach_buffers(indio_dev); 16169e69c935SLars-Peter Clausen 161715ea2878SJonathan Cameron ida_simple_remove(&iio_ida, iio_dev_opaque->id); 16186d4ebd56SAlexandru Ardelean kfree(iio_dev_opaque); 1619a980e046SJonathan Cameron } 1620a980e046SJonathan Cameron 162117d82b47SGuenter Roeck struct device_type iio_device_type = { 1622a980e046SJonathan Cameron .name = "iio_device", 1623a980e046SJonathan Cameron .release = iio_dev_release, 1624a980e046SJonathan Cameron }; 1625a980e046SJonathan Cameron 1626a7e57dceSSachin Kamat /** 1627a7e57dceSSachin Kamat * iio_device_alloc() - allocate an iio_dev from a driver 1628c9561fd2SJonathan Cameron * @parent: Parent device. 1629a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 1630a7e57dceSSachin Kamat **/ 163178289b4aSAlexandru Ardelean struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv) 1632a980e046SJonathan Cameron { 16336d4ebd56SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque; 16340d596bb2SAlexandru Ardelean struct iio_dev *indio_dev; 1635a980e046SJonathan Cameron size_t alloc_size; 1636a980e046SJonathan Cameron 16376d4ebd56SAlexandru Ardelean alloc_size = sizeof(struct iio_dev_opaque); 1638a980e046SJonathan Cameron if (sizeof_priv) { 1639a980e046SJonathan Cameron alloc_size = ALIGN(alloc_size, IIO_ALIGN); 1640a980e046SJonathan Cameron alloc_size += sizeof_priv; 1641a980e046SJonathan Cameron } 1642a980e046SJonathan Cameron 16436d4ebd56SAlexandru Ardelean iio_dev_opaque = kzalloc(alloc_size, GFP_KERNEL); 16446d4ebd56SAlexandru Ardelean if (!iio_dev_opaque) 16450d4b2184SJonathan Cameron return NULL; 1646a980e046SJonathan Cameron 16470d596bb2SAlexandru Ardelean indio_dev = &iio_dev_opaque->indio_dev; 16480d596bb2SAlexandru Ardelean indio_dev->priv = (char *)iio_dev_opaque + 16496d4ebd56SAlexandru Ardelean ALIGN(sizeof(struct iio_dev_opaque), IIO_ALIGN); 16506d4ebd56SAlexandru Ardelean 16510d596bb2SAlexandru Ardelean indio_dev->dev.parent = parent; 16520d596bb2SAlexandru Ardelean indio_dev->dev.type = &iio_device_type; 16530d596bb2SAlexandru Ardelean indio_dev->dev.bus = &iio_bus_type; 16540d596bb2SAlexandru Ardelean device_initialize(&indio_dev->dev); 1655b624fd14SJulia Lawall iio_device_set_drvdata(indio_dev, (void *)indio_dev); 16560d596bb2SAlexandru Ardelean mutex_init(&indio_dev->mlock); 1657b804e2b7SJonathan Cameron mutex_init(&iio_dev_opaque->info_exist_lock); 1658207c2d27SAlexandru Ardelean INIT_LIST_HEAD(&iio_dev_opaque->channel_attr_list); 1659a980e046SJonathan Cameron 166015ea2878SJonathan Cameron iio_dev_opaque->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL); 166115ea2878SJonathan Cameron if (iio_dev_opaque->id < 0) { 1662a980e046SJonathan Cameron /* cannot use a dev_err as the name isn't available */ 16633176dd5dSSachin Kamat pr_err("failed to get device id\n"); 16646d4ebd56SAlexandru Ardelean kfree(iio_dev_opaque); 1665a980e046SJonathan Cameron return NULL; 1666a980e046SJonathan Cameron } 1667*fe6f45f6SYang Yingliang 1668*fe6f45f6SYang Yingliang if (dev_set_name(&indio_dev->dev, "iio:device%d", iio_dev_opaque->id)) { 1669*fe6f45f6SYang Yingliang ida_simple_remove(&iio_ida, iio_dev_opaque->id); 1670*fe6f45f6SYang Yingliang kfree(iio_dev_opaque); 1671*fe6f45f6SYang Yingliang return NULL; 1672*fe6f45f6SYang Yingliang } 1673*fe6f45f6SYang Yingliang 16746a8c6b26SAlexandru Ardelean INIT_LIST_HEAD(&iio_dev_opaque->buffer_list); 16758dedcc3eSAlexandru Ardelean INIT_LIST_HEAD(&iio_dev_opaque->ioctl_handlers); 1676a980e046SJonathan Cameron 16770d596bb2SAlexandru Ardelean return indio_dev; 1678a980e046SJonathan Cameron } 16797cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_alloc); 1680a980e046SJonathan Cameron 1681a7e57dceSSachin Kamat /** 1682a7e57dceSSachin Kamat * iio_device_free() - free an iio_dev from a driver 1683a7e57dceSSachin Kamat * @dev: the iio_dev associated with the device 1684a7e57dceSSachin Kamat **/ 16857cbb7537SLars-Peter Clausen void iio_device_free(struct iio_dev *dev) 1686a980e046SJonathan Cameron { 1687e407fd65SLars-Peter Clausen if (dev) 1688e407fd65SLars-Peter Clausen put_device(&dev->dev); 1689a980e046SJonathan Cameron } 16907cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_free); 1691a980e046SJonathan Cameron 1692cf5724e9SYicong Yang static void devm_iio_device_release(void *iio_dev) 16939dabaf5eSGrygorii Strashko { 1694cf5724e9SYicong Yang iio_device_free(iio_dev); 16959dabaf5eSGrygorii Strashko } 16969dabaf5eSGrygorii Strashko 1697a7e57dceSSachin Kamat /** 1698a7e57dceSSachin Kamat * devm_iio_device_alloc - Resource-managed iio_device_alloc() 169978289b4aSAlexandru Ardelean * @parent: Device to allocate iio_dev for, and parent for this IIO device 1700a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 1701a7e57dceSSachin Kamat * 1702a7e57dceSSachin Kamat * Managed iio_device_alloc. iio_dev allocated with this function is 1703a7e57dceSSachin Kamat * automatically freed on driver detach. 1704a7e57dceSSachin Kamat * 1705a7e57dceSSachin Kamat * RETURNS: 1706a7e57dceSSachin Kamat * Pointer to allocated iio_dev on success, NULL on failure. 1707a7e57dceSSachin Kamat */ 170878289b4aSAlexandru Ardelean struct iio_dev *devm_iio_device_alloc(struct device *parent, int sizeof_priv) 17099dabaf5eSGrygorii Strashko { 1710cf5724e9SYicong Yang struct iio_dev *iio_dev; 1711cf5724e9SYicong Yang int ret; 17129dabaf5eSGrygorii Strashko 171378289b4aSAlexandru Ardelean iio_dev = iio_device_alloc(parent, sizeof_priv); 1714cf5724e9SYicong Yang if (!iio_dev) 1715cf5724e9SYicong Yang return NULL; 1716cf5724e9SYicong Yang 1717cf5724e9SYicong Yang ret = devm_add_action_or_reset(parent, devm_iio_device_release, 1718cf5724e9SYicong Yang iio_dev); 1719cf5724e9SYicong Yang if (ret) 172043fa7394SDan Carpenter return NULL; 17219dabaf5eSGrygorii Strashko 17229dabaf5eSGrygorii Strashko return iio_dev; 17239dabaf5eSGrygorii Strashko } 17249dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_alloc); 17259dabaf5eSGrygorii Strashko 1726a7e57dceSSachin Kamat /** 1727a980e046SJonathan Cameron * iio_chrdev_open() - chrdev file open for buffer access and ioctls 17282498dcf6SCristina Opriceana * @inode: Inode structure for identifying the device in the file system 17292498dcf6SCristina Opriceana * @filp: File structure for iio device used to keep and later access 17302498dcf6SCristina Opriceana * private data 17312498dcf6SCristina Opriceana * 17322498dcf6SCristina Opriceana * Return: 0 on success or -EBUSY if the device is already opened 1733a980e046SJonathan Cameron **/ 1734a980e046SJonathan Cameron static int iio_chrdev_open(struct inode *inode, struct file *filp) 1735a980e046SJonathan Cameron { 1736396f7234SJonathan Cameron struct iio_dev_opaque *iio_dev_opaque = 1737396f7234SJonathan Cameron container_of(inode->i_cdev, struct iio_dev_opaque, chrdev); 1738396f7234SJonathan Cameron struct iio_dev *indio_dev = &iio_dev_opaque->indio_dev; 1739be24dcb1SAlexandru Ardelean struct iio_dev_buffer_pair *ib; 1740a980e046SJonathan Cameron 17418b1c82cbSJonathan Cameron if (test_and_set_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags)) 1742a980e046SJonathan Cameron return -EBUSY; 1743a980e046SJonathan Cameron 1744cadc2125SLars-Peter Clausen iio_device_get(indio_dev); 1745cadc2125SLars-Peter Clausen 1746be24dcb1SAlexandru Ardelean ib = kmalloc(sizeof(*ib), GFP_KERNEL); 1747be24dcb1SAlexandru Ardelean if (!ib) { 1748be24dcb1SAlexandru Ardelean iio_device_put(indio_dev); 17498b1c82cbSJonathan Cameron clear_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags); 1750be24dcb1SAlexandru Ardelean return -ENOMEM; 1751be24dcb1SAlexandru Ardelean } 1752be24dcb1SAlexandru Ardelean 1753be24dcb1SAlexandru Ardelean ib->indio_dev = indio_dev; 1754be24dcb1SAlexandru Ardelean ib->buffer = indio_dev->buffer; 1755be24dcb1SAlexandru Ardelean 1756be24dcb1SAlexandru Ardelean filp->private_data = ib; 1757a980e046SJonathan Cameron 1758a980e046SJonathan Cameron return 0; 1759a980e046SJonathan Cameron } 1760a980e046SJonathan Cameron 1761a980e046SJonathan Cameron /** 1762a980e046SJonathan Cameron * iio_chrdev_release() - chrdev file close buffer access and ioctls 17632498dcf6SCristina Opriceana * @inode: Inode structure pointer for the char device 17642498dcf6SCristina Opriceana * @filp: File structure pointer for the char device 17652498dcf6SCristina Opriceana * 17662498dcf6SCristina Opriceana * Return: 0 for successful release 17672498dcf6SCristina Opriceana */ 1768a980e046SJonathan Cameron static int iio_chrdev_release(struct inode *inode, struct file *filp) 1769a980e046SJonathan Cameron { 1770be24dcb1SAlexandru Ardelean struct iio_dev_buffer_pair *ib = filp->private_data; 1771396f7234SJonathan Cameron struct iio_dev_opaque *iio_dev_opaque = 1772396f7234SJonathan Cameron container_of(inode->i_cdev, struct iio_dev_opaque, chrdev); 1773396f7234SJonathan Cameron struct iio_dev *indio_dev = &iio_dev_opaque->indio_dev; 1774be24dcb1SAlexandru Ardelean kfree(ib); 17758b1c82cbSJonathan Cameron clear_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags); 1776cadc2125SLars-Peter Clausen iio_device_put(indio_dev); 1777cadc2125SLars-Peter Clausen 1778a980e046SJonathan Cameron return 0; 1779a980e046SJonathan Cameron } 1780a980e046SJonathan Cameron 17818dedcc3eSAlexandru Ardelean void iio_device_ioctl_handler_register(struct iio_dev *indio_dev, 17828dedcc3eSAlexandru Ardelean struct iio_ioctl_handler *h) 17838dedcc3eSAlexandru Ardelean { 17848dedcc3eSAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 17858dedcc3eSAlexandru Ardelean 17868dedcc3eSAlexandru Ardelean list_add_tail(&h->entry, &iio_dev_opaque->ioctl_handlers); 17878dedcc3eSAlexandru Ardelean } 17888dedcc3eSAlexandru Ardelean 17898dedcc3eSAlexandru Ardelean void iio_device_ioctl_handler_unregister(struct iio_ioctl_handler *h) 17908dedcc3eSAlexandru Ardelean { 17918dedcc3eSAlexandru Ardelean list_del(&h->entry); 17928dedcc3eSAlexandru Ardelean } 17938dedcc3eSAlexandru Ardelean 1794a980e046SJonathan Cameron static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 1795a980e046SJonathan Cameron { 1796be24dcb1SAlexandru Ardelean struct iio_dev_buffer_pair *ib = filp->private_data; 1797be24dcb1SAlexandru Ardelean struct iio_dev *indio_dev = ib->indio_dev; 17988dedcc3eSAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 17998dedcc3eSAlexandru Ardelean struct iio_ioctl_handler *h; 18008dedcc3eSAlexandru Ardelean int ret = -ENODEV; 1801a980e046SJonathan Cameron 1802b804e2b7SJonathan Cameron mutex_lock(&iio_dev_opaque->info_exist_lock); 18038dedcc3eSAlexandru Ardelean 18048dedcc3eSAlexandru Ardelean /** 18058dedcc3eSAlexandru Ardelean * The NULL check here is required to prevent crashing when a device 18068dedcc3eSAlexandru Ardelean * is being removed while userspace would still have open file handles 18078dedcc3eSAlexandru Ardelean * to try to access this device. 18088dedcc3eSAlexandru Ardelean */ 1809f18e7a06SLars-Peter Clausen if (!indio_dev->info) 18108dedcc3eSAlexandru Ardelean goto out_unlock; 1811f18e7a06SLars-Peter Clausen 18128dedcc3eSAlexandru Ardelean list_for_each_entry(h, &iio_dev_opaque->ioctl_handlers, entry) { 18138dedcc3eSAlexandru Ardelean ret = h->ioctl(indio_dev, filp, cmd, arg); 18148dedcc3eSAlexandru Ardelean if (ret != IIO_IOCTL_UNHANDLED) 18158dedcc3eSAlexandru Ardelean break; 1816a980e046SJonathan Cameron } 18178dedcc3eSAlexandru Ardelean 181874d826daSAlexandru Ardelean if (ret == IIO_IOCTL_UNHANDLED) 1819af0670b0SAlexandru Ardelean ret = -ENODEV; 182074d826daSAlexandru Ardelean 18218dedcc3eSAlexandru Ardelean out_unlock: 1822b804e2b7SJonathan Cameron mutex_unlock(&iio_dev_opaque->info_exist_lock); 18238dedcc3eSAlexandru Ardelean 18248dedcc3eSAlexandru Ardelean return ret; 1825a980e046SJonathan Cameron } 1826a980e046SJonathan Cameron 1827a980e046SJonathan Cameron static const struct file_operations iio_buffer_fileops = { 1828a980e046SJonathan Cameron .owner = THIS_MODULE, 1829a980e046SJonathan Cameron .llseek = noop_llseek, 1830ee8caea0SAlexandru Ardelean .read = iio_buffer_read_outer_addr, 1831ee8caea0SAlexandru Ardelean .poll = iio_buffer_poll_addr, 1832a980e046SJonathan Cameron .unlocked_ioctl = iio_ioctl, 18331832f2d8SArnd Bergmann .compat_ioctl = compat_ptr_ioctl, 1834ee8caea0SAlexandru Ardelean .open = iio_chrdev_open, 1835ee8caea0SAlexandru Ardelean .release = iio_chrdev_release, 1836a980e046SJonathan Cameron }; 1837a980e046SJonathan Cameron 18388ebaa3ffSAlexandru Ardelean static const struct file_operations iio_event_fileops = { 18398ebaa3ffSAlexandru Ardelean .owner = THIS_MODULE, 18408ebaa3ffSAlexandru Ardelean .llseek = noop_llseek, 18418ebaa3ffSAlexandru Ardelean .unlocked_ioctl = iio_ioctl, 18428ebaa3ffSAlexandru Ardelean .compat_ioctl = compat_ptr_ioctl, 18438ebaa3ffSAlexandru Ardelean .open = iio_chrdev_open, 18448ebaa3ffSAlexandru Ardelean .release = iio_chrdev_release, 18458ebaa3ffSAlexandru Ardelean }; 18468ebaa3ffSAlexandru Ardelean 18478f5d8727SVlad Dogaru static int iio_check_unique_scan_index(struct iio_dev *indio_dev) 18488f5d8727SVlad Dogaru { 18498f5d8727SVlad Dogaru int i, j; 18508f5d8727SVlad Dogaru const struct iio_chan_spec *channels = indio_dev->channels; 18518f5d8727SVlad Dogaru 18528f5d8727SVlad Dogaru if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES)) 18538f5d8727SVlad Dogaru return 0; 18548f5d8727SVlad Dogaru 18558f5d8727SVlad Dogaru for (i = 0; i < indio_dev->num_channels - 1; i++) { 18568f5d8727SVlad Dogaru if (channels[i].scan_index < 0) 18578f5d8727SVlad Dogaru continue; 18588f5d8727SVlad Dogaru for (j = i + 1; j < indio_dev->num_channels; j++) 18598f5d8727SVlad Dogaru if (channels[i].scan_index == channels[j].scan_index) { 18608f5d8727SVlad Dogaru dev_err(&indio_dev->dev, 18618f5d8727SVlad Dogaru "Duplicate scan index %d\n", 18628f5d8727SVlad Dogaru channels[i].scan_index); 18638f5d8727SVlad Dogaru return -EINVAL; 18648f5d8727SVlad Dogaru } 18658f5d8727SVlad Dogaru } 18668f5d8727SVlad Dogaru 18678f5d8727SVlad Dogaru return 0; 18688f5d8727SVlad Dogaru } 18698f5d8727SVlad Dogaru 18700e0761f8SPaul Cercueil static int iio_check_extended_name(const struct iio_dev *indio_dev) 18710e0761f8SPaul Cercueil { 18720e0761f8SPaul Cercueil unsigned int i; 18730e0761f8SPaul Cercueil 18740e0761f8SPaul Cercueil if (!indio_dev->info->read_label) 18750e0761f8SPaul Cercueil return 0; 18760e0761f8SPaul Cercueil 18770e0761f8SPaul Cercueil for (i = 0; i < indio_dev->num_channels; i++) { 18780e0761f8SPaul Cercueil if (indio_dev->channels[i].extend_name) { 18790e0761f8SPaul Cercueil dev_err(&indio_dev->dev, 18800e0761f8SPaul Cercueil "Cannot use labels and extend_name at the same time\n"); 18810e0761f8SPaul Cercueil return -EINVAL; 18820e0761f8SPaul Cercueil } 18830e0761f8SPaul Cercueil } 18840e0761f8SPaul Cercueil 18850e0761f8SPaul Cercueil return 0; 18860e0761f8SPaul Cercueil } 18870e0761f8SPaul Cercueil 1888a980e046SJonathan Cameron static const struct iio_buffer_setup_ops noop_ring_setup_ops; 1889a980e046SJonathan Cameron 189063b19547SJonathan Cameron int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod) 1891a980e046SJonathan Cameron { 18928ebaa3ffSAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 18936f71bf19SHans de Goede const char *label; 1894a980e046SJonathan Cameron int ret; 1895a980e046SJonathan Cameron 1896a17cb784SAlexandru Ardelean if (!indio_dev->info) 1897a17cb784SAlexandru Ardelean return -EINVAL; 1898a17cb784SAlexandru Ardelean 18996eaf9f6aSJonathan Cameron iio_dev_opaque->driver_module = this_mod; 190017d82b47SGuenter Roeck /* If the calling driver did not initialize of_node, do it here */ 190117d82b47SGuenter Roeck if (!indio_dev->dev.of_node && indio_dev->dev.parent) 190217d82b47SGuenter Roeck indio_dev->dev.of_node = indio_dev->dev.parent->of_node; 190317d82b47SGuenter Roeck 19046f71bf19SHans de Goede label = of_get_property(indio_dev->dev.of_node, "label", NULL); 19056f71bf19SHans de Goede if (label) 19066f71bf19SHans de Goede indio_dev->label = label; 19072c3d0c9fSPhil Reid 19088f5d8727SVlad Dogaru ret = iio_check_unique_scan_index(indio_dev); 19098f5d8727SVlad Dogaru if (ret < 0) 19108f5d8727SVlad Dogaru return ret; 19118f5d8727SVlad Dogaru 19120e0761f8SPaul Cercueil ret = iio_check_extended_name(indio_dev); 19130e0761f8SPaul Cercueil if (ret < 0) 19140e0761f8SPaul Cercueil return ret; 19150e0761f8SPaul Cercueil 19168915aacaSGreg Kroah-Hartman iio_device_register_debugfs(indio_dev); 19173e1b6c95SLars-Peter Clausen 1918ee708e6bSAlexandru Ardelean ret = iio_buffers_alloc_sysfs_and_mask(indio_dev); 19193e1b6c95SLars-Peter Clausen if (ret) { 19203e1b6c95SLars-Peter Clausen dev_err(indio_dev->dev.parent, 19213e1b6c95SLars-Peter Clausen "Failed to create buffer sysfs interfaces\n"); 19223e1b6c95SLars-Peter Clausen goto error_unreg_debugfs; 19233e1b6c95SLars-Peter Clausen } 19243e1b6c95SLars-Peter Clausen 1925a980e046SJonathan Cameron ret = iio_device_register_sysfs(indio_dev); 1926a980e046SJonathan Cameron if (ret) { 1927a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1928a980e046SJonathan Cameron "Failed to register sysfs interfaces\n"); 19293e1b6c95SLars-Peter Clausen goto error_buffer_free_sysfs; 1930a980e046SJonathan Cameron } 1931a980e046SJonathan Cameron ret = iio_device_register_eventset(indio_dev); 1932a980e046SJonathan Cameron if (ret) { 1933a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1934a980e046SJonathan Cameron "Failed to register event set\n"); 1935a980e046SJonathan Cameron goto error_free_sysfs; 1936a980e046SJonathan Cameron } 1937d89e119aSBenjamin Gaignard if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES) 1938a980e046SJonathan Cameron iio_device_register_trigger_consumer(indio_dev); 1939a980e046SJonathan Cameron 1940a980e046SJonathan Cameron if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) && 1941a980e046SJonathan Cameron indio_dev->setup_ops == NULL) 1942a980e046SJonathan Cameron indio_dev->setup_ops = &noop_ring_setup_ops; 1943a980e046SJonathan Cameron 1944ee708e6bSAlexandru Ardelean if (iio_dev_opaque->attached_buffers_cnt) 1945396f7234SJonathan Cameron cdev_init(&iio_dev_opaque->chrdev, &iio_buffer_fileops); 19468ebaa3ffSAlexandru Ardelean else if (iio_dev_opaque->event_interface) 1947396f7234SJonathan Cameron cdev_init(&iio_dev_opaque->chrdev, &iio_event_fileops); 194863b19547SJonathan Cameron 1949ee708e6bSAlexandru Ardelean if (iio_dev_opaque->attached_buffers_cnt || iio_dev_opaque->event_interface) { 195015ea2878SJonathan Cameron indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), iio_dev_opaque->id); 1951396f7234SJonathan Cameron iio_dev_opaque->chrdev.owner = this_mod; 19528ebaa3ffSAlexandru Ardelean } 195338923911SLogan Gunthorpe 195432f17172SAlexandru Ardelean /* assign device groups now; they should be all registered now */ 195532f17172SAlexandru Ardelean indio_dev->dev.groups = iio_dev_opaque->groups; 195632f17172SAlexandru Ardelean 1957396f7234SJonathan Cameron ret = cdev_device_add(&iio_dev_opaque->chrdev, &indio_dev->dev); 1958a980e046SJonathan Cameron if (ret < 0) 19590d5b7daeSLars-Peter Clausen goto error_unreg_eventset; 1960a980e046SJonathan Cameron 19610d5b7daeSLars-Peter Clausen return 0; 196238923911SLogan Gunthorpe 1963a980e046SJonathan Cameron error_unreg_eventset: 1964a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 1965a980e046SJonathan Cameron error_free_sysfs: 1966a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 19673e1b6c95SLars-Peter Clausen error_buffer_free_sysfs: 1968ee708e6bSAlexandru Ardelean iio_buffers_free_sysfs_and_mask(indio_dev); 1969a980e046SJonathan Cameron error_unreg_debugfs: 1970a980e046SJonathan Cameron iio_device_unregister_debugfs(indio_dev); 1971a980e046SJonathan Cameron return ret; 1972a980e046SJonathan Cameron } 197363b19547SJonathan Cameron EXPORT_SYMBOL(__iio_device_register); 1974a980e046SJonathan Cameron 1975a7e57dceSSachin Kamat /** 1976a7e57dceSSachin Kamat * iio_device_unregister() - unregister a device from the IIO subsystem 1977a7e57dceSSachin Kamat * @indio_dev: Device structure representing the device. 1978a7e57dceSSachin Kamat **/ 1979a980e046SJonathan Cameron void iio_device_unregister(struct iio_dev *indio_dev) 1980a980e046SJonathan Cameron { 1981396f7234SJonathan Cameron struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1982396f7234SJonathan Cameron 1983396f7234SJonathan Cameron cdev_device_del(&iio_dev_opaque->chrdev, &indio_dev->dev); 1984a87c82e4SLars-Peter Clausen 1985b804e2b7SJonathan Cameron mutex_lock(&iio_dev_opaque->info_exist_lock); 19867f75591fSFabrice Gasnier 1987bc4c9612SLars-Peter Clausen iio_device_unregister_debugfs(indio_dev); 19880d5b7daeSLars-Peter Clausen 1989a87c82e4SLars-Peter Clausen iio_disable_all_buffers(indio_dev); 1990a87c82e4SLars-Peter Clausen 1991a980e046SJonathan Cameron indio_dev->info = NULL; 1992d2f0a48fSLars-Peter Clausen 1993d2f0a48fSLars-Peter Clausen iio_device_wakeup_eventset(indio_dev); 1994d2f0a48fSLars-Peter Clausen iio_buffer_wakeup_poll(indio_dev); 1995d2f0a48fSLars-Peter Clausen 1996b804e2b7SJonathan Cameron mutex_unlock(&iio_dev_opaque->info_exist_lock); 19973e1b6c95SLars-Peter Clausen 1998ee708e6bSAlexandru Ardelean iio_buffers_free_sysfs_and_mask(indio_dev); 1999a980e046SJonathan Cameron } 2000a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_unregister); 20018caa07c0SSachin Kamat 2002cf5724e9SYicong Yang static void devm_iio_device_unreg(void *indio_dev) 20038caa07c0SSachin Kamat { 2004cf5724e9SYicong Yang iio_device_unregister(indio_dev); 20058caa07c0SSachin Kamat } 20068caa07c0SSachin Kamat 200763b19547SJonathan Cameron int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev, 200863b19547SJonathan Cameron struct module *this_mod) 20098caa07c0SSachin Kamat { 20108caa07c0SSachin Kamat int ret; 20118caa07c0SSachin Kamat 201263b19547SJonathan Cameron ret = __iio_device_register(indio_dev, this_mod); 2013cf5724e9SYicong Yang if (ret) 20148caa07c0SSachin Kamat return ret; 2015cf5724e9SYicong Yang 2016cf5724e9SYicong Yang return devm_add_action_or_reset(dev, devm_iio_device_unreg, indio_dev); 20178caa07c0SSachin Kamat } 201863b19547SJonathan Cameron EXPORT_SYMBOL_GPL(__devm_iio_device_register); 20198caa07c0SSachin Kamat 20208caa07c0SSachin Kamat /** 202108a33805SAlison Schofield * iio_device_claim_direct_mode - Keep device in direct mode 202208a33805SAlison Schofield * @indio_dev: the iio_dev associated with the device 202308a33805SAlison Schofield * 202408a33805SAlison Schofield * If the device is in direct mode it is guaranteed to stay 202508a33805SAlison Schofield * that way until iio_device_release_direct_mode() is called. 202608a33805SAlison Schofield * 202708a33805SAlison Schofield * Use with iio_device_release_direct_mode() 202808a33805SAlison Schofield * 202908a33805SAlison Schofield * Returns: 0 on success, -EBUSY on failure 203008a33805SAlison Schofield */ 203108a33805SAlison Schofield int iio_device_claim_direct_mode(struct iio_dev *indio_dev) 203208a33805SAlison Schofield { 203308a33805SAlison Schofield mutex_lock(&indio_dev->mlock); 203408a33805SAlison Schofield 203508a33805SAlison Schofield if (iio_buffer_enabled(indio_dev)) { 203608a33805SAlison Schofield mutex_unlock(&indio_dev->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 { 205408a33805SAlison Schofield mutex_unlock(&indio_dev->mlock); 205508a33805SAlison Schofield } 205608a33805SAlison Schofield EXPORT_SYMBOL_GPL(iio_device_release_direct_mode); 205708a33805SAlison Schofield 2058a980e046SJonathan Cameron subsys_initcall(iio_init); 2059a980e046SJonathan Cameron module_exit(iio_exit); 2060a980e046SJonathan Cameron 2061c8b95952SJonathan Cameron MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); 2062a980e046SJonathan Cameron MODULE_DESCRIPTION("Industrial I/O core"); 2063a980e046SJonathan Cameron MODULE_LICENSE("GPL"); 2064