Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * Common library for ADIS16XXX devices
0004  *
0005  * Copyright 2012 Analog Devices Inc.
0006  *   Author: Lars-Peter Clausen <lars@metafoo.de>
0007  */
0008 
0009 #ifndef __IIO_ADIS_H__
0010 #define __IIO_ADIS_H__
0011 
0012 #include <linux/spi/spi.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/iio/types.h>
0015 
0016 #define ADIS_WRITE_REG(reg) ((0x80 | (reg)))
0017 #define ADIS_READ_REG(reg) ((reg) & 0x7f)
0018 
0019 #define ADIS_PAGE_SIZE 0x80
0020 #define ADIS_REG_PAGE_ID 0x00
0021 
0022 struct adis;
0023 
0024 /**
0025  * struct adis_timeouts - ADIS chip variant timeouts
0026  * @reset_ms - Wait time after rst pin goes inactive
0027  * @sw_reset_ms - Wait time after sw reset command
0028  * @self_test_ms - Wait time after self test command
0029  */
0030 struct adis_timeout {
0031     u16 reset_ms;
0032     u16 sw_reset_ms;
0033     u16 self_test_ms;
0034 };
0035 
0036 /**
0037  * struct adis_data - ADIS chip variant specific data
0038  * @read_delay: SPI delay for read operations in us
0039  * @write_delay: SPI delay for write operations in us
0040  * @cs_change_delay: SPI delay between CS changes in us
0041  * @glob_cmd_reg: Register address of the GLOB_CMD register
0042  * @msc_ctrl_reg: Register address of the MSC_CTRL register
0043  * @diag_stat_reg: Register address of the DIAG_STAT register
0044  * @prod_id_reg: Register address of the PROD_ID register
0045  * @prod_id: Product ID code that should be expected when reading @prod_id_reg
0046  * @self_test_mask: Bitmask of supported self-test operations
0047  * @self_test_reg: Register address to request self test command
0048  * @self_test_no_autoclear: True if device's self-test needs clear of ctrl reg
0049  * @status_error_msgs: Array of error messages
0050  * @status_error_mask: Bitmask of errors supported by the device
0051  * @timeouts: Chip specific delays
0052  * @enable_irq: Hook for ADIS devices that have a special IRQ enable/disable
0053  * @unmasked_drdy: True for devices that cannot mask/unmask the data ready pin
0054  * @has_paging: True if ADIS device has paged registers
0055  * @burst_reg_cmd:  Register command that triggers burst
0056  * @burst_len:      Burst size in the SPI RX buffer. If @burst_max_len is defined,
0057  *          this should be the minimum size supported by the device.
0058  * @burst_max_len:  Holds the maximum burst size when the device supports
0059  *          more than one burst mode with different sizes
0060  * @burst_max_speed_hz: Maximum spi speed that can be used in burst mode
0061  */
0062 struct adis_data {
0063     unsigned int read_delay;
0064     unsigned int write_delay;
0065     unsigned int cs_change_delay;
0066 
0067     unsigned int glob_cmd_reg;
0068     unsigned int msc_ctrl_reg;
0069     unsigned int diag_stat_reg;
0070     unsigned int prod_id_reg;
0071 
0072     unsigned int prod_id;
0073 
0074     unsigned int self_test_mask;
0075     unsigned int self_test_reg;
0076     bool self_test_no_autoclear;
0077     const struct adis_timeout *timeouts;
0078 
0079     const char * const *status_error_msgs;
0080     unsigned int status_error_mask;
0081 
0082     int (*enable_irq)(struct adis *adis, bool enable);
0083     bool unmasked_drdy;
0084 
0085     bool has_paging;
0086 
0087     unsigned int burst_reg_cmd;
0088     unsigned int burst_len;
0089     unsigned int burst_max_len;
0090     unsigned int burst_max_speed_hz;
0091 };
0092 
0093 /**
0094  * struct adis - ADIS device instance data
0095  * @spi: Reference to SPI device which owns this ADIS IIO device
0096  * @trig: IIO trigger object data
0097  * @data: ADIS chip variant specific data
0098  * @burst: ADIS burst transfer information
0099  * @burst_extra_len: Burst extra length. Should only be used by devices that can
0100  *           dynamically change their burst mode length.
0101  * @state_lock: Lock used by the device to protect state
0102  * @msg: SPI message object
0103  * @xfer: SPI transfer objects to be used for a @msg
0104  * @current_page: Some ADIS devices have registers, this selects current page
0105  * @irq_flag: IRQ handling flags as passed to request_irq()
0106  * @buffer: Data buffer for information read from the device
0107  * @tx: DMA safe TX buffer for SPI transfers
0108  * @rx: DMA safe RX buffer for SPI transfers
0109  */
0110 struct adis {
0111     struct spi_device   *spi;
0112     struct iio_trigger  *trig;
0113 
0114     const struct adis_data  *data;
0115     unsigned int        burst_extra_len;
0116     /**
0117      * The state_lock is meant to be used during operations that require
0118      * a sequence of SPI R/W in order to protect the SPI transfer
0119      * information (fields 'xfer', 'msg' & 'current_page') between
0120      * potential concurrent accesses.
0121      * This lock is used by all "adis_{functions}" that have to read/write
0122      * registers. These functions also have unlocked variants
0123      * (see "__adis_{functions}"), which don't hold this lock.
0124      * This allows users of the ADIS library to group SPI R/W into
0125      * the drivers, but they also must manage this lock themselves.
0126      */
0127     struct mutex        state_lock;
0128     struct spi_message  msg;
0129     struct spi_transfer *xfer;
0130     unsigned int        current_page;
0131     unsigned long       irq_flag;
0132     void            *buffer;
0133 
0134     u8          tx[10] ____cacheline_aligned;
0135     u8          rx[4];
0136 };
0137 
0138 int adis_init(struct adis *adis, struct iio_dev *indio_dev,
0139           struct spi_device *spi, const struct adis_data *data);
0140 int __adis_reset(struct adis *adis);
0141 
0142 /**
0143  * adis_reset() - Reset the device
0144  * @adis: The adis device
0145  *
0146  * Returns 0 on success, a negative error code otherwise
0147  */
0148 static inline int adis_reset(struct adis *adis)
0149 {
0150     int ret;
0151 
0152     mutex_lock(&adis->state_lock);
0153     ret = __adis_reset(adis);
0154     mutex_unlock(&adis->state_lock);
0155 
0156     return ret;
0157 }
0158 
0159 int __adis_write_reg(struct adis *adis, unsigned int reg,
0160              unsigned int val, unsigned int size);
0161 int __adis_read_reg(struct adis *adis, unsigned int reg,
0162             unsigned int *val, unsigned int size);
0163 
0164 /**
0165  * __adis_write_reg_8() - Write single byte to a register (unlocked)
0166  * @adis: The adis device
0167  * @reg: The address of the register to be written
0168  * @value: The value to write
0169  */
0170 static inline int __adis_write_reg_8(struct adis *adis, unsigned int reg,
0171                      u8 val)
0172 {
0173     return __adis_write_reg(adis, reg, val, 1);
0174 }
0175 
0176 /**
0177  * __adis_write_reg_16() - Write 2 bytes to a pair of registers (unlocked)
0178  * @adis: The adis device
0179  * @reg: The address of the lower of the two registers
0180  * @value: Value to be written
0181  */
0182 static inline int __adis_write_reg_16(struct adis *adis, unsigned int reg,
0183                       u16 val)
0184 {
0185     return __adis_write_reg(adis, reg, val, 2);
0186 }
0187 
0188 /**
0189  * __adis_write_reg_32() - write 4 bytes to four registers (unlocked)
0190  * @adis: The adis device
0191  * @reg: The address of the lower of the four register
0192  * @value: Value to be written
0193  */
0194 static inline int __adis_write_reg_32(struct adis *adis, unsigned int reg,
0195                       u32 val)
0196 {
0197     return __adis_write_reg(adis, reg, val, 4);
0198 }
0199 
0200 /**
0201  * __adis_read_reg_16() - read 2 bytes from a 16-bit register (unlocked)
0202  * @adis: The adis device
0203  * @reg: The address of the lower of the two registers
0204  * @val: The value read back from the device
0205  */
0206 static inline int __adis_read_reg_16(struct adis *adis, unsigned int reg,
0207                      u16 *val)
0208 {
0209     unsigned int tmp;
0210     int ret;
0211 
0212     ret = __adis_read_reg(adis, reg, &tmp, 2);
0213     if (ret == 0)
0214         *val = tmp;
0215 
0216     return ret;
0217 }
0218 
0219 /**
0220  * __adis_read_reg_32() - read 4 bytes from a 32-bit register (unlocked)
0221  * @adis: The adis device
0222  * @reg: The address of the lower of the two registers
0223  * @val: The value read back from the device
0224  */
0225 static inline int __adis_read_reg_32(struct adis *adis, unsigned int reg,
0226                      u32 *val)
0227 {
0228     unsigned int tmp;
0229     int ret;
0230 
0231     ret = __adis_read_reg(adis, reg, &tmp, 4);
0232     if (ret == 0)
0233         *val = tmp;
0234 
0235     return ret;
0236 }
0237 
0238 /**
0239  * adis_write_reg() - write N bytes to register
0240  * @adis: The adis device
0241  * @reg: The address of the lower of the two registers
0242  * @value: The value to write to device (up to 4 bytes)
0243  * @size: The size of the @value (in bytes)
0244  */
0245 static inline int adis_write_reg(struct adis *adis, unsigned int reg,
0246                  unsigned int val, unsigned int size)
0247 {
0248     int ret;
0249 
0250     mutex_lock(&adis->state_lock);
0251     ret = __adis_write_reg(adis, reg, val, size);
0252     mutex_unlock(&adis->state_lock);
0253 
0254     return ret;
0255 }
0256 
0257 /**
0258  * adis_read_reg() - read N bytes from register
0259  * @adis: The adis device
0260  * @reg: The address of the lower of the two registers
0261  * @val: The value read back from the device
0262  * @size: The size of the @val buffer
0263  */
0264 static int adis_read_reg(struct adis *adis, unsigned int reg,
0265              unsigned int *val, unsigned int size)
0266 {
0267     int ret;
0268 
0269     mutex_lock(&adis->state_lock);
0270     ret = __adis_read_reg(adis, reg, val, size);
0271     mutex_unlock(&adis->state_lock);
0272 
0273     return ret;
0274 }
0275 
0276 /**
0277  * adis_write_reg_8() - Write single byte to a register
0278  * @adis: The adis device
0279  * @reg: The address of the register to be written
0280  * @value: The value to write
0281  */
0282 static inline int adis_write_reg_8(struct adis *adis, unsigned int reg,
0283                    u8 val)
0284 {
0285     return adis_write_reg(adis, reg, val, 1);
0286 }
0287 
0288 /**
0289  * adis_write_reg_16() - Write 2 bytes to a pair of registers
0290  * @adis: The adis device
0291  * @reg: The address of the lower of the two registers
0292  * @value: Value to be written
0293  */
0294 static inline int adis_write_reg_16(struct adis *adis, unsigned int reg,
0295                     u16 val)
0296 {
0297     return adis_write_reg(adis, reg, val, 2);
0298 }
0299 
0300 /**
0301  * adis_write_reg_32() - write 4 bytes to four registers
0302  * @adis: The adis device
0303  * @reg: The address of the lower of the four register
0304  * @value: Value to be written
0305  */
0306 static inline int adis_write_reg_32(struct adis *adis, unsigned int reg,
0307                     u32 val)
0308 {
0309     return adis_write_reg(adis, reg, val, 4);
0310 }
0311 
0312 /**
0313  * adis_read_reg_16() - read 2 bytes from a 16-bit register
0314  * @adis: The adis device
0315  * @reg: The address of the lower of the two registers
0316  * @val: The value read back from the device
0317  */
0318 static inline int adis_read_reg_16(struct adis *adis, unsigned int reg,
0319                    u16 *val)
0320 {
0321     unsigned int tmp;
0322     int ret;
0323 
0324     ret = adis_read_reg(adis, reg, &tmp, 2);
0325     if (ret == 0)
0326         *val = tmp;
0327 
0328     return ret;
0329 }
0330 
0331 /**
0332  * adis_read_reg_32() - read 4 bytes from a 32-bit register
0333  * @adis: The adis device
0334  * @reg: The address of the lower of the two registers
0335  * @val: The value read back from the device
0336  */
0337 static inline int adis_read_reg_32(struct adis *adis, unsigned int reg,
0338                    u32 *val)
0339 {
0340     unsigned int tmp;
0341     int ret;
0342 
0343     ret = adis_read_reg(adis, reg, &tmp, 4);
0344     if (ret == 0)
0345         *val = tmp;
0346 
0347     return ret;
0348 }
0349 
0350 int __adis_update_bits_base(struct adis *adis, unsigned int reg, const u32 mask,
0351                 const u32 val, u8 size);
0352 /**
0353  * adis_update_bits_base() - ADIS Update bits function - Locked version
0354  * @adis: The adis device
0355  * @reg: The address of the lower of the two registers
0356  * @mask: Bitmask to change
0357  * @val: Value to be written
0358  * @size: Size of the register to update
0359  *
0360  * Updates the desired bits of @reg in accordance with @mask and @val.
0361  */
0362 static inline int adis_update_bits_base(struct adis *adis, unsigned int reg,
0363                     const u32 mask, const u32 val, u8 size)
0364 {
0365     int ret;
0366 
0367     mutex_lock(&adis->state_lock);
0368     ret = __adis_update_bits_base(adis, reg, mask, val, size);
0369     mutex_unlock(&adis->state_lock);
0370     return ret;
0371 }
0372 
0373 /**
0374  * adis_update_bits() - Wrapper macro for adis_update_bits_base - Locked version
0375  * @adis: The adis device
0376  * @reg: The address of the lower of the two registers
0377  * @mask: Bitmask to change
0378  * @val: Value to be written
0379  *
0380  * This macro evaluates the sizeof of @val at compile time and calls
0381  * adis_update_bits_base() accordingly. Be aware that using MACROS/DEFINES for
0382  * @val can lead to undesired behavior if the register to update is 16bit.
0383  */
0384 #define adis_update_bits(adis, reg, mask, val) ({           \
0385     BUILD_BUG_ON(sizeof(val) != 2 && sizeof(val) != 4);     \
0386     adis_update_bits_base(adis, reg, mask, val, sizeof(val));   \
0387 })
0388 
0389 /**
0390  * adis_update_bits() - Wrapper macro for adis_update_bits_base
0391  * @adis: The adis device
0392  * @reg: The address of the lower of the two registers
0393  * @mask: Bitmask to change
0394  * @val: Value to be written
0395  *
0396  * This macro evaluates the sizeof of @val at compile time and calls
0397  * adis_update_bits_base() accordingly. Be aware that using MACROS/DEFINES for
0398  * @val can lead to undesired behavior if the register to update is 16bit.
0399  */
0400 #define __adis_update_bits(adis, reg, mask, val) ({         \
0401     BUILD_BUG_ON(sizeof(val) != 2 && sizeof(val) != 4);     \
0402     __adis_update_bits_base(adis, reg, mask, val, sizeof(val)); \
0403 })
0404 
0405 int adis_enable_irq(struct adis *adis, bool enable);
0406 int __adis_check_status(struct adis *adis);
0407 int __adis_initial_startup(struct adis *adis);
0408 
0409 static inline int adis_check_status(struct adis *adis)
0410 {
0411     int ret;
0412 
0413     mutex_lock(&adis->state_lock);
0414     ret = __adis_check_status(adis);
0415     mutex_unlock(&adis->state_lock);
0416 
0417     return ret;
0418 }
0419 
0420 /* locked version of __adis_initial_startup() */
0421 static inline int adis_initial_startup(struct adis *adis)
0422 {
0423     int ret;
0424 
0425     mutex_lock(&adis->state_lock);
0426     ret = __adis_initial_startup(adis);
0427     mutex_unlock(&adis->state_lock);
0428 
0429     return ret;
0430 }
0431 
0432 static inline void adis_dev_lock(struct adis *adis)
0433 {
0434     mutex_lock(&adis->state_lock);
0435 }
0436 
0437 static inline void adis_dev_unlock(struct adis *adis)
0438 {
0439     mutex_unlock(&adis->state_lock);
0440 }
0441 
0442 int adis_single_conversion(struct iio_dev *indio_dev,
0443                const struct iio_chan_spec *chan,
0444                unsigned int error_mask, int *val);
0445 
0446 #define ADIS_VOLTAGE_CHAN(addr, si, chan, name, info_all, bits) { \
0447     .type = IIO_VOLTAGE, \
0448     .indexed = 1, \
0449     .channel = (chan), \
0450     .extend_name = name, \
0451     .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
0452         BIT(IIO_CHAN_INFO_SCALE), \
0453     .info_mask_shared_by_all = info_all, \
0454     .address = (addr), \
0455     .scan_index = (si), \
0456     .scan_type = { \
0457         .sign = 'u', \
0458         .realbits = (bits), \
0459         .storagebits = 16, \
0460         .endianness = IIO_BE, \
0461     }, \
0462 }
0463 
0464 #define ADIS_SUPPLY_CHAN(addr, si, info_all, bits) \
0465     ADIS_VOLTAGE_CHAN(addr, si, 0, "supply", info_all, bits)
0466 
0467 #define ADIS_AUX_ADC_CHAN(addr, si, info_all, bits) \
0468     ADIS_VOLTAGE_CHAN(addr, si, 1, NULL, info_all, bits)
0469 
0470 #define ADIS_TEMP_CHAN(addr, si, info_all, bits) { \
0471     .type = IIO_TEMP, \
0472     .indexed = 1, \
0473     .channel = 0, \
0474     .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
0475         BIT(IIO_CHAN_INFO_SCALE) | \
0476         BIT(IIO_CHAN_INFO_OFFSET), \
0477     .info_mask_shared_by_all = info_all, \
0478     .address = (addr), \
0479     .scan_index = (si), \
0480     .scan_type = { \
0481         .sign = 'u', \
0482         .realbits = (bits), \
0483         .storagebits = 16, \
0484         .endianness = IIO_BE, \
0485     }, \
0486 }
0487 
0488 #define ADIS_MOD_CHAN(_type, mod, addr, si, info_sep, info_all, bits) { \
0489     .type = (_type), \
0490     .modified = 1, \
0491     .channel2 = IIO_MOD_ ## mod, \
0492     .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
0493          (info_sep), \
0494     .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
0495     .info_mask_shared_by_all = info_all, \
0496     .address = (addr), \
0497     .scan_index = (si), \
0498     .scan_type = { \
0499         .sign = 's', \
0500         .realbits = (bits), \
0501         .storagebits = 16, \
0502         .endianness = IIO_BE, \
0503     }, \
0504 }
0505 
0506 #define ADIS_ACCEL_CHAN(mod, addr, si, info_sep, info_all, bits) \
0507     ADIS_MOD_CHAN(IIO_ACCEL, mod, addr, si, info_sep, info_all, bits)
0508 
0509 #define ADIS_GYRO_CHAN(mod, addr, si, info_sep, info_all, bits)     \
0510     ADIS_MOD_CHAN(IIO_ANGL_VEL, mod, addr, si, info_sep, info_all, bits)
0511 
0512 #define ADIS_INCLI_CHAN(mod, addr, si, info_sep, info_all, bits) \
0513     ADIS_MOD_CHAN(IIO_INCLI, mod, addr, si, info_sep, info_all, bits)
0514 
0515 #define ADIS_ROT_CHAN(mod, addr, si, info_sep, info_all, bits) \
0516     ADIS_MOD_CHAN(IIO_ROT, mod, addr, si, info_sep, info_all, bits)
0517 
0518 #ifdef CONFIG_IIO_ADIS_LIB_BUFFER
0519 
0520 int
0521 devm_adis_setup_buffer_and_trigger(struct adis *adis, struct iio_dev *indio_dev,
0522                    irq_handler_t trigger_handler);
0523 
0524 int devm_adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev);
0525 
0526 int adis_update_scan_mode(struct iio_dev *indio_dev,
0527               const unsigned long *scan_mask);
0528 
0529 #else /* CONFIG_IIO_BUFFER */
0530 
0531 static inline int
0532 devm_adis_setup_buffer_and_trigger(struct adis *adis, struct iio_dev *indio_dev,
0533                    irq_handler_t trigger_handler)
0534 {
0535     return 0;
0536 }
0537 
0538 static inline int devm_adis_probe_trigger(struct adis *adis,
0539                       struct iio_dev *indio_dev)
0540 {
0541     return 0;
0542 }
0543 
0544 #define adis_update_scan_mode NULL
0545 
0546 #endif /* CONFIG_IIO_BUFFER */
0547 
0548 #ifdef CONFIG_DEBUG_FS
0549 
0550 int adis_debugfs_reg_access(struct iio_dev *indio_dev,
0551                 unsigned int reg, unsigned int writeval,
0552                 unsigned int *readval);
0553 
0554 #else
0555 
0556 #define adis_debugfs_reg_access NULL
0557 
0558 #endif
0559 
0560 #endif