blob: 115b98c0021d8d4f0c3c033e3ad816f8025edaaf [file] [log] [blame]
Sungmin Choib5148892012-07-02 17:00:07 -07001/*
Sathish Ambley4342f272017-01-04 10:57:05 -08002 * Copyright (C) 2014, 2017 The Linux Foundation. All rights reserved.
3 * 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
22#include <cutils/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
53char const*const RED_LED_FILE
54 = "/sys/class/leds/red/brightness";
55
56char const*const GREEN_LED_FILE
57 = "/sys/class/leds/green/brightness";
58
59char const*const BLUE_LED_FILE
60 = "/sys/class/leds/blue/brightness";
61
62char const*const LCD_FILE
63 = "/sys/class/leds/lcd-backlight/brightness";
64
Saurabh Shah8b021cf2017-03-14 12:16:43 -070065char const*const LCD_FILE2
66 = "/sys/class/backlight/panel0-backlight/brightness";
67
HuiWangf73bc572013-07-31 10:48:36 +080068char const*const BUTTON_FILE
69 = "/sys/class/leds/button-backlight/brightness";
70
samin.ryud57c7d52012-08-03 23:59:41 +090071char const*const RED_BLINK_FILE
Yulian Shandorov784d7392013-06-20 04:28:59 -070072 = "/sys/class/leds/red/blink";
samin.ryud57c7d52012-08-03 23:59:41 +090073
Ameya Thakur192c8ac2013-08-12 16:50:01 -070074char const*const GREEN_BLINK_FILE
75 = "/sys/class/leds/green/blink";
76
77char const*const BLUE_BLINK_FILE
78 = "/sys/class/leds/blue/blink";
79
Sathish Ambley4342f272017-01-04 10:57:05 -080080char const*const PERSISTENCE_FILE
81 = "/sys/class/graphics/fb0/msm_fb_persist_mode";
82
Sungmin Choib5148892012-07-02 17:00:07 -070083/**
84 * device methods
85 */
86
87void init_globals(void)
88{
89 // init the mutex
90 pthread_mutex_init(&g_lock, NULL);
91}
92
93static int
94write_int(char const* path, int value)
95{
96 int fd;
97 static int already_warned = 0;
98
99 fd = open(path, O_RDWR);
100 if (fd >= 0) {
101 char buffer[20];
Manoj Kumar AVM001b3092014-04-29 22:08:51 -0700102 int bytes = snprintf(buffer, sizeof(buffer), "%d\n", value);
Dileep Kumar Reddibf333c72014-02-25 14:32:51 +0530103 ssize_t amt = write(fd, buffer, (size_t)bytes);
Sungmin Choib5148892012-07-02 17:00:07 -0700104 close(fd);
105 return amt == -1 ? -errno : 0;
106 } else {
107 if (already_warned == 0) {
108 ALOGE("write_int failed to open %s\n", path);
109 already_warned = 1;
110 }
111 return -errno;
112 }
113}
114
115static int
116is_lit(struct light_state_t const* state)
117{
118 return state->color & 0x00ffffff;
119}
120
121static int
122rgb_to_brightness(struct light_state_t const* state)
123{
124 int color = state->color & 0x00ffffff;
125 return ((77*((color>>16)&0x00ff))
126 + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8;
127}
128
129static int
130set_light_backlight(struct light_device_t* dev,
131 struct light_state_t const* state)
132{
133 int err = 0;
134 int brightness = rgb_to_brightness(state);
Sathish Ambley4342f272017-01-04 10:57:05 -0800135 unsigned int lpEnabled =
136 state->brightnessMode == BRIGHTNESS_MODE_LOW_PERSISTENCE;
Arun Kumar K.R0efad602014-01-21 21:32:36 -0800137 if(!dev) {
138 return -1;
139 }
Sathish Ambley4342f272017-01-04 10:57:05 -0800140
Sungmin Choib5148892012-07-02 17:00:07 -0700141 pthread_mutex_lock(&g_lock);
Sathish Ambley4342f272017-01-04 10:57:05 -0800142 // Toggle low persistence mode state
Pullakavi Srinivasda0b3252017-10-12 17:50:41 +0530143 bool persistence_mode = ((g_last_backlight_mode != state->brightnessMode && lpEnabled) ||
144 (!lpEnabled &&
145 g_last_backlight_mode == BRIGHTNESS_MODE_LOW_PERSISTENCE));
146 bool cannot_handle_persistence = !g_has_persistence_node && persistence_mode;
147 if (g_has_persistence_node) {
148 if (persistence_mode) {
149 if ((err = write_int(PERSISTENCE_FILE, lpEnabled)) != 0) {
150 ALOGE("%s: Failed to write to %s: %s\n", __FUNCTION__,
151 PERSISTENCE_FILE, strerror(errno));
152 }
153 if (lpEnabled != 0) {
154 brightness = DEFAULT_LOW_PERSISTENCE_MODE_BRIGHTNESS;
155 }
Sathish Ambley4342f272017-01-04 10:57:05 -0800156 }
Pullakavi Srinivasda0b3252017-10-12 17:50:41 +0530157 g_last_backlight_mode = state->brightnessMode;
Sathish Ambley4342f272017-01-04 10:57:05 -0800158 }
159
Sathish Ambley4342f272017-01-04 10:57:05 -0800160 if (!err) {
Saurabh Shah8b021cf2017-03-14 12:16:43 -0700161 if (!access(LCD_FILE, F_OK)) {
162 err = write_int(LCD_FILE, brightness);
163 } else {
164 err = write_int(LCD_FILE2, brightness);
165 }
Sathish Ambley4342f272017-01-04 10:57:05 -0800166 }
167
Sungmin Choib5148892012-07-02 17:00:07 -0700168 pthread_mutex_unlock(&g_lock);
Pullakavi Srinivasda0b3252017-10-12 17:50:41 +0530169 return cannot_handle_persistence ? -ENOSYS : err;
Sungmin Choib5148892012-07-02 17:00:07 -0700170}
171
172static int
Xu Yang586c6d52016-09-19 17:54:16 +0800173set_light_backlight_ext(struct light_device_t* dev,
174 struct light_state_t const* state)
175{
176 int err = 0;
177
178 if(!dev) {
179 return -1;
180 }
181
182 int brightness = state->color & 0x00ffffff;
183 pthread_mutex_lock(&g_lock);
184
185 if (brightness >= 0 && brightness <= g_brightness_max) {
186 set_brightness_ext_level(brightness);
187 }
188
189 pthread_mutex_unlock(&g_lock);
190
191 return err;
192}
193
194static int
Sungmin Choib5148892012-07-02 17:00:07 -0700195set_speaker_light_locked(struct light_device_t* dev,
196 struct light_state_t const* state)
197{
Arun Kumar K.R0efad602014-01-21 21:32:36 -0800198 int red, green, blue;
Yulian Shandorov784d7392013-06-20 04:28:59 -0700199 int blink;
Sungmin Choib5148892012-07-02 17:00:07 -0700200 int onMS, offMS;
201 unsigned int colorRGB;
202
Arun Kumar K.R0efad602014-01-21 21:32:36 -0800203 if(!dev) {
204 return -1;
205 }
206
Sungmin Choib5148892012-07-02 17:00:07 -0700207 switch (state->flashMode) {
208 case LIGHT_FLASH_TIMED:
209 onMS = state->flashOnMS;
210 offMS = state->flashOffMS;
211 break;
212 case LIGHT_FLASH_NONE:
213 default:
214 onMS = 0;
215 offMS = 0;
216 break;
217 }
218
219 colorRGB = state->color;
220
221#if 0
samin.ryud57c7d52012-08-03 23:59:41 +0900222 ALOGD("set_speaker_light_locked mode %d, colorRGB=%08X, onMS=%d, offMS=%d\n",
223 state->flashMode, colorRGB, onMS, offMS);
Sungmin Choib5148892012-07-02 17:00:07 -0700224#endif
225
226 red = (colorRGB >> 16) & 0xFF;
227 green = (colorRGB >> 8) & 0xFF;
228 blue = colorRGB & 0xFF;
229
Sungmin Choib5148892012-07-02 17:00:07 -0700230 if (onMS > 0 && offMS > 0) {
Ashay Jaiswal26f0b532015-05-03 17:58:58 +0530231 /*
232 * if ON time == OFF time
233 * use blink mode 2
234 * else
235 * use blink mode 1
236 */
237 if (onMS == offMS)
238 blink = 2;
239 else
240 blink = 1;
Sungmin Choib5148892012-07-02 17:00:07 -0700241 } else {
242 blink = 0;
Sungmin Choib5148892012-07-02 17:00:07 -0700243 }
244
245 if (blink) {
Mao Li728ee0b2014-07-01 23:06:16 +0800246 if (red) {
247 if (write_int(RED_BLINK_FILE, blink))
248 write_int(RED_LED_FILE, 0);
Xu Yang586c6d52016-09-19 17:54:16 +0800249 }
Mao Li728ee0b2014-07-01 23:06:16 +0800250 if (green) {
251 if (write_int(GREEN_BLINK_FILE, blink))
252 write_int(GREEN_LED_FILE, 0);
Xu Yang586c6d52016-09-19 17:54:16 +0800253 }
Mao Li728ee0b2014-07-01 23:06:16 +0800254 if (blue) {
255 if (write_int(BLUE_BLINK_FILE, blink))
256 write_int(BLUE_LED_FILE, 0);
Xu Yang586c6d52016-09-19 17:54:16 +0800257 }
Yulian Shandorov784d7392013-06-20 04:28:59 -0700258 } else {
259 write_int(RED_LED_FILE, red);
260 write_int(GREEN_LED_FILE, green);
261 write_int(BLUE_LED_FILE, blue);
Sungmin Choib5148892012-07-02 17:00:07 -0700262 }
Sungmin Choib5148892012-07-02 17:00:07 -0700263
264 return 0;
265}
266
267static void
268handle_speaker_battery_locked(struct light_device_t* dev)
269{
270 if (is_lit(&g_battery)) {
271 set_speaker_light_locked(dev, &g_battery);
272 } else {
273 set_speaker_light_locked(dev, &g_notification);
274 }
275}
276
277static int
Mao Lia4e053a2014-06-16 23:05:17 +0530278set_light_battery(struct light_device_t* dev,
279 struct light_state_t const* state)
280{
281 pthread_mutex_lock(&g_lock);
282 g_battery = *state;
283 handle_speaker_battery_locked(dev);
284 pthread_mutex_unlock(&g_lock);
285 return 0;
286}
287
288static int
Sungmin Choib5148892012-07-02 17:00:07 -0700289set_light_notifications(struct light_device_t* dev,
290 struct light_state_t const* state)
291{
292 pthread_mutex_lock(&g_lock);
293 g_notification = *state;
294 handle_speaker_battery_locked(dev);
295 pthread_mutex_unlock(&g_lock);
296 return 0;
297}
298
299static int
300set_light_attention(struct light_device_t* dev,
301 struct light_state_t const* state)
302{
303 pthread_mutex_lock(&g_lock);
304 if (state->flashMode == LIGHT_FLASH_HARDWARE) {
305 g_attention = state->flashOnMS;
306 } else if (state->flashMode == LIGHT_FLASH_NONE) {
307 g_attention = 0;
308 }
309 handle_speaker_battery_locked(dev);
310 pthread_mutex_unlock(&g_lock);
311 return 0;
312}
313
Arun Kumar K.Redd99982017-08-23 08:09:06 +0530314static int
315set_light_buttons(struct light_device_t* dev,
316 struct light_state_t const* state)
317{
318 int err = 0;
319 if(!dev) {
320 return -1;
321 }
322 pthread_mutex_lock(&g_lock);
323 err = write_int(BUTTON_FILE, state->color & 0xFF);
324 pthread_mutex_unlock(&g_lock);
325 return err;
326}
327
Sungmin Choib5148892012-07-02 17:00:07 -0700328/** Close the lights device */
329static int
330close_lights(struct light_device_t *dev)
331{
332 if (dev) {
333 free(dev);
334 }
335 return 0;
336}
337
338
339/******************************************************************************/
340
341/**
342 * module methods
343 */
344
345/** Open a new instance of a lights device using name */
346static int open_lights(const struct hw_module_t* module, char const* name,
347 struct hw_device_t** device)
348{
349 int (*set_light)(struct light_device_t* dev,
350 struct light_state_t const* state);
351
Xu Yang586c6d52016-09-19 17:54:16 +0800352 if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) {
353 char property[PROPERTY_VALUE_MAX];
354 property_get("persist.extend.brightness", property, "0");
355
356 if(!(strncmp(property, "1", PROPERTY_VALUE_MAX)) ||
357 !(strncmp(property, "true", PROPERTY_VALUE_MAX))) {
358 property_get("persist.display.max_brightness", property, "255");
359 g_brightness_max = atoi(property);
360 set_brightness_ext_init();
361 set_light = set_light_backlight_ext;
Pullakavi Srinivasda0b3252017-10-12 17:50:41 +0530362 } else {
363 g_has_persistence_node = !access(PERSISTENCE_FILE, F_OK);
Xu Yang586c6d52016-09-19 17:54:16 +0800364 set_light = set_light_backlight;
Pullakavi Srinivasda0b3252017-10-12 17:50:41 +0530365 }
Xu Yang586c6d52016-09-19 17:54:16 +0800366 } else if (0 == strcmp(LIGHT_ID_BATTERY, name))
Mao Lia4e053a2014-06-16 23:05:17 +0530367 set_light = set_light_battery;
Sungmin Choib5148892012-07-02 17:00:07 -0700368 else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name))
369 set_light = set_light_notifications;
Arun Kumar K.R9ae5a342017-08-23 13:46:03 +0530370 else if (0 == strcmp(LIGHT_ID_BUTTONS, name)) {
371 if (!access(BUTTON_FILE, F_OK)) {
372 // enable light button when the file is present
373 set_light = set_light_buttons;
374 } else {
375 return -EINVAL;
376 }
377 }
Sungmin Choib5148892012-07-02 17:00:07 -0700378 else if (0 == strcmp(LIGHT_ID_ATTENTION, name))
379 set_light = set_light_attention;
380 else
381 return -EINVAL;
382
383 pthread_once(&g_init, init_globals);
384
385 struct light_device_t *dev = malloc(sizeof(struct light_device_t));
Manoj Kumar AVM001b3092014-04-29 22:08:51 -0700386
387 if(!dev)
388 return -ENOMEM;
389
Sungmin Choib5148892012-07-02 17:00:07 -0700390 memset(dev, 0, sizeof(*dev));
391
392 dev->common.tag = HARDWARE_DEVICE_TAG;
Sathish Ambley4342f272017-01-04 10:57:05 -0800393 dev->common.version = LIGHTS_DEVICE_API_VERSION_2_0;
Sungmin Choib5148892012-07-02 17:00:07 -0700394 dev->common.module = (struct hw_module_t*)module;
395 dev->common.close = (int (*)(struct hw_device_t*))close_lights;
396 dev->set_light = set_light;
397
398 *device = (struct hw_device_t*)dev;
399 return 0;
400}
401
402static struct hw_module_methods_t lights_module_methods = {
403 .open = open_lights,
404};
405
406/*
407 * The lights Module
408 */
409struct hw_module_t HAL_MODULE_INFO_SYM = {
410 .tag = HARDWARE_MODULE_TAG,
411 .version_major = 1,
412 .version_minor = 0,
413 .id = LIGHTS_HARDWARE_MODULE_ID,
414 .name = "lights Module",
415 .author = "Google, Inc.",
416 .methods = &lights_module_methods,
417};