drm/nouveau/pm: style fixes
[deliverable/linux.git] / drivers / gpu / drm / nouveau / nouveau_pm.c
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 #include "drmP.h"
26
27 #include "nouveau_drv.h"
28 #include "nouveau_pm.h"
29 #include "nouveau_gpio.h"
30
31 #ifdef CONFIG_ACPI
32 #include <linux/acpi.h>
33 #endif
34 #include <linux/power_supply.h>
35 #include <linux/hwmon.h>
36 #include <linux/hwmon-sysfs.h>
37
38 static int
39 nouveau_pwmfan_get(struct drm_device *dev)
40 {
41 struct drm_nouveau_private *dev_priv = dev->dev_private;
42 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
43 struct gpio_func gpio;
44 u32 divs, duty;
45 int ret;
46
47 if (!pm->pwm_get)
48 return -ENODEV;
49
50 ret = nouveau_gpio_find(dev, 0, DCB_GPIO_PWM_FAN, 0xff, &gpio);
51 if (ret == 0) {
52 ret = pm->pwm_get(dev, gpio.line, &divs, &duty);
53 if (ret == 0) {
54 divs = max(divs, duty);
55 if (dev_priv->card_type <= NV_40 || (gpio.log[0] & 1))
56 duty = divs - duty;
57 return (duty * 100) / divs;
58 }
59
60 return nouveau_gpio_func_get(dev, gpio.func) * 100;
61 }
62
63 return -ENODEV;
64 }
65
66 static int
67 nouveau_pwmfan_set(struct drm_device *dev, int percent)
68 {
69 struct drm_nouveau_private *dev_priv = dev->dev_private;
70 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
71 struct gpio_func gpio;
72 u32 divs, duty;
73 int ret;
74
75 if (!pm->pwm_set)
76 return -ENODEV;
77
78 ret = nouveau_gpio_find(dev, 0, DCB_GPIO_PWM_FAN, 0xff, &gpio);
79 if (ret == 0) {
80 divs = pm->pwm_divisor;
81 if (pm->fan.pwm_freq) {
82 /*XXX: PNVIO clock more than likely... */
83 divs = 135000 / pm->fan.pwm_freq;
84 if (dev_priv->chipset < 0xa3)
85 divs /= 4;
86 }
87
88 duty = ((divs * percent) + 99) / 100;
89 if (dev_priv->card_type <= NV_40 || (gpio.log[0] & 1))
90 duty = divs - duty;
91
92 return pm->pwm_set(dev, gpio.line, divs, duty);
93 }
94
95 return -ENODEV;
96 }
97
98 static int
99 nouveau_pm_perflvl_aux(struct drm_device *dev, struct nouveau_pm_level *perflvl,
100 struct nouveau_pm_level *a, struct nouveau_pm_level *b)
101 {
102 struct drm_nouveau_private *dev_priv = dev->dev_private;
103 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
104 int ret;
105
106 /*XXX: not on all boards, we should control based on temperature
107 * on recent boards.. or maybe on some other factor we don't
108 * know about?
109 */
110 if (a->fanspeed && b->fanspeed && b->fanspeed > a->fanspeed) {
111 ret = nouveau_pwmfan_set(dev, perflvl->fanspeed);
112 if (ret && ret != -ENODEV) {
113 NV_ERROR(dev, "fanspeed set failed: %d\n", ret);
114 return ret;
115 }
116 }
117
118 if (pm->voltage.supported && pm->voltage_set) {
119 if (perflvl->volt_min && b->volt_min > a->volt_min) {
120 ret = pm->voltage_set(dev, perflvl->volt_min);
121 if (ret) {
122 NV_ERROR(dev, "voltage set failed: %d\n", ret);
123 return ret;
124 }
125 }
126 }
127
128 return 0;
129 }
130
131 static int
132 nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl)
133 {
134 struct drm_nouveau_private *dev_priv = dev->dev_private;
135 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
136 void *state;
137 int ret;
138
139 if (perflvl == pm->cur)
140 return 0;
141
142 ret = nouveau_pm_perflvl_aux(dev, perflvl, pm->cur, perflvl);
143 if (ret)
144 return ret;
145
146 state = pm->clocks_pre(dev, perflvl);
147 if (IS_ERR(state))
148 return PTR_ERR(state);
149 pm->clocks_set(dev, state);
150
151 ret = nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
152 if (ret)
153 return ret;
154
155 pm->cur = perflvl;
156 return 0;
157 }
158
159 static int
160 nouveau_pm_profile_set(struct drm_device *dev, const char *profile)
161 {
162 struct drm_nouveau_private *dev_priv = dev->dev_private;
163 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
164 struct nouveau_pm_level *perflvl = NULL;
165 long pl;
166
167 /* safety precaution, for now */
168 if (nouveau_perflvl_wr != 7777)
169 return -EPERM;
170
171 if (!strncmp(profile, "boot", 4))
172 perflvl = &pm->boot;
173 else {
174 int i;
175 if (kstrtol(profile, 10, &pl) == -EINVAL)
176 return -EINVAL;
177
178 for (i = 0; i < pm->nr_perflvl; i++) {
179 if (pm->perflvl[i].id == pl) {
180 perflvl = &pm->perflvl[i];
181 break;
182 }
183 }
184
185 if (!perflvl)
186 return -EINVAL;
187 }
188
189 NV_INFO(dev, "setting performance level: %s\n", profile);
190 return nouveau_pm_perflvl_set(dev, perflvl);
191 }
192
193 static int
194 nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
195 {
196 struct drm_nouveau_private *dev_priv = dev->dev_private;
197 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
198 int ret;
199
200 memset(perflvl, 0, sizeof(*perflvl));
201
202 ret = pm->clocks_get(dev, perflvl);
203 if (ret)
204 return ret;
205
206 if (pm->voltage.supported && pm->voltage_get) {
207 ret = pm->voltage_get(dev);
208 if (ret > 0) {
209 perflvl->volt_min = ret;
210 perflvl->volt_max = ret;
211 }
212 }
213
214 ret = nouveau_pwmfan_get(dev);
215 if (ret > 0)
216 perflvl->fanspeed = ret;
217
218 return 0;
219 }
220
221 static void
222 nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
223 {
224 char c[16], s[16], v[32], f[16], t[16], m[16];
225
226 c[0] = '\0';
227 if (perflvl->core)
228 snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000);
229
230 s[0] = '\0';
231 if (perflvl->shader)
232 snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
233
234 m[0] = '\0';
235 if (perflvl->memory)
236 snprintf(m, sizeof(m), " memory %dMHz", perflvl->memory / 1000);
237
238 v[0] = '\0';
239 if (perflvl->volt_min && perflvl->volt_min != perflvl->volt_max) {
240 snprintf(v, sizeof(v), " voltage %dmV-%dmV",
241 perflvl->volt_min / 1000, perflvl->volt_max / 1000);
242 } else
243 if (perflvl->volt_min) {
244 snprintf(v, sizeof(v), " voltage %dmV",
245 perflvl->volt_min / 1000);
246 }
247
248 f[0] = '\0';
249 if (perflvl->fanspeed)
250 snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
251
252 t[0] = '\0';
253 if (perflvl->timing)
254 snprintf(t, sizeof(t), " timing %d", perflvl->timing->id);
255
256 snprintf(ptr, len, "%s%s%s%s%s%s\n", c, s, m, t, v, f);
257 }
258
259 static ssize_t
260 nouveau_pm_get_perflvl_info(struct device *d,
261 struct device_attribute *a, char *buf)
262 {
263 struct nouveau_pm_level *perflvl = (struct nouveau_pm_level *)a;
264 char *ptr = buf;
265 int len = PAGE_SIZE;
266
267 snprintf(ptr, len, "%d:", perflvl->id);
268 ptr += strlen(buf);
269 len -= strlen(buf);
270
271 nouveau_pm_perflvl_info(perflvl, ptr, len);
272 return strlen(buf);
273 }
274
275 static ssize_t
276 nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
277 {
278 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
279 struct drm_nouveau_private *dev_priv = dev->dev_private;
280 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
281 struct nouveau_pm_level cur;
282 int len = PAGE_SIZE, ret;
283 char *ptr = buf;
284
285 if (!pm->cur)
286 snprintf(ptr, len, "setting: boot\n");
287 else if (pm->cur == &pm->boot)
288 snprintf(ptr, len, "setting: boot\nc:");
289 else
290 snprintf(ptr, len, "setting: static %d\nc:", pm->cur->id);
291 ptr += strlen(buf);
292 len -= strlen(buf);
293
294 ret = nouveau_pm_perflvl_get(dev, &cur);
295 if (ret == 0)
296 nouveau_pm_perflvl_info(&cur, ptr, len);
297 return strlen(buf);
298 }
299
300 static ssize_t
301 nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
302 const char *buf, size_t count)
303 {
304 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
305 int ret;
306
307 ret = nouveau_pm_profile_set(dev, buf);
308 if (ret)
309 return ret;
310 return strlen(buf);
311 }
312
313 static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
314 nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
315
316 static int
317 nouveau_sysfs_init(struct drm_device *dev)
318 {
319 struct drm_nouveau_private *dev_priv = dev->dev_private;
320 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
321 struct device *d = &dev->pdev->dev;
322 int ret, i;
323
324 ret = device_create_file(d, &dev_attr_performance_level);
325 if (ret)
326 return ret;
327
328 for (i = 0; i < pm->nr_perflvl; i++) {
329 struct nouveau_pm_level *perflvl = &pm->perflvl[i];
330
331 perflvl->dev_attr.attr.name = perflvl->name;
332 perflvl->dev_attr.attr.mode = S_IRUGO;
333 perflvl->dev_attr.show = nouveau_pm_get_perflvl_info;
334 perflvl->dev_attr.store = NULL;
335 sysfs_attr_init(&perflvl->dev_attr.attr);
336
337 ret = device_create_file(d, &perflvl->dev_attr);
338 if (ret) {
339 NV_ERROR(dev, "failed pervlvl %d sysfs: %d\n",
340 perflvl->id, i);
341 perflvl->dev_attr.attr.name = NULL;
342 nouveau_pm_fini(dev);
343 return ret;
344 }
345 }
346
347 return 0;
348 }
349
350 static void
351 nouveau_sysfs_fini(struct drm_device *dev)
352 {
353 struct drm_nouveau_private *dev_priv = dev->dev_private;
354 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
355 struct device *d = &dev->pdev->dev;
356 int i;
357
358 device_remove_file(d, &dev_attr_performance_level);
359 for (i = 0; i < pm->nr_perflvl; i++) {
360 struct nouveau_pm_level *pl = &pm->perflvl[i];
361
362 if (!pl->dev_attr.attr.name)
363 break;
364
365 device_remove_file(d, &pl->dev_attr);
366 }
367 }
368
369 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
370 static ssize_t
371 nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
372 {
373 struct drm_device *dev = dev_get_drvdata(d);
374 struct drm_nouveau_private *dev_priv = dev->dev_private;
375 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
376
377 return snprintf(buf, PAGE_SIZE, "%d\n", pm->temp_get(dev)*1000);
378 }
379 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
380 NULL, 0);
381
382 static ssize_t
383 nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
384 {
385 struct drm_device *dev = dev_get_drvdata(d);
386 struct drm_nouveau_private *dev_priv = dev->dev_private;
387 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
388 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
389
390 return snprintf(buf, PAGE_SIZE, "%d\n", temp->down_clock*1000);
391 }
392 static ssize_t
393 nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
394 const char *buf, size_t count)
395 {
396 struct drm_device *dev = dev_get_drvdata(d);
397 struct drm_nouveau_private *dev_priv = dev->dev_private;
398 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
399 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
400 long value;
401
402 if (kstrtol(buf, 10, &value) == -EINVAL)
403 return count;
404
405 temp->down_clock = value/1000;
406
407 nouveau_temp_safety_checks(dev);
408
409 return count;
410 }
411 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
412 nouveau_hwmon_set_max_temp,
413 0);
414
415 static ssize_t
416 nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
417 char *buf)
418 {
419 struct drm_device *dev = dev_get_drvdata(d);
420 struct drm_nouveau_private *dev_priv = dev->dev_private;
421 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
422 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
423
424 return snprintf(buf, PAGE_SIZE, "%d\n", temp->critical*1000);
425 }
426 static ssize_t
427 nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
428 const char *buf,
429 size_t count)
430 {
431 struct drm_device *dev = dev_get_drvdata(d);
432 struct drm_nouveau_private *dev_priv = dev->dev_private;
433 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
434 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
435 long value;
436
437 if (kstrtol(buf, 10, &value) == -EINVAL)
438 return count;
439
440 temp->critical = value/1000;
441
442 nouveau_temp_safety_checks(dev);
443
444 return count;
445 }
446 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
447 nouveau_hwmon_critical_temp,
448 nouveau_hwmon_set_critical_temp,
449 0);
450
451 static ssize_t nouveau_hwmon_show_name(struct device *dev,
452 struct device_attribute *attr,
453 char *buf)
454 {
455 return sprintf(buf, "nouveau\n");
456 }
457 static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
458
459 static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
460 struct device_attribute *attr,
461 char *buf)
462 {
463 return sprintf(buf, "1000\n");
464 }
465 static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
466 nouveau_hwmon_show_update_rate,
467 NULL, 0);
468
469 static ssize_t
470 nouveau_hwmon_show_fan0_input(struct device *d, struct device_attribute *attr,
471 char *buf)
472 {
473 struct drm_device *dev = dev_get_drvdata(d);
474 struct drm_nouveau_private *dev_priv = dev->dev_private;
475 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
476 struct gpio_func gpio;
477 u32 cycles, cur, prev;
478 u64 start;
479 int ret;
480
481 ret = nouveau_gpio_find(dev, 0, DCB_GPIO_FAN_SENSE, 0xff, &gpio);
482 if (ret)
483 return ret;
484
485 /* Monitor the GPIO input 0x3b for 250ms.
486 * When the fan spins, it changes the value of GPIO FAN_SENSE.
487 * We get 4 changes (0 -> 1 -> 0 -> 1 -> [...]) per complete rotation.
488 */
489 start = ptimer->read(dev);
490 prev = nouveau_gpio_sense(dev, 0, gpio.line);
491 cycles = 0;
492 do {
493 cur = nouveau_gpio_sense(dev, 0, gpio.line);
494 if (prev != cur) {
495 cycles++;
496 prev = cur;
497 }
498
499 usleep_range(500, 1000); /* supports 0 < rpm < 7500 */
500 } while (ptimer->read(dev) - start < 250000000);
501
502 /* interpolate to get rpm */
503 return sprintf(buf, "%i\n", cycles / 4 * 4 * 60);
504 }
505 static SENSOR_DEVICE_ATTR(fan0_input, S_IRUGO, nouveau_hwmon_show_fan0_input,
506 NULL, 0);
507
508 static ssize_t
509 nouveau_hwmon_get_pwm0(struct device *d, struct device_attribute *a, char *buf)
510 {
511 struct drm_device *dev = dev_get_drvdata(d);
512 int ret;
513
514 ret = nouveau_pwmfan_get(dev);
515 if (ret < 0)
516 return ret;
517
518 return sprintf(buf, "%i\n", ret);
519 }
520
521 static ssize_t
522 nouveau_hwmon_set_pwm0(struct device *d, struct device_attribute *a,
523 const char *buf, size_t count)
524 {
525 struct drm_device *dev = dev_get_drvdata(d);
526 struct drm_nouveau_private *dev_priv = dev->dev_private;
527 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
528 int ret = -ENODEV;
529 long value;
530
531 if (nouveau_perflvl_wr != 7777)
532 return -EPERM;
533
534 if (kstrtol(buf, 10, &value) == -EINVAL)
535 return -EINVAL;
536
537 if (value < pm->fan.min_duty)
538 value = pm->fan.min_duty;
539 if (value > pm->fan.max_duty)
540 value = pm->fan.max_duty;
541
542 ret = nouveau_pwmfan_set(dev, value);
543 if (ret)
544 return ret;
545
546 return count;
547 }
548
549 static SENSOR_DEVICE_ATTR(pwm0, S_IRUGO | S_IWUSR,
550 nouveau_hwmon_get_pwm0,
551 nouveau_hwmon_set_pwm0, 0);
552
553 static ssize_t
554 nouveau_hwmon_get_pwm0_min(struct device *d,
555 struct device_attribute *a, char *buf)
556 {
557 struct drm_device *dev = dev_get_drvdata(d);
558 struct drm_nouveau_private *dev_priv = dev->dev_private;
559 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
560
561 return sprintf(buf, "%i\n", pm->fan.min_duty);
562 }
563
564 static ssize_t
565 nouveau_hwmon_set_pwm0_min(struct device *d, struct device_attribute *a,
566 const char *buf, size_t count)
567 {
568 struct drm_device *dev = dev_get_drvdata(d);
569 struct drm_nouveau_private *dev_priv = dev->dev_private;
570 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
571 long value;
572
573 if (kstrtol(buf, 10, &value) == -EINVAL)
574 return -EINVAL;
575
576 if (value < 0)
577 value = 0;
578
579 if (pm->fan.max_duty - value < 10)
580 value = pm->fan.max_duty - 10;
581
582 if (value < 10)
583 pm->fan.min_duty = 10;
584 else
585 pm->fan.min_duty = value;
586
587 return count;
588 }
589
590 static SENSOR_DEVICE_ATTR(pwm0_min, S_IRUGO | S_IWUSR,
591 nouveau_hwmon_get_pwm0_min,
592 nouveau_hwmon_set_pwm0_min, 0);
593
594 static ssize_t
595 nouveau_hwmon_get_pwm0_max(struct device *d,
596 struct device_attribute *a, char *buf)
597 {
598 struct drm_device *dev = dev_get_drvdata(d);
599 struct drm_nouveau_private *dev_priv = dev->dev_private;
600 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
601
602 return sprintf(buf, "%i\n", pm->fan.max_duty);
603 }
604
605 static ssize_t
606 nouveau_hwmon_set_pwm0_max(struct device *d, struct device_attribute *a,
607 const char *buf, size_t count)
608 {
609 struct drm_device *dev = dev_get_drvdata(d);
610 struct drm_nouveau_private *dev_priv = dev->dev_private;
611 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
612 long value;
613
614 if (kstrtol(buf, 10, &value) == -EINVAL)
615 return -EINVAL;
616
617 if (value < 0)
618 value = 0;
619
620 if (value - pm->fan.min_duty < 10)
621 value = pm->fan.min_duty + 10;
622
623 if (value > 100)
624 pm->fan.max_duty = 100;
625 else
626 pm->fan.max_duty = value;
627
628 return count;
629 }
630
631 static SENSOR_DEVICE_ATTR(pwm0_max, S_IRUGO | S_IWUSR,
632 nouveau_hwmon_get_pwm0_max,
633 nouveau_hwmon_set_pwm0_max, 0);
634
635 static struct attribute *hwmon_attributes[] = {
636 &sensor_dev_attr_temp1_input.dev_attr.attr,
637 &sensor_dev_attr_temp1_max.dev_attr.attr,
638 &sensor_dev_attr_temp1_crit.dev_attr.attr,
639 &sensor_dev_attr_name.dev_attr.attr,
640 &sensor_dev_attr_update_rate.dev_attr.attr,
641 NULL
642 };
643 static struct attribute *hwmon_fan_rpm_attributes[] = {
644 &sensor_dev_attr_fan0_input.dev_attr.attr,
645 NULL
646 };
647 static struct attribute *hwmon_pwm_fan_attributes[] = {
648 &sensor_dev_attr_pwm0.dev_attr.attr,
649 &sensor_dev_attr_pwm0_min.dev_attr.attr,
650 &sensor_dev_attr_pwm0_max.dev_attr.attr,
651 NULL
652 };
653
654 static const struct attribute_group hwmon_attrgroup = {
655 .attrs = hwmon_attributes,
656 };
657 static const struct attribute_group hwmon_fan_rpm_attrgroup = {
658 .attrs = hwmon_fan_rpm_attributes,
659 };
660 static const struct attribute_group hwmon_pwm_fan_attrgroup = {
661 .attrs = hwmon_pwm_fan_attributes,
662 };
663 #endif
664
665 static int
666 nouveau_hwmon_init(struct drm_device *dev)
667 {
668 struct drm_nouveau_private *dev_priv = dev->dev_private;
669 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
670 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
671 struct device *hwmon_dev;
672 int ret = 0;
673
674 if (!pm->temp_get)
675 return -ENODEV;
676
677 hwmon_dev = hwmon_device_register(&dev->pdev->dev);
678 if (IS_ERR(hwmon_dev)) {
679 ret = PTR_ERR(hwmon_dev);
680 NV_ERROR(dev,
681 "Unable to register hwmon device: %d\n", ret);
682 return ret;
683 }
684 dev_set_drvdata(hwmon_dev, dev);
685
686 /* default sysfs entries */
687 ret = sysfs_create_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
688 if (ret) {
689 if (ret)
690 goto error;
691 }
692
693 /* if the card has a pwm fan */
694 /*XXX: incorrect, need better detection for this, some boards have
695 * the gpio entries for pwm fan control even when there's no
696 * actual fan connected to it... therm table? */
697 if (nouveau_pwmfan_get(dev) >= 0) {
698 ret = sysfs_create_group(&dev->pdev->dev.kobj,
699 &hwmon_pwm_fan_attrgroup);
700 if (ret)
701 goto error;
702 }
703
704 /* if the card can read the fan rpm */
705 if (nouveau_gpio_func_valid(dev, DCB_GPIO_FAN_SENSE)) {
706 ret = sysfs_create_group(&dev->pdev->dev.kobj,
707 &hwmon_fan_rpm_attrgroup);
708 if (ret)
709 goto error;
710 }
711
712 pm->hwmon = hwmon_dev;
713
714 return 0;
715
716 error:
717 NV_ERROR(dev, "Unable to create some hwmon sysfs files: %d\n", ret);
718 hwmon_device_unregister(hwmon_dev);
719 pm->hwmon = NULL;
720 return ret;
721 #else
722 pm->hwmon = NULL;
723 return 0;
724 #endif
725 }
726
727 static void
728 nouveau_hwmon_fini(struct drm_device *dev)
729 {
730 #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
731 struct drm_nouveau_private *dev_priv = dev->dev_private;
732 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
733
734 if (pm->hwmon) {
735 sysfs_remove_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
736 sysfs_remove_group(&dev->pdev->dev.kobj,
737 &hwmon_pwm_fan_attrgroup);
738 sysfs_remove_group(&dev->pdev->dev.kobj,
739 &hwmon_fan_rpm_attrgroup);
740
741 hwmon_device_unregister(pm->hwmon);
742 }
743 #endif
744 }
745
746 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
747 static int
748 nouveau_pm_acpi_event(struct notifier_block *nb, unsigned long val, void *data)
749 {
750 struct drm_nouveau_private *dev_priv =
751 container_of(nb, struct drm_nouveau_private, engine.pm.acpi_nb);
752 struct drm_device *dev = dev_priv->dev;
753 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
754
755 if (strcmp(entry->device_class, "ac_adapter") == 0) {
756 bool ac = power_supply_is_system_supplied();
757
758 NV_DEBUG(dev, "power supply changed: %s\n", ac ? "AC" : "DC");
759 }
760
761 return NOTIFY_OK;
762 }
763 #endif
764
765 int
766 nouveau_pm_init(struct drm_device *dev)
767 {
768 struct drm_nouveau_private *dev_priv = dev->dev_private;
769 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
770 char info[256];
771 int ret, i;
772
773 nouveau_mem_timing_init(dev);
774 nouveau_volt_init(dev);
775 nouveau_perf_init(dev);
776 nouveau_temp_init(dev);
777
778 NV_INFO(dev, "%d available performance level(s)\n", pm->nr_perflvl);
779 for (i = 0; i < pm->nr_perflvl; i++) {
780 nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
781 NV_INFO(dev, "%d:%s", pm->perflvl[i].id, info);
782 }
783
784 /* determine current ("boot") performance level */
785 ret = nouveau_pm_perflvl_get(dev, &pm->boot);
786 if (ret == 0) {
787 strncpy(pm->boot.name, "boot", 4);
788 pm->cur = &pm->boot;
789
790 nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
791 NV_INFO(dev, "c:%s", info);
792 }
793
794 /* switch performance levels now if requested */
795 if (nouveau_perflvl != NULL) {
796 ret = nouveau_pm_profile_set(dev, nouveau_perflvl);
797 if (ret) {
798 NV_ERROR(dev, "error setting perflvl \"%s\": %d\n",
799 nouveau_perflvl, ret);
800 }
801 }
802
803 nouveau_sysfs_init(dev);
804 nouveau_hwmon_init(dev);
805 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
806 pm->acpi_nb.notifier_call = nouveau_pm_acpi_event;
807 register_acpi_notifier(&pm->acpi_nb);
808 #endif
809
810 return 0;
811 }
812
813 void
814 nouveau_pm_fini(struct drm_device *dev)
815 {
816 struct drm_nouveau_private *dev_priv = dev->dev_private;
817 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
818
819 if (pm->cur != &pm->boot)
820 nouveau_pm_perflvl_set(dev, &pm->boot);
821
822 nouveau_temp_fini(dev);
823 nouveau_perf_fini(dev);
824 nouveau_volt_fini(dev);
825 nouveau_mem_timing_fini(dev);
826
827 #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
828 unregister_acpi_notifier(&pm->acpi_nb);
829 #endif
830 nouveau_hwmon_fini(dev);
831 nouveau_sysfs_fini(dev);
832 }
833
834 void
835 nouveau_pm_resume(struct drm_device *dev)
836 {
837 struct drm_nouveau_private *dev_priv = dev->dev_private;
838 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
839 struct nouveau_pm_level *perflvl;
840
841 if (!pm->cur || pm->cur == &pm->boot)
842 return;
843
844 perflvl = pm->cur;
845 pm->cur = &pm->boot;
846 nouveau_pm_perflvl_set(dev, perflvl);
847 }
This page took 0.048867 seconds and 5 git commands to generate.