1/*	$NetBSD: nouveau_led.c,v 1.2 2021/12/18 23:45:32 riastradh Exp $	*/
2
3/*
4 * Copyright (C) 2016 Martin Peres
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27
28/*
29 * Authors:
30 *  Martin Peres <martin.peres@free.fr>
31 */
32
33#include <sys/cdefs.h>
34__KERNEL_RCSID(0, "$NetBSD: nouveau_led.c,v 1.2 2021/12/18 23:45:32 riastradh Exp $");
35
36#include <linux/leds.h>
37
38#include "nouveau_led.h"
39#include <nvkm/subdev/gpio.h>
40
41static enum led_brightness
42nouveau_led_get_brightness(struct led_classdev *led)
43{
44	struct drm_device *drm_dev = container_of(led, struct nouveau_led, led)->dev;
45	struct nouveau_drm *drm = nouveau_drm(drm_dev);
46	struct nvif_object *device = &drm->client.device.object;
47	u32 div, duty;
48
49	div =  nvif_rd32(device, 0x61c880) & 0x00ffffff;
50	duty = nvif_rd32(device, 0x61c884) & 0x00ffffff;
51
52	if (div > 0)
53		return duty * LED_FULL / div;
54	else
55		return 0;
56}
57
58static void
59nouveau_led_set_brightness(struct led_classdev *led, enum led_brightness value)
60{
61	struct drm_device *drm_dev = container_of(led, struct nouveau_led, led)->dev;
62	struct nouveau_drm *drm = nouveau_drm(drm_dev);
63	struct nvif_object *device = &drm->client.device.object;
64
65	u32 input_clk = 27e6; /* PDISPLAY.SOR[1].PWM is connected to the crystal */
66	u32 freq = 100; /* this is what nvidia uses and it should be good-enough */
67	u32 div, duty;
68
69	div = input_clk / freq;
70	duty = value * div / LED_FULL;
71
72	/* for now, this is safe to directly poke those registers because:
73	 *  - A: nvidia never puts the logo led to any other PWM controler
74	 *       than PDISPLAY.SOR[1].PWM.
75	 *  - B: nouveau does not touch these registers anywhere else
76	 */
77	nvif_wr32(device, 0x61c880, div);
78	nvif_wr32(device, 0x61c884, 0xc0000000 | duty);
79}
80
81
82int
83nouveau_led_init(struct drm_device *dev)
84{
85	struct nouveau_drm *drm = nouveau_drm(dev);
86	struct nvkm_gpio *gpio = nvxx_gpio(&drm->client.device);
87	struct dcb_gpio_func logo_led;
88	int ret;
89
90	if (!gpio)
91		return 0;
92
93	/* check that there is a GPIO controlling the logo LED */
94	if (nvkm_gpio_find(gpio, 0, DCB_GPIO_LOGO_LED_PWM, 0xff, &logo_led))
95		return 0;
96
97	drm->led = kzalloc(sizeof(*drm->led), GFP_KERNEL);
98	if (!drm->led)
99		return -ENOMEM;
100	drm->led->dev = dev;
101
102	drm->led->led.name = "nvidia-logo";
103	drm->led->led.max_brightness = 255;
104	drm->led->led.brightness_get = nouveau_led_get_brightness;
105	drm->led->led.brightness_set = nouveau_led_set_brightness;
106
107	ret = led_classdev_register(dev->dev, &drm->led->led);
108	if (ret) {
109		kfree(drm->led);
110		drm->led = NULL;
111		return ret;
112	}
113
114	return 0;
115}
116
117void
118nouveau_led_suspend(struct drm_device *dev)
119{
120	struct nouveau_drm *drm = nouveau_drm(dev);
121
122	if (drm->led)
123		led_classdev_suspend(&drm->led->led);
124}
125
126void
127nouveau_led_resume(struct drm_device *dev)
128{
129	struct nouveau_drm *drm = nouveau_drm(dev);
130
131	if (drm->led)
132		led_classdev_resume(&drm->led->led);
133}
134
135void
136nouveau_led_fini(struct drm_device *dev)
137{
138	struct nouveau_drm *drm = nouveau_drm(dev);
139
140	if (drm->led) {
141		led_classdev_unregister(&drm->led->led);
142		kfree(drm->led);
143		drm->led = NULL;
144	}
145}
146