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> 284ba1bb12SWolfram Sang #include <linux/pmbus.h> 299d2ecfb7SGuenter Roeck #include "pmbus.h" 309d2ecfb7SGuenter Roeck 31*6f4a46f0SXiaoting Liu struct pmbus_device_info { 32*6f4a46f0SXiaoting Liu int pages; 33*6f4a46f0SXiaoting Liu u32 flags; 34*6f4a46f0SXiaoting Liu }; 35*6f4a46f0SXiaoting Liu 369d2ecfb7SGuenter Roeck /* 379d2ecfb7SGuenter Roeck * Find sensor groups and status registers on each page. 389d2ecfb7SGuenter Roeck */ 399d2ecfb7SGuenter Roeck static void pmbus_find_sensor_groups(struct i2c_client *client, 409d2ecfb7SGuenter Roeck struct pmbus_driver_info *info) 419d2ecfb7SGuenter Roeck { 429d2ecfb7SGuenter Roeck int page; 439d2ecfb7SGuenter Roeck 449d2ecfb7SGuenter Roeck /* Sensors detected on page 0 only */ 459d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, 0, PMBUS_READ_VIN)) 469d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_VIN; 479d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, 0, PMBUS_READ_VCAP)) 489d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_VCAP; 499d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, 0, PMBUS_READ_IIN)) 509d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_IIN; 519d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, 0, PMBUS_READ_PIN)) 529d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_PIN; 539d2ecfb7SGuenter Roeck if (info->func[0] 549d2ecfb7SGuenter Roeck && pmbus_check_byte_register(client, 0, PMBUS_STATUS_INPUT)) 559d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_STATUS_INPUT; 569d2ecfb7SGuenter Roeck if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_12) && 579d2ecfb7SGuenter Roeck pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_1)) { 589d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_FAN12; 599d2ecfb7SGuenter Roeck if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_12)) 609d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_STATUS_FAN12; 619d2ecfb7SGuenter Roeck } 629d2ecfb7SGuenter Roeck if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_34) && 639d2ecfb7SGuenter Roeck pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_3)) { 649d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_FAN34; 659d2ecfb7SGuenter Roeck if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_34)) 669d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_STATUS_FAN34; 679d2ecfb7SGuenter Roeck } 689d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_1)) 699d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_TEMP; 709d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_2)) 719d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_TEMP2; 729d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_3)) 739d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_TEMP3; 749d2ecfb7SGuenter Roeck if (info->func[0] & (PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 759d2ecfb7SGuenter Roeck | PMBUS_HAVE_TEMP3) 769d2ecfb7SGuenter Roeck && pmbus_check_byte_register(client, 0, 779d2ecfb7SGuenter Roeck PMBUS_STATUS_TEMPERATURE)) 789d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_STATUS_TEMP; 799d2ecfb7SGuenter Roeck 809d2ecfb7SGuenter Roeck /* Sensors detected on all pages */ 819d2ecfb7SGuenter Roeck for (page = 0; page < info->pages; page++) { 829d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, page, PMBUS_READ_VOUT)) { 839d2ecfb7SGuenter Roeck info->func[page] |= PMBUS_HAVE_VOUT; 849d2ecfb7SGuenter Roeck if (pmbus_check_byte_register(client, page, 859d2ecfb7SGuenter Roeck PMBUS_STATUS_VOUT)) 869d2ecfb7SGuenter Roeck info->func[page] |= PMBUS_HAVE_STATUS_VOUT; 879d2ecfb7SGuenter Roeck } 889d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, page, PMBUS_READ_IOUT)) { 899d2ecfb7SGuenter Roeck info->func[page] |= PMBUS_HAVE_IOUT; 909d2ecfb7SGuenter Roeck if (pmbus_check_byte_register(client, 0, 919d2ecfb7SGuenter Roeck PMBUS_STATUS_IOUT)) 929d2ecfb7SGuenter Roeck info->func[page] |= PMBUS_HAVE_STATUS_IOUT; 939d2ecfb7SGuenter Roeck } 949d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, page, PMBUS_READ_POUT)) 959d2ecfb7SGuenter Roeck info->func[page] |= PMBUS_HAVE_POUT; 969d2ecfb7SGuenter Roeck } 979d2ecfb7SGuenter Roeck } 989d2ecfb7SGuenter Roeck 999d2ecfb7SGuenter Roeck /* 1009d2ecfb7SGuenter Roeck * Identify chip parameters. 1019d2ecfb7SGuenter Roeck */ 1029d2ecfb7SGuenter Roeck static int pmbus_identify(struct i2c_client *client, 1039d2ecfb7SGuenter Roeck struct pmbus_driver_info *info) 1049d2ecfb7SGuenter Roeck { 1051061d851SGuenter Roeck int ret = 0; 1061061d851SGuenter Roeck 1079d2ecfb7SGuenter Roeck if (!info->pages) { 1089d2ecfb7SGuenter Roeck /* 1099d2ecfb7SGuenter Roeck * Check if the PAGE command is supported. If it is, 1109d2ecfb7SGuenter Roeck * keep setting the page number until it fails or until the 1119d2ecfb7SGuenter Roeck * maximum number of pages has been reached. Assume that 1129d2ecfb7SGuenter Roeck * this is the number of pages supported by the chip. 1139d2ecfb7SGuenter Roeck */ 1149d2ecfb7SGuenter Roeck if (pmbus_check_byte_register(client, 0, PMBUS_PAGE)) { 1159d2ecfb7SGuenter Roeck int page; 1169d2ecfb7SGuenter Roeck 1179d2ecfb7SGuenter Roeck for (page = 1; page < PMBUS_PAGES; page++) { 1189d2ecfb7SGuenter Roeck if (pmbus_set_page(client, page) < 0) 1199d2ecfb7SGuenter Roeck break; 1209d2ecfb7SGuenter Roeck } 1219d2ecfb7SGuenter Roeck pmbus_set_page(client, 0); 1229d2ecfb7SGuenter Roeck info->pages = page; 1239d2ecfb7SGuenter Roeck } else { 1249d2ecfb7SGuenter Roeck info->pages = 1; 1259d2ecfb7SGuenter Roeck } 126e7c6a556SDmitry Bazhenov 127e7c6a556SDmitry Bazhenov pmbus_clear_faults(client); 1289d2ecfb7SGuenter Roeck } 1299d2ecfb7SGuenter Roeck 1301061d851SGuenter Roeck if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE)) { 1311061d851SGuenter Roeck int vout_mode; 1321061d851SGuenter Roeck 1331061d851SGuenter Roeck vout_mode = pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE); 1341061d851SGuenter Roeck if (vout_mode >= 0 && vout_mode != 0xff) { 1351061d851SGuenter Roeck switch (vout_mode >> 5) { 1361061d851SGuenter Roeck case 0: 1371061d851SGuenter Roeck break; 1381061d851SGuenter Roeck case 1: 1391061d851SGuenter Roeck info->format[PSC_VOLTAGE_OUT] = vid; 140068c2270SGuenter Roeck info->vrm_version = vr11; 1411061d851SGuenter Roeck break; 1421061d851SGuenter Roeck case 2: 1431061d851SGuenter Roeck info->format[PSC_VOLTAGE_OUT] = direct; 1441061d851SGuenter Roeck break; 1451061d851SGuenter Roeck default: 1461061d851SGuenter Roeck ret = -ENODEV; 1471061d851SGuenter Roeck goto abort; 1481061d851SGuenter Roeck } 1491061d851SGuenter Roeck } 1501061d851SGuenter Roeck } 1511061d851SGuenter Roeck 1529d2ecfb7SGuenter Roeck /* 1539d2ecfb7SGuenter Roeck * We should check if the COEFFICIENTS register is supported. 1549d2ecfb7SGuenter Roeck * If it is, and the chip is configured for direct mode, we can read 1559d2ecfb7SGuenter Roeck * the coefficients from the chip, one set per group of sensor 1569d2ecfb7SGuenter Roeck * registers. 1579d2ecfb7SGuenter Roeck * 1589d2ecfb7SGuenter Roeck * To do this, we will need access to a chip which actually supports the 1599d2ecfb7SGuenter Roeck * COEFFICIENTS command, since the command is too complex to implement 1601061d851SGuenter Roeck * without testing it. Until then, abort if a chip configured for direct 1611061d851SGuenter Roeck * mode was detected. 1629d2ecfb7SGuenter Roeck */ 1631061d851SGuenter Roeck if (info->format[PSC_VOLTAGE_OUT] == direct) { 1641061d851SGuenter Roeck ret = -ENODEV; 1651061d851SGuenter Roeck goto abort; 1661061d851SGuenter Roeck } 1679d2ecfb7SGuenter Roeck 1689d2ecfb7SGuenter Roeck /* Try to find sensor groups */ 1699d2ecfb7SGuenter Roeck pmbus_find_sensor_groups(client, info); 1701061d851SGuenter Roeck abort: 1711061d851SGuenter Roeck return ret; 1729d2ecfb7SGuenter Roeck } 1739d2ecfb7SGuenter Roeck 1749d2ecfb7SGuenter Roeck static int pmbus_probe(struct i2c_client *client, 1759d2ecfb7SGuenter Roeck const struct i2c_device_id *id) 1769d2ecfb7SGuenter Roeck { 1779d2ecfb7SGuenter Roeck struct pmbus_driver_info *info; 178cc00decfSVadim Pasternak struct pmbus_platform_data *pdata = NULL; 179cc00decfSVadim Pasternak struct device *dev = &client->dev; 180*6f4a46f0SXiaoting Liu struct pmbus_device_info *device_info; 1819d2ecfb7SGuenter Roeck 182cc00decfSVadim Pasternak info = devm_kzalloc(dev, sizeof(struct pmbus_driver_info), GFP_KERNEL); 1839d2ecfb7SGuenter Roeck if (!info) 1849d2ecfb7SGuenter Roeck return -ENOMEM; 1859d2ecfb7SGuenter Roeck 186*6f4a46f0SXiaoting Liu device_info = (struct pmbus_device_info *)id->driver_data; 187*6f4a46f0SXiaoting Liu if (device_info->flags & PMBUS_SKIP_STATUS_CHECK) { 188cc00decfSVadim Pasternak pdata = devm_kzalloc(dev, sizeof(struct pmbus_platform_data), 189cc00decfSVadim Pasternak GFP_KERNEL); 190cc00decfSVadim Pasternak if (!pdata) 191cc00decfSVadim Pasternak return -ENOMEM; 192cc00decfSVadim Pasternak 193cc00decfSVadim Pasternak pdata->flags = PMBUS_SKIP_STATUS_CHECK; 194cc00decfSVadim Pasternak } 195cc00decfSVadim Pasternak 196*6f4a46f0SXiaoting Liu info->pages = device_info->pages; 1979d2ecfb7SGuenter Roeck info->identify = pmbus_identify; 198cc00decfSVadim Pasternak dev->platform_data = pdata; 1999d2ecfb7SGuenter Roeck 2008b313ca7SGuenter Roeck return pmbus_do_probe(client, id, info); 2019d2ecfb7SGuenter Roeck } 2029d2ecfb7SGuenter Roeck 203*6f4a46f0SXiaoting Liu static const struct pmbus_device_info pmbus_info_one = { 204*6f4a46f0SXiaoting Liu .pages = 1, 205*6f4a46f0SXiaoting Liu .flags = 0 206*6f4a46f0SXiaoting Liu }; 207*6f4a46f0SXiaoting Liu static const struct pmbus_device_info pmbus_info_zero = { 208*6f4a46f0SXiaoting Liu .pages = 0, 209*6f4a46f0SXiaoting Liu .flags = 0 210*6f4a46f0SXiaoting Liu }; 211*6f4a46f0SXiaoting Liu static const struct pmbus_device_info pmbus_info_one_skip = { 212*6f4a46f0SXiaoting Liu .pages = 1, 213*6f4a46f0SXiaoting Liu .flags = PMBUS_SKIP_STATUS_CHECK 214*6f4a46f0SXiaoting Liu }; 215*6f4a46f0SXiaoting Liu 2169d2ecfb7SGuenter Roeck /* 2179d2ecfb7SGuenter Roeck * Use driver_data to set the number of pages supported by the chip. 2189d2ecfb7SGuenter Roeck */ 2199d2ecfb7SGuenter Roeck static const struct i2c_device_id pmbus_id[] = { 220*6f4a46f0SXiaoting Liu {"adp4000", (kernel_ulong_t)&pmbus_info_one}, 221*6f4a46f0SXiaoting Liu {"bmr453", (kernel_ulong_t)&pmbus_info_one}, 222*6f4a46f0SXiaoting Liu {"bmr454", (kernel_ulong_t)&pmbus_info_one}, 223*6f4a46f0SXiaoting Liu {"dps460", (kernel_ulong_t)&pmbus_info_one_skip}, 224*6f4a46f0SXiaoting Liu {"dps800", (kernel_ulong_t)&pmbus_info_one_skip}, 225*6f4a46f0SXiaoting Liu {"mdt040", (kernel_ulong_t)&pmbus_info_one}, 226*6f4a46f0SXiaoting Liu {"ncp4200", (kernel_ulong_t)&pmbus_info_one}, 227*6f4a46f0SXiaoting Liu {"ncp4208", (kernel_ulong_t)&pmbus_info_one}, 228*6f4a46f0SXiaoting Liu {"pdt003", (kernel_ulong_t)&pmbus_info_one}, 229*6f4a46f0SXiaoting Liu {"pdt006", (kernel_ulong_t)&pmbus_info_one}, 230*6f4a46f0SXiaoting Liu {"pdt012", (kernel_ulong_t)&pmbus_info_one}, 231*6f4a46f0SXiaoting Liu {"pmbus", (kernel_ulong_t)&pmbus_info_zero}, 232*6f4a46f0SXiaoting Liu {"sgd009", (kernel_ulong_t)&pmbus_info_one_skip}, 233*6f4a46f0SXiaoting Liu {"tps40400", (kernel_ulong_t)&pmbus_info_one}, 234*6f4a46f0SXiaoting Liu {"tps544b20", (kernel_ulong_t)&pmbus_info_one}, 235*6f4a46f0SXiaoting Liu {"tps544b25", (kernel_ulong_t)&pmbus_info_one}, 236*6f4a46f0SXiaoting Liu {"tps544c20", (kernel_ulong_t)&pmbus_info_one}, 237*6f4a46f0SXiaoting Liu {"tps544c25", (kernel_ulong_t)&pmbus_info_one}, 238*6f4a46f0SXiaoting Liu {"udt020", (kernel_ulong_t)&pmbus_info_one}, 2399d2ecfb7SGuenter Roeck {} 2409d2ecfb7SGuenter Roeck }; 2419d2ecfb7SGuenter Roeck 2429d2ecfb7SGuenter Roeck MODULE_DEVICE_TABLE(i2c, pmbus_id); 2439d2ecfb7SGuenter Roeck 2449d2ecfb7SGuenter Roeck /* This is the driver that will be inserted */ 2459d2ecfb7SGuenter Roeck static struct i2c_driver pmbus_driver = { 2469d2ecfb7SGuenter Roeck .driver = { 2479d2ecfb7SGuenter Roeck .name = "pmbus", 2489d2ecfb7SGuenter Roeck }, 2499d2ecfb7SGuenter Roeck .probe = pmbus_probe, 250dd285ad7SGuenter Roeck .remove = pmbus_do_remove, 2519d2ecfb7SGuenter Roeck .id_table = pmbus_id, 2529d2ecfb7SGuenter Roeck }; 2539d2ecfb7SGuenter Roeck 254f0967eeaSAxel Lin module_i2c_driver(pmbus_driver); 2559d2ecfb7SGuenter Roeck 2569d2ecfb7SGuenter Roeck MODULE_AUTHOR("Guenter Roeck"); 2579d2ecfb7SGuenter Roeck MODULE_DESCRIPTION("Generic PMBus driver"); 2589d2ecfb7SGuenter Roeck MODULE_LICENSE("GPL"); 259