1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright 2017 IBM Corp. 4 */ 5 6 #include <linux/bitfield.h> 7 #include <linux/bitops.h> 8 #include <linux/debugfs.h> 9 #include <linux/device.h> 10 #include <linux/fs.h> 11 #include <linux/i2c.h> 12 #include <linux/jiffies.h> 13 #include <linux/leds.h> 14 #include <linux/module.h> 15 #include <linux/mutex.h> 16 #include <linux/of_device.h> 17 #include <linux/pmbus.h> 18 19 #include "pmbus.h" 20 21 #define CFFPS_FRU_CMD 0x9A 22 #define CFFPS_PN_CMD 0x9B 23 #define CFFPS_HEADER_CMD 0x9C 24 #define CFFPS_SN_CMD 0x9E 25 #define CFFPS_MAX_POWER_OUT_CMD 0xA7 26 #define CFFPS_CCIN_CMD 0xBD 27 #define CFFPS_FW_CMD 0xFA 28 #define CFFPS1_FW_NUM_BYTES 4 29 #define CFFPS2_FW_NUM_WORDS 3 30 #define CFFPS_SYS_CONFIG_CMD 0xDA 31 #define CFFPS_12VCS_VOUT_CMD 0xDE 32 33 #define CFFPS_INPUT_HISTORY_CMD 0xD6 34 #define CFFPS_INPUT_HISTORY_SIZE 100 35 36 #define CFFPS_CCIN_REVISION GENMASK(7, 0) 37 #define CFFPS_CCIN_REVISION_LEGACY 0xde 38 #define CFFPS_CCIN_VERSION GENMASK(15, 8) 39 #define CFFPS_CCIN_VERSION_1 0x2b 40 #define CFFPS_CCIN_VERSION_2 0x2e 41 #define CFFPS_CCIN_VERSION_3 0x51 42 43 /* STATUS_MFR_SPECIFIC bits */ 44 #define CFFPS_MFR_FAN_FAULT BIT(0) 45 #define CFFPS_MFR_THERMAL_FAULT BIT(1) 46 #define CFFPS_MFR_OV_FAULT BIT(2) 47 #define CFFPS_MFR_UV_FAULT BIT(3) 48 #define CFFPS_MFR_PS_KILL BIT(4) 49 #define CFFPS_MFR_OC_FAULT BIT(5) 50 #define CFFPS_MFR_VAUX_FAULT BIT(6) 51 #define CFFPS_MFR_CURRENT_SHARE_WARNING BIT(7) 52 53 #define CFFPS_LED_BLINK BIT(0) 54 #define CFFPS_LED_ON BIT(1) 55 #define CFFPS_LED_OFF BIT(2) 56 #define CFFPS_BLINK_RATE_MS 250 57 58 enum { 59 CFFPS_DEBUGFS_INPUT_HISTORY = 0, 60 CFFPS_DEBUGFS_FRU, 61 CFFPS_DEBUGFS_PN, 62 CFFPS_DEBUGFS_HEADER, 63 CFFPS_DEBUGFS_SN, 64 CFFPS_DEBUGFS_MAX_POWER_OUT, 65 CFFPS_DEBUGFS_CCIN, 66 CFFPS_DEBUGFS_FW, 67 CFFPS_DEBUGFS_ON_OFF_CONFIG, 68 CFFPS_DEBUGFS_NUM_ENTRIES 69 }; 70 71 enum versions { cffps1, cffps2, cffps_unknown }; 72 73 struct ibm_cffps_input_history { 74 struct mutex update_lock; 75 unsigned long last_update; 76 77 u8 byte_count; 78 u8 data[CFFPS_INPUT_HISTORY_SIZE]; 79 }; 80 81 struct ibm_cffps { 82 enum versions version; 83 struct i2c_client *client; 84 85 struct ibm_cffps_input_history input_history; 86 87 int debugfs_entries[CFFPS_DEBUGFS_NUM_ENTRIES]; 88 89 char led_name[32]; 90 u8 led_state; 91 struct led_classdev led; 92 }; 93 94 #define to_psu(x, y) container_of((x), struct ibm_cffps, debugfs_entries[(y)]) 95 96 static ssize_t ibm_cffps_read_input_history(struct ibm_cffps *psu, 97 char __user *buf, size_t count, 98 loff_t *ppos) 99 { 100 int rc; 101 u8 msgbuf0[1] = { CFFPS_INPUT_HISTORY_CMD }; 102 u8 msgbuf1[CFFPS_INPUT_HISTORY_SIZE + 1] = { 0 }; 103 struct i2c_msg msg[2] = { 104 { 105 .addr = psu->client->addr, 106 .flags = psu->client->flags, 107 .len = 1, 108 .buf = msgbuf0, 109 }, { 110 .addr = psu->client->addr, 111 .flags = psu->client->flags | I2C_M_RD, 112 .len = CFFPS_INPUT_HISTORY_SIZE + 1, 113 .buf = msgbuf1, 114 }, 115 }; 116 117 if (!*ppos) { 118 mutex_lock(&psu->input_history.update_lock); 119 if (time_after(jiffies, psu->input_history.last_update + HZ)) { 120 /* 121 * Use a raw i2c transfer, since we need more bytes 122 * than Linux I2C supports through smbus xfr (only 32). 123 */ 124 rc = i2c_transfer(psu->client->adapter, msg, 2); 125 if (rc < 0) { 126 mutex_unlock(&psu->input_history.update_lock); 127 return rc; 128 } 129 130 psu->input_history.byte_count = msgbuf1[0]; 131 memcpy(psu->input_history.data, &msgbuf1[1], 132 CFFPS_INPUT_HISTORY_SIZE); 133 psu->input_history.last_update = jiffies; 134 } 135 136 mutex_unlock(&psu->input_history.update_lock); 137 } 138 139 return simple_read_from_buffer(buf, count, ppos, 140 psu->input_history.data, 141 psu->input_history.byte_count); 142 } 143 144 static ssize_t ibm_cffps_debugfs_read(struct file *file, char __user *buf, 145 size_t count, loff_t *ppos) 146 { 147 u8 cmd; 148 int i, rc; 149 int *idxp = file->private_data; 150 int idx = *idxp; 151 struct ibm_cffps *psu = to_psu(idxp, idx); 152 char data[I2C_SMBUS_BLOCK_MAX + 2] = { 0 }; 153 154 pmbus_set_page(psu->client, 0, 0xff); 155 156 switch (idx) { 157 case CFFPS_DEBUGFS_INPUT_HISTORY: 158 return ibm_cffps_read_input_history(psu, buf, count, ppos); 159 case CFFPS_DEBUGFS_FRU: 160 cmd = CFFPS_FRU_CMD; 161 break; 162 case CFFPS_DEBUGFS_PN: 163 cmd = CFFPS_PN_CMD; 164 break; 165 case CFFPS_DEBUGFS_HEADER: 166 cmd = CFFPS_HEADER_CMD; 167 break; 168 case CFFPS_DEBUGFS_SN: 169 cmd = CFFPS_SN_CMD; 170 break; 171 case CFFPS_DEBUGFS_MAX_POWER_OUT: 172 rc = i2c_smbus_read_word_swapped(psu->client, 173 CFFPS_MAX_POWER_OUT_CMD); 174 if (rc < 0) 175 return rc; 176 177 rc = snprintf(data, I2C_SMBUS_BLOCK_MAX, "%d", rc); 178 goto done; 179 case CFFPS_DEBUGFS_CCIN: 180 rc = i2c_smbus_read_word_swapped(psu->client, CFFPS_CCIN_CMD); 181 if (rc < 0) 182 return rc; 183 184 rc = snprintf(data, 5, "%04X", rc); 185 goto done; 186 case CFFPS_DEBUGFS_FW: 187 switch (psu->version) { 188 case cffps1: 189 for (i = 0; i < CFFPS1_FW_NUM_BYTES; ++i) { 190 rc = i2c_smbus_read_byte_data(psu->client, 191 CFFPS_FW_CMD + 192 i); 193 if (rc < 0) 194 return rc; 195 196 snprintf(&data[i * 2], 3, "%02X", rc); 197 } 198 199 rc = i * 2; 200 break; 201 case cffps2: 202 for (i = 0; i < CFFPS2_FW_NUM_WORDS; ++i) { 203 rc = i2c_smbus_read_word_data(psu->client, 204 CFFPS_FW_CMD + 205 i); 206 if (rc < 0) 207 return rc; 208 209 snprintf(&data[i * 4], 5, "%04X", rc); 210 } 211 212 rc = i * 4; 213 break; 214 default: 215 return -EOPNOTSUPP; 216 } 217 goto done; 218 case CFFPS_DEBUGFS_ON_OFF_CONFIG: 219 rc = i2c_smbus_read_byte_data(psu->client, 220 PMBUS_ON_OFF_CONFIG); 221 if (rc < 0) 222 return rc; 223 224 rc = snprintf(data, 3, "%02x", rc); 225 goto done; 226 default: 227 return -EINVAL; 228 } 229 230 rc = i2c_smbus_read_block_data(psu->client, cmd, data); 231 if (rc < 0) 232 return rc; 233 234 done: 235 data[rc] = '\n'; 236 rc += 2; 237 238 return simple_read_from_buffer(buf, count, ppos, data, rc); 239 } 240 241 static ssize_t ibm_cffps_debugfs_write(struct file *file, 242 const char __user *buf, size_t count, 243 loff_t *ppos) 244 { 245 u8 data; 246 ssize_t rc; 247 int *idxp = file->private_data; 248 int idx = *idxp; 249 struct ibm_cffps *psu = to_psu(idxp, idx); 250 251 switch (idx) { 252 case CFFPS_DEBUGFS_ON_OFF_CONFIG: 253 pmbus_set_page(psu->client, 0, 0xff); 254 255 rc = simple_write_to_buffer(&data, 1, ppos, buf, count); 256 if (rc <= 0) 257 return rc; 258 259 rc = i2c_smbus_write_byte_data(psu->client, 260 PMBUS_ON_OFF_CONFIG, data); 261 if (rc) 262 return rc; 263 264 rc = 1; 265 break; 266 default: 267 return -EINVAL; 268 } 269 270 return rc; 271 } 272 273 static const struct file_operations ibm_cffps_fops = { 274 .llseek = noop_llseek, 275 .read = ibm_cffps_debugfs_read, 276 .write = ibm_cffps_debugfs_write, 277 .open = simple_open, 278 }; 279 280 static int ibm_cffps_read_byte_data(struct i2c_client *client, int page, 281 int reg) 282 { 283 int rc, mfr; 284 285 switch (reg) { 286 case PMBUS_STATUS_VOUT: 287 case PMBUS_STATUS_IOUT: 288 case PMBUS_STATUS_TEMPERATURE: 289 case PMBUS_STATUS_FAN_12: 290 rc = pmbus_read_byte_data(client, page, reg); 291 if (rc < 0) 292 return rc; 293 294 mfr = pmbus_read_byte_data(client, page, 295 PMBUS_STATUS_MFR_SPECIFIC); 296 if (mfr < 0) 297 /* 298 * Return the status register instead of an error, 299 * since we successfully read status. 300 */ 301 return rc; 302 303 /* Add MFR_SPECIFIC bits to the standard pmbus status regs. */ 304 if (reg == PMBUS_STATUS_FAN_12) { 305 if (mfr & CFFPS_MFR_FAN_FAULT) 306 rc |= PB_FAN_FAN1_FAULT; 307 } else if (reg == PMBUS_STATUS_TEMPERATURE) { 308 if (mfr & CFFPS_MFR_THERMAL_FAULT) 309 rc |= PB_TEMP_OT_FAULT; 310 } else if (reg == PMBUS_STATUS_VOUT) { 311 if (mfr & (CFFPS_MFR_OV_FAULT | CFFPS_MFR_VAUX_FAULT)) 312 rc |= PB_VOLTAGE_OV_FAULT; 313 if (mfr & CFFPS_MFR_UV_FAULT) 314 rc |= PB_VOLTAGE_UV_FAULT; 315 } else if (reg == PMBUS_STATUS_IOUT) { 316 if (mfr & CFFPS_MFR_OC_FAULT) 317 rc |= PB_IOUT_OC_FAULT; 318 if (mfr & CFFPS_MFR_CURRENT_SHARE_WARNING) 319 rc |= PB_CURRENT_SHARE_FAULT; 320 } 321 break; 322 default: 323 rc = -ENODATA; 324 break; 325 } 326 327 return rc; 328 } 329 330 static int ibm_cffps_read_word_data(struct i2c_client *client, int page, 331 int phase, int reg) 332 { 333 int rc, mfr; 334 335 switch (reg) { 336 case PMBUS_STATUS_WORD: 337 rc = pmbus_read_word_data(client, page, phase, reg); 338 if (rc < 0) 339 return rc; 340 341 mfr = pmbus_read_byte_data(client, page, 342 PMBUS_STATUS_MFR_SPECIFIC); 343 if (mfr < 0) 344 /* 345 * Return the status register instead of an error, 346 * since we successfully read status. 347 */ 348 return rc; 349 350 if (mfr & CFFPS_MFR_PS_KILL) 351 rc |= PB_STATUS_OFF; 352 break; 353 case PMBUS_VIRT_READ_VMON: 354 rc = pmbus_read_word_data(client, page, phase, 355 CFFPS_12VCS_VOUT_CMD); 356 break; 357 default: 358 rc = -ENODATA; 359 break; 360 } 361 362 return rc; 363 } 364 365 static int ibm_cffps_led_brightness_set(struct led_classdev *led_cdev, 366 enum led_brightness brightness) 367 { 368 int rc; 369 u8 next_led_state; 370 struct ibm_cffps *psu = container_of(led_cdev, struct ibm_cffps, led); 371 372 if (brightness == LED_OFF) { 373 next_led_state = CFFPS_LED_OFF; 374 } else { 375 brightness = LED_FULL; 376 377 if (psu->led_state != CFFPS_LED_BLINK) 378 next_led_state = CFFPS_LED_ON; 379 else 380 next_led_state = CFFPS_LED_BLINK; 381 } 382 383 dev_dbg(&psu->client->dev, "LED brightness set: %d. Command: %d.\n", 384 brightness, next_led_state); 385 386 pmbus_set_page(psu->client, 0, 0xff); 387 388 rc = i2c_smbus_write_byte_data(psu->client, CFFPS_SYS_CONFIG_CMD, 389 next_led_state); 390 if (rc < 0) 391 return rc; 392 393 psu->led_state = next_led_state; 394 led_cdev->brightness = brightness; 395 396 return 0; 397 } 398 399 static int ibm_cffps_led_blink_set(struct led_classdev *led_cdev, 400 unsigned long *delay_on, 401 unsigned long *delay_off) 402 { 403 int rc; 404 struct ibm_cffps *psu = container_of(led_cdev, struct ibm_cffps, led); 405 406 dev_dbg(&psu->client->dev, "LED blink set.\n"); 407 408 pmbus_set_page(psu->client, 0, 0xff); 409 410 rc = i2c_smbus_write_byte_data(psu->client, CFFPS_SYS_CONFIG_CMD, 411 CFFPS_LED_BLINK); 412 if (rc < 0) 413 return rc; 414 415 psu->led_state = CFFPS_LED_BLINK; 416 led_cdev->brightness = LED_FULL; 417 *delay_on = CFFPS_BLINK_RATE_MS; 418 *delay_off = CFFPS_BLINK_RATE_MS; 419 420 return 0; 421 } 422 423 static void ibm_cffps_create_led_class(struct ibm_cffps *psu) 424 { 425 int rc; 426 struct i2c_client *client = psu->client; 427 struct device *dev = &client->dev; 428 429 snprintf(psu->led_name, sizeof(psu->led_name), "%s-%02x", client->name, 430 client->addr); 431 psu->led.name = psu->led_name; 432 psu->led.max_brightness = LED_FULL; 433 psu->led.brightness_set_blocking = ibm_cffps_led_brightness_set; 434 psu->led.blink_set = ibm_cffps_led_blink_set; 435 436 rc = devm_led_classdev_register(dev, &psu->led); 437 if (rc) 438 dev_warn(dev, "failed to register led class: %d\n", rc); 439 else 440 i2c_smbus_write_byte_data(client, CFFPS_SYS_CONFIG_CMD, 441 CFFPS_LED_OFF); 442 } 443 444 static struct pmbus_driver_info ibm_cffps_info[] = { 445 [cffps1] = { 446 .pages = 1, 447 .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT | 448 PMBUS_HAVE_PIN | PMBUS_HAVE_FAN12 | PMBUS_HAVE_TEMP | 449 PMBUS_HAVE_TEMP2 | PMBUS_HAVE_TEMP3 | 450 PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT | 451 PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_STATUS_TEMP | 452 PMBUS_HAVE_STATUS_FAN12, 453 .read_byte_data = ibm_cffps_read_byte_data, 454 .read_word_data = ibm_cffps_read_word_data, 455 }, 456 [cffps2] = { 457 .pages = 2, 458 .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT | 459 PMBUS_HAVE_PIN | PMBUS_HAVE_FAN12 | PMBUS_HAVE_TEMP | 460 PMBUS_HAVE_TEMP2 | PMBUS_HAVE_TEMP3 | 461 PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT | 462 PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_STATUS_TEMP | 463 PMBUS_HAVE_STATUS_FAN12 | PMBUS_HAVE_VMON, 464 .func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT | 465 PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_TEMP3 | 466 PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT, 467 .read_byte_data = ibm_cffps_read_byte_data, 468 .read_word_data = ibm_cffps_read_word_data, 469 }, 470 }; 471 472 static struct pmbus_platform_data ibm_cffps_pdata = { 473 .flags = PMBUS_SKIP_STATUS_CHECK, 474 }; 475 476 static int ibm_cffps_probe(struct i2c_client *client, 477 const struct i2c_device_id *id) 478 { 479 int i, rc; 480 enum versions vs = cffps_unknown; 481 struct dentry *debugfs; 482 struct dentry *ibm_cffps_dir; 483 struct ibm_cffps *psu; 484 const void *md = of_device_get_match_data(&client->dev); 485 486 if (md) 487 vs = (enum versions)md; 488 else if (id) 489 vs = (enum versions)id->driver_data; 490 491 if (vs == cffps_unknown) { 492 u16 ccin_revision = 0; 493 u16 ccin_version = CFFPS_CCIN_VERSION_1; 494 int ccin = i2c_smbus_read_word_swapped(client, CFFPS_CCIN_CMD); 495 496 if (ccin > 0) { 497 ccin_revision = FIELD_GET(CFFPS_CCIN_REVISION, ccin); 498 ccin_version = FIELD_GET(CFFPS_CCIN_VERSION, ccin); 499 } 500 501 switch (ccin_version) { 502 default: 503 case CFFPS_CCIN_VERSION_1: 504 vs = cffps1; 505 break; 506 case CFFPS_CCIN_VERSION_2: 507 vs = cffps2; 508 break; 509 case CFFPS_CCIN_VERSION_3: 510 if (ccin_revision == CFFPS_CCIN_REVISION_LEGACY) 511 vs = cffps1; 512 else 513 vs = cffps2; 514 break; 515 } 516 517 /* Set the client name to include the version number. */ 518 snprintf(client->name, I2C_NAME_SIZE, "cffps%d", vs + 1); 519 } 520 521 client->dev.platform_data = &ibm_cffps_pdata; 522 rc = pmbus_do_probe(client, id, &ibm_cffps_info[vs]); 523 if (rc) 524 return rc; 525 526 /* 527 * Don't fail the probe if there isn't enough memory for leds and 528 * debugfs. 529 */ 530 psu = devm_kzalloc(&client->dev, sizeof(*psu), GFP_KERNEL); 531 if (!psu) 532 return 0; 533 534 psu->version = vs; 535 psu->client = client; 536 mutex_init(&psu->input_history.update_lock); 537 psu->input_history.last_update = jiffies - HZ; 538 539 ibm_cffps_create_led_class(psu); 540 541 /* Don't fail the probe if we can't create debugfs */ 542 debugfs = pmbus_get_debugfs_dir(client); 543 if (!debugfs) 544 return 0; 545 546 ibm_cffps_dir = debugfs_create_dir(client->name, debugfs); 547 if (!ibm_cffps_dir) 548 return 0; 549 550 for (i = 0; i < CFFPS_DEBUGFS_NUM_ENTRIES; ++i) 551 psu->debugfs_entries[i] = i; 552 553 debugfs_create_file("input_history", 0444, ibm_cffps_dir, 554 &psu->debugfs_entries[CFFPS_DEBUGFS_INPUT_HISTORY], 555 &ibm_cffps_fops); 556 debugfs_create_file("fru", 0444, ibm_cffps_dir, 557 &psu->debugfs_entries[CFFPS_DEBUGFS_FRU], 558 &ibm_cffps_fops); 559 debugfs_create_file("part_number", 0444, ibm_cffps_dir, 560 &psu->debugfs_entries[CFFPS_DEBUGFS_PN], 561 &ibm_cffps_fops); 562 debugfs_create_file("header", 0444, ibm_cffps_dir, 563 &psu->debugfs_entries[CFFPS_DEBUGFS_HEADER], 564 &ibm_cffps_fops); 565 debugfs_create_file("serial_number", 0444, ibm_cffps_dir, 566 &psu->debugfs_entries[CFFPS_DEBUGFS_SN], 567 &ibm_cffps_fops); 568 debugfs_create_file("max_power_out", 0444, ibm_cffps_dir, 569 &psu->debugfs_entries[CFFPS_DEBUGFS_MAX_POWER_OUT], 570 &ibm_cffps_fops); 571 debugfs_create_file("ccin", 0444, ibm_cffps_dir, 572 &psu->debugfs_entries[CFFPS_DEBUGFS_CCIN], 573 &ibm_cffps_fops); 574 debugfs_create_file("fw_version", 0444, ibm_cffps_dir, 575 &psu->debugfs_entries[CFFPS_DEBUGFS_FW], 576 &ibm_cffps_fops); 577 debugfs_create_file("on_off_config", 0644, ibm_cffps_dir, 578 &psu->debugfs_entries[CFFPS_DEBUGFS_ON_OFF_CONFIG], 579 &ibm_cffps_fops); 580 581 return 0; 582 } 583 584 static const struct i2c_device_id ibm_cffps_id[] = { 585 { "ibm_cffps1", cffps1 }, 586 { "ibm_cffps2", cffps2 }, 587 { "ibm_cffps", cffps_unknown }, 588 {} 589 }; 590 MODULE_DEVICE_TABLE(i2c, ibm_cffps_id); 591 592 static const struct of_device_id ibm_cffps_of_match[] = { 593 { 594 .compatible = "ibm,cffps1", 595 .data = (void *)cffps1 596 }, 597 { 598 .compatible = "ibm,cffps2", 599 .data = (void *)cffps2 600 }, 601 { 602 .compatible = "ibm,cffps", 603 .data = (void *)cffps_unknown 604 }, 605 {} 606 }; 607 MODULE_DEVICE_TABLE(of, ibm_cffps_of_match); 608 609 static struct i2c_driver ibm_cffps_driver = { 610 .driver = { 611 .name = "ibm-cffps", 612 .of_match_table = ibm_cffps_of_match, 613 }, 614 .probe = ibm_cffps_probe, 615 .remove = pmbus_do_remove, 616 .id_table = ibm_cffps_id, 617 }; 618 619 module_i2c_driver(ibm_cffps_driver); 620 621 MODULE_AUTHOR("Eddie James"); 622 MODULE_DESCRIPTION("PMBus driver for IBM Common Form Factor power supplies"); 623 MODULE_LICENSE("GPL"); 624