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