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