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