blob: 0222885b334c1395f8e5f70347a795462f0785fb [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Jonathan Camerone27d75d2012-02-15 19:48:01 +00002/* The industrial I/O core in kernel channel mapping
3 *
4 * Copyright (c) 2011 Jonathan Cameron
Jonathan Camerone27d75d2012-02-15 19:48:01 +00005 */
6#include <linux/err.h>
7#include <linux/export.h>
8#include <linux/slab.h>
9#include <linux/mutex.h>
Guenter Roeck17d82b42013-02-07 17:09:00 +000010#include <linux/of.h>
Jonathan Camerone27d75d2012-02-15 19:48:01 +000011
Jonathan Cameron06458e22012-04-25 15:54:58 +010012#include <linux/iio/iio.h>
Jonathan Cameronb804e2b2021-04-26 18:49:08 +010013#include <linux/iio/iio-opaque.h>
Jonathan Camerone27d75d2012-02-15 19:48:01 +000014#include "iio_core.h"
Jonathan Cameron06458e22012-04-25 15:54:58 +010015#include <linux/iio/machine.h>
16#include <linux/iio/driver.h>
17#include <linux/iio/consumer.h>
Jonathan Camerone27d75d2012-02-15 19:48:01 +000018
19struct iio_map_internal {
20 struct iio_dev *indio_dev;
21 struct iio_map *map;
22 struct list_head l;
23};
24
25static LIST_HEAD(iio_map_list);
26static DEFINE_MUTEX(iio_map_list_lock);
27
Lino Sanfilippocc9fb602020-11-28 14:44:18 +010028static int iio_map_array_unregister_locked(struct iio_dev *indio_dev)
29{
30 int ret = -ENODEV;
31 struct iio_map_internal *mapi, *next;
32
33 list_for_each_entry_safe(mapi, next, &iio_map_list, l) {
34 if (indio_dev == mapi->indio_dev) {
35 list_del(&mapi->l);
36 kfree(mapi);
37 ret = 0;
38 }
39 }
40 return ret;
41}
42
Jonathan Camerone27d75d2012-02-15 19:48:01 +000043int iio_map_array_register(struct iio_dev *indio_dev, struct iio_map *maps)
44{
45 int i = 0, ret = 0;
46 struct iio_map_internal *mapi;
47
48 if (maps == NULL)
49 return 0;
50
51 mutex_lock(&iio_map_list_lock);
52 while (maps[i].consumer_dev_name != NULL) {
53 mapi = kzalloc(sizeof(*mapi), GFP_KERNEL);
54 if (mapi == NULL) {
55 ret = -ENOMEM;
56 goto error_ret;
57 }
58 mapi->map = &maps[i];
59 mapi->indio_dev = indio_dev;
Gaurav Guptabc4b2a52017-06-27 09:46:01 -070060 list_add_tail(&mapi->l, &iio_map_list);
Jonathan Camerone27d75d2012-02-15 19:48:01 +000061 i++;
62 }
63error_ret:
Lino Sanfilippo34fce6c2020-11-28 14:44:19 +010064 if (ret)
65 iio_map_array_unregister_locked(indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +000066 mutex_unlock(&iio_map_list_lock);
67
68 return ret;
69}
70EXPORT_SYMBOL_GPL(iio_map_array_register);
71
72
Guenter Roeck6cb2afd2013-01-31 21:43:00 +000073/*
74 * Remove all map entries associated with the given iio device
Jonathan Camerone27d75d2012-02-15 19:48:01 +000075 */
Guenter Roeck6cb2afd2013-01-31 21:43:00 +000076int iio_map_array_unregister(struct iio_dev *indio_dev)
Jonathan Camerone27d75d2012-02-15 19:48:01 +000077{
Lino Sanfilippocc9fb602020-11-28 14:44:18 +010078 int ret;
Jonathan Camerone27d75d2012-02-15 19:48:01 +000079
80 mutex_lock(&iio_map_list_lock);
Lino Sanfilippocc9fb602020-11-28 14:44:18 +010081 ret = iio_map_array_unregister_locked(indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +000082 mutex_unlock(&iio_map_list_lock);
Lino Sanfilippocc9fb602020-11-28 14:44:18 +010083
Jonathan Camerone27d75d2012-02-15 19:48:01 +000084 return ret;
85}
86EXPORT_SYMBOL_GPL(iio_map_array_unregister);
87
Alexandru Ardelean25c02ed2021-09-03 10:29:13 +030088static void iio_map_array_unregister_cb(void *indio_dev)
89{
90 iio_map_array_unregister(indio_dev);
91}
92
93int devm_iio_map_array_register(struct device *dev, struct iio_dev *indio_dev, struct iio_map *maps)
94{
95 int ret;
96
97 ret = iio_map_array_register(indio_dev, maps);
98 if (ret)
99 return ret;
100
101 return devm_add_action_or_reset(dev, iio_map_array_unregister_cb, indio_dev);
102}
103EXPORT_SYMBOL_GPL(devm_iio_map_array_register);
104
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000105static const struct iio_chan_spec
Jonathan Cameron314be142012-05-01 21:04:24 +0100106*iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000107{
108 int i;
109 const struct iio_chan_spec *chan = NULL;
110
111 for (i = 0; i < indio_dev->num_channels; i++)
112 if (indio_dev->channels[i].datasheet_name &&
113 strcmp(name, indio_dev->channels[i].datasheet_name) == 0) {
114 chan = &indio_dev->channels[i];
115 break;
116 }
117 return chan;
118}
119
Guenter Roeck17d82b42013-02-07 17:09:00 +0000120#ifdef CONFIG_OF
121
Suzuki K Poulose418e3ea2019-06-14 18:53:59 +0100122static int iio_dev_node_match(struct device *dev, const void *data)
Guenter Roeck17d82b42013-02-07 17:09:00 +0000123{
124 return dev->of_node == data && dev->type == &iio_device_type;
125}
126
Ivan T. Ivanovacd82562014-10-22 18:29:43 +0300127/**
128 * __of_iio_simple_xlate - translate iiospec to the IIO channel index
129 * @indio_dev: pointer to the iio_dev structure
130 * @iiospec: IIO specifier as found in the device tree
131 *
132 * This is simple translation function, suitable for the most 1:1 mapped
133 * channels in IIO chips. This function performs only one sanity check:
134 * whether IIO index is less than num_channels (that is specified in the
135 * iio_dev).
136 */
137static int __of_iio_simple_xlate(struct iio_dev *indio_dev,
138 const struct of_phandle_args *iiospec)
139{
140 if (!iiospec->args_count)
141 return 0;
142
Stefan Wahren1f202722015-01-01 18:13:24 +0000143 if (iiospec->args[0] >= indio_dev->num_channels) {
144 dev_err(&indio_dev->dev, "invalid channel index %u\n",
145 iiospec->args[0]);
Ivan T. Ivanovacd82562014-10-22 18:29:43 +0300146 return -EINVAL;
Stefan Wahren1f202722015-01-01 18:13:24 +0000147 }
Ivan T. Ivanovacd82562014-10-22 18:29:43 +0300148
149 return iiospec->args[0];
150}
151
Guenter Roeck17d82b42013-02-07 17:09:00 +0000152static int __of_iio_channel_get(struct iio_channel *channel,
153 struct device_node *np, int index)
154{
155 struct device *idev;
156 struct iio_dev *indio_dev;
157 int err;
158 struct of_phandle_args iiospec;
159
160 err = of_parse_phandle_with_args(np, "io-channels",
161 "#io-channel-cells",
162 index, &iiospec);
163 if (err)
164 return err;
165
166 idev = bus_find_device(&iio_bus_type, NULL, iiospec.np,
167 iio_dev_node_match);
168 of_node_put(iiospec.np);
169 if (idev == NULL)
170 return -EPROBE_DEFER;
171
172 indio_dev = dev_to_iio_dev(idev);
173 channel->indio_dev = indio_dev;
Ivan T. Ivanovacd82562014-10-22 18:29:43 +0300174 if (indio_dev->info->of_xlate)
175 index = indio_dev->info->of_xlate(indio_dev, &iiospec);
176 else
177 index = __of_iio_simple_xlate(indio_dev, &iiospec);
178 if (index < 0)
Guenter Roeck17d82b42013-02-07 17:09:00 +0000179 goto err_put;
Guenter Roeck17d82b42013-02-07 17:09:00 +0000180 channel->channel = &indio_dev->channels[index];
181
182 return 0;
183
184err_put:
185 iio_device_put(indio_dev);
Ivan T. Ivanovacd82562014-10-22 18:29:43 +0300186 return index;
Guenter Roeck17d82b42013-02-07 17:09:00 +0000187}
188
189static struct iio_channel *of_iio_channel_get(struct device_node *np, int index)
190{
191 struct iio_channel *channel;
192 int err;
193
194 if (index < 0)
195 return ERR_PTR(-EINVAL);
196
197 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
198 if (channel == NULL)
199 return ERR_PTR(-ENOMEM);
200
201 err = __of_iio_channel_get(channel, np, index);
202 if (err)
203 goto err_free_channel;
204
205 return channel;
206
207err_free_channel:
208 kfree(channel);
209 return ERR_PTR(err);
210}
211
Dmitry Baryshkov6e39b142020-12-04 05:55:01 +0300212struct iio_channel *of_iio_channel_get_by_name(struct device_node *np,
213 const char *name)
Guenter Roeck17d82b42013-02-07 17:09:00 +0000214{
215 struct iio_channel *chan = NULL;
216
217 /* Walk up the tree of devices looking for a matching iio channel */
218 while (np) {
219 int index = 0;
220
221 /*
222 * For named iio channels, first look up the name in the
223 * "io-channel-names" property. If it cannot be found, the
224 * index will be an error code, and of_iio_channel_get()
225 * will fail.
226 */
227 if (name)
228 index = of_property_match_string(np, "io-channel-names",
229 name);
230 chan = of_iio_channel_get(np, index);
Johannes Pointner872687f2014-08-25 09:04:00 +0100231 if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
Guenter Roeck17d82b42013-02-07 17:09:00 +0000232 break;
233 else if (name && index >= 0) {
Rob Herring3921db42017-07-18 16:43:08 -0500234 pr_err("ERROR: could not get IIO channel %pOF:%s(%i)\n",
235 np, name ? name : "", index);
Adam Thomsona2c12492014-11-06 12:11:00 +0000236 return NULL;
Guenter Roeck17d82b42013-02-07 17:09:00 +0000237 }
238
239 /*
240 * No matching IIO channel found on this node.
241 * If the parent node has a "io-channel-ranges" property,
242 * then we can try one of its channels.
243 */
244 np = np->parent;
245 if (np && !of_get_property(np, "io-channel-ranges", NULL))
Adam Thomsona2c12492014-11-06 12:11:00 +0000246 return NULL;
Guenter Roeck17d82b42013-02-07 17:09:00 +0000247 }
Adam Thomsona2c12492014-11-06 12:11:00 +0000248
Guenter Roeck17d82b42013-02-07 17:09:00 +0000249 return chan;
250}
Dmitry Baryshkov6e39b142020-12-04 05:55:01 +0300251EXPORT_SYMBOL_GPL(of_iio_channel_get_by_name);
Guenter Roeck17d82b42013-02-07 17:09:00 +0000252
253static struct iio_channel *of_iio_channel_get_all(struct device *dev)
254{
255 struct iio_channel *chans;
256 int i, mapind, nummaps = 0;
257 int ret;
258
259 do {
260 ret = of_parse_phandle_with_args(dev->of_node,
261 "io-channels",
262 "#io-channel-cells",
263 nummaps, NULL);
264 if (ret < 0)
265 break;
266 } while (++nummaps);
267
268 if (nummaps == 0) /* no error, return NULL to search map table */
269 return NULL;
270
271 /* NULL terminated array to save passing size */
272 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
273 if (chans == NULL)
274 return ERR_PTR(-ENOMEM);
275
276 /* Search for OF matches */
277 for (mapind = 0; mapind < nummaps; mapind++) {
278 ret = __of_iio_channel_get(&chans[mapind], dev->of_node,
279 mapind);
280 if (ret)
281 goto error_free_chans;
282 }
283 return chans;
284
285error_free_chans:
286 for (i = 0; i < mapind; i++)
287 iio_device_put(chans[i].indio_dev);
288 kfree(chans);
289 return ERR_PTR(ret);
290}
291
292#else /* CONFIG_OF */
293
Guenter Roeck17d82b42013-02-07 17:09:00 +0000294static inline struct iio_channel *of_iio_channel_get_all(struct device *dev)
295{
296 return NULL;
297}
298
299#endif /* CONFIG_OF */
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000300
Guenter Roeck5aa57f02013-02-04 20:26:00 +0000301static struct iio_channel *iio_channel_get_sys(const char *name,
302 const char *channel_name)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000303{
304 struct iio_map_internal *c_i = NULL, *c = NULL;
305 struct iio_channel *channel;
Kim, Milo3183bac2012-09-18 05:56:00 +0100306 int err;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000307
308 if (name == NULL && channel_name == NULL)
309 return ERR_PTR(-ENODEV);
310
311 /* first find matching entry the channel map */
312 mutex_lock(&iio_map_list_lock);
313 list_for_each_entry(c_i, &iio_map_list, l) {
314 if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
315 (channel_name &&
316 strcmp(channel_name, c_i->map->consumer_channel) != 0))
317 continue;
318 c = c_i;
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200319 iio_device_get(c->indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000320 break;
321 }
322 mutex_unlock(&iio_map_list_lock);
323 if (c == NULL)
324 return ERR_PTR(-ENODEV);
325
Kim, Milo2cc412b2012-09-14 02:24:00 +0100326 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
Kim, Milo3183bac2012-09-18 05:56:00 +0100327 if (channel == NULL) {
328 err = -ENOMEM;
Kim, Milo801c4b52012-09-18 05:55:00 +0100329 goto error_no_mem;
Kim, Milo3183bac2012-09-18 05:56:00 +0100330 }
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000331
332 channel->indio_dev = c->indio_dev;
333
Kim, Milob2b79ff2012-09-17 09:44:00 +0100334 if (c->map->adc_channel_label) {
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000335 channel->channel =
336 iio_chan_spec_from_name(channel->indio_dev,
337 c->map->adc_channel_label);
338
Kim, Milo3183bac2012-09-18 05:56:00 +0100339 if (channel->channel == NULL) {
340 err = -EINVAL;
Kim, Milob2b79ff2012-09-17 09:44:00 +0100341 goto error_no_chan;
Kim, Milo3183bac2012-09-18 05:56:00 +0100342 }
Kim, Milob2b79ff2012-09-17 09:44:00 +0100343 }
344
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000345 return channel;
Kim, Milob2b79ff2012-09-17 09:44:00 +0100346
347error_no_chan:
Kim, Milob2b79ff2012-09-17 09:44:00 +0100348 kfree(channel);
Kim, Milo801c4b52012-09-18 05:55:00 +0100349error_no_mem:
350 iio_device_put(c->indio_dev);
Kim, Milo3183bac2012-09-18 05:56:00 +0100351 return ERR_PTR(err);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000352}
Guenter Roeck5aa57f02013-02-04 20:26:00 +0000353
354struct iio_channel *iio_channel_get(struct device *dev,
355 const char *channel_name)
356{
357 const char *name = dev ? dev_name(dev) : NULL;
Guenter Roeck17d82b42013-02-07 17:09:00 +0000358 struct iio_channel *channel;
Guenter Roeck5aa57f02013-02-04 20:26:00 +0000359
Guenter Roeck17d82b42013-02-07 17:09:00 +0000360 if (dev) {
361 channel = of_iio_channel_get_by_name(dev->of_node,
362 channel_name);
363 if (channel != NULL)
364 return channel;
365 }
Adam Thomsona2c12492014-11-06 12:11:00 +0000366
Guenter Roeck5aa57f02013-02-04 20:26:00 +0000367 return iio_channel_get_sys(name, channel_name);
368}
Jonathan Cameron314be142012-05-01 21:04:24 +0100369EXPORT_SYMBOL_GPL(iio_channel_get);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000370
Jonathan Cameron314be142012-05-01 21:04:24 +0100371void iio_channel_release(struct iio_channel *channel)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000372{
Dan Carpenterd81dac32016-01-26 12:25:21 +0300373 if (!channel)
374 return;
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200375 iio_device_put(channel->indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000376 kfree(channel);
377}
Jonathan Cameron314be142012-05-01 21:04:24 +0100378EXPORT_SYMBOL_GPL(iio_channel_release);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000379
Yicong Yang7349e8a2021-04-08 19:38:16 +0800380static void devm_iio_channel_free(void *iio_channel)
Laxman Dewangan8bf872d2016-04-06 16:01:06 +0530381{
Yicong Yang7349e8a2021-04-08 19:38:16 +0800382 iio_channel_release(iio_channel);
Laxman Dewangan8bf872d2016-04-06 16:01:06 +0530383}
384
Laxman Dewangan8bf872d2016-04-06 16:01:06 +0530385struct iio_channel *devm_iio_channel_get(struct device *dev,
386 const char *channel_name)
387{
Yicong Yang7349e8a2021-04-08 19:38:16 +0800388 struct iio_channel *channel;
389 int ret;
Laxman Dewangan8bf872d2016-04-06 16:01:06 +0530390
391 channel = iio_channel_get(dev, channel_name);
Yicong Yang7349e8a2021-04-08 19:38:16 +0800392 if (IS_ERR(channel))
Laxman Dewangan8bf872d2016-04-06 16:01:06 +0530393 return channel;
Laxman Dewangan8bf872d2016-04-06 16:01:06 +0530394
Yicong Yang7349e8a2021-04-08 19:38:16 +0800395 ret = devm_add_action_or_reset(dev, devm_iio_channel_free, channel);
396 if (ret)
397 return ERR_PTR(ret);
Laxman Dewangan8bf872d2016-04-06 16:01:06 +0530398
399 return channel;
400}
401EXPORT_SYMBOL_GPL(devm_iio_channel_get);
402
Dmitry Baryshkov6e39b142020-12-04 05:55:01 +0300403struct iio_channel *devm_of_iio_channel_get_by_name(struct device *dev,
404 struct device_node *np,
405 const char *channel_name)
406{
Yicong Yang7349e8a2021-04-08 19:38:16 +0800407 struct iio_channel *channel;
408 int ret;
Dmitry Baryshkov6e39b142020-12-04 05:55:01 +0300409
410 channel = of_iio_channel_get_by_name(np, channel_name);
Yicong Yang7349e8a2021-04-08 19:38:16 +0800411 if (IS_ERR(channel))
Dmitry Baryshkov6e39b142020-12-04 05:55:01 +0300412 return channel;
Dmitry Baryshkov6e39b142020-12-04 05:55:01 +0300413
Yicong Yang7349e8a2021-04-08 19:38:16 +0800414 ret = devm_add_action_or_reset(dev, devm_iio_channel_free, channel);
415 if (ret)
416 return ERR_PTR(ret);
Dmitry Baryshkov6e39b142020-12-04 05:55:01 +0300417
418 return channel;
419}
420EXPORT_SYMBOL_GPL(devm_of_iio_channel_get_by_name);
421
Guenter Roeckca7d98d2013-01-31 21:43:00 +0000422struct iio_channel *iio_channel_get_all(struct device *dev)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000423{
Guenter Roeckca7d98d2013-01-31 21:43:00 +0000424 const char *name;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000425 struct iio_channel *chans;
426 struct iio_map_internal *c = NULL;
427 int nummaps = 0;
428 int mapind = 0;
429 int i, ret;
430
Guenter Roeckca7d98d2013-01-31 21:43:00 +0000431 if (dev == NULL)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000432 return ERR_PTR(-EINVAL);
Guenter Roeck17d82b42013-02-07 17:09:00 +0000433
434 chans = of_iio_channel_get_all(dev);
435 if (chans)
436 return chans;
437
Guenter Roeckca7d98d2013-01-31 21:43:00 +0000438 name = dev_name(dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000439
440 mutex_lock(&iio_map_list_lock);
441 /* first count the matching maps */
442 list_for_each_entry(c, &iio_map_list, l)
443 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
444 continue;
445 else
446 nummaps++;
447
448 if (nummaps == 0) {
449 ret = -ENODEV;
450 goto error_ret;
451 }
452
453 /* NULL terminated array to save passing size */
Kees Cook6396bb22018-06-12 14:03:40 -0700454 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000455 if (chans == NULL) {
456 ret = -ENOMEM;
457 goto error_ret;
458 }
459
460 /* for each map fill in the chans element */
461 list_for_each_entry(c, &iio_map_list, l) {
462 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
463 continue;
464 chans[mapind].indio_dev = c->indio_dev;
Jonathan Cameron04644152012-06-30 20:06:00 +0100465 chans[mapind].data = c->map->consumer_data;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000466 chans[mapind].channel =
467 iio_chan_spec_from_name(chans[mapind].indio_dev,
468 c->map->adc_channel_label);
469 if (chans[mapind].channel == NULL) {
470 ret = -EINVAL;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000471 goto error_free_chans;
472 }
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200473 iio_device_get(chans[mapind].indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000474 mapind++;
475 }
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000476 if (mapind == 0) {
477 ret = -ENODEV;
478 goto error_free_chans;
479 }
Dan Carpentere59b9af2012-07-11 07:34:00 +0100480 mutex_unlock(&iio_map_list_lock);
481
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000482 return chans;
483
484error_free_chans:
485 for (i = 0; i < nummaps; i++)
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200486 iio_device_put(chans[i].indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000487 kfree(chans);
488error_ret:
489 mutex_unlock(&iio_map_list_lock);
490
491 return ERR_PTR(ret);
492}
Jonathan Cameron314be142012-05-01 21:04:24 +0100493EXPORT_SYMBOL_GPL(iio_channel_get_all);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000494
Jonathan Cameron314be142012-05-01 21:04:24 +0100495void iio_channel_release_all(struct iio_channel *channels)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000496{
497 struct iio_channel *chan = &channels[0];
498
499 while (chan->indio_dev) {
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200500 iio_device_put(chan->indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000501 chan++;
502 }
503 kfree(channels);
504}
Jonathan Cameron314be142012-05-01 21:04:24 +0100505EXPORT_SYMBOL_GPL(iio_channel_release_all);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000506
Yicong Yang7349e8a2021-04-08 19:38:16 +0800507static void devm_iio_channel_free_all(void *iio_channels)
Laxman Dewanganefc2c012016-04-06 16:01:07 +0530508{
Yicong Yang7349e8a2021-04-08 19:38:16 +0800509 iio_channel_release_all(iio_channels);
Laxman Dewanganefc2c012016-04-06 16:01:07 +0530510}
511
512struct iio_channel *devm_iio_channel_get_all(struct device *dev)
513{
Yicong Yang7349e8a2021-04-08 19:38:16 +0800514 struct iio_channel *channels;
515 int ret;
Laxman Dewanganefc2c012016-04-06 16:01:07 +0530516
517 channels = iio_channel_get_all(dev);
Yicong Yang7349e8a2021-04-08 19:38:16 +0800518 if (IS_ERR(channels))
Laxman Dewanganefc2c012016-04-06 16:01:07 +0530519 return channels;
Laxman Dewanganefc2c012016-04-06 16:01:07 +0530520
Yicong Yang7349e8a2021-04-08 19:38:16 +0800521 ret = devm_add_action_or_reset(dev, devm_iio_channel_free_all,
522 channels);
523 if (ret)
524 return ERR_PTR(ret);
Laxman Dewanganefc2c012016-04-06 16:01:07 +0530525
526 return channels;
527}
528EXPORT_SYMBOL_GPL(devm_iio_channel_get_all);
529
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100530static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
531 enum iio_chan_info_enum info)
532{
533 int unused;
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100534 int vals[INDIO_MAX_RAW_ELEMENTS];
535 int ret;
536 int val_len = 2;
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100537
538 if (val2 == NULL)
539 val2 = &unused;
540
Slawomir Stepienfc0b8172016-04-14 21:36:34 +0200541 if (!iio_channel_has_info(chan->channel, info))
Fabien Proriol65de7652015-01-01 12:46:48 +0000542 return -EINVAL;
543
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100544 if (chan->indio_dev->info->read_raw_multi) {
545 ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev,
546 chan->channel, INDIO_MAX_RAW_ELEMENTS,
547 vals, &val_len, info);
548 *val = vals[0];
549 *val2 = vals[1];
550 } else
551 ret = chan->indio_dev->info->read_raw(chan->indio_dev,
552 chan->channel, val, val2, info);
553
554 return ret;
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100555}
556
Jonathan Cameron314be142012-05-01 21:04:24 +0100557int iio_read_channel_raw(struct iio_channel *chan, int *val)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000558{
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100559 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100560 int ret;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000561
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100562 mutex_lock(&iio_dev_opaque->info_exist_lock);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000563 if (chan->indio_dev->info == NULL) {
564 ret = -ENODEV;
565 goto err_unlock;
566 }
567
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100568 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000569err_unlock:
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100570 mutex_unlock(&iio_dev_opaque->info_exist_lock);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000571
572 return ret;
573}
Jonathan Cameron314be142012-05-01 21:04:24 +0100574EXPORT_SYMBOL_GPL(iio_read_channel_raw);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000575
Sebastian Reichel476d4af2014-10-03 17:25:00 +0100576int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
577{
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100578 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
Sebastian Reichel476d4af2014-10-03 17:25:00 +0100579 int ret;
580
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100581 mutex_lock(&iio_dev_opaque->info_exist_lock);
Sebastian Reichel476d4af2014-10-03 17:25:00 +0100582 if (chan->indio_dev->info == NULL) {
583 ret = -ENODEV;
584 goto err_unlock;
585 }
586
587 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
588err_unlock:
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100589 mutex_unlock(&iio_dev_opaque->info_exist_lock);
Sebastian Reichel476d4af2014-10-03 17:25:00 +0100590
591 return ret;
592}
593EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
594
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100595static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
596 int raw, int *processed, unsigned int scale)
597{
598 int scale_type, scale_val, scale_val2, offset;
599 s64 raw64 = raw;
600 int ret;
601
Michael Hennerich6c5d4c92013-06-03 09:04:00 +0100602 ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_OFFSET);
Alexandre Bellonif91d1b62013-07-01 17:40:00 +0100603 if (ret >= 0)
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100604 raw64 += offset;
605
606 scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
607 IIO_CHAN_INFO_SCALE);
Linus Walleijadc8ec52017-01-11 11:48:39 +0100608 if (scale_type < 0) {
609 /*
610 * Just pass raw values as processed if no scaling is
611 * available.
612 */
613 *processed = raw;
614 return 0;
615 }
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100616
617 switch (scale_type) {
618 case IIO_VAL_INT:
619 *processed = raw64 * scale_val;
620 break;
621 case IIO_VAL_INT_PLUS_MICRO:
622 if (scale_val2 < 0)
623 *processed = -raw64 * scale_val;
624 else
625 *processed = raw64 * scale_val;
626 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
627 1000000LL);
628 break;
629 case IIO_VAL_INT_PLUS_NANO:
630 if (scale_val2 < 0)
631 *processed = -raw64 * scale_val;
632 else
633 *processed = raw64 * scale_val;
634 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
635 1000000000LL);
636 break;
637 case IIO_VAL_FRACTIONAL:
638 *processed = div_s64(raw64 * (s64)scale_val * scale,
639 scale_val2);
640 break;
Lars-Peter Clausen103d9fb2012-10-16 17:29:00 +0100641 case IIO_VAL_FRACTIONAL_LOG2:
642 *processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
643 break;
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100644 default:
645 return -EINVAL;
646 }
647
648 return 0;
649}
650
651int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
652 int *processed, unsigned int scale)
653{
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100654 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100655 int ret;
656
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100657 mutex_lock(&iio_dev_opaque->info_exist_lock);
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100658 if (chan->indio_dev->info == NULL) {
659 ret = -ENODEV;
660 goto err_unlock;
661 }
662
663 ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
664 scale);
665err_unlock:
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100666 mutex_unlock(&iio_dev_opaque->info_exist_lock);
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100667
668 return ret;
669}
670EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
671
Arnaud Pouliquen34739a212018-01-10 11:13:06 +0100672int iio_read_channel_attribute(struct iio_channel *chan, int *val, int *val2,
673 enum iio_chan_info_enum attribute)
Matt Ranostay0023e672016-09-23 23:04:07 -0700674{
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100675 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
Matt Ranostay0023e672016-09-23 23:04:07 -0700676 int ret;
677
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100678 mutex_lock(&iio_dev_opaque->info_exist_lock);
Matt Ranostay0023e672016-09-23 23:04:07 -0700679 if (chan->indio_dev->info == NULL) {
680 ret = -ENODEV;
681 goto err_unlock;
682 }
683
684 ret = iio_channel_read(chan, val, val2, attribute);
685err_unlock:
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100686 mutex_unlock(&iio_dev_opaque->info_exist_lock);
Matt Ranostay0023e672016-09-23 23:04:07 -0700687
688 return ret;
689}
Arnaud Pouliquen34739a212018-01-10 11:13:06 +0100690EXPORT_SYMBOL_GPL(iio_read_channel_attribute);
Matt Ranostay0023e672016-09-23 23:04:07 -0700691
692int iio_read_channel_offset(struct iio_channel *chan, int *val, int *val2)
693{
694 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_OFFSET);
695}
696EXPORT_SYMBOL_GPL(iio_read_channel_offset);
697
Linus Walleij635ef602021-03-08 11:02:18 +0100698int iio_read_channel_processed_scale(struct iio_channel *chan, int *val,
699 unsigned int scale)
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100700{
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100701 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100702 int ret;
703
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100704 mutex_lock(&iio_dev_opaque->info_exist_lock);
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100705 if (chan->indio_dev->info == NULL) {
706 ret = -ENODEV;
707 goto err_unlock;
708 }
709
710 if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
711 ret = iio_channel_read(chan, val, NULL,
712 IIO_CHAN_INFO_PROCESSED);
Linus Walleijb3b64e22021-03-23 13:27:05 +0100713 if (ret < 0)
Linus Walleij635ef602021-03-08 11:02:18 +0100714 goto err_unlock;
715 *val *= scale;
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100716 } else {
717 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
718 if (ret < 0)
719 goto err_unlock;
Linus Walleij635ef602021-03-08 11:02:18 +0100720 ret = iio_convert_raw_to_processed_unlocked(chan, *val, val,
721 scale);
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100722 }
723
724err_unlock:
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100725 mutex_unlock(&iio_dev_opaque->info_exist_lock);
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100726
727 return ret;
728}
Linus Walleij635ef602021-03-08 11:02:18 +0100729EXPORT_SYMBOL_GPL(iio_read_channel_processed_scale);
730
731int iio_read_channel_processed(struct iio_channel *chan, int *val)
732{
733 /* This is just a special case with scale factor 1 */
734 return iio_read_channel_processed_scale(chan, val, 1);
735}
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100736EXPORT_SYMBOL_GPL(iio_read_channel_processed);
737
Jonathan Cameron314be142012-05-01 21:04:24 +0100738int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000739{
Matt Ranostay0023e672016-09-23 23:04:07 -0700740 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_SCALE);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000741}
Jonathan Cameron314be142012-05-01 21:04:24 +0100742EXPORT_SYMBOL_GPL(iio_read_channel_scale);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000743
Peter Rosin00c5f802016-11-08 12:58:52 +0100744static int iio_channel_read_avail(struct iio_channel *chan,
745 const int **vals, int *type, int *length,
746 enum iio_chan_info_enum info)
747{
748 if (!iio_channel_has_available(chan->channel, info))
749 return -EINVAL;
750
751 return chan->indio_dev->info->read_avail(chan->indio_dev, chan->channel,
752 vals, type, length, info);
753}
754
Artur Rojek9f421092019-03-23 18:28:06 +0100755int iio_read_avail_channel_attribute(struct iio_channel *chan,
756 const int **vals, int *type, int *length,
757 enum iio_chan_info_enum attribute)
758{
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100759 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
Artur Rojek9f421092019-03-23 18:28:06 +0100760 int ret;
761
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100762 mutex_lock(&iio_dev_opaque->info_exist_lock);
Artur Rojek9f421092019-03-23 18:28:06 +0100763 if (!chan->indio_dev->info) {
764 ret = -ENODEV;
765 goto err_unlock;
766 }
767
768 ret = iio_channel_read_avail(chan, vals, type, length, attribute);
769err_unlock:
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100770 mutex_unlock(&iio_dev_opaque->info_exist_lock);
Artur Rojek9f421092019-03-23 18:28:06 +0100771
772 return ret;
773}
774EXPORT_SYMBOL_GPL(iio_read_avail_channel_attribute);
775
Peter Rosin00c5f802016-11-08 12:58:52 +0100776int iio_read_avail_channel_raw(struct iio_channel *chan,
777 const int **vals, int *length)
778{
779 int ret;
780 int type;
781
Artur Rojek89388ca2019-03-23 18:28:07 +0100782 ret = iio_read_avail_channel_attribute(chan, vals, &type, length,
783 IIO_CHAN_INFO_RAW);
Peter Rosin00c5f802016-11-08 12:58:52 +0100784
Peter Rosinc773f702017-04-20 23:01:57 +0200785 if (ret >= 0 && type != IIO_VAL_INT)
Peter Rosin00c5f802016-11-08 12:58:52 +0100786 /* raw values are assumed to be IIO_VAL_INT */
787 ret = -EINVAL;
Peter Rosin00c5f802016-11-08 12:58:52 +0100788
789 return ret;
790}
791EXPORT_SYMBOL_GPL(iio_read_avail_channel_raw);
792
793static int iio_channel_read_max(struct iio_channel *chan,
794 int *val, int *val2, int *type,
795 enum iio_chan_info_enum info)
796{
797 int unused;
798 const int *vals;
799 int length;
800 int ret;
801
802 if (!val2)
803 val2 = &unused;
804
805 ret = iio_channel_read_avail(chan, &vals, type, &length, info);
806 switch (ret) {
807 case IIO_AVAIL_RANGE:
808 switch (*type) {
809 case IIO_VAL_INT:
810 *val = vals[2];
811 break;
812 default:
813 *val = vals[4];
814 *val2 = vals[5];
815 }
816 return 0;
817
818 case IIO_AVAIL_LIST:
819 if (length <= 0)
820 return -EINVAL;
821 switch (*type) {
822 case IIO_VAL_INT:
823 *val = vals[--length];
824 while (length) {
825 if (vals[--length] > *val)
826 *val = vals[length];
827 }
828 break;
829 default:
830 /* FIXME: learn about max for other iio values */
831 return -EINVAL;
832 }
833 return 0;
834
835 default:
836 return ret;
837 }
838}
839
840int iio_read_max_channel_raw(struct iio_channel *chan, int *val)
841{
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100842 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
Peter Rosin00c5f802016-11-08 12:58:52 +0100843 int ret;
844 int type;
845
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100846 mutex_lock(&iio_dev_opaque->info_exist_lock);
Peter Rosin00c5f802016-11-08 12:58:52 +0100847 if (!chan->indio_dev->info) {
848 ret = -ENODEV;
849 goto err_unlock;
850 }
851
852 ret = iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
853err_unlock:
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100854 mutex_unlock(&iio_dev_opaque->info_exist_lock);
Peter Rosin00c5f802016-11-08 12:58:52 +0100855
856 return ret;
857}
858EXPORT_SYMBOL_GPL(iio_read_max_channel_raw);
859
Jonathan Cameron314be142012-05-01 21:04:24 +0100860int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000861{
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100862 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000863 int ret = 0;
864 /* Need to verify underlying driver has not gone away */
865
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100866 mutex_lock(&iio_dev_opaque->info_exist_lock);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000867 if (chan->indio_dev->info == NULL) {
868 ret = -ENODEV;
869 goto err_unlock;
870 }
871
872 *type = chan->channel->type;
873err_unlock:
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100874 mutex_unlock(&iio_dev_opaque->info_exist_lock);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000875
876 return ret;
877}
Jonathan Cameron314be142012-05-01 21:04:24 +0100878EXPORT_SYMBOL_GPL(iio_get_channel_type);
Dmitry Eremin-Solenikovf9380e72014-11-27 01:42:45 +0300879
880static int iio_channel_write(struct iio_channel *chan, int val, int val2,
881 enum iio_chan_info_enum info)
882{
883 return chan->indio_dev->info->write_raw(chan->indio_dev,
884 chan->channel, val, val2, info);
885}
886
Arnaud Pouliquen34739a212018-01-10 11:13:06 +0100887int iio_write_channel_attribute(struct iio_channel *chan, int val, int val2,
888 enum iio_chan_info_enum attribute)
Dmitry Eremin-Solenikovf9380e72014-11-27 01:42:45 +0300889{
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100890 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
Dmitry Eremin-Solenikovf9380e72014-11-27 01:42:45 +0300891 int ret;
892
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100893 mutex_lock(&iio_dev_opaque->info_exist_lock);
Dmitry Eremin-Solenikovf9380e72014-11-27 01:42:45 +0300894 if (chan->indio_dev->info == NULL) {
895 ret = -ENODEV;
896 goto err_unlock;
897 }
898
Arnaud Pouliquen34739a212018-01-10 11:13:06 +0100899 ret = iio_channel_write(chan, val, val2, attribute);
Dmitry Eremin-Solenikovf9380e72014-11-27 01:42:45 +0300900err_unlock:
Jonathan Cameronb804e2b2021-04-26 18:49:08 +0100901 mutex_unlock(&iio_dev_opaque->info_exist_lock);
Dmitry Eremin-Solenikovf9380e72014-11-27 01:42:45 +0300902
903 return ret;
904}
Arnaud Pouliquen34739a212018-01-10 11:13:06 +0100905EXPORT_SYMBOL_GPL(iio_write_channel_attribute);
906
907int iio_write_channel_raw(struct iio_channel *chan, int val)
908{
909 return iio_write_channel_attribute(chan, val, 0, IIO_CHAN_INFO_RAW);
910}
Dmitry Eremin-Solenikovf9380e72014-11-27 01:42:45 +0300911EXPORT_SYMBOL_GPL(iio_write_channel_raw);
Peter Rosin8a848e72017-05-14 21:51:08 +0200912
913unsigned int iio_get_channel_ext_info_count(struct iio_channel *chan)
914{
915 const struct iio_chan_spec_ext_info *ext_info;
916 unsigned int i = 0;
917
918 if (!chan->channel->ext_info)
919 return i;
920
921 for (ext_info = chan->channel->ext_info; ext_info->name; ext_info++)
922 ++i;
923
924 return i;
925}
926EXPORT_SYMBOL_GPL(iio_get_channel_ext_info_count);
927
928static const struct iio_chan_spec_ext_info *iio_lookup_ext_info(
929 const struct iio_channel *chan,
930 const char *attr)
931{
932 const struct iio_chan_spec_ext_info *ext_info;
933
934 if (!chan->channel->ext_info)
935 return NULL;
936
937 for (ext_info = chan->channel->ext_info; ext_info->name; ++ext_info) {
938 if (!strcmp(attr, ext_info->name))
939 return ext_info;
940 }
941
942 return NULL;
943}
944
945ssize_t iio_read_channel_ext_info(struct iio_channel *chan,
946 const char *attr, char *buf)
947{
948 const struct iio_chan_spec_ext_info *ext_info;
949
950 ext_info = iio_lookup_ext_info(chan, attr);
951 if (!ext_info)
952 return -EINVAL;
953
954 return ext_info->read(chan->indio_dev, ext_info->private,
955 chan->channel, buf);
956}
957EXPORT_SYMBOL_GPL(iio_read_channel_ext_info);
958
959ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
960 const char *buf, size_t len)
961{
962 const struct iio_chan_spec_ext_info *ext_info;
963
964 ext_info = iio_lookup_ext_info(chan, attr);
965 if (!ext_info)
966 return -EINVAL;
967
968 return ext_info->write(chan->indio_dev, ext_info->private,
969 chan->channel, buf, len);
970}
971EXPORT_SYMBOL_GPL(iio_write_channel_ext_info);