blob: 3524b0de872110d31442060a109c5c4243a3f0cf [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)",
Lars-Peter Clausen1ce87f22015-05-22 18:17:38 +0200104 [IIO_MOD_I] = "i",
105 [IIO_MOD_Q] = "q",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100106};
107
108/* relies on pairs of these shared then separate */
109static const char * const iio_chan_info_postfix[] = {
Jonathan Cameron75a973c2012-04-15 17:41:30 +0100110 [IIO_CHAN_INFO_RAW] = "raw",
111 [IIO_CHAN_INFO_PROCESSED] = "input",
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100112 [IIO_CHAN_INFO_SCALE] = "scale",
113 [IIO_CHAN_INFO_OFFSET] = "offset",
114 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
115 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
116 [IIO_CHAN_INFO_PEAK] = "peak_raw",
117 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
118 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
119 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
Jonathan Camerondf94aba2011-11-27 11:39:12 +0000120 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
121 = "filter_low_pass_3db_frequency",
Martin Fuzzey3f7f6422015-05-13 12:26:42 +0200122 [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY]
123 = "filter_high_pass_3db_frequency",
Laxman Dewangance85a1c2012-04-13 16:03:31 +0530124 [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
Michael Hennericha6b12852012-04-27 10:58:34 +0200125 [IIO_CHAN_INFO_FREQUENCY] = "frequency",
126 [IIO_CHAN_INFO_PHASE] = "phase",
Michael Hennerichb65d6212012-05-11 11:36:53 +0200127 [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
srinivas pandruvada7c9ab032012-09-05 13:56:00 +0100128 [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
Peter Meerwald899d90b2013-09-08 16:20:00 +0100129 [IIO_CHAN_INFO_INT_TIME] = "integration_time",
Irina Tirdeaa88bfe72014-11-10 14:45:32 +0200130 [IIO_CHAN_INFO_ENABLE] = "en",
Irina Tirdeabcdf28f2014-11-10 14:45:33 +0200131 [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight",
Irina Tirdead37f6832015-01-11 21:10:10 +0200132 [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight",
Irina Tirdea2f0ecb72015-01-27 20:41:52 +0200133 [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count",
134 [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time",
Vianney le Clément de Saint-Marcqc8a85852015-03-30 10:34:58 +0200135 [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity",
Irina Tirdeafaaa4492015-04-29 21:16:39 +0300136 [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100137};
138
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000139/**
140 * iio_find_channel_from_si() - get channel from its scan index
141 * @indio_dev: device
142 * @si: scan index to match
143 */
Jonathan Cameron5fb21c82011-12-05 21:37:10 +0000144const struct iio_chan_spec
145*iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
146{
147 int i;
148
149 for (i = 0; i < indio_dev->num_channels; i++)
150 if (indio_dev->channels[i].scan_index == si)
151 return &indio_dev->channels[i];
152 return NULL;
153}
154
Jonathan Cameron847ec802009-08-18 18:06:19 +0100155/* This turns up an awful lot */
156ssize_t iio_read_const_attr(struct device *dev,
157 struct device_attribute *attr,
158 char *buf)
159{
160 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
161}
162EXPORT_SYMBOL(iio_read_const_attr);
163
Jonathan Cameron847ec802009-08-18 18:06:19 +0100164static int __init iio_init(void)
165{
166 int ret;
167
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100168 /* Register sysfs bus */
169 ret = bus_register(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100170 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100171 pr_err("could not register bus type\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +0100172 goto error_nothing;
173 }
174
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100175 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
176 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100177 pr_err("failed to allocate char dev region\n");
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100178 goto error_unregister_bus_type;
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100179 }
Jonathan Cameron847ec802009-08-18 18:06:19 +0100180
Michael Henneriche553f182012-03-01 10:51:03 +0100181 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
182
Jonathan Cameron847ec802009-08-18 18:06:19 +0100183 return 0;
184
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100185error_unregister_bus_type:
186 bus_unregister(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100187error_nothing:
188 return ret;
189}
190
191static void __exit iio_exit(void)
192{
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100193 if (iio_devt)
194 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100195 bus_unregister(&iio_bus_type);
Michael Henneriche553f182012-03-01 10:51:03 +0100196 debugfs_remove(iio_debugfs_dentry);
197}
198
199#if defined(CONFIG_DEBUG_FS)
Michael Henneriche553f182012-03-01 10:51:03 +0100200static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
201 size_t count, loff_t *ppos)
202{
203 struct iio_dev *indio_dev = file->private_data;
204 char buf[20];
205 unsigned val = 0;
206 ssize_t len;
207 int ret;
208
209 ret = indio_dev->info->debugfs_reg_access(indio_dev,
210 indio_dev->cached_reg_addr,
211 0, &val);
212 if (ret)
213 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
214
215 len = snprintf(buf, sizeof(buf), "0x%X\n", val);
216
217 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
218}
219
220static ssize_t iio_debugfs_write_reg(struct file *file,
221 const char __user *userbuf, size_t count, loff_t *ppos)
222{
223 struct iio_dev *indio_dev = file->private_data;
224 unsigned reg, val;
225 char buf[80];
226 int ret;
227
228 count = min_t(size_t, count, (sizeof(buf)-1));
229 if (copy_from_user(buf, userbuf, count))
230 return -EFAULT;
231
232 buf[count] = 0;
233
234 ret = sscanf(buf, "%i %i", &reg, &val);
235
236 switch (ret) {
237 case 1:
238 indio_dev->cached_reg_addr = reg;
239 break;
240 case 2:
241 indio_dev->cached_reg_addr = reg;
242 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
243 val, NULL);
244 if (ret) {
245 dev_err(indio_dev->dev.parent, "%s: write failed\n",
246 __func__);
247 return ret;
248 }
249 break;
250 default:
251 return -EINVAL;
252 }
253
254 return count;
255}
256
257static const struct file_operations iio_debugfs_reg_fops = {
Axel Lin5a28c872012-05-03 09:50:51 +0800258 .open = simple_open,
Michael Henneriche553f182012-03-01 10:51:03 +0100259 .read = iio_debugfs_read_reg,
260 .write = iio_debugfs_write_reg,
261};
262
263static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
264{
265 debugfs_remove_recursive(indio_dev->debugfs_dentry);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100266}
267
Michael Henneriche553f182012-03-01 10:51:03 +0100268static int iio_device_register_debugfs(struct iio_dev *indio_dev)
269{
270 struct dentry *d;
271
272 if (indio_dev->info->debugfs_reg_access == NULL)
273 return 0;
274
Axel Linabd5a2f2012-05-03 22:56:58 +0800275 if (!iio_debugfs_dentry)
Michael Henneriche553f182012-03-01 10:51:03 +0100276 return 0;
277
278 indio_dev->debugfs_dentry =
279 debugfs_create_dir(dev_name(&indio_dev->dev),
280 iio_debugfs_dentry);
Michael Henneriche553f182012-03-01 10:51:03 +0100281 if (indio_dev->debugfs_dentry == NULL) {
282 dev_warn(indio_dev->dev.parent,
283 "Failed to create debugfs directory\n");
284 return -EFAULT;
285 }
286
287 d = debugfs_create_file("direct_reg_access", 0644,
288 indio_dev->debugfs_dentry,
289 indio_dev, &iio_debugfs_reg_fops);
290 if (!d) {
291 iio_device_unregister_debugfs(indio_dev);
292 return -ENOMEM;
293 }
294
295 return 0;
296}
297#else
298static int iio_device_register_debugfs(struct iio_dev *indio_dev)
299{
300 return 0;
301}
302
303static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
304{
305}
306#endif /* CONFIG_DEBUG_FS */
307
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100308static ssize_t iio_read_channel_ext_info(struct device *dev,
309 struct device_attribute *attr,
310 char *buf)
311{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200312 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100313 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
314 const struct iio_chan_spec_ext_info *ext_info;
315
316 ext_info = &this_attr->c->ext_info[this_attr->address];
317
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200318 return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100319}
320
321static ssize_t iio_write_channel_ext_info(struct device *dev,
322 struct device_attribute *attr,
323 const char *buf,
324 size_t len)
325{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200326 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100327 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
328 const struct iio_chan_spec_ext_info *ext_info;
329
330 ext_info = &this_attr->c->ext_info[this_attr->address];
331
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200332 return ext_info->write(indio_dev, ext_info->private,
333 this_attr->c, buf, len);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100334}
335
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200336ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
337 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
338{
339 const struct iio_enum *e = (const struct iio_enum *)priv;
340 unsigned int i;
341 size_t len = 0;
342
343 if (!e->num_items)
344 return 0;
345
346 for (i = 0; i < e->num_items; ++i)
Lars-Peter Clausen74dcd432012-06-05 18:24:12 +0200347 len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200348
349 /* replace last space with a newline */
350 buf[len - 1] = '\n';
351
352 return len;
353}
354EXPORT_SYMBOL_GPL(iio_enum_available_read);
355
356ssize_t iio_enum_read(struct iio_dev *indio_dev,
357 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
358{
359 const struct iio_enum *e = (const struct iio_enum *)priv;
360 int i;
361
362 if (!e->get)
363 return -EINVAL;
364
365 i = e->get(indio_dev, chan);
366 if (i < 0)
367 return i;
368 else if (i >= e->num_items)
369 return -EINVAL;
370
Kees Cook598db582014-03-13 16:46:00 +0000371 return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200372}
373EXPORT_SYMBOL_GPL(iio_enum_read);
374
375ssize_t iio_enum_write(struct iio_dev *indio_dev,
376 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
377 size_t len)
378{
379 const struct iio_enum *e = (const struct iio_enum *)priv;
380 unsigned int i;
381 int ret;
382
383 if (!e->set)
384 return -EINVAL;
385
386 for (i = 0; i < e->num_items; i++) {
387 if (sysfs_streq(buf, e->items[i]))
388 break;
389 }
390
391 if (i == e->num_items)
392 return -EINVAL;
393
394 ret = e->set(indio_dev, chan, i);
395 return ret ? ret : len;
396}
397EXPORT_SYMBOL_GPL(iio_enum_write);
398
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100399/**
400 * iio_format_value() - Formats a IIO value into its string representation
401 * @buf: The buffer to which the formated value gets written
402 * @type: One of the IIO_VAL_... constants. This decides how the val and val2
403 * parameters are formatted.
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100404 * @vals: pointer to the values, exact meaning depends on the type parameter.
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100405 */
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100406ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100407{
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100408 unsigned long long tmp;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200409 bool scale_db = false;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100410
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100411 switch (type) {
Michael Hennerich67eedba2012-05-11 11:36:52 +0200412 case IIO_VAL_INT:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100413 return sprintf(buf, "%d\n", vals[0]);
Michael Hennerich67eedba2012-05-11 11:36:52 +0200414 case IIO_VAL_INT_PLUS_MICRO_DB:
415 scale_db = true;
416 case IIO_VAL_INT_PLUS_MICRO:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100417 if (vals[1] < 0)
418 return sprintf(buf, "-%ld.%06u%s\n", abs(vals[0]),
419 -vals[1],
Michael Hennerich67eedba2012-05-11 11:36:52 +0200420 scale_db ? " dB" : "");
Jonathan Cameron1d892712011-05-18 14:40:51 +0100421 else
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100422 return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1],
Michael Hennerich67eedba2012-05-11 11:36:52 +0200423 scale_db ? " dB" : "");
424 case IIO_VAL_INT_PLUS_NANO:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100425 if (vals[1] < 0)
426 return sprintf(buf, "-%ld.%09u\n", abs(vals[0]),
427 -vals[1]);
Michael Hennerich71646e22011-06-27 13:07:07 +0100428 else
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100429 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100430 case IIO_VAL_FRACTIONAL:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100431 tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
432 vals[1] = do_div(tmp, 1000000000LL);
433 vals[0] = tmp;
434 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
Lars-Peter Clausen103d9fb2012-10-16 17:29:00 +0100435 case IIO_VAL_FRACTIONAL_LOG2:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100436 tmp = (s64)vals[0] * 1000000000LL >> vals[1];
437 vals[1] = do_div(tmp, 1000000000LL);
438 vals[0] = tmp;
439 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
440 case IIO_VAL_INT_MULTIPLE:
441 {
442 int i;
443 int len = 0;
444
445 for (i = 0; i < size; ++i)
446 len += snprintf(&buf[len], PAGE_SIZE - len, "%d ",
447 vals[i]);
448 len += snprintf(&buf[len], PAGE_SIZE - len, "\n");
449 return len;
450 }
Michael Hennerich67eedba2012-05-11 11:36:52 +0200451 default:
Jonathan Cameron1d892712011-05-18 14:40:51 +0100452 return 0;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200453 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100454}
455
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100456static ssize_t iio_read_channel_info(struct device *dev,
457 struct device_attribute *attr,
458 char *buf)
459{
460 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
461 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100462 int vals[INDIO_MAX_RAW_ELEMENTS];
463 int ret;
464 int val_len = 2;
465
466 if (indio_dev->info->read_raw_multi)
467 ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
468 INDIO_MAX_RAW_ELEMENTS,
469 vals, &val_len,
470 this_attr->address);
471 else
472 ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
473 &vals[0], &vals[1], this_attr->address);
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100474
475 if (ret < 0)
476 return ret;
477
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100478 return iio_format_value(buf, ret, val_len, vals);
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100479}
480
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000481/**
482 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
483 * @str: The string to parse
484 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
485 * @integer: The integer part of the number
486 * @fract: The fractional part of the number
487 *
488 * Returns 0 on success, or a negative error code if the string could not be
489 * parsed.
490 */
491int iio_str_to_fixpoint(const char *str, int fract_mult,
492 int *integer, int *fract)
493{
494 int i = 0, f = 0;
495 bool integer_part = true, negative = false;
496
497 if (str[0] == '-') {
498 negative = true;
499 str++;
500 } else if (str[0] == '+') {
501 str++;
502 }
503
504 while (*str) {
505 if ('0' <= *str && *str <= '9') {
506 if (integer_part) {
507 i = i * 10 + *str - '0';
508 } else {
509 f += fract_mult * (*str - '0');
510 fract_mult /= 10;
511 }
512 } else if (*str == '\n') {
513 if (*(str + 1) == '\0')
514 break;
515 else
516 return -EINVAL;
517 } else if (*str == '.' && integer_part) {
518 integer_part = false;
519 } else {
520 return -EINVAL;
521 }
522 str++;
523 }
524
525 if (negative) {
526 if (i)
527 i = -i;
528 else
529 f = -f;
530 }
531
532 *integer = i;
533 *fract = f;
534
535 return 0;
536}
537EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
538
Jonathan Cameron1d892712011-05-18 14:40:51 +0100539static ssize_t iio_write_channel_info(struct device *dev,
540 struct device_attribute *attr,
541 const char *buf,
542 size_t len)
543{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200544 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100545 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000546 int ret, fract_mult = 100000;
547 int integer, fract;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100548
549 /* Assumes decimal - precision based on number of digits */
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100550 if (!indio_dev->info->write_raw)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100551 return -EINVAL;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100552
553 if (indio_dev->info->write_raw_get_fmt)
554 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
555 this_attr->c, this_attr->address)) {
556 case IIO_VAL_INT_PLUS_MICRO:
557 fract_mult = 100000;
558 break;
559 case IIO_VAL_INT_PLUS_NANO:
560 fract_mult = 100000000;
561 break;
562 default:
563 return -EINVAL;
564 }
565
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000566 ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
567 if (ret)
568 return ret;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100569
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100570 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
Michael Hennerich5c04af02011-06-27 13:07:10 +0100571 integer, fract, this_attr->address);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100572 if (ret)
573 return ret;
574
575 return len;
576}
577
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100578static
Jonathan Cameron1d892712011-05-18 14:40:51 +0100579int __iio_device_attr_init(struct device_attribute *dev_attr,
580 const char *postfix,
581 struct iio_chan_spec const *chan,
582 ssize_t (*readfunc)(struct device *dev,
583 struct device_attribute *attr,
584 char *buf),
585 ssize_t (*writefunc)(struct device *dev,
586 struct device_attribute *attr,
587 const char *buf,
588 size_t len),
Jonathan Cameron37044322013-09-08 14:57:00 +0100589 enum iio_shared_by shared_by)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100590{
Jonathan Cameron37044322013-09-08 14:57:00 +0100591 int ret = 0;
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000592 char *name = NULL;
Jonathan Cameron37044322013-09-08 14:57:00 +0100593 char *full_postfix;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100594 sysfs_attr_init(&dev_attr->attr);
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100595
596 /* Build up postfix of <extend_name>_<modifier>_postfix */
Jonathan Cameron37044322013-09-08 14:57:00 +0100597 if (chan->modified && (shared_by == IIO_SEPARATE)) {
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100598 if (chan->extend_name)
599 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
600 iio_modifier_names[chan
601 ->channel2],
602 chan->extend_name,
603 postfix);
604 else
605 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
606 iio_modifier_names[chan
607 ->channel2],
608 postfix);
609 } else {
Lars-Peter Clausen77bfa8b2014-02-14 14:19:00 +0000610 if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100611 full_postfix = kstrdup(postfix, GFP_KERNEL);
612 else
613 full_postfix = kasprintf(GFP_KERNEL,
614 "%s_%s",
615 chan->extend_name,
616 postfix);
617 }
Jonathan Cameron37044322013-09-08 14:57:00 +0100618 if (full_postfix == NULL)
619 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100620
Justin P. Mattock4abf6f82012-02-29 22:00:38 -0800621 if (chan->differential) { /* Differential can not have modifier */
Jonathan Cameron37044322013-09-08 14:57:00 +0100622 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100623 case IIO_SHARED_BY_ALL:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000624 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100625 break;
626 case IIO_SHARED_BY_DIR:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000627 name = kasprintf(GFP_KERNEL, "%s_%s",
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100628 iio_direction[chan->output],
629 full_postfix);
630 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100631 case IIO_SHARED_BY_TYPE:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000632 name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100633 iio_direction[chan->output],
634 iio_chan_type_name_spec[chan->type],
635 iio_chan_type_name_spec[chan->type],
636 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100637 break;
638 case IIO_SEPARATE:
639 if (!chan->indexed) {
640 WARN_ON("Differential channels must be indexed\n");
641 ret = -EINVAL;
642 goto error_free_full_postfix;
643 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000644 name = kasprintf(GFP_KERNEL,
Jonathan Cameron37044322013-09-08 14:57:00 +0100645 "%s_%s%d-%s%d_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100646 iio_direction[chan->output],
647 iio_chan_type_name_spec[chan->type],
648 chan->channel,
649 iio_chan_type_name_spec[chan->type],
650 chan->channel2,
651 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100652 break;
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100653 }
654 } else { /* Single ended */
Jonathan Cameron37044322013-09-08 14:57:00 +0100655 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100656 case IIO_SHARED_BY_ALL:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000657 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100658 break;
659 case IIO_SHARED_BY_DIR:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000660 name = kasprintf(GFP_KERNEL, "%s_%s",
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100661 iio_direction[chan->output],
662 full_postfix);
663 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100664 case IIO_SHARED_BY_TYPE:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000665 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100666 iio_direction[chan->output],
667 iio_chan_type_name_spec[chan->type],
668 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100669 break;
670
671 case IIO_SEPARATE:
672 if (chan->indexed)
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000673 name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
Jonathan Cameron37044322013-09-08 14:57:00 +0100674 iio_direction[chan->output],
675 iio_chan_type_name_spec[chan->type],
676 chan->channel,
677 full_postfix);
678 else
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000679 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
Jonathan Cameron37044322013-09-08 14:57:00 +0100680 iio_direction[chan->output],
681 iio_chan_type_name_spec[chan->type],
682 full_postfix);
683 break;
684 }
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100685 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000686 if (name == NULL) {
Jonathan Cameron1d892712011-05-18 14:40:51 +0100687 ret = -ENOMEM;
688 goto error_free_full_postfix;
689 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000690 dev_attr->attr.name = name;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100691
692 if (readfunc) {
693 dev_attr->attr.mode |= S_IRUGO;
694 dev_attr->show = readfunc;
695 }
696
697 if (writefunc) {
698 dev_attr->attr.mode |= S_IWUSR;
699 dev_attr->store = writefunc;
700 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000701
Jonathan Cameron1d892712011-05-18 14:40:51 +0100702error_free_full_postfix:
703 kfree(full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100704
Jonathan Cameron847ec802009-08-18 18:06:19 +0100705 return ret;
706}
707
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100708static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100709{
710 kfree(dev_attr->attr.name);
711}
712
713int __iio_add_chan_devattr(const char *postfix,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100714 struct iio_chan_spec const *chan,
715 ssize_t (*readfunc)(struct device *dev,
716 struct device_attribute *attr,
717 char *buf),
718 ssize_t (*writefunc)(struct device *dev,
719 struct device_attribute *attr,
720 const char *buf,
721 size_t len),
Jonathan Camerone614a542011-09-02 17:14:41 +0100722 u64 mask,
Jonathan Cameron37044322013-09-08 14:57:00 +0100723 enum iio_shared_by shared_by,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100724 struct device *dev,
725 struct list_head *attr_list)
726{
727 int ret;
728 struct iio_dev_attr *iio_attr, *t;
729
Sachin Kamat670c1102013-10-24 12:53:00 +0100730 iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000731 if (iio_attr == NULL)
732 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100733 ret = __iio_device_attr_init(&iio_attr->dev_attr,
734 postfix, chan,
Jonathan Cameron37044322013-09-08 14:57:00 +0100735 readfunc, writefunc, shared_by);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100736 if (ret)
737 goto error_iio_dev_attr_free;
738 iio_attr->c = chan;
739 iio_attr->address = mask;
740 list_for_each_entry(t, attr_list, l)
741 if (strcmp(t->dev_attr.attr.name,
742 iio_attr->dev_attr.attr.name) == 0) {
Jonathan Cameron37044322013-09-08 14:57:00 +0100743 if (shared_by == IIO_SEPARATE)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100744 dev_err(dev, "tried to double register : %s\n",
745 t->dev_attr.attr.name);
746 ret = -EBUSY;
747 goto error_device_attr_deinit;
748 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100749 list_add(&iio_attr->l, attr_list);
750
751 return 0;
752
753error_device_attr_deinit:
754 __iio_device_attr_deinit(&iio_attr->dev_attr);
755error_iio_dev_attr_free:
756 kfree(iio_attr);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100757 return ret;
758}
759
Jonathan Cameron37044322013-09-08 14:57:00 +0100760static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
761 struct iio_chan_spec const *chan,
762 enum iio_shared_by shared_by,
763 const long *infomask)
764{
765 int i, ret, attrcount = 0;
766
767 for_each_set_bit(i, infomask, sizeof(infomask)*8) {
Jonathan Cameronef4b4852014-01-03 22:24:00 +0000768 if (i >= ARRAY_SIZE(iio_chan_info_postfix))
769 return -EINVAL;
Jonathan Cameron37044322013-09-08 14:57:00 +0100770 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
771 chan,
772 &iio_read_channel_info,
773 &iio_write_channel_info,
774 i,
775 shared_by,
776 &indio_dev->dev,
777 &indio_dev->channel_attr_list);
778 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
779 continue;
780 else if (ret < 0)
781 return ret;
782 attrcount++;
783 }
784
785 return attrcount;
786}
787
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100788static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100789 struct iio_chan_spec const *chan)
790{
Jonathan Cameron5ccb3ad2012-04-15 17:41:16 +0100791 int ret, attrcount = 0;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100792 const struct iio_chan_spec_ext_info *ext_info;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100793
Jonathan Cameron1d892712011-05-18 14:40:51 +0100794 if (chan->channel < 0)
795 return 0;
Jonathan Cameron37044322013-09-08 14:57:00 +0100796 ret = iio_device_add_info_mask_type(indio_dev, chan,
797 IIO_SEPARATE,
798 &chan->info_mask_separate);
799 if (ret < 0)
800 return ret;
801 attrcount += ret;
802
803 ret = iio_device_add_info_mask_type(indio_dev, chan,
804 IIO_SHARED_BY_TYPE,
805 &chan->info_mask_shared_by_type);
806 if (ret < 0)
807 return ret;
808 attrcount += ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100809
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100810 ret = iio_device_add_info_mask_type(indio_dev, chan,
811 IIO_SHARED_BY_DIR,
812 &chan->info_mask_shared_by_dir);
813 if (ret < 0)
814 return ret;
815 attrcount += ret;
816
817 ret = iio_device_add_info_mask_type(indio_dev, chan,
818 IIO_SHARED_BY_ALL,
819 &chan->info_mask_shared_by_all);
820 if (ret < 0)
821 return ret;
822 attrcount += ret;
823
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100824 if (chan->ext_info) {
825 unsigned int i = 0;
826 for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
827 ret = __iio_add_chan_devattr(ext_info->name,
828 chan,
829 ext_info->read ?
830 &iio_read_channel_ext_info : NULL,
831 ext_info->write ?
832 &iio_write_channel_ext_info : NULL,
833 i,
834 ext_info->shared,
835 &indio_dev->dev,
836 &indio_dev->channel_attr_list);
837 i++;
838 if (ret == -EBUSY && ext_info->shared)
839 continue;
840
841 if (ret)
Jonathan Cameron37044322013-09-08 14:57:00 +0100842 return ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100843
844 attrcount++;
845 }
846 }
847
Jonathan Cameron37044322013-09-08 14:57:00 +0100848 return attrcount;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100849}
850
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100851/**
852 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
853 * @attr_list: List of IIO device attributes
854 *
855 * This function frees the memory allocated for each of the IIO device
Martin Fuzzeyc1b03ab2015-02-19 15:17:44 +0100856 * attributes in the list.
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100857 */
858void iio_free_chan_devattr_list(struct list_head *attr_list)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100859{
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100860 struct iio_dev_attr *p, *n;
861
862 list_for_each_entry_safe(p, n, attr_list, l) {
863 kfree(p->dev_attr.attr.name);
Martin Fuzzeyc1b03ab2015-02-19 15:17:44 +0100864 list_del(&p->l);
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100865 kfree(p);
866 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100867}
868
Jonathan Cameron1b732882011-05-18 14:41:43 +0100869static ssize_t iio_show_dev_name(struct device *dev,
870 struct device_attribute *attr,
871 char *buf)
872{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200873 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Kees Cook598db582014-03-13 16:46:00 +0000874 return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name);
Jonathan Cameron1b732882011-05-18 14:41:43 +0100875}
876
877static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
878
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100879static int iio_device_register_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100880{
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100881 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100882 struct iio_dev_attr *p;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100883 struct attribute **attr;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100884
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100885 /* First count elements in any existing group */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100886 if (indio_dev->info->attrs) {
887 attr = indio_dev->info->attrs->attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100888 while (*attr++ != NULL)
889 attrcount_orig++;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100890 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100891 attrcount = attrcount_orig;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100892 /*
893 * New channel registration method - relies on the fact a group does
Peter Meerwaldd25b3802012-08-26 13:43:00 +0100894 * not need to be initialized if its name is NULL.
Jonathan Cameron1d892712011-05-18 14:40:51 +0100895 */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100896 if (indio_dev->channels)
897 for (i = 0; i < indio_dev->num_channels; i++) {
898 ret = iio_device_add_channel_sysfs(indio_dev,
899 &indio_dev
Jonathan Cameron1d892712011-05-18 14:40:51 +0100900 ->channels[i]);
901 if (ret < 0)
902 goto error_clear_attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100903 attrcount += ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100904 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100905
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100906 if (indio_dev->name)
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100907 attrcount++;
908
Thomas Meyerd83fb182011-11-29 22:08:00 +0100909 indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
910 sizeof(indio_dev->chan_attr_group.attrs[0]),
911 GFP_KERNEL);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100912 if (indio_dev->chan_attr_group.attrs == NULL) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100913 ret = -ENOMEM;
914 goto error_clear_attrs;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100915 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100916 /* Copy across original attributes */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100917 if (indio_dev->info->attrs)
918 memcpy(indio_dev->chan_attr_group.attrs,
919 indio_dev->info->attrs->attrs,
920 sizeof(indio_dev->chan_attr_group.attrs[0])
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100921 *attrcount_orig);
922 attrn = attrcount_orig;
923 /* Add all elements from the list. */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100924 list_for_each_entry(p, &indio_dev->channel_attr_list, l)
925 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
926 if (indio_dev->name)
927 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100928
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100929 indio_dev->groups[indio_dev->groupcounter++] =
930 &indio_dev->chan_attr_group;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100931
Jonathan Cameron1d892712011-05-18 14:40:51 +0100932 return 0;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100933
Jonathan Cameron1d892712011-05-18 14:40:51 +0100934error_clear_attrs:
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100935 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100936
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100937 return ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100938}
939
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100940static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100941{
Jonathan Cameron1d892712011-05-18 14:40:51 +0100942
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100943 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100944 kfree(indio_dev->chan_attr_group.attrs);
Martin Fuzzeyc1b03ab2015-02-19 15:17:44 +0100945 indio_dev->chan_attr_group.attrs = NULL;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100946}
947
Jonathan Cameron847ec802009-08-18 18:06:19 +0100948static void iio_dev_release(struct device *device)
949{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200950 struct iio_dev *indio_dev = dev_to_iio_dev(device);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100951 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
952 iio_device_unregister_trigger_consumer(indio_dev);
953 iio_device_unregister_eventset(indio_dev);
954 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200955
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100956 iio_buffer_put(indio_dev->buffer);
957
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200958 ida_simple_remove(&iio_ida, indio_dev->id);
959 kfree(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100960}
961
Guenter Roeck17d82b42013-02-07 17:09:00 +0000962struct device_type iio_device_type = {
Jonathan Cameron847ec802009-08-18 18:06:19 +0100963 .name = "iio_device",
964 .release = iio_dev_release,
965};
966
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000967/**
968 * iio_device_alloc() - allocate an iio_dev from a driver
969 * @sizeof_priv: Space to allocate for private structure.
970 **/
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200971struct iio_dev *iio_device_alloc(int sizeof_priv)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100972{
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +0100973 struct iio_dev *dev;
974 size_t alloc_size;
975
976 alloc_size = sizeof(struct iio_dev);
977 if (sizeof_priv) {
978 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
979 alloc_size += sizeof_priv;
980 }
981 /* ensure 32-byte alignment of whole construct ? */
982 alloc_size += IIO_ALIGN - 1;
983
984 dev = kzalloc(alloc_size, GFP_KERNEL);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100985
986 if (dev) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100987 dev->dev.groups = dev->groups;
Guenter Roeck17d82b42013-02-07 17:09:00 +0000988 dev->dev.type = &iio_device_type;
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100989 dev->dev.bus = &iio_bus_type;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100990 device_initialize(&dev->dev);
991 dev_set_drvdata(&dev->dev, (void *)dev);
992 mutex_init(&dev->mlock);
Jonathan Cameronac917a82012-02-15 19:48:00 +0000993 mutex_init(&dev->info_exist_lock);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200994 INIT_LIST_HEAD(&dev->channel_attr_list);
Jonathan Camerona9e39f92011-09-14 13:01:25 +0100995
996 dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
997 if (dev->id < 0) {
998 /* cannot use a dev_err as the name isn't available */
Sachin Kamat3176dd52013-10-24 12:53:00 +0100999 pr_err("failed to get device id\n");
Jonathan Camerona9e39f92011-09-14 13:01:25 +01001000 kfree(dev);
1001 return NULL;
1002 }
1003 dev_set_name(&dev->dev, "iio:device%d", dev->id);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001004 INIT_LIST_HEAD(&dev->buffer_list);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001005 }
1006
1007 return dev;
1008}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001009EXPORT_SYMBOL(iio_device_alloc);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001010
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001011/**
1012 * iio_device_free() - free an iio_dev from a driver
1013 * @dev: the iio_dev associated with the device
1014 **/
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001015void iio_device_free(struct iio_dev *dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001016{
Lars-Peter Clausene407fd62012-06-04 10:41:42 +02001017 if (dev)
1018 put_device(&dev->dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001019}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001020EXPORT_SYMBOL(iio_device_free);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001021
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001022static void devm_iio_device_release(struct device *dev, void *res)
1023{
1024 iio_device_free(*(struct iio_dev **)res);
1025}
1026
1027static int devm_iio_device_match(struct device *dev, void *res, void *data)
1028{
1029 struct iio_dev **r = res;
1030 if (!r || !*r) {
1031 WARN_ON(!r || !*r);
1032 return 0;
1033 }
1034 return *r == data;
1035}
1036
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001037/**
1038 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1039 * @dev: Device to allocate iio_dev for
1040 * @sizeof_priv: Space to allocate for private structure.
1041 *
1042 * Managed iio_device_alloc. iio_dev allocated with this function is
1043 * automatically freed on driver detach.
1044 *
1045 * If an iio_dev allocated with this function needs to be freed separately,
1046 * devm_iio_device_free() must be used.
1047 *
1048 * RETURNS:
1049 * Pointer to allocated iio_dev on success, NULL on failure.
1050 */
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001051struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
1052{
1053 struct iio_dev **ptr, *iio_dev;
1054
1055 ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
1056 GFP_KERNEL);
1057 if (!ptr)
1058 return NULL;
1059
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001060 iio_dev = iio_device_alloc(sizeof_priv);
1061 if (iio_dev) {
1062 *ptr = iio_dev;
1063 devres_add(dev, ptr);
1064 } else {
1065 devres_free(ptr);
1066 }
1067
1068 return iio_dev;
1069}
1070EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
1071
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001072/**
1073 * devm_iio_device_free - Resource-managed iio_device_free()
1074 * @dev: Device this iio_dev belongs to
1075 * @iio_dev: the iio_dev associated with the device
1076 *
1077 * Free iio_dev allocated with devm_iio_device_alloc().
1078 */
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001079void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
1080{
1081 int rc;
1082
1083 rc = devres_release(dev, devm_iio_device_release,
1084 devm_iio_device_match, iio_dev);
1085 WARN_ON(rc);
1086}
1087EXPORT_SYMBOL_GPL(devm_iio_device_free);
1088
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001089/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001090 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001091 **/
1092static int iio_chrdev_open(struct inode *inode, struct file *filp)
1093{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001094 struct iio_dev *indio_dev = container_of(inode->i_cdev,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001095 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001096
1097 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
1098 return -EBUSY;
1099
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001100 iio_device_get(indio_dev);
1101
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001102 filp->private_data = indio_dev;
Jonathan Cameron30eb82f2011-09-21 11:16:02 +01001103
Lars-Peter Clausen79335142011-12-19 15:23:49 +01001104 return 0;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001105}
1106
1107/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001108 * iio_chrdev_release() - chrdev file close buffer access and ioctls
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001109 **/
1110static int iio_chrdev_release(struct inode *inode, struct file *filp)
1111{
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001112 struct iio_dev *indio_dev = container_of(inode->i_cdev,
1113 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001114 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001115 iio_device_put(indio_dev);
1116
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001117 return 0;
1118}
1119
1120/* Somewhat of a cross file organization violation - ioctls here are actually
1121 * event related */
1122static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1123{
1124 struct iio_dev *indio_dev = filp->private_data;
1125 int __user *ip = (int __user *)arg;
1126 int fd;
1127
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +01001128 if (!indio_dev->info)
1129 return -ENODEV;
1130
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001131 if (cmd == IIO_GET_EVENT_FD_IOCTL) {
1132 fd = iio_event_getfd(indio_dev);
1133 if (copy_to_user(ip, &fd, sizeof(fd)))
1134 return -EFAULT;
1135 return 0;
1136 }
1137 return -EINVAL;
1138}
1139
Jonathan Cameron14555b12011-09-21 11:15:57 +01001140static const struct file_operations iio_buffer_fileops = {
1141 .read = iio_buffer_read_first_n_outer_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001142 .release = iio_chrdev_release,
1143 .open = iio_chrdev_open,
Jonathan Cameron14555b12011-09-21 11:15:57 +01001144 .poll = iio_buffer_poll_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001145 .owner = THIS_MODULE,
1146 .llseek = noop_llseek,
1147 .unlocked_ioctl = iio_ioctl,
1148 .compat_ioctl = iio_ioctl,
1149};
1150
Vlad Dogaru8f5d8722014-12-29 11:37:48 +02001151static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
1152{
1153 int i, j;
1154 const struct iio_chan_spec *channels = indio_dev->channels;
1155
1156 if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
1157 return 0;
1158
1159 for (i = 0; i < indio_dev->num_channels - 1; i++) {
1160 if (channels[i].scan_index < 0)
1161 continue;
1162 for (j = i + 1; j < indio_dev->num_channels; j++)
1163 if (channels[i].scan_index == channels[j].scan_index) {
1164 dev_err(&indio_dev->dev,
1165 "Duplicate scan index %d\n",
1166 channels[i].scan_index);
1167 return -EINVAL;
1168 }
1169 }
1170
1171 return 0;
1172}
1173
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001174static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1175
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001176/**
1177 * iio_device_register() - register a device with the IIO subsystem
1178 * @indio_dev: Device structure filled by the device driver
1179 **/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001180int iio_device_register(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001181{
1182 int ret;
1183
Guenter Roeck17d82b42013-02-07 17:09:00 +00001184 /* If the calling driver did not initialize of_node, do it here */
1185 if (!indio_dev->dev.of_node && indio_dev->dev.parent)
1186 indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
1187
Vlad Dogaru8f5d8722014-12-29 11:37:48 +02001188 ret = iio_check_unique_scan_index(indio_dev);
1189 if (ret < 0)
1190 return ret;
1191
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001192 /* configure elements for the chrdev */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001193 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001194
Michael Henneriche553f182012-03-01 10:51:03 +01001195 ret = iio_device_register_debugfs(indio_dev);
1196 if (ret) {
1197 dev_err(indio_dev->dev.parent,
1198 "Failed to register debugfs interfaces\n");
Hartmut Knaack92825ff2014-02-16 11:53:00 +00001199 return ret;
Michael Henneriche553f182012-03-01 10:51:03 +01001200 }
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001201
1202 ret = iio_buffer_alloc_sysfs_and_mask(indio_dev);
1203 if (ret) {
1204 dev_err(indio_dev->dev.parent,
1205 "Failed to create buffer sysfs interfaces\n");
1206 goto error_unreg_debugfs;
1207 }
1208
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001209 ret = iio_device_register_sysfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001210 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001211 dev_err(indio_dev->dev.parent,
Jonathan Cameron847ec802009-08-18 18:06:19 +01001212 "Failed to register sysfs interfaces\n");
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001213 goto error_buffer_free_sysfs;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001214 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001215 ret = iio_device_register_eventset(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001216 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001217 dev_err(indio_dev->dev.parent,
Roel Van Nyenc849d252010-04-29 19:27:31 +02001218 "Failed to register event set\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001219 goto error_free_sysfs;
1220 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001221 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
1222 iio_device_register_trigger_consumer(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001223
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001224 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1225 indio_dev->setup_ops == NULL)
1226 indio_dev->setup_ops = &noop_ring_setup_ops;
1227
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001228 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
1229 indio_dev->chrdev.owner = indio_dev->info->driver_module;
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001230 indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001231 ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001232 if (ret < 0)
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001233 goto error_unreg_eventset;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001234
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001235 ret = device_add(&indio_dev->dev);
1236 if (ret < 0)
1237 goto error_cdev_del;
1238
1239 return 0;
1240error_cdev_del:
1241 cdev_del(&indio_dev->chrdev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001242error_unreg_eventset:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001243 iio_device_unregister_eventset(indio_dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001244error_free_sysfs:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001245 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001246error_buffer_free_sysfs:
1247 iio_buffer_free_sysfs_and_mask(indio_dev);
Michael Henneriche553f182012-03-01 10:51:03 +01001248error_unreg_debugfs:
1249 iio_device_unregister_debugfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001250 return ret;
1251}
1252EXPORT_SYMBOL(iio_device_register);
1253
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001254/**
1255 * iio_device_unregister() - unregister a device from the IIO subsystem
1256 * @indio_dev: Device structure representing the device.
1257 **/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001258void iio_device_unregister(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001259{
Jonathan Cameronac917a82012-02-15 19:48:00 +00001260 mutex_lock(&indio_dev->info_exist_lock);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001261
1262 device_del(&indio_dev->dev);
1263
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001264 if (indio_dev->chrdev.dev)
1265 cdev_del(&indio_dev->chrdev);
Lars-Peter Clausenbc4c9612013-09-21 16:21:00 +01001266 iio_device_unregister_debugfs(indio_dev);
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001267
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001268 iio_disable_all_buffers(indio_dev);
1269
Jonathan Cameronac917a82012-02-15 19:48:00 +00001270 indio_dev->info = NULL;
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +01001271
1272 iio_device_wakeup_eventset(indio_dev);
1273 iio_buffer_wakeup_poll(indio_dev);
1274
Jonathan Cameronac917a82012-02-15 19:48:00 +00001275 mutex_unlock(&indio_dev->info_exist_lock);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001276
1277 iio_buffer_free_sysfs_and_mask(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001278}
1279EXPORT_SYMBOL(iio_device_unregister);
Sachin Kamat8caa07c2013-10-29 11:39:00 +00001280
1281static void devm_iio_device_unreg(struct device *dev, void *res)
1282{
1283 iio_device_unregister(*(struct iio_dev **)res);
1284}
1285
1286/**
1287 * devm_iio_device_register - Resource-managed iio_device_register()
1288 * @dev: Device to allocate iio_dev for
1289 * @indio_dev: Device structure filled by the device driver
1290 *
1291 * Managed iio_device_register. The IIO device registered with this
1292 * function is automatically unregistered on driver detach. This function
1293 * calls iio_device_register() internally. Refer to that function for more
1294 * information.
1295 *
1296 * If an iio_dev registered with this function needs to be unregistered
1297 * separately, devm_iio_device_unregister() must be used.
1298 *
1299 * RETURNS:
1300 * 0 on success, negative error number on failure.
1301 */
1302int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev)
1303{
1304 struct iio_dev **ptr;
1305 int ret;
1306
1307 ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL);
1308 if (!ptr)
1309 return -ENOMEM;
1310
1311 *ptr = indio_dev;
1312 ret = iio_device_register(indio_dev);
1313 if (!ret)
1314 devres_add(dev, ptr);
1315 else
1316 devres_free(ptr);
1317
1318 return ret;
1319}
1320EXPORT_SYMBOL_GPL(devm_iio_device_register);
1321
1322/**
1323 * devm_iio_device_unregister - Resource-managed iio_device_unregister()
1324 * @dev: Device this iio_dev belongs to
1325 * @indio_dev: the iio_dev associated with the device
1326 *
1327 * Unregister iio_dev registered with devm_iio_device_register().
1328 */
1329void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev)
1330{
1331 int rc;
1332
1333 rc = devres_release(dev, devm_iio_device_unreg,
1334 devm_iio_device_match, indio_dev);
1335 WARN_ON(rc);
1336}
1337EXPORT_SYMBOL_GPL(devm_iio_device_unregister);
1338
Jonathan Cameron847ec802009-08-18 18:06:19 +01001339subsys_initcall(iio_init);
1340module_exit(iio_exit);
1341
Jonathan Cameronc8b95952012-09-02 21:27:56 +01001342MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001343MODULE_DESCRIPTION("Industrial I/O core");
1344MODULE_LICENSE("GPL");