blob: c5bc73135436414566a66d4736c2536c71ce9be4 [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>
Alison Schofield08a33802016-03-09 11:30:12 -080028#include <linux/mutex.h>
Jonathan Cameron06458e22012-04-25 15:54:58 +010029#include <linux/iio/iio.h>
Jonathan Camerondf9c1c42011-08-12 17:56:03 +010030#include "iio_core.h"
Jonathan Cameron6aea1c32011-08-24 17:28:38 +010031#include "iio_core_trigger.h"
Jonathan Cameron06458e22012-04-25 15:54:58 +010032#include <linux/iio/sysfs.h>
33#include <linux/iio/events.h>
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +010034#include <linux/iio/buffer.h>
Jonathan Cameron9dd1cb32011-08-30 12:41:15 +010035
Peter Meerwald99698b42012-08-26 13:43:00 +010036/* IDA to assign each registered device a unique id */
Jonathan Cameronb156cf72010-09-04 17:54:43 +010037static DEFINE_IDA(iio_ida);
Jonathan Cameron847ec802009-08-18 18:06:19 +010038
Jonathan Cameronf625cb92011-08-30 12:32:48 +010039static dev_t iio_devt;
Jonathan Cameron847ec802009-08-18 18:06:19 +010040
41#define IIO_DEV_MAX 256
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +010042struct bus_type iio_bus_type = {
Jonathan Cameron847ec802009-08-18 18:06:19 +010043 .name = "iio",
Jonathan Cameron847ec802009-08-18 18:06:19 +010044};
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +010045EXPORT_SYMBOL(iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +010046
Michael Henneriche553f182012-03-01 10:51:03 +010047static struct dentry *iio_debugfs_dentry;
48
Jonathan Cameronc6fc8062011-09-02 17:14:34 +010049static const char * const iio_direction[] = {
50 [0] = "in",
51 [1] = "out",
52};
53
Jonathan Cameronade7ef72011-09-02 17:14:45 +010054static const char * const iio_chan_type_name_spec[] = {
Jonathan Cameronc6fc8062011-09-02 17:14:34 +010055 [IIO_VOLTAGE] = "voltage",
Michael Hennerichfaf290e2011-05-18 14:42:02 +010056 [IIO_CURRENT] = "current",
57 [IIO_POWER] = "power",
Bryan Freed9bff02f2011-07-07 12:01:54 -070058 [IIO_ACCEL] = "accel",
Jonathan Cameron41ea0402011-10-05 15:27:59 +010059 [IIO_ANGL_VEL] = "anglvel",
Jonathan Cameron1d892712011-05-18 14:40:51 +010060 [IIO_MAGN] = "magn",
Bryan Freed9bff02f2011-07-07 12:01:54 -070061 [IIO_LIGHT] = "illuminance",
62 [IIO_INTENSITY] = "intensity",
Bryan Freedf09f2c82011-07-07 12:01:55 -070063 [IIO_PROXIMITY] = "proximity",
Bryan Freed9bff02f2011-07-07 12:01:54 -070064 [IIO_TEMP] = "temp",
Jonathan Cameron1d892712011-05-18 14:40:51 +010065 [IIO_INCLI] = "incli",
66 [IIO_ROT] = "rot",
Jonathan Cameron1d892712011-05-18 14:40:51 +010067 [IIO_ANGL] = "angl",
Bryan Freed9bff02f2011-07-07 12:01:54 -070068 [IIO_TIMESTAMP] = "timestamp",
Jonathan Cameron66dbe702011-09-02 17:14:43 +010069 [IIO_CAPACITANCE] = "capacitance",
Michael Hennericha6b12852012-04-27 10:58:34 +020070 [IIO_ALTVOLTAGE] = "altvoltage",
Jon Brenner21cd1fa2012-05-16 10:46:42 -050071 [IIO_CCT] = "cct",
Lars-Peter Clausenc4f0c692012-11-20 13:36:00 +000072 [IIO_PRESSURE] = "pressure",
Harald Geyerac216aa2013-12-01 15:08:00 +000073 [IIO_HUMIDITYRELATIVE] = "humidityrelative",
Daniel Baluta55aebeb2014-11-10 14:45:30 +020074 [IIO_ACTIVITY] = "activity",
Irina Tirdeaa88bfe72014-11-10 14:45:32 +020075 [IIO_STEPS] = "steps",
Irina Tirdea72c66642015-01-11 21:10:07 +020076 [IIO_ENERGY] = "energy",
Irina Tirdeacc3c9ee2015-01-11 21:10:08 +020077 [IIO_DISTANCE] = "distance",
Irina Tirdea5a1a9322015-01-11 21:10:09 +020078 [IIO_VELOCITY] = "velocity",
Matt Ranostay8ff6b3b2015-09-13 20:26:11 -070079 [IIO_CONCENTRATION] = "concentration",
Matt Ranostayd38d5462015-09-13 20:26:12 -070080 [IIO_RESISTANCE] = "resistance",
Matt Ranostayecb3a7c2016-01-26 18:34:30 -080081 [IIO_PH] = "ph",
Peter Meerwald-Stadlerd4094042016-03-20 16:20:23 +010082 [IIO_UVINDEX] = "uvindex",
Matt Ranostay4b9d2092016-05-24 21:29:19 -070083 [IIO_ELECTRICALCONDUCTIVITY] = "electricalconductivity",
Jonathan Cameron1d892712011-05-18 14:40:51 +010084};
85
Jonathan Cameron330c6c52011-09-02 17:14:39 +010086static const char * const iio_modifier_names[] = {
Jonathan Cameron1d892712011-05-18 14:40:51 +010087 [IIO_MOD_X] = "x",
88 [IIO_MOD_Y] = "y",
89 [IIO_MOD_Z] = "z",
Peter Meerwald4b8d8012015-06-20 23:52:30 +020090 [IIO_MOD_X_AND_Y] = "x&y",
91 [IIO_MOD_X_AND_Z] = "x&z",
92 [IIO_MOD_Y_AND_Z] = "y&z",
93 [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
94 [IIO_MOD_X_OR_Y] = "x|y",
95 [IIO_MOD_X_OR_Z] = "x|z",
96 [IIO_MOD_Y_OR_Z] = "y|z",
97 [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
Jonathan Cameron8f5879b2012-05-05 10:39:22 +010098 [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
Jonathan Cameroncf82cb82012-05-05 10:56:41 +010099 [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100100 [IIO_MOD_LIGHT_BOTH] = "both",
101 [IIO_MOD_LIGHT_IR] = "ir",
Jon Brenner21cd1fa2012-05-16 10:46:42 -0500102 [IIO_MOD_LIGHT_CLEAR] = "clear",
103 [IIO_MOD_LIGHT_RED] = "red",
104 [IIO_MOD_LIGHT_GREEN] = "green",
105 [IIO_MOD_LIGHT_BLUE] = "blue",
Peter Meerwald-Stadler2c5ff1f2016-03-20 16:20:22 +0100106 [IIO_MOD_LIGHT_UV] = "uv",
Srinivas Pandruvada5082f402014-04-29 00:51:00 +0100107 [IIO_MOD_QUATERNION] = "quaternion",
Peter Meerwald638b43b2014-02-05 16:57:00 +0000108 [IIO_MOD_TEMP_AMBIENT] = "ambient",
109 [IIO_MOD_TEMP_OBJECT] = "object",
Reyad Attiyat11b8dda2014-07-17 19:18:00 +0100110 [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
111 [IIO_MOD_NORTH_TRUE] = "from_north_true",
112 [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
113 [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
Daniel Baluta55aebeb2014-11-10 14:45:30 +0200114 [IIO_MOD_RUNNING] = "running",
115 [IIO_MOD_JOGGING] = "jogging",
116 [IIO_MOD_WALKING] = "walking",
117 [IIO_MOD_STILL] = "still",
Irina Tirdea5a1a9322015-01-11 21:10:09 +0200118 [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
Lars-Peter Clausen1ce87f22015-05-22 18:17:38 +0200119 [IIO_MOD_I] = "i",
120 [IIO_MOD_Q] = "q",
Matt Ranostay8ff6b3b2015-09-13 20:26:11 -0700121 [IIO_MOD_CO2] = "co2",
122 [IIO_MOD_VOC] = "voc",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100123};
124
125/* relies on pairs of these shared then separate */
126static const char * const iio_chan_info_postfix[] = {
Jonathan Cameron75a973c2012-04-15 17:41:30 +0100127 [IIO_CHAN_INFO_RAW] = "raw",
128 [IIO_CHAN_INFO_PROCESSED] = "input",
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100129 [IIO_CHAN_INFO_SCALE] = "scale",
130 [IIO_CHAN_INFO_OFFSET] = "offset",
131 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
132 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
133 [IIO_CHAN_INFO_PEAK] = "peak_raw",
134 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
135 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
136 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
Jonathan Camerondf94aba2011-11-27 11:39:12 +0000137 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
138 = "filter_low_pass_3db_frequency",
Martin Fuzzey3f7f6422015-05-13 12:26:42 +0200139 [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY]
140 = "filter_high_pass_3db_frequency",
Laxman Dewangance85a1c2012-04-13 16:03:31 +0530141 [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
Michael Hennericha6b12852012-04-27 10:58:34 +0200142 [IIO_CHAN_INFO_FREQUENCY] = "frequency",
143 [IIO_CHAN_INFO_PHASE] = "phase",
Michael Hennerichb65d6212012-05-11 11:36:53 +0200144 [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
srinivas pandruvada7c9ab032012-09-05 13:56:00 +0100145 [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
Peter Meerwald899d90b2013-09-08 16:20:00 +0100146 [IIO_CHAN_INFO_INT_TIME] = "integration_time",
Irina Tirdeaa88bfe72014-11-10 14:45:32 +0200147 [IIO_CHAN_INFO_ENABLE] = "en",
Irina Tirdeabcdf28f2014-11-10 14:45:33 +0200148 [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight",
Irina Tirdead37f6832015-01-11 21:10:10 +0200149 [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight",
Irina Tirdea2f0ecb72015-01-27 20:41:52 +0200150 [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count",
151 [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time",
Vianney le Clément de Saint-Marcqc8a85852015-03-30 10:34:58 +0200152 [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity",
Irina Tirdeafaaa4492015-04-29 21:16:39 +0300153 [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100154};
155
Sachin Kamata7e57dc2013-10-29 11:39:00 +0000156/**
157 * iio_find_channel_from_si() - get channel from its scan index
158 * @indio_dev: device
159 * @si: scan index to match
160 */
Jonathan Cameron5fb21c82011-12-05 21:37:10 +0000161const struct iio_chan_spec
162*iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
163{
164 int i;
165
166 for (i = 0; i < indio_dev->num_channels; i++)
167 if (indio_dev->channels[i].scan_index == si)
168 return &indio_dev->channels[i];
169 return NULL;
170}
171
Jonathan Cameron847ec802009-08-18 18:06:19 +0100172/* This turns up an awful lot */
173ssize_t iio_read_const_attr(struct device *dev,
174 struct device_attribute *attr,
175 char *buf)
176{
177 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
178}
179EXPORT_SYMBOL(iio_read_const_attr);
180
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100181static int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id)
182{
183 int ret;
184 const struct iio_event_interface *ev_int = indio_dev->event_interface;
185
186 ret = mutex_lock_interruptible(&indio_dev->mlock);
187 if (ret)
188 return ret;
189 if ((ev_int && iio_event_enabled(ev_int)) ||
190 iio_buffer_enabled(indio_dev)) {
191 mutex_unlock(&indio_dev->mlock);
192 return -EBUSY;
193 }
194 indio_dev->clock_id = clock_id;
195 mutex_unlock(&indio_dev->mlock);
196
197 return 0;
198}
199
200/**
201 * iio_get_time_ns() - utility function to get a time stamp for events etc
202 * @indio_dev: device
203 */
204s64 iio_get_time_ns(const struct iio_dev *indio_dev)
205{
206 struct timespec tp;
207
208 switch (iio_device_get_clock(indio_dev)) {
209 case CLOCK_REALTIME:
210 ktime_get_real_ts(&tp);
211 break;
212 case CLOCK_MONOTONIC:
213 ktime_get_ts(&tp);
214 break;
215 case CLOCK_MONOTONIC_RAW:
216 getrawmonotonic(&tp);
217 break;
218 case CLOCK_REALTIME_COARSE:
219 tp = current_kernel_time();
220 break;
221 case CLOCK_MONOTONIC_COARSE:
222 tp = get_monotonic_coarse();
223 break;
224 case CLOCK_BOOTTIME:
225 get_monotonic_boottime(&tp);
226 break;
227 case CLOCK_TAI:
228 timekeeping_clocktai(&tp);
229 break;
230 default:
231 BUG();
232 }
233
234 return timespec_to_ns(&tp);
235}
236EXPORT_SYMBOL(iio_get_time_ns);
237
238/**
239 * iio_get_time_res() - utility function to get time stamp clock resolution in
240 * nano seconds.
241 * @indio_dev: device
242 */
243unsigned int iio_get_time_res(const struct iio_dev *indio_dev)
244{
245 switch (iio_device_get_clock(indio_dev)) {
246 case CLOCK_REALTIME:
247 case CLOCK_MONOTONIC:
248 case CLOCK_MONOTONIC_RAW:
249 case CLOCK_BOOTTIME:
250 case CLOCK_TAI:
251 return hrtimer_resolution;
252 case CLOCK_REALTIME_COARSE:
253 case CLOCK_MONOTONIC_COARSE:
254 return LOW_RES_NSEC;
255 default:
256 BUG();
257 }
258}
259EXPORT_SYMBOL(iio_get_time_res);
260
Jonathan Cameron847ec802009-08-18 18:06:19 +0100261static int __init iio_init(void)
262{
263 int ret;
264
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100265 /* Register sysfs bus */
266 ret = bus_register(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100267 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100268 pr_err("could not register bus type\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +0100269 goto error_nothing;
270 }
271
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100272 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
273 if (ret < 0) {
Sachin Kamat3176dd52013-10-24 12:53:00 +0100274 pr_err("failed to allocate char dev region\n");
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100275 goto error_unregister_bus_type;
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100276 }
Jonathan Cameron847ec802009-08-18 18:06:19 +0100277
Michael Henneriche553f182012-03-01 10:51:03 +0100278 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
279
Jonathan Cameron847ec802009-08-18 18:06:19 +0100280 return 0;
281
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100282error_unregister_bus_type:
283 bus_unregister(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100284error_nothing:
285 return ret;
286}
287
288static void __exit iio_exit(void)
289{
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100290 if (iio_devt)
291 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100292 bus_unregister(&iio_bus_type);
Michael Henneriche553f182012-03-01 10:51:03 +0100293 debugfs_remove(iio_debugfs_dentry);
294}
295
296#if defined(CONFIG_DEBUG_FS)
Michael Henneriche553f182012-03-01 10:51:03 +0100297static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
298 size_t count, loff_t *ppos)
299{
300 struct iio_dev *indio_dev = file->private_data;
301 char buf[20];
302 unsigned val = 0;
303 ssize_t len;
304 int ret;
305
306 ret = indio_dev->info->debugfs_reg_access(indio_dev,
307 indio_dev->cached_reg_addr,
308 0, &val);
Matt Fornero9af1bd52017-09-05 16:34:10 +0200309 if (ret) {
Michael Henneriche553f182012-03-01 10:51:03 +0100310 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
Matt Fornero9af1bd52017-09-05 16:34:10 +0200311 return ret;
312 }
Michael Henneriche553f182012-03-01 10:51:03 +0100313
314 len = snprintf(buf, sizeof(buf), "0x%X\n", val);
315
316 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
317}
318
319static ssize_t iio_debugfs_write_reg(struct file *file,
320 const char __user *userbuf, size_t count, loff_t *ppos)
321{
322 struct iio_dev *indio_dev = file->private_data;
323 unsigned reg, val;
324 char buf[80];
325 int ret;
326
327 count = min_t(size_t, count, (sizeof(buf)-1));
328 if (copy_from_user(buf, userbuf, count))
329 return -EFAULT;
330
331 buf[count] = 0;
332
333 ret = sscanf(buf, "%i %i", &reg, &val);
334
335 switch (ret) {
336 case 1:
337 indio_dev->cached_reg_addr = reg;
338 break;
339 case 2:
340 indio_dev->cached_reg_addr = reg;
341 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
342 val, NULL);
343 if (ret) {
344 dev_err(indio_dev->dev.parent, "%s: write failed\n",
345 __func__);
346 return ret;
347 }
348 break;
349 default:
350 return -EINVAL;
351 }
352
353 return count;
354}
355
356static const struct file_operations iio_debugfs_reg_fops = {
Axel Lin5a28c872012-05-03 09:50:51 +0800357 .open = simple_open,
Michael Henneriche553f182012-03-01 10:51:03 +0100358 .read = iio_debugfs_read_reg,
359 .write = iio_debugfs_write_reg,
360};
361
362static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
363{
364 debugfs_remove_recursive(indio_dev->debugfs_dentry);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100365}
366
Michael Henneriche553f182012-03-01 10:51:03 +0100367static int iio_device_register_debugfs(struct iio_dev *indio_dev)
368{
369 struct dentry *d;
370
371 if (indio_dev->info->debugfs_reg_access == NULL)
372 return 0;
373
Axel Linabd5a2f2012-05-03 22:56:58 +0800374 if (!iio_debugfs_dentry)
Michael Henneriche553f182012-03-01 10:51:03 +0100375 return 0;
376
377 indio_dev->debugfs_dentry =
378 debugfs_create_dir(dev_name(&indio_dev->dev),
379 iio_debugfs_dentry);
Michael Henneriche553f182012-03-01 10:51:03 +0100380 if (indio_dev->debugfs_dentry == NULL) {
381 dev_warn(indio_dev->dev.parent,
382 "Failed to create debugfs directory\n");
383 return -EFAULT;
384 }
385
386 d = debugfs_create_file("direct_reg_access", 0644,
387 indio_dev->debugfs_dentry,
388 indio_dev, &iio_debugfs_reg_fops);
389 if (!d) {
390 iio_device_unregister_debugfs(indio_dev);
391 return -ENOMEM;
392 }
393
394 return 0;
395}
396#else
397static int iio_device_register_debugfs(struct iio_dev *indio_dev)
398{
399 return 0;
400}
401
402static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
403{
404}
405#endif /* CONFIG_DEBUG_FS */
406
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100407static ssize_t iio_read_channel_ext_info(struct device *dev,
408 struct device_attribute *attr,
409 char *buf)
410{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200411 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100412 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
413 const struct iio_chan_spec_ext_info *ext_info;
414
415 ext_info = &this_attr->c->ext_info[this_attr->address];
416
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200417 return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100418}
419
420static ssize_t iio_write_channel_ext_info(struct device *dev,
421 struct device_attribute *attr,
422 const char *buf,
423 size_t len)
424{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200425 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100426 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
427 const struct iio_chan_spec_ext_info *ext_info;
428
429 ext_info = &this_attr->c->ext_info[this_attr->address];
430
Michael Hennerichfc6d1132012-04-27 10:58:36 +0200431 return ext_info->write(indio_dev, ext_info->private,
432 this_attr->c, buf, len);
Lars-Peter Clausen4fee7e12012-03-06 20:43:45 +0100433}
434
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200435ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
436 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
437{
438 const struct iio_enum *e = (const struct iio_enum *)priv;
439 unsigned int i;
440 size_t len = 0;
441
442 if (!e->num_items)
443 return 0;
444
445 for (i = 0; i < e->num_items; ++i)
Lars-Peter Clausen74dcd432012-06-05 18:24:12 +0200446 len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200447
448 /* replace last space with a newline */
449 buf[len - 1] = '\n';
450
451 return len;
452}
453EXPORT_SYMBOL_GPL(iio_enum_available_read);
454
455ssize_t iio_enum_read(struct iio_dev *indio_dev,
456 uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
457{
458 const struct iio_enum *e = (const struct iio_enum *)priv;
459 int i;
460
461 if (!e->get)
462 return -EINVAL;
463
464 i = e->get(indio_dev, chan);
465 if (i < 0)
466 return i;
467 else if (i >= e->num_items)
468 return -EINVAL;
469
Kees Cook598db582014-03-13 16:46:00 +0000470 return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]);
Lars-Peter Clausen5212cc82012-06-04 11:36:11 +0200471}
472EXPORT_SYMBOL_GPL(iio_enum_read);
473
474ssize_t iio_enum_write(struct iio_dev *indio_dev,
475 uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
476 size_t len)
477{
478 const struct iio_enum *e = (const struct iio_enum *)priv;
479 unsigned int i;
480 int ret;
481
482 if (!e->set)
483 return -EINVAL;
484
485 for (i = 0; i < e->num_items; i++) {
486 if (sysfs_streq(buf, e->items[i]))
487 break;
488 }
489
490 if (i == e->num_items)
491 return -EINVAL;
492
493 ret = e->set(indio_dev, chan, i);
494 return ret ? ret : len;
495}
496EXPORT_SYMBOL_GPL(iio_enum_write);
497
Gregor Boiriedfc57732016-04-20 19:23:43 +0200498static const struct iio_mount_matrix iio_mount_idmatrix = {
499 .rotation = {
500 "1", "0", "0",
501 "0", "1", "0",
502 "0", "0", "1"
503 }
504};
505
506static int iio_setup_mount_idmatrix(const struct device *dev,
507 struct iio_mount_matrix *matrix)
508{
509 *matrix = iio_mount_idmatrix;
510 dev_info(dev, "mounting matrix not found: using identity...\n");
511 return 0;
512}
513
514ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv,
515 const struct iio_chan_spec *chan, char *buf)
516{
517 const struct iio_mount_matrix *mtx = ((iio_get_mount_matrix_t *)
518 priv)(indio_dev, chan);
519
520 if (IS_ERR(mtx))
521 return PTR_ERR(mtx);
522
523 if (!mtx)
524 mtx = &iio_mount_idmatrix;
525
526 return snprintf(buf, PAGE_SIZE, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n",
527 mtx->rotation[0], mtx->rotation[1], mtx->rotation[2],
528 mtx->rotation[3], mtx->rotation[4], mtx->rotation[5],
529 mtx->rotation[6], mtx->rotation[7], mtx->rotation[8]);
530}
531EXPORT_SYMBOL_GPL(iio_show_mount_matrix);
532
533/**
534 * of_iio_read_mount_matrix() - retrieve iio device mounting matrix from
535 * device-tree "mount-matrix" property
536 * @dev: device the mounting matrix property is assigned to
537 * @propname: device specific mounting matrix property name
538 * @matrix: where to store retrieved matrix
539 *
540 * If device is assigned no mounting matrix property, a default 3x3 identity
541 * matrix will be filled in.
542 *
543 * Return: 0 if success, or a negative error code on failure.
544 */
545#ifdef CONFIG_OF
546int of_iio_read_mount_matrix(const struct device *dev,
547 const char *propname,
548 struct iio_mount_matrix *matrix)
549{
550 if (dev->of_node) {
551 int err = of_property_read_string_array(dev->of_node,
552 propname, matrix->rotation,
553 ARRAY_SIZE(iio_mount_idmatrix.rotation));
554
555 if (err == ARRAY_SIZE(iio_mount_idmatrix.rotation))
556 return 0;
557
558 if (err >= 0)
559 /* Invalid number of matrix entries. */
560 return -EINVAL;
561
562 if (err != -EINVAL)
563 /* Invalid matrix declaration format. */
564 return err;
565 }
566
567 /* Matrix was not declared at all: fallback to identity. */
568 return iio_setup_mount_idmatrix(dev, matrix);
569}
570#else
571int of_iio_read_mount_matrix(const struct device *dev,
572 const char *propname,
573 struct iio_mount_matrix *matrix)
574{
575 return iio_setup_mount_idmatrix(dev, matrix);
576}
577#endif
578EXPORT_SYMBOL(of_iio_read_mount_matrix);
579
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100580/**
581 * iio_format_value() - Formats a IIO value into its string representation
Cristina Opriceana2498dcf2015-07-24 16:16:19 +0300582 * @buf: The buffer to which the formatted value gets written
583 * @type: One of the IIO_VAL_... constants. This decides how the val
584 * and val2 parameters are formatted.
585 * @size: Number of IIO value entries contained in vals
586 * @vals: Pointer to the values, exact meaning depends on the
587 * type parameter.
588 *
589 * Return: 0 by default, a negative number on failure or the
590 * total number of characters written for a type that belongs
591 * to the IIO_VAL_... constant.
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100592 */
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100593ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100594{
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100595 unsigned long long tmp;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200596 bool scale_db = false;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100597
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100598 switch (type) {
Michael Hennerich67eedba2012-05-11 11:36:52 +0200599 case IIO_VAL_INT:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100600 return sprintf(buf, "%d\n", vals[0]);
Michael Hennerich67eedba2012-05-11 11:36:52 +0200601 case IIO_VAL_INT_PLUS_MICRO_DB:
602 scale_db = true;
603 case IIO_VAL_INT_PLUS_MICRO:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100604 if (vals[1] < 0)
Michal Nazarewicz8f57e4d2016-01-15 16:57:58 -0800605 return sprintf(buf, "-%d.%06u%s\n", abs(vals[0]),
606 -vals[1], scale_db ? " dB" : "");
Jonathan Cameron1d892712011-05-18 14:40:51 +0100607 else
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100608 return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1],
Michael Hennerich67eedba2012-05-11 11:36:52 +0200609 scale_db ? " dB" : "");
610 case IIO_VAL_INT_PLUS_NANO:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100611 if (vals[1] < 0)
Michal Nazarewicz8f57e4d2016-01-15 16:57:58 -0800612 return sprintf(buf, "-%d.%09u\n", abs(vals[0]),
613 -vals[1]);
Michael Hennerich71646e22011-06-27 13:07:07 +0100614 else
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100615 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
Lars-Peter Clausen7985e7c2012-09-14 16:21:00 +0100616 case IIO_VAL_FRACTIONAL:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100617 tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
Gregor Boirie171c0092016-09-02 20:27:46 +0200618 vals[0] = (int)div_s64_rem(tmp, 1000000000, &vals[1]);
619 return sprintf(buf, "%d.%09u\n", vals[0], abs(vals[1]));
Lars-Peter Clausen103d9fb2012-10-16 17:29:00 +0100620 case IIO_VAL_FRACTIONAL_LOG2:
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100621 tmp = (s64)vals[0] * 1000000000LL >> vals[1];
622 vals[1] = do_div(tmp, 1000000000LL);
623 vals[0] = tmp;
624 return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
625 case IIO_VAL_INT_MULTIPLE:
626 {
627 int i;
628 int len = 0;
629
630 for (i = 0; i < size; ++i)
631 len += snprintf(&buf[len], PAGE_SIZE - len, "%d ",
632 vals[i]);
633 len += snprintf(&buf[len], PAGE_SIZE - len, "\n");
634 return len;
635 }
Michael Hennerich67eedba2012-05-11 11:36:52 +0200636 default:
Jonathan Cameron1d892712011-05-18 14:40:51 +0100637 return 0;
Michael Hennerich67eedba2012-05-11 11:36:52 +0200638 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100639}
Andrew F. Davis7d2c2aca2015-12-14 16:35:57 -0600640EXPORT_SYMBOL_GPL(iio_format_value);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100641
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100642static ssize_t iio_read_channel_info(struct device *dev,
643 struct device_attribute *attr,
644 char *buf)
645{
646 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
647 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100648 int vals[INDIO_MAX_RAW_ELEMENTS];
649 int ret;
650 int val_len = 2;
651
652 if (indio_dev->info->read_raw_multi)
653 ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
654 INDIO_MAX_RAW_ELEMENTS,
655 vals, &val_len,
656 this_attr->address);
657 else
658 ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
659 &vals[0], &vals[1], this_attr->address);
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100660
661 if (ret < 0)
662 return ret;
663
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100664 return iio_format_value(buf, ret, val_len, vals);
Lars-Peter Clausen3661f3f2013-10-07 15:11:00 +0100665}
666
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000667/**
668 * iio_str_to_fixpoint() - Parse a fixed-point number from a string
669 * @str: The string to parse
670 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
671 * @integer: The integer part of the number
672 * @fract: The fractional part of the number
673 *
674 * Returns 0 on success, or a negative error code if the string could not be
675 * parsed.
676 */
677int iio_str_to_fixpoint(const char *str, int fract_mult,
678 int *integer, int *fract)
679{
680 int i = 0, f = 0;
681 bool integer_part = true, negative = false;
682
Sean Nyekjaerf47dff32015-11-09 13:55:34 +0100683 if (fract_mult == 0) {
684 *fract = 0;
685
686 return kstrtoint(str, 0, integer);
687 }
688
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000689 if (str[0] == '-') {
690 negative = true;
691 str++;
692 } else if (str[0] == '+') {
693 str++;
694 }
695
696 while (*str) {
697 if ('0' <= *str && *str <= '9') {
698 if (integer_part) {
699 i = i * 10 + *str - '0';
700 } else {
701 f += fract_mult * (*str - '0');
702 fract_mult /= 10;
703 }
704 } else if (*str == '\n') {
705 if (*(str + 1) == '\0')
706 break;
707 else
708 return -EINVAL;
709 } else if (*str == '.' && integer_part) {
710 integer_part = false;
711 } else {
712 return -EINVAL;
713 }
714 str++;
715 }
716
717 if (negative) {
718 if (i)
719 i = -i;
720 else
721 f = -f;
722 }
723
724 *integer = i;
725 *fract = f;
726
727 return 0;
728}
729EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
730
Jonathan Cameron1d892712011-05-18 14:40:51 +0100731static ssize_t iio_write_channel_info(struct device *dev,
732 struct device_attribute *attr,
733 const char *buf,
734 size_t len)
735{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200736 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100737 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000738 int ret, fract_mult = 100000;
739 int integer, fract;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100740
741 /* Assumes decimal - precision based on number of digits */
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100742 if (!indio_dev->info->write_raw)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100743 return -EINVAL;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100744
745 if (indio_dev->info->write_raw_get_fmt)
746 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
747 this_attr->c, this_attr->address)) {
Sean Nyekjaerf47dff32015-11-09 13:55:34 +0100748 case IIO_VAL_INT:
749 fract_mult = 0;
750 break;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100751 case IIO_VAL_INT_PLUS_MICRO:
752 fract_mult = 100000;
753 break;
754 case IIO_VAL_INT_PLUS_NANO:
755 fract_mult = 100000000;
756 break;
757 default:
758 return -EINVAL;
759 }
760
Lars-Peter Clausen6807d722012-11-20 13:36:00 +0000761 ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
762 if (ret)
763 return ret;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100764
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100765 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
Michael Hennerich5c04af02011-06-27 13:07:10 +0100766 integer, fract, this_attr->address);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100767 if (ret)
768 return ret;
769
770 return len;
771}
772
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100773static
Jonathan Cameron1d892712011-05-18 14:40:51 +0100774int __iio_device_attr_init(struct device_attribute *dev_attr,
775 const char *postfix,
776 struct iio_chan_spec const *chan,
777 ssize_t (*readfunc)(struct device *dev,
778 struct device_attribute *attr,
779 char *buf),
780 ssize_t (*writefunc)(struct device *dev,
781 struct device_attribute *attr,
782 const char *buf,
783 size_t len),
Jonathan Cameron37044322013-09-08 14:57:00 +0100784 enum iio_shared_by shared_by)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100785{
Jonathan Cameron37044322013-09-08 14:57:00 +0100786 int ret = 0;
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000787 char *name = NULL;
Jonathan Cameron37044322013-09-08 14:57:00 +0100788 char *full_postfix;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100789 sysfs_attr_init(&dev_attr->attr);
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100790
791 /* Build up postfix of <extend_name>_<modifier>_postfix */
Jonathan Cameron37044322013-09-08 14:57:00 +0100792 if (chan->modified && (shared_by == IIO_SEPARATE)) {
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100793 if (chan->extend_name)
794 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
795 iio_modifier_names[chan
796 ->channel2],
797 chan->extend_name,
798 postfix);
799 else
800 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
801 iio_modifier_names[chan
802 ->channel2],
803 postfix);
804 } else {
Lars-Peter Clausen77bfa8b2014-02-14 14:19:00 +0000805 if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100806 full_postfix = kstrdup(postfix, GFP_KERNEL);
807 else
808 full_postfix = kasprintf(GFP_KERNEL,
809 "%s_%s",
810 chan->extend_name,
811 postfix);
812 }
Jonathan Cameron37044322013-09-08 14:57:00 +0100813 if (full_postfix == NULL)
814 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100815
Justin P. Mattock4abf6f82012-02-29 22:00:38 -0800816 if (chan->differential) { /* Differential can not have modifier */
Jonathan Cameron37044322013-09-08 14:57:00 +0100817 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100818 case IIO_SHARED_BY_ALL:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000819 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100820 break;
821 case IIO_SHARED_BY_DIR:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000822 name = kasprintf(GFP_KERNEL, "%s_%s",
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100823 iio_direction[chan->output],
824 full_postfix);
825 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100826 case IIO_SHARED_BY_TYPE:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000827 name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100828 iio_direction[chan->output],
829 iio_chan_type_name_spec[chan->type],
830 iio_chan_type_name_spec[chan->type],
831 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100832 break;
833 case IIO_SEPARATE:
834 if (!chan->indexed) {
Dan Carpenter231bfe52015-11-21 13:33:00 +0300835 WARN(1, "Differential channels must be indexed\n");
Jonathan Cameron37044322013-09-08 14:57:00 +0100836 ret = -EINVAL;
837 goto error_free_full_postfix;
838 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000839 name = kasprintf(GFP_KERNEL,
Jonathan Cameron37044322013-09-08 14:57:00 +0100840 "%s_%s%d-%s%d_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100841 iio_direction[chan->output],
842 iio_chan_type_name_spec[chan->type],
843 chan->channel,
844 iio_chan_type_name_spec[chan->type],
845 chan->channel2,
846 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100847 break;
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100848 }
849 } else { /* Single ended */
Jonathan Cameron37044322013-09-08 14:57:00 +0100850 switch (shared_by) {
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100851 case IIO_SHARED_BY_ALL:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000852 name = kasprintf(GFP_KERNEL, "%s", full_postfix);
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100853 break;
854 case IIO_SHARED_BY_DIR:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000855 name = kasprintf(GFP_KERNEL, "%s_%s",
Jonathan Cameronc006ec82013-09-08 14:57:00 +0100856 iio_direction[chan->output],
857 full_postfix);
858 break;
Jonathan Cameron37044322013-09-08 14:57:00 +0100859 case IIO_SHARED_BY_TYPE:
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000860 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100861 iio_direction[chan->output],
862 iio_chan_type_name_spec[chan->type],
863 full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100864 break;
865
866 case IIO_SEPARATE:
867 if (chan->indexed)
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000868 name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
Jonathan Cameron37044322013-09-08 14:57:00 +0100869 iio_direction[chan->output],
870 iio_chan_type_name_spec[chan->type],
871 chan->channel,
872 full_postfix);
873 else
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000874 name = kasprintf(GFP_KERNEL, "%s_%s_%s",
Jonathan Cameron37044322013-09-08 14:57:00 +0100875 iio_direction[chan->output],
876 iio_chan_type_name_spec[chan->type],
877 full_postfix);
878 break;
879 }
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100880 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000881 if (name == NULL) {
Jonathan Cameron1d892712011-05-18 14:40:51 +0100882 ret = -ENOMEM;
883 goto error_free_full_postfix;
884 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000885 dev_attr->attr.name = name;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100886
887 if (readfunc) {
888 dev_attr->attr.mode |= S_IRUGO;
889 dev_attr->show = readfunc;
890 }
891
892 if (writefunc) {
893 dev_attr->attr.mode |= S_IWUSR;
894 dev_attr->store = writefunc;
895 }
Lars-Peter Clausen7bbcf7e2014-02-14 14:19:00 +0000896
Jonathan Cameron1d892712011-05-18 14:40:51 +0100897error_free_full_postfix:
898 kfree(full_postfix);
Jonathan Cameron37044322013-09-08 14:57:00 +0100899
Jonathan Cameron847ec802009-08-18 18:06:19 +0100900 return ret;
901}
902
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100903static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100904{
905 kfree(dev_attr->attr.name);
906}
907
908int __iio_add_chan_devattr(const char *postfix,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100909 struct iio_chan_spec const *chan,
910 ssize_t (*readfunc)(struct device *dev,
911 struct device_attribute *attr,
912 char *buf),
913 ssize_t (*writefunc)(struct device *dev,
914 struct device_attribute *attr,
915 const char *buf,
916 size_t len),
Jonathan Camerone614a542011-09-02 17:14:41 +0100917 u64 mask,
Jonathan Cameron37044322013-09-08 14:57:00 +0100918 enum iio_shared_by shared_by,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100919 struct device *dev,
920 struct list_head *attr_list)
921{
922 int ret;
923 struct iio_dev_attr *iio_attr, *t;
924
Sachin Kamat670c1102013-10-24 12:53:00 +0100925 iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000926 if (iio_attr == NULL)
927 return -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100928 ret = __iio_device_attr_init(&iio_attr->dev_attr,
929 postfix, chan,
Jonathan Cameron37044322013-09-08 14:57:00 +0100930 readfunc, writefunc, shared_by);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100931 if (ret)
932 goto error_iio_dev_attr_free;
933 iio_attr->c = chan;
934 iio_attr->address = mask;
935 list_for_each_entry(t, attr_list, l)
936 if (strcmp(t->dev_attr.attr.name,
937 iio_attr->dev_attr.attr.name) == 0) {
Jonathan Cameron37044322013-09-08 14:57:00 +0100938 if (shared_by == IIO_SEPARATE)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100939 dev_err(dev, "tried to double register : %s\n",
940 t->dev_attr.attr.name);
941 ret = -EBUSY;
942 goto error_device_attr_deinit;
943 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100944 list_add(&iio_attr->l, attr_list);
945
946 return 0;
947
948error_device_attr_deinit:
949 __iio_device_attr_deinit(&iio_attr->dev_attr);
950error_iio_dev_attr_free:
951 kfree(iio_attr);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100952 return ret;
953}
954
Jonathan Cameron37044322013-09-08 14:57:00 +0100955static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
956 struct iio_chan_spec const *chan,
957 enum iio_shared_by shared_by,
958 const long *infomask)
959{
960 int i, ret, attrcount = 0;
961
962 for_each_set_bit(i, infomask, sizeof(infomask)*8) {
Jonathan Cameronef4b4852014-01-03 22:24:00 +0000963 if (i >= ARRAY_SIZE(iio_chan_info_postfix))
964 return -EINVAL;
Jonathan Cameron37044322013-09-08 14:57:00 +0100965 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
966 chan,
967 &iio_read_channel_info,
968 &iio_write_channel_info,
969 i,
970 shared_by,
971 &indio_dev->dev,
972 &indio_dev->channel_attr_list);
973 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
974 continue;
975 else if (ret < 0)
976 return ret;
977 attrcount++;
978 }
979
980 return attrcount;
981}
982
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100983static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100984 struct iio_chan_spec const *chan)
985{
Jonathan Cameron5ccb3ad2012-04-15 17:41:16 +0100986 int ret, attrcount = 0;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +0100987 const struct iio_chan_spec_ext_info *ext_info;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100988
Jonathan Cameron1d892712011-05-18 14:40:51 +0100989 if (chan->channel < 0)
990 return 0;
Jonathan Cameron37044322013-09-08 14:57:00 +0100991 ret = iio_device_add_info_mask_type(indio_dev, chan,
992 IIO_SEPARATE,
993 &chan->info_mask_separate);
994 if (ret < 0)
995 return ret;
996 attrcount += ret;
997
998 ret = iio_device_add_info_mask_type(indio_dev, chan,
999 IIO_SHARED_BY_TYPE,
1000 &chan->info_mask_shared_by_type);
1001 if (ret < 0)
1002 return ret;
1003 attrcount += ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +01001004
Jonathan Cameronc006ec82013-09-08 14:57:00 +01001005 ret = iio_device_add_info_mask_type(indio_dev, chan,
1006 IIO_SHARED_BY_DIR,
1007 &chan->info_mask_shared_by_dir);
1008 if (ret < 0)
1009 return ret;
1010 attrcount += ret;
1011
1012 ret = iio_device_add_info_mask_type(indio_dev, chan,
1013 IIO_SHARED_BY_ALL,
1014 &chan->info_mask_shared_by_all);
1015 if (ret < 0)
1016 return ret;
1017 attrcount += ret;
1018
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +01001019 if (chan->ext_info) {
1020 unsigned int i = 0;
1021 for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
1022 ret = __iio_add_chan_devattr(ext_info->name,
1023 chan,
1024 ext_info->read ?
1025 &iio_read_channel_ext_info : NULL,
1026 ext_info->write ?
1027 &iio_write_channel_ext_info : NULL,
1028 i,
1029 ext_info->shared,
1030 &indio_dev->dev,
1031 &indio_dev->channel_attr_list);
1032 i++;
1033 if (ret == -EBUSY && ext_info->shared)
1034 continue;
1035
1036 if (ret)
Jonathan Cameron37044322013-09-08 14:57:00 +01001037 return ret;
Lars-Peter Clausen5f420b42012-02-21 18:38:12 +01001038
1039 attrcount++;
1040 }
1041 }
1042
Jonathan Cameron37044322013-09-08 14:57:00 +01001043 return attrcount;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001044}
1045
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +01001046/**
1047 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
1048 * @attr_list: List of IIO device attributes
1049 *
1050 * This function frees the memory allocated for each of the IIO device
Martin Fuzzeyc1b03ab2015-02-19 15:17:44 +01001051 * attributes in the list.
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +01001052 */
1053void iio_free_chan_devattr_list(struct list_head *attr_list)
Jonathan Cameron1d892712011-05-18 14:40:51 +01001054{
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +01001055 struct iio_dev_attr *p, *n;
1056
1057 list_for_each_entry_safe(p, n, attr_list, l) {
1058 kfree(p->dev_attr.attr.name);
Martin Fuzzeyc1b03ab2015-02-19 15:17:44 +01001059 list_del(&p->l);
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +01001060 kfree(p);
1061 }
Jonathan Cameron1d892712011-05-18 14:40:51 +01001062}
1063
Jonathan Cameron1b732882011-05-18 14:41:43 +01001064static ssize_t iio_show_dev_name(struct device *dev,
1065 struct device_attribute *attr,
1066 char *buf)
1067{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +02001068 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Kees Cook598db582014-03-13 16:46:00 +00001069 return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name);
Jonathan Cameron1b732882011-05-18 14:41:43 +01001070}
1071
1072static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
1073
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001074static ssize_t iio_show_timestamp_clock(struct device *dev,
1075 struct device_attribute *attr,
1076 char *buf)
1077{
1078 const struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1079 const clockid_t clk = iio_device_get_clock(indio_dev);
1080 const char *name;
1081 ssize_t sz;
1082
1083 switch (clk) {
1084 case CLOCK_REALTIME:
1085 name = "realtime\n";
1086 sz = sizeof("realtime\n");
1087 break;
1088 case CLOCK_MONOTONIC:
1089 name = "monotonic\n";
1090 sz = sizeof("monotonic\n");
1091 break;
1092 case CLOCK_MONOTONIC_RAW:
1093 name = "monotonic_raw\n";
1094 sz = sizeof("monotonic_raw\n");
1095 break;
1096 case CLOCK_REALTIME_COARSE:
1097 name = "realtime_coarse\n";
1098 sz = sizeof("realtime_coarse\n");
1099 break;
1100 case CLOCK_MONOTONIC_COARSE:
1101 name = "monotonic_coarse\n";
1102 sz = sizeof("monotonic_coarse\n");
1103 break;
1104 case CLOCK_BOOTTIME:
1105 name = "boottime\n";
1106 sz = sizeof("boottime\n");
1107 break;
1108 case CLOCK_TAI:
1109 name = "tai\n";
1110 sz = sizeof("tai\n");
1111 break;
1112 default:
1113 BUG();
1114 }
1115
1116 memcpy(buf, name, sz);
1117 return sz;
1118}
1119
1120static ssize_t iio_store_timestamp_clock(struct device *dev,
1121 struct device_attribute *attr,
1122 const char *buf, size_t len)
1123{
1124 clockid_t clk;
1125 int ret;
1126
1127 if (sysfs_streq(buf, "realtime"))
1128 clk = CLOCK_REALTIME;
1129 else if (sysfs_streq(buf, "monotonic"))
1130 clk = CLOCK_MONOTONIC;
1131 else if (sysfs_streq(buf, "monotonic_raw"))
1132 clk = CLOCK_MONOTONIC_RAW;
1133 else if (sysfs_streq(buf, "realtime_coarse"))
1134 clk = CLOCK_REALTIME_COARSE;
1135 else if (sysfs_streq(buf, "monotonic_coarse"))
1136 clk = CLOCK_MONOTONIC_COARSE;
1137 else if (sysfs_streq(buf, "boottime"))
1138 clk = CLOCK_BOOTTIME;
1139 else if (sysfs_streq(buf, "tai"))
1140 clk = CLOCK_TAI;
1141 else
1142 return -EINVAL;
1143
1144 ret = iio_device_set_clock(dev_to_iio_dev(dev), clk);
1145 if (ret)
1146 return ret;
1147
1148 return len;
1149}
1150
1151static DEVICE_ATTR(current_timestamp_clock, S_IRUGO | S_IWUSR,
1152 iio_show_timestamp_clock, iio_store_timestamp_clock);
1153
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001154static int iio_device_register_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron1d892712011-05-18 14:40:51 +01001155{
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001156 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +01001157 struct iio_dev_attr *p;
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001158 struct attribute **attr, *clk = NULL;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001159
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001160 /* First count elements in any existing group */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001161 if (indio_dev->info->attrs) {
1162 attr = indio_dev->info->attrs->attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001163 while (*attr++ != NULL)
1164 attrcount_orig++;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001165 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001166 attrcount = attrcount_orig;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001167 /*
1168 * New channel registration method - relies on the fact a group does
Peter Meerwaldd25b3802012-08-26 13:43:00 +01001169 * not need to be initialized if its name is NULL.
Jonathan Cameron1d892712011-05-18 14:40:51 +01001170 */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001171 if (indio_dev->channels)
1172 for (i = 0; i < indio_dev->num_channels; i++) {
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001173 const struct iio_chan_spec *chan =
1174 &indio_dev->channels[i];
1175
1176 if (chan->type == IIO_TIMESTAMP)
1177 clk = &dev_attr_current_timestamp_clock.attr;
1178
1179 ret = iio_device_add_channel_sysfs(indio_dev, chan);
Jonathan Cameron1d892712011-05-18 14:40:51 +01001180 if (ret < 0)
1181 goto error_clear_attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001182 attrcount += ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001183 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001184
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001185 if (indio_dev->event_interface)
1186 clk = &dev_attr_current_timestamp_clock.attr;
1187
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001188 if (indio_dev->name)
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001189 attrcount++;
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001190 if (clk)
1191 attrcount++;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001192
Thomas Meyerd83fb182011-11-29 22:08:00 +01001193 indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
1194 sizeof(indio_dev->chan_attr_group.attrs[0]),
1195 GFP_KERNEL);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001196 if (indio_dev->chan_attr_group.attrs == NULL) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001197 ret = -ENOMEM;
1198 goto error_clear_attrs;
Jonathan Cameron1b732882011-05-18 14:41:43 +01001199 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001200 /* Copy across original attributes */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001201 if (indio_dev->info->attrs)
1202 memcpy(indio_dev->chan_attr_group.attrs,
1203 indio_dev->info->attrs->attrs,
1204 sizeof(indio_dev->chan_attr_group.attrs[0])
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001205 *attrcount_orig);
1206 attrn = attrcount_orig;
1207 /* Add all elements from the list. */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001208 list_for_each_entry(p, &indio_dev->channel_attr_list, l)
1209 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
1210 if (indio_dev->name)
1211 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
Gregor Boiriebc2b7da2016-03-09 19:05:49 +01001212 if (clk)
1213 indio_dev->chan_attr_group.attrs[attrn++] = clk;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001214
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001215 indio_dev->groups[indio_dev->groupcounter++] =
1216 &indio_dev->chan_attr_group;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001217
Jonathan Cameron1d892712011-05-18 14:40:51 +01001218 return 0;
Jonathan Cameron1b732882011-05-18 14:41:43 +01001219
Jonathan Cameron1d892712011-05-18 14:40:51 +01001220error_clear_attrs:
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +01001221 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
Jonathan Cameron1d892712011-05-18 14:40:51 +01001222
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001223 return ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001224}
1225
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001226static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001227{
Jonathan Cameron1d892712011-05-18 14:40:51 +01001228
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +01001229 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001230 kfree(indio_dev->chan_attr_group.attrs);
Martin Fuzzeyc1b03ab2015-02-19 15:17:44 +01001231 indio_dev->chan_attr_group.attrs = NULL;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001232}
1233
Jonathan Cameron847ec802009-08-18 18:06:19 +01001234static void iio_dev_release(struct device *device)
1235{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +02001236 struct iio_dev *indio_dev = dev_to_iio_dev(device);
Vladimir Barinov735ad072015-08-20 22:37:39 +03001237 if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED))
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001238 iio_device_unregister_trigger_consumer(indio_dev);
1239 iio_device_unregister_eventset(indio_dev);
1240 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +02001241
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +01001242 iio_buffer_put(indio_dev->buffer);
1243
Lars-Peter Clausene407fd62012-06-04 10:41:42 +02001244 ida_simple_remove(&iio_ida, indio_dev->id);
1245 kfree(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001246}
1247
Guenter Roeck17d82b42013-02-07 17:09:00 +00001248struct device_type iio_device_type = {
Jonathan Cameron847ec802009-08-18 18:06:19 +01001249 .name = "iio_device",
1250 .release = iio_dev_release,
1251};
1252
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001253/**
1254 * iio_device_alloc() - allocate an iio_dev from a driver
1255 * @sizeof_priv: Space to allocate for private structure.
1256 **/
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001257struct iio_dev *iio_device_alloc(int sizeof_priv)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001258{
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +01001259 struct iio_dev *dev;
1260 size_t alloc_size;
1261
1262 alloc_size = sizeof(struct iio_dev);
1263 if (sizeof_priv) {
1264 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
1265 alloc_size += sizeof_priv;
1266 }
1267 /* ensure 32-byte alignment of whole construct ? */
1268 alloc_size += IIO_ALIGN - 1;
1269
1270 dev = kzalloc(alloc_size, GFP_KERNEL);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001271
1272 if (dev) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001273 dev->dev.groups = dev->groups;
Guenter Roeck17d82b42013-02-07 17:09:00 +00001274 dev->dev.type = &iio_device_type;
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +01001275 dev->dev.bus = &iio_bus_type;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001276 device_initialize(&dev->dev);
1277 dev_set_drvdata(&dev->dev, (void *)dev);
1278 mutex_init(&dev->mlock);
Jonathan Cameronac917a82012-02-15 19:48:00 +00001279 mutex_init(&dev->info_exist_lock);
Lars-Peter Clausene407fd62012-06-04 10:41:42 +02001280 INIT_LIST_HEAD(&dev->channel_attr_list);
Jonathan Camerona9e39f92011-09-14 13:01:25 +01001281
1282 dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
1283 if (dev->id < 0) {
1284 /* cannot use a dev_err as the name isn't available */
Sachin Kamat3176dd52013-10-24 12:53:00 +01001285 pr_err("failed to get device id\n");
Jonathan Camerona9e39f92011-09-14 13:01:25 +01001286 kfree(dev);
1287 return NULL;
1288 }
1289 dev_set_name(&dev->dev, "iio:device%d", dev->id);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001290 INIT_LIST_HEAD(&dev->buffer_list);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001291 }
1292
1293 return dev;
1294}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001295EXPORT_SYMBOL(iio_device_alloc);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001296
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001297/**
1298 * iio_device_free() - free an iio_dev from a driver
1299 * @dev: the iio_dev associated with the device
1300 **/
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001301void iio_device_free(struct iio_dev *dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001302{
Lars-Peter Clausene407fd62012-06-04 10:41:42 +02001303 if (dev)
1304 put_device(&dev->dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001305}
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +02001306EXPORT_SYMBOL(iio_device_free);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001307
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001308static void devm_iio_device_release(struct device *dev, void *res)
1309{
1310 iio_device_free(*(struct iio_dev **)res);
1311}
1312
Gregor Boirie70e48342016-09-02 20:47:55 +02001313int devm_iio_device_match(struct device *dev, void *res, void *data)
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001314{
1315 struct iio_dev **r = res;
1316 if (!r || !*r) {
1317 WARN_ON(!r || !*r);
1318 return 0;
1319 }
1320 return *r == data;
1321}
Gregor Boirie70e48342016-09-02 20:47:55 +02001322EXPORT_SYMBOL_GPL(devm_iio_device_match);
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001323
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001324/**
1325 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1326 * @dev: Device to allocate iio_dev for
1327 * @sizeof_priv: Space to allocate for private structure.
1328 *
1329 * Managed iio_device_alloc. iio_dev allocated with this function is
1330 * automatically freed on driver detach.
1331 *
1332 * If an iio_dev allocated with this function needs to be freed separately,
1333 * devm_iio_device_free() must be used.
1334 *
1335 * RETURNS:
1336 * Pointer to allocated iio_dev on success, NULL on failure.
1337 */
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001338struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
1339{
1340 struct iio_dev **ptr, *iio_dev;
1341
1342 ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
1343 GFP_KERNEL);
1344 if (!ptr)
1345 return NULL;
1346
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001347 iio_dev = iio_device_alloc(sizeof_priv);
1348 if (iio_dev) {
1349 *ptr = iio_dev;
1350 devres_add(dev, ptr);
1351 } else {
1352 devres_free(ptr);
1353 }
1354
1355 return iio_dev;
1356}
1357EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
1358
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001359/**
1360 * devm_iio_device_free - Resource-managed iio_device_free()
1361 * @dev: Device this iio_dev belongs to
1362 * @iio_dev: the iio_dev associated with the device
1363 *
1364 * Free iio_dev allocated with devm_iio_device_alloc().
1365 */
Grygorii Strashko9dabaf52013-07-18 11:19:00 +01001366void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
1367{
1368 int rc;
1369
1370 rc = devres_release(dev, devm_iio_device_release,
1371 devm_iio_device_match, iio_dev);
1372 WARN_ON(rc);
1373}
1374EXPORT_SYMBOL_GPL(devm_iio_device_free);
1375
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001376/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001377 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
Cristina Opriceana2498dcf2015-07-24 16:16:19 +03001378 * @inode: Inode structure for identifying the device in the file system
1379 * @filp: File structure for iio device used to keep and later access
1380 * private data
1381 *
1382 * Return: 0 on success or -EBUSY if the device is already opened
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001383 **/
1384static int iio_chrdev_open(struct inode *inode, struct file *filp)
1385{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001386 struct iio_dev *indio_dev = container_of(inode->i_cdev,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001387 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001388
1389 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
1390 return -EBUSY;
1391
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001392 iio_device_get(indio_dev);
1393
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001394 filp->private_data = indio_dev;
Jonathan Cameron30eb82f2011-09-21 11:16:02 +01001395
Lars-Peter Clausen79335142011-12-19 15:23:49 +01001396 return 0;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001397}
1398
1399/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001400 * iio_chrdev_release() - chrdev file close buffer access and ioctls
Cristina Opriceana2498dcf2015-07-24 16:16:19 +03001401 * @inode: Inode structure pointer for the char device
1402 * @filp: File structure pointer for the char device
1403 *
1404 * Return: 0 for successful release
1405 */
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001406static int iio_chrdev_release(struct inode *inode, struct file *filp)
1407{
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001408 struct iio_dev *indio_dev = container_of(inode->i_cdev,
1409 struct iio_dev, chrdev);
Lars-Peter Clausenbb014432011-12-19 15:23:45 +01001410 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
Lars-Peter Clausencadc2122013-09-18 21:02:00 +01001411 iio_device_put(indio_dev);
1412
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001413 return 0;
1414}
1415
1416/* Somewhat of a cross file organization violation - ioctls here are actually
1417 * event related */
1418static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1419{
1420 struct iio_dev *indio_dev = filp->private_data;
1421 int __user *ip = (int __user *)arg;
1422 int fd;
1423
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +01001424 if (!indio_dev->info)
1425 return -ENODEV;
1426
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001427 if (cmd == IIO_GET_EVENT_FD_IOCTL) {
1428 fd = iio_event_getfd(indio_dev);
Linus Walleij3f9059b2015-08-11 11:56:40 +02001429 if (fd < 0)
1430 return fd;
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001431 if (copy_to_user(ip, &fd, sizeof(fd)))
1432 return -EFAULT;
1433 return 0;
1434 }
1435 return -EINVAL;
1436}
1437
Jonathan Cameron14555b12011-09-21 11:15:57 +01001438static const struct file_operations iio_buffer_fileops = {
1439 .read = iio_buffer_read_first_n_outer_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001440 .release = iio_chrdev_release,
1441 .open = iio_chrdev_open,
Jonathan Cameron14555b12011-09-21 11:15:57 +01001442 .poll = iio_buffer_poll_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001443 .owner = THIS_MODULE,
1444 .llseek = noop_llseek,
1445 .unlocked_ioctl = iio_ioctl,
1446 .compat_ioctl = iio_ioctl,
1447};
1448
Vlad Dogaru8f5d8722014-12-29 11:37:48 +02001449static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
1450{
1451 int i, j;
1452 const struct iio_chan_spec *channels = indio_dev->channels;
1453
1454 if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
1455 return 0;
1456
1457 for (i = 0; i < indio_dev->num_channels - 1; i++) {
1458 if (channels[i].scan_index < 0)
1459 continue;
1460 for (j = i + 1; j < indio_dev->num_channels; j++)
1461 if (channels[i].scan_index == channels[j].scan_index) {
1462 dev_err(&indio_dev->dev,
1463 "Duplicate scan index %d\n",
1464 channels[i].scan_index);
1465 return -EINVAL;
1466 }
1467 }
1468
1469 return 0;
1470}
1471
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001472static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1473
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001474/**
1475 * iio_device_register() - register a device with the IIO subsystem
1476 * @indio_dev: Device structure filled by the device driver
1477 **/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001478int iio_device_register(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001479{
1480 int ret;
1481
Guenter Roeck17d82b42013-02-07 17:09:00 +00001482 /* If the calling driver did not initialize of_node, do it here */
1483 if (!indio_dev->dev.of_node && indio_dev->dev.parent)
1484 indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
1485
Vlad Dogaru8f5d8722014-12-29 11:37:48 +02001486 ret = iio_check_unique_scan_index(indio_dev);
1487 if (ret < 0)
1488 return ret;
1489
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001490 /* configure elements for the chrdev */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001491 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001492
Michael Henneriche553f182012-03-01 10:51:03 +01001493 ret = iio_device_register_debugfs(indio_dev);
1494 if (ret) {
1495 dev_err(indio_dev->dev.parent,
1496 "Failed to register debugfs interfaces\n");
Hartmut Knaack92825ff2014-02-16 11:53:00 +00001497 return ret;
Michael Henneriche553f182012-03-01 10:51:03 +01001498 }
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001499
1500 ret = iio_buffer_alloc_sysfs_and_mask(indio_dev);
1501 if (ret) {
1502 dev_err(indio_dev->dev.parent,
1503 "Failed to create buffer sysfs interfaces\n");
1504 goto error_unreg_debugfs;
1505 }
1506
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001507 ret = iio_device_register_sysfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001508 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001509 dev_err(indio_dev->dev.parent,
Jonathan Cameron847ec802009-08-18 18:06:19 +01001510 "Failed to register sysfs interfaces\n");
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001511 goto error_buffer_free_sysfs;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001512 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001513 ret = iio_device_register_eventset(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001514 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001515 dev_err(indio_dev->dev.parent,
Roel Van Nyenc849d252010-04-29 19:27:31 +02001516 "Failed to register event set\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001517 goto error_free_sysfs;
1518 }
Vladimir Barinov735ad072015-08-20 22:37:39 +03001519 if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED))
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001520 iio_device_register_trigger_consumer(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001521
Michael Hennerich0f1acee2012-03-01 10:51:04 +01001522 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
1523 indio_dev->setup_ops == NULL)
1524 indio_dev->setup_ops = &noop_ring_setup_ops;
1525
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001526 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
1527 indio_dev->chrdev.owner = indio_dev->info->driver_module;
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001528 indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001529 ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001530 if (ret < 0)
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001531 goto error_unreg_eventset;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001532
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001533 ret = device_add(&indio_dev->dev);
1534 if (ret < 0)
1535 goto error_cdev_del;
1536
1537 return 0;
1538error_cdev_del:
1539 cdev_del(&indio_dev->chrdev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001540error_unreg_eventset:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001541 iio_device_unregister_eventset(indio_dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001542error_free_sysfs:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001543 iio_device_unregister_sysfs(indio_dev);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001544error_buffer_free_sysfs:
1545 iio_buffer_free_sysfs_and_mask(indio_dev);
Michael Henneriche553f182012-03-01 10:51:03 +01001546error_unreg_debugfs:
1547 iio_device_unregister_debugfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001548 return ret;
1549}
1550EXPORT_SYMBOL(iio_device_register);
1551
Sachin Kamata7e57dc2013-10-29 11:39:00 +00001552/**
1553 * iio_device_unregister() - unregister a device from the IIO subsystem
1554 * @indio_dev: Device structure representing the device.
1555 **/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001556void iio_device_unregister(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001557{
Jonathan Cameronac917a82012-02-15 19:48:00 +00001558 mutex_lock(&indio_dev->info_exist_lock);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001559
1560 device_del(&indio_dev->dev);
1561
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001562 if (indio_dev->chrdev.dev)
1563 cdev_del(&indio_dev->chrdev);
Lars-Peter Clausenbc4c9612013-09-21 16:21:00 +01001564 iio_device_unregister_debugfs(indio_dev);
Lars-Peter Clausen0d5b7da2013-09-18 21:02:00 +01001565
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +01001566 iio_disable_all_buffers(indio_dev);
1567
Jonathan Cameronac917a82012-02-15 19:48:00 +00001568 indio_dev->info = NULL;
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +01001569
1570 iio_device_wakeup_eventset(indio_dev);
1571 iio_buffer_wakeup_poll(indio_dev);
1572
Jonathan Cameronac917a82012-02-15 19:48:00 +00001573 mutex_unlock(&indio_dev->info_exist_lock);
Lars-Peter Clausen3e1b6c92014-11-26 18:55:12 +01001574
1575 iio_buffer_free_sysfs_and_mask(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001576}
1577EXPORT_SYMBOL(iio_device_unregister);
Sachin Kamat8caa07c2013-10-29 11:39:00 +00001578
1579static void devm_iio_device_unreg(struct device *dev, void *res)
1580{
1581 iio_device_unregister(*(struct iio_dev **)res);
1582}
1583
1584/**
1585 * devm_iio_device_register - Resource-managed iio_device_register()
1586 * @dev: Device to allocate iio_dev for
1587 * @indio_dev: Device structure filled by the device driver
1588 *
1589 * Managed iio_device_register. The IIO device registered with this
1590 * function is automatically unregistered on driver detach. This function
1591 * calls iio_device_register() internally. Refer to that function for more
1592 * information.
1593 *
1594 * If an iio_dev registered with this function needs to be unregistered
1595 * separately, devm_iio_device_unregister() must be used.
1596 *
1597 * RETURNS:
1598 * 0 on success, negative error number on failure.
1599 */
1600int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev)
1601{
1602 struct iio_dev **ptr;
1603 int ret;
1604
1605 ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL);
1606 if (!ptr)
1607 return -ENOMEM;
1608
1609 *ptr = indio_dev;
1610 ret = iio_device_register(indio_dev);
1611 if (!ret)
1612 devres_add(dev, ptr);
1613 else
1614 devres_free(ptr);
1615
1616 return ret;
1617}
1618EXPORT_SYMBOL_GPL(devm_iio_device_register);
1619
1620/**
1621 * devm_iio_device_unregister - Resource-managed iio_device_unregister()
1622 * @dev: Device this iio_dev belongs to
1623 * @indio_dev: the iio_dev associated with the device
1624 *
1625 * Unregister iio_dev registered with devm_iio_device_register().
1626 */
1627void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev)
1628{
1629 int rc;
1630
1631 rc = devres_release(dev, devm_iio_device_unreg,
1632 devm_iio_device_match, indio_dev);
1633 WARN_ON(rc);
1634}
1635EXPORT_SYMBOL_GPL(devm_iio_device_unregister);
1636
Alison Schofield08a33802016-03-09 11:30:12 -08001637/**
1638 * iio_device_claim_direct_mode - Keep device in direct mode
1639 * @indio_dev: the iio_dev associated with the device
1640 *
1641 * If the device is in direct mode it is guaranteed to stay
1642 * that way until iio_device_release_direct_mode() is called.
1643 *
1644 * Use with iio_device_release_direct_mode()
1645 *
1646 * Returns: 0 on success, -EBUSY on failure
1647 */
1648int iio_device_claim_direct_mode(struct iio_dev *indio_dev)
1649{
1650 mutex_lock(&indio_dev->mlock);
1651
1652 if (iio_buffer_enabled(indio_dev)) {
1653 mutex_unlock(&indio_dev->mlock);
1654 return -EBUSY;
1655 }
1656 return 0;
1657}
1658EXPORT_SYMBOL_GPL(iio_device_claim_direct_mode);
1659
1660/**
1661 * iio_device_release_direct_mode - releases claim on direct mode
1662 * @indio_dev: the iio_dev associated with the device
1663 *
1664 * Release the claim. Device is no longer guaranteed to stay
1665 * in direct mode.
1666 *
1667 * Use with iio_device_claim_direct_mode()
1668 */
1669void iio_device_release_direct_mode(struct iio_dev *indio_dev)
1670{
1671 mutex_unlock(&indio_dev->mlock);
1672}
1673EXPORT_SYMBOL_GPL(iio_device_release_direct_mode);
1674
Jonathan Cameron847ec802009-08-18 18:06:19 +01001675subsys_initcall(iio_init);
1676module_exit(iio_exit);
1677
Jonathan Cameronc8b95952012-09-02 21:27:56 +01001678MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001679MODULE_DESCRIPTION("Industrial I/O core");
1680MODULE_LICENSE("GPL");