Amit Pundir | 4c517ea | 2021-05-23 02:24:19 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2014, 2017-2018 The Linux Foundation. All rights reserved. |
| 3 | * Not a contribution |
| 4 | * 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 |
| 21 | |
| 22 | #include <log/log.h> |
| 23 | #include <cutils/properties.h> |
| 24 | |
| 25 | #include <stdint.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <unistd.h> |
| 29 | #include <errno.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <pthread.h> |
| 32 | |
| 33 | #include <sys/ioctl.h> |
| 34 | #include <sys/types.h> |
| 35 | |
| 36 | #include <hardware/lights.h> |
| 37 | |
| 38 | #define CG_COLOR_ID_PROPERTY "ro.boot.hardware.color" |
| 39 | |
| 40 | /******************************************************************************/ |
| 41 | static pthread_once_t g_init = PTHREAD_ONCE_INIT; |
| 42 | static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER; |
| 43 | static int rgb_brightness_ratio = 255; |
| 44 | |
| 45 | char const*const LCD_FILE |
| 46 | = "/sys/class/backlight/wled/brightness"; |
| 47 | |
| 48 | /** |
| 49 | * device methods |
| 50 | */ |
| 51 | |
| 52 | void init_globals(void) |
| 53 | { |
| 54 | char color_id_prop[PROPERTY_VALUE_MAX] = {""}; |
| 55 | |
| 56 | // init the mutex |
| 57 | pthread_mutex_init(&g_lock, NULL); |
| 58 | |
| 59 | // check CG color |
| 60 | property_get(CG_COLOR_ID_PROPERTY, color_id_prop, "DEF00"); |
| 61 | if (strcmp(color_id_prop, "GRA00") == 0) { |
| 62 | rgb_brightness_ratio = 25; |
| 63 | } else if (strcmp(color_id_prop, "SLV00") == 0) { |
| 64 | rgb_brightness_ratio = 15; |
| 65 | } else if (strcmp(color_id_prop, "BLU00") == 0) { |
| 66 | rgb_brightness_ratio = 15; |
| 67 | } else { |
| 68 | rgb_brightness_ratio = 20; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | static int |
| 73 | write_int(char const* path, int value) |
| 74 | { |
| 75 | int fd; |
| 76 | static int already_warned = 0; |
| 77 | |
| 78 | fd = open(path, O_WRONLY); |
| 79 | if (fd >= 0) { |
| 80 | char buffer[20]; |
| 81 | ssize_t amt; |
| 82 | size_t bytes = snprintf(buffer, sizeof(buffer), "%d\n", value); |
| 83 | if(bytes < sizeof(buffer)) { |
| 84 | amt = write(fd, buffer, bytes); |
| 85 | } else { |
| 86 | amt = -1; |
| 87 | errno = -EINVAL; |
| 88 | } |
| 89 | close(fd); |
| 90 | return amt == -1 ? -errno : 0; |
| 91 | } else { |
| 92 | if (already_warned == 0) { |
| 93 | ALOGE("write_int failed to open %s\n", path); |
| 94 | already_warned = 1; |
| 95 | } |
| 96 | return -errno; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | static int |
| 101 | rgb_to_brightness(struct light_state_t const* state) |
| 102 | { |
| 103 | int color = state->color & 0x00ffffff; |
| 104 | return ((77*((color>>16)&0x00ff)) |
| 105 | + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8; |
| 106 | } |
| 107 | |
| 108 | static int |
| 109 | set_light_backlight(struct light_device_t* dev, |
| 110 | struct light_state_t const* state) |
| 111 | { |
| 112 | if(!dev) { |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | int brightness = rgb_to_brightness(state) << 4; // Scale up to 4095 |
| 117 | write_int(LCD_FILE, brightness); |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | /** Close the lights device */ |
| 123 | static int |
| 124 | close_lights(struct light_device_t *dev) |
| 125 | { |
| 126 | if (dev) { |
| 127 | free(dev); |
| 128 | } |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | /******************************************************************************/ |
| 134 | |
| 135 | /** |
| 136 | * module methods |
| 137 | */ |
| 138 | |
| 139 | /** Open a new instance of a lights device using name */ |
| 140 | static int open_lights(const struct hw_module_t* module, char const* name, |
| 141 | struct hw_device_t** device) |
| 142 | { |
| 143 | pthread_once(&g_init, init_globals); |
| 144 | |
| 145 | struct light_device_t *dev = malloc(sizeof(struct light_device_t)); |
| 146 | |
| 147 | if(!dev) |
| 148 | return -ENOMEM; |
| 149 | |
| 150 | memset(dev, 0, sizeof(*dev)); |
| 151 | |
| 152 | dev->common.tag = HARDWARE_DEVICE_TAG; |
| 153 | dev->common.version = LIGHTS_DEVICE_API_VERSION_2_0; |
| 154 | dev->common.module = (struct hw_module_t*)module; |
| 155 | dev->common.close = (int (*)(struct hw_device_t*))close_lights; |
| 156 | dev->set_light = set_light_backlight; |
| 157 | |
| 158 | *device = (struct hw_device_t*)dev; |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static struct hw_module_methods_t lights_module_methods = { |
| 163 | .open = open_lights, |
| 164 | }; |
| 165 | |
| 166 | /* |
| 167 | * The lights Module |
| 168 | */ |
| 169 | struct hw_module_t HAL_MODULE_INFO_SYM = { |
| 170 | .tag = HARDWARE_MODULE_TAG, |
| 171 | .version_major = 1, |
| 172 | .version_minor = 0, |
| 173 | .id = LIGHTS_HARDWARE_MODULE_ID, |
| 174 | .name = "lights Module", |
| 175 | .author = "Google, Inc.", |
| 176 | .methods = &lights_module_methods, |
| 177 | }; |