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/err.h> 12 #include <linux/errno.h> 13 #include <linux/kernel.h> 14 #include <linux/iio/iio.h> 15 #include <linux/interrupt.h> 16 #include <linux/list.h> 17 #include <linux/mod_devicetable.h> 18 #include <linux/module.h> 19 #include <linux/property.h> 20 #include <linux/regmap.h> 21 #include <linux/regulator/consumer.h> 22 #include <linux/spi/spi.h> 23 24 #include <asm/byteorder.h> 25 #include <linux/unaligned.h> 26 27 /* register map */ 28 #define LTC2983_STATUS_REG 0x0000 29 #define LTC2983_TEMP_RES_START_REG 0x0010 30 #define LTC2983_TEMP_RES_END_REG 0x005F 31 #define ADT7604_RES_RES_START_REG 0x0060 32 #define ADT7604_RES_RES_END_REG 0x00AF 33 #define LTC2983_EEPROM_KEY_REG 0x00B0 34 #define LTC2983_EEPROM_READ_STATUS_REG 0x00D0 35 #define LTC2983_GLOBAL_CONFIG_REG 0x00F0 36 #define LTC2983_MULT_CHANNEL_START_REG 0x00F4 37 #define LTC2983_MULT_CHANNEL_END_REG 0x00F7 38 #define LTC2986_EEPROM_STATUS_REG 0x00F9 39 #define LTC2983_MUX_CONFIG_REG 0x00FF 40 #define LTC2983_CHAN_ASSIGN_START_REG 0x0200 41 #define LTC2983_CHAN_ASSIGN_END_REG 0x024F 42 #define LTC2983_CUST_SENS_TBL_START_REG 0x0250 43 #define LTC2983_CUST_SENS_TBL_END_REG 0x03CF 44 45 #define LTC2983_DIFFERENTIAL_CHAN_MIN 2 46 #define LTC2983_MIN_CHANNELS_NR 1 47 #define LTC2983_SLEEP 0x97 48 #define LTC2983_CUSTOM_STEINHART_SIZE 24 49 #define LTC2983_CUSTOM_SENSOR_ENTRY_SZ 6 50 #define LTC2983_CUSTOM_STEINHART_ENTRY_SZ 4 51 52 #define LTC2983_EEPROM_KEY 0xA53C0F5A 53 #define LTC2983_EEPROM_WRITE_CMD 0x15 54 #define LTC2983_EEPROM_READ_CMD 0x16 55 #define LTC2983_EEPROM_STATUS_FAILURE_MASK GENMASK(3, 1) 56 #define LTC2983_EEPROM_READ_FAILURE_MASK GENMASK(7, 0) 57 58 #define LTC2983_EEPROM_WRITE_TIME_MS 2600 59 #define LTC2983_EEPROM_READ_TIME_MS 20 60 61 #define LTC2983_CHAN_ASSIGN_ADDR(chan) \ 62 ((((chan) - 1) * 4) + LTC2983_CHAN_ASSIGN_START_REG) 63 #define LTC2983_RESULT_ADDR(chan, base) \ 64 ((((chan) - 1) * 4) + (base)) 65 #define LTC2983_THERMOCOUPLE_DIFF_MASK BIT(3) 66 #define LTC2983_THERMOCOUPLE_SGL(x) \ 67 FIELD_PREP(LTC2983_THERMOCOUPLE_DIFF_MASK, x) 68 #define LTC2983_THERMOCOUPLE_OC_CURR_MASK GENMASK(1, 0) 69 #define LTC2983_THERMOCOUPLE_OC_CURR(x) \ 70 FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CURR_MASK, x) 71 #define LTC2983_THERMOCOUPLE_OC_CHECK_MASK BIT(2) 72 #define LTC2983_THERMOCOUPLE_OC_CHECK(x) \ 73 FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CHECK_MASK, x) 74 75 #define LTC2983_THERMISTOR_DIFF_MASK BIT(2) 76 #define LTC2983_THERMISTOR_SGL(x) \ 77 FIELD_PREP(LTC2983_THERMISTOR_DIFF_MASK, x) 78 #define LTC2983_THERMISTOR_R_SHARE_MASK BIT(1) 79 #define LTC2983_THERMISTOR_R_SHARE(x) \ 80 FIELD_PREP(LTC2983_THERMISTOR_R_SHARE_MASK, x) 81 #define LTC2983_THERMISTOR_C_ROTATE_MASK BIT(0) 82 #define LTC2983_THERMISTOR_C_ROTATE(x) \ 83 FIELD_PREP(LTC2983_THERMISTOR_C_ROTATE_MASK, x) 84 85 #define LTC2983_DIODE_DIFF_MASK BIT(2) 86 #define LTC2983_DIODE_SGL(x) \ 87 FIELD_PREP(LTC2983_DIODE_DIFF_MASK, x) 88 #define LTC2983_DIODE_3_CONV_CYCLE_MASK BIT(1) 89 #define LTC2983_DIODE_3_CONV_CYCLE(x) \ 90 FIELD_PREP(LTC2983_DIODE_3_CONV_CYCLE_MASK, x) 91 #define LTC2983_DIODE_AVERAGE_ON_MASK BIT(0) 92 #define LTC2983_DIODE_AVERAGE_ON(x) \ 93 FIELD_PREP(LTC2983_DIODE_AVERAGE_ON_MASK, x) 94 95 #define LTC2983_RTD_4_WIRE_MASK BIT(3) 96 #define LTC2983_RTD_ROTATION_MASK BIT(1) 97 #define LTC2983_RTD_C_ROTATE(x) \ 98 FIELD_PREP(LTC2983_RTD_ROTATION_MASK, x) 99 #define LTC2983_RTD_KELVIN_R_SENSE_MASK GENMASK(3, 2) 100 #define LTC2983_RTD_N_WIRES_MASK GENMASK(3, 2) 101 #define LTC2983_RTD_N_WIRES(x) \ 102 FIELD_PREP(LTC2983_RTD_N_WIRES_MASK, x) 103 #define LTC2983_RTD_R_SHARE_MASK BIT(0) 104 #define LTC2983_RTD_R_SHARE(x) \ 105 FIELD_PREP(LTC2983_RTD_R_SHARE_MASK, 1) 106 107 #define LTC2983_COMMON_HARD_FAULT_MASK GENMASK(31, 30) 108 #define LTC2983_COMMON_SOFT_FAULT_MASK GENMASK(27, 25) 109 110 #define LTC2983_STATUS_START_MASK BIT(7) 111 #define LTC2983_STATUS_START(x) FIELD_PREP(LTC2983_STATUS_START_MASK, x) 112 #define LTC2983_STATUS_UP_MASK GENMASK(7, 6) 113 #define LTC2983_STATUS_UP(reg) FIELD_GET(LTC2983_STATUS_UP_MASK, reg) 114 115 #define LTC2983_STATUS_CHAN_SEL_MASK GENMASK(4, 0) 116 #define LTC2983_STATUS_CHAN_SEL(x) \ 117 FIELD_PREP(LTC2983_STATUS_CHAN_SEL_MASK, x) 118 119 #define LTC2983_TEMP_UNITS_MASK BIT(2) 120 #define LTC2983_TEMP_UNITS(x) FIELD_PREP(LTC2983_TEMP_UNITS_MASK, x) 121 122 #define LTC2983_NOTCH_FREQ_MASK GENMASK(1, 0) 123 #define LTC2983_NOTCH_FREQ(x) FIELD_PREP(LTC2983_NOTCH_FREQ_MASK, x) 124 125 #define LTC2983_RES_VALID_MASK BIT(24) 126 #define LTC2983_DATA_MASK GENMASK(23, 0) 127 #define LTC2983_DATA_SIGN_BIT 23 128 129 #define LTC2983_CHAN_TYPE_MASK GENMASK(31, 27) 130 #define LTC2983_CHAN_TYPE(x) FIELD_PREP(LTC2983_CHAN_TYPE_MASK, x) 131 132 /* cold junction for thermocouples and rsense for rtd's and thermistor's */ 133 #define LTC2983_CHAN_ASSIGN_MASK GENMASK(26, 22) 134 #define LTC2983_CHAN_ASSIGN(x) FIELD_PREP(LTC2983_CHAN_ASSIGN_MASK, x) 135 136 #define LTC2983_CUSTOM_LEN_MASK GENMASK(5, 0) 137 #define LTC2983_CUSTOM_LEN(x) FIELD_PREP(LTC2983_CUSTOM_LEN_MASK, x) 138 139 #define LTC2983_CUSTOM_ADDR_MASK GENMASK(11, 6) 140 #define LTC2983_CUSTOM_ADDR(x) FIELD_PREP(LTC2983_CUSTOM_ADDR_MASK, x) 141 142 #define LTC2983_THERMOCOUPLE_CFG_MASK GENMASK(21, 18) 143 #define LTC2983_THERMOCOUPLE_CFG(x) \ 144 FIELD_PREP(LTC2983_THERMOCOUPLE_CFG_MASK, x) 145 #define LTC2983_THERMOCOUPLE_HARD_FAULT_MASK GENMASK(31, 29) 146 #define LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK GENMASK(28, 25) 147 148 #define LTC2983_RTD_CFG_MASK GENMASK(21, 18) 149 #define LTC2983_RTD_CFG(x) FIELD_PREP(LTC2983_RTD_CFG_MASK, x) 150 #define LTC2983_RTD_EXC_CURRENT_MASK GENMASK(17, 14) 151 #define LTC2983_RTD_EXC_CURRENT(x) \ 152 FIELD_PREP(LTC2983_RTD_EXC_CURRENT_MASK, x) 153 #define LTC2983_RTD_CURVE_MASK GENMASK(13, 12) 154 #define LTC2983_RTD_CURVE(x) FIELD_PREP(LTC2983_RTD_CURVE_MASK, x) 155 156 #define LTC2983_THERMISTOR_CFG_MASK GENMASK(21, 19) 157 #define LTC2983_THERMISTOR_CFG(x) \ 158 FIELD_PREP(LTC2983_THERMISTOR_CFG_MASK, x) 159 #define LTC2983_THERMISTOR_EXC_CURRENT_MASK GENMASK(18, 15) 160 #define LTC2983_THERMISTOR_EXC_CURRENT(x) \ 161 FIELD_PREP(LTC2983_THERMISTOR_EXC_CURRENT_MASK, x) 162 163 #define LTC2983_DIODE_CFG_MASK GENMASK(26, 24) 164 #define LTC2983_DIODE_CFG(x) FIELD_PREP(LTC2983_DIODE_CFG_MASK, x) 165 #define LTC2983_DIODE_EXC_CURRENT_MASK GENMASK(23, 22) 166 #define LTC2983_DIODE_EXC_CURRENT(x) \ 167 FIELD_PREP(LTC2983_DIODE_EXC_CURRENT_MASK, x) 168 #define LTC2983_DIODE_IDEAL_FACTOR_MASK GENMASK(21, 0) 169 #define LTC2983_DIODE_IDEAL_FACTOR(x) \ 170 FIELD_PREP(LTC2983_DIODE_IDEAL_FACTOR_MASK, x) 171 172 #define LTC2983_R_SENSE_VAL_MASK GENMASK(26, 0) 173 #define LTC2983_R_SENSE_VAL(x) FIELD_PREP(LTC2983_R_SENSE_VAL_MASK, x) 174 175 #define LTC2983_ADC_SINGLE_ENDED_MASK BIT(26) 176 #define LTC2983_ADC_SINGLE_ENDED(x) \ 177 FIELD_PREP(LTC2983_ADC_SINGLE_ENDED_MASK, x) 178 179 enum { 180 LTC2983_SENSOR_THERMOCOUPLE = 1, 181 LTC2983_SENSOR_THERMOCOUPLE_CUSTOM = 9, 182 LTC2983_SENSOR_RTD = 10, 183 LTC2983_SENSOR_RTD_CUSTOM = 18, 184 LTC2983_SENSOR_THERMISTOR = 19, 185 LTC2983_SENSOR_THERMISTOR_STEINHART = 26, 186 LTC2983_SENSOR_THERMISTOR_CUSTOM = 27, 187 LTC2983_SENSOR_DIODE = 28, 188 LTC2983_SENSOR_SENSE_RESISTOR = 29, 189 LTC2983_SENSOR_DIRECT_ADC = 30, 190 LTC2983_SENSOR_ACTIVE_TEMP = 31, 191 /* Sensor types for some parts only; map to RTD_CUSTOM/THERMISTOR_CUSTOM in HW */ 192 LTC2983_SENSOR_COPPER_TRACE = 32, 193 LTC2983_SENSOR_LEAK_DETECTOR = 33, 194 LTC2983_SENSOR_NUM 195 }; 196 197 /* Bitmask of sensor types supported by LTC2983/LTC2984 and derivatives */ 198 #define LTC2983_COMMON_SENSORS \ 199 (GENMASK_ULL(LTC2983_SENSOR_THERMOCOUPLE_CUSTOM, LTC2983_SENSOR_THERMOCOUPLE) | \ 200 GENMASK_ULL(LTC2983_SENSOR_RTD_CUSTOM, LTC2983_SENSOR_RTD) | \ 201 GENMASK_ULL(LTC2983_SENSOR_THERMISTOR_CUSTOM, LTC2983_SENSOR_THERMISTOR) | \ 202 BIT_ULL(LTC2983_SENSOR_DIODE) | \ 203 BIT_ULL(LTC2983_SENSOR_SENSE_RESISTOR) | \ 204 BIT_ULL(LTC2983_SENSOR_DIRECT_ADC)) 205 206 /* Bitmask of sensor types supported by ADT7604 */ 207 #define ADT7604_SENSORS \ 208 (GENMASK_ULL(LTC2983_SENSOR_RTD_CUSTOM - 1, LTC2983_SENSOR_RTD) | \ 209 GENMASK_ULL(LTC2983_SENSOR_THERMISTOR_CUSTOM - 1, LTC2983_SENSOR_THERMISTOR) | \ 210 BIT_ULL(LTC2983_SENSOR_SENSE_RESISTOR) | \ 211 BIT_ULL(LTC2983_SENSOR_COPPER_TRACE) | \ 212 BIT_ULL(LTC2983_SENSOR_LEAK_DETECTOR)) 213 214 #define to_thermocouple(_sensor) \ 215 container_of(_sensor, struct ltc2983_thermocouple, sensor) 216 217 #define to_rtd(_sensor) \ 218 container_of(_sensor, struct ltc2983_rtd, sensor) 219 220 #define to_copper_trace(_sensor) \ 221 container_of(_sensor, struct ltc2983_copper_trace, sensor) 222 223 #define to_thermistor(_sensor) \ 224 container_of(_sensor, struct ltc2983_thermistor, sensor) 225 226 #define to_leak_detector(_sensor) \ 227 container_of(_sensor, struct ltc2983_leak_detector, sensor) 228 229 #define to_diode(_sensor) \ 230 container_of(_sensor, struct ltc2983_diode, sensor) 231 232 #define to_rsense(_sensor) \ 233 container_of(_sensor, struct ltc2983_rsense, sensor) 234 235 #define to_adc(_sensor) \ 236 container_of(_sensor, struct ltc2983_adc, sensor) 237 238 #define to_temp(_sensor) \ 239 container_of(_sensor, struct ltc2983_temp, sensor) 240 241 struct ltc2983_chip_info { 242 const char *name; 243 unsigned int max_channels_nr; 244 u64 supported_sensors; 245 bool has_eeprom; 246 }; 247 248 struct ltc2983_data { 249 const struct ltc2983_chip_info *info; 250 struct regmap *regmap; 251 struct spi_device *spi; 252 struct mutex lock; 253 struct completion completion; 254 struct iio_chan_spec *iio_chan; 255 struct ltc2983_sensor **sensors; 256 u32 mux_delay_config; 257 u32 filter_notch_freq; 258 u16 custom_table_size; 259 u8 num_channels; 260 u8 iio_channels; 261 /* 262 * DMA (thus cache coherency maintenance) may require the 263 * transfer buffers to live in their own cache lines. 264 * Holds the converted temperature 265 */ 266 __be32 temp __aligned(IIO_DMA_MINALIGN); 267 __be32 chan_val; 268 __be32 eeprom_key; 269 }; 270 271 struct ltc2983_sensor { 272 int (*fault_handler)(const struct ltc2983_data *st, const u32 result); 273 int (*assign_chan)(struct ltc2983_data *st, 274 const struct ltc2983_sensor *sensor); 275 /* specifies the sensor channel */ 276 u32 chan; 277 /* sensor type */ 278 u32 type; 279 /* number of IIO channels this sensor produces */ 280 u8 n_iio_chan; 281 }; 282 283 struct ltc2983_custom_sensor { 284 /* raw table sensor data */ 285 void *table; 286 size_t size; 287 /* address offset */ 288 s8 offset; 289 bool is_steinhart; 290 }; 291 292 struct ltc2983_thermocouple { 293 struct ltc2983_sensor sensor; 294 struct ltc2983_custom_sensor *custom; 295 u32 sensor_config; 296 u32 cold_junction_chan; 297 }; 298 299 struct ltc2983_rtd { 300 struct ltc2983_sensor sensor; 301 struct ltc2983_custom_sensor *custom; 302 u32 sensor_config; 303 u32 r_sense_chan; 304 u32 excitation_current; 305 u32 rtd_curve; 306 }; 307 308 struct ltc2983_copper_trace { 309 struct ltc2983_sensor sensor; 310 struct ltc2983_custom_sensor *custom; 311 u32 r_sense_chan; 312 u32 excitation_current; 313 /* selects the <1Ω variant: bits 17:0 of the channel word are zeroed, 314 * disabling excitation current and custom table fields (ADT7604 315 * datasheet Table 26) 316 */ 317 bool is_sub_ohm; 318 }; 319 320 struct ltc2983_leak_detector { 321 struct ltc2983_sensor sensor; 322 struct ltc2983_custom_sensor *custom; 323 u32 r_sense_chan; 324 u32 excitation_current; 325 }; 326 327 struct ltc2983_thermistor { 328 struct ltc2983_sensor sensor; 329 struct ltc2983_custom_sensor *custom; 330 u32 sensor_config; 331 u32 r_sense_chan; 332 u32 excitation_current; 333 }; 334 335 struct ltc2983_diode { 336 struct ltc2983_sensor sensor; 337 u32 sensor_config; 338 u32 excitation_current; 339 u32 ideal_factor_value; 340 }; 341 342 struct ltc2983_rsense { 343 struct ltc2983_sensor sensor; 344 u32 r_sense_val; 345 }; 346 347 struct ltc2983_adc { 348 struct ltc2983_sensor sensor; 349 bool single_ended; 350 }; 351 352 struct ltc2983_temp { 353 struct ltc2983_sensor sensor; 354 struct ltc2983_custom_sensor *custom; 355 bool single_ended; 356 }; 357 358 /* 359 * Convert to Q format numbers. These number's are integers where 360 * the number of integer and fractional bits are specified. The resolution 361 * is given by 1/@resolution and tell us the number of fractional bits. For 362 * instance a resolution of 2^-10 means we have 10 fractional bits. 363 */ 364 static u32 __convert_to_raw(const u64 val, const u32 resolution) 365 { 366 u64 __res = val * resolution; 367 368 /* all values are multiplied by 1000000 to remove the fraction */ 369 do_div(__res, 1000000); 370 371 return __res; 372 } 373 374 static u32 __convert_to_raw_sign(const u64 val, const u32 resolution) 375 { 376 s64 __res = -(s32)val; 377 378 __res = __convert_to_raw(__res, resolution); 379 380 return (u32)-__res; 381 } 382 383 static int __ltc2983_fault_handler(const struct ltc2983_data *st, 384 const u32 result, const u32 hard_mask, 385 const u32 soft_mask) 386 { 387 const struct device *dev = &st->spi->dev; 388 389 if (result & hard_mask) { 390 dev_err(dev, "Invalid conversion: Sensor HARD fault\n"); 391 return -EIO; 392 } else if (result & soft_mask) { 393 /* just print a warning */ 394 dev_warn(dev, "Suspicious conversion: Sensor SOFT fault\n"); 395 } 396 397 return 0; 398 } 399 400 static int __ltc2983_chan_assign_common(struct ltc2983_data *st, 401 const struct ltc2983_sensor *sensor, 402 u32 chan_val) 403 { 404 struct device *dev = &st->spi->dev; 405 u32 reg = LTC2983_CHAN_ASSIGN_ADDR(sensor->chan); 406 u32 hw_type = sensor->type; 407 408 if (hw_type == LTC2983_SENSOR_COPPER_TRACE) 409 hw_type = LTC2983_SENSOR_RTD_CUSTOM; 410 else if (hw_type == LTC2983_SENSOR_LEAK_DETECTOR) 411 hw_type = LTC2983_SENSOR_THERMISTOR_CUSTOM; 412 413 chan_val |= LTC2983_CHAN_TYPE(hw_type); 414 dev_dbg(dev, "Assign reg:0x%04X, val:0x%08X\n", reg, chan_val); 415 st->chan_val = cpu_to_be32(chan_val); 416 return regmap_bulk_write(st->regmap, reg, &st->chan_val, 417 sizeof(st->chan_val)); 418 } 419 420 static int __ltc2983_chan_custom_sensor_assign(struct ltc2983_data *st, 421 struct ltc2983_custom_sensor *custom, 422 u32 *chan_val) 423 { 424 u32 reg; 425 u8 mult = custom->is_steinhart ? LTC2983_CUSTOM_STEINHART_ENTRY_SZ : 426 LTC2983_CUSTOM_SENSOR_ENTRY_SZ; 427 const struct device *dev = &st->spi->dev; 428 /* 429 * custom->size holds the raw size of the table. However, when 430 * configuring the sensor channel, we must write the number of 431 * entries of the table minus 1. For steinhart sensors 0 is written 432 * since the size is constant! 433 */ 434 const u8 len = custom->is_steinhart ? 0 : 435 (custom->size / LTC2983_CUSTOM_SENSOR_ENTRY_SZ) - 1; 436 /* 437 * Check if the offset was assigned already. It should be for steinhart 438 * sensors. When coming from sleep, it should be assigned for all. 439 */ 440 if (custom->offset < 0) { 441 /* 442 * This needs to be done again here because, from the moment 443 * when this test was done (successfully) for this custom 444 * sensor, a steinhart sensor might have been added changing 445 * custom_table_size... 446 */ 447 if (st->custom_table_size + custom->size > 448 (LTC2983_CUST_SENS_TBL_END_REG - 449 LTC2983_CUST_SENS_TBL_START_REG) + 1) { 450 dev_err(dev, 451 "Not space left(%d) for new custom sensor(%zu)", 452 st->custom_table_size, 453 custom->size); 454 return -EINVAL; 455 } 456 457 custom->offset = st->custom_table_size / 458 LTC2983_CUSTOM_SENSOR_ENTRY_SZ; 459 st->custom_table_size += custom->size; 460 } 461 462 reg = (custom->offset * mult) + LTC2983_CUST_SENS_TBL_START_REG; 463 464 *chan_val |= LTC2983_CUSTOM_LEN(len); 465 *chan_val |= LTC2983_CUSTOM_ADDR(custom->offset); 466 dev_dbg(dev, "Assign custom sensor, reg:0x%04X, off:%d, sz:%zu", 467 reg, custom->offset, 468 custom->size); 469 /* write custom sensor table */ 470 return regmap_bulk_write(st->regmap, reg, custom->table, custom->size); 471 } 472 473 static struct ltc2983_custom_sensor * 474 __ltc2983_custom_sensor_new(struct ltc2983_data *st, const struct fwnode_handle *fn, 475 const char *propname, const bool is_steinhart, 476 const u32 resolution, const bool has_signed) 477 { 478 struct ltc2983_custom_sensor *new_custom; 479 struct device *dev = &st->spi->dev; 480 /* 481 * For custom steinhart, the full u32 is taken. For all the others 482 * the MSB is discarded. 483 */ 484 const u8 n_size = is_steinhart ? 4 : 3; 485 u8 index, n_entries; 486 int ret; 487 488 if (is_steinhart) 489 n_entries = fwnode_property_count_u32(fn, propname); 490 else 491 n_entries = fwnode_property_count_u64(fn, propname); 492 /* n_entries must be an even number */ 493 if (!n_entries || (n_entries % 2) != 0) 494 return dev_err_ptr_probe(dev, -EINVAL, 495 "Number of entries either 0 or not even\n"); 496 497 new_custom = devm_kzalloc(dev, sizeof(*new_custom), GFP_KERNEL); 498 if (!new_custom) 499 return ERR_PTR(-ENOMEM); 500 501 new_custom->size = n_entries * n_size; 502 /* check Steinhart size */ 503 if (is_steinhart && new_custom->size != LTC2983_CUSTOM_STEINHART_SIZE) 504 return dev_err_ptr_probe(dev, -EINVAL, 505 "Steinhart sensors size(%zu) must be %u\n", 506 new_custom->size, LTC2983_CUSTOM_STEINHART_SIZE); 507 508 /* Check space on the table. */ 509 if (st->custom_table_size + new_custom->size > 510 (LTC2983_CUST_SENS_TBL_END_REG - LTC2983_CUST_SENS_TBL_START_REG) + 1) 511 return dev_err_ptr_probe(dev, -EINVAL, 512 "No space left(%d) for new custom sensor(%zu)\n", 513 st->custom_table_size, new_custom->size); 514 515 /* allocate the table */ 516 if (is_steinhart) 517 new_custom->table = devm_kcalloc(dev, n_entries, sizeof(u32), GFP_KERNEL); 518 else 519 new_custom->table = devm_kcalloc(dev, n_entries, sizeof(u64), GFP_KERNEL); 520 if (!new_custom->table) 521 return ERR_PTR(-ENOMEM); 522 523 /* 524 * Steinhart sensors are configured with raw values in the firmware 525 * node. For the other sensors we must convert the value to raw. 526 * The odd index's correspond to temperatures and always have 1/1024 527 * of resolution. Temperatures also come in Kelvin, so signed values 528 * are not possible. 529 */ 530 if (is_steinhart) { 531 ret = fwnode_property_read_u32_array(fn, propname, new_custom->table, n_entries); 532 if (ret < 0) 533 return ERR_PTR(ret); 534 535 cpu_to_be32_array(new_custom->table, new_custom->table, n_entries); 536 } else { 537 ret = fwnode_property_read_u64_array(fn, propname, new_custom->table, n_entries); 538 if (ret < 0) 539 return ERR_PTR(ret); 540 541 for (index = 0; index < n_entries; index++) { 542 u64 temp = ((u64 *)new_custom->table)[index]; 543 544 /* 545 * Users specify plain coverage percentage (0-100). Convert 546 * to µK so __convert_to_raw() produces the correct hardware 547 * encoding: P + 273.15 K. 548 */ 549 if ((index % 2) != 0 && !strcmp(propname, "adi,custom-leak-detector")) 550 temp = temp * 1000000 + 273150000; 551 552 if ((index % 2) != 0) 553 temp = __convert_to_raw(temp, 1024); 554 else if (has_signed && (s64)temp < 0) 555 temp = __convert_to_raw_sign(temp, resolution); 556 else 557 temp = __convert_to_raw(temp, resolution); 558 559 put_unaligned_be24(temp, new_custom->table + index * 3); 560 } 561 } 562 563 new_custom->is_steinhart = is_steinhart; 564 /* 565 * This is done to first add all the steinhart sensors to the table, 566 * in order to maximize the table usage. If we mix adding steinhart 567 * with the other sensors, we might have to do some roundup to make 568 * sure that sensor_addr - 0x250(start address) is a multiple of 4 569 * (for steinhart), and a multiple of 6 for all the other sensors. 570 * Since we have const 24 bytes for steinhart sensors and 24 is 571 * also a multiple of 6, we guarantee that the first non-steinhart 572 * sensor will sit in a correct address without the need of filling 573 * addresses. 574 */ 575 if (is_steinhart) { 576 new_custom->offset = st->custom_table_size / 577 LTC2983_CUSTOM_STEINHART_ENTRY_SZ; 578 st->custom_table_size += new_custom->size; 579 } else { 580 /* mark as unset. This is checked later on the assign phase */ 581 new_custom->offset = -1; 582 } 583 584 return new_custom; 585 } 586 587 static int ltc2983_thermocouple_fault_handler(const struct ltc2983_data *st, 588 const u32 result) 589 { 590 return __ltc2983_fault_handler(st, result, 591 LTC2983_THERMOCOUPLE_HARD_FAULT_MASK, 592 LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK); 593 } 594 595 static int ltc2983_common_fault_handler(const struct ltc2983_data *st, 596 const u32 result) 597 { 598 return __ltc2983_fault_handler(st, result, 599 LTC2983_COMMON_HARD_FAULT_MASK, 600 LTC2983_COMMON_SOFT_FAULT_MASK); 601 } 602 603 static int ltc2983_thermocouple_assign_chan(struct ltc2983_data *st, 604 const struct ltc2983_sensor *sensor) 605 { 606 struct ltc2983_thermocouple *thermo = to_thermocouple(sensor); 607 u32 chan_val; 608 609 chan_val = LTC2983_CHAN_ASSIGN(thermo->cold_junction_chan); 610 chan_val |= LTC2983_THERMOCOUPLE_CFG(thermo->sensor_config); 611 612 if (thermo->custom) { 613 int ret; 614 615 ret = __ltc2983_chan_custom_sensor_assign(st, thermo->custom, 616 &chan_val); 617 if (ret) 618 return ret; 619 } 620 return __ltc2983_chan_assign_common(st, sensor, chan_val); 621 } 622 623 static int ltc2983_rtd_assign_chan(struct ltc2983_data *st, 624 const struct ltc2983_sensor *sensor) 625 { 626 struct ltc2983_rtd *rtd = to_rtd(sensor); 627 u32 chan_val; 628 629 chan_val = LTC2983_CHAN_ASSIGN(rtd->r_sense_chan); 630 chan_val |= LTC2983_RTD_CFG(rtd->sensor_config); 631 chan_val |= LTC2983_RTD_EXC_CURRENT(rtd->excitation_current); 632 chan_val |= LTC2983_RTD_CURVE(rtd->rtd_curve); 633 634 if (rtd->custom) { 635 int ret; 636 637 ret = __ltc2983_chan_custom_sensor_assign(st, rtd->custom, 638 &chan_val); 639 if (ret) 640 return ret; 641 } 642 return __ltc2983_chan_assign_common(st, sensor, chan_val); 643 } 644 645 static int ltc2983_copper_trace_assign_chan(struct ltc2983_data *st, 646 const struct ltc2983_sensor *sensor) 647 { 648 struct ltc2983_copper_trace *ct = to_copper_trace(sensor); 649 u32 chan_val; 650 651 chan_val = LTC2983_CHAN_ASSIGN(ct->r_sense_chan); 652 /* Sensor config bits 21:18 must be 0b1001 (ADT7604 datasheet Table 26) */ 653 chan_val |= LTC2983_RTD_CFG(0x9); 654 655 if (ct->is_sub_ohm) { 656 chan_val &= ~GENMASK(17, 0); 657 } else { 658 int ret; 659 660 chan_val |= LTC2983_RTD_EXC_CURRENT(ct->excitation_current); 661 ret = __ltc2983_chan_custom_sensor_assign(st, ct->custom, 662 &chan_val); 663 if (ret) 664 return ret; 665 } 666 667 return __ltc2983_chan_assign_common(st, sensor, chan_val); 668 } 669 670 static int ltc2983_thermistor_assign_chan(struct ltc2983_data *st, 671 const struct ltc2983_sensor *sensor) 672 { 673 struct ltc2983_thermistor *thermistor = to_thermistor(sensor); 674 u32 chan_val; 675 676 chan_val = LTC2983_CHAN_ASSIGN(thermistor->r_sense_chan); 677 chan_val |= LTC2983_THERMISTOR_CFG(thermistor->sensor_config); 678 chan_val |= 679 LTC2983_THERMISTOR_EXC_CURRENT(thermistor->excitation_current); 680 681 if (thermistor->custom) { 682 int ret; 683 684 ret = __ltc2983_chan_custom_sensor_assign(st, 685 thermistor->custom, 686 &chan_val); 687 if (ret) 688 return ret; 689 } 690 return __ltc2983_chan_assign_common(st, sensor, chan_val); 691 } 692 693 static int ltc2983_leak_detector_assign_chan(struct ltc2983_data *st, 694 const struct ltc2983_sensor *sensor) 695 { 696 struct ltc2983_leak_detector *ld = to_leak_detector(sensor); 697 u32 chan_val; 698 int ret; 699 700 chan_val = LTC2983_CHAN_ASSIGN(ld->r_sense_chan); 701 /* bits 21:19 must be 0b001 (ADT7604 datasheet Table 38) */ 702 chan_val |= LTC2983_THERMISTOR_CFG(1); 703 chan_val |= LTC2983_THERMISTOR_EXC_CURRENT(ld->excitation_current); 704 705 ret = __ltc2983_chan_custom_sensor_assign(st, ld->custom, &chan_val); 706 if (ret) 707 return ret; 708 709 return __ltc2983_chan_assign_common(st, sensor, chan_val); 710 } 711 712 static int ltc2983_diode_assign_chan(struct ltc2983_data *st, 713 const struct ltc2983_sensor *sensor) 714 { 715 struct ltc2983_diode *diode = to_diode(sensor); 716 u32 chan_val; 717 718 chan_val = LTC2983_DIODE_CFG(diode->sensor_config); 719 chan_val |= LTC2983_DIODE_EXC_CURRENT(diode->excitation_current); 720 chan_val |= LTC2983_DIODE_IDEAL_FACTOR(diode->ideal_factor_value); 721 722 return __ltc2983_chan_assign_common(st, sensor, chan_val); 723 } 724 725 static int ltc2983_r_sense_assign_chan(struct ltc2983_data *st, 726 const struct ltc2983_sensor *sensor) 727 { 728 struct ltc2983_rsense *rsense = to_rsense(sensor); 729 u32 chan_val; 730 731 chan_val = LTC2983_R_SENSE_VAL(rsense->r_sense_val); 732 733 return __ltc2983_chan_assign_common(st, sensor, chan_val); 734 } 735 736 static int ltc2983_adc_assign_chan(struct ltc2983_data *st, 737 const struct ltc2983_sensor *sensor) 738 { 739 struct ltc2983_adc *adc = to_adc(sensor); 740 u32 chan_val; 741 742 chan_val = LTC2983_ADC_SINGLE_ENDED(adc->single_ended); 743 744 return __ltc2983_chan_assign_common(st, sensor, chan_val); 745 } 746 747 static int ltc2983_temp_assign_chan(struct ltc2983_data *st, 748 const struct ltc2983_sensor *sensor) 749 { 750 struct ltc2983_temp *temp = to_temp(sensor); 751 u32 chan_val; 752 int ret; 753 754 chan_val = LTC2983_ADC_SINGLE_ENDED(temp->single_ended); 755 756 ret = __ltc2983_chan_custom_sensor_assign(st, temp->custom, &chan_val); 757 if (ret) 758 return ret; 759 760 return __ltc2983_chan_assign_common(st, sensor, chan_val); 761 } 762 763 static struct ltc2983_sensor * 764 ltc2983_thermocouple_new(const struct fwnode_handle *child, struct ltc2983_data *st, 765 const struct ltc2983_sensor *sensor) 766 { 767 struct device *dev = &st->spi->dev; 768 struct ltc2983_thermocouple *thermo; 769 u32 oc_current; 770 int ret; 771 772 thermo = devm_kzalloc(dev, sizeof(*thermo), GFP_KERNEL); 773 if (!thermo) 774 return ERR_PTR(-ENOMEM); 775 776 if (fwnode_property_read_bool(child, "adi,single-ended")) 777 thermo->sensor_config = LTC2983_THERMOCOUPLE_SGL(1); 778 779 if (fwnode_property_present(child, "adi,sensor-oc-current-microamp")) { 780 ret = fwnode_property_read_u32(child, 781 "adi,sensor-oc-current-microamp", 782 &oc_current); 783 if (ret) 784 return dev_err_ptr_probe(dev, ret, 785 "Failed to read adi,sensor-oc-current-microamp\n"); 786 787 switch (oc_current) { 788 case 10: 789 thermo->sensor_config |= 790 LTC2983_THERMOCOUPLE_OC_CURR(0); 791 break; 792 case 100: 793 thermo->sensor_config |= 794 LTC2983_THERMOCOUPLE_OC_CURR(1); 795 break; 796 case 500: 797 thermo->sensor_config |= 798 LTC2983_THERMOCOUPLE_OC_CURR(2); 799 break; 800 case 1000: 801 thermo->sensor_config |= 802 LTC2983_THERMOCOUPLE_OC_CURR(3); 803 break; 804 default: 805 return dev_err_ptr_probe(dev, -EINVAL, 806 "Invalid open circuit current:%u\n", 807 oc_current); 808 } 809 810 thermo->sensor_config |= LTC2983_THERMOCOUPLE_OC_CHECK(1); 811 } 812 /* validate channel index */ 813 if (!(thermo->sensor_config & LTC2983_THERMOCOUPLE_DIFF_MASK) && 814 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) 815 return dev_err_ptr_probe(dev, -EINVAL, 816 "Invalid channel %d for differential thermocouple\n", 817 sensor->chan); 818 819 struct fwnode_handle *ref __free(fwnode_handle) = 820 fwnode_find_reference(child, "adi,cold-junction-handle", 0); 821 if (IS_ERR(ref)) { 822 ref = NULL; 823 } else { 824 ret = fwnode_property_read_u32(ref, "reg", &thermo->cold_junction_chan); 825 if (ret) 826 /* 827 * This would be caught later but we can just return 828 * the error right away. 829 */ 830 return dev_err_ptr_probe(dev, ret, 831 "Property reg must be given\n"); 832 } 833 834 /* check custom sensor */ 835 if (sensor->type == LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) { 836 const char *propname = "adi,custom-thermocouple"; 837 838 thermo->custom = __ltc2983_custom_sensor_new(st, child, 839 propname, false, 840 16384, true); 841 if (IS_ERR(thermo->custom)) 842 return ERR_CAST(thermo->custom); 843 } 844 845 /* set common parameters */ 846 thermo->sensor.fault_handler = ltc2983_thermocouple_fault_handler; 847 thermo->sensor.assign_chan = ltc2983_thermocouple_assign_chan; 848 849 return &thermo->sensor; 850 } 851 852 static struct ltc2983_sensor * 853 ltc2983_rtd_new(const struct fwnode_handle *child, struct ltc2983_data *st, 854 const struct ltc2983_sensor *sensor) 855 { 856 struct ltc2983_rtd *rtd; 857 int ret = 0; 858 struct device *dev = &st->spi->dev; 859 u32 excitation_current = 0, n_wires = 2; 860 861 rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL); 862 if (!rtd) 863 return ERR_PTR(-ENOMEM); 864 865 struct fwnode_handle *ref __free(fwnode_handle) = 866 fwnode_find_reference(child, "adi,rsense-handle", 0); 867 if (IS_ERR(ref)) 868 return dev_err_cast_probe(dev, ref, 869 "Property adi,rsense-handle missing or invalid\n"); 870 871 ret = fwnode_property_read_u32(ref, "reg", &rtd->r_sense_chan); 872 if (ret) 873 return dev_err_ptr_probe(dev, ret, 874 "Property reg must be given\n"); 875 876 if (fwnode_property_present(child, "adi,number-of-wires")) { 877 ret = fwnode_property_read_u32(child, "adi,number-of-wires", &n_wires); 878 if (ret) 879 return dev_err_ptr_probe(dev, ret, 880 "Failed to read adi,number-of-wires\n"); 881 882 switch (n_wires) { 883 case 2: 884 rtd->sensor_config = LTC2983_RTD_N_WIRES(0); 885 break; 886 case 3: 887 rtd->sensor_config = LTC2983_RTD_N_WIRES(1); 888 break; 889 case 4: 890 rtd->sensor_config = LTC2983_RTD_N_WIRES(2); 891 break; 892 case 5: 893 /* 4 wires, Kelvin Rsense */ 894 rtd->sensor_config = LTC2983_RTD_N_WIRES(3); 895 break; 896 default: 897 return dev_err_ptr_probe(dev, -EINVAL, 898 "Invalid number of wires:%u\n", 899 n_wires); 900 } 901 } 902 903 if (fwnode_property_read_bool(child, "adi,rsense-share")) { 904 /* Current rotation is only available with rsense sharing */ 905 if (fwnode_property_read_bool(child, "adi,current-rotate")) { 906 if (n_wires == 2 || n_wires == 3) 907 return dev_err_ptr_probe(dev, -EINVAL, 908 "Rotation not allowed for 2/3 Wire RTDs\n"); 909 910 rtd->sensor_config |= LTC2983_RTD_C_ROTATE(1); 911 } else { 912 rtd->sensor_config |= LTC2983_RTD_R_SHARE(1); 913 } 914 } 915 /* 916 * rtd channel indexes are a bit more complicated to validate. 917 * For 4wire RTD with rotation, the channel selection cannot be 918 * >=19 since the channel + 1 is used in this configuration. 919 * For 4wire RTDs with kelvin rsense, the rsense channel cannot be 920 * <=1 since channel - 1 and channel - 2 are used. 921 */ 922 if (rtd->sensor_config & LTC2983_RTD_4_WIRE_MASK) { 923 /* 4-wire */ 924 u8 min = LTC2983_DIFFERENTIAL_CHAN_MIN, 925 max = st->info->max_channels_nr; 926 927 if (rtd->sensor_config & LTC2983_RTD_ROTATION_MASK) 928 max = st->info->max_channels_nr - 1; 929 930 if (((rtd->sensor_config & LTC2983_RTD_KELVIN_R_SENSE_MASK) 931 == LTC2983_RTD_KELVIN_R_SENSE_MASK) && 932 (rtd->r_sense_chan <= min)) 933 /* kelvin rsense*/ 934 return dev_err_ptr_probe(dev, -EINVAL, 935 "Invalid channel %d for kelvin rsense\n", 936 rtd->r_sense_chan); 937 938 if (sensor->chan < min || sensor->chan > max) 939 return dev_err_ptr_probe(dev, -EINVAL, 940 "Invalid channel %d for RTD config\n", 941 sensor->chan); 942 } else { 943 /* same as differential case */ 944 if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) 945 return dev_err_ptr_probe(dev, -EINVAL, 946 "Invalid channel %d for RTD\n", 947 sensor->chan); 948 } 949 950 /* check custom sensor */ 951 if (sensor->type == LTC2983_SENSOR_RTD_CUSTOM) { 952 rtd->custom = __ltc2983_custom_sensor_new(st, child, 953 "adi,custom-rtd", 954 false, 2048, false); 955 if (IS_ERR(rtd->custom)) 956 return ERR_CAST(rtd->custom); 957 } 958 959 /* set common parameters */ 960 rtd->sensor.fault_handler = ltc2983_common_fault_handler; 961 rtd->sensor.assign_chan = ltc2983_rtd_assign_chan; 962 963 if (fwnode_property_present(child, "adi,excitation-current-microamp")) { 964 ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp", 965 &excitation_current); 966 if (ret) 967 return dev_err_ptr_probe(dev, ret, 968 "Failed to read adi,excitation-current-microamp\n"); 969 970 switch (excitation_current) { 971 case 5: 972 rtd->excitation_current = 0x01; 973 break; 974 case 10: 975 rtd->excitation_current = 0x02; 976 break; 977 case 25: 978 rtd->excitation_current = 0x03; 979 break; 980 case 50: 981 rtd->excitation_current = 0x04; 982 break; 983 case 100: 984 rtd->excitation_current = 0x05; 985 break; 986 case 250: 987 rtd->excitation_current = 0x06; 988 break; 989 case 500: 990 rtd->excitation_current = 0x07; 991 break; 992 case 1000: 993 rtd->excitation_current = 0x08; 994 break; 995 default: 996 return dev_err_ptr_probe(dev, -EINVAL, 997 "Invalid value for excitation current(%u)\n", 998 excitation_current); 999 } 1000 } else { 1001 /* default to 5uA */ 1002 rtd->excitation_current = 1; 1003 } 1004 1005 if (fwnode_property_present(child, "adi,rtd-curve")) { 1006 ret = fwnode_property_read_u32(child, "adi,rtd-curve", &rtd->rtd_curve); 1007 if (ret) 1008 return dev_err_ptr_probe(dev, ret, 1009 "Failed to read adi,rtd-curve\n"); 1010 } 1011 1012 return &rtd->sensor; 1013 } 1014 1015 static struct ltc2983_sensor * 1016 ltc2983_thermistor_new(const struct fwnode_handle *child, struct ltc2983_data *st, 1017 const struct ltc2983_sensor *sensor) 1018 { 1019 struct ltc2983_thermistor *thermistor; 1020 struct device *dev = &st->spi->dev; 1021 u32 excitation_current = 0; 1022 int ret = 0; 1023 1024 thermistor = devm_kzalloc(dev, sizeof(*thermistor), GFP_KERNEL); 1025 if (!thermistor) 1026 return ERR_PTR(-ENOMEM); 1027 1028 struct fwnode_handle *ref __free(fwnode_handle) = 1029 fwnode_find_reference(child, "adi,rsense-handle", 0); 1030 if (IS_ERR(ref)) 1031 return dev_err_cast_probe(dev, ref, 1032 "Property adi,rsense-handle missing or invalid\n"); 1033 1034 ret = fwnode_property_read_u32(ref, "reg", &thermistor->r_sense_chan); 1035 if (ret) 1036 return dev_err_ptr_probe(dev, ret, 1037 "rsense channel must be configured...\n"); 1038 1039 if (fwnode_property_read_bool(child, "adi,single-ended")) { 1040 thermistor->sensor_config = LTC2983_THERMISTOR_SGL(1); 1041 } else if (fwnode_property_read_bool(child, "adi,rsense-share")) { 1042 /* rotation is only possible if sharing rsense */ 1043 if (fwnode_property_read_bool(child, "adi,current-rotate")) 1044 thermistor->sensor_config = 1045 LTC2983_THERMISTOR_C_ROTATE(1); 1046 else 1047 thermistor->sensor_config = 1048 LTC2983_THERMISTOR_R_SHARE(1); 1049 } 1050 /* validate channel index */ 1051 if (!(thermistor->sensor_config & LTC2983_THERMISTOR_DIFF_MASK) && 1052 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) 1053 return dev_err_ptr_probe(dev, -EINVAL, 1054 "Invalid channel %d for differential thermistor\n", 1055 sensor->chan); 1056 1057 /* check custom sensor */ 1058 if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) { 1059 bool steinhart = false; 1060 const char *propname; 1061 1062 if (sensor->type == LTC2983_SENSOR_THERMISTOR_STEINHART) { 1063 steinhart = true; 1064 propname = "adi,custom-steinhart"; 1065 } else { 1066 propname = "adi,custom-thermistor"; 1067 } 1068 1069 thermistor->custom = __ltc2983_custom_sensor_new(st, child, 1070 propname, 1071 steinhart, 1072 64, false); 1073 if (IS_ERR(thermistor->custom)) 1074 return ERR_CAST(thermistor->custom); 1075 } 1076 /* set common parameters */ 1077 thermistor->sensor.fault_handler = ltc2983_common_fault_handler; 1078 thermistor->sensor.assign_chan = ltc2983_thermistor_assign_chan; 1079 1080 if (fwnode_property_present(child, "adi,excitation-current-nanoamp")) { 1081 ret = fwnode_property_read_u32(child, "adi,excitation-current-nanoamp", 1082 &excitation_current); 1083 if (ret) 1084 return dev_err_ptr_probe(dev, ret, 1085 "Failed to read adi,excitation-current-nanoamp\n"); 1086 1087 switch (excitation_current) { 1088 case 0: 1089 /* auto range */ 1090 if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) 1091 return dev_err_ptr_probe(dev, -EINVAL, 1092 "Auto Range not allowed for custom sensors\n"); 1093 1094 thermistor->excitation_current = 0x0c; 1095 break; 1096 case 250: 1097 thermistor->excitation_current = 0x01; 1098 break; 1099 case 500: 1100 thermistor->excitation_current = 0x02; 1101 break; 1102 case 1000: 1103 thermistor->excitation_current = 0x03; 1104 break; 1105 case 5000: 1106 thermistor->excitation_current = 0x04; 1107 break; 1108 case 10000: 1109 thermistor->excitation_current = 0x05; 1110 break; 1111 case 25000: 1112 thermistor->excitation_current = 0x06; 1113 break; 1114 case 50000: 1115 thermistor->excitation_current = 0x07; 1116 break; 1117 case 100000: 1118 thermistor->excitation_current = 0x08; 1119 break; 1120 case 250000: 1121 thermistor->excitation_current = 0x09; 1122 break; 1123 case 500000: 1124 thermistor->excitation_current = 0x0a; 1125 break; 1126 case 1000000: 1127 thermistor->excitation_current = 0x0b; 1128 break; 1129 default: 1130 return dev_err_ptr_probe(dev, -EINVAL, 1131 "Invalid value for excitation current(%u)\n", 1132 excitation_current); 1133 } 1134 } else { 1135 /* Auto range is not allowed for custom sensors */ 1136 if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) 1137 /* default to 1uA */ 1138 thermistor->excitation_current = 0x03; 1139 else 1140 /* default to auto-range */ 1141 thermistor->excitation_current = 0x0c; 1142 } 1143 1144 return &thermistor->sensor; 1145 } 1146 1147 static struct ltc2983_sensor * 1148 ltc2983_copper_trace_new(const struct fwnode_handle *child, struct ltc2983_data *st, 1149 const struct ltc2983_sensor *sensor) 1150 { 1151 struct device *dev = &st->spi->dev; 1152 struct ltc2983_copper_trace *ct; 1153 int ret; 1154 1155 if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) 1156 return dev_err_ptr_probe(dev, -EINVAL, 1157 "Invalid channel %d for copper trace\n", 1158 sensor->chan); 1159 1160 ct = devm_kzalloc(dev, sizeof(*ct), GFP_KERNEL); 1161 if (!ct) 1162 return ERR_PTR(-ENOMEM); 1163 1164 struct fwnode_handle *ref __free(fwnode_handle) = 1165 fwnode_find_reference(child, "adi,rsense-handle", 0); 1166 if (IS_ERR(ref)) 1167 return dev_err_cast_probe(dev, ref, 1168 "Property adi,rsense-handle missing or invalid\n"); 1169 1170 ret = fwnode_property_read_u32(ref, "reg", &ct->r_sense_chan); 1171 if (ret) 1172 return dev_err_ptr_probe(dev, ret, "Property reg must be given\n"); 1173 1174 ct->is_sub_ohm = fwnode_property_read_bool(child, "adi,copper-trace-sub-ohm"); 1175 1176 if (ct->is_sub_ohm && fwnode_property_present(child, "adi,custom-copper-trace")) 1177 return dev_err_ptr_probe(dev, -EINVAL, 1178 "sub-ohm copper trace cannot have a custom table\n"); 1179 1180 if (!ct->is_sub_ohm) { 1181 u32 excitation_current = 0; 1182 1183 if (!fwnode_property_present(child, "adi,custom-copper-trace")) 1184 return dev_err_ptr_probe(dev, -EINVAL, 1185 "adi,custom-copper-trace is required for >1 ohm copper trace\n"); 1186 1187 ct->custom = __ltc2983_custom_sensor_new(st, child, "adi,custom-copper-trace", 1188 false, 2048, false); 1189 if (IS_ERR(ct->custom)) 1190 return ERR_CAST(ct->custom); 1191 1192 if (fwnode_property_present(child, "adi,excitation-current-microamp")) { 1193 ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp", 1194 &excitation_current); 1195 if (ret) 1196 return dev_err_ptr_probe(dev, ret, 1197 "Failed to read adi,excitation-current-microamp\n"); 1198 1199 switch (excitation_current) { 1200 case 5: 1201 ct->excitation_current = 0x01; 1202 break; 1203 case 10: 1204 ct->excitation_current = 0x02; 1205 break; 1206 case 25: 1207 ct->excitation_current = 0x03; 1208 break; 1209 case 50: 1210 ct->excitation_current = 0x04; 1211 break; 1212 case 100: 1213 ct->excitation_current = 0x05; 1214 break; 1215 case 250: 1216 ct->excitation_current = 0x06; 1217 break; 1218 case 500: 1219 ct->excitation_current = 0x07; 1220 break; 1221 case 1000: 1222 ct->excitation_current = 0x08; 1223 break; 1224 default: 1225 return dev_err_ptr_probe(dev, -EINVAL, 1226 "Invalid value for excitation current(%u)\n", 1227 excitation_current); 1228 } 1229 } else { 1230 /* default to 1mA per datasheet recommendation for copper trace */ 1231 ct->excitation_current = 0x08; 1232 } 1233 } 1234 1235 ct->sensor.fault_handler = ltc2983_common_fault_handler; 1236 ct->sensor.assign_chan = ltc2983_copper_trace_assign_chan; 1237 if (ct->is_sub_ohm) 1238 ct->sensor.n_iio_chan = 1; 1239 else 1240 ct->sensor.n_iio_chan = 2; 1241 1242 return &ct->sensor; 1243 } 1244 1245 static struct ltc2983_sensor * 1246 ltc2983_leak_detector_new(const struct fwnode_handle *child, struct ltc2983_data *st, 1247 const struct ltc2983_sensor *sensor) 1248 { 1249 struct device *dev = &st->spi->dev; 1250 struct ltc2983_leak_detector *ld; 1251 int ret; 1252 u32 excitation_current = 0; 1253 1254 if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) 1255 return dev_err_ptr_probe(dev, -EINVAL, 1256 "Invalid channel %d for leak detector\n", 1257 sensor->chan); 1258 1259 ld = devm_kzalloc(dev, sizeof(*ld), GFP_KERNEL); 1260 if (!ld) 1261 return ERR_PTR(-ENOMEM); 1262 1263 struct fwnode_handle *ref __free(fwnode_handle) = 1264 fwnode_find_reference(child, "adi,rsense-handle", 0); 1265 if (IS_ERR(ref)) 1266 return dev_err_cast_probe(dev, ref, 1267 "Property adi,rsense-handle missing or invalid\n"); 1268 1269 ret = fwnode_property_read_u32(ref, "reg", &ld->r_sense_chan); 1270 if (ret) 1271 return dev_err_ptr_probe(dev, ret, 1272 "rsense channel must be configured\n"); 1273 1274 if (!fwnode_property_present(child, "adi,custom-leak-detector")) 1275 return dev_err_ptr_probe(dev, -EINVAL, 1276 "adi,custom-leak-detector is required for leak detectors\n"); 1277 1278 ld->custom = __ltc2983_custom_sensor_new(st, child, "adi,custom-leak-detector", 1279 false, 16, false); 1280 if (IS_ERR(ld->custom)) 1281 return ERR_CAST(ld->custom); 1282 1283 ret = fwnode_property_read_u32(child, "adi,excitation-current-nanoamp", 1284 &excitation_current); 1285 if (ret) 1286 return dev_err_ptr_probe(dev, ret, 1287 "adi,excitation-current-nanoamp is required for leak detectors\n"); 1288 1289 switch (excitation_current) { 1290 case 250: 1291 ld->excitation_current = 0x01; 1292 break; 1293 case 500: 1294 ld->excitation_current = 0x02; 1295 break; 1296 case 1000: 1297 ld->excitation_current = 0x03; 1298 break; 1299 case 5000: 1300 ld->excitation_current = 0x04; 1301 break; 1302 case 10000: 1303 ld->excitation_current = 0x05; 1304 break; 1305 case 25000: 1306 ld->excitation_current = 0x06; 1307 break; 1308 case 50000: 1309 ld->excitation_current = 0x07; 1310 break; 1311 case 100000: 1312 ld->excitation_current = 0x08; 1313 break; 1314 case 250000: 1315 ld->excitation_current = 0x09; 1316 break; 1317 case 500000: 1318 ld->excitation_current = 0x0a; 1319 break; 1320 case 1000000: 1321 ld->excitation_current = 0x0b; 1322 break; 1323 default: 1324 return dev_err_ptr_probe(dev, -EINVAL, 1325 "Invalid value for excitation current(%u)\n", 1326 excitation_current); 1327 } 1328 1329 ld->sensor.fault_handler = ltc2983_common_fault_handler; 1330 ld->sensor.assign_chan = ltc2983_leak_detector_assign_chan; 1331 ld->sensor.n_iio_chan = 2; 1332 1333 return &ld->sensor; 1334 } 1335 1336 static struct ltc2983_sensor * 1337 ltc2983_diode_new(const struct fwnode_handle *child, const struct ltc2983_data *st, 1338 const struct ltc2983_sensor *sensor) 1339 { 1340 struct device *dev = &st->spi->dev; 1341 struct ltc2983_diode *diode; 1342 u32 temp = 0, excitation_current = 0; 1343 int ret; 1344 1345 diode = devm_kzalloc(dev, sizeof(*diode), GFP_KERNEL); 1346 if (!diode) 1347 return ERR_PTR(-ENOMEM); 1348 1349 if (fwnode_property_read_bool(child, "adi,single-ended")) 1350 diode->sensor_config = LTC2983_DIODE_SGL(1); 1351 1352 if (fwnode_property_read_bool(child, "adi,three-conversion-cycles")) 1353 diode->sensor_config |= LTC2983_DIODE_3_CONV_CYCLE(1); 1354 1355 if (fwnode_property_read_bool(child, "adi,average-on")) 1356 diode->sensor_config |= LTC2983_DIODE_AVERAGE_ON(1); 1357 1358 /* validate channel index */ 1359 if (!(diode->sensor_config & LTC2983_DIODE_DIFF_MASK) && 1360 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) 1361 return dev_err_ptr_probe(dev, -EINVAL, 1362 "Invalid channel %d for differential diode\n", 1363 sensor->chan); 1364 1365 /* set common parameters */ 1366 diode->sensor.fault_handler = ltc2983_common_fault_handler; 1367 diode->sensor.assign_chan = ltc2983_diode_assign_chan; 1368 1369 if (fwnode_property_present(child, "adi,excitation-current-microamp")) { 1370 ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp", 1371 &excitation_current); 1372 if (ret) 1373 return dev_err_ptr_probe(dev, ret, 1374 "Failed to read adi,excitation-current-microamp\n"); 1375 1376 switch (excitation_current) { 1377 case 10: 1378 diode->excitation_current = 0x00; 1379 break; 1380 case 20: 1381 diode->excitation_current = 0x01; 1382 break; 1383 case 40: 1384 diode->excitation_current = 0x02; 1385 break; 1386 case 80: 1387 diode->excitation_current = 0x03; 1388 break; 1389 default: 1390 return dev_err_ptr_probe(dev, -EINVAL, 1391 "Invalid value for excitation current(%u)\n", 1392 excitation_current); 1393 } 1394 } 1395 1396 if (fwnode_property_present(child, "adi,ideal-factor-value")) { 1397 ret = fwnode_property_read_u32(child, "adi,ideal-factor-value", &temp); 1398 if (ret) 1399 return dev_err_ptr_probe(dev, ret, 1400 "Failed to read adi,ideal-factor-value\n"); 1401 } 1402 1403 /* 2^20 resolution */ 1404 diode->ideal_factor_value = __convert_to_raw(temp, 1048576); 1405 1406 return &diode->sensor; 1407 } 1408 1409 static struct ltc2983_sensor *ltc2983_r_sense_new(struct fwnode_handle *child, 1410 struct ltc2983_data *st, 1411 const struct ltc2983_sensor *sensor) 1412 { 1413 struct device *dev = &st->spi->dev; 1414 struct ltc2983_rsense *rsense; 1415 int ret; 1416 u32 temp; 1417 1418 rsense = devm_kzalloc(dev, sizeof(*rsense), GFP_KERNEL); 1419 if (!rsense) 1420 return ERR_PTR(-ENOMEM); 1421 1422 /* validate channel index */ 1423 if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) 1424 return dev_err_ptr_probe(dev, -EINVAL, 1425 "Invalid channel %d for r_sense\n", 1426 sensor->chan); 1427 1428 ret = fwnode_property_read_u32(child, "adi,rsense-val-milli-ohms", &temp); 1429 if (ret) 1430 return dev_err_ptr_probe(dev, -EINVAL, 1431 "Property adi,rsense-val-milli-ohms missing\n"); 1432 /* 1433 * Times 1000 because we have milli-ohms and __convert_to_raw 1434 * expects scales of 1000000 which are used for all other 1435 * properties. 1436 * 2^10 resolution 1437 */ 1438 rsense->r_sense_val = __convert_to_raw((u64)temp * 1000, 1024); 1439 1440 /* set common parameters */ 1441 rsense->sensor.assign_chan = ltc2983_r_sense_assign_chan; 1442 1443 return &rsense->sensor; 1444 } 1445 1446 static struct ltc2983_sensor *ltc2983_adc_new(struct fwnode_handle *child, 1447 struct ltc2983_data *st, 1448 const struct ltc2983_sensor *sensor) 1449 { 1450 struct device *dev = &st->spi->dev; 1451 struct ltc2983_adc *adc; 1452 1453 adc = devm_kzalloc(dev, sizeof(*adc), GFP_KERNEL); 1454 if (!adc) 1455 return ERR_PTR(-ENOMEM); 1456 1457 if (fwnode_property_read_bool(child, "adi,single-ended")) 1458 adc->single_ended = true; 1459 1460 if (!adc->single_ended && sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) 1461 return dev_err_ptr_probe(dev, -EINVAL, 1462 "Invalid channel %d for differential ADC\n", 1463 sensor->chan); 1464 1465 /* set common parameters */ 1466 adc->sensor.assign_chan = ltc2983_adc_assign_chan; 1467 adc->sensor.fault_handler = ltc2983_common_fault_handler; 1468 1469 return &adc->sensor; 1470 } 1471 1472 static struct ltc2983_sensor *ltc2983_temp_new(struct fwnode_handle *child, 1473 struct ltc2983_data *st, 1474 const struct ltc2983_sensor *sensor) 1475 { 1476 struct device *dev = &st->spi->dev; 1477 struct ltc2983_temp *temp; 1478 1479 temp = devm_kzalloc(dev, sizeof(*temp), GFP_KERNEL); 1480 if (!temp) 1481 return ERR_PTR(-ENOMEM); 1482 1483 if (fwnode_property_read_bool(child, "adi,single-ended")) 1484 temp->single_ended = true; 1485 1486 if (!temp->single_ended && sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) 1487 return dev_err_ptr_probe(dev, -EINVAL, 1488 "Invalid channel %d for differential temp\n", 1489 sensor->chan); 1490 1491 temp->custom = __ltc2983_custom_sensor_new(st, child, "adi,custom-temp", 1492 false, 4096, true); 1493 if (IS_ERR(temp->custom)) 1494 return ERR_CAST(temp->custom); 1495 1496 /* set common parameters */ 1497 temp->sensor.assign_chan = ltc2983_temp_assign_chan; 1498 temp->sensor.fault_handler = ltc2983_common_fault_handler; 1499 1500 return &temp->sensor; 1501 } 1502 1503 static int ltc2983_chan_read(struct ltc2983_data *st, 1504 const struct ltc2983_sensor *sensor, 1505 u32 base_reg, int *val) 1506 { 1507 struct device *dev = &st->spi->dev; 1508 u32 start_conversion = 0; 1509 int ret; 1510 unsigned long time; 1511 1512 start_conversion = LTC2983_STATUS_START(true); 1513 start_conversion |= LTC2983_STATUS_CHAN_SEL(sensor->chan); 1514 dev_dbg(dev, "Start conversion on channel:%d, status:%02X\n", 1515 sensor->chan, start_conversion); 1516 reinit_completion(&st->completion); 1517 /* start conversion */ 1518 ret = regmap_write(st->regmap, LTC2983_STATUS_REG, start_conversion); 1519 if (ret) 1520 return ret; 1521 /* 1522 * wait for conversion to complete. 1523 * 300 ms should be more than enough to complete the conversion. 1524 * Depending on the sensor configuration, there are 2/3 conversions 1525 * cycles of 82ms. 1526 */ 1527 time = wait_for_completion_timeout(&st->completion, 1528 msecs_to_jiffies(300)); 1529 if (!time) { 1530 dev_warn(dev, "Conversion timed out\n"); 1531 return -ETIMEDOUT; 1532 } 1533 1534 /* read the converted data */ 1535 ret = regmap_bulk_read(st->regmap, LTC2983_RESULT_ADDR(sensor->chan, base_reg), 1536 &st->temp, sizeof(st->temp)); 1537 if (ret) 1538 return ret; 1539 1540 *val = __be32_to_cpu(st->temp); 1541 1542 if (base_reg == ADT7604_RES_RES_START_REG) { 1543 /* 1544 * Resistance result register gives a plain unsigned value, 1545 * D31 is always 0, no valid bit, no fault bits. Read bits[30:0] 1546 * directly — the temperature result format does not apply here. 1547 */ 1548 *val &= GENMASK(30, 0); 1549 return 0; 1550 } 1551 1552 if (!(LTC2983_RES_VALID_MASK & *val)) { 1553 dev_err(dev, "Invalid conversion detected\n"); 1554 return -EIO; 1555 } 1556 1557 ret = sensor->fault_handler(st, *val); 1558 if (ret) 1559 return ret; 1560 1561 *val = sign_extend32((*val) & LTC2983_DATA_MASK, LTC2983_DATA_SIGN_BIT); 1562 return 0; 1563 } 1564 1565 static int ltc2983_read_raw(struct iio_dev *indio_dev, 1566 struct iio_chan_spec const *chan, 1567 int *val, int *val2, long mask) 1568 { 1569 struct ltc2983_data *st = iio_priv(indio_dev); 1570 struct device *dev = &st->spi->dev; 1571 int ret; 1572 1573 /* sanity check */ 1574 if (chan->address >= st->num_channels) { 1575 dev_err(dev, "Invalid channel address: %ld\n", chan->address); 1576 return -EINVAL; 1577 } 1578 1579 switch (mask) { 1580 case IIO_CHAN_INFO_RAW: 1581 mutex_lock(&st->lock); 1582 switch (chan->type) { 1583 case IIO_RESISTANCE: 1584 ret = ltc2983_chan_read(st, st->sensors[chan->address], 1585 ADT7604_RES_RES_START_REG, val); 1586 break; 1587 default: 1588 ret = ltc2983_chan_read(st, st->sensors[chan->address], 1589 LTC2983_TEMP_RES_START_REG, val); 1590 break; 1591 } 1592 mutex_unlock(&st->lock); 1593 return ret ?: IIO_VAL_INT; 1594 case IIO_CHAN_INFO_SCALE: 1595 switch (chan->type) { 1596 case IIO_TEMP: 1597 /* value in milli degrees */ 1598 *val = 1000; 1599 /* 2^10 */ 1600 *val2 = 1024; 1601 return IIO_VAL_FRACTIONAL; 1602 case IIO_VOLTAGE: 1603 /* value in millivolt */ 1604 *val = 1000; 1605 /* 2^21 */ 1606 *val2 = 2097152; 1607 return IIO_VAL_FRACTIONAL; 1608 case IIO_RESISTANCE: 1609 case IIO_COVERAGE: 1610 /* value in ohm/percent */ 1611 *val = 1; 1612 /* 2^10 */ 1613 *val2 = 1024; 1614 return IIO_VAL_FRACTIONAL; 1615 default: 1616 return -EINVAL; 1617 } 1618 } 1619 1620 return -EINVAL; 1621 } 1622 1623 static int ltc2983_reg_access(struct iio_dev *indio_dev, 1624 unsigned int reg, 1625 unsigned int writeval, 1626 unsigned int *readval) 1627 { 1628 struct ltc2983_data *st = iio_priv(indio_dev); 1629 1630 if (readval) 1631 return regmap_read(st->regmap, reg, readval); 1632 1633 return regmap_write(st->regmap, reg, writeval); 1634 } 1635 1636 static irqreturn_t ltc2983_irq_handler(int irq, void *data) 1637 { 1638 struct ltc2983_data *st = data; 1639 1640 complete(&st->completion); 1641 return IRQ_HANDLED; 1642 } 1643 1644 #define LTC2983_CHAN(__type, index, __address) ({ \ 1645 struct iio_chan_spec __chan = { \ 1646 .type = __type, \ 1647 .indexed = 1, \ 1648 .channel = index, \ 1649 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 1650 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 1651 .address = __address, \ 1652 }; \ 1653 __chan; \ 1654 }) 1655 1656 static int ltc2983_parse_fw(struct ltc2983_data *st) 1657 { 1658 struct device *dev = &st->spi->dev; 1659 int ret, chan = 0, channel_avail_mask = 0; 1660 1661 device_property_read_u32(dev, "adi,mux-delay-config-us", &st->mux_delay_config); 1662 1663 device_property_read_u32(dev, "adi,filter-notch-freq", &st->filter_notch_freq); 1664 1665 st->num_channels = device_get_child_node_count(dev); 1666 if (!st->num_channels) 1667 return dev_err_probe(dev, -EINVAL, 1668 "At least one channel must be given!\n"); 1669 1670 st->sensors = devm_kcalloc(dev, st->num_channels, sizeof(*st->sensors), 1671 GFP_KERNEL); 1672 if (!st->sensors) 1673 return -ENOMEM; 1674 1675 st->iio_channels = 0; 1676 device_for_each_child_node_scoped(dev, child) { 1677 struct ltc2983_sensor sensor; 1678 1679 ret = fwnode_property_read_u32(child, "reg", &sensor.chan); 1680 if (ret) 1681 return dev_err_probe(dev, ret, 1682 "reg property must given for child nodes\n"); 1683 1684 /* check if we have a valid channel */ 1685 if (sensor.chan < LTC2983_MIN_CHANNELS_NR || 1686 sensor.chan > st->info->max_channels_nr) 1687 return dev_err_probe(dev, -EINVAL, 1688 "channel:%d must be from %u to %u\n", 1689 sensor.chan, 1690 LTC2983_MIN_CHANNELS_NR, 1691 st->info->max_channels_nr); 1692 1693 if (channel_avail_mask & BIT(sensor.chan)) 1694 return dev_err_probe(dev, -EINVAL, 1695 "channel:%d already in use\n", 1696 sensor.chan); 1697 1698 ret = fwnode_property_read_u32(child, "adi,sensor-type", &sensor.type); 1699 if (ret) 1700 return dev_err_probe(dev, ret, 1701 "adi,sensor-type property must given for child nodes\n"); 1702 1703 if (sensor.type >= LTC2983_SENSOR_NUM || 1704 !(st->info->supported_sensors & BIT_ULL(sensor.type))) 1705 return dev_err_probe(dev, -EINVAL, 1706 "sensor type %d not supported on %s\n", 1707 sensor.type, st->info->name); 1708 1709 dev_dbg(dev, "Create new sensor, type %u, channel %u", 1710 sensor.type, sensor.chan); 1711 1712 if (sensor.type >= LTC2983_SENSOR_THERMOCOUPLE && 1713 sensor.type <= LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) { 1714 st->sensors[chan] = ltc2983_thermocouple_new(child, st, 1715 &sensor); 1716 } else if (sensor.type >= LTC2983_SENSOR_RTD && 1717 sensor.type <= LTC2983_SENSOR_RTD_CUSTOM) { 1718 st->sensors[chan] = ltc2983_rtd_new(child, st, &sensor); 1719 } else if (sensor.type >= LTC2983_SENSOR_THERMISTOR && 1720 sensor.type <= LTC2983_SENSOR_THERMISTOR_CUSTOM) { 1721 st->sensors[chan] = ltc2983_thermistor_new(child, st, 1722 &sensor); 1723 } else if (sensor.type == LTC2983_SENSOR_DIODE) { 1724 st->sensors[chan] = ltc2983_diode_new(child, st, 1725 &sensor); 1726 } else if (sensor.type == LTC2983_SENSOR_SENSE_RESISTOR) { 1727 st->sensors[chan] = ltc2983_r_sense_new(child, st, 1728 &sensor); 1729 } else if (sensor.type == LTC2983_SENSOR_DIRECT_ADC) { 1730 st->sensors[chan] = ltc2983_adc_new(child, st, &sensor); 1731 } else if (sensor.type == LTC2983_SENSOR_ACTIVE_TEMP) { 1732 st->sensors[chan] = ltc2983_temp_new(child, st, &sensor); 1733 } else if (sensor.type == LTC2983_SENSOR_COPPER_TRACE) { 1734 st->sensors[chan] = ltc2983_copper_trace_new(child, st, &sensor); 1735 } else if (sensor.type == LTC2983_SENSOR_LEAK_DETECTOR) { 1736 st->sensors[chan] = ltc2983_leak_detector_new(child, st, &sensor); 1737 } else { 1738 return dev_err_probe(dev, -EINVAL, 1739 "Unknown sensor type %d\n", 1740 sensor.type); 1741 } 1742 1743 if (IS_ERR(st->sensors[chan])) 1744 return dev_err_probe(dev, PTR_ERR(st->sensors[chan]), 1745 "Failed to create sensor\n"); 1746 1747 /* set generic sensor parameters */ 1748 st->sensors[chan]->chan = sensor.chan; 1749 st->sensors[chan]->type = sensor.type; 1750 1751 /* 1752 * Dedicated functions set n_iio_chan themselves; for all other 1753 * sensor types rsense produces 0 channels, everything else 1. 1754 */ 1755 if (!st->sensors[chan]->n_iio_chan) { 1756 if (sensor.type != LTC2983_SENSOR_SENSE_RESISTOR) 1757 st->sensors[chan]->n_iio_chan = 1; 1758 } 1759 st->iio_channels += st->sensors[chan]->n_iio_chan; 1760 1761 channel_avail_mask |= BIT(sensor.chan); 1762 chan++; 1763 } 1764 1765 return 0; 1766 } 1767 1768 static int ltc2983_eeprom_cmd(struct ltc2983_data *st, unsigned int cmd, 1769 unsigned int wait_time, unsigned int status_reg, 1770 unsigned long status_fail_mask) 1771 { 1772 struct device *dev = &st->spi->dev; 1773 unsigned long time; 1774 unsigned int val; 1775 int ret; 1776 1777 ret = regmap_bulk_write(st->regmap, LTC2983_EEPROM_KEY_REG, 1778 &st->eeprom_key, sizeof(st->eeprom_key)); 1779 if (ret) 1780 return ret; 1781 1782 reinit_completion(&st->completion); 1783 1784 ret = regmap_write(st->regmap, LTC2983_STATUS_REG, 1785 LTC2983_STATUS_START(true) | cmd); 1786 if (ret) 1787 return ret; 1788 1789 time = wait_for_completion_timeout(&st->completion, 1790 msecs_to_jiffies(wait_time)); 1791 if (!time) 1792 return dev_err_probe(dev, -ETIMEDOUT, 1793 "EEPROM command timed out\n"); 1794 1795 ret = regmap_read(st->regmap, status_reg, &val); 1796 if (ret) 1797 return ret; 1798 1799 if (val & status_fail_mask) 1800 return dev_err_probe(dev, -EINVAL, 1801 "EEPROM command failed: 0x%02X\n", val); 1802 1803 return 0; 1804 } 1805 1806 static int ltc2983_setup(struct ltc2983_data *st, bool assign_iio) 1807 { 1808 struct device *dev = &st->spi->dev; 1809 u32 iio_chan_t = 0, iio_chan_v = 0, iio_chan_r = 0, iio_chan_c = 0; 1810 u32 chan, iio_idx = 0, status; 1811 int ret; 1812 1813 /* make sure the device is up: start bit (7) is 0 and done bit (6) is 1 */ 1814 ret = regmap_read_poll_timeout(st->regmap, LTC2983_STATUS_REG, status, 1815 LTC2983_STATUS_UP(status) == 1, 25000, 1816 25000 * 10); 1817 if (ret) 1818 return dev_err_probe(dev, ret, "Device startup timed out\n"); 1819 1820 ret = regmap_update_bits(st->regmap, LTC2983_GLOBAL_CONFIG_REG, 1821 LTC2983_NOTCH_FREQ_MASK, 1822 LTC2983_NOTCH_FREQ(st->filter_notch_freq)); 1823 if (ret) 1824 return ret; 1825 1826 ret = regmap_write(st->regmap, LTC2983_MUX_CONFIG_REG, 1827 st->mux_delay_config); 1828 if (ret) 1829 return ret; 1830 1831 if (st->info->has_eeprom && !assign_iio) { 1832 ret = ltc2983_eeprom_cmd(st, LTC2983_EEPROM_READ_CMD, 1833 LTC2983_EEPROM_READ_TIME_MS, 1834 LTC2983_EEPROM_READ_STATUS_REG, 1835 LTC2983_EEPROM_READ_FAILURE_MASK); 1836 if (!ret) 1837 return 0; 1838 } 1839 1840 for (chan = 0; chan < st->num_channels; chan++) { 1841 u32 chan_type = 0, *iio_chan; 1842 1843 ret = st->sensors[chan]->assign_chan(st, st->sensors[chan]); 1844 if (ret) 1845 return ret; 1846 /* 1847 * The assign_iio flag is necessary for when the device is 1848 * coming out of sleep. In that case, we just need to 1849 * re-configure the device channels. 1850 * We also don't assign iio channels for rsense. 1851 */ 1852 if (st->sensors[chan]->type == LTC2983_SENSOR_SENSE_RESISTOR || 1853 !assign_iio) 1854 continue; 1855 1856 /* assign iio channel */ 1857 switch (st->sensors[chan]->type) { 1858 case LTC2983_SENSOR_COPPER_TRACE: 1859 if (st->sensors[chan]->n_iio_chan == 1) { 1860 /* sub-ohm copper traces produce only a resistance result */ 1861 st->iio_chan[iio_idx++] = 1862 LTC2983_CHAN(IIO_RESISTANCE, iio_chan_r++, chan); 1863 } else { 1864 st->iio_chan[iio_idx++] = 1865 LTC2983_CHAN(IIO_TEMP, iio_chan_t++, chan); 1866 st->iio_chan[iio_idx++] = 1867 LTC2983_CHAN(IIO_RESISTANCE, iio_chan_r++, chan); 1868 } 1869 continue; 1870 case LTC2983_SENSOR_LEAK_DETECTOR: 1871 st->iio_chan[iio_idx++] = 1872 LTC2983_CHAN(IIO_COVERAGE, iio_chan_c++, chan); 1873 st->iio_chan[iio_idx++] = 1874 LTC2983_CHAN(IIO_RESISTANCE, iio_chan_r++, chan); 1875 continue; 1876 case LTC2983_SENSOR_DIRECT_ADC: 1877 chan_type = IIO_VOLTAGE; 1878 iio_chan = &iio_chan_v; 1879 break; 1880 default: 1881 chan_type = IIO_TEMP; 1882 iio_chan = &iio_chan_t; 1883 break; 1884 } 1885 1886 /* 1887 * add chan as the iio .address so that, we can directly 1888 * reference the sensor given the iio_chan_spec 1889 */ 1890 st->iio_chan[iio_idx++] = LTC2983_CHAN(chan_type, (*iio_chan)++, 1891 chan); 1892 } 1893 1894 return 0; 1895 } 1896 1897 static const struct regmap_range ltc2983_reg_ranges[] = { 1898 regmap_reg_range(LTC2983_STATUS_REG, LTC2983_STATUS_REG), 1899 regmap_reg_range(LTC2983_TEMP_RES_START_REG, LTC2983_TEMP_RES_END_REG), 1900 regmap_reg_range(ADT7604_RES_RES_START_REG, ADT7604_RES_RES_END_REG), 1901 regmap_reg_range(LTC2983_EEPROM_KEY_REG, LTC2983_EEPROM_KEY_REG), 1902 regmap_reg_range(LTC2983_EEPROM_READ_STATUS_REG, 1903 LTC2983_EEPROM_READ_STATUS_REG), 1904 regmap_reg_range(LTC2983_GLOBAL_CONFIG_REG, LTC2983_GLOBAL_CONFIG_REG), 1905 regmap_reg_range(LTC2983_MULT_CHANNEL_START_REG, 1906 LTC2983_MULT_CHANNEL_END_REG), 1907 regmap_reg_range(LTC2986_EEPROM_STATUS_REG, LTC2986_EEPROM_STATUS_REG), 1908 regmap_reg_range(LTC2983_MUX_CONFIG_REG, LTC2983_MUX_CONFIG_REG), 1909 regmap_reg_range(LTC2983_CHAN_ASSIGN_START_REG, 1910 LTC2983_CHAN_ASSIGN_END_REG), 1911 regmap_reg_range(LTC2983_CUST_SENS_TBL_START_REG, 1912 LTC2983_CUST_SENS_TBL_END_REG), 1913 }; 1914 1915 static const struct regmap_access_table ltc2983_reg_table = { 1916 .yes_ranges = ltc2983_reg_ranges, 1917 .n_yes_ranges = ARRAY_SIZE(ltc2983_reg_ranges), 1918 }; 1919 1920 /* 1921 * The reg_bits are actually 12 but the device needs the first *complete* 1922 * byte for the command (R/W). 1923 */ 1924 static const struct regmap_config ltc2983_regmap_config = { 1925 .reg_bits = 24, 1926 .val_bits = 8, 1927 .wr_table = <c2983_reg_table, 1928 .rd_table = <c2983_reg_table, 1929 .read_flag_mask = GENMASK(1, 0), 1930 .write_flag_mask = BIT(1), 1931 }; 1932 1933 static const struct iio_info ltc2983_iio_info = { 1934 .read_raw = ltc2983_read_raw, 1935 .debugfs_reg_access = ltc2983_reg_access, 1936 }; 1937 1938 static int ltc2983_probe(struct spi_device *spi) 1939 { 1940 struct device *dev = &spi->dev; 1941 struct ltc2983_data *st; 1942 struct iio_dev *indio_dev; 1943 struct gpio_desc *gpio; 1944 int ret; 1945 1946 indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); 1947 if (!indio_dev) 1948 return -ENOMEM; 1949 1950 st = iio_priv(indio_dev); 1951 1952 st->info = spi_get_device_match_data(spi); 1953 if (!st->info) 1954 return -ENODEV; 1955 1956 st->regmap = devm_regmap_init_spi(spi, <c2983_regmap_config); 1957 if (IS_ERR(st->regmap)) 1958 return dev_err_probe(dev, PTR_ERR(st->regmap), 1959 "Failed to initialize regmap\n"); 1960 1961 mutex_init(&st->lock); 1962 init_completion(&st->completion); 1963 st->spi = spi; 1964 st->eeprom_key = cpu_to_be32(LTC2983_EEPROM_KEY); 1965 spi_set_drvdata(spi, st); 1966 1967 ret = ltc2983_parse_fw(st); 1968 if (ret) 1969 return ret; 1970 1971 ret = devm_regulator_get_enable(dev, "vdd"); 1972 if (ret) 1973 return ret; 1974 1975 gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); 1976 if (IS_ERR(gpio)) 1977 return PTR_ERR(gpio); 1978 1979 if (gpio) { 1980 /* bring the device out of reset */ 1981 usleep_range(1000, 1200); 1982 gpiod_set_value_cansleep(gpio, 0); 1983 } 1984 1985 st->iio_chan = devm_kzalloc(dev, 1986 st->iio_channels * sizeof(*st->iio_chan), 1987 GFP_KERNEL); 1988 if (!st->iio_chan) 1989 return -ENOMEM; 1990 1991 ret = ltc2983_setup(st, true); 1992 if (ret) 1993 return ret; 1994 1995 ret = devm_request_irq(dev, spi->irq, ltc2983_irq_handler, 1996 IRQF_TRIGGER_RISING, st->info->name, st); 1997 if (ret) 1998 return dev_err_probe(dev, ret, "failed to request an irq\n"); 1999 2000 if (st->info->has_eeprom) { 2001 ret = ltc2983_eeprom_cmd(st, LTC2983_EEPROM_WRITE_CMD, 2002 LTC2983_EEPROM_WRITE_TIME_MS, 2003 LTC2986_EEPROM_STATUS_REG, 2004 LTC2983_EEPROM_STATUS_FAILURE_MASK); 2005 if (ret) 2006 return ret; 2007 } 2008 2009 indio_dev->name = st->info->name; 2010 indio_dev->num_channels = st->iio_channels; 2011 indio_dev->channels = st->iio_chan; 2012 indio_dev->modes = INDIO_DIRECT_MODE; 2013 indio_dev->info = <c2983_iio_info; 2014 2015 return devm_iio_device_register(dev, indio_dev); 2016 } 2017 2018 static int ltc2983_resume(struct device *dev) 2019 { 2020 struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev)); 2021 int dummy; 2022 2023 /* dummy read to bring the device out of sleep */ 2024 regmap_read(st->regmap, LTC2983_STATUS_REG, &dummy); 2025 /* we need to re-assign the channels */ 2026 return ltc2983_setup(st, false); 2027 } 2028 2029 static int ltc2983_suspend(struct device *dev) 2030 { 2031 struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev)); 2032 2033 return regmap_write(st->regmap, LTC2983_STATUS_REG, LTC2983_SLEEP); 2034 } 2035 2036 static DEFINE_SIMPLE_DEV_PM_OPS(ltc2983_pm_ops, ltc2983_suspend, 2037 ltc2983_resume); 2038 2039 static const struct ltc2983_chip_info adt7604_chip_info_data = { 2040 .name = "adt7604", 2041 .max_channels_nr = 20, 2042 .has_eeprom = true, 2043 .supported_sensors = ADT7604_SENSORS, 2044 }; 2045 2046 static const struct ltc2983_chip_info ltc2983_chip_info_data = { 2047 .name = "ltc2983", 2048 .max_channels_nr = 20, 2049 .supported_sensors = LTC2983_COMMON_SENSORS, 2050 }; 2051 2052 static const struct ltc2983_chip_info ltc2984_chip_info_data = { 2053 .name = "ltc2984", 2054 .max_channels_nr = 20, 2055 .has_eeprom = true, 2056 .supported_sensors = LTC2983_COMMON_SENSORS, 2057 }; 2058 2059 static const struct ltc2983_chip_info ltc2986_chip_info_data = { 2060 .name = "ltc2986", 2061 .max_channels_nr = 10, 2062 .has_eeprom = true, 2063 .supported_sensors = LTC2983_COMMON_SENSORS | BIT_ULL(LTC2983_SENSOR_ACTIVE_TEMP), 2064 }; 2065 2066 static const struct ltc2983_chip_info ltm2985_chip_info_data = { 2067 .name = "ltm2985", 2068 .max_channels_nr = 10, 2069 .has_eeprom = true, 2070 .supported_sensors = LTC2983_COMMON_SENSORS | BIT_ULL(LTC2983_SENSOR_ACTIVE_TEMP), 2071 }; 2072 2073 static const struct spi_device_id ltc2983_id_table[] = { 2074 { "adt7604", (kernel_ulong_t)&adt7604_chip_info_data }, 2075 { "ltc2983", (kernel_ulong_t)<c2983_chip_info_data }, 2076 { "ltc2984", (kernel_ulong_t)<c2984_chip_info_data }, 2077 { "ltc2986", (kernel_ulong_t)<c2986_chip_info_data }, 2078 { "ltm2985", (kernel_ulong_t)<m2985_chip_info_data }, 2079 { } 2080 }; 2081 MODULE_DEVICE_TABLE(spi, ltc2983_id_table); 2082 2083 static const struct of_device_id ltc2983_of_match[] = { 2084 { .compatible = "adi,adt7604", .data = &adt7604_chip_info_data }, 2085 { .compatible = "adi,ltc2983", .data = <c2983_chip_info_data }, 2086 { .compatible = "adi,ltc2984", .data = <c2984_chip_info_data }, 2087 { .compatible = "adi,ltc2986", .data = <c2986_chip_info_data }, 2088 { .compatible = "adi,ltm2985", .data = <m2985_chip_info_data }, 2089 { } 2090 }; 2091 MODULE_DEVICE_TABLE(of, ltc2983_of_match); 2092 2093 static struct spi_driver ltc2983_driver = { 2094 .driver = { 2095 .name = "ltc2983", 2096 .of_match_table = ltc2983_of_match, 2097 .pm = pm_sleep_ptr(<c2983_pm_ops), 2098 }, 2099 .probe = ltc2983_probe, 2100 .id_table = ltc2983_id_table, 2101 }; 2102 2103 module_spi_driver(ltc2983_driver); 2104 2105 MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>"); 2106 MODULE_DESCRIPTION("Analog Devices LTC2983 SPI Temperature sensors"); 2107 MODULE_LICENSE("GPL"); 2108