blob: aaba9d3d980ee623ad6b78111a9c9708198b23e0 [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",
Jonathan Cameron1d892712011-05-18 14:40:51 +010078};
79
Jonathan Cameron330c6c52011-09-02 17:14:39 +010080static const char * const iio_modifier_names[] = {
Jonathan Cameron1d892712011-05-18 14:40:51 +010081 [IIO_MOD_X] = "x",
82 [IIO_MOD_Y] = "y",
83 [IIO_MOD_Z] = "z",
Jonathan Cameron8f5879b2012-05-05 10:39:22 +010084 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
Jonathan Cameroncf82cb82012-05-05 10:56:41 +010085 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
Jonathan Cameron330c6c52011-09-02 17:14:39 +010086 [IIO_MOD_LIGHT_BOTH] = "both",
87 [IIO_MOD_LIGHT_IR] = "ir",
Jon Brenner21cd1fa2012-05-16 10:46:42 -050088 [IIO_MOD_LIGHT_CLEAR] = "clear",
89 [IIO_MOD_LIGHT_RED] = "red",
90 [IIO_MOD_LIGHT_GREEN] = "green",
91 [IIO_MOD_LIGHT_BLUE] = "blue",
Srinivas Pandruvada5082f402014-04-29 00:51:00 +010092 [IIO_MOD_QUATERNION] = "quaternion",
Peter Meerwald638b43b2014-02-05 16:57:00 +000093 [IIO_MOD_TEMP_AMBIENT] = "ambient",
94 [IIO_MOD_TEMP_OBJECT] = "object",
Reyad Attiyat11b8dda2014-07-17 19:18:00 +010095 [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
96 [IIO_MOD_NORTH_TRUE] = "from_north_true",
97 [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
98 [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
Daniel Baluta55aebeb2014-11-10 14:45:30 +020099 [IIO_MOD_RUNNING] = "running",
100 [IIO_MOD_JOGGING] = "jogging",
101 [IIO_MOD_WALKING] = "walking",
102 [IIO_MOD_STILL] = "still",
Irina Tirdea5a1a9322015-01-11 21:10:09 +0200103 [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100104};
105
106/* relies on pairs of these shared then separate */
107static const char * const iio_chan_info_postfix[] = {
Jonathan Cameron75a973c2012-04-15 17:41:30 +0100108 [IIO_CHAN_INFO_RAW] = "raw",
109 [IIO_CHAN_INFO_PROCESSED] = "input",
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100110 [IIO_CHAN_INFO_SCALE] = "scale",
111 [IIO_CHAN_INFO_OFFSET] = "offset",
112 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
113 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
114 [IIO_CHAN_INFO_PEAK] = "peak_raw",
115 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
116 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
117 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
Jonathan Camerondf94aba2011-11-27 11:39:12 +0000118 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
119 = "filter_low_pass_3db_frequency",
Laxman Dewangance85a1c2012-04-13 16:03:31 +0530120 [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
Michael Hennericha6b12852012-04-27 10:58:34 +0200121 [IIO_CHAN_INFO_FREQUENCY] = "frequency",
122 [IIO_CHAN_INFO_PHASE] = "phase",
Michael Hennerichb65d6212012-05-11 11:36:53 +0200123 [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
srinivas pandruvada7c9ab032012-09-05 13:56:00 +0100124 [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
Peter Meerwald899d90b2013-09-08 16:20:00 +0100125 [IIO_CHAN_INFO_INT_TIME] = "integration_time",
Irina Tirdeaa88bfe72014-11-10 14:45:32 +0200126 [IIO_CHAN_INFO_ENABLE] = "en",
Irina Tirdeabcdf28f2014-11-10 14:45:33 +0200127 [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight",
Irina Tirdead37f6832015-01-11 21:10:10 +0200128 [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight",
Irina Tirdea2f0ecb72015-01-27 20:41:52 +0200129 [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count",
130 [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100131};
132
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000133/**
134 * iio_find_channel_from_si() - get channel from its scan index
135 * @indio_dev: device
136 * @si: scan index to match
137 */
Jonathan Cameron5fb21c82011-12-05 21:37:10 +0000138const struct iio_chan_spec
139*iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
140{
141 int i;
142
143 for (i = 0; i < indio_dev->num_channels; i++)
144 if (indio_dev->channels[i].scan_index == si)
145 return &indio_dev->channels[i];
146 return NULL;
147}
148
Jonathan Cameron847ec802009-08-18 18:06:19 +0100149/* This turns up an awful lot */
150ssize_t iio_read_const_attr(struct device *dev,
151 struct device_attribute *attr,
152 char *buf)
153{
154 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
155}
156EXPORT_SYMBOL(iio_read_const_attr);
157
Jonathan Cameron847ec802009-08-18 18:06:19 +0100158static int __init iio_init(void)
159{
160 int ret;
161
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100162 /* Register sysfs bus */
163 ret = bus_register(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100164 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100165 pr_err("could not register bus type\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +0100166 goto error_nothing;
167 }
168
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100169 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
170 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100171 pr_err("failed to allocate char dev region\n");
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100172 goto error_unregister_bus_type;
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100173 }
Jonathan Cameron847ec802009-08-18 18:06:19 +0100174
Michael Henneriche553f182012-03-01 10:51:03 +0100175 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
176
Jonathan Cameron847ec802009-08-18 18:06:19 +0100177 return 0;
178
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100179error_unregister_bus_type:
180 bus_unregister(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100181error_nothing:
182 return ret;
183}
184
185static void __exit iio_exit(void)
186{
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100187 if (iio_devt)
188 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100189 bus_unregister(&iio_bus_type);
Michael Henneriche553f182012-03-01 10:51:03 +0100190 debugfs_remove(iio_debugfs_dentry);
191}
192
193#if defined(CONFIG_DEBUG_FS)
Michael Henneriche553f182012-03-01 10:51:03 +0100194static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
195 size_t count, loff_t *ppos)
196{
197 struct iio_dev *indio_dev = file->private_data;
198 char buf[20];
199 unsigned val = 0;
200 ssize_t len;
201 int ret;
202
203 ret = indio_dev->info->debugfs_reg_access(indio_dev,
204 indio_dev->cached_reg_addr,
205 0, &val);
206 if (ret)
207 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
208
209 len = snprintf(buf, sizeof(buf), "0x%X\n", val);
210
211 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
212}
213
214static ssize_t iio_debugfs_write_reg(struct file *file,
215 const char __user *userbuf, size_t count, loff_t *ppos)
216{
217 struct iio_dev *indio_dev = file->private_data;
218 unsigned reg, val;
219 char buf[80];
220 int ret;
221
222 count = min_t(size_t, count, (sizeof(buf)-1));
223 if (copy_from_user(buf, userbuf, count))
224 return -EFAULT;
225
226 buf[count] = 0;
227
228 ret = sscanf(buf, "%i %i", &reg, &val);
229
230 switch (ret) {
231 case 1:
232 indio_dev->cached_reg_addr = reg;
233 break;
234 case 2:
235 indio_dev->cached_reg_addr = reg;
236 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
237 val, NULL);
238 if (ret) {
239 dev_err(indio_dev->dev.parent, "%s: write failed\n",
240 __func__);
241 return ret;
242 }
243 break;
244 default:
245 return -EINVAL;
246 }
247
248 return count;
249}
250
251static const struct file_operations iio_debugfs_reg_fops = {
Axel Lin5a28c872012-05-03 09:50:51 +0800252 .open = simple_open,
Michael Henneriche553f182012-03-01 10:51:03 +0100253 .read = iio_debugfs_read_reg,
254 .write = iio_debugfs_write_reg,
255};
256
257static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
258{
259 debugfs_remove_recursive(indio_dev->debugfs_dentry);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100260}
261
Michael Henneriche553f182012-03-01 10:51:03 +0100262static int iio_device_register_debugfs(struct iio_dev *indio_dev)
263{
264 struct dentry *d;
265
266 if (indio_dev->info->debugfs_reg_access == NULL)
267 return 0;
268
Axel Linabd5a2f2012-05-03 22:56:58 +0800269 if (!iio_debugfs_dentry)
Michael Henneriche553f182012-03-01 10:51:03 +0100270 return 0;
271
272 indio_dev->debugfs_dentry =
273 debugfs_create_dir(dev_name(&indio_dev->dev),
274 iio_debugfs_dentry);
Michael Henneriche553f182012-03-01 10:51:03 +0100275 if (indio_dev->debugfs_dentry == NULL) {
276 dev_warn(indio_dev->dev.parent,
277 "Failed to create debugfs directory\n");
278 return -EFAULT;
279 }
280
281 d = debugfs_create_file("direct_reg_access", 0644,
282 indio_dev->debugfs_dentry,
283 indio_dev, &iio_debugfs_reg_fops);
284 if (!d) {
285 iio_device_unregister_debugfs(indio_dev);
286 return -ENOMEM;
287 }
288
289 return 0;
290}
291#else
292static int iio_device_register_debugfs(struct iio_dev *indio_dev)
293{
294 return 0;
295}
296
297static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
298{
299}
300#endif /* CONFIG_DEBUG_FS */
301
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100302static ssize_t iio_read_channel_ext_info(struct device *dev,
303 struct device_attribute *attr,
304 char *buf)
305{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200306 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100307 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
308 const struct iio_chan_spec_ext_info *ext_info;
309
310 ext_info = &this_attr->c->ext_info[this_attr->address];
311
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200312 return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100313}
314
315static ssize_t iio_write_channel_ext_info(struct device *dev,
316 struct device_attribute *attr,
317 const char *buf,
318 size_t len)
319{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200320 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100321 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
322 const struct iio_chan_spec_ext_info *ext_info;
323
324 ext_info = &this_attr->c->ext_info[this_attr->address];
325
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200326 return ext_info->write(indio_dev, ext_info->private,
327 this_attr->c, buf, len);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100328}
329
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200330ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
331 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
332{
333 const struct iio_enum *e = (const struct iio_enum *)priv;
334 unsigned int i;
335 size_t len = 0;
336
337 if (!e->num_items)
338 return 0;
339
340 for (i = 0; i < e->num_items; ++i)
Lars-Peter Clausen74dcd432012-06-05 18:24:12 +0200341 len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200342
343 /* replace last space with a newline */
344 buf[len - 1] = '\n';
345
346 return len;
347}
348EXPORT_SYMBOL_GPL(iio_enum_available_read);
349
350ssize_t iio_enum_read(struct iio_dev *indio_dev,
351 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
352{
353 const struct iio_enum *e = (const struct iio_enum *)priv;
354 int i;
355
356 if (!e->get)
357 return -EINVAL;
358
359 i = e->get(indio_dev, chan);
360 if (i < 0)
361 return i;
362 else if (i >= e->num_items)
363 return -EINVAL;
364
Kees Cook598db582014-03-13 16:46:00 +0000365 return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200366}
367EXPORT_SYMBOL_GPL(iio_enum_read);
368
369ssize_t iio_enum_write(struct iio_dev *indio_dev,
370 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
371 size_t len)
372{
373 const struct iio_enum *e = (const struct iio_enum *)priv;
374 unsigned int i;
375 int ret;
376
377 if (!e->set)
378 return -EINVAL;
379
380 for (i = 0; i < e->num_items; i++) {
381 if (sysfs_streq(buf, e->items[i]))
382 break;
383 }
384
385 if (i == e->num_items)
386 return -EINVAL;
387
388 ret = e->set(indio_dev, chan, i);
389 return ret ? ret : len;
390}
391EXPORT_SYMBOL_GPL(iio_enum_write);
392
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100393/**
394 * iio_format_value() - Formats a IIO value into its string representation
395 * @buf: The buffer to which the formated value gets written
396 * @type: One of the IIO_VAL_... constants. This decides how the val and val2
397 * parameters are formatted.
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100398 * @vals: pointer to the values, exact meaning depends on the type parameter.
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100399 */
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100400ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100401{
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100402 unsigned long long tmp;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200403 bool scale_db = false;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100404
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100405 switch (type) {
Michael Hennerich67eedba2012-05-11 11:36:52 +0200406 case IIO_VAL_INT:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100407 return sprintf(buf, "%d\n", vals[0]);
Michael Hennerich67eedba2012-05-11 11:36:52 +0200408 case IIO_VAL_INT_PLUS_MICRO_DB:
409 scale_db = true;
410 case IIO_VAL_INT_PLUS_MICRO:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100411 if (vals[1] < 0)
412 return sprintf(buf, "-%ld.%06u%s\n", abs(vals[0]),
413 -vals[1],
Michael Hennerich67eedba2012-05-11 11:36:52 +0200414 scale_db ? " dB" : "");
Jonathan Cameron1d892712011-05-18 14:40:51 +0100415 else
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100416 return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1],
Michael Hennerich67eedba2012-05-11 11:36:52 +0200417 scale_db ? " dB" : "");
418 case IIO_VAL_INT_PLUS_NANO:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100419 if (vals[1] < 0)
420 return sprintf(buf, "-%ld.%09u\n", abs(vals[0]),
421 -vals[1]);
Michael Hennerich71646e22011-06-27 13:07:07 +0100422 else
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100423 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100424 case IIO_VAL_FRACTIONAL:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100425 tmp = div_s64((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]);
Lars-Peter Clausen103d9fb2012-10-16 17:29:00 +0100429 case IIO_VAL_FRACTIONAL_LOG2:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100430 tmp = (s64)vals[0] * 1000000000LL >> vals[1];
431 vals[1] = do_div(tmp, 1000000000LL);
432 vals[0] = tmp;
433 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
434 case IIO_VAL_INT_MULTIPLE:
435 {
436 int i;
437 int len = 0;
438
439 for (i = 0; i < size; ++i)
440 len += snprintf(&buf[len], PAGE_SIZE - len, "%d ",
441 vals[i]);
442 len += snprintf(&buf[len], PAGE_SIZE - len, "\n");
443 return len;
444 }
Michael Hennerich67eedba2012-05-11 11:36:52 +0200445 default:
Jonathan Cameron1d892712011-05-18 14:40:51 +0100446 return 0;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200447 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100448}
449
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100450static ssize_t iio_read_channel_info(struct device *dev,
451 struct device_attribute *attr,
452 char *buf)
453{
454 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
455 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100456 int vals[INDIO_MAX_RAW_ELEMENTS];
457 int ret;
458 int val_len = 2;
459
460 if (indio_dev->info->read_raw_multi)
461 ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
462 INDIO_MAX_RAW_ELEMENTS,
463 vals, &val_len,
464 this_attr->address);
465 else
466 ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
467 &vals[0], &vals[1], this_attr->address);
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100468
469 if (ret < 0)
470 return ret;
471
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100472 return iio_format_value(buf, ret, val_len, vals);
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100473}
474
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000475/**
476 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
477 * @str: The string to parse
478 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
479 * @integer: The integer part of the number
480 * @fract: The fractional part of the number
481 *
482 * Returns 0 on success, or a negative error code if the string could not be
483 * parsed.
484 */
485int iio_str_to_fixpoint(const char *str, int fract_mult,
486 int *integer, int *fract)
487{
488 int i = 0, f = 0;
489 bool integer_part = true, negative = false;
490
491 if (str[0] == '-') {
492 negative = true;
493 str++;
494 } else if (str[0] == '+') {
495 str++;
496 }
497
498 while (*str) {
499 if ('0' <= *str && *str <= '9') {
500 if (integer_part) {
501 i = i * 10 + *str - '0';
502 } else {
503 f += fract_mult * (*str - '0');
504 fract_mult /= 10;
505 }
506 } else if (*str == '\n') {
507 if (*(str + 1) == '\0')
508 break;
509 else
510 return -EINVAL;
511 } else if (*str == '.' && integer_part) {
512 integer_part = false;
513 } else {
514 return -EINVAL;
515 }
516 str++;
517 }
518
519 if (negative) {
520 if (i)
521 i = -i;
522 else
523 f = -f;
524 }
525
526 *integer = i;
527 *fract = f;
528
529 return 0;
530}
531EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
532
Jonathan Cameron1d892712011-05-18 14:40:51 +0100533static ssize_t iio_write_channel_info(struct device *dev,
534 struct device_attribute *attr,
535 const char *buf,
536 size_t len)
537{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200538 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100539 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000540 int ret, fract_mult = 100000;
541 int integer, fract;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100542
543 /* Assumes decimal - precision based on number of digits */
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100544 if (!indio_dev->info->write_raw)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100545 return -EINVAL;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100546
547 if (indio_dev->info->write_raw_get_fmt)
548 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
549 this_attr->c, this_attr->address)) {
550 case IIO_VAL_INT_PLUS_MICRO:
551 fract_mult = 100000;
552 break;
553 case IIO_VAL_INT_PLUS_NANO:
554 fract_mult = 100000000;
555 break;
556 default:
557 return -EINVAL;
558 }
559
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000560 ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
561 if (ret)
562 return ret;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100563
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100564 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
Michael Hennerich5c04af02011-06-27 13:07:10 +0100565 integer, fract, this_attr->address);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100566 if (ret)
567 return ret;
568
569 return len;
570}
571
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100572static
Jonathan Cameron1d892712011-05-18 14:40:51 +0100573int __iio_device_attr_init(struct device_attribute *dev_attr,
574 const char *postfix,
575 struct iio_chan_spec const *chan,
576 ssize_t (*readfunc)(struct device *dev,
577 struct device_attribute *attr,
578 char *buf),
579 ssize_t (*writefunc)(struct device *dev,
580 struct device_attribute *attr,
581 const char *buf,
582 size_t len),
Jonathan Cameron37044322013-09-08 14:57:00 +0100583 enum iio_shared_by shared_by)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100584{
Jonathan Cameron37044322013-09-08 14:57:00 +0100585 int ret = 0;
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000586 char *name = NULL;
Jonathan Cameron37044322013-09-08 14:57:00 +0100587 char *full_postfix;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100588 sysfs_attr_init(&dev_attr->attr);
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100589
590 /* Build up postfix of <extend_name>_<modifier>_postfix */
Jonathan Cameron37044322013-09-08 14:57:00 +0100591 if (chan->modified && (shared_by == IIO_SEPARATE)) {
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100592 if (chan->extend_name)
593 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
594 iio_modifier_names[chan
595 ->channel2],
596 chan->extend_name,
597 postfix);
598 else
599 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
600 iio_modifier_names[chan
601 ->channel2],
602 postfix);
603 } else {
Lars-Peter Clausen77bfa8b2014-02-14 14:19:00 +0000604 if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100605 full_postfix = kstrdup(postfix, GFP_KERNEL);
606 else
607 full_postfix = kasprintf(GFP_KERNEL,
608 "%s_%s",
609 chan->extend_name,
610 postfix);
611 }
Jonathan Cameron37044322013-09-08 14:57:00 +0100612 if (full_postfix == NULL)
613 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100614
Justin P. Mattock4abf6f82012-02-29 22:00:38 -0800615 if (chan->differential) { /* Differential can not have modifier */
Jonathan Cameron37044322013-09-08 14:57:00 +0100616 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100617 case IIO_SHARED_BY_ALL:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000618 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100619 break;
620 case IIO_SHARED_BY_DIR:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000621 name = kasprintf(GFP_KERNEL, "%s_%s",
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100622 iio_direction[chan->output],
623 full_postfix);
624 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100625 case IIO_SHARED_BY_TYPE:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000626 name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100627 iio_direction[chan->output],
628 iio_chan_type_name_spec[chan->type],
629 iio_chan_type_name_spec[chan->type],
630 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100631 break;
632 case IIO_SEPARATE:
633 if (!chan->indexed) {
634 WARN_ON("Differential channels must be indexed\n");
635 ret = -EINVAL;
636 goto error_free_full_postfix;
637 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000638 name = kasprintf(GFP_KERNEL,
Jonathan Cameron37044322013-09-08 14:57:00 +0100639 "%s_%s%d-%s%d_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100640 iio_direction[chan->output],
641 iio_chan_type_name_spec[chan->type],
642 chan->channel,
643 iio_chan_type_name_spec[chan->type],
644 chan->channel2,
645 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100646 break;
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100647 }
648 } else { /* Single ended */
Jonathan Cameron37044322013-09-08 14:57:00 +0100649 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100650 case IIO_SHARED_BY_ALL:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000651 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100652 break;
653 case IIO_SHARED_BY_DIR:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000654 name = kasprintf(GFP_KERNEL, "%s_%s",
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100655 iio_direction[chan->output],
656 full_postfix);
657 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100658 case IIO_SHARED_BY_TYPE:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000659 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100660 iio_direction[chan->output],
661 iio_chan_type_name_spec[chan->type],
662 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100663 break;
664
665 case IIO_SEPARATE:
666 if (chan->indexed)
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000667 name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
Jonathan Cameron37044322013-09-08 14:57:00 +0100668 iio_direction[chan->output],
669 iio_chan_type_name_spec[chan->type],
670 chan->channel,
671 full_postfix);
672 else
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000673 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
Jonathan Cameron37044322013-09-08 14:57:00 +0100674 iio_direction[chan->output],
675 iio_chan_type_name_spec[chan->type],
676 full_postfix);
677 break;
678 }
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100679 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000680 if (name == NULL) {
Jonathan Cameron1d892712011-05-18 14:40:51 +0100681 ret = -ENOMEM;
682 goto error_free_full_postfix;
683 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000684 dev_attr->attr.name = name;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100685
686 if (readfunc) {
687 dev_attr->attr.mode |= S_IRUGO;
688 dev_attr->show = readfunc;
689 }
690
691 if (writefunc) {
692 dev_attr->attr.mode |= S_IWUSR;
693 dev_attr->store = writefunc;
694 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000695
Jonathan Cameron1d892712011-05-18 14:40:51 +0100696error_free_full_postfix:
697 kfree(full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100698
Jonathan Cameron847ec802009-08-18 18:06:19 +0100699 return ret;
700}
701
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100702static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100703{
704 kfree(dev_attr->attr.name);
705}
706
707int __iio_add_chan_devattr(const char *postfix,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100708 struct iio_chan_spec const *chan,
709 ssize_t (*readfunc)(struct device *dev,
710 struct device_attribute *attr,
711 char *buf),
712 ssize_t (*writefunc)(struct device *dev,
713 struct device_attribute *attr,
714 const char *buf,
715 size_t len),
Jonathan Camerone614a542011-09-02 17:14:41 +0100716 u64 mask,
Jonathan Cameron37044322013-09-08 14:57:00 +0100717 enum iio_shared_by shared_by,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100718 struct device *dev,
719 struct list_head *attr_list)
720{
721 int ret;
722 struct iio_dev_attr *iio_attr, *t;
723
Sachin Kamat670c1102013-10-24 12:53:00 +0100724 iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000725 if (iio_attr == NULL)
726 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100727 ret = __iio_device_attr_init(&iio_attr->dev_attr,
728 postfix, chan,
Jonathan Cameron37044322013-09-08 14:57:00 +0100729 readfunc, writefunc, shared_by);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100730 if (ret)
731 goto error_iio_dev_attr_free;
732 iio_attr->c = chan;
733 iio_attr->address = mask;
734 list_for_each_entry(t, attr_list, l)
735 if (strcmp(t->dev_attr.attr.name,
736 iio_attr->dev_attr.attr.name) == 0) {
Jonathan Cameron37044322013-09-08 14:57:00 +0100737 if (shared_by == IIO_SEPARATE)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100738 dev_err(dev, "tried to double register : %s\n",
739 t->dev_attr.attr.name);
740 ret = -EBUSY;
741 goto error_device_attr_deinit;
742 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100743 list_add(&iio_attr->l, attr_list);
744
745 return 0;
746
747error_device_attr_deinit:
748 __iio_device_attr_deinit(&iio_attr->dev_attr);
749error_iio_dev_attr_free:
750 kfree(iio_attr);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100751 return ret;
752}
753
Jonathan Cameron37044322013-09-08 14:57:00 +0100754static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
755 struct iio_chan_spec const *chan,
756 enum iio_shared_by shared_by,
757 const long *infomask)
758{
759 int i, ret, attrcount = 0;
760
761 for_each_set_bit(i, infomask, sizeof(infomask)*8) {
Jonathan Cameronef4b4852014-01-03 22:24:00 +0000762 if (i >= ARRAY_SIZE(iio_chan_info_postfix))
763 return -EINVAL;
Jonathan Cameron37044322013-09-08 14:57:00 +0100764 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
765 chan,
766 &iio_read_channel_info,
767 &iio_write_channel_info,
768 i,
769 shared_by,
770 &indio_dev->dev,
771 &indio_dev->channel_attr_list);
772 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
773 continue;
774 else if (ret < 0)
775 return ret;
776 attrcount++;
777 }
778
779 return attrcount;
780}
781
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100782static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100783 struct iio_chan_spec const *chan)
784{
Jonathan Cameron5ccb3ad2012-04-15 17:41:16 +0100785 int ret, attrcount = 0;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100786 const struct iio_chan_spec_ext_info *ext_info;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100787
Jonathan Cameron1d892712011-05-18 14:40:51 +0100788 if (chan->channel < 0)
789 return 0;
Jonathan Cameron37044322013-09-08 14:57:00 +0100790 ret = iio_device_add_info_mask_type(indio_dev, chan,
791 IIO_SEPARATE,
792 &chan->info_mask_separate);
793 if (ret < 0)
794 return ret;
795 attrcount += ret;
796
797 ret = iio_device_add_info_mask_type(indio_dev, chan,
798 IIO_SHARED_BY_TYPE,
799 &chan->info_mask_shared_by_type);
800 if (ret < 0)
801 return ret;
802 attrcount += ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100803
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100804 ret = iio_device_add_info_mask_type(indio_dev, chan,
805 IIO_SHARED_BY_DIR,
806 &chan->info_mask_shared_by_dir);
807 if (ret < 0)
808 return ret;
809 attrcount += ret;
810
811 ret = iio_device_add_info_mask_type(indio_dev, chan,
812 IIO_SHARED_BY_ALL,
813 &chan->info_mask_shared_by_all);
814 if (ret < 0)
815 return ret;
816 attrcount += ret;
817
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100818 if (chan->ext_info) {
819 unsigned int i = 0;
820 for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
821 ret = __iio_add_chan_devattr(ext_info->name,
822 chan,
823 ext_info->read ?
824 &iio_read_channel_ext_info : NULL,
825 ext_info->write ?
826 &iio_write_channel_ext_info : NULL,
827 i,
828 ext_info->shared,
829 &indio_dev->dev,
830 &indio_dev->channel_attr_list);
831 i++;
832 if (ret == -EBUSY && ext_info->shared)
833 continue;
834
835 if (ret)
Jonathan Cameron37044322013-09-08 14:57:00 +0100836 return ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100837
838 attrcount++;
839 }
840 }
841
Jonathan Cameron37044322013-09-08 14:57:00 +0100842 return attrcount;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100843}
844
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100845/**
846 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
847 * @attr_list: List of IIO device attributes
848 *
849 * This function frees the memory allocated for each of the IIO device
850 * attributes in the list. Note: if you want to reuse the list after calling
851 * this function you have to reinitialize it using INIT_LIST_HEAD().
852 */
853void iio_free_chan_devattr_list(struct list_head *attr_list)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100854{
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100855 struct iio_dev_attr *p, *n;
856
857 list_for_each_entry_safe(p, n, attr_list, l) {
858 kfree(p->dev_attr.attr.name);
859 kfree(p);
860 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100861}
862
Jonathan Cameron1b732882011-05-18 14:41:43 +0100863static ssize_t iio_show_dev_name(struct device *dev,
864 struct device_attribute *attr,
865 char *buf)
866{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200867 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Kees Cook598db582014-03-13 16:46:00 +0000868 return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name);
Jonathan Cameron1b732882011-05-18 14:41:43 +0100869}
870
871static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
872
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100873static int iio_device_register_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100874{
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100875 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100876 struct iio_dev_attr *p;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100877 struct attribute **attr;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100878
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100879 /* First count elements in any existing group */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100880 if (indio_dev->info->attrs) {
881 attr = indio_dev->info->attrs->attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100882 while (*attr++ != NULL)
883 attrcount_orig++;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100884 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100885 attrcount = attrcount_orig;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100886 /*
887 * New channel registration method - relies on the fact a group does
Peter Meerwaldd25b3802012-08-26 13:43:00 +0100888 * not need to be initialized if its name is NULL.
Jonathan Cameron1d892712011-05-18 14:40:51 +0100889 */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100890 if (indio_dev->channels)
891 for (i = 0; i < indio_dev->num_channels; i++) {
892 ret = iio_device_add_channel_sysfs(indio_dev,
893 &indio_dev
Jonathan Cameron1d892712011-05-18 14:40:51 +0100894 ->channels[i]);
895 if (ret < 0)
896 goto error_clear_attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100897 attrcount += ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100898 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100899
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100900 if (indio_dev->name)
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100901 attrcount++;
902
Thomas Meyerd83fb182011-11-29 22:08:00 +0100903 indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
904 sizeof(indio_dev->chan_attr_group.attrs[0]),
905 GFP_KERNEL);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100906 if (indio_dev->chan_attr_group.attrs == NULL) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100907 ret = -ENOMEM;
908 goto error_clear_attrs;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100909 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100910 /* Copy across original attributes */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100911 if (indio_dev->info->attrs)
912 memcpy(indio_dev->chan_attr_group.attrs,
913 indio_dev->info->attrs->attrs,
914 sizeof(indio_dev->chan_attr_group.attrs[0])
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100915 *attrcount_orig);
916 attrn = attrcount_orig;
917 /* Add all elements from the list. */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100918 list_for_each_entry(p, &indio_dev->channel_attr_list, l)
919 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
920 if (indio_dev->name)
921 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100922
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100923 indio_dev->groups[indio_dev->groupcounter++] =
924 &indio_dev->chan_attr_group;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100925
Jonathan Cameron1d892712011-05-18 14:40:51 +0100926 return 0;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100927
Jonathan Cameron1d892712011-05-18 14:40:51 +0100928error_clear_attrs:
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100929 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100930
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100931 return ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100932}
933
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100934static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100935{
Jonathan Cameron1d892712011-05-18 14:40:51 +0100936
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100937 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100938 kfree(indio_dev->chan_attr_group.attrs);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100939}
940
Jonathan Cameron847ec802009-08-18 18:06:19 +0100941static void iio_dev_release(struct device *device)
942{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200943 struct iio_dev *indio_dev = dev_to_iio_dev(device);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100944 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
945 iio_device_unregister_trigger_consumer(indio_dev);
946 iio_device_unregister_eventset(indio_dev);
947 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200948
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100949 iio_buffer_put(indio_dev->buffer);
950
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200951 ida_simple_remove(&iio_ida, indio_dev->id);
952 kfree(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100953}
954
Guenter Roeck17d82b42013-02-07 17:09:00 +0000955struct device_type iio_device_type = {
Jonathan Cameron847ec802009-08-18 18:06:19 +0100956 .name = "iio_device",
957 .release = iio_dev_release,
958};
959
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000960/**
961 * iio_device_alloc() - allocate an iio_dev from a driver
962 * @sizeof_priv: Space to allocate for private structure.
963 **/
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200964struct iio_dev *iio_device_alloc(int sizeof_priv)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100965{
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +0100966 struct iio_dev *dev;
967 size_t alloc_size;
968
969 alloc_size = sizeof(struct iio_dev);
970 if (sizeof_priv) {
971 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
972 alloc_size += sizeof_priv;
973 }
974 /* ensure 32-byte alignment of whole construct ? */
975 alloc_size += IIO_ALIGN - 1;
976
977 dev = kzalloc(alloc_size, GFP_KERNEL);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100978
979 if (dev) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100980 dev->dev.groups = dev->groups;
Guenter Roeck17d82b42013-02-07 17:09:00 +0000981 dev->dev.type = &iio_device_type;
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100982 dev->dev.bus = &iio_bus_type;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100983 device_initialize(&dev->dev);
984 dev_set_drvdata(&dev->dev, (void *)dev);
985 mutex_init(&dev->mlock);
Jonathan Cameronac917a82012-02-15 19:48:00 +0000986 mutex_init(&dev->info_exist_lock);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200987 INIT_LIST_HEAD(&dev->channel_attr_list);
Jonathan Camerona9e39f92011-09-14 13:01:25 +0100988
989 dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
990 if (dev->id < 0) {
991 /* cannot use a dev_err as the name isn't available */
Sachin Kamat3176dd52013-10-24 12:53:00 +0100992 pr_err("failed to get device id\n");
Jonathan Camerona9e39f92011-09-14 13:01:25 +0100993 kfree(dev);
994 return NULL;
995 }
996 dev_set_name(&dev->dev, "iio:device%d", dev->id);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100997 INIT_LIST_HEAD(&dev->buffer_list);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100998 }
999
1000 return dev;
1001}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001002EXPORT_SYMBOL(iio_device_alloc);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001003
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001004/**
1005 * iio_device_free() - free an iio_dev from a driver
1006 * @dev: the iio_dev associated with the device
1007 **/
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001008void iio_device_free(struct iio_dev *dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001009{
Lars-Peter Clausene407fd62012-06-04 10:41:42 +02001010 if (dev)
1011 put_device(&dev->dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001012}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001013EXPORT_SYMBOL(iio_device_free);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001014
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001015static void devm_iio_device_release(struct device *dev, void *res)
1016{
1017 iio_device_free(*(struct iio_dev **)res);
1018}
1019
1020static int devm_iio_device_match(struct device *dev, void *res, void *data)
1021{
1022 struct iio_dev **r = res;
1023 if (!r || !*r) {
1024 WARN_ON(!r || !*r);
1025 return 0;
1026 }
1027 return *r == data;
1028}
1029
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001030/**
1031 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1032 * @dev: Device to allocate iio_dev for
1033 * @sizeof_priv: Space to allocate for private structure.
1034 *
1035 * Managed iio_device_alloc. iio_dev allocated with this function is
1036 * automatically freed on driver detach.
1037 *
1038 * If an iio_dev allocated with this function needs to be freed separately,
1039 * devm_iio_device_free() must be used.
1040 *
1041 * RETURNS:
1042 * Pointer to allocated iio_dev on success, NULL on failure.
1043 */
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001044struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
1045{
1046 struct iio_dev **ptr, *iio_dev;
1047
1048 ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
1049 GFP_KERNEL);
1050 if (!ptr)
1051 return NULL;
1052
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001053 iio_dev = iio_device_alloc(sizeof_priv);
1054 if (iio_dev) {
1055 *ptr = iio_dev;
1056 devres_add(dev, ptr);
1057 } else {
1058 devres_free(ptr);
1059 }
1060
1061 return iio_dev;
1062}
1063EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
1064
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001065/**
1066 * devm_iio_device_free - Resource-managed iio_device_free()
1067 * @dev: Device this iio_dev belongs to
1068 * @iio_dev: the iio_dev associated with the device
1069 *
1070 * Free iio_dev allocated with devm_iio_device_alloc().
1071 */
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001072void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
1073{
1074 int rc;
1075
1076 rc = devres_release(dev, devm_iio_device_release,
1077 devm_iio_device_match, iio_dev);
1078 WARN_ON(rc);
1079}
1080EXPORT_SYMBOL_GPL(devm_iio_device_free);
1081
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001082/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001083 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001084 **/
1085static int iio_chrdev_open(struct inode *inode, struct file *filp)
1086{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001087 struct iio_dev *indio_dev = container_of(inode->i_cdev,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001088 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001089
1090 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
1091 return -EBUSY;
1092
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001093 iio_device_get(indio_dev);
1094
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001095 filp->private_data = indio_dev;
Jonathan Cameron30eb82f2011-09-21 11:16:02 +01001096
Lars-Peter Clausen79335142011-12-19 15:23:49 +01001097 return 0;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001098}
1099
1100/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001101 * iio_chrdev_release() - chrdev file close buffer access and ioctls
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001102 **/
1103static int iio_chrdev_release(struct inode *inode, struct file *filp)
1104{
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001105 struct iio_dev *indio_dev = container_of(inode->i_cdev,
1106 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001107 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001108 iio_device_put(indio_dev);
1109
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001110 return 0;
1111}
1112
1113/* Somewhat of a cross file organization violation - ioctls here are actually
1114 * event related */
1115static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1116{
1117 struct iio_dev *indio_dev = filp->private_data;
1118 int __user *ip = (int __user *)arg;
1119 int fd;
1120
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +01001121 if (!indio_dev->info)
1122 return -ENODEV;
1123
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001124 if (cmd == IIO_GET_EVENT_FD_IOCTL) {
1125 fd = iio_event_getfd(indio_dev);
1126 if (copy_to_user(ip, &fd, sizeof(fd)))
1127 return -EFAULT;
1128 return 0;
1129 }
1130 return -EINVAL;
1131}
1132
Jonathan Cameron14555b12011-09-21 11:15:57 +01001133static const struct file_operations iio_buffer_fileops = {
1134 .read = iio_buffer_read_first_n_outer_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001135 .release = iio_chrdev_release,
1136 .open = iio_chrdev_open,
Jonathan Cameron14555b12011-09-21 11:15:57 +01001137 .poll = iio_buffer_poll_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001138 .owner = THIS_MODULE,
1139 .llseek = noop_llseek,
1140 .unlocked_ioctl = iio_ioctl,
1141 .compat_ioctl = iio_ioctl,
1142};
1143
Vlad Dogaru8f5d8722014-12-29 11:37:48 +02001144static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
1145{
1146 int i, j;
1147 const struct iio_chan_spec *channels = indio_dev->channels;
1148
1149 if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
1150 return 0;
1151
1152 for (i = 0; i < indio_dev->num_channels - 1; i++) {
1153 if (channels[i].scan_index < 0)
1154 continue;
1155 for (j = i + 1; j < indio_dev->num_channels; j++)
1156 if (channels[i].scan_index == channels[j].scan_index) {
1157 dev_err(&indio_dev->dev,
1158 "Duplicate scan index %d\n",
1159 channels[i].scan_index);
1160 return -EINVAL;
1161 }
1162 }
1163
1164 return 0;
1165}
1166
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001167static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1168
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001169/**
1170 * iio_device_register() - register a device with the IIO subsystem
1171 * @indio_dev: Device structure filled by the device driver
1172 **/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001173int iio_device_register(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001174{
1175 int ret;
1176
Guenter Roeck17d82b42013-02-07 17:09:00 +00001177 /* If the calling driver did not initialize of_node, do it here */
1178 if (!indio_dev->dev.of_node && indio_dev->dev.parent)
1179 indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
1180
Vlad Dogaru8f5d8722014-12-29 11:37:48 +02001181 ret = iio_check_unique_scan_index(indio_dev);
1182 if (ret < 0)
1183 return ret;
1184
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001185 /* configure elements for the chrdev */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001186 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001187
Michael Henneriche553f182012-03-01 10:51:03 +01001188 ret = iio_device_register_debugfs(indio_dev);
1189 if (ret) {
1190 dev_err(indio_dev->dev.parent,
1191 "Failed to register debugfs interfaces\n");
Hartmut Knaack92825ff2014-02-16 11:53:00 +00001192 return ret;
Michael Henneriche553f182012-03-01 10:51:03 +01001193 }
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001194
1195 ret = iio_buffer_alloc_sysfs_and_mask(indio_dev);
1196 if (ret) {
1197 dev_err(indio_dev->dev.parent,
1198 "Failed to create buffer sysfs interfaces\n");
1199 goto error_unreg_debugfs;
1200 }
1201
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001202 ret = iio_device_register_sysfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001203 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001204 dev_err(indio_dev->dev.parent,
Jonathan Cameron847ec802009-08-18 18:06:19 +01001205 "Failed to register sysfs interfaces\n");
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001206 goto error_buffer_free_sysfs;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001207 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001208 ret = iio_device_register_eventset(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001209 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001210 dev_err(indio_dev->dev.parent,
Roel Van Nyenc849d252010-04-29 19:27:31 +02001211 "Failed to register event set\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001212 goto error_free_sysfs;
1213 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001214 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
1215 iio_device_register_trigger_consumer(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001216
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001217 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1218 indio_dev->setup_ops == NULL)
1219 indio_dev->setup_ops = &noop_ring_setup_ops;
1220
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001221 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
1222 indio_dev->chrdev.owner = indio_dev->info->driver_module;
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001223 indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001224 ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001225 if (ret < 0)
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001226 goto error_unreg_eventset;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001227
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001228 ret = device_add(&indio_dev->dev);
1229 if (ret < 0)
1230 goto error_cdev_del;
1231
1232 return 0;
1233error_cdev_del:
1234 cdev_del(&indio_dev->chrdev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001235error_unreg_eventset:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001236 iio_device_unregister_eventset(indio_dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001237error_free_sysfs:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001238 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001239error_buffer_free_sysfs:
1240 iio_buffer_free_sysfs_and_mask(indio_dev);
Michael Henneriche553f182012-03-01 10:51:03 +01001241error_unreg_debugfs:
1242 iio_device_unregister_debugfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001243 return ret;
1244}
1245EXPORT_SYMBOL(iio_device_register);
1246
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001247/**
1248 * iio_device_unregister() - unregister a device from the IIO subsystem
1249 * @indio_dev: Device structure representing the device.
1250 **/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001251void iio_device_unregister(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001252{
Jonathan Cameronac917a82012-02-15 19:48:00 +00001253 mutex_lock(&indio_dev->info_exist_lock);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001254
1255 device_del(&indio_dev->dev);
1256
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001257 if (indio_dev->chrdev.dev)
1258 cdev_del(&indio_dev->chrdev);
Lars-Peter Clausenbc4c9612013-09-21 16:21:00 +01001259 iio_device_unregister_debugfs(indio_dev);
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001260
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001261 iio_disable_all_buffers(indio_dev);
1262
Jonathan Cameronac917a82012-02-15 19:48:00 +00001263 indio_dev->info = NULL;
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +01001264
1265 iio_device_wakeup_eventset(indio_dev);
1266 iio_buffer_wakeup_poll(indio_dev);
1267
Jonathan Cameronac917a82012-02-15 19:48:00 +00001268 mutex_unlock(&indio_dev->info_exist_lock);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001269
1270 iio_buffer_free_sysfs_and_mask(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001271}
1272EXPORT_SYMBOL(iio_device_unregister);
Sachin Kamat8caa07c2013-10-29 11:39:00 +00001273
1274static void devm_iio_device_unreg(struct device *dev, void *res)
1275{
1276 iio_device_unregister(*(struct iio_dev **)res);
1277}
1278
1279/**
1280 * devm_iio_device_register - Resource-managed iio_device_register()
1281 * @dev: Device to allocate iio_dev for
1282 * @indio_dev: Device structure filled by the device driver
1283 *
1284 * Managed iio_device_register. The IIO device registered with this
1285 * function is automatically unregistered on driver detach. This function
1286 * calls iio_device_register() internally. Refer to that function for more
1287 * information.
1288 *
1289 * If an iio_dev registered with this function needs to be unregistered
1290 * separately, devm_iio_device_unregister() must be used.
1291 *
1292 * RETURNS:
1293 * 0 on success, negative error number on failure.
1294 */
1295int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev)
1296{
1297 struct iio_dev **ptr;
1298 int ret;
1299
1300 ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL);
1301 if (!ptr)
1302 return -ENOMEM;
1303
1304 *ptr = indio_dev;
1305 ret = iio_device_register(indio_dev);
1306 if (!ret)
1307 devres_add(dev, ptr);
1308 else
1309 devres_free(ptr);
1310
1311 return ret;
1312}
1313EXPORT_SYMBOL_GPL(devm_iio_device_register);
1314
1315/**
1316 * devm_iio_device_unregister - Resource-managed iio_device_unregister()
1317 * @dev: Device this iio_dev belongs to
1318 * @indio_dev: the iio_dev associated with the device
1319 *
1320 * Unregister iio_dev registered with devm_iio_device_register().
1321 */
1322void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev)
1323{
1324 int rc;
1325
1326 rc = devres_release(dev, devm_iio_device_unreg,
1327 devm_iio_device_match, indio_dev);
1328 WARN_ON(rc);
1329}
1330EXPORT_SYMBOL_GPL(devm_iio_device_unregister);
1331
Jonathan Cameron847ec802009-08-18 18:06:19 +01001332subsys_initcall(iio_init);
1333module_exit(iio_exit);
1334
Jonathan Cameronc8b95952012-09-02 21:27:56 +01001335MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001336MODULE_DESCRIPTION("Industrial I/O core");
1337MODULE_LICENSE("GPL");