Loading...
1/*
2 * Copyright 2010 Red Hat Inc.
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 *
22 * Authors: Ben Skeggs
23 */
24
25#ifdef CONFIG_ACPI
26#include <linux/acpi.h>
27#endif
28#include <linux/power_supply.h>
29#include <linux/hwmon.h>
30#include <linux/hwmon-sysfs.h>
31
32#include "nouveau_drv.h"
33#include "nouveau_hwmon.h"
34
35#include <nvkm/subdev/iccsense.h>
36#include <nvkm/subdev/volt.h>
37
38#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
39
40static ssize_t
41nouveau_hwmon_show_temp1_auto_point1_pwm(struct device *d,
42 struct device_attribute *a, char *buf)
43{
44 return snprintf(buf, PAGE_SIZE, "%d\n", 100);
45}
46static SENSOR_DEVICE_ATTR(temp1_auto_point1_pwm, 0444,
47 nouveau_hwmon_show_temp1_auto_point1_pwm, NULL, 0);
48
49static ssize_t
50nouveau_hwmon_temp1_auto_point1_temp(struct device *d,
51 struct device_attribute *a, char *buf)
52{
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);
56
57 return snprintf(buf, PAGE_SIZE, "%d\n",
58 therm->attr_get(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST) * 1000);
59}
60static ssize_t
61nouveau_hwmon_set_temp1_auto_point1_temp(struct device *d,
62 struct device_attribute *a,
63 const char *buf, size_t count)
64{
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);
68 long value;
69
70 if (kstrtol(buf, 10, &value))
71 return -EINVAL;
72
73 therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST,
74 value / 1000);
75
76 return count;
77}
78static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp, 0644,
79 nouveau_hwmon_temp1_auto_point1_temp,
80 nouveau_hwmon_set_temp1_auto_point1_temp, 0);
81
82static ssize_t
83nouveau_hwmon_temp1_auto_point1_temp_hyst(struct device *d,
84 struct device_attribute *a, char *buf)
85{
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);
89
90 return snprintf(buf, PAGE_SIZE, "%d\n",
91 therm->attr_get(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST) * 1000);
92}
93static ssize_t
94nouveau_hwmon_set_temp1_auto_point1_temp_hyst(struct device *d,
95 struct device_attribute *a,
96 const char *buf, size_t count)
97{
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);
101 long value;
102
103 if (kstrtol(buf, 10, &value))
104 return -EINVAL;
105
106 therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST,
107 value / 1000);
108
109 return count;
110}
111static 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);
114
115static ssize_t
116nouveau_hwmon_get_pwm1_max(struct device *d,
117 struct device_attribute *a, char *buf)
118{
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);
122 int ret;
123
124 ret = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MAX_DUTY);
125 if (ret < 0)
126 return ret;
127
128 return sprintf(buf, "%i\n", ret);
129}
130
131static ssize_t
132nouveau_hwmon_get_pwm1_min(struct device *d,
133 struct device_attribute *a, char *buf)
134{
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);
138 int ret;
139
140 ret = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MIN_DUTY);
141 if (ret < 0)
142 return ret;
143
144 return sprintf(buf, "%i\n", ret);
145}
146
147static ssize_t
148nouveau_hwmon_set_pwm1_min(struct device *d, struct device_attribute *a,
149 const char *buf, size_t count)
150{
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);
154 long value;
155 int ret;
156
157 if (kstrtol(buf, 10, &value))
158 return -EINVAL;
159
160 ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MIN_DUTY, value);
161 if (ret < 0)
162 return ret;
163
164 return count;
165}
166static SENSOR_DEVICE_ATTR(pwm1_min, 0644,
167 nouveau_hwmon_get_pwm1_min,
168 nouveau_hwmon_set_pwm1_min, 0);
169
170static ssize_t
171nouveau_hwmon_set_pwm1_max(struct device *d, struct device_attribute *a,
172 const char *buf, size_t count)
173{
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);
177 long value;
178 int ret;
179
180 if (kstrtol(buf, 10, &value))
181 return -EINVAL;
182
183 ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MAX_DUTY, value);
184 if (ret < 0)
185 return ret;
186
187 return count;
188}
189static SENSOR_DEVICE_ATTR(pwm1_max, 0644,
190 nouveau_hwmon_get_pwm1_max,
191 nouveau_hwmon_set_pwm1_max, 0);
192
193static struct attribute *pwm_fan_sensor_attrs[] = {
194 &sensor_dev_attr_pwm1_min.dev_attr.attr,
195 &sensor_dev_attr_pwm1_max.dev_attr.attr,
196 NULL
197};
198static const struct attribute_group pwm_fan_sensor_group = {
199 .attrs = pwm_fan_sensor_attrs,
200};
201
202static 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,
206 NULL
207};
208static const struct attribute_group temp1_auto_point_sensor_group = {
209 .attrs = temp1_auto_point_sensor_attrs,
210};
211
212#define N_ATTR_GROUPS 3
213
214static const u32 nouveau_config_chip[] = {
215 HWMON_C_UPDATE_INTERVAL,
216 0
217};
218
219static const u32 nouveau_config_in[] = {
220 HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_LABEL,
221 0
222};
223
224static 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,
228 0
229};
230
231static const u32 nouveau_config_fan[] = {
232 HWMON_F_INPUT,
233 0
234};
235
236static const u32 nouveau_config_pwm[] = {
237 HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
238 0
239};
240
241static const u32 nouveau_config_power[] = {
242 HWMON_P_INPUT | HWMON_P_CAP_MAX | HWMON_P_CRIT,
243 0
244};
245
246static const struct hwmon_channel_info nouveau_chip = {
247 .type = hwmon_chip,
248 .config = nouveau_config_chip,
249};
250
251static const struct hwmon_channel_info nouveau_temp = {
252 .type = hwmon_temp,
253 .config = nouveau_config_temp,
254};
255
256static const struct hwmon_channel_info nouveau_fan = {
257 .type = hwmon_fan,
258 .config = nouveau_config_fan,
259};
260
261static const struct hwmon_channel_info nouveau_in = {
262 .type = hwmon_in,
263 .config = nouveau_config_in,
264};
265
266static const struct hwmon_channel_info nouveau_pwm = {
267 .type = hwmon_pwm,
268 .config = nouveau_config_pwm,
269};
270
271static const struct hwmon_channel_info nouveau_power = {
272 .type = hwmon_power,
273 .config = nouveau_config_power,
274};
275
276static const struct hwmon_channel_info *nouveau_info[] = {
277 &nouveau_chip,
278 &nouveau_temp,
279 &nouveau_fan,
280 &nouveau_in,
281 &nouveau_pwm,
282 &nouveau_power,
283 NULL
284};
285
286static umode_t
287nouveau_chip_is_visible(const void *data, u32 attr, int channel)
288{
289 switch (attr) {
290 case hwmon_chip_update_interval:
291 return 0444;
292 default:
293 return 0;
294 }
295}
296
297static umode_t
298nouveau_power_is_visible(const void *data, u32 attr, int channel)
299{
300 struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
301 struct nvkm_iccsense *iccsense = nvxx_iccsense(&drm->client.device);
302
303 if (!iccsense || !iccsense->data_valid || list_empty(&iccsense->rails))
304 return 0;
305
306 switch (attr) {
307 case hwmon_power_input:
308 return 0444;
309 case hwmon_power_max:
310 if (iccsense->power_w_max)
311 return 0444;
312 return 0;
313 case hwmon_power_crit:
314 if (iccsense->power_w_crit)
315 return 0444;
316 return 0;
317 default:
318 return 0;
319 }
320}
321
322static umode_t
323nouveau_temp_is_visible(const void *data, u32 attr, int channel)
324{
325 struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
326 struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
327
328 if (!therm || !therm->attr_get || nvkm_therm_temp_get(therm) < 0)
329 return 0;
330
331 switch (attr) {
332 case hwmon_temp_input:
333 return 0444;
334 case hwmon_temp_max:
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:
340 return 0644;
341 default:
342 return 0;
343 }
344}
345
346static umode_t
347nouveau_pwm_is_visible(const void *data, u32 attr, int channel)
348{
349 struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
350 struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
351
352 if (!therm || !therm->attr_get || !therm->fan_get ||
353 therm->fan_get(therm) < 0)
354 return 0;
355
356 switch (attr) {
357 case hwmon_pwm_enable:
358 case hwmon_pwm_input:
359 return 0644;
360 default:
361 return 0;
362 }
363}
364
365static umode_t
366nouveau_input_is_visible(const void *data, u32 attr, int channel)
367{
368 struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
369 struct nvkm_volt *volt = nvxx_volt(&drm->client.device);
370
371 if (!volt || nvkm_volt_get(volt) < 0)
372 return 0;
373
374 switch (attr) {
375 case hwmon_in_input:
376 case hwmon_in_label:
377 case hwmon_in_min:
378 case hwmon_in_max:
379 return 0444;
380 default:
381 return 0;
382 }
383}
384
385static umode_t
386nouveau_fan_is_visible(const void *data, u32 attr, int channel)
387{
388 struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
389 struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
390
391 if (!therm || !therm->attr_get || nvkm_therm_fan_sense(therm) < 0)
392 return 0;
393
394 switch (attr) {
395 case hwmon_fan_input:
396 return 0444;
397 default:
398 return 0;
399 }
400}
401
402static int
403nouveau_chip_read(struct device *dev, u32 attr, int channel, long *val)
404{
405 switch (attr) {
406 case hwmon_chip_update_interval:
407 *val = 1000;
408 break;
409 default:
410 return -EOPNOTSUPP;
411 }
412
413 return 0;
414}
415
416static int
417nouveau_temp_read(struct device *dev, u32 attr, int channel, long *val)
418{
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);
422 int ret;
423
424 if (!therm || !therm->attr_get)
425 return -EOPNOTSUPP;
426
427 switch (attr) {
428 case hwmon_temp_input:
429 if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
430 return -EINVAL;
431 ret = nvkm_therm_temp_get(therm);
432 *val = ret < 0 ? ret : (ret * 1000);
433 break;
434 case hwmon_temp_max:
435 *val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK)
436 * 1000;
437 break;
438 case hwmon_temp_max_hyst:
439 *val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST)
440 * 1000;
441 break;
442 case hwmon_temp_crit:
443 *val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_CRITICAL)
444 * 1000;
445 break;
446 case hwmon_temp_crit_hyst:
447 *val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_CRITICAL_HYST)
448 * 1000;
449 break;
450 case hwmon_temp_emergency:
451 *val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN)
452 * 1000;
453 break;
454 case hwmon_temp_emergency_hyst:
455 *val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST)
456 * 1000;
457 break;
458 default:
459 return -EOPNOTSUPP;
460 }
461
462 return 0;
463}
464
465static int
466nouveau_fan_read(struct device *dev, u32 attr, int channel, long *val)
467{
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);
471
472 if (!therm)
473 return -EOPNOTSUPP;
474
475 switch (attr) {
476 case hwmon_fan_input:
477 if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
478 return -EINVAL;
479 *val = nvkm_therm_fan_sense(therm);
480 break;
481 default:
482 return -EOPNOTSUPP;
483 }
484
485 return 0;
486}
487
488static int
489nouveau_in_read(struct device *dev, u32 attr, int channel, long *val)
490{
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);
494 int ret;
495
496 if (!volt)
497 return -EOPNOTSUPP;
498
499 switch (attr) {
500 case hwmon_in_input:
501 if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
502 return -EINVAL;
503 ret = nvkm_volt_get(volt);
504 *val = ret < 0 ? ret : (ret / 1000);
505 break;
506 case hwmon_in_min:
507 *val = volt->min_uv > 0 ? (volt->min_uv / 1000) : -ENODEV;
508 break;
509 case hwmon_in_max:
510 *val = volt->max_uv > 0 ? (volt->max_uv / 1000) : -ENODEV;
511 break;
512 default:
513 return -EOPNOTSUPP;
514 }
515
516 return 0;
517}
518
519static int
520nouveau_pwm_read(struct device *dev, u32 attr, int channel, long *val)
521{
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);
525
526 if (!therm || !therm->attr_get || !therm->fan_get)
527 return -EOPNOTSUPP;
528
529 switch (attr) {
530 case hwmon_pwm_enable:
531 *val = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MODE);
532 break;
533 case hwmon_pwm_input:
534 if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
535 return -EINVAL;
536 *val = therm->fan_get(therm);
537 break;
538 default:
539 return -EOPNOTSUPP;
540 }
541
542 return 0;
543}
544
545static int
546nouveau_power_read(struct device *dev, u32 attr, int channel, long *val)
547{
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);
551
552 if (!iccsense)
553 return -EOPNOTSUPP;
554
555 switch (attr) {
556 case hwmon_power_input:
557 if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
558 return -EINVAL;
559 *val = nvkm_iccsense_read_all(iccsense);
560 break;
561 case hwmon_power_max:
562 *val = iccsense->power_w_max;
563 break;
564 case hwmon_power_crit:
565 *val = iccsense->power_w_crit;
566 break;
567 default:
568 return -EOPNOTSUPP;
569 }
570
571 return 0;
572}
573
574static int
575nouveau_temp_write(struct device *dev, u32 attr, int channel, long val)
576{
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);
580
581 if (!therm || !therm->attr_set)
582 return -EOPNOTSUPP;
583
584 switch (attr) {
585 case hwmon_temp_max:
586 return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK,
587 val / 1000);
588 case hwmon_temp_max_hyst:
589 return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST,
590 val / 1000);
591 case hwmon_temp_crit:
592 return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_CRITICAL,
593 val / 1000);
594 case hwmon_temp_crit_hyst:
595 return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_CRITICAL_HYST,
596 val / 1000);
597 case hwmon_temp_emergency:
598 return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN,
599 val / 1000);
600 case hwmon_temp_emergency_hyst:
601 return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST,
602 val / 1000);
603 default:
604 return -EOPNOTSUPP;
605 }
606}
607
608static int
609nouveau_pwm_write(struct device *dev, u32 attr, int channel, long val)
610{
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);
614
615 if (!therm || !therm->attr_set)
616 return -EOPNOTSUPP;
617
618 switch (attr) {
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);
623 default:
624 return -EOPNOTSUPP;
625 }
626}
627
628static umode_t
629nouveau_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr,
630 int channel)
631{
632 switch (type) {
633 case hwmon_chip:
634 return nouveau_chip_is_visible(data, attr, channel);
635 case hwmon_temp:
636 return nouveau_temp_is_visible(data, attr, channel);
637 case hwmon_fan:
638 return nouveau_fan_is_visible(data, attr, channel);
639 case hwmon_in:
640 return nouveau_input_is_visible(data, attr, channel);
641 case hwmon_pwm:
642 return nouveau_pwm_is_visible(data, attr, channel);
643 case hwmon_power:
644 return nouveau_power_is_visible(data, attr, channel);
645 default:
646 return 0;
647 }
648}
649
650static const char input_label[] = "GPU core";
651
652static int
653nouveau_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr,
654 int channel, const char **buf)
655{
656 if (type == hwmon_in && attr == hwmon_in_label) {
657 *buf = input_label;
658 return 0;
659 }
660
661 return -EOPNOTSUPP;
662}
663
664static int
665nouveau_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
666 int channel, long *val)
667{
668 switch (type) {
669 case hwmon_chip:
670 return nouveau_chip_read(dev, attr, channel, val);
671 case hwmon_temp:
672 return nouveau_temp_read(dev, attr, channel, val);
673 case hwmon_fan:
674 return nouveau_fan_read(dev, attr, channel, val);
675 case hwmon_in:
676 return nouveau_in_read(dev, attr, channel, val);
677 case hwmon_pwm:
678 return nouveau_pwm_read(dev, attr, channel, val);
679 case hwmon_power:
680 return nouveau_power_read(dev, attr, channel, val);
681 default:
682 return -EOPNOTSUPP;
683 }
684}
685
686static int
687nouveau_write(struct device *dev, enum hwmon_sensor_types type, u32 attr,
688 int channel, long val)
689{
690 switch (type) {
691 case hwmon_temp:
692 return nouveau_temp_write(dev, attr, channel, val);
693 case hwmon_pwm:
694 return nouveau_pwm_write(dev, attr, channel, val);
695 default:
696 return -EOPNOTSUPP;
697 }
698}
699
700static 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,
705};
706
707static const struct hwmon_chip_info nouveau_chip_info = {
708 .ops = &nouveau_hwmon_ops,
709 .info = nouveau_info,
710};
711#endif
712
713int
714nouveau_hwmon_init(struct drm_device *dev)
715{
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;
724 int ret = 0;
725 int i = 0;
726
727 if (!iccsense && !therm && !volt) {
728 NV_DEBUG(drm, "Skipping hwmon registration\n");
729 return 0;
730 }
731
732 hwmon = drm->hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
733 if (!hwmon)
734 return -ENOMEM;
735 hwmon->dev = dev;
736
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;
742 }
743
744 special_groups[i] = 0;
745 hwmon_dev = hwmon_device_register_with_info(dev->dev, "nouveau", dev,
746 &nouveau_chip_info,
747 special_groups);
748 if (IS_ERR(hwmon_dev)) {
749 ret = PTR_ERR(hwmon_dev);
750 NV_ERROR(drm, "Unable to register hwmon device: %d\n", ret);
751 return ret;
752 }
753
754 hwmon->hwmon = hwmon_dev;
755 return 0;
756#else
757 return 0;
758#endif
759}
760
761void
762nouveau_hwmon_fini(struct drm_device *dev)
763{
764#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
765 struct nouveau_hwmon *hwmon = nouveau_hwmon(dev);
766
767 if (!hwmon)
768 return;
769
770 if (hwmon->hwmon)
771 hwmon_device_unregister(hwmon->hwmon);
772
773 nouveau_drm(dev)->hwmon = NULL;
774 kfree(hwmon);
775#endif
776}
1/*
2 * Copyright 2010 Red Hat Inc.
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 *
22 * Authors: Ben Skeggs
23 */
24
25#ifdef CONFIG_ACPI
26#include <linux/acpi.h>
27#endif
28#include <linux/power_supply.h>
29#include <linux/hwmon.h>
30#include <linux/hwmon-sysfs.h>
31
32#include <drm/drmP.h>
33
34#include "nouveau_drv.h"
35#include "nouveau_hwmon.h"
36
37#include <nvkm/subdev/iccsense.h>
38#include <nvkm/subdev/volt.h>
39
40#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
41static ssize_t
42nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
43{
44 struct drm_device *dev = dev_get_drvdata(d);
45 struct nouveau_drm *drm = nouveau_drm(dev);
46 struct nvkm_therm *therm = nvxx_therm(&drm->device);
47 int temp = nvkm_therm_temp_get(therm);
48
49 if (temp < 0)
50 return temp;
51
52 return snprintf(buf, PAGE_SIZE, "%d\n", temp * 1000);
53}
54static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
55 NULL, 0);
56
57static ssize_t
58nouveau_hwmon_show_temp1_auto_point1_pwm(struct device *d,
59 struct device_attribute *a, char *buf)
60{
61 return snprintf(buf, PAGE_SIZE, "%d\n", 100);
62}
63static SENSOR_DEVICE_ATTR(temp1_auto_point1_pwm, S_IRUGO,
64 nouveau_hwmon_show_temp1_auto_point1_pwm, NULL, 0);
65
66static ssize_t
67nouveau_hwmon_temp1_auto_point1_temp(struct device *d,
68 struct device_attribute *a, char *buf)
69{
70 struct drm_device *dev = dev_get_drvdata(d);
71 struct nouveau_drm *drm = nouveau_drm(dev);
72 struct nvkm_therm *therm = nvxx_therm(&drm->device);
73
74 return snprintf(buf, PAGE_SIZE, "%d\n",
75 therm->attr_get(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST) * 1000);
76}
77static ssize_t
78nouveau_hwmon_set_temp1_auto_point1_temp(struct device *d,
79 struct device_attribute *a,
80 const char *buf, size_t count)
81{
82 struct drm_device *dev = dev_get_drvdata(d);
83 struct nouveau_drm *drm = nouveau_drm(dev);
84 struct nvkm_therm *therm = nvxx_therm(&drm->device);
85 long value;
86
87 if (kstrtol(buf, 10, &value) == -EINVAL)
88 return count;
89
90 therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST,
91 value / 1000);
92
93 return count;
94}
95static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
96 nouveau_hwmon_temp1_auto_point1_temp,
97 nouveau_hwmon_set_temp1_auto_point1_temp, 0);
98
99static ssize_t
100nouveau_hwmon_temp1_auto_point1_temp_hyst(struct device *d,
101 struct device_attribute *a, char *buf)
102{
103 struct drm_device *dev = dev_get_drvdata(d);
104 struct nouveau_drm *drm = nouveau_drm(dev);
105 struct nvkm_therm *therm = nvxx_therm(&drm->device);
106
107 return snprintf(buf, PAGE_SIZE, "%d\n",
108 therm->attr_get(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST) * 1000);
109}
110static ssize_t
111nouveau_hwmon_set_temp1_auto_point1_temp_hyst(struct device *d,
112 struct device_attribute *a,
113 const char *buf, size_t count)
114{
115 struct drm_device *dev = dev_get_drvdata(d);
116 struct nouveau_drm *drm = nouveau_drm(dev);
117 struct nvkm_therm *therm = nvxx_therm(&drm->device);
118 long value;
119
120 if (kstrtol(buf, 10, &value) == -EINVAL)
121 return count;
122
123 therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST,
124 value / 1000);
125
126 return count;
127}
128static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
129 nouveau_hwmon_temp1_auto_point1_temp_hyst,
130 nouveau_hwmon_set_temp1_auto_point1_temp_hyst, 0);
131
132static ssize_t
133nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
134{
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->device);
138
139 return snprintf(buf, PAGE_SIZE, "%d\n",
140 therm->attr_get(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK) * 1000);
141}
142static ssize_t
143nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
144 const char *buf, size_t count)
145{
146 struct drm_device *dev = dev_get_drvdata(d);
147 struct nouveau_drm *drm = nouveau_drm(dev);
148 struct nvkm_therm *therm = nvxx_therm(&drm->device);
149 long value;
150
151 if (kstrtol(buf, 10, &value) == -EINVAL)
152 return count;
153
154 therm->attr_set(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK, value / 1000);
155
156 return count;
157}
158static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
159 nouveau_hwmon_set_max_temp,
160 0);
161
162static ssize_t
163nouveau_hwmon_max_temp_hyst(struct device *d, struct device_attribute *a,
164 char *buf)
165{
166 struct drm_device *dev = dev_get_drvdata(d);
167 struct nouveau_drm *drm = nouveau_drm(dev);
168 struct nvkm_therm *therm = nvxx_therm(&drm->device);
169
170 return snprintf(buf, PAGE_SIZE, "%d\n",
171 therm->attr_get(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST) * 1000);
172}
173static ssize_t
174nouveau_hwmon_set_max_temp_hyst(struct device *d, struct device_attribute *a,
175 const char *buf, size_t count)
176{
177 struct drm_device *dev = dev_get_drvdata(d);
178 struct nouveau_drm *drm = nouveau_drm(dev);
179 struct nvkm_therm *therm = nvxx_therm(&drm->device);
180 long value;
181
182 if (kstrtol(buf, 10, &value) == -EINVAL)
183 return count;
184
185 therm->attr_set(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST,
186 value / 1000);
187
188 return count;
189}
190static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
191 nouveau_hwmon_max_temp_hyst,
192 nouveau_hwmon_set_max_temp_hyst, 0);
193
194static ssize_t
195nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
196 char *buf)
197{
198 struct drm_device *dev = dev_get_drvdata(d);
199 struct nouveau_drm *drm = nouveau_drm(dev);
200 struct nvkm_therm *therm = nvxx_therm(&drm->device);
201
202 return snprintf(buf, PAGE_SIZE, "%d\n",
203 therm->attr_get(therm, NVKM_THERM_ATTR_THRS_CRITICAL) * 1000);
204}
205static ssize_t
206nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
207 const char *buf,
208 size_t count)
209{
210 struct drm_device *dev = dev_get_drvdata(d);
211 struct nouveau_drm *drm = nouveau_drm(dev);
212 struct nvkm_therm *therm = nvxx_therm(&drm->device);
213 long value;
214
215 if (kstrtol(buf, 10, &value) == -EINVAL)
216 return count;
217
218 therm->attr_set(therm, NVKM_THERM_ATTR_THRS_CRITICAL, value / 1000);
219
220 return count;
221}
222static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
223 nouveau_hwmon_critical_temp,
224 nouveau_hwmon_set_critical_temp,
225 0);
226
227static ssize_t
228nouveau_hwmon_critical_temp_hyst(struct device *d, struct device_attribute *a,
229 char *buf)
230{
231 struct drm_device *dev = dev_get_drvdata(d);
232 struct nouveau_drm *drm = nouveau_drm(dev);
233 struct nvkm_therm *therm = nvxx_therm(&drm->device);
234
235 return snprintf(buf, PAGE_SIZE, "%d\n",
236 therm->attr_get(therm, NVKM_THERM_ATTR_THRS_CRITICAL_HYST) * 1000);
237}
238static ssize_t
239nouveau_hwmon_set_critical_temp_hyst(struct device *d,
240 struct device_attribute *a,
241 const char *buf,
242 size_t count)
243{
244 struct drm_device *dev = dev_get_drvdata(d);
245 struct nouveau_drm *drm = nouveau_drm(dev);
246 struct nvkm_therm *therm = nvxx_therm(&drm->device);
247 long value;
248
249 if (kstrtol(buf, 10, &value) == -EINVAL)
250 return count;
251
252 therm->attr_set(therm, NVKM_THERM_ATTR_THRS_CRITICAL_HYST,
253 value / 1000);
254
255 return count;
256}
257static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO | S_IWUSR,
258 nouveau_hwmon_critical_temp_hyst,
259 nouveau_hwmon_set_critical_temp_hyst, 0);
260static ssize_t
261nouveau_hwmon_emergency_temp(struct device *d, struct device_attribute *a,
262 char *buf)
263{
264 struct drm_device *dev = dev_get_drvdata(d);
265 struct nouveau_drm *drm = nouveau_drm(dev);
266 struct nvkm_therm *therm = nvxx_therm(&drm->device);
267
268 return snprintf(buf, PAGE_SIZE, "%d\n",
269 therm->attr_get(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN) * 1000);
270}
271static ssize_t
272nouveau_hwmon_set_emergency_temp(struct device *d, struct device_attribute *a,
273 const char *buf,
274 size_t count)
275{
276 struct drm_device *dev = dev_get_drvdata(d);
277 struct nouveau_drm *drm = nouveau_drm(dev);
278 struct nvkm_therm *therm = nvxx_therm(&drm->device);
279 long value;
280
281 if (kstrtol(buf, 10, &value) == -EINVAL)
282 return count;
283
284 therm->attr_set(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN, value / 1000);
285
286 return count;
287}
288static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO | S_IWUSR,
289 nouveau_hwmon_emergency_temp,
290 nouveau_hwmon_set_emergency_temp,
291 0);
292
293static ssize_t
294nouveau_hwmon_emergency_temp_hyst(struct device *d, struct device_attribute *a,
295 char *buf)
296{
297 struct drm_device *dev = dev_get_drvdata(d);
298 struct nouveau_drm *drm = nouveau_drm(dev);
299 struct nvkm_therm *therm = nvxx_therm(&drm->device);
300
301 return snprintf(buf, PAGE_SIZE, "%d\n",
302 therm->attr_get(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST) * 1000);
303}
304static ssize_t
305nouveau_hwmon_set_emergency_temp_hyst(struct device *d,
306 struct device_attribute *a,
307 const char *buf,
308 size_t count)
309{
310 struct drm_device *dev = dev_get_drvdata(d);
311 struct nouveau_drm *drm = nouveau_drm(dev);
312 struct nvkm_therm *therm = nvxx_therm(&drm->device);
313 long value;
314
315 if (kstrtol(buf, 10, &value) == -EINVAL)
316 return count;
317
318 therm->attr_set(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST,
319 value / 1000);
320
321 return count;
322}
323static SENSOR_DEVICE_ATTR(temp1_emergency_hyst, S_IRUGO | S_IWUSR,
324 nouveau_hwmon_emergency_temp_hyst,
325 nouveau_hwmon_set_emergency_temp_hyst,
326 0);
327
328static ssize_t nouveau_hwmon_show_name(struct device *dev,
329 struct device_attribute *attr,
330 char *buf)
331{
332 return sprintf(buf, "nouveau\n");
333}
334static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
335
336static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
337 struct device_attribute *attr,
338 char *buf)
339{
340 return sprintf(buf, "1000\n");
341}
342static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
343 nouveau_hwmon_show_update_rate,
344 NULL, 0);
345
346static ssize_t
347nouveau_hwmon_show_fan1_input(struct device *d, struct device_attribute *attr,
348 char *buf)
349{
350 struct drm_device *dev = dev_get_drvdata(d);
351 struct nouveau_drm *drm = nouveau_drm(dev);
352 struct nvkm_therm *therm = nvxx_therm(&drm->device);
353
354 return snprintf(buf, PAGE_SIZE, "%d\n", nvkm_therm_fan_sense(therm));
355}
356static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, nouveau_hwmon_show_fan1_input,
357 NULL, 0);
358
359 static ssize_t
360nouveau_hwmon_get_pwm1_enable(struct device *d,
361 struct device_attribute *a, char *buf)
362{
363 struct drm_device *dev = dev_get_drvdata(d);
364 struct nouveau_drm *drm = nouveau_drm(dev);
365 struct nvkm_therm *therm = nvxx_therm(&drm->device);
366 int ret;
367
368 ret = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MODE);
369 if (ret < 0)
370 return ret;
371
372 return sprintf(buf, "%i\n", ret);
373}
374
375static ssize_t
376nouveau_hwmon_set_pwm1_enable(struct device *d, struct device_attribute *a,
377 const char *buf, size_t count)
378{
379 struct drm_device *dev = dev_get_drvdata(d);
380 struct nouveau_drm *drm = nouveau_drm(dev);
381 struct nvkm_therm *therm = nvxx_therm(&drm->device);
382 long value;
383 int ret;
384
385 ret = kstrtol(buf, 10, &value);
386 if (ret)
387 return ret;
388
389 ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MODE, value);
390 if (ret)
391 return ret;
392 else
393 return count;
394}
395static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
396 nouveau_hwmon_get_pwm1_enable,
397 nouveau_hwmon_set_pwm1_enable, 0);
398
399static ssize_t
400nouveau_hwmon_get_pwm1(struct device *d, struct device_attribute *a, char *buf)
401{
402 struct drm_device *dev = dev_get_drvdata(d);
403 struct nouveau_drm *drm = nouveau_drm(dev);
404 struct nvkm_therm *therm = nvxx_therm(&drm->device);
405 int ret;
406
407 ret = therm->fan_get(therm);
408 if (ret < 0)
409 return ret;
410
411 return sprintf(buf, "%i\n", ret);
412}
413
414static ssize_t
415nouveau_hwmon_set_pwm1(struct device *d, struct device_attribute *a,
416 const char *buf, size_t count)
417{
418 struct drm_device *dev = dev_get_drvdata(d);
419 struct nouveau_drm *drm = nouveau_drm(dev);
420 struct nvkm_therm *therm = nvxx_therm(&drm->device);
421 int ret = -ENODEV;
422 long value;
423
424 if (kstrtol(buf, 10, &value) == -EINVAL)
425 return -EINVAL;
426
427 ret = therm->fan_set(therm, value);
428 if (ret)
429 return ret;
430
431 return count;
432}
433
434static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR,
435 nouveau_hwmon_get_pwm1,
436 nouveau_hwmon_set_pwm1, 0);
437
438static ssize_t
439nouveau_hwmon_get_pwm1_min(struct device *d,
440 struct device_attribute *a, char *buf)
441{
442 struct drm_device *dev = dev_get_drvdata(d);
443 struct nouveau_drm *drm = nouveau_drm(dev);
444 struct nvkm_therm *therm = nvxx_therm(&drm->device);
445 int ret;
446
447 ret = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MIN_DUTY);
448 if (ret < 0)
449 return ret;
450
451 return sprintf(buf, "%i\n", ret);
452}
453
454static ssize_t
455nouveau_hwmon_set_pwm1_min(struct device *d, struct device_attribute *a,
456 const char *buf, size_t count)
457{
458 struct drm_device *dev = dev_get_drvdata(d);
459 struct nouveau_drm *drm = nouveau_drm(dev);
460 struct nvkm_therm *therm = nvxx_therm(&drm->device);
461 long value;
462 int ret;
463
464 if (kstrtol(buf, 10, &value) == -EINVAL)
465 return -EINVAL;
466
467 ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MIN_DUTY, value);
468 if (ret < 0)
469 return ret;
470
471 return count;
472}
473
474static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO | S_IWUSR,
475 nouveau_hwmon_get_pwm1_min,
476 nouveau_hwmon_set_pwm1_min, 0);
477
478static ssize_t
479nouveau_hwmon_get_pwm1_max(struct device *d,
480 struct device_attribute *a, char *buf)
481{
482 struct drm_device *dev = dev_get_drvdata(d);
483 struct nouveau_drm *drm = nouveau_drm(dev);
484 struct nvkm_therm *therm = nvxx_therm(&drm->device);
485 int ret;
486
487 ret = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MAX_DUTY);
488 if (ret < 0)
489 return ret;
490
491 return sprintf(buf, "%i\n", ret);
492}
493
494static ssize_t
495nouveau_hwmon_set_pwm1_max(struct device *d, struct device_attribute *a,
496 const char *buf, size_t count)
497{
498 struct drm_device *dev = dev_get_drvdata(d);
499 struct nouveau_drm *drm = nouveau_drm(dev);
500 struct nvkm_therm *therm = nvxx_therm(&drm->device);
501 long value;
502 int ret;
503
504 if (kstrtol(buf, 10, &value) == -EINVAL)
505 return -EINVAL;
506
507 ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MAX_DUTY, value);
508 if (ret < 0)
509 return ret;
510
511 return count;
512}
513
514static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO | S_IWUSR,
515 nouveau_hwmon_get_pwm1_max,
516 nouveau_hwmon_set_pwm1_max, 0);
517
518static ssize_t
519nouveau_hwmon_get_in0_input(struct device *d,
520 struct device_attribute *a, char *buf)
521{
522 struct drm_device *dev = dev_get_drvdata(d);
523 struct nouveau_drm *drm = nouveau_drm(dev);
524 struct nvkm_volt *volt = nvxx_volt(&drm->device);
525 int ret;
526
527 ret = nvkm_volt_get(volt);
528 if (ret < 0)
529 return ret;
530
531 return sprintf(buf, "%i\n", ret / 1000);
532}
533
534static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO,
535 nouveau_hwmon_get_in0_input, NULL, 0);
536
537static ssize_t
538nouveau_hwmon_get_in0_min(struct device *d,
539 struct device_attribute *a, char *buf)
540{
541 struct drm_device *dev = dev_get_drvdata(d);
542 struct nouveau_drm *drm = nouveau_drm(dev);
543 struct nvkm_volt *volt = nvxx_volt(&drm->device);
544
545 if (!volt || !volt->min_uv)
546 return -ENODEV;
547
548 return sprintf(buf, "%i\n", volt->min_uv / 1000);
549}
550
551static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO,
552 nouveau_hwmon_get_in0_min, NULL, 0);
553
554static ssize_t
555nouveau_hwmon_get_in0_max(struct device *d,
556 struct device_attribute *a, char *buf)
557{
558 struct drm_device *dev = dev_get_drvdata(d);
559 struct nouveau_drm *drm = nouveau_drm(dev);
560 struct nvkm_volt *volt = nvxx_volt(&drm->device);
561
562 if (!volt || !volt->max_uv)
563 return -ENODEV;
564
565 return sprintf(buf, "%i\n", volt->max_uv / 1000);
566}
567
568static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO,
569 nouveau_hwmon_get_in0_max, NULL, 0);
570
571static ssize_t
572nouveau_hwmon_get_in0_label(struct device *d,
573 struct device_attribute *a, char *buf)
574{
575 return sprintf(buf, "GPU core\n");
576}
577
578static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO,
579 nouveau_hwmon_get_in0_label, NULL, 0);
580
581static ssize_t
582nouveau_hwmon_get_power1_input(struct device *d, struct device_attribute *a,
583 char *buf)
584{
585 struct drm_device *dev = dev_get_drvdata(d);
586 struct nouveau_drm *drm = nouveau_drm(dev);
587 struct nvkm_iccsense *iccsense = nvxx_iccsense(&drm->device);
588 int result = nvkm_iccsense_read_all(iccsense);
589
590 if (result < 0)
591 return result;
592
593 return sprintf(buf, "%i\n", result);
594}
595
596static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO,
597 nouveau_hwmon_get_power1_input, NULL, 0);
598
599static struct attribute *hwmon_default_attributes[] = {
600 &sensor_dev_attr_name.dev_attr.attr,
601 &sensor_dev_attr_update_rate.dev_attr.attr,
602 NULL
603};
604static struct attribute *hwmon_temp_attributes[] = {
605 &sensor_dev_attr_temp1_input.dev_attr.attr,
606 &sensor_dev_attr_temp1_auto_point1_pwm.dev_attr.attr,
607 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
608 &sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr.attr,
609 &sensor_dev_attr_temp1_max.dev_attr.attr,
610 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
611 &sensor_dev_attr_temp1_crit.dev_attr.attr,
612 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
613 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
614 &sensor_dev_attr_temp1_emergency_hyst.dev_attr.attr,
615 NULL
616};
617static struct attribute *hwmon_fan_rpm_attributes[] = {
618 &sensor_dev_attr_fan1_input.dev_attr.attr,
619 NULL
620};
621static struct attribute *hwmon_pwm_fan_attributes[] = {
622 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
623 &sensor_dev_attr_pwm1.dev_attr.attr,
624 &sensor_dev_attr_pwm1_min.dev_attr.attr,
625 &sensor_dev_attr_pwm1_max.dev_attr.attr,
626 NULL
627};
628
629static struct attribute *hwmon_in0_attributes[] = {
630 &sensor_dev_attr_in0_input.dev_attr.attr,
631 &sensor_dev_attr_in0_min.dev_attr.attr,
632 &sensor_dev_attr_in0_max.dev_attr.attr,
633 &sensor_dev_attr_in0_label.dev_attr.attr,
634 NULL
635};
636
637static struct attribute *hwmon_power_attributes[] = {
638 &sensor_dev_attr_power1_input.dev_attr.attr,
639 NULL
640};
641
642static const struct attribute_group hwmon_default_attrgroup = {
643 .attrs = hwmon_default_attributes,
644};
645static const struct attribute_group hwmon_temp_attrgroup = {
646 .attrs = hwmon_temp_attributes,
647};
648static const struct attribute_group hwmon_fan_rpm_attrgroup = {
649 .attrs = hwmon_fan_rpm_attributes,
650};
651static const struct attribute_group hwmon_pwm_fan_attrgroup = {
652 .attrs = hwmon_pwm_fan_attributes,
653};
654static const struct attribute_group hwmon_in0_attrgroup = {
655 .attrs = hwmon_in0_attributes,
656};
657static const struct attribute_group hwmon_power_attrgroup = {
658 .attrs = hwmon_power_attributes,
659};
660#endif
661
662int
663nouveau_hwmon_init(struct drm_device *dev)
664{
665#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
666 struct nouveau_drm *drm = nouveau_drm(dev);
667 struct nvkm_therm *therm = nvxx_therm(&drm->device);
668 struct nvkm_volt *volt = nvxx_volt(&drm->device);
669 struct nvkm_iccsense *iccsense = nvxx_iccsense(&drm->device);
670 struct nouveau_hwmon *hwmon;
671 struct device *hwmon_dev;
672 int ret = 0;
673
674 hwmon = drm->hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
675 if (!hwmon)
676 return -ENOMEM;
677 hwmon->dev = dev;
678
679 hwmon_dev = hwmon_device_register(dev->dev);
680 if (IS_ERR(hwmon_dev)) {
681 ret = PTR_ERR(hwmon_dev);
682 NV_ERROR(drm, "Unable to register hwmon device: %d\n", ret);
683 return ret;
684 }
685 dev_set_drvdata(hwmon_dev, dev);
686
687 /* set the default attributes */
688 ret = sysfs_create_group(&hwmon_dev->kobj, &hwmon_default_attrgroup);
689 if (ret)
690 goto error;
691
692 if (therm && therm->attr_get && therm->attr_set) {
693 /* if the card has a working thermal sensor */
694 if (nvkm_therm_temp_get(therm) >= 0) {
695 ret = sysfs_create_group(&hwmon_dev->kobj, &hwmon_temp_attrgroup);
696 if (ret)
697 goto error;
698 }
699
700 /* if the card has a pwm fan */
701 /*XXX: incorrect, need better detection for this, some boards have
702 * the gpio entries for pwm fan control even when there's no
703 * actual fan connected to it... therm table? */
704 if (therm->fan_get && therm->fan_get(therm) >= 0) {
705 ret = sysfs_create_group(&hwmon_dev->kobj,
706 &hwmon_pwm_fan_attrgroup);
707 if (ret)
708 goto error;
709 }
710 }
711
712 /* if the card can read the fan rpm */
713 if (therm && nvkm_therm_fan_sense(therm) >= 0) {
714 ret = sysfs_create_group(&hwmon_dev->kobj,
715 &hwmon_fan_rpm_attrgroup);
716 if (ret)
717 goto error;
718 }
719
720 if (volt && nvkm_volt_get(volt) >= 0) {
721 ret = sysfs_create_group(&hwmon_dev->kobj,
722 &hwmon_in0_attrgroup);
723
724 if (ret)
725 goto error;
726 }
727
728 if (iccsense && iccsense->data_valid && !list_empty(&iccsense->rails)) {
729 ret = sysfs_create_group(&hwmon_dev->kobj,
730 &hwmon_power_attrgroup);
731 if (ret)
732 goto error;
733 }
734
735 hwmon->hwmon = hwmon_dev;
736
737 return 0;
738
739error:
740 NV_ERROR(drm, "Unable to create some hwmon sysfs files: %d\n", ret);
741 hwmon_device_unregister(hwmon_dev);
742 hwmon->hwmon = NULL;
743 return ret;
744#else
745 return 0;
746#endif
747}
748
749void
750nouveau_hwmon_fini(struct drm_device *dev)
751{
752#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
753 struct nouveau_hwmon *hwmon = nouveau_hwmon(dev);
754
755 if (hwmon->hwmon) {
756 sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_default_attrgroup);
757 sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_temp_attrgroup);
758 sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_pwm_fan_attrgroup);
759 sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_fan_rpm_attrgroup);
760 sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_in0_attrgroup);
761 sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_power_attrgroup);
762
763 hwmon_device_unregister(hwmon->hwmon);
764 }
765
766 nouveau_drm(dev)->hwmon = NULL;
767 kfree(hwmon);
768#endif
769}