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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* tsi.c - AMD SBTSI I2C/I3C core driver. Probes the SBTSI device over I2C/I3C
* and publishes an auxiliary device on the auxiliary bus.
*
* Copyright (C) 2026 Advanced Micro Devices, Inc.
*/
#include <linux/auxiliary_bus.h>
#include <linux/bitfield.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/slab.h>
#include "tsi-core.h"
#define SBTSI_REG_CONFIG 0x03 /* RO */
/*
* Bit for reporting value with temperature measurement range.
* bit == 0: Use default temperature range (0C to 255.875C).
* bit == 1: Use extended temperature range (-49C to +206.875C).
*/
#define SBTSI_CONFIG_EXT_RANGE_SHIFT 2
/*
* ReadOrder bit specifies the reading order of integer and decimal part of
* CPU temperature for atomic reads. If bit == 0, reading integer part triggers
* latching of the decimal part, so integer part should be read first.
*/
#define SBTSI_CONFIG_READ_ORDER_SHIFT 5
static void sbtsi_adev_release(struct device *dev)
{
kfree(to_auxiliary_dev(dev));
}
static void sbtsi_unregister_hwmon_adev(void *_adev)
{
struct auxiliary_device *adev = _adev;
auxiliary_device_delete(adev);
auxiliary_device_uninit(adev);
}
static void sbtsi_misc_unregister(void *arg)
{
struct sbtsi_data *data = arg;
misc_deregister(&data->sbtsi_misc_dev);
guard(sbtsi)(data);
data->detached = true;
}
static void sbtsi_driver_unref(void *arg)
{
struct sbtsi_data *data = arg;
kref_put(&data->kref, sbtsi_data_release);
}
/*
* Create and publish an auxiliary device. The hwmon driver in
* drivers/hwmon/sbtsi_temp.c binds to this device.
*
* @dev: I2C device (parent of the auxiliary device)
* @dev_addr: I2C address — used as the auxiliary device instance ID so that
* each socket gets a unique name.
*/
static int sbtsi_create_hwmon_adev(struct device *dev, u8 dev_addr)
{
struct auxiliary_device *adev;
int ret;
adev = kzalloc_obj(*adev);
if (!adev)
return -ENOMEM;
adev->name = AMD_SBTSI_AUX_HWMON;
adev->id = dev_addr;
adev->dev.parent = dev;
adev->dev.release = sbtsi_adev_release;
ret = auxiliary_device_init(adev);
if (ret) {
kfree(adev);
return ret;
}
ret = __auxiliary_device_add(adev, AMD_SBTSI_ADEV);
if (ret) {
auxiliary_device_uninit(adev);
return ret;
}
return devm_add_action_or_reset(dev, sbtsi_unregister_hwmon_adev, adev);
}
static int sbtsi_probe_common(struct device *dev, struct sbtsi_data *data)
{
u8 val;
int err;
mutex_init(&data->lock);
kref_init(&data->kref);
err = devm_add_action_or_reset(dev, sbtsi_driver_unref, data);
if (err)
return err;
err = sbtsi_xfer(data, SBTSI_REG_CONFIG, &val, true);
if (err)
return err;
data->ext_range_mode = FIELD_GET(BIT(SBTSI_CONFIG_EXT_RANGE_SHIFT), val);
data->read_order = FIELD_GET(BIT(SBTSI_CONFIG_READ_ORDER_SHIFT), val);
dev_set_drvdata(dev, data);
err = sbtsi_create_hwmon_adev(dev, data->dev_addr);
if (err < 0)
return err;
err = create_misc_tsi_device(data, dev);
if (err)
return err;
return devm_add_action_or_reset(dev, sbtsi_misc_unregister, data);
}
static int sbtsi_i2c_probe(struct i2c_client *client)
{
struct device *dev = &client->dev;
struct sbtsi_data *data;
data = kzalloc_obj(*data);
if (!data)
return -ENOMEM;
data->is_i3c = false;
data->client = client;
/* In a multi-socket system, devices that are otherwise identical do not
* share the same static address; each instance resides at a unique I2C
* client address on the same or different bus. Use the I2C client
* address as the auxiliary device instance ID to ensure each socket
* receives a distinct auxiliary device name.
*/
data->dev_addr = client->addr;
return sbtsi_probe_common(dev, data);
}
static const struct i2c_device_id sbtsi_id[] = {
{ .name = "sbtsi" },
{ }
};
MODULE_DEVICE_TABLE(i2c, sbtsi_id);
static const struct of_device_id __maybe_unused sbtsi_of_match[] = {
{
.compatible = "amd,sbtsi",
},
{ },
};
MODULE_DEVICE_TABLE(of, sbtsi_of_match);
static struct i2c_driver sbtsi_driver = {
.driver = {
.name = "sbtsi-i2c",
.of_match_table = of_match_ptr(sbtsi_of_match),
},
.probe = sbtsi_i2c_probe,
.id_table = sbtsi_id,
};
static int sbtsi_i3c_probe(struct i3c_device *i3cdev)
{
struct device *dev = i3cdev_to_dev(i3cdev);
struct i3c_device_info devinfo;
struct sbtsi_i3c_priv *i3c_priv;
struct sbtsi_data *data;
/*
* AMD OOB devices differ on basis of Instance ID,
* for SBTSI, instance ID is 0.
* As the device Id match is not on basis of Instance ID,
* add the below check to probe the SBTSI device only and
* not other OOB devices.
*/
i3c_device_get_info(i3cdev, &devinfo);
if (I3C_PID_INSTANCE_ID(devinfo.pid) != 0)
return -ENXIO;
i3c_priv = kzalloc_obj(*i3c_priv);
if (!i3c_priv)
return -ENOMEM;
data = &i3c_priv->data;
data->i3cdev = i3cdev;
data->is_i3c = true;
/*
* In a multi-socket system, otherwise identical devices do not share
* the same address; each instance is enumerated with a distinct dynamic
* (assigned) address on the I3C bus. Use that address (passed in as
* dev_addr) as the auxiliary device instance ID so that every socket
* gets a unique auxiliary device name.
*/
data->dev_addr = devinfo.dyn_addr;
return sbtsi_probe_common(dev, data);
}
static const struct i3c_device_id sbtsi_i3c_id[] = {
/* PID for AMD SBTSI device */
I3C_DEVICE_EXTRA_INFO(0x112, 0x0, 0x1, NULL), /* Socket:0, Turin and Genoa */
I3C_DEVICE_EXTRA_INFO(0x0, 0x0, 0x118, NULL), /* Socket:0, Venice */
I3C_DEVICE_EXTRA_INFO(0x0, 0x100, 0x118, NULL), /* Socket:1, Venice */
I3C_DEVICE_EXTRA_INFO(0x112, 0x0, 0x119, NULL), /* Socket:0, Venice */
I3C_DEVICE_EXTRA_INFO(0x112, 0x100, 0x119, NULL), /* Socket:1, Venice */
{}
};
MODULE_DEVICE_TABLE(i3c, sbtsi_i3c_id);
static struct i3c_driver sbtsi_i3c_driver = {
.driver = {
.name = "sbtsi-i3c",
},
.probe = sbtsi_i3c_probe,
.id_table = sbtsi_i3c_id,
};
module_i3c_i2c_driver(sbtsi_i3c_driver, &sbtsi_driver);
MODULE_DESCRIPTION("AMD SB-TSI I2C/I3C core driver");
MODULE_LICENSE("GPL");
|