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