| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Hardware monitoring driver for Analog Devices ADM1275 Hot-Swap Controller |
|---|
| 3 | 4 | * and Digital Power Monitor |
|---|
| 4 | 5 | * |
|---|
| 5 | 6 | * Copyright (c) 2011 Ericsson AB. |
|---|
| 6 | 7 | * Copyright (c) 2018 Guenter Roeck |
|---|
| 7 | | - * |
|---|
| 8 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 9 | | - * it under the terms of the GNU General Public License as published by |
|---|
| 10 | | - * the Free Software Foundation; either version 2 of the License, or |
|---|
| 11 | | - * (at your option) any later version. |
|---|
| 12 | | - * |
|---|
| 13 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 14 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | | - * GNU General Public License for more details. |
|---|
| 17 | 8 | */ |
|---|
| 18 | 9 | |
|---|
| 19 | 10 | #include <linux/kernel.h> |
|---|
| .. | .. |
|---|
| 23 | 14 | #include <linux/slab.h> |
|---|
| 24 | 15 | #include <linux/i2c.h> |
|---|
| 25 | 16 | #include <linux/bitops.h> |
|---|
| 17 | +#include <linux/bitfield.h> |
|---|
| 18 | +#include <linux/log2.h> |
|---|
| 26 | 19 | #include "pmbus.h" |
|---|
| 27 | 20 | |
|---|
| 28 | 21 | enum chips { adm1075, adm1272, adm1275, adm1276, adm1278, adm1293, adm1294 }; |
|---|
| .. | .. |
|---|
| 44 | 37 | |
|---|
| 45 | 38 | #define ADM1272_IRANGE BIT(0) |
|---|
| 46 | 39 | |
|---|
| 40 | +#define ADM1278_TSFILT BIT(15) |
|---|
| 47 | 41 | #define ADM1278_TEMP1_EN BIT(3) |
|---|
| 48 | 42 | #define ADM1278_VIN_EN BIT(2) |
|---|
| 49 | 43 | #define ADM1278_VOUT_EN BIT(1) |
|---|
| 44 | + |
|---|
| 45 | +#define ADM1278_PMON_DEFCONFIG (ADM1278_VOUT_EN | ADM1278_TEMP1_EN | ADM1278_TSFILT) |
|---|
| 50 | 46 | |
|---|
| 51 | 47 | #define ADM1293_IRANGE_25 0 |
|---|
| 52 | 48 | #define ADM1293_IRANGE_50 BIT(6) |
|---|
| .. | .. |
|---|
| 78 | 74 | #define ADM1075_VAUX_OV_WARN BIT(7) |
|---|
| 79 | 75 | #define ADM1075_VAUX_UV_WARN BIT(6) |
|---|
| 80 | 76 | |
|---|
| 77 | +#define ADM1275_VI_AVG_SHIFT 0 |
|---|
| 78 | +#define ADM1275_VI_AVG_MASK GENMASK(ADM1275_VI_AVG_SHIFT + 2, \ |
|---|
| 79 | + ADM1275_VI_AVG_SHIFT) |
|---|
| 80 | +#define ADM1275_SAMPLES_AVG_MAX 128 |
|---|
| 81 | + |
|---|
| 82 | +#define ADM1278_PWR_AVG_SHIFT 11 |
|---|
| 83 | +#define ADM1278_PWR_AVG_MASK GENMASK(ADM1278_PWR_AVG_SHIFT + 2, \ |
|---|
| 84 | + ADM1278_PWR_AVG_SHIFT) |
|---|
| 85 | +#define ADM1278_VI_AVG_SHIFT 8 |
|---|
| 86 | +#define ADM1278_VI_AVG_MASK GENMASK(ADM1278_VI_AVG_SHIFT + 2, \ |
|---|
| 87 | + ADM1278_VI_AVG_SHIFT) |
|---|
| 88 | + |
|---|
| 81 | 89 | struct adm1275_data { |
|---|
| 82 | 90 | int id; |
|---|
| 83 | 91 | bool have_oc_fault; |
|---|
| .. | .. |
|---|
| 89 | 97 | bool have_pin_min; |
|---|
| 90 | 98 | bool have_pin_max; |
|---|
| 91 | 99 | bool have_temp_max; |
|---|
| 100 | + bool have_power_sampling; |
|---|
| 92 | 101 | struct pmbus_driver_info info; |
|---|
| 93 | 102 | }; |
|---|
| 94 | 103 | |
|---|
| .. | .. |
|---|
| 164 | 173 | [18] = { 7658, 0, -3 }, /* power, 21V, irange200 */ |
|---|
| 165 | 174 | }; |
|---|
| 166 | 175 | |
|---|
| 167 | | -static int adm1275_read_word_data(struct i2c_client *client, int page, int reg) |
|---|
| 176 | +static int adm1275_read_pmon_config(const struct adm1275_data *data, |
|---|
| 177 | + struct i2c_client *client, bool is_power) |
|---|
| 178 | +{ |
|---|
| 179 | + int shift, ret; |
|---|
| 180 | + u16 mask; |
|---|
| 181 | + |
|---|
| 182 | + /* |
|---|
| 183 | + * The PMON configuration register is a 16-bit register only on chips |
|---|
| 184 | + * supporting power average sampling. On other chips it is an 8-bit |
|---|
| 185 | + * register. |
|---|
| 186 | + */ |
|---|
| 187 | + if (data->have_power_sampling) { |
|---|
| 188 | + ret = i2c_smbus_read_word_data(client, ADM1275_PMON_CONFIG); |
|---|
| 189 | + mask = is_power ? ADM1278_PWR_AVG_MASK : ADM1278_VI_AVG_MASK; |
|---|
| 190 | + shift = is_power ? ADM1278_PWR_AVG_SHIFT : ADM1278_VI_AVG_SHIFT; |
|---|
| 191 | + } else { |
|---|
| 192 | + ret = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG); |
|---|
| 193 | + mask = ADM1275_VI_AVG_MASK; |
|---|
| 194 | + shift = ADM1275_VI_AVG_SHIFT; |
|---|
| 195 | + } |
|---|
| 196 | + if (ret < 0) |
|---|
| 197 | + return ret; |
|---|
| 198 | + |
|---|
| 199 | + return (ret & mask) >> shift; |
|---|
| 200 | +} |
|---|
| 201 | + |
|---|
| 202 | +static int adm1275_write_pmon_config(const struct adm1275_data *data, |
|---|
| 203 | + struct i2c_client *client, |
|---|
| 204 | + bool is_power, u16 word) |
|---|
| 205 | +{ |
|---|
| 206 | + int shift, ret; |
|---|
| 207 | + u16 mask; |
|---|
| 208 | + |
|---|
| 209 | + if (data->have_power_sampling) { |
|---|
| 210 | + ret = i2c_smbus_read_word_data(client, ADM1275_PMON_CONFIG); |
|---|
| 211 | + mask = is_power ? ADM1278_PWR_AVG_MASK : ADM1278_VI_AVG_MASK; |
|---|
| 212 | + shift = is_power ? ADM1278_PWR_AVG_SHIFT : ADM1278_VI_AVG_SHIFT; |
|---|
| 213 | + } else { |
|---|
| 214 | + ret = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG); |
|---|
| 215 | + mask = ADM1275_VI_AVG_MASK; |
|---|
| 216 | + shift = ADM1275_VI_AVG_SHIFT; |
|---|
| 217 | + } |
|---|
| 218 | + if (ret < 0) |
|---|
| 219 | + return ret; |
|---|
| 220 | + |
|---|
| 221 | + word = (ret & ~mask) | ((word << shift) & mask); |
|---|
| 222 | + if (data->have_power_sampling) |
|---|
| 223 | + ret = i2c_smbus_write_word_data(client, ADM1275_PMON_CONFIG, |
|---|
| 224 | + word); |
|---|
| 225 | + else |
|---|
| 226 | + ret = i2c_smbus_write_byte_data(client, ADM1275_PMON_CONFIG, |
|---|
| 227 | + word); |
|---|
| 228 | + |
|---|
| 229 | + return ret; |
|---|
| 230 | +} |
|---|
| 231 | + |
|---|
| 232 | +static int adm1275_read_word_data(struct i2c_client *client, int page, |
|---|
| 233 | + int phase, int reg) |
|---|
| 168 | 234 | { |
|---|
| 169 | 235 | const struct pmbus_driver_info *info = pmbus_get_driver_info(client); |
|---|
| 170 | 236 | const struct adm1275_data *data = to_adm1275_data(info); |
|---|
| .. | .. |
|---|
| 177 | 243 | case PMBUS_IOUT_UC_FAULT_LIMIT: |
|---|
| 178 | 244 | if (!data->have_uc_fault) |
|---|
| 179 | 245 | return -ENXIO; |
|---|
| 180 | | - ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT); |
|---|
| 246 | + ret = pmbus_read_word_data(client, 0, 0xff, |
|---|
| 247 | + ADM1275_IOUT_WARN2_LIMIT); |
|---|
| 181 | 248 | break; |
|---|
| 182 | 249 | case PMBUS_IOUT_OC_FAULT_LIMIT: |
|---|
| 183 | 250 | if (!data->have_oc_fault) |
|---|
| 184 | 251 | return -ENXIO; |
|---|
| 185 | | - ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT); |
|---|
| 252 | + ret = pmbus_read_word_data(client, 0, 0xff, |
|---|
| 253 | + ADM1275_IOUT_WARN2_LIMIT); |
|---|
| 186 | 254 | break; |
|---|
| 187 | 255 | case PMBUS_VOUT_OV_WARN_LIMIT: |
|---|
| 188 | 256 | if (data->have_vout) |
|---|
| 189 | 257 | return -ENODATA; |
|---|
| 190 | | - ret = pmbus_read_word_data(client, 0, |
|---|
| 258 | + ret = pmbus_read_word_data(client, 0, 0xff, |
|---|
| 191 | 259 | ADM1075_VAUX_OV_WARN_LIMIT); |
|---|
| 192 | 260 | break; |
|---|
| 193 | 261 | case PMBUS_VOUT_UV_WARN_LIMIT: |
|---|
| 194 | 262 | if (data->have_vout) |
|---|
| 195 | 263 | return -ENODATA; |
|---|
| 196 | | - ret = pmbus_read_word_data(client, 0, |
|---|
| 264 | + ret = pmbus_read_word_data(client, 0, 0xff, |
|---|
| 197 | 265 | ADM1075_VAUX_UV_WARN_LIMIT); |
|---|
| 198 | 266 | break; |
|---|
| 199 | 267 | case PMBUS_READ_VOUT: |
|---|
| 200 | 268 | if (data->have_vout) |
|---|
| 201 | 269 | return -ENODATA; |
|---|
| 202 | | - ret = pmbus_read_word_data(client, 0, ADM1075_READ_VAUX); |
|---|
| 270 | + ret = pmbus_read_word_data(client, 0, 0xff, |
|---|
| 271 | + ADM1075_READ_VAUX); |
|---|
| 203 | 272 | break; |
|---|
| 204 | 273 | case PMBUS_VIRT_READ_IOUT_MIN: |
|---|
| 205 | 274 | if (!data->have_iout_min) |
|---|
| 206 | 275 | return -ENXIO; |
|---|
| 207 | | - ret = pmbus_read_word_data(client, 0, ADM1293_IOUT_MIN); |
|---|
| 276 | + ret = pmbus_read_word_data(client, 0, 0xff, |
|---|
| 277 | + ADM1293_IOUT_MIN); |
|---|
| 208 | 278 | break; |
|---|
| 209 | 279 | case PMBUS_VIRT_READ_IOUT_MAX: |
|---|
| 210 | | - ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_IOUT); |
|---|
| 280 | + ret = pmbus_read_word_data(client, 0, 0xff, |
|---|
| 281 | + ADM1275_PEAK_IOUT); |
|---|
| 211 | 282 | break; |
|---|
| 212 | 283 | case PMBUS_VIRT_READ_VOUT_MAX: |
|---|
| 213 | | - ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VOUT); |
|---|
| 284 | + ret = pmbus_read_word_data(client, 0, 0xff, |
|---|
| 285 | + ADM1275_PEAK_VOUT); |
|---|
| 214 | 286 | break; |
|---|
| 215 | 287 | case PMBUS_VIRT_READ_VIN_MAX: |
|---|
| 216 | | - ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VIN); |
|---|
| 288 | + ret = pmbus_read_word_data(client, 0, 0xff, |
|---|
| 289 | + ADM1275_PEAK_VIN); |
|---|
| 217 | 290 | break; |
|---|
| 218 | 291 | case PMBUS_VIRT_READ_PIN_MIN: |
|---|
| 219 | 292 | if (!data->have_pin_min) |
|---|
| 220 | 293 | return -ENXIO; |
|---|
| 221 | | - ret = pmbus_read_word_data(client, 0, ADM1293_PIN_MIN); |
|---|
| 294 | + ret = pmbus_read_word_data(client, 0, 0xff, |
|---|
| 295 | + ADM1293_PIN_MIN); |
|---|
| 222 | 296 | break; |
|---|
| 223 | 297 | case PMBUS_VIRT_READ_PIN_MAX: |
|---|
| 224 | 298 | if (!data->have_pin_max) |
|---|
| 225 | 299 | return -ENXIO; |
|---|
| 226 | | - ret = pmbus_read_word_data(client, 0, ADM1276_PEAK_PIN); |
|---|
| 300 | + ret = pmbus_read_word_data(client, 0, 0xff, |
|---|
| 301 | + ADM1276_PEAK_PIN); |
|---|
| 227 | 302 | break; |
|---|
| 228 | 303 | case PMBUS_VIRT_READ_TEMP_MAX: |
|---|
| 229 | 304 | if (!data->have_temp_max) |
|---|
| 230 | 305 | return -ENXIO; |
|---|
| 231 | | - ret = pmbus_read_word_data(client, 0, ADM1278_PEAK_TEMP); |
|---|
| 306 | + ret = pmbus_read_word_data(client, 0, 0xff, |
|---|
| 307 | + ADM1278_PEAK_TEMP); |
|---|
| 232 | 308 | break; |
|---|
| 233 | 309 | case PMBUS_VIRT_RESET_IOUT_HISTORY: |
|---|
| 234 | 310 | case PMBUS_VIRT_RESET_VOUT_HISTORY: |
|---|
| .. | .. |
|---|
| 241 | 317 | case PMBUS_VIRT_RESET_TEMP_HISTORY: |
|---|
| 242 | 318 | if (!data->have_temp_max) |
|---|
| 243 | 319 | return -ENXIO; |
|---|
| 320 | + break; |
|---|
| 321 | + case PMBUS_VIRT_POWER_SAMPLES: |
|---|
| 322 | + if (!data->have_power_sampling) |
|---|
| 323 | + return -ENXIO; |
|---|
| 324 | + ret = adm1275_read_pmon_config(data, client, true); |
|---|
| 325 | + if (ret < 0) |
|---|
| 326 | + break; |
|---|
| 327 | + ret = BIT(ret); |
|---|
| 328 | + break; |
|---|
| 329 | + case PMBUS_VIRT_IN_SAMPLES: |
|---|
| 330 | + case PMBUS_VIRT_CURR_SAMPLES: |
|---|
| 331 | + ret = adm1275_read_pmon_config(data, client, false); |
|---|
| 332 | + if (ret < 0) |
|---|
| 333 | + break; |
|---|
| 334 | + ret = BIT(ret); |
|---|
| 244 | 335 | break; |
|---|
| 245 | 336 | default: |
|---|
| 246 | 337 | ret = -ENODATA; |
|---|
| .. | .. |
|---|
| 285 | 376 | break; |
|---|
| 286 | 377 | case PMBUS_VIRT_RESET_TEMP_HISTORY: |
|---|
| 287 | 378 | ret = pmbus_write_word_data(client, 0, ADM1278_PEAK_TEMP, 0); |
|---|
| 379 | + break; |
|---|
| 380 | + case PMBUS_VIRT_POWER_SAMPLES: |
|---|
| 381 | + if (!data->have_power_sampling) |
|---|
| 382 | + return -ENXIO; |
|---|
| 383 | + word = clamp_val(word, 1, ADM1275_SAMPLES_AVG_MAX); |
|---|
| 384 | + ret = adm1275_write_pmon_config(data, client, true, |
|---|
| 385 | + ilog2(word)); |
|---|
| 386 | + break; |
|---|
| 387 | + case PMBUS_VIRT_IN_SAMPLES: |
|---|
| 388 | + case PMBUS_VIRT_CURR_SAMPLES: |
|---|
| 389 | + word = clamp_val(word, 1, ADM1275_SAMPLES_AVG_MAX); |
|---|
| 390 | + ret = adm1275_write_pmon_config(data, client, false, |
|---|
| 391 | + ilog2(word)); |
|---|
| 288 | 392 | break; |
|---|
| 289 | 393 | default: |
|---|
| 290 | 394 | ret = -ENODATA; |
|---|
| .. | .. |
|---|
| 361 | 465 | }; |
|---|
| 362 | 466 | MODULE_DEVICE_TABLE(i2c, adm1275_id); |
|---|
| 363 | 467 | |
|---|
| 364 | | -static int adm1275_probe(struct i2c_client *client, |
|---|
| 365 | | - const struct i2c_device_id *id) |
|---|
| 468 | +/* Enable VOUT & TEMP1 if not enabled (disabled by default) */ |
|---|
| 469 | +static int adm1275_enable_vout_temp(struct i2c_client *client, int config) |
|---|
| 470 | +{ |
|---|
| 471 | + int ret; |
|---|
| 472 | + |
|---|
| 473 | + if ((config & ADM1278_PMON_DEFCONFIG) != ADM1278_PMON_DEFCONFIG) { |
|---|
| 474 | + config |= ADM1278_PMON_DEFCONFIG; |
|---|
| 475 | + ret = i2c_smbus_write_word_data(client, ADM1275_PMON_CONFIG, config); |
|---|
| 476 | + if (ret < 0) { |
|---|
| 477 | + dev_err(&client->dev, "Failed to enable VOUT/TEMP1 monitoring\n"); |
|---|
| 478 | + return ret; |
|---|
| 479 | + } |
|---|
| 480 | + } |
|---|
| 481 | + return 0; |
|---|
| 482 | +} |
|---|
| 483 | + |
|---|
| 484 | +static int adm1275_probe(struct i2c_client *client) |
|---|
| 366 | 485 | { |
|---|
| 367 | 486 | s32 (*config_read_fn)(const struct i2c_client *client, u8 reg); |
|---|
| 368 | 487 | u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1]; |
|---|
| .. | .. |
|---|
| 374 | 493 | const struct coefficients *coefficients; |
|---|
| 375 | 494 | int vindex = -1, voindex = -1, cindex = -1, pindex = -1; |
|---|
| 376 | 495 | int tindex = -1; |
|---|
| 496 | + u32 shunt; |
|---|
| 497 | + u32 avg; |
|---|
| 377 | 498 | |
|---|
| 378 | 499 | if (!i2c_check_functionality(client->adapter, |
|---|
| 379 | 500 | I2C_FUNC_SMBUS_READ_BYTE_DATA |
|---|
| .. | .. |
|---|
| 404 | 525 | return -ENODEV; |
|---|
| 405 | 526 | } |
|---|
| 406 | 527 | |
|---|
| 407 | | - if (id->driver_data != mid->driver_data) |
|---|
| 528 | + if (strcmp(client->name, mid->name) != 0) |
|---|
| 408 | 529 | dev_notice(&client->dev, |
|---|
| 409 | 530 | "Device mismatch: Configured %s, detected %s\n", |
|---|
| 410 | | - id->name, mid->name); |
|---|
| 531 | + client->name, mid->name); |
|---|
| 411 | 532 | |
|---|
| 412 | 533 | if (mid->driver_data == adm1272 || mid->driver_data == adm1278 || |
|---|
| 413 | 534 | mid->driver_data == adm1293 || mid->driver_data == adm1294) |
|---|
| .. | .. |
|---|
| 427 | 548 | if (!data) |
|---|
| 428 | 549 | return -ENOMEM; |
|---|
| 429 | 550 | |
|---|
| 551 | + if (of_property_read_u32(client->dev.of_node, |
|---|
| 552 | + "shunt-resistor-micro-ohms", &shunt)) |
|---|
| 553 | + shunt = 1000; /* 1 mOhm if not set via DT */ |
|---|
| 554 | + |
|---|
| 555 | + if (shunt == 0) |
|---|
| 556 | + return -EINVAL; |
|---|
| 557 | + |
|---|
| 430 | 558 | data->id = mid->driver_data; |
|---|
| 431 | 559 | |
|---|
| 432 | 560 | info = &data->info; |
|---|
| .. | .. |
|---|
| 437 | 565 | info->format[PSC_CURRENT_OUT] = direct; |
|---|
| 438 | 566 | info->format[PSC_POWER] = direct; |
|---|
| 439 | 567 | info->format[PSC_TEMPERATURE] = direct; |
|---|
| 440 | | - info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT; |
|---|
| 568 | + info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT | |
|---|
| 569 | + PMBUS_HAVE_SAMPLES; |
|---|
| 441 | 570 | |
|---|
| 442 | 571 | info->read_word_data = adm1275_read_word_data; |
|---|
| 443 | 572 | info->read_byte_data = adm1275_read_byte_data; |
|---|
| .. | .. |
|---|
| 478 | 607 | data->have_vout = true; |
|---|
| 479 | 608 | data->have_pin_max = true; |
|---|
| 480 | 609 | data->have_temp_max = true; |
|---|
| 610 | + data->have_power_sampling = true; |
|---|
| 481 | 611 | |
|---|
| 482 | 612 | coefficients = adm1272_coefficients; |
|---|
| 483 | 613 | vindex = (config & ADM1275_VRANGE) ? 1 : 0; |
|---|
| .. | .. |
|---|
| 501 | 631 | tindex = 8; |
|---|
| 502 | 632 | |
|---|
| 503 | 633 | info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT | |
|---|
| 504 | | - PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT; |
|---|
| 634 | + PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | |
|---|
| 635 | + PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP; |
|---|
| 505 | 636 | |
|---|
| 506 | | - /* Enable VOUT if not enabled (it is disabled by default) */ |
|---|
| 507 | | - if (!(config & ADM1278_VOUT_EN)) { |
|---|
| 508 | | - config |= ADM1278_VOUT_EN; |
|---|
| 509 | | - ret = i2c_smbus_write_byte_data(client, |
|---|
| 510 | | - ADM1275_PMON_CONFIG, |
|---|
| 511 | | - config); |
|---|
| 512 | | - if (ret < 0) { |
|---|
| 513 | | - dev_err(&client->dev, |
|---|
| 514 | | - "Failed to enable VOUT monitoring\n"); |
|---|
| 515 | | - return -ENODEV; |
|---|
| 516 | | - } |
|---|
| 517 | | - } |
|---|
| 637 | + ret = adm1275_enable_vout_temp(client, config); |
|---|
| 638 | + if (ret) |
|---|
| 639 | + return ret; |
|---|
| 518 | 640 | |
|---|
| 519 | | - if (config & ADM1278_TEMP1_EN) |
|---|
| 520 | | - info->func[0] |= |
|---|
| 521 | | - PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP; |
|---|
| 522 | 641 | if (config & ADM1278_VIN_EN) |
|---|
| 523 | 642 | info->func[0] |= PMBUS_HAVE_VIN; |
|---|
| 524 | 643 | break; |
|---|
| .. | .. |
|---|
| 563 | 682 | data->have_vout = true; |
|---|
| 564 | 683 | data->have_pin_max = true; |
|---|
| 565 | 684 | data->have_temp_max = true; |
|---|
| 685 | + data->have_power_sampling = true; |
|---|
| 566 | 686 | |
|---|
| 567 | 687 | coefficients = adm1278_coefficients; |
|---|
| 568 | 688 | vindex = 0; |
|---|
| .. | .. |
|---|
| 571 | 691 | tindex = 3; |
|---|
| 572 | 692 | |
|---|
| 573 | 693 | info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT | |
|---|
| 574 | | - PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT; |
|---|
| 694 | + PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | |
|---|
| 695 | + PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP; |
|---|
| 575 | 696 | |
|---|
| 576 | | - /* Enable VOUT if not enabled (it is disabled by default) */ |
|---|
| 577 | | - if (!(config & ADM1278_VOUT_EN)) { |
|---|
| 578 | | - config |= ADM1278_VOUT_EN; |
|---|
| 579 | | - ret = i2c_smbus_write_byte_data(client, |
|---|
| 580 | | - ADM1275_PMON_CONFIG, |
|---|
| 581 | | - config); |
|---|
| 582 | | - if (ret < 0) { |
|---|
| 583 | | - dev_err(&client->dev, |
|---|
| 584 | | - "Failed to enable VOUT monitoring\n"); |
|---|
| 585 | | - return -ENODEV; |
|---|
| 586 | | - } |
|---|
| 587 | | - } |
|---|
| 697 | + ret = adm1275_enable_vout_temp(client, config); |
|---|
| 698 | + if (ret) |
|---|
| 699 | + return ret; |
|---|
| 588 | 700 | |
|---|
| 589 | | - if (config & ADM1278_TEMP1_EN) |
|---|
| 590 | | - info->func[0] |= |
|---|
| 591 | | - PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP; |
|---|
| 592 | 701 | if (config & ADM1278_VIN_EN) |
|---|
| 593 | 702 | info->func[0] |= PMBUS_HAVE_VIN; |
|---|
| 594 | 703 | break; |
|---|
| .. | .. |
|---|
| 598 | 707 | data->have_pin_min = true; |
|---|
| 599 | 708 | data->have_pin_max = true; |
|---|
| 600 | 709 | data->have_mfr_vaux_status = true; |
|---|
| 710 | + data->have_power_sampling = true; |
|---|
| 601 | 711 | |
|---|
| 602 | 712 | coefficients = adm1293_coefficients; |
|---|
| 603 | 713 | |
|---|
| .. | .. |
|---|
| 647 | 757 | return -ENODEV; |
|---|
| 648 | 758 | } |
|---|
| 649 | 759 | |
|---|
| 760 | + if (data->have_power_sampling && |
|---|
| 761 | + of_property_read_u32(client->dev.of_node, |
|---|
| 762 | + "adi,power-sample-average", &avg) == 0) { |
|---|
| 763 | + if (!avg || avg > ADM1275_SAMPLES_AVG_MAX || |
|---|
| 764 | + BIT(__fls(avg)) != avg) { |
|---|
| 765 | + dev_err(&client->dev, |
|---|
| 766 | + "Invalid number of power samples"); |
|---|
| 767 | + return -EINVAL; |
|---|
| 768 | + } |
|---|
| 769 | + ret = adm1275_write_pmon_config(data, client, true, |
|---|
| 770 | + ilog2(avg)); |
|---|
| 771 | + if (ret < 0) { |
|---|
| 772 | + dev_err(&client->dev, |
|---|
| 773 | + "Setting power sample averaging failed with error %d", |
|---|
| 774 | + ret); |
|---|
| 775 | + return ret; |
|---|
| 776 | + } |
|---|
| 777 | + } |
|---|
| 778 | + |
|---|
| 779 | + if (of_property_read_u32(client->dev.of_node, |
|---|
| 780 | + "adi,volt-curr-sample-average", &avg) == 0) { |
|---|
| 781 | + if (!avg || avg > ADM1275_SAMPLES_AVG_MAX || |
|---|
| 782 | + BIT(__fls(avg)) != avg) { |
|---|
| 783 | + dev_err(&client->dev, |
|---|
| 784 | + "Invalid number of voltage/current samples"); |
|---|
| 785 | + return -EINVAL; |
|---|
| 786 | + } |
|---|
| 787 | + ret = adm1275_write_pmon_config(data, client, false, |
|---|
| 788 | + ilog2(avg)); |
|---|
| 789 | + if (ret < 0) { |
|---|
| 790 | + dev_err(&client->dev, |
|---|
| 791 | + "Setting voltage and current sample averaging failed with error %d", |
|---|
| 792 | + ret); |
|---|
| 793 | + return ret; |
|---|
| 794 | + } |
|---|
| 795 | + } |
|---|
| 796 | + |
|---|
| 650 | 797 | if (voindex < 0) |
|---|
| 651 | 798 | voindex = vindex; |
|---|
| 652 | 799 | if (vindex >= 0) { |
|---|
| .. | .. |
|---|
| 660 | 807 | info->R[PSC_VOLTAGE_OUT] = coefficients[voindex].R; |
|---|
| 661 | 808 | } |
|---|
| 662 | 809 | if (cindex >= 0) { |
|---|
| 663 | | - info->m[PSC_CURRENT_OUT] = coefficients[cindex].m; |
|---|
| 810 | + /* Scale current with sense resistor value */ |
|---|
| 811 | + info->m[PSC_CURRENT_OUT] = |
|---|
| 812 | + coefficients[cindex].m * shunt / 1000; |
|---|
| 664 | 813 | info->b[PSC_CURRENT_OUT] = coefficients[cindex].b; |
|---|
| 665 | 814 | info->R[PSC_CURRENT_OUT] = coefficients[cindex].R; |
|---|
| 666 | 815 | } |
|---|
| 667 | 816 | if (pindex >= 0) { |
|---|
| 668 | | - info->m[PSC_POWER] = coefficients[pindex].m; |
|---|
| 817 | + info->m[PSC_POWER] = |
|---|
| 818 | + coefficients[pindex].m * shunt / 1000; |
|---|
| 669 | 819 | info->b[PSC_POWER] = coefficients[pindex].b; |
|---|
| 670 | 820 | info->R[PSC_POWER] = coefficients[pindex].R; |
|---|
| 671 | 821 | } |
|---|
| .. | .. |
|---|
| 675 | 825 | info->R[PSC_TEMPERATURE] = coefficients[tindex].R; |
|---|
| 676 | 826 | } |
|---|
| 677 | 827 | |
|---|
| 678 | | - return pmbus_do_probe(client, id, info); |
|---|
| 828 | + return pmbus_do_probe(client, info); |
|---|
| 679 | 829 | } |
|---|
| 680 | 830 | |
|---|
| 681 | 831 | static struct i2c_driver adm1275_driver = { |
|---|
| 682 | 832 | .driver = { |
|---|
| 683 | 833 | .name = "adm1275", |
|---|
| 684 | 834 | }, |
|---|
| 685 | | - .probe = adm1275_probe, |
|---|
| 835 | + .probe_new = adm1275_probe, |
|---|
| 686 | 836 | .remove = pmbus_do_remove, |
|---|
| 687 | 837 | .id_table = adm1275_id, |
|---|
| 688 | 838 | }; |
|---|