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> 2808a33805SAlison Schofield #include <linux/mutex.h> 29a980e046SJonathan Cameron #include <linux/iio/iio.h> 30a980e046SJonathan Cameron #include "iio_core.h" 31a980e046SJonathan Cameron #include "iio_core_trigger.h" 32a980e046SJonathan Cameron #include <linux/iio/sysfs.h> 33a980e046SJonathan Cameron #include <linux/iio/events.h> 349e69c935SLars-Peter Clausen #include <linux/iio/buffer.h> 35a980e046SJonathan Cameron 36a980e046SJonathan Cameron /* IDA to assign each registered device a unique id */ 37a980e046SJonathan Cameron static DEFINE_IDA(iio_ida); 38a980e046SJonathan Cameron 39a980e046SJonathan Cameron static dev_t iio_devt; 40a980e046SJonathan Cameron 41a980e046SJonathan Cameron #define IIO_DEV_MAX 256 42a980e046SJonathan Cameron struct bus_type iio_bus_type = { 43a980e046SJonathan Cameron .name = "iio", 44a980e046SJonathan Cameron }; 45a980e046SJonathan Cameron EXPORT_SYMBOL(iio_bus_type); 46a980e046SJonathan Cameron 47a980e046SJonathan Cameron static struct dentry *iio_debugfs_dentry; 48a980e046SJonathan Cameron 49a980e046SJonathan Cameron static const char * const iio_direction[] = { 50a980e046SJonathan Cameron [0] = "in", 51a980e046SJonathan Cameron [1] = "out", 52a980e046SJonathan Cameron }; 53a980e046SJonathan Cameron 54a980e046SJonathan Cameron static const char * const iio_chan_type_name_spec[] = { 55a980e046SJonathan Cameron [IIO_VOLTAGE] = "voltage", 56a980e046SJonathan Cameron [IIO_CURRENT] = "current", 57a980e046SJonathan Cameron [IIO_POWER] = "power", 58a980e046SJonathan Cameron [IIO_ACCEL] = "accel", 59a980e046SJonathan Cameron [IIO_ANGL_VEL] = "anglvel", 60a980e046SJonathan Cameron [IIO_MAGN] = "magn", 61a980e046SJonathan Cameron [IIO_LIGHT] = "illuminance", 62a980e046SJonathan Cameron [IIO_INTENSITY] = "intensity", 63a980e046SJonathan Cameron [IIO_PROXIMITY] = "proximity", 64a980e046SJonathan Cameron [IIO_TEMP] = "temp", 65a980e046SJonathan Cameron [IIO_INCLI] = "incli", 66a980e046SJonathan Cameron [IIO_ROT] = "rot", 67a980e046SJonathan Cameron [IIO_ANGL] = "angl", 68a980e046SJonathan Cameron [IIO_TIMESTAMP] = "timestamp", 69a980e046SJonathan Cameron [IIO_CAPACITANCE] = "capacitance", 70a6b12855SMichael Hennerich [IIO_ALTVOLTAGE] = "altvoltage", 7121cd1fabSJon Brenner [IIO_CCT] = "cct", 72c4f0c693SLars-Peter Clausen [IIO_PRESSURE] = "pressure", 73ac216aa2SHarald Geyer [IIO_HUMIDITYRELATIVE] = "humidityrelative", 7455aebeb9SDaniel Baluta [IIO_ACTIVITY] = "activity", 75a88bfe78SIrina Tirdea [IIO_STEPS] = "steps", 7672c66644SIrina Tirdea [IIO_ENERGY] = "energy", 77cc3c9eecSIrina Tirdea [IIO_DISTANCE] = "distance", 785a1a9329SIrina Tirdea [IIO_VELOCITY] = "velocity", 798ff6b3bcSMatt Ranostay [IIO_CONCENTRATION] = "concentration", 80d38d5469SMatt Ranostay [IIO_RESISTANCE] = "resistance", 81ecb3a7ccSMatt Ranostay [IIO_PH] = "ph", 82*d409404cSPeter Meerwald-Stadler [IIO_UVINDEX] = "uvindex", 83a980e046SJonathan Cameron }; 84a980e046SJonathan Cameron 85a980e046SJonathan Cameron static const char * const iio_modifier_names[] = { 86a980e046SJonathan Cameron [IIO_MOD_X] = "x", 87a980e046SJonathan Cameron [IIO_MOD_Y] = "y", 88a980e046SJonathan Cameron [IIO_MOD_Z] = "z", 894b8d8015SPeter Meerwald [IIO_MOD_X_AND_Y] = "x&y", 904b8d8015SPeter Meerwald [IIO_MOD_X_AND_Z] = "x&z", 914b8d8015SPeter Meerwald [IIO_MOD_Y_AND_Z] = "y&z", 924b8d8015SPeter Meerwald [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z", 934b8d8015SPeter Meerwald [IIO_MOD_X_OR_Y] = "x|y", 944b8d8015SPeter Meerwald [IIO_MOD_X_OR_Z] = "x|z", 954b8d8015SPeter Meerwald [IIO_MOD_Y_OR_Z] = "y|z", 964b8d8015SPeter Meerwald [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z", 978f5879b2SJonathan Cameron [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)", 98cf82cb81SJonathan Cameron [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2", 99a980e046SJonathan Cameron [IIO_MOD_LIGHT_BOTH] = "both", 100a980e046SJonathan Cameron [IIO_MOD_LIGHT_IR] = "ir", 10121cd1fabSJon Brenner [IIO_MOD_LIGHT_CLEAR] = "clear", 10221cd1fabSJon Brenner [IIO_MOD_LIGHT_RED] = "red", 10321cd1fabSJon Brenner [IIO_MOD_LIGHT_GREEN] = "green", 10421cd1fabSJon Brenner [IIO_MOD_LIGHT_BLUE] = "blue", 1052c5ff1f9SPeter Meerwald-Stadler [IIO_MOD_LIGHT_UV] = "uv", 1065082f405SSrinivas Pandruvada [IIO_MOD_QUATERNION] = "quaternion", 107638b43b3SPeter Meerwald [IIO_MOD_TEMP_AMBIENT] = "ambient", 108638b43b3SPeter Meerwald [IIO_MOD_TEMP_OBJECT] = "object", 10911b8ddabSReyad Attiyat [IIO_MOD_NORTH_MAGN] = "from_north_magnetic", 11011b8ddabSReyad Attiyat [IIO_MOD_NORTH_TRUE] = "from_north_true", 11111b8ddabSReyad Attiyat [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp", 11211b8ddabSReyad Attiyat [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp", 11355aebeb9SDaniel Baluta [IIO_MOD_RUNNING] = "running", 11455aebeb9SDaniel Baluta [IIO_MOD_JOGGING] = "jogging", 11555aebeb9SDaniel Baluta [IIO_MOD_WALKING] = "walking", 11655aebeb9SDaniel Baluta [IIO_MOD_STILL] = "still", 1175a1a9329SIrina Tirdea [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)", 1181ce87f21SLars-Peter Clausen [IIO_MOD_I] = "i", 1191ce87f21SLars-Peter Clausen [IIO_MOD_Q] = "q", 1208ff6b3bcSMatt Ranostay [IIO_MOD_CO2] = "co2", 1218ff6b3bcSMatt Ranostay [IIO_MOD_VOC] = "voc", 122a980e046SJonathan Cameron }; 123a980e046SJonathan Cameron 124a980e046SJonathan Cameron /* relies on pairs of these shared then separate */ 125a980e046SJonathan Cameron static const char * const iio_chan_info_postfix[] = { 126a980e046SJonathan Cameron [IIO_CHAN_INFO_RAW] = "raw", 127a980e046SJonathan Cameron [IIO_CHAN_INFO_PROCESSED] = "input", 128a980e046SJonathan Cameron [IIO_CHAN_INFO_SCALE] = "scale", 129a980e046SJonathan Cameron [IIO_CHAN_INFO_OFFSET] = "offset", 130a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBSCALE] = "calibscale", 131a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBBIAS] = "calibbias", 132a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK] = "peak_raw", 133a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale", 134a980e046SJonathan Cameron [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw", 135a980e046SJonathan Cameron [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw", 136a980e046SJonathan Cameron [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY] 137a980e046SJonathan Cameron = "filter_low_pass_3db_frequency", 1383f7f642bSMartin Fuzzey [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY] 1393f7f642bSMartin Fuzzey = "filter_high_pass_3db_frequency", 140a980e046SJonathan Cameron [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency", 141a6b12855SMichael Hennerich [IIO_CHAN_INFO_FREQUENCY] = "frequency", 142a6b12855SMichael Hennerich [IIO_CHAN_INFO_PHASE] = "phase", 143b65d6212SMichael Hennerich [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain", 1447c9ab035Ssrinivas pandruvada [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis", 145899d90bdSPeter Meerwald [IIO_CHAN_INFO_INT_TIME] = "integration_time", 146a88bfe78SIrina Tirdea [IIO_CHAN_INFO_ENABLE] = "en", 147bcdf28fbSIrina Tirdea [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight", 148d37f6836SIrina Tirdea [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight", 1492f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count", 1502f0ecb7cSIrina Tirdea [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time", 151c8a85854SVianney le Clément de Saint-Marcq [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity", 152faaa4495SIrina Tirdea [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio", 153a980e046SJonathan Cameron }; 154a980e046SJonathan Cameron 155a7e57dceSSachin Kamat /** 156a7e57dceSSachin Kamat * iio_find_channel_from_si() - get channel from its scan index 157a7e57dceSSachin Kamat * @indio_dev: device 158a7e57dceSSachin Kamat * @si: scan index to match 159a7e57dceSSachin Kamat */ 160a980e046SJonathan Cameron const struct iio_chan_spec 161a980e046SJonathan Cameron *iio_find_channel_from_si(struct iio_dev *indio_dev, int si) 162a980e046SJonathan Cameron { 163a980e046SJonathan Cameron int i; 164a980e046SJonathan Cameron 165a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) 166a980e046SJonathan Cameron if (indio_dev->channels[i].scan_index == si) 167a980e046SJonathan Cameron return &indio_dev->channels[i]; 168a980e046SJonathan Cameron return NULL; 169a980e046SJonathan Cameron } 170a980e046SJonathan Cameron 171a980e046SJonathan Cameron /* This turns up an awful lot */ 172a980e046SJonathan Cameron ssize_t iio_read_const_attr(struct device *dev, 173a980e046SJonathan Cameron struct device_attribute *attr, 174a980e046SJonathan Cameron char *buf) 175a980e046SJonathan Cameron { 176a980e046SJonathan Cameron return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string); 177a980e046SJonathan Cameron } 178a980e046SJonathan Cameron EXPORT_SYMBOL(iio_read_const_attr); 179a980e046SJonathan Cameron 180a980e046SJonathan Cameron static int __init iio_init(void) 181a980e046SJonathan Cameron { 182a980e046SJonathan Cameron int ret; 183a980e046SJonathan Cameron 184a980e046SJonathan Cameron /* Register sysfs bus */ 185a980e046SJonathan Cameron ret = bus_register(&iio_bus_type); 186a980e046SJonathan Cameron if (ret < 0) { 1873176dd5dSSachin Kamat pr_err("could not register bus type\n"); 188a980e046SJonathan Cameron goto error_nothing; 189a980e046SJonathan Cameron } 190a980e046SJonathan Cameron 191a980e046SJonathan Cameron ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio"); 192a980e046SJonathan Cameron if (ret < 0) { 1933176dd5dSSachin Kamat pr_err("failed to allocate char dev region\n"); 194a980e046SJonathan Cameron goto error_unregister_bus_type; 195a980e046SJonathan Cameron } 196a980e046SJonathan Cameron 197a980e046SJonathan Cameron iio_debugfs_dentry = debugfs_create_dir("iio", NULL); 198a980e046SJonathan Cameron 199a980e046SJonathan Cameron return 0; 200a980e046SJonathan Cameron 201a980e046SJonathan Cameron error_unregister_bus_type: 202a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 203a980e046SJonathan Cameron error_nothing: 204a980e046SJonathan Cameron return ret; 205a980e046SJonathan Cameron } 206a980e046SJonathan Cameron 207a980e046SJonathan Cameron static void __exit iio_exit(void) 208a980e046SJonathan Cameron { 209a980e046SJonathan Cameron if (iio_devt) 210a980e046SJonathan Cameron unregister_chrdev_region(iio_devt, IIO_DEV_MAX); 211a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 212a980e046SJonathan Cameron debugfs_remove(iio_debugfs_dentry); 213a980e046SJonathan Cameron } 214a980e046SJonathan Cameron 215a980e046SJonathan Cameron #if defined(CONFIG_DEBUG_FS) 216a980e046SJonathan Cameron static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf, 217a980e046SJonathan Cameron size_t count, loff_t *ppos) 218a980e046SJonathan Cameron { 219a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 220a980e046SJonathan Cameron char buf[20]; 221a980e046SJonathan Cameron unsigned val = 0; 222a980e046SJonathan Cameron ssize_t len; 223a980e046SJonathan Cameron int ret; 224a980e046SJonathan Cameron 225a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, 226a980e046SJonathan Cameron indio_dev->cached_reg_addr, 227a980e046SJonathan Cameron 0, &val); 228a980e046SJonathan Cameron if (ret) 229a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__); 230a980e046SJonathan Cameron 231a980e046SJonathan Cameron len = snprintf(buf, sizeof(buf), "0x%X\n", val); 232a980e046SJonathan Cameron 233a980e046SJonathan Cameron return simple_read_from_buffer(userbuf, count, ppos, buf, len); 234a980e046SJonathan Cameron } 235a980e046SJonathan Cameron 236a980e046SJonathan Cameron static ssize_t iio_debugfs_write_reg(struct file *file, 237a980e046SJonathan Cameron const char __user *userbuf, size_t count, loff_t *ppos) 238a980e046SJonathan Cameron { 239a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 240a980e046SJonathan Cameron unsigned reg, val; 241a980e046SJonathan Cameron char buf[80]; 242a980e046SJonathan Cameron int ret; 243a980e046SJonathan Cameron 244a980e046SJonathan Cameron count = min_t(size_t, count, (sizeof(buf)-1)); 245a980e046SJonathan Cameron if (copy_from_user(buf, userbuf, count)) 246a980e046SJonathan Cameron return -EFAULT; 247a980e046SJonathan Cameron 248a980e046SJonathan Cameron buf[count] = 0; 249a980e046SJonathan Cameron 250a980e046SJonathan Cameron ret = sscanf(buf, "%i %i", ®, &val); 251a980e046SJonathan Cameron 252a980e046SJonathan Cameron switch (ret) { 253a980e046SJonathan Cameron case 1: 254a980e046SJonathan Cameron indio_dev->cached_reg_addr = reg; 255a980e046SJonathan Cameron break; 256a980e046SJonathan Cameron case 2: 257a980e046SJonathan Cameron indio_dev->cached_reg_addr = reg; 258a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, reg, 259a980e046SJonathan Cameron val, NULL); 260a980e046SJonathan Cameron if (ret) { 261a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: write failed\n", 262a980e046SJonathan Cameron __func__); 263a980e046SJonathan Cameron return ret; 264a980e046SJonathan Cameron } 265a980e046SJonathan Cameron break; 266a980e046SJonathan Cameron default: 267a980e046SJonathan Cameron return -EINVAL; 268a980e046SJonathan Cameron } 269a980e046SJonathan Cameron 270a980e046SJonathan Cameron return count; 271a980e046SJonathan Cameron } 272a980e046SJonathan Cameron 273a980e046SJonathan Cameron static const struct file_operations iio_debugfs_reg_fops = { 2745a28c873SAxel Lin .open = simple_open, 275a980e046SJonathan Cameron .read = iio_debugfs_read_reg, 276a980e046SJonathan Cameron .write = iio_debugfs_write_reg, 277a980e046SJonathan Cameron }; 278a980e046SJonathan Cameron 279a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 280a980e046SJonathan Cameron { 281a980e046SJonathan Cameron debugfs_remove_recursive(indio_dev->debugfs_dentry); 282a980e046SJonathan Cameron } 283a980e046SJonathan Cameron 284a980e046SJonathan Cameron static int iio_device_register_debugfs(struct iio_dev *indio_dev) 285a980e046SJonathan Cameron { 286a980e046SJonathan Cameron struct dentry *d; 287a980e046SJonathan Cameron 288a980e046SJonathan Cameron if (indio_dev->info->debugfs_reg_access == NULL) 289a980e046SJonathan Cameron return 0; 290a980e046SJonathan Cameron 291abd5a2fbSAxel Lin if (!iio_debugfs_dentry) 292a980e046SJonathan Cameron return 0; 293a980e046SJonathan Cameron 294a980e046SJonathan Cameron indio_dev->debugfs_dentry = 295a980e046SJonathan Cameron debugfs_create_dir(dev_name(&indio_dev->dev), 296a980e046SJonathan Cameron iio_debugfs_dentry); 297a980e046SJonathan Cameron if (indio_dev->debugfs_dentry == NULL) { 298a980e046SJonathan Cameron dev_warn(indio_dev->dev.parent, 299a980e046SJonathan Cameron "Failed to create debugfs directory\n"); 300a980e046SJonathan Cameron return -EFAULT; 301a980e046SJonathan Cameron } 302a980e046SJonathan Cameron 303a980e046SJonathan Cameron d = debugfs_create_file("direct_reg_access", 0644, 304a980e046SJonathan Cameron indio_dev->debugfs_dentry, 305a980e046SJonathan Cameron indio_dev, &iio_debugfs_reg_fops); 306a980e046SJonathan Cameron if (!d) { 307a980e046SJonathan Cameron iio_device_unregister_debugfs(indio_dev); 308a980e046SJonathan Cameron return -ENOMEM; 309a980e046SJonathan Cameron } 310a980e046SJonathan Cameron 311a980e046SJonathan Cameron return 0; 312a980e046SJonathan Cameron } 313a980e046SJonathan Cameron #else 314a980e046SJonathan Cameron static int iio_device_register_debugfs(struct iio_dev *indio_dev) 315a980e046SJonathan Cameron { 316a980e046SJonathan Cameron return 0; 317a980e046SJonathan Cameron } 318a980e046SJonathan Cameron 319a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 320a980e046SJonathan Cameron { 321a980e046SJonathan Cameron } 322a980e046SJonathan Cameron #endif /* CONFIG_DEBUG_FS */ 323a980e046SJonathan Cameron 324a980e046SJonathan Cameron static ssize_t iio_read_channel_ext_info(struct device *dev, 325a980e046SJonathan Cameron struct device_attribute *attr, 326a980e046SJonathan Cameron char *buf) 327a980e046SJonathan Cameron { 328e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 329a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 330a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 331a980e046SJonathan Cameron 332a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 333a980e046SJonathan Cameron 334fc6d1139SMichael Hennerich return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf); 335a980e046SJonathan Cameron } 336a980e046SJonathan Cameron 337a980e046SJonathan Cameron static ssize_t iio_write_channel_ext_info(struct device *dev, 338a980e046SJonathan Cameron struct device_attribute *attr, 339a980e046SJonathan Cameron const char *buf, 340a980e046SJonathan Cameron size_t len) 341a980e046SJonathan Cameron { 342e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 343a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 344a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 345a980e046SJonathan Cameron 346a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 347a980e046SJonathan Cameron 348fc6d1139SMichael Hennerich return ext_info->write(indio_dev, ext_info->private, 349fc6d1139SMichael Hennerich this_attr->c, buf, len); 350a980e046SJonathan Cameron } 351a980e046SJonathan Cameron 3525212cc8aSLars-Peter Clausen ssize_t iio_enum_available_read(struct iio_dev *indio_dev, 3535212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 3545212cc8aSLars-Peter Clausen { 3555212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3565212cc8aSLars-Peter Clausen unsigned int i; 3575212cc8aSLars-Peter Clausen size_t len = 0; 3585212cc8aSLars-Peter Clausen 3595212cc8aSLars-Peter Clausen if (!e->num_items) 3605212cc8aSLars-Peter Clausen return 0; 3615212cc8aSLars-Peter Clausen 3625212cc8aSLars-Peter Clausen for (i = 0; i < e->num_items; ++i) 36374dcd439SLars-Peter Clausen len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]); 3645212cc8aSLars-Peter Clausen 3655212cc8aSLars-Peter Clausen /* replace last space with a newline */ 3665212cc8aSLars-Peter Clausen buf[len - 1] = '\n'; 3675212cc8aSLars-Peter Clausen 3685212cc8aSLars-Peter Clausen return len; 3695212cc8aSLars-Peter Clausen } 3705212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_available_read); 3715212cc8aSLars-Peter Clausen 3725212cc8aSLars-Peter Clausen ssize_t iio_enum_read(struct iio_dev *indio_dev, 3735212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 3745212cc8aSLars-Peter Clausen { 3755212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3765212cc8aSLars-Peter Clausen int i; 3775212cc8aSLars-Peter Clausen 3785212cc8aSLars-Peter Clausen if (!e->get) 3795212cc8aSLars-Peter Clausen return -EINVAL; 3805212cc8aSLars-Peter Clausen 3815212cc8aSLars-Peter Clausen i = e->get(indio_dev, chan); 3825212cc8aSLars-Peter Clausen if (i < 0) 3835212cc8aSLars-Peter Clausen return i; 3845212cc8aSLars-Peter Clausen else if (i >= e->num_items) 3855212cc8aSLars-Peter Clausen return -EINVAL; 3865212cc8aSLars-Peter Clausen 387598db581SKees Cook return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]); 3885212cc8aSLars-Peter Clausen } 3895212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_read); 3905212cc8aSLars-Peter Clausen 3915212cc8aSLars-Peter Clausen ssize_t iio_enum_write(struct iio_dev *indio_dev, 3925212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, const char *buf, 3935212cc8aSLars-Peter Clausen size_t len) 3945212cc8aSLars-Peter Clausen { 3955212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3965212cc8aSLars-Peter Clausen unsigned int i; 3975212cc8aSLars-Peter Clausen int ret; 3985212cc8aSLars-Peter Clausen 3995212cc8aSLars-Peter Clausen if (!e->set) 4005212cc8aSLars-Peter Clausen return -EINVAL; 4015212cc8aSLars-Peter Clausen 4025212cc8aSLars-Peter Clausen for (i = 0; i < e->num_items; i++) { 4035212cc8aSLars-Peter Clausen if (sysfs_streq(buf, e->items[i])) 4045212cc8aSLars-Peter Clausen break; 4055212cc8aSLars-Peter Clausen } 4065212cc8aSLars-Peter Clausen 4075212cc8aSLars-Peter Clausen if (i == e->num_items) 4085212cc8aSLars-Peter Clausen return -EINVAL; 4095212cc8aSLars-Peter Clausen 4105212cc8aSLars-Peter Clausen ret = e->set(indio_dev, chan, i); 4115212cc8aSLars-Peter Clausen return ret ? ret : len; 4125212cc8aSLars-Peter Clausen } 4135212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_write); 4145212cc8aSLars-Peter Clausen 4153661f3f5SLars-Peter Clausen /** 4163661f3f5SLars-Peter Clausen * iio_format_value() - Formats a IIO value into its string representation 4172498dcf6SCristina Opriceana * @buf: The buffer to which the formatted value gets written 4182498dcf6SCristina Opriceana * @type: One of the IIO_VAL_... constants. This decides how the val 4192498dcf6SCristina Opriceana * and val2 parameters are formatted. 4202498dcf6SCristina Opriceana * @size: Number of IIO value entries contained in vals 4212498dcf6SCristina Opriceana * @vals: Pointer to the values, exact meaning depends on the 4222498dcf6SCristina Opriceana * type parameter. 4232498dcf6SCristina Opriceana * 4242498dcf6SCristina Opriceana * Return: 0 by default, a negative number on failure or the 4252498dcf6SCristina Opriceana * total number of characters written for a type that belongs 4262498dcf6SCristina Opriceana * to the IIO_VAL_... constant. 4273661f3f5SLars-Peter Clausen */ 4289fbfb4b3SSrinivas Pandruvada ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals) 429a980e046SJonathan Cameron { 4307985e7c1SLars-Peter Clausen unsigned long long tmp; 43167eedba3SMichael Hennerich bool scale_db = false; 432a980e046SJonathan Cameron 4333661f3f5SLars-Peter Clausen switch (type) { 43467eedba3SMichael Hennerich case IIO_VAL_INT: 4359fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d\n", vals[0]); 43667eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_MICRO_DB: 43767eedba3SMichael Hennerich scale_db = true; 43867eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_MICRO: 4399fbfb4b3SSrinivas Pandruvada if (vals[1] < 0) 4408f57e4d9SMichal Nazarewicz return sprintf(buf, "-%d.%06u%s\n", abs(vals[0]), 4418f57e4d9SMichal Nazarewicz -vals[1], scale_db ? " dB" : ""); 442a980e046SJonathan Cameron else 4439fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1], 44467eedba3SMichael Hennerich scale_db ? " dB" : ""); 44567eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_NANO: 4469fbfb4b3SSrinivas Pandruvada if (vals[1] < 0) 4478f57e4d9SMichal Nazarewicz return sprintf(buf, "-%d.%09u\n", abs(vals[0]), 4489fbfb4b3SSrinivas Pandruvada -vals[1]); 449a980e046SJonathan Cameron else 4509fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 4517985e7c1SLars-Peter Clausen case IIO_VAL_FRACTIONAL: 4529fbfb4b3SSrinivas Pandruvada tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]); 4539fbfb4b3SSrinivas Pandruvada vals[1] = do_div(tmp, 1000000000LL); 4549fbfb4b3SSrinivas Pandruvada vals[0] = tmp; 4559fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 456103d9fb9SLars-Peter Clausen case IIO_VAL_FRACTIONAL_LOG2: 4579fbfb4b3SSrinivas Pandruvada tmp = (s64)vals[0] * 1000000000LL >> vals[1]; 4589fbfb4b3SSrinivas Pandruvada vals[1] = do_div(tmp, 1000000000LL); 4599fbfb4b3SSrinivas Pandruvada vals[0] = tmp; 4609fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 4619fbfb4b3SSrinivas Pandruvada case IIO_VAL_INT_MULTIPLE: 4629fbfb4b3SSrinivas Pandruvada { 4639fbfb4b3SSrinivas Pandruvada int i; 4649fbfb4b3SSrinivas Pandruvada int len = 0; 4659fbfb4b3SSrinivas Pandruvada 4669fbfb4b3SSrinivas Pandruvada for (i = 0; i < size; ++i) 4679fbfb4b3SSrinivas Pandruvada len += snprintf(&buf[len], PAGE_SIZE - len, "%d ", 4689fbfb4b3SSrinivas Pandruvada vals[i]); 4699fbfb4b3SSrinivas Pandruvada len += snprintf(&buf[len], PAGE_SIZE - len, "\n"); 4709fbfb4b3SSrinivas Pandruvada return len; 4719fbfb4b3SSrinivas Pandruvada } 47267eedba3SMichael Hennerich default: 473a980e046SJonathan Cameron return 0; 474a980e046SJonathan Cameron } 47567eedba3SMichael Hennerich } 4767d2c2acaSAndrew F. Davis EXPORT_SYMBOL_GPL(iio_format_value); 477a980e046SJonathan Cameron 4783661f3f5SLars-Peter Clausen static ssize_t iio_read_channel_info(struct device *dev, 4793661f3f5SLars-Peter Clausen struct device_attribute *attr, 4803661f3f5SLars-Peter Clausen char *buf) 4813661f3f5SLars-Peter Clausen { 4823661f3f5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 4833661f3f5SLars-Peter Clausen struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 4849fbfb4b3SSrinivas Pandruvada int vals[INDIO_MAX_RAW_ELEMENTS]; 4859fbfb4b3SSrinivas Pandruvada int ret; 4869fbfb4b3SSrinivas Pandruvada int val_len = 2; 4879fbfb4b3SSrinivas Pandruvada 4889fbfb4b3SSrinivas Pandruvada if (indio_dev->info->read_raw_multi) 4899fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c, 4909fbfb4b3SSrinivas Pandruvada INDIO_MAX_RAW_ELEMENTS, 4919fbfb4b3SSrinivas Pandruvada vals, &val_len, 4929fbfb4b3SSrinivas Pandruvada this_attr->address); 4939fbfb4b3SSrinivas Pandruvada else 4949fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw(indio_dev, this_attr->c, 4959fbfb4b3SSrinivas Pandruvada &vals[0], &vals[1], this_attr->address); 4963661f3f5SLars-Peter Clausen 4973661f3f5SLars-Peter Clausen if (ret < 0) 4983661f3f5SLars-Peter Clausen return ret; 4993661f3f5SLars-Peter Clausen 5009fbfb4b3SSrinivas Pandruvada return iio_format_value(buf, ret, val_len, vals); 5013661f3f5SLars-Peter Clausen } 5023661f3f5SLars-Peter Clausen 5036807d721SLars-Peter Clausen /** 5046807d721SLars-Peter Clausen * iio_str_to_fixpoint() - Parse a fixed-point number from a string 5056807d721SLars-Peter Clausen * @str: The string to parse 5066807d721SLars-Peter Clausen * @fract_mult: Multiplier for the first decimal place, should be a power of 10 5076807d721SLars-Peter Clausen * @integer: The integer part of the number 5086807d721SLars-Peter Clausen * @fract: The fractional part of the number 5096807d721SLars-Peter Clausen * 5106807d721SLars-Peter Clausen * Returns 0 on success, or a negative error code if the string could not be 5116807d721SLars-Peter Clausen * parsed. 5126807d721SLars-Peter Clausen */ 5136807d721SLars-Peter Clausen int iio_str_to_fixpoint(const char *str, int fract_mult, 5146807d721SLars-Peter Clausen int *integer, int *fract) 5156807d721SLars-Peter Clausen { 5166807d721SLars-Peter Clausen int i = 0, f = 0; 5176807d721SLars-Peter Clausen bool integer_part = true, negative = false; 5186807d721SLars-Peter Clausen 519f47dff32SSean Nyekjaer if (fract_mult == 0) { 520f47dff32SSean Nyekjaer *fract = 0; 521f47dff32SSean Nyekjaer 522f47dff32SSean Nyekjaer return kstrtoint(str, 0, integer); 523f47dff32SSean Nyekjaer } 524f47dff32SSean Nyekjaer 5256807d721SLars-Peter Clausen if (str[0] == '-') { 5266807d721SLars-Peter Clausen negative = true; 5276807d721SLars-Peter Clausen str++; 5286807d721SLars-Peter Clausen } else if (str[0] == '+') { 5296807d721SLars-Peter Clausen str++; 5306807d721SLars-Peter Clausen } 5316807d721SLars-Peter Clausen 5326807d721SLars-Peter Clausen while (*str) { 5336807d721SLars-Peter Clausen if ('0' <= *str && *str <= '9') { 5346807d721SLars-Peter Clausen if (integer_part) { 5356807d721SLars-Peter Clausen i = i * 10 + *str - '0'; 5366807d721SLars-Peter Clausen } else { 5376807d721SLars-Peter Clausen f += fract_mult * (*str - '0'); 5386807d721SLars-Peter Clausen fract_mult /= 10; 5396807d721SLars-Peter Clausen } 5406807d721SLars-Peter Clausen } else if (*str == '\n') { 5416807d721SLars-Peter Clausen if (*(str + 1) == '\0') 5426807d721SLars-Peter Clausen break; 5436807d721SLars-Peter Clausen else 5446807d721SLars-Peter Clausen return -EINVAL; 5456807d721SLars-Peter Clausen } else if (*str == '.' && integer_part) { 5466807d721SLars-Peter Clausen integer_part = false; 5476807d721SLars-Peter Clausen } else { 5486807d721SLars-Peter Clausen return -EINVAL; 5496807d721SLars-Peter Clausen } 5506807d721SLars-Peter Clausen str++; 5516807d721SLars-Peter Clausen } 5526807d721SLars-Peter Clausen 5536807d721SLars-Peter Clausen if (negative) { 5546807d721SLars-Peter Clausen if (i) 5556807d721SLars-Peter Clausen i = -i; 5566807d721SLars-Peter Clausen else 5576807d721SLars-Peter Clausen f = -f; 5586807d721SLars-Peter Clausen } 5596807d721SLars-Peter Clausen 5606807d721SLars-Peter Clausen *integer = i; 5616807d721SLars-Peter Clausen *fract = f; 5626807d721SLars-Peter Clausen 5636807d721SLars-Peter Clausen return 0; 5646807d721SLars-Peter Clausen } 5656807d721SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_str_to_fixpoint); 5666807d721SLars-Peter Clausen 567a980e046SJonathan Cameron static ssize_t iio_write_channel_info(struct device *dev, 568a980e046SJonathan Cameron struct device_attribute *attr, 569a980e046SJonathan Cameron const char *buf, 570a980e046SJonathan Cameron size_t len) 571a980e046SJonathan Cameron { 572e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 573a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 5746807d721SLars-Peter Clausen int ret, fract_mult = 100000; 5756807d721SLars-Peter Clausen int integer, fract; 576a980e046SJonathan Cameron 577a980e046SJonathan Cameron /* Assumes decimal - precision based on number of digits */ 578a980e046SJonathan Cameron if (!indio_dev->info->write_raw) 579a980e046SJonathan Cameron return -EINVAL; 580a980e046SJonathan Cameron 581a980e046SJonathan Cameron if (indio_dev->info->write_raw_get_fmt) 582a980e046SJonathan Cameron switch (indio_dev->info->write_raw_get_fmt(indio_dev, 583a980e046SJonathan Cameron this_attr->c, this_attr->address)) { 584f47dff32SSean Nyekjaer case IIO_VAL_INT: 585f47dff32SSean Nyekjaer fract_mult = 0; 586f47dff32SSean Nyekjaer break; 587a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_MICRO: 588a980e046SJonathan Cameron fract_mult = 100000; 589a980e046SJonathan Cameron break; 590a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_NANO: 591a980e046SJonathan Cameron fract_mult = 100000000; 592a980e046SJonathan Cameron break; 593a980e046SJonathan Cameron default: 594a980e046SJonathan Cameron return -EINVAL; 595a980e046SJonathan Cameron } 596a980e046SJonathan Cameron 5976807d721SLars-Peter Clausen ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract); 5986807d721SLars-Peter Clausen if (ret) 5996807d721SLars-Peter Clausen return ret; 600a980e046SJonathan Cameron 601a980e046SJonathan Cameron ret = indio_dev->info->write_raw(indio_dev, this_attr->c, 602a980e046SJonathan Cameron integer, fract, this_attr->address); 603a980e046SJonathan Cameron if (ret) 604a980e046SJonathan Cameron return ret; 605a980e046SJonathan Cameron 606a980e046SJonathan Cameron return len; 607a980e046SJonathan Cameron } 608a980e046SJonathan Cameron 609a980e046SJonathan Cameron static 610a980e046SJonathan Cameron int __iio_device_attr_init(struct device_attribute *dev_attr, 611a980e046SJonathan Cameron const char *postfix, 612a980e046SJonathan Cameron struct iio_chan_spec const *chan, 613a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 614a980e046SJonathan Cameron struct device_attribute *attr, 615a980e046SJonathan Cameron char *buf), 616a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 617a980e046SJonathan Cameron struct device_attribute *attr, 618a980e046SJonathan Cameron const char *buf, 619a980e046SJonathan Cameron size_t len), 6203704432fSJonathan Cameron enum iio_shared_by shared_by) 621a980e046SJonathan Cameron { 6223704432fSJonathan Cameron int ret = 0; 6237bbcf7e1SLars-Peter Clausen char *name = NULL; 6243704432fSJonathan Cameron char *full_postfix; 625a980e046SJonathan Cameron sysfs_attr_init(&dev_attr->attr); 626a980e046SJonathan Cameron 627a980e046SJonathan Cameron /* Build up postfix of <extend_name>_<modifier>_postfix */ 6283704432fSJonathan Cameron if (chan->modified && (shared_by == IIO_SEPARATE)) { 629a980e046SJonathan Cameron if (chan->extend_name) 630a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", 631a980e046SJonathan Cameron iio_modifier_names[chan 632a980e046SJonathan Cameron ->channel2], 633a980e046SJonathan Cameron chan->extend_name, 634a980e046SJonathan Cameron postfix); 635a980e046SJonathan Cameron else 636a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s", 637a980e046SJonathan Cameron iio_modifier_names[chan 638a980e046SJonathan Cameron ->channel2], 639a980e046SJonathan Cameron postfix); 640a980e046SJonathan Cameron } else { 64177bfa8baSLars-Peter Clausen if (chan->extend_name == NULL || shared_by != IIO_SEPARATE) 642a980e046SJonathan Cameron full_postfix = kstrdup(postfix, GFP_KERNEL); 643a980e046SJonathan Cameron else 644a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, 645a980e046SJonathan Cameron "%s_%s", 646a980e046SJonathan Cameron chan->extend_name, 647a980e046SJonathan Cameron postfix); 648a980e046SJonathan Cameron } 6493704432fSJonathan Cameron if (full_postfix == NULL) 6503704432fSJonathan Cameron return -ENOMEM; 651a980e046SJonathan Cameron 652a980e046SJonathan Cameron if (chan->differential) { /* Differential can not have modifier */ 6533704432fSJonathan Cameron switch (shared_by) { 654c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 6557bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 656c006ec83SJonathan Cameron break; 657c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 6587bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 659c006ec83SJonathan Cameron iio_direction[chan->output], 660c006ec83SJonathan Cameron full_postfix); 661c006ec83SJonathan Cameron break; 6623704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 6637bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s", 664a980e046SJonathan Cameron iio_direction[chan->output], 665a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 666a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 667a980e046SJonathan Cameron full_postfix); 6683704432fSJonathan Cameron break; 6693704432fSJonathan Cameron case IIO_SEPARATE: 6703704432fSJonathan Cameron if (!chan->indexed) { 671231bfe53SDan Carpenter WARN(1, "Differential channels must be indexed\n"); 6723704432fSJonathan Cameron ret = -EINVAL; 6733704432fSJonathan Cameron goto error_free_full_postfix; 6743704432fSJonathan Cameron } 6757bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, 6763704432fSJonathan Cameron "%s_%s%d-%s%d_%s", 677a980e046SJonathan Cameron iio_direction[chan->output], 678a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 679a980e046SJonathan Cameron chan->channel, 680a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 681a980e046SJonathan Cameron chan->channel2, 682a980e046SJonathan Cameron full_postfix); 6833704432fSJonathan Cameron break; 684a980e046SJonathan Cameron } 685a980e046SJonathan Cameron } else { /* Single ended */ 6863704432fSJonathan Cameron switch (shared_by) { 687c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 6887bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 689c006ec83SJonathan Cameron break; 690c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 6917bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 692c006ec83SJonathan Cameron iio_direction[chan->output], 693c006ec83SJonathan Cameron full_postfix); 694c006ec83SJonathan Cameron break; 6953704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 6967bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 697a980e046SJonathan Cameron iio_direction[chan->output], 698a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 699a980e046SJonathan Cameron full_postfix); 7003704432fSJonathan Cameron break; 7013704432fSJonathan Cameron 7023704432fSJonathan Cameron case IIO_SEPARATE: 7033704432fSJonathan Cameron if (chan->indexed) 7047bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s%d_%s", 705a980e046SJonathan Cameron iio_direction[chan->output], 706a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 707a980e046SJonathan Cameron chan->channel, 708a980e046SJonathan Cameron full_postfix); 709a980e046SJonathan Cameron else 7107bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 711a980e046SJonathan Cameron iio_direction[chan->output], 712a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 713a980e046SJonathan Cameron full_postfix); 7143704432fSJonathan Cameron break; 7153704432fSJonathan Cameron } 716a980e046SJonathan Cameron } 7177bbcf7e1SLars-Peter Clausen if (name == NULL) { 718a980e046SJonathan Cameron ret = -ENOMEM; 719a980e046SJonathan Cameron goto error_free_full_postfix; 720a980e046SJonathan Cameron } 7217bbcf7e1SLars-Peter Clausen dev_attr->attr.name = name; 722a980e046SJonathan Cameron 723a980e046SJonathan Cameron if (readfunc) { 724a980e046SJonathan Cameron dev_attr->attr.mode |= S_IRUGO; 725a980e046SJonathan Cameron dev_attr->show = readfunc; 726a980e046SJonathan Cameron } 727a980e046SJonathan Cameron 728a980e046SJonathan Cameron if (writefunc) { 729a980e046SJonathan Cameron dev_attr->attr.mode |= S_IWUSR; 730a980e046SJonathan Cameron dev_attr->store = writefunc; 731a980e046SJonathan Cameron } 7327bbcf7e1SLars-Peter Clausen 733a980e046SJonathan Cameron error_free_full_postfix: 734a980e046SJonathan Cameron kfree(full_postfix); 7353704432fSJonathan Cameron 736a980e046SJonathan Cameron return ret; 737a980e046SJonathan Cameron } 738a980e046SJonathan Cameron 739a980e046SJonathan Cameron static void __iio_device_attr_deinit(struct device_attribute *dev_attr) 740a980e046SJonathan Cameron { 741a980e046SJonathan Cameron kfree(dev_attr->attr.name); 742a980e046SJonathan Cameron } 743a980e046SJonathan Cameron 744a980e046SJonathan Cameron int __iio_add_chan_devattr(const char *postfix, 745a980e046SJonathan Cameron struct iio_chan_spec const *chan, 746a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 747a980e046SJonathan Cameron struct device_attribute *attr, 748a980e046SJonathan Cameron char *buf), 749a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 750a980e046SJonathan Cameron struct device_attribute *attr, 751a980e046SJonathan Cameron const char *buf, 752a980e046SJonathan Cameron size_t len), 753a980e046SJonathan Cameron u64 mask, 7543704432fSJonathan Cameron enum iio_shared_by shared_by, 755a980e046SJonathan Cameron struct device *dev, 756a980e046SJonathan Cameron struct list_head *attr_list) 757a980e046SJonathan Cameron { 758a980e046SJonathan Cameron int ret; 759a980e046SJonathan Cameron struct iio_dev_attr *iio_attr, *t; 760a980e046SJonathan Cameron 761670c1103SSachin Kamat iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL); 76292825ff9SHartmut Knaack if (iio_attr == NULL) 76392825ff9SHartmut Knaack return -ENOMEM; 764a980e046SJonathan Cameron ret = __iio_device_attr_init(&iio_attr->dev_attr, 765a980e046SJonathan Cameron postfix, chan, 7663704432fSJonathan Cameron readfunc, writefunc, shared_by); 767a980e046SJonathan Cameron if (ret) 768a980e046SJonathan Cameron goto error_iio_dev_attr_free; 769a980e046SJonathan Cameron iio_attr->c = chan; 770a980e046SJonathan Cameron iio_attr->address = mask; 771a980e046SJonathan Cameron list_for_each_entry(t, attr_list, l) 772a980e046SJonathan Cameron if (strcmp(t->dev_attr.attr.name, 773a980e046SJonathan Cameron iio_attr->dev_attr.attr.name) == 0) { 7743704432fSJonathan Cameron if (shared_by == IIO_SEPARATE) 775a980e046SJonathan Cameron dev_err(dev, "tried to double register : %s\n", 776a980e046SJonathan Cameron t->dev_attr.attr.name); 777a980e046SJonathan Cameron ret = -EBUSY; 778a980e046SJonathan Cameron goto error_device_attr_deinit; 779a980e046SJonathan Cameron } 780a980e046SJonathan Cameron list_add(&iio_attr->l, attr_list); 781a980e046SJonathan Cameron 782a980e046SJonathan Cameron return 0; 783a980e046SJonathan Cameron 784a980e046SJonathan Cameron error_device_attr_deinit: 785a980e046SJonathan Cameron __iio_device_attr_deinit(&iio_attr->dev_attr); 786a980e046SJonathan Cameron error_iio_dev_attr_free: 787a980e046SJonathan Cameron kfree(iio_attr); 788a980e046SJonathan Cameron return ret; 789a980e046SJonathan Cameron } 790a980e046SJonathan Cameron 7913704432fSJonathan Cameron static int iio_device_add_info_mask_type(struct iio_dev *indio_dev, 7923704432fSJonathan Cameron struct iio_chan_spec const *chan, 7933704432fSJonathan Cameron enum iio_shared_by shared_by, 7943704432fSJonathan Cameron const long *infomask) 7953704432fSJonathan Cameron { 7963704432fSJonathan Cameron int i, ret, attrcount = 0; 7973704432fSJonathan Cameron 7983704432fSJonathan Cameron for_each_set_bit(i, infomask, sizeof(infomask)*8) { 799ef4b4856SJonathan Cameron if (i >= ARRAY_SIZE(iio_chan_info_postfix)) 800ef4b4856SJonathan Cameron return -EINVAL; 8013704432fSJonathan Cameron ret = __iio_add_chan_devattr(iio_chan_info_postfix[i], 8023704432fSJonathan Cameron chan, 8033704432fSJonathan Cameron &iio_read_channel_info, 8043704432fSJonathan Cameron &iio_write_channel_info, 8053704432fSJonathan Cameron i, 8063704432fSJonathan Cameron shared_by, 8073704432fSJonathan Cameron &indio_dev->dev, 8083704432fSJonathan Cameron &indio_dev->channel_attr_list); 8093704432fSJonathan Cameron if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) 8103704432fSJonathan Cameron continue; 8113704432fSJonathan Cameron else if (ret < 0) 8123704432fSJonathan Cameron return ret; 8133704432fSJonathan Cameron attrcount++; 8143704432fSJonathan Cameron } 8153704432fSJonathan Cameron 8163704432fSJonathan Cameron return attrcount; 8173704432fSJonathan Cameron } 8183704432fSJonathan Cameron 819a980e046SJonathan Cameron static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev, 820a980e046SJonathan Cameron struct iio_chan_spec const *chan) 821a980e046SJonathan Cameron { 822a980e046SJonathan Cameron int ret, attrcount = 0; 823a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 824a980e046SJonathan Cameron 825a980e046SJonathan Cameron if (chan->channel < 0) 826a980e046SJonathan Cameron return 0; 8273704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 8283704432fSJonathan Cameron IIO_SEPARATE, 8293704432fSJonathan Cameron &chan->info_mask_separate); 8308655cc49SJonathan Cameron if (ret < 0) 8313704432fSJonathan Cameron return ret; 8323704432fSJonathan Cameron attrcount += ret; 8333704432fSJonathan Cameron 8343704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 8353704432fSJonathan Cameron IIO_SHARED_BY_TYPE, 8363704432fSJonathan Cameron &chan->info_mask_shared_by_type); 8373704432fSJonathan Cameron if (ret < 0) 8383704432fSJonathan Cameron return ret; 8393704432fSJonathan Cameron attrcount += ret; 840a980e046SJonathan Cameron 841c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 842c006ec83SJonathan Cameron IIO_SHARED_BY_DIR, 843c006ec83SJonathan Cameron &chan->info_mask_shared_by_dir); 844c006ec83SJonathan Cameron if (ret < 0) 845c006ec83SJonathan Cameron return ret; 846c006ec83SJonathan Cameron attrcount += ret; 847c006ec83SJonathan Cameron 848c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 849c006ec83SJonathan Cameron IIO_SHARED_BY_ALL, 850c006ec83SJonathan Cameron &chan->info_mask_shared_by_all); 851c006ec83SJonathan Cameron if (ret < 0) 852c006ec83SJonathan Cameron return ret; 853c006ec83SJonathan Cameron attrcount += ret; 854c006ec83SJonathan Cameron 855a980e046SJonathan Cameron if (chan->ext_info) { 856a980e046SJonathan Cameron unsigned int i = 0; 857a980e046SJonathan Cameron for (ext_info = chan->ext_info; ext_info->name; ext_info++) { 858a980e046SJonathan Cameron ret = __iio_add_chan_devattr(ext_info->name, 859a980e046SJonathan Cameron chan, 860a980e046SJonathan Cameron ext_info->read ? 861a980e046SJonathan Cameron &iio_read_channel_ext_info : NULL, 862a980e046SJonathan Cameron ext_info->write ? 863a980e046SJonathan Cameron &iio_write_channel_ext_info : NULL, 864a980e046SJonathan Cameron i, 865a980e046SJonathan Cameron ext_info->shared, 866a980e046SJonathan Cameron &indio_dev->dev, 867a980e046SJonathan Cameron &indio_dev->channel_attr_list); 868a980e046SJonathan Cameron i++; 869a980e046SJonathan Cameron if (ret == -EBUSY && ext_info->shared) 870a980e046SJonathan Cameron continue; 871a980e046SJonathan Cameron 872a980e046SJonathan Cameron if (ret) 8733704432fSJonathan Cameron return ret; 874a980e046SJonathan Cameron 875a980e046SJonathan Cameron attrcount++; 876a980e046SJonathan Cameron } 877a980e046SJonathan Cameron } 878a980e046SJonathan Cameron 8793704432fSJonathan Cameron return attrcount; 880a980e046SJonathan Cameron } 881a980e046SJonathan Cameron 88284088ebdSLars-Peter Clausen /** 88384088ebdSLars-Peter Clausen * iio_free_chan_devattr_list() - Free a list of IIO device attributes 88484088ebdSLars-Peter Clausen * @attr_list: List of IIO device attributes 88584088ebdSLars-Peter Clausen * 88684088ebdSLars-Peter Clausen * This function frees the memory allocated for each of the IIO device 887c1b03ab5SMartin Fuzzey * attributes in the list. 88884088ebdSLars-Peter Clausen */ 88984088ebdSLars-Peter Clausen void iio_free_chan_devattr_list(struct list_head *attr_list) 890a980e046SJonathan Cameron { 89184088ebdSLars-Peter Clausen struct iio_dev_attr *p, *n; 89284088ebdSLars-Peter Clausen 89384088ebdSLars-Peter Clausen list_for_each_entry_safe(p, n, attr_list, l) { 894a980e046SJonathan Cameron kfree(p->dev_attr.attr.name); 895c1b03ab5SMartin Fuzzey list_del(&p->l); 896a980e046SJonathan Cameron kfree(p); 897a980e046SJonathan Cameron } 89884088ebdSLars-Peter Clausen } 899a980e046SJonathan Cameron 900a980e046SJonathan Cameron static ssize_t iio_show_dev_name(struct device *dev, 901a980e046SJonathan Cameron struct device_attribute *attr, 902a980e046SJonathan Cameron char *buf) 903a980e046SJonathan Cameron { 904e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 905598db581SKees Cook return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name); 906a980e046SJonathan Cameron } 907a980e046SJonathan Cameron 908a980e046SJonathan Cameron static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL); 909a980e046SJonathan Cameron 910a980e046SJonathan Cameron static int iio_device_register_sysfs(struct iio_dev *indio_dev) 911a980e046SJonathan Cameron { 912a980e046SJonathan Cameron int i, ret = 0, attrcount, attrn, attrcount_orig = 0; 91384088ebdSLars-Peter Clausen struct iio_dev_attr *p; 914a980e046SJonathan Cameron struct attribute **attr; 915a980e046SJonathan Cameron 916a980e046SJonathan Cameron /* First count elements in any existing group */ 917a980e046SJonathan Cameron if (indio_dev->info->attrs) { 918a980e046SJonathan Cameron attr = indio_dev->info->attrs->attrs; 919a980e046SJonathan Cameron while (*attr++ != NULL) 920a980e046SJonathan Cameron attrcount_orig++; 921a980e046SJonathan Cameron } 922a980e046SJonathan Cameron attrcount = attrcount_orig; 923a980e046SJonathan Cameron /* 924a980e046SJonathan Cameron * New channel registration method - relies on the fact a group does 925d25b3808SPeter Meerwald * not need to be initialized if its name is NULL. 926a980e046SJonathan Cameron */ 927a980e046SJonathan Cameron if (indio_dev->channels) 928a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) { 929a980e046SJonathan Cameron ret = iio_device_add_channel_sysfs(indio_dev, 930a980e046SJonathan Cameron &indio_dev 931a980e046SJonathan Cameron ->channels[i]); 932a980e046SJonathan Cameron if (ret < 0) 933a980e046SJonathan Cameron goto error_clear_attrs; 934a980e046SJonathan Cameron attrcount += ret; 935a980e046SJonathan Cameron } 936a980e046SJonathan Cameron 937a980e046SJonathan Cameron if (indio_dev->name) 938a980e046SJonathan Cameron attrcount++; 939a980e046SJonathan Cameron 940a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1, 941a980e046SJonathan Cameron sizeof(indio_dev->chan_attr_group.attrs[0]), 942a980e046SJonathan Cameron GFP_KERNEL); 943a980e046SJonathan Cameron if (indio_dev->chan_attr_group.attrs == NULL) { 944a980e046SJonathan Cameron ret = -ENOMEM; 945a980e046SJonathan Cameron goto error_clear_attrs; 946a980e046SJonathan Cameron } 947a980e046SJonathan Cameron /* Copy across original attributes */ 948a980e046SJonathan Cameron if (indio_dev->info->attrs) 949a980e046SJonathan Cameron memcpy(indio_dev->chan_attr_group.attrs, 950a980e046SJonathan Cameron indio_dev->info->attrs->attrs, 951a980e046SJonathan Cameron sizeof(indio_dev->chan_attr_group.attrs[0]) 952a980e046SJonathan Cameron *attrcount_orig); 953a980e046SJonathan Cameron attrn = attrcount_orig; 954a980e046SJonathan Cameron /* Add all elements from the list. */ 955a980e046SJonathan Cameron list_for_each_entry(p, &indio_dev->channel_attr_list, l) 956a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr; 957a980e046SJonathan Cameron if (indio_dev->name) 958a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr; 959a980e046SJonathan Cameron 960a980e046SJonathan Cameron indio_dev->groups[indio_dev->groupcounter++] = 961a980e046SJonathan Cameron &indio_dev->chan_attr_group; 962a980e046SJonathan Cameron 963a980e046SJonathan Cameron return 0; 964a980e046SJonathan Cameron 965a980e046SJonathan Cameron error_clear_attrs: 96684088ebdSLars-Peter Clausen iio_free_chan_devattr_list(&indio_dev->channel_attr_list); 967a980e046SJonathan Cameron 968a980e046SJonathan Cameron return ret; 969a980e046SJonathan Cameron } 970a980e046SJonathan Cameron 971a980e046SJonathan Cameron static void iio_device_unregister_sysfs(struct iio_dev *indio_dev) 972a980e046SJonathan Cameron { 973a980e046SJonathan Cameron 97484088ebdSLars-Peter Clausen iio_free_chan_devattr_list(&indio_dev->channel_attr_list); 975a980e046SJonathan Cameron kfree(indio_dev->chan_attr_group.attrs); 976c1b03ab5SMartin Fuzzey indio_dev->chan_attr_group.attrs = NULL; 977a980e046SJonathan Cameron } 978a980e046SJonathan Cameron 979a980e046SJonathan Cameron static void iio_dev_release(struct device *device) 980a980e046SJonathan Cameron { 981e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(device); 982735ad074SVladimir Barinov if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED)) 983a980e046SJonathan Cameron iio_device_unregister_trigger_consumer(indio_dev); 984a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 985a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 986e407fd65SLars-Peter Clausen 9879e69c935SLars-Peter Clausen iio_buffer_put(indio_dev->buffer); 9889e69c935SLars-Peter Clausen 989e407fd65SLars-Peter Clausen ida_simple_remove(&iio_ida, indio_dev->id); 990e407fd65SLars-Peter Clausen kfree(indio_dev); 991a980e046SJonathan Cameron } 992a980e046SJonathan Cameron 99317d82b47SGuenter Roeck struct device_type iio_device_type = { 994a980e046SJonathan Cameron .name = "iio_device", 995a980e046SJonathan Cameron .release = iio_dev_release, 996a980e046SJonathan Cameron }; 997a980e046SJonathan Cameron 998a7e57dceSSachin Kamat /** 999a7e57dceSSachin Kamat * iio_device_alloc() - allocate an iio_dev from a driver 1000a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 1001a7e57dceSSachin Kamat **/ 10027cbb7537SLars-Peter Clausen struct iio_dev *iio_device_alloc(int sizeof_priv) 1003a980e046SJonathan Cameron { 1004a980e046SJonathan Cameron struct iio_dev *dev; 1005a980e046SJonathan Cameron size_t alloc_size; 1006a980e046SJonathan Cameron 1007a980e046SJonathan Cameron alloc_size = sizeof(struct iio_dev); 1008a980e046SJonathan Cameron if (sizeof_priv) { 1009a980e046SJonathan Cameron alloc_size = ALIGN(alloc_size, IIO_ALIGN); 1010a980e046SJonathan Cameron alloc_size += sizeof_priv; 1011a980e046SJonathan Cameron } 1012a980e046SJonathan Cameron /* ensure 32-byte alignment of whole construct ? */ 1013a980e046SJonathan Cameron alloc_size += IIO_ALIGN - 1; 1014a980e046SJonathan Cameron 1015a980e046SJonathan Cameron dev = kzalloc(alloc_size, GFP_KERNEL); 1016a980e046SJonathan Cameron 1017a980e046SJonathan Cameron if (dev) { 1018a980e046SJonathan Cameron dev->dev.groups = dev->groups; 101917d82b47SGuenter Roeck dev->dev.type = &iio_device_type; 1020a980e046SJonathan Cameron dev->dev.bus = &iio_bus_type; 1021a980e046SJonathan Cameron device_initialize(&dev->dev); 1022a980e046SJonathan Cameron dev_set_drvdata(&dev->dev, (void *)dev); 1023a980e046SJonathan Cameron mutex_init(&dev->mlock); 1024a980e046SJonathan Cameron mutex_init(&dev->info_exist_lock); 1025e407fd65SLars-Peter Clausen INIT_LIST_HEAD(&dev->channel_attr_list); 1026a980e046SJonathan Cameron 1027a980e046SJonathan Cameron dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL); 1028a980e046SJonathan Cameron if (dev->id < 0) { 1029a980e046SJonathan Cameron /* cannot use a dev_err as the name isn't available */ 10303176dd5dSSachin Kamat pr_err("failed to get device id\n"); 1031a980e046SJonathan Cameron kfree(dev); 1032a980e046SJonathan Cameron return NULL; 1033a980e046SJonathan Cameron } 1034a980e046SJonathan Cameron dev_set_name(&dev->dev, "iio:device%d", dev->id); 103584b36ce5SJonathan Cameron INIT_LIST_HEAD(&dev->buffer_list); 1036a980e046SJonathan Cameron } 1037a980e046SJonathan Cameron 1038a980e046SJonathan Cameron return dev; 1039a980e046SJonathan Cameron } 10407cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_alloc); 1041a980e046SJonathan Cameron 1042a7e57dceSSachin Kamat /** 1043a7e57dceSSachin Kamat * iio_device_free() - free an iio_dev from a driver 1044a7e57dceSSachin Kamat * @dev: the iio_dev associated with the device 1045a7e57dceSSachin Kamat **/ 10467cbb7537SLars-Peter Clausen void iio_device_free(struct iio_dev *dev) 1047a980e046SJonathan Cameron { 1048e407fd65SLars-Peter Clausen if (dev) 1049e407fd65SLars-Peter Clausen put_device(&dev->dev); 1050a980e046SJonathan Cameron } 10517cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_free); 1052a980e046SJonathan Cameron 10539dabaf5eSGrygorii Strashko static void devm_iio_device_release(struct device *dev, void *res) 10549dabaf5eSGrygorii Strashko { 10559dabaf5eSGrygorii Strashko iio_device_free(*(struct iio_dev **)res); 10569dabaf5eSGrygorii Strashko } 10579dabaf5eSGrygorii Strashko 10589dabaf5eSGrygorii Strashko static int devm_iio_device_match(struct device *dev, void *res, void *data) 10599dabaf5eSGrygorii Strashko { 10609dabaf5eSGrygorii Strashko struct iio_dev **r = res; 10619dabaf5eSGrygorii Strashko if (!r || !*r) { 10629dabaf5eSGrygorii Strashko WARN_ON(!r || !*r); 10639dabaf5eSGrygorii Strashko return 0; 10649dabaf5eSGrygorii Strashko } 10659dabaf5eSGrygorii Strashko return *r == data; 10669dabaf5eSGrygorii Strashko } 10679dabaf5eSGrygorii Strashko 1068a7e57dceSSachin Kamat /** 1069a7e57dceSSachin Kamat * devm_iio_device_alloc - Resource-managed iio_device_alloc() 1070a7e57dceSSachin Kamat * @dev: Device to allocate iio_dev for 1071a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 1072a7e57dceSSachin Kamat * 1073a7e57dceSSachin Kamat * Managed iio_device_alloc. iio_dev allocated with this function is 1074a7e57dceSSachin Kamat * automatically freed on driver detach. 1075a7e57dceSSachin Kamat * 1076a7e57dceSSachin Kamat * If an iio_dev allocated with this function needs to be freed separately, 1077a7e57dceSSachin Kamat * devm_iio_device_free() must be used. 1078a7e57dceSSachin Kamat * 1079a7e57dceSSachin Kamat * RETURNS: 1080a7e57dceSSachin Kamat * Pointer to allocated iio_dev on success, NULL on failure. 1081a7e57dceSSachin Kamat */ 10829dabaf5eSGrygorii Strashko struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv) 10839dabaf5eSGrygorii Strashko { 10849dabaf5eSGrygorii Strashko struct iio_dev **ptr, *iio_dev; 10859dabaf5eSGrygorii Strashko 10869dabaf5eSGrygorii Strashko ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr), 10879dabaf5eSGrygorii Strashko GFP_KERNEL); 10889dabaf5eSGrygorii Strashko if (!ptr) 10899dabaf5eSGrygorii Strashko return NULL; 10909dabaf5eSGrygorii Strashko 10919dabaf5eSGrygorii Strashko iio_dev = iio_device_alloc(sizeof_priv); 10929dabaf5eSGrygorii Strashko if (iio_dev) { 10939dabaf5eSGrygorii Strashko *ptr = iio_dev; 10949dabaf5eSGrygorii Strashko devres_add(dev, ptr); 10959dabaf5eSGrygorii Strashko } else { 10969dabaf5eSGrygorii Strashko devres_free(ptr); 10979dabaf5eSGrygorii Strashko } 10989dabaf5eSGrygorii Strashko 10999dabaf5eSGrygorii Strashko return iio_dev; 11009dabaf5eSGrygorii Strashko } 11019dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_alloc); 11029dabaf5eSGrygorii Strashko 1103a7e57dceSSachin Kamat /** 1104a7e57dceSSachin Kamat * devm_iio_device_free - Resource-managed iio_device_free() 1105a7e57dceSSachin Kamat * @dev: Device this iio_dev belongs to 1106a7e57dceSSachin Kamat * @iio_dev: the iio_dev associated with the device 1107a7e57dceSSachin Kamat * 1108a7e57dceSSachin Kamat * Free iio_dev allocated with devm_iio_device_alloc(). 1109a7e57dceSSachin Kamat */ 11109dabaf5eSGrygorii Strashko void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev) 11119dabaf5eSGrygorii Strashko { 11129dabaf5eSGrygorii Strashko int rc; 11139dabaf5eSGrygorii Strashko 11149dabaf5eSGrygorii Strashko rc = devres_release(dev, devm_iio_device_release, 11159dabaf5eSGrygorii Strashko devm_iio_device_match, iio_dev); 11169dabaf5eSGrygorii Strashko WARN_ON(rc); 11179dabaf5eSGrygorii Strashko } 11189dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_free); 11199dabaf5eSGrygorii Strashko 1120a980e046SJonathan Cameron /** 1121a980e046SJonathan Cameron * iio_chrdev_open() - chrdev file open for buffer access and ioctls 11222498dcf6SCristina Opriceana * @inode: Inode structure for identifying the device in the file system 11232498dcf6SCristina Opriceana * @filp: File structure for iio device used to keep and later access 11242498dcf6SCristina Opriceana * private data 11252498dcf6SCristina Opriceana * 11262498dcf6SCristina Opriceana * Return: 0 on success or -EBUSY if the device is already opened 1127a980e046SJonathan Cameron **/ 1128a980e046SJonathan Cameron static int iio_chrdev_open(struct inode *inode, struct file *filp) 1129a980e046SJonathan Cameron { 1130a980e046SJonathan Cameron struct iio_dev *indio_dev = container_of(inode->i_cdev, 1131a980e046SJonathan Cameron struct iio_dev, chrdev); 1132a980e046SJonathan Cameron 1133a980e046SJonathan Cameron if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags)) 1134a980e046SJonathan Cameron return -EBUSY; 1135a980e046SJonathan Cameron 1136cadc2125SLars-Peter Clausen iio_device_get(indio_dev); 1137cadc2125SLars-Peter Clausen 1138a980e046SJonathan Cameron filp->private_data = indio_dev; 1139a980e046SJonathan Cameron 1140a980e046SJonathan Cameron return 0; 1141a980e046SJonathan Cameron } 1142a980e046SJonathan Cameron 1143a980e046SJonathan Cameron /** 1144a980e046SJonathan Cameron * iio_chrdev_release() - chrdev file close buffer access and ioctls 11452498dcf6SCristina Opriceana * @inode: Inode structure pointer for the char device 11462498dcf6SCristina Opriceana * @filp: File structure pointer for the char device 11472498dcf6SCristina Opriceana * 11482498dcf6SCristina Opriceana * Return: 0 for successful release 11492498dcf6SCristina Opriceana */ 1150a980e046SJonathan Cameron static int iio_chrdev_release(struct inode *inode, struct file *filp) 1151a980e046SJonathan Cameron { 1152a980e046SJonathan Cameron struct iio_dev *indio_dev = container_of(inode->i_cdev, 1153a980e046SJonathan Cameron struct iio_dev, chrdev); 1154a980e046SJonathan Cameron clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags); 1155cadc2125SLars-Peter Clausen iio_device_put(indio_dev); 1156cadc2125SLars-Peter Clausen 1157a980e046SJonathan Cameron return 0; 1158a980e046SJonathan Cameron } 1159a980e046SJonathan Cameron 1160a980e046SJonathan Cameron /* Somewhat of a cross file organization violation - ioctls here are actually 1161a980e046SJonathan Cameron * event related */ 1162a980e046SJonathan Cameron static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 1163a980e046SJonathan Cameron { 1164a980e046SJonathan Cameron struct iio_dev *indio_dev = filp->private_data; 1165a980e046SJonathan Cameron int __user *ip = (int __user *)arg; 1166a980e046SJonathan Cameron int fd; 1167a980e046SJonathan Cameron 1168f18e7a06SLars-Peter Clausen if (!indio_dev->info) 1169f18e7a06SLars-Peter Clausen return -ENODEV; 1170f18e7a06SLars-Peter Clausen 1171a980e046SJonathan Cameron if (cmd == IIO_GET_EVENT_FD_IOCTL) { 1172a980e046SJonathan Cameron fd = iio_event_getfd(indio_dev); 11733f9059b7SLinus Walleij if (fd < 0) 11743f9059b7SLinus Walleij return fd; 1175a980e046SJonathan Cameron if (copy_to_user(ip, &fd, sizeof(fd))) 1176a980e046SJonathan Cameron return -EFAULT; 1177a980e046SJonathan Cameron return 0; 1178a980e046SJonathan Cameron } 1179a980e046SJonathan Cameron return -EINVAL; 1180a980e046SJonathan Cameron } 1181a980e046SJonathan Cameron 1182a980e046SJonathan Cameron static const struct file_operations iio_buffer_fileops = { 1183a980e046SJonathan Cameron .read = iio_buffer_read_first_n_outer_addr, 1184a980e046SJonathan Cameron .release = iio_chrdev_release, 1185a980e046SJonathan Cameron .open = iio_chrdev_open, 1186a980e046SJonathan Cameron .poll = iio_buffer_poll_addr, 1187a980e046SJonathan Cameron .owner = THIS_MODULE, 1188a980e046SJonathan Cameron .llseek = noop_llseek, 1189a980e046SJonathan Cameron .unlocked_ioctl = iio_ioctl, 1190a980e046SJonathan Cameron .compat_ioctl = iio_ioctl, 1191a980e046SJonathan Cameron }; 1192a980e046SJonathan Cameron 11938f5d8727SVlad Dogaru static int iio_check_unique_scan_index(struct iio_dev *indio_dev) 11948f5d8727SVlad Dogaru { 11958f5d8727SVlad Dogaru int i, j; 11968f5d8727SVlad Dogaru const struct iio_chan_spec *channels = indio_dev->channels; 11978f5d8727SVlad Dogaru 11988f5d8727SVlad Dogaru if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES)) 11998f5d8727SVlad Dogaru return 0; 12008f5d8727SVlad Dogaru 12018f5d8727SVlad Dogaru for (i = 0; i < indio_dev->num_channels - 1; i++) { 12028f5d8727SVlad Dogaru if (channels[i].scan_index < 0) 12038f5d8727SVlad Dogaru continue; 12048f5d8727SVlad Dogaru for (j = i + 1; j < indio_dev->num_channels; j++) 12058f5d8727SVlad Dogaru if (channels[i].scan_index == channels[j].scan_index) { 12068f5d8727SVlad Dogaru dev_err(&indio_dev->dev, 12078f5d8727SVlad Dogaru "Duplicate scan index %d\n", 12088f5d8727SVlad Dogaru channels[i].scan_index); 12098f5d8727SVlad Dogaru return -EINVAL; 12108f5d8727SVlad Dogaru } 12118f5d8727SVlad Dogaru } 12128f5d8727SVlad Dogaru 12138f5d8727SVlad Dogaru return 0; 12148f5d8727SVlad Dogaru } 12158f5d8727SVlad Dogaru 1216a980e046SJonathan Cameron static const struct iio_buffer_setup_ops noop_ring_setup_ops; 1217a980e046SJonathan Cameron 1218a7e57dceSSachin Kamat /** 1219a7e57dceSSachin Kamat * iio_device_register() - register a device with the IIO subsystem 1220a7e57dceSSachin Kamat * @indio_dev: Device structure filled by the device driver 1221a7e57dceSSachin Kamat **/ 1222a980e046SJonathan Cameron int iio_device_register(struct iio_dev *indio_dev) 1223a980e046SJonathan Cameron { 1224a980e046SJonathan Cameron int ret; 1225a980e046SJonathan Cameron 122617d82b47SGuenter Roeck /* If the calling driver did not initialize of_node, do it here */ 122717d82b47SGuenter Roeck if (!indio_dev->dev.of_node && indio_dev->dev.parent) 122817d82b47SGuenter Roeck indio_dev->dev.of_node = indio_dev->dev.parent->of_node; 122917d82b47SGuenter Roeck 12308f5d8727SVlad Dogaru ret = iio_check_unique_scan_index(indio_dev); 12318f5d8727SVlad Dogaru if (ret < 0) 12328f5d8727SVlad Dogaru return ret; 12338f5d8727SVlad Dogaru 1234a980e046SJonathan Cameron /* configure elements for the chrdev */ 1235a980e046SJonathan Cameron indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id); 1236a980e046SJonathan Cameron 1237a980e046SJonathan Cameron ret = iio_device_register_debugfs(indio_dev); 1238a980e046SJonathan Cameron if (ret) { 1239a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1240a980e046SJonathan Cameron "Failed to register debugfs interfaces\n"); 124192825ff9SHartmut Knaack return ret; 1242a980e046SJonathan Cameron } 12433e1b6c95SLars-Peter Clausen 12443e1b6c95SLars-Peter Clausen ret = iio_buffer_alloc_sysfs_and_mask(indio_dev); 12453e1b6c95SLars-Peter Clausen if (ret) { 12463e1b6c95SLars-Peter Clausen dev_err(indio_dev->dev.parent, 12473e1b6c95SLars-Peter Clausen "Failed to create buffer sysfs interfaces\n"); 12483e1b6c95SLars-Peter Clausen goto error_unreg_debugfs; 12493e1b6c95SLars-Peter Clausen } 12503e1b6c95SLars-Peter Clausen 1251a980e046SJonathan Cameron ret = iio_device_register_sysfs(indio_dev); 1252a980e046SJonathan Cameron if (ret) { 1253a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1254a980e046SJonathan Cameron "Failed to register sysfs interfaces\n"); 12553e1b6c95SLars-Peter Clausen goto error_buffer_free_sysfs; 1256a980e046SJonathan Cameron } 1257a980e046SJonathan Cameron ret = iio_device_register_eventset(indio_dev); 1258a980e046SJonathan Cameron if (ret) { 1259a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1260a980e046SJonathan Cameron "Failed to register event set\n"); 1261a980e046SJonathan Cameron goto error_free_sysfs; 1262a980e046SJonathan Cameron } 1263735ad074SVladimir Barinov if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED)) 1264a980e046SJonathan Cameron iio_device_register_trigger_consumer(indio_dev); 1265a980e046SJonathan Cameron 1266a980e046SJonathan Cameron if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) && 1267a980e046SJonathan Cameron indio_dev->setup_ops == NULL) 1268a980e046SJonathan Cameron indio_dev->setup_ops = &noop_ring_setup_ops; 1269a980e046SJonathan Cameron 1270a980e046SJonathan Cameron cdev_init(&indio_dev->chrdev, &iio_buffer_fileops); 1271a980e046SJonathan Cameron indio_dev->chrdev.owner = indio_dev->info->driver_module; 12720d5b7daeSLars-Peter Clausen indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj; 1273a980e046SJonathan Cameron ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1); 1274a980e046SJonathan Cameron if (ret < 0) 12750d5b7daeSLars-Peter Clausen goto error_unreg_eventset; 1276a980e046SJonathan Cameron 12770d5b7daeSLars-Peter Clausen ret = device_add(&indio_dev->dev); 12780d5b7daeSLars-Peter Clausen if (ret < 0) 12790d5b7daeSLars-Peter Clausen goto error_cdev_del; 12800d5b7daeSLars-Peter Clausen 12810d5b7daeSLars-Peter Clausen return 0; 12820d5b7daeSLars-Peter Clausen error_cdev_del: 12830d5b7daeSLars-Peter Clausen cdev_del(&indio_dev->chrdev); 1284a980e046SJonathan Cameron error_unreg_eventset: 1285a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 1286a980e046SJonathan Cameron error_free_sysfs: 1287a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 12883e1b6c95SLars-Peter Clausen error_buffer_free_sysfs: 12893e1b6c95SLars-Peter Clausen iio_buffer_free_sysfs_and_mask(indio_dev); 1290a980e046SJonathan Cameron error_unreg_debugfs: 1291a980e046SJonathan Cameron iio_device_unregister_debugfs(indio_dev); 1292a980e046SJonathan Cameron return ret; 1293a980e046SJonathan Cameron } 1294a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_register); 1295a980e046SJonathan Cameron 1296a7e57dceSSachin Kamat /** 1297a7e57dceSSachin Kamat * iio_device_unregister() - unregister a device from the IIO subsystem 1298a7e57dceSSachin Kamat * @indio_dev: Device structure representing the device. 1299a7e57dceSSachin Kamat **/ 1300a980e046SJonathan Cameron void iio_device_unregister(struct iio_dev *indio_dev) 1301a980e046SJonathan Cameron { 1302a980e046SJonathan Cameron mutex_lock(&indio_dev->info_exist_lock); 1303a87c82e4SLars-Peter Clausen 1304a87c82e4SLars-Peter Clausen device_del(&indio_dev->dev); 1305a87c82e4SLars-Peter Clausen 13060d5b7daeSLars-Peter Clausen if (indio_dev->chrdev.dev) 13070d5b7daeSLars-Peter Clausen cdev_del(&indio_dev->chrdev); 1308bc4c9612SLars-Peter Clausen iio_device_unregister_debugfs(indio_dev); 13090d5b7daeSLars-Peter Clausen 1310a87c82e4SLars-Peter Clausen iio_disable_all_buffers(indio_dev); 1311a87c82e4SLars-Peter Clausen 1312a980e046SJonathan Cameron indio_dev->info = NULL; 1313d2f0a48fSLars-Peter Clausen 1314d2f0a48fSLars-Peter Clausen iio_device_wakeup_eventset(indio_dev); 1315d2f0a48fSLars-Peter Clausen iio_buffer_wakeup_poll(indio_dev); 1316d2f0a48fSLars-Peter Clausen 1317a980e046SJonathan Cameron mutex_unlock(&indio_dev->info_exist_lock); 13183e1b6c95SLars-Peter Clausen 13193e1b6c95SLars-Peter Clausen iio_buffer_free_sysfs_and_mask(indio_dev); 1320a980e046SJonathan Cameron } 1321a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_unregister); 13228caa07c0SSachin Kamat 13238caa07c0SSachin Kamat static void devm_iio_device_unreg(struct device *dev, void *res) 13248caa07c0SSachin Kamat { 13258caa07c0SSachin Kamat iio_device_unregister(*(struct iio_dev **)res); 13268caa07c0SSachin Kamat } 13278caa07c0SSachin Kamat 13288caa07c0SSachin Kamat /** 13298caa07c0SSachin Kamat * devm_iio_device_register - Resource-managed iio_device_register() 13308caa07c0SSachin Kamat * @dev: Device to allocate iio_dev for 13318caa07c0SSachin Kamat * @indio_dev: Device structure filled by the device driver 13328caa07c0SSachin Kamat * 13338caa07c0SSachin Kamat * Managed iio_device_register. The IIO device registered with this 13348caa07c0SSachin Kamat * function is automatically unregistered on driver detach. This function 13358caa07c0SSachin Kamat * calls iio_device_register() internally. Refer to that function for more 13368caa07c0SSachin Kamat * information. 13378caa07c0SSachin Kamat * 13388caa07c0SSachin Kamat * If an iio_dev registered with this function needs to be unregistered 13398caa07c0SSachin Kamat * separately, devm_iio_device_unregister() must be used. 13408caa07c0SSachin Kamat * 13418caa07c0SSachin Kamat * RETURNS: 13428caa07c0SSachin Kamat * 0 on success, negative error number on failure. 13438caa07c0SSachin Kamat */ 13448caa07c0SSachin Kamat int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev) 13458caa07c0SSachin Kamat { 13468caa07c0SSachin Kamat struct iio_dev **ptr; 13478caa07c0SSachin Kamat int ret; 13488caa07c0SSachin Kamat 13498caa07c0SSachin Kamat ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL); 13508caa07c0SSachin Kamat if (!ptr) 13518caa07c0SSachin Kamat return -ENOMEM; 13528caa07c0SSachin Kamat 13538caa07c0SSachin Kamat *ptr = indio_dev; 13548caa07c0SSachin Kamat ret = iio_device_register(indio_dev); 13558caa07c0SSachin Kamat if (!ret) 13568caa07c0SSachin Kamat devres_add(dev, ptr); 13578caa07c0SSachin Kamat else 13588caa07c0SSachin Kamat devres_free(ptr); 13598caa07c0SSachin Kamat 13608caa07c0SSachin Kamat return ret; 13618caa07c0SSachin Kamat } 13628caa07c0SSachin Kamat EXPORT_SYMBOL_GPL(devm_iio_device_register); 13638caa07c0SSachin Kamat 13648caa07c0SSachin Kamat /** 13658caa07c0SSachin Kamat * devm_iio_device_unregister - Resource-managed iio_device_unregister() 13668caa07c0SSachin Kamat * @dev: Device this iio_dev belongs to 13678caa07c0SSachin Kamat * @indio_dev: the iio_dev associated with the device 13688caa07c0SSachin Kamat * 13698caa07c0SSachin Kamat * Unregister iio_dev registered with devm_iio_device_register(). 13708caa07c0SSachin Kamat */ 13718caa07c0SSachin Kamat void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev) 13728caa07c0SSachin Kamat { 13738caa07c0SSachin Kamat int rc; 13748caa07c0SSachin Kamat 13758caa07c0SSachin Kamat rc = devres_release(dev, devm_iio_device_unreg, 13768caa07c0SSachin Kamat devm_iio_device_match, indio_dev); 13778caa07c0SSachin Kamat WARN_ON(rc); 13788caa07c0SSachin Kamat } 13798caa07c0SSachin Kamat EXPORT_SYMBOL_GPL(devm_iio_device_unregister); 13808caa07c0SSachin Kamat 138108a33805SAlison Schofield /** 138208a33805SAlison Schofield * iio_device_claim_direct_mode - Keep device in direct mode 138308a33805SAlison Schofield * @indio_dev: the iio_dev associated with the device 138408a33805SAlison Schofield * 138508a33805SAlison Schofield * If the device is in direct mode it is guaranteed to stay 138608a33805SAlison Schofield * that way until iio_device_release_direct_mode() is called. 138708a33805SAlison Schofield * 138808a33805SAlison Schofield * Use with iio_device_release_direct_mode() 138908a33805SAlison Schofield * 139008a33805SAlison Schofield * Returns: 0 on success, -EBUSY on failure 139108a33805SAlison Schofield */ 139208a33805SAlison Schofield int iio_device_claim_direct_mode(struct iio_dev *indio_dev) 139308a33805SAlison Schofield { 139408a33805SAlison Schofield mutex_lock(&indio_dev->mlock); 139508a33805SAlison Schofield 139608a33805SAlison Schofield if (iio_buffer_enabled(indio_dev)) { 139708a33805SAlison Schofield mutex_unlock(&indio_dev->mlock); 139808a33805SAlison Schofield return -EBUSY; 139908a33805SAlison Schofield } 140008a33805SAlison Schofield return 0; 140108a33805SAlison Schofield } 140208a33805SAlison Schofield EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode); 140308a33805SAlison Schofield 140408a33805SAlison Schofield /** 140508a33805SAlison Schofield * iio_device_release_direct_mode - releases claim on direct mode 140608a33805SAlison Schofield * @indio_dev: the iio_dev associated with the device 140708a33805SAlison Schofield * 140808a33805SAlison Schofield * Release the claim. Device is no longer guaranteed to stay 140908a33805SAlison Schofield * in direct mode. 141008a33805SAlison Schofield * 141108a33805SAlison Schofield * Use with iio_device_claim_direct_mode() 141208a33805SAlison Schofield */ 141308a33805SAlison Schofield void iio_device_release_direct_mode(struct iio_dev *indio_dev) 141408a33805SAlison Schofield { 141508a33805SAlison Schofield mutex_unlock(&indio_dev->mlock); 141608a33805SAlison Schofield } 141708a33805SAlison Schofield EXPORT_SYMBOL_GPL(iio_device_release_direct_mode); 141808a33805SAlison Schofield 1419a980e046SJonathan Cameron subsys_initcall(iio_init); 1420a980e046SJonathan Cameron module_exit(iio_exit); 1421a980e046SJonathan Cameron 1422c8b95952SJonathan Cameron MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); 1423a980e046SJonathan Cameron MODULE_DESCRIPTION("Industrial I/O core"); 1424a980e046SJonathan Cameron MODULE_LICENSE("GPL"); 1425