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