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