blob: 57e1cb376e22d6140b136ea3090a4a8664d75d60 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Jonathan Cameron847ec802009-08-18 18:06:19 +01002/* The industrial I/O core
3 *
4 * Copyright (c) 2008 Jonathan Cameron
5 *
Jonathan Cameron847ec802009-08-18 18:06:19 +01006 * Based on elements of hwmon and input subsystems.
7 */
8
Sachin Kamat3176dd52013-10-24 12:53:00 +01009#define pr_fmt(fmt) "iio-core: " fmt
10
Jonathan Cameron847ec802009-08-18 18:06:19 +010011#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/idr.h>
14#include <linux/kdev_t.h>
15#include <linux/err.h>
16#include <linux/device.h>
17#include <linux/fs.h>
Jonathan Cameron847ec802009-08-18 18:06:19 +010018#include <linux/poll.h>
Andy Shevchenkofb158972019-02-21 18:02:46 +010019#include <linux/property.h>
Jonathan Cameronffc18af2009-10-12 19:18:09 +010020#include <linux/sched.h>
Jeff Mahoney4439c932009-10-12 17:10:34 -040021#include <linux/wait.h>
Jonathan Cameron847ec802009-08-18 18:06:19 +010022#include <linux/cdev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Jonathan Cameron8e7d9672011-08-30 12:32:45 +010024#include <linux/anon_inodes.h>
Michael Henneriche553f182012-03-01 10:51:03 +010025#include <linux/debugfs.h>
Alison Schofield08a33802016-03-09 11:30:12 -080026#include <linux/mutex.h>
Jonathan Cameron06458e22012-04-25 15:54:58 +010027#include <linux/iio/iio.h>
Alexandru Ardelean6d4ebd52020-06-30 07:57:03 +030028#include <linux/iio/iio-opaque.h>
Jonathan Camerondf9c1c42011-08-12 17:56:03 +010029#include "iio_core.h"
Jonathan Cameron6aea1c32011-08-24 17:28:38 +010030#include "iio_core_trigger.h"
Jonathan Cameron06458e22012-04-25 15:54:58 +010031#include <linux/iio/sysfs.h>
32#include <linux/iio/events.h>
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +010033#include <linux/iio/buffer.h>
Jonathan Cameron33dd94c2017-01-02 19:28:34 +000034#include <linux/iio/buffer_impl.h>
Jonathan Cameron9dd1cb32011-08-30 12:41:15 +010035
Peter Meerwald99698b42012-08-26 13:43:00 +010036/* IDA to assign each registered device a unique id */
Jonathan Cameronb156cf72010-09-04 17:54:43 +010037static DEFINE_IDA(iio_ida);
Jonathan Cameron847ec802009-08-18 18:06:19 +010038
Jonathan Cameronf625cb92011-08-30 12:32:48 +010039static dev_t iio_devt;
Jonathan Cameron847ec802009-08-18 18:06:19 +010040
41#define IIO_DEV_MAX 256
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +010042struct bus_type iio_bus_type = {
Jonathan Cameron847ec802009-08-18 18:06:19 +010043 .name = "iio",
Jonathan Cameron847ec802009-08-18 18:06:19 +010044};
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +010045EXPORT_SYMBOL(iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +010046
Michael Henneriche553f182012-03-01 10:51:03 +010047static struct dentry *iio_debugfs_dentry;
48
Jonathan Cameronc6fc8062011-09-02 17:14:34 +010049static const char * const iio_direction[] = {
50 [0] = "in",
51 [1] = "out",
52};
53
Jonathan Cameronade7ef72011-09-02 17:14:45 +010054static const char * const iio_chan_type_name_spec[] = {
Jonathan Cameronc6fc8062011-09-02 17:14:34 +010055 [IIO_VOLTAGE] = "voltage",
Michael Hennerichfaf290e2011-05-18 14:42:02 +010056 [IIO_CURRENT] = "current",
57 [IIO_POWER] = "power",
Bryan Freed9bff02f2011-07-07 12:01:54 -070058 [IIO_ACCEL] = "accel",
Jonathan Cameron41ea0402011-10-05 15:27:59 +010059 [IIO_ANGL_VEL] = "anglvel",
Jonathan Cameron1d892712011-05-18 14:40:51 +010060 [IIO_MAGN] = "magn",
Bryan Freed9bff02f2011-07-07 12:01:54 -070061 [IIO_LIGHT] = "illuminance",
62 [IIO_INTENSITY] = "intensity",
Bryan Freedf09f2c82011-07-07 12:01:55 -070063 [IIO_PROXIMITY] = "proximity",
Bryan Freed9bff02f2011-07-07 12:01:54 -070064 [IIO_TEMP] = "temp",
Jonathan Cameron1d892712011-05-18 14:40:51 +010065 [IIO_INCLI] = "incli",
66 [IIO_ROT] = "rot",
Jonathan Cameron1d892712011-05-18 14:40:51 +010067 [IIO_ANGL] = "angl",
Bryan Freed9bff02f2011-07-07 12:01:54 -070068 [IIO_TIMESTAMP] = "timestamp",
Jonathan Cameron66dbe702011-09-02 17:14:43 +010069 [IIO_CAPACITANCE] = "capacitance",
Michael Hennericha6b12852012-04-27 10:58:34 +020070 [IIO_ALTVOLTAGE] = "altvoltage",
Jon Brenner21cd1fa2012-05-16 10:46:42 -050071 [IIO_CCT] = "cct",
Lars-Peter Clausenc4f0c692012-11-20 13:36:00 +000072 [IIO_PRESSURE] = "pressure",
Harald Geyerac216aa2013-12-01 15:08:00 +000073 [IIO_HUMIDITYRELATIVE] = "humidityrelative",
Daniel Baluta55aebeb2014-11-10 14:45:30 +020074 [IIO_ACTIVITY] = "activity",
Irina Tirdeaa88bfe72014-11-10 14:45:32 +020075 [IIO_STEPS] = "steps",
Irina Tirdea72c66642015-01-11 21:10:07 +020076 [IIO_ENERGY] = "energy",
Irina Tirdeacc3c9ee2015-01-11 21:10:08 +020077 [IIO_DISTANCE] = "distance",
Irina Tirdea5a1a9322015-01-11 21:10:09 +020078 [IIO_VELOCITY] = "velocity",
Matt Ranostay8ff6b3b2015-09-13 20:26:11 -070079 [IIO_CONCENTRATION] = "concentration",
Matt Ranostayd38d5462015-09-13 20:26:12 -070080 [IIO_RESISTANCE] = "resistance",
Matt Ranostayecb3a7c2016-01-26 18:34:30 -080081 [IIO_PH] = "ph",
Peter Meerwald-Stadlerd4094042016-03-20 16:20:23 +010082 [IIO_UVINDEX] = "uvindex",
Matt Ranostay4b9d2092016-05-24 21:29:19 -070083 [IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity",
William Breathitt Gray1a8f3242016-09-28 13:59:49 -040084 [IIO_COUNT] = "count",
85 [IIO_INDEX] = "index",
Song Hongyan571299d2017-01-05 18:24:03 +080086 [IIO_GRAVITY] = "gravity",
Eugen Hristev3055a6c2018-05-22 10:52:32 +030087 [IIO_POSITIONRELATIVE] = "positionrelative",
Mathieu Othacehec73314e2018-07-20 19:34:25 +020088 [IIO_PHASE] = "phase",
Tomasz Duszynski17abc9e2018-12-14 19:28:01 +010089 [IIO_MASSCONCENTRATION] = "massconcentration",
Jonathan Cameron1d892712011-05-18 14:40:51 +010090};
91
Jonathan Cameron330c6c52011-09-02 17:14:39 +010092static const char * const iio_modifier_names[] = {
Jonathan Cameron1d892712011-05-18 14:40:51 +010093 [IIO_MOD_X] = "x",
94 [IIO_MOD_Y] = "y",
95 [IIO_MOD_Z] = "z",
Peter Meerwald4b8d8012015-06-20 23:52:30 +020096 [IIO_MOD_X_AND_Y] = "x&y",
97 [IIO_MOD_X_AND_Z] = "x&z",
98 [IIO_MOD_Y_AND_Z] = "y&z",
99 [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
100 [IIO_MOD_X_OR_Y] = "x|y",
101 [IIO_MOD_X_OR_Z] = "x|z",
102 [IIO_MOD_Y_OR_Z] = "y|z",
103 [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
Jonathan Cameron8f5879b2012-05-05 10:39:22 +0100104 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
Jonathan Cameroncf82cb82012-05-05 10:56:41 +0100105 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100106 [IIO_MOD_LIGHT_BOTH] = "both",
107 [IIO_MOD_LIGHT_IR] = "ir",
Jon Brenner21cd1fa2012-05-16 10:46:42 -0500108 [IIO_MOD_LIGHT_CLEAR] = "clear",
109 [IIO_MOD_LIGHT_RED] = "red",
110 [IIO_MOD_LIGHT_GREEN] = "green",
111 [IIO_MOD_LIGHT_BLUE] = "blue",
Peter Meerwald-Stadler2c5ff1f2016-03-20 16:20:22 +0100112 [IIO_MOD_LIGHT_UV] = "uv",
Maxime Roussin-Bélangerc0e4e0f2018-07-19 16:26:24 -0400113 [IIO_MOD_LIGHT_DUV] = "duv",
Srinivas Pandruvada5082f402014-04-29 00:51:00 +0100114 [IIO_MOD_QUATERNION] = "quaternion",
Peter Meerwald638b43b2014-02-05 16:57:00 +0000115 [IIO_MOD_TEMP_AMBIENT] = "ambient",
116 [IIO_MOD_TEMP_OBJECT] = "object",
Reyad Attiyat11b8dda2014-07-17 19:18:00 +0100117 [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
118 [IIO_MOD_NORTH_TRUE] = "from_north_true",
119 [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
120 [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
Daniel Baluta55aebeb2014-11-10 14:45:30 +0200121 [IIO_MOD_RUNNING] = "running",
122 [IIO_MOD_JOGGING] = "jogging",
123 [IIO_MOD_WALKING] = "walking",
124 [IIO_MOD_STILL] = "still",
Irina Tirdea5a1a9322015-01-11 21:10:09 +0200125 [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
Lars-Peter Clausen1ce87f22015-05-22 18:17:38 +0200126 [IIO_MOD_I] = "i",
127 [IIO_MOD_Q] = "q",
Matt Ranostay8ff6b3b2015-09-13 20:26:11 -0700128 [IIO_MOD_CO2] = "co2",
129 [IIO_MOD_VOC] = "voc",
Tomasz Duszynski17abc9e2018-12-14 19:28:01 +0100130 [IIO_MOD_PM1] = "pm1",
131 [IIO_MOD_PM2P5] = "pm2p5",
132 [IIO_MOD_PM4] = "pm4",
133 [IIO_MOD_PM10] = "pm10",
Matt Ranostay25f02d32020-06-09 06:01:16 +0300134 [IIO_MOD_ETHANOL] = "ethanol",
135 [IIO_MOD_H2] = "h2",
Matt Ranostay4ffa22f2020-07-23 09:29:43 +0300136 [IIO_MOD_O2] = "o2",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100137};
138
139/* relies on pairs of these shared then separate */
140static const char * const iio_chan_info_postfix[] = {
Jonathan Cameron75a973c2012-04-15 17:41:30 +0100141 [IIO_CHAN_INFO_RAW] = "raw",
142 [IIO_CHAN_INFO_PROCESSED] = "input",
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100143 [IIO_CHAN_INFO_SCALE] = "scale",
144 [IIO_CHAN_INFO_OFFSET] = "offset",
145 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
146 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
147 [IIO_CHAN_INFO_PEAK] = "peak_raw",
148 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
149 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
150 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
Jonathan Camerondf94aba2011-11-27 11:39:12 +0000151 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
152 = "filter_low_pass_3db_frequency",
Martin Fuzzey3f7f6422015-05-13 12:26:42 +0200153 [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY]
154 = "filter_high_pass_3db_frequency",
Laxman Dewangance85a1c2012-04-13 16:03:31 +0530155 [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
Michael Hennericha6b12852012-04-27 10:58:34 +0200156 [IIO_CHAN_INFO_FREQUENCY] = "frequency",
157 [IIO_CHAN_INFO_PHASE] = "phase",
Michael Hennerichb65d6212012-05-11 11:36:53 +0200158 [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
srinivas pandruvada7c9ab032012-09-05 13:56:00 +0100159 [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
Peter Meerwald899d90b2013-09-08 16:20:00 +0100160 [IIO_CHAN_INFO_INT_TIME] = "integration_time",
Irina Tirdeaa88bfe72014-11-10 14:45:32 +0200161 [IIO_CHAN_INFO_ENABLE] = "en",
Irina Tirdeabcdf28f2014-11-10 14:45:33 +0200162 [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight",
Irina Tirdead37f6832015-01-11 21:10:10 +0200163 [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight",
Irina Tirdea2f0ecb72015-01-27 20:41:52 +0200164 [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count",
165 [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time",
Vianney le Clément de Saint-Marcqc8a85852015-03-30 10:34:58 +0200166 [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity",
Irina Tirdeafaaa4492015-04-29 21:16:39 +0300167 [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio",
Andrea Merelloddfb97d2019-11-20 15:47:52 +0100168 [IIO_CHAN_INFO_THERMOCOUPLE_TYPE] = "thermocouple_type",
Crt Moridde8cee2020-09-06 23:02:31 +0200169 [IIO_CHAN_INFO_CALIBAMBIENT] = "calibambient",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100170};
171
Lars-Peter Clausend9a0e732021-01-07 13:20:49 +0200172/**
173 * iio_sysfs_match_string_with_gaps - matches given string in an array with gaps
174 * @array: array of strings
175 * @n: number of strings in the array
176 * @str: string to match with
177 *
178 * Returns index of @str in the @array or -EINVAL, similar to match_string().
179 * Uses sysfs_streq instead of strcmp for matching.
180 *
181 * This routine will look for a string in an array of strings.
182 * The search will continue until the element is found or the n-th element
183 * is reached, regardless of any NULL elements in the array.
184 */
185static int iio_sysfs_match_string_with_gaps(const char * const *array, size_t n,
186 const char *str)
187{
188 const char *item;
189 int index;
190
191 for (index = 0; index < n; index++) {
192 item = array[index];
193 if (!item)
194 continue;
195 if (sysfs_streq(item, str))
196 return index;
197 }
198
199 return -EINVAL;
200}
201
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300202#if defined(CONFIG_DEBUG_FS)
Jonathan Cameronc9561fd2020-09-13 14:21:15 +0100203/*
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300204 * There's also a CONFIG_DEBUG_FS guard in include/linux/iio/iio.h for
205 * iio_get_debugfs_dentry() to make it inline if CONFIG_DEBUG_FS is undefined
206 */
207struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
208{
209 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
210 return iio_dev_opaque->debugfs_dentry;
211}
212EXPORT_SYMBOL_GPL(iio_get_debugfs_dentry);
213#endif
214
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000215/**
216 * iio_find_channel_from_si() - get channel from its scan index
217 * @indio_dev: device
218 * @si: scan index to match
219 */
Jonathan Cameron5fb21c82011-12-05 21:37:10 +0000220const struct iio_chan_spec
221*iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
222{
223 int i;
224
225 for (i = 0; i < indio_dev->num_channels; i++)
226 if (indio_dev->channels[i].scan_index == si)
227 return &indio_dev->channels[i];
228 return NULL;
229}
230
Jonathan Cameron847ec802009-08-18 18:06:19 +0100231/* This turns up an awful lot */
232ssize_t iio_read_const_attr(struct device *dev,
233 struct device_attribute *attr,
234 char *buf)
235{
236 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
237}
238EXPORT_SYMBOL(iio_read_const_attr);
239
Gwendal Grignou69f07932020-03-27 15:34:37 -0700240/**
241 * iio_device_set_clock() - Set current timestamping clock for the device
242 * @indio_dev: IIO device structure containing the device
243 * @clock_id: timestamping clock posix identifier to set.
244 */
245int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id)
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100246{
247 int ret;
Alexandru Ardeleanfa83c3b2020-06-30 07:57:08 +0300248 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
249 const struct iio_event_interface *ev_int = iio_dev_opaque->event_interface;
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100250
251 ret = mutex_lock_interruptible(&indio_dev->mlock);
252 if (ret)
253 return ret;
254 if ((ev_int && iio_event_enabled(ev_int)) ||
255 iio_buffer_enabled(indio_dev)) {
256 mutex_unlock(&indio_dev->mlock);
257 return -EBUSY;
258 }
259 indio_dev->clock_id = clock_id;
260 mutex_unlock(&indio_dev->mlock);
261
262 return 0;
263}
Gwendal Grignou69f07932020-03-27 15:34:37 -0700264EXPORT_SYMBOL(iio_device_set_clock);
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100265
266/**
267 * iio_get_time_ns() - utility function to get a time stamp for events etc
268 * @indio_dev: device
269 */
270s64 iio_get_time_ns(const struct iio_dev *indio_dev)
271{
Arnd Bergmann45e7f5d2018-06-18 17:31:36 +0200272 struct timespec64 tp;
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100273
274 switch (iio_device_get_clock(indio_dev)) {
275 case CLOCK_REALTIME:
Arnd Bergmann45e7f5d2018-06-18 17:31:36 +0200276 return ktime_get_real_ns();
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100277 case CLOCK_MONOTONIC:
Arnd Bergmann45e7f5d2018-06-18 17:31:36 +0200278 return ktime_get_ns();
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100279 case CLOCK_MONOTONIC_RAW:
Arnd Bergmann45e7f5d2018-06-18 17:31:36 +0200280 return ktime_get_raw_ns();
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100281 case CLOCK_REALTIME_COARSE:
Arnd Bergmann45e7f5d2018-06-18 17:31:36 +0200282 return ktime_to_ns(ktime_get_coarse_real());
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100283 case CLOCK_MONOTONIC_COARSE:
Arnd Bergmann45e7f5d2018-06-18 17:31:36 +0200284 ktime_get_coarse_ts64(&tp);
285 return timespec64_to_ns(&tp);
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100286 case CLOCK_BOOTTIME:
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +0200287 return ktime_get_boottime_ns();
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100288 case CLOCK_TAI:
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +0200289 return ktime_get_clocktai_ns();
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100290 default:
291 BUG();
292 }
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100293}
294EXPORT_SYMBOL(iio_get_time_ns);
295
296/**
297 * iio_get_time_res() - utility function to get time stamp clock resolution in
298 * nano seconds.
299 * @indio_dev: device
300 */
301unsigned int iio_get_time_res(const struct iio_dev *indio_dev)
302{
303 switch (iio_device_get_clock(indio_dev)) {
304 case CLOCK_REALTIME:
305 case CLOCK_MONOTONIC:
306 case CLOCK_MONOTONIC_RAW:
307 case CLOCK_BOOTTIME:
308 case CLOCK_TAI:
309 return hrtimer_resolution;
310 case CLOCK_REALTIME_COARSE:
311 case CLOCK_MONOTONIC_COARSE:
312 return LOW_RES_NSEC;
313 default:
314 BUG();
315 }
316}
317EXPORT_SYMBOL(iio_get_time_res);
318
Jonathan Cameron847ec802009-08-18 18:06:19 +0100319static int __init iio_init(void)
320{
321 int ret;
322
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100323 /* Register sysfs bus */
324 ret = bus_register(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100325 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100326 pr_err("could not register bus type\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +0100327 goto error_nothing;
328 }
329
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100330 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
331 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100332 pr_err("failed to allocate char dev region\n");
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100333 goto error_unregister_bus_type;
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100334 }
Jonathan Cameron847ec802009-08-18 18:06:19 +0100335
Michael Henneriche553f182012-03-01 10:51:03 +0100336 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
337
Jonathan Cameron847ec802009-08-18 18:06:19 +0100338 return 0;
339
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100340error_unregister_bus_type:
341 bus_unregister(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100342error_nothing:
343 return ret;
344}
345
346static void __exit iio_exit(void)
347{
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100348 if (iio_devt)
349 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100350 bus_unregister(&iio_bus_type);
Michael Henneriche553f182012-03-01 10:51:03 +0100351 debugfs_remove(iio_debugfs_dentry);
352}
353
354#if defined(CONFIG_DEBUG_FS)
Michael Henneriche553f182012-03-01 10:51:03 +0100355static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
356 size_t count, loff_t *ppos)
357{
358 struct iio_dev *indio_dev = file->private_data;
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300359 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Michael Henneriche553f182012-03-01 10:51:03 +0100360 unsigned val = 0;
Michael Henneriche553f182012-03-01 10:51:03 +0100361 int ret;
362
Alexandru Tachici2ddc9822020-02-21 14:06:55 +0200363 if (*ppos > 0)
364 return simple_read_from_buffer(userbuf, count, ppos,
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300365 iio_dev_opaque->read_buf,
366 iio_dev_opaque->read_buf_len);
Alexandru Tachici2ddc9822020-02-21 14:06:55 +0200367
Michael Henneriche553f182012-03-01 10:51:03 +0100368 ret = indio_dev->info->debugfs_reg_access(indio_dev,
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300369 iio_dev_opaque->cached_reg_addr,
Michael Henneriche553f182012-03-01 10:51:03 +0100370 0, &val);
Matt Fornero3d62c782017-09-05 16:34:10 +0200371 if (ret) {
Michael Henneriche553f182012-03-01 10:51:03 +0100372 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
Matt Fornero3d62c782017-09-05 16:34:10 +0200373 return ret;
374 }
Michael Henneriche553f182012-03-01 10:51:03 +0100375
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300376 iio_dev_opaque->read_buf_len = snprintf(iio_dev_opaque->read_buf,
377 sizeof(iio_dev_opaque->read_buf),
378 "0x%X\n", val);
Michael Henneriche553f182012-03-01 10:51:03 +0100379
Alexandru Tachici2ddc9822020-02-21 14:06:55 +0200380 return simple_read_from_buffer(userbuf, count, ppos,
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300381 iio_dev_opaque->read_buf,
382 iio_dev_opaque->read_buf_len);
Michael Henneriche553f182012-03-01 10:51:03 +0100383}
384
385static ssize_t iio_debugfs_write_reg(struct file *file,
386 const char __user *userbuf, size_t count, loff_t *ppos)
387{
388 struct iio_dev *indio_dev = file->private_data;
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300389 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Michael Henneriche553f182012-03-01 10:51:03 +0100390 unsigned reg, val;
391 char buf[80];
392 int ret;
393
394 count = min_t(size_t, count, (sizeof(buf)-1));
395 if (copy_from_user(buf, userbuf, count))
396 return -EFAULT;
397
398 buf[count] = 0;
399
400 ret = sscanf(buf, "%i %i", &reg, &val);
401
402 switch (ret) {
403 case 1:
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300404 iio_dev_opaque->cached_reg_addr = reg;
Michael Henneriche553f182012-03-01 10:51:03 +0100405 break;
406 case 2:
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300407 iio_dev_opaque->cached_reg_addr = reg;
Michael Henneriche553f182012-03-01 10:51:03 +0100408 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
409 val, NULL);
410 if (ret) {
411 dev_err(indio_dev->dev.parent, "%s: write failed\n",
412 __func__);
413 return ret;
414 }
415 break;
416 default:
417 return -EINVAL;
418 }
419
420 return count;
421}
422
423static const struct file_operations iio_debugfs_reg_fops = {
Axel Lin5a28c872012-05-03 09:50:51 +0800424 .open = simple_open,
Michael Henneriche553f182012-03-01 10:51:03 +0100425 .read = iio_debugfs_read_reg,
426 .write = iio_debugfs_write_reg,
427};
428
429static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
430{
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300431 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
432 debugfs_remove_recursive(iio_dev_opaque->debugfs_dentry);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100433}
434
Greg Kroah-Hartman8915aac2019-06-18 17:54:40 +0200435static void iio_device_register_debugfs(struct iio_dev *indio_dev)
Michael Henneriche553f182012-03-01 10:51:03 +0100436{
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300437 struct iio_dev_opaque *iio_dev_opaque;
438
Michael Henneriche553f182012-03-01 10:51:03 +0100439 if (indio_dev->info->debugfs_reg_access == NULL)
Greg Kroah-Hartman8915aac2019-06-18 17:54:40 +0200440 return;
Michael Henneriche553f182012-03-01 10:51:03 +0100441
Axel Linabd5a2f2012-05-03 22:56:58 +0800442 if (!iio_debugfs_dentry)
Greg Kroah-Hartman8915aac2019-06-18 17:54:40 +0200443 return;
Michael Henneriche553f182012-03-01 10:51:03 +0100444
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300445 iio_dev_opaque = to_iio_dev_opaque(indio_dev);
446
447 iio_dev_opaque->debugfs_dentry =
Michael Henneriche553f182012-03-01 10:51:03 +0100448 debugfs_create_dir(dev_name(&indio_dev->dev),
449 iio_debugfs_dentry);
Michael Henneriche553f182012-03-01 10:51:03 +0100450
Greg Kroah-Hartman8915aac2019-06-18 17:54:40 +0200451 debugfs_create_file("direct_reg_access", 0644,
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300452 iio_dev_opaque->debugfs_dentry, indio_dev,
Greg Kroah-Hartman8915aac2019-06-18 17:54:40 +0200453 &iio_debugfs_reg_fops);
Michael Henneriche553f182012-03-01 10:51:03 +0100454}
455#else
Greg Kroah-Hartman8915aac2019-06-18 17:54:40 +0200456static void iio_device_register_debugfs(struct iio_dev *indio_dev)
Michael Henneriche553f182012-03-01 10:51:03 +0100457{
Michael Henneriche553f182012-03-01 10:51:03 +0100458}
459
460static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
461{
462}
463#endif /* CONFIG_DEBUG_FS */
464
Lars-Peter Clausen4fee7e162012-03-06 20:43:45 +0100465static ssize_t iio_read_channel_ext_info(struct device *dev,
466 struct device_attribute *attr,
467 char *buf)
468{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200469 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e162012-03-06 20:43:45 +0100470 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
471 const struct iio_chan_spec_ext_info *ext_info;
472
473 ext_info = &this_attr->c->ext_info[this_attr->address];
474
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200475 return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
Lars-Peter Clausen4fee7e162012-03-06 20:43:45 +0100476}
477
478static ssize_t iio_write_channel_ext_info(struct device *dev,
479 struct device_attribute *attr,
480 const char *buf,
481 size_t len)
482{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200483 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e162012-03-06 20:43:45 +0100484 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
485 const struct iio_chan_spec_ext_info *ext_info;
486
487 ext_info = &this_attr->c->ext_info[this_attr->address];
488
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200489 return ext_info->write(indio_dev, ext_info->private,
490 this_attr->c, buf, len);
Lars-Peter Clausen4fee7e162012-03-06 20:43:45 +0100491}
492
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200493ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
494 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
495{
496 const struct iio_enum *e = (const struct iio_enum *)priv;
497 unsigned int i;
498 size_t len = 0;
499
500 if (!e->num_items)
501 return 0;
502
Lars-Peter Clausend9a0e732021-01-07 13:20:49 +0200503 for (i = 0; i < e->num_items; ++i) {
504 if (!e->items[i])
505 continue;
Lars-Peter Clausen74dcd432012-06-05 18:24:12 +0200506 len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
Lars-Peter Clausend9a0e732021-01-07 13:20:49 +0200507 }
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200508
509 /* replace last space with a newline */
510 buf[len - 1] = '\n';
511
512 return len;
513}
514EXPORT_SYMBOL_GPL(iio_enum_available_read);
515
516ssize_t iio_enum_read(struct iio_dev *indio_dev,
517 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
518{
519 const struct iio_enum *e = (const struct iio_enum *)priv;
520 int i;
521
522 if (!e->get)
523 return -EINVAL;
524
525 i = e->get(indio_dev, chan);
526 if (i < 0)
527 return i;
Lars-Peter Clausend9a0e732021-01-07 13:20:49 +0200528 else if (i >= e->num_items || !e->items[i])
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200529 return -EINVAL;
530
Kees Cook598db582014-03-13 16:46:00 +0000531 return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200532}
533EXPORT_SYMBOL_GPL(iio_enum_read);
534
535ssize_t iio_enum_write(struct iio_dev *indio_dev,
536 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
537 size_t len)
538{
539 const struct iio_enum *e = (const struct iio_enum *)priv;
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200540 int ret;
541
542 if (!e->set)
543 return -EINVAL;
544
Lars-Peter Clausend9a0e732021-01-07 13:20:49 +0200545 ret = iio_sysfs_match_string_with_gaps(e->items, e->num_items, buf);
Andy Shevchenko02e9a0ff2017-06-09 15:08:16 +0300546 if (ret < 0)
547 return ret;
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200548
Andy Shevchenko02e9a0ff2017-06-09 15:08:16 +0300549 ret = e->set(indio_dev, chan, ret);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200550 return ret ? ret : len;
551}
552EXPORT_SYMBOL_GPL(iio_enum_write);
553
Gregor Boiriedfc57732016-04-20 19:23:43 +0200554static const struct iio_mount_matrix iio_mount_idmatrix = {
555 .rotation = {
556 "1", "0", "0",
557 "0", "1", "0",
558 "0", "0", "1"
559 }
560};
561
562static int iio_setup_mount_idmatrix(const struct device *dev,
563 struct iio_mount_matrix *matrix)
564{
565 *matrix = iio_mount_idmatrix;
566 dev_info(dev, "mounting matrix not found: using identity...\n");
567 return 0;
568}
569
570ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv,
571 const struct iio_chan_spec *chan, char *buf)
572{
573 const struct iio_mount_matrix *mtx = ((iio_get_mount_matrix_t *)
574 priv)(indio_dev, chan);
575
576 if (IS_ERR(mtx))
577 return PTR_ERR(mtx);
578
579 if (!mtx)
580 mtx = &iio_mount_idmatrix;
581
582 return snprintf(buf, PAGE_SIZE, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n",
583 mtx->rotation[0], mtx->rotation[1], mtx->rotation[2],
584 mtx->rotation[3], mtx->rotation[4], mtx->rotation[5],
585 mtx->rotation[6], mtx->rotation[7], mtx->rotation[8]);
586}
587EXPORT_SYMBOL_GPL(iio_show_mount_matrix);
588
589/**
Andy Shevchenkofb158972019-02-21 18:02:46 +0100590 * iio_read_mount_matrix() - retrieve iio device mounting matrix from
591 * device "mount-matrix" property
Gregor Boiriedfc57732016-04-20 19:23:43 +0200592 * @dev: device the mounting matrix property is assigned to
593 * @propname: device specific mounting matrix property name
594 * @matrix: where to store retrieved matrix
595 *
596 * If device is assigned no mounting matrix property, a default 3x3 identity
597 * matrix will be filled in.
598 *
599 * Return: 0 if success, or a negative error code on failure.
600 */
Andy Shevchenkofb158972019-02-21 18:02:46 +0100601int iio_read_mount_matrix(struct device *dev, const char *propname,
602 struct iio_mount_matrix *matrix)
Gregor Boiriedfc57732016-04-20 19:23:43 +0200603{
Andy Shevchenkofb158972019-02-21 18:02:46 +0100604 size_t len = ARRAY_SIZE(iio_mount_idmatrix.rotation);
605 int err;
Gregor Boiriedfc57732016-04-20 19:23:43 +0200606
Andy Shevchenkofb158972019-02-21 18:02:46 +0100607 err = device_property_read_string_array(dev, propname,
608 matrix->rotation, len);
609 if (err == len)
610 return 0;
Gregor Boiriedfc57732016-04-20 19:23:43 +0200611
Andy Shevchenkofb158972019-02-21 18:02:46 +0100612 if (err >= 0)
613 /* Invalid number of matrix entries. */
614 return -EINVAL;
Gregor Boiriedfc57732016-04-20 19:23:43 +0200615
Andy Shevchenkofb158972019-02-21 18:02:46 +0100616 if (err != -EINVAL)
617 /* Invalid matrix declaration format. */
618 return err;
Gregor Boiriedfc57732016-04-20 19:23:43 +0200619
620 /* Matrix was not declared at all: fallback to identity. */
621 return iio_setup_mount_idmatrix(dev, matrix);
622}
Andy Shevchenkofb158972019-02-21 18:02:46 +0100623EXPORT_SYMBOL(iio_read_mount_matrix);
Gregor Boiriedfc57732016-04-20 19:23:43 +0200624
Jonathan Cameron51239602016-11-08 12:58:51 +0100625static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
626 int size, const int *vals)
627{
Jonathan Cameron51239602016-11-08 12:58:51 +0100628 int tmp0, tmp1;
Anand Ashok Dumbre0e7a3972020-10-05 08:05:16 -0700629 s64 tmp2;
Jonathan Cameron51239602016-11-08 12:58:51 +0100630 bool scale_db = false;
631
632 switch (type) {
633 case IIO_VAL_INT:
Takashi Iwai35a4eeb2020-03-11 08:43:24 +0100634 return scnprintf(buf, len, "%d", vals[0]);
Jonathan Cameron51239602016-11-08 12:58:51 +0100635 case IIO_VAL_INT_PLUS_MICRO_DB:
636 scale_db = true;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500637 fallthrough;
Jonathan Cameron51239602016-11-08 12:58:51 +0100638 case IIO_VAL_INT_PLUS_MICRO:
639 if (vals[1] < 0)
Takashi Iwai35a4eeb2020-03-11 08:43:24 +0100640 return scnprintf(buf, len, "-%d.%06u%s", abs(vals[0]),
Jonathan Cameron51239602016-11-08 12:58:51 +0100641 -vals[1], scale_db ? " dB" : "");
642 else
Takashi Iwai35a4eeb2020-03-11 08:43:24 +0100643 return scnprintf(buf, len, "%d.%06u%s", vals[0], vals[1],
Jonathan Cameron51239602016-11-08 12:58:51 +0100644 scale_db ? " dB" : "");
645 case IIO_VAL_INT_PLUS_NANO:
646 if (vals[1] < 0)
Takashi Iwai35a4eeb2020-03-11 08:43:24 +0100647 return scnprintf(buf, len, "-%d.%09u", abs(vals[0]),
Jonathan Cameron51239602016-11-08 12:58:51 +0100648 -vals[1]);
649 else
Takashi Iwai35a4eeb2020-03-11 08:43:24 +0100650 return scnprintf(buf, len, "%d.%09u", vals[0], vals[1]);
Jonathan Cameron51239602016-11-08 12:58:51 +0100651 case IIO_VAL_FRACTIONAL:
Anand Ashok Dumbre0e7a3972020-10-05 08:05:16 -0700652 tmp2 = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
Jonathan Cameron51239602016-11-08 12:58:51 +0100653 tmp1 = vals[1];
Anand Ashok Dumbre0e7a3972020-10-05 08:05:16 -0700654 tmp0 = (int)div_s64_rem(tmp2, 1000000000, &tmp1);
655 if ((tmp2 < 0) && (tmp0 == 0))
656 return snprintf(buf, len, "-0.%09u", abs(tmp1));
657 else
658 return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
Jonathan Cameron51239602016-11-08 12:58:51 +0100659 case IIO_VAL_FRACTIONAL_LOG2:
Lars-Peter Clausen2646a952020-12-15 20:17:41 +0100660 tmp2 = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
661 tmp0 = (int)div_s64_rem(tmp2, 1000000000LL, &tmp1);
Lars-Peter Clausen38a52cd2020-12-15 20:17:42 +0100662 if (tmp0 == 0 && tmp2 < 0)
663 return snprintf(buf, len, "-0.%09u", abs(tmp1));
664 else
665 return scnprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
Jonathan Cameron51239602016-11-08 12:58:51 +0100666 case IIO_VAL_INT_MULTIPLE:
667 {
668 int i;
669 int l = 0;
670
671 for (i = 0; i < size; ++i) {
Takashi Iwai35a4eeb2020-03-11 08:43:24 +0100672 l += scnprintf(&buf[l], len - l, "%d ", vals[i]);
Jonathan Cameron51239602016-11-08 12:58:51 +0100673 if (l >= len)
674 break;
675 }
676 return l;
677 }
Andrea Merello8cb34032019-11-20 15:47:51 +0100678 case IIO_VAL_CHAR:
Takashi Iwai35a4eeb2020-03-11 08:43:24 +0100679 return scnprintf(buf, len, "%c", (char)vals[0]);
Jonathan Cameron51239602016-11-08 12:58:51 +0100680 default:
681 return 0;
682 }
683}
684
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100685/**
686 * iio_format_value() - Formats a IIO value into its string representation
Cristina Opriceana2498dcf2015-07-24 16:16:19 +0300687 * @buf: The buffer to which the formatted value gets written
Jonathan Cameron51239602016-11-08 12:58:51 +0100688 * which is assumed to be big enough (i.e. PAGE_SIZE).
Randy Dunlapc175cb72017-10-29 17:06:01 -0700689 * @type: One of the IIO_VAL_* constants. This decides how the val
Cristina Opriceana2498dcf2015-07-24 16:16:19 +0300690 * and val2 parameters are formatted.
691 * @size: Number of IIO value entries contained in vals
692 * @vals: Pointer to the values, exact meaning depends on the
693 * type parameter.
694 *
695 * Return: 0 by default, a negative number on failure or the
696 * total number of characters written for a type that belongs
Randy Dunlapc175cb72017-10-29 17:06:01 -0700697 * to the IIO_VAL_* constant.
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100698 */
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100699ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100700{
Jonathan Cameron51239602016-11-08 12:58:51 +0100701 ssize_t len;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100702
Jonathan Cameron51239602016-11-08 12:58:51 +0100703 len = __iio_format_value(buf, PAGE_SIZE, type, size, vals);
704 if (len >= PAGE_SIZE - 1)
705 return -EFBIG;
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100706
Jonathan Cameron51239602016-11-08 12:58:51 +0100707 return len + sprintf(buf + len, "\n");
Jonathan Cameron1d892712011-05-18 14:40:51 +0100708}
Andrew F. Davis7d2c2aca2015-12-14 16:35:57 -0600709EXPORT_SYMBOL_GPL(iio_format_value);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100710
Cristian Pop1d4ef9b2020-09-28 12:09:55 +0300711static ssize_t iio_read_channel_label(struct device *dev,
712 struct device_attribute *attr,
713 char *buf)
714{
715 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
716 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
717
718 if (!indio_dev->info->read_label)
719 return -EINVAL;
720
721 return indio_dev->info->read_label(indio_dev, this_attr->c, buf);
722}
723
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100724static ssize_t iio_read_channel_info(struct device *dev,
725 struct device_attribute *attr,
726 char *buf)
727{
728 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
729 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100730 int vals[INDIO_MAX_RAW_ELEMENTS];
731 int ret;
732 int val_len = 2;
733
734 if (indio_dev->info->read_raw_multi)
735 ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
736 INDIO_MAX_RAW_ELEMENTS,
737 vals, &val_len,
738 this_attr->address);
739 else
740 ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
741 &vals[0], &vals[1], this_attr->address);
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100742
743 if (ret < 0)
744 return ret;
745
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100746 return iio_format_value(buf, ret, val_len, vals);
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100747}
748
Lars-Peter Clauseneda20ba2020-11-14 12:59:59 +0100749static ssize_t iio_format_list(char *buf, const int *vals, int type, int length,
750 const char *prefix, const char *suffix)
Jonathan Cameron51239602016-11-08 12:58:51 +0100751{
Lars-Peter Clauseneda20ba2020-11-14 12:59:59 +0100752 ssize_t len;
Lars-Peter Clausene08b60d2020-11-14 13:00:00 +0100753 int stride;
Jonathan Cameron51239602016-11-08 12:58:51 +0100754 int i;
Lars-Peter Clauseneda20ba2020-11-14 12:59:59 +0100755
Jonathan Cameron51239602016-11-08 12:58:51 +0100756 switch (type) {
757 case IIO_VAL_INT:
Lars-Peter Clausene08b60d2020-11-14 13:00:00 +0100758 stride = 1;
Jonathan Cameron51239602016-11-08 12:58:51 +0100759 break;
760 default:
Lars-Peter Clausene08b60d2020-11-14 13:00:00 +0100761 stride = 2;
762 break;
763 }
764
765 len = scnprintf(buf, PAGE_SIZE, prefix);
766
767 for (i = 0; i <= length - stride; i += stride) {
768 if (i != 0) {
769 len += scnprintf(buf + len, PAGE_SIZE - len, " ");
Jonathan Cameron51239602016-11-08 12:58:51 +0100770 if (len >= PAGE_SIZE)
771 return -EFBIG;
772 }
Lars-Peter Clausene08b60d2020-11-14 13:00:00 +0100773
774 len += __iio_format_value(buf + len, PAGE_SIZE - len, type,
775 stride, &vals[i]);
776 if (len >= PAGE_SIZE)
777 return -EFBIG;
Jonathan Cameron51239602016-11-08 12:58:51 +0100778 }
779
Lars-Peter Clausene08b60d2020-11-14 13:00:00 +0100780 len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n", suffix);
781
Jonathan Cameron51239602016-11-08 12:58:51 +0100782 return len;
783}
784
Lars-Peter Clauseneda20ba2020-11-14 12:59:59 +0100785static ssize_t iio_format_avail_list(char *buf, const int *vals,
786 int type, int length)
787{
788
789 return iio_format_list(buf, vals, type, length, "", "");
790}
791
Jonathan Cameron51239602016-11-08 12:58:51 +0100792static ssize_t iio_format_avail_range(char *buf, const int *vals, int type)
793{
Lars-Peter Clauseneda20ba2020-11-14 12:59:59 +0100794 return iio_format_list(buf, vals, type, 3, "[", "]");
Jonathan Cameron51239602016-11-08 12:58:51 +0100795}
796
797static ssize_t iio_read_channel_info_avail(struct device *dev,
798 struct device_attribute *attr,
799 char *buf)
800{
801 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
802 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
803 const int *vals;
804 int ret;
805 int length;
806 int type;
807
808 ret = indio_dev->info->read_avail(indio_dev, this_attr->c,
809 &vals, &type, &length,
810 this_attr->address);
811
812 if (ret < 0)
813 return ret;
814 switch (ret) {
815 case IIO_AVAIL_LIST:
816 return iio_format_avail_list(buf, vals, type, length);
817 case IIO_AVAIL_RANGE:
818 return iio_format_avail_range(buf, vals, type);
819 default:
820 return -EINVAL;
821 }
822}
823
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000824/**
Beniamin Biab8528222020-02-06 17:11:45 +0200825 * __iio_str_to_fixpoint() - Parse a fixed-point number from a string
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000826 * @str: The string to parse
827 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
828 * @integer: The integer part of the number
829 * @fract: The fractional part of the number
Beniamin Biab8528222020-02-06 17:11:45 +0200830 * @scale_db: True if this should parse as dB
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000831 *
832 * Returns 0 on success, or a negative error code if the string could not be
833 * parsed.
834 */
Beniamin Biab8528222020-02-06 17:11:45 +0200835static int __iio_str_to_fixpoint(const char *str, int fract_mult,
836 int *integer, int *fract, bool scale_db)
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000837{
838 int i = 0, f = 0;
839 bool integer_part = true, negative = false;
840
Sean Nyekjaerf47dff32015-11-09 13:55:34 +0100841 if (fract_mult == 0) {
842 *fract = 0;
843
844 return kstrtoint(str, 0, integer);
845 }
846
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000847 if (str[0] == '-') {
848 negative = true;
849 str++;
850 } else if (str[0] == '+') {
851 str++;
852 }
853
854 while (*str) {
855 if ('0' <= *str && *str <= '9') {
856 if (integer_part) {
857 i = i * 10 + *str - '0';
858 } else {
859 f += fract_mult * (*str - '0');
860 fract_mult /= 10;
861 }
862 } else if (*str == '\n') {
863 if (*(str + 1) == '\0')
864 break;
865 else
866 return -EINVAL;
Beniamin Biab8528222020-02-06 17:11:45 +0200867 } else if (!strncmp(str, " dB", sizeof(" dB") - 1) && scale_db) {
868 /* Ignore the dB suffix */
869 str += sizeof(" dB") - 1;
870 continue;
871 } else if (!strncmp(str, "dB", sizeof("dB") - 1) && scale_db) {
872 /* Ignore the dB suffix */
873 str += sizeof("dB") - 1;
874 continue;
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000875 } else if (*str == '.' && integer_part) {
876 integer_part = false;
877 } else {
878 return -EINVAL;
879 }
880 str++;
881 }
882
883 if (negative) {
884 if (i)
885 i = -i;
886 else
887 f = -f;
888 }
889
890 *integer = i;
891 *fract = f;
892
893 return 0;
894}
Beniamin Biab8528222020-02-06 17:11:45 +0200895
896/**
897 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
898 * @str: The string to parse
899 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
900 * @integer: The integer part of the number
901 * @fract: The fractional part of the number
902 *
903 * Returns 0 on success, or a negative error code if the string could not be
904 * parsed.
905 */
906int iio_str_to_fixpoint(const char *str, int fract_mult,
907 int *integer, int *fract)
908{
909 return __iio_str_to_fixpoint(str, fract_mult, integer, fract, false);
910}
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000911EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
912
Jonathan Cameron1d892712011-05-18 14:40:51 +0100913static ssize_t iio_write_channel_info(struct device *dev,
914 struct device_attribute *attr,
915 const char *buf,
916 size_t len)
917{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200918 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100919 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000920 int ret, fract_mult = 100000;
Andrea Merello8cb34032019-11-20 15:47:51 +0100921 int integer, fract = 0;
922 bool is_char = false;
Beniamin Biab8528222020-02-06 17:11:45 +0200923 bool scale_db = false;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100924
925 /* Assumes decimal - precision based on number of digits */
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100926 if (!indio_dev->info->write_raw)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100927 return -EINVAL;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100928
929 if (indio_dev->info->write_raw_get_fmt)
930 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
931 this_attr->c, this_attr->address)) {
Sean Nyekjaerf47dff32015-11-09 13:55:34 +0100932 case IIO_VAL_INT:
933 fract_mult = 0;
934 break;
Beniamin Biab8528222020-02-06 17:11:45 +0200935 case IIO_VAL_INT_PLUS_MICRO_DB:
936 scale_db = true;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500937 fallthrough;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100938 case IIO_VAL_INT_PLUS_MICRO:
939 fract_mult = 100000;
940 break;
941 case IIO_VAL_INT_PLUS_NANO:
942 fract_mult = 100000000;
943 break;
Andrea Merello8cb34032019-11-20 15:47:51 +0100944 case IIO_VAL_CHAR:
945 is_char = true;
946 break;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100947 default:
948 return -EINVAL;
949 }
950
Andrea Merello8cb34032019-11-20 15:47:51 +0100951 if (is_char) {
952 char ch;
953
954 if (sscanf(buf, "%c", &ch) != 1)
955 return -EINVAL;
956 integer = ch;
957 } else {
Mircea Caprioru5403f282020-04-01 14:22:30 +0300958 ret = __iio_str_to_fixpoint(buf, fract_mult, &integer, &fract,
959 scale_db);
Andrea Merello8cb34032019-11-20 15:47:51 +0100960 if (ret)
961 return ret;
962 }
Jonathan Cameron847ec802009-08-18 18:06:19 +0100963
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100964 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
Michael Hennerich5c04af02011-06-27 13:07:10 +0100965 integer, fract, this_attr->address);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100966 if (ret)
967 return ret;
968
969 return len;
970}
971
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100972static
Jonathan Cameron1d892712011-05-18 14:40:51 +0100973int __iio_device_attr_init(struct device_attribute *dev_attr,
974 const char *postfix,
975 struct iio_chan_spec const *chan,
976 ssize_t (*readfunc)(struct device *dev,
977 struct device_attribute *attr,
978 char *buf),
979 ssize_t (*writefunc)(struct device *dev,
980 struct device_attribute *attr,
981 const char *buf,
982 size_t len),
Jonathan Cameron37044322013-09-08 14:57:00 +0100983 enum iio_shared_by shared_by)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100984{
Jonathan Cameron37044322013-09-08 14:57:00 +0100985 int ret = 0;
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000986 char *name = NULL;
Jonathan Cameron37044322013-09-08 14:57:00 +0100987 char *full_postfix;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100988 sysfs_attr_init(&dev_attr->attr);
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100989
990 /* Build up postfix of <extend_name>_<modifier>_postfix */
Jonathan Cameron37044322013-09-08 14:57:00 +0100991 if (chan->modified && (shared_by == IIO_SEPARATE)) {
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100992 if (chan->extend_name)
993 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
994 iio_modifier_names[chan
995 ->channel2],
996 chan->extend_name,
997 postfix);
998 else
999 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
1000 iio_modifier_names[chan
1001 ->channel2],
1002 postfix);
1003 } else {
Lars-Peter Clausen77bfa8b2014-02-14 14:19:00 +00001004 if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
Jonathan Cameronade7ef72011-09-02 17:14:45 +01001005 full_postfix = kstrdup(postfix, GFP_KERNEL);
1006 else
1007 full_postfix = kasprintf(GFP_KERNEL,
1008 "%s_%s",
1009 chan->extend_name,
1010 postfix);
1011 }
Jonathan Cameron37044322013-09-08 14:57:00 +01001012 if (full_postfix == NULL)
1013 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001014
Justin P. Mattock4abf6f82012-02-29 22:00:38 -08001015 if (chan->differential) { /* Differential can not have modifier */
Jonathan Cameron37044322013-09-08 14:57:00 +01001016 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +01001017 case IIO_SHARED_BY_ALL:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001018 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
Jonathan Cameronc006ec82013-09-08 14:57:00 +01001019 break;
1020 case IIO_SHARED_BY_DIR:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001021 name = kasprintf(GFP_KERNEL, "%s_%s",
Jonathan Cameronc006ec82013-09-08 14:57:00 +01001022 iio_direction[chan->output],
1023 full_postfix);
1024 break;
Jonathan Cameron37044322013-09-08 14:57:00 +01001025 case IIO_SHARED_BY_TYPE:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001026 name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +01001027 iio_direction[chan->output],
1028 iio_chan_type_name_spec[chan->type],
1029 iio_chan_type_name_spec[chan->type],
1030 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +01001031 break;
1032 case IIO_SEPARATE:
1033 if (!chan->indexed) {
Dan Carpenter231bfe52015-11-21 13:33:00 +03001034 WARN(1, "Differential channels must be indexed\n");
Jonathan Cameron37044322013-09-08 14:57:00 +01001035 ret = -EINVAL;
1036 goto error_free_full_postfix;
1037 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001038 name = kasprintf(GFP_KERNEL,
Jonathan Cameron37044322013-09-08 14:57:00 +01001039 "%s_%s%d-%s%d_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +01001040 iio_direction[chan->output],
1041 iio_chan_type_name_spec[chan->type],
1042 chan->channel,
1043 iio_chan_type_name_spec[chan->type],
1044 chan->channel2,
1045 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +01001046 break;
Jonathan Cameronade7ef72011-09-02 17:14:45 +01001047 }
1048 } else { /* Single ended */
Jonathan Cameron37044322013-09-08 14:57:00 +01001049 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +01001050 case IIO_SHARED_BY_ALL:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001051 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
Jonathan Cameronc006ec82013-09-08 14:57:00 +01001052 break;
1053 case IIO_SHARED_BY_DIR:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001054 name = kasprintf(GFP_KERNEL, "%s_%s",
Jonathan Cameronc006ec82013-09-08 14:57:00 +01001055 iio_direction[chan->output],
1056 full_postfix);
1057 break;
Jonathan Cameron37044322013-09-08 14:57:00 +01001058 case IIO_SHARED_BY_TYPE:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001059 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +01001060 iio_direction[chan->output],
1061 iio_chan_type_name_spec[chan->type],
1062 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +01001063 break;
1064
1065 case IIO_SEPARATE:
1066 if (chan->indexed)
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001067 name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
Jonathan Cameron37044322013-09-08 14:57:00 +01001068 iio_direction[chan->output],
1069 iio_chan_type_name_spec[chan->type],
1070 chan->channel,
1071 full_postfix);
1072 else
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001073 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
Jonathan Cameron37044322013-09-08 14:57:00 +01001074 iio_direction[chan->output],
1075 iio_chan_type_name_spec[chan->type],
1076 full_postfix);
1077 break;
1078 }
Jonathan Cameronade7ef72011-09-02 17:14:45 +01001079 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001080 if (name == NULL) {
Jonathan Cameron1d892712011-05-18 14:40:51 +01001081 ret = -ENOMEM;
1082 goto error_free_full_postfix;
1083 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001084 dev_attr->attr.name = name;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001085
1086 if (readfunc) {
1087 dev_attr->attr.mode |= S_IRUGO;
1088 dev_attr->show = readfunc;
1089 }
1090
1091 if (writefunc) {
1092 dev_attr->attr.mode |= S_IWUSR;
1093 dev_attr->store = writefunc;
1094 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001095
Jonathan Cameron1d892712011-05-18 14:40:51 +01001096error_free_full_postfix:
1097 kfree(full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +01001098
Jonathan Cameron847ec802009-08-18 18:06:19 +01001099 return ret;
1100}
1101
Jonathan Camerondf9c1c42011-08-12 17:56:03 +01001102static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
Jonathan Cameron1d892712011-05-18 14:40:51 +01001103{
1104 kfree(dev_attr->attr.name);
1105}
1106
1107int __iio_add_chan_devattr(const char *postfix,
Jonathan Cameron1d892712011-05-18 14:40:51 +01001108 struct iio_chan_spec const *chan,
1109 ssize_t (*readfunc)(struct device *dev,
1110 struct device_attribute *attr,
1111 char *buf),
1112 ssize_t (*writefunc)(struct device *dev,
1113 struct device_attribute *attr,
1114 const char *buf,
1115 size_t len),
Jonathan Camerone614a542011-09-02 17:14:41 +01001116 u64 mask,
Jonathan Cameron37044322013-09-08 14:57:00 +01001117 enum iio_shared_by shared_by,
Jonathan Cameron1d892712011-05-18 14:40:51 +01001118 struct device *dev,
Alexandru Ardelean3e3d11b2021-02-15 12:40:32 +02001119 struct iio_buffer *buffer,
Jonathan Cameron1d892712011-05-18 14:40:51 +01001120 struct list_head *attr_list)
1121{
1122 int ret;
1123 struct iio_dev_attr *iio_attr, *t;
1124
Sachin Kamat670c1102013-10-24 12:53:00 +01001125 iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
Hartmut Knaack92825ff2014-02-16 11:53:00 +00001126 if (iio_attr == NULL)
1127 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001128 ret = __iio_device_attr_init(&iio_attr->dev_attr,
1129 postfix, chan,
Jonathan Cameron37044322013-09-08 14:57:00 +01001130 readfunc, writefunc, shared_by);
Jonathan Cameron1d892712011-05-18 14:40:51 +01001131 if (ret)
1132 goto error_iio_dev_attr_free;
1133 iio_attr->c = chan;
1134 iio_attr->address = mask;
Alexandru Ardelean3e3d11b2021-02-15 12:40:32 +02001135 iio_attr->buffer = buffer;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001136 list_for_each_entry(t, attr_list, l)
1137 if (strcmp(t->dev_attr.attr.name,
1138 iio_attr->dev_attr.attr.name) == 0) {
Jonathan Cameron37044322013-09-08 14:57:00 +01001139 if (shared_by == IIO_SEPARATE)
Jonathan Cameron1d892712011-05-18 14:40:51 +01001140 dev_err(dev, "tried to double register : %s\n",
1141 t->dev_attr.attr.name);
1142 ret = -EBUSY;
1143 goto error_device_attr_deinit;
1144 }
Jonathan Cameron1d892712011-05-18 14:40:51 +01001145 list_add(&iio_attr->l, attr_list);
1146
1147 return 0;
1148
1149error_device_attr_deinit:
1150 __iio_device_attr_deinit(&iio_attr->dev_attr);
1151error_iio_dev_attr_free:
1152 kfree(iio_attr);
Jonathan Cameron1d892712011-05-18 14:40:51 +01001153 return ret;
1154}
1155
Cristian Pop1d4ef9b2020-09-28 12:09:55 +03001156static int iio_device_add_channel_label(struct iio_dev *indio_dev,
1157 struct iio_chan_spec const *chan)
1158{
1159 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1160 int ret;
1161
1162 if (!indio_dev->info->read_label)
1163 return 0;
1164
1165 ret = __iio_add_chan_devattr("label",
1166 chan,
1167 &iio_read_channel_label,
1168 NULL,
1169 0,
1170 IIO_SEPARATE,
1171 &indio_dev->dev,
Alexandru Ardelean3e3d11b2021-02-15 12:40:32 +02001172 NULL,
Cristian Pop1d4ef9b2020-09-28 12:09:55 +03001173 &iio_dev_opaque->channel_attr_list);
1174 if (ret < 0)
1175 return ret;
1176
1177 return 1;
1178}
1179
Jonathan Cameron37044322013-09-08 14:57:00 +01001180static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
1181 struct iio_chan_spec const *chan,
1182 enum iio_shared_by shared_by,
1183 const long *infomask)
1184{
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001185 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Jonathan Cameron37044322013-09-08 14:57:00 +01001186 int i, ret, attrcount = 0;
1187
Orson Zhai1016d562017-04-25 09:16:56 +08001188 for_each_set_bit(i, infomask, sizeof(*infomask)*8) {
Jonathan Cameronef4b4852014-01-03 22:24:00 +00001189 if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1190 return -EINVAL;
Jonathan Cameron37044322013-09-08 14:57:00 +01001191 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
1192 chan,
1193 &iio_read_channel_info,
1194 &iio_write_channel_info,
1195 i,
1196 shared_by,
1197 &indio_dev->dev,
Alexandru Ardelean3e3d11b2021-02-15 12:40:32 +02001198 NULL,
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001199 &iio_dev_opaque->channel_attr_list);
Jonathan Cameron37044322013-09-08 14:57:00 +01001200 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
1201 continue;
1202 else if (ret < 0)
1203 return ret;
1204 attrcount++;
1205 }
1206
1207 return attrcount;
1208}
1209
Jonathan Cameron51239602016-11-08 12:58:51 +01001210static int iio_device_add_info_mask_type_avail(struct iio_dev *indio_dev,
1211 struct iio_chan_spec const *chan,
1212 enum iio_shared_by shared_by,
1213 const long *infomask)
1214{
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001215 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Jonathan Cameron51239602016-11-08 12:58:51 +01001216 int i, ret, attrcount = 0;
1217 char *avail_postfix;
1218
Orson Zhai1016d562017-04-25 09:16:56 +08001219 for_each_set_bit(i, infomask, sizeof(*infomask) * 8) {
Young Xiao936d3e52019-06-04 20:40:00 +08001220 if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1221 return -EINVAL;
Jonathan Cameron51239602016-11-08 12:58:51 +01001222 avail_postfix = kasprintf(GFP_KERNEL,
1223 "%s_available",
1224 iio_chan_info_postfix[i]);
1225 if (!avail_postfix)
1226 return -ENOMEM;
1227
1228 ret = __iio_add_chan_devattr(avail_postfix,
1229 chan,
1230 &iio_read_channel_info_avail,
1231 NULL,
1232 i,
1233 shared_by,
1234 &indio_dev->dev,
Alexandru Ardelean3e3d11b2021-02-15 12:40:32 +02001235 NULL,
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001236 &iio_dev_opaque->channel_attr_list);
Jonathan Cameron51239602016-11-08 12:58:51 +01001237 kfree(avail_postfix);
1238 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
1239 continue;
1240 else if (ret < 0)
1241 return ret;
1242 attrcount++;
1243 }
1244
1245 return attrcount;
1246}
1247
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001248static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
Jonathan Cameron1d892712011-05-18 14:40:51 +01001249 struct iio_chan_spec const *chan)
1250{
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001251 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Jonathan Cameron5ccb3ad2012-04-15 17:41:16 +01001252 int ret, attrcount = 0;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +01001253 const struct iio_chan_spec_ext_info *ext_info;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001254
Jonathan Cameron1d892712011-05-18 14:40:51 +01001255 if (chan->channel < 0)
1256 return 0;
Jonathan Cameron37044322013-09-08 14:57:00 +01001257 ret = iio_device_add_info_mask_type(indio_dev, chan,
1258 IIO_SEPARATE,
1259 &chan->info_mask_separate);
1260 if (ret < 0)
1261 return ret;
1262 attrcount += ret;
1263
Jonathan Cameron51239602016-11-08 12:58:51 +01001264 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1265 IIO_SEPARATE,
1266 &chan->
1267 info_mask_separate_available);
1268 if (ret < 0)
1269 return ret;
1270 attrcount += ret;
1271
Jonathan Cameron37044322013-09-08 14:57:00 +01001272 ret = iio_device_add_info_mask_type(indio_dev, chan,
1273 IIO_SHARED_BY_TYPE,
1274 &chan->info_mask_shared_by_type);
1275 if (ret < 0)
1276 return ret;
1277 attrcount += ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +01001278
Jonathan Cameron51239602016-11-08 12:58:51 +01001279 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1280 IIO_SHARED_BY_TYPE,
1281 &chan->
1282 info_mask_shared_by_type_available);
1283 if (ret < 0)
1284 return ret;
1285 attrcount += ret;
1286
Jonathan Cameronc006ec82013-09-08 14:57:00 +01001287 ret = iio_device_add_info_mask_type(indio_dev, chan,
1288 IIO_SHARED_BY_DIR,
1289 &chan->info_mask_shared_by_dir);
1290 if (ret < 0)
1291 return ret;
1292 attrcount += ret;
1293
Jonathan Cameron51239602016-11-08 12:58:51 +01001294 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1295 IIO_SHARED_BY_DIR,
1296 &chan->info_mask_shared_by_dir_available);
1297 if (ret < 0)
1298 return ret;
1299 attrcount += ret;
1300
Jonathan Cameronc006ec82013-09-08 14:57:00 +01001301 ret = iio_device_add_info_mask_type(indio_dev, chan,
1302 IIO_SHARED_BY_ALL,
1303 &chan->info_mask_shared_by_all);
1304 if (ret < 0)
1305 return ret;
1306 attrcount += ret;
1307
Jonathan Cameron51239602016-11-08 12:58:51 +01001308 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1309 IIO_SHARED_BY_ALL,
1310 &chan->info_mask_shared_by_all_available);
1311 if (ret < 0)
1312 return ret;
1313 attrcount += ret;
1314
Cristian Pop1d4ef9b2020-09-28 12:09:55 +03001315 ret = iio_device_add_channel_label(indio_dev, chan);
1316 if (ret < 0)
1317 return ret;
1318 attrcount += ret;
1319
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +01001320 if (chan->ext_info) {
1321 unsigned int i = 0;
1322 for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
1323 ret = __iio_add_chan_devattr(ext_info->name,
1324 chan,
1325 ext_info->read ?
1326 &iio_read_channel_ext_info : NULL,
1327 ext_info->write ?
1328 &iio_write_channel_ext_info : NULL,
1329 i,
1330 ext_info->shared,
1331 &indio_dev->dev,
Alexandru Ardelean3e3d11b2021-02-15 12:40:32 +02001332 NULL,
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001333 &iio_dev_opaque->channel_attr_list);
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +01001334 i++;
1335 if (ret == -EBUSY && ext_info->shared)
1336 continue;
1337
1338 if (ret)
Jonathan Cameron37044322013-09-08 14:57:00 +01001339 return ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +01001340
1341 attrcount++;
1342 }
1343 }
1344
Jonathan Cameron37044322013-09-08 14:57:00 +01001345 return attrcount;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001346}
1347
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +01001348/**
1349 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
1350 * @attr_list: List of IIO device attributes
1351 *
1352 * This function frees the memory allocated for each of the IIO device
Martin Fuzzeyc1b03ab2015-02-19 15:17:44 +01001353 * attributes in the list.
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +01001354 */
1355void iio_free_chan_devattr_list(struct list_head *attr_list)
Jonathan Cameron1d892712011-05-18 14:40:51 +01001356{
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +01001357 struct iio_dev_attr *p, *n;
1358
1359 list_for_each_entry_safe(p, n, attr_list, l) {
Alexandru Ardelean15097c72021-02-15 12:40:33 +02001360 kfree_const(p->dev_attr.attr.name);
Martin Fuzzeyc1b03ab2015-02-19 15:17:44 +01001361 list_del(&p->l);
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +01001362 kfree(p);
1363 }
Jonathan Cameron1d892712011-05-18 14:40:51 +01001364}
1365
Jonathan Cameron1b732882011-05-18 14:41:43 +01001366static ssize_t iio_show_dev_name(struct device *dev,
1367 struct device_attribute *attr,
1368 char *buf)
1369{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +02001370 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Kees Cook598db582014-03-13 16:46:00 +00001371 return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name);
Jonathan Cameron1b732882011-05-18 14:41:43 +01001372}
1373
1374static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
1375
Phil Reid2c3d0c92019-09-19 22:36:08 +08001376static ssize_t iio_show_dev_label(struct device *dev,
1377 struct device_attribute *attr,
1378 char *buf)
1379{
1380 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1381 return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->label);
1382}
1383
1384static DEVICE_ATTR(label, S_IRUGO, iio_show_dev_label, NULL);
1385
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001386static ssize_t iio_show_timestamp_clock(struct device *dev,
1387 struct device_attribute *attr,
1388 char *buf)
1389{
1390 const struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1391 const clockid_t clk = iio_device_get_clock(indio_dev);
1392 const char *name;
1393 ssize_t sz;
1394
1395 switch (clk) {
1396 case CLOCK_REALTIME:
1397 name = "realtime\n";
1398 sz = sizeof("realtime\n");
1399 break;
1400 case CLOCK_MONOTONIC:
1401 name = "monotonic\n";
1402 sz = sizeof("monotonic\n");
1403 break;
1404 case CLOCK_MONOTONIC_RAW:
1405 name = "monotonic_raw\n";
1406 sz = sizeof("monotonic_raw\n");
1407 break;
1408 case CLOCK_REALTIME_COARSE:
1409 name = "realtime_coarse\n";
1410 sz = sizeof("realtime_coarse\n");
1411 break;
1412 case CLOCK_MONOTONIC_COARSE:
1413 name = "monotonic_coarse\n";
1414 sz = sizeof("monotonic_coarse\n");
1415 break;
1416 case CLOCK_BOOTTIME:
1417 name = "boottime\n";
1418 sz = sizeof("boottime\n");
1419 break;
1420 case CLOCK_TAI:
1421 name = "tai\n";
1422 sz = sizeof("tai\n");
1423 break;
1424 default:
1425 BUG();
1426 }
1427
1428 memcpy(buf, name, sz);
1429 return sz;
1430}
1431
1432static ssize_t iio_store_timestamp_clock(struct device *dev,
1433 struct device_attribute *attr,
1434 const char *buf, size_t len)
1435{
1436 clockid_t clk;
1437 int ret;
1438
1439 if (sysfs_streq(buf, "realtime"))
1440 clk = CLOCK_REALTIME;
1441 else if (sysfs_streq(buf, "monotonic"))
1442 clk = CLOCK_MONOTONIC;
1443 else if (sysfs_streq(buf, "monotonic_raw"))
1444 clk = CLOCK_MONOTONIC_RAW;
1445 else if (sysfs_streq(buf, "realtime_coarse"))
1446 clk = CLOCK_REALTIME_COARSE;
1447 else if (sysfs_streq(buf, "monotonic_coarse"))
1448 clk = CLOCK_MONOTONIC_COARSE;
1449 else if (sysfs_streq(buf, "boottime"))
1450 clk = CLOCK_BOOTTIME;
1451 else if (sysfs_streq(buf, "tai"))
1452 clk = CLOCK_TAI;
1453 else
1454 return -EINVAL;
1455
1456 ret = iio_device_set_clock(dev_to_iio_dev(dev), clk);
1457 if (ret)
1458 return ret;
1459
1460 return len;
1461}
1462
Alexandru Ardelean32f17172021-02-15 12:40:29 +02001463int iio_device_register_sysfs_group(struct iio_dev *indio_dev,
1464 const struct attribute_group *group)
1465{
1466 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1467 const struct attribute_group **new, **old = iio_dev_opaque->groups;
1468 unsigned int cnt = iio_dev_opaque->groupcounter;
1469
1470 new = krealloc(old, sizeof(*new) * (cnt + 2), GFP_KERNEL);
1471 if (!new)
1472 return -ENOMEM;
1473
1474 new[iio_dev_opaque->groupcounter++] = group;
1475 new[iio_dev_opaque->groupcounter] = NULL;
1476
1477 iio_dev_opaque->groups = new;
1478
1479 return 0;
1480}
1481
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001482static DEVICE_ATTR(current_timestamp_clock, S_IRUGO | S_IWUSR,
1483 iio_show_timestamp_clock, iio_store_timestamp_clock);
1484
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001485static int iio_device_register_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron1d892712011-05-18 14:40:51 +01001486{
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001487 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001488 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +01001489 struct iio_dev_attr *p;
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001490 struct attribute **attr, *clk = NULL;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001491
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001492 /* First count elements in any existing group */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001493 if (indio_dev->info->attrs) {
1494 attr = indio_dev->info->attrs->attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001495 while (*attr++ != NULL)
1496 attrcount_orig++;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001497 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001498 attrcount = attrcount_orig;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001499 /*
1500 * New channel registration method - relies on the fact a group does
Peter Meerwaldd25b3802012-08-26 13:43:00 +01001501 * not need to be initialized if its name is NULL.
Jonathan Cameron1d892712011-05-18 14:40:51 +01001502 */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001503 if (indio_dev->channels)
1504 for (i = 0; i < indio_dev->num_channels; i++) {
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001505 const struct iio_chan_spec *chan =
1506 &indio_dev->channels[i];
1507
1508 if (chan->type == IIO_TIMESTAMP)
1509 clk = &dev_attr_current_timestamp_clock.attr;
1510
1511 ret = iio_device_add_channel_sysfs(indio_dev, chan);
Jonathan Cameron1d892712011-05-18 14:40:51 +01001512 if (ret < 0)
1513 goto error_clear_attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001514 attrcount += ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001515 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001516
Alexandru Ardeleanfa83c3b2020-06-30 07:57:08 +03001517 if (iio_dev_opaque->event_interface)
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001518 clk = &dev_attr_current_timestamp_clock.attr;
1519
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001520 if (indio_dev->name)
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001521 attrcount++;
Phil Reid2c3d0c92019-09-19 22:36:08 +08001522 if (indio_dev->label)
1523 attrcount++;
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001524 if (clk)
1525 attrcount++;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001526
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001527 iio_dev_opaque->chan_attr_group.attrs =
1528 kcalloc(attrcount + 1,
1529 sizeof(iio_dev_opaque->chan_attr_group.attrs[0]),
1530 GFP_KERNEL);
1531 if (iio_dev_opaque->chan_attr_group.attrs == NULL) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001532 ret = -ENOMEM;
1533 goto error_clear_attrs;
Jonathan Cameron1b732882011-05-18 14:41:43 +01001534 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001535 /* Copy across original attributes */
Hans de Goede18b4c9c2020-11-25 09:46:06 +01001536 if (indio_dev->info->attrs) {
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001537 memcpy(iio_dev_opaque->chan_attr_group.attrs,
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001538 indio_dev->info->attrs->attrs,
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001539 sizeof(iio_dev_opaque->chan_attr_group.attrs[0])
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001540 *attrcount_orig);
Hans de Goede18b4c9c2020-11-25 09:46:06 +01001541 iio_dev_opaque->chan_attr_group.is_visible =
1542 indio_dev->info->attrs->is_visible;
1543 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001544 attrn = attrcount_orig;
1545 /* Add all elements from the list. */
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001546 list_for_each_entry(p, &iio_dev_opaque->channel_attr_list, l)
1547 iio_dev_opaque->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001548 if (indio_dev->name)
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001549 iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
Phil Reid2c3d0c92019-09-19 22:36:08 +08001550 if (indio_dev->label)
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001551 iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_label.attr;
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001552 if (clk)
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001553 iio_dev_opaque->chan_attr_group.attrs[attrn++] = clk;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001554
Alexandru Ardelean32f17172021-02-15 12:40:29 +02001555 ret = iio_device_register_sysfs_group(indio_dev,
1556 &iio_dev_opaque->chan_attr_group);
1557 if (ret)
1558 goto error_clear_attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001559
Jonathan Cameron1d892712011-05-18 14:40:51 +01001560 return 0;
Jonathan Cameron1b732882011-05-18 14:41:43 +01001561
Jonathan Cameron1d892712011-05-18 14:40:51 +01001562error_clear_attrs:
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001563 iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list);
Jonathan Cameron1d892712011-05-18 14:40:51 +01001564
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001565 return ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001566}
1567
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001568static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001569{
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001570 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Jonathan Cameron1d892712011-05-18 14:40:51 +01001571
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001572 iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list);
1573 kfree(iio_dev_opaque->chan_attr_group.attrs);
1574 iio_dev_opaque->chan_attr_group.attrs = NULL;
Alexandru Ardelean32f17172021-02-15 12:40:29 +02001575 kfree(iio_dev_opaque->groups);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001576}
1577
Jonathan Cameron847ec802009-08-18 18:06:19 +01001578static void iio_dev_release(struct device *device)
1579{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +02001580 struct iio_dev *indio_dev = dev_to_iio_dev(device);
Alexandru Ardelean6d4ebd52020-06-30 07:57:03 +03001581 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1582
Benjamin Gaignardd89e1192017-04-27 15:29:15 +02001583 if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001584 iio_device_unregister_trigger_consumer(indio_dev);
1585 iio_device_unregister_eventset(indio_dev);
1586 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +02001587
Alexandru Ardeleanee708e62021-02-15 12:40:38 +02001588 iio_buffers_put(indio_dev);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +01001589
Lars-Peter Clausene407fd62012-06-04 10:41:42 +02001590 ida_simple_remove(&iio_ida, indio_dev->id);
Alexandru Ardelean6d4ebd52020-06-30 07:57:03 +03001591 kfree(iio_dev_opaque);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001592}
1593
Guenter Roeck17d82b42013-02-07 17:09:00 +00001594struct device_type iio_device_type = {
Jonathan Cameron847ec802009-08-18 18:06:19 +01001595 .name = "iio_device",
1596 .release = iio_dev_release,
1597};
1598
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001599/**
1600 * iio_device_alloc() - allocate an iio_dev from a driver
Jonathan Cameronc9561fd2020-09-13 14:21:15 +01001601 * @parent: Parent device.
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001602 * @sizeof_priv: Space to allocate for private structure.
1603 **/
Alexandru Ardelean78289b42020-06-03 14:40:18 +03001604struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001605{
Alexandru Ardelean6d4ebd52020-06-30 07:57:03 +03001606 struct iio_dev_opaque *iio_dev_opaque;
Alexandru Ardelean0d596bb2021-02-15 12:40:40 +02001607 struct iio_dev *indio_dev;
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +01001608 size_t alloc_size;
1609
Alexandru Ardelean6d4ebd52020-06-30 07:57:03 +03001610 alloc_size = sizeof(struct iio_dev_opaque);
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +01001611 if (sizeof_priv) {
1612 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
1613 alloc_size += sizeof_priv;
1614 }
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +01001615
Alexandru Ardelean6d4ebd52020-06-30 07:57:03 +03001616 iio_dev_opaque = kzalloc(alloc_size, GFP_KERNEL);
1617 if (!iio_dev_opaque)
Jonathan Cameron0d4b2182020-04-19 16:13:37 +01001618 return NULL;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001619
Alexandru Ardelean0d596bb2021-02-15 12:40:40 +02001620 indio_dev = &iio_dev_opaque->indio_dev;
1621 indio_dev->priv = (char *)iio_dev_opaque +
Alexandru Ardelean6d4ebd52020-06-30 07:57:03 +03001622 ALIGN(sizeof(struct iio_dev_opaque), IIO_ALIGN);
1623
Alexandru Ardelean0d596bb2021-02-15 12:40:40 +02001624 indio_dev->dev.parent = parent;
1625 indio_dev->dev.type = &iio_device_type;
1626 indio_dev->dev.bus = &iio_bus_type;
1627 device_initialize(&indio_dev->dev);
1628 dev_set_drvdata(&indio_dev->dev, (void *)indio_dev);
1629 mutex_init(&indio_dev->mlock);
1630 mutex_init(&indio_dev->info_exist_lock);
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001631 INIT_LIST_HEAD(&iio_dev_opaque->channel_attr_list);
Jonathan Camerona9e39f92011-09-14 13:01:25 +01001632
Alexandru Ardelean0d596bb2021-02-15 12:40:40 +02001633 indio_dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
1634 if (indio_dev->id < 0) {
Jonathan Cameron0d4b2182020-04-19 16:13:37 +01001635 /* cannot use a dev_err as the name isn't available */
1636 pr_err("failed to get device id\n");
Alexandru Ardelean6d4ebd52020-06-30 07:57:03 +03001637 kfree(iio_dev_opaque);
Jonathan Cameron0d4b2182020-04-19 16:13:37 +01001638 return NULL;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001639 }
Alexandru Ardelean0d596bb2021-02-15 12:40:40 +02001640 dev_set_name(&indio_dev->dev, "iio:device%d", indio_dev->id);
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +03001641 INIT_LIST_HEAD(&iio_dev_opaque->buffer_list);
Alexandru Ardelean8dedcc32020-09-24 11:41:55 +03001642 INIT_LIST_HEAD(&iio_dev_opaque->ioctl_handlers);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001643
Alexandru Ardelean0d596bb2021-02-15 12:40:40 +02001644 return indio_dev;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001645}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001646EXPORT_SYMBOL(iio_device_alloc);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001647
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001648/**
1649 * iio_device_free() - free an iio_dev from a driver
1650 * @dev: the iio_dev associated with the device
1651 **/
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001652void iio_device_free(struct iio_dev *dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001653{
Lars-Peter Clausene407fd62012-06-04 10:41:42 +02001654 if (dev)
1655 put_device(&dev->dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001656}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001657EXPORT_SYMBOL(iio_device_free);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001658
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001659static void devm_iio_device_release(struct device *dev, void *res)
1660{
1661 iio_device_free(*(struct iio_dev **)res);
1662}
1663
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001664/**
1665 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
Alexandru Ardelean78289b42020-06-03 14:40:18 +03001666 * @parent: Device to allocate iio_dev for, and parent for this IIO device
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001667 * @sizeof_priv: Space to allocate for private structure.
1668 *
1669 * Managed iio_device_alloc. iio_dev allocated with this function is
1670 * automatically freed on driver detach.
1671 *
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001672 * RETURNS:
1673 * Pointer to allocated iio_dev on success, NULL on failure.
1674 */
Alexandru Ardelean78289b42020-06-03 14:40:18 +03001675struct iio_dev *devm_iio_device_alloc(struct device *parent, int sizeof_priv)
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001676{
1677 struct iio_dev **ptr, *iio_dev;
1678
1679 ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
1680 GFP_KERNEL);
1681 if (!ptr)
1682 return NULL;
1683
Alexandru Ardelean78289b42020-06-03 14:40:18 +03001684 iio_dev = iio_device_alloc(parent, sizeof_priv);
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001685 if (iio_dev) {
1686 *ptr = iio_dev;
Alexandru Ardelean78289b42020-06-03 14:40:18 +03001687 devres_add(parent, ptr);
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001688 } else {
1689 devres_free(ptr);
1690 }
1691
1692 return iio_dev;
1693}
1694EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
1695
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001696/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001697 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
Cristina Opriceana2498dcf2015-07-24 16:16:19 +03001698 * @inode: Inode structure for identifying the device in the file system
1699 * @filp: File structure for iio device used to keep and later access
1700 * private data
1701 *
1702 * Return: 0 on success or -EBUSY if the device is already opened
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001703 **/
1704static int iio_chrdev_open(struct inode *inode, struct file *filp)
1705{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001706 struct iio_dev *indio_dev = container_of(inode->i_cdev,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001707 struct iio_dev, chrdev);
Alexandru Ardeleanbe24dcb2021-02-15 12:40:35 +02001708 struct iio_dev_buffer_pair *ib;
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001709
1710 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
1711 return -EBUSY;
1712
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001713 iio_device_get(indio_dev);
1714
Alexandru Ardeleanbe24dcb2021-02-15 12:40:35 +02001715 ib = kmalloc(sizeof(*ib), GFP_KERNEL);
1716 if (!ib) {
1717 iio_device_put(indio_dev);
1718 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
1719 return -ENOMEM;
1720 }
1721
1722 ib->indio_dev = indio_dev;
1723 ib->buffer = indio_dev->buffer;
1724
1725 filp->private_data = ib;
Jonathan Cameron30eb82f2011-09-21 11:16:02 +01001726
Lars-Peter Clausen79335142011-12-19 15:23:49 +01001727 return 0;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001728}
1729
1730/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001731 * iio_chrdev_release() - chrdev file close buffer access and ioctls
Cristina Opriceana2498dcf2015-07-24 16:16:19 +03001732 * @inode: Inode structure pointer for the char device
1733 * @filp: File structure pointer for the char device
1734 *
1735 * Return: 0 for successful release
1736 */
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001737static int iio_chrdev_release(struct inode *inode, struct file *filp)
1738{
Alexandru Ardeleanbe24dcb2021-02-15 12:40:35 +02001739 struct iio_dev_buffer_pair *ib = filp->private_data;
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001740 struct iio_dev *indio_dev = container_of(inode->i_cdev,
1741 struct iio_dev, chrdev);
Alexandru Ardeleanbe24dcb2021-02-15 12:40:35 +02001742 kfree(ib);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001743 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001744 iio_device_put(indio_dev);
1745
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001746 return 0;
1747}
1748
Alexandru Ardelean8dedcc32020-09-24 11:41:55 +03001749void iio_device_ioctl_handler_register(struct iio_dev *indio_dev,
1750 struct iio_ioctl_handler *h)
1751{
1752 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1753
1754 list_add_tail(&h->entry, &iio_dev_opaque->ioctl_handlers);
1755}
1756
1757void iio_device_ioctl_handler_unregister(struct iio_ioctl_handler *h)
1758{
1759 list_del(&h->entry);
1760}
1761
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001762static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1763{
Alexandru Ardeleanbe24dcb2021-02-15 12:40:35 +02001764 struct iio_dev_buffer_pair *ib = filp->private_data;
1765 struct iio_dev *indio_dev = ib->indio_dev;
Alexandru Ardelean8dedcc32020-09-24 11:41:55 +03001766 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1767 struct iio_ioctl_handler *h;
1768 int ret = -ENODEV;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001769
Alexandru Ardelean8dedcc32020-09-24 11:41:55 +03001770 mutex_lock(&indio_dev->info_exist_lock);
1771
1772 /**
1773 * The NULL check here is required to prevent crashing when a device
1774 * is being removed while userspace would still have open file handles
1775 * to try to access this device.
1776 */
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +01001777 if (!indio_dev->info)
Alexandru Ardelean8dedcc32020-09-24 11:41:55 +03001778 goto out_unlock;
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +01001779
Alexandru Ardelean8dedcc32020-09-24 11:41:55 +03001780 ret = -EINVAL;
1781 list_for_each_entry(h, &iio_dev_opaque->ioctl_handlers, entry) {
1782 ret = h->ioctl(indio_dev, filp, cmd, arg);
1783 if (ret != IIO_IOCTL_UNHANDLED)
1784 break;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001785 }
Alexandru Ardelean8dedcc32020-09-24 11:41:55 +03001786
Alexandru Ardelean74d826d2020-11-17 11:51:54 +02001787 if (ret == IIO_IOCTL_UNHANDLED)
1788 ret = -EINVAL;
1789
Alexandru Ardelean8dedcc32020-09-24 11:41:55 +03001790out_unlock:
1791 mutex_unlock(&indio_dev->info_exist_lock);
1792
1793 return ret;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001794}
1795
Jonathan Cameron14555b12011-09-21 11:15:57 +01001796static const struct file_operations iio_buffer_fileops = {
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001797 .owner = THIS_MODULE,
1798 .llseek = noop_llseek,
Alexandru Ardeleanee8caea2020-11-17 12:37:53 +02001799 .read = iio_buffer_read_outer_addr,
1800 .poll = iio_buffer_poll_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001801 .unlocked_ioctl = iio_ioctl,
Arnd Bergmann1832f2d2018-09-11 21:59:08 +02001802 .compat_ioctl = compat_ptr_ioctl,
Alexandru Ardeleanee8caea2020-11-17 12:37:53 +02001803 .open = iio_chrdev_open,
1804 .release = iio_chrdev_release,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001805};
1806
Alexandru Ardelean8ebaa3f2021-02-15 12:40:27 +02001807static const struct file_operations iio_event_fileops = {
1808 .owner = THIS_MODULE,
1809 .llseek = noop_llseek,
1810 .unlocked_ioctl = iio_ioctl,
1811 .compat_ioctl = compat_ptr_ioctl,
1812 .open = iio_chrdev_open,
1813 .release = iio_chrdev_release,
1814};
1815
Vlad Dogaru8f5d8722014-12-29 11:37:48 +02001816static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
1817{
1818 int i, j;
1819 const struct iio_chan_spec *channels = indio_dev->channels;
1820
1821 if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
1822 return 0;
1823
1824 for (i = 0; i < indio_dev->num_channels - 1; i++) {
1825 if (channels[i].scan_index < 0)
1826 continue;
1827 for (j = i + 1; j < indio_dev->num_channels; j++)
1828 if (channels[i].scan_index == channels[j].scan_index) {
1829 dev_err(&indio_dev->dev,
1830 "Duplicate scan index %d\n",
1831 channels[i].scan_index);
1832 return -EINVAL;
1833 }
1834 }
1835
1836 return 0;
1837}
1838
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001839static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1840
Jonathan Cameron63b19542017-07-23 17:25:43 +01001841int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001842{
Alexandru Ardelean8ebaa3f2021-02-15 12:40:27 +02001843 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Hans de Goede6f71bf12021-02-07 17:08:59 +01001844 const char *label;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001845 int ret;
1846
Alexandru Ardeleana17cb782020-04-07 18:07:43 +03001847 if (!indio_dev->info)
1848 return -EINVAL;
1849
Jonathan Cameron63b19542017-07-23 17:25:43 +01001850 indio_dev->driver_module = this_mod;
Guenter Roeck17d82b42013-02-07 17:09:00 +00001851 /* If the calling driver did not initialize of_node, do it here */
1852 if (!indio_dev->dev.of_node && indio_dev->dev.parent)
1853 indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
1854
Hans de Goede6f71bf12021-02-07 17:08:59 +01001855 label = of_get_property(indio_dev->dev.of_node, "label", NULL);
1856 if (label)
1857 indio_dev->label = label;
Phil Reid2c3d0c92019-09-19 22:36:08 +08001858
Vlad Dogaru8f5d8722014-12-29 11:37:48 +02001859 ret = iio_check_unique_scan_index(indio_dev);
1860 if (ret < 0)
1861 return ret;
1862
Greg Kroah-Hartman8915aac2019-06-18 17:54:40 +02001863 iio_device_register_debugfs(indio_dev);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001864
Alexandru Ardeleanee708e62021-02-15 12:40:38 +02001865 ret = iio_buffers_alloc_sysfs_and_mask(indio_dev);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001866 if (ret) {
1867 dev_err(indio_dev->dev.parent,
1868 "Failed to create buffer sysfs interfaces\n");
1869 goto error_unreg_debugfs;
1870 }
1871
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001872 ret = iio_device_register_sysfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001873 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001874 dev_err(indio_dev->dev.parent,
Jonathan Cameron847ec802009-08-18 18:06:19 +01001875 "Failed to register sysfs interfaces\n");
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001876 goto error_buffer_free_sysfs;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001877 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001878 ret = iio_device_register_eventset(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001879 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001880 dev_err(indio_dev->dev.parent,
Roel Van Nyenc849d252010-04-29 19:27:31 +02001881 "Failed to register event set\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001882 goto error_free_sysfs;
1883 }
Benjamin Gaignardd89e1192017-04-27 15:29:15 +02001884 if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001885 iio_device_register_trigger_consumer(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001886
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001887 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1888 indio_dev->setup_ops == NULL)
1889 indio_dev->setup_ops = &noop_ring_setup_ops;
1890
Alexandru Ardeleanee708e62021-02-15 12:40:38 +02001891 if (iio_dev_opaque->attached_buffers_cnt)
Alexandru Ardelean8ebaa3f2021-02-15 12:40:27 +02001892 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
1893 else if (iio_dev_opaque->event_interface)
1894 cdev_init(&indio_dev->chrdev, &iio_event_fileops);
Jonathan Cameron63b19542017-07-23 17:25:43 +01001895
Alexandru Ardeleanee708e62021-02-15 12:40:38 +02001896 if (iio_dev_opaque->attached_buffers_cnt || iio_dev_opaque->event_interface) {
Alexandru Ardelean8ebaa3f2021-02-15 12:40:27 +02001897 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
1898 indio_dev->chrdev.owner = this_mod;
1899 }
Logan Gunthorpe389239112017-03-17 12:48:17 -06001900
Alexandru Ardelean32f17172021-02-15 12:40:29 +02001901 /* assign device groups now; they should be all registered now */
1902 indio_dev->dev.groups = iio_dev_opaque->groups;
1903
Logan Gunthorpe389239112017-03-17 12:48:17 -06001904 ret = cdev_device_add(&indio_dev->chrdev, &indio_dev->dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001905 if (ret < 0)
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001906 goto error_unreg_eventset;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001907
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001908 return 0;
Logan Gunthorpe389239112017-03-17 12:48:17 -06001909
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001910error_unreg_eventset:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001911 iio_device_unregister_eventset(indio_dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001912error_free_sysfs:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001913 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001914error_buffer_free_sysfs:
Alexandru Ardeleanee708e62021-02-15 12:40:38 +02001915 iio_buffers_free_sysfs_and_mask(indio_dev);
Michael Henneriche553f182012-03-01 10:51:03 +01001916error_unreg_debugfs:
1917 iio_device_unregister_debugfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001918 return ret;
1919}
Jonathan Cameron63b19542017-07-23 17:25:43 +01001920EXPORT_SYMBOL(__iio_device_register);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001921
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001922/**
1923 * iio_device_unregister() - unregister a device from the IIO subsystem
1924 * @indio_dev: Device structure representing the device.
1925 **/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001926void iio_device_unregister(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001927{
Alexandru Ardelean8dedcc32020-09-24 11:41:55 +03001928 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1929 struct iio_ioctl_handler *h, *t;
1930
Logan Gunthorpe389239112017-03-17 12:48:17 -06001931 cdev_device_del(&indio_dev->chrdev, &indio_dev->dev);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001932
Fabrice Gasnier7f75591fc2019-03-25 14:01:23 +01001933 mutex_lock(&indio_dev->info_exist_lock);
1934
Lars-Peter Clausenbc4c9612013-09-21 16:21:00 +01001935 iio_device_unregister_debugfs(indio_dev);
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001936
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001937 iio_disable_all_buffers(indio_dev);
1938
Jonathan Cameronac917a82012-02-15 19:48:00 +00001939 indio_dev->info = NULL;
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +01001940
Alexandru Ardelean8dedcc32020-09-24 11:41:55 +03001941 list_for_each_entry_safe(h, t, &iio_dev_opaque->ioctl_handlers, entry)
1942 list_del(&h->entry);
1943
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +01001944 iio_device_wakeup_eventset(indio_dev);
1945 iio_buffer_wakeup_poll(indio_dev);
1946
Jonathan Cameronac917a82012-02-15 19:48:00 +00001947 mutex_unlock(&indio_dev->info_exist_lock);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001948
Alexandru Ardeleanee708e62021-02-15 12:40:38 +02001949 iio_buffers_free_sysfs_and_mask(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001950}
1951EXPORT_SYMBOL(iio_device_unregister);
Sachin Kamat8caa07c2013-10-29 11:39:00 +00001952
1953static void devm_iio_device_unreg(struct device *dev, void *res)
1954{
1955 iio_device_unregister(*(struct iio_dev **)res);
1956}
1957
Jonathan Cameron63b19542017-07-23 17:25:43 +01001958int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev,
1959 struct module *this_mod)
Sachin Kamat8caa07c2013-10-29 11:39:00 +00001960{
1961 struct iio_dev **ptr;
1962 int ret;
1963
1964 ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL);
1965 if (!ptr)
1966 return -ENOMEM;
1967
1968 *ptr = indio_dev;
Jonathan Cameron63b19542017-07-23 17:25:43 +01001969 ret = __iio_device_register(indio_dev, this_mod);
Sachin Kamat8caa07c2013-10-29 11:39:00 +00001970 if (!ret)
1971 devres_add(dev, ptr);
1972 else
1973 devres_free(ptr);
1974
1975 return ret;
1976}
Jonathan Cameron63b19542017-07-23 17:25:43 +01001977EXPORT_SYMBOL_GPL(__devm_iio_device_register);
Sachin Kamat8caa07c2013-10-29 11:39:00 +00001978
1979/**
Alison Schofield08a33802016-03-09 11:30:12 -08001980 * iio_device_claim_direct_mode - Keep device in direct mode
1981 * @indio_dev: the iio_dev associated with the device
1982 *
1983 * If the device is in direct mode it is guaranteed to stay
1984 * that way until iio_device_release_direct_mode() is called.
1985 *
1986 * Use with iio_device_release_direct_mode()
1987 *
1988 * Returns: 0 on success, -EBUSY on failure
1989 */
1990int iio_device_claim_direct_mode(struct iio_dev *indio_dev)
1991{
1992 mutex_lock(&indio_dev->mlock);
1993
1994 if (iio_buffer_enabled(indio_dev)) {
1995 mutex_unlock(&indio_dev->mlock);
1996 return -EBUSY;
1997 }
1998 return 0;
1999}
2000EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode);
2001
2002/**
2003 * iio_device_release_direct_mode - releases claim on direct mode
2004 * @indio_dev: the iio_dev associated with the device
2005 *
2006 * Release the claim. Device is no longer guaranteed to stay
2007 * in direct mode.
2008 *
2009 * Use with iio_device_claim_direct_mode()
2010 */
2011void iio_device_release_direct_mode(struct iio_dev *indio_dev)
2012{
2013 mutex_unlock(&indio_dev->mlock);
2014}
2015EXPORT_SYMBOL_GPL(iio_device_release_direct_mode);
2016
Jonathan Cameron847ec802009-08-18 18:06:19 +01002017subsys_initcall(iio_init);
2018module_exit(iio_exit);
2019
Jonathan Cameronc8b95952012-09-02 21:27:56 +01002020MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Cameron847ec802009-08-18 18:06:19 +01002021MODULE_DESCRIPTION("Industrial I/O core");
2022MODULE_LICENSE("GPL");