sysfs.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-2.0+
  2. // Copyright IBM Corp 2019
  3. #include <linux/bitops.h>
  4. #include <linux/device.h>
  5. #include <linux/export.h>
  6. #include <linux/hwmon-sysfs.h>
  7. #include <linux/kernel.h>
  8. #include <linux/kstrtox.h>
  9. #include <linux/sysfs.h>
  10. #include "common.h"
  11. /* OCC status register */
  12. #define OCC_STAT_MASTER BIT(7)
  13. /* OCC extended status register */
  14. #define OCC_EXT_STAT_DVFS_OT BIT(7)
  15. #define OCC_EXT_STAT_DVFS_POWER BIT(6)
  16. #define OCC_EXT_STAT_MEM_THROTTLE BIT(5)
  17. #define OCC_EXT_STAT_QUICK_DROP BIT(4)
  18. #define OCC_EXT_STAT_DVFS_VDD BIT(3)
  19. #define OCC_EXT_STAT_GPU_THROTTLE GENMASK(2, 0)
  20. static ssize_t occ_active_store(struct device *dev,
  21. struct device_attribute *attr,
  22. const char *buf, size_t count)
  23. {
  24. int rc;
  25. bool active;
  26. struct occ *occ = dev_get_drvdata(dev);
  27. rc = kstrtobool(buf, &active);
  28. if (rc)
  29. return rc;
  30. rc = occ_active(occ, active);
  31. if (rc)
  32. return rc;
  33. return count;
  34. }
  35. static ssize_t occ_sysfs_show(struct device *dev,
  36. struct device_attribute *attr, char *buf)
  37. {
  38. int rc;
  39. int val = 0;
  40. struct occ *occ = dev_get_drvdata(dev);
  41. struct occ_poll_response_header *header;
  42. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  43. if (occ->active) {
  44. rc = occ_update_response(occ);
  45. if (rc)
  46. return rc;
  47. header = (struct occ_poll_response_header *)occ->resp.data;
  48. switch (sattr->index) {
  49. case 0:
  50. val = !!(header->status & OCC_STAT_MASTER);
  51. break;
  52. case 1:
  53. val = 1;
  54. break;
  55. case 2:
  56. val = !!(header->ext_status & OCC_EXT_STAT_DVFS_OT);
  57. break;
  58. case 3:
  59. val = !!(header->ext_status & OCC_EXT_STAT_DVFS_POWER);
  60. break;
  61. case 4:
  62. val = !!(header->ext_status &
  63. OCC_EXT_STAT_MEM_THROTTLE);
  64. break;
  65. case 5:
  66. val = !!(header->ext_status & OCC_EXT_STAT_QUICK_DROP);
  67. break;
  68. case 6:
  69. val = header->occ_state;
  70. break;
  71. case 7:
  72. if (header->status & OCC_STAT_MASTER)
  73. val = hweight8(header->occs_present);
  74. else
  75. val = 1;
  76. break;
  77. case 8:
  78. val = header->ips_status;
  79. break;
  80. case 9:
  81. val = header->mode;
  82. break;
  83. case 10:
  84. val = !!(header->ext_status & OCC_EXT_STAT_DVFS_VDD);
  85. break;
  86. case 11:
  87. val = header->ext_status & OCC_EXT_STAT_GPU_THROTTLE;
  88. break;
  89. default:
  90. return -EINVAL;
  91. }
  92. } else {
  93. if (sattr->index == 1)
  94. val = 0;
  95. else if (sattr->index <= 11)
  96. val = -ENODATA;
  97. else
  98. return -EINVAL;
  99. }
  100. return sysfs_emit(buf, "%d\n", val);
  101. }
  102. static ssize_t occ_error_show(struct device *dev,
  103. struct device_attribute *attr, char *buf)
  104. {
  105. struct occ *occ = dev_get_drvdata(dev);
  106. occ_update_response(occ);
  107. return sysfs_emit(buf, "%d\n", occ->error);
  108. }
  109. static SENSOR_DEVICE_ATTR(occ_master, 0444, occ_sysfs_show, NULL, 0);
  110. static SENSOR_DEVICE_ATTR(occ_active, 0644, occ_sysfs_show, occ_active_store,
  111. 1);
  112. static SENSOR_DEVICE_ATTR(occ_dvfs_overtemp, 0444, occ_sysfs_show, NULL, 2);
  113. static SENSOR_DEVICE_ATTR(occ_dvfs_power, 0444, occ_sysfs_show, NULL, 3);
  114. static SENSOR_DEVICE_ATTR(occ_mem_throttle, 0444, occ_sysfs_show, NULL, 4);
  115. static SENSOR_DEVICE_ATTR(occ_quick_pwr_drop, 0444, occ_sysfs_show, NULL, 5);
  116. static SENSOR_DEVICE_ATTR(occ_state, 0444, occ_sysfs_show, NULL, 6);
  117. static SENSOR_DEVICE_ATTR(occs_present, 0444, occ_sysfs_show, NULL, 7);
  118. static SENSOR_DEVICE_ATTR(occ_ips_status, 0444, occ_sysfs_show, NULL, 8);
  119. static SENSOR_DEVICE_ATTR(occ_mode, 0444, occ_sysfs_show, NULL, 9);
  120. static SENSOR_DEVICE_ATTR(occ_dvfs_vdd, 0444, occ_sysfs_show, NULL, 10);
  121. static SENSOR_DEVICE_ATTR(occ_gpu_throttle, 0444, occ_sysfs_show, NULL, 11);
  122. static DEVICE_ATTR_RO(occ_error);
  123. static struct attribute *occ_attributes[] = {
  124. &sensor_dev_attr_occ_master.dev_attr.attr,
  125. &sensor_dev_attr_occ_active.dev_attr.attr,
  126. &sensor_dev_attr_occ_dvfs_overtemp.dev_attr.attr,
  127. &sensor_dev_attr_occ_dvfs_power.dev_attr.attr,
  128. &sensor_dev_attr_occ_mem_throttle.dev_attr.attr,
  129. &sensor_dev_attr_occ_quick_pwr_drop.dev_attr.attr,
  130. &sensor_dev_attr_occ_state.dev_attr.attr,
  131. &sensor_dev_attr_occs_present.dev_attr.attr,
  132. &sensor_dev_attr_occ_ips_status.dev_attr.attr,
  133. &sensor_dev_attr_occ_mode.dev_attr.attr,
  134. &sensor_dev_attr_occ_dvfs_vdd.dev_attr.attr,
  135. &sensor_dev_attr_occ_gpu_throttle.dev_attr.attr,
  136. &dev_attr_occ_error.attr,
  137. NULL
  138. };
  139. static const struct attribute_group occ_sysfs = {
  140. .attrs = occ_attributes,
  141. };
  142. void occ_sysfs_poll_done(struct occ *occ)
  143. {
  144. const char *name;
  145. struct occ_poll_response_header *header =
  146. (struct occ_poll_response_header *)occ->resp.data;
  147. /*
  148. * On the first poll response, we haven't yet created the sysfs
  149. * attributes, so don't make any notify calls.
  150. */
  151. if (!occ->active)
  152. goto done;
  153. if ((header->status & OCC_STAT_MASTER) !=
  154. (occ->prev_stat & OCC_STAT_MASTER)) {
  155. name = sensor_dev_attr_occ_master.dev_attr.attr.name;
  156. sysfs_notify(&occ->bus_dev->kobj, NULL, name);
  157. }
  158. if ((header->ext_status & OCC_EXT_STAT_DVFS_OT) !=
  159. (occ->prev_ext_stat & OCC_EXT_STAT_DVFS_OT)) {
  160. name = sensor_dev_attr_occ_dvfs_overtemp.dev_attr.attr.name;
  161. sysfs_notify(&occ->bus_dev->kobj, NULL, name);
  162. }
  163. if ((header->ext_status & OCC_EXT_STAT_DVFS_POWER) !=
  164. (occ->prev_ext_stat & OCC_EXT_STAT_DVFS_POWER)) {
  165. name = sensor_dev_attr_occ_dvfs_power.dev_attr.attr.name;
  166. sysfs_notify(&occ->bus_dev->kobj, NULL, name);
  167. }
  168. if ((header->ext_status & OCC_EXT_STAT_MEM_THROTTLE) !=
  169. (occ->prev_ext_stat & OCC_EXT_STAT_MEM_THROTTLE)) {
  170. name = sensor_dev_attr_occ_mem_throttle.dev_attr.attr.name;
  171. sysfs_notify(&occ->bus_dev->kobj, NULL, name);
  172. }
  173. if ((header->ext_status & OCC_EXT_STAT_QUICK_DROP) !=
  174. (occ->prev_ext_stat & OCC_EXT_STAT_QUICK_DROP)) {
  175. name = sensor_dev_attr_occ_quick_pwr_drop.dev_attr.attr.name;
  176. sysfs_notify(&occ->bus_dev->kobj, NULL, name);
  177. }
  178. if ((header->ext_status & OCC_EXT_STAT_DVFS_VDD) !=
  179. (occ->prev_ext_stat & OCC_EXT_STAT_DVFS_VDD)) {
  180. name = sensor_dev_attr_occ_dvfs_vdd.dev_attr.attr.name;
  181. sysfs_notify(&occ->bus_dev->kobj, NULL, name);
  182. }
  183. if ((header->ext_status & OCC_EXT_STAT_GPU_THROTTLE) !=
  184. (occ->prev_ext_stat & OCC_EXT_STAT_GPU_THROTTLE)) {
  185. name = sensor_dev_attr_occ_gpu_throttle.dev_attr.attr.name;
  186. sysfs_notify(&occ->bus_dev->kobj, NULL, name);
  187. }
  188. if ((header->status & OCC_STAT_MASTER) &&
  189. header->occs_present != occ->prev_occs_present) {
  190. name = sensor_dev_attr_occs_present.dev_attr.attr.name;
  191. sysfs_notify(&occ->bus_dev->kobj, NULL, name);
  192. }
  193. if (header->ips_status != occ->prev_ips_status) {
  194. name = sensor_dev_attr_occ_ips_status.dev_attr.attr.name;
  195. sysfs_notify(&occ->bus_dev->kobj, NULL, name);
  196. }
  197. if (header->mode != occ->prev_mode) {
  198. name = sensor_dev_attr_occ_mode.dev_attr.attr.name;
  199. sysfs_notify(&occ->bus_dev->kobj, NULL, name);
  200. }
  201. if (occ->error && occ->error != occ->prev_error) {
  202. name = dev_attr_occ_error.attr.name;
  203. sysfs_notify(&occ->bus_dev->kobj, NULL, name);
  204. }
  205. /* no notifications for OCC state; doesn't indicate error condition */
  206. done:
  207. occ->prev_error = occ->error;
  208. occ->prev_stat = header->status;
  209. occ->prev_ext_stat = header->ext_status;
  210. occ->prev_occs_present = header->occs_present;
  211. occ->prev_ips_status = header->ips_status;
  212. occ->prev_mode = header->mode;
  213. }
  214. int occ_setup_sysfs(struct occ *occ)
  215. {
  216. return sysfs_create_group(&occ->bus_dev->kobj, &occ_sysfs);
  217. }
  218. void occ_shutdown_sysfs(struct occ *occ)
  219. {
  220. sysfs_remove_group(&occ->bus_dev->kobj, &occ_sysfs);
  221. }