summaryrefslogtreecommitdiff
path: root/drivers/misc/amd-sbi/tsi-core.c
blob: 5c178702c67ab8c1c6a49eed8c7790d53abea69b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * tsi-core.c - file defining SB-TSI protocols compliant
 *              AMD SoC device.
 *
 * Copyright (C) 2026 Advanced Micro Devices, Inc.
 */

#include <linux/fs.h>
#include <linux/ioctl.h>
#include <linux/module.h>
#include <linux/uaccess.h>
#include <uapi/misc/amd-apml.h>
#include "tsi-core.h"

static inline struct sbtsi_i3c_priv *to_sbtsi_i3c_priv(struct sbtsi_data *data)
{
	return container_of(data, struct sbtsi_i3c_priv, data);
}

void sbtsi_data_release(struct kref *kref)
{
	struct sbtsi_data *data = container_of(kref, struct sbtsi_data, kref);

	mutex_destroy(&data->lock);
	if (data->is_i3c)
		kfree(to_sbtsi_i3c_priv(data));
	else
		kfree(data);
}

/* I2C transfer function */
static int sbtsi_i2c_xfer(struct sbtsi_data *data, u8 reg, u8 *val, bool is_read)
{
	if (is_read) {
		int ret = i2c_smbus_read_byte_data(data->client, reg);

		if (ret < 0)
			return ret;
		*val = ret;
		return 0;
	}
	return i2c_smbus_write_byte_data(data->client, reg, *val);
}

/* I3C read transfer function */
static int sbtsi_i3c_read(struct sbtsi_data *data, u8 reg, u8 *val)
{
	struct sbtsi_i3c_priv *priv = to_sbtsi_i3c_priv(data);
	struct i3c_xfer xfers[2] = { };
	int ret;

	priv->tx[0] = reg;

	/* Write the register address (DMA_TO_DEVICE). */
	xfers[0].rnw = false;
	xfers[0].len = 1;
	xfers[0].data.out = priv->tx;

	/* Read the data byte into a separate buffer (DMA_FROM_DEVICE). */
	xfers[1].rnw = true;
	xfers[1].len = 1;
	xfers[1].data.in = &priv->rx;

	ret = i3c_device_do_xfers(data->i3cdev, xfers, 2, I3C_SDR);
	if (ret)
		return ret;

	*val = priv->rx;
	return ret;
}

/* I3C write transfer function */
static int sbtsi_i3c_write(struct sbtsi_data *data, u8 reg, u8 val)
{
	struct sbtsi_i3c_priv *priv = to_sbtsi_i3c_priv(data);
	struct i3c_xfer xfers = {
		.rnw = false,
		.len = 2,
		.data.out = priv->tx,
	};

	priv->tx[0] = reg;
	priv->tx[1] = val;

	return i3c_device_do_xfers(data->i3cdev, &xfers, 1, I3C_SDR);
}

/* Unified transfer function for I2C and I3C access */
int sbtsi_xfer(struct sbtsi_data *data, u8 reg, u8 *val, bool is_read)
{
	if (data->is_i3c)
		return is_read ? sbtsi_i3c_read(data, reg, val)
			       : sbtsi_i3c_write(data, reg, *val);
	return sbtsi_i2c_xfer(data, reg, val, is_read);
}
EXPORT_SYMBOL_GPL(sbtsi_xfer);

/*
 * The mutex protects against concurrent register transfers to the device
 * over the shared bus.
 */
static int sbtsi_xfer_ioctl(struct sbtsi_data *data, u8 reg, u8 *val, bool is_read)
{
	guard(sbtsi)(data);

	if (data->detached)
		return -ENODEV;

	return sbtsi_xfer(data, reg, val, is_read);
}

static int apml_tsi_reg_xfer(struct sbtsi_data *data,
			     struct apml_tsi_xfer_msg __user *arg)
{
	struct apml_tsi_xfer_msg msg = { 0 };
	int ret;

	if (copy_from_user(&msg, arg, sizeof(struct apml_tsi_xfer_msg)))
		return -EFAULT;

	/*
	 * rflag is a boolean direction flag (0 = write, 1 = read). Reject
	 * any other value so the upper values stay reserved for future
	 * extensions instead of being silently treated as a read.
	 */
	if (msg.pad || msg.rflag > 1)
		return -EINVAL;

	ret = sbtsi_xfer_ioctl(data, msg.reg_addr, &msg.data_in_out, msg.rflag);

	if (msg.rflag && !ret) {
		if (copy_to_user(arg, &msg, sizeof(struct apml_tsi_xfer_msg)))
			return -EFAULT;
	}
	return ret;
}

static int sbtsi_open(struct inode *inode, struct file *fp)
{
	struct sbtsi_data *data;

	data = container_of(fp->private_data, struct sbtsi_data, sbtsi_misc_dev);
	scoped_guard(sbtsi, data) {
		if (data->detached)
			return -ENODEV;
	}

	kref_get(&data->kref);

	return 0;
}

static int sbtsi_release(struct inode *inode, struct file *fp)
{
	struct sbtsi_data *data;

	data = container_of(fp->private_data, struct sbtsi_data, sbtsi_misc_dev);
	kref_put(&data->kref, sbtsi_data_release);
	return 0;
}

static long sbtsi_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
{
	void __user *argp = (void __user *)arg;
	struct sbtsi_data *data;

	data = container_of(fp->private_data, struct sbtsi_data, sbtsi_misc_dev);
	switch (cmd) {
	case SBTSI_IOCTL_REG_XFER_CMD:
		return apml_tsi_reg_xfer(data, argp);
	default:
		return -ENOTTY;
	}
}

static const struct file_operations sbtsi_fops = {
	.owner		= THIS_MODULE,
	.open		= sbtsi_open,
	.release	= sbtsi_release,
	.unlocked_ioctl	= sbtsi_ioctl,
	.compat_ioctl	= compat_ptr_ioctl,
};

int create_misc_tsi_device(struct sbtsi_data *data, struct device *dev)
{
	int ret;

	data->sbtsi_misc_dev.name = devm_kasprintf(dev, GFP_KERNEL,
						   "sbtsi-%x", data->dev_addr);
	if (!data->sbtsi_misc_dev.name)
		return -ENOMEM;
	data->sbtsi_misc_dev.minor    = MISC_DYNAMIC_MINOR;
	data->sbtsi_misc_dev.fops     = &sbtsi_fops;
	data->sbtsi_misc_dev.parent   = dev;
	data->sbtsi_misc_dev.nodename = devm_kasprintf(dev, GFP_KERNEL,
						       "sbtsi-%x", data->dev_addr);
	if (!data->sbtsi_misc_dev.nodename)
		return -ENOMEM;
	data->sbtsi_misc_dev.mode = 0600;

	ret = misc_register(&data->sbtsi_misc_dev);
	if (ret)
		return ret;

	return 0;
}