Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1 | /* The industrial I/O core |
| 2 | * |
| 3 | * Copyright (c) 2008 Jonathan Cameron |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published by |
| 7 | * the Free Software Foundation. |
| 8 | * |
| 9 | * Based on elements of hwmon and input subsystems. |
| 10 | */ |
| 11 | |
Sachin Kamat | 3176dd5 | 2013-10-24 12:53:00 +0100 | [diff] [blame] | 12 | #define pr_fmt(fmt) "iio-core: " fmt |
| 13 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/idr.h> |
| 17 | #include <linux/kdev_t.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/device.h> |
| 20 | #include <linux/fs.h> |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 21 | #include <linux/poll.h> |
Jonathan Cameron | ffc18af | 2009-10-12 19:18:09 +0100 | [diff] [blame] | 22 | #include <linux/sched.h> |
Jeff Mahoney | 4439c93 | 2009-10-12 17:10:34 -0400 | [diff] [blame] | 23 | #include <linux/wait.h> |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 24 | #include <linux/cdev.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Jonathan Cameron | 8e7d967 | 2011-08-30 12:32:45 +0100 | [diff] [blame] | 26 | #include <linux/anon_inodes.h> |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 27 | #include <linux/debugfs.h> |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 28 | #include <linux/iio/iio.h> |
Jonathan Cameron | df9c1c4 | 2011-08-12 17:56:03 +0100 | [diff] [blame] | 29 | #include "iio_core.h" |
Jonathan Cameron | 6aea1c3 | 2011-08-24 17:28:38 +0100 | [diff] [blame] | 30 | #include "iio_core_trigger.h" |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 31 | #include <linux/iio/sysfs.h> |
| 32 | #include <linux/iio/events.h> |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 33 | #include <linux/iio/buffer.h> |
Jonathan Cameron | 9dd1cb3 | 2011-08-30 12:41:15 +0100 | [diff] [blame] | 34 | |
Peter Meerwald | 99698b4 | 2012-08-26 13:43:00 +0100 | [diff] [blame] | 35 | /* IDA to assign each registered device a unique id */ |
Jonathan Cameron | b156cf7 | 2010-09-04 17:54:43 +0100 | [diff] [blame] | 36 | static DEFINE_IDA(iio_ida); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 37 | |
Jonathan Cameron | f625cb9 | 2011-08-30 12:32:48 +0100 | [diff] [blame] | 38 | static dev_t iio_devt; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 39 | |
| 40 | #define IIO_DEV_MAX 256 |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 41 | struct bus_type iio_bus_type = { |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 42 | .name = "iio", |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 43 | }; |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 44 | EXPORT_SYMBOL(iio_bus_type); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 45 | |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 46 | static struct dentry *iio_debugfs_dentry; |
| 47 | |
Jonathan Cameron | c6fc806 | 2011-09-02 17:14:34 +0100 | [diff] [blame] | 48 | static const char * const iio_direction[] = { |
| 49 | [0] = "in", |
| 50 | [1] = "out", |
| 51 | }; |
| 52 | |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 53 | static const char * const iio_chan_type_name_spec[] = { |
Jonathan Cameron | c6fc806 | 2011-09-02 17:14:34 +0100 | [diff] [blame] | 54 | [IIO_VOLTAGE] = "voltage", |
Michael Hennerich | faf290e | 2011-05-18 14:42:02 +0100 | [diff] [blame] | 55 | [IIO_CURRENT] = "current", |
| 56 | [IIO_POWER] = "power", |
Bryan Freed | 9bff02f | 2011-07-07 12:01:54 -0700 | [diff] [blame] | 57 | [IIO_ACCEL] = "accel", |
Jonathan Cameron | 41ea040 | 2011-10-05 15:27:59 +0100 | [diff] [blame] | 58 | [IIO_ANGL_VEL] = "anglvel", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 59 | [IIO_MAGN] = "magn", |
Bryan Freed | 9bff02f | 2011-07-07 12:01:54 -0700 | [diff] [blame] | 60 | [IIO_LIGHT] = "illuminance", |
| 61 | [IIO_INTENSITY] = "intensity", |
Bryan Freed | f09f2c8 | 2011-07-07 12:01:55 -0700 | [diff] [blame] | 62 | [IIO_PROXIMITY] = "proximity", |
Bryan Freed | 9bff02f | 2011-07-07 12:01:54 -0700 | [diff] [blame] | 63 | [IIO_TEMP] = "temp", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 64 | [IIO_INCLI] = "incli", |
| 65 | [IIO_ROT] = "rot", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 66 | [IIO_ANGL] = "angl", |
Bryan Freed | 9bff02f | 2011-07-07 12:01:54 -0700 | [diff] [blame] | 67 | [IIO_TIMESTAMP] = "timestamp", |
Jonathan Cameron | 66dbe70 | 2011-09-02 17:14:43 +0100 | [diff] [blame] | 68 | [IIO_CAPACITANCE] = "capacitance", |
Michael Hennerich | a6b1285 | 2012-04-27 10:58:34 +0200 | [diff] [blame] | 69 | [IIO_ALTVOLTAGE] = "altvoltage", |
Jon Brenner | 21cd1fa | 2012-05-16 10:46:42 -0500 | [diff] [blame] | 70 | [IIO_CCT] = "cct", |
Lars-Peter Clausen | c4f0c69 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 71 | [IIO_PRESSURE] = "pressure", |
Harald Geyer | ac216aa | 2013-12-01 15:08:00 +0000 | [diff] [blame] | 72 | [IIO_HUMIDITYRELATIVE] = "humidityrelative", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
Jonathan Cameron | 330c6c5 | 2011-09-02 17:14:39 +0100 | [diff] [blame] | 75 | static const char * const iio_modifier_names[] = { |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 76 | [IIO_MOD_X] = "x", |
| 77 | [IIO_MOD_Y] = "y", |
| 78 | [IIO_MOD_Z] = "z", |
Jonathan Cameron | 8f5879b | 2012-05-05 10:39:22 +0100 | [diff] [blame] | 79 | [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)", |
Jonathan Cameron | cf82cb8 | 2012-05-05 10:56:41 +0100 | [diff] [blame] | 80 | [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2", |
Jonathan Cameron | 330c6c5 | 2011-09-02 17:14:39 +0100 | [diff] [blame] | 81 | [IIO_MOD_LIGHT_BOTH] = "both", |
| 82 | [IIO_MOD_LIGHT_IR] = "ir", |
Jon Brenner | 21cd1fa | 2012-05-16 10:46:42 -0500 | [diff] [blame] | 83 | [IIO_MOD_LIGHT_CLEAR] = "clear", |
| 84 | [IIO_MOD_LIGHT_RED] = "red", |
| 85 | [IIO_MOD_LIGHT_GREEN] = "green", |
| 86 | [IIO_MOD_LIGHT_BLUE] = "blue", |
Srinivas Pandruvada | 5082f40 | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 87 | [IIO_MOD_QUATERNION] = "quaternion", |
Peter Meerwald | 638b43b | 2014-02-05 16:57:00 +0000 | [diff] [blame] | 88 | [IIO_MOD_TEMP_AMBIENT] = "ambient", |
| 89 | [IIO_MOD_TEMP_OBJECT] = "object", |
Reyad Attiyat | 11b8dda | 2014-07-17 19:18:00 +0100 | [diff] [blame] | 90 | [IIO_MOD_NORTH_MAGN] = "from_north_magnetic", |
| 91 | [IIO_MOD_NORTH_TRUE] = "from_north_true", |
| 92 | [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp", |
| 93 | [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | /* relies on pairs of these shared then separate */ |
| 97 | static const char * const iio_chan_info_postfix[] = { |
Jonathan Cameron | 75a973c | 2012-04-15 17:41:30 +0100 | [diff] [blame] | 98 | [IIO_CHAN_INFO_RAW] = "raw", |
| 99 | [IIO_CHAN_INFO_PROCESSED] = "input", |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 100 | [IIO_CHAN_INFO_SCALE] = "scale", |
| 101 | [IIO_CHAN_INFO_OFFSET] = "offset", |
| 102 | [IIO_CHAN_INFO_CALIBSCALE] = "calibscale", |
| 103 | [IIO_CHAN_INFO_CALIBBIAS] = "calibbias", |
| 104 | [IIO_CHAN_INFO_PEAK] = "peak_raw", |
| 105 | [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale", |
| 106 | [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw", |
| 107 | [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw", |
Jonathan Cameron | df94aba | 2011-11-27 11:39:12 +0000 | [diff] [blame] | 108 | [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY] |
| 109 | = "filter_low_pass_3db_frequency", |
Laxman Dewangan | ce85a1c | 2012-04-13 16:03:31 +0530 | [diff] [blame] | 110 | [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency", |
Michael Hennerich | a6b1285 | 2012-04-27 10:58:34 +0200 | [diff] [blame] | 111 | [IIO_CHAN_INFO_FREQUENCY] = "frequency", |
| 112 | [IIO_CHAN_INFO_PHASE] = "phase", |
Michael Hennerich | b65d621 | 2012-05-11 11:36:53 +0200 | [diff] [blame] | 113 | [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain", |
srinivas pandruvada | 7c9ab03 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 114 | [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis", |
Peter Meerwald | 899d90b | 2013-09-08 16:20:00 +0100 | [diff] [blame] | 115 | [IIO_CHAN_INFO_INT_TIME] = "integration_time", |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 116 | }; |
| 117 | |
Sachin Kamat | a7e57dc | 2013-10-29 11:39:00 +0000 | [diff] [blame] | 118 | /** |
| 119 | * iio_find_channel_from_si() - get channel from its scan index |
| 120 | * @indio_dev: device |
| 121 | * @si: scan index to match |
| 122 | */ |
Jonathan Cameron | 5fb21c8 | 2011-12-05 21:37:10 +0000 | [diff] [blame] | 123 | const struct iio_chan_spec |
| 124 | *iio_find_channel_from_si(struct iio_dev *indio_dev, int si) |
| 125 | { |
| 126 | int i; |
| 127 | |
| 128 | for (i = 0; i < indio_dev->num_channels; i++) |
| 129 | if (indio_dev->channels[i].scan_index == si) |
| 130 | return &indio_dev->channels[i]; |
| 131 | return NULL; |
| 132 | } |
| 133 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 134 | /* This turns up an awful lot */ |
| 135 | ssize_t iio_read_const_attr(struct device *dev, |
| 136 | struct device_attribute *attr, |
| 137 | char *buf) |
| 138 | { |
| 139 | return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string); |
| 140 | } |
| 141 | EXPORT_SYMBOL(iio_read_const_attr); |
| 142 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 143 | static int __init iio_init(void) |
| 144 | { |
| 145 | int ret; |
| 146 | |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 147 | /* Register sysfs bus */ |
| 148 | ret = bus_register(&iio_bus_type); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 149 | if (ret < 0) { |
Sachin Kamat | 3176dd5 | 2013-10-24 12:53:00 +0100 | [diff] [blame] | 150 | pr_err("could not register bus type\n"); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 151 | goto error_nothing; |
| 152 | } |
| 153 | |
Jonathan Cameron | 9aa1a16 | 2011-08-12 17:08:50 +0100 | [diff] [blame] | 154 | ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio"); |
| 155 | if (ret < 0) { |
Sachin Kamat | 3176dd5 | 2013-10-24 12:53:00 +0100 | [diff] [blame] | 156 | pr_err("failed to allocate char dev region\n"); |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 157 | goto error_unregister_bus_type; |
Jonathan Cameron | 9aa1a16 | 2011-08-12 17:08:50 +0100 | [diff] [blame] | 158 | } |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 159 | |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 160 | iio_debugfs_dentry = debugfs_create_dir("iio", NULL); |
| 161 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 162 | return 0; |
| 163 | |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 164 | error_unregister_bus_type: |
| 165 | bus_unregister(&iio_bus_type); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 166 | error_nothing: |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | static void __exit iio_exit(void) |
| 171 | { |
Jonathan Cameron | 9aa1a16 | 2011-08-12 17:08:50 +0100 | [diff] [blame] | 172 | if (iio_devt) |
| 173 | unregister_chrdev_region(iio_devt, IIO_DEV_MAX); |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 174 | bus_unregister(&iio_bus_type); |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 175 | debugfs_remove(iio_debugfs_dentry); |
| 176 | } |
| 177 | |
| 178 | #if defined(CONFIG_DEBUG_FS) |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 179 | static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf, |
| 180 | size_t count, loff_t *ppos) |
| 181 | { |
| 182 | struct iio_dev *indio_dev = file->private_data; |
| 183 | char buf[20]; |
| 184 | unsigned val = 0; |
| 185 | ssize_t len; |
| 186 | int ret; |
| 187 | |
| 188 | ret = indio_dev->info->debugfs_reg_access(indio_dev, |
| 189 | indio_dev->cached_reg_addr, |
| 190 | 0, &val); |
| 191 | if (ret) |
| 192 | dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__); |
| 193 | |
| 194 | len = snprintf(buf, sizeof(buf), "0x%X\n", val); |
| 195 | |
| 196 | return simple_read_from_buffer(userbuf, count, ppos, buf, len); |
| 197 | } |
| 198 | |
| 199 | static ssize_t iio_debugfs_write_reg(struct file *file, |
| 200 | const char __user *userbuf, size_t count, loff_t *ppos) |
| 201 | { |
| 202 | struct iio_dev *indio_dev = file->private_data; |
| 203 | unsigned reg, val; |
| 204 | char buf[80]; |
| 205 | int ret; |
| 206 | |
| 207 | count = min_t(size_t, count, (sizeof(buf)-1)); |
| 208 | if (copy_from_user(buf, userbuf, count)) |
| 209 | return -EFAULT; |
| 210 | |
| 211 | buf[count] = 0; |
| 212 | |
| 213 | ret = sscanf(buf, "%i %i", ®, &val); |
| 214 | |
| 215 | switch (ret) { |
| 216 | case 1: |
| 217 | indio_dev->cached_reg_addr = reg; |
| 218 | break; |
| 219 | case 2: |
| 220 | indio_dev->cached_reg_addr = reg; |
| 221 | ret = indio_dev->info->debugfs_reg_access(indio_dev, reg, |
| 222 | val, NULL); |
| 223 | if (ret) { |
| 224 | dev_err(indio_dev->dev.parent, "%s: write failed\n", |
| 225 | __func__); |
| 226 | return ret; |
| 227 | } |
| 228 | break; |
| 229 | default: |
| 230 | return -EINVAL; |
| 231 | } |
| 232 | |
| 233 | return count; |
| 234 | } |
| 235 | |
| 236 | static const struct file_operations iio_debugfs_reg_fops = { |
Axel Lin | 5a28c87 | 2012-05-03 09:50:51 +0800 | [diff] [blame] | 237 | .open = simple_open, |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 238 | .read = iio_debugfs_read_reg, |
| 239 | .write = iio_debugfs_write_reg, |
| 240 | }; |
| 241 | |
| 242 | static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) |
| 243 | { |
| 244 | debugfs_remove_recursive(indio_dev->debugfs_dentry); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 245 | } |
| 246 | |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 247 | static int iio_device_register_debugfs(struct iio_dev *indio_dev) |
| 248 | { |
| 249 | struct dentry *d; |
| 250 | |
| 251 | if (indio_dev->info->debugfs_reg_access == NULL) |
| 252 | return 0; |
| 253 | |
Axel Lin | abd5a2f | 2012-05-03 22:56:58 +0800 | [diff] [blame] | 254 | if (!iio_debugfs_dentry) |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 255 | return 0; |
| 256 | |
| 257 | indio_dev->debugfs_dentry = |
| 258 | debugfs_create_dir(dev_name(&indio_dev->dev), |
| 259 | iio_debugfs_dentry); |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 260 | if (indio_dev->debugfs_dentry == NULL) { |
| 261 | dev_warn(indio_dev->dev.parent, |
| 262 | "Failed to create debugfs directory\n"); |
| 263 | return -EFAULT; |
| 264 | } |
| 265 | |
| 266 | d = debugfs_create_file("direct_reg_access", 0644, |
| 267 | indio_dev->debugfs_dentry, |
| 268 | indio_dev, &iio_debugfs_reg_fops); |
| 269 | if (!d) { |
| 270 | iio_device_unregister_debugfs(indio_dev); |
| 271 | return -ENOMEM; |
| 272 | } |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | #else |
| 277 | static int iio_device_register_debugfs(struct iio_dev *indio_dev) |
| 278 | { |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) |
| 283 | { |
| 284 | } |
| 285 | #endif /* CONFIG_DEBUG_FS */ |
| 286 | |
Lars-Peter Clausen | 4fee7e1 | 2012-03-06 20:43:45 +0100 | [diff] [blame] | 287 | static ssize_t iio_read_channel_ext_info(struct device *dev, |
| 288 | struct device_attribute *attr, |
| 289 | char *buf) |
| 290 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 291 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Lars-Peter Clausen | 4fee7e1 | 2012-03-06 20:43:45 +0100 | [diff] [blame] | 292 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 293 | const struct iio_chan_spec_ext_info *ext_info; |
| 294 | |
| 295 | ext_info = &this_attr->c->ext_info[this_attr->address]; |
| 296 | |
Michael Hennerich | fc6d113 | 2012-04-27 10:58:36 +0200 | [diff] [blame] | 297 | return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf); |
Lars-Peter Clausen | 4fee7e1 | 2012-03-06 20:43:45 +0100 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | static ssize_t iio_write_channel_ext_info(struct device *dev, |
| 301 | struct device_attribute *attr, |
| 302 | const char *buf, |
| 303 | size_t len) |
| 304 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 305 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Lars-Peter Clausen | 4fee7e1 | 2012-03-06 20:43:45 +0100 | [diff] [blame] | 306 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 307 | const struct iio_chan_spec_ext_info *ext_info; |
| 308 | |
| 309 | ext_info = &this_attr->c->ext_info[this_attr->address]; |
| 310 | |
Michael Hennerich | fc6d113 | 2012-04-27 10:58:36 +0200 | [diff] [blame] | 311 | return ext_info->write(indio_dev, ext_info->private, |
| 312 | this_attr->c, buf, len); |
Lars-Peter Clausen | 4fee7e1 | 2012-03-06 20:43:45 +0100 | [diff] [blame] | 313 | } |
| 314 | |
Lars-Peter Clausen | 5212cc8 | 2012-06-04 11:36:11 +0200 | [diff] [blame] | 315 | ssize_t iio_enum_available_read(struct iio_dev *indio_dev, |
| 316 | uintptr_t priv, const struct iio_chan_spec *chan, char *buf) |
| 317 | { |
| 318 | const struct iio_enum *e = (const struct iio_enum *)priv; |
| 319 | unsigned int i; |
| 320 | size_t len = 0; |
| 321 | |
| 322 | if (!e->num_items) |
| 323 | return 0; |
| 324 | |
| 325 | for (i = 0; i < e->num_items; ++i) |
Lars-Peter Clausen | 74dcd43 | 2012-06-05 18:24:12 +0200 | [diff] [blame] | 326 | len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]); |
Lars-Peter Clausen | 5212cc8 | 2012-06-04 11:36:11 +0200 | [diff] [blame] | 327 | |
| 328 | /* replace last space with a newline */ |
| 329 | buf[len - 1] = '\n'; |
| 330 | |
| 331 | return len; |
| 332 | } |
| 333 | EXPORT_SYMBOL_GPL(iio_enum_available_read); |
| 334 | |
| 335 | ssize_t iio_enum_read(struct iio_dev *indio_dev, |
| 336 | uintptr_t priv, const struct iio_chan_spec *chan, char *buf) |
| 337 | { |
| 338 | const struct iio_enum *e = (const struct iio_enum *)priv; |
| 339 | int i; |
| 340 | |
| 341 | if (!e->get) |
| 342 | return -EINVAL; |
| 343 | |
| 344 | i = e->get(indio_dev, chan); |
| 345 | if (i < 0) |
| 346 | return i; |
| 347 | else if (i >= e->num_items) |
| 348 | return -EINVAL; |
| 349 | |
Kees Cook | 598db58 | 2014-03-13 16:46:00 +0000 | [diff] [blame] | 350 | return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]); |
Lars-Peter Clausen | 5212cc8 | 2012-06-04 11:36:11 +0200 | [diff] [blame] | 351 | } |
| 352 | EXPORT_SYMBOL_GPL(iio_enum_read); |
| 353 | |
| 354 | ssize_t iio_enum_write(struct iio_dev *indio_dev, |
| 355 | uintptr_t priv, const struct iio_chan_spec *chan, const char *buf, |
| 356 | size_t len) |
| 357 | { |
| 358 | const struct iio_enum *e = (const struct iio_enum *)priv; |
| 359 | unsigned int i; |
| 360 | int ret; |
| 361 | |
| 362 | if (!e->set) |
| 363 | return -EINVAL; |
| 364 | |
| 365 | for (i = 0; i < e->num_items; i++) { |
| 366 | if (sysfs_streq(buf, e->items[i])) |
| 367 | break; |
| 368 | } |
| 369 | |
| 370 | if (i == e->num_items) |
| 371 | return -EINVAL; |
| 372 | |
| 373 | ret = e->set(indio_dev, chan, i); |
| 374 | return ret ? ret : len; |
| 375 | } |
| 376 | EXPORT_SYMBOL_GPL(iio_enum_write); |
| 377 | |
Lars-Peter Clausen | 3661f3f | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 378 | /** |
| 379 | * iio_format_value() - Formats a IIO value into its string representation |
| 380 | * @buf: The buffer to which the formated value gets written |
| 381 | * @type: One of the IIO_VAL_... constants. This decides how the val and val2 |
| 382 | * parameters are formatted. |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 383 | * @vals: pointer to the values, exact meaning depends on the type parameter. |
Lars-Peter Clausen | 3661f3f | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 384 | */ |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 385 | ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 386 | { |
Lars-Peter Clausen | 7985e7c | 2012-09-14 16:21:00 +0100 | [diff] [blame] | 387 | unsigned long long tmp; |
Michael Hennerich | 67eedba | 2012-05-11 11:36:52 +0200 | [diff] [blame] | 388 | bool scale_db = false; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 389 | |
Lars-Peter Clausen | 3661f3f | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 390 | switch (type) { |
Michael Hennerich | 67eedba | 2012-05-11 11:36:52 +0200 | [diff] [blame] | 391 | case IIO_VAL_INT: |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 392 | return sprintf(buf, "%d\n", vals[0]); |
Michael Hennerich | 67eedba | 2012-05-11 11:36:52 +0200 | [diff] [blame] | 393 | case IIO_VAL_INT_PLUS_MICRO_DB: |
| 394 | scale_db = true; |
| 395 | case IIO_VAL_INT_PLUS_MICRO: |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 396 | if (vals[1] < 0) |
| 397 | return sprintf(buf, "-%ld.%06u%s\n", abs(vals[0]), |
| 398 | -vals[1], |
Michael Hennerich | 67eedba | 2012-05-11 11:36:52 +0200 | [diff] [blame] | 399 | scale_db ? " dB" : ""); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 400 | else |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 401 | return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1], |
Michael Hennerich | 67eedba | 2012-05-11 11:36:52 +0200 | [diff] [blame] | 402 | scale_db ? " dB" : ""); |
| 403 | case IIO_VAL_INT_PLUS_NANO: |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 404 | if (vals[1] < 0) |
| 405 | return sprintf(buf, "-%ld.%09u\n", abs(vals[0]), |
| 406 | -vals[1]); |
Michael Hennerich | 71646e2 | 2011-06-27 13:07:07 +0100 | [diff] [blame] | 407 | else |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 408 | return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); |
Lars-Peter Clausen | 7985e7c | 2012-09-14 16:21:00 +0100 | [diff] [blame] | 409 | case IIO_VAL_FRACTIONAL: |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 410 | tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]); |
| 411 | vals[1] = do_div(tmp, 1000000000LL); |
| 412 | vals[0] = tmp; |
| 413 | return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); |
Lars-Peter Clausen | 103d9fb | 2012-10-16 17:29:00 +0100 | [diff] [blame] | 414 | case IIO_VAL_FRACTIONAL_LOG2: |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 415 | tmp = (s64)vals[0] * 1000000000LL >> vals[1]; |
| 416 | vals[1] = do_div(tmp, 1000000000LL); |
| 417 | vals[0] = tmp; |
| 418 | return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); |
| 419 | case IIO_VAL_INT_MULTIPLE: |
| 420 | { |
| 421 | int i; |
| 422 | int len = 0; |
| 423 | |
| 424 | for (i = 0; i < size; ++i) |
| 425 | len += snprintf(&buf[len], PAGE_SIZE - len, "%d ", |
| 426 | vals[i]); |
| 427 | len += snprintf(&buf[len], PAGE_SIZE - len, "\n"); |
| 428 | return len; |
| 429 | } |
Michael Hennerich | 67eedba | 2012-05-11 11:36:52 +0200 | [diff] [blame] | 430 | default: |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 431 | return 0; |
Michael Hennerich | 67eedba | 2012-05-11 11:36:52 +0200 | [diff] [blame] | 432 | } |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 433 | } |
| 434 | |
Lars-Peter Clausen | 3661f3f | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 435 | static ssize_t iio_read_channel_info(struct device *dev, |
| 436 | struct device_attribute *attr, |
| 437 | char *buf) |
| 438 | { |
| 439 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 440 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 441 | int vals[INDIO_MAX_RAW_ELEMENTS]; |
| 442 | int ret; |
| 443 | int val_len = 2; |
| 444 | |
| 445 | if (indio_dev->info->read_raw_multi) |
| 446 | ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c, |
| 447 | INDIO_MAX_RAW_ELEMENTS, |
| 448 | vals, &val_len, |
| 449 | this_attr->address); |
| 450 | else |
| 451 | ret = indio_dev->info->read_raw(indio_dev, this_attr->c, |
| 452 | &vals[0], &vals[1], this_attr->address); |
Lars-Peter Clausen | 3661f3f | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 453 | |
| 454 | if (ret < 0) |
| 455 | return ret; |
| 456 | |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 457 | return iio_format_value(buf, ret, val_len, vals); |
Lars-Peter Clausen | 3661f3f | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 458 | } |
| 459 | |
Lars-Peter Clausen | 6807d72 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 460 | /** |
| 461 | * iio_str_to_fixpoint() - Parse a fixed-point number from a string |
| 462 | * @str: The string to parse |
| 463 | * @fract_mult: Multiplier for the first decimal place, should be a power of 10 |
| 464 | * @integer: The integer part of the number |
| 465 | * @fract: The fractional part of the number |
| 466 | * |
| 467 | * Returns 0 on success, or a negative error code if the string could not be |
| 468 | * parsed. |
| 469 | */ |
| 470 | int iio_str_to_fixpoint(const char *str, int fract_mult, |
| 471 | int *integer, int *fract) |
| 472 | { |
| 473 | int i = 0, f = 0; |
| 474 | bool integer_part = true, negative = false; |
| 475 | |
| 476 | if (str[0] == '-') { |
| 477 | negative = true; |
| 478 | str++; |
| 479 | } else if (str[0] == '+') { |
| 480 | str++; |
| 481 | } |
| 482 | |
| 483 | while (*str) { |
| 484 | if ('0' <= *str && *str <= '9') { |
| 485 | if (integer_part) { |
| 486 | i = i * 10 + *str - '0'; |
| 487 | } else { |
| 488 | f += fract_mult * (*str - '0'); |
| 489 | fract_mult /= 10; |
| 490 | } |
| 491 | } else if (*str == '\n') { |
| 492 | if (*(str + 1) == '\0') |
| 493 | break; |
| 494 | else |
| 495 | return -EINVAL; |
| 496 | } else if (*str == '.' && integer_part) { |
| 497 | integer_part = false; |
| 498 | } else { |
| 499 | return -EINVAL; |
| 500 | } |
| 501 | str++; |
| 502 | } |
| 503 | |
| 504 | if (negative) { |
| 505 | if (i) |
| 506 | i = -i; |
| 507 | else |
| 508 | f = -f; |
| 509 | } |
| 510 | |
| 511 | *integer = i; |
| 512 | *fract = f; |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | EXPORT_SYMBOL_GPL(iio_str_to_fixpoint); |
| 517 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 518 | static ssize_t iio_write_channel_info(struct device *dev, |
| 519 | struct device_attribute *attr, |
| 520 | const char *buf, |
| 521 | size_t len) |
| 522 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 523 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 524 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
Lars-Peter Clausen | 6807d72 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 525 | int ret, fract_mult = 100000; |
| 526 | int integer, fract; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 527 | |
| 528 | /* Assumes decimal - precision based on number of digits */ |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 529 | if (!indio_dev->info->write_raw) |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 530 | return -EINVAL; |
Michael Hennerich | 5c04af0 | 2011-06-27 13:07:10 +0100 | [diff] [blame] | 531 | |
| 532 | if (indio_dev->info->write_raw_get_fmt) |
| 533 | switch (indio_dev->info->write_raw_get_fmt(indio_dev, |
| 534 | this_attr->c, this_attr->address)) { |
| 535 | case IIO_VAL_INT_PLUS_MICRO: |
| 536 | fract_mult = 100000; |
| 537 | break; |
| 538 | case IIO_VAL_INT_PLUS_NANO: |
| 539 | fract_mult = 100000000; |
| 540 | break; |
| 541 | default: |
| 542 | return -EINVAL; |
| 543 | } |
| 544 | |
Lars-Peter Clausen | 6807d72 | 2012-11-20 13:36:00 +0000 | [diff] [blame] | 545 | ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract); |
| 546 | if (ret) |
| 547 | return ret; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 548 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 549 | ret = indio_dev->info->write_raw(indio_dev, this_attr->c, |
Michael Hennerich | 5c04af0 | 2011-06-27 13:07:10 +0100 | [diff] [blame] | 550 | integer, fract, this_attr->address); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 551 | if (ret) |
| 552 | return ret; |
| 553 | |
| 554 | return len; |
| 555 | } |
| 556 | |
Jonathan Cameron | df9c1c4 | 2011-08-12 17:56:03 +0100 | [diff] [blame] | 557 | static |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 558 | int __iio_device_attr_init(struct device_attribute *dev_attr, |
| 559 | const char *postfix, |
| 560 | struct iio_chan_spec const *chan, |
| 561 | ssize_t (*readfunc)(struct device *dev, |
| 562 | struct device_attribute *attr, |
| 563 | char *buf), |
| 564 | ssize_t (*writefunc)(struct device *dev, |
| 565 | struct device_attribute *attr, |
| 566 | const char *buf, |
| 567 | size_t len), |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 568 | enum iio_shared_by shared_by) |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 569 | { |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 570 | int ret = 0; |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 571 | char *name = NULL; |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 572 | char *full_postfix; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 573 | sysfs_attr_init(&dev_attr->attr); |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 574 | |
| 575 | /* Build up postfix of <extend_name>_<modifier>_postfix */ |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 576 | if (chan->modified && (shared_by == IIO_SEPARATE)) { |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 577 | if (chan->extend_name) |
| 578 | full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", |
| 579 | iio_modifier_names[chan |
| 580 | ->channel2], |
| 581 | chan->extend_name, |
| 582 | postfix); |
| 583 | else |
| 584 | full_postfix = kasprintf(GFP_KERNEL, "%s_%s", |
| 585 | iio_modifier_names[chan |
| 586 | ->channel2], |
| 587 | postfix); |
| 588 | } else { |
Lars-Peter Clausen | 77bfa8b | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 589 | if (chan->extend_name == NULL || shared_by != IIO_SEPARATE) |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 590 | full_postfix = kstrdup(postfix, GFP_KERNEL); |
| 591 | else |
| 592 | full_postfix = kasprintf(GFP_KERNEL, |
| 593 | "%s_%s", |
| 594 | chan->extend_name, |
| 595 | postfix); |
| 596 | } |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 597 | if (full_postfix == NULL) |
| 598 | return -ENOMEM; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 599 | |
Justin P. Mattock | 4abf6f8 | 2012-02-29 22:00:38 -0800 | [diff] [blame] | 600 | if (chan->differential) { /* Differential can not have modifier */ |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 601 | switch (shared_by) { |
Jonathan Cameron | c006ec8 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 602 | case IIO_SHARED_BY_ALL: |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 603 | name = kasprintf(GFP_KERNEL, "%s", full_postfix); |
Jonathan Cameron | c006ec8 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 604 | break; |
| 605 | case IIO_SHARED_BY_DIR: |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 606 | name = kasprintf(GFP_KERNEL, "%s_%s", |
Jonathan Cameron | c006ec8 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 607 | iio_direction[chan->output], |
| 608 | full_postfix); |
| 609 | break; |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 610 | case IIO_SHARED_BY_TYPE: |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 611 | name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s", |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 612 | iio_direction[chan->output], |
| 613 | iio_chan_type_name_spec[chan->type], |
| 614 | iio_chan_type_name_spec[chan->type], |
| 615 | full_postfix); |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 616 | break; |
| 617 | case IIO_SEPARATE: |
| 618 | if (!chan->indexed) { |
| 619 | WARN_ON("Differential channels must be indexed\n"); |
| 620 | ret = -EINVAL; |
| 621 | goto error_free_full_postfix; |
| 622 | } |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 623 | name = kasprintf(GFP_KERNEL, |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 624 | "%s_%s%d-%s%d_%s", |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 625 | iio_direction[chan->output], |
| 626 | iio_chan_type_name_spec[chan->type], |
| 627 | chan->channel, |
| 628 | iio_chan_type_name_spec[chan->type], |
| 629 | chan->channel2, |
| 630 | full_postfix); |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 631 | break; |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 632 | } |
| 633 | } else { /* Single ended */ |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 634 | switch (shared_by) { |
Jonathan Cameron | c006ec8 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 635 | case IIO_SHARED_BY_ALL: |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 636 | name = kasprintf(GFP_KERNEL, "%s", full_postfix); |
Jonathan Cameron | c006ec8 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 637 | break; |
| 638 | case IIO_SHARED_BY_DIR: |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 639 | name = kasprintf(GFP_KERNEL, "%s_%s", |
Jonathan Cameron | c006ec8 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 640 | iio_direction[chan->output], |
| 641 | full_postfix); |
| 642 | break; |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 643 | case IIO_SHARED_BY_TYPE: |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 644 | name = kasprintf(GFP_KERNEL, "%s_%s_%s", |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 645 | iio_direction[chan->output], |
| 646 | iio_chan_type_name_spec[chan->type], |
| 647 | full_postfix); |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 648 | break; |
| 649 | |
| 650 | case IIO_SEPARATE: |
| 651 | if (chan->indexed) |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 652 | name = kasprintf(GFP_KERNEL, "%s_%s%d_%s", |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 653 | iio_direction[chan->output], |
| 654 | iio_chan_type_name_spec[chan->type], |
| 655 | chan->channel, |
| 656 | full_postfix); |
| 657 | else |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 658 | name = kasprintf(GFP_KERNEL, "%s_%s_%s", |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 659 | iio_direction[chan->output], |
| 660 | iio_chan_type_name_spec[chan->type], |
| 661 | full_postfix); |
| 662 | break; |
| 663 | } |
Jonathan Cameron | ade7ef7 | 2011-09-02 17:14:45 +0100 | [diff] [blame] | 664 | } |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 665 | if (name == NULL) { |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 666 | ret = -ENOMEM; |
| 667 | goto error_free_full_postfix; |
| 668 | } |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 669 | dev_attr->attr.name = name; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 670 | |
| 671 | if (readfunc) { |
| 672 | dev_attr->attr.mode |= S_IRUGO; |
| 673 | dev_attr->show = readfunc; |
| 674 | } |
| 675 | |
| 676 | if (writefunc) { |
| 677 | dev_attr->attr.mode |= S_IWUSR; |
| 678 | dev_attr->store = writefunc; |
| 679 | } |
Lars-Peter Clausen | 7bbcf7e | 2014-02-14 14:19:00 +0000 | [diff] [blame] | 680 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 681 | error_free_full_postfix: |
| 682 | kfree(full_postfix); |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 683 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 684 | return ret; |
| 685 | } |
| 686 | |
Jonathan Cameron | df9c1c4 | 2011-08-12 17:56:03 +0100 | [diff] [blame] | 687 | static void __iio_device_attr_deinit(struct device_attribute *dev_attr) |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 688 | { |
| 689 | kfree(dev_attr->attr.name); |
| 690 | } |
| 691 | |
| 692 | int __iio_add_chan_devattr(const char *postfix, |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 693 | struct iio_chan_spec const *chan, |
| 694 | ssize_t (*readfunc)(struct device *dev, |
| 695 | struct device_attribute *attr, |
| 696 | char *buf), |
| 697 | ssize_t (*writefunc)(struct device *dev, |
| 698 | struct device_attribute *attr, |
| 699 | const char *buf, |
| 700 | size_t len), |
Jonathan Cameron | e614a54 | 2011-09-02 17:14:41 +0100 | [diff] [blame] | 701 | u64 mask, |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 702 | enum iio_shared_by shared_by, |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 703 | struct device *dev, |
| 704 | struct list_head *attr_list) |
| 705 | { |
| 706 | int ret; |
| 707 | struct iio_dev_attr *iio_attr, *t; |
| 708 | |
Sachin Kamat | 670c110 | 2013-10-24 12:53:00 +0100 | [diff] [blame] | 709 | iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL); |
Hartmut Knaack | 92825ff | 2014-02-16 11:53:00 +0000 | [diff] [blame] | 710 | if (iio_attr == NULL) |
| 711 | return -ENOMEM; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 712 | ret = __iio_device_attr_init(&iio_attr->dev_attr, |
| 713 | postfix, chan, |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 714 | readfunc, writefunc, shared_by); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 715 | if (ret) |
| 716 | goto error_iio_dev_attr_free; |
| 717 | iio_attr->c = chan; |
| 718 | iio_attr->address = mask; |
| 719 | list_for_each_entry(t, attr_list, l) |
| 720 | if (strcmp(t->dev_attr.attr.name, |
| 721 | iio_attr->dev_attr.attr.name) == 0) { |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 722 | if (shared_by == IIO_SEPARATE) |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 723 | dev_err(dev, "tried to double register : %s\n", |
| 724 | t->dev_attr.attr.name); |
| 725 | ret = -EBUSY; |
| 726 | goto error_device_attr_deinit; |
| 727 | } |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 728 | list_add(&iio_attr->l, attr_list); |
| 729 | |
| 730 | return 0; |
| 731 | |
| 732 | error_device_attr_deinit: |
| 733 | __iio_device_attr_deinit(&iio_attr->dev_attr); |
| 734 | error_iio_dev_attr_free: |
| 735 | kfree(iio_attr); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 736 | return ret; |
| 737 | } |
| 738 | |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 739 | static int iio_device_add_info_mask_type(struct iio_dev *indio_dev, |
| 740 | struct iio_chan_spec const *chan, |
| 741 | enum iio_shared_by shared_by, |
| 742 | const long *infomask) |
| 743 | { |
| 744 | int i, ret, attrcount = 0; |
| 745 | |
| 746 | for_each_set_bit(i, infomask, sizeof(infomask)*8) { |
Jonathan Cameron | ef4b485 | 2014-01-03 22:24:00 +0000 | [diff] [blame] | 747 | if (i >= ARRAY_SIZE(iio_chan_info_postfix)) |
| 748 | return -EINVAL; |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 749 | ret = __iio_add_chan_devattr(iio_chan_info_postfix[i], |
| 750 | chan, |
| 751 | &iio_read_channel_info, |
| 752 | &iio_write_channel_info, |
| 753 | i, |
| 754 | shared_by, |
| 755 | &indio_dev->dev, |
| 756 | &indio_dev->channel_attr_list); |
| 757 | if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) |
| 758 | continue; |
| 759 | else if (ret < 0) |
| 760 | return ret; |
| 761 | attrcount++; |
| 762 | } |
| 763 | |
| 764 | return attrcount; |
| 765 | } |
| 766 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 767 | static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev, |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 768 | struct iio_chan_spec const *chan) |
| 769 | { |
Jonathan Cameron | 5ccb3ad | 2012-04-15 17:41:16 +0100 | [diff] [blame] | 770 | int ret, attrcount = 0; |
Lars-Peter Clausen | 5f420b4 | 2012-02-21 18:38:12 +0100 | [diff] [blame] | 771 | const struct iio_chan_spec_ext_info *ext_info; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 772 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 773 | if (chan->channel < 0) |
| 774 | return 0; |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 775 | ret = iio_device_add_info_mask_type(indio_dev, chan, |
| 776 | IIO_SEPARATE, |
| 777 | &chan->info_mask_separate); |
| 778 | if (ret < 0) |
| 779 | return ret; |
| 780 | attrcount += ret; |
| 781 | |
| 782 | ret = iio_device_add_info_mask_type(indio_dev, chan, |
| 783 | IIO_SHARED_BY_TYPE, |
| 784 | &chan->info_mask_shared_by_type); |
| 785 | if (ret < 0) |
| 786 | return ret; |
| 787 | attrcount += ret; |
Lars-Peter Clausen | 5f420b4 | 2012-02-21 18:38:12 +0100 | [diff] [blame] | 788 | |
Jonathan Cameron | c006ec8 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 789 | ret = iio_device_add_info_mask_type(indio_dev, chan, |
| 790 | IIO_SHARED_BY_DIR, |
| 791 | &chan->info_mask_shared_by_dir); |
| 792 | if (ret < 0) |
| 793 | return ret; |
| 794 | attrcount += ret; |
| 795 | |
| 796 | ret = iio_device_add_info_mask_type(indio_dev, chan, |
| 797 | IIO_SHARED_BY_ALL, |
| 798 | &chan->info_mask_shared_by_all); |
| 799 | if (ret < 0) |
| 800 | return ret; |
| 801 | attrcount += ret; |
| 802 | |
Lars-Peter Clausen | 5f420b4 | 2012-02-21 18:38:12 +0100 | [diff] [blame] | 803 | if (chan->ext_info) { |
| 804 | unsigned int i = 0; |
| 805 | for (ext_info = chan->ext_info; ext_info->name; ext_info++) { |
| 806 | ret = __iio_add_chan_devattr(ext_info->name, |
| 807 | chan, |
| 808 | ext_info->read ? |
| 809 | &iio_read_channel_ext_info : NULL, |
| 810 | ext_info->write ? |
| 811 | &iio_write_channel_ext_info : NULL, |
| 812 | i, |
| 813 | ext_info->shared, |
| 814 | &indio_dev->dev, |
| 815 | &indio_dev->channel_attr_list); |
| 816 | i++; |
| 817 | if (ret == -EBUSY && ext_info->shared) |
| 818 | continue; |
| 819 | |
| 820 | if (ret) |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 821 | return ret; |
Lars-Peter Clausen | 5f420b4 | 2012-02-21 18:38:12 +0100 | [diff] [blame] | 822 | |
| 823 | attrcount++; |
| 824 | } |
| 825 | } |
| 826 | |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 827 | return attrcount; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 828 | } |
| 829 | |
Lars-Peter Clausen | 84088eb | 2013-10-07 12:50:00 +0100 | [diff] [blame] | 830 | /** |
| 831 | * iio_free_chan_devattr_list() - Free a list of IIO device attributes |
| 832 | * @attr_list: List of IIO device attributes |
| 833 | * |
| 834 | * This function frees the memory allocated for each of the IIO device |
| 835 | * attributes in the list. Note: if you want to reuse the list after calling |
| 836 | * this function you have to reinitialize it using INIT_LIST_HEAD(). |
| 837 | */ |
| 838 | void iio_free_chan_devattr_list(struct list_head *attr_list) |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 839 | { |
Lars-Peter Clausen | 84088eb | 2013-10-07 12:50:00 +0100 | [diff] [blame] | 840 | struct iio_dev_attr *p, *n; |
| 841 | |
| 842 | list_for_each_entry_safe(p, n, attr_list, l) { |
| 843 | kfree(p->dev_attr.attr.name); |
| 844 | kfree(p); |
| 845 | } |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 846 | } |
| 847 | |
Jonathan Cameron | 1b73288 | 2011-05-18 14:41:43 +0100 | [diff] [blame] | 848 | static ssize_t iio_show_dev_name(struct device *dev, |
| 849 | struct device_attribute *attr, |
| 850 | char *buf) |
| 851 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 852 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Kees Cook | 598db58 | 2014-03-13 16:46:00 +0000 | [diff] [blame] | 853 | return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name); |
Jonathan Cameron | 1b73288 | 2011-05-18 14:41:43 +0100 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL); |
| 857 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 858 | static int iio_device_register_sysfs(struct iio_dev *indio_dev) |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 859 | { |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 860 | int i, ret = 0, attrcount, attrn, attrcount_orig = 0; |
Lars-Peter Clausen | 84088eb | 2013-10-07 12:50:00 +0100 | [diff] [blame] | 861 | struct iio_dev_attr *p; |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 862 | struct attribute **attr; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 863 | |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 864 | /* First count elements in any existing group */ |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 865 | if (indio_dev->info->attrs) { |
| 866 | attr = indio_dev->info->attrs->attrs; |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 867 | while (*attr++ != NULL) |
| 868 | attrcount_orig++; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 869 | } |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 870 | attrcount = attrcount_orig; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 871 | /* |
| 872 | * New channel registration method - relies on the fact a group does |
Peter Meerwald | d25b380 | 2012-08-26 13:43:00 +0100 | [diff] [blame] | 873 | * not need to be initialized if its name is NULL. |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 874 | */ |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 875 | if (indio_dev->channels) |
| 876 | for (i = 0; i < indio_dev->num_channels; i++) { |
| 877 | ret = iio_device_add_channel_sysfs(indio_dev, |
| 878 | &indio_dev |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 879 | ->channels[i]); |
| 880 | if (ret < 0) |
| 881 | goto error_clear_attrs; |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 882 | attrcount += ret; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 883 | } |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 884 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 885 | if (indio_dev->name) |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 886 | attrcount++; |
| 887 | |
Thomas Meyer | d83fb18 | 2011-11-29 22:08:00 +0100 | [diff] [blame] | 888 | indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1, |
| 889 | sizeof(indio_dev->chan_attr_group.attrs[0]), |
| 890 | GFP_KERNEL); |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 891 | if (indio_dev->chan_attr_group.attrs == NULL) { |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 892 | ret = -ENOMEM; |
| 893 | goto error_clear_attrs; |
Jonathan Cameron | 1b73288 | 2011-05-18 14:41:43 +0100 | [diff] [blame] | 894 | } |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 895 | /* Copy across original attributes */ |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 896 | if (indio_dev->info->attrs) |
| 897 | memcpy(indio_dev->chan_attr_group.attrs, |
| 898 | indio_dev->info->attrs->attrs, |
| 899 | sizeof(indio_dev->chan_attr_group.attrs[0]) |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 900 | *attrcount_orig); |
| 901 | attrn = attrcount_orig; |
| 902 | /* Add all elements from the list. */ |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 903 | list_for_each_entry(p, &indio_dev->channel_attr_list, l) |
| 904 | indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr; |
| 905 | if (indio_dev->name) |
| 906 | indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr; |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 907 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 908 | indio_dev->groups[indio_dev->groupcounter++] = |
| 909 | &indio_dev->chan_attr_group; |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 910 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 911 | return 0; |
Jonathan Cameron | 1b73288 | 2011-05-18 14:41:43 +0100 | [diff] [blame] | 912 | |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 913 | error_clear_attrs: |
Lars-Peter Clausen | 84088eb | 2013-10-07 12:50:00 +0100 | [diff] [blame] | 914 | iio_free_chan_devattr_list(&indio_dev->channel_attr_list); |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 915 | |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 916 | return ret; |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 917 | } |
| 918 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 919 | static void iio_device_unregister_sysfs(struct iio_dev *indio_dev) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 920 | { |
Jonathan Cameron | 1d89271 | 2011-05-18 14:40:51 +0100 | [diff] [blame] | 921 | |
Lars-Peter Clausen | 84088eb | 2013-10-07 12:50:00 +0100 | [diff] [blame] | 922 | iio_free_chan_devattr_list(&indio_dev->channel_attr_list); |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 923 | kfree(indio_dev->chan_attr_group.attrs); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 924 | } |
| 925 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 926 | static void iio_dev_release(struct device *device) |
| 927 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 928 | struct iio_dev *indio_dev = dev_to_iio_dev(device); |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 929 | if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) |
| 930 | iio_device_unregister_trigger_consumer(indio_dev); |
| 931 | iio_device_unregister_eventset(indio_dev); |
| 932 | iio_device_unregister_sysfs(indio_dev); |
Lars-Peter Clausen | e407fd6 | 2012-06-04 10:41:42 +0200 | [diff] [blame] | 933 | |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 934 | iio_buffer_put(indio_dev->buffer); |
| 935 | |
Lars-Peter Clausen | e407fd6 | 2012-06-04 10:41:42 +0200 | [diff] [blame] | 936 | ida_simple_remove(&iio_ida, indio_dev->id); |
| 937 | kfree(indio_dev); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 938 | } |
| 939 | |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 940 | struct device_type iio_device_type = { |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 941 | .name = "iio_device", |
| 942 | .release = iio_dev_release, |
| 943 | }; |
| 944 | |
Sachin Kamat | a7e57dc | 2013-10-29 11:39:00 +0000 | [diff] [blame] | 945 | /** |
| 946 | * iio_device_alloc() - allocate an iio_dev from a driver |
| 947 | * @sizeof_priv: Space to allocate for private structure. |
| 948 | **/ |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 949 | struct iio_dev *iio_device_alloc(int sizeof_priv) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 950 | { |
Jonathan Cameron | 6f7c8ee | 2011-04-15 18:55:56 +0100 | [diff] [blame] | 951 | struct iio_dev *dev; |
| 952 | size_t alloc_size; |
| 953 | |
| 954 | alloc_size = sizeof(struct iio_dev); |
| 955 | if (sizeof_priv) { |
| 956 | alloc_size = ALIGN(alloc_size, IIO_ALIGN); |
| 957 | alloc_size += sizeof_priv; |
| 958 | } |
| 959 | /* ensure 32-byte alignment of whole construct ? */ |
| 960 | alloc_size += IIO_ALIGN - 1; |
| 961 | |
| 962 | dev = kzalloc(alloc_size, GFP_KERNEL); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 963 | |
| 964 | if (dev) { |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 965 | dev->dev.groups = dev->groups; |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 966 | dev->dev.type = &iio_device_type; |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 967 | dev->dev.bus = &iio_bus_type; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 968 | device_initialize(&dev->dev); |
| 969 | dev_set_drvdata(&dev->dev, (void *)dev); |
| 970 | mutex_init(&dev->mlock); |
Jonathan Cameron | ac917a8 | 2012-02-15 19:48:00 +0000 | [diff] [blame] | 971 | mutex_init(&dev->info_exist_lock); |
Lars-Peter Clausen | e407fd6 | 2012-06-04 10:41:42 +0200 | [diff] [blame] | 972 | INIT_LIST_HEAD(&dev->channel_attr_list); |
Jonathan Cameron | a9e39f9 | 2011-09-14 13:01:25 +0100 | [diff] [blame] | 973 | |
| 974 | dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL); |
| 975 | if (dev->id < 0) { |
| 976 | /* cannot use a dev_err as the name isn't available */ |
Sachin Kamat | 3176dd5 | 2013-10-24 12:53:00 +0100 | [diff] [blame] | 977 | pr_err("failed to get device id\n"); |
Jonathan Cameron | a9e39f9 | 2011-09-14 13:01:25 +0100 | [diff] [blame] | 978 | kfree(dev); |
| 979 | return NULL; |
| 980 | } |
| 981 | dev_set_name(&dev->dev, "iio:device%d", dev->id); |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 982 | INIT_LIST_HEAD(&dev->buffer_list); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | return dev; |
| 986 | } |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 987 | EXPORT_SYMBOL(iio_device_alloc); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 988 | |
Sachin Kamat | a7e57dc | 2013-10-29 11:39:00 +0000 | [diff] [blame] | 989 | /** |
| 990 | * iio_device_free() - free an iio_dev from a driver |
| 991 | * @dev: the iio_dev associated with the device |
| 992 | **/ |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 993 | void iio_device_free(struct iio_dev *dev) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 994 | { |
Lars-Peter Clausen | e407fd6 | 2012-06-04 10:41:42 +0200 | [diff] [blame] | 995 | if (dev) |
| 996 | put_device(&dev->dev); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 997 | } |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 998 | EXPORT_SYMBOL(iio_device_free); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 999 | |
Grygorii Strashko | 9dabaf5 | 2013-07-18 11:19:00 +0100 | [diff] [blame] | 1000 | static void devm_iio_device_release(struct device *dev, void *res) |
| 1001 | { |
| 1002 | iio_device_free(*(struct iio_dev **)res); |
| 1003 | } |
| 1004 | |
| 1005 | static int devm_iio_device_match(struct device *dev, void *res, void *data) |
| 1006 | { |
| 1007 | struct iio_dev **r = res; |
| 1008 | if (!r || !*r) { |
| 1009 | WARN_ON(!r || !*r); |
| 1010 | return 0; |
| 1011 | } |
| 1012 | return *r == data; |
| 1013 | } |
| 1014 | |
Sachin Kamat | a7e57dc | 2013-10-29 11:39:00 +0000 | [diff] [blame] | 1015 | /** |
| 1016 | * devm_iio_device_alloc - Resource-managed iio_device_alloc() |
| 1017 | * @dev: Device to allocate iio_dev for |
| 1018 | * @sizeof_priv: Space to allocate for private structure. |
| 1019 | * |
| 1020 | * Managed iio_device_alloc. iio_dev allocated with this function is |
| 1021 | * automatically freed on driver detach. |
| 1022 | * |
| 1023 | * If an iio_dev allocated with this function needs to be freed separately, |
| 1024 | * devm_iio_device_free() must be used. |
| 1025 | * |
| 1026 | * RETURNS: |
| 1027 | * Pointer to allocated iio_dev on success, NULL on failure. |
| 1028 | */ |
Grygorii Strashko | 9dabaf5 | 2013-07-18 11:19:00 +0100 | [diff] [blame] | 1029 | struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv) |
| 1030 | { |
| 1031 | struct iio_dev **ptr, *iio_dev; |
| 1032 | |
| 1033 | ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr), |
| 1034 | GFP_KERNEL); |
| 1035 | if (!ptr) |
| 1036 | return NULL; |
| 1037 | |
| 1038 | /* use raw alloc_dr for kmalloc caller tracing */ |
| 1039 | iio_dev = iio_device_alloc(sizeof_priv); |
| 1040 | if (iio_dev) { |
| 1041 | *ptr = iio_dev; |
| 1042 | devres_add(dev, ptr); |
| 1043 | } else { |
| 1044 | devres_free(ptr); |
| 1045 | } |
| 1046 | |
| 1047 | return iio_dev; |
| 1048 | } |
| 1049 | EXPORT_SYMBOL_GPL(devm_iio_device_alloc); |
| 1050 | |
Sachin Kamat | a7e57dc | 2013-10-29 11:39:00 +0000 | [diff] [blame] | 1051 | /** |
| 1052 | * devm_iio_device_free - Resource-managed iio_device_free() |
| 1053 | * @dev: Device this iio_dev belongs to |
| 1054 | * @iio_dev: the iio_dev associated with the device |
| 1055 | * |
| 1056 | * Free iio_dev allocated with devm_iio_device_alloc(). |
| 1057 | */ |
Grygorii Strashko | 9dabaf5 | 2013-07-18 11:19:00 +0100 | [diff] [blame] | 1058 | void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev) |
| 1059 | { |
| 1060 | int rc; |
| 1061 | |
| 1062 | rc = devres_release(dev, devm_iio_device_release, |
| 1063 | devm_iio_device_match, iio_dev); |
| 1064 | WARN_ON(rc); |
| 1065 | } |
| 1066 | EXPORT_SYMBOL_GPL(devm_iio_device_free); |
| 1067 | |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1068 | /** |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 1069 | * iio_chrdev_open() - chrdev file open for buffer access and ioctls |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1070 | **/ |
| 1071 | static int iio_chrdev_open(struct inode *inode, struct file *filp) |
| 1072 | { |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1073 | struct iio_dev *indio_dev = container_of(inode->i_cdev, |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1074 | struct iio_dev, chrdev); |
Lars-Peter Clausen | bb01443 | 2011-12-19 15:23:45 +0100 | [diff] [blame] | 1075 | |
| 1076 | if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags)) |
| 1077 | return -EBUSY; |
| 1078 | |
Lars-Peter Clausen | cadc212 | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1079 | iio_device_get(indio_dev); |
| 1080 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1081 | filp->private_data = indio_dev; |
Jonathan Cameron | 30eb82f | 2011-09-21 11:16:02 +0100 | [diff] [blame] | 1082 | |
Lars-Peter Clausen | 7933514 | 2011-12-19 15:23:49 +0100 | [diff] [blame] | 1083 | return 0; |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | /** |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 1087 | * iio_chrdev_release() - chrdev file close buffer access and ioctls |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1088 | **/ |
| 1089 | static int iio_chrdev_release(struct inode *inode, struct file *filp) |
| 1090 | { |
Lars-Peter Clausen | bb01443 | 2011-12-19 15:23:45 +0100 | [diff] [blame] | 1091 | struct iio_dev *indio_dev = container_of(inode->i_cdev, |
| 1092 | struct iio_dev, chrdev); |
Lars-Peter Clausen | bb01443 | 2011-12-19 15:23:45 +0100 | [diff] [blame] | 1093 | clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags); |
Lars-Peter Clausen | cadc212 | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1094 | iio_device_put(indio_dev); |
| 1095 | |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1096 | return 0; |
| 1097 | } |
| 1098 | |
| 1099 | /* Somewhat of a cross file organization violation - ioctls here are actually |
| 1100 | * event related */ |
| 1101 | static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 1102 | { |
| 1103 | struct iio_dev *indio_dev = filp->private_data; |
| 1104 | int __user *ip = (int __user *)arg; |
| 1105 | int fd; |
| 1106 | |
Lars-Peter Clausen | f18e7a0 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 1107 | if (!indio_dev->info) |
| 1108 | return -ENODEV; |
| 1109 | |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1110 | if (cmd == IIO_GET_EVENT_FD_IOCTL) { |
| 1111 | fd = iio_event_getfd(indio_dev); |
| 1112 | if (copy_to_user(ip, &fd, sizeof(fd))) |
| 1113 | return -EFAULT; |
| 1114 | return 0; |
| 1115 | } |
| 1116 | return -EINVAL; |
| 1117 | } |
| 1118 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 1119 | static const struct file_operations iio_buffer_fileops = { |
| 1120 | .read = iio_buffer_read_first_n_outer_addr, |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1121 | .release = iio_chrdev_release, |
| 1122 | .open = iio_chrdev_open, |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 1123 | .poll = iio_buffer_poll_addr, |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1124 | .owner = THIS_MODULE, |
| 1125 | .llseek = noop_llseek, |
| 1126 | .unlocked_ioctl = iio_ioctl, |
| 1127 | .compat_ioctl = iio_ioctl, |
| 1128 | }; |
| 1129 | |
Michael Hennerich | 0f1acee | 2012-03-01 10:51:04 +0100 | [diff] [blame] | 1130 | static const struct iio_buffer_setup_ops noop_ring_setup_ops; |
| 1131 | |
Sachin Kamat | a7e57dc | 2013-10-29 11:39:00 +0000 | [diff] [blame] | 1132 | /** |
| 1133 | * iio_device_register() - register a device with the IIO subsystem |
| 1134 | * @indio_dev: Device structure filled by the device driver |
| 1135 | **/ |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1136 | int iio_device_register(struct iio_dev *indio_dev) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1137 | { |
| 1138 | int ret; |
| 1139 | |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 1140 | /* If the calling driver did not initialize of_node, do it here */ |
| 1141 | if (!indio_dev->dev.of_node && indio_dev->dev.parent) |
| 1142 | indio_dev->dev.of_node = indio_dev->dev.parent->of_node; |
| 1143 | |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 1144 | /* configure elements for the chrdev */ |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1145 | indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1146 | |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 1147 | ret = iio_device_register_debugfs(indio_dev); |
| 1148 | if (ret) { |
| 1149 | dev_err(indio_dev->dev.parent, |
| 1150 | "Failed to register debugfs interfaces\n"); |
Hartmut Knaack | 92825ff | 2014-02-16 11:53:00 +0000 | [diff] [blame] | 1151 | return ret; |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 1152 | } |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1153 | ret = iio_device_register_sysfs(indio_dev); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1154 | if (ret) { |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1155 | dev_err(indio_dev->dev.parent, |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1156 | "Failed to register sysfs interfaces\n"); |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 1157 | goto error_unreg_debugfs; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1158 | } |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1159 | ret = iio_device_register_eventset(indio_dev); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1160 | if (ret) { |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1161 | dev_err(indio_dev->dev.parent, |
Roel Van Nyen | c849d25 | 2010-04-29 19:27:31 +0200 | [diff] [blame] | 1162 | "Failed to register event set\n"); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1163 | goto error_free_sysfs; |
| 1164 | } |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1165 | if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) |
| 1166 | iio_device_register_trigger_consumer(indio_dev); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1167 | |
Michael Hennerich | 0f1acee | 2012-03-01 10:51:04 +0100 | [diff] [blame] | 1168 | if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) && |
| 1169 | indio_dev->setup_ops == NULL) |
| 1170 | indio_dev->setup_ops = &noop_ring_setup_ops; |
| 1171 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1172 | cdev_init(&indio_dev->chrdev, &iio_buffer_fileops); |
| 1173 | indio_dev->chrdev.owner = indio_dev->info->driver_module; |
Lars-Peter Clausen | 0d5b7da | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1174 | indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj; |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1175 | ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1); |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1176 | if (ret < 0) |
Lars-Peter Clausen | 0d5b7da | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1177 | goto error_unreg_eventset; |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1178 | |
Lars-Peter Clausen | 0d5b7da | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1179 | ret = device_add(&indio_dev->dev); |
| 1180 | if (ret < 0) |
| 1181 | goto error_cdev_del; |
| 1182 | |
| 1183 | return 0; |
| 1184 | error_cdev_del: |
| 1185 | cdev_del(&indio_dev->chrdev); |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1186 | error_unreg_eventset: |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1187 | iio_device_unregister_eventset(indio_dev); |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 1188 | error_free_sysfs: |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1189 | iio_device_unregister_sysfs(indio_dev); |
Michael Hennerich | e553f18 | 2012-03-01 10:51:03 +0100 | [diff] [blame] | 1190 | error_unreg_debugfs: |
| 1191 | iio_device_unregister_debugfs(indio_dev); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1192 | return ret; |
| 1193 | } |
| 1194 | EXPORT_SYMBOL(iio_device_register); |
| 1195 | |
Sachin Kamat | a7e57dc | 2013-10-29 11:39:00 +0000 | [diff] [blame] | 1196 | /** |
| 1197 | * iio_device_unregister() - unregister a device from the IIO subsystem |
| 1198 | * @indio_dev: Device structure representing the device. |
| 1199 | **/ |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 1200 | void iio_device_unregister(struct iio_dev *indio_dev) |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1201 | { |
Jonathan Cameron | ac917a8 | 2012-02-15 19:48:00 +0000 | [diff] [blame] | 1202 | mutex_lock(&indio_dev->info_exist_lock); |
Lars-Peter Clausen | a87c82e | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1203 | |
| 1204 | device_del(&indio_dev->dev); |
| 1205 | |
Lars-Peter Clausen | 0d5b7da | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1206 | if (indio_dev->chrdev.dev) |
| 1207 | cdev_del(&indio_dev->chrdev); |
Lars-Peter Clausen | bc4c961 | 2013-09-21 16:21:00 +0100 | [diff] [blame] | 1208 | iio_device_unregister_debugfs(indio_dev); |
Lars-Peter Clausen | 0d5b7da | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1209 | |
Lars-Peter Clausen | a87c82e | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 1210 | iio_disable_all_buffers(indio_dev); |
| 1211 | |
Jonathan Cameron | ac917a8 | 2012-02-15 19:48:00 +0000 | [diff] [blame] | 1212 | indio_dev->info = NULL; |
Lars-Peter Clausen | d2f0a48 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 1213 | |
| 1214 | iio_device_wakeup_eventset(indio_dev); |
| 1215 | iio_buffer_wakeup_poll(indio_dev); |
| 1216 | |
Jonathan Cameron | ac917a8 | 2012-02-15 19:48:00 +0000 | [diff] [blame] | 1217 | mutex_unlock(&indio_dev->info_exist_lock); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1218 | } |
| 1219 | EXPORT_SYMBOL(iio_device_unregister); |
Sachin Kamat | 8caa07c | 2013-10-29 11:39:00 +0000 | [diff] [blame] | 1220 | |
| 1221 | static void devm_iio_device_unreg(struct device *dev, void *res) |
| 1222 | { |
| 1223 | iio_device_unregister(*(struct iio_dev **)res); |
| 1224 | } |
| 1225 | |
| 1226 | /** |
| 1227 | * devm_iio_device_register - Resource-managed iio_device_register() |
| 1228 | * @dev: Device to allocate iio_dev for |
| 1229 | * @indio_dev: Device structure filled by the device driver |
| 1230 | * |
| 1231 | * Managed iio_device_register. The IIO device registered with this |
| 1232 | * function is automatically unregistered on driver detach. This function |
| 1233 | * calls iio_device_register() internally. Refer to that function for more |
| 1234 | * information. |
| 1235 | * |
| 1236 | * If an iio_dev registered with this function needs to be unregistered |
| 1237 | * separately, devm_iio_device_unregister() must be used. |
| 1238 | * |
| 1239 | * RETURNS: |
| 1240 | * 0 on success, negative error number on failure. |
| 1241 | */ |
| 1242 | int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev) |
| 1243 | { |
| 1244 | struct iio_dev **ptr; |
| 1245 | int ret; |
| 1246 | |
| 1247 | ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL); |
| 1248 | if (!ptr) |
| 1249 | return -ENOMEM; |
| 1250 | |
| 1251 | *ptr = indio_dev; |
| 1252 | ret = iio_device_register(indio_dev); |
| 1253 | if (!ret) |
| 1254 | devres_add(dev, ptr); |
| 1255 | else |
| 1256 | devres_free(ptr); |
| 1257 | |
| 1258 | return ret; |
| 1259 | } |
| 1260 | EXPORT_SYMBOL_GPL(devm_iio_device_register); |
| 1261 | |
| 1262 | /** |
| 1263 | * devm_iio_device_unregister - Resource-managed iio_device_unregister() |
| 1264 | * @dev: Device this iio_dev belongs to |
| 1265 | * @indio_dev: the iio_dev associated with the device |
| 1266 | * |
| 1267 | * Unregister iio_dev registered with devm_iio_device_register(). |
| 1268 | */ |
| 1269 | void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev) |
| 1270 | { |
| 1271 | int rc; |
| 1272 | |
| 1273 | rc = devres_release(dev, devm_iio_device_unreg, |
| 1274 | devm_iio_device_match, indio_dev); |
| 1275 | WARN_ON(rc); |
| 1276 | } |
| 1277 | EXPORT_SYMBOL_GPL(devm_iio_device_unregister); |
| 1278 | |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1279 | subsys_initcall(iio_init); |
| 1280 | module_exit(iio_exit); |
| 1281 | |
Jonathan Cameron | c8b9595 | 2012-09-02 21:27:56 +0100 | [diff] [blame] | 1282 | MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); |
Jonathan Cameron | 847ec80 | 2009-08-18 18:06:19 +0100 | [diff] [blame] | 1283 | MODULE_DESCRIPTION("Industrial I/O core"); |
| 1284 | MODULE_LICENSE("GPL"); |