1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * sbtsi_temp.c - hwmon driver for a SBI Temperature Sensor Interface (SB-TSI)
4 * compliant AMD SoC temperature device.
6 * Copyright (c) 2020, Google Inc.
7 * Copyright (c) 2020, Kun Yi <kunyi@google.com>
10 #include <linux/err.h>
11 #include <linux/i2c.h>
12 #include <linux/init.h>
13 #include <linux/hwmon.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/of_device.h>
20 * SB-TSI registers only support SMBus byte data access. "_INT" registers are
21 * the integer part of a temperature value or limit, and "_DEC" registers are
22 * corresponding decimal parts.
24 #define SBTSI_REG_TEMP_INT 0x01 /* RO */
25 #define SBTSI_REG_STATUS 0x02 /* RO */
26 #define SBTSI_REG_CONFIG 0x03 /* RO */
27 #define SBTSI_REG_TEMP_HIGH_INT 0x07 /* RW */
28 #define SBTSI_REG_TEMP_LOW_INT 0x08 /* RW */
29 #define SBTSI_REG_TEMP_DEC 0x10 /* RW */
30 #define SBTSI_REG_TEMP_HIGH_DEC 0x13 /* RW */
31 #define SBTSI_REG_TEMP_LOW_DEC 0x14 /* RW */
33 #define SBTSI_CONFIG_READ_ORDER_SHIFT 5
35 #define SBTSI_TEMP_MIN 0
36 #define SBTSI_TEMP_MAX 255875
38 /* Each client has this additional data */
40 struct i2c_client *client;
45 * From SB-TSI spec: CPU temperature readings and limit registers encode the
46 * temperature in increments of 0.125 from 0 to 255.875. The "high byte"
47 * register encodes the base-2 of the integer portion, and the upper 3 bits of
48 * the "low byte" encode in base-2 the decimal portion.
50 * e.g. INT=0x19, DEC=0x20 represents 25.125 degrees Celsius
52 * Therefore temperature in millidegree Celsius =
53 * (INT + DEC / 256) * 1000 = (INT * 8 + DEC / 32) * 125
55 static inline int sbtsi_reg_to_mc(s32 integer, s32 decimal)
57 return ((integer << 3) + (decimal >> 5)) * 125;
61 * Inversely, given temperature in millidegree Celsius
62 * INT = (TEMP / 125) / 8
63 * DEC = ((TEMP / 125) % 8) * 32
64 * Caller have to make sure temp doesn't exceed 255875, the max valid value.
66 static inline void sbtsi_mc_to_reg(s32 temp, u8 *integer, u8 *decimal)
70 *decimal = (temp & 0x7) << 5;
73 static int sbtsi_read(struct device *dev, enum hwmon_sensor_types type,
74 u32 attr, int channel, long *val)
76 struct sbtsi_data *data = dev_get_drvdata(dev);
77 s32 temp_int, temp_dec;
81 case hwmon_temp_input:
83 * ReadOrder bit specifies the reading order of integer and
84 * decimal part of CPU temp for atomic reads. If bit == 0,
85 * reading integer part triggers latching of the decimal part,
86 * so integer part should be read first. If bit == 1, read
87 * order should be reversed.
89 err = i2c_smbus_read_byte_data(data->client, SBTSI_REG_CONFIG);
93 mutex_lock(&data->lock);
94 if (err & BIT(SBTSI_CONFIG_READ_ORDER_SHIFT)) {
95 temp_dec = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_DEC);
96 temp_int = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_INT);
98 temp_int = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_INT);
99 temp_dec = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_DEC);
101 mutex_unlock(&data->lock);
104 mutex_lock(&data->lock);
105 temp_int = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_HIGH_INT);
106 temp_dec = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_HIGH_DEC);
107 mutex_unlock(&data->lock);
110 mutex_lock(&data->lock);
111 temp_int = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_LOW_INT);
112 temp_dec = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_LOW_DEC);
113 mutex_unlock(&data->lock);
125 *val = sbtsi_reg_to_mc(temp_int, temp_dec);
130 static int sbtsi_write(struct device *dev, enum hwmon_sensor_types type,
131 u32 attr, int channel, long val)
133 struct sbtsi_data *data = dev_get_drvdata(dev);
134 int reg_int, reg_dec, err;
135 u8 temp_int, temp_dec;
139 reg_int = SBTSI_REG_TEMP_HIGH_INT;
140 reg_dec = SBTSI_REG_TEMP_HIGH_DEC;
143 reg_int = SBTSI_REG_TEMP_LOW_INT;
144 reg_dec = SBTSI_REG_TEMP_LOW_DEC;
150 val = clamp_val(val, SBTSI_TEMP_MIN, SBTSI_TEMP_MAX);
151 sbtsi_mc_to_reg(val, &temp_int, &temp_dec);
153 mutex_lock(&data->lock);
154 err = i2c_smbus_write_byte_data(data->client, reg_int, temp_int);
158 err = i2c_smbus_write_byte_data(data->client, reg_dec, temp_dec);
160 mutex_unlock(&data->lock);
164 static umode_t sbtsi_is_visible(const void *data,
165 enum hwmon_sensor_types type,
166 u32 attr, int channel)
171 case hwmon_temp_input:
185 static const struct hwmon_channel_info * const sbtsi_info[] = {
186 HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
187 HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX),
191 static const struct hwmon_ops sbtsi_hwmon_ops = {
192 .is_visible = sbtsi_is_visible,
194 .write = sbtsi_write,
197 static const struct hwmon_chip_info sbtsi_chip_info = {
198 .ops = &sbtsi_hwmon_ops,
202 static int sbtsi_probe(struct i2c_client *client)
204 struct device *dev = &client->dev;
205 struct device *hwmon_dev;
206 struct sbtsi_data *data;
208 data = devm_kzalloc(dev, sizeof(struct sbtsi_data), GFP_KERNEL);
212 data->client = client;
213 mutex_init(&data->lock);
215 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, data, &sbtsi_chip_info,
218 return PTR_ERR_OR_ZERO(hwmon_dev);
221 static const struct i2c_device_id sbtsi_id[] = {
225 MODULE_DEVICE_TABLE(i2c, sbtsi_id);
227 static const struct of_device_id __maybe_unused sbtsi_of_match[] = {
229 .compatible = "amd,sbtsi",
233 MODULE_DEVICE_TABLE(of, sbtsi_of_match);
235 static struct i2c_driver sbtsi_driver = {
236 .class = I2C_CLASS_HWMON,
239 .of_match_table = of_match_ptr(sbtsi_of_match),
241 .probe = sbtsi_probe,
242 .id_table = sbtsi_id,
245 module_i2c_driver(sbtsi_driver);
247 MODULE_AUTHOR("Kun Yi <kunyi@google.com>");
248 MODULE_DESCRIPTION("Hwmon driver for AMD SB-TSI emulated sensor");
249 MODULE_LICENSE("GPL");