Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Hardware monitoring driver for Vicor PLI1209BC Digital Supervisor
0004  *
0005  * Copyright (c) 2022 9elements GmbH
0006  */
0007 
0008 #include <linux/i2c.h>
0009 #include <linux/module.h>
0010 #include <linux/pmbus.h>
0011 #include <linux/regulator/driver.h>
0012 #include "pmbus.h"
0013 
0014 /*
0015  * The capability command is only supported at page 0. Probing the device while
0016  * the page register is set to 1 will falsely enable PEC support. Disable
0017  * capability probing accordingly, since the PLI1209BC does not have any
0018  * additional capabilities.
0019  */
0020 static struct pmbus_platform_data pli1209bc_plat_data = {
0021     .flags = PMBUS_NO_CAPABILITY,
0022 };
0023 
0024 static int pli1209bc_read_word_data(struct i2c_client *client, int page,
0025                     int phase, int reg)
0026 {
0027     int data;
0028 
0029     switch (reg) {
0030     /* PMBUS_READ_POUT uses a direct format with R=0 */
0031     case PMBUS_READ_POUT:
0032         data = pmbus_read_word_data(client, page, phase, reg);
0033         if (data < 0)
0034             return data;
0035         data = sign_extend32(data, 15) * 10;
0036         return clamp_val(data, -32768, 32767) & 0xffff;
0037     /*
0038      * PMBUS_READ_VOUT and PMBUS_READ_TEMPERATURE_1 return invalid data
0039      * when the BCM is turned off. Since it is not possible to return
0040      * ENODATA error, return zero instead.
0041      */
0042     case PMBUS_READ_VOUT:
0043     case PMBUS_READ_TEMPERATURE_1:
0044         data = pmbus_read_word_data(client, page, phase,
0045                         PMBUS_STATUS_WORD);
0046         if (data < 0)
0047             return data;
0048         if (data & PB_STATUS_POWER_GOOD_N)
0049             return 0;
0050         return pmbus_read_word_data(client, page, phase, reg);
0051     default:
0052         return -ENODATA;
0053     }
0054 }
0055 
0056 #if IS_ENABLED(CONFIG_SENSORS_PLI1209BC_REGULATOR)
0057 static const struct regulator_desc pli1209bc_reg_desc = {
0058     .name = "vout2",
0059     .id = 1,
0060     .of_match = of_match_ptr("vout2"),
0061     .regulators_node = of_match_ptr("regulators"),
0062     .ops = &pmbus_regulator_ops,
0063     .type = REGULATOR_VOLTAGE,
0064     .owner = THIS_MODULE,
0065 };
0066 #endif
0067 
0068 static struct pmbus_driver_info pli1209bc_info = {
0069     .pages = 2,
0070     .format[PSC_VOLTAGE_IN] = direct,
0071     .format[PSC_VOLTAGE_OUT] = direct,
0072     .format[PSC_CURRENT_IN] = direct,
0073     .format[PSC_CURRENT_OUT] = direct,
0074     .format[PSC_POWER] = direct,
0075     .format[PSC_TEMPERATURE] = direct,
0076     .m[PSC_VOLTAGE_IN] = 1,
0077     .b[PSC_VOLTAGE_IN] = 0,
0078     .R[PSC_VOLTAGE_IN] = 1,
0079     .m[PSC_VOLTAGE_OUT] = 1,
0080     .b[PSC_VOLTAGE_OUT] = 0,
0081     .R[PSC_VOLTAGE_OUT] = 1,
0082     .m[PSC_CURRENT_IN] = 1,
0083     .b[PSC_CURRENT_IN] = 0,
0084     .R[PSC_CURRENT_IN] = 3,
0085     .m[PSC_CURRENT_OUT] = 1,
0086     .b[PSC_CURRENT_OUT] = 0,
0087     .R[PSC_CURRENT_OUT] = 2,
0088     .m[PSC_POWER] = 1,
0089     .b[PSC_POWER] = 0,
0090     .R[PSC_POWER] = 1,
0091     .m[PSC_TEMPERATURE] = 1,
0092     .b[PSC_TEMPERATURE] = 0,
0093     .R[PSC_TEMPERATURE] = 0,
0094     /*
0095      * Page 0 sums up all attributes except voltage readings.
0096      * The pli1209 digital supervisor only contains a single BCM, making
0097      * page 0 redundant.
0098      */
0099     .func[1] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT
0100         | PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT
0101         | PMBUS_HAVE_PIN | PMBUS_HAVE_POUT
0102         | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP
0103         | PMBUS_HAVE_STATUS_IOUT | PMBUS_HAVE_STATUS_INPUT,
0104     .read_word_data = pli1209bc_read_word_data,
0105 #if IS_ENABLED(CONFIG_SENSORS_PLI1209BC_REGULATOR)
0106     .num_regulators = 1,
0107     .reg_desc = &pli1209bc_reg_desc,
0108 #endif
0109 };
0110 
0111 static int pli1209bc_probe(struct i2c_client *client)
0112 {
0113     client->dev.platform_data = &pli1209bc_plat_data;
0114     return pmbus_do_probe(client, &pli1209bc_info);
0115 }
0116 
0117 static const struct i2c_device_id pli1209bc_id[] = {
0118     {"pli1209bc", 0},
0119     {}
0120 };
0121 
0122 MODULE_DEVICE_TABLE(i2c, pli1209bc_id);
0123 
0124 #ifdef CONFIG_OF
0125 static const struct of_device_id pli1209bc_of_match[] = {
0126     { .compatible = "vicor,pli1209bc" },
0127     { },
0128 };
0129 MODULE_DEVICE_TABLE(of, pli1209bc_of_match);
0130 #endif
0131 
0132 static struct i2c_driver pli1209bc_driver = {
0133     .driver = {
0134            .name = "pli1209bc",
0135            .of_match_table = of_match_ptr(pli1209bc_of_match),
0136            },
0137     .probe_new = pli1209bc_probe,
0138     .id_table = pli1209bc_id,
0139 };
0140 
0141 module_i2c_driver(pli1209bc_driver);
0142 
0143 MODULE_AUTHOR("Marcello Sylvester Bauer <sylv@sylv.io>");
0144 MODULE_DESCRIPTION("PMBus driver for Vicor PLI1209BC");
0145 MODULE_LICENSE("GPL");
0146 MODULE_IMPORT_NS(PMBUS);