Commit | Line | Data |
---|---|---|
847ec80b JC |
1 | /* The industrial I/O core |
2 | * | |
3 | * Copyright (c) 2008 Jonathan Cameron | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License version 2 as published by | |
7 | * the Free Software Foundation. | |
8 | * | |
9 | * Based on elements of hwmon and input subsystems. | |
10 | */ | |
11 | ||
3176dd5d SK |
12 | #define pr_fmt(fmt) "iio-core: " fmt |
13 | ||
847ec80b JC |
14 | #include <linux/kernel.h> |
15 | #include <linux/module.h> | |
16 | #include <linux/idr.h> | |
17 | #include <linux/kdev_t.h> | |
18 | #include <linux/err.h> | |
19 | #include <linux/device.h> | |
20 | #include <linux/fs.h> | |
847ec80b | 21 | #include <linux/poll.h> |
ffc18afa | 22 | #include <linux/sched.h> |
4439c935 | 23 | #include <linux/wait.h> |
847ec80b | 24 | #include <linux/cdev.h> |
5a0e3ad6 | 25 | #include <linux/slab.h> |
8e7d9672 | 26 | #include <linux/anon_inodes.h> |
e553f182 | 27 | #include <linux/debugfs.h> |
08a33805 | 28 | #include <linux/mutex.h> |
06458e27 | 29 | #include <linux/iio/iio.h> |
df9c1c42 | 30 | #include "iio_core.h" |
6aea1c36 | 31 | #include "iio_core_trigger.h" |
06458e27 JC |
32 | #include <linux/iio/sysfs.h> |
33 | #include <linux/iio/events.h> | |
9e69c935 | 34 | #include <linux/iio/buffer.h> |
9dd1cb30 | 35 | |
99698b45 | 36 | /* IDA to assign each registered device a unique id */ |
b156cf70 | 37 | static DEFINE_IDA(iio_ida); |
847ec80b | 38 | |
f625cb97 | 39 | static dev_t iio_devt; |
847ec80b JC |
40 | |
41 | #define IIO_DEV_MAX 256 | |
5aaaeba8 | 42 | struct bus_type iio_bus_type = { |
847ec80b | 43 | .name = "iio", |
847ec80b | 44 | }; |
5aaaeba8 | 45 | EXPORT_SYMBOL(iio_bus_type); |
847ec80b | 46 | |
e553f182 MH |
47 | static struct dentry *iio_debugfs_dentry; |
48 | ||
c6fc8062 JC |
49 | static const char * const iio_direction[] = { |
50 | [0] = "in", | |
51 | [1] = "out", | |
52 | }; | |
53 | ||
ade7ef7b | 54 | static const char * const iio_chan_type_name_spec[] = { |
c6fc8062 | 55 | [IIO_VOLTAGE] = "voltage", |
faf290e8 MH |
56 | [IIO_CURRENT] = "current", |
57 | [IIO_POWER] = "power", | |
9bff02f8 | 58 | [IIO_ACCEL] = "accel", |
41ea040c | 59 | [IIO_ANGL_VEL] = "anglvel", |
1d892719 | 60 | [IIO_MAGN] = "magn", |
9bff02f8 BF |
61 | [IIO_LIGHT] = "illuminance", |
62 | [IIO_INTENSITY] = "intensity", | |
f09f2c81 | 63 | [IIO_PROXIMITY] = "proximity", |
9bff02f8 | 64 | [IIO_TEMP] = "temp", |
1d892719 JC |
65 | [IIO_INCLI] = "incli", |
66 | [IIO_ROT] = "rot", | |
1d892719 | 67 | [IIO_ANGL] = "angl", |
9bff02f8 | 68 | [IIO_TIMESTAMP] = "timestamp", |
66dbe704 | 69 | [IIO_CAPACITANCE] = "capacitance", |
a6b12855 | 70 | [IIO_ALTVOLTAGE] = "altvoltage", |
21cd1fab | 71 | [IIO_CCT] = "cct", |
c4f0c693 | 72 | [IIO_PRESSURE] = "pressure", |
ac216aa2 | 73 | [IIO_HUMIDITYRELATIVE] = "humidityrelative", |
55aebeb9 | 74 | [IIO_ACTIVITY] = "activity", |
a88bfe78 | 75 | [IIO_STEPS] = "steps", |
72c66644 | 76 | [IIO_ENERGY] = "energy", |
cc3c9eec | 77 | [IIO_DISTANCE] = "distance", |
5a1a9329 | 78 | [IIO_VELOCITY] = "velocity", |
8ff6b3bc | 79 | [IIO_CONCENTRATION] = "concentration", |
d38d5469 | 80 | [IIO_RESISTANCE] = "resistance", |
ecb3a7cc | 81 | [IIO_PH] = "ph", |
d409404c | 82 | [IIO_UVINDEX] = "uvindex", |
4b9d2090 | 83 | [IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity", |
1a8f324a WBG |
84 | [IIO_COUNT] = "count", |
85 | [IIO_INDEX] = "index", | |
1d892719 JC |
86 | }; |
87 | ||
330c6c57 | 88 | static const char * const iio_modifier_names[] = { |
1d892719 JC |
89 | [IIO_MOD_X] = "x", |
90 | [IIO_MOD_Y] = "y", | |
91 | [IIO_MOD_Z] = "z", | |
4b8d8015 PM |
92 | [IIO_MOD_X_AND_Y] = "x&y", |
93 | [IIO_MOD_X_AND_Z] = "x&z", | |
94 | [IIO_MOD_Y_AND_Z] = "y&z", | |
95 | [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z", | |
96 | [IIO_MOD_X_OR_Y] = "x|y", | |
97 | [IIO_MOD_X_OR_Z] = "x|z", | |
98 | [IIO_MOD_Y_OR_Z] = "y|z", | |
99 | [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z", | |
8f5879b2 | 100 | [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)", |
cf82cb81 | 101 | [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2", |
330c6c57 JC |
102 | [IIO_MOD_LIGHT_BOTH] = "both", |
103 | [IIO_MOD_LIGHT_IR] = "ir", | |
21cd1fab JB |
104 | [IIO_MOD_LIGHT_CLEAR] = "clear", |
105 | [IIO_MOD_LIGHT_RED] = "red", | |
106 | [IIO_MOD_LIGHT_GREEN] = "green", | |
107 | [IIO_MOD_LIGHT_BLUE] = "blue", | |
2c5ff1f9 | 108 | [IIO_MOD_LIGHT_UV] = "uv", |
5082f405 | 109 | [IIO_MOD_QUATERNION] = "quaternion", |
638b43b3 PM |
110 | [IIO_MOD_TEMP_AMBIENT] = "ambient", |
111 | [IIO_MOD_TEMP_OBJECT] = "object", | |
11b8ddab RA |
112 | [IIO_MOD_NORTH_MAGN] = "from_north_magnetic", |
113 | [IIO_MOD_NORTH_TRUE] = "from_north_true", | |
114 | [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp", | |
115 | [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp", | |
55aebeb9 DB |
116 | [IIO_MOD_RUNNING] = "running", |
117 | [IIO_MOD_JOGGING] = "jogging", | |
118 | [IIO_MOD_WALKING] = "walking", | |
119 | [IIO_MOD_STILL] = "still", | |
5a1a9329 | 120 | [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)", |
1ce87f21 LPC |
121 | [IIO_MOD_I] = "i", |
122 | [IIO_MOD_Q] = "q", | |
8ff6b3bc MR |
123 | [IIO_MOD_CO2] = "co2", |
124 | [IIO_MOD_VOC] = "voc", | |
1d892719 JC |
125 | }; |
126 | ||
127 | /* relies on pairs of these shared then separate */ | |
128 | static const char * const iio_chan_info_postfix[] = { | |
75a973c7 JC |
129 | [IIO_CHAN_INFO_RAW] = "raw", |
130 | [IIO_CHAN_INFO_PROCESSED] = "input", | |
c8a9f805 JC |
131 | [IIO_CHAN_INFO_SCALE] = "scale", |
132 | [IIO_CHAN_INFO_OFFSET] = "offset", | |
133 | [IIO_CHAN_INFO_CALIBSCALE] = "calibscale", | |
134 | [IIO_CHAN_INFO_CALIBBIAS] = "calibbias", | |
135 | [IIO_CHAN_INFO_PEAK] = "peak_raw", | |
136 | [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale", | |
137 | [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw", | |
138 | [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw", | |
df94aba8 JC |
139 | [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY] |
140 | = "filter_low_pass_3db_frequency", | |
3f7f642b MF |
141 | [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY] |
142 | = "filter_high_pass_3db_frequency", | |
ce85a1cb | 143 | [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency", |
a6b12855 MH |
144 | [IIO_CHAN_INFO_FREQUENCY] = "frequency", |
145 | [IIO_CHAN_INFO_PHASE] = "phase", | |
b65d6212 | 146 | [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain", |
7c9ab035 | 147 | [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis", |
899d90bd | 148 | [IIO_CHAN_INFO_INT_TIME] = "integration_time", |
a88bfe78 | 149 | [IIO_CHAN_INFO_ENABLE] = "en", |
bcdf28fb | 150 | [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight", |
d37f6836 | 151 | [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight", |
2f0ecb7c IT |
152 | [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count", |
153 | [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time", | |
c8a85854 | 154 | [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity", |
faaa4495 | 155 | [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio", |
1d892719 JC |
156 | }; |
157 | ||
a7e57dce SK |
158 | /** |
159 | * iio_find_channel_from_si() - get channel from its scan index | |
160 | * @indio_dev: device | |
161 | * @si: scan index to match | |
162 | */ | |
5fb21c82 JC |
163 | const struct iio_chan_spec |
164 | *iio_find_channel_from_si(struct iio_dev *indio_dev, int si) | |
165 | { | |
166 | int i; | |
167 | ||
168 | for (i = 0; i < indio_dev->num_channels; i++) | |
169 | if (indio_dev->channels[i].scan_index == si) | |
170 | return &indio_dev->channels[i]; | |
171 | return NULL; | |
172 | } | |
173 | ||
847ec80b JC |
174 | /* This turns up an awful lot */ |
175 | ssize_t iio_read_const_attr(struct device *dev, | |
176 | struct device_attribute *attr, | |
177 | char *buf) | |
178 | { | |
179 | return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string); | |
180 | } | |
181 | EXPORT_SYMBOL(iio_read_const_attr); | |
182 | ||
bc2b7dab GB |
183 | static int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id) |
184 | { | |
185 | int ret; | |
186 | const struct iio_event_interface *ev_int = indio_dev->event_interface; | |
187 | ||
188 | ret = mutex_lock_interruptible(&indio_dev->mlock); | |
189 | if (ret) | |
190 | return ret; | |
191 | if ((ev_int && iio_event_enabled(ev_int)) || | |
192 | iio_buffer_enabled(indio_dev)) { | |
193 | mutex_unlock(&indio_dev->mlock); | |
194 | return -EBUSY; | |
195 | } | |
196 | indio_dev->clock_id = clock_id; | |
197 | mutex_unlock(&indio_dev->mlock); | |
198 | ||
199 | return 0; | |
200 | } | |
201 | ||
202 | /** | |
203 | * iio_get_time_ns() - utility function to get a time stamp for events etc | |
204 | * @indio_dev: device | |
205 | */ | |
206 | s64 iio_get_time_ns(const struct iio_dev *indio_dev) | |
207 | { | |
208 | struct timespec tp; | |
209 | ||
210 | switch (iio_device_get_clock(indio_dev)) { | |
211 | case CLOCK_REALTIME: | |
212 | ktime_get_real_ts(&tp); | |
213 | break; | |
214 | case CLOCK_MONOTONIC: | |
215 | ktime_get_ts(&tp); | |
216 | break; | |
217 | case CLOCK_MONOTONIC_RAW: | |
218 | getrawmonotonic(&tp); | |
219 | break; | |
220 | case CLOCK_REALTIME_COARSE: | |
221 | tp = current_kernel_time(); | |
222 | break; | |
223 | case CLOCK_MONOTONIC_COARSE: | |
224 | tp = get_monotonic_coarse(); | |
225 | break; | |
226 | case CLOCK_BOOTTIME: | |
227 | get_monotonic_boottime(&tp); | |
228 | break; | |
229 | case CLOCK_TAI: | |
230 | timekeeping_clocktai(&tp); | |
231 | break; | |
232 | default: | |
233 | BUG(); | |
234 | } | |
235 | ||
236 | return timespec_to_ns(&tp); | |
237 | } | |
238 | EXPORT_SYMBOL(iio_get_time_ns); | |
239 | ||
240 | /** | |
241 | * iio_get_time_res() - utility function to get time stamp clock resolution in | |
242 | * nano seconds. | |
243 | * @indio_dev: device | |
244 | */ | |
245 | unsigned int iio_get_time_res(const struct iio_dev *indio_dev) | |
246 | { | |
247 | switch (iio_device_get_clock(indio_dev)) { | |
248 | case CLOCK_REALTIME: | |
249 | case CLOCK_MONOTONIC: | |
250 | case CLOCK_MONOTONIC_RAW: | |
251 | case CLOCK_BOOTTIME: | |
252 | case CLOCK_TAI: | |
253 | return hrtimer_resolution; | |
254 | case CLOCK_REALTIME_COARSE: | |
255 | case CLOCK_MONOTONIC_COARSE: | |
256 | return LOW_RES_NSEC; | |
257 | default: | |
258 | BUG(); | |
259 | } | |
260 | } | |
261 | EXPORT_SYMBOL(iio_get_time_res); | |
262 | ||
847ec80b JC |
263 | static int __init iio_init(void) |
264 | { | |
265 | int ret; | |
266 | ||
5aaaeba8 JC |
267 | /* Register sysfs bus */ |
268 | ret = bus_register(&iio_bus_type); | |
847ec80b | 269 | if (ret < 0) { |
3176dd5d | 270 | pr_err("could not register bus type\n"); |
847ec80b JC |
271 | goto error_nothing; |
272 | } | |
273 | ||
9aa1a167 JC |
274 | ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio"); |
275 | if (ret < 0) { | |
3176dd5d | 276 | pr_err("failed to allocate char dev region\n"); |
5aaaeba8 | 277 | goto error_unregister_bus_type; |
9aa1a167 | 278 | } |
847ec80b | 279 | |
e553f182 MH |
280 | iio_debugfs_dentry = debugfs_create_dir("iio", NULL); |
281 | ||
847ec80b JC |
282 | return 0; |
283 | ||
5aaaeba8 JC |
284 | error_unregister_bus_type: |
285 | bus_unregister(&iio_bus_type); | |
847ec80b JC |
286 | error_nothing: |
287 | return ret; | |
288 | } | |
289 | ||
290 | static void __exit iio_exit(void) | |
291 | { | |
9aa1a167 JC |
292 | if (iio_devt) |
293 | unregister_chrdev_region(iio_devt, IIO_DEV_MAX); | |
5aaaeba8 | 294 | bus_unregister(&iio_bus_type); |
e553f182 MH |
295 | debugfs_remove(iio_debugfs_dentry); |
296 | } | |
297 | ||
298 | #if defined(CONFIG_DEBUG_FS) | |
e553f182 MH |
299 | static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf, |
300 | size_t count, loff_t *ppos) | |
301 | { | |
302 | struct iio_dev *indio_dev = file->private_data; | |
303 | char buf[20]; | |
304 | unsigned val = 0; | |
305 | ssize_t len; | |
306 | int ret; | |
307 | ||
308 | ret = indio_dev->info->debugfs_reg_access(indio_dev, | |
309 | indio_dev->cached_reg_addr, | |
310 | 0, &val); | |
311 | if (ret) | |
312 | dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__); | |
313 | ||
314 | len = snprintf(buf, sizeof(buf), "0x%X\n", val); | |
315 | ||
316 | return simple_read_from_buffer(userbuf, count, ppos, buf, len); | |
317 | } | |
318 | ||
319 | static ssize_t iio_debugfs_write_reg(struct file *file, | |
320 | const char __user *userbuf, size_t count, loff_t *ppos) | |
321 | { | |
322 | struct iio_dev *indio_dev = file->private_data; | |
323 | unsigned reg, val; | |
324 | char buf[80]; | |
325 | int ret; | |
326 | ||
327 | count = min_t(size_t, count, (sizeof(buf)-1)); | |
328 | if (copy_from_user(buf, userbuf, count)) | |
329 | return -EFAULT; | |
330 | ||
331 | buf[count] = 0; | |
332 | ||
333 | ret = sscanf(buf, "%i %i", ®, &val); | |
334 | ||
335 | switch (ret) { | |
336 | case 1: | |
337 | indio_dev->cached_reg_addr = reg; | |
338 | break; | |
339 | case 2: | |
340 | indio_dev->cached_reg_addr = reg; | |
341 | ret = indio_dev->info->debugfs_reg_access(indio_dev, reg, | |
342 | val, NULL); | |
343 | if (ret) { | |
344 | dev_err(indio_dev->dev.parent, "%s: write failed\n", | |
345 | __func__); | |
346 | return ret; | |
347 | } | |
348 | break; | |
349 | default: | |
350 | return -EINVAL; | |
351 | } | |
352 | ||
353 | return count; | |
354 | } | |
355 | ||
356 | static const struct file_operations iio_debugfs_reg_fops = { | |
5a28c873 | 357 | .open = simple_open, |
e553f182 MH |
358 | .read = iio_debugfs_read_reg, |
359 | .write = iio_debugfs_write_reg, | |
360 | }; | |
361 | ||
362 | static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) | |
363 | { | |
364 | debugfs_remove_recursive(indio_dev->debugfs_dentry); | |
847ec80b JC |
365 | } |
366 | ||
e553f182 MH |
367 | static int iio_device_register_debugfs(struct iio_dev *indio_dev) |
368 | { | |
369 | struct dentry *d; | |
370 | ||
371 | if (indio_dev->info->debugfs_reg_access == NULL) | |
372 | return 0; | |
373 | ||
abd5a2fb | 374 | if (!iio_debugfs_dentry) |
e553f182 MH |
375 | return 0; |
376 | ||
377 | indio_dev->debugfs_dentry = | |
378 | debugfs_create_dir(dev_name(&indio_dev->dev), | |
379 | iio_debugfs_dentry); | |
e553f182 MH |
380 | if (indio_dev->debugfs_dentry == NULL) { |
381 | dev_warn(indio_dev->dev.parent, | |
382 | "Failed to create debugfs directory\n"); | |
383 | return -EFAULT; | |
384 | } | |
385 | ||
386 | d = debugfs_create_file("direct_reg_access", 0644, | |
387 | indio_dev->debugfs_dentry, | |
388 | indio_dev, &iio_debugfs_reg_fops); | |
389 | if (!d) { | |
390 | iio_device_unregister_debugfs(indio_dev); | |
391 | return -ENOMEM; | |
392 | } | |
393 | ||
394 | return 0; | |
395 | } | |
396 | #else | |
397 | static int iio_device_register_debugfs(struct iio_dev *indio_dev) | |
398 | { | |
399 | return 0; | |
400 | } | |
401 | ||
402 | static void iio_device_unregister_debugfs(struct iio_dev *indio_dev) | |
403 | { | |
404 | } | |
405 | #endif /* CONFIG_DEBUG_FS */ | |
406 | ||
4fee7e16 LPC |
407 | static ssize_t iio_read_channel_ext_info(struct device *dev, |
408 | struct device_attribute *attr, | |
409 | char *buf) | |
410 | { | |
e53f5ac5 | 411 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
4fee7e16 LPC |
412 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
413 | const struct iio_chan_spec_ext_info *ext_info; | |
414 | ||
415 | ext_info = &this_attr->c->ext_info[this_attr->address]; | |
416 | ||
fc6d1139 | 417 | return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf); |
4fee7e16 LPC |
418 | } |
419 | ||
420 | static ssize_t iio_write_channel_ext_info(struct device *dev, | |
421 | struct device_attribute *attr, | |
422 | const char *buf, | |
423 | size_t len) | |
424 | { | |
e53f5ac5 | 425 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
4fee7e16 LPC |
426 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
427 | const struct iio_chan_spec_ext_info *ext_info; | |
428 | ||
429 | ext_info = &this_attr->c->ext_info[this_attr->address]; | |
430 | ||
fc6d1139 MH |
431 | return ext_info->write(indio_dev, ext_info->private, |
432 | this_attr->c, buf, len); | |
4fee7e16 LPC |
433 | } |
434 | ||
5212cc8a LPC |
435 | ssize_t iio_enum_available_read(struct iio_dev *indio_dev, |
436 | uintptr_t priv, const struct iio_chan_spec *chan, char *buf) | |
437 | { | |
438 | const struct iio_enum *e = (const struct iio_enum *)priv; | |
439 | unsigned int i; | |
440 | size_t len = 0; | |
441 | ||
442 | if (!e->num_items) | |
443 | return 0; | |
444 | ||
445 | for (i = 0; i < e->num_items; ++i) | |
74dcd439 | 446 | len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]); |
5212cc8a LPC |
447 | |
448 | /* replace last space with a newline */ | |
449 | buf[len - 1] = '\n'; | |
450 | ||
451 | return len; | |
452 | } | |
453 | EXPORT_SYMBOL_GPL(iio_enum_available_read); | |
454 | ||
455 | ssize_t iio_enum_read(struct iio_dev *indio_dev, | |
456 | uintptr_t priv, const struct iio_chan_spec *chan, char *buf) | |
457 | { | |
458 | const struct iio_enum *e = (const struct iio_enum *)priv; | |
459 | int i; | |
460 | ||
461 | if (!e->get) | |
462 | return -EINVAL; | |
463 | ||
464 | i = e->get(indio_dev, chan); | |
465 | if (i < 0) | |
466 | return i; | |
467 | else if (i >= e->num_items) | |
468 | return -EINVAL; | |
469 | ||
598db581 | 470 | return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]); |
5212cc8a LPC |
471 | } |
472 | EXPORT_SYMBOL_GPL(iio_enum_read); | |
473 | ||
474 | ssize_t iio_enum_write(struct iio_dev *indio_dev, | |
475 | uintptr_t priv, const struct iio_chan_spec *chan, const char *buf, | |
476 | size_t len) | |
477 | { | |
478 | const struct iio_enum *e = (const struct iio_enum *)priv; | |
479 | unsigned int i; | |
480 | int ret; | |
481 | ||
482 | if (!e->set) | |
483 | return -EINVAL; | |
484 | ||
485 | for (i = 0; i < e->num_items; i++) { | |
486 | if (sysfs_streq(buf, e->items[i])) | |
487 | break; | |
488 | } | |
489 | ||
490 | if (i == e->num_items) | |
491 | return -EINVAL; | |
492 | ||
493 | ret = e->set(indio_dev, chan, i); | |
494 | return ret ? ret : len; | |
495 | } | |
496 | EXPORT_SYMBOL_GPL(iio_enum_write); | |
497 | ||
dfc57732 GB |
498 | static const struct iio_mount_matrix iio_mount_idmatrix = { |
499 | .rotation = { | |
500 | "1", "0", "0", | |
501 | "0", "1", "0", | |
502 | "0", "0", "1" | |
503 | } | |
504 | }; | |
505 | ||
506 | static int iio_setup_mount_idmatrix(const struct device *dev, | |
507 | struct iio_mount_matrix *matrix) | |
508 | { | |
509 | *matrix = iio_mount_idmatrix; | |
510 | dev_info(dev, "mounting matrix not found: using identity...\n"); | |
511 | return 0; | |
512 | } | |
513 | ||
514 | ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv, | |
515 | const struct iio_chan_spec *chan, char *buf) | |
516 | { | |
517 | const struct iio_mount_matrix *mtx = ((iio_get_mount_matrix_t *) | |
518 | priv)(indio_dev, chan); | |
519 | ||
520 | if (IS_ERR(mtx)) | |
521 | return PTR_ERR(mtx); | |
522 | ||
523 | if (!mtx) | |
524 | mtx = &iio_mount_idmatrix; | |
525 | ||
526 | return snprintf(buf, PAGE_SIZE, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n", | |
527 | mtx->rotation[0], mtx->rotation[1], mtx->rotation[2], | |
528 | mtx->rotation[3], mtx->rotation[4], mtx->rotation[5], | |
529 | mtx->rotation[6], mtx->rotation[7], mtx->rotation[8]); | |
530 | } | |
531 | EXPORT_SYMBOL_GPL(iio_show_mount_matrix); | |
532 | ||
533 | /** | |
534 | * of_iio_read_mount_matrix() - retrieve iio device mounting matrix from | |
535 | * device-tree "mount-matrix" property | |
536 | * @dev: device the mounting matrix property is assigned to | |
537 | * @propname: device specific mounting matrix property name | |
538 | * @matrix: where to store retrieved matrix | |
539 | * | |
540 | * If device is assigned no mounting matrix property, a default 3x3 identity | |
541 | * matrix will be filled in. | |
542 | * | |
543 | * Return: 0 if success, or a negative error code on failure. | |
544 | */ | |
545 | #ifdef CONFIG_OF | |
546 | int of_iio_read_mount_matrix(const struct device *dev, | |
547 | const char *propname, | |
548 | struct iio_mount_matrix *matrix) | |
549 | { | |
550 | if (dev->of_node) { | |
551 | int err = of_property_read_string_array(dev->of_node, | |
552 | propname, matrix->rotation, | |
553 | ARRAY_SIZE(iio_mount_idmatrix.rotation)); | |
554 | ||
555 | if (err == ARRAY_SIZE(iio_mount_idmatrix.rotation)) | |
556 | return 0; | |
557 | ||
558 | if (err >= 0) | |
559 | /* Invalid number of matrix entries. */ | |
560 | return -EINVAL; | |
561 | ||
562 | if (err != -EINVAL) | |
563 | /* Invalid matrix declaration format. */ | |
564 | return err; | |
565 | } | |
566 | ||
567 | /* Matrix was not declared at all: fallback to identity. */ | |
568 | return iio_setup_mount_idmatrix(dev, matrix); | |
569 | } | |
570 | #else | |
571 | int of_iio_read_mount_matrix(const struct device *dev, | |
572 | const char *propname, | |
573 | struct iio_mount_matrix *matrix) | |
574 | { | |
575 | return iio_setup_mount_idmatrix(dev, matrix); | |
576 | } | |
577 | #endif | |
578 | EXPORT_SYMBOL(of_iio_read_mount_matrix); | |
579 | ||
3661f3f5 LPC |
580 | /** |
581 | * iio_format_value() - Formats a IIO value into its string representation | |
2498dcf6 CO |
582 | * @buf: The buffer to which the formatted value gets written |
583 | * @type: One of the IIO_VAL_... constants. This decides how the val | |
584 | * and val2 parameters are formatted. | |
585 | * @size: Number of IIO value entries contained in vals | |
586 | * @vals: Pointer to the values, exact meaning depends on the | |
587 | * type parameter. | |
588 | * | |
589 | * Return: 0 by default, a negative number on failure or the | |
590 | * total number of characters written for a type that belongs | |
591 | * to the IIO_VAL_... constant. | |
3661f3f5 | 592 | */ |
9fbfb4b3 | 593 | ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals) |
847ec80b | 594 | { |
7985e7c1 | 595 | unsigned long long tmp; |
67eedba3 | 596 | bool scale_db = false; |
1d892719 | 597 | |
3661f3f5 | 598 | switch (type) { |
67eedba3 | 599 | case IIO_VAL_INT: |
9fbfb4b3 | 600 | return sprintf(buf, "%d\n", vals[0]); |
67eedba3 MH |
601 | case IIO_VAL_INT_PLUS_MICRO_DB: |
602 | scale_db = true; | |
603 | case IIO_VAL_INT_PLUS_MICRO: | |
9fbfb4b3 | 604 | if (vals[1] < 0) |
8f57e4d9 MN |
605 | return sprintf(buf, "-%d.%06u%s\n", abs(vals[0]), |
606 | -vals[1], scale_db ? " dB" : ""); | |
1d892719 | 607 | else |
9fbfb4b3 | 608 | return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1], |
67eedba3 MH |
609 | scale_db ? " dB" : ""); |
610 | case IIO_VAL_INT_PLUS_NANO: | |
9fbfb4b3 | 611 | if (vals[1] < 0) |
8f57e4d9 MN |
612 | return sprintf(buf, "-%d.%09u\n", abs(vals[0]), |
613 | -vals[1]); | |
71646e2c | 614 | else |
9fbfb4b3 | 615 | return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); |
7985e7c1 | 616 | case IIO_VAL_FRACTIONAL: |
9fbfb4b3 | 617 | tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]); |
171c0091 GB |
618 | vals[0] = (int)div_s64_rem(tmp, 1000000000, &vals[1]); |
619 | return sprintf(buf, "%d.%09u\n", vals[0], abs(vals[1])); | |
103d9fb9 | 620 | case IIO_VAL_FRACTIONAL_LOG2: |
9fbfb4b3 SP |
621 | tmp = (s64)vals[0] * 1000000000LL >> vals[1]; |
622 | vals[1] = do_div(tmp, 1000000000LL); | |
623 | vals[0] = tmp; | |
624 | return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); | |
625 | case IIO_VAL_INT_MULTIPLE: | |
626 | { | |
627 | int i; | |
628 | int len = 0; | |
629 | ||
630 | for (i = 0; i < size; ++i) | |
631 | len += snprintf(&buf[len], PAGE_SIZE - len, "%d ", | |
632 | vals[i]); | |
633 | len += snprintf(&buf[len], PAGE_SIZE - len, "\n"); | |
634 | return len; | |
635 | } | |
67eedba3 | 636 | default: |
1d892719 | 637 | return 0; |
67eedba3 | 638 | } |
1d892719 | 639 | } |
7d2c2aca | 640 | EXPORT_SYMBOL_GPL(iio_format_value); |
1d892719 | 641 | |
3661f3f5 LPC |
642 | static ssize_t iio_read_channel_info(struct device *dev, |
643 | struct device_attribute *attr, | |
644 | char *buf) | |
645 | { | |
646 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); | |
647 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
9fbfb4b3 SP |
648 | int vals[INDIO_MAX_RAW_ELEMENTS]; |
649 | int ret; | |
650 | int val_len = 2; | |
651 | ||
652 | if (indio_dev->info->read_raw_multi) | |
653 | ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c, | |
654 | INDIO_MAX_RAW_ELEMENTS, | |
655 | vals, &val_len, | |
656 | this_attr->address); | |
657 | else | |
658 | ret = indio_dev->info->read_raw(indio_dev, this_attr->c, | |
659 | &vals[0], &vals[1], this_attr->address); | |
3661f3f5 LPC |
660 | |
661 | if (ret < 0) | |
662 | return ret; | |
663 | ||
9fbfb4b3 | 664 | return iio_format_value(buf, ret, val_len, vals); |
3661f3f5 LPC |
665 | } |
666 | ||
6807d721 LPC |
667 | /** |
668 | * iio_str_to_fixpoint() - Parse a fixed-point number from a string | |
669 | * @str: The string to parse | |
670 | * @fract_mult: Multiplier for the first decimal place, should be a power of 10 | |
671 | * @integer: The integer part of the number | |
672 | * @fract: The fractional part of the number | |
673 | * | |
674 | * Returns 0 on success, or a negative error code if the string could not be | |
675 | * parsed. | |
676 | */ | |
677 | int iio_str_to_fixpoint(const char *str, int fract_mult, | |
678 | int *integer, int *fract) | |
679 | { | |
680 | int i = 0, f = 0; | |
681 | bool integer_part = true, negative = false; | |
682 | ||
f47dff32 SN |
683 | if (fract_mult == 0) { |
684 | *fract = 0; | |
685 | ||
686 | return kstrtoint(str, 0, integer); | |
687 | } | |
688 | ||
6807d721 LPC |
689 | if (str[0] == '-') { |
690 | negative = true; | |
691 | str++; | |
692 | } else if (str[0] == '+') { | |
693 | str++; | |
694 | } | |
695 | ||
696 | while (*str) { | |
697 | if ('0' <= *str && *str <= '9') { | |
698 | if (integer_part) { | |
699 | i = i * 10 + *str - '0'; | |
700 | } else { | |
701 | f += fract_mult * (*str - '0'); | |
702 | fract_mult /= 10; | |
703 | } | |
704 | } else if (*str == '\n') { | |
705 | if (*(str + 1) == '\0') | |
706 | break; | |
707 | else | |
708 | return -EINVAL; | |
709 | } else if (*str == '.' && integer_part) { | |
710 | integer_part = false; | |
711 | } else { | |
712 | return -EINVAL; | |
713 | } | |
714 | str++; | |
715 | } | |
716 | ||
717 | if (negative) { | |
718 | if (i) | |
719 | i = -i; | |
720 | else | |
721 | f = -f; | |
722 | } | |
723 | ||
724 | *integer = i; | |
725 | *fract = f; | |
726 | ||
727 | return 0; | |
728 | } | |
729 | EXPORT_SYMBOL_GPL(iio_str_to_fixpoint); | |
730 | ||
1d892719 JC |
731 | static ssize_t iio_write_channel_info(struct device *dev, |
732 | struct device_attribute *attr, | |
733 | const char *buf, | |
734 | size_t len) | |
735 | { | |
e53f5ac5 | 736 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
1d892719 | 737 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
6807d721 LPC |
738 | int ret, fract_mult = 100000; |
739 | int integer, fract; | |
1d892719 JC |
740 | |
741 | /* Assumes decimal - precision based on number of digits */ | |
6fe8135f | 742 | if (!indio_dev->info->write_raw) |
1d892719 | 743 | return -EINVAL; |
5c04af04 MH |
744 | |
745 | if (indio_dev->info->write_raw_get_fmt) | |
746 | switch (indio_dev->info->write_raw_get_fmt(indio_dev, | |
747 | this_attr->c, this_attr->address)) { | |
f47dff32 SN |
748 | case IIO_VAL_INT: |
749 | fract_mult = 0; | |
750 | break; | |
5c04af04 MH |
751 | case IIO_VAL_INT_PLUS_MICRO: |
752 | fract_mult = 100000; | |
753 | break; | |
754 | case IIO_VAL_INT_PLUS_NANO: | |
755 | fract_mult = 100000000; | |
756 | break; | |
757 | default: | |
758 | return -EINVAL; | |
759 | } | |
760 | ||
6807d721 LPC |
761 | ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract); |
762 | if (ret) | |
763 | return ret; | |
1d892719 | 764 | |
6fe8135f | 765 | ret = indio_dev->info->write_raw(indio_dev, this_attr->c, |
5c04af04 | 766 | integer, fract, this_attr->address); |
1d892719 JC |
767 | if (ret) |
768 | return ret; | |
769 | ||
770 | return len; | |
771 | } | |
772 | ||
df9c1c42 | 773 | static |
1d892719 JC |
774 | int __iio_device_attr_init(struct device_attribute *dev_attr, |
775 | const char *postfix, | |
776 | struct iio_chan_spec const *chan, | |
777 | ssize_t (*readfunc)(struct device *dev, | |
778 | struct device_attribute *attr, | |
779 | char *buf), | |
780 | ssize_t (*writefunc)(struct device *dev, | |
781 | struct device_attribute *attr, | |
782 | const char *buf, | |
783 | size_t len), | |
3704432f | 784 | enum iio_shared_by shared_by) |
1d892719 | 785 | { |
3704432f | 786 | int ret = 0; |
7bbcf7e1 | 787 | char *name = NULL; |
3704432f | 788 | char *full_postfix; |
1d892719 | 789 | sysfs_attr_init(&dev_attr->attr); |
1d892719 | 790 | |
ade7ef7b | 791 | /* Build up postfix of <extend_name>_<modifier>_postfix */ |
3704432f | 792 | if (chan->modified && (shared_by == IIO_SEPARATE)) { |
ade7ef7b JC |
793 | if (chan->extend_name) |
794 | full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", | |
795 | iio_modifier_names[chan | |
796 | ->channel2], | |
797 | chan->extend_name, | |
798 | postfix); | |
799 | else | |
800 | full_postfix = kasprintf(GFP_KERNEL, "%s_%s", | |
801 | iio_modifier_names[chan | |
802 | ->channel2], | |
803 | postfix); | |
804 | } else { | |
77bfa8ba | 805 | if (chan->extend_name == NULL || shared_by != IIO_SEPARATE) |
ade7ef7b JC |
806 | full_postfix = kstrdup(postfix, GFP_KERNEL); |
807 | else | |
808 | full_postfix = kasprintf(GFP_KERNEL, | |
809 | "%s_%s", | |
810 | chan->extend_name, | |
811 | postfix); | |
812 | } | |
3704432f JC |
813 | if (full_postfix == NULL) |
814 | return -ENOMEM; | |
1d892719 | 815 | |
4abf6f8b | 816 | if (chan->differential) { /* Differential can not have modifier */ |
3704432f | 817 | switch (shared_by) { |
c006ec83 | 818 | case IIO_SHARED_BY_ALL: |
7bbcf7e1 | 819 | name = kasprintf(GFP_KERNEL, "%s", full_postfix); |
c006ec83 JC |
820 | break; |
821 | case IIO_SHARED_BY_DIR: | |
7bbcf7e1 | 822 | name = kasprintf(GFP_KERNEL, "%s_%s", |
c006ec83 JC |
823 | iio_direction[chan->output], |
824 | full_postfix); | |
825 | break; | |
3704432f | 826 | case IIO_SHARED_BY_TYPE: |
7bbcf7e1 | 827 | name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s", |
ade7ef7b JC |
828 | iio_direction[chan->output], |
829 | iio_chan_type_name_spec[chan->type], | |
830 | iio_chan_type_name_spec[chan->type], | |
831 | full_postfix); | |
3704432f JC |
832 | break; |
833 | case IIO_SEPARATE: | |
834 | if (!chan->indexed) { | |
231bfe53 | 835 | WARN(1, "Differential channels must be indexed\n"); |
3704432f JC |
836 | ret = -EINVAL; |
837 | goto error_free_full_postfix; | |
838 | } | |
7bbcf7e1 | 839 | name = kasprintf(GFP_KERNEL, |
3704432f | 840 | "%s_%s%d-%s%d_%s", |
ade7ef7b JC |
841 | iio_direction[chan->output], |
842 | iio_chan_type_name_spec[chan->type], | |
843 | chan->channel, | |
844 | iio_chan_type_name_spec[chan->type], | |
845 | chan->channel2, | |
846 | full_postfix); | |
3704432f | 847 | break; |
ade7ef7b JC |
848 | } |
849 | } else { /* Single ended */ | |
3704432f | 850 | switch (shared_by) { |
c006ec83 | 851 | case IIO_SHARED_BY_ALL: |
7bbcf7e1 | 852 | name = kasprintf(GFP_KERNEL, "%s", full_postfix); |
c006ec83 JC |
853 | break; |
854 | case IIO_SHARED_BY_DIR: | |
7bbcf7e1 | 855 | name = kasprintf(GFP_KERNEL, "%s_%s", |
c006ec83 JC |
856 | iio_direction[chan->output], |
857 | full_postfix); | |
858 | break; | |
3704432f | 859 | case IIO_SHARED_BY_TYPE: |
7bbcf7e1 | 860 | name = kasprintf(GFP_KERNEL, "%s_%s_%s", |
ade7ef7b JC |
861 | iio_direction[chan->output], |
862 | iio_chan_type_name_spec[chan->type], | |
863 | full_postfix); | |
3704432f JC |
864 | break; |
865 | ||
866 | case IIO_SEPARATE: | |
867 | if (chan->indexed) | |
7bbcf7e1 | 868 | name = kasprintf(GFP_KERNEL, "%s_%s%d_%s", |
3704432f JC |
869 | iio_direction[chan->output], |
870 | iio_chan_type_name_spec[chan->type], | |
871 | chan->channel, | |
872 | full_postfix); | |
873 | else | |
7bbcf7e1 | 874 | name = kasprintf(GFP_KERNEL, "%s_%s_%s", |
3704432f JC |
875 | iio_direction[chan->output], |
876 | iio_chan_type_name_spec[chan->type], | |
877 | full_postfix); | |
878 | break; | |
879 | } | |
ade7ef7b | 880 | } |
7bbcf7e1 | 881 | if (name == NULL) { |
1d892719 JC |
882 | ret = -ENOMEM; |
883 | goto error_free_full_postfix; | |
884 | } | |
7bbcf7e1 | 885 | dev_attr->attr.name = name; |
1d892719 JC |
886 | |
887 | if (readfunc) { | |
888 | dev_attr->attr.mode |= S_IRUGO; | |
889 | dev_attr->show = readfunc; | |
890 | } | |
891 | ||
892 | if (writefunc) { | |
893 | dev_attr->attr.mode |= S_IWUSR; | |
894 | dev_attr->store = writefunc; | |
895 | } | |
7bbcf7e1 | 896 | |
1d892719 JC |
897 | error_free_full_postfix: |
898 | kfree(full_postfix); | |
3704432f | 899 | |
1d892719 JC |
900 | return ret; |
901 | } | |
902 | ||
df9c1c42 | 903 | static void __iio_device_attr_deinit(struct device_attribute *dev_attr) |
1d892719 JC |
904 | { |
905 | kfree(dev_attr->attr.name); | |
906 | } | |
907 | ||
908 | int __iio_add_chan_devattr(const char *postfix, | |
1d892719 JC |
909 | struct iio_chan_spec const *chan, |
910 | ssize_t (*readfunc)(struct device *dev, | |
911 | struct device_attribute *attr, | |
912 | char *buf), | |
913 | ssize_t (*writefunc)(struct device *dev, | |
914 | struct device_attribute *attr, | |
915 | const char *buf, | |
916 | size_t len), | |
e614a54b | 917 | u64 mask, |
3704432f | 918 | enum iio_shared_by shared_by, |
1d892719 JC |
919 | struct device *dev, |
920 | struct list_head *attr_list) | |
921 | { | |
922 | int ret; | |
923 | struct iio_dev_attr *iio_attr, *t; | |
924 | ||
670c1103 | 925 | iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL); |
92825ff9 HK |
926 | if (iio_attr == NULL) |
927 | return -ENOMEM; | |
1d892719 JC |
928 | ret = __iio_device_attr_init(&iio_attr->dev_attr, |
929 | postfix, chan, | |
3704432f | 930 | readfunc, writefunc, shared_by); |
1d892719 JC |
931 | if (ret) |
932 | goto error_iio_dev_attr_free; | |
933 | iio_attr->c = chan; | |
934 | iio_attr->address = mask; | |
935 | list_for_each_entry(t, attr_list, l) | |
936 | if (strcmp(t->dev_attr.attr.name, | |
937 | iio_attr->dev_attr.attr.name) == 0) { | |
3704432f | 938 | if (shared_by == IIO_SEPARATE) |
1d892719 JC |
939 | dev_err(dev, "tried to double register : %s\n", |
940 | t->dev_attr.attr.name); | |
941 | ret = -EBUSY; | |
942 | goto error_device_attr_deinit; | |
943 | } | |
1d892719 JC |
944 | list_add(&iio_attr->l, attr_list); |
945 | ||
946 | return 0; | |
947 | ||
948 | error_device_attr_deinit: | |
949 | __iio_device_attr_deinit(&iio_attr->dev_attr); | |
950 | error_iio_dev_attr_free: | |
951 | kfree(iio_attr); | |
1d892719 JC |
952 | return ret; |
953 | } | |
954 | ||
3704432f JC |
955 | static int iio_device_add_info_mask_type(struct iio_dev *indio_dev, |
956 | struct iio_chan_spec const *chan, | |
957 | enum iio_shared_by shared_by, | |
958 | const long *infomask) | |
1d892719 | 959 | { |
3704432f | 960 | int i, ret, attrcount = 0; |
1d892719 | 961 | |
3704432f | 962 | for_each_set_bit(i, infomask, sizeof(infomask)*8) { |
ef4b4856 JC |
963 | if (i >= ARRAY_SIZE(iio_chan_info_postfix)) |
964 | return -EINVAL; | |
8655cc49 JC |
965 | ret = __iio_add_chan_devattr(iio_chan_info_postfix[i], |
966 | chan, | |
967 | &iio_read_channel_info, | |
968 | &iio_write_channel_info, | |
969 | i, | |
3704432f | 970 | shared_by, |
8655cc49 JC |
971 | &indio_dev->dev, |
972 | &indio_dev->channel_attr_list); | |
3704432f | 973 | if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) |
8655cc49 | 974 | continue; |
3704432f JC |
975 | else if (ret < 0) |
976 | return ret; | |
8655cc49 JC |
977 | attrcount++; |
978 | } | |
5f420b42 | 979 | |
3704432f JC |
980 | return attrcount; |
981 | } | |
982 | ||
983 | static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev, | |
984 | struct iio_chan_spec const *chan) | |
985 | { | |
986 | int ret, attrcount = 0; | |
987 | const struct iio_chan_spec_ext_info *ext_info; | |
988 | ||
989 | if (chan->channel < 0) | |
990 | return 0; | |
991 | ret = iio_device_add_info_mask_type(indio_dev, chan, | |
992 | IIO_SEPARATE, | |
993 | &chan->info_mask_separate); | |
994 | if (ret < 0) | |
995 | return ret; | |
996 | attrcount += ret; | |
997 | ||
998 | ret = iio_device_add_info_mask_type(indio_dev, chan, | |
999 | IIO_SHARED_BY_TYPE, | |
1000 | &chan->info_mask_shared_by_type); | |
1001 | if (ret < 0) | |
1002 | return ret; | |
1003 | attrcount += ret; | |
1004 | ||
c006ec83 JC |
1005 | ret = iio_device_add_info_mask_type(indio_dev, chan, |
1006 | IIO_SHARED_BY_DIR, | |
1007 | &chan->info_mask_shared_by_dir); | |
1008 | if (ret < 0) | |
1009 | return ret; | |
1010 | attrcount += ret; | |
1011 | ||
1012 | ret = iio_device_add_info_mask_type(indio_dev, chan, | |
1013 | IIO_SHARED_BY_ALL, | |
1014 | &chan->info_mask_shared_by_all); | |
1015 | if (ret < 0) | |
1016 | return ret; | |
1017 | attrcount += ret; | |
1018 | ||
5f420b42 LPC |
1019 | if (chan->ext_info) { |
1020 | unsigned int i = 0; | |
1021 | for (ext_info = chan->ext_info; ext_info->name; ext_info++) { | |
1022 | ret = __iio_add_chan_devattr(ext_info->name, | |
1023 | chan, | |
1024 | ext_info->read ? | |
1025 | &iio_read_channel_ext_info : NULL, | |
1026 | ext_info->write ? | |
1027 | &iio_write_channel_ext_info : NULL, | |
1028 | i, | |
1029 | ext_info->shared, | |
1030 | &indio_dev->dev, | |
1031 | &indio_dev->channel_attr_list); | |
1032 | i++; | |
1033 | if (ret == -EBUSY && ext_info->shared) | |
1034 | continue; | |
1035 | ||
1036 | if (ret) | |
3704432f | 1037 | return ret; |
5f420b42 LPC |
1038 | |
1039 | attrcount++; | |
1040 | } | |
1041 | } | |
1042 | ||
3704432f | 1043 | return attrcount; |
1d892719 JC |
1044 | } |
1045 | ||
84088ebd LPC |
1046 | /** |
1047 | * iio_free_chan_devattr_list() - Free a list of IIO device attributes | |
1048 | * @attr_list: List of IIO device attributes | |
1049 | * | |
1050 | * This function frees the memory allocated for each of the IIO device | |
c1b03ab5 | 1051 | * attributes in the list. |
84088ebd LPC |
1052 | */ |
1053 | void iio_free_chan_devattr_list(struct list_head *attr_list) | |
1d892719 | 1054 | { |
84088ebd LPC |
1055 | struct iio_dev_attr *p, *n; |
1056 | ||
1057 | list_for_each_entry_safe(p, n, attr_list, l) { | |
1058 | kfree(p->dev_attr.attr.name); | |
c1b03ab5 | 1059 | list_del(&p->l); |
84088ebd LPC |
1060 | kfree(p); |
1061 | } | |
1d892719 JC |
1062 | } |
1063 | ||
1b732888 JC |
1064 | static ssize_t iio_show_dev_name(struct device *dev, |
1065 | struct device_attribute *attr, | |
1066 | char *buf) | |
1067 | { | |
e53f5ac5 | 1068 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
598db581 | 1069 | return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name); |
1b732888 JC |
1070 | } |
1071 | ||
1072 | static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL); | |
1073 | ||
bc2b7dab GB |
1074 | static ssize_t iio_show_timestamp_clock(struct device *dev, |
1075 | struct device_attribute *attr, | |
1076 | char *buf) | |
1077 | { | |
1078 | const struct iio_dev *indio_dev = dev_to_iio_dev(dev); | |
1079 | const clockid_t clk = iio_device_get_clock(indio_dev); | |
1080 | const char *name; | |
1081 | ssize_t sz; | |
1082 | ||
1083 | switch (clk) { | |
1084 | case CLOCK_REALTIME: | |
1085 | name = "realtime\n"; | |
1086 | sz = sizeof("realtime\n"); | |
1087 | break; | |
1088 | case CLOCK_MONOTONIC: | |
1089 | name = "monotonic\n"; | |
1090 | sz = sizeof("monotonic\n"); | |
1091 | break; | |
1092 | case CLOCK_MONOTONIC_RAW: | |
1093 | name = "monotonic_raw\n"; | |
1094 | sz = sizeof("monotonic_raw\n"); | |
1095 | break; | |
1096 | case CLOCK_REALTIME_COARSE: | |
1097 | name = "realtime_coarse\n"; | |
1098 | sz = sizeof("realtime_coarse\n"); | |
1099 | break; | |
1100 | case CLOCK_MONOTONIC_COARSE: | |
1101 | name = "monotonic_coarse\n"; | |
1102 | sz = sizeof("monotonic_coarse\n"); | |
1103 | break; | |
1104 | case CLOCK_BOOTTIME: | |
1105 | name = "boottime\n"; | |
1106 | sz = sizeof("boottime\n"); | |
1107 | break; | |
1108 | case CLOCK_TAI: | |
1109 | name = "tai\n"; | |
1110 | sz = sizeof("tai\n"); | |
1111 | break; | |
1112 | default: | |
1113 | BUG(); | |
1114 | } | |
1115 | ||
1116 | memcpy(buf, name, sz); | |
1117 | return sz; | |
1118 | } | |
1119 | ||
1120 | static ssize_t iio_store_timestamp_clock(struct device *dev, | |
1121 | struct device_attribute *attr, | |
1122 | const char *buf, size_t len) | |
1123 | { | |
1124 | clockid_t clk; | |
1125 | int ret; | |
1126 | ||
1127 | if (sysfs_streq(buf, "realtime")) | |
1128 | clk = CLOCK_REALTIME; | |
1129 | else if (sysfs_streq(buf, "monotonic")) | |
1130 | clk = CLOCK_MONOTONIC; | |
1131 | else if (sysfs_streq(buf, "monotonic_raw")) | |
1132 | clk = CLOCK_MONOTONIC_RAW; | |
1133 | else if (sysfs_streq(buf, "realtime_coarse")) | |
1134 | clk = CLOCK_REALTIME_COARSE; | |
1135 | else if (sysfs_streq(buf, "monotonic_coarse")) | |
1136 | clk = CLOCK_MONOTONIC_COARSE; | |
1137 | else if (sysfs_streq(buf, "boottime")) | |
1138 | clk = CLOCK_BOOTTIME; | |
1139 | else if (sysfs_streq(buf, "tai")) | |
1140 | clk = CLOCK_TAI; | |
1141 | else | |
1142 | return -EINVAL; | |
1143 | ||
1144 | ret = iio_device_set_clock(dev_to_iio_dev(dev), clk); | |
1145 | if (ret) | |
1146 | return ret; | |
1147 | ||
1148 | return len; | |
1149 | } | |
1150 | ||
1151 | static DEVICE_ATTR(current_timestamp_clock, S_IRUGO | S_IWUSR, | |
1152 | iio_show_timestamp_clock, iio_store_timestamp_clock); | |
1153 | ||
f8c6f4e9 | 1154 | static int iio_device_register_sysfs(struct iio_dev *indio_dev) |
1d892719 | 1155 | { |
26d25ae3 | 1156 | int i, ret = 0, attrcount, attrn, attrcount_orig = 0; |
84088ebd | 1157 | struct iio_dev_attr *p; |
bc2b7dab | 1158 | struct attribute **attr, *clk = NULL; |
1d892719 | 1159 | |
26d25ae3 | 1160 | /* First count elements in any existing group */ |
f8c6f4e9 JC |
1161 | if (indio_dev->info->attrs) { |
1162 | attr = indio_dev->info->attrs->attrs; | |
26d25ae3 JC |
1163 | while (*attr++ != NULL) |
1164 | attrcount_orig++; | |
847ec80b | 1165 | } |
26d25ae3 | 1166 | attrcount = attrcount_orig; |
1d892719 JC |
1167 | /* |
1168 | * New channel registration method - relies on the fact a group does | |
d25b3808 | 1169 | * not need to be initialized if its name is NULL. |
1d892719 | 1170 | */ |
f8c6f4e9 JC |
1171 | if (indio_dev->channels) |
1172 | for (i = 0; i < indio_dev->num_channels; i++) { | |
bc2b7dab GB |
1173 | const struct iio_chan_spec *chan = |
1174 | &indio_dev->channels[i]; | |
1175 | ||
1176 | if (chan->type == IIO_TIMESTAMP) | |
1177 | clk = &dev_attr_current_timestamp_clock.attr; | |
1178 | ||
1179 | ret = iio_device_add_channel_sysfs(indio_dev, chan); | |
1d892719 JC |
1180 | if (ret < 0) |
1181 | goto error_clear_attrs; | |
26d25ae3 | 1182 | attrcount += ret; |
1d892719 | 1183 | } |
26d25ae3 | 1184 | |
bc2b7dab GB |
1185 | if (indio_dev->event_interface) |
1186 | clk = &dev_attr_current_timestamp_clock.attr; | |
1187 | ||
f8c6f4e9 | 1188 | if (indio_dev->name) |
26d25ae3 | 1189 | attrcount++; |
bc2b7dab GB |
1190 | if (clk) |
1191 | attrcount++; | |
26d25ae3 | 1192 | |
d83fb184 TM |
1193 | indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1, |
1194 | sizeof(indio_dev->chan_attr_group.attrs[0]), | |
1195 | GFP_KERNEL); | |
f8c6f4e9 | 1196 | if (indio_dev->chan_attr_group.attrs == NULL) { |
26d25ae3 JC |
1197 | ret = -ENOMEM; |
1198 | goto error_clear_attrs; | |
1b732888 | 1199 | } |
26d25ae3 | 1200 | /* Copy across original attributes */ |
f8c6f4e9 JC |
1201 | if (indio_dev->info->attrs) |
1202 | memcpy(indio_dev->chan_attr_group.attrs, | |
1203 | indio_dev->info->attrs->attrs, | |
1204 | sizeof(indio_dev->chan_attr_group.attrs[0]) | |
26d25ae3 JC |
1205 | *attrcount_orig); |
1206 | attrn = attrcount_orig; | |
1207 | /* Add all elements from the list. */ | |
f8c6f4e9 JC |
1208 | list_for_each_entry(p, &indio_dev->channel_attr_list, l) |
1209 | indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr; | |
1210 | if (indio_dev->name) | |
1211 | indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr; | |
bc2b7dab GB |
1212 | if (clk) |
1213 | indio_dev->chan_attr_group.attrs[attrn++] = clk; | |
26d25ae3 | 1214 | |
f8c6f4e9 JC |
1215 | indio_dev->groups[indio_dev->groupcounter++] = |
1216 | &indio_dev->chan_attr_group; | |
26d25ae3 | 1217 | |
1d892719 | 1218 | return 0; |
1b732888 | 1219 | |
1d892719 | 1220 | error_clear_attrs: |
84088ebd | 1221 | iio_free_chan_devattr_list(&indio_dev->channel_attr_list); |
1d892719 | 1222 | |
26d25ae3 | 1223 | return ret; |
847ec80b JC |
1224 | } |
1225 | ||
f8c6f4e9 | 1226 | static void iio_device_unregister_sysfs(struct iio_dev *indio_dev) |
847ec80b | 1227 | { |
1d892719 | 1228 | |
84088ebd | 1229 | iio_free_chan_devattr_list(&indio_dev->channel_attr_list); |
f8c6f4e9 | 1230 | kfree(indio_dev->chan_attr_group.attrs); |
c1b03ab5 | 1231 | indio_dev->chan_attr_group.attrs = NULL; |
847ec80b JC |
1232 | } |
1233 | ||
847ec80b JC |
1234 | static void iio_dev_release(struct device *device) |
1235 | { | |
e53f5ac5 | 1236 | struct iio_dev *indio_dev = dev_to_iio_dev(device); |
735ad074 | 1237 | if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED)) |
f8c6f4e9 JC |
1238 | iio_device_unregister_trigger_consumer(indio_dev); |
1239 | iio_device_unregister_eventset(indio_dev); | |
1240 | iio_device_unregister_sysfs(indio_dev); | |
e407fd65 | 1241 | |
9e69c935 LPC |
1242 | iio_buffer_put(indio_dev->buffer); |
1243 | ||
e407fd65 LPC |
1244 | ida_simple_remove(&iio_ida, indio_dev->id); |
1245 | kfree(indio_dev); | |
847ec80b JC |
1246 | } |
1247 | ||
17d82b47 | 1248 | struct device_type iio_device_type = { |
847ec80b JC |
1249 | .name = "iio_device", |
1250 | .release = iio_dev_release, | |
1251 | }; | |
1252 | ||
a7e57dce SK |
1253 | /** |
1254 | * iio_device_alloc() - allocate an iio_dev from a driver | |
1255 | * @sizeof_priv: Space to allocate for private structure. | |
1256 | **/ | |
7cbb7537 | 1257 | struct iio_dev *iio_device_alloc(int sizeof_priv) |
847ec80b | 1258 | { |
6f7c8ee5 JC |
1259 | struct iio_dev *dev; |
1260 | size_t alloc_size; | |
1261 | ||
1262 | alloc_size = sizeof(struct iio_dev); | |
1263 | if (sizeof_priv) { | |
1264 | alloc_size = ALIGN(alloc_size, IIO_ALIGN); | |
1265 | alloc_size += sizeof_priv; | |
1266 | } | |
1267 | /* ensure 32-byte alignment of whole construct ? */ | |
1268 | alloc_size += IIO_ALIGN - 1; | |
1269 | ||
1270 | dev = kzalloc(alloc_size, GFP_KERNEL); | |
847ec80b JC |
1271 | |
1272 | if (dev) { | |
26d25ae3 | 1273 | dev->dev.groups = dev->groups; |
17d82b47 | 1274 | dev->dev.type = &iio_device_type; |
5aaaeba8 | 1275 | dev->dev.bus = &iio_bus_type; |
847ec80b JC |
1276 | device_initialize(&dev->dev); |
1277 | dev_set_drvdata(&dev->dev, (void *)dev); | |
1278 | mutex_init(&dev->mlock); | |
ac917a81 | 1279 | mutex_init(&dev->info_exist_lock); |
e407fd65 | 1280 | INIT_LIST_HEAD(&dev->channel_attr_list); |
a9e39f9e JC |
1281 | |
1282 | dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL); | |
1283 | if (dev->id < 0) { | |
1284 | /* cannot use a dev_err as the name isn't available */ | |
3176dd5d | 1285 | pr_err("failed to get device id\n"); |
a9e39f9e JC |
1286 | kfree(dev); |
1287 | return NULL; | |
1288 | } | |
1289 | dev_set_name(&dev->dev, "iio:device%d", dev->id); | |
84b36ce5 | 1290 | INIT_LIST_HEAD(&dev->buffer_list); |
847ec80b JC |
1291 | } |
1292 | ||
1293 | return dev; | |
1294 | } | |
7cbb7537 | 1295 | EXPORT_SYMBOL(iio_device_alloc); |
847ec80b | 1296 | |
a7e57dce SK |
1297 | /** |
1298 | * iio_device_free() - free an iio_dev from a driver | |
1299 | * @dev: the iio_dev associated with the device | |
1300 | **/ | |
7cbb7537 | 1301 | void iio_device_free(struct iio_dev *dev) |
847ec80b | 1302 | { |
e407fd65 LPC |
1303 | if (dev) |
1304 | put_device(&dev->dev); | |
847ec80b | 1305 | } |
7cbb7537 | 1306 | EXPORT_SYMBOL(iio_device_free); |
847ec80b | 1307 | |
9dabaf5e GS |
1308 | static void devm_iio_device_release(struct device *dev, void *res) |
1309 | { | |
1310 | iio_device_free(*(struct iio_dev **)res); | |
1311 | } | |
1312 | ||
70e48348 | 1313 | int devm_iio_device_match(struct device *dev, void *res, void *data) |
9dabaf5e GS |
1314 | { |
1315 | struct iio_dev **r = res; | |
1316 | if (!r || !*r) { | |
1317 | WARN_ON(!r || !*r); | |
1318 | return 0; | |
1319 | } | |
1320 | return *r == data; | |
1321 | } | |
70e48348 | 1322 | EXPORT_SYMBOL_GPL(devm_iio_device_match); |
9dabaf5e | 1323 | |
a7e57dce SK |
1324 | /** |
1325 | * devm_iio_device_alloc - Resource-managed iio_device_alloc() | |
1326 | * @dev: Device to allocate iio_dev for | |
1327 | * @sizeof_priv: Space to allocate for private structure. | |
1328 | * | |
1329 | * Managed iio_device_alloc. iio_dev allocated with this function is | |
1330 | * automatically freed on driver detach. | |
1331 | * | |
1332 | * If an iio_dev allocated with this function needs to be freed separately, | |
1333 | * devm_iio_device_free() must be used. | |
1334 | * | |
1335 | * RETURNS: | |
1336 | * Pointer to allocated iio_dev on success, NULL on failure. | |
1337 | */ | |
9dabaf5e GS |
1338 | struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv) |
1339 | { | |
1340 | struct iio_dev **ptr, *iio_dev; | |
1341 | ||
1342 | ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr), | |
1343 | GFP_KERNEL); | |
1344 | if (!ptr) | |
1345 | return NULL; | |
1346 | ||
9dabaf5e GS |
1347 | iio_dev = iio_device_alloc(sizeof_priv); |
1348 | if (iio_dev) { | |
1349 | *ptr = iio_dev; | |
1350 | devres_add(dev, ptr); | |
1351 | } else { | |
1352 | devres_free(ptr); | |
1353 | } | |
1354 | ||
1355 | return iio_dev; | |
1356 | } | |
1357 | EXPORT_SYMBOL_GPL(devm_iio_device_alloc); | |
1358 | ||
a7e57dce SK |
1359 | /** |
1360 | * devm_iio_device_free - Resource-managed iio_device_free() | |
1361 | * @dev: Device this iio_dev belongs to | |
1362 | * @iio_dev: the iio_dev associated with the device | |
1363 | * | |
1364 | * Free iio_dev allocated with devm_iio_device_alloc(). | |
1365 | */ | |
9dabaf5e GS |
1366 | void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev) |
1367 | { | |
1368 | int rc; | |
1369 | ||
1370 | rc = devres_release(dev, devm_iio_device_release, | |
1371 | devm_iio_device_match, iio_dev); | |
1372 | WARN_ON(rc); | |
1373 | } | |
1374 | EXPORT_SYMBOL_GPL(devm_iio_device_free); | |
1375 | ||
1aa04278 | 1376 | /** |
14555b14 | 1377 | * iio_chrdev_open() - chrdev file open for buffer access and ioctls |
2498dcf6 CO |
1378 | * @inode: Inode structure for identifying the device in the file system |
1379 | * @filp: File structure for iio device used to keep and later access | |
1380 | * private data | |
1381 | * | |
1382 | * Return: 0 on success or -EBUSY if the device is already opened | |
1aa04278 JC |
1383 | **/ |
1384 | static int iio_chrdev_open(struct inode *inode, struct file *filp) | |
1385 | { | |
f8c6f4e9 | 1386 | struct iio_dev *indio_dev = container_of(inode->i_cdev, |
1aa04278 | 1387 | struct iio_dev, chrdev); |
bb01443e LPC |
1388 | |
1389 | if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags)) | |
1390 | return -EBUSY; | |
1391 | ||
cadc2125 LPC |
1392 | iio_device_get(indio_dev); |
1393 | ||
f8c6f4e9 | 1394 | filp->private_data = indio_dev; |
30eb82f0 | 1395 | |
79335140 | 1396 | return 0; |
1aa04278 JC |
1397 | } |
1398 | ||
1399 | /** | |
14555b14 | 1400 | * iio_chrdev_release() - chrdev file close buffer access and ioctls |
2498dcf6 CO |
1401 | * @inode: Inode structure pointer for the char device |
1402 | * @filp: File structure pointer for the char device | |
1403 | * | |
1404 | * Return: 0 for successful release | |
1405 | */ | |
1aa04278 JC |
1406 | static int iio_chrdev_release(struct inode *inode, struct file *filp) |
1407 | { | |
bb01443e LPC |
1408 | struct iio_dev *indio_dev = container_of(inode->i_cdev, |
1409 | struct iio_dev, chrdev); | |
bb01443e | 1410 | clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags); |
cadc2125 LPC |
1411 | iio_device_put(indio_dev); |
1412 | ||
1aa04278 JC |
1413 | return 0; |
1414 | } | |
1415 | ||
1416 | /* Somewhat of a cross file organization violation - ioctls here are actually | |
1417 | * event related */ | |
1418 | static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) | |
1419 | { | |
1420 | struct iio_dev *indio_dev = filp->private_data; | |
1421 | int __user *ip = (int __user *)arg; | |
1422 | int fd; | |
1423 | ||
f18e7a06 LPC |
1424 | if (!indio_dev->info) |
1425 | return -ENODEV; | |
1426 | ||
1aa04278 JC |
1427 | if (cmd == IIO_GET_EVENT_FD_IOCTL) { |
1428 | fd = iio_event_getfd(indio_dev); | |
3f9059b7 LW |
1429 | if (fd < 0) |
1430 | return fd; | |
1aa04278 JC |
1431 | if (copy_to_user(ip, &fd, sizeof(fd))) |
1432 | return -EFAULT; | |
1433 | return 0; | |
1434 | } | |
1435 | return -EINVAL; | |
1436 | } | |
1437 | ||
14555b14 JC |
1438 | static const struct file_operations iio_buffer_fileops = { |
1439 | .read = iio_buffer_read_first_n_outer_addr, | |
1aa04278 JC |
1440 | .release = iio_chrdev_release, |
1441 | .open = iio_chrdev_open, | |
14555b14 | 1442 | .poll = iio_buffer_poll_addr, |
1aa04278 JC |
1443 | .owner = THIS_MODULE, |
1444 | .llseek = noop_llseek, | |
1445 | .unlocked_ioctl = iio_ioctl, | |
1446 | .compat_ioctl = iio_ioctl, | |
1447 | }; | |
1448 | ||
8f5d8727 VD |
1449 | static int iio_check_unique_scan_index(struct iio_dev *indio_dev) |
1450 | { | |
1451 | int i, j; | |
1452 | const struct iio_chan_spec *channels = indio_dev->channels; | |
1453 | ||
1454 | if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES)) | |
1455 | return 0; | |
1456 | ||
1457 | for (i = 0; i < indio_dev->num_channels - 1; i++) { | |
1458 | if (channels[i].scan_index < 0) | |
1459 | continue; | |
1460 | for (j = i + 1; j < indio_dev->num_channels; j++) | |
1461 | if (channels[i].scan_index == channels[j].scan_index) { | |
1462 | dev_err(&indio_dev->dev, | |
1463 | "Duplicate scan index %d\n", | |
1464 | channels[i].scan_index); | |
1465 | return -EINVAL; | |
1466 | } | |
1467 | } | |
1468 | ||
1469 | return 0; | |
1470 | } | |
1471 | ||
0f1acee5 MH |
1472 | static const struct iio_buffer_setup_ops noop_ring_setup_ops; |
1473 | ||
a7e57dce SK |
1474 | /** |
1475 | * iio_device_register() - register a device with the IIO subsystem | |
1476 | * @indio_dev: Device structure filled by the device driver | |
1477 | **/ | |
f8c6f4e9 | 1478 | int iio_device_register(struct iio_dev *indio_dev) |
847ec80b JC |
1479 | { |
1480 | int ret; | |
1481 | ||
17d82b47 GR |
1482 | /* If the calling driver did not initialize of_node, do it here */ |
1483 | if (!indio_dev->dev.of_node && indio_dev->dev.parent) | |
1484 | indio_dev->dev.of_node = indio_dev->dev.parent->of_node; | |
1485 | ||
8f5d8727 VD |
1486 | ret = iio_check_unique_scan_index(indio_dev); |
1487 | if (ret < 0) | |
1488 | return ret; | |
1489 | ||
1aa04278 | 1490 | /* configure elements for the chrdev */ |
f8c6f4e9 | 1491 | indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id); |
847ec80b | 1492 | |
e553f182 MH |
1493 | ret = iio_device_register_debugfs(indio_dev); |
1494 | if (ret) { | |
1495 | dev_err(indio_dev->dev.parent, | |
1496 | "Failed to register debugfs interfaces\n"); | |
92825ff9 | 1497 | return ret; |
e553f182 | 1498 | } |
3e1b6c95 LPC |
1499 | |
1500 | ret = iio_buffer_alloc_sysfs_and_mask(indio_dev); | |
1501 | if (ret) { | |
1502 | dev_err(indio_dev->dev.parent, | |
1503 | "Failed to create buffer sysfs interfaces\n"); | |
1504 | goto error_unreg_debugfs; | |
1505 | } | |
1506 | ||
f8c6f4e9 | 1507 | ret = iio_device_register_sysfs(indio_dev); |
847ec80b | 1508 | if (ret) { |
f8c6f4e9 | 1509 | dev_err(indio_dev->dev.parent, |
847ec80b | 1510 | "Failed to register sysfs interfaces\n"); |
3e1b6c95 | 1511 | goto error_buffer_free_sysfs; |
847ec80b | 1512 | } |
f8c6f4e9 | 1513 | ret = iio_device_register_eventset(indio_dev); |
847ec80b | 1514 | if (ret) { |
f8c6f4e9 | 1515 | dev_err(indio_dev->dev.parent, |
c849d253 | 1516 | "Failed to register event set\n"); |
847ec80b JC |
1517 | goto error_free_sysfs; |
1518 | } | |
735ad074 | 1519 | if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED)) |
f8c6f4e9 | 1520 | iio_device_register_trigger_consumer(indio_dev); |
847ec80b | 1521 | |
0f1acee5 MH |
1522 | if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) && |
1523 | indio_dev->setup_ops == NULL) | |
1524 | indio_dev->setup_ops = &noop_ring_setup_ops; | |
1525 | ||
f8c6f4e9 JC |
1526 | cdev_init(&indio_dev->chrdev, &iio_buffer_fileops); |
1527 | indio_dev->chrdev.owner = indio_dev->info->driver_module; | |
0d5b7dae | 1528 | indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj; |
f8c6f4e9 | 1529 | ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1); |
26d25ae3 | 1530 | if (ret < 0) |
0d5b7dae | 1531 | goto error_unreg_eventset; |
847ec80b | 1532 | |
0d5b7dae LPC |
1533 | ret = device_add(&indio_dev->dev); |
1534 | if (ret < 0) | |
1535 | goto error_cdev_del; | |
1536 | ||
1537 | return 0; | |
1538 | error_cdev_del: | |
1539 | cdev_del(&indio_dev->chrdev); | |
26d25ae3 | 1540 | error_unreg_eventset: |
f8c6f4e9 | 1541 | iio_device_unregister_eventset(indio_dev); |
26d25ae3 | 1542 | error_free_sysfs: |
f8c6f4e9 | 1543 | iio_device_unregister_sysfs(indio_dev); |
3e1b6c95 LPC |
1544 | error_buffer_free_sysfs: |
1545 | iio_buffer_free_sysfs_and_mask(indio_dev); | |
e553f182 MH |
1546 | error_unreg_debugfs: |
1547 | iio_device_unregister_debugfs(indio_dev); | |
847ec80b JC |
1548 | return ret; |
1549 | } | |
1550 | EXPORT_SYMBOL(iio_device_register); | |
1551 | ||
a7e57dce SK |
1552 | /** |
1553 | * iio_device_unregister() - unregister a device from the IIO subsystem | |
1554 | * @indio_dev: Device structure representing the device. | |
1555 | **/ | |
f8c6f4e9 | 1556 | void iio_device_unregister(struct iio_dev *indio_dev) |
847ec80b | 1557 | { |
ac917a81 | 1558 | mutex_lock(&indio_dev->info_exist_lock); |
a87c82e4 LPC |
1559 | |
1560 | device_del(&indio_dev->dev); | |
1561 | ||
0d5b7dae LPC |
1562 | if (indio_dev->chrdev.dev) |
1563 | cdev_del(&indio_dev->chrdev); | |
bc4c9612 | 1564 | iio_device_unregister_debugfs(indio_dev); |
0d5b7dae | 1565 | |
a87c82e4 LPC |
1566 | iio_disable_all_buffers(indio_dev); |
1567 | ||
ac917a81 | 1568 | indio_dev->info = NULL; |
d2f0a48f LPC |
1569 | |
1570 | iio_device_wakeup_eventset(indio_dev); | |
1571 | iio_buffer_wakeup_poll(indio_dev); | |
1572 | ||
ac917a81 | 1573 | mutex_unlock(&indio_dev->info_exist_lock); |
3e1b6c95 LPC |
1574 | |
1575 | iio_buffer_free_sysfs_and_mask(indio_dev); | |
847ec80b JC |
1576 | } |
1577 | EXPORT_SYMBOL(iio_device_unregister); | |
8caa07c0 SK |
1578 | |
1579 | static void devm_iio_device_unreg(struct device *dev, void *res) | |
1580 | { | |
1581 | iio_device_unregister(*(struct iio_dev **)res); | |
1582 | } | |
1583 | ||
1584 | /** | |
1585 | * devm_iio_device_register - Resource-managed iio_device_register() | |
1586 | * @dev: Device to allocate iio_dev for | |
1587 | * @indio_dev: Device structure filled by the device driver | |
1588 | * | |
1589 | * Managed iio_device_register. The IIO device registered with this | |
1590 | * function is automatically unregistered on driver detach. This function | |
1591 | * calls iio_device_register() internally. Refer to that function for more | |
1592 | * information. | |
1593 | * | |
1594 | * If an iio_dev registered with this function needs to be unregistered | |
1595 | * separately, devm_iio_device_unregister() must be used. | |
1596 | * | |
1597 | * RETURNS: | |
1598 | * 0 on success, negative error number on failure. | |
1599 | */ | |
1600 | int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev) | |
1601 | { | |
1602 | struct iio_dev **ptr; | |
1603 | int ret; | |
1604 | ||
1605 | ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL); | |
1606 | if (!ptr) | |
1607 | return -ENOMEM; | |
1608 | ||
1609 | *ptr = indio_dev; | |
1610 | ret = iio_device_register(indio_dev); | |
1611 | if (!ret) | |
1612 | devres_add(dev, ptr); | |
1613 | else | |
1614 | devres_free(ptr); | |
1615 | ||
1616 | return ret; | |
1617 | } | |
1618 | EXPORT_SYMBOL_GPL(devm_iio_device_register); | |
1619 | ||
1620 | /** | |
1621 | * devm_iio_device_unregister - Resource-managed iio_device_unregister() | |
1622 | * @dev: Device this iio_dev belongs to | |
1623 | * @indio_dev: the iio_dev associated with the device | |
1624 | * | |
1625 | * Unregister iio_dev registered with devm_iio_device_register(). | |
1626 | */ | |
1627 | void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev) | |
1628 | { | |
1629 | int rc; | |
1630 | ||
1631 | rc = devres_release(dev, devm_iio_device_unreg, | |
1632 | devm_iio_device_match, indio_dev); | |
1633 | WARN_ON(rc); | |
1634 | } | |
1635 | EXPORT_SYMBOL_GPL(devm_iio_device_unregister); | |
1636 | ||
08a33805 AS |
1637 | /** |
1638 | * iio_device_claim_direct_mode - Keep device in direct mode | |
1639 | * @indio_dev: the iio_dev associated with the device | |
1640 | * | |
1641 | * If the device is in direct mode it is guaranteed to stay | |
1642 | * that way until iio_device_release_direct_mode() is called. | |
1643 | * | |
1644 | * Use with iio_device_release_direct_mode() | |
1645 | * | |
1646 | * Returns: 0 on success, -EBUSY on failure | |
1647 | */ | |
1648 | int iio_device_claim_direct_mode(struct iio_dev *indio_dev) | |
1649 | { | |
1650 | mutex_lock(&indio_dev->mlock); | |
1651 | ||
1652 | if (iio_buffer_enabled(indio_dev)) { | |
1653 | mutex_unlock(&indio_dev->mlock); | |
1654 | return -EBUSY; | |
1655 | } | |
1656 | return 0; | |
1657 | } | |
1658 | EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode); | |
1659 | ||
1660 | /** | |
1661 | * iio_device_release_direct_mode - releases claim on direct mode | |
1662 | * @indio_dev: the iio_dev associated with the device | |
1663 | * | |
1664 | * Release the claim. Device is no longer guaranteed to stay | |
1665 | * in direct mode. | |
1666 | * | |
1667 | * Use with iio_device_claim_direct_mode() | |
1668 | */ | |
1669 | void iio_device_release_direct_mode(struct iio_dev *indio_dev) | |
1670 | { | |
1671 | mutex_unlock(&indio_dev->mlock); | |
1672 | } | |
1673 | EXPORT_SYMBOL_GPL(iio_device_release_direct_mode); | |
1674 | ||
847ec80b JC |
1675 | subsys_initcall(iio_init); |
1676 | module_exit(iio_exit); | |
1677 | ||
c8b95952 | 1678 | MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); |
847ec80b JC |
1679 | MODULE_DESCRIPTION("Industrial I/O core"); |
1680 | MODULE_LICENSE("GPL"); |