1 /*
2  *  Copyright (C) 2017 Chelsio Communications.  All rights reserved.
3  *
4  *  This program is free software; you can redistribute it and/or modify it
5  *  under the terms and conditions of the GNU General Public License,
6  *  version 2, as published by the Free Software Foundation.
7  *
8  *  This program is distributed in the hope it will be useful, but WITHOUT
9  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  *  more details.
12  *
13  *  The full GNU General Public License is included in this distribution in
14  *  the file called "COPYING".
15  *
16  *  Written by: Ganesh Goudar (ganeshgr@chelsio.com)
17  */
18 
19 #include "cxgb4.h"
20 
21 #define CXGB4_NUM_TRIPS 1
22 
23 static int cxgb4_thermal_get_temp(struct thermal_zone_device *tzdev,
24 				  int *temp)
25 {
26 	struct adapter *adap = tzdev->devdata;
27 	u32 param, val;
28 	int ret;
29 
30 	param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
31 		 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_DIAG) |
32 		 FW_PARAMS_PARAM_Y_V(FW_PARAM_DEV_DIAG_TMP));
33 
34 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
35 			      &param, &val);
36 	if (ret < 0 || val == 0)
37 		return -1;
38 
39 	*temp = val * 1000;
40 	return 0;
41 }
42 
43 static int cxgb4_thermal_get_trip_type(struct thermal_zone_device *tzdev,
44 				       int trip, enum thermal_trip_type *type)
45 {
46 	struct adapter *adap = tzdev->devdata;
47 
48 	if (!adap->ch_thermal.trip_temp)
49 		return -EINVAL;
50 
51 	*type = adap->ch_thermal.trip_type;
52 	return 0;
53 }
54 
55 static int cxgb4_thermal_get_trip_temp(struct thermal_zone_device *tzdev,
56 				       int trip, int *temp)
57 {
58 	struct adapter *adap = tzdev->devdata;
59 
60 	if (!adap->ch_thermal.trip_temp)
61 		return -EINVAL;
62 
63 	*temp = adap->ch_thermal.trip_temp;
64 	return 0;
65 }
66 
67 static struct thermal_zone_device_ops cxgb4_thermal_ops = {
68 	.get_temp = cxgb4_thermal_get_temp,
69 	.get_trip_type = cxgb4_thermal_get_trip_type,
70 	.get_trip_temp = cxgb4_thermal_get_trip_temp,
71 };
72 
73 int cxgb4_thermal_init(struct adapter *adap)
74 {
75 	struct ch_thermal *ch_thermal = &adap->ch_thermal;
76 	int num_trip = CXGB4_NUM_TRIPS;
77 	u32 param, val;
78 	int ret;
79 
80 	/* on older firmwares we may not get the trip temperature,
81 	 * set the num of trips to 0.
82 	 */
83 	param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
84 		 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_DIAG) |
85 		 FW_PARAMS_PARAM_Y_V(FW_PARAM_DEV_DIAG_MAXTMPTHRESH));
86 
87 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
88 			      &param, &val);
89 	if (ret < 0) {
90 		num_trip = 0; /* could not get trip temperature */
91 	} else {
92 		ch_thermal->trip_temp = val * 1000;
93 		ch_thermal->trip_type = THERMAL_TRIP_CRITICAL;
94 	}
95 
96 	ch_thermal->tzdev = thermal_zone_device_register("cxgb4", num_trip,
97 							 0, adap,
98 							 &cxgb4_thermal_ops,
99 							 NULL, 0, 0);
100 	if (IS_ERR(ch_thermal->tzdev)) {
101 		ret = PTR_ERR(ch_thermal->tzdev);
102 		dev_err(adap->pdev_dev, "Failed to register thermal zone\n");
103 		ch_thermal->tzdev = NULL;
104 		return ret;
105 	}
106 	return 0;
107 }
108 
109 int cxgb4_thermal_remove(struct adapter *adap)
110 {
111 	if (adap->ch_thermal.tzdev)
112 		thermal_zone_device_unregister(adap->ch_thermal.tzdev);
113 	return 0;
114 }
115