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