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 | |
Ivan T. Ivanov | acd8256 | 2014-10-22 18:29:43 +0300 | [diff] [blame] | 103 | /** |
| 104 | * __of_iio_simple_xlate - translate iiospec to the IIO channel index |
| 105 | * @indio_dev: pointer to the iio_dev structure |
| 106 | * @iiospec: IIO specifier as found in the device tree |
| 107 | * |
| 108 | * This is simple translation function, suitable for the most 1:1 mapped |
| 109 | * channels in IIO chips. This function performs only one sanity check: |
| 110 | * whether IIO index is less than num_channels (that is specified in the |
| 111 | * iio_dev). |
| 112 | */ |
| 113 | static int __of_iio_simple_xlate(struct iio_dev *indio_dev, |
| 114 | const struct of_phandle_args *iiospec) |
| 115 | { |
| 116 | if (!iiospec->args_count) |
| 117 | return 0; |
| 118 | |
| 119 | if (iiospec->args[0] >= indio_dev->num_channels) |
| 120 | return -EINVAL; |
| 121 | |
| 122 | return iiospec->args[0]; |
| 123 | } |
| 124 | |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 125 | static int __of_iio_channel_get(struct iio_channel *channel, |
| 126 | struct device_node *np, int index) |
| 127 | { |
| 128 | struct device *idev; |
| 129 | struct iio_dev *indio_dev; |
| 130 | int err; |
| 131 | struct of_phandle_args iiospec; |
| 132 | |
| 133 | err = of_parse_phandle_with_args(np, "io-channels", |
| 134 | "#io-channel-cells", |
| 135 | index, &iiospec); |
| 136 | if (err) |
| 137 | return err; |
| 138 | |
| 139 | idev = bus_find_device(&iio_bus_type, NULL, iiospec.np, |
| 140 | iio_dev_node_match); |
| 141 | of_node_put(iiospec.np); |
| 142 | if (idev == NULL) |
| 143 | return -EPROBE_DEFER; |
| 144 | |
| 145 | indio_dev = dev_to_iio_dev(idev); |
| 146 | channel->indio_dev = indio_dev; |
Ivan T. Ivanov | acd8256 | 2014-10-22 18:29:43 +0300 | [diff] [blame] | 147 | if (indio_dev->info->of_xlate) |
| 148 | index = indio_dev->info->of_xlate(indio_dev, &iiospec); |
| 149 | else |
| 150 | index = __of_iio_simple_xlate(indio_dev, &iiospec); |
| 151 | if (index < 0) |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 152 | goto err_put; |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 153 | channel->channel = &indio_dev->channels[index]; |
| 154 | |
| 155 | return 0; |
| 156 | |
| 157 | err_put: |
| 158 | iio_device_put(indio_dev); |
Ivan T. Ivanov | acd8256 | 2014-10-22 18:29:43 +0300 | [diff] [blame] | 159 | return index; |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static struct iio_channel *of_iio_channel_get(struct device_node *np, int index) |
| 163 | { |
| 164 | struct iio_channel *channel; |
| 165 | int err; |
| 166 | |
| 167 | if (index < 0) |
| 168 | return ERR_PTR(-EINVAL); |
| 169 | |
| 170 | channel = kzalloc(sizeof(*channel), GFP_KERNEL); |
| 171 | if (channel == NULL) |
| 172 | return ERR_PTR(-ENOMEM); |
| 173 | |
| 174 | err = __of_iio_channel_get(channel, np, index); |
| 175 | if (err) |
| 176 | goto err_free_channel; |
| 177 | |
| 178 | return channel; |
| 179 | |
| 180 | err_free_channel: |
| 181 | kfree(channel); |
| 182 | return ERR_PTR(err); |
| 183 | } |
| 184 | |
| 185 | static struct iio_channel *of_iio_channel_get_by_name(struct device_node *np, |
| 186 | const char *name) |
| 187 | { |
| 188 | struct iio_channel *chan = NULL; |
| 189 | |
| 190 | /* Walk up the tree of devices looking for a matching iio channel */ |
| 191 | while (np) { |
| 192 | int index = 0; |
| 193 | |
| 194 | /* |
| 195 | * For named iio channels, first look up the name in the |
| 196 | * "io-channel-names" property. If it cannot be found, the |
| 197 | * index will be an error code, and of_iio_channel_get() |
| 198 | * will fail. |
| 199 | */ |
| 200 | if (name) |
| 201 | index = of_property_match_string(np, "io-channel-names", |
| 202 | name); |
| 203 | chan = of_iio_channel_get(np, index); |
Johannes Pointner | 872687f | 2014-08-25 09:04:00 +0100 | [diff] [blame] | 204 | if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER) |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 205 | break; |
| 206 | else if (name && index >= 0) { |
| 207 | pr_err("ERROR: could not get IIO channel %s:%s(%i)\n", |
| 208 | np->full_name, name ? name : "", index); |
Adam Thomson | a2c1249 | 2014-11-06 12:11:00 +0000 | [diff] [blame] | 209 | return NULL; |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* |
| 213 | * No matching IIO channel found on this node. |
| 214 | * If the parent node has a "io-channel-ranges" property, |
| 215 | * then we can try one of its channels. |
| 216 | */ |
| 217 | np = np->parent; |
| 218 | if (np && !of_get_property(np, "io-channel-ranges", NULL)) |
Adam Thomson | a2c1249 | 2014-11-06 12:11:00 +0000 | [diff] [blame] | 219 | return NULL; |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 220 | } |
Adam Thomson | a2c1249 | 2014-11-06 12:11:00 +0000 | [diff] [blame] | 221 | |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 222 | return chan; |
| 223 | } |
| 224 | |
| 225 | static struct iio_channel *of_iio_channel_get_all(struct device *dev) |
| 226 | { |
| 227 | struct iio_channel *chans; |
| 228 | int i, mapind, nummaps = 0; |
| 229 | int ret; |
| 230 | |
| 231 | do { |
| 232 | ret = of_parse_phandle_with_args(dev->of_node, |
| 233 | "io-channels", |
| 234 | "#io-channel-cells", |
| 235 | nummaps, NULL); |
| 236 | if (ret < 0) |
| 237 | break; |
| 238 | } while (++nummaps); |
| 239 | |
| 240 | if (nummaps == 0) /* no error, return NULL to search map table */ |
| 241 | return NULL; |
| 242 | |
| 243 | /* NULL terminated array to save passing size */ |
| 244 | chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL); |
| 245 | if (chans == NULL) |
| 246 | return ERR_PTR(-ENOMEM); |
| 247 | |
| 248 | /* Search for OF matches */ |
| 249 | for (mapind = 0; mapind < nummaps; mapind++) { |
| 250 | ret = __of_iio_channel_get(&chans[mapind], dev->of_node, |
| 251 | mapind); |
| 252 | if (ret) |
| 253 | goto error_free_chans; |
| 254 | } |
| 255 | return chans; |
| 256 | |
| 257 | error_free_chans: |
| 258 | for (i = 0; i < mapind; i++) |
| 259 | iio_device_put(chans[i].indio_dev); |
| 260 | kfree(chans); |
| 261 | return ERR_PTR(ret); |
| 262 | } |
| 263 | |
| 264 | #else /* CONFIG_OF */ |
| 265 | |
| 266 | static inline struct iio_channel * |
| 267 | of_iio_channel_get_by_name(struct device_node *np, const char *name) |
| 268 | { |
| 269 | return NULL; |
| 270 | } |
| 271 | |
| 272 | static inline struct iio_channel *of_iio_channel_get_all(struct device *dev) |
| 273 | { |
| 274 | return NULL; |
| 275 | } |
| 276 | |
| 277 | #endif /* CONFIG_OF */ |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 278 | |
Guenter Roeck | 5aa57f0 | 2013-02-04 20:26:00 +0000 | [diff] [blame] | 279 | static struct iio_channel *iio_channel_get_sys(const char *name, |
| 280 | const char *channel_name) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 281 | { |
| 282 | struct iio_map_internal *c_i = NULL, *c = NULL; |
| 283 | struct iio_channel *channel; |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 284 | int err; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 285 | |
| 286 | if (name == NULL && channel_name == NULL) |
| 287 | return ERR_PTR(-ENODEV); |
| 288 | |
| 289 | /* first find matching entry the channel map */ |
| 290 | mutex_lock(&iio_map_list_lock); |
| 291 | list_for_each_entry(c_i, &iio_map_list, l) { |
| 292 | if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) || |
| 293 | (channel_name && |
| 294 | strcmp(channel_name, c_i->map->consumer_channel) != 0)) |
| 295 | continue; |
| 296 | c = c_i; |
Lars-Peter Clausen | 1875ffd | 2012-06-04 10:50:03 +0200 | [diff] [blame] | 297 | iio_device_get(c->indio_dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 298 | break; |
| 299 | } |
| 300 | mutex_unlock(&iio_map_list_lock); |
| 301 | if (c == NULL) |
| 302 | return ERR_PTR(-ENODEV); |
| 303 | |
Kim, Milo | 2cc412b | 2012-09-14 02:24:00 +0100 | [diff] [blame] | 304 | channel = kzalloc(sizeof(*channel), GFP_KERNEL); |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 305 | if (channel == NULL) { |
| 306 | err = -ENOMEM; |
Kim, Milo | 801c4b5 | 2012-09-18 05:55:00 +0100 | [diff] [blame] | 307 | goto error_no_mem; |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 308 | } |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 309 | |
| 310 | channel->indio_dev = c->indio_dev; |
| 311 | |
Kim, Milo | b2b79ff | 2012-09-17 09:44:00 +0100 | [diff] [blame] | 312 | if (c->map->adc_channel_label) { |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 313 | channel->channel = |
| 314 | iio_chan_spec_from_name(channel->indio_dev, |
| 315 | c->map->adc_channel_label); |
| 316 | |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 317 | if (channel->channel == NULL) { |
| 318 | err = -EINVAL; |
Kim, Milo | b2b79ff | 2012-09-17 09:44:00 +0100 | [diff] [blame] | 319 | goto error_no_chan; |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 320 | } |
Kim, Milo | b2b79ff | 2012-09-17 09:44:00 +0100 | [diff] [blame] | 321 | } |
| 322 | |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 323 | return channel; |
Kim, Milo | b2b79ff | 2012-09-17 09:44:00 +0100 | [diff] [blame] | 324 | |
| 325 | error_no_chan: |
Kim, Milo | b2b79ff | 2012-09-17 09:44:00 +0100 | [diff] [blame] | 326 | kfree(channel); |
Kim, Milo | 801c4b5 | 2012-09-18 05:55:00 +0100 | [diff] [blame] | 327 | error_no_mem: |
| 328 | iio_device_put(c->indio_dev); |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 329 | return ERR_PTR(err); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 330 | } |
Guenter Roeck | 5aa57f0 | 2013-02-04 20:26:00 +0000 | [diff] [blame] | 331 | |
| 332 | struct iio_channel *iio_channel_get(struct device *dev, |
| 333 | const char *channel_name) |
| 334 | { |
| 335 | const char *name = dev ? dev_name(dev) : NULL; |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 336 | struct iio_channel *channel; |
Guenter Roeck | 5aa57f0 | 2013-02-04 20:26:00 +0000 | [diff] [blame] | 337 | |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 338 | if (dev) { |
| 339 | channel = of_iio_channel_get_by_name(dev->of_node, |
| 340 | channel_name); |
| 341 | if (channel != NULL) |
| 342 | return channel; |
| 343 | } |
Adam Thomson | a2c1249 | 2014-11-06 12:11:00 +0000 | [diff] [blame] | 344 | |
Guenter Roeck | 5aa57f0 | 2013-02-04 20:26:00 +0000 | [diff] [blame] | 345 | return iio_channel_get_sys(name, channel_name); |
| 346 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 347 | EXPORT_SYMBOL_GPL(iio_channel_get); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 348 | |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 349 | void iio_channel_release(struct iio_channel *channel) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 350 | { |
Lars-Peter Clausen | 1875ffd | 2012-06-04 10:50:03 +0200 | [diff] [blame] | 351 | iio_device_put(channel->indio_dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 352 | kfree(channel); |
| 353 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 354 | EXPORT_SYMBOL_GPL(iio_channel_release); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 355 | |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 356 | struct iio_channel *iio_channel_get_all(struct device *dev) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 357 | { |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 358 | const char *name; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 359 | struct iio_channel *chans; |
| 360 | struct iio_map_internal *c = NULL; |
| 361 | int nummaps = 0; |
| 362 | int mapind = 0; |
| 363 | int i, ret; |
| 364 | |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 365 | if (dev == NULL) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 366 | return ERR_PTR(-EINVAL); |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 367 | |
| 368 | chans = of_iio_channel_get_all(dev); |
| 369 | if (chans) |
| 370 | return chans; |
| 371 | |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 372 | name = dev_name(dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 373 | |
| 374 | mutex_lock(&iio_map_list_lock); |
| 375 | /* first count the matching maps */ |
| 376 | list_for_each_entry(c, &iio_map_list, l) |
| 377 | if (name && strcmp(name, c->map->consumer_dev_name) != 0) |
| 378 | continue; |
| 379 | else |
| 380 | nummaps++; |
| 381 | |
| 382 | if (nummaps == 0) { |
| 383 | ret = -ENODEV; |
| 384 | goto error_ret; |
| 385 | } |
| 386 | |
| 387 | /* NULL terminated array to save passing size */ |
| 388 | chans = kzalloc(sizeof(*chans)*(nummaps + 1), GFP_KERNEL); |
| 389 | if (chans == NULL) { |
| 390 | ret = -ENOMEM; |
| 391 | goto error_ret; |
| 392 | } |
| 393 | |
| 394 | /* for each map fill in the chans element */ |
| 395 | list_for_each_entry(c, &iio_map_list, l) { |
| 396 | if (name && strcmp(name, c->map->consumer_dev_name) != 0) |
| 397 | continue; |
| 398 | chans[mapind].indio_dev = c->indio_dev; |
Jonathan Cameron | 0464415 | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 399 | chans[mapind].data = c->map->consumer_data; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 400 | chans[mapind].channel = |
| 401 | iio_chan_spec_from_name(chans[mapind].indio_dev, |
| 402 | c->map->adc_channel_label); |
| 403 | if (chans[mapind].channel == NULL) { |
| 404 | ret = -EINVAL; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 405 | goto error_free_chans; |
| 406 | } |
Lars-Peter Clausen | 1875ffd | 2012-06-04 10:50:03 +0200 | [diff] [blame] | 407 | iio_device_get(chans[mapind].indio_dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 408 | mapind++; |
| 409 | } |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 410 | if (mapind == 0) { |
| 411 | ret = -ENODEV; |
| 412 | goto error_free_chans; |
| 413 | } |
Dan Carpenter | e59b9af | 2012-07-11 07:34:00 +0100 | [diff] [blame] | 414 | mutex_unlock(&iio_map_list_lock); |
| 415 | |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 416 | return chans; |
| 417 | |
| 418 | error_free_chans: |
| 419 | for (i = 0; i < nummaps; i++) |
Lars-Peter Clausen | 1875ffd | 2012-06-04 10:50:03 +0200 | [diff] [blame] | 420 | iio_device_put(chans[i].indio_dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 421 | kfree(chans); |
| 422 | error_ret: |
| 423 | mutex_unlock(&iio_map_list_lock); |
| 424 | |
| 425 | return ERR_PTR(ret); |
| 426 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 427 | EXPORT_SYMBOL_GPL(iio_channel_get_all); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 428 | |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 429 | void iio_channel_release_all(struct iio_channel *channels) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 430 | { |
| 431 | struct iio_channel *chan = &channels[0]; |
| 432 | |
| 433 | while (chan->indio_dev) { |
Lars-Peter Clausen | 1875ffd | 2012-06-04 10:50:03 +0200 | [diff] [blame] | 434 | iio_device_put(chan->indio_dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 435 | chan++; |
| 436 | } |
| 437 | kfree(channels); |
| 438 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 439 | EXPORT_SYMBOL_GPL(iio_channel_release_all); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 440 | |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 441 | static int iio_channel_read(struct iio_channel *chan, int *val, int *val2, |
| 442 | enum iio_chan_info_enum info) |
| 443 | { |
| 444 | int unused; |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 445 | int vals[INDIO_MAX_RAW_ELEMENTS]; |
| 446 | int ret; |
| 447 | int val_len = 2; |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 448 | |
| 449 | if (val2 == NULL) |
| 450 | val2 = &unused; |
| 451 | |
Fabien Proriol | 65de765 | 2015-01-01 12:46:48 +0000 | [diff] [blame] | 452 | if(!iio_channel_has_info(chan->channel, info)) |
| 453 | return -EINVAL; |
| 454 | |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 455 | if (chan->indio_dev->info->read_raw_multi) { |
| 456 | ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev, |
| 457 | chan->channel, INDIO_MAX_RAW_ELEMENTS, |
| 458 | vals, &val_len, info); |
| 459 | *val = vals[0]; |
| 460 | *val2 = vals[1]; |
| 461 | } else |
| 462 | ret = chan->indio_dev->info->read_raw(chan->indio_dev, |
| 463 | chan->channel, val, val2, info); |
| 464 | |
| 465 | return ret; |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 466 | } |
| 467 | |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 468 | int iio_read_channel_raw(struct iio_channel *chan, int *val) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 469 | { |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 470 | int ret; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 471 | |
| 472 | mutex_lock(&chan->indio_dev->info_exist_lock); |
| 473 | if (chan->indio_dev->info == NULL) { |
| 474 | ret = -ENODEV; |
| 475 | goto err_unlock; |
| 476 | } |
| 477 | |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 478 | ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 479 | err_unlock: |
| 480 | mutex_unlock(&chan->indio_dev->info_exist_lock); |
| 481 | |
| 482 | return ret; |
| 483 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 484 | EXPORT_SYMBOL_GPL(iio_read_channel_raw); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 485 | |
Sebastian Reichel | 476d4af | 2014-10-03 17:25:00 +0100 | [diff] [blame] | 486 | int iio_read_channel_average_raw(struct iio_channel *chan, int *val) |
| 487 | { |
| 488 | int ret; |
| 489 | |
| 490 | mutex_lock(&chan->indio_dev->info_exist_lock); |
| 491 | if (chan->indio_dev->info == NULL) { |
| 492 | ret = -ENODEV; |
| 493 | goto err_unlock; |
| 494 | } |
| 495 | |
| 496 | ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW); |
| 497 | err_unlock: |
| 498 | mutex_unlock(&chan->indio_dev->info_exist_lock); |
| 499 | |
| 500 | return ret; |
| 501 | } |
| 502 | EXPORT_SYMBOL_GPL(iio_read_channel_average_raw); |
| 503 | |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 504 | static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan, |
| 505 | int raw, int *processed, unsigned int scale) |
| 506 | { |
| 507 | int scale_type, scale_val, scale_val2, offset; |
| 508 | s64 raw64 = raw; |
| 509 | int ret; |
| 510 | |
Michael Hennerich | 6c5d4c9 | 2013-06-03 09:04:00 +0100 | [diff] [blame] | 511 | ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_OFFSET); |
Alexandre Belloni | f91d1b6 | 2013-07-01 17:40:00 +0100 | [diff] [blame] | 512 | if (ret >= 0) |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 513 | raw64 += offset; |
| 514 | |
| 515 | scale_type = iio_channel_read(chan, &scale_val, &scale_val2, |
| 516 | IIO_CHAN_INFO_SCALE); |
| 517 | if (scale_type < 0) |
| 518 | return scale_type; |
| 519 | |
| 520 | switch (scale_type) { |
| 521 | case IIO_VAL_INT: |
| 522 | *processed = raw64 * scale_val; |
| 523 | break; |
| 524 | case IIO_VAL_INT_PLUS_MICRO: |
| 525 | if (scale_val2 < 0) |
| 526 | *processed = -raw64 * scale_val; |
| 527 | else |
| 528 | *processed = raw64 * scale_val; |
| 529 | *processed += div_s64(raw64 * (s64)scale_val2 * scale, |
| 530 | 1000000LL); |
| 531 | break; |
| 532 | case IIO_VAL_INT_PLUS_NANO: |
| 533 | if (scale_val2 < 0) |
| 534 | *processed = -raw64 * scale_val; |
| 535 | else |
| 536 | *processed = raw64 * scale_val; |
| 537 | *processed += div_s64(raw64 * (s64)scale_val2 * scale, |
| 538 | 1000000000LL); |
| 539 | break; |
| 540 | case IIO_VAL_FRACTIONAL: |
| 541 | *processed = div_s64(raw64 * (s64)scale_val * scale, |
| 542 | scale_val2); |
| 543 | break; |
Lars-Peter Clausen | 103d9fb | 2012-10-16 17:29:00 +0100 | [diff] [blame] | 544 | case IIO_VAL_FRACTIONAL_LOG2: |
| 545 | *processed = (raw64 * (s64)scale_val * scale) >> scale_val2; |
| 546 | break; |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 547 | default: |
| 548 | return -EINVAL; |
| 549 | } |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | int iio_convert_raw_to_processed(struct iio_channel *chan, int raw, |
| 555 | int *processed, unsigned int scale) |
| 556 | { |
| 557 | int ret; |
| 558 | |
| 559 | mutex_lock(&chan->indio_dev->info_exist_lock); |
| 560 | if (chan->indio_dev->info == NULL) { |
| 561 | ret = -ENODEV; |
| 562 | goto err_unlock; |
| 563 | } |
| 564 | |
| 565 | ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed, |
| 566 | scale); |
| 567 | err_unlock: |
| 568 | mutex_unlock(&chan->indio_dev->info_exist_lock); |
| 569 | |
| 570 | return ret; |
| 571 | } |
| 572 | EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed); |
| 573 | |
| 574 | int iio_read_channel_processed(struct iio_channel *chan, int *val) |
| 575 | { |
| 576 | int ret; |
| 577 | |
| 578 | mutex_lock(&chan->indio_dev->info_exist_lock); |
| 579 | if (chan->indio_dev->info == NULL) { |
| 580 | ret = -ENODEV; |
| 581 | goto err_unlock; |
| 582 | } |
| 583 | |
| 584 | if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) { |
| 585 | ret = iio_channel_read(chan, val, NULL, |
| 586 | IIO_CHAN_INFO_PROCESSED); |
| 587 | } else { |
| 588 | ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW); |
| 589 | if (ret < 0) |
| 590 | goto err_unlock; |
| 591 | ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1); |
| 592 | } |
| 593 | |
| 594 | err_unlock: |
| 595 | mutex_unlock(&chan->indio_dev->info_exist_lock); |
| 596 | |
| 597 | return ret; |
| 598 | } |
| 599 | EXPORT_SYMBOL_GPL(iio_read_channel_processed); |
| 600 | |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 601 | 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] | 602 | { |
| 603 | int ret; |
| 604 | |
| 605 | mutex_lock(&chan->indio_dev->info_exist_lock); |
| 606 | if (chan->indio_dev->info == NULL) { |
| 607 | ret = -ENODEV; |
| 608 | goto err_unlock; |
| 609 | } |
| 610 | |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 611 | ret = iio_channel_read(chan, val, val2, IIO_CHAN_INFO_SCALE); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 612 | err_unlock: |
| 613 | mutex_unlock(&chan->indio_dev->info_exist_lock); |
| 614 | |
| 615 | return ret; |
| 616 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 617 | EXPORT_SYMBOL_GPL(iio_read_channel_scale); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 618 | |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 619 | 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] | 620 | { |
| 621 | int ret = 0; |
| 622 | /* Need to verify underlying driver has not gone away */ |
| 623 | |
| 624 | mutex_lock(&chan->indio_dev->info_exist_lock); |
| 625 | if (chan->indio_dev->info == NULL) { |
| 626 | ret = -ENODEV; |
| 627 | goto err_unlock; |
| 628 | } |
| 629 | |
| 630 | *type = chan->channel->type; |
| 631 | err_unlock: |
| 632 | mutex_unlock(&chan->indio_dev->info_exist_lock); |
| 633 | |
| 634 | return ret; |
| 635 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 636 | EXPORT_SYMBOL_GPL(iio_get_channel_type); |