blob: 60ed911e50e6647f179cd982909461fafad03033 [file] [log] [blame]
Sungmin Choib5148892012-07-02 17:00:07 -07001/*
Fenglin Wu862b56b2018-02-02 10:20:28 +08002 * Copyright (C) 2014, 2017-2018 The Linux Foundation. All rights reserved.
Sathish Ambley4342f272017-01-04 10:57:05 -08003 * Not a contribution
Sungmin Choib5148892012-07-02 17:00:07 -07004 * Copyright (C) 2008 The Android Open Source Project
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// #define LOG_NDEBUG 0
Sungmin Choib5148892012-07-02 17:00:07 -070021
Naseer Ahmedfcad05e2018-03-06 20:41:14 -050022#include <log/log.h>
Xu Yang586c6d52016-09-19 17:54:16 +080023#include <cutils/properties.h>
Sungmin Choib5148892012-07-02 17:00:07 -070024#include <stdint.h>
Arun Kumar K.R62031612015-07-30 11:38:19 -070025#include <stdlib.h>
Sungmin Choib5148892012-07-02 17:00:07 -070026#include <string.h>
27#include <unistd.h>
28#include <errno.h>
29#include <fcntl.h>
30#include <pthread.h>
31
32#include <sys/ioctl.h>
33#include <sys/types.h>
34
35#include <hardware/lights.h>
Xu Yang586c6d52016-09-19 17:54:16 +080036#include "lights_prv.h"
Sungmin Choib5148892012-07-02 17:00:07 -070037
Sathish Ambley4342f272017-01-04 10:57:05 -080038#ifndef DEFAULT_LOW_PERSISTENCE_MODE_BRIGHTNESS
39#define DEFAULT_LOW_PERSISTENCE_MODE_BRIGHTNESS 0x80
40#endif
41
Sungmin Choib5148892012-07-02 17:00:07 -070042/******************************************************************************/
43
44static pthread_once_t g_init = PTHREAD_ONCE_INIT;
45static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
46static struct light_state_t g_notification;
47static struct light_state_t g_battery;
Sathish Ambley4342f272017-01-04 10:57:05 -080048static int g_last_backlight_mode = BRIGHTNESS_MODE_USER;
Sungmin Choib5148892012-07-02 17:00:07 -070049static int g_attention = 0;
Xu Yang586c6d52016-09-19 17:54:16 +080050static int g_brightness_max = 0;
Pullakavi Srinivasda0b3252017-10-12 17:50:41 +053051static bool g_has_persistence_node = false;
Sungmin Choib5148892012-07-02 17:00:07 -070052
Sungmin Choib5148892012-07-02 17:00:07 -070053char const*const LCD_FILE
54 = "/sys/class/leds/lcd-backlight/brightness";
55
Saurabh Shah8b021cf2017-03-14 12:16:43 -070056char const*const LCD_FILE2
57 = "/sys/class/backlight/panel0-backlight/brightness";
58
HuiWangf73bc572013-07-31 10:48:36 +080059char const*const BUTTON_FILE
60 = "/sys/class/leds/button-backlight/brightness";
61
Sathish Ambley4342f272017-01-04 10:57:05 -080062char const*const PERSISTENCE_FILE
63 = "/sys/class/graphics/fb0/msm_fb_persist_mode";
64
Fenglin Wu862b56b2018-02-02 10:20:28 +080065enum rgb_led {
66 LED_RED = 0,
67 LED_GREEN,
68 LED_BLUE,
69};
70
71char *led_names[] = {
72 "red",
73 "green",
74 "blue",
75};
Sungmin Choib5148892012-07-02 17:00:07 -070076/**
77 * device methods
78 */
79
80void init_globals(void)
81{
82 // init the mutex
83 pthread_mutex_init(&g_lock, NULL);
84}
85
Fenglin Wu862b56b2018-02-02 10:20:28 +080086static int write_int(char const* path, int value)
Sungmin Choib5148892012-07-02 17:00:07 -070087{
88 int fd;
89 static int already_warned = 0;
90
91 fd = open(path, O_RDWR);
92 if (fd >= 0) {
93 char buffer[20];
Manoj Kumar AVM001b3092014-04-29 22:08:51 -070094 int bytes = snprintf(buffer, sizeof(buffer), "%d\n", value);
Dileep Kumar Reddibf333c72014-02-25 14:32:51 +053095 ssize_t amt = write(fd, buffer, (size_t)bytes);
Sungmin Choib5148892012-07-02 17:00:07 -070096 close(fd);
97 return amt == -1 ? -errno : 0;
98 } else {
99 if (already_warned == 0) {
Fenglin Wu862b56b2018-02-02 10:20:28 +0800100 ALOGE("write_int failed to open %s, errno = %d\n", path, errno);
Sungmin Choib5148892012-07-02 17:00:07 -0700101 already_warned = 1;
102 }
103 return -errno;
104 }
105}
106
Fenglin Wu862b56b2018-02-02 10:20:28 +0800107static bool file_exists(const char *file)
108{
109 int fd;
110
111 fd = open(file, O_RDWR);
112 if (fd < 0) {
113 ALOGE("failed to open %s, errno=%d\n", file, errno);
114 return false;
115 }
116
117 close(fd);
118 return true;
119}
120
Sungmin Choib5148892012-07-02 17:00:07 -0700121static int
122is_lit(struct light_state_t const* state)
123{
124 return state->color & 0x00ffffff;
125}
126
127static int
128rgb_to_brightness(struct light_state_t const* state)
129{
130 int color = state->color & 0x00ffffff;
131 return ((77*((color>>16)&0x00ff))
132 + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8;
133}
134
135static int
136set_light_backlight(struct light_device_t* dev,
137 struct light_state_t const* state)
138{
139 int err = 0;
140 int brightness = rgb_to_brightness(state);
Sathish Ambley4342f272017-01-04 10:57:05 -0800141 unsigned int lpEnabled =
142 state->brightnessMode == BRIGHTNESS_MODE_LOW_PERSISTENCE;
Arun Kumar K.R0efad602014-01-21 21:32:36 -0800143 if(!dev) {
144 return -1;
145 }
Sathish Ambley4342f272017-01-04 10:57:05 -0800146
Sungmin Choib5148892012-07-02 17:00:07 -0700147 pthread_mutex_lock(&g_lock);
Sathish Ambley4342f272017-01-04 10:57:05 -0800148 // Toggle low persistence mode state
Pullakavi Srinivasda0b3252017-10-12 17:50:41 +0530149 bool persistence_mode = ((g_last_backlight_mode != state->brightnessMode && lpEnabled) ||
150 (!lpEnabled &&
151 g_last_backlight_mode == BRIGHTNESS_MODE_LOW_PERSISTENCE));
152 bool cannot_handle_persistence = !g_has_persistence_node && persistence_mode;
153 if (g_has_persistence_node) {
154 if (persistence_mode) {
155 if ((err = write_int(PERSISTENCE_FILE, lpEnabled)) != 0) {
156 ALOGE("%s: Failed to write to %s: %s\n", __FUNCTION__,
157 PERSISTENCE_FILE, strerror(errno));
158 }
159 if (lpEnabled != 0) {
160 brightness = DEFAULT_LOW_PERSISTENCE_MODE_BRIGHTNESS;
161 }
Sathish Ambley4342f272017-01-04 10:57:05 -0800162 }
Pullakavi Srinivasda0b3252017-10-12 17:50:41 +0530163 g_last_backlight_mode = state->brightnessMode;
Sathish Ambley4342f272017-01-04 10:57:05 -0800164 }
165
Sathish Ambley4342f272017-01-04 10:57:05 -0800166 if (!err) {
Saurabh Shah8b021cf2017-03-14 12:16:43 -0700167 if (!access(LCD_FILE, F_OK)) {
168 err = write_int(LCD_FILE, brightness);
169 } else {
170 err = write_int(LCD_FILE2, brightness);
171 }
Sathish Ambley4342f272017-01-04 10:57:05 -0800172 }
173
Sungmin Choib5148892012-07-02 17:00:07 -0700174 pthread_mutex_unlock(&g_lock);
Pullakavi Srinivasda0b3252017-10-12 17:50:41 +0530175 return cannot_handle_persistence ? -ENOSYS : err;
Sungmin Choib5148892012-07-02 17:00:07 -0700176}
177
178static int
Xu Yang586c6d52016-09-19 17:54:16 +0800179set_light_backlight_ext(struct light_device_t* dev,
180 struct light_state_t const* state)
181{
182 int err = 0;
183
184 if(!dev) {
185 return -1;
186 }
187
188 int brightness = state->color & 0x00ffffff;
189 pthread_mutex_lock(&g_lock);
190
191 if (brightness >= 0 && brightness <= g_brightness_max) {
192 set_brightness_ext_level(brightness);
193 }
194
195 pthread_mutex_unlock(&g_lock);
196
197 return err;
198}
199
Fenglin Wu862b56b2018-02-02 10:20:28 +0800200static int set_rgb_led_brightness(enum rgb_led led, int brightness)
201{
202 char file[48];
203
204 snprintf(file, sizeof(file), "/sys/class/leds/%s/brightness", led_names[led]);
205 return write_int(file, brightness);
206}
207
208static int set_rgb_led_timer_trigger(enum rgb_led led, int onMS, int offMS)
209{
210 char file[48];
211 int rc;
212
213 snprintf(file, sizeof(file), "/sys/class/leds/%s/delay_off", led_names[led]);
214 rc = write_int(file, offMS);
215 if (rc < 0)
216 goto out;
217
218 snprintf(file, sizeof(file), "/sys/class/leds/%s/delay_on", led_names[led]);
219 rc = write_int(file, onMS);
220 if (rc < 0)
221 goto out;
222
223 return 0;
224out:
225 ALOGD("%s doesn't support timer trigger\n", led_names[led]);
226 return rc;
227}
228
229static int set_rgb_led_hw_blink(enum rgb_led led, int blink)
230{
231 char file[48];
232
233 snprintf(file, sizeof(file), "/sys/class/leds/%s/breath", led_names[led]);
234 if (!file_exists(file))
235 snprintf(file, sizeof(file), "/sys/class/leds/%s/blink", led_names[led]);
236
237 return write_int(file, blink);
238}
239
Xu Yang586c6d52016-09-19 17:54:16 +0800240static int
Sungmin Choib5148892012-07-02 17:00:07 -0700241set_speaker_light_locked(struct light_device_t* dev,
242 struct light_state_t const* state)
243{
Arun Kumar K.R0efad602014-01-21 21:32:36 -0800244 int red, green, blue;
Sungmin Choib5148892012-07-02 17:00:07 -0700245 int onMS, offMS;
246 unsigned int colorRGB;
Fenglin Wu862b56b2018-02-02 10:20:28 +0800247 int blink = 0;
248 int rc = 0;
Sungmin Choib5148892012-07-02 17:00:07 -0700249
Arun Kumar K.R0efad602014-01-21 21:32:36 -0800250 if(!dev) {
251 return -1;
252 }
253
Sungmin Choib5148892012-07-02 17:00:07 -0700254 colorRGB = state->color;
Sungmin Choib5148892012-07-02 17:00:07 -0700255 red = (colorRGB >> 16) & 0xFF;
256 green = (colorRGB >> 8) & 0xFF;
257 blue = colorRGB & 0xFF;
258
Fenglin Wu862b56b2018-02-02 10:20:28 +0800259 onMS = state->flashOnMS;
260 offMS = state->flashOffMS;
261
262 if (onMS != 0 && offMS != 0)
263 blink = 1;
264
265 switch (state->flashMode) {
266 case LIGHT_FLASH_HARDWARE:
267 if (!!red)
268 rc = set_rgb_led_hw_blink(LED_RED, blink);
269 if (!!green)
270 rc |= set_rgb_led_hw_blink(LED_GREEN, blink);
271 if (!!blue)
272 rc |= set_rgb_led_hw_blink(LED_BLUE, blink);
273 /* fallback to timed blinking if breath is not supported */
274 if (rc == 0)
275 break;
276 case LIGHT_FLASH_TIMED:
277 if (!!red)
278 rc = set_rgb_led_timer_trigger(LED_RED, onMS, offMS);
279 if (!!green)
280 rc |= set_rgb_led_timer_trigger(LED_GREEN, onMS, offMS);
281 if (!!blue)
282 rc |= set_rgb_led_timer_trigger(LED_BLUE, onMS, offMS);
283 /* fallback to constant on if timed blinking is not supported */
284 if (rc == 0)
285 break;
286 case LIGHT_FLASH_NONE:
287 default:
288 rc = set_rgb_led_brightness(LED_RED, red);
289 rc |= set_rgb_led_brightness(LED_GREEN, green);
290 rc |= set_rgb_led_brightness(LED_BLUE, blue);
291 break;
Sungmin Choib5148892012-07-02 17:00:07 -0700292 }
293
Fenglin Wu862b56b2018-02-02 10:20:28 +0800294 ALOGD("set_speaker_light_locked mode=%d, colorRGB=%08X, onMS=%d, offMS=%d, rc=%d\n",
295 state->flashMode, colorRGB, onMS, offMS, rc);
Sungmin Choib5148892012-07-02 17:00:07 -0700296
Fenglin Wu862b56b2018-02-02 10:20:28 +0800297 return rc;
Sungmin Choib5148892012-07-02 17:00:07 -0700298}
299
300static void
301handle_speaker_battery_locked(struct light_device_t* dev)
302{
303 if (is_lit(&g_battery)) {
304 set_speaker_light_locked(dev, &g_battery);
305 } else {
306 set_speaker_light_locked(dev, &g_notification);
307 }
308}
309
310static int
Mao Lia4e053a2014-06-16 23:05:17 +0530311set_light_battery(struct light_device_t* dev,
312 struct light_state_t const* state)
313{
314 pthread_mutex_lock(&g_lock);
315 g_battery = *state;
316 handle_speaker_battery_locked(dev);
317 pthread_mutex_unlock(&g_lock);
318 return 0;
319}
320
321static int
Sungmin Choib5148892012-07-02 17:00:07 -0700322set_light_notifications(struct light_device_t* dev,
323 struct light_state_t const* state)
324{
325 pthread_mutex_lock(&g_lock);
326 g_notification = *state;
327 handle_speaker_battery_locked(dev);
328 pthread_mutex_unlock(&g_lock);
329 return 0;
330}
331
332static int
333set_light_attention(struct light_device_t* dev,
334 struct light_state_t const* state)
335{
336 pthread_mutex_lock(&g_lock);
337 if (state->flashMode == LIGHT_FLASH_HARDWARE) {
338 g_attention = state->flashOnMS;
339 } else if (state->flashMode == LIGHT_FLASH_NONE) {
340 g_attention = 0;
341 }
342 handle_speaker_battery_locked(dev);
343 pthread_mutex_unlock(&g_lock);
344 return 0;
345}
346
Arun Kumar K.Redd99982017-08-23 08:09:06 +0530347static int
348set_light_buttons(struct light_device_t* dev,
349 struct light_state_t const* state)
350{
351 int err = 0;
352 if(!dev) {
353 return -1;
354 }
355 pthread_mutex_lock(&g_lock);
356 err = write_int(BUTTON_FILE, state->color & 0xFF);
357 pthread_mutex_unlock(&g_lock);
358 return err;
359}
360
Sungmin Choib5148892012-07-02 17:00:07 -0700361/** Close the lights device */
362static int
363close_lights(struct light_device_t *dev)
364{
365 if (dev) {
366 free(dev);
367 }
368 return 0;
369}
370
371
372/******************************************************************************/
373
374/**
375 * module methods
376 */
377
378/** Open a new instance of a lights device using name */
379static int open_lights(const struct hw_module_t* module, char const* name,
380 struct hw_device_t** device)
381{
382 int (*set_light)(struct light_device_t* dev,
383 struct light_state_t const* state);
384
Xu Yang586c6d52016-09-19 17:54:16 +0800385 if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) {
386 char property[PROPERTY_VALUE_MAX];
387 property_get("persist.extend.brightness", property, "0");
388
389 if(!(strncmp(property, "1", PROPERTY_VALUE_MAX)) ||
390 !(strncmp(property, "true", PROPERTY_VALUE_MAX))) {
391 property_get("persist.display.max_brightness", property, "255");
392 g_brightness_max = atoi(property);
393 set_brightness_ext_init();
394 set_light = set_light_backlight_ext;
Pullakavi Srinivasda0b3252017-10-12 17:50:41 +0530395 } else {
396 g_has_persistence_node = !access(PERSISTENCE_FILE, F_OK);
Xu Yang586c6d52016-09-19 17:54:16 +0800397 set_light = set_light_backlight;
Pullakavi Srinivasda0b3252017-10-12 17:50:41 +0530398 }
Xu Yang586c6d52016-09-19 17:54:16 +0800399 } else if (0 == strcmp(LIGHT_ID_BATTERY, name))
Mao Lia4e053a2014-06-16 23:05:17 +0530400 set_light = set_light_battery;
Sungmin Choib5148892012-07-02 17:00:07 -0700401 else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name))
402 set_light = set_light_notifications;
Arun Kumar K.R9ae5a342017-08-23 13:46:03 +0530403 else if (0 == strcmp(LIGHT_ID_BUTTONS, name)) {
404 if (!access(BUTTON_FILE, F_OK)) {
405 // enable light button when the file is present
406 set_light = set_light_buttons;
407 } else {
408 return -EINVAL;
409 }
410 }
Sungmin Choib5148892012-07-02 17:00:07 -0700411 else if (0 == strcmp(LIGHT_ID_ATTENTION, name))
412 set_light = set_light_attention;
413 else
414 return -EINVAL;
415
416 pthread_once(&g_init, init_globals);
417
418 struct light_device_t *dev = malloc(sizeof(struct light_device_t));
Manoj Kumar AVM001b3092014-04-29 22:08:51 -0700419
420 if(!dev)
421 return -ENOMEM;
422
Sungmin Choib5148892012-07-02 17:00:07 -0700423 memset(dev, 0, sizeof(*dev));
424
425 dev->common.tag = HARDWARE_DEVICE_TAG;
Sathish Ambley4342f272017-01-04 10:57:05 -0800426 dev->common.version = LIGHTS_DEVICE_API_VERSION_2_0;
Sungmin Choib5148892012-07-02 17:00:07 -0700427 dev->common.module = (struct hw_module_t*)module;
428 dev->common.close = (int (*)(struct hw_device_t*))close_lights;
429 dev->set_light = set_light;
430
431 *device = (struct hw_device_t*)dev;
432 return 0;
433}
434
435static struct hw_module_methods_t lights_module_methods = {
436 .open = open_lights,
437};
438
439/*
440 * The lights Module
441 */
442struct hw_module_t HAL_MODULE_INFO_SYM = {
443 .tag = HARDWARE_MODULE_TAG,
444 .version_major = 1,
445 .version_minor = 0,
446 .id = LIGHTS_HARDWARE_MODULE_ID,
447 .name = "lights Module",
448 .author = "Google, Inc.",
449 .methods = &lights_module_methods,
450};