2 * Copyright 2010 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
26 #include <linux/acpi.h>
28 #include <linux/power_supply.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-sysfs.h>
32 #include "nouveau_drv.h"
33 #include "nouveau_hwmon.h"
35 #include <nvkm/subdev/iccsense.h>
36 #include <nvkm/subdev/volt.h>
38 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
41 nouveau_hwmon_show_temp1_auto_point1_pwm(struct device
*d
,
42 struct device_attribute
*a
, char *buf
)
44 return snprintf(buf
, PAGE_SIZE
, "%d\n", 100);
46 static SENSOR_DEVICE_ATTR(temp1_auto_point1_pwm
, 0444,
47 nouveau_hwmon_show_temp1_auto_point1_pwm
, NULL
, 0);
50 nouveau_hwmon_temp1_auto_point1_temp(struct device
*d
,
51 struct device_attribute
*a
, char *buf
)
53 struct drm_device
*dev
= dev_get_drvdata(d
);
54 struct nouveau_drm
*drm
= nouveau_drm(dev
);
55 struct nvkm_therm
*therm
= nvxx_therm(&drm
->client
.device
);
57 return snprintf(buf
, PAGE_SIZE
, "%d\n",
58 therm
->attr_get(therm
, NVKM_THERM_ATTR_THRS_FAN_BOOST
) * 1000);
61 nouveau_hwmon_set_temp1_auto_point1_temp(struct device
*d
,
62 struct device_attribute
*a
,
63 const char *buf
, size_t count
)
65 struct drm_device
*dev
= dev_get_drvdata(d
);
66 struct nouveau_drm
*drm
= nouveau_drm(dev
);
67 struct nvkm_therm
*therm
= nvxx_therm(&drm
->client
.device
);
70 if (kstrtol(buf
, 10, &value
))
73 therm
->attr_set(therm
, NVKM_THERM_ATTR_THRS_FAN_BOOST
,
78 static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp
, 0644,
79 nouveau_hwmon_temp1_auto_point1_temp
,
80 nouveau_hwmon_set_temp1_auto_point1_temp
, 0);
83 nouveau_hwmon_temp1_auto_point1_temp_hyst(struct device
*d
,
84 struct device_attribute
*a
, char *buf
)
86 struct drm_device
*dev
= dev_get_drvdata(d
);
87 struct nouveau_drm
*drm
= nouveau_drm(dev
);
88 struct nvkm_therm
*therm
= nvxx_therm(&drm
->client
.device
);
90 return snprintf(buf
, PAGE_SIZE
, "%d\n",
91 therm
->attr_get(therm
, NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST
) * 1000);
94 nouveau_hwmon_set_temp1_auto_point1_temp_hyst(struct device
*d
,
95 struct device_attribute
*a
,
96 const char *buf
, size_t count
)
98 struct drm_device
*dev
= dev_get_drvdata(d
);
99 struct nouveau_drm
*drm
= nouveau_drm(dev
);
100 struct nvkm_therm
*therm
= nvxx_therm(&drm
->client
.device
);
103 if (kstrtol(buf
, 10, &value
))
106 therm
->attr_set(therm
, NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST
,
111 static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp_hyst
, 0644,
112 nouveau_hwmon_temp1_auto_point1_temp_hyst
,
113 nouveau_hwmon_set_temp1_auto_point1_temp_hyst
, 0);
116 nouveau_hwmon_get_pwm1_max(struct device
*d
,
117 struct device_attribute
*a
, char *buf
)
119 struct drm_device
*dev
= dev_get_drvdata(d
);
120 struct nouveau_drm
*drm
= nouveau_drm(dev
);
121 struct nvkm_therm
*therm
= nvxx_therm(&drm
->client
.device
);
124 ret
= therm
->attr_get(therm
, NVKM_THERM_ATTR_FAN_MAX_DUTY
);
128 return sprintf(buf
, "%i\n", ret
);
132 nouveau_hwmon_get_pwm1_min(struct device
*d
,
133 struct device_attribute
*a
, char *buf
)
135 struct drm_device
*dev
= dev_get_drvdata(d
);
136 struct nouveau_drm
*drm
= nouveau_drm(dev
);
137 struct nvkm_therm
*therm
= nvxx_therm(&drm
->client
.device
);
140 ret
= therm
->attr_get(therm
, NVKM_THERM_ATTR_FAN_MIN_DUTY
);
144 return sprintf(buf
, "%i\n", ret
);
148 nouveau_hwmon_set_pwm1_min(struct device
*d
, struct device_attribute
*a
,
149 const char *buf
, size_t count
)
151 struct drm_device
*dev
= dev_get_drvdata(d
);
152 struct nouveau_drm
*drm
= nouveau_drm(dev
);
153 struct nvkm_therm
*therm
= nvxx_therm(&drm
->client
.device
);
157 if (kstrtol(buf
, 10, &value
))
160 ret
= therm
->attr_set(therm
, NVKM_THERM_ATTR_FAN_MIN_DUTY
, value
);
166 static SENSOR_DEVICE_ATTR(pwm1_min
, 0644,
167 nouveau_hwmon_get_pwm1_min
,
168 nouveau_hwmon_set_pwm1_min
, 0);
171 nouveau_hwmon_set_pwm1_max(struct device
*d
, struct device_attribute
*a
,
172 const char *buf
, size_t count
)
174 struct drm_device
*dev
= dev_get_drvdata(d
);
175 struct nouveau_drm
*drm
= nouveau_drm(dev
);
176 struct nvkm_therm
*therm
= nvxx_therm(&drm
->client
.device
);
180 if (kstrtol(buf
, 10, &value
))
183 ret
= therm
->attr_set(therm
, NVKM_THERM_ATTR_FAN_MAX_DUTY
, value
);
189 static SENSOR_DEVICE_ATTR(pwm1_max
, 0644,
190 nouveau_hwmon_get_pwm1_max
,
191 nouveau_hwmon_set_pwm1_max
, 0);
193 static struct attribute
*pwm_fan_sensor_attrs
[] = {
194 &sensor_dev_attr_pwm1_min
.dev_attr
.attr
,
195 &sensor_dev_attr_pwm1_max
.dev_attr
.attr
,
198 static const struct attribute_group pwm_fan_sensor_group
= {
199 .attrs
= pwm_fan_sensor_attrs
,
202 static struct attribute
*temp1_auto_point_sensor_attrs
[] = {
203 &sensor_dev_attr_temp1_auto_point1_pwm
.dev_attr
.attr
,
204 &sensor_dev_attr_temp1_auto_point1_temp
.dev_attr
.attr
,
205 &sensor_dev_attr_temp1_auto_point1_temp_hyst
.dev_attr
.attr
,
208 static const struct attribute_group temp1_auto_point_sensor_group
= {
209 .attrs
= temp1_auto_point_sensor_attrs
,
212 #define N_ATTR_GROUPS 3
214 static const u32 nouveau_config_chip
[] = {
215 HWMON_C_UPDATE_INTERVAL
,
219 static const u32 nouveau_config_in
[] = {
220 HWMON_I_INPUT
| HWMON_I_MIN
| HWMON_I_MAX
| HWMON_I_LABEL
,
224 static const u32 nouveau_config_temp
[] = {
225 HWMON_T_INPUT
| HWMON_T_MAX
| HWMON_T_MAX_HYST
|
226 HWMON_T_CRIT
| HWMON_T_CRIT_HYST
| HWMON_T_EMERGENCY
|
227 HWMON_T_EMERGENCY_HYST
,
231 static const u32 nouveau_config_fan
[] = {
236 static const u32 nouveau_config_pwm
[] = {
237 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
,
241 static const u32 nouveau_config_power
[] = {
242 HWMON_P_INPUT
| HWMON_P_CAP_MAX
| HWMON_P_CRIT
,
246 static const struct hwmon_channel_info nouveau_chip
= {
248 .config
= nouveau_config_chip
,
251 static const struct hwmon_channel_info nouveau_temp
= {
253 .config
= nouveau_config_temp
,
256 static const struct hwmon_channel_info nouveau_fan
= {
258 .config
= nouveau_config_fan
,
261 static const struct hwmon_channel_info nouveau_in
= {
263 .config
= nouveau_config_in
,
266 static const struct hwmon_channel_info nouveau_pwm
= {
268 .config
= nouveau_config_pwm
,
271 static const struct hwmon_channel_info nouveau_power
= {
273 .config
= nouveau_config_power
,
276 static const struct hwmon_channel_info
*nouveau_info
[] = {
287 nouveau_chip_is_visible(const void *data
, u32 attr
, int channel
)
290 case hwmon_chip_update_interval
:
298 nouveau_power_is_visible(const void *data
, u32 attr
, int channel
)
300 struct nouveau_drm
*drm
= nouveau_drm((struct drm_device
*)data
);
301 struct nvkm_iccsense
*iccsense
= nvxx_iccsense(&drm
->client
.device
);
303 if (!iccsense
|| !iccsense
->data_valid
|| list_empty(&iccsense
->rails
))
307 case hwmon_power_input
:
309 case hwmon_power_max
:
310 if (iccsense
->power_w_max
)
313 case hwmon_power_crit
:
314 if (iccsense
->power_w_crit
)
323 nouveau_temp_is_visible(const void *data
, u32 attr
, int channel
)
325 struct nouveau_drm
*drm
= nouveau_drm((struct drm_device
*)data
);
326 struct nvkm_therm
*therm
= nvxx_therm(&drm
->client
.device
);
328 if (!therm
|| !therm
->attr_get
|| nvkm_therm_temp_get(therm
) < 0)
332 case hwmon_temp_input
:
335 case hwmon_temp_max_hyst
:
336 case hwmon_temp_crit
:
337 case hwmon_temp_crit_hyst
:
338 case hwmon_temp_emergency
:
339 case hwmon_temp_emergency_hyst
:
347 nouveau_pwm_is_visible(const void *data
, u32 attr
, int channel
)
349 struct nouveau_drm
*drm
= nouveau_drm((struct drm_device
*)data
);
350 struct nvkm_therm
*therm
= nvxx_therm(&drm
->client
.device
);
352 if (!therm
|| !therm
->attr_get
|| !therm
->fan_get
||
353 therm
->fan_get(therm
) < 0)
357 case hwmon_pwm_enable
:
358 case hwmon_pwm_input
:
366 nouveau_input_is_visible(const void *data
, u32 attr
, int channel
)
368 struct nouveau_drm
*drm
= nouveau_drm((struct drm_device
*)data
);
369 struct nvkm_volt
*volt
= nvxx_volt(&drm
->client
.device
);
371 if (!volt
|| nvkm_volt_get(volt
) < 0)
386 nouveau_fan_is_visible(const void *data
, u32 attr
, int channel
)
388 struct nouveau_drm
*drm
= nouveau_drm((struct drm_device
*)data
);
389 struct nvkm_therm
*therm
= nvxx_therm(&drm
->client
.device
);
391 if (!therm
|| !therm
->attr_get
|| nvkm_therm_fan_sense(therm
) < 0)
395 case hwmon_fan_input
:
403 nouveau_chip_read(struct device
*dev
, u32 attr
, int channel
, long *val
)
406 case hwmon_chip_update_interval
:
417 nouveau_temp_read(struct device
*dev
, u32 attr
, int channel
, long *val
)
419 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
420 struct nouveau_drm
*drm
= nouveau_drm(drm_dev
);
421 struct nvkm_therm
*therm
= nvxx_therm(&drm
->client
.device
);
424 if (!therm
|| !therm
->attr_get
)
428 case hwmon_temp_input
:
429 if (drm_dev
->switch_power_state
!= DRM_SWITCH_POWER_ON
)
431 ret
= nvkm_therm_temp_get(therm
);
432 *val
= ret
< 0 ? ret
: (ret
* 1000);
435 *val
= therm
->attr_get(therm
, NVKM_THERM_ATTR_THRS_DOWN_CLK
)
438 case hwmon_temp_max_hyst
:
439 *val
= therm
->attr_get(therm
, NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST
)
442 case hwmon_temp_crit
:
443 *val
= therm
->attr_get(therm
, NVKM_THERM_ATTR_THRS_CRITICAL
)
446 case hwmon_temp_crit_hyst
:
447 *val
= therm
->attr_get(therm
, NVKM_THERM_ATTR_THRS_CRITICAL_HYST
)
450 case hwmon_temp_emergency
:
451 *val
= therm
->attr_get(therm
, NVKM_THERM_ATTR_THRS_SHUTDOWN
)
454 case hwmon_temp_emergency_hyst
:
455 *val
= therm
->attr_get(therm
, NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST
)
466 nouveau_fan_read(struct device
*dev
, u32 attr
, int channel
, long *val
)
468 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
469 struct nouveau_drm
*drm
= nouveau_drm(drm_dev
);
470 struct nvkm_therm
*therm
= nvxx_therm(&drm
->client
.device
);
476 case hwmon_fan_input
:
477 if (drm_dev
->switch_power_state
!= DRM_SWITCH_POWER_ON
)
479 *val
= nvkm_therm_fan_sense(therm
);
489 nouveau_in_read(struct device
*dev
, u32 attr
, int channel
, long *val
)
491 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
492 struct nouveau_drm
*drm
= nouveau_drm(drm_dev
);
493 struct nvkm_volt
*volt
= nvxx_volt(&drm
->client
.device
);
501 if (drm_dev
->switch_power_state
!= DRM_SWITCH_POWER_ON
)
503 ret
= nvkm_volt_get(volt
);
504 *val
= ret
< 0 ? ret
: (ret
/ 1000);
507 *val
= volt
->min_uv
> 0 ? (volt
->min_uv
/ 1000) : -ENODEV
;
510 *val
= volt
->max_uv
> 0 ? (volt
->max_uv
/ 1000) : -ENODEV
;
520 nouveau_pwm_read(struct device
*dev
, u32 attr
, int channel
, long *val
)
522 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
523 struct nouveau_drm
*drm
= nouveau_drm(drm_dev
);
524 struct nvkm_therm
*therm
= nvxx_therm(&drm
->client
.device
);
526 if (!therm
|| !therm
->attr_get
|| !therm
->fan_get
)
530 case hwmon_pwm_enable
:
531 *val
= therm
->attr_get(therm
, NVKM_THERM_ATTR_FAN_MODE
);
533 case hwmon_pwm_input
:
534 if (drm_dev
->switch_power_state
!= DRM_SWITCH_POWER_ON
)
536 *val
= therm
->fan_get(therm
);
546 nouveau_power_read(struct device
*dev
, u32 attr
, int channel
, long *val
)
548 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
549 struct nouveau_drm
*drm
= nouveau_drm(drm_dev
);
550 struct nvkm_iccsense
*iccsense
= nvxx_iccsense(&drm
->client
.device
);
556 case hwmon_power_input
:
557 if (drm_dev
->switch_power_state
!= DRM_SWITCH_POWER_ON
)
559 *val
= nvkm_iccsense_read_all(iccsense
);
561 case hwmon_power_max
:
562 *val
= iccsense
->power_w_max
;
564 case hwmon_power_crit
:
565 *val
= iccsense
->power_w_crit
;
575 nouveau_temp_write(struct device
*dev
, u32 attr
, int channel
, long val
)
577 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
578 struct nouveau_drm
*drm
= nouveau_drm(drm_dev
);
579 struct nvkm_therm
*therm
= nvxx_therm(&drm
->client
.device
);
581 if (!therm
|| !therm
->attr_set
)
586 return therm
->attr_set(therm
, NVKM_THERM_ATTR_THRS_DOWN_CLK
,
588 case hwmon_temp_max_hyst
:
589 return therm
->attr_set(therm
, NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST
,
591 case hwmon_temp_crit
:
592 return therm
->attr_set(therm
, NVKM_THERM_ATTR_THRS_CRITICAL
,
594 case hwmon_temp_crit_hyst
:
595 return therm
->attr_set(therm
, NVKM_THERM_ATTR_THRS_CRITICAL_HYST
,
597 case hwmon_temp_emergency
:
598 return therm
->attr_set(therm
, NVKM_THERM_ATTR_THRS_SHUTDOWN
,
600 case hwmon_temp_emergency_hyst
:
601 return therm
->attr_set(therm
, NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST
,
609 nouveau_pwm_write(struct device
*dev
, u32 attr
, int channel
, long val
)
611 struct drm_device
*drm_dev
= dev_get_drvdata(dev
);
612 struct nouveau_drm
*drm
= nouveau_drm(drm_dev
);
613 struct nvkm_therm
*therm
= nvxx_therm(&drm
->client
.device
);
615 if (!therm
|| !therm
->attr_set
)
619 case hwmon_pwm_input
:
620 return therm
->fan_set(therm
, val
);
621 case hwmon_pwm_enable
:
622 return therm
->attr_set(therm
, NVKM_THERM_ATTR_FAN_MODE
, val
);
629 nouveau_is_visible(const void *data
, enum hwmon_sensor_types type
, u32 attr
,
634 return nouveau_chip_is_visible(data
, attr
, channel
);
636 return nouveau_temp_is_visible(data
, attr
, channel
);
638 return nouveau_fan_is_visible(data
, attr
, channel
);
640 return nouveau_input_is_visible(data
, attr
, channel
);
642 return nouveau_pwm_is_visible(data
, attr
, channel
);
644 return nouveau_power_is_visible(data
, attr
, channel
);
650 static const char input_label
[] = "GPU core";
653 nouveau_read_string(struct device
*dev
, enum hwmon_sensor_types type
, u32 attr
,
654 int channel
, const char **buf
)
656 if (type
== hwmon_in
&& attr
== hwmon_in_label
) {
665 nouveau_read(struct device
*dev
, enum hwmon_sensor_types type
, u32 attr
,
666 int channel
, long *val
)
670 return nouveau_chip_read(dev
, attr
, channel
, val
);
672 return nouveau_temp_read(dev
, attr
, channel
, val
);
674 return nouveau_fan_read(dev
, attr
, channel
, val
);
676 return nouveau_in_read(dev
, attr
, channel
, val
);
678 return nouveau_pwm_read(dev
, attr
, channel
, val
);
680 return nouveau_power_read(dev
, attr
, channel
, val
);
687 nouveau_write(struct device
*dev
, enum hwmon_sensor_types type
, u32 attr
,
688 int channel
, long val
)
692 return nouveau_temp_write(dev
, attr
, channel
, val
);
694 return nouveau_pwm_write(dev
, attr
, channel
, val
);
700 static const struct hwmon_ops nouveau_hwmon_ops
= {
701 .is_visible
= nouveau_is_visible
,
702 .read
= nouveau_read
,
703 .read_string
= nouveau_read_string
,
704 .write
= nouveau_write
,
707 static const struct hwmon_chip_info nouveau_chip_info
= {
708 .ops
= &nouveau_hwmon_ops
,
709 .info
= nouveau_info
,
714 nouveau_hwmon_init(struct drm_device
*dev
)
716 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
717 struct nouveau_drm
*drm
= nouveau_drm(dev
);
718 struct nvkm_iccsense
*iccsense
= nvxx_iccsense(&drm
->client
.device
);
719 struct nvkm_therm
*therm
= nvxx_therm(&drm
->client
.device
);
720 struct nvkm_volt
*volt
= nvxx_volt(&drm
->client
.device
);
721 const struct attribute_group
*special_groups
[N_ATTR_GROUPS
];
722 struct nouveau_hwmon
*hwmon
;
723 struct device
*hwmon_dev
;
727 if (!iccsense
&& !therm
&& !volt
) {
728 NV_DEBUG(drm
, "Skipping hwmon registration\n");
732 hwmon
= drm
->hwmon
= kzalloc(sizeof(*hwmon
), GFP_KERNEL
);
737 if (therm
&& therm
->attr_get
&& therm
->attr_set
) {
738 if (nvkm_therm_temp_get(therm
) >= 0)
739 special_groups
[i
++] = &temp1_auto_point_sensor_group
;
740 if (therm
->fan_get
&& therm
->fan_get(therm
) >= 0)
741 special_groups
[i
++] = &pwm_fan_sensor_group
;
744 special_groups
[i
] = NULL
;
745 hwmon_dev
= hwmon_device_register_with_info(dev
->dev
, "nouveau", dev
,
748 if (IS_ERR(hwmon_dev
)) {
749 ret
= PTR_ERR(hwmon_dev
);
750 NV_ERROR(drm
, "Unable to register hwmon device: %d\n", ret
);
754 hwmon
->hwmon
= hwmon_dev
;
762 nouveau_hwmon_fini(struct drm_device
*dev
)
764 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
765 struct nouveau_hwmon
*hwmon
= nouveau_hwmon(dev
);
771 hwmon_device_unregister(hwmon
->hwmon
);
773 nouveau_drm(dev
)->hwmon
= NULL
;