Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 2 | /* The industrial I/O core in kernel channel mapping |
| 3 | * |
| 4 | * Copyright (c) 2011 Jonathan Cameron |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 5 | */ |
| 6 | #include <linux/err.h> |
| 7 | #include <linux/export.h> |
| 8 | #include <linux/slab.h> |
| 9 | #include <linux/mutex.h> |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 10 | #include <linux/of.h> |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 11 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 12 | #include <linux/iio/iio.h> |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 13 | #include <linux/iio/iio-opaque.h> |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 14 | #include "iio_core.h" |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 15 | #include <linux/iio/machine.h> |
| 16 | #include <linux/iio/driver.h> |
| 17 | #include <linux/iio/consumer.h> |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 18 | |
| 19 | struct iio_map_internal { |
| 20 | struct iio_dev *indio_dev; |
| 21 | struct iio_map *map; |
| 22 | struct list_head l; |
| 23 | }; |
| 24 | |
| 25 | static LIST_HEAD(iio_map_list); |
| 26 | static DEFINE_MUTEX(iio_map_list_lock); |
| 27 | |
Lino Sanfilippo | cc9fb60 | 2020-11-28 14:44:18 +0100 | [diff] [blame] | 28 | static int iio_map_array_unregister_locked(struct iio_dev *indio_dev) |
| 29 | { |
| 30 | int ret = -ENODEV; |
| 31 | struct iio_map_internal *mapi, *next; |
| 32 | |
| 33 | list_for_each_entry_safe(mapi, next, &iio_map_list, l) { |
| 34 | if (indio_dev == mapi->indio_dev) { |
| 35 | list_del(&mapi->l); |
| 36 | kfree(mapi); |
| 37 | ret = 0; |
| 38 | } |
| 39 | } |
| 40 | return ret; |
| 41 | } |
| 42 | |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 43 | int iio_map_array_register(struct iio_dev *indio_dev, struct iio_map *maps) |
| 44 | { |
| 45 | int i = 0, ret = 0; |
| 46 | struct iio_map_internal *mapi; |
| 47 | |
| 48 | if (maps == NULL) |
| 49 | return 0; |
| 50 | |
| 51 | mutex_lock(&iio_map_list_lock); |
| 52 | while (maps[i].consumer_dev_name != NULL) { |
| 53 | mapi = kzalloc(sizeof(*mapi), GFP_KERNEL); |
| 54 | if (mapi == NULL) { |
| 55 | ret = -ENOMEM; |
| 56 | goto error_ret; |
| 57 | } |
| 58 | mapi->map = &maps[i]; |
| 59 | mapi->indio_dev = indio_dev; |
Gaurav Gupta | bc4b2a5 | 2017-06-27 09:46:01 -0700 | [diff] [blame] | 60 | list_add_tail(&mapi->l, &iio_map_list); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 61 | i++; |
| 62 | } |
| 63 | error_ret: |
Lino Sanfilippo | 34fce6c | 2020-11-28 14:44:19 +0100 | [diff] [blame] | 64 | if (ret) |
| 65 | iio_map_array_unregister_locked(indio_dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 66 | mutex_unlock(&iio_map_list_lock); |
| 67 | |
| 68 | return ret; |
| 69 | } |
| 70 | EXPORT_SYMBOL_GPL(iio_map_array_register); |
| 71 | |
| 72 | |
Guenter Roeck | 6cb2afd | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 73 | /* |
| 74 | * Remove all map entries associated with the given iio device |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 75 | */ |
Guenter Roeck | 6cb2afd | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 76 | int iio_map_array_unregister(struct iio_dev *indio_dev) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 77 | { |
Lino Sanfilippo | cc9fb60 | 2020-11-28 14:44:18 +0100 | [diff] [blame] | 78 | int ret; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 79 | |
| 80 | mutex_lock(&iio_map_list_lock); |
Lino Sanfilippo | cc9fb60 | 2020-11-28 14:44:18 +0100 | [diff] [blame] | 81 | ret = iio_map_array_unregister_locked(indio_dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 82 | mutex_unlock(&iio_map_list_lock); |
Lino Sanfilippo | cc9fb60 | 2020-11-28 14:44:18 +0100 | [diff] [blame] | 83 | |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 84 | return ret; |
| 85 | } |
| 86 | EXPORT_SYMBOL_GPL(iio_map_array_unregister); |
| 87 | |
Alexandru Ardelean | 25c02ed | 2021-09-03 10:29:13 +0300 | [diff] [blame] | 88 | static void iio_map_array_unregister_cb(void *indio_dev) |
| 89 | { |
| 90 | iio_map_array_unregister(indio_dev); |
| 91 | } |
| 92 | |
| 93 | int devm_iio_map_array_register(struct device *dev, struct iio_dev *indio_dev, struct iio_map *maps) |
| 94 | { |
| 95 | int ret; |
| 96 | |
| 97 | ret = iio_map_array_register(indio_dev, maps); |
| 98 | if (ret) |
| 99 | return ret; |
| 100 | |
| 101 | return devm_add_action_or_reset(dev, iio_map_array_unregister_cb, indio_dev); |
| 102 | } |
| 103 | EXPORT_SYMBOL_GPL(devm_iio_map_array_register); |
| 104 | |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 105 | static const struct iio_chan_spec |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 106 | *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] | 107 | { |
| 108 | int i; |
| 109 | const struct iio_chan_spec *chan = NULL; |
| 110 | |
| 111 | for (i = 0; i < indio_dev->num_channels; i++) |
| 112 | if (indio_dev->channels[i].datasheet_name && |
| 113 | strcmp(name, indio_dev->channels[i].datasheet_name) == 0) { |
| 114 | chan = &indio_dev->channels[i]; |
| 115 | break; |
| 116 | } |
| 117 | return chan; |
| 118 | } |
| 119 | |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 120 | #ifdef CONFIG_OF |
| 121 | |
Suzuki K Poulose | 418e3ea | 2019-06-14 18:53:59 +0100 | [diff] [blame] | 122 | static int iio_dev_node_match(struct device *dev, const void *data) |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 123 | { |
| 124 | return dev->of_node == data && dev->type == &iio_device_type; |
| 125 | } |
| 126 | |
Ivan T. Ivanov | acd8256 | 2014-10-22 18:29:43 +0300 | [diff] [blame] | 127 | /** |
| 128 | * __of_iio_simple_xlate - translate iiospec to the IIO channel index |
| 129 | * @indio_dev: pointer to the iio_dev structure |
| 130 | * @iiospec: IIO specifier as found in the device tree |
| 131 | * |
| 132 | * This is simple translation function, suitable for the most 1:1 mapped |
| 133 | * channels in IIO chips. This function performs only one sanity check: |
| 134 | * whether IIO index is less than num_channels (that is specified in the |
| 135 | * iio_dev). |
| 136 | */ |
| 137 | static int __of_iio_simple_xlate(struct iio_dev *indio_dev, |
| 138 | const struct of_phandle_args *iiospec) |
| 139 | { |
| 140 | if (!iiospec->args_count) |
| 141 | return 0; |
| 142 | |
Stefan Wahren | 1f20272 | 2015-01-01 18:13:24 +0000 | [diff] [blame] | 143 | if (iiospec->args[0] >= indio_dev->num_channels) { |
| 144 | dev_err(&indio_dev->dev, "invalid channel index %u\n", |
| 145 | iiospec->args[0]); |
Ivan T. Ivanov | acd8256 | 2014-10-22 18:29:43 +0300 | [diff] [blame] | 146 | return -EINVAL; |
Stefan Wahren | 1f20272 | 2015-01-01 18:13:24 +0000 | [diff] [blame] | 147 | } |
Ivan T. Ivanov | acd8256 | 2014-10-22 18:29:43 +0300 | [diff] [blame] | 148 | |
| 149 | return iiospec->args[0]; |
| 150 | } |
| 151 | |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 152 | static int __of_iio_channel_get(struct iio_channel *channel, |
| 153 | struct device_node *np, int index) |
| 154 | { |
| 155 | struct device *idev; |
| 156 | struct iio_dev *indio_dev; |
| 157 | int err; |
| 158 | struct of_phandle_args iiospec; |
| 159 | |
| 160 | err = of_parse_phandle_with_args(np, "io-channels", |
| 161 | "#io-channel-cells", |
| 162 | index, &iiospec); |
| 163 | if (err) |
| 164 | return err; |
| 165 | |
| 166 | idev = bus_find_device(&iio_bus_type, NULL, iiospec.np, |
| 167 | iio_dev_node_match); |
| 168 | of_node_put(iiospec.np); |
| 169 | if (idev == NULL) |
| 170 | return -EPROBE_DEFER; |
| 171 | |
| 172 | indio_dev = dev_to_iio_dev(idev); |
| 173 | channel->indio_dev = indio_dev; |
Ivan T. Ivanov | acd8256 | 2014-10-22 18:29:43 +0300 | [diff] [blame] | 174 | if (indio_dev->info->of_xlate) |
| 175 | index = indio_dev->info->of_xlate(indio_dev, &iiospec); |
| 176 | else |
| 177 | index = __of_iio_simple_xlate(indio_dev, &iiospec); |
| 178 | if (index < 0) |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 179 | goto err_put; |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 180 | channel->channel = &indio_dev->channels[index]; |
| 181 | |
| 182 | return 0; |
| 183 | |
| 184 | err_put: |
| 185 | iio_device_put(indio_dev); |
Ivan T. Ivanov | acd8256 | 2014-10-22 18:29:43 +0300 | [diff] [blame] | 186 | return index; |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static struct iio_channel *of_iio_channel_get(struct device_node *np, int index) |
| 190 | { |
| 191 | struct iio_channel *channel; |
| 192 | int err; |
| 193 | |
| 194 | if (index < 0) |
| 195 | return ERR_PTR(-EINVAL); |
| 196 | |
| 197 | channel = kzalloc(sizeof(*channel), GFP_KERNEL); |
| 198 | if (channel == NULL) |
| 199 | return ERR_PTR(-ENOMEM); |
| 200 | |
| 201 | err = __of_iio_channel_get(channel, np, index); |
| 202 | if (err) |
| 203 | goto err_free_channel; |
| 204 | |
| 205 | return channel; |
| 206 | |
| 207 | err_free_channel: |
| 208 | kfree(channel); |
| 209 | return ERR_PTR(err); |
| 210 | } |
| 211 | |
Dmitry Baryshkov | 6e39b14 | 2020-12-04 05:55:01 +0300 | [diff] [blame] | 212 | struct iio_channel *of_iio_channel_get_by_name(struct device_node *np, |
| 213 | const char *name) |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 214 | { |
| 215 | struct iio_channel *chan = NULL; |
| 216 | |
| 217 | /* Walk up the tree of devices looking for a matching iio channel */ |
| 218 | while (np) { |
| 219 | int index = 0; |
| 220 | |
| 221 | /* |
| 222 | * For named iio channels, first look up the name in the |
| 223 | * "io-channel-names" property. If it cannot be found, the |
| 224 | * index will be an error code, and of_iio_channel_get() |
| 225 | * will fail. |
| 226 | */ |
| 227 | if (name) |
| 228 | index = of_property_match_string(np, "io-channel-names", |
| 229 | name); |
| 230 | chan = of_iio_channel_get(np, index); |
Johannes Pointner | 872687f | 2014-08-25 09:04:00 +0100 | [diff] [blame] | 231 | if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER) |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 232 | break; |
| 233 | else if (name && index >= 0) { |
Rob Herring | 3921db4 | 2017-07-18 16:43:08 -0500 | [diff] [blame] | 234 | pr_err("ERROR: could not get IIO channel %pOF:%s(%i)\n", |
| 235 | np, name ? name : "", index); |
Adam Thomson | a2c1249 | 2014-11-06 12:11:00 +0000 | [diff] [blame] | 236 | return NULL; |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /* |
| 240 | * No matching IIO channel found on this node. |
| 241 | * If the parent node has a "io-channel-ranges" property, |
| 242 | * then we can try one of its channels. |
| 243 | */ |
| 244 | np = np->parent; |
| 245 | if (np && !of_get_property(np, "io-channel-ranges", NULL)) |
Adam Thomson | a2c1249 | 2014-11-06 12:11:00 +0000 | [diff] [blame] | 246 | return NULL; |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 247 | } |
Adam Thomson | a2c1249 | 2014-11-06 12:11:00 +0000 | [diff] [blame] | 248 | |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 249 | return chan; |
| 250 | } |
Dmitry Baryshkov | 6e39b14 | 2020-12-04 05:55:01 +0300 | [diff] [blame] | 251 | EXPORT_SYMBOL_GPL(of_iio_channel_get_by_name); |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 252 | |
| 253 | static struct iio_channel *of_iio_channel_get_all(struct device *dev) |
| 254 | { |
| 255 | struct iio_channel *chans; |
| 256 | int i, mapind, nummaps = 0; |
| 257 | int ret; |
| 258 | |
| 259 | do { |
| 260 | ret = of_parse_phandle_with_args(dev->of_node, |
| 261 | "io-channels", |
| 262 | "#io-channel-cells", |
| 263 | nummaps, NULL); |
| 264 | if (ret < 0) |
| 265 | break; |
| 266 | } while (++nummaps); |
| 267 | |
| 268 | if (nummaps == 0) /* no error, return NULL to search map table */ |
| 269 | return NULL; |
| 270 | |
| 271 | /* NULL terminated array to save passing size */ |
| 272 | chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL); |
| 273 | if (chans == NULL) |
| 274 | return ERR_PTR(-ENOMEM); |
| 275 | |
| 276 | /* Search for OF matches */ |
| 277 | for (mapind = 0; mapind < nummaps; mapind++) { |
| 278 | ret = __of_iio_channel_get(&chans[mapind], dev->of_node, |
| 279 | mapind); |
| 280 | if (ret) |
| 281 | goto error_free_chans; |
| 282 | } |
| 283 | return chans; |
| 284 | |
| 285 | error_free_chans: |
| 286 | for (i = 0; i < mapind; i++) |
| 287 | iio_device_put(chans[i].indio_dev); |
| 288 | kfree(chans); |
| 289 | return ERR_PTR(ret); |
| 290 | } |
| 291 | |
| 292 | #else /* CONFIG_OF */ |
| 293 | |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 294 | static inline struct iio_channel *of_iio_channel_get_all(struct device *dev) |
| 295 | { |
| 296 | return NULL; |
| 297 | } |
| 298 | |
| 299 | #endif /* CONFIG_OF */ |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 300 | |
Guenter Roeck | 5aa57f0 | 2013-02-04 20:26:00 +0000 | [diff] [blame] | 301 | static struct iio_channel *iio_channel_get_sys(const char *name, |
| 302 | const char *channel_name) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 303 | { |
| 304 | struct iio_map_internal *c_i = NULL, *c = NULL; |
| 305 | struct iio_channel *channel; |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 306 | int err; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 307 | |
| 308 | if (name == NULL && channel_name == NULL) |
| 309 | return ERR_PTR(-ENODEV); |
| 310 | |
| 311 | /* first find matching entry the channel map */ |
| 312 | mutex_lock(&iio_map_list_lock); |
| 313 | list_for_each_entry(c_i, &iio_map_list, l) { |
| 314 | if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) || |
| 315 | (channel_name && |
| 316 | strcmp(channel_name, c_i->map->consumer_channel) != 0)) |
| 317 | continue; |
| 318 | c = c_i; |
Lars-Peter Clausen | 1875ffd | 2012-06-04 10:50:03 +0200 | [diff] [blame] | 319 | iio_device_get(c->indio_dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 320 | break; |
| 321 | } |
| 322 | mutex_unlock(&iio_map_list_lock); |
| 323 | if (c == NULL) |
| 324 | return ERR_PTR(-ENODEV); |
| 325 | |
Kim, Milo | 2cc412b | 2012-09-14 02:24:00 +0100 | [diff] [blame] | 326 | channel = kzalloc(sizeof(*channel), GFP_KERNEL); |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 327 | if (channel == NULL) { |
| 328 | err = -ENOMEM; |
Kim, Milo | 801c4b5 | 2012-09-18 05:55:00 +0100 | [diff] [blame] | 329 | goto error_no_mem; |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 330 | } |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 331 | |
| 332 | channel->indio_dev = c->indio_dev; |
| 333 | |
Kim, Milo | b2b79ff | 2012-09-17 09:44:00 +0100 | [diff] [blame] | 334 | if (c->map->adc_channel_label) { |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 335 | channel->channel = |
| 336 | iio_chan_spec_from_name(channel->indio_dev, |
| 337 | c->map->adc_channel_label); |
| 338 | |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 339 | if (channel->channel == NULL) { |
| 340 | err = -EINVAL; |
Kim, Milo | b2b79ff | 2012-09-17 09:44:00 +0100 | [diff] [blame] | 341 | goto error_no_chan; |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 342 | } |
Kim, Milo | b2b79ff | 2012-09-17 09:44:00 +0100 | [diff] [blame] | 343 | } |
| 344 | |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 345 | return channel; |
Kim, Milo | b2b79ff | 2012-09-17 09:44:00 +0100 | [diff] [blame] | 346 | |
| 347 | error_no_chan: |
Kim, Milo | b2b79ff | 2012-09-17 09:44:00 +0100 | [diff] [blame] | 348 | kfree(channel); |
Kim, Milo | 801c4b5 | 2012-09-18 05:55:00 +0100 | [diff] [blame] | 349 | error_no_mem: |
| 350 | iio_device_put(c->indio_dev); |
Kim, Milo | 3183bac | 2012-09-18 05:56:00 +0100 | [diff] [blame] | 351 | return ERR_PTR(err); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 352 | } |
Guenter Roeck | 5aa57f0 | 2013-02-04 20:26:00 +0000 | [diff] [blame] | 353 | |
| 354 | struct iio_channel *iio_channel_get(struct device *dev, |
| 355 | const char *channel_name) |
| 356 | { |
| 357 | const char *name = dev ? dev_name(dev) : NULL; |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 358 | struct iio_channel *channel; |
Guenter Roeck | 5aa57f0 | 2013-02-04 20:26:00 +0000 | [diff] [blame] | 359 | |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 360 | if (dev) { |
| 361 | channel = of_iio_channel_get_by_name(dev->of_node, |
| 362 | channel_name); |
| 363 | if (channel != NULL) |
| 364 | return channel; |
| 365 | } |
Adam Thomson | a2c1249 | 2014-11-06 12:11:00 +0000 | [diff] [blame] | 366 | |
Guenter Roeck | 5aa57f0 | 2013-02-04 20:26:00 +0000 | [diff] [blame] | 367 | return iio_channel_get_sys(name, channel_name); |
| 368 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 369 | EXPORT_SYMBOL_GPL(iio_channel_get); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 370 | |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 371 | void iio_channel_release(struct iio_channel *channel) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 372 | { |
Dan Carpenter | d81dac3 | 2016-01-26 12:25:21 +0300 | [diff] [blame] | 373 | if (!channel) |
| 374 | return; |
Lars-Peter Clausen | 1875ffd | 2012-06-04 10:50:03 +0200 | [diff] [blame] | 375 | iio_device_put(channel->indio_dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 376 | kfree(channel); |
| 377 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 378 | EXPORT_SYMBOL_GPL(iio_channel_release); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 379 | |
Yicong Yang | 7349e8a | 2021-04-08 19:38:16 +0800 | [diff] [blame] | 380 | static void devm_iio_channel_free(void *iio_channel) |
Laxman Dewangan | 8bf872d | 2016-04-06 16:01:06 +0530 | [diff] [blame] | 381 | { |
Yicong Yang | 7349e8a | 2021-04-08 19:38:16 +0800 | [diff] [blame] | 382 | iio_channel_release(iio_channel); |
Laxman Dewangan | 8bf872d | 2016-04-06 16:01:06 +0530 | [diff] [blame] | 383 | } |
| 384 | |
Laxman Dewangan | 8bf872d | 2016-04-06 16:01:06 +0530 | [diff] [blame] | 385 | struct iio_channel *devm_iio_channel_get(struct device *dev, |
| 386 | const char *channel_name) |
| 387 | { |
Yicong Yang | 7349e8a | 2021-04-08 19:38:16 +0800 | [diff] [blame] | 388 | struct iio_channel *channel; |
| 389 | int ret; |
Laxman Dewangan | 8bf872d | 2016-04-06 16:01:06 +0530 | [diff] [blame] | 390 | |
| 391 | channel = iio_channel_get(dev, channel_name); |
Yicong Yang | 7349e8a | 2021-04-08 19:38:16 +0800 | [diff] [blame] | 392 | if (IS_ERR(channel)) |
Laxman Dewangan | 8bf872d | 2016-04-06 16:01:06 +0530 | [diff] [blame] | 393 | return channel; |
Laxman Dewangan | 8bf872d | 2016-04-06 16:01:06 +0530 | [diff] [blame] | 394 | |
Yicong Yang | 7349e8a | 2021-04-08 19:38:16 +0800 | [diff] [blame] | 395 | ret = devm_add_action_or_reset(dev, devm_iio_channel_free, channel); |
| 396 | if (ret) |
| 397 | return ERR_PTR(ret); |
Laxman Dewangan | 8bf872d | 2016-04-06 16:01:06 +0530 | [diff] [blame] | 398 | |
| 399 | return channel; |
| 400 | } |
| 401 | EXPORT_SYMBOL_GPL(devm_iio_channel_get); |
| 402 | |
Dmitry Baryshkov | 6e39b14 | 2020-12-04 05:55:01 +0300 | [diff] [blame] | 403 | struct iio_channel *devm_of_iio_channel_get_by_name(struct device *dev, |
| 404 | struct device_node *np, |
| 405 | const char *channel_name) |
| 406 | { |
Yicong Yang | 7349e8a | 2021-04-08 19:38:16 +0800 | [diff] [blame] | 407 | struct iio_channel *channel; |
| 408 | int ret; |
Dmitry Baryshkov | 6e39b14 | 2020-12-04 05:55:01 +0300 | [diff] [blame] | 409 | |
| 410 | channel = of_iio_channel_get_by_name(np, channel_name); |
Yicong Yang | 7349e8a | 2021-04-08 19:38:16 +0800 | [diff] [blame] | 411 | if (IS_ERR(channel)) |
Dmitry Baryshkov | 6e39b14 | 2020-12-04 05:55:01 +0300 | [diff] [blame] | 412 | return channel; |
Dmitry Baryshkov | 6e39b14 | 2020-12-04 05:55:01 +0300 | [diff] [blame] | 413 | |
Yicong Yang | 7349e8a | 2021-04-08 19:38:16 +0800 | [diff] [blame] | 414 | ret = devm_add_action_or_reset(dev, devm_iio_channel_free, channel); |
| 415 | if (ret) |
| 416 | return ERR_PTR(ret); |
Dmitry Baryshkov | 6e39b14 | 2020-12-04 05:55:01 +0300 | [diff] [blame] | 417 | |
| 418 | return channel; |
| 419 | } |
| 420 | EXPORT_SYMBOL_GPL(devm_of_iio_channel_get_by_name); |
| 421 | |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 422 | struct iio_channel *iio_channel_get_all(struct device *dev) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 423 | { |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 424 | const char *name; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 425 | struct iio_channel *chans; |
| 426 | struct iio_map_internal *c = NULL; |
| 427 | int nummaps = 0; |
| 428 | int mapind = 0; |
| 429 | int i, ret; |
| 430 | |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 431 | if (dev == NULL) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 432 | return ERR_PTR(-EINVAL); |
Guenter Roeck | 17d82b4 | 2013-02-07 17:09:00 +0000 | [diff] [blame] | 433 | |
| 434 | chans = of_iio_channel_get_all(dev); |
| 435 | if (chans) |
| 436 | return chans; |
| 437 | |
Guenter Roeck | ca7d98d | 2013-01-31 21:43:00 +0000 | [diff] [blame] | 438 | name = dev_name(dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 439 | |
| 440 | mutex_lock(&iio_map_list_lock); |
| 441 | /* first count the matching maps */ |
| 442 | list_for_each_entry(c, &iio_map_list, l) |
| 443 | if (name && strcmp(name, c->map->consumer_dev_name) != 0) |
| 444 | continue; |
| 445 | else |
| 446 | nummaps++; |
| 447 | |
| 448 | if (nummaps == 0) { |
| 449 | ret = -ENODEV; |
| 450 | goto error_ret; |
| 451 | } |
| 452 | |
| 453 | /* NULL terminated array to save passing size */ |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 454 | chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 455 | if (chans == NULL) { |
| 456 | ret = -ENOMEM; |
| 457 | goto error_ret; |
| 458 | } |
| 459 | |
| 460 | /* for each map fill in the chans element */ |
| 461 | list_for_each_entry(c, &iio_map_list, l) { |
| 462 | if (name && strcmp(name, c->map->consumer_dev_name) != 0) |
| 463 | continue; |
| 464 | chans[mapind].indio_dev = c->indio_dev; |
Jonathan Cameron | 0464415 | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 465 | chans[mapind].data = c->map->consumer_data; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 466 | chans[mapind].channel = |
| 467 | iio_chan_spec_from_name(chans[mapind].indio_dev, |
| 468 | c->map->adc_channel_label); |
| 469 | if (chans[mapind].channel == NULL) { |
| 470 | ret = -EINVAL; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 471 | goto error_free_chans; |
| 472 | } |
Lars-Peter Clausen | 1875ffd | 2012-06-04 10:50:03 +0200 | [diff] [blame] | 473 | iio_device_get(chans[mapind].indio_dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 474 | mapind++; |
| 475 | } |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 476 | if (mapind == 0) { |
| 477 | ret = -ENODEV; |
| 478 | goto error_free_chans; |
| 479 | } |
Dan Carpenter | e59b9af | 2012-07-11 07:34:00 +0100 | [diff] [blame] | 480 | mutex_unlock(&iio_map_list_lock); |
| 481 | |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 482 | return chans; |
| 483 | |
| 484 | error_free_chans: |
| 485 | for (i = 0; i < nummaps; i++) |
Lars-Peter Clausen | 1875ffd | 2012-06-04 10:50:03 +0200 | [diff] [blame] | 486 | iio_device_put(chans[i].indio_dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 487 | kfree(chans); |
| 488 | error_ret: |
| 489 | mutex_unlock(&iio_map_list_lock); |
| 490 | |
| 491 | return ERR_PTR(ret); |
| 492 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 493 | EXPORT_SYMBOL_GPL(iio_channel_get_all); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 494 | |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 495 | void iio_channel_release_all(struct iio_channel *channels) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 496 | { |
| 497 | struct iio_channel *chan = &channels[0]; |
| 498 | |
| 499 | while (chan->indio_dev) { |
Lars-Peter Clausen | 1875ffd | 2012-06-04 10:50:03 +0200 | [diff] [blame] | 500 | iio_device_put(chan->indio_dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 501 | chan++; |
| 502 | } |
| 503 | kfree(channels); |
| 504 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 505 | EXPORT_SYMBOL_GPL(iio_channel_release_all); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 506 | |
Yicong Yang | 7349e8a | 2021-04-08 19:38:16 +0800 | [diff] [blame] | 507 | static void devm_iio_channel_free_all(void *iio_channels) |
Laxman Dewangan | efc2c01 | 2016-04-06 16:01:07 +0530 | [diff] [blame] | 508 | { |
Yicong Yang | 7349e8a | 2021-04-08 19:38:16 +0800 | [diff] [blame] | 509 | iio_channel_release_all(iio_channels); |
Laxman Dewangan | efc2c01 | 2016-04-06 16:01:07 +0530 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | struct iio_channel *devm_iio_channel_get_all(struct device *dev) |
| 513 | { |
Yicong Yang | 7349e8a | 2021-04-08 19:38:16 +0800 | [diff] [blame] | 514 | struct iio_channel *channels; |
| 515 | int ret; |
Laxman Dewangan | efc2c01 | 2016-04-06 16:01:07 +0530 | [diff] [blame] | 516 | |
| 517 | channels = iio_channel_get_all(dev); |
Yicong Yang | 7349e8a | 2021-04-08 19:38:16 +0800 | [diff] [blame] | 518 | if (IS_ERR(channels)) |
Laxman Dewangan | efc2c01 | 2016-04-06 16:01:07 +0530 | [diff] [blame] | 519 | return channels; |
Laxman Dewangan | efc2c01 | 2016-04-06 16:01:07 +0530 | [diff] [blame] | 520 | |
Yicong Yang | 7349e8a | 2021-04-08 19:38:16 +0800 | [diff] [blame] | 521 | ret = devm_add_action_or_reset(dev, devm_iio_channel_free_all, |
| 522 | channels); |
| 523 | if (ret) |
| 524 | return ERR_PTR(ret); |
Laxman Dewangan | efc2c01 | 2016-04-06 16:01:07 +0530 | [diff] [blame] | 525 | |
| 526 | return channels; |
| 527 | } |
| 528 | EXPORT_SYMBOL_GPL(devm_iio_channel_get_all); |
| 529 | |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 530 | static int iio_channel_read(struct iio_channel *chan, int *val, int *val2, |
| 531 | enum iio_chan_info_enum info) |
| 532 | { |
| 533 | int unused; |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 534 | int vals[INDIO_MAX_RAW_ELEMENTS]; |
| 535 | int ret; |
| 536 | int val_len = 2; |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 537 | |
| 538 | if (val2 == NULL) |
| 539 | val2 = &unused; |
| 540 | |
Slawomir Stepien | fc0b817 | 2016-04-14 21:36:34 +0200 | [diff] [blame] | 541 | if (!iio_channel_has_info(chan->channel, info)) |
Fabien Proriol | 65de765 | 2015-01-01 12:46:48 +0000 | [diff] [blame] | 542 | return -EINVAL; |
| 543 | |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 544 | if (chan->indio_dev->info->read_raw_multi) { |
| 545 | ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev, |
| 546 | chan->channel, INDIO_MAX_RAW_ELEMENTS, |
| 547 | vals, &val_len, info); |
| 548 | *val = vals[0]; |
| 549 | *val2 = vals[1]; |
| 550 | } else |
| 551 | ret = chan->indio_dev->info->read_raw(chan->indio_dev, |
| 552 | chan->channel, val, val2, info); |
| 553 | |
| 554 | return ret; |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 555 | } |
| 556 | |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 557 | int iio_read_channel_raw(struct iio_channel *chan, int *val) |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 558 | { |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 559 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 560 | int ret; |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 561 | |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 562 | mutex_lock(&iio_dev_opaque->info_exist_lock); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 563 | if (chan->indio_dev->info == NULL) { |
| 564 | ret = -ENODEV; |
| 565 | goto err_unlock; |
| 566 | } |
| 567 | |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 568 | ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 569 | err_unlock: |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 570 | mutex_unlock(&iio_dev_opaque->info_exist_lock); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 571 | |
| 572 | return ret; |
| 573 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 574 | EXPORT_SYMBOL_GPL(iio_read_channel_raw); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 575 | |
Sebastian Reichel | 476d4af | 2014-10-03 17:25:00 +0100 | [diff] [blame] | 576 | int iio_read_channel_average_raw(struct iio_channel *chan, int *val) |
| 577 | { |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 578 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); |
Sebastian Reichel | 476d4af | 2014-10-03 17:25:00 +0100 | [diff] [blame] | 579 | int ret; |
| 580 | |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 581 | mutex_lock(&iio_dev_opaque->info_exist_lock); |
Sebastian Reichel | 476d4af | 2014-10-03 17:25:00 +0100 | [diff] [blame] | 582 | if (chan->indio_dev->info == NULL) { |
| 583 | ret = -ENODEV; |
| 584 | goto err_unlock; |
| 585 | } |
| 586 | |
| 587 | ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW); |
| 588 | err_unlock: |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 589 | mutex_unlock(&iio_dev_opaque->info_exist_lock); |
Sebastian Reichel | 476d4af | 2014-10-03 17:25:00 +0100 | [diff] [blame] | 590 | |
| 591 | return ret; |
| 592 | } |
| 593 | EXPORT_SYMBOL_GPL(iio_read_channel_average_raw); |
| 594 | |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 595 | static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan, |
| 596 | int raw, int *processed, unsigned int scale) |
| 597 | { |
| 598 | int scale_type, scale_val, scale_val2, offset; |
| 599 | s64 raw64 = raw; |
| 600 | int ret; |
| 601 | |
Michael Hennerich | 6c5d4c9 | 2013-06-03 09:04:00 +0100 | [diff] [blame] | 602 | ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_OFFSET); |
Alexandre Belloni | f91d1b6 | 2013-07-01 17:40:00 +0100 | [diff] [blame] | 603 | if (ret >= 0) |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 604 | raw64 += offset; |
| 605 | |
| 606 | scale_type = iio_channel_read(chan, &scale_val, &scale_val2, |
| 607 | IIO_CHAN_INFO_SCALE); |
Linus Walleij | adc8ec5 | 2017-01-11 11:48:39 +0100 | [diff] [blame] | 608 | if (scale_type < 0) { |
| 609 | /* |
| 610 | * Just pass raw values as processed if no scaling is |
| 611 | * available. |
| 612 | */ |
| 613 | *processed = raw; |
| 614 | return 0; |
| 615 | } |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 616 | |
| 617 | switch (scale_type) { |
| 618 | case IIO_VAL_INT: |
| 619 | *processed = raw64 * scale_val; |
| 620 | break; |
| 621 | case IIO_VAL_INT_PLUS_MICRO: |
| 622 | if (scale_val2 < 0) |
| 623 | *processed = -raw64 * scale_val; |
| 624 | else |
| 625 | *processed = raw64 * scale_val; |
| 626 | *processed += div_s64(raw64 * (s64)scale_val2 * scale, |
| 627 | 1000000LL); |
| 628 | break; |
| 629 | case IIO_VAL_INT_PLUS_NANO: |
| 630 | if (scale_val2 < 0) |
| 631 | *processed = -raw64 * scale_val; |
| 632 | else |
| 633 | *processed = raw64 * scale_val; |
| 634 | *processed += div_s64(raw64 * (s64)scale_val2 * scale, |
| 635 | 1000000000LL); |
| 636 | break; |
| 637 | case IIO_VAL_FRACTIONAL: |
| 638 | *processed = div_s64(raw64 * (s64)scale_val * scale, |
| 639 | scale_val2); |
| 640 | break; |
Lars-Peter Clausen | 103d9fb | 2012-10-16 17:29:00 +0100 | [diff] [blame] | 641 | case IIO_VAL_FRACTIONAL_LOG2: |
| 642 | *processed = (raw64 * (s64)scale_val * scale) >> scale_val2; |
| 643 | break; |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 644 | default: |
| 645 | return -EINVAL; |
| 646 | } |
| 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | int iio_convert_raw_to_processed(struct iio_channel *chan, int raw, |
| 652 | int *processed, unsigned int scale) |
| 653 | { |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 654 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 655 | int ret; |
| 656 | |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 657 | mutex_lock(&iio_dev_opaque->info_exist_lock); |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 658 | if (chan->indio_dev->info == NULL) { |
| 659 | ret = -ENODEV; |
| 660 | goto err_unlock; |
| 661 | } |
| 662 | |
| 663 | ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed, |
| 664 | scale); |
| 665 | err_unlock: |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 666 | mutex_unlock(&iio_dev_opaque->info_exist_lock); |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 667 | |
| 668 | return ret; |
| 669 | } |
| 670 | EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed); |
| 671 | |
Arnaud Pouliquen | 34739a21 | 2018-01-10 11:13:06 +0100 | [diff] [blame] | 672 | int iio_read_channel_attribute(struct iio_channel *chan, int *val, int *val2, |
| 673 | enum iio_chan_info_enum attribute) |
Matt Ranostay | 0023e67 | 2016-09-23 23:04:07 -0700 | [diff] [blame] | 674 | { |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 675 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); |
Matt Ranostay | 0023e67 | 2016-09-23 23:04:07 -0700 | [diff] [blame] | 676 | int ret; |
| 677 | |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 678 | mutex_lock(&iio_dev_opaque->info_exist_lock); |
Matt Ranostay | 0023e67 | 2016-09-23 23:04:07 -0700 | [diff] [blame] | 679 | if (chan->indio_dev->info == NULL) { |
| 680 | ret = -ENODEV; |
| 681 | goto err_unlock; |
| 682 | } |
| 683 | |
| 684 | ret = iio_channel_read(chan, val, val2, attribute); |
| 685 | err_unlock: |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 686 | mutex_unlock(&iio_dev_opaque->info_exist_lock); |
Matt Ranostay | 0023e67 | 2016-09-23 23:04:07 -0700 | [diff] [blame] | 687 | |
| 688 | return ret; |
| 689 | } |
Arnaud Pouliquen | 34739a21 | 2018-01-10 11:13:06 +0100 | [diff] [blame] | 690 | EXPORT_SYMBOL_GPL(iio_read_channel_attribute); |
Matt Ranostay | 0023e67 | 2016-09-23 23:04:07 -0700 | [diff] [blame] | 691 | |
| 692 | int iio_read_channel_offset(struct iio_channel *chan, int *val, int *val2) |
| 693 | { |
| 694 | return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_OFFSET); |
| 695 | } |
| 696 | EXPORT_SYMBOL_GPL(iio_read_channel_offset); |
| 697 | |
Linus Walleij | 635ef60 | 2021-03-08 11:02:18 +0100 | [diff] [blame] | 698 | int iio_read_channel_processed_scale(struct iio_channel *chan, int *val, |
| 699 | unsigned int scale) |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 700 | { |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 701 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 702 | int ret; |
| 703 | |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 704 | mutex_lock(&iio_dev_opaque->info_exist_lock); |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 705 | if (chan->indio_dev->info == NULL) { |
| 706 | ret = -ENODEV; |
| 707 | goto err_unlock; |
| 708 | } |
| 709 | |
| 710 | if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) { |
| 711 | ret = iio_channel_read(chan, val, NULL, |
| 712 | IIO_CHAN_INFO_PROCESSED); |
Linus Walleij | b3b64e2 | 2021-03-23 13:27:05 +0100 | [diff] [blame] | 713 | if (ret < 0) |
Linus Walleij | 635ef60 | 2021-03-08 11:02:18 +0100 | [diff] [blame] | 714 | goto err_unlock; |
| 715 | *val *= scale; |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 716 | } else { |
| 717 | ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW); |
| 718 | if (ret < 0) |
| 719 | goto err_unlock; |
Linus Walleij | 635ef60 | 2021-03-08 11:02:18 +0100 | [diff] [blame] | 720 | ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, |
| 721 | scale); |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | err_unlock: |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 725 | mutex_unlock(&iio_dev_opaque->info_exist_lock); |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 726 | |
| 727 | return ret; |
| 728 | } |
Linus Walleij | 635ef60 | 2021-03-08 11:02:18 +0100 | [diff] [blame] | 729 | EXPORT_SYMBOL_GPL(iio_read_channel_processed_scale); |
| 730 | |
| 731 | int iio_read_channel_processed(struct iio_channel *chan, int *val) |
| 732 | { |
| 733 | /* This is just a special case with scale factor 1 */ |
| 734 | return iio_read_channel_processed_scale(chan, val, 1); |
| 735 | } |
Lars-Peter Clausen | 48e44ce | 2012-09-17 13:17:00 +0100 | [diff] [blame] | 736 | EXPORT_SYMBOL_GPL(iio_read_channel_processed); |
| 737 | |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 738 | 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] | 739 | { |
Matt Ranostay | 0023e67 | 2016-09-23 23:04:07 -0700 | [diff] [blame] | 740 | return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_SCALE); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 741 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 742 | EXPORT_SYMBOL_GPL(iio_read_channel_scale); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 743 | |
Peter Rosin | 00c5f80 | 2016-11-08 12:58:52 +0100 | [diff] [blame] | 744 | static int iio_channel_read_avail(struct iio_channel *chan, |
| 745 | const int **vals, int *type, int *length, |
| 746 | enum iio_chan_info_enum info) |
| 747 | { |
| 748 | if (!iio_channel_has_available(chan->channel, info)) |
| 749 | return -EINVAL; |
| 750 | |
| 751 | return chan->indio_dev->info->read_avail(chan->indio_dev, chan->channel, |
| 752 | vals, type, length, info); |
| 753 | } |
| 754 | |
Artur Rojek | 9f42109 | 2019-03-23 18:28:06 +0100 | [diff] [blame] | 755 | int iio_read_avail_channel_attribute(struct iio_channel *chan, |
| 756 | const int **vals, int *type, int *length, |
| 757 | enum iio_chan_info_enum attribute) |
| 758 | { |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 759 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); |
Artur Rojek | 9f42109 | 2019-03-23 18:28:06 +0100 | [diff] [blame] | 760 | int ret; |
| 761 | |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 762 | mutex_lock(&iio_dev_opaque->info_exist_lock); |
Artur Rojek | 9f42109 | 2019-03-23 18:28:06 +0100 | [diff] [blame] | 763 | if (!chan->indio_dev->info) { |
| 764 | ret = -ENODEV; |
| 765 | goto err_unlock; |
| 766 | } |
| 767 | |
| 768 | ret = iio_channel_read_avail(chan, vals, type, length, attribute); |
| 769 | err_unlock: |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 770 | mutex_unlock(&iio_dev_opaque->info_exist_lock); |
Artur Rojek | 9f42109 | 2019-03-23 18:28:06 +0100 | [diff] [blame] | 771 | |
| 772 | return ret; |
| 773 | } |
| 774 | EXPORT_SYMBOL_GPL(iio_read_avail_channel_attribute); |
| 775 | |
Peter Rosin | 00c5f80 | 2016-11-08 12:58:52 +0100 | [diff] [blame] | 776 | int iio_read_avail_channel_raw(struct iio_channel *chan, |
| 777 | const int **vals, int *length) |
| 778 | { |
| 779 | int ret; |
| 780 | int type; |
| 781 | |
Artur Rojek | 89388ca | 2019-03-23 18:28:07 +0100 | [diff] [blame] | 782 | ret = iio_read_avail_channel_attribute(chan, vals, &type, length, |
| 783 | IIO_CHAN_INFO_RAW); |
Peter Rosin | 00c5f80 | 2016-11-08 12:58:52 +0100 | [diff] [blame] | 784 | |
Peter Rosin | c773f70 | 2017-04-20 23:01:57 +0200 | [diff] [blame] | 785 | if (ret >= 0 && type != IIO_VAL_INT) |
Peter Rosin | 00c5f80 | 2016-11-08 12:58:52 +0100 | [diff] [blame] | 786 | /* raw values are assumed to be IIO_VAL_INT */ |
| 787 | ret = -EINVAL; |
Peter Rosin | 00c5f80 | 2016-11-08 12:58:52 +0100 | [diff] [blame] | 788 | |
| 789 | return ret; |
| 790 | } |
| 791 | EXPORT_SYMBOL_GPL(iio_read_avail_channel_raw); |
| 792 | |
| 793 | static int iio_channel_read_max(struct iio_channel *chan, |
| 794 | int *val, int *val2, int *type, |
| 795 | enum iio_chan_info_enum info) |
| 796 | { |
| 797 | int unused; |
| 798 | const int *vals; |
| 799 | int length; |
| 800 | int ret; |
| 801 | |
| 802 | if (!val2) |
| 803 | val2 = &unused; |
| 804 | |
| 805 | ret = iio_channel_read_avail(chan, &vals, type, &length, info); |
| 806 | switch (ret) { |
| 807 | case IIO_AVAIL_RANGE: |
| 808 | switch (*type) { |
| 809 | case IIO_VAL_INT: |
| 810 | *val = vals[2]; |
| 811 | break; |
| 812 | default: |
| 813 | *val = vals[4]; |
| 814 | *val2 = vals[5]; |
| 815 | } |
| 816 | return 0; |
| 817 | |
| 818 | case IIO_AVAIL_LIST: |
| 819 | if (length <= 0) |
| 820 | return -EINVAL; |
| 821 | switch (*type) { |
| 822 | case IIO_VAL_INT: |
| 823 | *val = vals[--length]; |
| 824 | while (length) { |
| 825 | if (vals[--length] > *val) |
| 826 | *val = vals[length]; |
| 827 | } |
| 828 | break; |
| 829 | default: |
| 830 | /* FIXME: learn about max for other iio values */ |
| 831 | return -EINVAL; |
| 832 | } |
| 833 | return 0; |
| 834 | |
| 835 | default: |
| 836 | return ret; |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | int iio_read_max_channel_raw(struct iio_channel *chan, int *val) |
| 841 | { |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 842 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); |
Peter Rosin | 00c5f80 | 2016-11-08 12:58:52 +0100 | [diff] [blame] | 843 | int ret; |
| 844 | int type; |
| 845 | |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 846 | mutex_lock(&iio_dev_opaque->info_exist_lock); |
Peter Rosin | 00c5f80 | 2016-11-08 12:58:52 +0100 | [diff] [blame] | 847 | if (!chan->indio_dev->info) { |
| 848 | ret = -ENODEV; |
| 849 | goto err_unlock; |
| 850 | } |
| 851 | |
| 852 | ret = iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW); |
| 853 | err_unlock: |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 854 | mutex_unlock(&iio_dev_opaque->info_exist_lock); |
Peter Rosin | 00c5f80 | 2016-11-08 12:58:52 +0100 | [diff] [blame] | 855 | |
| 856 | return ret; |
| 857 | } |
| 858 | EXPORT_SYMBOL_GPL(iio_read_max_channel_raw); |
| 859 | |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 860 | 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] | 861 | { |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 862 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 863 | int ret = 0; |
| 864 | /* Need to verify underlying driver has not gone away */ |
| 865 | |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 866 | mutex_lock(&iio_dev_opaque->info_exist_lock); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 867 | if (chan->indio_dev->info == NULL) { |
| 868 | ret = -ENODEV; |
| 869 | goto err_unlock; |
| 870 | } |
| 871 | |
| 872 | *type = chan->channel->type; |
| 873 | err_unlock: |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 874 | mutex_unlock(&iio_dev_opaque->info_exist_lock); |
Jonathan Cameron | e27d75d | 2012-02-15 19:48:01 +0000 | [diff] [blame] | 875 | |
| 876 | return ret; |
| 877 | } |
Jonathan Cameron | 314be14 | 2012-05-01 21:04:24 +0100 | [diff] [blame] | 878 | EXPORT_SYMBOL_GPL(iio_get_channel_type); |
Dmitry Eremin-Solenikov | f9380e7 | 2014-11-27 01:42:45 +0300 | [diff] [blame] | 879 | |
| 880 | static int iio_channel_write(struct iio_channel *chan, int val, int val2, |
| 881 | enum iio_chan_info_enum info) |
| 882 | { |
| 883 | return chan->indio_dev->info->write_raw(chan->indio_dev, |
| 884 | chan->channel, val, val2, info); |
| 885 | } |
| 886 | |
Arnaud Pouliquen | 34739a21 | 2018-01-10 11:13:06 +0100 | [diff] [blame] | 887 | int iio_write_channel_attribute(struct iio_channel *chan, int val, int val2, |
| 888 | enum iio_chan_info_enum attribute) |
Dmitry Eremin-Solenikov | f9380e7 | 2014-11-27 01:42:45 +0300 | [diff] [blame] | 889 | { |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 890 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); |
Dmitry Eremin-Solenikov | f9380e7 | 2014-11-27 01:42:45 +0300 | [diff] [blame] | 891 | int ret; |
| 892 | |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 893 | mutex_lock(&iio_dev_opaque->info_exist_lock); |
Dmitry Eremin-Solenikov | f9380e7 | 2014-11-27 01:42:45 +0300 | [diff] [blame] | 894 | if (chan->indio_dev->info == NULL) { |
| 895 | ret = -ENODEV; |
| 896 | goto err_unlock; |
| 897 | } |
| 898 | |
Arnaud Pouliquen | 34739a21 | 2018-01-10 11:13:06 +0100 | [diff] [blame] | 899 | ret = iio_channel_write(chan, val, val2, attribute); |
Dmitry Eremin-Solenikov | f9380e7 | 2014-11-27 01:42:45 +0300 | [diff] [blame] | 900 | err_unlock: |
Jonathan Cameron | b804e2b | 2021-04-26 18:49:08 +0100 | [diff] [blame] | 901 | mutex_unlock(&iio_dev_opaque->info_exist_lock); |
Dmitry Eremin-Solenikov | f9380e7 | 2014-11-27 01:42:45 +0300 | [diff] [blame] | 902 | |
| 903 | return ret; |
| 904 | } |
Arnaud Pouliquen | 34739a21 | 2018-01-10 11:13:06 +0100 | [diff] [blame] | 905 | EXPORT_SYMBOL_GPL(iio_write_channel_attribute); |
| 906 | |
| 907 | int iio_write_channel_raw(struct iio_channel *chan, int val) |
| 908 | { |
| 909 | return iio_write_channel_attribute(chan, val, 0, IIO_CHAN_INFO_RAW); |
| 910 | } |
Dmitry Eremin-Solenikov | f9380e7 | 2014-11-27 01:42:45 +0300 | [diff] [blame] | 911 | EXPORT_SYMBOL_GPL(iio_write_channel_raw); |
Peter Rosin | 8a848e7 | 2017-05-14 21:51:08 +0200 | [diff] [blame] | 912 | |
| 913 | unsigned int iio_get_channel_ext_info_count(struct iio_channel *chan) |
| 914 | { |
| 915 | const struct iio_chan_spec_ext_info *ext_info; |
| 916 | unsigned int i = 0; |
| 917 | |
| 918 | if (!chan->channel->ext_info) |
| 919 | return i; |
| 920 | |
| 921 | for (ext_info = chan->channel->ext_info; ext_info->name; ext_info++) |
| 922 | ++i; |
| 923 | |
| 924 | return i; |
| 925 | } |
| 926 | EXPORT_SYMBOL_GPL(iio_get_channel_ext_info_count); |
| 927 | |
| 928 | static const struct iio_chan_spec_ext_info *iio_lookup_ext_info( |
| 929 | const struct iio_channel *chan, |
| 930 | const char *attr) |
| 931 | { |
| 932 | const struct iio_chan_spec_ext_info *ext_info; |
| 933 | |
| 934 | if (!chan->channel->ext_info) |
| 935 | return NULL; |
| 936 | |
| 937 | for (ext_info = chan->channel->ext_info; ext_info->name; ++ext_info) { |
| 938 | if (!strcmp(attr, ext_info->name)) |
| 939 | return ext_info; |
| 940 | } |
| 941 | |
| 942 | return NULL; |
| 943 | } |
| 944 | |
| 945 | ssize_t iio_read_channel_ext_info(struct iio_channel *chan, |
| 946 | const char *attr, char *buf) |
| 947 | { |
| 948 | const struct iio_chan_spec_ext_info *ext_info; |
| 949 | |
| 950 | ext_info = iio_lookup_ext_info(chan, attr); |
| 951 | if (!ext_info) |
| 952 | return -EINVAL; |
| 953 | |
| 954 | return ext_info->read(chan->indio_dev, ext_info->private, |
| 955 | chan->channel, buf); |
| 956 | } |
| 957 | EXPORT_SYMBOL_GPL(iio_read_channel_ext_info); |
| 958 | |
| 959 | ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr, |
| 960 | const char *buf, size_t len) |
| 961 | { |
| 962 | const struct iio_chan_spec_ext_info *ext_info; |
| 963 | |
| 964 | ext_info = iio_lookup_ext_info(chan, attr); |
| 965 | if (!ext_info) |
| 966 | return -EINVAL; |
| 967 | |
| 968 | return ext_info->write(chan->indio_dev, ext_info->private, |
| 969 | chan->channel, buf, len); |
| 970 | } |
| 971 | EXPORT_SYMBOL_GPL(iio_write_channel_ext_info); |