blob: d63cc6799545f7011044da424c032bebc0b83641 [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;
Sungmin Choib5148892012-07-02 17:00:07 -070051
52char const*const RED_LED_FILE
53 = "/sys/class/leds/red/brightness";
54
55char const*const GREEN_LED_FILE
56 = "/sys/class/leds/green/brightness";
57
58char const*const BLUE_LED_FILE
59 = "/sys/class/leds/blue/brightness";
60
61char const*const LCD_FILE
62 = "/sys/class/leds/lcd-backlight/brightness";
63
HuiWangf73bc572013-07-31 10:48:36 +080064char const*const BUTTON_FILE
65 = "/sys/class/leds/button-backlight/brightness";
66
samin.ryud57c7d52012-08-03 23:59:41 +090067char const*const RED_BLINK_FILE
Yulian Shandorov784d7392013-06-20 04:28:59 -070068 = "/sys/class/leds/red/blink";
samin.ryud57c7d52012-08-03 23:59:41 +090069
Ameya Thakur192c8ac2013-08-12 16:50:01 -070070char const*const GREEN_BLINK_FILE
71 = "/sys/class/leds/green/blink";
72
73char const*const BLUE_BLINK_FILE
74 = "/sys/class/leds/blue/blink";
75
Sathish Ambley4342f272017-01-04 10:57:05 -080076char const*const PERSISTENCE_FILE
77 = "/sys/class/graphics/fb0/msm_fb_persist_mode";
78
Sungmin Choib5148892012-07-02 17:00:07 -070079/**
80 * device methods
81 */
82
83void init_globals(void)
84{
85 // init the mutex
86 pthread_mutex_init(&g_lock, NULL);
87}
88
89static int
90write_int(char const* path, int value)
91{
92 int fd;
93 static int already_warned = 0;
94
95 fd = open(path, O_RDWR);
96 if (fd >= 0) {
97 char buffer[20];
Manoj Kumar AVM001b3092014-04-29 22:08:51 -070098 int bytes = snprintf(buffer, sizeof(buffer), "%d\n", value);
Dileep Kumar Reddibf333c72014-02-25 14:32:51 +053099 ssize_t amt = write(fd, buffer, (size_t)bytes);
Sungmin Choib5148892012-07-02 17:00:07 -0700100 close(fd);
101 return amt == -1 ? -errno : 0;
102 } else {
103 if (already_warned == 0) {
104 ALOGE("write_int failed to open %s\n", path);
105 already_warned = 1;
106 }
107 return -errno;
108 }
109}
110
111static int
112is_lit(struct light_state_t const* state)
113{
114 return state->color & 0x00ffffff;
115}
116
117static int
118rgb_to_brightness(struct light_state_t const* state)
119{
120 int color = state->color & 0x00ffffff;
121 return ((77*((color>>16)&0x00ff))
122 + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8;
123}
124
125static int
126set_light_backlight(struct light_device_t* dev,
127 struct light_state_t const* state)
128{
129 int err = 0;
130 int brightness = rgb_to_brightness(state);
Sathish Ambley4342f272017-01-04 10:57:05 -0800131 unsigned int lpEnabled =
132 state->brightnessMode == BRIGHTNESS_MODE_LOW_PERSISTENCE;
Arun Kumar K.R0efad602014-01-21 21:32:36 -0800133 if(!dev) {
134 return -1;
135 }
Sathish Ambley4342f272017-01-04 10:57:05 -0800136
Sungmin Choib5148892012-07-02 17:00:07 -0700137 pthread_mutex_lock(&g_lock);
Sathish Ambley4342f272017-01-04 10:57:05 -0800138 // Toggle low persistence mode state
139 if ((g_last_backlight_mode != state->brightnessMode && lpEnabled) ||
140 (!lpEnabled &&
141 g_last_backlight_mode == BRIGHTNESS_MODE_LOW_PERSISTENCE)) {
142 if ((err = write_int(PERSISTENCE_FILE, lpEnabled)) != 0) {
143 ALOGE("%s: Failed to write to %s: %s\n", __FUNCTION__,
144 PERSISTENCE_FILE, strerror(errno));
145 }
146 if (lpEnabled != 0) {
147 brightness = DEFAULT_LOW_PERSISTENCE_MODE_BRIGHTNESS;
148 }
149 }
150
151 g_last_backlight_mode = state->brightnessMode;
152
153 if (!err) {
154 err = write_int(LCD_FILE, brightness);
155 }
156
Sungmin Choib5148892012-07-02 17:00:07 -0700157 pthread_mutex_unlock(&g_lock);
158 return err;
159}
160
161static int
Xu Yang586c6d52016-09-19 17:54:16 +0800162set_light_backlight_ext(struct light_device_t* dev,
163 struct light_state_t const* state)
164{
165 int err = 0;
166
167 if(!dev) {
168 return -1;
169 }
170
171 int brightness = state->color & 0x00ffffff;
172 pthread_mutex_lock(&g_lock);
173
174 if (brightness >= 0 && brightness <= g_brightness_max) {
175 set_brightness_ext_level(brightness);
176 }
177
178 pthread_mutex_unlock(&g_lock);
179
180 return err;
181}
182
183static int
Sungmin Choib5148892012-07-02 17:00:07 -0700184set_speaker_light_locked(struct light_device_t* dev,
185 struct light_state_t const* state)
186{
Arun Kumar K.R0efad602014-01-21 21:32:36 -0800187 int red, green, blue;
Yulian Shandorov784d7392013-06-20 04:28:59 -0700188 int blink;
Sungmin Choib5148892012-07-02 17:00:07 -0700189 int onMS, offMS;
190 unsigned int colorRGB;
191
Arun Kumar K.R0efad602014-01-21 21:32:36 -0800192 if(!dev) {
193 return -1;
194 }
195
Sungmin Choib5148892012-07-02 17:00:07 -0700196 switch (state->flashMode) {
197 case LIGHT_FLASH_TIMED:
198 onMS = state->flashOnMS;
199 offMS = state->flashOffMS;
200 break;
201 case LIGHT_FLASH_NONE:
202 default:
203 onMS = 0;
204 offMS = 0;
205 break;
206 }
207
208 colorRGB = state->color;
209
210#if 0
samin.ryud57c7d52012-08-03 23:59:41 +0900211 ALOGD("set_speaker_light_locked mode %d, colorRGB=%08X, onMS=%d, offMS=%d\n",
212 state->flashMode, colorRGB, onMS, offMS);
Sungmin Choib5148892012-07-02 17:00:07 -0700213#endif
214
215 red = (colorRGB >> 16) & 0xFF;
216 green = (colorRGB >> 8) & 0xFF;
217 blue = colorRGB & 0xFF;
218
Sungmin Choib5148892012-07-02 17:00:07 -0700219 if (onMS > 0 && offMS > 0) {
Ashay Jaiswal26f0b532015-05-03 17:58:58 +0530220 /*
221 * if ON time == OFF time
222 * use blink mode 2
223 * else
224 * use blink mode 1
225 */
226 if (onMS == offMS)
227 blink = 2;
228 else
229 blink = 1;
Sungmin Choib5148892012-07-02 17:00:07 -0700230 } else {
231 blink = 0;
Sungmin Choib5148892012-07-02 17:00:07 -0700232 }
233
234 if (blink) {
Mao Li728ee0b2014-07-01 23:06:16 +0800235 if (red) {
236 if (write_int(RED_BLINK_FILE, blink))
237 write_int(RED_LED_FILE, 0);
Xu Yang586c6d52016-09-19 17:54:16 +0800238 }
Mao Li728ee0b2014-07-01 23:06:16 +0800239 if (green) {
240 if (write_int(GREEN_BLINK_FILE, blink))
241 write_int(GREEN_LED_FILE, 0);
Xu Yang586c6d52016-09-19 17:54:16 +0800242 }
Mao Li728ee0b2014-07-01 23:06:16 +0800243 if (blue) {
244 if (write_int(BLUE_BLINK_FILE, blink))
245 write_int(BLUE_LED_FILE, 0);
Xu Yang586c6d52016-09-19 17:54:16 +0800246 }
Yulian Shandorov784d7392013-06-20 04:28:59 -0700247 } else {
248 write_int(RED_LED_FILE, red);
249 write_int(GREEN_LED_FILE, green);
250 write_int(BLUE_LED_FILE, blue);
Sungmin Choib5148892012-07-02 17:00:07 -0700251 }
Sungmin Choib5148892012-07-02 17:00:07 -0700252
253 return 0;
254}
255
256static void
257handle_speaker_battery_locked(struct light_device_t* dev)
258{
259 if (is_lit(&g_battery)) {
260 set_speaker_light_locked(dev, &g_battery);
261 } else {
262 set_speaker_light_locked(dev, &g_notification);
263 }
264}
265
266static int
Mao Lia4e053a2014-06-16 23:05:17 +0530267set_light_battery(struct light_device_t* dev,
268 struct light_state_t const* state)
269{
270 pthread_mutex_lock(&g_lock);
271 g_battery = *state;
272 handle_speaker_battery_locked(dev);
273 pthread_mutex_unlock(&g_lock);
274 return 0;
275}
276
277static int
Sungmin Choib5148892012-07-02 17:00:07 -0700278set_light_notifications(struct light_device_t* dev,
279 struct light_state_t const* state)
280{
281 pthread_mutex_lock(&g_lock);
282 g_notification = *state;
283 handle_speaker_battery_locked(dev);
284 pthread_mutex_unlock(&g_lock);
285 return 0;
286}
287
288static int
289set_light_attention(struct light_device_t* dev,
290 struct light_state_t const* state)
291{
292 pthread_mutex_lock(&g_lock);
293 if (state->flashMode == LIGHT_FLASH_HARDWARE) {
294 g_attention = state->flashOnMS;
295 } else if (state->flashMode == LIGHT_FLASH_NONE) {
296 g_attention = 0;
297 }
298 handle_speaker_battery_locked(dev);
299 pthread_mutex_unlock(&g_lock);
300 return 0;
301}
302
HuiWangf73bc572013-07-31 10:48:36 +0800303static int
304set_light_buttons(struct light_device_t* dev,
305 struct light_state_t const* state)
306{
307 int err = 0;
Arun Kumar K.R0efad602014-01-21 21:32:36 -0800308 if(!dev) {
309 return -1;
310 }
HuiWangf73bc572013-07-31 10:48:36 +0800311 pthread_mutex_lock(&g_lock);
312 err = write_int(BUTTON_FILE, state->color & 0xFF);
313 pthread_mutex_unlock(&g_lock);
314 return err;
315}
Sungmin Choib5148892012-07-02 17:00:07 -0700316
317/** Close the lights device */
318static int
319close_lights(struct light_device_t *dev)
320{
321 if (dev) {
322 free(dev);
323 }
324 return 0;
325}
326
327
328/******************************************************************************/
329
330/**
331 * module methods
332 */
333
334/** Open a new instance of a lights device using name */
335static int open_lights(const struct hw_module_t* module, char const* name,
336 struct hw_device_t** device)
337{
338 int (*set_light)(struct light_device_t* dev,
339 struct light_state_t const* state);
340
Xu Yang586c6d52016-09-19 17:54:16 +0800341 if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) {
342 char property[PROPERTY_VALUE_MAX];
343 property_get("persist.extend.brightness", property, "0");
344
345 if(!(strncmp(property, "1", PROPERTY_VALUE_MAX)) ||
346 !(strncmp(property, "true", PROPERTY_VALUE_MAX))) {
347 property_get("persist.display.max_brightness", property, "255");
348 g_brightness_max = atoi(property);
349 set_brightness_ext_init();
350 set_light = set_light_backlight_ext;
351 } else
352 set_light = set_light_backlight;
353 } else if (0 == strcmp(LIGHT_ID_BATTERY, name))
Mao Lia4e053a2014-06-16 23:05:17 +0530354 set_light = set_light_battery;
Sungmin Choib5148892012-07-02 17:00:07 -0700355 else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name))
356 set_light = set_light_notifications;
HuiWangf73bc572013-07-31 10:48:36 +0800357 else if (0 == strcmp(LIGHT_ID_BUTTONS, name))
358 set_light = set_light_buttons;
Sungmin Choib5148892012-07-02 17:00:07 -0700359 else if (0 == strcmp(LIGHT_ID_ATTENTION, name))
360 set_light = set_light_attention;
361 else
362 return -EINVAL;
363
364 pthread_once(&g_init, init_globals);
365
366 struct light_device_t *dev = malloc(sizeof(struct light_device_t));
Manoj Kumar AVM001b3092014-04-29 22:08:51 -0700367
368 if(!dev)
369 return -ENOMEM;
370
Sungmin Choib5148892012-07-02 17:00:07 -0700371 memset(dev, 0, sizeof(*dev));
372
373 dev->common.tag = HARDWARE_DEVICE_TAG;
Sathish Ambley4342f272017-01-04 10:57:05 -0800374 dev->common.version = LIGHTS_DEVICE_API_VERSION_2_0;
Sungmin Choib5148892012-07-02 17:00:07 -0700375 dev->common.module = (struct hw_module_t*)module;
376 dev->common.close = (int (*)(struct hw_device_t*))close_lights;
377 dev->set_light = set_light;
378
379 *device = (struct hw_device_t*)dev;
380 return 0;
381}
382
383static struct hw_module_methods_t lights_module_methods = {
384 .open = open_lights,
385};
386
387/*
388 * The lights Module
389 */
390struct hw_module_t HAL_MODULE_INFO_SYM = {
391 .tag = HARDWARE_MODULE_TAG,
392 .version_major = 1,
393 .version_minor = 0,
394 .id = LIGHTS_HARDWARE_MODULE_ID,
395 .name = "lights Module",
396 .author = "Google, Inc.",
397 .methods = &lights_module_methods,
398};