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", 120a980e046SJonathan Cameron [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency", 121a6b12855SMichael Hennerich [IIO_CHAN_INFO_FREQUENCY] = "frequency", 122a6b12855SMichael Hennerich [IIO_CHAN_INFO_PHASE] = "phase", 123b65d6212SMichael Hennerich [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain", 1247c9ab035Ssrinivas pandruvada [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis", 125899d90bdSPeter Meerwald [IIO_CHAN_INFO_INT_TIME] = "integration_time", 126a88bfe78SIrina Tirdea [IIO_CHAN_INFO_ENABLE] = "en", 127bcdf28fbSIrina Tirdea [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight", 128d37f6836SIrina Tirdea [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight", 1292f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count", 1302f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time", 131c8a85854SVianney le Clément de Saint-Marcq [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity", 132*faaa4495SIrina Tirdea [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio", 133a980e046SJonathan Cameron }; 134a980e046SJonathan Cameron 135a7e57dceSSachin Kamat /** 136a7e57dceSSachin Kamat * iio_find_channel_from_si() - get channel from its scan index 137a7e57dceSSachin Kamat * @indio_dev: device 138a7e57dceSSachin Kamat * @si: scan index to match 139a7e57dceSSachin Kamat */ 140a980e046SJonathan Cameron const struct iio_chan_spec 141a980e046SJonathan Cameron *iio_find_channel_from_si(struct iio_dev *indio_dev, int si) 142a980e046SJonathan Cameron { 143a980e046SJonathan Cameron int i; 144a980e046SJonathan Cameron 145a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) 146a980e046SJonathan Cameron if (indio_dev->channels[i].scan_index == si) 147a980e046SJonathan Cameron return &indio_dev->channels[i]; 148a980e046SJonathan Cameron return NULL; 149a980e046SJonathan Cameron } 150a980e046SJonathan Cameron 151a980e046SJonathan Cameron /* This turns up an awful lot */ 152a980e046SJonathan Cameron ssize_t iio_read_const_attr(struct device *dev, 153a980e046SJonathan Cameron struct device_attribute *attr, 154a980e046SJonathan Cameron char *buf) 155a980e046SJonathan Cameron { 156a980e046SJonathan Cameron return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string); 157a980e046SJonathan Cameron } 158a980e046SJonathan Cameron EXPORT_SYMBOL(iio_read_const_attr); 159a980e046SJonathan Cameron 160a980e046SJonathan Cameron static int __init iio_init(void) 161a980e046SJonathan Cameron { 162a980e046SJonathan Cameron int ret; 163a980e046SJonathan Cameron 164a980e046SJonathan Cameron /* Register sysfs bus */ 165a980e046SJonathan Cameron ret = bus_register(&iio_bus_type); 166a980e046SJonathan Cameron if (ret < 0) { 1673176dd5dSSachin Kamat pr_err("could not register bus type\n"); 168a980e046SJonathan Cameron goto error_nothing; 169a980e046SJonathan Cameron } 170a980e046SJonathan Cameron 171a980e046SJonathan Cameron ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio"); 172a980e046SJonathan Cameron if (ret < 0) { 1733176dd5dSSachin Kamat pr_err("failed to allocate char dev region\n"); 174a980e046SJonathan Cameron goto error_unregister_bus_type; 175a980e046SJonathan Cameron } 176a980e046SJonathan Cameron 177a980e046SJonathan Cameron iio_debugfs_dentry = debugfs_create_dir("iio", NULL); 178a980e046SJonathan Cameron 179a980e046SJonathan Cameron return 0; 180a980e046SJonathan Cameron 181a980e046SJonathan Cameron error_unregister_bus_type: 182a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 183a980e046SJonathan Cameron error_nothing: 184a980e046SJonathan Cameron return ret; 185a980e046SJonathan Cameron } 186a980e046SJonathan Cameron 187a980e046SJonathan Cameron static void __exit iio_exit(void) 188a980e046SJonathan Cameron { 189a980e046SJonathan Cameron if (iio_devt) 190a980e046SJonathan Cameron unregister_chrdev_region(iio_devt, IIO_DEV_MAX); 191a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 192a980e046SJonathan Cameron debugfs_remove(iio_debugfs_dentry); 193a980e046SJonathan Cameron } 194a980e046SJonathan Cameron 195a980e046SJonathan Cameron #if defined(CONFIG_DEBUG_FS) 196a980e046SJonathan Cameron static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf, 197a980e046SJonathan Cameron size_t count, loff_t *ppos) 198a980e046SJonathan Cameron { 199a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 200a980e046SJonathan Cameron char buf[20]; 201a980e046SJonathan Cameron unsigned val = 0; 202a980e046SJonathan Cameron ssize_t len; 203a980e046SJonathan Cameron int ret; 204a980e046SJonathan Cameron 205a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, 206a980e046SJonathan Cameron indio_dev->cached_reg_addr, 207a980e046SJonathan Cameron 0, &val); 208a980e046SJonathan Cameron if (ret) 209a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__); 210a980e046SJonathan Cameron 211a980e046SJonathan Cameron len = snprintf(buf, sizeof(buf), "0x%X\n", val); 212a980e046SJonathan Cameron 213a980e046SJonathan Cameron return simple_read_from_buffer(userbuf, count, ppos, buf, len); 214a980e046SJonathan Cameron } 215a980e046SJonathan Cameron 216a980e046SJonathan Cameron static ssize_t iio_debugfs_write_reg(struct file *file, 217a980e046SJonathan Cameron const char __user *userbuf, size_t count, loff_t *ppos) 218a980e046SJonathan Cameron { 219a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 220a980e046SJonathan Cameron unsigned reg, val; 221a980e046SJonathan Cameron char buf[80]; 222a980e046SJonathan Cameron int ret; 223a980e046SJonathan Cameron 224a980e046SJonathan Cameron count = min_t(size_t, count, (sizeof(buf)-1)); 225a980e046SJonathan Cameron if (copy_from_user(buf, userbuf, count)) 226a980e046SJonathan Cameron return -EFAULT; 227a980e046SJonathan Cameron 228a980e046SJonathan Cameron buf[count] = 0; 229a980e046SJonathan Cameron 230a980e046SJonathan Cameron ret = sscanf(buf, "%i %i", ®, &val); 231a980e046SJonathan Cameron 232a980e046SJonathan Cameron switch (ret) { 233a980e046SJonathan Cameron case 1: 234a980e046SJonathan Cameron indio_dev->cached_reg_addr = reg; 235a980e046SJonathan Cameron break; 236a980e046SJonathan Cameron case 2: 237a980e046SJonathan Cameron indio_dev->cached_reg_addr = reg; 238a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, reg, 239a980e046SJonathan Cameron val, NULL); 240a980e046SJonathan Cameron if (ret) { 241a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: write failed\n", 242a980e046SJonathan Cameron __func__); 243a980e046SJonathan Cameron return ret; 244a980e046SJonathan Cameron } 245a980e046SJonathan Cameron break; 246a980e046SJonathan Cameron default: 247a980e046SJonathan Cameron return -EINVAL; 248a980e046SJonathan Cameron } 249a980e046SJonathan Cameron 250a980e046SJonathan Cameron return count; 251a980e046SJonathan Cameron } 252a980e046SJonathan Cameron 253a980e046SJonathan Cameron static const struct file_operations iio_debugfs_reg_fops = { 2545a28c873SAxel Lin .open = simple_open, 255a980e046SJonathan Cameron .read = iio_debugfs_read_reg, 256a980e046SJonathan Cameron .write = iio_debugfs_write_reg, 257a980e046SJonathan Cameron }; 258a980e046SJonathan Cameron 259a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 260a980e046SJonathan Cameron { 261a980e046SJonathan Cameron debugfs_remove_recursive(indio_dev->debugfs_dentry); 262a980e046SJonathan Cameron } 263a980e046SJonathan Cameron 264a980e046SJonathan Cameron static int iio_device_register_debugfs(struct iio_dev *indio_dev) 265a980e046SJonathan Cameron { 266a980e046SJonathan Cameron struct dentry *d; 267a980e046SJonathan Cameron 268a980e046SJonathan Cameron if (indio_dev->info->debugfs_reg_access == NULL) 269a980e046SJonathan Cameron return 0; 270a980e046SJonathan Cameron 271abd5a2fbSAxel Lin if (!iio_debugfs_dentry) 272a980e046SJonathan Cameron return 0; 273a980e046SJonathan Cameron 274a980e046SJonathan Cameron indio_dev->debugfs_dentry = 275a980e046SJonathan Cameron debugfs_create_dir(dev_name(&indio_dev->dev), 276a980e046SJonathan Cameron iio_debugfs_dentry); 277a980e046SJonathan Cameron if (indio_dev->debugfs_dentry == NULL) { 278a980e046SJonathan Cameron dev_warn(indio_dev->dev.parent, 279a980e046SJonathan Cameron "Failed to create debugfs directory\n"); 280a980e046SJonathan Cameron return -EFAULT; 281a980e046SJonathan Cameron } 282a980e046SJonathan Cameron 283a980e046SJonathan Cameron d = debugfs_create_file("direct_reg_access", 0644, 284a980e046SJonathan Cameron indio_dev->debugfs_dentry, 285a980e046SJonathan Cameron indio_dev, &iio_debugfs_reg_fops); 286a980e046SJonathan Cameron if (!d) { 287a980e046SJonathan Cameron iio_device_unregister_debugfs(indio_dev); 288a980e046SJonathan Cameron return -ENOMEM; 289a980e046SJonathan Cameron } 290a980e046SJonathan Cameron 291a980e046SJonathan Cameron return 0; 292a980e046SJonathan Cameron } 293a980e046SJonathan Cameron #else 294a980e046SJonathan Cameron static int iio_device_register_debugfs(struct iio_dev *indio_dev) 295a980e046SJonathan Cameron { 296a980e046SJonathan Cameron return 0; 297a980e046SJonathan Cameron } 298a980e046SJonathan Cameron 299a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 300a980e046SJonathan Cameron { 301a980e046SJonathan Cameron } 302a980e046SJonathan Cameron #endif /* CONFIG_DEBUG_FS */ 303a980e046SJonathan Cameron 304a980e046SJonathan Cameron static ssize_t iio_read_channel_ext_info(struct device *dev, 305a980e046SJonathan Cameron struct device_attribute *attr, 306a980e046SJonathan Cameron char *buf) 307a980e046SJonathan Cameron { 308e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 309a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 310a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 311a980e046SJonathan Cameron 312a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 313a980e046SJonathan Cameron 314fc6d1139SMichael Hennerich return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf); 315a980e046SJonathan Cameron } 316a980e046SJonathan Cameron 317a980e046SJonathan Cameron static ssize_t iio_write_channel_ext_info(struct device *dev, 318a980e046SJonathan Cameron struct device_attribute *attr, 319a980e046SJonathan Cameron const char *buf, 320a980e046SJonathan Cameron size_t len) 321a980e046SJonathan Cameron { 322e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 323a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 324a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 325a980e046SJonathan Cameron 326a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 327a980e046SJonathan Cameron 328fc6d1139SMichael Hennerich return ext_info->write(indio_dev, ext_info->private, 329fc6d1139SMichael Hennerich this_attr->c, buf, len); 330a980e046SJonathan Cameron } 331a980e046SJonathan Cameron 3325212cc8aSLars-Peter Clausen ssize_t iio_enum_available_read(struct iio_dev *indio_dev, 3335212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 3345212cc8aSLars-Peter Clausen { 3355212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3365212cc8aSLars-Peter Clausen unsigned int i; 3375212cc8aSLars-Peter Clausen size_t len = 0; 3385212cc8aSLars-Peter Clausen 3395212cc8aSLars-Peter Clausen if (!e->num_items) 3405212cc8aSLars-Peter Clausen return 0; 3415212cc8aSLars-Peter Clausen 3425212cc8aSLars-Peter Clausen for (i = 0; i < e->num_items; ++i) 34374dcd439SLars-Peter Clausen len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]); 3445212cc8aSLars-Peter Clausen 3455212cc8aSLars-Peter Clausen /* replace last space with a newline */ 3465212cc8aSLars-Peter Clausen buf[len - 1] = '\n'; 3475212cc8aSLars-Peter Clausen 3485212cc8aSLars-Peter Clausen return len; 3495212cc8aSLars-Peter Clausen } 3505212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_available_read); 3515212cc8aSLars-Peter Clausen 3525212cc8aSLars-Peter Clausen ssize_t iio_enum_read(struct iio_dev *indio_dev, 3535212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 3545212cc8aSLars-Peter Clausen { 3555212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3565212cc8aSLars-Peter Clausen int i; 3575212cc8aSLars-Peter Clausen 3585212cc8aSLars-Peter Clausen if (!e->get) 3595212cc8aSLars-Peter Clausen return -EINVAL; 3605212cc8aSLars-Peter Clausen 3615212cc8aSLars-Peter Clausen i = e->get(indio_dev, chan); 3625212cc8aSLars-Peter Clausen if (i < 0) 3635212cc8aSLars-Peter Clausen return i; 3645212cc8aSLars-Peter Clausen else if (i >= e->num_items) 3655212cc8aSLars-Peter Clausen return -EINVAL; 3665212cc8aSLars-Peter Clausen 367598db581SKees Cook return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]); 3685212cc8aSLars-Peter Clausen } 3695212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_read); 3705212cc8aSLars-Peter Clausen 3715212cc8aSLars-Peter Clausen ssize_t iio_enum_write(struct iio_dev *indio_dev, 3725212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, const char *buf, 3735212cc8aSLars-Peter Clausen size_t len) 3745212cc8aSLars-Peter Clausen { 3755212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3765212cc8aSLars-Peter Clausen unsigned int i; 3775212cc8aSLars-Peter Clausen int ret; 3785212cc8aSLars-Peter Clausen 3795212cc8aSLars-Peter Clausen if (!e->set) 3805212cc8aSLars-Peter Clausen return -EINVAL; 3815212cc8aSLars-Peter Clausen 3825212cc8aSLars-Peter Clausen for (i = 0; i < e->num_items; i++) { 3835212cc8aSLars-Peter Clausen if (sysfs_streq(buf, e->items[i])) 3845212cc8aSLars-Peter Clausen break; 3855212cc8aSLars-Peter Clausen } 3865212cc8aSLars-Peter Clausen 3875212cc8aSLars-Peter Clausen if (i == e->num_items) 3885212cc8aSLars-Peter Clausen return -EINVAL; 3895212cc8aSLars-Peter Clausen 3905212cc8aSLars-Peter Clausen ret = e->set(indio_dev, chan, i); 3915212cc8aSLars-Peter Clausen return ret ? ret : len; 3925212cc8aSLars-Peter Clausen } 3935212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_write); 3945212cc8aSLars-Peter Clausen 3953661f3f5SLars-Peter Clausen /** 3963661f3f5SLars-Peter Clausen * iio_format_value() - Formats a IIO value into its string representation 3973661f3f5SLars-Peter Clausen * @buf: The buffer to which the formated value gets written 3983661f3f5SLars-Peter Clausen * @type: One of the IIO_VAL_... constants. This decides how the val and val2 3993661f3f5SLars-Peter Clausen * parameters are formatted. 4009fbfb4b3SSrinivas Pandruvada * @vals: pointer to the values, exact meaning depends on the type parameter. 4013661f3f5SLars-Peter Clausen */ 4029fbfb4b3SSrinivas Pandruvada ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals) 403a980e046SJonathan Cameron { 4047985e7c1SLars-Peter Clausen unsigned long long tmp; 40567eedba3SMichael Hennerich bool scale_db = false; 406a980e046SJonathan Cameron 4073661f3f5SLars-Peter Clausen switch (type) { 40867eedba3SMichael Hennerich case IIO_VAL_INT: 4099fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d\n", vals[0]); 41067eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_MICRO_DB: 41167eedba3SMichael Hennerich scale_db = true; 41267eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_MICRO: 4139fbfb4b3SSrinivas Pandruvada if (vals[1] < 0) 4149fbfb4b3SSrinivas Pandruvada return sprintf(buf, "-%ld.%06u%s\n", abs(vals[0]), 4159fbfb4b3SSrinivas Pandruvada -vals[1], 41667eedba3SMichael Hennerich scale_db ? " dB" : ""); 417a980e046SJonathan Cameron else 4189fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1], 41967eedba3SMichael Hennerich scale_db ? " dB" : ""); 42067eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_NANO: 4219fbfb4b3SSrinivas Pandruvada if (vals[1] < 0) 4229fbfb4b3SSrinivas Pandruvada return sprintf(buf, "-%ld.%09u\n", abs(vals[0]), 4239fbfb4b3SSrinivas Pandruvada -vals[1]); 424a980e046SJonathan Cameron else 4259fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 4267985e7c1SLars-Peter Clausen case IIO_VAL_FRACTIONAL: 4279fbfb4b3SSrinivas Pandruvada tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]); 4289fbfb4b3SSrinivas Pandruvada vals[1] = do_div(tmp, 1000000000LL); 4299fbfb4b3SSrinivas Pandruvada vals[0] = tmp; 4309fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 431103d9fb9SLars-Peter Clausen case IIO_VAL_FRACTIONAL_LOG2: 4329fbfb4b3SSrinivas Pandruvada tmp = (s64)vals[0] * 1000000000LL >> vals[1]; 4339fbfb4b3SSrinivas Pandruvada vals[1] = do_div(tmp, 1000000000LL); 4349fbfb4b3SSrinivas Pandruvada vals[0] = tmp; 4359fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 4369fbfb4b3SSrinivas Pandruvada case IIO_VAL_INT_MULTIPLE: 4379fbfb4b3SSrinivas Pandruvada { 4389fbfb4b3SSrinivas Pandruvada int i; 4399fbfb4b3SSrinivas Pandruvada int len = 0; 4409fbfb4b3SSrinivas Pandruvada 4419fbfb4b3SSrinivas Pandruvada for (i = 0; i < size; ++i) 4429fbfb4b3SSrinivas Pandruvada len += snprintf(&buf[len], PAGE_SIZE - len, "%d ", 4439fbfb4b3SSrinivas Pandruvada vals[i]); 4449fbfb4b3SSrinivas Pandruvada len += snprintf(&buf[len], PAGE_SIZE - len, "\n"); 4459fbfb4b3SSrinivas Pandruvada return len; 4469fbfb4b3SSrinivas Pandruvada } 44767eedba3SMichael Hennerich default: 448a980e046SJonathan Cameron return 0; 449a980e046SJonathan Cameron } 45067eedba3SMichael Hennerich } 451a980e046SJonathan Cameron 4523661f3f5SLars-Peter Clausen static ssize_t iio_read_channel_info(struct device *dev, 4533661f3f5SLars-Peter Clausen struct device_attribute *attr, 4543661f3f5SLars-Peter Clausen char *buf) 4553661f3f5SLars-Peter Clausen { 4563661f3f5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 4573661f3f5SLars-Peter Clausen struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 4589fbfb4b3SSrinivas Pandruvada int vals[INDIO_MAX_RAW_ELEMENTS]; 4599fbfb4b3SSrinivas Pandruvada int ret; 4609fbfb4b3SSrinivas Pandruvada int val_len = 2; 4619fbfb4b3SSrinivas Pandruvada 4629fbfb4b3SSrinivas Pandruvada if (indio_dev->info->read_raw_multi) 4639fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c, 4649fbfb4b3SSrinivas Pandruvada INDIO_MAX_RAW_ELEMENTS, 4659fbfb4b3SSrinivas Pandruvada vals, &val_len, 4669fbfb4b3SSrinivas Pandruvada this_attr->address); 4679fbfb4b3SSrinivas Pandruvada else 4689fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw(indio_dev, this_attr->c, 4699fbfb4b3SSrinivas Pandruvada &vals[0], &vals[1], this_attr->address); 4703661f3f5SLars-Peter Clausen 4713661f3f5SLars-Peter Clausen if (ret < 0) 4723661f3f5SLars-Peter Clausen return ret; 4733661f3f5SLars-Peter Clausen 4749fbfb4b3SSrinivas Pandruvada return iio_format_value(buf, ret, val_len, vals); 4753661f3f5SLars-Peter Clausen } 4763661f3f5SLars-Peter Clausen 4776807d721SLars-Peter Clausen /** 4786807d721SLars-Peter Clausen * iio_str_to_fixpoint() - Parse a fixed-point number from a string 4796807d721SLars-Peter Clausen * @str: The string to parse 4806807d721SLars-Peter Clausen * @fract_mult: Multiplier for the first decimal place, should be a power of 10 4816807d721SLars-Peter Clausen * @integer: The integer part of the number 4826807d721SLars-Peter Clausen * @fract: The fractional part of the number 4836807d721SLars-Peter Clausen * 4846807d721SLars-Peter Clausen * Returns 0 on success, or a negative error code if the string could not be 4856807d721SLars-Peter Clausen * parsed. 4866807d721SLars-Peter Clausen */ 4876807d721SLars-Peter Clausen int iio_str_to_fixpoint(const char *str, int fract_mult, 4886807d721SLars-Peter Clausen int *integer, int *fract) 4896807d721SLars-Peter Clausen { 4906807d721SLars-Peter Clausen int i = 0, f = 0; 4916807d721SLars-Peter Clausen bool integer_part = true, negative = false; 4926807d721SLars-Peter Clausen 4936807d721SLars-Peter Clausen if (str[0] == '-') { 4946807d721SLars-Peter Clausen negative = true; 4956807d721SLars-Peter Clausen str++; 4966807d721SLars-Peter Clausen } else if (str[0] == '+') { 4976807d721SLars-Peter Clausen str++; 4986807d721SLars-Peter Clausen } 4996807d721SLars-Peter Clausen 5006807d721SLars-Peter Clausen while (*str) { 5016807d721SLars-Peter Clausen if ('0' <= *str && *str <= '9') { 5026807d721SLars-Peter Clausen if (integer_part) { 5036807d721SLars-Peter Clausen i = i * 10 + *str - '0'; 5046807d721SLars-Peter Clausen } else { 5056807d721SLars-Peter Clausen f += fract_mult * (*str - '0'); 5066807d721SLars-Peter Clausen fract_mult /= 10; 5076807d721SLars-Peter Clausen } 5086807d721SLars-Peter Clausen } else if (*str == '\n') { 5096807d721SLars-Peter Clausen if (*(str + 1) == '\0') 5106807d721SLars-Peter Clausen break; 5116807d721SLars-Peter Clausen else 5126807d721SLars-Peter Clausen return -EINVAL; 5136807d721SLars-Peter Clausen } else if (*str == '.' && integer_part) { 5146807d721SLars-Peter Clausen integer_part = false; 5156807d721SLars-Peter Clausen } else { 5166807d721SLars-Peter Clausen return -EINVAL; 5176807d721SLars-Peter Clausen } 5186807d721SLars-Peter Clausen str++; 5196807d721SLars-Peter Clausen } 5206807d721SLars-Peter Clausen 5216807d721SLars-Peter Clausen if (negative) { 5226807d721SLars-Peter Clausen if (i) 5236807d721SLars-Peter Clausen i = -i; 5246807d721SLars-Peter Clausen else 5256807d721SLars-Peter Clausen f = -f; 5266807d721SLars-Peter Clausen } 5276807d721SLars-Peter Clausen 5286807d721SLars-Peter Clausen *integer = i; 5296807d721SLars-Peter Clausen *fract = f; 5306807d721SLars-Peter Clausen 5316807d721SLars-Peter Clausen return 0; 5326807d721SLars-Peter Clausen } 5336807d721SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_str_to_fixpoint); 5346807d721SLars-Peter Clausen 535a980e046SJonathan Cameron static ssize_t iio_write_channel_info(struct device *dev, 536a980e046SJonathan Cameron struct device_attribute *attr, 537a980e046SJonathan Cameron const char *buf, 538a980e046SJonathan Cameron size_t len) 539a980e046SJonathan Cameron { 540e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 541a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 5426807d721SLars-Peter Clausen int ret, fract_mult = 100000; 5436807d721SLars-Peter Clausen int integer, fract; 544a980e046SJonathan Cameron 545a980e046SJonathan Cameron /* Assumes decimal - precision based on number of digits */ 546a980e046SJonathan Cameron if (!indio_dev->info->write_raw) 547a980e046SJonathan Cameron return -EINVAL; 548a980e046SJonathan Cameron 549a980e046SJonathan Cameron if (indio_dev->info->write_raw_get_fmt) 550a980e046SJonathan Cameron switch (indio_dev->info->write_raw_get_fmt(indio_dev, 551a980e046SJonathan Cameron this_attr->c, this_attr->address)) { 552a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_MICRO: 553a980e046SJonathan Cameron fract_mult = 100000; 554a980e046SJonathan Cameron break; 555a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_NANO: 556a980e046SJonathan Cameron fract_mult = 100000000; 557a980e046SJonathan Cameron break; 558a980e046SJonathan Cameron default: 559a980e046SJonathan Cameron return -EINVAL; 560a980e046SJonathan Cameron } 561a980e046SJonathan Cameron 5626807d721SLars-Peter Clausen ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract); 5636807d721SLars-Peter Clausen if (ret) 5646807d721SLars-Peter Clausen return ret; 565a980e046SJonathan Cameron 566a980e046SJonathan Cameron ret = indio_dev->info->write_raw(indio_dev, this_attr->c, 567a980e046SJonathan Cameron integer, fract, this_attr->address); 568a980e046SJonathan Cameron if (ret) 569a980e046SJonathan Cameron return ret; 570a980e046SJonathan Cameron 571a980e046SJonathan Cameron return len; 572a980e046SJonathan Cameron } 573a980e046SJonathan Cameron 574a980e046SJonathan Cameron static 575a980e046SJonathan Cameron int __iio_device_attr_init(struct device_attribute *dev_attr, 576a980e046SJonathan Cameron const char *postfix, 577a980e046SJonathan Cameron struct iio_chan_spec const *chan, 578a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 579a980e046SJonathan Cameron struct device_attribute *attr, 580a980e046SJonathan Cameron char *buf), 581a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 582a980e046SJonathan Cameron struct device_attribute *attr, 583a980e046SJonathan Cameron const char *buf, 584a980e046SJonathan Cameron size_t len), 5853704432fSJonathan Cameron enum iio_shared_by shared_by) 586a980e046SJonathan Cameron { 5873704432fSJonathan Cameron int ret = 0; 5887bbcf7e1SLars-Peter Clausen char *name = NULL; 5893704432fSJonathan Cameron char *full_postfix; 590a980e046SJonathan Cameron sysfs_attr_init(&dev_attr->attr); 591a980e046SJonathan Cameron 592a980e046SJonathan Cameron /* Build up postfix of <extend_name>_<modifier>_postfix */ 5933704432fSJonathan Cameron if (chan->modified && (shared_by == IIO_SEPARATE)) { 594a980e046SJonathan Cameron if (chan->extend_name) 595a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", 596a980e046SJonathan Cameron iio_modifier_names[chan 597a980e046SJonathan Cameron ->channel2], 598a980e046SJonathan Cameron chan->extend_name, 599a980e046SJonathan Cameron postfix); 600a980e046SJonathan Cameron else 601a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s", 602a980e046SJonathan Cameron iio_modifier_names[chan 603a980e046SJonathan Cameron ->channel2], 604a980e046SJonathan Cameron postfix); 605a980e046SJonathan Cameron } else { 60677bfa8baSLars-Peter Clausen if (chan->extend_name == NULL || shared_by != IIO_SEPARATE) 607a980e046SJonathan Cameron full_postfix = kstrdup(postfix, GFP_KERNEL); 608a980e046SJonathan Cameron else 609a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, 610a980e046SJonathan Cameron "%s_%s", 611a980e046SJonathan Cameron chan->extend_name, 612a980e046SJonathan Cameron postfix); 613a980e046SJonathan Cameron } 6143704432fSJonathan Cameron if (full_postfix == NULL) 6153704432fSJonathan Cameron return -ENOMEM; 616a980e046SJonathan Cameron 617a980e046SJonathan Cameron if (chan->differential) { /* Differential can not have modifier */ 6183704432fSJonathan Cameron switch (shared_by) { 619c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 6207bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 621c006ec83SJonathan Cameron break; 622c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 6237bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 624c006ec83SJonathan Cameron iio_direction[chan->output], 625c006ec83SJonathan Cameron full_postfix); 626c006ec83SJonathan Cameron break; 6273704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 6287bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s", 629a980e046SJonathan Cameron iio_direction[chan->output], 630a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 631a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 632a980e046SJonathan Cameron full_postfix); 6333704432fSJonathan Cameron break; 6343704432fSJonathan Cameron case IIO_SEPARATE: 6353704432fSJonathan Cameron if (!chan->indexed) { 6363704432fSJonathan Cameron WARN_ON("Differential channels must be indexed\n"); 6373704432fSJonathan Cameron ret = -EINVAL; 6383704432fSJonathan Cameron goto error_free_full_postfix; 6393704432fSJonathan Cameron } 6407bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, 6413704432fSJonathan Cameron "%s_%s%d-%s%d_%s", 642a980e046SJonathan Cameron iio_direction[chan->output], 643a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 644a980e046SJonathan Cameron chan->channel, 645a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 646a980e046SJonathan Cameron chan->channel2, 647a980e046SJonathan Cameron full_postfix); 6483704432fSJonathan Cameron break; 649a980e046SJonathan Cameron } 650a980e046SJonathan Cameron } else { /* Single ended */ 6513704432fSJonathan Cameron switch (shared_by) { 652c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 6537bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 654c006ec83SJonathan Cameron break; 655c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 6567bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 657c006ec83SJonathan Cameron iio_direction[chan->output], 658c006ec83SJonathan Cameron full_postfix); 659c006ec83SJonathan Cameron break; 6603704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 6617bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 662a980e046SJonathan Cameron iio_direction[chan->output], 663a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 664a980e046SJonathan Cameron full_postfix); 6653704432fSJonathan Cameron break; 6663704432fSJonathan Cameron 6673704432fSJonathan Cameron case IIO_SEPARATE: 6683704432fSJonathan Cameron if (chan->indexed) 6697bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s%d_%s", 670a980e046SJonathan Cameron iio_direction[chan->output], 671a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 672a980e046SJonathan Cameron chan->channel, 673a980e046SJonathan Cameron full_postfix); 674a980e046SJonathan Cameron else 6757bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 676a980e046SJonathan Cameron iio_direction[chan->output], 677a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 678a980e046SJonathan Cameron full_postfix); 6793704432fSJonathan Cameron break; 6803704432fSJonathan Cameron } 681a980e046SJonathan Cameron } 6827bbcf7e1SLars-Peter Clausen if (name == NULL) { 683a980e046SJonathan Cameron ret = -ENOMEM; 684a980e046SJonathan Cameron goto error_free_full_postfix; 685a980e046SJonathan Cameron } 6867bbcf7e1SLars-Peter Clausen dev_attr->attr.name = name; 687a980e046SJonathan Cameron 688a980e046SJonathan Cameron if (readfunc) { 689a980e046SJonathan Cameron dev_attr->attr.mode |= S_IRUGO; 690a980e046SJonathan Cameron dev_attr->show = readfunc; 691a980e046SJonathan Cameron } 692a980e046SJonathan Cameron 693a980e046SJonathan Cameron if (writefunc) { 694a980e046SJonathan Cameron dev_attr->attr.mode |= S_IWUSR; 695a980e046SJonathan Cameron dev_attr->store = writefunc; 696a980e046SJonathan Cameron } 6977bbcf7e1SLars-Peter Clausen 698a980e046SJonathan Cameron error_free_full_postfix: 699a980e046SJonathan Cameron kfree(full_postfix); 7003704432fSJonathan Cameron 701a980e046SJonathan Cameron return ret; 702a980e046SJonathan Cameron } 703a980e046SJonathan Cameron 704a980e046SJonathan Cameron static void __iio_device_attr_deinit(struct device_attribute *dev_attr) 705a980e046SJonathan Cameron { 706a980e046SJonathan Cameron kfree(dev_attr->attr.name); 707a980e046SJonathan Cameron } 708a980e046SJonathan Cameron 709a980e046SJonathan Cameron int __iio_add_chan_devattr(const char *postfix, 710a980e046SJonathan Cameron struct iio_chan_spec const *chan, 711a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 712a980e046SJonathan Cameron struct device_attribute *attr, 713a980e046SJonathan Cameron char *buf), 714a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 715a980e046SJonathan Cameron struct device_attribute *attr, 716a980e046SJonathan Cameron const char *buf, 717a980e046SJonathan Cameron size_t len), 718a980e046SJonathan Cameron u64 mask, 7193704432fSJonathan Cameron enum iio_shared_by shared_by, 720a980e046SJonathan Cameron struct device *dev, 721a980e046SJonathan Cameron struct list_head *attr_list) 722a980e046SJonathan Cameron { 723a980e046SJonathan Cameron int ret; 724a980e046SJonathan Cameron struct iio_dev_attr *iio_attr, *t; 725a980e046SJonathan Cameron 726670c1103SSachin Kamat iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL); 72792825ff9SHartmut Knaack if (iio_attr == NULL) 72892825ff9SHartmut Knaack return -ENOMEM; 729a980e046SJonathan Cameron ret = __iio_device_attr_init(&iio_attr->dev_attr, 730a980e046SJonathan Cameron postfix, chan, 7313704432fSJonathan Cameron readfunc, writefunc, shared_by); 732a980e046SJonathan Cameron if (ret) 733a980e046SJonathan Cameron goto error_iio_dev_attr_free; 734a980e046SJonathan Cameron iio_attr->c = chan; 735a980e046SJonathan Cameron iio_attr->address = mask; 736a980e046SJonathan Cameron list_for_each_entry(t, attr_list, l) 737a980e046SJonathan Cameron if (strcmp(t->dev_attr.attr.name, 738a980e046SJonathan Cameron iio_attr->dev_attr.attr.name) == 0) { 7393704432fSJonathan Cameron if (shared_by == IIO_SEPARATE) 740a980e046SJonathan Cameron dev_err(dev, "tried to double register : %s\n", 741a980e046SJonathan Cameron t->dev_attr.attr.name); 742a980e046SJonathan Cameron ret = -EBUSY; 743a980e046SJonathan Cameron goto error_device_attr_deinit; 744a980e046SJonathan Cameron } 745a980e046SJonathan Cameron list_add(&iio_attr->l, attr_list); 746a980e046SJonathan Cameron 747a980e046SJonathan Cameron return 0; 748a980e046SJonathan Cameron 749a980e046SJonathan Cameron error_device_attr_deinit: 750a980e046SJonathan Cameron __iio_device_attr_deinit(&iio_attr->dev_attr); 751a980e046SJonathan Cameron error_iio_dev_attr_free: 752a980e046SJonathan Cameron kfree(iio_attr); 753a980e046SJonathan Cameron return ret; 754a980e046SJonathan Cameron } 755a980e046SJonathan Cameron 7563704432fSJonathan Cameron static int iio_device_add_info_mask_type(struct iio_dev *indio_dev, 7573704432fSJonathan Cameron struct iio_chan_spec const *chan, 7583704432fSJonathan Cameron enum iio_shared_by shared_by, 7593704432fSJonathan Cameron const long *infomask) 7603704432fSJonathan Cameron { 7613704432fSJonathan Cameron int i, ret, attrcount = 0; 7623704432fSJonathan Cameron 7633704432fSJonathan Cameron for_each_set_bit(i, infomask, sizeof(infomask)*8) { 764ef4b4856SJonathan Cameron if (i >= ARRAY_SIZE(iio_chan_info_postfix)) 765ef4b4856SJonathan Cameron return -EINVAL; 7663704432fSJonathan Cameron ret = __iio_add_chan_devattr(iio_chan_info_postfix[i], 7673704432fSJonathan Cameron chan, 7683704432fSJonathan Cameron &iio_read_channel_info, 7693704432fSJonathan Cameron &iio_write_channel_info, 7703704432fSJonathan Cameron i, 7713704432fSJonathan Cameron shared_by, 7723704432fSJonathan Cameron &indio_dev->dev, 7733704432fSJonathan Cameron &indio_dev->channel_attr_list); 7743704432fSJonathan Cameron if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) 7753704432fSJonathan Cameron continue; 7763704432fSJonathan Cameron else if (ret < 0) 7773704432fSJonathan Cameron return ret; 7783704432fSJonathan Cameron attrcount++; 7793704432fSJonathan Cameron } 7803704432fSJonathan Cameron 7813704432fSJonathan Cameron return attrcount; 7823704432fSJonathan Cameron } 7833704432fSJonathan Cameron 784a980e046SJonathan Cameron static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev, 785a980e046SJonathan Cameron struct iio_chan_spec const *chan) 786a980e046SJonathan Cameron { 787a980e046SJonathan Cameron int ret, attrcount = 0; 788a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 789a980e046SJonathan Cameron 790a980e046SJonathan Cameron if (chan->channel < 0) 791a980e046SJonathan Cameron return 0; 7923704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 7933704432fSJonathan Cameron IIO_SEPARATE, 7943704432fSJonathan Cameron &chan->info_mask_separate); 7958655cc49SJonathan Cameron if (ret < 0) 7963704432fSJonathan Cameron return ret; 7973704432fSJonathan Cameron attrcount += ret; 7983704432fSJonathan Cameron 7993704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 8003704432fSJonathan Cameron IIO_SHARED_BY_TYPE, 8013704432fSJonathan Cameron &chan->info_mask_shared_by_type); 8023704432fSJonathan Cameron if (ret < 0) 8033704432fSJonathan Cameron return ret; 8043704432fSJonathan Cameron attrcount += ret; 805a980e046SJonathan Cameron 806c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 807c006ec83SJonathan Cameron IIO_SHARED_BY_DIR, 808c006ec83SJonathan Cameron &chan->info_mask_shared_by_dir); 809c006ec83SJonathan Cameron if (ret < 0) 810c006ec83SJonathan Cameron return ret; 811c006ec83SJonathan Cameron attrcount += ret; 812c006ec83SJonathan Cameron 813c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 814c006ec83SJonathan Cameron IIO_SHARED_BY_ALL, 815c006ec83SJonathan Cameron &chan->info_mask_shared_by_all); 816c006ec83SJonathan Cameron if (ret < 0) 817c006ec83SJonathan Cameron return ret; 818c006ec83SJonathan Cameron attrcount += ret; 819c006ec83SJonathan Cameron 820a980e046SJonathan Cameron if (chan->ext_info) { 821a980e046SJonathan Cameron unsigned int i = 0; 822a980e046SJonathan Cameron for (ext_info = chan->ext_info; ext_info->name; ext_info++) { 823a980e046SJonathan Cameron ret = __iio_add_chan_devattr(ext_info->name, 824a980e046SJonathan Cameron chan, 825a980e046SJonathan Cameron ext_info->read ? 826a980e046SJonathan Cameron &iio_read_channel_ext_info : NULL, 827a980e046SJonathan Cameron ext_info->write ? 828a980e046SJonathan Cameron &iio_write_channel_ext_info : NULL, 829a980e046SJonathan Cameron i, 830a980e046SJonathan Cameron ext_info->shared, 831a980e046SJonathan Cameron &indio_dev->dev, 832a980e046SJonathan Cameron &indio_dev->channel_attr_list); 833a980e046SJonathan Cameron i++; 834a980e046SJonathan Cameron if (ret == -EBUSY && ext_info->shared) 835a980e046SJonathan Cameron continue; 836a980e046SJonathan Cameron 837a980e046SJonathan Cameron if (ret) 8383704432fSJonathan Cameron return ret; 839a980e046SJonathan Cameron 840a980e046SJonathan Cameron attrcount++; 841a980e046SJonathan Cameron } 842a980e046SJonathan Cameron } 843a980e046SJonathan Cameron 8443704432fSJonathan Cameron return attrcount; 845a980e046SJonathan Cameron } 846a980e046SJonathan Cameron 84784088ebdSLars-Peter Clausen /** 84884088ebdSLars-Peter Clausen * iio_free_chan_devattr_list() - Free a list of IIO device attributes 84984088ebdSLars-Peter Clausen * @attr_list: List of IIO device attributes 85084088ebdSLars-Peter Clausen * 85184088ebdSLars-Peter Clausen * This function frees the memory allocated for each of the IIO device 852c1b03ab5SMartin Fuzzey * attributes in the list. 85384088ebdSLars-Peter Clausen */ 85484088ebdSLars-Peter Clausen void iio_free_chan_devattr_list(struct list_head *attr_list) 855a980e046SJonathan Cameron { 85684088ebdSLars-Peter Clausen struct iio_dev_attr *p, *n; 85784088ebdSLars-Peter Clausen 85884088ebdSLars-Peter Clausen list_for_each_entry_safe(p, n, attr_list, l) { 859a980e046SJonathan Cameron kfree(p->dev_attr.attr.name); 860c1b03ab5SMartin Fuzzey list_del(&p->l); 861a980e046SJonathan Cameron kfree(p); 862a980e046SJonathan Cameron } 86384088ebdSLars-Peter Clausen } 864a980e046SJonathan Cameron 865a980e046SJonathan Cameron static ssize_t iio_show_dev_name(struct device *dev, 866a980e046SJonathan Cameron struct device_attribute *attr, 867a980e046SJonathan Cameron char *buf) 868a980e046SJonathan Cameron { 869e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 870598db581SKees Cook return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name); 871a980e046SJonathan Cameron } 872a980e046SJonathan Cameron 873a980e046SJonathan Cameron static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL); 874a980e046SJonathan Cameron 875a980e046SJonathan Cameron static int iio_device_register_sysfs(struct iio_dev *indio_dev) 876a980e046SJonathan Cameron { 877a980e046SJonathan Cameron int i, ret = 0, attrcount, attrn, attrcount_orig = 0; 87884088ebdSLars-Peter Clausen struct iio_dev_attr *p; 879a980e046SJonathan Cameron struct attribute **attr; 880a980e046SJonathan Cameron 881a980e046SJonathan Cameron /* First count elements in any existing group */ 882a980e046SJonathan Cameron if (indio_dev->info->attrs) { 883a980e046SJonathan Cameron attr = indio_dev->info->attrs->attrs; 884a980e046SJonathan Cameron while (*attr++ != NULL) 885a980e046SJonathan Cameron attrcount_orig++; 886a980e046SJonathan Cameron } 887a980e046SJonathan Cameron attrcount = attrcount_orig; 888a980e046SJonathan Cameron /* 889a980e046SJonathan Cameron * New channel registration method - relies on the fact a group does 890d25b3808SPeter Meerwald * not need to be initialized if its name is NULL. 891a980e046SJonathan Cameron */ 892a980e046SJonathan Cameron if (indio_dev->channels) 893a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) { 894a980e046SJonathan Cameron ret = iio_device_add_channel_sysfs(indio_dev, 895a980e046SJonathan Cameron &indio_dev 896a980e046SJonathan Cameron ->channels[i]); 897a980e046SJonathan Cameron if (ret < 0) 898a980e046SJonathan Cameron goto error_clear_attrs; 899a980e046SJonathan Cameron attrcount += ret; 900a980e046SJonathan Cameron } 901a980e046SJonathan Cameron 902a980e046SJonathan Cameron if (indio_dev->name) 903a980e046SJonathan Cameron attrcount++; 904a980e046SJonathan Cameron 905a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1, 906a980e046SJonathan Cameron sizeof(indio_dev->chan_attr_group.attrs[0]), 907a980e046SJonathan Cameron GFP_KERNEL); 908a980e046SJonathan Cameron if (indio_dev->chan_attr_group.attrs == NULL) { 909a980e046SJonathan Cameron ret = -ENOMEM; 910a980e046SJonathan Cameron goto error_clear_attrs; 911a980e046SJonathan Cameron } 912a980e046SJonathan Cameron /* Copy across original attributes */ 913a980e046SJonathan Cameron if (indio_dev->info->attrs) 914a980e046SJonathan Cameron memcpy(indio_dev->chan_attr_group.attrs, 915a980e046SJonathan Cameron indio_dev->info->attrs->attrs, 916a980e046SJonathan Cameron sizeof(indio_dev->chan_attr_group.attrs[0]) 917a980e046SJonathan Cameron *attrcount_orig); 918a980e046SJonathan Cameron attrn = attrcount_orig; 919a980e046SJonathan Cameron /* Add all elements from the list. */ 920a980e046SJonathan Cameron list_for_each_entry(p, &indio_dev->channel_attr_list, l) 921a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr; 922a980e046SJonathan Cameron if (indio_dev->name) 923a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr; 924a980e046SJonathan Cameron 925a980e046SJonathan Cameron indio_dev->groups[indio_dev->groupcounter++] = 926a980e046SJonathan Cameron &indio_dev->chan_attr_group; 927a980e046SJonathan Cameron 928a980e046SJonathan Cameron return 0; 929a980e046SJonathan Cameron 930a980e046SJonathan Cameron error_clear_attrs: 93184088ebdSLars-Peter Clausen iio_free_chan_devattr_list(&indio_dev->channel_attr_list); 932a980e046SJonathan Cameron 933a980e046SJonathan Cameron return ret; 934a980e046SJonathan Cameron } 935a980e046SJonathan Cameron 936a980e046SJonathan Cameron static void iio_device_unregister_sysfs(struct iio_dev *indio_dev) 937a980e046SJonathan Cameron { 938a980e046SJonathan Cameron 93984088ebdSLars-Peter Clausen iio_free_chan_devattr_list(&indio_dev->channel_attr_list); 940a980e046SJonathan Cameron kfree(indio_dev->chan_attr_group.attrs); 941c1b03ab5SMartin Fuzzey indio_dev->chan_attr_group.attrs = NULL; 942a980e046SJonathan Cameron } 943a980e046SJonathan Cameron 944a980e046SJonathan Cameron static void iio_dev_release(struct device *device) 945a980e046SJonathan Cameron { 946e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(device); 947a980e046SJonathan Cameron if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) 948a980e046SJonathan Cameron iio_device_unregister_trigger_consumer(indio_dev); 949a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 950a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 951e407fd65SLars-Peter Clausen 9529e69c935SLars-Peter Clausen iio_buffer_put(indio_dev->buffer); 9539e69c935SLars-Peter Clausen 954e407fd65SLars-Peter Clausen ida_simple_remove(&iio_ida, indio_dev->id); 955e407fd65SLars-Peter Clausen kfree(indio_dev); 956a980e046SJonathan Cameron } 957a980e046SJonathan Cameron 95817d82b47SGuenter Roeck struct device_type iio_device_type = { 959a980e046SJonathan Cameron .name = "iio_device", 960a980e046SJonathan Cameron .release = iio_dev_release, 961a980e046SJonathan Cameron }; 962a980e046SJonathan Cameron 963a7e57dceSSachin Kamat /** 964a7e57dceSSachin Kamat * iio_device_alloc() - allocate an iio_dev from a driver 965a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 966a7e57dceSSachin Kamat **/ 9677cbb7537SLars-Peter Clausen struct iio_dev *iio_device_alloc(int sizeof_priv) 968a980e046SJonathan Cameron { 969a980e046SJonathan Cameron struct iio_dev *dev; 970a980e046SJonathan Cameron size_t alloc_size; 971a980e046SJonathan Cameron 972a980e046SJonathan Cameron alloc_size = sizeof(struct iio_dev); 973a980e046SJonathan Cameron if (sizeof_priv) { 974a980e046SJonathan Cameron alloc_size = ALIGN(alloc_size, IIO_ALIGN); 975a980e046SJonathan Cameron alloc_size += sizeof_priv; 976a980e046SJonathan Cameron } 977a980e046SJonathan Cameron /* ensure 32-byte alignment of whole construct ? */ 978a980e046SJonathan Cameron alloc_size += IIO_ALIGN - 1; 979a980e046SJonathan Cameron 980a980e046SJonathan Cameron dev = kzalloc(alloc_size, GFP_KERNEL); 981a980e046SJonathan Cameron 982a980e046SJonathan Cameron if (dev) { 983a980e046SJonathan Cameron dev->dev.groups = dev->groups; 98417d82b47SGuenter Roeck dev->dev.type = &iio_device_type; 985a980e046SJonathan Cameron dev->dev.bus = &iio_bus_type; 986a980e046SJonathan Cameron device_initialize(&dev->dev); 987a980e046SJonathan Cameron dev_set_drvdata(&dev->dev, (void *)dev); 988a980e046SJonathan Cameron mutex_init(&dev->mlock); 989a980e046SJonathan Cameron mutex_init(&dev->info_exist_lock); 990e407fd65SLars-Peter Clausen INIT_LIST_HEAD(&dev->channel_attr_list); 991a980e046SJonathan Cameron 992a980e046SJonathan Cameron dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL); 993a980e046SJonathan Cameron if (dev->id < 0) { 994a980e046SJonathan Cameron /* cannot use a dev_err as the name isn't available */ 9953176dd5dSSachin Kamat pr_err("failed to get device id\n"); 996a980e046SJonathan Cameron kfree(dev); 997a980e046SJonathan Cameron return NULL; 998a980e046SJonathan Cameron } 999a980e046SJonathan Cameron dev_set_name(&dev->dev, "iio:device%d", dev->id); 100084b36ce5SJonathan Cameron INIT_LIST_HEAD(&dev->buffer_list); 1001a980e046SJonathan Cameron } 1002a980e046SJonathan Cameron 1003a980e046SJonathan Cameron return dev; 1004a980e046SJonathan Cameron } 10057cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_alloc); 1006a980e046SJonathan Cameron 1007a7e57dceSSachin Kamat /** 1008a7e57dceSSachin Kamat * iio_device_free() - free an iio_dev from a driver 1009a7e57dceSSachin Kamat * @dev: the iio_dev associated with the device 1010a7e57dceSSachin Kamat **/ 10117cbb7537SLars-Peter Clausen void iio_device_free(struct iio_dev *dev) 1012a980e046SJonathan Cameron { 1013e407fd65SLars-Peter Clausen if (dev) 1014e407fd65SLars-Peter Clausen put_device(&dev->dev); 1015a980e046SJonathan Cameron } 10167cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_free); 1017a980e046SJonathan Cameron 10189dabaf5eSGrygorii Strashko static void devm_iio_device_release(struct device *dev, void *res) 10199dabaf5eSGrygorii Strashko { 10209dabaf5eSGrygorii Strashko iio_device_free(*(struct iio_dev **)res); 10219dabaf5eSGrygorii Strashko } 10229dabaf5eSGrygorii Strashko 10239dabaf5eSGrygorii Strashko static int devm_iio_device_match(struct device *dev, void *res, void *data) 10249dabaf5eSGrygorii Strashko { 10259dabaf5eSGrygorii Strashko struct iio_dev **r = res; 10269dabaf5eSGrygorii Strashko if (!r || !*r) { 10279dabaf5eSGrygorii Strashko WARN_ON(!r || !*r); 10289dabaf5eSGrygorii Strashko return 0; 10299dabaf5eSGrygorii Strashko } 10309dabaf5eSGrygorii Strashko return *r == data; 10319dabaf5eSGrygorii Strashko } 10329dabaf5eSGrygorii Strashko 1033a7e57dceSSachin Kamat /** 1034a7e57dceSSachin Kamat * devm_iio_device_alloc - Resource-managed iio_device_alloc() 1035a7e57dceSSachin Kamat * @dev: Device to allocate iio_dev for 1036a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 1037a7e57dceSSachin Kamat * 1038a7e57dceSSachin Kamat * Managed iio_device_alloc. iio_dev allocated with this function is 1039a7e57dceSSachin Kamat * automatically freed on driver detach. 1040a7e57dceSSachin Kamat * 1041a7e57dceSSachin Kamat * If an iio_dev allocated with this function needs to be freed separately, 1042a7e57dceSSachin Kamat * devm_iio_device_free() must be used. 1043a7e57dceSSachin Kamat * 1044a7e57dceSSachin Kamat * RETURNS: 1045a7e57dceSSachin Kamat * Pointer to allocated iio_dev on success, NULL on failure. 1046a7e57dceSSachin Kamat */ 10479dabaf5eSGrygorii Strashko struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv) 10489dabaf5eSGrygorii Strashko { 10499dabaf5eSGrygorii Strashko struct iio_dev **ptr, *iio_dev; 10509dabaf5eSGrygorii Strashko 10519dabaf5eSGrygorii Strashko ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr), 10529dabaf5eSGrygorii Strashko GFP_KERNEL); 10539dabaf5eSGrygorii Strashko if (!ptr) 10549dabaf5eSGrygorii Strashko return NULL; 10559dabaf5eSGrygorii Strashko 10569dabaf5eSGrygorii Strashko iio_dev = iio_device_alloc(sizeof_priv); 10579dabaf5eSGrygorii Strashko if (iio_dev) { 10589dabaf5eSGrygorii Strashko *ptr = iio_dev; 10599dabaf5eSGrygorii Strashko devres_add(dev, ptr); 10609dabaf5eSGrygorii Strashko } else { 10619dabaf5eSGrygorii Strashko devres_free(ptr); 10629dabaf5eSGrygorii Strashko } 10639dabaf5eSGrygorii Strashko 10649dabaf5eSGrygorii Strashko return iio_dev; 10659dabaf5eSGrygorii Strashko } 10669dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_alloc); 10679dabaf5eSGrygorii Strashko 1068a7e57dceSSachin Kamat /** 1069a7e57dceSSachin Kamat * devm_iio_device_free - Resource-managed iio_device_free() 1070a7e57dceSSachin Kamat * @dev: Device this iio_dev belongs to 1071a7e57dceSSachin Kamat * @iio_dev: the iio_dev associated with the device 1072a7e57dceSSachin Kamat * 1073a7e57dceSSachin Kamat * Free iio_dev allocated with devm_iio_device_alloc(). 1074a7e57dceSSachin Kamat */ 10759dabaf5eSGrygorii Strashko void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev) 10769dabaf5eSGrygorii Strashko { 10779dabaf5eSGrygorii Strashko int rc; 10789dabaf5eSGrygorii Strashko 10799dabaf5eSGrygorii Strashko rc = devres_release(dev, devm_iio_device_release, 10809dabaf5eSGrygorii Strashko devm_iio_device_match, iio_dev); 10819dabaf5eSGrygorii Strashko WARN_ON(rc); 10829dabaf5eSGrygorii Strashko } 10839dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_free); 10849dabaf5eSGrygorii Strashko 1085a980e046SJonathan Cameron /** 1086a980e046SJonathan Cameron * iio_chrdev_open() - chrdev file open for buffer access and ioctls 1087a980e046SJonathan Cameron **/ 1088a980e046SJonathan Cameron static int iio_chrdev_open(struct inode *inode, struct file *filp) 1089a980e046SJonathan Cameron { 1090a980e046SJonathan Cameron struct iio_dev *indio_dev = container_of(inode->i_cdev, 1091a980e046SJonathan Cameron struct iio_dev, chrdev); 1092a980e046SJonathan Cameron 1093a980e046SJonathan Cameron if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags)) 1094a980e046SJonathan Cameron return -EBUSY; 1095a980e046SJonathan Cameron 1096cadc2125SLars-Peter Clausen iio_device_get(indio_dev); 1097cadc2125SLars-Peter Clausen 1098a980e046SJonathan Cameron filp->private_data = indio_dev; 1099a980e046SJonathan Cameron 1100a980e046SJonathan Cameron return 0; 1101a980e046SJonathan Cameron } 1102a980e046SJonathan Cameron 1103a980e046SJonathan Cameron /** 1104a980e046SJonathan Cameron * iio_chrdev_release() - chrdev file close buffer access and ioctls 1105a980e046SJonathan Cameron **/ 1106a980e046SJonathan Cameron static int iio_chrdev_release(struct inode *inode, struct file *filp) 1107a980e046SJonathan Cameron { 1108a980e046SJonathan Cameron struct iio_dev *indio_dev = container_of(inode->i_cdev, 1109a980e046SJonathan Cameron struct iio_dev, chrdev); 1110a980e046SJonathan Cameron clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags); 1111cadc2125SLars-Peter Clausen iio_device_put(indio_dev); 1112cadc2125SLars-Peter Clausen 1113a980e046SJonathan Cameron return 0; 1114a980e046SJonathan Cameron } 1115a980e046SJonathan Cameron 1116a980e046SJonathan Cameron /* Somewhat of a cross file organization violation - ioctls here are actually 1117a980e046SJonathan Cameron * event related */ 1118a980e046SJonathan Cameron static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 1119a980e046SJonathan Cameron { 1120a980e046SJonathan Cameron struct iio_dev *indio_dev = filp->private_data; 1121a980e046SJonathan Cameron int __user *ip = (int __user *)arg; 1122a980e046SJonathan Cameron int fd; 1123a980e046SJonathan Cameron 1124f18e7a06SLars-Peter Clausen if (!indio_dev->info) 1125f18e7a06SLars-Peter Clausen return -ENODEV; 1126f18e7a06SLars-Peter Clausen 1127a980e046SJonathan Cameron if (cmd == IIO_GET_EVENT_FD_IOCTL) { 1128a980e046SJonathan Cameron fd = iio_event_getfd(indio_dev); 1129a980e046SJonathan Cameron if (copy_to_user(ip, &fd, sizeof(fd))) 1130a980e046SJonathan Cameron return -EFAULT; 1131a980e046SJonathan Cameron return 0; 1132a980e046SJonathan Cameron } 1133a980e046SJonathan Cameron return -EINVAL; 1134a980e046SJonathan Cameron } 1135a980e046SJonathan Cameron 1136a980e046SJonathan Cameron static const struct file_operations iio_buffer_fileops = { 1137a980e046SJonathan Cameron .read = iio_buffer_read_first_n_outer_addr, 1138a980e046SJonathan Cameron .release = iio_chrdev_release, 1139a980e046SJonathan Cameron .open = iio_chrdev_open, 1140a980e046SJonathan Cameron .poll = iio_buffer_poll_addr, 1141a980e046SJonathan Cameron .owner = THIS_MODULE, 1142a980e046SJonathan Cameron .llseek = noop_llseek, 1143a980e046SJonathan Cameron .unlocked_ioctl = iio_ioctl, 1144a980e046SJonathan Cameron .compat_ioctl = iio_ioctl, 1145a980e046SJonathan Cameron }; 1146a980e046SJonathan Cameron 11478f5d8727SVlad Dogaru static int iio_check_unique_scan_index(struct iio_dev *indio_dev) 11488f5d8727SVlad Dogaru { 11498f5d8727SVlad Dogaru int i, j; 11508f5d8727SVlad Dogaru const struct iio_chan_spec *channels = indio_dev->channels; 11518f5d8727SVlad Dogaru 11528f5d8727SVlad Dogaru if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES)) 11538f5d8727SVlad Dogaru return 0; 11548f5d8727SVlad Dogaru 11558f5d8727SVlad Dogaru for (i = 0; i < indio_dev->num_channels - 1; i++) { 11568f5d8727SVlad Dogaru if (channels[i].scan_index < 0) 11578f5d8727SVlad Dogaru continue; 11588f5d8727SVlad Dogaru for (j = i + 1; j < indio_dev->num_channels; j++) 11598f5d8727SVlad Dogaru if (channels[i].scan_index == channels[j].scan_index) { 11608f5d8727SVlad Dogaru dev_err(&indio_dev->dev, 11618f5d8727SVlad Dogaru "Duplicate scan index %d\n", 11628f5d8727SVlad Dogaru channels[i].scan_index); 11638f5d8727SVlad Dogaru return -EINVAL; 11648f5d8727SVlad Dogaru } 11658f5d8727SVlad Dogaru } 11668f5d8727SVlad Dogaru 11678f5d8727SVlad Dogaru return 0; 11688f5d8727SVlad Dogaru } 11698f5d8727SVlad Dogaru 1170a980e046SJonathan Cameron static const struct iio_buffer_setup_ops noop_ring_setup_ops; 1171a980e046SJonathan Cameron 1172a7e57dceSSachin Kamat /** 1173a7e57dceSSachin Kamat * iio_device_register() - register a device with the IIO subsystem 1174a7e57dceSSachin Kamat * @indio_dev: Device structure filled by the device driver 1175a7e57dceSSachin Kamat **/ 1176a980e046SJonathan Cameron int iio_device_register(struct iio_dev *indio_dev) 1177a980e046SJonathan Cameron { 1178a980e046SJonathan Cameron int ret; 1179a980e046SJonathan Cameron 118017d82b47SGuenter Roeck /* If the calling driver did not initialize of_node, do it here */ 118117d82b47SGuenter Roeck if (!indio_dev->dev.of_node && indio_dev->dev.parent) 118217d82b47SGuenter Roeck indio_dev->dev.of_node = indio_dev->dev.parent->of_node; 118317d82b47SGuenter Roeck 11848f5d8727SVlad Dogaru ret = iio_check_unique_scan_index(indio_dev); 11858f5d8727SVlad Dogaru if (ret < 0) 11868f5d8727SVlad Dogaru return ret; 11878f5d8727SVlad Dogaru 1188a980e046SJonathan Cameron /* configure elements for the chrdev */ 1189a980e046SJonathan Cameron indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id); 1190a980e046SJonathan Cameron 1191a980e046SJonathan Cameron ret = iio_device_register_debugfs(indio_dev); 1192a980e046SJonathan Cameron if (ret) { 1193a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1194a980e046SJonathan Cameron "Failed to register debugfs interfaces\n"); 119592825ff9SHartmut Knaack return ret; 1196a980e046SJonathan Cameron } 11973e1b6c95SLars-Peter Clausen 11983e1b6c95SLars-Peter Clausen ret = iio_buffer_alloc_sysfs_and_mask(indio_dev); 11993e1b6c95SLars-Peter Clausen if (ret) { 12003e1b6c95SLars-Peter Clausen dev_err(indio_dev->dev.parent, 12013e1b6c95SLars-Peter Clausen "Failed to create buffer sysfs interfaces\n"); 12023e1b6c95SLars-Peter Clausen goto error_unreg_debugfs; 12033e1b6c95SLars-Peter Clausen } 12043e1b6c95SLars-Peter Clausen 1205a980e046SJonathan Cameron ret = iio_device_register_sysfs(indio_dev); 1206a980e046SJonathan Cameron if (ret) { 1207a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1208a980e046SJonathan Cameron "Failed to register sysfs interfaces\n"); 12093e1b6c95SLars-Peter Clausen goto error_buffer_free_sysfs; 1210a980e046SJonathan Cameron } 1211a980e046SJonathan Cameron ret = iio_device_register_eventset(indio_dev); 1212a980e046SJonathan Cameron if (ret) { 1213a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1214a980e046SJonathan Cameron "Failed to register event set\n"); 1215a980e046SJonathan Cameron goto error_free_sysfs; 1216a980e046SJonathan Cameron } 1217a980e046SJonathan Cameron if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) 1218a980e046SJonathan Cameron iio_device_register_trigger_consumer(indio_dev); 1219a980e046SJonathan Cameron 1220a980e046SJonathan Cameron if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) && 1221a980e046SJonathan Cameron indio_dev->setup_ops == NULL) 1222a980e046SJonathan Cameron indio_dev->setup_ops = &noop_ring_setup_ops; 1223a980e046SJonathan Cameron 1224a980e046SJonathan Cameron cdev_init(&indio_dev->chrdev, &iio_buffer_fileops); 1225a980e046SJonathan Cameron indio_dev->chrdev.owner = indio_dev->info->driver_module; 12260d5b7daeSLars-Peter Clausen indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj; 1227a980e046SJonathan Cameron ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1); 1228a980e046SJonathan Cameron if (ret < 0) 12290d5b7daeSLars-Peter Clausen goto error_unreg_eventset; 1230a980e046SJonathan Cameron 12310d5b7daeSLars-Peter Clausen ret = device_add(&indio_dev->dev); 12320d5b7daeSLars-Peter Clausen if (ret < 0) 12330d5b7daeSLars-Peter Clausen goto error_cdev_del; 12340d5b7daeSLars-Peter Clausen 12350d5b7daeSLars-Peter Clausen return 0; 12360d5b7daeSLars-Peter Clausen error_cdev_del: 12370d5b7daeSLars-Peter Clausen cdev_del(&indio_dev->chrdev); 1238a980e046SJonathan Cameron error_unreg_eventset: 1239a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 1240a980e046SJonathan Cameron error_free_sysfs: 1241a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 12423e1b6c95SLars-Peter Clausen error_buffer_free_sysfs: 12433e1b6c95SLars-Peter Clausen iio_buffer_free_sysfs_and_mask(indio_dev); 1244a980e046SJonathan Cameron error_unreg_debugfs: 1245a980e046SJonathan Cameron iio_device_unregister_debugfs(indio_dev); 1246a980e046SJonathan Cameron return ret; 1247a980e046SJonathan Cameron } 1248a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_register); 1249a980e046SJonathan Cameron 1250a7e57dceSSachin Kamat /** 1251a7e57dceSSachin Kamat * iio_device_unregister() - unregister a device from the IIO subsystem 1252a7e57dceSSachin Kamat * @indio_dev: Device structure representing the device. 1253a7e57dceSSachin Kamat **/ 1254a980e046SJonathan Cameron void iio_device_unregister(struct iio_dev *indio_dev) 1255a980e046SJonathan Cameron { 1256a980e046SJonathan Cameron mutex_lock(&indio_dev->info_exist_lock); 1257a87c82e4SLars-Peter Clausen 1258a87c82e4SLars-Peter Clausen device_del(&indio_dev->dev); 1259a87c82e4SLars-Peter Clausen 12600d5b7daeSLars-Peter Clausen if (indio_dev->chrdev.dev) 12610d5b7daeSLars-Peter Clausen cdev_del(&indio_dev->chrdev); 1262bc4c9612SLars-Peter Clausen iio_device_unregister_debugfs(indio_dev); 12630d5b7daeSLars-Peter Clausen 1264a87c82e4SLars-Peter Clausen iio_disable_all_buffers(indio_dev); 1265a87c82e4SLars-Peter Clausen 1266a980e046SJonathan Cameron indio_dev->info = NULL; 1267d2f0a48fSLars-Peter Clausen 1268d2f0a48fSLars-Peter Clausen iio_device_wakeup_eventset(indio_dev); 1269d2f0a48fSLars-Peter Clausen iio_buffer_wakeup_poll(indio_dev); 1270d2f0a48fSLars-Peter Clausen 1271a980e046SJonathan Cameron mutex_unlock(&indio_dev->info_exist_lock); 12723e1b6c95SLars-Peter Clausen 12733e1b6c95SLars-Peter Clausen iio_buffer_free_sysfs_and_mask(indio_dev); 1274a980e046SJonathan Cameron } 1275a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_unregister); 12768caa07c0SSachin Kamat 12778caa07c0SSachin Kamat static void devm_iio_device_unreg(struct device *dev, void *res) 12788caa07c0SSachin Kamat { 12798caa07c0SSachin Kamat iio_device_unregister(*(struct iio_dev **)res); 12808caa07c0SSachin Kamat } 12818caa07c0SSachin Kamat 12828caa07c0SSachin Kamat /** 12838caa07c0SSachin Kamat * devm_iio_device_register - Resource-managed iio_device_register() 12848caa07c0SSachin Kamat * @dev: Device to allocate iio_dev for 12858caa07c0SSachin Kamat * @indio_dev: Device structure filled by the device driver 12868caa07c0SSachin Kamat * 12878caa07c0SSachin Kamat * Managed iio_device_register. The IIO device registered with this 12888caa07c0SSachin Kamat * function is automatically unregistered on driver detach. This function 12898caa07c0SSachin Kamat * calls iio_device_register() internally. Refer to that function for more 12908caa07c0SSachin Kamat * information. 12918caa07c0SSachin Kamat * 12928caa07c0SSachin Kamat * If an iio_dev registered with this function needs to be unregistered 12938caa07c0SSachin Kamat * separately, devm_iio_device_unregister() must be used. 12948caa07c0SSachin Kamat * 12958caa07c0SSachin Kamat * RETURNS: 12968caa07c0SSachin Kamat * 0 on success, negative error number on failure. 12978caa07c0SSachin Kamat */ 12988caa07c0SSachin Kamat int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev) 12998caa07c0SSachin Kamat { 13008caa07c0SSachin Kamat struct iio_dev **ptr; 13018caa07c0SSachin Kamat int ret; 13028caa07c0SSachin Kamat 13038caa07c0SSachin Kamat ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL); 13048caa07c0SSachin Kamat if (!ptr) 13058caa07c0SSachin Kamat return -ENOMEM; 13068caa07c0SSachin Kamat 13078caa07c0SSachin Kamat *ptr = indio_dev; 13088caa07c0SSachin Kamat ret = iio_device_register(indio_dev); 13098caa07c0SSachin Kamat if (!ret) 13108caa07c0SSachin Kamat devres_add(dev, ptr); 13118caa07c0SSachin Kamat else 13128caa07c0SSachin Kamat devres_free(ptr); 13138caa07c0SSachin Kamat 13148caa07c0SSachin Kamat return ret; 13158caa07c0SSachin Kamat } 13168caa07c0SSachin Kamat EXPORT_SYMBOL_GPL(devm_iio_device_register); 13178caa07c0SSachin Kamat 13188caa07c0SSachin Kamat /** 13198caa07c0SSachin Kamat * devm_iio_device_unregister - Resource-managed iio_device_unregister() 13208caa07c0SSachin Kamat * @dev: Device this iio_dev belongs to 13218caa07c0SSachin Kamat * @indio_dev: the iio_dev associated with the device 13228caa07c0SSachin Kamat * 13238caa07c0SSachin Kamat * Unregister iio_dev registered with devm_iio_device_register(). 13248caa07c0SSachin Kamat */ 13258caa07c0SSachin Kamat void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev) 13268caa07c0SSachin Kamat { 13278caa07c0SSachin Kamat int rc; 13288caa07c0SSachin Kamat 13298caa07c0SSachin Kamat rc = devres_release(dev, devm_iio_device_unreg, 13308caa07c0SSachin Kamat devm_iio_device_match, indio_dev); 13318caa07c0SSachin Kamat WARN_ON(rc); 13328caa07c0SSachin Kamat } 13338caa07c0SSachin Kamat EXPORT_SYMBOL_GPL(devm_iio_device_unregister); 13348caa07c0SSachin Kamat 1335a980e046SJonathan Cameron subsys_initcall(iio_init); 1336a980e046SJonathan Cameron module_exit(iio_exit); 1337a980e046SJonathan Cameron 1338c8b95952SJonathan Cameron MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); 1339a980e046SJonathan Cameron MODULE_DESCRIPTION("Industrial I/O core"); 1340a980e046SJonathan Cameron MODULE_LICENSE("GPL"); 1341