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>
19 * SB-TSI registers only support SMBus byte data access. "_INT" registers are
20 * the integer part of a temperature value or limit, and "_DEC" registers are
21 * corresponding decimal parts.
23 #define SBTSI_REG_TEMP_INT 0x01 /* RO */
24 #define SBTSI_REG_STATUS 0x02 /* RO */
25 #define SBTSI_REG_CONFIG 0x03 /* RO */
26 #define SBTSI_REG_TEMP_HIGH_INT 0x07 /* RW */
27 #define SBTSI_REG_TEMP_LOW_INT 0x08 /* RW */
28 #define SBTSI_REG_TEMP_DEC 0x10 /* RW */
29 #define SBTSI_REG_TEMP_HIGH_DEC 0x13 /* RW */
30 #define SBTSI_REG_TEMP_LOW_DEC 0x14 /* RW */
32 #define SBTSI_CONFIG_READ_ORDER_SHIFT 5
34 #define SBTSI_TEMP_MIN 0
35 #define SBTSI_TEMP_MAX 255875
37 /* Each client has this additional data */
39 struct i2c_client
*client
;
44 * From SB-TSI spec: CPU temperature readings and limit registers encode the
45 * temperature in increments of 0.125 from 0 to 255.875. The "high byte"
46 * register encodes the base-2 of the integer portion, and the upper 3 bits of
47 * the "low byte" encode in base-2 the decimal portion.
49 * e.g. INT=0x19, DEC=0x20 represents 25.125 degrees Celsius
51 * Therefore temperature in millidegree Celsius =
52 * (INT + DEC / 256) * 1000 = (INT * 8 + DEC / 32) * 125
54 static inline int sbtsi_reg_to_mc(s32 integer
, s32 decimal
)
56 return ((integer
<< 3) + (decimal
>> 5)) * 125;
60 * Inversely, given temperature in millidegree Celsius
61 * INT = (TEMP / 125) / 8
62 * DEC = ((TEMP / 125) % 8) * 32
63 * Caller have to make sure temp doesn't exceed 255875, the max valid value.
65 static inline void sbtsi_mc_to_reg(s32 temp
, u8
*integer
, u8
*decimal
)
69 *decimal
= (temp
& 0x7) << 5;
72 static int sbtsi_read(struct device
*dev
, enum hwmon_sensor_types type
,
73 u32 attr
, int channel
, long *val
)
75 struct sbtsi_data
*data
= dev_get_drvdata(dev
);
76 s32 temp_int
, temp_dec
;
80 case hwmon_temp_input
:
82 * ReadOrder bit specifies the reading order of integer and
83 * decimal part of CPU temp for atomic reads. If bit == 0,
84 * reading integer part triggers latching of the decimal part,
85 * so integer part should be read first. If bit == 1, read
86 * order should be reversed.
88 err
= i2c_smbus_read_byte_data(data
->client
, SBTSI_REG_CONFIG
);
92 mutex_lock(&data
->lock
);
93 if (err
& BIT(SBTSI_CONFIG_READ_ORDER_SHIFT
)) {
94 temp_dec
= i2c_smbus_read_byte_data(data
->client
, SBTSI_REG_TEMP_DEC
);
95 temp_int
= i2c_smbus_read_byte_data(data
->client
, SBTSI_REG_TEMP_INT
);
97 temp_int
= i2c_smbus_read_byte_data(data
->client
, SBTSI_REG_TEMP_INT
);
98 temp_dec
= i2c_smbus_read_byte_data(data
->client
, SBTSI_REG_TEMP_DEC
);
100 mutex_unlock(&data
->lock
);
103 mutex_lock(&data
->lock
);
104 temp_int
= i2c_smbus_read_byte_data(data
->client
, SBTSI_REG_TEMP_HIGH_INT
);
105 temp_dec
= i2c_smbus_read_byte_data(data
->client
, SBTSI_REG_TEMP_HIGH_DEC
);
106 mutex_unlock(&data
->lock
);
109 mutex_lock(&data
->lock
);
110 temp_int
= i2c_smbus_read_byte_data(data
->client
, SBTSI_REG_TEMP_LOW_INT
);
111 temp_dec
= i2c_smbus_read_byte_data(data
->client
, SBTSI_REG_TEMP_LOW_DEC
);
112 mutex_unlock(&data
->lock
);
124 *val
= sbtsi_reg_to_mc(temp_int
, temp_dec
);
129 static int sbtsi_write(struct device
*dev
, enum hwmon_sensor_types type
,
130 u32 attr
, int channel
, long val
)
132 struct sbtsi_data
*data
= dev_get_drvdata(dev
);
133 int reg_int
, reg_dec
, err
;
134 u8 temp_int
, temp_dec
;
138 reg_int
= SBTSI_REG_TEMP_HIGH_INT
;
139 reg_dec
= SBTSI_REG_TEMP_HIGH_DEC
;
142 reg_int
= SBTSI_REG_TEMP_LOW_INT
;
143 reg_dec
= SBTSI_REG_TEMP_LOW_DEC
;
149 val
= clamp_val(val
, SBTSI_TEMP_MIN
, SBTSI_TEMP_MAX
);
150 sbtsi_mc_to_reg(val
, &temp_int
, &temp_dec
);
152 mutex_lock(&data
->lock
);
153 err
= i2c_smbus_write_byte_data(data
->client
, reg_int
, temp_int
);
157 err
= i2c_smbus_write_byte_data(data
->client
, reg_dec
, temp_dec
);
159 mutex_unlock(&data
->lock
);
163 static umode_t
sbtsi_is_visible(const void *data
,
164 enum hwmon_sensor_types type
,
165 u32 attr
, int channel
)
170 case hwmon_temp_input
:
184 static const struct hwmon_channel_info
* const sbtsi_info
[] = {
185 HWMON_CHANNEL_INFO(chip
, HWMON_C_REGISTER_TZ
),
186 HWMON_CHANNEL_INFO(temp
, HWMON_T_INPUT
| HWMON_T_MIN
| HWMON_T_MAX
),
190 static const struct hwmon_ops sbtsi_hwmon_ops
= {
191 .is_visible
= sbtsi_is_visible
,
193 .write
= sbtsi_write
,
196 static const struct hwmon_chip_info sbtsi_chip_info
= {
197 .ops
= &sbtsi_hwmon_ops
,
201 static int sbtsi_probe(struct i2c_client
*client
)
203 struct device
*dev
= &client
->dev
;
204 struct device
*hwmon_dev
;
205 struct sbtsi_data
*data
;
207 data
= devm_kzalloc(dev
, sizeof(struct sbtsi_data
), GFP_KERNEL
);
211 data
->client
= client
;
212 mutex_init(&data
->lock
);
214 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
, data
, &sbtsi_chip_info
,
217 return PTR_ERR_OR_ZERO(hwmon_dev
);
220 static const struct i2c_device_id sbtsi_id
[] = {
224 MODULE_DEVICE_TABLE(i2c
, sbtsi_id
);
226 static const struct of_device_id __maybe_unused sbtsi_of_match
[] = {
228 .compatible
= "amd,sbtsi",
232 MODULE_DEVICE_TABLE(of
, sbtsi_of_match
);
234 static struct i2c_driver sbtsi_driver
= {
237 .of_match_table
= of_match_ptr(sbtsi_of_match
),
239 .probe
= sbtsi_probe
,
240 .id_table
= sbtsi_id
,
243 module_i2c_driver(sbtsi_driver
);
245 MODULE_AUTHOR("Kun Yi <kunyi@google.com>");
246 MODULE_DESCRIPTION("Hwmon driver for AMD SB-TSI emulated sensor");
247 MODULE_LICENSE("GPL");