Tizen 2.1 base
[sdk/emulator/qemu.git] / tizen / src / hw / maru_brightness.c
1 /*
2  * Maru brightness device for VGA
3  *
4  * Copyright (C) 2011 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  * SeokYeon Hwang <syeon.hwang@samsung.com>
8  * SangJin Kim <sangjin3.kim@samsung.com>
9  * MunKyu Im <munkyu.im@samsung.com>
10  * KiTae Kim <kt920.kim@samsung.com>
11  * JinHyung Jo <jinhyung.jo@samsung.com>
12  * SungMin Ha <sungmin82.ha@samsung.com>
13  * JiHye Kim <jihye1128.kim@samsung.com>
14  * GiWoong Kim <giwoong.kim@samsung.com>
15  * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
16  * DongKyun Yun
17  * DoHyung Hong
18  * Hyunjun Son
19  *
20  * This program is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU General Public License
22  * as published by the Free Software Foundation; either version 2
23  * of the License, or (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program; if not, write to the Free Software
32  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
33  *
34  * Contributors:
35  * - S-Core Co., Ltd
36  *
37  */
38
39
40 #include "pc.h"
41 #ifdef TARGET_ARM
42 #include "console.h"
43 #endif
44 #include "pci.h"
45 #include "maru_device_ids.h"
46 #include "maru_brightness.h"
47 #include "debug_ch.h"
48
49 MULTI_DEBUG_CHANNEL(qemu, maru_brightness);
50
51 #define QEMU_DEV_NAME           "MARU_BRIGHTNESS"
52
53 #define BRIGHTNESS_MEM_SIZE    (4 * 1024)    /* 4KB */
54 #define BRIGHTNESS_REG_SIZE    256
55
56 typedef struct BrightnessState {
57     PCIDevice       dev;
58     ram_addr_t      vram_offset;
59     MemoryRegion    mmio_addr;
60 } BrightnessState;
61
62 enum {
63     BRIGHTNESS_LEVEL = 0x00,
64     BRIGHTNESS_OFF = 0x04,
65 };
66
67 uint32_t brightness_level = BRIGHTNESS_MAX;
68 uint32_t brightness_off;
69
70 /* level : 1 ~ 100, interval : 1 or 2 */
71 /* skip 100 level, set to default alpha */
72 uint8_t brightness_tbl[] = {100, /* level 0 : for dimming */
73 /* level 01 ~ 10 */         106, 108, 109, 111, 112, 114, 115, 117, 118, 120,
74 /* level 11 ~ 20 */         121, 123, 124, 126, 127, 129, 130, 132, 133, 135,
75 /* level 21 ~ 30 */         136, 138, 139, 141, 142, 144, 145, 147, 148, 150,
76 /* level 31 ~ 40 */         151, 153, 154, 156, 157, 159, 160, 162, 163, 165,
77 /* level 41 ~ 50 */         166, 168, 169, 171, 172, 174, 175, 177, 178, 180,
78 /* level 51 ~ 60 */         181, 183, 184, 186, 187, 189, 190, 192, 193, 195,
79 /* level 61 ~ 70 */         196, 198, 199, 201, 202, 204, 205, 207, 208, 210,
80 /* level 71 ~ 80 */         211, 213, 214, 216, 217, 219, 220, 222, 223, 225,
81 /* level 81 ~ 90 */         226, 228, 229, 231, 232, 234, 235, 237, 238, 240,
82 /* level 91 ~ 99 */         241, 243, 244, 246, 247, 249, 250, 252, 253};
83
84 static uint64_t brightness_reg_read(void *opaque,
85                                     target_phys_addr_t addr,
86                                     unsigned size)
87 {
88     switch (addr & 0xFF) {
89     case BRIGHTNESS_LEVEL:
90         INFO("brightness_reg_read: brightness_level = %d\n", brightness_level);
91         return brightness_level;
92     case BRIGHTNESS_OFF:
93         INFO("brightness_reg_read: brightness_off = %d\n", brightness_off);
94         return brightness_off;
95     default:
96         ERR("wrong brightness register read - addr : %d\n", (int)addr);
97         break;
98     }
99
100     return 0;
101 }
102
103 static void brightness_reg_write(void *opaque,
104                                  target_phys_addr_t addr,
105                                  uint64_t val,
106                                  unsigned size)
107 {
108     switch (addr & 0xFF) {
109     case BRIGHTNESS_LEVEL:
110 #if BRIGHTNESS_MIN > 0
111         if (val < BRIGHTNESS_MIN || val > BRIGHTNESS_MAX) {
112 #else
113         if (val > BRIGHTNESS_MAX) {
114 #endif
115             ERR("brightness_reg_write: Invalide brightness level.\n");
116         } else {
117             brightness_level = val;
118             INFO("brightness_level : %lld\n", val);
119 #ifdef TARGET_ARM
120             vga_hw_invalidate();
121 #endif
122         }
123         return;
124     case BRIGHTNESS_OFF:
125         INFO("brightness_off : %lld\n", val);
126         brightness_off = val;
127 #ifdef TARGET_ARM
128         vga_hw_invalidate();
129 #endif
130         return;
131     default:
132         ERR("wrong brightness register write - addr : %d\n", (int)addr);
133         break;
134     }
135 }
136
137 static const MemoryRegionOps brightness_mmio_ops = {
138     .read = brightness_reg_read,
139     .write = brightness_reg_write,
140     .endianness = DEVICE_LITTLE_ENDIAN,
141 };
142
143 static int brightness_initfn(PCIDevice *dev)
144 {
145     BrightnessState *s = DO_UPCAST(BrightnessState, dev, dev);
146     uint8_t *pci_conf = s->dev.config;
147
148     pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_TIZEN);
149     pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_VIRTUAL_BRIGHTNESS);
150     pci_config_set_class(pci_conf, PCI_CLASS_DISPLAY_OTHER);
151
152     memory_region_init_io(&s->mmio_addr, &brightness_mmio_ops, s,
153                             "maru_brightness_mmio", BRIGHTNESS_REG_SIZE);
154     pci_register_bar(&s->dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->mmio_addr);
155
156     return 0;
157 }
158
159 /* external interface */
160 int pci_get_brightness(void)
161 {
162     return brightness_level;
163 }
164
165 DeviceState *pci_maru_brightness_init(PCIBus *bus)
166 {
167     return &pci_create_simple(bus, -1, QEMU_DEV_NAME)->qdev;
168 }
169
170 static void brightness_classinit(ObjectClass *klass, void *data)
171 {
172     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
173
174     k->no_hotplug = 1;
175     k->init = brightness_initfn;
176 }
177
178 static TypeInfo brightness_info = {
179     .name = QEMU_DEV_NAME,
180     .parent = TYPE_PCI_DEVICE,
181     .instance_size = sizeof(BrightnessState),
182     .class_init = brightness_classinit,
183 };
184
185 static void brightness_register_types(void)
186 {
187     type_register_static(&brightness_info);
188 }
189
190 type_init(brightness_register_types);