| 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_UNSIGNED, |
| 18 | IIO_BACKEND_DATA_TYPE_MAX |
| 19 | }; |
| 20 | |
| 21 | enum iio_backend_data_source { |
| 22 | IIO_BACKEND_INTERNAL_CONTINUOUS_WAVE, |
| 23 | IIO_BACKEND_EXTERNAL, |
| 24 | IIO_BACKEND_INTERNAL_RAMP_16BIT, |
| 25 | IIO_BACKEND_DATA_SOURCE_MAX |
| 26 | }; |
| 27 | |
| 28 | #define iio_backend_debugfs_ptr(ptr) PTR_IF(IS_ENABLED(CONFIG_DEBUG_FS), ptr) |
| 29 | |
| 30 | /** |
| 31 | * IIO_BACKEND_EX_INFO - Helper for an IIO extended channel attribute |
| 32 | * @_name: Attribute name |
| 33 | * @_shared: Whether the attribute is shared between all channels |
| 34 | * @_what: Data private to the driver |
| 35 | */ |
| 36 | #define IIO_BACKEND_EX_INFO(_name, _shared, _what) { \ |
| 37 | .name = (_name), \ |
| 38 | .shared = (_shared), \ |
| 39 | .read = iio_backend_ext_info_get, \ |
| 40 | .write = iio_backend_ext_info_set, \ |
| 41 | .private = (_what), \ |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * struct iio_backend_data_fmt - Backend data format |
| 46 | * @type: Data type. |
| 47 | * @sign_extend: Bool to tell if the data is sign extended. |
| 48 | * @enable: Enable/Disable the data format module. If disabled, |
| 49 | * not formatting will happen. |
| 50 | */ |
| 51 | struct iio_backend_data_fmt { |
| 52 | enum iio_backend_data_type type; |
| 53 | bool sign_extend; |
| 54 | bool enable; |
| 55 | }; |
| 56 | |
| 57 | /* vendor specific from 32 */ |
| 58 | enum iio_backend_test_pattern { |
| 59 | IIO_BACKEND_NO_TEST_PATTERN, |
| 60 | /* modified prbs9 */ |
| 61 | IIO_BACKEND_ADI_PRBS_9A = 32, |
| 62 | /* modified prbs23 */ |
| 63 | IIO_BACKEND_ADI_PRBS_23A, |
| 64 | IIO_BACKEND_TEST_PATTERN_MAX |
| 65 | }; |
| 66 | |
| 67 | enum iio_backend_sample_trigger { |
| 68 | IIO_BACKEND_SAMPLE_TRIGGER_EDGE_FALLING, |
| 69 | IIO_BACKEND_SAMPLE_TRIGGER_EDGE_RISING, |
| 70 | IIO_BACKEND_SAMPLE_TRIGGER_MAX |
| 71 | }; |
| 72 | |
| 73 | enum iio_backend_interface_type { |
| 74 | IIO_BACKEND_INTERFACE_SERIAL_LVDS, |
| 75 | IIO_BACKEND_INTERFACE_SERIAL_CMOS, |
| 76 | IIO_BACKEND_INTERFACE_MAX |
| 77 | }; |
| 78 | |
| 79 | enum iio_backend_filter_type { |
| 80 | IIO_BACKEND_FILTER_TYPE_DISABLED, |
| 81 | IIO_BACKEND_FILTER_TYPE_SINC1, |
| 82 | IIO_BACKEND_FILTER_TYPE_SINC5, |
| 83 | IIO_BACKEND_FILTER_TYPE_SINC5_PLUS_COMP, |
| 84 | IIO_BACKEND_FILTER_TYPE_MAX |
| 85 | }; |
| 86 | |
| 87 | /** |
| 88 | * struct iio_backend_ops - operations structure for an iio_backend |
| 89 | * @enable: Enable backend. |
| 90 | * @disable: Disable backend. |
| 91 | * @chan_enable: Enable one channel. |
| 92 | * @chan_disable: Disable one channel. |
| 93 | * @data_format_set: Configure the data format for a specific channel. |
| 94 | * @data_source_set: Configure the data source for a specific channel. |
| 95 | * @data_source_get: Data source getter for a specific channel. |
| 96 | * @set_sample_rate: Configure the sampling rate for a specific channel. |
| 97 | * @test_pattern_set: Configure a test pattern. |
| 98 | * @chan_status: Get the channel status. |
| 99 | * @iodelay_set: Set digital I/O delay. |
| 100 | * @data_sample_trigger: Control when to sample data. |
| 101 | * @request_buffer: Request an IIO buffer. |
| 102 | * @free_buffer: Free an IIO buffer. |
| 103 | * @extend_chan_spec: Extend an IIO channel. |
| 104 | * @ext_info_set: Extended info setter. |
| 105 | * @ext_info_get: Extended info getter. |
| 106 | * @interface_type_get: Interface type. |
| 107 | * @data_size_set: Data size. |
| 108 | * @oversampling_ratio_set: Set Oversampling ratio. |
| 109 | * @read_raw: Read a channel attribute from a backend device |
| 110 | * @debugfs_print_chan_status: Print channel status into a buffer. |
| 111 | * @debugfs_reg_access: Read or write register value of backend. |
| 112 | * @filter_type_set: Set filter type. |
| 113 | * @interface_data_align: Perform the data alignment process. |
| 114 | * @num_lanes_set: Set the number of lanes enabled. |
| 115 | * @ddr_enable: Enable interface DDR (Double Data Rate) mode. |
| 116 | * @ddr_disable: Disable interface DDR (Double Data Rate) mode. |
| 117 | * @data_stream_enable: Enable data stream. |
| 118 | * @data_stream_disable: Disable data stream. |
| 119 | * @data_transfer_addr: Set data address. |
| 120 | **/ |
| 121 | struct iio_backend_ops { |
| 122 | int (*enable)(struct iio_backend *back); |
| 123 | void (*disable)(struct iio_backend *back); |
| 124 | int (*chan_enable)(struct iio_backend *back, unsigned int chan); |
| 125 | int (*chan_disable)(struct iio_backend *back, unsigned int chan); |
| 126 | int (*data_format_set)(struct iio_backend *back, unsigned int chan, |
| 127 | const struct iio_backend_data_fmt *data); |
| 128 | int (*data_source_set)(struct iio_backend *back, unsigned int chan, |
| 129 | enum iio_backend_data_source data); |
| 130 | int (*data_source_get)(struct iio_backend *back, unsigned int chan, |
| 131 | enum iio_backend_data_source *data); |
| 132 | int (*set_sample_rate)(struct iio_backend *back, unsigned int chan, |
| 133 | u64 sample_rate_hz); |
| 134 | int (*test_pattern_set)(struct iio_backend *back, |
| 135 | unsigned int chan, |
| 136 | enum iio_backend_test_pattern pattern); |
| 137 | int (*chan_status)(struct iio_backend *back, unsigned int chan, |
| 138 | bool *error); |
| 139 | int (*iodelay_set)(struct iio_backend *back, unsigned int chan, |
| 140 | unsigned int taps); |
| 141 | int (*data_sample_trigger)(struct iio_backend *back, |
| 142 | enum iio_backend_sample_trigger trigger); |
| 143 | struct iio_buffer *(*request_buffer)(struct iio_backend *back, |
| 144 | struct iio_dev *indio_dev); |
| 145 | void (*free_buffer)(struct iio_backend *back, |
| 146 | struct iio_buffer *buffer); |
| 147 | int (*extend_chan_spec)(struct iio_backend *back, |
| 148 | struct iio_chan_spec *chan); |
| 149 | int (*ext_info_set)(struct iio_backend *back, uintptr_t private, |
| 150 | const struct iio_chan_spec *chan, |
| 151 | const char *buf, size_t len); |
| 152 | int (*ext_info_get)(struct iio_backend *back, uintptr_t private, |
| 153 | const struct iio_chan_spec *chan, char *buf); |
| 154 | int (*interface_type_get)(struct iio_backend *back, |
| 155 | enum iio_backend_interface_type *type); |
| 156 | int (*data_size_set)(struct iio_backend *back, unsigned int size); |
| 157 | int (*oversampling_ratio_set)(struct iio_backend *back, |
| 158 | unsigned int chan, unsigned int ratio); |
| 159 | int (*read_raw)(struct iio_backend *back, |
| 160 | struct iio_chan_spec const *chan, int *val, int *val2, |
| 161 | long mask); |
| 162 | int (*debugfs_print_chan_status)(struct iio_backend *back, |
| 163 | unsigned int chan, char *buf, |
| 164 | size_t len); |
| 165 | int (*debugfs_reg_access)(struct iio_backend *back, unsigned int reg, |
| 166 | unsigned int writeval, unsigned int *readval); |
| 167 | int (*filter_type_set)(struct iio_backend *back, |
| 168 | enum iio_backend_filter_type type); |
| 169 | int (*interface_data_align)(struct iio_backend *back, u32 timeout_us); |
| 170 | int (*num_lanes_set)(struct iio_backend *back, unsigned int num_lanes); |
| 171 | int (*ddr_enable)(struct iio_backend *back); |
| 172 | int (*ddr_disable)(struct iio_backend *back); |
| 173 | int (*data_stream_enable)(struct iio_backend *back); |
| 174 | int (*data_stream_disable)(struct iio_backend *back); |
| 175 | int (*data_transfer_addr)(struct iio_backend *back, u32 address); |
| 176 | }; |
| 177 | |
| 178 | /** |
| 179 | * struct iio_backend_info - info structure for an iio_backend |
| 180 | * @name: Backend name. |
| 181 | * @ops: Backend operations. |
| 182 | */ |
| 183 | struct iio_backend_info { |
| 184 | const char *name; |
| 185 | const struct iio_backend_ops *ops; |
| 186 | }; |
| 187 | |
| 188 | int iio_backend_chan_enable(struct iio_backend *back, unsigned int chan); |
| 189 | int iio_backend_chan_disable(struct iio_backend *back, unsigned int chan); |
| 190 | int devm_iio_backend_enable(struct device *dev, struct iio_backend *back); |
| 191 | int iio_backend_enable(struct iio_backend *back); |
| 192 | void iio_backend_disable(struct iio_backend *back); |
| 193 | int iio_backend_data_format_set(struct iio_backend *back, unsigned int chan, |
| 194 | const struct iio_backend_data_fmt *data); |
| 195 | int iio_backend_data_source_set(struct iio_backend *back, unsigned int chan, |
| 196 | enum iio_backend_data_source data); |
| 197 | int iio_backend_data_source_get(struct iio_backend *back, unsigned int chan, |
| 198 | enum iio_backend_data_source *data); |
| 199 | int iio_backend_set_sampling_freq(struct iio_backend *back, unsigned int chan, |
| 200 | u64 sample_rate_hz); |
| 201 | int iio_backend_test_pattern_set(struct iio_backend *back, |
| 202 | unsigned int chan, |
| 203 | enum iio_backend_test_pattern pattern); |
| 204 | int iio_backend_chan_status(struct iio_backend *back, unsigned int chan, |
| 205 | bool *error); |
| 206 | int iio_backend_iodelay_set(struct iio_backend *back, unsigned int lane, |
| 207 | unsigned int taps); |
| 208 | int iio_backend_data_sample_trigger(struct iio_backend *back, |
| 209 | enum iio_backend_sample_trigger trigger); |
| 210 | int devm_iio_backend_request_buffer(struct device *dev, |
| 211 | struct iio_backend *back, |
| 212 | struct iio_dev *indio_dev); |
| 213 | int iio_backend_filter_type_set(struct iio_backend *back, |
| 214 | enum iio_backend_filter_type type); |
| 215 | int iio_backend_interface_data_align(struct iio_backend *back, u32 timeout_us); |
| 216 | int iio_backend_num_lanes_set(struct iio_backend *back, unsigned int num_lanes); |
| 217 | int iio_backend_ddr_enable(struct iio_backend *back); |
| 218 | int iio_backend_ddr_disable(struct iio_backend *back); |
| 219 | int iio_backend_data_stream_enable(struct iio_backend *back); |
| 220 | int iio_backend_data_stream_disable(struct iio_backend *back); |
| 221 | int iio_backend_data_transfer_addr(struct iio_backend *back, u32 address); |
| 222 | ssize_t iio_backend_ext_info_set(struct iio_dev *indio_dev, uintptr_t private, |
| 223 | const struct iio_chan_spec *chan, |
| 224 | const char *buf, size_t len); |
| 225 | ssize_t iio_backend_ext_info_get(struct iio_dev *indio_dev, uintptr_t private, |
| 226 | const struct iio_chan_spec *chan, char *buf); |
| 227 | int iio_backend_interface_type_get(struct iio_backend *back, |
| 228 | enum iio_backend_interface_type *type); |
| 229 | int iio_backend_data_size_set(struct iio_backend *back, unsigned int size); |
| 230 | int iio_backend_oversampling_ratio_set(struct iio_backend *back, |
| 231 | unsigned int chan, |
| 232 | unsigned int ratio); |
| 233 | int iio_backend_read_raw(struct iio_backend *back, |
| 234 | struct iio_chan_spec const *chan, int *val, int *val2, |
| 235 | long mask); |
| 236 | int iio_backend_extend_chan_spec(struct iio_backend *back, |
| 237 | struct iio_chan_spec *chan); |
| 238 | void *iio_backend_get_priv(const struct iio_backend *conv); |
| 239 | struct iio_backend *devm_iio_backend_get(struct device *dev, const char *name); |
| 240 | struct iio_backend *devm_iio_backend_fwnode_get(struct device *dev, |
| 241 | const char *name, |
| 242 | struct fwnode_handle *fwnode); |
| 243 | struct iio_backend * |
| 244 | __devm_iio_backend_get_from_fwnode_lookup(struct device *dev, |
| 245 | struct fwnode_handle *fwnode); |
| 246 | |
| 247 | int devm_iio_backend_register(struct device *dev, |
| 248 | const struct iio_backend_info *info, void *priv); |
| 249 | |
| 250 | static inline int iio_backend_read_scale(struct iio_backend *back, |
| 251 | struct iio_chan_spec const *chan, |
| 252 | int *val, int *val2) |
| 253 | { |
| 254 | return iio_backend_read_raw(back, chan, val, val2, mask: IIO_CHAN_INFO_SCALE); |
| 255 | } |
| 256 | |
| 257 | static inline int iio_backend_read_offset(struct iio_backend *back, |
| 258 | struct iio_chan_spec const *chan, |
| 259 | int *val, int *val2) |
| 260 | { |
| 261 | return iio_backend_read_raw(back, chan, val, val2, |
| 262 | mask: IIO_CHAN_INFO_OFFSET); |
| 263 | } |
| 264 | |
| 265 | ssize_t iio_backend_debugfs_print_chan_status(struct iio_backend *back, |
| 266 | unsigned int chan, char *buf, |
| 267 | size_t len); |
| 268 | void iio_backend_debugfs_add(struct iio_backend *back, |
| 269 | struct iio_dev *indio_dev); |
| 270 | #endif |
| 271 | |