blob: f74fb5429a37014ff3c291eff29290f954eab017 [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>
36
Sathish Ambley4342f272017-01-04 10:57:05 -080037#ifndef DEFAULT_LOW_PERSISTENCE_MODE_BRIGHTNESS
38#define DEFAULT_LOW_PERSISTENCE_MODE_BRIGHTNESS 0x80
39#endif
40
Sungmin Choib5148892012-07-02 17:00:07 -070041/******************************************************************************/
42
43static pthread_once_t g_init = PTHREAD_ONCE_INIT;
44static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
45static struct light_state_t g_notification;
46static struct light_state_t g_battery;
Sathish Ambley4342f272017-01-04 10:57:05 -080047static int g_last_backlight_mode = BRIGHTNESS_MODE_USER;
Sungmin Choib5148892012-07-02 17:00:07 -070048static int g_attention = 0;
Pullakavi Srinivasda0b3252017-10-12 17:50:41 +053049static bool g_has_persistence_node = false;
Sungmin Choib5148892012-07-02 17:00:07 -070050
Sungmin Choib5148892012-07-02 17:00:07 -070051char const*const LCD_FILE
52 = "/sys/class/leds/lcd-backlight/brightness";
53
Saurabh Shah8b021cf2017-03-14 12:16:43 -070054char const*const LCD_FILE2
55 = "/sys/class/backlight/panel0-backlight/brightness";
56
HuiWangf73bc572013-07-31 10:48:36 +080057char const*const BUTTON_FILE
58 = "/sys/class/leds/button-backlight/brightness";
59
Sathish Ambley4342f272017-01-04 10:57:05 -080060char const*const PERSISTENCE_FILE
61 = "/sys/class/graphics/fb0/msm_fb_persist_mode";
62
Fenglin Wu862b56b2018-02-02 10:20:28 +080063enum rgb_led {
64 LED_RED = 0,
65 LED_GREEN,
66 LED_BLUE,
67};
68
69char *led_names[] = {
70 "red",
71 "green",
72 "blue",
73};
Sungmin Choib5148892012-07-02 17:00:07 -070074/**
75 * device methods
76 */
77
78void init_globals(void)
79{
80 // init the mutex
81 pthread_mutex_init(&g_lock, NULL);
82}
83
Fenglin Wu862b56b2018-02-02 10:20:28 +080084static int write_int(char const* path, int value)
Sungmin Choib5148892012-07-02 17:00:07 -070085{
86 int fd;
87 static int already_warned = 0;
88
89 fd = open(path, O_RDWR);
90 if (fd >= 0) {
91 char buffer[20];
Manoj Kumar AVM001b3092014-04-29 22:08:51 -070092 int bytes = snprintf(buffer, sizeof(buffer), "%d\n", value);
Dileep Kumar Reddibf333c72014-02-25 14:32:51 +053093 ssize_t amt = write(fd, buffer, (size_t)bytes);
Sungmin Choib5148892012-07-02 17:00:07 -070094 close(fd);
95 return amt == -1 ? -errno : 0;
96 } else {
97 if (already_warned == 0) {
Fenglin Wu862b56b2018-02-02 10:20:28 +080098 ALOGE("write_int failed to open %s, errno = %d\n", path, errno);
Sungmin Choib5148892012-07-02 17:00:07 -070099 already_warned = 1;
100 }
101 return -errno;
102 }
103}
104
Fenglin Wu862b56b2018-02-02 10:20:28 +0800105static bool file_exists(const char *file)
106{
107 int fd;
108
109 fd = open(file, O_RDWR);
110 if (fd < 0) {
111 ALOGE("failed to open %s, errno=%d\n", file, errno);
112 return false;
113 }
114
115 close(fd);
116 return true;
117}
118
Sungmin Choib5148892012-07-02 17:00:07 -0700119static int
120is_lit(struct light_state_t const* state)
121{
122 return state->color & 0x00ffffff;
123}
124
125static int
126rgb_to_brightness(struct light_state_t const* state)
127{
128 int color = state->color & 0x00ffffff;
129 return ((77*((color>>16)&0x00ff))
130 + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8;
131}
132
133static int
134set_light_backlight(struct light_device_t* dev,
135 struct light_state_t const* state)
136{
137 int err = 0;
138 int brightness = rgb_to_brightness(state);
Sathish Ambley4342f272017-01-04 10:57:05 -0800139 unsigned int lpEnabled =
140 state->brightnessMode == BRIGHTNESS_MODE_LOW_PERSISTENCE;
Arun Kumar K.R0efad602014-01-21 21:32:36 -0800141 if(!dev) {
142 return -1;
143 }
Sathish Ambley4342f272017-01-04 10:57:05 -0800144
Sungmin Choib5148892012-07-02 17:00:07 -0700145 pthread_mutex_lock(&g_lock);
Sathish Ambley4342f272017-01-04 10:57:05 -0800146 // Toggle low persistence mode state
Pullakavi Srinivasda0b3252017-10-12 17:50:41 +0530147 bool persistence_mode = ((g_last_backlight_mode != state->brightnessMode && lpEnabled) ||
148 (!lpEnabled &&
149 g_last_backlight_mode == BRIGHTNESS_MODE_LOW_PERSISTENCE));
150 bool cannot_handle_persistence = !g_has_persistence_node && persistence_mode;
151 if (g_has_persistence_node) {
152 if (persistence_mode) {
153 if ((err = write_int(PERSISTENCE_FILE, lpEnabled)) != 0) {
154 ALOGE("%s: Failed to write to %s: %s\n", __FUNCTION__,
155 PERSISTENCE_FILE, strerror(errno));
156 }
157 if (lpEnabled != 0) {
158 brightness = DEFAULT_LOW_PERSISTENCE_MODE_BRIGHTNESS;
159 }
Sathish Ambley4342f272017-01-04 10:57:05 -0800160 }
Pullakavi Srinivasda0b3252017-10-12 17:50:41 +0530161 g_last_backlight_mode = state->brightnessMode;
Sathish Ambley4342f272017-01-04 10:57:05 -0800162 }
163
Sathish Ambley4342f272017-01-04 10:57:05 -0800164 if (!err) {
Saurabh Shah8b021cf2017-03-14 12:16:43 -0700165 if (!access(LCD_FILE, F_OK)) {
166 err = write_int(LCD_FILE, brightness);
167 } else {
168 err = write_int(LCD_FILE2, brightness);
169 }
Sathish Ambley4342f272017-01-04 10:57:05 -0800170 }
171
Sungmin Choib5148892012-07-02 17:00:07 -0700172 pthread_mutex_unlock(&g_lock);
Pullakavi Srinivasda0b3252017-10-12 17:50:41 +0530173 return cannot_handle_persistence ? -ENOSYS : err;
Sungmin Choib5148892012-07-02 17:00:07 -0700174}
175
Fenglin Wu862b56b2018-02-02 10:20:28 +0800176static int set_rgb_led_brightness(enum rgb_led led, int brightness)
177{
178 char file[48];
179
180 snprintf(file, sizeof(file), "/sys/class/leds/%s/brightness", led_names[led]);
181 return write_int(file, brightness);
182}
183
184static int set_rgb_led_timer_trigger(enum rgb_led led, int onMS, int offMS)
185{
186 char file[48];
187 int rc;
188
189 snprintf(file, sizeof(file), "/sys/class/leds/%s/delay_off", led_names[led]);
190 rc = write_int(file, offMS);
191 if (rc < 0)
192 goto out;
193
194 snprintf(file, sizeof(file), "/sys/class/leds/%s/delay_on", led_names[led]);
195 rc = write_int(file, onMS);
196 if (rc < 0)
197 goto out;
198
199 return 0;
200out:
201 ALOGD("%s doesn't support timer trigger\n", led_names[led]);
202 return rc;
203}
204
205static int set_rgb_led_hw_blink(enum rgb_led led, int blink)
206{
207 char file[48];
208
209 snprintf(file, sizeof(file), "/sys/class/leds/%s/breath", led_names[led]);
210 if (!file_exists(file))
211 snprintf(file, sizeof(file), "/sys/class/leds/%s/blink", led_names[led]);
212
213 return write_int(file, blink);
214}
215
Xu Yang586c6d52016-09-19 17:54:16 +0800216static int
Sungmin Choib5148892012-07-02 17:00:07 -0700217set_speaker_light_locked(struct light_device_t* dev,
218 struct light_state_t const* state)
219{
Arun Kumar K.R0efad602014-01-21 21:32:36 -0800220 int red, green, blue;
Sungmin Choib5148892012-07-02 17:00:07 -0700221 int onMS, offMS;
222 unsigned int colorRGB;
Fenglin Wu862b56b2018-02-02 10:20:28 +0800223 int blink = 0;
224 int rc = 0;
Sungmin Choib5148892012-07-02 17:00:07 -0700225
Arun Kumar K.R0efad602014-01-21 21:32:36 -0800226 if(!dev) {
227 return -1;
228 }
229
Sungmin Choib5148892012-07-02 17:00:07 -0700230 colorRGB = state->color;
Sungmin Choib5148892012-07-02 17:00:07 -0700231 red = (colorRGB >> 16) & 0xFF;
232 green = (colorRGB >> 8) & 0xFF;
233 blue = colorRGB & 0xFF;
234
Fenglin Wu862b56b2018-02-02 10:20:28 +0800235 onMS = state->flashOnMS;
236 offMS = state->flashOffMS;
237
238 if (onMS != 0 && offMS != 0)
239 blink = 1;
240
241 switch (state->flashMode) {
242 case LIGHT_FLASH_HARDWARE:
243 if (!!red)
244 rc = set_rgb_led_hw_blink(LED_RED, blink);
245 if (!!green)
246 rc |= set_rgb_led_hw_blink(LED_GREEN, blink);
247 if (!!blue)
248 rc |= set_rgb_led_hw_blink(LED_BLUE, blink);
249 /* fallback to timed blinking if breath is not supported */
250 if (rc == 0)
251 break;
252 case LIGHT_FLASH_TIMED:
253 if (!!red)
254 rc = set_rgb_led_timer_trigger(LED_RED, onMS, offMS);
255 if (!!green)
256 rc |= set_rgb_led_timer_trigger(LED_GREEN, onMS, offMS);
257 if (!!blue)
258 rc |= set_rgb_led_timer_trigger(LED_BLUE, onMS, offMS);
259 /* fallback to constant on if timed blinking is not supported */
260 if (rc == 0)
261 break;
262 case LIGHT_FLASH_NONE:
263 default:
264 rc = set_rgb_led_brightness(LED_RED, red);
265 rc |= set_rgb_led_brightness(LED_GREEN, green);
266 rc |= set_rgb_led_brightness(LED_BLUE, blue);
267 break;
Sungmin Choib5148892012-07-02 17:00:07 -0700268 }
269
Fenglin Wu862b56b2018-02-02 10:20:28 +0800270 ALOGD("set_speaker_light_locked mode=%d, colorRGB=%08X, onMS=%d, offMS=%d, rc=%d\n",
271 state->flashMode, colorRGB, onMS, offMS, rc);
Sungmin Choib5148892012-07-02 17:00:07 -0700272
Fenglin Wu862b56b2018-02-02 10:20:28 +0800273 return rc;
Sungmin Choib5148892012-07-02 17:00:07 -0700274}
275
276static void
277handle_speaker_battery_locked(struct light_device_t* dev)
278{
279 if (is_lit(&g_battery)) {
280 set_speaker_light_locked(dev, &g_battery);
281 } else {
282 set_speaker_light_locked(dev, &g_notification);
283 }
284}
285
286static int
Mao Lia4e053a2014-06-16 23:05:17 +0530287set_light_battery(struct light_device_t* dev,
288 struct light_state_t const* state)
289{
290 pthread_mutex_lock(&g_lock);
291 g_battery = *state;
292 handle_speaker_battery_locked(dev);
293 pthread_mutex_unlock(&g_lock);
294 return 0;
295}
296
297static int
Sungmin Choib5148892012-07-02 17:00:07 -0700298set_light_notifications(struct light_device_t* dev,
299 struct light_state_t const* state)
300{
301 pthread_mutex_lock(&g_lock);
302 g_notification = *state;
303 handle_speaker_battery_locked(dev);
304 pthread_mutex_unlock(&g_lock);
305 return 0;
306}
307
308static int
309set_light_attention(struct light_device_t* dev,
310 struct light_state_t const* state)
311{
312 pthread_mutex_lock(&g_lock);
313 if (state->flashMode == LIGHT_FLASH_HARDWARE) {
314 g_attention = state->flashOnMS;
315 } else if (state->flashMode == LIGHT_FLASH_NONE) {
316 g_attention = 0;
317 }
318 handle_speaker_battery_locked(dev);
319 pthread_mutex_unlock(&g_lock);
320 return 0;
321}
322
Arun Kumar K.Redd99982017-08-23 08:09:06 +0530323static int
324set_light_buttons(struct light_device_t* dev,
325 struct light_state_t const* state)
326{
327 int err = 0;
328 if(!dev) {
329 return -1;
330 }
331 pthread_mutex_lock(&g_lock);
332 err = write_int(BUTTON_FILE, state->color & 0xFF);
333 pthread_mutex_unlock(&g_lock);
334 return err;
335}
336
Sungmin Choib5148892012-07-02 17:00:07 -0700337/** Close the lights device */
338static int
339close_lights(struct light_device_t *dev)
340{
341 if (dev) {
342 free(dev);
343 }
344 return 0;
345}
346
347
348/******************************************************************************/
349
350/**
351 * module methods
352 */
353
354/** Open a new instance of a lights device using name */
355static int open_lights(const struct hw_module_t* module, char const* name,
356 struct hw_device_t** device)
357{
358 int (*set_light)(struct light_device_t* dev,
359 struct light_state_t const* state);
360
Xu Yang586c6d52016-09-19 17:54:16 +0800361 if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) {
Ch Ganesh Kumarae5a42e2018-03-14 16:51:43 +0530362 g_has_persistence_node = !access(PERSISTENCE_FILE, F_OK);
363 set_light = set_light_backlight;
Xu Yang586c6d52016-09-19 17:54:16 +0800364 } else if (0 == strcmp(LIGHT_ID_BATTERY, name))
Mao Lia4e053a2014-06-16 23:05:17 +0530365 set_light = set_light_battery;
Sungmin Choib5148892012-07-02 17:00:07 -0700366 else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name))
367 set_light = set_light_notifications;
Arun Kumar K.R9ae5a342017-08-23 13:46:03 +0530368 else if (0 == strcmp(LIGHT_ID_BUTTONS, name)) {
369 if (!access(BUTTON_FILE, F_OK)) {
370 // enable light button when the file is present
371 set_light = set_light_buttons;
372 } else {
373 return -EINVAL;
374 }
375 }
Sungmin Choib5148892012-07-02 17:00:07 -0700376 else if (0 == strcmp(LIGHT_ID_ATTENTION, name))
377 set_light = set_light_attention;
378 else
379 return -EINVAL;
380
381 pthread_once(&g_init, init_globals);
382
383 struct light_device_t *dev = malloc(sizeof(struct light_device_t));
Manoj Kumar AVM001b3092014-04-29 22:08:51 -0700384
385 if(!dev)
386 return -ENOMEM;
387
Sungmin Choib5148892012-07-02 17:00:07 -0700388 memset(dev, 0, sizeof(*dev));
389
390 dev->common.tag = HARDWARE_DEVICE_TAG;
Sathish Ambley4342f272017-01-04 10:57:05 -0800391 dev->common.version = LIGHTS_DEVICE_API_VERSION_2_0;
Sungmin Choib5148892012-07-02 17:00:07 -0700392 dev->common.module = (struct hw_module_t*)module;
393 dev->common.close = (int (*)(struct hw_device_t*))close_lights;
394 dev->set_light = set_light;
395
396 *device = (struct hw_device_t*)dev;
397 return 0;
398}
399
400static struct hw_module_methods_t lights_module_methods = {
401 .open = open_lights,
402};
403
404/*
405 * The lights Module
406 */
407struct hw_module_t HAL_MODULE_INFO_SYM = {
408 .tag = HARDWARE_MODULE_TAG,
409 .version_major = 1,
410 .version_minor = 0,
411 .id = LIGHTS_HARDWARE_MODULE_ID,
412 .name = "lights Module",
413 .author = "Google, Inc.",
414 .methods = &lights_module_methods,
415};