1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #define LOG_TAG "lights" 18 19 #include <cutils/log.h> 20 21 #include <stdint.h> 22 #include <string.h> 23 #include <unistd.h> 24 #include <errno.h> 25 #include <fcntl.h> 26 27 #include <sys/ioctl.h> 28 #include <sys/types.h> 29 30 #include <hardware/lights.h> 31 #include <hardware/hardware.h> 32 33 static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER; 34 char const* const MODE_RGB_FILE = "/sys/class/leds/indicator/ModeRGB"; 35 char const* const BACKLIGHT_FILE = "/sys/class/backlight/tegra-dsi-backlight.0/brightness"; 36 static int write_int(char const *path, int value) 37 { 38 int fd; 39 static int already_warned = -1; 40 fd = open(path, O_RDWR); 41 if (fd >= 0) { 42 char buffer[20]; 43 int bytes = snprintf(buffer, 20, "%d\n", value); 44 int amt = write(fd, buffer, bytes); 45 close(fd); 46 return amt == -1 ? -errno : 0; 47 } else { 48 if (already_warned == -1) { 49 ALOGE("write_int failed to open %s\n", path); 50 already_warned = 1; 51 } 52 return -errno; 53 } 54 } 55 56 static int rgb_to_brightness(struct light_state_t const *state) 57 { 58 int color = state->color & 0x00ffffff; 59 return ((77 * ((color >> 16) & 0x00ff)) 60 + (150 * ((color >> 8) & 0x00ff)) + 61 (29 * (color & 0x00ff))) >> 8; 62 } 63 64 static int set_light_battery(struct light_device_t* dev, 65 struct light_state_t const* state) 66 { 67 int err; 68 pthread_mutex_lock(&g_lock); 69 ALOGV("set_light_battery, flashMode:%x, color:%x", state->flashMode, state->color); 70 if(state->color) 71 err = write_int(MODE_RGB_FILE, 0x1ffffff); 72 else 73 err = write_int(MODE_RGB_FILE, 0x00); 74 pthread_mutex_unlock(&g_lock); 75 return err; 76 } 77 78 static int set_light_notifications(struct light_device_t* dev, 79 struct light_state_t const* state) 80 { 81 int err; 82 pthread_mutex_lock(&g_lock); 83 ALOGV("set_light_notifications, flashMode:%x, color:%x", state->flashMode, state->color); 84 if(state->flashMode) 85 err = write_int(MODE_RGB_FILE, 0x6ffffff); 86 else 87 err = write_int(MODE_RGB_FILE, 0x00); 88 pthread_mutex_unlock(&g_lock); 89 return err; 90 } 91 92 static int set_light_backlight(struct light_device_t *dev, 93 struct light_state_t const *state) 94 { 95 int err; 96 int brightness = rgb_to_brightness(state); 97 98 pthread_mutex_lock(&g_lock); 99 err = write_int(BACKLIGHT_FILE, brightness); 100 pthread_mutex_unlock(&g_lock); 101 102 return err; 103 } 104 105 /** Close the lights device */ 106 static int close_lights(struct light_device_t *dev) 107 { 108 if (dev) 109 free(dev); 110 return 0; 111 } 112 113 /** Open a new instance of a lights device using name */ 114 static int open_lights(const struct hw_module_t *module, char const *name, 115 struct hw_device_t **device) 116 { 117 struct light_device_t *dev = malloc(sizeof(struct light_device_t)); 118 int (*set_light) (struct light_device_t *dev, 119 struct light_state_t const *state); 120 pthread_t lighting_poll_thread; 121 ALOGV("open lights"); 122 if (dev == NULL) { 123 ALOGE("failed to allocate memory"); 124 return -1; 125 } 126 memset(dev, 0, sizeof(*dev)); 127 128 if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) 129 set_light = set_light_backlight; 130 else if (0 == strcmp(LIGHT_ID_BATTERY, name)) 131 set_light = set_light_battery; 132 else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name)) 133 set_light = set_light_notifications; 134 else 135 return -EINVAL; 136 137 pthread_mutex_init(&g_lock, NULL); 138 139 dev->common.tag = HARDWARE_DEVICE_TAG; 140 dev->common.version = 0; 141 dev->common.module = (struct hw_module_t *)module; 142 dev->common.close = (int (*)(struct hw_device_t *))close_lights; 143 dev->set_light = set_light; 144 145 *device = (struct hw_device_t *)dev; 146 147 return 0; 148 } 149 150 static struct hw_module_methods_t lights_methods = 151 { 152 .open = open_lights, 153 }; 154 155 /* 156 * The backlight Module 157 */ 158 struct hw_module_t HAL_MODULE_INFO_SYM = 159 { 160 .tag = HARDWARE_MODULE_TAG, 161 .version_major = 1, 162 .version_minor = 0, 163 .id = LIGHTS_HARDWARE_MODULE_ID, 164 .name = "Flounder lights module", 165 .author = "NVIDIA", 166 .methods = &lights_methods, 167 }; 168