surface_temp.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Thermal sensor subsystem driver for Surface System Aggregator Module (SSAM).
  4. *
  5. * Copyright (C) 2022-2023 Maximilian Luz <luzmaximilian@gmail.com>
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/hwmon.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/types.h>
  12. #include <linux/surface_aggregator/controller.h>
  13. #include <linux/surface_aggregator/device.h>
  14. /* -- SAM interface. -------------------------------------------------------- */
  15. /*
  16. * Available sensors are indicated by a 16-bit bitfield, where a 1 marks the
  17. * presence of a sensor. So we have at most 16 possible sensors/channels.
  18. */
  19. #define SSAM_TMP_SENSOR_MAX_COUNT 16
  20. /*
  21. * All names observed so far are 6 characters long, but there's only
  22. * zeros after the name, so perhaps they can be longer. This number reflects
  23. * the maximum zero-padded space observed in the returned buffer.
  24. */
  25. #define SSAM_TMP_SENSOR_NAME_LENGTH 18
  26. struct ssam_tmp_get_name_rsp {
  27. __le16 unknown1;
  28. char unknown2;
  29. char name[SSAM_TMP_SENSOR_NAME_LENGTH];
  30. } __packed;
  31. static_assert(sizeof(struct ssam_tmp_get_name_rsp) == 21);
  32. SSAM_DEFINE_SYNC_REQUEST_CL_R(__ssam_tmp_get_available_sensors, __le16, {
  33. .target_category = SSAM_SSH_TC_TMP,
  34. .command_id = 0x04,
  35. });
  36. SSAM_DEFINE_SYNC_REQUEST_MD_R(__ssam_tmp_get_temperature, __le16, {
  37. .target_category = SSAM_SSH_TC_TMP,
  38. .command_id = 0x01,
  39. });
  40. SSAM_DEFINE_SYNC_REQUEST_MD_R(__ssam_tmp_get_name, struct ssam_tmp_get_name_rsp, {
  41. .target_category = SSAM_SSH_TC_TMP,
  42. .command_id = 0x0e,
  43. });
  44. static int ssam_tmp_get_available_sensors(struct ssam_device *sdev, s16 *sensors)
  45. {
  46. __le16 sensors_le;
  47. int status;
  48. status = __ssam_tmp_get_available_sensors(sdev, &sensors_le);
  49. if (status)
  50. return status;
  51. *sensors = le16_to_cpu(sensors_le);
  52. return 0;
  53. }
  54. static int ssam_tmp_get_temperature(struct ssam_device *sdev, u8 iid, long *temperature)
  55. {
  56. __le16 temp_le;
  57. int status;
  58. status = __ssam_tmp_get_temperature(sdev->ctrl, sdev->uid.target, iid, &temp_le);
  59. if (status)
  60. return status;
  61. /* Convert 1/10 °K to 1/1000 °C */
  62. *temperature = (le16_to_cpu(temp_le) - 2731) * 100L;
  63. return 0;
  64. }
  65. static int ssam_tmp_get_name(struct ssam_device *sdev, u8 iid, char *buf, size_t buf_len)
  66. {
  67. struct ssam_tmp_get_name_rsp name_rsp;
  68. int status;
  69. status = __ssam_tmp_get_name(sdev->ctrl, sdev->uid.target, iid, &name_rsp);
  70. if (status)
  71. return status;
  72. /*
  73. * This should not fail unless the name in the returned struct is not
  74. * null-terminated or someone changed something in the struct
  75. * definitions above, since our buffer and struct have the same
  76. * capacity by design. So if this fails, log an error message. Since
  77. * the more likely cause is that the returned string isn't
  78. * null-terminated, we might have received garbage (as opposed to just
  79. * an incomplete string), so also fail the function.
  80. */
  81. status = strscpy(buf, name_rsp.name, buf_len);
  82. if (status < 0) {
  83. dev_err(&sdev->dev, "received non-null-terminated sensor name string\n");
  84. return status;
  85. }
  86. return 0;
  87. }
  88. /* -- Driver.---------------------------------------------------------------- */
  89. struct ssam_temp {
  90. struct ssam_device *sdev;
  91. s16 sensors;
  92. char names[SSAM_TMP_SENSOR_MAX_COUNT][SSAM_TMP_SENSOR_NAME_LENGTH];
  93. };
  94. static umode_t ssam_temp_hwmon_is_visible(const void *data,
  95. enum hwmon_sensor_types type,
  96. u32 attr, int channel)
  97. {
  98. const struct ssam_temp *ssam_temp = data;
  99. if (!(ssam_temp->sensors & BIT(channel)))
  100. return 0;
  101. return 0444;
  102. }
  103. static int ssam_temp_hwmon_read(struct device *dev,
  104. enum hwmon_sensor_types type,
  105. u32 attr, int channel, long *value)
  106. {
  107. const struct ssam_temp *ssam_temp = dev_get_drvdata(dev);
  108. return ssam_tmp_get_temperature(ssam_temp->sdev, channel + 1, value);
  109. }
  110. static int ssam_temp_hwmon_read_string(struct device *dev,
  111. enum hwmon_sensor_types type,
  112. u32 attr, int channel, const char **str)
  113. {
  114. const struct ssam_temp *ssam_temp = dev_get_drvdata(dev);
  115. *str = ssam_temp->names[channel];
  116. return 0;
  117. }
  118. static const struct hwmon_channel_info * const ssam_temp_hwmon_info[] = {
  119. HWMON_CHANNEL_INFO(chip,
  120. HWMON_C_REGISTER_TZ),
  121. HWMON_CHANNEL_INFO(temp,
  122. HWMON_T_INPUT | HWMON_T_LABEL,
  123. HWMON_T_INPUT | HWMON_T_LABEL,
  124. HWMON_T_INPUT | HWMON_T_LABEL,
  125. HWMON_T_INPUT | HWMON_T_LABEL,
  126. HWMON_T_INPUT | HWMON_T_LABEL,
  127. HWMON_T_INPUT | HWMON_T_LABEL,
  128. HWMON_T_INPUT | HWMON_T_LABEL,
  129. HWMON_T_INPUT | HWMON_T_LABEL,
  130. HWMON_T_INPUT | HWMON_T_LABEL,
  131. HWMON_T_INPUT | HWMON_T_LABEL,
  132. HWMON_T_INPUT | HWMON_T_LABEL,
  133. HWMON_T_INPUT | HWMON_T_LABEL,
  134. HWMON_T_INPUT | HWMON_T_LABEL,
  135. HWMON_T_INPUT | HWMON_T_LABEL,
  136. HWMON_T_INPUT | HWMON_T_LABEL,
  137. HWMON_T_INPUT | HWMON_T_LABEL),
  138. NULL
  139. };
  140. static const struct hwmon_ops ssam_temp_hwmon_ops = {
  141. .is_visible = ssam_temp_hwmon_is_visible,
  142. .read = ssam_temp_hwmon_read,
  143. .read_string = ssam_temp_hwmon_read_string,
  144. };
  145. static const struct hwmon_chip_info ssam_temp_hwmon_chip_info = {
  146. .ops = &ssam_temp_hwmon_ops,
  147. .info = ssam_temp_hwmon_info,
  148. };
  149. static int ssam_temp_probe(struct ssam_device *sdev)
  150. {
  151. struct ssam_temp *ssam_temp;
  152. struct device *hwmon_dev;
  153. s16 sensors;
  154. int channel;
  155. int status;
  156. status = ssam_tmp_get_available_sensors(sdev, &sensors);
  157. if (status)
  158. return status;
  159. ssam_temp = devm_kzalloc(&sdev->dev, sizeof(*ssam_temp), GFP_KERNEL);
  160. if (!ssam_temp)
  161. return -ENOMEM;
  162. ssam_temp->sdev = sdev;
  163. ssam_temp->sensors = sensors;
  164. /* Retrieve the name for each available sensor. */
  165. for (channel = 0; channel < SSAM_TMP_SENSOR_MAX_COUNT; channel++) {
  166. if (!(sensors & BIT(channel)))
  167. continue;
  168. status = ssam_tmp_get_name(sdev, channel + 1, ssam_temp->names[channel],
  169. SSAM_TMP_SENSOR_NAME_LENGTH);
  170. if (status)
  171. return status;
  172. }
  173. hwmon_dev = devm_hwmon_device_register_with_info(&sdev->dev, "surface_thermal", ssam_temp,
  174. &ssam_temp_hwmon_chip_info, NULL);
  175. return PTR_ERR_OR_ZERO(hwmon_dev);
  176. }
  177. static const struct ssam_device_id ssam_temp_match[] = {
  178. { SSAM_SDEV(TMP, SAM, 0x00, 0x02) },
  179. { },
  180. };
  181. MODULE_DEVICE_TABLE(ssam, ssam_temp_match);
  182. static struct ssam_device_driver ssam_temp = {
  183. .probe = ssam_temp_probe,
  184. .match_table = ssam_temp_match,
  185. .driver = {
  186. .name = "surface_temp",
  187. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  188. },
  189. };
  190. module_ssam_device_driver(ssam_temp);
  191. MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  192. MODULE_DESCRIPTION("Thermal sensor subsystem driver for Surface System Aggregator Module");
  193. MODULE_LICENSE("GPL");