xref: /linux/drivers/net/ethernet/chelsio/cxgb4/cxgb4_thermal.c (revision 24bce201d79807b668bf9d9e0aca801c5c0d5f78)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Copyright (C) 2017 Chelsio Communications.  All rights reserved.
4  *
5  *  Written by: Ganesh Goudar (ganeshgr@chelsio.com)
6  */
7 
8 #include "cxgb4.h"
9 
10 #define CXGB4_NUM_TRIPS 1
11 
12 static int cxgb4_thermal_get_temp(struct thermal_zone_device *tzdev,
13 				  int *temp)
14 {
15 	struct adapter *adap = tzdev->devdata;
16 	u32 param, val;
17 	int ret;
18 
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));
22 
23 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
24 			      &param, &val);
25 	if (ret < 0 || val == 0)
26 		return -1;
27 
28 	*temp = val * 1000;
29 	return 0;
30 }
31 
32 static int cxgb4_thermal_get_trip_type(struct thermal_zone_device *tzdev,
33 				       int trip, enum thermal_trip_type *type)
34 {
35 	struct adapter *adap = tzdev->devdata;
36 
37 	if (!adap->ch_thermal.trip_temp)
38 		return -EINVAL;
39 
40 	*type = adap->ch_thermal.trip_type;
41 	return 0;
42 }
43 
44 static int cxgb4_thermal_get_trip_temp(struct thermal_zone_device *tzdev,
45 				       int trip, int *temp)
46 {
47 	struct adapter *adap = tzdev->devdata;
48 
49 	if (!adap->ch_thermal.trip_temp)
50 		return -EINVAL;
51 
52 	*temp = adap->ch_thermal.trip_temp;
53 	return 0;
54 }
55 
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,
60 };
61 
62 int cxgb4_thermal_init(struct adapter *adap)
63 {
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;
69 
70 	/* on older firmwares we may not get the trip temperature,
71 	 * set the num of trips to 0.
72 	 */
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));
76 
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;
84 	}
85 
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;
96 	}
97 
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;
103 	}
104 
105 	return 0;
106 }
107 
108 int cxgb4_thermal_remove(struct adapter *adap)
109 {
110 	if (adap->ch_thermal.tzdev) {
111 		thermal_zone_device_unregister(adap->ch_thermal.tzdev);
112 		adap->ch_thermal.tzdev = NULL;
113 	}
114 	return 0;
115 }
116