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