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