blob: 88353ae0ec07c3ec621eca31746444988e8d83cb [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>
Alison Schofield08a33802016-03-09 11:30:12 -080028#include <linux/mutex.h>
Jonathan Cameron06458e22012-04-25 15:54:58 +010029#include <linux/iio/iio.h>
Jonathan Camerondf9c1c42011-08-12 17:56:03 +010030#include "iio_core.h"
Jonathan Cameron6aea1c32011-08-24 17:28:38 +010031#include "iio_core_trigger.h"
Jonathan Cameron06458e22012-04-25 15:54:58 +010032#include <linux/iio/sysfs.h>
33#include <linux/iio/events.h>
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +010034#include <linux/iio/buffer.h>
Jonathan Cameron9dd1cb32011-08-30 12:41:15 +010035
Peter Meerwald99698b42012-08-26 13:43:00 +010036/* IDA to assign each registered device a unique id */
Jonathan Cameronb156cf72010-09-04 17:54:43 +010037static DEFINE_IDA(iio_ida);
Jonathan Cameron847ec802009-08-18 18:06:19 +010038
Jonathan Cameronf625cb92011-08-30 12:32:48 +010039static dev_t iio_devt;
Jonathan Cameron847ec802009-08-18 18:06:19 +010040
41#define IIO_DEV_MAX 256
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +010042struct bus_type iio_bus_type = {
Jonathan Cameron847ec802009-08-18 18:06:19 +010043 .name = "iio",
Jonathan Cameron847ec802009-08-18 18:06:19 +010044};
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +010045EXPORT_SYMBOL(iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +010046
Michael Henneriche553f182012-03-01 10:51:03 +010047static struct dentry *iio_debugfs_dentry;
48
Jonathan Cameronc6fc8062011-09-02 17:14:34 +010049static const char * const iio_direction[] = {
50 [0] = "in",
51 [1] = "out",
52};
53
Jonathan Cameronade7ef72011-09-02 17:14:45 +010054static const char * const iio_chan_type_name_spec[] = {
Jonathan Cameronc6fc8062011-09-02 17:14:34 +010055 [IIO_VOLTAGE] = "voltage",
Michael Hennerichfaf290e2011-05-18 14:42:02 +010056 [IIO_CURRENT] = "current",
57 [IIO_POWER] = "power",
Bryan Freed9bff02f2011-07-07 12:01:54 -070058 [IIO_ACCEL] = "accel",
Jonathan Cameron41ea0402011-10-05 15:27:59 +010059 [IIO_ANGL_VEL] = "anglvel",
Jonathan Cameron1d892712011-05-18 14:40:51 +010060 [IIO_MAGN] = "magn",
Bryan Freed9bff02f2011-07-07 12:01:54 -070061 [IIO_LIGHT] = "illuminance",
62 [IIO_INTENSITY] = "intensity",
Bryan Freedf09f2c82011-07-07 12:01:55 -070063 [IIO_PROXIMITY] = "proximity",
Bryan Freed9bff02f2011-07-07 12:01:54 -070064 [IIO_TEMP] = "temp",
Jonathan Cameron1d892712011-05-18 14:40:51 +010065 [IIO_INCLI] = "incli",
66 [IIO_ROT] = "rot",
Jonathan Cameron1d892712011-05-18 14:40:51 +010067 [IIO_ANGL] = "angl",
Bryan Freed9bff02f2011-07-07 12:01:54 -070068 [IIO_TIMESTAMP] = "timestamp",
Jonathan Cameron66dbe702011-09-02 17:14:43 +010069 [IIO_CAPACITANCE] = "capacitance",
Michael Hennericha6b12852012-04-27 10:58:34 +020070 [IIO_ALTVOLTAGE] = "altvoltage",
Jon Brenner21cd1fa2012-05-16 10:46:42 -050071 [IIO_CCT] = "cct",
Lars-Peter Clausenc4f0c692012-11-20 13:36:00 +000072 [IIO_PRESSURE] = "pressure",
Harald Geyerac216aa2013-12-01 15:08:00 +000073 [IIO_HUMIDITYRELATIVE] = "humidityrelative",
Daniel Baluta55aebeb2014-11-10 14:45:30 +020074 [IIO_ACTIVITY] = "activity",
Irina Tirdeaa88bfe72014-11-10 14:45:32 +020075 [IIO_STEPS] = "steps",
Irina Tirdea72c66642015-01-11 21:10:07 +020076 [IIO_ENERGY] = "energy",
Irina Tirdeacc3c9ee2015-01-11 21:10:08 +020077 [IIO_DISTANCE] = "distance",
Irina Tirdea5a1a9322015-01-11 21:10:09 +020078 [IIO_VELOCITY] = "velocity",
Matt Ranostay8ff6b3b2015-09-13 20:26:11 -070079 [IIO_CONCENTRATION] = "concentration",
Matt Ranostayd38d5462015-09-13 20:26:12 -070080 [IIO_RESISTANCE] = "resistance",
Matt Ranostayecb3a7c2016-01-26 18:34:30 -080081 [IIO_PH] = "ph",
Jonathan Cameron1d892712011-05-18 14:40:51 +010082};
83
Jonathan Cameron330c6c52011-09-02 17:14:39 +010084static const char * const iio_modifier_names[] = {
Jonathan Cameron1d892712011-05-18 14:40:51 +010085 [IIO_MOD_X] = "x",
86 [IIO_MOD_Y] = "y",
87 [IIO_MOD_Z] = "z",
Peter Meerwald4b8d8012015-06-20 23:52:30 +020088 [IIO_MOD_X_AND_Y] = "x&y",
89 [IIO_MOD_X_AND_Z] = "x&z",
90 [IIO_MOD_Y_AND_Z] = "y&z",
91 [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
92 [IIO_MOD_X_OR_Y] = "x|y",
93 [IIO_MOD_X_OR_Z] = "x|z",
94 [IIO_MOD_Y_OR_Z] = "y|z",
95 [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
Jonathan Cameron8f5879b2012-05-05 10:39:22 +010096 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
Jonathan Cameroncf82cb82012-05-05 10:56:41 +010097 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
Jonathan Cameron330c6c52011-09-02 17:14:39 +010098 [IIO_MOD_LIGHT_BOTH] = "both",
99 [IIO_MOD_LIGHT_IR] = "ir",
Jon Brenner21cd1fa2012-05-16 10:46:42 -0500100 [IIO_MOD_LIGHT_CLEAR] = "clear",
101 [IIO_MOD_LIGHT_RED] = "red",
102 [IIO_MOD_LIGHT_GREEN] = "green",
103 [IIO_MOD_LIGHT_BLUE] = "blue",
Peter Meerwald-Stadler2c5ff1f2016-03-20 16:20:22 +0100104 [IIO_MOD_LIGHT_UV] = "uv",
Srinivas Pandruvada5082f402014-04-29 00:51:00 +0100105 [IIO_MOD_QUATERNION] = "quaternion",
Peter Meerwald638b43b2014-02-05 16:57:00 +0000106 [IIO_MOD_TEMP_AMBIENT] = "ambient",
107 [IIO_MOD_TEMP_OBJECT] = "object",
Reyad Attiyat11b8dda2014-07-17 19:18:00 +0100108 [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
109 [IIO_MOD_NORTH_TRUE] = "from_north_true",
110 [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
111 [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
Daniel Baluta55aebeb2014-11-10 14:45:30 +0200112 [IIO_MOD_RUNNING] = "running",
113 [IIO_MOD_JOGGING] = "jogging",
114 [IIO_MOD_WALKING] = "walking",
115 [IIO_MOD_STILL] = "still",
Irina Tirdea5a1a9322015-01-11 21:10:09 +0200116 [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
Lars-Peter Clausen1ce87f22015-05-22 18:17:38 +0200117 [IIO_MOD_I] = "i",
118 [IIO_MOD_Q] = "q",
Matt Ranostay8ff6b3b2015-09-13 20:26:11 -0700119 [IIO_MOD_CO2] = "co2",
120 [IIO_MOD_VOC] = "voc",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100121};
122
123/* relies on pairs of these shared then separate */
124static const char * const iio_chan_info_postfix[] = {
Jonathan Cameron75a973c2012-04-15 17:41:30 +0100125 [IIO_CHAN_INFO_RAW] = "raw",
126 [IIO_CHAN_INFO_PROCESSED] = "input",
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100127 [IIO_CHAN_INFO_SCALE] = "scale",
128 [IIO_CHAN_INFO_OFFSET] = "offset",
129 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
130 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
131 [IIO_CHAN_INFO_PEAK] = "peak_raw",
132 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
133 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
134 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
Jonathan Camerondf94aba2011-11-27 11:39:12 +0000135 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
136 = "filter_low_pass_3db_frequency",
Martin Fuzzey3f7f6422015-05-13 12:26:42 +0200137 [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY]
138 = "filter_high_pass_3db_frequency",
Laxman Dewangance85a1c2012-04-13 16:03:31 +0530139 [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
Michael Hennericha6b12852012-04-27 10:58:34 +0200140 [IIO_CHAN_INFO_FREQUENCY] = "frequency",
141 [IIO_CHAN_INFO_PHASE] = "phase",
Michael Hennerichb65d6212012-05-11 11:36:53 +0200142 [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
srinivas pandruvada7c9ab032012-09-05 13:56:00 +0100143 [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
Peter Meerwald899d90b2013-09-08 16:20:00 +0100144 [IIO_CHAN_INFO_INT_TIME] = "integration_time",
Irina Tirdeaa88bfe72014-11-10 14:45:32 +0200145 [IIO_CHAN_INFO_ENABLE] = "en",
Irina Tirdeabcdf28f2014-11-10 14:45:33 +0200146 [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight",
Irina Tirdead37f6832015-01-11 21:10:10 +0200147 [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight",
Irina Tirdea2f0ecb72015-01-27 20:41:52 +0200148 [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count",
149 [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time",
Vianney le Clément de Saint-Marcqc8a85852015-03-30 10:34:58 +0200150 [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity",
Irina Tirdeafaaa4492015-04-29 21:16:39 +0300151 [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100152};
153
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000154/**
155 * iio_find_channel_from_si() - get channel from its scan index
156 * @indio_dev: device
157 * @si: scan index to match
158 */
Jonathan Cameron5fb21c82011-12-05 21:37:10 +0000159const struct iio_chan_spec
160*iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
161{
162 int i;
163
164 for (i = 0; i < indio_dev->num_channels; i++)
165 if (indio_dev->channels[i].scan_index == si)
166 return &indio_dev->channels[i];
167 return NULL;
168}
169
Jonathan Cameron847ec802009-08-18 18:06:19 +0100170/* This turns up an awful lot */
171ssize_t iio_read_const_attr(struct device *dev,
172 struct device_attribute *attr,
173 char *buf)
174{
175 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
176}
177EXPORT_SYMBOL(iio_read_const_attr);
178
Jonathan Cameron847ec802009-08-18 18:06:19 +0100179static int __init iio_init(void)
180{
181 int ret;
182
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100183 /* Register sysfs bus */
184 ret = bus_register(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100185 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100186 pr_err("could not register bus type\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +0100187 goto error_nothing;
188 }
189
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100190 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
191 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100192 pr_err("failed to allocate char dev region\n");
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100193 goto error_unregister_bus_type;
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100194 }
Jonathan Cameron847ec802009-08-18 18:06:19 +0100195
Michael Henneriche553f182012-03-01 10:51:03 +0100196 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
197
Jonathan Cameron847ec802009-08-18 18:06:19 +0100198 return 0;
199
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100200error_unregister_bus_type:
201 bus_unregister(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100202error_nothing:
203 return ret;
204}
205
206static void __exit iio_exit(void)
207{
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100208 if (iio_devt)
209 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100210 bus_unregister(&iio_bus_type);
Michael Henneriche553f182012-03-01 10:51:03 +0100211 debugfs_remove(iio_debugfs_dentry);
212}
213
214#if defined(CONFIG_DEBUG_FS)
Michael Henneriche553f182012-03-01 10:51:03 +0100215static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
216 size_t count, loff_t *ppos)
217{
218 struct iio_dev *indio_dev = file->private_data;
219 char buf[20];
220 unsigned val = 0;
221 ssize_t len;
222 int ret;
223
224 ret = indio_dev->info->debugfs_reg_access(indio_dev,
225 indio_dev->cached_reg_addr,
226 0, &val);
227 if (ret)
228 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
229
230 len = snprintf(buf, sizeof(buf), "0x%X\n", val);
231
232 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
233}
234
235static ssize_t iio_debugfs_write_reg(struct file *file,
236 const char __user *userbuf, size_t count, loff_t *ppos)
237{
238 struct iio_dev *indio_dev = file->private_data;
239 unsigned reg, val;
240 char buf[80];
241 int ret;
242
243 count = min_t(size_t, count, (sizeof(buf)-1));
244 if (copy_from_user(buf, userbuf, count))
245 return -EFAULT;
246
247 buf[count] = 0;
248
249 ret = sscanf(buf, "%i %i", &reg, &val);
250
251 switch (ret) {
252 case 1:
253 indio_dev->cached_reg_addr = reg;
254 break;
255 case 2:
256 indio_dev->cached_reg_addr = reg;
257 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
258 val, NULL);
259 if (ret) {
260 dev_err(indio_dev->dev.parent, "%s: write failed\n",
261 __func__);
262 return ret;
263 }
264 break;
265 default:
266 return -EINVAL;
267 }
268
269 return count;
270}
271
272static const struct file_operations iio_debugfs_reg_fops = {
Axel Lin5a28c872012-05-03 09:50:51 +0800273 .open = simple_open,
Michael Henneriche553f182012-03-01 10:51:03 +0100274 .read = iio_debugfs_read_reg,
275 .write = iio_debugfs_write_reg,
276};
277
278static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
279{
280 debugfs_remove_recursive(indio_dev->debugfs_dentry);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100281}
282
Michael Henneriche553f182012-03-01 10:51:03 +0100283static int iio_device_register_debugfs(struct iio_dev *indio_dev)
284{
285 struct dentry *d;
286
287 if (indio_dev->info->debugfs_reg_access == NULL)
288 return 0;
289
Axel Linabd5a2f2012-05-03 22:56:58 +0800290 if (!iio_debugfs_dentry)
Michael Henneriche553f182012-03-01 10:51:03 +0100291 return 0;
292
293 indio_dev->debugfs_dentry =
294 debugfs_create_dir(dev_name(&indio_dev->dev),
295 iio_debugfs_dentry);
Michael Henneriche553f182012-03-01 10:51:03 +0100296 if (indio_dev->debugfs_dentry == NULL) {
297 dev_warn(indio_dev->dev.parent,
298 "Failed to create debugfs directory\n");
299 return -EFAULT;
300 }
301
302 d = debugfs_create_file("direct_reg_access", 0644,
303 indio_dev->debugfs_dentry,
304 indio_dev, &iio_debugfs_reg_fops);
305 if (!d) {
306 iio_device_unregister_debugfs(indio_dev);
307 return -ENOMEM;
308 }
309
310 return 0;
311}
312#else
313static int iio_device_register_debugfs(struct iio_dev *indio_dev)
314{
315 return 0;
316}
317
318static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
319{
320}
321#endif /* CONFIG_DEBUG_FS */
322
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100323static ssize_t iio_read_channel_ext_info(struct device *dev,
324 struct device_attribute *attr,
325 char *buf)
326{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200327 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100328 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
329 const struct iio_chan_spec_ext_info *ext_info;
330
331 ext_info = &this_attr->c->ext_info[this_attr->address];
332
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200333 return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100334}
335
336static ssize_t iio_write_channel_ext_info(struct device *dev,
337 struct device_attribute *attr,
338 const char *buf,
339 size_t len)
340{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200341 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100342 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
343 const struct iio_chan_spec_ext_info *ext_info;
344
345 ext_info = &this_attr->c->ext_info[this_attr->address];
346
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200347 return ext_info->write(indio_dev, ext_info->private,
348 this_attr->c, buf, len);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100349}
350
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200351ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
352 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
353{
354 const struct iio_enum *e = (const struct iio_enum *)priv;
355 unsigned int i;
356 size_t len = 0;
357
358 if (!e->num_items)
359 return 0;
360
361 for (i = 0; i < e->num_items; ++i)
Lars-Peter Clausen74dcd432012-06-05 18:24:12 +0200362 len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200363
364 /* replace last space with a newline */
365 buf[len - 1] = '\n';
366
367 return len;
368}
369EXPORT_SYMBOL_GPL(iio_enum_available_read);
370
371ssize_t iio_enum_read(struct iio_dev *indio_dev,
372 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
373{
374 const struct iio_enum *e = (const struct iio_enum *)priv;
375 int i;
376
377 if (!e->get)
378 return -EINVAL;
379
380 i = e->get(indio_dev, chan);
381 if (i < 0)
382 return i;
383 else if (i >= e->num_items)
384 return -EINVAL;
385
Kees Cook598db582014-03-13 16:46:00 +0000386 return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200387}
388EXPORT_SYMBOL_GPL(iio_enum_read);
389
390ssize_t iio_enum_write(struct iio_dev *indio_dev,
391 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
392 size_t len)
393{
394 const struct iio_enum *e = (const struct iio_enum *)priv;
395 unsigned int i;
396 int ret;
397
398 if (!e->set)
399 return -EINVAL;
400
401 for (i = 0; i < e->num_items; i++) {
402 if (sysfs_streq(buf, e->items[i]))
403 break;
404 }
405
406 if (i == e->num_items)
407 return -EINVAL;
408
409 ret = e->set(indio_dev, chan, i);
410 return ret ? ret : len;
411}
412EXPORT_SYMBOL_GPL(iio_enum_write);
413
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100414/**
415 * iio_format_value() - Formats a IIO value into its string representation
Cristina Opriceana2498dcf2015-07-24 16:16:19 +0300416 * @buf: The buffer to which the formatted value gets written
417 * @type: One of the IIO_VAL_... constants. This decides how the val
418 * and val2 parameters are formatted.
419 * @size: Number of IIO value entries contained in vals
420 * @vals: Pointer to the values, exact meaning depends on the
421 * type parameter.
422 *
423 * Return: 0 by default, a negative number on failure or the
424 * total number of characters written for a type that belongs
425 * to the IIO_VAL_... constant.
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100426 */
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100427ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100428{
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100429 unsigned long long tmp;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200430 bool scale_db = false;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100431
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100432 switch (type) {
Michael Hennerich67eedba2012-05-11 11:36:52 +0200433 case IIO_VAL_INT:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100434 return sprintf(buf, "%d\n", vals[0]);
Michael Hennerich67eedba2012-05-11 11:36:52 +0200435 case IIO_VAL_INT_PLUS_MICRO_DB:
436 scale_db = true;
437 case IIO_VAL_INT_PLUS_MICRO:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100438 if (vals[1] < 0)
Michal Nazarewicz8f57e4d2016-01-15 16:57:58 -0800439 return sprintf(buf, "-%d.%06u%s\n", abs(vals[0]),
440 -vals[1], scale_db ? " dB" : "");
Jonathan Cameron1d892712011-05-18 14:40:51 +0100441 else
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100442 return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1],
Michael Hennerich67eedba2012-05-11 11:36:52 +0200443 scale_db ? " dB" : "");
444 case IIO_VAL_INT_PLUS_NANO:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100445 if (vals[1] < 0)
Michal Nazarewicz8f57e4d2016-01-15 16:57:58 -0800446 return sprintf(buf, "-%d.%09u\n", abs(vals[0]),
447 -vals[1]);
Michael Hennerich71646e22011-06-27 13:07:07 +0100448 else
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100449 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100450 case IIO_VAL_FRACTIONAL:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100451 tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
452 vals[1] = do_div(tmp, 1000000000LL);
453 vals[0] = tmp;
454 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
Lars-Peter Clausen103d9fb2012-10-16 17:29:00 +0100455 case IIO_VAL_FRACTIONAL_LOG2:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100456 tmp = (s64)vals[0] * 1000000000LL >> vals[1];
457 vals[1] = do_div(tmp, 1000000000LL);
458 vals[0] = tmp;
459 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
460 case IIO_VAL_INT_MULTIPLE:
461 {
462 int i;
463 int len = 0;
464
465 for (i = 0; i < size; ++i)
466 len += snprintf(&buf[len], PAGE_SIZE - len, "%d ",
467 vals[i]);
468 len += snprintf(&buf[len], PAGE_SIZE - len, "\n");
469 return len;
470 }
Michael Hennerich67eedba2012-05-11 11:36:52 +0200471 default:
Jonathan Cameron1d892712011-05-18 14:40:51 +0100472 return 0;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200473 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100474}
Andrew F. Davis7d2c2aca2015-12-14 16:35:57 -0600475EXPORT_SYMBOL_GPL(iio_format_value);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100476
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100477static ssize_t iio_read_channel_info(struct device *dev,
478 struct device_attribute *attr,
479 char *buf)
480{
481 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
482 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100483 int vals[INDIO_MAX_RAW_ELEMENTS];
484 int ret;
485 int val_len = 2;
486
487 if (indio_dev->info->read_raw_multi)
488 ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
489 INDIO_MAX_RAW_ELEMENTS,
490 vals, &val_len,
491 this_attr->address);
492 else
493 ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
494 &vals[0], &vals[1], this_attr->address);
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100495
496 if (ret < 0)
497 return ret;
498
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100499 return iio_format_value(buf, ret, val_len, vals);
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100500}
501
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000502/**
503 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
504 * @str: The string to parse
505 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
506 * @integer: The integer part of the number
507 * @fract: The fractional part of the number
508 *
509 * Returns 0 on success, or a negative error code if the string could not be
510 * parsed.
511 */
512int iio_str_to_fixpoint(const char *str, int fract_mult,
513 int *integer, int *fract)
514{
515 int i = 0, f = 0;
516 bool integer_part = true, negative = false;
517
Sean Nyekjaerf47dff32015-11-09 13:55:34 +0100518 if (fract_mult == 0) {
519 *fract = 0;
520
521 return kstrtoint(str, 0, integer);
522 }
523
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000524 if (str[0] == '-') {
525 negative = true;
526 str++;
527 } else if (str[0] == '+') {
528 str++;
529 }
530
531 while (*str) {
532 if ('0' <= *str && *str <= '9') {
533 if (integer_part) {
534 i = i * 10 + *str - '0';
535 } else {
536 f += fract_mult * (*str - '0');
537 fract_mult /= 10;
538 }
539 } else if (*str == '\n') {
540 if (*(str + 1) == '\0')
541 break;
542 else
543 return -EINVAL;
544 } else if (*str == '.' && integer_part) {
545 integer_part = false;
546 } else {
547 return -EINVAL;
548 }
549 str++;
550 }
551
552 if (negative) {
553 if (i)
554 i = -i;
555 else
556 f = -f;
557 }
558
559 *integer = i;
560 *fract = f;
561
562 return 0;
563}
564EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
565
Jonathan Cameron1d892712011-05-18 14:40:51 +0100566static ssize_t iio_write_channel_info(struct device *dev,
567 struct device_attribute *attr,
568 const char *buf,
569 size_t len)
570{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200571 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100572 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000573 int ret, fract_mult = 100000;
574 int integer, fract;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100575
576 /* Assumes decimal - precision based on number of digits */
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100577 if (!indio_dev->info->write_raw)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100578 return -EINVAL;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100579
580 if (indio_dev->info->write_raw_get_fmt)
581 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
582 this_attr->c, this_attr->address)) {
Sean Nyekjaerf47dff32015-11-09 13:55:34 +0100583 case IIO_VAL_INT:
584 fract_mult = 0;
585 break;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100586 case IIO_VAL_INT_PLUS_MICRO:
587 fract_mult = 100000;
588 break;
589 case IIO_VAL_INT_PLUS_NANO:
590 fract_mult = 100000000;
591 break;
592 default:
593 return -EINVAL;
594 }
595
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000596 ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
597 if (ret)
598 return ret;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100599
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100600 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
Michael Hennerich5c04af02011-06-27 13:07:10 +0100601 integer, fract, this_attr->address);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100602 if (ret)
603 return ret;
604
605 return len;
606}
607
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100608static
Jonathan Cameron1d892712011-05-18 14:40:51 +0100609int __iio_device_attr_init(struct device_attribute *dev_attr,
610 const char *postfix,
611 struct iio_chan_spec const *chan,
612 ssize_t (*readfunc)(struct device *dev,
613 struct device_attribute *attr,
614 char *buf),
615 ssize_t (*writefunc)(struct device *dev,
616 struct device_attribute *attr,
617 const char *buf,
618 size_t len),
Jonathan Cameron37044322013-09-08 14:57:00 +0100619 enum iio_shared_by shared_by)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100620{
Jonathan Cameron37044322013-09-08 14:57:00 +0100621 int ret = 0;
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000622 char *name = NULL;
Jonathan Cameron37044322013-09-08 14:57:00 +0100623 char *full_postfix;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100624 sysfs_attr_init(&dev_attr->attr);
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100625
626 /* Build up postfix of <extend_name>_<modifier>_postfix */
Jonathan Cameron37044322013-09-08 14:57:00 +0100627 if (chan->modified && (shared_by == IIO_SEPARATE)) {
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100628 if (chan->extend_name)
629 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
630 iio_modifier_names[chan
631 ->channel2],
632 chan->extend_name,
633 postfix);
634 else
635 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
636 iio_modifier_names[chan
637 ->channel2],
638 postfix);
639 } else {
Lars-Peter Clausen77bfa8b2014-02-14 14:19:00 +0000640 if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100641 full_postfix = kstrdup(postfix, GFP_KERNEL);
642 else
643 full_postfix = kasprintf(GFP_KERNEL,
644 "%s_%s",
645 chan->extend_name,
646 postfix);
647 }
Jonathan Cameron37044322013-09-08 14:57:00 +0100648 if (full_postfix == NULL)
649 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100650
Justin P. Mattock4abf6f82012-02-29 22:00:38 -0800651 if (chan->differential) { /* Differential can not have modifier */
Jonathan Cameron37044322013-09-08 14:57:00 +0100652 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100653 case IIO_SHARED_BY_ALL:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000654 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100655 break;
656 case IIO_SHARED_BY_DIR:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000657 name = kasprintf(GFP_KERNEL, "%s_%s",
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100658 iio_direction[chan->output],
659 full_postfix);
660 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100661 case IIO_SHARED_BY_TYPE:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000662 name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100663 iio_direction[chan->output],
664 iio_chan_type_name_spec[chan->type],
665 iio_chan_type_name_spec[chan->type],
666 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100667 break;
668 case IIO_SEPARATE:
669 if (!chan->indexed) {
Dan Carpenter231bfe52015-11-21 13:33:00 +0300670 WARN(1, "Differential channels must be indexed\n");
Jonathan Cameron37044322013-09-08 14:57:00 +0100671 ret = -EINVAL;
672 goto error_free_full_postfix;
673 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000674 name = kasprintf(GFP_KERNEL,
Jonathan Cameron37044322013-09-08 14:57:00 +0100675 "%s_%s%d-%s%d_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100676 iio_direction[chan->output],
677 iio_chan_type_name_spec[chan->type],
678 chan->channel,
679 iio_chan_type_name_spec[chan->type],
680 chan->channel2,
681 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100682 break;
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100683 }
684 } else { /* Single ended */
Jonathan Cameron37044322013-09-08 14:57:00 +0100685 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100686 case IIO_SHARED_BY_ALL:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000687 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100688 break;
689 case IIO_SHARED_BY_DIR:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000690 name = kasprintf(GFP_KERNEL, "%s_%s",
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100691 iio_direction[chan->output],
692 full_postfix);
693 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100694 case IIO_SHARED_BY_TYPE:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000695 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100696 iio_direction[chan->output],
697 iio_chan_type_name_spec[chan->type],
698 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100699 break;
700
701 case IIO_SEPARATE:
702 if (chan->indexed)
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000703 name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
Jonathan Cameron37044322013-09-08 14:57:00 +0100704 iio_direction[chan->output],
705 iio_chan_type_name_spec[chan->type],
706 chan->channel,
707 full_postfix);
708 else
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000709 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
Jonathan Cameron37044322013-09-08 14:57:00 +0100710 iio_direction[chan->output],
711 iio_chan_type_name_spec[chan->type],
712 full_postfix);
713 break;
714 }
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100715 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000716 if (name == NULL) {
Jonathan Cameron1d892712011-05-18 14:40:51 +0100717 ret = -ENOMEM;
718 goto error_free_full_postfix;
719 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000720 dev_attr->attr.name = name;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100721
722 if (readfunc) {
723 dev_attr->attr.mode |= S_IRUGO;
724 dev_attr->show = readfunc;
725 }
726
727 if (writefunc) {
728 dev_attr->attr.mode |= S_IWUSR;
729 dev_attr->store = writefunc;
730 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000731
Jonathan Cameron1d892712011-05-18 14:40:51 +0100732error_free_full_postfix:
733 kfree(full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100734
Jonathan Cameron847ec802009-08-18 18:06:19 +0100735 return ret;
736}
737
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100738static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100739{
740 kfree(dev_attr->attr.name);
741}
742
743int __iio_add_chan_devattr(const char *postfix,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100744 struct iio_chan_spec const *chan,
745 ssize_t (*readfunc)(struct device *dev,
746 struct device_attribute *attr,
747 char *buf),
748 ssize_t (*writefunc)(struct device *dev,
749 struct device_attribute *attr,
750 const char *buf,
751 size_t len),
Jonathan Camerone614a542011-09-02 17:14:41 +0100752 u64 mask,
Jonathan Cameron37044322013-09-08 14:57:00 +0100753 enum iio_shared_by shared_by,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100754 struct device *dev,
755 struct list_head *attr_list)
756{
757 int ret;
758 struct iio_dev_attr *iio_attr, *t;
759
Sachin Kamat670c1102013-10-24 12:53:00 +0100760 iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000761 if (iio_attr == NULL)
762 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100763 ret = __iio_device_attr_init(&iio_attr->dev_attr,
764 postfix, chan,
Jonathan Cameron37044322013-09-08 14:57:00 +0100765 readfunc, writefunc, shared_by);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100766 if (ret)
767 goto error_iio_dev_attr_free;
768 iio_attr->c = chan;
769 iio_attr->address = mask;
770 list_for_each_entry(t, attr_list, l)
771 if (strcmp(t->dev_attr.attr.name,
772 iio_attr->dev_attr.attr.name) == 0) {
Jonathan Cameron37044322013-09-08 14:57:00 +0100773 if (shared_by == IIO_SEPARATE)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100774 dev_err(dev, "tried to double register : %s\n",
775 t->dev_attr.attr.name);
776 ret = -EBUSY;
777 goto error_device_attr_deinit;
778 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100779 list_add(&iio_attr->l, attr_list);
780
781 return 0;
782
783error_device_attr_deinit:
784 __iio_device_attr_deinit(&iio_attr->dev_attr);
785error_iio_dev_attr_free:
786 kfree(iio_attr);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100787 return ret;
788}
789
Jonathan Cameron37044322013-09-08 14:57:00 +0100790static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
791 struct iio_chan_spec const *chan,
792 enum iio_shared_by shared_by,
793 const long *infomask)
794{
795 int i, ret, attrcount = 0;
796
797 for_each_set_bit(i, infomask, sizeof(infomask)*8) {
Jonathan Cameronef4b4852014-01-03 22:24:00 +0000798 if (i >= ARRAY_SIZE(iio_chan_info_postfix))
799 return -EINVAL;
Jonathan Cameron37044322013-09-08 14:57:00 +0100800 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
801 chan,
802 &iio_read_channel_info,
803 &iio_write_channel_info,
804 i,
805 shared_by,
806 &indio_dev->dev,
807 &indio_dev->channel_attr_list);
808 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
809 continue;
810 else if (ret < 0)
811 return ret;
812 attrcount++;
813 }
814
815 return attrcount;
816}
817
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100818static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100819 struct iio_chan_spec const *chan)
820{
Jonathan Cameron5ccb3ad2012-04-15 17:41:16 +0100821 int ret, attrcount = 0;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100822 const struct iio_chan_spec_ext_info *ext_info;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100823
Jonathan Cameron1d892712011-05-18 14:40:51 +0100824 if (chan->channel < 0)
825 return 0;
Jonathan Cameron37044322013-09-08 14:57:00 +0100826 ret = iio_device_add_info_mask_type(indio_dev, chan,
827 IIO_SEPARATE,
828 &chan->info_mask_separate);
829 if (ret < 0)
830 return ret;
831 attrcount += ret;
832
833 ret = iio_device_add_info_mask_type(indio_dev, chan,
834 IIO_SHARED_BY_TYPE,
835 &chan->info_mask_shared_by_type);
836 if (ret < 0)
837 return ret;
838 attrcount += ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100839
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100840 ret = iio_device_add_info_mask_type(indio_dev, chan,
841 IIO_SHARED_BY_DIR,
842 &chan->info_mask_shared_by_dir);
843 if (ret < 0)
844 return ret;
845 attrcount += ret;
846
847 ret = iio_device_add_info_mask_type(indio_dev, chan,
848 IIO_SHARED_BY_ALL,
849 &chan->info_mask_shared_by_all);
850 if (ret < 0)
851 return ret;
852 attrcount += ret;
853
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100854 if (chan->ext_info) {
855 unsigned int i = 0;
856 for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
857 ret = __iio_add_chan_devattr(ext_info->name,
858 chan,
859 ext_info->read ?
860 &iio_read_channel_ext_info : NULL,
861 ext_info->write ?
862 &iio_write_channel_ext_info : NULL,
863 i,
864 ext_info->shared,
865 &indio_dev->dev,
866 &indio_dev->channel_attr_list);
867 i++;
868 if (ret == -EBUSY && ext_info->shared)
869 continue;
870
871 if (ret)
Jonathan Cameron37044322013-09-08 14:57:00 +0100872 return ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100873
874 attrcount++;
875 }
876 }
877
Jonathan Cameron37044322013-09-08 14:57:00 +0100878 return attrcount;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100879}
880
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100881/**
882 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
883 * @attr_list: List of IIO device attributes
884 *
885 * This function frees the memory allocated for each of the IIO device
Martin Fuzzeyc1b03ab2015-02-19 15:17:44 +0100886 * attributes in the list.
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100887 */
888void iio_free_chan_devattr_list(struct list_head *attr_list)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100889{
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100890 struct iio_dev_attr *p, *n;
891
892 list_for_each_entry_safe(p, n, attr_list, l) {
893 kfree(p->dev_attr.attr.name);
Martin Fuzzeyc1b03ab2015-02-19 15:17:44 +0100894 list_del(&p->l);
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100895 kfree(p);
896 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100897}
898
Jonathan Cameron1b732882011-05-18 14:41:43 +0100899static ssize_t iio_show_dev_name(struct device *dev,
900 struct device_attribute *attr,
901 char *buf)
902{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200903 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Kees Cook598db582014-03-13 16:46:00 +0000904 return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name);
Jonathan Cameron1b732882011-05-18 14:41:43 +0100905}
906
907static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
908
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100909static int iio_device_register_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100910{
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100911 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100912 struct iio_dev_attr *p;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100913 struct attribute **attr;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100914
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100915 /* First count elements in any existing group */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100916 if (indio_dev->info->attrs) {
917 attr = indio_dev->info->attrs->attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100918 while (*attr++ != NULL)
919 attrcount_orig++;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100920 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100921 attrcount = attrcount_orig;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100922 /*
923 * New channel registration method - relies on the fact a group does
Peter Meerwaldd25b3802012-08-26 13:43:00 +0100924 * not need to be initialized if its name is NULL.
Jonathan Cameron1d892712011-05-18 14:40:51 +0100925 */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100926 if (indio_dev->channels)
927 for (i = 0; i < indio_dev->num_channels; i++) {
928 ret = iio_device_add_channel_sysfs(indio_dev,
929 &indio_dev
Jonathan Cameron1d892712011-05-18 14:40:51 +0100930 ->channels[i]);
931 if (ret < 0)
932 goto error_clear_attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100933 attrcount += ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100934 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100935
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100936 if (indio_dev->name)
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100937 attrcount++;
938
Thomas Meyerd83fb182011-11-29 22:08:00 +0100939 indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
940 sizeof(indio_dev->chan_attr_group.attrs[0]),
941 GFP_KERNEL);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100942 if (indio_dev->chan_attr_group.attrs == NULL) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100943 ret = -ENOMEM;
944 goto error_clear_attrs;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100945 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100946 /* Copy across original attributes */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100947 if (indio_dev->info->attrs)
948 memcpy(indio_dev->chan_attr_group.attrs,
949 indio_dev->info->attrs->attrs,
950 sizeof(indio_dev->chan_attr_group.attrs[0])
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100951 *attrcount_orig);
952 attrn = attrcount_orig;
953 /* Add all elements from the list. */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100954 list_for_each_entry(p, &indio_dev->channel_attr_list, l)
955 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
956 if (indio_dev->name)
957 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100958
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100959 indio_dev->groups[indio_dev->groupcounter++] =
960 &indio_dev->chan_attr_group;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100961
Jonathan Cameron1d892712011-05-18 14:40:51 +0100962 return 0;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100963
Jonathan Cameron1d892712011-05-18 14:40:51 +0100964error_clear_attrs:
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100965 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100966
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100967 return ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100968}
969
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100970static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100971{
Jonathan Cameron1d892712011-05-18 14:40:51 +0100972
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100973 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100974 kfree(indio_dev->chan_attr_group.attrs);
Martin Fuzzeyc1b03ab2015-02-19 15:17:44 +0100975 indio_dev->chan_attr_group.attrs = NULL;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100976}
977
Jonathan Cameron847ec802009-08-18 18:06:19 +0100978static void iio_dev_release(struct device *device)
979{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200980 struct iio_dev *indio_dev = dev_to_iio_dev(device);
Vladimir Barinov735ad072015-08-20 22:37:39 +0300981 if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED))
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100982 iio_device_unregister_trigger_consumer(indio_dev);
983 iio_device_unregister_eventset(indio_dev);
984 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200985
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100986 iio_buffer_put(indio_dev->buffer);
987
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200988 ida_simple_remove(&iio_ida, indio_dev->id);
989 kfree(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100990}
991
Guenter Roeck17d82b42013-02-07 17:09:00 +0000992struct device_type iio_device_type = {
Jonathan Cameron847ec802009-08-18 18:06:19 +0100993 .name = "iio_device",
994 .release = iio_dev_release,
995};
996
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000997/**
998 * iio_device_alloc() - allocate an iio_dev from a driver
999 * @sizeof_priv: Space to allocate for private structure.
1000 **/
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001001struct iio_dev *iio_device_alloc(int sizeof_priv)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001002{
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +01001003 struct iio_dev *dev;
1004 size_t alloc_size;
1005
1006 alloc_size = sizeof(struct iio_dev);
1007 if (sizeof_priv) {
1008 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
1009 alloc_size += sizeof_priv;
1010 }
1011 /* ensure 32-byte alignment of whole construct ? */
1012 alloc_size += IIO_ALIGN - 1;
1013
1014 dev = kzalloc(alloc_size, GFP_KERNEL);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001015
1016 if (dev) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001017 dev->dev.groups = dev->groups;
Guenter Roeck17d82b42013-02-07 17:09:00 +00001018 dev->dev.type = &iio_device_type;
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +01001019 dev->dev.bus = &iio_bus_type;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001020 device_initialize(&dev->dev);
1021 dev_set_drvdata(&dev->dev, (void *)dev);
1022 mutex_init(&dev->mlock);
Jonathan Cameronac917a82012-02-15 19:48:00 +00001023 mutex_init(&dev->info_exist_lock);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +02001024 INIT_LIST_HEAD(&dev->channel_attr_list);
Jonathan Camerona9e39f92011-09-14 13:01:25 +01001025
1026 dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
1027 if (dev->id < 0) {
1028 /* cannot use a dev_err as the name isn't available */
Sachin Kamat3176dd52013-10-24 12:53:00 +01001029 pr_err("failed to get device id\n");
Jonathan Camerona9e39f92011-09-14 13:01:25 +01001030 kfree(dev);
1031 return NULL;
1032 }
1033 dev_set_name(&dev->dev, "iio:device%d", dev->id);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001034 INIT_LIST_HEAD(&dev->buffer_list);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001035 }
1036
1037 return dev;
1038}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001039EXPORT_SYMBOL(iio_device_alloc);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001040
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001041/**
1042 * iio_device_free() - free an iio_dev from a driver
1043 * @dev: the iio_dev associated with the device
1044 **/
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001045void iio_device_free(struct iio_dev *dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001046{
Lars-Peter Clausene407fd62012-06-04 10:41:42 +02001047 if (dev)
1048 put_device(&dev->dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001049}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001050EXPORT_SYMBOL(iio_device_free);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001051
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001052static void devm_iio_device_release(struct device *dev, void *res)
1053{
1054 iio_device_free(*(struct iio_dev **)res);
1055}
1056
1057static int devm_iio_device_match(struct device *dev, void *res, void *data)
1058{
1059 struct iio_dev **r = res;
1060 if (!r || !*r) {
1061 WARN_ON(!r || !*r);
1062 return 0;
1063 }
1064 return *r == data;
1065}
1066
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001067/**
1068 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1069 * @dev: Device to allocate iio_dev for
1070 * @sizeof_priv: Space to allocate for private structure.
1071 *
1072 * Managed iio_device_alloc. iio_dev allocated with this function is
1073 * automatically freed on driver detach.
1074 *
1075 * If an iio_dev allocated with this function needs to be freed separately,
1076 * devm_iio_device_free() must be used.
1077 *
1078 * RETURNS:
1079 * Pointer to allocated iio_dev on success, NULL on failure.
1080 */
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001081struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
1082{
1083 struct iio_dev **ptr, *iio_dev;
1084
1085 ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
1086 GFP_KERNEL);
1087 if (!ptr)
1088 return NULL;
1089
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001090 iio_dev = iio_device_alloc(sizeof_priv);
1091 if (iio_dev) {
1092 *ptr = iio_dev;
1093 devres_add(dev, ptr);
1094 } else {
1095 devres_free(ptr);
1096 }
1097
1098 return iio_dev;
1099}
1100EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
1101
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001102/**
1103 * devm_iio_device_free - Resource-managed iio_device_free()
1104 * @dev: Device this iio_dev belongs to
1105 * @iio_dev: the iio_dev associated with the device
1106 *
1107 * Free iio_dev allocated with devm_iio_device_alloc().
1108 */
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001109void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
1110{
1111 int rc;
1112
1113 rc = devres_release(dev, devm_iio_device_release,
1114 devm_iio_device_match, iio_dev);
1115 WARN_ON(rc);
1116}
1117EXPORT_SYMBOL_GPL(devm_iio_device_free);
1118
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001119/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001120 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
Cristina Opriceana2498dcf2015-07-24 16:16:19 +03001121 * @inode: Inode structure for identifying the device in the file system
1122 * @filp: File structure for iio device used to keep and later access
1123 * private data
1124 *
1125 * Return: 0 on success or -EBUSY if the device is already opened
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001126 **/
1127static int iio_chrdev_open(struct inode *inode, struct file *filp)
1128{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001129 struct iio_dev *indio_dev = container_of(inode->i_cdev,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001130 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001131
1132 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
1133 return -EBUSY;
1134
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001135 iio_device_get(indio_dev);
1136
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001137 filp->private_data = indio_dev;
Jonathan Cameron30eb82f2011-09-21 11:16:02 +01001138
Lars-Peter Clausen79335142011-12-19 15:23:49 +01001139 return 0;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001140}
1141
1142/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001143 * iio_chrdev_release() - chrdev file close buffer access and ioctls
Cristina Opriceana2498dcf2015-07-24 16:16:19 +03001144 * @inode: Inode structure pointer for the char device
1145 * @filp: File structure pointer for the char device
1146 *
1147 * Return: 0 for successful release
1148 */
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001149static int iio_chrdev_release(struct inode *inode, struct file *filp)
1150{
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001151 struct iio_dev *indio_dev = container_of(inode->i_cdev,
1152 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001153 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001154 iio_device_put(indio_dev);
1155
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001156 return 0;
1157}
1158
1159/* Somewhat of a cross file organization violation - ioctls here are actually
1160 * event related */
1161static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1162{
1163 struct iio_dev *indio_dev = filp->private_data;
1164 int __user *ip = (int __user *)arg;
1165 int fd;
1166
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +01001167 if (!indio_dev->info)
1168 return -ENODEV;
1169
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001170 if (cmd == IIO_GET_EVENT_FD_IOCTL) {
1171 fd = iio_event_getfd(indio_dev);
Linus Walleij3f9059b2015-08-11 11:56:40 +02001172 if (fd < 0)
1173 return fd;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001174 if (copy_to_user(ip, &fd, sizeof(fd)))
1175 return -EFAULT;
1176 return 0;
1177 }
1178 return -EINVAL;
1179}
1180
Jonathan Cameron14555b12011-09-21 11:15:57 +01001181static const struct file_operations iio_buffer_fileops = {
1182 .read = iio_buffer_read_first_n_outer_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001183 .release = iio_chrdev_release,
1184 .open = iio_chrdev_open,
Jonathan Cameron14555b12011-09-21 11:15:57 +01001185 .poll = iio_buffer_poll_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001186 .owner = THIS_MODULE,
1187 .llseek = noop_llseek,
1188 .unlocked_ioctl = iio_ioctl,
1189 .compat_ioctl = iio_ioctl,
1190};
1191
Vlad Dogaru8f5d8722014-12-29 11:37:48 +02001192static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
1193{
1194 int i, j;
1195 const struct iio_chan_spec *channels = indio_dev->channels;
1196
1197 if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
1198 return 0;
1199
1200 for (i = 0; i < indio_dev->num_channels - 1; i++) {
1201 if (channels[i].scan_index < 0)
1202 continue;
1203 for (j = i + 1; j < indio_dev->num_channels; j++)
1204 if (channels[i].scan_index == channels[j].scan_index) {
1205 dev_err(&indio_dev->dev,
1206 "Duplicate scan index %d\n",
1207 channels[i].scan_index);
1208 return -EINVAL;
1209 }
1210 }
1211
1212 return 0;
1213}
1214
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001215static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1216
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001217/**
1218 * iio_device_register() - register a device with the IIO subsystem
1219 * @indio_dev: Device structure filled by the device driver
1220 **/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001221int iio_device_register(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001222{
1223 int ret;
1224
Guenter Roeck17d82b42013-02-07 17:09:00 +00001225 /* If the calling driver did not initialize of_node, do it here */
1226 if (!indio_dev->dev.of_node && indio_dev->dev.parent)
1227 indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
1228
Vlad Dogaru8f5d8722014-12-29 11:37:48 +02001229 ret = iio_check_unique_scan_index(indio_dev);
1230 if (ret < 0)
1231 return ret;
1232
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001233 /* configure elements for the chrdev */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001234 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001235
Michael Henneriche553f182012-03-01 10:51:03 +01001236 ret = iio_device_register_debugfs(indio_dev);
1237 if (ret) {
1238 dev_err(indio_dev->dev.parent,
1239 "Failed to register debugfs interfaces\n");
Hartmut Knaack92825ff2014-02-16 11:53:00 +00001240 return ret;
Michael Henneriche553f182012-03-01 10:51:03 +01001241 }
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001242
1243 ret = iio_buffer_alloc_sysfs_and_mask(indio_dev);
1244 if (ret) {
1245 dev_err(indio_dev->dev.parent,
1246 "Failed to create buffer sysfs interfaces\n");
1247 goto error_unreg_debugfs;
1248 }
1249
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001250 ret = iio_device_register_sysfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001251 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001252 dev_err(indio_dev->dev.parent,
Jonathan Cameron847ec802009-08-18 18:06:19 +01001253 "Failed to register sysfs interfaces\n");
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001254 goto error_buffer_free_sysfs;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001255 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001256 ret = iio_device_register_eventset(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001257 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001258 dev_err(indio_dev->dev.parent,
Roel Van Nyenc849d252010-04-29 19:27:31 +02001259 "Failed to register event set\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001260 goto error_free_sysfs;
1261 }
Vladimir Barinov735ad072015-08-20 22:37:39 +03001262 if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED))
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001263 iio_device_register_trigger_consumer(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001264
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001265 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1266 indio_dev->setup_ops == NULL)
1267 indio_dev->setup_ops = &noop_ring_setup_ops;
1268
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001269 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
1270 indio_dev->chrdev.owner = indio_dev->info->driver_module;
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001271 indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001272 ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001273 if (ret < 0)
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001274 goto error_unreg_eventset;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001275
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001276 ret = device_add(&indio_dev->dev);
1277 if (ret < 0)
1278 goto error_cdev_del;
1279
1280 return 0;
1281error_cdev_del:
1282 cdev_del(&indio_dev->chrdev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001283error_unreg_eventset:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001284 iio_device_unregister_eventset(indio_dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001285error_free_sysfs:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001286 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001287error_buffer_free_sysfs:
1288 iio_buffer_free_sysfs_and_mask(indio_dev);
Michael Henneriche553f182012-03-01 10:51:03 +01001289error_unreg_debugfs:
1290 iio_device_unregister_debugfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001291 return ret;
1292}
1293EXPORT_SYMBOL(iio_device_register);
1294
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001295/**
1296 * iio_device_unregister() - unregister a device from the IIO subsystem
1297 * @indio_dev: Device structure representing the device.
1298 **/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001299void iio_device_unregister(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001300{
Jonathan Cameronac917a82012-02-15 19:48:00 +00001301 mutex_lock(&indio_dev->info_exist_lock);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001302
1303 device_del(&indio_dev->dev);
1304
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001305 if (indio_dev->chrdev.dev)
1306 cdev_del(&indio_dev->chrdev);
Lars-Peter Clausenbc4c9612013-09-21 16:21:00 +01001307 iio_device_unregister_debugfs(indio_dev);
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001308
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001309 iio_disable_all_buffers(indio_dev);
1310
Jonathan Cameronac917a82012-02-15 19:48:00 +00001311 indio_dev->info = NULL;
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +01001312
1313 iio_device_wakeup_eventset(indio_dev);
1314 iio_buffer_wakeup_poll(indio_dev);
1315
Jonathan Cameronac917a82012-02-15 19:48:00 +00001316 mutex_unlock(&indio_dev->info_exist_lock);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001317
1318 iio_buffer_free_sysfs_and_mask(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001319}
1320EXPORT_SYMBOL(iio_device_unregister);
Sachin Kamat8caa07c2013-10-29 11:39:00 +00001321
1322static void devm_iio_device_unreg(struct device *dev, void *res)
1323{
1324 iio_device_unregister(*(struct iio_dev **)res);
1325}
1326
1327/**
1328 * devm_iio_device_register - Resource-managed iio_device_register()
1329 * @dev: Device to allocate iio_dev for
1330 * @indio_dev: Device structure filled by the device driver
1331 *
1332 * Managed iio_device_register. The IIO device registered with this
1333 * function is automatically unregistered on driver detach. This function
1334 * calls iio_device_register() internally. Refer to that function for more
1335 * information.
1336 *
1337 * If an iio_dev registered with this function needs to be unregistered
1338 * separately, devm_iio_device_unregister() must be used.
1339 *
1340 * RETURNS:
1341 * 0 on success, negative error number on failure.
1342 */
1343int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev)
1344{
1345 struct iio_dev **ptr;
1346 int ret;
1347
1348 ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL);
1349 if (!ptr)
1350 return -ENOMEM;
1351
1352 *ptr = indio_dev;
1353 ret = iio_device_register(indio_dev);
1354 if (!ret)
1355 devres_add(dev, ptr);
1356 else
1357 devres_free(ptr);
1358
1359 return ret;
1360}
1361EXPORT_SYMBOL_GPL(devm_iio_device_register);
1362
1363/**
1364 * devm_iio_device_unregister - Resource-managed iio_device_unregister()
1365 * @dev: Device this iio_dev belongs to
1366 * @indio_dev: the iio_dev associated with the device
1367 *
1368 * Unregister iio_dev registered with devm_iio_device_register().
1369 */
1370void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev)
1371{
1372 int rc;
1373
1374 rc = devres_release(dev, devm_iio_device_unreg,
1375 devm_iio_device_match, indio_dev);
1376 WARN_ON(rc);
1377}
1378EXPORT_SYMBOL_GPL(devm_iio_device_unregister);
1379
Alison Schofield08a33802016-03-09 11:30:12 -08001380/**
1381 * iio_device_claim_direct_mode - Keep device in direct mode
1382 * @indio_dev: the iio_dev associated with the device
1383 *
1384 * If the device is in direct mode it is guaranteed to stay
1385 * that way until iio_device_release_direct_mode() is called.
1386 *
1387 * Use with iio_device_release_direct_mode()
1388 *
1389 * Returns: 0 on success, -EBUSY on failure
1390 */
1391int iio_device_claim_direct_mode(struct iio_dev *indio_dev)
1392{
1393 mutex_lock(&indio_dev->mlock);
1394
1395 if (iio_buffer_enabled(indio_dev)) {
1396 mutex_unlock(&indio_dev->mlock);
1397 return -EBUSY;
1398 }
1399 return 0;
1400}
1401EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode);
1402
1403/**
1404 * iio_device_release_direct_mode - releases claim on direct mode
1405 * @indio_dev: the iio_dev associated with the device
1406 *
1407 * Release the claim. Device is no longer guaranteed to stay
1408 * in direct mode.
1409 *
1410 * Use with iio_device_claim_direct_mode()
1411 */
1412void iio_device_release_direct_mode(struct iio_dev *indio_dev)
1413{
1414 mutex_unlock(&indio_dev->mlock);
1415}
1416EXPORT_SYMBOL_GPL(iio_device_release_direct_mode);
1417
Jonathan Cameron847ec802009-08-18 18:06:19 +01001418subsys_initcall(iio_init);
1419module_exit(iio_exit);
1420
Jonathan Cameronc8b95952012-09-02 21:27:56 +01001421MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001422MODULE_DESCRIPTION("Industrial I/O core");
1423MODULE_LICENSE("GPL");