blob: 67013f982dc2f8744c1b7ca8b899c4e313a5b2e0 [file] [log] [blame]
Colin Crossebf2cdf2012-03-12 13:58:29 -07001/*
2 * Copyright (C) 2012 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
28static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
29
30char const *const LCD_FILE = "/sys/class/backlight/pwm-backlight.0/brightness";
31
32static int write_int(char const *path, int value)
33{
34 int fd;
35 static int already_warned;
36
37 ALOGV("write_int: path %s, value %d", path, value);
38 fd = open(path, O_RDWR);
39
40 if (fd >= 0) {
41 char buffer[20];
42 int bytes = sprintf(buffer, "%d\n", value);
43 int amt = write(fd, buffer, bytes);
44 close(fd);
45 return amt == -1 ? -errno : 0;
46 } else {
47 if (already_warned == 0) {
48 ALOGE("write_int failed to open %s\n", path);
49 already_warned = 1;
50 }
51 return -errno;
52 }
53}
54
55static int rgb_to_brightness(struct light_state_t const *state)
56{
57 /* use max of the RGB components for brightness */
58 int color = state->color & 0x00ffffff;
59 int red = (color >> 16) & 0x000000ff;
60 int green = (color >> 8) & 0x000000ff;
61 int blue = color & 0x000000ff;
62
63 int brightness = red;
64 if (green > brightness)
65 brightness = green;
66 if (blue > brightness)
67 brightness = blue;
68
69 return brightness;
70}
71
72static int set_light_backlight(struct light_device_t *dev,
73 struct light_state_t const *state)
74{
75 int err = 0;
76 int brightness = rgb_to_brightness(state);
77
78 pthread_mutex_lock(&g_lock);
79 err = write_int(LCD_FILE, brightness);
80
81 pthread_mutex_unlock(&g_lock);
82 return err;
83}
84
85static int close_lights(struct hw_device_t *dev)
86{
87 ALOGV("close_light is called");
88 free(dev);
89
90 return 0;
91}
92
93static int open_lights(const struct hw_module_t *module, char const *name,
94 struct hw_device_t **device)
95{
96 int (*set_light)(struct light_device_t *dev,
97 struct light_state_t const *state);
98
99 if (strcmp(LIGHT_ID_BACKLIGHT, name) == 0)
100 set_light = set_light_backlight;
101 else
102 return -EINVAL;
103
104 struct light_device_t *dev = malloc(sizeof(struct light_device_t));
105 if (!dev)
106 return -ENOMEM;
107 memset(dev, 0, sizeof(*dev));
108
109 dev->common.tag = HARDWARE_DEVICE_TAG;
110 dev->common.version = 0;
111 dev->common.module = (struct hw_module_t *)module;
112 dev->common.close = close_lights;
113 dev->set_light = set_light;
114
115 *device = (struct hw_device_t *)dev;
116
117 return 0;
118}
119
120static struct hw_module_methods_t lights_module_methods = {
121 .open = open_lights,
122};
123
124struct hw_module_t HAL_MODULE_INFO_SYM = {
125 .tag = HARDWARE_MODULE_TAG,
126 .version_major = 1,
127 .version_minor = 0,
128 .id = LIGHTS_HARDWARE_MODULE_ID,
129 .name = "lights Module",
130 .author = "Google, Inc.",
131 .methods = &lights_module_methods,
132};