blob: 936b442a6ab7f3f2e09ecea341e3105df51ed630 [file] [log] [blame]
Ben Skeggs330c5982010-09-16 15:39:49 +10001/*
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
Ben Skeggs60326492010-10-12 12:31:32 +100025#ifdef CONFIG_ACPI
26#include <linux/acpi.h>
27#endif
28#include <linux/power_supply.h>
Martin Peres34e9d852010-09-22 20:54:22 +020029#include <linux/hwmon.h>
30#include <linux/hwmon-sysfs.h>
31
Linus Torvalds612a9aa2012-10-03 23:29:23 -070032#include <drm/drmP.h>
Ben Skeggsa1750942011-09-17 01:42:12 +100033
Ben Skeggs77145f12012-07-31 16:16:21 +100034#include "nouveau_drm.h"
35#include "nouveau_pm.h"
Ben Skeggsa1750942011-09-17 01:42:12 +100036
Ben Skeggs77145f12012-07-31 16:16:21 +100037#include <subdev/gpio.h>
38#include <subdev/timer.h>
Martin Peresaa1b9b42012-09-02 02:55:58 +020039#include <subdev/therm.h>
Ben Skeggsa1750942011-09-17 01:42:12 +100040
Ben Skeggs77145f12012-07-31 16:16:21 +100041MODULE_PARM_DESC(perflvl, "Performance level (default: boot)");
42static char *nouveau_perflvl;
43module_param_named(perflvl, nouveau_perflvl, charp, 0400);
Ben Skeggsa1750942011-09-17 01:42:12 +100044
Ben Skeggs77145f12012-07-31 16:16:21 +100045MODULE_PARM_DESC(perflvl_wr, "Allow perflvl changes (warning: dangerous!)");
46static int nouveau_perflvl_wr;
47module_param_named(perflvl_wr, nouveau_perflvl_wr, int, 0400);
Ben Skeggsa1750942011-09-17 01:42:12 +100048
49static int
Ben Skeggs0b627a02011-10-27 12:02:12 +100050nouveau_pm_perflvl_aux(struct drm_device *dev, struct nouveau_pm_level *perflvl,
51 struct nouveau_pm_level *a, struct nouveau_pm_level *b)
52{
Ben Skeggs77145f12012-07-31 16:16:21 +100053 struct nouveau_drm *drm = nouveau_drm(dev);
54 struct nouveau_pm *pm = nouveau_pm(dev);
Martin Peresa6fd5cf2012-10-04 00:44:19 +020055 struct nouveau_therm *therm = nouveau_therm(drm->device);
Ben Skeggs0b627a02011-10-27 12:02:12 +100056 int ret;
57
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 */
Martin Peresaa1b9b42012-09-02 02:55:58 +020062 if (therm && therm->fan_set &&
63 a->fanspeed && b->fanspeed && b->fanspeed > a->fanspeed) {
64 ret = therm->fan_set(therm, perflvl->fanspeed);
Ben Skeggs0b627a02011-10-27 12:02:12 +100065 if (ret && ret != -ENODEV) {
Ben Skeggs77145f12012-07-31 16:16:21 +100066 NV_ERROR(drm, "fanspeed set failed: %d\n", ret);
Ben Skeggs0b627a02011-10-27 12:02:12 +100067 }
68 }
69
70 if (pm->voltage.supported && pm->voltage_set) {
Ben Skeggsd2edab42011-11-10 13:20:14 +100071 if (perflvl->volt_min && b->volt_min > a->volt_min) {
Ben Skeggs0b627a02011-10-27 12:02:12 +100072 ret = pm->voltage_set(dev, perflvl->volt_min);
73 if (ret) {
Ben Skeggs77145f12012-07-31 16:16:21 +100074 NV_ERROR(drm, "voltage set failed: %d\n", ret);
Ben Skeggs0b627a02011-10-27 12:02:12 +100075 return ret;
76 }
77 }
78 }
79
80 return 0;
81}
82
83static int
Ben Skeggs64f1c112010-09-17 13:35:25 +100084nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl)
85{
Ben Skeggs77145f12012-07-31 16:16:21 +100086 struct nouveau_pm *pm = nouveau_pm(dev);
Ben Skeggsff2b6c62011-10-27 10:28:17 +100087 void *state;
Ben Skeggs64f1c112010-09-17 13:35:25 +100088 int ret;
89
90 if (perflvl == pm->cur)
91 return 0;
92
Ben Skeggs0b627a02011-10-27 12:02:12 +100093 ret = nouveau_pm_perflvl_aux(dev, perflvl, pm->cur, perflvl);
94 if (ret)
95 return ret;
Ben Skeggs64f1c112010-09-17 13:35:25 +100096
Ben Skeggsff2b6c62011-10-27 10:28:17 +100097 state = pm->clocks_pre(dev, perflvl);
Martin Peresb0103742011-11-03 00:03:06 +010098 if (IS_ERR(state)) {
99 ret = PTR_ERR(state);
100 goto error;
101 }
102 ret = pm->clocks_set(dev, state);
103 if (ret)
104 goto error;
Ben Skeggs64f1c112010-09-17 13:35:25 +1000105
Ben Skeggs0b627a02011-10-27 12:02:12 +1000106 ret = nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
107 if (ret)
108 return ret;
109
Ben Skeggs64f1c112010-09-17 13:35:25 +1000110 pm->cur = perflvl;
111 return 0;
Martin Peresb0103742011-11-03 00:03:06 +0100112
113error:
114 /* restore the fan speed and voltage before leaving */
115 nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
116 return ret;
Ben Skeggs64f1c112010-09-17 13:35:25 +1000117}
118
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000119void
120nouveau_pm_trigger(struct drm_device *dev)
121{
Ben Skeggs77145f12012-07-31 16:16:21 +1000122 struct nouveau_drm *drm = nouveau_drm(dev);
123 struct nouveau_timer *ptimer = nouveau_timer(drm->device);
124 struct nouveau_pm *pm = nouveau_pm(dev);
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000125 struct nouveau_pm_profile *profile = NULL;
126 struct nouveau_pm_level *perflvl = NULL;
127 int ret;
128
129 /* select power profile based on current power source */
130 if (power_supply_is_system_supplied())
131 profile = pm->profile_ac;
132 else
133 profile = pm->profile_dc;
134
Ben Skeggs25c53c12012-01-24 18:03:25 +1000135 if (profile != pm->profile) {
136 pm->profile->func->fini(pm->profile);
137 pm->profile = profile;
138 pm->profile->func->init(pm->profile);
139 }
140
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000141 /* select performance level based on profile */
142 perflvl = profile->func->select(profile);
143
144 /* change perflvl, if necessary */
145 if (perflvl != pm->cur) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000146 u64 time0 = ptimer->read(ptimer);
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000147
Ben Skeggs77145f12012-07-31 16:16:21 +1000148 NV_INFO(drm, "setting performance level: %d", perflvl->id);
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000149 ret = nouveau_pm_perflvl_set(dev, perflvl);
150 if (ret)
Ben Skeggs77145f12012-07-31 16:16:21 +1000151 NV_INFO(drm, "> reclocking failed: %d\n\n", ret);
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000152
Ben Skeggs77145f12012-07-31 16:16:21 +1000153 NV_INFO(drm, "> reclocking took %lluns\n\n",
154 ptimer->read(ptimer) - time0);
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000155 }
156}
157
158static struct nouveau_pm_profile *
159profile_find(struct drm_device *dev, const char *string)
160{
Ben Skeggs77145f12012-07-31 16:16:21 +1000161 struct nouveau_pm *pm = nouveau_pm(dev);
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000162 struct nouveau_pm_profile *profile;
163
164 list_for_each_entry(profile, &pm->profiles, head) {
165 if (!strncmp(profile->name, string, sizeof(profile->name)))
166 return profile;
167 }
168
169 return NULL;
170}
171
Ben Skeggs64f1c112010-09-17 13:35:25 +1000172static int
Ben Skeggs6f876982010-09-16 16:47:14 +1000173nouveau_pm_profile_set(struct drm_device *dev, const char *profile)
174{
Ben Skeggs77145f12012-07-31 16:16:21 +1000175 struct nouveau_pm *pm = nouveau_pm(dev);
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000176 struct nouveau_pm_profile *ac = NULL, *dc = NULL;
177 char string[16], *cur = string, *ptr;
Ben Skeggs6f876982010-09-16 16:47:14 +1000178
179 /* safety precaution, for now */
180 if (nouveau_perflvl_wr != 7777)
181 return -EPERM;
182
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000183 strncpy(string, profile, sizeof(string));
Jim Meyering5799d9e2012-04-17 21:27:54 +0200184 string[sizeof(string) - 1] = 0;
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000185 if ((ptr = strchr(string, '\n')))
186 *ptr = '\0';
Ben Skeggs6f876982010-09-16 16:47:14 +1000187
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000188 ptr = strsep(&cur, ",");
189 if (ptr)
190 ac = profile_find(dev, ptr);
Ben Skeggs6f876982010-09-16 16:47:14 +1000191
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000192 ptr = strsep(&cur, ",");
193 if (ptr)
194 dc = profile_find(dev, ptr);
195 else
196 dc = ac;
Ben Skeggs6f876982010-09-16 16:47:14 +1000197
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000198 if (ac == NULL || dc == NULL)
199 return -EINVAL;
Martin Peresb0103742011-11-03 00:03:06 +0100200
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000201 pm->profile_ac = ac;
202 pm->profile_dc = dc;
203 nouveau_pm_trigger(dev);
204 return 0;
Ben Skeggs6f876982010-09-16 16:47:14 +1000205}
206
Ben Skeggs25c53c12012-01-24 18:03:25 +1000207static void
208nouveau_pm_static_dummy(struct nouveau_pm_profile *profile)
209{
210}
211
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000212static struct nouveau_pm_level *
213nouveau_pm_static_select(struct nouveau_pm_profile *profile)
214{
215 return container_of(profile, struct nouveau_pm_level, profile);
216}
217
218const struct nouveau_pm_profile_func nouveau_pm_static_profile_func = {
Ben Skeggs25c53c12012-01-24 18:03:25 +1000219 .destroy = nouveau_pm_static_dummy,
220 .init = nouveau_pm_static_dummy,
221 .fini = nouveau_pm_static_dummy,
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000222 .select = nouveau_pm_static_select,
223};
224
Ben Skeggs6f876982010-09-16 16:47:14 +1000225static int
Ben Skeggs330c5982010-09-16 15:39:49 +1000226nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
227{
Martin Peresaa1b9b42012-09-02 02:55:58 +0200228 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +1000229 struct nouveau_pm *pm = nouveau_pm(dev);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200230 struct nouveau_therm *therm = nouveau_therm(drm->device);
Ben Skeggs330c5982010-09-16 15:39:49 +1000231 int ret;
232
Ben Skeggs330c5982010-09-16 15:39:49 +1000233 memset(perflvl, 0, sizeof(*perflvl));
234
Ben Skeggsc11dd0d2012-03-07 14:18:49 +1000235 if (pm->clocks_get) {
236 ret = pm->clocks_get(dev, perflvl);
237 if (ret)
238 return ret;
239 }
Ben Skeggs330c5982010-09-16 15:39:49 +1000240
241 if (pm->voltage.supported && pm->voltage_get) {
242 ret = pm->voltage_get(dev);
Ben Skeggs3b5565d2011-06-09 16:57:07 +1000243 if (ret > 0) {
244 perflvl->volt_min = ret;
245 perflvl->volt_max = ret;
246 }
Ben Skeggs330c5982010-09-16 15:39:49 +1000247 }
248
Martin Peresaa1b9b42012-09-02 02:55:58 +0200249 if (therm && therm->fan_get) {
250 ret = therm->fan_get(therm);
251 if (ret >= 0)
252 perflvl->fanspeed = ret;
253 }
Ben Skeggs771e1032011-07-28 11:01:21 +1000254
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000255 nouveau_mem_timing_read(dev, &perflvl->timing);
Ben Skeggs330c5982010-09-16 15:39:49 +1000256 return 0;
257}
258
259static void
260nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
261{
Ben Skeggs085028c2012-01-18 09:02:28 +1000262 char c[16], s[16], v[32], f[16], m[16];
Francisco Jerez0fbb1142010-09-20 16:18:28 +0200263
264 c[0] = '\0';
265 if (perflvl->core)
266 snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000);
Ben Skeggs330c5982010-09-16 15:39:49 +1000267
268 s[0] = '\0';
269 if (perflvl->shader)
270 snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
271
Ben Skeggs93dccbe2011-06-09 17:27:47 +1000272 m[0] = '\0';
273 if (perflvl->memory)
274 snprintf(m, sizeof(m), " memory %dMHz", perflvl->memory / 1000);
275
Ben Skeggs330c5982010-09-16 15:39:49 +1000276 v[0] = '\0';
Ben Skeggs3b5565d2011-06-09 16:57:07 +1000277 if (perflvl->volt_min && perflvl->volt_min != perflvl->volt_max) {
278 snprintf(v, sizeof(v), " voltage %dmV-%dmV",
279 perflvl->volt_min / 1000, perflvl->volt_max / 1000);
280 } else
281 if (perflvl->volt_min) {
282 snprintf(v, sizeof(v), " voltage %dmV",
283 perflvl->volt_min / 1000);
284 }
Ben Skeggs330c5982010-09-16 15:39:49 +1000285
286 f[0] = '\0';
287 if (perflvl->fanspeed)
288 snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
289
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000290 snprintf(ptr, len, "%s%s%s%s%s\n", c, s, m, v, f);
Ben Skeggs330c5982010-09-16 15:39:49 +1000291}
292
293static ssize_t
294nouveau_pm_get_perflvl_info(struct device *d,
295 struct device_attribute *a, char *buf)
296{
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000297 struct nouveau_pm_level *perflvl =
298 container_of(a, struct nouveau_pm_level, dev_attr);
Ben Skeggs330c5982010-09-16 15:39:49 +1000299 char *ptr = buf;
300 int len = PAGE_SIZE;
301
Ben Skeggs93dccbe2011-06-09 17:27:47 +1000302 snprintf(ptr, len, "%d:", perflvl->id);
Ben Skeggs330c5982010-09-16 15:39:49 +1000303 ptr += strlen(buf);
304 len -= strlen(buf);
305
306 nouveau_pm_perflvl_info(perflvl, ptr, len);
307 return strlen(buf);
308}
309
310static ssize_t
311nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
312{
Martin Peres34e9d852010-09-22 20:54:22 +0200313 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
Ben Skeggs77145f12012-07-31 16:16:21 +1000314 struct nouveau_pm *pm = nouveau_pm(dev);
Ben Skeggs330c5982010-09-16 15:39:49 +1000315 struct nouveau_pm_level cur;
316 int len = PAGE_SIZE, ret;
317 char *ptr = buf;
318
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000319 snprintf(ptr, len, "profile: %s, %s\nc:",
320 pm->profile_ac->name, pm->profile_dc->name);
Ben Skeggs330c5982010-09-16 15:39:49 +1000321 ptr += strlen(buf);
322 len -= strlen(buf);
323
324 ret = nouveau_pm_perflvl_get(dev, &cur);
325 if (ret == 0)
326 nouveau_pm_perflvl_info(&cur, ptr, len);
327 return strlen(buf);
328}
329
330static ssize_t
331nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
332 const char *buf, size_t count)
333{
Martin Peres34e9d852010-09-22 20:54:22 +0200334 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
Ben Skeggs6f876982010-09-16 16:47:14 +1000335 int ret;
336
337 ret = nouveau_pm_profile_set(dev, buf);
338 if (ret)
339 return ret;
340 return strlen(buf);
Ben Skeggs330c5982010-09-16 15:39:49 +1000341}
342
Francisco Jerez5c4abd02010-09-23 20:36:42 +0200343static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
344 nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
Ben Skeggs330c5982010-09-16 15:39:49 +1000345
Martin Peres34e9d852010-09-22 20:54:22 +0200346static int
347nouveau_sysfs_init(struct drm_device *dev)
Ben Skeggs330c5982010-09-16 15:39:49 +1000348{
Ben Skeggs77145f12012-07-31 16:16:21 +1000349 struct nouveau_drm *drm = nouveau_drm(dev);
350 struct nouveau_pm *pm = nouveau_pm(dev);
Ben Skeggs330c5982010-09-16 15:39:49 +1000351 struct device *d = &dev->pdev->dev;
Ben Skeggs330c5982010-09-16 15:39:49 +1000352 int ret, i;
353
Ben Skeggs330c5982010-09-16 15:39:49 +1000354 ret = device_create_file(d, &dev_attr_performance_level);
355 if (ret)
356 return ret;
357
358 for (i = 0; i < pm->nr_perflvl; i++) {
359 struct nouveau_pm_level *perflvl = &pm->perflvl[i];
360
361 perflvl->dev_attr.attr.name = perflvl->name;
362 perflvl->dev_attr.attr.mode = S_IRUGO;
363 perflvl->dev_attr.show = nouveau_pm_get_perflvl_info;
364 perflvl->dev_attr.store = NULL;
365 sysfs_attr_init(&perflvl->dev_attr.attr);
366
367 ret = device_create_file(d, &perflvl->dev_attr);
368 if (ret) {
Ben Skeggs77145f12012-07-31 16:16:21 +1000369 NV_ERROR(drm, "failed pervlvl %d sysfs: %d\n",
Ben Skeggs330c5982010-09-16 15:39:49 +1000370 perflvl->id, i);
371 perflvl->dev_attr.attr.name = NULL;
372 nouveau_pm_fini(dev);
373 return ret;
374 }
375 }
376
377 return 0;
378}
379
Martin Peres34e9d852010-09-22 20:54:22 +0200380static void
381nouveau_sysfs_fini(struct drm_device *dev)
Ben Skeggs330c5982010-09-16 15:39:49 +1000382{
Ben Skeggs77145f12012-07-31 16:16:21 +1000383 struct nouveau_pm *pm = nouveau_pm(dev);
Ben Skeggs330c5982010-09-16 15:39:49 +1000384 struct device *d = &dev->pdev->dev;
385 int i;
386
387 device_remove_file(d, &dev_attr_performance_level);
388 for (i = 0; i < pm->nr_perflvl; i++) {
389 struct nouveau_pm_level *pl = &pm->perflvl[i];
390
391 if (!pl->dev_attr.attr.name)
392 break;
393
394 device_remove_file(d, &pl->dev_attr);
395 }
Martin Peres34e9d852010-09-22 20:54:22 +0200396}
397
Ken Milmore658e86e2011-07-03 19:54:28 +0100398#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
Martin Peres34e9d852010-09-22 20:54:22 +0200399static ssize_t
400nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
401{
402 struct drm_device *dev = dev_get_drvdata(d);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200403 struct nouveau_drm *drm = nouveau_drm(dev);
404 struct nouveau_therm *therm = nouveau_therm(drm->device);
Martin Peres804ca902013-03-15 00:59:55 +0100405 int temp = therm->temp_get(therm);
Martin Peres34e9d852010-09-22 20:54:22 +0200406
Martin Peres804ca902013-03-15 00:59:55 +0100407 if (temp < 0)
408 return temp;
409
410 return snprintf(buf, PAGE_SIZE, "%d\n", temp * 1000);
Martin Peres34e9d852010-09-22 20:54:22 +0200411}
412static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
413 NULL, 0);
414
415static ssize_t
Martin Peres12e32892012-11-20 01:14:13 +0100416nouveau_hwmon_show_temp1_auto_point1_pwm(struct device *d,
417 struct device_attribute *a, char *buf)
418{
419 return snprintf(buf, PAGE_SIZE, "%d\n", 100);
420}
421static SENSOR_DEVICE_ATTR(temp1_auto_point1_pwm, S_IRUGO,
422 nouveau_hwmon_show_temp1_auto_point1_pwm, NULL, 0);
423
424static ssize_t
425nouveau_hwmon_temp1_auto_point1_temp(struct device *d,
426 struct device_attribute *a, char *buf)
427{
428 struct drm_device *dev = dev_get_drvdata(d);
429 struct nouveau_drm *drm = nouveau_drm(dev);
430 struct nouveau_therm *therm = nouveau_therm(drm->device);
431
432 return snprintf(buf, PAGE_SIZE, "%d\n",
433 therm->attr_get(therm, NOUVEAU_THERM_ATTR_THRS_FAN_BOOST) * 1000);
434}
435static ssize_t
436nouveau_hwmon_set_temp1_auto_point1_temp(struct device *d,
437 struct device_attribute *a,
438 const char *buf, size_t count)
439{
440 struct drm_device *dev = dev_get_drvdata(d);
441 struct nouveau_drm *drm = nouveau_drm(dev);
442 struct nouveau_therm *therm = nouveau_therm(drm->device);
443 long value;
444
445 if (kstrtol(buf, 10, &value) == -EINVAL)
446 return count;
447
448 therm->attr_set(therm, NOUVEAU_THERM_ATTR_THRS_FAN_BOOST,
449 value / 1000);
450
451 return count;
452}
453static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
454 nouveau_hwmon_temp1_auto_point1_temp,
455 nouveau_hwmon_set_temp1_auto_point1_temp, 0);
456
457static ssize_t
458nouveau_hwmon_temp1_auto_point1_temp_hyst(struct device *d,
459 struct device_attribute *a, char *buf)
460{
461 struct drm_device *dev = dev_get_drvdata(d);
462 struct nouveau_drm *drm = nouveau_drm(dev);
463 struct nouveau_therm *therm = nouveau_therm(drm->device);
464
465 return snprintf(buf, PAGE_SIZE, "%d\n",
466 therm->attr_get(therm, NOUVEAU_THERM_ATTR_THRS_FAN_BOOST_HYST) * 1000);
467}
468static ssize_t
469nouveau_hwmon_set_temp1_auto_point1_temp_hyst(struct device *d,
470 struct device_attribute *a,
471 const char *buf, size_t count)
472{
473 struct drm_device *dev = dev_get_drvdata(d);
474 struct nouveau_drm *drm = nouveau_drm(dev);
475 struct nouveau_therm *therm = nouveau_therm(drm->device);
476 long value;
477
478 if (kstrtol(buf, 10, &value) == -EINVAL)
479 return count;
480
481 therm->attr_set(therm, NOUVEAU_THERM_ATTR_THRS_FAN_BOOST_HYST,
482 value / 1000);
483
484 return count;
485}
486static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
487 nouveau_hwmon_temp1_auto_point1_temp_hyst,
488 nouveau_hwmon_set_temp1_auto_point1_temp_hyst, 0);
489
490static ssize_t
Martin Peres34e9d852010-09-22 20:54:22 +0200491nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
492{
493 struct drm_device *dev = dev_get_drvdata(d);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200494 struct nouveau_drm *drm = nouveau_drm(dev);
495 struct nouveau_therm *therm = nouveau_therm(drm->device);
Martin Peres34e9d852010-09-22 20:54:22 +0200496
Martin Peresaa1b9b42012-09-02 02:55:58 +0200497 return snprintf(buf, PAGE_SIZE, "%d\n",
498 therm->attr_get(therm, NOUVEAU_THERM_ATTR_THRS_DOWN_CLK) * 1000);
Martin Peres34e9d852010-09-22 20:54:22 +0200499}
500static ssize_t
501nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
502 const char *buf, size_t count)
503{
504 struct drm_device *dev = dev_get_drvdata(d);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200505 struct nouveau_drm *drm = nouveau_drm(dev);
506 struct nouveau_therm *therm = nouveau_therm(drm->device);
Martin Peres34e9d852010-09-22 20:54:22 +0200507 long value;
508
Martin Peresddb20052011-12-17 12:24:59 +0100509 if (kstrtol(buf, 10, &value) == -EINVAL)
Martin Peres34e9d852010-09-22 20:54:22 +0200510 return count;
511
Martin Peresaa1b9b42012-09-02 02:55:58 +0200512 therm->attr_set(therm, NOUVEAU_THERM_ATTR_THRS_DOWN_CLK, value / 1000);
Martin Peres34e9d852010-09-22 20:54:22 +0200513
514 return count;
515}
516static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
517 nouveau_hwmon_set_max_temp,
518 0);
519
520static ssize_t
Martin Peres12e32892012-11-20 01:14:13 +0100521nouveau_hwmon_max_temp_hyst(struct device *d, struct device_attribute *a,
522 char *buf)
523{
524 struct drm_device *dev = dev_get_drvdata(d);
525 struct nouveau_drm *drm = nouveau_drm(dev);
526 struct nouveau_therm *therm = nouveau_therm(drm->device);
527
528 return snprintf(buf, PAGE_SIZE, "%d\n",
529 therm->attr_get(therm, NOUVEAU_THERM_ATTR_THRS_DOWN_CLK_HYST) * 1000);
530}
531static ssize_t
532nouveau_hwmon_set_max_temp_hyst(struct device *d, struct device_attribute *a,
533 const char *buf, size_t count)
534{
535 struct drm_device *dev = dev_get_drvdata(d);
536 struct nouveau_drm *drm = nouveau_drm(dev);
537 struct nouveau_therm *therm = nouveau_therm(drm->device);
538 long value;
539
540 if (kstrtol(buf, 10, &value) == -EINVAL)
541 return count;
542
543 therm->attr_set(therm, NOUVEAU_THERM_ATTR_THRS_DOWN_CLK_HYST,
544 value / 1000);
545
546 return count;
547}
548static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
549 nouveau_hwmon_max_temp_hyst,
550 nouveau_hwmon_set_max_temp_hyst, 0);
551
552static ssize_t
Martin Peres34e9d852010-09-22 20:54:22 +0200553nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
554 char *buf)
555{
556 struct drm_device *dev = dev_get_drvdata(d);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200557 struct nouveau_drm *drm = nouveau_drm(dev);
558 struct nouveau_therm *therm = nouveau_therm(drm->device);
Martin Peres34e9d852010-09-22 20:54:22 +0200559
Martin Peresaa1b9b42012-09-02 02:55:58 +0200560 return snprintf(buf, PAGE_SIZE, "%d\n",
561 therm->attr_get(therm, NOUVEAU_THERM_ATTR_THRS_CRITICAL) * 1000);
Martin Peres34e9d852010-09-22 20:54:22 +0200562}
563static ssize_t
564nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
565 const char *buf,
566 size_t count)
567{
568 struct drm_device *dev = dev_get_drvdata(d);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200569 struct nouveau_drm *drm = nouveau_drm(dev);
570 struct nouveau_therm *therm = nouveau_therm(drm->device);
Martin Peres34e9d852010-09-22 20:54:22 +0200571 long value;
572
Martin Peresddb20052011-12-17 12:24:59 +0100573 if (kstrtol(buf, 10, &value) == -EINVAL)
Martin Peres34e9d852010-09-22 20:54:22 +0200574 return count;
575
Martin Peresaa1b9b42012-09-02 02:55:58 +0200576 therm->attr_set(therm, NOUVEAU_THERM_ATTR_THRS_CRITICAL, value / 1000);
Martin Peres34e9d852010-09-22 20:54:22 +0200577
578 return count;
579}
580static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
581 nouveau_hwmon_critical_temp,
582 nouveau_hwmon_set_critical_temp,
583 0);
584
Martin Peres12e32892012-11-20 01:14:13 +0100585static ssize_t
586nouveau_hwmon_critical_temp_hyst(struct device *d, struct device_attribute *a,
587 char *buf)
588{
589 struct drm_device *dev = dev_get_drvdata(d);
590 struct nouveau_drm *drm = nouveau_drm(dev);
591 struct nouveau_therm *therm = nouveau_therm(drm->device);
592
593 return snprintf(buf, PAGE_SIZE, "%d\n",
594 therm->attr_get(therm, NOUVEAU_THERM_ATTR_THRS_CRITICAL_HYST) * 1000);
595}
596static ssize_t
597nouveau_hwmon_set_critical_temp_hyst(struct device *d,
598 struct device_attribute *a,
599 const char *buf,
600 size_t count)
601{
602 struct drm_device *dev = dev_get_drvdata(d);
603 struct nouveau_drm *drm = nouveau_drm(dev);
604 struct nouveau_therm *therm = nouveau_therm(drm->device);
605 long value;
606
607 if (kstrtol(buf, 10, &value) == -EINVAL)
608 return count;
609
610 therm->attr_set(therm, NOUVEAU_THERM_ATTR_THRS_CRITICAL_HYST,
611 value / 1000);
612
613 return count;
614}
615static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO | S_IWUSR,
616 nouveau_hwmon_critical_temp_hyst,
617 nouveau_hwmon_set_critical_temp_hyst, 0);
618static ssize_t
619nouveau_hwmon_emergency_temp(struct device *d, struct device_attribute *a,
620 char *buf)
621{
622 struct drm_device *dev = dev_get_drvdata(d);
623 struct nouveau_drm *drm = nouveau_drm(dev);
624 struct nouveau_therm *therm = nouveau_therm(drm->device);
625
626 return snprintf(buf, PAGE_SIZE, "%d\n",
627 therm->attr_get(therm, NOUVEAU_THERM_ATTR_THRS_SHUTDOWN) * 1000);
628}
629static ssize_t
630nouveau_hwmon_set_emergency_temp(struct device *d, struct device_attribute *a,
631 const char *buf,
632 size_t count)
633{
634 struct drm_device *dev = dev_get_drvdata(d);
635 struct nouveau_drm *drm = nouveau_drm(dev);
636 struct nouveau_therm *therm = nouveau_therm(drm->device);
637 long value;
638
639 if (kstrtol(buf, 10, &value) == -EINVAL)
640 return count;
641
642 therm->attr_set(therm, NOUVEAU_THERM_ATTR_THRS_SHUTDOWN, value / 1000);
643
644 return count;
645}
646static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO | S_IWUSR,
647 nouveau_hwmon_emergency_temp,
648 nouveau_hwmon_set_emergency_temp,
649 0);
650
651static ssize_t
652nouveau_hwmon_emergency_temp_hyst(struct device *d, struct device_attribute *a,
653 char *buf)
654{
655 struct drm_device *dev = dev_get_drvdata(d);
656 struct nouveau_drm *drm = nouveau_drm(dev);
657 struct nouveau_therm *therm = nouveau_therm(drm->device);
658
659 return snprintf(buf, PAGE_SIZE, "%d\n",
660 therm->attr_get(therm, NOUVEAU_THERM_ATTR_THRS_SHUTDOWN_HYST) * 1000);
661}
662static ssize_t
663nouveau_hwmon_set_emergency_temp_hyst(struct device *d,
664 struct device_attribute *a,
665 const char *buf,
666 size_t count)
667{
668 struct drm_device *dev = dev_get_drvdata(d);
669 struct nouveau_drm *drm = nouveau_drm(dev);
670 struct nouveau_therm *therm = nouveau_therm(drm->device);
671 long value;
672
673 if (kstrtol(buf, 10, &value) == -EINVAL)
674 return count;
675
676 therm->attr_set(therm, NOUVEAU_THERM_ATTR_THRS_SHUTDOWN_HYST,
677 value / 1000);
678
679 return count;
680}
681static SENSOR_DEVICE_ATTR(temp1_emergency_hyst, S_IRUGO | S_IWUSR,
682 nouveau_hwmon_emergency_temp_hyst,
683 nouveau_hwmon_set_emergency_temp_hyst,
684 0);
685
Martin Peres34e9d852010-09-22 20:54:22 +0200686static ssize_t nouveau_hwmon_show_name(struct device *dev,
687 struct device_attribute *attr,
688 char *buf)
689{
690 return sprintf(buf, "nouveau\n");
691}
692static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
693
694static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
695 struct device_attribute *attr,
696 char *buf)
697{
698 return sprintf(buf, "1000\n");
699}
700static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
701 nouveau_hwmon_show_update_rate,
702 NULL, 0);
703
Martin Peres11b7d8952011-08-15 11:10:30 +1000704static ssize_t
Ben Skeggsb2c36312012-12-06 15:13:06 +1000705nouveau_hwmon_show_fan1_input(struct device *d, struct device_attribute *attr,
Martin Peres11b7d8952011-08-15 11:10:30 +1000706 char *buf)
707{
708 struct drm_device *dev = dev_get_drvdata(d);
Ben Skeggs77145f12012-07-31 16:16:21 +1000709 struct nouveau_drm *drm = nouveau_drm(dev);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200710 struct nouveau_therm *therm = nouveau_therm(drm->device);
Martin Peres11b7d8952011-08-15 11:10:30 +1000711
Martin Peresaa1b9b42012-09-02 02:55:58 +0200712 return snprintf(buf, PAGE_SIZE, "%d\n", therm->fan_sense(therm));
Martin Peres11b7d8952011-08-15 11:10:30 +1000713}
Ben Skeggsb2c36312012-12-06 15:13:06 +1000714static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, nouveau_hwmon_show_fan1_input,
Martin Peres11b7d8952011-08-15 11:10:30 +1000715 NULL, 0);
716
Martin Peres2f951a52012-09-04 13:52:00 +0200717 static ssize_t
718nouveau_hwmon_get_pwm1_enable(struct device *d,
719 struct device_attribute *a, char *buf)
Martin Peres11b7d8952011-08-15 11:10:30 +1000720{
721 struct drm_device *dev = dev_get_drvdata(d);
Martin Peres2f951a52012-09-04 13:52:00 +0200722 struct nouveau_drm *drm = nouveau_drm(dev);
723 struct nouveau_therm *therm = nouveau_therm(drm->device);
Ben Skeggsa1750942011-09-17 01:42:12 +1000724 int ret;
Martin Peres11b7d8952011-08-15 11:10:30 +1000725
Martin Peres2f951a52012-09-04 13:52:00 +0200726 ret = therm->attr_get(therm, NOUVEAU_THERM_ATTR_FAN_MODE);
Martin Peres11b7d8952011-08-15 11:10:30 +1000727 if (ret < 0)
728 return ret;
729
730 return sprintf(buf, "%i\n", ret);
731}
732
733static ssize_t
Martin Peres2f951a52012-09-04 13:52:00 +0200734nouveau_hwmon_set_pwm1_enable(struct device *d, struct device_attribute *a,
735 const char *buf, size_t count)
736{
737 struct drm_device *dev = dev_get_drvdata(d);
738 struct nouveau_drm *drm = nouveau_drm(dev);
739 struct nouveau_therm *therm = nouveau_therm(drm->device);
740 long value;
741 int ret;
742
743 if (strict_strtol(buf, 10, &value) == -EINVAL)
744 return -EINVAL;
745
746 ret = therm->attr_set(therm, NOUVEAU_THERM_ATTR_FAN_MODE, value);
747 if (ret)
748 return ret;
749 else
750 return count;
751}
752static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
753 nouveau_hwmon_get_pwm1_enable,
754 nouveau_hwmon_set_pwm1_enable, 0);
755
Martin Peres11b7d8952011-08-15 11:10:30 +1000756static ssize_t
Martin Peresc9cbf132012-09-04 13:39:40 +0200757nouveau_hwmon_get_pwm1(struct device *d, struct device_attribute *a, char *buf)
Martin Peres11b7d8952011-08-15 11:10:30 +1000758{
759 struct drm_device *dev = dev_get_drvdata(d);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200760 struct nouveau_drm *drm = nouveau_drm(dev);
761 struct nouveau_therm *therm = nouveau_therm(drm->device);
Martin Peres11b7d8952011-08-15 11:10:30 +1000762 int ret;
763
Martin Peresaa1b9b42012-09-02 02:55:58 +0200764 ret = therm->fan_get(therm);
Martin Peres11b7d8952011-08-15 11:10:30 +1000765 if (ret < 0)
766 return ret;
767
768 return sprintf(buf, "%i\n", ret);
769}
770
771static ssize_t
Martin Peresc9cbf132012-09-04 13:39:40 +0200772nouveau_hwmon_set_pwm1(struct device *d, struct device_attribute *a,
Martin Peres11b7d8952011-08-15 11:10:30 +1000773 const char *buf, size_t count)
774{
775 struct drm_device *dev = dev_get_drvdata(d);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200776 struct nouveau_drm *drm = nouveau_drm(dev);
777 struct nouveau_therm *therm = nouveau_therm(drm->device);
Martin Peres11b7d8952011-08-15 11:10:30 +1000778 int ret = -ENODEV;
779 long value;
780
781 if (nouveau_perflvl_wr != 7777)
782 return -EPERM;
783
Martin Peresddb20052011-12-17 12:24:59 +0100784 if (kstrtol(buf, 10, &value) == -EINVAL)
Martin Peres11b7d8952011-08-15 11:10:30 +1000785 return -EINVAL;
786
Martin Peresaa1b9b42012-09-02 02:55:58 +0200787 ret = therm->fan_set(therm, value);
Martin Peres11b7d8952011-08-15 11:10:30 +1000788 if (ret)
789 return ret;
790
791 return count;
792}
793
Martin Peresc9cbf132012-09-04 13:39:40 +0200794static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR,
795 nouveau_hwmon_get_pwm1,
796 nouveau_hwmon_set_pwm1, 0);
Martin Peres11b7d8952011-08-15 11:10:30 +1000797
798static ssize_t
Martin Peresc9cbf132012-09-04 13:39:40 +0200799nouveau_hwmon_get_pwm1_min(struct device *d,
Martin Peres11b7d8952011-08-15 11:10:30 +1000800 struct device_attribute *a, char *buf)
801{
802 struct drm_device *dev = dev_get_drvdata(d);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200803 struct nouveau_drm *drm = nouveau_drm(dev);
804 struct nouveau_therm *therm = nouveau_therm(drm->device);
805 int ret;
Martin Peres11b7d8952011-08-15 11:10:30 +1000806
Martin Peresaa1b9b42012-09-02 02:55:58 +0200807 ret = therm->attr_get(therm, NOUVEAU_THERM_ATTR_FAN_MIN_DUTY);
808 if (ret < 0)
809 return ret;
810
811 return sprintf(buf, "%i\n", ret);
Martin Peres11b7d8952011-08-15 11:10:30 +1000812}
813
814static ssize_t
Martin Peresc9cbf132012-09-04 13:39:40 +0200815nouveau_hwmon_set_pwm1_min(struct device *d, struct device_attribute *a,
Martin Peres11b7d8952011-08-15 11:10:30 +1000816 const char *buf, size_t count)
817{
818 struct drm_device *dev = dev_get_drvdata(d);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200819 struct nouveau_drm *drm = nouveau_drm(dev);
820 struct nouveau_therm *therm = nouveau_therm(drm->device);
Martin Peres11b7d8952011-08-15 11:10:30 +1000821 long value;
Martin Peresaa1b9b42012-09-02 02:55:58 +0200822 int ret;
Martin Peres11b7d8952011-08-15 11:10:30 +1000823
Martin Peresddb20052011-12-17 12:24:59 +0100824 if (kstrtol(buf, 10, &value) == -EINVAL)
Martin Peres11b7d8952011-08-15 11:10:30 +1000825 return -EINVAL;
826
Martin Peresaa1b9b42012-09-02 02:55:58 +0200827 ret = therm->attr_set(therm, NOUVEAU_THERM_ATTR_FAN_MIN_DUTY, value);
828 if (ret < 0)
829 return ret;
Martin Peres11b7d8952011-08-15 11:10:30 +1000830
831 return count;
832}
833
Martin Peresc9cbf132012-09-04 13:39:40 +0200834static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO | S_IWUSR,
835 nouveau_hwmon_get_pwm1_min,
836 nouveau_hwmon_set_pwm1_min, 0);
Martin Peres11b7d8952011-08-15 11:10:30 +1000837
838static ssize_t
Martin Peresc9cbf132012-09-04 13:39:40 +0200839nouveau_hwmon_get_pwm1_max(struct device *d,
Martin Peres11b7d8952011-08-15 11:10:30 +1000840 struct device_attribute *a, char *buf)
841{
842 struct drm_device *dev = dev_get_drvdata(d);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200843 struct nouveau_drm *drm = nouveau_drm(dev);
844 struct nouveau_therm *therm = nouveau_therm(drm->device);
845 int ret;
Martin Peres11b7d8952011-08-15 11:10:30 +1000846
Martin Peresaa1b9b42012-09-02 02:55:58 +0200847 ret = therm->attr_get(therm, NOUVEAU_THERM_ATTR_FAN_MAX_DUTY);
848 if (ret < 0)
849 return ret;
850
851 return sprintf(buf, "%i\n", ret);
Martin Peres11b7d8952011-08-15 11:10:30 +1000852}
853
854static ssize_t
Martin Peresc9cbf132012-09-04 13:39:40 +0200855nouveau_hwmon_set_pwm1_max(struct device *d, struct device_attribute *a,
Martin Peres11b7d8952011-08-15 11:10:30 +1000856 const char *buf, size_t count)
857{
858 struct drm_device *dev = dev_get_drvdata(d);
Martin Peresaa1b9b42012-09-02 02:55:58 +0200859 struct nouveau_drm *drm = nouveau_drm(dev);
860 struct nouveau_therm *therm = nouveau_therm(drm->device);
Martin Peres11b7d8952011-08-15 11:10:30 +1000861 long value;
Martin Peresaa1b9b42012-09-02 02:55:58 +0200862 int ret;
Martin Peres11b7d8952011-08-15 11:10:30 +1000863
Martin Peresddb20052011-12-17 12:24:59 +0100864 if (kstrtol(buf, 10, &value) == -EINVAL)
Martin Peres11b7d8952011-08-15 11:10:30 +1000865 return -EINVAL;
866
Martin Peresaa1b9b42012-09-02 02:55:58 +0200867 ret = therm->attr_set(therm, NOUVEAU_THERM_ATTR_FAN_MAX_DUTY, value);
868 if (ret < 0)
869 return ret;
Martin Peres11b7d8952011-08-15 11:10:30 +1000870
871 return count;
872}
873
Martin Peresc9cbf132012-09-04 13:39:40 +0200874static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO | S_IWUSR,
875 nouveau_hwmon_get_pwm1_max,
876 nouveau_hwmon_set_pwm1_max, 0);
Martin Peres11b7d8952011-08-15 11:10:30 +1000877
Martin Peres804ca902013-03-15 00:59:55 +0100878static struct attribute *hwmon_default_attributes[] = {
879 &sensor_dev_attr_name.dev_attr.attr,
880 &sensor_dev_attr_update_rate.dev_attr.attr,
881 NULL
882};
883static struct attribute *hwmon_temp_attributes[] = {
Martin Peres34e9d852010-09-22 20:54:22 +0200884 &sensor_dev_attr_temp1_input.dev_attr.attr,
Martin Peres12e32892012-11-20 01:14:13 +0100885 &sensor_dev_attr_temp1_auto_point1_pwm.dev_attr.attr,
886 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
887 &sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr.attr,
Martin Peres34e9d852010-09-22 20:54:22 +0200888 &sensor_dev_attr_temp1_max.dev_attr.attr,
Martin Peres12e32892012-11-20 01:14:13 +0100889 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
Martin Peres34e9d852010-09-22 20:54:22 +0200890 &sensor_dev_attr_temp1_crit.dev_attr.attr,
Martin Peres12e32892012-11-20 01:14:13 +0100891 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
892 &sensor_dev_attr_temp1_emergency.dev_attr.attr,
893 &sensor_dev_attr_temp1_emergency_hyst.dev_attr.attr,
Martin Peres34e9d852010-09-22 20:54:22 +0200894 NULL
895};
Martin Peres11b7d8952011-08-15 11:10:30 +1000896static struct attribute *hwmon_fan_rpm_attributes[] = {
Ben Skeggsb2c36312012-12-06 15:13:06 +1000897 &sensor_dev_attr_fan1_input.dev_attr.attr,
Martin Peres11b7d8952011-08-15 11:10:30 +1000898 NULL
899};
900static struct attribute *hwmon_pwm_fan_attributes[] = {
Martin Peres2f951a52012-09-04 13:52:00 +0200901 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Martin Peresc9cbf132012-09-04 13:39:40 +0200902 &sensor_dev_attr_pwm1.dev_attr.attr,
903 &sensor_dev_attr_pwm1_min.dev_attr.attr,
904 &sensor_dev_attr_pwm1_max.dev_attr.attr,
Martin Peres11b7d8952011-08-15 11:10:30 +1000905 NULL
906};
Martin Peres34e9d852010-09-22 20:54:22 +0200907
Martin Peres804ca902013-03-15 00:59:55 +0100908static const struct attribute_group hwmon_default_attrgroup = {
909 .attrs = hwmon_default_attributes,
910};
911static const struct attribute_group hwmon_temp_attrgroup = {
912 .attrs = hwmon_temp_attributes,
Martin Peres34e9d852010-09-22 20:54:22 +0200913};
Martin Peres11b7d8952011-08-15 11:10:30 +1000914static const struct attribute_group hwmon_fan_rpm_attrgroup = {
915 .attrs = hwmon_fan_rpm_attributes,
916};
917static const struct attribute_group hwmon_pwm_fan_attrgroup = {
918 .attrs = hwmon_pwm_fan_attributes,
919};
Martin Peresb54262f2010-10-26 12:48:28 +0200920#endif
Martin Peres34e9d852010-09-22 20:54:22 +0200921
922static int
923nouveau_hwmon_init(struct drm_device *dev)
924{
Ben Skeggs77145f12012-07-31 16:16:21 +1000925 struct nouveau_pm *pm = nouveau_pm(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +1000926
Dave Airlie095f9792012-01-10 10:13:16 +0000927#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
Guenter Roeck57cdf822013-01-01 03:28:00 -0800928 struct nouveau_drm *drm = nouveau_drm(dev);
929 struct nouveau_therm *therm = nouveau_therm(drm->device);
Martin Peres34e9d852010-09-22 20:54:22 +0200930 struct device *hwmon_dev;
Martin Peres11b7d8952011-08-15 11:10:30 +1000931 int ret = 0;
Martin Peres34e9d852010-09-22 20:54:22 +0200932
Martin Peresa5f5af82012-10-04 00:28:21 +0200933 if (!therm || !therm->temp_get || !therm->attr_get || !therm->attr_set)
Francisco Jerez8155cac2010-09-23 20:58:38 +0200934 return -ENODEV;
Martin Peres34e9d852010-09-22 20:54:22 +0200935
936 hwmon_dev = hwmon_device_register(&dev->pdev->dev);
937 if (IS_ERR(hwmon_dev)) {
938 ret = PTR_ERR(hwmon_dev);
Ben Skeggs77145f12012-07-31 16:16:21 +1000939 NV_ERROR(drm, "Unable to register hwmon device: %d\n", ret);
Martin Peres34e9d852010-09-22 20:54:22 +0200940 return ret;
941 }
942 dev_set_drvdata(hwmon_dev, dev);
Martin Peres11b7d8952011-08-15 11:10:30 +1000943
Martin Peres804ca902013-03-15 00:59:55 +0100944 /* set the default attributes */
945 ret = sysfs_create_group(&hwmon_dev->kobj, &hwmon_default_attrgroup);
Martin Peres34e9d852010-09-22 20:54:22 +0200946 if (ret) {
Martin Peres11b7d8952011-08-15 11:10:30 +1000947 if (ret)
948 goto error;
949 }
950
Martin Peres804ca902013-03-15 00:59:55 +0100951 /* if the card has a working thermal sensor */
952 if (therm->temp_get(therm) >= 0) {
953 ret = sysfs_create_group(&hwmon_dev->kobj, &hwmon_temp_attrgroup);
954 if (ret) {
955 if (ret)
956 goto error;
957 }
958 }
959
Martin Peres11b7d8952011-08-15 11:10:30 +1000960 /* if the card has a pwm fan */
961 /*XXX: incorrect, need better detection for this, some boards have
962 * the gpio entries for pwm fan control even when there's no
963 * actual fan connected to it... therm table? */
Martin Peresaa1b9b42012-09-02 02:55:58 +0200964 if (therm->fan_get && therm->fan_get(therm) >= 0) {
Ben Skeggs5e90a882012-12-06 15:17:12 +1000965 ret = sysfs_create_group(&hwmon_dev->kobj,
Martin Peres11b7d8952011-08-15 11:10:30 +1000966 &hwmon_pwm_fan_attrgroup);
967 if (ret)
968 goto error;
969 }
970
971 /* if the card can read the fan rpm */
Martin Peresaa1b9b42012-09-02 02:55:58 +0200972 if (therm->fan_sense(therm) >= 0) {
Ben Skeggs5e90a882012-12-06 15:17:12 +1000973 ret = sysfs_create_group(&hwmon_dev->kobj,
Martin Peres11b7d8952011-08-15 11:10:30 +1000974 &hwmon_fan_rpm_attrgroup);
975 if (ret)
976 goto error;
Martin Peres34e9d852010-09-22 20:54:22 +0200977 }
978
Francisco Jerez8155cac2010-09-23 20:58:38 +0200979 pm->hwmon = hwmon_dev;
Martin Peres11b7d8952011-08-15 11:10:30 +1000980
Martin Peres34e9d852010-09-22 20:54:22 +0200981 return 0;
Martin Peres11b7d8952011-08-15 11:10:30 +1000982
983error:
Ben Skeggs77145f12012-07-31 16:16:21 +1000984 NV_ERROR(drm, "Unable to create some hwmon sysfs files: %d\n", ret);
Martin Peres11b7d8952011-08-15 11:10:30 +1000985 hwmon_device_unregister(hwmon_dev);
986 pm->hwmon = NULL;
987 return ret;
988#else
989 pm->hwmon = NULL;
990 return 0;
991#endif
Martin Peres34e9d852010-09-22 20:54:22 +0200992}
993
994static void
995nouveau_hwmon_fini(struct drm_device *dev)
996{
Ken Milmore658e86e2011-07-03 19:54:28 +0100997#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
Ben Skeggs77145f12012-07-31 16:16:21 +1000998 struct nouveau_pm *pm = nouveau_pm(dev);
Martin Peres34e9d852010-09-22 20:54:22 +0200999
Francisco Jerez8155cac2010-09-23 20:58:38 +02001000 if (pm->hwmon) {
Martin Peres804ca902013-03-15 00:59:55 +01001001 sysfs_remove_group(&pm->hwmon->kobj, &hwmon_default_attrgroup);
1002 sysfs_remove_group(&pm->hwmon->kobj, &hwmon_temp_attrgroup);
1003 sysfs_remove_group(&pm->hwmon->kobj, &hwmon_pwm_fan_attrgroup);
1004 sysfs_remove_group(&pm->hwmon->kobj, &hwmon_fan_rpm_attrgroup);
Martin Peres11b7d8952011-08-15 11:10:30 +10001005
Francisco Jerez8155cac2010-09-23 20:58:38 +02001006 hwmon_device_unregister(pm->hwmon);
Martin Peres34e9d852010-09-22 20:54:22 +02001007 }
Martin Peresb54262f2010-10-26 12:48:28 +02001008#endif
Martin Peres34e9d852010-09-22 20:54:22 +02001009}
1010
Martin Peres1f962792011-04-12 00:55:44 +02001011#if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
Ben Skeggs60326492010-10-12 12:31:32 +10001012static int
1013nouveau_pm_acpi_event(struct notifier_block *nb, unsigned long val, void *data)
1014{
Ben Skeggs77145f12012-07-31 16:16:21 +10001015 struct nouveau_pm *pm = container_of(nb, struct nouveau_pm, acpi_nb);
1016 struct nouveau_drm *drm = nouveau_drm(pm->dev);
Ben Skeggs60326492010-10-12 12:31:32 +10001017 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
1018
1019 if (strcmp(entry->device_class, "ac_adapter") == 0) {
1020 bool ac = power_supply_is_system_supplied();
1021
Ben Skeggs77145f12012-07-31 16:16:21 +10001022 NV_DEBUG(drm, "power supply changed: %s\n", ac ? "AC" : "DC");
1023 nouveau_pm_trigger(pm->dev);
Ben Skeggs60326492010-10-12 12:31:32 +10001024 }
1025
1026 return NOTIFY_OK;
1027}
1028#endif
1029
Martin Peres34e9d852010-09-22 20:54:22 +02001030int
1031nouveau_pm_init(struct drm_device *dev)
1032{
Ben Skeggs77145f12012-07-31 16:16:21 +10001033 struct nouveau_device *device = nouveau_dev(dev);
1034 struct nouveau_drm *drm = nouveau_drm(dev);
1035 struct nouveau_pm *pm;
Martin Peres34e9d852010-09-22 20:54:22 +02001036 char info[256];
1037 int ret, i;
1038
Ben Skeggs77145f12012-07-31 16:16:21 +10001039 pm = drm->pm = kzalloc(sizeof(*pm), GFP_KERNEL);
1040 if (!pm)
1041 return -ENOMEM;
1042
1043 pm->dev = dev;
1044
1045 if (device->card_type < NV_40) {
1046 pm->clocks_get = nv04_pm_clocks_get;
1047 pm->clocks_pre = nv04_pm_clocks_pre;
1048 pm->clocks_set = nv04_pm_clocks_set;
1049 if (nouveau_gpio(drm->device)) {
1050 pm->voltage_get = nouveau_voltage_gpio_get;
1051 pm->voltage_set = nouveau_voltage_gpio_set;
1052 }
1053 } else
1054 if (device->card_type < NV_50) {
1055 pm->clocks_get = nv40_pm_clocks_get;
1056 pm->clocks_pre = nv40_pm_clocks_pre;
1057 pm->clocks_set = nv40_pm_clocks_set;
1058 pm->voltage_get = nouveau_voltage_gpio_get;
1059 pm->voltage_set = nouveau_voltage_gpio_set;
Ben Skeggs77145f12012-07-31 16:16:21 +10001060 } else
1061 if (device->card_type < NV_C0) {
1062 if (device->chipset < 0xa3 ||
1063 device->chipset == 0xaa ||
1064 device->chipset == 0xac) {
1065 pm->clocks_get = nv50_pm_clocks_get;
1066 pm->clocks_pre = nv50_pm_clocks_pre;
1067 pm->clocks_set = nv50_pm_clocks_set;
1068 } else {
1069 pm->clocks_get = nva3_pm_clocks_get;
1070 pm->clocks_pre = nva3_pm_clocks_pre;
1071 pm->clocks_set = nva3_pm_clocks_set;
1072 }
1073 pm->voltage_get = nouveau_voltage_gpio_get;
1074 pm->voltage_set = nouveau_voltage_gpio_set;
Ben Skeggs77145f12012-07-31 16:16:21 +10001075 } else
1076 if (device->card_type < NV_E0) {
1077 pm->clocks_get = nvc0_pm_clocks_get;
1078 pm->clocks_pre = nvc0_pm_clocks_pre;
1079 pm->clocks_set = nvc0_pm_clocks_set;
1080 pm->voltage_get = nouveau_voltage_gpio_get;
1081 pm->voltage_set = nouveau_voltage_gpio_set;
Ben Skeggs77145f12012-07-31 16:16:21 +10001082 }
1083
1084
Ben Skeggs68a64ca2012-01-23 13:47:02 +10001085 /* parse aux tables from vbios */
Martin Peres34e9d852010-09-22 20:54:22 +02001086 nouveau_volt_init(dev);
Martin Peres34e9d852010-09-22 20:54:22 +02001087
Dmitry Eremin-Solenikovd89c8ce2012-09-25 10:31:40 +04001088 INIT_LIST_HEAD(&pm->profiles);
Martin Peres34e9d852010-09-22 20:54:22 +02001089
Ben Skeggs68a64ca2012-01-23 13:47:02 +10001090 /* determine current ("boot") performance level */
1091 ret = nouveau_pm_perflvl_get(dev, &pm->boot);
1092 if (ret) {
Ben Skeggs77145f12012-07-31 16:16:21 +10001093 NV_ERROR(drm, "failed to determine boot perflvl\n");
Ben Skeggs68a64ca2012-01-23 13:47:02 +10001094 return ret;
1095 }
1096
1097 strncpy(pm->boot.name, "boot", 4);
Ben Skeggs8d7bb402012-01-24 15:59:07 +10001098 strncpy(pm->boot.profile.name, "boot", 4);
1099 pm->boot.profile.func = &nouveau_pm_static_profile_func;
1100
Ben Skeggs8d7bb402012-01-24 15:59:07 +10001101 list_add(&pm->boot.profile.head, &pm->profiles);
1102
1103 pm->profile_ac = &pm->boot.profile;
1104 pm->profile_dc = &pm->boot.profile;
Ben Skeggs25c53c12012-01-24 18:03:25 +10001105 pm->profile = &pm->boot.profile;
Ben Skeggs68a64ca2012-01-23 13:47:02 +10001106 pm->cur = &pm->boot;
1107
1108 /* add performance levels from vbios */
1109 nouveau_perf_init(dev);
1110
1111 /* display available performance levels */
Ben Skeggs77145f12012-07-31 16:16:21 +10001112 NV_INFO(drm, "%d available performance level(s)\n", pm->nr_perflvl);
Martin Peres34e9d852010-09-22 20:54:22 +02001113 for (i = 0; i < pm->nr_perflvl; i++) {
1114 nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
Ben Skeggs77145f12012-07-31 16:16:21 +10001115 NV_INFO(drm, "%d:%s", pm->perflvl[i].id, info);
Martin Peres34e9d852010-09-22 20:54:22 +02001116 }
1117
Ben Skeggs68a64ca2012-01-23 13:47:02 +10001118 nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
Ben Skeggs77145f12012-07-31 16:16:21 +10001119 NV_INFO(drm, "c:%s", info);
Martin Peres34e9d852010-09-22 20:54:22 +02001120
1121 /* switch performance levels now if requested */
Ben Skeggs8d7bb402012-01-24 15:59:07 +10001122 if (nouveau_perflvl != NULL)
1123 nouveau_pm_profile_set(dev, nouveau_perflvl);
Martin Peres34e9d852010-09-22 20:54:22 +02001124
1125 nouveau_sysfs_init(dev);
1126 nouveau_hwmon_init(dev);
Martin Peres1f962792011-04-12 00:55:44 +02001127#if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
Ben Skeggs60326492010-10-12 12:31:32 +10001128 pm->acpi_nb.notifier_call = nouveau_pm_acpi_event;
1129 register_acpi_notifier(&pm->acpi_nb);
1130#endif
Martin Peres34e9d852010-09-22 20:54:22 +02001131
1132 return 0;
1133}
1134
1135void
1136nouveau_pm_fini(struct drm_device *dev)
1137{
Ben Skeggs77145f12012-07-31 16:16:21 +10001138 struct nouveau_pm *pm = nouveau_pm(dev);
Ben Skeggs25c53c12012-01-24 18:03:25 +10001139 struct nouveau_pm_profile *profile, *tmp;
1140
1141 list_for_each_entry_safe(profile, tmp, &pm->profiles, head) {
1142 list_del(&profile->head);
1143 profile->func->destroy(profile);
1144 }
Martin Peres34e9d852010-09-22 20:54:22 +02001145
1146 if (pm->cur != &pm->boot)
1147 nouveau_pm_perflvl_set(dev, &pm->boot);
Ben Skeggs330c5982010-09-16 15:39:49 +10001148
1149 nouveau_perf_fini(dev);
1150 nouveau_volt_fini(dev);
Martin Peres34e9d852010-09-22 20:54:22 +02001151
Martin Peres1f962792011-04-12 00:55:44 +02001152#if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
Ben Skeggs60326492010-10-12 12:31:32 +10001153 unregister_acpi_notifier(&pm->acpi_nb);
1154#endif
Martin Peres34e9d852010-09-22 20:54:22 +02001155 nouveau_hwmon_fini(dev);
1156 nouveau_sysfs_fini(dev);
Ben Skeggs77145f12012-07-31 16:16:21 +10001157
1158 nouveau_drm(dev)->pm = NULL;
1159 kfree(pm);
Ben Skeggs330c5982010-09-16 15:39:49 +10001160}
1161
Ben Skeggs64f1c112010-09-17 13:35:25 +10001162void
1163nouveau_pm_resume(struct drm_device *dev)
1164{
Ben Skeggs77145f12012-07-31 16:16:21 +10001165 struct nouveau_pm *pm = nouveau_pm(dev);
Ben Skeggs64f1c112010-09-17 13:35:25 +10001166 struct nouveau_pm_level *perflvl;
1167
Ben Skeggs317495b2011-02-17 11:11:28 +10001168 if (!pm->cur || pm->cur == &pm->boot)
Ben Skeggs64f1c112010-09-17 13:35:25 +10001169 return;
1170
1171 perflvl = pm->cur;
1172 pm->cur = &pm->boot;
1173 nouveau_pm_perflvl_set(dev, perflvl);
1174}