Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright 2016-2019 HabanaLabs, Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include "habanalabs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #define HWMON_NR_SENSOR_TYPES		(hwmon_pwm + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) int hl_build_hwmon_channel_info(struct hl_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 				struct cpucp_sensor *sensors_arr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	u32 counts[HWMON_NR_SENSOR_TYPES] = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	u32 *sensors_by_type[HWMON_NR_SENSOR_TYPES] = {NULL};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	u32 sensors_by_type_next_index[HWMON_NR_SENSOR_TYPES] = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	struct hwmon_channel_info **channels_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	u32 num_sensors_for_type, num_active_sensor_types = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 			arr_size = 0, *curr_arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	enum hwmon_sensor_types type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	int rc, i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	for (i = 0 ; i < CPUCP_MAX_SENSORS ; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 		type = le32_to_cpu(sensors_arr[i].type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		if ((type == 0) && (sensors_arr[i].flags == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		if (type >= HWMON_NR_SENSOR_TYPES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 			dev_err(hdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 				"Got wrong sensor type %d from device\n", type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		counts[type]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		arr_size++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	for (i = 0 ; i < HWMON_NR_SENSOR_TYPES ; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		if (counts[i] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		num_sensors_for_type = counts[i] + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		curr_arr = kcalloc(num_sensors_for_type, sizeof(*curr_arr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 				GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		if (!curr_arr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 			rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 			goto sensors_type_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		num_active_sensor_types++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		sensors_by_type[i] = curr_arr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	for (i = 0 ; i < arr_size ; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		type = le32_to_cpu(sensors_arr[i].type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		curr_arr = sensors_by_type[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		curr_arr[sensors_by_type_next_index[type]++] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 				le32_to_cpu(sensors_arr[i].flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	channels_info = kcalloc(num_active_sensor_types + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			sizeof(*channels_info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	if (!channels_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		goto channels_info_array_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	for (i = 0 ; i < num_active_sensor_types ; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		channels_info[i] = kzalloc(sizeof(*channels_info[i]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 				GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		if (!channels_info[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			goto channel_info_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	for (i = 0, j = 0 ; i < HWMON_NR_SENSOR_TYPES ; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		if (!sensors_by_type[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		channels_info[j]->type = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		channels_info[j]->config = sensors_by_type[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		j++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	hdev->hl_chip_info->info =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			(const struct hwmon_channel_info **)channels_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) channel_info_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	for (i = 0 ; i < num_active_sensor_types ; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		if (channels_info[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			kfree(channels_info[i]->config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			kfree(channels_info[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	kfree(channels_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) channels_info_array_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) sensors_type_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	for (i = 0 ; i < HWMON_NR_SENSOR_TYPES ; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		kfree(sensors_by_type[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static int hl_read(struct device *dev, enum hwmon_sensor_types type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			u32 attr, int channel, long *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	struct hl_device *hdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (hl_device_disabled_or_in_reset(hdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	case hwmon_temp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		switch (attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		case hwmon_temp_input:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		case hwmon_temp_max:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		case hwmon_temp_crit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		case hwmon_temp_max_hyst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		case hwmon_temp_crit_hyst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		case hwmon_temp_offset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		case hwmon_temp_highest:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		rc = hl_get_temperature(hdev, channel, attr, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	case hwmon_in:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		switch (attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		case hwmon_in_input:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		case hwmon_in_min:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		case hwmon_in_max:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		case hwmon_in_highest:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		rc = hl_get_voltage(hdev, channel, attr, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	case hwmon_curr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		switch (attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		case hwmon_curr_input:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		case hwmon_curr_min:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		case hwmon_curr_max:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		case hwmon_curr_highest:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		rc = hl_get_current(hdev, channel, attr, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	case hwmon_fan:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		switch (attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		case hwmon_fan_input:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		case hwmon_fan_min:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		case hwmon_fan_max:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		rc = hl_get_fan_speed(hdev, channel, attr, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	case hwmon_pwm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		switch (attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		case hwmon_pwm_input:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		case hwmon_pwm_enable:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		rc = hl_get_pwm_info(hdev, channel, attr, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static int hl_write(struct device *dev, enum hwmon_sensor_types type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			u32 attr, int channel, long val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	struct hl_device *hdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	if (hl_device_disabled_or_in_reset(hdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	case hwmon_temp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		switch (attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		case hwmon_temp_offset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		case hwmon_temp_reset_history:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		hl_set_temperature(hdev, channel, attr, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	case hwmon_pwm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		switch (attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		case hwmon_pwm_input:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		case hwmon_pwm_enable:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		hl_set_pwm_info(hdev, channel, attr, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	case hwmon_in:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		switch (attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		case hwmon_in_reset_history:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		hl_set_voltage(hdev, channel, attr, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	case hwmon_curr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		switch (attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		case hwmon_curr_reset_history:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		hl_set_current(hdev, channel, attr, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static umode_t hl_is_visible(const void *data, enum hwmon_sensor_types type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 				u32 attr, int channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	case hwmon_temp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		switch (attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		case hwmon_temp_input:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		case hwmon_temp_max:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		case hwmon_temp_max_hyst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		case hwmon_temp_crit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		case hwmon_temp_crit_hyst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		case hwmon_temp_highest:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			return 0444;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		case hwmon_temp_offset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			return 0644;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		case hwmon_temp_reset_history:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 			return 0200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	case hwmon_in:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		switch (attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		case hwmon_in_input:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		case hwmon_in_min:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		case hwmon_in_max:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		case hwmon_in_highest:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			return 0444;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		case hwmon_in_reset_history:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 			return 0200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	case hwmon_curr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		switch (attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		case hwmon_curr_input:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		case hwmon_curr_min:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		case hwmon_curr_max:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		case hwmon_curr_highest:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			return 0444;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		case hwmon_curr_reset_history:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			return 0200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	case hwmon_fan:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		switch (attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		case hwmon_fan_input:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		case hwmon_fan_min:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		case hwmon_fan_max:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			return 0444;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	case hwmon_pwm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		switch (attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		case hwmon_pwm_input:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		case hwmon_pwm_enable:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			return 0644;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static const struct hwmon_ops hl_hwmon_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	.is_visible = hl_is_visible,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	.read = hl_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	.write = hl_write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) int hl_get_temperature(struct hl_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			int sensor_index, u32 attr, long *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	struct cpucp_packet pkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	memset(&pkt, 0, sizeof(pkt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_TEMPERATURE_GET <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 				CPUCP_PKT_CTL_OPCODE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	pkt.sensor_index = __cpu_to_le16(sensor_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	pkt.type = __cpu_to_le16(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 						0, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		dev_err(hdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 			"Failed to get temperature from sensor %d, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 			sensor_index, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		*value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) int hl_set_temperature(struct hl_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 			int sensor_index, u32 attr, long value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	struct cpucp_packet pkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	memset(&pkt, 0, sizeof(pkt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_TEMPERATURE_SET <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 				CPUCP_PKT_CTL_OPCODE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	pkt.sensor_index = __cpu_to_le16(sensor_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	pkt.type = __cpu_to_le16(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	pkt.value = __cpu_to_le64(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 						0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		dev_err(hdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 			"Failed to set temperature of sensor %d, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 			sensor_index, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) int hl_get_voltage(struct hl_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			int sensor_index, u32 attr, long *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	struct cpucp_packet pkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	memset(&pkt, 0, sizeof(pkt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_VOLTAGE_GET <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 				CPUCP_PKT_CTL_OPCODE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	pkt.sensor_index = __cpu_to_le16(sensor_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	pkt.type = __cpu_to_le16(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 						0, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		dev_err(hdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 			"Failed to get voltage from sensor %d, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 			sensor_index, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		*value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) int hl_get_current(struct hl_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 			int sensor_index, u32 attr, long *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	struct cpucp_packet pkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	memset(&pkt, 0, sizeof(pkt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_CURRENT_GET <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 				CPUCP_PKT_CTL_OPCODE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	pkt.sensor_index = __cpu_to_le16(sensor_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	pkt.type = __cpu_to_le16(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 						0, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		dev_err(hdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 			"Failed to get current from sensor %d, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 			sensor_index, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		*value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) int hl_get_fan_speed(struct hl_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 			int sensor_index, u32 attr, long *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	struct cpucp_packet pkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	memset(&pkt, 0, sizeof(pkt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_FAN_SPEED_GET <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 				CPUCP_PKT_CTL_OPCODE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	pkt.sensor_index = __cpu_to_le16(sensor_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	pkt.type = __cpu_to_le16(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 						0, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		dev_err(hdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 			"Failed to get fan speed from sensor %d, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 			sensor_index, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		*value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) int hl_get_pwm_info(struct hl_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 			int sensor_index, u32 attr, long *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	struct cpucp_packet pkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	memset(&pkt, 0, sizeof(pkt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_PWM_GET <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 				CPUCP_PKT_CTL_OPCODE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	pkt.sensor_index = __cpu_to_le16(sensor_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	pkt.type = __cpu_to_le16(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 						0, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		dev_err(hdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 			"Failed to get pwm info from sensor %d, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 			sensor_index, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		*value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) void hl_set_pwm_info(struct hl_device *hdev, int sensor_index, u32 attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 			long value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	struct cpucp_packet pkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	memset(&pkt, 0, sizeof(pkt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_PWM_SET <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 				CPUCP_PKT_CTL_OPCODE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	pkt.sensor_index = __cpu_to_le16(sensor_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	pkt.type = __cpu_to_le16(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	pkt.value = cpu_to_le64(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 						0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		dev_err(hdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 			"Failed to set pwm info to sensor %d, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 			sensor_index, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) int hl_set_voltage(struct hl_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 			int sensor_index, u32 attr, long value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	struct cpucp_packet pkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	memset(&pkt, 0, sizeof(pkt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_VOLTAGE_SET <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 				CPUCP_PKT_CTL_OPCODE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	pkt.sensor_index = __cpu_to_le16(sensor_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	pkt.type = __cpu_to_le16(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	pkt.value = __cpu_to_le64(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 						0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		dev_err(hdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 			"Failed to set voltage of sensor %d, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 			sensor_index, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) int hl_set_current(struct hl_device *hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 			int sensor_index, u32 attr, long value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	struct cpucp_packet pkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	memset(&pkt, 0, sizeof(pkt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	pkt.ctl = cpu_to_le32(CPUCP_PACKET_CURRENT_SET <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 				CPUCP_PKT_CTL_OPCODE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	pkt.sensor_index = __cpu_to_le16(sensor_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	pkt.type = __cpu_to_le16(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	pkt.value = __cpu_to_le64(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 						0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		dev_err(hdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 			"Failed to set current of sensor %d, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 			sensor_index, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) int hl_hwmon_init(struct hl_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	struct device *dev = hdev->pdev ? &hdev->pdev->dev : hdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	struct asic_fixed_properties *prop = &hdev->asic_prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	if ((hdev->hwmon_initialized) || !(hdev->fw_loading))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	if (hdev->hl_chip_info->info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		hdev->hl_chip_info->ops = &hl_hwmon_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		hdev->hwmon_dev = hwmon_device_register_with_info(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 					prop->cpucp_info.card_name, hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 					hdev->hl_chip_info, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		if (IS_ERR(hdev->hwmon_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 			rc = PTR_ERR(hdev->hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 			dev_err(hdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 				"Unable to register hwmon device: %d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		dev_info(hdev->dev, "%s: add sensors information\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 			dev_name(hdev->hwmon_dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 		hdev->hwmon_initialized = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		dev_info(hdev->dev, "no available sensors\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) void hl_hwmon_fini(struct hl_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	if (!hdev->hwmon_initialized)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	hwmon_device_unregister(hdev->hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) }