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 12b1871915SGanesh Goudar static int cxgb4_thermal_get_temp(struct thermal_zone_device *tzdev, 13b1871915SGanesh Goudar int *temp) 14b1871915SGanesh Goudar { 15b1871915SGanesh Goudar struct adapter *adap = tzdev->devdata; 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 ¶m, &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 int cxgb4_thermal_get_trip_type(struct thermal_zone_device *tzdev, 33b1871915SGanesh Goudar int trip, enum thermal_trip_type *type) 34b1871915SGanesh Goudar { 35b1871915SGanesh Goudar struct adapter *adap = tzdev->devdata; 36b1871915SGanesh Goudar 37b1871915SGanesh Goudar if (!adap->ch_thermal.trip_temp) 38b1871915SGanesh Goudar return -EINVAL; 39b1871915SGanesh Goudar 40b1871915SGanesh Goudar *type = adap->ch_thermal.trip_type; 41b1871915SGanesh Goudar return 0; 42b1871915SGanesh Goudar } 43b1871915SGanesh Goudar 44b1871915SGanesh Goudar static int cxgb4_thermal_get_trip_temp(struct thermal_zone_device *tzdev, 45b1871915SGanesh Goudar int trip, int *temp) 46b1871915SGanesh Goudar { 47b1871915SGanesh Goudar struct adapter *adap = tzdev->devdata; 48b1871915SGanesh Goudar 49b1871915SGanesh Goudar if (!adap->ch_thermal.trip_temp) 50b1871915SGanesh Goudar return -EINVAL; 51b1871915SGanesh Goudar 52b1871915SGanesh Goudar *temp = adap->ch_thermal.trip_temp; 53b1871915SGanesh Goudar return 0; 54b1871915SGanesh Goudar } 55b1871915SGanesh Goudar 56b1871915SGanesh Goudar static struct thermal_zone_device_ops cxgb4_thermal_ops = { 57b1871915SGanesh Goudar .get_temp = cxgb4_thermal_get_temp, 58b1871915SGanesh Goudar .get_trip_type = cxgb4_thermal_get_trip_type, 59b1871915SGanesh Goudar .get_trip_temp = cxgb4_thermal_get_trip_temp, 60b1871915SGanesh Goudar }; 61b1871915SGanesh Goudar 62b1871915SGanesh Goudar int cxgb4_thermal_init(struct adapter *adap) 63b1871915SGanesh Goudar { 64b1871915SGanesh Goudar struct ch_thermal *ch_thermal = &adap->ch_thermal; 65*6b6382a8SPotnuri Bharat Teja char ch_tz_name[THERMAL_NAME_LENGTH]; 66b1871915SGanesh Goudar int num_trip = CXGB4_NUM_TRIPS; 67b1871915SGanesh Goudar u32 param, val; 68b1871915SGanesh Goudar int ret; 69b1871915SGanesh Goudar 70b1871915SGanesh Goudar /* on older firmwares we may not get the trip temperature, 71b1871915SGanesh Goudar * set the num of trips to 0. 72b1871915SGanesh Goudar */ 73b1871915SGanesh Goudar param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) | 74b1871915SGanesh Goudar FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_DIAG) | 75b1871915SGanesh Goudar FW_PARAMS_PARAM_Y_V(FW_PARAM_DEV_DIAG_MAXTMPTHRESH)); 76b1871915SGanesh Goudar 77b1871915SGanesh Goudar ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, 78b1871915SGanesh Goudar ¶m, &val); 79b1871915SGanesh Goudar if (ret < 0) { 80b1871915SGanesh Goudar num_trip = 0; /* could not get trip temperature */ 81b1871915SGanesh Goudar } else { 82b1871915SGanesh Goudar ch_thermal->trip_temp = val * 1000; 83b1871915SGanesh Goudar ch_thermal->trip_type = THERMAL_TRIP_CRITICAL; 84b1871915SGanesh Goudar } 85b1871915SGanesh Goudar 86*6b6382a8SPotnuri Bharat Teja snprintf(ch_tz_name, sizeof(ch_tz_name), "cxgb4_%s", adap->name); 87*6b6382a8SPotnuri Bharat Teja ch_thermal->tzdev = thermal_zone_device_register(ch_tz_name, num_trip, 88b1871915SGanesh Goudar 0, adap, 89b1871915SGanesh Goudar &cxgb4_thermal_ops, 90b1871915SGanesh Goudar NULL, 0, 0); 91b1871915SGanesh Goudar if (IS_ERR(ch_thermal->tzdev)) { 92b1871915SGanesh Goudar ret = PTR_ERR(ch_thermal->tzdev); 93b1871915SGanesh Goudar dev_err(adap->pdev_dev, "Failed to register thermal zone\n"); 94b1871915SGanesh Goudar ch_thermal->tzdev = NULL; 95b1871915SGanesh Goudar return ret; 96b1871915SGanesh Goudar } 97bbcf90c0SAndrzej Pietrasiewicz 98bbcf90c0SAndrzej Pietrasiewicz ret = thermal_zone_device_enable(ch_thermal->tzdev); 99bbcf90c0SAndrzej Pietrasiewicz if (ret) { 100bbcf90c0SAndrzej Pietrasiewicz dev_err(adap->pdev_dev, "Failed to enable thermal zone\n"); 101bbcf90c0SAndrzej Pietrasiewicz thermal_zone_device_unregister(adap->ch_thermal.tzdev); 102bbcf90c0SAndrzej Pietrasiewicz return ret; 103bbcf90c0SAndrzej Pietrasiewicz } 104bbcf90c0SAndrzej Pietrasiewicz 105b1871915SGanesh Goudar return 0; 106b1871915SGanesh Goudar } 107b1871915SGanesh Goudar 108b1871915SGanesh Goudar int cxgb4_thermal_remove(struct adapter *adap) 109b1871915SGanesh Goudar { 110*6b6382a8SPotnuri Bharat Teja if (adap->ch_thermal.tzdev) { 111b1871915SGanesh Goudar thermal_zone_device_unregister(adap->ch_thermal.tzdev); 112*6b6382a8SPotnuri Bharat Teja adap->ch_thermal.tzdev = NULL; 113*6b6382a8SPotnuri Bharat Teja } 114b1871915SGanesh Goudar return 0; 115b1871915SGanesh Goudar } 116