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