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); |
Johannes Pointner | 872687f | 2014-08-25 09:04:00 +0100 | [diff] [blame] | 181 | if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER) |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 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); |
Adam Thomson | a2c1249 | 2014-11-06 12:11:00 +0000 | [diff] [blame] | 186 | return NULL; |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 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)) |
Adam Thomson | a2c1249 | 2014-11-06 12:11:00 +0000 | [diff] [blame] | 196 | return NULL; |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 197 | } |
Adam Thomson | a2c1249 | 2014-11-06 12:11:00 +0000 | [diff] [blame] | 198 | |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 199 | return chan; |
| 200 | } |
| 201 | |
| 202 | static struct iio_channel *of_iio_channel_get_all(struct device *dev) |
| 203 | { |
| 204 | struct iio_channel *chans; |
| 205 | int i, mapind, nummaps = 0; |
| 206 | int ret; |
| 207 | |
| 208 | do { |
| 209 | ret = of_parse_phandle_with_args(dev->of_node, |
| 210 | "io-channels", |
| 211 | "#io-channel-cells", |
| 212 | nummaps, NULL); |
| 213 | if (ret < 0) |
| 214 | break; |
| 215 | } while (++nummaps); |
| 216 | |
| 217 | if (nummaps == 0) /* no error, return NULL to search map table */ |
| 218 | return NULL; |
| 219 | |
| 220 | /* NULL terminated array to save passing size */ |
| 221 | chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL); |
| 222 | if (chans == NULL) |
| 223 | return ERR_PTR(-ENOMEM); |
| 224 | |
| 225 | /* Search for OF matches */ |
| 226 | for (mapind = 0; mapind < nummaps; mapind++) { |
| 227 | ret = __of_iio_channel_get(&chans[mapind], dev->of_node, |
| 228 | mapind); |
| 229 | if (ret) |
| 230 | goto error_free_chans; |
| 231 | } |
| 232 | return chans; |
| 233 | |
| 234 | error_free_chans: |
| 235 | for (i = 0; i < mapind; i++) |
| 236 | iio_device_put(chans[i].indio_dev); |
| 237 | kfree(chans); |
| 238 | return ERR_PTR(ret); |
| 239 | } |
| 240 | |
| 241 | #else /* CONFIG_OF */ |
| 242 | |
| 243 | static inline struct iio_channel * |
| 244 | of_iio_channel_get_by_name(struct device_node *np, const char *name) |
| 245 | { |
| 246 | return NULL; |
| 247 | } |
| 248 | |
| 249 | static inline struct iio_channel *of_iio_channel_get_all(struct device *dev) |
| 250 | { |
| 251 | return NULL; |
| 252 | } |
| 253 | |
| 254 | #endif /* CONFIG_OF */ |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 255 | |
Guenter Roeck | 5aa57f0 | 2013-02-04 20:26:00 +0000 | [diff] [blame] | 256 | static struct iio_channel *iio_channel_get_sys(const char *name, |
| 257 | const char *channel_name) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 258 | { |
| 259 | struct iio_map_internal *c_i = NULL, *c = NULL; |
| 260 | struct iio_channel *channel; |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 261 | int err; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 262 | |
| 263 | if (name == NULL && channel_name == NULL) |
| 264 | return ERR_PTR(-ENODEV); |
| 265 | |
| 266 | /* first find matching entry the channel map */ |
| 267 | mutex_lock(&iio_map_list_lock); |
| 268 | list_for_each_entry(c_i, &iio_map_list, l) { |
| 269 | if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) || |
| 270 | (channel_name && |
| 271 | strcmp(channel_name, c_i->map->consumer_channel) != 0)) |
| 272 | continue; |
| 273 | c = c_i; |
Lars-Peter Clausen | 1875ffd | 2012-06-04 10:50:03 +0200 | [diff] [blame] | 274 | iio_device_get(c->indio_dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 275 | break; |
| 276 | } |
| 277 | mutex_unlock(&iio_map_list_lock); |
| 278 | if (c == NULL) |
| 279 | return ERR_PTR(-ENODEV); |
| 280 | |
Kim, Milo | 2cc412b | 2012-09-14 02:24:00 +0100 | [diff] [blame] | 281 | channel = kzalloc(sizeof(*channel), GFP_KERNEL); |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 282 | if (channel == NULL) { |
| 283 | err = -ENOMEM; |
Kim, Milo | 801c4b5 | 2012-09-18 05:55:00 +0100 | [diff] [blame] | 284 | goto error_no_mem; |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 285 | } |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 286 | |
| 287 | channel->indio_dev = c->indio_dev; |
| 288 | |
Kim, Milo | b2b79ff | 2012-09-17 09:44:00 +0100 | [diff] [blame] | 289 | if (c->map->adc_channel_label) { |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 290 | channel->channel = |
| 291 | iio_chan_spec_from_name(channel->indio_dev, |
| 292 | c->map->adc_channel_label); |
| 293 | |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 294 | if (channel->channel == NULL) { |
| 295 | err = -EINVAL; |
Kim, Milo | b2b79ff | 2012-09-17 09:44:00 +0100 | [diff] [blame] | 296 | goto error_no_chan; |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 297 | } |
Kim, Milo | b2b79ff | 2012-09-17 09:44:00 +0100 | [diff] [blame] | 298 | } |
| 299 | |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 300 | return channel; |
Kim, Milo | b2b79ff | 2012-09-17 09:44:00 +0100 | [diff] [blame] | 301 | |
| 302 | error_no_chan: |
Kim, Milo | b2b79ff | 2012-09-17 09:44:00 +0100 | [diff] [blame] | 303 | kfree(channel); |
Kim, Milo | 801c4b5 | 2012-09-18 05:55:00 +0100 | [diff] [blame] | 304 | error_no_mem: |
| 305 | iio_device_put(c->indio_dev); |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 306 | return ERR_PTR(err); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 307 | } |
Guenter Roeck | 5aa57f0 | 2013-02-04 20:26:00 +0000 | [diff] [blame] | 308 | |
| 309 | struct iio_channel *iio_channel_get(struct device *dev, |
| 310 | const char *channel_name) |
| 311 | { |
| 312 | const char *name = dev ? dev_name(dev) : NULL; |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 313 | struct iio_channel *channel; |
Guenter Roeck | 5aa57f0 | 2013-02-04 20:26:00 +0000 | [diff] [blame] | 314 | |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 315 | if (dev) { |
| 316 | channel = of_iio_channel_get_by_name(dev->of_node, |
| 317 | channel_name); |
| 318 | if (channel != NULL) |
| 319 | return channel; |
| 320 | } |
Adam Thomson | a2c1249 | 2014-11-06 12:11:00 +0000 | [diff] [blame] | 321 | |
Guenter Roeck | 5aa57f0 | 2013-02-04 20:26:00 +0000 | [diff] [blame] | 322 | return iio_channel_get_sys(name, channel_name); |
| 323 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 324 | EXPORT_SYMBOL_GPL(iio_channel_get); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 325 | |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 326 | void iio_channel_release(struct iio_channel *channel) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 327 | { |
Lars-Peter Clausen | 1875ffd | 2012-06-04 10:50:03 +0200 | [diff] [blame] | 328 | iio_device_put(channel->indio_dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 329 | kfree(channel); |
| 330 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 331 | EXPORT_SYMBOL_GPL(iio_channel_release); |
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 | struct iio_channel *iio_channel_get_all(struct device *dev) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 334 | { |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 335 | const char *name; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 336 | struct iio_channel *chans; |
| 337 | struct iio_map_internal *c = NULL; |
| 338 | int nummaps = 0; |
| 339 | int mapind = 0; |
| 340 | int i, ret; |
| 341 | |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 342 | if (dev == NULL) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 343 | return ERR_PTR(-EINVAL); |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 344 | |
| 345 | chans = of_iio_channel_get_all(dev); |
| 346 | if (chans) |
| 347 | return chans; |
| 348 | |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 349 | name = dev_name(dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 350 | |
| 351 | mutex_lock(&iio_map_list_lock); |
| 352 | /* first count the matching maps */ |
| 353 | list_for_each_entry(c, &iio_map_list, l) |
| 354 | if (name && strcmp(name, c->map->consumer_dev_name) != 0) |
| 355 | continue; |
| 356 | else |
| 357 | nummaps++; |
| 358 | |
| 359 | if (nummaps == 0) { |
| 360 | ret = -ENODEV; |
| 361 | goto error_ret; |
| 362 | } |
| 363 | |
| 364 | /* NULL terminated array to save passing size */ |
| 365 | chans = kzalloc(sizeof(*chans)*(nummaps + 1), GFP_KERNEL); |
| 366 | if (chans == NULL) { |
| 367 | ret = -ENOMEM; |
| 368 | goto error_ret; |
| 369 | } |
| 370 | |
| 371 | /* for each map fill in the chans element */ |
| 372 | list_for_each_entry(c, &iio_map_list, l) { |
| 373 | if (name && strcmp(name, c->map->consumer_dev_name) != 0) |
| 374 | continue; |
| 375 | chans[mapind].indio_dev = c->indio_dev; |
Jonathan Cameron | 0464415 | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 376 | chans[mapind].data = c->map->consumer_data; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 377 | chans[mapind].channel = |
| 378 | iio_chan_spec_from_name(chans[mapind].indio_dev, |
| 379 | c->map->adc_channel_label); |
| 380 | if (chans[mapind].channel == NULL) { |
| 381 | ret = -EINVAL; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 382 | goto error_free_chans; |
| 383 | } |
Lars-Peter Clausen | 1875ffd | 2012-06-04 10:50:03 +0200 | [diff] [blame] | 384 | iio_device_get(chans[mapind].indio_dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 385 | mapind++; |
| 386 | } |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 387 | if (mapind == 0) { |
| 388 | ret = -ENODEV; |
| 389 | goto error_free_chans; |
| 390 | } |
Dan Carpenter | e59b9af | 2012-07-11 07:34:00 +0100 | [diff] [blame] | 391 | mutex_unlock(&iio_map_list_lock); |
| 392 | |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 393 | return chans; |
| 394 | |
| 395 | error_free_chans: |
| 396 | for (i = 0; i < nummaps; i++) |
Lars-Peter Clausen | 1875ffd | 2012-06-04 10:50:03 +0200 | [diff] [blame] | 397 | iio_device_put(chans[i].indio_dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 398 | kfree(chans); |
| 399 | error_ret: |
| 400 | mutex_unlock(&iio_map_list_lock); |
| 401 | |
| 402 | return ERR_PTR(ret); |
| 403 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 404 | EXPORT_SYMBOL_GPL(iio_channel_get_all); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 405 | |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 406 | void iio_channel_release_all(struct iio_channel *channels) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 407 | { |
| 408 | struct iio_channel *chan = &channels[0]; |
| 409 | |
| 410 | while (chan->indio_dev) { |
Lars-Peter Clausen | 1875ffd | 2012-06-04 10:50:03 +0200 | [diff] [blame] | 411 | iio_device_put(chan->indio_dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 412 | chan++; |
| 413 | } |
| 414 | kfree(channels); |
| 415 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 416 | EXPORT_SYMBOL_GPL(iio_channel_release_all); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 417 | |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 418 | static int iio_channel_read(struct iio_channel *chan, int *val, int *val2, |
| 419 | enum iio_chan_info_enum info) |
| 420 | { |
| 421 | int unused; |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 422 | int vals[INDIO_MAX_RAW_ELEMENTS]; |
| 423 | int ret; |
| 424 | int val_len = 2; |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 425 | |
| 426 | if (val2 == NULL) |
| 427 | val2 = &unused; |
| 428 | |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 429 | if (chan->indio_dev->info->read_raw_multi) { |
| 430 | ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev, |
| 431 | chan->channel, INDIO_MAX_RAW_ELEMENTS, |
| 432 | vals, &val_len, info); |
| 433 | *val = vals[0]; |
| 434 | *val2 = vals[1]; |
| 435 | } else |
| 436 | ret = chan->indio_dev->info->read_raw(chan->indio_dev, |
| 437 | chan->channel, val, val2, info); |
| 438 | |
| 439 | return ret; |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 442 | int iio_read_channel_raw(struct iio_channel *chan, int *val) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 443 | { |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 444 | int ret; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 445 | |
| 446 | mutex_lock(&chan->indio_dev->info_exist_lock); |
| 447 | if (chan->indio_dev->info == NULL) { |
| 448 | ret = -ENODEV; |
| 449 | goto err_unlock; |
| 450 | } |
| 451 | |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 452 | ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 453 | err_unlock: |
| 454 | mutex_unlock(&chan->indio_dev->info_exist_lock); |
| 455 | |
| 456 | return ret; |
| 457 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 458 | EXPORT_SYMBOL_GPL(iio_read_channel_raw); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 459 | |
Sebastian Reichel | 476d4af | 2014-10-03 17:25:00 +0100 | [diff] [blame] | 460 | int iio_read_channel_average_raw(struct iio_channel *chan, int *val) |
| 461 | { |
| 462 | int ret; |
| 463 | |
| 464 | mutex_lock(&chan->indio_dev->info_exist_lock); |
| 465 | if (chan->indio_dev->info == NULL) { |
| 466 | ret = -ENODEV; |
| 467 | goto err_unlock; |
| 468 | } |
| 469 | |
| 470 | ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW); |
| 471 | err_unlock: |
| 472 | mutex_unlock(&chan->indio_dev->info_exist_lock); |
| 473 | |
| 474 | return ret; |
| 475 | } |
| 476 | EXPORT_SYMBOL_GPL(iio_read_channel_average_raw); |
| 477 | |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 478 | static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan, |
| 479 | int raw, int *processed, unsigned int scale) |
| 480 | { |
| 481 | int scale_type, scale_val, scale_val2, offset; |
| 482 | s64 raw64 = raw; |
| 483 | int ret; |
| 484 | |
Michael Hennerich | 6c5d4c9 | 2013-06-03 09:04:00 +0100 | [diff] [blame] | 485 | ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_OFFSET); |
Alexandre Belloni | f91d1b6 | 2013-07-01 17:40:00 +0100 | [diff] [blame] | 486 | if (ret >= 0) |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 487 | raw64 += offset; |
| 488 | |
| 489 | scale_type = iio_channel_read(chan, &scale_val, &scale_val2, |
| 490 | IIO_CHAN_INFO_SCALE); |
| 491 | if (scale_type < 0) |
| 492 | return scale_type; |
| 493 | |
| 494 | switch (scale_type) { |
| 495 | case IIO_VAL_INT: |
| 496 | *processed = raw64 * scale_val; |
| 497 | break; |
| 498 | case IIO_VAL_INT_PLUS_MICRO: |
| 499 | if (scale_val2 < 0) |
| 500 | *processed = -raw64 * scale_val; |
| 501 | else |
| 502 | *processed = raw64 * scale_val; |
| 503 | *processed += div_s64(raw64 * (s64)scale_val2 * scale, |
| 504 | 1000000LL); |
| 505 | break; |
| 506 | case IIO_VAL_INT_PLUS_NANO: |
| 507 | if (scale_val2 < 0) |
| 508 | *processed = -raw64 * scale_val; |
| 509 | else |
| 510 | *processed = raw64 * scale_val; |
| 511 | *processed += div_s64(raw64 * (s64)scale_val2 * scale, |
| 512 | 1000000000LL); |
| 513 | break; |
| 514 | case IIO_VAL_FRACTIONAL: |
| 515 | *processed = div_s64(raw64 * (s64)scale_val * scale, |
| 516 | scale_val2); |
| 517 | break; |
Lars-Peter Clausen | 103d9fb | 2012-10-16 17:29:00 +0100 | [diff] [blame] | 518 | case IIO_VAL_FRACTIONAL_LOG2: |
| 519 | *processed = (raw64 * (s64)scale_val * scale) >> scale_val2; |
| 520 | break; |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 521 | default: |
| 522 | return -EINVAL; |
| 523 | } |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | int iio_convert_raw_to_processed(struct iio_channel *chan, int raw, |
| 529 | int *processed, unsigned int scale) |
| 530 | { |
| 531 | int ret; |
| 532 | |
| 533 | mutex_lock(&chan->indio_dev->info_exist_lock); |
| 534 | if (chan->indio_dev->info == NULL) { |
| 535 | ret = -ENODEV; |
| 536 | goto err_unlock; |
| 537 | } |
| 538 | |
| 539 | ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed, |
| 540 | scale); |
| 541 | err_unlock: |
| 542 | mutex_unlock(&chan->indio_dev->info_exist_lock); |
| 543 | |
| 544 | return ret; |
| 545 | } |
| 546 | EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed); |
| 547 | |
| 548 | int iio_read_channel_processed(struct iio_channel *chan, int *val) |
| 549 | { |
| 550 | int ret; |
| 551 | |
| 552 | mutex_lock(&chan->indio_dev->info_exist_lock); |
| 553 | if (chan->indio_dev->info == NULL) { |
| 554 | ret = -ENODEV; |
| 555 | goto err_unlock; |
| 556 | } |
| 557 | |
| 558 | if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) { |
| 559 | ret = iio_channel_read(chan, val, NULL, |
| 560 | IIO_CHAN_INFO_PROCESSED); |
| 561 | } else { |
| 562 | ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW); |
| 563 | if (ret < 0) |
| 564 | goto err_unlock; |
| 565 | ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1); |
| 566 | } |
| 567 | |
| 568 | err_unlock: |
| 569 | mutex_unlock(&chan->indio_dev->info_exist_lock); |
| 570 | |
| 571 | return ret; |
| 572 | } |
| 573 | EXPORT_SYMBOL_GPL(iio_read_channel_processed); |
| 574 | |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 575 | 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] | 576 | { |
| 577 | int ret; |
| 578 | |
| 579 | mutex_lock(&chan->indio_dev->info_exist_lock); |
| 580 | if (chan->indio_dev->info == NULL) { |
| 581 | ret = -ENODEV; |
| 582 | goto err_unlock; |
| 583 | } |
| 584 | |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 585 | ret = iio_channel_read(chan, val, val2, IIO_CHAN_INFO_SCALE); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 586 | err_unlock: |
| 587 | mutex_unlock(&chan->indio_dev->info_exist_lock); |
| 588 | |
| 589 | return ret; |
| 590 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 591 | EXPORT_SYMBOL_GPL(iio_read_channel_scale); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 592 | |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 593 | 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] | 594 | { |
| 595 | int ret = 0; |
| 596 | /* Need to verify underlying driver has not gone away */ |
| 597 | |
| 598 | mutex_lock(&chan->indio_dev->info_exist_lock); |
| 599 | if (chan->indio_dev->info == NULL) { |
| 600 | ret = -ENODEV; |
| 601 | goto err_unlock; |
| 602 | } |
| 603 | |
| 604 | *type = chan->channel->type; |
| 605 | err_unlock: |
| 606 | mutex_unlock(&chan->indio_dev->info_exist_lock); |
| 607 | |
| 608 | return ret; |
| 609 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 610 | EXPORT_SYMBOL_GPL(iio_get_channel_type); |