2019-05-20 10:19:02 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2011-01-27 07:09:02 +03:00
|
|
|
/*
|
|
|
|
* Hardware monitoring driver for PMBus devices
|
|
|
|
*
|
|
|
|
* Copyright (c) 2010, 2011 Ericsson AB.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/mutex.h>
|
|
|
|
#include <linux/i2c.h>
|
2017-05-21 23:34:43 +03:00
|
|
|
#include <linux/pmbus.h>
|
2011-01-27 07:09:02 +03:00
|
|
|
#include "pmbus.h"
|
|
|
|
|
2019-01-10 05:18:20 +03:00
|
|
|
struct pmbus_device_info {
|
|
|
|
int pages;
|
|
|
|
u32 flags;
|
|
|
|
};
|
|
|
|
|
2020-08-09 00:00:04 +03:00
|
|
|
static const struct i2c_device_id pmbus_id[];
|
|
|
|
|
2011-01-27 07:09:02 +03:00
|
|
|
/*
|
|
|
|
* Find sensor groups and status registers on each page.
|
|
|
|
*/
|
|
|
|
static void pmbus_find_sensor_groups(struct i2c_client *client,
|
|
|
|
struct pmbus_driver_info *info)
|
|
|
|
{
|
|
|
|
int page;
|
|
|
|
|
|
|
|
/* Sensors detected on page 0 only */
|
|
|
|
if (pmbus_check_word_register(client, 0, PMBUS_READ_VIN))
|
|
|
|
info->func[0] |= PMBUS_HAVE_VIN;
|
|
|
|
if (pmbus_check_word_register(client, 0, PMBUS_READ_VCAP))
|
|
|
|
info->func[0] |= PMBUS_HAVE_VCAP;
|
|
|
|
if (pmbus_check_word_register(client, 0, PMBUS_READ_IIN))
|
|
|
|
info->func[0] |= PMBUS_HAVE_IIN;
|
|
|
|
if (pmbus_check_word_register(client, 0, PMBUS_READ_PIN))
|
|
|
|
info->func[0] |= PMBUS_HAVE_PIN;
|
|
|
|
if (info->func[0]
|
|
|
|
&& pmbus_check_byte_register(client, 0, PMBUS_STATUS_INPUT))
|
|
|
|
info->func[0] |= PMBUS_HAVE_STATUS_INPUT;
|
2011-06-30 17:54:05 +04:00
|
|
|
if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_12) &&
|
|
|
|
pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_1)) {
|
2011-01-27 07:09:02 +03:00
|
|
|
info->func[0] |= PMBUS_HAVE_FAN12;
|
|
|
|
if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_12))
|
|
|
|
info->func[0] |= PMBUS_HAVE_STATUS_FAN12;
|
|
|
|
}
|
2011-06-30 17:54:05 +04:00
|
|
|
if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_34) &&
|
|
|
|
pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_3)) {
|
2011-01-27 07:09:02 +03:00
|
|
|
info->func[0] |= PMBUS_HAVE_FAN34;
|
|
|
|
if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_34))
|
|
|
|
info->func[0] |= PMBUS_HAVE_STATUS_FAN34;
|
|
|
|
}
|
2011-07-04 00:08:03 +04:00
|
|
|
if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_1))
|
2011-01-27 07:09:02 +03:00
|
|
|
info->func[0] |= PMBUS_HAVE_TEMP;
|
2011-06-30 17:57:41 +04:00
|
|
|
if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_2))
|
|
|
|
info->func[0] |= PMBUS_HAVE_TEMP2;
|
|
|
|
if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_3))
|
|
|
|
info->func[0] |= PMBUS_HAVE_TEMP3;
|
2011-07-04 00:08:03 +04:00
|
|
|
if (info->func[0] & (PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2
|
|
|
|
| PMBUS_HAVE_TEMP3)
|
|
|
|
&& pmbus_check_byte_register(client, 0,
|
|
|
|
PMBUS_STATUS_TEMPERATURE))
|
|
|
|
info->func[0] |= PMBUS_HAVE_STATUS_TEMP;
|
2011-01-27 07:09:02 +03:00
|
|
|
|
|
|
|
/* Sensors detected on all pages */
|
|
|
|
for (page = 0; page < info->pages; page++) {
|
|
|
|
if (pmbus_check_word_register(client, page, PMBUS_READ_VOUT)) {
|
|
|
|
info->func[page] |= PMBUS_HAVE_VOUT;
|
|
|
|
if (pmbus_check_byte_register(client, page,
|
|
|
|
PMBUS_STATUS_VOUT))
|
|
|
|
info->func[page] |= PMBUS_HAVE_STATUS_VOUT;
|
|
|
|
}
|
|
|
|
if (pmbus_check_word_register(client, page, PMBUS_READ_IOUT)) {
|
|
|
|
info->func[page] |= PMBUS_HAVE_IOUT;
|
|
|
|
if (pmbus_check_byte_register(client, 0,
|
|
|
|
PMBUS_STATUS_IOUT))
|
|
|
|
info->func[page] |= PMBUS_HAVE_STATUS_IOUT;
|
|
|
|
}
|
|
|
|
if (pmbus_check_word_register(client, page, PMBUS_READ_POUT))
|
|
|
|
info->func[page] |= PMBUS_HAVE_POUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Identify chip parameters.
|
|
|
|
*/
|
|
|
|
static int pmbus_identify(struct i2c_client *client,
|
|
|
|
struct pmbus_driver_info *info)
|
|
|
|
{
|
2011-06-25 22:21:49 +04:00
|
|
|
int ret = 0;
|
|
|
|
|
2011-01-27 07:09:02 +03:00
|
|
|
if (!info->pages) {
|
|
|
|
/*
|
|
|
|
* Check if the PAGE command is supported. If it is,
|
|
|
|
* keep setting the page number until it fails or until the
|
|
|
|
* maximum number of pages has been reached. Assume that
|
|
|
|
* this is the number of pages supported by the chip.
|
|
|
|
*/
|
|
|
|
if (pmbus_check_byte_register(client, 0, PMBUS_PAGE)) {
|
|
|
|
int page;
|
|
|
|
|
|
|
|
for (page = 1; page < PMBUS_PAGES; page++) {
|
2020-01-14 20:49:27 +03:00
|
|
|
if (pmbus_set_page(client, page, 0xff) < 0)
|
2011-01-27 07:09:02 +03:00
|
|
|
break;
|
|
|
|
}
|
2020-01-14 20:49:27 +03:00
|
|
|
pmbus_set_page(client, 0, 0xff);
|
2011-01-27 07:09:02 +03:00
|
|
|
info->pages = page;
|
|
|
|
} else {
|
|
|
|
info->pages = 1;
|
|
|
|
}
|
2018-10-15 12:21:22 +03:00
|
|
|
|
|
|
|
pmbus_clear_faults(client);
|
2011-01-27 07:09:02 +03:00
|
|
|
}
|
|
|
|
|
2011-06-25 22:21:49 +04:00
|
|
|
if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE)) {
|
2020-01-13 18:08:36 +03:00
|
|
|
int vout_mode, i;
|
2011-06-25 22:21:49 +04:00
|
|
|
|
|
|
|
vout_mode = pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE);
|
|
|
|
if (vout_mode >= 0 && vout_mode != 0xff) {
|
|
|
|
switch (vout_mode >> 5) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
info->format[PSC_VOLTAGE_OUT] = vid;
|
2020-01-13 18:08:36 +03:00
|
|
|
for (i = 0; i < info->pages; i++)
|
|
|
|
info->vrm_version[i] = vr11;
|
2011-06-25 22:21:49 +04:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
info->format[PSC_VOLTAGE_OUT] = direct;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto abort;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-27 07:09:02 +03:00
|
|
|
/*
|
|
|
|
* We should check if the COEFFICIENTS register is supported.
|
|
|
|
* If it is, and the chip is configured for direct mode, we can read
|
|
|
|
* the coefficients from the chip, one set per group of sensor
|
|
|
|
* registers.
|
|
|
|
*
|
|
|
|
* To do this, we will need access to a chip which actually supports the
|
|
|
|
* COEFFICIENTS command, since the command is too complex to implement
|
2011-06-25 22:21:49 +04:00
|
|
|
* without testing it. Until then, abort if a chip configured for direct
|
|
|
|
* mode was detected.
|
2011-01-27 07:09:02 +03:00
|
|
|
*/
|
2011-06-25 22:21:49 +04:00
|
|
|
if (info->format[PSC_VOLTAGE_OUT] == direct) {
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto abort;
|
|
|
|
}
|
2011-01-27 07:09:02 +03:00
|
|
|
|
|
|
|
/* Try to find sensor groups */
|
|
|
|
pmbus_find_sensor_groups(client, info);
|
2011-06-25 22:21:49 +04:00
|
|
|
abort:
|
|
|
|
return ret;
|
2011-01-27 07:09:02 +03:00
|
|
|
}
|
|
|
|
|
2020-08-09 00:00:04 +03:00
|
|
|
static int pmbus_probe(struct i2c_client *client)
|
2011-01-27 07:09:02 +03:00
|
|
|
{
|
|
|
|
struct pmbus_driver_info *info;
|
2016-07-25 13:55:53 +03:00
|
|
|
struct pmbus_platform_data *pdata = NULL;
|
|
|
|
struct device *dev = &client->dev;
|
2019-01-10 05:18:20 +03:00
|
|
|
struct pmbus_device_info *device_info;
|
2011-01-27 07:09:02 +03:00
|
|
|
|
2016-07-25 13:55:53 +03:00
|
|
|
info = devm_kzalloc(dev, sizeof(struct pmbus_driver_info), GFP_KERNEL);
|
2011-01-27 07:09:02 +03:00
|
|
|
if (!info)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2020-08-09 00:00:04 +03:00
|
|
|
device_info = (struct pmbus_device_info *)i2c_match_id(pmbus_id, client)->driver_data;
|
2021-05-07 22:40:23 +03:00
|
|
|
if (device_info->flags) {
|
2016-07-25 13:55:53 +03:00
|
|
|
pdata = devm_kzalloc(dev, sizeof(struct pmbus_platform_data),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!pdata)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2021-05-07 22:40:23 +03:00
|
|
|
pdata->flags = device_info->flags;
|
2016-07-25 13:55:53 +03:00
|
|
|
}
|
|
|
|
|
2019-01-10 05:18:20 +03:00
|
|
|
info->pages = device_info->pages;
|
2011-01-27 07:09:02 +03:00
|
|
|
info->identify = pmbus_identify;
|
2016-07-25 13:55:53 +03:00
|
|
|
dev->platform_data = pdata;
|
2011-01-27 07:09:02 +03:00
|
|
|
|
2020-08-09 00:00:04 +03:00
|
|
|
return pmbus_do_probe(client, info);
|
2011-01-27 07:09:02 +03:00
|
|
|
}
|
|
|
|
|
2019-01-10 05:18:20 +03:00
|
|
|
static const struct pmbus_device_info pmbus_info_one = {
|
|
|
|
.pages = 1,
|
|
|
|
.flags = 0
|
|
|
|
};
|
2021-05-07 22:40:23 +03:00
|
|
|
|
2019-01-10 05:18:20 +03:00
|
|
|
static const struct pmbus_device_info pmbus_info_zero = {
|
|
|
|
.pages = 0,
|
|
|
|
.flags = 0
|
|
|
|
};
|
2021-05-07 22:40:23 +03:00
|
|
|
|
2019-01-10 05:18:20 +03:00
|
|
|
static const struct pmbus_device_info pmbus_info_one_skip = {
|
|
|
|
.pages = 1,
|
|
|
|
.flags = PMBUS_SKIP_STATUS_CHECK
|
|
|
|
};
|
|
|
|
|
2021-05-07 22:40:23 +03:00
|
|
|
static const struct pmbus_device_info pmbus_info_one_status = {
|
|
|
|
.pages = 1,
|
|
|
|
.flags = PMBUS_READ_STATUS_AFTER_FAILED_CHECK
|
|
|
|
};
|
|
|
|
|
2011-01-27 07:09:02 +03:00
|
|
|
/*
|
|
|
|
* Use driver_data to set the number of pages supported by the chip.
|
|
|
|
*/
|
|
|
|
static const struct i2c_device_id pmbus_id[] = {
|
2019-01-10 05:18:20 +03:00
|
|
|
{"adp4000", (kernel_ulong_t)&pmbus_info_one},
|
2021-05-07 22:40:23 +03:00
|
|
|
{"bmr310", (kernel_ulong_t)&pmbus_info_one_status},
|
2019-01-10 05:18:20 +03:00
|
|
|
{"bmr453", (kernel_ulong_t)&pmbus_info_one},
|
|
|
|
{"bmr454", (kernel_ulong_t)&pmbus_info_one},
|
2021-05-07 22:40:23 +03:00
|
|
|
{"bmr456", (kernel_ulong_t)&pmbus_info_one},
|
|
|
|
{"bmr457", (kernel_ulong_t)&pmbus_info_one},
|
|
|
|
{"bmr458", (kernel_ulong_t)&pmbus_info_one_status},
|
|
|
|
{"bmr480", (kernel_ulong_t)&pmbus_info_one_status},
|
|
|
|
{"bmr490", (kernel_ulong_t)&pmbus_info_one_status},
|
|
|
|
{"bmr491", (kernel_ulong_t)&pmbus_info_one_status},
|
|
|
|
{"bmr492", (kernel_ulong_t)&pmbus_info_one},
|
2019-01-10 05:18:20 +03:00
|
|
|
{"dps460", (kernel_ulong_t)&pmbus_info_one_skip},
|
2019-01-10 05:31:44 +03:00
|
|
|
{"dps650ab", (kernel_ulong_t)&pmbus_info_one_skip},
|
2019-01-10 05:18:20 +03:00
|
|
|
{"dps800", (kernel_ulong_t)&pmbus_info_one_skip},
|
2019-12-14 00:36:36 +03:00
|
|
|
{"max20796", (kernel_ulong_t)&pmbus_info_one},
|
2019-01-10 05:18:20 +03:00
|
|
|
{"mdt040", (kernel_ulong_t)&pmbus_info_one},
|
|
|
|
{"ncp4200", (kernel_ulong_t)&pmbus_info_one},
|
|
|
|
{"ncp4208", (kernel_ulong_t)&pmbus_info_one},
|
|
|
|
{"pdt003", (kernel_ulong_t)&pmbus_info_one},
|
|
|
|
{"pdt006", (kernel_ulong_t)&pmbus_info_one},
|
|
|
|
{"pdt012", (kernel_ulong_t)&pmbus_info_one},
|
|
|
|
{"pmbus", (kernel_ulong_t)&pmbus_info_zero},
|
|
|
|
{"sgd009", (kernel_ulong_t)&pmbus_info_one_skip},
|
|
|
|
{"tps40400", (kernel_ulong_t)&pmbus_info_one},
|
|
|
|
{"tps544b20", (kernel_ulong_t)&pmbus_info_one},
|
|
|
|
{"tps544b25", (kernel_ulong_t)&pmbus_info_one},
|
|
|
|
{"tps544c20", (kernel_ulong_t)&pmbus_info_one},
|
|
|
|
{"tps544c25", (kernel_ulong_t)&pmbus_info_one},
|
|
|
|
{"udt020", (kernel_ulong_t)&pmbus_info_one},
|
2011-01-27 07:09:02 +03:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(i2c, pmbus_id);
|
|
|
|
|
|
|
|
/* This is the driver that will be inserted */
|
|
|
|
static struct i2c_driver pmbus_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "pmbus",
|
|
|
|
},
|
2020-08-09 00:00:04 +03:00
|
|
|
.probe_new = pmbus_probe,
|
2011-01-27 07:09:02 +03:00
|
|
|
.id_table = pmbus_id,
|
|
|
|
};
|
|
|
|
|
2012-01-20 11:38:18 +04:00
|
|
|
module_i2c_driver(pmbus_driver);
|
2011-01-27 07:09:02 +03:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Guenter Roeck");
|
|
|
|
MODULE_DESCRIPTION("Generic PMBus driver");
|
|
|
|
MODULE_LICENSE("GPL");
|
2021-04-20 09:07:07 +03:00
|
|
|
MODULE_IMPORT_NS(PMBUS);
|