sbtsi_temp.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 <[email protected]>
  8. */
  9. #include <linux/err.h>
  10. #include <linux/i2c.h>
  11. #include <linux/init.h>
  12. #include <linux/hwmon.h>
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. #include <linux/of_device.h>
  16. #include <linux/of.h>
  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_CONFIG 0x03 /* RO */
  25. #define SBTSI_REG_TEMP_HIGH_INT 0x07 /* RW */
  26. #define SBTSI_REG_TEMP_LOW_INT 0x08 /* RW */
  27. #define SBTSI_REG_TEMP_DEC 0x10 /* RW */
  28. #define SBTSI_REG_TEMP_HIGH_DEC 0x13 /* RW */
  29. #define SBTSI_REG_TEMP_LOW_DEC 0x14 /* RW */
  30. #define SBTSI_CONFIG_READ_ORDER_SHIFT 5
  31. #define SBTSI_TEMP_MIN 0
  32. #define SBTSI_TEMP_MAX 255875
  33. /* Each client has this additional data */
  34. struct sbtsi_data {
  35. struct i2c_client *client;
  36. struct mutex lock;
  37. };
  38. /*
  39. * From SB-TSI spec: CPU temperature readings and limit registers encode the
  40. * temperature in increments of 0.125 from 0 to 255.875. The "high byte"
  41. * register encodes the base-2 of the integer portion, and the upper 3 bits of
  42. * the "low byte" encode in base-2 the decimal portion.
  43. *
  44. * e.g. INT=0x19, DEC=0x20 represents 25.125 degrees Celsius
  45. *
  46. * Therefore temperature in millidegree Celsius =
  47. * (INT + DEC / 256) * 1000 = (INT * 8 + DEC / 32) * 125
  48. */
  49. static inline int sbtsi_reg_to_mc(s32 integer, s32 decimal)
  50. {
  51. return ((integer << 3) + (decimal >> 5)) * 125;
  52. }
  53. /*
  54. * Inversely, given temperature in millidegree Celsius
  55. * INT = (TEMP / 125) / 8
  56. * DEC = ((TEMP / 125) % 8) * 32
  57. * Caller have to make sure temp doesn't exceed 255875, the max valid value.
  58. */
  59. static inline void sbtsi_mc_to_reg(s32 temp, u8 *integer, u8 *decimal)
  60. {
  61. temp /= 125;
  62. *integer = temp >> 3;
  63. *decimal = (temp & 0x7) << 5;
  64. }
  65. static int sbtsi_read(struct device *dev, enum hwmon_sensor_types type,
  66. u32 attr, int channel, long *val)
  67. {
  68. struct sbtsi_data *data = dev_get_drvdata(dev);
  69. s32 temp_int, temp_dec;
  70. int err;
  71. switch (attr) {
  72. case hwmon_temp_input:
  73. /*
  74. * ReadOrder bit specifies the reading order of integer and
  75. * decimal part of CPU temp for atomic reads. If bit == 0,
  76. * reading integer part triggers latching of the decimal part,
  77. * so integer part should be read first. If bit == 1, read
  78. * order should be reversed.
  79. */
  80. err = i2c_smbus_read_byte_data(data->client, SBTSI_REG_CONFIG);
  81. if (err < 0)
  82. return err;
  83. mutex_lock(&data->lock);
  84. if (err & BIT(SBTSI_CONFIG_READ_ORDER_SHIFT)) {
  85. temp_dec = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_DEC);
  86. temp_int = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_INT);
  87. } else {
  88. temp_int = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_INT);
  89. temp_dec = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_DEC);
  90. }
  91. mutex_unlock(&data->lock);
  92. break;
  93. case hwmon_temp_max:
  94. mutex_lock(&data->lock);
  95. temp_int = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_HIGH_INT);
  96. temp_dec = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_HIGH_DEC);
  97. mutex_unlock(&data->lock);
  98. break;
  99. case hwmon_temp_min:
  100. mutex_lock(&data->lock);
  101. temp_int = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_LOW_INT);
  102. temp_dec = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_LOW_DEC);
  103. mutex_unlock(&data->lock);
  104. break;
  105. default:
  106. return -EINVAL;
  107. }
  108. if (temp_int < 0)
  109. return temp_int;
  110. if (temp_dec < 0)
  111. return temp_dec;
  112. *val = sbtsi_reg_to_mc(temp_int, temp_dec);
  113. return 0;
  114. }
  115. static int sbtsi_write(struct device *dev, enum hwmon_sensor_types type,
  116. u32 attr, int channel, long val)
  117. {
  118. struct sbtsi_data *data = dev_get_drvdata(dev);
  119. int reg_int, reg_dec, err;
  120. u8 temp_int, temp_dec;
  121. switch (attr) {
  122. case hwmon_temp_max:
  123. reg_int = SBTSI_REG_TEMP_HIGH_INT;
  124. reg_dec = SBTSI_REG_TEMP_HIGH_DEC;
  125. break;
  126. case hwmon_temp_min:
  127. reg_int = SBTSI_REG_TEMP_LOW_INT;
  128. reg_dec = SBTSI_REG_TEMP_LOW_DEC;
  129. break;
  130. default:
  131. return -EINVAL;
  132. }
  133. val = clamp_val(val, SBTSI_TEMP_MIN, SBTSI_TEMP_MAX);
  134. sbtsi_mc_to_reg(val, &temp_int, &temp_dec);
  135. mutex_lock(&data->lock);
  136. err = i2c_smbus_write_byte_data(data->client, reg_int, temp_int);
  137. if (err)
  138. goto exit;
  139. err = i2c_smbus_write_byte_data(data->client, reg_dec, temp_dec);
  140. exit:
  141. mutex_unlock(&data->lock);
  142. return err;
  143. }
  144. static umode_t sbtsi_is_visible(const void *data,
  145. enum hwmon_sensor_types type,
  146. u32 attr, int channel)
  147. {
  148. switch (type) {
  149. case hwmon_temp:
  150. switch (attr) {
  151. case hwmon_temp_input:
  152. return 0444;
  153. case hwmon_temp_min:
  154. return 0644;
  155. case hwmon_temp_max:
  156. return 0644;
  157. }
  158. break;
  159. default:
  160. break;
  161. }
  162. return 0;
  163. }
  164. static const struct hwmon_channel_info *sbtsi_info[] = {
  165. HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
  166. HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX),
  167. NULL
  168. };
  169. static const struct hwmon_ops sbtsi_hwmon_ops = {
  170. .is_visible = sbtsi_is_visible,
  171. .read = sbtsi_read,
  172. .write = sbtsi_write,
  173. };
  174. static const struct hwmon_chip_info sbtsi_chip_info = {
  175. .ops = &sbtsi_hwmon_ops,
  176. .info = sbtsi_info,
  177. };
  178. static int sbtsi_probe(struct i2c_client *client,
  179. const struct i2c_device_id *id)
  180. {
  181. struct device *dev = &client->dev;
  182. struct device *hwmon_dev;
  183. struct sbtsi_data *data;
  184. data = devm_kzalloc(dev, sizeof(struct sbtsi_data), GFP_KERNEL);
  185. if (!data)
  186. return -ENOMEM;
  187. data->client = client;
  188. mutex_init(&data->lock);
  189. hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, data, &sbtsi_chip_info,
  190. NULL);
  191. return PTR_ERR_OR_ZERO(hwmon_dev);
  192. }
  193. static const struct i2c_device_id sbtsi_id[] = {
  194. {"sbtsi", 0},
  195. {}
  196. };
  197. MODULE_DEVICE_TABLE(i2c, sbtsi_id);
  198. static const struct of_device_id __maybe_unused sbtsi_of_match[] = {
  199. {
  200. .compatible = "amd,sbtsi",
  201. },
  202. { },
  203. };
  204. MODULE_DEVICE_TABLE(of, sbtsi_of_match);
  205. static struct i2c_driver sbtsi_driver = {
  206. .class = I2C_CLASS_HWMON,
  207. .driver = {
  208. .name = "sbtsi",
  209. .of_match_table = of_match_ptr(sbtsi_of_match),
  210. },
  211. .probe = sbtsi_probe,
  212. .id_table = sbtsi_id,
  213. };
  214. module_i2c_driver(sbtsi_driver);
  215. MODULE_AUTHOR("Kun Yi <[email protected]>");
  216. MODULE_DESCRIPTION("Hwmon driver for AMD SB-TSI emulated sensor");
  217. MODULE_LICENSE("GPL");