nouveau_hwmon.c 18.6 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 38
#include <subdev/gpio.h>
#include <subdev/timer.h>
39
#include <subdev/therm.h>
40

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

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

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

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
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);
	struct nouveau_therm *therm = nouveau_therm(drm->device);

	return snprintf(buf, PAGE_SIZE, "%d\n",
	      therm->attr_get(therm, NOUVEAU_THERM_ATTR_THRS_FAN_BOOST) * 1000);
}
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);
	struct nouveau_therm *therm = nouveau_therm(drm->device);
	long value;

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

	therm->attr_set(therm, NOUVEAU_THERM_ATTR_THRS_FAN_BOOST,
			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);
	struct nouveau_therm *therm = nouveau_therm(drm->device);

	return snprintf(buf, PAGE_SIZE, "%d\n",
	 therm->attr_get(therm, NOUVEAU_THERM_ATTR_THRS_FAN_BOOST_HYST) * 1000);
}
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);
	struct nouveau_therm *therm = nouveau_therm(drm->device);
	long value;

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

	therm->attr_set(therm, NOUVEAU_THERM_ATTR_THRS_FAN_BOOST_HYST,
			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);

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

140 141
	return snprintf(buf, PAGE_SIZE, "%d\n",
	       therm->attr_get(therm, NOUVEAU_THERM_ATTR_THRS_DOWN_CLK) * 1000);
142 143 144 145 146 147
}
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);
148 149
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct nouveau_therm *therm = nouveau_therm(drm->device);
150 151
	long value;

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

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

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

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
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);
	struct nouveau_therm *therm = nouveau_therm(drm->device);

	return snprintf(buf, PAGE_SIZE, "%d\n",
	  therm->attr_get(therm, NOUVEAU_THERM_ATTR_THRS_DOWN_CLK_HYST) * 1000);
}
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);
	struct nouveau_therm *therm = nouveau_therm(drm->device);
	long value;

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

	therm->attr_set(therm, NOUVEAU_THERM_ATTR_THRS_DOWN_CLK_HYST,
			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);

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

203 204
	return snprintf(buf, PAGE_SIZE, "%d\n",
	       therm->attr_get(therm, NOUVEAU_THERM_ATTR_THRS_CRITICAL) * 1000);
205 206 207 208 209 210 211
}
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);
212 213
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct nouveau_therm *therm = nouveau_therm(drm->device);
214 215
	long value;

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

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

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

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
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);
	struct nouveau_therm *therm = nouveau_therm(drm->device);

	return snprintf(buf, PAGE_SIZE, "%d\n",
	  therm->attr_get(therm, NOUVEAU_THERM_ATTR_THRS_CRITICAL_HYST) * 1000);
}
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);
	struct nouveau_therm *therm = nouveau_therm(drm->device);
	long value;

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

	therm->attr_set(therm, NOUVEAU_THERM_ATTR_THRS_CRITICAL_HYST,
			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);
	struct nouveau_therm *therm = nouveau_therm(drm->device);

	return snprintf(buf, PAGE_SIZE, "%d\n",
	       therm->attr_get(therm, NOUVEAU_THERM_ATTR_THRS_SHUTDOWN) * 1000);
}
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);
	struct nouveau_therm *therm = nouveau_therm(drm->device);
	long value;

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

	therm->attr_set(therm, NOUVEAU_THERM_ATTR_THRS_SHUTDOWN, value / 1000);

	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);
	struct nouveau_therm *therm = nouveau_therm(drm->device);

	return snprintf(buf, PAGE_SIZE, "%d\n",
	  therm->attr_get(therm, NOUVEAU_THERM_ATTR_THRS_SHUTDOWN_HYST) * 1000);
}
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);
	struct nouveau_therm *therm = nouveau_therm(drm->device);
	long value;

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

	therm->attr_set(therm, NOUVEAU_THERM_ATTR_THRS_SHUTDOWN_HYST,
			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);

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
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);

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

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

360 361 362 363 364 365 366
 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);
	struct nouveau_therm *therm = nouveau_therm(drm->device);
367
	int ret;
368

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

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

376 377 378 379 380 381 382 383 384 385 386 387
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);
	struct nouveau_therm *therm = nouveau_therm(drm->device);
	long value;
	int ret;

	if (strict_strtol(buf, 10, &value) == -EINVAL)
		return -EINVAL;
388

389 390 391 392 393
	ret = therm->attr_set(therm, NOUVEAU_THERM_ATTR_FAN_MODE, value);
	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 404
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct nouveau_therm *therm = nouveau_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 420
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct nouveau_therm *therm = nouveau_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 444 445 446 447 448 449
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct nouveau_therm *therm = nouveau_therm(drm->device);
	int ret;

	ret = therm->attr_get(therm, NOUVEAU_THERM_ATTR_FAN_MIN_DUTY);
	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 460
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct nouveau_therm *therm = nouveau_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 468 469
	ret = therm->attr_set(therm, NOUVEAU_THERM_ATTR_FAN_MIN_DUTY, value);
	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 484 485
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct nouveau_therm *therm = nouveau_therm(drm->device);
	int ret;
486

487 488 489
	ret = therm->attr_get(therm, NOUVEAU_THERM_ATTR_FAN_MAX_DUTY);
	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 500
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct nouveau_therm *therm = nouveau_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 508 509
	ret = therm->attr_set(therm, NOUVEAU_THERM_ATTR_FAN_MAX_DUTY, value);
	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
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[] = {
524
	&sensor_dev_attr_temp1_input.dev_attr.attr,
525 526 527
	&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,
528
	&sensor_dev_attr_temp1_max.dev_attr.attr,
529
	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
530
	&sensor_dev_attr_temp1_crit.dev_attr.attr,
531 532 533
	&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,
534 535
	NULL
};
536
static struct attribute *hwmon_fan_rpm_attributes[] = {
B
Ben Skeggs 已提交
537
	&sensor_dev_attr_fan1_input.dev_attr.attr,
538 539 540
	NULL
};
static struct attribute *hwmon_pwm_fan_attributes[] = {
541
	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
542 543 544
	&sensor_dev_attr_pwm1.dev_attr.attr,
	&sensor_dev_attr_pwm1_min.dev_attr.attr,
	&sensor_dev_attr_pwm1_max.dev_attr.attr,
545 546
	NULL
};
547

548 549 550 551 552
static const struct attribute_group hwmon_default_attrgroup = {
	.attrs = hwmon_default_attributes,
};
static const struct attribute_group hwmon_temp_attrgroup = {
	.attrs = hwmon_temp_attributes,
553
};
554 555 556 557 558 559
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,
};
560
#endif
561

562
int
563 564
nouveau_hwmon_init(struct drm_device *dev)
{
565
#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
566 567
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct nouveau_therm *therm = nouveau_therm(drm->device);
568
	struct nouveau_hwmon *hwmon;
569
	struct device *hwmon_dev;
570
	int ret = 0;
571

572 573 574 575 576
	hwmon = drm->hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
	if (!hwmon)
		return -ENOMEM;
	hwmon->dev = dev;

577
	if (!therm || !therm->temp_get || !therm->attr_get || !therm->attr_set)
578
		return -ENODEV;
579 580 581 582

	hwmon_dev = hwmon_device_register(&dev->pdev->dev);
	if (IS_ERR(hwmon_dev)) {
		ret = PTR_ERR(hwmon_dev);
583
		NV_ERROR(drm, "Unable to register hwmon device: %d\n", ret);
584 585 586
		return ret;
	}
	dev_set_drvdata(hwmon_dev, dev);
587

588 589
	/* set the default attributes */
	ret = sysfs_create_group(&hwmon_dev->kobj, &hwmon_default_attrgroup);
590
	if (ret) {
591 592 593 594
		if (ret)
			goto error;
	}

595 596 597 598 599 600 601 602 603
	/* if the card has a working thermal sensor */
	if (therm->temp_get(therm) >= 0) {
		ret = sysfs_create_group(&hwmon_dev->kobj, &hwmon_temp_attrgroup);
		if (ret) {
			if (ret)
				goto error;
		}
	}

604 605 606 607
	/* 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? */
608
	if (therm->fan_get && therm->fan_get(therm) >= 0) {
609
		ret = sysfs_create_group(&hwmon_dev->kobj,
610 611 612 613 614 615
					 &hwmon_pwm_fan_attrgroup);
		if (ret)
			goto error;
	}

	/* if the card can read the fan rpm */
616
	if (therm->fan_sense(therm) >= 0) {
617
		ret = sysfs_create_group(&hwmon_dev->kobj,
618 619 620
					 &hwmon_fan_rpm_attrgroup);
		if (ret)
			goto error;
621 622
	}

623
	hwmon->hwmon = hwmon_dev;
624

625
	return 0;
626 627

error:
628
	NV_ERROR(drm, "Unable to create some hwmon sysfs files: %d\n", ret);
629
	hwmon_device_unregister(hwmon_dev);
630
	hwmon->hwmon = NULL;
631 632 633 634
	return ret;
#else
	return 0;
#endif
635 636
}

637
void
638 639
nouveau_hwmon_fini(struct drm_device *dev)
{
640
#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
641
	struct nouveau_hwmon *hwmon = nouveau_hwmon(dev);
642

643 644 645 646 647
	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);
648

649
		hwmon_device_unregister(hwmon->hwmon);
650
	}
651

652 653
	nouveau_drm(dev)->hwmon = NULL;
	kfree(hwmon);
654
#endif
655
}