Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright 2019 Inspur Corp.
0004  */
0005 
0006 #include <linux/debugfs.h>
0007 #include <linux/device.h>
0008 #include <linux/fs.h>
0009 #include <linux/i2c.h>
0010 #include <linux/module.h>
0011 #include <linux/pmbus.h>
0012 #include <linux/hwmon-sysfs.h>
0013 
0014 #include "pmbus.h"
0015 
0016 #define IPSPS_REG_VENDOR_ID 0x99
0017 #define IPSPS_REG_MODEL     0x9A
0018 #define IPSPS_REG_FW_VERSION    0x9B
0019 #define IPSPS_REG_PN        0x9C
0020 #define IPSPS_REG_SN        0x9E
0021 #define IPSPS_REG_HW_VERSION    0xB0
0022 #define IPSPS_REG_MODE      0xFC
0023 
0024 #define MODE_ACTIVE     0x55
0025 #define MODE_STANDBY        0x0E
0026 #define MODE_REDUNDANCY     0x00
0027 
0028 #define MODE_ACTIVE_STRING      "active"
0029 #define MODE_STANDBY_STRING     "standby"
0030 #define MODE_REDUNDANCY_STRING      "redundancy"
0031 
0032 enum ipsps_index {
0033     vendor,
0034     model,
0035     fw_version,
0036     part_number,
0037     serial_number,
0038     hw_version,
0039     mode,
0040     num_regs,
0041 };
0042 
0043 static const u8 ipsps_regs[num_regs] = {
0044     [vendor] = IPSPS_REG_VENDOR_ID,
0045     [model] = IPSPS_REG_MODEL,
0046     [fw_version] = IPSPS_REG_FW_VERSION,
0047     [part_number] = IPSPS_REG_PN,
0048     [serial_number] = IPSPS_REG_SN,
0049     [hw_version] = IPSPS_REG_HW_VERSION,
0050     [mode] = IPSPS_REG_MODE,
0051 };
0052 
0053 static ssize_t ipsps_string_show(struct device *dev,
0054                  struct device_attribute *devattr,
0055                  char *buf)
0056 {
0057     u8 reg;
0058     int rc;
0059     char *p;
0060     char data[I2C_SMBUS_BLOCK_MAX + 1];
0061     struct i2c_client *client = to_i2c_client(dev->parent);
0062     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0063 
0064     reg = ipsps_regs[attr->index];
0065     rc = i2c_smbus_read_block_data(client, reg, data);
0066     if (rc < 0)
0067         return rc;
0068 
0069     /* filled with printable characters, ending with # */
0070     p = memscan(data, '#', rc);
0071     *p = '\0';
0072 
0073     return sysfs_emit(buf, "%s\n", data);
0074 }
0075 
0076 static ssize_t ipsps_fw_version_show(struct device *dev,
0077                      struct device_attribute *devattr,
0078                      char *buf)
0079 {
0080     u8 reg;
0081     int rc;
0082     u8 data[I2C_SMBUS_BLOCK_MAX] = { 0 };
0083     struct i2c_client *client = to_i2c_client(dev->parent);
0084     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0085 
0086     reg = ipsps_regs[attr->index];
0087     rc = i2c_smbus_read_block_data(client, reg, data);
0088     if (rc < 0)
0089         return rc;
0090 
0091     if (rc != 6)
0092         return -EPROTO;
0093 
0094     return sysfs_emit(buf, "%u.%02u%u-%u.%02u\n",
0095               data[1], data[2]/* < 100 */, data[3]/*< 10*/,
0096               data[4], data[5]/* < 100 */);
0097 }
0098 
0099 static ssize_t ipsps_mode_show(struct device *dev,
0100                    struct device_attribute *devattr, char *buf)
0101 {
0102     u8 reg;
0103     int rc;
0104     struct i2c_client *client = to_i2c_client(dev->parent);
0105     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0106 
0107     reg = ipsps_regs[attr->index];
0108     rc = i2c_smbus_read_byte_data(client, reg);
0109     if (rc < 0)
0110         return rc;
0111 
0112     switch (rc) {
0113     case MODE_ACTIVE:
0114         return sysfs_emit(buf, "[%s] %s %s\n",
0115                   MODE_ACTIVE_STRING,
0116                   MODE_STANDBY_STRING, MODE_REDUNDANCY_STRING);
0117     case MODE_STANDBY:
0118         return sysfs_emit(buf, "%s [%s] %s\n",
0119                   MODE_ACTIVE_STRING,
0120                   MODE_STANDBY_STRING, MODE_REDUNDANCY_STRING);
0121     case MODE_REDUNDANCY:
0122         return sysfs_emit(buf, "%s %s [%s]\n",
0123                   MODE_ACTIVE_STRING,
0124                   MODE_STANDBY_STRING, MODE_REDUNDANCY_STRING);
0125     default:
0126         return sysfs_emit(buf, "unspecified\n");
0127     }
0128 }
0129 
0130 static ssize_t ipsps_mode_store(struct device *dev,
0131                 struct device_attribute *devattr,
0132                 const char *buf, size_t count)
0133 {
0134     u8 reg;
0135     int rc;
0136     struct i2c_client *client = to_i2c_client(dev->parent);
0137     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0138 
0139     reg = ipsps_regs[attr->index];
0140     if (sysfs_streq(MODE_STANDBY_STRING, buf)) {
0141         rc = i2c_smbus_write_byte_data(client, reg,
0142                            MODE_STANDBY);
0143         if (rc < 0)
0144             return rc;
0145         return count;
0146     } else if (sysfs_streq(MODE_ACTIVE_STRING, buf)) {
0147         rc = i2c_smbus_write_byte_data(client, reg,
0148                            MODE_ACTIVE);
0149         if (rc < 0)
0150             return rc;
0151         return count;
0152     }
0153 
0154     return -EINVAL;
0155 }
0156 
0157 static SENSOR_DEVICE_ATTR_RO(vendor, ipsps_string, vendor);
0158 static SENSOR_DEVICE_ATTR_RO(model, ipsps_string, model);
0159 static SENSOR_DEVICE_ATTR_RO(part_number, ipsps_string, part_number);
0160 static SENSOR_DEVICE_ATTR_RO(serial_number, ipsps_string, serial_number);
0161 static SENSOR_DEVICE_ATTR_RO(hw_version, ipsps_string, hw_version);
0162 static SENSOR_DEVICE_ATTR_RO(fw_version, ipsps_fw_version, fw_version);
0163 static SENSOR_DEVICE_ATTR_RW(mode, ipsps_mode, mode);
0164 
0165 static struct attribute *ipsps_attrs[] = {
0166     &sensor_dev_attr_vendor.dev_attr.attr,
0167     &sensor_dev_attr_model.dev_attr.attr,
0168     &sensor_dev_attr_part_number.dev_attr.attr,
0169     &sensor_dev_attr_serial_number.dev_attr.attr,
0170     &sensor_dev_attr_hw_version.dev_attr.attr,
0171     &sensor_dev_attr_fw_version.dev_attr.attr,
0172     &sensor_dev_attr_mode.dev_attr.attr,
0173     NULL,
0174 };
0175 
0176 ATTRIBUTE_GROUPS(ipsps);
0177 
0178 static struct pmbus_driver_info ipsps_info = {
0179     .pages = 1,
0180     .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
0181         PMBUS_HAVE_IIN | PMBUS_HAVE_POUT | PMBUS_HAVE_PIN |
0182         PMBUS_HAVE_FAN12 | PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 |
0183         PMBUS_HAVE_TEMP3 | PMBUS_HAVE_STATUS_VOUT |
0184         PMBUS_HAVE_STATUS_IOUT | PMBUS_HAVE_STATUS_INPUT |
0185         PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_STATUS_FAN12,
0186     .groups = ipsps_groups,
0187 };
0188 
0189 static struct pmbus_platform_data ipsps_pdata = {
0190     .flags = PMBUS_SKIP_STATUS_CHECK,
0191 };
0192 
0193 static int ipsps_probe(struct i2c_client *client)
0194 {
0195     client->dev.platform_data = &ipsps_pdata;
0196     return pmbus_do_probe(client, &ipsps_info);
0197 }
0198 
0199 static const struct i2c_device_id ipsps_id[] = {
0200     { "ipsps1", 0 },
0201     {}
0202 };
0203 MODULE_DEVICE_TABLE(i2c, ipsps_id);
0204 
0205 #ifdef CONFIG_OF
0206 static const struct of_device_id ipsps_of_match[] = {
0207     { .compatible = "inspur,ipsps1" },
0208     {}
0209 };
0210 MODULE_DEVICE_TABLE(of, ipsps_of_match);
0211 #endif
0212 
0213 static struct i2c_driver ipsps_driver = {
0214     .driver = {
0215         .name = "inspur-ipsps",
0216         .of_match_table = of_match_ptr(ipsps_of_match),
0217     },
0218     .probe_new = ipsps_probe,
0219     .id_table = ipsps_id,
0220 };
0221 
0222 module_i2c_driver(ipsps_driver);
0223 
0224 MODULE_AUTHOR("John Wang");
0225 MODULE_DESCRIPTION("PMBus driver for Inspur Power System power supplies");
0226 MODULE_LICENSE("GPL");
0227 MODULE_IMPORT_NS(PMBUS);