Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
Christopher N. Hesse | ae27148 | 2016-12-01 15:51:12 +0100 | [diff] [blame] | 3 | * Copyright (C) 2015-2016 The CyanogenMod Project |
Simon Shields | 310d199 | 2017-02-18 20:55:11 +1100 | [diff] [blame] | 4 | * Copyright (C) 2017 The LineageOS Project |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 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 | #define LOG_TAG "SamsungLightsHAL" |
| 20 | /* #define LOG_NDEBUG 0 */ |
| 21 | |
| 22 | #include <cutils/log.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <unistd.h> |
| 26 | #include <errno.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <pthread.h> |
| 29 | |
| 30 | #include <sys/ioctl.h> |
| 31 | #include <sys/types.h> |
| 32 | |
| 33 | #include <hardware/lights.h> |
Christopher N. Hesse | 898e1fe | 2016-12-07 12:12:23 +0100 | [diff] [blame] | 34 | #include <liblights/samsung_lights_helper.h> |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 35 | |
Christopher N. Hesse | ae27148 | 2016-12-01 15:51:12 +0100 | [diff] [blame] | 36 | #include "samsung_lights.h" |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 37 | |
| 38 | #define COLOR_MASK 0x00ffffff |
| 39 | |
Christopher N. Hesse | f3c6a42 | 2016-12-05 22:11:55 +0100 | [diff] [blame] | 40 | #define MAX_INPUT_BRIGHTNESS 255 |
| 41 | |
Christopher N. Hesse | a8fe2a1 | 2016-12-31 17:14:47 +0100 | [diff] [blame] | 42 | enum component_mask_t { |
| 43 | COMPONENT_BACKLIGHT = 0x1, |
| 44 | COMPONENT_BUTTON_LIGHT = 0x2, |
| 45 | COMPONENT_LED = 0x4, |
Dominggoes Isakh | df15802 | 2017-05-26 23:43:49 +0200 | [diff] [blame] | 46 | COMPONENT_BLN = 0x8, |
Christopher N. Hesse | a8fe2a1 | 2016-12-31 17:14:47 +0100 | [diff] [blame] | 47 | }; |
| 48 | |
Simon Shields | 310d199 | 2017-02-18 20:55:11 +1100 | [diff] [blame] | 49 | enum light_t { |
| 50 | TYPE_BATTERY = 0, |
| 51 | TYPE_NOTIFICATION = 1, |
| 52 | TYPE_ATTENTION = 2, |
| 53 | }; |
| 54 | |
Christopher N. Hesse | a8fe2a1 | 2016-12-31 17:14:47 +0100 | [diff] [blame] | 55 | // Assume backlight is always supported |
| 56 | static int hw_components = COMPONENT_BACKLIGHT; |
| 57 | |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 58 | static pthread_once_t g_init = PTHREAD_ONCE_INIT; |
| 59 | static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER; |
| 60 | |
Christopher N. Hesse | f3c6a42 | 2016-12-05 22:11:55 +0100 | [diff] [blame] | 61 | struct backlight_config { |
| 62 | int cur_brightness, max_brightness; |
| 63 | }; |
| 64 | |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 65 | struct led_config { |
| 66 | unsigned int color; |
| 67 | int delay_on, delay_off; |
| 68 | }; |
| 69 | |
Christopher N. Hesse | f3c6a42 | 2016-12-05 22:11:55 +0100 | [diff] [blame] | 70 | static struct backlight_config g_backlight; // For panel backlight |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 71 | static struct led_config g_leds[3]; // For battery, notifications, and attention. |
| 72 | static int g_cur_led = -1; // Presently showing LED of the above. |
| 73 | |
Christopher N. Hesse | a8fe2a1 | 2016-12-31 17:14:47 +0100 | [diff] [blame] | 74 | void check_component_support() |
| 75 | { |
| 76 | if (access(BUTTON_BRIGHTNESS_NODE, W_OK) == 0) |
| 77 | hw_components |= COMPONENT_BUTTON_LIGHT; |
| 78 | if (access(LED_BLINK_NODE, W_OK) == 0) |
| 79 | hw_components |= COMPONENT_LED; |
Dominggoes Isakh | df15802 | 2017-05-26 23:43:49 +0200 | [diff] [blame] | 80 | #ifdef LED_BLN_NODE |
| 81 | if (access(LED_BLN_NODE, W_OK) == 0) |
| 82 | hw_components |= COMPONENT_BLN; |
| 83 | #endif |
Christopher N. Hesse | a8fe2a1 | 2016-12-31 17:14:47 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 86 | void init_g_lock(void) |
| 87 | { |
| 88 | pthread_mutex_init(&g_lock, NULL); |
| 89 | } |
| 90 | |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 91 | static int write_str(char const *path, const char* value) |
| 92 | { |
| 93 | int fd; |
| 94 | static int already_warned; |
| 95 | |
| 96 | already_warned = 0; |
| 97 | |
| 98 | ALOGV("write_str: path %s, value %s", path, value); |
| 99 | fd = open(path, O_RDWR); |
| 100 | |
| 101 | if (fd >= 0) { |
| 102 | int amt = write(fd, value, strlen(value)); |
| 103 | close(fd); |
| 104 | return amt == -1 ? -errno : 0; |
| 105 | } else { |
| 106 | if (already_warned == 0) { |
Christopher N. Hesse | b5b9500 | 2017-03-04 15:04:16 +0100 | [diff] [blame] | 107 | ALOGE("write_str failed to open %s", path); |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 108 | already_warned = 1; |
| 109 | } |
| 110 | return -errno; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | static int rgb_to_brightness(struct light_state_t const *state) |
| 115 | { |
| 116 | int color = state->color & COLOR_MASK; |
| 117 | |
| 118 | return ((77*((color>>16) & 0x00ff)) |
| 119 | + (150*((color>>8) & 0x00ff)) + (29*(color & 0x00ff))) >> 8; |
| 120 | } |
| 121 | |
| 122 | static int set_light_backlight(struct light_device_t *dev __unused, |
| 123 | struct light_state_t const *state) |
| 124 | { |
| 125 | int err = 0; |
| 126 | int brightness = rgb_to_brightness(state); |
Christopher N. Hesse | f3c6a42 | 2016-12-05 22:11:55 +0100 | [diff] [blame] | 127 | int max_brightness = g_backlight.max_brightness; |
| 128 | |
| 129 | /* |
Luca Stefani | a042209 | 2017-03-13 14:53:14 +0100 | [diff] [blame] | 130 | * If max panel brightness is not the default (255), |
| 131 | * apply linear scaling across the accepted range. |
Christopher N. Hesse | f3c6a42 | 2016-12-05 22:11:55 +0100 | [diff] [blame] | 132 | */ |
Luca Stefani | a042209 | 2017-03-13 14:53:14 +0100 | [diff] [blame] | 133 | if (max_brightness != MAX_INPUT_BRIGHTNESS) { |
Christopher N. Hesse | f3c6a42 | 2016-12-05 22:11:55 +0100 | [diff] [blame] | 134 | int old_brightness = brightness; |
| 135 | brightness = brightness * max_brightness / MAX_INPUT_BRIGHTNESS; |
Christopher N. Hesse | b5b9500 | 2017-03-04 15:04:16 +0100 | [diff] [blame] | 136 | ALOGV("%s: scaling brightness %d => %d", __func__, |
Christopher N. Hesse | f3c6a42 | 2016-12-05 22:11:55 +0100 | [diff] [blame] | 137 | old_brightness, brightness); |
| 138 | } |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 139 | |
| 140 | pthread_mutex_lock(&g_lock); |
Christopher N. Hesse | 898e1fe | 2016-12-07 12:12:23 +0100 | [diff] [blame] | 141 | err = set_cur_panel_brightness(brightness); |
Christopher N. Hesse | f3c6a42 | 2016-12-05 22:11:55 +0100 | [diff] [blame] | 142 | if (err == 0) |
| 143 | g_backlight.cur_brightness = brightness; |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 144 | |
| 145 | pthread_mutex_unlock(&g_lock); |
| 146 | return err; |
| 147 | } |
| 148 | |
| 149 | static int set_light_buttons(struct light_device_t* dev __unused, |
| 150 | struct light_state_t const* state) |
| 151 | { |
| 152 | int err = 0; |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 153 | pthread_mutex_lock(&g_lock); |
Dominggoes Isakh | d81a0a3 | 2017-05-25 14:49:46 +0200 | [diff] [blame] | 154 | int brightness = (state->color & COLOR_MASK) ? 1 : 0; |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 155 | |
Dominggoes Isakh | d81a0a3 | 2017-05-25 14:49:46 +0200 | [diff] [blame] | 156 | #ifdef VAR_BUTTON_BRIGHTNESS |
| 157 | brightness = rgb_to_brightness(state); |
| 158 | #endif |
| 159 | err = set_cur_button_brightness(brightness); |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 160 | pthread_mutex_unlock(&g_lock); |
| 161 | |
| 162 | return err; |
| 163 | } |
| 164 | |
| 165 | static int close_lights(struct light_device_t *dev) |
| 166 | { |
| 167 | ALOGV("close_light is called"); |
| 168 | if (dev) |
| 169 | free(dev); |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | /* LEDs */ |
| 175 | static int write_leds(const struct led_config *led) |
| 176 | { |
| 177 | static const struct led_config led_off = {0, 0, 0}; |
| 178 | |
| 179 | char blink[32]; |
| 180 | int count, err; |
| 181 | int color; |
| 182 | |
| 183 | if (led == NULL) |
| 184 | led = &led_off; |
| 185 | |
| 186 | count = snprintf(blink, |
| 187 | sizeof(blink) - 1, |
| 188 | "0x%08x %d %d", |
| 189 | led->color, |
| 190 | led->delay_on, |
| 191 | led->delay_off); |
| 192 | if (count < 0) { |
| 193 | return -errno; |
| 194 | } else if ((unsigned int)count >= sizeof(blink)-1) { |
| 195 | ALOGE("%s: Truncated string: blink=\"%s\".", __func__, blink); |
| 196 | return -EINVAL; |
| 197 | } |
| 198 | |
Christopher N. Hesse | b5b9500 | 2017-03-04 15:04:16 +0100 | [diff] [blame] | 199 | ALOGV("%s: color=0x%08x, delay_on=%d, delay_off=%d, blink=%s", |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 200 | __func__, led->color, led->delay_on, led->delay_off, blink); |
| 201 | |
Jason Vidal | e95e9c3 | 2017-06-11 08:02:38 +0000 | [diff] [blame^] | 202 | /* Add '\n' here to make the above log message clean. */ |
| 203 | blink[count] = '\n'; |
| 204 | blink[count+1] = '\0'; |
| 205 | |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 206 | pthread_mutex_lock(&g_lock); |
Christopher N. Hesse | ae27148 | 2016-12-01 15:51:12 +0100 | [diff] [blame] | 207 | err = write_str(LED_BLINK_NODE, blink); |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 208 | pthread_mutex_unlock(&g_lock); |
| 209 | |
| 210 | return err; |
| 211 | } |
| 212 | |
Simon Shields | 310d199 | 2017-02-18 20:55:11 +1100 | [diff] [blame] | 213 | static int calibrate_color(int color, int brightness) |
| 214 | { |
| 215 | int red = ((color >> 16) & 0xFF) * LED_ADJUSTMENT_R; |
| 216 | int green = ((color >> 8) & 0xFF) * LED_ADJUSTMENT_G; |
| 217 | int blue = (color & 0xFF) * LED_ADJUSTMENT_B; |
| 218 | |
| 219 | return (((red * brightness) / 255) << 16) + (((green * brightness) / 255) << 8) + ((blue * brightness) / 255); |
| 220 | } |
| 221 | |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 222 | static int set_light_leds(struct light_state_t const *state, int type) |
| 223 | { |
| 224 | struct led_config *led; |
| 225 | int err = 0; |
Simon Shields | 310d199 | 2017-02-18 20:55:11 +1100 | [diff] [blame] | 226 | int adjusted_brightness; |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 227 | |
| 228 | ALOGV("%s: type=%d, color=0x%010x, fM=%d, fOnMS=%d, fOffMs=%d.", __func__, |
| 229 | type, state->color,state->flashMode, state->flashOnMS, state->flashOffMS); |
| 230 | |
| 231 | if (type < 0 || (size_t)type >= sizeof(g_leds)/sizeof(g_leds[0])) { |
| 232 | return -EINVAL; |
| 233 | } |
| 234 | |
| 235 | /* type is one of: |
| 236 | * 0. battery |
| 237 | * 1. notifications |
| 238 | * 2. attention |
| 239 | * which are multiplexed onto the same physical LED in the above order. */ |
| 240 | led = &g_leds[type]; |
| 241 | |
| 242 | switch (state->flashMode) { |
| 243 | case LIGHT_FLASH_NONE: |
| 244 | /* Set LED to a solid color, spec is unclear on the exact behavior here. */ |
| 245 | led->delay_on = led->delay_off = 0; |
| 246 | break; |
| 247 | case LIGHT_FLASH_TIMED: |
| 248 | case LIGHT_FLASH_HARDWARE: |
| 249 | led->delay_on = state->flashOnMS; |
| 250 | led->delay_off = state->flashOffMS; |
| 251 | break; |
| 252 | default: |
| 253 | return -EINVAL; |
| 254 | } |
| 255 | |
Simon Shields | 310d199 | 2017-02-18 20:55:11 +1100 | [diff] [blame] | 256 | switch (type) { |
| 257 | case TYPE_BATTERY: |
| 258 | adjusted_brightness = LED_BRIGHTNESS_BATTERY; |
| 259 | break; |
| 260 | case TYPE_NOTIFICATION: |
| 261 | adjusted_brightness = LED_BRIGHTNESS_NOTIFICATION; |
| 262 | break; |
| 263 | case TYPE_ATTENTION: |
| 264 | adjusted_brightness = LED_BRIGHTNESS_ATTENTION; |
| 265 | break; |
| 266 | default: |
| 267 | adjusted_brightness = 255; |
| 268 | } |
| 269 | |
| 270 | |
| 271 | |
| 272 | led->color = calibrate_color(state->color & COLOR_MASK, adjusted_brightness); |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 273 | |
| 274 | if (led->color > 0) { |
| 275 | /* This LED is lit. */ |
| 276 | if (type >= g_cur_led) { |
| 277 | /* And it has the highest priority, so show it. */ |
| 278 | err = write_leds(led); |
| 279 | g_cur_led = type; |
| 280 | } |
| 281 | } else { |
| 282 | /* This LED is not (any longer) lit. */ |
| 283 | if (type == g_cur_led) { |
| 284 | /* But it is currently showing, switch to a lower-priority LED. */ |
| 285 | int i; |
| 286 | |
| 287 | for (i = type-1; i >= 0; i--) { |
| 288 | if (g_leds[i].color > 0) { |
| 289 | /* Found a lower-priority LED to switch to. */ |
| 290 | err = write_leds(&g_leds[i]); |
| 291 | goto switched; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /* No LEDs are lit, turn off. */ |
| 296 | err = write_leds(NULL); |
| 297 | switched: |
| 298 | g_cur_led = i; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | return err; |
| 303 | } |
| 304 | |
Dominggoes Isakh | df15802 | 2017-05-26 23:43:49 +0200 | [diff] [blame] | 305 | #ifdef LED_BLN_NODE |
| 306 | static int set_light_bln_notifications(struct light_device_t *dev __unused, |
| 307 | struct light_state_t const *state) |
| 308 | { |
| 309 | int err = 0; |
| 310 | |
| 311 | pthread_mutex_lock(&g_lock); |
| 312 | err = write_str(LED_BLN_NODE, state->color & COLOR_MASK ? "1" : "0"); |
| 313 | pthread_mutex_unlock(&g_lock); |
| 314 | |
| 315 | return err; |
| 316 | } |
| 317 | #endif |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 318 | static int set_light_leds_battery(struct light_device_t *dev __unused, |
| 319 | struct light_state_t const *state) |
| 320 | { |
Simon Shields | 310d199 | 2017-02-18 20:55:11 +1100 | [diff] [blame] | 321 | return set_light_leds(state, TYPE_BATTERY); |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | static int set_light_leds_notifications(struct light_device_t *dev __unused, |
| 325 | struct light_state_t const *state) |
| 326 | { |
Simon Shields | 310d199 | 2017-02-18 20:55:11 +1100 | [diff] [blame] | 327 | return set_light_leds(state, TYPE_NOTIFICATION); |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static int set_light_leds_attention(struct light_device_t *dev __unused, |
| 331 | struct light_state_t const *state) |
| 332 | { |
| 333 | struct light_state_t fixed; |
| 334 | |
| 335 | memcpy(&fixed, state, sizeof(fixed)); |
| 336 | |
| 337 | /* The framework does odd things with the attention lights, fix them up to |
| 338 | * do something sensible here. */ |
| 339 | switch (fixed.flashMode) { |
| 340 | case LIGHT_FLASH_NONE: |
| 341 | /* LightsService.Light::stopFlashing calls with non-zero color. */ |
| 342 | fixed.color = 0; |
| 343 | break; |
| 344 | case LIGHT_FLASH_HARDWARE: |
| 345 | /* PowerManagerService::setAttentionLight calls with onMS=3, offMS=0, which |
| 346 | * just makes for a slightly-dimmer LED. */ |
| 347 | if (fixed.flashOnMS > 0 && fixed.flashOffMS == 0) |
| 348 | fixed.flashMode = LIGHT_FLASH_NONE; |
| 349 | fixed.color = 0x000000ff; |
| 350 | break; |
| 351 | } |
| 352 | |
Simon Shields | 310d199 | 2017-02-18 20:55:11 +1100 | [diff] [blame] | 353 | return set_light_leds(&fixed, TYPE_ATTENTION); |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | static int open_lights(const struct hw_module_t *module, char const *name, |
| 357 | struct hw_device_t **device) |
| 358 | { |
Christopher N. Hesse | a8fe2a1 | 2016-12-31 17:14:47 +0100 | [diff] [blame] | 359 | int requested_component; |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 360 | int (*set_light)(struct light_device_t *dev, |
| 361 | struct light_state_t const *state); |
| 362 | |
Christopher N. Hesse | a8fe2a1 | 2016-12-31 17:14:47 +0100 | [diff] [blame] | 363 | check_component_support(); |
| 364 | |
| 365 | if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) { |
| 366 | requested_component = COMPONENT_BACKLIGHT; |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 367 | set_light = set_light_backlight; |
Christopher N. Hesse | a8fe2a1 | 2016-12-31 17:14:47 +0100 | [diff] [blame] | 368 | } else if (0 == strcmp(LIGHT_ID_BUTTONS, name)) { |
| 369 | requested_component = COMPONENT_BUTTON_LIGHT; |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 370 | set_light = set_light_buttons; |
Christopher N. Hesse | a8fe2a1 | 2016-12-31 17:14:47 +0100 | [diff] [blame] | 371 | } else if (0 == strcmp(LIGHT_ID_BATTERY, name)) { |
| 372 | requested_component = COMPONENT_LED; |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 373 | set_light = set_light_leds_battery; |
Christopher N. Hesse | a8fe2a1 | 2016-12-31 17:14:47 +0100 | [diff] [blame] | 374 | } else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name)) { |
| 375 | requested_component = COMPONENT_LED; |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 376 | set_light = set_light_leds_notifications; |
Dominggoes Isakh | df15802 | 2017-05-26 23:43:49 +0200 | [diff] [blame] | 377 | #ifdef LED_BLN_NODE |
| 378 | if (hw_components & COMPONENT_BLN) { |
| 379 | requested_component = COMPONENT_BLN; |
| 380 | set_light = set_light_bln_notifications; |
| 381 | } |
| 382 | #endif |
Christopher N. Hesse | a8fe2a1 | 2016-12-31 17:14:47 +0100 | [diff] [blame] | 383 | } else if (0 == strcmp(LIGHT_ID_ATTENTION, name)) { |
| 384 | requested_component = COMPONENT_LED; |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 385 | set_light = set_light_leds_attention; |
Christopher N. Hesse | a8fe2a1 | 2016-12-31 17:14:47 +0100 | [diff] [blame] | 386 | } else { |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 387 | return -EINVAL; |
Christopher N. Hesse | a8fe2a1 | 2016-12-31 17:14:47 +0100 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | if ((hw_components & requested_component) == 0) { |
| 391 | ALOGV("%s: component 0x%x not supported by device", __func__, |
| 392 | requested_component); |
| 393 | return -EINVAL; |
| 394 | } |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 395 | |
Christopher N. Hesse | 898e1fe | 2016-12-07 12:12:23 +0100 | [diff] [blame] | 396 | int max_brightness = get_max_panel_brightness(); |
Christopher N. Hesse | f3c6a42 | 2016-12-05 22:11:55 +0100 | [diff] [blame] | 397 | if (max_brightness < 0) { |
| 398 | ALOGE("%s: failed to read max panel brightness, fallback to 255!", |
| 399 | __func__); |
| 400 | max_brightness = 255; |
| 401 | } |
| 402 | g_backlight.max_brightness = max_brightness; |
| 403 | |
Christopher N. Hesse | 79a9b15 | 2016-01-26 12:17:55 +0100 | [diff] [blame] | 404 | pthread_once(&g_init, init_g_lock); |
| 405 | |
| 406 | struct light_device_t *dev = malloc(sizeof(struct light_device_t)); |
| 407 | memset(dev, 0, sizeof(*dev)); |
| 408 | |
| 409 | dev->common.tag = HARDWARE_DEVICE_TAG; |
| 410 | dev->common.version = 0; |
| 411 | dev->common.module = (struct hw_module_t *)module; |
| 412 | dev->common.close = (int (*)(struct hw_device_t *))close_lights; |
| 413 | dev->set_light = set_light; |
| 414 | |
| 415 | *device = (struct hw_device_t *)dev; |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | static struct hw_module_methods_t lights_module_methods = { |
| 421 | .open = open_lights, |
| 422 | }; |
| 423 | |
| 424 | struct hw_module_t HAL_MODULE_INFO_SYM = { |
| 425 | .tag = HARDWARE_MODULE_TAG, |
| 426 | .version_major = 1, |
| 427 | .version_minor = 0, |
| 428 | .id = LIGHTS_HARDWARE_MODULE_ID, |
| 429 | .name = "Samsung Lights Module", |
| 430 | .author = "The CyanogenMod Project", |
| 431 | .methods = &lights_module_methods, |
| 432 | }; |