804-hwmon-tc654-add-thermal_cooling-device.patch 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. From 4d49367c5303e3ebd17502a45b74de280f6be539 Mon Sep 17 00:00:00 2001
  2. From: Christian Lamparter <[email protected]>
  3. Date: Sun, 13 Feb 2022 01:47:33 +0100
  4. Subject: hwmon: (tc654) Add thermal_cooling device support
  5. Adds thermal_cooling device support to the tc654/tc655
  6. driver. This make it possible to integrate it into a
  7. device-tree supported thermal-zone node as a
  8. cooling device.
  9. I have been using this patch as part of the Netgear WNDR4700
  10. Centria NAS Router support within OpenWrt since 2016.
  11. Signed-off-by: Christian Lamparter <[email protected]>
  12. Link: https://lore.kernel.org/r/[email protected]
  13. Signed-off-by: Guenter Roeck <[email protected]>
  14. ---
  15. --- a/drivers/hwmon/tc654.c
  16. +++ b/drivers/hwmon/tc654.c
  17. @@ -15,6 +15,7 @@
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/slab.h>
  21. +#include <linux/thermal.h>
  22. #include <linux/util_macros.h>
  23. enum tc654_regs {
  24. @@ -384,28 +385,20 @@ static ssize_t pwm_show(struct device *d
  25. return sprintf(buf, "%d\n", pwm);
  26. }
  27. -static ssize_t pwm_store(struct device *dev, struct device_attribute *da,
  28. - const char *buf, size_t count)
  29. +static int _set_pwm(struct tc654_data *data, unsigned long val)
  30. {
  31. - struct tc654_data *data = dev_get_drvdata(dev);
  32. struct i2c_client *client = data->client;
  33. - unsigned long val;
  34. int ret;
  35. - if (kstrtoul(buf, 10, &val))
  36. - return -EINVAL;
  37. - if (val > 255)
  38. - return -EINVAL;
  39. -
  40. mutex_lock(&data->update_lock);
  41. - if (val == 0)
  42. + if (val == 0) {
  43. data->config |= TC654_REG_CONFIG_SDM;
  44. - else
  45. + data->duty_cycle = 0;
  46. + } else {
  47. data->config &= ~TC654_REG_CONFIG_SDM;
  48. -
  49. - data->duty_cycle = find_closest(val, tc654_pwm_map,
  50. - ARRAY_SIZE(tc654_pwm_map));
  51. + data->duty_cycle = val - 1;
  52. + }
  53. ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config);
  54. if (ret < 0)
  55. @@ -416,6 +409,24 @@ static ssize_t pwm_store(struct device *
  56. out:
  57. mutex_unlock(&data->update_lock);
  58. + return ret;
  59. +}
  60. +
  61. +static ssize_t pwm_store(struct device *dev, struct device_attribute *da,
  62. + const char *buf, size_t count)
  63. +{
  64. + struct tc654_data *data = dev_get_drvdata(dev);
  65. + unsigned long val;
  66. + int ret;
  67. +
  68. + if (kstrtoul(buf, 10, &val))
  69. + return -EINVAL;
  70. + if (val > 255)
  71. + return -EINVAL;
  72. + if (val > 0)
  73. + val = find_closest(val, tc654_pwm_map, ARRAY_SIZE(tc654_pwm_map)) + 1;
  74. +
  75. + ret = _set_pwm(data, val);
  76. return ret < 0 ? ret : count;
  77. }
  78. @@ -448,6 +459,58 @@ static struct attribute *tc654_attrs[] =
  79. ATTRIBUTE_GROUPS(tc654);
  80. /*
  81. + * thermal cooling device functions
  82. + *
  83. + * Account for the "ShutDown Mode (SDM)" state by offsetting
  84. + * the 16 PWM duty cycle states by 1.
  85. + *
  86. + * State 0 = 0% PWM | Shutdown - Fan(s) are off
  87. + * State 1 = 30% PWM | duty_cycle = 0
  88. + * State 2 = ~35% PWM | duty_cycle = 1
  89. + * [...]
  90. + * State 15 = ~95% PWM | duty_cycle = 14
  91. + * State 16 = 100% PWM | duty_cycle = 15
  92. + */
  93. +#define TC654_MAX_COOLING_STATE 16
  94. +
  95. +static int tc654_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
  96. +{
  97. + *state = TC654_MAX_COOLING_STATE;
  98. + return 0;
  99. +}
  100. +
  101. +static int tc654_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
  102. +{
  103. + struct tc654_data *data = tc654_update_client(cdev->devdata);
  104. +
  105. + if (IS_ERR(data))
  106. + return PTR_ERR(data);
  107. +
  108. + if (data->config & TC654_REG_CONFIG_SDM)
  109. + *state = 0; /* FAN is off */
  110. + else
  111. + *state = data->duty_cycle + 1; /* offset PWM States by 1 */
  112. +
  113. + return 0;
  114. +}
  115. +
  116. +static int tc654_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
  117. +{
  118. + struct tc654_data *data = tc654_update_client(cdev->devdata);
  119. +
  120. + if (IS_ERR(data))
  121. + return PTR_ERR(data);
  122. +
  123. + return _set_pwm(data, clamp_val(state, 0, TC654_MAX_COOLING_STATE));
  124. +}
  125. +
  126. +static const struct thermal_cooling_device_ops tc654_fan_cool_ops = {
  127. + .get_max_state = tc654_get_max_state,
  128. + .get_cur_state = tc654_get_cur_state,
  129. + .set_cur_state = tc654_set_cur_state,
  130. +};
  131. +
  132. +/*
  133. * device probe and removal
  134. */
  135. @@ -477,7 +540,18 @@ static int tc654_probe(struct i2c_client
  136. hwmon_dev =
  137. devm_hwmon_device_register_with_groups(dev, client->name, data,
  138. tc654_groups);
  139. - return PTR_ERR_OR_ZERO(hwmon_dev);
  140. + if (IS_ERR(hwmon_dev))
  141. + return PTR_ERR(hwmon_dev);
  142. +
  143. + if (IS_ENABLED(CONFIG_THERMAL)) {
  144. + struct thermal_cooling_device *cdev;
  145. +
  146. + cdev = devm_thermal_of_cooling_device_register(dev, dev->of_node, client->name,
  147. + hwmon_dev, &tc654_fan_cool_ops);
  148. + return PTR_ERR_OR_ZERO(cdev);
  149. + }
  150. +
  151. + return 0;
  152. }
  153. static const struct i2c_device_id tc654_id[] = {