14fa9c49fSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b1871915SGanesh Goudar /*
3b1871915SGanesh Goudar  *  Copyright (C) 2017 Chelsio Communications.  All rights reserved.
4b1871915SGanesh Goudar  *
5b1871915SGanesh Goudar  *  Written by: Ganesh Goudar (ganeshgr@chelsio.com)
6b1871915SGanesh Goudar  */
7b1871915SGanesh Goudar 
8b1871915SGanesh Goudar #include "cxgb4.h"
9b1871915SGanesh Goudar 
10b1871915SGanesh Goudar #define CXGB4_NUM_TRIPS 1
11b1871915SGanesh Goudar 
cxgb4_thermal_get_temp(struct thermal_zone_device * tzdev,int * temp)12b1871915SGanesh Goudar static int cxgb4_thermal_get_temp(struct thermal_zone_device *tzdev,
13b1871915SGanesh Goudar 				  int *temp)
14b1871915SGanesh Goudar {
15*3d4e1badSDaniel Lezcano 	struct adapter *adap = thermal_zone_device_priv(tzdev);
16b1871915SGanesh Goudar 	u32 param, val;
17b1871915SGanesh Goudar 	int ret;
18b1871915SGanesh Goudar 
19b1871915SGanesh Goudar 	param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
20b1871915SGanesh Goudar 		 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_DIAG) |
21b1871915SGanesh Goudar 		 FW_PARAMS_PARAM_Y_V(FW_PARAM_DEV_DIAG_TMP));
22b1871915SGanesh Goudar 
23b1871915SGanesh Goudar 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
24b1871915SGanesh Goudar 			      &param, &val);
25b1871915SGanesh Goudar 	if (ret < 0 || val == 0)
26b1871915SGanesh Goudar 		return -1;
27b1871915SGanesh Goudar 
28b1871915SGanesh Goudar 	*temp = val * 1000;
29b1871915SGanesh Goudar 	return 0;
30b1871915SGanesh Goudar }
31b1871915SGanesh Goudar 
32b1871915SGanesh Goudar static struct thermal_zone_device_ops cxgb4_thermal_ops = {
33b1871915SGanesh Goudar 	.get_temp = cxgb4_thermal_get_temp,
34b1871915SGanesh Goudar };
35b1871915SGanesh Goudar 
369ddcb809SDaniel Lezcano static struct thermal_trip trip = { .type = THERMAL_TRIP_CRITICAL } ;
379ddcb809SDaniel Lezcano 
cxgb4_thermal_init(struct adapter * adap)38b1871915SGanesh Goudar int cxgb4_thermal_init(struct adapter *adap)
39b1871915SGanesh Goudar {
40b1871915SGanesh Goudar 	struct ch_thermal *ch_thermal = &adap->ch_thermal;
416b6382a8SPotnuri Bharat Teja 	char ch_tz_name[THERMAL_NAME_LENGTH];
42b1871915SGanesh Goudar 	int num_trip = CXGB4_NUM_TRIPS;
43b1871915SGanesh Goudar 	u32 param, val;
44b1871915SGanesh Goudar 	int ret;
45b1871915SGanesh Goudar 
46b1871915SGanesh Goudar 	/* on older firmwares we may not get the trip temperature,
47b1871915SGanesh Goudar 	 * set the num of trips to 0.
48b1871915SGanesh Goudar 	 */
49b1871915SGanesh Goudar 	param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
50b1871915SGanesh Goudar 		 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_DIAG) |
51b1871915SGanesh Goudar 		 FW_PARAMS_PARAM_Y_V(FW_PARAM_DEV_DIAG_MAXTMPTHRESH));
52b1871915SGanesh Goudar 
53b1871915SGanesh Goudar 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
54b1871915SGanesh Goudar 			      &param, &val);
55b1871915SGanesh Goudar 	if (ret < 0) {
56b1871915SGanesh Goudar 		num_trip = 0; /* could not get trip temperature */
57b1871915SGanesh Goudar 	} else {
589ddcb809SDaniel Lezcano 		trip.temperature = val * 1000;
59b1871915SGanesh Goudar 	}
60b1871915SGanesh Goudar 
616b6382a8SPotnuri Bharat Teja 	snprintf(ch_tz_name, sizeof(ch_tz_name), "cxgb4_%s", adap->name);
629ddcb809SDaniel Lezcano 	ch_thermal->tzdev = thermal_zone_device_register_with_trips(ch_tz_name, &trip, num_trip,
63b1871915SGanesh Goudar 								    0, adap,
64b1871915SGanesh Goudar 								    &cxgb4_thermal_ops,
65b1871915SGanesh Goudar 								    NULL, 0, 0);
66b1871915SGanesh Goudar 	if (IS_ERR(ch_thermal->tzdev)) {
67b1871915SGanesh Goudar 		ret = PTR_ERR(ch_thermal->tzdev);
68b1871915SGanesh Goudar 		dev_err(adap->pdev_dev, "Failed to register thermal zone\n");
69b1871915SGanesh Goudar 		ch_thermal->tzdev = NULL;
70b1871915SGanesh Goudar 		return ret;
71b1871915SGanesh Goudar 	}
72bbcf90c0SAndrzej Pietrasiewicz 
73bbcf90c0SAndrzej Pietrasiewicz 	ret = thermal_zone_device_enable(ch_thermal->tzdev);
74bbcf90c0SAndrzej Pietrasiewicz 	if (ret) {
75bbcf90c0SAndrzej Pietrasiewicz 		dev_err(adap->pdev_dev, "Failed to enable thermal zone\n");
76bbcf90c0SAndrzej Pietrasiewicz 		thermal_zone_device_unregister(adap->ch_thermal.tzdev);
77bbcf90c0SAndrzej Pietrasiewicz 		return ret;
78bbcf90c0SAndrzej Pietrasiewicz 	}
79bbcf90c0SAndrzej Pietrasiewicz 
80b1871915SGanesh Goudar 	return 0;
81b1871915SGanesh Goudar }
82b1871915SGanesh Goudar 
cxgb4_thermal_remove(struct adapter * adap)83b1871915SGanesh Goudar int cxgb4_thermal_remove(struct adapter *adap)
84b1871915SGanesh Goudar {
856b6382a8SPotnuri Bharat Teja 	if (adap->ch_thermal.tzdev) {
86b1871915SGanesh Goudar 		thermal_zone_device_unregister(adap->ch_thermal.tzdev);
876b6382a8SPotnuri Bharat Teja 		adap->ch_thermal.tzdev = NULL;
886b6382a8SPotnuri Bharat Teja 	}
89b1871915SGanesh Goudar 	return 0;
90b1871915SGanesh Goudar }
91