blob: 70cb7eb0a75cacc43c8dcdce72fc0ecf74af5398 [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",
Matt Ranostayecb3a7c2016-01-26 18:34:30 -080080 [IIO_PH] = "ph",
Jonathan Cameron1d892712011-05-18 14:40:51 +010081};
82
Jonathan Cameron330c6c52011-09-02 17:14:39 +010083static const char * const iio_modifier_names[] = {
Jonathan Cameron1d892712011-05-18 14:40:51 +010084 [IIO_MOD_X] = "x",
85 [IIO_MOD_Y] = "y",
86 [IIO_MOD_Z] = "z",
Peter Meerwald4b8d8012015-06-20 23:52:30 +020087 [IIO_MOD_X_AND_Y] = "x&y",
88 [IIO_MOD_X_AND_Z] = "x&z",
89 [IIO_MOD_Y_AND_Z] = "y&z",
90 [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
91 [IIO_MOD_X_OR_Y] = "x|y",
92 [IIO_MOD_X_OR_Z] = "x|z",
93 [IIO_MOD_Y_OR_Z] = "y|z",
94 [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
Jonathan Cameron8f5879b2012-05-05 10:39:22 +010095 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
Jonathan Cameroncf82cb82012-05-05 10:56:41 +010096 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
Jonathan Cameron330c6c52011-09-02 17:14:39 +010097 [IIO_MOD_LIGHT_BOTH] = "both",
98 [IIO_MOD_LIGHT_IR] = "ir",
Jon Brenner21cd1fa2012-05-16 10:46:42 -050099 [IIO_MOD_LIGHT_CLEAR] = "clear",
100 [IIO_MOD_LIGHT_RED] = "red",
101 [IIO_MOD_LIGHT_GREEN] = "green",
102 [IIO_MOD_LIGHT_BLUE] = "blue",
Srinivas Pandruvada5082f402014-04-29 00:51:00 +0100103 [IIO_MOD_QUATERNION] = "quaternion",
Peter Meerwald638b43b2014-02-05 16:57:00 +0000104 [IIO_MOD_TEMP_AMBIENT] = "ambient",
105 [IIO_MOD_TEMP_OBJECT] = "object",
Reyad Attiyat11b8dda2014-07-17 19:18:00 +0100106 [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
107 [IIO_MOD_NORTH_TRUE] = "from_north_true",
108 [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
109 [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
Daniel Baluta55aebeb2014-11-10 14:45:30 +0200110 [IIO_MOD_RUNNING] = "running",
111 [IIO_MOD_JOGGING] = "jogging",
112 [IIO_MOD_WALKING] = "walking",
113 [IIO_MOD_STILL] = "still",
Irina Tirdea5a1a9322015-01-11 21:10:09 +0200114 [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
Lars-Peter Clausen1ce87f22015-05-22 18:17:38 +0200115 [IIO_MOD_I] = "i",
116 [IIO_MOD_Q] = "q",
Matt Ranostay8ff6b3b2015-09-13 20:26:11 -0700117 [IIO_MOD_CO2] = "co2",
118 [IIO_MOD_VOC] = "voc",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100119};
120
121/* relies on pairs of these shared then separate */
122static const char * const iio_chan_info_postfix[] = {
Jonathan Cameron75a973c2012-04-15 17:41:30 +0100123 [IIO_CHAN_INFO_RAW] = "raw",
124 [IIO_CHAN_INFO_PROCESSED] = "input",
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100125 [IIO_CHAN_INFO_SCALE] = "scale",
126 [IIO_CHAN_INFO_OFFSET] = "offset",
127 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
128 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
129 [IIO_CHAN_INFO_PEAK] = "peak_raw",
130 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
131 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
132 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
Jonathan Camerondf94aba2011-11-27 11:39:12 +0000133 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
134 = "filter_low_pass_3db_frequency",
Martin Fuzzey3f7f6422015-05-13 12:26:42 +0200135 [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY]
136 = "filter_high_pass_3db_frequency",
Laxman Dewangance85a1c2012-04-13 16:03:31 +0530137 [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
Michael Hennericha6b12852012-04-27 10:58:34 +0200138 [IIO_CHAN_INFO_FREQUENCY] = "frequency",
139 [IIO_CHAN_INFO_PHASE] = "phase",
Michael Hennerichb65d6212012-05-11 11:36:53 +0200140 [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
srinivas pandruvada7c9ab032012-09-05 13:56:00 +0100141 [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
Peter Meerwald899d90b2013-09-08 16:20:00 +0100142 [IIO_CHAN_INFO_INT_TIME] = "integration_time",
Irina Tirdeaa88bfe72014-11-10 14:45:32 +0200143 [IIO_CHAN_INFO_ENABLE] = "en",
Irina Tirdeabcdf28f2014-11-10 14:45:33 +0200144 [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight",
Irina Tirdead37f6832015-01-11 21:10:10 +0200145 [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight",
Irina Tirdea2f0ecb72015-01-27 20:41:52 +0200146 [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count",
147 [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time",
Vianney le Clément de Saint-Marcqc8a85852015-03-30 10:34:58 +0200148 [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity",
Irina Tirdeafaaa4492015-04-29 21:16:39 +0300149 [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100150};
151
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000152/**
153 * iio_find_channel_from_si() - get channel from its scan index
154 * @indio_dev: device
155 * @si: scan index to match
156 */
Jonathan Cameron5fb21c82011-12-05 21:37:10 +0000157const struct iio_chan_spec
158*iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
159{
160 int i;
161
162 for (i = 0; i < indio_dev->num_channels; i++)
163 if (indio_dev->channels[i].scan_index == si)
164 return &indio_dev->channels[i];
165 return NULL;
166}
167
Jonathan Cameron847ec802009-08-18 18:06:19 +0100168/* This turns up an awful lot */
169ssize_t iio_read_const_attr(struct device *dev,
170 struct device_attribute *attr,
171 char *buf)
172{
173 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
174}
175EXPORT_SYMBOL(iio_read_const_attr);
176
Jonathan Cameron847ec802009-08-18 18:06:19 +0100177static int __init iio_init(void)
178{
179 int ret;
180
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100181 /* Register sysfs bus */
182 ret = bus_register(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100183 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100184 pr_err("could not register bus type\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +0100185 goto error_nothing;
186 }
187
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100188 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
189 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100190 pr_err("failed to allocate char dev region\n");
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100191 goto error_unregister_bus_type;
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100192 }
Jonathan Cameron847ec802009-08-18 18:06:19 +0100193
Michael Henneriche553f182012-03-01 10:51:03 +0100194 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
195
Jonathan Cameron847ec802009-08-18 18:06:19 +0100196 return 0;
197
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100198error_unregister_bus_type:
199 bus_unregister(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100200error_nothing:
201 return ret;
202}
203
204static void __exit iio_exit(void)
205{
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100206 if (iio_devt)
207 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100208 bus_unregister(&iio_bus_type);
Michael Henneriche553f182012-03-01 10:51:03 +0100209 debugfs_remove(iio_debugfs_dentry);
210}
211
212#if defined(CONFIG_DEBUG_FS)
Michael Henneriche553f182012-03-01 10:51:03 +0100213static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
214 size_t count, loff_t *ppos)
215{
216 struct iio_dev *indio_dev = file->private_data;
217 char buf[20];
218 unsigned val = 0;
219 ssize_t len;
220 int ret;
221
222 ret = indio_dev->info->debugfs_reg_access(indio_dev,
223 indio_dev->cached_reg_addr,
224 0, &val);
225 if (ret)
226 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
227
228 len = snprintf(buf, sizeof(buf), "0x%X\n", val);
229
230 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
231}
232
233static ssize_t iio_debugfs_write_reg(struct file *file,
234 const char __user *userbuf, size_t count, loff_t *ppos)
235{
236 struct iio_dev *indio_dev = file->private_data;
237 unsigned reg, val;
238 char buf[80];
239 int ret;
240
241 count = min_t(size_t, count, (sizeof(buf)-1));
242 if (copy_from_user(buf, userbuf, count))
243 return -EFAULT;
244
245 buf[count] = 0;
246
247 ret = sscanf(buf, "%i %i", &reg, &val);
248
249 switch (ret) {
250 case 1:
251 indio_dev->cached_reg_addr = reg;
252 break;
253 case 2:
254 indio_dev->cached_reg_addr = reg;
255 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
256 val, NULL);
257 if (ret) {
258 dev_err(indio_dev->dev.parent, "%s: write failed\n",
259 __func__);
260 return ret;
261 }
262 break;
263 default:
264 return -EINVAL;
265 }
266
267 return count;
268}
269
270static const struct file_operations iio_debugfs_reg_fops = {
Axel Lin5a28c872012-05-03 09:50:51 +0800271 .open = simple_open,
Michael Henneriche553f182012-03-01 10:51:03 +0100272 .read = iio_debugfs_read_reg,
273 .write = iio_debugfs_write_reg,
274};
275
276static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
277{
278 debugfs_remove_recursive(indio_dev->debugfs_dentry);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100279}
280
Michael Henneriche553f182012-03-01 10:51:03 +0100281static int iio_device_register_debugfs(struct iio_dev *indio_dev)
282{
283 struct dentry *d;
284
285 if (indio_dev->info->debugfs_reg_access == NULL)
286 return 0;
287
Axel Linabd5a2f2012-05-03 22:56:58 +0800288 if (!iio_debugfs_dentry)
Michael Henneriche553f182012-03-01 10:51:03 +0100289 return 0;
290
291 indio_dev->debugfs_dentry =
292 debugfs_create_dir(dev_name(&indio_dev->dev),
293 iio_debugfs_dentry);
Michael Henneriche553f182012-03-01 10:51:03 +0100294 if (indio_dev->debugfs_dentry == NULL) {
295 dev_warn(indio_dev->dev.parent,
296 "Failed to create debugfs directory\n");
297 return -EFAULT;
298 }
299
300 d = debugfs_create_file("direct_reg_access", 0644,
301 indio_dev->debugfs_dentry,
302 indio_dev, &iio_debugfs_reg_fops);
303 if (!d) {
304 iio_device_unregister_debugfs(indio_dev);
305 return -ENOMEM;
306 }
307
308 return 0;
309}
310#else
311static int iio_device_register_debugfs(struct iio_dev *indio_dev)
312{
313 return 0;
314}
315
316static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
317{
318}
319#endif /* CONFIG_DEBUG_FS */
320
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100321static ssize_t iio_read_channel_ext_info(struct device *dev,
322 struct device_attribute *attr,
323 char *buf)
324{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200325 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100326 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
327 const struct iio_chan_spec_ext_info *ext_info;
328
329 ext_info = &this_attr->c->ext_info[this_attr->address];
330
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200331 return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100332}
333
334static ssize_t iio_write_channel_ext_info(struct device *dev,
335 struct device_attribute *attr,
336 const char *buf,
337 size_t len)
338{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200339 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100340 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
341 const struct iio_chan_spec_ext_info *ext_info;
342
343 ext_info = &this_attr->c->ext_info[this_attr->address];
344
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200345 return ext_info->write(indio_dev, ext_info->private,
346 this_attr->c, buf, len);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100347}
348
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200349ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
350 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
351{
352 const struct iio_enum *e = (const struct iio_enum *)priv;
353 unsigned int i;
354 size_t len = 0;
355
356 if (!e->num_items)
357 return 0;
358
359 for (i = 0; i < e->num_items; ++i)
Lars-Peter Clausen74dcd432012-06-05 18:24:12 +0200360 len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200361
362 /* replace last space with a newline */
363 buf[len - 1] = '\n';
364
365 return len;
366}
367EXPORT_SYMBOL_GPL(iio_enum_available_read);
368
369ssize_t iio_enum_read(struct iio_dev *indio_dev,
370 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
371{
372 const struct iio_enum *e = (const struct iio_enum *)priv;
373 int i;
374
375 if (!e->get)
376 return -EINVAL;
377
378 i = e->get(indio_dev, chan);
379 if (i < 0)
380 return i;
381 else if (i >= e->num_items)
382 return -EINVAL;
383
Kees Cook598db582014-03-13 16:46:00 +0000384 return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200385}
386EXPORT_SYMBOL_GPL(iio_enum_read);
387
388ssize_t iio_enum_write(struct iio_dev *indio_dev,
389 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
390 size_t len)
391{
392 const struct iio_enum *e = (const struct iio_enum *)priv;
393 unsigned int i;
394 int ret;
395
396 if (!e->set)
397 return -EINVAL;
398
399 for (i = 0; i < e->num_items; i++) {
400 if (sysfs_streq(buf, e->items[i]))
401 break;
402 }
403
404 if (i == e->num_items)
405 return -EINVAL;
406
407 ret = e->set(indio_dev, chan, i);
408 return ret ? ret : len;
409}
410EXPORT_SYMBOL_GPL(iio_enum_write);
411
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100412/**
413 * iio_format_value() - Formats a IIO value into its string representation
Cristina Opriceana2498dcf2015-07-24 16:16:19 +0300414 * @buf: The buffer to which the formatted value gets written
415 * @type: One of the IIO_VAL_... constants. This decides how the val
416 * and val2 parameters are formatted.
417 * @size: Number of IIO value entries contained in vals
418 * @vals: Pointer to the values, exact meaning depends on the
419 * type parameter.
420 *
421 * Return: 0 by default, a negative number on failure or the
422 * total number of characters written for a type that belongs
423 * to the IIO_VAL_... constant.
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100424 */
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100425ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100426{
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100427 unsigned long long tmp;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200428 bool scale_db = false;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100429
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100430 switch (type) {
Michael Hennerich67eedba2012-05-11 11:36:52 +0200431 case IIO_VAL_INT:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100432 return sprintf(buf, "%d\n", vals[0]);
Michael Hennerich67eedba2012-05-11 11:36:52 +0200433 case IIO_VAL_INT_PLUS_MICRO_DB:
434 scale_db = true;
435 case IIO_VAL_INT_PLUS_MICRO:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100436 if (vals[1] < 0)
Michal Nazarewicz8f57e4d2016-01-15 16:57:58 -0800437 return sprintf(buf, "-%d.%06u%s\n", abs(vals[0]),
438 -vals[1], scale_db ? " dB" : "");
Jonathan Cameron1d892712011-05-18 14:40:51 +0100439 else
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100440 return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1],
Michael Hennerich67eedba2012-05-11 11:36:52 +0200441 scale_db ? " dB" : "");
442 case IIO_VAL_INT_PLUS_NANO:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100443 if (vals[1] < 0)
Michal Nazarewicz8f57e4d2016-01-15 16:57:58 -0800444 return sprintf(buf, "-%d.%09u\n", abs(vals[0]),
445 -vals[1]);
Michael Hennerich71646e22011-06-27 13:07:07 +0100446 else
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100447 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100448 case IIO_VAL_FRACTIONAL:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100449 tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
450 vals[1] = do_div(tmp, 1000000000LL);
451 vals[0] = tmp;
452 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
Lars-Peter Clausen103d9fb2012-10-16 17:29:00 +0100453 case IIO_VAL_FRACTIONAL_LOG2:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100454 tmp = (s64)vals[0] * 1000000000LL >> vals[1];
455 vals[1] = do_div(tmp, 1000000000LL);
456 vals[0] = tmp;
457 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
458 case IIO_VAL_INT_MULTIPLE:
459 {
460 int i;
461 int len = 0;
462
463 for (i = 0; i < size; ++i)
464 len += snprintf(&buf[len], PAGE_SIZE - len, "%d ",
465 vals[i]);
466 len += snprintf(&buf[len], PAGE_SIZE - len, "\n");
467 return len;
468 }
Michael Hennerich67eedba2012-05-11 11:36:52 +0200469 default:
Jonathan Cameron1d892712011-05-18 14:40:51 +0100470 return 0;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200471 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100472}
Andrew F. Davis7d2c2aca2015-12-14 16:35:57 -0600473EXPORT_SYMBOL_GPL(iio_format_value);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100474
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100475static ssize_t iio_read_channel_info(struct device *dev,
476 struct device_attribute *attr,
477 char *buf)
478{
479 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
480 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100481 int vals[INDIO_MAX_RAW_ELEMENTS];
482 int ret;
483 int val_len = 2;
484
485 if (indio_dev->info->read_raw_multi)
486 ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
487 INDIO_MAX_RAW_ELEMENTS,
488 vals, &val_len,
489 this_attr->address);
490 else
491 ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
492 &vals[0], &vals[1], this_attr->address);
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100493
494 if (ret < 0)
495 return ret;
496
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100497 return iio_format_value(buf, ret, val_len, vals);
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100498}
499
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000500/**
501 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
502 * @str: The string to parse
503 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
504 * @integer: The integer part of the number
505 * @fract: The fractional part of the number
506 *
507 * Returns 0 on success, or a negative error code if the string could not be
508 * parsed.
509 */
510int iio_str_to_fixpoint(const char *str, int fract_mult,
511 int *integer, int *fract)
512{
513 int i = 0, f = 0;
514 bool integer_part = true, negative = false;
515
Sean Nyekjaerf47dff32015-11-09 13:55:34 +0100516 if (fract_mult == 0) {
517 *fract = 0;
518
519 return kstrtoint(str, 0, integer);
520 }
521
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000522 if (str[0] == '-') {
523 negative = true;
524 str++;
525 } else if (str[0] == '+') {
526 str++;
527 }
528
529 while (*str) {
530 if ('0' <= *str && *str <= '9') {
531 if (integer_part) {
532 i = i * 10 + *str - '0';
533 } else {
534 f += fract_mult * (*str - '0');
535 fract_mult /= 10;
536 }
537 } else if (*str == '\n') {
538 if (*(str + 1) == '\0')
539 break;
540 else
541 return -EINVAL;
542 } else if (*str == '.' && integer_part) {
543 integer_part = false;
544 } else {
545 return -EINVAL;
546 }
547 str++;
548 }
549
550 if (negative) {
551 if (i)
552 i = -i;
553 else
554 f = -f;
555 }
556
557 *integer = i;
558 *fract = f;
559
560 return 0;
561}
562EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
563
Jonathan Cameron1d892712011-05-18 14:40:51 +0100564static ssize_t iio_write_channel_info(struct device *dev,
565 struct device_attribute *attr,
566 const char *buf,
567 size_t len)
568{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200569 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100570 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000571 int ret, fract_mult = 100000;
572 int integer, fract;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100573
574 /* Assumes decimal - precision based on number of digits */
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100575 if (!indio_dev->info->write_raw)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100576 return -EINVAL;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100577
578 if (indio_dev->info->write_raw_get_fmt)
579 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
580 this_attr->c, this_attr->address)) {
Sean Nyekjaerf47dff32015-11-09 13:55:34 +0100581 case IIO_VAL_INT:
582 fract_mult = 0;
583 break;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100584 case IIO_VAL_INT_PLUS_MICRO:
585 fract_mult = 100000;
586 break;
587 case IIO_VAL_INT_PLUS_NANO:
588 fract_mult = 100000000;
589 break;
590 default:
591 return -EINVAL;
592 }
593
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000594 ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
595 if (ret)
596 return ret;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100597
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100598 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
Michael Hennerich5c04af02011-06-27 13:07:10 +0100599 integer, fract, this_attr->address);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100600 if (ret)
601 return ret;
602
603 return len;
604}
605
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100606static
Jonathan Cameron1d892712011-05-18 14:40:51 +0100607int __iio_device_attr_init(struct device_attribute *dev_attr,
608 const char *postfix,
609 struct iio_chan_spec const *chan,
610 ssize_t (*readfunc)(struct device *dev,
611 struct device_attribute *attr,
612 char *buf),
613 ssize_t (*writefunc)(struct device *dev,
614 struct device_attribute *attr,
615 const char *buf,
616 size_t len),
Jonathan Cameron37044322013-09-08 14:57:00 +0100617 enum iio_shared_by shared_by)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100618{
Jonathan Cameron37044322013-09-08 14:57:00 +0100619 int ret = 0;
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000620 char *name = NULL;
Jonathan Cameron37044322013-09-08 14:57:00 +0100621 char *full_postfix;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100622 sysfs_attr_init(&dev_attr->attr);
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100623
624 /* Build up postfix of <extend_name>_<modifier>_postfix */
Jonathan Cameron37044322013-09-08 14:57:00 +0100625 if (chan->modified && (shared_by == IIO_SEPARATE)) {
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100626 if (chan->extend_name)
627 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
628 iio_modifier_names[chan
629 ->channel2],
630 chan->extend_name,
631 postfix);
632 else
633 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
634 iio_modifier_names[chan
635 ->channel2],
636 postfix);
637 } else {
Lars-Peter Clausen77bfa8b2014-02-14 14:19:00 +0000638 if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100639 full_postfix = kstrdup(postfix, GFP_KERNEL);
640 else
641 full_postfix = kasprintf(GFP_KERNEL,
642 "%s_%s",
643 chan->extend_name,
644 postfix);
645 }
Jonathan Cameron37044322013-09-08 14:57:00 +0100646 if (full_postfix == NULL)
647 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100648
Justin P. Mattock4abf6f82012-02-29 22:00:38 -0800649 if (chan->differential) { /* Differential can not have modifier */
Jonathan Cameron37044322013-09-08 14:57:00 +0100650 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100651 case IIO_SHARED_BY_ALL:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000652 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100653 break;
654 case IIO_SHARED_BY_DIR:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000655 name = kasprintf(GFP_KERNEL, "%s_%s",
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100656 iio_direction[chan->output],
657 full_postfix);
658 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100659 case IIO_SHARED_BY_TYPE:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000660 name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100661 iio_direction[chan->output],
662 iio_chan_type_name_spec[chan->type],
663 iio_chan_type_name_spec[chan->type],
664 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100665 break;
666 case IIO_SEPARATE:
667 if (!chan->indexed) {
Dan Carpenter231bfe52015-11-21 13:33:00 +0300668 WARN(1, "Differential channels must be indexed\n");
Jonathan Cameron37044322013-09-08 14:57:00 +0100669 ret = -EINVAL;
670 goto error_free_full_postfix;
671 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000672 name = kasprintf(GFP_KERNEL,
Jonathan Cameron37044322013-09-08 14:57:00 +0100673 "%s_%s%d-%s%d_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100674 iio_direction[chan->output],
675 iio_chan_type_name_spec[chan->type],
676 chan->channel,
677 iio_chan_type_name_spec[chan->type],
678 chan->channel2,
679 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100680 break;
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100681 }
682 } else { /* Single ended */
Jonathan Cameron37044322013-09-08 14:57:00 +0100683 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100684 case IIO_SHARED_BY_ALL:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000685 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100686 break;
687 case IIO_SHARED_BY_DIR:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000688 name = kasprintf(GFP_KERNEL, "%s_%s",
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100689 iio_direction[chan->output],
690 full_postfix);
691 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100692 case IIO_SHARED_BY_TYPE:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000693 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100694 iio_direction[chan->output],
695 iio_chan_type_name_spec[chan->type],
696 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100697 break;
698
699 case IIO_SEPARATE:
700 if (chan->indexed)
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000701 name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
Jonathan Cameron37044322013-09-08 14:57:00 +0100702 iio_direction[chan->output],
703 iio_chan_type_name_spec[chan->type],
704 chan->channel,
705 full_postfix);
706 else
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000707 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
Jonathan Cameron37044322013-09-08 14:57:00 +0100708 iio_direction[chan->output],
709 iio_chan_type_name_spec[chan->type],
710 full_postfix);
711 break;
712 }
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100713 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000714 if (name == NULL) {
Jonathan Cameron1d892712011-05-18 14:40:51 +0100715 ret = -ENOMEM;
716 goto error_free_full_postfix;
717 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000718 dev_attr->attr.name = name;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100719
720 if (readfunc) {
721 dev_attr->attr.mode |= S_IRUGO;
722 dev_attr->show = readfunc;
723 }
724
725 if (writefunc) {
726 dev_attr->attr.mode |= S_IWUSR;
727 dev_attr->store = writefunc;
728 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000729
Jonathan Cameron1d892712011-05-18 14:40:51 +0100730error_free_full_postfix:
731 kfree(full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100732
Jonathan Cameron847ec802009-08-18 18:06:19 +0100733 return ret;
734}
735
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100736static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100737{
738 kfree(dev_attr->attr.name);
739}
740
741int __iio_add_chan_devattr(const char *postfix,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100742 struct iio_chan_spec const *chan,
743 ssize_t (*readfunc)(struct device *dev,
744 struct device_attribute *attr,
745 char *buf),
746 ssize_t (*writefunc)(struct device *dev,
747 struct device_attribute *attr,
748 const char *buf,
749 size_t len),
Jonathan Camerone614a542011-09-02 17:14:41 +0100750 u64 mask,
Jonathan Cameron37044322013-09-08 14:57:00 +0100751 enum iio_shared_by shared_by,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100752 struct device *dev,
753 struct list_head *attr_list)
754{
755 int ret;
756 struct iio_dev_attr *iio_attr, *t;
757
Sachin Kamat670c1102013-10-24 12:53:00 +0100758 iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000759 if (iio_attr == NULL)
760 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100761 ret = __iio_device_attr_init(&iio_attr->dev_attr,
762 postfix, chan,
Jonathan Cameron37044322013-09-08 14:57:00 +0100763 readfunc, writefunc, shared_by);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100764 if (ret)
765 goto error_iio_dev_attr_free;
766 iio_attr->c = chan;
767 iio_attr->address = mask;
768 list_for_each_entry(t, attr_list, l)
769 if (strcmp(t->dev_attr.attr.name,
770 iio_attr->dev_attr.attr.name) == 0) {
Jonathan Cameron37044322013-09-08 14:57:00 +0100771 if (shared_by == IIO_SEPARATE)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100772 dev_err(dev, "tried to double register : %s\n",
773 t->dev_attr.attr.name);
774 ret = -EBUSY;
775 goto error_device_attr_deinit;
776 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100777 list_add(&iio_attr->l, attr_list);
778
779 return 0;
780
781error_device_attr_deinit:
782 __iio_device_attr_deinit(&iio_attr->dev_attr);
783error_iio_dev_attr_free:
784 kfree(iio_attr);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100785 return ret;
786}
787
Jonathan Cameron37044322013-09-08 14:57:00 +0100788static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
789 struct iio_chan_spec const *chan,
790 enum iio_shared_by shared_by,
791 const long *infomask)
792{
793 int i, ret, attrcount = 0;
794
795 for_each_set_bit(i, infomask, sizeof(infomask)*8) {
Jonathan Cameronef4b4852014-01-03 22:24:00 +0000796 if (i >= ARRAY_SIZE(iio_chan_info_postfix))
797 return -EINVAL;
Jonathan Cameron37044322013-09-08 14:57:00 +0100798 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
799 chan,
800 &iio_read_channel_info,
801 &iio_write_channel_info,
802 i,
803 shared_by,
804 &indio_dev->dev,
805 &indio_dev->channel_attr_list);
806 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
807 continue;
808 else if (ret < 0)
809 return ret;
810 attrcount++;
811 }
812
813 return attrcount;
814}
815
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100816static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100817 struct iio_chan_spec const *chan)
818{
Jonathan Cameron5ccb3ad2012-04-15 17:41:16 +0100819 int ret, attrcount = 0;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100820 const struct iio_chan_spec_ext_info *ext_info;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100821
Jonathan Cameron1d892712011-05-18 14:40:51 +0100822 if (chan->channel < 0)
823 return 0;
Jonathan Cameron37044322013-09-08 14:57:00 +0100824 ret = iio_device_add_info_mask_type(indio_dev, chan,
825 IIO_SEPARATE,
826 &chan->info_mask_separate);
827 if (ret < 0)
828 return ret;
829 attrcount += ret;
830
831 ret = iio_device_add_info_mask_type(indio_dev, chan,
832 IIO_SHARED_BY_TYPE,
833 &chan->info_mask_shared_by_type);
834 if (ret < 0)
835 return ret;
836 attrcount += ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100837
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100838 ret = iio_device_add_info_mask_type(indio_dev, chan,
839 IIO_SHARED_BY_DIR,
840 &chan->info_mask_shared_by_dir);
841 if (ret < 0)
842 return ret;
843 attrcount += ret;
844
845 ret = iio_device_add_info_mask_type(indio_dev, chan,
846 IIO_SHARED_BY_ALL,
847 &chan->info_mask_shared_by_all);
848 if (ret < 0)
849 return ret;
850 attrcount += ret;
851
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100852 if (chan->ext_info) {
853 unsigned int i = 0;
854 for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
855 ret = __iio_add_chan_devattr(ext_info->name,
856 chan,
857 ext_info->read ?
858 &iio_read_channel_ext_info : NULL,
859 ext_info->write ?
860 &iio_write_channel_ext_info : NULL,
861 i,
862 ext_info->shared,
863 &indio_dev->dev,
864 &indio_dev->channel_attr_list);
865 i++;
866 if (ret == -EBUSY && ext_info->shared)
867 continue;
868
869 if (ret)
Jonathan Cameron37044322013-09-08 14:57:00 +0100870 return ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100871
872 attrcount++;
873 }
874 }
875
Jonathan Cameron37044322013-09-08 14:57:00 +0100876 return attrcount;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100877}
878
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100879/**
880 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
881 * @attr_list: List of IIO device attributes
882 *
883 * This function frees the memory allocated for each of the IIO device
Martin Fuzzeyc1b03ab2015-02-19 15:17:44 +0100884 * attributes in the list.
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100885 */
886void iio_free_chan_devattr_list(struct list_head *attr_list)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100887{
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100888 struct iio_dev_attr *p, *n;
889
890 list_for_each_entry_safe(p, n, attr_list, l) {
891 kfree(p->dev_attr.attr.name);
Martin Fuzzeyc1b03ab2015-02-19 15:17:44 +0100892 list_del(&p->l);
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100893 kfree(p);
894 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100895}
896
Jonathan Cameron1b732882011-05-18 14:41:43 +0100897static ssize_t iio_show_dev_name(struct device *dev,
898 struct device_attribute *attr,
899 char *buf)
900{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200901 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Kees Cook598db582014-03-13 16:46:00 +0000902 return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name);
Jonathan Cameron1b732882011-05-18 14:41:43 +0100903}
904
905static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
906
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100907static int iio_device_register_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100908{
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100909 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100910 struct iio_dev_attr *p;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100911 struct attribute **attr;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100912
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100913 /* First count elements in any existing group */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100914 if (indio_dev->info->attrs) {
915 attr = indio_dev->info->attrs->attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100916 while (*attr++ != NULL)
917 attrcount_orig++;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100918 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100919 attrcount = attrcount_orig;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100920 /*
921 * New channel registration method - relies on the fact a group does
Peter Meerwaldd25b3802012-08-26 13:43:00 +0100922 * not need to be initialized if its name is NULL.
Jonathan Cameron1d892712011-05-18 14:40:51 +0100923 */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100924 if (indio_dev->channels)
925 for (i = 0; i < indio_dev->num_channels; i++) {
926 ret = iio_device_add_channel_sysfs(indio_dev,
927 &indio_dev
Jonathan Cameron1d892712011-05-18 14:40:51 +0100928 ->channels[i]);
929 if (ret < 0)
930 goto error_clear_attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100931 attrcount += ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100932 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100933
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100934 if (indio_dev->name)
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100935 attrcount++;
936
Thomas Meyerd83fb182011-11-29 22:08:00 +0100937 indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
938 sizeof(indio_dev->chan_attr_group.attrs[0]),
939 GFP_KERNEL);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100940 if (indio_dev->chan_attr_group.attrs == NULL) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100941 ret = -ENOMEM;
942 goto error_clear_attrs;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100943 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100944 /* Copy across original attributes */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100945 if (indio_dev->info->attrs)
946 memcpy(indio_dev->chan_attr_group.attrs,
947 indio_dev->info->attrs->attrs,
948 sizeof(indio_dev->chan_attr_group.attrs[0])
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100949 *attrcount_orig);
950 attrn = attrcount_orig;
951 /* Add all elements from the list. */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100952 list_for_each_entry(p, &indio_dev->channel_attr_list, l)
953 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
954 if (indio_dev->name)
955 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100956
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100957 indio_dev->groups[indio_dev->groupcounter++] =
958 &indio_dev->chan_attr_group;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100959
Jonathan Cameron1d892712011-05-18 14:40:51 +0100960 return 0;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100961
Jonathan Cameron1d892712011-05-18 14:40:51 +0100962error_clear_attrs:
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100963 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100964
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100965 return ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100966}
967
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100968static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100969{
Jonathan Cameron1d892712011-05-18 14:40:51 +0100970
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100971 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100972 kfree(indio_dev->chan_attr_group.attrs);
Martin Fuzzeyc1b03ab2015-02-19 15:17:44 +0100973 indio_dev->chan_attr_group.attrs = NULL;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100974}
975
Jonathan Cameron847ec802009-08-18 18:06:19 +0100976static void iio_dev_release(struct device *device)
977{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200978 struct iio_dev *indio_dev = dev_to_iio_dev(device);
Vladimir Barinov735ad072015-08-20 22:37:39 +0300979 if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED))
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100980 iio_device_unregister_trigger_consumer(indio_dev);
981 iio_device_unregister_eventset(indio_dev);
982 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200983
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100984 iio_buffer_put(indio_dev->buffer);
985
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200986 ida_simple_remove(&iio_ida, indio_dev->id);
987 kfree(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100988}
989
Guenter Roeck17d82b42013-02-07 17:09:00 +0000990struct device_type iio_device_type = {
Jonathan Cameron847ec802009-08-18 18:06:19 +0100991 .name = "iio_device",
992 .release = iio_dev_release,
993};
994
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000995/**
996 * iio_device_alloc() - allocate an iio_dev from a driver
997 * @sizeof_priv: Space to allocate for private structure.
998 **/
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200999struct iio_dev *iio_device_alloc(int sizeof_priv)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001000{
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +01001001 struct iio_dev *dev;
1002 size_t alloc_size;
1003
1004 alloc_size = sizeof(struct iio_dev);
1005 if (sizeof_priv) {
1006 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
1007 alloc_size += sizeof_priv;
1008 }
1009 /* ensure 32-byte alignment of whole construct ? */
1010 alloc_size += IIO_ALIGN - 1;
1011
1012 dev = kzalloc(alloc_size, GFP_KERNEL);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001013
1014 if (dev) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001015 dev->dev.groups = dev->groups;
Guenter Roeck17d82b42013-02-07 17:09:00 +00001016 dev->dev.type = &iio_device_type;
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +01001017 dev->dev.bus = &iio_bus_type;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001018 device_initialize(&dev->dev);
1019 dev_set_drvdata(&dev->dev, (void *)dev);
1020 mutex_init(&dev->mlock);
Jonathan Cameronac917a82012-02-15 19:48:00 +00001021 mutex_init(&dev->info_exist_lock);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +02001022 INIT_LIST_HEAD(&dev->channel_attr_list);
Jonathan Camerona9e39f92011-09-14 13:01:25 +01001023
1024 dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
1025 if (dev->id < 0) {
1026 /* cannot use a dev_err as the name isn't available */
Sachin Kamat3176dd52013-10-24 12:53:00 +01001027 pr_err("failed to get device id\n");
Jonathan Camerona9e39f92011-09-14 13:01:25 +01001028 kfree(dev);
1029 return NULL;
1030 }
1031 dev_set_name(&dev->dev, "iio:device%d", dev->id);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001032 INIT_LIST_HEAD(&dev->buffer_list);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001033 }
1034
1035 return dev;
1036}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001037EXPORT_SYMBOL(iio_device_alloc);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001038
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001039/**
1040 * iio_device_free() - free an iio_dev from a driver
1041 * @dev: the iio_dev associated with the device
1042 **/
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001043void iio_device_free(struct iio_dev *dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001044{
Lars-Peter Clausene407fd62012-06-04 10:41:42 +02001045 if (dev)
1046 put_device(&dev->dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001047}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001048EXPORT_SYMBOL(iio_device_free);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001049
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001050static void devm_iio_device_release(struct device *dev, void *res)
1051{
1052 iio_device_free(*(struct iio_dev **)res);
1053}
1054
1055static int devm_iio_device_match(struct device *dev, void *res, void *data)
1056{
1057 struct iio_dev **r = res;
1058 if (!r || !*r) {
1059 WARN_ON(!r || !*r);
1060 return 0;
1061 }
1062 return *r == data;
1063}
1064
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001065/**
1066 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1067 * @dev: Device to allocate iio_dev for
1068 * @sizeof_priv: Space to allocate for private structure.
1069 *
1070 * Managed iio_device_alloc. iio_dev allocated with this function is
1071 * automatically freed on driver detach.
1072 *
1073 * If an iio_dev allocated with this function needs to be freed separately,
1074 * devm_iio_device_free() must be used.
1075 *
1076 * RETURNS:
1077 * Pointer to allocated iio_dev on success, NULL on failure.
1078 */
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001079struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
1080{
1081 struct iio_dev **ptr, *iio_dev;
1082
1083 ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
1084 GFP_KERNEL);
1085 if (!ptr)
1086 return NULL;
1087
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001088 iio_dev = iio_device_alloc(sizeof_priv);
1089 if (iio_dev) {
1090 *ptr = iio_dev;
1091 devres_add(dev, ptr);
1092 } else {
1093 devres_free(ptr);
1094 }
1095
1096 return iio_dev;
1097}
1098EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
1099
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001100/**
1101 * devm_iio_device_free - Resource-managed iio_device_free()
1102 * @dev: Device this iio_dev belongs to
1103 * @iio_dev: the iio_dev associated with the device
1104 *
1105 * Free iio_dev allocated with devm_iio_device_alloc().
1106 */
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001107void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
1108{
1109 int rc;
1110
1111 rc = devres_release(dev, devm_iio_device_release,
1112 devm_iio_device_match, iio_dev);
1113 WARN_ON(rc);
1114}
1115EXPORT_SYMBOL_GPL(devm_iio_device_free);
1116
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001117/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001118 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
Cristina Opriceana2498dcf2015-07-24 16:16:19 +03001119 * @inode: Inode structure for identifying the device in the file system
1120 * @filp: File structure for iio device used to keep and later access
1121 * private data
1122 *
1123 * Return: 0 on success or -EBUSY if the device is already opened
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001124 **/
1125static int iio_chrdev_open(struct inode *inode, struct file *filp)
1126{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001127 struct iio_dev *indio_dev = container_of(inode->i_cdev,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001128 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001129
1130 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
1131 return -EBUSY;
1132
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001133 iio_device_get(indio_dev);
1134
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001135 filp->private_data = indio_dev;
Jonathan Cameron30eb82f2011-09-21 11:16:02 +01001136
Lars-Peter Clausen79335142011-12-19 15:23:49 +01001137 return 0;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001138}
1139
1140/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001141 * iio_chrdev_release() - chrdev file close buffer access and ioctls
Cristina Opriceana2498dcf2015-07-24 16:16:19 +03001142 * @inode: Inode structure pointer for the char device
1143 * @filp: File structure pointer for the char device
1144 *
1145 * Return: 0 for successful release
1146 */
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001147static int iio_chrdev_release(struct inode *inode, struct file *filp)
1148{
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001149 struct iio_dev *indio_dev = container_of(inode->i_cdev,
1150 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001151 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001152 iio_device_put(indio_dev);
1153
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001154 return 0;
1155}
1156
1157/* Somewhat of a cross file organization violation - ioctls here are actually
1158 * event related */
1159static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1160{
1161 struct iio_dev *indio_dev = filp->private_data;
1162 int __user *ip = (int __user *)arg;
1163 int fd;
1164
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +01001165 if (!indio_dev->info)
1166 return -ENODEV;
1167
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001168 if (cmd == IIO_GET_EVENT_FD_IOCTL) {
1169 fd = iio_event_getfd(indio_dev);
Linus Walleij3f9059b2015-08-11 11:56:40 +02001170 if (fd < 0)
1171 return fd;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001172 if (copy_to_user(ip, &fd, sizeof(fd)))
1173 return -EFAULT;
1174 return 0;
1175 }
1176 return -EINVAL;
1177}
1178
Jonathan Cameron14555b12011-09-21 11:15:57 +01001179static const struct file_operations iio_buffer_fileops = {
1180 .read = iio_buffer_read_first_n_outer_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001181 .release = iio_chrdev_release,
1182 .open = iio_chrdev_open,
Jonathan Cameron14555b12011-09-21 11:15:57 +01001183 .poll = iio_buffer_poll_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001184 .owner = THIS_MODULE,
1185 .llseek = noop_llseek,
1186 .unlocked_ioctl = iio_ioctl,
1187 .compat_ioctl = iio_ioctl,
1188};
1189
Vlad Dogaru8f5d8722014-12-29 11:37:48 +02001190static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
1191{
1192 int i, j;
1193 const struct iio_chan_spec *channels = indio_dev->channels;
1194
1195 if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
1196 return 0;
1197
1198 for (i = 0; i < indio_dev->num_channels - 1; i++) {
1199 if (channels[i].scan_index < 0)
1200 continue;
1201 for (j = i + 1; j < indio_dev->num_channels; j++)
1202 if (channels[i].scan_index == channels[j].scan_index) {
1203 dev_err(&indio_dev->dev,
1204 "Duplicate scan index %d\n",
1205 channels[i].scan_index);
1206 return -EINVAL;
1207 }
1208 }
1209
1210 return 0;
1211}
1212
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001213static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1214
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001215/**
1216 * iio_device_register() - register a device with the IIO subsystem
1217 * @indio_dev: Device structure filled by the device driver
1218 **/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001219int iio_device_register(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001220{
1221 int ret;
1222
Guenter Roeck17d82b42013-02-07 17:09:00 +00001223 /* If the calling driver did not initialize of_node, do it here */
1224 if (!indio_dev->dev.of_node && indio_dev->dev.parent)
1225 indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
1226
Vlad Dogaru8f5d8722014-12-29 11:37:48 +02001227 ret = iio_check_unique_scan_index(indio_dev);
1228 if (ret < 0)
1229 return ret;
1230
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001231 /* configure elements for the chrdev */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001232 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001233
Michael Henneriche553f182012-03-01 10:51:03 +01001234 ret = iio_device_register_debugfs(indio_dev);
1235 if (ret) {
1236 dev_err(indio_dev->dev.parent,
1237 "Failed to register debugfs interfaces\n");
Hartmut Knaack92825ff2014-02-16 11:53:00 +00001238 return ret;
Michael Henneriche553f182012-03-01 10:51:03 +01001239 }
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001240
1241 ret = iio_buffer_alloc_sysfs_and_mask(indio_dev);
1242 if (ret) {
1243 dev_err(indio_dev->dev.parent,
1244 "Failed to create buffer sysfs interfaces\n");
1245 goto error_unreg_debugfs;
1246 }
1247
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001248 ret = iio_device_register_sysfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001249 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001250 dev_err(indio_dev->dev.parent,
Jonathan Cameron847ec802009-08-18 18:06:19 +01001251 "Failed to register sysfs interfaces\n");
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001252 goto error_buffer_free_sysfs;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001253 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001254 ret = iio_device_register_eventset(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001255 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001256 dev_err(indio_dev->dev.parent,
Roel Van Nyenc849d252010-04-29 19:27:31 +02001257 "Failed to register event set\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001258 goto error_free_sysfs;
1259 }
Vladimir Barinov735ad072015-08-20 22:37:39 +03001260 if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED))
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001261 iio_device_register_trigger_consumer(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001262
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001263 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1264 indio_dev->setup_ops == NULL)
1265 indio_dev->setup_ops = &noop_ring_setup_ops;
1266
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001267 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
1268 indio_dev->chrdev.owner = indio_dev->info->driver_module;
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001269 indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001270 ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001271 if (ret < 0)
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001272 goto error_unreg_eventset;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001273
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001274 ret = device_add(&indio_dev->dev);
1275 if (ret < 0)
1276 goto error_cdev_del;
1277
1278 return 0;
1279error_cdev_del:
1280 cdev_del(&indio_dev->chrdev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001281error_unreg_eventset:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001282 iio_device_unregister_eventset(indio_dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001283error_free_sysfs:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001284 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001285error_buffer_free_sysfs:
1286 iio_buffer_free_sysfs_and_mask(indio_dev);
Michael Henneriche553f182012-03-01 10:51:03 +01001287error_unreg_debugfs:
1288 iio_device_unregister_debugfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001289 return ret;
1290}
1291EXPORT_SYMBOL(iio_device_register);
1292
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001293/**
1294 * iio_device_unregister() - unregister a device from the IIO subsystem
1295 * @indio_dev: Device structure representing the device.
1296 **/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001297void iio_device_unregister(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001298{
Jonathan Cameronac917a82012-02-15 19:48:00 +00001299 mutex_lock(&indio_dev->info_exist_lock);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001300
1301 device_del(&indio_dev->dev);
1302
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001303 if (indio_dev->chrdev.dev)
1304 cdev_del(&indio_dev->chrdev);
Lars-Peter Clausenbc4c9612013-09-21 16:21:00 +01001305 iio_device_unregister_debugfs(indio_dev);
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001306
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001307 iio_disable_all_buffers(indio_dev);
1308
Jonathan Cameronac917a82012-02-15 19:48:00 +00001309 indio_dev->info = NULL;
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +01001310
1311 iio_device_wakeup_eventset(indio_dev);
1312 iio_buffer_wakeup_poll(indio_dev);
1313
Jonathan Cameronac917a82012-02-15 19:48:00 +00001314 mutex_unlock(&indio_dev->info_exist_lock);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001315
1316 iio_buffer_free_sysfs_and_mask(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001317}
1318EXPORT_SYMBOL(iio_device_unregister);
Sachin Kamat8caa07c2013-10-29 11:39:00 +00001319
1320static void devm_iio_device_unreg(struct device *dev, void *res)
1321{
1322 iio_device_unregister(*(struct iio_dev **)res);
1323}
1324
1325/**
1326 * devm_iio_device_register - Resource-managed iio_device_register()
1327 * @dev: Device to allocate iio_dev for
1328 * @indio_dev: Device structure filled by the device driver
1329 *
1330 * Managed iio_device_register. The IIO device registered with this
1331 * function is automatically unregistered on driver detach. This function
1332 * calls iio_device_register() internally. Refer to that function for more
1333 * information.
1334 *
1335 * If an iio_dev registered with this function needs to be unregistered
1336 * separately, devm_iio_device_unregister() must be used.
1337 *
1338 * RETURNS:
1339 * 0 on success, negative error number on failure.
1340 */
1341int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev)
1342{
1343 struct iio_dev **ptr;
1344 int ret;
1345
1346 ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL);
1347 if (!ptr)
1348 return -ENOMEM;
1349
1350 *ptr = indio_dev;
1351 ret = iio_device_register(indio_dev);
1352 if (!ret)
1353 devres_add(dev, ptr);
1354 else
1355 devres_free(ptr);
1356
1357 return ret;
1358}
1359EXPORT_SYMBOL_GPL(devm_iio_device_register);
1360
1361/**
1362 * devm_iio_device_unregister - Resource-managed iio_device_unregister()
1363 * @dev: Device this iio_dev belongs to
1364 * @indio_dev: the iio_dev associated with the device
1365 *
1366 * Unregister iio_dev registered with devm_iio_device_register().
1367 */
1368void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev)
1369{
1370 int rc;
1371
1372 rc = devres_release(dev, devm_iio_device_unreg,
1373 devm_iio_device_match, indio_dev);
1374 WARN_ON(rc);
1375}
1376EXPORT_SYMBOL_GPL(devm_iio_device_unregister);
1377
Jonathan Cameron847ec802009-08-18 18:06:19 +01001378subsys_initcall(iio_init);
1379module_exit(iio_exit);
1380
Jonathan Cameronc8b95952012-09-02 21:27:56 +01001381MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001382MODULE_DESCRIPTION("Industrial I/O core");
1383MODULE_LICENSE("GPL");