WIP FPC-III support
[linux/fpc-iii.git] / drivers / net / ethernet / chelsio / cxgb4 / cxgb4_thermal.c
blob9a6d65243334af17beac881dd124b5a62ce9d9bd
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2017 Chelsio Communications. All rights reserved.
5 * Written by: Ganesh Goudar (ganeshgr@chelsio.com)
6 */
8 #include "cxgb4.h"
10 #define CXGB4_NUM_TRIPS 1
12 static int cxgb4_thermal_get_temp(struct thermal_zone_device *tzdev,
13 int *temp)
15 struct adapter *adap = tzdev->devdata;
16 u32 param, val;
17 int ret;
19 param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
20 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_DIAG) |
21 FW_PARAMS_PARAM_Y_V(FW_PARAM_DEV_DIAG_TMP));
23 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
24 &param, &val);
25 if (ret < 0 || val == 0)
26 return -1;
28 *temp = val * 1000;
29 return 0;
32 static int cxgb4_thermal_get_trip_type(struct thermal_zone_device *tzdev,
33 int trip, enum thermal_trip_type *type)
35 struct adapter *adap = tzdev->devdata;
37 if (!adap->ch_thermal.trip_temp)
38 return -EINVAL;
40 *type = adap->ch_thermal.trip_type;
41 return 0;
44 static int cxgb4_thermal_get_trip_temp(struct thermal_zone_device *tzdev,
45 int trip, int *temp)
47 struct adapter *adap = tzdev->devdata;
49 if (!adap->ch_thermal.trip_temp)
50 return -EINVAL;
52 *temp = adap->ch_thermal.trip_temp;
53 return 0;
56 static struct thermal_zone_device_ops cxgb4_thermal_ops = {
57 .get_temp = cxgb4_thermal_get_temp,
58 .get_trip_type = cxgb4_thermal_get_trip_type,
59 .get_trip_temp = cxgb4_thermal_get_trip_temp,
62 int cxgb4_thermal_init(struct adapter *adap)
64 struct ch_thermal *ch_thermal = &adap->ch_thermal;
65 char ch_tz_name[THERMAL_NAME_LENGTH];
66 int num_trip = CXGB4_NUM_TRIPS;
67 u32 param, val;
68 int ret;
70 /* on older firmwares we may not get the trip temperature,
71 * set the num of trips to 0.
73 param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
74 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_DIAG) |
75 FW_PARAMS_PARAM_Y_V(FW_PARAM_DEV_DIAG_MAXTMPTHRESH));
77 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
78 &param, &val);
79 if (ret < 0) {
80 num_trip = 0; /* could not get trip temperature */
81 } else {
82 ch_thermal->trip_temp = val * 1000;
83 ch_thermal->trip_type = THERMAL_TRIP_CRITICAL;
86 snprintf(ch_tz_name, sizeof(ch_tz_name), "cxgb4_%s", adap->name);
87 ch_thermal->tzdev = thermal_zone_device_register(ch_tz_name, num_trip,
88 0, adap,
89 &cxgb4_thermal_ops,
90 NULL, 0, 0);
91 if (IS_ERR(ch_thermal->tzdev)) {
92 ret = PTR_ERR(ch_thermal->tzdev);
93 dev_err(adap->pdev_dev, "Failed to register thermal zone\n");
94 ch_thermal->tzdev = NULL;
95 return ret;
98 ret = thermal_zone_device_enable(ch_thermal->tzdev);
99 if (ret) {
100 dev_err(adap->pdev_dev, "Failed to enable thermal zone\n");
101 thermal_zone_device_unregister(adap->ch_thermal.tzdev);
102 return ret;
105 return 0;
108 int cxgb4_thermal_remove(struct adapter *adap)
110 if (adap->ch_thermal.tzdev) {
111 thermal_zone_device_unregister(adap->ch_thermal.tzdev);
112 adap->ch_thermal.tzdev = NULL;
114 return 0;