nouveau_hwmon.c 20.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright 2010 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Ben Skeggs
 */

25 26 27 28
#ifdef CONFIG_ACPI
#include <linux/acpi.h>
#endif
#include <linux/power_supply.h>
29 30 31
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>

32
#include <drm/drmP.h>
33

34
#include "nouveau_drm.h"
35
#include "nouveau_hwmon.h"
36

37
#include <nvkm/subdev/iccsense.h>
38 39
#include <nvkm/subdev/volt.h>

40
#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
41 42 43 44
static ssize_t
nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
{
	struct drm_device *dev = dev_get_drvdata(d);
45
	struct nouveau_drm *drm = nouveau_drm(dev);
46
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
47
	int temp = nvkm_therm_temp_get(therm);
48

49 50 51 52
	if (temp < 0)
		return temp;

	return snprintf(buf, PAGE_SIZE, "%d\n", temp * 1000);
53 54 55 56
}
static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
						  NULL, 0);

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
static ssize_t
nouveau_hwmon_show_temp1_auto_point1_pwm(struct device *d,
					 struct device_attribute *a, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%d\n", 100);
}
static SENSOR_DEVICE_ATTR(temp1_auto_point1_pwm, S_IRUGO,
			  nouveau_hwmon_show_temp1_auto_point1_pwm, NULL, 0);

static ssize_t
nouveau_hwmon_temp1_auto_point1_temp(struct device *d,
				     struct device_attribute *a, char *buf)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct nouveau_drm *drm = nouveau_drm(dev);
72
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
73 74

	return snprintf(buf, PAGE_SIZE, "%d\n",
75
	      therm->attr_get(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST) * 1000);
76 77 78 79 80 81 82 83
}
static ssize_t
nouveau_hwmon_set_temp1_auto_point1_temp(struct device *d,
					 struct device_attribute *a,
					 const char *buf, size_t count)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct nouveau_drm *drm = nouveau_drm(dev);
84
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
85 86 87 88 89
	long value;

	if (kstrtol(buf, 10, &value) == -EINVAL)
		return count;

90
	therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST,
91 92 93 94 95 96 97 98 99 100 101 102 103 104
			value / 1000);

	return count;
}
static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
			  nouveau_hwmon_temp1_auto_point1_temp,
			  nouveau_hwmon_set_temp1_auto_point1_temp, 0);

static ssize_t
nouveau_hwmon_temp1_auto_point1_temp_hyst(struct device *d,
					  struct device_attribute *a, char *buf)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct nouveau_drm *drm = nouveau_drm(dev);
105
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
106 107

	return snprintf(buf, PAGE_SIZE, "%d\n",
108
	 therm->attr_get(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST) * 1000);
109 110 111 112 113 114 115 116
}
static ssize_t
nouveau_hwmon_set_temp1_auto_point1_temp_hyst(struct device *d,
					      struct device_attribute *a,
					      const char *buf, size_t count)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct nouveau_drm *drm = nouveau_drm(dev);
117
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
118 119 120 121 122
	long value;

	if (kstrtol(buf, 10, &value) == -EINVAL)
		return count;

123
	therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST,
124 125 126 127 128 129 130 131
			value / 1000);

	return count;
}
static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
			  nouveau_hwmon_temp1_auto_point1_temp_hyst,
			  nouveau_hwmon_set_temp1_auto_point1_temp_hyst, 0);

132 133 134 135
static ssize_t
nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
{
	struct drm_device *dev = dev_get_drvdata(d);
136
	struct nouveau_drm *drm = nouveau_drm(dev);
137
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
138

139
	return snprintf(buf, PAGE_SIZE, "%d\n",
140
	       therm->attr_get(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK) * 1000);
141 142 143 144 145 146
}
static ssize_t
nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
						const char *buf, size_t count)
{
	struct drm_device *dev = dev_get_drvdata(d);
147
	struct nouveau_drm *drm = nouveau_drm(dev);
148
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
149 150
	long value;

M
Martin Peres 已提交
151
	if (kstrtol(buf, 10, &value) == -EINVAL)
152 153
		return count;

154
	therm->attr_set(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK, value / 1000);
155 156 157 158 159 160 161

	return count;
}
static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
						  nouveau_hwmon_set_max_temp,
						  0);

162 163 164 165 166 167
static ssize_t
nouveau_hwmon_max_temp_hyst(struct device *d, struct device_attribute *a,
			    char *buf)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct nouveau_drm *drm = nouveau_drm(dev);
168
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
169 170

	return snprintf(buf, PAGE_SIZE, "%d\n",
171
	  therm->attr_get(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST) * 1000);
172 173 174 175 176 177 178
}
static ssize_t
nouveau_hwmon_set_max_temp_hyst(struct device *d, struct device_attribute *a,
						const char *buf, size_t count)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct nouveau_drm *drm = nouveau_drm(dev);
179
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
180 181 182 183 184
	long value;

	if (kstrtol(buf, 10, &value) == -EINVAL)
		return count;

185
	therm->attr_set(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST,
186 187 188 189 190 191 192 193
			value / 1000);

	return count;
}
static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
			  nouveau_hwmon_max_temp_hyst,
			  nouveau_hwmon_set_max_temp_hyst, 0);

194 195 196 197 198
static ssize_t
nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
							char *buf)
{
	struct drm_device *dev = dev_get_drvdata(d);
199
	struct nouveau_drm *drm = nouveau_drm(dev);
200
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
201

202
	return snprintf(buf, PAGE_SIZE, "%d\n",
203
	       therm->attr_get(therm, NVKM_THERM_ATTR_THRS_CRITICAL) * 1000);
204 205 206 207 208 209 210
}
static ssize_t
nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
							    const char *buf,
								size_t count)
{
	struct drm_device *dev = dev_get_drvdata(d);
211
	struct nouveau_drm *drm = nouveau_drm(dev);
212
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
213 214
	long value;

M
Martin Peres 已提交
215
	if (kstrtol(buf, 10, &value) == -EINVAL)
216 217
		return count;

218
	therm->attr_set(therm, NVKM_THERM_ATTR_THRS_CRITICAL, value / 1000);
219 220 221 222 223 224 225 226

	return count;
}
static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
						nouveau_hwmon_critical_temp,
						nouveau_hwmon_set_critical_temp,
						0);

227 228 229 230 231 232
static ssize_t
nouveau_hwmon_critical_temp_hyst(struct device *d, struct device_attribute *a,
							char *buf)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct nouveau_drm *drm = nouveau_drm(dev);
233
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
234 235

	return snprintf(buf, PAGE_SIZE, "%d\n",
236
	  therm->attr_get(therm, NVKM_THERM_ATTR_THRS_CRITICAL_HYST) * 1000);
237 238 239 240 241 242 243 244 245
}
static ssize_t
nouveau_hwmon_set_critical_temp_hyst(struct device *d,
				     struct device_attribute *a,
				     const char *buf,
				     size_t count)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct nouveau_drm *drm = nouveau_drm(dev);
246
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
247 248 249 250 251
	long value;

	if (kstrtol(buf, 10, &value) == -EINVAL)
		return count;

252
	therm->attr_set(therm, NVKM_THERM_ATTR_THRS_CRITICAL_HYST,
253 254 255 256 257 258 259 260 261 262 263 264 265
			value / 1000);

	return count;
}
static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO | S_IWUSR,
			  nouveau_hwmon_critical_temp_hyst,
			  nouveau_hwmon_set_critical_temp_hyst, 0);
static ssize_t
nouveau_hwmon_emergency_temp(struct device *d, struct device_attribute *a,
							char *buf)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct nouveau_drm *drm = nouveau_drm(dev);
266
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
267 268

	return snprintf(buf, PAGE_SIZE, "%d\n",
269
	       therm->attr_get(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN) * 1000);
270 271 272 273 274 275 276 277
}
static ssize_t
nouveau_hwmon_set_emergency_temp(struct device *d, struct device_attribute *a,
							    const char *buf,
								size_t count)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct nouveau_drm *drm = nouveau_drm(dev);
278
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
279 280 281 282 283
	long value;

	if (kstrtol(buf, 10, &value) == -EINVAL)
		return count;

284
	therm->attr_set(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN, value / 1000);
285 286 287 288 289 290 291 292 293 294 295 296 297 298

	return count;
}
static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO | S_IWUSR,
					nouveau_hwmon_emergency_temp,
					nouveau_hwmon_set_emergency_temp,
					0);

static ssize_t
nouveau_hwmon_emergency_temp_hyst(struct device *d, struct device_attribute *a,
							char *buf)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct nouveau_drm *drm = nouveau_drm(dev);
299
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
300 301

	return snprintf(buf, PAGE_SIZE, "%d\n",
302
	  therm->attr_get(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST) * 1000);
303 304 305 306 307 308 309 310 311
}
static ssize_t
nouveau_hwmon_set_emergency_temp_hyst(struct device *d,
				      struct device_attribute *a,
				      const char *buf,
				      size_t count)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct nouveau_drm *drm = nouveau_drm(dev);
312
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
313 314 315 316 317
	long value;

	if (kstrtol(buf, 10, &value) == -EINVAL)
		return count;

318
	therm->attr_set(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST,
319 320 321 322 323 324 325 326 327
			value / 1000);

	return count;
}
static SENSOR_DEVICE_ATTR(temp1_emergency_hyst, S_IRUGO | S_IWUSR,
					nouveau_hwmon_emergency_temp_hyst,
					nouveau_hwmon_set_emergency_temp_hyst,
					0);

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
static ssize_t nouveau_hwmon_show_name(struct device *dev,
				      struct device_attribute *attr,
				      char *buf)
{
	return sprintf(buf, "nouveau\n");
}
static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);

static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
				      struct device_attribute *attr,
				      char *buf)
{
	return sprintf(buf, "1000\n");
}
static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
						nouveau_hwmon_show_update_rate,
						NULL, 0);

346
static ssize_t
B
Ben Skeggs 已提交
347
nouveau_hwmon_show_fan1_input(struct device *d, struct device_attribute *attr,
348 349 350
			      char *buf)
{
	struct drm_device *dev = dev_get_drvdata(d);
351
	struct nouveau_drm *drm = nouveau_drm(dev);
352
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
353

354
	return snprintf(buf, PAGE_SIZE, "%d\n", nvkm_therm_fan_sense(therm));
355
}
B
Ben Skeggs 已提交
356
static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, nouveau_hwmon_show_fan1_input,
357 358
			  NULL, 0);

359 360 361 362 363 364
 static ssize_t
nouveau_hwmon_get_pwm1_enable(struct device *d,
			   struct device_attribute *a, char *buf)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct nouveau_drm *drm = nouveau_drm(dev);
365
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
366
	int ret;
367

368
	ret = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MODE);
369
	if (ret < 0)
370
		return ret;
371

372 373
	return sprintf(buf, "%i\n", ret);
}
374

375 376 377 378 379 380
static ssize_t
nouveau_hwmon_set_pwm1_enable(struct device *d, struct device_attribute *a,
			   const char *buf, size_t count)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct nouveau_drm *drm = nouveau_drm(dev);
381
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
382 383 384
	long value;
	int ret;

385 386 387
	ret = kstrtol(buf, 10, &value);
	if (ret)
		return ret;
388

389
	ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MODE, value);
390 391 392 393
	if (ret)
		return ret;
	else
		return count;
394
}
395 396 397
static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
			  nouveau_hwmon_get_pwm1_enable,
			  nouveau_hwmon_set_pwm1_enable, 0);
398 399

static ssize_t
400
nouveau_hwmon_get_pwm1(struct device *d, struct device_attribute *a, char *buf)
401 402
{
	struct drm_device *dev = dev_get_drvdata(d);
403
	struct nouveau_drm *drm = nouveau_drm(dev);
404
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
405
	int ret;
406

407
	ret = therm->fan_get(therm);
408 409 410 411 412 413 414
	if (ret < 0)
		return ret;

	return sprintf(buf, "%i\n", ret);
}

static ssize_t
415
nouveau_hwmon_set_pwm1(struct device *d, struct device_attribute *a,
416 417 418
		       const char *buf, size_t count)
{
	struct drm_device *dev = dev_get_drvdata(d);
419
	struct nouveau_drm *drm = nouveau_drm(dev);
420
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
421 422 423
	int ret = -ENODEV;
	long value;

M
Martin Peres 已提交
424
	if (kstrtol(buf, 10, &value) == -EINVAL)
425 426
		return -EINVAL;

427
	ret = therm->fan_set(therm, value);
428 429 430 431 432 433
	if (ret)
		return ret;

	return count;
}

434 435 436
static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR,
			  nouveau_hwmon_get_pwm1,
			  nouveau_hwmon_set_pwm1, 0);
437 438

static ssize_t
439
nouveau_hwmon_get_pwm1_min(struct device *d,
440 441 442
			   struct device_attribute *a, char *buf)
{
	struct drm_device *dev = dev_get_drvdata(d);
443
	struct nouveau_drm *drm = nouveau_drm(dev);
444
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
445 446
	int ret;

447
	ret = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MIN_DUTY);
448 449
	if (ret < 0)
		return ret;
450

451
	return sprintf(buf, "%i\n", ret);
452 453 454
}

static ssize_t
455
nouveau_hwmon_set_pwm1_min(struct device *d, struct device_attribute *a,
456 457 458
			   const char *buf, size_t count)
{
	struct drm_device *dev = dev_get_drvdata(d);
459
	struct nouveau_drm *drm = nouveau_drm(dev);
460
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
461
	long value;
462
	int ret;
463

M
Martin Peres 已提交
464
	if (kstrtol(buf, 10, &value) == -EINVAL)
465 466
		return -EINVAL;

467
	ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MIN_DUTY, value);
468 469
	if (ret < 0)
		return ret;
470 471 472 473

	return count;
}

474 475 476
static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO | S_IWUSR,
			  nouveau_hwmon_get_pwm1_min,
			  nouveau_hwmon_set_pwm1_min, 0);
477 478

static ssize_t
479
nouveau_hwmon_get_pwm1_max(struct device *d,
480 481 482
			   struct device_attribute *a, char *buf)
{
	struct drm_device *dev = dev_get_drvdata(d);
483
	struct nouveau_drm *drm = nouveau_drm(dev);
484
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
485
	int ret;
486

487
	ret = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MAX_DUTY);
488 489
	if (ret < 0)
		return ret;
490

491
	return sprintf(buf, "%i\n", ret);
492 493 494
}

static ssize_t
495
nouveau_hwmon_set_pwm1_max(struct device *d, struct device_attribute *a,
496 497 498
			   const char *buf, size_t count)
{
	struct drm_device *dev = dev_get_drvdata(d);
499
	struct nouveau_drm *drm = nouveau_drm(dev);
500
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
501
	long value;
502
	int ret;
503

M
Martin Peres 已提交
504
	if (kstrtol(buf, 10, &value) == -EINVAL)
505 506
		return -EINVAL;

507
	ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MAX_DUTY, value);
508 509
	if (ret < 0)
		return ret;
510 511 512 513

	return count;
}

514 515 516
static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO | S_IWUSR,
			  nouveau_hwmon_get_pwm1_max,
			  nouveau_hwmon_set_pwm1_max, 0);
517

518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
static ssize_t
nouveau_hwmon_get_in0_input(struct device *d,
			    struct device_attribute *a, char *buf)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct nvkm_volt *volt = nvxx_volt(&drm->device);
	int ret;

	ret = nvkm_volt_get(volt);
	if (ret < 0)
		return ret;

	return sprintf(buf, "%i\n", ret / 1000);
}

static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO,
			  nouveau_hwmon_get_in0_input, NULL, 0);

static ssize_t
nouveau_hwmon_get_in0_label(struct device *d,
			    struct device_attribute *a, char *buf)
{
	return sprintf(buf, "GPU core\n");
}

static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO,
			  nouveau_hwmon_get_in0_label, NULL, 0);

547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
static ssize_t
nouveau_hwmon_get_power1_input(struct device *d, struct device_attribute *a,
			       char *buf)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct nvkm_iccsense *iccsense = nvxx_iccsense(&drm->device);
	int result = nvkm_iccsense_read_all(iccsense);

	if (result < 0)
		return result;

	return sprintf(buf, "%i\n", result);
}

static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO,
			  nouveau_hwmon_get_power1_input, NULL, 0);

565 566 567 568 569 570
static struct attribute *hwmon_default_attributes[] = {
	&sensor_dev_attr_name.dev_attr.attr,
	&sensor_dev_attr_update_rate.dev_attr.attr,
	NULL
};
static struct attribute *hwmon_temp_attributes[] = {
571
	&sensor_dev_attr_temp1_input.dev_attr.attr,
572 573 574
	&sensor_dev_attr_temp1_auto_point1_pwm.dev_attr.attr,
	&sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
	&sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr.attr,
575
	&sensor_dev_attr_temp1_max.dev_attr.attr,
576
	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
577
	&sensor_dev_attr_temp1_crit.dev_attr.attr,
578 579 580
	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
	&sensor_dev_attr_temp1_emergency.dev_attr.attr,
	&sensor_dev_attr_temp1_emergency_hyst.dev_attr.attr,
581 582
	NULL
};
583
static struct attribute *hwmon_fan_rpm_attributes[] = {
B
Ben Skeggs 已提交
584
	&sensor_dev_attr_fan1_input.dev_attr.attr,
585 586 587
	NULL
};
static struct attribute *hwmon_pwm_fan_attributes[] = {
588
	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
589 590 591
	&sensor_dev_attr_pwm1.dev_attr.attr,
	&sensor_dev_attr_pwm1_min.dev_attr.attr,
	&sensor_dev_attr_pwm1_max.dev_attr.attr,
592 593
	NULL
};
594

595 596 597 598 599 600
static struct attribute *hwmon_in0_attributes[] = {
	&sensor_dev_attr_in0_input.dev_attr.attr,
	&sensor_dev_attr_in0_label.dev_attr.attr,
	NULL
};

601 602 603 604 605
static struct attribute *hwmon_power_attributes[] = {
	&sensor_dev_attr_power1_input.dev_attr.attr,
	NULL
};

606 607 608 609 610
static const struct attribute_group hwmon_default_attrgroup = {
	.attrs = hwmon_default_attributes,
};
static const struct attribute_group hwmon_temp_attrgroup = {
	.attrs = hwmon_temp_attributes,
611
};
612 613 614 615 616 617
static const struct attribute_group hwmon_fan_rpm_attrgroup = {
	.attrs = hwmon_fan_rpm_attributes,
};
static const struct attribute_group hwmon_pwm_fan_attrgroup = {
	.attrs = hwmon_pwm_fan_attributes,
};
618 619 620
static const struct attribute_group hwmon_in0_attrgroup = {
	.attrs = hwmon_in0_attributes,
};
621 622 623
static const struct attribute_group hwmon_power_attrgroup = {
	.attrs = hwmon_power_attributes,
};
624
#endif
625

626
int
627 628
nouveau_hwmon_init(struct drm_device *dev)
{
629
#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
630
	struct nouveau_drm *drm = nouveau_drm(dev);
631
	struct nvkm_therm *therm = nvxx_therm(&drm->device);
632
	struct nvkm_volt *volt = nvxx_volt(&drm->device);
633
	struct nvkm_iccsense *iccsense = nvxx_iccsense(&drm->device);
634
	struct nouveau_hwmon *hwmon;
635
	struct device *hwmon_dev;
636
	int ret = 0;
637

638 639 640 641 642
	hwmon = drm->hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
	if (!hwmon)
		return -ENOMEM;
	hwmon->dev = dev;

643
	hwmon_dev = hwmon_device_register(dev->dev);
644 645
	if (IS_ERR(hwmon_dev)) {
		ret = PTR_ERR(hwmon_dev);
646
		NV_ERROR(drm, "Unable to register hwmon device: %d\n", ret);
647 648 649
		return ret;
	}
	dev_set_drvdata(hwmon_dev, dev);
650

651 652
	/* set the default attributes */
	ret = sysfs_create_group(&hwmon_dev->kobj, &hwmon_default_attrgroup);
653 654
	if (ret)
		goto error;
655

656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
	if (therm && therm->attr_get && therm->attr_set) {
		/* if the card has a working thermal sensor */
		if (nvkm_therm_temp_get(therm) >= 0) {
			ret = sysfs_create_group(&hwmon_dev->kobj, &hwmon_temp_attrgroup);
			if (ret)
				goto error;
		}

		/* if the card has a pwm fan */
		/*XXX: incorrect, need better detection for this, some boards have
		 *     the gpio entries for pwm fan control even when there's no
		 *     actual fan connected to it... therm table? */
		if (therm->fan_get && therm->fan_get(therm) >= 0) {
			ret = sysfs_create_group(&hwmon_dev->kobj,
						 &hwmon_pwm_fan_attrgroup);
			if (ret)
				goto error;
		}
674 675 676
	}

	/* if the card can read the fan rpm */
677
	if (therm && nvkm_therm_fan_sense(therm) >= 0) {
678
		ret = sysfs_create_group(&hwmon_dev->kobj,
679 680 681
					 &hwmon_fan_rpm_attrgroup);
		if (ret)
			goto error;
682 683
	}

684 685 686 687 688 689 690 691
	if (volt && nvkm_volt_get(volt) >= 0) {
		ret = sysfs_create_group(&hwmon_dev->kobj,
					 &hwmon_in0_attrgroup);

		if (ret)
			goto error;
	}

692 693 694 695 696 697 698
	if (iccsense && iccsense->data_valid && iccsense->rail_count) {
		ret = sysfs_create_group(&hwmon_dev->kobj,
					 &hwmon_power_attrgroup);
		if (ret)
			goto error;
	}

699
	hwmon->hwmon = hwmon_dev;
700

701
	return 0;
702 703

error:
704
	NV_ERROR(drm, "Unable to create some hwmon sysfs files: %d\n", ret);
705
	hwmon_device_unregister(hwmon_dev);
706
	hwmon->hwmon = NULL;
707 708 709 710
	return ret;
#else
	return 0;
#endif
711 712
}

713
void
714 715
nouveau_hwmon_fini(struct drm_device *dev)
{
716
#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
717
	struct nouveau_hwmon *hwmon = nouveau_hwmon(dev);
718

719 720 721 722 723
	if (hwmon->hwmon) {
		sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_default_attrgroup);
		sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_temp_attrgroup);
		sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_pwm_fan_attrgroup);
		sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_fan_rpm_attrgroup);
724
		sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_in0_attrgroup);
725
		sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_power_attrgroup);
726

727
		hwmon_device_unregister(hwmon->hwmon);
728
	}
729

730 731
	nouveau_drm(dev)->hwmon = NULL;
	kfree(hwmon);
732
#endif
733
}