blob: 4fff09e3f5928b78a9a0cac7eda7eccf2cf1f1c1 [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
25#include "drmP.h"
26
27#include "nouveau_drv.h"
28#include "nouveau_pm.h"
Ben Skeggsa0b25632011-11-21 16:41:48 +100029#include "nouveau_gpio.h"
Ben Skeggs330c5982010-09-16 15:39:49 +100030
Ben Skeggs60326492010-10-12 12:31:32 +100031#ifdef CONFIG_ACPI
32#include <linux/acpi.h>
33#endif
34#include <linux/power_supply.h>
Martin Peres34e9d852010-09-22 20:54:22 +020035#include <linux/hwmon.h>
36#include <linux/hwmon-sysfs.h>
37
Ben Skeggs330c5982010-09-16 15:39:49 +100038static int
Ben Skeggsa1750942011-09-17 01:42:12 +100039nouveau_pwmfan_get(struct drm_device *dev)
40{
41 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggsa1750942011-09-17 01:42:12 +100042 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
Ben Skeggsa0b25632011-11-21 16:41:48 +100043 struct gpio_func gpio;
Ben Skeggsa1750942011-09-17 01:42:12 +100044 u32 divs, duty;
45 int ret;
46
Ben Skeggs1e054152011-09-17 06:38:56 +100047 if (!pm->pwm_get)
Ben Skeggsa1750942011-09-17 01:42:12 +100048 return -ENODEV;
Ben Skeggsa1750942011-09-17 01:42:12 +100049
Ben Skeggsa0b25632011-11-21 16:41:48 +100050 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);
Ben Skeggs3d8a4082012-01-23 12:58:33 +100053 if (ret == 0 && divs) {
Ben Skeggsa1750942011-09-17 01:42:12 +100054 divs = max(divs, duty);
Ben Skeggsa0b25632011-11-21 16:41:48 +100055 if (dev_priv->card_type <= NV_40 || (gpio.log[0] & 1))
Ben Skeggsa1750942011-09-17 01:42:12 +100056 duty = divs - duty;
57 return (duty * 100) / divs;
58 }
59
Ben Skeggsa0b25632011-11-21 16:41:48 +100060 return nouveau_gpio_func_get(dev, gpio.func) * 100;
Ben Skeggsa1750942011-09-17 01:42:12 +100061 }
62
63 return -ENODEV;
64}
65
66static int
67nouveau_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;
Ben Skeggsa0b25632011-11-21 16:41:48 +100071 struct gpio_func gpio;
Ben Skeggsa1750942011-09-17 01:42:12 +100072 u32 divs, duty;
Ben Skeggsa0b25632011-11-21 16:41:48 +100073 int ret;
Ben Skeggsa1750942011-09-17 01:42:12 +100074
Ben Skeggs1e054152011-09-17 06:38:56 +100075 if (!pm->pwm_set)
Ben Skeggsa1750942011-09-17 01:42:12 +100076 return -ENODEV;
Ben Skeggsa1750942011-09-17 01:42:12 +100077
Ben Skeggsa0b25632011-11-21 16:41:48 +100078 ret = nouveau_gpio_find(dev, 0, DCB_GPIO_PWM_FAN, 0xff, &gpio);
79 if (ret == 0) {
Martin Peresb1aa5532011-08-20 16:37:06 +020080 divs = pm->fan.pwm_divisor;
Ben Skeggsa1750942011-09-17 01:42:12 +100081 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;
Ben Skeggsa0b25632011-11-21 16:41:48 +100089 if (dev_priv->card_type <= NV_40 || (gpio.log[0] & 1))
Ben Skeggsa1750942011-09-17 01:42:12 +100090 duty = divs - duty;
91
Martin Peresbc6389e2011-10-07 01:33:12 +020092 ret = pm->pwm_set(dev, gpio.line, divs, duty);
93 if (!ret)
94 pm->fan.percent = percent;
95 return ret;
Ben Skeggsa1750942011-09-17 01:42:12 +100096 }
97
98 return -ENODEV;
99}
100
101static int
Ben Skeggs0b627a02011-10-27 12:02:12 +1000102nouveau_pm_perflvl_aux(struct drm_device *dev, struct nouveau_pm_level *perflvl,
103 struct nouveau_pm_level *a, struct nouveau_pm_level *b)
104{
105 struct drm_nouveau_private *dev_priv = dev->dev_private;
106 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
107 int ret;
108
109 /*XXX: not on all boards, we should control based on temperature
110 * on recent boards.. or maybe on some other factor we don't
111 * know about?
112 */
113 if (a->fanspeed && b->fanspeed && b->fanspeed > a->fanspeed) {
114 ret = nouveau_pwmfan_set(dev, perflvl->fanspeed);
115 if (ret && ret != -ENODEV) {
116 NV_ERROR(dev, "fanspeed set failed: %d\n", ret);
117 return ret;
118 }
119 }
120
121 if (pm->voltage.supported && pm->voltage_set) {
Ben Skeggsd2edab42011-11-10 13:20:14 +1000122 if (perflvl->volt_min && b->volt_min > a->volt_min) {
Ben Skeggs0b627a02011-10-27 12:02:12 +1000123 ret = pm->voltage_set(dev, perflvl->volt_min);
124 if (ret) {
125 NV_ERROR(dev, "voltage set failed: %d\n", ret);
126 return ret;
127 }
128 }
129 }
130
131 return 0;
132}
133
134static int
Ben Skeggs64f1c112010-09-17 13:35:25 +1000135nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl)
136{
137 struct drm_nouveau_private *dev_priv = dev->dev_private;
138 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
Ben Skeggsff2b6c62011-10-27 10:28:17 +1000139 void *state;
Ben Skeggs64f1c112010-09-17 13:35:25 +1000140 int ret;
141
142 if (perflvl == pm->cur)
143 return 0;
144
Ben Skeggs0b627a02011-10-27 12:02:12 +1000145 ret = nouveau_pm_perflvl_aux(dev, perflvl, pm->cur, perflvl);
146 if (ret)
147 return ret;
Ben Skeggs64f1c112010-09-17 13:35:25 +1000148
Ben Skeggsff2b6c62011-10-27 10:28:17 +1000149 state = pm->clocks_pre(dev, perflvl);
Martin Peresb0103742011-11-03 00:03:06 +0100150 if (IS_ERR(state)) {
151 ret = PTR_ERR(state);
152 goto error;
153 }
154 ret = pm->clocks_set(dev, state);
155 if (ret)
156 goto error;
Ben Skeggs64f1c112010-09-17 13:35:25 +1000157
Ben Skeggs0b627a02011-10-27 12:02:12 +1000158 ret = nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
159 if (ret)
160 return ret;
161
Ben Skeggs64f1c112010-09-17 13:35:25 +1000162 pm->cur = perflvl;
163 return 0;
Martin Peresb0103742011-11-03 00:03:06 +0100164
165error:
166 /* restore the fan speed and voltage before leaving */
167 nouveau_pm_perflvl_aux(dev, perflvl, perflvl, pm->cur);
168 return ret;
Ben Skeggs64f1c112010-09-17 13:35:25 +1000169}
170
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000171void
172nouveau_pm_trigger(struct drm_device *dev)
173{
174 struct drm_nouveau_private *dev_priv = dev->dev_private;
175 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
176 struct nouveau_pm_profile *profile = NULL;
177 struct nouveau_pm_level *perflvl = NULL;
178 int ret;
179
180 /* select power profile based on current power source */
181 if (power_supply_is_system_supplied())
182 profile = pm->profile_ac;
183 else
184 profile = pm->profile_dc;
185
186 /* select performance level based on profile */
187 perflvl = profile->func->select(profile);
188
189 /* change perflvl, if necessary */
190 if (perflvl != pm->cur) {
191 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
192 u64 time0 = ptimer->read(dev);
193
194 NV_INFO(dev, "setting performance level: %d", perflvl->id);
195 ret = nouveau_pm_perflvl_set(dev, perflvl);
196 if (ret)
197 NV_INFO(dev, "> reclocking failed: %d\n\n", ret);
198
199 NV_INFO(dev, "> reclocking took %lluns\n\n",
200 ptimer->read(dev) - time0);
201 }
202}
203
204static struct nouveau_pm_profile *
205profile_find(struct drm_device *dev, const char *string)
206{
207 struct drm_nouveau_private *dev_priv = dev->dev_private;
208 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
209 struct nouveau_pm_profile *profile;
210
211 list_for_each_entry(profile, &pm->profiles, head) {
212 if (!strncmp(profile->name, string, sizeof(profile->name)))
213 return profile;
214 }
215
216 return NULL;
217}
218
Ben Skeggs64f1c112010-09-17 13:35:25 +1000219static int
Ben Skeggs6f876982010-09-16 16:47:14 +1000220nouveau_pm_profile_set(struct drm_device *dev, const char *profile)
221{
222 struct drm_nouveau_private *dev_priv = dev->dev_private;
223 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000224 struct nouveau_pm_profile *ac = NULL, *dc = NULL;
225 char string[16], *cur = string, *ptr;
Ben Skeggs6f876982010-09-16 16:47:14 +1000226
227 /* safety precaution, for now */
228 if (nouveau_perflvl_wr != 7777)
229 return -EPERM;
230
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000231 strncpy(string, profile, sizeof(string));
232 if ((ptr = strchr(string, '\n')))
233 *ptr = '\0';
Ben Skeggs6f876982010-09-16 16:47:14 +1000234
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000235 ptr = strsep(&cur, ",");
236 if (ptr)
237 ac = profile_find(dev, ptr);
Ben Skeggs6f876982010-09-16 16:47:14 +1000238
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000239 ptr = strsep(&cur, ",");
240 if (ptr)
241 dc = profile_find(dev, ptr);
242 else
243 dc = ac;
Ben Skeggs6f876982010-09-16 16:47:14 +1000244
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000245 if (ac == NULL || dc == NULL)
246 return -EINVAL;
Martin Peresb0103742011-11-03 00:03:06 +0100247
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000248 pm->profile_ac = ac;
249 pm->profile_dc = dc;
250 nouveau_pm_trigger(dev);
251 return 0;
Ben Skeggs6f876982010-09-16 16:47:14 +1000252}
253
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000254static struct nouveau_pm_level *
255nouveau_pm_static_select(struct nouveau_pm_profile *profile)
256{
257 return container_of(profile, struct nouveau_pm_level, profile);
258}
259
260const struct nouveau_pm_profile_func nouveau_pm_static_profile_func = {
261 .select = nouveau_pm_static_select,
262};
263
Ben Skeggs6f876982010-09-16 16:47:14 +1000264static int
Ben Skeggs330c5982010-09-16 15:39:49 +1000265nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
266{
267 struct drm_nouveau_private *dev_priv = dev->dev_private;
268 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
269 int ret;
270
Ben Skeggs330c5982010-09-16 15:39:49 +1000271 memset(perflvl, 0, sizeof(*perflvl));
272
Ben Skeggsff2b6c62011-10-27 10:28:17 +1000273 ret = pm->clocks_get(dev, perflvl);
274 if (ret)
275 return ret;
Ben Skeggs330c5982010-09-16 15:39:49 +1000276
277 if (pm->voltage.supported && pm->voltage_get) {
278 ret = pm->voltage_get(dev);
Ben Skeggs3b5565d2011-06-09 16:57:07 +1000279 if (ret > 0) {
280 perflvl->volt_min = ret;
281 perflvl->volt_max = ret;
282 }
Ben Skeggs330c5982010-09-16 15:39:49 +1000283 }
284
Ben Skeggsa1750942011-09-17 01:42:12 +1000285 ret = nouveau_pwmfan_get(dev);
286 if (ret > 0)
287 perflvl->fanspeed = ret;
Ben Skeggs771e1032011-07-28 11:01:21 +1000288
Ben Skeggsfd99fd62012-01-17 21:10:58 +1000289 nouveau_mem_timing_read(dev, &perflvl->timing);
Ben Skeggs330c5982010-09-16 15:39:49 +1000290 return 0;
291}
292
293static void
294nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
295{
Ben Skeggs085028c2012-01-18 09:02:28 +1000296 char c[16], s[16], v[32], f[16], m[16];
Francisco Jerez0fbb1142010-09-20 16:18:28 +0200297
298 c[0] = '\0';
299 if (perflvl->core)
300 snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000);
Ben Skeggs330c5982010-09-16 15:39:49 +1000301
302 s[0] = '\0';
303 if (perflvl->shader)
304 snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
305
Ben Skeggs93dccbe2011-06-09 17:27:47 +1000306 m[0] = '\0';
307 if (perflvl->memory)
308 snprintf(m, sizeof(m), " memory %dMHz", perflvl->memory / 1000);
309
Ben Skeggs330c5982010-09-16 15:39:49 +1000310 v[0] = '\0';
Ben Skeggs3b5565d2011-06-09 16:57:07 +1000311 if (perflvl->volt_min && perflvl->volt_min != perflvl->volt_max) {
312 snprintf(v, sizeof(v), " voltage %dmV-%dmV",
313 perflvl->volt_min / 1000, perflvl->volt_max / 1000);
314 } else
315 if (perflvl->volt_min) {
316 snprintf(v, sizeof(v), " voltage %dmV",
317 perflvl->volt_min / 1000);
318 }
Ben Skeggs330c5982010-09-16 15:39:49 +1000319
320 f[0] = '\0';
321 if (perflvl->fanspeed)
322 snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
323
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000324 snprintf(ptr, len, "%s%s%s%s%s\n", c, s, m, v, f);
Ben Skeggs330c5982010-09-16 15:39:49 +1000325}
326
327static ssize_t
328nouveau_pm_get_perflvl_info(struct device *d,
329 struct device_attribute *a, char *buf)
330{
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000331 struct nouveau_pm_level *perflvl =
332 container_of(a, struct nouveau_pm_level, dev_attr);
Ben Skeggs330c5982010-09-16 15:39:49 +1000333 char *ptr = buf;
334 int len = PAGE_SIZE;
335
Ben Skeggs93dccbe2011-06-09 17:27:47 +1000336 snprintf(ptr, len, "%d:", perflvl->id);
Ben Skeggs330c5982010-09-16 15:39:49 +1000337 ptr += strlen(buf);
338 len -= strlen(buf);
339
340 nouveau_pm_perflvl_info(perflvl, ptr, len);
341 return strlen(buf);
342}
343
344static ssize_t
345nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
346{
Martin Peres34e9d852010-09-22 20:54:22 +0200347 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
Ben Skeggs330c5982010-09-16 15:39:49 +1000348 struct drm_nouveau_private *dev_priv = dev->dev_private;
349 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
350 struct nouveau_pm_level cur;
351 int len = PAGE_SIZE, ret;
352 char *ptr = buf;
353
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000354 snprintf(ptr, len, "profile: %s, %s\nc:",
355 pm->profile_ac->name, pm->profile_dc->name);
Ben Skeggs330c5982010-09-16 15:39:49 +1000356 ptr += strlen(buf);
357 len -= strlen(buf);
358
359 ret = nouveau_pm_perflvl_get(dev, &cur);
360 if (ret == 0)
361 nouveau_pm_perflvl_info(&cur, ptr, len);
362 return strlen(buf);
363}
364
365static ssize_t
366nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
367 const char *buf, size_t count)
368{
Martin Peres34e9d852010-09-22 20:54:22 +0200369 struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
Ben Skeggs6f876982010-09-16 16:47:14 +1000370 int ret;
371
372 ret = nouveau_pm_profile_set(dev, buf);
373 if (ret)
374 return ret;
375 return strlen(buf);
Ben Skeggs330c5982010-09-16 15:39:49 +1000376}
377
Francisco Jerez5c4abd02010-09-23 20:36:42 +0200378static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
379 nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
Ben Skeggs330c5982010-09-16 15:39:49 +1000380
Martin Peres34e9d852010-09-22 20:54:22 +0200381static int
382nouveau_sysfs_init(struct drm_device *dev)
Ben Skeggs330c5982010-09-16 15:39:49 +1000383{
384 struct drm_nouveau_private *dev_priv = dev->dev_private;
385 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
386 struct device *d = &dev->pdev->dev;
Ben Skeggs330c5982010-09-16 15:39:49 +1000387 int ret, i;
388
Ben Skeggs330c5982010-09-16 15:39:49 +1000389 ret = device_create_file(d, &dev_attr_performance_level);
390 if (ret)
391 return ret;
392
393 for (i = 0; i < pm->nr_perflvl; i++) {
394 struct nouveau_pm_level *perflvl = &pm->perflvl[i];
395
396 perflvl->dev_attr.attr.name = perflvl->name;
397 perflvl->dev_attr.attr.mode = S_IRUGO;
398 perflvl->dev_attr.show = nouveau_pm_get_perflvl_info;
399 perflvl->dev_attr.store = NULL;
400 sysfs_attr_init(&perflvl->dev_attr.attr);
401
402 ret = device_create_file(d, &perflvl->dev_attr);
403 if (ret) {
404 NV_ERROR(dev, "failed pervlvl %d sysfs: %d\n",
405 perflvl->id, i);
406 perflvl->dev_attr.attr.name = NULL;
407 nouveau_pm_fini(dev);
408 return ret;
409 }
410 }
411
412 return 0;
413}
414
Martin Peres34e9d852010-09-22 20:54:22 +0200415static void
416nouveau_sysfs_fini(struct drm_device *dev)
Ben Skeggs330c5982010-09-16 15:39:49 +1000417{
418 struct drm_nouveau_private *dev_priv = dev->dev_private;
419 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
420 struct device *d = &dev->pdev->dev;
421 int i;
422
423 device_remove_file(d, &dev_attr_performance_level);
424 for (i = 0; i < pm->nr_perflvl; i++) {
425 struct nouveau_pm_level *pl = &pm->perflvl[i];
426
427 if (!pl->dev_attr.attr.name)
428 break;
429
430 device_remove_file(d, &pl->dev_attr);
431 }
Martin Peres34e9d852010-09-22 20:54:22 +0200432}
433
Ken Milmore658e86e2011-07-03 19:54:28 +0100434#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
Martin Peres34e9d852010-09-22 20:54:22 +0200435static ssize_t
436nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
437{
438 struct drm_device *dev = dev_get_drvdata(d);
Francisco Jerez8155cac2010-09-23 20:58:38 +0200439 struct drm_nouveau_private *dev_priv = dev->dev_private;
440 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
Martin Peres34e9d852010-09-22 20:54:22 +0200441
Francisco Jerez8155cac2010-09-23 20:58:38 +0200442 return snprintf(buf, PAGE_SIZE, "%d\n", pm->temp_get(dev)*1000);
Martin Peres34e9d852010-09-22 20:54:22 +0200443}
444static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
445 NULL, 0);
446
447static ssize_t
448nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
449{
450 struct drm_device *dev = dev_get_drvdata(d);
451 struct drm_nouveau_private *dev_priv = dev->dev_private;
452 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
453 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
454
455 return snprintf(buf, PAGE_SIZE, "%d\n", temp->down_clock*1000);
456}
457static ssize_t
458nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
459 const char *buf, size_t count)
460{
461 struct drm_device *dev = dev_get_drvdata(d);
462 struct drm_nouveau_private *dev_priv = dev->dev_private;
463 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
464 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
465 long value;
466
Martin Peresddb20052011-12-17 12:24:59 +0100467 if (kstrtol(buf, 10, &value) == -EINVAL)
Martin Peres34e9d852010-09-22 20:54:22 +0200468 return count;
469
470 temp->down_clock = value/1000;
471
472 nouveau_temp_safety_checks(dev);
473
474 return count;
475}
476static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
477 nouveau_hwmon_set_max_temp,
478 0);
479
480static ssize_t
481nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
482 char *buf)
483{
484 struct drm_device *dev = dev_get_drvdata(d);
485 struct drm_nouveau_private *dev_priv = dev->dev_private;
486 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
487 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
488
489 return snprintf(buf, PAGE_SIZE, "%d\n", temp->critical*1000);
490}
491static ssize_t
492nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
493 const char *buf,
494 size_t count)
495{
496 struct drm_device *dev = dev_get_drvdata(d);
497 struct drm_nouveau_private *dev_priv = dev->dev_private;
498 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
499 struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
500 long value;
501
Martin Peresddb20052011-12-17 12:24:59 +0100502 if (kstrtol(buf, 10, &value) == -EINVAL)
Martin Peres34e9d852010-09-22 20:54:22 +0200503 return count;
504
505 temp->critical = value/1000;
506
507 nouveau_temp_safety_checks(dev);
508
509 return count;
510}
511static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
512 nouveau_hwmon_critical_temp,
513 nouveau_hwmon_set_critical_temp,
514 0);
515
516static ssize_t nouveau_hwmon_show_name(struct device *dev,
517 struct device_attribute *attr,
518 char *buf)
519{
520 return sprintf(buf, "nouveau\n");
521}
522static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
523
524static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
525 struct device_attribute *attr,
526 char *buf)
527{
528 return sprintf(buf, "1000\n");
529}
530static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
531 nouveau_hwmon_show_update_rate,
532 NULL, 0);
533
Martin Peres11b7d892011-08-15 11:10:30 +1000534static ssize_t
535nouveau_hwmon_show_fan0_input(struct device *d, struct device_attribute *attr,
536 char *buf)
537{
538 struct drm_device *dev = dev_get_drvdata(d);
539 struct drm_nouveau_private *dev_priv = dev->dev_private;
540 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000541 struct gpio_func gpio;
Martin Peres11b7d892011-08-15 11:10:30 +1000542 u32 cycles, cur, prev;
543 u64 start;
Ben Skeggsa0b25632011-11-21 16:41:48 +1000544 int ret;
Martin Peres11b7d892011-08-15 11:10:30 +1000545
Ben Skeggsa0b25632011-11-21 16:41:48 +1000546 ret = nouveau_gpio_find(dev, 0, DCB_GPIO_FAN_SENSE, 0xff, &gpio);
547 if (ret)
548 return ret;
Martin Peres11b7d892011-08-15 11:10:30 +1000549
550 /* Monitor the GPIO input 0x3b for 250ms.
551 * When the fan spins, it changes the value of GPIO FAN_SENSE.
552 * We get 4 changes (0 -> 1 -> 0 -> 1 -> [...]) per complete rotation.
553 */
554 start = ptimer->read(dev);
Ben Skeggsa0b25632011-11-21 16:41:48 +1000555 prev = nouveau_gpio_sense(dev, 0, gpio.line);
Martin Peres11b7d892011-08-15 11:10:30 +1000556 cycles = 0;
557 do {
Ben Skeggsa0b25632011-11-21 16:41:48 +1000558 cur = nouveau_gpio_sense(dev, 0, gpio.line);
Martin Peres11b7d892011-08-15 11:10:30 +1000559 if (prev != cur) {
560 cycles++;
561 prev = cur;
562 }
563
564 usleep_range(500, 1000); /* supports 0 < rpm < 7500 */
565 } while (ptimer->read(dev) - start < 250000000);
566
567 /* interpolate to get rpm */
568 return sprintf(buf, "%i\n", cycles / 4 * 4 * 60);
569}
570static SENSOR_DEVICE_ATTR(fan0_input, S_IRUGO, nouveau_hwmon_show_fan0_input,
571 NULL, 0);
572
573static ssize_t
574nouveau_hwmon_get_pwm0(struct device *d, struct device_attribute *a, char *buf)
575{
576 struct drm_device *dev = dev_get_drvdata(d);
Ben Skeggsa1750942011-09-17 01:42:12 +1000577 int ret;
Martin Peres11b7d892011-08-15 11:10:30 +1000578
Ben Skeggsa1750942011-09-17 01:42:12 +1000579 ret = nouveau_pwmfan_get(dev);
Martin Peres11b7d892011-08-15 11:10:30 +1000580 if (ret < 0)
581 return ret;
582
583 return sprintf(buf, "%i\n", ret);
584}
585
586static ssize_t
587nouveau_hwmon_set_pwm0(struct device *d, struct device_attribute *a,
588 const char *buf, size_t count)
589{
590 struct drm_device *dev = dev_get_drvdata(d);
591 struct drm_nouveau_private *dev_priv = dev->dev_private;
592 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
593 int ret = -ENODEV;
594 long value;
595
596 if (nouveau_perflvl_wr != 7777)
597 return -EPERM;
598
Martin Peresddb20052011-12-17 12:24:59 +0100599 if (kstrtol(buf, 10, &value) == -EINVAL)
Martin Peres11b7d892011-08-15 11:10:30 +1000600 return -EINVAL;
601
602 if (value < pm->fan.min_duty)
603 value = pm->fan.min_duty;
604 if (value > pm->fan.max_duty)
605 value = pm->fan.max_duty;
606
Ben Skeggsa1750942011-09-17 01:42:12 +1000607 ret = nouveau_pwmfan_set(dev, value);
Martin Peres11b7d892011-08-15 11:10:30 +1000608 if (ret)
609 return ret;
610
611 return count;
612}
613
614static SENSOR_DEVICE_ATTR(pwm0, S_IRUGO | S_IWUSR,
615 nouveau_hwmon_get_pwm0,
616 nouveau_hwmon_set_pwm0, 0);
617
618static ssize_t
619nouveau_hwmon_get_pwm0_min(struct device *d,
620 struct device_attribute *a, char *buf)
621{
622 struct drm_device *dev = dev_get_drvdata(d);
623 struct drm_nouveau_private *dev_priv = dev->dev_private;
624 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
625
626 return sprintf(buf, "%i\n", pm->fan.min_duty);
627}
628
629static ssize_t
630nouveau_hwmon_set_pwm0_min(struct device *d, struct device_attribute *a,
631 const char *buf, size_t count)
632{
633 struct drm_device *dev = dev_get_drvdata(d);
634 struct drm_nouveau_private *dev_priv = dev->dev_private;
635 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
636 long value;
637
Martin Peresddb20052011-12-17 12:24:59 +0100638 if (kstrtol(buf, 10, &value) == -EINVAL)
Martin Peres11b7d892011-08-15 11:10:30 +1000639 return -EINVAL;
640
641 if (value < 0)
642 value = 0;
643
644 if (pm->fan.max_duty - value < 10)
645 value = pm->fan.max_duty - 10;
646
647 if (value < 10)
648 pm->fan.min_duty = 10;
649 else
650 pm->fan.min_duty = value;
651
652 return count;
653}
654
655static SENSOR_DEVICE_ATTR(pwm0_min, S_IRUGO | S_IWUSR,
656 nouveau_hwmon_get_pwm0_min,
657 nouveau_hwmon_set_pwm0_min, 0);
658
659static ssize_t
660nouveau_hwmon_get_pwm0_max(struct device *d,
661 struct device_attribute *a, char *buf)
662{
663 struct drm_device *dev = dev_get_drvdata(d);
664 struct drm_nouveau_private *dev_priv = dev->dev_private;
665 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
666
667 return sprintf(buf, "%i\n", pm->fan.max_duty);
668}
669
670static ssize_t
671nouveau_hwmon_set_pwm0_max(struct device *d, struct device_attribute *a,
672 const char *buf, size_t count)
673{
674 struct drm_device *dev = dev_get_drvdata(d);
675 struct drm_nouveau_private *dev_priv = dev->dev_private;
676 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
677 long value;
678
Martin Peresddb20052011-12-17 12:24:59 +0100679 if (kstrtol(buf, 10, &value) == -EINVAL)
Martin Peres11b7d892011-08-15 11:10:30 +1000680 return -EINVAL;
681
682 if (value < 0)
683 value = 0;
684
685 if (value - pm->fan.min_duty < 10)
686 value = pm->fan.min_duty + 10;
687
688 if (value > 100)
689 pm->fan.max_duty = 100;
690 else
691 pm->fan.max_duty = value;
692
693 return count;
694}
695
696static SENSOR_DEVICE_ATTR(pwm0_max, S_IRUGO | S_IWUSR,
697 nouveau_hwmon_get_pwm0_max,
698 nouveau_hwmon_set_pwm0_max, 0);
699
Martin Peres34e9d852010-09-22 20:54:22 +0200700static struct attribute *hwmon_attributes[] = {
701 &sensor_dev_attr_temp1_input.dev_attr.attr,
702 &sensor_dev_attr_temp1_max.dev_attr.attr,
703 &sensor_dev_attr_temp1_crit.dev_attr.attr,
704 &sensor_dev_attr_name.dev_attr.attr,
705 &sensor_dev_attr_update_rate.dev_attr.attr,
706 NULL
707};
Martin Peres11b7d892011-08-15 11:10:30 +1000708static struct attribute *hwmon_fan_rpm_attributes[] = {
709 &sensor_dev_attr_fan0_input.dev_attr.attr,
710 NULL
711};
712static struct attribute *hwmon_pwm_fan_attributes[] = {
713 &sensor_dev_attr_pwm0.dev_attr.attr,
714 &sensor_dev_attr_pwm0_min.dev_attr.attr,
715 &sensor_dev_attr_pwm0_max.dev_attr.attr,
716 NULL
717};
Martin Peres34e9d852010-09-22 20:54:22 +0200718
719static const struct attribute_group hwmon_attrgroup = {
720 .attrs = hwmon_attributes,
721};
Martin Peres11b7d892011-08-15 11:10:30 +1000722static const struct attribute_group hwmon_fan_rpm_attrgroup = {
723 .attrs = hwmon_fan_rpm_attributes,
724};
725static const struct attribute_group hwmon_pwm_fan_attrgroup = {
726 .attrs = hwmon_pwm_fan_attributes,
727};
Martin Peresb54262f2010-10-26 12:48:28 +0200728#endif
Martin Peres34e9d852010-09-22 20:54:22 +0200729
730static int
731nouveau_hwmon_init(struct drm_device *dev)
732{
733 struct drm_nouveau_private *dev_priv = dev->dev_private;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200734 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
Dave Airlie095f9792012-01-10 10:13:16 +0000735#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
Martin Peres34e9d852010-09-22 20:54:22 +0200736 struct device *hwmon_dev;
Martin Peres11b7d892011-08-15 11:10:30 +1000737 int ret = 0;
Martin Peres34e9d852010-09-22 20:54:22 +0200738
Francisco Jerez8155cac2010-09-23 20:58:38 +0200739 if (!pm->temp_get)
740 return -ENODEV;
Martin Peres34e9d852010-09-22 20:54:22 +0200741
742 hwmon_dev = hwmon_device_register(&dev->pdev->dev);
743 if (IS_ERR(hwmon_dev)) {
744 ret = PTR_ERR(hwmon_dev);
745 NV_ERROR(dev,
746 "Unable to register hwmon device: %d\n", ret);
747 return ret;
748 }
749 dev_set_drvdata(hwmon_dev, dev);
Martin Peres11b7d892011-08-15 11:10:30 +1000750
751 /* default sysfs entries */
Lucas Stach07cfe0e2011-01-06 20:29:53 +0100752 ret = sysfs_create_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
Martin Peres34e9d852010-09-22 20:54:22 +0200753 if (ret) {
Martin Peres11b7d892011-08-15 11:10:30 +1000754 if (ret)
755 goto error;
756 }
757
758 /* if the card has a pwm fan */
759 /*XXX: incorrect, need better detection for this, some boards have
760 * the gpio entries for pwm fan control even when there's no
761 * actual fan connected to it... therm table? */
Ben Skeggsa1750942011-09-17 01:42:12 +1000762 if (nouveau_pwmfan_get(dev) >= 0) {
Martin Peres11b7d892011-08-15 11:10:30 +1000763 ret = sysfs_create_group(&dev->pdev->dev.kobj,
764 &hwmon_pwm_fan_attrgroup);
765 if (ret)
766 goto error;
767 }
768
769 /* if the card can read the fan rpm */
Ben Skeggsa0b25632011-11-21 16:41:48 +1000770 if (nouveau_gpio_func_valid(dev, DCB_GPIO_FAN_SENSE)) {
Martin Peres11b7d892011-08-15 11:10:30 +1000771 ret = sysfs_create_group(&dev->pdev->dev.kobj,
772 &hwmon_fan_rpm_attrgroup);
773 if (ret)
774 goto error;
Martin Peres34e9d852010-09-22 20:54:22 +0200775 }
776
Francisco Jerez8155cac2010-09-23 20:58:38 +0200777 pm->hwmon = hwmon_dev;
Martin Peres11b7d892011-08-15 11:10:30 +1000778
Martin Peres34e9d852010-09-22 20:54:22 +0200779 return 0;
Martin Peres11b7d892011-08-15 11:10:30 +1000780
781error:
782 NV_ERROR(dev, "Unable to create some hwmon sysfs files: %d\n", ret);
783 hwmon_device_unregister(hwmon_dev);
784 pm->hwmon = NULL;
785 return ret;
786#else
787 pm->hwmon = NULL;
788 return 0;
789#endif
Martin Peres34e9d852010-09-22 20:54:22 +0200790}
791
792static void
793nouveau_hwmon_fini(struct drm_device *dev)
794{
Ken Milmore658e86e2011-07-03 19:54:28 +0100795#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
Martin Peres34e9d852010-09-22 20:54:22 +0200796 struct drm_nouveau_private *dev_priv = dev->dev_private;
Francisco Jerez8155cac2010-09-23 20:58:38 +0200797 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
Martin Peres34e9d852010-09-22 20:54:22 +0200798
Francisco Jerez8155cac2010-09-23 20:58:38 +0200799 if (pm->hwmon) {
Lucas Stach8c06a3e2011-01-30 10:54:11 +0100800 sysfs_remove_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
Martin Peresddb20052011-12-17 12:24:59 +0100801 sysfs_remove_group(&dev->pdev->dev.kobj,
802 &hwmon_pwm_fan_attrgroup);
803 sysfs_remove_group(&dev->pdev->dev.kobj,
804 &hwmon_fan_rpm_attrgroup);
Martin Peres11b7d892011-08-15 11:10:30 +1000805
Francisco Jerez8155cac2010-09-23 20:58:38 +0200806 hwmon_device_unregister(pm->hwmon);
Martin Peres34e9d852010-09-22 20:54:22 +0200807 }
Martin Peresb54262f2010-10-26 12:48:28 +0200808#endif
Martin Peres34e9d852010-09-22 20:54:22 +0200809}
810
Martin Peres1f962792011-04-12 00:55:44 +0200811#if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
Ben Skeggs60326492010-10-12 12:31:32 +1000812static int
813nouveau_pm_acpi_event(struct notifier_block *nb, unsigned long val, void *data)
814{
815 struct drm_nouveau_private *dev_priv =
816 container_of(nb, struct drm_nouveau_private, engine.pm.acpi_nb);
817 struct drm_device *dev = dev_priv->dev;
818 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
819
820 if (strcmp(entry->device_class, "ac_adapter") == 0) {
821 bool ac = power_supply_is_system_supplied();
822
823 NV_DEBUG(dev, "power supply changed: %s\n", ac ? "AC" : "DC");
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000824 nouveau_pm_trigger(dev);
Ben Skeggs60326492010-10-12 12:31:32 +1000825 }
826
827 return NOTIFY_OK;
828}
829#endif
830
Martin Peres34e9d852010-09-22 20:54:22 +0200831int
832nouveau_pm_init(struct drm_device *dev)
833{
834 struct drm_nouveau_private *dev_priv = dev->dev_private;
835 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
836 char info[256];
837 int ret, i;
838
Ben Skeggs68a64ca2012-01-23 13:47:02 +1000839 /* parse aux tables from vbios */
Martin Peres34e9d852010-09-22 20:54:22 +0200840 nouveau_volt_init(dev);
Martin Peres34e9d852010-09-22 20:54:22 +0200841 nouveau_temp_init(dev);
842
Ben Skeggs68a64ca2012-01-23 13:47:02 +1000843 /* determine current ("boot") performance level */
844 ret = nouveau_pm_perflvl_get(dev, &pm->boot);
845 if (ret) {
846 NV_ERROR(dev, "failed to determine boot perflvl\n");
847 return ret;
848 }
849
850 strncpy(pm->boot.name, "boot", 4);
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000851 strncpy(pm->boot.profile.name, "boot", 4);
852 pm->boot.profile.func = &nouveau_pm_static_profile_func;
853
854 INIT_LIST_HEAD(&pm->profiles);
855 list_add(&pm->boot.profile.head, &pm->profiles);
856
857 pm->profile_ac = &pm->boot.profile;
858 pm->profile_dc = &pm->boot.profile;
Ben Skeggs68a64ca2012-01-23 13:47:02 +1000859 pm->cur = &pm->boot;
860
861 /* add performance levels from vbios */
862 nouveau_perf_init(dev);
863
864 /* display available performance levels */
Martin Peres34e9d852010-09-22 20:54:22 +0200865 NV_INFO(dev, "%d available performance level(s)\n", pm->nr_perflvl);
866 for (i = 0; i < pm->nr_perflvl; i++) {
867 nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
Ben Skeggs93dccbe2011-06-09 17:27:47 +1000868 NV_INFO(dev, "%d:%s", pm->perflvl[i].id, info);
Martin Peres34e9d852010-09-22 20:54:22 +0200869 }
870
Ben Skeggs68a64ca2012-01-23 13:47:02 +1000871 nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
872 NV_INFO(dev, "c:%s", info);
Martin Peres34e9d852010-09-22 20:54:22 +0200873
874 /* switch performance levels now if requested */
Ben Skeggs8d7bb402012-01-24 15:59:07 +1000875 if (nouveau_perflvl != NULL)
876 nouveau_pm_profile_set(dev, nouveau_perflvl);
Martin Peres34e9d852010-09-22 20:54:22 +0200877
Martin Peresbc6389e2011-10-07 01:33:12 +0200878 /* determine the current fan speed */
879 pm->fan.percent = nouveau_pwmfan_get(dev);
880
Martin Peres34e9d852010-09-22 20:54:22 +0200881 nouveau_sysfs_init(dev);
882 nouveau_hwmon_init(dev);
Martin Peres1f962792011-04-12 00:55:44 +0200883#if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
Ben Skeggs60326492010-10-12 12:31:32 +1000884 pm->acpi_nb.notifier_call = nouveau_pm_acpi_event;
885 register_acpi_notifier(&pm->acpi_nb);
886#endif
Martin Peres34e9d852010-09-22 20:54:22 +0200887
888 return 0;
889}
890
891void
892nouveau_pm_fini(struct drm_device *dev)
893{
894 struct drm_nouveau_private *dev_priv = dev->dev_private;
895 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
896
897 if (pm->cur != &pm->boot)
898 nouveau_pm_perflvl_set(dev, &pm->boot);
Ben Skeggs330c5982010-09-16 15:39:49 +1000899
Roy Spliet7760fcb2010-09-17 23:17:24 +0200900 nouveau_temp_fini(dev);
Ben Skeggs330c5982010-09-16 15:39:49 +1000901 nouveau_perf_fini(dev);
902 nouveau_volt_fini(dev);
Martin Peres34e9d852010-09-22 20:54:22 +0200903
Martin Peres1f962792011-04-12 00:55:44 +0200904#if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
Ben Skeggs60326492010-10-12 12:31:32 +1000905 unregister_acpi_notifier(&pm->acpi_nb);
906#endif
Martin Peres34e9d852010-09-22 20:54:22 +0200907 nouveau_hwmon_fini(dev);
908 nouveau_sysfs_fini(dev);
Ben Skeggs330c5982010-09-16 15:39:49 +1000909}
910
Ben Skeggs64f1c112010-09-17 13:35:25 +1000911void
912nouveau_pm_resume(struct drm_device *dev)
913{
914 struct drm_nouveau_private *dev_priv = dev->dev_private;
915 struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
916 struct nouveau_pm_level *perflvl;
917
Ben Skeggs317495b2011-02-17 11:11:28 +1000918 if (!pm->cur || pm->cur == &pm->boot)
Ben Skeggs64f1c112010-09-17 13:35:25 +1000919 return;
920
921 perflvl = pm->cur;
922 pm->cur = &pm->boot;
923 nouveau_pm_perflvl_set(dev, perflvl);
Martin Peresbc6389e2011-10-07 01:33:12 +0200924 nouveau_pwmfan_set(dev, pm->fan.percent);
Ben Skeggs64f1c112010-09-17 13:35:25 +1000925}