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