blob: e38f41464fe4bf727bde44ffc587d730c6426efb [file] [log] [blame]
Jonathan Camerone27d75d2012-02-15 19:48:01 +00001/* 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>
13
Jonathan Cameron06458e22012-04-25 15:54:58 +010014#include <linux/iio/iio.h>
Jonathan Camerone27d75d2012-02-15 19:48:01 +000015#include "iio_core.h"
Jonathan Cameron06458e22012-04-25 15:54:58 +010016#include <linux/iio/machine.h>
17#include <linux/iio/driver.h>
18#include <linux/iio/consumer.h>
Jonathan Camerone27d75d2012-02-15 19:48:01 +000019
20struct iio_map_internal {
21 struct iio_dev *indio_dev;
22 struct iio_map *map;
23 struct list_head l;
24};
25
26static LIST_HEAD(iio_map_list);
27static DEFINE_MUTEX(iio_map_list_lock);
28
29int iio_map_array_register(struct iio_dev *indio_dev, struct iio_map *maps)
30{
31 int i = 0, ret = 0;
32 struct iio_map_internal *mapi;
33
34 if (maps == NULL)
35 return 0;
36
37 mutex_lock(&iio_map_list_lock);
38 while (maps[i].consumer_dev_name != NULL) {
39 mapi = kzalloc(sizeof(*mapi), GFP_KERNEL);
40 if (mapi == NULL) {
41 ret = -ENOMEM;
42 goto error_ret;
43 }
44 mapi->map = &maps[i];
45 mapi->indio_dev = indio_dev;
46 list_add(&mapi->l, &iio_map_list);
47 i++;
48 }
49error_ret:
50 mutex_unlock(&iio_map_list_lock);
51
52 return ret;
53}
54EXPORT_SYMBOL_GPL(iio_map_array_register);
55
56
57/* Assumes the exact same array (e.g. memory locations)
58 * used at unregistration as used at registration rather than
59 * more complex checking of contents.
60 */
61int iio_map_array_unregister(struct iio_dev *indio_dev,
62 struct iio_map *maps)
63{
64 int i = 0, ret = 0;
65 bool found_it;
66 struct iio_map_internal *mapi;
67
68 if (maps == NULL)
69 return 0;
70
71 mutex_lock(&iio_map_list_lock);
72 while (maps[i].consumer_dev_name != NULL) {
73 found_it = false;
74 list_for_each_entry(mapi, &iio_map_list, l)
75 if (&maps[i] == mapi->map) {
76 list_del(&mapi->l);
77 kfree(mapi);
78 found_it = true;
79 break;
80 }
81 if (found_it == false) {
82 ret = -ENODEV;
83 goto error_ret;
84 }
Lothar Waßmann218f4d42012-03-24 07:19:25 +010085 i++;
Jonathan Camerone27d75d2012-02-15 19:48:01 +000086 }
87error_ret:
88 mutex_unlock(&iio_map_list_lock);
89
90 return ret;
91}
92EXPORT_SYMBOL_GPL(iio_map_array_unregister);
93
94static const struct iio_chan_spec
Jonathan Cameron314be142012-05-01 21:04:24 +010095*iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name)
Jonathan Camerone27d75d2012-02-15 19:48:01 +000096{
97 int i;
98 const struct iio_chan_spec *chan = NULL;
99
100 for (i = 0; i < indio_dev->num_channels; i++)
101 if (indio_dev->channels[i].datasheet_name &&
102 strcmp(name, indio_dev->channels[i].datasheet_name) == 0) {
103 chan = &indio_dev->channels[i];
104 break;
105 }
106 return chan;
107}
108
109
Jonathan Cameron314be142012-05-01 21:04:24 +0100110struct iio_channel *iio_channel_get(const char *name, const char *channel_name)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000111{
112 struct iio_map_internal *c_i = NULL, *c = NULL;
113 struct iio_channel *channel;
114
115 if (name == NULL && channel_name == NULL)
116 return ERR_PTR(-ENODEV);
117
118 /* first find matching entry the channel map */
119 mutex_lock(&iio_map_list_lock);
120 list_for_each_entry(c_i, &iio_map_list, l) {
121 if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
122 (channel_name &&
123 strcmp(channel_name, c_i->map->consumer_channel) != 0))
124 continue;
125 c = c_i;
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200126 iio_device_get(c->indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000127 break;
128 }
129 mutex_unlock(&iio_map_list_lock);
130 if (c == NULL)
131 return ERR_PTR(-ENODEV);
132
Kim, Milo2cc412b2012-09-14 02:24:00 +0100133 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000134 if (channel == NULL)
Kim, Milo801c4b52012-09-18 05:55:00 +0100135 goto error_no_mem;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000136
137 channel->indio_dev = c->indio_dev;
138
Kim, Milob2b79ff2012-09-17 09:44:00 +0100139 if (c->map->adc_channel_label) {
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000140 channel->channel =
141 iio_chan_spec_from_name(channel->indio_dev,
142 c->map->adc_channel_label);
143
Kim, Milob2b79ff2012-09-17 09:44:00 +0100144 if (channel->channel == NULL)
145 goto error_no_chan;
146 }
147
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000148 return channel;
Kim, Milob2b79ff2012-09-17 09:44:00 +0100149
150error_no_chan:
151 iio_device_put(c->indio_dev);
152 kfree(channel);
153 return ERR_PTR(-EINVAL);
Kim, Milo801c4b52012-09-18 05:55:00 +0100154error_no_mem:
155 iio_device_put(c->indio_dev);
156 return ERR_PTR(-ENOMEM);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000157}
Jonathan Cameron314be142012-05-01 21:04:24 +0100158EXPORT_SYMBOL_GPL(iio_channel_get);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000159
Jonathan Cameron314be142012-05-01 21:04:24 +0100160void iio_channel_release(struct iio_channel *channel)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000161{
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200162 iio_device_put(channel->indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000163 kfree(channel);
164}
Jonathan Cameron314be142012-05-01 21:04:24 +0100165EXPORT_SYMBOL_GPL(iio_channel_release);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000166
Jonathan Cameron314be142012-05-01 21:04:24 +0100167struct iio_channel *iio_channel_get_all(const char *name)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000168{
169 struct iio_channel *chans;
170 struct iio_map_internal *c = NULL;
171 int nummaps = 0;
172 int mapind = 0;
173 int i, ret;
174
175 if (name == NULL)
176 return ERR_PTR(-EINVAL);
177
178 mutex_lock(&iio_map_list_lock);
179 /* first count the matching maps */
180 list_for_each_entry(c, &iio_map_list, l)
181 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
182 continue;
183 else
184 nummaps++;
185
186 if (nummaps == 0) {
187 ret = -ENODEV;
188 goto error_ret;
189 }
190
191 /* NULL terminated array to save passing size */
192 chans = kzalloc(sizeof(*chans)*(nummaps + 1), GFP_KERNEL);
193 if (chans == NULL) {
194 ret = -ENOMEM;
195 goto error_ret;
196 }
197
198 /* for each map fill in the chans element */
199 list_for_each_entry(c, &iio_map_list, l) {
200 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
201 continue;
202 chans[mapind].indio_dev = c->indio_dev;
203 chans[mapind].channel =
204 iio_chan_spec_from_name(chans[mapind].indio_dev,
205 c->map->adc_channel_label);
206 if (chans[mapind].channel == NULL) {
207 ret = -EINVAL;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000208 goto error_free_chans;
209 }
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200210 iio_device_get(chans[mapind].indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000211 mapind++;
212 }
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000213 if (mapind == 0) {
214 ret = -ENODEV;
215 goto error_free_chans;
216 }
Dan Carpentere59b9af2012-07-11 07:34:00 +0100217 mutex_unlock(&iio_map_list_lock);
218
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000219 return chans;
220
221error_free_chans:
222 for (i = 0; i < nummaps; i++)
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200223 iio_device_put(chans[i].indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000224 kfree(chans);
225error_ret:
226 mutex_unlock(&iio_map_list_lock);
227
228 return ERR_PTR(ret);
229}
Jonathan Cameron314be142012-05-01 21:04:24 +0100230EXPORT_SYMBOL_GPL(iio_channel_get_all);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000231
Jonathan Cameron314be142012-05-01 21:04:24 +0100232void iio_channel_release_all(struct iio_channel *channels)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000233{
234 struct iio_channel *chan = &channels[0];
235
236 while (chan->indio_dev) {
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200237 iio_device_put(chan->indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000238 chan++;
239 }
240 kfree(channels);
241}
Jonathan Cameron314be142012-05-01 21:04:24 +0100242EXPORT_SYMBOL_GPL(iio_channel_release_all);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000243
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100244static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
245 enum iio_chan_info_enum info)
246{
247 int unused;
248
249 if (val2 == NULL)
250 val2 = &unused;
251
252 return chan->indio_dev->info->read_raw(chan->indio_dev, chan->channel,
253 val, val2, info);
254}
255
Jonathan Cameron314be142012-05-01 21:04:24 +0100256int iio_read_channel_raw(struct iio_channel *chan, int *val)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000257{
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100258 int ret;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000259
260 mutex_lock(&chan->indio_dev->info_exist_lock);
261 if (chan->indio_dev->info == NULL) {
262 ret = -ENODEV;
263 goto err_unlock;
264 }
265
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100266 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000267err_unlock:
268 mutex_unlock(&chan->indio_dev->info_exist_lock);
269
270 return ret;
271}
Jonathan Cameron314be142012-05-01 21:04:24 +0100272EXPORT_SYMBOL_GPL(iio_read_channel_raw);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000273
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100274static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
275 int raw, int *processed, unsigned int scale)
276{
277 int scale_type, scale_val, scale_val2, offset;
278 s64 raw64 = raw;
279 int ret;
280
281 ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_SCALE);
282 if (ret == 0)
283 raw64 += offset;
284
285 scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
286 IIO_CHAN_INFO_SCALE);
287 if (scale_type < 0)
288 return scale_type;
289
290 switch (scale_type) {
291 case IIO_VAL_INT:
292 *processed = raw64 * scale_val;
293 break;
294 case IIO_VAL_INT_PLUS_MICRO:
295 if (scale_val2 < 0)
296 *processed = -raw64 * scale_val;
297 else
298 *processed = raw64 * scale_val;
299 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
300 1000000LL);
301 break;
302 case IIO_VAL_INT_PLUS_NANO:
303 if (scale_val2 < 0)
304 *processed = -raw64 * scale_val;
305 else
306 *processed = raw64 * scale_val;
307 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
308 1000000000LL);
309 break;
310 case IIO_VAL_FRACTIONAL:
311 *processed = div_s64(raw64 * (s64)scale_val * scale,
312 scale_val2);
313 break;
314 default:
315 return -EINVAL;
316 }
317
318 return 0;
319}
320
321int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
322 int *processed, unsigned int scale)
323{
324 int ret;
325
326 mutex_lock(&chan->indio_dev->info_exist_lock);
327 if (chan->indio_dev->info == NULL) {
328 ret = -ENODEV;
329 goto err_unlock;
330 }
331
332 ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
333 scale);
334err_unlock:
335 mutex_unlock(&chan->indio_dev->info_exist_lock);
336
337 return ret;
338}
339EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
340
341int iio_read_channel_processed(struct iio_channel *chan, int *val)
342{
343 int ret;
344
345 mutex_lock(&chan->indio_dev->info_exist_lock);
346 if (chan->indio_dev->info == NULL) {
347 ret = -ENODEV;
348 goto err_unlock;
349 }
350
351 if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
352 ret = iio_channel_read(chan, val, NULL,
353 IIO_CHAN_INFO_PROCESSED);
354 } else {
355 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
356 if (ret < 0)
357 goto err_unlock;
358 ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1);
359 }
360
361err_unlock:
362 mutex_unlock(&chan->indio_dev->info_exist_lock);
363
364 return ret;
365}
366EXPORT_SYMBOL_GPL(iio_read_channel_processed);
367
Jonathan Cameron314be142012-05-01 21:04:24 +0100368int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000369{
370 int ret;
371
372 mutex_lock(&chan->indio_dev->info_exist_lock);
373 if (chan->indio_dev->info == NULL) {
374 ret = -ENODEV;
375 goto err_unlock;
376 }
377
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100378 ret = iio_channel_read(chan, val, val2, IIO_CHAN_INFO_SCALE);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000379err_unlock:
380 mutex_unlock(&chan->indio_dev->info_exist_lock);
381
382 return ret;
383}
Jonathan Cameron314be142012-05-01 21:04:24 +0100384EXPORT_SYMBOL_GPL(iio_read_channel_scale);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000385
Jonathan Cameron314be142012-05-01 21:04:24 +0100386int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000387{
388 int ret = 0;
389 /* Need to verify underlying driver has not gone away */
390
391 mutex_lock(&chan->indio_dev->info_exist_lock);
392 if (chan->indio_dev->info == NULL) {
393 ret = -ENODEV;
394 goto err_unlock;
395 }
396
397 *type = chan->channel->type;
398err_unlock:
399 mutex_unlock(&chan->indio_dev->info_exist_lock);
400
401 return ret;
402}
Jonathan Cameron314be142012-05-01 21:04:24 +0100403EXPORT_SYMBOL_GPL(iio_get_channel_type);