| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* The industrial I/O core in kernel channel mapping |
| 3 |
* |
| 4 |
* Copyright (c) 2011 Jonathan Cameron |
| 5 |
*/ |
| 6 |
#include <linux/cleanup.h> |
| 7 |
#include <linux/err.h> |
| 8 |
#include <linux/export.h> |
| 9 |
#include <linux/minmax.h> |
| 10 |
#include <linux/mm.h> |
| 11 |
#include <linux/mutex.h> |
| 12 |
#include <linux/property.h> |
| 13 |
#include <linux/slab.h> |
| 14 |
#include <linux/units.h> |
| 15 |
|
| 16 |
#include <linux/iio/iio.h> |
| 17 |
#include <linux/iio/iio-opaque.h> |
| 18 |
#include "iio_core.h" |
| 19 |
#include <linux/iio/machine.h> |
| 20 |
#include <linux/iio/driver.h> |
| 21 |
#include <linux/iio/consumer.h> |
| 22 |
|
| 23 |
struct iio_map_internal { |
| 24 |
struct iio_dev *indio_dev; |
| 25 |
const struct iio_map *map; |
| 26 |
struct list_head l; |
| 27 |
}; |
| 28 |
|
| 29 |
static LIST_HEAD(iio_map_list); |
| 30 |
static DEFINE_MUTEX(iio_map_list_lock); |
| 31 |
|
| 32 |
static int iio_map_array_unregister_locked(struct iio_dev *indio_dev) |
| 33 |
{ |
| 34 |
int ret = -ENODEV; |
| 35 |
struct iio_map_internal *mapi, *next; |
| 36 |
|
| 37 |
list_for_each_entry_safe(mapi, next, &iio_map_list, l) { |
| 38 |
if (indio_dev == mapi->indio_dev) { |
| 39 |
list_del(&mapi->l); |
| 40 |
kfree(mapi); |
| 41 |
ret = 0; |
| 42 |
} |
| 43 |
} |
| 44 |
return ret; |
| 45 |
} |
| 46 |
|
| 47 |
int iio_map_array_register(struct iio_dev *indio_dev, const struct iio_map *maps) |
| 48 |
{ |
| 49 |
struct iio_map_internal *mapi; |
| 50 |
int i = 0; |
| 51 |
int ret; |
| 52 |
|
| 53 |
if (!maps) |
| 54 |
return 0; |
| 55 |
|
| 56 |
guard(mutex)(&iio_map_list_lock); |
| 57 |
while (maps[i].consumer_dev_name) { |
| 58 |
mapi = kzalloc_obj(*mapi); |
| 59 |
if (!mapi) { |
| 60 |
ret = -ENOMEM; |
| 61 |
goto error_ret; |
| 62 |
} |
| 63 |
mapi->map = &maps[i]; |
| 64 |
mapi->indio_dev = indio_dev; |
| 65 |
list_add_tail(&mapi->l, &iio_map_list); |
| 66 |
i++; |
| 67 |
} |
| 68 |
|
| 69 |
return 0; |
| 70 |
error_ret: |
| 71 |
iio_map_array_unregister_locked(indio_dev); |
| 72 |
return ret; |
| 73 |
} |
| 74 |
EXPORT_SYMBOL_GPL(iio_map_array_register); |
| 75 |
|
| 76 |
/* |
| 77 |
* Remove all map entries associated with the given iio device |
| 78 |
*/ |
| 79 |
int iio_map_array_unregister(struct iio_dev *indio_dev) |
| 80 |
{ |
| 81 |
guard(mutex)(&iio_map_list_lock); |
| 82 |
return iio_map_array_unregister_locked(indio_dev); |
| 83 |
} |
| 84 |
EXPORT_SYMBOL_GPL(iio_map_array_unregister); |
| 85 |
|
| 86 |
static void iio_map_array_unregister_cb(void *indio_dev) |
| 87 |
{ |
| 88 |
iio_map_array_unregister(indio_dev); |
| 89 |
} |
| 90 |
|
| 91 |
int devm_iio_map_array_register(struct device *dev, struct iio_dev *indio_dev, |
| 92 |
const struct iio_map *maps) |
| 93 |
{ |
| 94 |
int ret; |
| 95 |
|
| 96 |
ret = iio_map_array_register(indio_dev, maps); |
| 97 |
if (ret) |
| 98 |
return ret; |
| 99 |
|
| 100 |
return devm_add_action_or_reset(dev, iio_map_array_unregister_cb, indio_dev); |
| 101 |
} |
| 102 |
EXPORT_SYMBOL_GPL(devm_iio_map_array_register); |
| 103 |
|
| 104 |
static const struct iio_chan_spec |
| 105 |
*iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name) |
| 106 |
{ |
| 107 |
int i; |
| 108 |
const struct iio_chan_spec *chan = NULL; |
| 109 |
|
| 110 |
for (i = 0; i < indio_dev->num_channels; i++) |
| 111 |
if (indio_dev->channels[i].datasheet_name && |
| 112 |
strcmp(name, indio_dev->channels[i].datasheet_name) == 0) { |
| 113 |
chan = &indio_dev->channels[i]; |
| 114 |
break; |
| 115 |
} |
| 116 |
return chan; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* __fwnode_iio_simple_xlate - translate iiospec to the IIO channel index |
| 121 |
* @indio_dev: pointer to the iio_dev structure |
| 122 |
* @iiospec: IIO specifier as found in the device tree |
| 123 |
* |
| 124 |
* This is simple translation function, suitable for the most 1:1 mapped |
| 125 |
* channels in IIO chips. This function performs only one sanity check: |
| 126 |
* whether IIO index is less than num_channels (that is specified in the |
| 127 |
* iio_dev). |
| 128 |
*/ |
| 129 |
static int __fwnode_iio_simple_xlate(struct iio_dev *indio_dev, |
| 130 |
const struct fwnode_reference_args *iiospec) |
| 131 |
{ |
| 132 |
if (!iiospec->nargs) |
| 133 |
return 0; |
| 134 |
|
| 135 |
if (iiospec->args[0] >= indio_dev->num_channels) { |
| 136 |
dev_err(&indio_dev->dev, "invalid channel index %llu\n", |
| 137 |
iiospec->args[0]); |
| 138 |
return -EINVAL; |
| 139 |
} |
| 140 |
|
| 141 |
return iiospec->args[0]; |
| 142 |
} |
| 143 |
|
| 144 |
static int __fwnode_iio_channel_get(struct iio_channel *channel, |
| 145 |
struct fwnode_handle *fwnode, int index) |
| 146 |
{ |
| 147 |
struct fwnode_reference_args iiospec; |
| 148 |
struct device *idev; |
| 149 |
struct iio_dev *indio_dev; |
| 150 |
int err; |
| 151 |
|
| 152 |
err = fwnode_property_get_reference_args(fwnode, "io-channels", |
| 153 |
"#io-channel-cells", 0, |
| 154 |
index, &iiospec); |
| 155 |
if (err) |
| 156 |
return err; |
| 157 |
|
| 158 |
idev = bus_find_device_by_fwnode(&iio_bus_type, iiospec.fwnode); |
| 159 |
if (!idev) { |
| 160 |
fwnode_handle_put(iiospec.fwnode); |
| 161 |
return -EPROBE_DEFER; |
| 162 |
} |
| 163 |
|
| 164 |
indio_dev = dev_to_iio_dev(idev); |
| 165 |
channel->indio_dev = indio_dev; |
| 166 |
if (indio_dev->info->fwnode_xlate) |
| 167 |
index = indio_dev->info->fwnode_xlate(indio_dev, &iiospec); |
| 168 |
else |
| 169 |
index = __fwnode_iio_simple_xlate(indio_dev, &iiospec); |
| 170 |
fwnode_handle_put(iiospec.fwnode); |
| 171 |
if (index < 0) |
| 172 |
goto err_put; |
| 173 |
channel->channel = &indio_dev->channels[index]; |
| 174 |
|
| 175 |
return 0; |
| 176 |
|
| 177 |
err_put: |
| 178 |
iio_device_put(indio_dev); |
| 179 |
return index; |
| 180 |
} |
| 181 |
|
| 182 |
static struct iio_channel *fwnode_iio_channel_get(struct fwnode_handle *fwnode, |
| 183 |
int index) |
| 184 |
{ |
| 185 |
int err; |
| 186 |
|
| 187 |
if (index < 0) |
| 188 |
return ERR_PTR(-EINVAL); |
| 189 |
|
| 190 |
struct iio_channel *channel __free(kfree) = |
| 191 |
kzalloc_obj(*channel); |
| 192 |
if (!channel) |
| 193 |
return ERR_PTR(-ENOMEM); |
| 194 |
|
| 195 |
err = __fwnode_iio_channel_get(channel, fwnode, index); |
| 196 |
if (err) |
| 197 |
return ERR_PTR(err); |
| 198 |
|
| 199 |
return_ptr(channel); |
| 200 |
} |
| 201 |
|
| 202 |
static struct iio_channel * |
| 203 |
__fwnode_iio_channel_get_by_name(struct fwnode_handle *fwnode, const char *name) |
| 204 |
{ |
| 205 |
struct iio_channel *chan; |
| 206 |
int index = 0; |
| 207 |
|
| 208 |
/* |
| 209 |
* For named iio channels, first look up the name in the |
| 210 |
* "io-channel-names" property. If it cannot be found, the |
| 211 |
* index will be an error code, and fwnode_iio_channel_get() |
| 212 |
* will fail. |
| 213 |
*/ |
| 214 |
if (name) |
| 215 |
index = fwnode_property_match_string(fwnode, "io-channel-names", |
| 216 |
name); |
| 217 |
|
| 218 |
chan = fwnode_iio_channel_get(fwnode, index); |
| 219 |
if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER) |
| 220 |
return chan; |
| 221 |
if (name) { |
| 222 |
if (index >= 0) { |
| 223 |
pr_err("ERROR: could not get IIO channel %pfw:%s(%i)\n", |
| 224 |
fwnode, name, index); |
| 225 |
/* |
| 226 |
* In this case, we found 'name' in 'io-channel-names' |
| 227 |
* but somehow we still fail so that we should not proceed |
| 228 |
* with any other lookup. Hence, explicitly return -EINVAL |
| 229 |
* (maybe not the better error code) so that the caller |
| 230 |
* won't do a system lookup. |
| 231 |
*/ |
| 232 |
return ERR_PTR(-EINVAL); |
| 233 |
} |
| 234 |
/* |
| 235 |
* If index < 0, then fwnode_property_get_reference_args() fails |
| 236 |
* with -EINVAL or -ENOENT (ACPI case) which is expected. We |
| 237 |
* should not proceed if we get any other error. |
| 238 |
*/ |
| 239 |
if (PTR_ERR(chan) != -EINVAL && PTR_ERR(chan) != -ENOENT) |
| 240 |
return chan; |
| 241 |
} else if (PTR_ERR(chan) != -ENOENT) { |
| 242 |
/* |
| 243 |
* if !name, then we should only proceed the lookup if |
| 244 |
* fwnode_property_get_reference_args() returns -ENOENT. |
| 245 |
*/ |
| 246 |
return chan; |
| 247 |
} |
| 248 |
|
| 249 |
/* so we continue the lookup */ |
| 250 |
return ERR_PTR(-ENODEV); |
| 251 |
} |
| 252 |
|
| 253 |
struct iio_channel *fwnode_iio_channel_get_by_name(struct fwnode_handle *fwnode, |
| 254 |
const char *name) |
| 255 |
{ |
| 256 |
struct fwnode_handle *parent; |
| 257 |
struct iio_channel *chan; |
| 258 |
|
| 259 |
/* Walk up the tree of devices looking for a matching iio channel */ |
| 260 |
chan = __fwnode_iio_channel_get_by_name(fwnode, name); |
| 261 |
if (!IS_ERR(chan) || PTR_ERR(chan) != -ENODEV) |
| 262 |
return chan; |
| 263 |
|
| 264 |
/* |
| 265 |
* No matching IIO channel found on this node. |
| 266 |
* If the parent node has a "io-channel-ranges" property, |
| 267 |
* then we can try one of its channels. |
| 268 |
*/ |
| 269 |
fwnode_for_each_parent_node(fwnode, parent) { |
| 270 |
if (!fwnode_property_present(parent, "io-channel-ranges")) { |
| 271 |
fwnode_handle_put(parent); |
| 272 |
return ERR_PTR(-ENODEV); |
| 273 |
} |
| 274 |
|
| 275 |
chan = __fwnode_iio_channel_get_by_name(parent, name); |
| 276 |
if (!IS_ERR(chan) || PTR_ERR(chan) != -ENODEV) { |
| 277 |
fwnode_handle_put(parent); |
| 278 |
return chan; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
return ERR_PTR(-ENODEV); |
| 283 |
} |
| 284 |
EXPORT_SYMBOL_GPL(fwnode_iio_channel_get_by_name); |
| 285 |
|
| 286 |
static struct iio_channel *fwnode_iio_channel_get_all(struct device *dev) |
| 287 |
{ |
| 288 |
struct fwnode_handle *fwnode = dev_fwnode(dev); |
| 289 |
int i, mapind, nummaps = 0; |
| 290 |
int ret; |
| 291 |
|
| 292 |
do { |
| 293 |
ret = fwnode_property_get_reference_args(fwnode, "io-channels", |
| 294 |
"#io-channel-cells", 0, |
| 295 |
nummaps, NULL); |
| 296 |
if (ret < 0) |
| 297 |
break; |
| 298 |
} while (++nummaps); |
| 299 |
|
| 300 |
if (nummaps == 0) |
| 301 |
return ERR_PTR(-ENODEV); |
| 302 |
|
| 303 |
/* NULL terminated array to save passing size */ |
| 304 |
struct iio_channel *chans __free(kfree) = |
| 305 |
kzalloc_objs(*chans, nummaps + 1); |
| 306 |
if (!chans) |
| 307 |
return ERR_PTR(-ENOMEM); |
| 308 |
|
| 309 |
/* Search for FW matches */ |
| 310 |
for (mapind = 0; mapind < nummaps; mapind++) { |
| 311 |
ret = __fwnode_iio_channel_get(&chans[mapind], fwnode, mapind); |
| 312 |
if (ret) |
| 313 |
goto error_free_chans; |
| 314 |
} |
| 315 |
return_ptr(chans); |
| 316 |
|
| 317 |
error_free_chans: |
| 318 |
for (i = 0; i < mapind; i++) |
| 319 |
iio_device_put(chans[i].indio_dev); |
| 320 |
return ERR_PTR(ret); |
| 321 |
} |
| 322 |
|
| 323 |
static struct iio_channel *iio_channel_get_sys(const char *name, |
| 324 |
const char *channel_name) |
| 325 |
{ |
| 326 |
struct iio_map_internal *c_i = NULL, *c = NULL; |
| 327 |
int err; |
| 328 |
|
| 329 |
if (!(name || channel_name)) |
| 330 |
return ERR_PTR(-ENODEV); |
| 331 |
|
| 332 |
/* first find matching entry the channel map */ |
| 333 |
scoped_guard(mutex, &iio_map_list_lock) { |
| 334 |
list_for_each_entry(c_i, &iio_map_list, l) { |
| 335 |
if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) || |
| 336 |
(channel_name && |
| 337 |
strcmp(channel_name, c_i->map->consumer_channel) != 0)) |
| 338 |
continue; |
| 339 |
c = c_i; |
| 340 |
iio_device_get(c->indio_dev); |
| 341 |
break; |
| 342 |
} |
| 343 |
} |
| 344 |
if (!c) |
| 345 |
return ERR_PTR(-ENODEV); |
| 346 |
|
| 347 |
struct iio_channel *channel __free(kfree) = |
| 348 |
kzalloc(sizeof(*channel), GFP_KERNEL); |
| 349 |
if (!channel) { |
| 350 |
err = -ENOMEM; |
| 351 |
goto error_no_mem; |
| 352 |
} |
| 353 |
|
| 354 |
channel->indio_dev = c->indio_dev; |
| 355 |
|
| 356 |
if (c->map->adc_channel_label) { |
| 357 |
channel->channel = |
| 358 |
iio_chan_spec_from_name(channel->indio_dev, |
| 359 |
c->map->adc_channel_label); |
| 360 |
|
| 361 |
if (!channel->channel) { |
| 362 |
err = -EINVAL; |
| 363 |
goto error_no_mem; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
return_ptr(channel); |
| 368 |
|
| 369 |
error_no_mem: |
| 370 |
iio_device_put(c->indio_dev); |
| 371 |
return ERR_PTR(err); |
| 372 |
} |
| 373 |
|
| 374 |
struct iio_channel *iio_channel_get(struct device *dev, |
| 375 |
const char *channel_name) |
| 376 |
{ |
| 377 |
const char *name = dev ? dev_name(dev) : NULL; |
| 378 |
struct iio_channel *channel; |
| 379 |
|
| 380 |
if (dev) { |
| 381 |
channel = fwnode_iio_channel_get_by_name(dev_fwnode(dev), |
| 382 |
channel_name); |
| 383 |
if (!IS_ERR(channel) || PTR_ERR(channel) != -ENODEV) |
| 384 |
return channel; |
| 385 |
} |
| 386 |
|
| 387 |
return iio_channel_get_sys(name, channel_name); |
| 388 |
} |
| 389 |
EXPORT_SYMBOL_GPL(iio_channel_get); |
| 390 |
|
| 391 |
void iio_channel_release(struct iio_channel *channel) |
| 392 |
{ |
| 393 |
if (!channel) |
| 394 |
return; |
| 395 |
iio_device_put(channel->indio_dev); |
| 396 |
kfree(channel); |
| 397 |
} |
| 398 |
EXPORT_SYMBOL_GPL(iio_channel_release); |
| 399 |
|
| 400 |
static void devm_iio_channel_free(void *iio_channel) |
| 401 |
{ |
| 402 |
iio_channel_release(iio_channel); |
| 403 |
} |
| 404 |
|
| 405 |
struct iio_channel *devm_iio_channel_get(struct device *dev, |
| 406 |
const char *channel_name) |
| 407 |
{ |
| 408 |
struct iio_channel *channel; |
| 409 |
int ret; |
| 410 |
|
| 411 |
channel = iio_channel_get(dev, channel_name); |
| 412 |
if (IS_ERR(channel)) |
| 413 |
return channel; |
| 414 |
|
| 415 |
ret = devm_add_action_or_reset(dev, devm_iio_channel_free, channel); |
| 416 |
if (ret) |
| 417 |
return ERR_PTR(ret); |
| 418 |
|
| 419 |
return channel; |
| 420 |
} |
| 421 |
EXPORT_SYMBOL_GPL(devm_iio_channel_get); |
| 422 |
|
| 423 |
struct iio_channel *devm_fwnode_iio_channel_get_by_name(struct device *dev, |
| 424 |
struct fwnode_handle *fwnode, |
| 425 |
const char *channel_name) |
| 426 |
{ |
| 427 |
struct iio_channel *channel; |
| 428 |
int ret; |
| 429 |
|
| 430 |
channel = fwnode_iio_channel_get_by_name(fwnode, channel_name); |
| 431 |
if (IS_ERR(channel)) |
| 432 |
return channel; |
| 433 |
|
| 434 |
ret = devm_add_action_or_reset(dev, devm_iio_channel_free, channel); |
| 435 |
if (ret) |
| 436 |
return ERR_PTR(ret); |
| 437 |
|
| 438 |
return channel; |
| 439 |
} |
| 440 |
EXPORT_SYMBOL_GPL(devm_fwnode_iio_channel_get_by_name); |
| 441 |
|
| 442 |
struct iio_channel *iio_channel_get_all(struct device *dev) |
| 443 |
{ |
| 444 |
const char *name; |
| 445 |
struct iio_map_internal *c = NULL; |
| 446 |
struct iio_channel *fw_chans; |
| 447 |
int nummaps = 0; |
| 448 |
int mapind = 0; |
| 449 |
int i, ret; |
| 450 |
|
| 451 |
if (!dev) |
| 452 |
return ERR_PTR(-EINVAL); |
| 453 |
|
| 454 |
fw_chans = fwnode_iio_channel_get_all(dev); |
| 455 |
/* |
| 456 |
* We only want to carry on if the error is -ENODEV. Anything else |
| 457 |
* should be reported up the stack. |
| 458 |
*/ |
| 459 |
if (!IS_ERR(fw_chans) || PTR_ERR(fw_chans) != -ENODEV) |
| 460 |
return fw_chans; |
| 461 |
|
| 462 |
name = dev_name(dev); |
| 463 |
|
| 464 |
guard(mutex)(&iio_map_list_lock); |
| 465 |
/* first count the matching maps */ |
| 466 |
list_for_each_entry(c, &iio_map_list, l) |
| 467 |
if (name && strcmp(name, c->map->consumer_dev_name) != 0) |
| 468 |
continue; |
| 469 |
else |
| 470 |
nummaps++; |
| 471 |
|
| 472 |
if (nummaps == 0) |
| 473 |
return ERR_PTR(-ENODEV); |
| 474 |
|
| 475 |
/* NULL terminated array to save passing size */ |
| 476 |
struct iio_channel *chans __free(kfree) = |
| 477 |
kzalloc_objs(*chans, nummaps + 1); |
| 478 |
if (!chans) |
| 479 |
return ERR_PTR(-ENOMEM); |
| 480 |
|
| 481 |
/* for each map fill in the chans element */ |
| 482 |
list_for_each_entry(c, &iio_map_list, l) { |
| 483 |
if (name && strcmp(name, c->map->consumer_dev_name) != 0) |
| 484 |
continue; |
| 485 |
chans[mapind].indio_dev = c->indio_dev; |
| 486 |
chans[mapind].data = c->map->consumer_data; |
| 487 |
chans[mapind].channel = |
| 488 |
iio_chan_spec_from_name(chans[mapind].indio_dev, |
| 489 |
c->map->adc_channel_label); |
| 490 |
if (!chans[mapind].channel) { |
| 491 |
ret = -EINVAL; |
| 492 |
goto error_free_chans; |
| 493 |
} |
| 494 |
iio_device_get(chans[mapind].indio_dev); |
| 495 |
mapind++; |
| 496 |
} |
| 497 |
if (mapind == 0) { |
| 498 |
ret = -ENODEV; |
| 499 |
goto error_free_chans; |
| 500 |
} |
| 501 |
|
| 502 |
return_ptr(chans); |
| 503 |
|
| 504 |
error_free_chans: |
| 505 |
for (i = 0; i < mapind; i++) |
| 506 |
iio_device_put(chans[i].indio_dev); |
| 507 |
return ERR_PTR(ret); |
| 508 |
} |
| 509 |
EXPORT_SYMBOL_GPL(iio_channel_get_all); |
| 510 |
|
| 511 |
void iio_channel_release_all(struct iio_channel *channels) |
| 512 |
{ |
| 513 |
struct iio_channel *chan = &channels[0]; |
| 514 |
|
| 515 |
while (chan->indio_dev) { |
| 516 |
iio_device_put(chan->indio_dev); |
| 517 |
chan++; |
| 518 |
} |
| 519 |
kfree(channels); |
| 520 |
} |
| 521 |
EXPORT_SYMBOL_GPL(iio_channel_release_all); |
| 522 |
|
| 523 |
static void devm_iio_channel_free_all(void *iio_channels) |
| 524 |
{ |
| 525 |
iio_channel_release_all(iio_channels); |
| 526 |
} |
| 527 |
|
| 528 |
struct iio_channel *devm_iio_channel_get_all(struct device *dev) |
| 529 |
{ |
| 530 |
struct iio_channel *channels; |
| 531 |
int ret; |
| 532 |
|
| 533 |
channels = iio_channel_get_all(dev); |
| 534 |
if (IS_ERR(channels)) |
| 535 |
return channels; |
| 536 |
|
| 537 |
ret = devm_add_action_or_reset(dev, devm_iio_channel_free_all, |
| 538 |
channels); |
| 539 |
if (ret) |
| 540 |
return ERR_PTR(ret); |
| 541 |
|
| 542 |
return channels; |
| 543 |
} |
| 544 |
EXPORT_SYMBOL_GPL(devm_iio_channel_get_all); |
| 545 |
|
| 546 |
static int iio_channel_read(struct iio_channel *chan, int *val, int *val2, |
| 547 |
enum iio_chan_info_enum info) |
| 548 |
{ |
| 549 |
const struct iio_info *iio_info = chan->indio_dev->info; |
| 550 |
int unused; |
| 551 |
int vals[INDIO_MAX_RAW_ELEMENTS]; |
| 552 |
int ret; |
| 553 |
int val_len = 2; |
| 554 |
|
| 555 |
if (!val2) |
| 556 |
val2 = &unused; |
| 557 |
|
| 558 |
if (!iio_channel_has_info(chan->channel, info)) |
| 559 |
return -EINVAL; |
| 560 |
|
| 561 |
if (iio_info->read_raw_multi) { |
| 562 |
ret = iio_info->read_raw_multi(chan->indio_dev, |
| 563 |
chan->channel, |
| 564 |
INDIO_MAX_RAW_ELEMENTS, |
| 565 |
vals, &val_len, info); |
| 566 |
*val = vals[0]; |
| 567 |
*val2 = vals[1]; |
| 568 |
} else if (iio_info->read_raw) { |
| 569 |
ret = iio_info->read_raw(chan->indio_dev, |
| 570 |
chan->channel, val, val2, info); |
| 571 |
} else { |
| 572 |
return -EINVAL; |
| 573 |
} |
| 574 |
|
| 575 |
return ret; |
| 576 |
} |
| 577 |
|
| 578 |
int iio_read_channel_raw(struct iio_channel *chan, int *val) |
| 579 |
{ |
| 580 |
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); |
| 581 |
|
| 582 |
guard(mutex)(&iio_dev_opaque->info_exist_lock); |
| 583 |
if (!chan->indio_dev->info) |
| 584 |
return -ENODEV; |
| 585 |
|
| 586 |
return iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW); |
| 587 |
} |
| 588 |
EXPORT_SYMBOL_GPL(iio_read_channel_raw); |
| 589 |
|
| 590 |
int iio_read_channel_average_raw(struct iio_channel *chan, int *val) |
| 591 |
{ |
| 592 |
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); |
| 593 |
|
| 594 |
guard(mutex)(&iio_dev_opaque->info_exist_lock); |
| 595 |
if (!chan->indio_dev->info) |
| 596 |
return -ENODEV; |
| 597 |
|
| 598 |
return iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW); |
| 599 |
} |
| 600 |
EXPORT_SYMBOL_GPL(iio_read_channel_average_raw); |
| 601 |
|
| 602 |
int iio_multiply_value(int *result, s64 multiplier, |
| 603 |
unsigned int type, int val, int val2) |
| 604 |
{ |
| 605 |
s64 denominator; |
| 606 |
|
| 607 |
switch (type) { |
| 608 |
case IIO_VAL_INT: |
| 609 |
*result = multiplier * val; |
| 610 |
return IIO_VAL_INT; |
| 611 |
case IIO_VAL_INT_PLUS_MICRO: |
| 612 |
case IIO_VAL_INT_PLUS_NANO: |
| 613 |
switch (type) { |
| 614 |
case IIO_VAL_INT_PLUS_MICRO: |
| 615 |
denominator = MICRO; |
| 616 |
break; |
| 617 |
case IIO_VAL_INT_PLUS_NANO: |
| 618 |
denominator = NANO; |
| 619 |
break; |
| 620 |
} |
| 621 |
*result = multiplier * abs(val); |
| 622 |
*result += div_s64(multiplier * abs(val2), denominator); |
| 623 |
if (val < 0 || val2 < 0) |
| 624 |
*result *= -1; |
| 625 |
return IIO_VAL_INT; |
| 626 |
case IIO_VAL_FRACTIONAL: |
| 627 |
*result = div_s64(multiplier * val, val2); |
| 628 |
return IIO_VAL_INT; |
| 629 |
case IIO_VAL_FRACTIONAL_LOG2: |
| 630 |
*result = (multiplier * val) >> val2; |
| 631 |
return IIO_VAL_INT; |
| 632 |
default: |
| 633 |
return -EINVAL; |
| 634 |
} |
| 635 |
} |
| 636 |
EXPORT_SYMBOL_NS_GPL(iio_multiply_value, "IIO_UNIT_TEST"); |
| 637 |
|
| 638 |
static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan, |
| 639 |
int raw, int *processed, |
| 640 |
unsigned int scale) |
| 641 |
{ |
| 642 |
int scale_type, scale_val, scale_val2; |
| 643 |
int offset_type, offset_val, offset_val2; |
| 644 |
s64 raw64 = raw; |
| 645 |
int ret; |
| 646 |
|
| 647 |
offset_type = iio_channel_read(chan, &offset_val, &offset_val2, |
| 648 |
IIO_CHAN_INFO_OFFSET); |
| 649 |
if (offset_type >= 0) { |
| 650 |
switch (offset_type) { |
| 651 |
case IIO_VAL_INT: |
| 652 |
break; |
| 653 |
case IIO_VAL_INT_PLUS_MICRO: |
| 654 |
case IIO_VAL_INT_PLUS_NANO: |
| 655 |
/* |
| 656 |
* Both IIO_VAL_INT_PLUS_MICRO and IIO_VAL_INT_PLUS_NANO |
| 657 |
* implicitely truncate the offset to it's integer form. |
| 658 |
*/ |
| 659 |
break; |
| 660 |
case IIO_VAL_FRACTIONAL: |
| 661 |
offset_val /= offset_val2; |
| 662 |
break; |
| 663 |
case IIO_VAL_FRACTIONAL_LOG2: |
| 664 |
offset_val >>= offset_val2; |
| 665 |
break; |
| 666 |
default: |
| 667 |
return -EINVAL; |
| 668 |
} |
| 669 |
|
| 670 |
raw64 += offset_val; |
| 671 |
} |
| 672 |
|
| 673 |
scale_type = iio_channel_read(chan, &scale_val, &scale_val2, |
| 674 |
IIO_CHAN_INFO_SCALE); |
| 675 |
if (scale_type < 0) { |
| 676 |
/* |
| 677 |
* If no channel scaling is available apply consumer scale to |
| 678 |
* raw value and return. |
| 679 |
*/ |
| 680 |
*processed = raw64 * scale; |
| 681 |
return 0; |
| 682 |
} |
| 683 |
|
| 684 |
ret = iio_multiply_value(processed, raw64 * scale, |
| 685 |
scale_type, scale_val, scale_val2); |
| 686 |
if (ret < 0) |
| 687 |
return ret; |
| 688 |
|
| 689 |
return 0; |
| 690 |
} |
| 691 |
|
| 692 |
int iio_convert_raw_to_processed(struct iio_channel *chan, int raw, |
| 693 |
int *processed, unsigned int scale) |
| 694 |
{ |
| 695 |
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); |
| 696 |
|
| 697 |
guard(mutex)(&iio_dev_opaque->info_exist_lock); |
| 698 |
if (!chan->indio_dev->info) |
| 699 |
return -ENODEV; |
| 700 |
|
| 701 |
return iio_convert_raw_to_processed_unlocked(chan, raw, processed, |
| 702 |
scale); |
| 703 |
} |
| 704 |
EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed); |
| 705 |
|
| 706 |
int iio_read_channel_attribute(struct iio_channel *chan, int *val, int *val2, |
| 707 |
enum iio_chan_info_enum attribute) |
| 708 |
{ |
| 709 |
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); |
| 710 |
|
| 711 |
guard(mutex)(&iio_dev_opaque->info_exist_lock); |
| 712 |
if (!chan->indio_dev->info) |
| 713 |
return -ENODEV; |
| 714 |
|
| 715 |
return iio_channel_read(chan, val, val2, attribute); |
| 716 |
} |
| 717 |
EXPORT_SYMBOL_GPL(iio_read_channel_attribute); |
| 718 |
|
| 719 |
int iio_read_channel_offset(struct iio_channel *chan, int *val, int *val2) |
| 720 |
{ |
| 721 |
return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_OFFSET); |
| 722 |
} |
| 723 |
EXPORT_SYMBOL_GPL(iio_read_channel_offset); |
| 724 |
|
| 725 |
int iio_read_channel_processed_scale(struct iio_channel *chan, int *val, |
| 726 |
unsigned int scale) |
| 727 |
{ |
| 728 |
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); |
| 729 |
int ret, pval, pval2; |
| 730 |
|
| 731 |
guard(mutex)(&iio_dev_opaque->info_exist_lock); |
| 732 |
if (!chan->indio_dev->info) |
| 733 |
return -ENODEV; |
| 734 |
|
| 735 |
if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) { |
| 736 |
ret = iio_channel_read(chan, &pval, &pval2, |
| 737 |
IIO_CHAN_INFO_PROCESSED); |
| 738 |
if (ret < 0) |
| 739 |
return ret; |
| 740 |
|
| 741 |
ret = iio_multiply_value(val, scale, ret, pval, pval2); |
| 742 |
if (ret < 0) |
| 743 |
return ret; |
| 744 |
|
| 745 |
return 0; |
| 746 |
} else { |
| 747 |
ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW); |
| 748 |
if (ret < 0) |
| 749 |
return ret; |
| 750 |
|
| 751 |
return iio_convert_raw_to_processed_unlocked(chan, *val, val, |
| 752 |
scale); |
| 753 |
} |
| 754 |
} |
| 755 |
EXPORT_SYMBOL_GPL(iio_read_channel_processed_scale); |
| 756 |
|
| 757 |
int iio_read_channel_processed(struct iio_channel *chan, int *val) |
| 758 |
{ |
| 759 |
/* This is just a special case with scale factor 1 */ |
| 760 |
return iio_read_channel_processed_scale(chan, val, 1); |
| 761 |
} |
| 762 |
EXPORT_SYMBOL_GPL(iio_read_channel_processed); |
| 763 |
|
| 764 |
int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2) |
| 765 |
{ |
| 766 |
return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_SCALE); |
| 767 |
} |
| 768 |
EXPORT_SYMBOL_GPL(iio_read_channel_scale); |
| 769 |
|
| 770 |
static int iio_channel_read_avail(struct iio_channel *chan, |
| 771 |
const int **vals, int *type, int *length, |
| 772 |
enum iio_chan_info_enum info) |
| 773 |
{ |
| 774 |
const struct iio_info *iio_info = chan->indio_dev->info; |
| 775 |
|
| 776 |
if (!iio_channel_has_available(chan->channel, info)) |
| 777 |
return -EINVAL; |
| 778 |
|
| 779 |
if (iio_info->read_avail) |
| 780 |
return iio_info->read_avail(chan->indio_dev, chan->channel, |
| 781 |
vals, type, length, info); |
| 782 |
return -EINVAL; |
| 783 |
} |
| 784 |
|
| 785 |
int iio_read_avail_channel_attribute(struct iio_channel *chan, |
| 786 |
const int **vals, int *type, int *length, |
| 787 |
enum iio_chan_info_enum attribute) |
| 788 |
{ |
| 789 |
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); |
| 790 |
|
| 791 |
guard(mutex)(&iio_dev_opaque->info_exist_lock); |
| 792 |
if (!chan->indio_dev->info) |
| 793 |
return -ENODEV; |
| 794 |
|
| 795 |
return iio_channel_read_avail(chan, vals, type, length, attribute); |
| 796 |
} |
| 797 |
EXPORT_SYMBOL_GPL(iio_read_avail_channel_attribute); |
| 798 |
|
| 799 |
int iio_read_avail_channel_raw(struct iio_channel *chan, |
| 800 |
const int **vals, int *length) |
| 801 |
{ |
| 802 |
int ret; |
| 803 |
int type; |
| 804 |
|
| 805 |
ret = iio_read_avail_channel_attribute(chan, vals, &type, length, |
| 806 |
IIO_CHAN_INFO_RAW); |
| 807 |
|
| 808 |
if (ret >= 0 && type != IIO_VAL_INT) |
| 809 |
/* raw values are assumed to be IIO_VAL_INT */ |
| 810 |
ret = -EINVAL; |
| 811 |
|
| 812 |
return ret; |
| 813 |
} |
| 814 |
EXPORT_SYMBOL_GPL(iio_read_avail_channel_raw); |
| 815 |
|
| 816 |
static int iio_channel_read_max(struct iio_channel *chan, |
| 817 |
int *val, int *val2, int *type, |
| 818 |
enum iio_chan_info_enum info) |
| 819 |
{ |
| 820 |
const int *vals; |
| 821 |
int length; |
| 822 |
int ret; |
| 823 |
|
| 824 |
ret = iio_channel_read_avail(chan, &vals, type, &length, info); |
| 825 |
if (ret < 0) |
| 826 |
return ret; |
| 827 |
|
| 828 |
switch (ret) { |
| 829 |
case IIO_AVAIL_RANGE: |
| 830 |
switch (*type) { |
| 831 |
case IIO_VAL_INT: |
| 832 |
*val = vals[2]; |
| 833 |
break; |
| 834 |
default: |
| 835 |
*val = vals[4]; |
| 836 |
if (val2) |
| 837 |
*val2 = vals[5]; |
| 838 |
} |
| 839 |
return 0; |
| 840 |
|
| 841 |
case IIO_AVAIL_LIST: |
| 842 |
if (length <= 0) |
| 843 |
return -EINVAL; |
| 844 |
switch (*type) { |
| 845 |
case IIO_VAL_INT: |
| 846 |
*val = max_array(vals, length); |
| 847 |
break; |
| 848 |
default: |
| 849 |
/* TODO: learn about max for other iio values */ |
| 850 |
return -EINVAL; |
| 851 |
} |
| 852 |
return 0; |
| 853 |
|
| 854 |
default: |
| 855 |
return -EINVAL; |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
int iio_read_max_channel_raw(struct iio_channel *chan, int *val) |
| 860 |
{ |
| 861 |
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); |
| 862 |
int type; |
| 863 |
|
| 864 |
guard(mutex)(&iio_dev_opaque->info_exist_lock); |
| 865 |
if (!chan->indio_dev->info) |
| 866 |
return -ENODEV; |
| 867 |
|
| 868 |
return iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW); |
| 869 |
} |
| 870 |
EXPORT_SYMBOL_GPL(iio_read_max_channel_raw); |
| 871 |
|
| 872 |
static int iio_channel_read_min(struct iio_channel *chan, |
| 873 |
int *val, int *val2, int *type, |
| 874 |
enum iio_chan_info_enum info) |
| 875 |
{ |
| 876 |
const int *vals; |
| 877 |
int length; |
| 878 |
int ret; |
| 879 |
|
| 880 |
ret = iio_channel_read_avail(chan, &vals, type, &length, info); |
| 881 |
if (ret < 0) |
| 882 |
return ret; |
| 883 |
|
| 884 |
switch (ret) { |
| 885 |
case IIO_AVAIL_RANGE: |
| 886 |
switch (*type) { |
| 887 |
case IIO_VAL_INT: |
| 888 |
*val = vals[0]; |
| 889 |
break; |
| 890 |
default: |
| 891 |
*val = vals[0]; |
| 892 |
if (val2) |
| 893 |
*val2 = vals[1]; |
| 894 |
} |
| 895 |
return 0; |
| 896 |
|
| 897 |
case IIO_AVAIL_LIST: |
| 898 |
if (length <= 0) |
| 899 |
return -EINVAL; |
| 900 |
switch (*type) { |
| 901 |
case IIO_VAL_INT: |
| 902 |
*val = min_array(vals, length); |
| 903 |
break; |
| 904 |
default: |
| 905 |
/* TODO: learn about min for other iio values */ |
| 906 |
return -EINVAL; |
| 907 |
} |
| 908 |
return 0; |
| 909 |
|
| 910 |
default: |
| 911 |
return -EINVAL; |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
int iio_read_min_channel_raw(struct iio_channel *chan, int *val) |
| 916 |
{ |
| 917 |
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); |
| 918 |
int type; |
| 919 |
|
| 920 |
guard(mutex)(&iio_dev_opaque->info_exist_lock); |
| 921 |
if (!chan->indio_dev->info) |
| 922 |
return -ENODEV; |
| 923 |
|
| 924 |
return iio_channel_read_min(chan, val, NULL, &type, IIO_CHAN_INFO_RAW); |
| 925 |
} |
| 926 |
EXPORT_SYMBOL_GPL(iio_read_min_channel_raw); |
| 927 |
|
| 928 |
int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type) |
| 929 |
{ |
| 930 |
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); |
| 931 |
|
| 932 |
guard(mutex)(&iio_dev_opaque->info_exist_lock); |
| 933 |
if (!chan->indio_dev->info) |
| 934 |
return -ENODEV; |
| 935 |
|
| 936 |
*type = chan->channel->type; |
| 937 |
|
| 938 |
return 0; |
| 939 |
} |
| 940 |
EXPORT_SYMBOL_GPL(iio_get_channel_type); |
| 941 |
|
| 942 |
static int iio_channel_write(struct iio_channel *chan, int val, int val2, |
| 943 |
enum iio_chan_info_enum info) |
| 944 |
{ |
| 945 |
const struct iio_info *iio_info = chan->indio_dev->info; |
| 946 |
|
| 947 |
if (iio_info->write_raw) |
| 948 |
return iio_info->write_raw(chan->indio_dev, |
| 949 |
chan->channel, val, val2, info); |
| 950 |
return -EINVAL; |
| 951 |
} |
| 952 |
|
| 953 |
int iio_write_channel_attribute(struct iio_channel *chan, int val, int val2, |
| 954 |
enum iio_chan_info_enum attribute) |
| 955 |
{ |
| 956 |
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); |
| 957 |
|
| 958 |
guard(mutex)(&iio_dev_opaque->info_exist_lock); |
| 959 |
if (!chan->indio_dev->info) |
| 960 |
return -ENODEV; |
| 961 |
|
| 962 |
return iio_channel_write(chan, val, val2, attribute); |
| 963 |
} |
| 964 |
EXPORT_SYMBOL_GPL(iio_write_channel_attribute); |
| 965 |
|
| 966 |
int iio_write_channel_raw(struct iio_channel *chan, int val) |
| 967 |
{ |
| 968 |
return iio_write_channel_attribute(chan, val, 0, IIO_CHAN_INFO_RAW); |
| 969 |
} |
| 970 |
EXPORT_SYMBOL_GPL(iio_write_channel_raw); |
| 971 |
|
| 972 |
unsigned int iio_get_channel_ext_info_count(struct iio_channel *chan) |
| 973 |
{ |
| 974 |
const struct iio_chan_spec_ext_info *ext_info; |
| 975 |
unsigned int i = 0; |
| 976 |
|
| 977 |
if (!chan->channel->ext_info) |
| 978 |
return i; |
| 979 |
|
| 980 |
for (ext_info = chan->channel->ext_info; ext_info->name; ext_info++) |
| 981 |
++i; |
| 982 |
|
| 983 |
return i; |
| 984 |
} |
| 985 |
EXPORT_SYMBOL_GPL(iio_get_channel_ext_info_count); |
| 986 |
|
| 987 |
static const struct iio_chan_spec_ext_info * |
| 988 |
iio_lookup_ext_info(const struct iio_channel *chan, const char *attr) |
| 989 |
{ |
| 990 |
const struct iio_chan_spec_ext_info *ext_info; |
| 991 |
|
| 992 |
if (!chan->channel->ext_info) |
| 993 |
return NULL; |
| 994 |
|
| 995 |
for (ext_info = chan->channel->ext_info; ext_info->name; ++ext_info) { |
| 996 |
if (!strcmp(attr, ext_info->name)) |
| 997 |
return ext_info; |
| 998 |
} |
| 999 |
|
| 1000 |
return NULL; |
| 1001 |
} |
| 1002 |
|
| 1003 |
ssize_t iio_read_channel_ext_info(struct iio_channel *chan, |
| 1004 |
const char *attr, char *buf) |
| 1005 |
{ |
| 1006 |
const struct iio_chan_spec_ext_info *ext_info; |
| 1007 |
|
| 1008 |
if (!buf || offset_in_page(buf)) { |
| 1009 |
pr_err("iio: invalid ext_info read buffer\n"); |
| 1010 |
return -EINVAL; |
| 1011 |
} |
| 1012 |
|
| 1013 |
ext_info = iio_lookup_ext_info(chan, attr); |
| 1014 |
if (!ext_info) |
| 1015 |
return -EINVAL; |
| 1016 |
|
| 1017 |
return ext_info->read(chan->indio_dev, ext_info->private, |
| 1018 |
chan->channel, buf); |
| 1019 |
} |
| 1020 |
EXPORT_SYMBOL_GPL(iio_read_channel_ext_info); |
| 1021 |
|
| 1022 |
ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr, |
| 1023 |
const char *buf, size_t len) |
| 1024 |
{ |
| 1025 |
const struct iio_chan_spec_ext_info *ext_info; |
| 1026 |
|
| 1027 |
ext_info = iio_lookup_ext_info(chan, attr); |
| 1028 |
if (!ext_info) |
| 1029 |
return -EINVAL; |
| 1030 |
|
| 1031 |
return ext_info->write(chan->indio_dev, ext_info->private, |
| 1032 |
chan->channel, buf, len); |
| 1033 |
} |
| 1034 |
EXPORT_SYMBOL_GPL(iio_write_channel_ext_info); |
| 1035 |
|
| 1036 |
ssize_t iio_read_channel_label(struct iio_channel *chan, char *buf) |
| 1037 |
{ |
| 1038 |
if (!buf || offset_in_page(buf)) { |
| 1039 |
pr_err("iio: invalid label read buffer\n"); |
| 1040 |
return -EINVAL; |
| 1041 |
} |
| 1042 |
|
| 1043 |
return do_iio_read_channel_label(chan->indio_dev, chan->channel, buf); |
| 1044 |
} |
| 1045 |
EXPORT_SYMBOL_GPL(iio_read_channel_label); |