blob: 4b1f375c5659d0e11330d8eba6da959a398fa4d5 [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",
Jonathan Cameron1d892712011-05-18 14:40:51 +010073};
74
Jonathan Cameron330c6c52011-09-02 17:14:39 +010075static const char * const iio_modifier_names[] = {
Jonathan Cameron1d892712011-05-18 14:40:51 +010076 [IIO_MOD_X] = "x",
77 [IIO_MOD_Y] = "y",
78 [IIO_MOD_Z] = "z",
Jonathan Cameron8f5879b2012-05-05 10:39:22 +010079 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
Jonathan Cameroncf82cb82012-05-05 10:56:41 +010080 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
Jonathan Cameron330c6c52011-09-02 17:14:39 +010081 [IIO_MOD_LIGHT_BOTH] = "both",
82 [IIO_MOD_LIGHT_IR] = "ir",
Jon Brenner21cd1fa2012-05-16 10:46:42 -050083 [IIO_MOD_LIGHT_CLEAR] = "clear",
84 [IIO_MOD_LIGHT_RED] = "red",
85 [IIO_MOD_LIGHT_GREEN] = "green",
86 [IIO_MOD_LIGHT_BLUE] = "blue",
Srinivas Pandruvada5082f402014-04-29 00:51:00 +010087 [IIO_MOD_QUATERNION] = "quaternion",
Peter Meerwald638b43b2014-02-05 16:57:00 +000088 [IIO_MOD_TEMP_AMBIENT] = "ambient",
89 [IIO_MOD_TEMP_OBJECT] = "object",
Jonathan Cameron1d892712011-05-18 14:40:51 +010090};
91
92/* relies on pairs of these shared then separate */
93static const char * const iio_chan_info_postfix[] = {
Jonathan Cameron75a973c2012-04-15 17:41:30 +010094 [IIO_CHAN_INFO_RAW] = "raw",
95 [IIO_CHAN_INFO_PROCESSED] = "input",
Jonathan Cameronc8a9f802011-10-26 17:41:36 +010096 [IIO_CHAN_INFO_SCALE] = "scale",
97 [IIO_CHAN_INFO_OFFSET] = "offset",
98 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
99 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
100 [IIO_CHAN_INFO_PEAK] = "peak_raw",
101 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
102 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
103 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
Jonathan Camerondf94aba2011-11-27 11:39:12 +0000104 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
105 = "filter_low_pass_3db_frequency",
Laxman Dewangance85a1c2012-04-13 16:03:31 +0530106 [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
Michael Hennericha6b12852012-04-27 10:58:34 +0200107 [IIO_CHAN_INFO_FREQUENCY] = "frequency",
108 [IIO_CHAN_INFO_PHASE] = "phase",
Michael Hennerichb65d6212012-05-11 11:36:53 +0200109 [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
srinivas pandruvada7c9ab032012-09-05 13:56:00 +0100110 [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
Peter Meerwald899d90b2013-09-08 16:20:00 +0100111 [IIO_CHAN_INFO_INT_TIME] = "integration_time",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100112};
113
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000114/**
115 * iio_find_channel_from_si() - get channel from its scan index
116 * @indio_dev: device
117 * @si: scan index to match
118 */
Jonathan Cameron5fb21c82011-12-05 21:37:10 +0000119const struct iio_chan_spec
120*iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
121{
122 int i;
123
124 for (i = 0; i < indio_dev->num_channels; i++)
125 if (indio_dev->channels[i].scan_index == si)
126 return &indio_dev->channels[i];
127 return NULL;
128}
129
Jonathan Cameron847ec802009-08-18 18:06:19 +0100130/* This turns up an awful lot */
131ssize_t iio_read_const_attr(struct device *dev,
132 struct device_attribute *attr,
133 char *buf)
134{
135 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
136}
137EXPORT_SYMBOL(iio_read_const_attr);
138
Jonathan Cameron847ec802009-08-18 18:06:19 +0100139static int __init iio_init(void)
140{
141 int ret;
142
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100143 /* Register sysfs bus */
144 ret = bus_register(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100145 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100146 pr_err("could not register bus type\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +0100147 goto error_nothing;
148 }
149
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100150 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
151 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100152 pr_err("failed to allocate char dev region\n");
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100153 goto error_unregister_bus_type;
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100154 }
Jonathan Cameron847ec802009-08-18 18:06:19 +0100155
Michael Henneriche553f182012-03-01 10:51:03 +0100156 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
157
Jonathan Cameron847ec802009-08-18 18:06:19 +0100158 return 0;
159
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100160error_unregister_bus_type:
161 bus_unregister(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100162error_nothing:
163 return ret;
164}
165
166static void __exit iio_exit(void)
167{
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100168 if (iio_devt)
169 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100170 bus_unregister(&iio_bus_type);
Michael Henneriche553f182012-03-01 10:51:03 +0100171 debugfs_remove(iio_debugfs_dentry);
172}
173
174#if defined(CONFIG_DEBUG_FS)
Michael Henneriche553f182012-03-01 10:51:03 +0100175static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
176 size_t count, loff_t *ppos)
177{
178 struct iio_dev *indio_dev = file->private_data;
179 char buf[20];
180 unsigned val = 0;
181 ssize_t len;
182 int ret;
183
184 ret = indio_dev->info->debugfs_reg_access(indio_dev,
185 indio_dev->cached_reg_addr,
186 0, &val);
187 if (ret)
188 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
189
190 len = snprintf(buf, sizeof(buf), "0x%X\n", val);
191
192 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
193}
194
195static ssize_t iio_debugfs_write_reg(struct file *file,
196 const char __user *userbuf, size_t count, loff_t *ppos)
197{
198 struct iio_dev *indio_dev = file->private_data;
199 unsigned reg, val;
200 char buf[80];
201 int ret;
202
203 count = min_t(size_t, count, (sizeof(buf)-1));
204 if (copy_from_user(buf, userbuf, count))
205 return -EFAULT;
206
207 buf[count] = 0;
208
209 ret = sscanf(buf, "%i %i", &reg, &val);
210
211 switch (ret) {
212 case 1:
213 indio_dev->cached_reg_addr = reg;
214 break;
215 case 2:
216 indio_dev->cached_reg_addr = reg;
217 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
218 val, NULL);
219 if (ret) {
220 dev_err(indio_dev->dev.parent, "%s: write failed\n",
221 __func__);
222 return ret;
223 }
224 break;
225 default:
226 return -EINVAL;
227 }
228
229 return count;
230}
231
232static const struct file_operations iio_debugfs_reg_fops = {
Axel Lin5a28c872012-05-03 09:50:51 +0800233 .open = simple_open,
Michael Henneriche553f182012-03-01 10:51:03 +0100234 .read = iio_debugfs_read_reg,
235 .write = iio_debugfs_write_reg,
236};
237
238static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
239{
240 debugfs_remove_recursive(indio_dev->debugfs_dentry);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100241}
242
Michael Henneriche553f182012-03-01 10:51:03 +0100243static int iio_device_register_debugfs(struct iio_dev *indio_dev)
244{
245 struct dentry *d;
246
247 if (indio_dev->info->debugfs_reg_access == NULL)
248 return 0;
249
Axel Linabd5a2f2012-05-03 22:56:58 +0800250 if (!iio_debugfs_dentry)
Michael Henneriche553f182012-03-01 10:51:03 +0100251 return 0;
252
253 indio_dev->debugfs_dentry =
254 debugfs_create_dir(dev_name(&indio_dev->dev),
255 iio_debugfs_dentry);
Michael Henneriche553f182012-03-01 10:51:03 +0100256 if (indio_dev->debugfs_dentry == NULL) {
257 dev_warn(indio_dev->dev.parent,
258 "Failed to create debugfs directory\n");
259 return -EFAULT;
260 }
261
262 d = debugfs_create_file("direct_reg_access", 0644,
263 indio_dev->debugfs_dentry,
264 indio_dev, &iio_debugfs_reg_fops);
265 if (!d) {
266 iio_device_unregister_debugfs(indio_dev);
267 return -ENOMEM;
268 }
269
270 return 0;
271}
272#else
273static int iio_device_register_debugfs(struct iio_dev *indio_dev)
274{
275 return 0;
276}
277
278static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
279{
280}
281#endif /* CONFIG_DEBUG_FS */
282
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100283static ssize_t iio_read_channel_ext_info(struct device *dev,
284 struct device_attribute *attr,
285 char *buf)
286{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200287 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100288 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
289 const struct iio_chan_spec_ext_info *ext_info;
290
291 ext_info = &this_attr->c->ext_info[this_attr->address];
292
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200293 return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100294}
295
296static ssize_t iio_write_channel_ext_info(struct device *dev,
297 struct device_attribute *attr,
298 const char *buf,
299 size_t len)
300{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200301 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100302 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
303 const struct iio_chan_spec_ext_info *ext_info;
304
305 ext_info = &this_attr->c->ext_info[this_attr->address];
306
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200307 return ext_info->write(indio_dev, ext_info->private,
308 this_attr->c, buf, len);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100309}
310
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200311ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
312 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
313{
314 const struct iio_enum *e = (const struct iio_enum *)priv;
315 unsigned int i;
316 size_t len = 0;
317
318 if (!e->num_items)
319 return 0;
320
321 for (i = 0; i < e->num_items; ++i)
Lars-Peter Clausen74dcd432012-06-05 18:24:12 +0200322 len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200323
324 /* replace last space with a newline */
325 buf[len - 1] = '\n';
326
327 return len;
328}
329EXPORT_SYMBOL_GPL(iio_enum_available_read);
330
331ssize_t iio_enum_read(struct iio_dev *indio_dev,
332 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
333{
334 const struct iio_enum *e = (const struct iio_enum *)priv;
335 int i;
336
337 if (!e->get)
338 return -EINVAL;
339
340 i = e->get(indio_dev, chan);
341 if (i < 0)
342 return i;
343 else if (i >= e->num_items)
344 return -EINVAL;
345
Kees Cook598db582014-03-13 16:46:00 +0000346 return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200347}
348EXPORT_SYMBOL_GPL(iio_enum_read);
349
350ssize_t iio_enum_write(struct iio_dev *indio_dev,
351 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
352 size_t len)
353{
354 const struct iio_enum *e = (const struct iio_enum *)priv;
355 unsigned int i;
356 int ret;
357
358 if (!e->set)
359 return -EINVAL;
360
361 for (i = 0; i < e->num_items; i++) {
362 if (sysfs_streq(buf, e->items[i]))
363 break;
364 }
365
366 if (i == e->num_items)
367 return -EINVAL;
368
369 ret = e->set(indio_dev, chan, i);
370 return ret ? ret : len;
371}
372EXPORT_SYMBOL_GPL(iio_enum_write);
373
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100374/**
375 * iio_format_value() - Formats a IIO value into its string representation
376 * @buf: The buffer to which the formated value gets written
377 * @type: One of the IIO_VAL_... constants. This decides how the val and val2
378 * parameters are formatted.
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100379 * @vals: pointer to the values, exact meaning depends on the type parameter.
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100380 */
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100381ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100382{
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100383 unsigned long long tmp;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200384 bool scale_db = false;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100385
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100386 switch (type) {
Michael Hennerich67eedba2012-05-11 11:36:52 +0200387 case IIO_VAL_INT:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100388 return sprintf(buf, "%d\n", vals[0]);
Michael Hennerich67eedba2012-05-11 11:36:52 +0200389 case IIO_VAL_INT_PLUS_MICRO_DB:
390 scale_db = true;
391 case IIO_VAL_INT_PLUS_MICRO:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100392 if (vals[1] < 0)
393 return sprintf(buf, "-%ld.%06u%s\n", abs(vals[0]),
394 -vals[1],
Michael Hennerich67eedba2012-05-11 11:36:52 +0200395 scale_db ? " dB" : "");
Jonathan Cameron1d892712011-05-18 14:40:51 +0100396 else
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100397 return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1],
Michael Hennerich67eedba2012-05-11 11:36:52 +0200398 scale_db ? " dB" : "");
399 case IIO_VAL_INT_PLUS_NANO:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100400 if (vals[1] < 0)
401 return sprintf(buf, "-%ld.%09u\n", abs(vals[0]),
402 -vals[1]);
Michael Hennerich71646e22011-06-27 13:07:07 +0100403 else
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100404 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100405 case IIO_VAL_FRACTIONAL:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100406 tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
407 vals[1] = do_div(tmp, 1000000000LL);
408 vals[0] = tmp;
409 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
Lars-Peter Clausen103d9fb2012-10-16 17:29:00 +0100410 case IIO_VAL_FRACTIONAL_LOG2:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100411 tmp = (s64)vals[0] * 1000000000LL >> vals[1];
412 vals[1] = do_div(tmp, 1000000000LL);
413 vals[0] = tmp;
414 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
415 case IIO_VAL_INT_MULTIPLE:
416 {
417 int i;
418 int len = 0;
419
420 for (i = 0; i < size; ++i)
421 len += snprintf(&buf[len], PAGE_SIZE - len, "%d ",
422 vals[i]);
423 len += snprintf(&buf[len], PAGE_SIZE - len, "\n");
424 return len;
425 }
Michael Hennerich67eedba2012-05-11 11:36:52 +0200426 default:
Jonathan Cameron1d892712011-05-18 14:40:51 +0100427 return 0;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200428 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100429}
430
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100431static ssize_t iio_read_channel_info(struct device *dev,
432 struct device_attribute *attr,
433 char *buf)
434{
435 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
436 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100437 int vals[INDIO_MAX_RAW_ELEMENTS];
438 int ret;
439 int val_len = 2;
440
441 if (indio_dev->info->read_raw_multi)
442 ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
443 INDIO_MAX_RAW_ELEMENTS,
444 vals, &val_len,
445 this_attr->address);
446 else
447 ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
448 &vals[0], &vals[1], this_attr->address);
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100449
450 if (ret < 0)
451 return ret;
452
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100453 return iio_format_value(buf, ret, val_len, vals);
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100454}
455
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000456/**
457 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
458 * @str: The string to parse
459 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
460 * @integer: The integer part of the number
461 * @fract: The fractional part of the number
462 *
463 * Returns 0 on success, or a negative error code if the string could not be
464 * parsed.
465 */
466int iio_str_to_fixpoint(const char *str, int fract_mult,
467 int *integer, int *fract)
468{
469 int i = 0, f = 0;
470 bool integer_part = true, negative = false;
471
472 if (str[0] == '-') {
473 negative = true;
474 str++;
475 } else if (str[0] == '+') {
476 str++;
477 }
478
479 while (*str) {
480 if ('0' <= *str && *str <= '9') {
481 if (integer_part) {
482 i = i * 10 + *str - '0';
483 } else {
484 f += fract_mult * (*str - '0');
485 fract_mult /= 10;
486 }
487 } else if (*str == '\n') {
488 if (*(str + 1) == '\0')
489 break;
490 else
491 return -EINVAL;
492 } else if (*str == '.' && integer_part) {
493 integer_part = false;
494 } else {
495 return -EINVAL;
496 }
497 str++;
498 }
499
500 if (negative) {
501 if (i)
502 i = -i;
503 else
504 f = -f;
505 }
506
507 *integer = i;
508 *fract = f;
509
510 return 0;
511}
512EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
513
Jonathan Cameron1d892712011-05-18 14:40:51 +0100514static ssize_t iio_write_channel_info(struct device *dev,
515 struct device_attribute *attr,
516 const char *buf,
517 size_t len)
518{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200519 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100520 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000521 int ret, fract_mult = 100000;
522 int integer, fract;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100523
524 /* Assumes decimal - precision based on number of digits */
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100525 if (!indio_dev->info->write_raw)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100526 return -EINVAL;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100527
528 if (indio_dev->info->write_raw_get_fmt)
529 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
530 this_attr->c, this_attr->address)) {
531 case IIO_VAL_INT_PLUS_MICRO:
532 fract_mult = 100000;
533 break;
534 case IIO_VAL_INT_PLUS_NANO:
535 fract_mult = 100000000;
536 break;
537 default:
538 return -EINVAL;
539 }
540
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000541 ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
542 if (ret)
543 return ret;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100544
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100545 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
Michael Hennerich5c04af02011-06-27 13:07:10 +0100546 integer, fract, this_attr->address);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100547 if (ret)
548 return ret;
549
550 return len;
551}
552
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100553static
Jonathan Cameron1d892712011-05-18 14:40:51 +0100554int __iio_device_attr_init(struct device_attribute *dev_attr,
555 const char *postfix,
556 struct iio_chan_spec const *chan,
557 ssize_t (*readfunc)(struct device *dev,
558 struct device_attribute *attr,
559 char *buf),
560 ssize_t (*writefunc)(struct device *dev,
561 struct device_attribute *attr,
562 const char *buf,
563 size_t len),
Jonathan Cameron37044322013-09-08 14:57:00 +0100564 enum iio_shared_by shared_by)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100565{
Jonathan Cameron37044322013-09-08 14:57:00 +0100566 int ret = 0;
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000567 char *name = NULL;
Jonathan Cameron37044322013-09-08 14:57:00 +0100568 char *full_postfix;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100569 sysfs_attr_init(&dev_attr->attr);
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100570
571 /* Build up postfix of <extend_name>_<modifier>_postfix */
Jonathan Cameron37044322013-09-08 14:57:00 +0100572 if (chan->modified && (shared_by == IIO_SEPARATE)) {
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100573 if (chan->extend_name)
574 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
575 iio_modifier_names[chan
576 ->channel2],
577 chan->extend_name,
578 postfix);
579 else
580 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
581 iio_modifier_names[chan
582 ->channel2],
583 postfix);
584 } else {
Lars-Peter Clausen77bfa8b2014-02-14 14:19:00 +0000585 if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100586 full_postfix = kstrdup(postfix, GFP_KERNEL);
587 else
588 full_postfix = kasprintf(GFP_KERNEL,
589 "%s_%s",
590 chan->extend_name,
591 postfix);
592 }
Jonathan Cameron37044322013-09-08 14:57:00 +0100593 if (full_postfix == NULL)
594 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100595
Justin P. Mattock4abf6f82012-02-29 22:00:38 -0800596 if (chan->differential) { /* Differential can not have modifier */
Jonathan Cameron37044322013-09-08 14:57:00 +0100597 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100598 case IIO_SHARED_BY_ALL:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000599 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100600 break;
601 case IIO_SHARED_BY_DIR:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000602 name = kasprintf(GFP_KERNEL, "%s_%s",
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100603 iio_direction[chan->output],
604 full_postfix);
605 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100606 case IIO_SHARED_BY_TYPE:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000607 name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100608 iio_direction[chan->output],
609 iio_chan_type_name_spec[chan->type],
610 iio_chan_type_name_spec[chan->type],
611 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100612 break;
613 case IIO_SEPARATE:
614 if (!chan->indexed) {
615 WARN_ON("Differential channels must be indexed\n");
616 ret = -EINVAL;
617 goto error_free_full_postfix;
618 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000619 name = kasprintf(GFP_KERNEL,
Jonathan Cameron37044322013-09-08 14:57:00 +0100620 "%s_%s%d-%s%d_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100621 iio_direction[chan->output],
622 iio_chan_type_name_spec[chan->type],
623 chan->channel,
624 iio_chan_type_name_spec[chan->type],
625 chan->channel2,
626 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100627 break;
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100628 }
629 } else { /* Single ended */
Jonathan Cameron37044322013-09-08 14:57:00 +0100630 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100631 case IIO_SHARED_BY_ALL:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000632 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100633 break;
634 case IIO_SHARED_BY_DIR:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000635 name = kasprintf(GFP_KERNEL, "%s_%s",
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100636 iio_direction[chan->output],
637 full_postfix);
638 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100639 case IIO_SHARED_BY_TYPE:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000640 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100641 iio_direction[chan->output],
642 iio_chan_type_name_spec[chan->type],
643 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100644 break;
645
646 case IIO_SEPARATE:
647 if (chan->indexed)
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000648 name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
Jonathan Cameron37044322013-09-08 14:57:00 +0100649 iio_direction[chan->output],
650 iio_chan_type_name_spec[chan->type],
651 chan->channel,
652 full_postfix);
653 else
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000654 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
Jonathan Cameron37044322013-09-08 14:57:00 +0100655 iio_direction[chan->output],
656 iio_chan_type_name_spec[chan->type],
657 full_postfix);
658 break;
659 }
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100660 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000661 if (name == NULL) {
Jonathan Cameron1d892712011-05-18 14:40:51 +0100662 ret = -ENOMEM;
663 goto error_free_full_postfix;
664 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000665 dev_attr->attr.name = name;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100666
667 if (readfunc) {
668 dev_attr->attr.mode |= S_IRUGO;
669 dev_attr->show = readfunc;
670 }
671
672 if (writefunc) {
673 dev_attr->attr.mode |= S_IWUSR;
674 dev_attr->store = writefunc;
675 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000676
Jonathan Cameron1d892712011-05-18 14:40:51 +0100677error_free_full_postfix:
678 kfree(full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100679
Jonathan Cameron847ec802009-08-18 18:06:19 +0100680 return ret;
681}
682
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100683static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100684{
685 kfree(dev_attr->attr.name);
686}
687
688int __iio_add_chan_devattr(const char *postfix,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100689 struct iio_chan_spec const *chan,
690 ssize_t (*readfunc)(struct device *dev,
691 struct device_attribute *attr,
692 char *buf),
693 ssize_t (*writefunc)(struct device *dev,
694 struct device_attribute *attr,
695 const char *buf,
696 size_t len),
Jonathan Camerone614a542011-09-02 17:14:41 +0100697 u64 mask,
Jonathan Cameron37044322013-09-08 14:57:00 +0100698 enum iio_shared_by shared_by,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100699 struct device *dev,
700 struct list_head *attr_list)
701{
702 int ret;
703 struct iio_dev_attr *iio_attr, *t;
704
Sachin Kamat670c1102013-10-24 12:53:00 +0100705 iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000706 if (iio_attr == NULL)
707 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100708 ret = __iio_device_attr_init(&iio_attr->dev_attr,
709 postfix, chan,
Jonathan Cameron37044322013-09-08 14:57:00 +0100710 readfunc, writefunc, shared_by);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100711 if (ret)
712 goto error_iio_dev_attr_free;
713 iio_attr->c = chan;
714 iio_attr->address = mask;
715 list_for_each_entry(t, attr_list, l)
716 if (strcmp(t->dev_attr.attr.name,
717 iio_attr->dev_attr.attr.name) == 0) {
Jonathan Cameron37044322013-09-08 14:57:00 +0100718 if (shared_by == IIO_SEPARATE)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100719 dev_err(dev, "tried to double register : %s\n",
720 t->dev_attr.attr.name);
721 ret = -EBUSY;
722 goto error_device_attr_deinit;
723 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100724 list_add(&iio_attr->l, attr_list);
725
726 return 0;
727
728error_device_attr_deinit:
729 __iio_device_attr_deinit(&iio_attr->dev_attr);
730error_iio_dev_attr_free:
731 kfree(iio_attr);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100732 return ret;
733}
734
Jonathan Cameron37044322013-09-08 14:57:00 +0100735static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
736 struct iio_chan_spec const *chan,
737 enum iio_shared_by shared_by,
738 const long *infomask)
739{
740 int i, ret, attrcount = 0;
741
742 for_each_set_bit(i, infomask, sizeof(infomask)*8) {
Jonathan Cameronef4b4852014-01-03 22:24:00 +0000743 if (i >= ARRAY_SIZE(iio_chan_info_postfix))
744 return -EINVAL;
Jonathan Cameron37044322013-09-08 14:57:00 +0100745 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
746 chan,
747 &iio_read_channel_info,
748 &iio_write_channel_info,
749 i,
750 shared_by,
751 &indio_dev->dev,
752 &indio_dev->channel_attr_list);
753 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
754 continue;
755 else if (ret < 0)
756 return ret;
757 attrcount++;
758 }
759
760 return attrcount;
761}
762
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100763static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100764 struct iio_chan_spec const *chan)
765{
Jonathan Cameron5ccb3ad2012-04-15 17:41:16 +0100766 int ret, attrcount = 0;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100767 const struct iio_chan_spec_ext_info *ext_info;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100768
Jonathan Cameron1d892712011-05-18 14:40:51 +0100769 if (chan->channel < 0)
770 return 0;
Jonathan Cameron37044322013-09-08 14:57:00 +0100771 ret = iio_device_add_info_mask_type(indio_dev, chan,
772 IIO_SEPARATE,
773 &chan->info_mask_separate);
774 if (ret < 0)
775 return ret;
776 attrcount += ret;
777
778 ret = iio_device_add_info_mask_type(indio_dev, chan,
779 IIO_SHARED_BY_TYPE,
780 &chan->info_mask_shared_by_type);
781 if (ret < 0)
782 return ret;
783 attrcount += ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100784
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100785 ret = iio_device_add_info_mask_type(indio_dev, chan,
786 IIO_SHARED_BY_DIR,
787 &chan->info_mask_shared_by_dir);
788 if (ret < 0)
789 return ret;
790 attrcount += ret;
791
792 ret = iio_device_add_info_mask_type(indio_dev, chan,
793 IIO_SHARED_BY_ALL,
794 &chan->info_mask_shared_by_all);
795 if (ret < 0)
796 return ret;
797 attrcount += ret;
798
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100799 if (chan->ext_info) {
800 unsigned int i = 0;
801 for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
802 ret = __iio_add_chan_devattr(ext_info->name,
803 chan,
804 ext_info->read ?
805 &iio_read_channel_ext_info : NULL,
806 ext_info->write ?
807 &iio_write_channel_ext_info : NULL,
808 i,
809 ext_info->shared,
810 &indio_dev->dev,
811 &indio_dev->channel_attr_list);
812 i++;
813 if (ret == -EBUSY && ext_info->shared)
814 continue;
815
816 if (ret)
Jonathan Cameron37044322013-09-08 14:57:00 +0100817 return ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100818
819 attrcount++;
820 }
821 }
822
Jonathan Cameron37044322013-09-08 14:57:00 +0100823 return attrcount;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100824}
825
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100826/**
827 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
828 * @attr_list: List of IIO device attributes
829 *
830 * This function frees the memory allocated for each of the IIO device
831 * attributes in the list. Note: if you want to reuse the list after calling
832 * this function you have to reinitialize it using INIT_LIST_HEAD().
833 */
834void iio_free_chan_devattr_list(struct list_head *attr_list)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100835{
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100836 struct iio_dev_attr *p, *n;
837
838 list_for_each_entry_safe(p, n, attr_list, l) {
839 kfree(p->dev_attr.attr.name);
840 kfree(p);
841 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100842}
843
Jonathan Cameron1b732882011-05-18 14:41:43 +0100844static ssize_t iio_show_dev_name(struct device *dev,
845 struct device_attribute *attr,
846 char *buf)
847{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200848 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Kees Cook598db582014-03-13 16:46:00 +0000849 return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name);
Jonathan Cameron1b732882011-05-18 14:41:43 +0100850}
851
852static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
853
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100854static int iio_device_register_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100855{
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100856 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100857 struct iio_dev_attr *p;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100858 struct attribute **attr;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100859
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100860 /* First count elements in any existing group */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100861 if (indio_dev->info->attrs) {
862 attr = indio_dev->info->attrs->attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100863 while (*attr++ != NULL)
864 attrcount_orig++;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100865 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100866 attrcount = attrcount_orig;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100867 /*
868 * New channel registration method - relies on the fact a group does
Peter Meerwaldd25b3802012-08-26 13:43:00 +0100869 * not need to be initialized if its name is NULL.
Jonathan Cameron1d892712011-05-18 14:40:51 +0100870 */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100871 if (indio_dev->channels)
872 for (i = 0; i < indio_dev->num_channels; i++) {
873 ret = iio_device_add_channel_sysfs(indio_dev,
874 &indio_dev
Jonathan Cameron1d892712011-05-18 14:40:51 +0100875 ->channels[i]);
876 if (ret < 0)
877 goto error_clear_attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100878 attrcount += ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100879 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100880
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100881 if (indio_dev->name)
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100882 attrcount++;
883
Thomas Meyerd83fb182011-11-29 22:08:00 +0100884 indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
885 sizeof(indio_dev->chan_attr_group.attrs[0]),
886 GFP_KERNEL);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100887 if (indio_dev->chan_attr_group.attrs == NULL) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100888 ret = -ENOMEM;
889 goto error_clear_attrs;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100890 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100891 /* Copy across original attributes */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100892 if (indio_dev->info->attrs)
893 memcpy(indio_dev->chan_attr_group.attrs,
894 indio_dev->info->attrs->attrs,
895 sizeof(indio_dev->chan_attr_group.attrs[0])
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100896 *attrcount_orig);
897 attrn = attrcount_orig;
898 /* Add all elements from the list. */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100899 list_for_each_entry(p, &indio_dev->channel_attr_list, l)
900 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
901 if (indio_dev->name)
902 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100903
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100904 indio_dev->groups[indio_dev->groupcounter++] =
905 &indio_dev->chan_attr_group;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100906
Jonathan Cameron1d892712011-05-18 14:40:51 +0100907 return 0;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100908
Jonathan Cameron1d892712011-05-18 14:40:51 +0100909error_clear_attrs:
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100910 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100911
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100912 return ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100913}
914
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100915static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100916{
Jonathan Cameron1d892712011-05-18 14:40:51 +0100917
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100918 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100919 kfree(indio_dev->chan_attr_group.attrs);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100920}
921
Jonathan Cameron847ec802009-08-18 18:06:19 +0100922static void iio_dev_release(struct device *device)
923{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200924 struct iio_dev *indio_dev = dev_to_iio_dev(device);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100925 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
926 iio_device_unregister_trigger_consumer(indio_dev);
927 iio_device_unregister_eventset(indio_dev);
928 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200929
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100930 iio_buffer_put(indio_dev->buffer);
931
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200932 ida_simple_remove(&iio_ida, indio_dev->id);
933 kfree(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100934}
935
Guenter Roeck17d82b42013-02-07 17:09:00 +0000936struct device_type iio_device_type = {
Jonathan Cameron847ec802009-08-18 18:06:19 +0100937 .name = "iio_device",
938 .release = iio_dev_release,
939};
940
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000941/**
942 * iio_device_alloc() - allocate an iio_dev from a driver
943 * @sizeof_priv: Space to allocate for private structure.
944 **/
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200945struct iio_dev *iio_device_alloc(int sizeof_priv)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100946{
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +0100947 struct iio_dev *dev;
948 size_t alloc_size;
949
950 alloc_size = sizeof(struct iio_dev);
951 if (sizeof_priv) {
952 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
953 alloc_size += sizeof_priv;
954 }
955 /* ensure 32-byte alignment of whole construct ? */
956 alloc_size += IIO_ALIGN - 1;
957
958 dev = kzalloc(alloc_size, GFP_KERNEL);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100959
960 if (dev) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100961 dev->dev.groups = dev->groups;
Guenter Roeck17d82b42013-02-07 17:09:00 +0000962 dev->dev.type = &iio_device_type;
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100963 dev->dev.bus = &iio_bus_type;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100964 device_initialize(&dev->dev);
965 dev_set_drvdata(&dev->dev, (void *)dev);
966 mutex_init(&dev->mlock);
Jonathan Cameronac917a82012-02-15 19:48:00 +0000967 mutex_init(&dev->info_exist_lock);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200968 INIT_LIST_HEAD(&dev->channel_attr_list);
Jonathan Camerona9e39f92011-09-14 13:01:25 +0100969
970 dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
971 if (dev->id < 0) {
972 /* cannot use a dev_err as the name isn't available */
Sachin Kamat3176dd52013-10-24 12:53:00 +0100973 pr_err("failed to get device id\n");
Jonathan Camerona9e39f92011-09-14 13:01:25 +0100974 kfree(dev);
975 return NULL;
976 }
977 dev_set_name(&dev->dev, "iio:device%d", dev->id);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100978 INIT_LIST_HEAD(&dev->buffer_list);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100979 }
980
981 return dev;
982}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200983EXPORT_SYMBOL(iio_device_alloc);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100984
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000985/**
986 * iio_device_free() - free an iio_dev from a driver
987 * @dev: the iio_dev associated with the device
988 **/
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200989void iio_device_free(struct iio_dev *dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100990{
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200991 if (dev)
992 put_device(&dev->dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100993}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200994EXPORT_SYMBOL(iio_device_free);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100995
Grygorii Strashko9dabaf52013-07-18 11:19:00 +0100996static void devm_iio_device_release(struct device *dev, void *res)
997{
998 iio_device_free(*(struct iio_dev **)res);
999}
1000
1001static int devm_iio_device_match(struct device *dev, void *res, void *data)
1002{
1003 struct iio_dev **r = res;
1004 if (!r || !*r) {
1005 WARN_ON(!r || !*r);
1006 return 0;
1007 }
1008 return *r == data;
1009}
1010
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001011/**
1012 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1013 * @dev: Device to allocate iio_dev for
1014 * @sizeof_priv: Space to allocate for private structure.
1015 *
1016 * Managed iio_device_alloc. iio_dev allocated with this function is
1017 * automatically freed on driver detach.
1018 *
1019 * If an iio_dev allocated with this function needs to be freed separately,
1020 * devm_iio_device_free() must be used.
1021 *
1022 * RETURNS:
1023 * Pointer to allocated iio_dev on success, NULL on failure.
1024 */
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001025struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
1026{
1027 struct iio_dev **ptr, *iio_dev;
1028
1029 ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
1030 GFP_KERNEL);
1031 if (!ptr)
1032 return NULL;
1033
1034 /* use raw alloc_dr for kmalloc caller tracing */
1035 iio_dev = iio_device_alloc(sizeof_priv);
1036 if (iio_dev) {
1037 *ptr = iio_dev;
1038 devres_add(dev, ptr);
1039 } else {
1040 devres_free(ptr);
1041 }
1042
1043 return iio_dev;
1044}
1045EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
1046
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001047/**
1048 * devm_iio_device_free - Resource-managed iio_device_free()
1049 * @dev: Device this iio_dev belongs to
1050 * @iio_dev: the iio_dev associated with the device
1051 *
1052 * Free iio_dev allocated with devm_iio_device_alloc().
1053 */
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001054void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
1055{
1056 int rc;
1057
1058 rc = devres_release(dev, devm_iio_device_release,
1059 devm_iio_device_match, iio_dev);
1060 WARN_ON(rc);
1061}
1062EXPORT_SYMBOL_GPL(devm_iio_device_free);
1063
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001064/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001065 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001066 **/
1067static int iio_chrdev_open(struct inode *inode, struct file *filp)
1068{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001069 struct iio_dev *indio_dev = container_of(inode->i_cdev,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001070 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001071
1072 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
1073 return -EBUSY;
1074
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001075 iio_device_get(indio_dev);
1076
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001077 filp->private_data = indio_dev;
Jonathan Cameron30eb82f2011-09-21 11:16:02 +01001078
Lars-Peter Clausen79335142011-12-19 15:23:49 +01001079 return 0;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001080}
1081
1082/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001083 * iio_chrdev_release() - chrdev file close buffer access and ioctls
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001084 **/
1085static int iio_chrdev_release(struct inode *inode, struct file *filp)
1086{
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001087 struct iio_dev *indio_dev = container_of(inode->i_cdev,
1088 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001089 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001090 iio_device_put(indio_dev);
1091
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001092 return 0;
1093}
1094
1095/* Somewhat of a cross file organization violation - ioctls here are actually
1096 * event related */
1097static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1098{
1099 struct iio_dev *indio_dev = filp->private_data;
1100 int __user *ip = (int __user *)arg;
1101 int fd;
1102
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +01001103 if (!indio_dev->info)
1104 return -ENODEV;
1105
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001106 if (cmd == IIO_GET_EVENT_FD_IOCTL) {
1107 fd = iio_event_getfd(indio_dev);
1108 if (copy_to_user(ip, &fd, sizeof(fd)))
1109 return -EFAULT;
1110 return 0;
1111 }
1112 return -EINVAL;
1113}
1114
Jonathan Cameron14555b12011-09-21 11:15:57 +01001115static const struct file_operations iio_buffer_fileops = {
1116 .read = iio_buffer_read_first_n_outer_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001117 .release = iio_chrdev_release,
1118 .open = iio_chrdev_open,
Jonathan Cameron14555b12011-09-21 11:15:57 +01001119 .poll = iio_buffer_poll_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001120 .owner = THIS_MODULE,
1121 .llseek = noop_llseek,
1122 .unlocked_ioctl = iio_ioctl,
1123 .compat_ioctl = iio_ioctl,
1124};
1125
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001126static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1127
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001128/**
1129 * iio_device_register() - register a device with the IIO subsystem
1130 * @indio_dev: Device structure filled by the device driver
1131 **/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001132int iio_device_register(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001133{
1134 int ret;
1135
Guenter Roeck17d82b42013-02-07 17:09:00 +00001136 /* If the calling driver did not initialize of_node, do it here */
1137 if (!indio_dev->dev.of_node && indio_dev->dev.parent)
1138 indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
1139
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001140 /* configure elements for the chrdev */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001141 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001142
Michael Henneriche553f182012-03-01 10:51:03 +01001143 ret = iio_device_register_debugfs(indio_dev);
1144 if (ret) {
1145 dev_err(indio_dev->dev.parent,
1146 "Failed to register debugfs interfaces\n");
Hartmut Knaack92825ff2014-02-16 11:53:00 +00001147 return ret;
Michael Henneriche553f182012-03-01 10:51:03 +01001148 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001149 ret = iio_device_register_sysfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001150 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001151 dev_err(indio_dev->dev.parent,
Jonathan Cameron847ec802009-08-18 18:06:19 +01001152 "Failed to register sysfs interfaces\n");
Michael Henneriche553f182012-03-01 10:51:03 +01001153 goto error_unreg_debugfs;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001154 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001155 ret = iio_device_register_eventset(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001156 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001157 dev_err(indio_dev->dev.parent,
Roel Van Nyenc849d252010-04-29 19:27:31 +02001158 "Failed to register event set\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001159 goto error_free_sysfs;
1160 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001161 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
1162 iio_device_register_trigger_consumer(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001163
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001164 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1165 indio_dev->setup_ops == NULL)
1166 indio_dev->setup_ops = &noop_ring_setup_ops;
1167
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001168 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
1169 indio_dev->chrdev.owner = indio_dev->info->driver_module;
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001170 indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001171 ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001172 if (ret < 0)
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001173 goto error_unreg_eventset;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001174
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001175 ret = device_add(&indio_dev->dev);
1176 if (ret < 0)
1177 goto error_cdev_del;
1178
1179 return 0;
1180error_cdev_del:
1181 cdev_del(&indio_dev->chrdev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001182error_unreg_eventset:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001183 iio_device_unregister_eventset(indio_dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001184error_free_sysfs:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001185 iio_device_unregister_sysfs(indio_dev);
Michael Henneriche553f182012-03-01 10:51:03 +01001186error_unreg_debugfs:
1187 iio_device_unregister_debugfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001188 return ret;
1189}
1190EXPORT_SYMBOL(iio_device_register);
1191
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001192/**
1193 * iio_device_unregister() - unregister a device from the IIO subsystem
1194 * @indio_dev: Device structure representing the device.
1195 **/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001196void iio_device_unregister(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001197{
Jonathan Cameronac917a82012-02-15 19:48:00 +00001198 mutex_lock(&indio_dev->info_exist_lock);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001199
1200 device_del(&indio_dev->dev);
1201
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001202 if (indio_dev->chrdev.dev)
1203 cdev_del(&indio_dev->chrdev);
Lars-Peter Clausenbc4c9612013-09-21 16:21:00 +01001204 iio_device_unregister_debugfs(indio_dev);
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001205
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001206 iio_disable_all_buffers(indio_dev);
1207
Jonathan Cameronac917a82012-02-15 19:48:00 +00001208 indio_dev->info = NULL;
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +01001209
1210 iio_device_wakeup_eventset(indio_dev);
1211 iio_buffer_wakeup_poll(indio_dev);
1212
Jonathan Cameronac917a82012-02-15 19:48:00 +00001213 mutex_unlock(&indio_dev->info_exist_lock);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001214}
1215EXPORT_SYMBOL(iio_device_unregister);
Sachin Kamat8caa07c2013-10-29 11:39:00 +00001216
1217static void devm_iio_device_unreg(struct device *dev, void *res)
1218{
1219 iio_device_unregister(*(struct iio_dev **)res);
1220}
1221
1222/**
1223 * devm_iio_device_register - Resource-managed iio_device_register()
1224 * @dev: Device to allocate iio_dev for
1225 * @indio_dev: Device structure filled by the device driver
1226 *
1227 * Managed iio_device_register. The IIO device registered with this
1228 * function is automatically unregistered on driver detach. This function
1229 * calls iio_device_register() internally. Refer to that function for more
1230 * information.
1231 *
1232 * If an iio_dev registered with this function needs to be unregistered
1233 * separately, devm_iio_device_unregister() must be used.
1234 *
1235 * RETURNS:
1236 * 0 on success, negative error number on failure.
1237 */
1238int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev)
1239{
1240 struct iio_dev **ptr;
1241 int ret;
1242
1243 ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL);
1244 if (!ptr)
1245 return -ENOMEM;
1246
1247 *ptr = indio_dev;
1248 ret = iio_device_register(indio_dev);
1249 if (!ret)
1250 devres_add(dev, ptr);
1251 else
1252 devres_free(ptr);
1253
1254 return ret;
1255}
1256EXPORT_SYMBOL_GPL(devm_iio_device_register);
1257
1258/**
1259 * devm_iio_device_unregister - Resource-managed iio_device_unregister()
1260 * @dev: Device this iio_dev belongs to
1261 * @indio_dev: the iio_dev associated with the device
1262 *
1263 * Unregister iio_dev registered with devm_iio_device_register().
1264 */
1265void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev)
1266{
1267 int rc;
1268
1269 rc = devres_release(dev, devm_iio_device_unreg,
1270 devm_iio_device_match, indio_dev);
1271 WARN_ON(rc);
1272}
1273EXPORT_SYMBOL_GPL(devm_iio_device_unregister);
1274
Jonathan Cameron847ec802009-08-18 18:06:19 +01001275subsys_initcall(iio_init);
1276module_exit(iio_exit);
1277
Jonathan Cameronc8b95952012-09-02 21:27:56 +01001278MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001279MODULE_DESCRIPTION("Industrial I/O core");
1280MODULE_LICENSE("GPL");