162306a36Sopenharmony_ci/* 262306a36Sopenharmony_ci * Copyright (C) 2016 Martin Peres 362306a36Sopenharmony_ci * 462306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining 562306a36Sopenharmony_ci * a copy of this software and associated documentation files (the 662306a36Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 762306a36Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 862306a36Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to 962306a36Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 1062306a36Sopenharmony_ci * the following conditions: 1162306a36Sopenharmony_ci * 1262306a36Sopenharmony_ci * The above copyright notice and this permission notice (including the 1362306a36Sopenharmony_ci * next paragraph) shall be included in all copies or substantial 1462306a36Sopenharmony_ci * portions of the Software. 1562306a36Sopenharmony_ci * 1662306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 1762306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 1862306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 1962306a36Sopenharmony_ci * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE 2062306a36Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 2162306a36Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 2262306a36Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2362306a36Sopenharmony_ci * 2462306a36Sopenharmony_ci */ 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci/* 2762306a36Sopenharmony_ci * Authors: 2862306a36Sopenharmony_ci * Martin Peres <martin.peres@free.fr> 2962306a36Sopenharmony_ci */ 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci#include <linux/leds.h> 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci#include "nouveau_led.h" 3462306a36Sopenharmony_ci#include <nvkm/subdev/gpio.h> 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_cistatic enum led_brightness 3762306a36Sopenharmony_cinouveau_led_get_brightness(struct led_classdev *led) 3862306a36Sopenharmony_ci{ 3962306a36Sopenharmony_ci struct drm_device *drm_dev = container_of(led, struct nouveau_led, led)->dev; 4062306a36Sopenharmony_ci struct nouveau_drm *drm = nouveau_drm(drm_dev); 4162306a36Sopenharmony_ci struct nvif_object *device = &drm->client.device.object; 4262306a36Sopenharmony_ci u32 div, duty; 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci div = nvif_rd32(device, 0x61c880) & 0x00ffffff; 4562306a36Sopenharmony_ci duty = nvif_rd32(device, 0x61c884) & 0x00ffffff; 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci if (div > 0) 4862306a36Sopenharmony_ci return duty * LED_FULL / div; 4962306a36Sopenharmony_ci else 5062306a36Sopenharmony_ci return 0; 5162306a36Sopenharmony_ci} 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_cistatic void 5462306a36Sopenharmony_cinouveau_led_set_brightness(struct led_classdev *led, enum led_brightness value) 5562306a36Sopenharmony_ci{ 5662306a36Sopenharmony_ci struct drm_device *drm_dev = container_of(led, struct nouveau_led, led)->dev; 5762306a36Sopenharmony_ci struct nouveau_drm *drm = nouveau_drm(drm_dev); 5862306a36Sopenharmony_ci struct nvif_object *device = &drm->client.device.object; 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ci u32 input_clk = 27e6; /* PDISPLAY.SOR[1].PWM is connected to the crystal */ 6162306a36Sopenharmony_ci u32 freq = 100; /* this is what nvidia uses and it should be good-enough */ 6262306a36Sopenharmony_ci u32 div, duty; 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci div = input_clk / freq; 6562306a36Sopenharmony_ci duty = value * div / LED_FULL; 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci /* for now, this is safe to directly poke those registers because: 6862306a36Sopenharmony_ci * - A: nvidia never puts the logo led to any other PWM controler 6962306a36Sopenharmony_ci * than PDISPLAY.SOR[1].PWM. 7062306a36Sopenharmony_ci * - B: nouveau does not touch these registers anywhere else 7162306a36Sopenharmony_ci */ 7262306a36Sopenharmony_ci nvif_wr32(device, 0x61c880, div); 7362306a36Sopenharmony_ci nvif_wr32(device, 0x61c884, 0xc0000000 | duty); 7462306a36Sopenharmony_ci} 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ciint 7862306a36Sopenharmony_cinouveau_led_init(struct drm_device *dev) 7962306a36Sopenharmony_ci{ 8062306a36Sopenharmony_ci struct nouveau_drm *drm = nouveau_drm(dev); 8162306a36Sopenharmony_ci struct nvkm_gpio *gpio = nvxx_gpio(&drm->client.device); 8262306a36Sopenharmony_ci struct dcb_gpio_func logo_led; 8362306a36Sopenharmony_ci int ret; 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ci if (!gpio) 8662306a36Sopenharmony_ci return 0; 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_ci /* check that there is a GPIO controlling the logo LED */ 8962306a36Sopenharmony_ci if (nvkm_gpio_find(gpio, 0, DCB_GPIO_LOGO_LED_PWM, 0xff, &logo_led)) 9062306a36Sopenharmony_ci return 0; 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ci drm->led = kzalloc(sizeof(*drm->led), GFP_KERNEL); 9362306a36Sopenharmony_ci if (!drm->led) 9462306a36Sopenharmony_ci return -ENOMEM; 9562306a36Sopenharmony_ci drm->led->dev = dev; 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ci drm->led->led.name = "nvidia-logo"; 9862306a36Sopenharmony_ci drm->led->led.max_brightness = 255; 9962306a36Sopenharmony_ci drm->led->led.brightness_get = nouveau_led_get_brightness; 10062306a36Sopenharmony_ci drm->led->led.brightness_set = nouveau_led_set_brightness; 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci ret = led_classdev_register(dev->dev, &drm->led->led); 10362306a36Sopenharmony_ci if (ret) { 10462306a36Sopenharmony_ci kfree(drm->led); 10562306a36Sopenharmony_ci drm->led = NULL; 10662306a36Sopenharmony_ci return ret; 10762306a36Sopenharmony_ci } 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_ci return 0; 11062306a36Sopenharmony_ci} 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_civoid 11362306a36Sopenharmony_cinouveau_led_suspend(struct drm_device *dev) 11462306a36Sopenharmony_ci{ 11562306a36Sopenharmony_ci struct nouveau_drm *drm = nouveau_drm(dev); 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci if (drm->led) 11862306a36Sopenharmony_ci led_classdev_suspend(&drm->led->led); 11962306a36Sopenharmony_ci} 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_civoid 12262306a36Sopenharmony_cinouveau_led_resume(struct drm_device *dev) 12362306a36Sopenharmony_ci{ 12462306a36Sopenharmony_ci struct nouveau_drm *drm = nouveau_drm(dev); 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ci if (drm->led) 12762306a36Sopenharmony_ci led_classdev_resume(&drm->led->led); 12862306a36Sopenharmony_ci} 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_civoid 13162306a36Sopenharmony_cinouveau_led_fini(struct drm_device *dev) 13262306a36Sopenharmony_ci{ 13362306a36Sopenharmony_ci struct nouveau_drm *drm = nouveau_drm(dev); 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ci if (drm->led) { 13662306a36Sopenharmony_ci led_classdev_unregister(&drm->led->led); 13762306a36Sopenharmony_ci kfree(drm->led); 13862306a36Sopenharmony_ci drm->led = NULL; 13962306a36Sopenharmony_ci } 14062306a36Sopenharmony_ci} 141