Merge commit '61e2bfc5f44267a7a3b0b25ff3ab922fca2a199c'
[osmocom-bb.git] / src / target / firmware / calypso / backlight.c
1 /* Calypso DBB internal PWL (Pulse Width / Light) Driver */
2
3 /* (C) 2010 by Harald Welte <laforge@gnumonks.org>
4  *
5  * All Rights Reserved
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  */
22
23 #include <stdint.h>
24 #include <memory.h>
25
26 #define BASE_ADDR_PWL   0xfffe8000
27 #define PWL_REG(m)      (BASE_ADDR_PWL + (m))
28
29 #define ASIC_CONF_REG           0xfffef008
30 #define LIGHT_LEVEL_REG         0xfffe4810
31
32 enum pwl_reg {
33         PWL_LEVEL       = 0,
34         PWL_CTRL        = 2,
35 };
36
37 #define ASCONF_PWL_ENA  (1 << 4)
38
39 void bl_mode_pwl(int on)
40 {
41         uint16_t reg;
42
43         reg = readw(ASIC_CONF_REG);
44
45         if (on) {
46                 writeb(0x01, PWL_REG(PWL_CTRL));
47                 /* Switch pin from LT to PWL */
48                 reg |= ASCONF_PWL_ENA;
49                 writew(reg, ASIC_CONF_REG);
50         } else {
51                 /* Switch pin from PWL to LT */
52                 reg |= ~ASCONF_PWL_ENA;
53                 writew(reg, ASIC_CONF_REG);
54                 writeb(0x00, PWL_REG(PWL_CTRL));
55         }
56 }
57
58 void bl_level(uint8_t level)
59 {
60         if (readw(ASIC_CONF_REG) & ASCONF_PWL_ENA) {
61                 writeb(level, PWL_REG(PWL_LEVEL));
62         } else {
63                 /* we need to scale the light level, as the
64                  * ARMIO light controller only knows 0..63 */
65                 writeb(level>>2, LIGHT_LEVEL_REG);
66         }
67 }