Home | History | Annotate | Download | only in liblight
      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 #include <cutils/log.h>
     19 #include <stdint.h>
     20 #include <string.h>
     21 #include <errno.h>
     22 #include <fcntl.h>
     23 #include <pthread.h>
     24 #include <sys/ioctl.h>
     25 #include <sys/types.h>
     26 #include <hardware/lights.h>
     27 
     28 static pthread_once_t g_init = PTHREAD_ONCE_INIT;
     29 static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
     30 
     31 char const *const LCD_FILE = "/sys/class/backlight/s5p_bl/brightness";
     32 
     33 static int write_int(char const *path, int value)
     34 {
     35 	int fd;
     36 	static int already_warned;
     37 
     38 	already_warned = 0;
     39 
     40 	ALOGV("write_int: path %s, value %d", path, value);
     41 	fd = open(path, O_RDWR);
     42 
     43 	if (fd >= 0) {
     44 		char buffer[20];
     45 		int bytes = sprintf(buffer, "%d\n", value);
     46 		int amt = write(fd, buffer, bytes);
     47 		close(fd);
     48 		return amt == -1 ? -errno : 0;
     49 	} else {
     50 		if (already_warned == 0) {
     51 			ALOGE("write_int failed to open %s\n", path);
     52 			already_warned = 1;
     53 		}
     54 		return -errno;
     55 	}
     56 }
     57 
     58 static int rgb_to_brightness(struct light_state_t const *state)
     59 {
     60 	int color = state->color & 0x00ffffff;
     61 
     62 	return ((77*((color>>16) & 0x00ff))
     63 		+ (150*((color>>8) & 0x00ff)) + (29*(color & 0x00ff))) >> 8;
     64 }
     65 
     66 static int set_light_backlight(struct light_device_t *dev,
     67 			struct light_state_t const *state)
     68 {
     69 	int err = 0;
     70 	int brightness = rgb_to_brightness(state);
     71 
     72 	pthread_mutex_lock(&g_lock);
     73 	err = write_int(LCD_FILE, brightness);
     74 
     75 	pthread_mutex_unlock(&g_lock);
     76 	return err;
     77 }
     78 
     79 static int close_lights(struct light_device_t *dev)
     80 {
     81 	ALOGV("close_light is called");
     82 	if (dev)
     83 		free(dev);
     84 
     85 	return 0;
     86 }
     87 
     88 static int open_lights(const struct hw_module_t *module, char const *name,
     89 						struct hw_device_t **device)
     90 {
     91 	int (*set_light)(struct light_device_t *dev,
     92 		struct light_state_t const *state);
     93 
     94 	ALOGV("open_lights: open with %s", name);
     95 
     96 	if (0 == strcmp(LIGHT_ID_BACKLIGHT, name))
     97 		set_light = set_light_backlight;
     98 	else
     99 		return -EINVAL;
    100 
    101 	pthread_mutex_init(&g_lock, NULL);
    102 
    103 	struct light_device_t *dev = malloc(sizeof(struct light_device_t));
    104 	memset(dev, 0, sizeof(*dev));
    105 
    106 	dev->common.tag = HARDWARE_DEVICE_TAG;
    107 	dev->common.version = 0;
    108 	dev->common.module = (struct hw_module_t *)module;
    109 	dev->common.close = (int (*)(struct hw_device_t *))close_lights;
    110 	dev->set_light = set_light;
    111 
    112 	*device = (struct hw_device_t *)dev;
    113 
    114 	return 0;
    115 }
    116 
    117 static struct hw_module_methods_t lights_module_methods = {
    118 	.open =  open_lights,
    119 };
    120 
    121 struct hw_module_t HAL_MODULE_INFO_SYM = {
    122 	.tag = HARDWARE_MODULE_TAG,
    123 	.version_major = 1,
    124 	.version_minor = 0,
    125 	.id = LIGHTS_HARDWARE_MODULE_ID,
    126 	.name = "lights Module",
    127 	.author = "Google, Inc.",
    128 	.methods = &lights_module_methods,
    129 };
    130