root/drivers/iio/accel/adis16201.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. adis16201_read_raw
  2. adis16201_write_raw
  3. adis16201_probe
  4. adis16201_remove

   1 // SPDX-License-Identifier: GPL-2.0-or-later
   2 /*
   3  * ADIS16201 Dual-Axis Digital Inclinometer and Accelerometer
   4  *
   5  * Copyright 2010 Analog Devices Inc.
   6  */
   7 
   8 #include <linux/device.h>
   9 #include <linux/kernel.h>
  10 #include <linux/module.h>
  11 #include <linux/mutex.h>
  12 #include <linux/slab.h>
  13 #include <linux/spi/spi.h>
  14 #include <linux/sysfs.h>
  15 
  16 #include <linux/iio/iio.h>
  17 #include <linux/iio/imu/adis.h>
  18 
  19 #define ADIS16201_STARTUP_DELAY_MS                      220
  20 #define ADIS16201_FLASH_CNT                             0x00
  21 
  22 /* Data Output Register Information */
  23 #define ADIS16201_SUPPLY_OUT_REG                        0x02
  24 #define ADIS16201_XACCL_OUT_REG                         0x04
  25 #define ADIS16201_YACCL_OUT_REG                         0x06
  26 #define ADIS16201_AUX_ADC_REG                           0x08
  27 #define ADIS16201_TEMP_OUT_REG                          0x0A
  28 #define ADIS16201_XINCL_OUT_REG                         0x0C
  29 #define ADIS16201_YINCL_OUT_REG                         0x0E
  30 
  31 /* Calibration Register Definition */
  32 #define ADIS16201_XACCL_OFFS_REG                        0x10
  33 #define ADIS16201_YACCL_OFFS_REG                        0x12
  34 #define ADIS16201_XACCL_SCALE_REG                       0x14
  35 #define ADIS16201_YACCL_SCALE_REG                       0x16
  36 #define ADIS16201_XINCL_OFFS_REG                        0x18
  37 #define ADIS16201_YINCL_OFFS_REG                        0x1A
  38 #define ADIS16201_XINCL_SCALE_REG                       0x1C
  39 #define ADIS16201_YINCL_SCALE_REG                       0x1E
  40 
  41 /* Alarm Register Definition */
  42 #define ADIS16201_ALM_MAG1_REG                          0x20
  43 #define ADIS16201_ALM_MAG2_REG                          0x22
  44 #define ADIS16201_ALM_SMPL1_REG                         0x24
  45 #define ADIS16201_ALM_SMPL2_REG                         0x26
  46 #define ADIS16201_ALM_CTRL_REG                          0x28
  47 
  48 #define ADIS16201_AUX_DAC_REG                           0x30
  49 #define ADIS16201_GPIO_CTRL_REG                         0x32
  50 #define ADIS16201_SMPL_PRD_REG                          0x36
  51 /* Operation, filter configuration */
  52 #define ADIS16201_AVG_CNT_REG                           0x38
  53 #define ADIS16201_SLP_CNT_REG                           0x3A
  54 
  55 /* Miscellaneous Control Register Definition */
  56 #define ADIS16201_MSC_CTRL_REG                          0x34
  57 #define  ADIS16201_MSC_CTRL_SELF_TEST_EN                BIT(8)
  58 /* Data-ready enable: 1 = enabled, 0 = disabled */
  59 #define  ADIS16201_MSC_CTRL_DATA_RDY_EN                 BIT(2)
  60 /* Data-ready polarity: 1 = active high, 0 = active low */
  61 #define  ADIS16201_MSC_CTRL_ACTIVE_DATA_RDY_HIGH        BIT(1)
  62 /* Data-ready line selection: 1 = DIO1, 0 = DIO0 */
  63 #define  ADIS16201_MSC_CTRL_DATA_RDY_DIO1               BIT(0)
  64 
  65 /* Diagnostics System Status Register Definition */
  66 #define ADIS16201_DIAG_STAT_REG                         0x3C
  67 #define  ADIS16201_DIAG_STAT_ALARM2                     BIT(9)
  68 #define  ADIS16201_DIAG_STAT_ALARM1                     BIT(8)
  69 #define  ADIS16201_DIAG_STAT_SPI_FAIL_BIT               3
  70 #define  ADIS16201_DIAG_STAT_FLASH_UPT_FAIL_BIT         2
  71 /* Power supply above 3.625 V */
  72 #define  ADIS16201_DIAG_STAT_POWER_HIGH_BIT             1
  73 /* Power supply below 2.975 V */
  74 #define  ADIS16201_DIAG_STAT_POWER_LOW_BIT              0
  75 
  76 /* System Command Register Definition */
  77 #define ADIS16201_GLOB_CMD_REG                          0x3E
  78 #define  ADIS16201_GLOB_CMD_SW_RESET                    BIT(7)
  79 #define  ADIS16201_GLOB_CMD_FACTORY_RESET               BIT(1)
  80 
  81 #define ADIS16201_ERROR_ACTIVE                          BIT(14)
  82 
  83 enum adis16201_scan {
  84         ADIS16201_SCAN_ACC_X,
  85         ADIS16201_SCAN_ACC_Y,
  86         ADIS16201_SCAN_INCLI_X,
  87         ADIS16201_SCAN_INCLI_Y,
  88         ADIS16201_SCAN_SUPPLY,
  89         ADIS16201_SCAN_AUX_ADC,
  90         ADIS16201_SCAN_TEMP,
  91 };
  92 
  93 static const u8 adis16201_addresses[] = {
  94         [ADIS16201_SCAN_ACC_X] = ADIS16201_XACCL_OFFS_REG,
  95         [ADIS16201_SCAN_ACC_Y] = ADIS16201_YACCL_OFFS_REG,
  96         [ADIS16201_SCAN_INCLI_X] = ADIS16201_XINCL_OFFS_REG,
  97         [ADIS16201_SCAN_INCLI_Y] = ADIS16201_YINCL_OFFS_REG,
  98 };
  99 
 100 static int adis16201_read_raw(struct iio_dev *indio_dev,
 101                               struct iio_chan_spec const *chan,
 102                               int *val, int *val2,
 103                               long mask)
 104 {
 105         struct adis *st = iio_priv(indio_dev);
 106         int ret;
 107         int bits;
 108         u8 addr;
 109         s16 val16;
 110 
 111         switch (mask) {
 112         case IIO_CHAN_INFO_RAW:
 113                 return adis_single_conversion(indio_dev, chan,
 114                                               ADIS16201_ERROR_ACTIVE, val);
 115         case IIO_CHAN_INFO_SCALE:
 116                 switch (chan->type) {
 117                 case IIO_VOLTAGE:
 118                         if (chan->channel == 0) {
 119                         /* Voltage base units are mV hence 1.22 mV */
 120                                 *val = 1;
 121                                 *val2 = 220000;
 122                         } else {
 123                         /* Voltage base units are mV hence 0.61 mV */
 124                                 *val = 0;
 125                                 *val2 = 610000;
 126                         }
 127                         return IIO_VAL_INT_PLUS_MICRO;
 128                 case IIO_TEMP:
 129                         *val = -470;
 130                         *val2 = 0;
 131                         return IIO_VAL_INT_PLUS_MICRO;
 132                 case IIO_ACCEL:
 133                         /*
 134                          * IIO base unit for sensitivity of accelerometer
 135                          * is milli g.
 136                          * 1 LSB represents 0.244 mg.
 137                          */
 138                         *val = 0;
 139                         *val2 = IIO_G_TO_M_S_2(462400);
 140                         return IIO_VAL_INT_PLUS_NANO;
 141                 case IIO_INCLI:
 142                         *val = 0;
 143                         *val2 = 100000;
 144                         return IIO_VAL_INT_PLUS_MICRO;
 145                 default:
 146                         return -EINVAL;
 147                 }
 148                 break;
 149         case IIO_CHAN_INFO_OFFSET:
 150                 /*
 151                  * The raw ADC value is 1278 when the temperature
 152                  * is 25 degrees and the scale factor per milli
 153                  * degree celcius is -470.
 154                  */
 155                 *val = 25000 / -470 - 1278;
 156                 return IIO_VAL_INT;
 157         case IIO_CHAN_INFO_CALIBBIAS:
 158                 switch (chan->type) {
 159                 case IIO_ACCEL:
 160                         bits = 12;
 161                         break;
 162                 case IIO_INCLI:
 163                         bits = 9;
 164                         break;
 165                 default:
 166                         return -EINVAL;
 167                 }
 168                 addr = adis16201_addresses[chan->scan_index];
 169                 ret = adis_read_reg_16(st, addr, &val16);
 170                 if (ret)
 171                         return ret;
 172 
 173                 *val = sign_extend32(val16, bits - 1);
 174                 return IIO_VAL_INT;
 175         }
 176 
 177         return -EINVAL;
 178 }
 179 
 180 static int adis16201_write_raw(struct iio_dev *indio_dev,
 181                                struct iio_chan_spec const *chan,
 182                                int val,
 183                                int val2,
 184                                long mask)
 185 {
 186         struct adis *st = iio_priv(indio_dev);
 187         int m;
 188 
 189         if (mask != IIO_CHAN_INFO_CALIBBIAS)
 190                 return -EINVAL;
 191 
 192         switch (chan->type) {
 193         case IIO_ACCEL:
 194                 m = GENMASK(11, 0);
 195                 break;
 196         case IIO_INCLI:
 197                 m = GENMASK(8, 0);
 198                 break;
 199         default:
 200                 return -EINVAL;
 201         }
 202 
 203         return adis_write_reg_16(st, adis16201_addresses[chan->scan_index],
 204                                  val & m);
 205 }
 206 
 207 static const struct iio_chan_spec adis16201_channels[] = {
 208         ADIS_SUPPLY_CHAN(ADIS16201_SUPPLY_OUT_REG, ADIS16201_SCAN_SUPPLY, 0,
 209                          12),
 210         ADIS_TEMP_CHAN(ADIS16201_TEMP_OUT_REG, ADIS16201_SCAN_TEMP, 0, 12),
 211         ADIS_ACCEL_CHAN(X, ADIS16201_XACCL_OUT_REG, ADIS16201_SCAN_ACC_X,
 212                         BIT(IIO_CHAN_INFO_CALIBBIAS), 0, 14),
 213         ADIS_ACCEL_CHAN(Y, ADIS16201_YACCL_OUT_REG, ADIS16201_SCAN_ACC_Y,
 214                         BIT(IIO_CHAN_INFO_CALIBBIAS), 0, 14),
 215         ADIS_AUX_ADC_CHAN(ADIS16201_AUX_ADC_REG, ADIS16201_SCAN_AUX_ADC, 0, 12),
 216         ADIS_INCLI_CHAN(X, ADIS16201_XINCL_OUT_REG, ADIS16201_SCAN_INCLI_X,
 217                         BIT(IIO_CHAN_INFO_CALIBBIAS), 0, 14),
 218         ADIS_INCLI_CHAN(X, ADIS16201_YINCL_OUT_REG, ADIS16201_SCAN_INCLI_Y,
 219                         BIT(IIO_CHAN_INFO_CALIBBIAS), 0, 14),
 220         IIO_CHAN_SOFT_TIMESTAMP(7)
 221 };
 222 
 223 static const struct iio_info adis16201_info = {
 224         .read_raw = adis16201_read_raw,
 225         .write_raw = adis16201_write_raw,
 226         .update_scan_mode = adis_update_scan_mode,
 227 };
 228 
 229 static const char * const adis16201_status_error_msgs[] = {
 230         [ADIS16201_DIAG_STAT_SPI_FAIL_BIT] = "SPI failure",
 231         [ADIS16201_DIAG_STAT_FLASH_UPT_FAIL_BIT] = "Flash update failed",
 232         [ADIS16201_DIAG_STAT_POWER_HIGH_BIT] = "Power supply above 3.625V",
 233         [ADIS16201_DIAG_STAT_POWER_LOW_BIT] = "Power supply below 2.975V",
 234 };
 235 
 236 static const struct adis_data adis16201_data = {
 237         .read_delay = 20,
 238         .msc_ctrl_reg = ADIS16201_MSC_CTRL_REG,
 239         .glob_cmd_reg = ADIS16201_GLOB_CMD_REG,
 240         .diag_stat_reg = ADIS16201_DIAG_STAT_REG,
 241 
 242         .self_test_mask = ADIS16201_MSC_CTRL_SELF_TEST_EN,
 243         .self_test_no_autoclear = true,
 244         .startup_delay = ADIS16201_STARTUP_DELAY_MS,
 245 
 246         .status_error_msgs = adis16201_status_error_msgs,
 247         .status_error_mask = BIT(ADIS16201_DIAG_STAT_SPI_FAIL_BIT) |
 248                 BIT(ADIS16201_DIAG_STAT_FLASH_UPT_FAIL_BIT) |
 249                 BIT(ADIS16201_DIAG_STAT_POWER_HIGH_BIT) |
 250                 BIT(ADIS16201_DIAG_STAT_POWER_LOW_BIT),
 251 };
 252 
 253 static int adis16201_probe(struct spi_device *spi)
 254 {
 255         struct iio_dev *indio_dev;
 256         struct adis *st;
 257         int ret;
 258 
 259         indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
 260         if (!indio_dev)
 261                 return -ENOMEM;
 262 
 263         st = iio_priv(indio_dev);
 264         spi_set_drvdata(spi, indio_dev);
 265 
 266         indio_dev->name = spi->dev.driver->name;
 267         indio_dev->dev.parent = &spi->dev;
 268         indio_dev->info = &adis16201_info;
 269 
 270         indio_dev->channels = adis16201_channels;
 271         indio_dev->num_channels = ARRAY_SIZE(adis16201_channels);
 272         indio_dev->modes = INDIO_DIRECT_MODE;
 273 
 274         ret = adis_init(st, indio_dev, spi, &adis16201_data);
 275         if (ret)
 276                 return ret;
 277 
 278         ret = adis_setup_buffer_and_trigger(st, indio_dev, NULL);
 279         if (ret)
 280                 return ret;
 281 
 282         ret = adis_initial_startup(st);
 283         if (ret)
 284                 goto error_cleanup_buffer_trigger;
 285 
 286         ret = iio_device_register(indio_dev);
 287         if (ret < 0)
 288                 goto error_cleanup_buffer_trigger;
 289 
 290         return 0;
 291 
 292 error_cleanup_buffer_trigger:
 293         adis_cleanup_buffer_and_trigger(st, indio_dev);
 294         return ret;
 295 }
 296 
 297 static int adis16201_remove(struct spi_device *spi)
 298 {
 299         struct iio_dev *indio_dev = spi_get_drvdata(spi);
 300         struct adis *st = iio_priv(indio_dev);
 301 
 302         iio_device_unregister(indio_dev);
 303         adis_cleanup_buffer_and_trigger(st, indio_dev);
 304 
 305         return 0;
 306 }
 307 
 308 static struct spi_driver adis16201_driver = {
 309         .driver = {
 310                 .name = "adis16201",
 311         },
 312         .probe = adis16201_probe,
 313         .remove = adis16201_remove,
 314 };
 315 module_spi_driver(adis16201_driver);
 316 
 317 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
 318 MODULE_DESCRIPTION("Analog Devices ADIS16201 Dual-Axis Digital Inclinometer and Accelerometer");
 319 MODULE_LICENSE("GPL v2");
 320 MODULE_ALIAS("spi:adis16201");

/* [<][>][^][v][top][bottom][index][help] */