blob: d86ab974c6eaac5978c33b174d05ac703a38175b [file] [log] [blame]
Luca Stefani63bdfc12019-07-14 18:44:22 +02001/*
micky3876899d892019-08-07 18:49:07 +02002 * Copyright (C) 2014, 2017-2018 The Linux Foundation. All rights reserved.
3 * Not a contribution
4 * Copyright (C) 2008 The Android Open Source Project
Luca Stefani63bdfc12019-07-14 18:44:22 +02005 *
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
micky3876899d892019-08-07 18:49:07 +020019
20// #define LOG_NDEBUG 0
21
22#include <log/log.h>
23#include <cutils/properties.h>
24#include <stdint.h>
25#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
Luca Stefani63bdfc12019-07-14 18:44:22 +020028#include <errno.h>
29#include <fcntl.h>
30#include <pthread.h>
micky3876899d892019-08-07 18:49:07 +020031
32#include <sys/ioctl.h>
33#include <sys/types.h>
Luca Stefani63bdfc12019-07-14 18:44:22 +020034
35#include <hardware/lights.h>
36
micky3876899d892019-08-07 18:49:07 +020037#ifndef DEFAULT_LOW_PERSISTENCE_MODE_BRIGHTNESS
38#define DEFAULT_LOW_PERSISTENCE_MODE_BRIGHTNESS 0x80
39#endif
40
Luca Stefani63bdfc12019-07-14 18:44:22 +020041/******************************************************************************/
42
Luca Stefani63bdfc12019-07-14 18:44:22 +020043static pthread_once_t g_init = PTHREAD_ONCE_INIT;
44static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
micky3876899d892019-08-07 18:49:07 +020045static struct light_state_t g_notification;
46static struct light_state_t g_battery;
47static int g_last_backlight_mode = BRIGHTNESS_MODE_USER;
48static int g_attention = 0;
49static bool g_has_persistence_node = false;
Luca Stefani63bdfc12019-07-14 18:44:22 +020050
micky3876899d892019-08-07 18:49:07 +020051char const*const LCD_FILE
52 = "/sys/class/leds/lcd-backlight/brightness";
Luca Stefani63bdfc12019-07-14 18:44:22 +020053
micky3876899d892019-08-07 18:49:07 +020054char const*const LCD_FILE2
55 = "/sys/class/backlight/panel0-backlight/brightness";
Luca Stefani63bdfc12019-07-14 18:44:22 +020056
micky3876899d892019-08-07 18:49:07 +020057char const*const BUTTON_FILE
58 = "/sys/class/leds/button-backlight/brightness";
Luca Stefani63bdfc12019-07-14 18:44:22 +020059
micky3876899d892019-08-07 18:49:07 +020060char const*const PERSISTENCE_FILE
61 = "/sys/class/graphics/fb0/msm_fb_persist_mode";
62
63enum rgb_led {
64 LED_RED = 0,
65 LED_GREEN,
66 LED_BLUE,
67};
68
69char *led_names[] = {
70 "red",
71 "green",
72 "blue",
73};
Luca Stefani63bdfc12019-07-14 18:44:22 +020074/**
75 * device methods
76 */
77
micky3876899d892019-08-07 18:49:07 +020078void init_globals(void)
79{
Luca Stefani63bdfc12019-07-14 18:44:22 +020080 // init the mutex
81 pthread_mutex_init(&g_lock, NULL);
82}
83
micky3876899d892019-08-07 18:49:07 +020084static int write_int(char const* path, int value)
85{
Luca Stefani63bdfc12019-07-14 18:44:22 +020086 int fd;
87 static int already_warned = 0;
88
89 fd = open(path, O_RDWR);
90 if (fd >= 0) {
91 char buffer[20];
micky3876899d892019-08-07 18:49:07 +020092 int bytes = snprintf(buffer, sizeof(buffer), "%d\n", value);
93 ssize_t amt = write(fd, buffer, (size_t)bytes);
Luca Stefani63bdfc12019-07-14 18:44:22 +020094 close(fd);
95 return amt == -1 ? -errno : 0;
96 } else {
97 if (already_warned == 0) {
micky3876899d892019-08-07 18:49:07 +020098 ALOGE("write_int failed to open %s, errno = %d\n", path, errno);
Luca Stefani63bdfc12019-07-14 18:44:22 +020099 already_warned = 1;
100 }
101 return -errno;
102 }
103}
104
micky3876899d892019-08-07 18:49:07 +0200105static 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
119static int
120is_lit(struct light_state_t const* state)
121{
Luca Stefani63bdfc12019-07-14 18:44:22 +0200122 return state->color & 0x00ffffff;
123}
124
micky3876899d892019-08-07 18:49:07 +0200125static int
126rgb_to_brightness(struct light_state_t const* state)
127{
Luca Stefani63bdfc12019-07-14 18:44:22 +0200128 int color = state->color & 0x00ffffff;
micky3876899d892019-08-07 18:49:07 +0200129 return ((77*((color>>16)&0x00ff))
130 + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8;
Luca Stefani63bdfc12019-07-14 18:44:22 +0200131}
132
micky3876899d892019-08-07 18:49:07 +0200133static int
134set_light_backlight(struct light_device_t* dev,
135 struct light_state_t const* state)
136{
Luca Stefani63bdfc12019-07-14 18:44:22 +0200137 int err = 0;
138 int brightness = rgb_to_brightness(state);
micky3876899d892019-08-07 18:49:07 +0200139 unsigned int lpEnabled =
140 state->brightnessMode == BRIGHTNESS_MODE_LOW_PERSISTENCE;
141 if(!dev) {
142 return -1;
Luca Stefani63bdfc12019-07-14 18:44:22 +0200143 }
144
micky3876899d892019-08-07 18:49:07 +0200145 pthread_mutex_lock(&g_lock);
146 // Toggle low persistence mode state
147 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 }
160 }
161 g_last_backlight_mode = state->brightnessMode;
Luca Stefani63bdfc12019-07-14 18:44:22 +0200162 }
163
micky3876899d892019-08-07 18:49:07 +0200164 if (!err) {
165 if (!access(LCD_FILE, F_OK)) {
166 err = write_int(LCD_FILE, brightness);
Luca Stefani63bdfc12019-07-14 18:44:22 +0200167 } else {
micky3876899d892019-08-07 18:49:07 +0200168 err = write_int(LCD_FILE2, brightness);
Luca Stefani63bdfc12019-07-14 18:44:22 +0200169 }
170 }
171
172 pthread_mutex_unlock(&g_lock);
micky3876899d892019-08-07 18:49:07 +0200173 return cannot_handle_persistence ? -ENOSYS : err;
174}
175
176static 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
216static int
217set_speaker_light_locked(struct light_device_t* dev,
218 struct light_state_t const* state)
219{
220 int red, green, blue;
221 int onMS, offMS;
222 unsigned int colorRGB;
223 int blink = 0;
224 int rc = 0;
225
226 if(!dev) {
227 return -1;
228 }
229
230 colorRGB = state->color;
231 red = (colorRGB >> 16) & 0xFF;
232 green = (colorRGB >> 8) & 0xFF;
233 blue = colorRGB & 0xFF;
234
235 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;
268 }
269
270 ALOGD("set_speaker_light_locked mode=%d, colorRGB=%08X, onMS=%d, offMS=%d, rc=%d\n",
271 state->flashMode, colorRGB, onMS, offMS, rc);
272
273 return rc;
274}
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
287set_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
298set_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
323static 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);
Luca Stefani63bdfc12019-07-14 18:44:22 +0200334 return err;
335}
336
337/** Close the lights device */
micky3876899d892019-08-07 18:49:07 +0200338static int
339close_lights(struct light_device_t *dev)
340{
341 if (dev) {
342 free(dev);
343 }
Luca Stefani63bdfc12019-07-14 18:44:22 +0200344 return 0;
345}
346
micky3876899d892019-08-07 18:49:07 +0200347
Luca Stefani63bdfc12019-07-14 18:44:22 +0200348/******************************************************************************/
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,
micky3876899d892019-08-07 18:49:07 +0200356 struct hw_device_t** device)
357{
358 int (*set_light)(struct light_device_t* dev,
359 struct light_state_t const* state);
Luca Stefani63bdfc12019-07-14 18:44:22 +0200360
micky3876899d892019-08-07 18:49:07 +0200361 if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) {
362 g_has_persistence_node = !access(PERSISTENCE_FILE, F_OK);
Luca Stefani63bdfc12019-07-14 18:44:22 +0200363 set_light = set_light_backlight;
micky3876899d892019-08-07 18:49:07 +0200364 } else if (0 == strcmp(LIGHT_ID_BATTERY, name))
Luca Stefani63bdfc12019-07-14 18:44:22 +0200365 set_light = set_light_battery;
micky3876899d892019-08-07 18:49:07 +0200366 else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name))
367 set_light = set_light_notifications;
368 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 }
376 else if (0 == strcmp(LIGHT_ID_ATTENTION, name))
377 set_light = set_light_attention;
Luca Stefani63bdfc12019-07-14 18:44:22 +0200378 else
379 return -EINVAL;
380
381 pthread_once(&g_init, init_globals);
382
micky3876899d892019-08-07 18:49:07 +0200383 struct light_device_t *dev = malloc(sizeof(struct light_device_t));
384
385 if(!dev)
386 return -ENOMEM;
387
Luca Stefani63bdfc12019-07-14 18:44:22 +0200388 memset(dev, 0, sizeof(*dev));
389
390 dev->common.tag = HARDWARE_DEVICE_TAG;
micky3876899d892019-08-07 18:49:07 +0200391 dev->common.version = LIGHTS_DEVICE_API_VERSION_2_0;
Luca Stefani63bdfc12019-07-14 18:44:22 +0200392 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 = {
micky3876899d892019-08-07 18:49:07 +0200401 .open = open_lights,
Luca Stefani63bdfc12019-07-14 18:44:22 +0200402};
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,
micky3876899d892019-08-07 18:49:07 +0200412 .name = "lights Module",
Luca Stefani63bdfc12019-07-14 18:44:22 +0200413 .author = "Google, Inc.",
414 .methods = &lights_module_methods,
micky3876899d892019-08-07 18:49:07 +0200415};