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