blob: af7cc1e65656e1bf0ac83f561945bbbcf45d9de9 [file] [log] [blame]
Jonathan Cameron847ec802009-08-18 18:06:19 +01001/* 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
Sachin Kamat3176dd52013-10-24 12:53:00 +010012#define pr_fmt(fmt) "iio-core: " fmt
13
Jonathan Cameron847ec802009-08-18 18:06:19 +010014#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>
Jonathan Cameron847ec802009-08-18 18:06:19 +010021#include <linux/poll.h>
Jonathan Cameronffc18af2009-10-12 19:18:09 +010022#include <linux/sched.h>
Jeff Mahoney4439c932009-10-12 17:10:34 -040023#include <linux/wait.h>
Jonathan Cameron847ec802009-08-18 18:06:19 +010024#include <linux/cdev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Jonathan Cameron8e7d9672011-08-30 12:32:45 +010026#include <linux/anon_inodes.h>
Michael Henneriche553f182012-03-01 10:51:03 +010027#include <linux/debugfs.h>
Jonathan Cameron06458e22012-04-25 15:54:58 +010028#include <linux/iio/iio.h>
Jonathan Camerondf9c1c42011-08-12 17:56:03 +010029#include "iio_core.h"
Jonathan Cameron6aea1c32011-08-24 17:28:38 +010030#include "iio_core_trigger.h"
Jonathan Cameron06458e22012-04-25 15:54:58 +010031#include <linux/iio/sysfs.h>
32#include <linux/iio/events.h>
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +010033#include <linux/iio/buffer.h>
Jonathan Cameron9dd1cb32011-08-30 12:41:15 +010034
Peter Meerwald99698b42012-08-26 13:43:00 +010035/* IDA to assign each registered device a unique id */
Jonathan Cameronb156cf72010-09-04 17:54:43 +010036static DEFINE_IDA(iio_ida);
Jonathan Cameron847ec802009-08-18 18:06:19 +010037
Jonathan Cameronf625cb92011-08-30 12:32:48 +010038static dev_t iio_devt;
Jonathan Cameron847ec802009-08-18 18:06:19 +010039
40#define IIO_DEV_MAX 256
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +010041struct bus_type iio_bus_type = {
Jonathan Cameron847ec802009-08-18 18:06:19 +010042 .name = "iio",
Jonathan Cameron847ec802009-08-18 18:06:19 +010043};
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +010044EXPORT_SYMBOL(iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +010045
Michael Henneriche553f182012-03-01 10:51:03 +010046static struct dentry *iio_debugfs_dentry;
47
Jonathan Cameronc6fc8062011-09-02 17:14:34 +010048static const char * const iio_direction[] = {
49 [0] = "in",
50 [1] = "out",
51};
52
Jonathan Cameronade7ef72011-09-02 17:14:45 +010053static const char * const iio_chan_type_name_spec[] = {
Jonathan Cameronc6fc8062011-09-02 17:14:34 +010054 [IIO_VOLTAGE] = "voltage",
Michael Hennerichfaf290e2011-05-18 14:42:02 +010055 [IIO_CURRENT] = "current",
56 [IIO_POWER] = "power",
Bryan Freed9bff02f2011-07-07 12:01:54 -070057 [IIO_ACCEL] = "accel",
Jonathan Cameron41ea0402011-10-05 15:27:59 +010058 [IIO_ANGL_VEL] = "anglvel",
Jonathan Cameron1d892712011-05-18 14:40:51 +010059 [IIO_MAGN] = "magn",
Bryan Freed9bff02f2011-07-07 12:01:54 -070060 [IIO_LIGHT] = "illuminance",
61 [IIO_INTENSITY] = "intensity",
Bryan Freedf09f2c82011-07-07 12:01:55 -070062 [IIO_PROXIMITY] = "proximity",
Bryan Freed9bff02f2011-07-07 12:01:54 -070063 [IIO_TEMP] = "temp",
Jonathan Cameron1d892712011-05-18 14:40:51 +010064 [IIO_INCLI] = "incli",
65 [IIO_ROT] = "rot",
Jonathan Cameron1d892712011-05-18 14:40:51 +010066 [IIO_ANGL] = "angl",
Bryan Freed9bff02f2011-07-07 12:01:54 -070067 [IIO_TIMESTAMP] = "timestamp",
Jonathan Cameron66dbe702011-09-02 17:14:43 +010068 [IIO_CAPACITANCE] = "capacitance",
Michael Hennericha6b12852012-04-27 10:58:34 +020069 [IIO_ALTVOLTAGE] = "altvoltage",
Jon Brenner21cd1fa2012-05-16 10:46:42 -050070 [IIO_CCT] = "cct",
Lars-Peter Clausenc4f0c692012-11-20 13:36:00 +000071 [IIO_PRESSURE] = "pressure",
Harald Geyerac216aa2013-12-01 15:08:00 +000072 [IIO_HUMIDITYRELATIVE] = "humidityrelative",
Daniel Baluta55aebeb2014-11-10 14:45:30 +020073 [IIO_ACTIVITY] = "activity",
Irina Tirdeaa88bfe72014-11-10 14:45:32 +020074 [IIO_STEPS] = "steps",
Irina Tirdea72c66642015-01-11 21:10:07 +020075 [IIO_ENERGY] = "energy",
Irina Tirdeacc3c9ee2015-01-11 21:10:08 +020076 [IIO_DISTANCE] = "distance",
Irina Tirdea5a1a9322015-01-11 21:10:09 +020077 [IIO_VELOCITY] = "velocity",
Matt Ranostay8ff6b3b2015-09-13 20:26:11 -070078 [IIO_CONCENTRATION] = "concentration",
Matt Ranostayd38d5462015-09-13 20:26:12 -070079 [IIO_RESISTANCE] = "resistance",
Jonathan Cameron1d892712011-05-18 14:40:51 +010080};
81
Jonathan Cameron330c6c52011-09-02 17:14:39 +010082static const char * const iio_modifier_names[] = {
Jonathan Cameron1d892712011-05-18 14:40:51 +010083 [IIO_MOD_X] = "x",
84 [IIO_MOD_Y] = "y",
85 [IIO_MOD_Z] = "z",
Peter Meerwald4b8d8012015-06-20 23:52:30 +020086 [IIO_MOD_X_AND_Y] = "x&y",
87 [IIO_MOD_X_AND_Z] = "x&z",
88 [IIO_MOD_Y_AND_Z] = "y&z",
89 [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
90 [IIO_MOD_X_OR_Y] = "x|y",
91 [IIO_MOD_X_OR_Z] = "x|z",
92 [IIO_MOD_Y_OR_Z] = "y|z",
93 [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
Jonathan Cameron8f5879b2012-05-05 10:39:22 +010094 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
Jonathan Cameroncf82cb82012-05-05 10:56:41 +010095 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
Jonathan Cameron330c6c52011-09-02 17:14:39 +010096 [IIO_MOD_LIGHT_BOTH] = "both",
97 [IIO_MOD_LIGHT_IR] = "ir",
Jon Brenner21cd1fa2012-05-16 10:46:42 -050098 [IIO_MOD_LIGHT_CLEAR] = "clear",
99 [IIO_MOD_LIGHT_RED] = "red",
100 [IIO_MOD_LIGHT_GREEN] = "green",
101 [IIO_MOD_LIGHT_BLUE] = "blue",
Srinivas Pandruvada5082f402014-04-29 00:51:00 +0100102 [IIO_MOD_QUATERNION] = "quaternion",
Peter Meerwald638b43b2014-02-05 16:57:00 +0000103 [IIO_MOD_TEMP_AMBIENT] = "ambient",
104 [IIO_MOD_TEMP_OBJECT] = "object",
Reyad Attiyat11b8dda2014-07-17 19:18:00 +0100105 [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
106 [IIO_MOD_NORTH_TRUE] = "from_north_true",
107 [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
108 [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
Daniel Baluta55aebeb2014-11-10 14:45:30 +0200109 [IIO_MOD_RUNNING] = "running",
110 [IIO_MOD_JOGGING] = "jogging",
111 [IIO_MOD_WALKING] = "walking",
112 [IIO_MOD_STILL] = "still",
Irina Tirdea5a1a9322015-01-11 21:10:09 +0200113 [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
Lars-Peter Clausen1ce87f22015-05-22 18:17:38 +0200114 [IIO_MOD_I] = "i",
115 [IIO_MOD_Q] = "q",
Matt Ranostay8ff6b3b2015-09-13 20:26:11 -0700116 [IIO_MOD_CO2] = "co2",
117 [IIO_MOD_VOC] = "voc",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100118};
119
120/* relies on pairs of these shared then separate */
121static const char * const iio_chan_info_postfix[] = {
Jonathan Cameron75a973c2012-04-15 17:41:30 +0100122 [IIO_CHAN_INFO_RAW] = "raw",
123 [IIO_CHAN_INFO_PROCESSED] = "input",
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100124 [IIO_CHAN_INFO_SCALE] = "scale",
125 [IIO_CHAN_INFO_OFFSET] = "offset",
126 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
127 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
128 [IIO_CHAN_INFO_PEAK] = "peak_raw",
129 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
130 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
131 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
Jonathan Camerondf94aba2011-11-27 11:39:12 +0000132 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
133 = "filter_low_pass_3db_frequency",
Martin Fuzzey3f7f6422015-05-13 12:26:42 +0200134 [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY]
135 = "filter_high_pass_3db_frequency",
Laxman Dewangance85a1c2012-04-13 16:03:31 +0530136 [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
Michael Hennericha6b12852012-04-27 10:58:34 +0200137 [IIO_CHAN_INFO_FREQUENCY] = "frequency",
138 [IIO_CHAN_INFO_PHASE] = "phase",
Michael Hennerichb65d6212012-05-11 11:36:53 +0200139 [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
srinivas pandruvada7c9ab032012-09-05 13:56:00 +0100140 [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
Peter Meerwald899d90b2013-09-08 16:20:00 +0100141 [IIO_CHAN_INFO_INT_TIME] = "integration_time",
Irina Tirdeaa88bfe72014-11-10 14:45:32 +0200142 [IIO_CHAN_INFO_ENABLE] = "en",
Irina Tirdeabcdf28f2014-11-10 14:45:33 +0200143 [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight",
Irina Tirdead37f6832015-01-11 21:10:10 +0200144 [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight",
Irina Tirdea2f0ecb72015-01-27 20:41:52 +0200145 [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count",
146 [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time",
Vianney le Clément de Saint-Marcqc8a85852015-03-30 10:34:58 +0200147 [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity",
Irina Tirdeafaaa4492015-04-29 21:16:39 +0300148 [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100149};
150
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000151/**
152 * iio_find_channel_from_si() - get channel from its scan index
153 * @indio_dev: device
154 * @si: scan index to match
155 */
Jonathan Cameron5fb21c82011-12-05 21:37:10 +0000156const struct iio_chan_spec
157*iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
158{
159 int i;
160
161 for (i = 0; i < indio_dev->num_channels; i++)
162 if (indio_dev->channels[i].scan_index == si)
163 return &indio_dev->channels[i];
164 return NULL;
165}
166
Jonathan Cameron847ec802009-08-18 18:06:19 +0100167/* This turns up an awful lot */
168ssize_t iio_read_const_attr(struct device *dev,
169 struct device_attribute *attr,
170 char *buf)
171{
172 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
173}
174EXPORT_SYMBOL(iio_read_const_attr);
175
Jonathan Cameron847ec802009-08-18 18:06:19 +0100176static int __init iio_init(void)
177{
178 int ret;
179
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100180 /* Register sysfs bus */
181 ret = bus_register(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100182 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100183 pr_err("could not register bus type\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +0100184 goto error_nothing;
185 }
186
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100187 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
188 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100189 pr_err("failed to allocate char dev region\n");
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100190 goto error_unregister_bus_type;
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100191 }
Jonathan Cameron847ec802009-08-18 18:06:19 +0100192
Michael Henneriche553f182012-03-01 10:51:03 +0100193 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
194
Jonathan Cameron847ec802009-08-18 18:06:19 +0100195 return 0;
196
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100197error_unregister_bus_type:
198 bus_unregister(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100199error_nothing:
200 return ret;
201}
202
203static void __exit iio_exit(void)
204{
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100205 if (iio_devt)
206 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100207 bus_unregister(&iio_bus_type);
Michael Henneriche553f182012-03-01 10:51:03 +0100208 debugfs_remove(iio_debugfs_dentry);
209}
210
211#if defined(CONFIG_DEBUG_FS)
Michael Henneriche553f182012-03-01 10:51:03 +0100212static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
213 size_t count, loff_t *ppos)
214{
215 struct iio_dev *indio_dev = file->private_data;
216 char buf[20];
217 unsigned val = 0;
218 ssize_t len;
219 int ret;
220
221 ret = indio_dev->info->debugfs_reg_access(indio_dev,
222 indio_dev->cached_reg_addr,
223 0, &val);
224 if (ret)
225 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
226
227 len = snprintf(buf, sizeof(buf), "0x%X\n", val);
228
229 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
230}
231
232static ssize_t iio_debugfs_write_reg(struct file *file,
233 const char __user *userbuf, size_t count, loff_t *ppos)
234{
235 struct iio_dev *indio_dev = file->private_data;
236 unsigned reg, val;
237 char buf[80];
238 int ret;
239
240 count = min_t(size_t, count, (sizeof(buf)-1));
241 if (copy_from_user(buf, userbuf, count))
242 return -EFAULT;
243
244 buf[count] = 0;
245
246 ret = sscanf(buf, "%i %i", &reg, &val);
247
248 switch (ret) {
249 case 1:
250 indio_dev->cached_reg_addr = reg;
251 break;
252 case 2:
253 indio_dev->cached_reg_addr = reg;
254 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
255 val, NULL);
256 if (ret) {
257 dev_err(indio_dev->dev.parent, "%s: write failed\n",
258 __func__);
259 return ret;
260 }
261 break;
262 default:
263 return -EINVAL;
264 }
265
266 return count;
267}
268
269static const struct file_operations iio_debugfs_reg_fops = {
Axel Lin5a28c872012-05-03 09:50:51 +0800270 .open = simple_open,
Michael Henneriche553f182012-03-01 10:51:03 +0100271 .read = iio_debugfs_read_reg,
272 .write = iio_debugfs_write_reg,
273};
274
275static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
276{
277 debugfs_remove_recursive(indio_dev->debugfs_dentry);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100278}
279
Michael Henneriche553f182012-03-01 10:51:03 +0100280static int iio_device_register_debugfs(struct iio_dev *indio_dev)
281{
282 struct dentry *d;
283
284 if (indio_dev->info->debugfs_reg_access == NULL)
285 return 0;
286
Axel Linabd5a2f2012-05-03 22:56:58 +0800287 if (!iio_debugfs_dentry)
Michael Henneriche553f182012-03-01 10:51:03 +0100288 return 0;
289
290 indio_dev->debugfs_dentry =
291 debugfs_create_dir(dev_name(&indio_dev->dev),
292 iio_debugfs_dentry);
Michael Henneriche553f182012-03-01 10:51:03 +0100293 if (indio_dev->debugfs_dentry == NULL) {
294 dev_warn(indio_dev->dev.parent,
295 "Failed to create debugfs directory\n");
296 return -EFAULT;
297 }
298
299 d = debugfs_create_file("direct_reg_access", 0644,
300 indio_dev->debugfs_dentry,
301 indio_dev, &iio_debugfs_reg_fops);
302 if (!d) {
303 iio_device_unregister_debugfs(indio_dev);
304 return -ENOMEM;
305 }
306
307 return 0;
308}
309#else
310static int iio_device_register_debugfs(struct iio_dev *indio_dev)
311{
312 return 0;
313}
314
315static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
316{
317}
318#endif /* CONFIG_DEBUG_FS */
319
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100320static ssize_t iio_read_channel_ext_info(struct device *dev,
321 struct device_attribute *attr,
322 char *buf)
323{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200324 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100325 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
326 const struct iio_chan_spec_ext_info *ext_info;
327
328 ext_info = &this_attr->c->ext_info[this_attr->address];
329
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200330 return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100331}
332
333static ssize_t iio_write_channel_ext_info(struct device *dev,
334 struct device_attribute *attr,
335 const char *buf,
336 size_t len)
337{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200338 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100339 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
340 const struct iio_chan_spec_ext_info *ext_info;
341
342 ext_info = &this_attr->c->ext_info[this_attr->address];
343
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200344 return ext_info->write(indio_dev, ext_info->private,
345 this_attr->c, buf, len);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100346}
347
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200348ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
349 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
350{
351 const struct iio_enum *e = (const struct iio_enum *)priv;
352 unsigned int i;
353 size_t len = 0;
354
355 if (!e->num_items)
356 return 0;
357
358 for (i = 0; i < e->num_items; ++i)
Lars-Peter Clausen74dcd432012-06-05 18:24:12 +0200359 len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200360
361 /* replace last space with a newline */
362 buf[len - 1] = '\n';
363
364 return len;
365}
366EXPORT_SYMBOL_GPL(iio_enum_available_read);
367
368ssize_t iio_enum_read(struct iio_dev *indio_dev,
369 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
370{
371 const struct iio_enum *e = (const struct iio_enum *)priv;
372 int i;
373
374 if (!e->get)
375 return -EINVAL;
376
377 i = e->get(indio_dev, chan);
378 if (i < 0)
379 return i;
380 else if (i >= e->num_items)
381 return -EINVAL;
382
Kees Cook598db582014-03-13 16:46:00 +0000383 return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200384}
385EXPORT_SYMBOL_GPL(iio_enum_read);
386
387ssize_t iio_enum_write(struct iio_dev *indio_dev,
388 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
389 size_t len)
390{
391 const struct iio_enum *e = (const struct iio_enum *)priv;
392 unsigned int i;
393 int ret;
394
395 if (!e->set)
396 return -EINVAL;
397
398 for (i = 0; i < e->num_items; i++) {
399 if (sysfs_streq(buf, e->items[i]))
400 break;
401 }
402
403 if (i == e->num_items)
404 return -EINVAL;
405
406 ret = e->set(indio_dev, chan, i);
407 return ret ? ret : len;
408}
409EXPORT_SYMBOL_GPL(iio_enum_write);
410
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100411/**
412 * iio_format_value() - Formats a IIO value into its string representation
Cristina Opriceana2498dcf2015-07-24 16:16:19 +0300413 * @buf: The buffer to which the formatted value gets written
414 * @type: One of the IIO_VAL_... constants. This decides how the val
415 * and val2 parameters are formatted.
416 * @size: Number of IIO value entries contained in vals
417 * @vals: Pointer to the values, exact meaning depends on the
418 * type parameter.
419 *
420 * Return: 0 by default, a negative number on failure or the
421 * total number of characters written for a type that belongs
422 * to the IIO_VAL_... constant.
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100423 */
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100424ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100425{
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100426 unsigned long long tmp;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200427 bool scale_db = false;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100428
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100429 switch (type) {
Michael Hennerich67eedba2012-05-11 11:36:52 +0200430 case IIO_VAL_INT:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100431 return sprintf(buf, "%d\n", vals[0]);
Michael Hennerich67eedba2012-05-11 11:36:52 +0200432 case IIO_VAL_INT_PLUS_MICRO_DB:
433 scale_db = true;
434 case IIO_VAL_INT_PLUS_MICRO:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100435 if (vals[1] < 0)
Michal Nazarewicz8f57e4d2016-01-15 16:57:58 -0800436 return sprintf(buf, "-%d.%06u%s\n", abs(vals[0]),
437 -vals[1], scale_db ? " dB" : "");
Jonathan Cameron1d892712011-05-18 14:40:51 +0100438 else
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100439 return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1],
Michael Hennerich67eedba2012-05-11 11:36:52 +0200440 scale_db ? " dB" : "");
441 case IIO_VAL_INT_PLUS_NANO:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100442 if (vals[1] < 0)
Michal Nazarewicz8f57e4d2016-01-15 16:57:58 -0800443 return sprintf(buf, "-%d.%09u\n", abs(vals[0]),
444 -vals[1]);
Michael Hennerich71646e22011-06-27 13:07:07 +0100445 else
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100446 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100447 case IIO_VAL_FRACTIONAL:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100448 tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
449 vals[1] = do_div(tmp, 1000000000LL);
450 vals[0] = tmp;
451 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
Lars-Peter Clausen103d9fb2012-10-16 17:29:00 +0100452 case IIO_VAL_FRACTIONAL_LOG2:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100453 tmp = (s64)vals[0] * 1000000000LL >> vals[1];
454 vals[1] = do_div(tmp, 1000000000LL);
455 vals[0] = tmp;
456 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
457 case IIO_VAL_INT_MULTIPLE:
458 {
459 int i;
460 int len = 0;
461
462 for (i = 0; i < size; ++i)
463 len += snprintf(&buf[len], PAGE_SIZE - len, "%d ",
464 vals[i]);
465 len += snprintf(&buf[len], PAGE_SIZE - len, "\n");
466 return len;
467 }
Michael Hennerich67eedba2012-05-11 11:36:52 +0200468 default:
Jonathan Cameron1d892712011-05-18 14:40:51 +0100469 return 0;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200470 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100471}
Andrew F. Davis7d2c2aca2015-12-14 16:35:57 -0600472EXPORT_SYMBOL_GPL(iio_format_value);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100473
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100474static ssize_t iio_read_channel_info(struct device *dev,
475 struct device_attribute *attr,
476 char *buf)
477{
478 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
479 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100480 int vals[INDIO_MAX_RAW_ELEMENTS];
481 int ret;
482 int val_len = 2;
483
484 if (indio_dev->info->read_raw_multi)
485 ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
486 INDIO_MAX_RAW_ELEMENTS,
487 vals, &val_len,
488 this_attr->address);
489 else
490 ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
491 &vals[0], &vals[1], this_attr->address);
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100492
493 if (ret < 0)
494 return ret;
495
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100496 return iio_format_value(buf, ret, val_len, vals);
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100497}
498
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000499/**
500 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
501 * @str: The string to parse
502 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
503 * @integer: The integer part of the number
504 * @fract: The fractional part of the number
505 *
506 * Returns 0 on success, or a negative error code if the string could not be
507 * parsed.
508 */
509int iio_str_to_fixpoint(const char *str, int fract_mult,
510 int *integer, int *fract)
511{
512 int i = 0, f = 0;
513 bool integer_part = true, negative = false;
514
Sean Nyekjaerf47dff32015-11-09 13:55:34 +0100515 if (fract_mult == 0) {
516 *fract = 0;
517
518 return kstrtoint(str, 0, integer);
519 }
520
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000521 if (str[0] == '-') {
522 negative = true;
523 str++;
524 } else if (str[0] == '+') {
525 str++;
526 }
527
528 while (*str) {
529 if ('0' <= *str && *str <= '9') {
530 if (integer_part) {
531 i = i * 10 + *str - '0';
532 } else {
533 f += fract_mult * (*str - '0');
534 fract_mult /= 10;
535 }
536 } else if (*str == '\n') {
537 if (*(str + 1) == '\0')
538 break;
539 else
540 return -EINVAL;
541 } else if (*str == '.' && integer_part) {
542 integer_part = false;
543 } else {
544 return -EINVAL;
545 }
546 str++;
547 }
548
549 if (negative) {
550 if (i)
551 i = -i;
552 else
553 f = -f;
554 }
555
556 *integer = i;
557 *fract = f;
558
559 return 0;
560}
561EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
562
Jonathan Cameron1d892712011-05-18 14:40:51 +0100563static ssize_t iio_write_channel_info(struct device *dev,
564 struct device_attribute *attr,
565 const char *buf,
566 size_t len)
567{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200568 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100569 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000570 int ret, fract_mult = 100000;
571 int integer, fract;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100572
573 /* Assumes decimal - precision based on number of digits */
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100574 if (!indio_dev->info->write_raw)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100575 return -EINVAL;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100576
577 if (indio_dev->info->write_raw_get_fmt)
578 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
579 this_attr->c, this_attr->address)) {
Sean Nyekjaerf47dff32015-11-09 13:55:34 +0100580 case IIO_VAL_INT:
581 fract_mult = 0;
582 break;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100583 case IIO_VAL_INT_PLUS_MICRO:
584 fract_mult = 100000;
585 break;
586 case IIO_VAL_INT_PLUS_NANO:
587 fract_mult = 100000000;
588 break;
589 default:
590 return -EINVAL;
591 }
592
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000593 ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
594 if (ret)
595 return ret;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100596
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100597 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
Michael Hennerich5c04af02011-06-27 13:07:10 +0100598 integer, fract, this_attr->address);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100599 if (ret)
600 return ret;
601
602 return len;
603}
604
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100605static
Jonathan Cameron1d892712011-05-18 14:40:51 +0100606int __iio_device_attr_init(struct device_attribute *dev_attr,
607 const char *postfix,
608 struct iio_chan_spec const *chan,
609 ssize_t (*readfunc)(struct device *dev,
610 struct device_attribute *attr,
611 char *buf),
612 ssize_t (*writefunc)(struct device *dev,
613 struct device_attribute *attr,
614 const char *buf,
615 size_t len),
Jonathan Cameron37044322013-09-08 14:57:00 +0100616 enum iio_shared_by shared_by)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100617{
Jonathan Cameron37044322013-09-08 14:57:00 +0100618 int ret = 0;
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000619 char *name = NULL;
Jonathan Cameron37044322013-09-08 14:57:00 +0100620 char *full_postfix;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100621 sysfs_attr_init(&dev_attr->attr);
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100622
623 /* Build up postfix of <extend_name>_<modifier>_postfix */
Jonathan Cameron37044322013-09-08 14:57:00 +0100624 if (chan->modified && (shared_by == IIO_SEPARATE)) {
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100625 if (chan->extend_name)
626 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
627 iio_modifier_names[chan
628 ->channel2],
629 chan->extend_name,
630 postfix);
631 else
632 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
633 iio_modifier_names[chan
634 ->channel2],
635 postfix);
636 } else {
Lars-Peter Clausen77bfa8b2014-02-14 14:19:00 +0000637 if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100638 full_postfix = kstrdup(postfix, GFP_KERNEL);
639 else
640 full_postfix = kasprintf(GFP_KERNEL,
641 "%s_%s",
642 chan->extend_name,
643 postfix);
644 }
Jonathan Cameron37044322013-09-08 14:57:00 +0100645 if (full_postfix == NULL)
646 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100647
Justin P. Mattock4abf6f82012-02-29 22:00:38 -0800648 if (chan->differential) { /* Differential can not have modifier */
Jonathan Cameron37044322013-09-08 14:57:00 +0100649 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100650 case IIO_SHARED_BY_ALL:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000651 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100652 break;
653 case IIO_SHARED_BY_DIR:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000654 name = kasprintf(GFP_KERNEL, "%s_%s",
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100655 iio_direction[chan->output],
656 full_postfix);
657 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100658 case IIO_SHARED_BY_TYPE:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000659 name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100660 iio_direction[chan->output],
661 iio_chan_type_name_spec[chan->type],
662 iio_chan_type_name_spec[chan->type],
663 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100664 break;
665 case IIO_SEPARATE:
666 if (!chan->indexed) {
Dan Carpenter231bfe52015-11-21 13:33:00 +0300667 WARN(1, "Differential channels must be indexed\n");
Jonathan Cameron37044322013-09-08 14:57:00 +0100668 ret = -EINVAL;
669 goto error_free_full_postfix;
670 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000671 name = kasprintf(GFP_KERNEL,
Jonathan Cameron37044322013-09-08 14:57:00 +0100672 "%s_%s%d-%s%d_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100673 iio_direction[chan->output],
674 iio_chan_type_name_spec[chan->type],
675 chan->channel,
676 iio_chan_type_name_spec[chan->type],
677 chan->channel2,
678 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100679 break;
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100680 }
681 } else { /* Single ended */
Jonathan Cameron37044322013-09-08 14:57:00 +0100682 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100683 case IIO_SHARED_BY_ALL:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000684 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100685 break;
686 case IIO_SHARED_BY_DIR:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000687 name = kasprintf(GFP_KERNEL, "%s_%s",
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100688 iio_direction[chan->output],
689 full_postfix);
690 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100691 case IIO_SHARED_BY_TYPE:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000692 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100693 iio_direction[chan->output],
694 iio_chan_type_name_spec[chan->type],
695 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100696 break;
697
698 case IIO_SEPARATE:
699 if (chan->indexed)
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000700 name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
Jonathan Cameron37044322013-09-08 14:57:00 +0100701 iio_direction[chan->output],
702 iio_chan_type_name_spec[chan->type],
703 chan->channel,
704 full_postfix);
705 else
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000706 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
Jonathan Cameron37044322013-09-08 14:57:00 +0100707 iio_direction[chan->output],
708 iio_chan_type_name_spec[chan->type],
709 full_postfix);
710 break;
711 }
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100712 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000713 if (name == NULL) {
Jonathan Cameron1d892712011-05-18 14:40:51 +0100714 ret = -ENOMEM;
715 goto error_free_full_postfix;
716 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000717 dev_attr->attr.name = name;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100718
719 if (readfunc) {
720 dev_attr->attr.mode |= S_IRUGO;
721 dev_attr->show = readfunc;
722 }
723
724 if (writefunc) {
725 dev_attr->attr.mode |= S_IWUSR;
726 dev_attr->store = writefunc;
727 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000728
Jonathan Cameron1d892712011-05-18 14:40:51 +0100729error_free_full_postfix:
730 kfree(full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100731
Jonathan Cameron847ec802009-08-18 18:06:19 +0100732 return ret;
733}
734
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100735static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100736{
737 kfree(dev_attr->attr.name);
738}
739
740int __iio_add_chan_devattr(const char *postfix,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100741 struct iio_chan_spec const *chan,
742 ssize_t (*readfunc)(struct device *dev,
743 struct device_attribute *attr,
744 char *buf),
745 ssize_t (*writefunc)(struct device *dev,
746 struct device_attribute *attr,
747 const char *buf,
748 size_t len),
Jonathan Camerone614a542011-09-02 17:14:41 +0100749 u64 mask,
Jonathan Cameron37044322013-09-08 14:57:00 +0100750 enum iio_shared_by shared_by,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100751 struct device *dev,
752 struct list_head *attr_list)
753{
754 int ret;
755 struct iio_dev_attr *iio_attr, *t;
756
Sachin Kamat670c1102013-10-24 12:53:00 +0100757 iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000758 if (iio_attr == NULL)
759 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100760 ret = __iio_device_attr_init(&iio_attr->dev_attr,
761 postfix, chan,
Jonathan Cameron37044322013-09-08 14:57:00 +0100762 readfunc, writefunc, shared_by);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100763 if (ret)
764 goto error_iio_dev_attr_free;
765 iio_attr->c = chan;
766 iio_attr->address = mask;
767 list_for_each_entry(t, attr_list, l)
768 if (strcmp(t->dev_attr.attr.name,
769 iio_attr->dev_attr.attr.name) == 0) {
Jonathan Cameron37044322013-09-08 14:57:00 +0100770 if (shared_by == IIO_SEPARATE)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100771 dev_err(dev, "tried to double register : %s\n",
772 t->dev_attr.attr.name);
773 ret = -EBUSY;
774 goto error_device_attr_deinit;
775 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100776 list_add(&iio_attr->l, attr_list);
777
778 return 0;
779
780error_device_attr_deinit:
781 __iio_device_attr_deinit(&iio_attr->dev_attr);
782error_iio_dev_attr_free:
783 kfree(iio_attr);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100784 return ret;
785}
786
Jonathan Cameron37044322013-09-08 14:57:00 +0100787static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
788 struct iio_chan_spec const *chan,
789 enum iio_shared_by shared_by,
790 const long *infomask)
791{
792 int i, ret, attrcount = 0;
793
794 for_each_set_bit(i, infomask, sizeof(infomask)*8) {
Jonathan Cameronef4b4852014-01-03 22:24:00 +0000795 if (i >= ARRAY_SIZE(iio_chan_info_postfix))
796 return -EINVAL;
Jonathan Cameron37044322013-09-08 14:57:00 +0100797 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
798 chan,
799 &iio_read_channel_info,
800 &iio_write_channel_info,
801 i,
802 shared_by,
803 &indio_dev->dev,
804 &indio_dev->channel_attr_list);
805 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
806 continue;
807 else if (ret < 0)
808 return ret;
809 attrcount++;
810 }
811
812 return attrcount;
813}
814
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100815static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100816 struct iio_chan_spec const *chan)
817{
Jonathan Cameron5ccb3ad2012-04-15 17:41:16 +0100818 int ret, attrcount = 0;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100819 const struct iio_chan_spec_ext_info *ext_info;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100820
Jonathan Cameron1d892712011-05-18 14:40:51 +0100821 if (chan->channel < 0)
822 return 0;
Jonathan Cameron37044322013-09-08 14:57:00 +0100823 ret = iio_device_add_info_mask_type(indio_dev, chan,
824 IIO_SEPARATE,
825 &chan->info_mask_separate);
826 if (ret < 0)
827 return ret;
828 attrcount += ret;
829
830 ret = iio_device_add_info_mask_type(indio_dev, chan,
831 IIO_SHARED_BY_TYPE,
832 &chan->info_mask_shared_by_type);
833 if (ret < 0)
834 return ret;
835 attrcount += ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100836
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100837 ret = iio_device_add_info_mask_type(indio_dev, chan,
838 IIO_SHARED_BY_DIR,
839 &chan->info_mask_shared_by_dir);
840 if (ret < 0)
841 return ret;
842 attrcount += ret;
843
844 ret = iio_device_add_info_mask_type(indio_dev, chan,
845 IIO_SHARED_BY_ALL,
846 &chan->info_mask_shared_by_all);
847 if (ret < 0)
848 return ret;
849 attrcount += ret;
850
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100851 if (chan->ext_info) {
852 unsigned int i = 0;
853 for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
854 ret = __iio_add_chan_devattr(ext_info->name,
855 chan,
856 ext_info->read ?
857 &iio_read_channel_ext_info : NULL,
858 ext_info->write ?
859 &iio_write_channel_ext_info : NULL,
860 i,
861 ext_info->shared,
862 &indio_dev->dev,
863 &indio_dev->channel_attr_list);
864 i++;
865 if (ret == -EBUSY && ext_info->shared)
866 continue;
867
868 if (ret)
Jonathan Cameron37044322013-09-08 14:57:00 +0100869 return ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100870
871 attrcount++;
872 }
873 }
874
Jonathan Cameron37044322013-09-08 14:57:00 +0100875 return attrcount;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100876}
877
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100878/**
879 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
880 * @attr_list: List of IIO device attributes
881 *
882 * This function frees the memory allocated for each of the IIO device
Martin Fuzzeyc1b03ab2015-02-19 15:17:44 +0100883 * attributes in the list.
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100884 */
885void iio_free_chan_devattr_list(struct list_head *attr_list)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100886{
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100887 struct iio_dev_attr *p, *n;
888
889 list_for_each_entry_safe(p, n, attr_list, l) {
890 kfree(p->dev_attr.attr.name);
Martin Fuzzeyc1b03ab2015-02-19 15:17:44 +0100891 list_del(&p->l);
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100892 kfree(p);
893 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100894}
895
Jonathan Cameron1b732882011-05-18 14:41:43 +0100896static ssize_t iio_show_dev_name(struct device *dev,
897 struct device_attribute *attr,
898 char *buf)
899{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200900 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Kees Cook598db582014-03-13 16:46:00 +0000901 return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name);
Jonathan Cameron1b732882011-05-18 14:41:43 +0100902}
903
904static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
905
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100906static int iio_device_register_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100907{
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100908 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100909 struct iio_dev_attr *p;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100910 struct attribute **attr;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100911
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100912 /* First count elements in any existing group */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100913 if (indio_dev->info->attrs) {
914 attr = indio_dev->info->attrs->attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100915 while (*attr++ != NULL)
916 attrcount_orig++;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100917 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100918 attrcount = attrcount_orig;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100919 /*
920 * New channel registration method - relies on the fact a group does
Peter Meerwaldd25b3802012-08-26 13:43:00 +0100921 * not need to be initialized if its name is NULL.
Jonathan Cameron1d892712011-05-18 14:40:51 +0100922 */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100923 if (indio_dev->channels)
924 for (i = 0; i < indio_dev->num_channels; i++) {
925 ret = iio_device_add_channel_sysfs(indio_dev,
926 &indio_dev
Jonathan Cameron1d892712011-05-18 14:40:51 +0100927 ->channels[i]);
928 if (ret < 0)
929 goto error_clear_attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100930 attrcount += ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100931 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100932
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100933 if (indio_dev->name)
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100934 attrcount++;
935
Thomas Meyerd83fb182011-11-29 22:08:00 +0100936 indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
937 sizeof(indio_dev->chan_attr_group.attrs[0]),
938 GFP_KERNEL);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100939 if (indio_dev->chan_attr_group.attrs == NULL) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100940 ret = -ENOMEM;
941 goto error_clear_attrs;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100942 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100943 /* Copy across original attributes */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100944 if (indio_dev->info->attrs)
945 memcpy(indio_dev->chan_attr_group.attrs,
946 indio_dev->info->attrs->attrs,
947 sizeof(indio_dev->chan_attr_group.attrs[0])
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100948 *attrcount_orig);
949 attrn = attrcount_orig;
950 /* Add all elements from the list. */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100951 list_for_each_entry(p, &indio_dev->channel_attr_list, l)
952 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
953 if (indio_dev->name)
954 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100955
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100956 indio_dev->groups[indio_dev->groupcounter++] =
957 &indio_dev->chan_attr_group;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100958
Jonathan Cameron1d892712011-05-18 14:40:51 +0100959 return 0;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100960
Jonathan Cameron1d892712011-05-18 14:40:51 +0100961error_clear_attrs:
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100962 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100963
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100964 return ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100965}
966
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100967static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100968{
Jonathan Cameron1d892712011-05-18 14:40:51 +0100969
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100970 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100971 kfree(indio_dev->chan_attr_group.attrs);
Martin Fuzzeyc1b03ab2015-02-19 15:17:44 +0100972 indio_dev->chan_attr_group.attrs = NULL;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100973}
974
Jonathan Cameron847ec802009-08-18 18:06:19 +0100975static void iio_dev_release(struct device *device)
976{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200977 struct iio_dev *indio_dev = dev_to_iio_dev(device);
Vladimir Barinov735ad072015-08-20 22:37:39 +0300978 if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED))
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100979 iio_device_unregister_trigger_consumer(indio_dev);
980 iio_device_unregister_eventset(indio_dev);
981 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200982
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100983 iio_buffer_put(indio_dev->buffer);
984
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200985 ida_simple_remove(&iio_ida, indio_dev->id);
986 kfree(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100987}
988
Guenter Roeck17d82b42013-02-07 17:09:00 +0000989struct device_type iio_device_type = {
Jonathan Cameron847ec802009-08-18 18:06:19 +0100990 .name = "iio_device",
991 .release = iio_dev_release,
992};
993
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000994/**
995 * iio_device_alloc() - allocate an iio_dev from a driver
996 * @sizeof_priv: Space to allocate for private structure.
997 **/
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200998struct iio_dev *iio_device_alloc(int sizeof_priv)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100999{
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +01001000 struct iio_dev *dev;
1001 size_t alloc_size;
1002
1003 alloc_size = sizeof(struct iio_dev);
1004 if (sizeof_priv) {
1005 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
1006 alloc_size += sizeof_priv;
1007 }
1008 /* ensure 32-byte alignment of whole construct ? */
1009 alloc_size += IIO_ALIGN - 1;
1010
1011 dev = kzalloc(alloc_size, GFP_KERNEL);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001012
1013 if (dev) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001014 dev->dev.groups = dev->groups;
Guenter Roeck17d82b42013-02-07 17:09:00 +00001015 dev->dev.type = &iio_device_type;
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +01001016 dev->dev.bus = &iio_bus_type;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001017 device_initialize(&dev->dev);
1018 dev_set_drvdata(&dev->dev, (void *)dev);
1019 mutex_init(&dev->mlock);
Jonathan Cameronac917a82012-02-15 19:48:00 +00001020 mutex_init(&dev->info_exist_lock);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +02001021 INIT_LIST_HEAD(&dev->channel_attr_list);
Jonathan Camerona9e39f92011-09-14 13:01:25 +01001022
1023 dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
1024 if (dev->id < 0) {
1025 /* cannot use a dev_err as the name isn't available */
Sachin Kamat3176dd52013-10-24 12:53:00 +01001026 pr_err("failed to get device id\n");
Jonathan Camerona9e39f92011-09-14 13:01:25 +01001027 kfree(dev);
1028 return NULL;
1029 }
1030 dev_set_name(&dev->dev, "iio:device%d", dev->id);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001031 INIT_LIST_HEAD(&dev->buffer_list);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001032 }
1033
1034 return dev;
1035}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001036EXPORT_SYMBOL(iio_device_alloc);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001037
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001038/**
1039 * iio_device_free() - free an iio_dev from a driver
1040 * @dev: the iio_dev associated with the device
1041 **/
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001042void iio_device_free(struct iio_dev *dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001043{
Lars-Peter Clausene407fd62012-06-04 10:41:42 +02001044 if (dev)
1045 put_device(&dev->dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001046}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001047EXPORT_SYMBOL(iio_device_free);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001048
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001049static void devm_iio_device_release(struct device *dev, void *res)
1050{
1051 iio_device_free(*(struct iio_dev **)res);
1052}
1053
1054static int devm_iio_device_match(struct device *dev, void *res, void *data)
1055{
1056 struct iio_dev **r = res;
1057 if (!r || !*r) {
1058 WARN_ON(!r || !*r);
1059 return 0;
1060 }
1061 return *r == data;
1062}
1063
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001064/**
1065 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1066 * @dev: Device to allocate iio_dev for
1067 * @sizeof_priv: Space to allocate for private structure.
1068 *
1069 * Managed iio_device_alloc. iio_dev allocated with this function is
1070 * automatically freed on driver detach.
1071 *
1072 * If an iio_dev allocated with this function needs to be freed separately,
1073 * devm_iio_device_free() must be used.
1074 *
1075 * RETURNS:
1076 * Pointer to allocated iio_dev on success, NULL on failure.
1077 */
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001078struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
1079{
1080 struct iio_dev **ptr, *iio_dev;
1081
1082 ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
1083 GFP_KERNEL);
1084 if (!ptr)
1085 return NULL;
1086
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001087 iio_dev = iio_device_alloc(sizeof_priv);
1088 if (iio_dev) {
1089 *ptr = iio_dev;
1090 devres_add(dev, ptr);
1091 } else {
1092 devres_free(ptr);
1093 }
1094
1095 return iio_dev;
1096}
1097EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
1098
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001099/**
1100 * devm_iio_device_free - Resource-managed iio_device_free()
1101 * @dev: Device this iio_dev belongs to
1102 * @iio_dev: the iio_dev associated with the device
1103 *
1104 * Free iio_dev allocated with devm_iio_device_alloc().
1105 */
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001106void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
1107{
1108 int rc;
1109
1110 rc = devres_release(dev, devm_iio_device_release,
1111 devm_iio_device_match, iio_dev);
1112 WARN_ON(rc);
1113}
1114EXPORT_SYMBOL_GPL(devm_iio_device_free);
1115
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001116/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001117 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
Cristina Opriceana2498dcf2015-07-24 16:16:19 +03001118 * @inode: Inode structure for identifying the device in the file system
1119 * @filp: File structure for iio device used to keep and later access
1120 * private data
1121 *
1122 * Return: 0 on success or -EBUSY if the device is already opened
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001123 **/
1124static int iio_chrdev_open(struct inode *inode, struct file *filp)
1125{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001126 struct iio_dev *indio_dev = container_of(inode->i_cdev,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001127 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001128
1129 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
1130 return -EBUSY;
1131
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001132 iio_device_get(indio_dev);
1133
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001134 filp->private_data = indio_dev;
Jonathan Cameron30eb82f2011-09-21 11:16:02 +01001135
Lars-Peter Clausen79335142011-12-19 15:23:49 +01001136 return 0;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001137}
1138
1139/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001140 * iio_chrdev_release() - chrdev file close buffer access and ioctls
Cristina Opriceana2498dcf2015-07-24 16:16:19 +03001141 * @inode: Inode structure pointer for the char device
1142 * @filp: File structure pointer for the char device
1143 *
1144 * Return: 0 for successful release
1145 */
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001146static int iio_chrdev_release(struct inode *inode, struct file *filp)
1147{
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001148 struct iio_dev *indio_dev = container_of(inode->i_cdev,
1149 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001150 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001151 iio_device_put(indio_dev);
1152
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001153 return 0;
1154}
1155
1156/* Somewhat of a cross file organization violation - ioctls here are actually
1157 * event related */
1158static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1159{
1160 struct iio_dev *indio_dev = filp->private_data;
1161 int __user *ip = (int __user *)arg;
1162 int fd;
1163
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +01001164 if (!indio_dev->info)
1165 return -ENODEV;
1166
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001167 if (cmd == IIO_GET_EVENT_FD_IOCTL) {
1168 fd = iio_event_getfd(indio_dev);
Linus Walleij3f9059b2015-08-11 11:56:40 +02001169 if (fd < 0)
1170 return fd;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001171 if (copy_to_user(ip, &fd, sizeof(fd)))
1172 return -EFAULT;
1173 return 0;
1174 }
1175 return -EINVAL;
1176}
1177
Jonathan Cameron14555b12011-09-21 11:15:57 +01001178static const struct file_operations iio_buffer_fileops = {
1179 .read = iio_buffer_read_first_n_outer_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001180 .release = iio_chrdev_release,
1181 .open = iio_chrdev_open,
Jonathan Cameron14555b12011-09-21 11:15:57 +01001182 .poll = iio_buffer_poll_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001183 .owner = THIS_MODULE,
1184 .llseek = noop_llseek,
1185 .unlocked_ioctl = iio_ioctl,
1186 .compat_ioctl = iio_ioctl,
1187};
1188
Vlad Dogaru8f5d8722014-12-29 11:37:48 +02001189static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
1190{
1191 int i, j;
1192 const struct iio_chan_spec *channels = indio_dev->channels;
1193
1194 if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
1195 return 0;
1196
1197 for (i = 0; i < indio_dev->num_channels - 1; i++) {
1198 if (channels[i].scan_index < 0)
1199 continue;
1200 for (j = i + 1; j < indio_dev->num_channels; j++)
1201 if (channels[i].scan_index == channels[j].scan_index) {
1202 dev_err(&indio_dev->dev,
1203 "Duplicate scan index %d\n",
1204 channels[i].scan_index);
1205 return -EINVAL;
1206 }
1207 }
1208
1209 return 0;
1210}
1211
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001212static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1213
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001214/**
1215 * iio_device_register() - register a device with the IIO subsystem
1216 * @indio_dev: Device structure filled by the device driver
1217 **/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001218int iio_device_register(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001219{
1220 int ret;
1221
Guenter Roeck17d82b42013-02-07 17:09:00 +00001222 /* If the calling driver did not initialize of_node, do it here */
1223 if (!indio_dev->dev.of_node && indio_dev->dev.parent)
1224 indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
1225
Vlad Dogaru8f5d8722014-12-29 11:37:48 +02001226 ret = iio_check_unique_scan_index(indio_dev);
1227 if (ret < 0)
1228 return ret;
1229
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001230 /* configure elements for the chrdev */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001231 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001232
Michael Henneriche553f182012-03-01 10:51:03 +01001233 ret = iio_device_register_debugfs(indio_dev);
1234 if (ret) {
1235 dev_err(indio_dev->dev.parent,
1236 "Failed to register debugfs interfaces\n");
Hartmut Knaack92825ff2014-02-16 11:53:00 +00001237 return ret;
Michael Henneriche553f182012-03-01 10:51:03 +01001238 }
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001239
1240 ret = iio_buffer_alloc_sysfs_and_mask(indio_dev);
1241 if (ret) {
1242 dev_err(indio_dev->dev.parent,
1243 "Failed to create buffer sysfs interfaces\n");
1244 goto error_unreg_debugfs;
1245 }
1246
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001247 ret = iio_device_register_sysfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001248 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001249 dev_err(indio_dev->dev.parent,
Jonathan Cameron847ec802009-08-18 18:06:19 +01001250 "Failed to register sysfs interfaces\n");
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001251 goto error_buffer_free_sysfs;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001252 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001253 ret = iio_device_register_eventset(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001254 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001255 dev_err(indio_dev->dev.parent,
Roel Van Nyenc849d252010-04-29 19:27:31 +02001256 "Failed to register event set\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001257 goto error_free_sysfs;
1258 }
Vladimir Barinov735ad072015-08-20 22:37:39 +03001259 if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED))
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001260 iio_device_register_trigger_consumer(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001261
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001262 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1263 indio_dev->setup_ops == NULL)
1264 indio_dev->setup_ops = &noop_ring_setup_ops;
1265
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001266 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
1267 indio_dev->chrdev.owner = indio_dev->info->driver_module;
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001268 indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001269 ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001270 if (ret < 0)
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001271 goto error_unreg_eventset;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001272
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001273 ret = device_add(&indio_dev->dev);
1274 if (ret < 0)
1275 goto error_cdev_del;
1276
1277 return 0;
1278error_cdev_del:
1279 cdev_del(&indio_dev->chrdev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001280error_unreg_eventset:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001281 iio_device_unregister_eventset(indio_dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001282error_free_sysfs:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001283 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001284error_buffer_free_sysfs:
1285 iio_buffer_free_sysfs_and_mask(indio_dev);
Michael Henneriche553f182012-03-01 10:51:03 +01001286error_unreg_debugfs:
1287 iio_device_unregister_debugfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001288 return ret;
1289}
1290EXPORT_SYMBOL(iio_device_register);
1291
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001292/**
1293 * iio_device_unregister() - unregister a device from the IIO subsystem
1294 * @indio_dev: Device structure representing the device.
1295 **/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001296void iio_device_unregister(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001297{
Jonathan Cameronac917a82012-02-15 19:48:00 +00001298 mutex_lock(&indio_dev->info_exist_lock);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001299
1300 device_del(&indio_dev->dev);
1301
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001302 if (indio_dev->chrdev.dev)
1303 cdev_del(&indio_dev->chrdev);
Lars-Peter Clausenbc4c9612013-09-21 16:21:00 +01001304 iio_device_unregister_debugfs(indio_dev);
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001305
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001306 iio_disable_all_buffers(indio_dev);
1307
Jonathan Cameronac917a82012-02-15 19:48:00 +00001308 indio_dev->info = NULL;
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +01001309
1310 iio_device_wakeup_eventset(indio_dev);
1311 iio_buffer_wakeup_poll(indio_dev);
1312
Jonathan Cameronac917a82012-02-15 19:48:00 +00001313 mutex_unlock(&indio_dev->info_exist_lock);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001314
1315 iio_buffer_free_sysfs_and_mask(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001316}
1317EXPORT_SYMBOL(iio_device_unregister);
Sachin Kamat8caa07c2013-10-29 11:39:00 +00001318
1319static void devm_iio_device_unreg(struct device *dev, void *res)
1320{
1321 iio_device_unregister(*(struct iio_dev **)res);
1322}
1323
1324/**
1325 * devm_iio_device_register - Resource-managed iio_device_register()
1326 * @dev: Device to allocate iio_dev for
1327 * @indio_dev: Device structure filled by the device driver
1328 *
1329 * Managed iio_device_register. The IIO device registered with this
1330 * function is automatically unregistered on driver detach. This function
1331 * calls iio_device_register() internally. Refer to that function for more
1332 * information.
1333 *
1334 * If an iio_dev registered with this function needs to be unregistered
1335 * separately, devm_iio_device_unregister() must be used.
1336 *
1337 * RETURNS:
1338 * 0 on success, negative error number on failure.
1339 */
1340int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev)
1341{
1342 struct iio_dev **ptr;
1343 int ret;
1344
1345 ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL);
1346 if (!ptr)
1347 return -ENOMEM;
1348
1349 *ptr = indio_dev;
1350 ret = iio_device_register(indio_dev);
1351 if (!ret)
1352 devres_add(dev, ptr);
1353 else
1354 devres_free(ptr);
1355
1356 return ret;
1357}
1358EXPORT_SYMBOL_GPL(devm_iio_device_register);
1359
1360/**
1361 * devm_iio_device_unregister - Resource-managed iio_device_unregister()
1362 * @dev: Device this iio_dev belongs to
1363 * @indio_dev: the iio_dev associated with the device
1364 *
1365 * Unregister iio_dev registered with devm_iio_device_register().
1366 */
1367void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev)
1368{
1369 int rc;
1370
1371 rc = devres_release(dev, devm_iio_device_unreg,
1372 devm_iio_device_match, indio_dev);
1373 WARN_ON(rc);
1374}
1375EXPORT_SYMBOL_GPL(devm_iio_device_unregister);
1376
Jonathan Cameron847ec802009-08-18 18:06:19 +01001377subsys_initcall(iio_init);
1378module_exit(iio_exit);
1379
Jonathan Cameronc8b95952012-09-02 21:27:56 +01001380MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001381MODULE_DESCRIPTION("Industrial I/O core");
1382MODULE_LICENSE("GPL");