Control intel backlight on FreeBSD (and OpenBSD)
openbsd
at master 122 lines 3.5 kB view raw
1/* 2 * Copyright © 2011 Intel Corporation 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 (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21 * DEALINGS IN THE SOFTWARE. 22 * 23 * Authors: 24 * Chris Wilson <chris@chris-wilson.co.uk> 25 * Maurizio Vairani <maurizio.vairani@cloverinformatica.it> 26 */ 27 28#include <stdint.h> 29#include <stdlib.h> 30#include <stdio.h> 31#include <string.h> 32 33#include "intel_gpu_tools.h" 34#define NUM_ELEMENTS(array) (sizeof(array) / sizeof(array[0])) 35 36/* XXX PCH only today */ 37 38static uint32_t reg_read(uint32_t reg) 39{ 40 return *(volatile uint32_t *)((volatile char*)mmio + reg); 41} 42 43static void reg_write(uint32_t reg, uint32_t val) 44{ 45 *(volatile uint32_t *)((volatile char*)mmio + reg) = val; 46} 47 48static int brightness_levels[] = {1, 2, 4, 6, 9, 12, 16, 20, 25, 30, 36, 43, 49 51, 60, 70, 80, 90, 100}; 50 51static int brightness_incr(int curr) 52{ 53 int i; 54 for (i = 0; i < NUM_ELEMENTS(brightness_levels) - 1; ++i) 55 if (curr < brightness_levels[i]) 56 break; 57 return brightness_levels[i]; 58} 59 60static int brightness_decr(int curr) 61{ 62 int i; 63 for (i = NUM_ELEMENTS(brightness_levels) - 1; i > 0; --i) 64 if (brightness_levels[i] < curr) 65 break; 66 return brightness_levels[i]; 67} 68 69int main(int argc, char** argv) 70{ 71 uint32_t current, max, min; 72 struct pci_device *dev; 73 int result; 74 75 dev = intel_get_pci_device(); 76 intel_get_mmio(dev); 77 78 if (IS_GEN8(dev->device_id) || IS_GEN9(dev->device_id)) /* bdw/skl */ 79 current = reg_read(BLC_PWM_PCH_CTL2) & BACKLIGHT_DUTY_CYCLE_MASK; 80 else 81 current = reg_read(BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK; 82 83 max = reg_read(BLC_PWM_PCH_CTL2) >> 16; 84 85 min = 0.5 + 0.5 * max / 100.0; // 0.5% 86 result = 0.5 + current * 100.0 / max; 87 88 printf ("%d", result); 89 90 if (argc > 1) { 91 uint32_t v; 92 if (0 == strcmp(argv[1], "incr")) 93 v = 0.5 + brightness_incr(result) * max / 100.0; 94 else if (0 == strcmp(argv[1], "decr")) 95 v = 0.5 + brightness_decr(result) * max / 100.0; 96 else 97 v = 0.5 + atoi (argv[1]) * max / 100.0; 98 99 if (v < min) 100 v = min; 101 else if (v > max) 102 v = max; 103 104 if (IS_GEN8(dev->device_id) || IS_GEN9(dev->device_id)) { 105 reg_write(BLC_PWM_PCH_CTL2, 106 (reg_read(BLC_PWM_PCH_CTL2) &~ 107 BACKLIGHT_DUTY_CYCLE_MASK) | v); 108 (void) reg_read(BLC_PWM_PCH_CTL2); 109 } else { 110 reg_write(BLC_PWM_CPU_CTL, 111 (reg_read(BLC_PWM_CPU_CTL) &~ 112 BACKLIGHT_DUTY_CYCLE_MASK) | v); 113 (void) reg_read(BLC_PWM_CPU_CTL); 114 } 115 result = 0.5 + v * 100.0 / max; 116 printf (" -> %d", result); 117 } 118 119 printf("\n"); 120 121 return (0); 122}