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", 75a980e046SJonathan Cameron }; 76a980e046SJonathan Cameron 77a980e046SJonathan Cameron static const char * const iio_modifier_names[] = { 78a980e046SJonathan Cameron [IIO_MOD_X] = "x", 79a980e046SJonathan Cameron [IIO_MOD_Y] = "y", 80a980e046SJonathan Cameron [IIO_MOD_Z] = "z", 818f5879b2SJonathan Cameron [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)", 82cf82cb81SJonathan Cameron [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2", 83a980e046SJonathan Cameron [IIO_MOD_LIGHT_BOTH] = "both", 84a980e046SJonathan Cameron [IIO_MOD_LIGHT_IR] = "ir", 8521cd1fabSJon Brenner [IIO_MOD_LIGHT_CLEAR] = "clear", 8621cd1fabSJon Brenner [IIO_MOD_LIGHT_RED] = "red", 8721cd1fabSJon Brenner [IIO_MOD_LIGHT_GREEN] = "green", 8821cd1fabSJon Brenner [IIO_MOD_LIGHT_BLUE] = "blue", 895082f405SSrinivas Pandruvada [IIO_MOD_QUATERNION] = "quaternion", 90638b43b3SPeter Meerwald [IIO_MOD_TEMP_AMBIENT] = "ambient", 91638b43b3SPeter Meerwald [IIO_MOD_TEMP_OBJECT] = "object", 9211b8ddabSReyad Attiyat [IIO_MOD_NORTH_MAGN] = "from_north_magnetic", 9311b8ddabSReyad Attiyat [IIO_MOD_NORTH_TRUE] = "from_north_true", 9411b8ddabSReyad Attiyat [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp", 9511b8ddabSReyad Attiyat [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp", 9655aebeb9SDaniel Baluta [IIO_MOD_RUNNING] = "running", 9755aebeb9SDaniel Baluta [IIO_MOD_JOGGING] = "jogging", 9855aebeb9SDaniel Baluta [IIO_MOD_WALKING] = "walking", 9955aebeb9SDaniel Baluta [IIO_MOD_STILL] = "still", 100a980e046SJonathan Cameron }; 101a980e046SJonathan Cameron 102a980e046SJonathan Cameron /* relies on pairs of these shared then separate */ 103a980e046SJonathan Cameron static const char * const iio_chan_info_postfix[] = { 104a980e046SJonathan Cameron [IIO_CHAN_INFO_RAW] = "raw", 105a980e046SJonathan Cameron [IIO_CHAN_INFO_PROCESSED] = "input", 106a980e046SJonathan Cameron [IIO_CHAN_INFO_SCALE] = "scale", 107a980e046SJonathan Cameron [IIO_CHAN_INFO_OFFSET] = "offset", 108a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBSCALE] = "calibscale", 109a980e046SJonathan Cameron [IIO_CHAN_INFO_CALIBBIAS] = "calibbias", 110a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK] = "peak_raw", 111a980e046SJonathan Cameron [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale", 112a980e046SJonathan Cameron [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw", 113a980e046SJonathan Cameron [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw", 114a980e046SJonathan Cameron [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY] 115a980e046SJonathan Cameron = "filter_low_pass_3db_frequency", 116a980e046SJonathan Cameron [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency", 117a6b12855SMichael Hennerich [IIO_CHAN_INFO_FREQUENCY] = "frequency", 118a6b12855SMichael Hennerich [IIO_CHAN_INFO_PHASE] = "phase", 119b65d6212SMichael Hennerich [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain", 1207c9ab035Ssrinivas pandruvada [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis", 121899d90bdSPeter Meerwald [IIO_CHAN_INFO_INT_TIME] = "integration_time", 122a88bfe78SIrina Tirdea [IIO_CHAN_INFO_ENABLE] = "en", 123bcdf28fbSIrina Tirdea [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight", 124a980e046SJonathan Cameron }; 125a980e046SJonathan Cameron 126a7e57dceSSachin Kamat /** 127a7e57dceSSachin Kamat * iio_find_channel_from_si() - get channel from its scan index 128a7e57dceSSachin Kamat * @indio_dev: device 129a7e57dceSSachin Kamat * @si: scan index to match 130a7e57dceSSachin Kamat */ 131a980e046SJonathan Cameron const struct iio_chan_spec 132a980e046SJonathan Cameron *iio_find_channel_from_si(struct iio_dev *indio_dev, int si) 133a980e046SJonathan Cameron { 134a980e046SJonathan Cameron int i; 135a980e046SJonathan Cameron 136a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) 137a980e046SJonathan Cameron if (indio_dev->channels[i].scan_index == si) 138a980e046SJonathan Cameron return &indio_dev->channels[i]; 139a980e046SJonathan Cameron return NULL; 140a980e046SJonathan Cameron } 141a980e046SJonathan Cameron 142a980e046SJonathan Cameron /* This turns up an awful lot */ 143a980e046SJonathan Cameron ssize_t iio_read_const_attr(struct device *dev, 144a980e046SJonathan Cameron struct device_attribute *attr, 145a980e046SJonathan Cameron char *buf) 146a980e046SJonathan Cameron { 147a980e046SJonathan Cameron return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string); 148a980e046SJonathan Cameron } 149a980e046SJonathan Cameron EXPORT_SYMBOL(iio_read_const_attr); 150a980e046SJonathan Cameron 151a980e046SJonathan Cameron static int __init iio_init(void) 152a980e046SJonathan Cameron { 153a980e046SJonathan Cameron int ret; 154a980e046SJonathan Cameron 155a980e046SJonathan Cameron /* Register sysfs bus */ 156a980e046SJonathan Cameron ret = bus_register(&iio_bus_type); 157a980e046SJonathan Cameron if (ret < 0) { 1583176dd5dSSachin Kamat pr_err("could not register bus type\n"); 159a980e046SJonathan Cameron goto error_nothing; 160a980e046SJonathan Cameron } 161a980e046SJonathan Cameron 162a980e046SJonathan Cameron ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio"); 163a980e046SJonathan Cameron if (ret < 0) { 1643176dd5dSSachin Kamat pr_err("failed to allocate char dev region\n"); 165a980e046SJonathan Cameron goto error_unregister_bus_type; 166a980e046SJonathan Cameron } 167a980e046SJonathan Cameron 168a980e046SJonathan Cameron iio_debugfs_dentry = debugfs_create_dir("iio", NULL); 169a980e046SJonathan Cameron 170a980e046SJonathan Cameron return 0; 171a980e046SJonathan Cameron 172a980e046SJonathan Cameron error_unregister_bus_type: 173a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 174a980e046SJonathan Cameron error_nothing: 175a980e046SJonathan Cameron return ret; 176a980e046SJonathan Cameron } 177a980e046SJonathan Cameron 178a980e046SJonathan Cameron static void __exit iio_exit(void) 179a980e046SJonathan Cameron { 180a980e046SJonathan Cameron if (iio_devt) 181a980e046SJonathan Cameron unregister_chrdev_region(iio_devt, IIO_DEV_MAX); 182a980e046SJonathan Cameron bus_unregister(&iio_bus_type); 183a980e046SJonathan Cameron debugfs_remove(iio_debugfs_dentry); 184a980e046SJonathan Cameron } 185a980e046SJonathan Cameron 186a980e046SJonathan Cameron #if defined(CONFIG_DEBUG_FS) 187a980e046SJonathan Cameron static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf, 188a980e046SJonathan Cameron size_t count, loff_t *ppos) 189a980e046SJonathan Cameron { 190a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 191a980e046SJonathan Cameron char buf[20]; 192a980e046SJonathan Cameron unsigned val = 0; 193a980e046SJonathan Cameron ssize_t len; 194a980e046SJonathan Cameron int ret; 195a980e046SJonathan Cameron 196a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, 197a980e046SJonathan Cameron indio_dev->cached_reg_addr, 198a980e046SJonathan Cameron 0, &val); 199a980e046SJonathan Cameron if (ret) 200a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__); 201a980e046SJonathan Cameron 202a980e046SJonathan Cameron len = snprintf(buf, sizeof(buf), "0x%X\n", val); 203a980e046SJonathan Cameron 204a980e046SJonathan Cameron return simple_read_from_buffer(userbuf, count, ppos, buf, len); 205a980e046SJonathan Cameron } 206a980e046SJonathan Cameron 207a980e046SJonathan Cameron static ssize_t iio_debugfs_write_reg(struct file *file, 208a980e046SJonathan Cameron const char __user *userbuf, size_t count, loff_t *ppos) 209a980e046SJonathan Cameron { 210a980e046SJonathan Cameron struct iio_dev *indio_dev = file->private_data; 211a980e046SJonathan Cameron unsigned reg, val; 212a980e046SJonathan Cameron char buf[80]; 213a980e046SJonathan Cameron int ret; 214a980e046SJonathan Cameron 215a980e046SJonathan Cameron count = min_t(size_t, count, (sizeof(buf)-1)); 216a980e046SJonathan Cameron if (copy_from_user(buf, userbuf, count)) 217a980e046SJonathan Cameron return -EFAULT; 218a980e046SJonathan Cameron 219a980e046SJonathan Cameron buf[count] = 0; 220a980e046SJonathan Cameron 221a980e046SJonathan Cameron ret = sscanf(buf, "%i %i", ®, &val); 222a980e046SJonathan Cameron 223a980e046SJonathan Cameron switch (ret) { 224a980e046SJonathan Cameron case 1: 225a980e046SJonathan Cameron indio_dev->cached_reg_addr = reg; 226a980e046SJonathan Cameron break; 227a980e046SJonathan Cameron case 2: 228a980e046SJonathan Cameron indio_dev->cached_reg_addr = reg; 229a980e046SJonathan Cameron ret = indio_dev->info->debugfs_reg_access(indio_dev, reg, 230a980e046SJonathan Cameron val, NULL); 231a980e046SJonathan Cameron if (ret) { 232a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, "%s: write failed\n", 233a980e046SJonathan Cameron __func__); 234a980e046SJonathan Cameron return ret; 235a980e046SJonathan Cameron } 236a980e046SJonathan Cameron break; 237a980e046SJonathan Cameron default: 238a980e046SJonathan Cameron return -EINVAL; 239a980e046SJonathan Cameron } 240a980e046SJonathan Cameron 241a980e046SJonathan Cameron return count; 242a980e046SJonathan Cameron } 243a980e046SJonathan Cameron 244a980e046SJonathan Cameron static const struct file_operations iio_debugfs_reg_fops = { 2455a28c873SAxel Lin .open = simple_open, 246a980e046SJonathan Cameron .read = iio_debugfs_read_reg, 247a980e046SJonathan Cameron .write = iio_debugfs_write_reg, 248a980e046SJonathan Cameron }; 249a980e046SJonathan Cameron 250a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 251a980e046SJonathan Cameron { 252a980e046SJonathan Cameron debugfs_remove_recursive(indio_dev->debugfs_dentry); 253a980e046SJonathan Cameron } 254a980e046SJonathan Cameron 255a980e046SJonathan Cameron static int iio_device_register_debugfs(struct iio_dev *indio_dev) 256a980e046SJonathan Cameron { 257a980e046SJonathan Cameron struct dentry *d; 258a980e046SJonathan Cameron 259a980e046SJonathan Cameron if (indio_dev->info->debugfs_reg_access == NULL) 260a980e046SJonathan Cameron return 0; 261a980e046SJonathan Cameron 262abd5a2fbSAxel Lin if (!iio_debugfs_dentry) 263a980e046SJonathan Cameron return 0; 264a980e046SJonathan Cameron 265a980e046SJonathan Cameron indio_dev->debugfs_dentry = 266a980e046SJonathan Cameron debugfs_create_dir(dev_name(&indio_dev->dev), 267a980e046SJonathan Cameron iio_debugfs_dentry); 268a980e046SJonathan Cameron if (indio_dev->debugfs_dentry == NULL) { 269a980e046SJonathan Cameron dev_warn(indio_dev->dev.parent, 270a980e046SJonathan Cameron "Failed to create debugfs directory\n"); 271a980e046SJonathan Cameron return -EFAULT; 272a980e046SJonathan Cameron } 273a980e046SJonathan Cameron 274a980e046SJonathan Cameron d = debugfs_create_file("direct_reg_access", 0644, 275a980e046SJonathan Cameron indio_dev->debugfs_dentry, 276a980e046SJonathan Cameron indio_dev, &iio_debugfs_reg_fops); 277a980e046SJonathan Cameron if (!d) { 278a980e046SJonathan Cameron iio_device_unregister_debugfs(indio_dev); 279a980e046SJonathan Cameron return -ENOMEM; 280a980e046SJonathan Cameron } 281a980e046SJonathan Cameron 282a980e046SJonathan Cameron return 0; 283a980e046SJonathan Cameron } 284a980e046SJonathan Cameron #else 285a980e046SJonathan Cameron static int iio_device_register_debugfs(struct iio_dev *indio_dev) 286a980e046SJonathan Cameron { 287a980e046SJonathan Cameron return 0; 288a980e046SJonathan Cameron } 289a980e046SJonathan Cameron 290a980e046SJonathan Cameron static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) 291a980e046SJonathan Cameron { 292a980e046SJonathan Cameron } 293a980e046SJonathan Cameron #endif /* CONFIG_DEBUG_FS */ 294a980e046SJonathan Cameron 295a980e046SJonathan Cameron static ssize_t iio_read_channel_ext_info(struct device *dev, 296a980e046SJonathan Cameron struct device_attribute *attr, 297a980e046SJonathan Cameron char *buf) 298a980e046SJonathan Cameron { 299e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 300a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 301a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 302a980e046SJonathan Cameron 303a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 304a980e046SJonathan Cameron 305fc6d1139SMichael Hennerich return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf); 306a980e046SJonathan Cameron } 307a980e046SJonathan Cameron 308a980e046SJonathan Cameron static ssize_t iio_write_channel_ext_info(struct device *dev, 309a980e046SJonathan Cameron struct device_attribute *attr, 310a980e046SJonathan Cameron const char *buf, 311a980e046SJonathan Cameron size_t len) 312a980e046SJonathan Cameron { 313e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 314a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 315a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 316a980e046SJonathan Cameron 317a980e046SJonathan Cameron ext_info = &this_attr->c->ext_info[this_attr->address]; 318a980e046SJonathan Cameron 319fc6d1139SMichael Hennerich return ext_info->write(indio_dev, ext_info->private, 320fc6d1139SMichael Hennerich this_attr->c, buf, len); 321a980e046SJonathan Cameron } 322a980e046SJonathan Cameron 3235212cc8aSLars-Peter Clausen ssize_t iio_enum_available_read(struct iio_dev *indio_dev, 3245212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 3255212cc8aSLars-Peter Clausen { 3265212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3275212cc8aSLars-Peter Clausen unsigned int i; 3285212cc8aSLars-Peter Clausen size_t len = 0; 3295212cc8aSLars-Peter Clausen 3305212cc8aSLars-Peter Clausen if (!e->num_items) 3315212cc8aSLars-Peter Clausen return 0; 3325212cc8aSLars-Peter Clausen 3335212cc8aSLars-Peter Clausen for (i = 0; i < e->num_items; ++i) 33474dcd439SLars-Peter Clausen len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]); 3355212cc8aSLars-Peter Clausen 3365212cc8aSLars-Peter Clausen /* replace last space with a newline */ 3375212cc8aSLars-Peter Clausen buf[len - 1] = '\n'; 3385212cc8aSLars-Peter Clausen 3395212cc8aSLars-Peter Clausen return len; 3405212cc8aSLars-Peter Clausen } 3415212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_available_read); 3425212cc8aSLars-Peter Clausen 3435212cc8aSLars-Peter Clausen ssize_t iio_enum_read(struct iio_dev *indio_dev, 3445212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, char *buf) 3455212cc8aSLars-Peter Clausen { 3465212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3475212cc8aSLars-Peter Clausen int i; 3485212cc8aSLars-Peter Clausen 3495212cc8aSLars-Peter Clausen if (!e->get) 3505212cc8aSLars-Peter Clausen return -EINVAL; 3515212cc8aSLars-Peter Clausen 3525212cc8aSLars-Peter Clausen i = e->get(indio_dev, chan); 3535212cc8aSLars-Peter Clausen if (i < 0) 3545212cc8aSLars-Peter Clausen return i; 3555212cc8aSLars-Peter Clausen else if (i >= e->num_items) 3565212cc8aSLars-Peter Clausen return -EINVAL; 3575212cc8aSLars-Peter Clausen 358598db581SKees Cook return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]); 3595212cc8aSLars-Peter Clausen } 3605212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_read); 3615212cc8aSLars-Peter Clausen 3625212cc8aSLars-Peter Clausen ssize_t iio_enum_write(struct iio_dev *indio_dev, 3635212cc8aSLars-Peter Clausen uintptr_t priv, const struct iio_chan_spec *chan, const char *buf, 3645212cc8aSLars-Peter Clausen size_t len) 3655212cc8aSLars-Peter Clausen { 3665212cc8aSLars-Peter Clausen const struct iio_enum *e = (const struct iio_enum *)priv; 3675212cc8aSLars-Peter Clausen unsigned int i; 3685212cc8aSLars-Peter Clausen int ret; 3695212cc8aSLars-Peter Clausen 3705212cc8aSLars-Peter Clausen if (!e->set) 3715212cc8aSLars-Peter Clausen return -EINVAL; 3725212cc8aSLars-Peter Clausen 3735212cc8aSLars-Peter Clausen for (i = 0; i < e->num_items; i++) { 3745212cc8aSLars-Peter Clausen if (sysfs_streq(buf, e->items[i])) 3755212cc8aSLars-Peter Clausen break; 3765212cc8aSLars-Peter Clausen } 3775212cc8aSLars-Peter Clausen 3785212cc8aSLars-Peter Clausen if (i == e->num_items) 3795212cc8aSLars-Peter Clausen return -EINVAL; 3805212cc8aSLars-Peter Clausen 3815212cc8aSLars-Peter Clausen ret = e->set(indio_dev, chan, i); 3825212cc8aSLars-Peter Clausen return ret ? ret : len; 3835212cc8aSLars-Peter Clausen } 3845212cc8aSLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_enum_write); 3855212cc8aSLars-Peter Clausen 3863661f3f5SLars-Peter Clausen /** 3873661f3f5SLars-Peter Clausen * iio_format_value() - Formats a IIO value into its string representation 3883661f3f5SLars-Peter Clausen * @buf: The buffer to which the formated value gets written 3893661f3f5SLars-Peter Clausen * @type: One of the IIO_VAL_... constants. This decides how the val and val2 3903661f3f5SLars-Peter Clausen * parameters are formatted. 3919fbfb4b3SSrinivas Pandruvada * @vals: pointer to the values, exact meaning depends on the type parameter. 3923661f3f5SLars-Peter Clausen */ 3939fbfb4b3SSrinivas Pandruvada ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals) 394a980e046SJonathan Cameron { 3957985e7c1SLars-Peter Clausen unsigned long long tmp; 39667eedba3SMichael Hennerich bool scale_db = false; 397a980e046SJonathan Cameron 3983661f3f5SLars-Peter Clausen switch (type) { 39967eedba3SMichael Hennerich case IIO_VAL_INT: 4009fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d\n", vals[0]); 40167eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_MICRO_DB: 40267eedba3SMichael Hennerich scale_db = true; 40367eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_MICRO: 4049fbfb4b3SSrinivas Pandruvada if (vals[1] < 0) 4059fbfb4b3SSrinivas Pandruvada return sprintf(buf, "-%ld.%06u%s\n", abs(vals[0]), 4069fbfb4b3SSrinivas Pandruvada -vals[1], 40767eedba3SMichael Hennerich scale_db ? " dB" : ""); 408a980e046SJonathan Cameron else 4099fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1], 41067eedba3SMichael Hennerich scale_db ? " dB" : ""); 41167eedba3SMichael Hennerich case IIO_VAL_INT_PLUS_NANO: 4129fbfb4b3SSrinivas Pandruvada if (vals[1] < 0) 4139fbfb4b3SSrinivas Pandruvada return sprintf(buf, "-%ld.%09u\n", abs(vals[0]), 4149fbfb4b3SSrinivas Pandruvada -vals[1]); 415a980e046SJonathan Cameron else 4169fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 4177985e7c1SLars-Peter Clausen case IIO_VAL_FRACTIONAL: 4189fbfb4b3SSrinivas Pandruvada tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]); 4199fbfb4b3SSrinivas Pandruvada vals[1] = do_div(tmp, 1000000000LL); 4209fbfb4b3SSrinivas Pandruvada vals[0] = tmp; 4219fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 422103d9fb9SLars-Peter Clausen case IIO_VAL_FRACTIONAL_LOG2: 4239fbfb4b3SSrinivas Pandruvada tmp = (s64)vals[0] * 1000000000LL >> vals[1]; 4249fbfb4b3SSrinivas Pandruvada vals[1] = do_div(tmp, 1000000000LL); 4259fbfb4b3SSrinivas Pandruvada vals[0] = tmp; 4269fbfb4b3SSrinivas Pandruvada return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); 4279fbfb4b3SSrinivas Pandruvada case IIO_VAL_INT_MULTIPLE: 4289fbfb4b3SSrinivas Pandruvada { 4299fbfb4b3SSrinivas Pandruvada int i; 4309fbfb4b3SSrinivas Pandruvada int len = 0; 4319fbfb4b3SSrinivas Pandruvada 4329fbfb4b3SSrinivas Pandruvada for (i = 0; i < size; ++i) 4339fbfb4b3SSrinivas Pandruvada len += snprintf(&buf[len], PAGE_SIZE - len, "%d ", 4349fbfb4b3SSrinivas Pandruvada vals[i]); 4359fbfb4b3SSrinivas Pandruvada len += snprintf(&buf[len], PAGE_SIZE - len, "\n"); 4369fbfb4b3SSrinivas Pandruvada return len; 4379fbfb4b3SSrinivas Pandruvada } 43867eedba3SMichael Hennerich default: 439a980e046SJonathan Cameron return 0; 440a980e046SJonathan Cameron } 44167eedba3SMichael Hennerich } 442a980e046SJonathan Cameron 4433661f3f5SLars-Peter Clausen static ssize_t iio_read_channel_info(struct device *dev, 4443661f3f5SLars-Peter Clausen struct device_attribute *attr, 4453661f3f5SLars-Peter Clausen char *buf) 4463661f3f5SLars-Peter Clausen { 4473661f3f5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 4483661f3f5SLars-Peter Clausen struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 4499fbfb4b3SSrinivas Pandruvada int vals[INDIO_MAX_RAW_ELEMENTS]; 4509fbfb4b3SSrinivas Pandruvada int ret; 4519fbfb4b3SSrinivas Pandruvada int val_len = 2; 4529fbfb4b3SSrinivas Pandruvada 4539fbfb4b3SSrinivas Pandruvada if (indio_dev->info->read_raw_multi) 4549fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c, 4559fbfb4b3SSrinivas Pandruvada INDIO_MAX_RAW_ELEMENTS, 4569fbfb4b3SSrinivas Pandruvada vals, &val_len, 4579fbfb4b3SSrinivas Pandruvada this_attr->address); 4589fbfb4b3SSrinivas Pandruvada else 4599fbfb4b3SSrinivas Pandruvada ret = indio_dev->info->read_raw(indio_dev, this_attr->c, 4609fbfb4b3SSrinivas Pandruvada &vals[0], &vals[1], this_attr->address); 4613661f3f5SLars-Peter Clausen 4623661f3f5SLars-Peter Clausen if (ret < 0) 4633661f3f5SLars-Peter Clausen return ret; 4643661f3f5SLars-Peter Clausen 4659fbfb4b3SSrinivas Pandruvada return iio_format_value(buf, ret, val_len, vals); 4663661f3f5SLars-Peter Clausen } 4673661f3f5SLars-Peter Clausen 4686807d721SLars-Peter Clausen /** 4696807d721SLars-Peter Clausen * iio_str_to_fixpoint() - Parse a fixed-point number from a string 4706807d721SLars-Peter Clausen * @str: The string to parse 4716807d721SLars-Peter Clausen * @fract_mult: Multiplier for the first decimal place, should be a power of 10 4726807d721SLars-Peter Clausen * @integer: The integer part of the number 4736807d721SLars-Peter Clausen * @fract: The fractional part of the number 4746807d721SLars-Peter Clausen * 4756807d721SLars-Peter Clausen * Returns 0 on success, or a negative error code if the string could not be 4766807d721SLars-Peter Clausen * parsed. 4776807d721SLars-Peter Clausen */ 4786807d721SLars-Peter Clausen int iio_str_to_fixpoint(const char *str, int fract_mult, 4796807d721SLars-Peter Clausen int *integer, int *fract) 4806807d721SLars-Peter Clausen { 4816807d721SLars-Peter Clausen int i = 0, f = 0; 4826807d721SLars-Peter Clausen bool integer_part = true, negative = false; 4836807d721SLars-Peter Clausen 4846807d721SLars-Peter Clausen if (str[0] == '-') { 4856807d721SLars-Peter Clausen negative = true; 4866807d721SLars-Peter Clausen str++; 4876807d721SLars-Peter Clausen } else if (str[0] == '+') { 4886807d721SLars-Peter Clausen str++; 4896807d721SLars-Peter Clausen } 4906807d721SLars-Peter Clausen 4916807d721SLars-Peter Clausen while (*str) { 4926807d721SLars-Peter Clausen if ('0' <= *str && *str <= '9') { 4936807d721SLars-Peter Clausen if (integer_part) { 4946807d721SLars-Peter Clausen i = i * 10 + *str - '0'; 4956807d721SLars-Peter Clausen } else { 4966807d721SLars-Peter Clausen f += fract_mult * (*str - '0'); 4976807d721SLars-Peter Clausen fract_mult /= 10; 4986807d721SLars-Peter Clausen } 4996807d721SLars-Peter Clausen } else if (*str == '\n') { 5006807d721SLars-Peter Clausen if (*(str + 1) == '\0') 5016807d721SLars-Peter Clausen break; 5026807d721SLars-Peter Clausen else 5036807d721SLars-Peter Clausen return -EINVAL; 5046807d721SLars-Peter Clausen } else if (*str == '.' && integer_part) { 5056807d721SLars-Peter Clausen integer_part = false; 5066807d721SLars-Peter Clausen } else { 5076807d721SLars-Peter Clausen return -EINVAL; 5086807d721SLars-Peter Clausen } 5096807d721SLars-Peter Clausen str++; 5106807d721SLars-Peter Clausen } 5116807d721SLars-Peter Clausen 5126807d721SLars-Peter Clausen if (negative) { 5136807d721SLars-Peter Clausen if (i) 5146807d721SLars-Peter Clausen i = -i; 5156807d721SLars-Peter Clausen else 5166807d721SLars-Peter Clausen f = -f; 5176807d721SLars-Peter Clausen } 5186807d721SLars-Peter Clausen 5196807d721SLars-Peter Clausen *integer = i; 5206807d721SLars-Peter Clausen *fract = f; 5216807d721SLars-Peter Clausen 5226807d721SLars-Peter Clausen return 0; 5236807d721SLars-Peter Clausen } 5246807d721SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_str_to_fixpoint); 5256807d721SLars-Peter Clausen 526a980e046SJonathan Cameron static ssize_t iio_write_channel_info(struct device *dev, 527a980e046SJonathan Cameron struct device_attribute *attr, 528a980e046SJonathan Cameron const char *buf, 529a980e046SJonathan Cameron size_t len) 530a980e046SJonathan Cameron { 531e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 532a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 5336807d721SLars-Peter Clausen int ret, fract_mult = 100000; 5346807d721SLars-Peter Clausen int integer, fract; 535a980e046SJonathan Cameron 536a980e046SJonathan Cameron /* Assumes decimal - precision based on number of digits */ 537a980e046SJonathan Cameron if (!indio_dev->info->write_raw) 538a980e046SJonathan Cameron return -EINVAL; 539a980e046SJonathan Cameron 540a980e046SJonathan Cameron if (indio_dev->info->write_raw_get_fmt) 541a980e046SJonathan Cameron switch (indio_dev->info->write_raw_get_fmt(indio_dev, 542a980e046SJonathan Cameron this_attr->c, this_attr->address)) { 543a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_MICRO: 544a980e046SJonathan Cameron fract_mult = 100000; 545a980e046SJonathan Cameron break; 546a980e046SJonathan Cameron case IIO_VAL_INT_PLUS_NANO: 547a980e046SJonathan Cameron fract_mult = 100000000; 548a980e046SJonathan Cameron break; 549a980e046SJonathan Cameron default: 550a980e046SJonathan Cameron return -EINVAL; 551a980e046SJonathan Cameron } 552a980e046SJonathan Cameron 5536807d721SLars-Peter Clausen ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract); 5546807d721SLars-Peter Clausen if (ret) 5556807d721SLars-Peter Clausen return ret; 556a980e046SJonathan Cameron 557a980e046SJonathan Cameron ret = indio_dev->info->write_raw(indio_dev, this_attr->c, 558a980e046SJonathan Cameron integer, fract, this_attr->address); 559a980e046SJonathan Cameron if (ret) 560a980e046SJonathan Cameron return ret; 561a980e046SJonathan Cameron 562a980e046SJonathan Cameron return len; 563a980e046SJonathan Cameron } 564a980e046SJonathan Cameron 565a980e046SJonathan Cameron static 566a980e046SJonathan Cameron int __iio_device_attr_init(struct device_attribute *dev_attr, 567a980e046SJonathan Cameron const char *postfix, 568a980e046SJonathan Cameron struct iio_chan_spec const *chan, 569a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 570a980e046SJonathan Cameron struct device_attribute *attr, 571a980e046SJonathan Cameron char *buf), 572a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 573a980e046SJonathan Cameron struct device_attribute *attr, 574a980e046SJonathan Cameron const char *buf, 575a980e046SJonathan Cameron size_t len), 5763704432fSJonathan Cameron enum iio_shared_by shared_by) 577a980e046SJonathan Cameron { 5783704432fSJonathan Cameron int ret = 0; 5797bbcf7e1SLars-Peter Clausen char *name = NULL; 5803704432fSJonathan Cameron char *full_postfix; 581a980e046SJonathan Cameron sysfs_attr_init(&dev_attr->attr); 582a980e046SJonathan Cameron 583a980e046SJonathan Cameron /* Build up postfix of <extend_name>_<modifier>_postfix */ 5843704432fSJonathan Cameron if (chan->modified && (shared_by == IIO_SEPARATE)) { 585a980e046SJonathan Cameron if (chan->extend_name) 586a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", 587a980e046SJonathan Cameron iio_modifier_names[chan 588a980e046SJonathan Cameron ->channel2], 589a980e046SJonathan Cameron chan->extend_name, 590a980e046SJonathan Cameron postfix); 591a980e046SJonathan Cameron else 592a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, "%s_%s", 593a980e046SJonathan Cameron iio_modifier_names[chan 594a980e046SJonathan Cameron ->channel2], 595a980e046SJonathan Cameron postfix); 596a980e046SJonathan Cameron } else { 59777bfa8baSLars-Peter Clausen if (chan->extend_name == NULL || shared_by != IIO_SEPARATE) 598a980e046SJonathan Cameron full_postfix = kstrdup(postfix, GFP_KERNEL); 599a980e046SJonathan Cameron else 600a980e046SJonathan Cameron full_postfix = kasprintf(GFP_KERNEL, 601a980e046SJonathan Cameron "%s_%s", 602a980e046SJonathan Cameron chan->extend_name, 603a980e046SJonathan Cameron postfix); 604a980e046SJonathan Cameron } 6053704432fSJonathan Cameron if (full_postfix == NULL) 6063704432fSJonathan Cameron return -ENOMEM; 607a980e046SJonathan Cameron 608a980e046SJonathan Cameron if (chan->differential) { /* Differential can not have modifier */ 6093704432fSJonathan Cameron switch (shared_by) { 610c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 6117bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 612c006ec83SJonathan Cameron break; 613c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 6147bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 615c006ec83SJonathan Cameron iio_direction[chan->output], 616c006ec83SJonathan Cameron full_postfix); 617c006ec83SJonathan Cameron break; 6183704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 6197bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s", 620a980e046SJonathan Cameron iio_direction[chan->output], 621a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 622a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 623a980e046SJonathan Cameron full_postfix); 6243704432fSJonathan Cameron break; 6253704432fSJonathan Cameron case IIO_SEPARATE: 6263704432fSJonathan Cameron if (!chan->indexed) { 6273704432fSJonathan Cameron WARN_ON("Differential channels must be indexed\n"); 6283704432fSJonathan Cameron ret = -EINVAL; 6293704432fSJonathan Cameron goto error_free_full_postfix; 6303704432fSJonathan Cameron } 6317bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, 6323704432fSJonathan Cameron "%s_%s%d-%s%d_%s", 633a980e046SJonathan Cameron iio_direction[chan->output], 634a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 635a980e046SJonathan Cameron chan->channel, 636a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 637a980e046SJonathan Cameron chan->channel2, 638a980e046SJonathan Cameron full_postfix); 6393704432fSJonathan Cameron break; 640a980e046SJonathan Cameron } 641a980e046SJonathan Cameron } else { /* Single ended */ 6423704432fSJonathan Cameron switch (shared_by) { 643c006ec83SJonathan Cameron case IIO_SHARED_BY_ALL: 6447bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s", full_postfix); 645c006ec83SJonathan Cameron break; 646c006ec83SJonathan Cameron case IIO_SHARED_BY_DIR: 6477bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s", 648c006ec83SJonathan Cameron iio_direction[chan->output], 649c006ec83SJonathan Cameron full_postfix); 650c006ec83SJonathan Cameron break; 6513704432fSJonathan Cameron case IIO_SHARED_BY_TYPE: 6527bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 653a980e046SJonathan Cameron iio_direction[chan->output], 654a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 655a980e046SJonathan Cameron full_postfix); 6563704432fSJonathan Cameron break; 6573704432fSJonathan Cameron 6583704432fSJonathan Cameron case IIO_SEPARATE: 6593704432fSJonathan Cameron if (chan->indexed) 6607bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s%d_%s", 661a980e046SJonathan Cameron iio_direction[chan->output], 662a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 663a980e046SJonathan Cameron chan->channel, 664a980e046SJonathan Cameron full_postfix); 665a980e046SJonathan Cameron else 6667bbcf7e1SLars-Peter Clausen name = kasprintf(GFP_KERNEL, "%s_%s_%s", 667a980e046SJonathan Cameron iio_direction[chan->output], 668a980e046SJonathan Cameron iio_chan_type_name_spec[chan->type], 669a980e046SJonathan Cameron full_postfix); 6703704432fSJonathan Cameron break; 6713704432fSJonathan Cameron } 672a980e046SJonathan Cameron } 6737bbcf7e1SLars-Peter Clausen if (name == NULL) { 674a980e046SJonathan Cameron ret = -ENOMEM; 675a980e046SJonathan Cameron goto error_free_full_postfix; 676a980e046SJonathan Cameron } 6777bbcf7e1SLars-Peter Clausen dev_attr->attr.name = name; 678a980e046SJonathan Cameron 679a980e046SJonathan Cameron if (readfunc) { 680a980e046SJonathan Cameron dev_attr->attr.mode |= S_IRUGO; 681a980e046SJonathan Cameron dev_attr->show = readfunc; 682a980e046SJonathan Cameron } 683a980e046SJonathan Cameron 684a980e046SJonathan Cameron if (writefunc) { 685a980e046SJonathan Cameron dev_attr->attr.mode |= S_IWUSR; 686a980e046SJonathan Cameron dev_attr->store = writefunc; 687a980e046SJonathan Cameron } 6887bbcf7e1SLars-Peter Clausen 689a980e046SJonathan Cameron error_free_full_postfix: 690a980e046SJonathan Cameron kfree(full_postfix); 6913704432fSJonathan Cameron 692a980e046SJonathan Cameron return ret; 693a980e046SJonathan Cameron } 694a980e046SJonathan Cameron 695a980e046SJonathan Cameron static void __iio_device_attr_deinit(struct device_attribute *dev_attr) 696a980e046SJonathan Cameron { 697a980e046SJonathan Cameron kfree(dev_attr->attr.name); 698a980e046SJonathan Cameron } 699a980e046SJonathan Cameron 700a980e046SJonathan Cameron int __iio_add_chan_devattr(const char *postfix, 701a980e046SJonathan Cameron struct iio_chan_spec const *chan, 702a980e046SJonathan Cameron ssize_t (*readfunc)(struct device *dev, 703a980e046SJonathan Cameron struct device_attribute *attr, 704a980e046SJonathan Cameron char *buf), 705a980e046SJonathan Cameron ssize_t (*writefunc)(struct device *dev, 706a980e046SJonathan Cameron struct device_attribute *attr, 707a980e046SJonathan Cameron const char *buf, 708a980e046SJonathan Cameron size_t len), 709a980e046SJonathan Cameron u64 mask, 7103704432fSJonathan Cameron enum iio_shared_by shared_by, 711a980e046SJonathan Cameron struct device *dev, 712a980e046SJonathan Cameron struct list_head *attr_list) 713a980e046SJonathan Cameron { 714a980e046SJonathan Cameron int ret; 715a980e046SJonathan Cameron struct iio_dev_attr *iio_attr, *t; 716a980e046SJonathan Cameron 717670c1103SSachin Kamat iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL); 71892825ff9SHartmut Knaack if (iio_attr == NULL) 71992825ff9SHartmut Knaack return -ENOMEM; 720a980e046SJonathan Cameron ret = __iio_device_attr_init(&iio_attr->dev_attr, 721a980e046SJonathan Cameron postfix, chan, 7223704432fSJonathan Cameron readfunc, writefunc, shared_by); 723a980e046SJonathan Cameron if (ret) 724a980e046SJonathan Cameron goto error_iio_dev_attr_free; 725a980e046SJonathan Cameron iio_attr->c = chan; 726a980e046SJonathan Cameron iio_attr->address = mask; 727a980e046SJonathan Cameron list_for_each_entry(t, attr_list, l) 728a980e046SJonathan Cameron if (strcmp(t->dev_attr.attr.name, 729a980e046SJonathan Cameron iio_attr->dev_attr.attr.name) == 0) { 7303704432fSJonathan Cameron if (shared_by == IIO_SEPARATE) 731a980e046SJonathan Cameron dev_err(dev, "tried to double register : %s\n", 732a980e046SJonathan Cameron t->dev_attr.attr.name); 733a980e046SJonathan Cameron ret = -EBUSY; 734a980e046SJonathan Cameron goto error_device_attr_deinit; 735a980e046SJonathan Cameron } 736a980e046SJonathan Cameron list_add(&iio_attr->l, attr_list); 737a980e046SJonathan Cameron 738a980e046SJonathan Cameron return 0; 739a980e046SJonathan Cameron 740a980e046SJonathan Cameron error_device_attr_deinit: 741a980e046SJonathan Cameron __iio_device_attr_deinit(&iio_attr->dev_attr); 742a980e046SJonathan Cameron error_iio_dev_attr_free: 743a980e046SJonathan Cameron kfree(iio_attr); 744a980e046SJonathan Cameron return ret; 745a980e046SJonathan Cameron } 746a980e046SJonathan Cameron 7473704432fSJonathan Cameron static int iio_device_add_info_mask_type(struct iio_dev *indio_dev, 7483704432fSJonathan Cameron struct iio_chan_spec const *chan, 7493704432fSJonathan Cameron enum iio_shared_by shared_by, 7503704432fSJonathan Cameron const long *infomask) 7513704432fSJonathan Cameron { 7523704432fSJonathan Cameron int i, ret, attrcount = 0; 7533704432fSJonathan Cameron 7543704432fSJonathan Cameron for_each_set_bit(i, infomask, sizeof(infomask)*8) { 755ef4b4856SJonathan Cameron if (i >= ARRAY_SIZE(iio_chan_info_postfix)) 756ef4b4856SJonathan Cameron return -EINVAL; 7573704432fSJonathan Cameron ret = __iio_add_chan_devattr(iio_chan_info_postfix[i], 7583704432fSJonathan Cameron chan, 7593704432fSJonathan Cameron &iio_read_channel_info, 7603704432fSJonathan Cameron &iio_write_channel_info, 7613704432fSJonathan Cameron i, 7623704432fSJonathan Cameron shared_by, 7633704432fSJonathan Cameron &indio_dev->dev, 7643704432fSJonathan Cameron &indio_dev->channel_attr_list); 7653704432fSJonathan Cameron if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) 7663704432fSJonathan Cameron continue; 7673704432fSJonathan Cameron else if (ret < 0) 7683704432fSJonathan Cameron return ret; 7693704432fSJonathan Cameron attrcount++; 7703704432fSJonathan Cameron } 7713704432fSJonathan Cameron 7723704432fSJonathan Cameron return attrcount; 7733704432fSJonathan Cameron } 7743704432fSJonathan Cameron 775a980e046SJonathan Cameron static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev, 776a980e046SJonathan Cameron struct iio_chan_spec const *chan) 777a980e046SJonathan Cameron { 778a980e046SJonathan Cameron int ret, attrcount = 0; 779a980e046SJonathan Cameron const struct iio_chan_spec_ext_info *ext_info; 780a980e046SJonathan Cameron 781a980e046SJonathan Cameron if (chan->channel < 0) 782a980e046SJonathan Cameron return 0; 7833704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 7843704432fSJonathan Cameron IIO_SEPARATE, 7853704432fSJonathan Cameron &chan->info_mask_separate); 7868655cc49SJonathan Cameron if (ret < 0) 7873704432fSJonathan Cameron return ret; 7883704432fSJonathan Cameron attrcount += ret; 7893704432fSJonathan Cameron 7903704432fSJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 7913704432fSJonathan Cameron IIO_SHARED_BY_TYPE, 7923704432fSJonathan Cameron &chan->info_mask_shared_by_type); 7933704432fSJonathan Cameron if (ret < 0) 7943704432fSJonathan Cameron return ret; 7953704432fSJonathan Cameron attrcount += ret; 796a980e046SJonathan Cameron 797c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 798c006ec83SJonathan Cameron IIO_SHARED_BY_DIR, 799c006ec83SJonathan Cameron &chan->info_mask_shared_by_dir); 800c006ec83SJonathan Cameron if (ret < 0) 801c006ec83SJonathan Cameron return ret; 802c006ec83SJonathan Cameron attrcount += ret; 803c006ec83SJonathan Cameron 804c006ec83SJonathan Cameron ret = iio_device_add_info_mask_type(indio_dev, chan, 805c006ec83SJonathan Cameron IIO_SHARED_BY_ALL, 806c006ec83SJonathan Cameron &chan->info_mask_shared_by_all); 807c006ec83SJonathan Cameron if (ret < 0) 808c006ec83SJonathan Cameron return ret; 809c006ec83SJonathan Cameron attrcount += ret; 810c006ec83SJonathan Cameron 811a980e046SJonathan Cameron if (chan->ext_info) { 812a980e046SJonathan Cameron unsigned int i = 0; 813a980e046SJonathan Cameron for (ext_info = chan->ext_info; ext_info->name; ext_info++) { 814a980e046SJonathan Cameron ret = __iio_add_chan_devattr(ext_info->name, 815a980e046SJonathan Cameron chan, 816a980e046SJonathan Cameron ext_info->read ? 817a980e046SJonathan Cameron &iio_read_channel_ext_info : NULL, 818a980e046SJonathan Cameron ext_info->write ? 819a980e046SJonathan Cameron &iio_write_channel_ext_info : NULL, 820a980e046SJonathan Cameron i, 821a980e046SJonathan Cameron ext_info->shared, 822a980e046SJonathan Cameron &indio_dev->dev, 823a980e046SJonathan Cameron &indio_dev->channel_attr_list); 824a980e046SJonathan Cameron i++; 825a980e046SJonathan Cameron if (ret == -EBUSY && ext_info->shared) 826a980e046SJonathan Cameron continue; 827a980e046SJonathan Cameron 828a980e046SJonathan Cameron if (ret) 8293704432fSJonathan Cameron return ret; 830a980e046SJonathan Cameron 831a980e046SJonathan Cameron attrcount++; 832a980e046SJonathan Cameron } 833a980e046SJonathan Cameron } 834a980e046SJonathan Cameron 8353704432fSJonathan Cameron return attrcount; 836a980e046SJonathan Cameron } 837a980e046SJonathan Cameron 83884088ebdSLars-Peter Clausen /** 83984088ebdSLars-Peter Clausen * iio_free_chan_devattr_list() - Free a list of IIO device attributes 84084088ebdSLars-Peter Clausen * @attr_list: List of IIO device attributes 84184088ebdSLars-Peter Clausen * 84284088ebdSLars-Peter Clausen * This function frees the memory allocated for each of the IIO device 84384088ebdSLars-Peter Clausen * attributes in the list. Note: if you want to reuse the list after calling 84484088ebdSLars-Peter Clausen * this function you have to reinitialize it using INIT_LIST_HEAD(). 84584088ebdSLars-Peter Clausen */ 84684088ebdSLars-Peter Clausen void iio_free_chan_devattr_list(struct list_head *attr_list) 847a980e046SJonathan Cameron { 84884088ebdSLars-Peter Clausen struct iio_dev_attr *p, *n; 84984088ebdSLars-Peter Clausen 85084088ebdSLars-Peter Clausen list_for_each_entry_safe(p, n, attr_list, l) { 851a980e046SJonathan Cameron kfree(p->dev_attr.attr.name); 852a980e046SJonathan Cameron kfree(p); 853a980e046SJonathan Cameron } 85484088ebdSLars-Peter Clausen } 855a980e046SJonathan Cameron 856a980e046SJonathan Cameron static ssize_t iio_show_dev_name(struct device *dev, 857a980e046SJonathan Cameron struct device_attribute *attr, 858a980e046SJonathan Cameron char *buf) 859a980e046SJonathan Cameron { 860e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 861598db581SKees Cook return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name); 862a980e046SJonathan Cameron } 863a980e046SJonathan Cameron 864a980e046SJonathan Cameron static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL); 865a980e046SJonathan Cameron 866a980e046SJonathan Cameron static int iio_device_register_sysfs(struct iio_dev *indio_dev) 867a980e046SJonathan Cameron { 868a980e046SJonathan Cameron int i, ret = 0, attrcount, attrn, attrcount_orig = 0; 86984088ebdSLars-Peter Clausen struct iio_dev_attr *p; 870a980e046SJonathan Cameron struct attribute **attr; 871a980e046SJonathan Cameron 872a980e046SJonathan Cameron /* First count elements in any existing group */ 873a980e046SJonathan Cameron if (indio_dev->info->attrs) { 874a980e046SJonathan Cameron attr = indio_dev->info->attrs->attrs; 875a980e046SJonathan Cameron while (*attr++ != NULL) 876a980e046SJonathan Cameron attrcount_orig++; 877a980e046SJonathan Cameron } 878a980e046SJonathan Cameron attrcount = attrcount_orig; 879a980e046SJonathan Cameron /* 880a980e046SJonathan Cameron * New channel registration method - relies on the fact a group does 881d25b3808SPeter Meerwald * not need to be initialized if its name is NULL. 882a980e046SJonathan Cameron */ 883a980e046SJonathan Cameron if (indio_dev->channels) 884a980e046SJonathan Cameron for (i = 0; i < indio_dev->num_channels; i++) { 885a980e046SJonathan Cameron ret = iio_device_add_channel_sysfs(indio_dev, 886a980e046SJonathan Cameron &indio_dev 887a980e046SJonathan Cameron ->channels[i]); 888a980e046SJonathan Cameron if (ret < 0) 889a980e046SJonathan Cameron goto error_clear_attrs; 890a980e046SJonathan Cameron attrcount += ret; 891a980e046SJonathan Cameron } 892a980e046SJonathan Cameron 893a980e046SJonathan Cameron if (indio_dev->name) 894a980e046SJonathan Cameron attrcount++; 895a980e046SJonathan Cameron 896a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1, 897a980e046SJonathan Cameron sizeof(indio_dev->chan_attr_group.attrs[0]), 898a980e046SJonathan Cameron GFP_KERNEL); 899a980e046SJonathan Cameron if (indio_dev->chan_attr_group.attrs == NULL) { 900a980e046SJonathan Cameron ret = -ENOMEM; 901a980e046SJonathan Cameron goto error_clear_attrs; 902a980e046SJonathan Cameron } 903a980e046SJonathan Cameron /* Copy across original attributes */ 904a980e046SJonathan Cameron if (indio_dev->info->attrs) 905a980e046SJonathan Cameron memcpy(indio_dev->chan_attr_group.attrs, 906a980e046SJonathan Cameron indio_dev->info->attrs->attrs, 907a980e046SJonathan Cameron sizeof(indio_dev->chan_attr_group.attrs[0]) 908a980e046SJonathan Cameron *attrcount_orig); 909a980e046SJonathan Cameron attrn = attrcount_orig; 910a980e046SJonathan Cameron /* Add all elements from the list. */ 911a980e046SJonathan Cameron list_for_each_entry(p, &indio_dev->channel_attr_list, l) 912a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr; 913a980e046SJonathan Cameron if (indio_dev->name) 914a980e046SJonathan Cameron indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr; 915a980e046SJonathan Cameron 916a980e046SJonathan Cameron indio_dev->groups[indio_dev->groupcounter++] = 917a980e046SJonathan Cameron &indio_dev->chan_attr_group; 918a980e046SJonathan Cameron 919a980e046SJonathan Cameron return 0; 920a980e046SJonathan Cameron 921a980e046SJonathan Cameron error_clear_attrs: 92284088ebdSLars-Peter Clausen iio_free_chan_devattr_list(&indio_dev->channel_attr_list); 923a980e046SJonathan Cameron 924a980e046SJonathan Cameron return ret; 925a980e046SJonathan Cameron } 926a980e046SJonathan Cameron 927a980e046SJonathan Cameron static void iio_device_unregister_sysfs(struct iio_dev *indio_dev) 928a980e046SJonathan Cameron { 929a980e046SJonathan Cameron 93084088ebdSLars-Peter Clausen iio_free_chan_devattr_list(&indio_dev->channel_attr_list); 931a980e046SJonathan Cameron kfree(indio_dev->chan_attr_group.attrs); 932a980e046SJonathan Cameron } 933a980e046SJonathan Cameron 934a980e046SJonathan Cameron static void iio_dev_release(struct device *device) 935a980e046SJonathan Cameron { 936e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(device); 937a980e046SJonathan Cameron if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) 938a980e046SJonathan Cameron iio_device_unregister_trigger_consumer(indio_dev); 939a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 940a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 941e407fd65SLars-Peter Clausen 9429e69c935SLars-Peter Clausen iio_buffer_put(indio_dev->buffer); 9439e69c935SLars-Peter Clausen 944e407fd65SLars-Peter Clausen ida_simple_remove(&iio_ida, indio_dev->id); 945e407fd65SLars-Peter Clausen kfree(indio_dev); 946a980e046SJonathan Cameron } 947a980e046SJonathan Cameron 94817d82b47SGuenter Roeck struct device_type iio_device_type = { 949a980e046SJonathan Cameron .name = "iio_device", 950a980e046SJonathan Cameron .release = iio_dev_release, 951a980e046SJonathan Cameron }; 952a980e046SJonathan Cameron 953a7e57dceSSachin Kamat /** 954a7e57dceSSachin Kamat * iio_device_alloc() - allocate an iio_dev from a driver 955a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 956a7e57dceSSachin Kamat **/ 9577cbb7537SLars-Peter Clausen struct iio_dev *iio_device_alloc(int sizeof_priv) 958a980e046SJonathan Cameron { 959a980e046SJonathan Cameron struct iio_dev *dev; 960a980e046SJonathan Cameron size_t alloc_size; 961a980e046SJonathan Cameron 962a980e046SJonathan Cameron alloc_size = sizeof(struct iio_dev); 963a980e046SJonathan Cameron if (sizeof_priv) { 964a980e046SJonathan Cameron alloc_size = ALIGN(alloc_size, IIO_ALIGN); 965a980e046SJonathan Cameron alloc_size += sizeof_priv; 966a980e046SJonathan Cameron } 967a980e046SJonathan Cameron /* ensure 32-byte alignment of whole construct ? */ 968a980e046SJonathan Cameron alloc_size += IIO_ALIGN - 1; 969a980e046SJonathan Cameron 970a980e046SJonathan Cameron dev = kzalloc(alloc_size, GFP_KERNEL); 971a980e046SJonathan Cameron 972a980e046SJonathan Cameron if (dev) { 973a980e046SJonathan Cameron dev->dev.groups = dev->groups; 97417d82b47SGuenter Roeck dev->dev.type = &iio_device_type; 975a980e046SJonathan Cameron dev->dev.bus = &iio_bus_type; 976a980e046SJonathan Cameron device_initialize(&dev->dev); 977a980e046SJonathan Cameron dev_set_drvdata(&dev->dev, (void *)dev); 978a980e046SJonathan Cameron mutex_init(&dev->mlock); 979a980e046SJonathan Cameron mutex_init(&dev->info_exist_lock); 980e407fd65SLars-Peter Clausen INIT_LIST_HEAD(&dev->channel_attr_list); 981a980e046SJonathan Cameron 982a980e046SJonathan Cameron dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL); 983a980e046SJonathan Cameron if (dev->id < 0) { 984a980e046SJonathan Cameron /* cannot use a dev_err as the name isn't available */ 9853176dd5dSSachin Kamat pr_err("failed to get device id\n"); 986a980e046SJonathan Cameron kfree(dev); 987a980e046SJonathan Cameron return NULL; 988a980e046SJonathan Cameron } 989a980e046SJonathan Cameron dev_set_name(&dev->dev, "iio:device%d", dev->id); 99084b36ce5SJonathan Cameron INIT_LIST_HEAD(&dev->buffer_list); 991a980e046SJonathan Cameron } 992a980e046SJonathan Cameron 993a980e046SJonathan Cameron return dev; 994a980e046SJonathan Cameron } 9957cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_alloc); 996a980e046SJonathan Cameron 997a7e57dceSSachin Kamat /** 998a7e57dceSSachin Kamat * iio_device_free() - free an iio_dev from a driver 999a7e57dceSSachin Kamat * @dev: the iio_dev associated with the device 1000a7e57dceSSachin Kamat **/ 10017cbb7537SLars-Peter Clausen void iio_device_free(struct iio_dev *dev) 1002a980e046SJonathan Cameron { 1003e407fd65SLars-Peter Clausen if (dev) 1004e407fd65SLars-Peter Clausen put_device(&dev->dev); 1005a980e046SJonathan Cameron } 10067cbb7537SLars-Peter Clausen EXPORT_SYMBOL(iio_device_free); 1007a980e046SJonathan Cameron 10089dabaf5eSGrygorii Strashko static void devm_iio_device_release(struct device *dev, void *res) 10099dabaf5eSGrygorii Strashko { 10109dabaf5eSGrygorii Strashko iio_device_free(*(struct iio_dev **)res); 10119dabaf5eSGrygorii Strashko } 10129dabaf5eSGrygorii Strashko 10139dabaf5eSGrygorii Strashko static int devm_iio_device_match(struct device *dev, void *res, void *data) 10149dabaf5eSGrygorii Strashko { 10159dabaf5eSGrygorii Strashko struct iio_dev **r = res; 10169dabaf5eSGrygorii Strashko if (!r || !*r) { 10179dabaf5eSGrygorii Strashko WARN_ON(!r || !*r); 10189dabaf5eSGrygorii Strashko return 0; 10199dabaf5eSGrygorii Strashko } 10209dabaf5eSGrygorii Strashko return *r == data; 10219dabaf5eSGrygorii Strashko } 10229dabaf5eSGrygorii Strashko 1023a7e57dceSSachin Kamat /** 1024a7e57dceSSachin Kamat * devm_iio_device_alloc - Resource-managed iio_device_alloc() 1025a7e57dceSSachin Kamat * @dev: Device to allocate iio_dev for 1026a7e57dceSSachin Kamat * @sizeof_priv: Space to allocate for private structure. 1027a7e57dceSSachin Kamat * 1028a7e57dceSSachin Kamat * Managed iio_device_alloc. iio_dev allocated with this function is 1029a7e57dceSSachin Kamat * automatically freed on driver detach. 1030a7e57dceSSachin Kamat * 1031a7e57dceSSachin Kamat * If an iio_dev allocated with this function needs to be freed separately, 1032a7e57dceSSachin Kamat * devm_iio_device_free() must be used. 1033a7e57dceSSachin Kamat * 1034a7e57dceSSachin Kamat * RETURNS: 1035a7e57dceSSachin Kamat * Pointer to allocated iio_dev on success, NULL on failure. 1036a7e57dceSSachin Kamat */ 10379dabaf5eSGrygorii Strashko struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv) 10389dabaf5eSGrygorii Strashko { 10399dabaf5eSGrygorii Strashko struct iio_dev **ptr, *iio_dev; 10409dabaf5eSGrygorii Strashko 10419dabaf5eSGrygorii Strashko ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr), 10429dabaf5eSGrygorii Strashko GFP_KERNEL); 10439dabaf5eSGrygorii Strashko if (!ptr) 10449dabaf5eSGrygorii Strashko return NULL; 10459dabaf5eSGrygorii Strashko 10469dabaf5eSGrygorii Strashko /* use raw alloc_dr for kmalloc caller tracing */ 10479dabaf5eSGrygorii Strashko iio_dev = iio_device_alloc(sizeof_priv); 10489dabaf5eSGrygorii Strashko if (iio_dev) { 10499dabaf5eSGrygorii Strashko *ptr = iio_dev; 10509dabaf5eSGrygorii Strashko devres_add(dev, ptr); 10519dabaf5eSGrygorii Strashko } else { 10529dabaf5eSGrygorii Strashko devres_free(ptr); 10539dabaf5eSGrygorii Strashko } 10549dabaf5eSGrygorii Strashko 10559dabaf5eSGrygorii Strashko return iio_dev; 10569dabaf5eSGrygorii Strashko } 10579dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_alloc); 10589dabaf5eSGrygorii Strashko 1059a7e57dceSSachin Kamat /** 1060a7e57dceSSachin Kamat * devm_iio_device_free - Resource-managed iio_device_free() 1061a7e57dceSSachin Kamat * @dev: Device this iio_dev belongs to 1062a7e57dceSSachin Kamat * @iio_dev: the iio_dev associated with the device 1063a7e57dceSSachin Kamat * 1064a7e57dceSSachin Kamat * Free iio_dev allocated with devm_iio_device_alloc(). 1065a7e57dceSSachin Kamat */ 10669dabaf5eSGrygorii Strashko void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev) 10679dabaf5eSGrygorii Strashko { 10689dabaf5eSGrygorii Strashko int rc; 10699dabaf5eSGrygorii Strashko 10709dabaf5eSGrygorii Strashko rc = devres_release(dev, devm_iio_device_release, 10719dabaf5eSGrygorii Strashko devm_iio_device_match, iio_dev); 10729dabaf5eSGrygorii Strashko WARN_ON(rc); 10739dabaf5eSGrygorii Strashko } 10749dabaf5eSGrygorii Strashko EXPORT_SYMBOL_GPL(devm_iio_device_free); 10759dabaf5eSGrygorii Strashko 1076a980e046SJonathan Cameron /** 1077a980e046SJonathan Cameron * iio_chrdev_open() - chrdev file open for buffer access and ioctls 1078a980e046SJonathan Cameron **/ 1079a980e046SJonathan Cameron static int iio_chrdev_open(struct inode *inode, struct file *filp) 1080a980e046SJonathan Cameron { 1081a980e046SJonathan Cameron struct iio_dev *indio_dev = container_of(inode->i_cdev, 1082a980e046SJonathan Cameron struct iio_dev, chrdev); 1083a980e046SJonathan Cameron 1084a980e046SJonathan Cameron if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags)) 1085a980e046SJonathan Cameron return -EBUSY; 1086a980e046SJonathan Cameron 1087cadc2125SLars-Peter Clausen iio_device_get(indio_dev); 1088cadc2125SLars-Peter Clausen 1089a980e046SJonathan Cameron filp->private_data = indio_dev; 1090a980e046SJonathan Cameron 1091a980e046SJonathan Cameron return 0; 1092a980e046SJonathan Cameron } 1093a980e046SJonathan Cameron 1094a980e046SJonathan Cameron /** 1095a980e046SJonathan Cameron * iio_chrdev_release() - chrdev file close buffer access and ioctls 1096a980e046SJonathan Cameron **/ 1097a980e046SJonathan Cameron static int iio_chrdev_release(struct inode *inode, struct file *filp) 1098a980e046SJonathan Cameron { 1099a980e046SJonathan Cameron struct iio_dev *indio_dev = container_of(inode->i_cdev, 1100a980e046SJonathan Cameron struct iio_dev, chrdev); 1101a980e046SJonathan Cameron clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags); 1102cadc2125SLars-Peter Clausen iio_device_put(indio_dev); 1103cadc2125SLars-Peter Clausen 1104a980e046SJonathan Cameron return 0; 1105a980e046SJonathan Cameron } 1106a980e046SJonathan Cameron 1107a980e046SJonathan Cameron /* Somewhat of a cross file organization violation - ioctls here are actually 1108a980e046SJonathan Cameron * event related */ 1109a980e046SJonathan Cameron static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 1110a980e046SJonathan Cameron { 1111a980e046SJonathan Cameron struct iio_dev *indio_dev = filp->private_data; 1112a980e046SJonathan Cameron int __user *ip = (int __user *)arg; 1113a980e046SJonathan Cameron int fd; 1114a980e046SJonathan Cameron 1115f18e7a06SLars-Peter Clausen if (!indio_dev->info) 1116f18e7a06SLars-Peter Clausen return -ENODEV; 1117f18e7a06SLars-Peter Clausen 1118a980e046SJonathan Cameron if (cmd == IIO_GET_EVENT_FD_IOCTL) { 1119a980e046SJonathan Cameron fd = iio_event_getfd(indio_dev); 1120a980e046SJonathan Cameron if (copy_to_user(ip, &fd, sizeof(fd))) 1121a980e046SJonathan Cameron return -EFAULT; 1122a980e046SJonathan Cameron return 0; 1123a980e046SJonathan Cameron } 1124a980e046SJonathan Cameron return -EINVAL; 1125a980e046SJonathan Cameron } 1126a980e046SJonathan Cameron 1127a980e046SJonathan Cameron static const struct file_operations iio_buffer_fileops = { 1128a980e046SJonathan Cameron .read = iio_buffer_read_first_n_outer_addr, 1129a980e046SJonathan Cameron .release = iio_chrdev_release, 1130a980e046SJonathan Cameron .open = iio_chrdev_open, 1131a980e046SJonathan Cameron .poll = iio_buffer_poll_addr, 1132a980e046SJonathan Cameron .owner = THIS_MODULE, 1133a980e046SJonathan Cameron .llseek = noop_llseek, 1134a980e046SJonathan Cameron .unlocked_ioctl = iio_ioctl, 1135a980e046SJonathan Cameron .compat_ioctl = iio_ioctl, 1136a980e046SJonathan Cameron }; 1137a980e046SJonathan Cameron 1138a980e046SJonathan Cameron static const struct iio_buffer_setup_ops noop_ring_setup_ops; 1139a980e046SJonathan Cameron 1140a7e57dceSSachin Kamat /** 1141a7e57dceSSachin Kamat * iio_device_register() - register a device with the IIO subsystem 1142a7e57dceSSachin Kamat * @indio_dev: Device structure filled by the device driver 1143a7e57dceSSachin Kamat **/ 1144a980e046SJonathan Cameron int iio_device_register(struct iio_dev *indio_dev) 1145a980e046SJonathan Cameron { 1146a980e046SJonathan Cameron int ret; 1147a980e046SJonathan Cameron 114817d82b47SGuenter Roeck /* If the calling driver did not initialize of_node, do it here */ 114917d82b47SGuenter Roeck if (!indio_dev->dev.of_node && indio_dev->dev.parent) 115017d82b47SGuenter Roeck indio_dev->dev.of_node = indio_dev->dev.parent->of_node; 115117d82b47SGuenter Roeck 1152a980e046SJonathan Cameron /* configure elements for the chrdev */ 1153a980e046SJonathan Cameron indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id); 1154a980e046SJonathan Cameron 1155a980e046SJonathan Cameron ret = iio_device_register_debugfs(indio_dev); 1156a980e046SJonathan Cameron if (ret) { 1157a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1158a980e046SJonathan Cameron "Failed to register debugfs interfaces\n"); 115992825ff9SHartmut Knaack return ret; 1160a980e046SJonathan Cameron } 1161*3e1b6c95SLars-Peter Clausen 1162*3e1b6c95SLars-Peter Clausen ret = iio_buffer_alloc_sysfs_and_mask(indio_dev); 1163*3e1b6c95SLars-Peter Clausen if (ret) { 1164*3e1b6c95SLars-Peter Clausen dev_err(indio_dev->dev.parent, 1165*3e1b6c95SLars-Peter Clausen "Failed to create buffer sysfs interfaces\n"); 1166*3e1b6c95SLars-Peter Clausen goto error_unreg_debugfs; 1167*3e1b6c95SLars-Peter Clausen } 1168*3e1b6c95SLars-Peter Clausen 1169a980e046SJonathan Cameron ret = iio_device_register_sysfs(indio_dev); 1170a980e046SJonathan Cameron if (ret) { 1171a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1172a980e046SJonathan Cameron "Failed to register sysfs interfaces\n"); 1173*3e1b6c95SLars-Peter Clausen goto error_buffer_free_sysfs; 1174a980e046SJonathan Cameron } 1175a980e046SJonathan Cameron ret = iio_device_register_eventset(indio_dev); 1176a980e046SJonathan Cameron if (ret) { 1177a980e046SJonathan Cameron dev_err(indio_dev->dev.parent, 1178a980e046SJonathan Cameron "Failed to register event set\n"); 1179a980e046SJonathan Cameron goto error_free_sysfs; 1180a980e046SJonathan Cameron } 1181a980e046SJonathan Cameron if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) 1182a980e046SJonathan Cameron iio_device_register_trigger_consumer(indio_dev); 1183a980e046SJonathan Cameron 1184a980e046SJonathan Cameron if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) && 1185a980e046SJonathan Cameron indio_dev->setup_ops == NULL) 1186a980e046SJonathan Cameron indio_dev->setup_ops = &noop_ring_setup_ops; 1187a980e046SJonathan Cameron 1188a980e046SJonathan Cameron cdev_init(&indio_dev->chrdev, &iio_buffer_fileops); 1189a980e046SJonathan Cameron indio_dev->chrdev.owner = indio_dev->info->driver_module; 11900d5b7daeSLars-Peter Clausen indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj; 1191a980e046SJonathan Cameron ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1); 1192a980e046SJonathan Cameron if (ret < 0) 11930d5b7daeSLars-Peter Clausen goto error_unreg_eventset; 1194a980e046SJonathan Cameron 11950d5b7daeSLars-Peter Clausen ret = device_add(&indio_dev->dev); 11960d5b7daeSLars-Peter Clausen if (ret < 0) 11970d5b7daeSLars-Peter Clausen goto error_cdev_del; 11980d5b7daeSLars-Peter Clausen 11990d5b7daeSLars-Peter Clausen return 0; 12000d5b7daeSLars-Peter Clausen error_cdev_del: 12010d5b7daeSLars-Peter Clausen cdev_del(&indio_dev->chrdev); 1202a980e046SJonathan Cameron error_unreg_eventset: 1203a980e046SJonathan Cameron iio_device_unregister_eventset(indio_dev); 1204a980e046SJonathan Cameron error_free_sysfs: 1205a980e046SJonathan Cameron iio_device_unregister_sysfs(indio_dev); 1206*3e1b6c95SLars-Peter Clausen error_buffer_free_sysfs: 1207*3e1b6c95SLars-Peter Clausen iio_buffer_free_sysfs_and_mask(indio_dev); 1208a980e046SJonathan Cameron error_unreg_debugfs: 1209a980e046SJonathan Cameron iio_device_unregister_debugfs(indio_dev); 1210a980e046SJonathan Cameron return ret; 1211a980e046SJonathan Cameron } 1212a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_register); 1213a980e046SJonathan Cameron 1214a7e57dceSSachin Kamat /** 1215a7e57dceSSachin Kamat * iio_device_unregister() - unregister a device from the IIO subsystem 1216a7e57dceSSachin Kamat * @indio_dev: Device structure representing the device. 1217a7e57dceSSachin Kamat **/ 1218a980e046SJonathan Cameron void iio_device_unregister(struct iio_dev *indio_dev) 1219a980e046SJonathan Cameron { 1220a980e046SJonathan Cameron mutex_lock(&indio_dev->info_exist_lock); 1221a87c82e4SLars-Peter Clausen 1222a87c82e4SLars-Peter Clausen device_del(&indio_dev->dev); 1223a87c82e4SLars-Peter Clausen 12240d5b7daeSLars-Peter Clausen if (indio_dev->chrdev.dev) 12250d5b7daeSLars-Peter Clausen cdev_del(&indio_dev->chrdev); 1226bc4c9612SLars-Peter Clausen iio_device_unregister_debugfs(indio_dev); 12270d5b7daeSLars-Peter Clausen 1228a87c82e4SLars-Peter Clausen iio_disable_all_buffers(indio_dev); 1229a87c82e4SLars-Peter Clausen 1230a980e046SJonathan Cameron indio_dev->info = NULL; 1231d2f0a48fSLars-Peter Clausen 1232d2f0a48fSLars-Peter Clausen iio_device_wakeup_eventset(indio_dev); 1233d2f0a48fSLars-Peter Clausen iio_buffer_wakeup_poll(indio_dev); 1234d2f0a48fSLars-Peter Clausen 1235a980e046SJonathan Cameron mutex_unlock(&indio_dev->info_exist_lock); 1236*3e1b6c95SLars-Peter Clausen 1237*3e1b6c95SLars-Peter Clausen iio_buffer_free_sysfs_and_mask(indio_dev); 1238a980e046SJonathan Cameron } 1239a980e046SJonathan Cameron EXPORT_SYMBOL(iio_device_unregister); 12408caa07c0SSachin Kamat 12418caa07c0SSachin Kamat static void devm_iio_device_unreg(struct device *dev, void *res) 12428caa07c0SSachin Kamat { 12438caa07c0SSachin Kamat iio_device_unregister(*(struct iio_dev **)res); 12448caa07c0SSachin Kamat } 12458caa07c0SSachin Kamat 12468caa07c0SSachin Kamat /** 12478caa07c0SSachin Kamat * devm_iio_device_register - Resource-managed iio_device_register() 12488caa07c0SSachin Kamat * @dev: Device to allocate iio_dev for 12498caa07c0SSachin Kamat * @indio_dev: Device structure filled by the device driver 12508caa07c0SSachin Kamat * 12518caa07c0SSachin Kamat * Managed iio_device_register. The IIO device registered with this 12528caa07c0SSachin Kamat * function is automatically unregistered on driver detach. This function 12538caa07c0SSachin Kamat * calls iio_device_register() internally. Refer to that function for more 12548caa07c0SSachin Kamat * information. 12558caa07c0SSachin Kamat * 12568caa07c0SSachin Kamat * If an iio_dev registered with this function needs to be unregistered 12578caa07c0SSachin Kamat * separately, devm_iio_device_unregister() must be used. 12588caa07c0SSachin Kamat * 12598caa07c0SSachin Kamat * RETURNS: 12608caa07c0SSachin Kamat * 0 on success, negative error number on failure. 12618caa07c0SSachin Kamat */ 12628caa07c0SSachin Kamat int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev) 12638caa07c0SSachin Kamat { 12648caa07c0SSachin Kamat struct iio_dev **ptr; 12658caa07c0SSachin Kamat int ret; 12668caa07c0SSachin Kamat 12678caa07c0SSachin Kamat ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL); 12688caa07c0SSachin Kamat if (!ptr) 12698caa07c0SSachin Kamat return -ENOMEM; 12708caa07c0SSachin Kamat 12718caa07c0SSachin Kamat *ptr = indio_dev; 12728caa07c0SSachin Kamat ret = iio_device_register(indio_dev); 12738caa07c0SSachin Kamat if (!ret) 12748caa07c0SSachin Kamat devres_add(dev, ptr); 12758caa07c0SSachin Kamat else 12768caa07c0SSachin Kamat devres_free(ptr); 12778caa07c0SSachin Kamat 12788caa07c0SSachin Kamat return ret; 12798caa07c0SSachin Kamat } 12808caa07c0SSachin Kamat EXPORT_SYMBOL_GPL(devm_iio_device_register); 12818caa07c0SSachin Kamat 12828caa07c0SSachin Kamat /** 12838caa07c0SSachin Kamat * devm_iio_device_unregister - Resource-managed iio_device_unregister() 12848caa07c0SSachin Kamat * @dev: Device this iio_dev belongs to 12858caa07c0SSachin Kamat * @indio_dev: the iio_dev associated with the device 12868caa07c0SSachin Kamat * 12878caa07c0SSachin Kamat * Unregister iio_dev registered with devm_iio_device_register(). 12888caa07c0SSachin Kamat */ 12898caa07c0SSachin Kamat void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev) 12908caa07c0SSachin Kamat { 12918caa07c0SSachin Kamat int rc; 12928caa07c0SSachin Kamat 12938caa07c0SSachin Kamat rc = devres_release(dev, devm_iio_device_unreg, 12948caa07c0SSachin Kamat devm_iio_device_match, indio_dev); 12958caa07c0SSachin Kamat WARN_ON(rc); 12968caa07c0SSachin Kamat } 12978caa07c0SSachin Kamat EXPORT_SYMBOL_GPL(devm_iio_device_unregister); 12988caa07c0SSachin Kamat 1299a980e046SJonathan Cameron subsys_initcall(iio_init); 1300a980e046SJonathan Cameron module_exit(iio_exit); 1301a980e046SJonathan Cameron 1302c8b95952SJonathan Cameron MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); 1303a980e046SJonathan Cameron MODULE_DESCRIPTION("Industrial I/O core"); 1304a980e046SJonathan Cameron MODULE_LICENSE("GPL"); 1305