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