blob: 58d0ffe856b6868cc587ccc05c135b2514127d6b [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 }
Lars-Peter Clausen7737fa62012-10-18 15:43:00 +010081 if (!found_it) {
Jonathan Camerone27d75d2012-02-15 19:48:01 +000082 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;
Kim, Milo3183bac2012-09-18 05:56:00 +0100114 int err;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000115
116 if (name == NULL && channel_name == NULL)
117 return ERR_PTR(-ENODEV);
118
119 /* first find matching entry the channel map */
120 mutex_lock(&iio_map_list_lock);
121 list_for_each_entry(c_i, &iio_map_list, l) {
122 if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
123 (channel_name &&
124 strcmp(channel_name, c_i->map->consumer_channel) != 0))
125 continue;
126 c = c_i;
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200127 iio_device_get(c->indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000128 break;
129 }
130 mutex_unlock(&iio_map_list_lock);
131 if (c == NULL)
132 return ERR_PTR(-ENODEV);
133
Kim, Milo2cc412b2012-09-14 02:24:00 +0100134 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
Kim, Milo3183bac2012-09-18 05:56:00 +0100135 if (channel == NULL) {
136 err = -ENOMEM;
Kim, Milo801c4b52012-09-18 05:55:00 +0100137 goto error_no_mem;
Kim, Milo3183bac2012-09-18 05:56:00 +0100138 }
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000139
140 channel->indio_dev = c->indio_dev;
141
Kim, Milob2b79ff2012-09-17 09:44:00 +0100142 if (c->map->adc_channel_label) {
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000143 channel->channel =
144 iio_chan_spec_from_name(channel->indio_dev,
145 c->map->adc_channel_label);
146
Kim, Milo3183bac2012-09-18 05:56:00 +0100147 if (channel->channel == NULL) {
148 err = -EINVAL;
Kim, Milob2b79ff2012-09-17 09:44:00 +0100149 goto error_no_chan;
Kim, Milo3183bac2012-09-18 05:56:00 +0100150 }
Kim, Milob2b79ff2012-09-17 09:44:00 +0100151 }
152
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000153 return channel;
Kim, Milob2b79ff2012-09-17 09:44:00 +0100154
155error_no_chan:
Kim, Milob2b79ff2012-09-17 09:44:00 +0100156 kfree(channel);
Kim, Milo801c4b52012-09-18 05:55:00 +0100157error_no_mem:
158 iio_device_put(c->indio_dev);
Kim, Milo3183bac2012-09-18 05:56:00 +0100159 return ERR_PTR(err);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000160}
Jonathan Cameron314be142012-05-01 21:04:24 +0100161EXPORT_SYMBOL_GPL(iio_channel_get);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000162
Jonathan Cameron314be142012-05-01 21:04:24 +0100163void iio_channel_release(struct iio_channel *channel)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000164{
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200165 iio_device_put(channel->indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000166 kfree(channel);
167}
Jonathan Cameron314be142012-05-01 21:04:24 +0100168EXPORT_SYMBOL_GPL(iio_channel_release);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000169
Guenter Roeckca7d98d2013-01-31 21:43:00 +0000170struct iio_channel *iio_channel_get_all(struct device *dev)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000171{
Guenter Roeckca7d98d2013-01-31 21:43:00 +0000172 const char *name;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000173 struct iio_channel *chans;
174 struct iio_map_internal *c = NULL;
175 int nummaps = 0;
176 int mapind = 0;
177 int i, ret;
178
Guenter Roeckca7d98d2013-01-31 21:43:00 +0000179 if (dev == NULL)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000180 return ERR_PTR(-EINVAL);
Guenter Roeckca7d98d2013-01-31 21:43:00 +0000181 name = dev_name(dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000182
183 mutex_lock(&iio_map_list_lock);
184 /* first count the matching maps */
185 list_for_each_entry(c, &iio_map_list, l)
186 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
187 continue;
188 else
189 nummaps++;
190
191 if (nummaps == 0) {
192 ret = -ENODEV;
193 goto error_ret;
194 }
195
196 /* NULL terminated array to save passing size */
197 chans = kzalloc(sizeof(*chans)*(nummaps + 1), GFP_KERNEL);
198 if (chans == NULL) {
199 ret = -ENOMEM;
200 goto error_ret;
201 }
202
203 /* for each map fill in the chans element */
204 list_for_each_entry(c, &iio_map_list, l) {
205 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
206 continue;
207 chans[mapind].indio_dev = c->indio_dev;
Jonathan Cameron04644152012-06-30 20:06:00 +0100208 chans[mapind].data = c->map->consumer_data;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000209 chans[mapind].channel =
210 iio_chan_spec_from_name(chans[mapind].indio_dev,
211 c->map->adc_channel_label);
212 if (chans[mapind].channel == NULL) {
213 ret = -EINVAL;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000214 goto error_free_chans;
215 }
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200216 iio_device_get(chans[mapind].indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000217 mapind++;
218 }
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000219 if (mapind == 0) {
220 ret = -ENODEV;
221 goto error_free_chans;
222 }
Dan Carpentere59b9af2012-07-11 07:34:00 +0100223 mutex_unlock(&iio_map_list_lock);
224
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000225 return chans;
226
227error_free_chans:
228 for (i = 0; i < nummaps; i++)
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200229 iio_device_put(chans[i].indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000230 kfree(chans);
231error_ret:
232 mutex_unlock(&iio_map_list_lock);
233
234 return ERR_PTR(ret);
235}
Jonathan Cameron314be142012-05-01 21:04:24 +0100236EXPORT_SYMBOL_GPL(iio_channel_get_all);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000237
Jonathan Cameron314be142012-05-01 21:04:24 +0100238void iio_channel_release_all(struct iio_channel *channels)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000239{
240 struct iio_channel *chan = &channels[0];
241
242 while (chan->indio_dev) {
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200243 iio_device_put(chan->indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000244 chan++;
245 }
246 kfree(channels);
247}
Jonathan Cameron314be142012-05-01 21:04:24 +0100248EXPORT_SYMBOL_GPL(iio_channel_release_all);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000249
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100250static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
251 enum iio_chan_info_enum info)
252{
253 int unused;
254
255 if (val2 == NULL)
256 val2 = &unused;
257
258 return chan->indio_dev->info->read_raw(chan->indio_dev, chan->channel,
259 val, val2, info);
260}
261
Jonathan Cameron314be142012-05-01 21:04:24 +0100262int iio_read_channel_raw(struct iio_channel *chan, int *val)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000263{
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100264 int ret;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000265
266 mutex_lock(&chan->indio_dev->info_exist_lock);
267 if (chan->indio_dev->info == NULL) {
268 ret = -ENODEV;
269 goto err_unlock;
270 }
271
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100272 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000273err_unlock:
274 mutex_unlock(&chan->indio_dev->info_exist_lock);
275
276 return ret;
277}
Jonathan Cameron314be142012-05-01 21:04:24 +0100278EXPORT_SYMBOL_GPL(iio_read_channel_raw);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000279
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100280static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
281 int raw, int *processed, unsigned int scale)
282{
283 int scale_type, scale_val, scale_val2, offset;
284 s64 raw64 = raw;
285 int ret;
286
287 ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_SCALE);
288 if (ret == 0)
289 raw64 += offset;
290
291 scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
292 IIO_CHAN_INFO_SCALE);
293 if (scale_type < 0)
294 return scale_type;
295
296 switch (scale_type) {
297 case IIO_VAL_INT:
298 *processed = raw64 * scale_val;
299 break;
300 case IIO_VAL_INT_PLUS_MICRO:
301 if (scale_val2 < 0)
302 *processed = -raw64 * scale_val;
303 else
304 *processed = raw64 * scale_val;
305 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
306 1000000LL);
307 break;
308 case IIO_VAL_INT_PLUS_NANO:
309 if (scale_val2 < 0)
310 *processed = -raw64 * scale_val;
311 else
312 *processed = raw64 * scale_val;
313 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
314 1000000000LL);
315 break;
316 case IIO_VAL_FRACTIONAL:
317 *processed = div_s64(raw64 * (s64)scale_val * scale,
318 scale_val2);
319 break;
Lars-Peter Clausen103d9fb2012-10-16 17:29:00 +0100320 case IIO_VAL_FRACTIONAL_LOG2:
321 *processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
322 break;
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100323 default:
324 return -EINVAL;
325 }
326
327 return 0;
328}
329
330int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
331 int *processed, unsigned int scale)
332{
333 int ret;
334
335 mutex_lock(&chan->indio_dev->info_exist_lock);
336 if (chan->indio_dev->info == NULL) {
337 ret = -ENODEV;
338 goto err_unlock;
339 }
340
341 ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
342 scale);
343err_unlock:
344 mutex_unlock(&chan->indio_dev->info_exist_lock);
345
346 return ret;
347}
348EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
349
350int iio_read_channel_processed(struct iio_channel *chan, int *val)
351{
352 int ret;
353
354 mutex_lock(&chan->indio_dev->info_exist_lock);
355 if (chan->indio_dev->info == NULL) {
356 ret = -ENODEV;
357 goto err_unlock;
358 }
359
360 if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
361 ret = iio_channel_read(chan, val, NULL,
362 IIO_CHAN_INFO_PROCESSED);
363 } else {
364 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
365 if (ret < 0)
366 goto err_unlock;
367 ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1);
368 }
369
370err_unlock:
371 mutex_unlock(&chan->indio_dev->info_exist_lock);
372
373 return ret;
374}
375EXPORT_SYMBOL_GPL(iio_read_channel_processed);
376
Jonathan Cameron314be142012-05-01 21:04:24 +0100377int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000378{
379 int ret;
380
381 mutex_lock(&chan->indio_dev->info_exist_lock);
382 if (chan->indio_dev->info == NULL) {
383 ret = -ENODEV;
384 goto err_unlock;
385 }
386
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100387 ret = iio_channel_read(chan, val, val2, IIO_CHAN_INFO_SCALE);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000388err_unlock:
389 mutex_unlock(&chan->indio_dev->info_exist_lock);
390
391 return ret;
392}
Jonathan Cameron314be142012-05-01 21:04:24 +0100393EXPORT_SYMBOL_GPL(iio_read_channel_scale);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000394
Jonathan Cameron314be142012-05-01 21:04:24 +0100395int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000396{
397 int ret = 0;
398 /* Need to verify underlying driver has not gone away */
399
400 mutex_lock(&chan->indio_dev->info_exist_lock);
401 if (chan->indio_dev->info == NULL) {
402 ret = -ENODEV;
403 goto err_unlock;
404 }
405
406 *type = chan->channel->type;
407err_unlock:
408 mutex_unlock(&chan->indio_dev->info_exist_lock);
409
410 return ret;
411}
Jonathan Cameron314be142012-05-01 21:04:24 +0100412EXPORT_SYMBOL_GPL(iio_get_channel_type);