display: Move set brightness with delay function from plugins to core display module
[platform/core/system/deviced.git] / plugins / mobile / display / device-interface.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2012 - 2015 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdbool.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <limits.h>
29 #include <math.h>
30 #include <assert.h>
31 #include <errno.h>
32 #include <dlfcn.h>
33 #include <hal/device/hal-display.h>
34
35 #include "ambient-mode.h"
36 #include "core/log.h"
37 #include "shared/devices.h"
38 #include "shared/common.h"
39 #include "shared/device-notifier.h"
40 #include "util.h"
41 #include "device-interface.h"
42 #include "vconf.h"
43 #include "core.h"
44 #include "display/display-dpms.h"
45 #include "display/display.h"
46 #include "display/display-lock.h"
47 #include "display/display-backlight.h"
48 #include "display/display-panel.h"
49 #include "power/power-boot.h"
50 #include "power/power-suspend.h"
51
52 #define TOUCH_ON        1
53 #define TOUCH_OFF       0
54
55 #define LCD_PHASED_MAX_BRIGHTNESS       100
56 #define LCD_PHASED_DELAY                10000   /* microsecond */
57
58 #define DISPLAY_HAL_LIB_PATH "/usr/lib/libdisplay-hal.so"
59
60 #define GESTURE_STR                     "gesture"
61 #define POWERKEY_STR                    "powerkey"
62 #define EVENT_STR                       "event"
63 #define TOUCH_STR                       "touch"
64 #define TIMEOUT_STR                     "timeout"
65 #define PROXIMITY_STR                   "proximity"
66 #define PALM_STR                        "palm"
67 #define UNKNOWN_STR                     "unknown"
68
69 static struct _backlight_ops backlight_ops;
70 static bool display_dev_available = false;
71 static const struct display_config *display_conf;
72
73 inline struct _backlight_ops *get_var_backlight_ops(void)
74 {
75         return &backlight_ops;
76 }
77
78 bool display_dev_ready(void)
79 {
80         return display_dev_available;
81 }
82
83 bool display_dimstay_check(void)
84 {
85         if (get_pm_status_flag() & DIM_FLAG)
86                 return true;
87
88         if ((get_pm_status_flag() & PWRSV_FLAG) && !(get_pm_status_flag() & BRTCH_FLAG))
89                 return true;
90
91         return false;
92 }
93
94 static void change_brightness(int start, int end, int step)
95 {
96         int diff, val;
97         int ret = -1;
98         int prev;
99
100         if (display_dimstay_check())
101                 return;
102
103         ret = backlight_ops.get_brightness(&prev);
104         if (ret < 0) {
105                 _E("Failed to get brightness: %d", ret);
106                 return;
107         }
108
109         if (prev == end)
110                 return;
111
112         if (get_pm_status_flag() & DIM_MASK)
113                 end = 0;
114
115         _I("start %d end %d step %d", start, end, step);
116
117         if (display_dev_available) {
118                 ret = hal_device_display_set_multi_brightness(end, step, LCD_PHASED_DELAY);
119                 if (ret != -ENODEV) {
120                         if (ret < 0)
121                                 _E("Failed to set_multi_brightness (%d)", ret);
122
123                         backlight_ops.set_brightness(end);
124
125                         return;
126                 }
127         }
128
129         diff = end - start;
130
131         if (abs(diff) < step)
132                 val = (diff > 0 ? 1 : -1);
133         else
134                 val = (int)ceil((double)diff / step);
135
136         while (start != end) {
137                 if (val == 0) break;
138
139                 start += val;
140                 if ((val > 0 && start > end) ||
141                     (val < 0 && start < end))
142                         start = end;
143
144                 display_backlight_set_brightness_with_delay(start, LCD_PHASED_DELAY);
145         }
146 }
147
148 static int set_brightness(int val)
149 {
150         int max, ret;
151         int default_brightness = 0, force_brightness = 0;
152
153         display_backlight_get_default_brightness(&default_brightness);
154         display_backlight_get_force_brightness(&force_brightness);
155
156         if (!display_dev_available) {
157                 _E("There is no display device.");
158                 return -ENOENT;
159         }
160
161         ret = display_backlight_get_max_brightness(&max);
162         if (ret < 0) {
163                 _E("Failed to get max brightness.");
164                 return ret;
165         }
166
167         if (force_brightness > 0 && val != PM_DIM_BRIGHTNESS) {
168                 _I("brightness(%d), force brightness(%d)",
169                     val, force_brightness);
170                 val = force_brightness;
171         }
172
173         if (get_pm_status_flag() & DIM_MASK)
174                 val = 0;
175
176         /* Maximum Brightness to users is 100.
177          * Thus real brightness need to be calculated */
178         val = val * max / 100;
179
180         _I("set brightness %d (default:%d)", val, default_brightness);
181         device_notify(DEVICE_NOTIFIER_DISPLAY_BRIGHTNESS, (void *)&val);
182
183         return hal_device_display_set_brightness(val);
184 }
185
186 static int get_brightness(int *val)
187 {
188         int brt, ret;
189
190         if (!display_dev_available) {
191                 _E("There is no display device.");
192                 return -ENOENT;
193         }
194
195         ret = hal_device_display_get_brightness(&brt);
196         if (ret < 0) {
197                 if (ret == -ENODEV)
198                         _E("Get brightness is not supported.");
199                 else
200                         _E("Failed to get brightness: %d", ret);
201
202                 return ret;
203         }
204
205         ret = display_backlight_get_normalized_brightness(brt, val);
206         if (ret < 0) {
207                 _E("Failed to get normalized brightness.");
208                 return ret;
209         }
210
211         return 0;
212 }
213
214 /* It was operated only AOD enter & leave */
215 static int backlight_transit_state(int state)
216 {
217         int brt, val;
218         int start, end;
219         int default_brightness = 0;
220
221         display_backlight_get_default_brightness(&default_brightness);
222
223         backlight_ops.get_brightness(&brt);
224
225         if (state == DPMS_OFF) {
226                 start = brt;
227                 end = display_conf->aod_enter_level;
228
229                 /*
230                  * The value of backlight_ops.get_brightness is system brightness.
231                  * But when device is LBM, the value is not same with real brightness.
232                  * So it should be read exactly value for transit smooth effect
233                  */
234                 get_brightness(&val);
235
236                 if (val > display_conf->aod_enter_level)
237                         backlight_ops.transit_brt(start, end, display_conf->brightness_change_step);
238         } else {
239                 /* prevent transit effect when another effect is already executed */
240                 if (brt != display_conf->aod_enter_level) {
241                         _W("effect is already executed brt(%d) aod_level(%d)",
242                                         brt, display_conf->aod_enter_level);
243                         return 0;
244                 }
245
246                 start = display_conf->aod_enter_level;
247                 end = default_brightness;
248                 backlight_ops.transit_brt(start, end, display_conf->brightness_change_step);
249         }
250
251         return 0;
252 }
253
254 static void restore_brightness_func(void)
255 {
256         backlight_ops.set_brightness = set_brightness;
257         backlight_ops.get_brightness = get_brightness;
258         backlight_ops.transit_brt = change_brightness;
259 }
260
261 static struct _backlight_ops backlight_ops = {
262         .get_lcd_power = dpms_get_cached_state,
263         .set_brightness = set_brightness,
264         .get_brightness = get_brightness,
265         .restore_brightness_func = restore_brightness_func,
266         .transit_state = backlight_transit_state,
267         .transit_brt = change_brightness,
268 };
269
270 int display_service_load(void)
271 {
272         int r;
273
274         if (display_dev_available)
275                 return 0;
276
277         r = hal_device_display_get_backend();
278         if (r < 0) {
279                 _E("There is no HAL for display.");
280                 display_dev_available = false;
281                 display_set_hal_backend_available(display_dev_available);
282                 return 0;
283         }
284
285         display_dev_available = true;
286         display_set_hal_backend_available(display_dev_available);
287         _D("Display device structure load success.");
288         return 0;
289 }
290
291 int display_service_free(void)
292 {
293         display_dev_available = false;
294         return hal_device_display_put_backend();
295 }
296
297 static int delayed_init_done(void *data)
298 {
299         static int done = false;
300
301         if (!data)
302                 return done;
303
304         done = *(int *)data;
305
306         return done;
307 }
308
309 int is_lcdon_blocked(void)
310 {
311         /* block lcdon until booting done in silent boot mode */
312         if (silent_boot && !delayed_init_done(NULL))
313                 return LCDON_BLOCK_DURING_SILENT_BOOT;
314
315         return LCDON_BLOCK_NONE;
316 }
317
318 int init_sysfs(unsigned int flags)
319 {
320         register_notifier(DEVICE_NOTIFIER_VITAL_STATE, vital_state_changed);
321         register_notifier(DEVICE_NOTIFIER_DELAYED_INIT, delayed_init_done);
322
323         return 0;
324 }
325
326 int exit_sysfs(void)
327 {
328         const struct device_ops *ops = NULL;
329
330         display_backlight_update_by_default_brightness();
331
332         dpms_exit();
333
334         ops = find_device("touchscreen");
335         if (!check_default(ops))
336                 ops->start(NORMAL_MODE);
337
338         ops = find_device("touchkey");
339         if (!check_default(ops))
340                 ops->start(NORMAL_MODE);
341
342         unregister_notifier(DEVICE_NOTIFIER_VITAL_STATE, vital_state_changed);
343
344         return 0;
345 }
346
347 static void __CONSTRUCTOR__ initialize(void)
348 {
349         display_conf = get_var_display_config();
350         if (!display_conf)
351                 _E("Failed to get display configuration variable.");
352 }
353