xref: /linux/drivers/hwmon/sbtsi_temp.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
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/auxiliary_bus.h>
11 #include <linux/err.h>
12 #include <linux/hwmon.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/misc/tsi.h>
16 
17 /*
18  * SB-TSI registers only support SMBus byte data access. "_INT" registers are
19  * the integer part of a temperature value or limit, and "_DEC" registers are
20  * corresponding decimal parts.
21  */
22 #define SBTSI_REG_TEMP_INT		0x01 /* RO */
23 #define SBTSI_REG_STATUS		0x02 /* RO */
24 #define SBTSI_REG_TEMP_HIGH_INT		0x07 /* RW */
25 #define SBTSI_REG_TEMP_LOW_INT		0x08 /* RW */
26 #define SBTSI_REG_TEMP_DEC		0x10 /* RW */
27 #define SBTSI_REG_TEMP_HIGH_DEC		0x13 /* RW */
28 #define SBTSI_REG_TEMP_LOW_DEC		0x14 /* RW */
29 
30 #define SBTSI_TEMP_EXT_RANGE_ADJ	49000
31 
32 #define SBTSI_TEMP_MIN	0
33 #define SBTSI_TEMP_MAX	255875
34 
35 /*
36  * From SB-TSI spec: CPU temperature readings and limit registers encode the
37  * temperature in increments of 0.125 from 0 to 255.875. The "high byte"
38  * register encodes the base-2 of the integer portion, and the upper 3 bits of
39  * the "low byte" encode in base-2 the decimal portion.
40  *
41  * e.g. INT=0x19, DEC=0x20 represents 25.125 degrees Celsius
42  *
43  * Therefore temperature in millidegree Celsius =
44  *   (INT + DEC / 256) * 1000 = (INT * 8 + DEC / 32) * 125
45  */
46 static inline int sbtsi_reg_to_mc(s32 integer, s32 decimal)
47 {
48 	return ((integer << 3) + (decimal >> 5)) * 125;
49 }
50 
51 /*
52  * Inversely, given temperature in millidegree Celsius
53  *   INT = (TEMP / 125) / 8
54  *   DEC = ((TEMP / 125) % 8) * 32
55  * Caller have to make sure temp doesn't exceed 255875, the max valid value.
56  */
57 static inline void sbtsi_mc_to_reg(s32 temp, u8 *integer, u8 *decimal)
58 {
59 	temp /= 125;
60 	*integer = temp >> 3;
61 	*decimal = (temp & 0x7) << 5;
62 }
63 
64 /*
65  * Read integer and decimal parts of an SB-TSI temperature register pair
66  * The read order is determined by the ReadOrder bit to ensure atomic latching.
67  */
68 static int sbtsi_temp_read(struct sbtsi_data *data, u8 reg1, u8 reg2,
69 			   u8 *val1, u8 *val2)
70 {
71 	int ret;
72 
73 	guard(sbtsi)(data);
74 	ret = sbtsi_xfer(data, reg1, val1, true);
75 	if (!ret)
76 		ret = sbtsi_xfer(data, reg2, val2, true);
77 	return ret;
78 }
79 
80 /*
81  * Write integer and decimal parts of an SB-TSI temperature register pair.
82  */
83 static int sbtsi_temp_write(struct sbtsi_data *data, u8 reg_int, u8 reg_dec,
84 			    u8 val_int, u8 val_dec)
85 {
86 	int ret;
87 
88 	guard(sbtsi)(data);
89 	ret = sbtsi_xfer(data, reg_int, &val_int, false);
90 	if (!ret)
91 		ret = sbtsi_xfer(data, reg_dec, &val_dec, false);
92 	return ret;
93 }
94 
95 static int sbtsi_read(struct device *dev, enum hwmon_sensor_types type,
96 		      u32 attr, int channel, long *val)
97 {
98 	struct sbtsi_data *data = dev_get_drvdata(dev);
99 	s32 temp_int, temp_dec;
100 	int err;
101 	u8 val_int, val_dec;
102 
103 	switch (attr) {
104 	case hwmon_temp_input:
105 		if (data->read_order)
106 			err = sbtsi_temp_read(data,
107 					      SBTSI_REG_TEMP_DEC, SBTSI_REG_TEMP_INT,
108 					      &val_dec, &val_int);
109 		else
110 			err = sbtsi_temp_read(data,
111 					      SBTSI_REG_TEMP_INT, SBTSI_REG_TEMP_DEC,
112 					      &val_int, &val_dec);
113 		if (err < 0)
114 			return err;
115 		break;
116 	case hwmon_temp_max:
117 		err = sbtsi_temp_read(data,
118 				      SBTSI_REG_TEMP_HIGH_INT, SBTSI_REG_TEMP_HIGH_DEC,
119 				      &val_int, &val_dec);
120 		if (err < 0)
121 			return err;
122 		break;
123 	case hwmon_temp_min:
124 		err = sbtsi_temp_read(data,
125 				      SBTSI_REG_TEMP_LOW_INT, SBTSI_REG_TEMP_LOW_DEC,
126 				      &val_int, &val_dec);
127 
128 		if (err < 0)
129 			return err;
130 		break;
131 	default:
132 		return -EINVAL;
133 	}
134 
135 	temp_int = val_int;
136 	temp_dec = val_dec;
137 	*val = sbtsi_reg_to_mc(temp_int, temp_dec);
138 	if (data->ext_range_mode)
139 		*val -= SBTSI_TEMP_EXT_RANGE_ADJ;
140 
141 	return 0;
142 }
143 
144 static int sbtsi_write(struct device *dev, enum hwmon_sensor_types type,
145 		       u32 attr, int channel, long val)
146 {
147 	struct sbtsi_data *data = dev_get_drvdata(dev);
148 	int reg_int, reg_dec;
149 	u8 temp_int, temp_dec;
150 
151 	switch (attr) {
152 	case hwmon_temp_max:
153 		reg_int = SBTSI_REG_TEMP_HIGH_INT;
154 		reg_dec = SBTSI_REG_TEMP_HIGH_DEC;
155 		break;
156 	case hwmon_temp_min:
157 		reg_int = SBTSI_REG_TEMP_LOW_INT;
158 		reg_dec = SBTSI_REG_TEMP_LOW_DEC;
159 		break;
160 	default:
161 		return -EINVAL;
162 	}
163 
164 	if (data->ext_range_mode)
165 		val += SBTSI_TEMP_EXT_RANGE_ADJ;
166 	val = clamp_val(val, SBTSI_TEMP_MIN, SBTSI_TEMP_MAX);
167 	sbtsi_mc_to_reg(val, &temp_int, &temp_dec);
168 
169 	return sbtsi_temp_write(data, reg_int, reg_dec, temp_int, temp_dec);
170 }
171 
172 static umode_t sbtsi_is_visible(const void *data,
173 				enum hwmon_sensor_types type,
174 				u32 attr, int channel)
175 {
176 	switch (type) {
177 	case hwmon_temp:
178 		switch (attr) {
179 		case hwmon_temp_input:
180 			return 0444;
181 		case hwmon_temp_min:
182 			return 0644;
183 		case hwmon_temp_max:
184 			return 0644;
185 		}
186 		break;
187 	default:
188 		break;
189 	}
190 	return 0;
191 }
192 
193 static const struct hwmon_channel_info * const sbtsi_info[] = {
194 	HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
195 	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX),
196 	NULL
197 };
198 
199 static const struct hwmon_ops sbtsi_hwmon_ops = {
200 	.is_visible = sbtsi_is_visible,
201 	.read = sbtsi_read,
202 	.write = sbtsi_write,
203 };
204 
205 static const struct hwmon_chip_info sbtsi_chip_info = {
206 	.ops = &sbtsi_hwmon_ops,
207 	.info = sbtsi_info,
208 };
209 
210 static int sbtsi_probe(struct auxiliary_device *adev,
211 		       const struct auxiliary_device_id *id)
212 {
213 	struct sbtsi_data *data = dev_get_drvdata(adev->dev.parent);
214 	struct device *dev = &adev->dev;
215 	struct device *hwmon_dev;
216 
217 	hwmon_dev = devm_hwmon_device_register_with_info(dev, "sbtsi", data,
218 							 &sbtsi_chip_info, NULL);
219 
220 	return PTR_ERR_OR_ZERO(hwmon_dev);
221 }
222 
223 static const struct auxiliary_device_id sbtsi_id[] = {
224 	{ .name = AMD_SBTSI_ADEV "." AMD_SBTSI_AUX_HWMON },
225 	{ }
226 };
227 MODULE_DEVICE_TABLE(auxiliary, sbtsi_id);
228 
229 static struct auxiliary_driver sbtsi_driver = {
230 	.driver = {
231 		.name = "sbtsi",
232 	},
233 	.probe = sbtsi_probe,
234 	.id_table = sbtsi_id,
235 };
236 module_auxiliary_driver(sbtsi_driver);
237 
238 MODULE_AUTHOR("Kun Yi <kunyi@google.com>");
239 MODULE_DESCRIPTION("Hwmon driver for AMD SB-TSI emulated sensor");
240 MODULE_LICENSE("GPL");
241