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