blob: a019a7b424cb5b7a78826c9db10c56fdc80f77bc [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
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/idr.h>
15#include <linux/kdev_t.h>
16#include <linux/err.h>
17#include <linux/device.h>
18#include <linux/fs.h>
Jonathan Cameron847ec802009-08-18 18:06:19 +010019#include <linux/poll.h>
Jonathan Cameronffc18af2009-10-12 19:18:09 +010020#include <linux/sched.h>
Jeff Mahoney4439c932009-10-12 17:10:34 -040021#include <linux/wait.h>
Jonathan Cameron847ec802009-08-18 18:06:19 +010022#include <linux/cdev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Jonathan Cameron8e7d9672011-08-30 12:32:45 +010024#include <linux/anon_inodes.h>
Michael Henneriche553f182012-03-01 10:51:03 +010025#include <linux/debugfs.h>
Jonathan Cameron06458e22012-04-25 15:54:58 +010026#include <linux/iio/iio.h>
Jonathan Camerondf9c1c42011-08-12 17:56:03 +010027#include "iio_core.h"
Jonathan Cameron6aea1c32011-08-24 17:28:38 +010028#include "iio_core_trigger.h"
Jonathan Cameron06458e22012-04-25 15:54:58 +010029#include <linux/iio/sysfs.h>
30#include <linux/iio/events.h>
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +010031#include <linux/iio/buffer.h>
Jonathan Cameron9dd1cb32011-08-30 12:41:15 +010032
Peter Meerwald99698b42012-08-26 13:43:00 +010033/* IDA to assign each registered device a unique id */
Jonathan Cameronb156cf72010-09-04 17:54:43 +010034static DEFINE_IDA(iio_ida);
Jonathan Cameron847ec802009-08-18 18:06:19 +010035
Jonathan Cameronf625cb92011-08-30 12:32:48 +010036static dev_t iio_devt;
Jonathan Cameron847ec802009-08-18 18:06:19 +010037
38#define IIO_DEV_MAX 256
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +010039struct bus_type iio_bus_type = {
Jonathan Cameron847ec802009-08-18 18:06:19 +010040 .name = "iio",
Jonathan Cameron847ec802009-08-18 18:06:19 +010041};
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +010042EXPORT_SYMBOL(iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +010043
Michael Henneriche553f182012-03-01 10:51:03 +010044static struct dentry *iio_debugfs_dentry;
45
Jonathan Cameronc6fc8062011-09-02 17:14:34 +010046static const char * const iio_direction[] = {
47 [0] = "in",
48 [1] = "out",
49};
50
Jonathan Cameronade7ef72011-09-02 17:14:45 +010051static const char * const iio_chan_type_name_spec[] = {
Jonathan Cameronc6fc8062011-09-02 17:14:34 +010052 [IIO_VOLTAGE] = "voltage",
Michael Hennerichfaf290e2011-05-18 14:42:02 +010053 [IIO_CURRENT] = "current",
54 [IIO_POWER] = "power",
Bryan Freed9bff02f2011-07-07 12:01:54 -070055 [IIO_ACCEL] = "accel",
Jonathan Cameron41ea0402011-10-05 15:27:59 +010056 [IIO_ANGL_VEL] = "anglvel",
Jonathan Cameron1d892712011-05-18 14:40:51 +010057 [IIO_MAGN] = "magn",
Bryan Freed9bff02f2011-07-07 12:01:54 -070058 [IIO_LIGHT] = "illuminance",
59 [IIO_INTENSITY] = "intensity",
Bryan Freedf09f2c82011-07-07 12:01:55 -070060 [IIO_PROXIMITY] = "proximity",
Bryan Freed9bff02f2011-07-07 12:01:54 -070061 [IIO_TEMP] = "temp",
Jonathan Cameron1d892712011-05-18 14:40:51 +010062 [IIO_INCLI] = "incli",
63 [IIO_ROT] = "rot",
Jonathan Cameron1d892712011-05-18 14:40:51 +010064 [IIO_ANGL] = "angl",
Bryan Freed9bff02f2011-07-07 12:01:54 -070065 [IIO_TIMESTAMP] = "timestamp",
Jonathan Cameron66dbe702011-09-02 17:14:43 +010066 [IIO_CAPACITANCE] = "capacitance",
Michael Hennericha6b12852012-04-27 10:58:34 +020067 [IIO_ALTVOLTAGE] = "altvoltage",
Jon Brenner21cd1fa2012-05-16 10:46:42 -050068 [IIO_CCT] = "cct",
Lars-Peter Clausenc4f0c692012-11-20 13:36:00 +000069 [IIO_PRESSURE] = "pressure",
Jonathan Cameron1d892712011-05-18 14:40:51 +010070};
71
Jonathan Cameron330c6c52011-09-02 17:14:39 +010072static const char * const iio_modifier_names[] = {
Jonathan Cameron1d892712011-05-18 14:40:51 +010073 [IIO_MOD_X] = "x",
74 [IIO_MOD_Y] = "y",
75 [IIO_MOD_Z] = "z",
Jonathan Cameron8f5879b2012-05-05 10:39:22 +010076 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
Jonathan Cameroncf82cb82012-05-05 10:56:41 +010077 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
Jonathan Cameron330c6c52011-09-02 17:14:39 +010078 [IIO_MOD_LIGHT_BOTH] = "both",
79 [IIO_MOD_LIGHT_IR] = "ir",
Jon Brenner21cd1fa2012-05-16 10:46:42 -050080 [IIO_MOD_LIGHT_CLEAR] = "clear",
81 [IIO_MOD_LIGHT_RED] = "red",
82 [IIO_MOD_LIGHT_GREEN] = "green",
83 [IIO_MOD_LIGHT_BLUE] = "blue",
Jonathan Cameron1d892712011-05-18 14:40:51 +010084};
85
86/* relies on pairs of these shared then separate */
87static const char * const iio_chan_info_postfix[] = {
Jonathan Cameron75a973c2012-04-15 17:41:30 +010088 [IIO_CHAN_INFO_RAW] = "raw",
89 [IIO_CHAN_INFO_PROCESSED] = "input",
Jonathan Cameronc8a9f802011-10-26 17:41:36 +010090 [IIO_CHAN_INFO_SCALE] = "scale",
91 [IIO_CHAN_INFO_OFFSET] = "offset",
92 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
93 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
94 [IIO_CHAN_INFO_PEAK] = "peak_raw",
95 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
96 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
97 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
Jonathan Camerondf94aba2011-11-27 11:39:12 +000098 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
99 = "filter_low_pass_3db_frequency",
Laxman Dewangance85a1c2012-04-13 16:03:31 +0530100 [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
Michael Hennericha6b12852012-04-27 10:58:34 +0200101 [IIO_CHAN_INFO_FREQUENCY] = "frequency",
102 [IIO_CHAN_INFO_PHASE] = "phase",
Michael Hennerichb65d6212012-05-11 11:36:53 +0200103 [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
srinivas pandruvada7c9ab032012-09-05 13:56:00 +0100104 [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
Peter Meerwald899d90b2013-09-08 16:20:00 +0100105 [IIO_CHAN_INFO_INT_TIME] = "integration_time",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100106};
107
Jonathan Cameron5fb21c82011-12-05 21:37:10 +0000108const struct iio_chan_spec
109*iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
110{
111 int i;
112
113 for (i = 0; i < indio_dev->num_channels; i++)
114 if (indio_dev->channels[i].scan_index == si)
115 return &indio_dev->channels[i];
116 return NULL;
117}
118
Jonathan Cameron847ec802009-08-18 18:06:19 +0100119/* This turns up an awful lot */
120ssize_t iio_read_const_attr(struct device *dev,
121 struct device_attribute *attr,
122 char *buf)
123{
124 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
125}
126EXPORT_SYMBOL(iio_read_const_attr);
127
Jonathan Cameron847ec802009-08-18 18:06:19 +0100128static int __init iio_init(void)
129{
130 int ret;
131
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100132 /* Register sysfs bus */
133 ret = bus_register(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100134 if (ret < 0) {
135 printk(KERN_ERR
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100136 "%s could not register bus type\n",
Jonathan Cameron847ec802009-08-18 18:06:19 +0100137 __FILE__);
138 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) {
143 printk(KERN_ERR "%s: failed to allocate char dev region\n",
144 __FILE__);
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100145 goto error_unregister_bus_type;
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100146 }
Jonathan Cameron847ec802009-08-18 18:06:19 +0100147
Michael Henneriche553f182012-03-01 10:51:03 +0100148 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
149
Jonathan Cameron847ec802009-08-18 18:06:19 +0100150 return 0;
151
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100152error_unregister_bus_type:
153 bus_unregister(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100154error_nothing:
155 return ret;
156}
157
158static void __exit iio_exit(void)
159{
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100160 if (iio_devt)
161 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100162 bus_unregister(&iio_bus_type);
Michael Henneriche553f182012-03-01 10:51:03 +0100163 debugfs_remove(iio_debugfs_dentry);
164}
165
166#if defined(CONFIG_DEBUG_FS)
Michael Henneriche553f182012-03-01 10:51:03 +0100167static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
168 size_t count, loff_t *ppos)
169{
170 struct iio_dev *indio_dev = file->private_data;
171 char buf[20];
172 unsigned val = 0;
173 ssize_t len;
174 int ret;
175
176 ret = indio_dev->info->debugfs_reg_access(indio_dev,
177 indio_dev->cached_reg_addr,
178 0, &val);
179 if (ret)
180 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
181
182 len = snprintf(buf, sizeof(buf), "0x%X\n", val);
183
184 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
185}
186
187static ssize_t iio_debugfs_write_reg(struct file *file,
188 const char __user *userbuf, size_t count, loff_t *ppos)
189{
190 struct iio_dev *indio_dev = file->private_data;
191 unsigned reg, val;
192 char buf[80];
193 int ret;
194
195 count = min_t(size_t, count, (sizeof(buf)-1));
196 if (copy_from_user(buf, userbuf, count))
197 return -EFAULT;
198
199 buf[count] = 0;
200
201 ret = sscanf(buf, "%i %i", &reg, &val);
202
203 switch (ret) {
204 case 1:
205 indio_dev->cached_reg_addr = reg;
206 break;
207 case 2:
208 indio_dev->cached_reg_addr = reg;
209 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
210 val, NULL);
211 if (ret) {
212 dev_err(indio_dev->dev.parent, "%s: write failed\n",
213 __func__);
214 return ret;
215 }
216 break;
217 default:
218 return -EINVAL;
219 }
220
221 return count;
222}
223
224static const struct file_operations iio_debugfs_reg_fops = {
Axel Lin5a28c872012-05-03 09:50:51 +0800225 .open = simple_open,
Michael Henneriche553f182012-03-01 10:51:03 +0100226 .read = iio_debugfs_read_reg,
227 .write = iio_debugfs_write_reg,
228};
229
230static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
231{
232 debugfs_remove_recursive(indio_dev->debugfs_dentry);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100233}
234
Michael Henneriche553f182012-03-01 10:51:03 +0100235static int iio_device_register_debugfs(struct iio_dev *indio_dev)
236{
237 struct dentry *d;
238
239 if (indio_dev->info->debugfs_reg_access == NULL)
240 return 0;
241
Axel Linabd5a2f2012-05-03 22:56:58 +0800242 if (!iio_debugfs_dentry)
Michael Henneriche553f182012-03-01 10:51:03 +0100243 return 0;
244
245 indio_dev->debugfs_dentry =
246 debugfs_create_dir(dev_name(&indio_dev->dev),
247 iio_debugfs_dentry);
Michael Henneriche553f182012-03-01 10:51:03 +0100248 if (indio_dev->debugfs_dentry == NULL) {
249 dev_warn(indio_dev->dev.parent,
250 "Failed to create debugfs directory\n");
251 return -EFAULT;
252 }
253
254 d = debugfs_create_file("direct_reg_access", 0644,
255 indio_dev->debugfs_dentry,
256 indio_dev, &iio_debugfs_reg_fops);
257 if (!d) {
258 iio_device_unregister_debugfs(indio_dev);
259 return -ENOMEM;
260 }
261
262 return 0;
263}
264#else
265static int iio_device_register_debugfs(struct iio_dev *indio_dev)
266{
267 return 0;
268}
269
270static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
271{
272}
273#endif /* CONFIG_DEBUG_FS */
274
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100275static ssize_t iio_read_channel_ext_info(struct device *dev,
276 struct device_attribute *attr,
277 char *buf)
278{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200279 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100280 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
281 const struct iio_chan_spec_ext_info *ext_info;
282
283 ext_info = &this_attr->c->ext_info[this_attr->address];
284
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200285 return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100286}
287
288static ssize_t iio_write_channel_ext_info(struct device *dev,
289 struct device_attribute *attr,
290 const char *buf,
291 size_t len)
292{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200293 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100294 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
295 const struct iio_chan_spec_ext_info *ext_info;
296
297 ext_info = &this_attr->c->ext_info[this_attr->address];
298
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200299 return ext_info->write(indio_dev, ext_info->private,
300 this_attr->c, buf, len);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100301}
302
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200303ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
304 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
305{
306 const struct iio_enum *e = (const struct iio_enum *)priv;
307 unsigned int i;
308 size_t len = 0;
309
310 if (!e->num_items)
311 return 0;
312
313 for (i = 0; i < e->num_items; ++i)
Lars-Peter Clausen74dcd432012-06-05 18:24:12 +0200314 len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200315
316 /* replace last space with a newline */
317 buf[len - 1] = '\n';
318
319 return len;
320}
321EXPORT_SYMBOL_GPL(iio_enum_available_read);
322
323ssize_t iio_enum_read(struct iio_dev *indio_dev,
324 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
325{
326 const struct iio_enum *e = (const struct iio_enum *)priv;
327 int i;
328
329 if (!e->get)
330 return -EINVAL;
331
332 i = e->get(indio_dev, chan);
333 if (i < 0)
334 return i;
335 else if (i >= e->num_items)
336 return -EINVAL;
337
338 return sprintf(buf, "%s\n", e->items[i]);
339}
340EXPORT_SYMBOL_GPL(iio_enum_read);
341
342ssize_t iio_enum_write(struct iio_dev *indio_dev,
343 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
344 size_t len)
345{
346 const struct iio_enum *e = (const struct iio_enum *)priv;
347 unsigned int i;
348 int ret;
349
350 if (!e->set)
351 return -EINVAL;
352
353 for (i = 0; i < e->num_items; i++) {
354 if (sysfs_streq(buf, e->items[i]))
355 break;
356 }
357
358 if (i == e->num_items)
359 return -EINVAL;
360
361 ret = e->set(indio_dev, chan, i);
362 return ret ? ret : len;
363}
364EXPORT_SYMBOL_GPL(iio_enum_write);
365
Jonathan Cameron1d892712011-05-18 14:40:51 +0100366static ssize_t iio_read_channel_info(struct device *dev,
367 struct device_attribute *attr,
368 char *buf)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100369{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200370 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100371 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100372 unsigned long long tmp;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100373 int val, val2;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200374 bool scale_db = false;
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100375 int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
376 &val, &val2, this_attr->address);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100377
Jonathan Cameron1d892712011-05-18 14:40:51 +0100378 if (ret < 0)
379 return ret;
380
Michael Hennerich67eedba2012-05-11 11:36:52 +0200381 switch (ret) {
382 case IIO_VAL_INT:
Jonathan Cameron1d892712011-05-18 14:40:51 +0100383 return sprintf(buf, "%d\n", val);
Michael Hennerich67eedba2012-05-11 11:36:52 +0200384 case IIO_VAL_INT_PLUS_MICRO_DB:
385 scale_db = true;
386 case IIO_VAL_INT_PLUS_MICRO:
Jonathan Cameron1d892712011-05-18 14:40:51 +0100387 if (val2 < 0)
Oleksandr Kravchenkod9a01342013-07-22 12:16:00 +0100388 return sprintf(buf, "-%ld.%06u%s\n", abs(val), -val2,
Michael Hennerich67eedba2012-05-11 11:36:52 +0200389 scale_db ? " dB" : "");
Jonathan Cameron1d892712011-05-18 14:40:51 +0100390 else
Michael Hennerich67eedba2012-05-11 11:36:52 +0200391 return sprintf(buf, "%d.%06u%s\n", val, val2,
392 scale_db ? " dB" : "");
393 case IIO_VAL_INT_PLUS_NANO:
Michael Hennerich71646e22011-06-27 13:07:07 +0100394 if (val2 < 0)
Oleksandr Kravchenkod9a01342013-07-22 12:16:00 +0100395 return sprintf(buf, "-%ld.%09u\n", abs(val), -val2);
Michael Hennerich71646e22011-06-27 13:07:07 +0100396 else
397 return sprintf(buf, "%d.%09u\n", val, val2);
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100398 case IIO_VAL_FRACTIONAL:
399 tmp = div_s64((s64)val * 1000000000LL, val2);
400 val2 = do_div(tmp, 1000000000LL);
401 val = tmp;
402 return sprintf(buf, "%d.%09u\n", val, val2);
Lars-Peter Clausen103d9fb2012-10-16 17:29:00 +0100403 case IIO_VAL_FRACTIONAL_LOG2:
404 tmp = (s64)val * 1000000000LL >> val2;
405 val2 = do_div(tmp, 1000000000LL);
406 val = tmp;
407 return sprintf(buf, "%d.%09u\n", val, val2);
Michael Hennerich67eedba2012-05-11 11:36:52 +0200408 default:
Jonathan Cameron1d892712011-05-18 14:40:51 +0100409 return 0;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200410 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100411}
412
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000413/**
414 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
415 * @str: The string to parse
416 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
417 * @integer: The integer part of the number
418 * @fract: The fractional part of the number
419 *
420 * Returns 0 on success, or a negative error code if the string could not be
421 * parsed.
422 */
423int iio_str_to_fixpoint(const char *str, int fract_mult,
424 int *integer, int *fract)
425{
426 int i = 0, f = 0;
427 bool integer_part = true, negative = false;
428
429 if (str[0] == '-') {
430 negative = true;
431 str++;
432 } else if (str[0] == '+') {
433 str++;
434 }
435
436 while (*str) {
437 if ('0' <= *str && *str <= '9') {
438 if (integer_part) {
439 i = i * 10 + *str - '0';
440 } else {
441 f += fract_mult * (*str - '0');
442 fract_mult /= 10;
443 }
444 } else if (*str == '\n') {
445 if (*(str + 1) == '\0')
446 break;
447 else
448 return -EINVAL;
449 } else if (*str == '.' && integer_part) {
450 integer_part = false;
451 } else {
452 return -EINVAL;
453 }
454 str++;
455 }
456
457 if (negative) {
458 if (i)
459 i = -i;
460 else
461 f = -f;
462 }
463
464 *integer = i;
465 *fract = f;
466
467 return 0;
468}
469EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
470
Jonathan Cameron1d892712011-05-18 14:40:51 +0100471static ssize_t iio_write_channel_info(struct device *dev,
472 struct device_attribute *attr,
473 const char *buf,
474 size_t len)
475{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200476 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100477 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000478 int ret, fract_mult = 100000;
479 int integer, fract;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100480
481 /* Assumes decimal - precision based on number of digits */
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100482 if (!indio_dev->info->write_raw)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100483 return -EINVAL;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100484
485 if (indio_dev->info->write_raw_get_fmt)
486 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
487 this_attr->c, this_attr->address)) {
488 case IIO_VAL_INT_PLUS_MICRO:
489 fract_mult = 100000;
490 break;
491 case IIO_VAL_INT_PLUS_NANO:
492 fract_mult = 100000000;
493 break;
494 default:
495 return -EINVAL;
496 }
497
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000498 ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
499 if (ret)
500 return ret;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100501
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100502 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
Michael Hennerich5c04af02011-06-27 13:07:10 +0100503 integer, fract, this_attr->address);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100504 if (ret)
505 return ret;
506
507 return len;
508}
509
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100510static
Jonathan Cameron1d892712011-05-18 14:40:51 +0100511int __iio_device_attr_init(struct device_attribute *dev_attr,
512 const char *postfix,
513 struct iio_chan_spec const *chan,
514 ssize_t (*readfunc)(struct device *dev,
515 struct device_attribute *attr,
516 char *buf),
517 ssize_t (*writefunc)(struct device *dev,
518 struct device_attribute *attr,
519 const char *buf,
520 size_t len),
Jonathan Cameron37044322013-09-08 14:57:00 +0100521 enum iio_shared_by shared_by)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100522{
Jonathan Cameron37044322013-09-08 14:57:00 +0100523 int ret = 0;
524 char *name_format = NULL;
525 char *full_postfix;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100526 sysfs_attr_init(&dev_attr->attr);
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100527
528 /* Build up postfix of <extend_name>_<modifier>_postfix */
Jonathan Cameron37044322013-09-08 14:57:00 +0100529 if (chan->modified && (shared_by == IIO_SEPARATE)) {
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100530 if (chan->extend_name)
531 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
532 iio_modifier_names[chan
533 ->channel2],
534 chan->extend_name,
535 postfix);
536 else
537 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
538 iio_modifier_names[chan
539 ->channel2],
540 postfix);
541 } else {
542 if (chan->extend_name == NULL)
543 full_postfix = kstrdup(postfix, GFP_KERNEL);
544 else
545 full_postfix = kasprintf(GFP_KERNEL,
546 "%s_%s",
547 chan->extend_name,
548 postfix);
549 }
Jonathan Cameron37044322013-09-08 14:57:00 +0100550 if (full_postfix == NULL)
551 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100552
Justin P. Mattock4abf6f82012-02-29 22:00:38 -0800553 if (chan->differential) { /* Differential can not have modifier */
Jonathan Cameron37044322013-09-08 14:57:00 +0100554 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100555 case IIO_SHARED_BY_ALL:
556 name_format = kasprintf(GFP_KERNEL, "%s", full_postfix);
557 break;
558 case IIO_SHARED_BY_DIR:
559 name_format = kasprintf(GFP_KERNEL, "%s_%s",
560 iio_direction[chan->output],
561 full_postfix);
562 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100563 case IIO_SHARED_BY_TYPE:
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100564 name_format
565 = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
566 iio_direction[chan->output],
567 iio_chan_type_name_spec[chan->type],
568 iio_chan_type_name_spec[chan->type],
569 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100570 break;
571 case IIO_SEPARATE:
572 if (!chan->indexed) {
573 WARN_ON("Differential channels must be indexed\n");
574 ret = -EINVAL;
575 goto error_free_full_postfix;
576 }
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100577 name_format
Jonathan Cameron37044322013-09-08 14:57:00 +0100578 = kasprintf(GFP_KERNEL,
579 "%s_%s%d-%s%d_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100580 iio_direction[chan->output],
581 iio_chan_type_name_spec[chan->type],
582 chan->channel,
583 iio_chan_type_name_spec[chan->type],
584 chan->channel2,
585 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100586 break;
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100587 }
588 } else { /* Single ended */
Jonathan Cameron37044322013-09-08 14:57:00 +0100589 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100590 case IIO_SHARED_BY_ALL:
591 name_format = kasprintf(GFP_KERNEL, "%s", full_postfix);
592 break;
593 case IIO_SHARED_BY_DIR:
594 name_format = kasprintf(GFP_KERNEL, "%s_%s",
595 iio_direction[chan->output],
596 full_postfix);
597 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100598 case IIO_SHARED_BY_TYPE:
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100599 name_format
600 = kasprintf(GFP_KERNEL, "%s_%s_%s",
601 iio_direction[chan->output],
602 iio_chan_type_name_spec[chan->type],
603 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100604 break;
605
606 case IIO_SEPARATE:
607 if (chan->indexed)
608 name_format
609 = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
610 iio_direction[chan->output],
611 iio_chan_type_name_spec[chan->type],
612 chan->channel,
613 full_postfix);
614 else
615 name_format
616 = kasprintf(GFP_KERNEL, "%s_%s_%s",
617 iio_direction[chan->output],
618 iio_chan_type_name_spec[chan->type],
619 full_postfix);
620 break;
621 }
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100622 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100623 if (name_format == NULL) {
624 ret = -ENOMEM;
625 goto error_free_full_postfix;
626 }
627 dev_attr->attr.name = kasprintf(GFP_KERNEL,
628 name_format,
629 chan->channel,
630 chan->channel2);
631 if (dev_attr->attr.name == NULL) {
632 ret = -ENOMEM;
633 goto error_free_name_format;
634 }
635
636 if (readfunc) {
637 dev_attr->attr.mode |= S_IRUGO;
638 dev_attr->show = readfunc;
639 }
640
641 if (writefunc) {
642 dev_attr->attr.mode |= S_IWUSR;
643 dev_attr->store = writefunc;
644 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100645error_free_name_format:
646 kfree(name_format);
647error_free_full_postfix:
648 kfree(full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100649
Jonathan Cameron847ec802009-08-18 18:06:19 +0100650 return ret;
651}
652
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100653static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100654{
655 kfree(dev_attr->attr.name);
656}
657
658int __iio_add_chan_devattr(const char *postfix,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100659 struct iio_chan_spec const *chan,
660 ssize_t (*readfunc)(struct device *dev,
661 struct device_attribute *attr,
662 char *buf),
663 ssize_t (*writefunc)(struct device *dev,
664 struct device_attribute *attr,
665 const char *buf,
666 size_t len),
Jonathan Camerone614a542011-09-02 17:14:41 +0100667 u64 mask,
Jonathan Cameron37044322013-09-08 14:57:00 +0100668 enum iio_shared_by shared_by,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100669 struct device *dev,
670 struct list_head *attr_list)
671{
672 int ret;
673 struct iio_dev_attr *iio_attr, *t;
674
675 iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL);
676 if (iio_attr == NULL) {
677 ret = -ENOMEM;
678 goto error_ret;
679 }
680 ret = __iio_device_attr_init(&iio_attr->dev_attr,
681 postfix, chan,
Jonathan Cameron37044322013-09-08 14:57:00 +0100682 readfunc, writefunc, shared_by);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100683 if (ret)
684 goto error_iio_dev_attr_free;
685 iio_attr->c = chan;
686 iio_attr->address = mask;
687 list_for_each_entry(t, attr_list, l)
688 if (strcmp(t->dev_attr.attr.name,
689 iio_attr->dev_attr.attr.name) == 0) {
Jonathan Cameron37044322013-09-08 14:57:00 +0100690 if (shared_by == IIO_SEPARATE)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100691 dev_err(dev, "tried to double register : %s\n",
692 t->dev_attr.attr.name);
693 ret = -EBUSY;
694 goto error_device_attr_deinit;
695 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100696 list_add(&iio_attr->l, attr_list);
697
698 return 0;
699
700error_device_attr_deinit:
701 __iio_device_attr_deinit(&iio_attr->dev_attr);
702error_iio_dev_attr_free:
703 kfree(iio_attr);
704error_ret:
705 return ret;
706}
707
Jonathan Cameron37044322013-09-08 14:57:00 +0100708static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
709 struct iio_chan_spec const *chan,
710 enum iio_shared_by shared_by,
711 const long *infomask)
712{
713 int i, ret, attrcount = 0;
714
715 for_each_set_bit(i, infomask, sizeof(infomask)*8) {
716 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
717 chan,
718 &iio_read_channel_info,
719 &iio_write_channel_info,
720 i,
721 shared_by,
722 &indio_dev->dev,
723 &indio_dev->channel_attr_list);
724 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
725 continue;
726 else if (ret < 0)
727 return ret;
728 attrcount++;
729 }
730
731 return attrcount;
732}
733
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100734static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100735 struct iio_chan_spec const *chan)
736{
Jonathan Cameron5ccb3ad2012-04-15 17:41:16 +0100737 int ret, attrcount = 0;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100738 const struct iio_chan_spec_ext_info *ext_info;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100739
Jonathan Cameron1d892712011-05-18 14:40:51 +0100740 if (chan->channel < 0)
741 return 0;
Jonathan Cameron37044322013-09-08 14:57:00 +0100742 ret = iio_device_add_info_mask_type(indio_dev, chan,
743 IIO_SEPARATE,
744 &chan->info_mask_separate);
745 if (ret < 0)
746 return ret;
747 attrcount += ret;
748
749 ret = iio_device_add_info_mask_type(indio_dev, chan,
750 IIO_SHARED_BY_TYPE,
751 &chan->info_mask_shared_by_type);
752 if (ret < 0)
753 return ret;
754 attrcount += ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100755
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100756 ret = iio_device_add_info_mask_type(indio_dev, chan,
757 IIO_SHARED_BY_DIR,
758 &chan->info_mask_shared_by_dir);
759 if (ret < 0)
760 return ret;
761 attrcount += ret;
762
763 ret = iio_device_add_info_mask_type(indio_dev, chan,
764 IIO_SHARED_BY_ALL,
765 &chan->info_mask_shared_by_all);
766 if (ret < 0)
767 return ret;
768 attrcount += ret;
769
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100770 if (chan->ext_info) {
771 unsigned int i = 0;
772 for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
773 ret = __iio_add_chan_devattr(ext_info->name,
774 chan,
775 ext_info->read ?
776 &iio_read_channel_ext_info : NULL,
777 ext_info->write ?
778 &iio_write_channel_ext_info : NULL,
779 i,
780 ext_info->shared,
781 &indio_dev->dev,
782 &indio_dev->channel_attr_list);
783 i++;
784 if (ret == -EBUSY && ext_info->shared)
785 continue;
786
787 if (ret)
Jonathan Cameron37044322013-09-08 14:57:00 +0100788 return ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100789
790 attrcount++;
791 }
792 }
793
Jonathan Cameron37044322013-09-08 14:57:00 +0100794 return attrcount;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100795}
796
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100797static void iio_device_remove_and_free_read_attr(struct iio_dev *indio_dev,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100798 struct iio_dev_attr *p)
799{
Jonathan Cameron1d892712011-05-18 14:40:51 +0100800 kfree(p->dev_attr.attr.name);
801 kfree(p);
802}
803
Jonathan Cameron1b732882011-05-18 14:41:43 +0100804static ssize_t iio_show_dev_name(struct device *dev,
805 struct device_attribute *attr,
806 char *buf)
807{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200808 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron1b732882011-05-18 14:41:43 +0100809 return sprintf(buf, "%s\n", indio_dev->name);
810}
811
812static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
813
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100814static int iio_device_register_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100815{
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100816 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100817 struct iio_dev_attr *p, *n;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100818 struct attribute **attr;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100819
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100820 /* First count elements in any existing group */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100821 if (indio_dev->info->attrs) {
822 attr = indio_dev->info->attrs->attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100823 while (*attr++ != NULL)
824 attrcount_orig++;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100825 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100826 attrcount = attrcount_orig;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100827 /*
828 * New channel registration method - relies on the fact a group does
Peter Meerwaldd25b3802012-08-26 13:43:00 +0100829 * not need to be initialized if its name is NULL.
Jonathan Cameron1d892712011-05-18 14:40:51 +0100830 */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100831 if (indio_dev->channels)
832 for (i = 0; i < indio_dev->num_channels; i++) {
833 ret = iio_device_add_channel_sysfs(indio_dev,
834 &indio_dev
Jonathan Cameron1d892712011-05-18 14:40:51 +0100835 ->channels[i]);
836 if (ret < 0)
837 goto error_clear_attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100838 attrcount += ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100839 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100840
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100841 if (indio_dev->name)
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100842 attrcount++;
843
Thomas Meyerd83fb182011-11-29 22:08:00 +0100844 indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
845 sizeof(indio_dev->chan_attr_group.attrs[0]),
846 GFP_KERNEL);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100847 if (indio_dev->chan_attr_group.attrs == NULL) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100848 ret = -ENOMEM;
849 goto error_clear_attrs;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100850 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100851 /* Copy across original attributes */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100852 if (indio_dev->info->attrs)
853 memcpy(indio_dev->chan_attr_group.attrs,
854 indio_dev->info->attrs->attrs,
855 sizeof(indio_dev->chan_attr_group.attrs[0])
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100856 *attrcount_orig);
857 attrn = attrcount_orig;
858 /* Add all elements from the list. */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100859 list_for_each_entry(p, &indio_dev->channel_attr_list, l)
860 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
861 if (indio_dev->name)
862 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100863
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100864 indio_dev->groups[indio_dev->groupcounter++] =
865 &indio_dev->chan_attr_group;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100866
Jonathan Cameron1d892712011-05-18 14:40:51 +0100867 return 0;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100868
Jonathan Cameron1d892712011-05-18 14:40:51 +0100869error_clear_attrs:
870 list_for_each_entry_safe(p, n,
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100871 &indio_dev->channel_attr_list, l) {
Jonathan Cameron1d892712011-05-18 14:40:51 +0100872 list_del(&p->l);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100873 iio_device_remove_and_free_read_attr(indio_dev, p);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100874 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100875
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100876 return ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100877}
878
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100879static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100880{
Jonathan Cameron1d892712011-05-18 14:40:51 +0100881
882 struct iio_dev_attr *p, *n;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100883
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100884 list_for_each_entry_safe(p, n, &indio_dev->channel_attr_list, l) {
Jonathan Cameron1d892712011-05-18 14:40:51 +0100885 list_del(&p->l);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100886 iio_device_remove_and_free_read_attr(indio_dev, p);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100887 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100888 kfree(indio_dev->chan_attr_group.attrs);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100889}
890
Jonathan Cameron847ec802009-08-18 18:06:19 +0100891static void iio_dev_release(struct device *device)
892{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200893 struct iio_dev *indio_dev = dev_to_iio_dev(device);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100894 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
895 iio_device_unregister_trigger_consumer(indio_dev);
896 iio_device_unregister_eventset(indio_dev);
897 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200898
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100899 iio_buffer_put(indio_dev->buffer);
900
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200901 ida_simple_remove(&iio_ida, indio_dev->id);
902 kfree(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100903}
904
Guenter Roeck17d82b42013-02-07 17:09:00 +0000905struct device_type iio_device_type = {
Jonathan Cameron847ec802009-08-18 18:06:19 +0100906 .name = "iio_device",
907 .release = iio_dev_release,
908};
909
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200910struct iio_dev *iio_device_alloc(int sizeof_priv)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100911{
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +0100912 struct iio_dev *dev;
913 size_t alloc_size;
914
915 alloc_size = sizeof(struct iio_dev);
916 if (sizeof_priv) {
917 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
918 alloc_size += sizeof_priv;
919 }
920 /* ensure 32-byte alignment of whole construct ? */
921 alloc_size += IIO_ALIGN - 1;
922
923 dev = kzalloc(alloc_size, GFP_KERNEL);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100924
925 if (dev) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100926 dev->dev.groups = dev->groups;
Guenter Roeck17d82b42013-02-07 17:09:00 +0000927 dev->dev.type = &iio_device_type;
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100928 dev->dev.bus = &iio_bus_type;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100929 device_initialize(&dev->dev);
930 dev_set_drvdata(&dev->dev, (void *)dev);
931 mutex_init(&dev->mlock);
Jonathan Cameronac917a82012-02-15 19:48:00 +0000932 mutex_init(&dev->info_exist_lock);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200933 INIT_LIST_HEAD(&dev->channel_attr_list);
Jonathan Camerona9e39f92011-09-14 13:01:25 +0100934
935 dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
936 if (dev->id < 0) {
937 /* cannot use a dev_err as the name isn't available */
938 printk(KERN_ERR "Failed to get id\n");
939 kfree(dev);
940 return NULL;
941 }
942 dev_set_name(&dev->dev, "iio:device%d", dev->id);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100943 INIT_LIST_HEAD(&dev->buffer_list);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100944 }
945
946 return dev;
947}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200948EXPORT_SYMBOL(iio_device_alloc);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100949
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200950void iio_device_free(struct iio_dev *dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100951{
Lars-Peter Clausene407fd62012-06-04 10:41:42 +0200952 if (dev)
953 put_device(&dev->dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100954}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200955EXPORT_SYMBOL(iio_device_free);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100956
Grygorii Strashko9dabaf52013-07-18 11:19:00 +0100957static void devm_iio_device_release(struct device *dev, void *res)
958{
959 iio_device_free(*(struct iio_dev **)res);
960}
961
962static int devm_iio_device_match(struct device *dev, void *res, void *data)
963{
964 struct iio_dev **r = res;
965 if (!r || !*r) {
966 WARN_ON(!r || !*r);
967 return 0;
968 }
969 return *r == data;
970}
971
972struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
973{
974 struct iio_dev **ptr, *iio_dev;
975
976 ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
977 GFP_KERNEL);
978 if (!ptr)
979 return NULL;
980
981 /* use raw alloc_dr for kmalloc caller tracing */
982 iio_dev = iio_device_alloc(sizeof_priv);
983 if (iio_dev) {
984 *ptr = iio_dev;
985 devres_add(dev, ptr);
986 } else {
987 devres_free(ptr);
988 }
989
990 return iio_dev;
991}
992EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
993
994void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
995{
996 int rc;
997
998 rc = devres_release(dev, devm_iio_device_release,
999 devm_iio_device_match, iio_dev);
1000 WARN_ON(rc);
1001}
1002EXPORT_SYMBOL_GPL(devm_iio_device_free);
1003
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001004/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001005 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001006 **/
1007static int iio_chrdev_open(struct inode *inode, struct file *filp)
1008{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001009 struct iio_dev *indio_dev = container_of(inode->i_cdev,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001010 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001011
1012 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
1013 return -EBUSY;
1014
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001015 iio_device_get(indio_dev);
1016
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001017 filp->private_data = indio_dev;
Jonathan Cameron30eb82f2011-09-21 11:16:02 +01001018
Lars-Peter Clausen79335142011-12-19 15:23:49 +01001019 return 0;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001020}
1021
1022/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001023 * iio_chrdev_release() - chrdev file close buffer access and ioctls
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001024 **/
1025static int iio_chrdev_release(struct inode *inode, struct file *filp)
1026{
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001027 struct iio_dev *indio_dev = container_of(inode->i_cdev,
1028 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001029 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001030 iio_device_put(indio_dev);
1031
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001032 return 0;
1033}
1034
1035/* Somewhat of a cross file organization violation - ioctls here are actually
1036 * event related */
1037static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1038{
1039 struct iio_dev *indio_dev = filp->private_data;
1040 int __user *ip = (int __user *)arg;
1041 int fd;
1042
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +01001043 if (!indio_dev->info)
1044 return -ENODEV;
1045
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001046 if (cmd == IIO_GET_EVENT_FD_IOCTL) {
1047 fd = iio_event_getfd(indio_dev);
1048 if (copy_to_user(ip, &fd, sizeof(fd)))
1049 return -EFAULT;
1050 return 0;
1051 }
1052 return -EINVAL;
1053}
1054
Jonathan Cameron14555b12011-09-21 11:15:57 +01001055static const struct file_operations iio_buffer_fileops = {
1056 .read = iio_buffer_read_first_n_outer_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001057 .release = iio_chrdev_release,
1058 .open = iio_chrdev_open,
Jonathan Cameron14555b12011-09-21 11:15:57 +01001059 .poll = iio_buffer_poll_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001060 .owner = THIS_MODULE,
1061 .llseek = noop_llseek,
1062 .unlocked_ioctl = iio_ioctl,
1063 .compat_ioctl = iio_ioctl,
1064};
1065
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001066static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1067
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001068int iio_device_register(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001069{
1070 int ret;
1071
Guenter Roeck17d82b42013-02-07 17:09:00 +00001072 /* If the calling driver did not initialize of_node, do it here */
1073 if (!indio_dev->dev.of_node && indio_dev->dev.parent)
1074 indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
1075
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001076 /* configure elements for the chrdev */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001077 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001078
Michael Henneriche553f182012-03-01 10:51:03 +01001079 ret = iio_device_register_debugfs(indio_dev);
1080 if (ret) {
1081 dev_err(indio_dev->dev.parent,
1082 "Failed to register debugfs interfaces\n");
1083 goto error_ret;
1084 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001085 ret = iio_device_register_sysfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001086 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001087 dev_err(indio_dev->dev.parent,
Jonathan Cameron847ec802009-08-18 18:06:19 +01001088 "Failed to register sysfs interfaces\n");
Michael Henneriche553f182012-03-01 10:51:03 +01001089 goto error_unreg_debugfs;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001090 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001091 ret = iio_device_register_eventset(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001092 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001093 dev_err(indio_dev->dev.parent,
Roel Van Nyenc849d252010-04-29 19:27:31 +02001094 "Failed to register event set\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001095 goto error_free_sysfs;
1096 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001097 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
1098 iio_device_register_trigger_consumer(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001099
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001100 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1101 indio_dev->setup_ops == NULL)
1102 indio_dev->setup_ops = &noop_ring_setup_ops;
1103
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001104 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
1105 indio_dev->chrdev.owner = indio_dev->info->driver_module;
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001106 indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001107 ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001108 if (ret < 0)
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001109 goto error_unreg_eventset;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001110
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001111 ret = device_add(&indio_dev->dev);
1112 if (ret < 0)
1113 goto error_cdev_del;
1114
1115 return 0;
1116error_cdev_del:
1117 cdev_del(&indio_dev->chrdev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001118error_unreg_eventset:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001119 iio_device_unregister_eventset(indio_dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001120error_free_sysfs:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001121 iio_device_unregister_sysfs(indio_dev);
Michael Henneriche553f182012-03-01 10:51:03 +01001122error_unreg_debugfs:
1123 iio_device_unregister_debugfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001124error_ret:
1125 return ret;
1126}
1127EXPORT_SYMBOL(iio_device_register);
1128
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001129void iio_device_unregister(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001130{
Jonathan Cameronac917a82012-02-15 19:48:00 +00001131 mutex_lock(&indio_dev->info_exist_lock);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001132
1133 device_del(&indio_dev->dev);
1134
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001135 if (indio_dev->chrdev.dev)
1136 cdev_del(&indio_dev->chrdev);
Lars-Peter Clausenbc4c9612013-09-21 16:21:00 +01001137 iio_device_unregister_debugfs(indio_dev);
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001138
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001139 iio_disable_all_buffers(indio_dev);
1140
Jonathan Cameronac917a82012-02-15 19:48:00 +00001141 indio_dev->info = NULL;
1142 mutex_unlock(&indio_dev->info_exist_lock);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001143}
1144EXPORT_SYMBOL(iio_device_unregister);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001145subsys_initcall(iio_init);
1146module_exit(iio_exit);
1147
Jonathan Cameronc8b95952012-09-02 21:27:56 +01001148MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001149MODULE_DESCRIPTION("Industrial I/O core");
1150MODULE_LICENSE("GPL");