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