174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 29d2ecfb7SGuenter Roeck /* 39d2ecfb7SGuenter Roeck * Hardware monitoring driver for PMBus devices 49d2ecfb7SGuenter Roeck * 59d2ecfb7SGuenter Roeck * Copyright (c) 2010, 2011 Ericsson AB. 69d2ecfb7SGuenter Roeck */ 79d2ecfb7SGuenter Roeck 89d2ecfb7SGuenter Roeck #include <linux/kernel.h> 99d2ecfb7SGuenter Roeck #include <linux/module.h> 109d2ecfb7SGuenter Roeck #include <linux/init.h> 119d2ecfb7SGuenter Roeck #include <linux/err.h> 129d2ecfb7SGuenter Roeck #include <linux/slab.h> 139d2ecfb7SGuenter Roeck #include <linux/mutex.h> 149d2ecfb7SGuenter Roeck #include <linux/i2c.h> 154ba1bb12SWolfram Sang #include <linux/pmbus.h> 169d2ecfb7SGuenter Roeck #include "pmbus.h" 179d2ecfb7SGuenter Roeck 186f4a46f0SXiaoting Liu struct pmbus_device_info { 196f4a46f0SXiaoting Liu int pages; 206f4a46f0SXiaoting Liu u32 flags; 216f4a46f0SXiaoting Liu }; 226f4a46f0SXiaoting Liu 239d2ecfb7SGuenter Roeck /* 249d2ecfb7SGuenter Roeck * Find sensor groups and status registers on each page. 259d2ecfb7SGuenter Roeck */ 269d2ecfb7SGuenter Roeck static void pmbus_find_sensor_groups(struct i2c_client *client, 279d2ecfb7SGuenter Roeck struct pmbus_driver_info *info) 289d2ecfb7SGuenter Roeck { 299d2ecfb7SGuenter Roeck int page; 309d2ecfb7SGuenter Roeck 319d2ecfb7SGuenter Roeck /* Sensors detected on page 0 only */ 329d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, 0, PMBUS_READ_VIN)) 339d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_VIN; 349d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, 0, PMBUS_READ_VCAP)) 359d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_VCAP; 369d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, 0, PMBUS_READ_IIN)) 379d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_IIN; 389d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, 0, PMBUS_READ_PIN)) 399d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_PIN; 409d2ecfb7SGuenter Roeck if (info->func[0] 419d2ecfb7SGuenter Roeck && pmbus_check_byte_register(client, 0, PMBUS_STATUS_INPUT)) 429d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_STATUS_INPUT; 439d2ecfb7SGuenter Roeck if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_12) && 449d2ecfb7SGuenter Roeck pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_1)) { 459d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_FAN12; 469d2ecfb7SGuenter Roeck if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_12)) 479d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_STATUS_FAN12; 489d2ecfb7SGuenter Roeck } 499d2ecfb7SGuenter Roeck if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_34) && 509d2ecfb7SGuenter Roeck pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_3)) { 519d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_FAN34; 529d2ecfb7SGuenter Roeck if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_34)) 539d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_STATUS_FAN34; 549d2ecfb7SGuenter Roeck } 559d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_1)) 569d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_TEMP; 579d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_2)) 589d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_TEMP2; 599d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_3)) 609d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_TEMP3; 619d2ecfb7SGuenter Roeck if (info->func[0] & (PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 629d2ecfb7SGuenter Roeck | PMBUS_HAVE_TEMP3) 639d2ecfb7SGuenter Roeck && pmbus_check_byte_register(client, 0, 649d2ecfb7SGuenter Roeck PMBUS_STATUS_TEMPERATURE)) 659d2ecfb7SGuenter Roeck info->func[0] |= PMBUS_HAVE_STATUS_TEMP; 669d2ecfb7SGuenter Roeck 679d2ecfb7SGuenter Roeck /* Sensors detected on all pages */ 689d2ecfb7SGuenter Roeck for (page = 0; page < info->pages; page++) { 699d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, page, PMBUS_READ_VOUT)) { 709d2ecfb7SGuenter Roeck info->func[page] |= PMBUS_HAVE_VOUT; 719d2ecfb7SGuenter Roeck if (pmbus_check_byte_register(client, page, 729d2ecfb7SGuenter Roeck PMBUS_STATUS_VOUT)) 739d2ecfb7SGuenter Roeck info->func[page] |= PMBUS_HAVE_STATUS_VOUT; 749d2ecfb7SGuenter Roeck } 759d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, page, PMBUS_READ_IOUT)) { 769d2ecfb7SGuenter Roeck info->func[page] |= PMBUS_HAVE_IOUT; 779d2ecfb7SGuenter Roeck if (pmbus_check_byte_register(client, 0, 789d2ecfb7SGuenter Roeck PMBUS_STATUS_IOUT)) 799d2ecfb7SGuenter Roeck info->func[page] |= PMBUS_HAVE_STATUS_IOUT; 809d2ecfb7SGuenter Roeck } 819d2ecfb7SGuenter Roeck if (pmbus_check_word_register(client, page, PMBUS_READ_POUT)) 829d2ecfb7SGuenter Roeck info->func[page] |= PMBUS_HAVE_POUT; 839d2ecfb7SGuenter Roeck } 849d2ecfb7SGuenter Roeck } 859d2ecfb7SGuenter Roeck 869d2ecfb7SGuenter Roeck /* 879d2ecfb7SGuenter Roeck * Identify chip parameters. 889d2ecfb7SGuenter Roeck */ 899d2ecfb7SGuenter Roeck static int pmbus_identify(struct i2c_client *client, 909d2ecfb7SGuenter Roeck struct pmbus_driver_info *info) 919d2ecfb7SGuenter Roeck { 921061d851SGuenter Roeck int ret = 0; 931061d851SGuenter Roeck 949d2ecfb7SGuenter Roeck if (!info->pages) { 959d2ecfb7SGuenter Roeck /* 969d2ecfb7SGuenter Roeck * Check if the PAGE command is supported. If it is, 979d2ecfb7SGuenter Roeck * keep setting the page number until it fails or until the 989d2ecfb7SGuenter Roeck * maximum number of pages has been reached. Assume that 999d2ecfb7SGuenter Roeck * this is the number of pages supported by the chip. 1009d2ecfb7SGuenter Roeck */ 1019d2ecfb7SGuenter Roeck if (pmbus_check_byte_register(client, 0, PMBUS_PAGE)) { 1029d2ecfb7SGuenter Roeck int page; 1039d2ecfb7SGuenter Roeck 1049d2ecfb7SGuenter Roeck for (page = 1; page < PMBUS_PAGES; page++) { 105*43f33b6eSGuenter Roeck if (pmbus_set_page(client, page, 0xff) < 0) 1069d2ecfb7SGuenter Roeck break; 1079d2ecfb7SGuenter Roeck } 108*43f33b6eSGuenter Roeck pmbus_set_page(client, 0, 0xff); 1099d2ecfb7SGuenter Roeck info->pages = page; 1109d2ecfb7SGuenter Roeck } else { 1119d2ecfb7SGuenter Roeck info->pages = 1; 1129d2ecfb7SGuenter Roeck } 113e7c6a556SDmitry Bazhenov 114e7c6a556SDmitry Bazhenov pmbus_clear_faults(client); 1159d2ecfb7SGuenter Roeck } 1169d2ecfb7SGuenter Roeck 1171061d851SGuenter Roeck if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE)) { 118b9fa0a3aSVadim Pasternak int vout_mode, i; 1191061d851SGuenter Roeck 1201061d851SGuenter Roeck vout_mode = pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE); 1211061d851SGuenter Roeck if (vout_mode >= 0 && vout_mode != 0xff) { 1221061d851SGuenter Roeck switch (vout_mode >> 5) { 1231061d851SGuenter Roeck case 0: 1241061d851SGuenter Roeck break; 1251061d851SGuenter Roeck case 1: 1261061d851SGuenter Roeck info->format[PSC_VOLTAGE_OUT] = vid; 127b9fa0a3aSVadim Pasternak for (i = 0; i < info->pages; i++) 128b9fa0a3aSVadim Pasternak info->vrm_version[i] = vr11; 1291061d851SGuenter Roeck break; 1301061d851SGuenter Roeck case 2: 1311061d851SGuenter Roeck info->format[PSC_VOLTAGE_OUT] = direct; 1321061d851SGuenter Roeck break; 1331061d851SGuenter Roeck default: 1341061d851SGuenter Roeck ret = -ENODEV; 1351061d851SGuenter Roeck goto abort; 1361061d851SGuenter Roeck } 1371061d851SGuenter Roeck } 1381061d851SGuenter Roeck } 1391061d851SGuenter Roeck 1409d2ecfb7SGuenter Roeck /* 1419d2ecfb7SGuenter Roeck * We should check if the COEFFICIENTS register is supported. 1429d2ecfb7SGuenter Roeck * If it is, and the chip is configured for direct mode, we can read 1439d2ecfb7SGuenter Roeck * the coefficients from the chip, one set per group of sensor 1449d2ecfb7SGuenter Roeck * registers. 1459d2ecfb7SGuenter Roeck * 1469d2ecfb7SGuenter Roeck * To do this, we will need access to a chip which actually supports the 1479d2ecfb7SGuenter Roeck * COEFFICIENTS command, since the command is too complex to implement 1481061d851SGuenter Roeck * without testing it. Until then, abort if a chip configured for direct 1491061d851SGuenter Roeck * mode was detected. 1509d2ecfb7SGuenter Roeck */ 1511061d851SGuenter Roeck if (info->format[PSC_VOLTAGE_OUT] == direct) { 1521061d851SGuenter Roeck ret = -ENODEV; 1531061d851SGuenter Roeck goto abort; 1541061d851SGuenter Roeck } 1559d2ecfb7SGuenter Roeck 1569d2ecfb7SGuenter Roeck /* Try to find sensor groups */ 1579d2ecfb7SGuenter Roeck pmbus_find_sensor_groups(client, info); 1581061d851SGuenter Roeck abort: 1591061d851SGuenter Roeck return ret; 1609d2ecfb7SGuenter Roeck } 1619d2ecfb7SGuenter Roeck 1629d2ecfb7SGuenter Roeck static int pmbus_probe(struct i2c_client *client, 1639d2ecfb7SGuenter Roeck const struct i2c_device_id *id) 1649d2ecfb7SGuenter Roeck { 1659d2ecfb7SGuenter Roeck struct pmbus_driver_info *info; 166cc00decfSVadim Pasternak struct pmbus_platform_data *pdata = NULL; 167cc00decfSVadim Pasternak struct device *dev = &client->dev; 1686f4a46f0SXiaoting Liu struct pmbus_device_info *device_info; 1699d2ecfb7SGuenter Roeck 170cc00decfSVadim Pasternak info = devm_kzalloc(dev, sizeof(struct pmbus_driver_info), GFP_KERNEL); 1719d2ecfb7SGuenter Roeck if (!info) 1729d2ecfb7SGuenter Roeck return -ENOMEM; 1739d2ecfb7SGuenter Roeck 1746f4a46f0SXiaoting Liu device_info = (struct pmbus_device_info *)id->driver_data; 1756f4a46f0SXiaoting Liu if (device_info->flags & PMBUS_SKIP_STATUS_CHECK) { 176cc00decfSVadim Pasternak pdata = devm_kzalloc(dev, sizeof(struct pmbus_platform_data), 177cc00decfSVadim Pasternak GFP_KERNEL); 178cc00decfSVadim Pasternak if (!pdata) 179cc00decfSVadim Pasternak return -ENOMEM; 180cc00decfSVadim Pasternak 181cc00decfSVadim Pasternak pdata->flags = PMBUS_SKIP_STATUS_CHECK; 182cc00decfSVadim Pasternak } 183cc00decfSVadim Pasternak 1846f4a46f0SXiaoting Liu info->pages = device_info->pages; 1859d2ecfb7SGuenter Roeck info->identify = pmbus_identify; 186cc00decfSVadim Pasternak dev->platform_data = pdata; 1879d2ecfb7SGuenter Roeck 1888b313ca7SGuenter Roeck return pmbus_do_probe(client, id, info); 1899d2ecfb7SGuenter Roeck } 1909d2ecfb7SGuenter Roeck 1916f4a46f0SXiaoting Liu static const struct pmbus_device_info pmbus_info_one = { 1926f4a46f0SXiaoting Liu .pages = 1, 1936f4a46f0SXiaoting Liu .flags = 0 1946f4a46f0SXiaoting Liu }; 1956f4a46f0SXiaoting Liu static const struct pmbus_device_info pmbus_info_zero = { 1966f4a46f0SXiaoting Liu .pages = 0, 1976f4a46f0SXiaoting Liu .flags = 0 1986f4a46f0SXiaoting Liu }; 1996f4a46f0SXiaoting Liu static const struct pmbus_device_info pmbus_info_one_skip = { 2006f4a46f0SXiaoting Liu .pages = 1, 2016f4a46f0SXiaoting Liu .flags = PMBUS_SKIP_STATUS_CHECK 2026f4a46f0SXiaoting Liu }; 2036f4a46f0SXiaoting Liu 2049d2ecfb7SGuenter Roeck /* 2059d2ecfb7SGuenter Roeck * Use driver_data to set the number of pages supported by the chip. 2069d2ecfb7SGuenter Roeck */ 2079d2ecfb7SGuenter Roeck static const struct i2c_device_id pmbus_id[] = { 2086f4a46f0SXiaoting Liu {"adp4000", (kernel_ulong_t)&pmbus_info_one}, 2096f4a46f0SXiaoting Liu {"bmr453", (kernel_ulong_t)&pmbus_info_one}, 2106f4a46f0SXiaoting Liu {"bmr454", (kernel_ulong_t)&pmbus_info_one}, 2116f4a46f0SXiaoting Liu {"dps460", (kernel_ulong_t)&pmbus_info_one_skip}, 212705f2c81SXiaoting Liu {"dps650ab", (kernel_ulong_t)&pmbus_info_one_skip}, 2136f4a46f0SXiaoting Liu {"dps800", (kernel_ulong_t)&pmbus_info_one_skip}, 2141a1ea120SGuenter Roeck {"max20796", (kernel_ulong_t)&pmbus_info_one}, 2156f4a46f0SXiaoting Liu {"mdt040", (kernel_ulong_t)&pmbus_info_one}, 2166f4a46f0SXiaoting Liu {"ncp4200", (kernel_ulong_t)&pmbus_info_one}, 2176f4a46f0SXiaoting Liu {"ncp4208", (kernel_ulong_t)&pmbus_info_one}, 2186f4a46f0SXiaoting Liu {"pdt003", (kernel_ulong_t)&pmbus_info_one}, 2196f4a46f0SXiaoting Liu {"pdt006", (kernel_ulong_t)&pmbus_info_one}, 2206f4a46f0SXiaoting Liu {"pdt012", (kernel_ulong_t)&pmbus_info_one}, 2216f4a46f0SXiaoting Liu {"pmbus", (kernel_ulong_t)&pmbus_info_zero}, 2226f4a46f0SXiaoting Liu {"sgd009", (kernel_ulong_t)&pmbus_info_one_skip}, 2236f4a46f0SXiaoting Liu {"tps40400", (kernel_ulong_t)&pmbus_info_one}, 2246f4a46f0SXiaoting Liu {"tps544b20", (kernel_ulong_t)&pmbus_info_one}, 2256f4a46f0SXiaoting Liu {"tps544b25", (kernel_ulong_t)&pmbus_info_one}, 2266f4a46f0SXiaoting Liu {"tps544c20", (kernel_ulong_t)&pmbus_info_one}, 2276f4a46f0SXiaoting Liu {"tps544c25", (kernel_ulong_t)&pmbus_info_one}, 2286f4a46f0SXiaoting Liu {"udt020", (kernel_ulong_t)&pmbus_info_one}, 2299d2ecfb7SGuenter Roeck {} 2309d2ecfb7SGuenter Roeck }; 2319d2ecfb7SGuenter Roeck 2329d2ecfb7SGuenter Roeck MODULE_DEVICE_TABLE(i2c, pmbus_id); 2339d2ecfb7SGuenter Roeck 2349d2ecfb7SGuenter Roeck /* This is the driver that will be inserted */ 2359d2ecfb7SGuenter Roeck static struct i2c_driver pmbus_driver = { 2369d2ecfb7SGuenter Roeck .driver = { 2379d2ecfb7SGuenter Roeck .name = "pmbus", 2389d2ecfb7SGuenter Roeck }, 2399d2ecfb7SGuenter Roeck .probe = pmbus_probe, 240dd285ad7SGuenter Roeck .remove = pmbus_do_remove, 2419d2ecfb7SGuenter Roeck .id_table = pmbus_id, 2429d2ecfb7SGuenter Roeck }; 2439d2ecfb7SGuenter Roeck 244f0967eeaSAxel Lin module_i2c_driver(pmbus_driver); 2459d2ecfb7SGuenter Roeck 2469d2ecfb7SGuenter Roeck MODULE_AUTHOR("Guenter Roeck"); 2479d2ecfb7SGuenter Roeck MODULE_DESCRIPTION("Generic PMBus driver"); 2489d2ecfb7SGuenter Roeck MODULE_LICENSE("GPL"); 249