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