linux/drivers/iio/temperature/ltc2983.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Analog Devices LTC2983 Multi-Sensor Digital Temperature Measurement System
   4 * driver
   5 *
   6 * Copyright 2019 Analog Devices Inc.
   7 */
   8#include <linux/bitfield.h>
   9#include <linux/completion.h>
  10#include <linux/device.h>
  11#include <linux/kernel.h>
  12#include <linux/iio/iio.h>
  13#include <linux/interrupt.h>
  14#include <linux/list.h>
  15#include <linux/module.h>
  16#include <linux/of_gpio.h>
  17#include <linux/regmap.h>
  18#include <linux/spi/spi.h>
  19
  20/* register map */
  21#define LTC2983_STATUS_REG                      0x0000
  22#define LTC2983_TEMP_RES_START_REG              0x0010
  23#define LTC2983_TEMP_RES_END_REG                0x005F
  24#define LTC2983_GLOBAL_CONFIG_REG               0x00F0
  25#define LTC2983_MULT_CHANNEL_START_REG          0x00F4
  26#define LTC2983_MULT_CHANNEL_END_REG            0x00F7
  27#define LTC2983_MUX_CONFIG_REG                  0x00FF
  28#define LTC2983_CHAN_ASSIGN_START_REG           0x0200
  29#define LTC2983_CHAN_ASSIGN_END_REG             0x024F
  30#define LTC2983_CUST_SENS_TBL_START_REG         0x0250
  31#define LTC2983_CUST_SENS_TBL_END_REG           0x03CF
  32
  33#define LTC2983_DIFFERENTIAL_CHAN_MIN           2
  34#define LTC2983_MAX_CHANNELS_NR                 20
  35#define LTC2983_MIN_CHANNELS_NR                 1
  36#define LTC2983_SLEEP                           0x97
  37#define LTC2983_CUSTOM_STEINHART_SIZE           24
  38#define LTC2983_CUSTOM_SENSOR_ENTRY_SZ          6
  39#define LTC2983_CUSTOM_STEINHART_ENTRY_SZ       4
  40
  41#define LTC2983_CHAN_START_ADDR(chan) \
  42                        (((chan - 1) * 4) + LTC2983_CHAN_ASSIGN_START_REG)
  43#define LTC2983_CHAN_RES_ADDR(chan) \
  44                        (((chan - 1) * 4) + LTC2983_TEMP_RES_START_REG)
  45#define LTC2983_THERMOCOUPLE_DIFF_MASK          BIT(3)
  46#define LTC2983_THERMOCOUPLE_SGL(x) \
  47                                FIELD_PREP(LTC2983_THERMOCOUPLE_DIFF_MASK, x)
  48#define LTC2983_THERMOCOUPLE_OC_CURR_MASK       GENMASK(1, 0)
  49#define LTC2983_THERMOCOUPLE_OC_CURR(x) \
  50                                FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CURR_MASK, x)
  51#define LTC2983_THERMOCOUPLE_OC_CHECK_MASK      BIT(2)
  52#define LTC2983_THERMOCOUPLE_OC_CHECK(x) \
  53                        FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CHECK_MASK, x)
  54
  55#define LTC2983_THERMISTOR_DIFF_MASK            BIT(2)
  56#define LTC2983_THERMISTOR_SGL(x) \
  57                                FIELD_PREP(LTC2983_THERMISTOR_DIFF_MASK, x)
  58#define LTC2983_THERMISTOR_R_SHARE_MASK         BIT(1)
  59#define LTC2983_THERMISTOR_R_SHARE(x) \
  60                                FIELD_PREP(LTC2983_THERMISTOR_R_SHARE_MASK, x)
  61#define LTC2983_THERMISTOR_C_ROTATE_MASK        BIT(0)
  62#define LTC2983_THERMISTOR_C_ROTATE(x) \
  63                                FIELD_PREP(LTC2983_THERMISTOR_C_ROTATE_MASK, x)
  64
  65#define LTC2983_DIODE_DIFF_MASK                 BIT(2)
  66#define LTC2983_DIODE_SGL(x) \
  67                        FIELD_PREP(LTC2983_DIODE_DIFF_MASK, x)
  68#define LTC2983_DIODE_3_CONV_CYCLE_MASK         BIT(1)
  69#define LTC2983_DIODE_3_CONV_CYCLE(x) \
  70                                FIELD_PREP(LTC2983_DIODE_3_CONV_CYCLE_MASK, x)
  71#define LTC2983_DIODE_AVERAGE_ON_MASK           BIT(0)
  72#define LTC2983_DIODE_AVERAGE_ON(x) \
  73                                FIELD_PREP(LTC2983_DIODE_AVERAGE_ON_MASK, x)
  74
  75#define LTC2983_RTD_4_WIRE_MASK                 BIT(3)
  76#define LTC2983_RTD_ROTATION_MASK               BIT(1)
  77#define LTC2983_RTD_C_ROTATE(x) \
  78                        FIELD_PREP(LTC2983_RTD_ROTATION_MASK, x)
  79#define LTC2983_RTD_KELVIN_R_SENSE_MASK         GENMASK(3, 2)
  80#define LTC2983_RTD_N_WIRES_MASK                GENMASK(3, 2)
  81#define LTC2983_RTD_N_WIRES(x) \
  82                        FIELD_PREP(LTC2983_RTD_N_WIRES_MASK, x)
  83#define LTC2983_RTD_R_SHARE_MASK                BIT(0)
  84#define LTC2983_RTD_R_SHARE(x) \
  85                        FIELD_PREP(LTC2983_RTD_R_SHARE_MASK, 1)
  86
  87#define LTC2983_COMMON_HARD_FAULT_MASK  GENMASK(31, 30)
  88#define LTC2983_COMMON_SOFT_FAULT_MASK  GENMASK(27, 25)
  89
  90#define LTC2983_STATUS_START_MASK       BIT(7)
  91#define LTC2983_STATUS_START(x)         FIELD_PREP(LTC2983_STATUS_START_MASK, x)
  92
  93#define LTC2983_STATUS_CHAN_SEL_MASK    GENMASK(4, 0)
  94#define LTC2983_STATUS_CHAN_SEL(x) \
  95                                FIELD_PREP(LTC2983_STATUS_CHAN_SEL_MASK, x)
  96
  97#define LTC2983_TEMP_UNITS_MASK         BIT(2)
  98#define LTC2983_TEMP_UNITS(x)           FIELD_PREP(LTC2983_TEMP_UNITS_MASK, x)
  99
 100#define LTC2983_NOTCH_FREQ_MASK         GENMASK(1, 0)
 101#define LTC2983_NOTCH_FREQ(x)           FIELD_PREP(LTC2983_NOTCH_FREQ_MASK, x)
 102
 103#define LTC2983_RES_VALID_MASK          BIT(24)
 104#define LTC2983_DATA_MASK               GENMASK(23, 0)
 105#define LTC2983_DATA_SIGN_BIT           23
 106
 107#define LTC2983_CHAN_TYPE_MASK          GENMASK(31, 27)
 108#define LTC2983_CHAN_TYPE(x)            FIELD_PREP(LTC2983_CHAN_TYPE_MASK, x)
 109
 110/* cold junction for thermocouples and rsense for rtd's and thermistor's */
 111#define LTC2983_CHAN_ASSIGN_MASK        GENMASK(26, 22)
 112#define LTC2983_CHAN_ASSIGN(x)          FIELD_PREP(LTC2983_CHAN_ASSIGN_MASK, x)
 113
 114#define LTC2983_CUSTOM_LEN_MASK         GENMASK(5, 0)
 115#define LTC2983_CUSTOM_LEN(x)           FIELD_PREP(LTC2983_CUSTOM_LEN_MASK, x)
 116
 117#define LTC2983_CUSTOM_ADDR_MASK        GENMASK(11, 6)
 118#define LTC2983_CUSTOM_ADDR(x)          FIELD_PREP(LTC2983_CUSTOM_ADDR_MASK, x)
 119
 120#define LTC2983_THERMOCOUPLE_CFG_MASK   GENMASK(21, 18)
 121#define LTC2983_THERMOCOUPLE_CFG(x) \
 122                                FIELD_PREP(LTC2983_THERMOCOUPLE_CFG_MASK, x)
 123#define LTC2983_THERMOCOUPLE_HARD_FAULT_MASK    GENMASK(31, 29)
 124#define LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK    GENMASK(28, 25)
 125
 126#define LTC2983_RTD_CFG_MASK            GENMASK(21, 18)
 127#define LTC2983_RTD_CFG(x)              FIELD_PREP(LTC2983_RTD_CFG_MASK, x)
 128#define LTC2983_RTD_EXC_CURRENT_MASK    GENMASK(17, 14)
 129#define LTC2983_RTD_EXC_CURRENT(x) \
 130                                FIELD_PREP(LTC2983_RTD_EXC_CURRENT_MASK, x)
 131#define LTC2983_RTD_CURVE_MASK          GENMASK(13, 12)
 132#define LTC2983_RTD_CURVE(x)            FIELD_PREP(LTC2983_RTD_CURVE_MASK, x)
 133
 134#define LTC2983_THERMISTOR_CFG_MASK     GENMASK(21, 19)
 135#define LTC2983_THERMISTOR_CFG(x) \
 136                                FIELD_PREP(LTC2983_THERMISTOR_CFG_MASK, x)
 137#define LTC2983_THERMISTOR_EXC_CURRENT_MASK     GENMASK(18, 15)
 138#define LTC2983_THERMISTOR_EXC_CURRENT(x) \
 139                        FIELD_PREP(LTC2983_THERMISTOR_EXC_CURRENT_MASK, x)
 140
 141#define LTC2983_DIODE_CFG_MASK          GENMASK(26, 24)
 142#define LTC2983_DIODE_CFG(x)            FIELD_PREP(LTC2983_DIODE_CFG_MASK, x)
 143#define LTC2983_DIODE_EXC_CURRENT_MASK  GENMASK(23, 22)
 144#define LTC2983_DIODE_EXC_CURRENT(x) \
 145                                FIELD_PREP(LTC2983_DIODE_EXC_CURRENT_MASK, x)
 146#define LTC2983_DIODE_IDEAL_FACTOR_MASK GENMASK(21, 0)
 147#define LTC2983_DIODE_IDEAL_FACTOR(x) \
 148                                FIELD_PREP(LTC2983_DIODE_IDEAL_FACTOR_MASK, x)
 149
 150#define LTC2983_R_SENSE_VAL_MASK        GENMASK(26, 0)
 151#define LTC2983_R_SENSE_VAL(x)          FIELD_PREP(LTC2983_R_SENSE_VAL_MASK, x)
 152
 153#define LTC2983_ADC_SINGLE_ENDED_MASK   BIT(26)
 154#define LTC2983_ADC_SINGLE_ENDED(x) \
 155                                FIELD_PREP(LTC2983_ADC_SINGLE_ENDED_MASK, x)
 156
 157enum {
 158        LTC2983_SENSOR_THERMOCOUPLE = 1,
 159        LTC2983_SENSOR_THERMOCOUPLE_CUSTOM = 9,
 160        LTC2983_SENSOR_RTD = 10,
 161        LTC2983_SENSOR_RTD_CUSTOM = 18,
 162        LTC2983_SENSOR_THERMISTOR = 19,
 163        LTC2983_SENSOR_THERMISTOR_STEINHART = 26,
 164        LTC2983_SENSOR_THERMISTOR_CUSTOM = 27,
 165        LTC2983_SENSOR_DIODE = 28,
 166        LTC2983_SENSOR_SENSE_RESISTOR = 29,
 167        LTC2983_SENSOR_DIRECT_ADC = 30,
 168};
 169
 170#define to_thermocouple(_sensor) \
 171                container_of(_sensor, struct ltc2983_thermocouple, sensor)
 172
 173#define to_rtd(_sensor) \
 174                container_of(_sensor, struct ltc2983_rtd, sensor)
 175
 176#define to_thermistor(_sensor) \
 177                container_of(_sensor, struct ltc2983_thermistor, sensor)
 178
 179#define to_diode(_sensor) \
 180                container_of(_sensor, struct ltc2983_diode, sensor)
 181
 182#define to_rsense(_sensor) \
 183                container_of(_sensor, struct ltc2983_rsense, sensor)
 184
 185#define to_adc(_sensor) \
 186                container_of(_sensor, struct ltc2983_adc, sensor)
 187
 188struct ltc2983_data {
 189        struct regmap *regmap;
 190        struct spi_device *spi;
 191        struct mutex lock;
 192        struct completion completion;
 193        struct iio_chan_spec *iio_chan;
 194        struct ltc2983_sensor **sensors;
 195        u32 mux_delay_config;
 196        u32 filter_notch_freq;
 197        u16 custom_table_size;
 198        u8 num_channels;
 199        u8 iio_channels;
 200        /*
 201         * DMA (thus cache coherency maintenance) requires the
 202         * transfer buffers to live in their own cache lines.
 203         * Holds the converted temperature
 204         */
 205        __be32 temp ____cacheline_aligned;
 206};
 207
 208struct ltc2983_sensor {
 209        int (*fault_handler)(const struct ltc2983_data *st, const u32 result);
 210        int (*assign_chan)(struct ltc2983_data *st,
 211                           const struct ltc2983_sensor *sensor);
 212        /* specifies the sensor channel */
 213        u32 chan;
 214        /* sensor type */
 215        u32 type;
 216};
 217
 218struct ltc2983_custom_sensor {
 219        /* raw table sensor data */
 220        u8 *table;
 221        size_t size;
 222        /* address offset */
 223        s8 offset;
 224        bool is_steinhart;
 225};
 226
 227struct ltc2983_thermocouple {
 228        struct ltc2983_sensor sensor;
 229        struct ltc2983_custom_sensor *custom;
 230        u32 sensor_config;
 231        u32 cold_junction_chan;
 232};
 233
 234struct ltc2983_rtd {
 235        struct ltc2983_sensor sensor;
 236        struct ltc2983_custom_sensor *custom;
 237        u32 sensor_config;
 238        u32 r_sense_chan;
 239        u32 excitation_current;
 240        u32 rtd_curve;
 241};
 242
 243struct ltc2983_thermistor {
 244        struct ltc2983_sensor sensor;
 245        struct ltc2983_custom_sensor *custom;
 246        u32 sensor_config;
 247        u32 r_sense_chan;
 248        u32 excitation_current;
 249};
 250
 251struct ltc2983_diode {
 252        struct ltc2983_sensor sensor;
 253        u32 sensor_config;
 254        u32 excitation_current;
 255        u32 ideal_factor_value;
 256};
 257
 258struct ltc2983_rsense {
 259        struct ltc2983_sensor sensor;
 260        u32 r_sense_val;
 261};
 262
 263struct ltc2983_adc {
 264        struct ltc2983_sensor sensor;
 265        bool single_ended;
 266};
 267
 268/*
 269 * Convert to Q format numbers. These number's are integers where
 270 * the number of integer and fractional bits are specified. The resolution
 271 * is given by 1/@resolution and tell us the number of fractional bits. For
 272 * instance a resolution of 2^-10 means we have 10 fractional bits.
 273 */
 274static u32 __convert_to_raw(const u64 val, const u32 resolution)
 275{
 276        u64 __res = val * resolution;
 277
 278        /* all values are multiplied by 1000000 to remove the fraction */
 279        do_div(__res, 1000000);
 280
 281        return __res;
 282}
 283
 284static u32 __convert_to_raw_sign(const u64 val, const u32 resolution)
 285{
 286        s64 __res = -(s32)val;
 287
 288        __res = __convert_to_raw(__res, resolution);
 289
 290        return (u32)-__res;
 291}
 292
 293static int __ltc2983_fault_handler(const struct ltc2983_data *st,
 294                                   const u32 result, const u32 hard_mask,
 295                                   const u32 soft_mask)
 296{
 297        const struct device *dev = &st->spi->dev;
 298
 299        if (result & hard_mask) {
 300                dev_err(dev, "Invalid conversion: Sensor HARD fault\n");
 301                return -EIO;
 302        } else if (result & soft_mask) {
 303                /* just print a warning */
 304                dev_warn(dev, "Suspicious conversion: Sensor SOFT fault\n");
 305        }
 306
 307        return 0;
 308}
 309
 310static int __ltc2983_chan_assign_common(const struct ltc2983_data *st,
 311                                        const struct ltc2983_sensor *sensor,
 312                                        u32 chan_val)
 313{
 314        u32 reg = LTC2983_CHAN_START_ADDR(sensor->chan);
 315        __be32 __chan_val;
 316
 317        chan_val |= LTC2983_CHAN_TYPE(sensor->type);
 318        dev_dbg(&st->spi->dev, "Assign reg:0x%04X, val:0x%08X\n", reg,
 319                chan_val);
 320        __chan_val = cpu_to_be32(chan_val);
 321        return regmap_bulk_write(st->regmap, reg, &__chan_val,
 322                                 sizeof(__chan_val));
 323}
 324
 325static int __ltc2983_chan_custom_sensor_assign(struct ltc2983_data *st,
 326                                          struct ltc2983_custom_sensor *custom,
 327                                          u32 *chan_val)
 328{
 329        u32 reg;
 330        u8 mult = custom->is_steinhart ? LTC2983_CUSTOM_STEINHART_ENTRY_SZ :
 331                LTC2983_CUSTOM_SENSOR_ENTRY_SZ;
 332        const struct device *dev = &st->spi->dev;
 333        /*
 334         * custom->size holds the raw size of the table. However, when
 335         * configuring the sensor channel, we must write the number of
 336         * entries of the table minus 1. For steinhart sensors 0 is written
 337         * since the size is constant!
 338         */
 339        const u8 len = custom->is_steinhart ? 0 :
 340                (custom->size / LTC2983_CUSTOM_SENSOR_ENTRY_SZ) - 1;
 341        /*
 342         * Check if the offset was assigned already. It should be for steinhart
 343         * sensors. When coming from sleep, it should be assigned for all.
 344         */
 345        if (custom->offset < 0) {
 346                /*
 347                 * This needs to be done again here because, from the moment
 348                 * when this test was done (successfully) for this custom
 349                 * sensor, a steinhart sensor might have been added changing
 350                 * custom_table_size...
 351                 */
 352                if (st->custom_table_size + custom->size >
 353                    (LTC2983_CUST_SENS_TBL_END_REG -
 354                     LTC2983_CUST_SENS_TBL_START_REG) + 1) {
 355                        dev_err(dev,
 356                                "Not space left(%d) for new custom sensor(%zu)",
 357                                st->custom_table_size,
 358                                custom->size);
 359                        return -EINVAL;
 360                }
 361
 362                custom->offset = st->custom_table_size /
 363                                        LTC2983_CUSTOM_SENSOR_ENTRY_SZ;
 364                st->custom_table_size += custom->size;
 365        }
 366
 367        reg = (custom->offset * mult) + LTC2983_CUST_SENS_TBL_START_REG;
 368
 369        *chan_val |= LTC2983_CUSTOM_LEN(len);
 370        *chan_val |= LTC2983_CUSTOM_ADDR(custom->offset);
 371        dev_dbg(dev, "Assign custom sensor, reg:0x%04X, off:%d, sz:%zu",
 372                reg, custom->offset,
 373                custom->size);
 374        /* write custom sensor table */
 375        return regmap_bulk_write(st->regmap, reg, custom->table, custom->size);
 376}
 377
 378static struct ltc2983_custom_sensor *__ltc2983_custom_sensor_new(
 379                                                struct ltc2983_data *st,
 380                                                const struct device_node *np,
 381                                                const char *propname,
 382                                                const bool is_steinhart,
 383                                                const u32 resolution,
 384                                                const bool has_signed)
 385{
 386        struct ltc2983_custom_sensor *new_custom;
 387        u8 index, n_entries, tbl = 0;
 388        struct device *dev = &st->spi->dev;
 389        /*
 390         * For custom steinhart, the full u32 is taken. For all the others
 391         * the MSB is discarded.
 392         */
 393        const u8 n_size = is_steinhart ? 4 : 3;
 394        const u8 e_size = is_steinhart ? sizeof(u32) : sizeof(u64);
 395
 396        n_entries = of_property_count_elems_of_size(np, propname, e_size);
 397        /* n_entries must be an even number */
 398        if (!n_entries || (n_entries % 2) != 0) {
 399                dev_err(dev, "Number of entries either 0 or not even\n");
 400                return ERR_PTR(-EINVAL);
 401        }
 402
 403        new_custom = devm_kzalloc(dev, sizeof(*new_custom), GFP_KERNEL);
 404        if (!new_custom)
 405                return ERR_PTR(-ENOMEM);
 406
 407        new_custom->size = n_entries * n_size;
 408        /* check Steinhart size */
 409        if (is_steinhart && new_custom->size != LTC2983_CUSTOM_STEINHART_SIZE) {
 410                dev_err(dev, "Steinhart sensors size(%zu) must be 24",
 411                                                        new_custom->size);
 412                return ERR_PTR(-EINVAL);
 413        }
 414        /* Check space on the table. */
 415        if (st->custom_table_size + new_custom->size >
 416            (LTC2983_CUST_SENS_TBL_END_REG -
 417             LTC2983_CUST_SENS_TBL_START_REG) + 1) {
 418                dev_err(dev, "No space left(%d) for new custom sensor(%zu)",
 419                                st->custom_table_size, new_custom->size);
 420                return ERR_PTR(-EINVAL);
 421        }
 422
 423        /* allocate the table */
 424        new_custom->table = devm_kzalloc(dev, new_custom->size, GFP_KERNEL);
 425        if (!new_custom->table)
 426                return ERR_PTR(-ENOMEM);
 427
 428        for (index = 0; index < n_entries; index++) {
 429                u64 temp = 0, j;
 430                /*
 431                 * Steinhart sensors are configured with raw values in the
 432                 * devicetree. For the other sensors we must convert the
 433                 * value to raw. The odd index's correspond to temperarures
 434                 * and always have 1/1024 of resolution. Temperatures also
 435                 * come in kelvin, so signed values is not possible
 436                 */
 437                if (!is_steinhart) {
 438                        of_property_read_u64_index(np, propname, index, &temp);
 439
 440                        if ((index % 2) != 0)
 441                                temp = __convert_to_raw(temp, 1024);
 442                        else if (has_signed && (s64)temp < 0)
 443                                temp = __convert_to_raw_sign(temp, resolution);
 444                        else
 445                                temp = __convert_to_raw(temp, resolution);
 446                } else {
 447                        u32 t32;
 448
 449                        of_property_read_u32_index(np, propname, index, &t32);
 450                        temp = t32;
 451                }
 452
 453                for (j = 0; j < n_size; j++)
 454                        new_custom->table[tbl++] =
 455                                temp >> (8 * (n_size - j - 1));
 456        }
 457
 458        new_custom->is_steinhart = is_steinhart;
 459        /*
 460         * This is done to first add all the steinhart sensors to the table,
 461         * in order to maximize the table usage. If we mix adding steinhart
 462         * with the other sensors, we might have to do some roundup to make
 463         * sure that sensor_addr - 0x250(start address) is a multiple of 4
 464         * (for steinhart), and a multiple of 6 for all the other sensors.
 465         * Since we have const 24 bytes for steinhart sensors and 24 is
 466         * also a multiple of 6, we guarantee that the first non-steinhart
 467         * sensor will sit in a correct address without the need of filling
 468         * addresses.
 469         */
 470        if (is_steinhart) {
 471                new_custom->offset = st->custom_table_size /
 472                                        LTC2983_CUSTOM_STEINHART_ENTRY_SZ;
 473                st->custom_table_size += new_custom->size;
 474        } else {
 475                /* mark as unset. This is checked later on the assign phase */
 476                new_custom->offset = -1;
 477        }
 478
 479        return new_custom;
 480}
 481
 482static int ltc2983_thermocouple_fault_handler(const struct ltc2983_data *st,
 483                                              const u32 result)
 484{
 485        return __ltc2983_fault_handler(st, result,
 486                                       LTC2983_THERMOCOUPLE_HARD_FAULT_MASK,
 487                                       LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK);
 488}
 489
 490static int ltc2983_common_fault_handler(const struct ltc2983_data *st,
 491                                        const u32 result)
 492{
 493        return __ltc2983_fault_handler(st, result,
 494                                       LTC2983_COMMON_HARD_FAULT_MASK,
 495                                       LTC2983_COMMON_SOFT_FAULT_MASK);
 496}
 497
 498static int ltc2983_thermocouple_assign_chan(struct ltc2983_data *st,
 499                                const struct ltc2983_sensor *sensor)
 500{
 501        struct ltc2983_thermocouple *thermo = to_thermocouple(sensor);
 502        u32 chan_val;
 503
 504        chan_val = LTC2983_CHAN_ASSIGN(thermo->cold_junction_chan);
 505        chan_val |= LTC2983_THERMOCOUPLE_CFG(thermo->sensor_config);
 506
 507        if (thermo->custom) {
 508                int ret;
 509
 510                ret = __ltc2983_chan_custom_sensor_assign(st, thermo->custom,
 511                                                          &chan_val);
 512                if (ret)
 513                        return ret;
 514        }
 515        return __ltc2983_chan_assign_common(st, sensor, chan_val);
 516}
 517
 518static int ltc2983_rtd_assign_chan(struct ltc2983_data *st,
 519                                   const struct ltc2983_sensor *sensor)
 520{
 521        struct ltc2983_rtd *rtd = to_rtd(sensor);
 522        u32 chan_val;
 523
 524        chan_val = LTC2983_CHAN_ASSIGN(rtd->r_sense_chan);
 525        chan_val |= LTC2983_RTD_CFG(rtd->sensor_config);
 526        chan_val |= LTC2983_RTD_EXC_CURRENT(rtd->excitation_current);
 527        chan_val |= LTC2983_RTD_CURVE(rtd->rtd_curve);
 528
 529        if (rtd->custom) {
 530                int ret;
 531
 532                ret = __ltc2983_chan_custom_sensor_assign(st, rtd->custom,
 533                                                          &chan_val);
 534                if (ret)
 535                        return ret;
 536        }
 537        return __ltc2983_chan_assign_common(st, sensor, chan_val);
 538}
 539
 540static int ltc2983_thermistor_assign_chan(struct ltc2983_data *st,
 541                                          const struct ltc2983_sensor *sensor)
 542{
 543        struct ltc2983_thermistor *thermistor = to_thermistor(sensor);
 544        u32 chan_val;
 545
 546        chan_val = LTC2983_CHAN_ASSIGN(thermistor->r_sense_chan);
 547        chan_val |= LTC2983_THERMISTOR_CFG(thermistor->sensor_config);
 548        chan_val |=
 549                LTC2983_THERMISTOR_EXC_CURRENT(thermistor->excitation_current);
 550
 551        if (thermistor->custom) {
 552                int ret;
 553
 554                ret = __ltc2983_chan_custom_sensor_assign(st,
 555                                                          thermistor->custom,
 556                                                          &chan_val);
 557                if (ret)
 558                        return ret;
 559        }
 560        return __ltc2983_chan_assign_common(st, sensor, chan_val);
 561}
 562
 563static int ltc2983_diode_assign_chan(struct ltc2983_data *st,
 564                                     const struct ltc2983_sensor *sensor)
 565{
 566        struct ltc2983_diode *diode = to_diode(sensor);
 567        u32 chan_val;
 568
 569        chan_val = LTC2983_DIODE_CFG(diode->sensor_config);
 570        chan_val |= LTC2983_DIODE_EXC_CURRENT(diode->excitation_current);
 571        chan_val |= LTC2983_DIODE_IDEAL_FACTOR(diode->ideal_factor_value);
 572
 573        return __ltc2983_chan_assign_common(st, sensor, chan_val);
 574}
 575
 576static int ltc2983_r_sense_assign_chan(struct ltc2983_data *st,
 577                                       const struct ltc2983_sensor *sensor)
 578{
 579        struct ltc2983_rsense *rsense = to_rsense(sensor);
 580        u32 chan_val;
 581
 582        chan_val = LTC2983_R_SENSE_VAL(rsense->r_sense_val);
 583
 584        return __ltc2983_chan_assign_common(st, sensor, chan_val);
 585}
 586
 587static int ltc2983_adc_assign_chan(struct ltc2983_data *st,
 588                                   const struct ltc2983_sensor *sensor)
 589{
 590        struct ltc2983_adc *adc = to_adc(sensor);
 591        u32 chan_val;
 592
 593        chan_val = LTC2983_ADC_SINGLE_ENDED(adc->single_ended);
 594
 595        return __ltc2983_chan_assign_common(st, sensor, chan_val);
 596}
 597
 598static struct ltc2983_sensor *ltc2983_thermocouple_new(
 599                                        const struct device_node *child,
 600                                        struct ltc2983_data *st,
 601                                        const struct ltc2983_sensor *sensor)
 602{
 603        struct ltc2983_thermocouple *thermo;
 604        struct device_node *phandle;
 605        u32 oc_current;
 606        int ret;
 607
 608        thermo = devm_kzalloc(&st->spi->dev, sizeof(*thermo), GFP_KERNEL);
 609        if (!thermo)
 610                return ERR_PTR(-ENOMEM);
 611
 612        if (of_property_read_bool(child, "adi,single-ended"))
 613                thermo->sensor_config = LTC2983_THERMOCOUPLE_SGL(1);
 614
 615        ret = of_property_read_u32(child, "adi,sensor-oc-current-microamp",
 616                                   &oc_current);
 617        if (!ret) {
 618                switch (oc_current) {
 619                case 10:
 620                        thermo->sensor_config |=
 621                                        LTC2983_THERMOCOUPLE_OC_CURR(0);
 622                        break;
 623                case 100:
 624                        thermo->sensor_config |=
 625                                        LTC2983_THERMOCOUPLE_OC_CURR(1);
 626                        break;
 627                case 500:
 628                        thermo->sensor_config |=
 629                                        LTC2983_THERMOCOUPLE_OC_CURR(2);
 630                        break;
 631                case 1000:
 632                        thermo->sensor_config |=
 633                                        LTC2983_THERMOCOUPLE_OC_CURR(3);
 634                        break;
 635                default:
 636                        dev_err(&st->spi->dev,
 637                                "Invalid open circuit current:%u", oc_current);
 638                        return ERR_PTR(-EINVAL);
 639                }
 640
 641                thermo->sensor_config |= LTC2983_THERMOCOUPLE_OC_CHECK(1);
 642        }
 643        /* validate channel index */
 644        if (!(thermo->sensor_config & LTC2983_THERMOCOUPLE_DIFF_MASK) &&
 645            sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
 646                dev_err(&st->spi->dev,
 647                        "Invalid chann:%d for differential thermocouple",
 648                        sensor->chan);
 649                return ERR_PTR(-EINVAL);
 650        }
 651
 652        phandle = of_parse_phandle(child, "adi,cold-junction-handle", 0);
 653        if (phandle) {
 654                int ret;
 655
 656                ret = of_property_read_u32(phandle, "reg",
 657                                           &thermo->cold_junction_chan);
 658                if (ret) {
 659                        /*
 660                         * This would be catched later but we can just return
 661                         * the error right away.
 662                         */
 663                        dev_err(&st->spi->dev, "Property reg must be given\n");
 664                        of_node_put(phandle);
 665                        return ERR_PTR(-EINVAL);
 666                }
 667        }
 668
 669        /* check custom sensor */
 670        if (sensor->type == LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
 671                const char *propname = "adi,custom-thermocouple";
 672
 673                thermo->custom = __ltc2983_custom_sensor_new(st, child,
 674                                                             propname, false,
 675                                                             16384, true);
 676                if (IS_ERR(thermo->custom)) {
 677                        of_node_put(phandle);
 678                        return ERR_CAST(thermo->custom);
 679                }
 680        }
 681
 682        /* set common parameters */
 683        thermo->sensor.fault_handler = ltc2983_thermocouple_fault_handler;
 684        thermo->sensor.assign_chan = ltc2983_thermocouple_assign_chan;
 685
 686        of_node_put(phandle);
 687        return &thermo->sensor;
 688}
 689
 690static struct ltc2983_sensor *ltc2983_rtd_new(const struct device_node *child,
 691                                          struct ltc2983_data *st,
 692                                          const struct ltc2983_sensor *sensor)
 693{
 694        struct ltc2983_rtd *rtd;
 695        int ret = 0;
 696        struct device *dev = &st->spi->dev;
 697        struct device_node *phandle;
 698        u32 excitation_current = 0, n_wires = 0;
 699
 700        rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
 701        if (!rtd)
 702                return ERR_PTR(-ENOMEM);
 703
 704        phandle = of_parse_phandle(child, "adi,rsense-handle", 0);
 705        if (!phandle) {
 706                dev_err(dev, "Property adi,rsense-handle missing or invalid");
 707                return ERR_PTR(-EINVAL);
 708        }
 709
 710        ret = of_property_read_u32(phandle, "reg", &rtd->r_sense_chan);
 711        if (ret) {
 712                dev_err(dev, "Property reg must be given\n");
 713                goto fail;
 714        }
 715
 716        ret = of_property_read_u32(child, "adi,number-of-wires", &n_wires);
 717        if (!ret) {
 718                switch (n_wires) {
 719                case 2:
 720                        rtd->sensor_config = LTC2983_RTD_N_WIRES(0);
 721                        break;
 722                case 3:
 723                        rtd->sensor_config = LTC2983_RTD_N_WIRES(1);
 724                        break;
 725                case 4:
 726                        rtd->sensor_config = LTC2983_RTD_N_WIRES(2);
 727                        break;
 728                case 5:
 729                        /* 4 wires, Kelvin Rsense */
 730                        rtd->sensor_config = LTC2983_RTD_N_WIRES(3);
 731                        break;
 732                default:
 733                        dev_err(dev, "Invalid number of wires:%u\n", n_wires);
 734                        ret = -EINVAL;
 735                        goto fail;
 736                }
 737        }
 738
 739        if (of_property_read_bool(child, "adi,rsense-share")) {
 740                /* Current rotation is only available with rsense sharing */
 741                if (of_property_read_bool(child, "adi,current-rotate")) {
 742                        if (n_wires == 2 || n_wires == 3) {
 743                                dev_err(dev,
 744                                        "Rotation not allowed for 2/3 Wire RTDs");
 745                                ret = -EINVAL;
 746                                goto fail;
 747                        }
 748                        rtd->sensor_config |= LTC2983_RTD_C_ROTATE(1);
 749                } else {
 750                        rtd->sensor_config |= LTC2983_RTD_R_SHARE(1);
 751                }
 752        }
 753        /*
 754         * rtd channel indexes are a bit more complicated to validate.
 755         * For 4wire RTD with rotation, the channel selection cannot be
 756         * >=19 since the chann + 1 is used in this configuration.
 757         * For 4wire RTDs with kelvin rsense, the rsense channel cannot be
 758         * <=1 since chanel - 1 and channel - 2 are used.
 759         */
 760        if (rtd->sensor_config & LTC2983_RTD_4_WIRE_MASK) {
 761                /* 4-wire */
 762                u8 min = LTC2983_DIFFERENTIAL_CHAN_MIN,
 763                        max = LTC2983_MAX_CHANNELS_NR;
 764
 765                if (rtd->sensor_config & LTC2983_RTD_ROTATION_MASK)
 766                        max = LTC2983_MAX_CHANNELS_NR - 1;
 767
 768                if (((rtd->sensor_config & LTC2983_RTD_KELVIN_R_SENSE_MASK)
 769                     == LTC2983_RTD_KELVIN_R_SENSE_MASK) &&
 770                    (rtd->r_sense_chan <=  min)) {
 771                        /* kelvin rsense*/
 772                        dev_err(dev,
 773                                "Invalid rsense chann:%d to use in kelvin rsense",
 774                                rtd->r_sense_chan);
 775
 776                        ret = -EINVAL;
 777                        goto fail;
 778                }
 779
 780                if (sensor->chan < min || sensor->chan > max) {
 781                        dev_err(dev, "Invalid chann:%d for the rtd config",
 782                                sensor->chan);
 783
 784                        ret = -EINVAL;
 785                        goto fail;
 786                }
 787        } else {
 788                /* same as differential case */
 789                if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
 790                        dev_err(&st->spi->dev,
 791                                "Invalid chann:%d for RTD", sensor->chan);
 792
 793                        ret = -EINVAL;
 794                        goto fail;
 795                }
 796        }
 797
 798        /* check custom sensor */
 799        if (sensor->type == LTC2983_SENSOR_RTD_CUSTOM) {
 800                rtd->custom = __ltc2983_custom_sensor_new(st, child,
 801                                                          "adi,custom-rtd",
 802                                                          false, 2048, false);
 803                if (IS_ERR(rtd->custom)) {
 804                        of_node_put(phandle);
 805                        return ERR_CAST(rtd->custom);
 806                }
 807        }
 808
 809        /* set common parameters */
 810        rtd->sensor.fault_handler = ltc2983_common_fault_handler;
 811        rtd->sensor.assign_chan = ltc2983_rtd_assign_chan;
 812
 813        ret = of_property_read_u32(child, "adi,excitation-current-microamp",
 814                                   &excitation_current);
 815        if (ret) {
 816                /* default to 5uA */
 817                rtd->excitation_current = 1;
 818        } else {
 819                switch (excitation_current) {
 820                case 5:
 821                        rtd->excitation_current = 0x01;
 822                        break;
 823                case 10:
 824                        rtd->excitation_current = 0x02;
 825                        break;
 826                case 25:
 827                        rtd->excitation_current = 0x03;
 828                        break;
 829                case 50:
 830                        rtd->excitation_current = 0x04;
 831                        break;
 832                case 100:
 833                        rtd->excitation_current = 0x05;
 834                        break;
 835                case 250:
 836                        rtd->excitation_current = 0x06;
 837                        break;
 838                case 500:
 839                        rtd->excitation_current = 0x07;
 840                        break;
 841                case 1000:
 842                        rtd->excitation_current = 0x08;
 843                        break;
 844                default:
 845                        dev_err(&st->spi->dev,
 846                                "Invalid value for excitation current(%u)",
 847                                excitation_current);
 848                        ret = -EINVAL;
 849                        goto fail;
 850                }
 851        }
 852
 853        of_property_read_u32(child, "adi,rtd-curve", &rtd->rtd_curve);
 854
 855        of_node_put(phandle);
 856        return &rtd->sensor;
 857fail:
 858        of_node_put(phandle);
 859        return ERR_PTR(ret);
 860}
 861
 862static struct ltc2983_sensor *ltc2983_thermistor_new(
 863                                        const struct device_node *child,
 864                                        struct ltc2983_data *st,
 865                                        const struct ltc2983_sensor *sensor)
 866{
 867        struct ltc2983_thermistor *thermistor;
 868        struct device *dev = &st->spi->dev;
 869        struct device_node *phandle;
 870        u32 excitation_current = 0;
 871        int ret = 0;
 872
 873        thermistor = devm_kzalloc(dev, sizeof(*thermistor), GFP_KERNEL);
 874        if (!thermistor)
 875                return ERR_PTR(-ENOMEM);
 876
 877        phandle = of_parse_phandle(child, "adi,rsense-handle", 0);
 878        if (!phandle) {
 879                dev_err(dev, "Property adi,rsense-handle missing or invalid");
 880                return ERR_PTR(-EINVAL);
 881        }
 882
 883        ret = of_property_read_u32(phandle, "reg", &thermistor->r_sense_chan);
 884        if (ret) {
 885                dev_err(dev, "rsense channel must be configured...\n");
 886                goto fail;
 887        }
 888
 889        if (of_property_read_bool(child, "adi,single-ended")) {
 890                thermistor->sensor_config = LTC2983_THERMISTOR_SGL(1);
 891        } else if (of_property_read_bool(child, "adi,rsense-share")) {
 892                /* rotation is only possible if sharing rsense */
 893                if (of_property_read_bool(child, "adi,current-rotate"))
 894                        thermistor->sensor_config =
 895                                                LTC2983_THERMISTOR_C_ROTATE(1);
 896                else
 897                        thermistor->sensor_config =
 898                                                LTC2983_THERMISTOR_R_SHARE(1);
 899        }
 900        /* validate channel index */
 901        if (!(thermistor->sensor_config & LTC2983_THERMISTOR_DIFF_MASK) &&
 902            sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
 903                dev_err(&st->spi->dev,
 904                        "Invalid chann:%d for differential thermistor",
 905                        sensor->chan);
 906                ret = -EINVAL;
 907                goto fail;
 908        }
 909
 910        /* check custom sensor */
 911        if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) {
 912                bool steinhart = false;
 913                const char *propname;
 914
 915                if (sensor->type == LTC2983_SENSOR_THERMISTOR_STEINHART) {
 916                        steinhart = true;
 917                        propname = "adi,custom-steinhart";
 918                } else {
 919                        propname = "adi,custom-thermistor";
 920                }
 921
 922                thermistor->custom = __ltc2983_custom_sensor_new(st, child,
 923                                                                 propname,
 924                                                                 steinhart,
 925                                                                 64, false);
 926                if (IS_ERR(thermistor->custom)) {
 927                        of_node_put(phandle);
 928                        return ERR_CAST(thermistor->custom);
 929                }
 930        }
 931        /* set common parameters */
 932        thermistor->sensor.fault_handler = ltc2983_common_fault_handler;
 933        thermistor->sensor.assign_chan = ltc2983_thermistor_assign_chan;
 934
 935        ret = of_property_read_u32(child, "adi,excitation-current-nanoamp",
 936                                   &excitation_current);
 937        if (ret) {
 938                /* Auto range is not allowed for custom sensors */
 939                if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART)
 940                        /* default to 1uA */
 941                        thermistor->excitation_current = 0x03;
 942                else
 943                        /* default to auto-range */
 944                        thermistor->excitation_current = 0x0c;
 945        } else {
 946                switch (excitation_current) {
 947                case 0:
 948                        /* auto range */
 949                        if (sensor->type >=
 950                            LTC2983_SENSOR_THERMISTOR_STEINHART) {
 951                                dev_err(&st->spi->dev,
 952                                        "Auto Range not allowed for custom sensors\n");
 953                                ret = -EINVAL;
 954                                goto fail;
 955                        }
 956                        thermistor->excitation_current = 0x0c;
 957                        break;
 958                case 250:
 959                        thermistor->excitation_current = 0x01;
 960                        break;
 961                case 500:
 962                        thermistor->excitation_current = 0x02;
 963                        break;
 964                case 1000:
 965                        thermistor->excitation_current = 0x03;
 966                        break;
 967                case 5000:
 968                        thermistor->excitation_current = 0x04;
 969                        break;
 970                case 10000:
 971                        thermistor->excitation_current = 0x05;
 972                        break;
 973                case 25000:
 974                        thermistor->excitation_current = 0x06;
 975                        break;
 976                case 50000:
 977                        thermistor->excitation_current = 0x07;
 978                        break;
 979                case 100000:
 980                        thermistor->excitation_current = 0x08;
 981                        break;
 982                case 250000:
 983                        thermistor->excitation_current = 0x09;
 984                        break;
 985                case 500000:
 986                        thermistor->excitation_current = 0x0a;
 987                        break;
 988                case 1000000:
 989                        thermistor->excitation_current = 0x0b;
 990                        break;
 991                default:
 992                        dev_err(&st->spi->dev,
 993                                "Invalid value for excitation current(%u)",
 994                                excitation_current);
 995                        ret = -EINVAL;
 996                        goto fail;
 997                }
 998        }
 999
1000        of_node_put(phandle);
1001        return &thermistor->sensor;
1002fail:
1003        of_node_put(phandle);
1004        return ERR_PTR(ret);
1005}
1006
1007static struct ltc2983_sensor *ltc2983_diode_new(
1008                                        const struct device_node *child,
1009                                        const struct ltc2983_data *st,
1010                                        const struct ltc2983_sensor *sensor)
1011{
1012        struct ltc2983_diode *diode;
1013        u32 temp = 0, excitation_current = 0;
1014        int ret;
1015
1016        diode = devm_kzalloc(&st->spi->dev, sizeof(*diode), GFP_KERNEL);
1017        if (!diode)
1018                return ERR_PTR(-ENOMEM);
1019
1020        if (of_property_read_bool(child, "adi,single-ended"))
1021                diode->sensor_config = LTC2983_DIODE_SGL(1);
1022
1023        if (of_property_read_bool(child, "adi,three-conversion-cycles"))
1024                diode->sensor_config |= LTC2983_DIODE_3_CONV_CYCLE(1);
1025
1026        if (of_property_read_bool(child, "adi,average-on"))
1027                diode->sensor_config |= LTC2983_DIODE_AVERAGE_ON(1);
1028
1029        /* validate channel index */
1030        if (!(diode->sensor_config & LTC2983_DIODE_DIFF_MASK) &&
1031            sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
1032                dev_err(&st->spi->dev,
1033                        "Invalid chann:%d for differential thermistor",
1034                        sensor->chan);
1035                return ERR_PTR(-EINVAL);
1036        }
1037        /* set common parameters */
1038        diode->sensor.fault_handler = ltc2983_common_fault_handler;
1039        diode->sensor.assign_chan = ltc2983_diode_assign_chan;
1040
1041        ret = of_property_read_u32(child, "adi,excitation-current-microamp",
1042                                   &excitation_current);
1043        if (!ret) {
1044                switch (excitation_current) {
1045                case 10:
1046                        diode->excitation_current = 0x00;
1047                        break;
1048                case 20:
1049                        diode->excitation_current = 0x01;
1050                        break;
1051                case 40:
1052                        diode->excitation_current = 0x02;
1053                        break;
1054                case 80:
1055                        diode->excitation_current = 0x03;
1056                        break;
1057                default:
1058                        dev_err(&st->spi->dev,
1059                                "Invalid value for excitation current(%u)",
1060                                excitation_current);
1061                        return ERR_PTR(-EINVAL);
1062                }
1063        }
1064
1065        of_property_read_u32(child, "adi,ideal-factor-value", &temp);
1066
1067        /* 2^20 resolution */
1068        diode->ideal_factor_value = __convert_to_raw(temp, 1048576);
1069
1070        return &diode->sensor;
1071}
1072
1073static struct ltc2983_sensor *ltc2983_r_sense_new(struct device_node *child,
1074                                        struct ltc2983_data *st,
1075                                        const struct ltc2983_sensor *sensor)
1076{
1077        struct ltc2983_rsense *rsense;
1078        int ret;
1079        u32 temp;
1080
1081        rsense = devm_kzalloc(&st->spi->dev, sizeof(*rsense), GFP_KERNEL);
1082        if (!rsense)
1083                return ERR_PTR(-ENOMEM);
1084
1085        /* validate channel index */
1086        if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
1087                dev_err(&st->spi->dev, "Invalid chann:%d for r_sense",
1088                        sensor->chan);
1089                return ERR_PTR(-EINVAL);
1090        }
1091
1092        ret = of_property_read_u32(child, "adi,rsense-val-milli-ohms", &temp);
1093        if (ret) {
1094                dev_err(&st->spi->dev, "Property adi,rsense-val-milli-ohms missing\n");
1095                return ERR_PTR(-EINVAL);
1096        }
1097        /*
1098         * Times 1000 because we have milli-ohms and __convert_to_raw
1099         * expects scales of 1000000 which are used for all other
1100         * properties.
1101         * 2^10 resolution
1102         */
1103        rsense->r_sense_val = __convert_to_raw((u64)temp * 1000, 1024);
1104
1105        /* set common parameters */
1106        rsense->sensor.assign_chan = ltc2983_r_sense_assign_chan;
1107
1108        return &rsense->sensor;
1109}
1110
1111static struct ltc2983_sensor *ltc2983_adc_new(struct device_node *child,
1112                                         struct ltc2983_data *st,
1113                                         const struct ltc2983_sensor *sensor)
1114{
1115        struct ltc2983_adc *adc;
1116
1117        adc = devm_kzalloc(&st->spi->dev, sizeof(*adc), GFP_KERNEL);
1118        if (!adc)
1119                return ERR_PTR(-ENOMEM);
1120
1121        if (of_property_read_bool(child, "adi,single-ended"))
1122                adc->single_ended = true;
1123
1124        if (!adc->single_ended &&
1125            sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
1126                dev_err(&st->spi->dev, "Invalid chan:%d for differential adc\n",
1127                        sensor->chan);
1128                return ERR_PTR(-EINVAL);
1129        }
1130        /* set common parameters */
1131        adc->sensor.assign_chan = ltc2983_adc_assign_chan;
1132        adc->sensor.fault_handler = ltc2983_common_fault_handler;
1133
1134        return &adc->sensor;
1135}
1136
1137static int ltc2983_chan_read(struct ltc2983_data *st,
1138                        const struct ltc2983_sensor *sensor, int *val)
1139{
1140        u32 start_conversion = 0;
1141        int ret;
1142        unsigned long time;
1143
1144        start_conversion = LTC2983_STATUS_START(true);
1145        start_conversion |= LTC2983_STATUS_CHAN_SEL(sensor->chan);
1146        dev_dbg(&st->spi->dev, "Start conversion on chan:%d, status:%02X\n",
1147                sensor->chan, start_conversion);
1148        /* start conversion */
1149        ret = regmap_write(st->regmap, LTC2983_STATUS_REG, start_conversion);
1150        if (ret)
1151                return ret;
1152
1153        reinit_completion(&st->completion);
1154        /*
1155         * wait for conversion to complete.
1156         * 300 ms should be more than enough to complete the conversion.
1157         * Depending on the sensor configuration, there are 2/3 conversions
1158         * cycles of 82ms.
1159         */
1160        time = wait_for_completion_timeout(&st->completion,
1161                                           msecs_to_jiffies(300));
1162        if (!time) {
1163                dev_warn(&st->spi->dev, "Conversion timed out\n");
1164                return -ETIMEDOUT;
1165        }
1166
1167        /* read the converted data */
1168        ret = regmap_bulk_read(st->regmap, LTC2983_CHAN_RES_ADDR(sensor->chan),
1169                               &st->temp, sizeof(st->temp));
1170        if (ret)
1171                return ret;
1172
1173        *val = __be32_to_cpu(st->temp);
1174
1175        if (!(LTC2983_RES_VALID_MASK & *val)) {
1176                dev_err(&st->spi->dev, "Invalid conversion detected\n");
1177                return -EIO;
1178        }
1179
1180        ret = sensor->fault_handler(st, *val);
1181        if (ret)
1182                return ret;
1183
1184        *val = sign_extend32((*val) & LTC2983_DATA_MASK, LTC2983_DATA_SIGN_BIT);
1185        return 0;
1186}
1187
1188static int ltc2983_read_raw(struct iio_dev *indio_dev,
1189                            struct iio_chan_spec const *chan,
1190                            int *val, int *val2, long mask)
1191{
1192        struct ltc2983_data *st = iio_priv(indio_dev);
1193        int ret;
1194
1195        /* sanity check */
1196        if (chan->address >= st->num_channels) {
1197                dev_err(&st->spi->dev, "Invalid chan address:%ld",
1198                        chan->address);
1199                return -EINVAL;
1200        }
1201
1202        switch (mask) {
1203        case IIO_CHAN_INFO_RAW:
1204                mutex_lock(&st->lock);
1205                ret = ltc2983_chan_read(st, st->sensors[chan->address], val);
1206                mutex_unlock(&st->lock);
1207                return ret ?: IIO_VAL_INT;
1208        case IIO_CHAN_INFO_SCALE:
1209                switch (chan->type) {
1210                case IIO_TEMP:
1211                        /* value in milli degrees */
1212                        *val = 1000;
1213                        /* 2^10 */
1214                        *val2 = 1024;
1215                        return IIO_VAL_FRACTIONAL;
1216                case IIO_VOLTAGE:
1217                        /* value in millivolt */
1218                        *val = 1000;
1219                        /* 2^21 */
1220                        *val2 = 2097152;
1221                        return IIO_VAL_FRACTIONAL;
1222                default:
1223                        return -EINVAL;
1224                }
1225        }
1226
1227        return -EINVAL;
1228}
1229
1230static int ltc2983_reg_access(struct iio_dev *indio_dev,
1231                              unsigned int reg,
1232                              unsigned int writeval,
1233                              unsigned int *readval)
1234{
1235        struct ltc2983_data *st = iio_priv(indio_dev);
1236
1237        if (readval)
1238                return regmap_read(st->regmap, reg, readval);
1239        else
1240                return regmap_write(st->regmap, reg, writeval);
1241}
1242
1243static irqreturn_t ltc2983_irq_handler(int irq, void *data)
1244{
1245        struct ltc2983_data *st = data;
1246
1247        complete(&st->completion);
1248        return IRQ_HANDLED;
1249}
1250
1251#define LTC2983_CHAN(__type, index, __address) ({ \
1252        struct iio_chan_spec __chan = { \
1253                .type = __type, \
1254                .indexed = 1, \
1255                .channel = index, \
1256                .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
1257                .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
1258                .address = __address, \
1259        }; \
1260        __chan; \
1261})
1262
1263static int ltc2983_parse_dt(struct ltc2983_data *st)
1264{
1265        struct device_node *child;
1266        struct device *dev = &st->spi->dev;
1267        int ret = 0, chan = 0, channel_avail_mask = 0;
1268
1269        of_property_read_u32(dev->of_node, "adi,mux-delay-config-us",
1270                             &st->mux_delay_config);
1271
1272        of_property_read_u32(dev->of_node, "adi,filter-notch-freq",
1273                             &st->filter_notch_freq);
1274
1275        st->num_channels = of_get_available_child_count(dev->of_node);
1276        st->sensors = devm_kcalloc(dev, st->num_channels, sizeof(*st->sensors),
1277                                   GFP_KERNEL);
1278        if (!st->sensors)
1279                return -ENOMEM;
1280
1281        st->iio_channels = st->num_channels;
1282        for_each_available_child_of_node(dev->of_node, child) {
1283                struct ltc2983_sensor sensor;
1284
1285                ret = of_property_read_u32(child, "reg", &sensor.chan);
1286                if (ret) {
1287                        dev_err(dev, "reg property must given for child nodes\n");
1288                        goto put_child;
1289                }
1290
1291                /* check if we have a valid channel */
1292                if (sensor.chan < LTC2983_MIN_CHANNELS_NR ||
1293                    sensor.chan > LTC2983_MAX_CHANNELS_NR) {
1294                        ret = -EINVAL;
1295                        dev_err(dev,
1296                                "chan:%d must be from 1 to 20\n", sensor.chan);
1297                        goto put_child;
1298                } else if (channel_avail_mask & BIT(sensor.chan)) {
1299                        ret = -EINVAL;
1300                        dev_err(dev, "chan:%d already in use\n", sensor.chan);
1301                        goto put_child;
1302                }
1303
1304                ret = of_property_read_u32(child, "adi,sensor-type",
1305                                               &sensor.type);
1306                if (ret) {
1307                        dev_err(dev,
1308                                "adi,sensor-type property must given for child nodes\n");
1309                        goto put_child;
1310                }
1311
1312                dev_dbg(dev, "Create new sensor, type %u, chann %u",
1313                                                                sensor.type,
1314                                                                sensor.chan);
1315
1316                if (sensor.type >= LTC2983_SENSOR_THERMOCOUPLE &&
1317                    sensor.type <= LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
1318                        st->sensors[chan] = ltc2983_thermocouple_new(child, st,
1319                                                                     &sensor);
1320                } else if (sensor.type >= LTC2983_SENSOR_RTD &&
1321                           sensor.type <= LTC2983_SENSOR_RTD_CUSTOM) {
1322                        st->sensors[chan] = ltc2983_rtd_new(child, st, &sensor);
1323                } else if (sensor.type >= LTC2983_SENSOR_THERMISTOR &&
1324                           sensor.type <= LTC2983_SENSOR_THERMISTOR_CUSTOM) {
1325                        st->sensors[chan] = ltc2983_thermistor_new(child, st,
1326                                                                   &sensor);
1327                } else if (sensor.type == LTC2983_SENSOR_DIODE) {
1328                        st->sensors[chan] = ltc2983_diode_new(child, st,
1329                                                              &sensor);
1330                } else if (sensor.type == LTC2983_SENSOR_SENSE_RESISTOR) {
1331                        st->sensors[chan] = ltc2983_r_sense_new(child, st,
1332                                                                &sensor);
1333                        /* don't add rsense to iio */
1334                        st->iio_channels--;
1335                } else if (sensor.type == LTC2983_SENSOR_DIRECT_ADC) {
1336                        st->sensors[chan] = ltc2983_adc_new(child, st, &sensor);
1337                } else {
1338                        dev_err(dev, "Unknown sensor type %d\n", sensor.type);
1339                        ret = -EINVAL;
1340                        goto put_child;
1341                }
1342
1343                if (IS_ERR(st->sensors[chan])) {
1344                        dev_err(dev, "Failed to create sensor %ld",
1345                                PTR_ERR(st->sensors[chan]));
1346                        ret = PTR_ERR(st->sensors[chan]);
1347                        goto put_child;
1348                }
1349                /* set generic sensor parameters */
1350                st->sensors[chan]->chan = sensor.chan;
1351                st->sensors[chan]->type = sensor.type;
1352
1353                channel_avail_mask |= BIT(sensor.chan);
1354                chan++;
1355        }
1356
1357        return 0;
1358put_child:
1359        of_node_put(child);
1360        return ret;
1361}
1362
1363static int ltc2983_setup(struct ltc2983_data *st, bool assign_iio)
1364{
1365        u32 iio_chan_t = 0, iio_chan_v = 0, chan, iio_idx = 0;
1366        int ret;
1367        unsigned long time;
1368
1369        /* make sure the device is up */
1370        time = wait_for_completion_timeout(&st->completion,
1371                                            msecs_to_jiffies(250));
1372
1373        if (!time) {
1374                dev_err(&st->spi->dev, "Device startup timed out\n");
1375                return -ETIMEDOUT;
1376        }
1377
1378        st->iio_chan = devm_kzalloc(&st->spi->dev,
1379                                    st->iio_channels * sizeof(*st->iio_chan),
1380                                    GFP_KERNEL);
1381
1382        if (!st->iio_chan)
1383                return -ENOMEM;
1384
1385        ret = regmap_update_bits(st->regmap, LTC2983_GLOBAL_CONFIG_REG,
1386                                 LTC2983_NOTCH_FREQ_MASK,
1387                                 LTC2983_NOTCH_FREQ(st->filter_notch_freq));
1388        if (ret)
1389                return ret;
1390
1391        ret = regmap_write(st->regmap, LTC2983_MUX_CONFIG_REG,
1392                           st->mux_delay_config);
1393        if (ret)
1394                return ret;
1395
1396        for (chan = 0; chan < st->num_channels; chan++) {
1397                u32 chan_type = 0, *iio_chan;
1398
1399                ret = st->sensors[chan]->assign_chan(st, st->sensors[chan]);
1400                if (ret)
1401                        return ret;
1402                /*
1403                 * The assign_iio flag is necessary for when the device is
1404                 * coming out of sleep. In that case, we just need to
1405                 * re-configure the device channels.
1406                 * We also don't assign iio channels for rsense.
1407                 */
1408                if (st->sensors[chan]->type == LTC2983_SENSOR_SENSE_RESISTOR ||
1409                    !assign_iio)
1410                        continue;
1411
1412                /* assign iio channel */
1413                if (st->sensors[chan]->type != LTC2983_SENSOR_DIRECT_ADC) {
1414                        chan_type = IIO_TEMP;
1415                        iio_chan = &iio_chan_t;
1416                } else {
1417                        chan_type = IIO_VOLTAGE;
1418                        iio_chan = &iio_chan_v;
1419                }
1420
1421                /*
1422                 * add chan as the iio .address so that, we can directly
1423                 * reference the sensor given the iio_chan_spec
1424                 */
1425                st->iio_chan[iio_idx++] = LTC2983_CHAN(chan_type, (*iio_chan)++,
1426                                                       chan);
1427        }
1428
1429        return 0;
1430}
1431
1432static const struct regmap_range ltc2983_reg_ranges[] = {
1433        regmap_reg_range(LTC2983_STATUS_REG, LTC2983_STATUS_REG),
1434        regmap_reg_range(LTC2983_TEMP_RES_START_REG, LTC2983_TEMP_RES_END_REG),
1435        regmap_reg_range(LTC2983_GLOBAL_CONFIG_REG, LTC2983_GLOBAL_CONFIG_REG),
1436        regmap_reg_range(LTC2983_MULT_CHANNEL_START_REG,
1437                         LTC2983_MULT_CHANNEL_END_REG),
1438        regmap_reg_range(LTC2983_MUX_CONFIG_REG, LTC2983_MUX_CONFIG_REG),
1439        regmap_reg_range(LTC2983_CHAN_ASSIGN_START_REG,
1440                         LTC2983_CHAN_ASSIGN_END_REG),
1441        regmap_reg_range(LTC2983_CUST_SENS_TBL_START_REG,
1442                         LTC2983_CUST_SENS_TBL_END_REG),
1443};
1444
1445static const struct regmap_access_table ltc2983_reg_table = {
1446        .yes_ranges = ltc2983_reg_ranges,
1447        .n_yes_ranges = ARRAY_SIZE(ltc2983_reg_ranges),
1448};
1449
1450/*
1451 *  The reg_bits are actually 12 but the device needs the first *complete*
1452 *  byte for the command (R/W).
1453 */
1454static const struct regmap_config ltc2983_regmap_config = {
1455        .reg_bits = 24,
1456        .val_bits = 8,
1457        .wr_table = &ltc2983_reg_table,
1458        .rd_table = &ltc2983_reg_table,
1459        .read_flag_mask = GENMASK(1, 0),
1460        .write_flag_mask = BIT(1),
1461};
1462
1463static const struct  iio_info ltc2983_iio_info = {
1464        .read_raw = ltc2983_read_raw,
1465        .debugfs_reg_access = ltc2983_reg_access,
1466};
1467
1468static int ltc2983_probe(struct spi_device *spi)
1469{
1470        struct ltc2983_data *st;
1471        struct iio_dev *indio_dev;
1472        const char *name = spi_get_device_id(spi)->name;
1473        int ret;
1474
1475        indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
1476        if (!indio_dev)
1477                return -ENOMEM;
1478
1479        st = iio_priv(indio_dev);
1480
1481        st->regmap = devm_regmap_init_spi(spi, &ltc2983_regmap_config);
1482        if (IS_ERR(st->regmap)) {
1483                dev_err(&spi->dev, "Failed to initialize regmap\n");
1484                return PTR_ERR(st->regmap);
1485        }
1486
1487        mutex_init(&st->lock);
1488        init_completion(&st->completion);
1489        st->spi = spi;
1490        spi_set_drvdata(spi, st);
1491
1492        ret = ltc2983_parse_dt(st);
1493        if (ret)
1494                return ret;
1495        /*
1496         * let's request the irq now so it is used to sync the device
1497         * startup in ltc2983_setup()
1498         */
1499        ret = devm_request_irq(&spi->dev, spi->irq, ltc2983_irq_handler,
1500                               IRQF_TRIGGER_RISING, name, st);
1501        if (ret) {
1502                dev_err(&spi->dev, "failed to request an irq, %d", ret);
1503                return ret;
1504        }
1505
1506        ret = ltc2983_setup(st, true);
1507        if (ret)
1508                return ret;
1509
1510        indio_dev->name = name;
1511        indio_dev->num_channels = st->iio_channels;
1512        indio_dev->channels = st->iio_chan;
1513        indio_dev->modes = INDIO_DIRECT_MODE;
1514        indio_dev->info = &ltc2983_iio_info;
1515
1516        return devm_iio_device_register(&spi->dev, indio_dev);
1517}
1518
1519static int __maybe_unused ltc2983_resume(struct device *dev)
1520{
1521        struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));
1522        int dummy;
1523
1524        /* dummy read to bring the device out of sleep */
1525        regmap_read(st->regmap, LTC2983_STATUS_REG, &dummy);
1526        /* we need to re-assign the channels */
1527        return ltc2983_setup(st, false);
1528}
1529
1530static int __maybe_unused ltc2983_suspend(struct device *dev)
1531{
1532        struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));
1533
1534        return regmap_write(st->regmap, LTC2983_STATUS_REG, LTC2983_SLEEP);
1535}
1536
1537static SIMPLE_DEV_PM_OPS(ltc2983_pm_ops, ltc2983_suspend, ltc2983_resume);
1538
1539static const struct spi_device_id ltc2983_id_table[] = {
1540        { "ltc2983" },
1541        {},
1542};
1543MODULE_DEVICE_TABLE(spi, ltc2983_id_table);
1544
1545static const struct of_device_id ltc2983_of_match[] = {
1546        { .compatible = "adi,ltc2983" },
1547        {},
1548};
1549MODULE_DEVICE_TABLE(of, ltc2983_of_match);
1550
1551static struct spi_driver ltc2983_driver = {
1552        .driver = {
1553                .name = "ltc2983",
1554                .of_match_table = ltc2983_of_match,
1555                .pm = &ltc2983_pm_ops,
1556        },
1557        .probe = ltc2983_probe,
1558        .id_table = ltc2983_id_table,
1559};
1560
1561module_spi_driver(ltc2983_driver);
1562
1563MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
1564MODULE_DESCRIPTION("Analog Devices LTC2983 SPI Temperature sensors");
1565MODULE_LICENSE("GPL");
1566