1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * sbtsi_temp.c - hwmon driver for a SBI Temperature Sensor Interface (SB-TSI)
4 * compliant AMD SoC temperature device.
5 *
6 * Copyright (c) 2020, Google Inc.
7 * Copyright (c) 2020, Kun Yi <kunyi@google.com>
8 */
9
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.h>
17
18 /*
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.
22 */
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 */
31
32 #define SBTSI_CONFIG_READ_ORDER_SHIFT 5
33
34 #define SBTSI_TEMP_MIN 0
35 #define SBTSI_TEMP_MAX 255875
36
37 /* Each client has this additional data */
38 struct sbtsi_data {
39 struct i2c_client *client;
40 struct mutex lock;
41 };
42
43 /*
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.
48 *
49 * e.g. INT=0x19, DEC=0x20 represents 25.125 degrees Celsius
50 *
51 * Therefore temperature in millidegree Celsius =
52 * (INT + DEC / 256) * 1000 = (INT * 8 + DEC / 32) * 125
53 */
sbtsi_reg_to_mc(s32 integer,s32 decimal)54 static inline int sbtsi_reg_to_mc(s32 integer, s32 decimal)
55 {
56 return ((integer << 3) + (decimal >> 5)) * 125;
57 }
58
59 /*
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.
64 */
sbtsi_mc_to_reg(s32 temp,u8 * integer,u8 * decimal)65 static inline void sbtsi_mc_to_reg(s32 temp, u8 *integer, u8 *decimal)
66 {
67 temp /= 125;
68 *integer = temp >> 3;
69 *decimal = (temp & 0x7) << 5;
70 }
71
sbtsi_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)72 static int sbtsi_read(struct device *dev, enum hwmon_sensor_types type,
73 u32 attr, int channel, long *val)
74 {
75 struct sbtsi_data *data = dev_get_drvdata(dev);
76 s32 temp_int, temp_dec;
77 int err;
78
79 switch (attr) {
80 case hwmon_temp_input:
81 /*
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.
87 */
88 err = i2c_smbus_read_byte_data(data->client, SBTSI_REG_CONFIG);
89 if (err < 0)
90 return err;
91
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);
96 } else {
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);
99 }
100 mutex_unlock(&data->lock);
101 break;
102 case hwmon_temp_max:
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);
107 break;
108 case hwmon_temp_min:
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);
113 break;
114 default:
115 return -EINVAL;
116 }
117
118
119 if (temp_int < 0)
120 return temp_int;
121 if (temp_dec < 0)
122 return temp_dec;
123
124 *val = sbtsi_reg_to_mc(temp_int, temp_dec);
125
126 return 0;
127 }
128
sbtsi_write(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long val)129 static int sbtsi_write(struct device *dev, enum hwmon_sensor_types type,
130 u32 attr, int channel, long val)
131 {
132 struct sbtsi_data *data = dev_get_drvdata(dev);
133 int reg_int, reg_dec, err;
134 u8 temp_int, temp_dec;
135
136 switch (attr) {
137 case hwmon_temp_max:
138 reg_int = SBTSI_REG_TEMP_HIGH_INT;
139 reg_dec = SBTSI_REG_TEMP_HIGH_DEC;
140 break;
141 case hwmon_temp_min:
142 reg_int = SBTSI_REG_TEMP_LOW_INT;
143 reg_dec = SBTSI_REG_TEMP_LOW_DEC;
144 break;
145 default:
146 return -EINVAL;
147 }
148
149 val = clamp_val(val, SBTSI_TEMP_MIN, SBTSI_TEMP_MAX);
150 sbtsi_mc_to_reg(val, &temp_int, &temp_dec);
151
152 mutex_lock(&data->lock);
153 err = i2c_smbus_write_byte_data(data->client, reg_int, temp_int);
154 if (err)
155 goto exit;
156
157 err = i2c_smbus_write_byte_data(data->client, reg_dec, temp_dec);
158 exit:
159 mutex_unlock(&data->lock);
160 return err;
161 }
162
sbtsi_is_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)163 static umode_t sbtsi_is_visible(const void *data,
164 enum hwmon_sensor_types type,
165 u32 attr, int channel)
166 {
167 switch (type) {
168 case hwmon_temp:
169 switch (attr) {
170 case hwmon_temp_input:
171 return 0444;
172 case hwmon_temp_min:
173 return 0644;
174 case hwmon_temp_max:
175 return 0644;
176 }
177 break;
178 default:
179 break;
180 }
181 return 0;
182 }
183
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),
187 NULL
188 };
189
190 static const struct hwmon_ops sbtsi_hwmon_ops = {
191 .is_visible = sbtsi_is_visible,
192 .read = sbtsi_read,
193 .write = sbtsi_write,
194 };
195
196 static const struct hwmon_chip_info sbtsi_chip_info = {
197 .ops = &sbtsi_hwmon_ops,
198 .info = sbtsi_info,
199 };
200
sbtsi_probe(struct i2c_client * client)201 static int sbtsi_probe(struct i2c_client *client)
202 {
203 struct device *dev = &client->dev;
204 struct device *hwmon_dev;
205 struct sbtsi_data *data;
206
207 data = devm_kzalloc(dev, sizeof(struct sbtsi_data), GFP_KERNEL);
208 if (!data)
209 return -ENOMEM;
210
211 data->client = client;
212 mutex_init(&data->lock);
213
214 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, data, &sbtsi_chip_info,
215 NULL);
216
217 return PTR_ERR_OR_ZERO(hwmon_dev);
218 }
219
220 static const struct i2c_device_id sbtsi_id[] = {
221 {"sbtsi", 0},
222 {}
223 };
224 MODULE_DEVICE_TABLE(i2c, sbtsi_id);
225
226 static const struct of_device_id __maybe_unused sbtsi_of_match[] = {
227 {
228 .compatible = "amd,sbtsi",
229 },
230 { },
231 };
232 MODULE_DEVICE_TABLE(of, sbtsi_of_match);
233
234 static struct i2c_driver sbtsi_driver = {
235 .class = I2C_CLASS_HWMON,
236 .driver = {
237 .name = "sbtsi",
238 .of_match_table = of_match_ptr(sbtsi_of_match),
239 },
240 .probe = sbtsi_probe,
241 .id_table = sbtsi_id,
242 };
243
244 module_i2c_driver(sbtsi_driver);
245
246 MODULE_AUTHOR("Kun Yi <kunyi@google.com>");
247 MODULE_DESCRIPTION("Hwmon driver for AMD SB-TSI emulated sensor");
248 MODULE_LICENSE("GPL");
249