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", 788ff6b3bcSMatt Ranostay [IIO_CONCENTRATION] = "concentration", 79d38d5469SMatt Ranostay [IIO_RESISTANCE] = "resistance", 80a980e046SJonathan Cameron }; 81a980e046SJonathan Cameron 82a980e046SJonathan Cameron static const char * const iio_modifier_names[] = { 83a980e046SJonathan Cameron [IIO_MOD_X] = "x", 84a980e046SJonathan Cameron [IIO_MOD_Y] = "y", 85a980e046SJonathan Cameron [IIO_MOD_Z] = "z", 864b8d8015SPeter Meerwald [IIO_MOD_X_AND_Y] = "x&y", 874b8d8015SPeter Meerwald [IIO_MOD_X_AND_Z] = "x&z", 884b8d8015SPeter Meerwald [IIO_MOD_Y_AND_Z] = "y&z", 894b8d8015SPeter Meerwald [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z", 904b8d8015SPeter Meerwald [IIO_MOD_X_OR_Y] = "x|y", 914b8d8015SPeter Meerwald [IIO_MOD_X_OR_Z] = "x|z", 924b8d8015SPeter Meerwald [IIO_MOD_Y_OR_Z] = "y|z", 934b8d8015SPeter Meerwald [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z", 948f5879b2SJonathan Cameron [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)", 95cf82cb81SJonathan Cameron [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2", 96a980e046SJonathan Cameron [IIO_MOD_LIGHT_BOTH] = "both", 97a980e046SJonathan Cameron [IIO_MOD_LIGHT_IR] = "ir", 9821cd1fabSJon Brenner [IIO_MOD_LIGHT_CLEAR] = "clear", 9921cd1fabSJon Brenner [IIO_MOD_LIGHT_RED] = "red", 10021cd1fabSJon Brenner [IIO_MOD_LIGHT_GREEN] = "green", 10121cd1fabSJon Brenner [IIO_MOD_LIGHT_BLUE] = "blue", 1025082f405SSrinivas Pandruvada [IIO_MOD_QUATERNION] = "quaternion", 103638b43b3SPeter Meerwald [IIO_MOD_TEMP_AMBIENT] = "ambient", 104638b43b3SPeter Meerwald [IIO_MOD_TEMP_OBJECT] = "object", 10511b8ddabSReyad Attiyat [IIO_MOD_NORTH_MAGN] = "from_north_magnetic", 10611b8ddabSReyad Attiyat [IIO_MOD_NORTH_TRUE] = "from_north_true", 10711b8ddabSReyad Attiyat [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp", 10811b8ddabSReyad Attiyat [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp", 10955aebeb9SDaniel Baluta [IIO_MOD_RUNNING] = "running", 11055aebeb9SDaniel Baluta [IIO_MOD_JOGGING] = "jogging", 11155aebeb9SDaniel Baluta [IIO_MOD_WALKING] = "walking", 11255aebeb9SDaniel Baluta [IIO_MOD_STILL] = "still", 1135a1a9329SIrina Tirdea [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)", 1141ce87f21SLars-Peter Clausen [IIO_MOD_I] = "i", 1151ce87f21SLars-Peter Clausen [IIO_MOD_Q] = "q", 1168ff6b3bcSMatt Ranostay [IIO_MOD_CO2] = "co2", 1178ff6b3bcSMatt Ranostay [IIO_MOD_VOC] = "voc", 118a980e046SJonathan Cameron }; 119a980e046SJonathan Cameron 120a980e046SJonathan Cameron /* relies on pairs of these shared then separate */ 121a980e046SJonathan Cameron static const char * const iio_chan_info_postfix[] = { 122a980e046SJonathan Cameron [IIO_CHAN_INFO_RAW] = "raw", 123a980e046SJonathan Cameron [IIO_CHAN_INFO_PROCESSED] = "input", 124a980e046SJonathan Cameron [IIO_CHAN_INFO_SCALE] = "scale", 125a980e046SJonathan Cameron [IIO_CHAN_INFO_OFFSET] = "offset", 126a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBSCALE] = "calibscale", 127a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBBIAS] = "calibbias", 128a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK] = "peak_raw", 129a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale", 130a980e046SJonathan Cameron [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw", 131a980e046SJonathan Cameron [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw", 132a980e046SJonathan Cameron [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY] 133a980e046SJonathan Cameron = "filter_low_pass_3db_frequency", 1343f7f642bSMartin Fuzzey [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY] 1353f7f642bSMartin Fuzzey = "filter_high_pass_3db_frequency", 136a980e046SJonathan Cameron [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency", 137a6b12855SMichael Hennerich [IIO_CHAN_INFO_FREQUENCY] = "frequency", 138a6b12855SMichael Hennerich [IIO_CHAN_INFO_PHASE] = "phase", 139b65d6212SMichael Hennerich [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain", 1407c9ab035Ssrinivas pandruvada [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis", 141899d90bdSPeter Meerwald [IIO_CHAN_INFO_INT_TIME] = "integration_time", 142a88bfe78SIrina Tirdea [IIO_CHAN_INFO_ENABLE] = "en", 143bcdf28fbSIrina Tirdea [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight", 144d37f6836SIrina Tirdea [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight", 1452f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count", 1462f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time", 147c8a85854SVianney le Clément de Saint-Marcq [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity", 148faaa4495SIrina Tirdea [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio", 149a980e046SJonathan Cameron }; 150a980e046SJonathan Cameron 151a7e57dceSSachin Kamat /** 152a7e57dceSSachin Kamat * iio_find_channel_from_si() - get channel from its scan index 153a7e57dceSSachin Kamat * @indio_dev: device 154a7e57dceSSachin Kamat * @si: scan index to match 155a7e57dceSSachin Kamat */ 156a980e046SJonathan Cameron const struct iio_chan_spec 157a980e046SJonathan Cameron *iio_find_channel_from_si(struct iio_dev *indio_dev, int si) 158a980e046SJonathan Cameron { 159a980e046SJonathan Cameron int i; 160a980e046SJonathan Cameron 161a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) 162a980e046SJonathan Cameron if (indio_dev->channels[i].scan_index == si) 163a980e046SJonathan Cameron return &indio_dev->channels[i]; 164a980e046SJonathan Cameron return NULL; 165a980e046SJonathan Cameron } 166a980e046SJonathan Cameron 167a980e046SJonathan Cameron /* This turns up an awful lot */ 168a980e046SJonathan Cameron ssize_t iio_read_const_attr(struct device *dev, 169a980e046SJonathan Cameron struct device_attribute *attr, 170a980e046SJonathan Cameron char *buf) 171a980e046SJonathan Cameron { 172a980e046SJonathan Cameron return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string); 173a980e046SJonathan Cameron } 174a980e046SJonathan Cameron EXPORT_SYMBOL(iio_read_const_attr); 175a980e046SJonathan Cameron 176a980e046SJonathan Cameron static int __init iio_init(void) 177a980e046SJonathan Cameron { 178a980e046SJonathan Cameron int ret; 179a980e046SJonathan Cameron 180a980e046SJonathan Cameron /* Register sysfs bus */ 181a980e046SJonathan Cameron ret = bus_register(&iio_bus_type); 182a980e046SJonathan Cameron if (ret < 0) { 1833176dd5dSSachin Kamat pr_err("could not register bus type\n"); 184a980e046SJonathan Cameron goto error_nothing; 185a980e046SJonathan Cameron } 186a980e046SJonathan Cameron 187a980e046SJonathan Cameron ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio"); 188a980e046SJonathan Cameron if (ret < 0) { 1893176dd5dSSachin Kamat pr_err("failed to allocate char dev region\n"); 190a980e046SJonathan Cameron goto error_unregister_bus_type; 191a980e046SJonathan Cameron } 192a980e046SJonathan Cameron 193a980e046SJonathan Cameron iio_debugfs_dentry = debugfs_create_dir("iio", NULL); 194a980e046SJonathan Cameron 195a980e046SJonathan Cameron return 0; 196a980e046SJonathan Cameron 197a980e046SJonathan Cameron error_unregister_bus_type: 198a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 199a980e046SJonathan Cameron error_nothing: 200a980e046SJonathan Cameron return ret; 201a980e046SJonathan Cameron } 202a980e046SJonathan Cameron 203a980e046SJonathan Cameron static void __exit iio_exit(void) 204a980e046SJonathan Cameron { 205a980e046SJonathan Cameron if (iio_devt) 206a980e046SJonathan Cameron unregister_chrdev_region(iio_devt, IIO_DEV_MAX); 207a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 208a980e046SJonathan Cameron debugfs_remove(iio_debugfs_dentry); 209a980e046SJonathan Cameron } 210a980e046SJonathan Cameron 211a980e046SJonathan Cameron #if defined(CONFIG_DEBUG_FS) 212a980e046SJonathan Cameron static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf, 213a980e046SJonathan Cameron size_t count, loff_t *ppos) 214a980e046SJonathan Cameron { 215a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 216a980e046SJonathan Cameron char buf[20]; 217a980e046SJonathan Cameron unsigned val = 0; 218a980e046SJonathan Cameron ssize_t len; 219a980e046SJonathan Cameron int ret; 220a980e046SJonathan Cameron 221a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, 222a980e046SJonathan Cameron indio_dev->cached_reg_addr, 223a980e046SJonathan Cameron 0, &val); 224a980e046SJonathan Cameron if (ret) 225a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__); 226a980e046SJonathan Cameron 227a980e046SJonathan Cameron len = snprintf(buf, sizeof(buf), "0x%X\n", val); 228a980e046SJonathan Cameron 229a980e046SJonathan Cameron return simple_read_from_buffer(userbuf, count, ppos, buf, len); 230a980e046SJonathan Cameron } 231a980e046SJonathan Cameron 232a980e046SJonathan Cameron static ssize_t iio_debugfs_write_reg(struct file *file, 233a980e046SJonathan Cameron const char __user *userbuf, size_t count, loff_t *ppos) 234a980e046SJonathan Cameron { 235a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 236a980e046SJonathan Cameron unsigned reg, val; 237a980e046SJonathan Cameron char buf[80]; 238a980e046SJonathan Cameron int ret; 239a980e046SJonathan Cameron 240a980e046SJonathan Cameron count = min_t(size_t, count, (sizeof(buf)-1)); 241a980e046SJonathan Cameron if (copy_from_user(buf, userbuf, count)) 242a980e046SJonathan Cameron return -EFAULT; 243a980e046SJonathan Cameron 244a980e046SJonathan Cameron buf[count] = 0; 245a980e046SJonathan Cameron 246a980e046SJonathan Cameron ret = sscanf(buf, "%i %i", ®, &val); 247a980e046SJonathan Cameron 248a980e046SJonathan Cameron switch (ret) { 249a980e046SJonathan Cameron case 1: 250a980e046SJonathan Cameron indio_dev->cached_reg_addr = reg; 251a980e046SJonathan Cameron break; 252a980e046SJonathan Cameron case 2: 253a980e046SJonathan Cameron indio_dev->cached_reg_addr = reg; 254a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, reg, 255a980e046SJonathan Cameron val, NULL); 256a980e046SJonathan Cameron if (ret) { 257a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: write failed\n", 258a980e046SJonathan Cameron __func__); 259a980e046SJonathan Cameron return ret; 260a980e046SJonathan Cameron } 261a980e046SJonathan Cameron break; 262a980e046SJonathan Cameron default: 263a980e046SJonathan Cameron return -EINVAL; 264a980e046SJonathan Cameron } 265a980e046SJonathan Cameron 266a980e046SJonathan Cameron return count; 267a980e046SJonathan Cameron } 268a980e046SJonathan Cameron 269a980e046SJonathan Cameron static const struct file_operations iio_debugfs_reg_fops = { 2705a28c873SAxel Lin .open = simple_open, 271a980e046SJonathan Cameron .read = iio_debugfs_read_reg, 272a980e046SJonathan Cameron .write = iio_debugfs_write_reg, 273a980e046SJonathan Cameron }; 274a980e046SJonathan Cameron 275a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 276a980e046SJonathan Cameron { 277a980e046SJonathan Cameron debugfs_remove_recursive(indio_dev->debugfs_dentry); 278a980e046SJonathan Cameron } 279a980e046SJonathan Cameron 280a980e046SJonathan Cameron static int iio_device_register_debugfs(struct iio_dev *indio_dev) 281a980e046SJonathan Cameron { 282a980e046SJonathan Cameron struct dentry *d; 283a980e046SJonathan Cameron 284a980e046SJonathan Cameron if (indio_dev->info->debugfs_reg_access == NULL) 285a980e046SJonathan Cameron return 0; 286a980e046SJonathan Cameron 287abd5a2fbSAxel Lin if (!iio_debugfs_dentry) 288a980e046SJonathan Cameron return 0; 289a980e046SJonathan Cameron 290a980e046SJonathan Cameron indio_dev->debugfs_dentry = 291a980e046SJonathan Cameron debugfs_create_dir(dev_name(&indio_dev->dev), 292a980e046SJonathan Cameron iio_debugfs_dentry); 293a980e046SJonathan Cameron if (indio_dev->debugfs_dentry == NULL) { 294a980e046SJonathan Cameron dev_warn(indio_dev->dev.parent, 295a980e046SJonathan Cameron "Failed to create debugfs directory\n"); 296a980e046SJonathan Cameron return -EFAULT; 297a980e046SJonathan Cameron } 298a980e046SJonathan Cameron 299a980e046SJonathan Cameron d = debugfs_create_file("direct_reg_access", 0644, 300a980e046SJonathan Cameron indio_dev->debugfs_dentry, 301a980e046SJonathan Cameron indio_dev, &iio_debugfs_reg_fops); 302a980e046SJonathan Cameron if (!d) { 303a980e046SJonathan Cameron iio_device_unregister_debugfs(indio_dev); 304a980e046SJonathan Cameron return -ENOMEM; 305a980e046SJonathan Cameron } 306a980e046SJonathan Cameron 307a980e046SJonathan Cameron return 0; 308a980e046SJonathan Cameron } 309a980e046SJonathan Cameron #else 310a980e046SJonathan Cameron static int iio_device_register_debugfs(struct iio_dev *indio_dev) 311a980e046SJonathan Cameron { 312a980e046SJonathan Cameron return 0; 313a980e046SJonathan Cameron } 314a980e046SJonathan Cameron 315a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 316a980e046SJonathan Cameron { 317a980e046SJonathan Cameron } 318a980e046SJonathan Cameron #endif /* CONFIG_DEBUG_FS */ 319a980e046SJonathan Cameron 320a980e046SJonathan Cameron static ssize_t iio_read_channel_ext_info(struct device *dev, 321a980e046SJonathan Cameron struct device_attribute *attr, 322a980e046SJonathan Cameron char *buf) 323a980e046SJonathan Cameron { 324e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 325a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 326a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 327a980e046SJonathan Cameron 328a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 329a980e046SJonathan Cameron 330fc6d1139SMichael Hennerich return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf); 331a980e046SJonathan Cameron } 332a980e046SJonathan Cameron 333a980e046SJonathan Cameron static ssize_t iio_write_channel_ext_info(struct device *dev, 334a980e046SJonathan Cameron struct device_attribute *attr, 335a980e046SJonathan Cameron const char *buf, 336a980e046SJonathan Cameron size_t len) 337a980e046SJonathan Cameron { 338e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 339a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 340a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 341a980e046SJonathan Cameron 342a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 343a980e046SJonathan Cameron 344fc6d1139SMichael Hennerich return ext_info->write(indio_dev, ext_info->private, 345fc6d1139SMichael Hennerich this_attr->c, buf, len); 346a980e046SJonathan Cameron } 347a980e046SJonathan Cameron 3485212cc8aSLars-Peter Clausen ssize_t iio_enum_available_read(struct iio_dev *indio_dev, 3495212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 3505212cc8aSLars-Peter Clausen { 3515212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3525212cc8aSLars-Peter Clausen unsigned int i; 3535212cc8aSLars-Peter Clausen size_t len = 0; 3545212cc8aSLars-Peter Clausen 3555212cc8aSLars-Peter Clausen if (!e->num_items) 3565212cc8aSLars-Peter Clausen return 0; 3575212cc8aSLars-Peter Clausen 3585212cc8aSLars-Peter Clausen for (i = 0; i < e->num_items; ++i) 35974dcd439SLars-Peter Clausen len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]); 3605212cc8aSLars-Peter Clausen 3615212cc8aSLars-Peter Clausen /* replace last space with a newline */ 3625212cc8aSLars-Peter Clausen buf[len - 1] = '\n'; 3635212cc8aSLars-Peter Clausen 3645212cc8aSLars-Peter Clausen return len; 3655212cc8aSLars-Peter Clausen } 3665212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_available_read); 3675212cc8aSLars-Peter Clausen 3685212cc8aSLars-Peter Clausen ssize_t iio_enum_read(struct iio_dev *indio_dev, 3695212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 3705212cc8aSLars-Peter Clausen { 3715212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3725212cc8aSLars-Peter Clausen int i; 3735212cc8aSLars-Peter Clausen 3745212cc8aSLars-Peter Clausen if (!e->get) 3755212cc8aSLars-Peter Clausen return -EINVAL; 3765212cc8aSLars-Peter Clausen 3775212cc8aSLars-Peter Clausen i = e->get(indio_dev, chan); 3785212cc8aSLars-Peter Clausen if (i < 0) 3795212cc8aSLars-Peter Clausen return i; 3805212cc8aSLars-Peter Clausen else if (i >= e->num_items) 3815212cc8aSLars-Peter Clausen return -EINVAL; 3825212cc8aSLars-Peter Clausen 383598db581SKees Cook return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]); 3845212cc8aSLars-Peter Clausen } 3855212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_read); 3865212cc8aSLars-Peter Clausen 3875212cc8aSLars-Peter Clausen ssize_t iio_enum_write(struct iio_dev *indio_dev, 3885212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, const char *buf, 3895212cc8aSLars-Peter Clausen size_t len) 3905212cc8aSLars-Peter Clausen { 3915212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3925212cc8aSLars-Peter Clausen unsigned int i; 3935212cc8aSLars-Peter Clausen int ret; 3945212cc8aSLars-Peter Clausen 3955212cc8aSLars-Peter Clausen if (!e->set) 3965212cc8aSLars-Peter Clausen return -EINVAL; 3975212cc8aSLars-Peter Clausen 3985212cc8aSLars-Peter Clausen for (i = 0; i < e->num_items; i++) { 3995212cc8aSLars-Peter Clausen if (sysfs_streq(buf, e->items[i])) 4005212cc8aSLars-Peter Clausen break; 4015212cc8aSLars-Peter Clausen } 4025212cc8aSLars-Peter Clausen 4035212cc8aSLars-Peter Clausen if (i == e->num_items) 4045212cc8aSLars-Peter Clausen return -EINVAL; 4055212cc8aSLars-Peter Clausen 4065212cc8aSLars-Peter Clausen ret = e->set(indio_dev, chan, i); 4075212cc8aSLars-Peter Clausen return ret ? ret : len; 4085212cc8aSLars-Peter Clausen } 4095212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_write); 4105212cc8aSLars-Peter Clausen 4113661f3f5SLars-Peter Clausen /** 4123661f3f5SLars-Peter Clausen * iio_format_value() - Formats a IIO value into its string representation 4132498dcf6SCristina Opriceana * @buf: The buffer to which the formatted value gets written 4142498dcf6SCristina Opriceana * @type: One of the IIO_VAL_... constants. This decides how the val 4152498dcf6SCristina Opriceana * and val2 parameters are formatted. 4162498dcf6SCristina Opriceana * @size: Number of IIO value entries contained in vals 4172498dcf6SCristina Opriceana * @vals: Pointer to the values, exact meaning depends on the 4182498dcf6SCristina Opriceana * type parameter. 4192498dcf6SCristina Opriceana * 4202498dcf6SCristina Opriceana * Return: 0 by default, a negative number on failure or the 4212498dcf6SCristina Opriceana * total number of characters written for a type that belongs 4222498dcf6SCristina Opriceana * to the IIO_VAL_... constant. 4233661f3f5SLars-Peter Clausen */ 4249fbfb4b3SSrinivas Pandruvada ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals) 425a980e046SJonathan Cameron { 4267985e7c1SLars-Peter Clausen unsigned long long tmp; 42767eedba3SMichael Hennerich bool scale_db = false; 428a980e046SJonathan Cameron 4293661f3f5SLars-Peter Clausen switch (type) { 43067eedba3SMichael Hennerich case IIO_VAL_INT: 4319fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d\n", vals[0]); 43267eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_MICRO_DB: 43367eedba3SMichael Hennerich scale_db = true; 43467eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_MICRO: 4359fbfb4b3SSrinivas Pandruvada if (vals[1] < 0) 4369fbfb4b3SSrinivas Pandruvada return sprintf(buf, "-%ld.%06u%s\n", abs(vals[0]), 4379fbfb4b3SSrinivas Pandruvada -vals[1], 43867eedba3SMichael Hennerich scale_db ? " dB" : ""); 439a980e046SJonathan Cameron else 4409fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1], 44167eedba3SMichael Hennerich scale_db ? " dB" : ""); 44267eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_NANO: 4439fbfb4b3SSrinivas Pandruvada if (vals[1] < 0) 4449fbfb4b3SSrinivas Pandruvada return sprintf(buf, "-%ld.%09u\n", abs(vals[0]), 4459fbfb4b3SSrinivas Pandruvada -vals[1]); 446a980e046SJonathan Cameron else 4479fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 4487985e7c1SLars-Peter Clausen case IIO_VAL_FRACTIONAL: 4499fbfb4b3SSrinivas Pandruvada tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]); 4509fbfb4b3SSrinivas Pandruvada vals[1] = do_div(tmp, 1000000000LL); 4519fbfb4b3SSrinivas Pandruvada vals[0] = tmp; 4529fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 453103d9fb9SLars-Peter Clausen case IIO_VAL_FRACTIONAL_LOG2: 4549fbfb4b3SSrinivas Pandruvada tmp = (s64)vals[0] * 1000000000LL >> vals[1]; 4559fbfb4b3SSrinivas Pandruvada vals[1] = do_div(tmp, 1000000000LL); 4569fbfb4b3SSrinivas Pandruvada vals[0] = tmp; 4579fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 4589fbfb4b3SSrinivas Pandruvada case IIO_VAL_INT_MULTIPLE: 4599fbfb4b3SSrinivas Pandruvada { 4609fbfb4b3SSrinivas Pandruvada int i; 4619fbfb4b3SSrinivas Pandruvada int len = 0; 4629fbfb4b3SSrinivas Pandruvada 4639fbfb4b3SSrinivas Pandruvada for (i = 0; i < size; ++i) 4649fbfb4b3SSrinivas Pandruvada len += snprintf(&buf[len], PAGE_SIZE - len, "%d ", 4659fbfb4b3SSrinivas Pandruvada vals[i]); 4669fbfb4b3SSrinivas Pandruvada len += snprintf(&buf[len], PAGE_SIZE - len, "\n"); 4679fbfb4b3SSrinivas Pandruvada return len; 4689fbfb4b3SSrinivas Pandruvada } 46967eedba3SMichael Hennerich default: 470a980e046SJonathan Cameron return 0; 471a980e046SJonathan Cameron } 47267eedba3SMichael Hennerich } 473*7d2c2acaSAndrew F. Davis EXPORT_SYMBOL_GPL(iio_format_value); 474a980e046SJonathan Cameron 4753661f3f5SLars-Peter Clausen static ssize_t iio_read_channel_info(struct device *dev, 4763661f3f5SLars-Peter Clausen struct device_attribute *attr, 4773661f3f5SLars-Peter Clausen char *buf) 4783661f3f5SLars-Peter Clausen { 4793661f3f5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 4803661f3f5SLars-Peter Clausen struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 4819fbfb4b3SSrinivas Pandruvada int vals[INDIO_MAX_RAW_ELEMENTS]; 4829fbfb4b3SSrinivas Pandruvada int ret; 4839fbfb4b3SSrinivas Pandruvada int val_len = 2; 4849fbfb4b3SSrinivas Pandruvada 4859fbfb4b3SSrinivas Pandruvada if (indio_dev->info->read_raw_multi) 4869fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c, 4879fbfb4b3SSrinivas Pandruvada INDIO_MAX_RAW_ELEMENTS, 4889fbfb4b3SSrinivas Pandruvada vals, &val_len, 4899fbfb4b3SSrinivas Pandruvada this_attr->address); 4909fbfb4b3SSrinivas Pandruvada else 4919fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw(indio_dev, this_attr->c, 4929fbfb4b3SSrinivas Pandruvada &vals[0], &vals[1], this_attr->address); 4933661f3f5SLars-Peter Clausen 4943661f3f5SLars-Peter Clausen if (ret < 0) 4953661f3f5SLars-Peter Clausen return ret; 4963661f3f5SLars-Peter Clausen 4979fbfb4b3SSrinivas Pandruvada return iio_format_value(buf, ret, val_len, vals); 4983661f3f5SLars-Peter Clausen } 4993661f3f5SLars-Peter Clausen 5006807d721SLars-Peter Clausen /** 5016807d721SLars-Peter Clausen * iio_str_to_fixpoint() - Parse a fixed-point number from a string 5026807d721SLars-Peter Clausen * @str: The string to parse 5036807d721SLars-Peter Clausen * @fract_mult: Multiplier for the first decimal place, should be a power of 10 5046807d721SLars-Peter Clausen * @integer: The integer part of the number 5056807d721SLars-Peter Clausen * @fract: The fractional part of the number 5066807d721SLars-Peter Clausen * 5076807d721SLars-Peter Clausen * Returns 0 on success, or a negative error code if the string could not be 5086807d721SLars-Peter Clausen * parsed. 5096807d721SLars-Peter Clausen */ 5106807d721SLars-Peter Clausen int iio_str_to_fixpoint(const char *str, int fract_mult, 5116807d721SLars-Peter Clausen int *integer, int *fract) 5126807d721SLars-Peter Clausen { 5136807d721SLars-Peter Clausen int i = 0, f = 0; 5146807d721SLars-Peter Clausen bool integer_part = true, negative = false; 5156807d721SLars-Peter Clausen 516f47dff32SSean Nyekjaer if (fract_mult == 0) { 517f47dff32SSean Nyekjaer *fract = 0; 518f47dff32SSean Nyekjaer 519f47dff32SSean Nyekjaer return kstrtoint(str, 0, integer); 520f47dff32SSean Nyekjaer } 521f47dff32SSean Nyekjaer 5226807d721SLars-Peter Clausen if (str[0] == '-') { 5236807d721SLars-Peter Clausen negative = true; 5246807d721SLars-Peter Clausen str++; 5256807d721SLars-Peter Clausen } else if (str[0] == '+') { 5266807d721SLars-Peter Clausen str++; 5276807d721SLars-Peter Clausen } 5286807d721SLars-Peter Clausen 5296807d721SLars-Peter Clausen while (*str) { 5306807d721SLars-Peter Clausen if ('0' <= *str && *str <= '9') { 5316807d721SLars-Peter Clausen if (integer_part) { 5326807d721SLars-Peter Clausen i = i * 10 + *str - '0'; 5336807d721SLars-Peter Clausen } else { 5346807d721SLars-Peter Clausen f += fract_mult * (*str - '0'); 5356807d721SLars-Peter Clausen fract_mult /= 10; 5366807d721SLars-Peter Clausen } 5376807d721SLars-Peter Clausen } else if (*str == '\n') { 5386807d721SLars-Peter Clausen if (*(str + 1) == '\0') 5396807d721SLars-Peter Clausen break; 5406807d721SLars-Peter Clausen else 5416807d721SLars-Peter Clausen return -EINVAL; 5426807d721SLars-Peter Clausen } else if (*str == '.' && integer_part) { 5436807d721SLars-Peter Clausen integer_part = false; 5446807d721SLars-Peter Clausen } else { 5456807d721SLars-Peter Clausen return -EINVAL; 5466807d721SLars-Peter Clausen } 5476807d721SLars-Peter Clausen str++; 5486807d721SLars-Peter Clausen } 5496807d721SLars-Peter Clausen 5506807d721SLars-Peter Clausen if (negative) { 5516807d721SLars-Peter Clausen if (i) 5526807d721SLars-Peter Clausen i = -i; 5536807d721SLars-Peter Clausen else 5546807d721SLars-Peter Clausen f = -f; 5556807d721SLars-Peter Clausen } 5566807d721SLars-Peter Clausen 5576807d721SLars-Peter Clausen *integer = i; 5586807d721SLars-Peter Clausen *fract = f; 5596807d721SLars-Peter Clausen 5606807d721SLars-Peter Clausen return 0; 5616807d721SLars-Peter Clausen } 5626807d721SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_str_to_fixpoint); 5636807d721SLars-Peter Clausen 564a980e046SJonathan Cameron static ssize_t iio_write_channel_info(struct device *dev, 565a980e046SJonathan Cameron struct device_attribute *attr, 566a980e046SJonathan Cameron const char *buf, 567a980e046SJonathan Cameron size_t len) 568a980e046SJonathan Cameron { 569e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 570a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 5716807d721SLars-Peter Clausen int ret, fract_mult = 100000; 5726807d721SLars-Peter Clausen int integer, fract; 573a980e046SJonathan Cameron 574a980e046SJonathan Cameron /* Assumes decimal - precision based on number of digits */ 575a980e046SJonathan Cameron if (!indio_dev->info->write_raw) 576a980e046SJonathan Cameron return -EINVAL; 577a980e046SJonathan Cameron 578a980e046SJonathan Cameron if (indio_dev->info->write_raw_get_fmt) 579a980e046SJonathan Cameron switch (indio_dev->info->write_raw_get_fmt(indio_dev, 580a980e046SJonathan Cameron this_attr->c, this_attr->address)) { 581f47dff32SSean Nyekjaer case IIO_VAL_INT: 582f47dff32SSean Nyekjaer fract_mult = 0; 583f47dff32SSean Nyekjaer break; 584a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_MICRO: 585a980e046SJonathan Cameron fract_mult = 100000; 586a980e046SJonathan Cameron break; 587a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_NANO: 588a980e046SJonathan Cameron fract_mult = 100000000; 589a980e046SJonathan Cameron break; 590a980e046SJonathan Cameron default: 591a980e046SJonathan Cameron return -EINVAL; 592a980e046SJonathan Cameron } 593a980e046SJonathan Cameron 5946807d721SLars-Peter Clausen ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract); 5956807d721SLars-Peter Clausen if (ret) 5966807d721SLars-Peter Clausen return ret; 597a980e046SJonathan Cameron 598a980e046SJonathan Cameron ret = indio_dev->info->write_raw(indio_dev, this_attr->c, 599a980e046SJonathan Cameron integer, fract, this_attr->address); 600a980e046SJonathan Cameron if (ret) 601a980e046SJonathan Cameron return ret; 602a980e046SJonathan Cameron 603a980e046SJonathan Cameron return len; 604a980e046SJonathan Cameron } 605a980e046SJonathan Cameron 606a980e046SJonathan Cameron static 607a980e046SJonathan Cameron int __iio_device_attr_init(struct device_attribute *dev_attr, 608a980e046SJonathan Cameron const char *postfix, 609a980e046SJonathan Cameron struct iio_chan_spec const *chan, 610a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 611a980e046SJonathan Cameron struct device_attribute *attr, 612a980e046SJonathan Cameron char *buf), 613a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 614a980e046SJonathan Cameron struct device_attribute *attr, 615a980e046SJonathan Cameron const char *buf, 616a980e046SJonathan Cameron size_t len), 6173704432fSJonathan Cameron enum iio_shared_by shared_by) 618a980e046SJonathan Cameron { 6193704432fSJonathan Cameron int ret = 0; 6207bbcf7e1SLars-Peter Clausen char *name = NULL; 6213704432fSJonathan Cameron char *full_postfix; 622a980e046SJonathan Cameron sysfs_attr_init(&dev_attr->attr); 623a980e046SJonathan Cameron 624a980e046SJonathan Cameron /* Build up postfix of <extend_name>_<modifier>_postfix */ 6253704432fSJonathan Cameron if (chan->modified && (shared_by == IIO_SEPARATE)) { 626a980e046SJonathan Cameron if (chan->extend_name) 627a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", 628a980e046SJonathan Cameron iio_modifier_names[chan 629a980e046SJonathan Cameron ->channel2], 630a980e046SJonathan Cameron chan->extend_name, 631a980e046SJonathan Cameron postfix); 632a980e046SJonathan Cameron else 633a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s", 634a980e046SJonathan Cameron iio_modifier_names[chan 635a980e046SJonathan Cameron ->channel2], 636a980e046SJonathan Cameron postfix); 637a980e046SJonathan Cameron } else { 63877bfa8baSLars-Peter Clausen if (chan->extend_name == NULL || shared_by != IIO_SEPARATE) 639a980e046SJonathan Cameron full_postfix = kstrdup(postfix, GFP_KERNEL); 640a980e046SJonathan Cameron else 641a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, 642a980e046SJonathan Cameron "%s_%s", 643a980e046SJonathan Cameron chan->extend_name, 644a980e046SJonathan Cameron postfix); 645a980e046SJonathan Cameron } 6463704432fSJonathan Cameron if (full_postfix == NULL) 6473704432fSJonathan Cameron return -ENOMEM; 648a980e046SJonathan Cameron 649a980e046SJonathan Cameron if (chan->differential) { /* Differential can not have modifier */ 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_%s", 661a980e046SJonathan Cameron iio_direction[chan->output], 662a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 663a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 664a980e046SJonathan Cameron full_postfix); 6653704432fSJonathan Cameron break; 6663704432fSJonathan Cameron case IIO_SEPARATE: 6673704432fSJonathan Cameron if (!chan->indexed) { 6683704432fSJonathan Cameron WARN_ON("Differential channels must be indexed\n"); 6693704432fSJonathan Cameron ret = -EINVAL; 6703704432fSJonathan Cameron goto error_free_full_postfix; 6713704432fSJonathan Cameron } 6727bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, 6733704432fSJonathan Cameron "%s_%s%d-%s%d_%s", 674a980e046SJonathan Cameron iio_direction[chan->output], 675a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 676a980e046SJonathan Cameron chan->channel, 677a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 678a980e046SJonathan Cameron chan->channel2, 679a980e046SJonathan Cameron full_postfix); 6803704432fSJonathan Cameron break; 681a980e046SJonathan Cameron } 682a980e046SJonathan Cameron } else { /* Single ended */ 6833704432fSJonathan Cameron switch (shared_by) { 684c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 6857bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 686c006ec83SJonathan Cameron break; 687c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 6887bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 689c006ec83SJonathan Cameron iio_direction[chan->output], 690c006ec83SJonathan Cameron full_postfix); 691c006ec83SJonathan Cameron break; 6923704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 6937bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 694a980e046SJonathan Cameron iio_direction[chan->output], 695a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 696a980e046SJonathan Cameron full_postfix); 6973704432fSJonathan Cameron break; 6983704432fSJonathan Cameron 6993704432fSJonathan Cameron case IIO_SEPARATE: 7003704432fSJonathan Cameron if (chan->indexed) 7017bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s%d_%s", 702a980e046SJonathan Cameron iio_direction[chan->output], 703a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 704a980e046SJonathan Cameron chan->channel, 705a980e046SJonathan Cameron full_postfix); 706a980e046SJonathan Cameron else 7077bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 708a980e046SJonathan Cameron iio_direction[chan->output], 709a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 710a980e046SJonathan Cameron full_postfix); 7113704432fSJonathan Cameron break; 7123704432fSJonathan Cameron } 713a980e046SJonathan Cameron } 7147bbcf7e1SLars-Peter Clausen if (name == NULL) { 715a980e046SJonathan Cameron ret = -ENOMEM; 716a980e046SJonathan Cameron goto error_free_full_postfix; 717a980e046SJonathan Cameron } 7187bbcf7e1SLars-Peter Clausen dev_attr->attr.name = name; 719a980e046SJonathan Cameron 720a980e046SJonathan Cameron if (readfunc) { 721a980e046SJonathan Cameron dev_attr->attr.mode |= S_IRUGO; 722a980e046SJonathan Cameron dev_attr->show = readfunc; 723a980e046SJonathan Cameron } 724a980e046SJonathan Cameron 725a980e046SJonathan Cameron if (writefunc) { 726a980e046SJonathan Cameron dev_attr->attr.mode |= S_IWUSR; 727a980e046SJonathan Cameron dev_attr->store = writefunc; 728a980e046SJonathan Cameron } 7297bbcf7e1SLars-Peter Clausen 730a980e046SJonathan Cameron error_free_full_postfix: 731a980e046SJonathan Cameron kfree(full_postfix); 7323704432fSJonathan Cameron 733a980e046SJonathan Cameron return ret; 734a980e046SJonathan Cameron } 735a980e046SJonathan Cameron 736a980e046SJonathan Cameron static void __iio_device_attr_deinit(struct device_attribute *dev_attr) 737a980e046SJonathan Cameron { 738a980e046SJonathan Cameron kfree(dev_attr->attr.name); 739a980e046SJonathan Cameron } 740a980e046SJonathan Cameron 741a980e046SJonathan Cameron int __iio_add_chan_devattr(const char *postfix, 742a980e046SJonathan Cameron struct iio_chan_spec const *chan, 743a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 744a980e046SJonathan Cameron struct device_attribute *attr, 745a980e046SJonathan Cameron char *buf), 746a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 747a980e046SJonathan Cameron struct device_attribute *attr, 748a980e046SJonathan Cameron const char *buf, 749a980e046SJonathan Cameron size_t len), 750a980e046SJonathan Cameron u64 mask, 7513704432fSJonathan Cameron enum iio_shared_by shared_by, 752a980e046SJonathan Cameron struct device *dev, 753a980e046SJonathan Cameron struct list_head *attr_list) 754a980e046SJonathan Cameron { 755a980e046SJonathan Cameron int ret; 756a980e046SJonathan Cameron struct iio_dev_attr *iio_attr, *t; 757a980e046SJonathan Cameron 758670c1103SSachin Kamat iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL); 75992825ff9SHartmut Knaack if (iio_attr == NULL) 76092825ff9SHartmut Knaack return -ENOMEM; 761a980e046SJonathan Cameron ret = __iio_device_attr_init(&iio_attr->dev_attr, 762a980e046SJonathan Cameron postfix, chan, 7633704432fSJonathan Cameron readfunc, writefunc, shared_by); 764a980e046SJonathan Cameron if (ret) 765a980e046SJonathan Cameron goto error_iio_dev_attr_free; 766a980e046SJonathan Cameron iio_attr->c = chan; 767a980e046SJonathan Cameron iio_attr->address = mask; 768a980e046SJonathan Cameron list_for_each_entry(t, attr_list, l) 769a980e046SJonathan Cameron if (strcmp(t->dev_attr.attr.name, 770a980e046SJonathan Cameron iio_attr->dev_attr.attr.name) == 0) { 7713704432fSJonathan Cameron if (shared_by == IIO_SEPARATE) 772a980e046SJonathan Cameron dev_err(dev, "tried to double register : %s\n", 773a980e046SJonathan Cameron t->dev_attr.attr.name); 774a980e046SJonathan Cameron ret = -EBUSY; 775a980e046SJonathan Cameron goto error_device_attr_deinit; 776a980e046SJonathan Cameron } 777a980e046SJonathan Cameron list_add(&iio_attr->l, attr_list); 778a980e046SJonathan Cameron 779a980e046SJonathan Cameron return 0; 780a980e046SJonathan Cameron 781a980e046SJonathan Cameron error_device_attr_deinit: 782a980e046SJonathan Cameron __iio_device_attr_deinit(&iio_attr->dev_attr); 783a980e046SJonathan Cameron error_iio_dev_attr_free: 784a980e046SJonathan Cameron kfree(iio_attr); 785a980e046SJonathan Cameron return ret; 786a980e046SJonathan Cameron } 787a980e046SJonathan Cameron 7883704432fSJonathan Cameron static int iio_device_add_info_mask_type(struct iio_dev *indio_dev, 7893704432fSJonathan Cameron struct iio_chan_spec const *chan, 7903704432fSJonathan Cameron enum iio_shared_by shared_by, 7913704432fSJonathan Cameron const long *infomask) 7923704432fSJonathan Cameron { 7933704432fSJonathan Cameron int i, ret, attrcount = 0; 7943704432fSJonathan Cameron 7953704432fSJonathan Cameron for_each_set_bit(i, infomask, sizeof(infomask)*8) { 796ef4b4856SJonathan Cameron if (i >= ARRAY_SIZE(iio_chan_info_postfix)) 797ef4b4856SJonathan Cameron return -EINVAL; 7983704432fSJonathan Cameron ret = __iio_add_chan_devattr(iio_chan_info_postfix[i], 7993704432fSJonathan Cameron chan, 8003704432fSJonathan Cameron &iio_read_channel_info, 8013704432fSJonathan Cameron &iio_write_channel_info, 8023704432fSJonathan Cameron i, 8033704432fSJonathan Cameron shared_by, 8043704432fSJonathan Cameron &indio_dev->dev, 8053704432fSJonathan Cameron &indio_dev->channel_attr_list); 8063704432fSJonathan Cameron if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) 8073704432fSJonathan Cameron continue; 8083704432fSJonathan Cameron else if (ret < 0) 8093704432fSJonathan Cameron return ret; 8103704432fSJonathan Cameron attrcount++; 8113704432fSJonathan Cameron } 8123704432fSJonathan Cameron 8133704432fSJonathan Cameron return attrcount; 8143704432fSJonathan Cameron } 8153704432fSJonathan Cameron 816a980e046SJonathan Cameron static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev, 817a980e046SJonathan Cameron struct iio_chan_spec const *chan) 818a980e046SJonathan Cameron { 819a980e046SJonathan Cameron int ret, attrcount = 0; 820a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 821a980e046SJonathan Cameron 822a980e046SJonathan Cameron if (chan->channel < 0) 823a980e046SJonathan Cameron return 0; 8243704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 8253704432fSJonathan Cameron IIO_SEPARATE, 8263704432fSJonathan Cameron &chan->info_mask_separate); 8278655cc49SJonathan Cameron if (ret < 0) 8283704432fSJonathan Cameron return ret; 8293704432fSJonathan Cameron attrcount += ret; 8303704432fSJonathan Cameron 8313704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 8323704432fSJonathan Cameron IIO_SHARED_BY_TYPE, 8333704432fSJonathan Cameron &chan->info_mask_shared_by_type); 8343704432fSJonathan Cameron if (ret < 0) 8353704432fSJonathan Cameron return ret; 8363704432fSJonathan Cameron attrcount += ret; 837a980e046SJonathan Cameron 838c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 839c006ec83SJonathan Cameron IIO_SHARED_BY_DIR, 840c006ec83SJonathan Cameron &chan->info_mask_shared_by_dir); 841c006ec83SJonathan Cameron if (ret < 0) 842c006ec83SJonathan Cameron return ret; 843c006ec83SJonathan Cameron attrcount += ret; 844c006ec83SJonathan Cameron 845c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 846c006ec83SJonathan Cameron IIO_SHARED_BY_ALL, 847c006ec83SJonathan Cameron &chan->info_mask_shared_by_all); 848c006ec83SJonathan Cameron if (ret < 0) 849c006ec83SJonathan Cameron return ret; 850c006ec83SJonathan Cameron attrcount += ret; 851c006ec83SJonathan Cameron 852a980e046SJonathan Cameron if (chan->ext_info) { 853a980e046SJonathan Cameron unsigned int i = 0; 854a980e046SJonathan Cameron for (ext_info = chan->ext_info; ext_info->name; ext_info++) { 855a980e046SJonathan Cameron ret = __iio_add_chan_devattr(ext_info->name, 856a980e046SJonathan Cameron chan, 857a980e046SJonathan Cameron ext_info->read ? 858a980e046SJonathan Cameron &iio_read_channel_ext_info : NULL, 859a980e046SJonathan Cameron ext_info->write ? 860a980e046SJonathan Cameron &iio_write_channel_ext_info : NULL, 861a980e046SJonathan Cameron i, 862a980e046SJonathan Cameron ext_info->shared, 863a980e046SJonathan Cameron &indio_dev->dev, 864a980e046SJonathan Cameron &indio_dev->channel_attr_list); 865a980e046SJonathan Cameron i++; 866a980e046SJonathan Cameron if (ret == -EBUSY && ext_info->shared) 867a980e046SJonathan Cameron continue; 868a980e046SJonathan Cameron 869a980e046SJonathan Cameron if (ret) 8703704432fSJonathan Cameron return ret; 871a980e046SJonathan Cameron 872a980e046SJonathan Cameron attrcount++; 873a980e046SJonathan Cameron } 874a980e046SJonathan Cameron } 875a980e046SJonathan Cameron 8763704432fSJonathan Cameron return attrcount; 877a980e046SJonathan Cameron } 878a980e046SJonathan Cameron 87984088ebdSLars-Peter Clausen /** 88084088ebdSLars-Peter Clausen * iio_free_chan_devattr_list() - Free a list of IIO device attributes 88184088ebdSLars-Peter Clausen * @attr_list: List of IIO device attributes 88284088ebdSLars-Peter Clausen * 88384088ebdSLars-Peter Clausen * This function frees the memory allocated for each of the IIO device 884c1b03ab5SMartin Fuzzey * attributes in the list. 88584088ebdSLars-Peter Clausen */ 88684088ebdSLars-Peter Clausen void iio_free_chan_devattr_list(struct list_head *attr_list) 887a980e046SJonathan Cameron { 88884088ebdSLars-Peter Clausen struct iio_dev_attr *p, *n; 88984088ebdSLars-Peter Clausen 89084088ebdSLars-Peter Clausen list_for_each_entry_safe(p, n, attr_list, l) { 891a980e046SJonathan Cameron kfree(p->dev_attr.attr.name); 892c1b03ab5SMartin Fuzzey list_del(&p->l); 893a980e046SJonathan Cameron kfree(p); 894a980e046SJonathan Cameron } 89584088ebdSLars-Peter Clausen } 896a980e046SJonathan Cameron 897a980e046SJonathan Cameron static ssize_t iio_show_dev_name(struct device *dev, 898a980e046SJonathan Cameron struct device_attribute *attr, 899a980e046SJonathan Cameron char *buf) 900a980e046SJonathan Cameron { 901e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 902598db581SKees Cook return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name); 903a980e046SJonathan Cameron } 904a980e046SJonathan Cameron 905a980e046SJonathan Cameron static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL); 906a980e046SJonathan Cameron 907a980e046SJonathan Cameron static int iio_device_register_sysfs(struct iio_dev *indio_dev) 908a980e046SJonathan Cameron { 909a980e046SJonathan Cameron int i, ret = 0, attrcount, attrn, attrcount_orig = 0; 91084088ebdSLars-Peter Clausen struct iio_dev_attr *p; 911a980e046SJonathan Cameron struct attribute **attr; 912a980e046SJonathan Cameron 913a980e046SJonathan Cameron /* First count elements in any existing group */ 914a980e046SJonathan Cameron if (indio_dev->info->attrs) { 915a980e046SJonathan Cameron attr = indio_dev->info->attrs->attrs; 916a980e046SJonathan Cameron while (*attr++ != NULL) 917a980e046SJonathan Cameron attrcount_orig++; 918a980e046SJonathan Cameron } 919a980e046SJonathan Cameron attrcount = attrcount_orig; 920a980e046SJonathan Cameron /* 921a980e046SJonathan Cameron * New channel registration method - relies on the fact a group does 922d25b3808SPeter Meerwald * not need to be initialized if its name is NULL. 923a980e046SJonathan Cameron */ 924a980e046SJonathan Cameron if (indio_dev->channels) 925a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) { 926a980e046SJonathan Cameron ret = iio_device_add_channel_sysfs(indio_dev, 927a980e046SJonathan Cameron &indio_dev 928a980e046SJonathan Cameron ->channels[i]); 929a980e046SJonathan Cameron if (ret < 0) 930a980e046SJonathan Cameron goto error_clear_attrs; 931a980e046SJonathan Cameron attrcount += ret; 932a980e046SJonathan Cameron } 933a980e046SJonathan Cameron 934a980e046SJonathan Cameron if (indio_dev->name) 935a980e046SJonathan Cameron attrcount++; 936a980e046SJonathan Cameron 937a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1, 938a980e046SJonathan Cameron sizeof(indio_dev->chan_attr_group.attrs[0]), 939a980e046SJonathan Cameron GFP_KERNEL); 940a980e046SJonathan Cameron if (indio_dev->chan_attr_group.attrs == NULL) { 941a980e046SJonathan Cameron ret = -ENOMEM; 942a980e046SJonathan Cameron goto error_clear_attrs; 943a980e046SJonathan Cameron } 944a980e046SJonathan Cameron /* Copy across original attributes */ 945a980e046SJonathan Cameron if (indio_dev->info->attrs) 946a980e046SJonathan Cameron memcpy(indio_dev->chan_attr_group.attrs, 947a980e046SJonathan Cameron indio_dev->info->attrs->attrs, 948a980e046SJonathan Cameron sizeof(indio_dev->chan_attr_group.attrs[0]) 949a980e046SJonathan Cameron *attrcount_orig); 950a980e046SJonathan Cameron attrn = attrcount_orig; 951a980e046SJonathan Cameron /* Add all elements from the list. */ 952a980e046SJonathan Cameron list_for_each_entry(p, &indio_dev->channel_attr_list, l) 953a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr; 954a980e046SJonathan Cameron if (indio_dev->name) 955a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr; 956a980e046SJonathan Cameron 957a980e046SJonathan Cameron indio_dev->groups[indio_dev->groupcounter++] = 958a980e046SJonathan Cameron &indio_dev->chan_attr_group; 959a980e046SJonathan Cameron 960a980e046SJonathan Cameron return 0; 961a980e046SJonathan Cameron 962a980e046SJonathan Cameron error_clear_attrs: 96384088ebdSLars-Peter Clausen iio_free_chan_devattr_list(&indio_dev->channel_attr_list); 964a980e046SJonathan Cameron 965a980e046SJonathan Cameron return ret; 966a980e046SJonathan Cameron } 967a980e046SJonathan Cameron 968a980e046SJonathan Cameron static void iio_device_unregister_sysfs(struct iio_dev *indio_dev) 969a980e046SJonathan Cameron { 970a980e046SJonathan Cameron 97184088ebdSLars-Peter Clausen iio_free_chan_devattr_list(&indio_dev->channel_attr_list); 972a980e046SJonathan Cameron kfree(indio_dev->chan_attr_group.attrs); 973c1b03ab5SMartin Fuzzey indio_dev->chan_attr_group.attrs = NULL; 974a980e046SJonathan Cameron } 975a980e046SJonathan Cameron 976a980e046SJonathan Cameron static void iio_dev_release(struct device *device) 977a980e046SJonathan Cameron { 978e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(device); 979735ad074SVladimir Barinov if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED)) 980a980e046SJonathan Cameron iio_device_unregister_trigger_consumer(indio_dev); 981a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 982a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 983e407fd65SLars-Peter Clausen 9849e69c935SLars-Peter Clausen iio_buffer_put(indio_dev->buffer); 9859e69c935SLars-Peter Clausen 986e407fd65SLars-Peter Clausen ida_simple_remove(&iio_ida, indio_dev->id); 987e407fd65SLars-Peter Clausen kfree(indio_dev); 988a980e046SJonathan Cameron } 989a980e046SJonathan Cameron 99017d82b47SGuenter Roeck struct device_type iio_device_type = { 991a980e046SJonathan Cameron .name = "iio_device", 992a980e046SJonathan Cameron .release = iio_dev_release, 993a980e046SJonathan Cameron }; 994a980e046SJonathan Cameron 995a7e57dceSSachin Kamat /** 996a7e57dceSSachin Kamat * iio_device_alloc() - allocate an iio_dev from a driver 997a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 998a7e57dceSSachin Kamat **/ 9997cbb7537SLars-Peter Clausen struct iio_dev *iio_device_alloc(int sizeof_priv) 1000a980e046SJonathan Cameron { 1001a980e046SJonathan Cameron struct iio_dev *dev; 1002a980e046SJonathan Cameron size_t alloc_size; 1003a980e046SJonathan Cameron 1004a980e046SJonathan Cameron alloc_size = sizeof(struct iio_dev); 1005a980e046SJonathan Cameron if (sizeof_priv) { 1006a980e046SJonathan Cameron alloc_size = ALIGN(alloc_size, IIO_ALIGN); 1007a980e046SJonathan Cameron alloc_size += sizeof_priv; 1008a980e046SJonathan Cameron } 1009a980e046SJonathan Cameron /* ensure 32-byte alignment of whole construct ? */ 1010a980e046SJonathan Cameron alloc_size += IIO_ALIGN - 1; 1011a980e046SJonathan Cameron 1012a980e046SJonathan Cameron dev = kzalloc(alloc_size, GFP_KERNEL); 1013a980e046SJonathan Cameron 1014a980e046SJonathan Cameron if (dev) { 1015a980e046SJonathan Cameron dev->dev.groups = dev->groups; 101617d82b47SGuenter Roeck dev->dev.type = &iio_device_type; 1017a980e046SJonathan Cameron dev->dev.bus = &iio_bus_type; 1018a980e046SJonathan Cameron device_initialize(&dev->dev); 1019a980e046SJonathan Cameron dev_set_drvdata(&dev->dev, (void *)dev); 1020a980e046SJonathan Cameron mutex_init(&dev->mlock); 1021a980e046SJonathan Cameron mutex_init(&dev->info_exist_lock); 1022e407fd65SLars-Peter Clausen INIT_LIST_HEAD(&dev->channel_attr_list); 1023a980e046SJonathan Cameron 1024a980e046SJonathan Cameron dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL); 1025a980e046SJonathan Cameron if (dev->id < 0) { 1026a980e046SJonathan Cameron /* cannot use a dev_err as the name isn't available */ 10273176dd5dSSachin Kamat pr_err("failed to get device id\n"); 1028a980e046SJonathan Cameron kfree(dev); 1029a980e046SJonathan Cameron return NULL; 1030a980e046SJonathan Cameron } 1031a980e046SJonathan Cameron dev_set_name(&dev->dev, "iio:device%d", dev->id); 103284b36ce5SJonathan Cameron INIT_LIST_HEAD(&dev->buffer_list); 1033a980e046SJonathan Cameron } 1034a980e046SJonathan Cameron 1035a980e046SJonathan Cameron return dev; 1036a980e046SJonathan Cameron } 10377cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_alloc); 1038a980e046SJonathan Cameron 1039a7e57dceSSachin Kamat /** 1040a7e57dceSSachin Kamat * iio_device_free() - free an iio_dev from a driver 1041a7e57dceSSachin Kamat * @dev: the iio_dev associated with the device 1042a7e57dceSSachin Kamat **/ 10437cbb7537SLars-Peter Clausen void iio_device_free(struct iio_dev *dev) 1044a980e046SJonathan Cameron { 1045e407fd65SLars-Peter Clausen if (dev) 1046e407fd65SLars-Peter Clausen put_device(&dev->dev); 1047a980e046SJonathan Cameron } 10487cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_free); 1049a980e046SJonathan Cameron 10509dabaf5eSGrygorii Strashko static void devm_iio_device_release(struct device *dev, void *res) 10519dabaf5eSGrygorii Strashko { 10529dabaf5eSGrygorii Strashko iio_device_free(*(struct iio_dev **)res); 10539dabaf5eSGrygorii Strashko } 10549dabaf5eSGrygorii Strashko 10559dabaf5eSGrygorii Strashko static int devm_iio_device_match(struct device *dev, void *res, void *data) 10569dabaf5eSGrygorii Strashko { 10579dabaf5eSGrygorii Strashko struct iio_dev **r = res; 10589dabaf5eSGrygorii Strashko if (!r || !*r) { 10599dabaf5eSGrygorii Strashko WARN_ON(!r || !*r); 10609dabaf5eSGrygorii Strashko return 0; 10619dabaf5eSGrygorii Strashko } 10629dabaf5eSGrygorii Strashko return *r == data; 10639dabaf5eSGrygorii Strashko } 10649dabaf5eSGrygorii Strashko 1065a7e57dceSSachin Kamat /** 1066a7e57dceSSachin Kamat * devm_iio_device_alloc - Resource-managed iio_device_alloc() 1067a7e57dceSSachin Kamat * @dev: Device to allocate iio_dev for 1068a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 1069a7e57dceSSachin Kamat * 1070a7e57dceSSachin Kamat * Managed iio_device_alloc. iio_dev allocated with this function is 1071a7e57dceSSachin Kamat * automatically freed on driver detach. 1072a7e57dceSSachin Kamat * 1073a7e57dceSSachin Kamat * If an iio_dev allocated with this function needs to be freed separately, 1074a7e57dceSSachin Kamat * devm_iio_device_free() must be used. 1075a7e57dceSSachin Kamat * 1076a7e57dceSSachin Kamat * RETURNS: 1077a7e57dceSSachin Kamat * Pointer to allocated iio_dev on success, NULL on failure. 1078a7e57dceSSachin Kamat */ 10799dabaf5eSGrygorii Strashko struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv) 10809dabaf5eSGrygorii Strashko { 10819dabaf5eSGrygorii Strashko struct iio_dev **ptr, *iio_dev; 10829dabaf5eSGrygorii Strashko 10839dabaf5eSGrygorii Strashko ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr), 10849dabaf5eSGrygorii Strashko GFP_KERNEL); 10859dabaf5eSGrygorii Strashko if (!ptr) 10869dabaf5eSGrygorii Strashko return NULL; 10879dabaf5eSGrygorii Strashko 10889dabaf5eSGrygorii Strashko iio_dev = iio_device_alloc(sizeof_priv); 10899dabaf5eSGrygorii Strashko if (iio_dev) { 10909dabaf5eSGrygorii Strashko *ptr = iio_dev; 10919dabaf5eSGrygorii Strashko devres_add(dev, ptr); 10929dabaf5eSGrygorii Strashko } else { 10939dabaf5eSGrygorii Strashko devres_free(ptr); 10949dabaf5eSGrygorii Strashko } 10959dabaf5eSGrygorii Strashko 10969dabaf5eSGrygorii Strashko return iio_dev; 10979dabaf5eSGrygorii Strashko } 10989dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_alloc); 10999dabaf5eSGrygorii Strashko 1100a7e57dceSSachin Kamat /** 1101a7e57dceSSachin Kamat * devm_iio_device_free - Resource-managed iio_device_free() 1102a7e57dceSSachin Kamat * @dev: Device this iio_dev belongs to 1103a7e57dceSSachin Kamat * @iio_dev: the iio_dev associated with the device 1104a7e57dceSSachin Kamat * 1105a7e57dceSSachin Kamat * Free iio_dev allocated with devm_iio_device_alloc(). 1106a7e57dceSSachin Kamat */ 11079dabaf5eSGrygorii Strashko void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev) 11089dabaf5eSGrygorii Strashko { 11099dabaf5eSGrygorii Strashko int rc; 11109dabaf5eSGrygorii Strashko 11119dabaf5eSGrygorii Strashko rc = devres_release(dev, devm_iio_device_release, 11129dabaf5eSGrygorii Strashko devm_iio_device_match, iio_dev); 11139dabaf5eSGrygorii Strashko WARN_ON(rc); 11149dabaf5eSGrygorii Strashko } 11159dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_free); 11169dabaf5eSGrygorii Strashko 1117a980e046SJonathan Cameron /** 1118a980e046SJonathan Cameron * iio_chrdev_open() - chrdev file open for buffer access and ioctls 11192498dcf6SCristina Opriceana * @inode: Inode structure for identifying the device in the file system 11202498dcf6SCristina Opriceana * @filp: File structure for iio device used to keep and later access 11212498dcf6SCristina Opriceana * private data 11222498dcf6SCristina Opriceana * 11232498dcf6SCristina Opriceana * Return: 0 on success or -EBUSY if the device is already opened 1124a980e046SJonathan Cameron **/ 1125a980e046SJonathan Cameron static int iio_chrdev_open(struct inode *inode, struct file *filp) 1126a980e046SJonathan Cameron { 1127a980e046SJonathan Cameron struct iio_dev *indio_dev = container_of(inode->i_cdev, 1128a980e046SJonathan Cameron struct iio_dev, chrdev); 1129a980e046SJonathan Cameron 1130a980e046SJonathan Cameron if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags)) 1131a980e046SJonathan Cameron return -EBUSY; 1132a980e046SJonathan Cameron 1133cadc2125SLars-Peter Clausen iio_device_get(indio_dev); 1134cadc2125SLars-Peter Clausen 1135a980e046SJonathan Cameron filp->private_data = indio_dev; 1136a980e046SJonathan Cameron 1137a980e046SJonathan Cameron return 0; 1138a980e046SJonathan Cameron } 1139a980e046SJonathan Cameron 1140a980e046SJonathan Cameron /** 1141a980e046SJonathan Cameron * iio_chrdev_release() - chrdev file close buffer access and ioctls 11422498dcf6SCristina Opriceana * @inode: Inode structure pointer for the char device 11432498dcf6SCristina Opriceana * @filp: File structure pointer for the char device 11442498dcf6SCristina Opriceana * 11452498dcf6SCristina Opriceana * Return: 0 for successful release 11462498dcf6SCristina Opriceana */ 1147a980e046SJonathan Cameron static int iio_chrdev_release(struct inode *inode, struct file *filp) 1148a980e046SJonathan Cameron { 1149a980e046SJonathan Cameron struct iio_dev *indio_dev = container_of(inode->i_cdev, 1150a980e046SJonathan Cameron struct iio_dev, chrdev); 1151a980e046SJonathan Cameron clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags); 1152cadc2125SLars-Peter Clausen iio_device_put(indio_dev); 1153cadc2125SLars-Peter Clausen 1154a980e046SJonathan Cameron return 0; 1155a980e046SJonathan Cameron } 1156a980e046SJonathan Cameron 1157a980e046SJonathan Cameron /* Somewhat of a cross file organization violation - ioctls here are actually 1158a980e046SJonathan Cameron * event related */ 1159a980e046SJonathan Cameron static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 1160a980e046SJonathan Cameron { 1161a980e046SJonathan Cameron struct iio_dev *indio_dev = filp->private_data; 1162a980e046SJonathan Cameron int __user *ip = (int __user *)arg; 1163a980e046SJonathan Cameron int fd; 1164a980e046SJonathan Cameron 1165f18e7a06SLars-Peter Clausen if (!indio_dev->info) 1166f18e7a06SLars-Peter Clausen return -ENODEV; 1167f18e7a06SLars-Peter Clausen 1168a980e046SJonathan Cameron if (cmd == IIO_GET_EVENT_FD_IOCTL) { 1169a980e046SJonathan Cameron fd = iio_event_getfd(indio_dev); 11703f9059b7SLinus Walleij if (fd < 0) 11713f9059b7SLinus Walleij return fd; 1172a980e046SJonathan Cameron if (copy_to_user(ip, &fd, sizeof(fd))) 1173a980e046SJonathan Cameron return -EFAULT; 1174a980e046SJonathan Cameron return 0; 1175a980e046SJonathan Cameron } 1176a980e046SJonathan Cameron return -EINVAL; 1177a980e046SJonathan Cameron } 1178a980e046SJonathan Cameron 1179a980e046SJonathan Cameron static const struct file_operations iio_buffer_fileops = { 1180a980e046SJonathan Cameron .read = iio_buffer_read_first_n_outer_addr, 1181a980e046SJonathan Cameron .release = iio_chrdev_release, 1182a980e046SJonathan Cameron .open = iio_chrdev_open, 1183a980e046SJonathan Cameron .poll = iio_buffer_poll_addr, 1184a980e046SJonathan Cameron .owner = THIS_MODULE, 1185a980e046SJonathan Cameron .llseek = noop_llseek, 1186a980e046SJonathan Cameron .unlocked_ioctl = iio_ioctl, 1187a980e046SJonathan Cameron .compat_ioctl = iio_ioctl, 1188a980e046SJonathan Cameron }; 1189a980e046SJonathan Cameron 11908f5d8727SVlad Dogaru static int iio_check_unique_scan_index(struct iio_dev *indio_dev) 11918f5d8727SVlad Dogaru { 11928f5d8727SVlad Dogaru int i, j; 11938f5d8727SVlad Dogaru const struct iio_chan_spec *channels = indio_dev->channels; 11948f5d8727SVlad Dogaru 11958f5d8727SVlad Dogaru if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES)) 11968f5d8727SVlad Dogaru return 0; 11978f5d8727SVlad Dogaru 11988f5d8727SVlad Dogaru for (i = 0; i < indio_dev->num_channels - 1; i++) { 11998f5d8727SVlad Dogaru if (channels[i].scan_index < 0) 12008f5d8727SVlad Dogaru continue; 12018f5d8727SVlad Dogaru for (j = i + 1; j < indio_dev->num_channels; j++) 12028f5d8727SVlad Dogaru if (channels[i].scan_index == channels[j].scan_index) { 12038f5d8727SVlad Dogaru dev_err(&indio_dev->dev, 12048f5d8727SVlad Dogaru "Duplicate scan index %d\n", 12058f5d8727SVlad Dogaru channels[i].scan_index); 12068f5d8727SVlad Dogaru return -EINVAL; 12078f5d8727SVlad Dogaru } 12088f5d8727SVlad Dogaru } 12098f5d8727SVlad Dogaru 12108f5d8727SVlad Dogaru return 0; 12118f5d8727SVlad Dogaru } 12128f5d8727SVlad Dogaru 1213a980e046SJonathan Cameron static const struct iio_buffer_setup_ops noop_ring_setup_ops; 1214a980e046SJonathan Cameron 1215a7e57dceSSachin Kamat /** 1216a7e57dceSSachin Kamat * iio_device_register() - register a device with the IIO subsystem 1217a7e57dceSSachin Kamat * @indio_dev: Device structure filled by the device driver 1218a7e57dceSSachin Kamat **/ 1219a980e046SJonathan Cameron int iio_device_register(struct iio_dev *indio_dev) 1220a980e046SJonathan Cameron { 1221a980e046SJonathan Cameron int ret; 1222a980e046SJonathan Cameron 122317d82b47SGuenter Roeck /* If the calling driver did not initialize of_node, do it here */ 122417d82b47SGuenter Roeck if (!indio_dev->dev.of_node && indio_dev->dev.parent) 122517d82b47SGuenter Roeck indio_dev->dev.of_node = indio_dev->dev.parent->of_node; 122617d82b47SGuenter Roeck 12278f5d8727SVlad Dogaru ret = iio_check_unique_scan_index(indio_dev); 12288f5d8727SVlad Dogaru if (ret < 0) 12298f5d8727SVlad Dogaru return ret; 12308f5d8727SVlad Dogaru 1231a980e046SJonathan Cameron /* configure elements for the chrdev */ 1232a980e046SJonathan Cameron indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id); 1233a980e046SJonathan Cameron 1234a980e046SJonathan Cameron ret = iio_device_register_debugfs(indio_dev); 1235a980e046SJonathan Cameron if (ret) { 1236a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1237a980e046SJonathan Cameron "Failed to register debugfs interfaces\n"); 123892825ff9SHartmut Knaack return ret; 1239a980e046SJonathan Cameron } 12403e1b6c95SLars-Peter Clausen 12413e1b6c95SLars-Peter Clausen ret = iio_buffer_alloc_sysfs_and_mask(indio_dev); 12423e1b6c95SLars-Peter Clausen if (ret) { 12433e1b6c95SLars-Peter Clausen dev_err(indio_dev->dev.parent, 12443e1b6c95SLars-Peter Clausen "Failed to create buffer sysfs interfaces\n"); 12453e1b6c95SLars-Peter Clausen goto error_unreg_debugfs; 12463e1b6c95SLars-Peter Clausen } 12473e1b6c95SLars-Peter Clausen 1248a980e046SJonathan Cameron ret = iio_device_register_sysfs(indio_dev); 1249a980e046SJonathan Cameron if (ret) { 1250a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1251a980e046SJonathan Cameron "Failed to register sysfs interfaces\n"); 12523e1b6c95SLars-Peter Clausen goto error_buffer_free_sysfs; 1253a980e046SJonathan Cameron } 1254a980e046SJonathan Cameron ret = iio_device_register_eventset(indio_dev); 1255a980e046SJonathan Cameron if (ret) { 1256a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1257a980e046SJonathan Cameron "Failed to register event set\n"); 1258a980e046SJonathan Cameron goto error_free_sysfs; 1259a980e046SJonathan Cameron } 1260735ad074SVladimir Barinov if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED)) 1261a980e046SJonathan Cameron iio_device_register_trigger_consumer(indio_dev); 1262a980e046SJonathan Cameron 1263a980e046SJonathan Cameron if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) && 1264a980e046SJonathan Cameron indio_dev->setup_ops == NULL) 1265a980e046SJonathan Cameron indio_dev->setup_ops = &noop_ring_setup_ops; 1266a980e046SJonathan Cameron 1267a980e046SJonathan Cameron cdev_init(&indio_dev->chrdev, &iio_buffer_fileops); 1268a980e046SJonathan Cameron indio_dev->chrdev.owner = indio_dev->info->driver_module; 12690d5b7daeSLars-Peter Clausen indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj; 1270a980e046SJonathan Cameron ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1); 1271a980e046SJonathan Cameron if (ret < 0) 12720d5b7daeSLars-Peter Clausen goto error_unreg_eventset; 1273a980e046SJonathan Cameron 12740d5b7daeSLars-Peter Clausen ret = device_add(&indio_dev->dev); 12750d5b7daeSLars-Peter Clausen if (ret < 0) 12760d5b7daeSLars-Peter Clausen goto error_cdev_del; 12770d5b7daeSLars-Peter Clausen 12780d5b7daeSLars-Peter Clausen return 0; 12790d5b7daeSLars-Peter Clausen error_cdev_del: 12800d5b7daeSLars-Peter Clausen cdev_del(&indio_dev->chrdev); 1281a980e046SJonathan Cameron error_unreg_eventset: 1282a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 1283a980e046SJonathan Cameron error_free_sysfs: 1284a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 12853e1b6c95SLars-Peter Clausen error_buffer_free_sysfs: 12863e1b6c95SLars-Peter Clausen iio_buffer_free_sysfs_and_mask(indio_dev); 1287a980e046SJonathan Cameron error_unreg_debugfs: 1288a980e046SJonathan Cameron iio_device_unregister_debugfs(indio_dev); 1289a980e046SJonathan Cameron return ret; 1290a980e046SJonathan Cameron } 1291a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_register); 1292a980e046SJonathan Cameron 1293a7e57dceSSachin Kamat /** 1294a7e57dceSSachin Kamat * iio_device_unregister() - unregister a device from the IIO subsystem 1295a7e57dceSSachin Kamat * @indio_dev: Device structure representing the device. 1296a7e57dceSSachin Kamat **/ 1297a980e046SJonathan Cameron void iio_device_unregister(struct iio_dev *indio_dev) 1298a980e046SJonathan Cameron { 1299a980e046SJonathan Cameron mutex_lock(&indio_dev->info_exist_lock); 1300a87c82e4SLars-Peter Clausen 1301a87c82e4SLars-Peter Clausen device_del(&indio_dev->dev); 1302a87c82e4SLars-Peter Clausen 13030d5b7daeSLars-Peter Clausen if (indio_dev->chrdev.dev) 13040d5b7daeSLars-Peter Clausen cdev_del(&indio_dev->chrdev); 1305bc4c9612SLars-Peter Clausen iio_device_unregister_debugfs(indio_dev); 13060d5b7daeSLars-Peter Clausen 1307a87c82e4SLars-Peter Clausen iio_disable_all_buffers(indio_dev); 1308a87c82e4SLars-Peter Clausen 1309a980e046SJonathan Cameron indio_dev->info = NULL; 1310d2f0a48fSLars-Peter Clausen 1311d2f0a48fSLars-Peter Clausen iio_device_wakeup_eventset(indio_dev); 1312d2f0a48fSLars-Peter Clausen iio_buffer_wakeup_poll(indio_dev); 1313d2f0a48fSLars-Peter Clausen 1314a980e046SJonathan Cameron mutex_unlock(&indio_dev->info_exist_lock); 13153e1b6c95SLars-Peter Clausen 13163e1b6c95SLars-Peter Clausen iio_buffer_free_sysfs_and_mask(indio_dev); 1317a980e046SJonathan Cameron } 1318a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_unregister); 13198caa07c0SSachin Kamat 13208caa07c0SSachin Kamat static void devm_iio_device_unreg(struct device *dev, void *res) 13218caa07c0SSachin Kamat { 13228caa07c0SSachin Kamat iio_device_unregister(*(struct iio_dev **)res); 13238caa07c0SSachin Kamat } 13248caa07c0SSachin Kamat 13258caa07c0SSachin Kamat /** 13268caa07c0SSachin Kamat * devm_iio_device_register - Resource-managed iio_device_register() 13278caa07c0SSachin Kamat * @dev: Device to allocate iio_dev for 13288caa07c0SSachin Kamat * @indio_dev: Device structure filled by the device driver 13298caa07c0SSachin Kamat * 13308caa07c0SSachin Kamat * Managed iio_device_register. The IIO device registered with this 13318caa07c0SSachin Kamat * function is automatically unregistered on driver detach. This function 13328caa07c0SSachin Kamat * calls iio_device_register() internally. Refer to that function for more 13338caa07c0SSachin Kamat * information. 13348caa07c0SSachin Kamat * 13358caa07c0SSachin Kamat * If an iio_dev registered with this function needs to be unregistered 13368caa07c0SSachin Kamat * separately, devm_iio_device_unregister() must be used. 13378caa07c0SSachin Kamat * 13388caa07c0SSachin Kamat * RETURNS: 13398caa07c0SSachin Kamat * 0 on success, negative error number on failure. 13408caa07c0SSachin Kamat */ 13418caa07c0SSachin Kamat int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev) 13428caa07c0SSachin Kamat { 13438caa07c0SSachin Kamat struct iio_dev **ptr; 13448caa07c0SSachin Kamat int ret; 13458caa07c0SSachin Kamat 13468caa07c0SSachin Kamat ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL); 13478caa07c0SSachin Kamat if (!ptr) 13488caa07c0SSachin Kamat return -ENOMEM; 13498caa07c0SSachin Kamat 13508caa07c0SSachin Kamat *ptr = indio_dev; 13518caa07c0SSachin Kamat ret = iio_device_register(indio_dev); 13528caa07c0SSachin Kamat if (!ret) 13538caa07c0SSachin Kamat devres_add(dev, ptr); 13548caa07c0SSachin Kamat else 13558caa07c0SSachin Kamat devres_free(ptr); 13568caa07c0SSachin Kamat 13578caa07c0SSachin Kamat return ret; 13588caa07c0SSachin Kamat } 13598caa07c0SSachin Kamat EXPORT_SYMBOL_GPL(devm_iio_device_register); 13608caa07c0SSachin Kamat 13618caa07c0SSachin Kamat /** 13628caa07c0SSachin Kamat * devm_iio_device_unregister - Resource-managed iio_device_unregister() 13638caa07c0SSachin Kamat * @dev: Device this iio_dev belongs to 13648caa07c0SSachin Kamat * @indio_dev: the iio_dev associated with the device 13658caa07c0SSachin Kamat * 13668caa07c0SSachin Kamat * Unregister iio_dev registered with devm_iio_device_register(). 13678caa07c0SSachin Kamat */ 13688caa07c0SSachin Kamat void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev) 13698caa07c0SSachin Kamat { 13708caa07c0SSachin Kamat int rc; 13718caa07c0SSachin Kamat 13728caa07c0SSachin Kamat rc = devres_release(dev, devm_iio_device_unreg, 13738caa07c0SSachin Kamat devm_iio_device_match, indio_dev); 13748caa07c0SSachin Kamat WARN_ON(rc); 13758caa07c0SSachin Kamat } 13768caa07c0SSachin Kamat EXPORT_SYMBOL_GPL(devm_iio_device_unregister); 13778caa07c0SSachin Kamat 1378a980e046SJonathan Cameron subsys_initcall(iio_init); 1379a980e046SJonathan Cameron module_exit(iio_exit); 1380a980e046SJonathan Cameron 1381c8b95952SJonathan Cameron MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); 1382a980e046SJonathan Cameron MODULE_DESCRIPTION("Industrial I/O core"); 1383a980e046SJonathan Cameron MODULE_LICENSE("GPL"); 1384