Skip to content

Commit 013f7e7

Browse files
akky16gregkh
authored andcommitted
misc: amd-sbi: Use regmap subsystem
- regmap subsystem provides multiple benefits over direct smbus APIs - subsystem adds another abstraction layer on top of struct i2c_client to make it easy to read or write registers. - The subsystem can be helpful in following cases - Different types of bus (i2c/i3c), we have plans to support i3c. - Different Register address size (1byte/2byte) Reviewed-by: Naveen Krishna Chatradhi <[email protected]> Signed-off-by: Akshay Gupta <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent f4dc640 commit 013f7e7

File tree

3 files changed

+30
-27
lines changed

3 files changed

+30
-27
lines changed

drivers/misc/amd-sbi/rmi-core.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <linux/err.h>
1010
#include <linux/i2c.h>
1111
#include <linux/mutex.h>
12+
#include <linux/regmap.h>
1213
#include "rmi-core.h"
1314

1415
/* Mask for Status Register bit[1] */
@@ -21,21 +22,20 @@
2122
int rmi_mailbox_xfer(struct sbrmi_data *data,
2223
struct sbrmi_mailbox_msg *msg)
2324
{
25+
unsigned int bytes;
2426
int i, ret, retry = 10;
2527
int sw_status;
2628
u8 byte;
2729

2830
mutex_lock(&data->lock);
2931

3032
/* Indicate firmware a command is to be serviced */
31-
ret = i2c_smbus_write_byte_data(data->client,
32-
SBRMI_INBNDMSG7, START_CMD);
33+
ret = regmap_write(data->regmap, SBRMI_INBNDMSG7, START_CMD);
3334
if (ret < 0)
3435
goto exit_unlock;
3536

3637
/* Write the command to SBRMI::InBndMsg_inst0 */
37-
ret = i2c_smbus_write_byte_data(data->client,
38-
SBRMI_INBNDMSG0, msg->cmd);
38+
ret = regmap_write(data->regmap, SBRMI_INBNDMSG0, msg->cmd);
3939
if (ret < 0)
4040
goto exit_unlock;
4141

@@ -46,8 +46,7 @@ int rmi_mailbox_xfer(struct sbrmi_data *data,
4646
*/
4747
for (i = 0; i < 4; i++) {
4848
byte = (msg->data_in >> i * 8) & 0xff;
49-
ret = i2c_smbus_write_byte_data(data->client,
50-
SBRMI_INBNDMSG1 + i, byte);
49+
ret = regmap_write(data->regmap, SBRMI_INBNDMSG1 + i, byte);
5150
if (ret < 0)
5251
goto exit_unlock;
5352
}
@@ -56,8 +55,7 @@ int rmi_mailbox_xfer(struct sbrmi_data *data,
5655
* Write 0x01 to SBRMI::SoftwareInterrupt to notify firmware to
5756
* perform the requested read or write command
5857
*/
59-
ret = i2c_smbus_write_byte_data(data->client,
60-
SBRMI_SW_INTERRUPT, TRIGGER_MAILBOX);
58+
ret = regmap_write(data->regmap, SBRMI_SW_INTERRUPT, TRIGGER_MAILBOX);
6159
if (ret < 0)
6260
goto exit_unlock;
6361

@@ -67,8 +65,7 @@ int rmi_mailbox_xfer(struct sbrmi_data *data,
6765
* of the requested command
6866
*/
6967
do {
70-
sw_status = i2c_smbus_read_byte_data(data->client,
71-
SBRMI_STATUS);
68+
ret = regmap_read(data->regmap, SBRMI_STATUS, &sw_status);
7269
if (sw_status < 0) {
7370
ret = sw_status;
7471
goto exit_unlock;
@@ -79,8 +76,6 @@ int rmi_mailbox_xfer(struct sbrmi_data *data,
7976
} while (retry--);
8077

8178
if (retry < 0) {
82-
dev_err(&data->client->dev,
83-
"Firmware fail to indicate command completion\n");
8479
ret = -EIO;
8580
goto exit_unlock;
8681
}
@@ -92,20 +87,20 @@ int rmi_mailbox_xfer(struct sbrmi_data *data,
9287
*/
9388
if (msg->read) {
9489
for (i = 0; i < 4; i++) {
95-
ret = i2c_smbus_read_byte_data(data->client,
96-
SBRMI_OUTBNDMSG1 + i);
90+
ret = regmap_read(data->regmap,
91+
SBRMI_OUTBNDMSG1 + i, &bytes);
9792
if (ret < 0)
9893
goto exit_unlock;
99-
msg->data_out |= ret << i * 8;
94+
msg->data_out |= bytes << i * 8;
10095
}
10196
}
10297

10398
/*
10499
* BMC must write 1'b1 to SBRMI::Status[SwAlertSts] to clear the
105100
* ALERT to initiator
106101
*/
107-
ret = i2c_smbus_write_byte_data(data->client, SBRMI_STATUS,
108-
sw_status | SW_ALERT_MASK);
102+
ret = regmap_write(data->regmap, SBRMI_STATUS,
103+
sw_status | SW_ALERT_MASK);
109104

110105
exit_unlock:
111106
mutex_unlock(&data->lock);

drivers/misc/amd-sbi/rmi-core.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <linux/mutex.h>
1010
#include <linux/i2c.h>
1111
#include <linux/platform_device.h>
12+
#include <linux/regmap.h>
1213

1314
/* SB-RMI registers */
1415
enum sbrmi_reg {
@@ -47,7 +48,7 @@ enum sbrmi_msg_id {
4748

4849
/* Each client has this additional data */
4950
struct sbrmi_data {
50-
struct i2c_client *client;
51+
struct regmap *regmap;
5152
struct mutex lock;
5253
u32 pwr_limit_max;
5354
};

drivers/misc/amd-sbi/rmi-i2c.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@
1313
#include <linux/module.h>
1414
#include <linux/mutex.h>
1515
#include <linux/of.h>
16+
#include <linux/regmap.h>
1617
#include "rmi-core.h"
1718

18-
static int sbrmi_enable_alert(struct i2c_client *client)
19+
static int sbrmi_enable_alert(struct sbrmi_data *data)
1920
{
20-
int ctrl;
21+
int ctrl, ret;
2122

2223
/*
2324
* Enable the SB-RMI Software alert status
2425
* by writing 0 to bit 4 of Control register(0x1)
2526
*/
26-
ctrl = i2c_smbus_read_byte_data(client, SBRMI_CTRL);
27-
if (ctrl < 0)
28-
return ctrl;
27+
ret = regmap_read(data->regmap, SBRMI_CTRL, &ctrl);
28+
if (ret < 0)
29+
return ret;
2930

3031
if (ctrl & 0x10) {
3132
ctrl &= ~0x10;
32-
return i2c_smbus_write_byte_data(client,
33-
SBRMI_CTRL, ctrl);
33+
return regmap_write(data->regmap, SBRMI_CTRL, ctrl);
3434
}
3535

3636
return 0;
@@ -55,17 +55,24 @@ static int sbrmi_i2c_probe(struct i2c_client *client)
5555
{
5656
struct device *dev = &client->dev;
5757
struct sbrmi_data *data;
58+
struct regmap_config sbrmi_i2c_regmap_config = {
59+
.reg_bits = 8,
60+
.val_bits = 8,
61+
};
5862
int ret;
5963

6064
data = devm_kzalloc(dev, sizeof(struct sbrmi_data), GFP_KERNEL);
6165
if (!data)
6266
return -ENOMEM;
6367

64-
data->client = client;
6568
mutex_init(&data->lock);
6669

70+
data->regmap = devm_regmap_init_i2c(client, &sbrmi_i2c_regmap_config);
71+
if (IS_ERR(data->regmap))
72+
return PTR_ERR(data->regmap);
73+
6774
/* Enable alert for SB-RMI sequence */
68-
ret = sbrmi_enable_alert(client);
75+
ret = sbrmi_enable_alert(data);
6976
if (ret < 0)
7077
return ret;
7178

0 commit comments

Comments
 (0)