Initialize
[sdk/emulator/qemu.git] / tizen / src / hw / brightness.c
1 /* 
2  * Qemu brightness emulator
3  *
4  * Copyright (C) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: 
7  * DoHyung Hong <don.hong@samsung.com>
8  * SeokYeon Hwang <syeon.hwang@samsung.com>
9  * Hyunjun Son <hj79.son@samsung.com>
10  * SangJin Kim <sangjin3.kim@samsung.com>
11  * MunKyu Im <munkyu.im@samsung.com>
12  * KiTae Kim <kt920.kim@samsung.com>
13  * JinHyung Jo <jinhyung.jo@samsung.com>
14  * SungMin Ha <sungmin82.ha@samsung.com>
15  * JiHye Kim <jihye1128.kim@samsung.com>
16  * GiWoong Kim <giwoong.kim@samsung.com>
17  * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
18  * DongKyun Yun <dk77.yun@samsung.com>
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 #include "pci.h"
42 #include "pci_ids.h"
43
44 #define QEMU_DEV_NAME                       "brightness"
45
46 #define BRIGHTNESS_MEM_SIZE         (4 * 1024)      /* 4KB */
47 #define BRIGHTNESS_REG_SIZE         256
48
49 #define BRIGHTNESS_MIN              (0)
50 #define BRIGHTNESS_MAX              (24)
51
52 typedef struct BrightnessState {
53     PCIDevice dev;
54     
55     ram_addr_t  offset;
56     int brightness_mmio_io_addr;
57
58     uint32_t ioport_addr;   // guest addr
59     uint32_t mmio_addr;     // guest addr
60 } BrightnessState;
61
62 enum {
63     BRIGHTNESS_LEVEL    = 0x00,
64     BRIGHTNESS_OFF    = 0x04,
65 };
66
67 //uint8_t* brightness_ptr;  // pointer in qemu space
68 uint32_t brightness_level = 24;
69 uint32_t brightness_off = 0;
70
71 //#define DEBUG_BRIGHTNESS
72
73 #if defined (DEBUG_BRIGHTNESS)
74 #  define DEBUG_PRINT(x) do { printf x ; } while (0)
75 #else
76 #  define DEBUG_PRINT(x)
77 #endif
78
79 static uint32_t brightness_reg_read(void *opaque, target_phys_addr_t addr)
80 {
81     switch (addr & 0xFF) {
82     case BRIGHTNESS_LEVEL:
83         DEBUG_PRINT(("brightness_reg_read: brightness_level = %d\n", brightness_level));
84         return brightness_level;
85
86     default:
87         fprintf(stderr, "wrong brightness register read - addr : %d\n", addr);
88     }
89
90     return 0;
91 }
92
93 static void brightness_reg_write(void *opaque, target_phys_addr_t addr, uint32_t val)
94 {
95     DEBUG_PRINT(("brightness_reg_write: addr = %d, val = %d\n", addr, val));
96
97 #if BRIGHTNESS_MIN > 0
98     if (val < BRIGHTNESS_MIN || val > BRIGHTNESS_MAX) {
99 #else
100     if (val > BRIGHTNESS_MAX) {
101 #endif
102         fprintf(stderr, "brightness_reg_write: Invalide brightness level.\n");
103     }
104
105     switch (addr & 0xFF) {
106     case BRIGHTNESS_LEVEL:
107         brightness_level = val;
108         return;
109     case BRIGHTNESS_OFF:
110         DEBUG_PRINT(("Brightness off : %d\n", val));
111         brightness_off = val;
112         return;
113     default:
114         fprintf(stderr, "wrong brightness register write - addr : %d\n", addr);
115     }
116 }
117
118 static CPUReadMemoryFunc * const brightness_reg_readfn[3] = {
119     brightness_reg_read,
120     brightness_reg_read,
121     brightness_reg_read,
122 };
123
124 static CPUWriteMemoryFunc * const brightness_reg_writefn[3] = {
125     brightness_reg_write,
126     brightness_reg_write,
127     brightness_reg_write,
128 };
129
130 #if 0
131 static void brightness_ioport_map(PCIDevice *dev, int region_num,
132                    pcibus_t addr, pcibus_t size, int type)
133 {
134     //BrightnessState *s = DO_UPCAST(BrightnessState, dev, dev);
135
136     //cpu_register_physical_memory(addr, BRIGHTNESS_MEM_SIZE,
137     //           s->offset);
138
139     //s->ioport_addr = addr;
140 }
141 #endif
142
143 static void brightness_mmio_map(PCIDevice *dev, int region_num,
144                    pcibus_t addr, pcibus_t size, int type)
145 {
146     BrightnessState *s = DO_UPCAST(BrightnessState, dev, dev);
147     
148     cpu_register_physical_memory(addr, BRIGHTNESS_REG_SIZE,
149                                  s->brightness_mmio_io_addr);
150     
151     s->mmio_addr = addr;
152 }
153
154 static int brightness_initfn(PCIDevice *dev)
155 {
156     BrightnessState *s = DO_UPCAST(BrightnessState, dev, dev);
157     uint8_t *pci_conf = s->dev.config;
158
159     pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_TIZEN);
160     pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_VIRTUAL_BRIGHTNESS);
161     pci_config_set_class(pci_conf, PCI_CLASS_DISPLAY_OTHER);
162
163     //s->offset = qemu_ram_alloc(NULL, "brightness", BRIGHTNESS_MEM_SIZE);
164     //brightness_ptr = qemu_get_ram_ptr(s->offset);
165
166     s->brightness_mmio_io_addr = cpu_register_io_memory(brightness_reg_readfn,
167                                              brightness_reg_writefn, s,
168                                              DEVICE_LITTLE_ENDIAN);
169  
170     /* setup memory space */
171     /* memory #0 device memory (overlay surface) */
172     /* memory #1 memory-mapped I/O */
173     //pci_register_bar(&s->dev, 0, BRIGHTNESS_MEM_SIZE,
174     //      PCI_BASE_ADDRESS_SPACE_IO, brightness_ioport_map);
175
176     pci_register_bar(&s->dev, 1, BRIGHTNESS_REG_SIZE,
177                      PCI_BASE_ADDRESS_SPACE_MEMORY, brightness_mmio_map);
178
179     return 0;
180 }
181
182 /* external interface */
183 int pci_get_brightness(void)
184 {
185     return brightness_level;
186 }
187
188 int pci_brightness_init(PCIBus *bus)
189 {
190     pci_create_simple(bus, -1, QEMU_DEV_NAME);
191     return 0;
192 }
193
194 static PCIDeviceInfo brightness_info = {
195     .qdev.name    = QEMU_DEV_NAME,
196     .qdev.size    = sizeof(BrightnessState),
197     .no_hotplug   = 1,
198     .init         = brightness_initfn,
199 };
200
201 static void brightness_register(void)
202 {
203     pci_qdev_register(&brightness_info);
204 }
205
206 device_init(brightness_register);