nouveau_hwmon.c 27.9 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_drv.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->client.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->client.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->client.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->client.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->client.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->client.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->client.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->client.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->client.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->client.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->client.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->client.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->client.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->client.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->client.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->client.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->client.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->client.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->client.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->client.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->client.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->client.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->client.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->client.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->client.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->client.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
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);
524
	struct nvkm_volt *volt = nvxx_volt(&drm->client.device);
525 526 527 528 529 530 531 532 533 534 535 536
	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);

537 538 539 540 541 542
static ssize_t
nouveau_hwmon_get_in0_min(struct device *d,
			    struct device_attribute *a, char *buf)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct nouveau_drm *drm = nouveau_drm(dev);
543
	struct nvkm_volt *volt = nvxx_volt(&drm->client.device);
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559

	if (!volt || !volt->min_uv)
		return -ENODEV;

	return sprintf(buf, "%i\n", volt->min_uv / 1000);
}

static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO,
			  nouveau_hwmon_get_in0_min, NULL, 0);

static ssize_t
nouveau_hwmon_get_in0_max(struct device *d,
			    struct device_attribute *a, char *buf)
{
	struct drm_device *dev = dev_get_drvdata(d);
	struct nouveau_drm *drm = nouveau_drm(dev);
560
	struct nvkm_volt *volt = nvxx_volt(&drm->client.device);
561 562 563 564 565 566 567 568 569 570

	if (!volt || !volt->max_uv)
		return -ENODEV;

	return sprintf(buf, "%i\n", volt->max_uv / 1000);
}

static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO,
			  nouveau_hwmon_get_in0_max, NULL, 0);

571 572 573 574 575 576 577 578 579 580
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);

581 582 583 584 585 586
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);
587
	struct nvkm_iccsense *iccsense = nvxx_iccsense(&drm->client.device);
588 589 590 591 592 593 594 595 596 597 598
	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);

599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
static ssize_t
nouveau_hwmon_get_power1_max(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->client.device);
	return sprintf(buf, "%i\n", iccsense->power_w_max);
}

static SENSOR_DEVICE_ATTR(power1_max, S_IRUGO,
			  nouveau_hwmon_get_power1_max, NULL, 0);

static ssize_t
nouveau_hwmon_get_power1_crit(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->client.device);
	return sprintf(buf, "%i\n", iccsense->power_w_crit);
}

static SENSOR_DEVICE_ATTR(power1_crit, S_IRUGO,
			  nouveau_hwmon_get_power1_crit, NULL, 0);

625 626 627 628 629 630
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[] = {
631
	&sensor_dev_attr_temp1_input.dev_attr.attr,
632 633 634
	&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,
635
	&sensor_dev_attr_temp1_max.dev_attr.attr,
636
	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
637
	&sensor_dev_attr_temp1_crit.dev_attr.attr,
638 639 640
	&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,
641 642
	NULL
};
643
static struct attribute *hwmon_fan_rpm_attributes[] = {
B
Ben Skeggs 已提交
644
	&sensor_dev_attr_fan1_input.dev_attr.attr,
645 646 647
	NULL
};
static struct attribute *hwmon_pwm_fan_attributes[] = {
648
	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
649 650 651
	&sensor_dev_attr_pwm1.dev_attr.attr,
	&sensor_dev_attr_pwm1_min.dev_attr.attr,
	&sensor_dev_attr_pwm1_max.dev_attr.attr,
652 653
	NULL
};
654

655 656
static struct attribute *hwmon_in0_attributes[] = {
	&sensor_dev_attr_in0_input.dev_attr.attr,
657 658
	&sensor_dev_attr_in0_min.dev_attr.attr,
	&sensor_dev_attr_in0_max.dev_attr.attr,
659 660 661 662
	&sensor_dev_attr_in0_label.dev_attr.attr,
	NULL
};

663 664 665 666 667
static struct attribute *hwmon_power_attributes[] = {
	&sensor_dev_attr_power1_input.dev_attr.attr,
	NULL
};

668 669 670 671 672 673
static struct attribute *hwmon_power_caps_attributes[] = {
	&sensor_dev_attr_power1_max.dev_attr.attr,
	&sensor_dev_attr_power1_crit.dev_attr.attr,
	NULL
};

674 675 676 677 678
static const struct attribute_group hwmon_default_attrgroup = {
	.attrs = hwmon_default_attributes,
};
static const struct attribute_group hwmon_temp_attrgroup = {
	.attrs = hwmon_temp_attributes,
679
};
680 681 682 683 684 685
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,
};
686 687 688
static const struct attribute_group hwmon_in0_attrgroup = {
	.attrs = hwmon_in0_attributes,
};
689 690 691
static const struct attribute_group hwmon_power_attrgroup = {
	.attrs = hwmon_power_attributes,
};
692 693 694
static const struct attribute_group hwmon_power_caps_attrgroup = {
	.attrs = hwmon_power_caps_attributes,
};
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766

static const u32 nouveau_config_chip[] = {
	HWMON_C_UPDATE_INTERVAL,
	0
};

static const u32 nouveau_config_in[] = {
	HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_LABEL,
	0
};

static const u32 nouveau_config_temp[] = {
	HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST |
	HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_EMERGENCY |
	HWMON_T_EMERGENCY_HYST,
	0
};

static const u32 nouveau_config_fan[] = {
	HWMON_F_INPUT,
	0
};

static const u32 nouveau_config_pwm[] = {
	HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
	0
};

static const u32 nouveau_config_power[] = {
	HWMON_P_INPUT | HWMON_P_CAP_MAX | HWMON_P_CRIT,
	0
};

static const struct hwmon_channel_info nouveau_chip = {
	.type = hwmon_chip,
	.config = nouveau_config_chip,
};

static const struct hwmon_channel_info nouveau_temp = {
	.type = hwmon_temp,
	.config = nouveau_config_temp,
};

static const struct hwmon_channel_info nouveau_fan = {
	.type = hwmon_fan,
	.config = nouveau_config_fan,
};

static const struct hwmon_channel_info nouveau_in = {
	.type = hwmon_in,
	.config = nouveau_config_in,
};

static const struct hwmon_channel_info nouveau_pwm = {
	.type = hwmon_pwm,
	.config = nouveau_config_pwm,
};

static const struct hwmon_channel_info nouveau_power = {
	.type = hwmon_power,
	.config = nouveau_config_power,
};

static const struct hwmon_channel_info *nouveau_info[] = {
	&nouveau_chip,
	&nouveau_temp,
	&nouveau_fan,
	&nouveau_in,
	&nouveau_pwm,
	&nouveau_power,
	NULL
};
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929

static umode_t
nouveau_chip_is_visible(const void *data, u32 attr, int channel)
{
	switch (attr) {
	case hwmon_chip_update_interval:
		return 0444;
	default:
		return 0;
	}
}

static umode_t
nouveau_power_is_visible(const void *data, u32 attr, int channel)
{
	struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
	struct nvkm_iccsense *iccsense = nvxx_iccsense(&drm->client.device);

	if (!iccsense || !iccsense->data_valid || list_empty(&iccsense->rails))
		return 0;

	switch (attr) {
	case hwmon_power_input:
		return 0444;
	case hwmon_power_max:
		if (iccsense->power_w_max)
			return 0444;
		return 0;
	case hwmon_power_crit:
		if (iccsense->power_w_crit)
			return 0444;
		return 0;
	default:
		return 0;
	}
}

static umode_t
nouveau_temp_is_visible(const void *data, u32 attr, int channel)
{
	struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);

	if (therm && therm->attr_get && nvkm_therm_temp_get(therm) < 0)
		return 0;

	switch (attr) {
	case hwmon_temp_input:
	case hwmon_temp_max:
	case hwmon_temp_max_hyst:
	case hwmon_temp_crit:
	case hwmon_temp_crit_hyst:
	case hwmon_temp_emergency:
	case hwmon_temp_emergency_hyst:
		return 0444;
	default:
		return 0;
	}
}

static umode_t
nouveau_pwm_is_visible(const void *data, u32 attr, int channel)
{
	struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);

	if (therm && therm->attr_get && therm->fan_get &&
				therm->fan_get(therm) < 0)
		return 0;

	switch (attr) {
	case hwmon_pwm_enable:
	case hwmon_pwm_input:
		return 0644;
	default:
		return 0;
	}
}

static umode_t
nouveau_input_is_visible(const void *data, u32 attr, int channel)
{
	struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
	struct nvkm_volt *volt = nvxx_volt(&drm->client.device);

	if (!volt || nvkm_volt_get(volt) < 0)
		return 0;

	switch (attr) {
	case hwmon_in_input:
	case hwmon_in_label:
	case hwmon_in_min:
	case hwmon_in_max:
		return 0444;
	default:
		return 0;
	}
}

static umode_t
nouveau_fan_is_visible(const void *data, u32 attr, int channel)
{
	struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);

	if (!therm || !therm->attr_get || nvkm_therm_fan_sense(therm) < 0)
		return 0;

	switch (attr) {
	case hwmon_fan_input:
		return 0444;
	default:
		return 0;
	}
}

static umode_t
nouveau_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr,
			int channel)
{
	switch (type) {
	case hwmon_chip:
		return nouveau_chip_is_visible(data, attr, channel);
	case hwmon_temp:
		return nouveau_temp_is_visible(data, attr, channel);
	case hwmon_fan:
		return nouveau_fan_is_visible(data, attr, channel);
	case hwmon_in:
		return nouveau_input_is_visible(data, attr, channel);
	case hwmon_pwm:
		return nouveau_pwm_is_visible(data, attr, channel);
	case hwmon_power:
		return nouveau_power_is_visible(data, attr, channel);
	default:
		return 0;
	}
}

static const char input_label[] = "GPU core";

static int
nouveau_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr,
		    int channel, const char **buf)
{
	if (type == hwmon_in && attr == hwmon_in_label) {
		*buf = input_label;
		return 0;
	}

	return -EOPNOTSUPP;
}

static const struct hwmon_ops nouveau_hwmon_ops = {
	.is_visible = nouveau_is_visible,
	.read = NULL,
	.read_string = nouveau_read_string,
	.write = NULL,
};

static const struct hwmon_chip_info nouveau_chip_info = {
	.ops = &nouveau_hwmon_ops,
	.info = nouveau_info,
};
930
#endif
931

932
int
933 934
nouveau_hwmon_init(struct drm_device *dev)
{
935
#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
936
	struct nouveau_drm *drm = nouveau_drm(dev);
937 938 939
	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
	struct nvkm_volt *volt = nvxx_volt(&drm->client.device);
	struct nvkm_iccsense *iccsense = nvxx_iccsense(&drm->client.device);
940
	struct nouveau_hwmon *hwmon;
941
	struct device *hwmon_dev;
942
	int ret = 0;
943

944 945 946 947 948
	hwmon = drm->hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
	if (!hwmon)
		return -ENOMEM;
	hwmon->dev = dev;

949
	hwmon_dev = hwmon_device_register(dev->dev);
950 951
	if (IS_ERR(hwmon_dev)) {
		ret = PTR_ERR(hwmon_dev);
952
		NV_ERROR(drm, "Unable to register hwmon device: %d\n", ret);
953 954 955
		return ret;
	}
	dev_set_drvdata(hwmon_dev, dev);
956

957 958
	/* set the default attributes */
	ret = sysfs_create_group(&hwmon_dev->kobj, &hwmon_default_attrgroup);
959 960
	if (ret)
		goto error;
961

962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
	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;
		}
980 981 982
	}

	/* if the card can read the fan rpm */
983
	if (therm && nvkm_therm_fan_sense(therm) >= 0) {
984
		ret = sysfs_create_group(&hwmon_dev->kobj,
985 986 987
					 &hwmon_fan_rpm_attrgroup);
		if (ret)
			goto error;
988 989
	}

990 991 992 993 994 995 996 997
	if (volt && nvkm_volt_get(volt) >= 0) {
		ret = sysfs_create_group(&hwmon_dev->kobj,
					 &hwmon_in0_attrgroup);

		if (ret)
			goto error;
	}

998
	if (iccsense && iccsense->data_valid && !list_empty(&iccsense->rails)) {
999 1000
		ret = sysfs_create_group(&hwmon_dev->kobj,
					 &hwmon_power_attrgroup);
1001

1002 1003
		if (ret)
			goto error;
1004 1005 1006 1007 1008 1009 1010

		if (iccsense->power_w_max && iccsense->power_w_crit) {
			ret = sysfs_create_group(&hwmon_dev->kobj,
						 &hwmon_power_caps_attrgroup);
			if (ret)
				goto error;
		}
1011 1012
	}

1013
	hwmon->hwmon = hwmon_dev;
1014

1015
	return 0;
1016 1017

error:
1018
	NV_ERROR(drm, "Unable to create some hwmon sysfs files: %d\n", ret);
1019
	hwmon_device_unregister(hwmon_dev);
1020
	hwmon->hwmon = NULL;
1021 1022 1023 1024
	return ret;
#else
	return 0;
#endif
1025 1026
}

1027
void
1028 1029
nouveau_hwmon_fini(struct drm_device *dev)
{
1030
#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
1031
	struct nouveau_hwmon *hwmon = nouveau_hwmon(dev);
1032

1033 1034 1035 1036 1037
	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);
1038
		sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_in0_attrgroup);
1039
		sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_power_attrgroup);
1040
		sysfs_remove_group(&hwmon->hwmon->kobj, &hwmon_power_caps_attrgroup);
1041

1042
		hwmon_device_unregister(hwmon->hwmon);
1043
	}
1044

1045 1046
	nouveau_drm(dev)->hwmon = NULL;
	kfree(hwmon);
1047
#endif
1048
}