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