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