tizen 2.4 release
[platform/hal/backend/tm1/device-tm1.git] / src / device_manager_siop.c
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16 #include <string.h>
17 #include <sys/types.h>
18 #include <stdio.h>
19 #include <dirent.h>
20 #include <errno.h>
21
22 #include "devman_define_node_path.h"
23 #include "device_manager_siop.h"
24 #include "device_manager_io.h"
25
26 #define DEVMGR_LOG
27 #if defined (DEVMGR_LOG)
28 #define LOG_TAG "DEVICE_PLUGIN"
29 #include <dlog/dlog.h>
30 #define devmgr_log(fmt, args...)        SLOGD(fmt, ##args)
31 #else
32 #define devmgr_log(fmt, args...)
33 #endif
34
35 /*===================================================
36 Redwood SIOP table
37 Last updated on 2013.5.15
38 ====================================================*/
39 const device_siop_table siop_table[SIOP_SCENARIO_MAX][SIOP_LEVEL_MAX] =
40 {
41         {/***********  LCD ON *****************************/
42                 /*  ITEM :   cpu_freq cpu_core  charging  BL lcd_freq */
43                 /*  UNIT :    MHz               #core           mA              cd              Hz       */
44                 /* level 0 */{RELEASE,          IGNORE,         IGNORE,         100,            IGNORE},
45                 /* level 1 */{RELEASE,          IGNORE,         IGNORE,         100,            IGNORE},
46                 /* level 2 */{RELEASE,          IGNORE,         IGNORE,         69,             IGNORE},
47                 /* level 3 */{1497600,          IGNORE,         IGNORE,         63,             IGNORE},
48                 /* level 4 */{1267200,          IGNORE,         IGNORE,         55,             IGNORE},
49                 /* level 5 */{1036800,          IGNORE,         IGNORE,         55,             IGNORE},
50                 /* level 6 */{1036800,          IGNORE,         IGNORE,         55,             IGNORE},
51                 /* level 7 */{1036800,          IGNORE,         IGNORE,         55,             IGNORE},
52         },
53
54         {/***********  LCD OFF ****************************/
55                 /*  ITEM :   cpu_freq cpu_core  charging  BL lcd_freq */
56                 /*  UNIT :    MHz               #core           mA              cd              Hz       */
57                 /* level 0 */{IGNORE,           IGNORE,         IGNORE,         IGNORE,         IGNORE},
58                 /* level 1 */{IGNORE,           IGNORE,         IGNORE,         IGNORE,         IGNORE},
59                 /* level 2 */{IGNORE,           IGNORE,         IGNORE,         IGNORE,         IGNORE},
60                 /* level 3 */{IGNORE,           IGNORE,         IGNORE,         IGNORE,         IGNORE},
61                 /* level 4 */{IGNORE,           IGNORE,         IGNORE,         IGNORE,         IGNORE},
62                 /* level 5 */{IGNORE,           IGNORE,         IGNORE,         IGNORE,         IGNORE},
63                 /* level 6 */{IGNORE,           IGNORE,         IGNORE,         IGNORE,         IGNORE},
64                 /* level 7 */{IGNORE,           IGNORE,         IGNORE,         IGNORE,         IGNORE},
65         },
66 };
67
68
69 #define MAX_NAME 255
70 #define BUFF_MAX        255
71
72 enum display_type
73 {
74         DISP_MAIN = 0,
75         DISP_SUB,
76         DISP_MAX
77 };
78
79 struct display_info
80 {
81         enum display_type etype; /* FIXME:!! Main LCD or Sub LCD node */
82         char bl_name[MAX_NAME+1]; /* backlight name */
83         char lcd_name[MAX_NAME+1]; /* lcd name */
84 };
85
86 extern struct display_info disp_info[DISP_MAX];
87
88 int OEM_sys_get_battery_siop_active(int *value)
89 {
90         int ret;
91         int siop_activated, siop_level;
92
93         ret = sys_get_int(BATTERY_SIOP_ACTIVATE_PATH, &siop_activated);
94         if (ret == -1) {
95                 return -ENODEV;
96         }
97
98         ret = sys_get_int(BATTERY_SIOP_LEVEL_PATH, &siop_level);
99         if (ret == -1) {
100                 return -ENODEV;
101         }
102
103         if (siop_activated == 0)
104                 *value = 0;
105         else
106                 *value = siop_level;
107
108         return ret;
109
110 }
111
112 int OEM_sys_set_battery_siop_active(int value)
113 {
114         int ret;
115         int siop_activated, siop_level;
116
117         siop_level = value;
118
119         if (siop_level > 0)
120                 siop_activated = 1;
121         else
122                 siop_activated = 0;
123
124         ret = sys_set_int(BATTERY_SIOP_ACTIVATE_PATH, siop_activated);
125         if (ret == -1) {
126                 return -ENODEV;
127         }
128
129         ret = sys_set_int(BATTERY_SIOP_LEVEL_PATH, siop_level);
130         if (ret == -1) {
131                 return -ENODEV;
132         }
133
134         return ret;
135 }
136
137 static int sys_set_battery_siop_control(int level, int charge_current)
138 {
139         int ret;
140         int siop_activated=0;
141         char buf[BUFF_MAX];
142
143         if (charge_current>=0)
144                 siop_activated = 1;
145
146         ret = sys_set_int(BATTERY_SIOP_ACTIVATE_PATH, siop_activated);
147         if (ret == -1) {
148                 return -ENODEV;
149         }
150
151         if(siop_activated == 1) {
152                 ret = sys_set_int(BATTERY_SIOP_LEVEL_PATH, level);
153                 if (ret == -1) {
154                         return -ENODEV;
155                 }
156
157                 snprintf(buf, sizeof(buf), "%d %d", level, charge_current);
158
159                 ret = sys_set_str(BATTERY_SIOP_LEVEL_CURRENT_PATH, buf);
160                 if (ret == -1) {
161                         return -ENODEV;
162                 }
163         }
164
165         return ret;
166 }
167
168 int OEM_sys_get_backlight_overheating_control(int index, int *value)
169 {
170         int ret = -1;
171         char path[MAX_NAME+1];
172
173         if (index >= DISP_MAX) {
174                 devmgr_log("supports %d display node", DISP_MAX);
175                 return ret;
176         }
177
178         snprintf(path, MAX_NAME, MDNIE_BACKLIGHT_OVERHEATING_PATH);
179         if(!sys_check_node((char *)path)) {
180                 ret = sys_get_int(path, value);
181         } else {
182                 snprintf(path, MAX_NAME, BACKLIGHT_OVERHEATING_PATH, disp_info[index].bl_name);
183                 ret = sys_get_int(path, value);
184         }
185
186         return ret;
187 }
188
189 extern int current_brightness;
190 extern int set_backlight_brightness(int index, int value);
191
192 int OEM_sys_set_backlight_overheating_control(int index, int value)
193 {
194         int ret = -1;
195         char path[MAX_NAME+1];
196
197         if (index >= DISP_MAX) {
198                 devmgr_log("supports %d display node", DISP_MAX);
199                 return ret;
200         }
201
202         if ((current_brightness != -1) && (value > current_brightness)) {
203                 value = current_brightness;
204         }
205
206         ret = set_backlight_brightness(index, value);
207         if (ret)
208         {
209                 devmgr_log("Can't set backlight brightness");
210                 return ret;
211         }
212
213         return ret;
214 }
215
216 int current_level=-1, current_mode=-1;
217 device_siop_table current_table = {RELEASE, RELEASE, RELEASE, 100, 60};
218
219 int OEM_sys_set_siop_control(int level, int mode)
220 {
221         device_siop_table new_table;
222
223         memcpy(&new_table, &siop_table[mode][level], sizeof(device_siop_table));
224
225         devmgr_log("level %d, mode %d",level, mode);
226         devmgr_log("current_siop {%d, %d, %d, %d, %d}",current_table.cpu_freq, current_table.cpu_max_core,\
227                 current_table.battery_charing, current_table.backlight, current_table.lcd_freq);
228         devmgr_log("new_siop {%d, %d, %d, %d, %d}",new_table.cpu_freq, new_table.cpu_max_core,\
229                 new_table.battery_charing, new_table.backlight, new_table.lcd_freq);
230
231         if((current_level==level) && (current_mode==mode))
232                 return 0;
233
234         /* CPU */
235         if((new_table.cpu_freq != IGNORE) && (new_table.cpu_freq != current_table.cpu_freq)) {
236                 devmgr_log("CPU max clock %d",new_table.cpu_freq);
237                 sys_set_int(CPUFREQ_POWER_MAX_FREQ_PATH, new_table.cpu_freq);
238                 current_table.cpu_freq = new_table.cpu_freq;
239         }
240         if((new_table.cpu_max_core != IGNORE) && (new_table.cpu_max_core != current_table.cpu_max_core)) {
241                 devmgr_log("CPU max core num %d",new_table.cpu_max_core);
242                 sys_set_int(CPU_ENABLE_MAX_NUMBER_PATH, new_table.cpu_max_core);
243                 current_table.cpu_max_core = new_table.cpu_max_core;
244         }
245
246         /* Battery charging */
247         //OEM_sys_set_battery_siop_active(siop_table[level].battery_charing);
248         if((new_table.battery_charing != IGNORE) && (new_table.battery_charing != current_table.battery_charing)) {
249                 devmgr_log("battery_charing current %d",new_table.battery_charing);
250                 sys_set_battery_siop_control(level, new_table.battery_charing);
251                 current_table.battery_charing = new_table.battery_charing;
252         }
253
254         /* LCD */
255         if((new_table.backlight != IGNORE) && (new_table.backlight != current_table.backlight)) {
256                 devmgr_log("backlight %d",new_table.backlight);
257                 OEM_sys_set_backlight_overheating_control(DISP_MAIN, new_table.backlight);
258                 current_table.backlight = new_table.backlight;
259         }
260
261         if((new_table.lcd_freq != IGNORE) && (new_table.lcd_freq != current_table.lcd_freq)) {
262                 devmgr_log("lcd_freq %d",new_table.lcd_freq);
263                 sys_set_int(DISPLAY_FRAME_RATE_PATH, new_table.lcd_freq);
264                 current_table.lcd_freq = new_table.lcd_freq;
265         }
266
267         current_level=level;
268         current_mode=mode;
269
270         return 0;
271 }
272