x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / iio / inkern.c
blob1e8e94d4db7dec54e620f5ce822b1dd0b71293cc
1 /* The industrial I/O core in kernel channel mapping
3 * Copyright (c) 2011 Jonathan Cameron
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 #include <linux/err.h>
10 #include <linux/export.h>
11 #include <linux/slab.h>
12 #include <linux/mutex.h>
13 #include <linux/of.h>
15 #include <linux/iio/iio.h>
16 #include "iio_core.h"
17 #include <linux/iio/machine.h>
18 #include <linux/iio/driver.h>
19 #include <linux/iio/consumer.h>
21 struct iio_map_internal {
22 struct iio_dev *indio_dev;
23 struct iio_map *map;
24 struct list_head l;
27 static LIST_HEAD(iio_map_list);
28 static DEFINE_MUTEX(iio_map_list_lock);
30 int iio_map_array_register(struct iio_dev *indio_dev, struct iio_map *maps)
32 int i = 0, ret = 0;
33 struct iio_map_internal *mapi;
35 if (maps == NULL)
36 return 0;
38 mutex_lock(&iio_map_list_lock);
39 while (maps[i].consumer_dev_name != NULL) {
40 mapi = kzalloc(sizeof(*mapi), GFP_KERNEL);
41 if (mapi == NULL) {
42 ret = -ENOMEM;
43 goto error_ret;
45 mapi->map = &maps[i];
46 mapi->indio_dev = indio_dev;
47 list_add(&mapi->l, &iio_map_list);
48 i++;
50 error_ret:
51 mutex_unlock(&iio_map_list_lock);
53 return ret;
55 EXPORT_SYMBOL_GPL(iio_map_array_register);
59 * Remove all map entries associated with the given iio device
61 int iio_map_array_unregister(struct iio_dev *indio_dev)
63 int ret = -ENODEV;
64 struct iio_map_internal *mapi;
65 struct list_head *pos, *tmp;
67 mutex_lock(&iio_map_list_lock);
68 list_for_each_safe(pos, tmp, &iio_map_list) {
69 mapi = list_entry(pos, struct iio_map_internal, l);
70 if (indio_dev == mapi->indio_dev) {
71 list_del(&mapi->l);
72 kfree(mapi);
73 ret = 0;
76 mutex_unlock(&iio_map_list_lock);
77 return ret;
79 EXPORT_SYMBOL_GPL(iio_map_array_unregister);
81 static const struct iio_chan_spec
82 *iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name)
84 int i;
85 const struct iio_chan_spec *chan = NULL;
87 for (i = 0; i < indio_dev->num_channels; i++)
88 if (indio_dev->channels[i].datasheet_name &&
89 strcmp(name, indio_dev->channels[i].datasheet_name) == 0) {
90 chan = &indio_dev->channels[i];
91 break;
93 return chan;
96 #ifdef CONFIG_OF
98 static int iio_dev_node_match(struct device *dev, void *data)
100 return dev->of_node == data && dev->type == &iio_device_type;
103 static int __of_iio_channel_get(struct iio_channel *channel,
104 struct device_node *np, int index)
106 struct device *idev;
107 struct iio_dev *indio_dev;
108 int err;
109 struct of_phandle_args iiospec;
111 err = of_parse_phandle_with_args(np, "io-channels",
112 "#io-channel-cells",
113 index, &iiospec);
114 if (err)
115 return err;
117 idev = bus_find_device(&iio_bus_type, NULL, iiospec.np,
118 iio_dev_node_match);
119 of_node_put(iiospec.np);
120 if (idev == NULL)
121 return -EPROBE_DEFER;
123 indio_dev = dev_to_iio_dev(idev);
124 channel->indio_dev = indio_dev;
125 index = iiospec.args_count ? iiospec.args[0] : 0;
126 if (index >= indio_dev->num_channels) {
127 err = -EINVAL;
128 goto err_put;
130 channel->channel = &indio_dev->channels[index];
132 return 0;
134 err_put:
135 iio_device_put(indio_dev);
136 return err;
139 static struct iio_channel *of_iio_channel_get(struct device_node *np, int index)
141 struct iio_channel *channel;
142 int err;
144 if (index < 0)
145 return ERR_PTR(-EINVAL);
147 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
148 if (channel == NULL)
149 return ERR_PTR(-ENOMEM);
151 err = __of_iio_channel_get(channel, np, index);
152 if (err)
153 goto err_free_channel;
155 return channel;
157 err_free_channel:
158 kfree(channel);
159 return ERR_PTR(err);
162 static struct iio_channel *of_iio_channel_get_by_name(struct device_node *np,
163 const char *name)
165 struct iio_channel *chan = NULL;
167 /* Walk up the tree of devices looking for a matching iio channel */
168 while (np) {
169 int index = 0;
172 * For named iio channels, first look up the name in the
173 * "io-channel-names" property. If it cannot be found, the
174 * index will be an error code, and of_iio_channel_get()
175 * will fail.
177 if (name)
178 index = of_property_match_string(np, "io-channel-names",
179 name);
180 chan = of_iio_channel_get(np, index);
181 if (!IS_ERR(chan))
182 break;
183 else if (name && index >= 0) {
184 pr_err("ERROR: could not get IIO channel %s:%s(%i)\n",
185 np->full_name, name ? name : "", index);
186 return NULL;
190 * No matching IIO channel found on this node.
191 * If the parent node has a "io-channel-ranges" property,
192 * then we can try one of its channels.
194 np = np->parent;
195 if (np && !of_get_property(np, "io-channel-ranges", NULL))
196 return NULL;
199 return chan;
202 static struct iio_channel *of_iio_channel_get_all(struct device *dev)
204 struct iio_channel *chans;
205 int i, mapind, nummaps = 0;
206 int ret;
208 do {
209 ret = of_parse_phandle_with_args(dev->of_node,
210 "io-channels",
211 "#io-channel-cells",
212 nummaps, NULL);
213 if (ret < 0)
214 break;
215 } while (++nummaps);
217 if (nummaps == 0) /* no error, return NULL to search map table */
218 return NULL;
220 /* NULL terminated array to save passing size */
221 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
222 if (chans == NULL)
223 return ERR_PTR(-ENOMEM);
225 /* Search for OF matches */
226 for (mapind = 0; mapind < nummaps; mapind++) {
227 ret = __of_iio_channel_get(&chans[mapind], dev->of_node,
228 mapind);
229 if (ret)
230 goto error_free_chans;
232 return chans;
234 error_free_chans:
235 for (i = 0; i < mapind; i++)
236 iio_device_put(chans[i].indio_dev);
237 kfree(chans);
238 return ERR_PTR(ret);
241 #else /* CONFIG_OF */
243 static inline struct iio_channel *
244 of_iio_channel_get_by_name(struct device_node *np, const char *name)
246 return NULL;
249 static inline struct iio_channel *of_iio_channel_get_all(struct device *dev)
251 return NULL;
254 #endif /* CONFIG_OF */
256 static struct iio_channel *iio_channel_get_sys(const char *name,
257 const char *channel_name)
259 struct iio_map_internal *c_i = NULL, *c = NULL;
260 struct iio_channel *channel;
261 int err;
263 if (name == NULL && channel_name == NULL)
264 return ERR_PTR(-ENODEV);
266 /* first find matching entry the channel map */
267 mutex_lock(&iio_map_list_lock);
268 list_for_each_entry(c_i, &iio_map_list, l) {
269 if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
270 (channel_name &&
271 strcmp(channel_name, c_i->map->consumer_channel) != 0))
272 continue;
273 c = c_i;
274 iio_device_get(c->indio_dev);
275 break;
277 mutex_unlock(&iio_map_list_lock);
278 if (c == NULL)
279 return ERR_PTR(-ENODEV);
281 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
282 if (channel == NULL) {
283 err = -ENOMEM;
284 goto error_no_mem;
287 channel->indio_dev = c->indio_dev;
289 if (c->map->adc_channel_label) {
290 channel->channel =
291 iio_chan_spec_from_name(channel->indio_dev,
292 c->map->adc_channel_label);
294 if (channel->channel == NULL) {
295 err = -EINVAL;
296 goto error_no_chan;
300 return channel;
302 error_no_chan:
303 kfree(channel);
304 error_no_mem:
305 iio_device_put(c->indio_dev);
306 return ERR_PTR(err);
309 struct iio_channel *iio_channel_get(struct device *dev,
310 const char *channel_name)
312 const char *name = dev ? dev_name(dev) : NULL;
313 struct iio_channel *channel;
315 if (dev) {
316 channel = of_iio_channel_get_by_name(dev->of_node,
317 channel_name);
318 if (channel != NULL)
319 return channel;
322 return iio_channel_get_sys(name, channel_name);
324 EXPORT_SYMBOL_GPL(iio_channel_get);
326 void iio_channel_release(struct iio_channel *channel)
328 iio_device_put(channel->indio_dev);
329 kfree(channel);
331 EXPORT_SYMBOL_GPL(iio_channel_release);
333 struct iio_channel *iio_channel_get_all(struct device *dev)
335 const char *name;
336 struct iio_channel *chans;
337 struct iio_map_internal *c = NULL;
338 int nummaps = 0;
339 int mapind = 0;
340 int i, ret;
342 if (dev == NULL)
343 return ERR_PTR(-EINVAL);
345 chans = of_iio_channel_get_all(dev);
346 if (chans)
347 return chans;
349 name = dev_name(dev);
351 mutex_lock(&iio_map_list_lock);
352 /* first count the matching maps */
353 list_for_each_entry(c, &iio_map_list, l)
354 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
355 continue;
356 else
357 nummaps++;
359 if (nummaps == 0) {
360 ret = -ENODEV;
361 goto error_ret;
364 /* NULL terminated array to save passing size */
365 chans = kzalloc(sizeof(*chans)*(nummaps + 1), GFP_KERNEL);
366 if (chans == NULL) {
367 ret = -ENOMEM;
368 goto error_ret;
371 /* for each map fill in the chans element */
372 list_for_each_entry(c, &iio_map_list, l) {
373 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
374 continue;
375 chans[mapind].indio_dev = c->indio_dev;
376 chans[mapind].data = c->map->consumer_data;
377 chans[mapind].channel =
378 iio_chan_spec_from_name(chans[mapind].indio_dev,
379 c->map->adc_channel_label);
380 if (chans[mapind].channel == NULL) {
381 ret = -EINVAL;
382 goto error_free_chans;
384 iio_device_get(chans[mapind].indio_dev);
385 mapind++;
387 if (mapind == 0) {
388 ret = -ENODEV;
389 goto error_free_chans;
391 mutex_unlock(&iio_map_list_lock);
393 return chans;
395 error_free_chans:
396 for (i = 0; i < nummaps; i++)
397 iio_device_put(chans[i].indio_dev);
398 kfree(chans);
399 error_ret:
400 mutex_unlock(&iio_map_list_lock);
402 return ERR_PTR(ret);
404 EXPORT_SYMBOL_GPL(iio_channel_get_all);
406 void iio_channel_release_all(struct iio_channel *channels)
408 struct iio_channel *chan = &channels[0];
410 while (chan->indio_dev) {
411 iio_device_put(chan->indio_dev);
412 chan++;
414 kfree(channels);
416 EXPORT_SYMBOL_GPL(iio_channel_release_all);
418 static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
419 enum iio_chan_info_enum info)
421 int unused;
423 if (val2 == NULL)
424 val2 = &unused;
426 return chan->indio_dev->info->read_raw(chan->indio_dev, chan->channel,
427 val, val2, info);
430 int iio_read_channel_raw(struct iio_channel *chan, int *val)
432 int ret;
434 mutex_lock(&chan->indio_dev->info_exist_lock);
435 if (chan->indio_dev->info == NULL) {
436 ret = -ENODEV;
437 goto err_unlock;
440 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
441 err_unlock:
442 mutex_unlock(&chan->indio_dev->info_exist_lock);
444 return ret;
446 EXPORT_SYMBOL_GPL(iio_read_channel_raw);
448 static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
449 int raw, int *processed, unsigned int scale)
451 int scale_type, scale_val, scale_val2, offset;
452 s64 raw64 = raw;
453 int ret;
455 ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_OFFSET);
456 if (ret >= 0)
457 raw64 += offset;
459 scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
460 IIO_CHAN_INFO_SCALE);
461 if (scale_type < 0)
462 return scale_type;
464 switch (scale_type) {
465 case IIO_VAL_INT:
466 *processed = raw64 * scale_val;
467 break;
468 case IIO_VAL_INT_PLUS_MICRO:
469 if (scale_val2 < 0)
470 *processed = -raw64 * scale_val;
471 else
472 *processed = raw64 * scale_val;
473 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
474 1000000LL);
475 break;
476 case IIO_VAL_INT_PLUS_NANO:
477 if (scale_val2 < 0)
478 *processed = -raw64 * scale_val;
479 else
480 *processed = raw64 * scale_val;
481 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
482 1000000000LL);
483 break;
484 case IIO_VAL_FRACTIONAL:
485 *processed = div_s64(raw64 * (s64)scale_val * scale,
486 scale_val2);
487 break;
488 case IIO_VAL_FRACTIONAL_LOG2:
489 *processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
490 break;
491 default:
492 return -EINVAL;
495 return 0;
498 int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
499 int *processed, unsigned int scale)
501 int ret;
503 mutex_lock(&chan->indio_dev->info_exist_lock);
504 if (chan->indio_dev->info == NULL) {
505 ret = -ENODEV;
506 goto err_unlock;
509 ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
510 scale);
511 err_unlock:
512 mutex_unlock(&chan->indio_dev->info_exist_lock);
514 return ret;
516 EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
518 int iio_read_channel_processed(struct iio_channel *chan, int *val)
520 int ret;
522 mutex_lock(&chan->indio_dev->info_exist_lock);
523 if (chan->indio_dev->info == NULL) {
524 ret = -ENODEV;
525 goto err_unlock;
528 if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
529 ret = iio_channel_read(chan, val, NULL,
530 IIO_CHAN_INFO_PROCESSED);
531 } else {
532 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
533 if (ret < 0)
534 goto err_unlock;
535 ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1);
538 err_unlock:
539 mutex_unlock(&chan->indio_dev->info_exist_lock);
541 return ret;
543 EXPORT_SYMBOL_GPL(iio_read_channel_processed);
545 int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
547 int ret;
549 mutex_lock(&chan->indio_dev->info_exist_lock);
550 if (chan->indio_dev->info == NULL) {
551 ret = -ENODEV;
552 goto err_unlock;
555 ret = iio_channel_read(chan, val, val2, IIO_CHAN_INFO_SCALE);
556 err_unlock:
557 mutex_unlock(&chan->indio_dev->info_exist_lock);
559 return ret;
561 EXPORT_SYMBOL_GPL(iio_read_channel_scale);
563 int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
565 int ret = 0;
566 /* Need to verify underlying driver has not gone away */
568 mutex_lock(&chan->indio_dev->info_exist_lock);
569 if (chan->indio_dev->info == NULL) {
570 ret = -ENODEV;
571 goto err_unlock;
574 *type = chan->channel->type;
575 err_unlock:
576 mutex_unlock(&chan->indio_dev->info_exist_lock);
578 return ret;
580 EXPORT_SYMBOL_GPL(iio_get_channel_type);