OSDN Git Service

intel: panel max_brightness can be > 65536
[android-x86/hardware-liblights.git] / lights.c
1 /*
2  * Copyright (C) 2012 Stefan Seidel
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 <sys/types.h>
22 #include <dirent.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <pthread.h>
29
30 #include <hardware/lights.h>
31 #include <cutils/properties.h>
32
33 /******************************************************************************/
34
35 static pthread_once_t g_init = PTHREAD_ONCE_INIT;
36 static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
37 static int max_brightness = 255;
38 static char brightness_file[PROPERTY_VALUE_MAX] = { '\0' };
39
40 char const*const LLP_BRIGHTNESS_FILE     = "backlight.brightness_file";
41 char const*const LLP_MAX_BRIGHTNESS_FILE = "backlight.max_brightness_file";
42 char const*const LLP_MAX_BRIGHTNESS      = "backlight.max_brightness";
43
44 void init_globals(void)
45 {
46     pthread_mutex_init(&g_lock, NULL);
47 }
48
49 static int write_int(char* path, int value)
50 {
51     int fd;
52     static int already_warned = 0;
53
54     fd = open(path, O_RDWR);
55     if (fd >= 0) {
56         char buffer[20];
57         int bytes = sprintf(buffer, "%d\n", value);
58         int amt = write(fd, buffer, bytes);
59         close(fd);
60         return amt == -1 ? -errno : 0;
61     } else {
62         if (already_warned == 0) {
63             ALOGE("write_int failed to open %s\n", path);
64             already_warned = 1;
65         }
66         return -errno;
67     }
68 }
69
70 static int read_int(char* path)
71 {
72     int fd;
73
74     fd = open(path, O_RDONLY);
75     if (fd >= 0) {
76         char buffer[20];
77         int amt = read(fd, buffer, 20);
78         close(fd);
79         if (amt <= 0) return -errno;
80         int ret = -1;
81         amt = sscanf(buffer, "%d", &ret);
82         return amt == -1 ? -errno : ret;
83     }
84     return -errno;
85 }
86
87 static int rgb_to_brightness16(struct light_state_t const* state)
88 {
89     int color = state->color & 0x00ffffff;
90     return ((77*((color>>16)&0x00ff)) + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff)));
91 }
92
93 static int set_light_backlight(struct light_device_t* dev, struct light_state_t const* state)
94 {
95     int err = 0;
96     int brightness = rgb_to_brightness16(state);
97
98     if (max_brightness < 65536)
99         brightness = brightness / (65536 / (max_brightness + 1));
100     else
101         brightness = brightness * (max_brightness / 65536);
102
103     pthread_mutex_lock(&g_lock);
104     err = write_int(brightness_file, (brightness));
105     pthread_mutex_unlock(&g_lock);
106
107     return err;
108 }
109
110 static int close_lights(struct light_device_t *dev)
111 {
112     free(dev);
113     return 0;
114 }
115
116 static int check_backlight_file(char const* name, char* path)
117 {
118     int ret;
119     if (access(name, F_OK)) {
120         ret = 0;
121     } else {
122         ALOGD("Detect %s", name);
123         strcpy(path, name);
124         ret = 1; // found
125     }
126     return ret;
127 }
128
129 static int find_backlight_file(char const* file, char* path)
130 {
131     int ret = 0;
132     size_t i;
133     DIR* dir;
134     char name[PATH_MAX];
135     const char* dirname = "/sys/class/backlight";
136     const char* dirs[] = {
137         "eeepc",
138         "intel_backlight",
139         "thinkpad_screen",
140         "acpi_video0",
141     };
142
143     // Check some known dirs
144     for (i = 0; i < sizeof(dirs) / sizeof(const char*); ++i) {
145         snprintf(name, PATH_MAX, "%s/%s/%s", dirname, dirs[i], file);
146         if (check_backlight_file(name, path)) {
147             ALOGV("Using device %s", name);
148             return 1;
149         }
150     }
151
152     if ((dir = opendir(dirname))) {
153         struct dirent* de;
154         while ((de = readdir(dir))) {
155             if (de->d_name[0] != '.') {
156                 snprintf(name, PATH_MAX, "%s/%s/%s", dirname, de->d_name, file);
157                 if ((ret = check_backlight_file(name, path))) {
158                     break;
159                 }
160             }
161         }
162         closedir(dir);
163     }
164     return ret;
165 }
166
167 static int open_lights(const struct hw_module_t* module, char const* name, struct hw_device_t** device)
168 {
169     int (*set_light)(struct light_device_t* dev,
170             struct light_state_t const* state);
171
172     if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) {
173         set_light = set_light_backlight;
174         char max_b_file[PROPERTY_VALUE_MAX] = { '\0' };
175         if (property_get(LLP_MAX_BRIGHTNESS, max_b_file, NULL)) {
176             if (!sscanf(max_b_file, "%d", &max_brightness)) {
177                 ALOGE("%s system property is set to '%s', this could not be parsed as an integer!", LLP_MAX_BRIGHTNESS, max_b_file);
178                 return -EINVAL;
179             }
180         } else {
181             if (property_get(LLP_MAX_BRIGHTNESS_FILE, max_b_file, NULL) ||
182                     find_backlight_file("max_brightness", max_b_file)) {
183                 max_brightness = read_int(max_b_file);
184             } else {
185                 ALOGE("Unable to detect max_brightness. Try to set %s", LLP_MAX_BRIGHTNESS_FILE);
186                 return -EINVAL;
187             }
188         }
189         ALOGV("Read max display brightness of %d", max_brightness);
190         if (max_brightness < 1) {
191             max_brightness = 255;
192         }
193         if (!property_get(LLP_BRIGHTNESS_FILE, brightness_file, NULL) &&
194                 !find_backlight_file("brightness", brightness_file)) {
195             ALOGE("%s system property not set", LLP_BRIGHTNESS_FILE);
196             return -EINVAL;
197         }
198     } else {
199         return -EINVAL;
200     }
201
202     pthread_once(&g_init, init_globals);
203
204     struct light_device_t *dev = malloc(sizeof(struct light_device_t));
205     memset(dev, 0, sizeof(*dev));
206
207     dev->common.tag = HARDWARE_DEVICE_TAG;
208     dev->common.close = (int (*)(struct hw_device_t*))close_lights;
209     dev->common.module = (struct hw_module_t*)module;
210     dev->common.version = 0;
211     dev->set_light = set_light;
212
213     *device = (struct hw_device_t*)dev;
214     return 0;
215 }
216
217
218 static struct hw_module_methods_t lights_module_methods = {
219     .open = open_lights,
220 };
221
222 struct hw_module_t HAL_MODULE_INFO_SYM = {
223     .tag = HARDWARE_MODULE_TAG,
224     .version_major = 1,
225     .version_minor = 0,
226     .id = LIGHTS_HARDWARE_MODULE_ID,
227     .name = "Generic sysfs liblights implementation",
228     .author = "Stefan Seidel",
229     .methods = &lights_module_methods,
230 };