4 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <hal/device/hal-display.h>
21 #include "shared/log.h"
22 #include "power/power-suspend.h"
25 #include "display-backlight.h"
26 #include "display-dpms.h"
28 #define DUMP_MODE_WAITING_TIME 600000 /* milisecond */
30 static const struct display_config *display_conf;
31 static struct _backlight_ops *backlight_ops;
33 static int default_brightness;
34 static int custom_brightness;
35 static bool g_custom_status;
36 static int force_brightness;
37 static guint release_timer;
39 /* FIXME: This function is for temporary use, should be fixed after plugin refactoring */
40 int display_backlight_get_max_brightness(int *max_brightness)
49 *max_brightness = max;
53 if (!display_is_hal_backend_available()) {
54 _E("There is no HAL for display.");
58 ret = hal_device_display_get_max_brightness(&max);
60 _W("Get max brightness is not supported.");
61 *max_brightness = DEFAULT_DISPLAY_MAX_BRIGHTNESS;
64 _E("Failed to get max brightness: %d", ret);
70 /* FIXME: This function is for temporary use, should be fixed after plugin refactoring */
71 int display_backlight_get_normalized_brightness(int raw_brightness,
72 int *normalized_brightness)
74 int quotient, remainder;
77 if (!normalized_brightness)
80 ret = display_backlight_get_max_brightness(&max);
82 _E("Failed to get max brightness.");
87 * Thus the brightness value need to be calculated using real brightness.
88 * ex) Let's suppose that the maximum brightness of driver is 255.
89 * case 1) When the user sets the brightness to 41,
91 * 41 * 255 / 100 = 104.55 = 104 (rounded off)
92 * case 2) When the user gets the brightness,
93 * the driver returns the brightness 104.
94 * Thus the brightness to users is
95 * 104 * 100 / 255 = 40.7843.... = 41 (rounded up)
97 quotient = raw_brightness * 100 / max;
98 remainder = raw_brightness * 100 % max;
102 *normalized_brightness = quotient;
106 int display_backlight_set_default_brightness(int brightness)
108 if (brightness < PM_MIN_BRIGHTNESS || brightness > PM_MAX_BRIGHTNESS) {
110 brightness = display_conf->pm_default_brightness;
115 default_brightness = brightness;
119 /* FIXME: default_brightness value should be initialized by vconf_get_int */
120 int display_backlight_get_default_brightness(int *brightness)
122 *brightness = default_brightness;
126 int display_backlight_set_brightness_by_dim_brightness(void)
130 ret = backlight_ops->set_brightness(PM_DIM_BRIGHTNESS);
134 pm_history_save(PM_LOG_LCD_DIM, get_pm_cur_state());
136 pm_history_save(PM_LOG_LCD_DIM_FAIL, get_pm_cur_state());
141 /* FIXME: display_dimstay_check function should be changed to plugin api call */
142 int display_backlight_update_by_default_brightness(void)
145 int default_brightness;
148 display_backlight_get_custom_status(&custom_status);
150 _I("Custom brightness mode, no update brightness");
154 if (display_dimstay_check()) {
155 ret = display_backlight_set_brightness_by_dim_brightness();
157 display_backlight_get_default_brightness(&default_brightness);
158 ret = backlight_ops->set_brightness(default_brightness);
164 /* FIXME: This custom_brightness policy can be changed, after plugin refactoring */
165 void display_backlight_set_custom_brightness(int brightness)
167 custom_brightness = brightness;
170 /* FIXME: display_dimstay_check function should be changed to plugin api call */
171 int display_backlight_update_by_custom_brightness(void)
175 if (custom_brightness < PM_MIN_BRIGHTNESS || custom_brightness > PM_MAX_BRIGHTNESS)
178 if (display_dimstay_check()) {
179 ret = display_backlight_set_brightness_by_dim_brightness();
181 _I("Custom brightness restored! %d", custom_brightness);
182 ret = backlight_ops->set_brightness(custom_brightness);
188 /* FIXME: This custom_status policy can be changed, after plugin refactoting*/
189 void display_backlight_set_custom_status(bool on)
191 g_custom_status = on;
194 /* FIXME: This custom_status policy can be changed, after plugin refactoting*/
195 void display_backlight_get_custom_status(bool *on)
197 *on = g_custom_status;
200 /* FIXME: This function is not tested */
201 int display_backlight_get_brightness_by_light_sensor(float lmax, float lmin,
202 float light, int *brightness)
204 int raw_brightness, ret = 0;
206 if (!display_is_hal_backend_available())
209 ret = hal_device_display_get_auto_brightness(lmax, lmin, light, &raw_brightness);
212 _E("Get auto brightness is not supported.");
214 _E("Failed to get brightness by light sensor: %d", ret);
218 ret = display_backlight_get_normalized_brightness(raw_brightness, brightness);
220 _E("Failed to get normalized brightness.");
227 static gboolean blink_cb(gpointer data)
231 backlight_ops->set_brightness(flag ? PM_MAX_BRIGHTNESS : PM_MIN_BRIGHTNESS);
234 return G_SOURCE_CONTINUE;
237 void display_backlight_set_blink(int timeout)
239 static guint blink_timer;
242 g_source_remove(blink_timer);
247 _W("timeout value is invalid %d", timeout);
252 display_backlight_update_by_default_brightness();
256 blink_timer = g_timeout_add(timeout, blink_cb, NULL);
259 static gboolean release_blink_cb(gpointer data)
261 display_backlight_set_blink(0);
264 return G_SOURCE_REMOVE;
267 void display_backlight_unset_blink(void)
270 g_source_remove(release_timer);
274 release_timer = g_timeout_add(DUMP_MODE_WAITING_TIME, release_blink_cb, NULL);
277 /* FIXME: This function is only used for wearable plugin */
278 int display_backlight_set_force_brightness(int brightness)
280 if (brightness < PM_DIM_BRIGHTNESS || brightness > PM_MAX_BRIGHTNESS)
283 force_brightness = brightness;
288 /* FIXME: This function is only used for wearable plugin */
289 void display_backlight_get_force_brightness(int *brightness)
291 *brightness = force_brightness;
294 /* FIXME: This code structure should be changed to plugin api call, after plugin refactoting*/
295 static void __CONSTRUCTOR__ initialize(void)
297 display_conf = get_var_display_config();
299 _W("Failed to get display configuration variable.");
301 backlight_ops = get_var_backlight_ops();
303 _W("Failed to get backlight operator variable.");