HiFiMAN: Implement lcd powersave mode.
[maemo-rb.git] / firmware / target / arm / rk27xx / backlight-rk27xx.c
blob06c9cc394ddf311d96a811ce83adfdf92c08ce1d
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2011 by Marcin Bukat
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdbool.h>
23 #include "config.h"
24 #include "backlight.h"
25 #include "backlight-target.h"
26 #include "system.h"
27 #include "lcd.h"
29 static int brightness = DEFAULT_BRIGHTNESS_SETTING;
31 /* Based on http://www.poynton.com/PDFs/SMPTE93_Gamma.pdf
32 * CIE standarized function that relates physical luminance
33 * to perceived lightness
35 * L* = 116*(Y/Yn)^(1/3) - 16 (Y/Yn > 0 008856)
36 * where Yn is luminance of reference white
38 * Actual function is lightly tweaked to account for the fact
39 * that fill factor of the PWM below ~15% gives black.
40 * So the function used to calculate the values in the matrix was:
41 * f(x) = 42000 * ((100*x/31 + 16)/116)^3 + 8000
43 static const unsigned short lin_brightness[] = {
44 8110, 8191, 8304, 8455, 8649, 8892, 9189, 9545,
45 9966, 10457, 11024, 11671, 12406, 13232, 14156, 15182,
46 16316, 17565, 18932, 20423, 22045, 23801, 25699, 27742,
47 29937, 32289, 34803, 37485, 40340, 43374, 46592, 50000
50 bool _backlight_init(void)
52 /* configure PD4 as output */
53 GPIO_PDCON |= (1<<4);
55 /* set PD4 low (backlight off) */
56 GPIO_PDDR &= ~(1<<4);
58 /* IOMUXB - set PWM0 pin as GPIO */
59 SCU_IOMUXB_CON &= ~(1 << 11); /* type<<11<<channel */
61 /* DIV/2, PWM reset */
62 PWMT0_CTRL = (0<<9) | (1<<7);
64 /* set pwm frequency to 500Hz - my lcd panel can't cope more reliably */
65 /* (apb_freq/pwm_freq)/pwm_div = (50 000 000/500)/2 */
66 PWMT0_LRC = 50000;
67 PWMT0_HRC = lin_brightness[brightness];
69 /* reset counter */
70 PWMT0_CNTR = 0x00;
72 /* DIV/2, PWM output enable, PWM timer enable */
73 PWMT0_CTRL = (0<<9) | (1<<3) | (1<<0);
75 return true;
78 void _backlight_on(void)
80 #ifdef HAVE_LCD_ENABLE
81 lcd_enable(true);
82 #endif
83 /* enable PWM clock */
84 SCU_CLKCFG &= ~(1<<29);
86 /* set output pin as PWM pin */
87 SCU_IOMUXB_CON |= (1<<11); /* type<<11<<channel */
89 /* pwm enable */
90 PWMT0_CTRL |= (1<<3) | (1<<0);
93 void _backlight_off(void)
95 /* setup PWM0 pin as GPIO which is pulled low */
96 SCU_IOMUXB_CON &= ~(1<<11);
98 /* stop pwm timer */
99 PWMT0_CTRL &= ~(1<<3) | (1<<0);
101 /* disable PWM clock */
102 SCU_CLKCFG |= (1<<29);
103 #ifdef HAVE_LCD_ENABLE
104 lcd_enable(false);
105 #endif
108 void _backlight_set_brightness(int val)
110 brightness = val & 0x1f;
111 PWMT0_HRC = lin_brightness[brightness];