1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2016-2018 Mellanox Technologies. All rights reserved
3 * Copyright (c) 2016 Ivan Vecera <cera@cera.cz>
6 #include <linux/kernel.h>
7 #include <linux/types.h>
8 #include <linux/device.h>
9 #include <linux/sysfs.h>
10 #include <linux/thermal.h>
11 #include <linux/err.h>
15 #define MLXSW_THERMAL_POLL_INT 1000 /* ms */
16 #define MLXSW_THERMAL_MAX_TEMP 110000 /* 110C */
17 #define MLXSW_THERMAL_MAX_STATE 10
18 #define MLXSW_THERMAL_MAX_DUTY 255
20 struct mlxsw_thermal_trip {
27 static const struct mlxsw_thermal_trip default_thermal_trips[] = {
28 { /* In range - 0-40% PWM */
29 .type = THERMAL_TRIP_ACTIVE,
32 .max_state = (4 * MLXSW_THERMAL_MAX_STATE) / 10,
34 { /* High - 40-100% PWM */
35 .type = THERMAL_TRIP_ACTIVE,
37 .min_state = (4 * MLXSW_THERMAL_MAX_STATE) / 10,
38 .max_state = MLXSW_THERMAL_MAX_STATE,
41 /* Very high - 100% PWM */
42 .type = THERMAL_TRIP_ACTIVE,
44 .min_state = MLXSW_THERMAL_MAX_STATE,
45 .max_state = MLXSW_THERMAL_MAX_STATE,
48 .type = THERMAL_TRIP_HOT,
50 .min_state = MLXSW_THERMAL_MAX_STATE,
51 .max_state = MLXSW_THERMAL_MAX_STATE,
53 { /* Critical - soft poweroff */
54 .type = THERMAL_TRIP_CRITICAL,
55 .temp = MLXSW_THERMAL_MAX_TEMP,
56 .min_state = MLXSW_THERMAL_MAX_STATE,
57 .max_state = MLXSW_THERMAL_MAX_STATE,
61 #define MLXSW_THERMAL_NUM_TRIPS ARRAY_SIZE(default_thermal_trips)
63 /* Make sure all trips are writable */
64 #define MLXSW_THERMAL_TRIP_MASK (BIT(MLXSW_THERMAL_NUM_TRIPS) - 1)
66 struct mlxsw_thermal {
67 struct mlxsw_core *core;
68 const struct mlxsw_bus_info *bus_info;
69 struct thermal_zone_device *tzdev;
70 struct thermal_cooling_device *cdevs[MLXSW_MFCR_PWMS_MAX];
71 struct mlxsw_thermal_trip trips[MLXSW_THERMAL_NUM_TRIPS];
72 enum thermal_device_mode mode;
75 static inline u8 mlxsw_state_to_duty(int state)
77 return DIV_ROUND_CLOSEST(state * MLXSW_THERMAL_MAX_DUTY,
78 MLXSW_THERMAL_MAX_STATE);
81 static inline int mlxsw_duty_to_state(u8 duty)
83 return DIV_ROUND_CLOSEST(duty * MLXSW_THERMAL_MAX_STATE,
84 MLXSW_THERMAL_MAX_DUTY);
87 static int mlxsw_get_cooling_device_idx(struct mlxsw_thermal *thermal,
88 struct thermal_cooling_device *cdev)
92 for (i = 0; i < MLXSW_MFCR_PWMS_MAX; i++)
93 if (thermal->cdevs[i] == cdev)
99 static int mlxsw_thermal_bind(struct thermal_zone_device *tzdev,
100 struct thermal_cooling_device *cdev)
102 struct mlxsw_thermal *thermal = tzdev->devdata;
103 struct device *dev = thermal->bus_info->dev;
106 /* If the cooling device is one of ours bind it */
107 if (mlxsw_get_cooling_device_idx(thermal, cdev) < 0)
110 for (i = 0; i < MLXSW_THERMAL_NUM_TRIPS; i++) {
111 const struct mlxsw_thermal_trip *trip = &thermal->trips[i];
113 err = thermal_zone_bind_cooling_device(tzdev, i, cdev,
116 THERMAL_WEIGHT_DEFAULT);
118 dev_err(dev, "Failed to bind cooling device to trip %d\n", i);
125 static int mlxsw_thermal_unbind(struct thermal_zone_device *tzdev,
126 struct thermal_cooling_device *cdev)
128 struct mlxsw_thermal *thermal = tzdev->devdata;
129 struct device *dev = thermal->bus_info->dev;
133 /* If the cooling device is our one unbind it */
134 if (mlxsw_get_cooling_device_idx(thermal, cdev) < 0)
137 for (i = 0; i < MLXSW_THERMAL_NUM_TRIPS; i++) {
138 err = thermal_zone_unbind_cooling_device(tzdev, i, cdev);
140 dev_err(dev, "Failed to unbind cooling device\n");
147 static int mlxsw_thermal_get_mode(struct thermal_zone_device *tzdev,
148 enum thermal_device_mode *mode)
150 struct mlxsw_thermal *thermal = tzdev->devdata;
152 *mode = thermal->mode;
157 static int mlxsw_thermal_set_mode(struct thermal_zone_device *tzdev,
158 enum thermal_device_mode mode)
160 struct mlxsw_thermal *thermal = tzdev->devdata;
162 mutex_lock(&tzdev->lock);
164 if (mode == THERMAL_DEVICE_ENABLED)
165 tzdev->polling_delay = MLXSW_THERMAL_POLL_INT;
167 tzdev->polling_delay = 0;
169 mutex_unlock(&tzdev->lock);
171 thermal->mode = mode;
172 thermal_zone_device_update(tzdev, THERMAL_EVENT_UNSPECIFIED);
177 static int mlxsw_thermal_get_temp(struct thermal_zone_device *tzdev,
180 struct mlxsw_thermal *thermal = tzdev->devdata;
181 struct device *dev = thermal->bus_info->dev;
182 char mtmp_pl[MLXSW_REG_MTMP_LEN];
186 mlxsw_reg_mtmp_pack(mtmp_pl, 0, false, false);
188 err = mlxsw_reg_query(thermal->core, MLXSW_REG(mtmp), mtmp_pl);
190 dev_err(dev, "Failed to query temp sensor\n");
193 mlxsw_reg_mtmp_unpack(mtmp_pl, &temp, NULL, NULL);
195 *p_temp = (int) temp;
199 static int mlxsw_thermal_get_trip_type(struct thermal_zone_device *tzdev,
201 enum thermal_trip_type *p_type)
203 struct mlxsw_thermal *thermal = tzdev->devdata;
205 if (trip < 0 || trip >= MLXSW_THERMAL_NUM_TRIPS)
208 *p_type = thermal->trips[trip].type;
212 static int mlxsw_thermal_get_trip_temp(struct thermal_zone_device *tzdev,
213 int trip, int *p_temp)
215 struct mlxsw_thermal *thermal = tzdev->devdata;
217 if (trip < 0 || trip >= MLXSW_THERMAL_NUM_TRIPS)
220 *p_temp = thermal->trips[trip].temp;
224 static int mlxsw_thermal_set_trip_temp(struct thermal_zone_device *tzdev,
227 struct mlxsw_thermal *thermal = tzdev->devdata;
229 if (trip < 0 || trip >= MLXSW_THERMAL_NUM_TRIPS ||
230 temp > MLXSW_THERMAL_MAX_TEMP)
233 thermal->trips[trip].temp = temp;
237 static struct thermal_zone_device_ops mlxsw_thermal_ops = {
238 .bind = mlxsw_thermal_bind,
239 .unbind = mlxsw_thermal_unbind,
240 .get_mode = mlxsw_thermal_get_mode,
241 .set_mode = mlxsw_thermal_set_mode,
242 .get_temp = mlxsw_thermal_get_temp,
243 .get_trip_type = mlxsw_thermal_get_trip_type,
244 .get_trip_temp = mlxsw_thermal_get_trip_temp,
245 .set_trip_temp = mlxsw_thermal_set_trip_temp,
248 static int mlxsw_thermal_get_max_state(struct thermal_cooling_device *cdev,
249 unsigned long *p_state)
251 *p_state = MLXSW_THERMAL_MAX_STATE;
255 static int mlxsw_thermal_get_cur_state(struct thermal_cooling_device *cdev,
256 unsigned long *p_state)
259 struct mlxsw_thermal *thermal = cdev->devdata;
260 struct device *dev = thermal->bus_info->dev;
261 char mfsc_pl[MLXSW_REG_MFSC_LEN];
265 idx = mlxsw_get_cooling_device_idx(thermal, cdev);
269 mlxsw_reg_mfsc_pack(mfsc_pl, idx, 0);
270 err = mlxsw_reg_query(thermal->core, MLXSW_REG(mfsc), mfsc_pl);
272 dev_err(dev, "Failed to query PWM duty\n");
276 duty = mlxsw_reg_mfsc_pwm_duty_cycle_get(mfsc_pl);
277 *p_state = mlxsw_duty_to_state(duty);
281 static int mlxsw_thermal_set_cur_state(struct thermal_cooling_device *cdev,
285 struct mlxsw_thermal *thermal = cdev->devdata;
286 struct device *dev = thermal->bus_info->dev;
287 char mfsc_pl[MLXSW_REG_MFSC_LEN];
290 idx = mlxsw_get_cooling_device_idx(thermal, cdev);
294 mlxsw_reg_mfsc_pack(mfsc_pl, idx, mlxsw_state_to_duty(state));
295 err = mlxsw_reg_write(thermal->core, MLXSW_REG(mfsc), mfsc_pl);
297 dev_err(dev, "Failed to write PWM duty\n");
303 static const struct thermal_cooling_device_ops mlxsw_cooling_ops = {
304 .get_max_state = mlxsw_thermal_get_max_state,
305 .get_cur_state = mlxsw_thermal_get_cur_state,
306 .set_cur_state = mlxsw_thermal_set_cur_state,
309 int mlxsw_thermal_init(struct mlxsw_core *core,
310 const struct mlxsw_bus_info *bus_info,
311 struct mlxsw_thermal **p_thermal)
313 char mfcr_pl[MLXSW_REG_MFCR_LEN] = { 0 };
314 enum mlxsw_reg_mfcr_pwm_frequency freq;
315 struct device *dev = bus_info->dev;
316 struct mlxsw_thermal *thermal;
321 thermal = devm_kzalloc(dev, sizeof(*thermal),
326 thermal->core = core;
327 thermal->bus_info = bus_info;
328 memcpy(thermal->trips, default_thermal_trips, sizeof(thermal->trips));
330 err = mlxsw_reg_query(thermal->core, MLXSW_REG(mfcr), mfcr_pl);
332 dev_err(dev, "Failed to probe PWMs\n");
333 goto err_free_thermal;
335 mlxsw_reg_mfcr_unpack(mfcr_pl, &freq, &tacho_active, &pwm_active);
337 for (i = 0; i < MLXSW_MFCR_TACHOS_MAX; i++) {
338 if (tacho_active & BIT(i)) {
339 char mfsl_pl[MLXSW_REG_MFSL_LEN];
341 mlxsw_reg_mfsl_pack(mfsl_pl, i, 0, 0);
343 /* We need to query the register to preserve maximum */
344 err = mlxsw_reg_query(thermal->core, MLXSW_REG(mfsl),
347 goto err_free_thermal;
349 /* set the minimal RPMs to 0 */
350 mlxsw_reg_mfsl_tach_min_set(mfsl_pl, 0);
351 err = mlxsw_reg_write(thermal->core, MLXSW_REG(mfsl),
354 goto err_free_thermal;
357 for (i = 0; i < MLXSW_MFCR_PWMS_MAX; i++) {
358 if (pwm_active & BIT(i)) {
359 struct thermal_cooling_device *cdev;
361 cdev = thermal_cooling_device_register("Fan", thermal,
365 dev_err(dev, "Failed to register cooling device\n");
366 goto err_unreg_cdevs;
368 thermal->cdevs[i] = cdev;
372 thermal->tzdev = thermal_zone_device_register("mlxsw",
373 MLXSW_THERMAL_NUM_TRIPS,
374 MLXSW_THERMAL_TRIP_MASK,
378 MLXSW_THERMAL_POLL_INT);
379 if (IS_ERR(thermal->tzdev)) {
380 err = PTR_ERR(thermal->tzdev);
381 dev_err(dev, "Failed to register thermal zone\n");
382 goto err_unreg_cdevs;
385 thermal->mode = THERMAL_DEVICE_ENABLED;
386 *p_thermal = thermal;
389 for (i = 0; i < MLXSW_MFCR_PWMS_MAX; i++)
390 if (thermal->cdevs[i])
391 thermal_cooling_device_unregister(thermal->cdevs[i]);
393 devm_kfree(dev, thermal);
397 void mlxsw_thermal_fini(struct mlxsw_thermal *thermal)
401 if (thermal->tzdev) {
402 thermal_zone_device_unregister(thermal->tzdev);
403 thermal->tzdev = NULL;
406 for (i = 0; i < MLXSW_MFCR_PWMS_MAX; i++) {
407 if (thermal->cdevs[i]) {
408 thermal_cooling_device_unregister(thermal->cdevs[i]);
409 thermal->cdevs[i] = NULL;
413 devm_kfree(thermal->bus_info->dev, thermal);