gxp-fan-ctrl.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (C) 2022 Hewlett-Packard Enterprise Development Company, L.P. */
  3. #include <linux/bits.h>
  4. #include <linux/err.h>
  5. #include <linux/hwmon.h>
  6. #include <linux/io.h>
  7. #include <linux/module.h>
  8. #include <linux/mod_devicetable.h>
  9. #include <linux/platform_device.h>
  10. #define OFS_FAN_INST 0 /* Is 0 because plreg base will be set at INST */
  11. #define OFS_FAN_FAIL 2 /* Is 2 bytes after base */
  12. #define OFS_SEVSTAT 0 /* Is 0 because fn2 base will be set at SEVSTAT */
  13. #define POWER_BIT 24
  14. struct gxp_fan_ctrl_drvdata {
  15. void __iomem *base;
  16. void __iomem *plreg;
  17. void __iomem *fn2;
  18. };
  19. static bool fan_installed(struct device *dev, int fan)
  20. {
  21. struct gxp_fan_ctrl_drvdata *drvdata = dev_get_drvdata(dev);
  22. u8 val;
  23. val = readb(drvdata->plreg + OFS_FAN_INST);
  24. return !!(val & BIT(fan));
  25. }
  26. static long fan_failed(struct device *dev, int fan)
  27. {
  28. struct gxp_fan_ctrl_drvdata *drvdata = dev_get_drvdata(dev);
  29. u8 val;
  30. val = readb(drvdata->plreg + OFS_FAN_FAIL);
  31. return !!(val & BIT(fan));
  32. }
  33. static long fan_enabled(struct device *dev, int fan)
  34. {
  35. struct gxp_fan_ctrl_drvdata *drvdata = dev_get_drvdata(dev);
  36. u32 val;
  37. /*
  38. * Check the power status as if the platform is off the value
  39. * reported for the PWM will be incorrect. Report fan as
  40. * disabled.
  41. */
  42. val = readl(drvdata->fn2 + OFS_SEVSTAT);
  43. return !!((val & BIT(POWER_BIT)) && fan_installed(dev, fan));
  44. }
  45. static int gxp_pwm_write(struct device *dev, u32 attr, int channel, long val)
  46. {
  47. struct gxp_fan_ctrl_drvdata *drvdata = dev_get_drvdata(dev);
  48. switch (attr) {
  49. case hwmon_pwm_input:
  50. if (val > 255 || val < 0)
  51. return -EINVAL;
  52. writeb(val, drvdata->base + channel);
  53. return 0;
  54. default:
  55. return -EOPNOTSUPP;
  56. }
  57. }
  58. static int gxp_fan_ctrl_write(struct device *dev, enum hwmon_sensor_types type,
  59. u32 attr, int channel, long val)
  60. {
  61. switch (type) {
  62. case hwmon_pwm:
  63. return gxp_pwm_write(dev, attr, channel, val);
  64. default:
  65. return -EOPNOTSUPP;
  66. }
  67. }
  68. static int gxp_fan_read(struct device *dev, u32 attr, int channel, long *val)
  69. {
  70. switch (attr) {
  71. case hwmon_fan_enable:
  72. *val = fan_enabled(dev, channel);
  73. return 0;
  74. case hwmon_fan_fault:
  75. *val = fan_failed(dev, channel);
  76. return 0;
  77. default:
  78. return -EOPNOTSUPP;
  79. }
  80. }
  81. static int gxp_pwm_read(struct device *dev, u32 attr, int channel, long *val)
  82. {
  83. struct gxp_fan_ctrl_drvdata *drvdata = dev_get_drvdata(dev);
  84. u32 reg;
  85. /*
  86. * Check the power status of the platform. If the platform is off
  87. * the value reported for the PWM will be incorrect. In this case
  88. * report a PWM of zero.
  89. */
  90. reg = readl(drvdata->fn2 + OFS_SEVSTAT);
  91. if (reg & BIT(POWER_BIT))
  92. *val = fan_installed(dev, channel) ? readb(drvdata->base + channel) : 0;
  93. else
  94. *val = 0;
  95. return 0;
  96. }
  97. static int gxp_fan_ctrl_read(struct device *dev, enum hwmon_sensor_types type,
  98. u32 attr, int channel, long *val)
  99. {
  100. switch (type) {
  101. case hwmon_fan:
  102. return gxp_fan_read(dev, attr, channel, val);
  103. case hwmon_pwm:
  104. return gxp_pwm_read(dev, attr, channel, val);
  105. default:
  106. return -EOPNOTSUPP;
  107. }
  108. }
  109. static umode_t gxp_fan_ctrl_is_visible(const void *_data,
  110. enum hwmon_sensor_types type,
  111. u32 attr, int channel)
  112. {
  113. umode_t mode = 0;
  114. switch (type) {
  115. case hwmon_fan:
  116. switch (attr) {
  117. case hwmon_fan_enable:
  118. case hwmon_fan_fault:
  119. mode = 0444;
  120. break;
  121. default:
  122. break;
  123. }
  124. break;
  125. case hwmon_pwm:
  126. switch (attr) {
  127. case hwmon_pwm_input:
  128. mode = 0644;
  129. break;
  130. default:
  131. break;
  132. }
  133. break;
  134. default:
  135. break;
  136. }
  137. return mode;
  138. }
  139. static const struct hwmon_ops gxp_fan_ctrl_ops = {
  140. .is_visible = gxp_fan_ctrl_is_visible,
  141. .read = gxp_fan_ctrl_read,
  142. .write = gxp_fan_ctrl_write,
  143. };
  144. static const struct hwmon_channel_info * const gxp_fan_ctrl_info[] = {
  145. HWMON_CHANNEL_INFO(fan,
  146. HWMON_F_FAULT | HWMON_F_ENABLE,
  147. HWMON_F_FAULT | HWMON_F_ENABLE,
  148. HWMON_F_FAULT | HWMON_F_ENABLE,
  149. HWMON_F_FAULT | HWMON_F_ENABLE,
  150. HWMON_F_FAULT | HWMON_F_ENABLE,
  151. HWMON_F_FAULT | HWMON_F_ENABLE,
  152. HWMON_F_FAULT | HWMON_F_ENABLE,
  153. HWMON_F_FAULT | HWMON_F_ENABLE),
  154. HWMON_CHANNEL_INFO(pwm,
  155. HWMON_PWM_INPUT,
  156. HWMON_PWM_INPUT,
  157. HWMON_PWM_INPUT,
  158. HWMON_PWM_INPUT,
  159. HWMON_PWM_INPUT,
  160. HWMON_PWM_INPUT,
  161. HWMON_PWM_INPUT,
  162. HWMON_PWM_INPUT),
  163. NULL
  164. };
  165. static const struct hwmon_chip_info gxp_fan_ctrl_chip_info = {
  166. .ops = &gxp_fan_ctrl_ops,
  167. .info = gxp_fan_ctrl_info,
  168. };
  169. static int gxp_fan_ctrl_probe(struct platform_device *pdev)
  170. {
  171. struct gxp_fan_ctrl_drvdata *drvdata;
  172. struct device *dev = &pdev->dev;
  173. struct device *hwmon_dev;
  174. drvdata = devm_kzalloc(dev, sizeof(struct gxp_fan_ctrl_drvdata),
  175. GFP_KERNEL);
  176. if (!drvdata)
  177. return -ENOMEM;
  178. drvdata->base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
  179. if (IS_ERR(drvdata->base))
  180. return dev_err_probe(dev, PTR_ERR(drvdata->base),
  181. "failed to map base\n");
  182. drvdata->plreg = devm_platform_ioremap_resource_byname(pdev,
  183. "pl");
  184. if (IS_ERR(drvdata->plreg))
  185. return dev_err_probe(dev, PTR_ERR(drvdata->plreg),
  186. "failed to map plreg\n");
  187. drvdata->fn2 = devm_platform_ioremap_resource_byname(pdev,
  188. "fn2");
  189. if (IS_ERR(drvdata->fn2))
  190. return dev_err_probe(dev, PTR_ERR(drvdata->fn2),
  191. "failed to map fn2\n");
  192. hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev,
  193. "hpe_gxp_fan_ctrl",
  194. drvdata,
  195. &gxp_fan_ctrl_chip_info,
  196. NULL);
  197. return PTR_ERR_OR_ZERO(hwmon_dev);
  198. }
  199. static const struct of_device_id gxp_fan_ctrl_of_match[] = {
  200. { .compatible = "hpe,gxp-fan-ctrl", },
  201. {},
  202. };
  203. MODULE_DEVICE_TABLE(of, gxp_fan_ctrl_of_match);
  204. static struct platform_driver gxp_fan_ctrl_driver = {
  205. .probe = gxp_fan_ctrl_probe,
  206. .driver = {
  207. .name = "gxp-fan-ctrl",
  208. .of_match_table = gxp_fan_ctrl_of_match,
  209. },
  210. };
  211. module_platform_driver(gxp_fan_ctrl_driver);
  212. MODULE_AUTHOR("Nick Hawkins <nick.hawkins@hpe.com>");
  213. MODULE_DESCRIPTION("HPE GXP fan controller");
  214. MODULE_LICENSE("GPL");