surface_platform_profile.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Surface Platform Profile / Performance Mode driver for Surface System
  4. * Aggregator Module (thermal and fan subsystem).
  5. *
  6. * Copyright (C) 2021-2022 Maximilian Luz <luzmaximilian@gmail.com>
  7. */
  8. #include <linux/unaligned.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_profile.h>
  12. #include <linux/types.h>
  13. #include <linux/surface_aggregator/device.h>
  14. // Enum for the platform performance profile sent to the TMP module.
  15. enum ssam_tmp_profile {
  16. SSAM_TMP_PROFILE_NORMAL = 1,
  17. SSAM_TMP_PROFILE_BATTERY_SAVER = 2,
  18. SSAM_TMP_PROFILE_BETTER_PERFORMANCE = 3,
  19. SSAM_TMP_PROFILE_BEST_PERFORMANCE = 4,
  20. };
  21. // Enum for the fan profile sent to the FAN module. This fan profile is
  22. // only sent to the EC if the 'has_fan' property is set. The integers are
  23. // not a typo, they differ from the performance profile indices.
  24. enum ssam_fan_profile {
  25. SSAM_FAN_PROFILE_NORMAL = 2,
  26. SSAM_FAN_PROFILE_BATTERY_SAVER = 1,
  27. SSAM_FAN_PROFILE_BETTER_PERFORMANCE = 3,
  28. SSAM_FAN_PROFILE_BEST_PERFORMANCE = 4,
  29. };
  30. struct ssam_tmp_profile_info {
  31. __le32 profile;
  32. __le16 unknown1;
  33. __le16 unknown2;
  34. } __packed;
  35. struct ssam_platform_profile_device {
  36. struct ssam_device *sdev;
  37. struct platform_profile_handler handler;
  38. bool has_fan;
  39. };
  40. SSAM_DEFINE_SYNC_REQUEST_CL_R(__ssam_tmp_profile_get, struct ssam_tmp_profile_info, {
  41. .target_category = SSAM_SSH_TC_TMP,
  42. .command_id = 0x02,
  43. });
  44. SSAM_DEFINE_SYNC_REQUEST_CL_W(__ssam_tmp_profile_set, __le32, {
  45. .target_category = SSAM_SSH_TC_TMP,
  46. .command_id = 0x03,
  47. });
  48. SSAM_DEFINE_SYNC_REQUEST_W(__ssam_fan_profile_set, u8, {
  49. .target_category = SSAM_SSH_TC_FAN,
  50. .target_id = SSAM_SSH_TID_SAM,
  51. .command_id = 0x0e,
  52. .instance_id = 0x01,
  53. });
  54. static int ssam_tmp_profile_get(struct ssam_device *sdev, enum ssam_tmp_profile *p)
  55. {
  56. struct ssam_tmp_profile_info info;
  57. int status;
  58. status = ssam_retry(__ssam_tmp_profile_get, sdev, &info);
  59. if (status < 0)
  60. return status;
  61. *p = le32_to_cpu(info.profile);
  62. return 0;
  63. }
  64. static int ssam_tmp_profile_set(struct ssam_device *sdev, enum ssam_tmp_profile p)
  65. {
  66. const __le32 profile_le = cpu_to_le32(p);
  67. return ssam_retry(__ssam_tmp_profile_set, sdev, &profile_le);
  68. }
  69. static int ssam_fan_profile_set(struct ssam_device *sdev, enum ssam_fan_profile p)
  70. {
  71. const u8 profile = p;
  72. return ssam_retry(__ssam_fan_profile_set, sdev->ctrl, &profile);
  73. }
  74. static int convert_ssam_tmp_to_profile(struct ssam_device *sdev, enum ssam_tmp_profile p)
  75. {
  76. switch (p) {
  77. case SSAM_TMP_PROFILE_NORMAL:
  78. return PLATFORM_PROFILE_BALANCED;
  79. case SSAM_TMP_PROFILE_BATTERY_SAVER:
  80. return PLATFORM_PROFILE_LOW_POWER;
  81. case SSAM_TMP_PROFILE_BETTER_PERFORMANCE:
  82. return PLATFORM_PROFILE_BALANCED_PERFORMANCE;
  83. case SSAM_TMP_PROFILE_BEST_PERFORMANCE:
  84. return PLATFORM_PROFILE_PERFORMANCE;
  85. default:
  86. dev_err(&sdev->dev, "invalid performance profile: %d", p);
  87. return -EINVAL;
  88. }
  89. }
  90. static int convert_profile_to_ssam_tmp(struct ssam_device *sdev, enum platform_profile_option p)
  91. {
  92. switch (p) {
  93. case PLATFORM_PROFILE_LOW_POWER:
  94. return SSAM_TMP_PROFILE_BATTERY_SAVER;
  95. case PLATFORM_PROFILE_BALANCED:
  96. return SSAM_TMP_PROFILE_NORMAL;
  97. case PLATFORM_PROFILE_BALANCED_PERFORMANCE:
  98. return SSAM_TMP_PROFILE_BETTER_PERFORMANCE;
  99. case PLATFORM_PROFILE_PERFORMANCE:
  100. return SSAM_TMP_PROFILE_BEST_PERFORMANCE;
  101. default:
  102. /* This should have already been caught by platform_profile_store(). */
  103. WARN(true, "unsupported platform profile");
  104. return -EOPNOTSUPP;
  105. }
  106. }
  107. static int convert_profile_to_ssam_fan(struct ssam_device *sdev, enum platform_profile_option p)
  108. {
  109. switch (p) {
  110. case PLATFORM_PROFILE_LOW_POWER:
  111. return SSAM_FAN_PROFILE_BATTERY_SAVER;
  112. case PLATFORM_PROFILE_BALANCED:
  113. return SSAM_FAN_PROFILE_NORMAL;
  114. case PLATFORM_PROFILE_BALANCED_PERFORMANCE:
  115. return SSAM_FAN_PROFILE_BETTER_PERFORMANCE;
  116. case PLATFORM_PROFILE_PERFORMANCE:
  117. return SSAM_FAN_PROFILE_BEST_PERFORMANCE;
  118. default:
  119. /* This should have already been caught by platform_profile_store(). */
  120. WARN(true, "unsupported platform profile");
  121. return -EOPNOTSUPP;
  122. }
  123. }
  124. static int ssam_platform_profile_get(struct platform_profile_handler *pprof,
  125. enum platform_profile_option *profile)
  126. {
  127. struct ssam_platform_profile_device *tpd;
  128. enum ssam_tmp_profile tp;
  129. int status;
  130. tpd = container_of(pprof, struct ssam_platform_profile_device, handler);
  131. status = ssam_tmp_profile_get(tpd->sdev, &tp);
  132. if (status)
  133. return status;
  134. status = convert_ssam_tmp_to_profile(tpd->sdev, tp);
  135. if (status < 0)
  136. return status;
  137. *profile = status;
  138. return 0;
  139. }
  140. static int ssam_platform_profile_set(struct platform_profile_handler *pprof,
  141. enum platform_profile_option profile)
  142. {
  143. struct ssam_platform_profile_device *tpd;
  144. int tp;
  145. tpd = container_of(pprof, struct ssam_platform_profile_device, handler);
  146. tp = convert_profile_to_ssam_tmp(tpd->sdev, profile);
  147. if (tp < 0)
  148. return tp;
  149. tp = ssam_tmp_profile_set(tpd->sdev, tp);
  150. if (tp < 0)
  151. return tp;
  152. if (tpd->has_fan) {
  153. tp = convert_profile_to_ssam_fan(tpd->sdev, profile);
  154. if (tp < 0)
  155. return tp;
  156. tp = ssam_fan_profile_set(tpd->sdev, tp);
  157. }
  158. return tp;
  159. }
  160. static int surface_platform_profile_probe(struct ssam_device *sdev)
  161. {
  162. struct ssam_platform_profile_device *tpd;
  163. tpd = devm_kzalloc(&sdev->dev, sizeof(*tpd), GFP_KERNEL);
  164. if (!tpd)
  165. return -ENOMEM;
  166. tpd->sdev = sdev;
  167. tpd->handler.profile_get = ssam_platform_profile_get;
  168. tpd->handler.profile_set = ssam_platform_profile_set;
  169. tpd->has_fan = device_property_read_bool(&sdev->dev, "has_fan");
  170. set_bit(PLATFORM_PROFILE_LOW_POWER, tpd->handler.choices);
  171. set_bit(PLATFORM_PROFILE_BALANCED, tpd->handler.choices);
  172. set_bit(PLATFORM_PROFILE_BALANCED_PERFORMANCE, tpd->handler.choices);
  173. set_bit(PLATFORM_PROFILE_PERFORMANCE, tpd->handler.choices);
  174. return platform_profile_register(&tpd->handler);
  175. }
  176. static void surface_platform_profile_remove(struct ssam_device *sdev)
  177. {
  178. platform_profile_remove();
  179. }
  180. static const struct ssam_device_id ssam_platform_profile_match[] = {
  181. { SSAM_SDEV(TMP, SAM, 0x00, 0x01) },
  182. { },
  183. };
  184. MODULE_DEVICE_TABLE(ssam, ssam_platform_profile_match);
  185. static struct ssam_device_driver surface_platform_profile = {
  186. .probe = surface_platform_profile_probe,
  187. .remove = surface_platform_profile_remove,
  188. .match_table = ssam_platform_profile_match,
  189. .driver = {
  190. .name = "surface_platform_profile",
  191. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  192. },
  193. };
  194. module_ssam_device_driver(surface_platform_profile);
  195. MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  196. MODULE_DESCRIPTION("Platform Profile Support for Surface System Aggregator Module");
  197. MODULE_LICENSE("GPL");