19d2ecfb7SGuenter Roeck /* 29d2ecfb7SGuenter Roeck * Hardware monitoring driver for PMBus devices 39d2ecfb7SGuenter Roeck * 49d2ecfb7SGuenter Roeck * Copyright (c) 2010, 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/mutex.h> 279d2ecfb7SGuenter Roeck #include <linux/i2c.h> 289d2ecfb7SGuenter Roeck #include "pmbus.h" 299d2ecfb7SGuenter Roeck 309d2ecfb7SGuenter Roeck /* 319d2ecfb7SGuenter Roeck * Find sensor groups and status registers on each page. 329d2ecfb7SGuenter Roeck */ 339d2ecfb7SGuenter Roeck static void pmbus_find_sensor_groups(struct i2c_client *client, 349d2ecfb7SGuenter Roeck struct pmbus_driver_info *info) 359d2ecfb7SGuenter Roeck { 369d2ecfb7SGuenter Roeck int page; 379d2ecfb7SGuenter Roeck 389d2ecfb7SGuenter Roeck /* Sensors detected on page 0 only */ 399d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, 0, PMBUS_READ_VIN)) 409d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_VIN; 419d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, 0, PMBUS_READ_VCAP)) 429d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_VCAP; 439d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, 0, PMBUS_READ_IIN)) 449d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_IIN; 459d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, 0, PMBUS_READ_PIN)) 469d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_PIN; 479d2ecfb7SGuenter Roeck if (info->func[0] 489d2ecfb7SGuenter Roeck && pmbus_check_byte_register(client, 0, PMBUS_STATUS_INPUT)) 499d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_STATUS_INPUT; 509d2ecfb7SGuenter Roeck if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_12) && 519d2ecfb7SGuenter Roeck pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_1)) { 529d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_FAN12; 539d2ecfb7SGuenter Roeck if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_12)) 549d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_STATUS_FAN12; 559d2ecfb7SGuenter Roeck } 569d2ecfb7SGuenter Roeck if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_34) && 579d2ecfb7SGuenter Roeck pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_3)) { 589d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_FAN34; 599d2ecfb7SGuenter Roeck if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_34)) 609d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_STATUS_FAN34; 619d2ecfb7SGuenter Roeck } 629d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_1)) 639d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_TEMP; 649d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_2)) 659d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_TEMP2; 669d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_3)) 679d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_TEMP3; 689d2ecfb7SGuenter Roeck if (info->func[0] & (PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 699d2ecfb7SGuenter Roeck | PMBUS_HAVE_TEMP3) 709d2ecfb7SGuenter Roeck && pmbus_check_byte_register(client, 0, 719d2ecfb7SGuenter Roeck PMBUS_STATUS_TEMPERATURE)) 729d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_STATUS_TEMP; 739d2ecfb7SGuenter Roeck 749d2ecfb7SGuenter Roeck /* Sensors detected on all pages */ 759d2ecfb7SGuenter Roeck for (page = 0; page < info->pages; page++) { 769d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, page, PMBUS_READ_VOUT)) { 779d2ecfb7SGuenter Roeck info->func[page] |= PMBUS_HAVE_VOUT; 789d2ecfb7SGuenter Roeck if (pmbus_check_byte_register(client, page, 799d2ecfb7SGuenter Roeck PMBUS_STATUS_VOUT)) 809d2ecfb7SGuenter Roeck info->func[page] |= PMBUS_HAVE_STATUS_VOUT; 819d2ecfb7SGuenter Roeck } 829d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, page, PMBUS_READ_IOUT)) { 839d2ecfb7SGuenter Roeck info->func[page] |= PMBUS_HAVE_IOUT; 849d2ecfb7SGuenter Roeck if (pmbus_check_byte_register(client, 0, 859d2ecfb7SGuenter Roeck PMBUS_STATUS_IOUT)) 869d2ecfb7SGuenter Roeck info->func[page] |= PMBUS_HAVE_STATUS_IOUT; 879d2ecfb7SGuenter Roeck } 889d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, page, PMBUS_READ_POUT)) 899d2ecfb7SGuenter Roeck info->func[page] |= PMBUS_HAVE_POUT; 909d2ecfb7SGuenter Roeck } 919d2ecfb7SGuenter Roeck } 929d2ecfb7SGuenter Roeck 939d2ecfb7SGuenter Roeck /* 949d2ecfb7SGuenter Roeck * Identify chip parameters. 959d2ecfb7SGuenter Roeck */ 969d2ecfb7SGuenter Roeck static int pmbus_identify(struct i2c_client *client, 979d2ecfb7SGuenter Roeck struct pmbus_driver_info *info) 989d2ecfb7SGuenter Roeck { 99*1061d851SGuenter Roeck int ret = 0; 100*1061d851SGuenter Roeck 1019d2ecfb7SGuenter Roeck if (!info->pages) { 1029d2ecfb7SGuenter Roeck /* 1039d2ecfb7SGuenter Roeck * Check if the PAGE command is supported. If it is, 1049d2ecfb7SGuenter Roeck * keep setting the page number until it fails or until the 1059d2ecfb7SGuenter Roeck * maximum number of pages has been reached. Assume that 1069d2ecfb7SGuenter Roeck * this is the number of pages supported by the chip. 1079d2ecfb7SGuenter Roeck */ 1089d2ecfb7SGuenter Roeck if (pmbus_check_byte_register(client, 0, PMBUS_PAGE)) { 1099d2ecfb7SGuenter Roeck int page; 1109d2ecfb7SGuenter Roeck 1119d2ecfb7SGuenter Roeck for (page = 1; page < PMBUS_PAGES; page++) { 1129d2ecfb7SGuenter Roeck if (pmbus_set_page(client, page) < 0) 1139d2ecfb7SGuenter Roeck break; 1149d2ecfb7SGuenter Roeck } 1159d2ecfb7SGuenter Roeck pmbus_set_page(client, 0); 1169d2ecfb7SGuenter Roeck info->pages = page; 1179d2ecfb7SGuenter Roeck } else { 1189d2ecfb7SGuenter Roeck info->pages = 1; 1199d2ecfb7SGuenter Roeck } 1209d2ecfb7SGuenter Roeck } 1219d2ecfb7SGuenter Roeck 122*1061d851SGuenter Roeck if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE)) { 123*1061d851SGuenter Roeck int vout_mode; 124*1061d851SGuenter Roeck 125*1061d851SGuenter Roeck vout_mode = pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE); 126*1061d851SGuenter Roeck if (vout_mode >= 0 && vout_mode != 0xff) { 127*1061d851SGuenter Roeck switch (vout_mode >> 5) { 128*1061d851SGuenter Roeck case 0: 129*1061d851SGuenter Roeck break; 130*1061d851SGuenter Roeck case 1: 131*1061d851SGuenter Roeck info->format[PSC_VOLTAGE_OUT] = vid; 132*1061d851SGuenter Roeck break; 133*1061d851SGuenter Roeck case 2: 134*1061d851SGuenter Roeck info->format[PSC_VOLTAGE_OUT] = direct; 135*1061d851SGuenter Roeck break; 136*1061d851SGuenter Roeck default: 137*1061d851SGuenter Roeck ret = -ENODEV; 138*1061d851SGuenter Roeck goto abort; 139*1061d851SGuenter Roeck } 140*1061d851SGuenter Roeck } 141*1061d851SGuenter Roeck } 142*1061d851SGuenter Roeck 1439d2ecfb7SGuenter Roeck /* 1449d2ecfb7SGuenter Roeck * We should check if the COEFFICIENTS register is supported. 1459d2ecfb7SGuenter Roeck * If it is, and the chip is configured for direct mode, we can read 1469d2ecfb7SGuenter Roeck * the coefficients from the chip, one set per group of sensor 1479d2ecfb7SGuenter Roeck * registers. 1489d2ecfb7SGuenter Roeck * 1499d2ecfb7SGuenter Roeck * To do this, we will need access to a chip which actually supports the 1509d2ecfb7SGuenter Roeck * COEFFICIENTS command, since the command is too complex to implement 151*1061d851SGuenter Roeck * without testing it. Until then, abort if a chip configured for direct 152*1061d851SGuenter Roeck * mode was detected. 1539d2ecfb7SGuenter Roeck */ 154*1061d851SGuenter Roeck if (info->format[PSC_VOLTAGE_OUT] == direct) { 155*1061d851SGuenter Roeck ret = -ENODEV; 156*1061d851SGuenter Roeck goto abort; 157*1061d851SGuenter Roeck } 1589d2ecfb7SGuenter Roeck 1599d2ecfb7SGuenter Roeck /* Try to find sensor groups */ 1609d2ecfb7SGuenter Roeck pmbus_find_sensor_groups(client, info); 161*1061d851SGuenter Roeck abort: 162*1061d851SGuenter Roeck return ret; 1639d2ecfb7SGuenter Roeck } 1649d2ecfb7SGuenter Roeck 1659d2ecfb7SGuenter Roeck static int pmbus_probe(struct i2c_client *client, 1669d2ecfb7SGuenter Roeck const struct i2c_device_id *id) 1679d2ecfb7SGuenter Roeck { 1689d2ecfb7SGuenter Roeck struct pmbus_driver_info *info; 1699d2ecfb7SGuenter Roeck int ret; 1709d2ecfb7SGuenter Roeck 1719d2ecfb7SGuenter Roeck info = kzalloc(sizeof(struct pmbus_driver_info), GFP_KERNEL); 1729d2ecfb7SGuenter Roeck if (!info) 1739d2ecfb7SGuenter Roeck return -ENOMEM; 1749d2ecfb7SGuenter Roeck 1759d2ecfb7SGuenter Roeck info->pages = id->driver_data; 1769d2ecfb7SGuenter Roeck info->identify = pmbus_identify; 1779d2ecfb7SGuenter Roeck 1789d2ecfb7SGuenter Roeck ret = pmbus_do_probe(client, id, info); 1799d2ecfb7SGuenter Roeck if (ret < 0) 1809d2ecfb7SGuenter Roeck goto out; 1819d2ecfb7SGuenter Roeck return 0; 1829d2ecfb7SGuenter Roeck 1839d2ecfb7SGuenter Roeck out: 1849d2ecfb7SGuenter Roeck kfree(info); 1859d2ecfb7SGuenter Roeck return ret; 1869d2ecfb7SGuenter Roeck } 1879d2ecfb7SGuenter Roeck 1889d2ecfb7SGuenter Roeck static int pmbus_remove(struct i2c_client *client) 1899d2ecfb7SGuenter Roeck { 1909d2ecfb7SGuenter Roeck int ret; 1919d2ecfb7SGuenter Roeck const struct pmbus_driver_info *info; 1929d2ecfb7SGuenter Roeck 1939d2ecfb7SGuenter Roeck info = pmbus_get_driver_info(client); 1949d2ecfb7SGuenter Roeck ret = pmbus_do_remove(client); 1959d2ecfb7SGuenter Roeck kfree(info); 1969d2ecfb7SGuenter Roeck return ret; 1979d2ecfb7SGuenter Roeck } 1989d2ecfb7SGuenter Roeck 1999d2ecfb7SGuenter Roeck /* 2009d2ecfb7SGuenter Roeck * Use driver_data to set the number of pages supported by the chip. 2019d2ecfb7SGuenter Roeck */ 2029d2ecfb7SGuenter Roeck static const struct i2c_device_id pmbus_id[] = { 2039d2ecfb7SGuenter Roeck {"bmr450", 1}, 2049d2ecfb7SGuenter Roeck {"bmr451", 1}, 2059d2ecfb7SGuenter Roeck {"bmr453", 1}, 2069d2ecfb7SGuenter Roeck {"bmr454", 1}, 2079d2ecfb7SGuenter Roeck {"ltc2978", 8}, 2089d2ecfb7SGuenter Roeck {"pmbus", 0}, 2099d2ecfb7SGuenter Roeck {} 2109d2ecfb7SGuenter Roeck }; 2119d2ecfb7SGuenter Roeck 2129d2ecfb7SGuenter Roeck MODULE_DEVICE_TABLE(i2c, pmbus_id); 2139d2ecfb7SGuenter Roeck 2149d2ecfb7SGuenter Roeck /* This is the driver that will be inserted */ 2159d2ecfb7SGuenter Roeck static struct i2c_driver pmbus_driver = { 2169d2ecfb7SGuenter Roeck .driver = { 2179d2ecfb7SGuenter Roeck .name = "pmbus", 2189d2ecfb7SGuenter Roeck }, 2199d2ecfb7SGuenter Roeck .probe = pmbus_probe, 2209d2ecfb7SGuenter Roeck .remove = pmbus_remove, 2219d2ecfb7SGuenter Roeck .id_table = pmbus_id, 2229d2ecfb7SGuenter Roeck }; 2239d2ecfb7SGuenter Roeck 2249d2ecfb7SGuenter Roeck static int __init pmbus_init(void) 2259d2ecfb7SGuenter Roeck { 2269d2ecfb7SGuenter Roeck return i2c_add_driver(&pmbus_driver); 2279d2ecfb7SGuenter Roeck } 2289d2ecfb7SGuenter Roeck 2299d2ecfb7SGuenter Roeck static void __exit pmbus_exit(void) 2309d2ecfb7SGuenter Roeck { 2319d2ecfb7SGuenter Roeck i2c_del_driver(&pmbus_driver); 2329d2ecfb7SGuenter Roeck } 2339d2ecfb7SGuenter Roeck 2349d2ecfb7SGuenter Roeck MODULE_AUTHOR("Guenter Roeck"); 2359d2ecfb7SGuenter Roeck MODULE_DESCRIPTION("Generic PMBus driver"); 2369d2ecfb7SGuenter Roeck MODULE_LICENSE("GPL"); 2379d2ecfb7SGuenter Roeck module_init(pmbus_init); 2389d2ecfb7SGuenter Roeck module_exit(pmbus_exit); 239