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