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)", 104*1ce87f21SLars-Peter Clausen [IIO_MOD_I] = "i", 105*1ce87f21SLars-Peter Clausen [IIO_MOD_Q] = "q", 106a980e046SJonathan Cameron }; 107a980e046SJonathan Cameron 108a980e046SJonathan Cameron /* relies on pairs of these shared then separate */ 109a980e046SJonathan Cameron static const char * const iio_chan_info_postfix[] = { 110a980e046SJonathan Cameron [IIO_CHAN_INFO_RAW] = "raw", 111a980e046SJonathan Cameron [IIO_CHAN_INFO_PROCESSED] = "input", 112a980e046SJonathan Cameron [IIO_CHAN_INFO_SCALE] = "scale", 113a980e046SJonathan Cameron [IIO_CHAN_INFO_OFFSET] = "offset", 114a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBSCALE] = "calibscale", 115a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBBIAS] = "calibbias", 116a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK] = "peak_raw", 117a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale", 118a980e046SJonathan Cameron [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw", 119a980e046SJonathan Cameron [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw", 120a980e046SJonathan Cameron [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY] 121a980e046SJonathan Cameron = "filter_low_pass_3db_frequency", 1223f7f642bSMartin Fuzzey [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY] 1233f7f642bSMartin Fuzzey = "filter_high_pass_3db_frequency", 124a980e046SJonathan Cameron [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency", 125a6b12855SMichael Hennerich [IIO_CHAN_INFO_FREQUENCY] = "frequency", 126a6b12855SMichael Hennerich [IIO_CHAN_INFO_PHASE] = "phase", 127b65d6212SMichael Hennerich [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain", 1287c9ab035Ssrinivas pandruvada [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis", 129899d90bdSPeter Meerwald [IIO_CHAN_INFO_INT_TIME] = "integration_time", 130a88bfe78SIrina Tirdea [IIO_CHAN_INFO_ENABLE] = "en", 131bcdf28fbSIrina Tirdea [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight", 132d37f6836SIrina Tirdea [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight", 1332f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count", 1342f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time", 135c8a85854SVianney le Clément de Saint-Marcq [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity", 136faaa4495SIrina Tirdea [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio", 137a980e046SJonathan Cameron }; 138a980e046SJonathan Cameron 139a7e57dceSSachin Kamat /** 140a7e57dceSSachin Kamat * iio_find_channel_from_si() - get channel from its scan index 141a7e57dceSSachin Kamat * @indio_dev: device 142a7e57dceSSachin Kamat * @si: scan index to match 143a7e57dceSSachin Kamat */ 144a980e046SJonathan Cameron const struct iio_chan_spec 145a980e046SJonathan Cameron *iio_find_channel_from_si(struct iio_dev *indio_dev, int si) 146a980e046SJonathan Cameron { 147a980e046SJonathan Cameron int i; 148a980e046SJonathan Cameron 149a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) 150a980e046SJonathan Cameron if (indio_dev->channels[i].scan_index == si) 151a980e046SJonathan Cameron return &indio_dev->channels[i]; 152a980e046SJonathan Cameron return NULL; 153a980e046SJonathan Cameron } 154a980e046SJonathan Cameron 155a980e046SJonathan Cameron /* This turns up an awful lot */ 156a980e046SJonathan Cameron ssize_t iio_read_const_attr(struct device *dev, 157a980e046SJonathan Cameron struct device_attribute *attr, 158a980e046SJonathan Cameron char *buf) 159a980e046SJonathan Cameron { 160a980e046SJonathan Cameron return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string); 161a980e046SJonathan Cameron } 162a980e046SJonathan Cameron EXPORT_SYMBOL(iio_read_const_attr); 163a980e046SJonathan Cameron 164a980e046SJonathan Cameron static int __init iio_init(void) 165a980e046SJonathan Cameron { 166a980e046SJonathan Cameron int ret; 167a980e046SJonathan Cameron 168a980e046SJonathan Cameron /* Register sysfs bus */ 169a980e046SJonathan Cameron ret = bus_register(&iio_bus_type); 170a980e046SJonathan Cameron if (ret < 0) { 1713176dd5dSSachin Kamat pr_err("could not register bus type\n"); 172a980e046SJonathan Cameron goto error_nothing; 173a980e046SJonathan Cameron } 174a980e046SJonathan Cameron 175a980e046SJonathan Cameron ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio"); 176a980e046SJonathan Cameron if (ret < 0) { 1773176dd5dSSachin Kamat pr_err("failed to allocate char dev region\n"); 178a980e046SJonathan Cameron goto error_unregister_bus_type; 179a980e046SJonathan Cameron } 180a980e046SJonathan Cameron 181a980e046SJonathan Cameron iio_debugfs_dentry = debugfs_create_dir("iio", NULL); 182a980e046SJonathan Cameron 183a980e046SJonathan Cameron return 0; 184a980e046SJonathan Cameron 185a980e046SJonathan Cameron error_unregister_bus_type: 186a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 187a980e046SJonathan Cameron error_nothing: 188a980e046SJonathan Cameron return ret; 189a980e046SJonathan Cameron } 190a980e046SJonathan Cameron 191a980e046SJonathan Cameron static void __exit iio_exit(void) 192a980e046SJonathan Cameron { 193a980e046SJonathan Cameron if (iio_devt) 194a980e046SJonathan Cameron unregister_chrdev_region(iio_devt, IIO_DEV_MAX); 195a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 196a980e046SJonathan Cameron debugfs_remove(iio_debugfs_dentry); 197a980e046SJonathan Cameron } 198a980e046SJonathan Cameron 199a980e046SJonathan Cameron #if defined(CONFIG_DEBUG_FS) 200a980e046SJonathan Cameron static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf, 201a980e046SJonathan Cameron size_t count, loff_t *ppos) 202a980e046SJonathan Cameron { 203a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 204a980e046SJonathan Cameron char buf[20]; 205a980e046SJonathan Cameron unsigned val = 0; 206a980e046SJonathan Cameron ssize_t len; 207a980e046SJonathan Cameron int ret; 208a980e046SJonathan Cameron 209a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, 210a980e046SJonathan Cameron indio_dev->cached_reg_addr, 211a980e046SJonathan Cameron 0, &val); 212a980e046SJonathan Cameron if (ret) 213a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__); 214a980e046SJonathan Cameron 215a980e046SJonathan Cameron len = snprintf(buf, sizeof(buf), "0x%X\n", val); 216a980e046SJonathan Cameron 217a980e046SJonathan Cameron return simple_read_from_buffer(userbuf, count, ppos, buf, len); 218a980e046SJonathan Cameron } 219a980e046SJonathan Cameron 220a980e046SJonathan Cameron static ssize_t iio_debugfs_write_reg(struct file *file, 221a980e046SJonathan Cameron const char __user *userbuf, size_t count, loff_t *ppos) 222a980e046SJonathan Cameron { 223a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 224a980e046SJonathan Cameron unsigned reg, val; 225a980e046SJonathan Cameron char buf[80]; 226a980e046SJonathan Cameron int ret; 227a980e046SJonathan Cameron 228a980e046SJonathan Cameron count = min_t(size_t, count, (sizeof(buf)-1)); 229a980e046SJonathan Cameron if (copy_from_user(buf, userbuf, count)) 230a980e046SJonathan Cameron return -EFAULT; 231a980e046SJonathan Cameron 232a980e046SJonathan Cameron buf[count] = 0; 233a980e046SJonathan Cameron 234a980e046SJonathan Cameron ret = sscanf(buf, "%i %i", ®, &val); 235a980e046SJonathan Cameron 236a980e046SJonathan Cameron switch (ret) { 237a980e046SJonathan Cameron case 1: 238a980e046SJonathan Cameron indio_dev->cached_reg_addr = reg; 239a980e046SJonathan Cameron break; 240a980e046SJonathan Cameron case 2: 241a980e046SJonathan Cameron indio_dev->cached_reg_addr = reg; 242a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, reg, 243a980e046SJonathan Cameron val, NULL); 244a980e046SJonathan Cameron if (ret) { 245a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: write failed\n", 246a980e046SJonathan Cameron __func__); 247a980e046SJonathan Cameron return ret; 248a980e046SJonathan Cameron } 249a980e046SJonathan Cameron break; 250a980e046SJonathan Cameron default: 251a980e046SJonathan Cameron return -EINVAL; 252a980e046SJonathan Cameron } 253a980e046SJonathan Cameron 254a980e046SJonathan Cameron return count; 255a980e046SJonathan Cameron } 256a980e046SJonathan Cameron 257a980e046SJonathan Cameron static const struct file_operations iio_debugfs_reg_fops = { 2585a28c873SAxel Lin .open = simple_open, 259a980e046SJonathan Cameron .read = iio_debugfs_read_reg, 260a980e046SJonathan Cameron .write = iio_debugfs_write_reg, 261a980e046SJonathan Cameron }; 262a980e046SJonathan Cameron 263a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 264a980e046SJonathan Cameron { 265a980e046SJonathan Cameron debugfs_remove_recursive(indio_dev->debugfs_dentry); 266a980e046SJonathan Cameron } 267a980e046SJonathan Cameron 268a980e046SJonathan Cameron static int iio_device_register_debugfs(struct iio_dev *indio_dev) 269a980e046SJonathan Cameron { 270a980e046SJonathan Cameron struct dentry *d; 271a980e046SJonathan Cameron 272a980e046SJonathan Cameron if (indio_dev->info->debugfs_reg_access == NULL) 273a980e046SJonathan Cameron return 0; 274a980e046SJonathan Cameron 275abd5a2fbSAxel Lin if (!iio_debugfs_dentry) 276a980e046SJonathan Cameron return 0; 277a980e046SJonathan Cameron 278a980e046SJonathan Cameron indio_dev->debugfs_dentry = 279a980e046SJonathan Cameron debugfs_create_dir(dev_name(&indio_dev->dev), 280a980e046SJonathan Cameron iio_debugfs_dentry); 281a980e046SJonathan Cameron if (indio_dev->debugfs_dentry == NULL) { 282a980e046SJonathan Cameron dev_warn(indio_dev->dev.parent, 283a980e046SJonathan Cameron "Failed to create debugfs directory\n"); 284a980e046SJonathan Cameron return -EFAULT; 285a980e046SJonathan Cameron } 286a980e046SJonathan Cameron 287a980e046SJonathan Cameron d = debugfs_create_file("direct_reg_access", 0644, 288a980e046SJonathan Cameron indio_dev->debugfs_dentry, 289a980e046SJonathan Cameron indio_dev, &iio_debugfs_reg_fops); 290a980e046SJonathan Cameron if (!d) { 291a980e046SJonathan Cameron iio_device_unregister_debugfs(indio_dev); 292a980e046SJonathan Cameron return -ENOMEM; 293a980e046SJonathan Cameron } 294a980e046SJonathan Cameron 295a980e046SJonathan Cameron return 0; 296a980e046SJonathan Cameron } 297a980e046SJonathan Cameron #else 298a980e046SJonathan Cameron static int iio_device_register_debugfs(struct iio_dev *indio_dev) 299a980e046SJonathan Cameron { 300a980e046SJonathan Cameron return 0; 301a980e046SJonathan Cameron } 302a980e046SJonathan Cameron 303a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 304a980e046SJonathan Cameron { 305a980e046SJonathan Cameron } 306a980e046SJonathan Cameron #endif /* CONFIG_DEBUG_FS */ 307a980e046SJonathan Cameron 308a980e046SJonathan Cameron static ssize_t iio_read_channel_ext_info(struct device *dev, 309a980e046SJonathan Cameron struct device_attribute *attr, 310a980e046SJonathan Cameron char *buf) 311a980e046SJonathan Cameron { 312e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 313a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 314a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 315a980e046SJonathan Cameron 316a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 317a980e046SJonathan Cameron 318fc6d1139SMichael Hennerich return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf); 319a980e046SJonathan Cameron } 320a980e046SJonathan Cameron 321a980e046SJonathan Cameron static ssize_t iio_write_channel_ext_info(struct device *dev, 322a980e046SJonathan Cameron struct device_attribute *attr, 323a980e046SJonathan Cameron const char *buf, 324a980e046SJonathan Cameron size_t len) 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->write(indio_dev, ext_info->private, 333fc6d1139SMichael Hennerich this_attr->c, buf, len); 334a980e046SJonathan Cameron } 335a980e046SJonathan Cameron 3365212cc8aSLars-Peter Clausen ssize_t iio_enum_available_read(struct iio_dev *indio_dev, 3375212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 3385212cc8aSLars-Peter Clausen { 3395212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3405212cc8aSLars-Peter Clausen unsigned int i; 3415212cc8aSLars-Peter Clausen size_t len = 0; 3425212cc8aSLars-Peter Clausen 3435212cc8aSLars-Peter Clausen if (!e->num_items) 3445212cc8aSLars-Peter Clausen return 0; 3455212cc8aSLars-Peter Clausen 3465212cc8aSLars-Peter Clausen for (i = 0; i < e->num_items; ++i) 34774dcd439SLars-Peter Clausen len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]); 3485212cc8aSLars-Peter Clausen 3495212cc8aSLars-Peter Clausen /* replace last space with a newline */ 3505212cc8aSLars-Peter Clausen buf[len - 1] = '\n'; 3515212cc8aSLars-Peter Clausen 3525212cc8aSLars-Peter Clausen return len; 3535212cc8aSLars-Peter Clausen } 3545212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_available_read); 3555212cc8aSLars-Peter Clausen 3565212cc8aSLars-Peter Clausen ssize_t iio_enum_read(struct iio_dev *indio_dev, 3575212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 3585212cc8aSLars-Peter Clausen { 3595212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3605212cc8aSLars-Peter Clausen int i; 3615212cc8aSLars-Peter Clausen 3625212cc8aSLars-Peter Clausen if (!e->get) 3635212cc8aSLars-Peter Clausen return -EINVAL; 3645212cc8aSLars-Peter Clausen 3655212cc8aSLars-Peter Clausen i = e->get(indio_dev, chan); 3665212cc8aSLars-Peter Clausen if (i < 0) 3675212cc8aSLars-Peter Clausen return i; 3685212cc8aSLars-Peter Clausen else if (i >= e->num_items) 3695212cc8aSLars-Peter Clausen return -EINVAL; 3705212cc8aSLars-Peter Clausen 371598db581SKees Cook return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]); 3725212cc8aSLars-Peter Clausen } 3735212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_read); 3745212cc8aSLars-Peter Clausen 3755212cc8aSLars-Peter Clausen ssize_t iio_enum_write(struct iio_dev *indio_dev, 3765212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, const char *buf, 3775212cc8aSLars-Peter Clausen size_t len) 3785212cc8aSLars-Peter Clausen { 3795212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3805212cc8aSLars-Peter Clausen unsigned int i; 3815212cc8aSLars-Peter Clausen int ret; 3825212cc8aSLars-Peter Clausen 3835212cc8aSLars-Peter Clausen if (!e->set) 3845212cc8aSLars-Peter Clausen return -EINVAL; 3855212cc8aSLars-Peter Clausen 3865212cc8aSLars-Peter Clausen for (i = 0; i < e->num_items; i++) { 3875212cc8aSLars-Peter Clausen if (sysfs_streq(buf, e->items[i])) 3885212cc8aSLars-Peter Clausen break; 3895212cc8aSLars-Peter Clausen } 3905212cc8aSLars-Peter Clausen 3915212cc8aSLars-Peter Clausen if (i == e->num_items) 3925212cc8aSLars-Peter Clausen return -EINVAL; 3935212cc8aSLars-Peter Clausen 3945212cc8aSLars-Peter Clausen ret = e->set(indio_dev, chan, i); 3955212cc8aSLars-Peter Clausen return ret ? ret : len; 3965212cc8aSLars-Peter Clausen } 3975212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_write); 3985212cc8aSLars-Peter Clausen 3993661f3f5SLars-Peter Clausen /** 4003661f3f5SLars-Peter Clausen * iio_format_value() - Formats a IIO value into its string representation 4013661f3f5SLars-Peter Clausen * @buf: The buffer to which the formated value gets written 4023661f3f5SLars-Peter Clausen * @type: One of the IIO_VAL_... constants. This decides how the val and val2 4033661f3f5SLars-Peter Clausen * parameters are formatted. 4049fbfb4b3SSrinivas Pandruvada * @vals: pointer to the values, exact meaning depends on the type parameter. 4053661f3f5SLars-Peter Clausen */ 4069fbfb4b3SSrinivas Pandruvada ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals) 407a980e046SJonathan Cameron { 4087985e7c1SLars-Peter Clausen unsigned long long tmp; 40967eedba3SMichael Hennerich bool scale_db = false; 410a980e046SJonathan Cameron 4113661f3f5SLars-Peter Clausen switch (type) { 41267eedba3SMichael Hennerich case IIO_VAL_INT: 4139fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d\n", vals[0]); 41467eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_MICRO_DB: 41567eedba3SMichael Hennerich scale_db = true; 41667eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_MICRO: 4179fbfb4b3SSrinivas Pandruvada if (vals[1] < 0) 4189fbfb4b3SSrinivas Pandruvada return sprintf(buf, "-%ld.%06u%s\n", abs(vals[0]), 4199fbfb4b3SSrinivas Pandruvada -vals[1], 42067eedba3SMichael Hennerich scale_db ? " dB" : ""); 421a980e046SJonathan Cameron else 4229fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1], 42367eedba3SMichael Hennerich scale_db ? " dB" : ""); 42467eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_NANO: 4259fbfb4b3SSrinivas Pandruvada if (vals[1] < 0) 4269fbfb4b3SSrinivas Pandruvada return sprintf(buf, "-%ld.%09u\n", abs(vals[0]), 4279fbfb4b3SSrinivas Pandruvada -vals[1]); 428a980e046SJonathan Cameron else 4299fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 4307985e7c1SLars-Peter Clausen case IIO_VAL_FRACTIONAL: 4319fbfb4b3SSrinivas Pandruvada tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]); 4329fbfb4b3SSrinivas Pandruvada vals[1] = do_div(tmp, 1000000000LL); 4339fbfb4b3SSrinivas Pandruvada vals[0] = tmp; 4349fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 435103d9fb9SLars-Peter Clausen case IIO_VAL_FRACTIONAL_LOG2: 4369fbfb4b3SSrinivas Pandruvada tmp = (s64)vals[0] * 1000000000LL >> vals[1]; 4379fbfb4b3SSrinivas Pandruvada vals[1] = do_div(tmp, 1000000000LL); 4389fbfb4b3SSrinivas Pandruvada vals[0] = tmp; 4399fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 4409fbfb4b3SSrinivas Pandruvada case IIO_VAL_INT_MULTIPLE: 4419fbfb4b3SSrinivas Pandruvada { 4429fbfb4b3SSrinivas Pandruvada int i; 4439fbfb4b3SSrinivas Pandruvada int len = 0; 4449fbfb4b3SSrinivas Pandruvada 4459fbfb4b3SSrinivas Pandruvada for (i = 0; i < size; ++i) 4469fbfb4b3SSrinivas Pandruvada len += snprintf(&buf[len], PAGE_SIZE - len, "%d ", 4479fbfb4b3SSrinivas Pandruvada vals[i]); 4489fbfb4b3SSrinivas Pandruvada len += snprintf(&buf[len], PAGE_SIZE - len, "\n"); 4499fbfb4b3SSrinivas Pandruvada return len; 4509fbfb4b3SSrinivas Pandruvada } 45167eedba3SMichael Hennerich default: 452a980e046SJonathan Cameron return 0; 453a980e046SJonathan Cameron } 45467eedba3SMichael Hennerich } 455a980e046SJonathan Cameron 4563661f3f5SLars-Peter Clausen static ssize_t iio_read_channel_info(struct device *dev, 4573661f3f5SLars-Peter Clausen struct device_attribute *attr, 4583661f3f5SLars-Peter Clausen char *buf) 4593661f3f5SLars-Peter Clausen { 4603661f3f5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 4613661f3f5SLars-Peter Clausen struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 4629fbfb4b3SSrinivas Pandruvada int vals[INDIO_MAX_RAW_ELEMENTS]; 4639fbfb4b3SSrinivas Pandruvada int ret; 4649fbfb4b3SSrinivas Pandruvada int val_len = 2; 4659fbfb4b3SSrinivas Pandruvada 4669fbfb4b3SSrinivas Pandruvada if (indio_dev->info->read_raw_multi) 4679fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c, 4689fbfb4b3SSrinivas Pandruvada INDIO_MAX_RAW_ELEMENTS, 4699fbfb4b3SSrinivas Pandruvada vals, &val_len, 4709fbfb4b3SSrinivas Pandruvada this_attr->address); 4719fbfb4b3SSrinivas Pandruvada else 4729fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw(indio_dev, this_attr->c, 4739fbfb4b3SSrinivas Pandruvada &vals[0], &vals[1], this_attr->address); 4743661f3f5SLars-Peter Clausen 4753661f3f5SLars-Peter Clausen if (ret < 0) 4763661f3f5SLars-Peter Clausen return ret; 4773661f3f5SLars-Peter Clausen 4789fbfb4b3SSrinivas Pandruvada return iio_format_value(buf, ret, val_len, vals); 4793661f3f5SLars-Peter Clausen } 4803661f3f5SLars-Peter Clausen 4816807d721SLars-Peter Clausen /** 4826807d721SLars-Peter Clausen * iio_str_to_fixpoint() - Parse a fixed-point number from a string 4836807d721SLars-Peter Clausen * @str: The string to parse 4846807d721SLars-Peter Clausen * @fract_mult: Multiplier for the first decimal place, should be a power of 10 4856807d721SLars-Peter Clausen * @integer: The integer part of the number 4866807d721SLars-Peter Clausen * @fract: The fractional part of the number 4876807d721SLars-Peter Clausen * 4886807d721SLars-Peter Clausen * Returns 0 on success, or a negative error code if the string could not be 4896807d721SLars-Peter Clausen * parsed. 4906807d721SLars-Peter Clausen */ 4916807d721SLars-Peter Clausen int iio_str_to_fixpoint(const char *str, int fract_mult, 4926807d721SLars-Peter Clausen int *integer, int *fract) 4936807d721SLars-Peter Clausen { 4946807d721SLars-Peter Clausen int i = 0, f = 0; 4956807d721SLars-Peter Clausen bool integer_part = true, negative = false; 4966807d721SLars-Peter Clausen 4976807d721SLars-Peter Clausen if (str[0] == '-') { 4986807d721SLars-Peter Clausen negative = true; 4996807d721SLars-Peter Clausen str++; 5006807d721SLars-Peter Clausen } else if (str[0] == '+') { 5016807d721SLars-Peter Clausen str++; 5026807d721SLars-Peter Clausen } 5036807d721SLars-Peter Clausen 5046807d721SLars-Peter Clausen while (*str) { 5056807d721SLars-Peter Clausen if ('0' <= *str && *str <= '9') { 5066807d721SLars-Peter Clausen if (integer_part) { 5076807d721SLars-Peter Clausen i = i * 10 + *str - '0'; 5086807d721SLars-Peter Clausen } else { 5096807d721SLars-Peter Clausen f += fract_mult * (*str - '0'); 5106807d721SLars-Peter Clausen fract_mult /= 10; 5116807d721SLars-Peter Clausen } 5126807d721SLars-Peter Clausen } else if (*str == '\n') { 5136807d721SLars-Peter Clausen if (*(str + 1) == '\0') 5146807d721SLars-Peter Clausen break; 5156807d721SLars-Peter Clausen else 5166807d721SLars-Peter Clausen return -EINVAL; 5176807d721SLars-Peter Clausen } else if (*str == '.' && integer_part) { 5186807d721SLars-Peter Clausen integer_part = false; 5196807d721SLars-Peter Clausen } else { 5206807d721SLars-Peter Clausen return -EINVAL; 5216807d721SLars-Peter Clausen } 5226807d721SLars-Peter Clausen str++; 5236807d721SLars-Peter Clausen } 5246807d721SLars-Peter Clausen 5256807d721SLars-Peter Clausen if (negative) { 5266807d721SLars-Peter Clausen if (i) 5276807d721SLars-Peter Clausen i = -i; 5286807d721SLars-Peter Clausen else 5296807d721SLars-Peter Clausen f = -f; 5306807d721SLars-Peter Clausen } 5316807d721SLars-Peter Clausen 5326807d721SLars-Peter Clausen *integer = i; 5336807d721SLars-Peter Clausen *fract = f; 5346807d721SLars-Peter Clausen 5356807d721SLars-Peter Clausen return 0; 5366807d721SLars-Peter Clausen } 5376807d721SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_str_to_fixpoint); 5386807d721SLars-Peter Clausen 539a980e046SJonathan Cameron static ssize_t iio_write_channel_info(struct device *dev, 540a980e046SJonathan Cameron struct device_attribute *attr, 541a980e046SJonathan Cameron const char *buf, 542a980e046SJonathan Cameron size_t len) 543a980e046SJonathan Cameron { 544e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 545a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 5466807d721SLars-Peter Clausen int ret, fract_mult = 100000; 5476807d721SLars-Peter Clausen int integer, fract; 548a980e046SJonathan Cameron 549a980e046SJonathan Cameron /* Assumes decimal - precision based on number of digits */ 550a980e046SJonathan Cameron if (!indio_dev->info->write_raw) 551a980e046SJonathan Cameron return -EINVAL; 552a980e046SJonathan Cameron 553a980e046SJonathan Cameron if (indio_dev->info->write_raw_get_fmt) 554a980e046SJonathan Cameron switch (indio_dev->info->write_raw_get_fmt(indio_dev, 555a980e046SJonathan Cameron this_attr->c, this_attr->address)) { 556a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_MICRO: 557a980e046SJonathan Cameron fract_mult = 100000; 558a980e046SJonathan Cameron break; 559a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_NANO: 560a980e046SJonathan Cameron fract_mult = 100000000; 561a980e046SJonathan Cameron break; 562a980e046SJonathan Cameron default: 563a980e046SJonathan Cameron return -EINVAL; 564a980e046SJonathan Cameron } 565a980e046SJonathan Cameron 5666807d721SLars-Peter Clausen ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract); 5676807d721SLars-Peter Clausen if (ret) 5686807d721SLars-Peter Clausen return ret; 569a980e046SJonathan Cameron 570a980e046SJonathan Cameron ret = indio_dev->info->write_raw(indio_dev, this_attr->c, 571a980e046SJonathan Cameron integer, fract, this_attr->address); 572a980e046SJonathan Cameron if (ret) 573a980e046SJonathan Cameron return ret; 574a980e046SJonathan Cameron 575a980e046SJonathan Cameron return len; 576a980e046SJonathan Cameron } 577a980e046SJonathan Cameron 578a980e046SJonathan Cameron static 579a980e046SJonathan Cameron int __iio_device_attr_init(struct device_attribute *dev_attr, 580a980e046SJonathan Cameron const char *postfix, 581a980e046SJonathan Cameron struct iio_chan_spec const *chan, 582a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 583a980e046SJonathan Cameron struct device_attribute *attr, 584a980e046SJonathan Cameron char *buf), 585a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 586a980e046SJonathan Cameron struct device_attribute *attr, 587a980e046SJonathan Cameron const char *buf, 588a980e046SJonathan Cameron size_t len), 5893704432fSJonathan Cameron enum iio_shared_by shared_by) 590a980e046SJonathan Cameron { 5913704432fSJonathan Cameron int ret = 0; 5927bbcf7e1SLars-Peter Clausen char *name = NULL; 5933704432fSJonathan Cameron char *full_postfix; 594a980e046SJonathan Cameron sysfs_attr_init(&dev_attr->attr); 595a980e046SJonathan Cameron 596a980e046SJonathan Cameron /* Build up postfix of <extend_name>_<modifier>_postfix */ 5973704432fSJonathan Cameron if (chan->modified && (shared_by == IIO_SEPARATE)) { 598a980e046SJonathan Cameron if (chan->extend_name) 599a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", 600a980e046SJonathan Cameron iio_modifier_names[chan 601a980e046SJonathan Cameron ->channel2], 602a980e046SJonathan Cameron chan->extend_name, 603a980e046SJonathan Cameron postfix); 604a980e046SJonathan Cameron else 605a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s", 606a980e046SJonathan Cameron iio_modifier_names[chan 607a980e046SJonathan Cameron ->channel2], 608a980e046SJonathan Cameron postfix); 609a980e046SJonathan Cameron } else { 61077bfa8baSLars-Peter Clausen if (chan->extend_name == NULL || shared_by != IIO_SEPARATE) 611a980e046SJonathan Cameron full_postfix = kstrdup(postfix, GFP_KERNEL); 612a980e046SJonathan Cameron else 613a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, 614a980e046SJonathan Cameron "%s_%s", 615a980e046SJonathan Cameron chan->extend_name, 616a980e046SJonathan Cameron postfix); 617a980e046SJonathan Cameron } 6183704432fSJonathan Cameron if (full_postfix == NULL) 6193704432fSJonathan Cameron return -ENOMEM; 620a980e046SJonathan Cameron 621a980e046SJonathan Cameron if (chan->differential) { /* Differential can not have modifier */ 6223704432fSJonathan Cameron switch (shared_by) { 623c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 6247bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 625c006ec83SJonathan Cameron break; 626c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 6277bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 628c006ec83SJonathan Cameron iio_direction[chan->output], 629c006ec83SJonathan Cameron full_postfix); 630c006ec83SJonathan Cameron break; 6313704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 6327bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s", 633a980e046SJonathan Cameron iio_direction[chan->output], 634a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 635a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 636a980e046SJonathan Cameron full_postfix); 6373704432fSJonathan Cameron break; 6383704432fSJonathan Cameron case IIO_SEPARATE: 6393704432fSJonathan Cameron if (!chan->indexed) { 6403704432fSJonathan Cameron WARN_ON("Differential channels must be indexed\n"); 6413704432fSJonathan Cameron ret = -EINVAL; 6423704432fSJonathan Cameron goto error_free_full_postfix; 6433704432fSJonathan Cameron } 6447bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, 6453704432fSJonathan Cameron "%s_%s%d-%s%d_%s", 646a980e046SJonathan Cameron iio_direction[chan->output], 647a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 648a980e046SJonathan Cameron chan->channel, 649a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 650a980e046SJonathan Cameron chan->channel2, 651a980e046SJonathan Cameron full_postfix); 6523704432fSJonathan Cameron break; 653a980e046SJonathan Cameron } 654a980e046SJonathan Cameron } else { /* Single ended */ 6553704432fSJonathan Cameron switch (shared_by) { 656c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 6577bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 658c006ec83SJonathan Cameron break; 659c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 6607bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 661c006ec83SJonathan Cameron iio_direction[chan->output], 662c006ec83SJonathan Cameron full_postfix); 663c006ec83SJonathan Cameron break; 6643704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 6657bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 666a980e046SJonathan Cameron iio_direction[chan->output], 667a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 668a980e046SJonathan Cameron full_postfix); 6693704432fSJonathan Cameron break; 6703704432fSJonathan Cameron 6713704432fSJonathan Cameron case IIO_SEPARATE: 6723704432fSJonathan Cameron if (chan->indexed) 6737bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s%d_%s", 674a980e046SJonathan Cameron iio_direction[chan->output], 675a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 676a980e046SJonathan Cameron chan->channel, 677a980e046SJonathan Cameron full_postfix); 678a980e046SJonathan Cameron else 6797bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 680a980e046SJonathan Cameron iio_direction[chan->output], 681a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 682a980e046SJonathan Cameron full_postfix); 6833704432fSJonathan Cameron break; 6843704432fSJonathan Cameron } 685a980e046SJonathan Cameron } 6867bbcf7e1SLars-Peter Clausen if (name == NULL) { 687a980e046SJonathan Cameron ret = -ENOMEM; 688a980e046SJonathan Cameron goto error_free_full_postfix; 689a980e046SJonathan Cameron } 6907bbcf7e1SLars-Peter Clausen dev_attr->attr.name = name; 691a980e046SJonathan Cameron 692a980e046SJonathan Cameron if (readfunc) { 693a980e046SJonathan Cameron dev_attr->attr.mode |= S_IRUGO; 694a980e046SJonathan Cameron dev_attr->show = readfunc; 695a980e046SJonathan Cameron } 696a980e046SJonathan Cameron 697a980e046SJonathan Cameron if (writefunc) { 698a980e046SJonathan Cameron dev_attr->attr.mode |= S_IWUSR; 699a980e046SJonathan Cameron dev_attr->store = writefunc; 700a980e046SJonathan Cameron } 7017bbcf7e1SLars-Peter Clausen 702a980e046SJonathan Cameron error_free_full_postfix: 703a980e046SJonathan Cameron kfree(full_postfix); 7043704432fSJonathan Cameron 705a980e046SJonathan Cameron return ret; 706a980e046SJonathan Cameron } 707a980e046SJonathan Cameron 708a980e046SJonathan Cameron static void __iio_device_attr_deinit(struct device_attribute *dev_attr) 709a980e046SJonathan Cameron { 710a980e046SJonathan Cameron kfree(dev_attr->attr.name); 711a980e046SJonathan Cameron } 712a980e046SJonathan Cameron 713a980e046SJonathan Cameron int __iio_add_chan_devattr(const char *postfix, 714a980e046SJonathan Cameron struct iio_chan_spec const *chan, 715a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 716a980e046SJonathan Cameron struct device_attribute *attr, 717a980e046SJonathan Cameron char *buf), 718a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 719a980e046SJonathan Cameron struct device_attribute *attr, 720a980e046SJonathan Cameron const char *buf, 721a980e046SJonathan Cameron size_t len), 722a980e046SJonathan Cameron u64 mask, 7233704432fSJonathan Cameron enum iio_shared_by shared_by, 724a980e046SJonathan Cameron struct device *dev, 725a980e046SJonathan Cameron struct list_head *attr_list) 726a980e046SJonathan Cameron { 727a980e046SJonathan Cameron int ret; 728a980e046SJonathan Cameron struct iio_dev_attr *iio_attr, *t; 729a980e046SJonathan Cameron 730670c1103SSachin Kamat iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL); 73192825ff9SHartmut Knaack if (iio_attr == NULL) 73292825ff9SHartmut Knaack return -ENOMEM; 733a980e046SJonathan Cameron ret = __iio_device_attr_init(&iio_attr->dev_attr, 734a980e046SJonathan Cameron postfix, chan, 7353704432fSJonathan Cameron readfunc, writefunc, shared_by); 736a980e046SJonathan Cameron if (ret) 737a980e046SJonathan Cameron goto error_iio_dev_attr_free; 738a980e046SJonathan Cameron iio_attr->c = chan; 739a980e046SJonathan Cameron iio_attr->address = mask; 740a980e046SJonathan Cameron list_for_each_entry(t, attr_list, l) 741a980e046SJonathan Cameron if (strcmp(t->dev_attr.attr.name, 742a980e046SJonathan Cameron iio_attr->dev_attr.attr.name) == 0) { 7433704432fSJonathan Cameron if (shared_by == IIO_SEPARATE) 744a980e046SJonathan Cameron dev_err(dev, "tried to double register : %s\n", 745a980e046SJonathan Cameron t->dev_attr.attr.name); 746a980e046SJonathan Cameron ret = -EBUSY; 747a980e046SJonathan Cameron goto error_device_attr_deinit; 748a980e046SJonathan Cameron } 749a980e046SJonathan Cameron list_add(&iio_attr->l, attr_list); 750a980e046SJonathan Cameron 751a980e046SJonathan Cameron return 0; 752a980e046SJonathan Cameron 753a980e046SJonathan Cameron error_device_attr_deinit: 754a980e046SJonathan Cameron __iio_device_attr_deinit(&iio_attr->dev_attr); 755a980e046SJonathan Cameron error_iio_dev_attr_free: 756a980e046SJonathan Cameron kfree(iio_attr); 757a980e046SJonathan Cameron return ret; 758a980e046SJonathan Cameron } 759a980e046SJonathan Cameron 7603704432fSJonathan Cameron static int iio_device_add_info_mask_type(struct iio_dev *indio_dev, 7613704432fSJonathan Cameron struct iio_chan_spec const *chan, 7623704432fSJonathan Cameron enum iio_shared_by shared_by, 7633704432fSJonathan Cameron const long *infomask) 7643704432fSJonathan Cameron { 7653704432fSJonathan Cameron int i, ret, attrcount = 0; 7663704432fSJonathan Cameron 7673704432fSJonathan Cameron for_each_set_bit(i, infomask, sizeof(infomask)*8) { 768ef4b4856SJonathan Cameron if (i >= ARRAY_SIZE(iio_chan_info_postfix)) 769ef4b4856SJonathan Cameron return -EINVAL; 7703704432fSJonathan Cameron ret = __iio_add_chan_devattr(iio_chan_info_postfix[i], 7713704432fSJonathan Cameron chan, 7723704432fSJonathan Cameron &iio_read_channel_info, 7733704432fSJonathan Cameron &iio_write_channel_info, 7743704432fSJonathan Cameron i, 7753704432fSJonathan Cameron shared_by, 7763704432fSJonathan Cameron &indio_dev->dev, 7773704432fSJonathan Cameron &indio_dev->channel_attr_list); 7783704432fSJonathan Cameron if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) 7793704432fSJonathan Cameron continue; 7803704432fSJonathan Cameron else if (ret < 0) 7813704432fSJonathan Cameron return ret; 7823704432fSJonathan Cameron attrcount++; 7833704432fSJonathan Cameron } 7843704432fSJonathan Cameron 7853704432fSJonathan Cameron return attrcount; 7863704432fSJonathan Cameron } 7873704432fSJonathan Cameron 788a980e046SJonathan Cameron static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev, 789a980e046SJonathan Cameron struct iio_chan_spec const *chan) 790a980e046SJonathan Cameron { 791a980e046SJonathan Cameron int ret, attrcount = 0; 792a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 793a980e046SJonathan Cameron 794a980e046SJonathan Cameron if (chan->channel < 0) 795a980e046SJonathan Cameron return 0; 7963704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 7973704432fSJonathan Cameron IIO_SEPARATE, 7983704432fSJonathan Cameron &chan->info_mask_separate); 7998655cc49SJonathan Cameron if (ret < 0) 8003704432fSJonathan Cameron return ret; 8013704432fSJonathan Cameron attrcount += ret; 8023704432fSJonathan Cameron 8033704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 8043704432fSJonathan Cameron IIO_SHARED_BY_TYPE, 8053704432fSJonathan Cameron &chan->info_mask_shared_by_type); 8063704432fSJonathan Cameron if (ret < 0) 8073704432fSJonathan Cameron return ret; 8083704432fSJonathan Cameron attrcount += ret; 809a980e046SJonathan Cameron 810c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 811c006ec83SJonathan Cameron IIO_SHARED_BY_DIR, 812c006ec83SJonathan Cameron &chan->info_mask_shared_by_dir); 813c006ec83SJonathan Cameron if (ret < 0) 814c006ec83SJonathan Cameron return ret; 815c006ec83SJonathan Cameron attrcount += ret; 816c006ec83SJonathan Cameron 817c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 818c006ec83SJonathan Cameron IIO_SHARED_BY_ALL, 819c006ec83SJonathan Cameron &chan->info_mask_shared_by_all); 820c006ec83SJonathan Cameron if (ret < 0) 821c006ec83SJonathan Cameron return ret; 822c006ec83SJonathan Cameron attrcount += ret; 823c006ec83SJonathan Cameron 824a980e046SJonathan Cameron if (chan->ext_info) { 825a980e046SJonathan Cameron unsigned int i = 0; 826a980e046SJonathan Cameron for (ext_info = chan->ext_info; ext_info->name; ext_info++) { 827a980e046SJonathan Cameron ret = __iio_add_chan_devattr(ext_info->name, 828a980e046SJonathan Cameron chan, 829a980e046SJonathan Cameron ext_info->read ? 830a980e046SJonathan Cameron &iio_read_channel_ext_info : NULL, 831a980e046SJonathan Cameron ext_info->write ? 832a980e046SJonathan Cameron &iio_write_channel_ext_info : NULL, 833a980e046SJonathan Cameron i, 834a980e046SJonathan Cameron ext_info->shared, 835a980e046SJonathan Cameron &indio_dev->dev, 836a980e046SJonathan Cameron &indio_dev->channel_attr_list); 837a980e046SJonathan Cameron i++; 838a980e046SJonathan Cameron if (ret == -EBUSY && ext_info->shared) 839a980e046SJonathan Cameron continue; 840a980e046SJonathan Cameron 841a980e046SJonathan Cameron if (ret) 8423704432fSJonathan Cameron return ret; 843a980e046SJonathan Cameron 844a980e046SJonathan Cameron attrcount++; 845a980e046SJonathan Cameron } 846a980e046SJonathan Cameron } 847a980e046SJonathan Cameron 8483704432fSJonathan Cameron return attrcount; 849a980e046SJonathan Cameron } 850a980e046SJonathan Cameron 85184088ebdSLars-Peter Clausen /** 85284088ebdSLars-Peter Clausen * iio_free_chan_devattr_list() - Free a list of IIO device attributes 85384088ebdSLars-Peter Clausen * @attr_list: List of IIO device attributes 85484088ebdSLars-Peter Clausen * 85584088ebdSLars-Peter Clausen * This function frees the memory allocated for each of the IIO device 856c1b03ab5SMartin Fuzzey * attributes in the list. 85784088ebdSLars-Peter Clausen */ 85884088ebdSLars-Peter Clausen void iio_free_chan_devattr_list(struct list_head *attr_list) 859a980e046SJonathan Cameron { 86084088ebdSLars-Peter Clausen struct iio_dev_attr *p, *n; 86184088ebdSLars-Peter Clausen 86284088ebdSLars-Peter Clausen list_for_each_entry_safe(p, n, attr_list, l) { 863a980e046SJonathan Cameron kfree(p->dev_attr.attr.name); 864c1b03ab5SMartin Fuzzey list_del(&p->l); 865a980e046SJonathan Cameron kfree(p); 866a980e046SJonathan Cameron } 86784088ebdSLars-Peter Clausen } 868a980e046SJonathan Cameron 869a980e046SJonathan Cameron static ssize_t iio_show_dev_name(struct device *dev, 870a980e046SJonathan Cameron struct device_attribute *attr, 871a980e046SJonathan Cameron char *buf) 872a980e046SJonathan Cameron { 873e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 874598db581SKees Cook return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name); 875a980e046SJonathan Cameron } 876a980e046SJonathan Cameron 877a980e046SJonathan Cameron static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL); 878a980e046SJonathan Cameron 879a980e046SJonathan Cameron static int iio_device_register_sysfs(struct iio_dev *indio_dev) 880a980e046SJonathan Cameron { 881a980e046SJonathan Cameron int i, ret = 0, attrcount, attrn, attrcount_orig = 0; 88284088ebdSLars-Peter Clausen struct iio_dev_attr *p; 883a980e046SJonathan Cameron struct attribute **attr; 884a980e046SJonathan Cameron 885a980e046SJonathan Cameron /* First count elements in any existing group */ 886a980e046SJonathan Cameron if (indio_dev->info->attrs) { 887a980e046SJonathan Cameron attr = indio_dev->info->attrs->attrs; 888a980e046SJonathan Cameron while (*attr++ != NULL) 889a980e046SJonathan Cameron attrcount_orig++; 890a980e046SJonathan Cameron } 891a980e046SJonathan Cameron attrcount = attrcount_orig; 892a980e046SJonathan Cameron /* 893a980e046SJonathan Cameron * New channel registration method - relies on the fact a group does 894d25b3808SPeter Meerwald * not need to be initialized if its name is NULL. 895a980e046SJonathan Cameron */ 896a980e046SJonathan Cameron if (indio_dev->channels) 897a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) { 898a980e046SJonathan Cameron ret = iio_device_add_channel_sysfs(indio_dev, 899a980e046SJonathan Cameron &indio_dev 900a980e046SJonathan Cameron ->channels[i]); 901a980e046SJonathan Cameron if (ret < 0) 902a980e046SJonathan Cameron goto error_clear_attrs; 903a980e046SJonathan Cameron attrcount += ret; 904a980e046SJonathan Cameron } 905a980e046SJonathan Cameron 906a980e046SJonathan Cameron if (indio_dev->name) 907a980e046SJonathan Cameron attrcount++; 908a980e046SJonathan Cameron 909a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1, 910a980e046SJonathan Cameron sizeof(indio_dev->chan_attr_group.attrs[0]), 911a980e046SJonathan Cameron GFP_KERNEL); 912a980e046SJonathan Cameron if (indio_dev->chan_attr_group.attrs == NULL) { 913a980e046SJonathan Cameron ret = -ENOMEM; 914a980e046SJonathan Cameron goto error_clear_attrs; 915a980e046SJonathan Cameron } 916a980e046SJonathan Cameron /* Copy across original attributes */ 917a980e046SJonathan Cameron if (indio_dev->info->attrs) 918a980e046SJonathan Cameron memcpy(indio_dev->chan_attr_group.attrs, 919a980e046SJonathan Cameron indio_dev->info->attrs->attrs, 920a980e046SJonathan Cameron sizeof(indio_dev->chan_attr_group.attrs[0]) 921a980e046SJonathan Cameron *attrcount_orig); 922a980e046SJonathan Cameron attrn = attrcount_orig; 923a980e046SJonathan Cameron /* Add all elements from the list. */ 924a980e046SJonathan Cameron list_for_each_entry(p, &indio_dev->channel_attr_list, l) 925a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr; 926a980e046SJonathan Cameron if (indio_dev->name) 927a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr; 928a980e046SJonathan Cameron 929a980e046SJonathan Cameron indio_dev->groups[indio_dev->groupcounter++] = 930a980e046SJonathan Cameron &indio_dev->chan_attr_group; 931a980e046SJonathan Cameron 932a980e046SJonathan Cameron return 0; 933a980e046SJonathan Cameron 934a980e046SJonathan Cameron error_clear_attrs: 93584088ebdSLars-Peter Clausen iio_free_chan_devattr_list(&indio_dev->channel_attr_list); 936a980e046SJonathan Cameron 937a980e046SJonathan Cameron return ret; 938a980e046SJonathan Cameron } 939a980e046SJonathan Cameron 940a980e046SJonathan Cameron static void iio_device_unregister_sysfs(struct iio_dev *indio_dev) 941a980e046SJonathan Cameron { 942a980e046SJonathan Cameron 94384088ebdSLars-Peter Clausen iio_free_chan_devattr_list(&indio_dev->channel_attr_list); 944a980e046SJonathan Cameron kfree(indio_dev->chan_attr_group.attrs); 945c1b03ab5SMartin Fuzzey indio_dev->chan_attr_group.attrs = NULL; 946a980e046SJonathan Cameron } 947a980e046SJonathan Cameron 948a980e046SJonathan Cameron static void iio_dev_release(struct device *device) 949a980e046SJonathan Cameron { 950e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(device); 951a980e046SJonathan Cameron if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) 952a980e046SJonathan Cameron iio_device_unregister_trigger_consumer(indio_dev); 953a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 954a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 955e407fd65SLars-Peter Clausen 9569e69c935SLars-Peter Clausen iio_buffer_put(indio_dev->buffer); 9579e69c935SLars-Peter Clausen 958e407fd65SLars-Peter Clausen ida_simple_remove(&iio_ida, indio_dev->id); 959e407fd65SLars-Peter Clausen kfree(indio_dev); 960a980e046SJonathan Cameron } 961a980e046SJonathan Cameron 96217d82b47SGuenter Roeck struct device_type iio_device_type = { 963a980e046SJonathan Cameron .name = "iio_device", 964a980e046SJonathan Cameron .release = iio_dev_release, 965a980e046SJonathan Cameron }; 966a980e046SJonathan Cameron 967a7e57dceSSachin Kamat /** 968a7e57dceSSachin Kamat * iio_device_alloc() - allocate an iio_dev from a driver 969a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 970a7e57dceSSachin Kamat **/ 9717cbb7537SLars-Peter Clausen struct iio_dev *iio_device_alloc(int sizeof_priv) 972a980e046SJonathan Cameron { 973a980e046SJonathan Cameron struct iio_dev *dev; 974a980e046SJonathan Cameron size_t alloc_size; 975a980e046SJonathan Cameron 976a980e046SJonathan Cameron alloc_size = sizeof(struct iio_dev); 977a980e046SJonathan Cameron if (sizeof_priv) { 978a980e046SJonathan Cameron alloc_size = ALIGN(alloc_size, IIO_ALIGN); 979a980e046SJonathan Cameron alloc_size += sizeof_priv; 980a980e046SJonathan Cameron } 981a980e046SJonathan Cameron /* ensure 32-byte alignment of whole construct ? */ 982a980e046SJonathan Cameron alloc_size += IIO_ALIGN - 1; 983a980e046SJonathan Cameron 984a980e046SJonathan Cameron dev = kzalloc(alloc_size, GFP_KERNEL); 985a980e046SJonathan Cameron 986a980e046SJonathan Cameron if (dev) { 987a980e046SJonathan Cameron dev->dev.groups = dev->groups; 98817d82b47SGuenter Roeck dev->dev.type = &iio_device_type; 989a980e046SJonathan Cameron dev->dev.bus = &iio_bus_type; 990a980e046SJonathan Cameron device_initialize(&dev->dev); 991a980e046SJonathan Cameron dev_set_drvdata(&dev->dev, (void *)dev); 992a980e046SJonathan Cameron mutex_init(&dev->mlock); 993a980e046SJonathan Cameron mutex_init(&dev->info_exist_lock); 994e407fd65SLars-Peter Clausen INIT_LIST_HEAD(&dev->channel_attr_list); 995a980e046SJonathan Cameron 996a980e046SJonathan Cameron dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL); 997a980e046SJonathan Cameron if (dev->id < 0) { 998a980e046SJonathan Cameron /* cannot use a dev_err as the name isn't available */ 9993176dd5dSSachin Kamat pr_err("failed to get device id\n"); 1000a980e046SJonathan Cameron kfree(dev); 1001a980e046SJonathan Cameron return NULL; 1002a980e046SJonathan Cameron } 1003a980e046SJonathan Cameron dev_set_name(&dev->dev, "iio:device%d", dev->id); 100484b36ce5SJonathan Cameron INIT_LIST_HEAD(&dev->buffer_list); 1005a980e046SJonathan Cameron } 1006a980e046SJonathan Cameron 1007a980e046SJonathan Cameron return dev; 1008a980e046SJonathan Cameron } 10097cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_alloc); 1010a980e046SJonathan Cameron 1011a7e57dceSSachin Kamat /** 1012a7e57dceSSachin Kamat * iio_device_free() - free an iio_dev from a driver 1013a7e57dceSSachin Kamat * @dev: the iio_dev associated with the device 1014a7e57dceSSachin Kamat **/ 10157cbb7537SLars-Peter Clausen void iio_device_free(struct iio_dev *dev) 1016a980e046SJonathan Cameron { 1017e407fd65SLars-Peter Clausen if (dev) 1018e407fd65SLars-Peter Clausen put_device(&dev->dev); 1019a980e046SJonathan Cameron } 10207cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_free); 1021a980e046SJonathan Cameron 10229dabaf5eSGrygorii Strashko static void devm_iio_device_release(struct device *dev, void *res) 10239dabaf5eSGrygorii Strashko { 10249dabaf5eSGrygorii Strashko iio_device_free(*(struct iio_dev **)res); 10259dabaf5eSGrygorii Strashko } 10269dabaf5eSGrygorii Strashko 10279dabaf5eSGrygorii Strashko static int devm_iio_device_match(struct device *dev, void *res, void *data) 10289dabaf5eSGrygorii Strashko { 10299dabaf5eSGrygorii Strashko struct iio_dev **r = res; 10309dabaf5eSGrygorii Strashko if (!r || !*r) { 10319dabaf5eSGrygorii Strashko WARN_ON(!r || !*r); 10329dabaf5eSGrygorii Strashko return 0; 10339dabaf5eSGrygorii Strashko } 10349dabaf5eSGrygorii Strashko return *r == data; 10359dabaf5eSGrygorii Strashko } 10369dabaf5eSGrygorii Strashko 1037a7e57dceSSachin Kamat /** 1038a7e57dceSSachin Kamat * devm_iio_device_alloc - Resource-managed iio_device_alloc() 1039a7e57dceSSachin Kamat * @dev: Device to allocate iio_dev for 1040a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 1041a7e57dceSSachin Kamat * 1042a7e57dceSSachin Kamat * Managed iio_device_alloc. iio_dev allocated with this function is 1043a7e57dceSSachin Kamat * automatically freed on driver detach. 1044a7e57dceSSachin Kamat * 1045a7e57dceSSachin Kamat * If an iio_dev allocated with this function needs to be freed separately, 1046a7e57dceSSachin Kamat * devm_iio_device_free() must be used. 1047a7e57dceSSachin Kamat * 1048a7e57dceSSachin Kamat * RETURNS: 1049a7e57dceSSachin Kamat * Pointer to allocated iio_dev on success, NULL on failure. 1050a7e57dceSSachin Kamat */ 10519dabaf5eSGrygorii Strashko struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv) 10529dabaf5eSGrygorii Strashko { 10539dabaf5eSGrygorii Strashko struct iio_dev **ptr, *iio_dev; 10549dabaf5eSGrygorii Strashko 10559dabaf5eSGrygorii Strashko ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr), 10569dabaf5eSGrygorii Strashko GFP_KERNEL); 10579dabaf5eSGrygorii Strashko if (!ptr) 10589dabaf5eSGrygorii Strashko return NULL; 10599dabaf5eSGrygorii Strashko 10609dabaf5eSGrygorii Strashko iio_dev = iio_device_alloc(sizeof_priv); 10619dabaf5eSGrygorii Strashko if (iio_dev) { 10629dabaf5eSGrygorii Strashko *ptr = iio_dev; 10639dabaf5eSGrygorii Strashko devres_add(dev, ptr); 10649dabaf5eSGrygorii Strashko } else { 10659dabaf5eSGrygorii Strashko devres_free(ptr); 10669dabaf5eSGrygorii Strashko } 10679dabaf5eSGrygorii Strashko 10689dabaf5eSGrygorii Strashko return iio_dev; 10699dabaf5eSGrygorii Strashko } 10709dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_alloc); 10719dabaf5eSGrygorii Strashko 1072a7e57dceSSachin Kamat /** 1073a7e57dceSSachin Kamat * devm_iio_device_free - Resource-managed iio_device_free() 1074a7e57dceSSachin Kamat * @dev: Device this iio_dev belongs to 1075a7e57dceSSachin Kamat * @iio_dev: the iio_dev associated with the device 1076a7e57dceSSachin Kamat * 1077a7e57dceSSachin Kamat * Free iio_dev allocated with devm_iio_device_alloc(). 1078a7e57dceSSachin Kamat */ 10799dabaf5eSGrygorii Strashko void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev) 10809dabaf5eSGrygorii Strashko { 10819dabaf5eSGrygorii Strashko int rc; 10829dabaf5eSGrygorii Strashko 10839dabaf5eSGrygorii Strashko rc = devres_release(dev, devm_iio_device_release, 10849dabaf5eSGrygorii Strashko devm_iio_device_match, iio_dev); 10859dabaf5eSGrygorii Strashko WARN_ON(rc); 10869dabaf5eSGrygorii Strashko } 10879dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_free); 10889dabaf5eSGrygorii Strashko 1089a980e046SJonathan Cameron /** 1090a980e046SJonathan Cameron * iio_chrdev_open() - chrdev file open for buffer access and ioctls 1091a980e046SJonathan Cameron **/ 1092a980e046SJonathan Cameron static int iio_chrdev_open(struct inode *inode, struct file *filp) 1093a980e046SJonathan Cameron { 1094a980e046SJonathan Cameron struct iio_dev *indio_dev = container_of(inode->i_cdev, 1095a980e046SJonathan Cameron struct iio_dev, chrdev); 1096a980e046SJonathan Cameron 1097a980e046SJonathan Cameron if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags)) 1098a980e046SJonathan Cameron return -EBUSY; 1099a980e046SJonathan Cameron 1100cadc2125SLars-Peter Clausen iio_device_get(indio_dev); 1101cadc2125SLars-Peter Clausen 1102a980e046SJonathan Cameron filp->private_data = indio_dev; 1103a980e046SJonathan Cameron 1104a980e046SJonathan Cameron return 0; 1105a980e046SJonathan Cameron } 1106a980e046SJonathan Cameron 1107a980e046SJonathan Cameron /** 1108a980e046SJonathan Cameron * iio_chrdev_release() - chrdev file close buffer access and ioctls 1109a980e046SJonathan Cameron **/ 1110a980e046SJonathan Cameron static int iio_chrdev_release(struct inode *inode, struct file *filp) 1111a980e046SJonathan Cameron { 1112a980e046SJonathan Cameron struct iio_dev *indio_dev = container_of(inode->i_cdev, 1113a980e046SJonathan Cameron struct iio_dev, chrdev); 1114a980e046SJonathan Cameron clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags); 1115cadc2125SLars-Peter Clausen iio_device_put(indio_dev); 1116cadc2125SLars-Peter Clausen 1117a980e046SJonathan Cameron return 0; 1118a980e046SJonathan Cameron } 1119a980e046SJonathan Cameron 1120a980e046SJonathan Cameron /* Somewhat of a cross file organization violation - ioctls here are actually 1121a980e046SJonathan Cameron * event related */ 1122a980e046SJonathan Cameron static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 1123a980e046SJonathan Cameron { 1124a980e046SJonathan Cameron struct iio_dev *indio_dev = filp->private_data; 1125a980e046SJonathan Cameron int __user *ip = (int __user *)arg; 1126a980e046SJonathan Cameron int fd; 1127a980e046SJonathan Cameron 1128f18e7a06SLars-Peter Clausen if (!indio_dev->info) 1129f18e7a06SLars-Peter Clausen return -ENODEV; 1130f18e7a06SLars-Peter Clausen 1131a980e046SJonathan Cameron if (cmd == IIO_GET_EVENT_FD_IOCTL) { 1132a980e046SJonathan Cameron fd = iio_event_getfd(indio_dev); 1133a980e046SJonathan Cameron if (copy_to_user(ip, &fd, sizeof(fd))) 1134a980e046SJonathan Cameron return -EFAULT; 1135a980e046SJonathan Cameron return 0; 1136a980e046SJonathan Cameron } 1137a980e046SJonathan Cameron return -EINVAL; 1138a980e046SJonathan Cameron } 1139a980e046SJonathan Cameron 1140a980e046SJonathan Cameron static const struct file_operations iio_buffer_fileops = { 1141a980e046SJonathan Cameron .read = iio_buffer_read_first_n_outer_addr, 1142a980e046SJonathan Cameron .release = iio_chrdev_release, 1143a980e046SJonathan Cameron .open = iio_chrdev_open, 1144a980e046SJonathan Cameron .poll = iio_buffer_poll_addr, 1145a980e046SJonathan Cameron .owner = THIS_MODULE, 1146a980e046SJonathan Cameron .llseek = noop_llseek, 1147a980e046SJonathan Cameron .unlocked_ioctl = iio_ioctl, 1148a980e046SJonathan Cameron .compat_ioctl = iio_ioctl, 1149a980e046SJonathan Cameron }; 1150a980e046SJonathan Cameron 11518f5d8727SVlad Dogaru static int iio_check_unique_scan_index(struct iio_dev *indio_dev) 11528f5d8727SVlad Dogaru { 11538f5d8727SVlad Dogaru int i, j; 11548f5d8727SVlad Dogaru const struct iio_chan_spec *channels = indio_dev->channels; 11558f5d8727SVlad Dogaru 11568f5d8727SVlad Dogaru if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES)) 11578f5d8727SVlad Dogaru return 0; 11588f5d8727SVlad Dogaru 11598f5d8727SVlad Dogaru for (i = 0; i < indio_dev->num_channels - 1; i++) { 11608f5d8727SVlad Dogaru if (channels[i].scan_index < 0) 11618f5d8727SVlad Dogaru continue; 11628f5d8727SVlad Dogaru for (j = i + 1; j < indio_dev->num_channels; j++) 11638f5d8727SVlad Dogaru if (channels[i].scan_index == channels[j].scan_index) { 11648f5d8727SVlad Dogaru dev_err(&indio_dev->dev, 11658f5d8727SVlad Dogaru "Duplicate scan index %d\n", 11668f5d8727SVlad Dogaru channels[i].scan_index); 11678f5d8727SVlad Dogaru return -EINVAL; 11688f5d8727SVlad Dogaru } 11698f5d8727SVlad Dogaru } 11708f5d8727SVlad Dogaru 11718f5d8727SVlad Dogaru return 0; 11728f5d8727SVlad Dogaru } 11738f5d8727SVlad Dogaru 1174a980e046SJonathan Cameron static const struct iio_buffer_setup_ops noop_ring_setup_ops; 1175a980e046SJonathan Cameron 1176a7e57dceSSachin Kamat /** 1177a7e57dceSSachin Kamat * iio_device_register() - register a device with the IIO subsystem 1178a7e57dceSSachin Kamat * @indio_dev: Device structure filled by the device driver 1179a7e57dceSSachin Kamat **/ 1180a980e046SJonathan Cameron int iio_device_register(struct iio_dev *indio_dev) 1181a980e046SJonathan Cameron { 1182a980e046SJonathan Cameron int ret; 1183a980e046SJonathan Cameron 118417d82b47SGuenter Roeck /* If the calling driver did not initialize of_node, do it here */ 118517d82b47SGuenter Roeck if (!indio_dev->dev.of_node && indio_dev->dev.parent) 118617d82b47SGuenter Roeck indio_dev->dev.of_node = indio_dev->dev.parent->of_node; 118717d82b47SGuenter Roeck 11888f5d8727SVlad Dogaru ret = iio_check_unique_scan_index(indio_dev); 11898f5d8727SVlad Dogaru if (ret < 0) 11908f5d8727SVlad Dogaru return ret; 11918f5d8727SVlad Dogaru 1192a980e046SJonathan Cameron /* configure elements for the chrdev */ 1193a980e046SJonathan Cameron indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id); 1194a980e046SJonathan Cameron 1195a980e046SJonathan Cameron ret = iio_device_register_debugfs(indio_dev); 1196a980e046SJonathan Cameron if (ret) { 1197a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1198a980e046SJonathan Cameron "Failed to register debugfs interfaces\n"); 119992825ff9SHartmut Knaack return ret; 1200a980e046SJonathan Cameron } 12013e1b6c95SLars-Peter Clausen 12023e1b6c95SLars-Peter Clausen ret = iio_buffer_alloc_sysfs_and_mask(indio_dev); 12033e1b6c95SLars-Peter Clausen if (ret) { 12043e1b6c95SLars-Peter Clausen dev_err(indio_dev->dev.parent, 12053e1b6c95SLars-Peter Clausen "Failed to create buffer sysfs interfaces\n"); 12063e1b6c95SLars-Peter Clausen goto error_unreg_debugfs; 12073e1b6c95SLars-Peter Clausen } 12083e1b6c95SLars-Peter Clausen 1209a980e046SJonathan Cameron ret = iio_device_register_sysfs(indio_dev); 1210a980e046SJonathan Cameron if (ret) { 1211a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1212a980e046SJonathan Cameron "Failed to register sysfs interfaces\n"); 12133e1b6c95SLars-Peter Clausen goto error_buffer_free_sysfs; 1214a980e046SJonathan Cameron } 1215a980e046SJonathan Cameron ret = iio_device_register_eventset(indio_dev); 1216a980e046SJonathan Cameron if (ret) { 1217a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1218a980e046SJonathan Cameron "Failed to register event set\n"); 1219a980e046SJonathan Cameron goto error_free_sysfs; 1220a980e046SJonathan Cameron } 1221a980e046SJonathan Cameron if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) 1222a980e046SJonathan Cameron iio_device_register_trigger_consumer(indio_dev); 1223a980e046SJonathan Cameron 1224a980e046SJonathan Cameron if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) && 1225a980e046SJonathan Cameron indio_dev->setup_ops == NULL) 1226a980e046SJonathan Cameron indio_dev->setup_ops = &noop_ring_setup_ops; 1227a980e046SJonathan Cameron 1228a980e046SJonathan Cameron cdev_init(&indio_dev->chrdev, &iio_buffer_fileops); 1229a980e046SJonathan Cameron indio_dev->chrdev.owner = indio_dev->info->driver_module; 12300d5b7daeSLars-Peter Clausen indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj; 1231a980e046SJonathan Cameron ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1); 1232a980e046SJonathan Cameron if (ret < 0) 12330d5b7daeSLars-Peter Clausen goto error_unreg_eventset; 1234a980e046SJonathan Cameron 12350d5b7daeSLars-Peter Clausen ret = device_add(&indio_dev->dev); 12360d5b7daeSLars-Peter Clausen if (ret < 0) 12370d5b7daeSLars-Peter Clausen goto error_cdev_del; 12380d5b7daeSLars-Peter Clausen 12390d5b7daeSLars-Peter Clausen return 0; 12400d5b7daeSLars-Peter Clausen error_cdev_del: 12410d5b7daeSLars-Peter Clausen cdev_del(&indio_dev->chrdev); 1242a980e046SJonathan Cameron error_unreg_eventset: 1243a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 1244a980e046SJonathan Cameron error_free_sysfs: 1245a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 12463e1b6c95SLars-Peter Clausen error_buffer_free_sysfs: 12473e1b6c95SLars-Peter Clausen iio_buffer_free_sysfs_and_mask(indio_dev); 1248a980e046SJonathan Cameron error_unreg_debugfs: 1249a980e046SJonathan Cameron iio_device_unregister_debugfs(indio_dev); 1250a980e046SJonathan Cameron return ret; 1251a980e046SJonathan Cameron } 1252a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_register); 1253a980e046SJonathan Cameron 1254a7e57dceSSachin Kamat /** 1255a7e57dceSSachin Kamat * iio_device_unregister() - unregister a device from the IIO subsystem 1256a7e57dceSSachin Kamat * @indio_dev: Device structure representing the device. 1257a7e57dceSSachin Kamat **/ 1258a980e046SJonathan Cameron void iio_device_unregister(struct iio_dev *indio_dev) 1259a980e046SJonathan Cameron { 1260a980e046SJonathan Cameron mutex_lock(&indio_dev->info_exist_lock); 1261a87c82e4SLars-Peter Clausen 1262a87c82e4SLars-Peter Clausen device_del(&indio_dev->dev); 1263a87c82e4SLars-Peter Clausen 12640d5b7daeSLars-Peter Clausen if (indio_dev->chrdev.dev) 12650d5b7daeSLars-Peter Clausen cdev_del(&indio_dev->chrdev); 1266bc4c9612SLars-Peter Clausen iio_device_unregister_debugfs(indio_dev); 12670d5b7daeSLars-Peter Clausen 1268a87c82e4SLars-Peter Clausen iio_disable_all_buffers(indio_dev); 1269a87c82e4SLars-Peter Clausen 1270a980e046SJonathan Cameron indio_dev->info = NULL; 1271d2f0a48fSLars-Peter Clausen 1272d2f0a48fSLars-Peter Clausen iio_device_wakeup_eventset(indio_dev); 1273d2f0a48fSLars-Peter Clausen iio_buffer_wakeup_poll(indio_dev); 1274d2f0a48fSLars-Peter Clausen 1275a980e046SJonathan Cameron mutex_unlock(&indio_dev->info_exist_lock); 12763e1b6c95SLars-Peter Clausen 12773e1b6c95SLars-Peter Clausen iio_buffer_free_sysfs_and_mask(indio_dev); 1278a980e046SJonathan Cameron } 1279a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_unregister); 12808caa07c0SSachin Kamat 12818caa07c0SSachin Kamat static void devm_iio_device_unreg(struct device *dev, void *res) 12828caa07c0SSachin Kamat { 12838caa07c0SSachin Kamat iio_device_unregister(*(struct iio_dev **)res); 12848caa07c0SSachin Kamat } 12858caa07c0SSachin Kamat 12868caa07c0SSachin Kamat /** 12878caa07c0SSachin Kamat * devm_iio_device_register - Resource-managed iio_device_register() 12888caa07c0SSachin Kamat * @dev: Device to allocate iio_dev for 12898caa07c0SSachin Kamat * @indio_dev: Device structure filled by the device driver 12908caa07c0SSachin Kamat * 12918caa07c0SSachin Kamat * Managed iio_device_register. The IIO device registered with this 12928caa07c0SSachin Kamat * function is automatically unregistered on driver detach. This function 12938caa07c0SSachin Kamat * calls iio_device_register() internally. Refer to that function for more 12948caa07c0SSachin Kamat * information. 12958caa07c0SSachin Kamat * 12968caa07c0SSachin Kamat * If an iio_dev registered with this function needs to be unregistered 12978caa07c0SSachin Kamat * separately, devm_iio_device_unregister() must be used. 12988caa07c0SSachin Kamat * 12998caa07c0SSachin Kamat * RETURNS: 13008caa07c0SSachin Kamat * 0 on success, negative error number on failure. 13018caa07c0SSachin Kamat */ 13028caa07c0SSachin Kamat int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev) 13038caa07c0SSachin Kamat { 13048caa07c0SSachin Kamat struct iio_dev **ptr; 13058caa07c0SSachin Kamat int ret; 13068caa07c0SSachin Kamat 13078caa07c0SSachin Kamat ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL); 13088caa07c0SSachin Kamat if (!ptr) 13098caa07c0SSachin Kamat return -ENOMEM; 13108caa07c0SSachin Kamat 13118caa07c0SSachin Kamat *ptr = indio_dev; 13128caa07c0SSachin Kamat ret = iio_device_register(indio_dev); 13138caa07c0SSachin Kamat if (!ret) 13148caa07c0SSachin Kamat devres_add(dev, ptr); 13158caa07c0SSachin Kamat else 13168caa07c0SSachin Kamat devres_free(ptr); 13178caa07c0SSachin Kamat 13188caa07c0SSachin Kamat return ret; 13198caa07c0SSachin Kamat } 13208caa07c0SSachin Kamat EXPORT_SYMBOL_GPL(devm_iio_device_register); 13218caa07c0SSachin Kamat 13228caa07c0SSachin Kamat /** 13238caa07c0SSachin Kamat * devm_iio_device_unregister - Resource-managed iio_device_unregister() 13248caa07c0SSachin Kamat * @dev: Device this iio_dev belongs to 13258caa07c0SSachin Kamat * @indio_dev: the iio_dev associated with the device 13268caa07c0SSachin Kamat * 13278caa07c0SSachin Kamat * Unregister iio_dev registered with devm_iio_device_register(). 13288caa07c0SSachin Kamat */ 13298caa07c0SSachin Kamat void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev) 13308caa07c0SSachin Kamat { 13318caa07c0SSachin Kamat int rc; 13328caa07c0SSachin Kamat 13338caa07c0SSachin Kamat rc = devres_release(dev, devm_iio_device_unreg, 13348caa07c0SSachin Kamat devm_iio_device_match, indio_dev); 13358caa07c0SSachin Kamat WARN_ON(rc); 13368caa07c0SSachin Kamat } 13378caa07c0SSachin Kamat EXPORT_SYMBOL_GPL(devm_iio_device_unregister); 13388caa07c0SSachin Kamat 1339a980e046SJonathan Cameron subsys_initcall(iio_init); 1340a980e046SJonathan Cameron module_exit(iio_exit); 1341a980e046SJonathan Cameron 1342c8b95952SJonathan Cameron MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); 1343a980e046SJonathan Cameron MODULE_DESCRIPTION("Industrial I/O core"); 1344a980e046SJonathan Cameron MODULE_LICENSE("GPL"); 1345