1a980e046SJonathan Cameron /* The industrial I/O core 2a980e046SJonathan Cameron * 3a980e046SJonathan Cameron * Copyright (c) 2008 Jonathan Cameron 4a980e046SJonathan Cameron * 5a980e046SJonathan Cameron * This program is free software; you can redistribute it and/or modify it 6a980e046SJonathan Cameron * under the terms of the GNU General Public License version 2 as published by 7a980e046SJonathan Cameron * the Free Software Foundation. 8a980e046SJonathan Cameron * 9a980e046SJonathan Cameron * Based on elements of hwmon and input subsystems. 10a980e046SJonathan Cameron */ 11a980e046SJonathan Cameron 123176dd5dSSachin Kamat #define pr_fmt(fmt) "iio-core: " fmt 133176dd5dSSachin Kamat 14a980e046SJonathan Cameron #include <linux/kernel.h> 15a980e046SJonathan Cameron #include <linux/module.h> 16a980e046SJonathan Cameron #include <linux/idr.h> 17a980e046SJonathan Cameron #include <linux/kdev_t.h> 18a980e046SJonathan Cameron #include <linux/err.h> 19a980e046SJonathan Cameron #include <linux/device.h> 20a980e046SJonathan Cameron #include <linux/fs.h> 21a980e046SJonathan Cameron #include <linux/poll.h> 22a980e046SJonathan Cameron #include <linux/sched.h> 23a980e046SJonathan Cameron #include <linux/wait.h> 24a980e046SJonathan Cameron #include <linux/cdev.h> 25a980e046SJonathan Cameron #include <linux/slab.h> 26a980e046SJonathan Cameron #include <linux/anon_inodes.h> 27a980e046SJonathan Cameron #include <linux/debugfs.h> 28*08a33805SAlison Schofield #include <linux/mutex.h> 29a980e046SJonathan Cameron #include <linux/iio/iio.h> 30a980e046SJonathan Cameron #include "iio_core.h" 31a980e046SJonathan Cameron #include "iio_core_trigger.h" 32a980e046SJonathan Cameron #include <linux/iio/sysfs.h> 33a980e046SJonathan Cameron #include <linux/iio/events.h> 349e69c935SLars-Peter Clausen #include <linux/iio/buffer.h> 35a980e046SJonathan Cameron 36a980e046SJonathan Cameron /* IDA to assign each registered device a unique id */ 37a980e046SJonathan Cameron static DEFINE_IDA(iio_ida); 38a980e046SJonathan Cameron 39a980e046SJonathan Cameron static dev_t iio_devt; 40a980e046SJonathan Cameron 41a980e046SJonathan Cameron #define IIO_DEV_MAX 256 42a980e046SJonathan Cameron struct bus_type iio_bus_type = { 43a980e046SJonathan Cameron .name = "iio", 44a980e046SJonathan Cameron }; 45a980e046SJonathan Cameron EXPORT_SYMBOL(iio_bus_type); 46a980e046SJonathan Cameron 47a980e046SJonathan Cameron static struct dentry *iio_debugfs_dentry; 48a980e046SJonathan Cameron 49a980e046SJonathan Cameron static const char * const iio_direction[] = { 50a980e046SJonathan Cameron [0] = "in", 51a980e046SJonathan Cameron [1] = "out", 52a980e046SJonathan Cameron }; 53a980e046SJonathan Cameron 54a980e046SJonathan Cameron static const char * const iio_chan_type_name_spec[] = { 55a980e046SJonathan Cameron [IIO_VOLTAGE] = "voltage", 56a980e046SJonathan Cameron [IIO_CURRENT] = "current", 57a980e046SJonathan Cameron [IIO_POWER] = "power", 58a980e046SJonathan Cameron [IIO_ACCEL] = "accel", 59a980e046SJonathan Cameron [IIO_ANGL_VEL] = "anglvel", 60a980e046SJonathan Cameron [IIO_MAGN] = "magn", 61a980e046SJonathan Cameron [IIO_LIGHT] = "illuminance", 62a980e046SJonathan Cameron [IIO_INTENSITY] = "intensity", 63a980e046SJonathan Cameron [IIO_PROXIMITY] = "proximity", 64a980e046SJonathan Cameron [IIO_TEMP] = "temp", 65a980e046SJonathan Cameron [IIO_INCLI] = "incli", 66a980e046SJonathan Cameron [IIO_ROT] = "rot", 67a980e046SJonathan Cameron [IIO_ANGL] = "angl", 68a980e046SJonathan Cameron [IIO_TIMESTAMP] = "timestamp", 69a980e046SJonathan Cameron [IIO_CAPACITANCE] = "capacitance", 70a6b12855SMichael Hennerich [IIO_ALTVOLTAGE] = "altvoltage", 7121cd1fabSJon Brenner [IIO_CCT] = "cct", 72c4f0c693SLars-Peter Clausen [IIO_PRESSURE] = "pressure", 73ac216aa2SHarald Geyer [IIO_HUMIDITYRELATIVE] = "humidityrelative", 7455aebeb9SDaniel Baluta [IIO_ACTIVITY] = "activity", 75a88bfe78SIrina Tirdea [IIO_STEPS] = "steps", 7672c66644SIrina Tirdea [IIO_ENERGY] = "energy", 77cc3c9eecSIrina Tirdea [IIO_DISTANCE] = "distance", 785a1a9329SIrina Tirdea [IIO_VELOCITY] = "velocity", 798ff6b3bcSMatt Ranostay [IIO_CONCENTRATION] = "concentration", 80d38d5469SMatt Ranostay [IIO_RESISTANCE] = "resistance", 81ecb3a7ccSMatt Ranostay [IIO_PH] = "ph", 82a980e046SJonathan Cameron }; 83a980e046SJonathan Cameron 84a980e046SJonathan Cameron static const char * const iio_modifier_names[] = { 85a980e046SJonathan Cameron [IIO_MOD_X] = "x", 86a980e046SJonathan Cameron [IIO_MOD_Y] = "y", 87a980e046SJonathan Cameron [IIO_MOD_Z] = "z", 884b8d8015SPeter Meerwald [IIO_MOD_X_AND_Y] = "x&y", 894b8d8015SPeter Meerwald [IIO_MOD_X_AND_Z] = "x&z", 904b8d8015SPeter Meerwald [IIO_MOD_Y_AND_Z] = "y&z", 914b8d8015SPeter Meerwald [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z", 924b8d8015SPeter Meerwald [IIO_MOD_X_OR_Y] = "x|y", 934b8d8015SPeter Meerwald [IIO_MOD_X_OR_Z] = "x|z", 944b8d8015SPeter Meerwald [IIO_MOD_Y_OR_Z] = "y|z", 954b8d8015SPeter Meerwald [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z", 968f5879b2SJonathan Cameron [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)", 97cf82cb81SJonathan Cameron [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2", 98a980e046SJonathan Cameron [IIO_MOD_LIGHT_BOTH] = "both", 99a980e046SJonathan Cameron [IIO_MOD_LIGHT_IR] = "ir", 10021cd1fabSJon Brenner [IIO_MOD_LIGHT_CLEAR] = "clear", 10121cd1fabSJon Brenner [IIO_MOD_LIGHT_RED] = "red", 10221cd1fabSJon Brenner [IIO_MOD_LIGHT_GREEN] = "green", 10321cd1fabSJon Brenner [IIO_MOD_LIGHT_BLUE] = "blue", 1045082f405SSrinivas Pandruvada [IIO_MOD_QUATERNION] = "quaternion", 105638b43b3SPeter Meerwald [IIO_MOD_TEMP_AMBIENT] = "ambient", 106638b43b3SPeter Meerwald [IIO_MOD_TEMP_OBJECT] = "object", 10711b8ddabSReyad Attiyat [IIO_MOD_NORTH_MAGN] = "from_north_magnetic", 10811b8ddabSReyad Attiyat [IIO_MOD_NORTH_TRUE] = "from_north_true", 10911b8ddabSReyad Attiyat [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp", 11011b8ddabSReyad Attiyat [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp", 11155aebeb9SDaniel Baluta [IIO_MOD_RUNNING] = "running", 11255aebeb9SDaniel Baluta [IIO_MOD_JOGGING] = "jogging", 11355aebeb9SDaniel Baluta [IIO_MOD_WALKING] = "walking", 11455aebeb9SDaniel Baluta [IIO_MOD_STILL] = "still", 1155a1a9329SIrina Tirdea [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)", 1161ce87f21SLars-Peter Clausen [IIO_MOD_I] = "i", 1171ce87f21SLars-Peter Clausen [IIO_MOD_Q] = "q", 1188ff6b3bcSMatt Ranostay [IIO_MOD_CO2] = "co2", 1198ff6b3bcSMatt Ranostay [IIO_MOD_VOC] = "voc", 120a980e046SJonathan Cameron }; 121a980e046SJonathan Cameron 122a980e046SJonathan Cameron /* relies on pairs of these shared then separate */ 123a980e046SJonathan Cameron static const char * const iio_chan_info_postfix[] = { 124a980e046SJonathan Cameron [IIO_CHAN_INFO_RAW] = "raw", 125a980e046SJonathan Cameron [IIO_CHAN_INFO_PROCESSED] = "input", 126a980e046SJonathan Cameron [IIO_CHAN_INFO_SCALE] = "scale", 127a980e046SJonathan Cameron [IIO_CHAN_INFO_OFFSET] = "offset", 128a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBSCALE] = "calibscale", 129a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBBIAS] = "calibbias", 130a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK] = "peak_raw", 131a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale", 132a980e046SJonathan Cameron [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw", 133a980e046SJonathan Cameron [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw", 134a980e046SJonathan Cameron [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY] 135a980e046SJonathan Cameron = "filter_low_pass_3db_frequency", 1363f7f642bSMartin Fuzzey [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY] 1373f7f642bSMartin Fuzzey = "filter_high_pass_3db_frequency", 138a980e046SJonathan Cameron [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency", 139a6b12855SMichael Hennerich [IIO_CHAN_INFO_FREQUENCY] = "frequency", 140a6b12855SMichael Hennerich [IIO_CHAN_INFO_PHASE] = "phase", 141b65d6212SMichael Hennerich [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain", 1427c9ab035Ssrinivas pandruvada [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis", 143899d90bdSPeter Meerwald [IIO_CHAN_INFO_INT_TIME] = "integration_time", 144a88bfe78SIrina Tirdea [IIO_CHAN_INFO_ENABLE] = "en", 145bcdf28fbSIrina Tirdea [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight", 146d37f6836SIrina Tirdea [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight", 1472f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count", 1482f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time", 149c8a85854SVianney le Clément de Saint-Marcq [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity", 150faaa4495SIrina Tirdea [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio", 151a980e046SJonathan Cameron }; 152a980e046SJonathan Cameron 153a7e57dceSSachin Kamat /** 154a7e57dceSSachin Kamat * iio_find_channel_from_si() - get channel from its scan index 155a7e57dceSSachin Kamat * @indio_dev: device 156a7e57dceSSachin Kamat * @si: scan index to match 157a7e57dceSSachin Kamat */ 158a980e046SJonathan Cameron const struct iio_chan_spec 159a980e046SJonathan Cameron *iio_find_channel_from_si(struct iio_dev *indio_dev, int si) 160a980e046SJonathan Cameron { 161a980e046SJonathan Cameron int i; 162a980e046SJonathan Cameron 163a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) 164a980e046SJonathan Cameron if (indio_dev->channels[i].scan_index == si) 165a980e046SJonathan Cameron return &indio_dev->channels[i]; 166a980e046SJonathan Cameron return NULL; 167a980e046SJonathan Cameron } 168a980e046SJonathan Cameron 169a980e046SJonathan Cameron /* This turns up an awful lot */ 170a980e046SJonathan Cameron ssize_t iio_read_const_attr(struct device *dev, 171a980e046SJonathan Cameron struct device_attribute *attr, 172a980e046SJonathan Cameron char *buf) 173a980e046SJonathan Cameron { 174a980e046SJonathan Cameron return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string); 175a980e046SJonathan Cameron } 176a980e046SJonathan Cameron EXPORT_SYMBOL(iio_read_const_attr); 177a980e046SJonathan Cameron 178a980e046SJonathan Cameron static int __init iio_init(void) 179a980e046SJonathan Cameron { 180a980e046SJonathan Cameron int ret; 181a980e046SJonathan Cameron 182a980e046SJonathan Cameron /* Register sysfs bus */ 183a980e046SJonathan Cameron ret = bus_register(&iio_bus_type); 184a980e046SJonathan Cameron if (ret < 0) { 1853176dd5dSSachin Kamat pr_err("could not register bus type\n"); 186a980e046SJonathan Cameron goto error_nothing; 187a980e046SJonathan Cameron } 188a980e046SJonathan Cameron 189a980e046SJonathan Cameron ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio"); 190a980e046SJonathan Cameron if (ret < 0) { 1913176dd5dSSachin Kamat pr_err("failed to allocate char dev region\n"); 192a980e046SJonathan Cameron goto error_unregister_bus_type; 193a980e046SJonathan Cameron } 194a980e046SJonathan Cameron 195a980e046SJonathan Cameron iio_debugfs_dentry = debugfs_create_dir("iio", NULL); 196a980e046SJonathan Cameron 197a980e046SJonathan Cameron return 0; 198a980e046SJonathan Cameron 199a980e046SJonathan Cameron error_unregister_bus_type: 200a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 201a980e046SJonathan Cameron error_nothing: 202a980e046SJonathan Cameron return ret; 203a980e046SJonathan Cameron } 204a980e046SJonathan Cameron 205a980e046SJonathan Cameron static void __exit iio_exit(void) 206a980e046SJonathan Cameron { 207a980e046SJonathan Cameron if (iio_devt) 208a980e046SJonathan Cameron unregister_chrdev_region(iio_devt, IIO_DEV_MAX); 209a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 210a980e046SJonathan Cameron debugfs_remove(iio_debugfs_dentry); 211a980e046SJonathan Cameron } 212a980e046SJonathan Cameron 213a980e046SJonathan Cameron #if defined(CONFIG_DEBUG_FS) 214a980e046SJonathan Cameron static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf, 215a980e046SJonathan Cameron size_t count, loff_t *ppos) 216a980e046SJonathan Cameron { 217a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 218a980e046SJonathan Cameron char buf[20]; 219a980e046SJonathan Cameron unsigned val = 0; 220a980e046SJonathan Cameron ssize_t len; 221a980e046SJonathan Cameron int ret; 222a980e046SJonathan Cameron 223a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, 224a980e046SJonathan Cameron indio_dev->cached_reg_addr, 225a980e046SJonathan Cameron 0, &val); 226a980e046SJonathan Cameron if (ret) 227a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__); 228a980e046SJonathan Cameron 229a980e046SJonathan Cameron len = snprintf(buf, sizeof(buf), "0x%X\n", val); 230a980e046SJonathan Cameron 231a980e046SJonathan Cameron return simple_read_from_buffer(userbuf, count, ppos, buf, len); 232a980e046SJonathan Cameron } 233a980e046SJonathan Cameron 234a980e046SJonathan Cameron static ssize_t iio_debugfs_write_reg(struct file *file, 235a980e046SJonathan Cameron const char __user *userbuf, size_t count, loff_t *ppos) 236a980e046SJonathan Cameron { 237a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 238a980e046SJonathan Cameron unsigned reg, val; 239a980e046SJonathan Cameron char buf[80]; 240a980e046SJonathan Cameron int ret; 241a980e046SJonathan Cameron 242a980e046SJonathan Cameron count = min_t(size_t, count, (sizeof(buf)-1)); 243a980e046SJonathan Cameron if (copy_from_user(buf, userbuf, count)) 244a980e046SJonathan Cameron return -EFAULT; 245a980e046SJonathan Cameron 246a980e046SJonathan Cameron buf[count] = 0; 247a980e046SJonathan Cameron 248a980e046SJonathan Cameron ret = sscanf(buf, "%i %i", ®, &val); 249a980e046SJonathan Cameron 250a980e046SJonathan Cameron switch (ret) { 251a980e046SJonathan Cameron case 1: 252a980e046SJonathan Cameron indio_dev->cached_reg_addr = reg; 253a980e046SJonathan Cameron break; 254a980e046SJonathan Cameron case 2: 255a980e046SJonathan Cameron indio_dev->cached_reg_addr = reg; 256a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, reg, 257a980e046SJonathan Cameron val, NULL); 258a980e046SJonathan Cameron if (ret) { 259a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: write failed\n", 260a980e046SJonathan Cameron __func__); 261a980e046SJonathan Cameron return ret; 262a980e046SJonathan Cameron } 263a980e046SJonathan Cameron break; 264a980e046SJonathan Cameron default: 265a980e046SJonathan Cameron return -EINVAL; 266a980e046SJonathan Cameron } 267a980e046SJonathan Cameron 268a980e046SJonathan Cameron return count; 269a980e046SJonathan Cameron } 270a980e046SJonathan Cameron 271a980e046SJonathan Cameron static const struct file_operations iio_debugfs_reg_fops = { 2725a28c873SAxel Lin .open = simple_open, 273a980e046SJonathan Cameron .read = iio_debugfs_read_reg, 274a980e046SJonathan Cameron .write = iio_debugfs_write_reg, 275a980e046SJonathan Cameron }; 276a980e046SJonathan Cameron 277a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 278a980e046SJonathan Cameron { 279a980e046SJonathan Cameron debugfs_remove_recursive(indio_dev->debugfs_dentry); 280a980e046SJonathan Cameron } 281a980e046SJonathan Cameron 282a980e046SJonathan Cameron static int iio_device_register_debugfs(struct iio_dev *indio_dev) 283a980e046SJonathan Cameron { 284a980e046SJonathan Cameron struct dentry *d; 285a980e046SJonathan Cameron 286a980e046SJonathan Cameron if (indio_dev->info->debugfs_reg_access == NULL) 287a980e046SJonathan Cameron return 0; 288a980e046SJonathan Cameron 289abd5a2fbSAxel Lin if (!iio_debugfs_dentry) 290a980e046SJonathan Cameron return 0; 291a980e046SJonathan Cameron 292a980e046SJonathan Cameron indio_dev->debugfs_dentry = 293a980e046SJonathan Cameron debugfs_create_dir(dev_name(&indio_dev->dev), 294a980e046SJonathan Cameron iio_debugfs_dentry); 295a980e046SJonathan Cameron if (indio_dev->debugfs_dentry == NULL) { 296a980e046SJonathan Cameron dev_warn(indio_dev->dev.parent, 297a980e046SJonathan Cameron "Failed to create debugfs directory\n"); 298a980e046SJonathan Cameron return -EFAULT; 299a980e046SJonathan Cameron } 300a980e046SJonathan Cameron 301a980e046SJonathan Cameron d = debugfs_create_file("direct_reg_access", 0644, 302a980e046SJonathan Cameron indio_dev->debugfs_dentry, 303a980e046SJonathan Cameron indio_dev, &iio_debugfs_reg_fops); 304a980e046SJonathan Cameron if (!d) { 305a980e046SJonathan Cameron iio_device_unregister_debugfs(indio_dev); 306a980e046SJonathan Cameron return -ENOMEM; 307a980e046SJonathan Cameron } 308a980e046SJonathan Cameron 309a980e046SJonathan Cameron return 0; 310a980e046SJonathan Cameron } 311a980e046SJonathan Cameron #else 312a980e046SJonathan Cameron static int iio_device_register_debugfs(struct iio_dev *indio_dev) 313a980e046SJonathan Cameron { 314a980e046SJonathan Cameron return 0; 315a980e046SJonathan Cameron } 316a980e046SJonathan Cameron 317a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 318a980e046SJonathan Cameron { 319a980e046SJonathan Cameron } 320a980e046SJonathan Cameron #endif /* CONFIG_DEBUG_FS */ 321a980e046SJonathan Cameron 322a980e046SJonathan Cameron static ssize_t iio_read_channel_ext_info(struct device *dev, 323a980e046SJonathan Cameron struct device_attribute *attr, 324a980e046SJonathan Cameron char *buf) 325a980e046SJonathan Cameron { 326e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 327a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 328a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 329a980e046SJonathan Cameron 330a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 331a980e046SJonathan Cameron 332fc6d1139SMichael Hennerich return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf); 333a980e046SJonathan Cameron } 334a980e046SJonathan Cameron 335a980e046SJonathan Cameron static ssize_t iio_write_channel_ext_info(struct device *dev, 336a980e046SJonathan Cameron struct device_attribute *attr, 337a980e046SJonathan Cameron const char *buf, 338a980e046SJonathan Cameron size_t len) 339a980e046SJonathan Cameron { 340e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 341a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 342a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 343a980e046SJonathan Cameron 344a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 345a980e046SJonathan Cameron 346fc6d1139SMichael Hennerich return ext_info->write(indio_dev, ext_info->private, 347fc6d1139SMichael Hennerich this_attr->c, buf, len); 348a980e046SJonathan Cameron } 349a980e046SJonathan Cameron 3505212cc8aSLars-Peter Clausen ssize_t iio_enum_available_read(struct iio_dev *indio_dev, 3515212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 3525212cc8aSLars-Peter Clausen { 3535212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3545212cc8aSLars-Peter Clausen unsigned int i; 3555212cc8aSLars-Peter Clausen size_t len = 0; 3565212cc8aSLars-Peter Clausen 3575212cc8aSLars-Peter Clausen if (!e->num_items) 3585212cc8aSLars-Peter Clausen return 0; 3595212cc8aSLars-Peter Clausen 3605212cc8aSLars-Peter Clausen for (i = 0; i < e->num_items; ++i) 36174dcd439SLars-Peter Clausen len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]); 3625212cc8aSLars-Peter Clausen 3635212cc8aSLars-Peter Clausen /* replace last space with a newline */ 3645212cc8aSLars-Peter Clausen buf[len - 1] = '\n'; 3655212cc8aSLars-Peter Clausen 3665212cc8aSLars-Peter Clausen return len; 3675212cc8aSLars-Peter Clausen } 3685212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_available_read); 3695212cc8aSLars-Peter Clausen 3705212cc8aSLars-Peter Clausen ssize_t iio_enum_read(struct iio_dev *indio_dev, 3715212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 3725212cc8aSLars-Peter Clausen { 3735212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3745212cc8aSLars-Peter Clausen int i; 3755212cc8aSLars-Peter Clausen 3765212cc8aSLars-Peter Clausen if (!e->get) 3775212cc8aSLars-Peter Clausen return -EINVAL; 3785212cc8aSLars-Peter Clausen 3795212cc8aSLars-Peter Clausen i = e->get(indio_dev, chan); 3805212cc8aSLars-Peter Clausen if (i < 0) 3815212cc8aSLars-Peter Clausen return i; 3825212cc8aSLars-Peter Clausen else if (i >= e->num_items) 3835212cc8aSLars-Peter Clausen return -EINVAL; 3845212cc8aSLars-Peter Clausen 385598db581SKees Cook return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]); 3865212cc8aSLars-Peter Clausen } 3875212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_read); 3885212cc8aSLars-Peter Clausen 3895212cc8aSLars-Peter Clausen ssize_t iio_enum_write(struct iio_dev *indio_dev, 3905212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, const char *buf, 3915212cc8aSLars-Peter Clausen size_t len) 3925212cc8aSLars-Peter Clausen { 3935212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3945212cc8aSLars-Peter Clausen unsigned int i; 3955212cc8aSLars-Peter Clausen int ret; 3965212cc8aSLars-Peter Clausen 3975212cc8aSLars-Peter Clausen if (!e->set) 3985212cc8aSLars-Peter Clausen return -EINVAL; 3995212cc8aSLars-Peter Clausen 4005212cc8aSLars-Peter Clausen for (i = 0; i < e->num_items; i++) { 4015212cc8aSLars-Peter Clausen if (sysfs_streq(buf, e->items[i])) 4025212cc8aSLars-Peter Clausen break; 4035212cc8aSLars-Peter Clausen } 4045212cc8aSLars-Peter Clausen 4055212cc8aSLars-Peter Clausen if (i == e->num_items) 4065212cc8aSLars-Peter Clausen return -EINVAL; 4075212cc8aSLars-Peter Clausen 4085212cc8aSLars-Peter Clausen ret = e->set(indio_dev, chan, i); 4095212cc8aSLars-Peter Clausen return ret ? ret : len; 4105212cc8aSLars-Peter Clausen } 4115212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_write); 4125212cc8aSLars-Peter Clausen 4133661f3f5SLars-Peter Clausen /** 4143661f3f5SLars-Peter Clausen * iio_format_value() - Formats a IIO value into its string representation 4152498dcf6SCristina Opriceana * @buf: The buffer to which the formatted value gets written 4162498dcf6SCristina Opriceana * @type: One of the IIO_VAL_... constants. This decides how the val 4172498dcf6SCristina Opriceana * and val2 parameters are formatted. 4182498dcf6SCristina Opriceana * @size: Number of IIO value entries contained in vals 4192498dcf6SCristina Opriceana * @vals: Pointer to the values, exact meaning depends on the 4202498dcf6SCristina Opriceana * type parameter. 4212498dcf6SCristina Opriceana * 4222498dcf6SCristina Opriceana * Return: 0 by default, a negative number on failure or the 4232498dcf6SCristina Opriceana * total number of characters written for a type that belongs 4242498dcf6SCristina Opriceana * to the IIO_VAL_... constant. 4253661f3f5SLars-Peter Clausen */ 4269fbfb4b3SSrinivas Pandruvada ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals) 427a980e046SJonathan Cameron { 4287985e7c1SLars-Peter Clausen unsigned long long tmp; 42967eedba3SMichael Hennerich bool scale_db = false; 430a980e046SJonathan Cameron 4313661f3f5SLars-Peter Clausen switch (type) { 43267eedba3SMichael Hennerich case IIO_VAL_INT: 4339fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d\n", vals[0]); 43467eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_MICRO_DB: 43567eedba3SMichael Hennerich scale_db = true; 43667eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_MICRO: 4379fbfb4b3SSrinivas Pandruvada if (vals[1] < 0) 4388f57e4d9SMichal Nazarewicz return sprintf(buf, "-%d.%06u%s\n", abs(vals[0]), 4398f57e4d9SMichal Nazarewicz -vals[1], scale_db ? " dB" : ""); 440a980e046SJonathan Cameron else 4419fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1], 44267eedba3SMichael Hennerich scale_db ? " dB" : ""); 44367eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_NANO: 4449fbfb4b3SSrinivas Pandruvada if (vals[1] < 0) 4458f57e4d9SMichal Nazarewicz return sprintf(buf, "-%d.%09u\n", abs(vals[0]), 4469fbfb4b3SSrinivas Pandruvada -vals[1]); 447a980e046SJonathan Cameron else 4489fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 4497985e7c1SLars-Peter Clausen case IIO_VAL_FRACTIONAL: 4509fbfb4b3SSrinivas Pandruvada tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]); 4519fbfb4b3SSrinivas Pandruvada vals[1] = do_div(tmp, 1000000000LL); 4529fbfb4b3SSrinivas Pandruvada vals[0] = tmp; 4539fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 454103d9fb9SLars-Peter Clausen case IIO_VAL_FRACTIONAL_LOG2: 4559fbfb4b3SSrinivas Pandruvada tmp = (s64)vals[0] * 1000000000LL >> vals[1]; 4569fbfb4b3SSrinivas Pandruvada vals[1] = do_div(tmp, 1000000000LL); 4579fbfb4b3SSrinivas Pandruvada vals[0] = tmp; 4589fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 4599fbfb4b3SSrinivas Pandruvada case IIO_VAL_INT_MULTIPLE: 4609fbfb4b3SSrinivas Pandruvada { 4619fbfb4b3SSrinivas Pandruvada int i; 4629fbfb4b3SSrinivas Pandruvada int len = 0; 4639fbfb4b3SSrinivas Pandruvada 4649fbfb4b3SSrinivas Pandruvada for (i = 0; i < size; ++i) 4659fbfb4b3SSrinivas Pandruvada len += snprintf(&buf[len], PAGE_SIZE - len, "%d ", 4669fbfb4b3SSrinivas Pandruvada vals[i]); 4679fbfb4b3SSrinivas Pandruvada len += snprintf(&buf[len], PAGE_SIZE - len, "\n"); 4689fbfb4b3SSrinivas Pandruvada return len; 4699fbfb4b3SSrinivas Pandruvada } 47067eedba3SMichael Hennerich default: 471a980e046SJonathan Cameron return 0; 472a980e046SJonathan Cameron } 47367eedba3SMichael Hennerich } 4747d2c2acaSAndrew F. Davis EXPORT_SYMBOL_GPL(iio_format_value); 475a980e046SJonathan Cameron 4763661f3f5SLars-Peter Clausen static ssize_t iio_read_channel_info(struct device *dev, 4773661f3f5SLars-Peter Clausen struct device_attribute *attr, 4783661f3f5SLars-Peter Clausen char *buf) 4793661f3f5SLars-Peter Clausen { 4803661f3f5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 4813661f3f5SLars-Peter Clausen struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 4829fbfb4b3SSrinivas Pandruvada int vals[INDIO_MAX_RAW_ELEMENTS]; 4839fbfb4b3SSrinivas Pandruvada int ret; 4849fbfb4b3SSrinivas Pandruvada int val_len = 2; 4859fbfb4b3SSrinivas Pandruvada 4869fbfb4b3SSrinivas Pandruvada if (indio_dev->info->read_raw_multi) 4879fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c, 4889fbfb4b3SSrinivas Pandruvada INDIO_MAX_RAW_ELEMENTS, 4899fbfb4b3SSrinivas Pandruvada vals, &val_len, 4909fbfb4b3SSrinivas Pandruvada this_attr->address); 4919fbfb4b3SSrinivas Pandruvada else 4929fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw(indio_dev, this_attr->c, 4939fbfb4b3SSrinivas Pandruvada &vals[0], &vals[1], this_attr->address); 4943661f3f5SLars-Peter Clausen 4953661f3f5SLars-Peter Clausen if (ret < 0) 4963661f3f5SLars-Peter Clausen return ret; 4973661f3f5SLars-Peter Clausen 4989fbfb4b3SSrinivas Pandruvada return iio_format_value(buf, ret, val_len, vals); 4993661f3f5SLars-Peter Clausen } 5003661f3f5SLars-Peter Clausen 5016807d721SLars-Peter Clausen /** 5026807d721SLars-Peter Clausen * iio_str_to_fixpoint() - Parse a fixed-point number from a string 5036807d721SLars-Peter Clausen * @str: The string to parse 5046807d721SLars-Peter Clausen * @fract_mult: Multiplier for the first decimal place, should be a power of 10 5056807d721SLars-Peter Clausen * @integer: The integer part of the number 5066807d721SLars-Peter Clausen * @fract: The fractional part of the number 5076807d721SLars-Peter Clausen * 5086807d721SLars-Peter Clausen * Returns 0 on success, or a negative error code if the string could not be 5096807d721SLars-Peter Clausen * parsed. 5106807d721SLars-Peter Clausen */ 5116807d721SLars-Peter Clausen int iio_str_to_fixpoint(const char *str, int fract_mult, 5126807d721SLars-Peter Clausen int *integer, int *fract) 5136807d721SLars-Peter Clausen { 5146807d721SLars-Peter Clausen int i = 0, f = 0; 5156807d721SLars-Peter Clausen bool integer_part = true, negative = false; 5166807d721SLars-Peter Clausen 517f47dff32SSean Nyekjaer if (fract_mult == 0) { 518f47dff32SSean Nyekjaer *fract = 0; 519f47dff32SSean Nyekjaer 520f47dff32SSean Nyekjaer return kstrtoint(str, 0, integer); 521f47dff32SSean Nyekjaer } 522f47dff32SSean Nyekjaer 5236807d721SLars-Peter Clausen if (str[0] == '-') { 5246807d721SLars-Peter Clausen negative = true; 5256807d721SLars-Peter Clausen str++; 5266807d721SLars-Peter Clausen } else if (str[0] == '+') { 5276807d721SLars-Peter Clausen str++; 5286807d721SLars-Peter Clausen } 5296807d721SLars-Peter Clausen 5306807d721SLars-Peter Clausen while (*str) { 5316807d721SLars-Peter Clausen if ('0' <= *str && *str <= '9') { 5326807d721SLars-Peter Clausen if (integer_part) { 5336807d721SLars-Peter Clausen i = i * 10 + *str - '0'; 5346807d721SLars-Peter Clausen } else { 5356807d721SLars-Peter Clausen f += fract_mult * (*str - '0'); 5366807d721SLars-Peter Clausen fract_mult /= 10; 5376807d721SLars-Peter Clausen } 5386807d721SLars-Peter Clausen } else if (*str == '\n') { 5396807d721SLars-Peter Clausen if (*(str + 1) == '\0') 5406807d721SLars-Peter Clausen break; 5416807d721SLars-Peter Clausen else 5426807d721SLars-Peter Clausen return -EINVAL; 5436807d721SLars-Peter Clausen } else if (*str == '.' && integer_part) { 5446807d721SLars-Peter Clausen integer_part = false; 5456807d721SLars-Peter Clausen } else { 5466807d721SLars-Peter Clausen return -EINVAL; 5476807d721SLars-Peter Clausen } 5486807d721SLars-Peter Clausen str++; 5496807d721SLars-Peter Clausen } 5506807d721SLars-Peter Clausen 5516807d721SLars-Peter Clausen if (negative) { 5526807d721SLars-Peter Clausen if (i) 5536807d721SLars-Peter Clausen i = -i; 5546807d721SLars-Peter Clausen else 5556807d721SLars-Peter Clausen f = -f; 5566807d721SLars-Peter Clausen } 5576807d721SLars-Peter Clausen 5586807d721SLars-Peter Clausen *integer = i; 5596807d721SLars-Peter Clausen *fract = f; 5606807d721SLars-Peter Clausen 5616807d721SLars-Peter Clausen return 0; 5626807d721SLars-Peter Clausen } 5636807d721SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_str_to_fixpoint); 5646807d721SLars-Peter Clausen 565a980e046SJonathan Cameron static ssize_t iio_write_channel_info(struct device *dev, 566a980e046SJonathan Cameron struct device_attribute *attr, 567a980e046SJonathan Cameron const char *buf, 568a980e046SJonathan Cameron size_t len) 569a980e046SJonathan Cameron { 570e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 571a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 5726807d721SLars-Peter Clausen int ret, fract_mult = 100000; 5736807d721SLars-Peter Clausen int integer, fract; 574a980e046SJonathan Cameron 575a980e046SJonathan Cameron /* Assumes decimal - precision based on number of digits */ 576a980e046SJonathan Cameron if (!indio_dev->info->write_raw) 577a980e046SJonathan Cameron return -EINVAL; 578a980e046SJonathan Cameron 579a980e046SJonathan Cameron if (indio_dev->info->write_raw_get_fmt) 580a980e046SJonathan Cameron switch (indio_dev->info->write_raw_get_fmt(indio_dev, 581a980e046SJonathan Cameron this_attr->c, this_attr->address)) { 582f47dff32SSean Nyekjaer case IIO_VAL_INT: 583f47dff32SSean Nyekjaer fract_mult = 0; 584f47dff32SSean Nyekjaer break; 585a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_MICRO: 586a980e046SJonathan Cameron fract_mult = 100000; 587a980e046SJonathan Cameron break; 588a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_NANO: 589a980e046SJonathan Cameron fract_mult = 100000000; 590a980e046SJonathan Cameron break; 591a980e046SJonathan Cameron default: 592a980e046SJonathan Cameron return -EINVAL; 593a980e046SJonathan Cameron } 594a980e046SJonathan Cameron 5956807d721SLars-Peter Clausen ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract); 5966807d721SLars-Peter Clausen if (ret) 5976807d721SLars-Peter Clausen return ret; 598a980e046SJonathan Cameron 599a980e046SJonathan Cameron ret = indio_dev->info->write_raw(indio_dev, this_attr->c, 600a980e046SJonathan Cameron integer, fract, this_attr->address); 601a980e046SJonathan Cameron if (ret) 602a980e046SJonathan Cameron return ret; 603a980e046SJonathan Cameron 604a980e046SJonathan Cameron return len; 605a980e046SJonathan Cameron } 606a980e046SJonathan Cameron 607a980e046SJonathan Cameron static 608a980e046SJonathan Cameron int __iio_device_attr_init(struct device_attribute *dev_attr, 609a980e046SJonathan Cameron const char *postfix, 610a980e046SJonathan Cameron struct iio_chan_spec const *chan, 611a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 612a980e046SJonathan Cameron struct device_attribute *attr, 613a980e046SJonathan Cameron char *buf), 614a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 615a980e046SJonathan Cameron struct device_attribute *attr, 616a980e046SJonathan Cameron const char *buf, 617a980e046SJonathan Cameron size_t len), 6183704432fSJonathan Cameron enum iio_shared_by shared_by) 619a980e046SJonathan Cameron { 6203704432fSJonathan Cameron int ret = 0; 6217bbcf7e1SLars-Peter Clausen char *name = NULL; 6223704432fSJonathan Cameron char *full_postfix; 623a980e046SJonathan Cameron sysfs_attr_init(&dev_attr->attr); 624a980e046SJonathan Cameron 625a980e046SJonathan Cameron /* Build up postfix of <extend_name>_<modifier>_postfix */ 6263704432fSJonathan Cameron if (chan->modified && (shared_by == IIO_SEPARATE)) { 627a980e046SJonathan Cameron if (chan->extend_name) 628a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", 629a980e046SJonathan Cameron iio_modifier_names[chan 630a980e046SJonathan Cameron ->channel2], 631a980e046SJonathan Cameron chan->extend_name, 632a980e046SJonathan Cameron postfix); 633a980e046SJonathan Cameron else 634a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s", 635a980e046SJonathan Cameron iio_modifier_names[chan 636a980e046SJonathan Cameron ->channel2], 637a980e046SJonathan Cameron postfix); 638a980e046SJonathan Cameron } else { 63977bfa8baSLars-Peter Clausen if (chan->extend_name == NULL || shared_by != IIO_SEPARATE) 640a980e046SJonathan Cameron full_postfix = kstrdup(postfix, GFP_KERNEL); 641a980e046SJonathan Cameron else 642a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, 643a980e046SJonathan Cameron "%s_%s", 644a980e046SJonathan Cameron chan->extend_name, 645a980e046SJonathan Cameron postfix); 646a980e046SJonathan Cameron } 6473704432fSJonathan Cameron if (full_postfix == NULL) 6483704432fSJonathan Cameron return -ENOMEM; 649a980e046SJonathan Cameron 650a980e046SJonathan Cameron if (chan->differential) { /* Differential can not have modifier */ 6513704432fSJonathan Cameron switch (shared_by) { 652c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 6537bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 654c006ec83SJonathan Cameron break; 655c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 6567bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 657c006ec83SJonathan Cameron iio_direction[chan->output], 658c006ec83SJonathan Cameron full_postfix); 659c006ec83SJonathan Cameron break; 6603704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 6617bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s", 662a980e046SJonathan Cameron iio_direction[chan->output], 663a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 664a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 665a980e046SJonathan Cameron full_postfix); 6663704432fSJonathan Cameron break; 6673704432fSJonathan Cameron case IIO_SEPARATE: 6683704432fSJonathan Cameron if (!chan->indexed) { 669231bfe53SDan Carpenter WARN(1, "Differential channels must be indexed\n"); 6703704432fSJonathan Cameron ret = -EINVAL; 6713704432fSJonathan Cameron goto error_free_full_postfix; 6723704432fSJonathan Cameron } 6737bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, 6743704432fSJonathan Cameron "%s_%s%d-%s%d_%s", 675a980e046SJonathan Cameron iio_direction[chan->output], 676a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 677a980e046SJonathan Cameron chan->channel, 678a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 679a980e046SJonathan Cameron chan->channel2, 680a980e046SJonathan Cameron full_postfix); 6813704432fSJonathan Cameron break; 682a980e046SJonathan Cameron } 683a980e046SJonathan Cameron } else { /* Single ended */ 6843704432fSJonathan Cameron switch (shared_by) { 685c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 6867bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 687c006ec83SJonathan Cameron break; 688c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 6897bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 690c006ec83SJonathan Cameron iio_direction[chan->output], 691c006ec83SJonathan Cameron full_postfix); 692c006ec83SJonathan Cameron break; 6933704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 6947bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 695a980e046SJonathan Cameron iio_direction[chan->output], 696a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 697a980e046SJonathan Cameron full_postfix); 6983704432fSJonathan Cameron break; 6993704432fSJonathan Cameron 7003704432fSJonathan Cameron case IIO_SEPARATE: 7013704432fSJonathan Cameron if (chan->indexed) 7027bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s%d_%s", 703a980e046SJonathan Cameron iio_direction[chan->output], 704a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 705a980e046SJonathan Cameron chan->channel, 706a980e046SJonathan Cameron full_postfix); 707a980e046SJonathan Cameron else 7087bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 709a980e046SJonathan Cameron iio_direction[chan->output], 710a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 711a980e046SJonathan Cameron full_postfix); 7123704432fSJonathan Cameron break; 7133704432fSJonathan Cameron } 714a980e046SJonathan Cameron } 7157bbcf7e1SLars-Peter Clausen if (name == NULL) { 716a980e046SJonathan Cameron ret = -ENOMEM; 717a980e046SJonathan Cameron goto error_free_full_postfix; 718a980e046SJonathan Cameron } 7197bbcf7e1SLars-Peter Clausen dev_attr->attr.name = name; 720a980e046SJonathan Cameron 721a980e046SJonathan Cameron if (readfunc) { 722a980e046SJonathan Cameron dev_attr->attr.mode |= S_IRUGO; 723a980e046SJonathan Cameron dev_attr->show = readfunc; 724a980e046SJonathan Cameron } 725a980e046SJonathan Cameron 726a980e046SJonathan Cameron if (writefunc) { 727a980e046SJonathan Cameron dev_attr->attr.mode |= S_IWUSR; 728a980e046SJonathan Cameron dev_attr->store = writefunc; 729a980e046SJonathan Cameron } 7307bbcf7e1SLars-Peter Clausen 731a980e046SJonathan Cameron error_free_full_postfix: 732a980e046SJonathan Cameron kfree(full_postfix); 7333704432fSJonathan Cameron 734a980e046SJonathan Cameron return ret; 735a980e046SJonathan Cameron } 736a980e046SJonathan Cameron 737a980e046SJonathan Cameron static void __iio_device_attr_deinit(struct device_attribute *dev_attr) 738a980e046SJonathan Cameron { 739a980e046SJonathan Cameron kfree(dev_attr->attr.name); 740a980e046SJonathan Cameron } 741a980e046SJonathan Cameron 742a980e046SJonathan Cameron int __iio_add_chan_devattr(const char *postfix, 743a980e046SJonathan Cameron struct iio_chan_spec const *chan, 744a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 745a980e046SJonathan Cameron struct device_attribute *attr, 746a980e046SJonathan Cameron char *buf), 747a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 748a980e046SJonathan Cameron struct device_attribute *attr, 749a980e046SJonathan Cameron const char *buf, 750a980e046SJonathan Cameron size_t len), 751a980e046SJonathan Cameron u64 mask, 7523704432fSJonathan Cameron enum iio_shared_by shared_by, 753a980e046SJonathan Cameron struct device *dev, 754a980e046SJonathan Cameron struct list_head *attr_list) 755a980e046SJonathan Cameron { 756a980e046SJonathan Cameron int ret; 757a980e046SJonathan Cameron struct iio_dev_attr *iio_attr, *t; 758a980e046SJonathan Cameron 759670c1103SSachin Kamat iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL); 76092825ff9SHartmut Knaack if (iio_attr == NULL) 76192825ff9SHartmut Knaack return -ENOMEM; 762a980e046SJonathan Cameron ret = __iio_device_attr_init(&iio_attr->dev_attr, 763a980e046SJonathan Cameron postfix, chan, 7643704432fSJonathan Cameron readfunc, writefunc, shared_by); 765a980e046SJonathan Cameron if (ret) 766a980e046SJonathan Cameron goto error_iio_dev_attr_free; 767a980e046SJonathan Cameron iio_attr->c = chan; 768a980e046SJonathan Cameron iio_attr->address = mask; 769a980e046SJonathan Cameron list_for_each_entry(t, attr_list, l) 770a980e046SJonathan Cameron if (strcmp(t->dev_attr.attr.name, 771a980e046SJonathan Cameron iio_attr->dev_attr.attr.name) == 0) { 7723704432fSJonathan Cameron if (shared_by == IIO_SEPARATE) 773a980e046SJonathan Cameron dev_err(dev, "tried to double register : %s\n", 774a980e046SJonathan Cameron t->dev_attr.attr.name); 775a980e046SJonathan Cameron ret = -EBUSY; 776a980e046SJonathan Cameron goto error_device_attr_deinit; 777a980e046SJonathan Cameron } 778a980e046SJonathan Cameron list_add(&iio_attr->l, attr_list); 779a980e046SJonathan Cameron 780a980e046SJonathan Cameron return 0; 781a980e046SJonathan Cameron 782a980e046SJonathan Cameron error_device_attr_deinit: 783a980e046SJonathan Cameron __iio_device_attr_deinit(&iio_attr->dev_attr); 784a980e046SJonathan Cameron error_iio_dev_attr_free: 785a980e046SJonathan Cameron kfree(iio_attr); 786a980e046SJonathan Cameron return ret; 787a980e046SJonathan Cameron } 788a980e046SJonathan Cameron 7893704432fSJonathan Cameron static int iio_device_add_info_mask_type(struct iio_dev *indio_dev, 7903704432fSJonathan Cameron struct iio_chan_spec const *chan, 7913704432fSJonathan Cameron enum iio_shared_by shared_by, 7923704432fSJonathan Cameron const long *infomask) 7933704432fSJonathan Cameron { 7943704432fSJonathan Cameron int i, ret, attrcount = 0; 7953704432fSJonathan Cameron 7963704432fSJonathan Cameron for_each_set_bit(i, infomask, sizeof(infomask)*8) { 797ef4b4856SJonathan Cameron if (i >= ARRAY_SIZE(iio_chan_info_postfix)) 798ef4b4856SJonathan Cameron return -EINVAL; 7993704432fSJonathan Cameron ret = __iio_add_chan_devattr(iio_chan_info_postfix[i], 8003704432fSJonathan Cameron chan, 8013704432fSJonathan Cameron &iio_read_channel_info, 8023704432fSJonathan Cameron &iio_write_channel_info, 8033704432fSJonathan Cameron i, 8043704432fSJonathan Cameron shared_by, 8053704432fSJonathan Cameron &indio_dev->dev, 8063704432fSJonathan Cameron &indio_dev->channel_attr_list); 8073704432fSJonathan Cameron if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) 8083704432fSJonathan Cameron continue; 8093704432fSJonathan Cameron else if (ret < 0) 8103704432fSJonathan Cameron return ret; 8113704432fSJonathan Cameron attrcount++; 8123704432fSJonathan Cameron } 8133704432fSJonathan Cameron 8143704432fSJonathan Cameron return attrcount; 8153704432fSJonathan Cameron } 8163704432fSJonathan Cameron 817a980e046SJonathan Cameron static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev, 818a980e046SJonathan Cameron struct iio_chan_spec const *chan) 819a980e046SJonathan Cameron { 820a980e046SJonathan Cameron int ret, attrcount = 0; 821a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 822a980e046SJonathan Cameron 823a980e046SJonathan Cameron if (chan->channel < 0) 824a980e046SJonathan Cameron return 0; 8253704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 8263704432fSJonathan Cameron IIO_SEPARATE, 8273704432fSJonathan Cameron &chan->info_mask_separate); 8288655cc49SJonathan Cameron if (ret < 0) 8293704432fSJonathan Cameron return ret; 8303704432fSJonathan Cameron attrcount += ret; 8313704432fSJonathan Cameron 8323704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 8333704432fSJonathan Cameron IIO_SHARED_BY_TYPE, 8343704432fSJonathan Cameron &chan->info_mask_shared_by_type); 8353704432fSJonathan Cameron if (ret < 0) 8363704432fSJonathan Cameron return ret; 8373704432fSJonathan Cameron attrcount += ret; 838a980e046SJonathan Cameron 839c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 840c006ec83SJonathan Cameron IIO_SHARED_BY_DIR, 841c006ec83SJonathan Cameron &chan->info_mask_shared_by_dir); 842c006ec83SJonathan Cameron if (ret < 0) 843c006ec83SJonathan Cameron return ret; 844c006ec83SJonathan Cameron attrcount += ret; 845c006ec83SJonathan Cameron 846c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 847c006ec83SJonathan Cameron IIO_SHARED_BY_ALL, 848c006ec83SJonathan Cameron &chan->info_mask_shared_by_all); 849c006ec83SJonathan Cameron if (ret < 0) 850c006ec83SJonathan Cameron return ret; 851c006ec83SJonathan Cameron attrcount += ret; 852c006ec83SJonathan Cameron 853a980e046SJonathan Cameron if (chan->ext_info) { 854a980e046SJonathan Cameron unsigned int i = 0; 855a980e046SJonathan Cameron for (ext_info = chan->ext_info; ext_info->name; ext_info++) { 856a980e046SJonathan Cameron ret = __iio_add_chan_devattr(ext_info->name, 857a980e046SJonathan Cameron chan, 858a980e046SJonathan Cameron ext_info->read ? 859a980e046SJonathan Cameron &iio_read_channel_ext_info : NULL, 860a980e046SJonathan Cameron ext_info->write ? 861a980e046SJonathan Cameron &iio_write_channel_ext_info : NULL, 862a980e046SJonathan Cameron i, 863a980e046SJonathan Cameron ext_info->shared, 864a980e046SJonathan Cameron &indio_dev->dev, 865a980e046SJonathan Cameron &indio_dev->channel_attr_list); 866a980e046SJonathan Cameron i++; 867a980e046SJonathan Cameron if (ret == -EBUSY && ext_info->shared) 868a980e046SJonathan Cameron continue; 869a980e046SJonathan Cameron 870a980e046SJonathan Cameron if (ret) 8713704432fSJonathan Cameron return ret; 872a980e046SJonathan Cameron 873a980e046SJonathan Cameron attrcount++; 874a980e046SJonathan Cameron } 875a980e046SJonathan Cameron } 876a980e046SJonathan Cameron 8773704432fSJonathan Cameron return attrcount; 878a980e046SJonathan Cameron } 879a980e046SJonathan Cameron 88084088ebdSLars-Peter Clausen /** 88184088ebdSLars-Peter Clausen * iio_free_chan_devattr_list() - Free a list of IIO device attributes 88284088ebdSLars-Peter Clausen * @attr_list: List of IIO device attributes 88384088ebdSLars-Peter Clausen * 88484088ebdSLars-Peter Clausen * This function frees the memory allocated for each of the IIO device 885c1b03ab5SMartin Fuzzey * attributes in the list. 88684088ebdSLars-Peter Clausen */ 88784088ebdSLars-Peter Clausen void iio_free_chan_devattr_list(struct list_head *attr_list) 888a980e046SJonathan Cameron { 88984088ebdSLars-Peter Clausen struct iio_dev_attr *p, *n; 89084088ebdSLars-Peter Clausen 89184088ebdSLars-Peter Clausen list_for_each_entry_safe(p, n, attr_list, l) { 892a980e046SJonathan Cameron kfree(p->dev_attr.attr.name); 893c1b03ab5SMartin Fuzzey list_del(&p->l); 894a980e046SJonathan Cameron kfree(p); 895a980e046SJonathan Cameron } 89684088ebdSLars-Peter Clausen } 897a980e046SJonathan Cameron 898a980e046SJonathan Cameron static ssize_t iio_show_dev_name(struct device *dev, 899a980e046SJonathan Cameron struct device_attribute *attr, 900a980e046SJonathan Cameron char *buf) 901a980e046SJonathan Cameron { 902e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 903598db581SKees Cook return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name); 904a980e046SJonathan Cameron } 905a980e046SJonathan Cameron 906a980e046SJonathan Cameron static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL); 907a980e046SJonathan Cameron 908a980e046SJonathan Cameron static int iio_device_register_sysfs(struct iio_dev *indio_dev) 909a980e046SJonathan Cameron { 910a980e046SJonathan Cameron int i, ret = 0, attrcount, attrn, attrcount_orig = 0; 91184088ebdSLars-Peter Clausen struct iio_dev_attr *p; 912a980e046SJonathan Cameron struct attribute **attr; 913a980e046SJonathan Cameron 914a980e046SJonathan Cameron /* First count elements in any existing group */ 915a980e046SJonathan Cameron if (indio_dev->info->attrs) { 916a980e046SJonathan Cameron attr = indio_dev->info->attrs->attrs; 917a980e046SJonathan Cameron while (*attr++ != NULL) 918a980e046SJonathan Cameron attrcount_orig++; 919a980e046SJonathan Cameron } 920a980e046SJonathan Cameron attrcount = attrcount_orig; 921a980e046SJonathan Cameron /* 922a980e046SJonathan Cameron * New channel registration method - relies on the fact a group does 923d25b3808SPeter Meerwald * not need to be initialized if its name is NULL. 924a980e046SJonathan Cameron */ 925a980e046SJonathan Cameron if (indio_dev->channels) 926a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) { 927a980e046SJonathan Cameron ret = iio_device_add_channel_sysfs(indio_dev, 928a980e046SJonathan Cameron &indio_dev 929a980e046SJonathan Cameron ->channels[i]); 930a980e046SJonathan Cameron if (ret < 0) 931a980e046SJonathan Cameron goto error_clear_attrs; 932a980e046SJonathan Cameron attrcount += ret; 933a980e046SJonathan Cameron } 934a980e046SJonathan Cameron 935a980e046SJonathan Cameron if (indio_dev->name) 936a980e046SJonathan Cameron attrcount++; 937a980e046SJonathan Cameron 938a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1, 939a980e046SJonathan Cameron sizeof(indio_dev->chan_attr_group.attrs[0]), 940a980e046SJonathan Cameron GFP_KERNEL); 941a980e046SJonathan Cameron if (indio_dev->chan_attr_group.attrs == NULL) { 942a980e046SJonathan Cameron ret = -ENOMEM; 943a980e046SJonathan Cameron goto error_clear_attrs; 944a980e046SJonathan Cameron } 945a980e046SJonathan Cameron /* Copy across original attributes */ 946a980e046SJonathan Cameron if (indio_dev->info->attrs) 947a980e046SJonathan Cameron memcpy(indio_dev->chan_attr_group.attrs, 948a980e046SJonathan Cameron indio_dev->info->attrs->attrs, 949a980e046SJonathan Cameron sizeof(indio_dev->chan_attr_group.attrs[0]) 950a980e046SJonathan Cameron *attrcount_orig); 951a980e046SJonathan Cameron attrn = attrcount_orig; 952a980e046SJonathan Cameron /* Add all elements from the list. */ 953a980e046SJonathan Cameron list_for_each_entry(p, &indio_dev->channel_attr_list, l) 954a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr; 955a980e046SJonathan Cameron if (indio_dev->name) 956a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr; 957a980e046SJonathan Cameron 958a980e046SJonathan Cameron indio_dev->groups[indio_dev->groupcounter++] = 959a980e046SJonathan Cameron &indio_dev->chan_attr_group; 960a980e046SJonathan Cameron 961a980e046SJonathan Cameron return 0; 962a980e046SJonathan Cameron 963a980e046SJonathan Cameron error_clear_attrs: 96484088ebdSLars-Peter Clausen iio_free_chan_devattr_list(&indio_dev->channel_attr_list); 965a980e046SJonathan Cameron 966a980e046SJonathan Cameron return ret; 967a980e046SJonathan Cameron } 968a980e046SJonathan Cameron 969a980e046SJonathan Cameron static void iio_device_unregister_sysfs(struct iio_dev *indio_dev) 970a980e046SJonathan Cameron { 971a980e046SJonathan Cameron 97284088ebdSLars-Peter Clausen iio_free_chan_devattr_list(&indio_dev->channel_attr_list); 973a980e046SJonathan Cameron kfree(indio_dev->chan_attr_group.attrs); 974c1b03ab5SMartin Fuzzey indio_dev->chan_attr_group.attrs = NULL; 975a980e046SJonathan Cameron } 976a980e046SJonathan Cameron 977a980e046SJonathan Cameron static void iio_dev_release(struct device *device) 978a980e046SJonathan Cameron { 979e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(device); 980735ad074SVladimir Barinov if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED)) 981a980e046SJonathan Cameron iio_device_unregister_trigger_consumer(indio_dev); 982a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 983a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 984e407fd65SLars-Peter Clausen 9859e69c935SLars-Peter Clausen iio_buffer_put(indio_dev->buffer); 9869e69c935SLars-Peter Clausen 987e407fd65SLars-Peter Clausen ida_simple_remove(&iio_ida, indio_dev->id); 988e407fd65SLars-Peter Clausen kfree(indio_dev); 989a980e046SJonathan Cameron } 990a980e046SJonathan Cameron 99117d82b47SGuenter Roeck struct device_type iio_device_type = { 992a980e046SJonathan Cameron .name = "iio_device", 993a980e046SJonathan Cameron .release = iio_dev_release, 994a980e046SJonathan Cameron }; 995a980e046SJonathan Cameron 996a7e57dceSSachin Kamat /** 997a7e57dceSSachin Kamat * iio_device_alloc() - allocate an iio_dev from a driver 998a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 999a7e57dceSSachin Kamat **/ 10007cbb7537SLars-Peter Clausen struct iio_dev *iio_device_alloc(int sizeof_priv) 1001a980e046SJonathan Cameron { 1002a980e046SJonathan Cameron struct iio_dev *dev; 1003a980e046SJonathan Cameron size_t alloc_size; 1004a980e046SJonathan Cameron 1005a980e046SJonathan Cameron alloc_size = sizeof(struct iio_dev); 1006a980e046SJonathan Cameron if (sizeof_priv) { 1007a980e046SJonathan Cameron alloc_size = ALIGN(alloc_size, IIO_ALIGN); 1008a980e046SJonathan Cameron alloc_size += sizeof_priv; 1009a980e046SJonathan Cameron } 1010a980e046SJonathan Cameron /* ensure 32-byte alignment of whole construct ? */ 1011a980e046SJonathan Cameron alloc_size += IIO_ALIGN - 1; 1012a980e046SJonathan Cameron 1013a980e046SJonathan Cameron dev = kzalloc(alloc_size, GFP_KERNEL); 1014a980e046SJonathan Cameron 1015a980e046SJonathan Cameron if (dev) { 1016a980e046SJonathan Cameron dev->dev.groups = dev->groups; 101717d82b47SGuenter Roeck dev->dev.type = &iio_device_type; 1018a980e046SJonathan Cameron dev->dev.bus = &iio_bus_type; 1019a980e046SJonathan Cameron device_initialize(&dev->dev); 1020a980e046SJonathan Cameron dev_set_drvdata(&dev->dev, (void *)dev); 1021a980e046SJonathan Cameron mutex_init(&dev->mlock); 1022a980e046SJonathan Cameron mutex_init(&dev->info_exist_lock); 1023e407fd65SLars-Peter Clausen INIT_LIST_HEAD(&dev->channel_attr_list); 1024a980e046SJonathan Cameron 1025a980e046SJonathan Cameron dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL); 1026a980e046SJonathan Cameron if (dev->id < 0) { 1027a980e046SJonathan Cameron /* cannot use a dev_err as the name isn't available */ 10283176dd5dSSachin Kamat pr_err("failed to get device id\n"); 1029a980e046SJonathan Cameron kfree(dev); 1030a980e046SJonathan Cameron return NULL; 1031a980e046SJonathan Cameron } 1032a980e046SJonathan Cameron dev_set_name(&dev->dev, "iio:device%d", dev->id); 103384b36ce5SJonathan Cameron INIT_LIST_HEAD(&dev->buffer_list); 1034a980e046SJonathan Cameron } 1035a980e046SJonathan Cameron 1036a980e046SJonathan Cameron return dev; 1037a980e046SJonathan Cameron } 10387cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_alloc); 1039a980e046SJonathan Cameron 1040a7e57dceSSachin Kamat /** 1041a7e57dceSSachin Kamat * iio_device_free() - free an iio_dev from a driver 1042a7e57dceSSachin Kamat * @dev: the iio_dev associated with the device 1043a7e57dceSSachin Kamat **/ 10447cbb7537SLars-Peter Clausen void iio_device_free(struct iio_dev *dev) 1045a980e046SJonathan Cameron { 1046e407fd65SLars-Peter Clausen if (dev) 1047e407fd65SLars-Peter Clausen put_device(&dev->dev); 1048a980e046SJonathan Cameron } 10497cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_free); 1050a980e046SJonathan Cameron 10519dabaf5eSGrygorii Strashko static void devm_iio_device_release(struct device *dev, void *res) 10529dabaf5eSGrygorii Strashko { 10539dabaf5eSGrygorii Strashko iio_device_free(*(struct iio_dev **)res); 10549dabaf5eSGrygorii Strashko } 10559dabaf5eSGrygorii Strashko 10569dabaf5eSGrygorii Strashko static int devm_iio_device_match(struct device *dev, void *res, void *data) 10579dabaf5eSGrygorii Strashko { 10589dabaf5eSGrygorii Strashko struct iio_dev **r = res; 10599dabaf5eSGrygorii Strashko if (!r || !*r) { 10609dabaf5eSGrygorii Strashko WARN_ON(!r || !*r); 10619dabaf5eSGrygorii Strashko return 0; 10629dabaf5eSGrygorii Strashko } 10639dabaf5eSGrygorii Strashko return *r == data; 10649dabaf5eSGrygorii Strashko } 10659dabaf5eSGrygorii Strashko 1066a7e57dceSSachin Kamat /** 1067a7e57dceSSachin Kamat * devm_iio_device_alloc - Resource-managed iio_device_alloc() 1068a7e57dceSSachin Kamat * @dev: Device to allocate iio_dev for 1069a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 1070a7e57dceSSachin Kamat * 1071a7e57dceSSachin Kamat * Managed iio_device_alloc. iio_dev allocated with this function is 1072a7e57dceSSachin Kamat * automatically freed on driver detach. 1073a7e57dceSSachin Kamat * 1074a7e57dceSSachin Kamat * If an iio_dev allocated with this function needs to be freed separately, 1075a7e57dceSSachin Kamat * devm_iio_device_free() must be used. 1076a7e57dceSSachin Kamat * 1077a7e57dceSSachin Kamat * RETURNS: 1078a7e57dceSSachin Kamat * Pointer to allocated iio_dev on success, NULL on failure. 1079a7e57dceSSachin Kamat */ 10809dabaf5eSGrygorii Strashko struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv) 10819dabaf5eSGrygorii Strashko { 10829dabaf5eSGrygorii Strashko struct iio_dev **ptr, *iio_dev; 10839dabaf5eSGrygorii Strashko 10849dabaf5eSGrygorii Strashko ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr), 10859dabaf5eSGrygorii Strashko GFP_KERNEL); 10869dabaf5eSGrygorii Strashko if (!ptr) 10879dabaf5eSGrygorii Strashko return NULL; 10889dabaf5eSGrygorii Strashko 10899dabaf5eSGrygorii Strashko iio_dev = iio_device_alloc(sizeof_priv); 10909dabaf5eSGrygorii Strashko if (iio_dev) { 10919dabaf5eSGrygorii Strashko *ptr = iio_dev; 10929dabaf5eSGrygorii Strashko devres_add(dev, ptr); 10939dabaf5eSGrygorii Strashko } else { 10949dabaf5eSGrygorii Strashko devres_free(ptr); 10959dabaf5eSGrygorii Strashko } 10969dabaf5eSGrygorii Strashko 10979dabaf5eSGrygorii Strashko return iio_dev; 10989dabaf5eSGrygorii Strashko } 10999dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_alloc); 11009dabaf5eSGrygorii Strashko 1101a7e57dceSSachin Kamat /** 1102a7e57dceSSachin Kamat * devm_iio_device_free - Resource-managed iio_device_free() 1103a7e57dceSSachin Kamat * @dev: Device this iio_dev belongs to 1104a7e57dceSSachin Kamat * @iio_dev: the iio_dev associated with the device 1105a7e57dceSSachin Kamat * 1106a7e57dceSSachin Kamat * Free iio_dev allocated with devm_iio_device_alloc(). 1107a7e57dceSSachin Kamat */ 11089dabaf5eSGrygorii Strashko void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev) 11099dabaf5eSGrygorii Strashko { 11109dabaf5eSGrygorii Strashko int rc; 11119dabaf5eSGrygorii Strashko 11129dabaf5eSGrygorii Strashko rc = devres_release(dev, devm_iio_device_release, 11139dabaf5eSGrygorii Strashko devm_iio_device_match, iio_dev); 11149dabaf5eSGrygorii Strashko WARN_ON(rc); 11159dabaf5eSGrygorii Strashko } 11169dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_free); 11179dabaf5eSGrygorii Strashko 1118a980e046SJonathan Cameron /** 1119a980e046SJonathan Cameron * iio_chrdev_open() - chrdev file open for buffer access and ioctls 11202498dcf6SCristina Opriceana * @inode: Inode structure for identifying the device in the file system 11212498dcf6SCristina Opriceana * @filp: File structure for iio device used to keep and later access 11222498dcf6SCristina Opriceana * private data 11232498dcf6SCristina Opriceana * 11242498dcf6SCristina Opriceana * Return: 0 on success or -EBUSY if the device is already opened 1125a980e046SJonathan Cameron **/ 1126a980e046SJonathan Cameron static int iio_chrdev_open(struct inode *inode, struct file *filp) 1127a980e046SJonathan Cameron { 1128a980e046SJonathan Cameron struct iio_dev *indio_dev = container_of(inode->i_cdev, 1129a980e046SJonathan Cameron struct iio_dev, chrdev); 1130a980e046SJonathan Cameron 1131a980e046SJonathan Cameron if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags)) 1132a980e046SJonathan Cameron return -EBUSY; 1133a980e046SJonathan Cameron 1134cadc2125SLars-Peter Clausen iio_device_get(indio_dev); 1135cadc2125SLars-Peter Clausen 1136a980e046SJonathan Cameron filp->private_data = indio_dev; 1137a980e046SJonathan Cameron 1138a980e046SJonathan Cameron return 0; 1139a980e046SJonathan Cameron } 1140a980e046SJonathan Cameron 1141a980e046SJonathan Cameron /** 1142a980e046SJonathan Cameron * iio_chrdev_release() - chrdev file close buffer access and ioctls 11432498dcf6SCristina Opriceana * @inode: Inode structure pointer for the char device 11442498dcf6SCristina Opriceana * @filp: File structure pointer for the char device 11452498dcf6SCristina Opriceana * 11462498dcf6SCristina Opriceana * Return: 0 for successful release 11472498dcf6SCristina Opriceana */ 1148a980e046SJonathan Cameron static int iio_chrdev_release(struct inode *inode, struct file *filp) 1149a980e046SJonathan Cameron { 1150a980e046SJonathan Cameron struct iio_dev *indio_dev = container_of(inode->i_cdev, 1151a980e046SJonathan Cameron struct iio_dev, chrdev); 1152a980e046SJonathan Cameron clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags); 1153cadc2125SLars-Peter Clausen iio_device_put(indio_dev); 1154cadc2125SLars-Peter Clausen 1155a980e046SJonathan Cameron return 0; 1156a980e046SJonathan Cameron } 1157a980e046SJonathan Cameron 1158a980e046SJonathan Cameron /* Somewhat of a cross file organization violation - ioctls here are actually 1159a980e046SJonathan Cameron * event related */ 1160a980e046SJonathan Cameron static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 1161a980e046SJonathan Cameron { 1162a980e046SJonathan Cameron struct iio_dev *indio_dev = filp->private_data; 1163a980e046SJonathan Cameron int __user *ip = (int __user *)arg; 1164a980e046SJonathan Cameron int fd; 1165a980e046SJonathan Cameron 1166f18e7a06SLars-Peter Clausen if (!indio_dev->info) 1167f18e7a06SLars-Peter Clausen return -ENODEV; 1168f18e7a06SLars-Peter Clausen 1169a980e046SJonathan Cameron if (cmd == IIO_GET_EVENT_FD_IOCTL) { 1170a980e046SJonathan Cameron fd = iio_event_getfd(indio_dev); 11713f9059b7SLinus Walleij if (fd < 0) 11723f9059b7SLinus Walleij return fd; 1173a980e046SJonathan Cameron if (copy_to_user(ip, &fd, sizeof(fd))) 1174a980e046SJonathan Cameron return -EFAULT; 1175a980e046SJonathan Cameron return 0; 1176a980e046SJonathan Cameron } 1177a980e046SJonathan Cameron return -EINVAL; 1178a980e046SJonathan Cameron } 1179a980e046SJonathan Cameron 1180a980e046SJonathan Cameron static const struct file_operations iio_buffer_fileops = { 1181a980e046SJonathan Cameron .read = iio_buffer_read_first_n_outer_addr, 1182a980e046SJonathan Cameron .release = iio_chrdev_release, 1183a980e046SJonathan Cameron .open = iio_chrdev_open, 1184a980e046SJonathan Cameron .poll = iio_buffer_poll_addr, 1185a980e046SJonathan Cameron .owner = THIS_MODULE, 1186a980e046SJonathan Cameron .llseek = noop_llseek, 1187a980e046SJonathan Cameron .unlocked_ioctl = iio_ioctl, 1188a980e046SJonathan Cameron .compat_ioctl = iio_ioctl, 1189a980e046SJonathan Cameron }; 1190a980e046SJonathan Cameron 11918f5d8727SVlad Dogaru static int iio_check_unique_scan_index(struct iio_dev *indio_dev) 11928f5d8727SVlad Dogaru { 11938f5d8727SVlad Dogaru int i, j; 11948f5d8727SVlad Dogaru const struct iio_chan_spec *channels = indio_dev->channels; 11958f5d8727SVlad Dogaru 11968f5d8727SVlad Dogaru if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES)) 11978f5d8727SVlad Dogaru return 0; 11988f5d8727SVlad Dogaru 11998f5d8727SVlad Dogaru for (i = 0; i < indio_dev->num_channels - 1; i++) { 12008f5d8727SVlad Dogaru if (channels[i].scan_index < 0) 12018f5d8727SVlad Dogaru continue; 12028f5d8727SVlad Dogaru for (j = i + 1; j < indio_dev->num_channels; j++) 12038f5d8727SVlad Dogaru if (channels[i].scan_index == channels[j].scan_index) { 12048f5d8727SVlad Dogaru dev_err(&indio_dev->dev, 12058f5d8727SVlad Dogaru "Duplicate scan index %d\n", 12068f5d8727SVlad Dogaru channels[i].scan_index); 12078f5d8727SVlad Dogaru return -EINVAL; 12088f5d8727SVlad Dogaru } 12098f5d8727SVlad Dogaru } 12108f5d8727SVlad Dogaru 12118f5d8727SVlad Dogaru return 0; 12128f5d8727SVlad Dogaru } 12138f5d8727SVlad Dogaru 1214a980e046SJonathan Cameron static const struct iio_buffer_setup_ops noop_ring_setup_ops; 1215a980e046SJonathan Cameron 1216a7e57dceSSachin Kamat /** 1217a7e57dceSSachin Kamat * iio_device_register() - register a device with the IIO subsystem 1218a7e57dceSSachin Kamat * @indio_dev: Device structure filled by the device driver 1219a7e57dceSSachin Kamat **/ 1220a980e046SJonathan Cameron int iio_device_register(struct iio_dev *indio_dev) 1221a980e046SJonathan Cameron { 1222a980e046SJonathan Cameron int ret; 1223a980e046SJonathan Cameron 122417d82b47SGuenter Roeck /* If the calling driver did not initialize of_node, do it here */ 122517d82b47SGuenter Roeck if (!indio_dev->dev.of_node && indio_dev->dev.parent) 122617d82b47SGuenter Roeck indio_dev->dev.of_node = indio_dev->dev.parent->of_node; 122717d82b47SGuenter Roeck 12288f5d8727SVlad Dogaru ret = iio_check_unique_scan_index(indio_dev); 12298f5d8727SVlad Dogaru if (ret < 0) 12308f5d8727SVlad Dogaru return ret; 12318f5d8727SVlad Dogaru 1232a980e046SJonathan Cameron /* configure elements for the chrdev */ 1233a980e046SJonathan Cameron indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id); 1234a980e046SJonathan Cameron 1235a980e046SJonathan Cameron ret = iio_device_register_debugfs(indio_dev); 1236a980e046SJonathan Cameron if (ret) { 1237a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1238a980e046SJonathan Cameron "Failed to register debugfs interfaces\n"); 123992825ff9SHartmut Knaack return ret; 1240a980e046SJonathan Cameron } 12413e1b6c95SLars-Peter Clausen 12423e1b6c95SLars-Peter Clausen ret = iio_buffer_alloc_sysfs_and_mask(indio_dev); 12433e1b6c95SLars-Peter Clausen if (ret) { 12443e1b6c95SLars-Peter Clausen dev_err(indio_dev->dev.parent, 12453e1b6c95SLars-Peter Clausen "Failed to create buffer sysfs interfaces\n"); 12463e1b6c95SLars-Peter Clausen goto error_unreg_debugfs; 12473e1b6c95SLars-Peter Clausen } 12483e1b6c95SLars-Peter Clausen 1249a980e046SJonathan Cameron ret = iio_device_register_sysfs(indio_dev); 1250a980e046SJonathan Cameron if (ret) { 1251a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1252a980e046SJonathan Cameron "Failed to register sysfs interfaces\n"); 12533e1b6c95SLars-Peter Clausen goto error_buffer_free_sysfs; 1254a980e046SJonathan Cameron } 1255a980e046SJonathan Cameron ret = iio_device_register_eventset(indio_dev); 1256a980e046SJonathan Cameron if (ret) { 1257a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1258a980e046SJonathan Cameron "Failed to register event set\n"); 1259a980e046SJonathan Cameron goto error_free_sysfs; 1260a980e046SJonathan Cameron } 1261735ad074SVladimir Barinov if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED)) 1262a980e046SJonathan Cameron iio_device_register_trigger_consumer(indio_dev); 1263a980e046SJonathan Cameron 1264a980e046SJonathan Cameron if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) && 1265a980e046SJonathan Cameron indio_dev->setup_ops == NULL) 1266a980e046SJonathan Cameron indio_dev->setup_ops = &noop_ring_setup_ops; 1267a980e046SJonathan Cameron 1268a980e046SJonathan Cameron cdev_init(&indio_dev->chrdev, &iio_buffer_fileops); 1269a980e046SJonathan Cameron indio_dev->chrdev.owner = indio_dev->info->driver_module; 12700d5b7daeSLars-Peter Clausen indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj; 1271a980e046SJonathan Cameron ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1); 1272a980e046SJonathan Cameron if (ret < 0) 12730d5b7daeSLars-Peter Clausen goto error_unreg_eventset; 1274a980e046SJonathan Cameron 12750d5b7daeSLars-Peter Clausen ret = device_add(&indio_dev->dev); 12760d5b7daeSLars-Peter Clausen if (ret < 0) 12770d5b7daeSLars-Peter Clausen goto error_cdev_del; 12780d5b7daeSLars-Peter Clausen 12790d5b7daeSLars-Peter Clausen return 0; 12800d5b7daeSLars-Peter Clausen error_cdev_del: 12810d5b7daeSLars-Peter Clausen cdev_del(&indio_dev->chrdev); 1282a980e046SJonathan Cameron error_unreg_eventset: 1283a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 1284a980e046SJonathan Cameron error_free_sysfs: 1285a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 12863e1b6c95SLars-Peter Clausen error_buffer_free_sysfs: 12873e1b6c95SLars-Peter Clausen iio_buffer_free_sysfs_and_mask(indio_dev); 1288a980e046SJonathan Cameron error_unreg_debugfs: 1289a980e046SJonathan Cameron iio_device_unregister_debugfs(indio_dev); 1290a980e046SJonathan Cameron return ret; 1291a980e046SJonathan Cameron } 1292a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_register); 1293a980e046SJonathan Cameron 1294a7e57dceSSachin Kamat /** 1295a7e57dceSSachin Kamat * iio_device_unregister() - unregister a device from the IIO subsystem 1296a7e57dceSSachin Kamat * @indio_dev: Device structure representing the device. 1297a7e57dceSSachin Kamat **/ 1298a980e046SJonathan Cameron void iio_device_unregister(struct iio_dev *indio_dev) 1299a980e046SJonathan Cameron { 1300a980e046SJonathan Cameron mutex_lock(&indio_dev->info_exist_lock); 1301a87c82e4SLars-Peter Clausen 1302a87c82e4SLars-Peter Clausen device_del(&indio_dev->dev); 1303a87c82e4SLars-Peter Clausen 13040d5b7daeSLars-Peter Clausen if (indio_dev->chrdev.dev) 13050d5b7daeSLars-Peter Clausen cdev_del(&indio_dev->chrdev); 1306bc4c9612SLars-Peter Clausen iio_device_unregister_debugfs(indio_dev); 13070d5b7daeSLars-Peter Clausen 1308a87c82e4SLars-Peter Clausen iio_disable_all_buffers(indio_dev); 1309a87c82e4SLars-Peter Clausen 1310a980e046SJonathan Cameron indio_dev->info = NULL; 1311d2f0a48fSLars-Peter Clausen 1312d2f0a48fSLars-Peter Clausen iio_device_wakeup_eventset(indio_dev); 1313d2f0a48fSLars-Peter Clausen iio_buffer_wakeup_poll(indio_dev); 1314d2f0a48fSLars-Peter Clausen 1315a980e046SJonathan Cameron mutex_unlock(&indio_dev->info_exist_lock); 13163e1b6c95SLars-Peter Clausen 13173e1b6c95SLars-Peter Clausen iio_buffer_free_sysfs_and_mask(indio_dev); 1318a980e046SJonathan Cameron } 1319a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_unregister); 13208caa07c0SSachin Kamat 13218caa07c0SSachin Kamat static void devm_iio_device_unreg(struct device *dev, void *res) 13228caa07c0SSachin Kamat { 13238caa07c0SSachin Kamat iio_device_unregister(*(struct iio_dev **)res); 13248caa07c0SSachin Kamat } 13258caa07c0SSachin Kamat 13268caa07c0SSachin Kamat /** 13278caa07c0SSachin Kamat * devm_iio_device_register - Resource-managed iio_device_register() 13288caa07c0SSachin Kamat * @dev: Device to allocate iio_dev for 13298caa07c0SSachin Kamat * @indio_dev: Device structure filled by the device driver 13308caa07c0SSachin Kamat * 13318caa07c0SSachin Kamat * Managed iio_device_register. The IIO device registered with this 13328caa07c0SSachin Kamat * function is automatically unregistered on driver detach. This function 13338caa07c0SSachin Kamat * calls iio_device_register() internally. Refer to that function for more 13348caa07c0SSachin Kamat * information. 13358caa07c0SSachin Kamat * 13368caa07c0SSachin Kamat * If an iio_dev registered with this function needs to be unregistered 13378caa07c0SSachin Kamat * separately, devm_iio_device_unregister() must be used. 13388caa07c0SSachin Kamat * 13398caa07c0SSachin Kamat * RETURNS: 13408caa07c0SSachin Kamat * 0 on success, negative error number on failure. 13418caa07c0SSachin Kamat */ 13428caa07c0SSachin Kamat int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev) 13438caa07c0SSachin Kamat { 13448caa07c0SSachin Kamat struct iio_dev **ptr; 13458caa07c0SSachin Kamat int ret; 13468caa07c0SSachin Kamat 13478caa07c0SSachin Kamat ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL); 13488caa07c0SSachin Kamat if (!ptr) 13498caa07c0SSachin Kamat return -ENOMEM; 13508caa07c0SSachin Kamat 13518caa07c0SSachin Kamat *ptr = indio_dev; 13528caa07c0SSachin Kamat ret = iio_device_register(indio_dev); 13538caa07c0SSachin Kamat if (!ret) 13548caa07c0SSachin Kamat devres_add(dev, ptr); 13558caa07c0SSachin Kamat else 13568caa07c0SSachin Kamat devres_free(ptr); 13578caa07c0SSachin Kamat 13588caa07c0SSachin Kamat return ret; 13598caa07c0SSachin Kamat } 13608caa07c0SSachin Kamat EXPORT_SYMBOL_GPL(devm_iio_device_register); 13618caa07c0SSachin Kamat 13628caa07c0SSachin Kamat /** 13638caa07c0SSachin Kamat * devm_iio_device_unregister - Resource-managed iio_device_unregister() 13648caa07c0SSachin Kamat * @dev: Device this iio_dev belongs to 13658caa07c0SSachin Kamat * @indio_dev: the iio_dev associated with the device 13668caa07c0SSachin Kamat * 13678caa07c0SSachin Kamat * Unregister iio_dev registered with devm_iio_device_register(). 13688caa07c0SSachin Kamat */ 13698caa07c0SSachin Kamat void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev) 13708caa07c0SSachin Kamat { 13718caa07c0SSachin Kamat int rc; 13728caa07c0SSachin Kamat 13738caa07c0SSachin Kamat rc = devres_release(dev, devm_iio_device_unreg, 13748caa07c0SSachin Kamat devm_iio_device_match, indio_dev); 13758caa07c0SSachin Kamat WARN_ON(rc); 13768caa07c0SSachin Kamat } 13778caa07c0SSachin Kamat EXPORT_SYMBOL_GPL(devm_iio_device_unregister); 13788caa07c0SSachin Kamat 1379*08a33805SAlison Schofield /** 1380*08a33805SAlison Schofield * iio_device_claim_direct_mode - Keep device in direct mode 1381*08a33805SAlison Schofield * @indio_dev: the iio_dev associated with the device 1382*08a33805SAlison Schofield * 1383*08a33805SAlison Schofield * If the device is in direct mode it is guaranteed to stay 1384*08a33805SAlison Schofield * that way until iio_device_release_direct_mode() is called. 1385*08a33805SAlison Schofield * 1386*08a33805SAlison Schofield * Use with iio_device_release_direct_mode() 1387*08a33805SAlison Schofield * 1388*08a33805SAlison Schofield * Returns: 0 on success, -EBUSY on failure 1389*08a33805SAlison Schofield */ 1390*08a33805SAlison Schofield int iio_device_claim_direct_mode(struct iio_dev *indio_dev) 1391*08a33805SAlison Schofield { 1392*08a33805SAlison Schofield mutex_lock(&indio_dev->mlock); 1393*08a33805SAlison Schofield 1394*08a33805SAlison Schofield if (iio_buffer_enabled(indio_dev)) { 1395*08a33805SAlison Schofield mutex_unlock(&indio_dev->mlock); 1396*08a33805SAlison Schofield return -EBUSY; 1397*08a33805SAlison Schofield } 1398*08a33805SAlison Schofield return 0; 1399*08a33805SAlison Schofield } 1400*08a33805SAlison Schofield EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode); 1401*08a33805SAlison Schofield 1402*08a33805SAlison Schofield /** 1403*08a33805SAlison Schofield * iio_device_release_direct_mode - releases claim on direct mode 1404*08a33805SAlison Schofield * @indio_dev: the iio_dev associated with the device 1405*08a33805SAlison Schofield * 1406*08a33805SAlison Schofield * Release the claim. Device is no longer guaranteed to stay 1407*08a33805SAlison Schofield * in direct mode. 1408*08a33805SAlison Schofield * 1409*08a33805SAlison Schofield * Use with iio_device_claim_direct_mode() 1410*08a33805SAlison Schofield */ 1411*08a33805SAlison Schofield void iio_device_release_direct_mode(struct iio_dev *indio_dev) 1412*08a33805SAlison Schofield { 1413*08a33805SAlison Schofield mutex_unlock(&indio_dev->mlock); 1414*08a33805SAlison Schofield } 1415*08a33805SAlison Schofield EXPORT_SYMBOL_GPL(iio_device_release_direct_mode); 1416*08a33805SAlison Schofield 1417a980e046SJonathan Cameron subsys_initcall(iio_init); 1418a980e046SJonathan Cameron module_exit(iio_exit); 1419a980e046SJonathan Cameron 1420c8b95952SJonathan Cameron MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); 1421a980e046SJonathan Cameron MODULE_DESCRIPTION("Industrial I/O core"); 1422a980e046SJonathan Cameron MODULE_LICENSE("GPL"); 1423