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