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