1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Framework to handle complex IIO aggregate devices.
 *
 * The typical architecture is to have one device as the frontend device which
 * can be "linked" against one or multiple backend devices. All the IIO and
 * userspace interface is expected to be registers/managed by the frontend
 * device which will callback into the backends when needed (to get/set some
 * configuration that it does not directly control).
 *
 *                                           -------------------------------------------------------
 * ------------------                        | ------------         ------------      -------  FPGA|
 * |     ADC        |------------------------| | ADC CORE |---------| DMA CORE |------| RAM |      |
 * | (Frontend/IIO) | Serial Data (eg: LVDS) | |(backend) |---------|          |------|     |      |
 * |                |------------------------| ------------         ------------      -------      |
 * ------------------                        -------------------------------------------------------
 *
 * The framework interface is pretty simple:
 *   - Backends should register themselves with devm_iio_backend_register()
 *   - Frontend devices should get backends with devm_iio_backend_get()
 *
 * Also to note that the primary target for this framework are converters like
 * ADC/DACs so iio_backend_ops will have some operations typical of converter
 * devices. On top of that, this is "generic" for all IIO which means any kind
 * of device can make use of the framework. That said, If the iio_backend_ops
 * struct begins to grow out of control, we can always refactor things so that
 * the industrialio-backend.c is only left with the really generic stuff. Then,
 * we can build on top of it depending on the needs.
 *
 * Copyright (C) 2023-2024 Analog Devices Inc.
 */
#define dev_fmt(fmt) "iio-backend: " fmt

#include <linux/cleanup.h>
#include <linux/debugfs.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/property.h>
#include <linux/slab.h>
#include <linux/stringify.h>
#include <linux/types.h>

#include <linux/iio/backend.h>
#include <linux/iio/iio.h>

struct iio_backend {
	struct list_head entry;
	const struct iio_backend_ops *ops;
	struct device *frontend_dev;
	struct device *dev;
	struct module *owner;
	void *priv;
	const char *name;
	unsigned int cached_reg_addr;
	u32 caps;
	/*
	 * This index is relative to the frontend. Meaning that for
	 * frontends with multiple backends, this will be the index of this
	 * backend. Used for the debugfs directory name.
	 */
	u8 idx;
};

/*
 * Helper struct for requesting buffers. This ensures that we have all data
 * that we need to free the buffer in a device managed action.
 */
struct iio_backend_buffer_pair {
	struct iio_backend *back;
	struct iio_buffer *buffer;
};

static LIST_HEAD(iio_back_list);
static DEFINE_MUTEX(iio_back_lock);

/*
 * Helper macros to call backend ops. Makes sure the option is supported.
 */
#define iio_backend_check_op(back, op) ({ \
	struct iio_backend *____back = back;				\
	int ____ret = 0;						\
									\
	if (!____back->ops->op)						\
		____ret = -EOPNOTSUPP;					\
									\
	____ret;							\
})

#define iio_backend_op_call(back, op, args...) ({		\
	struct iio_backend *__back = back;			\
	int __ret;						\
								\
	__ret = iio_backend_check_op(__back, op);		\
	if (!__ret)						\
		__ret = __back->ops->op(__back, ##args);	\
								\
	__ret;							\
})

#define iio_backend_ptr_op_call(back, op, args...) ({		\
	struct iio_backend *__back = back;			\
	void *ptr_err;						\
	int __ret;						\
								\
	__ret = iio_backend_check_op(__back, op);		\
	if (__ret)						\
		ptr_err = ERR_PTR(__ret);			\
	else							\
		ptr_err = __back->ops->op(__back, ##args);	\
								\
	ptr_err;						\
})

#define iio_backend_void_op_call(back, op, args...) {		\
	struct iio_backend *__back = back;			\
	int __ret;						\
								\
	__ret = iio_backend_check_op(__back, op);		\
	if (!__ret)						\
		__back->ops->op(__back, ##args);		\
	else							\
		dev_dbg(__back->dev, "Op(%s) not implemented\n",\
			__stringify(op));			\
}

static ssize_t iio_backend_debugfs_read_reg(struct file *file,
					    char __user *userbuf,
					    size_t count, loff_t *ppos)
{
	struct iio_backend *back = file->private_data;
	char read_buf[20];
	unsigned int val;
	int ret, len;

	ret = iio_backend_op_call(back, debugfs_reg_access,
				  back->cached_reg_addr, 0, &val);
	if (ret)
		return ret;

	len = scnprintf(read_buf, sizeof(read_buf), "0x%X\n", val);

	return simple_read_from_buffer(userbuf, count, ppos, read_buf, len);
}

static ssize_t iio_backend_debugfs_write_reg(struct file *file,
					     const char __user *userbuf,
					     size_t count, loff_t *ppos)
{
	struct iio_backend *back = file->private_data;
	unsigned int val;
	char buf[80];
	ssize_t rc;
	int ret;

	if (*ppos != 0 || count >= sizeof(buf))
		return -ENOSPC;

	rc = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, userbuf, count);
	if (rc < 0)
		return rc;

	buf[rc] = '\0';

	ret = sscanf(buf, "%i %i", &back->cached_reg_addr, &val);

	switch (ret) {
	case 1:
		return count;
	case 2:
		ret = iio_backend_op_call(back, debugfs_reg_access,
					  back->cached_reg_addr, val, NULL);
		if (ret)
			return ret;
		return count;
	default:
		return -EINVAL;
	}
}

static const struct file_operations iio_backend_debugfs_reg_fops = {
	.open = simple_open,
	.read = iio_backend_debugfs_read_reg,
	.write = iio_backend_debugfs_write_reg,
};

static ssize_t iio_backend_debugfs_read_name(struct file *file,
					     char __user *userbuf,
					     size_t count, loff_t *ppos)
{
	struct iio_backend *back = file->private_data;
	char name[128];
	int len;

	len = scnprintf(name, sizeof(name), "%s\n", back->name);

	return simple_read_from_buffer(userbuf, count, ppos, name, len);
}

static const struct file_operations iio_backend_debugfs_name_fops = {
	.open = simple_open,
	.read = iio_backend_debugfs_read_name,
};

/**
 * iio_backend_debugfs_add - Add debugfs interfaces for Backends
 * @back: Backend device
 * @indio_dev: IIO device
 */
void iio_backend_debugfs_add(struct iio_backend *back,
			     struct iio_dev *indio_dev)
{
	struct dentry *d = iio_get_debugfs_dentry(indio_dev);
	struct dentry *back_d;
	char name[128];

	if (!IS_ENABLED(CONFIG_DEBUG_FS) || !d)
		return;
	if (!back->ops->debugfs_reg_access && !back->name)
		return;

	snprintf(name, sizeof(name), "backend%d", back->idx);

	back_d = debugfs_create_dir(name, d);
	if (IS_ERR(back_d))
		return;

	if (back->ops->debugfs_reg_access)
		debugfs_create_file("direct_reg_access", 0600, back_d, back,
				    &iio_backend_debugfs_reg_fops);

	if (back->name)
		debugfs_create_file("name", 0400, back_d, back,
				    &iio_backend_debugfs_name_fops);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_debugfs_add, "IIO_BACKEND");

/**
 * iio_backend_debugfs_print_chan_status - Print channel status
 * @back: Backend device
 * @chan: Channel number
 * @buf: Buffer where to print the status
 * @len: Available space
 *
 * One usecase where this is useful is for testing test tones in a digital
 * interface and "ask" the backend to dump more details on why a test tone might
 * have errors.
 *
 * RETURNS:
 * Number of copied bytes on success, negative error code on failure.
 */
ssize_t iio_backend_debugfs_print_chan_status(struct iio_backend *back,
					      unsigned int chan, char *buf,
					      size_t len)
{
	if (!IS_ENABLED(CONFIG_DEBUG_FS))
		return -ENODEV;

	return iio_backend_op_call(back, debugfs_print_chan_status, chan, buf,
				   len);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_debugfs_print_chan_status, "IIO_BACKEND");

/**
 * iio_backend_chan_enable - Enable a backend channel
 * @back: Backend device
 * @chan: Channel number
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_chan_enable(struct iio_backend *back, unsigned int chan)
{
	return iio_backend_op_call(back, chan_enable, chan);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_chan_enable, "IIO_BACKEND");

/**
 * iio_backend_chan_disable - Disable a backend channel
 * @back: Backend device
 * @chan: Channel number
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_chan_disable(struct iio_backend *back, unsigned int chan)
{
	return iio_backend_op_call(back, chan_disable, chan);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_chan_disable, "IIO_BACKEND");

static void __iio_backend_disable(void *back)
{
	iio_backend_void_op_call(back, disable);
}

/**
 * iio_backend_disable - Backend disable
 * @back: Backend device
 */
void iio_backend_disable(struct iio_backend *back)
{
	__iio_backend_disable(back);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_disable, "IIO_BACKEND");

/**
 * iio_backend_enable - Backend enable
 * @back: Backend device
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_enable(struct iio_backend *back)
{
	return iio_backend_op_call(back, enable);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_enable, "IIO_BACKEND");

/**
 * devm_iio_backend_enable - Device managed backend enable
 * @dev: Consumer device for the backend
 * @back: Backend device
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int devm_iio_backend_enable(struct device *dev, struct iio_backend *back)
{
	int ret;

	ret = iio_backend_enable(back);
	if (ret)
		return ret;

	return devm_add_action_or_reset(dev, __iio_backend_disable, back);
}
EXPORT_SYMBOL_NS_GPL(devm_iio_backend_enable, "IIO_BACKEND");

/**
 * iio_backend_data_format_set - Configure the channel data format
 * @back: Backend device
 * @chan: Channel number
 * @data: Data format
 *
 * Properly configure a channel with respect to the expected data format. A
 * @struct iio_backend_data_fmt must be passed with the settings.
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_data_format_set(struct iio_backend *back, unsigned int chan,
				const struct iio_backend_data_fmt *data)
{
	if (!data || data->type >= IIO_BACKEND_DATA_TYPE_MAX)
		return -EINVAL;

	return iio_backend_op_call(back, data_format_set, chan, data);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_data_format_set, "IIO_BACKEND");

/**
 * iio_backend_data_source_set - Select data source
 * @back: Backend device
 * @chan: Channel number
 * @data: Data source
 *
 * A given backend may have different sources to stream/sync data. This allows
 * to choose that source.
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_data_source_set(struct iio_backend *back, unsigned int chan,
				enum iio_backend_data_source data)
{
	if (data >= IIO_BACKEND_DATA_SOURCE_MAX)
		return -EINVAL;

	return iio_backend_op_call(back, data_source_set, chan, data);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_data_source_set, "IIO_BACKEND");

/**
 * iio_backend_data_source_get - Get current data source
 * @back: Backend device
 * @chan: Channel number
 * @data: Pointer to receive the current source value
 *
 * A given backend may have different sources to stream/sync data. This allows
 * to know what source is in use.
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_data_source_get(struct iio_backend *back, unsigned int chan,
				enum iio_backend_data_source *data)
{
	int ret;

	ret = iio_backend_op_call(back, data_source_get, chan, data);
	if (ret)
		return ret;

	if (*data >= IIO_BACKEND_DATA_SOURCE_MAX)
		return -EINVAL;

	return 0;
}
EXPORT_SYMBOL_NS_GPL(iio_backend_data_source_get, "IIO_BACKEND");

/**
 * iio_backend_set_sampling_freq - Set channel sampling rate
 * @back: Backend device
 * @chan: Channel number
 * @sample_rate_hz: Sample rate
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_set_sampling_freq(struct iio_backend *back, unsigned int chan,
				  u64 sample_rate_hz)
{
	return iio_backend_op_call(back, set_sample_rate, chan, sample_rate_hz);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_set_sampling_freq, "IIO_BACKEND");

/**
 * iio_backend_test_pattern_set - Configure a test pattern
 * @back: Backend device
 * @chan: Channel number
 * @pattern: Test pattern
 *
 * Configure a test pattern on the backend. This is typically used for
 * calibrating the timings on the data digital interface.
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_test_pattern_set(struct iio_backend *back,
				 unsigned int chan,
				 enum iio_backend_test_pattern pattern)
{
	if (pattern >= IIO_BACKEND_TEST_PATTERN_MAX)
		return -EINVAL;

	return iio_backend_op_call(back, test_pattern_set, chan, pattern);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_test_pattern_set, "IIO_BACKEND");

/**
 * iio_backend_chan_status - Get the channel status
 * @back: Backend device
 * @chan: Channel number
 * @error: Error indication
 *
 * Get the current state of the backend channel. Typically used to check if
 * there were any errors sending/receiving data.
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_chan_status(struct iio_backend *back, unsigned int chan,
			    bool *error)
{
	return iio_backend_op_call(back, chan_status, chan, error);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_chan_status, "IIO_BACKEND");

/**
 * iio_backend_iodelay_set - Set digital I/O delay
 * @back: Backend device
 * @lane: Lane number
 * @taps: Number of taps
 *
 * Controls delays on sending/receiving data. One usecase for this is to
 * calibrate the data digital interface so we get the best results when
 * transferring data. Note that @taps has no unit since the actual delay per tap
 * is very backend specific. Hence, frontend devices typically should go through
 * an array of @taps (the size of that array should typically match the size of
 * calibration points on the frontend device) and call this API.
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_iodelay_set(struct iio_backend *back, unsigned int lane,
			    unsigned int taps)
{
	return iio_backend_op_call(back, iodelay_set, lane, taps);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_iodelay_set, "IIO_BACKEND");

/**
 * iio_backend_data_sample_trigger - Control when to sample data
 * @back: Backend device
 * @trigger: Data trigger
 *
 * Mostly useful for input backends. Configures the backend for when to sample
 * data (eg: rising vs falling edge).
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_data_sample_trigger(struct iio_backend *back,
				    enum iio_backend_sample_trigger trigger)
{
	if (trigger >= IIO_BACKEND_SAMPLE_TRIGGER_MAX)
		return -EINVAL;

	return iio_backend_op_call(back, data_sample_trigger, trigger);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_data_sample_trigger, "IIO_BACKEND");

static void iio_backend_free_buffer(void *arg)
{
	struct iio_backend_buffer_pair *pair = arg;

	iio_backend_void_op_call(pair->back, free_buffer, pair->buffer);
}

/**
 * devm_iio_backend_request_buffer - Device managed buffer request
 * @dev: Consumer device for the backend
 * @back: Backend device
 * @indio_dev: IIO device
 *
 * Request an IIO buffer from the backend. The type of the buffer (typically
 * INDIO_BUFFER_HARDWARE) is up to the backend to decide. This is because,
 * normally, the backend dictates what kind of buffering we can get.
 *
 * The backend .free_buffer() hooks is automatically called on @dev detach.
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int devm_iio_backend_request_buffer(struct device *dev,
				    struct iio_backend *back,
				    struct iio_dev *indio_dev)
{
	struct iio_backend_buffer_pair *pair;
	struct iio_buffer *buffer;

	pair = devm_kzalloc(dev, sizeof(*pair), GFP_KERNEL);
	if (!pair)
		return -ENOMEM;

	buffer = iio_backend_ptr_op_call(back, request_buffer, indio_dev);
	if (IS_ERR(buffer))
		return PTR_ERR(buffer);

	/* weak reference should be all what we need */
	pair->back = back;
	pair->buffer = buffer;

	return devm_add_action_or_reset(dev, iio_backend_free_buffer, pair);
}
EXPORT_SYMBOL_NS_GPL(devm_iio_backend_request_buffer, "IIO_BACKEND");

/**
 * iio_backend_read_raw - Read a channel attribute from a backend device.
 * @back:	Backend device
 * @chan:	IIO channel reference
 * @val:	First returned value
 * @val2:	Second returned value
 * @mask:	Specify the attribute to return
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_read_raw(struct iio_backend *back,
			 struct iio_chan_spec const *chan, int *val, int *val2,
			 long mask)
{
	return iio_backend_op_call(back, read_raw, chan, val, val2, mask);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_read_raw, "IIO_BACKEND");

static struct iio_backend *iio_backend_from_indio_dev_parent(const struct device *dev)
{
	struct iio_backend *back = ERR_PTR(-ENODEV), *iter;

	/*
	 * We deliberately go through all backends even after finding a match.
	 * The reason is that we want to catch frontend devices which have more
	 * than one backend in which case returning the first we find is bogus.
	 * For those cases, frontends need to explicitly define
	 * get_iio_backend() in struct iio_info.
	 */
	guard(mutex)(&iio_back_lock);
	list_for_each_entry(iter, &iio_back_list, entry) {
		if (dev == iter->frontend_dev) {
			if (!IS_ERR(back)) {
				dev_warn(dev,
					 "Multiple backends! get_iio_backend() needs to be implemented");
				return ERR_PTR(-ENODEV);
			}

			back = iter;
		}
	}

	return back;
}

/**
 * iio_backend_ext_info_get - IIO ext_info read callback
 * @indio_dev: IIO device
 * @private: Data private to the driver
 * @chan: IIO channel
 * @buf: Buffer where to place the attribute data
 *
 * This helper is intended to be used by backends that extend an IIO channel
 * (through iio_backend_extend_chan_spec()) with extended info. In that case,
 * backends are not supposed to give their own callbacks (as they would not have
 * a way to get the backend from indio_dev). This is the getter.
 *
 * RETURNS:
 * Number of bytes written to buf, negative error number on failure.
 */
ssize_t iio_backend_ext_info_get(struct iio_dev *indio_dev, uintptr_t private,
				 const struct iio_chan_spec *chan, char *buf)
{
	struct iio_backend *back;

	/*
	 * The below should work for the majority of the cases. It will not work
	 * when one frontend has multiple backends in which case we'll need a
	 * new callback in struct iio_info so we can directly request the proper
	 * backend from the frontend. Anyways, let's only introduce new options
	 * when really needed...
	 */
	back = iio_backend_from_indio_dev_parent(indio_dev->dev.parent);
	if (IS_ERR(back))
		return PTR_ERR(back);

	return iio_backend_op_call(back, ext_info_get, private, chan, buf);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_ext_info_get, "IIO_BACKEND");

/**
 * iio_backend_ext_info_set - IIO ext_info write callback
 * @indio_dev: IIO device
 * @private: Data private to the driver
 * @chan: IIO channel
 * @buf: Buffer holding the sysfs attribute
 * @len: Buffer length
 *
 * This helper is intended to be used by backends that extend an IIO channel
 * (through iio_backend_extend_chan_spec()) with extended info. In that case,
 * backends are not supposed to give their own callbacks (as they would not have
 * a way to get the backend from indio_dev). This is the setter.
 *
 * RETURNS:
 * Buffer length on success, negative error number on failure.
 */
ssize_t iio_backend_ext_info_set(struct iio_dev *indio_dev, uintptr_t private,
				 const struct iio_chan_spec *chan,
				 const char *buf, size_t len)
{
	struct iio_backend *back;

	back = iio_backend_from_indio_dev_parent(indio_dev->dev.parent);
	if (IS_ERR(back))
		return PTR_ERR(back);

	return iio_backend_op_call(back, ext_info_set, private, chan, buf, len);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_ext_info_set, "IIO_BACKEND");

/**
 * iio_backend_interface_type_get - get the interface type used.
 * @back: Backend device
 * @type: Interface type
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_interface_type_get(struct iio_backend *back,
				   enum iio_backend_interface_type *type)
{
	int ret;

	ret = iio_backend_op_call(back, interface_type_get, type);
	if (ret)
		return ret;

	if (*type >= IIO_BACKEND_INTERFACE_MAX)
		return -EINVAL;

	return 0;
}
EXPORT_SYMBOL_NS_GPL(iio_backend_interface_type_get, "IIO_BACKEND");

/**
 * iio_backend_data_size_set - set the data width/size in the data bus.
 * @back: Backend device
 * @size: Size in bits
 *
 * Some frontend devices can dynamically control the word/data size on the
 * interface/data bus. Hence, the backend device needs to be aware of it so
 * data can be correctly transferred.
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_data_size_set(struct iio_backend *back, unsigned int size)
{
	if (!size)
		return -EINVAL;

	return iio_backend_op_call(back, data_size_set, size);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_data_size_set, "IIO_BACKEND");

/**
 * iio_backend_oversampling_ratio_set - set the oversampling ratio
 * @back: Backend device
 * @chan: Channel number
 * @ratio: The oversampling ratio - value 1 corresponds to no oversampling.
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_oversampling_ratio_set(struct iio_backend *back,
				       unsigned int chan,
				       unsigned int ratio)
{
	return iio_backend_op_call(back, oversampling_ratio_set, chan, ratio);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_oversampling_ratio_set, "IIO_BACKEND");

/**
 * iio_backend_extend_chan_spec - Extend an IIO channel
 * @back: Backend device
 * @chan: IIO channel
 *
 * Some backends may have their own functionalities and hence capable of
 * extending a frontend's channel.
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_extend_chan_spec(struct iio_backend *back,
				 struct iio_chan_spec *chan)
{
	const struct iio_chan_spec_ext_info *frontend_ext_info = chan->ext_info;
	const struct iio_chan_spec_ext_info *back_ext_info;
	int ret;

	ret = iio_backend_op_call(back, extend_chan_spec, chan);
	if (ret)
		return ret;
	/*
	 * Let's keep things simple for now. Don't allow to overwrite the
	 * frontend's extended info. If ever needed, we can support appending
	 * it.
	 */
	if (frontend_ext_info && chan->ext_info != frontend_ext_info)
		return -EOPNOTSUPP;
	if (!chan->ext_info)
		return 0;

	/* Don't allow backends to get creative and force their own handlers */
	for (back_ext_info = chan->ext_info; back_ext_info->name; back_ext_info++) {
		if (back_ext_info->read != iio_backend_ext_info_get)
			return -EINVAL;
		if (back_ext_info->write != iio_backend_ext_info_set)
			return -EINVAL;
	}

	return 0;
}
EXPORT_SYMBOL_NS_GPL(iio_backend_extend_chan_spec, "IIO_BACKEND");

/**
 * iio_backend_has_caps - Check if backend has specific capabilities
 * @back: Backend device
 * @caps: Capabilities to check
 *
 * RETURNS:
 * True if backend has all the requested capabilities, false otherwise.
 */
bool iio_backend_has_caps(struct iio_backend *back, u32 caps)
{
	return (back->caps & caps) == caps;
}
EXPORT_SYMBOL_NS_GPL(iio_backend_has_caps, "IIO_BACKEND");

static void iio_backend_release(void *arg)
{
	struct iio_backend *back = arg;

	module_put(back->owner);
}

static int __devm_iio_backend_get(struct device *dev, struct iio_backend *back)
{
	struct device_link *link;
	int ret;

	/*
	 * Make sure the provider cannot be unloaded before the consumer module.
	 * Note that device_links would still guarantee that nothing is
	 * accessible (and breaks) but this makes it explicit that the consumer
	 * module must be also unloaded.
	 */
	if (!try_module_get(back->owner))
		return dev_err_probe(dev, -ENODEV,
				     "Cannot get module reference\n");

	ret = devm_add_action_or_reset(dev, iio_backend_release, back);
	if (ret)
		return ret;

	link = device_link_add(dev, back->dev, DL_FLAG_AUTOREMOVE_CONSUMER);
	if (!link)
		return dev_err_probe(dev, -EINVAL,
				     "Could not link to supplier(%s)\n",
				     dev_name(back->dev));

	back->frontend_dev = dev;

	dev_dbg(dev, "Found backend(%s) device\n", dev_name(back->dev));

	return 0;
}

/**
 * iio_backend_filter_type_set - Set filter type
 * @back: Backend device
 * @type: Filter type.
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_filter_type_set(struct iio_backend *back,
				enum iio_backend_filter_type type)
{
	if (type >= IIO_BACKEND_FILTER_TYPE_MAX)
		return -EINVAL;

	return iio_backend_op_call(back, filter_type_set, type);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_filter_type_set, "IIO_BACKEND");

/**
 * iio_backend_interface_data_align - Perform the data alignment process.
 * @back: Backend device
 * @timeout_us: Timeout value in us.
 *
 * When activated, it initiates a process that aligns the sample's most
 * significant bit (MSB) based solely on the captured data, without
 * considering any other external signals.
 *
 * The timeout_us value must be greater than 0.
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_interface_data_align(struct iio_backend *back, u32 timeout_us)
{
	if (!timeout_us)
		return -EINVAL;

	return iio_backend_op_call(back, interface_data_align, timeout_us);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_interface_data_align, "IIO_BACKEND");

/**
 * iio_backend_num_lanes_set - Number of lanes enabled.
 * @back: Backend device
 * @num_lanes: Number of lanes.
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_num_lanes_set(struct iio_backend *back, unsigned int num_lanes)
{
	if (!num_lanes)
		return -EINVAL;

	return iio_backend_op_call(back, num_lanes_set, num_lanes);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_num_lanes_set, "IIO_BACKEND");

/**
 * iio_backend_ddr_enable - Enable interface DDR (Double Data Rate) mode
 * @back: Backend device
 *
 * Enable DDR, data is generated by the IP at each front (raising and falling)
 * of the bus clock signal.
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_ddr_enable(struct iio_backend *back)
{
	return iio_backend_op_call(back, ddr_enable);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_ddr_enable, "IIO_BACKEND");

/**
 * iio_backend_ddr_disable - Disable interface DDR (Double Data Rate) mode
 * @back: Backend device
 *
 * Disable DDR, setting into SDR mode (Single Data Rate).
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_ddr_disable(struct iio_backend *back)
{
	return iio_backend_op_call(back, ddr_disable);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_ddr_disable, "IIO_BACKEND");

/**
 * iio_backend_data_stream_enable - Enable data stream
 * @back: Backend device
 *
 * Enable data stream over the bus interface.
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_data_stream_enable(struct iio_backend *back)
{
	return iio_backend_op_call(back, data_stream_enable);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_data_stream_enable, "IIO_BACKEND");

/**
 * iio_backend_data_stream_disable - Disable data stream
 * @back: Backend device
 *
 * Disable data stream over the bus interface.
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_data_stream_disable(struct iio_backend *back)
{
	return iio_backend_op_call(back, data_stream_disable);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_data_stream_disable, "IIO_BACKEND");

/**
 * iio_backend_data_transfer_addr - Set data address.
 * @back: Backend device
 * @address: Data register address
 *
 * Some devices may need to inform the backend about an address
 * where to read or write the data.
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int iio_backend_data_transfer_addr(struct iio_backend *back, u32 address)
{
	return iio_backend_op_call(back, data_transfer_addr, address);
}
EXPORT_SYMBOL_NS_GPL(iio_backend_data_transfer_addr, "IIO_BACKEND");

static struct iio_backend *__devm_iio_backend_fwnode_get_by_index(struct device *dev,
								  struct fwnode_handle *fwnode,
								  unsigned int index)
{
	struct iio_backend *back;
	int ret;

	struct fwnode_handle *fwnode_back __free(fwnode_handle) =
		fwnode_find_reference(fwnode, "io-backends", index);
	if (IS_ERR(fwnode_back))
		return dev_err_cast_probe(dev, fwnode_back,
					  "Cannot get Firmware reference\n");

	guard(mutex)(&iio_back_lock);
	list_for_each_entry(back, &iio_back_list, entry) {
		if (!device_match_fwnode(back->dev, fwnode_back))
			continue;

		ret = __devm_iio_backend_get(dev, back);
		if (ret)
			return ERR_PTR(ret);

		back->idx = index;

		return back;
	}

	return ERR_PTR(-EPROBE_DEFER);
}

static struct iio_backend *__devm_iio_backend_fwnode_get(struct device *dev, const char *name,
							 struct fwnode_handle *fwnode)
{
	unsigned int index;
	int ret;

	if (name) {
		ret = device_property_match_string(dev, "io-backend-names", name);
		if (ret < 0)
			return ERR_PTR(ret);
		index = ret;
	} else {
		index = 0;
	}

	return __devm_iio_backend_fwnode_get_by_index(dev, fwnode, index);
}

/**
 * devm_iio_backend_get - Device managed backend device get
 * @dev: Consumer device for the backend
 * @name: Backend name
 *
 * Gets the backend associated with @dev.
 *
 * RETURNS:
 * A backend pointer, negative error pointer otherwise.
 */
struct iio_backend *devm_iio_backend_get(struct device *dev, const char *name)
{
	return __devm_iio_backend_fwnode_get(dev, name, dev_fwnode(dev));
}
EXPORT_SYMBOL_NS_GPL(devm_iio_backend_get, "IIO_BACKEND");

/**
 * devm_iio_backend_get_by_index - Device managed backend device get by index
 * @dev: Consumer device for the backend
 * @index: Index of the backend in the io-backends property
 *
 * Gets the backend at @index associated with @dev.
 *
 * RETURNS:
 * A backend pointer, negative error pointer otherwise.
 */
struct iio_backend *devm_iio_backend_get_by_index(struct device *dev, unsigned int index)
{
	return __devm_iio_backend_fwnode_get_by_index(dev, dev_fwnode(dev), index);
}
EXPORT_SYMBOL_NS_GPL(devm_iio_backend_get_by_index, "IIO_BACKEND");

/**
 * devm_iio_backend_fwnode_get - Device managed backend firmware node get
 * @dev: Consumer device for the backend
 * @name: Backend name
 * @fwnode: Firmware node of the backend consumer
 *
 * Gets the backend associated with a firmware node.
 *
 * RETURNS:
 * A backend pointer, negative error pointer otherwise.
 */
struct iio_backend *devm_iio_backend_fwnode_get(struct device *dev,
						const char *name,
						struct fwnode_handle *fwnode)
{
	return __devm_iio_backend_fwnode_get(dev, name, fwnode);
}
EXPORT_SYMBOL_NS_GPL(devm_iio_backend_fwnode_get, "IIO_BACKEND");

/**
 * __devm_iio_backend_get_from_fwnode_lookup - Device managed fwnode backend device get
 * @dev: Consumer device for the backend
 * @fwnode: Firmware node of the backend device
 *
 * Search the backend list for a device matching @fwnode.
 * This API should not be used and it's only present for preventing the first
 * user of this framework to break it's DT ABI.
 *
 * RETURNS:
 * A backend pointer, negative error pointer otherwise.
 */
struct iio_backend *
__devm_iio_backend_get_from_fwnode_lookup(struct device *dev,
					  struct fwnode_handle *fwnode)
{
	struct iio_backend *back;
	int ret;

	guard(mutex)(&iio_back_lock);
	list_for_each_entry(back, &iio_back_list, entry) {
		if (!device_match_fwnode(back->dev, fwnode))
			continue;

		ret = __devm_iio_backend_get(dev, back);
		if (ret)
			return ERR_PTR(ret);

		return back;
	}

	return ERR_PTR(-EPROBE_DEFER);
}
EXPORT_SYMBOL_NS_GPL(__devm_iio_backend_get_from_fwnode_lookup, "IIO_BACKEND");

/**
 * iio_backend_get_priv - Get driver private data
 * @back: Backend device
 *
 * RETURNS:
 * Pointer to the driver private data associated with the backend.
 */
void *iio_backend_get_priv(const struct iio_backend *back)
{
	return back->priv;
}
EXPORT_SYMBOL_NS_GPL(iio_backend_get_priv, "IIO_BACKEND");

static void iio_backend_unregister(void *arg)
{
	struct iio_backend *back = arg;

	guard(mutex)(&iio_back_lock);
	list_del(&back->entry);
}

/**
 * devm_iio_backend_register - Device managed backend device register
 * @dev: Backend device being registered
 * @info: Backend info
 * @priv: Device private data
 *
 * @info is mandatory. Not providing it results in -EINVAL.
 *
 * RETURNS:
 * 0 on success, negative error number on failure.
 */
int devm_iio_backend_register(struct device *dev,
			      const struct iio_backend_info *info, void *priv)
{
	struct iio_backend *back;

	if (!info || !info->ops)
		return dev_err_probe(dev, -EINVAL, "No backend ops given\n");

	/*
	 * Through device_links, we guarantee that a frontend device cannot be
	 * bound/exist if the backend driver is not around. Hence, we can bind
	 * the backend object lifetime with the device being passed since
	 * removing it will tear the frontend/consumer down.
	 */
	back = devm_kzalloc(dev, sizeof(*back), GFP_KERNEL);
	if (!back)
		return -ENOMEM;

	back->ops = info->ops;
	back->name = info->name;
	back->caps = info->caps;
	back->owner = dev->driver->owner;
	back->dev = dev;
	back->priv = priv;
	scoped_guard(mutex, &iio_back_lock)
		list_add(&back->entry, &iio_back_list);

	return devm_add_action_or_reset(dev, iio_backend_unregister, back);
}
EXPORT_SYMBOL_NS_GPL(devm_iio_backend_register, "IIO_BACKEND");

MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
MODULE_DESCRIPTION("Framework to handle complex IIO aggregate devices");
MODULE_LICENSE("GPL");