1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2017 Chelsio Communications. All rights reserved.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Written by: Ganesh Goudar (ganeshgr@chelsio.com)
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include "cxgb4.h"
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #define CXGB4_NUM_TRIPS 1
11*4882a593Smuzhiyun
cxgb4_thermal_get_temp(struct thermal_zone_device * tzdev,int * temp)12*4882a593Smuzhiyun static int cxgb4_thermal_get_temp(struct thermal_zone_device *tzdev,
13*4882a593Smuzhiyun int *temp)
14*4882a593Smuzhiyun {
15*4882a593Smuzhiyun struct adapter *adap = tzdev->devdata;
16*4882a593Smuzhiyun u32 param, val;
17*4882a593Smuzhiyun int ret;
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
20*4882a593Smuzhiyun FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_DIAG) |
21*4882a593Smuzhiyun FW_PARAMS_PARAM_Y_V(FW_PARAM_DEV_DIAG_TMP));
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
24*4882a593Smuzhiyun ¶m, &val);
25*4882a593Smuzhiyun if (ret < 0 || val == 0)
26*4882a593Smuzhiyun return -1;
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun *temp = val * 1000;
29*4882a593Smuzhiyun return 0;
30*4882a593Smuzhiyun }
31*4882a593Smuzhiyun
cxgb4_thermal_get_trip_type(struct thermal_zone_device * tzdev,int trip,enum thermal_trip_type * type)32*4882a593Smuzhiyun static int cxgb4_thermal_get_trip_type(struct thermal_zone_device *tzdev,
33*4882a593Smuzhiyun int trip, enum thermal_trip_type *type)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun struct adapter *adap = tzdev->devdata;
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun if (!adap->ch_thermal.trip_temp)
38*4882a593Smuzhiyun return -EINVAL;
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun *type = adap->ch_thermal.trip_type;
41*4882a593Smuzhiyun return 0;
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun
cxgb4_thermal_get_trip_temp(struct thermal_zone_device * tzdev,int trip,int * temp)44*4882a593Smuzhiyun static int cxgb4_thermal_get_trip_temp(struct thermal_zone_device *tzdev,
45*4882a593Smuzhiyun int trip, int *temp)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun struct adapter *adap = tzdev->devdata;
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun if (!adap->ch_thermal.trip_temp)
50*4882a593Smuzhiyun return -EINVAL;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun *temp = adap->ch_thermal.trip_temp;
53*4882a593Smuzhiyun return 0;
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun static struct thermal_zone_device_ops cxgb4_thermal_ops = {
57*4882a593Smuzhiyun .get_temp = cxgb4_thermal_get_temp,
58*4882a593Smuzhiyun .get_trip_type = cxgb4_thermal_get_trip_type,
59*4882a593Smuzhiyun .get_trip_temp = cxgb4_thermal_get_trip_temp,
60*4882a593Smuzhiyun };
61*4882a593Smuzhiyun
cxgb4_thermal_init(struct adapter * adap)62*4882a593Smuzhiyun int cxgb4_thermal_init(struct adapter *adap)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun struct ch_thermal *ch_thermal = &adap->ch_thermal;
65*4882a593Smuzhiyun char ch_tz_name[THERMAL_NAME_LENGTH];
66*4882a593Smuzhiyun int num_trip = CXGB4_NUM_TRIPS;
67*4882a593Smuzhiyun u32 param, val;
68*4882a593Smuzhiyun int ret;
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun /* on older firmwares we may not get the trip temperature,
71*4882a593Smuzhiyun * set the num of trips to 0.
72*4882a593Smuzhiyun */
73*4882a593Smuzhiyun param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
74*4882a593Smuzhiyun FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_DIAG) |
75*4882a593Smuzhiyun FW_PARAMS_PARAM_Y_V(FW_PARAM_DEV_DIAG_MAXTMPTHRESH));
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
78*4882a593Smuzhiyun ¶m, &val);
79*4882a593Smuzhiyun if (ret < 0) {
80*4882a593Smuzhiyun num_trip = 0; /* could not get trip temperature */
81*4882a593Smuzhiyun } else {
82*4882a593Smuzhiyun ch_thermal->trip_temp = val * 1000;
83*4882a593Smuzhiyun ch_thermal->trip_type = THERMAL_TRIP_CRITICAL;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun snprintf(ch_tz_name, sizeof(ch_tz_name), "cxgb4_%s", adap->name);
87*4882a593Smuzhiyun ch_thermal->tzdev = thermal_zone_device_register(ch_tz_name, num_trip,
88*4882a593Smuzhiyun 0, adap,
89*4882a593Smuzhiyun &cxgb4_thermal_ops,
90*4882a593Smuzhiyun NULL, 0, 0);
91*4882a593Smuzhiyun if (IS_ERR(ch_thermal->tzdev)) {
92*4882a593Smuzhiyun ret = PTR_ERR(ch_thermal->tzdev);
93*4882a593Smuzhiyun dev_err(adap->pdev_dev, "Failed to register thermal zone\n");
94*4882a593Smuzhiyun ch_thermal->tzdev = NULL;
95*4882a593Smuzhiyun return ret;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun ret = thermal_zone_device_enable(ch_thermal->tzdev);
99*4882a593Smuzhiyun if (ret) {
100*4882a593Smuzhiyun dev_err(adap->pdev_dev, "Failed to enable thermal zone\n");
101*4882a593Smuzhiyun thermal_zone_device_unregister(adap->ch_thermal.tzdev);
102*4882a593Smuzhiyun return ret;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun return 0;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
cxgb4_thermal_remove(struct adapter * adap)108*4882a593Smuzhiyun int cxgb4_thermal_remove(struct adapter *adap)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun if (adap->ch_thermal.tzdev) {
111*4882a593Smuzhiyun thermal_zone_device_unregister(adap->ch_thermal.tzdev);
112*4882a593Smuzhiyun adap->ch_thermal.tzdev = NULL;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun return 0;
115*4882a593Smuzhiyun }
116