blob: ee09a945a2c7abb5f5593b8fec1a58dba501f5e8 [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",
Jonathan Cameron1d892712011-05-18 14:40:51 +010079};
80
Jonathan Cameron330c6c52011-09-02 17:14:39 +010081static const char * const iio_modifier_names[] = {
Jonathan Cameron1d892712011-05-18 14:40:51 +010082 [IIO_MOD_X] = "x",
83 [IIO_MOD_Y] = "y",
84 [IIO_MOD_Z] = "z",
Peter Meerwald4b8d8012015-06-20 23:52:30 +020085 [IIO_MOD_X_AND_Y] = "x&y",
86 [IIO_MOD_X_AND_Z] = "x&z",
87 [IIO_MOD_Y_AND_Z] = "y&z",
88 [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
89 [IIO_MOD_X_OR_Y] = "x|y",
90 [IIO_MOD_X_OR_Z] = "x|z",
91 [IIO_MOD_Y_OR_Z] = "y|z",
92 [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
Jonathan Cameron8f5879b2012-05-05 10:39:22 +010093 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
Jonathan Cameroncf82cb82012-05-05 10:56:41 +010094 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
Jonathan Cameron330c6c52011-09-02 17:14:39 +010095 [IIO_MOD_LIGHT_BOTH] = "both",
96 [IIO_MOD_LIGHT_IR] = "ir",
Jon Brenner21cd1fa2012-05-16 10:46:42 -050097 [IIO_MOD_LIGHT_CLEAR] = "clear",
98 [IIO_MOD_LIGHT_RED] = "red",
99 [IIO_MOD_LIGHT_GREEN] = "green",
100 [IIO_MOD_LIGHT_BLUE] = "blue",
Srinivas Pandruvada5082f402014-04-29 00:51:00 +0100101 [IIO_MOD_QUATERNION] = "quaternion",
Peter Meerwald638b43b2014-02-05 16:57:00 +0000102 [IIO_MOD_TEMP_AMBIENT] = "ambient",
103 [IIO_MOD_TEMP_OBJECT] = "object",
Reyad Attiyat11b8dda2014-07-17 19:18:00 +0100104 [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
105 [IIO_MOD_NORTH_TRUE] = "from_north_true",
106 [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
107 [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
Daniel Baluta55aebeb2014-11-10 14:45:30 +0200108 [IIO_MOD_RUNNING] = "running",
109 [IIO_MOD_JOGGING] = "jogging",
110 [IIO_MOD_WALKING] = "walking",
111 [IIO_MOD_STILL] = "still",
Irina Tirdea5a1a9322015-01-11 21:10:09 +0200112 [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
Lars-Peter Clausen1ce87f22015-05-22 18:17:38 +0200113 [IIO_MOD_I] = "i",
114 [IIO_MOD_Q] = "q",
Matt Ranostay8ff6b3b2015-09-13 20:26:11 -0700115 [IIO_MOD_CO2] = "co2",
116 [IIO_MOD_VOC] = "voc",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100117};
118
119/* relies on pairs of these shared then separate */
120static const char * const iio_chan_info_postfix[] = {
Jonathan Cameron75a973c2012-04-15 17:41:30 +0100121 [IIO_CHAN_INFO_RAW] = "raw",
122 [IIO_CHAN_INFO_PROCESSED] = "input",
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100123 [IIO_CHAN_INFO_SCALE] = "scale",
124 [IIO_CHAN_INFO_OFFSET] = "offset",
125 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
126 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
127 [IIO_CHAN_INFO_PEAK] = "peak_raw",
128 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
129 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
130 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
Jonathan Camerondf94aba2011-11-27 11:39:12 +0000131 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
132 = "filter_low_pass_3db_frequency",
Martin Fuzzey3f7f6422015-05-13 12:26:42 +0200133 [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY]
134 = "filter_high_pass_3db_frequency",
Laxman Dewangance85a1c2012-04-13 16:03:31 +0530135 [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
Michael Hennericha6b12852012-04-27 10:58:34 +0200136 [IIO_CHAN_INFO_FREQUENCY] = "frequency",
137 [IIO_CHAN_INFO_PHASE] = "phase",
Michael Hennerichb65d6212012-05-11 11:36:53 +0200138 [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
srinivas pandruvada7c9ab032012-09-05 13:56:00 +0100139 [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
Peter Meerwald899d90b2013-09-08 16:20:00 +0100140 [IIO_CHAN_INFO_INT_TIME] = "integration_time",
Irina Tirdeaa88bfe72014-11-10 14:45:32 +0200141 [IIO_CHAN_INFO_ENABLE] = "en",
Irina Tirdeabcdf28f2014-11-10 14:45:33 +0200142 [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight",
Irina Tirdead37f6832015-01-11 21:10:10 +0200143 [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight",
Irina Tirdea2f0ecb72015-01-27 20:41:52 +0200144 [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count",
145 [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time",
Vianney le Clément de Saint-Marcqc8a85852015-03-30 10:34:58 +0200146 [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity",
Irina Tirdeafaaa4492015-04-29 21:16:39 +0300147 [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100148};
149
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000150/**
151 * iio_find_channel_from_si() - get channel from its scan index
152 * @indio_dev: device
153 * @si: scan index to match
154 */
Jonathan Cameron5fb21c82011-12-05 21:37:10 +0000155const struct iio_chan_spec
156*iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
157{
158 int i;
159
160 for (i = 0; i < indio_dev->num_channels; i++)
161 if (indio_dev->channels[i].scan_index == si)
162 return &indio_dev->channels[i];
163 return NULL;
164}
165
Jonathan Cameron847ec802009-08-18 18:06:19 +0100166/* This turns up an awful lot */
167ssize_t iio_read_const_attr(struct device *dev,
168 struct device_attribute *attr,
169 char *buf)
170{
171 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
172}
173EXPORT_SYMBOL(iio_read_const_attr);
174
Jonathan Cameron847ec802009-08-18 18:06:19 +0100175static int __init iio_init(void)
176{
177 int ret;
178
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100179 /* Register sysfs bus */
180 ret = bus_register(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100181 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100182 pr_err("could not register bus type\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +0100183 goto error_nothing;
184 }
185
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100186 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
187 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100188 pr_err("failed to allocate char dev region\n");
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100189 goto error_unregister_bus_type;
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100190 }
Jonathan Cameron847ec802009-08-18 18:06:19 +0100191
Michael Henneriche553f182012-03-01 10:51:03 +0100192 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
193
Jonathan Cameron847ec802009-08-18 18:06:19 +0100194 return 0;
195
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100196error_unregister_bus_type:
197 bus_unregister(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100198error_nothing:
199 return ret;
200}
201
202static void __exit iio_exit(void)
203{
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100204 if (iio_devt)
205 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100206 bus_unregister(&iio_bus_type);
Michael Henneriche553f182012-03-01 10:51:03 +0100207 debugfs_remove(iio_debugfs_dentry);
208}
209
210#if defined(CONFIG_DEBUG_FS)
Michael Henneriche553f182012-03-01 10:51:03 +0100211static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
212 size_t count, loff_t *ppos)
213{
214 struct iio_dev *indio_dev = file->private_data;
215 char buf[20];
216 unsigned val = 0;
217 ssize_t len;
218 int ret;
219
220 ret = indio_dev->info->debugfs_reg_access(indio_dev,
221 indio_dev->cached_reg_addr,
222 0, &val);
223 if (ret)
224 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
225
226 len = snprintf(buf, sizeof(buf), "0x%X\n", val);
227
228 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
229}
230
231static ssize_t iio_debugfs_write_reg(struct file *file,
232 const char __user *userbuf, size_t count, loff_t *ppos)
233{
234 struct iio_dev *indio_dev = file->private_data;
235 unsigned reg, val;
236 char buf[80];
237 int ret;
238
239 count = min_t(size_t, count, (sizeof(buf)-1));
240 if (copy_from_user(buf, userbuf, count))
241 return -EFAULT;
242
243 buf[count] = 0;
244
245 ret = sscanf(buf, "%i %i", &reg, &val);
246
247 switch (ret) {
248 case 1:
249 indio_dev->cached_reg_addr = reg;
250 break;
251 case 2:
252 indio_dev->cached_reg_addr = reg;
253 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
254 val, NULL);
255 if (ret) {
256 dev_err(indio_dev->dev.parent, "%s: write failed\n",
257 __func__);
258 return ret;
259 }
260 break;
261 default:
262 return -EINVAL;
263 }
264
265 return count;
266}
267
268static const struct file_operations iio_debugfs_reg_fops = {
Axel Lin5a28c872012-05-03 09:50:51 +0800269 .open = simple_open,
Michael Henneriche553f182012-03-01 10:51:03 +0100270 .read = iio_debugfs_read_reg,
271 .write = iio_debugfs_write_reg,
272};
273
274static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
275{
276 debugfs_remove_recursive(indio_dev->debugfs_dentry);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100277}
278
Michael Henneriche553f182012-03-01 10:51:03 +0100279static int iio_device_register_debugfs(struct iio_dev *indio_dev)
280{
281 struct dentry *d;
282
283 if (indio_dev->info->debugfs_reg_access == NULL)
284 return 0;
285
Axel Linabd5a2f2012-05-03 22:56:58 +0800286 if (!iio_debugfs_dentry)
Michael Henneriche553f182012-03-01 10:51:03 +0100287 return 0;
288
289 indio_dev->debugfs_dentry =
290 debugfs_create_dir(dev_name(&indio_dev->dev),
291 iio_debugfs_dentry);
Michael Henneriche553f182012-03-01 10:51:03 +0100292 if (indio_dev->debugfs_dentry == NULL) {
293 dev_warn(indio_dev->dev.parent,
294 "Failed to create debugfs directory\n");
295 return -EFAULT;
296 }
297
298 d = debugfs_create_file("direct_reg_access", 0644,
299 indio_dev->debugfs_dentry,
300 indio_dev, &iio_debugfs_reg_fops);
301 if (!d) {
302 iio_device_unregister_debugfs(indio_dev);
303 return -ENOMEM;
304 }
305
306 return 0;
307}
308#else
309static int iio_device_register_debugfs(struct iio_dev *indio_dev)
310{
311 return 0;
312}
313
314static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
315{
316}
317#endif /* CONFIG_DEBUG_FS */
318
Lars-Peter Clausen4fee7e162012-03-06 20:43:45 +0100319static ssize_t iio_read_channel_ext_info(struct device *dev,
320 struct device_attribute *attr,
321 char *buf)
322{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200323 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e162012-03-06 20:43:45 +0100324 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
325 const struct iio_chan_spec_ext_info *ext_info;
326
327 ext_info = &this_attr->c->ext_info[this_attr->address];
328
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200329 return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
Lars-Peter Clausen4fee7e162012-03-06 20:43:45 +0100330}
331
332static ssize_t iio_write_channel_ext_info(struct device *dev,
333 struct device_attribute *attr,
334 const char *buf,
335 size_t len)
336{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200337 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e162012-03-06 20:43:45 +0100338 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
339 const struct iio_chan_spec_ext_info *ext_info;
340
341 ext_info = &this_attr->c->ext_info[this_attr->address];
342
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200343 return ext_info->write(indio_dev, ext_info->private,
344 this_attr->c, buf, len);
Lars-Peter Clausen4fee7e162012-03-06 20:43:45 +0100345}
346
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200347ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
348 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
349{
350 const struct iio_enum *e = (const struct iio_enum *)priv;
351 unsigned int i;
352 size_t len = 0;
353
354 if (!e->num_items)
355 return 0;
356
357 for (i = 0; i < e->num_items; ++i)
Lars-Peter Clausen74dcd432012-06-05 18:24:12 +0200358 len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200359
360 /* replace last space with a newline */
361 buf[len - 1] = '\n';
362
363 return len;
364}
365EXPORT_SYMBOL_GPL(iio_enum_available_read);
366
367ssize_t iio_enum_read(struct iio_dev *indio_dev,
368 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
369{
370 const struct iio_enum *e = (const struct iio_enum *)priv;
371 int i;
372
373 if (!e->get)
374 return -EINVAL;
375
376 i = e->get(indio_dev, chan);
377 if (i < 0)
378 return i;
379 else if (i >= e->num_items)
380 return -EINVAL;
381
Kees Cook598db582014-03-13 16:46:00 +0000382 return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200383}
384EXPORT_SYMBOL_GPL(iio_enum_read);
385
386ssize_t iio_enum_write(struct iio_dev *indio_dev,
387 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
388 size_t len)
389{
390 const struct iio_enum *e = (const struct iio_enum *)priv;
391 unsigned int i;
392 int ret;
393
394 if (!e->set)
395 return -EINVAL;
396
397 for (i = 0; i < e->num_items; i++) {
398 if (sysfs_streq(buf, e->items[i]))
399 break;
400 }
401
402 if (i == e->num_items)
403 return -EINVAL;
404
405 ret = e->set(indio_dev, chan, i);
406 return ret ? ret : len;
407}
408EXPORT_SYMBOL_GPL(iio_enum_write);
409
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100410/**
411 * iio_format_value() - Formats a IIO value into its string representation
Cristina Opriceana2498dcf2015-07-24 16:16:19 +0300412 * @buf: The buffer to which the formatted value gets written
413 * @type: One of the IIO_VAL_... constants. This decides how the val
414 * and val2 parameters are formatted.
415 * @size: Number of IIO value entries contained in vals
416 * @vals: Pointer to the values, exact meaning depends on the
417 * type parameter.
418 *
419 * Return: 0 by default, a negative number on failure or the
420 * total number of characters written for a type that belongs
421 * to the IIO_VAL_... constant.
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100422 */
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100423ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100424{
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100425 unsigned long long tmp;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200426 bool scale_db = false;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100427
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100428 switch (type) {
Michael Hennerich67eedba2012-05-11 11:36:52 +0200429 case IIO_VAL_INT:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100430 return sprintf(buf, "%d\n", vals[0]);
Michael Hennerich67eedba2012-05-11 11:36:52 +0200431 case IIO_VAL_INT_PLUS_MICRO_DB:
432 scale_db = true;
433 case IIO_VAL_INT_PLUS_MICRO:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100434 if (vals[1] < 0)
435 return sprintf(buf, "-%ld.%06u%s\n", abs(vals[0]),
436 -vals[1],
Michael Hennerich67eedba2012-05-11 11:36:52 +0200437 scale_db ? " dB" : "");
Jonathan Cameron1d892712011-05-18 14:40:51 +0100438 else
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100439 return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1],
Michael Hennerich67eedba2012-05-11 11:36:52 +0200440 scale_db ? " dB" : "");
441 case IIO_VAL_INT_PLUS_NANO:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100442 if (vals[1] < 0)
443 return sprintf(buf, "-%ld.%09u\n", abs(vals[0]),
444 -vals[1]);
Michael Hennerich71646e22011-06-27 13:07:07 +0100445 else
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100446 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100447 case IIO_VAL_FRACTIONAL:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100448 tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
449 vals[1] = do_div(tmp, 1000000000LL);
450 vals[0] = tmp;
451 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
Lars-Peter Clausen103d9fb2012-10-16 17:29:00 +0100452 case IIO_VAL_FRACTIONAL_LOG2:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100453 tmp = (s64)vals[0] * 1000000000LL >> vals[1];
454 vals[1] = do_div(tmp, 1000000000LL);
455 vals[0] = tmp;
456 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
457 case IIO_VAL_INT_MULTIPLE:
458 {
459 int i;
460 int len = 0;
461
462 for (i = 0; i < size; ++i)
463 len += snprintf(&buf[len], PAGE_SIZE - len, "%d ",
464 vals[i]);
465 len += snprintf(&buf[len], PAGE_SIZE - len, "\n");
466 return len;
467 }
Michael Hennerich67eedba2012-05-11 11:36:52 +0200468 default:
Jonathan Cameron1d892712011-05-18 14:40:51 +0100469 return 0;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200470 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100471}
472
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100473static ssize_t iio_read_channel_info(struct device *dev,
474 struct device_attribute *attr,
475 char *buf)
476{
477 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
478 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100479 int vals[INDIO_MAX_RAW_ELEMENTS];
480 int ret;
481 int val_len = 2;
482
483 if (indio_dev->info->read_raw_multi)
484 ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
485 INDIO_MAX_RAW_ELEMENTS,
486 vals, &val_len,
487 this_attr->address);
488 else
489 ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
490 &vals[0], &vals[1], this_attr->address);
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100491
492 if (ret < 0)
493 return ret;
494
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100495 return iio_format_value(buf, ret, val_len, vals);
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100496}
497
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000498/**
499 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
500 * @str: The string to parse
501 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
502 * @integer: The integer part of the number
503 * @fract: The fractional part of the number
504 *
505 * Returns 0 on success, or a negative error code if the string could not be
506 * parsed.
507 */
508int iio_str_to_fixpoint(const char *str, int fract_mult,
509 int *integer, int *fract)
510{
511 int i = 0, f = 0;
512 bool integer_part = true, negative = false;
513
514 if (str[0] == '-') {
515 negative = true;
516 str++;
517 } else if (str[0] == '+') {
518 str++;
519 }
520
521 while (*str) {
522 if ('0' <= *str && *str <= '9') {
523 if (integer_part) {
524 i = i * 10 + *str - '0';
525 } else {
526 f += fract_mult * (*str - '0');
527 fract_mult /= 10;
528 }
529 } else if (*str == '\n') {
530 if (*(str + 1) == '\0')
531 break;
532 else
533 return -EINVAL;
534 } else if (*str == '.' && integer_part) {
535 integer_part = false;
536 } else {
537 return -EINVAL;
538 }
539 str++;
540 }
541
542 if (negative) {
543 if (i)
544 i = -i;
545 else
546 f = -f;
547 }
548
549 *integer = i;
550 *fract = f;
551
552 return 0;
553}
554EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
555
Jonathan Cameron1d892712011-05-18 14:40:51 +0100556static ssize_t iio_write_channel_info(struct device *dev,
557 struct device_attribute *attr,
558 const char *buf,
559 size_t len)
560{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200561 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100562 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000563 int ret, fract_mult = 100000;
564 int integer, fract;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100565
566 /* Assumes decimal - precision based on number of digits */
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100567 if (!indio_dev->info->write_raw)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100568 return -EINVAL;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100569
570 if (indio_dev->info->write_raw_get_fmt)
571 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
572 this_attr->c, this_attr->address)) {
573 case IIO_VAL_INT_PLUS_MICRO:
574 fract_mult = 100000;
575 break;
576 case IIO_VAL_INT_PLUS_NANO:
577 fract_mult = 100000000;
578 break;
579 default:
580 return -EINVAL;
581 }
582
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000583 ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
584 if (ret)
585 return ret;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100586
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100587 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
Michael Hennerich5c04af02011-06-27 13:07:10 +0100588 integer, fract, this_attr->address);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100589 if (ret)
590 return ret;
591
592 return len;
593}
594
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100595static
Jonathan Cameron1d892712011-05-18 14:40:51 +0100596int __iio_device_attr_init(struct device_attribute *dev_attr,
597 const char *postfix,
598 struct iio_chan_spec const *chan,
599 ssize_t (*readfunc)(struct device *dev,
600 struct device_attribute *attr,
601 char *buf),
602 ssize_t (*writefunc)(struct device *dev,
603 struct device_attribute *attr,
604 const char *buf,
605 size_t len),
Jonathan Cameron37044322013-09-08 14:57:00 +0100606 enum iio_shared_by shared_by)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100607{
Jonathan Cameron37044322013-09-08 14:57:00 +0100608 int ret = 0;
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000609 char *name = NULL;
Jonathan Cameron37044322013-09-08 14:57:00 +0100610 char *full_postfix;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100611 sysfs_attr_init(&dev_attr->attr);
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100612
613 /* Build up postfix of <extend_name>_<modifier>_postfix */
Jonathan Cameron37044322013-09-08 14:57:00 +0100614 if (chan->modified && (shared_by == IIO_SEPARATE)) {
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100615 if (chan->extend_name)
616 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
617 iio_modifier_names[chan
618 ->channel2],
619 chan->extend_name,
620 postfix);
621 else
622 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
623 iio_modifier_names[chan
624 ->channel2],
625 postfix);
626 } else {
Lars-Peter Clausen77bfa8b2014-02-14 14:19:00 +0000627 if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100628 full_postfix = kstrdup(postfix, GFP_KERNEL);
629 else
630 full_postfix = kasprintf(GFP_KERNEL,
631 "%s_%s",
632 chan->extend_name,
633 postfix);
634 }
Jonathan Cameron37044322013-09-08 14:57:00 +0100635 if (full_postfix == NULL)
636 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100637
Justin P. Mattock4abf6f82012-02-29 22:00:38 -0800638 if (chan->differential) { /* Differential can not have modifier */
Jonathan Cameron37044322013-09-08 14:57:00 +0100639 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100640 case IIO_SHARED_BY_ALL:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000641 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100642 break;
643 case IIO_SHARED_BY_DIR:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000644 name = kasprintf(GFP_KERNEL, "%s_%s",
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100645 iio_direction[chan->output],
646 full_postfix);
647 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100648 case IIO_SHARED_BY_TYPE:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000649 name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100650 iio_direction[chan->output],
651 iio_chan_type_name_spec[chan->type],
652 iio_chan_type_name_spec[chan->type],
653 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100654 break;
655 case IIO_SEPARATE:
656 if (!chan->indexed) {
657 WARN_ON("Differential channels must be indexed\n");
658 ret = -EINVAL;
659 goto error_free_full_postfix;
660 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000661 name = kasprintf(GFP_KERNEL,
Jonathan Cameron37044322013-09-08 14:57:00 +0100662 "%s_%s%d-%s%d_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100663 iio_direction[chan->output],
664 iio_chan_type_name_spec[chan->type],
665 chan->channel,
666 iio_chan_type_name_spec[chan->type],
667 chan->channel2,
668 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100669 break;
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100670 }
671 } else { /* Single ended */
Jonathan Cameron37044322013-09-08 14:57:00 +0100672 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100673 case IIO_SHARED_BY_ALL:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000674 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100675 break;
676 case IIO_SHARED_BY_DIR:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000677 name = kasprintf(GFP_KERNEL, "%s_%s",
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100678 iio_direction[chan->output],
679 full_postfix);
680 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100681 case IIO_SHARED_BY_TYPE:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000682 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100683 iio_direction[chan->output],
684 iio_chan_type_name_spec[chan->type],
685 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100686 break;
687
688 case IIO_SEPARATE:
689 if (chan->indexed)
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000690 name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
Jonathan Cameron37044322013-09-08 14:57:00 +0100691 iio_direction[chan->output],
692 iio_chan_type_name_spec[chan->type],
693 chan->channel,
694 full_postfix);
695 else
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000696 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
Jonathan Cameron37044322013-09-08 14:57:00 +0100697 iio_direction[chan->output],
698 iio_chan_type_name_spec[chan->type],
699 full_postfix);
700 break;
701 }
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100702 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000703 if (name == NULL) {
Jonathan Cameron1d892712011-05-18 14:40:51 +0100704 ret = -ENOMEM;
705 goto error_free_full_postfix;
706 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000707 dev_attr->attr.name = name;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100708
709 if (readfunc) {
710 dev_attr->attr.mode |= S_IRUGO;
711 dev_attr->show = readfunc;
712 }
713
714 if (writefunc) {
715 dev_attr->attr.mode |= S_IWUSR;
716 dev_attr->store = writefunc;
717 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000718
Jonathan Cameron1d892712011-05-18 14:40:51 +0100719error_free_full_postfix:
720 kfree(full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100721
Jonathan Cameron847ec802009-08-18 18:06:19 +0100722 return ret;
723}
724
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100725static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100726{
727 kfree(dev_attr->attr.name);
728}
729
730int __iio_add_chan_devattr(const char *postfix,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100731 struct iio_chan_spec const *chan,
732 ssize_t (*readfunc)(struct device *dev,
733 struct device_attribute *attr,
734 char *buf),
735 ssize_t (*writefunc)(struct device *dev,
736 struct device_attribute *attr,
737 const char *buf,
738 size_t len),
Jonathan Camerone614a542011-09-02 17:14:41 +0100739 u64 mask,
Jonathan Cameron37044322013-09-08 14:57:00 +0100740 enum iio_shared_by shared_by,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100741 struct device *dev,
742 struct list_head *attr_list)
743{
744 int ret;
745 struct iio_dev_attr *iio_attr, *t;
746
Sachin Kamat670c1102013-10-24 12:53:00 +0100747 iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000748 if (iio_attr == NULL)
749 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100750 ret = __iio_device_attr_init(&iio_attr->dev_attr,
751 postfix, chan,
Jonathan Cameron37044322013-09-08 14:57:00 +0100752 readfunc, writefunc, shared_by);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100753 if (ret)
754 goto error_iio_dev_attr_free;
755 iio_attr->c = chan;
756 iio_attr->address = mask;
757 list_for_each_entry(t, attr_list, l)
758 if (strcmp(t->dev_attr.attr.name,
759 iio_attr->dev_attr.attr.name) == 0) {
Jonathan Cameron37044322013-09-08 14:57:00 +0100760 if (shared_by == IIO_SEPARATE)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100761 dev_err(dev, "tried to double register : %s\n",
762 t->dev_attr.attr.name);
763 ret = -EBUSY;
764 goto error_device_attr_deinit;
765 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100766 list_add(&iio_attr->l, attr_list);
767
768 return 0;
769
770error_device_attr_deinit:
771 __iio_device_attr_deinit(&iio_attr->dev_attr);
772error_iio_dev_attr_free:
773 kfree(iio_attr);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100774 return ret;
775}
776
Jonathan Cameron37044322013-09-08 14:57:00 +0100777static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
778 struct iio_chan_spec const *chan,
779 enum iio_shared_by shared_by,
780 const long *infomask)
781{
782 int i, ret, attrcount = 0;
783
784 for_each_set_bit(i, infomask, sizeof(infomask)*8) {
Jonathan Cameronef4b4852014-01-03 22:24:00 +0000785 if (i >= ARRAY_SIZE(iio_chan_info_postfix))
786 return -EINVAL;
Jonathan Cameron37044322013-09-08 14:57:00 +0100787 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
788 chan,
789 &iio_read_channel_info,
790 &iio_write_channel_info,
791 i,
792 shared_by,
793 &indio_dev->dev,
794 &indio_dev->channel_attr_list);
795 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
796 continue;
797 else if (ret < 0)
798 return ret;
799 attrcount++;
800 }
801
802 return attrcount;
803}
804
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100805static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100806 struct iio_chan_spec const *chan)
807{
Jonathan Cameron5ccb3ad2012-04-15 17:41:16 +0100808 int ret, attrcount = 0;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100809 const struct iio_chan_spec_ext_info *ext_info;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100810
Jonathan Cameron1d892712011-05-18 14:40:51 +0100811 if (chan->channel < 0)
812 return 0;
Jonathan Cameron37044322013-09-08 14:57:00 +0100813 ret = iio_device_add_info_mask_type(indio_dev, chan,
814 IIO_SEPARATE,
815 &chan->info_mask_separate);
816 if (ret < 0)
817 return ret;
818 attrcount += ret;
819
820 ret = iio_device_add_info_mask_type(indio_dev, chan,
821 IIO_SHARED_BY_TYPE,
822 &chan->info_mask_shared_by_type);
823 if (ret < 0)
824 return ret;
825 attrcount += ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100826
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100827 ret = iio_device_add_info_mask_type(indio_dev, chan,
828 IIO_SHARED_BY_DIR,
829 &chan->info_mask_shared_by_dir);
830 if (ret < 0)
831 return ret;
832 attrcount += ret;
833
834 ret = iio_device_add_info_mask_type(indio_dev, chan,
835 IIO_SHARED_BY_ALL,
836 &chan->info_mask_shared_by_all);
837 if (ret < 0)
838 return ret;
839 attrcount += ret;
840
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100841 if (chan->ext_info) {
842 unsigned int i = 0;
843 for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
844 ret = __iio_add_chan_devattr(ext_info->name,
845 chan,
846 ext_info->read ?
847 &iio_read_channel_ext_info : NULL,
848 ext_info->write ?
849 &iio_write_channel_ext_info : NULL,
850 i,
851 ext_info->shared,
852 &indio_dev->dev,
853 &indio_dev->channel_attr_list);
854 i++;
855 if (ret == -EBUSY && ext_info->shared)
856 continue;
857
858 if (ret)
Jonathan Cameron37044322013-09-08 14:57:00 +0100859 return ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100860
861 attrcount++;
862 }
863 }
864
Jonathan Cameron37044322013-09-08 14:57:00 +0100865 return attrcount;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100866}
867
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100868/**
869 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
870 * @attr_list: List of IIO device attributes
871 *
872 * This function frees the memory allocated for each of the IIO device
Martin Fuzzeyc1b03ab2015-02-19 15:17:44 +0100873 * attributes in the list.
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100874 */
875void iio_free_chan_devattr_list(struct list_head *attr_list)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100876{
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100877 struct iio_dev_attr *p, *n;
878
879 list_for_each_entry_safe(p, n, attr_list, l) {
880 kfree(p->dev_attr.attr.name);
Martin Fuzzeyc1b03ab2015-02-19 15:17:44 +0100881 list_del(&p->l);
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100882 kfree(p);
883 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100884}
885
Jonathan Cameron1b732882011-05-18 14:41:43 +0100886static ssize_t iio_show_dev_name(struct device *dev,
887 struct device_attribute *attr,
888 char *buf)
889{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200890 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Kees Cook598db582014-03-13 16:46:00 +0000891 return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name);
Jonathan Cameron1b732882011-05-18 14:41:43 +0100892}
893
894static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
895
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100896static int iio_device_register_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100897{
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100898 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100899 struct iio_dev_attr *p;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100900 struct attribute **attr;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100901
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100902 /* First count elements in any existing group */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100903 if (indio_dev->info->attrs) {
904 attr = indio_dev->info->attrs->attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100905 while (*attr++ != NULL)
906 attrcount_orig++;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100907 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100908 attrcount = attrcount_orig;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100909 /*
910 * New channel registration method - relies on the fact a group does
Peter Meerwaldd25b3802012-08-26 13:43:00 +0100911 * not need to be initialized if its name is NULL.
Jonathan Cameron1d892712011-05-18 14:40:51 +0100912 */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100913 if (indio_dev->channels)
914 for (i = 0; i < indio_dev->num_channels; i++) {
915 ret = iio_device_add_channel_sysfs(indio_dev,
916 &indio_dev
Jonathan Cameron1d892712011-05-18 14:40:51 +0100917 ->channels[i]);
918 if (ret < 0)
919 goto error_clear_attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100920 attrcount += ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100921 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100922
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100923 if (indio_dev->name)
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100924 attrcount++;
925
Thomas Meyerd83fb182011-11-29 22:08:00 +0100926 indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
927 sizeof(indio_dev->chan_attr_group.attrs[0]),
928 GFP_KERNEL);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100929 if (indio_dev->chan_attr_group.attrs == NULL) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100930 ret = -ENOMEM;
931 goto error_clear_attrs;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100932 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100933 /* Copy across original attributes */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100934 if (indio_dev->info->attrs)
935 memcpy(indio_dev->chan_attr_group.attrs,
936 indio_dev->info->attrs->attrs,
937 sizeof(indio_dev->chan_attr_group.attrs[0])
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100938 *attrcount_orig);
939 attrn = attrcount_orig;
940 /* Add all elements from the list. */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100941 list_for_each_entry(p, &indio_dev->channel_attr_list, l)
942 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
943 if (indio_dev->name)
944 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100945
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100946 indio_dev->groups[indio_dev->groupcounter++] =
947 &indio_dev->chan_attr_group;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100948
Jonathan Cameron1d892712011-05-18 14:40:51 +0100949 return 0;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100950
Jonathan Cameron1d892712011-05-18 14:40:51 +0100951error_clear_attrs:
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100952 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100953
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100954 return ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100955}
956
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100957static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100958{
Jonathan Cameron1d892712011-05-18 14:40:51 +0100959
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100960 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100961 kfree(indio_dev->chan_attr_group.attrs);
Martin Fuzzeyc1b03ab2015-02-19 15:17:44 +0100962 indio_dev->chan_attr_group.attrs = NULL;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100963}
964
Jonathan Cameron847ec802009-08-18 18:06:19 +0100965static void iio_dev_release(struct device *device)
966{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200967 struct iio_dev *indio_dev = dev_to_iio_dev(device);
Vladimir Barinov735ad072015-08-20 22:37:39 +0300968 if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED))
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100969 iio_device_unregister_trigger_consumer(indio_dev);
970 iio_device_unregister_eventset(indio_dev);
971 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200972
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100973 iio_buffer_put(indio_dev->buffer);
974
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200975 ida_simple_remove(&iio_ida, indio_dev->id);
976 kfree(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100977}
978
Guenter Roeck17d82b42013-02-07 17:09:00 +0000979struct device_type iio_device_type = {
Jonathan Cameron847ec802009-08-18 18:06:19 +0100980 .name = "iio_device",
981 .release = iio_dev_release,
982};
983
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000984/**
985 * iio_device_alloc() - allocate an iio_dev from a driver
986 * @sizeof_priv: Space to allocate for private structure.
987 **/
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200988struct iio_dev *iio_device_alloc(int sizeof_priv)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100989{
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +0100990 struct iio_dev *dev;
991 size_t alloc_size;
992
993 alloc_size = sizeof(struct iio_dev);
994 if (sizeof_priv) {
995 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
996 alloc_size += sizeof_priv;
997 }
998 /* ensure 32-byte alignment of whole construct ? */
999 alloc_size += IIO_ALIGN - 1;
1000
1001 dev = kzalloc(alloc_size, GFP_KERNEL);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001002
1003 if (dev) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001004 dev->dev.groups = dev->groups;
Guenter Roeck17d82b42013-02-07 17:09:00 +00001005 dev->dev.type = &iio_device_type;
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +01001006 dev->dev.bus = &iio_bus_type;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001007 device_initialize(&dev->dev);
1008 dev_set_drvdata(&dev->dev, (void *)dev);
1009 mutex_init(&dev->mlock);
Jonathan Cameronac917a82012-02-15 19:48:00 +00001010 mutex_init(&dev->info_exist_lock);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +02001011 INIT_LIST_HEAD(&dev->channel_attr_list);
Jonathan Camerona9e39f92011-09-14 13:01:25 +01001012
1013 dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
1014 if (dev->id < 0) {
1015 /* cannot use a dev_err as the name isn't available */
Sachin Kamat3176dd52013-10-24 12:53:00 +01001016 pr_err("failed to get device id\n");
Jonathan Camerona9e39f92011-09-14 13:01:25 +01001017 kfree(dev);
1018 return NULL;
1019 }
1020 dev_set_name(&dev->dev, "iio:device%d", dev->id);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001021 INIT_LIST_HEAD(&dev->buffer_list);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001022 }
1023
1024 return dev;
1025}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001026EXPORT_SYMBOL(iio_device_alloc);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001027
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001028/**
1029 * iio_device_free() - free an iio_dev from a driver
1030 * @dev: the iio_dev associated with the device
1031 **/
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001032void iio_device_free(struct iio_dev *dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001033{
Lars-Peter Clausene407fd62012-06-04 10:41:42 +02001034 if (dev)
1035 put_device(&dev->dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001036}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001037EXPORT_SYMBOL(iio_device_free);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001038
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001039static void devm_iio_device_release(struct device *dev, void *res)
1040{
1041 iio_device_free(*(struct iio_dev **)res);
1042}
1043
1044static int devm_iio_device_match(struct device *dev, void *res, void *data)
1045{
1046 struct iio_dev **r = res;
1047 if (!r || !*r) {
1048 WARN_ON(!r || !*r);
1049 return 0;
1050 }
1051 return *r == data;
1052}
1053
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001054/**
1055 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1056 * @dev: Device to allocate iio_dev for
1057 * @sizeof_priv: Space to allocate for private structure.
1058 *
1059 * Managed iio_device_alloc. iio_dev allocated with this function is
1060 * automatically freed on driver detach.
1061 *
1062 * If an iio_dev allocated with this function needs to be freed separately,
1063 * devm_iio_device_free() must be used.
1064 *
1065 * RETURNS:
1066 * Pointer to allocated iio_dev on success, NULL on failure.
1067 */
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001068struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
1069{
1070 struct iio_dev **ptr, *iio_dev;
1071
1072 ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
1073 GFP_KERNEL);
1074 if (!ptr)
1075 return NULL;
1076
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001077 iio_dev = iio_device_alloc(sizeof_priv);
1078 if (iio_dev) {
1079 *ptr = iio_dev;
1080 devres_add(dev, ptr);
1081 } else {
1082 devres_free(ptr);
1083 }
1084
1085 return iio_dev;
1086}
1087EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
1088
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001089/**
1090 * devm_iio_device_free - Resource-managed iio_device_free()
1091 * @dev: Device this iio_dev belongs to
1092 * @iio_dev: the iio_dev associated with the device
1093 *
1094 * Free iio_dev allocated with devm_iio_device_alloc().
1095 */
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001096void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
1097{
1098 int rc;
1099
1100 rc = devres_release(dev, devm_iio_device_release,
1101 devm_iio_device_match, iio_dev);
1102 WARN_ON(rc);
1103}
1104EXPORT_SYMBOL_GPL(devm_iio_device_free);
1105
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001106/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001107 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
Cristina Opriceana2498dcf2015-07-24 16:16:19 +03001108 * @inode: Inode structure for identifying the device in the file system
1109 * @filp: File structure for iio device used to keep and later access
1110 * private data
1111 *
1112 * Return: 0 on success or -EBUSY if the device is already opened
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001113 **/
1114static int iio_chrdev_open(struct inode *inode, struct file *filp)
1115{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001116 struct iio_dev *indio_dev = container_of(inode->i_cdev,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001117 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001118
1119 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
1120 return -EBUSY;
1121
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001122 iio_device_get(indio_dev);
1123
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001124 filp->private_data = indio_dev;
Jonathan Cameron30eb82f2011-09-21 11:16:02 +01001125
Lars-Peter Clausen79335142011-12-19 15:23:49 +01001126 return 0;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001127}
1128
1129/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001130 * iio_chrdev_release() - chrdev file close buffer access and ioctls
Cristina Opriceana2498dcf2015-07-24 16:16:19 +03001131 * @inode: Inode structure pointer for the char device
1132 * @filp: File structure pointer for the char device
1133 *
1134 * Return: 0 for successful release
1135 */
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001136static int iio_chrdev_release(struct inode *inode, struct file *filp)
1137{
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001138 struct iio_dev *indio_dev = container_of(inode->i_cdev,
1139 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001140 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001141 iio_device_put(indio_dev);
1142
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001143 return 0;
1144}
1145
1146/* Somewhat of a cross file organization violation - ioctls here are actually
1147 * event related */
1148static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1149{
1150 struct iio_dev *indio_dev = filp->private_data;
1151 int __user *ip = (int __user *)arg;
1152 int fd;
1153
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +01001154 if (!indio_dev->info)
1155 return -ENODEV;
1156
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001157 if (cmd == IIO_GET_EVENT_FD_IOCTL) {
1158 fd = iio_event_getfd(indio_dev);
Linus Walleij3f9059b2015-08-11 11:56:40 +02001159 if (fd < 0)
1160 return fd;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001161 if (copy_to_user(ip, &fd, sizeof(fd)))
1162 return -EFAULT;
1163 return 0;
1164 }
1165 return -EINVAL;
1166}
1167
Jonathan Cameron14555b12011-09-21 11:15:57 +01001168static const struct file_operations iio_buffer_fileops = {
1169 .read = iio_buffer_read_first_n_outer_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001170 .release = iio_chrdev_release,
1171 .open = iio_chrdev_open,
Jonathan Cameron14555b12011-09-21 11:15:57 +01001172 .poll = iio_buffer_poll_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001173 .owner = THIS_MODULE,
1174 .llseek = noop_llseek,
1175 .unlocked_ioctl = iio_ioctl,
1176 .compat_ioctl = iio_ioctl,
1177};
1178
Vlad Dogaru8f5d8722014-12-29 11:37:48 +02001179static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
1180{
1181 int i, j;
1182 const struct iio_chan_spec *channels = indio_dev->channels;
1183
1184 if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
1185 return 0;
1186
1187 for (i = 0; i < indio_dev->num_channels - 1; i++) {
1188 if (channels[i].scan_index < 0)
1189 continue;
1190 for (j = i + 1; j < indio_dev->num_channels; j++)
1191 if (channels[i].scan_index == channels[j].scan_index) {
1192 dev_err(&indio_dev->dev,
1193 "Duplicate scan index %d\n",
1194 channels[i].scan_index);
1195 return -EINVAL;
1196 }
1197 }
1198
1199 return 0;
1200}
1201
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001202static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1203
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001204/**
1205 * iio_device_register() - register a device with the IIO subsystem
1206 * @indio_dev: Device structure filled by the device driver
1207 **/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001208int iio_device_register(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001209{
1210 int ret;
1211
Guenter Roeck17d82b42013-02-07 17:09:00 +00001212 /* If the calling driver did not initialize of_node, do it here */
1213 if (!indio_dev->dev.of_node && indio_dev->dev.parent)
1214 indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
1215
Vlad Dogaru8f5d8722014-12-29 11:37:48 +02001216 ret = iio_check_unique_scan_index(indio_dev);
1217 if (ret < 0)
1218 return ret;
1219
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001220 /* configure elements for the chrdev */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001221 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001222
Michael Henneriche553f182012-03-01 10:51:03 +01001223 ret = iio_device_register_debugfs(indio_dev);
1224 if (ret) {
1225 dev_err(indio_dev->dev.parent,
1226 "Failed to register debugfs interfaces\n");
Hartmut Knaack92825ff2014-02-16 11:53:00 +00001227 return ret;
Michael Henneriche553f182012-03-01 10:51:03 +01001228 }
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001229
1230 ret = iio_buffer_alloc_sysfs_and_mask(indio_dev);
1231 if (ret) {
1232 dev_err(indio_dev->dev.parent,
1233 "Failed to create buffer sysfs interfaces\n");
1234 goto error_unreg_debugfs;
1235 }
1236
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001237 ret = iio_device_register_sysfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001238 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001239 dev_err(indio_dev->dev.parent,
Jonathan Cameron847ec802009-08-18 18:06:19 +01001240 "Failed to register sysfs interfaces\n");
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001241 goto error_buffer_free_sysfs;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001242 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001243 ret = iio_device_register_eventset(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001244 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001245 dev_err(indio_dev->dev.parent,
Roel Van Nyenc849d252010-04-29 19:27:31 +02001246 "Failed to register event set\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001247 goto error_free_sysfs;
1248 }
Vladimir Barinov735ad072015-08-20 22:37:39 +03001249 if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED))
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001250 iio_device_register_trigger_consumer(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001251
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001252 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1253 indio_dev->setup_ops == NULL)
1254 indio_dev->setup_ops = &noop_ring_setup_ops;
1255
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001256 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
1257 indio_dev->chrdev.owner = indio_dev->info->driver_module;
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001258 indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001259 ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001260 if (ret < 0)
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001261 goto error_unreg_eventset;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001262
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001263 ret = device_add(&indio_dev->dev);
1264 if (ret < 0)
1265 goto error_cdev_del;
1266
1267 return 0;
1268error_cdev_del:
1269 cdev_del(&indio_dev->chrdev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001270error_unreg_eventset:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001271 iio_device_unregister_eventset(indio_dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001272error_free_sysfs:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001273 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001274error_buffer_free_sysfs:
1275 iio_buffer_free_sysfs_and_mask(indio_dev);
Michael Henneriche553f182012-03-01 10:51:03 +01001276error_unreg_debugfs:
1277 iio_device_unregister_debugfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001278 return ret;
1279}
1280EXPORT_SYMBOL(iio_device_register);
1281
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001282/**
1283 * iio_device_unregister() - unregister a device from the IIO subsystem
1284 * @indio_dev: Device structure representing the device.
1285 **/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001286void iio_device_unregister(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001287{
Jonathan Cameronac917a82012-02-15 19:48:00 +00001288 mutex_lock(&indio_dev->info_exist_lock);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001289
1290 device_del(&indio_dev->dev);
1291
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001292 if (indio_dev->chrdev.dev)
1293 cdev_del(&indio_dev->chrdev);
Lars-Peter Clausenbc4c9612013-09-21 16:21:00 +01001294 iio_device_unregister_debugfs(indio_dev);
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001295
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001296 iio_disable_all_buffers(indio_dev);
1297
Jonathan Cameronac917a82012-02-15 19:48:00 +00001298 indio_dev->info = NULL;
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +01001299
1300 iio_device_wakeup_eventset(indio_dev);
1301 iio_buffer_wakeup_poll(indio_dev);
1302
Jonathan Cameronac917a82012-02-15 19:48:00 +00001303 mutex_unlock(&indio_dev->info_exist_lock);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001304
1305 iio_buffer_free_sysfs_and_mask(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001306}
1307EXPORT_SYMBOL(iio_device_unregister);
Sachin Kamat8caa07c2013-10-29 11:39:00 +00001308
1309static void devm_iio_device_unreg(struct device *dev, void *res)
1310{
1311 iio_device_unregister(*(struct iio_dev **)res);
1312}
1313
1314/**
1315 * devm_iio_device_register - Resource-managed iio_device_register()
1316 * @dev: Device to allocate iio_dev for
1317 * @indio_dev: Device structure filled by the device driver
1318 *
1319 * Managed iio_device_register. The IIO device registered with this
1320 * function is automatically unregistered on driver detach. This function
1321 * calls iio_device_register() internally. Refer to that function for more
1322 * information.
1323 *
1324 * If an iio_dev registered with this function needs to be unregistered
1325 * separately, devm_iio_device_unregister() must be used.
1326 *
1327 * RETURNS:
1328 * 0 on success, negative error number on failure.
1329 */
1330int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev)
1331{
1332 struct iio_dev **ptr;
1333 int ret;
1334
1335 ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL);
1336 if (!ptr)
1337 return -ENOMEM;
1338
1339 *ptr = indio_dev;
1340 ret = iio_device_register(indio_dev);
1341 if (!ret)
1342 devres_add(dev, ptr);
1343 else
1344 devres_free(ptr);
1345
1346 return ret;
1347}
1348EXPORT_SYMBOL_GPL(devm_iio_device_register);
1349
1350/**
1351 * devm_iio_device_unregister - Resource-managed iio_device_unregister()
1352 * @dev: Device this iio_dev belongs to
1353 * @indio_dev: the iio_dev associated with the device
1354 *
1355 * Unregister iio_dev registered with devm_iio_device_register().
1356 */
1357void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev)
1358{
1359 int rc;
1360
1361 rc = devres_release(dev, devm_iio_device_unreg,
1362 devm_iio_device_match, indio_dev);
1363 WARN_ON(rc);
1364}
1365EXPORT_SYMBOL_GPL(devm_iio_device_unregister);
1366
Jonathan Cameron847ec802009-08-18 18:06:19 +01001367subsys_initcall(iio_init);
1368module_exit(iio_exit);
1369
Jonathan Cameronc8b95952012-09-02 21:27:56 +01001370MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001371MODULE_DESCRIPTION("Industrial I/O core");
1372MODULE_LICENSE("GPL");