blob: 48fda6a79076b9c54e72b68cc114ce38ee24e5af [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",
Ye Xiang1c71a282021-02-07 15:00:46 +0800160 [IIO_CHAN_INFO_HYSTERESIS_RELATIVE] = "hysteresis_relative",
Peter Meerwald899d90b2013-09-08 16:20:00 +0100161 [IIO_CHAN_INFO_INT_TIME] = "integration_time",
Irina Tirdeaa88bfe72014-11-10 14:45:32 +0200162 [IIO_CHAN_INFO_ENABLE] = "en",
Irina Tirdeabcdf28f2014-11-10 14:45:33 +0200163 [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight",
Irina Tirdead37f6832015-01-11 21:10:10 +0200164 [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight",
Irina Tirdea2f0ecb72015-01-27 20:41:52 +0200165 [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count",
166 [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time",
Vianney le Clément de Saint-Marcqc8a85852015-03-30 10:34:58 +0200167 [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity",
Irina Tirdeafaaa4492015-04-29 21:16:39 +0300168 [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio",
Andrea Merelloddfb97d2019-11-20 15:47:52 +0100169 [IIO_CHAN_INFO_THERMOCOUPLE_TYPE] = "thermocouple_type",
Crt Moridde8cee2020-09-06 23:02:31 +0200170 [IIO_CHAN_INFO_CALIBAMBIENT] = "calibambient",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100171};
Jonathan Cameron15ea28782021-04-26 18:49:03 +0100172/**
173 * iio_device_id() - query the unique ID for the device
174 * @indio_dev: Device structure whose ID is being queried
175 *
176 * The IIO device ID is a unique index used for example for the naming
177 * of the character device /dev/iio\:device[ID]
178 */
179int iio_device_id(struct iio_dev *indio_dev)
180{
181 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
182
183 return iio_dev_opaque->id;
184}
185EXPORT_SYMBOL_GPL(iio_device_id);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100186
Lars-Peter Clausend9a0e732021-01-07 13:20:49 +0200187/**
188 * iio_sysfs_match_string_with_gaps - matches given string in an array with gaps
189 * @array: array of strings
190 * @n: number of strings in the array
191 * @str: string to match with
192 *
193 * Returns index of @str in the @array or -EINVAL, similar to match_string().
194 * Uses sysfs_streq instead of strcmp for matching.
195 *
196 * This routine will look for a string in an array of strings.
197 * The search will continue until the element is found or the n-th element
198 * is reached, regardless of any NULL elements in the array.
199 */
200static int iio_sysfs_match_string_with_gaps(const char * const *array, size_t n,
201 const char *str)
202{
203 const char *item;
204 int index;
205
206 for (index = 0; index < n; index++) {
207 item = array[index];
208 if (!item)
209 continue;
210 if (sysfs_streq(item, str))
211 return index;
212 }
213
214 return -EINVAL;
215}
216
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300217#if defined(CONFIG_DEBUG_FS)
Jonathan Cameronc9561fd2020-09-13 14:21:15 +0100218/*
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300219 * There's also a CONFIG_DEBUG_FS guard in include/linux/iio/iio.h for
220 * iio_get_debugfs_dentry() to make it inline if CONFIG_DEBUG_FS is undefined
221 */
222struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
223{
224 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
225 return iio_dev_opaque->debugfs_dentry;
226}
227EXPORT_SYMBOL_GPL(iio_get_debugfs_dentry);
228#endif
229
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000230/**
231 * iio_find_channel_from_si() - get channel from its scan index
232 * @indio_dev: device
233 * @si: scan index to match
234 */
Jonathan Cameron5fb21c82011-12-05 21:37:10 +0000235const struct iio_chan_spec
236*iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
237{
238 int i;
239
240 for (i = 0; i < indio_dev->num_channels; i++)
241 if (indio_dev->channels[i].scan_index == si)
242 return &indio_dev->channels[i];
243 return NULL;
244}
245
Jonathan Cameron847ec802009-08-18 18:06:19 +0100246/* This turns up an awful lot */
247ssize_t iio_read_const_attr(struct device *dev,
248 struct device_attribute *attr,
249 char *buf)
250{
Lars-Peter Clausen83ca56b2021-03-20 08:14:02 +0100251 return sysfs_emit(buf, "%s\n", to_iio_const_attr(attr)->string);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100252}
253EXPORT_SYMBOL(iio_read_const_attr);
254
Gwendal Grignou69f07932020-03-27 15:34:37 -0700255/**
256 * iio_device_set_clock() - Set current timestamping clock for the device
257 * @indio_dev: IIO device structure containing the device
258 * @clock_id: timestamping clock posix identifier to set.
259 */
260int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id)
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100261{
262 int ret;
Alexandru Ardeleanfa83c3b2020-06-30 07:57:08 +0300263 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
264 const struct iio_event_interface *ev_int = iio_dev_opaque->event_interface;
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100265
266 ret = mutex_lock_interruptible(&indio_dev->mlock);
267 if (ret)
268 return ret;
269 if ((ev_int && iio_event_enabled(ev_int)) ||
270 iio_buffer_enabled(indio_dev)) {
271 mutex_unlock(&indio_dev->mlock);
272 return -EBUSY;
273 }
Jonathan Cameron62a486c42021-04-26 18:49:11 +0100274 iio_dev_opaque->clock_id = clock_id;
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100275 mutex_unlock(&indio_dev->mlock);
276
277 return 0;
278}
Gwendal Grignou69f07932020-03-27 15:34:37 -0700279EXPORT_SYMBOL(iio_device_set_clock);
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100280
281/**
Jonathan Cameron62a486c42021-04-26 18:49:11 +0100282 * iio_device_get_clock() - Retrieve current timestamping clock for the device
283 * @indio_dev: IIO device structure containing the device
284 */
285clockid_t iio_device_get_clock(const struct iio_dev *indio_dev)
286{
287 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
288
289 return iio_dev_opaque->clock_id;
290}
291EXPORT_SYMBOL(iio_device_get_clock);
292
293/**
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100294 * iio_get_time_ns() - utility function to get a time stamp for events etc
295 * @indio_dev: device
296 */
297s64 iio_get_time_ns(const struct iio_dev *indio_dev)
298{
Arnd Bergmann45e7f5d2018-06-18 17:31:36 +0200299 struct timespec64 tp;
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100300
301 switch (iio_device_get_clock(indio_dev)) {
302 case CLOCK_REALTIME:
Arnd Bergmann45e7f5d2018-06-18 17:31:36 +0200303 return ktime_get_real_ns();
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100304 case CLOCK_MONOTONIC:
Arnd Bergmann45e7f5d2018-06-18 17:31:36 +0200305 return ktime_get_ns();
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100306 case CLOCK_MONOTONIC_RAW:
Arnd Bergmann45e7f5d2018-06-18 17:31:36 +0200307 return ktime_get_raw_ns();
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100308 case CLOCK_REALTIME_COARSE:
Arnd Bergmann45e7f5d2018-06-18 17:31:36 +0200309 return ktime_to_ns(ktime_get_coarse_real());
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100310 case CLOCK_MONOTONIC_COARSE:
Arnd Bergmann45e7f5d2018-06-18 17:31:36 +0200311 ktime_get_coarse_ts64(&tp);
312 return timespec64_to_ns(&tp);
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100313 case CLOCK_BOOTTIME:
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +0200314 return ktime_get_boottime_ns();
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100315 case CLOCK_TAI:
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +0200316 return ktime_get_clocktai_ns();
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100317 default:
318 BUG();
319 }
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100320}
321EXPORT_SYMBOL(iio_get_time_ns);
322
323/**
324 * iio_get_time_res() - utility function to get time stamp clock resolution in
325 * nano seconds.
326 * @indio_dev: device
327 */
328unsigned int iio_get_time_res(const struct iio_dev *indio_dev)
329{
330 switch (iio_device_get_clock(indio_dev)) {
331 case CLOCK_REALTIME:
332 case CLOCK_MONOTONIC:
333 case CLOCK_MONOTONIC_RAW:
334 case CLOCK_BOOTTIME:
335 case CLOCK_TAI:
336 return hrtimer_resolution;
337 case CLOCK_REALTIME_COARSE:
338 case CLOCK_MONOTONIC_COARSE:
339 return LOW_RES_NSEC;
340 default:
341 BUG();
342 }
343}
344EXPORT_SYMBOL(iio_get_time_res);
345
Jonathan Cameron847ec802009-08-18 18:06:19 +0100346static int __init iio_init(void)
347{
348 int ret;
349
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100350 /* Register sysfs bus */
351 ret = bus_register(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100352 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100353 pr_err("could not register bus type\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +0100354 goto error_nothing;
355 }
356
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100357 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
358 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100359 pr_err("failed to allocate char dev region\n");
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100360 goto error_unregister_bus_type;
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100361 }
Jonathan Cameron847ec802009-08-18 18:06:19 +0100362
Michael Henneriche553f182012-03-01 10:51:03 +0100363 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
364
Jonathan Cameron847ec802009-08-18 18:06:19 +0100365 return 0;
366
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100367error_unregister_bus_type:
368 bus_unregister(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100369error_nothing:
370 return ret;
371}
372
373static void __exit iio_exit(void)
374{
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100375 if (iio_devt)
376 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100377 bus_unregister(&iio_bus_type);
Michael Henneriche553f182012-03-01 10:51:03 +0100378 debugfs_remove(iio_debugfs_dentry);
379}
380
381#if defined(CONFIG_DEBUG_FS)
Michael Henneriche553f182012-03-01 10:51:03 +0100382static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
383 size_t count, loff_t *ppos)
384{
385 struct iio_dev *indio_dev = file->private_data;
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300386 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Michael Henneriche553f182012-03-01 10:51:03 +0100387 unsigned val = 0;
Michael Henneriche553f182012-03-01 10:51:03 +0100388 int ret;
389
Alexandru Tachici2ddc9822020-02-21 14:06:55 +0200390 if (*ppos > 0)
391 return simple_read_from_buffer(userbuf, count, ppos,
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300392 iio_dev_opaque->read_buf,
393 iio_dev_opaque->read_buf_len);
Alexandru Tachici2ddc9822020-02-21 14:06:55 +0200394
Michael Henneriche553f182012-03-01 10:51:03 +0100395 ret = indio_dev->info->debugfs_reg_access(indio_dev,
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300396 iio_dev_opaque->cached_reg_addr,
Michael Henneriche553f182012-03-01 10:51:03 +0100397 0, &val);
Matt Fornero3d62c782017-09-05 16:34:10 +0200398 if (ret) {
Michael Henneriche553f182012-03-01 10:51:03 +0100399 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
Matt Fornero3d62c782017-09-05 16:34:10 +0200400 return ret;
401 }
Michael Henneriche553f182012-03-01 10:51:03 +0100402
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300403 iio_dev_opaque->read_buf_len = snprintf(iio_dev_opaque->read_buf,
404 sizeof(iio_dev_opaque->read_buf),
405 "0x%X\n", val);
Michael Henneriche553f182012-03-01 10:51:03 +0100406
Alexandru Tachici2ddc9822020-02-21 14:06:55 +0200407 return simple_read_from_buffer(userbuf, count, ppos,
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300408 iio_dev_opaque->read_buf,
409 iio_dev_opaque->read_buf_len);
Michael Henneriche553f182012-03-01 10:51:03 +0100410}
411
412static ssize_t iio_debugfs_write_reg(struct file *file,
413 const char __user *userbuf, size_t count, loff_t *ppos)
414{
415 struct iio_dev *indio_dev = file->private_data;
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300416 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Michael Henneriche553f182012-03-01 10:51:03 +0100417 unsigned reg, val;
418 char buf[80];
419 int ret;
420
421 count = min_t(size_t, count, (sizeof(buf)-1));
422 if (copy_from_user(buf, userbuf, count))
423 return -EFAULT;
424
425 buf[count] = 0;
426
427 ret = sscanf(buf, "%i %i", &reg, &val);
428
429 switch (ret) {
430 case 1:
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300431 iio_dev_opaque->cached_reg_addr = reg;
Michael Henneriche553f182012-03-01 10:51:03 +0100432 break;
433 case 2:
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300434 iio_dev_opaque->cached_reg_addr = reg;
Michael Henneriche553f182012-03-01 10:51:03 +0100435 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
436 val, NULL);
437 if (ret) {
438 dev_err(indio_dev->dev.parent, "%s: write failed\n",
439 __func__);
440 return ret;
441 }
442 break;
443 default:
444 return -EINVAL;
445 }
446
447 return count;
448}
449
450static const struct file_operations iio_debugfs_reg_fops = {
Axel Lin5a28c872012-05-03 09:50:51 +0800451 .open = simple_open,
Michael Henneriche553f182012-03-01 10:51:03 +0100452 .read = iio_debugfs_read_reg,
453 .write = iio_debugfs_write_reg,
454};
455
456static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
457{
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300458 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
459 debugfs_remove_recursive(iio_dev_opaque->debugfs_dentry);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100460}
461
Greg Kroah-Hartman8915aac2019-06-18 17:54:40 +0200462static void iio_device_register_debugfs(struct iio_dev *indio_dev)
Michael Henneriche553f182012-03-01 10:51:03 +0100463{
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300464 struct iio_dev_opaque *iio_dev_opaque;
465
Michael Henneriche553f182012-03-01 10:51:03 +0100466 if (indio_dev->info->debugfs_reg_access == NULL)
Greg Kroah-Hartman8915aac2019-06-18 17:54:40 +0200467 return;
Michael Henneriche553f182012-03-01 10:51:03 +0100468
Axel Linabd5a2f2012-05-03 22:56:58 +0800469 if (!iio_debugfs_dentry)
Greg Kroah-Hartman8915aac2019-06-18 17:54:40 +0200470 return;
Michael Henneriche553f182012-03-01 10:51:03 +0100471
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300472 iio_dev_opaque = to_iio_dev_opaque(indio_dev);
473
474 iio_dev_opaque->debugfs_dentry =
Michael Henneriche553f182012-03-01 10:51:03 +0100475 debugfs_create_dir(dev_name(&indio_dev->dev),
476 iio_debugfs_dentry);
Michael Henneriche553f182012-03-01 10:51:03 +0100477
Greg Kroah-Hartman8915aac2019-06-18 17:54:40 +0200478 debugfs_create_file("direct_reg_access", 0644,
Alexandru Ardelean96fb1b62020-06-30 07:57:05 +0300479 iio_dev_opaque->debugfs_dentry, indio_dev,
Greg Kroah-Hartman8915aac2019-06-18 17:54:40 +0200480 &iio_debugfs_reg_fops);
Michael Henneriche553f182012-03-01 10:51:03 +0100481}
482#else
Greg Kroah-Hartman8915aac2019-06-18 17:54:40 +0200483static void iio_device_register_debugfs(struct iio_dev *indio_dev)
Michael Henneriche553f182012-03-01 10:51:03 +0100484{
Michael Henneriche553f182012-03-01 10:51:03 +0100485}
486
487static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
488{
489}
490#endif /* CONFIG_DEBUG_FS */
491
Lars-Peter Clausen4fee7e162012-03-06 20:43:45 +0100492static ssize_t iio_read_channel_ext_info(struct device *dev,
493 struct device_attribute *attr,
494 char *buf)
495{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200496 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e162012-03-06 20:43:45 +0100497 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
498 const struct iio_chan_spec_ext_info *ext_info;
499
500 ext_info = &this_attr->c->ext_info[this_attr->address];
501
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200502 return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
Lars-Peter Clausen4fee7e162012-03-06 20:43:45 +0100503}
504
505static ssize_t iio_write_channel_ext_info(struct device *dev,
506 struct device_attribute *attr,
507 const char *buf,
508 size_t len)
509{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200510 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e162012-03-06 20:43:45 +0100511 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
512 const struct iio_chan_spec_ext_info *ext_info;
513
514 ext_info = &this_attr->c->ext_info[this_attr->address];
515
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200516 return ext_info->write(indio_dev, ext_info->private,
517 this_attr->c, buf, len);
Lars-Peter Clausen4fee7e162012-03-06 20:43:45 +0100518}
519
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200520ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
521 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
522{
523 const struct iio_enum *e = (const struct iio_enum *)priv;
524 unsigned int i;
525 size_t len = 0;
526
527 if (!e->num_items)
528 return 0;
529
Lars-Peter Clausend9a0e732021-01-07 13:20:49 +0200530 for (i = 0; i < e->num_items; ++i) {
531 if (!e->items[i])
532 continue;
Lars-Peter Clausen02074832021-03-20 08:14:03 +0100533 len += sysfs_emit_at(buf, len, "%s ", e->items[i]);
Lars-Peter Clausend9a0e732021-01-07 13:20:49 +0200534 }
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200535
536 /* replace last space with a newline */
537 buf[len - 1] = '\n';
538
539 return len;
540}
541EXPORT_SYMBOL_GPL(iio_enum_available_read);
542
543ssize_t iio_enum_read(struct iio_dev *indio_dev,
544 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
545{
546 const struct iio_enum *e = (const struct iio_enum *)priv;
547 int i;
548
549 if (!e->get)
550 return -EINVAL;
551
552 i = e->get(indio_dev, chan);
553 if (i < 0)
554 return i;
Lars-Peter Clausend9a0e732021-01-07 13:20:49 +0200555 else if (i >= e->num_items || !e->items[i])
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200556 return -EINVAL;
557
Lars-Peter Clausen83ca56b2021-03-20 08:14:02 +0100558 return sysfs_emit(buf, "%s\n", e->items[i]);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200559}
560EXPORT_SYMBOL_GPL(iio_enum_read);
561
562ssize_t iio_enum_write(struct iio_dev *indio_dev,
563 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
564 size_t len)
565{
566 const struct iio_enum *e = (const struct iio_enum *)priv;
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200567 int ret;
568
569 if (!e->set)
570 return -EINVAL;
571
Lars-Peter Clausend9a0e732021-01-07 13:20:49 +0200572 ret = iio_sysfs_match_string_with_gaps(e->items, e->num_items, buf);
Andy Shevchenko02e9a0ff2017-06-09 15:08:16 +0300573 if (ret < 0)
574 return ret;
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200575
Andy Shevchenko02e9a0ff2017-06-09 15:08:16 +0300576 ret = e->set(indio_dev, chan, ret);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200577 return ret ? ret : len;
578}
579EXPORT_SYMBOL_GPL(iio_enum_write);
580
Gregor Boiriedfc57732016-04-20 19:23:43 +0200581static const struct iio_mount_matrix iio_mount_idmatrix = {
582 .rotation = {
583 "1", "0", "0",
584 "0", "1", "0",
585 "0", "0", "1"
586 }
587};
588
589static int iio_setup_mount_idmatrix(const struct device *dev,
590 struct iio_mount_matrix *matrix)
591{
592 *matrix = iio_mount_idmatrix;
593 dev_info(dev, "mounting matrix not found: using identity...\n");
594 return 0;
595}
596
597ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv,
598 const struct iio_chan_spec *chan, char *buf)
599{
600 const struct iio_mount_matrix *mtx = ((iio_get_mount_matrix_t *)
601 priv)(indio_dev, chan);
602
603 if (IS_ERR(mtx))
604 return PTR_ERR(mtx);
605
606 if (!mtx)
607 mtx = &iio_mount_idmatrix;
608
Lars-Peter Clausen83ca56b2021-03-20 08:14:02 +0100609 return sysfs_emit(buf, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n",
610 mtx->rotation[0], mtx->rotation[1], mtx->rotation[2],
611 mtx->rotation[3], mtx->rotation[4], mtx->rotation[5],
612 mtx->rotation[6], mtx->rotation[7], mtx->rotation[8]);
Gregor Boiriedfc57732016-04-20 19:23:43 +0200613}
614EXPORT_SYMBOL_GPL(iio_show_mount_matrix);
615
616/**
Andy Shevchenkofb158972019-02-21 18:02:46 +0100617 * iio_read_mount_matrix() - retrieve iio device mounting matrix from
618 * device "mount-matrix" property
Gregor Boiriedfc57732016-04-20 19:23:43 +0200619 * @dev: device the mounting matrix property is assigned to
Gregor Boiriedfc57732016-04-20 19:23:43 +0200620 * @matrix: where to store retrieved matrix
621 *
622 * If device is assigned no mounting matrix property, a default 3x3 identity
623 * matrix will be filled in.
624 *
625 * Return: 0 if success, or a negative error code on failure.
626 */
Andy Shevchenkob8927702021-05-18 14:25:46 +0300627int iio_read_mount_matrix(struct device *dev, struct iio_mount_matrix *matrix)
Gregor Boiriedfc57732016-04-20 19:23:43 +0200628{
Andy Shevchenkofb158972019-02-21 18:02:46 +0100629 size_t len = ARRAY_SIZE(iio_mount_idmatrix.rotation);
630 int err;
Gregor Boiriedfc57732016-04-20 19:23:43 +0200631
Andy Shevchenkob8927702021-05-18 14:25:46 +0300632 err = device_property_read_string_array(dev, "mount-matrix", matrix->rotation, len);
Andy Shevchenkofb158972019-02-21 18:02:46 +0100633 if (err == len)
634 return 0;
Gregor Boiriedfc57732016-04-20 19:23:43 +0200635
Andy Shevchenkofb158972019-02-21 18:02:46 +0100636 if (err >= 0)
637 /* Invalid number of matrix entries. */
638 return -EINVAL;
Gregor Boiriedfc57732016-04-20 19:23:43 +0200639
Andy Shevchenkofb158972019-02-21 18:02:46 +0100640 if (err != -EINVAL)
641 /* Invalid matrix declaration format. */
642 return err;
Gregor Boiriedfc57732016-04-20 19:23:43 +0200643
644 /* Matrix was not declared at all: fallback to identity. */
645 return iio_setup_mount_idmatrix(dev, matrix);
646}
Andy Shevchenkofb158972019-02-21 18:02:46 +0100647EXPORT_SYMBOL(iio_read_mount_matrix);
Gregor Boiriedfc57732016-04-20 19:23:43 +0200648
Lars-Peter Clausen6b92ba02021-03-20 08:14:04 +0100649static ssize_t __iio_format_value(char *buf, size_t offset, unsigned int type,
Jonathan Cameron51239602016-11-08 12:58:51 +0100650 int size, const int *vals)
651{
Jonathan Cameron51239602016-11-08 12:58:51 +0100652 int tmp0, tmp1;
Anand Ashok Dumbre0e7a3972020-10-05 08:05:16 -0700653 s64 tmp2;
Jonathan Cameron51239602016-11-08 12:58:51 +0100654 bool scale_db = false;
655
656 switch (type) {
657 case IIO_VAL_INT:
Lars-Peter Clausen6b92ba02021-03-20 08:14:04 +0100658 return sysfs_emit_at(buf, offset, "%d", vals[0]);
Jonathan Cameron51239602016-11-08 12:58:51 +0100659 case IIO_VAL_INT_PLUS_MICRO_DB:
660 scale_db = true;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500661 fallthrough;
Jonathan Cameron51239602016-11-08 12:58:51 +0100662 case IIO_VAL_INT_PLUS_MICRO:
663 if (vals[1] < 0)
Lars-Peter Clausen6b92ba02021-03-20 08:14:04 +0100664 return sysfs_emit_at(buf, offset, "-%d.%06u%s",
665 abs(vals[0]), -vals[1],
666 scale_db ? " dB" : "");
Jonathan Cameron51239602016-11-08 12:58:51 +0100667 else
Lars-Peter Clausen6b92ba02021-03-20 08:14:04 +0100668 return sysfs_emit_at(buf, offset, "%d.%06u%s", vals[0],
669 vals[1], scale_db ? " dB" : "");
Jonathan Cameron51239602016-11-08 12:58:51 +0100670 case IIO_VAL_INT_PLUS_NANO:
671 if (vals[1] < 0)
Lars-Peter Clausen6b92ba02021-03-20 08:14:04 +0100672 return sysfs_emit_at(buf, offset, "-%d.%09u",
673 abs(vals[0]), -vals[1]);
Jonathan Cameron51239602016-11-08 12:58:51 +0100674 else
Lars-Peter Clausen6b92ba02021-03-20 08:14:04 +0100675 return sysfs_emit_at(buf, offset, "%d.%09u", vals[0],
676 vals[1]);
Jonathan Cameron51239602016-11-08 12:58:51 +0100677 case IIO_VAL_FRACTIONAL:
Anand Ashok Dumbre0e7a3972020-10-05 08:05:16 -0700678 tmp2 = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
Jonathan Cameron51239602016-11-08 12:58:51 +0100679 tmp1 = vals[1];
Anand Ashok Dumbre0e7a3972020-10-05 08:05:16 -0700680 tmp0 = (int)div_s64_rem(tmp2, 1000000000, &tmp1);
681 if ((tmp2 < 0) && (tmp0 == 0))
Lars-Peter Clausen6b92ba02021-03-20 08:14:04 +0100682 return sysfs_emit_at(buf, offset, "-0.%09u", abs(tmp1));
Anand Ashok Dumbre0e7a3972020-10-05 08:05:16 -0700683 else
Lars-Peter Clausen6b92ba02021-03-20 08:14:04 +0100684 return sysfs_emit_at(buf, offset, "%d.%09u", tmp0,
685 abs(tmp1));
Jonathan Cameron51239602016-11-08 12:58:51 +0100686 case IIO_VAL_FRACTIONAL_LOG2:
Lars-Peter Clausen2646a952020-12-15 20:17:41 +0100687 tmp2 = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
688 tmp0 = (int)div_s64_rem(tmp2, 1000000000LL, &tmp1);
Lars-Peter Clausen38a52cd2020-12-15 20:17:42 +0100689 if (tmp0 == 0 && tmp2 < 0)
Lars-Peter Clausen6b92ba02021-03-20 08:14:04 +0100690 return sysfs_emit_at(buf, offset, "-0.%09u", abs(tmp1));
Lars-Peter Clausen38a52cd2020-12-15 20:17:42 +0100691 else
Lars-Peter Clausen6b92ba02021-03-20 08:14:04 +0100692 return sysfs_emit_at(buf, offset, "%d.%09u", tmp0,
693 abs(tmp1));
Jonathan Cameron51239602016-11-08 12:58:51 +0100694 case IIO_VAL_INT_MULTIPLE:
695 {
696 int i;
697 int l = 0;
698
Lars-Peter Clausen6b92ba02021-03-20 08:14:04 +0100699 for (i = 0; i < size; ++i)
700 l += sysfs_emit_at(buf, offset + l, "%d ", vals[i]);
Jonathan Cameron51239602016-11-08 12:58:51 +0100701 return l;
702 }
Andrea Merello8cb34032019-11-20 15:47:51 +0100703 case IIO_VAL_CHAR:
Lars-Peter Clausen6b92ba02021-03-20 08:14:04 +0100704 return sysfs_emit_at(buf, offset, "%c", (char)vals[0]);
Jonathan Cameron51239602016-11-08 12:58:51 +0100705 default:
706 return 0;
707 }
708}
709
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100710/**
711 * iio_format_value() - Formats a IIO value into its string representation
Cristina Opriceana2498dcf2015-07-24 16:16:19 +0300712 * @buf: The buffer to which the formatted value gets written
Jonathan Cameron51239602016-11-08 12:58:51 +0100713 * which is assumed to be big enough (i.e. PAGE_SIZE).
Randy Dunlapc175cb72017-10-29 17:06:01 -0700714 * @type: One of the IIO_VAL_* constants. This decides how the val
Cristina Opriceana2498dcf2015-07-24 16:16:19 +0300715 * and val2 parameters are formatted.
716 * @size: Number of IIO value entries contained in vals
717 * @vals: Pointer to the values, exact meaning depends on the
718 * type parameter.
719 *
720 * Return: 0 by default, a negative number on failure or the
721 * total number of characters written for a type that belongs
Randy Dunlapc175cb72017-10-29 17:06:01 -0700722 * to the IIO_VAL_* constant.
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100723 */
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100724ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100725{
Jonathan Cameron51239602016-11-08 12:58:51 +0100726 ssize_t len;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100727
Lars-Peter Clausen6b92ba02021-03-20 08:14:04 +0100728 len = __iio_format_value(buf, 0, type, size, vals);
Jonathan Cameron51239602016-11-08 12:58:51 +0100729 if (len >= PAGE_SIZE - 1)
730 return -EFBIG;
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100731
Lars-Peter Clausen6b92ba02021-03-20 08:14:04 +0100732 return len + sysfs_emit_at(buf, len, "\n");
Jonathan Cameron1d892712011-05-18 14:40:51 +0100733}
Andrew F. Davis7d2c2aca2015-12-14 16:35:57 -0600734EXPORT_SYMBOL_GPL(iio_format_value);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100735
Cristian Pop1d4ef9b2020-09-28 12:09:55 +0300736static ssize_t iio_read_channel_label(struct device *dev,
737 struct device_attribute *attr,
738 char *buf)
739{
740 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
741 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
742
Paul Cercueil13efdc32021-06-18 13:30:05 +0100743 if (indio_dev->info->read_label)
744 return indio_dev->info->read_label(indio_dev, this_attr->c, buf);
Cristian Pop1d4ef9b2020-09-28 12:09:55 +0300745
Paul Cercueil13efdc32021-06-18 13:30:05 +0100746 if (this_attr->c->extend_name)
747 return sprintf(buf, "%s\n", this_attr->c->extend_name);
748
749 return -EINVAL;
Cristian Pop1d4ef9b2020-09-28 12:09:55 +0300750}
751
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100752static ssize_t iio_read_channel_info(struct device *dev,
753 struct device_attribute *attr,
754 char *buf)
755{
756 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
757 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100758 int vals[INDIO_MAX_RAW_ELEMENTS];
759 int ret;
760 int val_len = 2;
761
762 if (indio_dev->info->read_raw_multi)
763 ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
764 INDIO_MAX_RAW_ELEMENTS,
765 vals, &val_len,
766 this_attr->address);
767 else
768 ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
769 &vals[0], &vals[1], this_attr->address);
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100770
771 if (ret < 0)
772 return ret;
773
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100774 return iio_format_value(buf, ret, val_len, vals);
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100775}
776
Lars-Peter Clauseneda20ba2020-11-14 12:59:59 +0100777static ssize_t iio_format_list(char *buf, const int *vals, int type, int length,
778 const char *prefix, const char *suffix)
Jonathan Cameron51239602016-11-08 12:58:51 +0100779{
Lars-Peter Clauseneda20ba2020-11-14 12:59:59 +0100780 ssize_t len;
Lars-Peter Clausene08b60d2020-11-14 13:00:00 +0100781 int stride;
Jonathan Cameron51239602016-11-08 12:58:51 +0100782 int i;
Lars-Peter Clauseneda20ba2020-11-14 12:59:59 +0100783
Jonathan Cameron51239602016-11-08 12:58:51 +0100784 switch (type) {
785 case IIO_VAL_INT:
Lars-Peter Clausene08b60d2020-11-14 13:00:00 +0100786 stride = 1;
Jonathan Cameron51239602016-11-08 12:58:51 +0100787 break;
788 default:
Lars-Peter Clausene08b60d2020-11-14 13:00:00 +0100789 stride = 2;
790 break;
791 }
792
Lars-Peter Clausen6b92ba02021-03-20 08:14:04 +0100793 len = sysfs_emit(buf, prefix);
Lars-Peter Clausene08b60d2020-11-14 13:00:00 +0100794
795 for (i = 0; i <= length - stride; i += stride) {
796 if (i != 0) {
Lars-Peter Clausen6b92ba02021-03-20 08:14:04 +0100797 len += sysfs_emit_at(buf, len, " ");
Jonathan Cameron51239602016-11-08 12:58:51 +0100798 if (len >= PAGE_SIZE)
799 return -EFBIG;
800 }
Lars-Peter Clausene08b60d2020-11-14 13:00:00 +0100801
Lars-Peter Clausen6b92ba02021-03-20 08:14:04 +0100802 len += __iio_format_value(buf, len, type, stride, &vals[i]);
Lars-Peter Clausene08b60d2020-11-14 13:00:00 +0100803 if (len >= PAGE_SIZE)
804 return -EFBIG;
Jonathan Cameron51239602016-11-08 12:58:51 +0100805 }
806
Lars-Peter Clausen6b92ba02021-03-20 08:14:04 +0100807 len += sysfs_emit_at(buf, len, "%s\n", suffix);
Lars-Peter Clausene08b60d2020-11-14 13:00:00 +0100808
Jonathan Cameron51239602016-11-08 12:58:51 +0100809 return len;
810}
811
Lars-Peter Clauseneda20ba2020-11-14 12:59:59 +0100812static ssize_t iio_format_avail_list(char *buf, const int *vals,
813 int type, int length)
814{
815
816 return iio_format_list(buf, vals, type, length, "", "");
817}
818
Jonathan Cameron51239602016-11-08 12:58:51 +0100819static ssize_t iio_format_avail_range(char *buf, const int *vals, int type)
820{
Lars-Peter Clauseneda20ba2020-11-14 12:59:59 +0100821 return iio_format_list(buf, vals, type, 3, "[", "]");
Jonathan Cameron51239602016-11-08 12:58:51 +0100822}
823
824static ssize_t iio_read_channel_info_avail(struct device *dev,
825 struct device_attribute *attr,
826 char *buf)
827{
828 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
829 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
830 const int *vals;
831 int ret;
832 int length;
833 int type;
834
835 ret = indio_dev->info->read_avail(indio_dev, this_attr->c,
836 &vals, &type, &length,
837 this_attr->address);
838
839 if (ret < 0)
840 return ret;
841 switch (ret) {
842 case IIO_AVAIL_LIST:
843 return iio_format_avail_list(buf, vals, type, length);
844 case IIO_AVAIL_RANGE:
845 return iio_format_avail_range(buf, vals, type);
846 default:
847 return -EINVAL;
848 }
849}
850
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000851/**
Beniamin Biab8528222020-02-06 17:11:45 +0200852 * __iio_str_to_fixpoint() - Parse a fixed-point number from a string
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000853 * @str: The string to parse
854 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
855 * @integer: The integer part of the number
856 * @fract: The fractional part of the number
Beniamin Biab8528222020-02-06 17:11:45 +0200857 * @scale_db: True if this should parse as dB
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000858 *
859 * Returns 0 on success, or a negative error code if the string could not be
860 * parsed.
861 */
Beniamin Biab8528222020-02-06 17:11:45 +0200862static int __iio_str_to_fixpoint(const char *str, int fract_mult,
863 int *integer, int *fract, bool scale_db)
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000864{
865 int i = 0, f = 0;
866 bool integer_part = true, negative = false;
867
Sean Nyekjaerf47dff32015-11-09 13:55:34 +0100868 if (fract_mult == 0) {
869 *fract = 0;
870
871 return kstrtoint(str, 0, integer);
872 }
873
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000874 if (str[0] == '-') {
875 negative = true;
876 str++;
877 } else if (str[0] == '+') {
878 str++;
879 }
880
881 while (*str) {
882 if ('0' <= *str && *str <= '9') {
883 if (integer_part) {
884 i = i * 10 + *str - '0';
885 } else {
886 f += fract_mult * (*str - '0');
887 fract_mult /= 10;
888 }
889 } else if (*str == '\n') {
890 if (*(str + 1) == '\0')
891 break;
892 else
893 return -EINVAL;
Beniamin Biab8528222020-02-06 17:11:45 +0200894 } else if (!strncmp(str, " dB", sizeof(" dB") - 1) && scale_db) {
895 /* Ignore the dB suffix */
896 str += sizeof(" dB") - 1;
897 continue;
898 } else if (!strncmp(str, "dB", sizeof("dB") - 1) && scale_db) {
899 /* Ignore the dB suffix */
900 str += sizeof("dB") - 1;
901 continue;
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000902 } else if (*str == '.' && integer_part) {
903 integer_part = false;
904 } else {
905 return -EINVAL;
906 }
907 str++;
908 }
909
910 if (negative) {
911 if (i)
912 i = -i;
913 else
914 f = -f;
915 }
916
917 *integer = i;
918 *fract = f;
919
920 return 0;
921}
Beniamin Biab8528222020-02-06 17:11:45 +0200922
923/**
924 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
925 * @str: The string to parse
926 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
927 * @integer: The integer part of the number
928 * @fract: The fractional part of the number
929 *
930 * Returns 0 on success, or a negative error code if the string could not be
931 * parsed.
932 */
933int iio_str_to_fixpoint(const char *str, int fract_mult,
934 int *integer, int *fract)
935{
936 return __iio_str_to_fixpoint(str, fract_mult, integer, fract, false);
937}
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000938EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
939
Jonathan Cameron1d892712011-05-18 14:40:51 +0100940static ssize_t iio_write_channel_info(struct device *dev,
941 struct device_attribute *attr,
942 const char *buf,
943 size_t len)
944{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200945 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100946 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000947 int ret, fract_mult = 100000;
Andrea Merello8cb34032019-11-20 15:47:51 +0100948 int integer, fract = 0;
949 bool is_char = false;
Beniamin Biab8528222020-02-06 17:11:45 +0200950 bool scale_db = false;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100951
952 /* Assumes decimal - precision based on number of digits */
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100953 if (!indio_dev->info->write_raw)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100954 return -EINVAL;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100955
956 if (indio_dev->info->write_raw_get_fmt)
957 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
958 this_attr->c, this_attr->address)) {
Sean Nyekjaerf47dff32015-11-09 13:55:34 +0100959 case IIO_VAL_INT:
960 fract_mult = 0;
961 break;
Beniamin Biab8528222020-02-06 17:11:45 +0200962 case IIO_VAL_INT_PLUS_MICRO_DB:
963 scale_db = true;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500964 fallthrough;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100965 case IIO_VAL_INT_PLUS_MICRO:
966 fract_mult = 100000;
967 break;
968 case IIO_VAL_INT_PLUS_NANO:
969 fract_mult = 100000000;
970 break;
Andrea Merello8cb34032019-11-20 15:47:51 +0100971 case IIO_VAL_CHAR:
972 is_char = true;
973 break;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100974 default:
975 return -EINVAL;
976 }
977
Andrea Merello8cb34032019-11-20 15:47:51 +0100978 if (is_char) {
979 char ch;
980
981 if (sscanf(buf, "%c", &ch) != 1)
982 return -EINVAL;
983 integer = ch;
984 } else {
Mircea Caprioru5403f282020-04-01 14:22:30 +0300985 ret = __iio_str_to_fixpoint(buf, fract_mult, &integer, &fract,
986 scale_db);
Andrea Merello8cb34032019-11-20 15:47:51 +0100987 if (ret)
988 return ret;
989 }
Jonathan Cameron847ec802009-08-18 18:06:19 +0100990
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100991 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
Michael Hennerich5c04af02011-06-27 13:07:10 +0100992 integer, fract, this_attr->address);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100993 if (ret)
994 return ret;
995
996 return len;
997}
998
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100999static
Jonathan Cameron1d892712011-05-18 14:40:51 +01001000int __iio_device_attr_init(struct device_attribute *dev_attr,
1001 const char *postfix,
1002 struct iio_chan_spec const *chan,
1003 ssize_t (*readfunc)(struct device *dev,
1004 struct device_attribute *attr,
1005 char *buf),
1006 ssize_t (*writefunc)(struct device *dev,
1007 struct device_attribute *attr,
1008 const char *buf,
1009 size_t len),
Jonathan Cameron37044322013-09-08 14:57:00 +01001010 enum iio_shared_by shared_by)
Jonathan Cameron1d892712011-05-18 14:40:51 +01001011{
Jonathan Cameron37044322013-09-08 14:57:00 +01001012 int ret = 0;
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001013 char *name = NULL;
Jonathan Cameron37044322013-09-08 14:57:00 +01001014 char *full_postfix;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001015 sysfs_attr_init(&dev_attr->attr);
Jonathan Cameronade7ef72011-09-02 17:14:45 +01001016
1017 /* Build up postfix of <extend_name>_<modifier>_postfix */
Jonathan Cameron37044322013-09-08 14:57:00 +01001018 if (chan->modified && (shared_by == IIO_SEPARATE)) {
Jonathan Cameronade7ef72011-09-02 17:14:45 +01001019 if (chan->extend_name)
1020 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
1021 iio_modifier_names[chan
1022 ->channel2],
1023 chan->extend_name,
1024 postfix);
1025 else
1026 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
1027 iio_modifier_names[chan
1028 ->channel2],
1029 postfix);
1030 } else {
Lars-Peter Clausen77bfa8b2014-02-14 14:19:00 +00001031 if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
Jonathan Cameronade7ef72011-09-02 17:14:45 +01001032 full_postfix = kstrdup(postfix, GFP_KERNEL);
1033 else
1034 full_postfix = kasprintf(GFP_KERNEL,
1035 "%s_%s",
1036 chan->extend_name,
1037 postfix);
1038 }
Jonathan Cameron37044322013-09-08 14:57:00 +01001039 if (full_postfix == NULL)
1040 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001041
Justin P. Mattock4abf6f82012-02-29 22:00:38 -08001042 if (chan->differential) { /* Differential can not have modifier */
Jonathan Cameron37044322013-09-08 14:57:00 +01001043 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +01001044 case IIO_SHARED_BY_ALL:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001045 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
Jonathan Cameronc006ec82013-09-08 14:57:00 +01001046 break;
1047 case IIO_SHARED_BY_DIR:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001048 name = kasprintf(GFP_KERNEL, "%s_%s",
Jonathan Cameronc006ec82013-09-08 14:57:00 +01001049 iio_direction[chan->output],
1050 full_postfix);
1051 break;
Jonathan Cameron37044322013-09-08 14:57:00 +01001052 case IIO_SHARED_BY_TYPE:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001053 name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +01001054 iio_direction[chan->output],
1055 iio_chan_type_name_spec[chan->type],
1056 iio_chan_type_name_spec[chan->type],
1057 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +01001058 break;
1059 case IIO_SEPARATE:
1060 if (!chan->indexed) {
Dan Carpenter231bfe52015-11-21 13:33:00 +03001061 WARN(1, "Differential channels must be indexed\n");
Jonathan Cameron37044322013-09-08 14:57:00 +01001062 ret = -EINVAL;
1063 goto error_free_full_postfix;
1064 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001065 name = kasprintf(GFP_KERNEL,
Jonathan Cameron37044322013-09-08 14:57:00 +01001066 "%s_%s%d-%s%d_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +01001067 iio_direction[chan->output],
1068 iio_chan_type_name_spec[chan->type],
1069 chan->channel,
1070 iio_chan_type_name_spec[chan->type],
1071 chan->channel2,
1072 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +01001073 break;
Jonathan Cameronade7ef72011-09-02 17:14:45 +01001074 }
1075 } else { /* Single ended */
Jonathan Cameron37044322013-09-08 14:57:00 +01001076 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +01001077 case IIO_SHARED_BY_ALL:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001078 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
Jonathan Cameronc006ec82013-09-08 14:57:00 +01001079 break;
1080 case IIO_SHARED_BY_DIR:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001081 name = kasprintf(GFP_KERNEL, "%s_%s",
Jonathan Cameronc006ec82013-09-08 14:57:00 +01001082 iio_direction[chan->output],
1083 full_postfix);
1084 break;
Jonathan Cameron37044322013-09-08 14:57:00 +01001085 case IIO_SHARED_BY_TYPE:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001086 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +01001087 iio_direction[chan->output],
1088 iio_chan_type_name_spec[chan->type],
1089 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +01001090 break;
1091
1092 case IIO_SEPARATE:
1093 if (chan->indexed)
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001094 name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
Jonathan Cameron37044322013-09-08 14:57:00 +01001095 iio_direction[chan->output],
1096 iio_chan_type_name_spec[chan->type],
1097 chan->channel,
1098 full_postfix);
1099 else
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001100 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
Jonathan Cameron37044322013-09-08 14:57:00 +01001101 iio_direction[chan->output],
1102 iio_chan_type_name_spec[chan->type],
1103 full_postfix);
1104 break;
1105 }
Jonathan Cameronade7ef72011-09-02 17:14:45 +01001106 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001107 if (name == NULL) {
Jonathan Cameron1d892712011-05-18 14:40:51 +01001108 ret = -ENOMEM;
1109 goto error_free_full_postfix;
1110 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001111 dev_attr->attr.name = name;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001112
1113 if (readfunc) {
1114 dev_attr->attr.mode |= S_IRUGO;
1115 dev_attr->show = readfunc;
1116 }
1117
1118 if (writefunc) {
1119 dev_attr->attr.mode |= S_IWUSR;
1120 dev_attr->store = writefunc;
1121 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +00001122
Jonathan Cameron1d892712011-05-18 14:40:51 +01001123error_free_full_postfix:
1124 kfree(full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +01001125
Jonathan Cameron847ec802009-08-18 18:06:19 +01001126 return ret;
1127}
1128
Jonathan Camerondf9c1c42011-08-12 17:56:03 +01001129static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
Jonathan Cameron1d892712011-05-18 14:40:51 +01001130{
1131 kfree(dev_attr->attr.name);
1132}
1133
1134int __iio_add_chan_devattr(const char *postfix,
Jonathan Cameron1d892712011-05-18 14:40:51 +01001135 struct iio_chan_spec const *chan,
1136 ssize_t (*readfunc)(struct device *dev,
1137 struct device_attribute *attr,
1138 char *buf),
1139 ssize_t (*writefunc)(struct device *dev,
1140 struct device_attribute *attr,
1141 const char *buf,
1142 size_t len),
Jonathan Camerone614a542011-09-02 17:14:41 +01001143 u64 mask,
Jonathan Cameron37044322013-09-08 14:57:00 +01001144 enum iio_shared_by shared_by,
Jonathan Cameron1d892712011-05-18 14:40:51 +01001145 struct device *dev,
Alexandru Ardelean3e3d11b2021-02-15 12:40:32 +02001146 struct iio_buffer *buffer,
Jonathan Cameron1d892712011-05-18 14:40:51 +01001147 struct list_head *attr_list)
1148{
1149 int ret;
1150 struct iio_dev_attr *iio_attr, *t;
1151
Sachin Kamat670c1102013-10-24 12:53:00 +01001152 iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
Hartmut Knaack92825ff2014-02-16 11:53:00 +00001153 if (iio_attr == NULL)
1154 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001155 ret = __iio_device_attr_init(&iio_attr->dev_attr,
1156 postfix, chan,
Jonathan Cameron37044322013-09-08 14:57:00 +01001157 readfunc, writefunc, shared_by);
Jonathan Cameron1d892712011-05-18 14:40:51 +01001158 if (ret)
1159 goto error_iio_dev_attr_free;
1160 iio_attr->c = chan;
1161 iio_attr->address = mask;
Alexandru Ardelean3e3d11b2021-02-15 12:40:32 +02001162 iio_attr->buffer = buffer;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001163 list_for_each_entry(t, attr_list, l)
1164 if (strcmp(t->dev_attr.attr.name,
1165 iio_attr->dev_attr.attr.name) == 0) {
Jonathan Cameron37044322013-09-08 14:57:00 +01001166 if (shared_by == IIO_SEPARATE)
Jonathan Cameron1d892712011-05-18 14:40:51 +01001167 dev_err(dev, "tried to double register : %s\n",
1168 t->dev_attr.attr.name);
1169 ret = -EBUSY;
1170 goto error_device_attr_deinit;
1171 }
Jonathan Cameron1d892712011-05-18 14:40:51 +01001172 list_add(&iio_attr->l, attr_list);
1173
1174 return 0;
1175
1176error_device_attr_deinit:
1177 __iio_device_attr_deinit(&iio_attr->dev_attr);
1178error_iio_dev_attr_free:
1179 kfree(iio_attr);
Jonathan Cameron1d892712011-05-18 14:40:51 +01001180 return ret;
1181}
1182
Cristian Pop1d4ef9b2020-09-28 12:09:55 +03001183static int iio_device_add_channel_label(struct iio_dev *indio_dev,
1184 struct iio_chan_spec const *chan)
1185{
1186 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1187 int ret;
1188
Paul Cercueil13efdc32021-06-18 13:30:05 +01001189 if (!indio_dev->info->read_label && !chan->extend_name)
Cristian Pop1d4ef9b2020-09-28 12:09:55 +03001190 return 0;
1191
1192 ret = __iio_add_chan_devattr("label",
1193 chan,
1194 &iio_read_channel_label,
1195 NULL,
1196 0,
1197 IIO_SEPARATE,
1198 &indio_dev->dev,
Alexandru Ardelean3e3d11b2021-02-15 12:40:32 +02001199 NULL,
Cristian Pop1d4ef9b2020-09-28 12:09:55 +03001200 &iio_dev_opaque->channel_attr_list);
1201 if (ret < 0)
1202 return ret;
1203
1204 return 1;
1205}
1206
Jonathan Cameron37044322013-09-08 14:57:00 +01001207static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
1208 struct iio_chan_spec const *chan,
1209 enum iio_shared_by shared_by,
1210 const long *infomask)
1211{
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001212 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Jonathan Cameron37044322013-09-08 14:57:00 +01001213 int i, ret, attrcount = 0;
1214
Orson Zhai1016d562017-04-25 09:16:56 +08001215 for_each_set_bit(i, infomask, sizeof(*infomask)*8) {
Jonathan Cameronef4b4852014-01-03 22:24:00 +00001216 if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1217 return -EINVAL;
Jonathan Cameron37044322013-09-08 14:57:00 +01001218 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
1219 chan,
1220 &iio_read_channel_info,
1221 &iio_write_channel_info,
1222 i,
1223 shared_by,
1224 &indio_dev->dev,
Alexandru Ardelean3e3d11b2021-02-15 12:40:32 +02001225 NULL,
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001226 &iio_dev_opaque->channel_attr_list);
Jonathan Cameron37044322013-09-08 14:57:00 +01001227 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
1228 continue;
1229 else if (ret < 0)
1230 return ret;
1231 attrcount++;
1232 }
1233
1234 return attrcount;
1235}
1236
Jonathan Cameron51239602016-11-08 12:58:51 +01001237static int iio_device_add_info_mask_type_avail(struct iio_dev *indio_dev,
1238 struct iio_chan_spec const *chan,
1239 enum iio_shared_by shared_by,
1240 const long *infomask)
1241{
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001242 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Jonathan Cameron51239602016-11-08 12:58:51 +01001243 int i, ret, attrcount = 0;
1244 char *avail_postfix;
1245
Orson Zhai1016d562017-04-25 09:16:56 +08001246 for_each_set_bit(i, infomask, sizeof(*infomask) * 8) {
Young Xiao936d3e52019-06-04 20:40:00 +08001247 if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1248 return -EINVAL;
Jonathan Cameron51239602016-11-08 12:58:51 +01001249 avail_postfix = kasprintf(GFP_KERNEL,
1250 "%s_available",
1251 iio_chan_info_postfix[i]);
1252 if (!avail_postfix)
1253 return -ENOMEM;
1254
1255 ret = __iio_add_chan_devattr(avail_postfix,
1256 chan,
1257 &iio_read_channel_info_avail,
1258 NULL,
1259 i,
1260 shared_by,
1261 &indio_dev->dev,
Alexandru Ardelean3e3d11b2021-02-15 12:40:32 +02001262 NULL,
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001263 &iio_dev_opaque->channel_attr_list);
Jonathan Cameron51239602016-11-08 12:58:51 +01001264 kfree(avail_postfix);
1265 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
1266 continue;
1267 else if (ret < 0)
1268 return ret;
1269 attrcount++;
1270 }
1271
1272 return attrcount;
1273}
1274
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001275static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
Jonathan Cameron1d892712011-05-18 14:40:51 +01001276 struct iio_chan_spec const *chan)
1277{
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001278 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Jonathan Cameron5ccb3ad2012-04-15 17:41:16 +01001279 int ret, attrcount = 0;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +01001280 const struct iio_chan_spec_ext_info *ext_info;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001281
Jonathan Cameron1d892712011-05-18 14:40:51 +01001282 if (chan->channel < 0)
1283 return 0;
Jonathan Cameron37044322013-09-08 14:57:00 +01001284 ret = iio_device_add_info_mask_type(indio_dev, chan,
1285 IIO_SEPARATE,
1286 &chan->info_mask_separate);
1287 if (ret < 0)
1288 return ret;
1289 attrcount += ret;
1290
Jonathan Cameron51239602016-11-08 12:58:51 +01001291 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1292 IIO_SEPARATE,
1293 &chan->
1294 info_mask_separate_available);
1295 if (ret < 0)
1296 return ret;
1297 attrcount += ret;
1298
Jonathan Cameron37044322013-09-08 14:57:00 +01001299 ret = iio_device_add_info_mask_type(indio_dev, chan,
1300 IIO_SHARED_BY_TYPE,
1301 &chan->info_mask_shared_by_type);
1302 if (ret < 0)
1303 return ret;
1304 attrcount += ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +01001305
Jonathan Cameron51239602016-11-08 12:58:51 +01001306 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1307 IIO_SHARED_BY_TYPE,
1308 &chan->
1309 info_mask_shared_by_type_available);
1310 if (ret < 0)
1311 return ret;
1312 attrcount += ret;
1313
Jonathan Cameronc006ec82013-09-08 14:57:00 +01001314 ret = iio_device_add_info_mask_type(indio_dev, chan,
1315 IIO_SHARED_BY_DIR,
1316 &chan->info_mask_shared_by_dir);
1317 if (ret < 0)
1318 return ret;
1319 attrcount += ret;
1320
Jonathan Cameron51239602016-11-08 12:58:51 +01001321 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1322 IIO_SHARED_BY_DIR,
1323 &chan->info_mask_shared_by_dir_available);
1324 if (ret < 0)
1325 return ret;
1326 attrcount += ret;
1327
Jonathan Cameronc006ec82013-09-08 14:57:00 +01001328 ret = iio_device_add_info_mask_type(indio_dev, chan,
1329 IIO_SHARED_BY_ALL,
1330 &chan->info_mask_shared_by_all);
1331 if (ret < 0)
1332 return ret;
1333 attrcount += ret;
1334
Jonathan Cameron51239602016-11-08 12:58:51 +01001335 ret = iio_device_add_info_mask_type_avail(indio_dev, chan,
1336 IIO_SHARED_BY_ALL,
1337 &chan->info_mask_shared_by_all_available);
1338 if (ret < 0)
1339 return ret;
1340 attrcount += ret;
1341
Cristian Pop1d4ef9b2020-09-28 12:09:55 +03001342 ret = iio_device_add_channel_label(indio_dev, chan);
1343 if (ret < 0)
1344 return ret;
1345 attrcount += ret;
1346
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +01001347 if (chan->ext_info) {
1348 unsigned int i = 0;
1349 for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
1350 ret = __iio_add_chan_devattr(ext_info->name,
1351 chan,
1352 ext_info->read ?
1353 &iio_read_channel_ext_info : NULL,
1354 ext_info->write ?
1355 &iio_write_channel_ext_info : NULL,
1356 i,
1357 ext_info->shared,
1358 &indio_dev->dev,
Alexandru Ardelean3e3d11b2021-02-15 12:40:32 +02001359 NULL,
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001360 &iio_dev_opaque->channel_attr_list);
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +01001361 i++;
1362 if (ret == -EBUSY && ext_info->shared)
1363 continue;
1364
1365 if (ret)
Jonathan Cameron37044322013-09-08 14:57:00 +01001366 return ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +01001367
1368 attrcount++;
1369 }
1370 }
1371
Jonathan Cameron37044322013-09-08 14:57:00 +01001372 return attrcount;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001373}
1374
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +01001375/**
1376 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
1377 * @attr_list: List of IIO device attributes
1378 *
1379 * This function frees the memory allocated for each of the IIO device
Martin Fuzzeyc1b03ab2015-02-19 15:17:44 +01001380 * attributes in the list.
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +01001381 */
1382void iio_free_chan_devattr_list(struct list_head *attr_list)
Jonathan Cameron1d892712011-05-18 14:40:51 +01001383{
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +01001384 struct iio_dev_attr *p, *n;
1385
1386 list_for_each_entry_safe(p, n, attr_list, l) {
Alexandru Ardelean15097c72021-02-15 12:40:33 +02001387 kfree_const(p->dev_attr.attr.name);
Martin Fuzzeyc1b03ab2015-02-19 15:17:44 +01001388 list_del(&p->l);
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +01001389 kfree(p);
1390 }
Jonathan Cameron1d892712011-05-18 14:40:51 +01001391}
1392
Jonathan Cameron1b732882011-05-18 14:41:43 +01001393static ssize_t iio_show_dev_name(struct device *dev,
1394 struct device_attribute *attr,
1395 char *buf)
1396{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +02001397 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen83ca56b2021-03-20 08:14:02 +01001398 return sysfs_emit(buf, "%s\n", indio_dev->name);
Jonathan Cameron1b732882011-05-18 14:41:43 +01001399}
1400
1401static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
1402
Phil Reid2c3d0c92019-09-19 22:36:08 +08001403static ssize_t iio_show_dev_label(struct device *dev,
1404 struct device_attribute *attr,
1405 char *buf)
1406{
1407 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen83ca56b2021-03-20 08:14:02 +01001408 return sysfs_emit(buf, "%s\n", indio_dev->label);
Phil Reid2c3d0c92019-09-19 22:36:08 +08001409}
1410
1411static DEVICE_ATTR(label, S_IRUGO, iio_show_dev_label, NULL);
1412
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001413static ssize_t iio_show_timestamp_clock(struct device *dev,
1414 struct device_attribute *attr,
1415 char *buf)
1416{
1417 const struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1418 const clockid_t clk = iio_device_get_clock(indio_dev);
1419 const char *name;
1420 ssize_t sz;
1421
1422 switch (clk) {
1423 case CLOCK_REALTIME:
1424 name = "realtime\n";
1425 sz = sizeof("realtime\n");
1426 break;
1427 case CLOCK_MONOTONIC:
1428 name = "monotonic\n";
1429 sz = sizeof("monotonic\n");
1430 break;
1431 case CLOCK_MONOTONIC_RAW:
1432 name = "monotonic_raw\n";
1433 sz = sizeof("monotonic_raw\n");
1434 break;
1435 case CLOCK_REALTIME_COARSE:
1436 name = "realtime_coarse\n";
1437 sz = sizeof("realtime_coarse\n");
1438 break;
1439 case CLOCK_MONOTONIC_COARSE:
1440 name = "monotonic_coarse\n";
1441 sz = sizeof("monotonic_coarse\n");
1442 break;
1443 case CLOCK_BOOTTIME:
1444 name = "boottime\n";
1445 sz = sizeof("boottime\n");
1446 break;
1447 case CLOCK_TAI:
1448 name = "tai\n";
1449 sz = sizeof("tai\n");
1450 break;
1451 default:
1452 BUG();
1453 }
1454
1455 memcpy(buf, name, sz);
1456 return sz;
1457}
1458
1459static ssize_t iio_store_timestamp_clock(struct device *dev,
1460 struct device_attribute *attr,
1461 const char *buf, size_t len)
1462{
1463 clockid_t clk;
1464 int ret;
1465
1466 if (sysfs_streq(buf, "realtime"))
1467 clk = CLOCK_REALTIME;
1468 else if (sysfs_streq(buf, "monotonic"))
1469 clk = CLOCK_MONOTONIC;
1470 else if (sysfs_streq(buf, "monotonic_raw"))
1471 clk = CLOCK_MONOTONIC_RAW;
1472 else if (sysfs_streq(buf, "realtime_coarse"))
1473 clk = CLOCK_REALTIME_COARSE;
1474 else if (sysfs_streq(buf, "monotonic_coarse"))
1475 clk = CLOCK_MONOTONIC_COARSE;
1476 else if (sysfs_streq(buf, "boottime"))
1477 clk = CLOCK_BOOTTIME;
1478 else if (sysfs_streq(buf, "tai"))
1479 clk = CLOCK_TAI;
1480 else
1481 return -EINVAL;
1482
1483 ret = iio_device_set_clock(dev_to_iio_dev(dev), clk);
1484 if (ret)
1485 return ret;
1486
1487 return len;
1488}
1489
Alexandru Ardelean32f17172021-02-15 12:40:29 +02001490int iio_device_register_sysfs_group(struct iio_dev *indio_dev,
1491 const struct attribute_group *group)
1492{
1493 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1494 const struct attribute_group **new, **old = iio_dev_opaque->groups;
1495 unsigned int cnt = iio_dev_opaque->groupcounter;
1496
1497 new = krealloc(old, sizeof(*new) * (cnt + 2), GFP_KERNEL);
1498 if (!new)
1499 return -ENOMEM;
1500
1501 new[iio_dev_opaque->groupcounter++] = group;
1502 new[iio_dev_opaque->groupcounter] = NULL;
1503
1504 iio_dev_opaque->groups = new;
1505
1506 return 0;
1507}
1508
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001509static DEVICE_ATTR(current_timestamp_clock, S_IRUGO | S_IWUSR,
1510 iio_show_timestamp_clock, iio_store_timestamp_clock);
1511
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001512static int iio_device_register_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron1d892712011-05-18 14:40:51 +01001513{
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001514 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001515 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +01001516 struct iio_dev_attr *p;
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001517 struct attribute **attr, *clk = NULL;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001518
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001519 /* First count elements in any existing group */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001520 if (indio_dev->info->attrs) {
1521 attr = indio_dev->info->attrs->attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001522 while (*attr++ != NULL)
1523 attrcount_orig++;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001524 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001525 attrcount = attrcount_orig;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001526 /*
1527 * New channel registration method - relies on the fact a group does
Peter Meerwaldd25b3802012-08-26 13:43:00 +01001528 * not need to be initialized if its name is NULL.
Jonathan Cameron1d892712011-05-18 14:40:51 +01001529 */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001530 if (indio_dev->channels)
1531 for (i = 0; i < indio_dev->num_channels; i++) {
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001532 const struct iio_chan_spec *chan =
1533 &indio_dev->channels[i];
1534
1535 if (chan->type == IIO_TIMESTAMP)
1536 clk = &dev_attr_current_timestamp_clock.attr;
1537
1538 ret = iio_device_add_channel_sysfs(indio_dev, chan);
Jonathan Cameron1d892712011-05-18 14:40:51 +01001539 if (ret < 0)
1540 goto error_clear_attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001541 attrcount += ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001542 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001543
Alexandru Ardeleanfa83c3b2020-06-30 07:57:08 +03001544 if (iio_dev_opaque->event_interface)
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001545 clk = &dev_attr_current_timestamp_clock.attr;
1546
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001547 if (indio_dev->name)
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001548 attrcount++;
Phil Reid2c3d0c92019-09-19 22:36:08 +08001549 if (indio_dev->label)
1550 attrcount++;
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001551 if (clk)
1552 attrcount++;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001553
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001554 iio_dev_opaque->chan_attr_group.attrs =
1555 kcalloc(attrcount + 1,
1556 sizeof(iio_dev_opaque->chan_attr_group.attrs[0]),
1557 GFP_KERNEL);
1558 if (iio_dev_opaque->chan_attr_group.attrs == NULL) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001559 ret = -ENOMEM;
1560 goto error_clear_attrs;
Jonathan Cameron1b732882011-05-18 14:41:43 +01001561 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001562 /* Copy across original attributes */
Hans de Goede18b4c9c2020-11-25 09:46:06 +01001563 if (indio_dev->info->attrs) {
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001564 memcpy(iio_dev_opaque->chan_attr_group.attrs,
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001565 indio_dev->info->attrs->attrs,
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001566 sizeof(iio_dev_opaque->chan_attr_group.attrs[0])
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001567 *attrcount_orig);
Hans de Goede18b4c9c2020-11-25 09:46:06 +01001568 iio_dev_opaque->chan_attr_group.is_visible =
1569 indio_dev->info->attrs->is_visible;
1570 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001571 attrn = attrcount_orig;
1572 /* Add all elements from the list. */
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001573 list_for_each_entry(p, &iio_dev_opaque->channel_attr_list, l)
1574 iio_dev_opaque->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001575 if (indio_dev->name)
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001576 iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
Phil Reid2c3d0c92019-09-19 22:36:08 +08001577 if (indio_dev->label)
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001578 iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_label.attr;
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001579 if (clk)
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001580 iio_dev_opaque->chan_attr_group.attrs[attrn++] = clk;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001581
Alexandru Ardelean32f17172021-02-15 12:40:29 +02001582 ret = iio_device_register_sysfs_group(indio_dev,
1583 &iio_dev_opaque->chan_attr_group);
1584 if (ret)
1585 goto error_clear_attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001586
Jonathan Cameron1d892712011-05-18 14:40:51 +01001587 return 0;
Jonathan Cameron1b732882011-05-18 14:41:43 +01001588
Jonathan Cameron1d892712011-05-18 14:40:51 +01001589error_clear_attrs:
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001590 iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list);
Jonathan Cameron1d892712011-05-18 14:40:51 +01001591
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001592 return ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001593}
1594
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001595static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001596{
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001597 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Jonathan Cameron1d892712011-05-18 14:40:51 +01001598
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001599 iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list);
1600 kfree(iio_dev_opaque->chan_attr_group.attrs);
1601 iio_dev_opaque->chan_attr_group.attrs = NULL;
Alexandru Ardelean32f17172021-02-15 12:40:29 +02001602 kfree(iio_dev_opaque->groups);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001603}
1604
Jonathan Cameron847ec802009-08-18 18:06:19 +01001605static void iio_dev_release(struct device *device)
1606{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +02001607 struct iio_dev *indio_dev = dev_to_iio_dev(device);
Alexandru Ardelean6d4ebd52020-06-30 07:57:03 +03001608 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1609
Benjamin Gaignardd89e1192017-04-27 15:29:15 +02001610 if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001611 iio_device_unregister_trigger_consumer(indio_dev);
1612 iio_device_unregister_eventset(indio_dev);
1613 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +02001614
Alexandru Ardelean218bc532021-03-07 20:54:44 +02001615 iio_device_detach_buffers(indio_dev);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +01001616
Jonathan Cameron15ea28782021-04-26 18:49:03 +01001617 ida_simple_remove(&iio_ida, iio_dev_opaque->id);
Alexandru Ardelean6d4ebd52020-06-30 07:57:03 +03001618 kfree(iio_dev_opaque);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001619}
1620
Guenter Roeck17d82b42013-02-07 17:09:00 +00001621struct device_type iio_device_type = {
Jonathan Cameron847ec802009-08-18 18:06:19 +01001622 .name = "iio_device",
1623 .release = iio_dev_release,
1624};
1625
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001626/**
1627 * iio_device_alloc() - allocate an iio_dev from a driver
Jonathan Cameronc9561fd2020-09-13 14:21:15 +01001628 * @parent: Parent device.
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001629 * @sizeof_priv: Space to allocate for private structure.
1630 **/
Alexandru Ardelean78289b42020-06-03 14:40:18 +03001631struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001632{
Alexandru Ardelean6d4ebd52020-06-30 07:57:03 +03001633 struct iio_dev_opaque *iio_dev_opaque;
Alexandru Ardelean0d596bb2021-02-15 12:40:40 +02001634 struct iio_dev *indio_dev;
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +01001635 size_t alloc_size;
1636
Alexandru Ardelean6d4ebd52020-06-30 07:57:03 +03001637 alloc_size = sizeof(struct iio_dev_opaque);
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +01001638 if (sizeof_priv) {
1639 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
1640 alloc_size += sizeof_priv;
1641 }
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +01001642
Alexandru Ardelean6d4ebd52020-06-30 07:57:03 +03001643 iio_dev_opaque = kzalloc(alloc_size, GFP_KERNEL);
1644 if (!iio_dev_opaque)
Jonathan Cameron0d4b2182020-04-19 16:13:37 +01001645 return NULL;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001646
Alexandru Ardelean0d596bb2021-02-15 12:40:40 +02001647 indio_dev = &iio_dev_opaque->indio_dev;
1648 indio_dev->priv = (char *)iio_dev_opaque +
Alexandru Ardelean6d4ebd52020-06-30 07:57:03 +03001649 ALIGN(sizeof(struct iio_dev_opaque), IIO_ALIGN);
1650
Alexandru Ardelean0d596bb2021-02-15 12:40:40 +02001651 indio_dev->dev.parent = parent;
1652 indio_dev->dev.type = &iio_device_type;
1653 indio_dev->dev.bus = &iio_bus_type;
1654 device_initialize(&indio_dev->dev);
Julia Lawallb624fd12021-02-09 22:13:15 +01001655 iio_device_set_drvdata(indio_dev, (void *)indio_dev);
Alexandru Ardelean0d596bb2021-02-15 12:40:40 +02001656 mutex_init(&indio_dev->mlock);
Jonathan Cameronb804e2b2021-04-26 18:49:08 +01001657 mutex_init(&iio_dev_opaque->info_exist_lock);
Alexandru Ardelean207c2d22020-06-30 07:57:06 +03001658 INIT_LIST_HEAD(&iio_dev_opaque->channel_attr_list);
Jonathan Camerona9e39f92011-09-14 13:01:25 +01001659
Jonathan Cameron15ea28782021-04-26 18:49:03 +01001660 iio_dev_opaque->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
1661 if (iio_dev_opaque->id < 0) {
Jonathan Cameron0d4b2182020-04-19 16:13:37 +01001662 /* cannot use a dev_err as the name isn't available */
1663 pr_err("failed to get device id\n");
Alexandru Ardelean6d4ebd52020-06-30 07:57:03 +03001664 kfree(iio_dev_opaque);
Jonathan Cameron0d4b2182020-04-19 16:13:37 +01001665 return NULL;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001666 }
Yang Yingliangfe6f45f2021-10-12 14:36:24 +08001667
1668 if (dev_set_name(&indio_dev->dev, "iio:device%d", iio_dev_opaque->id)) {
1669 ida_simple_remove(&iio_ida, iio_dev_opaque->id);
1670 kfree(iio_dev_opaque);
1671 return NULL;
1672 }
1673
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +03001674 INIT_LIST_HEAD(&iio_dev_opaque->buffer_list);
Alexandru Ardelean8dedcc32020-09-24 11:41:55 +03001675 INIT_LIST_HEAD(&iio_dev_opaque->ioctl_handlers);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001676
Alexandru Ardelean0d596bb2021-02-15 12:40:40 +02001677 return indio_dev;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001678}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001679EXPORT_SYMBOL(iio_device_alloc);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001680
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001681/**
1682 * iio_device_free() - free an iio_dev from a driver
1683 * @dev: the iio_dev associated with the device
1684 **/
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001685void iio_device_free(struct iio_dev *dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001686{
Lars-Peter Clausene407fd62012-06-04 10:41:42 +02001687 if (dev)
1688 put_device(&dev->dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001689}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001690EXPORT_SYMBOL(iio_device_free);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001691
Yicong Yangcf5724e2021-04-08 19:38:14 +08001692static void devm_iio_device_release(void *iio_dev)
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001693{
Yicong Yangcf5724e2021-04-08 19:38:14 +08001694 iio_device_free(iio_dev);
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001695}
1696
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001697/**
1698 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
Alexandru Ardelean78289b42020-06-03 14:40:18 +03001699 * @parent: Device to allocate iio_dev for, and parent for this IIO device
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001700 * @sizeof_priv: Space to allocate for private structure.
1701 *
1702 * Managed iio_device_alloc. iio_dev allocated with this function is
1703 * automatically freed on driver detach.
1704 *
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001705 * RETURNS:
1706 * Pointer to allocated iio_dev on success, NULL on failure.
1707 */
Alexandru Ardelean78289b42020-06-03 14:40:18 +03001708struct iio_dev *devm_iio_device_alloc(struct device *parent, int sizeof_priv)
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001709{
Yicong Yangcf5724e2021-04-08 19:38:14 +08001710 struct iio_dev *iio_dev;
1711 int ret;
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001712
Alexandru Ardelean78289b42020-06-03 14:40:18 +03001713 iio_dev = iio_device_alloc(parent, sizeof_priv);
Yicong Yangcf5724e2021-04-08 19:38:14 +08001714 if (!iio_dev)
1715 return NULL;
1716
1717 ret = devm_add_action_or_reset(parent, devm_iio_device_release,
1718 iio_dev);
1719 if (ret)
Dan Carpenter43fa7392021-05-15 12:56:39 +03001720 return NULL;
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001721
1722 return iio_dev;
1723}
1724EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
1725
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001726/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001727 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
Cristina Opriceana2498dcf2015-07-24 16:16:19 +03001728 * @inode: Inode structure for identifying the device in the file system
1729 * @filp: File structure for iio device used to keep and later access
1730 * private data
1731 *
1732 * Return: 0 on success or -EBUSY if the device is already opened
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001733 **/
1734static int iio_chrdev_open(struct inode *inode, struct file *filp)
1735{
Jonathan Cameron396f7232021-04-26 18:49:09 +01001736 struct iio_dev_opaque *iio_dev_opaque =
1737 container_of(inode->i_cdev, struct iio_dev_opaque, chrdev);
1738 struct iio_dev *indio_dev = &iio_dev_opaque->indio_dev;
Alexandru Ardeleanbe24dcb2021-02-15 12:40:35 +02001739 struct iio_dev_buffer_pair *ib;
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001740
Jonathan Cameron8b1c82c2021-04-26 18:49:10 +01001741 if (test_and_set_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags))
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001742 return -EBUSY;
1743
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001744 iio_device_get(indio_dev);
1745
Alexandru Ardeleanbe24dcb2021-02-15 12:40:35 +02001746 ib = kmalloc(sizeof(*ib), GFP_KERNEL);
1747 if (!ib) {
1748 iio_device_put(indio_dev);
Jonathan Cameron8b1c82c2021-04-26 18:49:10 +01001749 clear_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags);
Alexandru Ardeleanbe24dcb2021-02-15 12:40:35 +02001750 return -ENOMEM;
1751 }
1752
1753 ib->indio_dev = indio_dev;
1754 ib->buffer = indio_dev->buffer;
1755
1756 filp->private_data = ib;
Jonathan Cameron30eb82f2011-09-21 11:16:02 +01001757
Lars-Peter Clausen79335142011-12-19 15:23:49 +01001758 return 0;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001759}
1760
1761/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001762 * iio_chrdev_release() - chrdev file close buffer access and ioctls
Cristina Opriceana2498dcf2015-07-24 16:16:19 +03001763 * @inode: Inode structure pointer for the char device
1764 * @filp: File structure pointer for the char device
1765 *
1766 * Return: 0 for successful release
1767 */
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001768static int iio_chrdev_release(struct inode *inode, struct file *filp)
1769{
Alexandru Ardeleanbe24dcb2021-02-15 12:40:35 +02001770 struct iio_dev_buffer_pair *ib = filp->private_data;
Jonathan Cameron396f7232021-04-26 18:49:09 +01001771 struct iio_dev_opaque *iio_dev_opaque =
1772 container_of(inode->i_cdev, struct iio_dev_opaque, chrdev);
1773 struct iio_dev *indio_dev = &iio_dev_opaque->indio_dev;
Alexandru Ardeleanbe24dcb2021-02-15 12:40:35 +02001774 kfree(ib);
Jonathan Cameron8b1c82c2021-04-26 18:49:10 +01001775 clear_bit(IIO_BUSY_BIT_POS, &iio_dev_opaque->flags);
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001776 iio_device_put(indio_dev);
1777
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001778 return 0;
1779}
1780
Alexandru Ardelean8dedcc32020-09-24 11:41:55 +03001781void iio_device_ioctl_handler_register(struct iio_dev *indio_dev,
1782 struct iio_ioctl_handler *h)
1783{
1784 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1785
1786 list_add_tail(&h->entry, &iio_dev_opaque->ioctl_handlers);
1787}
1788
1789void iio_device_ioctl_handler_unregister(struct iio_ioctl_handler *h)
1790{
1791 list_del(&h->entry);
1792}
1793
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001794static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1795{
Alexandru Ardeleanbe24dcb2021-02-15 12:40:35 +02001796 struct iio_dev_buffer_pair *ib = filp->private_data;
1797 struct iio_dev *indio_dev = ib->indio_dev;
Alexandru Ardelean8dedcc32020-09-24 11:41:55 +03001798 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1799 struct iio_ioctl_handler *h;
1800 int ret = -ENODEV;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001801
Jonathan Cameronb804e2b2021-04-26 18:49:08 +01001802 mutex_lock(&iio_dev_opaque->info_exist_lock);
Alexandru Ardelean8dedcc32020-09-24 11:41:55 +03001803
1804 /**
1805 * The NULL check here is required to prevent crashing when a device
1806 * is being removed while userspace would still have open file handles
1807 * to try to access this device.
1808 */
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +01001809 if (!indio_dev->info)
Alexandru Ardelean8dedcc32020-09-24 11:41:55 +03001810 goto out_unlock;
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +01001811
Alexandru Ardelean8dedcc32020-09-24 11:41:55 +03001812 list_for_each_entry(h, &iio_dev_opaque->ioctl_handlers, entry) {
1813 ret = h->ioctl(indio_dev, filp, cmd, arg);
1814 if (ret != IIO_IOCTL_UNHANDLED)
1815 break;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001816 }
Alexandru Ardelean8dedcc32020-09-24 11:41:55 +03001817
Alexandru Ardelean74d826d2020-11-17 11:51:54 +02001818 if (ret == IIO_IOCTL_UNHANDLED)
Alexandru Ardeleanaf0670b2021-05-03 17:43:50 +03001819 ret = -ENODEV;
Alexandru Ardelean74d826d2020-11-17 11:51:54 +02001820
Alexandru Ardelean8dedcc32020-09-24 11:41:55 +03001821out_unlock:
Jonathan Cameronb804e2b2021-04-26 18:49:08 +01001822 mutex_unlock(&iio_dev_opaque->info_exist_lock);
Alexandru Ardelean8dedcc32020-09-24 11:41:55 +03001823
1824 return ret;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001825}
1826
Jonathan Cameron14555b12011-09-21 11:15:57 +01001827static const struct file_operations iio_buffer_fileops = {
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001828 .owner = THIS_MODULE,
1829 .llseek = noop_llseek,
Alexandru Ardeleanee8caea2020-11-17 12:37:53 +02001830 .read = iio_buffer_read_outer_addr,
1831 .poll = iio_buffer_poll_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001832 .unlocked_ioctl = iio_ioctl,
Arnd Bergmann1832f2d2018-09-11 21:59:08 +02001833 .compat_ioctl = compat_ptr_ioctl,
Alexandru Ardeleanee8caea2020-11-17 12:37:53 +02001834 .open = iio_chrdev_open,
1835 .release = iio_chrdev_release,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001836};
1837
Alexandru Ardelean8ebaa3f2021-02-15 12:40:27 +02001838static const struct file_operations iio_event_fileops = {
1839 .owner = THIS_MODULE,
1840 .llseek = noop_llseek,
1841 .unlocked_ioctl = iio_ioctl,
1842 .compat_ioctl = compat_ptr_ioctl,
1843 .open = iio_chrdev_open,
1844 .release = iio_chrdev_release,
1845};
1846
Vlad Dogaru8f5d8722014-12-29 11:37:48 +02001847static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
1848{
1849 int i, j;
1850 const struct iio_chan_spec *channels = indio_dev->channels;
1851
1852 if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
1853 return 0;
1854
1855 for (i = 0; i < indio_dev->num_channels - 1; i++) {
1856 if (channels[i].scan_index < 0)
1857 continue;
1858 for (j = i + 1; j < indio_dev->num_channels; j++)
1859 if (channels[i].scan_index == channels[j].scan_index) {
1860 dev_err(&indio_dev->dev,
1861 "Duplicate scan index %d\n",
1862 channels[i].scan_index);
1863 return -EINVAL;
1864 }
1865 }
1866
1867 return 0;
1868}
1869
Paul Cercueil0e0761f2021-06-18 13:30:04 +01001870static int iio_check_extended_name(const struct iio_dev *indio_dev)
1871{
1872 unsigned int i;
1873
1874 if (!indio_dev->info->read_label)
1875 return 0;
1876
1877 for (i = 0; i < indio_dev->num_channels; i++) {
1878 if (indio_dev->channels[i].extend_name) {
1879 dev_err(&indio_dev->dev,
1880 "Cannot use labels and extend_name at the same time\n");
1881 return -EINVAL;
1882 }
1883 }
1884
1885 return 0;
1886}
1887
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001888static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1889
Jonathan Cameron63b19542017-07-23 17:25:43 +01001890int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001891{
Alexandru Ardelean8ebaa3f2021-02-15 12:40:27 +02001892 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Hans de Goede6f71bf12021-02-07 17:08:59 +01001893 const char *label;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001894 int ret;
1895
Alexandru Ardeleana17cb782020-04-07 18:07:43 +03001896 if (!indio_dev->info)
1897 return -EINVAL;
1898
Jonathan Cameron6eaf9f62021-04-26 18:49:05 +01001899 iio_dev_opaque->driver_module = this_mod;
Guenter Roeck17d82b42013-02-07 17:09:00 +00001900 /* If the calling driver did not initialize of_node, do it here */
1901 if (!indio_dev->dev.of_node && indio_dev->dev.parent)
1902 indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
1903
Hans de Goede6f71bf12021-02-07 17:08:59 +01001904 label = of_get_property(indio_dev->dev.of_node, "label", NULL);
1905 if (label)
1906 indio_dev->label = label;
Phil Reid2c3d0c92019-09-19 22:36:08 +08001907
Vlad Dogaru8f5d8722014-12-29 11:37:48 +02001908 ret = iio_check_unique_scan_index(indio_dev);
1909 if (ret < 0)
1910 return ret;
1911
Paul Cercueil0e0761f2021-06-18 13:30:04 +01001912 ret = iio_check_extended_name(indio_dev);
1913 if (ret < 0)
1914 return ret;
1915
Greg Kroah-Hartman8915aac2019-06-18 17:54:40 +02001916 iio_device_register_debugfs(indio_dev);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001917
Alexandru Ardeleanee708e62021-02-15 12:40:38 +02001918 ret = iio_buffers_alloc_sysfs_and_mask(indio_dev);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001919 if (ret) {
1920 dev_err(indio_dev->dev.parent,
1921 "Failed to create buffer sysfs interfaces\n");
1922 goto error_unreg_debugfs;
1923 }
1924
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001925 ret = iio_device_register_sysfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001926 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001927 dev_err(indio_dev->dev.parent,
Jonathan Cameron847ec802009-08-18 18:06:19 +01001928 "Failed to register sysfs interfaces\n");
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001929 goto error_buffer_free_sysfs;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001930 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001931 ret = iio_device_register_eventset(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001932 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001933 dev_err(indio_dev->dev.parent,
Roel Van Nyenc849d252010-04-29 19:27:31 +02001934 "Failed to register event set\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001935 goto error_free_sysfs;
1936 }
Benjamin Gaignardd89e1192017-04-27 15:29:15 +02001937 if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001938 iio_device_register_trigger_consumer(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001939
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001940 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1941 indio_dev->setup_ops == NULL)
1942 indio_dev->setup_ops = &noop_ring_setup_ops;
1943
Alexandru Ardeleanee708e62021-02-15 12:40:38 +02001944 if (iio_dev_opaque->attached_buffers_cnt)
Jonathan Cameron396f7232021-04-26 18:49:09 +01001945 cdev_init(&iio_dev_opaque->chrdev, &iio_buffer_fileops);
Alexandru Ardelean8ebaa3f2021-02-15 12:40:27 +02001946 else if (iio_dev_opaque->event_interface)
Jonathan Cameron396f7232021-04-26 18:49:09 +01001947 cdev_init(&iio_dev_opaque->chrdev, &iio_event_fileops);
Jonathan Cameron63b19542017-07-23 17:25:43 +01001948
Alexandru Ardeleanee708e62021-02-15 12:40:38 +02001949 if (iio_dev_opaque->attached_buffers_cnt || iio_dev_opaque->event_interface) {
Jonathan Cameron15ea28782021-04-26 18:49:03 +01001950 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), iio_dev_opaque->id);
Jonathan Cameron396f7232021-04-26 18:49:09 +01001951 iio_dev_opaque->chrdev.owner = this_mod;
Alexandru Ardelean8ebaa3f2021-02-15 12:40:27 +02001952 }
Logan Gunthorpe389239112017-03-17 12:48:17 -06001953
Alexandru Ardelean32f17172021-02-15 12:40:29 +02001954 /* assign device groups now; they should be all registered now */
1955 indio_dev->dev.groups = iio_dev_opaque->groups;
1956
Jonathan Cameron396f7232021-04-26 18:49:09 +01001957 ret = cdev_device_add(&iio_dev_opaque->chrdev, &indio_dev->dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001958 if (ret < 0)
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001959 goto error_unreg_eventset;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001960
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001961 return 0;
Logan Gunthorpe389239112017-03-17 12:48:17 -06001962
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001963error_unreg_eventset:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001964 iio_device_unregister_eventset(indio_dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001965error_free_sysfs:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001966 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001967error_buffer_free_sysfs:
Alexandru Ardeleanee708e62021-02-15 12:40:38 +02001968 iio_buffers_free_sysfs_and_mask(indio_dev);
Michael Henneriche553f182012-03-01 10:51:03 +01001969error_unreg_debugfs:
1970 iio_device_unregister_debugfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001971 return ret;
1972}
Jonathan Cameron63b19542017-07-23 17:25:43 +01001973EXPORT_SYMBOL(__iio_device_register);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001974
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001975/**
1976 * iio_device_unregister() - unregister a device from the IIO subsystem
1977 * @indio_dev: Device structure representing the device.
1978 **/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001979void iio_device_unregister(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001980{
Jonathan Cameron396f7232021-04-26 18:49:09 +01001981 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1982
1983 cdev_device_del(&iio_dev_opaque->chrdev, &indio_dev->dev);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001984
Jonathan Cameronb804e2b2021-04-26 18:49:08 +01001985 mutex_lock(&iio_dev_opaque->info_exist_lock);
Fabrice Gasnier7f75591fc2019-03-25 14:01:23 +01001986
Lars-Peter Clausenbc4c9612013-09-21 16:21:00 +01001987 iio_device_unregister_debugfs(indio_dev);
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001988
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001989 iio_disable_all_buffers(indio_dev);
1990
Jonathan Cameronac917a82012-02-15 19:48:00 +00001991 indio_dev->info = NULL;
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +01001992
1993 iio_device_wakeup_eventset(indio_dev);
1994 iio_buffer_wakeup_poll(indio_dev);
1995
Jonathan Cameronb804e2b2021-04-26 18:49:08 +01001996 mutex_unlock(&iio_dev_opaque->info_exist_lock);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001997
Alexandru Ardeleanee708e62021-02-15 12:40:38 +02001998 iio_buffers_free_sysfs_and_mask(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001999}
2000EXPORT_SYMBOL(iio_device_unregister);
Sachin Kamat8caa07c2013-10-29 11:39:00 +00002001
Yicong Yangcf5724e2021-04-08 19:38:14 +08002002static void devm_iio_device_unreg(void *indio_dev)
Sachin Kamat8caa07c2013-10-29 11:39:00 +00002003{
Yicong Yangcf5724e2021-04-08 19:38:14 +08002004 iio_device_unregister(indio_dev);
Sachin Kamat8caa07c2013-10-29 11:39:00 +00002005}
2006
Jonathan Cameron63b19542017-07-23 17:25:43 +01002007int __devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev,
2008 struct module *this_mod)
Sachin Kamat8caa07c2013-10-29 11:39:00 +00002009{
Sachin Kamat8caa07c2013-10-29 11:39:00 +00002010 int ret;
2011
Jonathan Cameron63b19542017-07-23 17:25:43 +01002012 ret = __iio_device_register(indio_dev, this_mod);
Yicong Yangcf5724e2021-04-08 19:38:14 +08002013 if (ret)
2014 return ret;
Sachin Kamat8caa07c2013-10-29 11:39:00 +00002015
Yicong Yangcf5724e2021-04-08 19:38:14 +08002016 return devm_add_action_or_reset(dev, devm_iio_device_unreg, indio_dev);
Sachin Kamat8caa07c2013-10-29 11:39:00 +00002017}
Jonathan Cameron63b19542017-07-23 17:25:43 +01002018EXPORT_SYMBOL_GPL(__devm_iio_device_register);
Sachin Kamat8caa07c2013-10-29 11:39:00 +00002019
2020/**
Alison Schofield08a33802016-03-09 11:30:12 -08002021 * iio_device_claim_direct_mode - Keep device in direct mode
2022 * @indio_dev: the iio_dev associated with the device
2023 *
2024 * If the device is in direct mode it is guaranteed to stay
2025 * that way until iio_device_release_direct_mode() is called.
2026 *
2027 * Use with iio_device_release_direct_mode()
2028 *
2029 * Returns: 0 on success, -EBUSY on failure
2030 */
2031int iio_device_claim_direct_mode(struct iio_dev *indio_dev)
2032{
2033 mutex_lock(&indio_dev->mlock);
2034
2035 if (iio_buffer_enabled(indio_dev)) {
2036 mutex_unlock(&indio_dev->mlock);
2037 return -EBUSY;
2038 }
2039 return 0;
2040}
2041EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode);
2042
2043/**
2044 * iio_device_release_direct_mode - releases claim on direct mode
2045 * @indio_dev: the iio_dev associated with the device
2046 *
2047 * Release the claim. Device is no longer guaranteed to stay
2048 * in direct mode.
2049 *
2050 * Use with iio_device_claim_direct_mode()
2051 */
2052void iio_device_release_direct_mode(struct iio_dev *indio_dev)
2053{
2054 mutex_unlock(&indio_dev->mlock);
2055}
2056EXPORT_SYMBOL_GPL(iio_device_release_direct_mode);
2057
Jonathan Cameron847ec802009-08-18 18:06:19 +01002058subsys_initcall(iio_init);
2059module_exit(iio_exit);
2060
Jonathan Cameronc8b95952012-09-02 21:27:56 +01002061MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Cameron847ec802009-08-18 18:06:19 +01002062MODULE_DESCRIPTION("Industrial I/O core");
2063MODULE_LICENSE("GPL");