19d2ecfb7SGuenter Roeck /* 29d2ecfb7SGuenter Roeck * Hardware monitoring driver for ucd9200 series Digital PWM System Controllers 39d2ecfb7SGuenter Roeck * 49d2ecfb7SGuenter Roeck * Copyright (C) 2011 Ericsson AB. 59d2ecfb7SGuenter Roeck * 69d2ecfb7SGuenter Roeck * This program is free software; you can redistribute it and/or modify 79d2ecfb7SGuenter Roeck * it under the terms of the GNU General Public License as published by 89d2ecfb7SGuenter Roeck * the Free Software Foundation; either version 2 of the License, or 99d2ecfb7SGuenter Roeck * (at your option) any later version. 109d2ecfb7SGuenter Roeck * 119d2ecfb7SGuenter Roeck * This program is distributed in the hope that it will be useful, 129d2ecfb7SGuenter Roeck * but WITHOUT ANY WARRANTY; without even the implied warranty of 139d2ecfb7SGuenter Roeck * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 149d2ecfb7SGuenter Roeck * GNU General Public License for more details. 159d2ecfb7SGuenter Roeck * 169d2ecfb7SGuenter Roeck * You should have received a copy of the GNU General Public License 179d2ecfb7SGuenter Roeck * along with this program; if not, write to the Free Software 189d2ecfb7SGuenter Roeck * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 199d2ecfb7SGuenter Roeck */ 209d2ecfb7SGuenter Roeck 219d2ecfb7SGuenter Roeck #include <linux/kernel.h> 229d2ecfb7SGuenter Roeck #include <linux/module.h> 239d2ecfb7SGuenter Roeck #include <linux/init.h> 249d2ecfb7SGuenter Roeck #include <linux/err.h> 259d2ecfb7SGuenter Roeck #include <linux/slab.h> 269d2ecfb7SGuenter Roeck #include <linux/i2c.h> 279d2ecfb7SGuenter Roeck #include <linux/i2c/pmbus.h> 289d2ecfb7SGuenter Roeck #include "pmbus.h" 299d2ecfb7SGuenter Roeck 309d2ecfb7SGuenter Roeck #define UCD9200_PHASE_INFO 0xd2 319d2ecfb7SGuenter Roeck #define UCD9200_DEVICE_ID 0xfd 329d2ecfb7SGuenter Roeck 339d2ecfb7SGuenter Roeck enum chips { ucd9200, ucd9220, ucd9222, ucd9224, ucd9240, ucd9244, ucd9246, 349d2ecfb7SGuenter Roeck ucd9248 }; 359d2ecfb7SGuenter Roeck 369d2ecfb7SGuenter Roeck static const struct i2c_device_id ucd9200_id[] = { 379d2ecfb7SGuenter Roeck {"ucd9200", ucd9200}, 389d2ecfb7SGuenter Roeck {"ucd9220", ucd9220}, 399d2ecfb7SGuenter Roeck {"ucd9222", ucd9222}, 409d2ecfb7SGuenter Roeck {"ucd9224", ucd9224}, 419d2ecfb7SGuenter Roeck {"ucd9240", ucd9240}, 429d2ecfb7SGuenter Roeck {"ucd9244", ucd9244}, 439d2ecfb7SGuenter Roeck {"ucd9246", ucd9246}, 449d2ecfb7SGuenter Roeck {"ucd9248", ucd9248}, 459d2ecfb7SGuenter Roeck {} 469d2ecfb7SGuenter Roeck }; 479d2ecfb7SGuenter Roeck MODULE_DEVICE_TABLE(i2c, ucd9200_id); 489d2ecfb7SGuenter Roeck 499d2ecfb7SGuenter Roeck static int ucd9200_probe(struct i2c_client *client, 509d2ecfb7SGuenter Roeck const struct i2c_device_id *id) 519d2ecfb7SGuenter Roeck { 529d2ecfb7SGuenter Roeck u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1]; 539d2ecfb7SGuenter Roeck struct pmbus_driver_info *info; 549d2ecfb7SGuenter Roeck const struct i2c_device_id *mid; 559d2ecfb7SGuenter Roeck int i, j, ret; 569d2ecfb7SGuenter Roeck 579d2ecfb7SGuenter Roeck if (!i2c_check_functionality(client->adapter, 589d2ecfb7SGuenter Roeck I2C_FUNC_SMBUS_BYTE_DATA | 599d2ecfb7SGuenter Roeck I2C_FUNC_SMBUS_BLOCK_DATA)) 609d2ecfb7SGuenter Roeck return -ENODEV; 619d2ecfb7SGuenter Roeck 629d2ecfb7SGuenter Roeck ret = i2c_smbus_read_block_data(client, UCD9200_DEVICE_ID, 639d2ecfb7SGuenter Roeck block_buffer); 649d2ecfb7SGuenter Roeck if (ret < 0) { 659d2ecfb7SGuenter Roeck dev_err(&client->dev, "Failed to read device ID\n"); 669d2ecfb7SGuenter Roeck return ret; 679d2ecfb7SGuenter Roeck } 689d2ecfb7SGuenter Roeck block_buffer[ret] = '\0'; 699d2ecfb7SGuenter Roeck dev_info(&client->dev, "Device ID %s\n", block_buffer); 709d2ecfb7SGuenter Roeck 71f020b007SJean Delvare for (mid = ucd9200_id; mid->name[0]; mid++) { 729d2ecfb7SGuenter Roeck if (!strncasecmp(mid->name, block_buffer, strlen(mid->name))) 739d2ecfb7SGuenter Roeck break; 749d2ecfb7SGuenter Roeck } 75f020b007SJean Delvare if (!mid->name[0]) { 769d2ecfb7SGuenter Roeck dev_err(&client->dev, "Unsupported device\n"); 779d2ecfb7SGuenter Roeck return -ENODEV; 789d2ecfb7SGuenter Roeck } 799d2ecfb7SGuenter Roeck if (id->driver_data != ucd9200 && id->driver_data != mid->driver_data) 809d2ecfb7SGuenter Roeck dev_notice(&client->dev, 819d2ecfb7SGuenter Roeck "Device mismatch: Configured %s, detected %s\n", 829d2ecfb7SGuenter Roeck id->name, mid->name); 839d2ecfb7SGuenter Roeck 849d2ecfb7SGuenter Roeck info = kzalloc(sizeof(struct pmbus_driver_info), GFP_KERNEL); 859d2ecfb7SGuenter Roeck if (!info) 869d2ecfb7SGuenter Roeck return -ENOMEM; 879d2ecfb7SGuenter Roeck 889d2ecfb7SGuenter Roeck ret = i2c_smbus_read_block_data(client, UCD9200_PHASE_INFO, 899d2ecfb7SGuenter Roeck block_buffer); 909d2ecfb7SGuenter Roeck if (ret < 0) { 919d2ecfb7SGuenter Roeck dev_err(&client->dev, "Failed to read phase information\n"); 929d2ecfb7SGuenter Roeck goto out; 939d2ecfb7SGuenter Roeck } 949d2ecfb7SGuenter Roeck 959d2ecfb7SGuenter Roeck /* 969d2ecfb7SGuenter Roeck * Calculate number of configured pages (rails) from PHASE_INFO 979d2ecfb7SGuenter Roeck * register. 989d2ecfb7SGuenter Roeck * Rails have to be sequential, so we can abort after finding 999d2ecfb7SGuenter Roeck * the first unconfigured rail. 1009d2ecfb7SGuenter Roeck */ 1019d2ecfb7SGuenter Roeck info->pages = 0; 1029d2ecfb7SGuenter Roeck for (i = 0; i < ret; i++) { 1039d2ecfb7SGuenter Roeck if (!block_buffer[i]) 1049d2ecfb7SGuenter Roeck break; 1059d2ecfb7SGuenter Roeck info->pages++; 1069d2ecfb7SGuenter Roeck } 1079d2ecfb7SGuenter Roeck if (!info->pages) { 1089d2ecfb7SGuenter Roeck dev_err(&client->dev, "No rails configured\n"); 1099d2ecfb7SGuenter Roeck ret = -ENODEV; 1109d2ecfb7SGuenter Roeck goto out; 1119d2ecfb7SGuenter Roeck } 1129d2ecfb7SGuenter Roeck dev_info(&client->dev, "%d rails configured\n", info->pages); 1139d2ecfb7SGuenter Roeck 1149d2ecfb7SGuenter Roeck /* 1159d2ecfb7SGuenter Roeck * Set PHASE registers on all pages to 0xff to ensure that phase 1169d2ecfb7SGuenter Roeck * specific commands will apply to all phases of a given page (rail). 1179d2ecfb7SGuenter Roeck * This only affects the READ_IOUT and READ_TEMPERATURE2 registers. 1189d2ecfb7SGuenter Roeck * READ_IOUT will return the sum of currents of all phases of a rail, 1199d2ecfb7SGuenter Roeck * and READ_TEMPERATURE2 will return the maximum temperature detected 1209d2ecfb7SGuenter Roeck * for the the phases of the rail. 1219d2ecfb7SGuenter Roeck */ 1229d2ecfb7SGuenter Roeck for (i = 0; i < info->pages; i++) { 1239d2ecfb7SGuenter Roeck /* 1249d2ecfb7SGuenter Roeck * Setting PAGE & PHASE fails once in a while for no obvious 1259d2ecfb7SGuenter Roeck * reason, so we need to retry a couple of times. 1269d2ecfb7SGuenter Roeck */ 1279d2ecfb7SGuenter Roeck for (j = 0; j < 3; j++) { 1289d2ecfb7SGuenter Roeck ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i); 1299d2ecfb7SGuenter Roeck if (ret < 0) 1309d2ecfb7SGuenter Roeck continue; 1319d2ecfb7SGuenter Roeck ret = i2c_smbus_write_byte_data(client, PMBUS_PHASE, 1329d2ecfb7SGuenter Roeck 0xff); 1339d2ecfb7SGuenter Roeck if (ret < 0) 1349d2ecfb7SGuenter Roeck continue; 1359d2ecfb7SGuenter Roeck break; 1369d2ecfb7SGuenter Roeck } 1379d2ecfb7SGuenter Roeck if (ret < 0) { 1389d2ecfb7SGuenter Roeck dev_err(&client->dev, 1399d2ecfb7SGuenter Roeck "Failed to initialize PHASE registers\n"); 1409d2ecfb7SGuenter Roeck goto out; 1419d2ecfb7SGuenter Roeck } 1429d2ecfb7SGuenter Roeck } 1439d2ecfb7SGuenter Roeck if (info->pages > 1) 1449d2ecfb7SGuenter Roeck i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0); 1459d2ecfb7SGuenter Roeck 1469d2ecfb7SGuenter Roeck info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT | 1479d2ecfb7SGuenter Roeck PMBUS_HAVE_IIN | PMBUS_HAVE_PIN | 1489d2ecfb7SGuenter Roeck PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | 1499d2ecfb7SGuenter Roeck PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT | 1509d2ecfb7SGuenter Roeck PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP | 1519d2ecfb7SGuenter Roeck PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP; 1529d2ecfb7SGuenter Roeck 1539d2ecfb7SGuenter Roeck for (i = 1; i < info->pages; i++) 1549d2ecfb7SGuenter Roeck info->func[i] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | 1559d2ecfb7SGuenter Roeck PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT | 1569d2ecfb7SGuenter Roeck PMBUS_HAVE_POUT | 1579d2ecfb7SGuenter Roeck PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP; 1589d2ecfb7SGuenter Roeck 1599d2ecfb7SGuenter Roeck /* ucd9240 supports a single fan */ 1609d2ecfb7SGuenter Roeck if (mid->driver_data == ucd9240) 1619d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12; 1629d2ecfb7SGuenter Roeck 1639d2ecfb7SGuenter Roeck ret = pmbus_do_probe(client, mid, info); 1649d2ecfb7SGuenter Roeck if (ret < 0) 1659d2ecfb7SGuenter Roeck goto out; 1669d2ecfb7SGuenter Roeck return 0; 1679d2ecfb7SGuenter Roeck out: 1689d2ecfb7SGuenter Roeck kfree(info); 1699d2ecfb7SGuenter Roeck return ret; 1709d2ecfb7SGuenter Roeck } 1719d2ecfb7SGuenter Roeck 1729d2ecfb7SGuenter Roeck static int ucd9200_remove(struct i2c_client *client) 1739d2ecfb7SGuenter Roeck { 1749d2ecfb7SGuenter Roeck const struct pmbus_driver_info *info; 1759d2ecfb7SGuenter Roeck 1769d2ecfb7SGuenter Roeck info = pmbus_get_driver_info(client); 177866cf12aSGuenter Roeck pmbus_do_remove(client); 1789d2ecfb7SGuenter Roeck kfree(info); 179866cf12aSGuenter Roeck return 0; 1809d2ecfb7SGuenter Roeck } 1819d2ecfb7SGuenter Roeck 1829d2ecfb7SGuenter Roeck 1839d2ecfb7SGuenter Roeck /* This is the driver that will be inserted */ 1849d2ecfb7SGuenter Roeck static struct i2c_driver ucd9200_driver = { 1859d2ecfb7SGuenter Roeck .driver = { 1869d2ecfb7SGuenter Roeck .name = "ucd9200", 1879d2ecfb7SGuenter Roeck }, 1889d2ecfb7SGuenter Roeck .probe = ucd9200_probe, 1899d2ecfb7SGuenter Roeck .remove = ucd9200_remove, 1909d2ecfb7SGuenter Roeck .id_table = ucd9200_id, 1919d2ecfb7SGuenter Roeck }; 1929d2ecfb7SGuenter Roeck 193*f0967eeaSAxel Lin module_i2c_driver(ucd9200_driver); 1949d2ecfb7SGuenter Roeck 1959d2ecfb7SGuenter Roeck MODULE_AUTHOR("Guenter Roeck"); 1969d2ecfb7SGuenter Roeck MODULE_DESCRIPTION("PMBus driver for TI UCD922x, UCD924x"); 1979d2ecfb7SGuenter Roeck MODULE_LICENSE("GPL"); 198