Merge branch 'drm-next' of git://people.freedesktop.org/~airlied/linux
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / drivers / gpu / drm / nouveau / nouveau_pm.c
CommitLineData
330c5988
BS
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
6032649d
BS
25#ifdef CONFIG_ACPI
26#include <linux/acpi.h>
27#endif
28#include <linux/power_supply.h>
34e9d85a
MP
29#include <linux/hwmon.h>
30#include <linux/hwmon-sysfs.h>
31
612a9aab 32#include <drm/drmP.h>
a175094c 33
77145f1c
BS
34#include "nouveau_drm.h"
35#include "nouveau_pm.h"
a175094c 36
77145f1c
BS
37#include <subdev/gpio.h>
38#include <subdev/timer.h>
aa1b9b48 39#include <subdev/therm.h>
a175094c 40
77145f1c
BS
41MODULE_PARM_DESC(perflvl, "Performance level (default: boot)");
42static char *nouveau_perflvl;
43module_param_named(perflvl, nouveau_perflvl, charp, 0400);
a175094c 44
77145f1c
BS
45MODULE_PARM_DESC(perflvl_wr, "Allow perflvl changes (warning: dangerous!)");
46static int nouveau_perflvl_wr;
47module_param_named(perflvl_wr, nouveau_perflvl_wr, int, 0400);
a175094c 48
64f1c11a 49static int
0b627a0b
BS
50nouveau_pm_perflvl_aux(struct drm_device *dev, struct nouveau_pm_level *perflvl,
51 struct nouveau_pm_level *a, struct nouveau_pm_level *b)
64f1c11a 52{
77145f1c
BS
53 struct nouveau_drm *drm = nouveau_drm(dev);
54 struct nouveau_pm *pm = nouveau_pm(dev);
aa1b9b48 55 struct nouveau_therm *therm = nouveau_therm(drm);
64f1c11a
BS
56 int ret;
57
11b7d895
MP
58 /*XXX: not on all boards, we should control based on temperature
59 * on recent boards.. or maybe on some other factor we don't
60 * know about?
61 */
aa1b9b48
MP
62 if (therm && therm->fan_set &&
63 a->fanspeed && b->fanspeed && b->fanspeed > a->fanspeed) {
64 ret = therm->fan_set(therm, perflvl->fanspeed);
0b627a0b 65 if (ret && ret != -ENODEV) {
77145f1c 66 NV_ERROR(drm, "fanspeed set failed: %d\n", ret);
0b627a0b
BS
67 return ret;
68 }
771e1035
BS
69 }
70
0b627a0b 71 if (pm->voltage.supported && pm->voltage_set) {
d2edab4a 72 if (perflvl->volt_min && b->volt_min > a->volt_min) {
0b627a0b
BS
73 ret = pm->voltage_set(dev, perflvl->volt_min);
74 if (ret) {
77145f1c 75 NV_ERROR(drm, "voltage set failed: %d\n", ret);
0b627a0b
BS
76 return ret;
77 }
64f1c11a
BS
78 }
79 }
80
0b627a0b
BS
81 return 0;
82}
83
84static int
85nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl)
86{
77145f1c 87 struct nouveau_pm *pm = nouveau_pm(dev);
0b627a0b
BS
88 void *state;
89 int ret;
90
91 if (perflvl == pm->cur)
92 return 0;
93
94 ret = nouveau_pm_perflvl_aux(dev, perflvl, pm->cur, perflvl);
95 if (ret)
96 return ret;
97
ff2b6c6e 98 state = pm->clocks_pre(dev, perflvl);
b0103747
MP
99 if (IS_ERR(state)) {
100 ret = PTR_ERR(state);
101 goto error;
102 }
103 ret = pm->clocks_set(dev, state);
104 if (ret)
105 goto error;
64f1c11a 106
0b627a0b
BS
107 ret = nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
108 if (ret)
109 return ret;
110
64f1c11a
BS
111 pm->cur = perflvl;
112 return 0;
b0103747
MP
113
114error:
115 /* restore the fan speed and voltage before leaving */
116 nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
117 return ret;
64f1c11a
BS
118}
119
8d7bb400
BS
120void
121nouveau_pm_trigger(struct drm_device *dev)
122{
77145f1c
BS
123 struct nouveau_drm *drm = nouveau_drm(dev);
124 struct nouveau_timer *ptimer = nouveau_timer(drm->device);
125 struct nouveau_pm *pm = nouveau_pm(dev);
8d7bb400
BS
126 struct nouveau_pm_profile *profile = NULL;
127 struct nouveau_pm_level *perflvl = NULL;
128 int ret;
129
130 /* select power profile based on current power source */
131 if (power_supply_is_system_supplied())
132 profile = pm->profile_ac;
133 else
134 profile = pm->profile_dc;
135
25c53c10
BS
136 if (profile != pm->profile) {
137 pm->profile->func->fini(pm->profile);
138 pm->profile = profile;
139 pm->profile->func->init(pm->profile);
140 }
141
8d7bb400
BS
142 /* select performance level based on profile */
143 perflvl = profile->func->select(profile);
144
145 /* change perflvl, if necessary */
146 if (perflvl != pm->cur) {
77145f1c 147 u64 time0 = ptimer->read(ptimer);
8d7bb400 148
77145f1c 149 NV_INFO(drm, "setting performance level: %d", perflvl->id);
8d7bb400
BS
150 ret = nouveau_pm_perflvl_set(dev, perflvl);
151 if (ret)
77145f1c 152 NV_INFO(drm, "> reclocking failed: %d\n\n", ret);
8d7bb400 153
77145f1c
BS
154 NV_INFO(drm, "> reclocking took %lluns\n\n",
155 ptimer->read(ptimer) - time0);
8d7bb400
BS
156 }
157}
158
159static struct nouveau_pm_profile *
160profile_find(struct drm_device *dev, const char *string)
161{
77145f1c 162 struct nouveau_pm *pm = nouveau_pm(dev);
8d7bb400
BS
163 struct nouveau_pm_profile *profile;
164
165 list_for_each_entry(profile, &pm->profiles, head) {
166 if (!strncmp(profile->name, string, sizeof(profile->name)))
167 return profile;
168 }
169
170 return NULL;
171}
172
6f876986
BS
173static int
174nouveau_pm_profile_set(struct drm_device *dev, const char *profile)
175{
77145f1c 176 struct nouveau_pm *pm = nouveau_pm(dev);
8d7bb400
BS
177 struct nouveau_pm_profile *ac = NULL, *dc = NULL;
178 char string[16], *cur = string, *ptr;
6f876986
BS
179
180 /* safety precaution, for now */
181 if (nouveau_perflvl_wr != 7777)
182 return -EPERM;
183
8d7bb400 184 strncpy(string, profile, sizeof(string));
5799d9e2 185 string[sizeof(string) - 1] = 0;
8d7bb400
BS
186 if ((ptr = strchr(string, '\n')))
187 *ptr = '\0';
6f876986 188
8d7bb400
BS
189 ptr = strsep(&cur, ",");
190 if (ptr)
191 ac = profile_find(dev, ptr);
6f876986 192
8d7bb400
BS
193 ptr = strsep(&cur, ",");
194 if (ptr)
195 dc = profile_find(dev, ptr);
196 else
197 dc = ac;
b0103747 198
8d7bb400
BS
199 if (ac == NULL || dc == NULL)
200 return -EINVAL;
201
202 pm->profile_ac = ac;
203 pm->profile_dc = dc;
204 nouveau_pm_trigger(dev);
205 return 0;
6f876986
BS
206}
207
25c53c10
BS
208static void
209nouveau_pm_static_dummy(struct nouveau_pm_profile *profile)
210{
211}
212
8d7bb400
BS
213static struct nouveau_pm_level *
214nouveau_pm_static_select(struct nouveau_pm_profile *profile)
215{
216 return container_of(profile, struct nouveau_pm_level, profile);
217}
218
219const struct nouveau_pm_profile_func nouveau_pm_static_profile_func = {
25c53c10
BS
220 .destroy = nouveau_pm_static_dummy,
221 .init = nouveau_pm_static_dummy,
222 .fini = nouveau_pm_static_dummy,
8d7bb400
BS
223 .select = nouveau_pm_static_select,
224};
225
330c5988
BS
226static int
227nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
228{
aa1b9b48 229 struct nouveau_drm *drm = nouveau_drm(dev);
77145f1c 230 struct nouveau_pm *pm = nouveau_pm(dev);
aa1b9b48 231 struct nouveau_therm *therm = nouveau_therm(drm->device);
330c5988
BS
232 int ret;
233
330c5988
BS
234 memset(perflvl, 0, sizeof(*perflvl));
235
c11dd0da
BS
236 if (pm->clocks_get) {
237 ret = pm->clocks_get(dev, perflvl);
238 if (ret)
239 return ret;
240 }
330c5988
BS
241
242 if (pm->voltage.supported && pm->voltage_get) {
243 ret = pm->voltage_get(dev);
3b5565dd
BS
244 if (ret > 0) {
245 perflvl->volt_min = ret;
246 perflvl->volt_max = ret;
247 }
330c5988
BS
248 }
249
aa1b9b48
MP
250 if (therm && therm->fan_get) {
251 ret = therm->fan_get(therm);
252 if (ret >= 0)
253 perflvl->fanspeed = ret;
254 }
771e1035 255
fd99fd61 256 nouveau_mem_timing_read(dev, &perflvl->timing);
330c5988
BS
257 return 0;
258}
259
260static void
261nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
262{
085028ce 263 char c[16], s[16], v[32], f[16], m[16];
0fbb114a
FJ
264
265 c[0] = '\0';
266 if (perflvl->core)
267 snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000);
330c5988
BS
268
269 s[0] = '\0';
270 if (perflvl->shader)
271 snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
272
93dccbed
BS
273 m[0] = '\0';
274 if (perflvl->memory)
275 snprintf(m, sizeof(m), " memory %dMHz", perflvl->memory / 1000);
276
330c5988 277 v[0] = '\0';
3b5565dd
BS
278 if (perflvl->volt_min && perflvl->volt_min != perflvl->volt_max) {
279 snprintf(v, sizeof(v), " voltage %dmV-%dmV",
280 perflvl->volt_min / 1000, perflvl->volt_max / 1000);
281 } else
282 if (perflvl->volt_min) {
283 snprintf(v, sizeof(v), " voltage %dmV",
284 perflvl->volt_min / 1000);
285 }
330c5988
BS
286
287 f[0] = '\0';
288 if (perflvl->fanspeed)
289 snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
290
8d7bb400 291 snprintf(ptr, len, "%s%s%s%s%s\n", c, s, m, v, f);
330c5988
BS
292}
293
294static ssize_t
295nouveau_pm_get_perflvl_info(struct device *d,
296 struct device_attribute *a, char *buf)
297{
8d7bb400
BS
298 struct nouveau_pm_level *perflvl =
299 container_of(a, struct nouveau_pm_level, dev_attr);
330c5988
BS
300 char *ptr = buf;
301 int len = PAGE_SIZE;
302
93dccbed 303 snprintf(ptr, len, "%d:", perflvl->id);
330c5988
BS
304 ptr += strlen(buf);
305 len -= strlen(buf);
306
307 nouveau_pm_perflvl_info(perflvl, ptr, len);
308 return strlen(buf);
309}
310
311static ssize_t
312nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
313{
34e9d85a 314 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
77145f1c 315 struct nouveau_pm *pm = nouveau_pm(dev);
330c5988
BS
316 struct nouveau_pm_level cur;
317 int len = PAGE_SIZE, ret;
318 char *ptr = buf;
319
8d7bb400
BS
320 snprintf(ptr, len, "profile: %s, %s\nc:",
321 pm->profile_ac->name, pm->profile_dc->name);
330c5988
BS
322 ptr += strlen(buf);
323 len -= strlen(buf);
324
325 ret = nouveau_pm_perflvl_get(dev, &cur);
326 if (ret == 0)
327 nouveau_pm_perflvl_info(&cur, ptr, len);
328 return strlen(buf);
329}
330
331static ssize_t
332nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
333 const char *buf, size_t count)
334{
34e9d85a 335 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
6f876986
BS
336 int ret;
337
338 ret = nouveau_pm_profile_set(dev, buf);
339 if (ret)
340 return ret;
341 return strlen(buf);
330c5988
BS
342}
343
5c4abd09
FJ
344static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
345 nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
330c5988 346
34e9d85a
MP
347static int
348nouveau_sysfs_init(struct drm_device *dev)
330c5988 349{
77145f1c
BS
350 struct nouveau_drm *drm = nouveau_drm(dev);
351 struct nouveau_pm *pm = nouveau_pm(dev);
330c5988 352 struct device *d = &dev->pdev->dev;
330c5988
BS
353 int ret, i;
354
330c5988
BS
355 ret = device_create_file(d, &dev_attr_performance_level);
356 if (ret)
357 return ret;
358
359 for (i = 0; i < pm->nr_perflvl; i++) {
360 struct nouveau_pm_level *perflvl = &pm->perflvl[i];
361
362 perflvl->dev_attr.attr.name = perflvl->name;
363 perflvl->dev_attr.attr.mode = S_IRUGO;
364 perflvl->dev_attr.show = nouveau_pm_get_perflvl_info;
365 perflvl->dev_attr.store = NULL;
366 sysfs_attr_init(&perflvl->dev_attr.attr);
367
368 ret = device_create_file(d, &perflvl->dev_attr);
369 if (ret) {
77145f1c 370 NV_ERROR(drm, "failed pervlvl %d sysfs: %d\n",
330c5988
BS
371 perflvl->id, i);
372 perflvl->dev_attr.attr.name = NULL;
373 nouveau_pm_fini(dev);
374 return ret;
375 }
376 }
377
378 return 0;
379}
380
34e9d85a
MP
381static void
382nouveau_sysfs_fini(struct drm_device *dev)
330c5988 383{
77145f1c 384 struct nouveau_pm *pm = nouveau_pm(dev);
330c5988
BS
385 struct device *d = &dev->pdev->dev;
386 int i;
387
388 device_remove_file(d, &dev_attr_performance_level);
389 for (i = 0; i < pm->nr_perflvl; i++) {
390 struct nouveau_pm_level *pl = &pm->perflvl[i];
391
392 if (!pl->dev_attr.attr.name)
393 break;
394
395 device_remove_file(d, &pl->dev_attr);
396 }
34e9d85a
MP
397}
398
658e86ee 399#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
34e9d85a
MP
400static ssize_t
401nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
402{
403 struct drm_device *dev = dev_get_drvdata(d);
aa1b9b48
MP
404 struct nouveau_drm *drm = nouveau_drm(dev);
405 struct nouveau_therm *therm = nouveau_therm(drm->device);
34e9d85a 406
aa1b9b48 407 return snprintf(buf, PAGE_SIZE, "%d\n", therm->temp_get(therm) * 1000);
34e9d85a
MP
408}
409static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
410 NULL, 0);
411
412static ssize_t
413nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
414{
415 struct drm_device *dev = dev_get_drvdata(d);
aa1b9b48
MP
416 struct nouveau_drm *drm = nouveau_drm(dev);
417 struct nouveau_therm *therm = nouveau_therm(drm->device);
34e9d85a 418
aa1b9b48
MP
419 return snprintf(buf, PAGE_SIZE, "%d\n",
420 therm->attr_get(therm, NOUVEAU_THERM_ATTR_THRS_DOWN_CLK) * 1000);
34e9d85a
MP
421}
422static ssize_t
423nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
424 const char *buf, size_t count)
425{
426 struct drm_device *dev = dev_get_drvdata(d);
aa1b9b48
MP
427 struct nouveau_drm *drm = nouveau_drm(dev);
428 struct nouveau_therm *therm = nouveau_therm(drm->device);
34e9d85a
MP
429 long value;
430
ddb20055 431 if (kstrtol(buf, 10, &value) == -EINVAL)
34e9d85a
MP
432 return count;
433
aa1b9b48 434 therm->attr_set(therm, NOUVEAU_THERM_ATTR_THRS_DOWN_CLK, value / 1000);
34e9d85a
MP
435
436 return count;
437}
438static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
439 nouveau_hwmon_set_max_temp,
440 0);
441
442static ssize_t
443nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
444 char *buf)
445{
446 struct drm_device *dev = dev_get_drvdata(d);
aa1b9b48
MP
447 struct nouveau_drm *drm = nouveau_drm(dev);
448 struct nouveau_therm *therm = nouveau_therm(drm->device);
34e9d85a 449
aa1b9b48
MP
450 return snprintf(buf, PAGE_SIZE, "%d\n",
451 therm->attr_get(therm, NOUVEAU_THERM_ATTR_THRS_CRITICAL) * 1000);
34e9d85a
MP
452}
453static ssize_t
454nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
455 const char *buf,
456 size_t count)
457{
458 struct drm_device *dev = dev_get_drvdata(d);
aa1b9b48
MP
459 struct nouveau_drm *drm = nouveau_drm(dev);
460 struct nouveau_therm *therm = nouveau_therm(drm->device);
34e9d85a
MP
461 long value;
462
ddb20055 463 if (kstrtol(buf, 10, &value) == -EINVAL)
34e9d85a
MP
464 return count;
465
aa1b9b48 466 therm->attr_set(therm, NOUVEAU_THERM_ATTR_THRS_CRITICAL, value / 1000);
34e9d85a
MP
467
468 return count;
469}
470static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
471 nouveau_hwmon_critical_temp,
472 nouveau_hwmon_set_critical_temp,
473 0);
474
475static ssize_t nouveau_hwmon_show_name(struct device *dev,
476 struct device_attribute *attr,
477 char *buf)
478{
479 return sprintf(buf, "nouveau\n");
480}
481static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
482
483static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
484 struct device_attribute *attr,
485 char *buf)
486{
487 return sprintf(buf, "1000\n");
488}
489static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
490 nouveau_hwmon_show_update_rate,
491 NULL, 0);
492
11b7d895
MP
493static ssize_t
494nouveau_hwmon_show_fan0_input(struct device *d, struct device_attribute *attr,
495 char *buf)
496{
497 struct drm_device *dev = dev_get_drvdata(d);
77145f1c 498 struct nouveau_drm *drm = nouveau_drm(dev);
aa1b9b48 499 struct nouveau_therm *therm = nouveau_therm(drm->device);
11b7d895 500
aa1b9b48 501 return snprintf(buf, PAGE_SIZE, "%d\n", therm->fan_sense(therm));
11b7d895
MP
502}
503static SENSOR_DEVICE_ATTR(fan0_input, S_IRUGO, nouveau_hwmon_show_fan0_input,
504 NULL, 0);
505
2f951a5d
MP
506 static ssize_t
507nouveau_hwmon_get_pwm1_enable(struct device *d,
508 struct device_attribute *a, char *buf)
509{
510 struct drm_device *dev = dev_get_drvdata(d);
511 struct nouveau_drm *drm = nouveau_drm(dev);
512 struct nouveau_therm *therm = nouveau_therm(drm->device);
a0b25635 513 int ret;
11b7d895 514
2f951a5d
MP
515 ret = therm->attr_get(therm, NOUVEAU_THERM_ATTR_FAN_MODE);
516 if (ret < 0)
a0b25635 517 return ret;
11b7d895 518
2f951a5d
MP
519 return sprintf(buf, "%i\n", ret);
520}
11b7d895 521
2f951a5d
MP
522static ssize_t
523nouveau_hwmon_set_pwm1_enable(struct device *d, struct device_attribute *a,
524 const char *buf, size_t count)
525{
526 struct drm_device *dev = dev_get_drvdata(d);
527 struct nouveau_drm *drm = nouveau_drm(dev);
528 struct nouveau_therm *therm = nouveau_therm(drm->device);
529 long value;
530 int ret;
531
532 if (strict_strtol(buf, 10, &value) == -EINVAL)
533 return -EINVAL;
11b7d895 534
2f951a5d
MP
535 ret = therm->attr_set(therm, NOUVEAU_THERM_ATTR_FAN_MODE, value);
536 if (ret)
537 return ret;
538 else
539 return count;
11b7d895 540}
2f951a5d
MP
541static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
542 nouveau_hwmon_get_pwm1_enable,
543 nouveau_hwmon_set_pwm1_enable, 0);
11b7d895
MP
544
545static ssize_t
c9cbf135 546nouveau_hwmon_get_pwm1(struct device *d, struct device_attribute *a, char *buf)
11b7d895
MP
547{
548 struct drm_device *dev = dev_get_drvdata(d);
aa1b9b48
MP
549 struct nouveau_drm *drm = nouveau_drm(dev);
550 struct nouveau_therm *therm = nouveau_therm(drm->device);
a175094c 551 int ret;
11b7d895 552
aa1b9b48 553 ret = therm->fan_get(therm);
11b7d895
MP
554 if (ret < 0)
555 return ret;
556
557 return sprintf(buf, "%i\n", ret);
558}
559
560static ssize_t
c9cbf135 561nouveau_hwmon_set_pwm1(struct device *d, struct device_attribute *a,
11b7d895
MP
562 const char *buf, size_t count)
563{
564 struct drm_device *dev = dev_get_drvdata(d);
aa1b9b48
MP
565 struct nouveau_drm *drm = nouveau_drm(dev);
566 struct nouveau_therm *therm = nouveau_therm(drm->device);
11b7d895
MP
567 int ret = -ENODEV;
568 long value;
569
570 if (nouveau_perflvl_wr != 7777)
571 return -EPERM;
572
ddb20055 573 if (kstrtol(buf, 10, &value) == -EINVAL)
11b7d895
MP
574 return -EINVAL;
575
aa1b9b48 576 ret = therm->fan_set(therm, value);
11b7d895
MP
577 if (ret)
578 return ret;
579
580 return count;
581}
582
c9cbf135
MP
583static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR,
584 nouveau_hwmon_get_pwm1,
585 nouveau_hwmon_set_pwm1, 0);
11b7d895
MP
586
587static ssize_t
c9cbf135 588nouveau_hwmon_get_pwm1_min(struct device *d,
11b7d895
MP
589 struct device_attribute *a, char *buf)
590{
591 struct drm_device *dev = dev_get_drvdata(d);
aa1b9b48
MP
592 struct nouveau_drm *drm = nouveau_drm(dev);
593 struct nouveau_therm *therm = nouveau_therm(drm->device);
594 int ret;
595
596 ret = therm->attr_get(therm, NOUVEAU_THERM_ATTR_FAN_MIN_DUTY);
597 if (ret < 0)
598 return ret;
11b7d895 599
aa1b9b48 600 return sprintf(buf, "%i\n", ret);
11b7d895
MP
601}
602
603static ssize_t
c9cbf135 604nouveau_hwmon_set_pwm1_min(struct device *d, struct device_attribute *a,
11b7d895
MP
605 const char *buf, size_t count)
606{
607 struct drm_device *dev = dev_get_drvdata(d);
aa1b9b48
MP
608 struct nouveau_drm *drm = nouveau_drm(dev);
609 struct nouveau_therm *therm = nouveau_therm(drm->device);
11b7d895 610 long value;
aa1b9b48 611 int ret;
11b7d895 612
ddb20055 613 if (kstrtol(buf, 10, &value) == -EINVAL)
11b7d895
MP
614 return -EINVAL;
615
aa1b9b48
MP
616 ret = therm->attr_set(therm, NOUVEAU_THERM_ATTR_FAN_MIN_DUTY, value);
617 if (ret < 0)
618 return ret;
11b7d895
MP
619
620 return count;
621}
622
c9cbf135
MP
623static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO | S_IWUSR,
624 nouveau_hwmon_get_pwm1_min,
625 nouveau_hwmon_set_pwm1_min, 0);
11b7d895
MP
626
627static ssize_t
c9cbf135 628nouveau_hwmon_get_pwm1_max(struct device *d,
11b7d895
MP
629 struct device_attribute *a, char *buf)
630{
631 struct drm_device *dev = dev_get_drvdata(d);
aa1b9b48
MP
632 struct nouveau_drm *drm = nouveau_drm(dev);
633 struct nouveau_therm *therm = nouveau_therm(drm->device);
634 int ret;
11b7d895 635
aa1b9b48
MP
636 ret = therm->attr_get(therm, NOUVEAU_THERM_ATTR_FAN_MAX_DUTY);
637 if (ret < 0)
638 return ret;
11b7d895 639
aa1b9b48 640 return sprintf(buf, "%i\n", ret);
11b7d895
MP
641}
642
643static ssize_t
c9cbf135 644nouveau_hwmon_set_pwm1_max(struct device *d, struct device_attribute *a,
11b7d895
MP
645 const char *buf, size_t count)
646{
647 struct drm_device *dev = dev_get_drvdata(d);
aa1b9b48
MP
648 struct nouveau_drm *drm = nouveau_drm(dev);
649 struct nouveau_therm *therm = nouveau_therm(drm->device);
11b7d895 650 long value;
aa1b9b48 651 int ret;
11b7d895 652
ddb20055 653 if (kstrtol(buf, 10, &value) == -EINVAL)
11b7d895
MP
654 return -EINVAL;
655
aa1b9b48
MP
656 ret = therm->attr_set(therm, NOUVEAU_THERM_ATTR_FAN_MAX_DUTY, value);
657 if (ret < 0)
658 return ret;
11b7d895
MP
659
660 return count;
661}
662
c9cbf135
MP
663static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO | S_IWUSR,
664 nouveau_hwmon_get_pwm1_max,
665 nouveau_hwmon_set_pwm1_max, 0);
11b7d895 666
34e9d85a
MP
667static struct attribute *hwmon_attributes[] = {
668 &sensor_dev_attr_temp1_input.dev_attr.attr,
669 &sensor_dev_attr_temp1_max.dev_attr.attr,
670 &sensor_dev_attr_temp1_crit.dev_attr.attr,
671 &sensor_dev_attr_name.dev_attr.attr,
672 &sensor_dev_attr_update_rate.dev_attr.attr,
673 NULL
674};
11b7d895
MP
675static struct attribute *hwmon_fan_rpm_attributes[] = {
676 &sensor_dev_attr_fan0_input.dev_attr.attr,
677 NULL
678};
679static struct attribute *hwmon_pwm_fan_attributes[] = {
2f951a5d 680 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
c9cbf135
MP
681 &sensor_dev_attr_pwm1.dev_attr.attr,
682 &sensor_dev_attr_pwm1_min.dev_attr.attr,
683 &sensor_dev_attr_pwm1_max.dev_attr.attr,
11b7d895
MP
684 NULL
685};
34e9d85a
MP
686
687static const struct attribute_group hwmon_attrgroup = {
688 .attrs = hwmon_attributes,
689};
11b7d895
MP
690static const struct attribute_group hwmon_fan_rpm_attrgroup = {
691 .attrs = hwmon_fan_rpm_attributes,
692};
693static const struct attribute_group hwmon_pwm_fan_attrgroup = {
694 .attrs = hwmon_pwm_fan_attributes,
695};
b54262f3 696#endif
34e9d85a
MP
697
698static int
699nouveau_hwmon_init(struct drm_device *dev)
700{
77145f1c
BS
701 struct nouveau_pm *pm = nouveau_pm(dev);
702 struct nouveau_drm *drm = nouveau_drm(dev);
aa1b9b48 703 struct nouveau_therm *therm = nouveau_therm(drm->device);
77145f1c 704
095f979a 705#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
34e9d85a 706 struct device *hwmon_dev;
11b7d895 707 int ret = 0;
34e9d85a 708
aa1b9b48
MP
709 if (!therm || !therm->temp_get || !therm->attr_get ||
710 !therm->attr_set || therm->temp_get(therm) < 0)
8155cac4 711 return -ENODEV;
34e9d85a
MP
712
713 hwmon_dev = hwmon_device_register(&dev->pdev->dev);
714 if (IS_ERR(hwmon_dev)) {
715 ret = PTR_ERR(hwmon_dev);
77145f1c 716 NV_ERROR(drm, "Unable to register hwmon device: %d\n", ret);
34e9d85a
MP
717 return ret;
718 }
719 dev_set_drvdata(hwmon_dev, dev);
11b7d895
MP
720
721 /* default sysfs entries */
07cfe0e7 722 ret = sysfs_create_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
34e9d85a 723 if (ret) {
11b7d895
MP
724 if (ret)
725 goto error;
726 }
727
728 /* if the card has a pwm fan */
729 /*XXX: incorrect, need better detection for this, some boards have
730 * the gpio entries for pwm fan control even when there's no
731 * actual fan connected to it... therm table? */
aa1b9b48 732 if (therm->fan_get && therm->fan_get(therm) >= 0) {
11b7d895
MP
733 ret = sysfs_create_group(&dev->pdev->dev.kobj,
734 &hwmon_pwm_fan_attrgroup);
735 if (ret)
736 goto error;
737 }
738
739 /* if the card can read the fan rpm */
aa1b9b48 740 if (therm->fan_sense(therm) >= 0) {
11b7d895
MP
741 ret = sysfs_create_group(&dev->pdev->dev.kobj,
742 &hwmon_fan_rpm_attrgroup);
743 if (ret)
744 goto error;
34e9d85a
MP
745 }
746
8155cac4 747 pm->hwmon = hwmon_dev;
11b7d895 748
34e9d85a 749 return 0;
11b7d895
MP
750
751error:
77145f1c 752 NV_ERROR(drm, "Unable to create some hwmon sysfs files: %d\n", ret);
11b7d895
MP
753 hwmon_device_unregister(hwmon_dev);
754 pm->hwmon = NULL;
755 return ret;
756#else
757 pm->hwmon = NULL;
758 return 0;
759#endif
34e9d85a
MP
760}
761
762static void
763nouveau_hwmon_fini(struct drm_device *dev)
764{
658e86ee 765#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
77145f1c 766 struct nouveau_pm *pm = nouveau_pm(dev);
34e9d85a 767
8155cac4 768 if (pm->hwmon) {
8c06a3e0 769 sysfs_remove_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
ddb20055
MP
770 sysfs_remove_group(&dev->pdev->dev.kobj,
771 &hwmon_pwm_fan_attrgroup);
772 sysfs_remove_group(&dev->pdev->dev.kobj,
773 &hwmon_fan_rpm_attrgroup);
11b7d895 774
8155cac4 775 hwmon_device_unregister(pm->hwmon);
34e9d85a 776 }
b54262f3 777#endif
34e9d85a
MP
778}
779
1f962797 780#if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
6032649d
BS
781static int
782nouveau_pm_acpi_event(struct notifier_block *nb, unsigned long val, void *data)
783{
77145f1c
BS
784 struct nouveau_pm *pm = container_of(nb, struct nouveau_pm, acpi_nb);
785 struct nouveau_drm *drm = nouveau_drm(pm->dev);
6032649d
BS
786 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
787
788 if (strcmp(entry->device_class, "ac_adapter") == 0) {
789 bool ac = power_supply_is_system_supplied();
790
77145f1c
BS
791 NV_DEBUG(drm, "power supply changed: %s\n", ac ? "AC" : "DC");
792 nouveau_pm_trigger(pm->dev);
6032649d
BS
793 }
794
795 return NOTIFY_OK;
796}
797#endif
798
34e9d85a
MP
799int
800nouveau_pm_init(struct drm_device *dev)
801{
77145f1c
BS
802 struct nouveau_device *device = nouveau_dev(dev);
803 struct nouveau_drm *drm = nouveau_drm(dev);
804 struct nouveau_pm *pm;
34e9d85a
MP
805 char info[256];
806 int ret, i;
807
77145f1c
BS
808 pm = drm->pm = kzalloc(sizeof(*pm), GFP_KERNEL);
809 if (!pm)
810 return -ENOMEM;
811
812 pm->dev = dev;
813
814 if (device->card_type < NV_40) {
815 pm->clocks_get = nv04_pm_clocks_get;
816 pm->clocks_pre = nv04_pm_clocks_pre;
817 pm->clocks_set = nv04_pm_clocks_set;
818 if (nouveau_gpio(drm->device)) {
819 pm->voltage_get = nouveau_voltage_gpio_get;
820 pm->voltage_set = nouveau_voltage_gpio_set;
821 }
822 } else
823 if (device->card_type < NV_50) {
824 pm->clocks_get = nv40_pm_clocks_get;
825 pm->clocks_pre = nv40_pm_clocks_pre;
826 pm->clocks_set = nv40_pm_clocks_set;
827 pm->voltage_get = nouveau_voltage_gpio_get;
828 pm->voltage_set = nouveau_voltage_gpio_set;
77145f1c
BS
829 } else
830 if (device->card_type < NV_C0) {
831 if (device->chipset < 0xa3 ||
832 device->chipset == 0xaa ||
833 device->chipset == 0xac) {
834 pm->clocks_get = nv50_pm_clocks_get;
835 pm->clocks_pre = nv50_pm_clocks_pre;
836 pm->clocks_set = nv50_pm_clocks_set;
837 } else {
838 pm->clocks_get = nva3_pm_clocks_get;
839 pm->clocks_pre = nva3_pm_clocks_pre;
840 pm->clocks_set = nva3_pm_clocks_set;
841 }
842 pm->voltage_get = nouveau_voltage_gpio_get;
843 pm->voltage_set = nouveau_voltage_gpio_set;
77145f1c
BS
844 } else
845 if (device->card_type < NV_E0) {
846 pm->clocks_get = nvc0_pm_clocks_get;
847 pm->clocks_pre = nvc0_pm_clocks_pre;
848 pm->clocks_set = nvc0_pm_clocks_set;
849 pm->voltage_get = nouveau_voltage_gpio_get;
850 pm->voltage_set = nouveau_voltage_gpio_set;
77145f1c
BS
851 }
852
853
68a64cad 854 /* parse aux tables from vbios */
34e9d85a 855 nouveau_volt_init(dev);
34e9d85a 856
d89c8ce0 857 INIT_LIST_HEAD(&pm->profiles);
34e9d85a 858
68a64cad
BS
859 /* determine current ("boot") performance level */
860 ret = nouveau_pm_perflvl_get(dev, &pm->boot);
861 if (ret) {
77145f1c 862 NV_ERROR(drm, "failed to determine boot perflvl\n");
68a64cad
BS
863 return ret;
864 }
865
866 strncpy(pm->boot.name, "boot", 4);
8d7bb400
BS
867 strncpy(pm->boot.profile.name, "boot", 4);
868 pm->boot.profile.func = &nouveau_pm_static_profile_func;
869
8d7bb400
BS
870 list_add(&pm->boot.profile.head, &pm->profiles);
871
872 pm->profile_ac = &pm->boot.profile;
873 pm->profile_dc = &pm->boot.profile;
25c53c10 874 pm->profile = &pm->boot.profile;
68a64cad
BS
875 pm->cur = &pm->boot;
876
877 /* add performance levels from vbios */
878 nouveau_perf_init(dev);
879
880 /* display available performance levels */
77145f1c 881 NV_INFO(drm, "%d available performance level(s)\n", pm->nr_perflvl);
34e9d85a
MP
882 for (i = 0; i < pm->nr_perflvl; i++) {
883 nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
77145f1c 884 NV_INFO(drm, "%d:%s", pm->perflvl[i].id, info);
34e9d85a
MP
885 }
886
68a64cad 887 nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
77145f1c 888 NV_INFO(drm, "c:%s", info);
34e9d85a
MP
889
890 /* switch performance levels now if requested */
8d7bb400
BS
891 if (nouveau_perflvl != NULL)
892 nouveau_pm_profile_set(dev, nouveau_perflvl);
34e9d85a
MP
893
894 nouveau_sysfs_init(dev);
895 nouveau_hwmon_init(dev);
1f962797 896#if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
6032649d
BS
897 pm->acpi_nb.notifier_call = nouveau_pm_acpi_event;
898 register_acpi_notifier(&pm->acpi_nb);
899#endif
34e9d85a
MP
900
901 return 0;
902}
903
904void
905nouveau_pm_fini(struct drm_device *dev)
906{
77145f1c 907 struct nouveau_pm *pm = nouveau_pm(dev);
25c53c10
BS
908 struct nouveau_pm_profile *profile, *tmp;
909
910 list_for_each_entry_safe(profile, tmp, &pm->profiles, head) {
911 list_del(&profile->head);
912 profile->func->destroy(profile);
913 }
34e9d85a
MP
914
915 if (pm->cur != &pm->boot)
916 nouveau_pm_perflvl_set(dev, &pm->boot);
330c5988
BS
917
918 nouveau_perf_fini(dev);
919 nouveau_volt_fini(dev);
34e9d85a 920
1f962797 921#if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
6032649d
BS
922 unregister_acpi_notifier(&pm->acpi_nb);
923#endif
34e9d85a
MP
924 nouveau_hwmon_fini(dev);
925 nouveau_sysfs_fini(dev);
77145f1c
BS
926
927 nouveau_drm(dev)->pm = NULL;
928 kfree(pm);
330c5988
BS
929}
930
64f1c11a
BS
931void
932nouveau_pm_resume(struct drm_device *dev)
933{
77145f1c 934 struct nouveau_pm *pm = nouveau_pm(dev);
64f1c11a
BS
935 struct nouveau_pm_level *perflvl;
936
317495b2 937 if (!pm->cur || pm->cur == &pm->boot)
64f1c11a
BS
938 return;
939
940 perflvl = pm->cur;
941 pm->cur = &pm->boot;
942 nouveau_pm_perflvl_set(dev, perflvl);
943}