1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Driver for Texas Instruments TMP512, TMP513 power monitor chips 4 * 5 * TMP513: 6 * Thermal/Power Management with Triple Remote and 7 * Local Temperature Sensor and Current Shunt Monitor 8 * Datasheet: https://www.ti.com/lit/gpn/tmp513 9 * 10 * TMP512: 11 * Thermal/Power Management with Dual Remote 12 * and Local Temperature Sensor and Current Shunt Monitor 13 * Datasheet: https://www.ti.com/lit/gpn/tmp512 14 * 15 * Copyright (C) 2019 Eric Tremblay <etremblay@distech-controls.com> 16 * 17 * This program is free software; you can redistribute it and/or modify 18 * it under the terms of the GNU General Public License as published by 19 * the Free Software Foundation; version 2 of the License. 20 */ 21 22 #include <linux/bitops.h> 23 #include <linux/bug.h> 24 #include <linux/device.h> 25 #include <linux/err.h> 26 #include <linux/hwmon.h> 27 #include <linux/i2c.h> 28 #include <linux/init.h> 29 #include <linux/math.h> 30 #include <linux/module.h> 31 #include <linux/property.h> 32 #include <linux/regmap.h> 33 #include <linux/slab.h> 34 #include <linux/types.h> 35 #include <linux/units.h> 36 37 // Common register definition 38 #define TMP51X_SHUNT_CONFIG 0x00 39 #define TMP51X_TEMP_CONFIG 0x01 40 #define TMP51X_STATUS 0x02 41 #define TMP51X_SMBUS_ALERT 0x03 42 #define TMP51X_SHUNT_CURRENT_RESULT 0x04 43 #define TMP51X_BUS_VOLTAGE_RESULT 0x05 44 #define TMP51X_POWER_RESULT 0x06 45 #define TMP51X_BUS_CURRENT_RESULT 0x07 46 #define TMP51X_LOCAL_TEMP_RESULT 0x08 47 #define TMP51X_REMOTE_TEMP_RESULT_1 0x09 48 #define TMP51X_REMOTE_TEMP_RESULT_2 0x0A 49 #define TMP51X_SHUNT_CURRENT_H_LIMIT 0x0C 50 #define TMP51X_SHUNT_CURRENT_L_LIMIT 0x0D 51 #define TMP51X_BUS_VOLTAGE_H_LIMIT 0x0E 52 #define TMP51X_BUS_VOLTAGE_L_LIMIT 0x0F 53 #define TMP51X_POWER_LIMIT 0x10 54 #define TMP51X_LOCAL_TEMP_LIMIT 0x11 55 #define TMP51X_REMOTE_TEMP_LIMIT_1 0x12 56 #define TMP51X_REMOTE_TEMP_LIMIT_2 0x13 57 #define TMP51X_SHUNT_CALIBRATION 0x15 58 #define TMP51X_N_FACTOR_AND_HYST_1 0x16 59 #define TMP51X_N_FACTOR_2 0x17 60 #define TMP51X_MAN_ID_REG 0xFE 61 #define TMP51X_DEVICE_ID_REG 0xFF 62 63 // TMP513 specific register definition 64 #define TMP513_REMOTE_TEMP_RESULT_3 0x0B 65 #define TMP513_REMOTE_TEMP_LIMIT_3 0x14 66 #define TMP513_N_FACTOR_3 0x18 67 68 // Common attrs, and NULL 69 #define TMP51X_MANUFACTURER_ID 0x55FF 70 71 #define TMP512_DEVICE_ID 0x22FF 72 #define TMP513_DEVICE_ID 0x23FF 73 74 // Default config 75 #define TMP51X_SHUNT_CONFIG_DEFAULT 0x399F 76 #define TMP51X_SHUNT_VALUE_DEFAULT 1000 77 #define TMP51X_VBUS_RANGE_DEFAULT TMP51X_VBUS_RANGE_32V 78 #define TMP51X_PGA_DEFAULT 8 79 #define TMP51X_MAX_REGISTER_ADDR 0xFF 80 81 // Mask and shift 82 #define CURRENT_SENSE_VOLTAGE_320_MASK 0x1800 83 #define CURRENT_SENSE_VOLTAGE_160_MASK 0x1000 84 #define CURRENT_SENSE_VOLTAGE_80_MASK 0x0800 85 #define CURRENT_SENSE_VOLTAGE_40_MASK 0 86 87 #define TMP51X_BUS_VOLTAGE_MASK 0x2000 88 #define TMP51X_NFACTOR_MASK 0xFF00 89 #define TMP51X_HYST_MASK 0x00FF 90 91 #define TMP51X_BUS_VOLTAGE_SHIFT 3 92 #define TMP51X_TEMP_SHIFT 3 93 94 // Alarms 95 #define TMP51X_SHUNT_CURRENT_H_LIMIT_POS 15 96 #define TMP51X_SHUNT_CURRENT_L_LIMIT_POS 14 97 #define TMP51X_BUS_VOLTAGE_H_LIMIT_POS 13 98 #define TMP51X_BUS_VOLTAGE_L_LIMIT_POS 12 99 #define TMP51X_POWER_LIMIT_POS 11 100 #define TMP51X_LOCAL_TEMP_LIMIT_POS 10 101 #define TMP51X_REMOTE_TEMP_LIMIT_1_POS 9 102 #define TMP51X_REMOTE_TEMP_LIMIT_2_POS 8 103 #define TMP513_REMOTE_TEMP_LIMIT_3_POS 7 104 105 #define TMP51X_VBUS_RANGE_32V (32 * MICRO) 106 #define TMP51X_VBUS_RANGE_16V (16 * MICRO) 107 108 // Max and Min value 109 #define MAX_BUS_VOLTAGE_32_LIMIT 32764 110 #define MAX_BUS_VOLTAGE_16_LIMIT 16382 111 112 // Max possible value is -256 to +256 but datasheet indicated -40 to 125. 113 #define MAX_TEMP_LIMIT 125000 114 #define MIN_TEMP_LIMIT -40000 115 116 #define MAX_TEMP_HYST 127500 117 118 #define TMP512_MAX_CHANNELS 3 119 #define TMP513_MAX_CHANNELS 4 120 121 #define TMP51X_TEMP_CONFIG_CONV_RATE GENMASK(9, 7) 122 #define TMP51X_TEMP_CONFIG_RC BIT(10) 123 #define TMP51X_TEMP_CHANNEL_MASK(n) (GENMASK((n) - 1, 0) << 11) 124 #define TMP51X_TEMP_CONFIG_CONT BIT(15) 125 #define TMP51X_TEMP_CONFIG_DEFAULT(n) \ 126 (TMP51X_TEMP_CHANNEL_MASK(n) | TMP51X_TEMP_CONFIG_CONT | \ 127 TMP51X_TEMP_CONFIG_CONV_RATE | TMP51X_TEMP_CONFIG_RC) 128 129 static const u8 TMP51X_TEMP_INPUT[4] = { 130 TMP51X_LOCAL_TEMP_RESULT, 131 TMP51X_REMOTE_TEMP_RESULT_1, 132 TMP51X_REMOTE_TEMP_RESULT_2, 133 TMP513_REMOTE_TEMP_RESULT_3 134 }; 135 136 static const u8 TMP51X_TEMP_CRIT[4] = { 137 TMP51X_LOCAL_TEMP_LIMIT, 138 TMP51X_REMOTE_TEMP_LIMIT_1, 139 TMP51X_REMOTE_TEMP_LIMIT_2, 140 TMP513_REMOTE_TEMP_LIMIT_3 141 }; 142 143 static const u8 TMP51X_TEMP_CRIT_ALARM[4] = { 144 TMP51X_LOCAL_TEMP_LIMIT_POS, 145 TMP51X_REMOTE_TEMP_LIMIT_1_POS, 146 TMP51X_REMOTE_TEMP_LIMIT_2_POS, 147 TMP513_REMOTE_TEMP_LIMIT_3_POS 148 }; 149 150 static const u8 TMP51X_TEMP_CRIT_HYST[4] = { 151 TMP51X_N_FACTOR_AND_HYST_1, 152 TMP51X_N_FACTOR_AND_HYST_1, 153 TMP51X_N_FACTOR_AND_HYST_1, 154 TMP51X_N_FACTOR_AND_HYST_1 155 }; 156 157 static const u8 TMP51X_CURR_INPUT[2] = { 158 TMP51X_SHUNT_CURRENT_RESULT, 159 TMP51X_BUS_CURRENT_RESULT 160 }; 161 162 static const struct regmap_config tmp51x_regmap_config = { 163 .reg_bits = 8, 164 .val_bits = 16, 165 .max_register = TMP51X_MAX_REGISTER_ADDR, 166 }; 167 168 struct tmp51x_data { 169 u16 shunt_config; 170 u16 pga_gain; 171 u32 vbus_range_uvolt; 172 173 u16 temp_config; 174 u32 nfactor[3]; 175 176 u32 shunt_uohms; 177 178 u32 curr_lsb_ua; 179 u32 pwr_lsb_uw; 180 181 u8 max_channels; 182 struct regmap *regmap; 183 }; 184 185 // Set the shift based on the gain: 8 -> 1, 4 -> 2, 2 -> 3, 1 -> 4 186 static inline u8 tmp51x_get_pga_shift(struct tmp51x_data *data) 187 { 188 return 5 - ffs(data->pga_gain); 189 } 190 191 static int tmp51x_get_value(struct tmp51x_data *data, u8 reg, u8 pos, 192 unsigned int regval, long *val) 193 { 194 switch (reg) { 195 case TMP51X_STATUS: 196 *val = (regval >> pos) & 1; 197 break; 198 case TMP51X_SHUNT_CURRENT_RESULT: 199 case TMP51X_SHUNT_CURRENT_H_LIMIT: 200 case TMP51X_SHUNT_CURRENT_L_LIMIT: 201 /* 202 * The valus is read in voltage in the chip but reported as 203 * current to the user. 204 * 2's complement number shifted by one to four depending 205 * on the pga gain setting. 1lsb = 10uV 206 */ 207 *val = sign_extend32(regval, 208 reg == TMP51X_SHUNT_CURRENT_RESULT ? 209 16 - tmp51x_get_pga_shift(data) : 15); 210 *val = DIV_ROUND_CLOSEST(*val * 10 * MILLI, data->shunt_uohms); 211 break; 212 case TMP51X_BUS_VOLTAGE_RESULT: 213 case TMP51X_BUS_VOLTAGE_H_LIMIT: 214 case TMP51X_BUS_VOLTAGE_L_LIMIT: 215 // 1lsb = 4mV 216 *val = (regval >> TMP51X_BUS_VOLTAGE_SHIFT) * 4; 217 break; 218 case TMP51X_POWER_RESULT: 219 case TMP51X_POWER_LIMIT: 220 // Power = (current * BusVoltage) / 5000 221 *val = regval * data->pwr_lsb_uw; 222 break; 223 case TMP51X_BUS_CURRENT_RESULT: 224 // Current = (ShuntVoltage * CalibrationRegister) / 4096 225 *val = sign_extend32(regval, 15) * (long)data->curr_lsb_ua; 226 *val = DIV_ROUND_CLOSEST(*val, MILLI); 227 break; 228 case TMP51X_LOCAL_TEMP_RESULT: 229 case TMP51X_REMOTE_TEMP_RESULT_1: 230 case TMP51X_REMOTE_TEMP_RESULT_2: 231 case TMP513_REMOTE_TEMP_RESULT_3: 232 case TMP51X_LOCAL_TEMP_LIMIT: 233 case TMP51X_REMOTE_TEMP_LIMIT_1: 234 case TMP51X_REMOTE_TEMP_LIMIT_2: 235 case TMP513_REMOTE_TEMP_LIMIT_3: 236 // 1lsb = 0.0625 degrees centigrade 237 *val = sign_extend32(regval, 15) >> TMP51X_TEMP_SHIFT; 238 *val = DIV_ROUND_CLOSEST(*val * 625, 10); 239 break; 240 case TMP51X_N_FACTOR_AND_HYST_1: 241 // 1lsb = 0.5 degrees centigrade 242 *val = (regval & TMP51X_HYST_MASK) * 500; 243 break; 244 default: 245 // Programmer goofed 246 WARN_ON_ONCE(1); 247 *val = 0; 248 return -EOPNOTSUPP; 249 } 250 251 return 0; 252 } 253 254 static int tmp51x_set_value(struct tmp51x_data *data, u8 reg, long val) 255 { 256 int regval, max_val; 257 u32 mask = 0; 258 259 switch (reg) { 260 case TMP51X_SHUNT_CURRENT_H_LIMIT: 261 case TMP51X_SHUNT_CURRENT_L_LIMIT: 262 /* 263 * The user enter current value and we convert it to 264 * voltage. 1lsb = 10uV 265 */ 266 val = DIV_ROUND_CLOSEST(val * data->shunt_uohms, 10 * MILLI); 267 max_val = U16_MAX >> tmp51x_get_pga_shift(data); 268 regval = clamp_val(val, -max_val, max_val); 269 break; 270 case TMP51X_BUS_VOLTAGE_H_LIMIT: 271 case TMP51X_BUS_VOLTAGE_L_LIMIT: 272 // 1lsb = 4mV 273 max_val = (data->vbus_range_uvolt == TMP51X_VBUS_RANGE_32V) ? 274 MAX_BUS_VOLTAGE_32_LIMIT : MAX_BUS_VOLTAGE_16_LIMIT; 275 276 val = clamp_val(DIV_ROUND_CLOSEST(val, 4), 0, max_val); 277 regval = val << TMP51X_BUS_VOLTAGE_SHIFT; 278 break; 279 case TMP51X_POWER_LIMIT: 280 regval = clamp_val(DIV_ROUND_CLOSEST(val, data->pwr_lsb_uw), 0, 281 U16_MAX); 282 break; 283 case TMP51X_LOCAL_TEMP_LIMIT: 284 case TMP51X_REMOTE_TEMP_LIMIT_1: 285 case TMP51X_REMOTE_TEMP_LIMIT_2: 286 case TMP513_REMOTE_TEMP_LIMIT_3: 287 // 1lsb = 0.0625 degrees centigrade 288 val = clamp_val(val, MIN_TEMP_LIMIT, MAX_TEMP_LIMIT); 289 regval = DIV_ROUND_CLOSEST(val * 10, 625) << TMP51X_TEMP_SHIFT; 290 break; 291 case TMP51X_N_FACTOR_AND_HYST_1: 292 // 1lsb = 0.5 degrees centigrade 293 val = clamp_val(val, 0, MAX_TEMP_HYST); 294 regval = DIV_ROUND_CLOSEST(val, 500); 295 mask = TMP51X_HYST_MASK; 296 break; 297 default: 298 // Programmer goofed 299 WARN_ON_ONCE(1); 300 return -EOPNOTSUPP; 301 } 302 303 if (mask == 0) 304 return regmap_write(data->regmap, reg, regval); 305 else 306 return regmap_update_bits(data->regmap, reg, mask, regval); 307 } 308 309 static u8 tmp51x_get_reg(enum hwmon_sensor_types type, u32 attr, int channel) 310 { 311 switch (type) { 312 case hwmon_temp: 313 switch (attr) { 314 case hwmon_temp_input: 315 return TMP51X_TEMP_INPUT[channel]; 316 case hwmon_temp_crit_alarm: 317 return TMP51X_STATUS; 318 case hwmon_temp_crit: 319 return TMP51X_TEMP_CRIT[channel]; 320 case hwmon_temp_crit_hyst: 321 return TMP51X_TEMP_CRIT_HYST[channel]; 322 } 323 break; 324 case hwmon_in: 325 switch (attr) { 326 case hwmon_in_input: 327 return TMP51X_BUS_VOLTAGE_RESULT; 328 case hwmon_in_lcrit_alarm: 329 case hwmon_in_crit_alarm: 330 return TMP51X_STATUS; 331 case hwmon_in_lcrit: 332 return TMP51X_BUS_VOLTAGE_L_LIMIT; 333 case hwmon_in_crit: 334 return TMP51X_BUS_VOLTAGE_H_LIMIT; 335 } 336 break; 337 case hwmon_curr: 338 switch (attr) { 339 case hwmon_curr_input: 340 return TMP51X_CURR_INPUT[channel]; 341 case hwmon_curr_lcrit_alarm: 342 case hwmon_curr_crit_alarm: 343 return TMP51X_STATUS; 344 case hwmon_curr_lcrit: 345 return TMP51X_SHUNT_CURRENT_L_LIMIT; 346 case hwmon_curr_crit: 347 return TMP51X_SHUNT_CURRENT_H_LIMIT; 348 } 349 break; 350 case hwmon_power: 351 switch (attr) { 352 case hwmon_power_input: 353 return TMP51X_POWER_RESULT; 354 case hwmon_power_crit_alarm: 355 return TMP51X_STATUS; 356 case hwmon_power_crit: 357 return TMP51X_POWER_LIMIT; 358 } 359 break; 360 default: 361 break; 362 } 363 364 return 0; 365 } 366 367 static u8 tmp51x_get_status_pos(enum hwmon_sensor_types type, u32 attr, 368 int channel) 369 { 370 switch (type) { 371 case hwmon_temp: 372 switch (attr) { 373 case hwmon_temp_crit_alarm: 374 return TMP51X_TEMP_CRIT_ALARM[channel]; 375 } 376 break; 377 case hwmon_in: 378 switch (attr) { 379 case hwmon_in_lcrit_alarm: 380 return TMP51X_BUS_VOLTAGE_L_LIMIT_POS; 381 case hwmon_in_crit_alarm: 382 return TMP51X_BUS_VOLTAGE_H_LIMIT_POS; 383 } 384 break; 385 case hwmon_curr: 386 switch (attr) { 387 case hwmon_curr_lcrit_alarm: 388 return TMP51X_SHUNT_CURRENT_L_LIMIT_POS; 389 case hwmon_curr_crit_alarm: 390 return TMP51X_SHUNT_CURRENT_H_LIMIT_POS; 391 } 392 break; 393 case hwmon_power: 394 switch (attr) { 395 case hwmon_power_crit_alarm: 396 return TMP51X_POWER_LIMIT_POS; 397 } 398 break; 399 default: 400 break; 401 } 402 403 return 0; 404 } 405 406 static int tmp51x_read(struct device *dev, enum hwmon_sensor_types type, 407 u32 attr, int channel, long *val) 408 { 409 struct tmp51x_data *data = dev_get_drvdata(dev); 410 int ret; 411 u32 regval; 412 u8 pos = 0, reg = 0; 413 414 reg = tmp51x_get_reg(type, attr, channel); 415 if (reg == 0) 416 return -EOPNOTSUPP; 417 418 if (reg == TMP51X_STATUS) 419 pos = tmp51x_get_status_pos(type, attr, channel); 420 421 ret = regmap_read(data->regmap, reg, ®val); 422 if (ret < 0) 423 return ret; 424 425 return tmp51x_get_value(data, reg, pos, regval, val); 426 } 427 428 static int tmp51x_write(struct device *dev, enum hwmon_sensor_types type, 429 u32 attr, int channel, long val) 430 { 431 u8 reg = 0; 432 433 reg = tmp51x_get_reg(type, attr, channel); 434 if (reg == 0) 435 return -EOPNOTSUPP; 436 437 return tmp51x_set_value(dev_get_drvdata(dev), reg, val); 438 } 439 440 static umode_t tmp51x_is_visible(const void *_data, 441 enum hwmon_sensor_types type, u32 attr, 442 int channel) 443 { 444 const struct tmp51x_data *data = _data; 445 446 switch (type) { 447 case hwmon_temp: 448 if (channel >= data->max_channels) 449 return 0; 450 switch (attr) { 451 case hwmon_temp_input: 452 case hwmon_temp_crit_alarm: 453 return 0444; 454 case hwmon_temp_crit: 455 return 0644; 456 case hwmon_temp_crit_hyst: 457 if (channel == 0) 458 return 0644; 459 return 0444; 460 } 461 break; 462 case hwmon_in: 463 switch (attr) { 464 case hwmon_in_input: 465 case hwmon_in_lcrit_alarm: 466 case hwmon_in_crit_alarm: 467 return 0444; 468 case hwmon_in_lcrit: 469 case hwmon_in_crit: 470 return 0644; 471 } 472 break; 473 case hwmon_curr: 474 if (!data->shunt_uohms) 475 return 0; 476 477 switch (attr) { 478 case hwmon_curr_input: 479 case hwmon_curr_lcrit_alarm: 480 case hwmon_curr_crit_alarm: 481 return 0444; 482 case hwmon_curr_lcrit: 483 case hwmon_curr_crit: 484 return 0644; 485 } 486 break; 487 case hwmon_power: 488 if (!data->shunt_uohms) 489 return 0; 490 491 switch (attr) { 492 case hwmon_power_input: 493 case hwmon_power_crit_alarm: 494 return 0444; 495 case hwmon_power_crit: 496 return 0644; 497 } 498 break; 499 default: 500 break; 501 } 502 return 0; 503 } 504 505 static const struct hwmon_channel_info * const tmp51x_info[] = { 506 HWMON_CHANNEL_INFO(temp, 507 HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_ALARM | 508 HWMON_T_CRIT_HYST, 509 HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_ALARM | 510 HWMON_T_CRIT_HYST, 511 HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_ALARM | 512 HWMON_T_CRIT_HYST, 513 HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_CRIT_ALARM | 514 HWMON_T_CRIT_HYST), 515 HWMON_CHANNEL_INFO(in, 516 HWMON_I_INPUT | HWMON_I_LCRIT | HWMON_I_LCRIT_ALARM | 517 HWMON_I_CRIT | HWMON_I_CRIT_ALARM), 518 HWMON_CHANNEL_INFO(curr, 519 HWMON_C_INPUT | HWMON_C_LCRIT | HWMON_C_LCRIT_ALARM | 520 HWMON_C_CRIT | HWMON_C_CRIT_ALARM, 521 HWMON_C_INPUT), 522 HWMON_CHANNEL_INFO(power, 523 HWMON_P_INPUT | HWMON_P_CRIT | HWMON_P_CRIT_ALARM), 524 NULL 525 }; 526 527 static const struct hwmon_ops tmp51x_hwmon_ops = { 528 .is_visible = tmp51x_is_visible, 529 .read = tmp51x_read, 530 .write = tmp51x_write, 531 }; 532 533 static const struct hwmon_chip_info tmp51x_chip_info = { 534 .ops = &tmp51x_hwmon_ops, 535 .info = tmp51x_info, 536 }; 537 538 /* 539 * Calibrate the tmp51x following the datasheet method 540 */ 541 static int tmp51x_calibrate(struct tmp51x_data *data) 542 { 543 int vshunt_max = data->pga_gain * 40; 544 u64 max_curr_ma; 545 u32 div; 546 547 /* 548 * If shunt_uohms is equal to 0, the calibration should be set to 0. 549 * The consequence will be that the current and power measurement engine 550 * of the sensor will not work. Temperature and voltage sensing will 551 * continue to work. 552 */ 553 if (data->shunt_uohms == 0) 554 return regmap_write(data->regmap, TMP51X_SHUNT_CALIBRATION, 0); 555 556 max_curr_ma = DIV_ROUND_CLOSEST_ULL(vshunt_max * MICRO, data->shunt_uohms); 557 558 /* 559 * Calculate the minimal bit resolution for the current and the power. 560 * Those values will be used during register interpretation. 561 */ 562 data->curr_lsb_ua = DIV_ROUND_CLOSEST_ULL(max_curr_ma * MILLI, 32767); 563 data->pwr_lsb_uw = 20 * data->curr_lsb_ua; 564 565 div = DIV_ROUND_CLOSEST_ULL(data->curr_lsb_ua * data->shunt_uohms, MICRO); 566 567 return regmap_write(data->regmap, TMP51X_SHUNT_CALIBRATION, 568 DIV_ROUND_CLOSEST(40960, div)); 569 } 570 571 /* 572 * Initialize the configuration and calibration registers. 573 */ 574 static int tmp51x_init(struct tmp51x_data *data) 575 { 576 unsigned int regval; 577 int ret = regmap_write(data->regmap, TMP51X_SHUNT_CONFIG, 578 data->shunt_config); 579 if (ret < 0) 580 return ret; 581 582 ret = regmap_write(data->regmap, TMP51X_TEMP_CONFIG, data->temp_config); 583 if (ret < 0) 584 return ret; 585 586 // nFactor configuration 587 ret = regmap_update_bits(data->regmap, TMP51X_N_FACTOR_AND_HYST_1, 588 TMP51X_NFACTOR_MASK, data->nfactor[0] << 8); 589 if (ret < 0) 590 return ret; 591 592 ret = regmap_write(data->regmap, TMP51X_N_FACTOR_2, 593 data->nfactor[1] << 8); 594 if (ret < 0) 595 return ret; 596 597 if (data->max_channels == TMP513_MAX_CHANNELS) { 598 ret = regmap_write(data->regmap, TMP513_N_FACTOR_3, 599 data->nfactor[2] << 8); 600 if (ret < 0) 601 return ret; 602 } 603 604 ret = tmp51x_calibrate(data); 605 if (ret < 0) 606 return ret; 607 608 // Read the status register before using as the datasheet propose 609 return regmap_read(data->regmap, TMP51X_STATUS, ®val); 610 } 611 612 static const struct i2c_device_id tmp51x_id[] = { 613 { "tmp512", TMP512_MAX_CHANNELS }, 614 { "tmp513", TMP513_MAX_CHANNELS }, 615 { } 616 }; 617 MODULE_DEVICE_TABLE(i2c, tmp51x_id); 618 619 static const struct of_device_id tmp51x_of_match[] = { 620 { .compatible = "ti,tmp512", .data = (void *)TMP512_MAX_CHANNELS }, 621 { .compatible = "ti,tmp513", .data = (void *)TMP513_MAX_CHANNELS }, 622 { } 623 }; 624 MODULE_DEVICE_TABLE(of, tmp51x_of_match); 625 626 static int tmp51x_vbus_range_to_reg(struct device *dev, 627 struct tmp51x_data *data) 628 { 629 if (data->vbus_range_uvolt == TMP51X_VBUS_RANGE_32V) { 630 data->shunt_config |= TMP51X_BUS_VOLTAGE_MASK; 631 } else if (data->vbus_range_uvolt == TMP51X_VBUS_RANGE_16V) { 632 data->shunt_config &= ~TMP51X_BUS_VOLTAGE_MASK; 633 } else { 634 return dev_err_probe(dev, -EINVAL, 635 "ti,bus-range-microvolt is invalid: %u\n", 636 data->vbus_range_uvolt); 637 } 638 return 0; 639 } 640 641 static int tmp51x_pga_gain_to_reg(struct device *dev, struct tmp51x_data *data) 642 { 643 if (data->pga_gain == 8) { 644 data->shunt_config |= CURRENT_SENSE_VOLTAGE_320_MASK; 645 } else if (data->pga_gain == 4) { 646 data->shunt_config |= CURRENT_SENSE_VOLTAGE_160_MASK; 647 } else if (data->pga_gain == 2) { 648 data->shunt_config |= CURRENT_SENSE_VOLTAGE_80_MASK; 649 } else if (data->pga_gain == 1) { 650 data->shunt_config |= CURRENT_SENSE_VOLTAGE_40_MASK; 651 } else { 652 return dev_err_probe(dev, -EINVAL, 653 "ti,pga-gain is invalid: %u\n", data->pga_gain); 654 } 655 return 0; 656 } 657 658 static int tmp51x_read_properties(struct device *dev, struct tmp51x_data *data) 659 { 660 int ret; 661 u32 val; 662 663 ret = device_property_read_u32(dev, "shunt-resistor-micro-ohms", &val); 664 data->shunt_uohms = (ret >= 0) ? val : TMP51X_SHUNT_VALUE_DEFAULT; 665 666 ret = device_property_read_u32(dev, "ti,bus-range-microvolt", &val); 667 data->vbus_range_uvolt = (ret >= 0) ? val : TMP51X_VBUS_RANGE_DEFAULT; 668 ret = tmp51x_vbus_range_to_reg(dev, data); 669 if (ret < 0) 670 return ret; 671 672 ret = device_property_read_u32(dev, "ti,pga-gain", &val); 673 data->pga_gain = (ret >= 0) ? val : TMP51X_PGA_DEFAULT; 674 ret = tmp51x_pga_gain_to_reg(dev, data); 675 if (ret < 0) 676 return ret; 677 678 device_property_read_u32_array(dev, "ti,nfactor", data->nfactor, 679 data->max_channels - 1); 680 681 // Check if shunt value is compatible with pga-gain 682 if (data->shunt_uohms > data->pga_gain * 40 * MICRO) { 683 return dev_err_probe(dev, -EINVAL, 684 "shunt-resistor: %u too big for pga_gain: %u\n", 685 data->shunt_uohms, data->pga_gain); 686 } 687 688 return 0; 689 } 690 691 static void tmp51x_use_default(struct tmp51x_data *data) 692 { 693 data->vbus_range_uvolt = TMP51X_VBUS_RANGE_DEFAULT; 694 data->pga_gain = TMP51X_PGA_DEFAULT; 695 data->shunt_uohms = TMP51X_SHUNT_VALUE_DEFAULT; 696 } 697 698 static int tmp51x_configure(struct device *dev, struct tmp51x_data *data) 699 { 700 data->shunt_config = TMP51X_SHUNT_CONFIG_DEFAULT; 701 data->temp_config = TMP51X_TEMP_CONFIG_DEFAULT(data->max_channels); 702 703 if (dev->of_node) 704 return tmp51x_read_properties(dev, data); 705 706 tmp51x_use_default(data); 707 708 return 0; 709 } 710 711 static int tmp51x_probe(struct i2c_client *client) 712 { 713 struct device *dev = &client->dev; 714 struct tmp51x_data *data; 715 struct device *hwmon_dev; 716 int ret; 717 718 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 719 if (!data) 720 return -ENOMEM; 721 722 data->max_channels = (uintptr_t)i2c_get_match_data(client); 723 724 ret = tmp51x_configure(dev, data); 725 if (ret < 0) 726 return dev_err_probe(dev, ret, "error configuring the device\n"); 727 728 data->regmap = devm_regmap_init_i2c(client, &tmp51x_regmap_config); 729 if (IS_ERR(data->regmap)) 730 return dev_err_probe(dev, PTR_ERR(data->regmap), 731 "failed to allocate register map\n"); 732 733 ret = tmp51x_init(data); 734 if (ret < 0) 735 return dev_err_probe(dev, ret, "error configuring the device\n"); 736 737 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, 738 data, 739 &tmp51x_chip_info, 740 NULL); 741 if (IS_ERR(hwmon_dev)) 742 return PTR_ERR(hwmon_dev); 743 744 dev_dbg(dev, "power monitor %s\n", client->name); 745 746 return 0; 747 } 748 749 static struct i2c_driver tmp51x_driver = { 750 .driver = { 751 .name = "tmp51x", 752 .of_match_table = tmp51x_of_match, 753 }, 754 .probe = tmp51x_probe, 755 .id_table = tmp51x_id, 756 }; 757 758 module_i2c_driver(tmp51x_driver); 759 760 MODULE_AUTHOR("Eric Tremblay <etremblay@distechcontrols.com>"); 761 MODULE_DESCRIPTION("tmp51x driver"); 762 MODULE_LICENSE("GPL"); 763