Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 1 | /* The industrial I/O core in kernel channel mapping |
| 2 | * |
| 3 | * Copyright (c) 2011 Jonathan Cameron |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published by |
| 7 | * the Free Software Foundation. |
| 8 | */ |
| 9 | #include <linux/err.h> |
| 10 | #include <linux/export.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/mutex.h> |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 13 | #include <linux/of.h> |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 14 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 15 | #include <linux/iio/iio.h> |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 16 | #include "iio_core.h" |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 17 | #include <linux/iio/machine.h> |
| 18 | #include <linux/iio/driver.h> |
| 19 | #include <linux/iio/consumer.h> |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 20 | |
| 21 | struct iio_map_internal { |
| 22 | struct iio_dev *indio_dev; |
| 23 | struct iio_map *map; |
| 24 | struct list_head l; |
| 25 | }; |
| 26 | |
| 27 | static LIST_HEAD(iio_map_list); |
| 28 | static DEFINE_MUTEX(iio_map_list_lock); |
| 29 | |
| 30 | int iio_map_array_register(struct iio_dev *indio_dev, struct iio_map *maps) |
| 31 | { |
| 32 | int i = 0, ret = 0; |
| 33 | struct iio_map_internal *mapi; |
| 34 | |
| 35 | if (maps == NULL) |
| 36 | return 0; |
| 37 | |
| 38 | mutex_lock(&iio_map_list_lock); |
| 39 | while (maps[i].consumer_dev_name != NULL) { |
| 40 | mapi = kzalloc(sizeof(*mapi), GFP_KERNEL); |
| 41 | if (mapi == NULL) { |
| 42 | ret = -ENOMEM; |
| 43 | goto error_ret; |
| 44 | } |
| 45 | mapi->map = &maps[i]; |
| 46 | mapi->indio_dev = indio_dev; |
| 47 | list_add(&mapi->l, &iio_map_list); |
| 48 | i++; |
| 49 | } |
| 50 | error_ret: |
| 51 | mutex_unlock(&iio_map_list_lock); |
| 52 | |
| 53 | return ret; |
| 54 | } |
| 55 | EXPORT_SYMBOL_GPL(iio_map_array_register); |
| 56 | |
| 57 | |
Guenter Roeck | 6cb2afd | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 58 | /* |
| 59 | * Remove all map entries associated with the given iio device |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 60 | */ |
Guenter Roeck | 6cb2afd | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 61 | int iio_map_array_unregister(struct iio_dev *indio_dev) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 62 | { |
Guenter Roeck | 6cb2afd | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 63 | int ret = -ENODEV; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 64 | struct iio_map_internal *mapi; |
Guenter Roeck | 6cb2afd | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 65 | struct list_head *pos, *tmp; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 66 | |
| 67 | mutex_lock(&iio_map_list_lock); |
Guenter Roeck | 6cb2afd | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 68 | list_for_each_safe(pos, tmp, &iio_map_list) { |
| 69 | mapi = list_entry(pos, struct iio_map_internal, l); |
| 70 | if (indio_dev == mapi->indio_dev) { |
| 71 | list_del(&mapi->l); |
| 72 | kfree(mapi); |
| 73 | ret = 0; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 74 | } |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 75 | } |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 76 | mutex_unlock(&iio_map_list_lock); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 77 | return ret; |
| 78 | } |
| 79 | EXPORT_SYMBOL_GPL(iio_map_array_unregister); |
| 80 | |
| 81 | static const struct iio_chan_spec |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 82 | *iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 83 | { |
| 84 | int i; |
| 85 | const struct iio_chan_spec *chan = NULL; |
| 86 | |
| 87 | for (i = 0; i < indio_dev->num_channels; i++) |
| 88 | if (indio_dev->channels[i].datasheet_name && |
| 89 | strcmp(name, indio_dev->channels[i].datasheet_name) == 0) { |
| 90 | chan = &indio_dev->channels[i]; |
| 91 | break; |
| 92 | } |
| 93 | return chan; |
| 94 | } |
| 95 | |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 96 | #ifdef CONFIG_OF |
| 97 | |
| 98 | static int iio_dev_node_match(struct device *dev, void *data) |
| 99 | { |
| 100 | return dev->of_node == data && dev->type == &iio_device_type; |
| 101 | } |
| 102 | |
| 103 | static int __of_iio_channel_get(struct iio_channel *channel, |
| 104 | struct device_node *np, int index) |
| 105 | { |
| 106 | struct device *idev; |
| 107 | struct iio_dev *indio_dev; |
| 108 | int err; |
| 109 | struct of_phandle_args iiospec; |
| 110 | |
| 111 | err = of_parse_phandle_with_args(np, "io-channels", |
| 112 | "#io-channel-cells", |
| 113 | index, &iiospec); |
| 114 | if (err) |
| 115 | return err; |
| 116 | |
| 117 | idev = bus_find_device(&iio_bus_type, NULL, iiospec.np, |
| 118 | iio_dev_node_match); |
| 119 | of_node_put(iiospec.np); |
| 120 | if (idev == NULL) |
| 121 | return -EPROBE_DEFER; |
| 122 | |
| 123 | indio_dev = dev_to_iio_dev(idev); |
| 124 | channel->indio_dev = indio_dev; |
| 125 | index = iiospec.args_count ? iiospec.args[0] : 0; |
| 126 | if (index >= indio_dev->num_channels) { |
Joe Perches | e916b80 | 2013-06-04 15:44:00 +0100 | [diff] [blame] | 127 | err = -EINVAL; |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 128 | goto err_put; |
| 129 | } |
| 130 | channel->channel = &indio_dev->channels[index]; |
| 131 | |
| 132 | return 0; |
| 133 | |
| 134 | err_put: |
| 135 | iio_device_put(indio_dev); |
| 136 | return err; |
| 137 | } |
| 138 | |
| 139 | static struct iio_channel *of_iio_channel_get(struct device_node *np, int index) |
| 140 | { |
| 141 | struct iio_channel *channel; |
| 142 | int err; |
| 143 | |
| 144 | if (index < 0) |
| 145 | return ERR_PTR(-EINVAL); |
| 146 | |
| 147 | channel = kzalloc(sizeof(*channel), GFP_KERNEL); |
| 148 | if (channel == NULL) |
| 149 | return ERR_PTR(-ENOMEM); |
| 150 | |
| 151 | err = __of_iio_channel_get(channel, np, index); |
| 152 | if (err) |
| 153 | goto err_free_channel; |
| 154 | |
| 155 | return channel; |
| 156 | |
| 157 | err_free_channel: |
| 158 | kfree(channel); |
| 159 | return ERR_PTR(err); |
| 160 | } |
| 161 | |
| 162 | static struct iio_channel *of_iio_channel_get_by_name(struct device_node *np, |
| 163 | const char *name) |
| 164 | { |
| 165 | struct iio_channel *chan = NULL; |
| 166 | |
| 167 | /* Walk up the tree of devices looking for a matching iio channel */ |
| 168 | while (np) { |
| 169 | int index = 0; |
| 170 | |
| 171 | /* |
| 172 | * For named iio channels, first look up the name in the |
| 173 | * "io-channel-names" property. If it cannot be found, the |
| 174 | * index will be an error code, and of_iio_channel_get() |
| 175 | * will fail. |
| 176 | */ |
| 177 | if (name) |
| 178 | index = of_property_match_string(np, "io-channel-names", |
| 179 | name); |
| 180 | chan = of_iio_channel_get(np, index); |
| 181 | if (!IS_ERR(chan)) |
| 182 | break; |
| 183 | else if (name && index >= 0) { |
| 184 | pr_err("ERROR: could not get IIO channel %s:%s(%i)\n", |
| 185 | np->full_name, name ? name : "", index); |
| 186 | return chan; |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * No matching IIO channel found on this node. |
| 191 | * If the parent node has a "io-channel-ranges" property, |
| 192 | * then we can try one of its channels. |
| 193 | */ |
| 194 | np = np->parent; |
| 195 | if (np && !of_get_property(np, "io-channel-ranges", NULL)) |
| 196 | break; |
| 197 | } |
| 198 | return chan; |
| 199 | } |
| 200 | |
| 201 | static struct iio_channel *of_iio_channel_get_all(struct device *dev) |
| 202 | { |
| 203 | struct iio_channel *chans; |
| 204 | int i, mapind, nummaps = 0; |
| 205 | int ret; |
| 206 | |
| 207 | do { |
| 208 | ret = of_parse_phandle_with_args(dev->of_node, |
| 209 | "io-channels", |
| 210 | "#io-channel-cells", |
| 211 | nummaps, NULL); |
| 212 | if (ret < 0) |
| 213 | break; |
| 214 | } while (++nummaps); |
| 215 | |
| 216 | if (nummaps == 0) /* no error, return NULL to search map table */ |
| 217 | return NULL; |
| 218 | |
| 219 | /* NULL terminated array to save passing size */ |
| 220 | chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL); |
| 221 | if (chans == NULL) |
| 222 | return ERR_PTR(-ENOMEM); |
| 223 | |
| 224 | /* Search for OF matches */ |
| 225 | for (mapind = 0; mapind < nummaps; mapind++) { |
| 226 | ret = __of_iio_channel_get(&chans[mapind], dev->of_node, |
| 227 | mapind); |
| 228 | if (ret) |
| 229 | goto error_free_chans; |
| 230 | } |
| 231 | return chans; |
| 232 | |
| 233 | error_free_chans: |
| 234 | for (i = 0; i < mapind; i++) |
| 235 | iio_device_put(chans[i].indio_dev); |
| 236 | kfree(chans); |
| 237 | return ERR_PTR(ret); |
| 238 | } |
| 239 | |
| 240 | #else /* CONFIG_OF */ |
| 241 | |
| 242 | static inline struct iio_channel * |
| 243 | of_iio_channel_get_by_name(struct device_node *np, const char *name) |
| 244 | { |
| 245 | return NULL; |
| 246 | } |
| 247 | |
| 248 | static inline struct iio_channel *of_iio_channel_get_all(struct device *dev) |
| 249 | { |
| 250 | return NULL; |
| 251 | } |
| 252 | |
| 253 | #endif /* CONFIG_OF */ |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 254 | |
Guenter Roeck | 5aa57f0 | 2013-02-04 20:26:00 +0000 | [diff] [blame] | 255 | static struct iio_channel *iio_channel_get_sys(const char *name, |
| 256 | const char *channel_name) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 257 | { |
| 258 | struct iio_map_internal *c_i = NULL, *c = NULL; |
| 259 | struct iio_channel *channel; |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 260 | int err; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 261 | |
| 262 | if (name == NULL && channel_name == NULL) |
| 263 | return ERR_PTR(-ENODEV); |
| 264 | |
| 265 | /* first find matching entry the channel map */ |
| 266 | mutex_lock(&iio_map_list_lock); |
| 267 | list_for_each_entry(c_i, &iio_map_list, l) { |
| 268 | if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) || |
| 269 | (channel_name && |
| 270 | strcmp(channel_name, c_i->map->consumer_channel) != 0)) |
| 271 | continue; |
| 272 | c = c_i; |
Lars-Peter Clausen | 1875ffd | 2012-06-04 10:50:03 +0200 | [diff] [blame] | 273 | iio_device_get(c->indio_dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 274 | break; |
| 275 | } |
| 276 | mutex_unlock(&iio_map_list_lock); |
| 277 | if (c == NULL) |
| 278 | return ERR_PTR(-ENODEV); |
| 279 | |
Kim, Milo | 2cc412b | 2012-09-14 02:24:00 +0100 | [diff] [blame] | 280 | channel = kzalloc(sizeof(*channel), GFP_KERNEL); |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 281 | if (channel == NULL) { |
| 282 | err = -ENOMEM; |
Kim, Milo | 801c4b5 | 2012-09-18 05:55:00 +0100 | [diff] [blame] | 283 | goto error_no_mem; |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 284 | } |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 285 | |
| 286 | channel->indio_dev = c->indio_dev; |
| 287 | |
Kim, Milo | b2b79ff | 2012-09-17 09:44:00 +0100 | [diff] [blame] | 288 | if (c->map->adc_channel_label) { |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 289 | channel->channel = |
| 290 | iio_chan_spec_from_name(channel->indio_dev, |
| 291 | c->map->adc_channel_label); |
| 292 | |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 293 | if (channel->channel == NULL) { |
| 294 | err = -EINVAL; |
Kim, Milo | b2b79ff | 2012-09-17 09:44:00 +0100 | [diff] [blame] | 295 | goto error_no_chan; |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 296 | } |
Kim, Milo | b2b79ff | 2012-09-17 09:44:00 +0100 | [diff] [blame] | 297 | } |
| 298 | |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 299 | return channel; |
Kim, Milo | b2b79ff | 2012-09-17 09:44:00 +0100 | [diff] [blame] | 300 | |
| 301 | error_no_chan: |
Kim, Milo | b2b79ff | 2012-09-17 09:44:00 +0100 | [diff] [blame] | 302 | kfree(channel); |
Kim, Milo | 801c4b5 | 2012-09-18 05:55:00 +0100 | [diff] [blame] | 303 | error_no_mem: |
| 304 | iio_device_put(c->indio_dev); |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 305 | return ERR_PTR(err); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 306 | } |
Guenter Roeck | 5aa57f0 | 2013-02-04 20:26:00 +0000 | [diff] [blame] | 307 | |
| 308 | struct iio_channel *iio_channel_get(struct device *dev, |
| 309 | const char *channel_name) |
| 310 | { |
| 311 | const char *name = dev ? dev_name(dev) : NULL; |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 312 | struct iio_channel *channel; |
Guenter Roeck | 5aa57f0 | 2013-02-04 20:26:00 +0000 | [diff] [blame] | 313 | |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 314 | if (dev) { |
| 315 | channel = of_iio_channel_get_by_name(dev->of_node, |
| 316 | channel_name); |
| 317 | if (channel != NULL) |
| 318 | return channel; |
| 319 | } |
Guenter Roeck | 5aa57f0 | 2013-02-04 20:26:00 +0000 | [diff] [blame] | 320 | return iio_channel_get_sys(name, channel_name); |
| 321 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 322 | EXPORT_SYMBOL_GPL(iio_channel_get); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 323 | |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 324 | void iio_channel_release(struct iio_channel *channel) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 325 | { |
Lars-Peter Clausen | 1875ffd | 2012-06-04 10:50:03 +0200 | [diff] [blame] | 326 | iio_device_put(channel->indio_dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 327 | kfree(channel); |
| 328 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 329 | EXPORT_SYMBOL_GPL(iio_channel_release); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 330 | |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 331 | struct iio_channel *iio_channel_get_all(struct device *dev) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 332 | { |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 333 | const char *name; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 334 | struct iio_channel *chans; |
| 335 | struct iio_map_internal *c = NULL; |
| 336 | int nummaps = 0; |
| 337 | int mapind = 0; |
| 338 | int i, ret; |
| 339 | |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 340 | if (dev == NULL) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 341 | return ERR_PTR(-EINVAL); |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 342 | |
| 343 | chans = of_iio_channel_get_all(dev); |
| 344 | if (chans) |
| 345 | return chans; |
| 346 | |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 347 | name = dev_name(dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 348 | |
| 349 | mutex_lock(&iio_map_list_lock); |
| 350 | /* first count the matching maps */ |
| 351 | list_for_each_entry(c, &iio_map_list, l) |
| 352 | if (name && strcmp(name, c->map->consumer_dev_name) != 0) |
| 353 | continue; |
| 354 | else |
| 355 | nummaps++; |
| 356 | |
| 357 | if (nummaps == 0) { |
| 358 | ret = -ENODEV; |
| 359 | goto error_ret; |
| 360 | } |
| 361 | |
| 362 | /* NULL terminated array to save passing size */ |
| 363 | chans = kzalloc(sizeof(*chans)*(nummaps + 1), GFP_KERNEL); |
| 364 | if (chans == NULL) { |
| 365 | ret = -ENOMEM; |
| 366 | goto error_ret; |
| 367 | } |
| 368 | |
| 369 | /* for each map fill in the chans element */ |
| 370 | list_for_each_entry(c, &iio_map_list, l) { |
| 371 | if (name && strcmp(name, c->map->consumer_dev_name) != 0) |
| 372 | continue; |
| 373 | chans[mapind].indio_dev = c->indio_dev; |
Jonathan Cameron | 0464415 | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 374 | chans[mapind].data = c->map->consumer_data; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 375 | chans[mapind].channel = |
| 376 | iio_chan_spec_from_name(chans[mapind].indio_dev, |
| 377 | c->map->adc_channel_label); |
| 378 | if (chans[mapind].channel == NULL) { |
| 379 | ret = -EINVAL; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 380 | goto error_free_chans; |
| 381 | } |
Lars-Peter Clausen | 1875ffd | 2012-06-04 10:50:03 +0200 | [diff] [blame] | 382 | iio_device_get(chans[mapind].indio_dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 383 | mapind++; |
| 384 | } |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 385 | if (mapind == 0) { |
| 386 | ret = -ENODEV; |
| 387 | goto error_free_chans; |
| 388 | } |
Dan Carpenter | e59b9af | 2012-07-11 07:34:00 +0100 | [diff] [blame] | 389 | mutex_unlock(&iio_map_list_lock); |
| 390 | |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 391 | return chans; |
| 392 | |
| 393 | error_free_chans: |
| 394 | for (i = 0; i < nummaps; i++) |
Lars-Peter Clausen | 1875ffd | 2012-06-04 10:50:03 +0200 | [diff] [blame] | 395 | iio_device_put(chans[i].indio_dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 396 | kfree(chans); |
| 397 | error_ret: |
| 398 | mutex_unlock(&iio_map_list_lock); |
| 399 | |
| 400 | return ERR_PTR(ret); |
| 401 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 402 | EXPORT_SYMBOL_GPL(iio_channel_get_all); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 403 | |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 404 | void iio_channel_release_all(struct iio_channel *channels) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 405 | { |
| 406 | struct iio_channel *chan = &channels[0]; |
| 407 | |
| 408 | while (chan->indio_dev) { |
Lars-Peter Clausen | 1875ffd | 2012-06-04 10:50:03 +0200 | [diff] [blame] | 409 | iio_device_put(chan->indio_dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 410 | chan++; |
| 411 | } |
| 412 | kfree(channels); |
| 413 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 414 | EXPORT_SYMBOL_GPL(iio_channel_release_all); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 415 | |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 416 | static int iio_channel_read(struct iio_channel *chan, int *val, int *val2, |
| 417 | enum iio_chan_info_enum info) |
| 418 | { |
| 419 | int unused; |
| 420 | |
| 421 | if (val2 == NULL) |
| 422 | val2 = &unused; |
| 423 | |
| 424 | return chan->indio_dev->info->read_raw(chan->indio_dev, chan->channel, |
| 425 | val, val2, info); |
| 426 | } |
| 427 | |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 428 | int iio_read_channel_raw(struct iio_channel *chan, int *val) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 429 | { |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 430 | int ret; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 431 | |
| 432 | mutex_lock(&chan->indio_dev->info_exist_lock); |
| 433 | if (chan->indio_dev->info == NULL) { |
| 434 | ret = -ENODEV; |
| 435 | goto err_unlock; |
| 436 | } |
| 437 | |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 438 | ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 439 | err_unlock: |
| 440 | mutex_unlock(&chan->indio_dev->info_exist_lock); |
| 441 | |
| 442 | return ret; |
| 443 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 444 | EXPORT_SYMBOL_GPL(iio_read_channel_raw); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 445 | |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 446 | static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan, |
| 447 | int raw, int *processed, unsigned int scale) |
| 448 | { |
| 449 | int scale_type, scale_val, scale_val2, offset; |
| 450 | s64 raw64 = raw; |
| 451 | int ret; |
| 452 | |
Michael Hennerich | 6c5d4c9 | 2013-06-03 09:04:00 +0100 | [diff] [blame] | 453 | ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_OFFSET); |
Alexandre Belloni | f91d1b6 | 2013-07-01 17:40:00 +0100 | [diff] [blame] | 454 | if (ret >= 0) |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 455 | raw64 += offset; |
| 456 | |
| 457 | scale_type = iio_channel_read(chan, &scale_val, &scale_val2, |
| 458 | IIO_CHAN_INFO_SCALE); |
| 459 | if (scale_type < 0) |
| 460 | return scale_type; |
| 461 | |
| 462 | switch (scale_type) { |
| 463 | case IIO_VAL_INT: |
| 464 | *processed = raw64 * scale_val; |
| 465 | break; |
| 466 | case IIO_VAL_INT_PLUS_MICRO: |
| 467 | if (scale_val2 < 0) |
| 468 | *processed = -raw64 * scale_val; |
| 469 | else |
| 470 | *processed = raw64 * scale_val; |
| 471 | *processed += div_s64(raw64 * (s64)scale_val2 * scale, |
| 472 | 1000000LL); |
| 473 | break; |
| 474 | case IIO_VAL_INT_PLUS_NANO: |
| 475 | if (scale_val2 < 0) |
| 476 | *processed = -raw64 * scale_val; |
| 477 | else |
| 478 | *processed = raw64 * scale_val; |
| 479 | *processed += div_s64(raw64 * (s64)scale_val2 * scale, |
| 480 | 1000000000LL); |
| 481 | break; |
| 482 | case IIO_VAL_FRACTIONAL: |
| 483 | *processed = div_s64(raw64 * (s64)scale_val * scale, |
| 484 | scale_val2); |
| 485 | break; |
Lars-Peter Clausen | 103d9fb | 2012-10-16 17:29:00 +0100 | [diff] [blame] | 486 | case IIO_VAL_FRACTIONAL_LOG2: |
| 487 | *processed = (raw64 * (s64)scale_val * scale) >> scale_val2; |
| 488 | break; |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 489 | default: |
| 490 | return -EINVAL; |
| 491 | } |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | int iio_convert_raw_to_processed(struct iio_channel *chan, int raw, |
| 497 | int *processed, unsigned int scale) |
| 498 | { |
| 499 | int ret; |
| 500 | |
| 501 | mutex_lock(&chan->indio_dev->info_exist_lock); |
| 502 | if (chan->indio_dev->info == NULL) { |
| 503 | ret = -ENODEV; |
| 504 | goto err_unlock; |
| 505 | } |
| 506 | |
| 507 | ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed, |
| 508 | scale); |
| 509 | err_unlock: |
| 510 | mutex_unlock(&chan->indio_dev->info_exist_lock); |
| 511 | |
| 512 | return ret; |
| 513 | } |
| 514 | EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed); |
| 515 | |
| 516 | int iio_read_channel_processed(struct iio_channel *chan, int *val) |
| 517 | { |
| 518 | int ret; |
| 519 | |
| 520 | mutex_lock(&chan->indio_dev->info_exist_lock); |
| 521 | if (chan->indio_dev->info == NULL) { |
| 522 | ret = -ENODEV; |
| 523 | goto err_unlock; |
| 524 | } |
| 525 | |
| 526 | if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) { |
| 527 | ret = iio_channel_read(chan, val, NULL, |
| 528 | IIO_CHAN_INFO_PROCESSED); |
| 529 | } else { |
| 530 | ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW); |
| 531 | if (ret < 0) |
| 532 | goto err_unlock; |
| 533 | ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1); |
| 534 | } |
| 535 | |
| 536 | err_unlock: |
| 537 | mutex_unlock(&chan->indio_dev->info_exist_lock); |
| 538 | |
| 539 | return ret; |
| 540 | } |
| 541 | EXPORT_SYMBOL_GPL(iio_read_channel_processed); |
| 542 | |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 543 | int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 544 | { |
| 545 | int ret; |
| 546 | |
| 547 | mutex_lock(&chan->indio_dev->info_exist_lock); |
| 548 | if (chan->indio_dev->info == NULL) { |
| 549 | ret = -ENODEV; |
| 550 | goto err_unlock; |
| 551 | } |
| 552 | |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 553 | ret = iio_channel_read(chan, val, val2, IIO_CHAN_INFO_SCALE); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 554 | err_unlock: |
| 555 | mutex_unlock(&chan->indio_dev->info_exist_lock); |
| 556 | |
| 557 | return ret; |
| 558 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 559 | EXPORT_SYMBOL_GPL(iio_read_channel_scale); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 560 | |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 561 | int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 562 | { |
| 563 | int ret = 0; |
| 564 | /* Need to verify underlying driver has not gone away */ |
| 565 | |
| 566 | mutex_lock(&chan->indio_dev->info_exist_lock); |
| 567 | if (chan->indio_dev->info == NULL) { |
| 568 | ret = -ENODEV; |
| 569 | goto err_unlock; |
| 570 | } |
| 571 | |
| 572 | *type = chan->channel->type; |
| 573 | err_unlock: |
| 574 | mutex_unlock(&chan->indio_dev->info_exist_lock); |
| 575 | |
| 576 | return ret; |
| 577 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 578 | EXPORT_SYMBOL_GPL(iio_get_channel_type); |