blob: c9cbaafcfb29766e03d291582c7c0688c6599486 [file] [log] [blame]
Sungmin Choib5148892012-07-02 17:00:07 -07001/*
Ch Ganesh Kumar71f50f32018-03-14 16:51:43 +05302 * 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 Ahmed79dc3b02018-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
yutingshih0e7c4bb2020-04-21 15:56:11 +080041#if defined( ARIMA_DEBUG )
42 #undef ARIMA_DEBUG
43#endif
44 #define ARIMA_DEBUG 0
45
46#if defined( PNX )
47 #undef PNX
48#endif
49#define PNX "[ARIMA]"
50
51#if defined( LIGHT_DBG )
52 #undef LIGHT_DBG
53#endif
54
55#if( ARIMA_DEBUG )
56 #define LIGHT_DBG(fmt,args...) ALOGE(PNX "[%s] " fmt,__FUNCTION__,##args)
57#else
58 #define LIGHT_DBG(fmt,args...) do{}while(0)
59#endif
60
Sungmin Choib5148892012-07-02 17:00:07 -070061/******************************************************************************/
62
63static pthread_once_t g_init = PTHREAD_ONCE_INIT;
64static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
65static struct light_state_t g_notification;
66static struct light_state_t g_battery;
Sathish Ambley4342f272017-01-04 10:57:05 -080067static int g_last_backlight_mode = BRIGHTNESS_MODE_USER;
Sungmin Choib5148892012-07-02 17:00:07 -070068static int g_attention = 0;
69
70char const*const RED_LED_FILE
71 = "/sys/class/leds/red/brightness";
72
73char const*const GREEN_LED_FILE
74 = "/sys/class/leds/green/brightness";
75
76char const*const BLUE_LED_FILE
77 = "/sys/class/leds/blue/brightness";
78
79char const*const LCD_FILE
80 = "/sys/class/leds/lcd-backlight/brightness";
81
Saurabh Shah8b021cf2017-03-14 12:16:43 -070082char const*const LCD_FILE2
83 = "/sys/class/backlight/panel0-backlight/brightness";
84
HuiWangf73bc572013-07-31 10:48:36 +080085char const*const BUTTON_FILE
86 = "/sys/class/leds/button-backlight/brightness";
87
yutingshih0e7c4bb2020-04-21 15:56:11 +080088#if( 1 )
89
90char const*const RED_BLINK_FILE
91 = "/sys/class/leds/red/breath";
92
93char const*const GREEN_BLINK_FILE
94 = "/sys/class/leds/green/breath";
95
96char const*const BLUE_BLINK_FILE
97 = "/sys/class/leds/blue/breath";
98
99#else /* Default */
samin.ryud57c7d52012-08-03 23:59:41 +0900100char const*const RED_BLINK_FILE
Yulian Shandorov784d7392013-06-20 04:28:59 -0700101 = "/sys/class/leds/red/blink";
samin.ryud57c7d52012-08-03 23:59:41 +0900102
Ameya Thakur192c8ac2013-08-12 16:50:01 -0700103char const*const GREEN_BLINK_FILE
104 = "/sys/class/leds/green/blink";
105
106char const*const BLUE_BLINK_FILE
107 = "/sys/class/leds/blue/blink";
108
yutingshih0e7c4bb2020-04-21 15:56:11 +0800109#endif /* End.. */
110
111#if defined( LED_DELAY_FUNC_USAGE )
112 #undef LED_DELAY_FUNC_USAGE
113#endif
114#define LED_DELAY_FUNC_USAGE 0
115
116#if( LED_DELAY_FUNC_USAGE )
117char const*const RED_DELAY_ON_FILE
118 = "/sys/class/leds/red/delay_on";
119
120char const*const RED_DELAY_OFF_FILE
121 = "/sys/class/leds/red/delay_off";
122
123char const*const GREEN_DELAY_ON_FILE
124 = "/sys/class/leds/green/delay_on";
125
126char const*const GREEN_DELAY_OFF_FILE
127 = "/sys/class/leds/green/delay_off";
128
129char const*const BLUE_DELAY_ON_FILE
130 = "/sys/class/leds/blue/delay_on";
131
132char const*const BLUE_DELAY_OFF_FILE
133 = "/sys/class/leds/blue/delay_off";
134
135#define LED_DELAY_MAX_MS 1500
136
137#endif /* End..(LED_DELAY_FUNC_USAGE) */
138
139
Sathish Ambley4342f272017-01-04 10:57:05 -0800140char const*const PERSISTENCE_FILE
141 = "/sys/class/graphics/fb0/msm_fb_persist_mode";
142
Sungmin Choib5148892012-07-02 17:00:07 -0700143/**
144 * device methods
145 */
146
147void init_globals(void)
148{
149 // init the mutex
150 pthread_mutex_init(&g_lock, NULL);
151}
152
153static int
154write_int(char const* path, int value)
155{
156 int fd;
157 static int already_warned = 0;
158
159 fd = open(path, O_RDWR);
160 if (fd >= 0) {
161 char buffer[20];
Manoj Kumar AVM001b3092014-04-29 22:08:51 -0700162 int bytes = snprintf(buffer, sizeof(buffer), "%d\n", value);
Dileep Kumar Reddibf333c72014-02-25 14:32:51 +0530163 ssize_t amt = write(fd, buffer, (size_t)bytes);
Sungmin Choib5148892012-07-02 17:00:07 -0700164 close(fd);
165 return amt == -1 ? -errno : 0;
166 } else {
167 if (already_warned == 0) {
168 ALOGE("write_int failed to open %s\n", path);
169 already_warned = 1;
170 }
171 return -errno;
172 }
173}
174
175static int
176is_lit(struct light_state_t const* state)
177{
178 return state->color & 0x00ffffff;
179}
180
181static int
182rgb_to_brightness(struct light_state_t const* state)
183{
184 int color = state->color & 0x00ffffff;
185 return ((77*((color>>16)&0x00ff))
186 + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8;
187}
188
189static int
190set_light_backlight(struct light_device_t* dev,
191 struct light_state_t const* state)
192{
193 int err = 0;
194 int brightness = rgb_to_brightness(state);
Sathish Ambley4342f272017-01-04 10:57:05 -0800195 unsigned int lpEnabled =
196 state->brightnessMode == BRIGHTNESS_MODE_LOW_PERSISTENCE;
Arun Kumar K.R0efad602014-01-21 21:32:36 -0800197 if(!dev) {
198 return -1;
199 }
Sathish Ambley4342f272017-01-04 10:57:05 -0800200
Sungmin Choib5148892012-07-02 17:00:07 -0700201 pthread_mutex_lock(&g_lock);
Sathish Ambley4342f272017-01-04 10:57:05 -0800202 // Toggle low persistence mode state
203 if ((g_last_backlight_mode != state->brightnessMode && lpEnabled) ||
204 (!lpEnabled &&
205 g_last_backlight_mode == BRIGHTNESS_MODE_LOW_PERSISTENCE)) {
206 if ((err = write_int(PERSISTENCE_FILE, lpEnabled)) != 0) {
207 ALOGE("%s: Failed to write to %s: %s\n", __FUNCTION__,
208 PERSISTENCE_FILE, strerror(errno));
209 }
210 if (lpEnabled != 0) {
211 brightness = DEFAULT_LOW_PERSISTENCE_MODE_BRIGHTNESS;
212 }
213 }
214
215 g_last_backlight_mode = state->brightnessMode;
216
217 if (!err) {
Saurabh Shah8b021cf2017-03-14 12:16:43 -0700218 if (!access(LCD_FILE, F_OK)) {
219 err = write_int(LCD_FILE, brightness);
220 } else {
221 err = write_int(LCD_FILE2, brightness);
222 }
Sathish Ambley4342f272017-01-04 10:57:05 -0800223 }
224
Sungmin Choib5148892012-07-02 17:00:07 -0700225 pthread_mutex_unlock(&g_lock);
226 return err;
227}
228
229static int
230set_speaker_light_locked(struct light_device_t* dev,
231 struct light_state_t const* state)
232{
Arun Kumar K.R0efad602014-01-21 21:32:36 -0800233 int red, green, blue;
Yulian Shandorov784d7392013-06-20 04:28:59 -0700234 int blink;
Sungmin Choib5148892012-07-02 17:00:07 -0700235 int onMS, offMS;
236 unsigned int colorRGB;
237
Arun Kumar K.R0efad602014-01-21 21:32:36 -0800238 if(!dev) {
239 return -1;
240 }
241
Sungmin Choib5148892012-07-02 17:00:07 -0700242 switch (state->flashMode) {
243 case LIGHT_FLASH_TIMED:
244 onMS = state->flashOnMS;
245 offMS = state->flashOffMS;
246 break;
247 case LIGHT_FLASH_NONE:
248 default:
249 onMS = 0;
250 offMS = 0;
251 break;
252 }
253
254 colorRGB = state->color;
255
256#if 0
samin.ryud57c7d52012-08-03 23:59:41 +0900257 ALOGD("set_speaker_light_locked mode %d, colorRGB=%08X, onMS=%d, offMS=%d\n",
258 state->flashMode, colorRGB, onMS, offMS);
Sungmin Choib5148892012-07-02 17:00:07 -0700259#endif
260
261 red = (colorRGB >> 16) & 0xFF;
262 green = (colorRGB >> 8) & 0xFF;
263 blue = colorRGB & 0xFF;
264
Sungmin Choib5148892012-07-02 17:00:07 -0700265 if (onMS > 0 && offMS > 0) {
Ashay Jaiswal26f0b532015-05-03 17:58:58 +0530266 /*
267 * if ON time == OFF time
268 * use blink mode 2
269 * else
270 * use blink mode 1
271 */
272 if (onMS == offMS)
273 blink = 2;
274 else
275 blink = 1;
Sungmin Choib5148892012-07-02 17:00:07 -0700276 } else {
277 blink = 0;
Sungmin Choib5148892012-07-02 17:00:07 -0700278 }
279
yutingshih0e7c4bb2020-04-21 15:56:11 +0800280 LIGHT_DBG("red=%02X, green=%02X, blue=%02X, blink=%d\n", red, green, blue, blink );
281
Sungmin Choib5148892012-07-02 17:00:07 -0700282 if (blink) {
yutingshih0e7c4bb2020-04-21 15:56:11 +0800283 write_int( RED_BLINK_FILE, 0 );
284 write_int( GREEN_BLINK_FILE, 0 );
285 write_int( BLUE_BLINK_FILE, 0 );
286 usleep( 6000 ); /* Delay 6ms for off effective and complete */
287
Mao Li728ee0b2014-07-01 23:06:16 +0800288 if (red) {
289 if (write_int(RED_BLINK_FILE, blink))
290 write_int(RED_LED_FILE, 0);
Ch Ganesh Kumar71f50f32018-03-14 16:51:43 +0530291 }
yutingshih0e7c4bb2020-04-21 15:56:11 +0800292 else
293 {
294 write_int( RED_LED_FILE, 0 );
295 }
296
Mao Li728ee0b2014-07-01 23:06:16 +0800297 if (green) {
298 if (write_int(GREEN_BLINK_FILE, blink))
299 write_int(GREEN_LED_FILE, 0);
Ch Ganesh Kumar71f50f32018-03-14 16:51:43 +0530300 }
yutingshih0e7c4bb2020-04-21 15:56:11 +0800301 else
302 {
303 write_int( GREEN_LED_FILE, 0 );
304 }
305
Mao Li728ee0b2014-07-01 23:06:16 +0800306 if (blue) {
307 if (write_int(BLUE_BLINK_FILE, blink))
308 write_int(BLUE_LED_FILE, 0);
Ch Ganesh Kumar71f50f32018-03-14 16:51:43 +0530309 }
yutingshih0e7c4bb2020-04-21 15:56:11 +0800310 else
311 {
312 write_int( BLUE_LED_FILE, 0 );
313 }
Yulian Shandorov784d7392013-06-20 04:28:59 -0700314 } else {
yutingshih0e7c4bb2020-04-21 15:56:11 +0800315 write_int( RED_LED_FILE, 0 );
316 write_int( GREEN_LED_FILE, 0 );
317 write_int( BLUE_LED_FILE, 0 );
318 usleep( 6000 ); /* Delay 6ms for off effective and complete */
319
Yulian Shandorov784d7392013-06-20 04:28:59 -0700320 write_int(RED_LED_FILE, red);
321 write_int(GREEN_LED_FILE, green);
322 write_int(BLUE_LED_FILE, blue);
Sungmin Choib5148892012-07-02 17:00:07 -0700323 }
yutingshih0e7c4bb2020-04-21 15:56:11 +0800324 usleep( 5000 ); /* 5ms */
Sungmin Choib5148892012-07-02 17:00:07 -0700325
326 return 0;
327}
328
yutingshih0e7c4bb2020-04-21 15:56:11 +0800329#if defined( LED_FIRST_NOTIFICATION )
330 #undef LED_FIRST_NOTIFICATION
331#endif
332 #define LED_FIRST_NOTIFICATION 1
333
Sungmin Choib5148892012-07-02 17:00:07 -0700334static void
335handle_speaker_battery_locked(struct light_device_t* dev)
336{
yutingshih0e7c4bb2020-04-21 15:56:11 +0800337#if( LED_FIRST_NOTIFICATION )
338 if( is_lit( &g_notification ))
339 {
340 LIGHT_DBG("Notification\n");
341 set_speaker_light_locked( dev, &g_notification );
342 }
343 else
344 {
345 LIGHT_DBG("Battery\n");
346 set_speaker_light_locked( dev, &g_battery );
347 }
348#else /* Default */
Sungmin Choib5148892012-07-02 17:00:07 -0700349 if (is_lit(&g_battery)) {
350 set_speaker_light_locked(dev, &g_battery);
351 } else {
352 set_speaker_light_locked(dev, &g_notification);
353 }
yutingshih0e7c4bb2020-04-21 15:56:11 +0800354#endif
Sungmin Choib5148892012-07-02 17:00:07 -0700355}
356
357static int
Mao Lia4e053a2014-06-16 23:05:17 +0530358set_light_battery(struct light_device_t* dev,
359 struct light_state_t const* state)
360{
yutingshih0e7c4bb2020-04-21 15:56:11 +0800361 LIGHT_DBG("Enter...\n");
Mao Lia4e053a2014-06-16 23:05:17 +0530362 pthread_mutex_lock(&g_lock);
363 g_battery = *state;
364 handle_speaker_battery_locked(dev);
365 pthread_mutex_unlock(&g_lock);
366 return 0;
367}
368
369static int
Sungmin Choib5148892012-07-02 17:00:07 -0700370set_light_notifications(struct light_device_t* dev,
371 struct light_state_t const* state)
372{
yutingshih0e7c4bb2020-04-21 15:56:11 +0800373 LIGHT_DBG("Enter...\n");
Sungmin Choib5148892012-07-02 17:00:07 -0700374 pthread_mutex_lock(&g_lock);
375 g_notification = *state;
376 handle_speaker_battery_locked(dev);
377 pthread_mutex_unlock(&g_lock);
378 return 0;
379}
380
381static int
382set_light_attention(struct light_device_t* dev,
383 struct light_state_t const* state)
384{
385 pthread_mutex_lock(&g_lock);
386 if (state->flashMode == LIGHT_FLASH_HARDWARE) {
387 g_attention = state->flashOnMS;
388 } else if (state->flashMode == LIGHT_FLASH_NONE) {
389 g_attention = 0;
390 }
391 handle_speaker_battery_locked(dev);
392 pthread_mutex_unlock(&g_lock);
393 return 0;
394}
395
HuiWangf73bc572013-07-31 10:48:36 +0800396static int
397set_light_buttons(struct light_device_t* dev,
398 struct light_state_t const* state)
399{
400 int err = 0;
Arun Kumar K.R0efad602014-01-21 21:32:36 -0800401 if(!dev) {
402 return -1;
403 }
HuiWangf73bc572013-07-31 10:48:36 +0800404 pthread_mutex_lock(&g_lock);
405 err = write_int(BUTTON_FILE, state->color & 0xFF);
406 pthread_mutex_unlock(&g_lock);
407 return err;
408}
Sungmin Choib5148892012-07-02 17:00:07 -0700409
410/** Close the lights device */
411static int
412close_lights(struct light_device_t *dev)
413{
414 if (dev) {
415 free(dev);
416 }
417 return 0;
418}
419
420
421/******************************************************************************/
422
423/**
424 * module methods
425 */
426
427/** Open a new instance of a lights device using name */
428static int open_lights(const struct hw_module_t* module, char const* name,
429 struct hw_device_t** device)
430{
431 int (*set_light)(struct light_device_t* dev,
432 struct light_state_t const* state);
433
yutingshih0e7c4bb2020-04-21 15:56:11 +0800434 LIGHT_DBG("Enter...\n");
Xu Yang586c6d52016-09-19 17:54:16 +0800435 if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) {
Ch Ganesh Kumar71f50f32018-03-14 16:51:43 +0530436 set_light = set_light_backlight;
Xu Yang586c6d52016-09-19 17:54:16 +0800437 } else if (0 == strcmp(LIGHT_ID_BATTERY, name))
Mao Lia4e053a2014-06-16 23:05:17 +0530438 set_light = set_light_battery;
Sungmin Choib5148892012-07-02 17:00:07 -0700439 else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name))
440 set_light = set_light_notifications;
Arun Kumar K.R9e35f2b2017-08-23 13:46:03 +0530441 else if (0 == strcmp(LIGHT_ID_BUTTONS, name)) {
442 if (!access(BUTTON_FILE, F_OK)) {
443 // enable light button when the file is present
444 set_light = set_light_buttons;
445 } else {
446 return -EINVAL;
447 }
448 }
Sungmin Choib5148892012-07-02 17:00:07 -0700449 else if (0 == strcmp(LIGHT_ID_ATTENTION, name))
450 set_light = set_light_attention;
451 else
452 return -EINVAL;
453
454 pthread_once(&g_init, init_globals);
455
456 struct light_device_t *dev = malloc(sizeof(struct light_device_t));
Manoj Kumar AVM001b3092014-04-29 22:08:51 -0700457
458 if(!dev)
459 return -ENOMEM;
460
Sungmin Choib5148892012-07-02 17:00:07 -0700461 memset(dev, 0, sizeof(*dev));
462
463 dev->common.tag = HARDWARE_DEVICE_TAG;
Sathish Ambley4342f272017-01-04 10:57:05 -0800464 dev->common.version = LIGHTS_DEVICE_API_VERSION_2_0;
Sungmin Choib5148892012-07-02 17:00:07 -0700465 dev->common.module = (struct hw_module_t*)module;
466 dev->common.close = (int (*)(struct hw_device_t*))close_lights;
467 dev->set_light = set_light;
468
469 *device = (struct hw_device_t*)dev;
470 return 0;
471}
472
473static struct hw_module_methods_t lights_module_methods = {
474 .open = open_lights,
475};
476
477/*
478 * The lights Module
479 */
480struct hw_module_t HAL_MODULE_INFO_SYM = {
481 .tag = HARDWARE_MODULE_TAG,
482 .version_major = 1,
483 .version_minor = 0,
484 .id = LIGHTS_HARDWARE_MODULE_ID,
485 .name = "lights Module",
486 .author = "Google, Inc.",
487 .methods = &lights_module_methods,
488};