Merge commit '28dbfe9bf7a799ab1da2563fd5e007d007b54168'
[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        = 1,
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                 /* Enable pwl */
47                 writeb(0x01, PWL_REG(PWL_CTRL));
48                 /* Switch pin from LT to PWL */
49                 reg |= ASCONF_PWL_ENA;
50                 writew(reg, ASIC_CONF_REG);
51         } else {
52                 /* Switch pin from PWL to LT */
53                 reg |= ~ASCONF_PWL_ENA;
54                 writew(reg, ASIC_CONF_REG);
55                 /* Disable pwl */
56                 writeb(0x00, PWL_REG(PWL_CTRL));
57         }
58 }
59
60 void bl_level(uint8_t level)
61 {
62         if (readw(ASIC_CONF_REG) & ASCONF_PWL_ENA) {
63                 writeb(level, PWL_REG(PWL_LEVEL));
64         } else {
65                 /* we need to scale the light level, as the
66                  * ARMIO light controller only knows 0..63 */
67                 writeb(level>>2, LIGHT_LEVEL_REG);
68         }
69 }