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 	int num_trip = CXGB4_NUM_TRIPS;
66 	u32 param, val;
67 	int ret;
68 
69 	/* on older firmwares we may not get the trip temperature,
70 	 * set the num of trips to 0.
71 	 */
72 	param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
73 		 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_DIAG) |
74 		 FW_PARAMS_PARAM_Y_V(FW_PARAM_DEV_DIAG_MAXTMPTHRESH));
75 
76 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
77 			      &param, &val);
78 	if (ret < 0) {
79 		num_trip = 0; /* could not get trip temperature */
80 	} else {
81 		ch_thermal->trip_temp = val * 1000;
82 		ch_thermal->trip_type = THERMAL_TRIP_CRITICAL;
83 	}
84 
85 	ch_thermal->tzdev = thermal_zone_device_register("cxgb4", num_trip,
86 							 0, adap,
87 							 &cxgb4_thermal_ops,
88 							 NULL, 0, 0);
89 	if (IS_ERR(ch_thermal->tzdev)) {
90 		ret = PTR_ERR(ch_thermal->tzdev);
91 		dev_err(adap->pdev_dev, "Failed to register thermal zone\n");
92 		ch_thermal->tzdev = NULL;
93 		return ret;
94 	}
95 	return 0;
96 }
97 
98 int cxgb4_thermal_remove(struct adapter *adap)
99 {
100 	if (adap->ch_thermal.tzdev)
101 		thermal_zone_device_unregister(adap->ch_thermal.tzdev);
102 	return 0;
103 }
104