1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _IIO_BACKEND_H_
3 #define _IIO_BACKEND_H_
4 
5 #include <linux/types.h>
6 #include <linux/iio/iio.h>
7 
8 struct iio_chan_spec;
9 struct fwnode_handle;
10 struct iio_backend;
11 struct device;
12 struct iio_dev;
13 
14 enum iio_backend_data_type {
15 	IIO_BACKEND_TWOS_COMPLEMENT,
16 	IIO_BACKEND_OFFSET_BINARY,
17 	IIO_BACKEND_DATA_TYPE_MAX
18 };
19 
20 enum iio_backend_data_source {
21 	IIO_BACKEND_INTERNAL_CONTINUOUS_WAVE,
22 	IIO_BACKEND_EXTERNAL,
23 	IIO_BACKEND_DATA_SOURCE_MAX
24 };
25 
26 #define iio_backend_debugfs_ptr(ptr)	PTR_IF(IS_ENABLED(CONFIG_DEBUG_FS), ptr)
27 
28 /**
29  * IIO_BACKEND_EX_INFO - Helper for an IIO extended channel attribute
30  * @_name: Attribute name
31  * @_shared: Whether the attribute is shared between all channels
32  * @_what: Data private to the driver
33  */
34 #define IIO_BACKEND_EX_INFO(_name, _shared, _what) {	\
35 	.name = (_name),				\
36 	.shared = (_shared),				\
37 	.read =  iio_backend_ext_info_get,		\
38 	.write = iio_backend_ext_info_set,		\
39 	.private = (_what),				\
40 }
41 
42 /**
43  * struct iio_backend_data_fmt - Backend data format
44  * @type: Data type.
45  * @sign_extend: Bool to tell if the data is sign extended.
46  * @enable: Enable/Disable the data format module. If disabled,
47  *	    not formatting will happen.
48  */
49 struct iio_backend_data_fmt {
50 	enum iio_backend_data_type type;
51 	bool sign_extend;
52 	bool enable;
53 };
54 
55 /* vendor specific from 32 */
56 enum iio_backend_test_pattern {
57 	IIO_BACKEND_NO_TEST_PATTERN,
58 	/* modified prbs9 */
59 	IIO_BACKEND_ADI_PRBS_9A = 32,
60 	/* modified prbs23 */
61 	IIO_BACKEND_ADI_PRBS_23A,
62 	IIO_BACKEND_TEST_PATTERN_MAX
63 };
64 
65 enum iio_backend_sample_trigger {
66 	IIO_BACKEND_SAMPLE_TRIGGER_EDGE_FALLING,
67 	IIO_BACKEND_SAMPLE_TRIGGER_EDGE_RISING,
68 	IIO_BACKEND_SAMPLE_TRIGGER_MAX
69 };
70 
71 /**
72  * struct iio_backend_ops - operations structure for an iio_backend
73  * @enable: Enable backend.
74  * @disable: Disable backend.
75  * @chan_enable: Enable one channel.
76  * @chan_disable: Disable one channel.
77  * @data_format_set: Configure the data format for a specific channel.
78  * @data_source_set: Configure the data source for a specific channel.
79  * @set_sample_rate: Configure the sampling rate for a specific channel.
80  * @test_pattern_set: Configure a test pattern.
81  * @chan_status: Get the channel status.
82  * @iodelay_set: Set digital I/O delay.
83  * @data_sample_trigger: Control when to sample data.
84  * @request_buffer: Request an IIO buffer.
85  * @free_buffer: Free an IIO buffer.
86  * @extend_chan_spec: Extend an IIO channel.
87  * @ext_info_set: Extended info setter.
88  * @ext_info_get: Extended info getter.
89  * @read_raw: Read a channel attribute from a backend device
90  * @debugfs_print_chan_status: Print channel status into a buffer.
91  * @debugfs_reg_access: Read or write register value of backend.
92  **/
93 struct iio_backend_ops {
94 	int (*enable)(struct iio_backend *back);
95 	void (*disable)(struct iio_backend *back);
96 	int (*chan_enable)(struct iio_backend *back, unsigned int chan);
97 	int (*chan_disable)(struct iio_backend *back, unsigned int chan);
98 	int (*data_format_set)(struct iio_backend *back, unsigned int chan,
99 			       const struct iio_backend_data_fmt *data);
100 	int (*data_source_set)(struct iio_backend *back, unsigned int chan,
101 			       enum iio_backend_data_source data);
102 	int (*set_sample_rate)(struct iio_backend *back, unsigned int chan,
103 			       u64 sample_rate_hz);
104 	int (*test_pattern_set)(struct iio_backend *back,
105 				unsigned int chan,
106 				enum iio_backend_test_pattern pattern);
107 	int (*chan_status)(struct iio_backend *back, unsigned int chan,
108 			   bool *error);
109 	int (*iodelay_set)(struct iio_backend *back, unsigned int chan,
110 			   unsigned int taps);
111 	int (*data_sample_trigger)(struct iio_backend *back,
112 				   enum iio_backend_sample_trigger trigger);
113 	struct iio_buffer *(*request_buffer)(struct iio_backend *back,
114 					     struct iio_dev *indio_dev);
115 	void (*free_buffer)(struct iio_backend *back,
116 			    struct iio_buffer *buffer);
117 	int (*extend_chan_spec)(struct iio_backend *back,
118 				struct iio_chan_spec *chan);
119 	int (*ext_info_set)(struct iio_backend *back, uintptr_t private,
120 			    const struct iio_chan_spec *chan,
121 			    const char *buf, size_t len);
122 	int (*ext_info_get)(struct iio_backend *back, uintptr_t private,
123 			    const struct iio_chan_spec *chan, char *buf);
124 	int (*read_raw)(struct iio_backend *back,
125 			struct iio_chan_spec const *chan, int *val, int *val2,
126 			long mask);
127 	int (*debugfs_print_chan_status)(struct iio_backend *back,
128 					 unsigned int chan, char *buf,
129 					 size_t len);
130 	int (*debugfs_reg_access)(struct iio_backend *back, unsigned int reg,
131 				  unsigned int writeval, unsigned int *readval);
132 };
133 
134 /**
135  * struct iio_backend_info - info structure for an iio_backend
136  * @name: Backend name.
137  * @ops: Backend operations.
138  */
139 struct iio_backend_info {
140 	const char *name;
141 	const struct iio_backend_ops *ops;
142 };
143 
144 int iio_backend_chan_enable(struct iio_backend *back, unsigned int chan);
145 int iio_backend_chan_disable(struct iio_backend *back, unsigned int chan);
146 int devm_iio_backend_enable(struct device *dev, struct iio_backend *back);
147 int iio_backend_enable(struct iio_backend *back);
148 void iio_backend_disable(struct iio_backend *back);
149 int iio_backend_data_format_set(struct iio_backend *back, unsigned int chan,
150 				const struct iio_backend_data_fmt *data);
151 int iio_backend_data_source_set(struct iio_backend *back, unsigned int chan,
152 				enum iio_backend_data_source data);
153 int iio_backend_set_sampling_freq(struct iio_backend *back, unsigned int chan,
154 				  u64 sample_rate_hz);
155 int iio_backend_test_pattern_set(struct iio_backend *back,
156 				 unsigned int chan,
157 				 enum iio_backend_test_pattern pattern);
158 int iio_backend_chan_status(struct iio_backend *back, unsigned int chan,
159 			    bool *error);
160 int iio_backend_iodelay_set(struct iio_backend *back, unsigned int lane,
161 			    unsigned int taps);
162 int iio_backend_data_sample_trigger(struct iio_backend *back,
163 				    enum iio_backend_sample_trigger trigger);
164 int devm_iio_backend_request_buffer(struct device *dev,
165 				    struct iio_backend *back,
166 				    struct iio_dev *indio_dev);
167 ssize_t iio_backend_ext_info_set(struct iio_dev *indio_dev, uintptr_t private,
168 				 const struct iio_chan_spec *chan,
169 				 const char *buf, size_t len);
170 ssize_t iio_backend_ext_info_get(struct iio_dev *indio_dev, uintptr_t private,
171 				 const struct iio_chan_spec *chan, char *buf);
172 int iio_backend_read_raw(struct iio_backend *back,
173 			 struct iio_chan_spec const *chan, int *val, int *val2,
174 			 long mask);
175 int iio_backend_extend_chan_spec(struct iio_backend *back,
176 				 struct iio_chan_spec *chan);
177 void *iio_backend_get_priv(const struct iio_backend *conv);
178 struct iio_backend *devm_iio_backend_get(struct device *dev, const char *name);
179 struct iio_backend *devm_iio_backend_fwnode_get(struct device *dev,
180 						const char *name,
181 						struct fwnode_handle *fwnode);
182 struct iio_backend *
183 __devm_iio_backend_get_from_fwnode_lookup(struct device *dev,
184 					  struct fwnode_handle *fwnode);
185 
186 int devm_iio_backend_register(struct device *dev,
187 			      const struct iio_backend_info *info, void *priv);
188 
iio_backend_read_scale(struct iio_backend * back,struct iio_chan_spec const * chan,int * val,int * val2)189 static inline int iio_backend_read_scale(struct iio_backend *back,
190 					 struct iio_chan_spec const *chan,
191 					 int *val, int *val2)
192 {
193 	return iio_backend_read_raw(back, chan, val, val2, IIO_CHAN_INFO_SCALE);
194 }
195 
iio_backend_read_offset(struct iio_backend * back,struct iio_chan_spec const * chan,int * val,int * val2)196 static inline int iio_backend_read_offset(struct iio_backend *back,
197 					  struct iio_chan_spec const *chan,
198 					  int *val, int *val2)
199 {
200 	return iio_backend_read_raw(back, chan, val, val2,
201 				    IIO_CHAN_INFO_OFFSET);
202 }
203 
204 ssize_t iio_backend_debugfs_print_chan_status(struct iio_backend *back,
205 					      unsigned int chan, char *buf,
206 					      size_t len);
207 void iio_backend_debugfs_add(struct iio_backend *back,
208 			     struct iio_dev *indio_dev);
209 #endif
210