blob: 655755b49ccdd7dc701b8c511f0e778f6b333648 [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",
Jonathan Cameron1d892712011-05-18 14:40:51 +010077};
78
Jonathan Cameron330c6c52011-09-02 17:14:39 +010079static const char * const iio_modifier_names[] = {
Jonathan Cameron1d892712011-05-18 14:40:51 +010080 [IIO_MOD_X] = "x",
81 [IIO_MOD_Y] = "y",
82 [IIO_MOD_Z] = "z",
Jonathan Cameron8f5879b2012-05-05 10:39:22 +010083 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
Jonathan Cameroncf82cb82012-05-05 10:56:41 +010084 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
Jonathan Cameron330c6c52011-09-02 17:14:39 +010085 [IIO_MOD_LIGHT_BOTH] = "both",
86 [IIO_MOD_LIGHT_IR] = "ir",
Jon Brenner21cd1fa2012-05-16 10:46:42 -050087 [IIO_MOD_LIGHT_CLEAR] = "clear",
88 [IIO_MOD_LIGHT_RED] = "red",
89 [IIO_MOD_LIGHT_GREEN] = "green",
90 [IIO_MOD_LIGHT_BLUE] = "blue",
Srinivas Pandruvada5082f402014-04-29 00:51:00 +010091 [IIO_MOD_QUATERNION] = "quaternion",
Peter Meerwald638b43b2014-02-05 16:57:00 +000092 [IIO_MOD_TEMP_AMBIENT] = "ambient",
93 [IIO_MOD_TEMP_OBJECT] = "object",
Reyad Attiyat11b8dda2014-07-17 19:18:00 +010094 [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
95 [IIO_MOD_NORTH_TRUE] = "from_north_true",
96 [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
97 [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
Daniel Baluta55aebeb2014-11-10 14:45:30 +020098 [IIO_MOD_RUNNING] = "running",
99 [IIO_MOD_JOGGING] = "jogging",
100 [IIO_MOD_WALKING] = "walking",
101 [IIO_MOD_STILL] = "still",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100102};
103
104/* relies on pairs of these shared then separate */
105static const char * const iio_chan_info_postfix[] = {
Jonathan Cameron75a973c2012-04-15 17:41:30 +0100106 [IIO_CHAN_INFO_RAW] = "raw",
107 [IIO_CHAN_INFO_PROCESSED] = "input",
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100108 [IIO_CHAN_INFO_SCALE] = "scale",
109 [IIO_CHAN_INFO_OFFSET] = "offset",
110 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
111 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
112 [IIO_CHAN_INFO_PEAK] = "peak_raw",
113 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
114 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
115 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
Jonathan Camerondf94aba2011-11-27 11:39:12 +0000116 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
117 = "filter_low_pass_3db_frequency",
Laxman Dewangance85a1c2012-04-13 16:03:31 +0530118 [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
Michael Hennericha6b12852012-04-27 10:58:34 +0200119 [IIO_CHAN_INFO_FREQUENCY] = "frequency",
120 [IIO_CHAN_INFO_PHASE] = "phase",
Michael Hennerichb65d6212012-05-11 11:36:53 +0200121 [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
srinivas pandruvada7c9ab032012-09-05 13:56:00 +0100122 [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
Peter Meerwald899d90b2013-09-08 16:20:00 +0100123 [IIO_CHAN_INFO_INT_TIME] = "integration_time",
Irina Tirdeaa88bfe72014-11-10 14:45:32 +0200124 [IIO_CHAN_INFO_ENABLE] = "en",
Irina Tirdeabcdf28f2014-11-10 14:45:33 +0200125 [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100126};
127
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000128/**
129 * iio_find_channel_from_si() - get channel from its scan index
130 * @indio_dev: device
131 * @si: scan index to match
132 */
Jonathan Cameron5fb21c82011-12-05 21:37:10 +0000133const struct iio_chan_spec
134*iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
135{
136 int i;
137
138 for (i = 0; i < indio_dev->num_channels; i++)
139 if (indio_dev->channels[i].scan_index == si)
140 return &indio_dev->channels[i];
141 return NULL;
142}
143
Jonathan Cameron847ec802009-08-18 18:06:19 +0100144/* This turns up an awful lot */
145ssize_t iio_read_const_attr(struct device *dev,
146 struct device_attribute *attr,
147 char *buf)
148{
149 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
150}
151EXPORT_SYMBOL(iio_read_const_attr);
152
Jonathan Cameron847ec802009-08-18 18:06:19 +0100153static int __init iio_init(void)
154{
155 int ret;
156
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100157 /* Register sysfs bus */
158 ret = bus_register(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100159 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100160 pr_err("could not register bus type\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +0100161 goto error_nothing;
162 }
163
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100164 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
165 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100166 pr_err("failed to allocate char dev region\n");
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100167 goto error_unregister_bus_type;
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100168 }
Jonathan Cameron847ec802009-08-18 18:06:19 +0100169
Michael Henneriche553f182012-03-01 10:51:03 +0100170 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
171
Jonathan Cameron847ec802009-08-18 18:06:19 +0100172 return 0;
173
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100174error_unregister_bus_type:
175 bus_unregister(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100176error_nothing:
177 return ret;
178}
179
180static void __exit iio_exit(void)
181{
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100182 if (iio_devt)
183 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100184 bus_unregister(&iio_bus_type);
Michael Henneriche553f182012-03-01 10:51:03 +0100185 debugfs_remove(iio_debugfs_dentry);
186}
187
188#if defined(CONFIG_DEBUG_FS)
Michael Henneriche553f182012-03-01 10:51:03 +0100189static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
190 size_t count, loff_t *ppos)
191{
192 struct iio_dev *indio_dev = file->private_data;
193 char buf[20];
194 unsigned val = 0;
195 ssize_t len;
196 int ret;
197
198 ret = indio_dev->info->debugfs_reg_access(indio_dev,
199 indio_dev->cached_reg_addr,
200 0, &val);
201 if (ret)
202 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
203
204 len = snprintf(buf, sizeof(buf), "0x%X\n", val);
205
206 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
207}
208
209static ssize_t iio_debugfs_write_reg(struct file *file,
210 const char __user *userbuf, size_t count, loff_t *ppos)
211{
212 struct iio_dev *indio_dev = file->private_data;
213 unsigned reg, val;
214 char buf[80];
215 int ret;
216
217 count = min_t(size_t, count, (sizeof(buf)-1));
218 if (copy_from_user(buf, userbuf, count))
219 return -EFAULT;
220
221 buf[count] = 0;
222
223 ret = sscanf(buf, "%i %i", &reg, &val);
224
225 switch (ret) {
226 case 1:
227 indio_dev->cached_reg_addr = reg;
228 break;
229 case 2:
230 indio_dev->cached_reg_addr = reg;
231 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
232 val, NULL);
233 if (ret) {
234 dev_err(indio_dev->dev.parent, "%s: write failed\n",
235 __func__);
236 return ret;
237 }
238 break;
239 default:
240 return -EINVAL;
241 }
242
243 return count;
244}
245
246static const struct file_operations iio_debugfs_reg_fops = {
Axel Lin5a28c872012-05-03 09:50:51 +0800247 .open = simple_open,
Michael Henneriche553f182012-03-01 10:51:03 +0100248 .read = iio_debugfs_read_reg,
249 .write = iio_debugfs_write_reg,
250};
251
252static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
253{
254 debugfs_remove_recursive(indio_dev->debugfs_dentry);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100255}
256
Michael Henneriche553f182012-03-01 10:51:03 +0100257static int iio_device_register_debugfs(struct iio_dev *indio_dev)
258{
259 struct dentry *d;
260
261 if (indio_dev->info->debugfs_reg_access == NULL)
262 return 0;
263
Axel Linabd5a2f2012-05-03 22:56:58 +0800264 if (!iio_debugfs_dentry)
Michael Henneriche553f182012-03-01 10:51:03 +0100265 return 0;
266
267 indio_dev->debugfs_dentry =
268 debugfs_create_dir(dev_name(&indio_dev->dev),
269 iio_debugfs_dentry);
Michael Henneriche553f182012-03-01 10:51:03 +0100270 if (indio_dev->debugfs_dentry == NULL) {
271 dev_warn(indio_dev->dev.parent,
272 "Failed to create debugfs directory\n");
273 return -EFAULT;
274 }
275
276 d = debugfs_create_file("direct_reg_access", 0644,
277 indio_dev->debugfs_dentry,
278 indio_dev, &iio_debugfs_reg_fops);
279 if (!d) {
280 iio_device_unregister_debugfs(indio_dev);
281 return -ENOMEM;
282 }
283
284 return 0;
285}
286#else
287static int iio_device_register_debugfs(struct iio_dev *indio_dev)
288{
289 return 0;
290}
291
292static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
293{
294}
295#endif /* CONFIG_DEBUG_FS */
296
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100297static ssize_t iio_read_channel_ext_info(struct device *dev,
298 struct device_attribute *attr,
299 char *buf)
300{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200301 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100302 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
303 const struct iio_chan_spec_ext_info *ext_info;
304
305 ext_info = &this_attr->c->ext_info[this_attr->address];
306
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200307 return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100308}
309
310static ssize_t iio_write_channel_ext_info(struct device *dev,
311 struct device_attribute *attr,
312 const char *buf,
313 size_t len)
314{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200315 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100316 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
317 const struct iio_chan_spec_ext_info *ext_info;
318
319 ext_info = &this_attr->c->ext_info[this_attr->address];
320
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200321 return ext_info->write(indio_dev, ext_info->private,
322 this_attr->c, buf, len);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100323}
324
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200325ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
326 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
327{
328 const struct iio_enum *e = (const struct iio_enum *)priv;
329 unsigned int i;
330 size_t len = 0;
331
332 if (!e->num_items)
333 return 0;
334
335 for (i = 0; i < e->num_items; ++i)
Lars-Peter Clausen74dcd432012-06-05 18:24:12 +0200336 len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200337
338 /* replace last space with a newline */
339 buf[len - 1] = '\n';
340
341 return len;
342}
343EXPORT_SYMBOL_GPL(iio_enum_available_read);
344
345ssize_t iio_enum_read(struct iio_dev *indio_dev,
346 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
347{
348 const struct iio_enum *e = (const struct iio_enum *)priv;
349 int i;
350
351 if (!e->get)
352 return -EINVAL;
353
354 i = e->get(indio_dev, chan);
355 if (i < 0)
356 return i;
357 else if (i >= e->num_items)
358 return -EINVAL;
359
Kees Cook598db582014-03-13 16:46:00 +0000360 return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200361}
362EXPORT_SYMBOL_GPL(iio_enum_read);
363
364ssize_t iio_enum_write(struct iio_dev *indio_dev,
365 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
366 size_t len)
367{
368 const struct iio_enum *e = (const struct iio_enum *)priv;
369 unsigned int i;
370 int ret;
371
372 if (!e->set)
373 return -EINVAL;
374
375 for (i = 0; i < e->num_items; i++) {
376 if (sysfs_streq(buf, e->items[i]))
377 break;
378 }
379
380 if (i == e->num_items)
381 return -EINVAL;
382
383 ret = e->set(indio_dev, chan, i);
384 return ret ? ret : len;
385}
386EXPORT_SYMBOL_GPL(iio_enum_write);
387
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100388/**
389 * iio_format_value() - Formats a IIO value into its string representation
390 * @buf: The buffer to which the formated value gets written
391 * @type: One of the IIO_VAL_... constants. This decides how the val and val2
392 * parameters are formatted.
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100393 * @vals: pointer to the values, exact meaning depends on the type parameter.
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100394 */
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100395ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100396{
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100397 unsigned long long tmp;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200398 bool scale_db = false;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100399
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100400 switch (type) {
Michael Hennerich67eedba2012-05-11 11:36:52 +0200401 case IIO_VAL_INT:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100402 return sprintf(buf, "%d\n", vals[0]);
Michael Hennerich67eedba2012-05-11 11:36:52 +0200403 case IIO_VAL_INT_PLUS_MICRO_DB:
404 scale_db = true;
405 case IIO_VAL_INT_PLUS_MICRO:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100406 if (vals[1] < 0)
407 return sprintf(buf, "-%ld.%06u%s\n", abs(vals[0]),
408 -vals[1],
Michael Hennerich67eedba2012-05-11 11:36:52 +0200409 scale_db ? " dB" : "");
Jonathan Cameron1d892712011-05-18 14:40:51 +0100410 else
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100411 return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1],
Michael Hennerich67eedba2012-05-11 11:36:52 +0200412 scale_db ? " dB" : "");
413 case IIO_VAL_INT_PLUS_NANO:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100414 if (vals[1] < 0)
415 return sprintf(buf, "-%ld.%09u\n", abs(vals[0]),
416 -vals[1]);
Michael Hennerich71646e22011-06-27 13:07:07 +0100417 else
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100418 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100419 case IIO_VAL_FRACTIONAL:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100420 tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
421 vals[1] = do_div(tmp, 1000000000LL);
422 vals[0] = tmp;
423 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
Lars-Peter Clausen103d9fb2012-10-16 17:29:00 +0100424 case IIO_VAL_FRACTIONAL_LOG2:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100425 tmp = (s64)vals[0] * 1000000000LL >> vals[1];
426 vals[1] = do_div(tmp, 1000000000LL);
427 vals[0] = tmp;
428 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
429 case IIO_VAL_INT_MULTIPLE:
430 {
431 int i;
432 int len = 0;
433
434 for (i = 0; i < size; ++i)
435 len += snprintf(&buf[len], PAGE_SIZE - len, "%d ",
436 vals[i]);
437 len += snprintf(&buf[len], PAGE_SIZE - len, "\n");
438 return len;
439 }
Michael Hennerich67eedba2012-05-11 11:36:52 +0200440 default:
Jonathan Cameron1d892712011-05-18 14:40:51 +0100441 return 0;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200442 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100443}
444
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100445static ssize_t iio_read_channel_info(struct device *dev,
446 struct device_attribute *attr,
447 char *buf)
448{
449 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
450 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100451 int vals[INDIO_MAX_RAW_ELEMENTS];
452 int ret;
453 int val_len = 2;
454
455 if (indio_dev->info->read_raw_multi)
456 ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
457 INDIO_MAX_RAW_ELEMENTS,
458 vals, &val_len,
459 this_attr->address);
460 else
461 ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
462 &vals[0], &vals[1], this_attr->address);
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100463
464 if (ret < 0)
465 return ret;
466
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100467 return iio_format_value(buf, ret, val_len, vals);
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100468}
469
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000470/**
471 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
472 * @str: The string to parse
473 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
474 * @integer: The integer part of the number
475 * @fract: The fractional part of the number
476 *
477 * Returns 0 on success, or a negative error code if the string could not be
478 * parsed.
479 */
480int iio_str_to_fixpoint(const char *str, int fract_mult,
481 int *integer, int *fract)
482{
483 int i = 0, f = 0;
484 bool integer_part = true, negative = false;
485
486 if (str[0] == '-') {
487 negative = true;
488 str++;
489 } else if (str[0] == '+') {
490 str++;
491 }
492
493 while (*str) {
494 if ('0' <= *str && *str <= '9') {
495 if (integer_part) {
496 i = i * 10 + *str - '0';
497 } else {
498 f += fract_mult * (*str - '0');
499 fract_mult /= 10;
500 }
501 } else if (*str == '\n') {
502 if (*(str + 1) == '\0')
503 break;
504 else
505 return -EINVAL;
506 } else if (*str == '.' && integer_part) {
507 integer_part = false;
508 } else {
509 return -EINVAL;
510 }
511 str++;
512 }
513
514 if (negative) {
515 if (i)
516 i = -i;
517 else
518 f = -f;
519 }
520
521 *integer = i;
522 *fract = f;
523
524 return 0;
525}
526EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
527
Jonathan Cameron1d892712011-05-18 14:40:51 +0100528static ssize_t iio_write_channel_info(struct device *dev,
529 struct device_attribute *attr,
530 const char *buf,
531 size_t len)
532{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200533 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100534 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000535 int ret, fract_mult = 100000;
536 int integer, fract;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100537
538 /* Assumes decimal - precision based on number of digits */
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100539 if (!indio_dev->info->write_raw)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100540 return -EINVAL;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100541
542 if (indio_dev->info->write_raw_get_fmt)
543 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
544 this_attr->c, this_attr->address)) {
545 case IIO_VAL_INT_PLUS_MICRO:
546 fract_mult = 100000;
547 break;
548 case IIO_VAL_INT_PLUS_NANO:
549 fract_mult = 100000000;
550 break;
551 default:
552 return -EINVAL;
553 }
554
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000555 ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
556 if (ret)
557 return ret;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100558
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100559 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
Michael Hennerich5c04af02011-06-27 13:07:10 +0100560 integer, fract, this_attr->address);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100561 if (ret)
562 return ret;
563
564 return len;
565}
566
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100567static
Jonathan Cameron1d892712011-05-18 14:40:51 +0100568int __iio_device_attr_init(struct device_attribute *dev_attr,
569 const char *postfix,
570 struct iio_chan_spec const *chan,
571 ssize_t (*readfunc)(struct device *dev,
572 struct device_attribute *attr,
573 char *buf),
574 ssize_t (*writefunc)(struct device *dev,
575 struct device_attribute *attr,
576 const char *buf,
577 size_t len),
Jonathan Cameron37044322013-09-08 14:57:00 +0100578 enum iio_shared_by shared_by)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100579{
Jonathan Cameron37044322013-09-08 14:57:00 +0100580 int ret = 0;
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000581 char *name = NULL;
Jonathan Cameron37044322013-09-08 14:57:00 +0100582 char *full_postfix;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100583 sysfs_attr_init(&dev_attr->attr);
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100584
585 /* Build up postfix of <extend_name>_<modifier>_postfix */
Jonathan Cameron37044322013-09-08 14:57:00 +0100586 if (chan->modified && (shared_by == IIO_SEPARATE)) {
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100587 if (chan->extend_name)
588 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
589 iio_modifier_names[chan
590 ->channel2],
591 chan->extend_name,
592 postfix);
593 else
594 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
595 iio_modifier_names[chan
596 ->channel2],
597 postfix);
598 } else {
Lars-Peter Clausen77bfa8b2014-02-14 14:19:00 +0000599 if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100600 full_postfix = kstrdup(postfix, GFP_KERNEL);
601 else
602 full_postfix = kasprintf(GFP_KERNEL,
603 "%s_%s",
604 chan->extend_name,
605 postfix);
606 }
Jonathan Cameron37044322013-09-08 14:57:00 +0100607 if (full_postfix == NULL)
608 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100609
Justin P. Mattock4abf6f82012-02-29 22:00:38 -0800610 if (chan->differential) { /* Differential can not have modifier */
Jonathan Cameron37044322013-09-08 14:57:00 +0100611 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100612 case IIO_SHARED_BY_ALL:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000613 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100614 break;
615 case IIO_SHARED_BY_DIR:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000616 name = kasprintf(GFP_KERNEL, "%s_%s",
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100617 iio_direction[chan->output],
618 full_postfix);
619 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100620 case IIO_SHARED_BY_TYPE:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000621 name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100622 iio_direction[chan->output],
623 iio_chan_type_name_spec[chan->type],
624 iio_chan_type_name_spec[chan->type],
625 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100626 break;
627 case IIO_SEPARATE:
628 if (!chan->indexed) {
629 WARN_ON("Differential channels must be indexed\n");
630 ret = -EINVAL;
631 goto error_free_full_postfix;
632 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000633 name = kasprintf(GFP_KERNEL,
Jonathan Cameron37044322013-09-08 14:57:00 +0100634 "%s_%s%d-%s%d_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100635 iio_direction[chan->output],
636 iio_chan_type_name_spec[chan->type],
637 chan->channel,
638 iio_chan_type_name_spec[chan->type],
639 chan->channel2,
640 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100641 break;
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100642 }
643 } else { /* Single ended */
Jonathan Cameron37044322013-09-08 14:57:00 +0100644 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100645 case IIO_SHARED_BY_ALL:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000646 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100647 break;
648 case IIO_SHARED_BY_DIR:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000649 name = kasprintf(GFP_KERNEL, "%s_%s",
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100650 iio_direction[chan->output],
651 full_postfix);
652 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100653 case IIO_SHARED_BY_TYPE:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000654 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100655 iio_direction[chan->output],
656 iio_chan_type_name_spec[chan->type],
657 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100658 break;
659
660 case IIO_SEPARATE:
661 if (chan->indexed)
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000662 name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
Jonathan Cameron37044322013-09-08 14:57:00 +0100663 iio_direction[chan->output],
664 iio_chan_type_name_spec[chan->type],
665 chan->channel,
666 full_postfix);
667 else
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000668 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
Jonathan Cameron37044322013-09-08 14:57:00 +0100669 iio_direction[chan->output],
670 iio_chan_type_name_spec[chan->type],
671 full_postfix);
672 break;
673 }
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100674 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000675 if (name == NULL) {
Jonathan Cameron1d892712011-05-18 14:40:51 +0100676 ret = -ENOMEM;
677 goto error_free_full_postfix;
678 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000679 dev_attr->attr.name = name;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100680
681 if (readfunc) {
682 dev_attr->attr.mode |= S_IRUGO;
683 dev_attr->show = readfunc;
684 }
685
686 if (writefunc) {
687 dev_attr->attr.mode |= S_IWUSR;
688 dev_attr->store = writefunc;
689 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000690
Jonathan Cameron1d892712011-05-18 14:40:51 +0100691error_free_full_postfix:
692 kfree(full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100693
Jonathan Cameron847ec802009-08-18 18:06:19 +0100694 return ret;
695}
696
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100697static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100698{
699 kfree(dev_attr->attr.name);
700}
701
702int __iio_add_chan_devattr(const char *postfix,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100703 struct iio_chan_spec const *chan,
704 ssize_t (*readfunc)(struct device *dev,
705 struct device_attribute *attr,
706 char *buf),
707 ssize_t (*writefunc)(struct device *dev,
708 struct device_attribute *attr,
709 const char *buf,
710 size_t len),
Jonathan Camerone614a542011-09-02 17:14:41 +0100711 u64 mask,
Jonathan Cameron37044322013-09-08 14:57:00 +0100712 enum iio_shared_by shared_by,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100713 struct device *dev,
714 struct list_head *attr_list)
715{
716 int ret;
717 struct iio_dev_attr *iio_attr, *t;
718
Sachin Kamat670c1102013-10-24 12:53:00 +0100719 iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000720 if (iio_attr == NULL)
721 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100722 ret = __iio_device_attr_init(&iio_attr->dev_attr,
723 postfix, chan,
Jonathan Cameron37044322013-09-08 14:57:00 +0100724 readfunc, writefunc, shared_by);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100725 if (ret)
726 goto error_iio_dev_attr_free;
727 iio_attr->c = chan;
728 iio_attr->address = mask;
729 list_for_each_entry(t, attr_list, l)
730 if (strcmp(t->dev_attr.attr.name,
731 iio_attr->dev_attr.attr.name) == 0) {
Jonathan Cameron37044322013-09-08 14:57:00 +0100732 if (shared_by == IIO_SEPARATE)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100733 dev_err(dev, "tried to double register : %s\n",
734 t->dev_attr.attr.name);
735 ret = -EBUSY;
736 goto error_device_attr_deinit;
737 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100738 list_add(&iio_attr->l, attr_list);
739
740 return 0;
741
742error_device_attr_deinit:
743 __iio_device_attr_deinit(&iio_attr->dev_attr);
744error_iio_dev_attr_free:
745 kfree(iio_attr);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100746 return ret;
747}
748
Jonathan Cameron37044322013-09-08 14:57:00 +0100749static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
750 struct iio_chan_spec const *chan,
751 enum iio_shared_by shared_by,
752 const long *infomask)
753{
754 int i, ret, attrcount = 0;
755
756 for_each_set_bit(i, infomask, sizeof(infomask)*8) {
Jonathan Cameronef4b4852014-01-03 22:24:00 +0000757 if (i >= ARRAY_SIZE(iio_chan_info_postfix))
758 return -EINVAL;
Jonathan Cameron37044322013-09-08 14:57:00 +0100759 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
760 chan,
761 &iio_read_channel_info,
762 &iio_write_channel_info,
763 i,
764 shared_by,
765 &indio_dev->dev,
766 &indio_dev->channel_attr_list);
767 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
768 continue;
769 else if (ret < 0)
770 return ret;
771 attrcount++;
772 }
773
774 return attrcount;
775}
776
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100777static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100778 struct iio_chan_spec const *chan)
779{
Jonathan Cameron5ccb3ad2012-04-15 17:41:16 +0100780 int ret, attrcount = 0;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100781 const struct iio_chan_spec_ext_info *ext_info;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100782
Jonathan Cameron1d892712011-05-18 14:40:51 +0100783 if (chan->channel < 0)
784 return 0;
Jonathan Cameron37044322013-09-08 14:57:00 +0100785 ret = iio_device_add_info_mask_type(indio_dev, chan,
786 IIO_SEPARATE,
787 &chan->info_mask_separate);
788 if (ret < 0)
789 return ret;
790 attrcount += ret;
791
792 ret = iio_device_add_info_mask_type(indio_dev, chan,
793 IIO_SHARED_BY_TYPE,
794 &chan->info_mask_shared_by_type);
795 if (ret < 0)
796 return ret;
797 attrcount += ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100798
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100799 ret = iio_device_add_info_mask_type(indio_dev, chan,
800 IIO_SHARED_BY_DIR,
801 &chan->info_mask_shared_by_dir);
802 if (ret < 0)
803 return ret;
804 attrcount += ret;
805
806 ret = iio_device_add_info_mask_type(indio_dev, chan,
807 IIO_SHARED_BY_ALL,
808 &chan->info_mask_shared_by_all);
809 if (ret < 0)
810 return ret;
811 attrcount += ret;
812
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100813 if (chan->ext_info) {
814 unsigned int i = 0;
815 for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
816 ret = __iio_add_chan_devattr(ext_info->name,
817 chan,
818 ext_info->read ?
819 &iio_read_channel_ext_info : NULL,
820 ext_info->write ?
821 &iio_write_channel_ext_info : NULL,
822 i,
823 ext_info->shared,
824 &indio_dev->dev,
825 &indio_dev->channel_attr_list);
826 i++;
827 if (ret == -EBUSY && ext_info->shared)
828 continue;
829
830 if (ret)
Jonathan Cameron37044322013-09-08 14:57:00 +0100831 return ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100832
833 attrcount++;
834 }
835 }
836
Jonathan Cameron37044322013-09-08 14:57:00 +0100837 return attrcount;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100838}
839
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100840/**
841 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
842 * @attr_list: List of IIO device attributes
843 *
844 * This function frees the memory allocated for each of the IIO device
845 * attributes in the list. Note: if you want to reuse the list after calling
846 * this function you have to reinitialize it using INIT_LIST_HEAD().
847 */
848void iio_free_chan_devattr_list(struct list_head *attr_list)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100849{
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100850 struct iio_dev_attr *p, *n;
851
852 list_for_each_entry_safe(p, n, attr_list, l) {
853 kfree(p->dev_attr.attr.name);
854 kfree(p);
855 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100856}
857
Jonathan Cameron1b732882011-05-18 14:41:43 +0100858static ssize_t iio_show_dev_name(struct device *dev,
859 struct device_attribute *attr,
860 char *buf)
861{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200862 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Kees Cook598db582014-03-13 16:46:00 +0000863 return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name);
Jonathan Cameron1b732882011-05-18 14:41:43 +0100864}
865
866static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
867
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100868static int iio_device_register_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100869{
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100870 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100871 struct iio_dev_attr *p;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100872 struct attribute **attr;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100873
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100874 /* First count elements in any existing group */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100875 if (indio_dev->info->attrs) {
876 attr = indio_dev->info->attrs->attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100877 while (*attr++ != NULL)
878 attrcount_orig++;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100879 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100880 attrcount = attrcount_orig;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100881 /*
882 * New channel registration method - relies on the fact a group does
Peter Meerwaldd25b3802012-08-26 13:43:00 +0100883 * not need to be initialized if its name is NULL.
Jonathan Cameron1d892712011-05-18 14:40:51 +0100884 */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100885 if (indio_dev->channels)
886 for (i = 0; i < indio_dev->num_channels; i++) {
887 ret = iio_device_add_channel_sysfs(indio_dev,
888 &indio_dev
Jonathan Cameron1d892712011-05-18 14:40:51 +0100889 ->channels[i]);
890 if (ret < 0)
891 goto error_clear_attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100892 attrcount += ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100893 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100894
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100895 if (indio_dev->name)
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100896 attrcount++;
897
Thomas Meyerd83fb182011-11-29 22:08:00 +0100898 indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
899 sizeof(indio_dev->chan_attr_group.attrs[0]),
900 GFP_KERNEL);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100901 if (indio_dev->chan_attr_group.attrs == NULL) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100902 ret = -ENOMEM;
903 goto error_clear_attrs;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100904 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100905 /* Copy across original attributes */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100906 if (indio_dev->info->attrs)
907 memcpy(indio_dev->chan_attr_group.attrs,
908 indio_dev->info->attrs->attrs,
909 sizeof(indio_dev->chan_attr_group.attrs[0])
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100910 *attrcount_orig);
911 attrn = attrcount_orig;
912 /* Add all elements from the list. */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100913 list_for_each_entry(p, &indio_dev->channel_attr_list, l)
914 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
915 if (indio_dev->name)
916 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100917
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100918 indio_dev->groups[indio_dev->groupcounter++] =
919 &indio_dev->chan_attr_group;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100920
Jonathan Cameron1d892712011-05-18 14:40:51 +0100921 return 0;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100922
Jonathan Cameron1d892712011-05-18 14:40:51 +0100923error_clear_attrs:
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100924 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100925
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100926 return ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100927}
928
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100929static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100930{
Jonathan Cameron1d892712011-05-18 14:40:51 +0100931
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100932 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100933 kfree(indio_dev->chan_attr_group.attrs);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100934}
935
Jonathan Cameron847ec802009-08-18 18:06:19 +0100936static void iio_dev_release(struct device *device)
937{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200938 struct iio_dev *indio_dev = dev_to_iio_dev(device);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100939 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
940 iio_device_unregister_trigger_consumer(indio_dev);
941 iio_device_unregister_eventset(indio_dev);
942 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200943
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100944 iio_buffer_put(indio_dev->buffer);
945
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200946 ida_simple_remove(&iio_ida, indio_dev->id);
947 kfree(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100948}
949
Guenter Roeck17d82b42013-02-07 17:09:00 +0000950struct device_type iio_device_type = {
Jonathan Cameron847ec802009-08-18 18:06:19 +0100951 .name = "iio_device",
952 .release = iio_dev_release,
953};
954
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000955/**
956 * iio_device_alloc() - allocate an iio_dev from a driver
957 * @sizeof_priv: Space to allocate for private structure.
958 **/
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200959struct iio_dev *iio_device_alloc(int sizeof_priv)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100960{
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +0100961 struct iio_dev *dev;
962 size_t alloc_size;
963
964 alloc_size = sizeof(struct iio_dev);
965 if (sizeof_priv) {
966 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
967 alloc_size += sizeof_priv;
968 }
969 /* ensure 32-byte alignment of whole construct ? */
970 alloc_size += IIO_ALIGN - 1;
971
972 dev = kzalloc(alloc_size, GFP_KERNEL);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100973
974 if (dev) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100975 dev->dev.groups = dev->groups;
Guenter Roeck17d82b42013-02-07 17:09:00 +0000976 dev->dev.type = &iio_device_type;
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100977 dev->dev.bus = &iio_bus_type;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100978 device_initialize(&dev->dev);
979 dev_set_drvdata(&dev->dev, (void *)dev);
980 mutex_init(&dev->mlock);
Jonathan Cameronac917a82012-02-15 19:48:00 +0000981 mutex_init(&dev->info_exist_lock);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200982 INIT_LIST_HEAD(&dev->channel_attr_list);
Jonathan Camerona9e39f92011-09-14 13:01:25 +0100983
984 dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
985 if (dev->id < 0) {
986 /* cannot use a dev_err as the name isn't available */
Sachin Kamat3176dd52013-10-24 12:53:00 +0100987 pr_err("failed to get device id\n");
Jonathan Camerona9e39f92011-09-14 13:01:25 +0100988 kfree(dev);
989 return NULL;
990 }
991 dev_set_name(&dev->dev, "iio:device%d", dev->id);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100992 INIT_LIST_HEAD(&dev->buffer_list);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100993 }
994
995 return dev;
996}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200997EXPORT_SYMBOL(iio_device_alloc);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100998
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000999/**
1000 * iio_device_free() - free an iio_dev from a driver
1001 * @dev: the iio_dev associated with the device
1002 **/
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001003void iio_device_free(struct iio_dev *dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001004{
Lars-Peter Clausene407fd62012-06-04 10:41:42 +02001005 if (dev)
1006 put_device(&dev->dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001007}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001008EXPORT_SYMBOL(iio_device_free);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001009
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001010static void devm_iio_device_release(struct device *dev, void *res)
1011{
1012 iio_device_free(*(struct iio_dev **)res);
1013}
1014
1015static int devm_iio_device_match(struct device *dev, void *res, void *data)
1016{
1017 struct iio_dev **r = res;
1018 if (!r || !*r) {
1019 WARN_ON(!r || !*r);
1020 return 0;
1021 }
1022 return *r == data;
1023}
1024
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001025/**
1026 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1027 * @dev: Device to allocate iio_dev for
1028 * @sizeof_priv: Space to allocate for private structure.
1029 *
1030 * Managed iio_device_alloc. iio_dev allocated with this function is
1031 * automatically freed on driver detach.
1032 *
1033 * If an iio_dev allocated with this function needs to be freed separately,
1034 * devm_iio_device_free() must be used.
1035 *
1036 * RETURNS:
1037 * Pointer to allocated iio_dev on success, NULL on failure.
1038 */
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001039struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
1040{
1041 struct iio_dev **ptr, *iio_dev;
1042
1043 ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
1044 GFP_KERNEL);
1045 if (!ptr)
1046 return NULL;
1047
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001048 iio_dev = iio_device_alloc(sizeof_priv);
1049 if (iio_dev) {
1050 *ptr = iio_dev;
1051 devres_add(dev, ptr);
1052 } else {
1053 devres_free(ptr);
1054 }
1055
1056 return iio_dev;
1057}
1058EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
1059
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001060/**
1061 * devm_iio_device_free - Resource-managed iio_device_free()
1062 * @dev: Device this iio_dev belongs to
1063 * @iio_dev: the iio_dev associated with the device
1064 *
1065 * Free iio_dev allocated with devm_iio_device_alloc().
1066 */
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001067void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
1068{
1069 int rc;
1070
1071 rc = devres_release(dev, devm_iio_device_release,
1072 devm_iio_device_match, iio_dev);
1073 WARN_ON(rc);
1074}
1075EXPORT_SYMBOL_GPL(devm_iio_device_free);
1076
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001077/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001078 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001079 **/
1080static int iio_chrdev_open(struct inode *inode, struct file *filp)
1081{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001082 struct iio_dev *indio_dev = container_of(inode->i_cdev,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001083 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001084
1085 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
1086 return -EBUSY;
1087
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001088 iio_device_get(indio_dev);
1089
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001090 filp->private_data = indio_dev;
Jonathan Cameron30eb82f2011-09-21 11:16:02 +01001091
Lars-Peter Clausen79335142011-12-19 15:23:49 +01001092 return 0;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001093}
1094
1095/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001096 * iio_chrdev_release() - chrdev file close buffer access and ioctls
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001097 **/
1098static int iio_chrdev_release(struct inode *inode, struct file *filp)
1099{
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001100 struct iio_dev *indio_dev = container_of(inode->i_cdev,
1101 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001102 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001103 iio_device_put(indio_dev);
1104
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001105 return 0;
1106}
1107
1108/* Somewhat of a cross file organization violation - ioctls here are actually
1109 * event related */
1110static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1111{
1112 struct iio_dev *indio_dev = filp->private_data;
1113 int __user *ip = (int __user *)arg;
1114 int fd;
1115
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +01001116 if (!indio_dev->info)
1117 return -ENODEV;
1118
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001119 if (cmd == IIO_GET_EVENT_FD_IOCTL) {
1120 fd = iio_event_getfd(indio_dev);
1121 if (copy_to_user(ip, &fd, sizeof(fd)))
1122 return -EFAULT;
1123 return 0;
1124 }
1125 return -EINVAL;
1126}
1127
Jonathan Cameron14555b12011-09-21 11:15:57 +01001128static const struct file_operations iio_buffer_fileops = {
1129 .read = iio_buffer_read_first_n_outer_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001130 .release = iio_chrdev_release,
1131 .open = iio_chrdev_open,
Jonathan Cameron14555b12011-09-21 11:15:57 +01001132 .poll = iio_buffer_poll_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001133 .owner = THIS_MODULE,
1134 .llseek = noop_llseek,
1135 .unlocked_ioctl = iio_ioctl,
1136 .compat_ioctl = iio_ioctl,
1137};
1138
Vlad Dogaru8f5d8722014-12-29 11:37:48 +02001139static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
1140{
1141 int i, j;
1142 const struct iio_chan_spec *channels = indio_dev->channels;
1143
1144 if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
1145 return 0;
1146
1147 for (i = 0; i < indio_dev->num_channels - 1; i++) {
1148 if (channels[i].scan_index < 0)
1149 continue;
1150 for (j = i + 1; j < indio_dev->num_channels; j++)
1151 if (channels[i].scan_index == channels[j].scan_index) {
1152 dev_err(&indio_dev->dev,
1153 "Duplicate scan index %d\n",
1154 channels[i].scan_index);
1155 return -EINVAL;
1156 }
1157 }
1158
1159 return 0;
1160}
1161
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001162static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1163
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001164/**
1165 * iio_device_register() - register a device with the IIO subsystem
1166 * @indio_dev: Device structure filled by the device driver
1167 **/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001168int iio_device_register(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001169{
1170 int ret;
1171
Guenter Roeck17d82b42013-02-07 17:09:00 +00001172 /* If the calling driver did not initialize of_node, do it here */
1173 if (!indio_dev->dev.of_node && indio_dev->dev.parent)
1174 indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
1175
Vlad Dogaru8f5d8722014-12-29 11:37:48 +02001176 ret = iio_check_unique_scan_index(indio_dev);
1177 if (ret < 0)
1178 return ret;
1179
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001180 /* configure elements for the chrdev */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001181 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001182
Michael Henneriche553f182012-03-01 10:51:03 +01001183 ret = iio_device_register_debugfs(indio_dev);
1184 if (ret) {
1185 dev_err(indio_dev->dev.parent,
1186 "Failed to register debugfs interfaces\n");
Hartmut Knaack92825ff2014-02-16 11:53:00 +00001187 return ret;
Michael Henneriche553f182012-03-01 10:51:03 +01001188 }
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001189
1190 ret = iio_buffer_alloc_sysfs_and_mask(indio_dev);
1191 if (ret) {
1192 dev_err(indio_dev->dev.parent,
1193 "Failed to create buffer sysfs interfaces\n");
1194 goto error_unreg_debugfs;
1195 }
1196
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001197 ret = iio_device_register_sysfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001198 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001199 dev_err(indio_dev->dev.parent,
Jonathan Cameron847ec802009-08-18 18:06:19 +01001200 "Failed to register sysfs interfaces\n");
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001201 goto error_buffer_free_sysfs;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001202 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001203 ret = iio_device_register_eventset(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001204 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001205 dev_err(indio_dev->dev.parent,
Roel Van Nyenc849d252010-04-29 19:27:31 +02001206 "Failed to register event set\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001207 goto error_free_sysfs;
1208 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001209 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
1210 iio_device_register_trigger_consumer(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001211
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001212 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1213 indio_dev->setup_ops == NULL)
1214 indio_dev->setup_ops = &noop_ring_setup_ops;
1215
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001216 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
1217 indio_dev->chrdev.owner = indio_dev->info->driver_module;
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001218 indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001219 ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001220 if (ret < 0)
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001221 goto error_unreg_eventset;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001222
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001223 ret = device_add(&indio_dev->dev);
1224 if (ret < 0)
1225 goto error_cdev_del;
1226
1227 return 0;
1228error_cdev_del:
1229 cdev_del(&indio_dev->chrdev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001230error_unreg_eventset:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001231 iio_device_unregister_eventset(indio_dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001232error_free_sysfs:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001233 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001234error_buffer_free_sysfs:
1235 iio_buffer_free_sysfs_and_mask(indio_dev);
Michael Henneriche553f182012-03-01 10:51:03 +01001236error_unreg_debugfs:
1237 iio_device_unregister_debugfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001238 return ret;
1239}
1240EXPORT_SYMBOL(iio_device_register);
1241
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001242/**
1243 * iio_device_unregister() - unregister a device from the IIO subsystem
1244 * @indio_dev: Device structure representing the device.
1245 **/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001246void iio_device_unregister(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001247{
Jonathan Cameronac917a82012-02-15 19:48:00 +00001248 mutex_lock(&indio_dev->info_exist_lock);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001249
1250 device_del(&indio_dev->dev);
1251
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001252 if (indio_dev->chrdev.dev)
1253 cdev_del(&indio_dev->chrdev);
Lars-Peter Clausenbc4c9612013-09-21 16:21:00 +01001254 iio_device_unregister_debugfs(indio_dev);
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001255
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001256 iio_disable_all_buffers(indio_dev);
1257
Jonathan Cameronac917a82012-02-15 19:48:00 +00001258 indio_dev->info = NULL;
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +01001259
1260 iio_device_wakeup_eventset(indio_dev);
1261 iio_buffer_wakeup_poll(indio_dev);
1262
Jonathan Cameronac917a82012-02-15 19:48:00 +00001263 mutex_unlock(&indio_dev->info_exist_lock);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001264
1265 iio_buffer_free_sysfs_and_mask(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001266}
1267EXPORT_SYMBOL(iio_device_unregister);
Sachin Kamat8caa07c2013-10-29 11:39:00 +00001268
1269static void devm_iio_device_unreg(struct device *dev, void *res)
1270{
1271 iio_device_unregister(*(struct iio_dev **)res);
1272}
1273
1274/**
1275 * devm_iio_device_register - Resource-managed iio_device_register()
1276 * @dev: Device to allocate iio_dev for
1277 * @indio_dev: Device structure filled by the device driver
1278 *
1279 * Managed iio_device_register. The IIO device registered with this
1280 * function is automatically unregistered on driver detach. This function
1281 * calls iio_device_register() internally. Refer to that function for more
1282 * information.
1283 *
1284 * If an iio_dev registered with this function needs to be unregistered
1285 * separately, devm_iio_device_unregister() must be used.
1286 *
1287 * RETURNS:
1288 * 0 on success, negative error number on failure.
1289 */
1290int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev)
1291{
1292 struct iio_dev **ptr;
1293 int ret;
1294
1295 ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL);
1296 if (!ptr)
1297 return -ENOMEM;
1298
1299 *ptr = indio_dev;
1300 ret = iio_device_register(indio_dev);
1301 if (!ret)
1302 devres_add(dev, ptr);
1303 else
1304 devres_free(ptr);
1305
1306 return ret;
1307}
1308EXPORT_SYMBOL_GPL(devm_iio_device_register);
1309
1310/**
1311 * devm_iio_device_unregister - Resource-managed iio_device_unregister()
1312 * @dev: Device this iio_dev belongs to
1313 * @indio_dev: the iio_dev associated with the device
1314 *
1315 * Unregister iio_dev registered with devm_iio_device_register().
1316 */
1317void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev)
1318{
1319 int rc;
1320
1321 rc = devres_release(dev, devm_iio_device_unreg,
1322 devm_iio_device_match, indio_dev);
1323 WARN_ON(rc);
1324}
1325EXPORT_SYMBOL_GPL(devm_iio_device_unregister);
1326
Jonathan Cameron847ec802009-08-18 18:06:19 +01001327subsys_initcall(iio_init);
1328module_exit(iio_exit);
1329
Jonathan Cameronc8b95952012-09-02 21:27:56 +01001330MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001331MODULE_DESCRIPTION("Industrial I/O core");
1332MODULE_LICENSE("GPL");