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