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