1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include <linux/leds.h>
32
33#include "nouveau_led.h"
34#include <nvkm/subdev/gpio.h>
35
36static enum led_brightness
37nouveau_led_get_brightness(struct led_classdev *led)
38{
39 struct drm_device *drm_dev = container_of(led, struct nouveau_led, led)->dev;
40 struct nouveau_drm *drm = nouveau_drm(drm_dev);
41 struct nvif_object *device = &drm->device.object;
42 u32 div, duty;
43
44 div = nvif_rd32(device, 0x61c880) & 0x00ffffff;
45 duty = nvif_rd32(device, 0x61c884) & 0x00ffffff;
46
47 if (div > 0)
48 return duty * LED_FULL / div;
49 else
50 return 0;
51}
52
53static void
54nouveau_led_set_brightness(struct led_classdev *led, enum led_brightness value)
55{
56 struct drm_device *drm_dev = container_of(led, struct nouveau_led, led)->dev;
57 struct nouveau_drm *drm = nouveau_drm(drm_dev);
58 struct nvif_object *device = &drm->device.object;
59
60 u32 input_clk = 27e6;
61 u32 freq = 100;
62 u32 div, duty;
63
64 div = input_clk / freq;
65 duty = value * div / LED_FULL;
66
67
68
69
70
71
72 nvif_wr32(device, 0x61c880, div);
73 nvif_wr32(device, 0x61c884, 0xc0000000 | duty);
74}
75
76
77int
78nouveau_led_init(struct drm_device *dev)
79{
80 struct nouveau_drm *drm = nouveau_drm(dev);
81 struct nvkm_gpio *gpio = nvxx_gpio(&drm->device);
82 struct dcb_gpio_func logo_led;
83 int ret;
84
85 if (!gpio)
86 return 0;
87
88
89 if (nvkm_gpio_find(gpio, 0, DCB_GPIO_LOGO_LED_PWM, 0xff, &logo_led))
90 return 0;
91
92 drm->led = kzalloc(sizeof(*drm->led), GFP_KERNEL);
93 if (!drm->led)
94 return -ENOMEM;
95 drm->led->dev = dev;
96
97 drm->led->led.name = "nvidia-logo";
98 drm->led->led.max_brightness = 255;
99 drm->led->led.brightness_get = nouveau_led_get_brightness;
100 drm->led->led.brightness_set = nouveau_led_set_brightness;
101
102 ret = led_classdev_register(dev->dev, &drm->led->led);
103 if (ret) {
104 kfree(drm->led);
105 return ret;
106 }
107
108 return 0;
109}
110
111void
112nouveau_led_suspend(struct drm_device *dev)
113{
114 struct nouveau_drm *drm = nouveau_drm(dev);
115
116 if (drm->led)
117 led_classdev_suspend(&drm->led->led);
118}
119
120void
121nouveau_led_resume(struct drm_device *dev)
122{
123 struct nouveau_drm *drm = nouveau_drm(dev);
124
125 if (drm->led)
126 led_classdev_resume(&drm->led->led);
127}
128
129void
130nouveau_led_fini(struct drm_device *dev)
131{
132 struct nouveau_drm *drm = nouveau_drm(dev);
133
134 if (drm->led) {
135 led_classdev_unregister(&drm->led->led);
136 kfree(drm->led);
137 drm->led = NULL;
138 }
139}
140