Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Driver for Delta DPS920AB PSU
0004  *
0005  * Copyright (C) 2021 Delta Networks, Inc.
0006  * Copyright (C) 2021 Sartura Ltd.
0007  */
0008 
0009 #include <linux/debugfs.h>
0010 #include <linux/i2c.h>
0011 #include <linux/module.h>
0012 #include <linux/of_device.h>
0013 #include "pmbus.h"
0014 
0015 struct dps920ab_data {
0016     char *mfr_model;
0017     char *mfr_id;
0018 };
0019 
0020 static int dps920ab_read_word_data(struct i2c_client *client, int page, int phase, int reg)
0021 {
0022     /*
0023      * This masks commands which are not supported.
0024      * PSU advertises that all features are supported,
0025      * in reality that unfortunately is not true.
0026      * So enable only those that the datasheet confirms.
0027      */
0028     switch (reg) {
0029     case PMBUS_FAN_COMMAND_1:
0030     case PMBUS_IOUT_OC_WARN_LIMIT:
0031     case PMBUS_STATUS_WORD:
0032     case PMBUS_READ_VIN:
0033     case PMBUS_READ_IIN:
0034     case PMBUS_READ_VOUT:
0035     case PMBUS_READ_IOUT:
0036     case PMBUS_READ_TEMPERATURE_1:
0037     case PMBUS_READ_TEMPERATURE_2:
0038     case PMBUS_READ_TEMPERATURE_3:
0039     case PMBUS_READ_FAN_SPEED_1:
0040     case PMBUS_READ_POUT:
0041     case PMBUS_READ_PIN:
0042     case PMBUS_MFR_VOUT_MIN:
0043     case PMBUS_MFR_VOUT_MAX:
0044     case PMBUS_MFR_IOUT_MAX:
0045     case PMBUS_MFR_POUT_MAX:
0046         return pmbus_read_word_data(client, page, phase, reg);
0047     default:
0048         return -ENXIO;
0049     }
0050 }
0051 
0052 static int dps920ab_write_word_data(struct i2c_client *client, int page, int reg,
0053                     u16 word)
0054 {
0055     /*
0056      * This masks commands which are not supported.
0057      * PSU only has one R/W register and that is
0058      * for the fan.
0059      */
0060     switch (reg) {
0061     case PMBUS_FAN_COMMAND_1:
0062         return pmbus_write_word_data(client, page, reg, word);
0063     default:
0064         return -EACCES;
0065     }
0066 }
0067 
0068 static struct pmbus_driver_info dps920ab_info = {
0069     .pages = 1,
0070 
0071     .format[PSC_VOLTAGE_IN] = linear,
0072     .format[PSC_VOLTAGE_OUT] = linear,
0073     .format[PSC_CURRENT_IN] = linear,
0074     .format[PSC_CURRENT_OUT] = linear,
0075     .format[PSC_POWER] = linear,
0076     .format[PSC_FAN] = linear,
0077     .format[PSC_TEMPERATURE] = linear,
0078 
0079     .func[0] =
0080         PMBUS_HAVE_VIN | PMBUS_HAVE_IIN | PMBUS_HAVE_PIN |
0081         PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT | PMBUS_HAVE_POUT |
0082         PMBUS_HAVE_TEMP  | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_TEMP3 |
0083         PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12 |
0084         PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT |
0085         PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_STATUS_TEMP,
0086     .read_word_data = dps920ab_read_word_data,
0087     .write_word_data = dps920ab_write_word_data,
0088 };
0089 
0090 static int dps920ab_mfr_id_show(struct seq_file *s, void *data)
0091 {
0092     struct dps920ab_data *priv = s->private;
0093 
0094     seq_printf(s, "%s\n", priv->mfr_id);
0095 
0096     return 0;
0097 }
0098 
0099 DEFINE_SHOW_ATTRIBUTE(dps920ab_mfr_id);
0100 
0101 static int dps920ab_mfr_model_show(struct seq_file *s, void *data)
0102 {
0103     struct dps920ab_data *priv = s->private;
0104 
0105     seq_printf(s, "%s\n", priv->mfr_model);
0106 
0107     return 0;
0108 }
0109 
0110 DEFINE_SHOW_ATTRIBUTE(dps920ab_mfr_model);
0111 
0112 static void dps920ab_init_debugfs(struct dps920ab_data *data, struct i2c_client *client)
0113 {
0114     struct dentry *debugfs_dir;
0115     struct dentry *root;
0116 
0117     root = pmbus_get_debugfs_dir(client);
0118     if (!root)
0119         return;
0120 
0121     debugfs_dir = debugfs_create_dir(client->name, root);
0122 
0123     debugfs_create_file("mfr_id",
0124                 0400,
0125                 debugfs_dir,
0126                 data,
0127                 &dps920ab_mfr_id_fops);
0128 
0129     debugfs_create_file("mfr_model",
0130                 0400,
0131                 debugfs_dir,
0132                 data,
0133                 &dps920ab_mfr_model_fops);
0134 }
0135 
0136 static int dps920ab_probe(struct i2c_client *client)
0137 {
0138     u8 buf[I2C_SMBUS_BLOCK_MAX + 1];
0139     struct dps920ab_data *data;
0140     int ret;
0141 
0142     data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
0143     if (!data)
0144         return -ENOMEM;
0145 
0146     ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, buf);
0147     if (ret < 0) {
0148         dev_err(&client->dev, "Failed to read Manufacturer ID\n");
0149         return ret;
0150     }
0151 
0152     buf[ret] = '\0';
0153     if (ret != 5 || strncmp(buf, "DELTA", 5)) {
0154         buf[ret] = '\0';
0155         dev_err(&client->dev, "Unsupported Manufacturer ID '%s'\n", buf);
0156         return -ENODEV;
0157     }
0158     data->mfr_id = devm_kstrdup(&client->dev, buf, GFP_KERNEL);
0159     if (!data->mfr_id)
0160         return -ENOMEM;
0161 
0162     ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf);
0163     if (ret < 0) {
0164         dev_err(&client->dev, "Failed to read Manufacturer Model\n");
0165         return ret;
0166     }
0167 
0168     buf[ret] = '\0';
0169     if (ret != 11 || strncmp(buf, "DPS-920AB", 9)) {
0170         dev_err(&client->dev, "Unsupported Manufacturer Model '%s'\n", buf);
0171         return -ENODEV;
0172     }
0173     data->mfr_model = devm_kstrdup(&client->dev, buf, GFP_KERNEL);
0174     if (!data->mfr_model)
0175         return -ENOMEM;
0176 
0177     ret = pmbus_do_probe(client, &dps920ab_info);
0178     if (ret)
0179         return ret;
0180 
0181     dps920ab_init_debugfs(data, client);
0182 
0183     return 0;
0184 }
0185 
0186 static const struct of_device_id __maybe_unused dps920ab_of_match[] = {
0187     { .compatible = "delta,dps920ab", },
0188     {}
0189 };
0190 
0191 MODULE_DEVICE_TABLE(of, dps920ab_of_match);
0192 
0193 static struct i2c_driver dps920ab_driver = {
0194     .driver = {
0195            .name = "dps920ab",
0196            .of_match_table = of_match_ptr(dps920ab_of_match),
0197     },
0198     .probe_new = dps920ab_probe,
0199 };
0200 
0201 module_i2c_driver(dps920ab_driver);
0202 
0203 MODULE_AUTHOR("Robert Marko <robert.marko@sartura.hr>");
0204 MODULE_DESCRIPTION("PMBus driver for Delta DPS920AB PSU");
0205 MODULE_LICENSE("GPL");
0206 MODULE_IMPORT_NS(PMBUS);