Home | History | Annotate | Line # | Download | only in nouveau
      1  1.2  riastrad /*	$NetBSD: nouveau_hwmon.c,v 1.3 2021/12/18 23:45:32 riastradh Exp $	*/
      2  1.2  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2010 Red Hat Inc.
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     12  1.1  riastrad  *
     13  1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     14  1.1  riastrad  * all copies or substantial portions of the Software.
     15  1.1  riastrad  *
     16  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     23  1.1  riastrad  *
     24  1.1  riastrad  * Authors: Ben Skeggs
     25  1.1  riastrad  */
     26  1.1  riastrad 
     27  1.2  riastrad #include <sys/cdefs.h>
     28  1.2  riastrad __KERNEL_RCSID(0, "$NetBSD: nouveau_hwmon.c,v 1.3 2021/12/18 23:45:32 riastradh Exp $");
     29  1.2  riastrad 
     30  1.1  riastrad #ifdef CONFIG_ACPI
     31  1.1  riastrad #include <linux/acpi.h>
     32  1.1  riastrad #endif
     33  1.1  riastrad #include <linux/power_supply.h>
     34  1.1  riastrad #include <linux/hwmon.h>
     35  1.1  riastrad #include <linux/hwmon-sysfs.h>
     36  1.1  riastrad 
     37  1.3  riastrad #include "nouveau_drv.h"
     38  1.3  riastrad #include "nouveau_hwmon.h"
     39  1.1  riastrad 
     40  1.3  riastrad #include <nvkm/subdev/iccsense.h>
     41  1.3  riastrad #include <nvkm/subdev/volt.h>
     42  1.1  riastrad 
     43  1.1  riastrad #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
     44  1.1  riastrad 
     45  1.1  riastrad static ssize_t
     46  1.1  riastrad nouveau_hwmon_show_temp1_auto_point1_pwm(struct device *d,
     47  1.1  riastrad 					 struct device_attribute *a, char *buf)
     48  1.1  riastrad {
     49  1.1  riastrad 	return snprintf(buf, PAGE_SIZE, "%d\n", 100);
     50  1.1  riastrad }
     51  1.3  riastrad static SENSOR_DEVICE_ATTR(temp1_auto_point1_pwm, 0444,
     52  1.1  riastrad 			  nouveau_hwmon_show_temp1_auto_point1_pwm, NULL, 0);
     53  1.1  riastrad 
     54  1.1  riastrad static ssize_t
     55  1.1  riastrad nouveau_hwmon_temp1_auto_point1_temp(struct device *d,
     56  1.1  riastrad 				     struct device_attribute *a, char *buf)
     57  1.1  riastrad {
     58  1.1  riastrad 	struct drm_device *dev = dev_get_drvdata(d);
     59  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
     60  1.3  riastrad 	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
     61  1.1  riastrad 
     62  1.1  riastrad 	return snprintf(buf, PAGE_SIZE, "%d\n",
     63  1.2  riastrad 	      therm->attr_get(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST) * 1000);
     64  1.1  riastrad }
     65  1.1  riastrad static ssize_t
     66  1.1  riastrad nouveau_hwmon_set_temp1_auto_point1_temp(struct device *d,
     67  1.1  riastrad 					 struct device_attribute *a,
     68  1.1  riastrad 					 const char *buf, size_t count)
     69  1.1  riastrad {
     70  1.1  riastrad 	struct drm_device *dev = dev_get_drvdata(d);
     71  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
     72  1.3  riastrad 	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
     73  1.1  riastrad 	long value;
     74  1.1  riastrad 
     75  1.3  riastrad 	if (kstrtol(buf, 10, &value))
     76  1.3  riastrad 		return -EINVAL;
     77  1.1  riastrad 
     78  1.2  riastrad 	therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST,
     79  1.1  riastrad 			value / 1000);
     80  1.1  riastrad 
     81  1.1  riastrad 	return count;
     82  1.1  riastrad }
     83  1.3  riastrad static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp, 0644,
     84  1.1  riastrad 			  nouveau_hwmon_temp1_auto_point1_temp,
     85  1.1  riastrad 			  nouveau_hwmon_set_temp1_auto_point1_temp, 0);
     86  1.1  riastrad 
     87  1.1  riastrad static ssize_t
     88  1.1  riastrad nouveau_hwmon_temp1_auto_point1_temp_hyst(struct device *d,
     89  1.1  riastrad 					  struct device_attribute *a, char *buf)
     90  1.1  riastrad {
     91  1.1  riastrad 	struct drm_device *dev = dev_get_drvdata(d);
     92  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
     93  1.3  riastrad 	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
     94  1.1  riastrad 
     95  1.1  riastrad 	return snprintf(buf, PAGE_SIZE, "%d\n",
     96  1.2  riastrad 	 therm->attr_get(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST) * 1000);
     97  1.1  riastrad }
     98  1.1  riastrad static ssize_t
     99  1.1  riastrad nouveau_hwmon_set_temp1_auto_point1_temp_hyst(struct device *d,
    100  1.1  riastrad 					      struct device_attribute *a,
    101  1.1  riastrad 					      const char *buf, size_t count)
    102  1.1  riastrad {
    103  1.1  riastrad 	struct drm_device *dev = dev_get_drvdata(d);
    104  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    105  1.3  riastrad 	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
    106  1.1  riastrad 	long value;
    107  1.1  riastrad 
    108  1.3  riastrad 	if (kstrtol(buf, 10, &value))
    109  1.3  riastrad 		return -EINVAL;
    110  1.1  riastrad 
    111  1.2  riastrad 	therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST,
    112  1.1  riastrad 			value / 1000);
    113  1.1  riastrad 
    114  1.1  riastrad 	return count;
    115  1.1  riastrad }
    116  1.3  riastrad static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp_hyst, 0644,
    117  1.1  riastrad 			  nouveau_hwmon_temp1_auto_point1_temp_hyst,
    118  1.1  riastrad 			  nouveau_hwmon_set_temp1_auto_point1_temp_hyst, 0);
    119  1.1  riastrad 
    120  1.1  riastrad static ssize_t
    121  1.3  riastrad nouveau_hwmon_get_pwm1_max(struct device *d,
    122  1.3  riastrad 			   struct device_attribute *a, char *buf)
    123  1.1  riastrad {
    124  1.1  riastrad 	struct drm_device *dev = dev_get_drvdata(d);
    125  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    126  1.3  riastrad 	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
    127  1.3  riastrad 	int ret;
    128  1.3  riastrad 
    129  1.3  riastrad 	ret = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MAX_DUTY);
    130  1.3  riastrad 	if (ret < 0)
    131  1.3  riastrad 		return ret;
    132  1.1  riastrad 
    133  1.3  riastrad 	return sprintf(buf, "%i\n", ret);
    134  1.1  riastrad }
    135  1.3  riastrad 
    136  1.1  riastrad static ssize_t
    137  1.3  riastrad nouveau_hwmon_get_pwm1_min(struct device *d,
    138  1.3  riastrad 			   struct device_attribute *a, char *buf)
    139  1.1  riastrad {
    140  1.1  riastrad 	struct drm_device *dev = dev_get_drvdata(d);
    141  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    142  1.3  riastrad 	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
    143  1.3  riastrad 	int ret;
    144  1.1  riastrad 
    145  1.3  riastrad 	ret = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MIN_DUTY);
    146  1.3  riastrad 	if (ret < 0)
    147  1.3  riastrad 		return ret;
    148  1.1  riastrad 
    149  1.3  riastrad 	return sprintf(buf, "%i\n", ret);
    150  1.1  riastrad }
    151  1.1  riastrad 
    152  1.1  riastrad static ssize_t
    153  1.3  riastrad nouveau_hwmon_set_pwm1_min(struct device *d, struct device_attribute *a,
    154  1.3  riastrad 			   const char *buf, size_t count)
    155  1.1  riastrad {
    156  1.1  riastrad 	struct drm_device *dev = dev_get_drvdata(d);
    157  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    158  1.3  riastrad 	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
    159  1.1  riastrad 	long value;
    160  1.3  riastrad 	int ret;
    161  1.1  riastrad 
    162  1.3  riastrad 	if (kstrtol(buf, 10, &value))
    163  1.3  riastrad 		return -EINVAL;
    164  1.1  riastrad 
    165  1.3  riastrad 	ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MIN_DUTY, value);
    166  1.3  riastrad 	if (ret < 0)
    167  1.3  riastrad 		return ret;
    168  1.1  riastrad 
    169  1.1  riastrad 	return count;
    170  1.1  riastrad }
    171  1.3  riastrad static SENSOR_DEVICE_ATTR(pwm1_min, 0644,
    172  1.3  riastrad 			  nouveau_hwmon_get_pwm1_min,
    173  1.3  riastrad 			  nouveau_hwmon_set_pwm1_min, 0);
    174  1.1  riastrad 
    175  1.1  riastrad static ssize_t
    176  1.3  riastrad nouveau_hwmon_set_pwm1_max(struct device *d, struct device_attribute *a,
    177  1.3  riastrad 			   const char *buf, size_t count)
    178  1.1  riastrad {
    179  1.1  riastrad 	struct drm_device *dev = dev_get_drvdata(d);
    180  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    181  1.3  riastrad 	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
    182  1.1  riastrad 	long value;
    183  1.3  riastrad 	int ret;
    184  1.1  riastrad 
    185  1.3  riastrad 	if (kstrtol(buf, 10, &value))
    186  1.3  riastrad 		return -EINVAL;
    187  1.1  riastrad 
    188  1.3  riastrad 	ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MAX_DUTY, value);
    189  1.3  riastrad 	if (ret < 0)
    190  1.3  riastrad 		return ret;
    191  1.1  riastrad 
    192  1.1  riastrad 	return count;
    193  1.1  riastrad }
    194  1.3  riastrad static SENSOR_DEVICE_ATTR(pwm1_max, 0644,
    195  1.3  riastrad 			  nouveau_hwmon_get_pwm1_max,
    196  1.3  riastrad 			  nouveau_hwmon_set_pwm1_max, 0);
    197  1.3  riastrad 
    198  1.3  riastrad static struct attribute *pwm_fan_sensor_attrs[] = {
    199  1.3  riastrad 	&sensor_dev_attr_pwm1_min.dev_attr.attr,
    200  1.3  riastrad 	&sensor_dev_attr_pwm1_max.dev_attr.attr,
    201  1.3  riastrad 	NULL
    202  1.3  riastrad };
    203  1.3  riastrad static const struct attribute_group pwm_fan_sensor_group = {
    204  1.3  riastrad 	.attrs = pwm_fan_sensor_attrs,
    205  1.3  riastrad };
    206  1.1  riastrad 
    207  1.3  riastrad static struct attribute *temp1_auto_point_sensor_attrs[] = {
    208  1.3  riastrad 	&sensor_dev_attr_temp1_auto_point1_pwm.dev_attr.attr,
    209  1.3  riastrad 	&sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
    210  1.3  riastrad 	&sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr.attr,
    211  1.3  riastrad 	NULL
    212  1.3  riastrad };
    213  1.3  riastrad static const struct attribute_group temp1_auto_point_sensor_group = {
    214  1.3  riastrad 	.attrs = temp1_auto_point_sensor_attrs,
    215  1.3  riastrad };
    216  1.3  riastrad 
    217  1.3  riastrad #define N_ATTR_GROUPS   3
    218  1.3  riastrad 
    219  1.3  riastrad static const u32 nouveau_config_chip[] = {
    220  1.3  riastrad 	HWMON_C_UPDATE_INTERVAL,
    221  1.3  riastrad 	0
    222  1.3  riastrad };
    223  1.1  riastrad 
    224  1.3  riastrad static const u32 nouveau_config_in[] = {
    225  1.3  riastrad 	HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_LABEL,
    226  1.3  riastrad 	0
    227  1.3  riastrad };
    228  1.1  riastrad 
    229  1.3  riastrad static const u32 nouveau_config_temp[] = {
    230  1.3  riastrad 	HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST |
    231  1.3  riastrad 	HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_EMERGENCY |
    232  1.3  riastrad 	HWMON_T_EMERGENCY_HYST,
    233  1.3  riastrad 	0
    234  1.3  riastrad };
    235  1.1  riastrad 
    236  1.3  riastrad static const u32 nouveau_config_fan[] = {
    237  1.3  riastrad 	HWMON_F_INPUT,
    238  1.3  riastrad 	0
    239  1.3  riastrad };
    240  1.1  riastrad 
    241  1.3  riastrad static const u32 nouveau_config_pwm[] = {
    242  1.3  riastrad 	HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
    243  1.3  riastrad 	0
    244  1.3  riastrad };
    245  1.1  riastrad 
    246  1.3  riastrad static const u32 nouveau_config_power[] = {
    247  1.3  riastrad 	HWMON_P_INPUT | HWMON_P_CAP_MAX | HWMON_P_CRIT,
    248  1.3  riastrad 	0
    249  1.3  riastrad };
    250  1.1  riastrad 
    251  1.3  riastrad static const struct hwmon_channel_info nouveau_chip = {
    252  1.3  riastrad 	.type = hwmon_chip,
    253  1.3  riastrad 	.config = nouveau_config_chip,
    254  1.3  riastrad };
    255  1.1  riastrad 
    256  1.3  riastrad static const struct hwmon_channel_info nouveau_temp = {
    257  1.3  riastrad 	.type = hwmon_temp,
    258  1.3  riastrad 	.config = nouveau_config_temp,
    259  1.3  riastrad };
    260  1.1  riastrad 
    261  1.3  riastrad static const struct hwmon_channel_info nouveau_fan = {
    262  1.3  riastrad 	.type = hwmon_fan,
    263  1.3  riastrad 	.config = nouveau_config_fan,
    264  1.3  riastrad };
    265  1.1  riastrad 
    266  1.3  riastrad static const struct hwmon_channel_info nouveau_in = {
    267  1.3  riastrad 	.type = hwmon_in,
    268  1.3  riastrad 	.config = nouveau_config_in,
    269  1.3  riastrad };
    270  1.1  riastrad 
    271  1.3  riastrad static const struct hwmon_channel_info nouveau_pwm = {
    272  1.3  riastrad 	.type = hwmon_pwm,
    273  1.3  riastrad 	.config = nouveau_config_pwm,
    274  1.3  riastrad };
    275  1.1  riastrad 
    276  1.3  riastrad static const struct hwmon_channel_info nouveau_power = {
    277  1.3  riastrad 	.type = hwmon_power,
    278  1.3  riastrad 	.config = nouveau_config_power,
    279  1.3  riastrad };
    280  1.1  riastrad 
    281  1.3  riastrad static const struct hwmon_channel_info *nouveau_info[] = {
    282  1.3  riastrad 	&nouveau_chip,
    283  1.3  riastrad 	&nouveau_temp,
    284  1.3  riastrad 	&nouveau_fan,
    285  1.3  riastrad 	&nouveau_in,
    286  1.3  riastrad 	&nouveau_pwm,
    287  1.3  riastrad 	&nouveau_power,
    288  1.3  riastrad 	NULL
    289  1.3  riastrad };
    290  1.1  riastrad 
    291  1.3  riastrad static umode_t
    292  1.3  riastrad nouveau_chip_is_visible(const void *data, u32 attr, int channel)
    293  1.3  riastrad {
    294  1.3  riastrad 	switch (attr) {
    295  1.3  riastrad 	case hwmon_chip_update_interval:
    296  1.3  riastrad 		return 0444;
    297  1.3  riastrad 	default:
    298  1.3  riastrad 		return 0;
    299  1.3  riastrad 	}
    300  1.1  riastrad }
    301  1.1  riastrad 
    302  1.3  riastrad static umode_t
    303  1.3  riastrad nouveau_power_is_visible(const void *data, u32 attr, int channel)
    304  1.1  riastrad {
    305  1.3  riastrad 	struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
    306  1.3  riastrad 	struct nvkm_iccsense *iccsense = nvxx_iccsense(&drm->client.device);
    307  1.3  riastrad 
    308  1.3  riastrad 	if (!iccsense || !iccsense->data_valid || list_empty(&iccsense->rails))
    309  1.3  riastrad 		return 0;
    310  1.3  riastrad 
    311  1.3  riastrad 	switch (attr) {
    312  1.3  riastrad 	case hwmon_power_input:
    313  1.3  riastrad 		return 0444;
    314  1.3  riastrad 	case hwmon_power_max:
    315  1.3  riastrad 		if (iccsense->power_w_max)
    316  1.3  riastrad 			return 0444;
    317  1.3  riastrad 		return 0;
    318  1.3  riastrad 	case hwmon_power_crit:
    319  1.3  riastrad 		if (iccsense->power_w_crit)
    320  1.3  riastrad 			return 0444;
    321  1.3  riastrad 		return 0;
    322  1.3  riastrad 	default:
    323  1.3  riastrad 		return 0;
    324  1.3  riastrad 	}
    325  1.1  riastrad }
    326  1.1  riastrad 
    327  1.3  riastrad static umode_t
    328  1.3  riastrad nouveau_temp_is_visible(const void *data, u32 attr, int channel)
    329  1.1  riastrad {
    330  1.3  riastrad 	struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
    331  1.3  riastrad 	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
    332  1.3  riastrad 
    333  1.3  riastrad 	if (!therm || !therm->attr_get || nvkm_therm_temp_get(therm) < 0)
    334  1.3  riastrad 		return 0;
    335  1.3  riastrad 
    336  1.3  riastrad 	switch (attr) {
    337  1.3  riastrad 	case hwmon_temp_input:
    338  1.3  riastrad 		return 0444;
    339  1.3  riastrad 	case hwmon_temp_max:
    340  1.3  riastrad 	case hwmon_temp_max_hyst:
    341  1.3  riastrad 	case hwmon_temp_crit:
    342  1.3  riastrad 	case hwmon_temp_crit_hyst:
    343  1.3  riastrad 	case hwmon_temp_emergency:
    344  1.3  riastrad 	case hwmon_temp_emergency_hyst:
    345  1.3  riastrad 		return 0644;
    346  1.3  riastrad 	default:
    347  1.3  riastrad 		return 0;
    348  1.3  riastrad 	}
    349  1.1  riastrad }
    350  1.1  riastrad 
    351  1.3  riastrad static umode_t
    352  1.3  riastrad nouveau_pwm_is_visible(const void *data, u32 attr, int channel)
    353  1.1  riastrad {
    354  1.3  riastrad 	struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
    355  1.3  riastrad 	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
    356  1.1  riastrad 
    357  1.3  riastrad 	if (!therm || !therm->attr_get || !therm->fan_get ||
    358  1.3  riastrad 	    therm->fan_get(therm) < 0)
    359  1.3  riastrad 		return 0;
    360  1.3  riastrad 
    361  1.3  riastrad 	switch (attr) {
    362  1.3  riastrad 	case hwmon_pwm_enable:
    363  1.3  riastrad 	case hwmon_pwm_input:
    364  1.3  riastrad 		return 0644;
    365  1.3  riastrad 	default:
    366  1.3  riastrad 		return 0;
    367  1.3  riastrad 	}
    368  1.1  riastrad }
    369  1.1  riastrad 
    370  1.3  riastrad static umode_t
    371  1.3  riastrad nouveau_input_is_visible(const void *data, u32 attr, int channel)
    372  1.1  riastrad {
    373  1.3  riastrad 	struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
    374  1.3  riastrad 	struct nvkm_volt *volt = nvxx_volt(&drm->client.device);
    375  1.1  riastrad 
    376  1.3  riastrad 	if (!volt || nvkm_volt_get(volt) < 0)
    377  1.3  riastrad 		return 0;
    378  1.1  riastrad 
    379  1.3  riastrad 	switch (attr) {
    380  1.3  riastrad 	case hwmon_in_input:
    381  1.3  riastrad 	case hwmon_in_label:
    382  1.3  riastrad 	case hwmon_in_min:
    383  1.3  riastrad 	case hwmon_in_max:
    384  1.3  riastrad 		return 0444;
    385  1.3  riastrad 	default:
    386  1.3  riastrad 		return 0;
    387  1.3  riastrad 	}
    388  1.1  riastrad }
    389  1.1  riastrad 
    390  1.3  riastrad static umode_t
    391  1.3  riastrad nouveau_fan_is_visible(const void *data, u32 attr, int channel)
    392  1.1  riastrad {
    393  1.3  riastrad 	struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
    394  1.3  riastrad 	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
    395  1.1  riastrad 
    396  1.3  riastrad 	if (!therm || !therm->attr_get || nvkm_therm_fan_sense(therm) < 0)
    397  1.3  riastrad 		return 0;
    398  1.1  riastrad 
    399  1.3  riastrad 	switch (attr) {
    400  1.3  riastrad 	case hwmon_fan_input:
    401  1.3  riastrad 		return 0444;
    402  1.3  riastrad 	default:
    403  1.3  riastrad 		return 0;
    404  1.3  riastrad 	}
    405  1.1  riastrad }
    406  1.1  riastrad 
    407  1.3  riastrad static int
    408  1.3  riastrad nouveau_chip_read(struct device *dev, u32 attr, int channel, long *val)
    409  1.1  riastrad {
    410  1.3  riastrad 	switch (attr) {
    411  1.3  riastrad 	case hwmon_chip_update_interval:
    412  1.3  riastrad 		*val = 1000;
    413  1.3  riastrad 		break;
    414  1.3  riastrad 	default:
    415  1.3  riastrad 		return -EOPNOTSUPP;
    416  1.3  riastrad 	}
    417  1.1  riastrad 
    418  1.3  riastrad 	return 0;
    419  1.1  riastrad }
    420  1.1  riastrad 
    421  1.3  riastrad static int
    422  1.3  riastrad nouveau_temp_read(struct device *dev, u32 attr, int channel, long *val)
    423  1.1  riastrad {
    424  1.3  riastrad 	struct drm_device *drm_dev = dev_get_drvdata(dev);
    425  1.3  riastrad 	struct nouveau_drm *drm = nouveau_drm(drm_dev);
    426  1.3  riastrad 	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
    427  1.3  riastrad 	int ret;
    428  1.1  riastrad 
    429  1.3  riastrad 	if (!therm || !therm->attr_get)
    430  1.3  riastrad 		return -EOPNOTSUPP;
    431  1.1  riastrad 
    432  1.3  riastrad 	switch (attr) {
    433  1.3  riastrad 	case hwmon_temp_input:
    434  1.3  riastrad 		if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
    435  1.3  riastrad 			return -EINVAL;
    436  1.3  riastrad 		ret = nvkm_therm_temp_get(therm);
    437  1.3  riastrad 		*val = ret < 0 ? ret : (ret * 1000);
    438  1.3  riastrad 		break;
    439  1.3  riastrad 	case hwmon_temp_max:
    440  1.3  riastrad 		*val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK)
    441  1.3  riastrad 					* 1000;
    442  1.3  riastrad 		break;
    443  1.3  riastrad 	case hwmon_temp_max_hyst:
    444  1.3  riastrad 		*val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST)
    445  1.3  riastrad 					* 1000;
    446  1.3  riastrad 		break;
    447  1.3  riastrad 	case hwmon_temp_crit:
    448  1.3  riastrad 		*val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_CRITICAL)
    449  1.3  riastrad 					* 1000;
    450  1.3  riastrad 		break;
    451  1.3  riastrad 	case hwmon_temp_crit_hyst:
    452  1.3  riastrad 		*val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_CRITICAL_HYST)
    453  1.3  riastrad 					* 1000;
    454  1.3  riastrad 		break;
    455  1.3  riastrad 	case hwmon_temp_emergency:
    456  1.3  riastrad 		*val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN)
    457  1.3  riastrad 					* 1000;
    458  1.3  riastrad 		break;
    459  1.3  riastrad 	case hwmon_temp_emergency_hyst:
    460  1.3  riastrad 		*val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST)
    461  1.3  riastrad 					* 1000;
    462  1.3  riastrad 		break;
    463  1.3  riastrad 	default:
    464  1.3  riastrad 		return -EOPNOTSUPP;
    465  1.3  riastrad 	}
    466  1.1  riastrad 
    467  1.3  riastrad 	return 0;
    468  1.1  riastrad }
    469  1.1  riastrad 
    470  1.3  riastrad static int
    471  1.3  riastrad nouveau_fan_read(struct device *dev, u32 attr, int channel, long *val)
    472  1.3  riastrad {
    473  1.3  riastrad 	struct drm_device *drm_dev = dev_get_drvdata(dev);
    474  1.3  riastrad 	struct nouveau_drm *drm = nouveau_drm(drm_dev);
    475  1.3  riastrad 	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
    476  1.3  riastrad 
    477  1.3  riastrad 	if (!therm)
    478  1.3  riastrad 		return -EOPNOTSUPP;
    479  1.3  riastrad 
    480  1.3  riastrad 	switch (attr) {
    481  1.3  riastrad 	case hwmon_fan_input:
    482  1.3  riastrad 		if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
    483  1.3  riastrad 			return -EINVAL;
    484  1.3  riastrad 		*val = nvkm_therm_fan_sense(therm);
    485  1.3  riastrad 		break;
    486  1.3  riastrad 	default:
    487  1.3  riastrad 		return -EOPNOTSUPP;
    488  1.3  riastrad 	}
    489  1.3  riastrad 
    490  1.3  riastrad 	return 0;
    491  1.3  riastrad }
    492  1.1  riastrad 
    493  1.3  riastrad static int
    494  1.3  riastrad nouveau_in_read(struct device *dev, u32 attr, int channel, long *val)
    495  1.1  riastrad {
    496  1.3  riastrad 	struct drm_device *drm_dev = dev_get_drvdata(dev);
    497  1.3  riastrad 	struct nouveau_drm *drm = nouveau_drm(drm_dev);
    498  1.3  riastrad 	struct nvkm_volt *volt = nvxx_volt(&drm->client.device);
    499  1.1  riastrad 	int ret;
    500  1.1  riastrad 
    501  1.3  riastrad 	if (!volt)
    502  1.3  riastrad 		return -EOPNOTSUPP;
    503  1.3  riastrad 
    504  1.3  riastrad 	switch (attr) {
    505  1.3  riastrad 	case hwmon_in_input:
    506  1.3  riastrad 		if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
    507  1.3  riastrad 			return -EINVAL;
    508  1.3  riastrad 		ret = nvkm_volt_get(volt);
    509  1.3  riastrad 		*val = ret < 0 ? ret : (ret / 1000);
    510  1.3  riastrad 		break;
    511  1.3  riastrad 	case hwmon_in_min:
    512  1.3  riastrad 		*val = volt->min_uv > 0 ? (volt->min_uv / 1000) : -ENODEV;
    513  1.3  riastrad 		break;
    514  1.3  riastrad 	case hwmon_in_max:
    515  1.3  riastrad 		*val = volt->max_uv > 0 ? (volt->max_uv / 1000) : -ENODEV;
    516  1.3  riastrad 		break;
    517  1.3  riastrad 	default:
    518  1.3  riastrad 		return -EOPNOTSUPP;
    519  1.3  riastrad 	}
    520  1.1  riastrad 
    521  1.3  riastrad 	return 0;
    522  1.1  riastrad }
    523  1.1  riastrad 
    524  1.3  riastrad static int
    525  1.3  riastrad nouveau_pwm_read(struct device *dev, u32 attr, int channel, long *val)
    526  1.1  riastrad {
    527  1.3  riastrad 	struct drm_device *drm_dev = dev_get_drvdata(dev);
    528  1.3  riastrad 	struct nouveau_drm *drm = nouveau_drm(drm_dev);
    529  1.3  riastrad 	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
    530  1.3  riastrad 
    531  1.3  riastrad 	if (!therm || !therm->attr_get || !therm->fan_get)
    532  1.3  riastrad 		return -EOPNOTSUPP;
    533  1.3  riastrad 
    534  1.3  riastrad 	switch (attr) {
    535  1.3  riastrad 	case hwmon_pwm_enable:
    536  1.3  riastrad 		*val = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MODE);
    537  1.3  riastrad 		break;
    538  1.3  riastrad 	case hwmon_pwm_input:
    539  1.3  riastrad 		if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
    540  1.3  riastrad 			return -EINVAL;
    541  1.3  riastrad 		*val = therm->fan_get(therm);
    542  1.3  riastrad 		break;
    543  1.3  riastrad 	default:
    544  1.3  riastrad 		return -EOPNOTSUPP;
    545  1.3  riastrad 	}
    546  1.1  riastrad 
    547  1.3  riastrad 	return 0;
    548  1.3  riastrad }
    549  1.1  riastrad 
    550  1.3  riastrad static int
    551  1.3  riastrad nouveau_power_read(struct device *dev, u32 attr, int channel, long *val)
    552  1.3  riastrad {
    553  1.3  riastrad 	struct drm_device *drm_dev = dev_get_drvdata(dev);
    554  1.3  riastrad 	struct nouveau_drm *drm = nouveau_drm(drm_dev);
    555  1.3  riastrad 	struct nvkm_iccsense *iccsense = nvxx_iccsense(&drm->client.device);
    556  1.3  riastrad 
    557  1.3  riastrad 	if (!iccsense)
    558  1.3  riastrad 		return -EOPNOTSUPP;
    559  1.3  riastrad 
    560  1.3  riastrad 	switch (attr) {
    561  1.3  riastrad 	case hwmon_power_input:
    562  1.3  riastrad 		if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
    563  1.3  riastrad 			return -EINVAL;
    564  1.3  riastrad 		*val = nvkm_iccsense_read_all(iccsense);
    565  1.3  riastrad 		break;
    566  1.3  riastrad 	case hwmon_power_max:
    567  1.3  riastrad 		*val = iccsense->power_w_max;
    568  1.3  riastrad 		break;
    569  1.3  riastrad 	case hwmon_power_crit:
    570  1.3  riastrad 		*val = iccsense->power_w_crit;
    571  1.3  riastrad 		break;
    572  1.3  riastrad 	default:
    573  1.3  riastrad 		return -EOPNOTSUPP;
    574  1.3  riastrad 	}
    575  1.1  riastrad 
    576  1.3  riastrad 	return 0;
    577  1.1  riastrad }
    578  1.1  riastrad 
    579  1.3  riastrad static int
    580  1.3  riastrad nouveau_temp_write(struct device *dev, u32 attr, int channel, long val)
    581  1.3  riastrad {
    582  1.3  riastrad 	struct drm_device *drm_dev = dev_get_drvdata(dev);
    583  1.3  riastrad 	struct nouveau_drm *drm = nouveau_drm(drm_dev);
    584  1.3  riastrad 	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
    585  1.3  riastrad 
    586  1.3  riastrad 	if (!therm || !therm->attr_set)
    587  1.3  riastrad 		return -EOPNOTSUPP;
    588  1.3  riastrad 
    589  1.3  riastrad 	switch (attr) {
    590  1.3  riastrad 	case hwmon_temp_max:
    591  1.3  riastrad 		return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK,
    592  1.3  riastrad 					val / 1000);
    593  1.3  riastrad 	case hwmon_temp_max_hyst:
    594  1.3  riastrad 		return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST,
    595  1.3  riastrad 					val / 1000);
    596  1.3  riastrad 	case hwmon_temp_crit:
    597  1.3  riastrad 		return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_CRITICAL,
    598  1.3  riastrad 					val / 1000);
    599  1.3  riastrad 	case hwmon_temp_crit_hyst:
    600  1.3  riastrad 		return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_CRITICAL_HYST,
    601  1.3  riastrad 					val / 1000);
    602  1.3  riastrad 	case hwmon_temp_emergency:
    603  1.3  riastrad 		return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN,
    604  1.3  riastrad 					val / 1000);
    605  1.3  riastrad 	case hwmon_temp_emergency_hyst:
    606  1.3  riastrad 		return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST,
    607  1.3  riastrad 					val / 1000);
    608  1.3  riastrad 	default:
    609  1.3  riastrad 		return -EOPNOTSUPP;
    610  1.3  riastrad 	}
    611  1.3  riastrad }
    612  1.1  riastrad 
    613  1.3  riastrad static int
    614  1.3  riastrad nouveau_pwm_write(struct device *dev, u32 attr, int channel, long val)
    615  1.1  riastrad {
    616  1.3  riastrad 	struct drm_device *drm_dev = dev_get_drvdata(dev);
    617  1.3  riastrad 	struct nouveau_drm *drm = nouveau_drm(drm_dev);
    618  1.3  riastrad 	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
    619  1.3  riastrad 
    620  1.3  riastrad 	if (!therm || !therm->attr_set)
    621  1.3  riastrad 		return -EOPNOTSUPP;
    622  1.3  riastrad 
    623  1.3  riastrad 	switch (attr) {
    624  1.3  riastrad 	case hwmon_pwm_input:
    625  1.3  riastrad 		return therm->fan_set(therm, val);
    626  1.3  riastrad 	case hwmon_pwm_enable:
    627  1.3  riastrad 		return therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MODE, val);
    628  1.3  riastrad 	default:
    629  1.3  riastrad 		return -EOPNOTSUPP;
    630  1.3  riastrad 	}
    631  1.3  riastrad }
    632  1.1  riastrad 
    633  1.3  riastrad static umode_t
    634  1.3  riastrad nouveau_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr,
    635  1.3  riastrad 			int channel)
    636  1.3  riastrad {
    637  1.3  riastrad 	switch (type) {
    638  1.3  riastrad 	case hwmon_chip:
    639  1.3  riastrad 		return nouveau_chip_is_visible(data, attr, channel);
    640  1.3  riastrad 	case hwmon_temp:
    641  1.3  riastrad 		return nouveau_temp_is_visible(data, attr, channel);
    642  1.3  riastrad 	case hwmon_fan:
    643  1.3  riastrad 		return nouveau_fan_is_visible(data, attr, channel);
    644  1.3  riastrad 	case hwmon_in:
    645  1.3  riastrad 		return nouveau_input_is_visible(data, attr, channel);
    646  1.3  riastrad 	case hwmon_pwm:
    647  1.3  riastrad 		return nouveau_pwm_is_visible(data, attr, channel);
    648  1.3  riastrad 	case hwmon_power:
    649  1.3  riastrad 		return nouveau_power_is_visible(data, attr, channel);
    650  1.3  riastrad 	default:
    651  1.3  riastrad 		return 0;
    652  1.3  riastrad 	}
    653  1.1  riastrad }
    654  1.1  riastrad 
    655  1.3  riastrad static const char input_label[] = "GPU core";
    656  1.1  riastrad 
    657  1.3  riastrad static int
    658  1.3  riastrad nouveau_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr,
    659  1.3  riastrad 		    int channel, const char **buf)
    660  1.3  riastrad {
    661  1.3  riastrad 	if (type == hwmon_in && attr == hwmon_in_label) {
    662  1.3  riastrad 		*buf = input_label;
    663  1.3  riastrad 		return 0;
    664  1.3  riastrad 	}
    665  1.1  riastrad 
    666  1.3  riastrad 	return -EOPNOTSUPP;
    667  1.3  riastrad }
    668  1.1  riastrad 
    669  1.3  riastrad static int
    670  1.3  riastrad nouveau_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
    671  1.3  riastrad 							int channel, long *val)
    672  1.3  riastrad {
    673  1.3  riastrad 	switch (type) {
    674  1.3  riastrad 	case hwmon_chip:
    675  1.3  riastrad 		return nouveau_chip_read(dev, attr, channel, val);
    676  1.3  riastrad 	case hwmon_temp:
    677  1.3  riastrad 		return nouveau_temp_read(dev, attr, channel, val);
    678  1.3  riastrad 	case hwmon_fan:
    679  1.3  riastrad 		return nouveau_fan_read(dev, attr, channel, val);
    680  1.3  riastrad 	case hwmon_in:
    681  1.3  riastrad 		return nouveau_in_read(dev, attr, channel, val);
    682  1.3  riastrad 	case hwmon_pwm:
    683  1.3  riastrad 		return nouveau_pwm_read(dev, attr, channel, val);
    684  1.3  riastrad 	case hwmon_power:
    685  1.3  riastrad 		return nouveau_power_read(dev, attr, channel, val);
    686  1.3  riastrad 	default:
    687  1.3  riastrad 		return -EOPNOTSUPP;
    688  1.3  riastrad 	}
    689  1.1  riastrad }
    690  1.1  riastrad 
    691  1.3  riastrad static int
    692  1.3  riastrad nouveau_write(struct device *dev, enum hwmon_sensor_types type, u32 attr,
    693  1.3  riastrad 							int channel, long val)
    694  1.3  riastrad {
    695  1.3  riastrad 	switch (type) {
    696  1.3  riastrad 	case hwmon_temp:
    697  1.3  riastrad 		return nouveau_temp_write(dev, attr, channel, val);
    698  1.3  riastrad 	case hwmon_pwm:
    699  1.3  riastrad 		return nouveau_pwm_write(dev, attr, channel, val);
    700  1.3  riastrad 	default:
    701  1.3  riastrad 		return -EOPNOTSUPP;
    702  1.3  riastrad 	}
    703  1.3  riastrad }
    704  1.1  riastrad 
    705  1.3  riastrad static const struct hwmon_ops nouveau_hwmon_ops = {
    706  1.3  riastrad 	.is_visible = nouveau_is_visible,
    707  1.3  riastrad 	.read = nouveau_read,
    708  1.3  riastrad 	.read_string = nouveau_read_string,
    709  1.3  riastrad 	.write = nouveau_write,
    710  1.1  riastrad };
    711  1.1  riastrad 
    712  1.3  riastrad static const struct hwmon_chip_info nouveau_chip_info = {
    713  1.3  riastrad 	.ops = &nouveau_hwmon_ops,
    714  1.3  riastrad 	.info = nouveau_info,
    715  1.1  riastrad };
    716  1.1  riastrad #endif
    717  1.1  riastrad 
    718  1.1  riastrad int
    719  1.1  riastrad nouveau_hwmon_init(struct drm_device *dev)
    720  1.1  riastrad {
    721  1.1  riastrad #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
    722  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    723  1.3  riastrad 	struct nvkm_iccsense *iccsense = nvxx_iccsense(&drm->client.device);
    724  1.3  riastrad 	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
    725  1.3  riastrad 	struct nvkm_volt *volt = nvxx_volt(&drm->client.device);
    726  1.3  riastrad 	const struct attribute_group *special_groups[N_ATTR_GROUPS];
    727  1.1  riastrad 	struct nouveau_hwmon *hwmon;
    728  1.1  riastrad 	struct device *hwmon_dev;
    729  1.1  riastrad 	int ret = 0;
    730  1.3  riastrad 	int i = 0;
    731  1.3  riastrad 
    732  1.3  riastrad 	if (!iccsense && !therm && !volt) {
    733  1.3  riastrad 		NV_DEBUG(drm, "Skipping hwmon registration\n");
    734  1.3  riastrad 		return 0;
    735  1.3  riastrad 	}
    736  1.1  riastrad 
    737  1.1  riastrad 	hwmon = drm->hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
    738  1.1  riastrad 	if (!hwmon)
    739  1.1  riastrad 		return -ENOMEM;
    740  1.1  riastrad 	hwmon->dev = dev;
    741  1.1  riastrad 
    742  1.3  riastrad 	if (therm && therm->attr_get && therm->attr_set) {
    743  1.3  riastrad 		if (nvkm_therm_temp_get(therm) >= 0)
    744  1.3  riastrad 			special_groups[i++] = &temp1_auto_point_sensor_group;
    745  1.3  riastrad 		if (therm->fan_get && therm->fan_get(therm) >= 0)
    746  1.3  riastrad 			special_groups[i++] = &pwm_fan_sensor_group;
    747  1.3  riastrad 	}
    748  1.1  riastrad 
    749  1.3  riastrad 	special_groups[i] = NULL;
    750  1.3  riastrad 	hwmon_dev = hwmon_device_register_with_info(dev->dev, "nouveau", dev,
    751  1.3  riastrad 							&nouveau_chip_info,
    752  1.3  riastrad 							special_groups);
    753  1.1  riastrad 	if (IS_ERR(hwmon_dev)) {
    754  1.1  riastrad 		ret = PTR_ERR(hwmon_dev);
    755  1.1  riastrad 		NV_ERROR(drm, "Unable to register hwmon device: %d\n", ret);
    756  1.1  riastrad 		return ret;
    757  1.1  riastrad 	}
    758  1.1  riastrad 
    759  1.1  riastrad 	hwmon->hwmon = hwmon_dev;
    760  1.1  riastrad 	return 0;
    761  1.1  riastrad #else
    762  1.1  riastrad 	return 0;
    763  1.1  riastrad #endif
    764  1.1  riastrad }
    765  1.1  riastrad 
    766  1.1  riastrad void
    767  1.1  riastrad nouveau_hwmon_fini(struct drm_device *dev)
    768  1.1  riastrad {
    769  1.1  riastrad #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
    770  1.1  riastrad 	struct nouveau_hwmon *hwmon = nouveau_hwmon(dev);
    771  1.1  riastrad 
    772  1.3  riastrad 	if (!hwmon)
    773  1.3  riastrad 		return;
    774  1.1  riastrad 
    775  1.3  riastrad 	if (hwmon->hwmon)
    776  1.1  riastrad 		hwmon_device_unregister(hwmon->hwmon);
    777  1.1  riastrad 
    778  1.1  riastrad 	nouveau_drm(dev)->hwmon = NULL;
    779  1.1  riastrad 	kfree(hwmon);
    780  1.1  riastrad #endif
    781  1.1  riastrad }
    782