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>
12 #include <linux/sfp.h>
17 #define MLXSW_THERMAL_POLL_INT 1000 /* ms */
18 #define MLXSW_THERMAL_SLOW_POLL_INT 20000 /* ms */
19 #define MLXSW_THERMAL_ASIC_TEMP_NORM 75000 /* 75C */
20 #define MLXSW_THERMAL_ASIC_TEMP_HIGH 85000 /* 85C */
21 #define MLXSW_THERMAL_ASIC_TEMP_HOT 105000 /* 105C */
22 #define MLXSW_THERMAL_ASIC_TEMP_CRIT 110000 /* 110C */
23 #define MLXSW_THERMAL_HYSTERESIS_TEMP 5000 /* 5C */
24 #define MLXSW_THERMAL_MODULE_TEMP_SHIFT (MLXSW_THERMAL_HYSTERESIS_TEMP * 2)
25 #define MLXSW_THERMAL_ZONE_MAX_NAME 16
26 #define MLXSW_THERMAL_TEMP_SCORE_MAX GENMASK(31, 0)
27 #define MLXSW_THERMAL_MAX_STATE 10
28 #define MLXSW_THERMAL_MAX_DUTY 255
29 /* Minimum and maximum fan allowed speed in percent: from 20% to 100%. Values
30 * MLXSW_THERMAL_MAX_STATE + x, where x is between 2 and 10 are used for
31 * setting fan speed dynamic minimum. For example, if value is set to 14 (40%)
32 * cooling levels vector will be set to 4, 4, 4, 4, 4, 5, 6, 7, 8, 9, 10 to
33 * introduce PWM speed in percent: 40, 40, 40, 40, 40, 50, 60. 70, 80, 90, 100.
35 #define MLXSW_THERMAL_SPEED_MIN (MLXSW_THERMAL_MAX_STATE + 2)
36 #define MLXSW_THERMAL_SPEED_MAX (MLXSW_THERMAL_MAX_STATE * 2)
37 #define MLXSW_THERMAL_SPEED_MIN_LEVEL 2 /* 20% */
39 /* External cooling devices, allowed for binding to mlxsw thermal zones. */
40 static char * const mlxsw_thermal_external_allowed_cdev[] = {
44 enum mlxsw_thermal_trips {
45 MLXSW_THERMAL_TEMP_TRIP_NORM,
46 MLXSW_THERMAL_TEMP_TRIP_HIGH,
47 MLXSW_THERMAL_TEMP_TRIP_HOT,
48 MLXSW_THERMAL_TEMP_TRIP_CRIT,
51 struct mlxsw_thermal_trip {
59 static const struct mlxsw_thermal_trip default_thermal_trips[] = {
60 { /* In range - 0-40% PWM */
61 .type = THERMAL_TRIP_ACTIVE,
62 .temp = MLXSW_THERMAL_ASIC_TEMP_NORM,
63 .hyst = MLXSW_THERMAL_HYSTERESIS_TEMP,
65 .max_state = (4 * MLXSW_THERMAL_MAX_STATE) / 10,
68 /* In range - 40-100% PWM */
69 .type = THERMAL_TRIP_ACTIVE,
70 .temp = MLXSW_THERMAL_ASIC_TEMP_HIGH,
71 .hyst = MLXSW_THERMAL_HYSTERESIS_TEMP,
72 .min_state = (4 * MLXSW_THERMAL_MAX_STATE) / 10,
73 .max_state = MLXSW_THERMAL_MAX_STATE,
76 .type = THERMAL_TRIP_HOT,
77 .temp = MLXSW_THERMAL_ASIC_TEMP_HOT,
78 .hyst = MLXSW_THERMAL_HYSTERESIS_TEMP,
79 .min_state = MLXSW_THERMAL_MAX_STATE,
80 .max_state = MLXSW_THERMAL_MAX_STATE,
82 { /* Critical - soft poweroff */
83 .type = THERMAL_TRIP_CRITICAL,
84 .temp = MLXSW_THERMAL_ASIC_TEMP_CRIT,
85 .min_state = MLXSW_THERMAL_MAX_STATE,
86 .max_state = MLXSW_THERMAL_MAX_STATE,
90 #define MLXSW_THERMAL_NUM_TRIPS ARRAY_SIZE(default_thermal_trips)
92 /* Make sure all trips are writable */
93 #define MLXSW_THERMAL_TRIP_MASK (BIT(MLXSW_THERMAL_NUM_TRIPS) - 1)
97 struct mlxsw_thermal_module {
98 struct mlxsw_thermal *parent;
99 struct thermal_zone_device *tzdev;
100 struct mlxsw_thermal_trip trips[MLXSW_THERMAL_NUM_TRIPS];
101 int module; /* Module or gearbox number */
104 struct mlxsw_thermal {
105 struct mlxsw_core *core;
106 const struct mlxsw_bus_info *bus_info;
107 struct thermal_zone_device *tzdev;
109 struct thermal_cooling_device *cdevs[MLXSW_MFCR_PWMS_MAX];
110 u8 cooling_levels[MLXSW_THERMAL_MAX_STATE + 1];
111 struct mlxsw_thermal_trip trips[MLXSW_THERMAL_NUM_TRIPS];
112 struct mlxsw_thermal_module *tz_module_arr;
114 struct mlxsw_thermal_module *tz_gearbox_arr;
116 unsigned int tz_highest_score;
117 struct thermal_zone_device *tz_highest_dev;
120 static inline u8 mlxsw_state_to_duty(int state)
122 return DIV_ROUND_CLOSEST(state * MLXSW_THERMAL_MAX_DUTY,
123 MLXSW_THERMAL_MAX_STATE);
126 static inline int mlxsw_duty_to_state(u8 duty)
128 return DIV_ROUND_CLOSEST(duty * MLXSW_THERMAL_MAX_STATE,
129 MLXSW_THERMAL_MAX_DUTY);
132 static int mlxsw_get_cooling_device_idx(struct mlxsw_thermal *thermal,
133 struct thermal_cooling_device *cdev)
137 for (i = 0; i < MLXSW_MFCR_PWMS_MAX; i++)
138 if (thermal->cdevs[i] == cdev)
141 /* Allow mlxsw thermal zone binding to an external cooling device */
142 for (i = 0; i < ARRAY_SIZE(mlxsw_thermal_external_allowed_cdev); i++) {
143 if (strnstr(cdev->type, mlxsw_thermal_external_allowed_cdev[i],
152 mlxsw_thermal_module_trips_reset(struct mlxsw_thermal_module *tz)
154 tz->trips[MLXSW_THERMAL_TEMP_TRIP_NORM].temp = 0;
155 tz->trips[MLXSW_THERMAL_TEMP_TRIP_HIGH].temp = 0;
156 tz->trips[MLXSW_THERMAL_TEMP_TRIP_HOT].temp = 0;
157 tz->trips[MLXSW_THERMAL_TEMP_TRIP_CRIT].temp = 0;
161 mlxsw_thermal_module_trips_update(struct device *dev, struct mlxsw_core *core,
162 struct mlxsw_thermal_module *tz)
164 int crit_temp, emerg_temp;
167 err = mlxsw_env_module_temp_thresholds_get(core, tz->module,
173 err = mlxsw_env_module_temp_thresholds_get(core, tz->module,
179 /* According to the system thermal requirements, the thermal zones are
180 * defined with four trip points. The critical and emergency
181 * temperature thresholds, provided by QSFP module are set as "active"
182 * and "hot" trip points, "normal" and "critical" trip points are
183 * derived from "active" and "hot" by subtracting or adding double
186 if (crit_temp >= MLXSW_THERMAL_MODULE_TEMP_SHIFT)
187 tz->trips[MLXSW_THERMAL_TEMP_TRIP_NORM].temp = crit_temp -
188 MLXSW_THERMAL_MODULE_TEMP_SHIFT;
190 tz->trips[MLXSW_THERMAL_TEMP_TRIP_NORM].temp = crit_temp;
191 tz->trips[MLXSW_THERMAL_TEMP_TRIP_HIGH].temp = crit_temp;
192 tz->trips[MLXSW_THERMAL_TEMP_TRIP_HOT].temp = emerg_temp;
193 if (emerg_temp > crit_temp)
194 tz->trips[MLXSW_THERMAL_TEMP_TRIP_CRIT].temp = emerg_temp +
195 MLXSW_THERMAL_MODULE_TEMP_SHIFT;
197 tz->trips[MLXSW_THERMAL_TEMP_TRIP_CRIT].temp = emerg_temp;
202 static void mlxsw_thermal_tz_score_update(struct mlxsw_thermal *thermal,
203 struct thermal_zone_device *tzdev,
204 struct mlxsw_thermal_trip *trips,
207 struct mlxsw_thermal_trip *trip = trips;
208 unsigned int score, delta, i, shift = 1;
210 /* Calculate thermal zone score, if temperature is above the critical
211 * threshold score is set to MLXSW_THERMAL_TEMP_SCORE_MAX.
213 score = MLXSW_THERMAL_TEMP_SCORE_MAX;
214 for (i = MLXSW_THERMAL_TEMP_TRIP_NORM; i < MLXSW_THERMAL_NUM_TRIPS;
216 if (temp < trip->temp) {
217 delta = DIV_ROUND_CLOSEST(temp, trip->temp - temp);
218 score = delta * shift;
224 if (score > thermal->tz_highest_score) {
225 thermal->tz_highest_score = score;
226 thermal->tz_highest_dev = tzdev;
230 static int mlxsw_thermal_bind(struct thermal_zone_device *tzdev,
231 struct thermal_cooling_device *cdev)
233 struct mlxsw_thermal *thermal = tzdev->devdata;
234 struct device *dev = thermal->bus_info->dev;
237 /* If the cooling device is one of ours bind it */
238 if (mlxsw_get_cooling_device_idx(thermal, cdev) < 0)
241 for (i = 0; i < MLXSW_THERMAL_NUM_TRIPS; i++) {
242 const struct mlxsw_thermal_trip *trip = &thermal->trips[i];
244 err = thermal_zone_bind_cooling_device(tzdev, i, cdev,
247 THERMAL_WEIGHT_DEFAULT);
249 dev_err(dev, "Failed to bind cooling device to trip %d\n", i);
256 static int mlxsw_thermal_unbind(struct thermal_zone_device *tzdev,
257 struct thermal_cooling_device *cdev)
259 struct mlxsw_thermal *thermal = tzdev->devdata;
260 struct device *dev = thermal->bus_info->dev;
264 /* If the cooling device is our one unbind it */
265 if (mlxsw_get_cooling_device_idx(thermal, cdev) < 0)
268 for (i = 0; i < MLXSW_THERMAL_NUM_TRIPS; i++) {
269 err = thermal_zone_unbind_cooling_device(tzdev, i, cdev);
271 dev_err(dev, "Failed to unbind cooling device\n");
278 static int mlxsw_thermal_get_temp(struct thermal_zone_device *tzdev,
281 struct mlxsw_thermal *thermal = tzdev->devdata;
282 struct device *dev = thermal->bus_info->dev;
283 char mtmp_pl[MLXSW_REG_MTMP_LEN];
287 mlxsw_reg_mtmp_pack(mtmp_pl, 0, false, false);
289 err = mlxsw_reg_query(thermal->core, MLXSW_REG(mtmp), mtmp_pl);
291 dev_err(dev, "Failed to query temp sensor\n");
294 mlxsw_reg_mtmp_unpack(mtmp_pl, &temp, NULL, NULL);
296 mlxsw_thermal_tz_score_update(thermal, tzdev, thermal->trips,
303 static int mlxsw_thermal_get_trip_type(struct thermal_zone_device *tzdev,
305 enum thermal_trip_type *p_type)
307 struct mlxsw_thermal *thermal = tzdev->devdata;
309 if (trip < 0 || trip >= MLXSW_THERMAL_NUM_TRIPS)
312 *p_type = thermal->trips[trip].type;
316 static int mlxsw_thermal_get_trip_temp(struct thermal_zone_device *tzdev,
317 int trip, int *p_temp)
319 struct mlxsw_thermal *thermal = tzdev->devdata;
321 if (trip < 0 || trip >= MLXSW_THERMAL_NUM_TRIPS)
324 *p_temp = thermal->trips[trip].temp;
328 static int mlxsw_thermal_set_trip_temp(struct thermal_zone_device *tzdev,
331 struct mlxsw_thermal *thermal = tzdev->devdata;
333 if (trip < 0 || trip >= MLXSW_THERMAL_NUM_TRIPS ||
334 temp > MLXSW_THERMAL_ASIC_TEMP_CRIT)
337 thermal->trips[trip].temp = temp;
341 static int mlxsw_thermal_get_trip_hyst(struct thermal_zone_device *tzdev,
342 int trip, int *p_hyst)
344 struct mlxsw_thermal *thermal = tzdev->devdata;
346 *p_hyst = thermal->trips[trip].hyst;
350 static int mlxsw_thermal_set_trip_hyst(struct thermal_zone_device *tzdev,
353 struct mlxsw_thermal *thermal = tzdev->devdata;
355 thermal->trips[trip].hyst = hyst;
359 static int mlxsw_thermal_trend_get(struct thermal_zone_device *tzdev,
360 int trip, enum thermal_trend *trend)
362 struct mlxsw_thermal *thermal = tzdev->devdata;
364 if (trip < 0 || trip >= MLXSW_THERMAL_NUM_TRIPS)
367 if (tzdev == thermal->tz_highest_dev)
370 *trend = THERMAL_TREND_STABLE;
374 static struct thermal_zone_device_ops mlxsw_thermal_ops = {
375 .bind = mlxsw_thermal_bind,
376 .unbind = mlxsw_thermal_unbind,
377 .get_temp = mlxsw_thermal_get_temp,
378 .get_trip_type = mlxsw_thermal_get_trip_type,
379 .get_trip_temp = mlxsw_thermal_get_trip_temp,
380 .set_trip_temp = mlxsw_thermal_set_trip_temp,
381 .get_trip_hyst = mlxsw_thermal_get_trip_hyst,
382 .set_trip_hyst = mlxsw_thermal_set_trip_hyst,
383 .get_trend = mlxsw_thermal_trend_get,
386 static int mlxsw_thermal_module_bind(struct thermal_zone_device *tzdev,
387 struct thermal_cooling_device *cdev)
389 struct mlxsw_thermal_module *tz = tzdev->devdata;
390 struct mlxsw_thermal *thermal = tz->parent;
393 /* If the cooling device is one of ours bind it */
394 if (mlxsw_get_cooling_device_idx(thermal, cdev) < 0)
397 for (i = 0; i < MLXSW_THERMAL_NUM_TRIPS; i++) {
398 const struct mlxsw_thermal_trip *trip = &tz->trips[i];
400 err = thermal_zone_bind_cooling_device(tzdev, i, cdev,
403 THERMAL_WEIGHT_DEFAULT);
405 goto err_bind_cooling_device;
409 err_bind_cooling_device:
410 for (j = i - 1; j >= 0; j--)
411 thermal_zone_unbind_cooling_device(tzdev, j, cdev);
415 static int mlxsw_thermal_module_unbind(struct thermal_zone_device *tzdev,
416 struct thermal_cooling_device *cdev)
418 struct mlxsw_thermal_module *tz = tzdev->devdata;
419 struct mlxsw_thermal *thermal = tz->parent;
423 /* If the cooling device is one of ours unbind it */
424 if (mlxsw_get_cooling_device_idx(thermal, cdev) < 0)
427 for (i = 0; i < MLXSW_THERMAL_NUM_TRIPS; i++) {
428 err = thermal_zone_unbind_cooling_device(tzdev, i, cdev);
434 static int mlxsw_thermal_module_temp_get(struct thermal_zone_device *tzdev,
437 struct mlxsw_thermal_module *tz = tzdev->devdata;
438 struct mlxsw_thermal *thermal = tz->parent;
439 struct device *dev = thermal->bus_info->dev;
440 char mtmp_pl[MLXSW_REG_MTMP_LEN];
444 /* Read module temperature. */
445 mlxsw_reg_mtmp_pack(mtmp_pl, MLXSW_REG_MTMP_MODULE_INDEX_MIN +
446 tz->module, false, false);
447 err = mlxsw_reg_query(thermal->core, MLXSW_REG(mtmp), mtmp_pl);
449 /* Do not return error - in case of broken module's sensor
450 * it will cause error message flooding.
453 *p_temp = (int) temp;
456 mlxsw_reg_mtmp_unpack(mtmp_pl, &temp, NULL, NULL);
462 /* Update trip points. */
463 err = mlxsw_thermal_module_trips_update(dev, thermal->core, tz);
464 if (!err && temp > 0)
465 mlxsw_thermal_tz_score_update(thermal, tzdev, tz->trips, temp);
471 mlxsw_thermal_module_trip_type_get(struct thermal_zone_device *tzdev, int trip,
472 enum thermal_trip_type *p_type)
474 struct mlxsw_thermal_module *tz = tzdev->devdata;
476 if (trip < 0 || trip >= MLXSW_THERMAL_NUM_TRIPS)
479 *p_type = tz->trips[trip].type;
484 mlxsw_thermal_module_trip_temp_get(struct thermal_zone_device *tzdev,
485 int trip, int *p_temp)
487 struct mlxsw_thermal_module *tz = tzdev->devdata;
489 if (trip < 0 || trip >= MLXSW_THERMAL_NUM_TRIPS)
492 *p_temp = tz->trips[trip].temp;
497 mlxsw_thermal_module_trip_temp_set(struct thermal_zone_device *tzdev,
500 struct mlxsw_thermal_module *tz = tzdev->devdata;
502 if (trip < 0 || trip >= MLXSW_THERMAL_NUM_TRIPS ||
503 temp > tz->trips[MLXSW_THERMAL_TEMP_TRIP_CRIT].temp)
506 tz->trips[trip].temp = temp;
511 mlxsw_thermal_module_trip_hyst_get(struct thermal_zone_device *tzdev, int trip,
514 struct mlxsw_thermal_module *tz = tzdev->devdata;
516 *p_hyst = tz->trips[trip].hyst;
521 mlxsw_thermal_module_trip_hyst_set(struct thermal_zone_device *tzdev, int trip,
524 struct mlxsw_thermal_module *tz = tzdev->devdata;
526 tz->trips[trip].hyst = hyst;
530 static int mlxsw_thermal_module_trend_get(struct thermal_zone_device *tzdev,
531 int trip, enum thermal_trend *trend)
533 struct mlxsw_thermal_module *tz = tzdev->devdata;
534 struct mlxsw_thermal *thermal = tz->parent;
536 if (trip < 0 || trip >= MLXSW_THERMAL_NUM_TRIPS)
539 if (tzdev == thermal->tz_highest_dev)
542 *trend = THERMAL_TREND_STABLE;
546 static struct thermal_zone_device_ops mlxsw_thermal_module_ops = {
547 .bind = mlxsw_thermal_module_bind,
548 .unbind = mlxsw_thermal_module_unbind,
549 .get_temp = mlxsw_thermal_module_temp_get,
550 .get_trip_type = mlxsw_thermal_module_trip_type_get,
551 .get_trip_temp = mlxsw_thermal_module_trip_temp_get,
552 .set_trip_temp = mlxsw_thermal_module_trip_temp_set,
553 .get_trip_hyst = mlxsw_thermal_module_trip_hyst_get,
554 .set_trip_hyst = mlxsw_thermal_module_trip_hyst_set,
555 .get_trend = mlxsw_thermal_module_trend_get,
558 static int mlxsw_thermal_gearbox_temp_get(struct thermal_zone_device *tzdev,
561 struct mlxsw_thermal_module *tz = tzdev->devdata;
562 struct mlxsw_thermal *thermal = tz->parent;
563 char mtmp_pl[MLXSW_REG_MTMP_LEN];
568 index = MLXSW_REG_MTMP_GBOX_INDEX_MIN + tz->module;
569 mlxsw_reg_mtmp_pack(mtmp_pl, index, false, false);
571 err = mlxsw_reg_query(thermal->core, MLXSW_REG(mtmp), mtmp_pl);
575 mlxsw_reg_mtmp_unpack(mtmp_pl, &temp, NULL, NULL);
577 mlxsw_thermal_tz_score_update(thermal, tzdev, tz->trips, temp);
583 static struct thermal_zone_device_ops mlxsw_thermal_gearbox_ops = {
584 .bind = mlxsw_thermal_module_bind,
585 .unbind = mlxsw_thermal_module_unbind,
586 .get_temp = mlxsw_thermal_gearbox_temp_get,
587 .get_trip_type = mlxsw_thermal_module_trip_type_get,
588 .get_trip_temp = mlxsw_thermal_module_trip_temp_get,
589 .set_trip_temp = mlxsw_thermal_module_trip_temp_set,
590 .get_trip_hyst = mlxsw_thermal_module_trip_hyst_get,
591 .set_trip_hyst = mlxsw_thermal_module_trip_hyst_set,
592 .get_trend = mlxsw_thermal_module_trend_get,
595 static int mlxsw_thermal_get_max_state(struct thermal_cooling_device *cdev,
596 unsigned long *p_state)
598 *p_state = MLXSW_THERMAL_MAX_STATE;
602 static int mlxsw_thermal_get_cur_state(struct thermal_cooling_device *cdev,
603 unsigned long *p_state)
606 struct mlxsw_thermal *thermal = cdev->devdata;
607 struct device *dev = thermal->bus_info->dev;
608 char mfsc_pl[MLXSW_REG_MFSC_LEN];
612 idx = mlxsw_get_cooling_device_idx(thermal, cdev);
616 mlxsw_reg_mfsc_pack(mfsc_pl, idx, 0);
617 err = mlxsw_reg_query(thermal->core, MLXSW_REG(mfsc), mfsc_pl);
619 dev_err(dev, "Failed to query PWM duty\n");
623 duty = mlxsw_reg_mfsc_pwm_duty_cycle_get(mfsc_pl);
624 *p_state = mlxsw_duty_to_state(duty);
628 static int mlxsw_thermal_set_cur_state(struct thermal_cooling_device *cdev,
632 struct mlxsw_thermal *thermal = cdev->devdata;
633 struct device *dev = thermal->bus_info->dev;
634 char mfsc_pl[MLXSW_REG_MFSC_LEN];
635 unsigned long cur_state, i;
640 idx = mlxsw_get_cooling_device_idx(thermal, cdev);
644 /* Verify if this request is for changing allowed fan dynamical
645 * minimum. If it is - update cooling levels accordingly and update
646 * state, if current state is below the newly requested minimum state.
647 * For example, if current state is 5, and minimal state is to be
648 * changed from 4 to 6, thermal->cooling_levels[0 to 5] will be changed
649 * all from 4 to 6. And state 5 (thermal->cooling_levels[4]) should be
652 if (state >= MLXSW_THERMAL_SPEED_MIN &&
653 state <= MLXSW_THERMAL_SPEED_MAX) {
654 state -= MLXSW_THERMAL_MAX_STATE;
655 for (i = 0; i <= MLXSW_THERMAL_MAX_STATE; i++)
656 thermal->cooling_levels[i] = max(state, i);
658 mlxsw_reg_mfsc_pack(mfsc_pl, idx, 0);
659 err = mlxsw_reg_query(thermal->core, MLXSW_REG(mfsc), mfsc_pl);
663 duty = mlxsw_reg_mfsc_pwm_duty_cycle_get(mfsc_pl);
664 cur_state = mlxsw_duty_to_state(duty);
666 /* If current fan state is lower than requested dynamical
667 * minimum, increase fan speed up to dynamical minimum.
669 if (state < cur_state)
675 if (state > MLXSW_THERMAL_MAX_STATE)
678 /* Normalize the state to the valid speed range. */
679 state = thermal->cooling_levels[state];
680 mlxsw_reg_mfsc_pack(mfsc_pl, idx, mlxsw_state_to_duty(state));
681 err = mlxsw_reg_write(thermal->core, MLXSW_REG(mfsc), mfsc_pl);
683 dev_err(dev, "Failed to write PWM duty\n");
689 static const struct thermal_cooling_device_ops mlxsw_cooling_ops = {
690 .get_max_state = mlxsw_thermal_get_max_state,
691 .get_cur_state = mlxsw_thermal_get_cur_state,
692 .set_cur_state = mlxsw_thermal_set_cur_state,
696 mlxsw_thermal_module_tz_init(struct mlxsw_thermal_module *module_tz)
698 char tz_name[MLXSW_THERMAL_ZONE_MAX_NAME];
701 snprintf(tz_name, sizeof(tz_name), "mlxsw-module%d",
702 module_tz->module + 1);
703 module_tz->tzdev = thermal_zone_device_register(tz_name,
704 MLXSW_THERMAL_NUM_TRIPS,
705 MLXSW_THERMAL_TRIP_MASK,
707 &mlxsw_thermal_module_ops,
709 if (IS_ERR(module_tz->tzdev)) {
710 err = PTR_ERR(module_tz->tzdev);
714 err = thermal_zone_device_enable(module_tz->tzdev);
716 thermal_zone_device_unregister(module_tz->tzdev);
721 static void mlxsw_thermal_module_tz_fini(struct thermal_zone_device *tzdev)
723 thermal_zone_device_unregister(tzdev);
727 mlxsw_thermal_module_init(struct device *dev, struct mlxsw_core *core,
728 struct mlxsw_thermal *thermal, u8 module)
730 struct mlxsw_thermal_module *module_tz;
732 module_tz = &thermal->tz_module_arr[module];
733 /* Skip if parent is already set (case of port split). */
734 if (module_tz->parent)
736 module_tz->module = module;
737 module_tz->parent = thermal;
738 memcpy(module_tz->trips, default_thermal_trips,
739 sizeof(thermal->trips));
740 /* Initialize all trip point. */
741 mlxsw_thermal_module_trips_reset(module_tz);
742 /* Update trip point according to the module data. */
743 return mlxsw_thermal_module_trips_update(dev, core, module_tz);
746 static void mlxsw_thermal_module_fini(struct mlxsw_thermal_module *module_tz)
748 if (module_tz && module_tz->tzdev) {
749 mlxsw_thermal_module_tz_fini(module_tz->tzdev);
750 module_tz->tzdev = NULL;
751 module_tz->parent = NULL;
756 mlxsw_thermal_modules_init(struct device *dev, struct mlxsw_core *core,
757 struct mlxsw_thermal *thermal)
759 struct mlxsw_thermal_module *module_tz;
760 char mgpir_pl[MLXSW_REG_MGPIR_LEN];
763 if (!mlxsw_core_res_query_enabled(core))
766 mlxsw_reg_mgpir_pack(mgpir_pl);
767 err = mlxsw_reg_query(core, MLXSW_REG(mgpir), mgpir_pl);
771 mlxsw_reg_mgpir_unpack(mgpir_pl, NULL, NULL, NULL,
772 &thermal->tz_module_num);
774 thermal->tz_module_arr = kcalloc(thermal->tz_module_num,
775 sizeof(*thermal->tz_module_arr),
777 if (!thermal->tz_module_arr)
780 for (i = 0; i < thermal->tz_module_num; i++) {
781 err = mlxsw_thermal_module_init(dev, core, thermal, i);
783 goto err_unreg_tz_module_arr;
786 for (i = 0; i < thermal->tz_module_num; i++) {
787 module_tz = &thermal->tz_module_arr[i];
788 if (!module_tz->parent)
790 err = mlxsw_thermal_module_tz_init(module_tz);
792 goto err_unreg_tz_module_arr;
797 err_unreg_tz_module_arr:
798 for (i = thermal->tz_module_num - 1; i >= 0; i--)
799 mlxsw_thermal_module_fini(&thermal->tz_module_arr[i]);
800 kfree(thermal->tz_module_arr);
805 mlxsw_thermal_modules_fini(struct mlxsw_thermal *thermal)
809 if (!mlxsw_core_res_query_enabled(thermal->core))
812 for (i = thermal->tz_module_num - 1; i >= 0; i--)
813 mlxsw_thermal_module_fini(&thermal->tz_module_arr[i]);
814 kfree(thermal->tz_module_arr);
818 mlxsw_thermal_gearbox_tz_init(struct mlxsw_thermal_module *gearbox_tz)
820 char tz_name[MLXSW_THERMAL_ZONE_MAX_NAME];
823 snprintf(tz_name, sizeof(tz_name), "mlxsw-gearbox%d",
824 gearbox_tz->module + 1);
825 gearbox_tz->tzdev = thermal_zone_device_register(tz_name,
826 MLXSW_THERMAL_NUM_TRIPS,
827 MLXSW_THERMAL_TRIP_MASK,
829 &mlxsw_thermal_gearbox_ops,
831 if (IS_ERR(gearbox_tz->tzdev))
832 return PTR_ERR(gearbox_tz->tzdev);
834 ret = thermal_zone_device_enable(gearbox_tz->tzdev);
836 thermal_zone_device_unregister(gearbox_tz->tzdev);
842 mlxsw_thermal_gearbox_tz_fini(struct mlxsw_thermal_module *gearbox_tz)
844 thermal_zone_device_unregister(gearbox_tz->tzdev);
848 mlxsw_thermal_gearboxes_init(struct device *dev, struct mlxsw_core *core,
849 struct mlxsw_thermal *thermal)
851 enum mlxsw_reg_mgpir_device_type device_type;
852 struct mlxsw_thermal_module *gearbox_tz;
853 char mgpir_pl[MLXSW_REG_MGPIR_LEN];
858 if (!mlxsw_core_res_query_enabled(core))
861 mlxsw_reg_mgpir_pack(mgpir_pl);
862 err = mlxsw_reg_query(core, MLXSW_REG(mgpir), mgpir_pl);
866 mlxsw_reg_mgpir_unpack(mgpir_pl, &gbox_num, &device_type, NULL,
868 if (device_type != MLXSW_REG_MGPIR_DEVICE_TYPE_GEARBOX_DIE ||
872 thermal->tz_gearbox_num = gbox_num;
873 thermal->tz_gearbox_arr = kcalloc(thermal->tz_gearbox_num,
874 sizeof(*thermal->tz_gearbox_arr),
876 if (!thermal->tz_gearbox_arr)
879 for (i = 0; i < thermal->tz_gearbox_num; i++) {
880 gearbox_tz = &thermal->tz_gearbox_arr[i];
881 memcpy(gearbox_tz->trips, default_thermal_trips,
882 sizeof(thermal->trips));
883 gearbox_tz->module = i;
884 gearbox_tz->parent = thermal;
885 err = mlxsw_thermal_gearbox_tz_init(gearbox_tz);
887 goto err_unreg_tz_gearbox;
892 err_unreg_tz_gearbox:
893 for (i--; i >= 0; i--)
894 mlxsw_thermal_gearbox_tz_fini(&thermal->tz_gearbox_arr[i]);
895 kfree(thermal->tz_gearbox_arr);
900 mlxsw_thermal_gearboxes_fini(struct mlxsw_thermal *thermal)
904 if (!mlxsw_core_res_query_enabled(thermal->core))
907 for (i = thermal->tz_gearbox_num - 1; i >= 0; i--)
908 mlxsw_thermal_gearbox_tz_fini(&thermal->tz_gearbox_arr[i]);
909 kfree(thermal->tz_gearbox_arr);
912 int mlxsw_thermal_init(struct mlxsw_core *core,
913 const struct mlxsw_bus_info *bus_info,
914 struct mlxsw_thermal **p_thermal)
916 char mfcr_pl[MLXSW_REG_MFCR_LEN] = { 0 };
917 enum mlxsw_reg_mfcr_pwm_frequency freq;
918 struct device *dev = bus_info->dev;
919 struct mlxsw_thermal *thermal;
924 thermal = devm_kzalloc(dev, sizeof(*thermal),
929 thermal->core = core;
930 thermal->bus_info = bus_info;
931 memcpy(thermal->trips, default_thermal_trips, sizeof(thermal->trips));
933 err = mlxsw_reg_query(thermal->core, MLXSW_REG(mfcr), mfcr_pl);
935 dev_err(dev, "Failed to probe PWMs\n");
936 goto err_free_thermal;
938 mlxsw_reg_mfcr_unpack(mfcr_pl, &freq, &tacho_active, &pwm_active);
940 for (i = 0; i < MLXSW_MFCR_TACHOS_MAX; i++) {
941 if (tacho_active & BIT(i)) {
942 char mfsl_pl[MLXSW_REG_MFSL_LEN];
944 mlxsw_reg_mfsl_pack(mfsl_pl, i, 0, 0);
946 /* We need to query the register to preserve maximum */
947 err = mlxsw_reg_query(thermal->core, MLXSW_REG(mfsl),
950 goto err_free_thermal;
952 /* set the minimal RPMs to 0 */
953 mlxsw_reg_mfsl_tach_min_set(mfsl_pl, 0);
954 err = mlxsw_reg_write(thermal->core, MLXSW_REG(mfsl),
957 goto err_free_thermal;
960 for (i = 0; i < MLXSW_MFCR_PWMS_MAX; i++) {
961 if (pwm_active & BIT(i)) {
962 struct thermal_cooling_device *cdev;
964 cdev = thermal_cooling_device_register("mlxsw_fan",
969 dev_err(dev, "Failed to register cooling device\n");
970 goto err_unreg_cdevs;
972 thermal->cdevs[i] = cdev;
976 /* Initialize cooling levels per PWM state. */
977 for (i = 0; i < MLXSW_THERMAL_MAX_STATE; i++)
978 thermal->cooling_levels[i] = max(MLXSW_THERMAL_SPEED_MIN_LEVEL,
981 thermal->polling_delay = bus_info->low_frequency ?
982 MLXSW_THERMAL_SLOW_POLL_INT :
983 MLXSW_THERMAL_POLL_INT;
985 thermal->tzdev = thermal_zone_device_register("mlxsw",
986 MLXSW_THERMAL_NUM_TRIPS,
987 MLXSW_THERMAL_TRIP_MASK,
991 thermal->polling_delay);
992 if (IS_ERR(thermal->tzdev)) {
993 err = PTR_ERR(thermal->tzdev);
994 dev_err(dev, "Failed to register thermal zone\n");
995 goto err_unreg_cdevs;
998 err = mlxsw_thermal_modules_init(dev, core, thermal);
1000 goto err_unreg_tzdev;
1002 err = mlxsw_thermal_gearboxes_init(dev, core, thermal);
1004 goto err_unreg_modules_tzdev;
1006 err = thermal_zone_device_enable(thermal->tzdev);
1008 goto err_unreg_gearboxes;
1010 *p_thermal = thermal;
1013 err_unreg_gearboxes:
1014 mlxsw_thermal_gearboxes_fini(thermal);
1015 err_unreg_modules_tzdev:
1016 mlxsw_thermal_modules_fini(thermal);
1018 if (thermal->tzdev) {
1019 thermal_zone_device_unregister(thermal->tzdev);
1020 thermal->tzdev = NULL;
1023 for (i = 0; i < MLXSW_MFCR_PWMS_MAX; i++)
1024 if (thermal->cdevs[i])
1025 thermal_cooling_device_unregister(thermal->cdevs[i]);
1027 devm_kfree(dev, thermal);
1031 void mlxsw_thermal_fini(struct mlxsw_thermal *thermal)
1035 mlxsw_thermal_gearboxes_fini(thermal);
1036 mlxsw_thermal_modules_fini(thermal);
1037 if (thermal->tzdev) {
1038 thermal_zone_device_unregister(thermal->tzdev);
1039 thermal->tzdev = NULL;
1042 for (i = 0; i < MLXSW_MFCR_PWMS_MAX; i++) {
1043 if (thermal->cdevs[i]) {
1044 thermal_cooling_device_unregister(thermal->cdevs[i]);
1045 thermal->cdevs[i] = NULL;
1049 devm_kfree(thermal->bus_info->dev, thermal);