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/err.h> 7 #include <linux/export.h> 8 #include <linux/slab.h> 9 #include <linux/mutex.h> 10 #include <linux/of.h> 11 12 #include <linux/iio/iio.h> 13 #include <linux/iio/iio-opaque.h> 14 #include "iio_core.h" 15 #include <linux/iio/machine.h> 16 #include <linux/iio/driver.h> 17 #include <linux/iio/consumer.h> 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 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 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; 60 list_add_tail(&mapi->l, &iio_map_list); 61 i++; 62 } 63 error_ret: 64 if (ret) 65 iio_map_array_unregister_locked(indio_dev); 66 mutex_unlock(&iio_map_list_lock); 67 68 return ret; 69 } 70 EXPORT_SYMBOL_GPL(iio_map_array_register); 71 72 73 /* 74 * Remove all map entries associated with the given iio device 75 */ 76 int iio_map_array_unregister(struct iio_dev *indio_dev) 77 { 78 int ret; 79 80 mutex_lock(&iio_map_list_lock); 81 ret = iio_map_array_unregister_locked(indio_dev); 82 mutex_unlock(&iio_map_list_lock); 83 84 return ret; 85 } 86 EXPORT_SYMBOL_GPL(iio_map_array_unregister); 87 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 105 static const struct iio_chan_spec 106 *iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name) 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 120 #ifdef CONFIG_OF 121 122 static int iio_dev_node_match(struct device *dev, const void *data) 123 { 124 return dev->of_node == data && dev->type == &iio_device_type; 125 } 126 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 143 if (iiospec->args[0] >= indio_dev->num_channels) { 144 dev_err(&indio_dev->dev, "invalid channel index %u\n", 145 iiospec->args[0]); 146 return -EINVAL; 147 } 148 149 return iiospec->args[0]; 150 } 151 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; 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) 179 goto err_put; 180 channel->channel = &indio_dev->channels[index]; 181 182 return 0; 183 184 err_put: 185 iio_device_put(indio_dev); 186 return index; 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 212 struct iio_channel *of_iio_channel_get_by_name(struct device_node *np, 213 const char *name) 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); 231 if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER) 232 break; 233 else if (name && index >= 0) { 234 pr_err("ERROR: could not get IIO channel %pOF:%s(%i)\n", 235 np, name ? name : "", index); 236 return NULL; 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)) 246 return NULL; 247 } 248 249 return chan; 250 } 251 EXPORT_SYMBOL_GPL(of_iio_channel_get_by_name); 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 294 static inline struct iio_channel *of_iio_channel_get_all(struct device *dev) 295 { 296 return NULL; 297 } 298 299 #endif /* CONFIG_OF */ 300 301 static struct iio_channel *iio_channel_get_sys(const char *name, 302 const char *channel_name) 303 { 304 struct iio_map_internal *c_i = NULL, *c = NULL; 305 struct iio_channel *channel; 306 int err; 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; 319 iio_device_get(c->indio_dev); 320 break; 321 } 322 mutex_unlock(&iio_map_list_lock); 323 if (c == NULL) 324 return ERR_PTR(-ENODEV); 325 326 channel = kzalloc(sizeof(*channel), GFP_KERNEL); 327 if (channel == NULL) { 328 err = -ENOMEM; 329 goto error_no_mem; 330 } 331 332 channel->indio_dev = c->indio_dev; 333 334 if (c->map->adc_channel_label) { 335 channel->channel = 336 iio_chan_spec_from_name(channel->indio_dev, 337 c->map->adc_channel_label); 338 339 if (channel->channel == NULL) { 340 err = -EINVAL; 341 goto error_no_chan; 342 } 343 } 344 345 return channel; 346 347 error_no_chan: 348 kfree(channel); 349 error_no_mem: 350 iio_device_put(c->indio_dev); 351 return ERR_PTR(err); 352 } 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; 358 struct iio_channel *channel; 359 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 } 366 367 return iio_channel_get_sys(name, channel_name); 368 } 369 EXPORT_SYMBOL_GPL(iio_channel_get); 370 371 void iio_channel_release(struct iio_channel *channel) 372 { 373 if (!channel) 374 return; 375 iio_device_put(channel->indio_dev); 376 kfree(channel); 377 } 378 EXPORT_SYMBOL_GPL(iio_channel_release); 379 380 static void devm_iio_channel_free(void *iio_channel) 381 { 382 iio_channel_release(iio_channel); 383 } 384 385 struct iio_channel *devm_iio_channel_get(struct device *dev, 386 const char *channel_name) 387 { 388 struct iio_channel *channel; 389 int ret; 390 391 channel = iio_channel_get(dev, channel_name); 392 if (IS_ERR(channel)) 393 return channel; 394 395 ret = devm_add_action_or_reset(dev, devm_iio_channel_free, channel); 396 if (ret) 397 return ERR_PTR(ret); 398 399 return channel; 400 } 401 EXPORT_SYMBOL_GPL(devm_iio_channel_get); 402 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 { 407 struct iio_channel *channel; 408 int ret; 409 410 channel = of_iio_channel_get_by_name(np, channel_name); 411 if (IS_ERR(channel)) 412 return channel; 413 414 ret = devm_add_action_or_reset(dev, devm_iio_channel_free, channel); 415 if (ret) 416 return ERR_PTR(ret); 417 418 return channel; 419 } 420 EXPORT_SYMBOL_GPL(devm_of_iio_channel_get_by_name); 421 422 struct iio_channel *iio_channel_get_all(struct device *dev) 423 { 424 const char *name; 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 431 if (dev == NULL) 432 return ERR_PTR(-EINVAL); 433 434 chans = of_iio_channel_get_all(dev); 435 if (chans) 436 return chans; 437 438 name = dev_name(dev); 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 */ 454 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL); 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; 465 chans[mapind].data = c->map->consumer_data; 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; 471 goto error_free_chans; 472 } 473 iio_device_get(chans[mapind].indio_dev); 474 mapind++; 475 } 476 if (mapind == 0) { 477 ret = -ENODEV; 478 goto error_free_chans; 479 } 480 mutex_unlock(&iio_map_list_lock); 481 482 return chans; 483 484 error_free_chans: 485 for (i = 0; i < nummaps; i++) 486 iio_device_put(chans[i].indio_dev); 487 kfree(chans); 488 error_ret: 489 mutex_unlock(&iio_map_list_lock); 490 491 return ERR_PTR(ret); 492 } 493 EXPORT_SYMBOL_GPL(iio_channel_get_all); 494 495 void iio_channel_release_all(struct iio_channel *channels) 496 { 497 struct iio_channel *chan = &channels[0]; 498 499 while (chan->indio_dev) { 500 iio_device_put(chan->indio_dev); 501 chan++; 502 } 503 kfree(channels); 504 } 505 EXPORT_SYMBOL_GPL(iio_channel_release_all); 506 507 static void devm_iio_channel_free_all(void *iio_channels) 508 { 509 iio_channel_release_all(iio_channels); 510 } 511 512 struct iio_channel *devm_iio_channel_get_all(struct device *dev) 513 { 514 struct iio_channel *channels; 515 int ret; 516 517 channels = iio_channel_get_all(dev); 518 if (IS_ERR(channels)) 519 return channels; 520 521 ret = devm_add_action_or_reset(dev, devm_iio_channel_free_all, 522 channels); 523 if (ret) 524 return ERR_PTR(ret); 525 526 return channels; 527 } 528 EXPORT_SYMBOL_GPL(devm_iio_channel_get_all); 529 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; 534 int vals[INDIO_MAX_RAW_ELEMENTS]; 535 int ret; 536 int val_len = 2; 537 538 if (val2 == NULL) 539 val2 = &unused; 540 541 if (!iio_channel_has_info(chan->channel, info)) 542 return -EINVAL; 543 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; 555 } 556 557 int iio_read_channel_raw(struct iio_channel *chan, int *val) 558 { 559 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 560 int ret; 561 562 mutex_lock(&iio_dev_opaque->info_exist_lock); 563 if (chan->indio_dev->info == NULL) { 564 ret = -ENODEV; 565 goto err_unlock; 566 } 567 568 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW); 569 err_unlock: 570 mutex_unlock(&iio_dev_opaque->info_exist_lock); 571 572 return ret; 573 } 574 EXPORT_SYMBOL_GPL(iio_read_channel_raw); 575 576 int iio_read_channel_average_raw(struct iio_channel *chan, int *val) 577 { 578 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 579 int ret; 580 581 mutex_lock(&iio_dev_opaque->info_exist_lock); 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: 589 mutex_unlock(&iio_dev_opaque->info_exist_lock); 590 591 return ret; 592 } 593 EXPORT_SYMBOL_GPL(iio_read_channel_average_raw); 594 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 602 ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_OFFSET); 603 if (ret >= 0) 604 raw64 += offset; 605 606 scale_type = iio_channel_read(chan, &scale_val, &scale_val2, 607 IIO_CHAN_INFO_SCALE); 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 } 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; 641 case IIO_VAL_FRACTIONAL_LOG2: 642 *processed = (raw64 * (s64)scale_val * scale) >> scale_val2; 643 break; 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 { 654 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 655 int ret; 656 657 mutex_lock(&iio_dev_opaque->info_exist_lock); 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: 666 mutex_unlock(&iio_dev_opaque->info_exist_lock); 667 668 return ret; 669 } 670 EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed); 671 672 int iio_read_channel_attribute(struct iio_channel *chan, int *val, int *val2, 673 enum iio_chan_info_enum attribute) 674 { 675 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 676 int ret; 677 678 mutex_lock(&iio_dev_opaque->info_exist_lock); 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: 686 mutex_unlock(&iio_dev_opaque->info_exist_lock); 687 688 return ret; 689 } 690 EXPORT_SYMBOL_GPL(iio_read_channel_attribute); 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 698 int iio_read_channel_processed_scale(struct iio_channel *chan, int *val, 699 unsigned int scale) 700 { 701 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 702 int ret; 703 704 mutex_lock(&iio_dev_opaque->info_exist_lock); 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); 713 if (ret < 0) 714 goto err_unlock; 715 *val *= scale; 716 } else { 717 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW); 718 if (ret < 0) 719 goto err_unlock; 720 ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 721 scale); 722 } 723 724 err_unlock: 725 mutex_unlock(&iio_dev_opaque->info_exist_lock); 726 727 return ret; 728 } 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 } 736 EXPORT_SYMBOL_GPL(iio_read_channel_processed); 737 738 int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2) 739 { 740 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_SCALE); 741 } 742 EXPORT_SYMBOL_GPL(iio_read_channel_scale); 743 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 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 { 759 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 760 int ret; 761 762 mutex_lock(&iio_dev_opaque->info_exist_lock); 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: 770 mutex_unlock(&iio_dev_opaque->info_exist_lock); 771 772 return ret; 773 } 774 EXPORT_SYMBOL_GPL(iio_read_avail_channel_attribute); 775 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 782 ret = iio_read_avail_channel_attribute(chan, vals, &type, length, 783 IIO_CHAN_INFO_RAW); 784 785 if (ret >= 0 && type != IIO_VAL_INT) 786 /* raw values are assumed to be IIO_VAL_INT */ 787 ret = -EINVAL; 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 { 842 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 843 int ret; 844 int type; 845 846 mutex_lock(&iio_dev_opaque->info_exist_lock); 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: 854 mutex_unlock(&iio_dev_opaque->info_exist_lock); 855 856 return ret; 857 } 858 EXPORT_SYMBOL_GPL(iio_read_max_channel_raw); 859 860 int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type) 861 { 862 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 863 int ret = 0; 864 /* Need to verify underlying driver has not gone away */ 865 866 mutex_lock(&iio_dev_opaque->info_exist_lock); 867 if (chan->indio_dev->info == NULL) { 868 ret = -ENODEV; 869 goto err_unlock; 870 } 871 872 *type = chan->channel->type; 873 err_unlock: 874 mutex_unlock(&iio_dev_opaque->info_exist_lock); 875 876 return ret; 877 } 878 EXPORT_SYMBOL_GPL(iio_get_channel_type); 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 887 int iio_write_channel_attribute(struct iio_channel *chan, int val, int val2, 888 enum iio_chan_info_enum attribute) 889 { 890 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 891 int ret; 892 893 mutex_lock(&iio_dev_opaque->info_exist_lock); 894 if (chan->indio_dev->info == NULL) { 895 ret = -ENODEV; 896 goto err_unlock; 897 } 898 899 ret = iio_channel_write(chan, val, val2, attribute); 900 err_unlock: 901 mutex_unlock(&iio_dev_opaque->info_exist_lock); 902 903 return ret; 904 } 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 } 911 EXPORT_SYMBOL_GPL(iio_write_channel_raw); 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); 972