]>
Commit | Line | Data |
---|---|---|
e27d75d7 JC |
1 | /* The industrial I/O core in kernel channel mapping |
2 | * | |
3 | * Copyright (c) 2011 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 | #include <linux/err.h> | |
10 | #include <linux/export.h> | |
11 | #include <linux/slab.h> | |
12 | #include <linux/mutex.h> | |
17d82b47 | 13 | #include <linux/of.h> |
e27d75d7 | 14 | |
06458e27 | 15 | #include <linux/iio/iio.h> |
e27d75d7 | 16 | #include "iio_core.h" |
06458e27 JC |
17 | #include <linux/iio/machine.h> |
18 | #include <linux/iio/driver.h> | |
19 | #include <linux/iio/consumer.h> | |
e27d75d7 JC |
20 | |
21 | struct iio_map_internal { | |
22 | struct iio_dev *indio_dev; | |
23 | struct iio_map *map; | |
24 | struct list_head l; | |
25 | }; | |
26 | ||
27 | static LIST_HEAD(iio_map_list); | |
28 | static DEFINE_MUTEX(iio_map_list_lock); | |
29 | ||
30 | int iio_map_array_register(struct iio_dev *indio_dev, struct iio_map *maps) | |
31 | { | |
32 | int i = 0, ret = 0; | |
33 | struct iio_map_internal *mapi; | |
34 | ||
35 | if (maps == NULL) | |
36 | return 0; | |
37 | ||
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; | |
44 | } | |
45 | mapi->map = &maps[i]; | |
46 | mapi->indio_dev = indio_dev; | |
bc4b2a51 | 47 | list_add_tail(&mapi->l, &iio_map_list); |
e27d75d7 JC |
48 | i++; |
49 | } | |
50 | error_ret: | |
51 | mutex_unlock(&iio_map_list_lock); | |
52 | ||
53 | return ret; | |
54 | } | |
55 | EXPORT_SYMBOL_GPL(iio_map_array_register); | |
56 | ||
57 | ||
6cb2afd7 GR |
58 | /* |
59 | * Remove all map entries associated with the given iio device | |
e27d75d7 | 60 | */ |
6cb2afd7 | 61 | int iio_map_array_unregister(struct iio_dev *indio_dev) |
e27d75d7 | 62 | { |
6cb2afd7 | 63 | int ret = -ENODEV; |
c34c1819 | 64 | struct iio_map_internal *mapi, *next; |
e27d75d7 JC |
65 | |
66 | mutex_lock(&iio_map_list_lock); | |
c34c1819 | 67 | list_for_each_entry_safe(mapi, next, &iio_map_list, l) { |
6cb2afd7 GR |
68 | if (indio_dev == mapi->indio_dev) { |
69 | list_del(&mapi->l); | |
70 | kfree(mapi); | |
71 | ret = 0; | |
e27d75d7 JC |
72 | } |
73 | } | |
e27d75d7 | 74 | mutex_unlock(&iio_map_list_lock); |
e27d75d7 JC |
75 | return ret; |
76 | } | |
77 | EXPORT_SYMBOL_GPL(iio_map_array_unregister); | |
78 | ||
79 | static const struct iio_chan_spec | |
314be14b | 80 | *iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name) |
e27d75d7 JC |
81 | { |
82 | int i; | |
83 | const struct iio_chan_spec *chan = NULL; | |
84 | ||
85 | for (i = 0; i < indio_dev->num_channels; i++) | |
86 | if (indio_dev->channels[i].datasheet_name && | |
87 | strcmp(name, indio_dev->channels[i].datasheet_name) == 0) { | |
88 | chan = &indio_dev->channels[i]; | |
89 | break; | |
90 | } | |
91 | return chan; | |
92 | } | |
93 | ||
17d82b47 GR |
94 | #ifdef CONFIG_OF |
95 | ||
96 | static int iio_dev_node_match(struct device *dev, void *data) | |
97 | { | |
98 | return dev->of_node == data && dev->type == &iio_device_type; | |
99 | } | |
100 | ||
acd82567 II |
101 | /** |
102 | * __of_iio_simple_xlate - translate iiospec to the IIO channel index | |
103 | * @indio_dev: pointer to the iio_dev structure | |
104 | * @iiospec: IIO specifier as found in the device tree | |
105 | * | |
106 | * This is simple translation function, suitable for the most 1:1 mapped | |
107 | * channels in IIO chips. This function performs only one sanity check: | |
108 | * whether IIO index is less than num_channels (that is specified in the | |
109 | * iio_dev). | |
110 | */ | |
111 | static int __of_iio_simple_xlate(struct iio_dev *indio_dev, | |
112 | const struct of_phandle_args *iiospec) | |
113 | { | |
114 | if (!iiospec->args_count) | |
115 | return 0; | |
116 | ||
1f202725 SW |
117 | if (iiospec->args[0] >= indio_dev->num_channels) { |
118 | dev_err(&indio_dev->dev, "invalid channel index %u\n", | |
119 | iiospec->args[0]); | |
acd82567 | 120 | return -EINVAL; |
1f202725 | 121 | } |
acd82567 II |
122 | |
123 | return iiospec->args[0]; | |
124 | } | |
125 | ||
17d82b47 GR |
126 | static int __of_iio_channel_get(struct iio_channel *channel, |
127 | struct device_node *np, int index) | |
128 | { | |
129 | struct device *idev; | |
130 | struct iio_dev *indio_dev; | |
131 | int err; | |
132 | struct of_phandle_args iiospec; | |
133 | ||
134 | err = of_parse_phandle_with_args(np, "io-channels", | |
135 | "#io-channel-cells", | |
136 | index, &iiospec); | |
137 | if (err) | |
138 | return err; | |
139 | ||
140 | idev = bus_find_device(&iio_bus_type, NULL, iiospec.np, | |
141 | iio_dev_node_match); | |
142 | of_node_put(iiospec.np); | |
143 | if (idev == NULL) | |
144 | return -EPROBE_DEFER; | |
145 | ||
146 | indio_dev = dev_to_iio_dev(idev); | |
147 | channel->indio_dev = indio_dev; | |
acd82567 II |
148 | if (indio_dev->info->of_xlate) |
149 | index = indio_dev->info->of_xlate(indio_dev, &iiospec); | |
150 | else | |
151 | index = __of_iio_simple_xlate(indio_dev, &iiospec); | |
152 | if (index < 0) | |
17d82b47 | 153 | goto err_put; |
17d82b47 GR |
154 | channel->channel = &indio_dev->channels[index]; |
155 | ||
156 | return 0; | |
157 | ||
158 | err_put: | |
159 | iio_device_put(indio_dev); | |
acd82567 | 160 | return index; |
17d82b47 GR |
161 | } |
162 | ||
163 | static struct iio_channel *of_iio_channel_get(struct device_node *np, int index) | |
164 | { | |
165 | struct iio_channel *channel; | |
166 | int err; | |
167 | ||
168 | if (index < 0) | |
169 | return ERR_PTR(-EINVAL); | |
170 | ||
171 | channel = kzalloc(sizeof(*channel), GFP_KERNEL); | |
172 | if (channel == NULL) | |
173 | return ERR_PTR(-ENOMEM); | |
174 | ||
175 | err = __of_iio_channel_get(channel, np, index); | |
176 | if (err) | |
177 | goto err_free_channel; | |
178 | ||
179 | return channel; | |
180 | ||
181 | err_free_channel: | |
182 | kfree(channel); | |
183 | return ERR_PTR(err); | |
184 | } | |
185 | ||
186 | static struct iio_channel *of_iio_channel_get_by_name(struct device_node *np, | |
187 | const char *name) | |
188 | { | |
189 | struct iio_channel *chan = NULL; | |
190 | ||
191 | /* Walk up the tree of devices looking for a matching iio channel */ | |
192 | while (np) { | |
193 | int index = 0; | |
194 | ||
195 | /* | |
196 | * For named iio channels, first look up the name in the | |
197 | * "io-channel-names" property. If it cannot be found, the | |
198 | * index will be an error code, and of_iio_channel_get() | |
199 | * will fail. | |
200 | */ | |
201 | if (name) | |
202 | index = of_property_match_string(np, "io-channel-names", | |
203 | name); | |
204 | chan = of_iio_channel_get(np, index); | |
872687f6 | 205 | if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER) |
17d82b47 GR |
206 | break; |
207 | else if (name && index >= 0) { | |
3921db46 RH |
208 | pr_err("ERROR: could not get IIO channel %pOF:%s(%i)\n", |
209 | np, name ? name : "", index); | |
a2c12493 | 210 | return NULL; |
17d82b47 GR |
211 | } |
212 | ||
213 | /* | |
214 | * No matching IIO channel found on this node. | |
215 | * If the parent node has a "io-channel-ranges" property, | |
216 | * then we can try one of its channels. | |
217 | */ | |
218 | np = np->parent; | |
219 | if (np && !of_get_property(np, "io-channel-ranges", NULL)) | |
a2c12493 | 220 | return NULL; |
17d82b47 | 221 | } |
a2c12493 | 222 | |
17d82b47 GR |
223 | return chan; |
224 | } | |
225 | ||
226 | static struct iio_channel *of_iio_channel_get_all(struct device *dev) | |
227 | { | |
228 | struct iio_channel *chans; | |
229 | int i, mapind, nummaps = 0; | |
230 | int ret; | |
231 | ||
232 | do { | |
233 | ret = of_parse_phandle_with_args(dev->of_node, | |
234 | "io-channels", | |
235 | "#io-channel-cells", | |
236 | nummaps, NULL); | |
237 | if (ret < 0) | |
238 | break; | |
239 | } while (++nummaps); | |
240 | ||
241 | if (nummaps == 0) /* no error, return NULL to search map table */ | |
242 | return NULL; | |
243 | ||
244 | /* NULL terminated array to save passing size */ | |
245 | chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL); | |
246 | if (chans == NULL) | |
247 | return ERR_PTR(-ENOMEM); | |
248 | ||
249 | /* Search for OF matches */ | |
250 | for (mapind = 0; mapind < nummaps; mapind++) { | |
251 | ret = __of_iio_channel_get(&chans[mapind], dev->of_node, | |
252 | mapind); | |
253 | if (ret) | |
254 | goto error_free_chans; | |
255 | } | |
256 | return chans; | |
257 | ||
258 | error_free_chans: | |
259 | for (i = 0; i < mapind; i++) | |
260 | iio_device_put(chans[i].indio_dev); | |
261 | kfree(chans); | |
262 | return ERR_PTR(ret); | |
263 | } | |
264 | ||
265 | #else /* CONFIG_OF */ | |
266 | ||
267 | static inline struct iio_channel * | |
268 | of_iio_channel_get_by_name(struct device_node *np, const char *name) | |
269 | { | |
270 | return NULL; | |
271 | } | |
272 | ||
273 | static inline struct iio_channel *of_iio_channel_get_all(struct device *dev) | |
274 | { | |
275 | return NULL; | |
276 | } | |
277 | ||
278 | #endif /* CONFIG_OF */ | |
e27d75d7 | 279 | |
5aa57f0a GR |
280 | static struct iio_channel *iio_channel_get_sys(const char *name, |
281 | const char *channel_name) | |
e27d75d7 JC |
282 | { |
283 | struct iio_map_internal *c_i = NULL, *c = NULL; | |
284 | struct iio_channel *channel; | |
3183bac1 | 285 | int err; |
e27d75d7 JC |
286 | |
287 | if (name == NULL && channel_name == NULL) | |
288 | return ERR_PTR(-ENODEV); | |
289 | ||
290 | /* first find matching entry the channel map */ | |
291 | mutex_lock(&iio_map_list_lock); | |
292 | list_for_each_entry(c_i, &iio_map_list, l) { | |
293 | if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) || | |
294 | (channel_name && | |
295 | strcmp(channel_name, c_i->map->consumer_channel) != 0)) | |
296 | continue; | |
297 | c = c_i; | |
1875ffd2 | 298 | iio_device_get(c->indio_dev); |
e27d75d7 JC |
299 | break; |
300 | } | |
301 | mutex_unlock(&iio_map_list_lock); | |
302 | if (c == NULL) | |
303 | return ERR_PTR(-ENODEV); | |
304 | ||
2cc412b5 | 305 | channel = kzalloc(sizeof(*channel), GFP_KERNEL); |
3183bac1 KM |
306 | if (channel == NULL) { |
307 | err = -ENOMEM; | |
801c4b5c | 308 | goto error_no_mem; |
3183bac1 | 309 | } |
e27d75d7 JC |
310 | |
311 | channel->indio_dev = c->indio_dev; | |
312 | ||
b2b79ffa | 313 | if (c->map->adc_channel_label) { |
e27d75d7 JC |
314 | channel->channel = |
315 | iio_chan_spec_from_name(channel->indio_dev, | |
316 | c->map->adc_channel_label); | |
317 | ||
3183bac1 KM |
318 | if (channel->channel == NULL) { |
319 | err = -EINVAL; | |
b2b79ffa | 320 | goto error_no_chan; |
3183bac1 | 321 | } |
b2b79ffa KM |
322 | } |
323 | ||
e27d75d7 | 324 | return channel; |
b2b79ffa KM |
325 | |
326 | error_no_chan: | |
b2b79ffa | 327 | kfree(channel); |
801c4b5c KM |
328 | error_no_mem: |
329 | iio_device_put(c->indio_dev); | |
3183bac1 | 330 | return ERR_PTR(err); |
e27d75d7 | 331 | } |
5aa57f0a GR |
332 | |
333 | struct iio_channel *iio_channel_get(struct device *dev, | |
334 | const char *channel_name) | |
335 | { | |
336 | const char *name = dev ? dev_name(dev) : NULL; | |
17d82b47 | 337 | struct iio_channel *channel; |
5aa57f0a | 338 | |
17d82b47 GR |
339 | if (dev) { |
340 | channel = of_iio_channel_get_by_name(dev->of_node, | |
341 | channel_name); | |
342 | if (channel != NULL) | |
343 | return channel; | |
344 | } | |
a2c12493 | 345 | |
5aa57f0a GR |
346 | return iio_channel_get_sys(name, channel_name); |
347 | } | |
314be14b | 348 | EXPORT_SYMBOL_GPL(iio_channel_get); |
e27d75d7 | 349 | |
314be14b | 350 | void iio_channel_release(struct iio_channel *channel) |
e27d75d7 | 351 | { |
d81dac3c DC |
352 | if (!channel) |
353 | return; | |
1875ffd2 | 354 | iio_device_put(channel->indio_dev); |
e27d75d7 JC |
355 | kfree(channel); |
356 | } | |
314be14b | 357 | EXPORT_SYMBOL_GPL(iio_channel_release); |
e27d75d7 | 358 | |
8bf872d8 LD |
359 | static void devm_iio_channel_free(struct device *dev, void *res) |
360 | { | |
361 | struct iio_channel *channel = *(struct iio_channel **)res; | |
362 | ||
363 | iio_channel_release(channel); | |
364 | } | |
365 | ||
366 | static int devm_iio_channel_match(struct device *dev, void *res, void *data) | |
367 | { | |
368 | struct iio_channel **r = res; | |
369 | ||
370 | if (!r || !*r) { | |
371 | WARN_ON(!r || !*r); | |
372 | return 0; | |
373 | } | |
374 | ||
375 | return *r == data; | |
376 | } | |
377 | ||
378 | struct iio_channel *devm_iio_channel_get(struct device *dev, | |
379 | const char *channel_name) | |
380 | { | |
381 | struct iio_channel **ptr, *channel; | |
382 | ||
383 | ptr = devres_alloc(devm_iio_channel_free, sizeof(*ptr), GFP_KERNEL); | |
384 | if (!ptr) | |
385 | return ERR_PTR(-ENOMEM); | |
386 | ||
387 | channel = iio_channel_get(dev, channel_name); | |
388 | if (IS_ERR(channel)) { | |
389 | devres_free(ptr); | |
390 | return channel; | |
391 | } | |
392 | ||
393 | *ptr = channel; | |
394 | devres_add(dev, ptr); | |
395 | ||
396 | return channel; | |
397 | } | |
398 | EXPORT_SYMBOL_GPL(devm_iio_channel_get); | |
399 | ||
400 | void devm_iio_channel_release(struct device *dev, struct iio_channel *channel) | |
401 | { | |
402 | WARN_ON(devres_release(dev, devm_iio_channel_free, | |
403 | devm_iio_channel_match, channel)); | |
404 | } | |
405 | EXPORT_SYMBOL_GPL(devm_iio_channel_release); | |
406 | ||
ca7d98db | 407 | struct iio_channel *iio_channel_get_all(struct device *dev) |
e27d75d7 | 408 | { |
ca7d98db | 409 | const char *name; |
e27d75d7 JC |
410 | struct iio_channel *chans; |
411 | struct iio_map_internal *c = NULL; | |
412 | int nummaps = 0; | |
413 | int mapind = 0; | |
414 | int i, ret; | |
415 | ||
ca7d98db | 416 | if (dev == NULL) |
e27d75d7 | 417 | return ERR_PTR(-EINVAL); |
17d82b47 GR |
418 | |
419 | chans = of_iio_channel_get_all(dev); | |
420 | if (chans) | |
421 | return chans; | |
422 | ||
ca7d98db | 423 | name = dev_name(dev); |
e27d75d7 JC |
424 | |
425 | mutex_lock(&iio_map_list_lock); | |
426 | /* first count the matching maps */ | |
427 | list_for_each_entry(c, &iio_map_list, l) | |
428 | if (name && strcmp(name, c->map->consumer_dev_name) != 0) | |
429 | continue; | |
430 | else | |
431 | nummaps++; | |
432 | ||
433 | if (nummaps == 0) { | |
434 | ret = -ENODEV; | |
435 | goto error_ret; | |
436 | } | |
437 | ||
438 | /* NULL terminated array to save passing size */ | |
6396bb22 | 439 | chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL); |
e27d75d7 JC |
440 | if (chans == NULL) { |
441 | ret = -ENOMEM; | |
442 | goto error_ret; | |
443 | } | |
444 | ||
445 | /* for each map fill in the chans element */ | |
446 | list_for_each_entry(c, &iio_map_list, l) { | |
447 | if (name && strcmp(name, c->map->consumer_dev_name) != 0) | |
448 | continue; | |
449 | chans[mapind].indio_dev = c->indio_dev; | |
0464415d | 450 | chans[mapind].data = c->map->consumer_data; |
e27d75d7 JC |
451 | chans[mapind].channel = |
452 | iio_chan_spec_from_name(chans[mapind].indio_dev, | |
453 | c->map->adc_channel_label); | |
454 | if (chans[mapind].channel == NULL) { | |
455 | ret = -EINVAL; | |
e27d75d7 JC |
456 | goto error_free_chans; |
457 | } | |
1875ffd2 | 458 | iio_device_get(chans[mapind].indio_dev); |
e27d75d7 JC |
459 | mapind++; |
460 | } | |
e27d75d7 JC |
461 | if (mapind == 0) { |
462 | ret = -ENODEV; | |
463 | goto error_free_chans; | |
464 | } | |
e59b9afe DC |
465 | mutex_unlock(&iio_map_list_lock); |
466 | ||
e27d75d7 JC |
467 | return chans; |
468 | ||
469 | error_free_chans: | |
470 | for (i = 0; i < nummaps; i++) | |
1875ffd2 | 471 | iio_device_put(chans[i].indio_dev); |
e27d75d7 JC |
472 | kfree(chans); |
473 | error_ret: | |
474 | mutex_unlock(&iio_map_list_lock); | |
475 | ||
476 | return ERR_PTR(ret); | |
477 | } | |
314be14b | 478 | EXPORT_SYMBOL_GPL(iio_channel_get_all); |
e27d75d7 | 479 | |
314be14b | 480 | void iio_channel_release_all(struct iio_channel *channels) |
e27d75d7 JC |
481 | { |
482 | struct iio_channel *chan = &channels[0]; | |
483 | ||
484 | while (chan->indio_dev) { | |
1875ffd2 | 485 | iio_device_put(chan->indio_dev); |
e27d75d7 JC |
486 | chan++; |
487 | } | |
488 | kfree(channels); | |
489 | } | |
314be14b | 490 | EXPORT_SYMBOL_GPL(iio_channel_release_all); |
e27d75d7 | 491 | |
efc2c013 LD |
492 | static void devm_iio_channel_free_all(struct device *dev, void *res) |
493 | { | |
494 | struct iio_channel *channels = *(struct iio_channel **)res; | |
495 | ||
496 | iio_channel_release_all(channels); | |
497 | } | |
498 | ||
499 | struct iio_channel *devm_iio_channel_get_all(struct device *dev) | |
500 | { | |
501 | struct iio_channel **ptr, *channels; | |
502 | ||
503 | ptr = devres_alloc(devm_iio_channel_free_all, sizeof(*ptr), GFP_KERNEL); | |
504 | if (!ptr) | |
505 | return ERR_PTR(-ENOMEM); | |
506 | ||
507 | channels = iio_channel_get_all(dev); | |
508 | if (IS_ERR(channels)) { | |
509 | devres_free(ptr); | |
510 | return channels; | |
511 | } | |
512 | ||
513 | *ptr = channels; | |
514 | devres_add(dev, ptr); | |
515 | ||
516 | return channels; | |
517 | } | |
518 | EXPORT_SYMBOL_GPL(devm_iio_channel_get_all); | |
519 | ||
520 | void devm_iio_channel_release_all(struct device *dev, | |
521 | struct iio_channel *channels) | |
522 | { | |
523 | WARN_ON(devres_release(dev, devm_iio_channel_free_all, | |
524 | devm_iio_channel_match, channels)); | |
525 | } | |
526 | EXPORT_SYMBOL_GPL(devm_iio_channel_release_all); | |
527 | ||
48e44ce0 LPC |
528 | static int iio_channel_read(struct iio_channel *chan, int *val, int *val2, |
529 | enum iio_chan_info_enum info) | |
530 | { | |
531 | int unused; | |
9fbfb4b3 SP |
532 | int vals[INDIO_MAX_RAW_ELEMENTS]; |
533 | int ret; | |
534 | int val_len = 2; | |
48e44ce0 LPC |
535 | |
536 | if (val2 == NULL) | |
537 | val2 = &unused; | |
538 | ||
fc0b8170 | 539 | if (!iio_channel_has_info(chan->channel, info)) |
65de7654 FP |
540 | return -EINVAL; |
541 | ||
9fbfb4b3 SP |
542 | if (chan->indio_dev->info->read_raw_multi) { |
543 | ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev, | |
544 | chan->channel, INDIO_MAX_RAW_ELEMENTS, | |
545 | vals, &val_len, info); | |
546 | *val = vals[0]; | |
547 | *val2 = vals[1]; | |
548 | } else | |
549 | ret = chan->indio_dev->info->read_raw(chan->indio_dev, | |
550 | chan->channel, val, val2, info); | |
551 | ||
552 | return ret; | |
48e44ce0 LPC |
553 | } |
554 | ||
314be14b | 555 | int iio_read_channel_raw(struct iio_channel *chan, int *val) |
e27d75d7 | 556 | { |
48e44ce0 | 557 | int ret; |
e27d75d7 JC |
558 | |
559 | mutex_lock(&chan->indio_dev->info_exist_lock); | |
560 | if (chan->indio_dev->info == NULL) { | |
561 | ret = -ENODEV; | |
562 | goto err_unlock; | |
563 | } | |
564 | ||
48e44ce0 | 565 | ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW); |
e27d75d7 JC |
566 | err_unlock: |
567 | mutex_unlock(&chan->indio_dev->info_exist_lock); | |
568 | ||
569 | return ret; | |
570 | } | |
314be14b | 571 | EXPORT_SYMBOL_GPL(iio_read_channel_raw); |
e27d75d7 | 572 | |
476d4af2 SR |
573 | int iio_read_channel_average_raw(struct iio_channel *chan, int *val) |
574 | { | |
575 | int ret; | |
576 | ||
577 | mutex_lock(&chan->indio_dev->info_exist_lock); | |
578 | if (chan->indio_dev->info == NULL) { | |
579 | ret = -ENODEV; | |
580 | goto err_unlock; | |
581 | } | |
582 | ||
583 | ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW); | |
584 | err_unlock: | |
585 | mutex_unlock(&chan->indio_dev->info_exist_lock); | |
586 | ||
587 | return ret; | |
588 | } | |
589 | EXPORT_SYMBOL_GPL(iio_read_channel_average_raw); | |
590 | ||
48e44ce0 LPC |
591 | static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan, |
592 | int raw, int *processed, unsigned int scale) | |
593 | { | |
594 | int scale_type, scale_val, scale_val2, offset; | |
595 | s64 raw64 = raw; | |
596 | int ret; | |
597 | ||
6c5d4c96 | 598 | ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_OFFSET); |
f91d1b63 | 599 | if (ret >= 0) |
48e44ce0 LPC |
600 | raw64 += offset; |
601 | ||
602 | scale_type = iio_channel_read(chan, &scale_val, &scale_val2, | |
603 | IIO_CHAN_INFO_SCALE); | |
adc8ec5f LW |
604 | if (scale_type < 0) { |
605 | /* | |
606 | * Just pass raw values as processed if no scaling is | |
607 | * available. | |
608 | */ | |
609 | *processed = raw; | |
610 | return 0; | |
611 | } | |
48e44ce0 LPC |
612 | |
613 | switch (scale_type) { | |
614 | case IIO_VAL_INT: | |
615 | *processed = raw64 * scale_val; | |
616 | break; | |
617 | case IIO_VAL_INT_PLUS_MICRO: | |
618 | if (scale_val2 < 0) | |
619 | *processed = -raw64 * scale_val; | |
620 | else | |
621 | *processed = raw64 * scale_val; | |
622 | *processed += div_s64(raw64 * (s64)scale_val2 * scale, | |
623 | 1000000LL); | |
624 | break; | |
625 | case IIO_VAL_INT_PLUS_NANO: | |
626 | if (scale_val2 < 0) | |
627 | *processed = -raw64 * scale_val; | |
628 | else | |
629 | *processed = raw64 * scale_val; | |
630 | *processed += div_s64(raw64 * (s64)scale_val2 * scale, | |
631 | 1000000000LL); | |
632 | break; | |
633 | case IIO_VAL_FRACTIONAL: | |
634 | *processed = div_s64(raw64 * (s64)scale_val * scale, | |
635 | scale_val2); | |
636 | break; | |
103d9fb9 LPC |
637 | case IIO_VAL_FRACTIONAL_LOG2: |
638 | *processed = (raw64 * (s64)scale_val * scale) >> scale_val2; | |
639 | break; | |
48e44ce0 LPC |
640 | default: |
641 | return -EINVAL; | |
642 | } | |
643 | ||
644 | return 0; | |
645 | } | |
646 | ||
647 | int iio_convert_raw_to_processed(struct iio_channel *chan, int raw, | |
648 | int *processed, unsigned int scale) | |
649 | { | |
650 | int ret; | |
651 | ||
652 | mutex_lock(&chan->indio_dev->info_exist_lock); | |
653 | if (chan->indio_dev->info == NULL) { | |
654 | ret = -ENODEV; | |
655 | goto err_unlock; | |
656 | } | |
657 | ||
658 | ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed, | |
659 | scale); | |
660 | err_unlock: | |
661 | mutex_unlock(&chan->indio_dev->info_exist_lock); | |
662 | ||
663 | return ret; | |
664 | } | |
665 | EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed); | |
666 | ||
34739a21 AP |
667 | int iio_read_channel_attribute(struct iio_channel *chan, int *val, int *val2, |
668 | enum iio_chan_info_enum attribute) | |
0023e67d MR |
669 | { |
670 | int ret; | |
671 | ||
672 | mutex_lock(&chan->indio_dev->info_exist_lock); | |
673 | if (chan->indio_dev->info == NULL) { | |
674 | ret = -ENODEV; | |
675 | goto err_unlock; | |
676 | } | |
677 | ||
678 | ret = iio_channel_read(chan, val, val2, attribute); | |
679 | err_unlock: | |
680 | mutex_unlock(&chan->indio_dev->info_exist_lock); | |
681 | ||
682 | return ret; | |
683 | } | |
34739a21 | 684 | EXPORT_SYMBOL_GPL(iio_read_channel_attribute); |
0023e67d MR |
685 | |
686 | int iio_read_channel_offset(struct iio_channel *chan, int *val, int *val2) | |
687 | { | |
688 | return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_OFFSET); | |
689 | } | |
690 | EXPORT_SYMBOL_GPL(iio_read_channel_offset); | |
691 | ||
48e44ce0 LPC |
692 | int iio_read_channel_processed(struct iio_channel *chan, int *val) |
693 | { | |
694 | int ret; | |
695 | ||
696 | mutex_lock(&chan->indio_dev->info_exist_lock); | |
697 | if (chan->indio_dev->info == NULL) { | |
698 | ret = -ENODEV; | |
699 | goto err_unlock; | |
700 | } | |
701 | ||
702 | if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) { | |
703 | ret = iio_channel_read(chan, val, NULL, | |
704 | IIO_CHAN_INFO_PROCESSED); | |
705 | } else { | |
706 | ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW); | |
707 | if (ret < 0) | |
708 | goto err_unlock; | |
709 | ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1); | |
710 | } | |
711 | ||
712 | err_unlock: | |
713 | mutex_unlock(&chan->indio_dev->info_exist_lock); | |
714 | ||
715 | return ret; | |
716 | } | |
717 | EXPORT_SYMBOL_GPL(iio_read_channel_processed); | |
718 | ||
314be14b | 719 | int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2) |
e27d75d7 | 720 | { |
0023e67d | 721 | return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_SCALE); |
e27d75d7 | 722 | } |
314be14b | 723 | EXPORT_SYMBOL_GPL(iio_read_channel_scale); |
00c5f80c PR |
724 | |
725 | static int iio_channel_read_avail(struct iio_channel *chan, | |
726 | const int **vals, int *type, int *length, | |
727 | enum iio_chan_info_enum info) | |
728 | { | |
729 | if (!iio_channel_has_available(chan->channel, info)) | |
730 | return -EINVAL; | |
731 | ||
732 | return chan->indio_dev->info->read_avail(chan->indio_dev, chan->channel, | |
733 | vals, type, length, info); | |
734 | } | |
735 | ||
736 | int iio_read_avail_channel_raw(struct iio_channel *chan, | |
737 | const int **vals, int *length) | |
738 | { | |
739 | int ret; | |
740 | int type; | |
741 | ||
742 | mutex_lock(&chan->indio_dev->info_exist_lock); | |
743 | if (!chan->indio_dev->info) { | |
744 | ret = -ENODEV; | |
745 | goto err_unlock; | |
746 | } | |
747 | ||
748 | ret = iio_channel_read_avail(chan, | |
749 | vals, &type, length, IIO_CHAN_INFO_RAW); | |
750 | err_unlock: | |
751 | mutex_unlock(&chan->indio_dev->info_exist_lock); | |
752 | ||
c773f700 | 753 | if (ret >= 0 && type != IIO_VAL_INT) |
00c5f80c PR |
754 | /* raw values are assumed to be IIO_VAL_INT */ |
755 | ret = -EINVAL; | |
00c5f80c PR |
756 | |
757 | return ret; | |
758 | } | |
759 | EXPORT_SYMBOL_GPL(iio_read_avail_channel_raw); | |
760 | ||
761 | static int iio_channel_read_max(struct iio_channel *chan, | |
762 | int *val, int *val2, int *type, | |
763 | enum iio_chan_info_enum info) | |
764 | { | |
765 | int unused; | |
766 | const int *vals; | |
767 | int length; | |
768 | int ret; | |
769 | ||
770 | if (!val2) | |
771 | val2 = &unused; | |
772 | ||
773 | ret = iio_channel_read_avail(chan, &vals, type, &length, info); | |
774 | switch (ret) { | |
775 | case IIO_AVAIL_RANGE: | |
776 | switch (*type) { | |
777 | case IIO_VAL_INT: | |
778 | *val = vals[2]; | |
779 | break; | |
780 | default: | |
781 | *val = vals[4]; | |
782 | *val2 = vals[5]; | |
783 | } | |
784 | return 0; | |
785 | ||
786 | case IIO_AVAIL_LIST: | |
787 | if (length <= 0) | |
788 | return -EINVAL; | |
789 | switch (*type) { | |
790 | case IIO_VAL_INT: | |
791 | *val = vals[--length]; | |
792 | while (length) { | |
793 | if (vals[--length] > *val) | |
794 | *val = vals[length]; | |
795 | } | |
796 | break; | |
797 | default: | |
798 | /* FIXME: learn about max for other iio values */ | |
799 | return -EINVAL; | |
800 | } | |
801 | return 0; | |
802 | ||
803 | default: | |
804 | return ret; | |
805 | } | |
806 | } | |
807 | ||
808 | int iio_read_max_channel_raw(struct iio_channel *chan, int *val) | |
809 | { | |
810 | int ret; | |
811 | int type; | |
812 | ||
813 | mutex_lock(&chan->indio_dev->info_exist_lock); | |
814 | if (!chan->indio_dev->info) { | |
815 | ret = -ENODEV; | |
816 | goto err_unlock; | |
817 | } | |
818 | ||
819 | ret = iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW); | |
820 | err_unlock: | |
821 | mutex_unlock(&chan->indio_dev->info_exist_lock); | |
822 | ||
823 | return ret; | |
824 | } | |
825 | EXPORT_SYMBOL_GPL(iio_read_max_channel_raw); | |
e27d75d7 | 826 | |
314be14b | 827 | int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type) |
e27d75d7 JC |
828 | { |
829 | int ret = 0; | |
830 | /* Need to verify underlying driver has not gone away */ | |
831 | ||
832 | mutex_lock(&chan->indio_dev->info_exist_lock); | |
833 | if (chan->indio_dev->info == NULL) { | |
834 | ret = -ENODEV; | |
835 | goto err_unlock; | |
836 | } | |
837 | ||
838 | *type = chan->channel->type; | |
839 | err_unlock: | |
840 | mutex_unlock(&chan->indio_dev->info_exist_lock); | |
841 | ||
842 | return ret; | |
843 | } | |
314be14b | 844 | EXPORT_SYMBOL_GPL(iio_get_channel_type); |
f9380e71 DB |
845 | |
846 | static int iio_channel_write(struct iio_channel *chan, int val, int val2, | |
847 | enum iio_chan_info_enum info) | |
848 | { | |
849 | return chan->indio_dev->info->write_raw(chan->indio_dev, | |
850 | chan->channel, val, val2, info); | |
851 | } | |
852 | ||
34739a21 AP |
853 | int iio_write_channel_attribute(struct iio_channel *chan, int val, int val2, |
854 | enum iio_chan_info_enum attribute) | |
f9380e71 DB |
855 | { |
856 | int ret; | |
857 | ||
858 | mutex_lock(&chan->indio_dev->info_exist_lock); | |
859 | if (chan->indio_dev->info == NULL) { | |
860 | ret = -ENODEV; | |
861 | goto err_unlock; | |
862 | } | |
863 | ||
34739a21 | 864 | ret = iio_channel_write(chan, val, val2, attribute); |
f9380e71 DB |
865 | err_unlock: |
866 | mutex_unlock(&chan->indio_dev->info_exist_lock); | |
867 | ||
868 | return ret; | |
869 | } | |
34739a21 AP |
870 | EXPORT_SYMBOL_GPL(iio_write_channel_attribute); |
871 | ||
872 | int iio_write_channel_raw(struct iio_channel *chan, int val) | |
873 | { | |
874 | return iio_write_channel_attribute(chan, val, 0, IIO_CHAN_INFO_RAW); | |
875 | } | |
f9380e71 | 876 | EXPORT_SYMBOL_GPL(iio_write_channel_raw); |
8a848e75 PR |
877 | |
878 | unsigned int iio_get_channel_ext_info_count(struct iio_channel *chan) | |
879 | { | |
880 | const struct iio_chan_spec_ext_info *ext_info; | |
881 | unsigned int i = 0; | |
882 | ||
883 | if (!chan->channel->ext_info) | |
884 | return i; | |
885 | ||
886 | for (ext_info = chan->channel->ext_info; ext_info->name; ext_info++) | |
887 | ++i; | |
888 | ||
889 | return i; | |
890 | } | |
891 | EXPORT_SYMBOL_GPL(iio_get_channel_ext_info_count); | |
892 | ||
893 | static const struct iio_chan_spec_ext_info *iio_lookup_ext_info( | |
894 | const struct iio_channel *chan, | |
895 | const char *attr) | |
896 | { | |
897 | const struct iio_chan_spec_ext_info *ext_info; | |
898 | ||
899 | if (!chan->channel->ext_info) | |
900 | return NULL; | |
901 | ||
902 | for (ext_info = chan->channel->ext_info; ext_info->name; ++ext_info) { | |
903 | if (!strcmp(attr, ext_info->name)) | |
904 | return ext_info; | |
905 | } | |
906 | ||
907 | return NULL; | |
908 | } | |
909 | ||
910 | ssize_t iio_read_channel_ext_info(struct iio_channel *chan, | |
911 | const char *attr, char *buf) | |
912 | { | |
913 | const struct iio_chan_spec_ext_info *ext_info; | |
914 | ||
915 | ext_info = iio_lookup_ext_info(chan, attr); | |
916 | if (!ext_info) | |
917 | return -EINVAL; | |
918 | ||
919 | return ext_info->read(chan->indio_dev, ext_info->private, | |
920 | chan->channel, buf); | |
921 | } | |
922 | EXPORT_SYMBOL_GPL(iio_read_channel_ext_info); | |
923 | ||
924 | ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr, | |
925 | const char *buf, size_t len) | |
926 | { | |
927 | const struct iio_chan_spec_ext_info *ext_info; | |
928 | ||
929 | ext_info = iio_lookup_ext_info(chan, attr); | |
930 | if (!ext_info) | |
931 | return -EINVAL; | |
932 | ||
933 | return ext_info->write(chan->indio_dev, ext_info->private, | |
934 | chan->channel, buf, len); | |
935 | } | |
936 | EXPORT_SYMBOL_GPL(iio_write_channel_ext_info); |