linux/include/linux/iio/imu/adis.h
<<
>>
Prefs
   1/* SPDX-License-Identifier: GPL-2.0-or-later */
   2/*
   3 * Common library for ADIS16XXX devices
   4 *
   5 * Copyright 2012 Analog Devices Inc.
   6 *   Author: Lars-Peter Clausen <lars@metafoo.de>
   7 */
   8
   9#ifndef __IIO_ADIS_H__
  10#define __IIO_ADIS_H__
  11
  12#include <linux/spi/spi.h>
  13#include <linux/interrupt.h>
  14#include <linux/iio/types.h>
  15
  16#define ADIS_WRITE_REG(reg) ((0x80 | (reg)))
  17#define ADIS_READ_REG(reg) ((reg) & 0x7f)
  18
  19#define ADIS_PAGE_SIZE 0x80
  20#define ADIS_REG_PAGE_ID 0x00
  21
  22struct adis;
  23
  24/**
  25 * struct adis_timeouts - ADIS chip variant timeouts
  26 * @reset_ms - Wait time after rst pin goes inactive
  27 * @sw_reset_ms - Wait time after sw reset command
  28 * @self_test_ms - Wait time after self test command
  29 */
  30struct adis_timeout {
  31        u16 reset_ms;
  32        u16 sw_reset_ms;
  33        u16 self_test_ms;
  34};
  35/**
  36 * struct adis_data - ADIS chip variant specific data
  37 * @read_delay: SPI delay for read operations in us
  38 * @write_delay: SPI delay for write operations in us
  39 * @cs_change_delay: SPI delay between CS changes in us
  40 * @glob_cmd_reg: Register address of the GLOB_CMD register
  41 * @msc_ctrl_reg: Register address of the MSC_CTRL register
  42 * @diag_stat_reg: Register address of the DIAG_STAT register
  43 * @prod_id_reg: Register address of the PROD_ID register
  44 * @prod_id: Product ID code that should be expected when reading @prod_id_reg
  45 * @self_test_mask: Bitmask of supported self-test operations
  46 * @self_test_reg: Register address to request self test command
  47 * @self_test_no_autoclear: True if device's self-test needs clear of ctrl reg
  48 * @status_error_msgs: Array of error messgaes
  49 * @status_error_mask: Bitmask of errors supported by the device
  50 * @timeouts: Chip specific delays
  51 * @enable_irq: Hook for ADIS devices that have a special IRQ enable/disable
  52 * @has_paging: True if ADIS device has paged registers
  53 * @burst_reg_cmd:      Register command that triggers burst
  54 * @burst_len:          Burst size in the SPI RX buffer. If @burst_max_len is defined,
  55 *                      this should be the minimum size supported by the device.
  56 * @burst_max_len:      Holds the maximum burst size when the device supports
  57 *                      more than one burst mode with different sizes
  58 * @burst_max_speed_hz: Maximum spi speed that can be used in burst mode
  59 */
  60struct adis_data {
  61        unsigned int read_delay;
  62        unsigned int write_delay;
  63        unsigned int cs_change_delay;
  64
  65        unsigned int glob_cmd_reg;
  66        unsigned int msc_ctrl_reg;
  67        unsigned int diag_stat_reg;
  68        unsigned int prod_id_reg;
  69
  70        unsigned int prod_id;
  71
  72        unsigned int self_test_mask;
  73        unsigned int self_test_reg;
  74        bool self_test_no_autoclear;
  75        const struct adis_timeout *timeouts;
  76
  77        const char * const *status_error_msgs;
  78        unsigned int status_error_mask;
  79
  80        int (*enable_irq)(struct adis *adis, bool enable);
  81
  82        bool has_paging;
  83
  84        unsigned int burst_reg_cmd;
  85        unsigned int burst_len;
  86        unsigned int burst_max_len;
  87        unsigned int burst_max_speed_hz;
  88};
  89
  90/**
  91 * struct adis - ADIS device instance data
  92 * @spi: Reference to SPI device which owns this ADIS IIO device
  93 * @trig: IIO trigger object data
  94 * @data: ADIS chip variant specific data
  95 * @burst: ADIS burst transfer information
  96 * @burst_extra_len: Burst extra length. Should only be used by devices that can
  97 *                   dynamically change their burst mode length.
  98 * @state_lock: Lock used by the device to protect state
  99 * @msg: SPI message object
 100 * @xfer: SPI transfer objects to be used for a @msg
 101 * @current_page: Some ADIS devices have registers, this selects current page
 102 * @irq_flag: IRQ handling flags as passed to request_irq()
 103 * @buffer: Data buffer for information read from the device
 104 * @tx: DMA safe TX buffer for SPI transfers
 105 * @rx: DMA safe RX buffer for SPI transfers
 106 */
 107struct adis {
 108        struct spi_device       *spi;
 109        struct iio_trigger      *trig;
 110
 111        const struct adis_data  *data;
 112        unsigned int            burst_extra_len;
 113        /**
 114         * The state_lock is meant to be used during operations that require
 115         * a sequence of SPI R/W in order to protect the SPI transfer
 116         * information (fields 'xfer', 'msg' & 'current_page') between
 117         * potential concurrent accesses.
 118         * This lock is used by all "adis_{functions}" that have to read/write
 119         * registers. These functions also have unlocked variants
 120         * (see "__adis_{functions}"), which don't hold this lock.
 121         * This allows users of the ADIS library to group SPI R/W into
 122         * the drivers, but they also must manage this lock themselves.
 123         */
 124        struct mutex            state_lock;
 125        struct spi_message      msg;
 126        struct spi_transfer     *xfer;
 127        unsigned int            current_page;
 128        unsigned long           irq_flag;
 129        void                    *buffer;
 130
 131        uint8_t                 tx[10] ____cacheline_aligned;
 132        uint8_t                 rx[4];
 133};
 134
 135int adis_init(struct adis *adis, struct iio_dev *indio_dev,
 136        struct spi_device *spi, const struct adis_data *data);
 137int __adis_reset(struct adis *adis);
 138
 139/**
 140 * adis_reset() - Reset the device
 141 * @adis: The adis device
 142 *
 143 * Returns 0 on success, a negative error code otherwise
 144 */
 145static inline int adis_reset(struct adis *adis)
 146{
 147        int ret;
 148
 149        mutex_lock(&adis->state_lock);
 150        ret = __adis_reset(adis);
 151        mutex_unlock(&adis->state_lock);
 152
 153        return ret;
 154}
 155
 156int __adis_write_reg(struct adis *adis, unsigned int reg,
 157        unsigned int val, unsigned int size);
 158int __adis_read_reg(struct adis *adis, unsigned int reg,
 159        unsigned int *val, unsigned int size);
 160
 161/**
 162 * __adis_write_reg_8() - Write single byte to a register (unlocked)
 163 * @adis: The adis device
 164 * @reg: The address of the register to be written
 165 * @value: The value to write
 166 */
 167static inline int __adis_write_reg_8(struct adis *adis, unsigned int reg,
 168        uint8_t val)
 169{
 170        return __adis_write_reg(adis, reg, val, 1);
 171}
 172
 173/**
 174 * __adis_write_reg_16() - Write 2 bytes to a pair of registers (unlocked)
 175 * @adis: The adis device
 176 * @reg: The address of the lower of the two registers
 177 * @value: Value to be written
 178 */
 179static inline int __adis_write_reg_16(struct adis *adis, unsigned int reg,
 180        uint16_t val)
 181{
 182        return __adis_write_reg(adis, reg, val, 2);
 183}
 184
 185/**
 186 * __adis_write_reg_32() - write 4 bytes to four registers (unlocked)
 187 * @adis: The adis device
 188 * @reg: The address of the lower of the four register
 189 * @value: Value to be written
 190 */
 191static inline int __adis_write_reg_32(struct adis *adis, unsigned int reg,
 192        uint32_t val)
 193{
 194        return __adis_write_reg(adis, reg, val, 4);
 195}
 196
 197/**
 198 * __adis_read_reg_16() - read 2 bytes from a 16-bit register (unlocked)
 199 * @adis: The adis device
 200 * @reg: The address of the lower of the two registers
 201 * @val: The value read back from the device
 202 */
 203static inline int __adis_read_reg_16(struct adis *adis, unsigned int reg,
 204        uint16_t *val)
 205{
 206        unsigned int tmp;
 207        int ret;
 208
 209        ret = __adis_read_reg(adis, reg, &tmp, 2);
 210        if (ret == 0)
 211                *val = tmp;
 212
 213        return ret;
 214}
 215
 216/**
 217 * __adis_read_reg_32() - read 4 bytes from a 32-bit register (unlocked)
 218 * @adis: The adis device
 219 * @reg: The address of the lower of the two registers
 220 * @val: The value read back from the device
 221 */
 222static inline int __adis_read_reg_32(struct adis *adis, unsigned int reg,
 223        uint32_t *val)
 224{
 225        unsigned int tmp;
 226        int ret;
 227
 228        ret = __adis_read_reg(adis, reg, &tmp, 4);
 229        if (ret == 0)
 230                *val = tmp;
 231
 232        return ret;
 233}
 234
 235/**
 236 * adis_write_reg() - write N bytes to register
 237 * @adis: The adis device
 238 * @reg: The address of the lower of the two registers
 239 * @value: The value to write to device (up to 4 bytes)
 240 * @size: The size of the @value (in bytes)
 241 */
 242static inline int adis_write_reg(struct adis *adis, unsigned int reg,
 243        unsigned int val, unsigned int size)
 244{
 245        int ret;
 246
 247        mutex_lock(&adis->state_lock);
 248        ret = __adis_write_reg(adis, reg, val, size);
 249        mutex_unlock(&adis->state_lock);
 250
 251        return ret;
 252}
 253
 254/**
 255 * adis_read_reg() - read N bytes from register
 256 * @adis: The adis device
 257 * @reg: The address of the lower of the two registers
 258 * @val: The value read back from the device
 259 * @size: The size of the @val buffer
 260 */
 261static int adis_read_reg(struct adis *adis, unsigned int reg,
 262        unsigned int *val, unsigned int size)
 263{
 264        int ret;
 265
 266        mutex_lock(&adis->state_lock);
 267        ret = __adis_read_reg(adis, reg, val, size);
 268        mutex_unlock(&adis->state_lock);
 269
 270        return ret;
 271}
 272
 273/**
 274 * adis_write_reg_8() - Write single byte to a register
 275 * @adis: The adis device
 276 * @reg: The address of the register to be written
 277 * @value: The value to write
 278 */
 279static inline int adis_write_reg_8(struct adis *adis, unsigned int reg,
 280        uint8_t val)
 281{
 282        return adis_write_reg(adis, reg, val, 1);
 283}
 284
 285/**
 286 * adis_write_reg_16() - Write 2 bytes to a pair of registers
 287 * @adis: The adis device
 288 * @reg: The address of the lower of the two registers
 289 * @value: Value to be written
 290 */
 291static inline int adis_write_reg_16(struct adis *adis, unsigned int reg,
 292        uint16_t val)
 293{
 294        return adis_write_reg(adis, reg, val, 2);
 295}
 296
 297/**
 298 * adis_write_reg_32() - write 4 bytes to four registers
 299 * @adis: The adis device
 300 * @reg: The address of the lower of the four register
 301 * @value: Value to be written
 302 */
 303static inline int adis_write_reg_32(struct adis *adis, unsigned int reg,
 304        uint32_t val)
 305{
 306        return adis_write_reg(adis, reg, val, 4);
 307}
 308
 309/**
 310 * adis_read_reg_16() - read 2 bytes from a 16-bit register
 311 * @adis: The adis device
 312 * @reg: The address of the lower of the two registers
 313 * @val: The value read back from the device
 314 */
 315static inline int adis_read_reg_16(struct adis *adis, unsigned int reg,
 316        uint16_t *val)
 317{
 318        unsigned int tmp;
 319        int ret;
 320
 321        ret = adis_read_reg(adis, reg, &tmp, 2);
 322        if (ret == 0)
 323                *val = tmp;
 324
 325        return ret;
 326}
 327
 328/**
 329 * adis_read_reg_32() - read 4 bytes from a 32-bit register
 330 * @adis: The adis device
 331 * @reg: The address of the lower of the two registers
 332 * @val: The value read back from the device
 333 */
 334static inline int adis_read_reg_32(struct adis *adis, unsigned int reg,
 335        uint32_t *val)
 336{
 337        unsigned int tmp;
 338        int ret;
 339
 340        ret = adis_read_reg(adis, reg, &tmp, 4);
 341        if (ret == 0)
 342                *val = tmp;
 343
 344        return ret;
 345}
 346
 347int __adis_update_bits_base(struct adis *adis, unsigned int reg, const u32 mask,
 348                            const u32 val, u8 size);
 349/**
 350 * adis_update_bits_base() - ADIS Update bits function - Locked version
 351 * @adis: The adis device
 352 * @reg: The address of the lower of the two registers
 353 * @mask: Bitmask to change
 354 * @val: Value to be written
 355 * @size: Size of the register to update
 356 *
 357 * Updates the desired bits of @reg in accordance with @mask and @val.
 358 */
 359static inline int adis_update_bits_base(struct adis *adis, unsigned int reg,
 360                                        const u32 mask, const u32 val, u8 size)
 361{
 362        int ret;
 363
 364        mutex_lock(&adis->state_lock);
 365        ret = __adis_update_bits_base(adis, reg, mask, val, size);
 366        mutex_unlock(&adis->state_lock);
 367        return ret;
 368}
 369
 370/**
 371 * adis_update_bits() - Wrapper macro for adis_update_bits_base - Locked version
 372 * @adis: The adis device
 373 * @reg: The address of the lower of the two registers
 374 * @mask: Bitmask to change
 375 * @val: Value to be written
 376 *
 377 * This macro evaluates the sizeof of @val at compile time and calls
 378 * adis_update_bits_base() accordingly. Be aware that using MACROS/DEFINES for
 379 * @val can lead to undesired behavior if the register to update is 16bit.
 380 */
 381#define adis_update_bits(adis, reg, mask, val) ({                       \
 382        BUILD_BUG_ON(sizeof(val) == 1 || sizeof(val) == 8);             \
 383        __builtin_choose_expr(sizeof(val) == 4,                         \
 384                adis_update_bits_base(adis, reg, mask, val, 4),         \
 385                adis_update_bits_base(adis, reg, mask, val, 2));        \
 386})
 387
 388/**
 389 * adis_update_bits() - Wrapper macro for adis_update_bits_base
 390 * @adis: The adis device
 391 * @reg: The address of the lower of the two registers
 392 * @mask: Bitmask to change
 393 * @val: Value to be written
 394 *
 395 * This macro evaluates the sizeof of @val at compile time and calls
 396 * adis_update_bits_base() accordingly. Be aware that using MACROS/DEFINES for
 397 * @val can lead to undesired behavior if the register to update is 16bit.
 398 */
 399#define __adis_update_bits(adis, reg, mask, val) ({                     \
 400        BUILD_BUG_ON(sizeof(val) == 1 || sizeof(val) == 8);             \
 401        __builtin_choose_expr(sizeof(val) == 4,                         \
 402                __adis_update_bits_base(adis, reg, mask, val, 4),       \
 403                __adis_update_bits_base(adis, reg, mask, val, 2));      \
 404})
 405
 406int adis_enable_irq(struct adis *adis, bool enable);
 407int __adis_check_status(struct adis *adis);
 408int __adis_initial_startup(struct adis *adis);
 409
 410static inline int adis_check_status(struct adis *adis)
 411{
 412        int ret;
 413
 414        mutex_lock(&adis->state_lock);
 415        ret = __adis_check_status(adis);
 416        mutex_unlock(&adis->state_lock);
 417
 418        return ret;
 419}
 420
 421/* locked version of __adis_initial_startup() */
 422static inline int adis_initial_startup(struct adis *adis)
 423{
 424        int ret;
 425
 426        mutex_lock(&adis->state_lock);
 427        ret = __adis_initial_startup(adis);
 428        mutex_unlock(&adis->state_lock);
 429
 430        return ret;
 431}
 432
 433static inline void adis_dev_lock(struct adis *adis)
 434{
 435        mutex_lock(&adis->state_lock);
 436}
 437
 438static inline void adis_dev_unlock(struct adis *adis)
 439{
 440        mutex_unlock(&adis->state_lock);
 441}
 442
 443int adis_single_conversion(struct iio_dev *indio_dev,
 444        const struct iio_chan_spec *chan, unsigned int error_mask,
 445        int *val);
 446
 447#define ADIS_VOLTAGE_CHAN(addr, si, chan, name, info_all, bits) { \
 448        .type = IIO_VOLTAGE, \
 449        .indexed = 1, \
 450        .channel = (chan), \
 451        .extend_name = name, \
 452        .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
 453                BIT(IIO_CHAN_INFO_SCALE), \
 454        .info_mask_shared_by_all = info_all, \
 455        .address = (addr), \
 456        .scan_index = (si), \
 457        .scan_type = { \
 458                .sign = 'u', \
 459                .realbits = (bits), \
 460                .storagebits = 16, \
 461                .endianness = IIO_BE, \
 462        }, \
 463}
 464
 465#define ADIS_SUPPLY_CHAN(addr, si, info_all, bits) \
 466        ADIS_VOLTAGE_CHAN(addr, si, 0, "supply", info_all, bits)
 467
 468#define ADIS_AUX_ADC_CHAN(addr, si, info_all, bits) \
 469        ADIS_VOLTAGE_CHAN(addr, si, 1, NULL, info_all, bits)
 470
 471#define ADIS_TEMP_CHAN(addr, si, info_all, bits) { \
 472        .type = IIO_TEMP, \
 473        .indexed = 1, \
 474        .channel = 0, \
 475        .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
 476                BIT(IIO_CHAN_INFO_SCALE) | \
 477                BIT(IIO_CHAN_INFO_OFFSET), \
 478        .info_mask_shared_by_all = info_all, \
 479        .address = (addr), \
 480        .scan_index = (si), \
 481        .scan_type = { \
 482                .sign = 'u', \
 483                .realbits = (bits), \
 484                .storagebits = 16, \
 485                .endianness = IIO_BE, \
 486        }, \
 487}
 488
 489#define ADIS_MOD_CHAN(_type, mod, addr, si, info_sep, info_all, bits) { \
 490        .type = (_type), \
 491        .modified = 1, \
 492        .channel2 = IIO_MOD_ ## mod, \
 493        .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
 494                 info_sep, \
 495        .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
 496        .info_mask_shared_by_all = info_all, \
 497        .address = (addr), \
 498        .scan_index = (si), \
 499        .scan_type = { \
 500                .sign = 's', \
 501                .realbits = (bits), \
 502                .storagebits = 16, \
 503                .endianness = IIO_BE, \
 504        }, \
 505}
 506
 507#define ADIS_ACCEL_CHAN(mod, addr, si, info_sep, info_all, bits) \
 508        ADIS_MOD_CHAN(IIO_ACCEL, mod, addr, si, info_sep, info_all, bits)
 509
 510#define ADIS_GYRO_CHAN(mod, addr, si, info_sep, info_all, bits)         \
 511        ADIS_MOD_CHAN(IIO_ANGL_VEL, mod, addr, si, info_sep, info_all, bits)
 512
 513#define ADIS_INCLI_CHAN(mod, addr, si, info_sep, info_all, bits) \
 514        ADIS_MOD_CHAN(IIO_INCLI, mod, addr, si, info_sep, info_all, bits)
 515
 516#define ADIS_ROT_CHAN(mod, addr, si, info_sep, info_all, bits) \
 517        ADIS_MOD_CHAN(IIO_ROT, mod, addr, si, info_sep, info_all, bits)
 518
 519#ifdef CONFIG_IIO_ADIS_LIB_BUFFER
 520
 521int
 522devm_adis_setup_buffer_and_trigger(struct adis *adis, struct iio_dev *indio_dev,
 523                                   irq_handler_t trigger_handler);
 524
 525int devm_adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev);
 526
 527int adis_update_scan_mode(struct iio_dev *indio_dev,
 528        const unsigned long *scan_mask);
 529
 530#else /* CONFIG_IIO_BUFFER */
 531
 532static inline int
 533devm_adis_setup_buffer_and_trigger(struct adis *adis, struct iio_dev *indio_dev,
 534                                   irq_handler_t trigger_handler)
 535{
 536        return 0;
 537}
 538
 539static inline int devm_adis_probe_trigger(struct adis *adis,
 540                                          struct iio_dev *indio_dev)
 541{
 542        return 0;
 543}
 544
 545#define adis_update_scan_mode NULL
 546
 547#endif /* CONFIG_IIO_BUFFER */
 548
 549#ifdef CONFIG_DEBUG_FS
 550
 551int adis_debugfs_reg_access(struct iio_dev *indio_dev,
 552        unsigned int reg, unsigned int writeval, unsigned int *readval);
 553
 554#else
 555
 556#define adis_debugfs_reg_access NULL
 557
 558#endif
 559
 560#endif
 561