Linux Audio

Check our new training course

Loading...
v6.13.7
  1/*
  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#ifdef CONFIG_ACPI
 26#include <linux/acpi.h>
 27#endif
 28#include <linux/power_supply.h>
 29#include <linux/hwmon.h>
 30#include <linux/hwmon-sysfs.h>
 31
 
 
 32#include "nouveau_drv.h"
 33#include "nouveau_hwmon.h"
 34
 35#include <nvkm/subdev/iccsense.h>
 36#include <nvkm/subdev/volt.h>
 37
 38#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
 39
 40static ssize_t
 41nouveau_hwmon_show_temp1_auto_point1_pwm(struct device *d,
 42					 struct device_attribute *a, char *buf)
 43{
 44	return sysfs_emit(buf, "%d\n", 100);
 45}
 46static SENSOR_DEVICE_ATTR(temp1_auto_point1_pwm, 0444,
 47			  nouveau_hwmon_show_temp1_auto_point1_pwm, NULL, 0);
 48
 49static ssize_t
 50nouveau_hwmon_temp1_auto_point1_temp(struct device *d,
 51				     struct device_attribute *a, char *buf)
 52{
 53	struct drm_device *dev = dev_get_drvdata(d);
 54	struct nouveau_drm *drm = nouveau_drm(dev);
 55	struct nvkm_therm *therm = nvxx_therm(drm);
 56
 57	return sysfs_emit(buf, "%d\n",
 58			  therm->attr_get(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST) * 1000);
 59}
 60static ssize_t
 61nouveau_hwmon_set_temp1_auto_point1_temp(struct device *d,
 62					 struct device_attribute *a,
 63					 const char *buf, size_t count)
 64{
 65	struct drm_device *dev = dev_get_drvdata(d);
 66	struct nouveau_drm *drm = nouveau_drm(dev);
 67	struct nvkm_therm *therm = nvxx_therm(drm);
 68	long value;
 69
 70	if (kstrtol(buf, 10, &value))
 71		return -EINVAL;
 72
 73	therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST,
 74			value / 1000);
 75
 76	return count;
 77}
 78static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp, 0644,
 79			  nouveau_hwmon_temp1_auto_point1_temp,
 80			  nouveau_hwmon_set_temp1_auto_point1_temp, 0);
 81
 82static ssize_t
 83nouveau_hwmon_temp1_auto_point1_temp_hyst(struct device *d,
 84					  struct device_attribute *a, char *buf)
 85{
 86	struct drm_device *dev = dev_get_drvdata(d);
 87	struct nouveau_drm *drm = nouveau_drm(dev);
 88	struct nvkm_therm *therm = nvxx_therm(drm);
 89
 90	return sysfs_emit(buf, "%d\n",
 91			  therm->attr_get(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST) * 1000);
 92}
 93static ssize_t
 94nouveau_hwmon_set_temp1_auto_point1_temp_hyst(struct device *d,
 95					      struct device_attribute *a,
 96					      const char *buf, size_t count)
 97{
 98	struct drm_device *dev = dev_get_drvdata(d);
 99	struct nouveau_drm *drm = nouveau_drm(dev);
100	struct nvkm_therm *therm = nvxx_therm(drm);
101	long value;
102
103	if (kstrtol(buf, 10, &value))
104		return -EINVAL;
105
106	therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST,
107			value / 1000);
108
109	return count;
110}
111static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp_hyst, 0644,
112			  nouveau_hwmon_temp1_auto_point1_temp_hyst,
113			  nouveau_hwmon_set_temp1_auto_point1_temp_hyst, 0);
114
115static ssize_t
116nouveau_hwmon_get_pwm1_max(struct device *d,
117			   struct device_attribute *a, char *buf)
118{
119	struct drm_device *dev = dev_get_drvdata(d);
120	struct nouveau_drm *drm = nouveau_drm(dev);
121	struct nvkm_therm *therm = nvxx_therm(drm);
122	int ret;
123
124	ret = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MAX_DUTY);
125	if (ret < 0)
126		return ret;
127
128	return sprintf(buf, "%i\n", ret);
129}
130
131static ssize_t
132nouveau_hwmon_get_pwm1_min(struct device *d,
133			   struct device_attribute *a, char *buf)
134{
135	struct drm_device *dev = dev_get_drvdata(d);
136	struct nouveau_drm *drm = nouveau_drm(dev);
137	struct nvkm_therm *therm = nvxx_therm(drm);
138	int ret;
139
140	ret = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MIN_DUTY);
141	if (ret < 0)
142		return ret;
143
144	return sprintf(buf, "%i\n", ret);
145}
146
147static ssize_t
148nouveau_hwmon_set_pwm1_min(struct device *d, struct device_attribute *a,
149			   const char *buf, size_t count)
150{
151	struct drm_device *dev = dev_get_drvdata(d);
152	struct nouveau_drm *drm = nouveau_drm(dev);
153	struct nvkm_therm *therm = nvxx_therm(drm);
154	long value;
155	int ret;
156
157	if (kstrtol(buf, 10, &value))
158		return -EINVAL;
159
160	ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MIN_DUTY, value);
161	if (ret < 0)
162		return ret;
163
164	return count;
165}
166static SENSOR_DEVICE_ATTR(pwm1_min, 0644,
167			  nouveau_hwmon_get_pwm1_min,
168			  nouveau_hwmon_set_pwm1_min, 0);
169
170static ssize_t
171nouveau_hwmon_set_pwm1_max(struct device *d, struct device_attribute *a,
172			   const char *buf, size_t count)
173{
174	struct drm_device *dev = dev_get_drvdata(d);
175	struct nouveau_drm *drm = nouveau_drm(dev);
176	struct nvkm_therm *therm = nvxx_therm(drm);
177	long value;
178	int ret;
179
180	if (kstrtol(buf, 10, &value))
181		return -EINVAL;
182
183	ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MAX_DUTY, value);
184	if (ret < 0)
185		return ret;
186
187	return count;
188}
189static SENSOR_DEVICE_ATTR(pwm1_max, 0644,
190			  nouveau_hwmon_get_pwm1_max,
191			  nouveau_hwmon_set_pwm1_max, 0);
192
193static struct attribute *pwm_fan_sensor_attrs[] = {
194	&sensor_dev_attr_pwm1_min.dev_attr.attr,
195	&sensor_dev_attr_pwm1_max.dev_attr.attr,
196	NULL
197};
198static const struct attribute_group pwm_fan_sensor_group = {
199	.attrs = pwm_fan_sensor_attrs,
200};
201
202static struct attribute *temp1_auto_point_sensor_attrs[] = {
203	&sensor_dev_attr_temp1_auto_point1_pwm.dev_attr.attr,
204	&sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
205	&sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr.attr,
206	NULL
207};
208static const struct attribute_group temp1_auto_point_sensor_group = {
209	.attrs = temp1_auto_point_sensor_attrs,
210};
211
212#define N_ATTR_GROUPS   3
213
214static const struct hwmon_channel_info * const nouveau_info[] = {
215	HWMON_CHANNEL_INFO(chip,
216			   HWMON_C_UPDATE_INTERVAL),
217	HWMON_CHANNEL_INFO(temp,
218			   HWMON_T_INPUT |
219			   HWMON_T_MAX | HWMON_T_MAX_HYST |
220			   HWMON_T_CRIT | HWMON_T_CRIT_HYST |
221			   HWMON_T_EMERGENCY | HWMON_T_EMERGENCY_HYST),
222	HWMON_CHANNEL_INFO(fan,
223			   HWMON_F_INPUT),
224	HWMON_CHANNEL_INFO(in,
225			   HWMON_I_INPUT |
226			   HWMON_I_MIN | HWMON_I_MAX |
227			   HWMON_I_LABEL),
228	HWMON_CHANNEL_INFO(pwm,
229			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
230	HWMON_CHANNEL_INFO(power,
231			   HWMON_P_INPUT | HWMON_P_CAP_MAX | HWMON_P_CRIT),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232	NULL
233};
234
235static umode_t
236nouveau_chip_is_visible(const void *data, u32 attr, int channel)
237{
238	switch (attr) {
239	case hwmon_chip_update_interval:
240		return 0444;
241	default:
242		return 0;
243	}
244}
245
246static umode_t
247nouveau_power_is_visible(const void *data, u32 attr, int channel)
248{
249	struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
250	struct nvkm_iccsense *iccsense = nvxx_iccsense(drm);
251
252	if (!iccsense || !iccsense->data_valid || list_empty(&iccsense->rails))
253		return 0;
254
255	switch (attr) {
256	case hwmon_power_input:
257		return 0444;
258	case hwmon_power_max:
259		if (iccsense->power_w_max)
260			return 0444;
261		return 0;
262	case hwmon_power_crit:
263		if (iccsense->power_w_crit)
264			return 0444;
265		return 0;
266	default:
267		return 0;
268	}
269}
270
271static umode_t
272nouveau_temp_is_visible(const void *data, u32 attr, int channel)
273{
274	struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
275	struct nvkm_therm *therm = nvxx_therm(drm);
276
277	if (!therm || !therm->attr_get || nvkm_therm_temp_get(therm) < 0)
278		return 0;
279
280	switch (attr) {
281	case hwmon_temp_input:
282		return 0444;
283	case hwmon_temp_max:
284	case hwmon_temp_max_hyst:
285	case hwmon_temp_crit:
286	case hwmon_temp_crit_hyst:
287	case hwmon_temp_emergency:
288	case hwmon_temp_emergency_hyst:
289		return 0644;
290	default:
291		return 0;
292	}
293}
294
295static umode_t
296nouveau_pwm_is_visible(const void *data, u32 attr, int channel)
297{
298	struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
299	struct nvkm_therm *therm = nvxx_therm(drm);
300
301	if (!therm || !therm->attr_get || !therm->fan_get ||
302	    therm->fan_get(therm) < 0)
303		return 0;
304
305	switch (attr) {
306	case hwmon_pwm_enable:
307	case hwmon_pwm_input:
308		return 0644;
309	default:
310		return 0;
311	}
312}
313
314static umode_t
315nouveau_input_is_visible(const void *data, u32 attr, int channel)
316{
317	struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
318	struct nvkm_volt *volt = nvxx_volt(drm);
319
320	if (!volt || nvkm_volt_get(volt) < 0)
321		return 0;
322
323	switch (attr) {
324	case hwmon_in_input:
325	case hwmon_in_label:
326	case hwmon_in_min:
327	case hwmon_in_max:
328		return 0444;
329	default:
330		return 0;
331	}
332}
333
334static umode_t
335nouveau_fan_is_visible(const void *data, u32 attr, int channel)
336{
337	struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
338	struct nvkm_therm *therm = nvxx_therm(drm);
339
340	if (!therm || !therm->attr_get || nvkm_therm_fan_sense(therm) < 0)
341		return 0;
342
343	switch (attr) {
344	case hwmon_fan_input:
345		return 0444;
346	default:
347		return 0;
348	}
349}
350
351static int
352nouveau_chip_read(struct device *dev, u32 attr, int channel, long *val)
353{
354	switch (attr) {
355	case hwmon_chip_update_interval:
356		*val = 1000;
357		break;
358	default:
359		return -EOPNOTSUPP;
360	}
361
362	return 0;
363}
364
365static int
366nouveau_temp_read(struct device *dev, u32 attr, int channel, long *val)
367{
368	struct drm_device *drm_dev = dev_get_drvdata(dev);
369	struct nouveau_drm *drm = nouveau_drm(drm_dev);
370	struct nvkm_therm *therm = nvxx_therm(drm);
371	int ret;
372
373	if (!therm || !therm->attr_get)
374		return -EOPNOTSUPP;
375
376	switch (attr) {
377	case hwmon_temp_input:
378		if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
379			return -EINVAL;
380		ret = nvkm_therm_temp_get(therm);
381		*val = ret < 0 ? ret : (ret * 1000);
382		break;
383	case hwmon_temp_max:
384		*val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK)
385					* 1000;
386		break;
387	case hwmon_temp_max_hyst:
388		*val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST)
389					* 1000;
390		break;
391	case hwmon_temp_crit:
392		*val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_CRITICAL)
393					* 1000;
394		break;
395	case hwmon_temp_crit_hyst:
396		*val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_CRITICAL_HYST)
397					* 1000;
398		break;
399	case hwmon_temp_emergency:
400		*val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN)
401					* 1000;
402		break;
403	case hwmon_temp_emergency_hyst:
404		*val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST)
405					* 1000;
406		break;
407	default:
408		return -EOPNOTSUPP;
409	}
410
411	return 0;
412}
413
414static int
415nouveau_fan_read(struct device *dev, u32 attr, int channel, long *val)
416{
417	struct drm_device *drm_dev = dev_get_drvdata(dev);
418	struct nouveau_drm *drm = nouveau_drm(drm_dev);
419	struct nvkm_therm *therm = nvxx_therm(drm);
420
421	if (!therm)
422		return -EOPNOTSUPP;
423
424	switch (attr) {
425	case hwmon_fan_input:
426		if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
427			return -EINVAL;
428		*val = nvkm_therm_fan_sense(therm);
429		break;
430	default:
431		return -EOPNOTSUPP;
432	}
433
434	return 0;
435}
436
437static int
438nouveau_in_read(struct device *dev, u32 attr, int channel, long *val)
439{
440	struct drm_device *drm_dev = dev_get_drvdata(dev);
441	struct nouveau_drm *drm = nouveau_drm(drm_dev);
442	struct nvkm_volt *volt = nvxx_volt(drm);
443	int ret;
444
445	if (!volt)
446		return -EOPNOTSUPP;
447
448	switch (attr) {
449	case hwmon_in_input:
450		if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
451			return -EINVAL;
452		ret = nvkm_volt_get(volt);
453		*val = ret < 0 ? ret : (ret / 1000);
454		break;
455	case hwmon_in_min:
456		*val = volt->min_uv > 0 ? (volt->min_uv / 1000) : -ENODEV;
457		break;
458	case hwmon_in_max:
459		*val = volt->max_uv > 0 ? (volt->max_uv / 1000) : -ENODEV;
460		break;
461	default:
462		return -EOPNOTSUPP;
463	}
464
465	return 0;
466}
467
468static int
469nouveau_pwm_read(struct device *dev, u32 attr, int channel, long *val)
470{
471	struct drm_device *drm_dev = dev_get_drvdata(dev);
472	struct nouveau_drm *drm = nouveau_drm(drm_dev);
473	struct nvkm_therm *therm = nvxx_therm(drm);
474
475	if (!therm || !therm->attr_get || !therm->fan_get)
476		return -EOPNOTSUPP;
477
478	switch (attr) {
479	case hwmon_pwm_enable:
480		*val = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MODE);
481		break;
482	case hwmon_pwm_input:
483		if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
484			return -EINVAL;
485		*val = therm->fan_get(therm);
486		break;
487	default:
488		return -EOPNOTSUPP;
489	}
490
491	return 0;
492}
493
494static int
495nouveau_power_read(struct device *dev, u32 attr, int channel, long *val)
496{
497	struct drm_device *drm_dev = dev_get_drvdata(dev);
498	struct nouveau_drm *drm = nouveau_drm(drm_dev);
499	struct nvkm_iccsense *iccsense = nvxx_iccsense(drm);
500
501	if (!iccsense)
502		return -EOPNOTSUPP;
503
504	switch (attr) {
505	case hwmon_power_input:
506		if (drm_dev->switch_power_state != DRM_SWITCH_POWER_ON)
507			return -EINVAL;
508		*val = nvkm_iccsense_read_all(iccsense);
509		break;
510	case hwmon_power_max:
511		*val = iccsense->power_w_max;
512		break;
513	case hwmon_power_crit:
514		*val = iccsense->power_w_crit;
515		break;
516	default:
517		return -EOPNOTSUPP;
518	}
519
520	return 0;
521}
522
523static int
524nouveau_temp_write(struct device *dev, u32 attr, int channel, long val)
525{
526	struct drm_device *drm_dev = dev_get_drvdata(dev);
527	struct nouveau_drm *drm = nouveau_drm(drm_dev);
528	struct nvkm_therm *therm = nvxx_therm(drm);
529
530	if (!therm || !therm->attr_set)
531		return -EOPNOTSUPP;
532
533	switch (attr) {
534	case hwmon_temp_max:
535		return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK,
536					val / 1000);
537	case hwmon_temp_max_hyst:
538		return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST,
539					val / 1000);
540	case hwmon_temp_crit:
541		return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_CRITICAL,
542					val / 1000);
543	case hwmon_temp_crit_hyst:
544		return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_CRITICAL_HYST,
545					val / 1000);
546	case hwmon_temp_emergency:
547		return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN,
548					val / 1000);
549	case hwmon_temp_emergency_hyst:
550		return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST,
551					val / 1000);
552	default:
553		return -EOPNOTSUPP;
554	}
555}
556
557static int
558nouveau_pwm_write(struct device *dev, u32 attr, int channel, long val)
559{
560	struct drm_device *drm_dev = dev_get_drvdata(dev);
561	struct nouveau_drm *drm = nouveau_drm(drm_dev);
562	struct nvkm_therm *therm = nvxx_therm(drm);
563
564	if (!therm || !therm->attr_set)
565		return -EOPNOTSUPP;
566
567	switch (attr) {
568	case hwmon_pwm_input:
569		return therm->fan_set(therm, val);
570	case hwmon_pwm_enable:
571		return therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MODE, val);
572	default:
573		return -EOPNOTSUPP;
574	}
575}
576
577static umode_t
578nouveau_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr,
579			int channel)
580{
581	switch (type) {
582	case hwmon_chip:
583		return nouveau_chip_is_visible(data, attr, channel);
584	case hwmon_temp:
585		return nouveau_temp_is_visible(data, attr, channel);
586	case hwmon_fan:
587		return nouveau_fan_is_visible(data, attr, channel);
588	case hwmon_in:
589		return nouveau_input_is_visible(data, attr, channel);
590	case hwmon_pwm:
591		return nouveau_pwm_is_visible(data, attr, channel);
592	case hwmon_power:
593		return nouveau_power_is_visible(data, attr, channel);
594	default:
595		return 0;
596	}
597}
598
599static const char input_label[] = "GPU core";
600
601static int
602nouveau_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr,
603		    int channel, const char **buf)
604{
605	if (type == hwmon_in && attr == hwmon_in_label) {
606		*buf = input_label;
607		return 0;
608	}
609
610	return -EOPNOTSUPP;
611}
612
613static int
614nouveau_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
615							int channel, long *val)
616{
617	switch (type) {
618	case hwmon_chip:
619		return nouveau_chip_read(dev, attr, channel, val);
620	case hwmon_temp:
621		return nouveau_temp_read(dev, attr, channel, val);
622	case hwmon_fan:
623		return nouveau_fan_read(dev, attr, channel, val);
624	case hwmon_in:
625		return nouveau_in_read(dev, attr, channel, val);
626	case hwmon_pwm:
627		return nouveau_pwm_read(dev, attr, channel, val);
628	case hwmon_power:
629		return nouveau_power_read(dev, attr, channel, val);
630	default:
631		return -EOPNOTSUPP;
632	}
633}
634
635static int
636nouveau_write(struct device *dev, enum hwmon_sensor_types type, u32 attr,
637							int channel, long val)
638{
639	switch (type) {
640	case hwmon_temp:
641		return nouveau_temp_write(dev, attr, channel, val);
642	case hwmon_pwm:
643		return nouveau_pwm_write(dev, attr, channel, val);
644	default:
645		return -EOPNOTSUPP;
646	}
647}
648
649static const struct hwmon_ops nouveau_hwmon_ops = {
650	.is_visible = nouveau_is_visible,
651	.read = nouveau_read,
652	.read_string = nouveau_read_string,
653	.write = nouveau_write,
654};
655
656static const struct hwmon_chip_info nouveau_chip_info = {
657	.ops = &nouveau_hwmon_ops,
658	.info = nouveau_info,
659};
660#endif
661
662int
663nouveau_hwmon_init(struct drm_device *dev)
664{
665#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
666	struct nouveau_drm *drm = nouveau_drm(dev);
667	struct nvkm_iccsense *iccsense = nvxx_iccsense(drm);
668	struct nvkm_therm *therm = nvxx_therm(drm);
669	struct nvkm_volt *volt = nvxx_volt(drm);
670	const struct attribute_group *special_groups[N_ATTR_GROUPS];
671	struct nouveau_hwmon *hwmon;
672	struct device *hwmon_dev;
673	int ret = 0;
674	int i = 0;
675
676	if (!iccsense && !therm && !volt) {
677		NV_DEBUG(drm, "Skipping hwmon registration\n");
678		return 0;
679	}
680
681	hwmon = drm->hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
682	if (!hwmon)
683		return -ENOMEM;
684	hwmon->dev = dev;
685
686	if (therm && therm->attr_get && therm->attr_set) {
687		if (nvkm_therm_temp_get(therm) >= 0)
688			special_groups[i++] = &temp1_auto_point_sensor_group;
689		if (therm->fan_get && therm->fan_get(therm) >= 0)
690			special_groups[i++] = &pwm_fan_sensor_group;
691	}
692
693	special_groups[i] = NULL;
694	hwmon_dev = hwmon_device_register_with_info(dev->dev, "nouveau", dev,
695							&nouveau_chip_info,
696							special_groups);
697	if (IS_ERR(hwmon_dev)) {
698		ret = PTR_ERR(hwmon_dev);
699		NV_ERROR(drm, "Unable to register hwmon device: %d\n", ret);
700		return ret;
701	}
702
703	hwmon->hwmon = hwmon_dev;
704	return 0;
705#else
706	return 0;
707#endif
708}
709
710void
711nouveau_hwmon_fini(struct drm_device *dev)
712{
713#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
714	struct nouveau_hwmon *hwmon = nouveau_hwmon(dev);
715
716	if (!hwmon)
717		return;
718
719	if (hwmon->hwmon)
720		hwmon_device_unregister(hwmon->hwmon);
721
722	nouveau_drm(dev)->hwmon = NULL;
723	kfree(hwmon);
724#endif
725}
v4.17
  1/*
  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#ifdef CONFIG_ACPI
 26#include <linux/acpi.h>
 27#endif
 28#include <linux/power_supply.h>
 29#include <linux/hwmon.h>
 30#include <linux/hwmon-sysfs.h>
 31
 32#include <drm/drmP.h>
 33
 34#include "nouveau_drv.h"
 35#include "nouveau_hwmon.h"
 36
 37#include <nvkm/subdev/iccsense.h>
 38#include <nvkm/subdev/volt.h>
 39
 40#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
 41
 42static ssize_t
 43nouveau_hwmon_show_temp1_auto_point1_pwm(struct device *d,
 44					 struct device_attribute *a, char *buf)
 45{
 46	return snprintf(buf, PAGE_SIZE, "%d\n", 100);
 47}
 48static SENSOR_DEVICE_ATTR(temp1_auto_point1_pwm, 0444,
 49			  nouveau_hwmon_show_temp1_auto_point1_pwm, NULL, 0);
 50
 51static ssize_t
 52nouveau_hwmon_temp1_auto_point1_temp(struct device *d,
 53				     struct device_attribute *a, char *buf)
 54{
 55	struct drm_device *dev = dev_get_drvdata(d);
 56	struct nouveau_drm *drm = nouveau_drm(dev);
 57	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
 58
 59	return snprintf(buf, PAGE_SIZE, "%d\n",
 60	      therm->attr_get(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST) * 1000);
 61}
 62static ssize_t
 63nouveau_hwmon_set_temp1_auto_point1_temp(struct device *d,
 64					 struct device_attribute *a,
 65					 const char *buf, size_t count)
 66{
 67	struct drm_device *dev = dev_get_drvdata(d);
 68	struct nouveau_drm *drm = nouveau_drm(dev);
 69	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
 70	long value;
 71
 72	if (kstrtol(buf, 10, &value) == -EINVAL)
 73		return count;
 74
 75	therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST,
 76			value / 1000);
 77
 78	return count;
 79}
 80static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp, 0644,
 81			  nouveau_hwmon_temp1_auto_point1_temp,
 82			  nouveau_hwmon_set_temp1_auto_point1_temp, 0);
 83
 84static ssize_t
 85nouveau_hwmon_temp1_auto_point1_temp_hyst(struct device *d,
 86					  struct device_attribute *a, char *buf)
 87{
 88	struct drm_device *dev = dev_get_drvdata(d);
 89	struct nouveau_drm *drm = nouveau_drm(dev);
 90	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
 91
 92	return snprintf(buf, PAGE_SIZE, "%d\n",
 93	 therm->attr_get(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST) * 1000);
 94}
 95static ssize_t
 96nouveau_hwmon_set_temp1_auto_point1_temp_hyst(struct device *d,
 97					      struct device_attribute *a,
 98					      const char *buf, size_t count)
 99{
100	struct drm_device *dev = dev_get_drvdata(d);
101	struct nouveau_drm *drm = nouveau_drm(dev);
102	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
103	long value;
104
105	if (kstrtol(buf, 10, &value) == -EINVAL)
106		return count;
107
108	therm->attr_set(therm, NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST,
109			value / 1000);
110
111	return count;
112}
113static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp_hyst, 0644,
114			  nouveau_hwmon_temp1_auto_point1_temp_hyst,
115			  nouveau_hwmon_set_temp1_auto_point1_temp_hyst, 0);
116
117static ssize_t
118nouveau_hwmon_get_pwm1_max(struct device *d,
119			   struct device_attribute *a, char *buf)
120{
121	struct drm_device *dev = dev_get_drvdata(d);
122	struct nouveau_drm *drm = nouveau_drm(dev);
123	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
124	int ret;
125
126	ret = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MAX_DUTY);
127	if (ret < 0)
128		return ret;
129
130	return sprintf(buf, "%i\n", ret);
131}
132
133static ssize_t
134nouveau_hwmon_get_pwm1_min(struct device *d,
135			   struct device_attribute *a, char *buf)
136{
137	struct drm_device *dev = dev_get_drvdata(d);
138	struct nouveau_drm *drm = nouveau_drm(dev);
139	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
140	int ret;
141
142	ret = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MIN_DUTY);
143	if (ret < 0)
144		return ret;
145
146	return sprintf(buf, "%i\n", ret);
147}
148
149static ssize_t
150nouveau_hwmon_set_pwm1_min(struct device *d, struct device_attribute *a,
151			   const char *buf, size_t count)
152{
153	struct drm_device *dev = dev_get_drvdata(d);
154	struct nouveau_drm *drm = nouveau_drm(dev);
155	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
156	long value;
157	int ret;
158
159	if (kstrtol(buf, 10, &value) == -EINVAL)
160		return -EINVAL;
161
162	ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MIN_DUTY, value);
163	if (ret < 0)
164		return ret;
165
166	return count;
167}
168static SENSOR_DEVICE_ATTR(pwm1_min, 0644,
169			  nouveau_hwmon_get_pwm1_min,
170			  nouveau_hwmon_set_pwm1_min, 0);
171
172static ssize_t
173nouveau_hwmon_set_pwm1_max(struct device *d, struct device_attribute *a,
174			   const char *buf, size_t count)
175{
176	struct drm_device *dev = dev_get_drvdata(d);
177	struct nouveau_drm *drm = nouveau_drm(dev);
178	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
179	long value;
180	int ret;
181
182	if (kstrtol(buf, 10, &value) == -EINVAL)
183		return -EINVAL;
184
185	ret = therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MAX_DUTY, value);
186	if (ret < 0)
187		return ret;
188
189	return count;
190}
191static SENSOR_DEVICE_ATTR(pwm1_max, 0644,
192			  nouveau_hwmon_get_pwm1_max,
193			  nouveau_hwmon_set_pwm1_max, 0);
194
195static struct attribute *pwm_fan_sensor_attrs[] = {
196	&sensor_dev_attr_pwm1_min.dev_attr.attr,
197	&sensor_dev_attr_pwm1_max.dev_attr.attr,
198	NULL
199};
200static const struct attribute_group pwm_fan_sensor_group = {
201	.attrs = pwm_fan_sensor_attrs,
202};
203
204static struct attribute *temp1_auto_point_sensor_attrs[] = {
205	&sensor_dev_attr_temp1_auto_point1_pwm.dev_attr.attr,
206	&sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
207	&sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr.attr,
208	NULL
209};
210static const struct attribute_group temp1_auto_point_sensor_group = {
211	.attrs = temp1_auto_point_sensor_attrs,
212};
213
214#define N_ATTR_GROUPS   3
215
216static const u32 nouveau_config_chip[] = {
217	HWMON_C_UPDATE_INTERVAL,
218	0
219};
220
221static const u32 nouveau_config_in[] = {
222	HWMON_I_INPUT | HWMON_I_MIN | HWMON_I_MAX | HWMON_I_LABEL,
223	0
224};
225
226static const u32 nouveau_config_temp[] = {
227	HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST |
228	HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_EMERGENCY |
229	HWMON_T_EMERGENCY_HYST,
230	0
231};
232
233static const u32 nouveau_config_fan[] = {
234	HWMON_F_INPUT,
235	0
236};
237
238static const u32 nouveau_config_pwm[] = {
239	HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
240	0
241};
242
243static const u32 nouveau_config_power[] = {
244	HWMON_P_INPUT | HWMON_P_CAP_MAX | HWMON_P_CRIT,
245	0
246};
247
248static const struct hwmon_channel_info nouveau_chip = {
249	.type = hwmon_chip,
250	.config = nouveau_config_chip,
251};
252
253static const struct hwmon_channel_info nouveau_temp = {
254	.type = hwmon_temp,
255	.config = nouveau_config_temp,
256};
257
258static const struct hwmon_channel_info nouveau_fan = {
259	.type = hwmon_fan,
260	.config = nouveau_config_fan,
261};
262
263static const struct hwmon_channel_info nouveau_in = {
264	.type = hwmon_in,
265	.config = nouveau_config_in,
266};
267
268static const struct hwmon_channel_info nouveau_pwm = {
269	.type = hwmon_pwm,
270	.config = nouveau_config_pwm,
271};
272
273static const struct hwmon_channel_info nouveau_power = {
274	.type = hwmon_power,
275	.config = nouveau_config_power,
276};
277
278static const struct hwmon_channel_info *nouveau_info[] = {
279	&nouveau_chip,
280	&nouveau_temp,
281	&nouveau_fan,
282	&nouveau_in,
283	&nouveau_pwm,
284	&nouveau_power,
285	NULL
286};
287
288static umode_t
289nouveau_chip_is_visible(const void *data, u32 attr, int channel)
290{
291	switch (attr) {
292	case hwmon_chip_update_interval:
293		return 0444;
294	default:
295		return 0;
296	}
297}
298
299static umode_t
300nouveau_power_is_visible(const void *data, u32 attr, int channel)
301{
302	struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
303	struct nvkm_iccsense *iccsense = nvxx_iccsense(&drm->client.device);
304
305	if (!iccsense || !iccsense->data_valid || list_empty(&iccsense->rails))
306		return 0;
307
308	switch (attr) {
309	case hwmon_power_input:
310		return 0444;
311	case hwmon_power_max:
312		if (iccsense->power_w_max)
313			return 0444;
314		return 0;
315	case hwmon_power_crit:
316		if (iccsense->power_w_crit)
317			return 0444;
318		return 0;
319	default:
320		return 0;
321	}
322}
323
324static umode_t
325nouveau_temp_is_visible(const void *data, u32 attr, int channel)
326{
327	struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
328	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
329
330	if (therm && therm->attr_get && nvkm_therm_temp_get(therm) < 0)
331		return 0;
332
333	switch (attr) {
334	case hwmon_temp_input:
335		return 0444;
336	case hwmon_temp_max:
337	case hwmon_temp_max_hyst:
338	case hwmon_temp_crit:
339	case hwmon_temp_crit_hyst:
340	case hwmon_temp_emergency:
341	case hwmon_temp_emergency_hyst:
342		return 0644;
343	default:
344		return 0;
345	}
346}
347
348static umode_t
349nouveau_pwm_is_visible(const void *data, u32 attr, int channel)
350{
351	struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
352	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
353
354	if (therm && therm->attr_get && therm->fan_get &&
355				therm->fan_get(therm) < 0)
356		return 0;
357
358	switch (attr) {
359	case hwmon_pwm_enable:
360	case hwmon_pwm_input:
361		return 0644;
362	default:
363		return 0;
364	}
365}
366
367static umode_t
368nouveau_input_is_visible(const void *data, u32 attr, int channel)
369{
370	struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
371	struct nvkm_volt *volt = nvxx_volt(&drm->client.device);
372
373	if (!volt || nvkm_volt_get(volt) < 0)
374		return 0;
375
376	switch (attr) {
377	case hwmon_in_input:
378	case hwmon_in_label:
379	case hwmon_in_min:
380	case hwmon_in_max:
381		return 0444;
382	default:
383		return 0;
384	}
385}
386
387static umode_t
388nouveau_fan_is_visible(const void *data, u32 attr, int channel)
389{
390	struct nouveau_drm *drm = nouveau_drm((struct drm_device *)data);
391	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
392
393	if (!therm || !therm->attr_get || nvkm_therm_fan_sense(therm) < 0)
394		return 0;
395
396	switch (attr) {
397	case hwmon_fan_input:
398		return 0444;
399	default:
400		return 0;
401	}
402}
403
404static int
405nouveau_chip_read(struct device *dev, u32 attr, int channel, long *val)
406{
407	switch (attr) {
408	case hwmon_chip_update_interval:
409		*val = 1000;
410		break;
411	default:
412		return -EOPNOTSUPP;
413	}
414
415	return 0;
416}
417
418static int
419nouveau_temp_read(struct device *dev, u32 attr, int channel, long *val)
420{
421	struct drm_device *drm_dev = dev_get_drvdata(dev);
422	struct nouveau_drm *drm = nouveau_drm(drm_dev);
423	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
424	int ret;
425
426	if (!therm || !therm->attr_get)
427		return -EOPNOTSUPP;
428
429	switch (attr) {
430	case hwmon_temp_input:
 
 
431		ret = nvkm_therm_temp_get(therm);
432		*val = ret < 0 ? ret : (ret * 1000);
433		break;
434	case hwmon_temp_max:
435		*val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK)
436					* 1000;
437		break;
438	case hwmon_temp_max_hyst:
439		*val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST)
440					* 1000;
441		break;
442	case hwmon_temp_crit:
443		*val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_CRITICAL)
444					* 1000;
445		break;
446	case hwmon_temp_crit_hyst:
447		*val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_CRITICAL_HYST)
448					* 1000;
449		break;
450	case hwmon_temp_emergency:
451		*val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN)
452					* 1000;
453		break;
454	case hwmon_temp_emergency_hyst:
455		*val = therm->attr_get(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST)
456					* 1000;
457		break;
458	default:
459		return -EOPNOTSUPP;
460	}
461
462	return 0;
463}
464
465static int
466nouveau_fan_read(struct device *dev, u32 attr, int channel, long *val)
467{
468	struct drm_device *drm_dev = dev_get_drvdata(dev);
469	struct nouveau_drm *drm = nouveau_drm(drm_dev);
470	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
471
472	if (!therm)
473		return -EOPNOTSUPP;
474
475	switch (attr) {
476	case hwmon_fan_input:
 
 
477		*val = nvkm_therm_fan_sense(therm);
478		break;
479	default:
480		return -EOPNOTSUPP;
481	}
482
483	return 0;
484}
485
486static int
487nouveau_in_read(struct device *dev, u32 attr, int channel, long *val)
488{
489	struct drm_device *drm_dev = dev_get_drvdata(dev);
490	struct nouveau_drm *drm = nouveau_drm(drm_dev);
491	struct nvkm_volt *volt = nvxx_volt(&drm->client.device);
492	int ret;
493
494	if (!volt)
495		return -EOPNOTSUPP;
496
497	switch (attr) {
498	case hwmon_in_input:
 
 
499		ret = nvkm_volt_get(volt);
500		*val = ret < 0 ? ret : (ret / 1000);
501		break;
502	case hwmon_in_min:
503		*val = volt->min_uv > 0 ? (volt->min_uv / 1000) : -ENODEV;
504		break;
505	case hwmon_in_max:
506		*val = volt->max_uv > 0 ? (volt->max_uv / 1000) : -ENODEV;
507		break;
508	default:
509		return -EOPNOTSUPP;
510	}
511
512	return 0;
513}
514
515static int
516nouveau_pwm_read(struct device *dev, u32 attr, int channel, long *val)
517{
518	struct drm_device *drm_dev = dev_get_drvdata(dev);
519	struct nouveau_drm *drm = nouveau_drm(drm_dev);
520	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
521
522	if (!therm || !therm->attr_get || !therm->fan_get)
523		return -EOPNOTSUPP;
524
525	switch (attr) {
526	case hwmon_pwm_enable:
527		*val = therm->attr_get(therm, NVKM_THERM_ATTR_FAN_MODE);
528		break;
529	case hwmon_pwm_input:
 
 
530		*val = therm->fan_get(therm);
531		break;
532	default:
533		return -EOPNOTSUPP;
534	}
535
536	return 0;
537}
538
539static int
540nouveau_power_read(struct device *dev, u32 attr, int channel, long *val)
541{
542	struct drm_device *drm_dev = dev_get_drvdata(dev);
543	struct nouveau_drm *drm = nouveau_drm(drm_dev);
544	struct nvkm_iccsense *iccsense = nvxx_iccsense(&drm->client.device);
545
546	if (!iccsense)
547		return -EOPNOTSUPP;
548
549	switch (attr) {
550	case hwmon_power_input:
 
 
551		*val = nvkm_iccsense_read_all(iccsense);
552		break;
553	case hwmon_power_max:
554		*val = iccsense->power_w_max;
555		break;
556	case hwmon_power_crit:
557		*val = iccsense->power_w_crit;
558		break;
559	default:
560		return -EOPNOTSUPP;
561	}
562
563	return 0;
564}
565
566static int
567nouveau_temp_write(struct device *dev, u32 attr, int channel, long val)
568{
569	struct drm_device *drm_dev = dev_get_drvdata(dev);
570	struct nouveau_drm *drm = nouveau_drm(drm_dev);
571	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
572
573	if (!therm || !therm->attr_set)
574		return -EOPNOTSUPP;
575
576	switch (attr) {
577	case hwmon_temp_max:
578		return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK,
579					val / 1000);
580	case hwmon_temp_max_hyst:
581		return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST,
582					val / 1000);
583	case hwmon_temp_crit:
584		return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_CRITICAL,
585					val / 1000);
586	case hwmon_temp_crit_hyst:
587		return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_CRITICAL_HYST,
588					val / 1000);
589	case hwmon_temp_emergency:
590		return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN,
591					val / 1000);
592	case hwmon_temp_emergency_hyst:
593		return therm->attr_set(therm, NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST,
594					val / 1000);
595	default:
596		return -EOPNOTSUPP;
597	}
598}
599
600static int
601nouveau_pwm_write(struct device *dev, u32 attr, int channel, long val)
602{
603	struct drm_device *drm_dev = dev_get_drvdata(dev);
604	struct nouveau_drm *drm = nouveau_drm(drm_dev);
605	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
606
607	if (!therm || !therm->attr_set)
608		return -EOPNOTSUPP;
609
610	switch (attr) {
611	case hwmon_pwm_input:
612		return therm->fan_set(therm, val);
613	case hwmon_pwm_enable:
614		return therm->attr_set(therm, NVKM_THERM_ATTR_FAN_MODE, val);
615	default:
616		return -EOPNOTSUPP;
617	}
618}
619
620static umode_t
621nouveau_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr,
622			int channel)
623{
624	switch (type) {
625	case hwmon_chip:
626		return nouveau_chip_is_visible(data, attr, channel);
627	case hwmon_temp:
628		return nouveau_temp_is_visible(data, attr, channel);
629	case hwmon_fan:
630		return nouveau_fan_is_visible(data, attr, channel);
631	case hwmon_in:
632		return nouveau_input_is_visible(data, attr, channel);
633	case hwmon_pwm:
634		return nouveau_pwm_is_visible(data, attr, channel);
635	case hwmon_power:
636		return nouveau_power_is_visible(data, attr, channel);
637	default:
638		return 0;
639	}
640}
641
642static const char input_label[] = "GPU core";
643
644static int
645nouveau_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr,
646		    int channel, const char **buf)
647{
648	if (type == hwmon_in && attr == hwmon_in_label) {
649		*buf = input_label;
650		return 0;
651	}
652
653	return -EOPNOTSUPP;
654}
655
656static int
657nouveau_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
658							int channel, long *val)
659{
660	switch (type) {
661	case hwmon_chip:
662		return nouveau_chip_read(dev, attr, channel, val);
663	case hwmon_temp:
664		return nouveau_temp_read(dev, attr, channel, val);
665	case hwmon_fan:
666		return nouveau_fan_read(dev, attr, channel, val);
667	case hwmon_in:
668		return nouveau_in_read(dev, attr, channel, val);
669	case hwmon_pwm:
670		return nouveau_pwm_read(dev, attr, channel, val);
671	case hwmon_power:
672		return nouveau_power_read(dev, attr, channel, val);
673	default:
674		return -EOPNOTSUPP;
675	}
676}
677
678static int
679nouveau_write(struct device *dev, enum hwmon_sensor_types type, u32 attr,
680							int channel, long val)
681{
682	switch (type) {
683	case hwmon_temp:
684		return nouveau_temp_write(dev, attr, channel, val);
685	case hwmon_pwm:
686		return nouveau_pwm_write(dev, attr, channel, val);
687	default:
688		return -EOPNOTSUPP;
689	}
690}
691
692static const struct hwmon_ops nouveau_hwmon_ops = {
693	.is_visible = nouveau_is_visible,
694	.read = nouveau_read,
695	.read_string = nouveau_read_string,
696	.write = nouveau_write,
697};
698
699static const struct hwmon_chip_info nouveau_chip_info = {
700	.ops = &nouveau_hwmon_ops,
701	.info = nouveau_info,
702};
703#endif
704
705int
706nouveau_hwmon_init(struct drm_device *dev)
707{
708#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
709	struct nouveau_drm *drm = nouveau_drm(dev);
710	struct nvkm_therm *therm = nvxx_therm(&drm->client.device);
 
 
711	const struct attribute_group *special_groups[N_ATTR_GROUPS];
712	struct nouveau_hwmon *hwmon;
713	struct device *hwmon_dev;
714	int ret = 0;
715	int i = 0;
716
 
 
 
 
 
717	hwmon = drm->hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
718	if (!hwmon)
719		return -ENOMEM;
720	hwmon->dev = dev;
721
722	if (therm && therm->attr_get && therm->attr_set) {
723		if (nvkm_therm_temp_get(therm) >= 0)
724			special_groups[i++] = &temp1_auto_point_sensor_group;
725		if (therm->fan_get && therm->fan_get(therm) >= 0)
726			special_groups[i++] = &pwm_fan_sensor_group;
727	}
728
729	special_groups[i] = 0;
730	hwmon_dev = hwmon_device_register_with_info(dev->dev, "nouveau", dev,
731							&nouveau_chip_info,
732							special_groups);
733	if (IS_ERR(hwmon_dev)) {
734		ret = PTR_ERR(hwmon_dev);
735		NV_ERROR(drm, "Unable to register hwmon device: %d\n", ret);
736		return ret;
737	}
738
739	hwmon->hwmon = hwmon_dev;
740	return 0;
741#else
742	return 0;
743#endif
744}
745
746void
747nouveau_hwmon_fini(struct drm_device *dev)
748{
749#if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
750	struct nouveau_hwmon *hwmon = nouveau_hwmon(dev);
 
 
 
751
752	if (hwmon->hwmon)
753		hwmon_device_unregister(hwmon->hwmon);
754
755	nouveau_drm(dev)->hwmon = NULL;
756	kfree(hwmon);
757#endif
758}