1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * sbrmi.c - hwmon driver for a SB-RMI mailbox
4 * compliant AMD SoC device.
6 * Copyright (C) 2020-2021 Advanced Micro Devices, Inc.
9 #include <linux/delay.h>
10 #include <linux/err.h>
11 #include <linux/hwmon.h>
12 #include <linux/i2c.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
18 /* Do not allow setting negative power limit */
19 #define SBRMI_PWR_MIN 0
20 /* Mask for Status Register bit[1] */
21 #define SW_ALERT_MASK 0x2
23 /* Software Interrupt for triggering */
24 #define START_CMD 0x80
25 #define TRIGGER_MAILBOX 0x01
28 * SB-RMI supports soft mailbox service request to MP1 (power management
29 * firmware) through SBRMI inbound/outbound message registers.
33 SBRMI_READ_PKG_PWR_CONSUMPTION
= 0x1,
34 SBRMI_WRITE_PKG_PWR_LIMIT
,
35 SBRMI_READ_PKG_PWR_LIMIT
,
36 SBRMI_READ_PKG_MAX_PWR_LIMIT
,
39 /* SB-RMI registers */
43 SBRMI_OUTBNDMSG0
= 0x30,
62 /* Each client has this additional data */
64 struct i2c_client
*client
;
69 struct sbrmi_mailbox_msg
{
76 static int sbrmi_enable_alert(struct i2c_client
*client
)
81 * Enable the SB-RMI Software alert status
82 * by writing 0 to bit 4 of Control register(0x1)
84 ctrl
= i2c_smbus_read_byte_data(client
, SBRMI_CTRL
);
90 return i2c_smbus_write_byte_data(client
,
97 static int rmi_mailbox_xfer(struct sbrmi_data
*data
,
98 struct sbrmi_mailbox_msg
*msg
)
100 int i
, ret
, retry
= 10;
104 mutex_lock(&data
->lock
);
106 /* Indicate firmware a command is to be serviced */
107 ret
= i2c_smbus_write_byte_data(data
->client
,
108 SBRMI_INBNDMSG7
, START_CMD
);
112 /* Write the command to SBRMI::InBndMsg_inst0 */
113 ret
= i2c_smbus_write_byte_data(data
->client
,
114 SBRMI_INBNDMSG0
, msg
->cmd
);
119 * For both read and write the initiator (BMC) writes
120 * Command Data In[31:0] to SBRMI::InBndMsg_inst[4:1]
121 * SBRMI_x3C(MSB):SBRMI_x39(LSB)
123 for (i
= 0; i
< 4; i
++) {
124 byte
= (msg
->data_in
>> i
* 8) & 0xff;
125 ret
= i2c_smbus_write_byte_data(data
->client
,
126 SBRMI_INBNDMSG1
+ i
, byte
);
132 * Write 0x01 to SBRMI::SoftwareInterrupt to notify firmware to
133 * perform the requested read or write command
135 ret
= i2c_smbus_write_byte_data(data
->client
,
136 SBRMI_SW_INTERRUPT
, TRIGGER_MAILBOX
);
141 * Firmware will write SBRMI::Status[SwAlertSts]=1 to generate
142 * an ALERT (if enabled) to initiator (BMC) to indicate completion
143 * of the requested command
146 sw_status
= i2c_smbus_read_byte_data(data
->client
,
152 if (sw_status
& SW_ALERT_MASK
)
154 usleep_range(50, 100);
158 dev_err(&data
->client
->dev
,
159 "Firmware fail to indicate command completion\n");
165 * For a read operation, the initiator (BMC) reads the firmware
166 * response Command Data Out[31:0] from SBRMI::OutBndMsg_inst[4:1]
167 * {SBRMI_x34(MSB):SBRMI_x31(LSB)}.
170 for (i
= 0; i
< 4; i
++) {
171 ret
= i2c_smbus_read_byte_data(data
->client
,
172 SBRMI_OUTBNDMSG1
+ i
);
175 msg
->data_out
|= ret
<< i
* 8;
180 * BMC must write 1'b1 to SBRMI::Status[SwAlertSts] to clear the
183 ret
= i2c_smbus_write_byte_data(data
->client
, SBRMI_STATUS
,
184 sw_status
| SW_ALERT_MASK
);
187 mutex_unlock(&data
->lock
);
191 static int sbrmi_read(struct device
*dev
, enum hwmon_sensor_types type
,
192 u32 attr
, int channel
, long *val
)
194 struct sbrmi_data
*data
= dev_get_drvdata(dev
);
195 struct sbrmi_mailbox_msg msg
= { 0 };
198 if (type
!= hwmon_power
)
203 case hwmon_power_input
:
204 msg
.cmd
= SBRMI_READ_PKG_PWR_CONSUMPTION
;
205 ret
= rmi_mailbox_xfer(data
, &msg
);
207 case hwmon_power_cap
:
208 msg
.cmd
= SBRMI_READ_PKG_PWR_LIMIT
;
209 ret
= rmi_mailbox_xfer(data
, &msg
);
211 case hwmon_power_cap_max
:
212 msg
.data_out
= data
->pwr_limit_max
;
220 /* hwmon power attributes are in microWatt */
221 *val
= (long)msg
.data_out
* 1000;
225 static int sbrmi_write(struct device
*dev
, enum hwmon_sensor_types type
,
226 u32 attr
, int channel
, long val
)
228 struct sbrmi_data
*data
= dev_get_drvdata(dev
);
229 struct sbrmi_mailbox_msg msg
= { 0 };
231 if (type
!= hwmon_power
&& attr
!= hwmon_power_cap
)
234 * hwmon power attributes are in microWatt
235 * mailbox read/write is in mWatt
239 val
= clamp_val(val
, SBRMI_PWR_MIN
, data
->pwr_limit_max
);
241 msg
.cmd
= SBRMI_WRITE_PKG_PWR_LIMIT
;
245 return rmi_mailbox_xfer(data
, &msg
);
248 static umode_t
sbrmi_is_visible(const void *data
,
249 enum hwmon_sensor_types type
,
250 u32 attr
, int channel
)
255 case hwmon_power_input
:
256 case hwmon_power_cap_max
:
258 case hwmon_power_cap
:
268 static const struct hwmon_channel_info
* const sbrmi_info
[] = {
269 HWMON_CHANNEL_INFO(power
,
270 HWMON_P_INPUT
| HWMON_P_CAP
| HWMON_P_CAP_MAX
),
274 static const struct hwmon_ops sbrmi_hwmon_ops
= {
275 .is_visible
= sbrmi_is_visible
,
277 .write
= sbrmi_write
,
280 static const struct hwmon_chip_info sbrmi_chip_info
= {
281 .ops
= &sbrmi_hwmon_ops
,
285 static int sbrmi_get_max_pwr_limit(struct sbrmi_data
*data
)
287 struct sbrmi_mailbox_msg msg
= { 0 };
290 msg
.cmd
= SBRMI_READ_PKG_MAX_PWR_LIMIT
;
292 ret
= rmi_mailbox_xfer(data
, &msg
);
295 data
->pwr_limit_max
= msg
.data_out
;
300 static int sbrmi_probe(struct i2c_client
*client
)
302 struct device
*dev
= &client
->dev
;
303 struct device
*hwmon_dev
;
304 struct sbrmi_data
*data
;
307 data
= devm_kzalloc(dev
, sizeof(struct sbrmi_data
), GFP_KERNEL
);
311 data
->client
= client
;
312 mutex_init(&data
->lock
);
314 /* Enable alert for SB-RMI sequence */
315 ret
= sbrmi_enable_alert(client
);
319 /* Cache maximum power limit */
320 ret
= sbrmi_get_max_pwr_limit(data
);
324 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
, data
,
325 &sbrmi_chip_info
, NULL
);
327 return PTR_ERR_OR_ZERO(hwmon_dev
);
330 static const struct i2c_device_id sbrmi_id
[] = {
334 MODULE_DEVICE_TABLE(i2c
, sbrmi_id
);
336 static const struct of_device_id __maybe_unused sbrmi_of_match
[] = {
338 .compatible
= "amd,sbrmi",
342 MODULE_DEVICE_TABLE(of
, sbrmi_of_match
);
344 static struct i2c_driver sbrmi_driver
= {
347 .of_match_table
= of_match_ptr(sbrmi_of_match
),
349 .probe
= sbrmi_probe
,
350 .id_table
= sbrmi_id
,
353 module_i2c_driver(sbrmi_driver
);
355 MODULE_AUTHOR("Akshay Gupta <akshay.gupta@amd.com>");
356 MODULE_DESCRIPTION("Hwmon driver for AMD SB-RMI emulated sensor");
357 MODULE_LICENSE("GPL");