at commit c03c1a14
| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Hardware monitoring driver for PMBus devices |
| 4 | * |
| 5 | * Copyright (c) 2010, 2011 Ericsson AB. |
| 6 | * Copyright (c) 2012 Guenter Roeck |
| 7 | */ |
| 8 | |
| 9 | #include <linux/atomic.h> |
| 10 | #include <linux/debugfs.h> |
| 11 | #include <linux/delay.h> |
| 12 | #include <linux/dcache.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/math64.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/i2c.h> |
| 20 | #include <linux/hwmon.h> |
| 21 | #include <linux/hwmon-sysfs.h> |
| 22 | #include <linux/pmbus.h> |
| 23 | #include <linux/regulator/driver.h> |
| 24 | #include <linux/regulator/machine.h> |
| 25 | #include <linux/thermal.h> |
| 26 | #include <linux/workqueue.h> |
| 27 | #include "pmbus.h" |
| 28 | |
| 29 | /* |
| 30 | * Number of additional attribute pointers to allocate |
| 31 | * with each call to krealloc |
| 32 | */ |
| 33 | #define PMBUS_ATTR_ALLOC_SIZE 32 |
| 34 | #define PMBUS_NAME_SIZE 24 |
| 35 | |
| 36 | /* |
| 37 | * The type of operation used for picking the delay between |
| 38 | * successive pmbus operations. |
| 39 | */ |
| 40 | /* PMBUS_OP_WRITE and PMBUS_OP_PAGE_CHANGE are defined in pmbus.h */ |
| 41 | |
| 42 | static int wp = -1; |
| 43 | module_param(wp, int, 0444); |
| 44 | |
| 45 | struct pmbus_sensor { |
| 46 | struct pmbus_sensor *next; |
| 47 | char name[PMBUS_NAME_SIZE]; /* sysfs sensor name */ |
| 48 | struct sensor_device_attribute attribute; |
| 49 | u8 page; /* page number */ |
| 50 | u8 phase; /* phase number, 0xff for all phases */ |
| 51 | u16 reg; /* register */ |
| 52 | enum pmbus_sensor_classes class; /* sensor class */ |
| 53 | bool update; /* runtime sensor update needed */ |
| 54 | bool convert; /* Whether or not to apply linear/vid/direct */ |
| 55 | int data; /* Sensor data; negative if there was a read error */ |
| 56 | }; |
| 57 | #define to_pmbus_sensor(_attr) \ |
| 58 | container_of(_attr, struct pmbus_sensor, attribute) |
| 59 | |
| 60 | struct pmbus_boolean { |
| 61 | char name[PMBUS_NAME_SIZE]; /* sysfs boolean name */ |
| 62 | struct sensor_device_attribute attribute; |
| 63 | struct pmbus_sensor *s1; |
| 64 | struct pmbus_sensor *s2; |
| 65 | }; |
| 66 | #define to_pmbus_boolean(_attr) \ |
| 67 | container_of(_attr, struct pmbus_boolean, attribute) |
| 68 | |
| 69 | struct pmbus_label { |
| 70 | char name[PMBUS_NAME_SIZE]; /* sysfs label name */ |
| 71 | struct sensor_device_attribute attribute; |
| 72 | char label[PMBUS_NAME_SIZE]; /* label */ |
| 73 | }; |
| 74 | #define to_pmbus_label(_attr) \ |
| 75 | container_of(_attr, struct pmbus_label, attribute) |
| 76 | |
| 77 | /* Macros for converting between sensor index and register/page/status mask */ |
| 78 | |
| 79 | #define PB_STATUS_MASK 0xffff |
| 80 | #define PB_REG_SHIFT 16 |
| 81 | #define PB_REG_MASK 0x3ff |
| 82 | #define PB_PAGE_SHIFT 26 |
| 83 | #define PB_PAGE_MASK 0x3f |
| 84 | |
| 85 | #define pb_reg_to_index(page, reg, mask) (((page) << PB_PAGE_SHIFT) | \ |
| 86 | ((reg) << PB_REG_SHIFT) | (mask)) |
| 87 | |
| 88 | #define pb_index_to_page(index) (((index) >> PB_PAGE_SHIFT) & PB_PAGE_MASK) |
| 89 | #define pb_index_to_reg(index) (((index) >> PB_REG_SHIFT) & PB_REG_MASK) |
| 90 | #define pb_index_to_mask(index) ((index) & PB_STATUS_MASK) |
| 91 | |
| 92 | struct pmbus_data { |
| 93 | struct device *dev; |
| 94 | struct device *hwmon_dev; |
| 95 | struct regulator_dev **rdevs; |
| 96 | |
| 97 | u32 flags; /* from platform data */ |
| 98 | |
| 99 | bool have_pmbus_revision; |
| 100 | u8 revision; /* The PMBus revision the device is compliant with */ |
| 101 | |
| 102 | int exponent[PMBUS_PAGES]; |
| 103 | /* linear mode: exponent for output voltages */ |
| 104 | |
| 105 | const struct pmbus_driver_info *info; |
| 106 | |
| 107 | int max_attributes; |
| 108 | int num_attributes; |
| 109 | struct attribute_group group; |
| 110 | const struct attribute_group **groups; |
| 111 | |
| 112 | struct pmbus_sensor *sensors; |
| 113 | |
| 114 | struct mutex update_lock; |
| 115 | |
| 116 | #if IS_ENABLED(CONFIG_REGULATOR) |
| 117 | atomic_t regulator_events[PMBUS_PAGES]; |
| 118 | struct work_struct regulator_notify_work; |
| 119 | #endif |
| 120 | |
| 121 | bool has_status_word; /* device uses STATUS_WORD register */ |
| 122 | int (*read_status)(struct i2c_client *client, int page); |
| 123 | |
| 124 | s16 currpage; /* current page, -1 for unknown/unset */ |
| 125 | s16 currphase; /* current phase, 0xff for all, -1 for unknown/unset */ |
| 126 | |
| 127 | int vout_low[PMBUS_PAGES]; /* voltage low margin */ |
| 128 | int vout_high[PMBUS_PAGES]; /* voltage high margin */ |
| 129 | |
| 130 | ktime_t next_access_backoff; /* Wait until at least this time */ |
| 131 | }; |
| 132 | |
| 133 | struct pmbus_debugfs_entry { |
| 134 | struct i2c_client *client; |
| 135 | u8 page; |
| 136 | u8 reg; |
| 137 | }; |
| 138 | |
| 139 | static const int pmbus_fan_rpm_mask[] = { |
| 140 | PB_FAN_1_RPM, |
| 141 | PB_FAN_2_RPM, |
| 142 | PB_FAN_1_RPM, |
| 143 | PB_FAN_2_RPM, |
| 144 | }; |
| 145 | |
| 146 | static const int pmbus_fan_config_registers[] = { |
| 147 | PMBUS_FAN_CONFIG_12, |
| 148 | PMBUS_FAN_CONFIG_12, |
| 149 | PMBUS_FAN_CONFIG_34, |
| 150 | PMBUS_FAN_CONFIG_34 |
| 151 | }; |
| 152 | |
| 153 | static const int pmbus_fan_command_registers[] = { |
| 154 | PMBUS_FAN_COMMAND_1, |
| 155 | PMBUS_FAN_COMMAND_2, |
| 156 | PMBUS_FAN_COMMAND_3, |
| 157 | PMBUS_FAN_COMMAND_4, |
| 158 | }; |
| 159 | |
| 160 | void pmbus_clear_cache(struct i2c_client *client) |
| 161 | { |
| 162 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 163 | struct pmbus_sensor *sensor; |
| 164 | |
| 165 | for (sensor = data->sensors; sensor; sensor = sensor->next) |
| 166 | sensor->data = -ENODATA; |
| 167 | } |
| 168 | EXPORT_SYMBOL_NS_GPL(pmbus_clear_cache, "PMBUS"); |
| 169 | |
| 170 | void pmbus_set_update(struct i2c_client *client, u8 reg, bool update) |
| 171 | { |
| 172 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 173 | struct pmbus_sensor *sensor; |
| 174 | |
| 175 | for (sensor = data->sensors; sensor; sensor = sensor->next) |
| 176 | if (sensor->reg == reg) |
| 177 | sensor->update = update; |
| 178 | } |
| 179 | EXPORT_SYMBOL_NS_GPL(pmbus_set_update, "PMBUS"); |
| 180 | |
| 181 | /* Some chips need a delay between accesses. */ |
| 182 | void pmbus_wait(struct i2c_client *client) |
| 183 | { |
| 184 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 185 | s64 delay; |
| 186 | |
| 187 | if (!data) |
| 188 | return; |
| 189 | |
| 190 | delay = ktime_us_delta(data->next_access_backoff, ktime_get()); |
| 191 | |
| 192 | if (delay > 0) |
| 193 | fsleep(delay); |
| 194 | } |
| 195 | EXPORT_SYMBOL_NS_GPL(pmbus_wait, "PMBUS"); |
| 196 | |
| 197 | /* Sets the last operation timestamp for pmbus_wait */ |
| 198 | void pmbus_update_ts(struct i2c_client *client, int op) |
| 199 | { |
| 200 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 201 | const struct pmbus_driver_info *info; |
| 202 | int delay; |
| 203 | |
| 204 | if (!data) |
| 205 | return; |
| 206 | |
| 207 | info = data->info; |
| 208 | delay = info->access_delay; |
| 209 | |
| 210 | if (op & PMBUS_OP_WRITE) |
| 211 | delay = max(delay, info->write_delay); |
| 212 | if (op & PMBUS_OP_PAGE_CHANGE) |
| 213 | delay = max(delay, info->page_change_delay); |
| 214 | |
| 215 | if (delay > 0) |
| 216 | data->next_access_backoff = ktime_add_us(ktime_get(), delay); |
| 217 | } |
| 218 | EXPORT_SYMBOL_NS_GPL(pmbus_update_ts, "PMBUS"); |
| 219 | |
| 220 | int pmbus_set_page(struct i2c_client *client, int page, int phase) |
| 221 | { |
| 222 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 223 | int rv; |
| 224 | |
| 225 | if (page < 0) |
| 226 | return 0; |
| 227 | |
| 228 | if (!(data->info->func[page] & PMBUS_PAGE_VIRTUAL) && |
| 229 | data->info->pages > 1 && page != data->currpage) { |
| 230 | pmbus_wait(client); |
| 231 | rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page); |
| 232 | pmbus_update_ts(client, PMBUS_OP_WRITE | PMBUS_OP_PAGE_CHANGE); |
| 233 | if (rv < 0) |
| 234 | return rv; |
| 235 | |
| 236 | pmbus_wait(client); |
| 237 | rv = i2c_smbus_read_byte_data(client, PMBUS_PAGE); |
| 238 | pmbus_update_ts(client, 0); |
| 239 | if (rv < 0) |
| 240 | return rv; |
| 241 | |
| 242 | if (rv != page) |
| 243 | return -EIO; |
| 244 | } |
| 245 | data->currpage = page; |
| 246 | |
| 247 | if (data->info->phases[page] && data->currphase != phase && |
| 248 | !(data->info->func[page] & PMBUS_PHASE_VIRTUAL)) { |
| 249 | pmbus_wait(client); |
| 250 | rv = i2c_smbus_write_byte_data(client, PMBUS_PHASE, |
| 251 | phase); |
| 252 | pmbus_update_ts(client, PMBUS_OP_WRITE); |
| 253 | if (rv) |
| 254 | return rv; |
| 255 | } |
| 256 | data->currphase = phase; |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | EXPORT_SYMBOL_NS_GPL(pmbus_set_page, "PMBUS"); |
| 261 | |
| 262 | int pmbus_write_byte(struct i2c_client *client, int page, u8 value) |
| 263 | { |
| 264 | int rv; |
| 265 | |
| 266 | rv = pmbus_set_page(client, page, 0xff); |
| 267 | if (rv < 0) |
| 268 | return rv; |
| 269 | |
| 270 | pmbus_wait(client); |
| 271 | rv = i2c_smbus_write_byte(client, value); |
| 272 | pmbus_update_ts(client, PMBUS_OP_WRITE); |
| 273 | |
| 274 | return rv; |
| 275 | } |
| 276 | EXPORT_SYMBOL_NS_GPL(pmbus_write_byte, "PMBUS"); |
| 277 | |
| 278 | /* |
| 279 | * _pmbus_write_byte() is similar to pmbus_write_byte(), but checks if |
| 280 | * a device specific mapping function exists and calls it if necessary. |
| 281 | */ |
| 282 | static int _pmbus_write_byte(struct i2c_client *client, int page, u8 value) |
| 283 | { |
| 284 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 285 | const struct pmbus_driver_info *info = data->info; |
| 286 | int status; |
| 287 | |
| 288 | if (info->write_byte) { |
| 289 | status = info->write_byte(client, page, value); |
| 290 | if (status != -ENODATA) |
| 291 | return status; |
| 292 | } |
| 293 | return pmbus_write_byte(client, page, value); |
| 294 | } |
| 295 | |
| 296 | int pmbus_write_word_data(struct i2c_client *client, int page, u8 reg, |
| 297 | u16 word) |
| 298 | { |
| 299 | int rv; |
| 300 | |
| 301 | rv = pmbus_set_page(client, page, 0xff); |
| 302 | if (rv < 0) |
| 303 | return rv; |
| 304 | |
| 305 | pmbus_wait(client); |
| 306 | rv = i2c_smbus_write_word_data(client, reg, word); |
| 307 | pmbus_update_ts(client, PMBUS_OP_WRITE); |
| 308 | |
| 309 | return rv; |
| 310 | } |
| 311 | EXPORT_SYMBOL_NS_GPL(pmbus_write_word_data, "PMBUS"); |
| 312 | |
| 313 | static int pmbus_write_virt_reg(struct i2c_client *client, int page, int reg, |
| 314 | u16 word) |
| 315 | { |
| 316 | int bit; |
| 317 | int id; |
| 318 | int rv; |
| 319 | |
| 320 | switch (reg) { |
| 321 | case PMBUS_VIRT_FAN_TARGET_1 ... PMBUS_VIRT_FAN_TARGET_4: |
| 322 | id = reg - PMBUS_VIRT_FAN_TARGET_1; |
| 323 | bit = pmbus_fan_rpm_mask[id]; |
| 324 | rv = pmbus_update_fan(client, page, id, bit, bit, word); |
| 325 | break; |
| 326 | default: |
| 327 | rv = -ENXIO; |
| 328 | break; |
| 329 | } |
| 330 | |
| 331 | return rv; |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * _pmbus_write_word_data() is similar to pmbus_write_word_data(), but checks if |
| 336 | * a device specific mapping function exists and calls it if necessary. |
| 337 | */ |
| 338 | static int _pmbus_write_word_data(struct i2c_client *client, int page, int reg, |
| 339 | u16 word) |
| 340 | { |
| 341 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 342 | const struct pmbus_driver_info *info = data->info; |
| 343 | int status; |
| 344 | |
| 345 | if (info->write_word_data) { |
| 346 | status = info->write_word_data(client, page, reg, word); |
| 347 | if (status != -ENODATA) |
| 348 | return status; |
| 349 | } |
| 350 | |
| 351 | if (reg >= PMBUS_VIRT_BASE) |
| 352 | return pmbus_write_virt_reg(client, page, reg, word); |
| 353 | |
| 354 | return pmbus_write_word_data(client, page, reg, word); |
| 355 | } |
| 356 | |
| 357 | /* |
| 358 | * _pmbus_write_byte_data() is similar to pmbus_write_byte_data(), but checks if |
| 359 | * a device specific mapping function exists and calls it if necessary. |
| 360 | */ |
| 361 | static int _pmbus_write_byte_data(struct i2c_client *client, int page, int reg, u8 value) |
| 362 | { |
| 363 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 364 | const struct pmbus_driver_info *info = data->info; |
| 365 | int status; |
| 366 | |
| 367 | if (info->write_byte_data) { |
| 368 | status = info->write_byte_data(client, page, reg, value); |
| 369 | if (status != -ENODATA) |
| 370 | return status; |
| 371 | } |
| 372 | return pmbus_write_byte_data(client, page, reg, value); |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if |
| 377 | * a device specific mapping function exists and calls it if necessary. |
| 378 | */ |
| 379 | static int _pmbus_read_byte_data(struct i2c_client *client, int page, int reg) |
| 380 | { |
| 381 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 382 | const struct pmbus_driver_info *info = data->info; |
| 383 | int status; |
| 384 | |
| 385 | if (info->read_byte_data) { |
| 386 | status = info->read_byte_data(client, page, reg); |
| 387 | if (status != -ENODATA) |
| 388 | return status; |
| 389 | } |
| 390 | return pmbus_read_byte_data(client, page, reg); |
| 391 | } |
| 392 | |
| 393 | int pmbus_update_fan(struct i2c_client *client, int page, int id, |
| 394 | u8 config, u8 mask, u16 command) |
| 395 | { |
| 396 | int from; |
| 397 | int rv; |
| 398 | u8 to; |
| 399 | |
| 400 | from = _pmbus_read_byte_data(client, page, |
| 401 | pmbus_fan_config_registers[id]); |
| 402 | if (from < 0) |
| 403 | return from; |
| 404 | |
| 405 | to = (from & ~mask) | (config & mask); |
| 406 | if (to != from) { |
| 407 | rv = _pmbus_write_byte_data(client, page, |
| 408 | pmbus_fan_config_registers[id], to); |
| 409 | if (rv < 0) |
| 410 | return rv; |
| 411 | } |
| 412 | |
| 413 | return _pmbus_write_word_data(client, page, |
| 414 | pmbus_fan_command_registers[id], command); |
| 415 | } |
| 416 | EXPORT_SYMBOL_NS_GPL(pmbus_update_fan, "PMBUS"); |
| 417 | |
| 418 | int pmbus_read_word_data(struct i2c_client *client, int page, int phase, u8 reg) |
| 419 | { |
| 420 | int rv; |
| 421 | |
| 422 | rv = pmbus_set_page(client, page, phase); |
| 423 | if (rv < 0) |
| 424 | return rv; |
| 425 | |
| 426 | pmbus_wait(client); |
| 427 | rv = i2c_smbus_read_word_data(client, reg); |
| 428 | pmbus_update_ts(client, 0); |
| 429 | |
| 430 | return rv; |
| 431 | } |
| 432 | EXPORT_SYMBOL_NS_GPL(pmbus_read_word_data, "PMBUS"); |
| 433 | |
| 434 | static int pmbus_read_virt_reg(struct i2c_client *client, int page, int reg) |
| 435 | { |
| 436 | int rv; |
| 437 | int id; |
| 438 | |
| 439 | switch (reg) { |
| 440 | case PMBUS_VIRT_FAN_TARGET_1 ... PMBUS_VIRT_FAN_TARGET_4: |
| 441 | id = reg - PMBUS_VIRT_FAN_TARGET_1; |
| 442 | rv = pmbus_get_fan_rate_device(client, page, id, rpm); |
| 443 | break; |
| 444 | default: |
| 445 | rv = -ENXIO; |
| 446 | break; |
| 447 | } |
| 448 | |
| 449 | return rv; |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | * _pmbus_read_word_data() is similar to pmbus_read_word_data(), but checks if |
| 454 | * a device specific mapping function exists and calls it if necessary. |
| 455 | */ |
| 456 | static int _pmbus_read_word_data(struct i2c_client *client, int page, |
| 457 | int phase, int reg) |
| 458 | { |
| 459 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 460 | const struct pmbus_driver_info *info = data->info; |
| 461 | int status; |
| 462 | |
| 463 | if (info->read_word_data) { |
| 464 | status = info->read_word_data(client, page, phase, reg); |
| 465 | if (status != -ENODATA) |
| 466 | return status; |
| 467 | } |
| 468 | |
| 469 | if (reg >= PMBUS_VIRT_BASE) |
| 470 | return pmbus_read_virt_reg(client, page, reg); |
| 471 | |
| 472 | return pmbus_read_word_data(client, page, phase, reg); |
| 473 | } |
| 474 | |
| 475 | /* Same as above, but without phase parameter, for use in check functions */ |
| 476 | static int __pmbus_read_word_data(struct i2c_client *client, int page, int reg) |
| 477 | { |
| 478 | return _pmbus_read_word_data(client, page, 0xff, reg); |
| 479 | } |
| 480 | |
| 481 | int pmbus_read_byte_data(struct i2c_client *client, int page, u8 reg) |
| 482 | { |
| 483 | int rv; |
| 484 | |
| 485 | rv = pmbus_set_page(client, page, 0xff); |
| 486 | if (rv < 0) |
| 487 | return rv; |
| 488 | |
| 489 | pmbus_wait(client); |
| 490 | rv = i2c_smbus_read_byte_data(client, reg); |
| 491 | pmbus_update_ts(client, 0); |
| 492 | |
| 493 | return rv; |
| 494 | } |
| 495 | EXPORT_SYMBOL_NS_GPL(pmbus_read_byte_data, "PMBUS"); |
| 496 | |
| 497 | int pmbus_write_byte_data(struct i2c_client *client, int page, u8 reg, u8 value) |
| 498 | { |
| 499 | int rv; |
| 500 | |
| 501 | rv = pmbus_set_page(client, page, 0xff); |
| 502 | if (rv < 0) |
| 503 | return rv; |
| 504 | |
| 505 | pmbus_wait(client); |
| 506 | rv = i2c_smbus_write_byte_data(client, reg, value); |
| 507 | pmbus_update_ts(client, PMBUS_OP_WRITE); |
| 508 | |
| 509 | return rv; |
| 510 | } |
| 511 | EXPORT_SYMBOL_NS_GPL(pmbus_write_byte_data, "PMBUS"); |
| 512 | |
| 513 | int pmbus_update_byte_data(struct i2c_client *client, int page, u8 reg, |
| 514 | u8 mask, u8 value) |
| 515 | { |
| 516 | unsigned int tmp; |
| 517 | int rv; |
| 518 | |
| 519 | rv = _pmbus_read_byte_data(client, page, reg); |
| 520 | if (rv < 0) |
| 521 | return rv; |
| 522 | |
| 523 | tmp = (rv & ~mask) | (value & mask); |
| 524 | |
| 525 | if (tmp != rv) |
| 526 | rv = _pmbus_write_byte_data(client, page, reg, tmp); |
| 527 | |
| 528 | return rv < 0 ? rv : 0; |
| 529 | } |
| 530 | EXPORT_SYMBOL_NS_GPL(pmbus_update_byte_data, "PMBUS"); |
| 531 | |
| 532 | /** |
| 533 | * pmbus_read_smbus_i2c_block_data() - Read SMBus/I2C block data |
| 534 | * @client: Handle to slave device |
| 535 | * @reg: Byte interpreted by slave |
| 536 | * @data_buf: Byte array into which data will be read |
| 537 | * Return: Negative errno or number of bytes read |
| 538 | * |
| 539 | * PMBus internal function to read a SMBus block from a PMBus chip. |
| 540 | * |
| 541 | * PMBus chips report various properties using SMBus block read operations. |
| 542 | * However, not all I2C controllers support this operation. |
| 543 | * |
| 544 | * Execute SMBus block read if supported. If not supported, but SMBus I2C block |
| 545 | * read is supported, use it instead. Note that at most 31 data bytes can be |
| 546 | * read from the device if i2c_smbus_read_i2c_block_data() is used to read the |
| 547 | * data. This is a SMBUs protocol limit which can not be avoided. |
| 548 | * |
| 549 | * Return -EOPNOTSUPP if neither I2C_FUNC_SMBUS_READ_BLOCK_DATA nor |
| 550 | * I2C_FUNC_SMBUS_READ_I2C_BLOCK is supported. |
| 551 | * |
| 552 | * Callers must hold pmbus_lock or execute calls from the probe function. |
| 553 | */ |
| 554 | int pmbus_read_smbus_i2c_block_data(struct i2c_client *client, u8 reg, char *data_buf) |
| 555 | { |
| 556 | u8 buf[I2C_SMBUS_BLOCK_MAX]; |
| 557 | int blen, len, ret; |
| 558 | |
| 559 | if (i2c_check_functionality(client->adapter, |
| 560 | I2C_FUNC_SMBUS_READ_BLOCK_DATA)) { |
| 561 | pmbus_wait(client); |
| 562 | ret = i2c_smbus_read_block_data(client, reg, data_buf); |
| 563 | pmbus_update_ts(client, 0); |
| 564 | return ret; |
| 565 | } |
| 566 | |
| 567 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) { |
| 568 | dev_err_once(&client->dev, "I2C adapter does not support I2C_FUNC_SMBUS_READ_I2C_BLOCK\n"); |
| 569 | return -EOPNOTSUPP; |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * If the returned data is valid SMBus block data, the first byte |
| 574 | * must be the data length. |
| 575 | * |
| 576 | * i2c_smbus_read_i2c_block_data() may return an error if the chip |
| 577 | * sends NACK before the number of requested bytes is received. |
| 578 | * Handle this by reading the data length first, then reading the |
| 579 | * entire message up to I2C_SMBUS_BLOCK_MAX bytes. This ensures |
| 580 | * that requested number of bytes never exceeds the number of |
| 581 | * bytes sent by the chip. |
| 582 | */ |
| 583 | pmbus_wait(client); |
| 584 | ret = i2c_smbus_read_i2c_block_data(client, reg, 1, buf); |
| 585 | pmbus_update_ts(client, 0); |
| 586 | if (ret < 0) |
| 587 | return ret; |
| 588 | |
| 589 | len = buf[0]; |
| 590 | if (len == 0) |
| 591 | return 0; |
| 592 | blen = len; |
| 593 | if (len >= I2C_SMBUS_BLOCK_MAX) |
| 594 | len = I2C_SMBUS_BLOCK_MAX - 1; |
| 595 | pmbus_wait(client); |
| 596 | ret = i2c_smbus_read_i2c_block_data(client, reg, len + 1, buf); |
| 597 | pmbus_update_ts(client, 0); |
| 598 | if (ret < 0) |
| 599 | return ret; |
| 600 | if (buf[0] != blen) |
| 601 | return -EIO; |
| 602 | memcpy(data_buf, buf + 1, len); |
| 603 | return len; |
| 604 | } |
| 605 | EXPORT_SYMBOL_NS_GPL(pmbus_read_smbus_i2c_block_data, "PMBUS"); |
| 606 | |
| 607 | static int pmbus_read_block_data(struct i2c_client *client, int page, u8 reg, |
| 608 | char *data_buf) |
| 609 | { |
| 610 | int rv; |
| 611 | |
| 612 | rv = pmbus_set_page(client, page, 0xff); |
| 613 | if (rv < 0) |
| 614 | return rv; |
| 615 | |
| 616 | return pmbus_read_smbus_i2c_block_data(client, reg, data_buf); |
| 617 | } |
| 618 | |
| 619 | static struct pmbus_sensor *pmbus_find_sensor(struct pmbus_data *data, int page, |
| 620 | int reg) |
| 621 | { |
| 622 | struct pmbus_sensor *sensor; |
| 623 | |
| 624 | for (sensor = data->sensors; sensor; sensor = sensor->next) { |
| 625 | if (sensor->page == page && sensor->reg == reg) |
| 626 | return sensor; |
| 627 | } |
| 628 | |
| 629 | return ERR_PTR(-EINVAL); |
| 630 | } |
| 631 | |
| 632 | static int pmbus_get_fan_rate(struct i2c_client *client, int page, int id, |
| 633 | enum pmbus_fan_mode mode, |
| 634 | bool from_cache) |
| 635 | { |
| 636 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 637 | bool want_rpm, have_rpm; |
| 638 | struct pmbus_sensor *s; |
| 639 | int config; |
| 640 | int reg; |
| 641 | |
| 642 | want_rpm = (mode == rpm); |
| 643 | |
| 644 | if (from_cache) { |
| 645 | reg = want_rpm ? PMBUS_VIRT_FAN_TARGET_1 : PMBUS_VIRT_PWM_1; |
| 646 | s = pmbus_find_sensor(data, page, reg + id); |
| 647 | if (IS_ERR(s)) |
| 648 | return PTR_ERR(s); |
| 649 | |
| 650 | return s->data; |
| 651 | } |
| 652 | |
| 653 | config = _pmbus_read_byte_data(client, page, |
| 654 | pmbus_fan_config_registers[id]); |
| 655 | if (config < 0) |
| 656 | return config; |
| 657 | |
| 658 | have_rpm = !!(config & pmbus_fan_rpm_mask[id]); |
| 659 | if (want_rpm == have_rpm) |
| 660 | return pmbus_read_word_data(client, page, 0xff, |
| 661 | pmbus_fan_command_registers[id]); |
| 662 | |
| 663 | /* Can't sensibly map between RPM and PWM, just return zero */ |
| 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | int pmbus_get_fan_rate_device(struct i2c_client *client, int page, int id, |
| 668 | enum pmbus_fan_mode mode) |
| 669 | { |
| 670 | return pmbus_get_fan_rate(client, page, id, mode, false); |
| 671 | } |
| 672 | EXPORT_SYMBOL_NS_GPL(pmbus_get_fan_rate_device, "PMBUS"); |
| 673 | |
| 674 | int pmbus_get_fan_rate_cached(struct i2c_client *client, int page, int id, |
| 675 | enum pmbus_fan_mode mode) |
| 676 | { |
| 677 | return pmbus_get_fan_rate(client, page, id, mode, true); |
| 678 | } |
| 679 | EXPORT_SYMBOL_NS_GPL(pmbus_get_fan_rate_cached, "PMBUS"); |
| 680 | |
| 681 | static void pmbus_clear_fault_page(struct i2c_client *client, int page) |
| 682 | { |
| 683 | _pmbus_write_byte(client, page, PMBUS_CLEAR_FAULTS); |
| 684 | } |
| 685 | |
| 686 | void pmbus_clear_faults(struct i2c_client *client) |
| 687 | { |
| 688 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 689 | int i; |
| 690 | |
| 691 | for (i = 0; i < data->info->pages; i++) |
| 692 | pmbus_clear_fault_page(client, i); |
| 693 | } |
| 694 | EXPORT_SYMBOL_NS_GPL(pmbus_clear_faults, "PMBUS"); |
| 695 | |
| 696 | static int pmbus_check_status_cml(struct i2c_client *client) |
| 697 | { |
| 698 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 699 | int status, status2; |
| 700 | |
| 701 | status = data->read_status(client, -1); |
| 702 | if (status < 0 || (status & PB_STATUS_CML)) { |
| 703 | status2 = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML); |
| 704 | if (status2 < 0 || (status2 & PB_CML_FAULT_INVALID_COMMAND)) |
| 705 | return -EIO; |
| 706 | } |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | static bool pmbus_check_register(struct i2c_client *client, |
| 711 | int (*func)(struct i2c_client *client, |
| 712 | int page, int reg), |
| 713 | int page, int reg) |
| 714 | { |
| 715 | int rv; |
| 716 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 717 | |
| 718 | rv = func(client, page, reg); |
| 719 | if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK)) |
| 720 | rv = pmbus_check_status_cml(client); |
| 721 | if (rv < 0 && (data->flags & PMBUS_READ_STATUS_AFTER_FAILED_CHECK)) |
| 722 | data->read_status(client, -1); |
| 723 | if (reg < PMBUS_VIRT_BASE) |
| 724 | pmbus_clear_fault_page(client, -1); |
| 725 | return rv >= 0; |
| 726 | } |
| 727 | |
| 728 | static bool pmbus_check_status_register(struct i2c_client *client, int page) |
| 729 | { |
| 730 | int status; |
| 731 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 732 | |
| 733 | status = data->read_status(client, page); |
| 734 | if (status >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK) && |
| 735 | (status & PB_STATUS_CML)) { |
| 736 | status = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML); |
| 737 | if (status < 0 || (status & PB_CML_FAULT_INVALID_COMMAND)) |
| 738 | status = -EIO; |
| 739 | } |
| 740 | |
| 741 | pmbus_clear_fault_page(client, -1); |
| 742 | return status >= 0; |
| 743 | } |
| 744 | |
| 745 | bool pmbus_check_byte_register(struct i2c_client *client, int page, int reg) |
| 746 | { |
| 747 | return pmbus_check_register(client, _pmbus_read_byte_data, page, reg); |
| 748 | } |
| 749 | EXPORT_SYMBOL_NS_GPL(pmbus_check_byte_register, "PMBUS"); |
| 750 | |
| 751 | bool pmbus_check_word_register(struct i2c_client *client, int page, int reg) |
| 752 | { |
| 753 | return pmbus_check_register(client, __pmbus_read_word_data, page, reg); |
| 754 | } |
| 755 | EXPORT_SYMBOL_NS_GPL(pmbus_check_word_register, "PMBUS"); |
| 756 | |
| 757 | static bool __maybe_unused pmbus_check_block_register(struct i2c_client *client, |
| 758 | int page, int reg) |
| 759 | { |
| 760 | int rv; |
| 761 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 762 | char data_buf[I2C_SMBUS_BLOCK_MAX + 2]; |
| 763 | |
| 764 | rv = pmbus_read_block_data(client, page, reg, data_buf); |
| 765 | if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK)) |
| 766 | rv = pmbus_check_status_cml(client); |
| 767 | if (rv < 0 && (data->flags & PMBUS_READ_STATUS_AFTER_FAILED_CHECK)) |
| 768 | data->read_status(client, -1); |
| 769 | pmbus_clear_fault_page(client, -1); |
| 770 | return rv >= 0; |
| 771 | } |
| 772 | |
| 773 | const struct pmbus_driver_info *pmbus_get_driver_info(struct i2c_client *client) |
| 774 | { |
| 775 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 776 | |
| 777 | return data->info; |
| 778 | } |
| 779 | EXPORT_SYMBOL_NS_GPL(pmbus_get_driver_info, "PMBUS"); |
| 780 | |
| 781 | static int pmbus_get_status(struct i2c_client *client, int page, int reg) |
| 782 | { |
| 783 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 784 | int status; |
| 785 | |
| 786 | switch (reg) { |
| 787 | case PMBUS_STATUS_WORD: |
| 788 | status = data->read_status(client, page); |
| 789 | break; |
| 790 | default: |
| 791 | status = _pmbus_read_byte_data(client, page, reg); |
| 792 | break; |
| 793 | } |
| 794 | if (status < 0) |
| 795 | pmbus_clear_faults(client); |
| 796 | return status; |
| 797 | } |
| 798 | |
| 799 | static void pmbus_update_sensor_data(struct i2c_client *client, struct pmbus_sensor *sensor) |
| 800 | { |
| 801 | if (sensor->data < 0 || sensor->update) |
| 802 | sensor->data = _pmbus_read_word_data(client, sensor->page, |
| 803 | sensor->phase, sensor->reg); |
| 804 | } |
| 805 | |
| 806 | /* |
| 807 | * Convert ieee754 sensor values to milli- or micro-units |
| 808 | * depending on sensor type. |
| 809 | * |
| 810 | * ieee754 data format: |
| 811 | * bit 15: sign |
| 812 | * bit 10..14: exponent |
| 813 | * bit 0..9: mantissa |
| 814 | * exponent=0: |
| 815 | * v=(−1)^signbit * 2^(−14) * 0.significantbits |
| 816 | * exponent=1..30: |
| 817 | * v=(−1)^signbit * 2^(exponent - 15) * 1.significantbits |
| 818 | * exponent=31: |
| 819 | * v=NaN |
| 820 | * |
| 821 | * Add the number mantissa bits into the calculations for simplicity. |
| 822 | * To do that, add '10' to the exponent. By doing that, we can just add |
| 823 | * 0x400 to normal values and get the expected result. |
| 824 | */ |
| 825 | static long pmbus_reg2data_ieee754(struct pmbus_data *data, |
| 826 | struct pmbus_sensor *sensor) |
| 827 | { |
| 828 | int exponent; |
| 829 | bool sign; |
| 830 | long val; |
| 831 | |
| 832 | /* only support half precision for now */ |
| 833 | sign = sensor->data & 0x8000; |
| 834 | exponent = (sensor->data >> 10) & 0x1f; |
| 835 | val = sensor->data & 0x3ff; |
| 836 | |
| 837 | if (exponent == 0) { /* subnormal */ |
| 838 | exponent = -(14 + 10); |
| 839 | } else if (exponent == 0x1f) { /* NaN, convert to min/max */ |
| 840 | exponent = 0; |
| 841 | val = 65504; |
| 842 | } else { |
| 843 | exponent -= (15 + 10); /* normal */ |
| 844 | val |= 0x400; |
| 845 | } |
| 846 | |
| 847 | /* scale result to milli-units for all sensors except fans */ |
| 848 | if (sensor->class != PSC_FAN) |
| 849 | val = val * 1000L; |
| 850 | |
| 851 | /* scale result to micro-units for power sensors */ |
| 852 | if (sensor->class == PSC_POWER) |
| 853 | val = val * 1000L; |
| 854 | |
| 855 | if (exponent >= 0) |
| 856 | val <<= exponent; |
| 857 | else |
| 858 | val >>= -exponent; |
| 859 | |
| 860 | if (sign) |
| 861 | val = -val; |
| 862 | |
| 863 | return val; |
| 864 | } |
| 865 | |
| 866 | /* |
| 867 | * Convert linear sensor values to milli- or micro-units |
| 868 | * depending on sensor type. |
| 869 | */ |
| 870 | static s64 pmbus_reg2data_linear(struct pmbus_data *data, |
| 871 | struct pmbus_sensor *sensor) |
| 872 | { |
| 873 | s16 exponent; |
| 874 | s32 mantissa; |
| 875 | s64 val; |
| 876 | |
| 877 | if (sensor->class == PSC_VOLTAGE_OUT) { /* LINEAR16 */ |
| 878 | exponent = data->exponent[sensor->page]; |
| 879 | mantissa = (u16)sensor->data; |
| 880 | } else { /* LINEAR11 */ |
| 881 | exponent = ((s16)sensor->data) >> 11; |
| 882 | mantissa = ((s16)((sensor->data & 0x7ff) << 5)) >> 5; |
| 883 | } |
| 884 | |
| 885 | val = mantissa; |
| 886 | |
| 887 | /* scale result to milli-units for all sensors except fans */ |
| 888 | if (sensor->class != PSC_FAN) |
| 889 | val = val * 1000LL; |
| 890 | |
| 891 | /* scale result to micro-units for power sensors */ |
| 892 | if (sensor->class == PSC_POWER) |
| 893 | val = val * 1000LL; |
| 894 | |
| 895 | if (exponent >= 0) |
| 896 | val <<= exponent; |
| 897 | else |
| 898 | val >>= -exponent; |
| 899 | |
| 900 | return val; |
| 901 | } |
| 902 | |
| 903 | /* |
| 904 | * Convert direct sensor values to milli- or micro-units |
| 905 | * depending on sensor type. |
| 906 | */ |
| 907 | static s64 pmbus_reg2data_direct(struct pmbus_data *data, |
| 908 | struct pmbus_sensor *sensor) |
| 909 | { |
| 910 | s64 b, val = (s16)sensor->data; |
| 911 | s32 m, R; |
| 912 | |
| 913 | m = data->info->m[sensor->class]; |
| 914 | b = data->info->b[sensor->class]; |
| 915 | R = data->info->R[sensor->class]; |
| 916 | |
| 917 | if (m == 0) |
| 918 | return 0; |
| 919 | |
| 920 | /* X = 1/m * (Y * 10^-R - b) */ |
| 921 | R = -R; |
| 922 | /* scale result to milli-units for everything but fans */ |
| 923 | if (!(sensor->class == PSC_FAN || sensor->class == PSC_PWM)) { |
| 924 | R += 3; |
| 925 | b *= 1000; |
| 926 | } |
| 927 | |
| 928 | /* scale result to micro-units for power sensors */ |
| 929 | if (sensor->class == PSC_POWER) { |
| 930 | R += 3; |
| 931 | b *= 1000; |
| 932 | } |
| 933 | |
| 934 | while (R > 0) { |
| 935 | val *= 10; |
| 936 | R--; |
| 937 | } |
| 938 | while (R < 0) { |
| 939 | val = div_s64(val + 5LL, 10L); /* round closest */ |
| 940 | R++; |
| 941 | } |
| 942 | |
| 943 | val = div_s64(val - b, m); |
| 944 | return val; |
| 945 | } |
| 946 | |
| 947 | /* |
| 948 | * Convert VID sensor values to milli- or micro-units |
| 949 | * depending on sensor type. |
| 950 | */ |
| 951 | static s64 pmbus_reg2data_vid(struct pmbus_data *data, |
| 952 | struct pmbus_sensor *sensor) |
| 953 | { |
| 954 | long val = sensor->data; |
| 955 | long rv = 0; |
| 956 | |
| 957 | switch (data->info->vrm_version[sensor->page]) { |
| 958 | case vr11: |
| 959 | if (val >= 0x02 && val <= 0xb2) |
| 960 | rv = DIV_ROUND_CLOSEST(160000 - (val - 2) * 625, 100); |
| 961 | break; |
| 962 | case vr12: |
| 963 | if (val >= 0x01) |
| 964 | rv = 250 + (val - 1) * 5; |
| 965 | break; |
| 966 | case vr13: |
| 967 | if (val >= 0x01) |
| 968 | rv = 500 + (val - 1) * 10; |
| 969 | break; |
| 970 | case imvp9: |
| 971 | if (val >= 0x01) |
| 972 | rv = 200 + (val - 1) * 10; |
| 973 | break; |
| 974 | case amd625mv: |
| 975 | if (val >= 0x0 && val <= 0xd8) |
| 976 | rv = DIV_ROUND_CLOSEST(155000 - val * 625, 100); |
| 977 | break; |
| 978 | case nvidia195mv: |
| 979 | if (val >= 0x01) |
| 980 | rv = 195 + (val - 1) * 5; /* VID step is 5mv */ |
| 981 | break; |
| 982 | } |
| 983 | return rv; |
| 984 | } |
| 985 | |
| 986 | static s64 pmbus_reg2data(struct pmbus_data *data, struct pmbus_sensor *sensor) |
| 987 | { |
| 988 | s64 val; |
| 989 | |
| 990 | if (!sensor->convert) |
| 991 | return sensor->data; |
| 992 | |
| 993 | switch (data->info->format[sensor->class]) { |
| 994 | case direct: |
| 995 | val = pmbus_reg2data_direct(data, sensor); |
| 996 | break; |
| 997 | case vid: |
| 998 | val = pmbus_reg2data_vid(data, sensor); |
| 999 | break; |
| 1000 | case ieee754: |
| 1001 | val = pmbus_reg2data_ieee754(data, sensor); |
| 1002 | break; |
| 1003 | case linear: |
| 1004 | default: |
| 1005 | val = pmbus_reg2data_linear(data, sensor); |
| 1006 | break; |
| 1007 | } |
| 1008 | return val; |
| 1009 | } |
| 1010 | |
| 1011 | #define MAX_IEEE_MANTISSA (0x7ff * 1000) |
| 1012 | #define MIN_IEEE_MANTISSA (0x400 * 1000) |
| 1013 | |
| 1014 | static u16 pmbus_data2reg_ieee754(struct pmbus_data *data, |
| 1015 | struct pmbus_sensor *sensor, long val) |
| 1016 | { |
| 1017 | u16 exponent = (15 + 10); |
| 1018 | long mantissa; |
| 1019 | u16 sign = 0; |
| 1020 | |
| 1021 | /* simple case */ |
| 1022 | if (val == 0) |
| 1023 | return 0; |
| 1024 | |
| 1025 | if (val < 0) { |
| 1026 | sign = 0x8000; |
| 1027 | val = -val; |
| 1028 | } |
| 1029 | |
| 1030 | /* Power is in uW. Convert to mW before converting. */ |
| 1031 | if (sensor->class == PSC_POWER) |
| 1032 | val = DIV_ROUND_CLOSEST(val, 1000L); |
| 1033 | |
| 1034 | /* |
| 1035 | * For simplicity, convert fan data to milli-units |
| 1036 | * before calculating the exponent. |
| 1037 | */ |
| 1038 | if (sensor->class == PSC_FAN) |
| 1039 | val = val * 1000; |
| 1040 | |
| 1041 | /* Reduce large mantissa until it fits into 10 bit */ |
| 1042 | while (val > MAX_IEEE_MANTISSA && exponent < 30) { |
| 1043 | exponent++; |
| 1044 | val >>= 1; |
| 1045 | } |
| 1046 | /* |
| 1047 | * Increase small mantissa to generate valid 'normal' |
| 1048 | * number |
| 1049 | */ |
| 1050 | while (val < MIN_IEEE_MANTISSA && exponent > 1) { |
| 1051 | exponent--; |
| 1052 | val <<= 1; |
| 1053 | } |
| 1054 | |
| 1055 | /* Convert mantissa from milli-units to units */ |
| 1056 | mantissa = DIV_ROUND_CLOSEST(val, 1000); |
| 1057 | |
| 1058 | /* |
| 1059 | * Ensure that the resulting number is within range. |
| 1060 | * Valid range is 0x400..0x7ff, where bit 10 reflects |
| 1061 | * the implied high bit in normalized ieee754 numbers. |
| 1062 | * Set the range to 0x400..0x7ff to reflect this. |
| 1063 | * The upper bit is then removed by the mask against |
| 1064 | * 0x3ff in the final assignment. |
| 1065 | */ |
| 1066 | if (mantissa > 0x7ff) |
| 1067 | mantissa = 0x7ff; |
| 1068 | else if (mantissa < 0x400) |
| 1069 | mantissa = 0x400; |
| 1070 | |
| 1071 | /* Convert to sign, 5 bit exponent, 10 bit mantissa */ |
| 1072 | return sign | (mantissa & 0x3ff) | ((exponent << 10) & 0x7c00); |
| 1073 | } |
| 1074 | |
| 1075 | #define MAX_LIN_MANTISSA (1023 * 1000) |
| 1076 | #define MIN_LIN_MANTISSA (511 * 1000) |
| 1077 | |
| 1078 | static u16 pmbus_data2reg_linear(struct pmbus_data *data, |
| 1079 | struct pmbus_sensor *sensor, s64 val) |
| 1080 | { |
| 1081 | s16 exponent = 0, mantissa; |
| 1082 | bool negative = false; |
| 1083 | |
| 1084 | /* simple case */ |
| 1085 | if (val == 0) |
| 1086 | return 0; |
| 1087 | |
| 1088 | if (sensor->class == PSC_VOLTAGE_OUT) { |
| 1089 | /* LINEAR16 does not support negative voltages */ |
| 1090 | if (val < 0) |
| 1091 | return 0; |
| 1092 | |
| 1093 | /* |
| 1094 | * For a static exponents, we don't have a choice |
| 1095 | * but to adjust the value to it. |
| 1096 | */ |
| 1097 | if (data->exponent[sensor->page] < 0) |
| 1098 | val <<= -data->exponent[sensor->page]; |
| 1099 | else |
| 1100 | val >>= data->exponent[sensor->page]; |
| 1101 | val = DIV_ROUND_CLOSEST_ULL(val, 1000); |
| 1102 | return clamp_val(val, 0, 0xffff); |
| 1103 | } |
| 1104 | |
| 1105 | if (val < 0) { |
| 1106 | negative = true; |
| 1107 | val = -val; |
| 1108 | } |
| 1109 | |
| 1110 | /* Power is in uW. Convert to mW before converting. */ |
| 1111 | if (sensor->class == PSC_POWER) |
| 1112 | val = DIV_ROUND_CLOSEST_ULL(val, 1000); |
| 1113 | |
| 1114 | /* |
| 1115 | * For simplicity, convert fan data to milli-units |
| 1116 | * before calculating the exponent. |
| 1117 | */ |
| 1118 | if (sensor->class == PSC_FAN) |
| 1119 | val = val * 1000LL; |
| 1120 | |
| 1121 | /* Reduce large mantissa until it fits into 10 bit */ |
| 1122 | while (val >= MAX_LIN_MANTISSA && exponent < 15) { |
| 1123 | exponent++; |
| 1124 | val >>= 1; |
| 1125 | } |
| 1126 | /* Increase small mantissa to improve precision */ |
| 1127 | while (val < MIN_LIN_MANTISSA && exponent > -15) { |
| 1128 | exponent--; |
| 1129 | val <<= 1; |
| 1130 | } |
| 1131 | |
| 1132 | /* Convert mantissa from milli-units to units */ |
| 1133 | mantissa = clamp_val(DIV_ROUND_CLOSEST_ULL(val, 1000), 0, 0x3ff); |
| 1134 | |
| 1135 | /* restore sign */ |
| 1136 | if (negative) |
| 1137 | mantissa = -mantissa; |
| 1138 | |
| 1139 | /* Convert to 5 bit exponent, 11 bit mantissa */ |
| 1140 | return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800); |
| 1141 | } |
| 1142 | |
| 1143 | static u16 pmbus_data2reg_direct(struct pmbus_data *data, |
| 1144 | struct pmbus_sensor *sensor, s64 val) |
| 1145 | { |
| 1146 | s64 b; |
| 1147 | s32 m, R; |
| 1148 | |
| 1149 | m = data->info->m[sensor->class]; |
| 1150 | b = data->info->b[sensor->class]; |
| 1151 | R = data->info->R[sensor->class]; |
| 1152 | |
| 1153 | /* Power is in uW. Adjust R and b. */ |
| 1154 | if (sensor->class == PSC_POWER) { |
| 1155 | R -= 3; |
| 1156 | b *= 1000; |
| 1157 | } |
| 1158 | |
| 1159 | /* Calculate Y = (m * X + b) * 10^R */ |
| 1160 | if (!(sensor->class == PSC_FAN || sensor->class == PSC_PWM)) { |
| 1161 | R -= 3; /* Adjust R and b for data in milli-units */ |
| 1162 | b *= 1000; |
| 1163 | } |
| 1164 | val = val * m + b; |
| 1165 | |
| 1166 | while (R > 0) { |
| 1167 | val *= 10; |
| 1168 | R--; |
| 1169 | } |
| 1170 | while (R < 0) { |
| 1171 | val = div_s64(val + 5LL, 10L); /* round closest */ |
| 1172 | R++; |
| 1173 | } |
| 1174 | |
| 1175 | return (u16)clamp_val(val, S16_MIN, S16_MAX); |
| 1176 | } |
| 1177 | |
| 1178 | static u16 pmbus_data2reg_vid(struct pmbus_data *data, |
| 1179 | struct pmbus_sensor *sensor, s64 val) |
| 1180 | { |
| 1181 | switch (data->info->vrm_version[sensor->page]) { |
| 1182 | case vr12: |
| 1183 | val = clamp_val(val, 250, 1520); |
| 1184 | return 1 + DIV_ROUND_CLOSEST_ULL(val - 250, 5); |
| 1185 | case vr13: |
| 1186 | val = clamp_val(val, 500, 3040); |
| 1187 | return 1 + DIV_ROUND_CLOSEST_ULL(val - 500, 10); |
| 1188 | case imvp9: |
| 1189 | val = clamp_val(val, 200, 2740); |
| 1190 | return 1 + DIV_ROUND_CLOSEST_ULL(val - 200, 10); |
| 1191 | case amd625mv: |
| 1192 | val = clamp_val(val, 200, 1550); |
| 1193 | return DIV_ROUND_CLOSEST_ULL((1550LL - val) * 100LL, 625); |
| 1194 | case nvidia195mv: |
| 1195 | val = clamp_val(val, 195, 1465); |
| 1196 | return 1 + DIV_ROUND_CLOSEST_ULL(val - 195, 5); |
| 1197 | case vr11: |
| 1198 | default: |
| 1199 | val = clamp_val(val, 500, 1600); |
| 1200 | return 2 + DIV_ROUND_CLOSEST_ULL((1600LL - val) * 100LL, 625); |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | static u16 pmbus_data2reg(struct pmbus_data *data, |
| 1205 | struct pmbus_sensor *sensor, s64 val) |
| 1206 | { |
| 1207 | u16 regval; |
| 1208 | |
| 1209 | if (!sensor->convert) |
| 1210 | return val; |
| 1211 | |
| 1212 | switch (data->info->format[sensor->class]) { |
| 1213 | case direct: |
| 1214 | regval = pmbus_data2reg_direct(data, sensor, val); |
| 1215 | break; |
| 1216 | case vid: |
| 1217 | regval = pmbus_data2reg_vid(data, sensor, val); |
| 1218 | break; |
| 1219 | case ieee754: |
| 1220 | regval = pmbus_data2reg_ieee754(data, sensor, val); |
| 1221 | break; |
| 1222 | case linear: |
| 1223 | default: |
| 1224 | regval = pmbus_data2reg_linear(data, sensor, val); |
| 1225 | break; |
| 1226 | } |
| 1227 | return regval; |
| 1228 | } |
| 1229 | |
| 1230 | /* |
| 1231 | * Return boolean calculated from converted data. |
| 1232 | * <index> defines a status register index and mask. |
| 1233 | * The mask is in the lower 8 bits, the register index is in bits 8..23. |
| 1234 | * |
| 1235 | * The associated pmbus_boolean structure contains optional pointers to two |
| 1236 | * sensor attributes. If specified, those attributes are compared against each |
| 1237 | * other to determine if a limit has been exceeded. |
| 1238 | * |
| 1239 | * If the sensor attribute pointers are NULL, the function returns true if |
| 1240 | * (status[reg] & mask) is true. |
| 1241 | * |
| 1242 | * If sensor attribute pointers are provided, a comparison against a specified |
| 1243 | * limit has to be performed to determine the boolean result. |
| 1244 | * In this case, the function returns true if v1 >= v2 (where v1 and v2 are |
| 1245 | * sensor values referenced by sensor attribute pointers s1 and s2). |
| 1246 | * |
| 1247 | * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>. |
| 1248 | * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>. |
| 1249 | * |
| 1250 | * If a negative value is stored in any of the referenced registers, this value |
| 1251 | * reflects an error code which will be returned. |
| 1252 | */ |
| 1253 | static int pmbus_get_boolean(struct i2c_client *client, struct pmbus_boolean *b, |
| 1254 | int index) |
| 1255 | { |
| 1256 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 1257 | struct pmbus_sensor *s1 = b->s1; |
| 1258 | struct pmbus_sensor *s2 = b->s2; |
| 1259 | u16 mask = pb_index_to_mask(index); |
| 1260 | u8 page = pb_index_to_page(index); |
| 1261 | u16 reg = pb_index_to_reg(index); |
| 1262 | int ret, status; |
| 1263 | u16 regval; |
| 1264 | |
| 1265 | guard(pmbus_lock)(client); |
| 1266 | |
| 1267 | status = pmbus_get_status(client, page, reg); |
| 1268 | if (status < 0) |
| 1269 | return status; |
| 1270 | |
| 1271 | if (s1) |
| 1272 | pmbus_update_sensor_data(client, s1); |
| 1273 | if (s2) |
| 1274 | pmbus_update_sensor_data(client, s2); |
| 1275 | |
| 1276 | regval = status & mask; |
| 1277 | if (regval) { |
| 1278 | /* Generic STATUS_WORD alarms are not individually clearable. */ |
| 1279 | if (data->revision >= PMBUS_REV_12 && |
| 1280 | reg != PMBUS_STATUS_WORD) { |
| 1281 | ret = _pmbus_write_byte_data(client, page, reg, regval); |
| 1282 | if (ret) |
| 1283 | return ret; |
| 1284 | } else { |
| 1285 | pmbus_clear_fault_page(client, page); |
| 1286 | } |
| 1287 | } |
| 1288 | if (s1 && s2) { |
| 1289 | s64 v1, v2; |
| 1290 | |
| 1291 | if (s1->data < 0) |
| 1292 | return s1->data; |
| 1293 | if (s2->data < 0) |
| 1294 | return s2->data; |
| 1295 | |
| 1296 | v1 = pmbus_reg2data(data, s1); |
| 1297 | v2 = pmbus_reg2data(data, s2); |
| 1298 | ret = !!(regval && v1 >= v2); |
| 1299 | } else { |
| 1300 | ret = !!regval; |
| 1301 | } |
| 1302 | return ret; |
| 1303 | } |
| 1304 | |
| 1305 | static ssize_t pmbus_show_boolean(struct device *dev, |
| 1306 | struct device_attribute *da, char *buf) |
| 1307 | { |
| 1308 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 1309 | struct pmbus_boolean *boolean = to_pmbus_boolean(attr); |
| 1310 | struct i2c_client *client = to_i2c_client(dev->parent); |
| 1311 | int val; |
| 1312 | |
| 1313 | val = pmbus_get_boolean(client, boolean, attr->index); |
| 1314 | if (val < 0) |
| 1315 | return val; |
| 1316 | return sysfs_emit(buf, "%d\n", val); |
| 1317 | } |
| 1318 | |
| 1319 | static ssize_t pmbus_show_zero(struct device *dev, |
| 1320 | struct device_attribute *devattr, char *buf) |
| 1321 | { |
| 1322 | return sysfs_emit(buf, "0\n"); |
| 1323 | } |
| 1324 | |
| 1325 | static ssize_t pmbus_show_sensor(struct device *dev, |
| 1326 | struct device_attribute *devattr, char *buf) |
| 1327 | { |
| 1328 | struct i2c_client *client = to_i2c_client(dev->parent); |
| 1329 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 1330 | struct pmbus_sensor *sensor = to_pmbus_sensor(attr); |
| 1331 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 1332 | s64 val; |
| 1333 | |
| 1334 | scoped_guard(pmbus_lock, client) { |
| 1335 | pmbus_update_sensor_data(client, sensor); |
| 1336 | if (sensor->data < 0) |
| 1337 | return sensor->data; |
| 1338 | val = pmbus_reg2data(data, sensor); |
| 1339 | } |
| 1340 | |
| 1341 | return sysfs_emit(buf, "%lld\n", val); |
| 1342 | } |
| 1343 | |
| 1344 | static ssize_t pmbus_set_sensor(struct device *dev, |
| 1345 | struct device_attribute *devattr, |
| 1346 | const char *buf, size_t count) |
| 1347 | { |
| 1348 | struct i2c_client *client = to_i2c_client(dev->parent); |
| 1349 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 1350 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 1351 | struct pmbus_sensor *sensor = to_pmbus_sensor(attr); |
| 1352 | s64 val; |
| 1353 | int ret; |
| 1354 | u16 regval; |
| 1355 | |
| 1356 | if (kstrtos64(buf, 10, &val) < 0) |
| 1357 | return -EINVAL; |
| 1358 | |
| 1359 | guard(pmbus_lock)(client); |
| 1360 | |
| 1361 | regval = pmbus_data2reg(data, sensor, val); |
| 1362 | ret = _pmbus_write_word_data(client, sensor->page, sensor->reg, regval); |
| 1363 | if (ret < 0) |
| 1364 | return ret; |
| 1365 | |
| 1366 | sensor->data = -ENODATA; |
| 1367 | return count; |
| 1368 | } |
| 1369 | |
| 1370 | static ssize_t pmbus_show_label(struct device *dev, |
| 1371 | struct device_attribute *da, char *buf) |
| 1372 | { |
| 1373 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 1374 | struct pmbus_label *label = to_pmbus_label(attr); |
| 1375 | |
| 1376 | return sysfs_emit(buf, "%s\n", label->label); |
| 1377 | } |
| 1378 | |
| 1379 | static int pmbus_add_attribute(struct pmbus_data *data, struct attribute *attr) |
| 1380 | { |
| 1381 | if (data->num_attributes >= data->max_attributes - 1) { |
| 1382 | int new_max_attrs = data->max_attributes + PMBUS_ATTR_ALLOC_SIZE; |
| 1383 | void *new_attrs = devm_krealloc_array(data->dev, data->group.attrs, |
| 1384 | new_max_attrs, sizeof(void *), |
| 1385 | GFP_KERNEL); |
| 1386 | if (!new_attrs) |
| 1387 | return -ENOMEM; |
| 1388 | data->group.attrs = new_attrs; |
| 1389 | data->max_attributes = new_max_attrs; |
| 1390 | } |
| 1391 | |
| 1392 | data->group.attrs[data->num_attributes++] = attr; |
| 1393 | data->group.attrs[data->num_attributes] = NULL; |
| 1394 | return 0; |
| 1395 | } |
| 1396 | |
| 1397 | static void pmbus_dev_attr_init(struct device_attribute *dev_attr, |
| 1398 | const char *name, |
| 1399 | umode_t mode, |
| 1400 | ssize_t (*show)(struct device *dev, |
| 1401 | struct device_attribute *attr, |
| 1402 | char *buf), |
| 1403 | ssize_t (*store)(struct device *dev, |
| 1404 | struct device_attribute *attr, |
| 1405 | const char *buf, size_t count)) |
| 1406 | { |
| 1407 | sysfs_attr_init(&dev_attr->attr); |
| 1408 | dev_attr->attr.name = name; |
| 1409 | dev_attr->attr.mode = mode; |
| 1410 | dev_attr->show = show; |
| 1411 | dev_attr->store = store; |
| 1412 | } |
| 1413 | |
| 1414 | static void pmbus_attr_init(struct sensor_device_attribute *a, |
| 1415 | const char *name, |
| 1416 | umode_t mode, |
| 1417 | ssize_t (*show)(struct device *dev, |
| 1418 | struct device_attribute *attr, |
| 1419 | char *buf), |
| 1420 | ssize_t (*store)(struct device *dev, |
| 1421 | struct device_attribute *attr, |
| 1422 | const char *buf, size_t count), |
| 1423 | int idx) |
| 1424 | { |
| 1425 | pmbus_dev_attr_init(&a->dev_attr, name, mode, show, store); |
| 1426 | a->index = idx; |
| 1427 | } |
| 1428 | |
| 1429 | static int pmbus_add_boolean(struct pmbus_data *data, |
| 1430 | const char *name, const char *type, int seq, |
| 1431 | struct pmbus_sensor *s1, |
| 1432 | struct pmbus_sensor *s2, |
| 1433 | u8 page, u16 reg, u16 mask) |
| 1434 | { |
| 1435 | struct pmbus_boolean *boolean; |
| 1436 | struct sensor_device_attribute *a; |
| 1437 | |
| 1438 | if (WARN((s1 && !s2) || (!s1 && s2), "Bad s1/s2 parameters\n")) |
| 1439 | return -EINVAL; |
| 1440 | |
| 1441 | boolean = devm_kzalloc(data->dev, sizeof(*boolean), GFP_KERNEL); |
| 1442 | if (!boolean) |
| 1443 | return -ENOMEM; |
| 1444 | |
| 1445 | a = &boolean->attribute; |
| 1446 | |
| 1447 | snprintf(boolean->name, sizeof(boolean->name), "%s%d_%s", |
| 1448 | name, seq, type); |
| 1449 | boolean->s1 = s1; |
| 1450 | boolean->s2 = s2; |
| 1451 | pmbus_attr_init(a, boolean->name, 0444, pmbus_show_boolean, NULL, |
| 1452 | pb_reg_to_index(page, reg, mask)); |
| 1453 | |
| 1454 | return pmbus_add_attribute(data, &a->dev_attr.attr); |
| 1455 | } |
| 1456 | |
| 1457 | /* of thermal for pmbus temperature sensors */ |
| 1458 | struct pmbus_thermal_data { |
| 1459 | struct pmbus_data *pmbus_data; |
| 1460 | struct pmbus_sensor *sensor; |
| 1461 | }; |
| 1462 | |
| 1463 | static int pmbus_thermal_get_temp(struct thermal_zone_device *tz, int *temp) |
| 1464 | { |
| 1465 | struct pmbus_thermal_data *tdata = thermal_zone_device_priv(tz); |
| 1466 | struct pmbus_sensor *sensor = tdata->sensor; |
| 1467 | struct pmbus_data *pmbus_data = tdata->pmbus_data; |
| 1468 | struct i2c_client *client = to_i2c_client(pmbus_data->dev); |
| 1469 | struct device *dev = pmbus_data->hwmon_dev; |
| 1470 | int _temp; |
| 1471 | |
| 1472 | if (!dev) { |
| 1473 | /* May not even get to hwmon yet */ |
| 1474 | *temp = 0; |
| 1475 | return 0; |
| 1476 | } |
| 1477 | |
| 1478 | scoped_guard(pmbus_lock, client) { |
| 1479 | pmbus_update_sensor_data(client, sensor); |
| 1480 | if (sensor->data < 0) |
| 1481 | return sensor->data; |
| 1482 | _temp = (int)pmbus_reg2data(pmbus_data, sensor); |
| 1483 | } |
| 1484 | |
| 1485 | *temp = _temp; |
| 1486 | return 0; |
| 1487 | } |
| 1488 | |
| 1489 | static const struct thermal_zone_device_ops pmbus_thermal_ops = { |
| 1490 | .get_temp = pmbus_thermal_get_temp, |
| 1491 | }; |
| 1492 | |
| 1493 | static int pmbus_thermal_add_sensor(struct pmbus_data *pmbus_data, |
| 1494 | struct pmbus_sensor *sensor, int index) |
| 1495 | { |
| 1496 | struct device *dev = pmbus_data->dev; |
| 1497 | struct pmbus_thermal_data *tdata; |
| 1498 | struct thermal_zone_device *tzd; |
| 1499 | |
| 1500 | tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL); |
| 1501 | if (!tdata) |
| 1502 | return -ENOMEM; |
| 1503 | |
| 1504 | tdata->sensor = sensor; |
| 1505 | tdata->pmbus_data = pmbus_data; |
| 1506 | |
| 1507 | tzd = devm_thermal_of_zone_register(dev, index, tdata, |
| 1508 | &pmbus_thermal_ops); |
| 1509 | /* |
| 1510 | * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV, |
| 1511 | * so ignore that error but forward any other error. |
| 1512 | */ |
| 1513 | if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV)) |
| 1514 | return PTR_ERR(tzd); |
| 1515 | |
| 1516 | return 0; |
| 1517 | } |
| 1518 | |
| 1519 | static struct pmbus_sensor *pmbus_add_sensor(struct pmbus_data *data, |
| 1520 | const char *name, const char *type, |
| 1521 | int seq, int page, int phase, |
| 1522 | int reg, |
| 1523 | enum pmbus_sensor_classes class, |
| 1524 | bool update, bool readonly, |
| 1525 | bool writeonly, bool convert) |
| 1526 | { |
| 1527 | struct sensor_device_attribute *a; |
| 1528 | struct pmbus_sensor *sensor; |
| 1529 | |
| 1530 | sensor = devm_kzalloc(data->dev, sizeof(*sensor), GFP_KERNEL); |
| 1531 | if (!sensor) |
| 1532 | return NULL; |
| 1533 | a = &sensor->attribute; |
| 1534 | |
| 1535 | if (type) |
| 1536 | snprintf(sensor->name, sizeof(sensor->name), "%s%d_%s", |
| 1537 | name, seq, type); |
| 1538 | else |
| 1539 | snprintf(sensor->name, sizeof(sensor->name), "%s%d", |
| 1540 | name, seq); |
| 1541 | |
| 1542 | if (data->flags & PMBUS_WRITE_PROTECTED) |
| 1543 | readonly = true; |
| 1544 | |
| 1545 | sensor->page = page; |
| 1546 | sensor->phase = phase; |
| 1547 | sensor->reg = reg; |
| 1548 | sensor->class = class; |
| 1549 | sensor->update = update; |
| 1550 | sensor->convert = convert; |
| 1551 | sensor->data = -ENODATA; |
| 1552 | pmbus_attr_init(a, sensor->name, readonly ? 0444 : 0644, |
| 1553 | writeonly ? pmbus_show_zero : pmbus_show_sensor, |
| 1554 | pmbus_set_sensor, -1); |
| 1555 | |
| 1556 | if (pmbus_add_attribute(data, &a->dev_attr.attr)) |
| 1557 | return NULL; |
| 1558 | |
| 1559 | sensor->next = data->sensors; |
| 1560 | data->sensors = sensor; |
| 1561 | |
| 1562 | /* temperature sensors with _input values are registered with thermal */ |
| 1563 | if (class == PSC_TEMPERATURE && strcmp(type, "input") == 0) |
| 1564 | pmbus_thermal_add_sensor(data, sensor, seq); |
| 1565 | |
| 1566 | return sensor; |
| 1567 | } |
| 1568 | |
| 1569 | static int pmbus_add_label(struct pmbus_data *data, |
| 1570 | const char *name, int seq, |
| 1571 | const char *lstring, int index, int phase) |
| 1572 | { |
| 1573 | struct sensor_device_attribute *a; |
| 1574 | struct pmbus_label *label; |
| 1575 | |
| 1576 | label = devm_kzalloc(data->dev, sizeof(*label), GFP_KERNEL); |
| 1577 | if (!label) |
| 1578 | return -ENOMEM; |
| 1579 | |
| 1580 | a = &label->attribute; |
| 1581 | |
| 1582 | snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq); |
| 1583 | if (!index) { |
| 1584 | if (phase == 0xff) |
| 1585 | strscpy(label->label, lstring); |
| 1586 | else |
| 1587 | snprintf(label->label, sizeof(label->label), "%s.%d", |
| 1588 | lstring, phase); |
| 1589 | } else { |
| 1590 | if (phase == 0xff) |
| 1591 | snprintf(label->label, sizeof(label->label), "%s%d", |
| 1592 | lstring, index); |
| 1593 | else |
| 1594 | snprintf(label->label, sizeof(label->label), "%s%d.%d", |
| 1595 | lstring, index, phase); |
| 1596 | } |
| 1597 | |
| 1598 | pmbus_attr_init(a, label->name, 0444, pmbus_show_label, NULL, -1); |
| 1599 | return pmbus_add_attribute(data, &a->dev_attr.attr); |
| 1600 | } |
| 1601 | |
| 1602 | /* |
| 1603 | * Search for attributes. Allocate sensors, booleans, and labels as needed. |
| 1604 | */ |
| 1605 | |
| 1606 | /* |
| 1607 | * The pmbus_limit_attr structure describes a single limit attribute |
| 1608 | * and its associated alarm attribute. |
| 1609 | */ |
| 1610 | struct pmbus_limit_attr { |
| 1611 | u16 reg; /* Limit register */ |
| 1612 | u16 sbit; /* Alarm attribute status bit */ |
| 1613 | bool readonly:1; /* True if the attribute is read-only */ |
| 1614 | bool writeonly:1; /* True if the attribute is write-only */ |
| 1615 | bool update:1; /* True if register needs updates */ |
| 1616 | bool low:1; /* True if low limit; for limits with compare functions only */ |
| 1617 | const char *attr; /* Attribute name */ |
| 1618 | const char *alarm; /* Alarm attribute name */ |
| 1619 | }; |
| 1620 | |
| 1621 | /* |
| 1622 | * The pmbus_sensor_attr structure describes one sensor attribute. This |
| 1623 | * description includes a reference to the associated limit attributes. |
| 1624 | */ |
| 1625 | struct pmbus_sensor_attr { |
| 1626 | u16 reg; /* sensor register */ |
| 1627 | u16 gbit; /* generic status bit */ |
| 1628 | u8 nlimit; /* # of limit registers */ |
| 1629 | enum pmbus_sensor_classes class;/* sensor class */ |
| 1630 | const char *label; /* sensor label */ |
| 1631 | bool paged:1; /* true if paged sensor */ |
| 1632 | bool update:1; /* true if update needed */ |
| 1633 | bool compare:1; /* true if compare function needed */ |
| 1634 | u32 func; /* sensor mask */ |
| 1635 | u32 sfunc; /* sensor status mask */ |
| 1636 | int sreg; /* status register */ |
| 1637 | const struct pmbus_limit_attr *limit;/* limit registers */ |
| 1638 | }; |
| 1639 | |
| 1640 | /* |
| 1641 | * Add a set of limit attributes and, if supported, the associated |
| 1642 | * alarm attributes. |
| 1643 | * returns 0 if no alarm register found, 1 if an alarm register was found, |
| 1644 | * < 0 on errors. |
| 1645 | */ |
| 1646 | static int pmbus_add_limit_attrs(struct i2c_client *client, |
| 1647 | struct pmbus_data *data, |
| 1648 | const struct pmbus_driver_info *info, |
| 1649 | const char *name, int index, int page, |
| 1650 | struct pmbus_sensor *base, |
| 1651 | const struct pmbus_sensor_attr *attr) |
| 1652 | { |
| 1653 | const struct pmbus_limit_attr *l = attr->limit; |
| 1654 | int nlimit = attr->nlimit; |
| 1655 | int have_alarm = 0; |
| 1656 | int i, ret; |
| 1657 | struct pmbus_sensor *curr; |
| 1658 | |
| 1659 | for (i = 0; i < nlimit; i++) { |
| 1660 | if (pmbus_check_word_register(client, page, l->reg)) { |
| 1661 | curr = pmbus_add_sensor(data, name, l->attr, index, |
| 1662 | page, 0xff, l->reg, attr->class, |
| 1663 | attr->update || l->update, |
| 1664 | l->readonly, l->writeonly, true); |
| 1665 | if (!curr) |
| 1666 | return -ENOMEM; |
| 1667 | if (l->sbit && (info->func[page] & attr->sfunc)) { |
| 1668 | ret = pmbus_add_boolean(data, name, |
| 1669 | l->alarm, index, |
| 1670 | attr->compare ? l->low ? curr : base |
| 1671 | : NULL, |
| 1672 | attr->compare ? l->low ? base : curr |
| 1673 | : NULL, |
| 1674 | page, attr->sreg, l->sbit); |
| 1675 | if (ret) |
| 1676 | return ret; |
| 1677 | have_alarm = 1; |
| 1678 | } |
| 1679 | } |
| 1680 | l++; |
| 1681 | } |
| 1682 | return have_alarm; |
| 1683 | } |
| 1684 | |
| 1685 | static int pmbus_add_sensor_attrs_one(struct i2c_client *client, |
| 1686 | struct pmbus_data *data, |
| 1687 | const struct pmbus_driver_info *info, |
| 1688 | const char *name, |
| 1689 | int index, int page, int phase, |
| 1690 | const struct pmbus_sensor_attr *attr, |
| 1691 | bool paged) |
| 1692 | { |
| 1693 | struct pmbus_sensor *base; |
| 1694 | bool upper = !!(attr->gbit & 0xff00); /* need to check STATUS_WORD */ |
| 1695 | int ret; |
| 1696 | |
| 1697 | if (attr->label) { |
| 1698 | ret = pmbus_add_label(data, name, index, attr->label, |
| 1699 | paged ? page + 1 : 0, phase); |
| 1700 | if (ret) |
| 1701 | return ret; |
| 1702 | } |
| 1703 | base = pmbus_add_sensor(data, name, "input", index, page, phase, |
| 1704 | attr->reg, attr->class, true, true, false, true); |
| 1705 | if (!base) |
| 1706 | return -ENOMEM; |
| 1707 | /* No limit and alarm attributes for phase specific sensors */ |
| 1708 | if (attr->sfunc && phase == 0xff) { |
| 1709 | ret = pmbus_add_limit_attrs(client, data, info, name, |
| 1710 | index, page, base, attr); |
| 1711 | if (ret < 0) |
| 1712 | return ret; |
| 1713 | /* |
| 1714 | * Add generic alarm attribute only if there are no individual |
| 1715 | * alarm attributes, if there is a global alarm bit, and if |
| 1716 | * the generic status register (word or byte, depending on |
| 1717 | * which global bit is set) for this page is accessible. |
| 1718 | */ |
| 1719 | if (!ret && attr->gbit && |
| 1720 | (!upper || data->has_status_word) && |
| 1721 | pmbus_check_status_register(client, page)) { |
| 1722 | ret = pmbus_add_boolean(data, name, "alarm", index, |
| 1723 | NULL, NULL, |
| 1724 | page, PMBUS_STATUS_WORD, |
| 1725 | attr->gbit); |
| 1726 | if (ret) |
| 1727 | return ret; |
| 1728 | } |
| 1729 | } |
| 1730 | return 0; |
| 1731 | } |
| 1732 | |
| 1733 | static bool pmbus_sensor_is_paged(const struct pmbus_driver_info *info, |
| 1734 | const struct pmbus_sensor_attr *attr) |
| 1735 | { |
| 1736 | int p; |
| 1737 | |
| 1738 | if (attr->paged) |
| 1739 | return true; |
| 1740 | |
| 1741 | /* |
| 1742 | * Some attributes may be present on more than one page despite |
| 1743 | * not being marked with the paged attribute. If that is the case, |
| 1744 | * then treat the sensor as being paged and add the page suffix to the |
| 1745 | * attribute name. |
| 1746 | * We don't just add the paged attribute to all such attributes, in |
| 1747 | * order to maintain the un-suffixed labels in the case where the |
| 1748 | * attribute is only on page 0. |
| 1749 | */ |
| 1750 | for (p = 1; p < info->pages; p++) { |
| 1751 | if (info->func[p] & attr->func) |
| 1752 | return true; |
| 1753 | } |
| 1754 | return false; |
| 1755 | } |
| 1756 | |
| 1757 | static int pmbus_add_sensor_attrs(struct i2c_client *client, |
| 1758 | struct pmbus_data *data, |
| 1759 | const char *name, |
| 1760 | const struct pmbus_sensor_attr *attrs, |
| 1761 | int nattrs) |
| 1762 | { |
| 1763 | const struct pmbus_driver_info *info = data->info; |
| 1764 | int index, i; |
| 1765 | int ret; |
| 1766 | |
| 1767 | index = 1; |
| 1768 | for (i = 0; i < nattrs; i++) { |
| 1769 | int page, pages; |
| 1770 | bool paged = pmbus_sensor_is_paged(info, attrs); |
| 1771 | |
| 1772 | pages = paged ? info->pages : 1; |
| 1773 | for (page = 0; page < pages; page++) { |
| 1774 | if (info->func[page] & attrs->func) { |
| 1775 | ret = pmbus_add_sensor_attrs_one(client, data, info, |
| 1776 | name, index, page, |
| 1777 | 0xff, attrs, paged); |
| 1778 | if (ret) |
| 1779 | return ret; |
| 1780 | index++; |
| 1781 | } |
| 1782 | if (info->phases[page]) { |
| 1783 | int phase; |
| 1784 | |
| 1785 | for (phase = 0; phase < info->phases[page]; |
| 1786 | phase++) { |
| 1787 | if (!(info->pfunc[phase] & attrs->func)) |
| 1788 | continue; |
| 1789 | ret = pmbus_add_sensor_attrs_one(client, |
| 1790 | data, info, name, index, page, |
| 1791 | phase, attrs, paged); |
| 1792 | if (ret) |
| 1793 | return ret; |
| 1794 | index++; |
| 1795 | } |
| 1796 | } |
| 1797 | } |
| 1798 | attrs++; |
| 1799 | } |
| 1800 | return 0; |
| 1801 | } |
| 1802 | |
| 1803 | static const struct pmbus_limit_attr vin_limit_attrs[] = { |
| 1804 | { |
| 1805 | .reg = PMBUS_VIN_UV_WARN_LIMIT, |
| 1806 | .attr = "min", |
| 1807 | .alarm = "min_alarm", |
| 1808 | .sbit = PB_VOLTAGE_UV_WARNING, |
| 1809 | }, { |
| 1810 | .reg = PMBUS_VIN_UV_FAULT_LIMIT, |
| 1811 | .attr = "lcrit", |
| 1812 | .alarm = "lcrit_alarm", |
| 1813 | .sbit = PB_VOLTAGE_UV_FAULT | PB_VOLTAGE_VIN_OFF, |
| 1814 | }, { |
| 1815 | .reg = PMBUS_VIN_OV_WARN_LIMIT, |
| 1816 | .attr = "max", |
| 1817 | .alarm = "max_alarm", |
| 1818 | .sbit = PB_VOLTAGE_OV_WARNING, |
| 1819 | }, { |
| 1820 | .reg = PMBUS_VIN_OV_FAULT_LIMIT, |
| 1821 | .attr = "crit", |
| 1822 | .alarm = "crit_alarm", |
| 1823 | .sbit = PB_VOLTAGE_OV_FAULT, |
| 1824 | }, { |
| 1825 | .reg = PMBUS_VIRT_READ_VIN_AVG, |
| 1826 | .update = true, |
| 1827 | .readonly = true, |
| 1828 | .attr = "average", |
| 1829 | }, { |
| 1830 | .reg = PMBUS_VIRT_READ_VIN_MIN, |
| 1831 | .update = true, |
| 1832 | .readonly = true, |
| 1833 | .attr = "lowest", |
| 1834 | }, { |
| 1835 | .reg = PMBUS_VIRT_READ_VIN_MAX, |
| 1836 | .update = true, |
| 1837 | .readonly = true, |
| 1838 | .attr = "highest", |
| 1839 | }, { |
| 1840 | .reg = PMBUS_VIRT_RESET_VIN_HISTORY, |
| 1841 | .writeonly = true, |
| 1842 | .attr = "reset_history", |
| 1843 | }, { |
| 1844 | .reg = PMBUS_MFR_VIN_MIN, |
| 1845 | .readonly = true, |
| 1846 | .attr = "rated_min", |
| 1847 | }, { |
| 1848 | .reg = PMBUS_MFR_VIN_MAX, |
| 1849 | .readonly = true, |
| 1850 | .attr = "rated_max", |
| 1851 | }, |
| 1852 | }; |
| 1853 | |
| 1854 | static const struct pmbus_limit_attr vmon_limit_attrs[] = { |
| 1855 | { |
| 1856 | .reg = PMBUS_VIRT_VMON_UV_WARN_LIMIT, |
| 1857 | .attr = "min", |
| 1858 | .alarm = "min_alarm", |
| 1859 | .sbit = PB_VOLTAGE_UV_WARNING, |
| 1860 | }, { |
| 1861 | .reg = PMBUS_VIRT_VMON_UV_FAULT_LIMIT, |
| 1862 | .attr = "lcrit", |
| 1863 | .alarm = "lcrit_alarm", |
| 1864 | .sbit = PB_VOLTAGE_UV_FAULT, |
| 1865 | }, { |
| 1866 | .reg = PMBUS_VIRT_VMON_OV_WARN_LIMIT, |
| 1867 | .attr = "max", |
| 1868 | .alarm = "max_alarm", |
| 1869 | .sbit = PB_VOLTAGE_OV_WARNING, |
| 1870 | }, { |
| 1871 | .reg = PMBUS_VIRT_VMON_OV_FAULT_LIMIT, |
| 1872 | .attr = "crit", |
| 1873 | .alarm = "crit_alarm", |
| 1874 | .sbit = PB_VOLTAGE_OV_FAULT, |
| 1875 | } |
| 1876 | }; |
| 1877 | |
| 1878 | static const struct pmbus_limit_attr vout_limit_attrs[] = { |
| 1879 | { |
| 1880 | .reg = PMBUS_VOUT_UV_WARN_LIMIT, |
| 1881 | .attr = "min", |
| 1882 | .alarm = "min_alarm", |
| 1883 | .sbit = PB_VOLTAGE_UV_WARNING, |
| 1884 | }, { |
| 1885 | .reg = PMBUS_VOUT_UV_FAULT_LIMIT, |
| 1886 | .attr = "lcrit", |
| 1887 | .alarm = "lcrit_alarm", |
| 1888 | .sbit = PB_VOLTAGE_UV_FAULT, |
| 1889 | }, { |
| 1890 | .reg = PMBUS_VOUT_OV_WARN_LIMIT, |
| 1891 | .attr = "max", |
| 1892 | .alarm = "max_alarm", |
| 1893 | .sbit = PB_VOLTAGE_OV_WARNING, |
| 1894 | }, { |
| 1895 | .reg = PMBUS_VOUT_OV_FAULT_LIMIT, |
| 1896 | .attr = "crit", |
| 1897 | .alarm = "crit_alarm", |
| 1898 | .sbit = PB_VOLTAGE_OV_FAULT, |
| 1899 | }, { |
| 1900 | .reg = PMBUS_VIRT_READ_VOUT_AVG, |
| 1901 | .update = true, |
| 1902 | .readonly = true, |
| 1903 | .attr = "average", |
| 1904 | }, { |
| 1905 | .reg = PMBUS_VIRT_READ_VOUT_MIN, |
| 1906 | .update = true, |
| 1907 | .readonly = true, |
| 1908 | .attr = "lowest", |
| 1909 | }, { |
| 1910 | .reg = PMBUS_VIRT_READ_VOUT_MAX, |
| 1911 | .update = true, |
| 1912 | .readonly = true, |
| 1913 | .attr = "highest", |
| 1914 | }, { |
| 1915 | .reg = PMBUS_VIRT_RESET_VOUT_HISTORY, |
| 1916 | .writeonly = true, |
| 1917 | .attr = "reset_history", |
| 1918 | }, { |
| 1919 | .reg = PMBUS_MFR_VOUT_MIN, |
| 1920 | .readonly = true, |
| 1921 | .attr = "rated_min", |
| 1922 | }, { |
| 1923 | .reg = PMBUS_MFR_VOUT_MAX, |
| 1924 | .readonly = true, |
| 1925 | .attr = "rated_max", |
| 1926 | }, |
| 1927 | }; |
| 1928 | |
| 1929 | static const struct pmbus_sensor_attr voltage_attributes[] = { |
| 1930 | { |
| 1931 | .reg = PMBUS_READ_VIN, |
| 1932 | .class = PSC_VOLTAGE_IN, |
| 1933 | .label = "vin", |
| 1934 | .func = PMBUS_HAVE_VIN, |
| 1935 | .sfunc = PMBUS_HAVE_STATUS_INPUT, |
| 1936 | .sreg = PMBUS_STATUS_INPUT, |
| 1937 | .gbit = PB_STATUS_VIN_UV, |
| 1938 | .limit = vin_limit_attrs, |
| 1939 | .nlimit = ARRAY_SIZE(vin_limit_attrs), |
| 1940 | }, { |
| 1941 | .reg = PMBUS_VIRT_READ_VMON, |
| 1942 | .class = PSC_VOLTAGE_IN, |
| 1943 | .label = "vmon", |
| 1944 | .func = PMBUS_HAVE_VMON, |
| 1945 | .sfunc = PMBUS_HAVE_STATUS_VMON, |
| 1946 | .sreg = PMBUS_VIRT_STATUS_VMON, |
| 1947 | .limit = vmon_limit_attrs, |
| 1948 | .nlimit = ARRAY_SIZE(vmon_limit_attrs), |
| 1949 | }, { |
| 1950 | .reg = PMBUS_READ_VCAP, |
| 1951 | .class = PSC_VOLTAGE_IN, |
| 1952 | .label = "vcap", |
| 1953 | .func = PMBUS_HAVE_VCAP, |
| 1954 | }, { |
| 1955 | .reg = PMBUS_READ_VOUT, |
| 1956 | .class = PSC_VOLTAGE_OUT, |
| 1957 | .label = "vout", |
| 1958 | .paged = true, |
| 1959 | .func = PMBUS_HAVE_VOUT, |
| 1960 | .sfunc = PMBUS_HAVE_STATUS_VOUT, |
| 1961 | .sreg = PMBUS_STATUS_VOUT, |
| 1962 | .gbit = PB_STATUS_VOUT_OV, |
| 1963 | .limit = vout_limit_attrs, |
| 1964 | .nlimit = ARRAY_SIZE(vout_limit_attrs), |
| 1965 | } |
| 1966 | }; |
| 1967 | |
| 1968 | /* Current attributes */ |
| 1969 | |
| 1970 | static const struct pmbus_limit_attr iin_limit_attrs[] = { |
| 1971 | { |
| 1972 | .reg = PMBUS_IIN_OC_WARN_LIMIT, |
| 1973 | .attr = "max", |
| 1974 | .alarm = "max_alarm", |
| 1975 | .sbit = PB_IIN_OC_WARNING, |
| 1976 | }, { |
| 1977 | .reg = PMBUS_IIN_OC_FAULT_LIMIT, |
| 1978 | .attr = "crit", |
| 1979 | .alarm = "crit_alarm", |
| 1980 | .sbit = PB_IIN_OC_FAULT, |
| 1981 | }, { |
| 1982 | .reg = PMBUS_VIRT_READ_IIN_AVG, |
| 1983 | .update = true, |
| 1984 | .readonly = true, |
| 1985 | .attr = "average", |
| 1986 | }, { |
| 1987 | .reg = PMBUS_VIRT_READ_IIN_MIN, |
| 1988 | .update = true, |
| 1989 | .readonly = true, |
| 1990 | .attr = "lowest", |
| 1991 | }, { |
| 1992 | .reg = PMBUS_VIRT_READ_IIN_MAX, |
| 1993 | .update = true, |
| 1994 | .readonly = true, |
| 1995 | .attr = "highest", |
| 1996 | }, { |
| 1997 | .reg = PMBUS_VIRT_RESET_IIN_HISTORY, |
| 1998 | .writeonly = true, |
| 1999 | .attr = "reset_history", |
| 2000 | }, { |
| 2001 | .reg = PMBUS_MFR_IIN_MAX, |
| 2002 | .readonly = true, |
| 2003 | .attr = "rated_max", |
| 2004 | }, |
| 2005 | }; |
| 2006 | |
| 2007 | static const struct pmbus_limit_attr iout_limit_attrs[] = { |
| 2008 | { |
| 2009 | .reg = PMBUS_IOUT_OC_WARN_LIMIT, |
| 2010 | .attr = "max", |
| 2011 | .alarm = "max_alarm", |
| 2012 | .sbit = PB_IOUT_OC_WARNING, |
| 2013 | }, { |
| 2014 | .reg = PMBUS_IOUT_UC_FAULT_LIMIT, |
| 2015 | .attr = "lcrit", |
| 2016 | .alarm = "lcrit_alarm", |
| 2017 | .sbit = PB_IOUT_UC_FAULT, |
| 2018 | }, { |
| 2019 | .reg = PMBUS_IOUT_OC_FAULT_LIMIT, |
| 2020 | .attr = "crit", |
| 2021 | .alarm = "crit_alarm", |
| 2022 | .sbit = PB_IOUT_OC_FAULT, |
| 2023 | }, { |
| 2024 | .reg = PMBUS_VIRT_READ_IOUT_AVG, |
| 2025 | .update = true, |
| 2026 | .readonly = true, |
| 2027 | .attr = "average", |
| 2028 | }, { |
| 2029 | .reg = PMBUS_VIRT_READ_IOUT_MIN, |
| 2030 | .update = true, |
| 2031 | .readonly = true, |
| 2032 | .attr = "lowest", |
| 2033 | }, { |
| 2034 | .reg = PMBUS_VIRT_READ_IOUT_MAX, |
| 2035 | .update = true, |
| 2036 | .readonly = true, |
| 2037 | .attr = "highest", |
| 2038 | }, { |
| 2039 | .reg = PMBUS_VIRT_RESET_IOUT_HISTORY, |
| 2040 | .writeonly = true, |
| 2041 | .attr = "reset_history", |
| 2042 | }, { |
| 2043 | .reg = PMBUS_MFR_IOUT_MAX, |
| 2044 | .readonly = true, |
| 2045 | .attr = "rated_max", |
| 2046 | }, |
| 2047 | }; |
| 2048 | |
| 2049 | static const struct pmbus_sensor_attr current_attributes[] = { |
| 2050 | { |
| 2051 | .reg = PMBUS_READ_IIN, |
| 2052 | .class = PSC_CURRENT_IN, |
| 2053 | .label = "iin", |
| 2054 | .func = PMBUS_HAVE_IIN, |
| 2055 | .sfunc = PMBUS_HAVE_STATUS_INPUT, |
| 2056 | .sreg = PMBUS_STATUS_INPUT, |
| 2057 | .gbit = PB_STATUS_INPUT, |
| 2058 | .limit = iin_limit_attrs, |
| 2059 | .nlimit = ARRAY_SIZE(iin_limit_attrs), |
| 2060 | }, { |
| 2061 | .reg = PMBUS_READ_IOUT, |
| 2062 | .class = PSC_CURRENT_OUT, |
| 2063 | .label = "iout", |
| 2064 | .paged = true, |
| 2065 | .func = PMBUS_HAVE_IOUT, |
| 2066 | .sfunc = PMBUS_HAVE_STATUS_IOUT, |
| 2067 | .sreg = PMBUS_STATUS_IOUT, |
| 2068 | .gbit = PB_STATUS_IOUT_OC, |
| 2069 | .limit = iout_limit_attrs, |
| 2070 | .nlimit = ARRAY_SIZE(iout_limit_attrs), |
| 2071 | } |
| 2072 | }; |
| 2073 | |
| 2074 | /* Power attributes */ |
| 2075 | |
| 2076 | static const struct pmbus_limit_attr pin_limit_attrs[] = { |
| 2077 | { |
| 2078 | .reg = PMBUS_PIN_OP_WARN_LIMIT, |
| 2079 | .attr = "max", |
| 2080 | .alarm = "alarm", |
| 2081 | .sbit = PB_PIN_OP_WARNING, |
| 2082 | }, { |
| 2083 | .reg = PMBUS_VIRT_READ_PIN_AVG, |
| 2084 | .update = true, |
| 2085 | .readonly = true, |
| 2086 | .attr = "average", |
| 2087 | }, { |
| 2088 | .reg = PMBUS_VIRT_READ_PIN_MIN, |
| 2089 | .update = true, |
| 2090 | .readonly = true, |
| 2091 | .attr = "input_lowest", |
| 2092 | }, { |
| 2093 | .reg = PMBUS_VIRT_READ_PIN_MAX, |
| 2094 | .update = true, |
| 2095 | .readonly = true, |
| 2096 | .attr = "input_highest", |
| 2097 | }, { |
| 2098 | .reg = PMBUS_VIRT_RESET_PIN_HISTORY, |
| 2099 | .writeonly = true, |
| 2100 | .attr = "reset_history", |
| 2101 | }, { |
| 2102 | .reg = PMBUS_MFR_PIN_MAX, |
| 2103 | .readonly = true, |
| 2104 | .attr = "rated_max", |
| 2105 | }, |
| 2106 | }; |
| 2107 | |
| 2108 | static const struct pmbus_limit_attr pout_limit_attrs[] = { |
| 2109 | { |
| 2110 | .reg = PMBUS_POUT_MAX, |
| 2111 | .attr = "cap", |
| 2112 | .alarm = "cap_alarm", |
| 2113 | .sbit = PB_POWER_LIMITING, |
| 2114 | }, { |
| 2115 | .reg = PMBUS_POUT_OP_WARN_LIMIT, |
| 2116 | .attr = "max", |
| 2117 | .alarm = "max_alarm", |
| 2118 | .sbit = PB_POUT_OP_WARNING, |
| 2119 | }, { |
| 2120 | .reg = PMBUS_POUT_OP_FAULT_LIMIT, |
| 2121 | .attr = "crit", |
| 2122 | .alarm = "crit_alarm", |
| 2123 | .sbit = PB_POUT_OP_FAULT, |
| 2124 | }, { |
| 2125 | .reg = PMBUS_VIRT_READ_POUT_AVG, |
| 2126 | .update = true, |
| 2127 | .readonly = true, |
| 2128 | .attr = "average", |
| 2129 | }, { |
| 2130 | .reg = PMBUS_VIRT_READ_POUT_MIN, |
| 2131 | .update = true, |
| 2132 | .readonly = true, |
| 2133 | .attr = "input_lowest", |
| 2134 | }, { |
| 2135 | .reg = PMBUS_VIRT_READ_POUT_MAX, |
| 2136 | .update = true, |
| 2137 | .readonly = true, |
| 2138 | .attr = "input_highest", |
| 2139 | }, { |
| 2140 | .reg = PMBUS_VIRT_RESET_POUT_HISTORY, |
| 2141 | .writeonly = true, |
| 2142 | .attr = "reset_history", |
| 2143 | }, { |
| 2144 | .reg = PMBUS_MFR_POUT_MAX, |
| 2145 | .readonly = true, |
| 2146 | .attr = "rated_max", |
| 2147 | }, |
| 2148 | }; |
| 2149 | |
| 2150 | static const struct pmbus_sensor_attr power_attributes[] = { |
| 2151 | { |
| 2152 | .reg = PMBUS_READ_PIN, |
| 2153 | .class = PSC_POWER, |
| 2154 | .label = "pin", |
| 2155 | .func = PMBUS_HAVE_PIN, |
| 2156 | .sfunc = PMBUS_HAVE_STATUS_INPUT, |
| 2157 | .sreg = PMBUS_STATUS_INPUT, |
| 2158 | .gbit = PB_STATUS_INPUT, |
| 2159 | .limit = pin_limit_attrs, |
| 2160 | .nlimit = ARRAY_SIZE(pin_limit_attrs), |
| 2161 | }, { |
| 2162 | .reg = PMBUS_READ_POUT, |
| 2163 | .class = PSC_POWER, |
| 2164 | .label = "pout", |
| 2165 | .paged = true, |
| 2166 | .func = PMBUS_HAVE_POUT, |
| 2167 | .sfunc = PMBUS_HAVE_STATUS_IOUT, |
| 2168 | .sreg = PMBUS_STATUS_IOUT, |
| 2169 | .limit = pout_limit_attrs, |
| 2170 | .nlimit = ARRAY_SIZE(pout_limit_attrs), |
| 2171 | } |
| 2172 | }; |
| 2173 | |
| 2174 | /* Temperature atributes */ |
| 2175 | |
| 2176 | static const struct pmbus_limit_attr temp_limit_attrs[] = { |
| 2177 | { |
| 2178 | .reg = PMBUS_UT_WARN_LIMIT, |
| 2179 | .low = true, |
| 2180 | .attr = "min", |
| 2181 | .alarm = "min_alarm", |
| 2182 | .sbit = PB_TEMP_UT_WARNING, |
| 2183 | }, { |
| 2184 | .reg = PMBUS_UT_FAULT_LIMIT, |
| 2185 | .low = true, |
| 2186 | .attr = "lcrit", |
| 2187 | .alarm = "lcrit_alarm", |
| 2188 | .sbit = PB_TEMP_UT_FAULT, |
| 2189 | }, { |
| 2190 | .reg = PMBUS_OT_WARN_LIMIT, |
| 2191 | .attr = "max", |
| 2192 | .alarm = "max_alarm", |
| 2193 | .sbit = PB_TEMP_OT_WARNING, |
| 2194 | }, { |
| 2195 | .reg = PMBUS_OT_FAULT_LIMIT, |
| 2196 | .attr = "crit", |
| 2197 | .alarm = "crit_alarm", |
| 2198 | .sbit = PB_TEMP_OT_FAULT, |
| 2199 | }, { |
| 2200 | .reg = PMBUS_VIRT_READ_TEMP_MIN, |
| 2201 | .readonly = true, |
| 2202 | .attr = "lowest", |
| 2203 | }, { |
| 2204 | .reg = PMBUS_VIRT_READ_TEMP_AVG, |
| 2205 | .readonly = true, |
| 2206 | .attr = "average", |
| 2207 | }, { |
| 2208 | .reg = PMBUS_VIRT_READ_TEMP_MAX, |
| 2209 | .readonly = true, |
| 2210 | .attr = "highest", |
| 2211 | }, { |
| 2212 | .reg = PMBUS_VIRT_RESET_TEMP_HISTORY, |
| 2213 | .writeonly = true, |
| 2214 | .attr = "reset_history", |
| 2215 | }, { |
| 2216 | .reg = PMBUS_MFR_MAX_TEMP_1, |
| 2217 | .readonly = true, |
| 2218 | .attr = "rated_max", |
| 2219 | }, |
| 2220 | }; |
| 2221 | |
| 2222 | static const struct pmbus_limit_attr temp_limit_attrs2[] = { |
| 2223 | { |
| 2224 | .reg = PMBUS_UT_WARN_LIMIT, |
| 2225 | .low = true, |
| 2226 | .attr = "min", |
| 2227 | .alarm = "min_alarm", |
| 2228 | .sbit = PB_TEMP_UT_WARNING, |
| 2229 | }, { |
| 2230 | .reg = PMBUS_UT_FAULT_LIMIT, |
| 2231 | .low = true, |
| 2232 | .attr = "lcrit", |
| 2233 | .alarm = "lcrit_alarm", |
| 2234 | .sbit = PB_TEMP_UT_FAULT, |
| 2235 | }, { |
| 2236 | .reg = PMBUS_OT_WARN_LIMIT, |
| 2237 | .attr = "max", |
| 2238 | .alarm = "max_alarm", |
| 2239 | .sbit = PB_TEMP_OT_WARNING, |
| 2240 | }, { |
| 2241 | .reg = PMBUS_OT_FAULT_LIMIT, |
| 2242 | .attr = "crit", |
| 2243 | .alarm = "crit_alarm", |
| 2244 | .sbit = PB_TEMP_OT_FAULT, |
| 2245 | }, { |
| 2246 | .reg = PMBUS_VIRT_READ_TEMP2_MIN, |
| 2247 | .readonly = true, |
| 2248 | .attr = "lowest", |
| 2249 | }, { |
| 2250 | .reg = PMBUS_VIRT_READ_TEMP2_AVG, |
| 2251 | .readonly = true, |
| 2252 | .attr = "average", |
| 2253 | }, { |
| 2254 | .reg = PMBUS_VIRT_READ_TEMP2_MAX, |
| 2255 | .readonly = true, |
| 2256 | .attr = "highest", |
| 2257 | }, { |
| 2258 | .reg = PMBUS_VIRT_RESET_TEMP2_HISTORY, |
| 2259 | .writeonly = true, |
| 2260 | .attr = "reset_history", |
| 2261 | }, { |
| 2262 | .reg = PMBUS_MFR_MAX_TEMP_2, |
| 2263 | .readonly = true, |
| 2264 | .attr = "rated_max", |
| 2265 | }, |
| 2266 | }; |
| 2267 | |
| 2268 | static const struct pmbus_limit_attr temp_limit_attrs3[] = { |
| 2269 | { |
| 2270 | .reg = PMBUS_UT_WARN_LIMIT, |
| 2271 | .low = true, |
| 2272 | .attr = "min", |
| 2273 | .alarm = "min_alarm", |
| 2274 | .sbit = PB_TEMP_UT_WARNING, |
| 2275 | }, { |
| 2276 | .reg = PMBUS_UT_FAULT_LIMIT, |
| 2277 | .low = true, |
| 2278 | .attr = "lcrit", |
| 2279 | .alarm = "lcrit_alarm", |
| 2280 | .sbit = PB_TEMP_UT_FAULT, |
| 2281 | }, { |
| 2282 | .reg = PMBUS_OT_WARN_LIMIT, |
| 2283 | .attr = "max", |
| 2284 | .alarm = "max_alarm", |
| 2285 | .sbit = PB_TEMP_OT_WARNING, |
| 2286 | }, { |
| 2287 | .reg = PMBUS_OT_FAULT_LIMIT, |
| 2288 | .attr = "crit", |
| 2289 | .alarm = "crit_alarm", |
| 2290 | .sbit = PB_TEMP_OT_FAULT, |
| 2291 | }, { |
| 2292 | .reg = PMBUS_MFR_MAX_TEMP_3, |
| 2293 | .readonly = true, |
| 2294 | .attr = "rated_max", |
| 2295 | }, |
| 2296 | }; |
| 2297 | |
| 2298 | static const struct pmbus_sensor_attr temp_attributes[] = { |
| 2299 | { |
| 2300 | .reg = PMBUS_READ_TEMPERATURE_1, |
| 2301 | .class = PSC_TEMPERATURE, |
| 2302 | .paged = true, |
| 2303 | .update = true, |
| 2304 | .compare = true, |
| 2305 | .func = PMBUS_HAVE_TEMP, |
| 2306 | .sfunc = PMBUS_HAVE_STATUS_TEMP, |
| 2307 | .sreg = PMBUS_STATUS_TEMPERATURE, |
| 2308 | .gbit = PB_STATUS_TEMPERATURE, |
| 2309 | .limit = temp_limit_attrs, |
| 2310 | .nlimit = ARRAY_SIZE(temp_limit_attrs), |
| 2311 | }, { |
| 2312 | .reg = PMBUS_READ_TEMPERATURE_2, |
| 2313 | .class = PSC_TEMPERATURE, |
| 2314 | .paged = true, |
| 2315 | .update = true, |
| 2316 | .compare = true, |
| 2317 | .func = PMBUS_HAVE_TEMP2, |
| 2318 | .sfunc = PMBUS_HAVE_STATUS_TEMP, |
| 2319 | .sreg = PMBUS_STATUS_TEMPERATURE, |
| 2320 | .gbit = PB_STATUS_TEMPERATURE, |
| 2321 | .limit = temp_limit_attrs2, |
| 2322 | .nlimit = ARRAY_SIZE(temp_limit_attrs2), |
| 2323 | }, { |
| 2324 | .reg = PMBUS_READ_TEMPERATURE_3, |
| 2325 | .class = PSC_TEMPERATURE, |
| 2326 | .paged = true, |
| 2327 | .update = true, |
| 2328 | .compare = true, |
| 2329 | .func = PMBUS_HAVE_TEMP3, |
| 2330 | .sfunc = PMBUS_HAVE_STATUS_TEMP, |
| 2331 | .sreg = PMBUS_STATUS_TEMPERATURE, |
| 2332 | .gbit = PB_STATUS_TEMPERATURE, |
| 2333 | .limit = temp_limit_attrs3, |
| 2334 | .nlimit = ARRAY_SIZE(temp_limit_attrs3), |
| 2335 | } |
| 2336 | }; |
| 2337 | |
| 2338 | static const int pmbus_fan_registers[] = { |
| 2339 | PMBUS_READ_FAN_SPEED_1, |
| 2340 | PMBUS_READ_FAN_SPEED_2, |
| 2341 | PMBUS_READ_FAN_SPEED_3, |
| 2342 | PMBUS_READ_FAN_SPEED_4 |
| 2343 | }; |
| 2344 | |
| 2345 | static const int pmbus_fan_status_registers[] = { |
| 2346 | PMBUS_STATUS_FAN_12, |
| 2347 | PMBUS_STATUS_FAN_12, |
| 2348 | PMBUS_STATUS_FAN_34, |
| 2349 | PMBUS_STATUS_FAN_34 |
| 2350 | }; |
| 2351 | |
| 2352 | static const u32 pmbus_fan_flags[] = { |
| 2353 | PMBUS_HAVE_FAN12, |
| 2354 | PMBUS_HAVE_FAN12, |
| 2355 | PMBUS_HAVE_FAN34, |
| 2356 | PMBUS_HAVE_FAN34 |
| 2357 | }; |
| 2358 | |
| 2359 | static const u32 pmbus_fan_status_flags[] = { |
| 2360 | PMBUS_HAVE_STATUS_FAN12, |
| 2361 | PMBUS_HAVE_STATUS_FAN12, |
| 2362 | PMBUS_HAVE_STATUS_FAN34, |
| 2363 | PMBUS_HAVE_STATUS_FAN34 |
| 2364 | }; |
| 2365 | |
| 2366 | /* Fans */ |
| 2367 | |
| 2368 | /* Precondition: FAN_CONFIG_x_y and FAN_COMMAND_x must exist for the fan ID */ |
| 2369 | static int pmbus_add_fan_ctrl(struct i2c_client *client, |
| 2370 | struct pmbus_data *data, int index, int page, |
| 2371 | int id, u8 config) |
| 2372 | { |
| 2373 | struct pmbus_sensor *sensor; |
| 2374 | |
| 2375 | sensor = pmbus_add_sensor(data, "fan", "target", index, page, |
| 2376 | 0xff, PMBUS_VIRT_FAN_TARGET_1 + id, PSC_FAN, |
| 2377 | false, false, false, true); |
| 2378 | |
| 2379 | if (!sensor) |
| 2380 | return -ENOMEM; |
| 2381 | |
| 2382 | if (!((data->info->func[page] & PMBUS_HAVE_PWM12) || |
| 2383 | (data->info->func[page] & PMBUS_HAVE_PWM34))) |
| 2384 | return 0; |
| 2385 | |
| 2386 | sensor = pmbus_add_sensor(data, "pwm", NULL, index, page, |
| 2387 | 0xff, PMBUS_VIRT_PWM_1 + id, PSC_PWM, |
| 2388 | false, false, false, true); |
| 2389 | |
| 2390 | if (!sensor) |
| 2391 | return -ENOMEM; |
| 2392 | |
| 2393 | sensor = pmbus_add_sensor(data, "pwm", "enable", index, page, |
| 2394 | 0xff, PMBUS_VIRT_PWM_ENABLE_1 + id, PSC_PWM, |
| 2395 | true, false, false, false); |
| 2396 | |
| 2397 | if (!sensor) |
| 2398 | return -ENOMEM; |
| 2399 | |
| 2400 | return 0; |
| 2401 | } |
| 2402 | |
| 2403 | static int pmbus_add_fan_attributes(struct i2c_client *client, |
| 2404 | struct pmbus_data *data) |
| 2405 | { |
| 2406 | const struct pmbus_driver_info *info = data->info; |
| 2407 | int index = 1; |
| 2408 | int page; |
| 2409 | int ret; |
| 2410 | |
| 2411 | for (page = 0; page < info->pages; page++) { |
| 2412 | int f; |
| 2413 | |
| 2414 | for (f = 0; f < ARRAY_SIZE(pmbus_fan_registers); f++) { |
| 2415 | int regval; |
| 2416 | |
| 2417 | if (!(info->func[page] & pmbus_fan_flags[f])) |
| 2418 | break; |
| 2419 | |
| 2420 | if (!pmbus_check_word_register(client, page, |
| 2421 | pmbus_fan_registers[f])) |
| 2422 | break; |
| 2423 | |
| 2424 | /* |
| 2425 | * Skip fan if not installed. |
| 2426 | * Each fan configuration register covers multiple fans, |
| 2427 | * so we have to do some magic. |
| 2428 | */ |
| 2429 | regval = _pmbus_read_byte_data(client, page, |
| 2430 | pmbus_fan_config_registers[f]); |
| 2431 | if (regval < 0 || |
| 2432 | (!(regval & (PB_FAN_1_INSTALLED >> ((f & 1) * 4))))) |
| 2433 | continue; |
| 2434 | |
| 2435 | if (pmbus_add_sensor(data, "fan", "input", index, |
| 2436 | page, 0xff, pmbus_fan_registers[f], |
| 2437 | PSC_FAN, true, true, false, true) == NULL) |
| 2438 | return -ENOMEM; |
| 2439 | |
| 2440 | /* Fan control */ |
| 2441 | if (pmbus_check_word_register(client, page, |
| 2442 | pmbus_fan_command_registers[f])) { |
| 2443 | ret = pmbus_add_fan_ctrl(client, data, index, |
| 2444 | page, f, regval); |
| 2445 | if (ret < 0) |
| 2446 | return ret; |
| 2447 | } |
| 2448 | |
| 2449 | /* |
| 2450 | * Each fan status register covers multiple fans, |
| 2451 | * so we have to do some magic. |
| 2452 | */ |
| 2453 | if ((info->func[page] & pmbus_fan_status_flags[f]) && |
| 2454 | pmbus_check_byte_register(client, |
| 2455 | page, pmbus_fan_status_registers[f])) { |
| 2456 | int reg; |
| 2457 | |
| 2458 | if (f > 1) /* fan 3, 4 */ |
| 2459 | reg = PMBUS_STATUS_FAN_34; |
| 2460 | else |
| 2461 | reg = PMBUS_STATUS_FAN_12; |
| 2462 | ret = pmbus_add_boolean(data, "fan", |
| 2463 | "alarm", index, NULL, NULL, page, reg, |
| 2464 | PB_FAN_FAN1_WARNING >> (f & 1)); |
| 2465 | if (ret) |
| 2466 | return ret; |
| 2467 | ret = pmbus_add_boolean(data, "fan", |
| 2468 | "fault", index, NULL, NULL, page, reg, |
| 2469 | PB_FAN_FAN1_FAULT >> (f & 1)); |
| 2470 | if (ret) |
| 2471 | return ret; |
| 2472 | } |
| 2473 | index++; |
| 2474 | } |
| 2475 | } |
| 2476 | return 0; |
| 2477 | } |
| 2478 | |
| 2479 | struct pmbus_samples_attr { |
| 2480 | int reg; |
| 2481 | char *name; |
| 2482 | }; |
| 2483 | |
| 2484 | struct pmbus_samples_reg { |
| 2485 | int page; |
| 2486 | struct pmbus_samples_attr *attr; |
| 2487 | struct sensor_device_attribute attribute; |
| 2488 | }; |
| 2489 | |
| 2490 | static struct pmbus_samples_attr pmbus_samples_registers[] = { |
| 2491 | { |
| 2492 | .reg = PMBUS_VIRT_SAMPLES, |
| 2493 | .name = "samples", |
| 2494 | }, { |
| 2495 | .reg = PMBUS_VIRT_IN_SAMPLES, |
| 2496 | .name = "in_samples", |
| 2497 | }, { |
| 2498 | .reg = PMBUS_VIRT_CURR_SAMPLES, |
| 2499 | .name = "curr_samples", |
| 2500 | }, { |
| 2501 | .reg = PMBUS_VIRT_POWER_SAMPLES, |
| 2502 | .name = "power_samples", |
| 2503 | }, { |
| 2504 | .reg = PMBUS_VIRT_TEMP_SAMPLES, |
| 2505 | .name = "temp_samples", |
| 2506 | } |
| 2507 | }; |
| 2508 | |
| 2509 | #define to_samples_reg(x) container_of(x, struct pmbus_samples_reg, attribute) |
| 2510 | |
| 2511 | static ssize_t pmbus_show_samples(struct device *dev, |
| 2512 | struct device_attribute *devattr, char *buf) |
| 2513 | { |
| 2514 | int val; |
| 2515 | struct i2c_client *client = to_i2c_client(dev->parent); |
| 2516 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 2517 | struct pmbus_samples_reg *reg = to_samples_reg(attr); |
| 2518 | |
| 2519 | scoped_guard(pmbus_lock, client) { |
| 2520 | val = _pmbus_read_word_data(client, reg->page, 0xff, reg->attr->reg); |
| 2521 | if (val < 0) |
| 2522 | return val; |
| 2523 | } |
| 2524 | |
| 2525 | return sysfs_emit(buf, "%d\n", val); |
| 2526 | } |
| 2527 | |
| 2528 | static ssize_t pmbus_set_samples(struct device *dev, |
| 2529 | struct device_attribute *devattr, |
| 2530 | const char *buf, size_t count) |
| 2531 | { |
| 2532 | int ret; |
| 2533 | long val; |
| 2534 | struct i2c_client *client = to_i2c_client(dev->parent); |
| 2535 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 2536 | struct pmbus_samples_reg *reg = to_samples_reg(attr); |
| 2537 | |
| 2538 | if (kstrtol(buf, 0, &val) < 0) |
| 2539 | return -EINVAL; |
| 2540 | |
| 2541 | guard(pmbus_lock)(client); |
| 2542 | |
| 2543 | ret = _pmbus_write_word_data(client, reg->page, reg->attr->reg, val); |
| 2544 | |
| 2545 | return ret ? : count; |
| 2546 | } |
| 2547 | |
| 2548 | static int pmbus_add_samples_attr(struct pmbus_data *data, int page, |
| 2549 | struct pmbus_samples_attr *attr) |
| 2550 | { |
| 2551 | struct sensor_device_attribute *a; |
| 2552 | struct pmbus_samples_reg *reg; |
| 2553 | |
| 2554 | reg = devm_kzalloc(data->dev, sizeof(*reg), GFP_KERNEL); |
| 2555 | if (!reg) |
| 2556 | return -ENOMEM; |
| 2557 | |
| 2558 | reg->attr = attr; |
| 2559 | reg->page = page; |
| 2560 | |
| 2561 | a = ®->attribute; |
| 2562 | |
| 2563 | pmbus_attr_init(a, attr->name, 0644, |
| 2564 | pmbus_show_samples, pmbus_set_samples, -1); |
| 2565 | |
| 2566 | return pmbus_add_attribute(data, &a->dev_attr.attr); |
| 2567 | } |
| 2568 | |
| 2569 | static int pmbus_add_samples_attributes(struct i2c_client *client, |
| 2570 | struct pmbus_data *data) |
| 2571 | { |
| 2572 | const struct pmbus_driver_info *info = data->info; |
| 2573 | int s; |
| 2574 | |
| 2575 | if (!(info->func[0] & PMBUS_HAVE_SAMPLES)) |
| 2576 | return 0; |
| 2577 | |
| 2578 | for (s = 0; s < ARRAY_SIZE(pmbus_samples_registers); s++) { |
| 2579 | struct pmbus_samples_attr *attr; |
| 2580 | int ret; |
| 2581 | |
| 2582 | attr = &pmbus_samples_registers[s]; |
| 2583 | if (!pmbus_check_word_register(client, 0, attr->reg)) |
| 2584 | continue; |
| 2585 | |
| 2586 | ret = pmbus_add_samples_attr(data, 0, attr); |
| 2587 | if (ret) |
| 2588 | return ret; |
| 2589 | } |
| 2590 | |
| 2591 | return 0; |
| 2592 | } |
| 2593 | |
| 2594 | static int pmbus_find_attributes(struct i2c_client *client, |
| 2595 | struct pmbus_data *data) |
| 2596 | { |
| 2597 | int ret; |
| 2598 | |
| 2599 | /* Voltage sensors */ |
| 2600 | ret = pmbus_add_sensor_attrs(client, data, "in", voltage_attributes, |
| 2601 | ARRAY_SIZE(voltage_attributes)); |
| 2602 | if (ret) |
| 2603 | return ret; |
| 2604 | |
| 2605 | /* Current sensors */ |
| 2606 | ret = pmbus_add_sensor_attrs(client, data, "curr", current_attributes, |
| 2607 | ARRAY_SIZE(current_attributes)); |
| 2608 | if (ret) |
| 2609 | return ret; |
| 2610 | |
| 2611 | /* Power sensors */ |
| 2612 | ret = pmbus_add_sensor_attrs(client, data, "power", power_attributes, |
| 2613 | ARRAY_SIZE(power_attributes)); |
| 2614 | if (ret) |
| 2615 | return ret; |
| 2616 | |
| 2617 | /* Temperature sensors */ |
| 2618 | ret = pmbus_add_sensor_attrs(client, data, "temp", temp_attributes, |
| 2619 | ARRAY_SIZE(temp_attributes)); |
| 2620 | if (ret) |
| 2621 | return ret; |
| 2622 | |
| 2623 | /* Fans */ |
| 2624 | ret = pmbus_add_fan_attributes(client, data); |
| 2625 | if (ret) |
| 2626 | return ret; |
| 2627 | |
| 2628 | ret = pmbus_add_samples_attributes(client, data); |
| 2629 | return ret; |
| 2630 | } |
| 2631 | |
| 2632 | /* |
| 2633 | * The pmbus_class_attr_map structure maps one sensor class to |
| 2634 | * it's corresponding sensor attributes array. |
| 2635 | */ |
| 2636 | struct pmbus_class_attr_map { |
| 2637 | enum pmbus_sensor_classes class; |
| 2638 | int nattr; |
| 2639 | const struct pmbus_sensor_attr *attr; |
| 2640 | }; |
| 2641 | |
| 2642 | static const struct pmbus_class_attr_map class_attr_map[] = { |
| 2643 | { |
| 2644 | .class = PSC_VOLTAGE_IN, |
| 2645 | .attr = voltage_attributes, |
| 2646 | .nattr = ARRAY_SIZE(voltage_attributes), |
| 2647 | }, { |
| 2648 | .class = PSC_VOLTAGE_OUT, |
| 2649 | .attr = voltage_attributes, |
| 2650 | .nattr = ARRAY_SIZE(voltage_attributes), |
| 2651 | }, { |
| 2652 | .class = PSC_CURRENT_IN, |
| 2653 | .attr = current_attributes, |
| 2654 | .nattr = ARRAY_SIZE(current_attributes), |
| 2655 | }, { |
| 2656 | .class = PSC_CURRENT_OUT, |
| 2657 | .attr = current_attributes, |
| 2658 | .nattr = ARRAY_SIZE(current_attributes), |
| 2659 | }, { |
| 2660 | .class = PSC_POWER, |
| 2661 | .attr = power_attributes, |
| 2662 | .nattr = ARRAY_SIZE(power_attributes), |
| 2663 | }, { |
| 2664 | .class = PSC_TEMPERATURE, |
| 2665 | .attr = temp_attributes, |
| 2666 | .nattr = ARRAY_SIZE(temp_attributes), |
| 2667 | } |
| 2668 | }; |
| 2669 | |
| 2670 | /* |
| 2671 | * Read the coefficients for direct mode. |
| 2672 | */ |
| 2673 | static int pmbus_read_coefficients(struct i2c_client *client, |
| 2674 | struct pmbus_driver_info *info, |
| 2675 | const struct pmbus_sensor_attr *attr) |
| 2676 | { |
| 2677 | int rv; |
| 2678 | union i2c_smbus_data data; |
| 2679 | enum pmbus_sensor_classes class = attr->class; |
| 2680 | s8 R; |
| 2681 | s16 m, b; |
| 2682 | |
| 2683 | data.block[0] = 2; |
| 2684 | data.block[1] = attr->reg; |
| 2685 | data.block[2] = 0x01; |
| 2686 | |
| 2687 | pmbus_wait(client); |
| 2688 | rv = i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
| 2689 | I2C_SMBUS_WRITE, PMBUS_COEFFICIENTS, |
| 2690 | I2C_SMBUS_BLOCK_PROC_CALL, &data); |
| 2691 | pmbus_update_ts(client, PMBUS_OP_WRITE); |
| 2692 | |
| 2693 | if (rv < 0) |
| 2694 | return rv; |
| 2695 | |
| 2696 | if (data.block[0] != 5) |
| 2697 | return -EIO; |
| 2698 | |
| 2699 | m = data.block[1] | (data.block[2] << 8); |
| 2700 | b = data.block[3] | (data.block[4] << 8); |
| 2701 | R = data.block[5]; |
| 2702 | info->m[class] = m; |
| 2703 | info->b[class] = b; |
| 2704 | info->R[class] = R; |
| 2705 | |
| 2706 | return rv; |
| 2707 | } |
| 2708 | |
| 2709 | static int pmbus_init_coefficients(struct i2c_client *client, |
| 2710 | struct pmbus_driver_info *info) |
| 2711 | { |
| 2712 | int i, n, ret = -EINVAL; |
| 2713 | const struct pmbus_class_attr_map *map; |
| 2714 | const struct pmbus_sensor_attr *attr; |
| 2715 | |
| 2716 | for (i = 0; i < ARRAY_SIZE(class_attr_map); i++) { |
| 2717 | map = &class_attr_map[i]; |
| 2718 | if (info->format[map->class] != direct) |
| 2719 | continue; |
| 2720 | for (n = 0; n < map->nattr; n++) { |
| 2721 | attr = &map->attr[n]; |
| 2722 | if (map->class != attr->class) |
| 2723 | continue; |
| 2724 | ret = pmbus_read_coefficients(client, info, attr); |
| 2725 | if (ret >= 0) |
| 2726 | break; |
| 2727 | } |
| 2728 | if (ret < 0) { |
| 2729 | dev_err(&client->dev, |
| 2730 | "No coefficients found for sensor class %d\n", |
| 2731 | map->class); |
| 2732 | return -EINVAL; |
| 2733 | } |
| 2734 | } |
| 2735 | |
| 2736 | return 0; |
| 2737 | } |
| 2738 | |
| 2739 | /* |
| 2740 | * Identify chip parameters. |
| 2741 | * This function is called for all chips. |
| 2742 | */ |
| 2743 | static int pmbus_identify_common(struct i2c_client *client, |
| 2744 | struct pmbus_data *data, int page) |
| 2745 | { |
| 2746 | int vout_mode = -1; |
| 2747 | |
| 2748 | if (pmbus_check_byte_register(client, page, PMBUS_VOUT_MODE)) |
| 2749 | vout_mode = _pmbus_read_byte_data(client, page, |
| 2750 | PMBUS_VOUT_MODE); |
| 2751 | if (vout_mode >= 0 && vout_mode != 0xff) { |
| 2752 | /* |
| 2753 | * Not all chips support the VOUT_MODE command, |
| 2754 | * so a failure to read it is not an error. |
| 2755 | */ |
| 2756 | switch (vout_mode >> 5) { |
| 2757 | case 0: /* linear mode */ |
| 2758 | if (data->info->format[PSC_VOLTAGE_OUT] != linear) |
| 2759 | return -ENODEV; |
| 2760 | |
| 2761 | data->exponent[page] = ((s8)(vout_mode << 3)) >> 3; |
| 2762 | break; |
| 2763 | case 1: /* VID mode */ |
| 2764 | if (data->info->format[PSC_VOLTAGE_OUT] != vid) |
| 2765 | return -ENODEV; |
| 2766 | break; |
| 2767 | case 2: /* direct mode */ |
| 2768 | if (data->info->format[PSC_VOLTAGE_OUT] != direct) |
| 2769 | return -ENODEV; |
| 2770 | break; |
| 2771 | case 3: /* ieee 754 half precision */ |
| 2772 | if (data->info->format[PSC_VOLTAGE_OUT] != ieee754) |
| 2773 | return -ENODEV; |
| 2774 | break; |
| 2775 | default: |
| 2776 | return -ENODEV; |
| 2777 | } |
| 2778 | } |
| 2779 | |
| 2780 | return 0; |
| 2781 | } |
| 2782 | |
| 2783 | static int pmbus_read_status_byte(struct i2c_client *client, int page) |
| 2784 | { |
| 2785 | return _pmbus_read_byte_data(client, page, PMBUS_STATUS_BYTE); |
| 2786 | } |
| 2787 | |
| 2788 | static int pmbus_read_status_word(struct i2c_client *client, int page) |
| 2789 | { |
| 2790 | return _pmbus_read_word_data(client, page, 0xff, PMBUS_STATUS_WORD); |
| 2791 | } |
| 2792 | |
| 2793 | /* PEC attribute support */ |
| 2794 | |
| 2795 | static ssize_t pec_show(struct device *dev, struct device_attribute *dummy, |
| 2796 | char *buf) |
| 2797 | { |
| 2798 | struct i2c_client *client = to_i2c_client(dev); |
| 2799 | |
| 2800 | return sysfs_emit(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC)); |
| 2801 | } |
| 2802 | |
| 2803 | static ssize_t pec_store(struct device *dev, struct device_attribute *dummy, |
| 2804 | const char *buf, size_t count) |
| 2805 | { |
| 2806 | struct i2c_client *client = to_i2c_client(dev); |
| 2807 | bool enable; |
| 2808 | int err; |
| 2809 | |
| 2810 | err = kstrtobool(buf, &enable); |
| 2811 | if (err < 0) |
| 2812 | return err; |
| 2813 | |
| 2814 | if (enable) |
| 2815 | client->flags |= I2C_CLIENT_PEC; |
| 2816 | else |
| 2817 | client->flags &= ~I2C_CLIENT_PEC; |
| 2818 | |
| 2819 | return count; |
| 2820 | } |
| 2821 | |
| 2822 | static DEVICE_ATTR_RW(pec); |
| 2823 | |
| 2824 | static void pmbus_remove_pec(void *dev) |
| 2825 | { |
| 2826 | device_remove_file(dev, &dev_attr_pec); |
| 2827 | } |
| 2828 | |
| 2829 | static void pmbus_init_wp(struct i2c_client *client, struct pmbus_data *data) |
| 2830 | { |
| 2831 | int ret; |
| 2832 | |
| 2833 | switch (wp) { |
| 2834 | case 0: |
| 2835 | _pmbus_write_byte_data(client, -1, |
| 2836 | PMBUS_WRITE_PROTECT, 0); |
| 2837 | break; |
| 2838 | |
| 2839 | case 1: |
| 2840 | _pmbus_write_byte_data(client, -1, |
| 2841 | PMBUS_WRITE_PROTECT, PB_WP_VOUT); |
| 2842 | break; |
| 2843 | |
| 2844 | case 2: |
| 2845 | _pmbus_write_byte_data(client, -1, |
| 2846 | PMBUS_WRITE_PROTECT, PB_WP_OP); |
| 2847 | break; |
| 2848 | |
| 2849 | case 3: |
| 2850 | _pmbus_write_byte_data(client, -1, |
| 2851 | PMBUS_WRITE_PROTECT, PB_WP_ALL); |
| 2852 | break; |
| 2853 | |
| 2854 | default: |
| 2855 | /* Ignore the other values */ |
| 2856 | break; |
| 2857 | } |
| 2858 | |
| 2859 | ret = _pmbus_read_byte_data(client, -1, PMBUS_WRITE_PROTECT); |
| 2860 | if (ret < 0) |
| 2861 | return; |
| 2862 | |
| 2863 | switch (ret & PB_WP_ANY) { |
| 2864 | case PB_WP_ALL: |
| 2865 | data->flags |= PMBUS_OP_PROTECTED; |
| 2866 | fallthrough; |
| 2867 | case PB_WP_OP: |
| 2868 | data->flags |= PMBUS_VOUT_PROTECTED; |
| 2869 | fallthrough; |
| 2870 | case PB_WP_VOUT: |
| 2871 | data->flags |= PMBUS_WRITE_PROTECTED | PMBUS_SKIP_STATUS_CHECK; |
| 2872 | break; |
| 2873 | |
| 2874 | default: |
| 2875 | break; |
| 2876 | } |
| 2877 | } |
| 2878 | |
| 2879 | static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data, |
| 2880 | struct pmbus_driver_info *info) |
| 2881 | { |
| 2882 | struct device *dev = &client->dev; |
| 2883 | int page, ret; |
| 2884 | |
| 2885 | /* |
| 2886 | * Figure out if PEC is enabled before accessing any other register. |
| 2887 | * Make sure PEC is disabled, will be enabled later if needed. |
| 2888 | */ |
| 2889 | client->flags &= ~I2C_CLIENT_PEC; |
| 2890 | |
| 2891 | /* Enable PEC if the controller and bus supports it */ |
| 2892 | if (!(data->flags & PMBUS_NO_CAPABILITY)) { |
| 2893 | pmbus_wait(client); |
| 2894 | ret = i2c_smbus_read_byte_data(client, PMBUS_CAPABILITY); |
| 2895 | pmbus_update_ts(client, 0); |
| 2896 | |
| 2897 | if (ret >= 0 && (ret & PB_CAPABILITY_ERROR_CHECK)) { |
| 2898 | if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_PEC)) |
| 2899 | client->flags |= I2C_CLIENT_PEC; |
| 2900 | } |
| 2901 | } |
| 2902 | |
| 2903 | /* |
| 2904 | * Some PMBus chips don't support PMBUS_STATUS_WORD, so try |
| 2905 | * to use PMBUS_STATUS_BYTE instead if that is the case. |
| 2906 | * Bail out if both registers are not supported. |
| 2907 | */ |
| 2908 | data->read_status = pmbus_read_status_word; |
| 2909 | pmbus_wait(client); |
| 2910 | ret = i2c_smbus_read_word_data(client, PMBUS_STATUS_WORD); |
| 2911 | pmbus_update_ts(client, 0); |
| 2912 | |
| 2913 | if (ret < 0 || ret == 0xffff) { |
| 2914 | data->read_status = pmbus_read_status_byte; |
| 2915 | pmbus_wait(client); |
| 2916 | ret = i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE); |
| 2917 | pmbus_update_ts(client, 0); |
| 2918 | |
| 2919 | if (ret < 0 || ret == 0xff) { |
| 2920 | dev_err(dev, "PMBus status register not found\n"); |
| 2921 | return -ENODEV; |
| 2922 | } |
| 2923 | } else { |
| 2924 | data->has_status_word = true; |
| 2925 | } |
| 2926 | |
| 2927 | /* |
| 2928 | * Check if the chip is write protected. If it is, we can not clear |
| 2929 | * faults, and we should not try it. Also, in that case, writes into |
| 2930 | * limit registers need to be disabled. |
| 2931 | */ |
| 2932 | if (!(data->flags & PMBUS_NO_WRITE_PROTECT)) |
| 2933 | pmbus_init_wp(client, data); |
| 2934 | |
| 2935 | if (info->have_pmbus_revision) { |
| 2936 | data->have_pmbus_revision = true; |
| 2937 | data->revision = info->pmbus_revision; |
| 2938 | } else { |
| 2939 | ret = i2c_smbus_read_byte_data(client, PMBUS_REVISION); |
| 2940 | if (ret >= 0) { |
| 2941 | data->have_pmbus_revision = true; |
| 2942 | data->revision = ret; |
| 2943 | } |
| 2944 | } |
| 2945 | |
| 2946 | if (data->info->pages) |
| 2947 | pmbus_clear_faults(client); |
| 2948 | else |
| 2949 | pmbus_clear_fault_page(client, -1); |
| 2950 | |
| 2951 | if (info->identify) { |
| 2952 | ret = (*info->identify)(client, info); |
| 2953 | if (ret < 0) { |
| 2954 | dev_err(dev, "Chip identification failed\n"); |
| 2955 | return ret; |
| 2956 | } |
| 2957 | } |
| 2958 | |
| 2959 | if (info->pages <= 0 || info->pages > PMBUS_PAGES) { |
| 2960 | dev_err(dev, "Bad number of PMBus pages: %d\n", info->pages); |
| 2961 | return -ENODEV; |
| 2962 | } |
| 2963 | |
| 2964 | for (page = 0; page < info->pages; page++) { |
| 2965 | ret = pmbus_identify_common(client, data, page); |
| 2966 | if (ret < 0) { |
| 2967 | dev_err(dev, "Failed to identify chip capabilities\n"); |
| 2968 | return ret; |
| 2969 | } |
| 2970 | } |
| 2971 | |
| 2972 | if (data->flags & PMBUS_USE_COEFFICIENTS_CMD) { |
| 2973 | if (!i2c_check_functionality(client->adapter, |
| 2974 | I2C_FUNC_SMBUS_BLOCK_PROC_CALL)) |
| 2975 | return -ENODEV; |
| 2976 | |
| 2977 | ret = pmbus_init_coefficients(client, info); |
| 2978 | if (ret < 0) |
| 2979 | return ret; |
| 2980 | } |
| 2981 | |
| 2982 | if (client->flags & I2C_CLIENT_PEC) { |
| 2983 | /* |
| 2984 | * If I2C_CLIENT_PEC is set here, both the I2C adapter and the |
| 2985 | * chip support PEC. Add 'pec' attribute to client device to let |
| 2986 | * the user control it. |
| 2987 | */ |
| 2988 | ret = device_create_file(dev, &dev_attr_pec); |
| 2989 | if (ret) |
| 2990 | return ret; |
| 2991 | ret = devm_add_action_or_reset(dev, pmbus_remove_pec, dev); |
| 2992 | if (ret) |
| 2993 | return ret; |
| 2994 | } |
| 2995 | |
| 2996 | return 0; |
| 2997 | } |
| 2998 | |
| 2999 | /* A PMBus status flag and the corresponding REGULATOR_ERROR_* and REGULATOR_EVENTS_* flag */ |
| 3000 | struct pmbus_status_assoc { |
| 3001 | int pflag, rflag, eflag; |
| 3002 | }; |
| 3003 | |
| 3004 | /* PMBus->regulator bit mappings for a PMBus status register */ |
| 3005 | struct pmbus_status_category { |
| 3006 | int func; |
| 3007 | int reg; |
| 3008 | const struct pmbus_status_assoc *bits; /* zero-terminated */ |
| 3009 | }; |
| 3010 | |
| 3011 | static const struct pmbus_status_category __maybe_unused pmbus_status_flag_map[] = { |
| 3012 | { |
| 3013 | .func = PMBUS_HAVE_STATUS_VOUT, |
| 3014 | .reg = PMBUS_STATUS_VOUT, |
| 3015 | .bits = (const struct pmbus_status_assoc[]) { |
| 3016 | { PB_VOLTAGE_UV_WARNING, REGULATOR_ERROR_UNDER_VOLTAGE_WARN, |
| 3017 | REGULATOR_EVENT_UNDER_VOLTAGE_WARN }, |
| 3018 | { PB_VOLTAGE_UV_FAULT, REGULATOR_ERROR_UNDER_VOLTAGE, |
| 3019 | REGULATOR_EVENT_UNDER_VOLTAGE }, |
| 3020 | { PB_VOLTAGE_OV_WARNING, REGULATOR_ERROR_OVER_VOLTAGE_WARN, |
| 3021 | REGULATOR_EVENT_OVER_VOLTAGE_WARN }, |
| 3022 | { PB_VOLTAGE_OV_FAULT, REGULATOR_ERROR_REGULATION_OUT, |
| 3023 | REGULATOR_EVENT_OVER_VOLTAGE_WARN }, |
| 3024 | { }, |
| 3025 | }, |
| 3026 | }, { |
| 3027 | .func = PMBUS_HAVE_STATUS_IOUT, |
| 3028 | .reg = PMBUS_STATUS_IOUT, |
| 3029 | .bits = (const struct pmbus_status_assoc[]) { |
| 3030 | { PB_IOUT_OC_WARNING, REGULATOR_ERROR_OVER_CURRENT_WARN, |
| 3031 | REGULATOR_EVENT_OVER_CURRENT_WARN }, |
| 3032 | { PB_IOUT_OC_FAULT, REGULATOR_ERROR_OVER_CURRENT, |
| 3033 | REGULATOR_EVENT_OVER_CURRENT }, |
| 3034 | { PB_IOUT_OC_LV_FAULT, REGULATOR_ERROR_OVER_CURRENT, |
| 3035 | REGULATOR_EVENT_OVER_CURRENT }, |
| 3036 | { }, |
| 3037 | }, |
| 3038 | }, { |
| 3039 | .func = PMBUS_HAVE_STATUS_TEMP, |
| 3040 | .reg = PMBUS_STATUS_TEMPERATURE, |
| 3041 | .bits = (const struct pmbus_status_assoc[]) { |
| 3042 | { PB_TEMP_OT_WARNING, REGULATOR_ERROR_OVER_TEMP_WARN, |
| 3043 | REGULATOR_EVENT_OVER_TEMP_WARN }, |
| 3044 | { PB_TEMP_OT_FAULT, REGULATOR_ERROR_OVER_TEMP, |
| 3045 | REGULATOR_EVENT_OVER_TEMP }, |
| 3046 | { }, |
| 3047 | }, |
| 3048 | }, |
| 3049 | }; |
| 3050 | |
| 3051 | static int _pmbus_is_enabled(struct i2c_client *client, u8 page) |
| 3052 | { |
| 3053 | int ret; |
| 3054 | |
| 3055 | ret = _pmbus_read_byte_data(client, page, PMBUS_OPERATION); |
| 3056 | |
| 3057 | if (ret < 0) |
| 3058 | return ret; |
| 3059 | |
| 3060 | return !!(ret & PB_OPERATION_CONTROL_ON); |
| 3061 | } |
| 3062 | |
| 3063 | static int __maybe_unused pmbus_is_enabled(struct i2c_client *client, u8 page) |
| 3064 | { |
| 3065 | guard(pmbus_lock)(client); |
| 3066 | |
| 3067 | return _pmbus_is_enabled(client, page); |
| 3068 | } |
| 3069 | |
| 3070 | #define to_dev_attr(_dev_attr) \ |
| 3071 | container_of(_dev_attr, struct device_attribute, attr) |
| 3072 | |
| 3073 | static void pmbus_notify(struct pmbus_data *data, int page, int reg, int flags) |
| 3074 | { |
| 3075 | int i; |
| 3076 | |
| 3077 | for (i = 0; i < data->num_attributes; i++) { |
| 3078 | struct device_attribute *da = to_dev_attr(data->group.attrs[i]); |
| 3079 | struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
| 3080 | int index = attr->index; |
| 3081 | u16 smask, sreg; |
| 3082 | u8 spage; |
| 3083 | |
| 3084 | if (index == -1) |
| 3085 | continue; |
| 3086 | |
| 3087 | smask = pb_index_to_mask(index); |
| 3088 | spage = pb_index_to_page(index); |
| 3089 | sreg = pb_index_to_reg(index); |
| 3090 | |
| 3091 | if (reg == sreg && page == spage && (smask & flags)) { |
| 3092 | dev_dbg(data->dev, "sysfs notify: %s", da->attr.name); |
| 3093 | sysfs_notify(&data->hwmon_dev->kobj, NULL, |
| 3094 | da->attr.name); |
| 3095 | kobject_uevent(&data->hwmon_dev->kobj, KOBJ_CHANGE); |
| 3096 | flags &= ~smask; |
| 3097 | } |
| 3098 | |
| 3099 | if (!flags) |
| 3100 | break; |
| 3101 | } |
| 3102 | } |
| 3103 | |
| 3104 | static int _pmbus_get_flags(struct i2c_client *client, u8 page, unsigned int *flags, |
| 3105 | unsigned int *event, bool notify) |
| 3106 | { |
| 3107 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 3108 | int i, status; |
| 3109 | const struct pmbus_status_category *cat; |
| 3110 | const struct pmbus_status_assoc *bit; |
| 3111 | int func = data->info->func[page]; |
| 3112 | |
| 3113 | *flags = 0; |
| 3114 | *event = 0; |
| 3115 | |
| 3116 | for (i = 0; i < ARRAY_SIZE(pmbus_status_flag_map); i++) { |
| 3117 | cat = &pmbus_status_flag_map[i]; |
| 3118 | if (!(func & cat->func)) |
| 3119 | continue; |
| 3120 | |
| 3121 | status = _pmbus_read_byte_data(client, page, cat->reg); |
| 3122 | if (status < 0) |
| 3123 | return status; |
| 3124 | |
| 3125 | for (bit = cat->bits; bit->pflag; bit++) |
| 3126 | if (status & bit->pflag) { |
| 3127 | *flags |= bit->rflag; |
| 3128 | *event |= bit->eflag; |
| 3129 | } |
| 3130 | |
| 3131 | if (notify && status) |
| 3132 | pmbus_notify(data, page, cat->reg, status); |
| 3133 | } |
| 3134 | |
| 3135 | /* |
| 3136 | * Map what bits of STATUS_{WORD,BYTE} we can to REGULATOR_ERROR_* |
| 3137 | * bits. Some of the other bits are tempting (especially for cases |
| 3138 | * where we don't have the relevant PMBUS_HAVE_STATUS_* |
| 3139 | * functionality), but there's an unfortunate ambiguity in that |
| 3140 | * they're defined as indicating a fault *or* a warning, so we can't |
| 3141 | * easily determine whether to report REGULATOR_ERROR_<foo> or |
| 3142 | * REGULATOR_ERROR_<foo>_WARN. |
| 3143 | */ |
| 3144 | status = pmbus_get_status(client, page, PMBUS_STATUS_WORD); |
| 3145 | if (status < 0) |
| 3146 | return status; |
| 3147 | |
| 3148 | if (_pmbus_is_enabled(client, page)) { |
| 3149 | if (status & PB_STATUS_OFF) { |
| 3150 | *flags |= REGULATOR_ERROR_FAIL; |
| 3151 | *event |= REGULATOR_EVENT_FAIL; |
| 3152 | } |
| 3153 | |
| 3154 | if (status & PB_STATUS_POWER_GOOD_N) { |
| 3155 | *flags |= REGULATOR_ERROR_REGULATION_OUT; |
| 3156 | *event |= REGULATOR_EVENT_REGULATION_OUT; |
| 3157 | } |
| 3158 | } |
| 3159 | /* |
| 3160 | * Unlike most other status bits, PB_STATUS_{IOUT_OC,VOUT_OV} are |
| 3161 | * defined strictly as fault indicators (not warnings). |
| 3162 | */ |
| 3163 | if (status & PB_STATUS_IOUT_OC) { |
| 3164 | *flags |= REGULATOR_ERROR_OVER_CURRENT; |
| 3165 | *event |= REGULATOR_EVENT_OVER_CURRENT; |
| 3166 | } |
| 3167 | if (status & PB_STATUS_VOUT_OV) { |
| 3168 | *flags |= REGULATOR_ERROR_REGULATION_OUT; |
| 3169 | *event |= REGULATOR_EVENT_FAIL; |
| 3170 | } |
| 3171 | |
| 3172 | /* |
| 3173 | * If we haven't discovered any thermal faults or warnings via |
| 3174 | * PMBUS_STATUS_TEMPERATURE, map PB_STATUS_TEMPERATURE to a warning as |
| 3175 | * a (conservative) best-effort interpretation. |
| 3176 | */ |
| 3177 | if (!(*flags & (REGULATOR_ERROR_OVER_TEMP | REGULATOR_ERROR_OVER_TEMP_WARN)) && |
| 3178 | (status & PB_STATUS_TEMPERATURE)) { |
| 3179 | *flags |= REGULATOR_ERROR_OVER_TEMP_WARN; |
| 3180 | *event |= REGULATOR_EVENT_OVER_TEMP_WARN; |
| 3181 | } |
| 3182 | |
| 3183 | return 0; |
| 3184 | } |
| 3185 | |
| 3186 | static int __maybe_unused pmbus_get_flags(struct i2c_client *client, u8 page, unsigned int *flags, |
| 3187 | unsigned int *event, bool notify) |
| 3188 | { |
| 3189 | guard(pmbus_lock)(client); |
| 3190 | |
| 3191 | return _pmbus_get_flags(client, page, flags, event, notify); |
| 3192 | } |
| 3193 | |
| 3194 | #if IS_ENABLED(CONFIG_REGULATOR) |
| 3195 | static int pmbus_regulator_is_enabled(struct regulator_dev *rdev) |
| 3196 | { |
| 3197 | struct device *dev = rdev_get_dev(rdev); |
| 3198 | struct i2c_client *client = to_i2c_client(dev->parent); |
| 3199 | |
| 3200 | return pmbus_is_enabled(client, rdev_get_id(rdev)); |
| 3201 | } |
| 3202 | |
| 3203 | static int _pmbus_regulator_on_off(struct regulator_dev *rdev, bool enable) |
| 3204 | { |
| 3205 | struct device *dev = rdev_get_dev(rdev); |
| 3206 | struct i2c_client *client = to_i2c_client(dev->parent); |
| 3207 | u8 page = rdev_get_id(rdev); |
| 3208 | |
| 3209 | guard(pmbus_lock)(client); |
| 3210 | |
| 3211 | return pmbus_update_byte_data(client, page, PMBUS_OPERATION, |
| 3212 | PB_OPERATION_CONTROL_ON, |
| 3213 | enable ? PB_OPERATION_CONTROL_ON : 0); |
| 3214 | } |
| 3215 | |
| 3216 | static int pmbus_regulator_enable(struct regulator_dev *rdev) |
| 3217 | { |
| 3218 | return _pmbus_regulator_on_off(rdev, 1); |
| 3219 | } |
| 3220 | |
| 3221 | static int pmbus_regulator_disable(struct regulator_dev *rdev) |
| 3222 | { |
| 3223 | return _pmbus_regulator_on_off(rdev, 0); |
| 3224 | } |
| 3225 | |
| 3226 | static int pmbus_regulator_get_error_flags(struct regulator_dev *rdev, unsigned int *flags) |
| 3227 | { |
| 3228 | struct device *dev = rdev_get_dev(rdev); |
| 3229 | struct i2c_client *client = to_i2c_client(dev->parent); |
| 3230 | int event; |
| 3231 | |
| 3232 | return pmbus_get_flags(client, rdev_get_id(rdev), flags, &event, false); |
| 3233 | } |
| 3234 | |
| 3235 | static int pmbus_regulator_get_status(struct regulator_dev *rdev) |
| 3236 | { |
| 3237 | struct device *dev = rdev_get_dev(rdev); |
| 3238 | struct i2c_client *client = to_i2c_client(dev->parent); |
| 3239 | u8 page = rdev_get_id(rdev); |
| 3240 | int status, ret; |
| 3241 | int event; |
| 3242 | |
| 3243 | guard(pmbus_lock)(client); |
| 3244 | |
| 3245 | status = pmbus_get_status(client, page, PMBUS_STATUS_WORD); |
| 3246 | if (status < 0) |
| 3247 | return status; |
| 3248 | |
| 3249 | if (status & PB_STATUS_OFF) |
| 3250 | return REGULATOR_STATUS_OFF; |
| 3251 | |
| 3252 | /* If regulator is ON & reports power good then return ON */ |
| 3253 | if (!(status & PB_STATUS_POWER_GOOD_N)) |
| 3254 | return REGULATOR_STATUS_ON; |
| 3255 | |
| 3256 | ret = _pmbus_get_flags(client, rdev_get_id(rdev), &status, &event, false); |
| 3257 | if (ret) |
| 3258 | return ret; |
| 3259 | |
| 3260 | if (status & (REGULATOR_ERROR_UNDER_VOLTAGE | REGULATOR_ERROR_OVER_CURRENT | |
| 3261 | REGULATOR_ERROR_REGULATION_OUT | REGULATOR_ERROR_FAIL | REGULATOR_ERROR_OVER_TEMP)) |
| 3262 | return REGULATOR_STATUS_ERROR; |
| 3263 | |
| 3264 | return REGULATOR_STATUS_UNDEFINED; |
| 3265 | } |
| 3266 | |
| 3267 | static int pmbus_regulator_get_low_margin(struct i2c_client *client, int page) |
| 3268 | { |
| 3269 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 3270 | struct pmbus_sensor s = { |
| 3271 | .page = page, |
| 3272 | .class = PSC_VOLTAGE_OUT, |
| 3273 | .convert = true, |
| 3274 | .data = -1, |
| 3275 | }; |
| 3276 | |
| 3277 | if (data->vout_low[page] < 0) { |
| 3278 | if (pmbus_check_word_register(client, page, PMBUS_MFR_VOUT_MIN)) |
| 3279 | s.data = _pmbus_read_word_data(client, page, 0xff, |
| 3280 | PMBUS_MFR_VOUT_MIN); |
| 3281 | if (s.data < 0) { |
| 3282 | s.data = _pmbus_read_word_data(client, page, 0xff, |
| 3283 | PMBUS_VOUT_MARGIN_LOW); |
| 3284 | if (s.data < 0) |
| 3285 | return s.data; |
| 3286 | } |
| 3287 | data->vout_low[page] = pmbus_reg2data(data, &s); |
| 3288 | } |
| 3289 | |
| 3290 | return data->vout_low[page]; |
| 3291 | } |
| 3292 | |
| 3293 | static int pmbus_regulator_get_high_margin(struct i2c_client *client, int page) |
| 3294 | { |
| 3295 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 3296 | struct pmbus_sensor s = { |
| 3297 | .page = page, |
| 3298 | .class = PSC_VOLTAGE_OUT, |
| 3299 | .convert = true, |
| 3300 | .data = -1, |
| 3301 | }; |
| 3302 | |
| 3303 | if (data->vout_high[page] < 0) { |
| 3304 | if (pmbus_check_word_register(client, page, PMBUS_MFR_VOUT_MAX)) |
| 3305 | s.data = _pmbus_read_word_data(client, page, 0xff, |
| 3306 | PMBUS_MFR_VOUT_MAX); |
| 3307 | if (s.data < 0) { |
| 3308 | s.data = _pmbus_read_word_data(client, page, 0xff, |
| 3309 | PMBUS_VOUT_MARGIN_HIGH); |
| 3310 | if (s.data < 0) |
| 3311 | return s.data; |
| 3312 | } |
| 3313 | data->vout_high[page] = pmbus_reg2data(data, &s); |
| 3314 | } |
| 3315 | |
| 3316 | return data->vout_high[page]; |
| 3317 | } |
| 3318 | |
| 3319 | static int pmbus_regulator_get_voltage(struct regulator_dev *rdev) |
| 3320 | { |
| 3321 | struct device *dev = rdev_get_dev(rdev); |
| 3322 | struct i2c_client *client = to_i2c_client(dev->parent); |
| 3323 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 3324 | struct pmbus_sensor s = { |
| 3325 | .page = rdev_get_id(rdev), |
| 3326 | .class = PSC_VOLTAGE_OUT, |
| 3327 | .convert = true, |
| 3328 | }; |
| 3329 | int voltage; |
| 3330 | |
| 3331 | scoped_guard(pmbus_lock, client) { |
| 3332 | s.data = _pmbus_read_word_data(client, s.page, 0xff, PMBUS_READ_VOUT); |
| 3333 | if (s.data < 0) |
| 3334 | return s.data; |
| 3335 | voltage = (int)pmbus_reg2data(data, &s); |
| 3336 | } |
| 3337 | |
| 3338 | return voltage * 1000; /* unit is uV */ |
| 3339 | } |
| 3340 | |
| 3341 | static int pmbus_regulator_set_voltage(struct regulator_dev *rdev, int min_uv, |
| 3342 | int max_uv, unsigned int *selector) |
| 3343 | { |
| 3344 | struct device *dev = rdev_get_dev(rdev); |
| 3345 | struct i2c_client *client = to_i2c_client(dev->parent); |
| 3346 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 3347 | struct pmbus_sensor s = { |
| 3348 | .page = rdev_get_id(rdev), |
| 3349 | .class = PSC_VOLTAGE_OUT, |
| 3350 | .convert = true, |
| 3351 | .data = -1, |
| 3352 | }; |
| 3353 | int val = DIV_ROUND_CLOSEST(min_uv, 1000); /* convert to mV */ |
| 3354 | int low, high; |
| 3355 | |
| 3356 | *selector = 0; |
| 3357 | |
| 3358 | guard(pmbus_lock)(client); |
| 3359 | |
| 3360 | low = pmbus_regulator_get_low_margin(client, s.page); |
| 3361 | if (low < 0) |
| 3362 | return low; |
| 3363 | |
| 3364 | high = pmbus_regulator_get_high_margin(client, s.page); |
| 3365 | if (high < 0) |
| 3366 | return high; |
| 3367 | |
| 3368 | /* Make sure we are within margins */ |
| 3369 | if (low > val) |
| 3370 | val = low; |
| 3371 | if (high < val) |
| 3372 | val = high; |
| 3373 | |
| 3374 | val = pmbus_data2reg(data, &s, val); |
| 3375 | |
| 3376 | return _pmbus_write_word_data(client, s.page, PMBUS_VOUT_COMMAND, (u16)val); |
| 3377 | } |
| 3378 | |
| 3379 | static int pmbus_regulator_list_voltage(struct regulator_dev *rdev, |
| 3380 | unsigned int selector) |
| 3381 | { |
| 3382 | struct device *dev = rdev_get_dev(rdev); |
| 3383 | struct i2c_client *client = to_i2c_client(dev->parent); |
| 3384 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 3385 | int val, low, high; |
| 3386 | |
| 3387 | if (data->flags & PMBUS_VOUT_PROTECTED) |
| 3388 | return 0; |
| 3389 | |
| 3390 | if (selector >= rdev->desc->n_voltages || |
| 3391 | selector < rdev->desc->linear_min_sel) |
| 3392 | return -EINVAL; |
| 3393 | |
| 3394 | selector -= rdev->desc->linear_min_sel; |
| 3395 | val = DIV_ROUND_CLOSEST(rdev->desc->min_uV + |
| 3396 | (rdev->desc->uV_step * selector), 1000); /* convert to mV */ |
| 3397 | |
| 3398 | guard(pmbus_lock)(client); |
| 3399 | |
| 3400 | low = pmbus_regulator_get_low_margin(client, rdev_get_id(rdev)); |
| 3401 | if (low < 0) |
| 3402 | return low; |
| 3403 | |
| 3404 | high = pmbus_regulator_get_high_margin(client, rdev_get_id(rdev)); |
| 3405 | if (high < 0) |
| 3406 | return high; |
| 3407 | |
| 3408 | if (val >= low && val <= high) |
| 3409 | return val * 1000; /* unit is uV */ |
| 3410 | |
| 3411 | return 0; |
| 3412 | } |
| 3413 | |
| 3414 | const struct regulator_ops pmbus_regulator_ops = { |
| 3415 | .enable = pmbus_regulator_enable, |
| 3416 | .disable = pmbus_regulator_disable, |
| 3417 | .is_enabled = pmbus_regulator_is_enabled, |
| 3418 | .get_error_flags = pmbus_regulator_get_error_flags, |
| 3419 | .get_status = pmbus_regulator_get_status, |
| 3420 | .get_voltage = pmbus_regulator_get_voltage, |
| 3421 | .set_voltage = pmbus_regulator_set_voltage, |
| 3422 | .list_voltage = pmbus_regulator_list_voltage, |
| 3423 | }; |
| 3424 | EXPORT_SYMBOL_NS_GPL(pmbus_regulator_ops, "PMBUS"); |
| 3425 | |
| 3426 | int pmbus_regulator_init_cb(struct regulator_dev *rdev, |
| 3427 | struct regulator_config *config) |
| 3428 | { |
| 3429 | struct pmbus_data *data = config->driver_data; |
| 3430 | struct regulation_constraints *constraints = rdev->constraints; |
| 3431 | |
| 3432 | if (data->flags & PMBUS_OP_PROTECTED) |
| 3433 | constraints->valid_ops_mask &= ~REGULATOR_CHANGE_STATUS; |
| 3434 | |
| 3435 | if (data->flags & PMBUS_VOUT_PROTECTED) |
| 3436 | constraints->valid_ops_mask &= ~REGULATOR_CHANGE_VOLTAGE; |
| 3437 | |
| 3438 | return 0; |
| 3439 | } |
| 3440 | EXPORT_SYMBOL_NS_GPL(pmbus_regulator_init_cb, "PMBUS"); |
| 3441 | |
| 3442 | static void pmbus_regulator_notify_work_cancel(void *data) |
| 3443 | { |
| 3444 | struct pmbus_data *pdata = data; |
| 3445 | |
| 3446 | cancel_work_sync(&pdata->regulator_notify_work); |
| 3447 | } |
| 3448 | |
| 3449 | static void pmbus_regulator_notify_worker(struct work_struct *work) |
| 3450 | { |
| 3451 | struct pmbus_data *data = |
| 3452 | container_of(work, struct pmbus_data, regulator_notify_work); |
| 3453 | int i, j; |
| 3454 | |
| 3455 | for (i = 0; i < data->info->pages; i++) { |
| 3456 | unsigned int event; |
| 3457 | |
| 3458 | event = atomic_xchg(&data->regulator_events[i], 0); |
| 3459 | if (!event) |
| 3460 | continue; |
| 3461 | |
| 3462 | for (j = 0; j < data->info->num_regulators; j++) { |
| 3463 | if (i != rdev_get_id(data->rdevs[j])) |
| 3464 | continue; |
| 3465 | while (event) { |
| 3466 | unsigned int _event = BIT(__ffs(event)); |
| 3467 | |
| 3468 | regulator_notifier_call_chain(data->rdevs[j], |
| 3469 | _event, NULL); |
| 3470 | event &= ~_event; |
| 3471 | } |
| 3472 | break; |
| 3473 | } |
| 3474 | } |
| 3475 | } |
| 3476 | |
| 3477 | static int pmbus_regulator_register(struct pmbus_data *data) |
| 3478 | { |
| 3479 | struct device *dev = data->dev; |
| 3480 | const struct pmbus_driver_info *info = data->info; |
| 3481 | const struct pmbus_platform_data *pdata = dev_get_platdata(dev); |
| 3482 | int i, ret; |
| 3483 | |
| 3484 | data->rdevs = devm_kzalloc(dev, sizeof(struct regulator_dev *) * info->num_regulators, |
| 3485 | GFP_KERNEL); |
| 3486 | if (!data->rdevs) |
| 3487 | return -ENOMEM; |
| 3488 | |
| 3489 | for (i = 0; i < info->num_regulators; i++) { |
| 3490 | struct regulator_config config = { }; |
| 3491 | |
| 3492 | config.dev = dev; |
| 3493 | config.driver_data = data; |
| 3494 | |
| 3495 | if (pdata && pdata->reg_init_data) |
| 3496 | config.init_data = &pdata->reg_init_data[i]; |
| 3497 | |
| 3498 | data->rdevs[i] = devm_regulator_register(dev, &info->reg_desc[i], |
| 3499 | &config); |
| 3500 | if (IS_ERR(data->rdevs[i])) |
| 3501 | return dev_err_probe(dev, PTR_ERR(data->rdevs[i]), |
| 3502 | "Failed to register %s regulator\n", |
| 3503 | info->reg_desc[i].name); |
| 3504 | } |
| 3505 | |
| 3506 | INIT_WORK(&data->regulator_notify_work, pmbus_regulator_notify_worker); |
| 3507 | |
| 3508 | ret = devm_add_action_or_reset(dev, pmbus_regulator_notify_work_cancel, data); |
| 3509 | if (ret) |
| 3510 | return ret; |
| 3511 | |
| 3512 | return 0; |
| 3513 | } |
| 3514 | |
| 3515 | static void pmbus_regulator_notify(struct pmbus_data *data, int page, int event) |
| 3516 | { |
| 3517 | atomic_or(event, &data->regulator_events[page]); |
| 3518 | schedule_work(&data->regulator_notify_work); |
| 3519 | } |
| 3520 | #else |
| 3521 | static int pmbus_regulator_register(struct pmbus_data *data) |
| 3522 | { |
| 3523 | return 0; |
| 3524 | } |
| 3525 | |
| 3526 | static void pmbus_regulator_notify(struct pmbus_data *data, int page, int event) |
| 3527 | { |
| 3528 | } |
| 3529 | #endif |
| 3530 | |
| 3531 | static int pmbus_write_smbalert_mask(struct i2c_client *client, u8 page, u8 reg, u8 val) |
| 3532 | { |
| 3533 | int ret; |
| 3534 | |
| 3535 | guard(pmbus_lock)(client); |
| 3536 | |
| 3537 | ret = _pmbus_write_word_data(client, page, PMBUS_SMBALERT_MASK, reg | (val << 8)); |
| 3538 | |
| 3539 | /* |
| 3540 | * Clear fault systematically in case writing PMBUS_SMBALERT_MASK |
| 3541 | * is not supported by the chip. |
| 3542 | */ |
| 3543 | pmbus_clear_fault_page(client, page); |
| 3544 | |
| 3545 | return ret; |
| 3546 | } |
| 3547 | |
| 3548 | void pmbus_check_and_notify_faults(struct i2c_client *client) |
| 3549 | { |
| 3550 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 3551 | int i, status, event; |
| 3552 | |
| 3553 | guard(pmbus_lock)(client); |
| 3554 | |
| 3555 | for (i = 0; i < data->info->pages; i++) { |
| 3556 | _pmbus_get_flags(client, i, &status, &event, true); |
| 3557 | |
| 3558 | if (event) |
| 3559 | pmbus_regulator_notify(data, i, event); |
| 3560 | } |
| 3561 | |
| 3562 | pmbus_clear_faults(client); |
| 3563 | } |
| 3564 | EXPORT_SYMBOL_NS_GPL(pmbus_check_and_notify_faults, "PMBUS"); |
| 3565 | |
| 3566 | static irqreturn_t pmbus_fault_handler(int irq, void *pdata) |
| 3567 | { |
| 3568 | struct pmbus_data *data = pdata; |
| 3569 | struct i2c_client *client = to_i2c_client(data->dev); |
| 3570 | |
| 3571 | pmbus_check_and_notify_faults(client); |
| 3572 | |
| 3573 | return IRQ_HANDLED; |
| 3574 | } |
| 3575 | |
| 3576 | static int pmbus_irq_setup(struct i2c_client *client, struct pmbus_data *data) |
| 3577 | { |
| 3578 | struct device *dev = &client->dev; |
| 3579 | const struct pmbus_status_category *cat; |
| 3580 | const struct pmbus_status_assoc *bit; |
| 3581 | int i, j, err, func; |
| 3582 | u8 mask; |
| 3583 | |
| 3584 | static const u8 misc_status[] = {PMBUS_STATUS_CML, PMBUS_STATUS_OTHER, |
| 3585 | PMBUS_STATUS_MFR_SPECIFIC, PMBUS_STATUS_FAN_12, |
| 3586 | PMBUS_STATUS_FAN_34}; |
| 3587 | |
| 3588 | if (!client->irq) |
| 3589 | return 0; |
| 3590 | |
| 3591 | for (i = 0; i < data->info->pages; i++) { |
| 3592 | func = data->info->func[i]; |
| 3593 | |
| 3594 | for (j = 0; j < ARRAY_SIZE(pmbus_status_flag_map); j++) { |
| 3595 | cat = &pmbus_status_flag_map[j]; |
| 3596 | if (!(func & cat->func)) |
| 3597 | continue; |
| 3598 | mask = 0; |
| 3599 | for (bit = cat->bits; bit->pflag; bit++) |
| 3600 | mask |= bit->pflag; |
| 3601 | |
| 3602 | err = pmbus_write_smbalert_mask(client, i, cat->reg, ~mask); |
| 3603 | if (err) |
| 3604 | dev_dbg_once(dev, "Failed to set smbalert for reg 0x%02x\n", |
| 3605 | cat->reg); |
| 3606 | } |
| 3607 | |
| 3608 | for (j = 0; j < ARRAY_SIZE(misc_status); j++) |
| 3609 | pmbus_write_smbalert_mask(client, i, misc_status[j], 0xff); |
| 3610 | } |
| 3611 | |
| 3612 | /* Register notifiers */ |
| 3613 | err = devm_request_threaded_irq(dev, client->irq, NULL, pmbus_fault_handler, |
| 3614 | IRQF_ONESHOT, "pmbus-irq", data); |
| 3615 | if (err) |
| 3616 | return err; |
| 3617 | |
| 3618 | return 0; |
| 3619 | } |
| 3620 | |
| 3621 | static struct dentry *pmbus_debugfs_dir; /* pmbus debugfs directory */ |
| 3622 | |
| 3623 | static int pmbus_debugfs_get(void *data, u64 *val) |
| 3624 | { |
| 3625 | struct pmbus_debugfs_entry *entry = data; |
| 3626 | struct i2c_client *client = entry->client; |
| 3627 | int rc; |
| 3628 | |
| 3629 | guard(pmbus_lock)(client); |
| 3630 | |
| 3631 | rc = _pmbus_read_byte_data(client, entry->page, entry->reg); |
| 3632 | if (rc < 0) |
| 3633 | return rc; |
| 3634 | |
| 3635 | *val = rc; |
| 3636 | |
| 3637 | return 0; |
| 3638 | } |
| 3639 | DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops, pmbus_debugfs_get, NULL, |
| 3640 | "0x%02llx\n"); |
| 3641 | |
| 3642 | static int pmbus_debugfs_get_revision(void *data, u64 *val) |
| 3643 | { |
| 3644 | struct pmbus_data *pdata = data; |
| 3645 | |
| 3646 | *val = pdata->revision; |
| 3647 | |
| 3648 | return 0; |
| 3649 | } |
| 3650 | DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_revision_ops, pmbus_debugfs_get_revision, NULL, |
| 3651 | "0x%02llx\n"); |
| 3652 | |
| 3653 | static int pmbus_debugfs_get_status(void *data, u64 *val) |
| 3654 | { |
| 3655 | struct pmbus_debugfs_entry *entry = data; |
| 3656 | struct i2c_client *client = entry->client; |
| 3657 | struct pmbus_data *pdata = i2c_get_clientdata(client); |
| 3658 | int rc; |
| 3659 | |
| 3660 | guard(pmbus_lock)(client); |
| 3661 | |
| 3662 | rc = pdata->read_status(client, entry->page); |
| 3663 | if (rc < 0) |
| 3664 | return rc; |
| 3665 | |
| 3666 | *val = rc; |
| 3667 | |
| 3668 | return 0; |
| 3669 | } |
| 3670 | DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops_status, pmbus_debugfs_get_status, |
| 3671 | NULL, "0x%04llx\n"); |
| 3672 | |
| 3673 | static ssize_t pmbus_debugfs_block_read(struct file *file, char __user *buf, |
| 3674 | size_t count, loff_t *ppos) |
| 3675 | { |
| 3676 | int rc; |
| 3677 | struct pmbus_debugfs_entry *entry = file->private_data; |
| 3678 | struct i2c_client *client = entry->client; |
| 3679 | char data[I2C_SMBUS_BLOCK_MAX + 2] = { 0 }; |
| 3680 | |
| 3681 | scoped_guard(pmbus_lock, client) { |
| 3682 | rc = pmbus_read_block_data(client, entry->page, entry->reg, data); |
| 3683 | if (rc < 0) |
| 3684 | return rc; |
| 3685 | } |
| 3686 | |
| 3687 | /* Add newline at the end of a read data */ |
| 3688 | data[rc] = '\n'; |
| 3689 | |
| 3690 | /* Include newline into the length */ |
| 3691 | rc += 1; |
| 3692 | |
| 3693 | return simple_read_from_buffer(buf, count, ppos, data, rc); |
| 3694 | } |
| 3695 | |
| 3696 | static const struct file_operations pmbus_debugfs_block_ops = { |
| 3697 | .llseek = noop_llseek, |
| 3698 | .read = pmbus_debugfs_block_read, |
| 3699 | .write = NULL, |
| 3700 | .open = simple_open, |
| 3701 | }; |
| 3702 | |
| 3703 | static void pmbus_remove_symlink(void *symlink) |
| 3704 | { |
| 3705 | debugfs_remove(symlink); |
| 3706 | } |
| 3707 | |
| 3708 | struct pmbus_debugfs_data { |
| 3709 | u8 reg; |
| 3710 | u32 flag; |
| 3711 | const char *name; |
| 3712 | }; |
| 3713 | |
| 3714 | static const struct pmbus_debugfs_data pmbus_debugfs_block_data[] = { |
| 3715 | { .reg = PMBUS_MFR_ID, .name = "mfr_id" }, |
| 3716 | { .reg = PMBUS_MFR_MODEL, .name = "mfr_model" }, |
| 3717 | { .reg = PMBUS_MFR_REVISION, .name = "mfr_revision" }, |
| 3718 | { .reg = PMBUS_MFR_LOCATION, .name = "mfr_location" }, |
| 3719 | { .reg = PMBUS_MFR_DATE, .name = "mfr_date" }, |
| 3720 | { .reg = PMBUS_MFR_SERIAL, .name = "mfr_serial" }, |
| 3721 | }; |
| 3722 | |
| 3723 | static const struct pmbus_debugfs_data pmbus_debugfs_status_data[] = { |
| 3724 | { .reg = PMBUS_STATUS_VOUT, .flag = PMBUS_HAVE_STATUS_VOUT, .name = "status%d_vout" }, |
| 3725 | { .reg = PMBUS_STATUS_IOUT, .flag = PMBUS_HAVE_STATUS_IOUT, .name = "status%d_iout" }, |
| 3726 | { .reg = PMBUS_STATUS_INPUT, .flag = PMBUS_HAVE_STATUS_INPUT, .name = "status%d_input" }, |
| 3727 | { .reg = PMBUS_STATUS_TEMPERATURE, .flag = PMBUS_HAVE_STATUS_TEMP, |
| 3728 | .name = "status%d_temp" }, |
| 3729 | { .reg = PMBUS_STATUS_FAN_12, .flag = PMBUS_HAVE_STATUS_FAN12, .name = "status%d_fan12" }, |
| 3730 | { .reg = PMBUS_STATUS_FAN_34, .flag = PMBUS_HAVE_STATUS_FAN34, .name = "status%d_fan34" }, |
| 3731 | { .reg = PMBUS_STATUS_CML, .name = "status%d_cml" }, |
| 3732 | { .reg = PMBUS_STATUS_OTHER, .name = "status%d_other" }, |
| 3733 | { .reg = PMBUS_STATUS_MFR_SPECIFIC, .name = "status%d_mfr" }, |
| 3734 | }; |
| 3735 | |
| 3736 | static void pmbus_init_debugfs(struct i2c_client *client, |
| 3737 | struct pmbus_data *data) |
| 3738 | { |
| 3739 | struct dentry *symlink_d, *debugfs = client->debugfs; |
| 3740 | struct pmbus_debugfs_entry *entries; |
| 3741 | const char *pathname, *symlink; |
| 3742 | char name[PMBUS_NAME_SIZE]; |
| 3743 | int page, i, idx = 0; |
| 3744 | |
| 3745 | /* |
| 3746 | * client->debugfs may be NULL or an ERR_PTR(). dentry_path_raw() |
| 3747 | * does not check if its parameters are valid, so validate |
| 3748 | * client->debugfs before using it. |
| 3749 | */ |
| 3750 | if (!pmbus_debugfs_dir || IS_ERR_OR_NULL(debugfs)) |
| 3751 | return; |
| 3752 | |
| 3753 | /* |
| 3754 | * Backwards compatibility: Create symlink from /pmbus/<hwmon_device> |
| 3755 | * to i2c debugfs directory. |
| 3756 | */ |
| 3757 | pathname = dentry_path_raw(debugfs, name, sizeof(name)); |
| 3758 | if (IS_ERR(pathname)) |
| 3759 | return; |
| 3760 | |
| 3761 | /* |
| 3762 | * The path returned by dentry_path_raw() starts with '/'. Prepend it |
| 3763 | * with ".." to get the symlink relative to the pmbus root directory. |
| 3764 | */ |
| 3765 | symlink = kasprintf(GFP_KERNEL, "..%s", pathname); |
| 3766 | if (!symlink) |
| 3767 | return; |
| 3768 | |
| 3769 | symlink_d = debugfs_create_symlink(dev_name(data->hwmon_dev), |
| 3770 | pmbus_debugfs_dir, symlink); |
| 3771 | kfree(symlink); |
| 3772 | |
| 3773 | devm_add_action_or_reset(data->dev, pmbus_remove_symlink, symlink_d); |
| 3774 | |
| 3775 | /* |
| 3776 | * Allocate the max possible entries we need. |
| 3777 | * device specific: |
| 3778 | * ARRAY_SIZE(pmbus_debugfs_block_data) + 2 |
| 3779 | * page specific: |
| 3780 | * ARRAY_SIZE(pmbus_debugfs_status_data) + 1 |
| 3781 | */ |
| 3782 | entries = devm_kcalloc(data->dev, |
| 3783 | ARRAY_SIZE(pmbus_debugfs_block_data) + 2 + |
| 3784 | data->info->pages * (ARRAY_SIZE(pmbus_debugfs_status_data) + 1), |
| 3785 | sizeof(*entries), GFP_KERNEL); |
| 3786 | if (!entries) |
| 3787 | return; |
| 3788 | |
| 3789 | guard(pmbus_lock)(client); |
| 3790 | |
| 3791 | /* |
| 3792 | * Add device-specific entries. |
| 3793 | * Please note that the PMBUS standard allows all registers to be |
| 3794 | * page-specific. |
| 3795 | * To reduce the number of debugfs entries for devices with many pages |
| 3796 | * assume that values of the following registers are the same for all |
| 3797 | * pages and report values only for page 0. |
| 3798 | */ |
| 3799 | if (!(data->flags & PMBUS_NO_CAPABILITY) && |
| 3800 | pmbus_check_byte_register(client, 0, PMBUS_CAPABILITY)) { |
| 3801 | entries[idx].client = client; |
| 3802 | entries[idx].page = 0; |
| 3803 | entries[idx].reg = PMBUS_CAPABILITY; |
| 3804 | debugfs_create_file("capability", 0444, debugfs, |
| 3805 | &entries[idx++], |
| 3806 | &pmbus_debugfs_ops); |
| 3807 | } |
| 3808 | if (data->have_pmbus_revision) |
| 3809 | debugfs_create_file("pmbus_revision", 0444, debugfs, data, |
| 3810 | &pmbus_debugfs_revision_ops); |
| 3811 | |
| 3812 | for (i = 0; i < ARRAY_SIZE(pmbus_debugfs_block_data); i++) { |
| 3813 | const struct pmbus_debugfs_data *d = &pmbus_debugfs_block_data[i]; |
| 3814 | |
| 3815 | if (pmbus_check_block_register(client, 0, d->reg)) { |
| 3816 | entries[idx].client = client; |
| 3817 | entries[idx].page = 0; |
| 3818 | entries[idx].reg = d->reg; |
| 3819 | debugfs_create_file(d->name, 0444, debugfs, |
| 3820 | &entries[idx++], |
| 3821 | &pmbus_debugfs_block_ops); |
| 3822 | } |
| 3823 | } |
| 3824 | |
| 3825 | /* Add page specific entries */ |
| 3826 | for (page = 0; page < data->info->pages; ++page) { |
| 3827 | /* Check accessibility of status register if it's not page 0 */ |
| 3828 | if (!page || pmbus_check_status_register(client, page)) { |
| 3829 | /* No need to set reg as we have special read op. */ |
| 3830 | entries[idx].client = client; |
| 3831 | entries[idx].page = page; |
| 3832 | scnprintf(name, PMBUS_NAME_SIZE, "status%d", page); |
| 3833 | debugfs_create_file(name, 0444, debugfs, |
| 3834 | &entries[idx++], |
| 3835 | &pmbus_debugfs_ops_status); |
| 3836 | } |
| 3837 | |
| 3838 | for (i = 0; i < ARRAY_SIZE(pmbus_debugfs_status_data); i++) { |
| 3839 | const struct pmbus_debugfs_data *d = |
| 3840 | &pmbus_debugfs_status_data[i]; |
| 3841 | |
| 3842 | if ((data->info->func[page] & d->flag) || |
| 3843 | (!d->flag && pmbus_check_byte_register(client, page, d->reg))) { |
| 3844 | entries[idx].client = client; |
| 3845 | entries[idx].page = page; |
| 3846 | entries[idx].reg = d->reg; |
| 3847 | scnprintf(name, PMBUS_NAME_SIZE, d->name, page); |
| 3848 | debugfs_create_file(name, 0444, debugfs, |
| 3849 | &entries[idx++], |
| 3850 | &pmbus_debugfs_ops); |
| 3851 | } |
| 3852 | } |
| 3853 | } |
| 3854 | } |
| 3855 | |
| 3856 | int pmbus_do_probe(struct i2c_client *client, struct pmbus_driver_info *info) |
| 3857 | { |
| 3858 | struct device *dev = &client->dev; |
| 3859 | const struct pmbus_platform_data *pdata = dev_get_platdata(dev); |
| 3860 | struct pmbus_data *data; |
| 3861 | size_t groups_num = 0; |
| 3862 | int ret; |
| 3863 | int i; |
| 3864 | char *name; |
| 3865 | |
| 3866 | if (!info) |
| 3867 | return -ENODEV; |
| 3868 | |
| 3869 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE |
| 3870 | | I2C_FUNC_SMBUS_BYTE_DATA |
| 3871 | | I2C_FUNC_SMBUS_WORD_DATA)) |
| 3872 | return -ENODEV; |
| 3873 | |
| 3874 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
| 3875 | if (!data) |
| 3876 | return -ENOMEM; |
| 3877 | |
| 3878 | if (info->groups) |
| 3879 | while (info->groups[groups_num]) |
| 3880 | groups_num++; |
| 3881 | |
| 3882 | data->groups = devm_kcalloc(dev, groups_num + 2, sizeof(void *), |
| 3883 | GFP_KERNEL); |
| 3884 | if (!data->groups) |
| 3885 | return -ENOMEM; |
| 3886 | |
| 3887 | i2c_set_clientdata(client, data); |
| 3888 | mutex_init(&data->update_lock); |
| 3889 | data->dev = dev; |
| 3890 | |
| 3891 | if (pdata) |
| 3892 | data->flags = pdata->flags; |
| 3893 | data->info = info; |
| 3894 | data->currpage = -1; |
| 3895 | data->currphase = -1; |
| 3896 | |
| 3897 | for (i = 0; i < ARRAY_SIZE(data->vout_low); i++) { |
| 3898 | data->vout_low[i] = -1; |
| 3899 | data->vout_high[i] = -1; |
| 3900 | } |
| 3901 | |
| 3902 | ret = pmbus_init_common(client, data, info); |
| 3903 | if (ret < 0) |
| 3904 | return ret; |
| 3905 | |
| 3906 | ret = pmbus_find_attributes(client, data); |
| 3907 | if (ret) |
| 3908 | return ret; |
| 3909 | |
| 3910 | /* |
| 3911 | * If there are no attributes, something is wrong. |
| 3912 | * Bail out instead of trying to register nothing. |
| 3913 | */ |
| 3914 | if (!data->num_attributes) { |
| 3915 | dev_err(dev, "No attributes found\n"); |
| 3916 | return -ENODEV; |
| 3917 | } |
| 3918 | |
| 3919 | name = devm_kstrdup(dev, client->name, GFP_KERNEL); |
| 3920 | if (!name) |
| 3921 | return -ENOMEM; |
| 3922 | strreplace(name, '-', '_'); |
| 3923 | |
| 3924 | data->groups[0] = &data->group; |
| 3925 | memcpy(data->groups + 1, info->groups, sizeof(void *) * groups_num); |
| 3926 | data->hwmon_dev = devm_hwmon_device_register_with_groups(dev, name, |
| 3927 | data, data->groups); |
| 3928 | if (IS_ERR(data->hwmon_dev)) { |
| 3929 | dev_err(dev, "Failed to register hwmon device\n"); |
| 3930 | return PTR_ERR(data->hwmon_dev); |
| 3931 | } |
| 3932 | |
| 3933 | ret = pmbus_regulator_register(data); |
| 3934 | if (ret) |
| 3935 | return ret; |
| 3936 | |
| 3937 | ret = pmbus_irq_setup(client, data); |
| 3938 | if (ret) |
| 3939 | return ret; |
| 3940 | |
| 3941 | pmbus_init_debugfs(client, data); |
| 3942 | |
| 3943 | return 0; |
| 3944 | } |
| 3945 | EXPORT_SYMBOL_NS_GPL(pmbus_do_probe, "PMBUS"); |
| 3946 | |
| 3947 | struct dentry *pmbus_get_debugfs_dir(struct i2c_client *client) |
| 3948 | { |
| 3949 | /* |
| 3950 | * client->debugfs may be an ERR_PTR(). Returning that to |
| 3951 | * the calling code would potentially require additional |
| 3952 | * complexity in the calling code and otherwise add no |
| 3953 | * value. Return NULL in that case. |
| 3954 | */ |
| 3955 | if (IS_ERR_OR_NULL(client->debugfs)) |
| 3956 | return NULL; |
| 3957 | return client->debugfs; |
| 3958 | } |
| 3959 | EXPORT_SYMBOL_NS_GPL(pmbus_get_debugfs_dir, "PMBUS"); |
| 3960 | |
| 3961 | void pmbus_lock(struct i2c_client *client) |
| 3962 | { |
| 3963 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 3964 | |
| 3965 | mutex_lock(&data->update_lock); |
| 3966 | } |
| 3967 | EXPORT_SYMBOL_NS_GPL(pmbus_lock, "PMBUS"); |
| 3968 | |
| 3969 | int pmbus_lock_interruptible(struct i2c_client *client) |
| 3970 | { |
| 3971 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 3972 | |
| 3973 | return mutex_lock_interruptible(&data->update_lock); |
| 3974 | } |
| 3975 | EXPORT_SYMBOL_NS_GPL(pmbus_lock_interruptible, "PMBUS"); |
| 3976 | |
| 3977 | void pmbus_unlock(struct i2c_client *client) |
| 3978 | { |
| 3979 | struct pmbus_data *data = i2c_get_clientdata(client); |
| 3980 | |
| 3981 | mutex_unlock(&data->update_lock); |
| 3982 | } |
| 3983 | EXPORT_SYMBOL_NS_GPL(pmbus_unlock, "PMBUS"); |
| 3984 | |
| 3985 | static int __init pmbus_core_init(void) |
| 3986 | { |
| 3987 | pmbus_debugfs_dir = debugfs_create_dir("pmbus", NULL); |
| 3988 | if (IS_ERR(pmbus_debugfs_dir)) |
| 3989 | pmbus_debugfs_dir = NULL; |
| 3990 | |
| 3991 | return 0; |
| 3992 | } |
| 3993 | |
| 3994 | static void __exit pmbus_core_exit(void) |
| 3995 | { |
| 3996 | debugfs_remove_recursive(pmbus_debugfs_dir); |
| 3997 | } |
| 3998 | |
| 3999 | module_init(pmbus_core_init); |
| 4000 | module_exit(pmbus_core_exit); |
| 4001 | |
| 4002 | MODULE_AUTHOR("Guenter Roeck"); |
| 4003 | MODULE_DESCRIPTION("PMBus core driver"); |
| 4004 | MODULE_LICENSE("GPL"); |