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 /** 80 * struct iio_backend_ops - operations structure for an iio_backend 81 * @enable: Enable backend. 82 * @disable: Disable backend. 83 * @chan_enable: Enable one channel. 84 * @chan_disable: Disable one channel. 85 * @data_format_set: Configure the data format for a specific channel. 86 * @data_source_set: Configure the data source for a specific channel. 87 * @set_sample_rate: Configure the sampling rate for a specific channel. 88 * @test_pattern_set: Configure a test pattern. 89 * @chan_status: Get the channel status. 90 * @iodelay_set: Set digital I/O delay. 91 * @data_sample_trigger: Control when to sample data. 92 * @request_buffer: Request an IIO buffer. 93 * @free_buffer: Free an IIO buffer. 94 * @extend_chan_spec: Extend an IIO channel. 95 * @ext_info_set: Extended info setter. 96 * @ext_info_get: Extended info getter. 97 * @interface_type_get: Interface type. 98 * @data_size_set: Data size. 99 * @read_raw: Read a channel attribute from a backend device 100 * @debugfs_print_chan_status: Print channel status into a buffer. 101 * @debugfs_reg_access: Read or write register value of backend. 102 * @ddr_enable: Enable interface DDR (Double Data Rate) mode. 103 * @ddr_disable: Disable interface DDR (Double Data Rate) mode. 104 * @data_stream_enable: Enable data stream. 105 * @data_stream_disable: Disable data stream. 106 * @data_transfer_addr: Set data address. 107 **/ 108 struct iio_backend_ops { 109 int (*enable)(struct iio_backend *back); 110 void (*disable)(struct iio_backend *back); 111 int (*chan_enable)(struct iio_backend *back, unsigned int chan); 112 int (*chan_disable)(struct iio_backend *back, unsigned int chan); 113 int (*data_format_set)(struct iio_backend *back, unsigned int chan, 114 const struct iio_backend_data_fmt *data); 115 int (*data_source_set)(struct iio_backend *back, unsigned int chan, 116 enum iio_backend_data_source data); 117 int (*set_sample_rate)(struct iio_backend *back, unsigned int chan, 118 u64 sample_rate_hz); 119 int (*test_pattern_set)(struct iio_backend *back, 120 unsigned int chan, 121 enum iio_backend_test_pattern pattern); 122 int (*chan_status)(struct iio_backend *back, unsigned int chan, 123 bool *error); 124 int (*iodelay_set)(struct iio_backend *back, unsigned int chan, 125 unsigned int taps); 126 int (*data_sample_trigger)(struct iio_backend *back, 127 enum iio_backend_sample_trigger trigger); 128 struct iio_buffer *(*request_buffer)(struct iio_backend *back, 129 struct iio_dev *indio_dev); 130 void (*free_buffer)(struct iio_backend *back, 131 struct iio_buffer *buffer); 132 int (*extend_chan_spec)(struct iio_backend *back, 133 struct iio_chan_spec *chan); 134 int (*ext_info_set)(struct iio_backend *back, uintptr_t private, 135 const struct iio_chan_spec *chan, 136 const char *buf, size_t len); 137 int (*ext_info_get)(struct iio_backend *back, uintptr_t private, 138 const struct iio_chan_spec *chan, char *buf); 139 int (*interface_type_get)(struct iio_backend *back, 140 enum iio_backend_interface_type *type); 141 int (*data_size_set)(struct iio_backend *back, unsigned int size); 142 int (*read_raw)(struct iio_backend *back, 143 struct iio_chan_spec const *chan, int *val, int *val2, 144 long mask); 145 int (*debugfs_print_chan_status)(struct iio_backend *back, 146 unsigned int chan, char *buf, 147 size_t len); 148 int (*debugfs_reg_access)(struct iio_backend *back, unsigned int reg, 149 unsigned int writeval, unsigned int *readval); 150 int (*ddr_enable)(struct iio_backend *back); 151 int (*ddr_disable)(struct iio_backend *back); 152 int (*data_stream_enable)(struct iio_backend *back); 153 int (*data_stream_disable)(struct iio_backend *back); 154 int (*data_transfer_addr)(struct iio_backend *back, u32 address); 155 }; 156 157 /** 158 * struct iio_backend_info - info structure for an iio_backend 159 * @name: Backend name. 160 * @ops: Backend operations. 161 */ 162 struct iio_backend_info { 163 const char *name; 164 const struct iio_backend_ops *ops; 165 }; 166 167 int iio_backend_chan_enable(struct iio_backend *back, unsigned int chan); 168 int iio_backend_chan_disable(struct iio_backend *back, unsigned int chan); 169 int devm_iio_backend_enable(struct device *dev, struct iio_backend *back); 170 int iio_backend_enable(struct iio_backend *back); 171 void iio_backend_disable(struct iio_backend *back); 172 int iio_backend_data_format_set(struct iio_backend *back, unsigned int chan, 173 const struct iio_backend_data_fmt *data); 174 int iio_backend_data_source_set(struct iio_backend *back, unsigned int chan, 175 enum iio_backend_data_source data); 176 int iio_backend_set_sampling_freq(struct iio_backend *back, unsigned int chan, 177 u64 sample_rate_hz); 178 int iio_backend_test_pattern_set(struct iio_backend *back, 179 unsigned int chan, 180 enum iio_backend_test_pattern pattern); 181 int iio_backend_chan_status(struct iio_backend *back, unsigned int chan, 182 bool *error); 183 int iio_backend_iodelay_set(struct iio_backend *back, unsigned int lane, 184 unsigned int taps); 185 int iio_backend_data_sample_trigger(struct iio_backend *back, 186 enum iio_backend_sample_trigger trigger); 187 int devm_iio_backend_request_buffer(struct device *dev, 188 struct iio_backend *back, 189 struct iio_dev *indio_dev); 190 int iio_backend_ddr_enable(struct iio_backend *back); 191 int iio_backend_ddr_disable(struct iio_backend *back); 192 int iio_backend_data_stream_enable(struct iio_backend *back); 193 int iio_backend_data_stream_disable(struct iio_backend *back); 194 int iio_backend_data_transfer_addr(struct iio_backend *back, u32 address); 195 ssize_t iio_backend_ext_info_set(struct iio_dev *indio_dev, uintptr_t private, 196 const struct iio_chan_spec *chan, 197 const char *buf, size_t len); 198 ssize_t iio_backend_ext_info_get(struct iio_dev *indio_dev, uintptr_t private, 199 const struct iio_chan_spec *chan, char *buf); 200 int iio_backend_interface_type_get(struct iio_backend *back, 201 enum iio_backend_interface_type *type); 202 int iio_backend_data_size_set(struct iio_backend *back, unsigned int size); 203 int iio_backend_read_raw(struct iio_backend *back, 204 struct iio_chan_spec const *chan, int *val, int *val2, 205 long mask); 206 int iio_backend_extend_chan_spec(struct iio_backend *back, 207 struct iio_chan_spec *chan); 208 void *iio_backend_get_priv(const struct iio_backend *conv); 209 struct iio_backend *devm_iio_backend_get(struct device *dev, const char *name); 210 struct iio_backend *devm_iio_backend_fwnode_get(struct device *dev, 211 const char *name, 212 struct fwnode_handle *fwnode); 213 struct iio_backend * 214 __devm_iio_backend_get_from_fwnode_lookup(struct device *dev, 215 struct fwnode_handle *fwnode); 216 217 int devm_iio_backend_register(struct device *dev, 218 const struct iio_backend_info *info, void *priv); 219 220 static inline int iio_backend_read_scale(struct iio_backend *back, 221 struct iio_chan_spec const *chan, 222 int *val, int *val2) 223 { 224 return iio_backend_read_raw(back, chan, val, val2, IIO_CHAN_INFO_SCALE); 225 } 226 227 static inline int iio_backend_read_offset(struct iio_backend *back, 228 struct iio_chan_spec const *chan, 229 int *val, int *val2) 230 { 231 return iio_backend_read_raw(back, chan, val, val2, 232 IIO_CHAN_INFO_OFFSET); 233 } 234 235 ssize_t iio_backend_debugfs_print_chan_status(struct iio_backend *back, 236 unsigned int chan, char *buf, 237 size_t len); 238 void iio_backend_debugfs_add(struct iio_backend *back, 239 struct iio_dev *indio_dev); 240 #endif 241