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", 844b8d8015SPeter Meerwald [IIO_MOD_X_AND_Y] = "x&y", 854b8d8015SPeter Meerwald [IIO_MOD_X_AND_Z] = "x&z", 864b8d8015SPeter Meerwald [IIO_MOD_Y_AND_Z] = "y&z", 874b8d8015SPeter Meerwald [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z", 884b8d8015SPeter Meerwald [IIO_MOD_X_OR_Y] = "x|y", 894b8d8015SPeter Meerwald [IIO_MOD_X_OR_Z] = "x|z", 904b8d8015SPeter Meerwald [IIO_MOD_Y_OR_Z] = "y|z", 914b8d8015SPeter Meerwald [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z", 928f5879b2SJonathan Cameron [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)", 93cf82cb81SJonathan Cameron [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2", 94a980e046SJonathan Cameron [IIO_MOD_LIGHT_BOTH] = "both", 95a980e046SJonathan Cameron [IIO_MOD_LIGHT_IR] = "ir", 9621cd1fabSJon Brenner [IIO_MOD_LIGHT_CLEAR] = "clear", 9721cd1fabSJon Brenner [IIO_MOD_LIGHT_RED] = "red", 9821cd1fabSJon Brenner [IIO_MOD_LIGHT_GREEN] = "green", 9921cd1fabSJon Brenner [IIO_MOD_LIGHT_BLUE] = "blue", 1005082f405SSrinivas Pandruvada [IIO_MOD_QUATERNION] = "quaternion", 101638b43b3SPeter Meerwald [IIO_MOD_TEMP_AMBIENT] = "ambient", 102638b43b3SPeter Meerwald [IIO_MOD_TEMP_OBJECT] = "object", 10311b8ddabSReyad Attiyat [IIO_MOD_NORTH_MAGN] = "from_north_magnetic", 10411b8ddabSReyad Attiyat [IIO_MOD_NORTH_TRUE] = "from_north_true", 10511b8ddabSReyad Attiyat [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp", 10611b8ddabSReyad Attiyat [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp", 10755aebeb9SDaniel Baluta [IIO_MOD_RUNNING] = "running", 10855aebeb9SDaniel Baluta [IIO_MOD_JOGGING] = "jogging", 10955aebeb9SDaniel Baluta [IIO_MOD_WALKING] = "walking", 11055aebeb9SDaniel Baluta [IIO_MOD_STILL] = "still", 1115a1a9329SIrina Tirdea [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)", 1121ce87f21SLars-Peter Clausen [IIO_MOD_I] = "i", 1131ce87f21SLars-Peter Clausen [IIO_MOD_Q] = "q", 114a980e046SJonathan Cameron }; 115a980e046SJonathan Cameron 116a980e046SJonathan Cameron /* relies on pairs of these shared then separate */ 117a980e046SJonathan Cameron static const char * const iio_chan_info_postfix[] = { 118a980e046SJonathan Cameron [IIO_CHAN_INFO_RAW] = "raw", 119a980e046SJonathan Cameron [IIO_CHAN_INFO_PROCESSED] = "input", 120a980e046SJonathan Cameron [IIO_CHAN_INFO_SCALE] = "scale", 121a980e046SJonathan Cameron [IIO_CHAN_INFO_OFFSET] = "offset", 122a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBSCALE] = "calibscale", 123a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBBIAS] = "calibbias", 124a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK] = "peak_raw", 125a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale", 126a980e046SJonathan Cameron [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw", 127a980e046SJonathan Cameron [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw", 128a980e046SJonathan Cameron [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY] 129a980e046SJonathan Cameron = "filter_low_pass_3db_frequency", 1303f7f642bSMartin Fuzzey [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY] 1313f7f642bSMartin Fuzzey = "filter_high_pass_3db_frequency", 132a980e046SJonathan Cameron [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency", 133a6b12855SMichael Hennerich [IIO_CHAN_INFO_FREQUENCY] = "frequency", 134a6b12855SMichael Hennerich [IIO_CHAN_INFO_PHASE] = "phase", 135b65d6212SMichael Hennerich [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain", 1367c9ab035Ssrinivas pandruvada [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis", 137899d90bdSPeter Meerwald [IIO_CHAN_INFO_INT_TIME] = "integration_time", 138a88bfe78SIrina Tirdea [IIO_CHAN_INFO_ENABLE] = "en", 139bcdf28fbSIrina Tirdea [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight", 140d37f6836SIrina Tirdea [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight", 1412f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count", 1422f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time", 143c8a85854SVianney le Clément de Saint-Marcq [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity", 144faaa4495SIrina Tirdea [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio", 145a980e046SJonathan Cameron }; 146a980e046SJonathan Cameron 147a7e57dceSSachin Kamat /** 148a7e57dceSSachin Kamat * iio_find_channel_from_si() - get channel from its scan index 149a7e57dceSSachin Kamat * @indio_dev: device 150a7e57dceSSachin Kamat * @si: scan index to match 151a7e57dceSSachin Kamat */ 152a980e046SJonathan Cameron const struct iio_chan_spec 153a980e046SJonathan Cameron *iio_find_channel_from_si(struct iio_dev *indio_dev, int si) 154a980e046SJonathan Cameron { 155a980e046SJonathan Cameron int i; 156a980e046SJonathan Cameron 157a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) 158a980e046SJonathan Cameron if (indio_dev->channels[i].scan_index == si) 159a980e046SJonathan Cameron return &indio_dev->channels[i]; 160a980e046SJonathan Cameron return NULL; 161a980e046SJonathan Cameron } 162a980e046SJonathan Cameron 163a980e046SJonathan Cameron /* This turns up an awful lot */ 164a980e046SJonathan Cameron ssize_t iio_read_const_attr(struct device *dev, 165a980e046SJonathan Cameron struct device_attribute *attr, 166a980e046SJonathan Cameron char *buf) 167a980e046SJonathan Cameron { 168a980e046SJonathan Cameron return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string); 169a980e046SJonathan Cameron } 170a980e046SJonathan Cameron EXPORT_SYMBOL(iio_read_const_attr); 171a980e046SJonathan Cameron 172a980e046SJonathan Cameron static int __init iio_init(void) 173a980e046SJonathan Cameron { 174a980e046SJonathan Cameron int ret; 175a980e046SJonathan Cameron 176a980e046SJonathan Cameron /* Register sysfs bus */ 177a980e046SJonathan Cameron ret = bus_register(&iio_bus_type); 178a980e046SJonathan Cameron if (ret < 0) { 1793176dd5dSSachin Kamat pr_err("could not register bus type\n"); 180a980e046SJonathan Cameron goto error_nothing; 181a980e046SJonathan Cameron } 182a980e046SJonathan Cameron 183a980e046SJonathan Cameron ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio"); 184a980e046SJonathan Cameron if (ret < 0) { 1853176dd5dSSachin Kamat pr_err("failed to allocate char dev region\n"); 186a980e046SJonathan Cameron goto error_unregister_bus_type; 187a980e046SJonathan Cameron } 188a980e046SJonathan Cameron 189a980e046SJonathan Cameron iio_debugfs_dentry = debugfs_create_dir("iio", NULL); 190a980e046SJonathan Cameron 191a980e046SJonathan Cameron return 0; 192a980e046SJonathan Cameron 193a980e046SJonathan Cameron error_unregister_bus_type: 194a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 195a980e046SJonathan Cameron error_nothing: 196a980e046SJonathan Cameron return ret; 197a980e046SJonathan Cameron } 198a980e046SJonathan Cameron 199a980e046SJonathan Cameron static void __exit iio_exit(void) 200a980e046SJonathan Cameron { 201a980e046SJonathan Cameron if (iio_devt) 202a980e046SJonathan Cameron unregister_chrdev_region(iio_devt, IIO_DEV_MAX); 203a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 204a980e046SJonathan Cameron debugfs_remove(iio_debugfs_dentry); 205a980e046SJonathan Cameron } 206a980e046SJonathan Cameron 207a980e046SJonathan Cameron #if defined(CONFIG_DEBUG_FS) 208a980e046SJonathan Cameron static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf, 209a980e046SJonathan Cameron size_t count, loff_t *ppos) 210a980e046SJonathan Cameron { 211a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 212a980e046SJonathan Cameron char buf[20]; 213a980e046SJonathan Cameron unsigned val = 0; 214a980e046SJonathan Cameron ssize_t len; 215a980e046SJonathan Cameron int ret; 216a980e046SJonathan Cameron 217a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, 218a980e046SJonathan Cameron indio_dev->cached_reg_addr, 219a980e046SJonathan Cameron 0, &val); 220a980e046SJonathan Cameron if (ret) 221a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__); 222a980e046SJonathan Cameron 223a980e046SJonathan Cameron len = snprintf(buf, sizeof(buf), "0x%X\n", val); 224a980e046SJonathan Cameron 225a980e046SJonathan Cameron return simple_read_from_buffer(userbuf, count, ppos, buf, len); 226a980e046SJonathan Cameron } 227a980e046SJonathan Cameron 228a980e046SJonathan Cameron static ssize_t iio_debugfs_write_reg(struct file *file, 229a980e046SJonathan Cameron const char __user *userbuf, size_t count, loff_t *ppos) 230a980e046SJonathan Cameron { 231a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 232a980e046SJonathan Cameron unsigned reg, val; 233a980e046SJonathan Cameron char buf[80]; 234a980e046SJonathan Cameron int ret; 235a980e046SJonathan Cameron 236a980e046SJonathan Cameron count = min_t(size_t, count, (sizeof(buf)-1)); 237a980e046SJonathan Cameron if (copy_from_user(buf, userbuf, count)) 238a980e046SJonathan Cameron return -EFAULT; 239a980e046SJonathan Cameron 240a980e046SJonathan Cameron buf[count] = 0; 241a980e046SJonathan Cameron 242a980e046SJonathan Cameron ret = sscanf(buf, "%i %i", ®, &val); 243a980e046SJonathan Cameron 244a980e046SJonathan Cameron switch (ret) { 245a980e046SJonathan Cameron case 1: 246a980e046SJonathan Cameron indio_dev->cached_reg_addr = reg; 247a980e046SJonathan Cameron break; 248a980e046SJonathan Cameron case 2: 249a980e046SJonathan Cameron indio_dev->cached_reg_addr = reg; 250a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, reg, 251a980e046SJonathan Cameron val, NULL); 252a980e046SJonathan Cameron if (ret) { 253a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: write failed\n", 254a980e046SJonathan Cameron __func__); 255a980e046SJonathan Cameron return ret; 256a980e046SJonathan Cameron } 257a980e046SJonathan Cameron break; 258a980e046SJonathan Cameron default: 259a980e046SJonathan Cameron return -EINVAL; 260a980e046SJonathan Cameron } 261a980e046SJonathan Cameron 262a980e046SJonathan Cameron return count; 263a980e046SJonathan Cameron } 264a980e046SJonathan Cameron 265a980e046SJonathan Cameron static const struct file_operations iio_debugfs_reg_fops = { 2665a28c873SAxel Lin .open = simple_open, 267a980e046SJonathan Cameron .read = iio_debugfs_read_reg, 268a980e046SJonathan Cameron .write = iio_debugfs_write_reg, 269a980e046SJonathan Cameron }; 270a980e046SJonathan Cameron 271a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 272a980e046SJonathan Cameron { 273a980e046SJonathan Cameron debugfs_remove_recursive(indio_dev->debugfs_dentry); 274a980e046SJonathan Cameron } 275a980e046SJonathan Cameron 276a980e046SJonathan Cameron static int iio_device_register_debugfs(struct iio_dev *indio_dev) 277a980e046SJonathan Cameron { 278a980e046SJonathan Cameron struct dentry *d; 279a980e046SJonathan Cameron 280a980e046SJonathan Cameron if (indio_dev->info->debugfs_reg_access == NULL) 281a980e046SJonathan Cameron return 0; 282a980e046SJonathan Cameron 283abd5a2fbSAxel Lin if (!iio_debugfs_dentry) 284a980e046SJonathan Cameron return 0; 285a980e046SJonathan Cameron 286a980e046SJonathan Cameron indio_dev->debugfs_dentry = 287a980e046SJonathan Cameron debugfs_create_dir(dev_name(&indio_dev->dev), 288a980e046SJonathan Cameron iio_debugfs_dentry); 289a980e046SJonathan Cameron if (indio_dev->debugfs_dentry == NULL) { 290a980e046SJonathan Cameron dev_warn(indio_dev->dev.parent, 291a980e046SJonathan Cameron "Failed to create debugfs directory\n"); 292a980e046SJonathan Cameron return -EFAULT; 293a980e046SJonathan Cameron } 294a980e046SJonathan Cameron 295a980e046SJonathan Cameron d = debugfs_create_file("direct_reg_access", 0644, 296a980e046SJonathan Cameron indio_dev->debugfs_dentry, 297a980e046SJonathan Cameron indio_dev, &iio_debugfs_reg_fops); 298a980e046SJonathan Cameron if (!d) { 299a980e046SJonathan Cameron iio_device_unregister_debugfs(indio_dev); 300a980e046SJonathan Cameron return -ENOMEM; 301a980e046SJonathan Cameron } 302a980e046SJonathan Cameron 303a980e046SJonathan Cameron return 0; 304a980e046SJonathan Cameron } 305a980e046SJonathan Cameron #else 306a980e046SJonathan Cameron static int iio_device_register_debugfs(struct iio_dev *indio_dev) 307a980e046SJonathan Cameron { 308a980e046SJonathan Cameron return 0; 309a980e046SJonathan Cameron } 310a980e046SJonathan Cameron 311a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 312a980e046SJonathan Cameron { 313a980e046SJonathan Cameron } 314a980e046SJonathan Cameron #endif /* CONFIG_DEBUG_FS */ 315a980e046SJonathan Cameron 316a980e046SJonathan Cameron static ssize_t iio_read_channel_ext_info(struct device *dev, 317a980e046SJonathan Cameron struct device_attribute *attr, 318a980e046SJonathan Cameron char *buf) 319a980e046SJonathan Cameron { 320e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 321a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 322a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 323a980e046SJonathan Cameron 324a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 325a980e046SJonathan Cameron 326fc6d1139SMichael Hennerich return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf); 327a980e046SJonathan Cameron } 328a980e046SJonathan Cameron 329a980e046SJonathan Cameron static ssize_t iio_write_channel_ext_info(struct device *dev, 330a980e046SJonathan Cameron struct device_attribute *attr, 331a980e046SJonathan Cameron const char *buf, 332a980e046SJonathan Cameron size_t len) 333a980e046SJonathan Cameron { 334e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 335a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 336a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 337a980e046SJonathan Cameron 338a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 339a980e046SJonathan Cameron 340fc6d1139SMichael Hennerich return ext_info->write(indio_dev, ext_info->private, 341fc6d1139SMichael Hennerich this_attr->c, buf, len); 342a980e046SJonathan Cameron } 343a980e046SJonathan Cameron 3445212cc8aSLars-Peter Clausen ssize_t iio_enum_available_read(struct iio_dev *indio_dev, 3455212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 3465212cc8aSLars-Peter Clausen { 3475212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3485212cc8aSLars-Peter Clausen unsigned int i; 3495212cc8aSLars-Peter Clausen size_t len = 0; 3505212cc8aSLars-Peter Clausen 3515212cc8aSLars-Peter Clausen if (!e->num_items) 3525212cc8aSLars-Peter Clausen return 0; 3535212cc8aSLars-Peter Clausen 3545212cc8aSLars-Peter Clausen for (i = 0; i < e->num_items; ++i) 35574dcd439SLars-Peter Clausen len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]); 3565212cc8aSLars-Peter Clausen 3575212cc8aSLars-Peter Clausen /* replace last space with a newline */ 3585212cc8aSLars-Peter Clausen buf[len - 1] = '\n'; 3595212cc8aSLars-Peter Clausen 3605212cc8aSLars-Peter Clausen return len; 3615212cc8aSLars-Peter Clausen } 3625212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_available_read); 3635212cc8aSLars-Peter Clausen 3645212cc8aSLars-Peter Clausen ssize_t iio_enum_read(struct iio_dev *indio_dev, 3655212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 3665212cc8aSLars-Peter Clausen { 3675212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3685212cc8aSLars-Peter Clausen int i; 3695212cc8aSLars-Peter Clausen 3705212cc8aSLars-Peter Clausen if (!e->get) 3715212cc8aSLars-Peter Clausen return -EINVAL; 3725212cc8aSLars-Peter Clausen 3735212cc8aSLars-Peter Clausen i = e->get(indio_dev, chan); 3745212cc8aSLars-Peter Clausen if (i < 0) 3755212cc8aSLars-Peter Clausen return i; 3765212cc8aSLars-Peter Clausen else if (i >= e->num_items) 3775212cc8aSLars-Peter Clausen return -EINVAL; 3785212cc8aSLars-Peter Clausen 379598db581SKees Cook return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]); 3805212cc8aSLars-Peter Clausen } 3815212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_read); 3825212cc8aSLars-Peter Clausen 3835212cc8aSLars-Peter Clausen ssize_t iio_enum_write(struct iio_dev *indio_dev, 3845212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, const char *buf, 3855212cc8aSLars-Peter Clausen size_t len) 3865212cc8aSLars-Peter Clausen { 3875212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3885212cc8aSLars-Peter Clausen unsigned int i; 3895212cc8aSLars-Peter Clausen int ret; 3905212cc8aSLars-Peter Clausen 3915212cc8aSLars-Peter Clausen if (!e->set) 3925212cc8aSLars-Peter Clausen return -EINVAL; 3935212cc8aSLars-Peter Clausen 3945212cc8aSLars-Peter Clausen for (i = 0; i < e->num_items; i++) { 3955212cc8aSLars-Peter Clausen if (sysfs_streq(buf, e->items[i])) 3965212cc8aSLars-Peter Clausen break; 3975212cc8aSLars-Peter Clausen } 3985212cc8aSLars-Peter Clausen 3995212cc8aSLars-Peter Clausen if (i == e->num_items) 4005212cc8aSLars-Peter Clausen return -EINVAL; 4015212cc8aSLars-Peter Clausen 4025212cc8aSLars-Peter Clausen ret = e->set(indio_dev, chan, i); 4035212cc8aSLars-Peter Clausen return ret ? ret : len; 4045212cc8aSLars-Peter Clausen } 4055212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_write); 4065212cc8aSLars-Peter Clausen 4073661f3f5SLars-Peter Clausen /** 4083661f3f5SLars-Peter Clausen * iio_format_value() - Formats a IIO value into its string representation 4092498dcf6SCristina Opriceana * @buf: The buffer to which the formatted value gets written 4102498dcf6SCristina Opriceana * @type: One of the IIO_VAL_... constants. This decides how the val 4112498dcf6SCristina Opriceana * and val2 parameters are formatted. 4122498dcf6SCristina Opriceana * @size: Number of IIO value entries contained in vals 4132498dcf6SCristina Opriceana * @vals: Pointer to the values, exact meaning depends on the 4142498dcf6SCristina Opriceana * type parameter. 4152498dcf6SCristina Opriceana * 4162498dcf6SCristina Opriceana * Return: 0 by default, a negative number on failure or the 4172498dcf6SCristina Opriceana * total number of characters written for a type that belongs 4182498dcf6SCristina Opriceana * to the IIO_VAL_... constant. 4193661f3f5SLars-Peter Clausen */ 4209fbfb4b3SSrinivas Pandruvada ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals) 421a980e046SJonathan Cameron { 4227985e7c1SLars-Peter Clausen unsigned long long tmp; 42367eedba3SMichael Hennerich bool scale_db = false; 424a980e046SJonathan Cameron 4253661f3f5SLars-Peter Clausen switch (type) { 42667eedba3SMichael Hennerich case IIO_VAL_INT: 4279fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d\n", vals[0]); 42867eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_MICRO_DB: 42967eedba3SMichael Hennerich scale_db = true; 43067eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_MICRO: 4319fbfb4b3SSrinivas Pandruvada if (vals[1] < 0) 4329fbfb4b3SSrinivas Pandruvada return sprintf(buf, "-%ld.%06u%s\n", abs(vals[0]), 4339fbfb4b3SSrinivas Pandruvada -vals[1], 43467eedba3SMichael Hennerich scale_db ? " dB" : ""); 435a980e046SJonathan Cameron else 4369fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1], 43767eedba3SMichael Hennerich scale_db ? " dB" : ""); 43867eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_NANO: 4399fbfb4b3SSrinivas Pandruvada if (vals[1] < 0) 4409fbfb4b3SSrinivas Pandruvada return sprintf(buf, "-%ld.%09u\n", abs(vals[0]), 4419fbfb4b3SSrinivas Pandruvada -vals[1]); 442a980e046SJonathan Cameron else 4439fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 4447985e7c1SLars-Peter Clausen case IIO_VAL_FRACTIONAL: 4459fbfb4b3SSrinivas Pandruvada tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]); 4469fbfb4b3SSrinivas Pandruvada vals[1] = do_div(tmp, 1000000000LL); 4479fbfb4b3SSrinivas Pandruvada vals[0] = tmp; 4489fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 449103d9fb9SLars-Peter Clausen case IIO_VAL_FRACTIONAL_LOG2: 4509fbfb4b3SSrinivas Pandruvada tmp = (s64)vals[0] * 1000000000LL >> vals[1]; 4519fbfb4b3SSrinivas Pandruvada vals[1] = do_div(tmp, 1000000000LL); 4529fbfb4b3SSrinivas Pandruvada vals[0] = tmp; 4539fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 4549fbfb4b3SSrinivas Pandruvada case IIO_VAL_INT_MULTIPLE: 4559fbfb4b3SSrinivas Pandruvada { 4569fbfb4b3SSrinivas Pandruvada int i; 4579fbfb4b3SSrinivas Pandruvada int len = 0; 4589fbfb4b3SSrinivas Pandruvada 4599fbfb4b3SSrinivas Pandruvada for (i = 0; i < size; ++i) 4609fbfb4b3SSrinivas Pandruvada len += snprintf(&buf[len], PAGE_SIZE - len, "%d ", 4619fbfb4b3SSrinivas Pandruvada vals[i]); 4629fbfb4b3SSrinivas Pandruvada len += snprintf(&buf[len], PAGE_SIZE - len, "\n"); 4639fbfb4b3SSrinivas Pandruvada return len; 4649fbfb4b3SSrinivas Pandruvada } 46567eedba3SMichael Hennerich default: 466a980e046SJonathan Cameron return 0; 467a980e046SJonathan Cameron } 46867eedba3SMichael Hennerich } 469a980e046SJonathan Cameron 4703661f3f5SLars-Peter Clausen static ssize_t iio_read_channel_info(struct device *dev, 4713661f3f5SLars-Peter Clausen struct device_attribute *attr, 4723661f3f5SLars-Peter Clausen char *buf) 4733661f3f5SLars-Peter Clausen { 4743661f3f5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 4753661f3f5SLars-Peter Clausen struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 4769fbfb4b3SSrinivas Pandruvada int vals[INDIO_MAX_RAW_ELEMENTS]; 4779fbfb4b3SSrinivas Pandruvada int ret; 4789fbfb4b3SSrinivas Pandruvada int val_len = 2; 4799fbfb4b3SSrinivas Pandruvada 4809fbfb4b3SSrinivas Pandruvada if (indio_dev->info->read_raw_multi) 4819fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c, 4829fbfb4b3SSrinivas Pandruvada INDIO_MAX_RAW_ELEMENTS, 4839fbfb4b3SSrinivas Pandruvada vals, &val_len, 4849fbfb4b3SSrinivas Pandruvada this_attr->address); 4859fbfb4b3SSrinivas Pandruvada else 4869fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw(indio_dev, this_attr->c, 4879fbfb4b3SSrinivas Pandruvada &vals[0], &vals[1], this_attr->address); 4883661f3f5SLars-Peter Clausen 4893661f3f5SLars-Peter Clausen if (ret < 0) 4903661f3f5SLars-Peter Clausen return ret; 4913661f3f5SLars-Peter Clausen 4929fbfb4b3SSrinivas Pandruvada return iio_format_value(buf, ret, val_len, vals); 4933661f3f5SLars-Peter Clausen } 4943661f3f5SLars-Peter Clausen 4956807d721SLars-Peter Clausen /** 4966807d721SLars-Peter Clausen * iio_str_to_fixpoint() - Parse a fixed-point number from a string 4976807d721SLars-Peter Clausen * @str: The string to parse 4986807d721SLars-Peter Clausen * @fract_mult: Multiplier for the first decimal place, should be a power of 10 4996807d721SLars-Peter Clausen * @integer: The integer part of the number 5006807d721SLars-Peter Clausen * @fract: The fractional part of the number 5016807d721SLars-Peter Clausen * 5026807d721SLars-Peter Clausen * Returns 0 on success, or a negative error code if the string could not be 5036807d721SLars-Peter Clausen * parsed. 5046807d721SLars-Peter Clausen */ 5056807d721SLars-Peter Clausen int iio_str_to_fixpoint(const char *str, int fract_mult, 5066807d721SLars-Peter Clausen int *integer, int *fract) 5076807d721SLars-Peter Clausen { 5086807d721SLars-Peter Clausen int i = 0, f = 0; 5096807d721SLars-Peter Clausen bool integer_part = true, negative = false; 5106807d721SLars-Peter Clausen 5116807d721SLars-Peter Clausen if (str[0] == '-') { 5126807d721SLars-Peter Clausen negative = true; 5136807d721SLars-Peter Clausen str++; 5146807d721SLars-Peter Clausen } else if (str[0] == '+') { 5156807d721SLars-Peter Clausen str++; 5166807d721SLars-Peter Clausen } 5176807d721SLars-Peter Clausen 5186807d721SLars-Peter Clausen while (*str) { 5196807d721SLars-Peter Clausen if ('0' <= *str && *str <= '9') { 5206807d721SLars-Peter Clausen if (integer_part) { 5216807d721SLars-Peter Clausen i = i * 10 + *str - '0'; 5226807d721SLars-Peter Clausen } else { 5236807d721SLars-Peter Clausen f += fract_mult * (*str - '0'); 5246807d721SLars-Peter Clausen fract_mult /= 10; 5256807d721SLars-Peter Clausen } 5266807d721SLars-Peter Clausen } else if (*str == '\n') { 5276807d721SLars-Peter Clausen if (*(str + 1) == '\0') 5286807d721SLars-Peter Clausen break; 5296807d721SLars-Peter Clausen else 5306807d721SLars-Peter Clausen return -EINVAL; 5316807d721SLars-Peter Clausen } else if (*str == '.' && integer_part) { 5326807d721SLars-Peter Clausen integer_part = false; 5336807d721SLars-Peter Clausen } else { 5346807d721SLars-Peter Clausen return -EINVAL; 5356807d721SLars-Peter Clausen } 5366807d721SLars-Peter Clausen str++; 5376807d721SLars-Peter Clausen } 5386807d721SLars-Peter Clausen 5396807d721SLars-Peter Clausen if (negative) { 5406807d721SLars-Peter Clausen if (i) 5416807d721SLars-Peter Clausen i = -i; 5426807d721SLars-Peter Clausen else 5436807d721SLars-Peter Clausen f = -f; 5446807d721SLars-Peter Clausen } 5456807d721SLars-Peter Clausen 5466807d721SLars-Peter Clausen *integer = i; 5476807d721SLars-Peter Clausen *fract = f; 5486807d721SLars-Peter Clausen 5496807d721SLars-Peter Clausen return 0; 5506807d721SLars-Peter Clausen } 5516807d721SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_str_to_fixpoint); 5526807d721SLars-Peter Clausen 553a980e046SJonathan Cameron static ssize_t iio_write_channel_info(struct device *dev, 554a980e046SJonathan Cameron struct device_attribute *attr, 555a980e046SJonathan Cameron const char *buf, 556a980e046SJonathan Cameron size_t len) 557a980e046SJonathan Cameron { 558e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 559a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 5606807d721SLars-Peter Clausen int ret, fract_mult = 100000; 5616807d721SLars-Peter Clausen int integer, fract; 562a980e046SJonathan Cameron 563a980e046SJonathan Cameron /* Assumes decimal - precision based on number of digits */ 564a980e046SJonathan Cameron if (!indio_dev->info->write_raw) 565a980e046SJonathan Cameron return -EINVAL; 566a980e046SJonathan Cameron 567a980e046SJonathan Cameron if (indio_dev->info->write_raw_get_fmt) 568a980e046SJonathan Cameron switch (indio_dev->info->write_raw_get_fmt(indio_dev, 569a980e046SJonathan Cameron this_attr->c, this_attr->address)) { 570a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_MICRO: 571a980e046SJonathan Cameron fract_mult = 100000; 572a980e046SJonathan Cameron break; 573a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_NANO: 574a980e046SJonathan Cameron fract_mult = 100000000; 575a980e046SJonathan Cameron break; 576a980e046SJonathan Cameron default: 577a980e046SJonathan Cameron return -EINVAL; 578a980e046SJonathan Cameron } 579a980e046SJonathan Cameron 5806807d721SLars-Peter Clausen ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract); 5816807d721SLars-Peter Clausen if (ret) 5826807d721SLars-Peter Clausen return ret; 583a980e046SJonathan Cameron 584a980e046SJonathan Cameron ret = indio_dev->info->write_raw(indio_dev, this_attr->c, 585a980e046SJonathan Cameron integer, fract, this_attr->address); 586a980e046SJonathan Cameron if (ret) 587a980e046SJonathan Cameron return ret; 588a980e046SJonathan Cameron 589a980e046SJonathan Cameron return len; 590a980e046SJonathan Cameron } 591a980e046SJonathan Cameron 592a980e046SJonathan Cameron static 593a980e046SJonathan Cameron int __iio_device_attr_init(struct device_attribute *dev_attr, 594a980e046SJonathan Cameron const char *postfix, 595a980e046SJonathan Cameron struct iio_chan_spec const *chan, 596a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 597a980e046SJonathan Cameron struct device_attribute *attr, 598a980e046SJonathan Cameron char *buf), 599a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 600a980e046SJonathan Cameron struct device_attribute *attr, 601a980e046SJonathan Cameron const char *buf, 602a980e046SJonathan Cameron size_t len), 6033704432fSJonathan Cameron enum iio_shared_by shared_by) 604a980e046SJonathan Cameron { 6053704432fSJonathan Cameron int ret = 0; 6067bbcf7e1SLars-Peter Clausen char *name = NULL; 6073704432fSJonathan Cameron char *full_postfix; 608a980e046SJonathan Cameron sysfs_attr_init(&dev_attr->attr); 609a980e046SJonathan Cameron 610a980e046SJonathan Cameron /* Build up postfix of <extend_name>_<modifier>_postfix */ 6113704432fSJonathan Cameron if (chan->modified && (shared_by == IIO_SEPARATE)) { 612a980e046SJonathan Cameron if (chan->extend_name) 613a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", 614a980e046SJonathan Cameron iio_modifier_names[chan 615a980e046SJonathan Cameron ->channel2], 616a980e046SJonathan Cameron chan->extend_name, 617a980e046SJonathan Cameron postfix); 618a980e046SJonathan Cameron else 619a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s", 620a980e046SJonathan Cameron iio_modifier_names[chan 621a980e046SJonathan Cameron ->channel2], 622a980e046SJonathan Cameron postfix); 623a980e046SJonathan Cameron } else { 62477bfa8baSLars-Peter Clausen if (chan->extend_name == NULL || shared_by != IIO_SEPARATE) 625a980e046SJonathan Cameron full_postfix = kstrdup(postfix, GFP_KERNEL); 626a980e046SJonathan Cameron else 627a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, 628a980e046SJonathan Cameron "%s_%s", 629a980e046SJonathan Cameron chan->extend_name, 630a980e046SJonathan Cameron postfix); 631a980e046SJonathan Cameron } 6323704432fSJonathan Cameron if (full_postfix == NULL) 6333704432fSJonathan Cameron return -ENOMEM; 634a980e046SJonathan Cameron 635a980e046SJonathan Cameron if (chan->differential) { /* Differential can not have modifier */ 6363704432fSJonathan Cameron switch (shared_by) { 637c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 6387bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 639c006ec83SJonathan Cameron break; 640c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 6417bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 642c006ec83SJonathan Cameron iio_direction[chan->output], 643c006ec83SJonathan Cameron full_postfix); 644c006ec83SJonathan Cameron break; 6453704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 6467bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s", 647a980e046SJonathan Cameron iio_direction[chan->output], 648a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 649a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 650a980e046SJonathan Cameron full_postfix); 6513704432fSJonathan Cameron break; 6523704432fSJonathan Cameron case IIO_SEPARATE: 6533704432fSJonathan Cameron if (!chan->indexed) { 6543704432fSJonathan Cameron WARN_ON("Differential channels must be indexed\n"); 6553704432fSJonathan Cameron ret = -EINVAL; 6563704432fSJonathan Cameron goto error_free_full_postfix; 6573704432fSJonathan Cameron } 6587bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, 6593704432fSJonathan Cameron "%s_%s%d-%s%d_%s", 660a980e046SJonathan Cameron iio_direction[chan->output], 661a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 662a980e046SJonathan Cameron chan->channel, 663a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 664a980e046SJonathan Cameron chan->channel2, 665a980e046SJonathan Cameron full_postfix); 6663704432fSJonathan Cameron break; 667a980e046SJonathan Cameron } 668a980e046SJonathan Cameron } else { /* Single ended */ 6693704432fSJonathan Cameron switch (shared_by) { 670c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 6717bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 672c006ec83SJonathan Cameron break; 673c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 6747bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 675c006ec83SJonathan Cameron iio_direction[chan->output], 676c006ec83SJonathan Cameron full_postfix); 677c006ec83SJonathan Cameron break; 6783704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 6797bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 680a980e046SJonathan Cameron iio_direction[chan->output], 681a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 682a980e046SJonathan Cameron full_postfix); 6833704432fSJonathan Cameron break; 6843704432fSJonathan Cameron 6853704432fSJonathan Cameron case IIO_SEPARATE: 6863704432fSJonathan Cameron if (chan->indexed) 6877bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s%d_%s", 688a980e046SJonathan Cameron iio_direction[chan->output], 689a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 690a980e046SJonathan Cameron chan->channel, 691a980e046SJonathan Cameron full_postfix); 692a980e046SJonathan Cameron else 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 } 699a980e046SJonathan Cameron } 7007bbcf7e1SLars-Peter Clausen if (name == NULL) { 701a980e046SJonathan Cameron ret = -ENOMEM; 702a980e046SJonathan Cameron goto error_free_full_postfix; 703a980e046SJonathan Cameron } 7047bbcf7e1SLars-Peter Clausen dev_attr->attr.name = name; 705a980e046SJonathan Cameron 706a980e046SJonathan Cameron if (readfunc) { 707a980e046SJonathan Cameron dev_attr->attr.mode |= S_IRUGO; 708a980e046SJonathan Cameron dev_attr->show = readfunc; 709a980e046SJonathan Cameron } 710a980e046SJonathan Cameron 711a980e046SJonathan Cameron if (writefunc) { 712a980e046SJonathan Cameron dev_attr->attr.mode |= S_IWUSR; 713a980e046SJonathan Cameron dev_attr->store = writefunc; 714a980e046SJonathan Cameron } 7157bbcf7e1SLars-Peter Clausen 716a980e046SJonathan Cameron error_free_full_postfix: 717a980e046SJonathan Cameron kfree(full_postfix); 7183704432fSJonathan Cameron 719a980e046SJonathan Cameron return ret; 720a980e046SJonathan Cameron } 721a980e046SJonathan Cameron 722a980e046SJonathan Cameron static void __iio_device_attr_deinit(struct device_attribute *dev_attr) 723a980e046SJonathan Cameron { 724a980e046SJonathan Cameron kfree(dev_attr->attr.name); 725a980e046SJonathan Cameron } 726a980e046SJonathan Cameron 727a980e046SJonathan Cameron int __iio_add_chan_devattr(const char *postfix, 728a980e046SJonathan Cameron struct iio_chan_spec const *chan, 729a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 730a980e046SJonathan Cameron struct device_attribute *attr, 731a980e046SJonathan Cameron char *buf), 732a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 733a980e046SJonathan Cameron struct device_attribute *attr, 734a980e046SJonathan Cameron const char *buf, 735a980e046SJonathan Cameron size_t len), 736a980e046SJonathan Cameron u64 mask, 7373704432fSJonathan Cameron enum iio_shared_by shared_by, 738a980e046SJonathan Cameron struct device *dev, 739a980e046SJonathan Cameron struct list_head *attr_list) 740a980e046SJonathan Cameron { 741a980e046SJonathan Cameron int ret; 742a980e046SJonathan Cameron struct iio_dev_attr *iio_attr, *t; 743a980e046SJonathan Cameron 744670c1103SSachin Kamat iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL); 74592825ff9SHartmut Knaack if (iio_attr == NULL) 74692825ff9SHartmut Knaack return -ENOMEM; 747a980e046SJonathan Cameron ret = __iio_device_attr_init(&iio_attr->dev_attr, 748a980e046SJonathan Cameron postfix, chan, 7493704432fSJonathan Cameron readfunc, writefunc, shared_by); 750a980e046SJonathan Cameron if (ret) 751a980e046SJonathan Cameron goto error_iio_dev_attr_free; 752a980e046SJonathan Cameron iio_attr->c = chan; 753a980e046SJonathan Cameron iio_attr->address = mask; 754a980e046SJonathan Cameron list_for_each_entry(t, attr_list, l) 755a980e046SJonathan Cameron if (strcmp(t->dev_attr.attr.name, 756a980e046SJonathan Cameron iio_attr->dev_attr.attr.name) == 0) { 7573704432fSJonathan Cameron if (shared_by == IIO_SEPARATE) 758a980e046SJonathan Cameron dev_err(dev, "tried to double register : %s\n", 759a980e046SJonathan Cameron t->dev_attr.attr.name); 760a980e046SJonathan Cameron ret = -EBUSY; 761a980e046SJonathan Cameron goto error_device_attr_deinit; 762a980e046SJonathan Cameron } 763a980e046SJonathan Cameron list_add(&iio_attr->l, attr_list); 764a980e046SJonathan Cameron 765a980e046SJonathan Cameron return 0; 766a980e046SJonathan Cameron 767a980e046SJonathan Cameron error_device_attr_deinit: 768a980e046SJonathan Cameron __iio_device_attr_deinit(&iio_attr->dev_attr); 769a980e046SJonathan Cameron error_iio_dev_attr_free: 770a980e046SJonathan Cameron kfree(iio_attr); 771a980e046SJonathan Cameron return ret; 772a980e046SJonathan Cameron } 773a980e046SJonathan Cameron 7743704432fSJonathan Cameron static int iio_device_add_info_mask_type(struct iio_dev *indio_dev, 7753704432fSJonathan Cameron struct iio_chan_spec const *chan, 7763704432fSJonathan Cameron enum iio_shared_by shared_by, 7773704432fSJonathan Cameron const long *infomask) 7783704432fSJonathan Cameron { 7793704432fSJonathan Cameron int i, ret, attrcount = 0; 7803704432fSJonathan Cameron 7813704432fSJonathan Cameron for_each_set_bit(i, infomask, sizeof(infomask)*8) { 782ef4b4856SJonathan Cameron if (i >= ARRAY_SIZE(iio_chan_info_postfix)) 783ef4b4856SJonathan Cameron return -EINVAL; 7843704432fSJonathan Cameron ret = __iio_add_chan_devattr(iio_chan_info_postfix[i], 7853704432fSJonathan Cameron chan, 7863704432fSJonathan Cameron &iio_read_channel_info, 7873704432fSJonathan Cameron &iio_write_channel_info, 7883704432fSJonathan Cameron i, 7893704432fSJonathan Cameron shared_by, 7903704432fSJonathan Cameron &indio_dev->dev, 7913704432fSJonathan Cameron &indio_dev->channel_attr_list); 7923704432fSJonathan Cameron if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) 7933704432fSJonathan Cameron continue; 7943704432fSJonathan Cameron else if (ret < 0) 7953704432fSJonathan Cameron return ret; 7963704432fSJonathan Cameron attrcount++; 7973704432fSJonathan Cameron } 7983704432fSJonathan Cameron 7993704432fSJonathan Cameron return attrcount; 8003704432fSJonathan Cameron } 8013704432fSJonathan Cameron 802a980e046SJonathan Cameron static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev, 803a980e046SJonathan Cameron struct iio_chan_spec const *chan) 804a980e046SJonathan Cameron { 805a980e046SJonathan Cameron int ret, attrcount = 0; 806a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 807a980e046SJonathan Cameron 808a980e046SJonathan Cameron if (chan->channel < 0) 809a980e046SJonathan Cameron return 0; 8103704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 8113704432fSJonathan Cameron IIO_SEPARATE, 8123704432fSJonathan Cameron &chan->info_mask_separate); 8138655cc49SJonathan Cameron if (ret < 0) 8143704432fSJonathan Cameron return ret; 8153704432fSJonathan Cameron attrcount += ret; 8163704432fSJonathan Cameron 8173704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 8183704432fSJonathan Cameron IIO_SHARED_BY_TYPE, 8193704432fSJonathan Cameron &chan->info_mask_shared_by_type); 8203704432fSJonathan Cameron if (ret < 0) 8213704432fSJonathan Cameron return ret; 8223704432fSJonathan Cameron attrcount += ret; 823a980e046SJonathan Cameron 824c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 825c006ec83SJonathan Cameron IIO_SHARED_BY_DIR, 826c006ec83SJonathan Cameron &chan->info_mask_shared_by_dir); 827c006ec83SJonathan Cameron if (ret < 0) 828c006ec83SJonathan Cameron return ret; 829c006ec83SJonathan Cameron attrcount += ret; 830c006ec83SJonathan Cameron 831c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 832c006ec83SJonathan Cameron IIO_SHARED_BY_ALL, 833c006ec83SJonathan Cameron &chan->info_mask_shared_by_all); 834c006ec83SJonathan Cameron if (ret < 0) 835c006ec83SJonathan Cameron return ret; 836c006ec83SJonathan Cameron attrcount += ret; 837c006ec83SJonathan Cameron 838a980e046SJonathan Cameron if (chan->ext_info) { 839a980e046SJonathan Cameron unsigned int i = 0; 840a980e046SJonathan Cameron for (ext_info = chan->ext_info; ext_info->name; ext_info++) { 841a980e046SJonathan Cameron ret = __iio_add_chan_devattr(ext_info->name, 842a980e046SJonathan Cameron chan, 843a980e046SJonathan Cameron ext_info->read ? 844a980e046SJonathan Cameron &iio_read_channel_ext_info : NULL, 845a980e046SJonathan Cameron ext_info->write ? 846a980e046SJonathan Cameron &iio_write_channel_ext_info : NULL, 847a980e046SJonathan Cameron i, 848a980e046SJonathan Cameron ext_info->shared, 849a980e046SJonathan Cameron &indio_dev->dev, 850a980e046SJonathan Cameron &indio_dev->channel_attr_list); 851a980e046SJonathan Cameron i++; 852a980e046SJonathan Cameron if (ret == -EBUSY && ext_info->shared) 853a980e046SJonathan Cameron continue; 854a980e046SJonathan Cameron 855a980e046SJonathan Cameron if (ret) 8563704432fSJonathan Cameron return ret; 857a980e046SJonathan Cameron 858a980e046SJonathan Cameron attrcount++; 859a980e046SJonathan Cameron } 860a980e046SJonathan Cameron } 861a980e046SJonathan Cameron 8623704432fSJonathan Cameron return attrcount; 863a980e046SJonathan Cameron } 864a980e046SJonathan Cameron 86584088ebdSLars-Peter Clausen /** 86684088ebdSLars-Peter Clausen * iio_free_chan_devattr_list() - Free a list of IIO device attributes 86784088ebdSLars-Peter Clausen * @attr_list: List of IIO device attributes 86884088ebdSLars-Peter Clausen * 86984088ebdSLars-Peter Clausen * This function frees the memory allocated for each of the IIO device 870c1b03ab5SMartin Fuzzey * attributes in the list. 87184088ebdSLars-Peter Clausen */ 87284088ebdSLars-Peter Clausen void iio_free_chan_devattr_list(struct list_head *attr_list) 873a980e046SJonathan Cameron { 87484088ebdSLars-Peter Clausen struct iio_dev_attr *p, *n; 87584088ebdSLars-Peter Clausen 87684088ebdSLars-Peter Clausen list_for_each_entry_safe(p, n, attr_list, l) { 877a980e046SJonathan Cameron kfree(p->dev_attr.attr.name); 878c1b03ab5SMartin Fuzzey list_del(&p->l); 879a980e046SJonathan Cameron kfree(p); 880a980e046SJonathan Cameron } 88184088ebdSLars-Peter Clausen } 882a980e046SJonathan Cameron 883a980e046SJonathan Cameron static ssize_t iio_show_dev_name(struct device *dev, 884a980e046SJonathan Cameron struct device_attribute *attr, 885a980e046SJonathan Cameron char *buf) 886a980e046SJonathan Cameron { 887e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 888598db581SKees Cook return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name); 889a980e046SJonathan Cameron } 890a980e046SJonathan Cameron 891a980e046SJonathan Cameron static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL); 892a980e046SJonathan Cameron 893a980e046SJonathan Cameron static int iio_device_register_sysfs(struct iio_dev *indio_dev) 894a980e046SJonathan Cameron { 895a980e046SJonathan Cameron int i, ret = 0, attrcount, attrn, attrcount_orig = 0; 89684088ebdSLars-Peter Clausen struct iio_dev_attr *p; 897a980e046SJonathan Cameron struct attribute **attr; 898a980e046SJonathan Cameron 899a980e046SJonathan Cameron /* First count elements in any existing group */ 900a980e046SJonathan Cameron if (indio_dev->info->attrs) { 901a980e046SJonathan Cameron attr = indio_dev->info->attrs->attrs; 902a980e046SJonathan Cameron while (*attr++ != NULL) 903a980e046SJonathan Cameron attrcount_orig++; 904a980e046SJonathan Cameron } 905a980e046SJonathan Cameron attrcount = attrcount_orig; 906a980e046SJonathan Cameron /* 907a980e046SJonathan Cameron * New channel registration method - relies on the fact a group does 908d25b3808SPeter Meerwald * not need to be initialized if its name is NULL. 909a980e046SJonathan Cameron */ 910a980e046SJonathan Cameron if (indio_dev->channels) 911a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) { 912a980e046SJonathan Cameron ret = iio_device_add_channel_sysfs(indio_dev, 913a980e046SJonathan Cameron &indio_dev 914a980e046SJonathan Cameron ->channels[i]); 915a980e046SJonathan Cameron if (ret < 0) 916a980e046SJonathan Cameron goto error_clear_attrs; 917a980e046SJonathan Cameron attrcount += ret; 918a980e046SJonathan Cameron } 919a980e046SJonathan Cameron 920a980e046SJonathan Cameron if (indio_dev->name) 921a980e046SJonathan Cameron attrcount++; 922a980e046SJonathan Cameron 923a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1, 924a980e046SJonathan Cameron sizeof(indio_dev->chan_attr_group.attrs[0]), 925a980e046SJonathan Cameron GFP_KERNEL); 926a980e046SJonathan Cameron if (indio_dev->chan_attr_group.attrs == NULL) { 927a980e046SJonathan Cameron ret = -ENOMEM; 928a980e046SJonathan Cameron goto error_clear_attrs; 929a980e046SJonathan Cameron } 930a980e046SJonathan Cameron /* Copy across original attributes */ 931a980e046SJonathan Cameron if (indio_dev->info->attrs) 932a980e046SJonathan Cameron memcpy(indio_dev->chan_attr_group.attrs, 933a980e046SJonathan Cameron indio_dev->info->attrs->attrs, 934a980e046SJonathan Cameron sizeof(indio_dev->chan_attr_group.attrs[0]) 935a980e046SJonathan Cameron *attrcount_orig); 936a980e046SJonathan Cameron attrn = attrcount_orig; 937a980e046SJonathan Cameron /* Add all elements from the list. */ 938a980e046SJonathan Cameron list_for_each_entry(p, &indio_dev->channel_attr_list, l) 939a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr; 940a980e046SJonathan Cameron if (indio_dev->name) 941a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr; 942a980e046SJonathan Cameron 943a980e046SJonathan Cameron indio_dev->groups[indio_dev->groupcounter++] = 944a980e046SJonathan Cameron &indio_dev->chan_attr_group; 945a980e046SJonathan Cameron 946a980e046SJonathan Cameron return 0; 947a980e046SJonathan Cameron 948a980e046SJonathan Cameron error_clear_attrs: 94984088ebdSLars-Peter Clausen iio_free_chan_devattr_list(&indio_dev->channel_attr_list); 950a980e046SJonathan Cameron 951a980e046SJonathan Cameron return ret; 952a980e046SJonathan Cameron } 953a980e046SJonathan Cameron 954a980e046SJonathan Cameron static void iio_device_unregister_sysfs(struct iio_dev *indio_dev) 955a980e046SJonathan Cameron { 956a980e046SJonathan Cameron 95784088ebdSLars-Peter Clausen iio_free_chan_devattr_list(&indio_dev->channel_attr_list); 958a980e046SJonathan Cameron kfree(indio_dev->chan_attr_group.attrs); 959c1b03ab5SMartin Fuzzey indio_dev->chan_attr_group.attrs = NULL; 960a980e046SJonathan Cameron } 961a980e046SJonathan Cameron 962a980e046SJonathan Cameron static void iio_dev_release(struct device *device) 963a980e046SJonathan Cameron { 964e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(device); 965*735ad074SVladimir Barinov if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED)) 966a980e046SJonathan Cameron iio_device_unregister_trigger_consumer(indio_dev); 967a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 968a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 969e407fd65SLars-Peter Clausen 9709e69c935SLars-Peter Clausen iio_buffer_put(indio_dev->buffer); 9719e69c935SLars-Peter Clausen 972e407fd65SLars-Peter Clausen ida_simple_remove(&iio_ida, indio_dev->id); 973e407fd65SLars-Peter Clausen kfree(indio_dev); 974a980e046SJonathan Cameron } 975a980e046SJonathan Cameron 97617d82b47SGuenter Roeck struct device_type iio_device_type = { 977a980e046SJonathan Cameron .name = "iio_device", 978a980e046SJonathan Cameron .release = iio_dev_release, 979a980e046SJonathan Cameron }; 980a980e046SJonathan Cameron 981a7e57dceSSachin Kamat /** 982a7e57dceSSachin Kamat * iio_device_alloc() - allocate an iio_dev from a driver 983a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 984a7e57dceSSachin Kamat **/ 9857cbb7537SLars-Peter Clausen struct iio_dev *iio_device_alloc(int sizeof_priv) 986a980e046SJonathan Cameron { 987a980e046SJonathan Cameron struct iio_dev *dev; 988a980e046SJonathan Cameron size_t alloc_size; 989a980e046SJonathan Cameron 990a980e046SJonathan Cameron alloc_size = sizeof(struct iio_dev); 991a980e046SJonathan Cameron if (sizeof_priv) { 992a980e046SJonathan Cameron alloc_size = ALIGN(alloc_size, IIO_ALIGN); 993a980e046SJonathan Cameron alloc_size += sizeof_priv; 994a980e046SJonathan Cameron } 995a980e046SJonathan Cameron /* ensure 32-byte alignment of whole construct ? */ 996a980e046SJonathan Cameron alloc_size += IIO_ALIGN - 1; 997a980e046SJonathan Cameron 998a980e046SJonathan Cameron dev = kzalloc(alloc_size, GFP_KERNEL); 999a980e046SJonathan Cameron 1000a980e046SJonathan Cameron if (dev) { 1001a980e046SJonathan Cameron dev->dev.groups = dev->groups; 100217d82b47SGuenter Roeck dev->dev.type = &iio_device_type; 1003a980e046SJonathan Cameron dev->dev.bus = &iio_bus_type; 1004a980e046SJonathan Cameron device_initialize(&dev->dev); 1005a980e046SJonathan Cameron dev_set_drvdata(&dev->dev, (void *)dev); 1006a980e046SJonathan Cameron mutex_init(&dev->mlock); 1007a980e046SJonathan Cameron mutex_init(&dev->info_exist_lock); 1008e407fd65SLars-Peter Clausen INIT_LIST_HEAD(&dev->channel_attr_list); 1009a980e046SJonathan Cameron 1010a980e046SJonathan Cameron dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL); 1011a980e046SJonathan Cameron if (dev->id < 0) { 1012a980e046SJonathan Cameron /* cannot use a dev_err as the name isn't available */ 10133176dd5dSSachin Kamat pr_err("failed to get device id\n"); 1014a980e046SJonathan Cameron kfree(dev); 1015a980e046SJonathan Cameron return NULL; 1016a980e046SJonathan Cameron } 1017a980e046SJonathan Cameron dev_set_name(&dev->dev, "iio:device%d", dev->id); 101884b36ce5SJonathan Cameron INIT_LIST_HEAD(&dev->buffer_list); 1019a980e046SJonathan Cameron } 1020a980e046SJonathan Cameron 1021a980e046SJonathan Cameron return dev; 1022a980e046SJonathan Cameron } 10237cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_alloc); 1024a980e046SJonathan Cameron 1025a7e57dceSSachin Kamat /** 1026a7e57dceSSachin Kamat * iio_device_free() - free an iio_dev from a driver 1027a7e57dceSSachin Kamat * @dev: the iio_dev associated with the device 1028a7e57dceSSachin Kamat **/ 10297cbb7537SLars-Peter Clausen void iio_device_free(struct iio_dev *dev) 1030a980e046SJonathan Cameron { 1031e407fd65SLars-Peter Clausen if (dev) 1032e407fd65SLars-Peter Clausen put_device(&dev->dev); 1033a980e046SJonathan Cameron } 10347cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_free); 1035a980e046SJonathan Cameron 10369dabaf5eSGrygorii Strashko static void devm_iio_device_release(struct device *dev, void *res) 10379dabaf5eSGrygorii Strashko { 10389dabaf5eSGrygorii Strashko iio_device_free(*(struct iio_dev **)res); 10399dabaf5eSGrygorii Strashko } 10409dabaf5eSGrygorii Strashko 10419dabaf5eSGrygorii Strashko static int devm_iio_device_match(struct device *dev, void *res, void *data) 10429dabaf5eSGrygorii Strashko { 10439dabaf5eSGrygorii Strashko struct iio_dev **r = res; 10449dabaf5eSGrygorii Strashko if (!r || !*r) { 10459dabaf5eSGrygorii Strashko WARN_ON(!r || !*r); 10469dabaf5eSGrygorii Strashko return 0; 10479dabaf5eSGrygorii Strashko } 10489dabaf5eSGrygorii Strashko return *r == data; 10499dabaf5eSGrygorii Strashko } 10509dabaf5eSGrygorii Strashko 1051a7e57dceSSachin Kamat /** 1052a7e57dceSSachin Kamat * devm_iio_device_alloc - Resource-managed iio_device_alloc() 1053a7e57dceSSachin Kamat * @dev: Device to allocate iio_dev for 1054a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 1055a7e57dceSSachin Kamat * 1056a7e57dceSSachin Kamat * Managed iio_device_alloc. iio_dev allocated with this function is 1057a7e57dceSSachin Kamat * automatically freed on driver detach. 1058a7e57dceSSachin Kamat * 1059a7e57dceSSachin Kamat * If an iio_dev allocated with this function needs to be freed separately, 1060a7e57dceSSachin Kamat * devm_iio_device_free() must be used. 1061a7e57dceSSachin Kamat * 1062a7e57dceSSachin Kamat * RETURNS: 1063a7e57dceSSachin Kamat * Pointer to allocated iio_dev on success, NULL on failure. 1064a7e57dceSSachin Kamat */ 10659dabaf5eSGrygorii Strashko struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv) 10669dabaf5eSGrygorii Strashko { 10679dabaf5eSGrygorii Strashko struct iio_dev **ptr, *iio_dev; 10689dabaf5eSGrygorii Strashko 10699dabaf5eSGrygorii Strashko ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr), 10709dabaf5eSGrygorii Strashko GFP_KERNEL); 10719dabaf5eSGrygorii Strashko if (!ptr) 10729dabaf5eSGrygorii Strashko return NULL; 10739dabaf5eSGrygorii Strashko 10749dabaf5eSGrygorii Strashko iio_dev = iio_device_alloc(sizeof_priv); 10759dabaf5eSGrygorii Strashko if (iio_dev) { 10769dabaf5eSGrygorii Strashko *ptr = iio_dev; 10779dabaf5eSGrygorii Strashko devres_add(dev, ptr); 10789dabaf5eSGrygorii Strashko } else { 10799dabaf5eSGrygorii Strashko devres_free(ptr); 10809dabaf5eSGrygorii Strashko } 10819dabaf5eSGrygorii Strashko 10829dabaf5eSGrygorii Strashko return iio_dev; 10839dabaf5eSGrygorii Strashko } 10849dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_alloc); 10859dabaf5eSGrygorii Strashko 1086a7e57dceSSachin Kamat /** 1087a7e57dceSSachin Kamat * devm_iio_device_free - Resource-managed iio_device_free() 1088a7e57dceSSachin Kamat * @dev: Device this iio_dev belongs to 1089a7e57dceSSachin Kamat * @iio_dev: the iio_dev associated with the device 1090a7e57dceSSachin Kamat * 1091a7e57dceSSachin Kamat * Free iio_dev allocated with devm_iio_device_alloc(). 1092a7e57dceSSachin Kamat */ 10939dabaf5eSGrygorii Strashko void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev) 10949dabaf5eSGrygorii Strashko { 10959dabaf5eSGrygorii Strashko int rc; 10969dabaf5eSGrygorii Strashko 10979dabaf5eSGrygorii Strashko rc = devres_release(dev, devm_iio_device_release, 10989dabaf5eSGrygorii Strashko devm_iio_device_match, iio_dev); 10999dabaf5eSGrygorii Strashko WARN_ON(rc); 11009dabaf5eSGrygorii Strashko } 11019dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_free); 11029dabaf5eSGrygorii Strashko 1103a980e046SJonathan Cameron /** 1104a980e046SJonathan Cameron * iio_chrdev_open() - chrdev file open for buffer access and ioctls 11052498dcf6SCristina Opriceana * @inode: Inode structure for identifying the device in the file system 11062498dcf6SCristina Opriceana * @filp: File structure for iio device used to keep and later access 11072498dcf6SCristina Opriceana * private data 11082498dcf6SCristina Opriceana * 11092498dcf6SCristina Opriceana * Return: 0 on success or -EBUSY if the device is already opened 1110a980e046SJonathan Cameron **/ 1111a980e046SJonathan Cameron static int iio_chrdev_open(struct inode *inode, struct file *filp) 1112a980e046SJonathan Cameron { 1113a980e046SJonathan Cameron struct iio_dev *indio_dev = container_of(inode->i_cdev, 1114a980e046SJonathan Cameron struct iio_dev, chrdev); 1115a980e046SJonathan Cameron 1116a980e046SJonathan Cameron if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags)) 1117a980e046SJonathan Cameron return -EBUSY; 1118a980e046SJonathan Cameron 1119cadc2125SLars-Peter Clausen iio_device_get(indio_dev); 1120cadc2125SLars-Peter Clausen 1121a980e046SJonathan Cameron filp->private_data = indio_dev; 1122a980e046SJonathan Cameron 1123a980e046SJonathan Cameron return 0; 1124a980e046SJonathan Cameron } 1125a980e046SJonathan Cameron 1126a980e046SJonathan Cameron /** 1127a980e046SJonathan Cameron * iio_chrdev_release() - chrdev file close buffer access and ioctls 11282498dcf6SCristina Opriceana * @inode: Inode structure pointer for the char device 11292498dcf6SCristina Opriceana * @filp: File structure pointer for the char device 11302498dcf6SCristina Opriceana * 11312498dcf6SCristina Opriceana * Return: 0 for successful release 11322498dcf6SCristina Opriceana */ 1133a980e046SJonathan Cameron static int iio_chrdev_release(struct inode *inode, struct file *filp) 1134a980e046SJonathan Cameron { 1135a980e046SJonathan Cameron struct iio_dev *indio_dev = container_of(inode->i_cdev, 1136a980e046SJonathan Cameron struct iio_dev, chrdev); 1137a980e046SJonathan Cameron clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags); 1138cadc2125SLars-Peter Clausen iio_device_put(indio_dev); 1139cadc2125SLars-Peter Clausen 1140a980e046SJonathan Cameron return 0; 1141a980e046SJonathan Cameron } 1142a980e046SJonathan Cameron 1143a980e046SJonathan Cameron /* Somewhat of a cross file organization violation - ioctls here are actually 1144a980e046SJonathan Cameron * event related */ 1145a980e046SJonathan Cameron static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 1146a980e046SJonathan Cameron { 1147a980e046SJonathan Cameron struct iio_dev *indio_dev = filp->private_data; 1148a980e046SJonathan Cameron int __user *ip = (int __user *)arg; 1149a980e046SJonathan Cameron int fd; 1150a980e046SJonathan Cameron 1151f18e7a06SLars-Peter Clausen if (!indio_dev->info) 1152f18e7a06SLars-Peter Clausen return -ENODEV; 1153f18e7a06SLars-Peter Clausen 1154a980e046SJonathan Cameron if (cmd == IIO_GET_EVENT_FD_IOCTL) { 1155a980e046SJonathan Cameron fd = iio_event_getfd(indio_dev); 11563f9059b7SLinus Walleij if (fd < 0) 11573f9059b7SLinus Walleij return fd; 1158a980e046SJonathan Cameron if (copy_to_user(ip, &fd, sizeof(fd))) 1159a980e046SJonathan Cameron return -EFAULT; 1160a980e046SJonathan Cameron return 0; 1161a980e046SJonathan Cameron } 1162a980e046SJonathan Cameron return -EINVAL; 1163a980e046SJonathan Cameron } 1164a980e046SJonathan Cameron 1165a980e046SJonathan Cameron static const struct file_operations iio_buffer_fileops = { 1166a980e046SJonathan Cameron .read = iio_buffer_read_first_n_outer_addr, 1167a980e046SJonathan Cameron .release = iio_chrdev_release, 1168a980e046SJonathan Cameron .open = iio_chrdev_open, 1169a980e046SJonathan Cameron .poll = iio_buffer_poll_addr, 1170a980e046SJonathan Cameron .owner = THIS_MODULE, 1171a980e046SJonathan Cameron .llseek = noop_llseek, 1172a980e046SJonathan Cameron .unlocked_ioctl = iio_ioctl, 1173a980e046SJonathan Cameron .compat_ioctl = iio_ioctl, 1174a980e046SJonathan Cameron }; 1175a980e046SJonathan Cameron 11768f5d8727SVlad Dogaru static int iio_check_unique_scan_index(struct iio_dev *indio_dev) 11778f5d8727SVlad Dogaru { 11788f5d8727SVlad Dogaru int i, j; 11798f5d8727SVlad Dogaru const struct iio_chan_spec *channels = indio_dev->channels; 11808f5d8727SVlad Dogaru 11818f5d8727SVlad Dogaru if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES)) 11828f5d8727SVlad Dogaru return 0; 11838f5d8727SVlad Dogaru 11848f5d8727SVlad Dogaru for (i = 0; i < indio_dev->num_channels - 1; i++) { 11858f5d8727SVlad Dogaru if (channels[i].scan_index < 0) 11868f5d8727SVlad Dogaru continue; 11878f5d8727SVlad Dogaru for (j = i + 1; j < indio_dev->num_channels; j++) 11888f5d8727SVlad Dogaru if (channels[i].scan_index == channels[j].scan_index) { 11898f5d8727SVlad Dogaru dev_err(&indio_dev->dev, 11908f5d8727SVlad Dogaru "Duplicate scan index %d\n", 11918f5d8727SVlad Dogaru channels[i].scan_index); 11928f5d8727SVlad Dogaru return -EINVAL; 11938f5d8727SVlad Dogaru } 11948f5d8727SVlad Dogaru } 11958f5d8727SVlad Dogaru 11968f5d8727SVlad Dogaru return 0; 11978f5d8727SVlad Dogaru } 11988f5d8727SVlad Dogaru 1199a980e046SJonathan Cameron static const struct iio_buffer_setup_ops noop_ring_setup_ops; 1200a980e046SJonathan Cameron 1201a7e57dceSSachin Kamat /** 1202a7e57dceSSachin Kamat * iio_device_register() - register a device with the IIO subsystem 1203a7e57dceSSachin Kamat * @indio_dev: Device structure filled by the device driver 1204a7e57dceSSachin Kamat **/ 1205a980e046SJonathan Cameron int iio_device_register(struct iio_dev *indio_dev) 1206a980e046SJonathan Cameron { 1207a980e046SJonathan Cameron int ret; 1208a980e046SJonathan Cameron 120917d82b47SGuenter Roeck /* If the calling driver did not initialize of_node, do it here */ 121017d82b47SGuenter Roeck if (!indio_dev->dev.of_node && indio_dev->dev.parent) 121117d82b47SGuenter Roeck indio_dev->dev.of_node = indio_dev->dev.parent->of_node; 121217d82b47SGuenter Roeck 12138f5d8727SVlad Dogaru ret = iio_check_unique_scan_index(indio_dev); 12148f5d8727SVlad Dogaru if (ret < 0) 12158f5d8727SVlad Dogaru return ret; 12168f5d8727SVlad Dogaru 1217a980e046SJonathan Cameron /* configure elements for the chrdev */ 1218a980e046SJonathan Cameron indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id); 1219a980e046SJonathan Cameron 1220a980e046SJonathan Cameron ret = iio_device_register_debugfs(indio_dev); 1221a980e046SJonathan Cameron if (ret) { 1222a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1223a980e046SJonathan Cameron "Failed to register debugfs interfaces\n"); 122492825ff9SHartmut Knaack return ret; 1225a980e046SJonathan Cameron } 12263e1b6c95SLars-Peter Clausen 12273e1b6c95SLars-Peter Clausen ret = iio_buffer_alloc_sysfs_and_mask(indio_dev); 12283e1b6c95SLars-Peter Clausen if (ret) { 12293e1b6c95SLars-Peter Clausen dev_err(indio_dev->dev.parent, 12303e1b6c95SLars-Peter Clausen "Failed to create buffer sysfs interfaces\n"); 12313e1b6c95SLars-Peter Clausen goto error_unreg_debugfs; 12323e1b6c95SLars-Peter Clausen } 12333e1b6c95SLars-Peter Clausen 1234a980e046SJonathan Cameron ret = iio_device_register_sysfs(indio_dev); 1235a980e046SJonathan Cameron if (ret) { 1236a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1237a980e046SJonathan Cameron "Failed to register sysfs interfaces\n"); 12383e1b6c95SLars-Peter Clausen goto error_buffer_free_sysfs; 1239a980e046SJonathan Cameron } 1240a980e046SJonathan Cameron ret = iio_device_register_eventset(indio_dev); 1241a980e046SJonathan Cameron if (ret) { 1242a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1243a980e046SJonathan Cameron "Failed to register event set\n"); 1244a980e046SJonathan Cameron goto error_free_sysfs; 1245a980e046SJonathan Cameron } 1246*735ad074SVladimir Barinov if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED)) 1247a980e046SJonathan Cameron iio_device_register_trigger_consumer(indio_dev); 1248a980e046SJonathan Cameron 1249a980e046SJonathan Cameron if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) && 1250a980e046SJonathan Cameron indio_dev->setup_ops == NULL) 1251a980e046SJonathan Cameron indio_dev->setup_ops = &noop_ring_setup_ops; 1252a980e046SJonathan Cameron 1253a980e046SJonathan Cameron cdev_init(&indio_dev->chrdev, &iio_buffer_fileops); 1254a980e046SJonathan Cameron indio_dev->chrdev.owner = indio_dev->info->driver_module; 12550d5b7daeSLars-Peter Clausen indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj; 1256a980e046SJonathan Cameron ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1); 1257a980e046SJonathan Cameron if (ret < 0) 12580d5b7daeSLars-Peter Clausen goto error_unreg_eventset; 1259a980e046SJonathan Cameron 12600d5b7daeSLars-Peter Clausen ret = device_add(&indio_dev->dev); 12610d5b7daeSLars-Peter Clausen if (ret < 0) 12620d5b7daeSLars-Peter Clausen goto error_cdev_del; 12630d5b7daeSLars-Peter Clausen 12640d5b7daeSLars-Peter Clausen return 0; 12650d5b7daeSLars-Peter Clausen error_cdev_del: 12660d5b7daeSLars-Peter Clausen cdev_del(&indio_dev->chrdev); 1267a980e046SJonathan Cameron error_unreg_eventset: 1268a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 1269a980e046SJonathan Cameron error_free_sysfs: 1270a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 12713e1b6c95SLars-Peter Clausen error_buffer_free_sysfs: 12723e1b6c95SLars-Peter Clausen iio_buffer_free_sysfs_and_mask(indio_dev); 1273a980e046SJonathan Cameron error_unreg_debugfs: 1274a980e046SJonathan Cameron iio_device_unregister_debugfs(indio_dev); 1275a980e046SJonathan Cameron return ret; 1276a980e046SJonathan Cameron } 1277a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_register); 1278a980e046SJonathan Cameron 1279a7e57dceSSachin Kamat /** 1280a7e57dceSSachin Kamat * iio_device_unregister() - unregister a device from the IIO subsystem 1281a7e57dceSSachin Kamat * @indio_dev: Device structure representing the device. 1282a7e57dceSSachin Kamat **/ 1283a980e046SJonathan Cameron void iio_device_unregister(struct iio_dev *indio_dev) 1284a980e046SJonathan Cameron { 1285a980e046SJonathan Cameron mutex_lock(&indio_dev->info_exist_lock); 1286a87c82e4SLars-Peter Clausen 1287a87c82e4SLars-Peter Clausen device_del(&indio_dev->dev); 1288a87c82e4SLars-Peter Clausen 12890d5b7daeSLars-Peter Clausen if (indio_dev->chrdev.dev) 12900d5b7daeSLars-Peter Clausen cdev_del(&indio_dev->chrdev); 1291bc4c9612SLars-Peter Clausen iio_device_unregister_debugfs(indio_dev); 12920d5b7daeSLars-Peter Clausen 1293a87c82e4SLars-Peter Clausen iio_disable_all_buffers(indio_dev); 1294a87c82e4SLars-Peter Clausen 1295a980e046SJonathan Cameron indio_dev->info = NULL; 1296d2f0a48fSLars-Peter Clausen 1297d2f0a48fSLars-Peter Clausen iio_device_wakeup_eventset(indio_dev); 1298d2f0a48fSLars-Peter Clausen iio_buffer_wakeup_poll(indio_dev); 1299d2f0a48fSLars-Peter Clausen 1300a980e046SJonathan Cameron mutex_unlock(&indio_dev->info_exist_lock); 13013e1b6c95SLars-Peter Clausen 13023e1b6c95SLars-Peter Clausen iio_buffer_free_sysfs_and_mask(indio_dev); 1303a980e046SJonathan Cameron } 1304a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_unregister); 13058caa07c0SSachin Kamat 13068caa07c0SSachin Kamat static void devm_iio_device_unreg(struct device *dev, void *res) 13078caa07c0SSachin Kamat { 13088caa07c0SSachin Kamat iio_device_unregister(*(struct iio_dev **)res); 13098caa07c0SSachin Kamat } 13108caa07c0SSachin Kamat 13118caa07c0SSachin Kamat /** 13128caa07c0SSachin Kamat * devm_iio_device_register - Resource-managed iio_device_register() 13138caa07c0SSachin Kamat * @dev: Device to allocate iio_dev for 13148caa07c0SSachin Kamat * @indio_dev: Device structure filled by the device driver 13158caa07c0SSachin Kamat * 13168caa07c0SSachin Kamat * Managed iio_device_register. The IIO device registered with this 13178caa07c0SSachin Kamat * function is automatically unregistered on driver detach. This function 13188caa07c0SSachin Kamat * calls iio_device_register() internally. Refer to that function for more 13198caa07c0SSachin Kamat * information. 13208caa07c0SSachin Kamat * 13218caa07c0SSachin Kamat * If an iio_dev registered with this function needs to be unregistered 13228caa07c0SSachin Kamat * separately, devm_iio_device_unregister() must be used. 13238caa07c0SSachin Kamat * 13248caa07c0SSachin Kamat * RETURNS: 13258caa07c0SSachin Kamat * 0 on success, negative error number on failure. 13268caa07c0SSachin Kamat */ 13278caa07c0SSachin Kamat int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev) 13288caa07c0SSachin Kamat { 13298caa07c0SSachin Kamat struct iio_dev **ptr; 13308caa07c0SSachin Kamat int ret; 13318caa07c0SSachin Kamat 13328caa07c0SSachin Kamat ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL); 13338caa07c0SSachin Kamat if (!ptr) 13348caa07c0SSachin Kamat return -ENOMEM; 13358caa07c0SSachin Kamat 13368caa07c0SSachin Kamat *ptr = indio_dev; 13378caa07c0SSachin Kamat ret = iio_device_register(indio_dev); 13388caa07c0SSachin Kamat if (!ret) 13398caa07c0SSachin Kamat devres_add(dev, ptr); 13408caa07c0SSachin Kamat else 13418caa07c0SSachin Kamat devres_free(ptr); 13428caa07c0SSachin Kamat 13438caa07c0SSachin Kamat return ret; 13448caa07c0SSachin Kamat } 13458caa07c0SSachin Kamat EXPORT_SYMBOL_GPL(devm_iio_device_register); 13468caa07c0SSachin Kamat 13478caa07c0SSachin Kamat /** 13488caa07c0SSachin Kamat * devm_iio_device_unregister - Resource-managed iio_device_unregister() 13498caa07c0SSachin Kamat * @dev: Device this iio_dev belongs to 13508caa07c0SSachin Kamat * @indio_dev: the iio_dev associated with the device 13518caa07c0SSachin Kamat * 13528caa07c0SSachin Kamat * Unregister iio_dev registered with devm_iio_device_register(). 13538caa07c0SSachin Kamat */ 13548caa07c0SSachin Kamat void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev) 13558caa07c0SSachin Kamat { 13568caa07c0SSachin Kamat int rc; 13578caa07c0SSachin Kamat 13588caa07c0SSachin Kamat rc = devres_release(dev, devm_iio_device_unreg, 13598caa07c0SSachin Kamat devm_iio_device_match, indio_dev); 13608caa07c0SSachin Kamat WARN_ON(rc); 13618caa07c0SSachin Kamat } 13628caa07c0SSachin Kamat EXPORT_SYMBOL_GPL(devm_iio_device_unregister); 13638caa07c0SSachin Kamat 1364a980e046SJonathan Cameron subsys_initcall(iio_init); 1365a980e046SJonathan Cameron module_exit(iio_exit); 1366a980e046SJonathan Cameron 1367c8b95952SJonathan Cameron MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); 1368a980e046SJonathan Cameron MODULE_DESCRIPTION("Industrial I/O core"); 1369a980e046SJonathan Cameron MODULE_LICENSE("GPL"); 1370