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