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 sysfs_emit(buf, "%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 sysfs_emit(buf, "%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 sysfs_emit(buf, "%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 struct hwmon_channel_info * const nouveau_info[] = {
215 HWMON_CHANNEL_INFO(chip,
216 HWMON_C_UPDATE_INTERVAL),
217 HWMON_CHANNEL_INFO(temp,
219 HWMON_T_MAX | HWMON_T_MAX_HYST |
220 HWMON_T_CRIT | HWMON_T_CRIT_HYST |
221 HWMON_T_EMERGENCY | HWMON_T_EMERGENCY_HYST),
222 HWMON_CHANNEL_INFO(fan,
224 HWMON_CHANNEL_INFO(in,
226 HWMON_I_MIN | HWMON_I_MAX |
228 HWMON_CHANNEL_INFO(pwm,
229 HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
230 HWMON_CHANNEL_INFO(power,
231 HWMON_P_INPUT | HWMON_P_CAP_MAX | HWMON_P_CRIT),
236 nouveau_chip_is_visible(const void *data, u32 attr, int channel)
239 case hwmon_chip_update_interval:
247 nouveau_power_is_visible(const void *data, u32 attr, int channel)
249 struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
250 struct nvkm_iccsense *iccsense = nvxx_iccsense(&drm->client.device);
252 if (!iccsense || !iccsense->data_valid || list_empty(&iccsense->rails))
256 case hwmon_power_input:
258 case hwmon_power_max:
259 if (iccsense->power_w_max)
262 case hwmon_power_crit:
263 if (iccsense->power_w_crit)
272 nouveau_temp_is_visible(const void *data, u32 attr, int channel)
274 struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
275 struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
277 if (!therm || !therm->attr_get || nvkm_therm_temp_get(therm) < 0)
281 case hwmon_temp_input:
284 case hwmon_temp_max_hyst:
285 case hwmon_temp_crit:
286 case hwmon_temp_crit_hyst:
287 case hwmon_temp_emergency:
288 case hwmon_temp_emergency_hyst:
296 nouveau_pwm_is_visible(const void *data, u32 attr, int channel)
298 struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
299 struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
301 if (!therm || !therm->attr_get || !therm->fan_get ||
302 therm->fan_get(therm) < 0)
306 case hwmon_pwm_enable:
307 case hwmon_pwm_input:
315 nouveau_input_is_visible(const void *data, u32 attr, int channel)
317 struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
318 struct nvkm_volt *volt = nvxx_volt(&drm->client.device);
320 if (!volt || nvkm_volt_get(volt) < 0)
335 nouveau_fan_is_visible(const void *data, u32 attr, int channel)
337 struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
338 struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
340 if (!therm || !therm->attr_get || nvkm_therm_fan_sense(therm) < 0)
344 case hwmon_fan_input:
352 nouveau_chip_read(struct device *dev, u32 attr, int channel, long *val)
355 case hwmon_chip_update_interval:
366 nouveau_temp_read(struct device *dev, u32 attr, int channel, long *val)
368 struct drm_device *drm_dev = dev_get_drvdata(dev);
369 struct nouveau_drm *drm = nouveau_drm(drm_dev);
370 struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
373 if (!therm || !therm->attr_get)
377 case hwmon_temp_input:
378 if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
380 ret = nvkm_therm_temp_get(therm);
381 *val = ret < 0 ? ret : (ret * 1000);
384 *val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK)
387 case hwmon_temp_max_hyst:
388 *val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST)
391 case hwmon_temp_crit:
392 *val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_CRITICAL)
395 case hwmon_temp_crit_hyst:
396 *val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_CRITICAL_HYST)
399 case hwmon_temp_emergency:
400 *val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN)
403 case hwmon_temp_emergency_hyst:
404 *val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST)
415 nouveau_fan_read(struct device *dev, u32 attr, int channel, long *val)
417 struct drm_device *drm_dev = dev_get_drvdata(dev);
418 struct nouveau_drm *drm = nouveau_drm(drm_dev);
419 struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
425 case hwmon_fan_input:
426 if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
428 *val = nvkm_therm_fan_sense(therm);
438 nouveau_in_read(struct device *dev, u32 attr, int channel, long *val)
440 struct drm_device *drm_dev = dev_get_drvdata(dev);
441 struct nouveau_drm *drm = nouveau_drm(drm_dev);
442 struct nvkm_volt *volt = nvxx_volt(&drm->client.device);
450 if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
452 ret = nvkm_volt_get(volt);
453 *val = ret < 0 ? ret : (ret / 1000);
456 *val = volt->min_uv > 0 ? (volt->min_uv / 1000) : -ENODEV;
459 *val = volt->max_uv > 0 ? (volt->max_uv / 1000) : -ENODEV;
469 nouveau_pwm_read(struct device *dev, u32 attr, int channel, long *val)
471 struct drm_device *drm_dev = dev_get_drvdata(dev);
472 struct nouveau_drm *drm = nouveau_drm(drm_dev);
473 struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
475 if (!therm || !therm->attr_get || !therm->fan_get)
479 case hwmon_pwm_enable:
480 *val = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MODE);
482 case hwmon_pwm_input:
483 if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
485 *val = therm->fan_get(therm);
495 nouveau_power_read(struct device *dev, u32 attr, int channel, long *val)
497 struct drm_device *drm_dev = dev_get_drvdata(dev);
498 struct nouveau_drm *drm = nouveau_drm(drm_dev);
499 struct nvkm_iccsense *iccsense = nvxx_iccsense(&drm->client.device);
505 case hwmon_power_input:
506 if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
508 *val = nvkm_iccsense_read_all(iccsense);
510 case hwmon_power_max:
511 *val = iccsense->power_w_max;
513 case hwmon_power_crit:
514 *val = iccsense->power_w_crit;
524 nouveau_temp_write(struct device *dev, u32 attr, int channel, long val)
526 struct drm_device *drm_dev = dev_get_drvdata(dev);
527 struct nouveau_drm *drm = nouveau_drm(drm_dev);
528 struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
530 if (!therm || !therm->attr_set)
535 return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK,
537 case hwmon_temp_max_hyst:
538 return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST,
540 case hwmon_temp_crit:
541 return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_CRITICAL,
543 case hwmon_temp_crit_hyst:
544 return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_CRITICAL_HYST,
546 case hwmon_temp_emergency:
547 return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN,
549 case hwmon_temp_emergency_hyst:
550 return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST,
558 nouveau_pwm_write(struct device *dev, u32 attr, int channel, long val)
560 struct drm_device *drm_dev = dev_get_drvdata(dev);
561 struct nouveau_drm *drm = nouveau_drm(drm_dev);
562 struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
564 if (!therm || !therm->attr_set)
568 case hwmon_pwm_input:
569 return therm->fan_set(therm, val);
570 case hwmon_pwm_enable:
571 return therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MODE, val);
578 nouveau_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr,
583 return nouveau_chip_is_visible(data, attr, channel);
585 return nouveau_temp_is_visible(data, attr, channel);
587 return nouveau_fan_is_visible(data, attr, channel);
589 return nouveau_input_is_visible(data, attr, channel);
591 return nouveau_pwm_is_visible(data, attr, channel);
593 return nouveau_power_is_visible(data, attr, channel);
599 static const char input_label[] = "GPU core";
602 nouveau_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr,
603 int channel, const char **buf)
605 if (type == hwmon_in && attr == hwmon_in_label) {
614 nouveau_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
615 int channel, long *val)
619 return nouveau_chip_read(dev, attr, channel, val);
621 return nouveau_temp_read(dev, attr, channel, val);
623 return nouveau_fan_read(dev, attr, channel, val);
625 return nouveau_in_read(dev, attr, channel, val);
627 return nouveau_pwm_read(dev, attr, channel, val);
629 return nouveau_power_read(dev, attr, channel, val);
636 nouveau_write(struct device *dev, enum hwmon_sensor_types type, u32 attr,
637 int channel, long val)
641 return nouveau_temp_write(dev, attr, channel, val);
643 return nouveau_pwm_write(dev, attr, channel, val);
649 static const struct hwmon_ops nouveau_hwmon_ops = {
650 .is_visible = nouveau_is_visible,
651 .read = nouveau_read,
652 .read_string = nouveau_read_string,
653 .write = nouveau_write,
656 static const struct hwmon_chip_info nouveau_chip_info = {
657 .ops = &nouveau_hwmon_ops,
658 .info = nouveau_info,
663 nouveau_hwmon_init(struct drm_device *dev)
665 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
666 struct nouveau_drm *drm = nouveau_drm(dev);
667 struct nvkm_iccsense *iccsense = nvxx_iccsense(&drm->client.device);
668 struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
669 struct nvkm_volt *volt = nvxx_volt(&drm->client.device);
670 const struct attribute_group *special_groups[N_ATTR_GROUPS];
671 struct nouveau_hwmon *hwmon;
672 struct device *hwmon_dev;
676 if (!iccsense && !therm && !volt) {
677 NV_DEBUG(drm, "Skipping hwmon registration\n");
681 hwmon = drm->hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
686 if (therm && therm->attr_get && therm->attr_set) {
687 if (nvkm_therm_temp_get(therm) >= 0)
688 special_groups[i++] = &temp1_auto_point_sensor_group;
689 if (therm->fan_get && therm->fan_get(therm) >= 0)
690 special_groups[i++] = &pwm_fan_sensor_group;
693 special_groups[i] = NULL;
694 hwmon_dev = hwmon_device_register_with_info(dev->dev, "nouveau", dev,
697 if (IS_ERR(hwmon_dev)) {
698 ret = PTR_ERR(hwmon_dev);
699 NV_ERROR(drm, "Unable to register hwmon device: %d\n", ret);
703 hwmon->hwmon = hwmon_dev;
711 nouveau_hwmon_fini(struct drm_device *dev)
713 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
714 struct nouveau_hwmon *hwmon = nouveau_hwmon(dev);
720 hwmon_device_unregister(hwmon->hwmon);
722 nouveau_drm(dev)->hwmon = NULL;