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 ret = sbtsi_xfer(data, reg1, val1, true); 74 if (!ret) 75 ret = sbtsi_xfer(data, reg2, val2, true); 76 return ret; 77 } 78 79 /* 80 * Write integer and decimal parts of an SB-TSI temperature register pair. 81 */ 82 static int sbtsi_temp_write(struct sbtsi_data *data, u8 reg_int, u8 reg_dec, 83 u8 val_int, u8 val_dec) 84 { 85 int ret; 86 87 ret = sbtsi_xfer(data, reg_int, &val_int, false); 88 if (!ret) 89 ret = sbtsi_xfer(data, reg_dec, &val_dec, false); 90 return ret; 91 } 92 93 static int sbtsi_read(struct device *dev, enum hwmon_sensor_types type, 94 u32 attr, int channel, long *val) 95 { 96 struct sbtsi_data *data = dev_get_drvdata(dev); 97 s32 temp_int, temp_dec; 98 int err; 99 u8 val_int, val_dec; 100 101 switch (attr) { 102 case hwmon_temp_input: 103 if (data->read_order) 104 err = sbtsi_temp_read(data, 105 SBTSI_REG_TEMP_DEC, SBTSI_REG_TEMP_INT, 106 &val_dec, &val_int); 107 else 108 err = sbtsi_temp_read(data, 109 SBTSI_REG_TEMP_INT, SBTSI_REG_TEMP_DEC, 110 &val_int, &val_dec); 111 if (err < 0) 112 return err; 113 break; 114 case hwmon_temp_max: 115 err = sbtsi_temp_read(data, 116 SBTSI_REG_TEMP_HIGH_INT, SBTSI_REG_TEMP_HIGH_DEC, 117 &val_int, &val_dec); 118 if (err < 0) 119 return err; 120 break; 121 case hwmon_temp_min: 122 err = sbtsi_temp_read(data, 123 SBTSI_REG_TEMP_LOW_INT, SBTSI_REG_TEMP_LOW_DEC, 124 &val_int, &val_dec); 125 126 if (err < 0) 127 return err; 128 break; 129 default: 130 return -EINVAL; 131 } 132 133 temp_int = val_int; 134 temp_dec = val_dec; 135 *val = sbtsi_reg_to_mc(temp_int, temp_dec); 136 if (data->ext_range_mode) 137 *val -= SBTSI_TEMP_EXT_RANGE_ADJ; 138 139 return 0; 140 } 141 142 static int sbtsi_write(struct device *dev, enum hwmon_sensor_types type, 143 u32 attr, int channel, long val) 144 { 145 struct sbtsi_data *data = dev_get_drvdata(dev); 146 int reg_int, reg_dec; 147 u8 temp_int, temp_dec; 148 149 switch (attr) { 150 case hwmon_temp_max: 151 reg_int = SBTSI_REG_TEMP_HIGH_INT; 152 reg_dec = SBTSI_REG_TEMP_HIGH_DEC; 153 break; 154 case hwmon_temp_min: 155 reg_int = SBTSI_REG_TEMP_LOW_INT; 156 reg_dec = SBTSI_REG_TEMP_LOW_DEC; 157 break; 158 default: 159 return -EINVAL; 160 } 161 162 if (data->ext_range_mode) 163 val += SBTSI_TEMP_EXT_RANGE_ADJ; 164 val = clamp_val(val, SBTSI_TEMP_MIN, SBTSI_TEMP_MAX); 165 sbtsi_mc_to_reg(val, &temp_int, &temp_dec); 166 167 return sbtsi_temp_write(data, reg_int, reg_dec, temp_int, temp_dec); 168 } 169 170 static umode_t sbtsi_is_visible(const void *data, 171 enum hwmon_sensor_types type, 172 u32 attr, int channel) 173 { 174 switch (type) { 175 case hwmon_temp: 176 switch (attr) { 177 case hwmon_temp_input: 178 return 0444; 179 case hwmon_temp_min: 180 return 0644; 181 case hwmon_temp_max: 182 return 0644; 183 } 184 break; 185 default: 186 break; 187 } 188 return 0; 189 } 190 191 static const struct hwmon_channel_info * const sbtsi_info[] = { 192 HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ), 193 HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX), 194 NULL 195 }; 196 197 static const struct hwmon_ops sbtsi_hwmon_ops = { 198 .is_visible = sbtsi_is_visible, 199 .read = sbtsi_read, 200 .write = sbtsi_write, 201 }; 202 203 static const struct hwmon_chip_info sbtsi_chip_info = { 204 .ops = &sbtsi_hwmon_ops, 205 .info = sbtsi_info, 206 }; 207 208 static int sbtsi_probe(struct auxiliary_device *adev, 209 const struct auxiliary_device_id *id) 210 { 211 struct sbtsi_data *data = dev_get_drvdata(adev->dev.parent); 212 struct device *dev = &adev->dev; 213 struct device *hwmon_dev; 214 215 hwmon_dev = devm_hwmon_device_register_with_info(dev, "sbtsi", data, 216 &sbtsi_chip_info, NULL); 217 218 return PTR_ERR_OR_ZERO(hwmon_dev); 219 } 220 221 static const struct auxiliary_device_id sbtsi_id[] = { 222 { .name = AMD_SBTSI_ADEV "." AMD_SBTSI_AUX_HWMON }, 223 { } 224 }; 225 MODULE_DEVICE_TABLE(auxiliary, sbtsi_id); 226 227 static struct auxiliary_driver sbtsi_driver = { 228 .driver = { 229 .name = "sbtsi", 230 }, 231 .probe = sbtsi_probe, 232 .id_table = sbtsi_id, 233 }; 234 module_auxiliary_driver(sbtsi_driver); 235 236 MODULE_AUTHOR("Kun Yi <kunyi@google.com>"); 237 MODULE_DESCRIPTION("Hwmon driver for AMD SB-TSI emulated sensor"); 238 MODULE_LICENSE("GPL"); 239