George Hung | d9f9d7e | 2020-07-16 22:03:39 +0800 | [diff] [blame] | 1 | SB Temperature Sensor Interface (SB-TSI) is an SMBus compatible |
| 2 | interface that reports AMD SoC's Ttcl (normalized temperature), |
| 3 | and resembles a typical 8-pin remote temperature sensor's I2C interface |
| 4 | to BMC. |
| 5 | |
| 6 | This commit adds basic support using this interface to read CPU |
| 7 | temperature, and read/write high/low CPU temp thresholds. |
| 8 | |
| 9 | To instantiate this driver on an AMD CPU with SB-TSI |
| 10 | support, the i2c bus number would be the bus connected from the board |
| 11 | management controller (BMC) to the CPU. The i2c address is specified in |
| 12 | Section 6.3.1 of the spec [1]: The SB-TSI address is normally 98h for socket 0 |
| 13 | and 90h for socket 1, but it could vary based on hardware address select pins. |
| 14 | |
| 15 | [1]: https://www.amd.com/system/files/TechDocs/56255_OSRR.pdf |
| 16 | |
| 17 | Test status: tested reading temp1_input, and reading/writing |
| 18 | temp1_max/min. |
| 19 | |
| 20 | Signed-off-by: Kun Yi <kunyi at google.com> |
| 21 | --- |
| 22 | drivers/hwmon/Kconfig | 10 ++ |
| 23 | drivers/hwmon/Makefile | 1 + |
| 24 | drivers/hwmon/sbtsi_temp.c | 259 +++++++++++++++++++++++++++++++++++++ |
| 25 | 3 files changed, 270 insertions(+) |
| 26 | create mode 100644 drivers/hwmon/sbtsi_temp.c |
| 27 | |
| 28 | diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig |
| 29 | index 05a30832c6ba..9585dcd01d1b 100644 |
| 30 | --- a/drivers/hwmon/Kconfig |
| 31 | +++ b/drivers/hwmon/Kconfig |
| 32 | @@ -1412,6 +1412,16 @@ config SENSORS_RASPBERRYPI_HWMON |
| 33 | This driver can also be built as a module. If so, the module |
| 34 | will be called raspberrypi-hwmon. |
| 35 | |
| 36 | +config SENSORS_SBTSI |
| 37 | + tristate "Emulated SB-TSI temperature sensor" |
| 38 | + depends on I2C |
| 39 | + help |
| 40 | + If you say yes here you get support for emulated temperature |
| 41 | + sensors on AMD SoCs with SB-TSI interface connected to a BMC device. |
| 42 | + |
| 43 | + This driver can also be built as a module. If so, the module will |
| 44 | + be called sbtsi_temp. |
| 45 | + |
| 46 | config SENSORS_SHT15 |
| 47 | tristate "Sensiron humidity and temperature sensors. SHT15 and compat." |
| 48 | depends on GPIOLIB || COMPILE_TEST |
| 49 | diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile |
| 50 | index b0b9c8e57176..cd109f003ce4 100644 |
| 51 | --- a/drivers/hwmon/Makefile |
| 52 | +++ b/drivers/hwmon/Makefile |
| 53 | @@ -152,6 +152,7 @@ obj-$(CONFIG_SENSORS_POWR1220) += powr1220.o |
| 54 | obj-$(CONFIG_SENSORS_PWM_FAN) += pwm-fan.o |
| 55 | obj-$(CONFIG_SENSORS_RASPBERRYPI_HWMON) += raspberrypi-hwmon.o |
| 56 | obj-$(CONFIG_SENSORS_S3C) += s3c-hwmon.o |
| 57 | +obj-$(CONFIG_SENSORS_SBTSI) += sbtsi_temp.o |
| 58 | obj-$(CONFIG_SENSORS_SCH56XX_COMMON)+= sch56xx-common.o |
| 59 | obj-$(CONFIG_SENSORS_SCH5627) += sch5627.o |
| 60 | obj-$(CONFIG_SENSORS_SCH5636) += sch5636.o |
| 61 | diff --git a/drivers/hwmon/sbtsi_temp.c b/drivers/hwmon/sbtsi_temp.c |
| 62 | new file mode 100644 |
| 63 | index 000000000000..e3ad6a9f7ec1 |
| 64 | --- /dev/null |
| 65 | +++ b/drivers/hwmon/sbtsi_temp.c |
| 66 | @@ -0,0 +1,259 @@ |
| 67 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 68 | +/* |
| 69 | + * sbtsi_temp.c - hwmon driver for a SBI Temperature Sensor Interface (SB-TSI) |
| 70 | + * compliant AMD SoC temperature device. |
| 71 | + * |
| 72 | + * Copyright (c) 2020, Google Inc. |
| 73 | + * Copyright (c) 2020, Kun Yi <kunyi at google.com> |
| 74 | + */ |
| 75 | + |
| 76 | +#include <linux/err.h> |
| 77 | +#include <linux/i2c.h> |
| 78 | +#include <linux/init.h> |
| 79 | +#include <linux/hwmon.h> |
| 80 | +#include <linux/module.h> |
| 81 | +#include <linux/mutex.h> |
| 82 | +#include <linux/of_device.h> |
| 83 | +#include <linux/of.h> |
| 84 | + |
| 85 | +/* |
| 86 | + * SB-TSI registers only support SMBus byte data access. "_INT" registers are |
| 87 | + * the integer part of a temperature value or limit, and "_DEC" registers are |
| 88 | + * corresponding decimal parts. |
| 89 | + */ |
| 90 | +#define SBTSI_REG_TEMP_INT 0x01 /* RO */ |
| 91 | +#define SBTSI_REG_STATUS 0x02 /* RO */ |
| 92 | +#define SBTSI_REG_CONFIG 0x03 /* RO */ |
| 93 | +#define SBTSI_REG_TEMP_HIGH_INT 0x07 /* RW */ |
| 94 | +#define SBTSI_REG_TEMP_LOW_INT 0x08 /* RW */ |
| 95 | +#define SBTSI_REG_TEMP_DEC 0x10 /* RW */ |
| 96 | +#define SBTSI_REG_TEMP_HIGH_DEC 0x13 /* RW */ |
| 97 | +#define SBTSI_REG_TEMP_LOW_DEC 0x14 /* RW */ |
| 98 | +#define SBTSI_REG_REV 0xFF /* RO */ |
| 99 | + |
| 100 | +#define SBTSI_CONFIG_READ_ORDER_SHIFT 5 |
| 101 | + |
| 102 | +#define SBTSI_TEMP_MIN 0 |
| 103 | +#define SBTSI_TEMP_MAX 255875 |
| 104 | +#define SBTSI_REV_MAX_VALID_ID 4 |
| 105 | + |
| 106 | +/* Each client has this additional data */ |
| 107 | +struct sbtsi_data { |
| 108 | + struct i2c_client *client; |
| 109 | + struct mutex lock; |
| 110 | +}; |
| 111 | + |
| 112 | +/* |
| 113 | + * From SB-TSI spec: CPU temperature readings and limit registers encode the |
| 114 | + * temperature in increments of 0.125 from 0 to 255.875. The "high byte" |
| 115 | + * register encodes the base-2 of the integer portion, and the upper 3 bits of |
| 116 | + * the "low byte" encode in base-2 the decimal portion. |
| 117 | + * |
| 118 | + * e.g. INT=0x19, DEC=0x20 represents 25.125 degrees Celsius |
| 119 | + * |
| 120 | + * Therefore temperature in millidegree Celsius = |
| 121 | + * (INT + DEC / 256) * 1000 = (INT * 8 + DEC / 32) * 125 |
| 122 | + */ |
| 123 | +static inline int sbtsi_reg_to_mc(s32 integer, s32 decimal) |
| 124 | +{ |
| 125 | + return ((integer << 3) + (decimal >> 5)) * 125; |
| 126 | +} |
| 127 | + |
| 128 | +/* |
| 129 | + * Inversely, given temperature in millidegree Celsius |
| 130 | + * INT = (TEMP / 125) / 8 |
| 131 | + * DEC = ((TEMP / 125) % 8) * 32 |
| 132 | + * Caller have to make sure temp doesn't exceed 255875, the max valid value. |
| 133 | + */ |
| 134 | +static inline void sbtsi_mc_to_reg(s32 temp, u8 *integer, u8 *decimal) |
| 135 | +{ |
| 136 | + temp /= 125; |
| 137 | + *integer = temp >> 3; |
| 138 | + *decimal = (temp & 0x7) << 5; |
| 139 | +} |
| 140 | + |
| 141 | +static int sbtsi_read(struct device *dev, enum hwmon_sensor_types type, |
| 142 | + u32 attr, int channel, long *val) |
| 143 | +{ |
| 144 | + struct sbtsi_data *data = dev_get_drvdata(dev); |
| 145 | + s32 temp_int, temp_dec; |
| 146 | + int err, reg_int, reg_dec; |
| 147 | + u8 read_order; |
| 148 | + |
| 149 | + if (type != hwmon_temp) |
| 150 | + return -EINVAL; |
| 151 | + |
| 152 | + read_order = 0; |
| 153 | + switch (attr) { |
| 154 | + case hwmon_temp_input: |
| 155 | + /* |
| 156 | + * ReadOrder bit specifies the reading order of integer and |
| 157 | + * decimal part of CPU temp for atomic reads. If bit == 0, |
| 158 | + * reading integer part triggers latching of the decimal part, |
| 159 | + * so integer part should be read first. If bit == 1, read |
| 160 | + * order should be reversed. |
| 161 | + */ |
| 162 | + err = i2c_smbus_read_byte_data(data->client, SBTSI_REG_CONFIG); |
| 163 | + if (err < 0) |
| 164 | + return err; |
| 165 | + |
| 166 | + read_order = (u8)err & BIT(SBTSI_CONFIG_READ_ORDER_SHIFT); |
| 167 | + reg_int = SBTSI_REG_TEMP_INT; |
| 168 | + reg_dec = SBTSI_REG_TEMP_DEC; |
| 169 | + break; |
| 170 | + case hwmon_temp_max: |
| 171 | + reg_int = SBTSI_REG_TEMP_HIGH_INT; |
| 172 | + reg_dec = SBTSI_REG_TEMP_HIGH_DEC; |
| 173 | + break; |
| 174 | + case hwmon_temp_min: |
| 175 | + reg_int = SBTSI_REG_TEMP_LOW_INT; |
| 176 | + reg_dec = SBTSI_REG_TEMP_LOW_DEC; |
| 177 | + break; |
| 178 | + default: |
| 179 | + return -EINVAL; |
| 180 | + } |
| 181 | + |
| 182 | + if (read_order == 0) { |
| 183 | + temp_int = i2c_smbus_read_byte_data(data->client, reg_int); |
| 184 | + temp_dec = i2c_smbus_read_byte_data(data->client, reg_dec); |
| 185 | + } else { |
| 186 | + temp_dec = i2c_smbus_read_byte_data(data->client, reg_dec); |
| 187 | + temp_int = i2c_smbus_read_byte_data(data->client, reg_int); |
| 188 | + } |
| 189 | + |
| 190 | + if (temp_int < 0) |
| 191 | + return temp_int; |
| 192 | + if (temp_dec < 0) |
| 193 | + return temp_dec; |
| 194 | + |
| 195 | + *val = sbtsi_reg_to_mc(temp_int, temp_dec); |
| 196 | + |
| 197 | + return 0; |
| 198 | +} |
| 199 | + |
| 200 | +static int sbtsi_write(struct device *dev, enum hwmon_sensor_types type, |
| 201 | + u32 attr, int channel, long val) |
| 202 | +{ |
| 203 | + struct sbtsi_data *data = dev_get_drvdata(dev); |
| 204 | + int reg_int, reg_dec, err; |
| 205 | + u8 temp_int, temp_dec; |
| 206 | + |
| 207 | + if (type != hwmon_temp) |
| 208 | + return -EINVAL; |
| 209 | + |
| 210 | + switch (attr) { |
| 211 | + case hwmon_temp_max: |
| 212 | + reg_int = SBTSI_REG_TEMP_HIGH_INT; |
| 213 | + reg_dec = SBTSI_REG_TEMP_HIGH_DEC; |
| 214 | + break; |
| 215 | + case hwmon_temp_min: |
| 216 | + reg_int = SBTSI_REG_TEMP_LOW_INT; |
| 217 | + reg_dec = SBTSI_REG_TEMP_LOW_DEC; |
| 218 | + break; |
| 219 | + default: |
| 220 | + return -EINVAL; |
| 221 | + } |
| 222 | + |
| 223 | + val = clamp_val(val, SBTSI_TEMP_MIN, SBTSI_TEMP_MAX); |
| 224 | + mutex_lock(&data->lock); |
| 225 | + sbtsi_mc_to_reg(val, &temp_int, &temp_dec); |
| 226 | + err = i2c_smbus_write_byte_data(data->client, reg_int, temp_int); |
| 227 | + if (err) |
| 228 | + goto exit; |
| 229 | + |
| 230 | + err = i2c_smbus_write_byte_data(data->client, reg_dec, temp_dec); |
| 231 | +exit: |
| 232 | + mutex_unlock(&data->lock); |
| 233 | + return err; |
| 234 | +} |
| 235 | + |
| 236 | +static umode_t sbtsi_is_visible(const void *data, |
| 237 | + enum hwmon_sensor_types type, |
| 238 | + u32 attr, int channel) |
| 239 | +{ |
| 240 | + switch (type) { |
| 241 | + case hwmon_temp: |
| 242 | + switch (attr) { |
| 243 | + case hwmon_temp_input: |
| 244 | + return 0444; |
| 245 | + case hwmon_temp_min: |
| 246 | + return 0644; |
| 247 | + case hwmon_temp_max: |
| 248 | + return 0644; |
| 249 | + } |
| 250 | + break; |
| 251 | + default: |
| 252 | + break; |
| 253 | + } |
| 254 | + return 0; |
| 255 | +} |
| 256 | + |
| 257 | +static const struct hwmon_channel_info *sbtsi_info[] = { |
| 258 | + HWMON_CHANNEL_INFO(chip, |
| 259 | + HWMON_C_REGISTER_TZ), |
| 260 | + HWMON_CHANNEL_INFO(temp, |
| 261 | + HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX), |
| 262 | + NULL |
| 263 | +}; |
| 264 | + |
| 265 | +static const struct hwmon_ops sbtsi_hwmon_ops = { |
| 266 | + .is_visible = sbtsi_is_visible, |
| 267 | + .read = sbtsi_read, |
| 268 | + .write = sbtsi_write, |
| 269 | +}; |
| 270 | + |
| 271 | +static const struct hwmon_chip_info sbtsi_chip_info = { |
| 272 | + .ops = &sbtsi_hwmon_ops, |
| 273 | + .info = sbtsi_info, |
| 274 | +}; |
| 275 | + |
| 276 | +static int sbtsi_probe(struct i2c_client *client, |
| 277 | + const struct i2c_device_id *id) |
| 278 | +{ |
| 279 | + struct device *dev = &client->dev; |
| 280 | + struct device *hwmon_dev; |
| 281 | + struct sbtsi_data *data; |
| 282 | + |
| 283 | + data = devm_kzalloc(dev, sizeof(struct sbtsi_data), GFP_KERNEL); |
| 284 | + if (!data) |
| 285 | + return -ENOMEM; |
| 286 | + |
| 287 | + data->client = client; |
| 288 | + mutex_init(&data->lock); |
| 289 | + |
| 290 | + hwmon_dev = |
| 291 | + devm_hwmon_device_register_with_info(dev, client->name, data, |
| 292 | + &sbtsi_chip_info, NULL); |
| 293 | + |
| 294 | + return PTR_ERR_OR_ZERO(hwmon_dev); |
| 295 | +} |
| 296 | + |
| 297 | +static const struct i2c_device_id sbtsi_id[] = { |
| 298 | + {"sbtsi", 0}, |
| 299 | + {} |
| 300 | +}; |
| 301 | +MODULE_DEVICE_TABLE(i2c, sbtsi_id); |
| 302 | + |
| 303 | +static const struct of_device_id __maybe_unused sbtsi_of_match[] = { |
| 304 | + { |
| 305 | + .compatible = "amd,sbtsi", |
| 306 | + }, |
| 307 | + { }, |
| 308 | +}; |
| 309 | +MODULE_DEVICE_TABLE(of, sbtsi_of_match); |
| 310 | + |
| 311 | +static struct i2c_driver sbtsi_driver = { |
| 312 | + .class = I2C_CLASS_HWMON, |
| 313 | + .driver = { |
| 314 | + .name = "sbtsi", |
| 315 | + .of_match_table = of_match_ptr(sbtsi_of_match), |
| 316 | + }, |
| 317 | + .probe = sbtsi_probe, |
| 318 | + .id_table = sbtsi_id, |
| 319 | +}; |
| 320 | + |
| 321 | +module_i2c_driver(sbtsi_driver); |
| 322 | + |
| 323 | +MODULE_AUTHOR("Kun Yi <kunyi at google.com>"); |
| 324 | +MODULE_DESCRIPTION("Hwmon driver for AMD SB-TSI emulated sensor"); |
| 325 | +MODULE_LICENSE("GPL"); |
| 326 | -- |
| 327 | 2.26.0.292.g33ef6b2f38-goog |