blob: 18f72e3d0ed6e6c8e9bb73ec8616124a2422aed4 [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",
Jonathan Cameron1d892712011-05-18 14:40:51 +010072};
73
Jonathan Cameron330c6c52011-09-02 17:14:39 +010074static const char * const iio_modifier_names[] = {
Jonathan Cameron1d892712011-05-18 14:40:51 +010075 [IIO_MOD_X] = "x",
76 [IIO_MOD_Y] = "y",
77 [IIO_MOD_Z] = "z",
Jonathan Cameron8f5879b2012-05-05 10:39:22 +010078 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
Jonathan Cameroncf82cb82012-05-05 10:56:41 +010079 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
Jonathan Cameron330c6c52011-09-02 17:14:39 +010080 [IIO_MOD_LIGHT_BOTH] = "both",
81 [IIO_MOD_LIGHT_IR] = "ir",
Jon Brenner21cd1fa2012-05-16 10:46:42 -050082 [IIO_MOD_LIGHT_CLEAR] = "clear",
83 [IIO_MOD_LIGHT_RED] = "red",
84 [IIO_MOD_LIGHT_GREEN] = "green",
85 [IIO_MOD_LIGHT_BLUE] = "blue",
Jonathan Cameron1d892712011-05-18 14:40:51 +010086};
87
88/* relies on pairs of these shared then separate */
89static const char * const iio_chan_info_postfix[] = {
Jonathan Cameron75a973c2012-04-15 17:41:30 +010090 [IIO_CHAN_INFO_RAW] = "raw",
91 [IIO_CHAN_INFO_PROCESSED] = "input",
Jonathan Cameronc8a9f802011-10-26 17:41:36 +010092 [IIO_CHAN_INFO_SCALE] = "scale",
93 [IIO_CHAN_INFO_OFFSET] = "offset",
94 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
95 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
96 [IIO_CHAN_INFO_PEAK] = "peak_raw",
97 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
98 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
99 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
Jonathan Camerondf94aba2011-11-27 11:39:12 +0000100 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
101 = "filter_low_pass_3db_frequency",
Laxman Dewangance85a1c2012-04-13 16:03:31 +0530102 [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
Michael Hennericha6b12852012-04-27 10:58:34 +0200103 [IIO_CHAN_INFO_FREQUENCY] = "frequency",
104 [IIO_CHAN_INFO_PHASE] = "phase",
Michael Hennerichb65d6212012-05-11 11:36:53 +0200105 [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
srinivas pandruvada7c9ab032012-09-05 13:56:00 +0100106 [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
Peter Meerwald899d90b2013-09-08 16:20:00 +0100107 [IIO_CHAN_INFO_INT_TIME] = "integration_time",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100108};
109
Jonathan Cameron5fb21c82011-12-05 21:37:10 +0000110const struct iio_chan_spec
111*iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
112{
113 int i;
114
115 for (i = 0; i < indio_dev->num_channels; i++)
116 if (indio_dev->channels[i].scan_index == si)
117 return &indio_dev->channels[i];
118 return NULL;
119}
120
Jonathan Cameron847ec802009-08-18 18:06:19 +0100121/* This turns up an awful lot */
122ssize_t iio_read_const_attr(struct device *dev,
123 struct device_attribute *attr,
124 char *buf)
125{
126 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
127}
128EXPORT_SYMBOL(iio_read_const_attr);
129
Jonathan Cameron847ec802009-08-18 18:06:19 +0100130static int __init iio_init(void)
131{
132 int ret;
133
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100134 /* Register sysfs bus */
135 ret = bus_register(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100136 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100137 pr_err("could not register bus type\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +0100138 goto error_nothing;
139 }
140
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100141 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
142 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100143 pr_err("failed to allocate char dev region\n");
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100144 goto error_unregister_bus_type;
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100145 }
Jonathan Cameron847ec802009-08-18 18:06:19 +0100146
Michael Henneriche553f182012-03-01 10:51:03 +0100147 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
148
Jonathan Cameron847ec802009-08-18 18:06:19 +0100149 return 0;
150
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100151error_unregister_bus_type:
152 bus_unregister(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100153error_nothing:
154 return ret;
155}
156
157static void __exit iio_exit(void)
158{
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100159 if (iio_devt)
160 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100161 bus_unregister(&iio_bus_type);
Michael Henneriche553f182012-03-01 10:51:03 +0100162 debugfs_remove(iio_debugfs_dentry);
163}
164
165#if defined(CONFIG_DEBUG_FS)
Michael Henneriche553f182012-03-01 10:51:03 +0100166static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
167 size_t count, loff_t *ppos)
168{
169 struct iio_dev *indio_dev = file->private_data;
170 char buf[20];
171 unsigned val = 0;
172 ssize_t len;
173 int ret;
174
175 ret = indio_dev->info->debugfs_reg_access(indio_dev,
176 indio_dev->cached_reg_addr,
177 0, &val);
178 if (ret)
179 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
180
181 len = snprintf(buf, sizeof(buf), "0x%X\n", val);
182
183 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
184}
185
186static ssize_t iio_debugfs_write_reg(struct file *file,
187 const char __user *userbuf, size_t count, loff_t *ppos)
188{
189 struct iio_dev *indio_dev = file->private_data;
190 unsigned reg, val;
191 char buf[80];
192 int ret;
193
194 count = min_t(size_t, count, (sizeof(buf)-1));
195 if (copy_from_user(buf, userbuf, count))
196 return -EFAULT;
197
198 buf[count] = 0;
199
200 ret = sscanf(buf, "%i %i", &reg, &val);
201
202 switch (ret) {
203 case 1:
204 indio_dev->cached_reg_addr = reg;
205 break;
206 case 2:
207 indio_dev->cached_reg_addr = reg;
208 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
209 val, NULL);
210 if (ret) {
211 dev_err(indio_dev->dev.parent, "%s: write failed\n",
212 __func__);
213 return ret;
214 }
215 break;
216 default:
217 return -EINVAL;
218 }
219
220 return count;
221}
222
223static const struct file_operations iio_debugfs_reg_fops = {
Axel Lin5a28c872012-05-03 09:50:51 +0800224 .open = simple_open,
Michael Henneriche553f182012-03-01 10:51:03 +0100225 .read = iio_debugfs_read_reg,
226 .write = iio_debugfs_write_reg,
227};
228
229static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
230{
231 debugfs_remove_recursive(indio_dev->debugfs_dentry);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100232}
233
Michael Henneriche553f182012-03-01 10:51:03 +0100234static int iio_device_register_debugfs(struct iio_dev *indio_dev)
235{
236 struct dentry *d;
237
238 if (indio_dev->info->debugfs_reg_access == NULL)
239 return 0;
240
Axel Linabd5a2f2012-05-03 22:56:58 +0800241 if (!iio_debugfs_dentry)
Michael Henneriche553f182012-03-01 10:51:03 +0100242 return 0;
243
244 indio_dev->debugfs_dentry =
245 debugfs_create_dir(dev_name(&indio_dev->dev),
246 iio_debugfs_dentry);
Michael Henneriche553f182012-03-01 10:51:03 +0100247 if (indio_dev->debugfs_dentry == NULL) {
248 dev_warn(indio_dev->dev.parent,
249 "Failed to create debugfs directory\n");
250 return -EFAULT;
251 }
252
253 d = debugfs_create_file("direct_reg_access", 0644,
254 indio_dev->debugfs_dentry,
255 indio_dev, &iio_debugfs_reg_fops);
256 if (!d) {
257 iio_device_unregister_debugfs(indio_dev);
258 return -ENOMEM;
259 }
260
261 return 0;
262}
263#else
264static int iio_device_register_debugfs(struct iio_dev *indio_dev)
265{
266 return 0;
267}
268
269static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
270{
271}
272#endif /* CONFIG_DEBUG_FS */
273
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100274static ssize_t iio_read_channel_ext_info(struct device *dev,
275 struct device_attribute *attr,
276 char *buf)
277{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200278 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100279 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
280 const struct iio_chan_spec_ext_info *ext_info;
281
282 ext_info = &this_attr->c->ext_info[this_attr->address];
283
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200284 return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100285}
286
287static ssize_t iio_write_channel_ext_info(struct device *dev,
288 struct device_attribute *attr,
289 const char *buf,
290 size_t len)
291{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200292 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100293 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
294 const struct iio_chan_spec_ext_info *ext_info;
295
296 ext_info = &this_attr->c->ext_info[this_attr->address];
297
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200298 return ext_info->write(indio_dev, ext_info->private,
299 this_attr->c, buf, len);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100300}
301
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200302ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
303 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
304{
305 const struct iio_enum *e = (const struct iio_enum *)priv;
306 unsigned int i;
307 size_t len = 0;
308
309 if (!e->num_items)
310 return 0;
311
312 for (i = 0; i < e->num_items; ++i)
Lars-Peter Clausen74dcd432012-06-05 18:24:12 +0200313 len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200314
315 /* replace last space with a newline */
316 buf[len - 1] = '\n';
317
318 return len;
319}
320EXPORT_SYMBOL_GPL(iio_enum_available_read);
321
322ssize_t iio_enum_read(struct iio_dev *indio_dev,
323 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
324{
325 const struct iio_enum *e = (const struct iio_enum *)priv;
326 int i;
327
328 if (!e->get)
329 return -EINVAL;
330
331 i = e->get(indio_dev, chan);
332 if (i < 0)
333 return i;
334 else if (i >= e->num_items)
335 return -EINVAL;
336
337 return sprintf(buf, "%s\n", e->items[i]);
338}
339EXPORT_SYMBOL_GPL(iio_enum_read);
340
341ssize_t iio_enum_write(struct iio_dev *indio_dev,
342 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
343 size_t len)
344{
345 const struct iio_enum *e = (const struct iio_enum *)priv;
346 unsigned int i;
347 int ret;
348
349 if (!e->set)
350 return -EINVAL;
351
352 for (i = 0; i < e->num_items; i++) {
353 if (sysfs_streq(buf, e->items[i]))
354 break;
355 }
356
357 if (i == e->num_items)
358 return -EINVAL;
359
360 ret = e->set(indio_dev, chan, i);
361 return ret ? ret : len;
362}
363EXPORT_SYMBOL_GPL(iio_enum_write);
364
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100365/**
366 * iio_format_value() - Formats a IIO value into its string representation
367 * @buf: The buffer to which the formated value gets written
368 * @type: One of the IIO_VAL_... constants. This decides how the val and val2
369 * parameters are formatted.
370 * @val: First part of the value, exact meaning depends on the type parameter.
371 * @val2: Second part of the value, exact meaning depends on the type parameter.
372 */
373ssize_t iio_format_value(char *buf, unsigned int type, int val, int val2)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100374{
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100375 unsigned long long tmp;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200376 bool scale_db = false;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100377
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100378 switch (type) {
Michael Hennerich67eedba2012-05-11 11:36:52 +0200379 case IIO_VAL_INT:
Jonathan Cameron1d892712011-05-18 14:40:51 +0100380 return sprintf(buf, "%d\n", val);
Michael Hennerich67eedba2012-05-11 11:36:52 +0200381 case IIO_VAL_INT_PLUS_MICRO_DB:
382 scale_db = true;
383 case IIO_VAL_INT_PLUS_MICRO:
Jonathan Cameron1d892712011-05-18 14:40:51 +0100384 if (val2 < 0)
Oleksandr Kravchenkod9a01342013-07-22 12:16:00 +0100385 return sprintf(buf, "-%ld.%06u%s\n", abs(val), -val2,
Michael Hennerich67eedba2012-05-11 11:36:52 +0200386 scale_db ? " dB" : "");
Jonathan Cameron1d892712011-05-18 14:40:51 +0100387 else
Michael Hennerich67eedba2012-05-11 11:36:52 +0200388 return sprintf(buf, "%d.%06u%s\n", val, val2,
389 scale_db ? " dB" : "");
390 case IIO_VAL_INT_PLUS_NANO:
Michael Hennerich71646e22011-06-27 13:07:07 +0100391 if (val2 < 0)
Oleksandr Kravchenkod9a01342013-07-22 12:16:00 +0100392 return sprintf(buf, "-%ld.%09u\n", abs(val), -val2);
Michael Hennerich71646e22011-06-27 13:07:07 +0100393 else
394 return sprintf(buf, "%d.%09u\n", val, val2);
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100395 case IIO_VAL_FRACTIONAL:
396 tmp = div_s64((s64)val * 1000000000LL, val2);
397 val2 = do_div(tmp, 1000000000LL);
398 val = tmp;
399 return sprintf(buf, "%d.%09u\n", val, val2);
Lars-Peter Clausen103d9fb2012-10-16 17:29:00 +0100400 case IIO_VAL_FRACTIONAL_LOG2:
401 tmp = (s64)val * 1000000000LL >> val2;
402 val2 = do_div(tmp, 1000000000LL);
403 val = tmp;
404 return sprintf(buf, "%d.%09u\n", val, val2);
Michael Hennerich67eedba2012-05-11 11:36:52 +0200405 default:
Jonathan Cameron1d892712011-05-18 14:40:51 +0100406 return 0;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200407 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100408}
409
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100410static ssize_t iio_read_channel_info(struct device *dev,
411 struct device_attribute *attr,
412 char *buf)
413{
414 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
415 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
416 int val, val2;
417 int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
418 &val, &val2, this_attr->address);
419
420 if (ret < 0)
421 return ret;
422
423 return iio_format_value(buf, ret, val, val2);
424}
425
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000426/**
427 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
428 * @str: The string to parse
429 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
430 * @integer: The integer part of the number
431 * @fract: The fractional part of the number
432 *
433 * Returns 0 on success, or a negative error code if the string could not be
434 * parsed.
435 */
436int iio_str_to_fixpoint(const char *str, int fract_mult,
437 int *integer, int *fract)
438{
439 int i = 0, f = 0;
440 bool integer_part = true, negative = false;
441
442 if (str[0] == '-') {
443 negative = true;
444 str++;
445 } else if (str[0] == '+') {
446 str++;
447 }
448
449 while (*str) {
450 if ('0' <= *str && *str <= '9') {
451 if (integer_part) {
452 i = i * 10 + *str - '0';
453 } else {
454 f += fract_mult * (*str - '0');
455 fract_mult /= 10;
456 }
457 } else if (*str == '\n') {
458 if (*(str + 1) == '\0')
459 break;
460 else
461 return -EINVAL;
462 } else if (*str == '.' && integer_part) {
463 integer_part = false;
464 } else {
465 return -EINVAL;
466 }
467 str++;
468 }
469
470 if (negative) {
471 if (i)
472 i = -i;
473 else
474 f = -f;
475 }
476
477 *integer = i;
478 *fract = f;
479
480 return 0;
481}
482EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
483
Jonathan Cameron1d892712011-05-18 14:40:51 +0100484static ssize_t iio_write_channel_info(struct device *dev,
485 struct device_attribute *attr,
486 const char *buf,
487 size_t len)
488{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200489 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100490 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000491 int ret, fract_mult = 100000;
492 int integer, fract;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100493
494 /* Assumes decimal - precision based on number of digits */
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100495 if (!indio_dev->info->write_raw)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100496 return -EINVAL;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100497
498 if (indio_dev->info->write_raw_get_fmt)
499 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
500 this_attr->c, this_attr->address)) {
501 case IIO_VAL_INT_PLUS_MICRO:
502 fract_mult = 100000;
503 break;
504 case IIO_VAL_INT_PLUS_NANO:
505 fract_mult = 100000000;
506 break;
507 default:
508 return -EINVAL;
509 }
510
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000511 ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
512 if (ret)
513 return ret;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100514
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100515 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
Michael Hennerich5c04af02011-06-27 13:07:10 +0100516 integer, fract, this_attr->address);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100517 if (ret)
518 return ret;
519
520 return len;
521}
522
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100523static
Jonathan Cameron1d892712011-05-18 14:40:51 +0100524int __iio_device_attr_init(struct device_attribute *dev_attr,
525 const char *postfix,
526 struct iio_chan_spec const *chan,
527 ssize_t (*readfunc)(struct device *dev,
528 struct device_attribute *attr,
529 char *buf),
530 ssize_t (*writefunc)(struct device *dev,
531 struct device_attribute *attr,
532 const char *buf,
533 size_t len),
Jonathan Cameron37044322013-09-08 14:57:00 +0100534 enum iio_shared_by shared_by)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100535{
Jonathan Cameron37044322013-09-08 14:57:00 +0100536 int ret = 0;
537 char *name_format = NULL;
538 char *full_postfix;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100539 sysfs_attr_init(&dev_attr->attr);
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100540
541 /* Build up postfix of <extend_name>_<modifier>_postfix */
Jonathan Cameron37044322013-09-08 14:57:00 +0100542 if (chan->modified && (shared_by == IIO_SEPARATE)) {
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100543 if (chan->extend_name)
544 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
545 iio_modifier_names[chan
546 ->channel2],
547 chan->extend_name,
548 postfix);
549 else
550 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
551 iio_modifier_names[chan
552 ->channel2],
553 postfix);
554 } else {
555 if (chan->extend_name == NULL)
556 full_postfix = kstrdup(postfix, GFP_KERNEL);
557 else
558 full_postfix = kasprintf(GFP_KERNEL,
559 "%s_%s",
560 chan->extend_name,
561 postfix);
562 }
Jonathan Cameron37044322013-09-08 14:57:00 +0100563 if (full_postfix == NULL)
564 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100565
Justin P. Mattock4abf6f82012-02-29 22:00:38 -0800566 if (chan->differential) { /* Differential can not have modifier */
Jonathan Cameron37044322013-09-08 14:57:00 +0100567 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100568 case IIO_SHARED_BY_ALL:
569 name_format = kasprintf(GFP_KERNEL, "%s", full_postfix);
570 break;
571 case IIO_SHARED_BY_DIR:
572 name_format = kasprintf(GFP_KERNEL, "%s_%s",
573 iio_direction[chan->output],
574 full_postfix);
575 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100576 case IIO_SHARED_BY_TYPE:
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100577 name_format
578 = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
579 iio_direction[chan->output],
580 iio_chan_type_name_spec[chan->type],
581 iio_chan_type_name_spec[chan->type],
582 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100583 break;
584 case IIO_SEPARATE:
585 if (!chan->indexed) {
586 WARN_ON("Differential channels must be indexed\n");
587 ret = -EINVAL;
588 goto error_free_full_postfix;
589 }
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100590 name_format
Jonathan Cameron37044322013-09-08 14:57:00 +0100591 = kasprintf(GFP_KERNEL,
592 "%s_%s%d-%s%d_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100593 iio_direction[chan->output],
594 iio_chan_type_name_spec[chan->type],
595 chan->channel,
596 iio_chan_type_name_spec[chan->type],
597 chan->channel2,
598 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100599 break;
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100600 }
601 } else { /* Single ended */
Jonathan Cameron37044322013-09-08 14:57:00 +0100602 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100603 case IIO_SHARED_BY_ALL:
604 name_format = kasprintf(GFP_KERNEL, "%s", full_postfix);
605 break;
606 case IIO_SHARED_BY_DIR:
607 name_format = kasprintf(GFP_KERNEL, "%s_%s",
608 iio_direction[chan->output],
609 full_postfix);
610 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100611 case IIO_SHARED_BY_TYPE:
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100612 name_format
613 = kasprintf(GFP_KERNEL, "%s_%s_%s",
614 iio_direction[chan->output],
615 iio_chan_type_name_spec[chan->type],
616 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100617 break;
618
619 case IIO_SEPARATE:
620 if (chan->indexed)
621 name_format
622 = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
623 iio_direction[chan->output],
624 iio_chan_type_name_spec[chan->type],
625 chan->channel,
626 full_postfix);
627 else
628 name_format
629 = kasprintf(GFP_KERNEL, "%s_%s_%s",
630 iio_direction[chan->output],
631 iio_chan_type_name_spec[chan->type],
632 full_postfix);
633 break;
634 }
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100635 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100636 if (name_format == NULL) {
637 ret = -ENOMEM;
638 goto error_free_full_postfix;
639 }
640 dev_attr->attr.name = kasprintf(GFP_KERNEL,
641 name_format,
642 chan->channel,
643 chan->channel2);
644 if (dev_attr->attr.name == NULL) {
645 ret = -ENOMEM;
646 goto error_free_name_format;
647 }
648
649 if (readfunc) {
650 dev_attr->attr.mode |= S_IRUGO;
651 dev_attr->show = readfunc;
652 }
653
654 if (writefunc) {
655 dev_attr->attr.mode |= S_IWUSR;
656 dev_attr->store = writefunc;
657 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100658error_free_name_format:
659 kfree(name_format);
660error_free_full_postfix:
661 kfree(full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100662
Jonathan Cameron847ec802009-08-18 18:06:19 +0100663 return ret;
664}
665
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100666static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100667{
668 kfree(dev_attr->attr.name);
669}
670
671int __iio_add_chan_devattr(const char *postfix,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100672 struct iio_chan_spec const *chan,
673 ssize_t (*readfunc)(struct device *dev,
674 struct device_attribute *attr,
675 char *buf),
676 ssize_t (*writefunc)(struct device *dev,
677 struct device_attribute *attr,
678 const char *buf,
679 size_t len),
Jonathan Camerone614a542011-09-02 17:14:41 +0100680 u64 mask,
Jonathan Cameron37044322013-09-08 14:57:00 +0100681 enum iio_shared_by shared_by,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100682 struct device *dev,
683 struct list_head *attr_list)
684{
685 int ret;
686 struct iio_dev_attr *iio_attr, *t;
687
Sachin Kamat670c1102013-10-24 12:53:00 +0100688 iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100689 if (iio_attr == NULL) {
690 ret = -ENOMEM;
691 goto error_ret;
692 }
693 ret = __iio_device_attr_init(&iio_attr->dev_attr,
694 postfix, chan,
Jonathan Cameron37044322013-09-08 14:57:00 +0100695 readfunc, writefunc, shared_by);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100696 if (ret)
697 goto error_iio_dev_attr_free;
698 iio_attr->c = chan;
699 iio_attr->address = mask;
700 list_for_each_entry(t, attr_list, l)
701 if (strcmp(t->dev_attr.attr.name,
702 iio_attr->dev_attr.attr.name) == 0) {
Jonathan Cameron37044322013-09-08 14:57:00 +0100703 if (shared_by == IIO_SEPARATE)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100704 dev_err(dev, "tried to double register : %s\n",
705 t->dev_attr.attr.name);
706 ret = -EBUSY;
707 goto error_device_attr_deinit;
708 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100709 list_add(&iio_attr->l, attr_list);
710
711 return 0;
712
713error_device_attr_deinit:
714 __iio_device_attr_deinit(&iio_attr->dev_attr);
715error_iio_dev_attr_free:
716 kfree(iio_attr);
717error_ret:
718 return ret;
719}
720
Jonathan Cameron37044322013-09-08 14:57:00 +0100721static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
722 struct iio_chan_spec const *chan,
723 enum iio_shared_by shared_by,
724 const long *infomask)
725{
726 int i, ret, attrcount = 0;
727
728 for_each_set_bit(i, infomask, sizeof(infomask)*8) {
729 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
730 chan,
731 &iio_read_channel_info,
732 &iio_write_channel_info,
733 i,
734 shared_by,
735 &indio_dev->dev,
736 &indio_dev->channel_attr_list);
737 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
738 continue;
739 else if (ret < 0)
740 return ret;
741 attrcount++;
742 }
743
744 return attrcount;
745}
746
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100747static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100748 struct iio_chan_spec const *chan)
749{
Jonathan Cameron5ccb3ad2012-04-15 17:41:16 +0100750 int ret, attrcount = 0;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100751 const struct iio_chan_spec_ext_info *ext_info;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100752
Jonathan Cameron1d892712011-05-18 14:40:51 +0100753 if (chan->channel < 0)
754 return 0;
Jonathan Cameron37044322013-09-08 14:57:00 +0100755 ret = iio_device_add_info_mask_type(indio_dev, chan,
756 IIO_SEPARATE,
757 &chan->info_mask_separate);
758 if (ret < 0)
759 return ret;
760 attrcount += ret;
761
762 ret = iio_device_add_info_mask_type(indio_dev, chan,
763 IIO_SHARED_BY_TYPE,
764 &chan->info_mask_shared_by_type);
765 if (ret < 0)
766 return ret;
767 attrcount += ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100768
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100769 ret = iio_device_add_info_mask_type(indio_dev, chan,
770 IIO_SHARED_BY_DIR,
771 &chan->info_mask_shared_by_dir);
772 if (ret < 0)
773 return ret;
774 attrcount += ret;
775
776 ret = iio_device_add_info_mask_type(indio_dev, chan,
777 IIO_SHARED_BY_ALL,
778 &chan->info_mask_shared_by_all);
779 if (ret < 0)
780 return ret;
781 attrcount += ret;
782
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100783 if (chan->ext_info) {
784 unsigned int i = 0;
785 for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
786 ret = __iio_add_chan_devattr(ext_info->name,
787 chan,
788 ext_info->read ?
789 &iio_read_channel_ext_info : NULL,
790 ext_info->write ?
791 &iio_write_channel_ext_info : NULL,
792 i,
793 ext_info->shared,
794 &indio_dev->dev,
795 &indio_dev->channel_attr_list);
796 i++;
797 if (ret == -EBUSY && ext_info->shared)
798 continue;
799
800 if (ret)
Jonathan Cameron37044322013-09-08 14:57:00 +0100801 return ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100802
803 attrcount++;
804 }
805 }
806
Jonathan Cameron37044322013-09-08 14:57:00 +0100807 return attrcount;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100808}
809
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100810/**
811 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
812 * @attr_list: List of IIO device attributes
813 *
814 * This function frees the memory allocated for each of the IIO device
815 * attributes in the list. Note: if you want to reuse the list after calling
816 * this function you have to reinitialize it using INIT_LIST_HEAD().
817 */
818void iio_free_chan_devattr_list(struct list_head *attr_list)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100819{
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100820 struct iio_dev_attr *p, *n;
821
822 list_for_each_entry_safe(p, n, attr_list, l) {
823 kfree(p->dev_attr.attr.name);
824 kfree(p);
825 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100826}
827
Jonathan Cameron1b732882011-05-18 14:41:43 +0100828static ssize_t iio_show_dev_name(struct device *dev,
829 struct device_attribute *attr,
830 char *buf)
831{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200832 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron1b732882011-05-18 14:41:43 +0100833 return sprintf(buf, "%s\n", indio_dev->name);
834}
835
836static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
837
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100838static int iio_device_register_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100839{
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100840 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100841 struct iio_dev_attr *p;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100842 struct attribute **attr;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100843
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100844 /* First count elements in any existing group */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100845 if (indio_dev->info->attrs) {
846 attr = indio_dev->info->attrs->attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100847 while (*attr++ != NULL)
848 attrcount_orig++;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100849 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100850 attrcount = attrcount_orig;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100851 /*
852 * New channel registration method - relies on the fact a group does
Peter Meerwaldd25b3802012-08-26 13:43:00 +0100853 * not need to be initialized if its name is NULL.
Jonathan Cameron1d892712011-05-18 14:40:51 +0100854 */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100855 if (indio_dev->channels)
856 for (i = 0; i < indio_dev->num_channels; i++) {
857 ret = iio_device_add_channel_sysfs(indio_dev,
858 &indio_dev
Jonathan Cameron1d892712011-05-18 14:40:51 +0100859 ->channels[i]);
860 if (ret < 0)
861 goto error_clear_attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100862 attrcount += ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100863 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100864
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100865 if (indio_dev->name)
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100866 attrcount++;
867
Thomas Meyerd83fb182011-11-29 22:08:00 +0100868 indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
869 sizeof(indio_dev->chan_attr_group.attrs[0]),
870 GFP_KERNEL);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100871 if (indio_dev->chan_attr_group.attrs == NULL) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100872 ret = -ENOMEM;
873 goto error_clear_attrs;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100874 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100875 /* Copy across original attributes */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100876 if (indio_dev->info->attrs)
877 memcpy(indio_dev->chan_attr_group.attrs,
878 indio_dev->info->attrs->attrs,
879 sizeof(indio_dev->chan_attr_group.attrs[0])
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100880 *attrcount_orig);
881 attrn = attrcount_orig;
882 /* Add all elements from the list. */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100883 list_for_each_entry(p, &indio_dev->channel_attr_list, l)
884 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
885 if (indio_dev->name)
886 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100887
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100888 indio_dev->groups[indio_dev->groupcounter++] =
889 &indio_dev->chan_attr_group;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100890
Jonathan Cameron1d892712011-05-18 14:40:51 +0100891 return 0;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100892
Jonathan Cameron1d892712011-05-18 14:40:51 +0100893error_clear_attrs:
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100894 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100895
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100896 return ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100897}
898
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100899static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100900{
Jonathan Cameron1d892712011-05-18 14:40:51 +0100901
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100902 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100903 kfree(indio_dev->chan_attr_group.attrs);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100904}
905
Jonathan Cameron847ec802009-08-18 18:06:19 +0100906static void iio_dev_release(struct device *device)
907{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200908 struct iio_dev *indio_dev = dev_to_iio_dev(device);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100909 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
910 iio_device_unregister_trigger_consumer(indio_dev);
911 iio_device_unregister_eventset(indio_dev);
912 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200913
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100914 iio_buffer_put(indio_dev->buffer);
915
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200916 ida_simple_remove(&iio_ida, indio_dev->id);
917 kfree(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100918}
919
Guenter Roeck17d82b42013-02-07 17:09:00 +0000920struct device_type iio_device_type = {
Jonathan Cameron847ec802009-08-18 18:06:19 +0100921 .name = "iio_device",
922 .release = iio_dev_release,
923};
924
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200925struct iio_dev *iio_device_alloc(int sizeof_priv)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100926{
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +0100927 struct iio_dev *dev;
928 size_t alloc_size;
929
930 alloc_size = sizeof(struct iio_dev);
931 if (sizeof_priv) {
932 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
933 alloc_size += sizeof_priv;
934 }
935 /* ensure 32-byte alignment of whole construct ? */
936 alloc_size += IIO_ALIGN - 1;
937
938 dev = kzalloc(alloc_size, GFP_KERNEL);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100939
940 if (dev) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100941 dev->dev.groups = dev->groups;
Guenter Roeck17d82b42013-02-07 17:09:00 +0000942 dev->dev.type = &iio_device_type;
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100943 dev->dev.bus = &iio_bus_type;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100944 device_initialize(&dev->dev);
945 dev_set_drvdata(&dev->dev, (void *)dev);
946 mutex_init(&dev->mlock);
Jonathan Cameronac917a82012-02-15 19:48:00 +0000947 mutex_init(&dev->info_exist_lock);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200948 INIT_LIST_HEAD(&dev->channel_attr_list);
Jonathan Camerona9e39f92011-09-14 13:01:25 +0100949
950 dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
951 if (dev->id < 0) {
952 /* cannot use a dev_err as the name isn't available */
Sachin Kamat3176dd52013-10-24 12:53:00 +0100953 pr_err("failed to get device id\n");
Jonathan Camerona9e39f92011-09-14 13:01:25 +0100954 kfree(dev);
955 return NULL;
956 }
957 dev_set_name(&dev->dev, "iio:device%d", dev->id);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100958 INIT_LIST_HEAD(&dev->buffer_list);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100959 }
960
961 return dev;
962}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200963EXPORT_SYMBOL(iio_device_alloc);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100964
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200965void iio_device_free(struct iio_dev *dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100966{
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200967 if (dev)
968 put_device(&dev->dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100969}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200970EXPORT_SYMBOL(iio_device_free);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100971
Grygorii Strashko9dabaf52013-07-18 11:19:00 +0100972static void devm_iio_device_release(struct device *dev, void *res)
973{
974 iio_device_free(*(struct iio_dev **)res);
975}
976
977static int devm_iio_device_match(struct device *dev, void *res, void *data)
978{
979 struct iio_dev **r = res;
980 if (!r || !*r) {
981 WARN_ON(!r || !*r);
982 return 0;
983 }
984 return *r == data;
985}
986
987struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
988{
989 struct iio_dev **ptr, *iio_dev;
990
991 ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
992 GFP_KERNEL);
993 if (!ptr)
994 return NULL;
995
996 /* use raw alloc_dr for kmalloc caller tracing */
997 iio_dev = iio_device_alloc(sizeof_priv);
998 if (iio_dev) {
999 *ptr = iio_dev;
1000 devres_add(dev, ptr);
1001 } else {
1002 devres_free(ptr);
1003 }
1004
1005 return iio_dev;
1006}
1007EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
1008
1009void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
1010{
1011 int rc;
1012
1013 rc = devres_release(dev, devm_iio_device_release,
1014 devm_iio_device_match, iio_dev);
1015 WARN_ON(rc);
1016}
1017EXPORT_SYMBOL_GPL(devm_iio_device_free);
1018
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001019/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001020 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001021 **/
1022static int iio_chrdev_open(struct inode *inode, struct file *filp)
1023{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001024 struct iio_dev *indio_dev = container_of(inode->i_cdev,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001025 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001026
1027 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
1028 return -EBUSY;
1029
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001030 iio_device_get(indio_dev);
1031
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001032 filp->private_data = indio_dev;
Jonathan Cameron30eb82f2011-09-21 11:16:02 +01001033
Lars-Peter Clausen79335142011-12-19 15:23:49 +01001034 return 0;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001035}
1036
1037/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001038 * iio_chrdev_release() - chrdev file close buffer access and ioctls
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001039 **/
1040static int iio_chrdev_release(struct inode *inode, struct file *filp)
1041{
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001042 struct iio_dev *indio_dev = container_of(inode->i_cdev,
1043 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001044 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001045 iio_device_put(indio_dev);
1046
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001047 return 0;
1048}
1049
1050/* Somewhat of a cross file organization violation - ioctls here are actually
1051 * event related */
1052static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1053{
1054 struct iio_dev *indio_dev = filp->private_data;
1055 int __user *ip = (int __user *)arg;
1056 int fd;
1057
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +01001058 if (!indio_dev->info)
1059 return -ENODEV;
1060
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001061 if (cmd == IIO_GET_EVENT_FD_IOCTL) {
1062 fd = iio_event_getfd(indio_dev);
1063 if (copy_to_user(ip, &fd, sizeof(fd)))
1064 return -EFAULT;
1065 return 0;
1066 }
1067 return -EINVAL;
1068}
1069
Jonathan Cameron14555b12011-09-21 11:15:57 +01001070static const struct file_operations iio_buffer_fileops = {
1071 .read = iio_buffer_read_first_n_outer_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001072 .release = iio_chrdev_release,
1073 .open = iio_chrdev_open,
Jonathan Cameron14555b12011-09-21 11:15:57 +01001074 .poll = iio_buffer_poll_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001075 .owner = THIS_MODULE,
1076 .llseek = noop_llseek,
1077 .unlocked_ioctl = iio_ioctl,
1078 .compat_ioctl = iio_ioctl,
1079};
1080
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001081static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1082
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001083int iio_device_register(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001084{
1085 int ret;
1086
Guenter Roeck17d82b42013-02-07 17:09:00 +00001087 /* If the calling driver did not initialize of_node, do it here */
1088 if (!indio_dev->dev.of_node && indio_dev->dev.parent)
1089 indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
1090
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001091 /* configure elements for the chrdev */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001092 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001093
Michael Henneriche553f182012-03-01 10:51:03 +01001094 ret = iio_device_register_debugfs(indio_dev);
1095 if (ret) {
1096 dev_err(indio_dev->dev.parent,
1097 "Failed to register debugfs interfaces\n");
1098 goto error_ret;
1099 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001100 ret = iio_device_register_sysfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001101 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001102 dev_err(indio_dev->dev.parent,
Jonathan Cameron847ec802009-08-18 18:06:19 +01001103 "Failed to register sysfs interfaces\n");
Michael Henneriche553f182012-03-01 10:51:03 +01001104 goto error_unreg_debugfs;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001105 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001106 ret = iio_device_register_eventset(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001107 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001108 dev_err(indio_dev->dev.parent,
Roel Van Nyenc849d252010-04-29 19:27:31 +02001109 "Failed to register event set\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001110 goto error_free_sysfs;
1111 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001112 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
1113 iio_device_register_trigger_consumer(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001114
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001115 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1116 indio_dev->setup_ops == NULL)
1117 indio_dev->setup_ops = &noop_ring_setup_ops;
1118
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001119 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
1120 indio_dev->chrdev.owner = indio_dev->info->driver_module;
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001121 indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001122 ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001123 if (ret < 0)
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001124 goto error_unreg_eventset;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001125
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001126 ret = device_add(&indio_dev->dev);
1127 if (ret < 0)
1128 goto error_cdev_del;
1129
1130 return 0;
1131error_cdev_del:
1132 cdev_del(&indio_dev->chrdev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001133error_unreg_eventset:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001134 iio_device_unregister_eventset(indio_dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001135error_free_sysfs:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001136 iio_device_unregister_sysfs(indio_dev);
Michael Henneriche553f182012-03-01 10:51:03 +01001137error_unreg_debugfs:
1138 iio_device_unregister_debugfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001139error_ret:
1140 return ret;
1141}
1142EXPORT_SYMBOL(iio_device_register);
1143
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001144void iio_device_unregister(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001145{
Jonathan Cameronac917a82012-02-15 19:48:00 +00001146 mutex_lock(&indio_dev->info_exist_lock);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001147
1148 device_del(&indio_dev->dev);
1149
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001150 if (indio_dev->chrdev.dev)
1151 cdev_del(&indio_dev->chrdev);
Lars-Peter Clausenbc4c9612013-09-21 16:21:00 +01001152 iio_device_unregister_debugfs(indio_dev);
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001153
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001154 iio_disable_all_buffers(indio_dev);
1155
Jonathan Cameronac917a82012-02-15 19:48:00 +00001156 indio_dev->info = NULL;
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +01001157
1158 iio_device_wakeup_eventset(indio_dev);
1159 iio_buffer_wakeup_poll(indio_dev);
1160
Jonathan Cameronac917a82012-02-15 19:48:00 +00001161 mutex_unlock(&indio_dev->info_exist_lock);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001162}
1163EXPORT_SYMBOL(iio_device_unregister);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001164subsys_initcall(iio_init);
1165module_exit(iio_exit);
1166
Jonathan Cameronc8b95952012-09-02 21:27:56 +01001167MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001168MODULE_DESCRIPTION("Industrial I/O core");
1169MODULE_LICENSE("GPL");