1 From 4d49367c5303e3ebd17502a45b74de280f6be539 Mon Sep 17 00:00:00 2001
2 From: Christian Lamparter <chunkeey@gmail.com>
3 Date: Sun, 13 Feb 2022 01:47:33 +0100
4 Subject: hwmon: (tc654) Add thermal_cooling device support
6 Adds thermal_cooling device support to the tc654/tc655
7 driver. This make it possible to integrate it into a
8 device-tree supported thermal-zone node as a
11 I have been using this patch as part of the Netgear WNDR4700
12 Centria NAS Router support within OpenWrt since 2016.
14 Signed-off-by: Christian Lamparter <chunkeey@gmail.com>
15 Link: https://lore.kernel.org/r/20220213004733.2421193-1-chunkeey@gmail.com
16 Signed-off-by: Guenter Roeck <linux@roeck-us.net>
18 --- a/drivers/hwmon/tc654.c
19 +++ b/drivers/hwmon/tc654.c
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/slab.h>
24 +#include <linux/thermal.h>
25 #include <linux/util_macros.h>
28 @@ -379,28 +380,20 @@ static ssize_t pwm_show(struct device *d
29 return sprintf(buf, "%d\n", pwm);
32 -static ssize_t pwm_store(struct device *dev, struct device_attribute *da,
33 - const char *buf, size_t count)
34 +static int _set_pwm(struct tc654_data *data, unsigned long val)
36 - struct tc654_data *data = dev_get_drvdata(dev);
37 struct i2c_client *client = data->client;
41 - if (kstrtoul(buf, 10, &val))
46 mutex_lock(&data->update_lock);
50 data->config |= TC654_REG_CONFIG_SDM;
52 + data->duty_cycle = 0;
54 data->config &= ~TC654_REG_CONFIG_SDM;
56 - data->duty_cycle = find_closest(val, tc654_pwm_map,
57 - ARRAY_SIZE(tc654_pwm_map));
58 + data->duty_cycle = val - 1;
61 ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config);
63 @@ -411,6 +404,24 @@ static ssize_t pwm_store(struct device *
66 mutex_unlock(&data->update_lock);
70 +static ssize_t pwm_store(struct device *dev, struct device_attribute *da,
71 + const char *buf, size_t count)
73 + struct tc654_data *data = dev_get_drvdata(dev);
77 + if (kstrtoul(buf, 10, &val))
82 + val = find_closest(val, tc654_pwm_map, ARRAY_SIZE(tc654_pwm_map)) + 1;
84 + ret = _set_pwm(data, val);
85 return ret < 0 ? ret : count;
88 @@ -443,6 +454,58 @@ static struct attribute *tc654_attrs[] =
89 ATTRIBUTE_GROUPS(tc654);
92 + * thermal cooling device functions
94 + * Account for the "ShutDown Mode (SDM)" state by offsetting
95 + * the 16 PWM duty cycle states by 1.
97 + * State 0 = 0% PWM | Shutdown - Fan(s) are off
98 + * State 1 = 30% PWM | duty_cycle = 0
99 + * State 2 = ~35% PWM | duty_cycle = 1
101 + * State 15 = ~95% PWM | duty_cycle = 14
102 + * State 16 = 100% PWM | duty_cycle = 15
104 +#define TC654_MAX_COOLING_STATE 16
106 +static int tc654_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
108 + *state = TC654_MAX_COOLING_STATE;
112 +static int tc654_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
114 + struct tc654_data *data = tc654_update_client(cdev->devdata);
117 + return PTR_ERR(data);
119 + if (data->config & TC654_REG_CONFIG_SDM)
120 + *state = 0; /* FAN is off */
122 + *state = data->duty_cycle + 1; /* offset PWM States by 1 */
127 +static int tc654_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
129 + struct tc654_data *data = tc654_update_client(cdev->devdata);
132 + return PTR_ERR(data);
134 + return _set_pwm(data, clamp_val(state, 0, TC654_MAX_COOLING_STATE));
137 +static const struct thermal_cooling_device_ops tc654_fan_cool_ops = {
138 + .get_max_state = tc654_get_max_state,
139 + .get_cur_state = tc654_get_cur_state,
140 + .set_cur_state = tc654_set_cur_state,
144 * device probe and removal
147 @@ -472,7 +535,18 @@ static int tc654_probe(struct i2c_client
149 devm_hwmon_device_register_with_groups(dev, client->name, data,
151 - return PTR_ERR_OR_ZERO(hwmon_dev);
152 + if (IS_ERR(hwmon_dev))
153 + return PTR_ERR(hwmon_dev);
155 + if (IS_ENABLED(CONFIG_THERMAL)) {
156 + struct thermal_cooling_device *cdev;
158 + cdev = devm_thermal_of_cooling_device_register(dev, dev->of_node, client->name,
159 + hwmon_dev, &tc654_fan_cool_ops);
160 + return PTR_ERR_OR_ZERO(cdev);
166 static const struct i2c_device_id tc654_id[] = {