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> 28a980e046SJonathan Cameron #include <linux/iio/iio.h> 29a980e046SJonathan Cameron #include "iio_core.h" 30a980e046SJonathan Cameron #include "iio_core_trigger.h" 31a980e046SJonathan Cameron #include <linux/iio/sysfs.h> 32a980e046SJonathan Cameron #include <linux/iio/events.h> 339e69c935SLars-Peter Clausen #include <linux/iio/buffer.h> 34a980e046SJonathan Cameron 35a980e046SJonathan Cameron /* IDA to assign each registered device a unique id */ 36a980e046SJonathan Cameron static DEFINE_IDA(iio_ida); 37a980e046SJonathan Cameron 38a980e046SJonathan Cameron static dev_t iio_devt; 39a980e046SJonathan Cameron 40a980e046SJonathan Cameron #define IIO_DEV_MAX 256 41a980e046SJonathan Cameron struct bus_type iio_bus_type = { 42a980e046SJonathan Cameron .name = "iio", 43a980e046SJonathan Cameron }; 44a980e046SJonathan Cameron EXPORT_SYMBOL(iio_bus_type); 45a980e046SJonathan Cameron 46a980e046SJonathan Cameron static struct dentry *iio_debugfs_dentry; 47a980e046SJonathan Cameron 48a980e046SJonathan Cameron static const char * const iio_direction[] = { 49a980e046SJonathan Cameron [0] = "in", 50a980e046SJonathan Cameron [1] = "out", 51a980e046SJonathan Cameron }; 52a980e046SJonathan Cameron 53a980e046SJonathan Cameron static const char * const iio_chan_type_name_spec[] = { 54a980e046SJonathan Cameron [IIO_VOLTAGE] = "voltage", 55a980e046SJonathan Cameron [IIO_CURRENT] = "current", 56a980e046SJonathan Cameron [IIO_POWER] = "power", 57a980e046SJonathan Cameron [IIO_ACCEL] = "accel", 58a980e046SJonathan Cameron [IIO_ANGL_VEL] = "anglvel", 59a980e046SJonathan Cameron [IIO_MAGN] = "magn", 60a980e046SJonathan Cameron [IIO_LIGHT] = "illuminance", 61a980e046SJonathan Cameron [IIO_INTENSITY] = "intensity", 62a980e046SJonathan Cameron [IIO_PROXIMITY] = "proximity", 63a980e046SJonathan Cameron [IIO_TEMP] = "temp", 64a980e046SJonathan Cameron [IIO_INCLI] = "incli", 65a980e046SJonathan Cameron [IIO_ROT] = "rot", 66a980e046SJonathan Cameron [IIO_ANGL] = "angl", 67a980e046SJonathan Cameron [IIO_TIMESTAMP] = "timestamp", 68a980e046SJonathan Cameron [IIO_CAPACITANCE] = "capacitance", 69a6b12855SMichael Hennerich [IIO_ALTVOLTAGE] = "altvoltage", 7021cd1fabSJon Brenner [IIO_CCT] = "cct", 71c4f0c693SLars-Peter Clausen [IIO_PRESSURE] = "pressure", 72ac216aa2SHarald Geyer [IIO_HUMIDITYRELATIVE] = "humidityrelative", 7355aebeb9SDaniel Baluta [IIO_ACTIVITY] = "activity", 74a88bfe78SIrina Tirdea [IIO_STEPS] = "steps", 7572c66644SIrina Tirdea [IIO_ENERGY] = "energy", 76cc3c9eecSIrina Tirdea [IIO_DISTANCE] = "distance", 775a1a9329SIrina Tirdea [IIO_VELOCITY] = "velocity", 78a980e046SJonathan Cameron }; 79a980e046SJonathan Cameron 80a980e046SJonathan Cameron static const char * const iio_modifier_names[] = { 81a980e046SJonathan Cameron [IIO_MOD_X] = "x", 82a980e046SJonathan Cameron [IIO_MOD_Y] = "y", 83a980e046SJonathan Cameron [IIO_MOD_Z] = "z", 848f5879b2SJonathan Cameron [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)", 85cf82cb81SJonathan Cameron [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2", 86a980e046SJonathan Cameron [IIO_MOD_LIGHT_BOTH] = "both", 87a980e046SJonathan Cameron [IIO_MOD_LIGHT_IR] = "ir", 8821cd1fabSJon Brenner [IIO_MOD_LIGHT_CLEAR] = "clear", 8921cd1fabSJon Brenner [IIO_MOD_LIGHT_RED] = "red", 9021cd1fabSJon Brenner [IIO_MOD_LIGHT_GREEN] = "green", 9121cd1fabSJon Brenner [IIO_MOD_LIGHT_BLUE] = "blue", 925082f405SSrinivas Pandruvada [IIO_MOD_QUATERNION] = "quaternion", 93638b43b3SPeter Meerwald [IIO_MOD_TEMP_AMBIENT] = "ambient", 94638b43b3SPeter Meerwald [IIO_MOD_TEMP_OBJECT] = "object", 9511b8ddabSReyad Attiyat [IIO_MOD_NORTH_MAGN] = "from_north_magnetic", 9611b8ddabSReyad Attiyat [IIO_MOD_NORTH_TRUE] = "from_north_true", 9711b8ddabSReyad Attiyat [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp", 9811b8ddabSReyad Attiyat [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp", 9955aebeb9SDaniel Baluta [IIO_MOD_RUNNING] = "running", 10055aebeb9SDaniel Baluta [IIO_MOD_JOGGING] = "jogging", 10155aebeb9SDaniel Baluta [IIO_MOD_WALKING] = "walking", 10255aebeb9SDaniel Baluta [IIO_MOD_STILL] = "still", 1035a1a9329SIrina Tirdea [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)", 104a980e046SJonathan Cameron }; 105a980e046SJonathan Cameron 106a980e046SJonathan Cameron /* relies on pairs of these shared then separate */ 107a980e046SJonathan Cameron static const char * const iio_chan_info_postfix[] = { 108a980e046SJonathan Cameron [IIO_CHAN_INFO_RAW] = "raw", 109a980e046SJonathan Cameron [IIO_CHAN_INFO_PROCESSED] = "input", 110a980e046SJonathan Cameron [IIO_CHAN_INFO_SCALE] = "scale", 111a980e046SJonathan Cameron [IIO_CHAN_INFO_OFFSET] = "offset", 112a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBSCALE] = "calibscale", 113a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBBIAS] = "calibbias", 114a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK] = "peak_raw", 115a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale", 116a980e046SJonathan Cameron [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw", 117a980e046SJonathan Cameron [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw", 118a980e046SJonathan Cameron [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY] 119a980e046SJonathan Cameron = "filter_low_pass_3db_frequency", 120*3f7f642bSMartin Fuzzey [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY] 121*3f7f642bSMartin Fuzzey = "filter_high_pass_3db_frequency", 122a980e046SJonathan Cameron [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency", 123a6b12855SMichael Hennerich [IIO_CHAN_INFO_FREQUENCY] = "frequency", 124a6b12855SMichael Hennerich [IIO_CHAN_INFO_PHASE] = "phase", 125b65d6212SMichael Hennerich [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain", 1267c9ab035Ssrinivas pandruvada [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis", 127899d90bdSPeter Meerwald [IIO_CHAN_INFO_INT_TIME] = "integration_time", 128a88bfe78SIrina Tirdea [IIO_CHAN_INFO_ENABLE] = "en", 129bcdf28fbSIrina Tirdea [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight", 130d37f6836SIrina Tirdea [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight", 1312f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count", 1322f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time", 133c8a85854SVianney le Clément de Saint-Marcq [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity", 134faaa4495SIrina Tirdea [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio", 135a980e046SJonathan Cameron }; 136a980e046SJonathan Cameron 137a7e57dceSSachin Kamat /** 138a7e57dceSSachin Kamat * iio_find_channel_from_si() - get channel from its scan index 139a7e57dceSSachin Kamat * @indio_dev: device 140a7e57dceSSachin Kamat * @si: scan index to match 141a7e57dceSSachin Kamat */ 142a980e046SJonathan Cameron const struct iio_chan_spec 143a980e046SJonathan Cameron *iio_find_channel_from_si(struct iio_dev *indio_dev, int si) 144a980e046SJonathan Cameron { 145a980e046SJonathan Cameron int i; 146a980e046SJonathan Cameron 147a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) 148a980e046SJonathan Cameron if (indio_dev->channels[i].scan_index == si) 149a980e046SJonathan Cameron return &indio_dev->channels[i]; 150a980e046SJonathan Cameron return NULL; 151a980e046SJonathan Cameron } 152a980e046SJonathan Cameron 153a980e046SJonathan Cameron /* This turns up an awful lot */ 154a980e046SJonathan Cameron ssize_t iio_read_const_attr(struct device *dev, 155a980e046SJonathan Cameron struct device_attribute *attr, 156a980e046SJonathan Cameron char *buf) 157a980e046SJonathan Cameron { 158a980e046SJonathan Cameron return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string); 159a980e046SJonathan Cameron } 160a980e046SJonathan Cameron EXPORT_SYMBOL(iio_read_const_attr); 161a980e046SJonathan Cameron 162a980e046SJonathan Cameron static int __init iio_init(void) 163a980e046SJonathan Cameron { 164a980e046SJonathan Cameron int ret; 165a980e046SJonathan Cameron 166a980e046SJonathan Cameron /* Register sysfs bus */ 167a980e046SJonathan Cameron ret = bus_register(&iio_bus_type); 168a980e046SJonathan Cameron if (ret < 0) { 1693176dd5dSSachin Kamat pr_err("could not register bus type\n"); 170a980e046SJonathan Cameron goto error_nothing; 171a980e046SJonathan Cameron } 172a980e046SJonathan Cameron 173a980e046SJonathan Cameron ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio"); 174a980e046SJonathan Cameron if (ret < 0) { 1753176dd5dSSachin Kamat pr_err("failed to allocate char dev region\n"); 176a980e046SJonathan Cameron goto error_unregister_bus_type; 177a980e046SJonathan Cameron } 178a980e046SJonathan Cameron 179a980e046SJonathan Cameron iio_debugfs_dentry = debugfs_create_dir("iio", NULL); 180a980e046SJonathan Cameron 181a980e046SJonathan Cameron return 0; 182a980e046SJonathan Cameron 183a980e046SJonathan Cameron error_unregister_bus_type: 184a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 185a980e046SJonathan Cameron error_nothing: 186a980e046SJonathan Cameron return ret; 187a980e046SJonathan Cameron } 188a980e046SJonathan Cameron 189a980e046SJonathan Cameron static void __exit iio_exit(void) 190a980e046SJonathan Cameron { 191a980e046SJonathan Cameron if (iio_devt) 192a980e046SJonathan Cameron unregister_chrdev_region(iio_devt, IIO_DEV_MAX); 193a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 194a980e046SJonathan Cameron debugfs_remove(iio_debugfs_dentry); 195a980e046SJonathan Cameron } 196a980e046SJonathan Cameron 197a980e046SJonathan Cameron #if defined(CONFIG_DEBUG_FS) 198a980e046SJonathan Cameron static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf, 199a980e046SJonathan Cameron size_t count, loff_t *ppos) 200a980e046SJonathan Cameron { 201a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 202a980e046SJonathan Cameron char buf[20]; 203a980e046SJonathan Cameron unsigned val = 0; 204a980e046SJonathan Cameron ssize_t len; 205a980e046SJonathan Cameron int ret; 206a980e046SJonathan Cameron 207a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, 208a980e046SJonathan Cameron indio_dev->cached_reg_addr, 209a980e046SJonathan Cameron 0, &val); 210a980e046SJonathan Cameron if (ret) 211a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__); 212a980e046SJonathan Cameron 213a980e046SJonathan Cameron len = snprintf(buf, sizeof(buf), "0x%X\n", val); 214a980e046SJonathan Cameron 215a980e046SJonathan Cameron return simple_read_from_buffer(userbuf, count, ppos, buf, len); 216a980e046SJonathan Cameron } 217a980e046SJonathan Cameron 218a980e046SJonathan Cameron static ssize_t iio_debugfs_write_reg(struct file *file, 219a980e046SJonathan Cameron const char __user *userbuf, size_t count, loff_t *ppos) 220a980e046SJonathan Cameron { 221a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 222a980e046SJonathan Cameron unsigned reg, val; 223a980e046SJonathan Cameron char buf[80]; 224a980e046SJonathan Cameron int ret; 225a980e046SJonathan Cameron 226a980e046SJonathan Cameron count = min_t(size_t, count, (sizeof(buf)-1)); 227a980e046SJonathan Cameron if (copy_from_user(buf, userbuf, count)) 228a980e046SJonathan Cameron return -EFAULT; 229a980e046SJonathan Cameron 230a980e046SJonathan Cameron buf[count] = 0; 231a980e046SJonathan Cameron 232a980e046SJonathan Cameron ret = sscanf(buf, "%i %i", ®, &val); 233a980e046SJonathan Cameron 234a980e046SJonathan Cameron switch (ret) { 235a980e046SJonathan Cameron case 1: 236a980e046SJonathan Cameron indio_dev->cached_reg_addr = reg; 237a980e046SJonathan Cameron break; 238a980e046SJonathan Cameron case 2: 239a980e046SJonathan Cameron indio_dev->cached_reg_addr = reg; 240a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, reg, 241a980e046SJonathan Cameron val, NULL); 242a980e046SJonathan Cameron if (ret) { 243a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: write failed\n", 244a980e046SJonathan Cameron __func__); 245a980e046SJonathan Cameron return ret; 246a980e046SJonathan Cameron } 247a980e046SJonathan Cameron break; 248a980e046SJonathan Cameron default: 249a980e046SJonathan Cameron return -EINVAL; 250a980e046SJonathan Cameron } 251a980e046SJonathan Cameron 252a980e046SJonathan Cameron return count; 253a980e046SJonathan Cameron } 254a980e046SJonathan Cameron 255a980e046SJonathan Cameron static const struct file_operations iio_debugfs_reg_fops = { 2565a28c873SAxel Lin .open = simple_open, 257a980e046SJonathan Cameron .read = iio_debugfs_read_reg, 258a980e046SJonathan Cameron .write = iio_debugfs_write_reg, 259a980e046SJonathan Cameron }; 260a980e046SJonathan Cameron 261a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 262a980e046SJonathan Cameron { 263a980e046SJonathan Cameron debugfs_remove_recursive(indio_dev->debugfs_dentry); 264a980e046SJonathan Cameron } 265a980e046SJonathan Cameron 266a980e046SJonathan Cameron static int iio_device_register_debugfs(struct iio_dev *indio_dev) 267a980e046SJonathan Cameron { 268a980e046SJonathan Cameron struct dentry *d; 269a980e046SJonathan Cameron 270a980e046SJonathan Cameron if (indio_dev->info->debugfs_reg_access == NULL) 271a980e046SJonathan Cameron return 0; 272a980e046SJonathan Cameron 273abd5a2fbSAxel Lin if (!iio_debugfs_dentry) 274a980e046SJonathan Cameron return 0; 275a980e046SJonathan Cameron 276a980e046SJonathan Cameron indio_dev->debugfs_dentry = 277a980e046SJonathan Cameron debugfs_create_dir(dev_name(&indio_dev->dev), 278a980e046SJonathan Cameron iio_debugfs_dentry); 279a980e046SJonathan Cameron if (indio_dev->debugfs_dentry == NULL) { 280a980e046SJonathan Cameron dev_warn(indio_dev->dev.parent, 281a980e046SJonathan Cameron "Failed to create debugfs directory\n"); 282a980e046SJonathan Cameron return -EFAULT; 283a980e046SJonathan Cameron } 284a980e046SJonathan Cameron 285a980e046SJonathan Cameron d = debugfs_create_file("direct_reg_access", 0644, 286a980e046SJonathan Cameron indio_dev->debugfs_dentry, 287a980e046SJonathan Cameron indio_dev, &iio_debugfs_reg_fops); 288a980e046SJonathan Cameron if (!d) { 289a980e046SJonathan Cameron iio_device_unregister_debugfs(indio_dev); 290a980e046SJonathan Cameron return -ENOMEM; 291a980e046SJonathan Cameron } 292a980e046SJonathan Cameron 293a980e046SJonathan Cameron return 0; 294a980e046SJonathan Cameron } 295a980e046SJonathan Cameron #else 296a980e046SJonathan Cameron static int iio_device_register_debugfs(struct iio_dev *indio_dev) 297a980e046SJonathan Cameron { 298a980e046SJonathan Cameron return 0; 299a980e046SJonathan Cameron } 300a980e046SJonathan Cameron 301a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 302a980e046SJonathan Cameron { 303a980e046SJonathan Cameron } 304a980e046SJonathan Cameron #endif /* CONFIG_DEBUG_FS */ 305a980e046SJonathan Cameron 306a980e046SJonathan Cameron static ssize_t iio_read_channel_ext_info(struct device *dev, 307a980e046SJonathan Cameron struct device_attribute *attr, 308a980e046SJonathan Cameron char *buf) 309a980e046SJonathan Cameron { 310e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 311a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 312a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 313a980e046SJonathan Cameron 314a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 315a980e046SJonathan Cameron 316fc6d1139SMichael Hennerich return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf); 317a980e046SJonathan Cameron } 318a980e046SJonathan Cameron 319a980e046SJonathan Cameron static ssize_t iio_write_channel_ext_info(struct device *dev, 320a980e046SJonathan Cameron struct device_attribute *attr, 321a980e046SJonathan Cameron const char *buf, 322a980e046SJonathan Cameron size_t len) 323a980e046SJonathan Cameron { 324e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 325a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 326a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 327a980e046SJonathan Cameron 328a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 329a980e046SJonathan Cameron 330fc6d1139SMichael Hennerich return ext_info->write(indio_dev, ext_info->private, 331fc6d1139SMichael Hennerich this_attr->c, buf, len); 332a980e046SJonathan Cameron } 333a980e046SJonathan Cameron 3345212cc8aSLars-Peter Clausen ssize_t iio_enum_available_read(struct iio_dev *indio_dev, 3355212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 3365212cc8aSLars-Peter Clausen { 3375212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3385212cc8aSLars-Peter Clausen unsigned int i; 3395212cc8aSLars-Peter Clausen size_t len = 0; 3405212cc8aSLars-Peter Clausen 3415212cc8aSLars-Peter Clausen if (!e->num_items) 3425212cc8aSLars-Peter Clausen return 0; 3435212cc8aSLars-Peter Clausen 3445212cc8aSLars-Peter Clausen for (i = 0; i < e->num_items; ++i) 34574dcd439SLars-Peter Clausen len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]); 3465212cc8aSLars-Peter Clausen 3475212cc8aSLars-Peter Clausen /* replace last space with a newline */ 3485212cc8aSLars-Peter Clausen buf[len - 1] = '\n'; 3495212cc8aSLars-Peter Clausen 3505212cc8aSLars-Peter Clausen return len; 3515212cc8aSLars-Peter Clausen } 3525212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_available_read); 3535212cc8aSLars-Peter Clausen 3545212cc8aSLars-Peter Clausen ssize_t iio_enum_read(struct iio_dev *indio_dev, 3555212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 3565212cc8aSLars-Peter Clausen { 3575212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3585212cc8aSLars-Peter Clausen int i; 3595212cc8aSLars-Peter Clausen 3605212cc8aSLars-Peter Clausen if (!e->get) 3615212cc8aSLars-Peter Clausen return -EINVAL; 3625212cc8aSLars-Peter Clausen 3635212cc8aSLars-Peter Clausen i = e->get(indio_dev, chan); 3645212cc8aSLars-Peter Clausen if (i < 0) 3655212cc8aSLars-Peter Clausen return i; 3665212cc8aSLars-Peter Clausen else if (i >= e->num_items) 3675212cc8aSLars-Peter Clausen return -EINVAL; 3685212cc8aSLars-Peter Clausen 369598db581SKees Cook return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]); 3705212cc8aSLars-Peter Clausen } 3715212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_read); 3725212cc8aSLars-Peter Clausen 3735212cc8aSLars-Peter Clausen ssize_t iio_enum_write(struct iio_dev *indio_dev, 3745212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, const char *buf, 3755212cc8aSLars-Peter Clausen size_t len) 3765212cc8aSLars-Peter Clausen { 3775212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3785212cc8aSLars-Peter Clausen unsigned int i; 3795212cc8aSLars-Peter Clausen int ret; 3805212cc8aSLars-Peter Clausen 3815212cc8aSLars-Peter Clausen if (!e->set) 3825212cc8aSLars-Peter Clausen return -EINVAL; 3835212cc8aSLars-Peter Clausen 3845212cc8aSLars-Peter Clausen for (i = 0; i < e->num_items; i++) { 3855212cc8aSLars-Peter Clausen if (sysfs_streq(buf, e->items[i])) 3865212cc8aSLars-Peter Clausen break; 3875212cc8aSLars-Peter Clausen } 3885212cc8aSLars-Peter Clausen 3895212cc8aSLars-Peter Clausen if (i == e->num_items) 3905212cc8aSLars-Peter Clausen return -EINVAL; 3915212cc8aSLars-Peter Clausen 3925212cc8aSLars-Peter Clausen ret = e->set(indio_dev, chan, i); 3935212cc8aSLars-Peter Clausen return ret ? ret : len; 3945212cc8aSLars-Peter Clausen } 3955212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_write); 3965212cc8aSLars-Peter Clausen 3973661f3f5SLars-Peter Clausen /** 3983661f3f5SLars-Peter Clausen * iio_format_value() - Formats a IIO value into its string representation 3993661f3f5SLars-Peter Clausen * @buf: The buffer to which the formated value gets written 4003661f3f5SLars-Peter Clausen * @type: One of the IIO_VAL_... constants. This decides how the val and val2 4013661f3f5SLars-Peter Clausen * parameters are formatted. 4029fbfb4b3SSrinivas Pandruvada * @vals: pointer to the values, exact meaning depends on the type parameter. 4033661f3f5SLars-Peter Clausen */ 4049fbfb4b3SSrinivas Pandruvada ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals) 405a980e046SJonathan Cameron { 4067985e7c1SLars-Peter Clausen unsigned long long tmp; 40767eedba3SMichael Hennerich bool scale_db = false; 408a980e046SJonathan Cameron 4093661f3f5SLars-Peter Clausen switch (type) { 41067eedba3SMichael Hennerich case IIO_VAL_INT: 4119fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d\n", vals[0]); 41267eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_MICRO_DB: 41367eedba3SMichael Hennerich scale_db = true; 41467eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_MICRO: 4159fbfb4b3SSrinivas Pandruvada if (vals[1] < 0) 4169fbfb4b3SSrinivas Pandruvada return sprintf(buf, "-%ld.%06u%s\n", abs(vals[0]), 4179fbfb4b3SSrinivas Pandruvada -vals[1], 41867eedba3SMichael Hennerich scale_db ? " dB" : ""); 419a980e046SJonathan Cameron else 4209fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1], 42167eedba3SMichael Hennerich scale_db ? " dB" : ""); 42267eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_NANO: 4239fbfb4b3SSrinivas Pandruvada if (vals[1] < 0) 4249fbfb4b3SSrinivas Pandruvada return sprintf(buf, "-%ld.%09u\n", abs(vals[0]), 4259fbfb4b3SSrinivas Pandruvada -vals[1]); 426a980e046SJonathan Cameron else 4279fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 4287985e7c1SLars-Peter Clausen case IIO_VAL_FRACTIONAL: 4299fbfb4b3SSrinivas Pandruvada tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]); 4309fbfb4b3SSrinivas Pandruvada vals[1] = do_div(tmp, 1000000000LL); 4319fbfb4b3SSrinivas Pandruvada vals[0] = tmp; 4329fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 433103d9fb9SLars-Peter Clausen case IIO_VAL_FRACTIONAL_LOG2: 4349fbfb4b3SSrinivas Pandruvada tmp = (s64)vals[0] * 1000000000LL >> vals[1]; 4359fbfb4b3SSrinivas Pandruvada vals[1] = do_div(tmp, 1000000000LL); 4369fbfb4b3SSrinivas Pandruvada vals[0] = tmp; 4379fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 4389fbfb4b3SSrinivas Pandruvada case IIO_VAL_INT_MULTIPLE: 4399fbfb4b3SSrinivas Pandruvada { 4409fbfb4b3SSrinivas Pandruvada int i; 4419fbfb4b3SSrinivas Pandruvada int len = 0; 4429fbfb4b3SSrinivas Pandruvada 4439fbfb4b3SSrinivas Pandruvada for (i = 0; i < size; ++i) 4449fbfb4b3SSrinivas Pandruvada len += snprintf(&buf[len], PAGE_SIZE - len, "%d ", 4459fbfb4b3SSrinivas Pandruvada vals[i]); 4469fbfb4b3SSrinivas Pandruvada len += snprintf(&buf[len], PAGE_SIZE - len, "\n"); 4479fbfb4b3SSrinivas Pandruvada return len; 4489fbfb4b3SSrinivas Pandruvada } 44967eedba3SMichael Hennerich default: 450a980e046SJonathan Cameron return 0; 451a980e046SJonathan Cameron } 45267eedba3SMichael Hennerich } 453a980e046SJonathan Cameron 4543661f3f5SLars-Peter Clausen static ssize_t iio_read_channel_info(struct device *dev, 4553661f3f5SLars-Peter Clausen struct device_attribute *attr, 4563661f3f5SLars-Peter Clausen char *buf) 4573661f3f5SLars-Peter Clausen { 4583661f3f5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 4593661f3f5SLars-Peter Clausen struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 4609fbfb4b3SSrinivas Pandruvada int vals[INDIO_MAX_RAW_ELEMENTS]; 4619fbfb4b3SSrinivas Pandruvada int ret; 4629fbfb4b3SSrinivas Pandruvada int val_len = 2; 4639fbfb4b3SSrinivas Pandruvada 4649fbfb4b3SSrinivas Pandruvada if (indio_dev->info->read_raw_multi) 4659fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c, 4669fbfb4b3SSrinivas Pandruvada INDIO_MAX_RAW_ELEMENTS, 4679fbfb4b3SSrinivas Pandruvada vals, &val_len, 4689fbfb4b3SSrinivas Pandruvada this_attr->address); 4699fbfb4b3SSrinivas Pandruvada else 4709fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw(indio_dev, this_attr->c, 4719fbfb4b3SSrinivas Pandruvada &vals[0], &vals[1], this_attr->address); 4723661f3f5SLars-Peter Clausen 4733661f3f5SLars-Peter Clausen if (ret < 0) 4743661f3f5SLars-Peter Clausen return ret; 4753661f3f5SLars-Peter Clausen 4769fbfb4b3SSrinivas Pandruvada return iio_format_value(buf, ret, val_len, vals); 4773661f3f5SLars-Peter Clausen } 4783661f3f5SLars-Peter Clausen 4796807d721SLars-Peter Clausen /** 4806807d721SLars-Peter Clausen * iio_str_to_fixpoint() - Parse a fixed-point number from a string 4816807d721SLars-Peter Clausen * @str: The string to parse 4826807d721SLars-Peter Clausen * @fract_mult: Multiplier for the first decimal place, should be a power of 10 4836807d721SLars-Peter Clausen * @integer: The integer part of the number 4846807d721SLars-Peter Clausen * @fract: The fractional part of the number 4856807d721SLars-Peter Clausen * 4866807d721SLars-Peter Clausen * Returns 0 on success, or a negative error code if the string could not be 4876807d721SLars-Peter Clausen * parsed. 4886807d721SLars-Peter Clausen */ 4896807d721SLars-Peter Clausen int iio_str_to_fixpoint(const char *str, int fract_mult, 4906807d721SLars-Peter Clausen int *integer, int *fract) 4916807d721SLars-Peter Clausen { 4926807d721SLars-Peter Clausen int i = 0, f = 0; 4936807d721SLars-Peter Clausen bool integer_part = true, negative = false; 4946807d721SLars-Peter Clausen 4956807d721SLars-Peter Clausen if (str[0] == '-') { 4966807d721SLars-Peter Clausen negative = true; 4976807d721SLars-Peter Clausen str++; 4986807d721SLars-Peter Clausen } else if (str[0] == '+') { 4996807d721SLars-Peter Clausen str++; 5006807d721SLars-Peter Clausen } 5016807d721SLars-Peter Clausen 5026807d721SLars-Peter Clausen while (*str) { 5036807d721SLars-Peter Clausen if ('0' <= *str && *str <= '9') { 5046807d721SLars-Peter Clausen if (integer_part) { 5056807d721SLars-Peter Clausen i = i * 10 + *str - '0'; 5066807d721SLars-Peter Clausen } else { 5076807d721SLars-Peter Clausen f += fract_mult * (*str - '0'); 5086807d721SLars-Peter Clausen fract_mult /= 10; 5096807d721SLars-Peter Clausen } 5106807d721SLars-Peter Clausen } else if (*str == '\n') { 5116807d721SLars-Peter Clausen if (*(str + 1) == '\0') 5126807d721SLars-Peter Clausen break; 5136807d721SLars-Peter Clausen else 5146807d721SLars-Peter Clausen return -EINVAL; 5156807d721SLars-Peter Clausen } else if (*str == '.' && integer_part) { 5166807d721SLars-Peter Clausen integer_part = false; 5176807d721SLars-Peter Clausen } else { 5186807d721SLars-Peter Clausen return -EINVAL; 5196807d721SLars-Peter Clausen } 5206807d721SLars-Peter Clausen str++; 5216807d721SLars-Peter Clausen } 5226807d721SLars-Peter Clausen 5236807d721SLars-Peter Clausen if (negative) { 5246807d721SLars-Peter Clausen if (i) 5256807d721SLars-Peter Clausen i = -i; 5266807d721SLars-Peter Clausen else 5276807d721SLars-Peter Clausen f = -f; 5286807d721SLars-Peter Clausen } 5296807d721SLars-Peter Clausen 5306807d721SLars-Peter Clausen *integer = i; 5316807d721SLars-Peter Clausen *fract = f; 5326807d721SLars-Peter Clausen 5336807d721SLars-Peter Clausen return 0; 5346807d721SLars-Peter Clausen } 5356807d721SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_str_to_fixpoint); 5366807d721SLars-Peter Clausen 537a980e046SJonathan Cameron static ssize_t iio_write_channel_info(struct device *dev, 538a980e046SJonathan Cameron struct device_attribute *attr, 539a980e046SJonathan Cameron const char *buf, 540a980e046SJonathan Cameron size_t len) 541a980e046SJonathan Cameron { 542e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 543a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 5446807d721SLars-Peter Clausen int ret, fract_mult = 100000; 5456807d721SLars-Peter Clausen int integer, fract; 546a980e046SJonathan Cameron 547a980e046SJonathan Cameron /* Assumes decimal - precision based on number of digits */ 548a980e046SJonathan Cameron if (!indio_dev->info->write_raw) 549a980e046SJonathan Cameron return -EINVAL; 550a980e046SJonathan Cameron 551a980e046SJonathan Cameron if (indio_dev->info->write_raw_get_fmt) 552a980e046SJonathan Cameron switch (indio_dev->info->write_raw_get_fmt(indio_dev, 553a980e046SJonathan Cameron this_attr->c, this_attr->address)) { 554a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_MICRO: 555a980e046SJonathan Cameron fract_mult = 100000; 556a980e046SJonathan Cameron break; 557a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_NANO: 558a980e046SJonathan Cameron fract_mult = 100000000; 559a980e046SJonathan Cameron break; 560a980e046SJonathan Cameron default: 561a980e046SJonathan Cameron return -EINVAL; 562a980e046SJonathan Cameron } 563a980e046SJonathan Cameron 5646807d721SLars-Peter Clausen ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract); 5656807d721SLars-Peter Clausen if (ret) 5666807d721SLars-Peter Clausen return ret; 567a980e046SJonathan Cameron 568a980e046SJonathan Cameron ret = indio_dev->info->write_raw(indio_dev, this_attr->c, 569a980e046SJonathan Cameron integer, fract, this_attr->address); 570a980e046SJonathan Cameron if (ret) 571a980e046SJonathan Cameron return ret; 572a980e046SJonathan Cameron 573a980e046SJonathan Cameron return len; 574a980e046SJonathan Cameron } 575a980e046SJonathan Cameron 576a980e046SJonathan Cameron static 577a980e046SJonathan Cameron int __iio_device_attr_init(struct device_attribute *dev_attr, 578a980e046SJonathan Cameron const char *postfix, 579a980e046SJonathan Cameron struct iio_chan_spec const *chan, 580a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 581a980e046SJonathan Cameron struct device_attribute *attr, 582a980e046SJonathan Cameron char *buf), 583a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 584a980e046SJonathan Cameron struct device_attribute *attr, 585a980e046SJonathan Cameron const char *buf, 586a980e046SJonathan Cameron size_t len), 5873704432fSJonathan Cameron enum iio_shared_by shared_by) 588a980e046SJonathan Cameron { 5893704432fSJonathan Cameron int ret = 0; 5907bbcf7e1SLars-Peter Clausen char *name = NULL; 5913704432fSJonathan Cameron char *full_postfix; 592a980e046SJonathan Cameron sysfs_attr_init(&dev_attr->attr); 593a980e046SJonathan Cameron 594a980e046SJonathan Cameron /* Build up postfix of <extend_name>_<modifier>_postfix */ 5953704432fSJonathan Cameron if (chan->modified && (shared_by == IIO_SEPARATE)) { 596a980e046SJonathan Cameron if (chan->extend_name) 597a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", 598a980e046SJonathan Cameron iio_modifier_names[chan 599a980e046SJonathan Cameron ->channel2], 600a980e046SJonathan Cameron chan->extend_name, 601a980e046SJonathan Cameron postfix); 602a980e046SJonathan Cameron else 603a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s", 604a980e046SJonathan Cameron iio_modifier_names[chan 605a980e046SJonathan Cameron ->channel2], 606a980e046SJonathan Cameron postfix); 607a980e046SJonathan Cameron } else { 60877bfa8baSLars-Peter Clausen if (chan->extend_name == NULL || shared_by != IIO_SEPARATE) 609a980e046SJonathan Cameron full_postfix = kstrdup(postfix, GFP_KERNEL); 610a980e046SJonathan Cameron else 611a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, 612a980e046SJonathan Cameron "%s_%s", 613a980e046SJonathan Cameron chan->extend_name, 614a980e046SJonathan Cameron postfix); 615a980e046SJonathan Cameron } 6163704432fSJonathan Cameron if (full_postfix == NULL) 6173704432fSJonathan Cameron return -ENOMEM; 618a980e046SJonathan Cameron 619a980e046SJonathan Cameron if (chan->differential) { /* Differential can not have modifier */ 6203704432fSJonathan Cameron switch (shared_by) { 621c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 6227bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 623c006ec83SJonathan Cameron break; 624c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 6257bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 626c006ec83SJonathan Cameron iio_direction[chan->output], 627c006ec83SJonathan Cameron full_postfix); 628c006ec83SJonathan Cameron break; 6293704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 6307bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s", 631a980e046SJonathan Cameron iio_direction[chan->output], 632a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 633a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 634a980e046SJonathan Cameron full_postfix); 6353704432fSJonathan Cameron break; 6363704432fSJonathan Cameron case IIO_SEPARATE: 6373704432fSJonathan Cameron if (!chan->indexed) { 6383704432fSJonathan Cameron WARN_ON("Differential channels must be indexed\n"); 6393704432fSJonathan Cameron ret = -EINVAL; 6403704432fSJonathan Cameron goto error_free_full_postfix; 6413704432fSJonathan Cameron } 6427bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, 6433704432fSJonathan Cameron "%s_%s%d-%s%d_%s", 644a980e046SJonathan Cameron iio_direction[chan->output], 645a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 646a980e046SJonathan Cameron chan->channel, 647a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 648a980e046SJonathan Cameron chan->channel2, 649a980e046SJonathan Cameron full_postfix); 6503704432fSJonathan Cameron break; 651a980e046SJonathan Cameron } 652a980e046SJonathan Cameron } else { /* Single ended */ 6533704432fSJonathan Cameron switch (shared_by) { 654c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 6557bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 656c006ec83SJonathan Cameron break; 657c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 6587bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 659c006ec83SJonathan Cameron iio_direction[chan->output], 660c006ec83SJonathan Cameron full_postfix); 661c006ec83SJonathan Cameron break; 6623704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 6637bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 664a980e046SJonathan Cameron iio_direction[chan->output], 665a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 666a980e046SJonathan Cameron full_postfix); 6673704432fSJonathan Cameron break; 6683704432fSJonathan Cameron 6693704432fSJonathan Cameron case IIO_SEPARATE: 6703704432fSJonathan Cameron if (chan->indexed) 6717bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s%d_%s", 672a980e046SJonathan Cameron iio_direction[chan->output], 673a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 674a980e046SJonathan Cameron chan->channel, 675a980e046SJonathan Cameron full_postfix); 676a980e046SJonathan Cameron else 6777bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 678a980e046SJonathan Cameron iio_direction[chan->output], 679a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 680a980e046SJonathan Cameron full_postfix); 6813704432fSJonathan Cameron break; 6823704432fSJonathan Cameron } 683a980e046SJonathan Cameron } 6847bbcf7e1SLars-Peter Clausen if (name == NULL) { 685a980e046SJonathan Cameron ret = -ENOMEM; 686a980e046SJonathan Cameron goto error_free_full_postfix; 687a980e046SJonathan Cameron } 6887bbcf7e1SLars-Peter Clausen dev_attr->attr.name = name; 689a980e046SJonathan Cameron 690a980e046SJonathan Cameron if (readfunc) { 691a980e046SJonathan Cameron dev_attr->attr.mode |= S_IRUGO; 692a980e046SJonathan Cameron dev_attr->show = readfunc; 693a980e046SJonathan Cameron } 694a980e046SJonathan Cameron 695a980e046SJonathan Cameron if (writefunc) { 696a980e046SJonathan Cameron dev_attr->attr.mode |= S_IWUSR; 697a980e046SJonathan Cameron dev_attr->store = writefunc; 698a980e046SJonathan Cameron } 6997bbcf7e1SLars-Peter Clausen 700a980e046SJonathan Cameron error_free_full_postfix: 701a980e046SJonathan Cameron kfree(full_postfix); 7023704432fSJonathan Cameron 703a980e046SJonathan Cameron return ret; 704a980e046SJonathan Cameron } 705a980e046SJonathan Cameron 706a980e046SJonathan Cameron static void __iio_device_attr_deinit(struct device_attribute *dev_attr) 707a980e046SJonathan Cameron { 708a980e046SJonathan Cameron kfree(dev_attr->attr.name); 709a980e046SJonathan Cameron } 710a980e046SJonathan Cameron 711a980e046SJonathan Cameron int __iio_add_chan_devattr(const char *postfix, 712a980e046SJonathan Cameron struct iio_chan_spec const *chan, 713a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 714a980e046SJonathan Cameron struct device_attribute *attr, 715a980e046SJonathan Cameron char *buf), 716a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 717a980e046SJonathan Cameron struct device_attribute *attr, 718a980e046SJonathan Cameron const char *buf, 719a980e046SJonathan Cameron size_t len), 720a980e046SJonathan Cameron u64 mask, 7213704432fSJonathan Cameron enum iio_shared_by shared_by, 722a980e046SJonathan Cameron struct device *dev, 723a980e046SJonathan Cameron struct list_head *attr_list) 724a980e046SJonathan Cameron { 725a980e046SJonathan Cameron int ret; 726a980e046SJonathan Cameron struct iio_dev_attr *iio_attr, *t; 727a980e046SJonathan Cameron 728670c1103SSachin Kamat iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL); 72992825ff9SHartmut Knaack if (iio_attr == NULL) 73092825ff9SHartmut Knaack return -ENOMEM; 731a980e046SJonathan Cameron ret = __iio_device_attr_init(&iio_attr->dev_attr, 732a980e046SJonathan Cameron postfix, chan, 7333704432fSJonathan Cameron readfunc, writefunc, shared_by); 734a980e046SJonathan Cameron if (ret) 735a980e046SJonathan Cameron goto error_iio_dev_attr_free; 736a980e046SJonathan Cameron iio_attr->c = chan; 737a980e046SJonathan Cameron iio_attr->address = mask; 738a980e046SJonathan Cameron list_for_each_entry(t, attr_list, l) 739a980e046SJonathan Cameron if (strcmp(t->dev_attr.attr.name, 740a980e046SJonathan Cameron iio_attr->dev_attr.attr.name) == 0) { 7413704432fSJonathan Cameron if (shared_by == IIO_SEPARATE) 742a980e046SJonathan Cameron dev_err(dev, "tried to double register : %s\n", 743a980e046SJonathan Cameron t->dev_attr.attr.name); 744a980e046SJonathan Cameron ret = -EBUSY; 745a980e046SJonathan Cameron goto error_device_attr_deinit; 746a980e046SJonathan Cameron } 747a980e046SJonathan Cameron list_add(&iio_attr->l, attr_list); 748a980e046SJonathan Cameron 749a980e046SJonathan Cameron return 0; 750a980e046SJonathan Cameron 751a980e046SJonathan Cameron error_device_attr_deinit: 752a980e046SJonathan Cameron __iio_device_attr_deinit(&iio_attr->dev_attr); 753a980e046SJonathan Cameron error_iio_dev_attr_free: 754a980e046SJonathan Cameron kfree(iio_attr); 755a980e046SJonathan Cameron return ret; 756a980e046SJonathan Cameron } 757a980e046SJonathan Cameron 7583704432fSJonathan Cameron static int iio_device_add_info_mask_type(struct iio_dev *indio_dev, 7593704432fSJonathan Cameron struct iio_chan_spec const *chan, 7603704432fSJonathan Cameron enum iio_shared_by shared_by, 7613704432fSJonathan Cameron const long *infomask) 7623704432fSJonathan Cameron { 7633704432fSJonathan Cameron int i, ret, attrcount = 0; 7643704432fSJonathan Cameron 7653704432fSJonathan Cameron for_each_set_bit(i, infomask, sizeof(infomask)*8) { 766ef4b4856SJonathan Cameron if (i >= ARRAY_SIZE(iio_chan_info_postfix)) 767ef4b4856SJonathan Cameron return -EINVAL; 7683704432fSJonathan Cameron ret = __iio_add_chan_devattr(iio_chan_info_postfix[i], 7693704432fSJonathan Cameron chan, 7703704432fSJonathan Cameron &iio_read_channel_info, 7713704432fSJonathan Cameron &iio_write_channel_info, 7723704432fSJonathan Cameron i, 7733704432fSJonathan Cameron shared_by, 7743704432fSJonathan Cameron &indio_dev->dev, 7753704432fSJonathan Cameron &indio_dev->channel_attr_list); 7763704432fSJonathan Cameron if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) 7773704432fSJonathan Cameron continue; 7783704432fSJonathan Cameron else if (ret < 0) 7793704432fSJonathan Cameron return ret; 7803704432fSJonathan Cameron attrcount++; 7813704432fSJonathan Cameron } 7823704432fSJonathan Cameron 7833704432fSJonathan Cameron return attrcount; 7843704432fSJonathan Cameron } 7853704432fSJonathan Cameron 786a980e046SJonathan Cameron static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev, 787a980e046SJonathan Cameron struct iio_chan_spec const *chan) 788a980e046SJonathan Cameron { 789a980e046SJonathan Cameron int ret, attrcount = 0; 790a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 791a980e046SJonathan Cameron 792a980e046SJonathan Cameron if (chan->channel < 0) 793a980e046SJonathan Cameron return 0; 7943704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 7953704432fSJonathan Cameron IIO_SEPARATE, 7963704432fSJonathan Cameron &chan->info_mask_separate); 7978655cc49SJonathan Cameron if (ret < 0) 7983704432fSJonathan Cameron return ret; 7993704432fSJonathan Cameron attrcount += ret; 8003704432fSJonathan Cameron 8013704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 8023704432fSJonathan Cameron IIO_SHARED_BY_TYPE, 8033704432fSJonathan Cameron &chan->info_mask_shared_by_type); 8043704432fSJonathan Cameron if (ret < 0) 8053704432fSJonathan Cameron return ret; 8063704432fSJonathan Cameron attrcount += ret; 807a980e046SJonathan Cameron 808c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 809c006ec83SJonathan Cameron IIO_SHARED_BY_DIR, 810c006ec83SJonathan Cameron &chan->info_mask_shared_by_dir); 811c006ec83SJonathan Cameron if (ret < 0) 812c006ec83SJonathan Cameron return ret; 813c006ec83SJonathan Cameron attrcount += ret; 814c006ec83SJonathan Cameron 815c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 816c006ec83SJonathan Cameron IIO_SHARED_BY_ALL, 817c006ec83SJonathan Cameron &chan->info_mask_shared_by_all); 818c006ec83SJonathan Cameron if (ret < 0) 819c006ec83SJonathan Cameron return ret; 820c006ec83SJonathan Cameron attrcount += ret; 821c006ec83SJonathan Cameron 822a980e046SJonathan Cameron if (chan->ext_info) { 823a980e046SJonathan Cameron unsigned int i = 0; 824a980e046SJonathan Cameron for (ext_info = chan->ext_info; ext_info->name; ext_info++) { 825a980e046SJonathan Cameron ret = __iio_add_chan_devattr(ext_info->name, 826a980e046SJonathan Cameron chan, 827a980e046SJonathan Cameron ext_info->read ? 828a980e046SJonathan Cameron &iio_read_channel_ext_info : NULL, 829a980e046SJonathan Cameron ext_info->write ? 830a980e046SJonathan Cameron &iio_write_channel_ext_info : NULL, 831a980e046SJonathan Cameron i, 832a980e046SJonathan Cameron ext_info->shared, 833a980e046SJonathan Cameron &indio_dev->dev, 834a980e046SJonathan Cameron &indio_dev->channel_attr_list); 835a980e046SJonathan Cameron i++; 836a980e046SJonathan Cameron if (ret == -EBUSY && ext_info->shared) 837a980e046SJonathan Cameron continue; 838a980e046SJonathan Cameron 839a980e046SJonathan Cameron if (ret) 8403704432fSJonathan Cameron return ret; 841a980e046SJonathan Cameron 842a980e046SJonathan Cameron attrcount++; 843a980e046SJonathan Cameron } 844a980e046SJonathan Cameron } 845a980e046SJonathan Cameron 8463704432fSJonathan Cameron return attrcount; 847a980e046SJonathan Cameron } 848a980e046SJonathan Cameron 84984088ebdSLars-Peter Clausen /** 85084088ebdSLars-Peter Clausen * iio_free_chan_devattr_list() - Free a list of IIO device attributes 85184088ebdSLars-Peter Clausen * @attr_list: List of IIO device attributes 85284088ebdSLars-Peter Clausen * 85384088ebdSLars-Peter Clausen * This function frees the memory allocated for each of the IIO device 854c1b03ab5SMartin Fuzzey * attributes in the list. 85584088ebdSLars-Peter Clausen */ 85684088ebdSLars-Peter Clausen void iio_free_chan_devattr_list(struct list_head *attr_list) 857a980e046SJonathan Cameron { 85884088ebdSLars-Peter Clausen struct iio_dev_attr *p, *n; 85984088ebdSLars-Peter Clausen 86084088ebdSLars-Peter Clausen list_for_each_entry_safe(p, n, attr_list, l) { 861a980e046SJonathan Cameron kfree(p->dev_attr.attr.name); 862c1b03ab5SMartin Fuzzey list_del(&p->l); 863a980e046SJonathan Cameron kfree(p); 864a980e046SJonathan Cameron } 86584088ebdSLars-Peter Clausen } 866a980e046SJonathan Cameron 867a980e046SJonathan Cameron static ssize_t iio_show_dev_name(struct device *dev, 868a980e046SJonathan Cameron struct device_attribute *attr, 869a980e046SJonathan Cameron char *buf) 870a980e046SJonathan Cameron { 871e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 872598db581SKees Cook return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name); 873a980e046SJonathan Cameron } 874a980e046SJonathan Cameron 875a980e046SJonathan Cameron static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL); 876a980e046SJonathan Cameron 877a980e046SJonathan Cameron static int iio_device_register_sysfs(struct iio_dev *indio_dev) 878a980e046SJonathan Cameron { 879a980e046SJonathan Cameron int i, ret = 0, attrcount, attrn, attrcount_orig = 0; 88084088ebdSLars-Peter Clausen struct iio_dev_attr *p; 881a980e046SJonathan Cameron struct attribute **attr; 882a980e046SJonathan Cameron 883a980e046SJonathan Cameron /* First count elements in any existing group */ 884a980e046SJonathan Cameron if (indio_dev->info->attrs) { 885a980e046SJonathan Cameron attr = indio_dev->info->attrs->attrs; 886a980e046SJonathan Cameron while (*attr++ != NULL) 887a980e046SJonathan Cameron attrcount_orig++; 888a980e046SJonathan Cameron } 889a980e046SJonathan Cameron attrcount = attrcount_orig; 890a980e046SJonathan Cameron /* 891a980e046SJonathan Cameron * New channel registration method - relies on the fact a group does 892d25b3808SPeter Meerwald * not need to be initialized if its name is NULL. 893a980e046SJonathan Cameron */ 894a980e046SJonathan Cameron if (indio_dev->channels) 895a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) { 896a980e046SJonathan Cameron ret = iio_device_add_channel_sysfs(indio_dev, 897a980e046SJonathan Cameron &indio_dev 898a980e046SJonathan Cameron ->channels[i]); 899a980e046SJonathan Cameron if (ret < 0) 900a980e046SJonathan Cameron goto error_clear_attrs; 901a980e046SJonathan Cameron attrcount += ret; 902a980e046SJonathan Cameron } 903a980e046SJonathan Cameron 904a980e046SJonathan Cameron if (indio_dev->name) 905a980e046SJonathan Cameron attrcount++; 906a980e046SJonathan Cameron 907a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1, 908a980e046SJonathan Cameron sizeof(indio_dev->chan_attr_group.attrs[0]), 909a980e046SJonathan Cameron GFP_KERNEL); 910a980e046SJonathan Cameron if (indio_dev->chan_attr_group.attrs == NULL) { 911a980e046SJonathan Cameron ret = -ENOMEM; 912a980e046SJonathan Cameron goto error_clear_attrs; 913a980e046SJonathan Cameron } 914a980e046SJonathan Cameron /* Copy across original attributes */ 915a980e046SJonathan Cameron if (indio_dev->info->attrs) 916a980e046SJonathan Cameron memcpy(indio_dev->chan_attr_group.attrs, 917a980e046SJonathan Cameron indio_dev->info->attrs->attrs, 918a980e046SJonathan Cameron sizeof(indio_dev->chan_attr_group.attrs[0]) 919a980e046SJonathan Cameron *attrcount_orig); 920a980e046SJonathan Cameron attrn = attrcount_orig; 921a980e046SJonathan Cameron /* Add all elements from the list. */ 922a980e046SJonathan Cameron list_for_each_entry(p, &indio_dev->channel_attr_list, l) 923a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr; 924a980e046SJonathan Cameron if (indio_dev->name) 925a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr; 926a980e046SJonathan Cameron 927a980e046SJonathan Cameron indio_dev->groups[indio_dev->groupcounter++] = 928a980e046SJonathan Cameron &indio_dev->chan_attr_group; 929a980e046SJonathan Cameron 930a980e046SJonathan Cameron return 0; 931a980e046SJonathan Cameron 932a980e046SJonathan Cameron error_clear_attrs: 93384088ebdSLars-Peter Clausen iio_free_chan_devattr_list(&indio_dev->channel_attr_list); 934a980e046SJonathan Cameron 935a980e046SJonathan Cameron return ret; 936a980e046SJonathan Cameron } 937a980e046SJonathan Cameron 938a980e046SJonathan Cameron static void iio_device_unregister_sysfs(struct iio_dev *indio_dev) 939a980e046SJonathan Cameron { 940a980e046SJonathan Cameron 94184088ebdSLars-Peter Clausen iio_free_chan_devattr_list(&indio_dev->channel_attr_list); 942a980e046SJonathan Cameron kfree(indio_dev->chan_attr_group.attrs); 943c1b03ab5SMartin Fuzzey indio_dev->chan_attr_group.attrs = NULL; 944a980e046SJonathan Cameron } 945a980e046SJonathan Cameron 946a980e046SJonathan Cameron static void iio_dev_release(struct device *device) 947a980e046SJonathan Cameron { 948e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(device); 949a980e046SJonathan Cameron if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) 950a980e046SJonathan Cameron iio_device_unregister_trigger_consumer(indio_dev); 951a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 952a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 953e407fd65SLars-Peter Clausen 9549e69c935SLars-Peter Clausen iio_buffer_put(indio_dev->buffer); 9559e69c935SLars-Peter Clausen 956e407fd65SLars-Peter Clausen ida_simple_remove(&iio_ida, indio_dev->id); 957e407fd65SLars-Peter Clausen kfree(indio_dev); 958a980e046SJonathan Cameron } 959a980e046SJonathan Cameron 96017d82b47SGuenter Roeck struct device_type iio_device_type = { 961a980e046SJonathan Cameron .name = "iio_device", 962a980e046SJonathan Cameron .release = iio_dev_release, 963a980e046SJonathan Cameron }; 964a980e046SJonathan Cameron 965a7e57dceSSachin Kamat /** 966a7e57dceSSachin Kamat * iio_device_alloc() - allocate an iio_dev from a driver 967a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 968a7e57dceSSachin Kamat **/ 9697cbb7537SLars-Peter Clausen struct iio_dev *iio_device_alloc(int sizeof_priv) 970a980e046SJonathan Cameron { 971a980e046SJonathan Cameron struct iio_dev *dev; 972a980e046SJonathan Cameron size_t alloc_size; 973a980e046SJonathan Cameron 974a980e046SJonathan Cameron alloc_size = sizeof(struct iio_dev); 975a980e046SJonathan Cameron if (sizeof_priv) { 976a980e046SJonathan Cameron alloc_size = ALIGN(alloc_size, IIO_ALIGN); 977a980e046SJonathan Cameron alloc_size += sizeof_priv; 978a980e046SJonathan Cameron } 979a980e046SJonathan Cameron /* ensure 32-byte alignment of whole construct ? */ 980a980e046SJonathan Cameron alloc_size += IIO_ALIGN - 1; 981a980e046SJonathan Cameron 982a980e046SJonathan Cameron dev = kzalloc(alloc_size, GFP_KERNEL); 983a980e046SJonathan Cameron 984a980e046SJonathan Cameron if (dev) { 985a980e046SJonathan Cameron dev->dev.groups = dev->groups; 98617d82b47SGuenter Roeck dev->dev.type = &iio_device_type; 987a980e046SJonathan Cameron dev->dev.bus = &iio_bus_type; 988a980e046SJonathan Cameron device_initialize(&dev->dev); 989a980e046SJonathan Cameron dev_set_drvdata(&dev->dev, (void *)dev); 990a980e046SJonathan Cameron mutex_init(&dev->mlock); 991a980e046SJonathan Cameron mutex_init(&dev->info_exist_lock); 992e407fd65SLars-Peter Clausen INIT_LIST_HEAD(&dev->channel_attr_list); 993a980e046SJonathan Cameron 994a980e046SJonathan Cameron dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL); 995a980e046SJonathan Cameron if (dev->id < 0) { 996a980e046SJonathan Cameron /* cannot use a dev_err as the name isn't available */ 9973176dd5dSSachin Kamat pr_err("failed to get device id\n"); 998a980e046SJonathan Cameron kfree(dev); 999a980e046SJonathan Cameron return NULL; 1000a980e046SJonathan Cameron } 1001a980e046SJonathan Cameron dev_set_name(&dev->dev, "iio:device%d", dev->id); 100284b36ce5SJonathan Cameron INIT_LIST_HEAD(&dev->buffer_list); 1003a980e046SJonathan Cameron } 1004a980e046SJonathan Cameron 1005a980e046SJonathan Cameron return dev; 1006a980e046SJonathan Cameron } 10077cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_alloc); 1008a980e046SJonathan Cameron 1009a7e57dceSSachin Kamat /** 1010a7e57dceSSachin Kamat * iio_device_free() - free an iio_dev from a driver 1011a7e57dceSSachin Kamat * @dev: the iio_dev associated with the device 1012a7e57dceSSachin Kamat **/ 10137cbb7537SLars-Peter Clausen void iio_device_free(struct iio_dev *dev) 1014a980e046SJonathan Cameron { 1015e407fd65SLars-Peter Clausen if (dev) 1016e407fd65SLars-Peter Clausen put_device(&dev->dev); 1017a980e046SJonathan Cameron } 10187cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_free); 1019a980e046SJonathan Cameron 10209dabaf5eSGrygorii Strashko static void devm_iio_device_release(struct device *dev, void *res) 10219dabaf5eSGrygorii Strashko { 10229dabaf5eSGrygorii Strashko iio_device_free(*(struct iio_dev **)res); 10239dabaf5eSGrygorii Strashko } 10249dabaf5eSGrygorii Strashko 10259dabaf5eSGrygorii Strashko static int devm_iio_device_match(struct device *dev, void *res, void *data) 10269dabaf5eSGrygorii Strashko { 10279dabaf5eSGrygorii Strashko struct iio_dev **r = res; 10289dabaf5eSGrygorii Strashko if (!r || !*r) { 10299dabaf5eSGrygorii Strashko WARN_ON(!r || !*r); 10309dabaf5eSGrygorii Strashko return 0; 10319dabaf5eSGrygorii Strashko } 10329dabaf5eSGrygorii Strashko return *r == data; 10339dabaf5eSGrygorii Strashko } 10349dabaf5eSGrygorii Strashko 1035a7e57dceSSachin Kamat /** 1036a7e57dceSSachin Kamat * devm_iio_device_alloc - Resource-managed iio_device_alloc() 1037a7e57dceSSachin Kamat * @dev: Device to allocate iio_dev for 1038a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 1039a7e57dceSSachin Kamat * 1040a7e57dceSSachin Kamat * Managed iio_device_alloc. iio_dev allocated with this function is 1041a7e57dceSSachin Kamat * automatically freed on driver detach. 1042a7e57dceSSachin Kamat * 1043a7e57dceSSachin Kamat * If an iio_dev allocated with this function needs to be freed separately, 1044a7e57dceSSachin Kamat * devm_iio_device_free() must be used. 1045a7e57dceSSachin Kamat * 1046a7e57dceSSachin Kamat * RETURNS: 1047a7e57dceSSachin Kamat * Pointer to allocated iio_dev on success, NULL on failure. 1048a7e57dceSSachin Kamat */ 10499dabaf5eSGrygorii Strashko struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv) 10509dabaf5eSGrygorii Strashko { 10519dabaf5eSGrygorii Strashko struct iio_dev **ptr, *iio_dev; 10529dabaf5eSGrygorii Strashko 10539dabaf5eSGrygorii Strashko ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr), 10549dabaf5eSGrygorii Strashko GFP_KERNEL); 10559dabaf5eSGrygorii Strashko if (!ptr) 10569dabaf5eSGrygorii Strashko return NULL; 10579dabaf5eSGrygorii Strashko 10589dabaf5eSGrygorii Strashko iio_dev = iio_device_alloc(sizeof_priv); 10599dabaf5eSGrygorii Strashko if (iio_dev) { 10609dabaf5eSGrygorii Strashko *ptr = iio_dev; 10619dabaf5eSGrygorii Strashko devres_add(dev, ptr); 10629dabaf5eSGrygorii Strashko } else { 10639dabaf5eSGrygorii Strashko devres_free(ptr); 10649dabaf5eSGrygorii Strashko } 10659dabaf5eSGrygorii Strashko 10669dabaf5eSGrygorii Strashko return iio_dev; 10679dabaf5eSGrygorii Strashko } 10689dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_alloc); 10699dabaf5eSGrygorii Strashko 1070a7e57dceSSachin Kamat /** 1071a7e57dceSSachin Kamat * devm_iio_device_free - Resource-managed iio_device_free() 1072a7e57dceSSachin Kamat * @dev: Device this iio_dev belongs to 1073a7e57dceSSachin Kamat * @iio_dev: the iio_dev associated with the device 1074a7e57dceSSachin Kamat * 1075a7e57dceSSachin Kamat * Free iio_dev allocated with devm_iio_device_alloc(). 1076a7e57dceSSachin Kamat */ 10779dabaf5eSGrygorii Strashko void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev) 10789dabaf5eSGrygorii Strashko { 10799dabaf5eSGrygorii Strashko int rc; 10809dabaf5eSGrygorii Strashko 10819dabaf5eSGrygorii Strashko rc = devres_release(dev, devm_iio_device_release, 10829dabaf5eSGrygorii Strashko devm_iio_device_match, iio_dev); 10839dabaf5eSGrygorii Strashko WARN_ON(rc); 10849dabaf5eSGrygorii Strashko } 10859dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_free); 10869dabaf5eSGrygorii Strashko 1087a980e046SJonathan Cameron /** 1088a980e046SJonathan Cameron * iio_chrdev_open() - chrdev file open for buffer access and ioctls 1089a980e046SJonathan Cameron **/ 1090a980e046SJonathan Cameron static int iio_chrdev_open(struct inode *inode, struct file *filp) 1091a980e046SJonathan Cameron { 1092a980e046SJonathan Cameron struct iio_dev *indio_dev = container_of(inode->i_cdev, 1093a980e046SJonathan Cameron struct iio_dev, chrdev); 1094a980e046SJonathan Cameron 1095a980e046SJonathan Cameron if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags)) 1096a980e046SJonathan Cameron return -EBUSY; 1097a980e046SJonathan Cameron 1098cadc2125SLars-Peter Clausen iio_device_get(indio_dev); 1099cadc2125SLars-Peter Clausen 1100a980e046SJonathan Cameron filp->private_data = indio_dev; 1101a980e046SJonathan Cameron 1102a980e046SJonathan Cameron return 0; 1103a980e046SJonathan Cameron } 1104a980e046SJonathan Cameron 1105a980e046SJonathan Cameron /** 1106a980e046SJonathan Cameron * iio_chrdev_release() - chrdev file close buffer access and ioctls 1107a980e046SJonathan Cameron **/ 1108a980e046SJonathan Cameron static int iio_chrdev_release(struct inode *inode, struct file *filp) 1109a980e046SJonathan Cameron { 1110a980e046SJonathan Cameron struct iio_dev *indio_dev = container_of(inode->i_cdev, 1111a980e046SJonathan Cameron struct iio_dev, chrdev); 1112a980e046SJonathan Cameron clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags); 1113cadc2125SLars-Peter Clausen iio_device_put(indio_dev); 1114cadc2125SLars-Peter Clausen 1115a980e046SJonathan Cameron return 0; 1116a980e046SJonathan Cameron } 1117a980e046SJonathan Cameron 1118a980e046SJonathan Cameron /* Somewhat of a cross file organization violation - ioctls here are actually 1119a980e046SJonathan Cameron * event related */ 1120a980e046SJonathan Cameron static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 1121a980e046SJonathan Cameron { 1122a980e046SJonathan Cameron struct iio_dev *indio_dev = filp->private_data; 1123a980e046SJonathan Cameron int __user *ip = (int __user *)arg; 1124a980e046SJonathan Cameron int fd; 1125a980e046SJonathan Cameron 1126f18e7a06SLars-Peter Clausen if (!indio_dev->info) 1127f18e7a06SLars-Peter Clausen return -ENODEV; 1128f18e7a06SLars-Peter Clausen 1129a980e046SJonathan Cameron if (cmd == IIO_GET_EVENT_FD_IOCTL) { 1130a980e046SJonathan Cameron fd = iio_event_getfd(indio_dev); 1131a980e046SJonathan Cameron if (copy_to_user(ip, &fd, sizeof(fd))) 1132a980e046SJonathan Cameron return -EFAULT; 1133a980e046SJonathan Cameron return 0; 1134a980e046SJonathan Cameron } 1135a980e046SJonathan Cameron return -EINVAL; 1136a980e046SJonathan Cameron } 1137a980e046SJonathan Cameron 1138a980e046SJonathan Cameron static const struct file_operations iio_buffer_fileops = { 1139a980e046SJonathan Cameron .read = iio_buffer_read_first_n_outer_addr, 1140a980e046SJonathan Cameron .release = iio_chrdev_release, 1141a980e046SJonathan Cameron .open = iio_chrdev_open, 1142a980e046SJonathan Cameron .poll = iio_buffer_poll_addr, 1143a980e046SJonathan Cameron .owner = THIS_MODULE, 1144a980e046SJonathan Cameron .llseek = noop_llseek, 1145a980e046SJonathan Cameron .unlocked_ioctl = iio_ioctl, 1146a980e046SJonathan Cameron .compat_ioctl = iio_ioctl, 1147a980e046SJonathan Cameron }; 1148a980e046SJonathan Cameron 11498f5d8727SVlad Dogaru static int iio_check_unique_scan_index(struct iio_dev *indio_dev) 11508f5d8727SVlad Dogaru { 11518f5d8727SVlad Dogaru int i, j; 11528f5d8727SVlad Dogaru const struct iio_chan_spec *channels = indio_dev->channels; 11538f5d8727SVlad Dogaru 11548f5d8727SVlad Dogaru if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES)) 11558f5d8727SVlad Dogaru return 0; 11568f5d8727SVlad Dogaru 11578f5d8727SVlad Dogaru for (i = 0; i < indio_dev->num_channels - 1; i++) { 11588f5d8727SVlad Dogaru if (channels[i].scan_index < 0) 11598f5d8727SVlad Dogaru continue; 11608f5d8727SVlad Dogaru for (j = i + 1; j < indio_dev->num_channels; j++) 11618f5d8727SVlad Dogaru if (channels[i].scan_index == channels[j].scan_index) { 11628f5d8727SVlad Dogaru dev_err(&indio_dev->dev, 11638f5d8727SVlad Dogaru "Duplicate scan index %d\n", 11648f5d8727SVlad Dogaru channels[i].scan_index); 11658f5d8727SVlad Dogaru return -EINVAL; 11668f5d8727SVlad Dogaru } 11678f5d8727SVlad Dogaru } 11688f5d8727SVlad Dogaru 11698f5d8727SVlad Dogaru return 0; 11708f5d8727SVlad Dogaru } 11718f5d8727SVlad Dogaru 1172a980e046SJonathan Cameron static const struct iio_buffer_setup_ops noop_ring_setup_ops; 1173a980e046SJonathan Cameron 1174a7e57dceSSachin Kamat /** 1175a7e57dceSSachin Kamat * iio_device_register() - register a device with the IIO subsystem 1176a7e57dceSSachin Kamat * @indio_dev: Device structure filled by the device driver 1177a7e57dceSSachin Kamat **/ 1178a980e046SJonathan Cameron int iio_device_register(struct iio_dev *indio_dev) 1179a980e046SJonathan Cameron { 1180a980e046SJonathan Cameron int ret; 1181a980e046SJonathan Cameron 118217d82b47SGuenter Roeck /* If the calling driver did not initialize of_node, do it here */ 118317d82b47SGuenter Roeck if (!indio_dev->dev.of_node && indio_dev->dev.parent) 118417d82b47SGuenter Roeck indio_dev->dev.of_node = indio_dev->dev.parent->of_node; 118517d82b47SGuenter Roeck 11868f5d8727SVlad Dogaru ret = iio_check_unique_scan_index(indio_dev); 11878f5d8727SVlad Dogaru if (ret < 0) 11888f5d8727SVlad Dogaru return ret; 11898f5d8727SVlad Dogaru 1190a980e046SJonathan Cameron /* configure elements for the chrdev */ 1191a980e046SJonathan Cameron indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id); 1192a980e046SJonathan Cameron 1193a980e046SJonathan Cameron ret = iio_device_register_debugfs(indio_dev); 1194a980e046SJonathan Cameron if (ret) { 1195a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1196a980e046SJonathan Cameron "Failed to register debugfs interfaces\n"); 119792825ff9SHartmut Knaack return ret; 1198a980e046SJonathan Cameron } 11993e1b6c95SLars-Peter Clausen 12003e1b6c95SLars-Peter Clausen ret = iio_buffer_alloc_sysfs_and_mask(indio_dev); 12013e1b6c95SLars-Peter Clausen if (ret) { 12023e1b6c95SLars-Peter Clausen dev_err(indio_dev->dev.parent, 12033e1b6c95SLars-Peter Clausen "Failed to create buffer sysfs interfaces\n"); 12043e1b6c95SLars-Peter Clausen goto error_unreg_debugfs; 12053e1b6c95SLars-Peter Clausen } 12063e1b6c95SLars-Peter Clausen 1207a980e046SJonathan Cameron ret = iio_device_register_sysfs(indio_dev); 1208a980e046SJonathan Cameron if (ret) { 1209a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1210a980e046SJonathan Cameron "Failed to register sysfs interfaces\n"); 12113e1b6c95SLars-Peter Clausen goto error_buffer_free_sysfs; 1212a980e046SJonathan Cameron } 1213a980e046SJonathan Cameron ret = iio_device_register_eventset(indio_dev); 1214a980e046SJonathan Cameron if (ret) { 1215a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1216a980e046SJonathan Cameron "Failed to register event set\n"); 1217a980e046SJonathan Cameron goto error_free_sysfs; 1218a980e046SJonathan Cameron } 1219a980e046SJonathan Cameron if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) 1220a980e046SJonathan Cameron iio_device_register_trigger_consumer(indio_dev); 1221a980e046SJonathan Cameron 1222a980e046SJonathan Cameron if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) && 1223a980e046SJonathan Cameron indio_dev->setup_ops == NULL) 1224a980e046SJonathan Cameron indio_dev->setup_ops = &noop_ring_setup_ops; 1225a980e046SJonathan Cameron 1226a980e046SJonathan Cameron cdev_init(&indio_dev->chrdev, &iio_buffer_fileops); 1227a980e046SJonathan Cameron indio_dev->chrdev.owner = indio_dev->info->driver_module; 12280d5b7daeSLars-Peter Clausen indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj; 1229a980e046SJonathan Cameron ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1); 1230a980e046SJonathan Cameron if (ret < 0) 12310d5b7daeSLars-Peter Clausen goto error_unreg_eventset; 1232a980e046SJonathan Cameron 12330d5b7daeSLars-Peter Clausen ret = device_add(&indio_dev->dev); 12340d5b7daeSLars-Peter Clausen if (ret < 0) 12350d5b7daeSLars-Peter Clausen goto error_cdev_del; 12360d5b7daeSLars-Peter Clausen 12370d5b7daeSLars-Peter Clausen return 0; 12380d5b7daeSLars-Peter Clausen error_cdev_del: 12390d5b7daeSLars-Peter Clausen cdev_del(&indio_dev->chrdev); 1240a980e046SJonathan Cameron error_unreg_eventset: 1241a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 1242a980e046SJonathan Cameron error_free_sysfs: 1243a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 12443e1b6c95SLars-Peter Clausen error_buffer_free_sysfs: 12453e1b6c95SLars-Peter Clausen iio_buffer_free_sysfs_and_mask(indio_dev); 1246a980e046SJonathan Cameron error_unreg_debugfs: 1247a980e046SJonathan Cameron iio_device_unregister_debugfs(indio_dev); 1248a980e046SJonathan Cameron return ret; 1249a980e046SJonathan Cameron } 1250a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_register); 1251a980e046SJonathan Cameron 1252a7e57dceSSachin Kamat /** 1253a7e57dceSSachin Kamat * iio_device_unregister() - unregister a device from the IIO subsystem 1254a7e57dceSSachin Kamat * @indio_dev: Device structure representing the device. 1255a7e57dceSSachin Kamat **/ 1256a980e046SJonathan Cameron void iio_device_unregister(struct iio_dev *indio_dev) 1257a980e046SJonathan Cameron { 1258a980e046SJonathan Cameron mutex_lock(&indio_dev->info_exist_lock); 1259a87c82e4SLars-Peter Clausen 1260a87c82e4SLars-Peter Clausen device_del(&indio_dev->dev); 1261a87c82e4SLars-Peter Clausen 12620d5b7daeSLars-Peter Clausen if (indio_dev->chrdev.dev) 12630d5b7daeSLars-Peter Clausen cdev_del(&indio_dev->chrdev); 1264bc4c9612SLars-Peter Clausen iio_device_unregister_debugfs(indio_dev); 12650d5b7daeSLars-Peter Clausen 1266a87c82e4SLars-Peter Clausen iio_disable_all_buffers(indio_dev); 1267a87c82e4SLars-Peter Clausen 1268a980e046SJonathan Cameron indio_dev->info = NULL; 1269d2f0a48fSLars-Peter Clausen 1270d2f0a48fSLars-Peter Clausen iio_device_wakeup_eventset(indio_dev); 1271d2f0a48fSLars-Peter Clausen iio_buffer_wakeup_poll(indio_dev); 1272d2f0a48fSLars-Peter Clausen 1273a980e046SJonathan Cameron mutex_unlock(&indio_dev->info_exist_lock); 12743e1b6c95SLars-Peter Clausen 12753e1b6c95SLars-Peter Clausen iio_buffer_free_sysfs_and_mask(indio_dev); 1276a980e046SJonathan Cameron } 1277a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_unregister); 12788caa07c0SSachin Kamat 12798caa07c0SSachin Kamat static void devm_iio_device_unreg(struct device *dev, void *res) 12808caa07c0SSachin Kamat { 12818caa07c0SSachin Kamat iio_device_unregister(*(struct iio_dev **)res); 12828caa07c0SSachin Kamat } 12838caa07c0SSachin Kamat 12848caa07c0SSachin Kamat /** 12858caa07c0SSachin Kamat * devm_iio_device_register - Resource-managed iio_device_register() 12868caa07c0SSachin Kamat * @dev: Device to allocate iio_dev for 12878caa07c0SSachin Kamat * @indio_dev: Device structure filled by the device driver 12888caa07c0SSachin Kamat * 12898caa07c0SSachin Kamat * Managed iio_device_register. The IIO device registered with this 12908caa07c0SSachin Kamat * function is automatically unregistered on driver detach. This function 12918caa07c0SSachin Kamat * calls iio_device_register() internally. Refer to that function for more 12928caa07c0SSachin Kamat * information. 12938caa07c0SSachin Kamat * 12948caa07c0SSachin Kamat * If an iio_dev registered with this function needs to be unregistered 12958caa07c0SSachin Kamat * separately, devm_iio_device_unregister() must be used. 12968caa07c0SSachin Kamat * 12978caa07c0SSachin Kamat * RETURNS: 12988caa07c0SSachin Kamat * 0 on success, negative error number on failure. 12998caa07c0SSachin Kamat */ 13008caa07c0SSachin Kamat int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev) 13018caa07c0SSachin Kamat { 13028caa07c0SSachin Kamat struct iio_dev **ptr; 13038caa07c0SSachin Kamat int ret; 13048caa07c0SSachin Kamat 13058caa07c0SSachin Kamat ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL); 13068caa07c0SSachin Kamat if (!ptr) 13078caa07c0SSachin Kamat return -ENOMEM; 13088caa07c0SSachin Kamat 13098caa07c0SSachin Kamat *ptr = indio_dev; 13108caa07c0SSachin Kamat ret = iio_device_register(indio_dev); 13118caa07c0SSachin Kamat if (!ret) 13128caa07c0SSachin Kamat devres_add(dev, ptr); 13138caa07c0SSachin Kamat else 13148caa07c0SSachin Kamat devres_free(ptr); 13158caa07c0SSachin Kamat 13168caa07c0SSachin Kamat return ret; 13178caa07c0SSachin Kamat } 13188caa07c0SSachin Kamat EXPORT_SYMBOL_GPL(devm_iio_device_register); 13198caa07c0SSachin Kamat 13208caa07c0SSachin Kamat /** 13218caa07c0SSachin Kamat * devm_iio_device_unregister - Resource-managed iio_device_unregister() 13228caa07c0SSachin Kamat * @dev: Device this iio_dev belongs to 13238caa07c0SSachin Kamat * @indio_dev: the iio_dev associated with the device 13248caa07c0SSachin Kamat * 13258caa07c0SSachin Kamat * Unregister iio_dev registered with devm_iio_device_register(). 13268caa07c0SSachin Kamat */ 13278caa07c0SSachin Kamat void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev) 13288caa07c0SSachin Kamat { 13298caa07c0SSachin Kamat int rc; 13308caa07c0SSachin Kamat 13318caa07c0SSachin Kamat rc = devres_release(dev, devm_iio_device_unreg, 13328caa07c0SSachin Kamat devm_iio_device_match, indio_dev); 13338caa07c0SSachin Kamat WARN_ON(rc); 13348caa07c0SSachin Kamat } 13358caa07c0SSachin Kamat EXPORT_SYMBOL_GPL(devm_iio_device_unregister); 13368caa07c0SSachin Kamat 1337a980e046SJonathan Cameron subsys_initcall(iio_init); 1338a980e046SJonathan Cameron module_exit(iio_exit); 1339a980e046SJonathan Cameron 1340c8b95952SJonathan Cameron MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); 1341a980e046SJonathan Cameron MODULE_DESCRIPTION("Industrial I/O core"); 1342a980e046SJonathan Cameron MODULE_LICENSE("GPL"); 1343