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