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.
10 #include <linux/export.h>
11 #include <linux/slab.h>
12 #include <linux/mutex.h>
15 #include <linux/iio/iio.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
;
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
)
33 struct iio_map_internal
*mapi
;
38 mutex_lock(&iio_map_list_lock
);
39 while (maps
[i
].consumer_dev_name
!= NULL
) {
40 mapi
= kzalloc(sizeof(*mapi
), GFP_KERNEL
);
46 mapi
->indio_dev
= indio_dev
;
47 list_add(&mapi
->l
, &iio_map_list
);
51 mutex_unlock(&iio_map_list_lock
);
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
)
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
) {
76 mutex_unlock(&iio_map_list_lock
);
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
)
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
];
98 static int iio_dev_node_match(struct device
*dev
, void *data
)
100 return dev
->of_node
== data
&& dev
->type
== &iio_device_type
;
104 * __of_iio_simple_xlate - translate iiospec to the IIO channel index
105 * @indio_dev: pointer to the iio_dev structure
106 * @iiospec: IIO specifier as found in the device tree
108 * This is simple translation function, suitable for the most 1:1 mapped
109 * channels in IIO chips. This function performs only one sanity check:
110 * whether IIO index is less than num_channels (that is specified in the
113 static int __of_iio_simple_xlate(struct iio_dev
*indio_dev
,
114 const struct of_phandle_args
*iiospec
)
116 if (!iiospec
->args_count
)
119 if (iiospec
->args
[0] >= indio_dev
->num_channels
)
122 return iiospec
->args
[0];
125 static int __of_iio_channel_get(struct iio_channel
*channel
,
126 struct device_node
*np
, int index
)
129 struct iio_dev
*indio_dev
;
131 struct of_phandle_args iiospec
;
133 err
= of_parse_phandle_with_args(np
, "io-channels",
139 idev
= bus_find_device(&iio_bus_type
, NULL
, iiospec
.np
,
141 of_node_put(iiospec
.np
);
143 return -EPROBE_DEFER
;
145 indio_dev
= dev_to_iio_dev(idev
);
146 channel
->indio_dev
= indio_dev
;
147 if (indio_dev
->info
->of_xlate
)
148 index
= indio_dev
->info
->of_xlate(indio_dev
, &iiospec
);
150 index
= __of_iio_simple_xlate(indio_dev
, &iiospec
);
153 channel
->channel
= &indio_dev
->channels
[index
];
158 iio_device_put(indio_dev
);
162 static struct iio_channel
*of_iio_channel_get(struct device_node
*np
, int index
)
164 struct iio_channel
*channel
;
168 return ERR_PTR(-EINVAL
);
170 channel
= kzalloc(sizeof(*channel
), GFP_KERNEL
);
172 return ERR_PTR(-ENOMEM
);
174 err
= __of_iio_channel_get(channel
, np
, index
);
176 goto err_free_channel
;
185 static struct iio_channel
*of_iio_channel_get_by_name(struct device_node
*np
,
188 struct iio_channel
*chan
= NULL
;
190 /* Walk up the tree of devices looking for a matching iio channel */
195 * For named iio channels, first look up the name in the
196 * "io-channel-names" property. If it cannot be found, the
197 * index will be an error code, and of_iio_channel_get()
201 index
= of_property_match_string(np
, "io-channel-names",
203 chan
= of_iio_channel_get(np
, index
);
204 if (!IS_ERR(chan
) || PTR_ERR(chan
) == -EPROBE_DEFER
)
206 else if (name
&& index
>= 0) {
207 pr_err("ERROR: could not get IIO channel %s:%s(%i)\n",
208 np
->full_name
, name
? name
: "", index
);
213 * No matching IIO channel found on this node.
214 * If the parent node has a "io-channel-ranges" property,
215 * then we can try one of its channels.
218 if (np
&& !of_get_property(np
, "io-channel-ranges", NULL
))
225 static struct iio_channel
*of_iio_channel_get_all(struct device
*dev
)
227 struct iio_channel
*chans
;
228 int i
, mapind
, nummaps
= 0;
232 ret
= of_parse_phandle_with_args(dev
->of_node
,
240 if (nummaps
== 0) /* no error, return NULL to search map table */
243 /* NULL terminated array to save passing size */
244 chans
= kcalloc(nummaps
+ 1, sizeof(*chans
), GFP_KERNEL
);
246 return ERR_PTR(-ENOMEM
);
248 /* Search for OF matches */
249 for (mapind
= 0; mapind
< nummaps
; mapind
++) {
250 ret
= __of_iio_channel_get(&chans
[mapind
], dev
->of_node
,
253 goto error_free_chans
;
258 for (i
= 0; i
< mapind
; i
++)
259 iio_device_put(chans
[i
].indio_dev
);
264 #else /* CONFIG_OF */
266 static inline struct iio_channel
*
267 of_iio_channel_get_by_name(struct device_node
*np
, const char *name
)
272 static inline struct iio_channel
*of_iio_channel_get_all(struct device
*dev
)
277 #endif /* CONFIG_OF */
279 static struct iio_channel
*iio_channel_get_sys(const char *name
,
280 const char *channel_name
)
282 struct iio_map_internal
*c_i
= NULL
, *c
= NULL
;
283 struct iio_channel
*channel
;
286 if (name
== NULL
&& channel_name
== NULL
)
287 return ERR_PTR(-ENODEV
);
289 /* first find matching entry the channel map */
290 mutex_lock(&iio_map_list_lock
);
291 list_for_each_entry(c_i
, &iio_map_list
, l
) {
292 if ((name
&& strcmp(name
, c_i
->map
->consumer_dev_name
) != 0) ||
294 strcmp(channel_name
, c_i
->map
->consumer_channel
) != 0))
297 iio_device_get(c
->indio_dev
);
300 mutex_unlock(&iio_map_list_lock
);
302 return ERR_PTR(-ENODEV
);
304 channel
= kzalloc(sizeof(*channel
), GFP_KERNEL
);
305 if (channel
== NULL
) {
310 channel
->indio_dev
= c
->indio_dev
;
312 if (c
->map
->adc_channel_label
) {
314 iio_chan_spec_from_name(channel
->indio_dev
,
315 c
->map
->adc_channel_label
);
317 if (channel
->channel
== NULL
) {
328 iio_device_put(c
->indio_dev
);
332 struct iio_channel
*iio_channel_get(struct device
*dev
,
333 const char *channel_name
)
335 const char *name
= dev
? dev_name(dev
) : NULL
;
336 struct iio_channel
*channel
;
339 channel
= of_iio_channel_get_by_name(dev
->of_node
,
345 return iio_channel_get_sys(name
, channel_name
);
347 EXPORT_SYMBOL_GPL(iio_channel_get
);
349 void iio_channel_release(struct iio_channel
*channel
)
351 iio_device_put(channel
->indio_dev
);
354 EXPORT_SYMBOL_GPL(iio_channel_release
);
356 struct iio_channel
*iio_channel_get_all(struct device
*dev
)
359 struct iio_channel
*chans
;
360 struct iio_map_internal
*c
= NULL
;
366 return ERR_PTR(-EINVAL
);
368 chans
= of_iio_channel_get_all(dev
);
372 name
= dev_name(dev
);
374 mutex_lock(&iio_map_list_lock
);
375 /* first count the matching maps */
376 list_for_each_entry(c
, &iio_map_list
, l
)
377 if (name
&& strcmp(name
, c
->map
->consumer_dev_name
) != 0)
387 /* NULL terminated array to save passing size */
388 chans
= kzalloc(sizeof(*chans
)*(nummaps
+ 1), GFP_KERNEL
);
394 /* for each map fill in the chans element */
395 list_for_each_entry(c
, &iio_map_list
, l
) {
396 if (name
&& strcmp(name
, c
->map
->consumer_dev_name
) != 0)
398 chans
[mapind
].indio_dev
= c
->indio_dev
;
399 chans
[mapind
].data
= c
->map
->consumer_data
;
400 chans
[mapind
].channel
=
401 iio_chan_spec_from_name(chans
[mapind
].indio_dev
,
402 c
->map
->adc_channel_label
);
403 if (chans
[mapind
].channel
== NULL
) {
405 goto error_free_chans
;
407 iio_device_get(chans
[mapind
].indio_dev
);
412 goto error_free_chans
;
414 mutex_unlock(&iio_map_list_lock
);
419 for (i
= 0; i
< nummaps
; i
++)
420 iio_device_put(chans
[i
].indio_dev
);
423 mutex_unlock(&iio_map_list_lock
);
427 EXPORT_SYMBOL_GPL(iio_channel_get_all
);
429 void iio_channel_release_all(struct iio_channel
*channels
)
431 struct iio_channel
*chan
= &channels
[0];
433 while (chan
->indio_dev
) {
434 iio_device_put(chan
->indio_dev
);
439 EXPORT_SYMBOL_GPL(iio_channel_release_all
);
441 static int iio_channel_read(struct iio_channel
*chan
, int *val
, int *val2
,
442 enum iio_chan_info_enum info
)
445 int vals
[INDIO_MAX_RAW_ELEMENTS
];
452 if (chan
->indio_dev
->info
->read_raw_multi
) {
453 ret
= chan
->indio_dev
->info
->read_raw_multi(chan
->indio_dev
,
454 chan
->channel
, INDIO_MAX_RAW_ELEMENTS
,
455 vals
, &val_len
, info
);
459 ret
= chan
->indio_dev
->info
->read_raw(chan
->indio_dev
,
460 chan
->channel
, val
, val2
, info
);
465 int iio_read_channel_raw(struct iio_channel
*chan
, int *val
)
469 mutex_lock(&chan
->indio_dev
->info_exist_lock
);
470 if (chan
->indio_dev
->info
== NULL
) {
475 ret
= iio_channel_read(chan
, val
, NULL
, IIO_CHAN_INFO_RAW
);
477 mutex_unlock(&chan
->indio_dev
->info_exist_lock
);
481 EXPORT_SYMBOL_GPL(iio_read_channel_raw
);
483 int iio_read_channel_average_raw(struct iio_channel
*chan
, int *val
)
487 mutex_lock(&chan
->indio_dev
->info_exist_lock
);
488 if (chan
->indio_dev
->info
== NULL
) {
493 ret
= iio_channel_read(chan
, val
, NULL
, IIO_CHAN_INFO_AVERAGE_RAW
);
495 mutex_unlock(&chan
->indio_dev
->info_exist_lock
);
499 EXPORT_SYMBOL_GPL(iio_read_channel_average_raw
);
501 static int iio_convert_raw_to_processed_unlocked(struct iio_channel
*chan
,
502 int raw
, int *processed
, unsigned int scale
)
504 int scale_type
, scale_val
, scale_val2
, offset
;
508 ret
= iio_channel_read(chan
, &offset
, NULL
, IIO_CHAN_INFO_OFFSET
);
512 scale_type
= iio_channel_read(chan
, &scale_val
, &scale_val2
,
513 IIO_CHAN_INFO_SCALE
);
517 switch (scale_type
) {
519 *processed
= raw64
* scale_val
;
521 case IIO_VAL_INT_PLUS_MICRO
:
523 *processed
= -raw64
* scale_val
;
525 *processed
= raw64
* scale_val
;
526 *processed
+= div_s64(raw64
* (s64
)scale_val2
* scale
,
529 case IIO_VAL_INT_PLUS_NANO
:
531 *processed
= -raw64
* scale_val
;
533 *processed
= raw64
* scale_val
;
534 *processed
+= div_s64(raw64
* (s64
)scale_val2
* scale
,
537 case IIO_VAL_FRACTIONAL
:
538 *processed
= div_s64(raw64
* (s64
)scale_val
* scale
,
541 case IIO_VAL_FRACTIONAL_LOG2
:
542 *processed
= (raw64
* (s64
)scale_val
* scale
) >> scale_val2
;
551 int iio_convert_raw_to_processed(struct iio_channel
*chan
, int raw
,
552 int *processed
, unsigned int scale
)
556 mutex_lock(&chan
->indio_dev
->info_exist_lock
);
557 if (chan
->indio_dev
->info
== NULL
) {
562 ret
= iio_convert_raw_to_processed_unlocked(chan
, raw
, processed
,
565 mutex_unlock(&chan
->indio_dev
->info_exist_lock
);
569 EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed
);
571 int iio_read_channel_processed(struct iio_channel
*chan
, int *val
)
575 mutex_lock(&chan
->indio_dev
->info_exist_lock
);
576 if (chan
->indio_dev
->info
== NULL
) {
581 if (iio_channel_has_info(chan
->channel
, IIO_CHAN_INFO_PROCESSED
)) {
582 ret
= iio_channel_read(chan
, val
, NULL
,
583 IIO_CHAN_INFO_PROCESSED
);
585 ret
= iio_channel_read(chan
, val
, NULL
, IIO_CHAN_INFO_RAW
);
588 ret
= iio_convert_raw_to_processed_unlocked(chan
, *val
, val
, 1);
592 mutex_unlock(&chan
->indio_dev
->info_exist_lock
);
596 EXPORT_SYMBOL_GPL(iio_read_channel_processed
);
598 int iio_read_channel_scale(struct iio_channel
*chan
, int *val
, int *val2
)
602 mutex_lock(&chan
->indio_dev
->info_exist_lock
);
603 if (chan
->indio_dev
->info
== NULL
) {
608 ret
= iio_channel_read(chan
, val
, val2
, IIO_CHAN_INFO_SCALE
);
610 mutex_unlock(&chan
->indio_dev
->info_exist_lock
);
614 EXPORT_SYMBOL_GPL(iio_read_channel_scale
);
616 int iio_get_channel_type(struct iio_channel
*chan
, enum iio_chan_type
*type
)
619 /* Need to verify underlying driver has not gone away */
621 mutex_lock(&chan
->indio_dev
->info_exist_lock
);
622 if (chan
->indio_dev
->info
== NULL
) {
627 *type
= chan
->channel
->type
;
629 mutex_unlock(&chan
->indio_dev
->info_exist_lock
);
633 EXPORT_SYMBOL_GPL(iio_get_channel_type
);