Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Hardware monitoring driver for Analog Devices LT7182S
0004  *
0005  * Copyright (c) 2022 Guenter Roeck
0006  *
0007  */
0008 
0009 #include <linux/bits.h>
0010 #include <linux/err.h>
0011 #include <linux/i2c.h>
0012 #include <linux/init.h>
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 #include <linux/of.h>
0016 #include "pmbus.h"
0017 
0018 #define LT7182S_NUM_PAGES   2
0019 
0020 #define MFR_READ_EXTVCC     0xcd
0021 #define MFR_READ_ITH        0xce
0022 #define MFR_CONFIG_ALL_LT7182S  0xd1
0023 #define MFR_IOUT_PEAK       0xd7
0024 #define MFR_ADC_CONTROL_LT7182S 0xd8
0025 
0026 #define MFR_DEBUG_TELEMETRY BIT(0)
0027 
0028 #define MFR_VOUT_PEAK       0xdd
0029 #define MFR_VIN_PEAK        0xde
0030 #define MFR_TEMPERATURE_1_PEAK  0xdf
0031 #define MFR_CLEAR_PEAKS     0xe3
0032 
0033 #define MFR_CONFIG_IEEE     BIT(8)
0034 
0035 static int lt7182s_read_word_data(struct i2c_client *client, int page, int phase, int reg)
0036 {
0037     int ret;
0038 
0039     switch (reg) {
0040     case PMBUS_VIRT_READ_VMON:
0041         if (page == 0 || page == 1)
0042             ret = pmbus_read_word_data(client, page, phase, MFR_READ_ITH);
0043         else
0044             ret = pmbus_read_word_data(client, 0, phase, MFR_READ_EXTVCC);
0045         break;
0046     case PMBUS_VIRT_READ_IOUT_MAX:
0047         ret = pmbus_read_word_data(client, page, phase, MFR_IOUT_PEAK);
0048         break;
0049     case PMBUS_VIRT_READ_VOUT_MAX:
0050         ret = pmbus_read_word_data(client, page, phase, MFR_VOUT_PEAK);
0051         break;
0052     case PMBUS_VIRT_READ_VIN_MAX:
0053         ret = pmbus_read_word_data(client, page, phase, MFR_VIN_PEAK);
0054         break;
0055     case PMBUS_VIRT_READ_TEMP_MAX:
0056         ret = pmbus_read_word_data(client, page, phase, MFR_TEMPERATURE_1_PEAK);
0057         break;
0058     case PMBUS_VIRT_RESET_VIN_HISTORY:
0059         ret = (page == 0) ? 0 : -ENODATA;
0060         break;
0061     default:
0062         ret = -ENODATA;
0063         break;
0064     }
0065     return ret;
0066 }
0067 
0068 static int lt7182s_write_word_data(struct i2c_client *client, int page, int reg, u16 word)
0069 {
0070     int ret;
0071 
0072     switch (reg) {
0073     case PMBUS_VIRT_RESET_VIN_HISTORY:
0074         ret = pmbus_write_byte(client, 0, MFR_CLEAR_PEAKS);
0075         break;
0076     default:
0077         ret = -ENODATA;
0078         break;
0079     }
0080     return ret;
0081 }
0082 
0083 static struct pmbus_driver_info lt7182s_info = {
0084     .pages = LT7182S_NUM_PAGES,
0085     .format[PSC_VOLTAGE_IN] = linear,
0086     .format[PSC_VOLTAGE_OUT] = linear,
0087     .format[PSC_CURRENT_IN] = linear,
0088     .format[PSC_CURRENT_OUT] = linear,
0089     .format[PSC_TEMPERATURE] = linear,
0090     .format[PSC_POWER] = linear,
0091     .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT |
0092       PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_POUT |
0093       PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT |
0094       PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_STATUS_TEMP,
0095     .func[1] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT |
0096       PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_POUT |
0097       PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT |
0098       PMBUS_HAVE_STATUS_INPUT,
0099     .read_word_data = lt7182s_read_word_data,
0100     .write_word_data = lt7182s_write_word_data,
0101 };
0102 
0103 static int lt7182s_probe(struct i2c_client *client)
0104 {
0105     struct device *dev = &client->dev;
0106     struct pmbus_driver_info *info;
0107     u8 buf[I2C_SMBUS_BLOCK_MAX + 1];
0108     int ret;
0109 
0110     if (!i2c_check_functionality(client->adapter,
0111                      I2C_FUNC_SMBUS_READ_BYTE_DATA |
0112                      I2C_FUNC_SMBUS_READ_WORD_DATA |
0113                      I2C_FUNC_SMBUS_READ_BLOCK_DATA))
0114         return -ENODEV;
0115 
0116     ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, buf);
0117     if (ret < 0) {
0118         dev_err(dev, "Failed to read PMBUS_MFR_ID\n");
0119         return ret;
0120     }
0121     if (ret != 3 || strncmp(buf, "ADI", 3)) {
0122         buf[ret] = '\0';
0123         dev_err(dev, "Manufacturer '%s' not supported\n", buf);
0124         return -ENODEV;
0125     }
0126 
0127     ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf);
0128     if (ret < 0) {
0129         dev_err(dev, "Failed to read PMBUS_MFR_MODEL\n");
0130         return ret;
0131     }
0132     if (ret != 7 || strncmp(buf, "LT7182S", 7)) {
0133         buf[ret] = '\0';
0134         dev_err(dev, "Model '%s' not supported\n", buf);
0135         return -ENODEV;
0136     }
0137 
0138     info = devm_kmemdup(dev, &lt7182s_info,
0139                 sizeof(struct pmbus_driver_info), GFP_KERNEL);
0140     if (!info)
0141         return -ENOMEM;
0142 
0143     /* Set data format to IEEE754 if configured */
0144     ret = i2c_smbus_read_word_data(client, MFR_CONFIG_ALL_LT7182S);
0145     if (ret < 0)
0146         return ret;
0147     if (ret & MFR_CONFIG_IEEE) {
0148         info->format[PSC_VOLTAGE_IN] = ieee754;
0149         info->format[PSC_VOLTAGE_OUT] = ieee754;
0150         info->format[PSC_CURRENT_IN] = ieee754;
0151         info->format[PSC_CURRENT_OUT] = ieee754;
0152         info->format[PSC_TEMPERATURE] = ieee754;
0153         info->format[PSC_POWER] = ieee754;
0154     }
0155 
0156     /* Enable VMON output if configured */
0157     ret = i2c_smbus_read_byte_data(client, MFR_ADC_CONTROL_LT7182S);
0158     if (ret < 0)
0159         return ret;
0160     if (ret & MFR_DEBUG_TELEMETRY) {
0161         info->pages = 3;
0162         info->func[0] |= PMBUS_HAVE_VMON;
0163         info->func[1] |= PMBUS_HAVE_VMON;
0164         info->func[2] = PMBUS_HAVE_VMON;
0165     }
0166 
0167     return pmbus_do_probe(client, info);
0168 }
0169 
0170 static const struct i2c_device_id lt7182s_id[] = {
0171     { "lt7182s", 0 },
0172     {}
0173 };
0174 MODULE_DEVICE_TABLE(i2c, lt7182s_id);
0175 
0176 static const struct of_device_id __maybe_unused lt7182s_of_match[] = {
0177     { .compatible = "adi,lt7182s" },
0178     {}
0179 };
0180 
0181 static struct i2c_driver lt7182s_driver = {
0182     .driver = {
0183         .name = "lt7182s",
0184         .of_match_table = of_match_ptr(lt7182s_of_match),
0185     },
0186     .probe_new = lt7182s_probe,
0187     .id_table = lt7182s_id,
0188 };
0189 
0190 module_i2c_driver(lt7182s_driver);
0191 
0192 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
0193 MODULE_DESCRIPTION("PMBus driver for Analog Devices LT7182S");
0194 MODULE_LICENSE("GPL");
0195 MODULE_IMPORT_NS(PMBUS);