Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * sbrmi.c - hwmon driver for a SB-RMI mailbox
0004  *           compliant AMD SoC device.
0005  *
0006  * Copyright (C) 2020-2021 Advanced Micro Devices, Inc.
0007  */
0008 
0009 #include <linux/delay.h>
0010 #include <linux/err.h>
0011 #include <linux/hwmon.h>
0012 #include <linux/i2c.h>
0013 #include <linux/init.h>
0014 #include <linux/module.h>
0015 #include <linux/mutex.h>
0016 #include <linux/of.h>
0017 
0018 /* Do not allow setting negative power limit */
0019 #define SBRMI_PWR_MIN   0
0020 /* Mask for Status Register bit[1] */
0021 #define SW_ALERT_MASK   0x2
0022 
0023 /* Software Interrupt for triggering */
0024 #define START_CMD   0x80
0025 #define TRIGGER_MAILBOX 0x01
0026 
0027 /*
0028  * SB-RMI supports soft mailbox service request to MP1 (power management
0029  * firmware) through SBRMI inbound/outbound message registers.
0030  * SB-RMI message IDs
0031  */
0032 enum sbrmi_msg_id {
0033     SBRMI_READ_PKG_PWR_CONSUMPTION = 0x1,
0034     SBRMI_WRITE_PKG_PWR_LIMIT,
0035     SBRMI_READ_PKG_PWR_LIMIT,
0036     SBRMI_READ_PKG_MAX_PWR_LIMIT,
0037 };
0038 
0039 /* SB-RMI registers */
0040 enum sbrmi_reg {
0041     SBRMI_CTRL      = 0x01,
0042     SBRMI_STATUS,
0043     SBRMI_OUTBNDMSG0    = 0x30,
0044     SBRMI_OUTBNDMSG1,
0045     SBRMI_OUTBNDMSG2,
0046     SBRMI_OUTBNDMSG3,
0047     SBRMI_OUTBNDMSG4,
0048     SBRMI_OUTBNDMSG5,
0049     SBRMI_OUTBNDMSG6,
0050     SBRMI_OUTBNDMSG7,
0051     SBRMI_INBNDMSG0,
0052     SBRMI_INBNDMSG1,
0053     SBRMI_INBNDMSG2,
0054     SBRMI_INBNDMSG3,
0055     SBRMI_INBNDMSG4,
0056     SBRMI_INBNDMSG5,
0057     SBRMI_INBNDMSG6,
0058     SBRMI_INBNDMSG7,
0059     SBRMI_SW_INTERRUPT,
0060 };
0061 
0062 /* Each client has this additional data */
0063 struct sbrmi_data {
0064     struct i2c_client *client;
0065     struct mutex lock;
0066     u32 pwr_limit_max;
0067 };
0068 
0069 struct sbrmi_mailbox_msg {
0070     u8 cmd;
0071     bool read;
0072     u32 data_in;
0073     u32 data_out;
0074 };
0075 
0076 static int sbrmi_enable_alert(struct i2c_client *client)
0077 {
0078     int ctrl;
0079 
0080     /*
0081      * Enable the SB-RMI Software alert status
0082      * by writing 0 to bit 4 of Control register(0x1)
0083      */
0084     ctrl = i2c_smbus_read_byte_data(client, SBRMI_CTRL);
0085     if (ctrl < 0)
0086         return ctrl;
0087 
0088     if (ctrl & 0x10) {
0089         ctrl &= ~0x10;
0090         return i2c_smbus_write_byte_data(client,
0091                          SBRMI_CTRL, ctrl);
0092     }
0093 
0094     return 0;
0095 }
0096 
0097 static int rmi_mailbox_xfer(struct sbrmi_data *data,
0098                 struct sbrmi_mailbox_msg *msg)
0099 {
0100     int i, ret, retry = 10;
0101     int sw_status;
0102     u8 byte;
0103 
0104     mutex_lock(&data->lock);
0105 
0106     /* Indicate firmware a command is to be serviced */
0107     ret = i2c_smbus_write_byte_data(data->client,
0108                     SBRMI_INBNDMSG7, START_CMD);
0109     if (ret < 0)
0110         goto exit_unlock;
0111 
0112     /* Write the command to SBRMI::InBndMsg_inst0 */
0113     ret = i2c_smbus_write_byte_data(data->client,
0114                     SBRMI_INBNDMSG0, msg->cmd);
0115     if (ret < 0)
0116         goto exit_unlock;
0117 
0118     /*
0119      * For both read and write the initiator (BMC) writes
0120      * Command Data In[31:0] to SBRMI::InBndMsg_inst[4:1]
0121      * SBRMI_x3C(MSB):SBRMI_x39(LSB)
0122      */
0123     for (i = 0; i < 4; i++) {
0124         byte = (msg->data_in >> i * 8) & 0xff;
0125         ret = i2c_smbus_write_byte_data(data->client,
0126                         SBRMI_INBNDMSG1 + i, byte);
0127         if (ret < 0)
0128             goto exit_unlock;
0129     }
0130 
0131     /*
0132      * Write 0x01 to SBRMI::SoftwareInterrupt to notify firmware to
0133      * perform the requested read or write command
0134      */
0135     ret = i2c_smbus_write_byte_data(data->client,
0136                     SBRMI_SW_INTERRUPT, TRIGGER_MAILBOX);
0137     if (ret < 0)
0138         goto exit_unlock;
0139 
0140     /*
0141      * Firmware will write SBRMI::Status[SwAlertSts]=1 to generate
0142      * an ALERT (if enabled) to initiator (BMC) to indicate completion
0143      * of the requested command
0144      */
0145     do {
0146         sw_status = i2c_smbus_read_byte_data(data->client,
0147                              SBRMI_STATUS);
0148         if (sw_status < 0) {
0149             ret = sw_status;
0150             goto exit_unlock;
0151         }
0152         if (sw_status & SW_ALERT_MASK)
0153             break;
0154         usleep_range(50, 100);
0155     } while (retry--);
0156 
0157     if (retry < 0) {
0158         dev_err(&data->client->dev,
0159             "Firmware fail to indicate command completion\n");
0160         ret = -EIO;
0161         goto exit_unlock;
0162     }
0163 
0164     /*
0165      * For a read operation, the initiator (BMC) reads the firmware
0166      * response Command Data Out[31:0] from SBRMI::OutBndMsg_inst[4:1]
0167      * {SBRMI_x34(MSB):SBRMI_x31(LSB)}.
0168      */
0169     if (msg->read) {
0170         for (i = 0; i < 4; i++) {
0171             ret = i2c_smbus_read_byte_data(data->client,
0172                                SBRMI_OUTBNDMSG1 + i);
0173             if (ret < 0)
0174                 goto exit_unlock;
0175             msg->data_out |= ret << i * 8;
0176         }
0177     }
0178 
0179     /*
0180      * BMC must write 1'b1 to SBRMI::Status[SwAlertSts] to clear the
0181      * ALERT to initiator
0182      */
0183     ret = i2c_smbus_write_byte_data(data->client, SBRMI_STATUS,
0184                     sw_status | SW_ALERT_MASK);
0185 
0186 exit_unlock:
0187     mutex_unlock(&data->lock);
0188     return ret;
0189 }
0190 
0191 static int sbrmi_read(struct device *dev, enum hwmon_sensor_types type,
0192               u32 attr, int channel, long *val)
0193 {
0194     struct sbrmi_data *data = dev_get_drvdata(dev);
0195     struct sbrmi_mailbox_msg msg = { 0 };
0196     int ret;
0197 
0198     if (type != hwmon_power)
0199         return -EINVAL;
0200 
0201     msg.read = true;
0202     switch (attr) {
0203     case hwmon_power_input:
0204         msg.cmd = SBRMI_READ_PKG_PWR_CONSUMPTION;
0205         ret = rmi_mailbox_xfer(data, &msg);
0206         break;
0207     case hwmon_power_cap:
0208         msg.cmd = SBRMI_READ_PKG_PWR_LIMIT;
0209         ret = rmi_mailbox_xfer(data, &msg);
0210         break;
0211     case hwmon_power_cap_max:
0212         msg.data_out = data->pwr_limit_max;
0213         ret = 0;
0214         break;
0215     default:
0216         return -EINVAL;
0217     }
0218     if (ret < 0)
0219         return ret;
0220     /* hwmon power attributes are in microWatt */
0221     *val = (long)msg.data_out * 1000;
0222     return ret;
0223 }
0224 
0225 static int sbrmi_write(struct device *dev, enum hwmon_sensor_types type,
0226                u32 attr, int channel, long val)
0227 {
0228     struct sbrmi_data *data = dev_get_drvdata(dev);
0229     struct sbrmi_mailbox_msg msg = { 0 };
0230 
0231     if (type != hwmon_power && attr != hwmon_power_cap)
0232         return -EINVAL;
0233     /*
0234      * hwmon power attributes are in microWatt
0235      * mailbox read/write is in mWatt
0236      */
0237     val /= 1000;
0238 
0239     val = clamp_val(val, SBRMI_PWR_MIN, data->pwr_limit_max);
0240 
0241     msg.cmd = SBRMI_WRITE_PKG_PWR_LIMIT;
0242     msg.data_in = val;
0243     msg.read = false;
0244 
0245     return rmi_mailbox_xfer(data, &msg);
0246 }
0247 
0248 static umode_t sbrmi_is_visible(const void *data,
0249                 enum hwmon_sensor_types type,
0250                 u32 attr, int channel)
0251 {
0252     switch (type) {
0253     case hwmon_power:
0254         switch (attr) {
0255         case hwmon_power_input:
0256         case hwmon_power_cap_max:
0257             return 0444;
0258         case hwmon_power_cap:
0259             return 0644;
0260         }
0261         break;
0262     default:
0263         break;
0264     }
0265     return 0;
0266 }
0267 
0268 static const struct hwmon_channel_info *sbrmi_info[] = {
0269     HWMON_CHANNEL_INFO(power,
0270                HWMON_P_INPUT | HWMON_P_CAP | HWMON_P_CAP_MAX),
0271     NULL
0272 };
0273 
0274 static const struct hwmon_ops sbrmi_hwmon_ops = {
0275     .is_visible = sbrmi_is_visible,
0276     .read = sbrmi_read,
0277     .write = sbrmi_write,
0278 };
0279 
0280 static const struct hwmon_chip_info sbrmi_chip_info = {
0281     .ops = &sbrmi_hwmon_ops,
0282     .info = sbrmi_info,
0283 };
0284 
0285 static int sbrmi_get_max_pwr_limit(struct sbrmi_data *data)
0286 {
0287     struct sbrmi_mailbox_msg msg = { 0 };
0288     int ret;
0289 
0290     msg.cmd = SBRMI_READ_PKG_MAX_PWR_LIMIT;
0291     msg.read = true;
0292     ret = rmi_mailbox_xfer(data, &msg);
0293     if (ret < 0)
0294         return ret;
0295     data->pwr_limit_max = msg.data_out;
0296 
0297     return ret;
0298 }
0299 
0300 static int sbrmi_probe(struct i2c_client *client,
0301                const struct i2c_device_id *id)
0302 {
0303     struct device *dev = &client->dev;
0304     struct device *hwmon_dev;
0305     struct sbrmi_data *data;
0306     int ret;
0307 
0308     data = devm_kzalloc(dev, sizeof(struct sbrmi_data), GFP_KERNEL);
0309     if (!data)
0310         return -ENOMEM;
0311 
0312     data->client = client;
0313     mutex_init(&data->lock);
0314 
0315     /* Enable alert for SB-RMI sequence */
0316     ret = sbrmi_enable_alert(client);
0317     if (ret < 0)
0318         return ret;
0319 
0320     /* Cache maximum power limit */
0321     ret = sbrmi_get_max_pwr_limit(data);
0322     if (ret < 0)
0323         return ret;
0324 
0325     hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, data,
0326                              &sbrmi_chip_info, NULL);
0327 
0328     return PTR_ERR_OR_ZERO(hwmon_dev);
0329 }
0330 
0331 static const struct i2c_device_id sbrmi_id[] = {
0332     {"sbrmi", 0},
0333     {}
0334 };
0335 MODULE_DEVICE_TABLE(i2c, sbrmi_id);
0336 
0337 static const struct of_device_id __maybe_unused sbrmi_of_match[] = {
0338     {
0339         .compatible = "amd,sbrmi",
0340     },
0341     { },
0342 };
0343 MODULE_DEVICE_TABLE(of, sbrmi_of_match);
0344 
0345 static struct i2c_driver sbrmi_driver = {
0346     .class = I2C_CLASS_HWMON,
0347     .driver = {
0348         .name = "sbrmi",
0349         .of_match_table = of_match_ptr(sbrmi_of_match),
0350     },
0351     .probe = sbrmi_probe,
0352     .id_table = sbrmi_id,
0353 };
0354 
0355 module_i2c_driver(sbrmi_driver);
0356 
0357 MODULE_AUTHOR("Akshay Gupta <akshay.gupta@amd.com>");
0358 MODULE_DESCRIPTION("Hwmon driver for AMD SB-RMI emulated sensor");
0359 MODULE_LICENSE("GPL");