blob: acc911a836ca19e94d6ef0e0cebaee1df9935888 [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",
Jonathan Cameron1d892712011-05-18 14:40:51 +010087};
88
89/* relies on pairs of these shared then separate */
90static const char * const iio_chan_info_postfix[] = {
Jonathan Cameron75a973c2012-04-15 17:41:30 +010091 [IIO_CHAN_INFO_RAW] = "raw",
92 [IIO_CHAN_INFO_PROCESSED] = "input",
Jonathan Cameronc8a9f802011-10-26 17:41:36 +010093 [IIO_CHAN_INFO_SCALE] = "scale",
94 [IIO_CHAN_INFO_OFFSET] = "offset",
95 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
96 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
97 [IIO_CHAN_INFO_PEAK] = "peak_raw",
98 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
99 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
100 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
Jonathan Camerondf94aba2011-11-27 11:39:12 +0000101 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
102 = "filter_low_pass_3db_frequency",
Laxman Dewangance85a1c2012-04-13 16:03:31 +0530103 [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
Michael Hennericha6b12852012-04-27 10:58:34 +0200104 [IIO_CHAN_INFO_FREQUENCY] = "frequency",
105 [IIO_CHAN_INFO_PHASE] = "phase",
Michael Hennerichb65d6212012-05-11 11:36:53 +0200106 [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
srinivas pandruvada7c9ab032012-09-05 13:56:00 +0100107 [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
Peter Meerwald899d90b2013-09-08 16:20:00 +0100108 [IIO_CHAN_INFO_INT_TIME] = "integration_time",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100109};
110
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000111/**
112 * iio_find_channel_from_si() - get channel from its scan index
113 * @indio_dev: device
114 * @si: scan index to match
115 */
Jonathan Cameron5fb21c82011-12-05 21:37:10 +0000116const struct iio_chan_spec
117*iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
118{
119 int i;
120
121 for (i = 0; i < indio_dev->num_channels; i++)
122 if (indio_dev->channels[i].scan_index == si)
123 return &indio_dev->channels[i];
124 return NULL;
125}
126
Jonathan Cameron847ec802009-08-18 18:06:19 +0100127/* This turns up an awful lot */
128ssize_t iio_read_const_attr(struct device *dev,
129 struct device_attribute *attr,
130 char *buf)
131{
132 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
133}
134EXPORT_SYMBOL(iio_read_const_attr);
135
Jonathan Cameron847ec802009-08-18 18:06:19 +0100136static int __init iio_init(void)
137{
138 int ret;
139
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100140 /* Register sysfs bus */
141 ret = bus_register(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100142 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100143 pr_err("could not register bus type\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +0100144 goto error_nothing;
145 }
146
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100147 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
148 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100149 pr_err("failed to allocate char dev region\n");
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100150 goto error_unregister_bus_type;
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100151 }
Jonathan Cameron847ec802009-08-18 18:06:19 +0100152
Michael Henneriche553f182012-03-01 10:51:03 +0100153 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
154
Jonathan Cameron847ec802009-08-18 18:06:19 +0100155 return 0;
156
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100157error_unregister_bus_type:
158 bus_unregister(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100159error_nothing:
160 return ret;
161}
162
163static void __exit iio_exit(void)
164{
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100165 if (iio_devt)
166 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100167 bus_unregister(&iio_bus_type);
Michael Henneriche553f182012-03-01 10:51:03 +0100168 debugfs_remove(iio_debugfs_dentry);
169}
170
171#if defined(CONFIG_DEBUG_FS)
Michael Henneriche553f182012-03-01 10:51:03 +0100172static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
173 size_t count, loff_t *ppos)
174{
175 struct iio_dev *indio_dev = file->private_data;
176 char buf[20];
177 unsigned val = 0;
178 ssize_t len;
179 int ret;
180
181 ret = indio_dev->info->debugfs_reg_access(indio_dev,
182 indio_dev->cached_reg_addr,
183 0, &val);
184 if (ret)
185 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
186
187 len = snprintf(buf, sizeof(buf), "0x%X\n", val);
188
189 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
190}
191
192static ssize_t iio_debugfs_write_reg(struct file *file,
193 const char __user *userbuf, size_t count, loff_t *ppos)
194{
195 struct iio_dev *indio_dev = file->private_data;
196 unsigned reg, val;
197 char buf[80];
198 int ret;
199
200 count = min_t(size_t, count, (sizeof(buf)-1));
201 if (copy_from_user(buf, userbuf, count))
202 return -EFAULT;
203
204 buf[count] = 0;
205
206 ret = sscanf(buf, "%i %i", &reg, &val);
207
208 switch (ret) {
209 case 1:
210 indio_dev->cached_reg_addr = reg;
211 break;
212 case 2:
213 indio_dev->cached_reg_addr = reg;
214 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
215 val, NULL);
216 if (ret) {
217 dev_err(indio_dev->dev.parent, "%s: write failed\n",
218 __func__);
219 return ret;
220 }
221 break;
222 default:
223 return -EINVAL;
224 }
225
226 return count;
227}
228
229static const struct file_operations iio_debugfs_reg_fops = {
Axel Lin5a28c872012-05-03 09:50:51 +0800230 .open = simple_open,
Michael Henneriche553f182012-03-01 10:51:03 +0100231 .read = iio_debugfs_read_reg,
232 .write = iio_debugfs_write_reg,
233};
234
235static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
236{
237 debugfs_remove_recursive(indio_dev->debugfs_dentry);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100238}
239
Michael Henneriche553f182012-03-01 10:51:03 +0100240static int iio_device_register_debugfs(struct iio_dev *indio_dev)
241{
242 struct dentry *d;
243
244 if (indio_dev->info->debugfs_reg_access == NULL)
245 return 0;
246
Axel Linabd5a2f2012-05-03 22:56:58 +0800247 if (!iio_debugfs_dentry)
Michael Henneriche553f182012-03-01 10:51:03 +0100248 return 0;
249
250 indio_dev->debugfs_dentry =
251 debugfs_create_dir(dev_name(&indio_dev->dev),
252 iio_debugfs_dentry);
Michael Henneriche553f182012-03-01 10:51:03 +0100253 if (indio_dev->debugfs_dentry == NULL) {
254 dev_warn(indio_dev->dev.parent,
255 "Failed to create debugfs directory\n");
256 return -EFAULT;
257 }
258
259 d = debugfs_create_file("direct_reg_access", 0644,
260 indio_dev->debugfs_dentry,
261 indio_dev, &iio_debugfs_reg_fops);
262 if (!d) {
263 iio_device_unregister_debugfs(indio_dev);
264 return -ENOMEM;
265 }
266
267 return 0;
268}
269#else
270static int iio_device_register_debugfs(struct iio_dev *indio_dev)
271{
272 return 0;
273}
274
275static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
276{
277}
278#endif /* CONFIG_DEBUG_FS */
279
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100280static ssize_t iio_read_channel_ext_info(struct device *dev,
281 struct device_attribute *attr,
282 char *buf)
283{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200284 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100285 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
286 const struct iio_chan_spec_ext_info *ext_info;
287
288 ext_info = &this_attr->c->ext_info[this_attr->address];
289
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200290 return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100291}
292
293static ssize_t iio_write_channel_ext_info(struct device *dev,
294 struct device_attribute *attr,
295 const char *buf,
296 size_t len)
297{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200298 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100299 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
300 const struct iio_chan_spec_ext_info *ext_info;
301
302 ext_info = &this_attr->c->ext_info[this_attr->address];
303
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200304 return ext_info->write(indio_dev, ext_info->private,
305 this_attr->c, buf, len);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100306}
307
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200308ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
309 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
310{
311 const struct iio_enum *e = (const struct iio_enum *)priv;
312 unsigned int i;
313 size_t len = 0;
314
315 if (!e->num_items)
316 return 0;
317
318 for (i = 0; i < e->num_items; ++i)
Lars-Peter Clausen74dcd432012-06-05 18:24:12 +0200319 len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200320
321 /* replace last space with a newline */
322 buf[len - 1] = '\n';
323
324 return len;
325}
326EXPORT_SYMBOL_GPL(iio_enum_available_read);
327
328ssize_t iio_enum_read(struct iio_dev *indio_dev,
329 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
330{
331 const struct iio_enum *e = (const struct iio_enum *)priv;
332 int i;
333
334 if (!e->get)
335 return -EINVAL;
336
337 i = e->get(indio_dev, chan);
338 if (i < 0)
339 return i;
340 else if (i >= e->num_items)
341 return -EINVAL;
342
343 return sprintf(buf, "%s\n", e->items[i]);
344}
345EXPORT_SYMBOL_GPL(iio_enum_read);
346
347ssize_t iio_enum_write(struct iio_dev *indio_dev,
348 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
349 size_t len)
350{
351 const struct iio_enum *e = (const struct iio_enum *)priv;
352 unsigned int i;
353 int ret;
354
355 if (!e->set)
356 return -EINVAL;
357
358 for (i = 0; i < e->num_items; i++) {
359 if (sysfs_streq(buf, e->items[i]))
360 break;
361 }
362
363 if (i == e->num_items)
364 return -EINVAL;
365
366 ret = e->set(indio_dev, chan, i);
367 return ret ? ret : len;
368}
369EXPORT_SYMBOL_GPL(iio_enum_write);
370
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100371/**
372 * iio_format_value() - Formats a IIO value into its string representation
373 * @buf: The buffer to which the formated value gets written
374 * @type: One of the IIO_VAL_... constants. This decides how the val and val2
375 * parameters are formatted.
376 * @val: First part of the value, exact meaning depends on the type parameter.
377 * @val2: Second part of the value, exact meaning depends on the type parameter.
378 */
379ssize_t iio_format_value(char *buf, unsigned int type, int val, int val2)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100380{
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100381 unsigned long long tmp;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200382 bool scale_db = false;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100383
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100384 switch (type) {
Michael Hennerich67eedba2012-05-11 11:36:52 +0200385 case IIO_VAL_INT:
Jonathan Cameron1d892712011-05-18 14:40:51 +0100386 return sprintf(buf, "%d\n", val);
Michael Hennerich67eedba2012-05-11 11:36:52 +0200387 case IIO_VAL_INT_PLUS_MICRO_DB:
388 scale_db = true;
389 case IIO_VAL_INT_PLUS_MICRO:
Jonathan Cameron1d892712011-05-18 14:40:51 +0100390 if (val2 < 0)
Oleksandr Kravchenkod9a01342013-07-22 12:16:00 +0100391 return sprintf(buf, "-%ld.%06u%s\n", abs(val), -val2,
Michael Hennerich67eedba2012-05-11 11:36:52 +0200392 scale_db ? " dB" : "");
Jonathan Cameron1d892712011-05-18 14:40:51 +0100393 else
Michael Hennerich67eedba2012-05-11 11:36:52 +0200394 return sprintf(buf, "%d.%06u%s\n", val, val2,
395 scale_db ? " dB" : "");
396 case IIO_VAL_INT_PLUS_NANO:
Michael Hennerich71646e22011-06-27 13:07:07 +0100397 if (val2 < 0)
Oleksandr Kravchenkod9a01342013-07-22 12:16:00 +0100398 return sprintf(buf, "-%ld.%09u\n", abs(val), -val2);
Michael Hennerich71646e22011-06-27 13:07:07 +0100399 else
400 return sprintf(buf, "%d.%09u\n", val, val2);
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100401 case IIO_VAL_FRACTIONAL:
402 tmp = div_s64((s64)val * 1000000000LL, val2);
403 val2 = do_div(tmp, 1000000000LL);
404 val = tmp;
405 return sprintf(buf, "%d.%09u\n", val, val2);
Lars-Peter Clausen103d9fb2012-10-16 17:29:00 +0100406 case IIO_VAL_FRACTIONAL_LOG2:
407 tmp = (s64)val * 1000000000LL >> val2;
408 val2 = do_div(tmp, 1000000000LL);
409 val = tmp;
410 return sprintf(buf, "%d.%09u\n", val, val2);
Michael Hennerich67eedba2012-05-11 11:36:52 +0200411 default:
Jonathan Cameron1d892712011-05-18 14:40:51 +0100412 return 0;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200413 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100414}
415
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100416static ssize_t iio_read_channel_info(struct device *dev,
417 struct device_attribute *attr,
418 char *buf)
419{
420 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
421 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
422 int val, val2;
423 int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
424 &val, &val2, this_attr->address);
425
426 if (ret < 0)
427 return ret;
428
429 return iio_format_value(buf, ret, val, val2);
430}
431
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000432/**
433 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
434 * @str: The string to parse
435 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
436 * @integer: The integer part of the number
437 * @fract: The fractional part of the number
438 *
439 * Returns 0 on success, or a negative error code if the string could not be
440 * parsed.
441 */
442int iio_str_to_fixpoint(const char *str, int fract_mult,
443 int *integer, int *fract)
444{
445 int i = 0, f = 0;
446 bool integer_part = true, negative = false;
447
448 if (str[0] == '-') {
449 negative = true;
450 str++;
451 } else if (str[0] == '+') {
452 str++;
453 }
454
455 while (*str) {
456 if ('0' <= *str && *str <= '9') {
457 if (integer_part) {
458 i = i * 10 + *str - '0';
459 } else {
460 f += fract_mult * (*str - '0');
461 fract_mult /= 10;
462 }
463 } else if (*str == '\n') {
464 if (*(str + 1) == '\0')
465 break;
466 else
467 return -EINVAL;
468 } else if (*str == '.' && integer_part) {
469 integer_part = false;
470 } else {
471 return -EINVAL;
472 }
473 str++;
474 }
475
476 if (negative) {
477 if (i)
478 i = -i;
479 else
480 f = -f;
481 }
482
483 *integer = i;
484 *fract = f;
485
486 return 0;
487}
488EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
489
Jonathan Cameron1d892712011-05-18 14:40:51 +0100490static ssize_t iio_write_channel_info(struct device *dev,
491 struct device_attribute *attr,
492 const char *buf,
493 size_t len)
494{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200495 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100496 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000497 int ret, fract_mult = 100000;
498 int integer, fract;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100499
500 /* Assumes decimal - precision based on number of digits */
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100501 if (!indio_dev->info->write_raw)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100502 return -EINVAL;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100503
504 if (indio_dev->info->write_raw_get_fmt)
505 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
506 this_attr->c, this_attr->address)) {
507 case IIO_VAL_INT_PLUS_MICRO:
508 fract_mult = 100000;
509 break;
510 case IIO_VAL_INT_PLUS_NANO:
511 fract_mult = 100000000;
512 break;
513 default:
514 return -EINVAL;
515 }
516
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000517 ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
518 if (ret)
519 return ret;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100520
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100521 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
Michael Hennerich5c04af02011-06-27 13:07:10 +0100522 integer, fract, this_attr->address);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100523 if (ret)
524 return ret;
525
526 return len;
527}
528
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100529static
Jonathan Cameron1d892712011-05-18 14:40:51 +0100530int __iio_device_attr_init(struct device_attribute *dev_attr,
531 const char *postfix,
532 struct iio_chan_spec const *chan,
533 ssize_t (*readfunc)(struct device *dev,
534 struct device_attribute *attr,
535 char *buf),
536 ssize_t (*writefunc)(struct device *dev,
537 struct device_attribute *attr,
538 const char *buf,
539 size_t len),
Jonathan Cameron37044322013-09-08 14:57:00 +0100540 enum iio_shared_by shared_by)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100541{
Jonathan Cameron37044322013-09-08 14:57:00 +0100542 int ret = 0;
543 char *name_format = NULL;
544 char *full_postfix;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100545 sysfs_attr_init(&dev_attr->attr);
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100546
547 /* Build up postfix of <extend_name>_<modifier>_postfix */
Jonathan Cameron37044322013-09-08 14:57:00 +0100548 if (chan->modified && (shared_by == IIO_SEPARATE)) {
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100549 if (chan->extend_name)
550 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
551 iio_modifier_names[chan
552 ->channel2],
553 chan->extend_name,
554 postfix);
555 else
556 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
557 iio_modifier_names[chan
558 ->channel2],
559 postfix);
560 } else {
561 if (chan->extend_name == NULL)
562 full_postfix = kstrdup(postfix, GFP_KERNEL);
563 else
564 full_postfix = kasprintf(GFP_KERNEL,
565 "%s_%s",
566 chan->extend_name,
567 postfix);
568 }
Jonathan Cameron37044322013-09-08 14:57:00 +0100569 if (full_postfix == NULL)
570 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100571
Justin P. Mattock4abf6f82012-02-29 22:00:38 -0800572 if (chan->differential) { /* Differential can not have modifier */
Jonathan Cameron37044322013-09-08 14:57:00 +0100573 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100574 case IIO_SHARED_BY_ALL:
575 name_format = kasprintf(GFP_KERNEL, "%s", full_postfix);
576 break;
577 case IIO_SHARED_BY_DIR:
578 name_format = kasprintf(GFP_KERNEL, "%s_%s",
579 iio_direction[chan->output],
580 full_postfix);
581 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100582 case IIO_SHARED_BY_TYPE:
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100583 name_format
584 = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
585 iio_direction[chan->output],
586 iio_chan_type_name_spec[chan->type],
587 iio_chan_type_name_spec[chan->type],
588 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100589 break;
590 case IIO_SEPARATE:
591 if (!chan->indexed) {
592 WARN_ON("Differential channels must be indexed\n");
593 ret = -EINVAL;
594 goto error_free_full_postfix;
595 }
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100596 name_format
Jonathan Cameron37044322013-09-08 14:57:00 +0100597 = kasprintf(GFP_KERNEL,
598 "%s_%s%d-%s%d_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100599 iio_direction[chan->output],
600 iio_chan_type_name_spec[chan->type],
601 chan->channel,
602 iio_chan_type_name_spec[chan->type],
603 chan->channel2,
604 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100605 break;
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100606 }
607 } else { /* Single ended */
Jonathan Cameron37044322013-09-08 14:57:00 +0100608 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100609 case IIO_SHARED_BY_ALL:
610 name_format = kasprintf(GFP_KERNEL, "%s", full_postfix);
611 break;
612 case IIO_SHARED_BY_DIR:
613 name_format = kasprintf(GFP_KERNEL, "%s_%s",
614 iio_direction[chan->output],
615 full_postfix);
616 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100617 case IIO_SHARED_BY_TYPE:
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100618 name_format
619 = kasprintf(GFP_KERNEL, "%s_%s_%s",
620 iio_direction[chan->output],
621 iio_chan_type_name_spec[chan->type],
622 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100623 break;
624
625 case IIO_SEPARATE:
626 if (chan->indexed)
627 name_format
628 = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
629 iio_direction[chan->output],
630 iio_chan_type_name_spec[chan->type],
631 chan->channel,
632 full_postfix);
633 else
634 name_format
635 = kasprintf(GFP_KERNEL, "%s_%s_%s",
636 iio_direction[chan->output],
637 iio_chan_type_name_spec[chan->type],
638 full_postfix);
639 break;
640 }
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100641 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100642 if (name_format == NULL) {
643 ret = -ENOMEM;
644 goto error_free_full_postfix;
645 }
646 dev_attr->attr.name = kasprintf(GFP_KERNEL,
647 name_format,
648 chan->channel,
649 chan->channel2);
650 if (dev_attr->attr.name == NULL) {
651 ret = -ENOMEM;
652 goto error_free_name_format;
653 }
654
655 if (readfunc) {
656 dev_attr->attr.mode |= S_IRUGO;
657 dev_attr->show = readfunc;
658 }
659
660 if (writefunc) {
661 dev_attr->attr.mode |= S_IWUSR;
662 dev_attr->store = writefunc;
663 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100664error_free_name_format:
665 kfree(name_format);
666error_free_full_postfix:
667 kfree(full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100668
Jonathan Cameron847ec802009-08-18 18:06:19 +0100669 return ret;
670}
671
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100672static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100673{
674 kfree(dev_attr->attr.name);
675}
676
677int __iio_add_chan_devattr(const char *postfix,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100678 struct iio_chan_spec const *chan,
679 ssize_t (*readfunc)(struct device *dev,
680 struct device_attribute *attr,
681 char *buf),
682 ssize_t (*writefunc)(struct device *dev,
683 struct device_attribute *attr,
684 const char *buf,
685 size_t len),
Jonathan Camerone614a542011-09-02 17:14:41 +0100686 u64 mask,
Jonathan Cameron37044322013-09-08 14:57:00 +0100687 enum iio_shared_by shared_by,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100688 struct device *dev,
689 struct list_head *attr_list)
690{
691 int ret;
692 struct iio_dev_attr *iio_attr, *t;
693
Sachin Kamat670c1102013-10-24 12:53:00 +0100694 iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100695 if (iio_attr == NULL) {
696 ret = -ENOMEM;
697 goto error_ret;
698 }
699 ret = __iio_device_attr_init(&iio_attr->dev_attr,
700 postfix, chan,
Jonathan Cameron37044322013-09-08 14:57:00 +0100701 readfunc, writefunc, shared_by);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100702 if (ret)
703 goto error_iio_dev_attr_free;
704 iio_attr->c = chan;
705 iio_attr->address = mask;
706 list_for_each_entry(t, attr_list, l)
707 if (strcmp(t->dev_attr.attr.name,
708 iio_attr->dev_attr.attr.name) == 0) {
Jonathan Cameron37044322013-09-08 14:57:00 +0100709 if (shared_by == IIO_SEPARATE)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100710 dev_err(dev, "tried to double register : %s\n",
711 t->dev_attr.attr.name);
712 ret = -EBUSY;
713 goto error_device_attr_deinit;
714 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100715 list_add(&iio_attr->l, attr_list);
716
717 return 0;
718
719error_device_attr_deinit:
720 __iio_device_attr_deinit(&iio_attr->dev_attr);
721error_iio_dev_attr_free:
722 kfree(iio_attr);
723error_ret:
724 return ret;
725}
726
Jonathan Cameron37044322013-09-08 14:57:00 +0100727static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
728 struct iio_chan_spec const *chan,
729 enum iio_shared_by shared_by,
730 const long *infomask)
731{
732 int i, ret, attrcount = 0;
733
734 for_each_set_bit(i, infomask, sizeof(infomask)*8) {
735 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
736 chan,
737 &iio_read_channel_info,
738 &iio_write_channel_info,
739 i,
740 shared_by,
741 &indio_dev->dev,
742 &indio_dev->channel_attr_list);
743 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
744 continue;
745 else if (ret < 0)
746 return ret;
747 attrcount++;
748 }
749
750 return attrcount;
751}
752
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100753static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100754 struct iio_chan_spec const *chan)
755{
Jonathan Cameron5ccb3ad2012-04-15 17:41:16 +0100756 int ret, attrcount = 0;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100757 const struct iio_chan_spec_ext_info *ext_info;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100758
Jonathan Cameron1d892712011-05-18 14:40:51 +0100759 if (chan->channel < 0)
760 return 0;
Jonathan Cameron37044322013-09-08 14:57:00 +0100761 ret = iio_device_add_info_mask_type(indio_dev, chan,
762 IIO_SEPARATE,
763 &chan->info_mask_separate);
764 if (ret < 0)
765 return ret;
766 attrcount += ret;
767
768 ret = iio_device_add_info_mask_type(indio_dev, chan,
769 IIO_SHARED_BY_TYPE,
770 &chan->info_mask_shared_by_type);
771 if (ret < 0)
772 return ret;
773 attrcount += ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100774
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100775 ret = iio_device_add_info_mask_type(indio_dev, chan,
776 IIO_SHARED_BY_DIR,
777 &chan->info_mask_shared_by_dir);
778 if (ret < 0)
779 return ret;
780 attrcount += ret;
781
782 ret = iio_device_add_info_mask_type(indio_dev, chan,
783 IIO_SHARED_BY_ALL,
784 &chan->info_mask_shared_by_all);
785 if (ret < 0)
786 return ret;
787 attrcount += ret;
788
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100789 if (chan->ext_info) {
790 unsigned int i = 0;
791 for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
792 ret = __iio_add_chan_devattr(ext_info->name,
793 chan,
794 ext_info->read ?
795 &iio_read_channel_ext_info : NULL,
796 ext_info->write ?
797 &iio_write_channel_ext_info : NULL,
798 i,
799 ext_info->shared,
800 &indio_dev->dev,
801 &indio_dev->channel_attr_list);
802 i++;
803 if (ret == -EBUSY && ext_info->shared)
804 continue;
805
806 if (ret)
Jonathan Cameron37044322013-09-08 14:57:00 +0100807 return ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100808
809 attrcount++;
810 }
811 }
812
Jonathan Cameron37044322013-09-08 14:57:00 +0100813 return attrcount;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100814}
815
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100816/**
817 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
818 * @attr_list: List of IIO device attributes
819 *
820 * This function frees the memory allocated for each of the IIO device
821 * attributes in the list. Note: if you want to reuse the list after calling
822 * this function you have to reinitialize it using INIT_LIST_HEAD().
823 */
824void iio_free_chan_devattr_list(struct list_head *attr_list)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100825{
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100826 struct iio_dev_attr *p, *n;
827
828 list_for_each_entry_safe(p, n, attr_list, l) {
829 kfree(p->dev_attr.attr.name);
830 kfree(p);
831 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100832}
833
Jonathan Cameron1b732882011-05-18 14:41:43 +0100834static ssize_t iio_show_dev_name(struct device *dev,
835 struct device_attribute *attr,
836 char *buf)
837{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200838 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron1b732882011-05-18 14:41:43 +0100839 return sprintf(buf, "%s\n", indio_dev->name);
840}
841
842static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
843
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100844static int iio_device_register_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100845{
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100846 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100847 struct iio_dev_attr *p;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100848 struct attribute **attr;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100849
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100850 /* First count elements in any existing group */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100851 if (indio_dev->info->attrs) {
852 attr = indio_dev->info->attrs->attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100853 while (*attr++ != NULL)
854 attrcount_orig++;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100855 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100856 attrcount = attrcount_orig;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100857 /*
858 * New channel registration method - relies on the fact a group does
Peter Meerwaldd25b3802012-08-26 13:43:00 +0100859 * not need to be initialized if its name is NULL.
Jonathan Cameron1d892712011-05-18 14:40:51 +0100860 */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100861 if (indio_dev->channels)
862 for (i = 0; i < indio_dev->num_channels; i++) {
863 ret = iio_device_add_channel_sysfs(indio_dev,
864 &indio_dev
Jonathan Cameron1d892712011-05-18 14:40:51 +0100865 ->channels[i]);
866 if (ret < 0)
867 goto error_clear_attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100868 attrcount += ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100869 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100870
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100871 if (indio_dev->name)
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100872 attrcount++;
873
Thomas Meyerd83fb182011-11-29 22:08:00 +0100874 indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
875 sizeof(indio_dev->chan_attr_group.attrs[0]),
876 GFP_KERNEL);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100877 if (indio_dev->chan_attr_group.attrs == NULL) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100878 ret = -ENOMEM;
879 goto error_clear_attrs;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100880 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100881 /* Copy across original attributes */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100882 if (indio_dev->info->attrs)
883 memcpy(indio_dev->chan_attr_group.attrs,
884 indio_dev->info->attrs->attrs,
885 sizeof(indio_dev->chan_attr_group.attrs[0])
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100886 *attrcount_orig);
887 attrn = attrcount_orig;
888 /* Add all elements from the list. */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100889 list_for_each_entry(p, &indio_dev->channel_attr_list, l)
890 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
891 if (indio_dev->name)
892 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100893
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100894 indio_dev->groups[indio_dev->groupcounter++] =
895 &indio_dev->chan_attr_group;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100896
Jonathan Cameron1d892712011-05-18 14:40:51 +0100897 return 0;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100898
Jonathan Cameron1d892712011-05-18 14:40:51 +0100899error_clear_attrs:
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100900 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100901
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100902 return ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100903}
904
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100905static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100906{
Jonathan Cameron1d892712011-05-18 14:40:51 +0100907
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100908 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100909 kfree(indio_dev->chan_attr_group.attrs);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100910}
911
Jonathan Cameron847ec802009-08-18 18:06:19 +0100912static void iio_dev_release(struct device *device)
913{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200914 struct iio_dev *indio_dev = dev_to_iio_dev(device);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100915 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
916 iio_device_unregister_trigger_consumer(indio_dev);
917 iio_device_unregister_eventset(indio_dev);
918 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200919
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100920 iio_buffer_put(indio_dev->buffer);
921
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200922 ida_simple_remove(&iio_ida, indio_dev->id);
923 kfree(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100924}
925
Guenter Roeck17d82b42013-02-07 17:09:00 +0000926struct device_type iio_device_type = {
Jonathan Cameron847ec802009-08-18 18:06:19 +0100927 .name = "iio_device",
928 .release = iio_dev_release,
929};
930
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000931/**
932 * iio_device_alloc() - allocate an iio_dev from a driver
933 * @sizeof_priv: Space to allocate for private structure.
934 **/
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200935struct iio_dev *iio_device_alloc(int sizeof_priv)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100936{
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +0100937 struct iio_dev *dev;
938 size_t alloc_size;
939
940 alloc_size = sizeof(struct iio_dev);
941 if (sizeof_priv) {
942 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
943 alloc_size += sizeof_priv;
944 }
945 /* ensure 32-byte alignment of whole construct ? */
946 alloc_size += IIO_ALIGN - 1;
947
948 dev = kzalloc(alloc_size, GFP_KERNEL);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100949
950 if (dev) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100951 dev->dev.groups = dev->groups;
Guenter Roeck17d82b42013-02-07 17:09:00 +0000952 dev->dev.type = &iio_device_type;
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100953 dev->dev.bus = &iio_bus_type;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100954 device_initialize(&dev->dev);
955 dev_set_drvdata(&dev->dev, (void *)dev);
956 mutex_init(&dev->mlock);
Jonathan Cameronac917a82012-02-15 19:48:00 +0000957 mutex_init(&dev->info_exist_lock);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200958 INIT_LIST_HEAD(&dev->channel_attr_list);
Jonathan Camerona9e39f92011-09-14 13:01:25 +0100959
960 dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
961 if (dev->id < 0) {
962 /* cannot use a dev_err as the name isn't available */
Sachin Kamat3176dd52013-10-24 12:53:00 +0100963 pr_err("failed to get device id\n");
Jonathan Camerona9e39f92011-09-14 13:01:25 +0100964 kfree(dev);
965 return NULL;
966 }
967 dev_set_name(&dev->dev, "iio:device%d", dev->id);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100968 INIT_LIST_HEAD(&dev->buffer_list);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100969 }
970
971 return dev;
972}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200973EXPORT_SYMBOL(iio_device_alloc);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100974
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000975/**
976 * iio_device_free() - free an iio_dev from a driver
977 * @dev: the iio_dev associated with the device
978 **/
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200979void iio_device_free(struct iio_dev *dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100980{
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200981 if (dev)
982 put_device(&dev->dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100983}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200984EXPORT_SYMBOL(iio_device_free);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100985
Grygorii Strashko9dabaf52013-07-18 11:19:00 +0100986static void devm_iio_device_release(struct device *dev, void *res)
987{
988 iio_device_free(*(struct iio_dev **)res);
989}
990
991static int devm_iio_device_match(struct device *dev, void *res, void *data)
992{
993 struct iio_dev **r = res;
994 if (!r || !*r) {
995 WARN_ON(!r || !*r);
996 return 0;
997 }
998 return *r == data;
999}
1000
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001001/**
1002 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1003 * @dev: Device to allocate iio_dev for
1004 * @sizeof_priv: Space to allocate for private structure.
1005 *
1006 * Managed iio_device_alloc. iio_dev allocated with this function is
1007 * automatically freed on driver detach.
1008 *
1009 * If an iio_dev allocated with this function needs to be freed separately,
1010 * devm_iio_device_free() must be used.
1011 *
1012 * RETURNS:
1013 * Pointer to allocated iio_dev on success, NULL on failure.
1014 */
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001015struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
1016{
1017 struct iio_dev **ptr, *iio_dev;
1018
1019 ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
1020 GFP_KERNEL);
1021 if (!ptr)
1022 return NULL;
1023
1024 /* use raw alloc_dr for kmalloc caller tracing */
1025 iio_dev = iio_device_alloc(sizeof_priv);
1026 if (iio_dev) {
1027 *ptr = iio_dev;
1028 devres_add(dev, ptr);
1029 } else {
1030 devres_free(ptr);
1031 }
1032
1033 return iio_dev;
1034}
1035EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
1036
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001037/**
1038 * devm_iio_device_free - Resource-managed iio_device_free()
1039 * @dev: Device this iio_dev belongs to
1040 * @iio_dev: the iio_dev associated with the device
1041 *
1042 * Free iio_dev allocated with devm_iio_device_alloc().
1043 */
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001044void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
1045{
1046 int rc;
1047
1048 rc = devres_release(dev, devm_iio_device_release,
1049 devm_iio_device_match, iio_dev);
1050 WARN_ON(rc);
1051}
1052EXPORT_SYMBOL_GPL(devm_iio_device_free);
1053
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001054/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001055 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001056 **/
1057static int iio_chrdev_open(struct inode *inode, struct file *filp)
1058{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001059 struct iio_dev *indio_dev = container_of(inode->i_cdev,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001060 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001061
1062 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
1063 return -EBUSY;
1064
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001065 iio_device_get(indio_dev);
1066
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001067 filp->private_data = indio_dev;
Jonathan Cameron30eb82f2011-09-21 11:16:02 +01001068
Lars-Peter Clausen79335142011-12-19 15:23:49 +01001069 return 0;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001070}
1071
1072/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001073 * iio_chrdev_release() - chrdev file close buffer access and ioctls
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001074 **/
1075static int iio_chrdev_release(struct inode *inode, struct file *filp)
1076{
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001077 struct iio_dev *indio_dev = container_of(inode->i_cdev,
1078 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001079 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001080 iio_device_put(indio_dev);
1081
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001082 return 0;
1083}
1084
1085/* Somewhat of a cross file organization violation - ioctls here are actually
1086 * event related */
1087static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1088{
1089 struct iio_dev *indio_dev = filp->private_data;
1090 int __user *ip = (int __user *)arg;
1091 int fd;
1092
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +01001093 if (!indio_dev->info)
1094 return -ENODEV;
1095
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001096 if (cmd == IIO_GET_EVENT_FD_IOCTL) {
1097 fd = iio_event_getfd(indio_dev);
1098 if (copy_to_user(ip, &fd, sizeof(fd)))
1099 return -EFAULT;
1100 return 0;
1101 }
1102 return -EINVAL;
1103}
1104
Jonathan Cameron14555b12011-09-21 11:15:57 +01001105static const struct file_operations iio_buffer_fileops = {
1106 .read = iio_buffer_read_first_n_outer_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001107 .release = iio_chrdev_release,
1108 .open = iio_chrdev_open,
Jonathan Cameron14555b12011-09-21 11:15:57 +01001109 .poll = iio_buffer_poll_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001110 .owner = THIS_MODULE,
1111 .llseek = noop_llseek,
1112 .unlocked_ioctl = iio_ioctl,
1113 .compat_ioctl = iio_ioctl,
1114};
1115
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001116static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1117
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001118/**
1119 * iio_device_register() - register a device with the IIO subsystem
1120 * @indio_dev: Device structure filled by the device driver
1121 **/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001122int iio_device_register(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001123{
1124 int ret;
1125
Guenter Roeck17d82b42013-02-07 17:09:00 +00001126 /* If the calling driver did not initialize of_node, do it here */
1127 if (!indio_dev->dev.of_node && indio_dev->dev.parent)
1128 indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
1129
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001130 /* configure elements for the chrdev */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001131 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001132
Michael Henneriche553f182012-03-01 10:51:03 +01001133 ret = iio_device_register_debugfs(indio_dev);
1134 if (ret) {
1135 dev_err(indio_dev->dev.parent,
1136 "Failed to register debugfs interfaces\n");
1137 goto error_ret;
1138 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001139 ret = iio_device_register_sysfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001140 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001141 dev_err(indio_dev->dev.parent,
Jonathan Cameron847ec802009-08-18 18:06:19 +01001142 "Failed to register sysfs interfaces\n");
Michael Henneriche553f182012-03-01 10:51:03 +01001143 goto error_unreg_debugfs;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001144 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001145 ret = iio_device_register_eventset(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001146 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001147 dev_err(indio_dev->dev.parent,
Roel Van Nyenc849d252010-04-29 19:27:31 +02001148 "Failed to register event set\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001149 goto error_free_sysfs;
1150 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001151 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
1152 iio_device_register_trigger_consumer(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001153
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001154 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1155 indio_dev->setup_ops == NULL)
1156 indio_dev->setup_ops = &noop_ring_setup_ops;
1157
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001158 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
1159 indio_dev->chrdev.owner = indio_dev->info->driver_module;
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001160 indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001161 ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001162 if (ret < 0)
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001163 goto error_unreg_eventset;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001164
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001165 ret = device_add(&indio_dev->dev);
1166 if (ret < 0)
1167 goto error_cdev_del;
1168
1169 return 0;
1170error_cdev_del:
1171 cdev_del(&indio_dev->chrdev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001172error_unreg_eventset:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001173 iio_device_unregister_eventset(indio_dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001174error_free_sysfs:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001175 iio_device_unregister_sysfs(indio_dev);
Michael Henneriche553f182012-03-01 10:51:03 +01001176error_unreg_debugfs:
1177 iio_device_unregister_debugfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001178error_ret:
1179 return ret;
1180}
1181EXPORT_SYMBOL(iio_device_register);
1182
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001183/**
1184 * iio_device_unregister() - unregister a device from the IIO subsystem
1185 * @indio_dev: Device structure representing the device.
1186 **/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001187void iio_device_unregister(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001188{
Jonathan Cameronac917a82012-02-15 19:48:00 +00001189 mutex_lock(&indio_dev->info_exist_lock);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001190
1191 device_del(&indio_dev->dev);
1192
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001193 if (indio_dev->chrdev.dev)
1194 cdev_del(&indio_dev->chrdev);
Lars-Peter Clausenbc4c9612013-09-21 16:21:00 +01001195 iio_device_unregister_debugfs(indio_dev);
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001196
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001197 iio_disable_all_buffers(indio_dev);
1198
Jonathan Cameronac917a82012-02-15 19:48:00 +00001199 indio_dev->info = NULL;
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +01001200
1201 iio_device_wakeup_eventset(indio_dev);
1202 iio_buffer_wakeup_poll(indio_dev);
1203
Jonathan Cameronac917a82012-02-15 19:48:00 +00001204 mutex_unlock(&indio_dev->info_exist_lock);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001205}
1206EXPORT_SYMBOL(iio_device_unregister);
Sachin Kamat8caa07c2013-10-29 11:39:00 +00001207
1208static void devm_iio_device_unreg(struct device *dev, void *res)
1209{
1210 iio_device_unregister(*(struct iio_dev **)res);
1211}
1212
1213/**
1214 * devm_iio_device_register - Resource-managed iio_device_register()
1215 * @dev: Device to allocate iio_dev for
1216 * @indio_dev: Device structure filled by the device driver
1217 *
1218 * Managed iio_device_register. The IIO device registered with this
1219 * function is automatically unregistered on driver detach. This function
1220 * calls iio_device_register() internally. Refer to that function for more
1221 * information.
1222 *
1223 * If an iio_dev registered with this function needs to be unregistered
1224 * separately, devm_iio_device_unregister() must be used.
1225 *
1226 * RETURNS:
1227 * 0 on success, negative error number on failure.
1228 */
1229int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev)
1230{
1231 struct iio_dev **ptr;
1232 int ret;
1233
1234 ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL);
1235 if (!ptr)
1236 return -ENOMEM;
1237
1238 *ptr = indio_dev;
1239 ret = iio_device_register(indio_dev);
1240 if (!ret)
1241 devres_add(dev, ptr);
1242 else
1243 devres_free(ptr);
1244
1245 return ret;
1246}
1247EXPORT_SYMBOL_GPL(devm_iio_device_register);
1248
1249/**
1250 * devm_iio_device_unregister - Resource-managed iio_device_unregister()
1251 * @dev: Device this iio_dev belongs to
1252 * @indio_dev: the iio_dev associated with the device
1253 *
1254 * Unregister iio_dev registered with devm_iio_device_register().
1255 */
1256void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev)
1257{
1258 int rc;
1259
1260 rc = devres_release(dev, devm_iio_device_unreg,
1261 devm_iio_device_match, indio_dev);
1262 WARN_ON(rc);
1263}
1264EXPORT_SYMBOL_GPL(devm_iio_device_unregister);
1265
Jonathan Cameron847ec802009-08-18 18:06:19 +01001266subsys_initcall(iio_init);
1267module_exit(iio_exit);
1268
Jonathan Cameronc8b95952012-09-02 21:27:56 +01001269MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001270MODULE_DESCRIPTION("Industrial I/O core");
1271MODULE_LICENSE("GPL");