1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Hardware monitoring driver for UCD90xxx Sequencer and System Health 4 * Controller series 5 * 6 * Copyright (C) 2011 Ericsson AB. 7 */ 8 9 #include <linux/debugfs.h> 10 #include <linux/delay.h> 11 #include <linux/hex.h> 12 #include <linux/kernel.h> 13 #include <linux/module.h> 14 #include <linux/of.h> 15 #include <linux/init.h> 16 #include <linux/err.h> 17 #include <linux/slab.h> 18 #include <linux/i2c.h> 19 #include <linux/pmbus.h> 20 #include <linux/gpio/driver.h> 21 #include <linux/timekeeping.h> 22 #include "pmbus.h" 23 24 enum chips { ucd9000, ucd90120, ucd90124, ucd90160, ucd90320, ucd9090, 25 ucd90910 }; 26 27 #define UCD9000_MONITOR_CONFIG 0xd5 28 #define UCD9000_NUM_PAGES 0xd6 29 #define UCD9000_FAN_CONFIG_INDEX 0xe7 30 #define UCD9000_FAN_CONFIG 0xe8 31 #define UCD9000_MFR_STATUS 0xf3 32 #define UCD9000_GPIO_SELECT 0xfa 33 #define UCD9000_GPIO_CONFIG 0xfb 34 #define UCD9000_DEVICE_ID 0xfd 35 36 /* GPIO CONFIG bits */ 37 #define UCD9000_GPIO_CONFIG_ENABLE BIT(0) 38 #define UCD9000_GPIO_CONFIG_OUT_ENABLE BIT(1) 39 #define UCD9000_GPIO_CONFIG_OUT_VALUE BIT(2) 40 #define UCD9000_GPIO_CONFIG_STATUS BIT(3) 41 #define UCD9000_GPIO_INPUT 0 42 #define UCD9000_GPIO_OUTPUT 1 43 44 #define UCD9000_MON_TYPE(x) (((x) >> 5) & 0x07) 45 #define UCD9000_MON_PAGE(x) ((x) & 0x1f) 46 47 #define UCD9000_MON_VOLTAGE 1 48 #define UCD9000_MON_TEMPERATURE 2 49 #define UCD9000_MON_CURRENT 3 50 #define UCD9000_MON_VOLTAGE_HW 4 51 52 #define UCD9000_NUM_FAN 4 53 54 #define UCD9000_GPIO_NAME_LEN 16 55 #define UCD9090_NUM_GPIOS 23 56 #define UCD901XX_NUM_GPIOS 26 57 #define UCD90320_NUM_GPIOS 84 58 #define UCD90910_NUM_GPIOS 26 59 60 #define UCD9000_DEBUGFS_NAME_LEN 24 61 #define UCD9000_GPI_COUNT 8 62 #define UCD90320_GPI_COUNT 32 63 64 struct ucd9000_data { 65 u8 fan_data[UCD9000_NUM_FAN][I2C_SMBUS_BLOCK_MAX]; 66 struct pmbus_driver_info info; 67 #ifdef CONFIG_GPIOLIB 68 struct gpio_chip gpio; 69 #endif 70 struct dentry *debugfs; 71 }; 72 #define to_ucd9000_data(_info) container_of(_info, struct ucd9000_data, info) 73 74 struct ucd9000_debugfs_entry { 75 struct i2c_client *client; 76 u8 index; 77 }; 78 79 /* 80 * It has been observed that the UCD90320 randomly fails register access when 81 * doing another access right on the back of a register write. To mitigate this 82 * make sure that there is a minimum delay between a write access and the 83 * following access. The 500 is based on experimental data. At a delay of 84 * 350us the issue seems to go away. Add a bit of extra margin to allow for 85 * system to system differences. 86 */ 87 #define UCD90320_WAIT_DELAY_US 500 88 89 static int ucd9000_get_fan_config(struct i2c_client *client, int fan) 90 { 91 int fan_config = 0; 92 struct ucd9000_data *data 93 = to_ucd9000_data(pmbus_get_driver_info(client)); 94 95 if (data->fan_data[fan][3] & 1) 96 fan_config |= PB_FAN_2_INSTALLED; /* Use lower bit position */ 97 98 /* Pulses/revolution */ 99 fan_config |= (data->fan_data[fan][3] & 0x06) >> 1; 100 101 return fan_config; 102 } 103 104 static int ucd9000_read_byte_data(struct i2c_client *client, int page, int reg) 105 { 106 int ret = 0; 107 int fan_config; 108 109 switch (reg) { 110 case PMBUS_FAN_CONFIG_12: 111 if (page > 0) 112 return -ENXIO; 113 114 ret = ucd9000_get_fan_config(client, 0); 115 if (ret < 0) 116 return ret; 117 fan_config = ret << 4; 118 ret = ucd9000_get_fan_config(client, 1); 119 if (ret < 0) 120 return ret; 121 fan_config |= ret; 122 ret = fan_config; 123 break; 124 case PMBUS_FAN_CONFIG_34: 125 if (page > 0) 126 return -ENXIO; 127 128 ret = ucd9000_get_fan_config(client, 2); 129 if (ret < 0) 130 return ret; 131 fan_config = ret << 4; 132 ret = ucd9000_get_fan_config(client, 3); 133 if (ret < 0) 134 return ret; 135 fan_config |= ret; 136 ret = fan_config; 137 break; 138 default: 139 ret = -ENODATA; 140 break; 141 } 142 return ret; 143 } 144 145 static const struct i2c_device_id ucd9000_id[] = { 146 {"ucd9000", ucd9000}, 147 {"ucd90120", ucd90120}, 148 {"ucd90124", ucd90124}, 149 {"ucd90160", ucd90160}, 150 {"ucd90320", ucd90320}, 151 {"ucd9090", ucd9090}, 152 {"ucd90910", ucd90910}, 153 {} 154 }; 155 MODULE_DEVICE_TABLE(i2c, ucd9000_id); 156 157 static const struct of_device_id __maybe_unused ucd9000_of_match[] = { 158 { 159 .compatible = "ti,ucd9000", 160 .data = (void *)ucd9000 161 }, 162 { 163 .compatible = "ti,ucd90120", 164 .data = (void *)ucd90120 165 }, 166 { 167 .compatible = "ti,ucd90124", 168 .data = (void *)ucd90124 169 }, 170 { 171 .compatible = "ti,ucd90160", 172 .data = (void *)ucd90160 173 }, 174 { 175 .compatible = "ti,ucd90320", 176 .data = (void *)ucd90320 177 }, 178 { 179 .compatible = "ti,ucd9090", 180 .data = (void *)ucd9090 181 }, 182 { 183 .compatible = "ti,ucd90910", 184 .data = (void *)ucd90910 185 }, 186 { }, 187 }; 188 MODULE_DEVICE_TABLE(of, ucd9000_of_match); 189 190 #ifdef CONFIG_GPIOLIB 191 static int ucd9000_gpio_read_config(struct i2c_client *client, 192 unsigned int offset) 193 { 194 int ret; 195 196 /* No page set required */ 197 ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_SELECT, offset); 198 if (ret < 0) 199 return ret; 200 201 return i2c_smbus_read_byte_data(client, UCD9000_GPIO_CONFIG); 202 } 203 204 static int ucd9000_gpio_get(struct gpio_chip *gc, unsigned int offset) 205 { 206 struct i2c_client *client = gpiochip_get_data(gc); 207 int ret; 208 209 ret = ucd9000_gpio_read_config(client, offset); 210 if (ret < 0) 211 return ret; 212 213 return !!(ret & UCD9000_GPIO_CONFIG_STATUS); 214 } 215 216 static int ucd9000_gpio_set(struct gpio_chip *gc, unsigned int offset, 217 int value) 218 { 219 struct i2c_client *client = gpiochip_get_data(gc); 220 int ret; 221 222 ret = ucd9000_gpio_read_config(client, offset); 223 if (ret < 0) { 224 dev_dbg(&client->dev, "failed to read GPIO %d config: %d\n", 225 offset, ret); 226 return ret; 227 } 228 229 if (value) { 230 if (ret & UCD9000_GPIO_CONFIG_OUT_VALUE) 231 return 0; 232 233 ret |= UCD9000_GPIO_CONFIG_OUT_VALUE; 234 } else { 235 if (!(ret & UCD9000_GPIO_CONFIG_OUT_VALUE)) 236 return 0; 237 238 ret &= ~UCD9000_GPIO_CONFIG_OUT_VALUE; 239 } 240 241 ret |= UCD9000_GPIO_CONFIG_ENABLE; 242 243 /* Page set not required */ 244 ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, ret); 245 if (ret < 0) { 246 dev_dbg(&client->dev, "Failed to write GPIO %d config: %d\n", 247 offset, ret); 248 return ret; 249 } 250 251 ret &= ~UCD9000_GPIO_CONFIG_ENABLE; 252 253 ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, ret); 254 if (ret < 0) 255 dev_dbg(&client->dev, "Failed to write GPIO %d config: %d\n", 256 offset, ret); 257 258 return ret; 259 } 260 261 static int ucd9000_gpio_get_direction(struct gpio_chip *gc, 262 unsigned int offset) 263 { 264 struct i2c_client *client = gpiochip_get_data(gc); 265 int ret; 266 267 ret = ucd9000_gpio_read_config(client, offset); 268 if (ret < 0) 269 return ret; 270 271 return !(ret & UCD9000_GPIO_CONFIG_OUT_ENABLE); 272 } 273 274 static int ucd9000_gpio_set_direction(struct gpio_chip *gc, 275 unsigned int offset, bool direction_out, 276 int requested_out) 277 { 278 struct i2c_client *client = gpiochip_get_data(gc); 279 int ret, config, out_val; 280 281 ret = ucd9000_gpio_read_config(client, offset); 282 if (ret < 0) 283 return ret; 284 285 if (direction_out) { 286 out_val = requested_out ? UCD9000_GPIO_CONFIG_OUT_VALUE : 0; 287 288 if (ret & UCD9000_GPIO_CONFIG_OUT_ENABLE) { 289 if ((ret & UCD9000_GPIO_CONFIG_OUT_VALUE) == out_val) 290 return 0; 291 } else { 292 ret |= UCD9000_GPIO_CONFIG_OUT_ENABLE; 293 } 294 295 if (out_val) 296 ret |= UCD9000_GPIO_CONFIG_OUT_VALUE; 297 else 298 ret &= ~UCD9000_GPIO_CONFIG_OUT_VALUE; 299 300 } else { 301 if (!(ret & UCD9000_GPIO_CONFIG_OUT_ENABLE)) 302 return 0; 303 304 ret &= ~UCD9000_GPIO_CONFIG_OUT_ENABLE; 305 } 306 307 ret |= UCD9000_GPIO_CONFIG_ENABLE; 308 config = ret; 309 310 /* Page set not required */ 311 ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, config); 312 if (ret < 0) 313 return ret; 314 315 config &= ~UCD9000_GPIO_CONFIG_ENABLE; 316 317 return i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, config); 318 } 319 320 static int ucd9000_gpio_direction_input(struct gpio_chip *gc, 321 unsigned int offset) 322 { 323 return ucd9000_gpio_set_direction(gc, offset, UCD9000_GPIO_INPUT, 0); 324 } 325 326 static int ucd9000_gpio_direction_output(struct gpio_chip *gc, 327 unsigned int offset, int val) 328 { 329 return ucd9000_gpio_set_direction(gc, offset, UCD9000_GPIO_OUTPUT, 330 val); 331 } 332 333 static void ucd9000_probe_gpio(struct i2c_client *client, 334 const struct i2c_device_id *mid, 335 struct ucd9000_data *data) 336 { 337 int rc; 338 339 switch (mid->driver_data) { 340 case ucd9090: 341 data->gpio.ngpio = UCD9090_NUM_GPIOS; 342 break; 343 case ucd90120: 344 case ucd90124: 345 case ucd90160: 346 data->gpio.ngpio = UCD901XX_NUM_GPIOS; 347 break; 348 case ucd90320: 349 data->gpio.ngpio = UCD90320_NUM_GPIOS; 350 break; 351 case ucd90910: 352 data->gpio.ngpio = UCD90910_NUM_GPIOS; 353 break; 354 default: 355 return; /* GPIO support is optional. */ 356 } 357 358 /* 359 * Pinmux support has not been added to the new gpio_chip. 360 * This support should be added when possible given the mux 361 * behavior of these IO devices. 362 */ 363 data->gpio.label = client->name; 364 data->gpio.get_direction = ucd9000_gpio_get_direction; 365 data->gpio.direction_input = ucd9000_gpio_direction_input; 366 data->gpio.direction_output = ucd9000_gpio_direction_output; 367 data->gpio.get = ucd9000_gpio_get; 368 data->gpio.set = ucd9000_gpio_set; 369 data->gpio.can_sleep = true; 370 data->gpio.base = -1; 371 data->gpio.parent = &client->dev; 372 373 rc = devm_gpiochip_add_data(&client->dev, &data->gpio, client); 374 if (rc) 375 dev_warn(&client->dev, "Could not add gpiochip: %d\n", rc); 376 } 377 #else 378 static void ucd9000_probe_gpio(struct i2c_client *client, 379 const struct i2c_device_id *mid, 380 struct ucd9000_data *data) 381 { 382 } 383 #endif /* CONFIG_GPIOLIB */ 384 385 #ifdef CONFIG_DEBUG_FS 386 static int ucd9000_get_mfr_status(struct i2c_client *client, u8 *buffer) 387 { 388 int ret = pmbus_set_page(client, 0, 0xff); 389 390 if (ret < 0) 391 return ret; 392 393 return i2c_smbus_read_block_data(client, UCD9000_MFR_STATUS, buffer); 394 } 395 396 static int ucd9000_debugfs_show_mfr_status_bit(void *data, u64 *val) 397 { 398 struct ucd9000_debugfs_entry *entry = data; 399 struct i2c_client *client = entry->client; 400 u8 buffer[I2C_SMBUS_BLOCK_MAX]; 401 int ret, i; 402 403 ret = ucd9000_get_mfr_status(client, buffer); 404 if (ret < 0) 405 return ret; 406 407 /* 408 * GPI fault bits are in sets of 8, two bytes from end of response. 409 */ 410 i = ret - 3 - entry->index / 8; 411 if (i >= 0) 412 *val = !!(buffer[i] & BIT(entry->index % 8)); 413 414 return 0; 415 } 416 DEFINE_DEBUGFS_ATTRIBUTE(ucd9000_debugfs_mfr_status_bit, 417 ucd9000_debugfs_show_mfr_status_bit, NULL, "%1lld\n"); 418 419 static ssize_t ucd9000_debugfs_read_mfr_status(struct file *file, 420 char __user *buf, size_t count, 421 loff_t *ppos) 422 { 423 struct i2c_client *client = file->private_data; 424 u8 buffer[I2C_SMBUS_BLOCK_MAX]; 425 char str[(I2C_SMBUS_BLOCK_MAX * 2) + 2]; 426 char *res; 427 int rc; 428 429 rc = ucd9000_get_mfr_status(client, buffer); 430 if (rc < 0) 431 return rc; 432 433 res = bin2hex(str, buffer, min(rc, I2C_SMBUS_BLOCK_MAX)); 434 *res++ = '\n'; 435 *res = 0; 436 437 return simple_read_from_buffer(buf, count, ppos, str, res - str); 438 } 439 440 static const struct file_operations ucd9000_debugfs_show_mfr_status_fops = { 441 .llseek = noop_llseek, 442 .read = ucd9000_debugfs_read_mfr_status, 443 .open = simple_open, 444 }; 445 446 static int ucd9000_init_debugfs(struct i2c_client *client, 447 const struct i2c_device_id *mid, 448 struct ucd9000_data *data) 449 { 450 struct dentry *debugfs; 451 struct ucd9000_debugfs_entry *entries; 452 int i, gpi_count; 453 char name[UCD9000_DEBUGFS_NAME_LEN]; 454 455 debugfs = pmbus_get_debugfs_dir(client); 456 if (!debugfs) 457 return -ENOENT; 458 459 data->debugfs = debugfs_create_dir(client->name, debugfs); 460 461 /* 462 * Of the chips this driver supports, only the UCD9090, UCD90160, 463 * UCD90320, and UCD90910 report GPI faults in their MFR_STATUS 464 * register, so only create the GPI fault debugfs attributes for those 465 * chips. 466 */ 467 if (mid->driver_data == ucd9090 || mid->driver_data == ucd90160 || 468 mid->driver_data == ucd90320 || mid->driver_data == ucd90910) { 469 gpi_count = mid->driver_data == ucd90320 ? UCD90320_GPI_COUNT 470 : UCD9000_GPI_COUNT; 471 entries = devm_kcalloc(&client->dev, 472 gpi_count, sizeof(*entries), 473 GFP_KERNEL); 474 if (!entries) 475 return -ENOMEM; 476 477 for (i = 0; i < gpi_count; i++) { 478 entries[i].client = client; 479 entries[i].index = i; 480 scnprintf(name, UCD9000_DEBUGFS_NAME_LEN, 481 "gpi%d_alarm", i + 1); 482 debugfs_create_file(name, 0444, data->debugfs, 483 &entries[i], 484 &ucd9000_debugfs_mfr_status_bit); 485 } 486 } 487 488 scnprintf(name, UCD9000_DEBUGFS_NAME_LEN, "mfr_status"); 489 debugfs_create_file(name, 0444, data->debugfs, client, 490 &ucd9000_debugfs_show_mfr_status_fops); 491 492 return 0; 493 } 494 #else 495 static int ucd9000_init_debugfs(struct i2c_client *client, 496 const struct i2c_device_id *mid, 497 struct ucd9000_data *data) 498 { 499 return 0; 500 } 501 #endif /* CONFIG_DEBUG_FS */ 502 503 static int ucd9000_probe(struct i2c_client *client) 504 { 505 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1]; 506 struct ucd9000_data *data; 507 struct pmbus_driver_info *info; 508 const struct i2c_device_id *mid; 509 enum chips chip; 510 int i, ret; 511 512 if (!i2c_check_functionality(client->adapter, 513 I2C_FUNC_SMBUS_BYTE_DATA | 514 I2C_FUNC_SMBUS_BLOCK_DATA)) 515 return -ENODEV; 516 517 ret = i2c_smbus_read_block_data(client, UCD9000_DEVICE_ID, 518 block_buffer); 519 if (ret < 0) { 520 dev_err(&client->dev, "Failed to read device ID\n"); 521 return ret; 522 } 523 block_buffer[ret] = '\0'; 524 dev_info(&client->dev, "Device ID %s\n", block_buffer); 525 526 for (mid = ucd9000_id; mid->name[0]; mid++) { 527 if (!strncasecmp(mid->name, block_buffer, strlen(mid->name))) 528 break; 529 } 530 if (!mid->name[0]) { 531 dev_err(&client->dev, "Unsupported device\n"); 532 return -ENODEV; 533 } 534 535 if (client->dev.of_node) 536 chip = (uintptr_t)of_device_get_match_data(&client->dev); 537 else 538 chip = mid->driver_data; 539 540 if (chip != ucd9000 && strcmp(client->name, mid->name) != 0) 541 dev_notice(&client->dev, 542 "Device mismatch: Configured %s, detected %s\n", 543 client->name, mid->name); 544 545 data = devm_kzalloc(&client->dev, sizeof(struct ucd9000_data), 546 GFP_KERNEL); 547 if (!data) 548 return -ENOMEM; 549 info = &data->info; 550 551 ret = i2c_smbus_read_byte_data(client, UCD9000_NUM_PAGES); 552 if (ret < 0) { 553 dev_err(&client->dev, 554 "Failed to read number of active pages\n"); 555 return ret; 556 } 557 info->pages = ret; 558 if (!info->pages) { 559 dev_err(&client->dev, "No pages configured\n"); 560 return -ENODEV; 561 } 562 563 /* The internal temperature sensor is always active */ 564 info->func[0] = PMBUS_HAVE_TEMP; 565 566 /* Everything else is configurable */ 567 ret = i2c_smbus_read_block_data(client, UCD9000_MONITOR_CONFIG, 568 block_buffer); 569 if (ret <= 0) { 570 dev_err(&client->dev, "Failed to read configuration data\n"); 571 return -ENODEV; 572 } 573 for (i = 0; i < ret; i++) { 574 int page = UCD9000_MON_PAGE(block_buffer[i]); 575 576 if (page >= info->pages) 577 continue; 578 579 switch (UCD9000_MON_TYPE(block_buffer[i])) { 580 case UCD9000_MON_VOLTAGE: 581 case UCD9000_MON_VOLTAGE_HW: 582 info->func[page] |= PMBUS_HAVE_VOUT 583 | PMBUS_HAVE_STATUS_VOUT; 584 break; 585 case UCD9000_MON_TEMPERATURE: 586 info->func[page] |= PMBUS_HAVE_TEMP2 587 | PMBUS_HAVE_STATUS_TEMP; 588 break; 589 case UCD9000_MON_CURRENT: 590 info->func[page] |= PMBUS_HAVE_IOUT 591 | PMBUS_HAVE_STATUS_IOUT; 592 break; 593 default: 594 break; 595 } 596 } 597 598 /* Fan configuration */ 599 if (mid->driver_data == ucd90124) { 600 for (i = 0; i < UCD9000_NUM_FAN; i++) { 601 i2c_smbus_write_byte_data(client, 602 UCD9000_FAN_CONFIG_INDEX, i); 603 ret = i2c_smbus_read_block_data(client, 604 UCD9000_FAN_CONFIG, 605 data->fan_data[i]); 606 if (ret < 0) 607 return ret; 608 } 609 i2c_smbus_write_byte_data(client, UCD9000_FAN_CONFIG_INDEX, 0); 610 611 info->read_byte_data = ucd9000_read_byte_data; 612 info->func[0] |= PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12 613 | PMBUS_HAVE_FAN34 | PMBUS_HAVE_STATUS_FAN34; 614 } else if (mid->driver_data == ucd90320) { 615 /* Delay SMBus operations after a write */ 616 info->write_delay = UCD90320_WAIT_DELAY_US; 617 } 618 619 ucd9000_probe_gpio(client, mid, data); 620 621 ret = pmbus_do_probe(client, info); 622 if (ret) 623 return ret; 624 625 ret = ucd9000_init_debugfs(client, mid, data); 626 if (ret) 627 dev_warn(&client->dev, "Failed to register debugfs: %d\n", 628 ret); 629 630 return 0; 631 } 632 633 /* This is the driver that will be inserted */ 634 static struct i2c_driver ucd9000_driver = { 635 .driver = { 636 .name = "ucd9000", 637 .of_match_table = of_match_ptr(ucd9000_of_match), 638 }, 639 .probe = ucd9000_probe, 640 .id_table = ucd9000_id, 641 }; 642 643 module_i2c_driver(ucd9000_driver); 644 645 MODULE_AUTHOR("Guenter Roeck"); 646 MODULE_DESCRIPTION("PMBus driver for TI UCD90xxx"); 647 MODULE_LICENSE("GPL"); 648 MODULE_IMPORT_NS("PMBUS"); 649