1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2*65659a8dSAndy Shevchenko /* 3*65659a8dSAndy Shevchenko * The industrial I/O core 4a980e046SJonathan Cameron * 5a980e046SJonathan Cameron * Copyright (c) 2008 Jonathan Cameron 6a980e046SJonathan Cameron * 7a980e046SJonathan Cameron * Based on elements of hwmon and input subsystems. 8a980e046SJonathan Cameron */ 9a980e046SJonathan Cameron 103176dd5dSSachin Kamat #define pr_fmt(fmt) "iio-core: " fmt 113176dd5dSSachin Kamat 1205f59abaSAndy Shevchenko #include <linux/anon_inodes.h> 1305f59abaSAndy Shevchenko #include <linux/cdev.h> 1405f59abaSAndy Shevchenko #include <linux/debugfs.h> 1505f59abaSAndy Shevchenko #include <linux/device.h> 1605f59abaSAndy Shevchenko #include <linux/err.h> 1705f59abaSAndy Shevchenko #include <linux/fs.h> 18a980e046SJonathan Cameron #include <linux/idr.h> 19a980e046SJonathan Cameron #include <linux/kdev_t.h> 2005f59abaSAndy Shevchenko #include <linux/kernel.h> 2105f59abaSAndy Shevchenko #include <linux/module.h> 2205f59abaSAndy Shevchenko #include <linux/mutex.h> 23a980e046SJonathan Cameron #include <linux/poll.h> 24fb158971SAndy Shevchenko #include <linux/property.h> 25a980e046SJonathan Cameron #include <linux/sched.h> 26a980e046SJonathan Cameron #include <linux/slab.h> 2705f59abaSAndy Shevchenko #include <linux/wait.h> 2805f59abaSAndy Shevchenko 299e69c935SLars-Peter Clausen #include <linux/iio/buffer.h> 3033dd94cbSJonathan Cameron #include <linux/iio/buffer_impl.h> 3105f59abaSAndy Shevchenko #include <linux/iio/events.h> 3205f59abaSAndy Shevchenko #include <linux/iio/iio-opaque.h> 3305f59abaSAndy Shevchenko #include <linux/iio/iio.h> 3405f59abaSAndy Shevchenko #include <linux/iio/sysfs.h> 3505f59abaSAndy Shevchenko 3605f59abaSAndy Shevchenko #include "iio_core.h" 3705f59abaSAndy Shevchenko #include "iio_core_trigger.h" 38a980e046SJonathan Cameron 39a980e046SJonathan Cameron /* IDA to assign each registered device a unique id */ 40a980e046SJonathan Cameron static DEFINE_IDA(iio_ida); 41a980e046SJonathan Cameron 42a980e046SJonathan Cameron static dev_t iio_devt; 43a980e046SJonathan Cameron 44a980e046SJonathan Cameron #define IIO_DEV_MAX 256 45a980e046SJonathan Cameron struct bus_type iio_bus_type = { 46a980e046SJonathan Cameron .name = "iio", 47a980e046SJonathan Cameron }; 48a980e046SJonathan Cameron EXPORT_SYMBOL(iio_bus_type); 49a980e046SJonathan Cameron 50a980e046SJonathan Cameron static struct dentry *iio_debugfs_dentry; 51a980e046SJonathan Cameron 52a980e046SJonathan Cameron static const char * const iio_direction[] = { 53a980e046SJonathan Cameron [0] = "in", 54a980e046SJonathan Cameron [1] = "out", 55a980e046SJonathan Cameron }; 56a980e046SJonathan Cameron 57a980e046SJonathan Cameron static const char * const iio_chan_type_name_spec[] = { 58a980e046SJonathan Cameron [IIO_VOLTAGE] = "voltage", 59a980e046SJonathan Cameron [IIO_CURRENT] = "current", 60a980e046SJonathan Cameron [IIO_POWER] = "power", 61a980e046SJonathan Cameron [IIO_ACCEL] = "accel", 62a980e046SJonathan Cameron [IIO_ANGL_VEL] = "anglvel", 63a980e046SJonathan Cameron [IIO_MAGN] = "magn", 64a980e046SJonathan Cameron [IIO_LIGHT] = "illuminance", 65a980e046SJonathan Cameron [IIO_INTENSITY] = "intensity", 66a980e046SJonathan Cameron [IIO_PROXIMITY] = "proximity", 67a980e046SJonathan Cameron [IIO_TEMP] = "temp", 68a980e046SJonathan Cameron [IIO_INCLI] = "incli", 69a980e046SJonathan Cameron [IIO_ROT] = "rot", 70a980e046SJonathan Cameron [IIO_ANGL] = "angl", 71a980e046SJonathan Cameron [IIO_TIMESTAMP] = "timestamp", 72a980e046SJonathan Cameron [IIO_CAPACITANCE] = "capacitance", 73a6b12855SMichael Hennerich [IIO_ALTVOLTAGE] = "altvoltage", 7421cd1fabSJon Brenner [IIO_CCT] = "cct", 75c4f0c693SLars-Peter Clausen [IIO_PRESSURE] = "pressure", 76ac216aa2SHarald Geyer [IIO_HUMIDITYRELATIVE] = "humidityrelative", 7755aebeb9SDaniel Baluta [IIO_ACTIVITY] = "activity", 78a88bfe78SIrina Tirdea [IIO_STEPS] = "steps", 7972c66644SIrina Tirdea [IIO_ENERGY] = "energy", 80cc3c9eecSIrina Tirdea [IIO_DISTANCE] = "distance", 815a1a9329SIrina Tirdea [IIO_VELOCITY] = "velocity", 828ff6b3bcSMatt Ranostay [IIO_CONCENTRATION] = "concentration", 83d38d5469SMatt Ranostay [IIO_RESISTANCE] = "resistance", 84ecb3a7ccSMatt Ranostay [IIO_PH] = "ph", 85d409404cSPeter Meerwald-Stadler [IIO_UVINDEX] = "uvindex", 864b9d2090SMatt Ranostay [IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity", 871a8f324aSWilliam Breathitt Gray [IIO_COUNT] = "count", 881a8f324aSWilliam Breathitt Gray [IIO_INDEX] = "index", 89571299d0SSong Hongyan [IIO_GRAVITY] = "gravity", 903055a6cfSEugen Hristev [IIO_POSITIONRELATIVE] = "positionrelative", 91c73314e6SMathieu Othacehe [IIO_PHASE] = "phase", 9217abc9ecSTomasz Duszynski [IIO_MASSCONCENTRATION] = "massconcentration", 93a980e046SJonathan Cameron }; 94a980e046SJonathan Cameron 95a980e046SJonathan Cameron static const char * const iio_modifier_names[] = { 96a980e046SJonathan Cameron [IIO_MOD_X] = "x", 97a980e046SJonathan Cameron [IIO_MOD_Y] = "y", 98a980e046SJonathan Cameron [IIO_MOD_Z] = "z", 994b8d8015SPeter Meerwald [IIO_MOD_X_AND_Y] = "x&y", 1004b8d8015SPeter Meerwald [IIO_MOD_X_AND_Z] = "x&z", 1014b8d8015SPeter Meerwald [IIO_MOD_Y_AND_Z] = "y&z", 1024b8d8015SPeter Meerwald [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z", 1034b8d8015SPeter Meerwald [IIO_MOD_X_OR_Y] = "x|y", 1044b8d8015SPeter Meerwald [IIO_MOD_X_OR_Z] = "x|z", 1054b8d8015SPeter Meerwald [IIO_MOD_Y_OR_Z] = "y|z", 1064b8d8015SPeter Meerwald [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z", 1078f5879b2SJonathan Cameron [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)", 108cf82cb81SJonathan Cameron [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2", 109a980e046SJonathan Cameron [IIO_MOD_LIGHT_BOTH] = "both", 110a980e046SJonathan Cameron [IIO_MOD_LIGHT_IR] = "ir", 11121cd1fabSJon Brenner [IIO_MOD_LIGHT_CLEAR] = "clear", 11221cd1fabSJon Brenner [IIO_MOD_LIGHT_RED] = "red", 11321cd1fabSJon Brenner [IIO_MOD_LIGHT_GREEN] = "green", 11421cd1fabSJon Brenner [IIO_MOD_LIGHT_BLUE] = "blue", 1152c5ff1f9SPeter Meerwald-Stadler [IIO_MOD_LIGHT_UV] = "uv", 116c0e4e0fdSMaxime Roussin-Bélanger [IIO_MOD_LIGHT_DUV] = "duv", 1175082f405SSrinivas Pandruvada [IIO_MOD_QUATERNION] = "quaternion", 118638b43b3SPeter Meerwald [IIO_MOD_TEMP_AMBIENT] = "ambient", 119638b43b3SPeter Meerwald [IIO_MOD_TEMP_OBJECT] = "object", 12011b8ddabSReyad Attiyat [IIO_MOD_NORTH_MAGN] = "from_north_magnetic", 12111b8ddabSReyad Attiyat [IIO_MOD_NORTH_TRUE] = "from_north_true", 12211b8ddabSReyad Attiyat [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp", 12311b8ddabSReyad Attiyat [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp", 12455aebeb9SDaniel Baluta [IIO_MOD_RUNNING] = "running", 12555aebeb9SDaniel Baluta [IIO_MOD_JOGGING] = "jogging", 12655aebeb9SDaniel Baluta [IIO_MOD_WALKING] = "walking", 12755aebeb9SDaniel Baluta [IIO_MOD_STILL] = "still", 1285a1a9329SIrina Tirdea [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)", 1291ce87f21SLars-Peter Clausen [IIO_MOD_I] = "i", 1301ce87f21SLars-Peter Clausen [IIO_MOD_Q] = "q", 1318ff6b3bcSMatt Ranostay [IIO_MOD_CO2] = "co2", 1328ff6b3bcSMatt Ranostay [IIO_MOD_VOC] = "voc", 13317abc9ecSTomasz Duszynski [IIO_MOD_PM1] = "pm1", 13417abc9ecSTomasz Duszynski [IIO_MOD_PM2P5] = "pm2p5", 13517abc9ecSTomasz Duszynski [IIO_MOD_PM4] = "pm4", 13617abc9ecSTomasz Duszynski [IIO_MOD_PM10] = "pm10", 13725f02d32SMatt Ranostay [IIO_MOD_ETHANOL] = "ethanol", 13825f02d32SMatt Ranostay [IIO_MOD_H2] = "h2", 1394ffa22fdSMatt Ranostay [IIO_MOD_O2] = "o2", 140bcc57a48SAndrea Merello [IIO_MOD_LINEAR_X] = "linear_x", 141bcc57a48SAndrea Merello [IIO_MOD_LINEAR_Y] = "linear_y", 142bcc57a48SAndrea Merello [IIO_MOD_LINEAR_Z] = "linear_z", 143dcedf145SAndrea Merello [IIO_MOD_PITCH] = "pitch", 144dcedf145SAndrea Merello [IIO_MOD_YAW] = "yaw", 145dcedf145SAndrea Merello [IIO_MOD_ROLL] = "roll", 146a980e046SJonathan Cameron }; 147a980e046SJonathan Cameron 148a980e046SJonathan Cameron /* relies on pairs of these shared then separate */ 149a980e046SJonathan Cameron static const char * const iio_chan_info_postfix[] = { 150a980e046SJonathan Cameron [IIO_CHAN_INFO_RAW] = "raw", 151a980e046SJonathan Cameron [IIO_CHAN_INFO_PROCESSED] = "input", 152a980e046SJonathan Cameron [IIO_CHAN_INFO_SCALE] = "scale", 153a980e046SJonathan Cameron [IIO_CHAN_INFO_OFFSET] = "offset", 154a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBSCALE] = "calibscale", 155a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBBIAS] = "calibbias", 156a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK] = "peak_raw", 157a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale", 158a980e046SJonathan Cameron [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw", 159a980e046SJonathan Cameron [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw", 160a980e046SJonathan Cameron [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY] 161a980e046SJonathan Cameron = "filter_low_pass_3db_frequency", 1623f7f642bSMartin Fuzzey [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY] 1633f7f642bSMartin Fuzzey = "filter_high_pass_3db_frequency", 164a980e046SJonathan Cameron [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency", 165a6b12855SMichael Hennerich [IIO_CHAN_INFO_FREQUENCY] = "frequency", 166a6b12855SMichael Hennerich [IIO_CHAN_INFO_PHASE] = "phase", 167b65d6212SMichael Hennerich [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain", 1687c9ab035Ssrinivas pandruvada [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis", 1691c71a286SYe Xiang [IIO_CHAN_INFO_HYSTERESIS_RELATIVE] = "hysteresis_relative", 170899d90bdSPeter Meerwald [IIO_CHAN_INFO_INT_TIME] = "integration_time", 171a88bfe78SIrina Tirdea [IIO_CHAN_INFO_ENABLE] = "en", 172bcdf28fbSIrina Tirdea [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight", 173d37f6836SIrina Tirdea [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight", 1742f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count", 1752f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time", 176c8a85854SVianney le Clément de Saint-Marcq [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity", 177faaa4495SIrina Tirdea [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio", 178ddfb97d8SAndrea Merello [IIO_CHAN_INFO_THERMOCOUPLE_TYPE] = "thermocouple_type", 179dde8ceecSCrt Mori [IIO_CHAN_INFO_CALIBAMBIENT] = "calibambient", 1805c64990bSJonathan Cameron [IIO_CHAN_INFO_ZEROPOINT] = "zeropoint", 181a980e046SJonathan Cameron }; 18215ea2878SJonathan Cameron /** 18315ea2878SJonathan Cameron * iio_device_id() - query the unique ID for the device 18415ea2878SJonathan Cameron * @indio_dev: Device structure whose ID is being queried 18515ea2878SJonathan Cameron * 18615ea2878SJonathan Cameron * The IIO device ID is a unique index used for example for the naming 187*65659a8dSAndy Shevchenko * of the character device /dev/iio\:device[ID]. 188*65659a8dSAndy Shevchenko * 189*65659a8dSAndy Shevchenko * Returns: Unique ID for the device. 19015ea2878SJonathan Cameron */ 19115ea2878SJonathan Cameron int iio_device_id(struct iio_dev *indio_dev) 19215ea2878SJonathan Cameron { 19315ea2878SJonathan Cameron struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 19415ea2878SJonathan Cameron 19515ea2878SJonathan Cameron return iio_dev_opaque->id; 19615ea2878SJonathan Cameron } 19715ea2878SJonathan Cameron EXPORT_SYMBOL_GPL(iio_device_id); 198a980e046SJonathan Cameron 199d9a0e73cSLars-Peter Clausen /** 2002f53b4adSMiquel Raynal * iio_buffer_enabled() - helper function to test if the buffer is enabled 2012f53b4adSMiquel Raynal * @indio_dev: IIO device structure for device 202*65659a8dSAndy Shevchenko * 203*65659a8dSAndy Shevchenko * Returns: True, if the buffer is enabled. 2042f53b4adSMiquel Raynal */ 2052f53b4adSMiquel Raynal bool iio_buffer_enabled(struct iio_dev *indio_dev) 2062f53b4adSMiquel Raynal { 20751570c9dSMiquel Raynal struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 20851570c9dSMiquel Raynal 209b662f4baSAndy Shevchenko return iio_dev_opaque->currentmode & 210b662f4baSAndy Shevchenko (INDIO_BUFFER_HARDWARE | INDIO_BUFFER_SOFTWARE | 211b662f4baSAndy Shevchenko INDIO_BUFFER_TRIGGERED); 2122f53b4adSMiquel Raynal } 2132f53b4adSMiquel Raynal EXPORT_SYMBOL_GPL(iio_buffer_enabled); 2142f53b4adSMiquel Raynal 21596fb1b67SAlexandru Ardelean #if defined(CONFIG_DEBUG_FS) 216c9561fd2SJonathan Cameron /* 21796fb1b67SAlexandru Ardelean * There's also a CONFIG_DEBUG_FS guard in include/linux/iio/iio.h for 21896fb1b67SAlexandru Ardelean * iio_get_debugfs_dentry() to make it inline if CONFIG_DEBUG_FS is undefined 21996fb1b67SAlexandru Ardelean */ 22096fb1b67SAlexandru Ardelean struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev) 22196fb1b67SAlexandru Ardelean { 22296fb1b67SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 2239d9ec8d0SJoe Simmons-Talbott 22496fb1b67SAlexandru Ardelean return iio_dev_opaque->debugfs_dentry; 22596fb1b67SAlexandru Ardelean } 22696fb1b67SAlexandru Ardelean EXPORT_SYMBOL_GPL(iio_get_debugfs_dentry); 22796fb1b67SAlexandru Ardelean #endif 22896fb1b67SAlexandru Ardelean 229a7e57dceSSachin Kamat /** 230a7e57dceSSachin Kamat * iio_find_channel_from_si() - get channel from its scan index 231a7e57dceSSachin Kamat * @indio_dev: device 232a7e57dceSSachin Kamat * @si: scan index to match 233*65659a8dSAndy Shevchenko * 234*65659a8dSAndy Shevchenko * Returns: 235*65659a8dSAndy Shevchenko * Constant pointer to iio_chan_spec, if scan index matches, NULL on failure. 236a7e57dceSSachin Kamat */ 237a980e046SJonathan Cameron const struct iio_chan_spec 238a980e046SJonathan Cameron *iio_find_channel_from_si(struct iio_dev *indio_dev, int si) 239a980e046SJonathan Cameron { 240a980e046SJonathan Cameron int i; 241a980e046SJonathan Cameron 242a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) 243a980e046SJonathan Cameron if (indio_dev->channels[i].scan_index == si) 244a980e046SJonathan Cameron return &indio_dev->channels[i]; 245a980e046SJonathan Cameron return NULL; 246a980e046SJonathan Cameron } 247a980e046SJonathan Cameron 248a980e046SJonathan Cameron /* This turns up an awful lot */ 249a980e046SJonathan Cameron ssize_t iio_read_const_attr(struct device *dev, 250a980e046SJonathan Cameron struct device_attribute *attr, 251a980e046SJonathan Cameron char *buf) 252a980e046SJonathan Cameron { 25383ca56b6SLars-Peter Clausen return sysfs_emit(buf, "%s\n", to_iio_const_attr(attr)->string); 254a980e046SJonathan Cameron } 255a980e046SJonathan Cameron EXPORT_SYMBOL(iio_read_const_attr); 256a980e046SJonathan Cameron 25769f0793eSGwendal Grignou /** 25869f0793eSGwendal Grignou * iio_device_set_clock() - Set current timestamping clock for the device 25969f0793eSGwendal Grignou * @indio_dev: IIO device structure containing the device 260*65659a8dSAndy Shevchenko * @clock_id: timestamping clock POSIX identifier to set. 261*65659a8dSAndy Shevchenko * 262*65659a8dSAndy Shevchenko * Returns: 0 on success, or a negative error code. 26369f0793eSGwendal Grignou */ 26469f0793eSGwendal Grignou int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id) 265bc2b7dabSGregor Boirie { 266bc2b7dabSGregor Boirie int ret; 267fa83c3baSAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 268fa83c3baSAlexandru Ardelean const struct iio_event_interface *ev_int = iio_dev_opaque->event_interface; 269bc2b7dabSGregor Boirie 27016afe125SNuno Sá ret = mutex_lock_interruptible(&iio_dev_opaque->mlock); 271bc2b7dabSGregor Boirie if (ret) 272bc2b7dabSGregor Boirie return ret; 273bc2b7dabSGregor Boirie if ((ev_int && iio_event_enabled(ev_int)) || 274bc2b7dabSGregor Boirie iio_buffer_enabled(indio_dev)) { 27516afe125SNuno Sá mutex_unlock(&iio_dev_opaque->mlock); 276bc2b7dabSGregor Boirie return -EBUSY; 277bc2b7dabSGregor Boirie } 27862a486c4SJonathan Cameron iio_dev_opaque->clock_id = clock_id; 27916afe125SNuno Sá mutex_unlock(&iio_dev_opaque->mlock); 280bc2b7dabSGregor Boirie 281bc2b7dabSGregor Boirie return 0; 282bc2b7dabSGregor Boirie } 28369f0793eSGwendal Grignou EXPORT_SYMBOL(iio_device_set_clock); 284bc2b7dabSGregor Boirie 285bc2b7dabSGregor Boirie /** 28662a486c4SJonathan Cameron * iio_device_get_clock() - Retrieve current timestamping clock for the device 28762a486c4SJonathan Cameron * @indio_dev: IIO device structure containing the device 288*65659a8dSAndy Shevchenko * 289*65659a8dSAndy Shevchenko * Returns: Clock ID of the current timestamping clock for the device. 29062a486c4SJonathan Cameron */ 29162a486c4SJonathan Cameron clockid_t iio_device_get_clock(const struct iio_dev *indio_dev) 29262a486c4SJonathan Cameron { 29362a486c4SJonathan Cameron struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 29462a486c4SJonathan Cameron 29562a486c4SJonathan Cameron return iio_dev_opaque->clock_id; 29662a486c4SJonathan Cameron } 29762a486c4SJonathan Cameron EXPORT_SYMBOL(iio_device_get_clock); 29862a486c4SJonathan Cameron 29962a486c4SJonathan Cameron /** 300bc2b7dabSGregor Boirie * iio_get_time_ns() - utility function to get a time stamp for events etc 301bc2b7dabSGregor Boirie * @indio_dev: device 302*65659a8dSAndy Shevchenko * 303*65659a8dSAndy Shevchenko * Returns: Timestamp of the event in nanoseconds. 304bc2b7dabSGregor Boirie */ 305bc2b7dabSGregor Boirie s64 iio_get_time_ns(const struct iio_dev *indio_dev) 306bc2b7dabSGregor Boirie { 30745e7f5d2SArnd Bergmann struct timespec64 tp; 308bc2b7dabSGregor Boirie 309bc2b7dabSGregor Boirie switch (iio_device_get_clock(indio_dev)) { 310bc2b7dabSGregor Boirie case CLOCK_REALTIME: 31145e7f5d2SArnd Bergmann return ktime_get_real_ns(); 312bc2b7dabSGregor Boirie case CLOCK_MONOTONIC: 31345e7f5d2SArnd Bergmann return ktime_get_ns(); 314bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_RAW: 31545e7f5d2SArnd Bergmann return ktime_get_raw_ns(); 316bc2b7dabSGregor Boirie case CLOCK_REALTIME_COARSE: 31745e7f5d2SArnd Bergmann return ktime_to_ns(ktime_get_coarse_real()); 318bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_COARSE: 31945e7f5d2SArnd Bergmann ktime_get_coarse_ts64(&tp); 32045e7f5d2SArnd Bergmann return timespec64_to_ns(&tp); 321bc2b7dabSGregor Boirie case CLOCK_BOOTTIME: 3229285ec4cSJason A. Donenfeld return ktime_get_boottime_ns(); 323bc2b7dabSGregor Boirie case CLOCK_TAI: 3249285ec4cSJason A. Donenfeld return ktime_get_clocktai_ns(); 325bc2b7dabSGregor Boirie default: 326bc2b7dabSGregor Boirie BUG(); 327bc2b7dabSGregor Boirie } 328bc2b7dabSGregor Boirie } 329bc2b7dabSGregor Boirie EXPORT_SYMBOL(iio_get_time_ns); 330bc2b7dabSGregor Boirie 331a980e046SJonathan Cameron static int __init iio_init(void) 332a980e046SJonathan Cameron { 333a980e046SJonathan Cameron int ret; 334a980e046SJonathan Cameron 335a980e046SJonathan Cameron /* Register sysfs bus */ 336a980e046SJonathan Cameron ret = bus_register(&iio_bus_type); 337a980e046SJonathan Cameron if (ret < 0) { 3383176dd5dSSachin Kamat pr_err("could not register bus type\n"); 339a980e046SJonathan Cameron goto error_nothing; 340a980e046SJonathan Cameron } 341a980e046SJonathan Cameron 342a980e046SJonathan Cameron ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio"); 343a980e046SJonathan Cameron if (ret < 0) { 3443176dd5dSSachin Kamat pr_err("failed to allocate char dev region\n"); 345a980e046SJonathan Cameron goto error_unregister_bus_type; 346a980e046SJonathan Cameron } 347a980e046SJonathan Cameron 348a980e046SJonathan Cameron iio_debugfs_dentry = debugfs_create_dir("iio", NULL); 349a980e046SJonathan Cameron 350a980e046SJonathan Cameron return 0; 351a980e046SJonathan Cameron 352a980e046SJonathan Cameron error_unregister_bus_type: 353a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 354a980e046SJonathan Cameron error_nothing: 355a980e046SJonathan Cameron return ret; 356a980e046SJonathan Cameron } 357a980e046SJonathan Cameron 358a980e046SJonathan Cameron static void __exit iio_exit(void) 359a980e046SJonathan Cameron { 360a980e046SJonathan Cameron if (iio_devt) 361a980e046SJonathan Cameron unregister_chrdev_region(iio_devt, IIO_DEV_MAX); 362a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 363a980e046SJonathan Cameron debugfs_remove(iio_debugfs_dentry); 364a980e046SJonathan Cameron } 365a980e046SJonathan Cameron 366a980e046SJonathan Cameron #if defined(CONFIG_DEBUG_FS) 367a980e046SJonathan Cameron static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf, 368a980e046SJonathan Cameron size_t count, loff_t *ppos) 369a980e046SJonathan Cameron { 370a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 37196fb1b67SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 372abeba59fSJoe Simmons-Talbott unsigned int val = 0; 373a980e046SJonathan Cameron int ret; 374a980e046SJonathan Cameron 3752ddc982aSAlexandru Tachici if (*ppos > 0) 3762ddc982aSAlexandru Tachici return simple_read_from_buffer(userbuf, count, ppos, 37796fb1b67SAlexandru Ardelean iio_dev_opaque->read_buf, 37896fb1b67SAlexandru Ardelean iio_dev_opaque->read_buf_len); 3792ddc982aSAlexandru Tachici 380a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, 38196fb1b67SAlexandru Ardelean iio_dev_opaque->cached_reg_addr, 382a980e046SJonathan Cameron 0, &val); 3833d62c78aSMatt Fornero if (ret) { 384a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__); 3853d62c78aSMatt Fornero return ret; 3863d62c78aSMatt Fornero } 387a980e046SJonathan Cameron 38896fb1b67SAlexandru Ardelean iio_dev_opaque->read_buf_len = snprintf(iio_dev_opaque->read_buf, 38996fb1b67SAlexandru Ardelean sizeof(iio_dev_opaque->read_buf), 3902ddc982aSAlexandru Tachici "0x%X\n", val); 391a980e046SJonathan Cameron 3922ddc982aSAlexandru Tachici return simple_read_from_buffer(userbuf, count, ppos, 39396fb1b67SAlexandru Ardelean iio_dev_opaque->read_buf, 39496fb1b67SAlexandru Ardelean iio_dev_opaque->read_buf_len); 395a980e046SJonathan Cameron } 396a980e046SJonathan Cameron 397a980e046SJonathan Cameron static ssize_t iio_debugfs_write_reg(struct file *file, 398a980e046SJonathan Cameron const char __user *userbuf, size_t count, loff_t *ppos) 399a980e046SJonathan Cameron { 400a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 40196fb1b67SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 402abeba59fSJoe Simmons-Talbott unsigned int reg, val; 403a980e046SJonathan Cameron char buf[80]; 404a980e046SJonathan Cameron int ret; 405a980e046SJonathan Cameron 406cb1d1753SAndy Shevchenko count = min(count, sizeof(buf) - 1); 407a980e046SJonathan Cameron if (copy_from_user(buf, userbuf, count)) 408a980e046SJonathan Cameron return -EFAULT; 409a980e046SJonathan Cameron 410a980e046SJonathan Cameron buf[count] = 0; 411a980e046SJonathan Cameron 412a980e046SJonathan Cameron ret = sscanf(buf, "%i %i", ®, &val); 413a980e046SJonathan Cameron 414a980e046SJonathan Cameron switch (ret) { 415a980e046SJonathan Cameron case 1: 41696fb1b67SAlexandru Ardelean iio_dev_opaque->cached_reg_addr = reg; 417a980e046SJonathan Cameron break; 418a980e046SJonathan Cameron case 2: 41996fb1b67SAlexandru Ardelean iio_dev_opaque->cached_reg_addr = reg; 420a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, reg, 421a980e046SJonathan Cameron val, NULL); 422a980e046SJonathan Cameron if (ret) { 423a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: write failed\n", 424a980e046SJonathan Cameron __func__); 425a980e046SJonathan Cameron return ret; 426a980e046SJonathan Cameron } 427a980e046SJonathan Cameron break; 428a980e046SJonathan Cameron default: 429a980e046SJonathan Cameron return -EINVAL; 430a980e046SJonathan Cameron } 431a980e046SJonathan Cameron 432a980e046SJonathan Cameron return count; 433a980e046SJonathan Cameron } 434a980e046SJonathan Cameron 435a980e046SJonathan Cameron static const struct file_operations iio_debugfs_reg_fops = { 4365a28c873SAxel Lin .open = simple_open, 437a980e046SJonathan Cameron .read = iio_debugfs_read_reg, 438a980e046SJonathan Cameron .write = iio_debugfs_write_reg, 439a980e046SJonathan Cameron }; 440a980e046SJonathan Cameron 441a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 442a980e046SJonathan Cameron { 44396fb1b67SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 4449d9ec8d0SJoe Simmons-Talbott 44596fb1b67SAlexandru Ardelean debugfs_remove_recursive(iio_dev_opaque->debugfs_dentry); 446a980e046SJonathan Cameron } 447a980e046SJonathan Cameron 4488915aacaSGreg Kroah-Hartman static void iio_device_register_debugfs(struct iio_dev *indio_dev) 449a980e046SJonathan Cameron { 45096fb1b67SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque; 45196fb1b67SAlexandru Ardelean 452a980e046SJonathan Cameron if (indio_dev->info->debugfs_reg_access == NULL) 4538915aacaSGreg Kroah-Hartman return; 454a980e046SJonathan Cameron 455abd5a2fbSAxel Lin if (!iio_debugfs_dentry) 4568915aacaSGreg Kroah-Hartman return; 457a980e046SJonathan Cameron 45896fb1b67SAlexandru Ardelean iio_dev_opaque = to_iio_dev_opaque(indio_dev); 45996fb1b67SAlexandru Ardelean 46096fb1b67SAlexandru Ardelean iio_dev_opaque->debugfs_dentry = 461a980e046SJonathan Cameron debugfs_create_dir(dev_name(&indio_dev->dev), 462a980e046SJonathan Cameron iio_debugfs_dentry); 463a980e046SJonathan Cameron 4648915aacaSGreg Kroah-Hartman debugfs_create_file("direct_reg_access", 0644, 46596fb1b67SAlexandru Ardelean iio_dev_opaque->debugfs_dentry, indio_dev, 4668915aacaSGreg Kroah-Hartman &iio_debugfs_reg_fops); 467a980e046SJonathan Cameron } 468a980e046SJonathan Cameron #else 4698915aacaSGreg Kroah-Hartman static void iio_device_register_debugfs(struct iio_dev *indio_dev) 470a980e046SJonathan Cameron { 471a980e046SJonathan Cameron } 472a980e046SJonathan Cameron 473a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 474a980e046SJonathan Cameron { 475a980e046SJonathan Cameron } 476a980e046SJonathan Cameron #endif /* CONFIG_DEBUG_FS */ 477a980e046SJonathan Cameron 478a980e046SJonathan Cameron static ssize_t iio_read_channel_ext_info(struct device *dev, 479a980e046SJonathan Cameron struct device_attribute *attr, 480a980e046SJonathan Cameron char *buf) 481a980e046SJonathan Cameron { 482e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 483a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 484a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 485a980e046SJonathan Cameron 486a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 487a980e046SJonathan Cameron 488fc6d1139SMichael Hennerich return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf); 489a980e046SJonathan Cameron } 490a980e046SJonathan Cameron 491a980e046SJonathan Cameron static ssize_t iio_write_channel_ext_info(struct device *dev, 492a980e046SJonathan Cameron struct device_attribute *attr, 493b662f4baSAndy Shevchenko const char *buf, size_t len) 494a980e046SJonathan Cameron { 495e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 496a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 497a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 498a980e046SJonathan Cameron 499a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 500a980e046SJonathan Cameron 501fc6d1139SMichael Hennerich return ext_info->write(indio_dev, ext_info->private, 502fc6d1139SMichael Hennerich this_attr->c, buf, len); 503a980e046SJonathan Cameron } 504a980e046SJonathan Cameron 5055212cc8aSLars-Peter Clausen ssize_t iio_enum_available_read(struct iio_dev *indio_dev, 5065212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 5075212cc8aSLars-Peter Clausen { 5085212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 5095212cc8aSLars-Peter Clausen unsigned int i; 5105212cc8aSLars-Peter Clausen size_t len = 0; 5115212cc8aSLars-Peter Clausen 5125212cc8aSLars-Peter Clausen if (!e->num_items) 5135212cc8aSLars-Peter Clausen return 0; 5145212cc8aSLars-Peter Clausen 515d9a0e73cSLars-Peter Clausen for (i = 0; i < e->num_items; ++i) { 516d9a0e73cSLars-Peter Clausen if (!e->items[i]) 517d9a0e73cSLars-Peter Clausen continue; 5180207483bSLars-Peter Clausen len += sysfs_emit_at(buf, len, "%s ", e->items[i]); 519d9a0e73cSLars-Peter Clausen } 5205212cc8aSLars-Peter Clausen 5215212cc8aSLars-Peter Clausen /* replace last space with a newline */ 5225212cc8aSLars-Peter Clausen buf[len - 1] = '\n'; 5235212cc8aSLars-Peter Clausen 5245212cc8aSLars-Peter Clausen return len; 5255212cc8aSLars-Peter Clausen } 5265212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_available_read); 5275212cc8aSLars-Peter Clausen 5285212cc8aSLars-Peter Clausen ssize_t iio_enum_read(struct iio_dev *indio_dev, 5295212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 5305212cc8aSLars-Peter Clausen { 5315212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 5325212cc8aSLars-Peter Clausen int i; 5335212cc8aSLars-Peter Clausen 5345212cc8aSLars-Peter Clausen if (!e->get) 5355212cc8aSLars-Peter Clausen return -EINVAL; 5365212cc8aSLars-Peter Clausen 5375212cc8aSLars-Peter Clausen i = e->get(indio_dev, chan); 5385212cc8aSLars-Peter Clausen if (i < 0) 5395212cc8aSLars-Peter Clausen return i; 5401702df5dSAndy Shevchenko if (i >= e->num_items || !e->items[i]) 5415212cc8aSLars-Peter Clausen return -EINVAL; 5425212cc8aSLars-Peter Clausen 54383ca56b6SLars-Peter Clausen return sysfs_emit(buf, "%s\n", e->items[i]); 5445212cc8aSLars-Peter Clausen } 5455212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_read); 5465212cc8aSLars-Peter Clausen 5475212cc8aSLars-Peter Clausen ssize_t iio_enum_write(struct iio_dev *indio_dev, 5485212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, const char *buf, 5495212cc8aSLars-Peter Clausen size_t len) 5505212cc8aSLars-Peter Clausen { 5515212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 5525212cc8aSLars-Peter Clausen int ret; 5535212cc8aSLars-Peter Clausen 5545212cc8aSLars-Peter Clausen if (!e->set) 5555212cc8aSLars-Peter Clausen return -EINVAL; 5565212cc8aSLars-Peter Clausen 557122fb873SAndy Shevchenko ret = __sysfs_match_string(e->items, e->num_items, buf); 55802e9a0ffSAndy Shevchenko if (ret < 0) 55902e9a0ffSAndy Shevchenko return ret; 5605212cc8aSLars-Peter Clausen 56102e9a0ffSAndy Shevchenko ret = e->set(indio_dev, chan, ret); 5625212cc8aSLars-Peter Clausen return ret ? ret : len; 5635212cc8aSLars-Peter Clausen } 5645212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_write); 5655212cc8aSLars-Peter Clausen 566dfc57732SGregor Boirie static const struct iio_mount_matrix iio_mount_idmatrix = { 567dfc57732SGregor Boirie .rotation = { 568dfc57732SGregor Boirie "1", "0", "0", 569dfc57732SGregor Boirie "0", "1", "0", 570dfc57732SGregor Boirie "0", "0", "1" 571dfc57732SGregor Boirie } 572dfc57732SGregor Boirie }; 573dfc57732SGregor Boirie 574dfc57732SGregor Boirie static int iio_setup_mount_idmatrix(const struct device *dev, 575dfc57732SGregor Boirie struct iio_mount_matrix *matrix) 576dfc57732SGregor Boirie { 577dfc57732SGregor Boirie *matrix = iio_mount_idmatrix; 578dfc57732SGregor Boirie dev_info(dev, "mounting matrix not found: using identity...\n"); 579dfc57732SGregor Boirie return 0; 580dfc57732SGregor Boirie } 581dfc57732SGregor Boirie 582dfc57732SGregor Boirie ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv, 583dfc57732SGregor Boirie const struct iio_chan_spec *chan, char *buf) 584dfc57732SGregor Boirie { 585b662f4baSAndy Shevchenko const struct iio_mount_matrix *mtx; 586dfc57732SGregor Boirie 587b662f4baSAndy Shevchenko mtx = ((iio_get_mount_matrix_t *)priv)(indio_dev, chan); 588dfc57732SGregor Boirie if (IS_ERR(mtx)) 589dfc57732SGregor Boirie return PTR_ERR(mtx); 590dfc57732SGregor Boirie 591dfc57732SGregor Boirie if (!mtx) 592dfc57732SGregor Boirie mtx = &iio_mount_idmatrix; 593dfc57732SGregor Boirie 59483ca56b6SLars-Peter Clausen return sysfs_emit(buf, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n", 595dfc57732SGregor Boirie mtx->rotation[0], mtx->rotation[1], mtx->rotation[2], 596dfc57732SGregor Boirie mtx->rotation[3], mtx->rotation[4], mtx->rotation[5], 597dfc57732SGregor Boirie mtx->rotation[6], mtx->rotation[7], mtx->rotation[8]); 598dfc57732SGregor Boirie } 599dfc57732SGregor Boirie EXPORT_SYMBOL_GPL(iio_show_mount_matrix); 600dfc57732SGregor Boirie 601dfc57732SGregor Boirie /** 602fb158971SAndy Shevchenko * iio_read_mount_matrix() - retrieve iio device mounting matrix from 603fb158971SAndy Shevchenko * device "mount-matrix" property 604dfc57732SGregor Boirie * @dev: device the mounting matrix property is assigned to 605dfc57732SGregor Boirie * @matrix: where to store retrieved matrix 606dfc57732SGregor Boirie * 607dfc57732SGregor Boirie * If device is assigned no mounting matrix property, a default 3x3 identity 608dfc57732SGregor Boirie * matrix will be filled in. 609dfc57732SGregor Boirie * 610*65659a8dSAndy Shevchenko * Returns: 0 if success, or a negative error code on failure. 611dfc57732SGregor Boirie */ 612b892770aSAndy Shevchenko int iio_read_mount_matrix(struct device *dev, struct iio_mount_matrix *matrix) 613dfc57732SGregor Boirie { 614fb158971SAndy Shevchenko size_t len = ARRAY_SIZE(iio_mount_idmatrix.rotation); 615fb158971SAndy Shevchenko int err; 616dfc57732SGregor Boirie 617b892770aSAndy Shevchenko err = device_property_read_string_array(dev, "mount-matrix", matrix->rotation, len); 618fb158971SAndy Shevchenko if (err == len) 619dfc57732SGregor Boirie return 0; 620dfc57732SGregor Boirie 621dfc57732SGregor Boirie if (err >= 0) 622dfc57732SGregor Boirie /* Invalid number of matrix entries. */ 623dfc57732SGregor Boirie return -EINVAL; 624dfc57732SGregor Boirie 625dfc57732SGregor Boirie if (err != -EINVAL) 626dfc57732SGregor Boirie /* Invalid matrix declaration format. */ 627dfc57732SGregor Boirie return err; 628dfc57732SGregor Boirie 629dfc57732SGregor Boirie /* Matrix was not declared at all: fallback to identity. */ 630dfc57732SGregor Boirie return iio_setup_mount_idmatrix(dev, matrix); 631dfc57732SGregor Boirie } 632fb158971SAndy Shevchenko EXPORT_SYMBOL(iio_read_mount_matrix); 633dfc57732SGregor Boirie 6346b92ba0aSLars-Peter Clausen static ssize_t __iio_format_value(char *buf, size_t offset, unsigned int type, 63551239600SJonathan Cameron int size, const int *vals) 63651239600SJonathan Cameron { 63751239600SJonathan Cameron int tmp0, tmp1; 6380e7a3978SAnand Ashok Dumbre s64 tmp2; 63951239600SJonathan Cameron bool scale_db = false; 64051239600SJonathan Cameron 64151239600SJonathan Cameron switch (type) { 64251239600SJonathan Cameron case IIO_VAL_INT: 6436b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "%d", vals[0]); 64451239600SJonathan Cameron case IIO_VAL_INT_PLUS_MICRO_DB: 64551239600SJonathan Cameron scale_db = true; 646df561f66SGustavo A. R. Silva fallthrough; 64751239600SJonathan Cameron case IIO_VAL_INT_PLUS_MICRO: 64851239600SJonathan Cameron if (vals[1] < 0) 6496b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "-%d.%06u%s", 6506b92ba0aSLars-Peter Clausen abs(vals[0]), -vals[1], 65151239600SJonathan Cameron scale_db ? " dB" : ""); 6526b92ba0aSLars-Peter Clausen else 6536b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "%d.%06u%s", vals[0], 6546b92ba0aSLars-Peter Clausen vals[1], scale_db ? " dB" : ""); 65551239600SJonathan Cameron case IIO_VAL_INT_PLUS_NANO: 65651239600SJonathan Cameron if (vals[1] < 0) 6576b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "-%d.%09u", 6586b92ba0aSLars-Peter Clausen abs(vals[0]), -vals[1]); 65951239600SJonathan Cameron else 6606b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "%d.%09u", vals[0], 6616b92ba0aSLars-Peter Clausen vals[1]); 66251239600SJonathan Cameron case IIO_VAL_FRACTIONAL: 6630e7a3978SAnand Ashok Dumbre tmp2 = div_s64((s64)vals[0] * 1000000000LL, vals[1]); 66451239600SJonathan Cameron tmp1 = vals[1]; 6650e7a3978SAnand Ashok Dumbre tmp0 = (int)div_s64_rem(tmp2, 1000000000, &tmp1); 6660e7a3978SAnand Ashok Dumbre if ((tmp2 < 0) && (tmp0 == 0)) 6676b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "-0.%09u", abs(tmp1)); 6680e7a3978SAnand Ashok Dumbre else 6696b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "%d.%09u", tmp0, 6706b92ba0aSLars-Peter Clausen abs(tmp1)); 67151239600SJonathan Cameron case IIO_VAL_FRACTIONAL_LOG2: 6722646a95dSLars-Peter Clausen tmp2 = shift_right((s64)vals[0] * 1000000000LL, vals[1]); 6732646a95dSLars-Peter Clausen tmp0 = (int)div_s64_rem(tmp2, 1000000000LL, &tmp1); 67438a52cdeSLars-Peter Clausen if (tmp0 == 0 && tmp2 < 0) 6756b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "-0.%09u", abs(tmp1)); 67638a52cdeSLars-Peter Clausen else 6776b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "%d.%09u", tmp0, 6786b92ba0aSLars-Peter Clausen abs(tmp1)); 67951239600SJonathan Cameron case IIO_VAL_INT_MULTIPLE: 68051239600SJonathan Cameron { 68151239600SJonathan Cameron int i; 68251239600SJonathan Cameron int l = 0; 68351239600SJonathan Cameron 6846b92ba0aSLars-Peter Clausen for (i = 0; i < size; ++i) 6856b92ba0aSLars-Peter Clausen l += sysfs_emit_at(buf, offset + l, "%d ", vals[i]); 68651239600SJonathan Cameron return l; 68751239600SJonathan Cameron } 6888cb34036SAndrea Merello case IIO_VAL_CHAR: 6896b92ba0aSLars-Peter Clausen return sysfs_emit_at(buf, offset, "%c", (char)vals[0]); 6906bb835f3SAndriy Tryshnivskyy case IIO_VAL_INT_64: 6916bb835f3SAndriy Tryshnivskyy tmp2 = (s64)((((u64)vals[1]) << 32) | (u32)vals[0]); 6926bb835f3SAndriy Tryshnivskyy return sysfs_emit_at(buf, offset, "%lld", tmp2); 69351239600SJonathan Cameron default: 69451239600SJonathan Cameron return 0; 69551239600SJonathan Cameron } 69651239600SJonathan Cameron } 69751239600SJonathan Cameron 6983661f3f5SLars-Peter Clausen /** 6993661f3f5SLars-Peter Clausen * iio_format_value() - Formats a IIO value into its string representation 7002498dcf6SCristina Opriceana * @buf: The buffer to which the formatted value gets written 70151239600SJonathan Cameron * which is assumed to be big enough (i.e. PAGE_SIZE). 702c175cb7cSRandy Dunlap * @type: One of the IIO_VAL_* constants. This decides how the val 7032498dcf6SCristina Opriceana * and val2 parameters are formatted. 7042498dcf6SCristina Opriceana * @size: Number of IIO value entries contained in vals 7052498dcf6SCristina Opriceana * @vals: Pointer to the values, exact meaning depends on the 7062498dcf6SCristina Opriceana * type parameter. 7072498dcf6SCristina Opriceana * 708*65659a8dSAndy Shevchenko * Returns: 709*65659a8dSAndy Shevchenko * 0 by default, a negative number on failure or the total number of characters 710*65659a8dSAndy Shevchenko * written for a type that belongs to the IIO_VAL_* constant. 7113661f3f5SLars-Peter Clausen */ 7129fbfb4b3SSrinivas Pandruvada ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals) 713a980e046SJonathan Cameron { 71451239600SJonathan Cameron ssize_t len; 715a980e046SJonathan Cameron 7166b92ba0aSLars-Peter Clausen len = __iio_format_value(buf, 0, type, size, vals); 71751239600SJonathan Cameron if (len >= PAGE_SIZE - 1) 71851239600SJonathan Cameron return -EFBIG; 7199fbfb4b3SSrinivas Pandruvada 7206b92ba0aSLars-Peter Clausen return len + sysfs_emit_at(buf, len, "\n"); 72167eedba3SMichael Hennerich } 7227d2c2acaSAndrew F. Davis EXPORT_SYMBOL_GPL(iio_format_value); 723a980e046SJonathan Cameron 7241d4ef9b3SCristian Pop static ssize_t iio_read_channel_label(struct device *dev, 7251d4ef9b3SCristian Pop struct device_attribute *attr, 7261d4ef9b3SCristian Pop char *buf) 7271d4ef9b3SCristian Pop { 7281d4ef9b3SCristian Pop struct iio_dev *indio_dev = dev_to_iio_dev(dev); 7291d4ef9b3SCristian Pop struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 7301d4ef9b3SCristian Pop 73113efdc3dSPaul Cercueil if (indio_dev->info->read_label) 7321d4ef9b3SCristian Pop return indio_dev->info->read_label(indio_dev, this_attr->c, buf); 73313efdc3dSPaul Cercueil 73413efdc3dSPaul Cercueil if (this_attr->c->extend_name) 735c3154defSLars-Peter Clausen return sysfs_emit(buf, "%s\n", this_attr->c->extend_name); 73613efdc3dSPaul Cercueil 73713efdc3dSPaul Cercueil return -EINVAL; 7381d4ef9b3SCristian Pop } 7391d4ef9b3SCristian Pop 7403661f3f5SLars-Peter Clausen static ssize_t iio_read_channel_info(struct device *dev, 7413661f3f5SLars-Peter Clausen struct device_attribute *attr, 7423661f3f5SLars-Peter Clausen char *buf) 7433661f3f5SLars-Peter Clausen { 7443661f3f5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 7453661f3f5SLars-Peter Clausen struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 7469fbfb4b3SSrinivas Pandruvada int vals[INDIO_MAX_RAW_ELEMENTS]; 7479fbfb4b3SSrinivas Pandruvada int ret; 7489fbfb4b3SSrinivas Pandruvada int val_len = 2; 7499fbfb4b3SSrinivas Pandruvada 7509fbfb4b3SSrinivas Pandruvada if (indio_dev->info->read_raw_multi) 7519fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c, 7529fbfb4b3SSrinivas Pandruvada INDIO_MAX_RAW_ELEMENTS, 7539fbfb4b3SSrinivas Pandruvada vals, &val_len, 7549fbfb4b3SSrinivas Pandruvada this_attr->address); 7559fbfb4b3SSrinivas Pandruvada else 7569fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw(indio_dev, this_attr->c, 7579fbfb4b3SSrinivas Pandruvada &vals[0], &vals[1], this_attr->address); 7583661f3f5SLars-Peter Clausen 7593661f3f5SLars-Peter Clausen if (ret < 0) 7603661f3f5SLars-Peter Clausen return ret; 7613661f3f5SLars-Peter Clausen 7629fbfb4b3SSrinivas Pandruvada return iio_format_value(buf, ret, val_len, vals); 7633661f3f5SLars-Peter Clausen } 7643661f3f5SLars-Peter Clausen 765eda20ba1SLars-Peter Clausen static ssize_t iio_format_list(char *buf, const int *vals, int type, int length, 766eda20ba1SLars-Peter Clausen const char *prefix, const char *suffix) 76751239600SJonathan Cameron { 768eda20ba1SLars-Peter Clausen ssize_t len; 769e08b60d3SLars-Peter Clausen int stride; 77051239600SJonathan Cameron int i; 771eda20ba1SLars-Peter Clausen 77251239600SJonathan Cameron switch (type) { 77351239600SJonathan Cameron case IIO_VAL_INT: 774e08b60d3SLars-Peter Clausen stride = 1; 77551239600SJonathan Cameron break; 77651239600SJonathan Cameron default: 777e08b60d3SLars-Peter Clausen stride = 2; 778e08b60d3SLars-Peter Clausen break; 779e08b60d3SLars-Peter Clausen } 780e08b60d3SLars-Peter Clausen 7816b92ba0aSLars-Peter Clausen len = sysfs_emit(buf, prefix); 782e08b60d3SLars-Peter Clausen 783e08b60d3SLars-Peter Clausen for (i = 0; i <= length - stride; i += stride) { 784e08b60d3SLars-Peter Clausen if (i != 0) { 7856b92ba0aSLars-Peter Clausen len += sysfs_emit_at(buf, len, " "); 78651239600SJonathan Cameron if (len >= PAGE_SIZE) 78751239600SJonathan Cameron return -EFBIG; 78851239600SJonathan Cameron } 789e08b60d3SLars-Peter Clausen 7906b92ba0aSLars-Peter Clausen len += __iio_format_value(buf, len, type, stride, &vals[i]); 791e08b60d3SLars-Peter Clausen if (len >= PAGE_SIZE) 792e08b60d3SLars-Peter Clausen return -EFBIG; 79351239600SJonathan Cameron } 79451239600SJonathan Cameron 7956b92ba0aSLars-Peter Clausen len += sysfs_emit_at(buf, len, "%s\n", suffix); 796e08b60d3SLars-Peter Clausen 79751239600SJonathan Cameron return len; 79851239600SJonathan Cameron } 79951239600SJonathan Cameron 800eda20ba1SLars-Peter Clausen static ssize_t iio_format_avail_list(char *buf, const int *vals, 801eda20ba1SLars-Peter Clausen int type, int length) 802eda20ba1SLars-Peter Clausen { 803eda20ba1SLars-Peter Clausen 804eda20ba1SLars-Peter Clausen return iio_format_list(buf, vals, type, length, "", ""); 805eda20ba1SLars-Peter Clausen } 806eda20ba1SLars-Peter Clausen 80751239600SJonathan Cameron static ssize_t iio_format_avail_range(char *buf, const int *vals, int type) 80851239600SJonathan Cameron { 8095e1f9185SFawzi Khaber int length; 8105e1f9185SFawzi Khaber 8115e1f9185SFawzi Khaber /* 8125e1f9185SFawzi Khaber * length refers to the array size , not the number of elements. 8135e1f9185SFawzi Khaber * The purpose is to print the range [min , step ,max] so length should 8145e1f9185SFawzi Khaber * be 3 in case of int, and 6 for other types. 8155e1f9185SFawzi Khaber */ 8165e1f9185SFawzi Khaber switch (type) { 8175e1f9185SFawzi Khaber case IIO_VAL_INT: 8185e1f9185SFawzi Khaber length = 3; 8195e1f9185SFawzi Khaber break; 8205e1f9185SFawzi Khaber default: 8215e1f9185SFawzi Khaber length = 6; 8225e1f9185SFawzi Khaber break; 8235e1f9185SFawzi Khaber } 8245e1f9185SFawzi Khaber 8255e1f9185SFawzi Khaber return iio_format_list(buf, vals, type, length, "[", "]"); 82651239600SJonathan Cameron } 82751239600SJonathan Cameron 82851239600SJonathan Cameron static ssize_t iio_read_channel_info_avail(struct device *dev, 82951239600SJonathan Cameron struct device_attribute *attr, 83051239600SJonathan Cameron char *buf) 83151239600SJonathan Cameron { 83251239600SJonathan Cameron struct iio_dev *indio_dev = dev_to_iio_dev(dev); 83351239600SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 83451239600SJonathan Cameron const int *vals; 83551239600SJonathan Cameron int ret; 83651239600SJonathan Cameron int length; 83751239600SJonathan Cameron int type; 83851239600SJonathan Cameron 83951239600SJonathan Cameron ret = indio_dev->info->read_avail(indio_dev, this_attr->c, 84051239600SJonathan Cameron &vals, &type, &length, 84151239600SJonathan Cameron this_attr->address); 84251239600SJonathan Cameron 84351239600SJonathan Cameron if (ret < 0) 84451239600SJonathan Cameron return ret; 84551239600SJonathan Cameron switch (ret) { 84651239600SJonathan Cameron case IIO_AVAIL_LIST: 84751239600SJonathan Cameron return iio_format_avail_list(buf, vals, type, length); 84851239600SJonathan Cameron case IIO_AVAIL_RANGE: 84951239600SJonathan Cameron return iio_format_avail_range(buf, vals, type); 85051239600SJonathan Cameron default: 85151239600SJonathan Cameron return -EINVAL; 85251239600SJonathan Cameron } 85351239600SJonathan Cameron } 85451239600SJonathan Cameron 8556807d721SLars-Peter Clausen /** 856b8528224SBeniamin Bia * __iio_str_to_fixpoint() - Parse a fixed-point number from a string 8576807d721SLars-Peter Clausen * @str: The string to parse 8586807d721SLars-Peter Clausen * @fract_mult: Multiplier for the first decimal place, should be a power of 10 8596807d721SLars-Peter Clausen * @integer: The integer part of the number 8606807d721SLars-Peter Clausen * @fract: The fractional part of the number 861b8528224SBeniamin Bia * @scale_db: True if this should parse as dB 8626807d721SLars-Peter Clausen * 863*65659a8dSAndy Shevchenko * Returns: 864*65659a8dSAndy Shevchenko * 0 on success, or a negative error code if the string could not be parsed. 8656807d721SLars-Peter Clausen */ 866b8528224SBeniamin Bia static int __iio_str_to_fixpoint(const char *str, int fract_mult, 867b8528224SBeniamin Bia int *integer, int *fract, bool scale_db) 8686807d721SLars-Peter Clausen { 8696807d721SLars-Peter Clausen int i = 0, f = 0; 8706807d721SLars-Peter Clausen bool integer_part = true, negative = false; 8716807d721SLars-Peter Clausen 872f47dff32SSean Nyekjaer if (fract_mult == 0) { 873f47dff32SSean Nyekjaer *fract = 0; 874f47dff32SSean Nyekjaer 875f47dff32SSean Nyekjaer return kstrtoint(str, 0, integer); 876f47dff32SSean Nyekjaer } 877f47dff32SSean Nyekjaer 8786807d721SLars-Peter Clausen if (str[0] == '-') { 8796807d721SLars-Peter Clausen negative = true; 8806807d721SLars-Peter Clausen str++; 8816807d721SLars-Peter Clausen } else if (str[0] == '+') { 8826807d721SLars-Peter Clausen str++; 8836807d721SLars-Peter Clausen } 8846807d721SLars-Peter Clausen 8856807d721SLars-Peter Clausen while (*str) { 8866807d721SLars-Peter Clausen if ('0' <= *str && *str <= '9') { 8876807d721SLars-Peter Clausen if (integer_part) { 8886807d721SLars-Peter Clausen i = i * 10 + *str - '0'; 8896807d721SLars-Peter Clausen } else { 8906807d721SLars-Peter Clausen f += fract_mult * (*str - '0'); 8916807d721SLars-Peter Clausen fract_mult /= 10; 8926807d721SLars-Peter Clausen } 8936807d721SLars-Peter Clausen } else if (*str == '\n') { 8946807d721SLars-Peter Clausen if (*(str + 1) == '\0') 8956807d721SLars-Peter Clausen break; 8966807d721SLars-Peter Clausen return -EINVAL; 897b8528224SBeniamin Bia } else if (!strncmp(str, " dB", sizeof(" dB") - 1) && scale_db) { 898b8528224SBeniamin Bia /* Ignore the dB suffix */ 899b8528224SBeniamin Bia str += sizeof(" dB") - 1; 900b8528224SBeniamin Bia continue; 901b8528224SBeniamin Bia } else if (!strncmp(str, "dB", sizeof("dB") - 1) && scale_db) { 902b8528224SBeniamin Bia /* Ignore the dB suffix */ 903b8528224SBeniamin Bia str += sizeof("dB") - 1; 904b8528224SBeniamin Bia continue; 9056807d721SLars-Peter Clausen } else if (*str == '.' && integer_part) { 9066807d721SLars-Peter Clausen integer_part = false; 9076807d721SLars-Peter Clausen } else { 9086807d721SLars-Peter Clausen return -EINVAL; 9096807d721SLars-Peter Clausen } 9106807d721SLars-Peter Clausen str++; 9116807d721SLars-Peter Clausen } 9126807d721SLars-Peter Clausen 9136807d721SLars-Peter Clausen if (negative) { 9146807d721SLars-Peter Clausen if (i) 9156807d721SLars-Peter Clausen i = -i; 9166807d721SLars-Peter Clausen else 9176807d721SLars-Peter Clausen f = -f; 9186807d721SLars-Peter Clausen } 9196807d721SLars-Peter Clausen 9206807d721SLars-Peter Clausen *integer = i; 9216807d721SLars-Peter Clausen *fract = f; 9226807d721SLars-Peter Clausen 9236807d721SLars-Peter Clausen return 0; 9246807d721SLars-Peter Clausen } 925b8528224SBeniamin Bia 926b8528224SBeniamin Bia /** 927b8528224SBeniamin Bia * iio_str_to_fixpoint() - Parse a fixed-point number from a string 928b8528224SBeniamin Bia * @str: The string to parse 929b8528224SBeniamin Bia * @fract_mult: Multiplier for the first decimal place, should be a power of 10 930b8528224SBeniamin Bia * @integer: The integer part of the number 931b8528224SBeniamin Bia * @fract: The fractional part of the number 932b8528224SBeniamin Bia * 933*65659a8dSAndy Shevchenko * Returns: 934*65659a8dSAndy Shevchenko * 0 on success, or a negative error code if the string could not be parsed. 935b8528224SBeniamin Bia */ 936b8528224SBeniamin Bia int iio_str_to_fixpoint(const char *str, int fract_mult, 937b8528224SBeniamin Bia int *integer, int *fract) 938b8528224SBeniamin Bia { 939b8528224SBeniamin Bia return __iio_str_to_fixpoint(str, fract_mult, integer, fract, false); 940b8528224SBeniamin Bia } 9416807d721SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_str_to_fixpoint); 9426807d721SLars-Peter Clausen 943a980e046SJonathan Cameron static ssize_t iio_write_channel_info(struct device *dev, 944a980e046SJonathan Cameron struct device_attribute *attr, 945a980e046SJonathan Cameron const char *buf, 946a980e046SJonathan Cameron size_t len) 947a980e046SJonathan Cameron { 948e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 949a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 9506807d721SLars-Peter Clausen int ret, fract_mult = 100000; 9518cb34036SAndrea Merello int integer, fract = 0; 9528cb34036SAndrea Merello bool is_char = false; 953b8528224SBeniamin Bia bool scale_db = false; 954a980e046SJonathan Cameron 955a980e046SJonathan Cameron /* Assumes decimal - precision based on number of digits */ 956a980e046SJonathan Cameron if (!indio_dev->info->write_raw) 957a980e046SJonathan Cameron return -EINVAL; 958a980e046SJonathan Cameron 959a980e046SJonathan Cameron if (indio_dev->info->write_raw_get_fmt) 960a980e046SJonathan Cameron switch (indio_dev->info->write_raw_get_fmt(indio_dev, 961a980e046SJonathan Cameron this_attr->c, this_attr->address)) { 962f47dff32SSean Nyekjaer case IIO_VAL_INT: 963f47dff32SSean Nyekjaer fract_mult = 0; 964f47dff32SSean Nyekjaer break; 965b8528224SBeniamin Bia case IIO_VAL_INT_PLUS_MICRO_DB: 966b8528224SBeniamin Bia scale_db = true; 967df561f66SGustavo A. R. Silva fallthrough; 968a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_MICRO: 969a980e046SJonathan Cameron fract_mult = 100000; 970a980e046SJonathan Cameron break; 971a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_NANO: 972a980e046SJonathan Cameron fract_mult = 100000000; 973a980e046SJonathan Cameron break; 9748cb34036SAndrea Merello case IIO_VAL_CHAR: 9758cb34036SAndrea Merello is_char = true; 9768cb34036SAndrea Merello break; 977a980e046SJonathan Cameron default: 978a980e046SJonathan Cameron return -EINVAL; 979a980e046SJonathan Cameron } 980a980e046SJonathan Cameron 9818cb34036SAndrea Merello if (is_char) { 9828cb34036SAndrea Merello char ch; 9838cb34036SAndrea Merello 9848cb34036SAndrea Merello if (sscanf(buf, "%c", &ch) != 1) 9858cb34036SAndrea Merello return -EINVAL; 9868cb34036SAndrea Merello integer = ch; 9878cb34036SAndrea Merello } else { 988b8528224SBeniamin Bia ret = __iio_str_to_fixpoint(buf, fract_mult, &integer, &fract, 989b8528224SBeniamin Bia scale_db); 990b8528224SBeniamin Bia if (ret) 991b8528224SBeniamin Bia return ret; 9925403f280SMircea Caprioru } 993a980e046SJonathan Cameron 994a980e046SJonathan Cameron ret = indio_dev->info->write_raw(indio_dev, this_attr->c, 995a980e046SJonathan Cameron integer, fract, this_attr->address); 996a980e046SJonathan Cameron if (ret) 997a980e046SJonathan Cameron return ret; 998a980e046SJonathan Cameron 999a980e046SJonathan Cameron return len; 1000a980e046SJonathan Cameron } 1001a980e046SJonathan Cameron 1002a980e046SJonathan Cameron static 1003a980e046SJonathan Cameron int __iio_device_attr_init(struct device_attribute *dev_attr, 1004a980e046SJonathan Cameron const char *postfix, 1005a980e046SJonathan Cameron struct iio_chan_spec const *chan, 1006a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 1007a980e046SJonathan Cameron struct device_attribute *attr, 1008a980e046SJonathan Cameron char *buf), 1009a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 1010a980e046SJonathan Cameron struct device_attribute *attr, 1011a980e046SJonathan Cameron const char *buf, 1012a980e046SJonathan Cameron size_t len), 10133704432fSJonathan Cameron enum iio_shared_by shared_by) 1014a980e046SJonathan Cameron { 10153704432fSJonathan Cameron int ret = 0; 10167bbcf7e1SLars-Peter Clausen char *name = NULL; 10173704432fSJonathan Cameron char *full_postfix; 10189d9ec8d0SJoe Simmons-Talbott 1019a980e046SJonathan Cameron sysfs_attr_init(&dev_attr->attr); 1020a980e046SJonathan Cameron 1021a980e046SJonathan Cameron /* Build up postfix of <extend_name>_<modifier>_postfix */ 10223704432fSJonathan Cameron if (chan->modified && (shared_by == IIO_SEPARATE)) { 1023a980e046SJonathan Cameron if (chan->extend_name) 1024a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", 1025b662f4baSAndy Shevchenko iio_modifier_names[chan->channel2], 1026a980e046SJonathan Cameron chan->extend_name, 1027a980e046SJonathan Cameron postfix); 1028a980e046SJonathan Cameron else 1029a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s", 1030b662f4baSAndy Shevchenko iio_modifier_names[chan->channel2], 1031a980e046SJonathan Cameron postfix); 1032a980e046SJonathan Cameron } else { 103377bfa8baSLars-Peter Clausen if (chan->extend_name == NULL || shared_by != IIO_SEPARATE) 1034a980e046SJonathan Cameron full_postfix = kstrdup(postfix, GFP_KERNEL); 1035a980e046SJonathan Cameron else 1036a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, 1037a980e046SJonathan Cameron "%s_%s", 1038a980e046SJonathan Cameron chan->extend_name, 1039a980e046SJonathan Cameron postfix); 1040a980e046SJonathan Cameron } 10413704432fSJonathan Cameron if (full_postfix == NULL) 10423704432fSJonathan Cameron return -ENOMEM; 1043a980e046SJonathan Cameron 1044a980e046SJonathan Cameron if (chan->differential) { /* Differential can not have modifier */ 10453704432fSJonathan Cameron switch (shared_by) { 1046c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 10477bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 1048c006ec83SJonathan Cameron break; 1049c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 10507bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 1051c006ec83SJonathan Cameron iio_direction[chan->output], 1052c006ec83SJonathan Cameron full_postfix); 1053c006ec83SJonathan Cameron break; 10543704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 10557bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s", 1056a980e046SJonathan Cameron iio_direction[chan->output], 1057a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1058a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1059a980e046SJonathan Cameron full_postfix); 10603704432fSJonathan Cameron break; 10613704432fSJonathan Cameron case IIO_SEPARATE: 10623704432fSJonathan Cameron if (!chan->indexed) { 1063231bfe53SDan Carpenter WARN(1, "Differential channels must be indexed\n"); 10643704432fSJonathan Cameron ret = -EINVAL; 10653704432fSJonathan Cameron goto error_free_full_postfix; 10663704432fSJonathan Cameron } 10677bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, 10683704432fSJonathan Cameron "%s_%s%d-%s%d_%s", 1069a980e046SJonathan Cameron iio_direction[chan->output], 1070a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1071a980e046SJonathan Cameron chan->channel, 1072a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1073a980e046SJonathan Cameron chan->channel2, 1074a980e046SJonathan Cameron full_postfix); 10753704432fSJonathan Cameron break; 1076a980e046SJonathan Cameron } 1077a980e046SJonathan Cameron } else { /* Single ended */ 10783704432fSJonathan Cameron switch (shared_by) { 1079c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 10807bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 1081c006ec83SJonathan Cameron break; 1082c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 10837bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 1084c006ec83SJonathan Cameron iio_direction[chan->output], 1085c006ec83SJonathan Cameron full_postfix); 1086c006ec83SJonathan Cameron break; 10873704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 10887bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 1089a980e046SJonathan Cameron iio_direction[chan->output], 1090a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1091a980e046SJonathan Cameron full_postfix); 10923704432fSJonathan Cameron break; 10933704432fSJonathan Cameron 10943704432fSJonathan Cameron case IIO_SEPARATE: 10953704432fSJonathan Cameron if (chan->indexed) 10967bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s%d_%s", 1097a980e046SJonathan Cameron iio_direction[chan->output], 1098a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1099a980e046SJonathan Cameron chan->channel, 1100a980e046SJonathan Cameron full_postfix); 1101a980e046SJonathan Cameron else 11027bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 1103a980e046SJonathan Cameron iio_direction[chan->output], 1104a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 1105a980e046SJonathan Cameron full_postfix); 11063704432fSJonathan Cameron break; 11073704432fSJonathan Cameron } 1108a980e046SJonathan Cameron } 11097bbcf7e1SLars-Peter Clausen if (name == NULL) { 1110a980e046SJonathan Cameron ret = -ENOMEM; 1111a980e046SJonathan Cameron goto error_free_full_postfix; 1112a980e046SJonathan Cameron } 11137bbcf7e1SLars-Peter Clausen dev_attr->attr.name = name; 1114a980e046SJonathan Cameron 1115a980e046SJonathan Cameron if (readfunc) { 11169cf0b618SJoe Simmons-Talbott dev_attr->attr.mode |= 0444; 1117a980e046SJonathan Cameron dev_attr->show = readfunc; 1118a980e046SJonathan Cameron } 1119a980e046SJonathan Cameron 1120a980e046SJonathan Cameron if (writefunc) { 11219cf0b618SJoe Simmons-Talbott dev_attr->attr.mode |= 0200; 1122a980e046SJonathan Cameron dev_attr->store = writefunc; 1123a980e046SJonathan Cameron } 11247bbcf7e1SLars-Peter Clausen 1125a980e046SJonathan Cameron error_free_full_postfix: 1126a980e046SJonathan Cameron kfree(full_postfix); 11273704432fSJonathan Cameron 1128a980e046SJonathan Cameron return ret; 1129a980e046SJonathan Cameron } 1130a980e046SJonathan Cameron 1131a980e046SJonathan Cameron static void __iio_device_attr_deinit(struct device_attribute *dev_attr) 1132a980e046SJonathan Cameron { 1133a980e046SJonathan Cameron kfree(dev_attr->attr.name); 1134a980e046SJonathan Cameron } 1135a980e046SJonathan Cameron 1136a980e046SJonathan Cameron int __iio_add_chan_devattr(const char *postfix, 1137a980e046SJonathan Cameron struct iio_chan_spec const *chan, 1138a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 1139a980e046SJonathan Cameron struct device_attribute *attr, 1140a980e046SJonathan Cameron char *buf), 1141a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 1142a980e046SJonathan Cameron struct device_attribute *attr, 1143a980e046SJonathan Cameron const char *buf, 1144a980e046SJonathan Cameron size_t len), 1145a980e046SJonathan Cameron u64 mask, 11463704432fSJonathan Cameron enum iio_shared_by shared_by, 1147a980e046SJonathan Cameron struct device *dev, 11483e3d11b2SAlexandru Ardelean struct iio_buffer *buffer, 1149a980e046SJonathan Cameron struct list_head *attr_list) 1150a980e046SJonathan Cameron { 1151a980e046SJonathan Cameron int ret; 1152a980e046SJonathan Cameron struct iio_dev_attr *iio_attr, *t; 1153a980e046SJonathan Cameron 1154670c1103SSachin Kamat iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL); 115592825ff9SHartmut Knaack if (iio_attr == NULL) 115692825ff9SHartmut Knaack return -ENOMEM; 1157a980e046SJonathan Cameron ret = __iio_device_attr_init(&iio_attr->dev_attr, 1158a980e046SJonathan Cameron postfix, chan, 11593704432fSJonathan Cameron readfunc, writefunc, shared_by); 1160a980e046SJonathan Cameron if (ret) 1161a980e046SJonathan Cameron goto error_iio_dev_attr_free; 1162a980e046SJonathan Cameron iio_attr->c = chan; 1163a980e046SJonathan Cameron iio_attr->address = mask; 11643e3d11b2SAlexandru Ardelean iio_attr->buffer = buffer; 1165a980e046SJonathan Cameron list_for_each_entry(t, attr_list, l) 1166a980e046SJonathan Cameron if (strcmp(t->dev_attr.attr.name, 1167a980e046SJonathan Cameron iio_attr->dev_attr.attr.name) == 0) { 11683704432fSJonathan Cameron if (shared_by == IIO_SEPARATE) 1169a980e046SJonathan Cameron dev_err(dev, "tried to double register : %s\n", 1170a980e046SJonathan Cameron t->dev_attr.attr.name); 1171a980e046SJonathan Cameron ret = -EBUSY; 1172a980e046SJonathan Cameron goto error_device_attr_deinit; 1173a980e046SJonathan Cameron } 1174a980e046SJonathan Cameron list_add(&iio_attr->l, attr_list); 1175a980e046SJonathan Cameron 1176a980e046SJonathan Cameron return 0; 1177a980e046SJonathan Cameron 1178a980e046SJonathan Cameron error_device_attr_deinit: 1179a980e046SJonathan Cameron __iio_device_attr_deinit(&iio_attr->dev_attr); 1180a980e046SJonathan Cameron error_iio_dev_attr_free: 1181a980e046SJonathan Cameron kfree(iio_attr); 1182a980e046SJonathan Cameron return ret; 1183a980e046SJonathan Cameron } 1184a980e046SJonathan Cameron 11851d4ef9b3SCristian Pop static int iio_device_add_channel_label(struct iio_dev *indio_dev, 11861d4ef9b3SCristian Pop struct iio_chan_spec const *chan) 11871d4ef9b3SCristian Pop { 11881d4ef9b3SCristian Pop struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 11891d4ef9b3SCristian Pop int ret; 11901d4ef9b3SCristian Pop 119113efdc3dSPaul Cercueil if (!indio_dev->info->read_label && !chan->extend_name) 11921d4ef9b3SCristian Pop return 0; 11931d4ef9b3SCristian Pop 11941d4ef9b3SCristian Pop ret = __iio_add_chan_devattr("label", 11951d4ef9b3SCristian Pop chan, 11961d4ef9b3SCristian Pop &iio_read_channel_label, 11971d4ef9b3SCristian Pop NULL, 11981d4ef9b3SCristian Pop 0, 11991d4ef9b3SCristian Pop IIO_SEPARATE, 12001d4ef9b3SCristian Pop &indio_dev->dev, 12013e3d11b2SAlexandru Ardelean NULL, 12021d4ef9b3SCristian Pop &iio_dev_opaque->channel_attr_list); 12031d4ef9b3SCristian Pop if (ret < 0) 12041d4ef9b3SCristian Pop return ret; 12051d4ef9b3SCristian Pop 12061d4ef9b3SCristian Pop return 1; 12071d4ef9b3SCristian Pop } 12081d4ef9b3SCristian Pop 12093704432fSJonathan Cameron static int iio_device_add_info_mask_type(struct iio_dev *indio_dev, 12103704432fSJonathan Cameron struct iio_chan_spec const *chan, 12113704432fSJonathan Cameron enum iio_shared_by shared_by, 12123704432fSJonathan Cameron const long *infomask) 12133704432fSJonathan Cameron { 1214207c2d27SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 12153704432fSJonathan Cameron int i, ret, attrcount = 0; 12163704432fSJonathan Cameron 12171016d567SOrson Zhai for_each_set_bit(i, infomask, sizeof(*infomask)*8) { 1218ef4b4856SJonathan Cameron if (i >= ARRAY_SIZE(iio_chan_info_postfix)) 1219ef4b4856SJonathan Cameron return -EINVAL; 12203704432fSJonathan Cameron ret = __iio_add_chan_devattr(iio_chan_info_postfix[i], 12213704432fSJonathan Cameron chan, 12223704432fSJonathan Cameron &iio_read_channel_info, 12233704432fSJonathan Cameron &iio_write_channel_info, 12243704432fSJonathan Cameron i, 12253704432fSJonathan Cameron shared_by, 12263704432fSJonathan Cameron &indio_dev->dev, 12273e3d11b2SAlexandru Ardelean NULL, 1228207c2d27SAlexandru Ardelean &iio_dev_opaque->channel_attr_list); 12293704432fSJonathan Cameron if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) 12303704432fSJonathan Cameron continue; 12311702df5dSAndy Shevchenko if (ret < 0) 12323704432fSJonathan Cameron return ret; 12333704432fSJonathan Cameron attrcount++; 12343704432fSJonathan Cameron } 12353704432fSJonathan Cameron 12363704432fSJonathan Cameron return attrcount; 12373704432fSJonathan Cameron } 12383704432fSJonathan Cameron 123951239600SJonathan Cameron static int iio_device_add_info_mask_type_avail(struct iio_dev *indio_dev, 124051239600SJonathan Cameron struct iio_chan_spec const *chan, 124151239600SJonathan Cameron enum iio_shared_by shared_by, 124251239600SJonathan Cameron const long *infomask) 124351239600SJonathan Cameron { 1244207c2d27SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 124551239600SJonathan Cameron int i, ret, attrcount = 0; 124651239600SJonathan Cameron char *avail_postfix; 124751239600SJonathan Cameron 12481016d567SOrson Zhai for_each_set_bit(i, infomask, sizeof(*infomask) * 8) { 1249936d3e53SYoung Xiao if (i >= ARRAY_SIZE(iio_chan_info_postfix)) 1250936d3e53SYoung Xiao return -EINVAL; 125151239600SJonathan Cameron avail_postfix = kasprintf(GFP_KERNEL, 125251239600SJonathan Cameron "%s_available", 125351239600SJonathan Cameron iio_chan_info_postfix[i]); 125451239600SJonathan Cameron if (!avail_postfix) 125551239600SJonathan Cameron return -ENOMEM; 125651239600SJonathan Cameron 125751239600SJonathan Cameron ret = __iio_add_chan_devattr(avail_postfix, 125851239600SJonathan Cameron chan, 125951239600SJonathan Cameron &iio_read_channel_info_avail, 126051239600SJonathan Cameron NULL, 126151239600SJonathan Cameron i, 126251239600SJonathan Cameron shared_by, 126351239600SJonathan Cameron &indio_dev->dev, 12643e3d11b2SAlexandru Ardelean NULL, 1265207c2d27SAlexandru Ardelean &iio_dev_opaque->channel_attr_list); 126651239600SJonathan Cameron kfree(avail_postfix); 126751239600SJonathan Cameron if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) 126851239600SJonathan Cameron continue; 12691702df5dSAndy Shevchenko if (ret < 0) 127051239600SJonathan Cameron return ret; 127151239600SJonathan Cameron attrcount++; 127251239600SJonathan Cameron } 127351239600SJonathan Cameron 127451239600SJonathan Cameron return attrcount; 127551239600SJonathan Cameron } 127651239600SJonathan Cameron 1277a980e046SJonathan Cameron static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev, 1278a980e046SJonathan Cameron struct iio_chan_spec const *chan) 1279a980e046SJonathan Cameron { 1280207c2d27SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1281a980e046SJonathan Cameron int ret, attrcount = 0; 1282a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 1283a980e046SJonathan Cameron 1284a980e046SJonathan Cameron if (chan->channel < 0) 1285a980e046SJonathan Cameron return 0; 12863704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 12873704432fSJonathan Cameron IIO_SEPARATE, 12883704432fSJonathan Cameron &chan->info_mask_separate); 12898655cc49SJonathan Cameron if (ret < 0) 12903704432fSJonathan Cameron return ret; 12913704432fSJonathan Cameron attrcount += ret; 12923704432fSJonathan Cameron 129351239600SJonathan Cameron ret = iio_device_add_info_mask_type_avail(indio_dev, chan, 129451239600SJonathan Cameron IIO_SEPARATE, 1295682ca76bSJoe Simmons-Talbott &chan->info_mask_separate_available); 129651239600SJonathan Cameron if (ret < 0) 129751239600SJonathan Cameron return ret; 129851239600SJonathan Cameron attrcount += ret; 129951239600SJonathan Cameron 13003704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 13013704432fSJonathan Cameron IIO_SHARED_BY_TYPE, 13023704432fSJonathan Cameron &chan->info_mask_shared_by_type); 13033704432fSJonathan Cameron if (ret < 0) 13043704432fSJonathan Cameron return ret; 13053704432fSJonathan Cameron attrcount += ret; 1306a980e046SJonathan Cameron 130751239600SJonathan Cameron ret = iio_device_add_info_mask_type_avail(indio_dev, chan, 130851239600SJonathan Cameron IIO_SHARED_BY_TYPE, 1309682ca76bSJoe Simmons-Talbott &chan->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; 13499d9ec8d0SJoe Simmons-Talbott 1350a980e046SJonathan Cameron for (ext_info = chan->ext_info; ext_info->name; ext_info++) { 1351a980e046SJonathan Cameron ret = __iio_add_chan_devattr(ext_info->name, 1352a980e046SJonathan Cameron chan, 1353a980e046SJonathan Cameron ext_info->read ? 1354a980e046SJonathan Cameron &iio_read_channel_ext_info : NULL, 1355a980e046SJonathan Cameron ext_info->write ? 1356a980e046SJonathan Cameron &iio_write_channel_ext_info : NULL, 1357a980e046SJonathan Cameron i, 1358a980e046SJonathan Cameron ext_info->shared, 1359a980e046SJonathan Cameron &indio_dev->dev, 13603e3d11b2SAlexandru Ardelean NULL, 1361207c2d27SAlexandru Ardelean &iio_dev_opaque->channel_attr_list); 1362a980e046SJonathan Cameron i++; 1363a980e046SJonathan Cameron if (ret == -EBUSY && ext_info->shared) 1364a980e046SJonathan Cameron continue; 1365a980e046SJonathan Cameron 1366a980e046SJonathan Cameron if (ret) 13673704432fSJonathan Cameron return ret; 1368a980e046SJonathan Cameron 1369a980e046SJonathan Cameron attrcount++; 1370a980e046SJonathan Cameron } 1371a980e046SJonathan Cameron } 1372a980e046SJonathan Cameron 13733704432fSJonathan Cameron return attrcount; 1374a980e046SJonathan Cameron } 1375a980e046SJonathan Cameron 137684088ebdSLars-Peter Clausen /** 137784088ebdSLars-Peter Clausen * iio_free_chan_devattr_list() - Free a list of IIO device attributes 137884088ebdSLars-Peter Clausen * @attr_list: List of IIO device attributes 137984088ebdSLars-Peter Clausen * 138084088ebdSLars-Peter Clausen * This function frees the memory allocated for each of the IIO device 1381c1b03ab5SMartin Fuzzey * attributes in the list. 138284088ebdSLars-Peter Clausen */ 138384088ebdSLars-Peter Clausen void iio_free_chan_devattr_list(struct list_head *attr_list) 1384a980e046SJonathan Cameron { 138584088ebdSLars-Peter Clausen struct iio_dev_attr *p, *n; 138684088ebdSLars-Peter Clausen 138784088ebdSLars-Peter Clausen list_for_each_entry_safe(p, n, attr_list, l) { 138815097c7aSAlexandru Ardelean kfree_const(p->dev_attr.attr.name); 1389c1b03ab5SMartin Fuzzey list_del(&p->l); 1390a980e046SJonathan Cameron kfree(p); 1391a980e046SJonathan Cameron } 139284088ebdSLars-Peter Clausen } 1393a980e046SJonathan Cameron 13949cf0b618SJoe Simmons-Talbott static ssize_t name_show(struct device *dev, struct device_attribute *attr, 1395a980e046SJonathan Cameron char *buf) 1396a980e046SJonathan Cameron { 1397e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 13989d9ec8d0SJoe Simmons-Talbott 139983ca56b6SLars-Peter Clausen return sysfs_emit(buf, "%s\n", indio_dev->name); 1400a980e046SJonathan Cameron } 1401a980e046SJonathan Cameron 14029cf0b618SJoe Simmons-Talbott static DEVICE_ATTR_RO(name); 1403a980e046SJonathan Cameron 14049cf0b618SJoe Simmons-Talbott static ssize_t label_show(struct device *dev, struct device_attribute *attr, 14052c3d0c9fSPhil Reid char *buf) 14062c3d0c9fSPhil Reid { 14072c3d0c9fSPhil Reid struct iio_dev *indio_dev = dev_to_iio_dev(dev); 14089d9ec8d0SJoe Simmons-Talbott 140983ca56b6SLars-Peter Clausen return sysfs_emit(buf, "%s\n", indio_dev->label); 14102c3d0c9fSPhil Reid } 14112c3d0c9fSPhil Reid 14129cf0b618SJoe Simmons-Talbott static DEVICE_ATTR_RO(label); 14132c3d0c9fSPhil Reid 1414971ddd4bSAndy Shevchenko static const char * const clock_names[] = { 1415971ddd4bSAndy Shevchenko [CLOCK_REALTIME] = "realtime", 1416971ddd4bSAndy Shevchenko [CLOCK_MONOTONIC] = "monotonic", 1417971ddd4bSAndy Shevchenko [CLOCK_PROCESS_CPUTIME_ID] = "process_cputime_id", 1418971ddd4bSAndy Shevchenko [CLOCK_THREAD_CPUTIME_ID] = "thread_cputime_id", 1419971ddd4bSAndy Shevchenko [CLOCK_MONOTONIC_RAW] = "monotonic_raw", 1420971ddd4bSAndy Shevchenko [CLOCK_REALTIME_COARSE] = "realtime_coarse", 1421971ddd4bSAndy Shevchenko [CLOCK_MONOTONIC_COARSE] = "monotonic_coarse", 1422971ddd4bSAndy Shevchenko [CLOCK_BOOTTIME] = "boottime", 1423971ddd4bSAndy Shevchenko [CLOCK_REALTIME_ALARM] = "realtime_alarm", 1424971ddd4bSAndy Shevchenko [CLOCK_BOOTTIME_ALARM] = "boottime_alarm", 1425971ddd4bSAndy Shevchenko [CLOCK_SGI_CYCLE] = "sgi_cycle", 1426971ddd4bSAndy Shevchenko [CLOCK_TAI] = "tai", 1427971ddd4bSAndy Shevchenko }; 1428971ddd4bSAndy Shevchenko 14299cf0b618SJoe Simmons-Talbott static ssize_t current_timestamp_clock_show(struct device *dev, 1430bc2b7dabSGregor Boirie struct device_attribute *attr, 1431bc2b7dabSGregor Boirie char *buf) 1432bc2b7dabSGregor Boirie { 1433bc2b7dabSGregor Boirie const struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1434bc2b7dabSGregor Boirie const clockid_t clk = iio_device_get_clock(indio_dev); 1435bc2b7dabSGregor Boirie 1436bc2b7dabSGregor Boirie switch (clk) { 1437bc2b7dabSGregor Boirie case CLOCK_REALTIME: 1438bc2b7dabSGregor Boirie case CLOCK_MONOTONIC: 1439bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_RAW: 1440bc2b7dabSGregor Boirie case CLOCK_REALTIME_COARSE: 1441bc2b7dabSGregor Boirie case CLOCK_MONOTONIC_COARSE: 1442bc2b7dabSGregor Boirie case CLOCK_BOOTTIME: 1443bc2b7dabSGregor Boirie case CLOCK_TAI: 1444bc2b7dabSGregor Boirie break; 1445bc2b7dabSGregor Boirie default: 1446bc2b7dabSGregor Boirie BUG(); 1447bc2b7dabSGregor Boirie } 1448bc2b7dabSGregor Boirie 1449971ddd4bSAndy Shevchenko return sysfs_emit(buf, "%s\n", clock_names[clk]); 1450bc2b7dabSGregor Boirie } 1451bc2b7dabSGregor Boirie 14529cf0b618SJoe Simmons-Talbott static ssize_t current_timestamp_clock_store(struct device *dev, 1453bc2b7dabSGregor Boirie struct device_attribute *attr, 1454bc2b7dabSGregor Boirie const char *buf, size_t len) 1455bc2b7dabSGregor Boirie { 1456bc2b7dabSGregor Boirie clockid_t clk; 1457bc2b7dabSGregor Boirie int ret; 1458bc2b7dabSGregor Boirie 1459971ddd4bSAndy Shevchenko ret = sysfs_match_string(clock_names, buf); 1460971ddd4bSAndy Shevchenko if (ret < 0) 1461971ddd4bSAndy Shevchenko return ret; 1462971ddd4bSAndy Shevchenko clk = ret; 1463971ddd4bSAndy Shevchenko 1464971ddd4bSAndy Shevchenko switch (clk) { 1465971ddd4bSAndy Shevchenko case CLOCK_REALTIME: 1466971ddd4bSAndy Shevchenko case CLOCK_MONOTONIC: 1467971ddd4bSAndy Shevchenko case CLOCK_MONOTONIC_RAW: 1468971ddd4bSAndy Shevchenko case CLOCK_REALTIME_COARSE: 1469971ddd4bSAndy Shevchenko case CLOCK_MONOTONIC_COARSE: 1470971ddd4bSAndy Shevchenko case CLOCK_BOOTTIME: 1471971ddd4bSAndy Shevchenko case CLOCK_TAI: 1472971ddd4bSAndy Shevchenko break; 1473971ddd4bSAndy Shevchenko default: 1474bc2b7dabSGregor Boirie return -EINVAL; 1475971ddd4bSAndy Shevchenko } 1476bc2b7dabSGregor Boirie 1477bc2b7dabSGregor Boirie ret = iio_device_set_clock(dev_to_iio_dev(dev), clk); 1478bc2b7dabSGregor Boirie if (ret) 1479bc2b7dabSGregor Boirie return ret; 1480bc2b7dabSGregor Boirie 1481bc2b7dabSGregor Boirie return len; 1482bc2b7dabSGregor Boirie } 1483bc2b7dabSGregor Boirie 148432f17172SAlexandru Ardelean int iio_device_register_sysfs_group(struct iio_dev *indio_dev, 148532f17172SAlexandru Ardelean const struct attribute_group *group) 148632f17172SAlexandru Ardelean { 148732f17172SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 148832f17172SAlexandru Ardelean const struct attribute_group **new, **old = iio_dev_opaque->groups; 148932f17172SAlexandru Ardelean unsigned int cnt = iio_dev_opaque->groupcounter; 149032f17172SAlexandru Ardelean 14915a0821e0SAndy Shevchenko new = krealloc_array(old, cnt + 2, sizeof(*new), GFP_KERNEL); 149232f17172SAlexandru Ardelean if (!new) 149332f17172SAlexandru Ardelean return -ENOMEM; 149432f17172SAlexandru Ardelean 149532f17172SAlexandru Ardelean new[iio_dev_opaque->groupcounter++] = group; 149632f17172SAlexandru Ardelean new[iio_dev_opaque->groupcounter] = NULL; 149732f17172SAlexandru Ardelean 149832f17172SAlexandru Ardelean iio_dev_opaque->groups = new; 149932f17172SAlexandru Ardelean 150032f17172SAlexandru Ardelean return 0; 150132f17172SAlexandru Ardelean } 150232f17172SAlexandru Ardelean 15039cf0b618SJoe Simmons-Talbott static DEVICE_ATTR_RW(current_timestamp_clock); 1504bc2b7dabSGregor Boirie 1505a980e046SJonathan Cameron static int iio_device_register_sysfs(struct iio_dev *indio_dev) 1506a980e046SJonathan Cameron { 1507207c2d27SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1508a980e046SJonathan Cameron int i, ret = 0, attrcount, attrn, attrcount_orig = 0; 150984088ebdSLars-Peter Clausen struct iio_dev_attr *p; 1510bc2b7dabSGregor Boirie struct attribute **attr, *clk = NULL; 1511a980e046SJonathan Cameron 1512a980e046SJonathan Cameron /* First count elements in any existing group */ 1513a980e046SJonathan Cameron if (indio_dev->info->attrs) { 1514a980e046SJonathan Cameron attr = indio_dev->info->attrs->attrs; 1515a980e046SJonathan Cameron while (*attr++ != NULL) 1516a980e046SJonathan Cameron attrcount_orig++; 1517a980e046SJonathan Cameron } 1518a980e046SJonathan Cameron attrcount = attrcount_orig; 1519a980e046SJonathan Cameron /* 1520a980e046SJonathan Cameron * New channel registration method - relies on the fact a group does 1521d25b3808SPeter Meerwald * not need to be initialized if its name is NULL. 1522a980e046SJonathan Cameron */ 1523a980e046SJonathan Cameron if (indio_dev->channels) 1524a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) { 1525bc2b7dabSGregor Boirie const struct iio_chan_spec *chan = 1526bc2b7dabSGregor Boirie &indio_dev->channels[i]; 1527bc2b7dabSGregor Boirie 1528bc2b7dabSGregor Boirie if (chan->type == IIO_TIMESTAMP) 1529bc2b7dabSGregor Boirie clk = &dev_attr_current_timestamp_clock.attr; 1530bc2b7dabSGregor Boirie 1531bc2b7dabSGregor Boirie ret = iio_device_add_channel_sysfs(indio_dev, chan); 1532a980e046SJonathan Cameron if (ret < 0) 1533a980e046SJonathan Cameron goto error_clear_attrs; 1534a980e046SJonathan Cameron attrcount += ret; 1535a980e046SJonathan Cameron } 1536a980e046SJonathan Cameron 1537fa83c3baSAlexandru Ardelean if (iio_dev_opaque->event_interface) 1538bc2b7dabSGregor Boirie clk = &dev_attr_current_timestamp_clock.attr; 1539bc2b7dabSGregor Boirie 1540a980e046SJonathan Cameron if (indio_dev->name) 1541a980e046SJonathan Cameron attrcount++; 15422c3d0c9fSPhil Reid if (indio_dev->label) 15432c3d0c9fSPhil Reid attrcount++; 1544bc2b7dabSGregor Boirie if (clk) 1545bc2b7dabSGregor Boirie attrcount++; 1546a980e046SJonathan Cameron 1547207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs = 1548207c2d27SAlexandru Ardelean kcalloc(attrcount + 1, 1549207c2d27SAlexandru Ardelean sizeof(iio_dev_opaque->chan_attr_group.attrs[0]), 1550a980e046SJonathan Cameron GFP_KERNEL); 1551207c2d27SAlexandru Ardelean if (iio_dev_opaque->chan_attr_group.attrs == NULL) { 1552a980e046SJonathan Cameron ret = -ENOMEM; 1553a980e046SJonathan Cameron goto error_clear_attrs; 1554a980e046SJonathan Cameron } 1555e0d27ea3SAndrea Merello /* Copy across original attributes, and point to original binary attributes */ 155618b4c9cdSHans de Goede if (indio_dev->info->attrs) { 1557207c2d27SAlexandru Ardelean memcpy(iio_dev_opaque->chan_attr_group.attrs, 1558a980e046SJonathan Cameron indio_dev->info->attrs->attrs, 1559207c2d27SAlexandru Ardelean sizeof(iio_dev_opaque->chan_attr_group.attrs[0]) 1560a980e046SJonathan Cameron *attrcount_orig); 156118b4c9cdSHans de Goede iio_dev_opaque->chan_attr_group.is_visible = 156218b4c9cdSHans de Goede indio_dev->info->attrs->is_visible; 1563e0d27ea3SAndrea Merello iio_dev_opaque->chan_attr_group.bin_attrs = 1564e0d27ea3SAndrea Merello indio_dev->info->attrs->bin_attrs; 156518b4c9cdSHans de Goede } 1566a980e046SJonathan Cameron attrn = attrcount_orig; 1567a980e046SJonathan Cameron /* Add all elements from the list. */ 1568207c2d27SAlexandru Ardelean list_for_each_entry(p, &iio_dev_opaque->channel_attr_list, l) 1569207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr; 1570a980e046SJonathan Cameron if (indio_dev->name) 1571207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr; 15722c3d0c9fSPhil Reid if (indio_dev->label) 1573207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_label.attr; 1574bc2b7dabSGregor Boirie if (clk) 1575207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs[attrn++] = clk; 1576a980e046SJonathan Cameron 157732f17172SAlexandru Ardelean ret = iio_device_register_sysfs_group(indio_dev, 157832f17172SAlexandru Ardelean &iio_dev_opaque->chan_attr_group); 157932f17172SAlexandru Ardelean if (ret) 158032f17172SAlexandru Ardelean goto error_clear_attrs; 1581a980e046SJonathan Cameron 1582a980e046SJonathan Cameron return 0; 1583a980e046SJonathan Cameron 1584a980e046SJonathan Cameron error_clear_attrs: 1585207c2d27SAlexandru Ardelean iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list); 1586a980e046SJonathan Cameron 1587a980e046SJonathan Cameron return ret; 1588a980e046SJonathan Cameron } 1589a980e046SJonathan Cameron 1590a980e046SJonathan Cameron static void iio_device_unregister_sysfs(struct iio_dev *indio_dev) 1591a980e046SJonathan Cameron { 1592207c2d27SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1593a980e046SJonathan Cameron 1594207c2d27SAlexandru Ardelean iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list); 1595207c2d27SAlexandru Ardelean kfree(iio_dev_opaque->chan_attr_group.attrs); 1596207c2d27SAlexandru Ardelean iio_dev_opaque->chan_attr_group.attrs = NULL; 159732f17172SAlexandru Ardelean kfree(iio_dev_opaque->groups); 159819833c40SYang Yingliang iio_dev_opaque->groups = NULL; 1599a980e046SJonathan Cameron } 1600a980e046SJonathan Cameron 1601a980e046SJonathan Cameron static void iio_dev_release(struct device *device) 1602a980e046SJonathan Cameron { 1603e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(device); 16046d4ebd56SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 16056d4ebd56SAlexandru Ardelean 1606d89e119aSBenjamin Gaignard if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES) 1607a980e046SJonathan Cameron iio_device_unregister_trigger_consumer(indio_dev); 1608a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 1609a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 1610e407fd65SLars-Peter Clausen 1611218bc53dSAlexandru Ardelean iio_device_detach_buffers(indio_dev); 16129e69c935SLars-Peter Clausen 16132bc9cd66SVincent Whitchurch lockdep_unregister_key(&iio_dev_opaque->mlock_key); 16142bc9cd66SVincent Whitchurch 1615319dbcd8Skeliu ida_free(&iio_ida, iio_dev_opaque->id); 16166d4ebd56SAlexandru Ardelean kfree(iio_dev_opaque); 1617a980e046SJonathan Cameron } 1618a980e046SJonathan Cameron 16193c33b7b8SLars-Peter Clausen const struct device_type iio_device_type = { 1620a980e046SJonathan Cameron .name = "iio_device", 1621a980e046SJonathan Cameron .release = iio_dev_release, 1622a980e046SJonathan Cameron }; 1623a980e046SJonathan Cameron 1624a7e57dceSSachin Kamat /** 1625a7e57dceSSachin Kamat * iio_device_alloc() - allocate an iio_dev from a driver 1626c9561fd2SJonathan Cameron * @parent: Parent device. 1627a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 1628*65659a8dSAndy Shevchenko * 1629*65659a8dSAndy Shevchenko * Returns: 1630*65659a8dSAndy Shevchenko * Pointer to allocated iio_dev on success, NULL on failure. 1631*65659a8dSAndy Shevchenko */ 163278289b4aSAlexandru Ardelean struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv) 1633a980e046SJonathan Cameron { 16346d4ebd56SAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque; 16350d596bb2SAlexandru Ardelean struct iio_dev *indio_dev; 1636a980e046SJonathan Cameron size_t alloc_size; 1637a980e046SJonathan Cameron 16386d4ebd56SAlexandru Ardelean alloc_size = sizeof(struct iio_dev_opaque); 1639a980e046SJonathan Cameron if (sizeof_priv) { 164012c4efe3SJonathan Cameron alloc_size = ALIGN(alloc_size, IIO_DMA_MINALIGN); 1641a980e046SJonathan Cameron alloc_size += sizeof_priv; 1642a980e046SJonathan Cameron } 1643a980e046SJonathan Cameron 16446d4ebd56SAlexandru Ardelean iio_dev_opaque = kzalloc(alloc_size, GFP_KERNEL); 16456d4ebd56SAlexandru Ardelean if (!iio_dev_opaque) 16460d4b2184SJonathan Cameron return NULL; 1647a980e046SJonathan Cameron 16480d596bb2SAlexandru Ardelean indio_dev = &iio_dev_opaque->indio_dev; 16490d596bb2SAlexandru Ardelean indio_dev->priv = (char *)iio_dev_opaque + 165012c4efe3SJonathan Cameron ALIGN(sizeof(struct iio_dev_opaque), IIO_DMA_MINALIGN); 16516d4ebd56SAlexandru Ardelean 16520d596bb2SAlexandru Ardelean indio_dev->dev.parent = parent; 16530d596bb2SAlexandru Ardelean indio_dev->dev.type = &iio_device_type; 16540d596bb2SAlexandru Ardelean indio_dev->dev.bus = &iio_bus_type; 16550d596bb2SAlexandru Ardelean device_initialize(&indio_dev->dev); 165616afe125SNuno Sá mutex_init(&iio_dev_opaque->mlock); 1657b804e2b7SJonathan Cameron mutex_init(&iio_dev_opaque->info_exist_lock); 1658207c2d27SAlexandru Ardelean INIT_LIST_HEAD(&iio_dev_opaque->channel_attr_list); 1659a980e046SJonathan Cameron 1660319dbcd8Skeliu iio_dev_opaque->id = ida_alloc(&iio_ida, 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 } 1667fe6f45f6SYang Yingliang 1668fe6f45f6SYang Yingliang if (dev_set_name(&indio_dev->dev, "iio:device%d", iio_dev_opaque->id)) { 1669319dbcd8Skeliu ida_free(&iio_ida, iio_dev_opaque->id); 1670fe6f45f6SYang Yingliang kfree(iio_dev_opaque); 1671fe6f45f6SYang Yingliang return NULL; 1672fe6f45f6SYang Yingliang } 1673fe6f45f6SYang Yingliang 16746a8c6b26SAlexandru Ardelean INIT_LIST_HEAD(&iio_dev_opaque->buffer_list); 16758dedcc3eSAlexandru Ardelean INIT_LIST_HEAD(&iio_dev_opaque->ioctl_handlers); 1676a980e046SJonathan Cameron 16772bc9cd66SVincent Whitchurch lockdep_register_key(&iio_dev_opaque->mlock_key); 167816afe125SNuno Sá lockdep_set_class(&iio_dev_opaque->mlock, &iio_dev_opaque->mlock_key); 16792bc9cd66SVincent Whitchurch 16800d596bb2SAlexandru Ardelean return indio_dev; 1681a980e046SJonathan Cameron } 16827cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_alloc); 1683a980e046SJonathan Cameron 1684a7e57dceSSachin Kamat /** 1685a7e57dceSSachin Kamat * iio_device_free() - free an iio_dev from a driver 1686a7e57dceSSachin Kamat * @dev: the iio_dev associated with the device 1687*65659a8dSAndy Shevchenko */ 16887cbb7537SLars-Peter Clausen void iio_device_free(struct iio_dev *dev) 1689a980e046SJonathan Cameron { 1690e407fd65SLars-Peter Clausen if (dev) 1691e407fd65SLars-Peter Clausen put_device(&dev->dev); 1692a980e046SJonathan Cameron } 16937cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_free); 1694a980e046SJonathan Cameron 1695cf5724e9SYicong Yang static void devm_iio_device_release(void *iio_dev) 16969dabaf5eSGrygorii Strashko { 1697cf5724e9SYicong Yang iio_device_free(iio_dev); 16989dabaf5eSGrygorii Strashko } 16999dabaf5eSGrygorii Strashko 1700a7e57dceSSachin Kamat /** 1701a7e57dceSSachin Kamat * devm_iio_device_alloc - Resource-managed iio_device_alloc() 170278289b4aSAlexandru Ardelean * @parent: Device to allocate iio_dev for, and parent for this IIO device 1703a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 1704a7e57dceSSachin Kamat * 1705a7e57dceSSachin Kamat * Managed iio_device_alloc. iio_dev allocated with this function is 1706a7e57dceSSachin Kamat * automatically freed on driver detach. 1707a7e57dceSSachin Kamat * 1708*65659a8dSAndy Shevchenko * Returns: 1709a7e57dceSSachin Kamat * Pointer to allocated iio_dev on success, NULL on failure. 1710a7e57dceSSachin Kamat */ 171178289b4aSAlexandru Ardelean struct iio_dev *devm_iio_device_alloc(struct device *parent, int sizeof_priv) 17129dabaf5eSGrygorii Strashko { 1713cf5724e9SYicong Yang struct iio_dev *iio_dev; 1714cf5724e9SYicong Yang int ret; 17159dabaf5eSGrygorii Strashko 171678289b4aSAlexandru Ardelean iio_dev = iio_device_alloc(parent, sizeof_priv); 1717cf5724e9SYicong Yang if (!iio_dev) 1718cf5724e9SYicong Yang return NULL; 1719cf5724e9SYicong Yang 1720cf5724e9SYicong Yang ret = devm_add_action_or_reset(parent, devm_iio_device_release, 1721cf5724e9SYicong Yang iio_dev); 1722cf5724e9SYicong Yang if (ret) 172343fa7394SDan Carpenter return NULL; 17249dabaf5eSGrygorii Strashko 17259dabaf5eSGrygorii Strashko return iio_dev; 17269dabaf5eSGrygorii Strashko } 17279dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_alloc); 17289dabaf5eSGrygorii Strashko 1729a7e57dceSSachin Kamat /** 1730a980e046SJonathan Cameron * iio_chrdev_open() - chrdev file open for buffer access and ioctls 17312498dcf6SCristina Opriceana * @inode: Inode structure for identifying the device in the file system 17322498dcf6SCristina Opriceana * @filp: File structure for iio device used to keep and later access 17332498dcf6SCristina Opriceana * private data 17342498dcf6SCristina Opriceana * 1735*65659a8dSAndy Shevchenko * Returns: 0 on success or -EBUSY if the device is already opened 1736*65659a8dSAndy Shevchenko */ 1737a980e046SJonathan Cameron static int iio_chrdev_open(struct inode *inode, struct file *filp) 1738a980e046SJonathan Cameron { 1739396f7234SJonathan Cameron struct iio_dev_opaque *iio_dev_opaque = 1740396f7234SJonathan Cameron container_of(inode->i_cdev, struct iio_dev_opaque, chrdev); 1741396f7234SJonathan Cameron struct iio_dev *indio_dev = &iio_dev_opaque->indio_dev; 1742be24dcb1SAlexandru Ardelean struct iio_dev_buffer_pair *ib; 1743a980e046SJonathan Cameron 17448b1c82cbSJonathan Cameron if (test_and_set_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags)) 1745a980e046SJonathan Cameron return -EBUSY; 1746a980e046SJonathan Cameron 1747cadc2125SLars-Peter Clausen iio_device_get(indio_dev); 1748cadc2125SLars-Peter Clausen 1749be24dcb1SAlexandru Ardelean ib = kmalloc(sizeof(*ib), GFP_KERNEL); 1750be24dcb1SAlexandru Ardelean if (!ib) { 1751be24dcb1SAlexandru Ardelean iio_device_put(indio_dev); 17528b1c82cbSJonathan Cameron clear_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags); 1753be24dcb1SAlexandru Ardelean return -ENOMEM; 1754be24dcb1SAlexandru Ardelean } 1755be24dcb1SAlexandru Ardelean 1756be24dcb1SAlexandru Ardelean ib->indio_dev = indio_dev; 1757be24dcb1SAlexandru Ardelean ib->buffer = indio_dev->buffer; 1758be24dcb1SAlexandru Ardelean 1759be24dcb1SAlexandru Ardelean filp->private_data = ib; 1760a980e046SJonathan Cameron 1761a980e046SJonathan Cameron return 0; 1762a980e046SJonathan Cameron } 1763a980e046SJonathan Cameron 1764a980e046SJonathan Cameron /** 1765a980e046SJonathan Cameron * iio_chrdev_release() - chrdev file close buffer access and ioctls 17662498dcf6SCristina Opriceana * @inode: Inode structure pointer for the char device 17672498dcf6SCristina Opriceana * @filp: File structure pointer for the char device 17682498dcf6SCristina Opriceana * 1769*65659a8dSAndy Shevchenko * Returns: 0 for successful release. 17702498dcf6SCristina Opriceana */ 1771a980e046SJonathan Cameron static int iio_chrdev_release(struct inode *inode, struct file *filp) 1772a980e046SJonathan Cameron { 1773be24dcb1SAlexandru Ardelean struct iio_dev_buffer_pair *ib = filp->private_data; 1774396f7234SJonathan Cameron struct iio_dev_opaque *iio_dev_opaque = 1775396f7234SJonathan Cameron container_of(inode->i_cdev, struct iio_dev_opaque, chrdev); 1776396f7234SJonathan Cameron struct iio_dev *indio_dev = &iio_dev_opaque->indio_dev; 17779d9ec8d0SJoe Simmons-Talbott 1778be24dcb1SAlexandru Ardelean kfree(ib); 17798b1c82cbSJonathan Cameron clear_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags); 1780cadc2125SLars-Peter Clausen iio_device_put(indio_dev); 1781cadc2125SLars-Peter Clausen 1782a980e046SJonathan Cameron return 0; 1783a980e046SJonathan Cameron } 1784a980e046SJonathan Cameron 17858dedcc3eSAlexandru Ardelean void iio_device_ioctl_handler_register(struct iio_dev *indio_dev, 17868dedcc3eSAlexandru Ardelean struct iio_ioctl_handler *h) 17878dedcc3eSAlexandru Ardelean { 17888dedcc3eSAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 17898dedcc3eSAlexandru Ardelean 17908dedcc3eSAlexandru Ardelean list_add_tail(&h->entry, &iio_dev_opaque->ioctl_handlers); 17918dedcc3eSAlexandru Ardelean } 17928dedcc3eSAlexandru Ardelean 17938dedcc3eSAlexandru Ardelean void iio_device_ioctl_handler_unregister(struct iio_ioctl_handler *h) 17948dedcc3eSAlexandru Ardelean { 17958dedcc3eSAlexandru Ardelean list_del(&h->entry); 17968dedcc3eSAlexandru Ardelean } 17978dedcc3eSAlexandru Ardelean 1798a980e046SJonathan Cameron static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 1799a980e046SJonathan Cameron { 1800be24dcb1SAlexandru Ardelean struct iio_dev_buffer_pair *ib = filp->private_data; 1801be24dcb1SAlexandru Ardelean struct iio_dev *indio_dev = ib->indio_dev; 18028dedcc3eSAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 18038dedcc3eSAlexandru Ardelean struct iio_ioctl_handler *h; 18048dedcc3eSAlexandru Ardelean int ret = -ENODEV; 1805a980e046SJonathan Cameron 1806b804e2b7SJonathan Cameron mutex_lock(&iio_dev_opaque->info_exist_lock); 18078dedcc3eSAlexandru Ardelean 1808*65659a8dSAndy Shevchenko /* 18098dedcc3eSAlexandru Ardelean * The NULL check here is required to prevent crashing when a device 18108dedcc3eSAlexandru Ardelean * is being removed while userspace would still have open file handles 18118dedcc3eSAlexandru Ardelean * to try to access this device. 18128dedcc3eSAlexandru Ardelean */ 1813f18e7a06SLars-Peter Clausen if (!indio_dev->info) 18148dedcc3eSAlexandru Ardelean goto out_unlock; 1815f18e7a06SLars-Peter Clausen 18168dedcc3eSAlexandru Ardelean list_for_each_entry(h, &iio_dev_opaque->ioctl_handlers, entry) { 18178dedcc3eSAlexandru Ardelean ret = h->ioctl(indio_dev, filp, cmd, arg); 18188dedcc3eSAlexandru Ardelean if (ret != IIO_IOCTL_UNHANDLED) 18198dedcc3eSAlexandru Ardelean break; 1820a980e046SJonathan Cameron } 18218dedcc3eSAlexandru Ardelean 182274d826daSAlexandru Ardelean if (ret == IIO_IOCTL_UNHANDLED) 1823af0670b0SAlexandru Ardelean ret = -ENODEV; 182474d826daSAlexandru Ardelean 18258dedcc3eSAlexandru Ardelean out_unlock: 1826b804e2b7SJonathan Cameron mutex_unlock(&iio_dev_opaque->info_exist_lock); 18278dedcc3eSAlexandru Ardelean 18288dedcc3eSAlexandru Ardelean return ret; 1829a980e046SJonathan Cameron } 1830a980e046SJonathan Cameron 1831a980e046SJonathan Cameron static const struct file_operations iio_buffer_fileops = { 1832a980e046SJonathan Cameron .owner = THIS_MODULE, 1833a980e046SJonathan Cameron .llseek = noop_llseek, 1834ee8caea0SAlexandru Ardelean .read = iio_buffer_read_outer_addr, 18359eeee3b0SMihail Chindris .write = iio_buffer_write_outer_addr, 1836ee8caea0SAlexandru Ardelean .poll = iio_buffer_poll_addr, 1837a980e046SJonathan Cameron .unlocked_ioctl = iio_ioctl, 18381832f2d8SArnd Bergmann .compat_ioctl = compat_ptr_ioctl, 1839ee8caea0SAlexandru Ardelean .open = iio_chrdev_open, 1840ee8caea0SAlexandru Ardelean .release = iio_chrdev_release, 1841a980e046SJonathan Cameron }; 1842a980e046SJonathan Cameron 18438ebaa3ffSAlexandru Ardelean static const struct file_operations iio_event_fileops = { 18448ebaa3ffSAlexandru Ardelean .owner = THIS_MODULE, 18458ebaa3ffSAlexandru Ardelean .llseek = noop_llseek, 18468ebaa3ffSAlexandru Ardelean .unlocked_ioctl = iio_ioctl, 18478ebaa3ffSAlexandru Ardelean .compat_ioctl = compat_ptr_ioctl, 18488ebaa3ffSAlexandru Ardelean .open = iio_chrdev_open, 18498ebaa3ffSAlexandru Ardelean .release = iio_chrdev_release, 18508ebaa3ffSAlexandru Ardelean }; 18518ebaa3ffSAlexandru Ardelean 18528f5d8727SVlad Dogaru static int iio_check_unique_scan_index(struct iio_dev *indio_dev) 18538f5d8727SVlad Dogaru { 18548f5d8727SVlad Dogaru int i, j; 18558f5d8727SVlad Dogaru const struct iio_chan_spec *channels = indio_dev->channels; 18568f5d8727SVlad Dogaru 18578f5d8727SVlad Dogaru if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES)) 18588f5d8727SVlad Dogaru return 0; 18598f5d8727SVlad Dogaru 18608f5d8727SVlad Dogaru for (i = 0; i < indio_dev->num_channels - 1; i++) { 18618f5d8727SVlad Dogaru if (channels[i].scan_index < 0) 18628f5d8727SVlad Dogaru continue; 18638f5d8727SVlad Dogaru for (j = i + 1; j < indio_dev->num_channels; j++) 18648f5d8727SVlad Dogaru if (channels[i].scan_index == channels[j].scan_index) { 18658f5d8727SVlad Dogaru dev_err(&indio_dev->dev, 18668f5d8727SVlad Dogaru "Duplicate scan index %d\n", 18678f5d8727SVlad Dogaru channels[i].scan_index); 18688f5d8727SVlad Dogaru return -EINVAL; 18698f5d8727SVlad Dogaru } 18708f5d8727SVlad Dogaru } 18718f5d8727SVlad Dogaru 18728f5d8727SVlad Dogaru return 0; 18738f5d8727SVlad Dogaru } 18748f5d8727SVlad Dogaru 18750e0761f8SPaul Cercueil static int iio_check_extended_name(const struct iio_dev *indio_dev) 18760e0761f8SPaul Cercueil { 18770e0761f8SPaul Cercueil unsigned int i; 18780e0761f8SPaul Cercueil 18790e0761f8SPaul Cercueil if (!indio_dev->info->read_label) 18800e0761f8SPaul Cercueil return 0; 18810e0761f8SPaul Cercueil 18820e0761f8SPaul Cercueil for (i = 0; i < indio_dev->num_channels; i++) { 18830e0761f8SPaul Cercueil if (indio_dev->channels[i].extend_name) { 18840e0761f8SPaul Cercueil dev_err(&indio_dev->dev, 18850e0761f8SPaul Cercueil "Cannot use labels and extend_name at the same time\n"); 18860e0761f8SPaul Cercueil return -EINVAL; 18870e0761f8SPaul Cercueil } 18880e0761f8SPaul Cercueil } 18890e0761f8SPaul Cercueil 18900e0761f8SPaul Cercueil return 0; 18910e0761f8SPaul Cercueil } 18920e0761f8SPaul Cercueil 1893a980e046SJonathan Cameron static const struct iio_buffer_setup_ops noop_ring_setup_ops; 1894a980e046SJonathan Cameron 189563b19547SJonathan Cameron int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod) 1896a980e046SJonathan Cameron { 18978ebaa3ffSAlexandru Ardelean struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 189881366556SAndy Shevchenko struct fwnode_handle *fwnode; 1899a980e046SJonathan Cameron int ret; 1900a980e046SJonathan Cameron 1901a17cb784SAlexandru Ardelean if (!indio_dev->info) 1902a17cb784SAlexandru Ardelean return -EINVAL; 1903a17cb784SAlexandru Ardelean 19046eaf9f6aSJonathan Cameron iio_dev_opaque->driver_module = this_mod; 190517d82b47SGuenter Roeck 190681366556SAndy Shevchenko /* If the calling driver did not initialize firmware node, do it here */ 190781366556SAndy Shevchenko if (dev_fwnode(&indio_dev->dev)) 190881366556SAndy Shevchenko fwnode = dev_fwnode(&indio_dev->dev); 190981366556SAndy Shevchenko else 191081366556SAndy Shevchenko fwnode = dev_fwnode(indio_dev->dev.parent); 191181366556SAndy Shevchenko device_set_node(&indio_dev->dev, fwnode); 191281366556SAndy Shevchenko 191381366556SAndy Shevchenko fwnode_property_read_string(fwnode, "label", &indio_dev->label); 19142c3d0c9fSPhil Reid 19158f5d8727SVlad Dogaru ret = iio_check_unique_scan_index(indio_dev); 19168f5d8727SVlad Dogaru if (ret < 0) 19178f5d8727SVlad Dogaru return ret; 19188f5d8727SVlad Dogaru 19190e0761f8SPaul Cercueil ret = iio_check_extended_name(indio_dev); 19200e0761f8SPaul Cercueil if (ret < 0) 19210e0761f8SPaul Cercueil return ret; 19220e0761f8SPaul Cercueil 19238915aacaSGreg Kroah-Hartman iio_device_register_debugfs(indio_dev); 19243e1b6c95SLars-Peter Clausen 1925ee708e6bSAlexandru Ardelean ret = iio_buffers_alloc_sysfs_and_mask(indio_dev); 19263e1b6c95SLars-Peter Clausen if (ret) { 19273e1b6c95SLars-Peter Clausen dev_err(indio_dev->dev.parent, 19283e1b6c95SLars-Peter Clausen "Failed to create buffer sysfs interfaces\n"); 19293e1b6c95SLars-Peter Clausen goto error_unreg_debugfs; 19303e1b6c95SLars-Peter Clausen } 19313e1b6c95SLars-Peter Clausen 1932a980e046SJonathan Cameron ret = iio_device_register_sysfs(indio_dev); 1933a980e046SJonathan Cameron if (ret) { 1934a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1935a980e046SJonathan Cameron "Failed to register sysfs interfaces\n"); 19363e1b6c95SLars-Peter Clausen goto error_buffer_free_sysfs; 1937a980e046SJonathan Cameron } 1938a980e046SJonathan Cameron ret = iio_device_register_eventset(indio_dev); 1939a980e046SJonathan Cameron if (ret) { 1940a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1941a980e046SJonathan Cameron "Failed to register event set\n"); 1942a980e046SJonathan Cameron goto error_free_sysfs; 1943a980e046SJonathan Cameron } 1944d89e119aSBenjamin Gaignard if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES) 1945a980e046SJonathan Cameron iio_device_register_trigger_consumer(indio_dev); 1946a980e046SJonathan Cameron 1947a980e046SJonathan Cameron if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) && 1948a980e046SJonathan Cameron indio_dev->setup_ops == NULL) 1949a980e046SJonathan Cameron indio_dev->setup_ops = &noop_ring_setup_ops; 1950a980e046SJonathan Cameron 1951ee708e6bSAlexandru Ardelean if (iio_dev_opaque->attached_buffers_cnt) 1952396f7234SJonathan Cameron cdev_init(&iio_dev_opaque->chrdev, &iio_buffer_fileops); 19538ebaa3ffSAlexandru Ardelean else if (iio_dev_opaque->event_interface) 1954396f7234SJonathan Cameron cdev_init(&iio_dev_opaque->chrdev, &iio_event_fileops); 195563b19547SJonathan Cameron 1956ee708e6bSAlexandru Ardelean if (iio_dev_opaque->attached_buffers_cnt || iio_dev_opaque->event_interface) { 195715ea2878SJonathan Cameron indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), iio_dev_opaque->id); 1958396f7234SJonathan Cameron iio_dev_opaque->chrdev.owner = this_mod; 19598ebaa3ffSAlexandru Ardelean } 196038923911SLogan Gunthorpe 196132f17172SAlexandru Ardelean /* assign device groups now; they should be all registered now */ 196232f17172SAlexandru Ardelean indio_dev->dev.groups = iio_dev_opaque->groups; 196332f17172SAlexandru Ardelean 1964396f7234SJonathan Cameron ret = cdev_device_add(&iio_dev_opaque->chrdev, &indio_dev->dev); 1965a980e046SJonathan Cameron if (ret < 0) 19660d5b7daeSLars-Peter Clausen goto error_unreg_eventset; 1967a980e046SJonathan Cameron 19680d5b7daeSLars-Peter Clausen return 0; 196938923911SLogan Gunthorpe 1970a980e046SJonathan Cameron error_unreg_eventset: 1971a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 1972a980e046SJonathan Cameron error_free_sysfs: 1973a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 19743e1b6c95SLars-Peter Clausen error_buffer_free_sysfs: 1975ee708e6bSAlexandru Ardelean iio_buffers_free_sysfs_and_mask(indio_dev); 1976a980e046SJonathan Cameron error_unreg_debugfs: 1977a980e046SJonathan Cameron iio_device_unregister_debugfs(indio_dev); 1978a980e046SJonathan Cameron return ret; 1979a980e046SJonathan Cameron } 198063b19547SJonathan Cameron EXPORT_SYMBOL(__iio_device_register); 1981a980e046SJonathan Cameron 1982a7e57dceSSachin Kamat /** 1983a7e57dceSSachin Kamat * iio_device_unregister() - unregister a device from the IIO subsystem 1984a7e57dceSSachin Kamat * @indio_dev: Device structure representing the device. 1985*65659a8dSAndy Shevchenko */ 1986a980e046SJonathan Cameron void iio_device_unregister(struct iio_dev *indio_dev) 1987a980e046SJonathan Cameron { 1988396f7234SJonathan Cameron struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1989396f7234SJonathan Cameron 1990396f7234SJonathan Cameron cdev_device_del(&iio_dev_opaque->chrdev, &indio_dev->dev); 1991a87c82e4SLars-Peter Clausen 1992b804e2b7SJonathan Cameron mutex_lock(&iio_dev_opaque->info_exist_lock); 19937f75591fSFabrice Gasnier 1994bc4c9612SLars-Peter Clausen iio_device_unregister_debugfs(indio_dev); 19950d5b7daeSLars-Peter Clausen 1996a87c82e4SLars-Peter Clausen iio_disable_all_buffers(indio_dev); 1997a87c82e4SLars-Peter Clausen 1998a980e046SJonathan Cameron indio_dev->info = NULL; 1999d2f0a48fSLars-Peter Clausen 2000d2f0a48fSLars-Peter Clausen iio_device_wakeup_eventset(indio_dev); 2001d2f0a48fSLars-Peter Clausen iio_buffer_wakeup_poll(indio_dev); 2002d2f0a48fSLars-Peter Clausen 2003b804e2b7SJonathan Cameron mutex_unlock(&iio_dev_opaque->info_exist_lock); 20043e1b6c95SLars-Peter Clausen 2005ee708e6bSAlexandru Ardelean iio_buffers_free_sysfs_and_mask(indio_dev); 2006a980e046SJonathan Cameron } 2007a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_unregister); 20088caa07c0SSachin Kamat 2009cf5724e9SYicong Yang static void devm_iio_device_unreg(void *indio_dev) 20108caa07c0SSachin Kamat { 2011cf5724e9SYicong Yang iio_device_unregister(indio_dev); 20128caa07c0SSachin Kamat } 20138caa07c0SSachin Kamat 201463b19547SJonathan Cameron int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev, 201563b19547SJonathan Cameron struct module *this_mod) 20168caa07c0SSachin Kamat { 20178caa07c0SSachin Kamat int ret; 20188caa07c0SSachin Kamat 201963b19547SJonathan Cameron ret = __iio_device_register(indio_dev, this_mod); 2020cf5724e9SYicong Yang if (ret) 20218caa07c0SSachin Kamat return ret; 2022cf5724e9SYicong Yang 2023cf5724e9SYicong Yang return devm_add_action_or_reset(dev, devm_iio_device_unreg, indio_dev); 20248caa07c0SSachin Kamat } 202563b19547SJonathan Cameron EXPORT_SYMBOL_GPL(__devm_iio_device_register); 20268caa07c0SSachin Kamat 20278caa07c0SSachin Kamat /** 202808a33805SAlison Schofield * iio_device_claim_direct_mode - Keep device in direct mode 202908a33805SAlison Schofield * @indio_dev: the iio_dev associated with the device 203008a33805SAlison Schofield * 203108a33805SAlison Schofield * If the device is in direct mode it is guaranteed to stay 203208a33805SAlison Schofield * that way until iio_device_release_direct_mode() is called. 203308a33805SAlison Schofield * 203408a33805SAlison Schofield * Use with iio_device_release_direct_mode() 203508a33805SAlison Schofield * 2036*65659a8dSAndy Shevchenko * Returns: 0 on success, -EBUSY on failure. 203708a33805SAlison Schofield */ 203808a33805SAlison Schofield int iio_device_claim_direct_mode(struct iio_dev *indio_dev) 203908a33805SAlison Schofield { 204016afe125SNuno Sá struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 204116afe125SNuno Sá 204216afe125SNuno Sá mutex_lock(&iio_dev_opaque->mlock); 204308a33805SAlison Schofield 204408a33805SAlison Schofield if (iio_buffer_enabled(indio_dev)) { 204516afe125SNuno Sá mutex_unlock(&iio_dev_opaque->mlock); 204608a33805SAlison Schofield return -EBUSY; 204708a33805SAlison Schofield } 204808a33805SAlison Schofield return 0; 204908a33805SAlison Schofield } 205008a33805SAlison Schofield EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode); 205108a33805SAlison Schofield 205208a33805SAlison Schofield /** 205308a33805SAlison Schofield * iio_device_release_direct_mode - releases claim on direct mode 205408a33805SAlison Schofield * @indio_dev: the iio_dev associated with the device 205508a33805SAlison Schofield * 205608a33805SAlison Schofield * Release the claim. Device is no longer guaranteed to stay 205708a33805SAlison Schofield * in direct mode. 205808a33805SAlison Schofield * 205908a33805SAlison Schofield * Use with iio_device_claim_direct_mode() 206008a33805SAlison Schofield */ 206108a33805SAlison Schofield void iio_device_release_direct_mode(struct iio_dev *indio_dev) 206208a33805SAlison Schofield { 206316afe125SNuno Sá mutex_unlock(&to_iio_dev_opaque(indio_dev)->mlock); 206408a33805SAlison Schofield } 206508a33805SAlison Schofield EXPORT_SYMBOL_GPL(iio_device_release_direct_mode); 206608a33805SAlison Schofield 20678c576f87SMiquel Raynal /** 20680a856542SNuno Sá * iio_device_claim_buffer_mode - Keep device in buffer mode 20690a856542SNuno Sá * @indio_dev: the iio_dev associated with the device 20700a856542SNuno Sá * 20710a856542SNuno Sá * If the device is in buffer mode it is guaranteed to stay 20720a856542SNuno Sá * that way until iio_device_release_buffer_mode() is called. 20730a856542SNuno Sá * 20740a856542SNuno Sá * Use with iio_device_release_buffer_mode(). 20750a856542SNuno Sá * 20760a856542SNuno Sá * Returns: 0 on success, -EBUSY on failure. 20770a856542SNuno Sá */ 20780a856542SNuno Sá int iio_device_claim_buffer_mode(struct iio_dev *indio_dev) 20790a856542SNuno Sá { 208016afe125SNuno Sá struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 208116afe125SNuno Sá 208216afe125SNuno Sá mutex_lock(&iio_dev_opaque->mlock); 20830a856542SNuno Sá 20840a856542SNuno Sá if (iio_buffer_enabled(indio_dev)) 20850a856542SNuno Sá return 0; 20860a856542SNuno Sá 208716afe125SNuno Sá mutex_unlock(&iio_dev_opaque->mlock); 20880a856542SNuno Sá return -EBUSY; 20890a856542SNuno Sá } 20900a856542SNuno Sá EXPORT_SYMBOL_GPL(iio_device_claim_buffer_mode); 20910a856542SNuno Sá 20920a856542SNuno Sá /** 20930a856542SNuno Sá * iio_device_release_buffer_mode - releases claim on buffer mode 20940a856542SNuno Sá * @indio_dev: the iio_dev associated with the device 20950a856542SNuno Sá * 20960a856542SNuno Sá * Release the claim. Device is no longer guaranteed to stay 20970a856542SNuno Sá * in buffer mode. 20980a856542SNuno Sá * 20990a856542SNuno Sá * Use with iio_device_claim_buffer_mode(). 21000a856542SNuno Sá */ 21010a856542SNuno Sá void iio_device_release_buffer_mode(struct iio_dev *indio_dev) 21020a856542SNuno Sá { 210316afe125SNuno Sá mutex_unlock(&to_iio_dev_opaque(indio_dev)->mlock); 21040a856542SNuno Sá } 21050a856542SNuno Sá EXPORT_SYMBOL_GPL(iio_device_release_buffer_mode); 21060a856542SNuno Sá 21070a856542SNuno Sá /** 21088c576f87SMiquel Raynal * iio_device_get_current_mode() - helper function providing read-only access to 210951570c9dSMiquel Raynal * the opaque @currentmode variable 21108c576f87SMiquel Raynal * @indio_dev: IIO device structure for device 21118c576f87SMiquel Raynal */ 21128c576f87SMiquel Raynal int iio_device_get_current_mode(struct iio_dev *indio_dev) 21138c576f87SMiquel Raynal { 211451570c9dSMiquel Raynal struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 211551570c9dSMiquel Raynal 211651570c9dSMiquel Raynal return iio_dev_opaque->currentmode; 21178c576f87SMiquel Raynal } 21188c576f87SMiquel Raynal EXPORT_SYMBOL_GPL(iio_device_get_current_mode); 21198c576f87SMiquel Raynal 2120a980e046SJonathan Cameron subsys_initcall(iio_init); 2121a980e046SJonathan Cameron module_exit(iio_exit); 2122a980e046SJonathan Cameron 2123c8b95952SJonathan Cameron MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); 2124a980e046SJonathan Cameron MODULE_DESCRIPTION("Industrial I/O core"); 2125a980e046SJonathan Cameron MODULE_LICENSE("GPL"); 2126