blob: 2c627448620e76ad4dad28c5ea83244b72433c60 [file] [log] [blame]
Sungmin Choib5148892012-07-02 17:00:07 -07001/*
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
18// #define LOG_NDEBUG 0
Sungmin Choib5148892012-07-02 17:00:07 -070019
20#include <cutils/log.h>
21
22#include <stdint.h>
23#include <string.h>
24#include <unistd.h>
25#include <errno.h>
26#include <fcntl.h>
27#include <pthread.h>
28
29#include <sys/ioctl.h>
30#include <sys/types.h>
31
32#include <hardware/lights.h>
33
34/******************************************************************************/
35
36static pthread_once_t g_init = PTHREAD_ONCE_INIT;
37static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
38static struct light_state_t g_notification;
39static struct light_state_t g_battery;
40static int g_attention = 0;
41
42char const*const RED_LED_FILE
43 = "/sys/class/leds/red/brightness";
44
45char const*const GREEN_LED_FILE
46 = "/sys/class/leds/green/brightness";
47
48char const*const BLUE_LED_FILE
49 = "/sys/class/leds/blue/brightness";
50
51char const*const LCD_FILE
52 = "/sys/class/leds/lcd-backlight/brightness";
53
samin.ryud57c7d52012-08-03 23:59:41 +090054char const*const RED_FREQ_FILE
55 = "/sys/class/leds/red/device/grpfreq";
56
57char const*const RED_PWM_FILE
58 = "/sys/class/leds/red/device/grppwm";
59
60char const*const RED_BLINK_FILE
61 = "/sys/class/leds/red/device/blink";
62
63char const*const LED_LOCK_UPDATE_FILE
64 = "/sys/class/leds/red/device/lock";
65
Sungmin Choib5148892012-07-02 17:00:07 -070066/**
67 * device methods
68 */
69
70void init_globals(void)
71{
72 // init the mutex
73 pthread_mutex_init(&g_lock, NULL);
74}
75
76static int
77write_int(char const* path, int value)
78{
79 int fd;
80 static int already_warned = 0;
81
82 fd = open(path, O_RDWR);
83 if (fd >= 0) {
84 char buffer[20];
85 int bytes = sprintf(buffer, "%d\n", value);
86 int amt = write(fd, buffer, bytes);
87 close(fd);
88 return amt == -1 ? -errno : 0;
89 } else {
90 if (already_warned == 0) {
91 ALOGE("write_int failed to open %s\n", path);
92 already_warned = 1;
93 }
94 return -errno;
95 }
96}
97
98static int
99is_lit(struct light_state_t const* state)
100{
101 return state->color & 0x00ffffff;
102}
103
104static int
105rgb_to_brightness(struct light_state_t const* state)
106{
107 int color = state->color & 0x00ffffff;
108 return ((77*((color>>16)&0x00ff))
109 + (150*((color>>8)&0x00ff)) + (29*(color&0x00ff))) >> 8;
110}
111
112static int
113set_light_backlight(struct light_device_t* dev,
114 struct light_state_t const* state)
115{
116 int err = 0;
117 int brightness = rgb_to_brightness(state);
118 pthread_mutex_lock(&g_lock);
119 err = write_int(LCD_FILE, brightness);
120 pthread_mutex_unlock(&g_lock);
121 return err;
122}
123
124static int
125set_speaker_light_locked(struct light_device_t* dev,
126 struct light_state_t const* state)
127{
128 int len;
129 int alpha, red, green, blue;
130 int blink, freq, pwm;
131 int onMS, offMS;
132 unsigned int colorRGB;
133
134 switch (state->flashMode) {
135 case LIGHT_FLASH_TIMED:
136 onMS = state->flashOnMS;
137 offMS = state->flashOffMS;
138 break;
139 case LIGHT_FLASH_NONE:
140 default:
141 onMS = 0;
142 offMS = 0;
143 break;
144 }
145
146 colorRGB = state->color;
147
148#if 0
samin.ryud57c7d52012-08-03 23:59:41 +0900149 ALOGD("set_speaker_light_locked mode %d, colorRGB=%08X, onMS=%d, offMS=%d\n",
150 state->flashMode, colorRGB, onMS, offMS);
Sungmin Choib5148892012-07-02 17:00:07 -0700151#endif
152
153 red = (colorRGB >> 16) & 0xFF;
154 green = (colorRGB >> 8) & 0xFF;
155 blue = colorRGB & 0xFF;
156
Sungmin Choib5148892012-07-02 17:00:07 -0700157 if (onMS > 0 && offMS > 0) {
158 int totalMS = onMS + offMS;
159
160 // the LED appears to blink about once per second if freq is 20
161 // 1000ms / 20 = 50
162 freq = totalMS / 50;
163 // pwm specifies the ratio of ON versus OFF
164 // pwm = 0 -> always off
165 // pwm = 255 => always on
166 pwm = (onMS * 255) / totalMS;
167
168 // the low 4 bits are ignored, so round up if necessary
169 if (pwm > 0 && pwm < 16)
170 pwm = 16;
171
172 blink = 1;
173 } else {
174 blink = 0;
175 freq = 0;
176 pwm = 0;
177 }
178
samin.ryud57c7d52012-08-03 23:59:41 +0900179 write_int(LED_LOCK_UPDATE_FILE, 1); // for LED On/Off synchronization
180
181 write_int(RED_LED_FILE, red);
182 write_int(GREEN_LED_FILE, green);
183 write_int(BLUE_LED_FILE, blue);
184
Sungmin Choib5148892012-07-02 17:00:07 -0700185 if (blink) {
samin.ryud57c7d52012-08-03 23:59:41 +0900186 write_int(RED_FREQ_FILE, freq);
187 write_int(RED_PWM_FILE, pwm);
Sungmin Choib5148892012-07-02 17:00:07 -0700188 }
samin.ryud57c7d52012-08-03 23:59:41 +0900189 write_int(RED_BLINK_FILE, blink);
190
191 write_int(LED_LOCK_UPDATE_FILE, 0);
Sungmin Choib5148892012-07-02 17:00:07 -0700192
193 return 0;
194}
195
196static void
197handle_speaker_battery_locked(struct light_device_t* dev)
198{
199 if (is_lit(&g_battery)) {
200 set_speaker_light_locked(dev, &g_battery);
201 } else {
202 set_speaker_light_locked(dev, &g_notification);
203 }
204}
205
206static int
Sungmin Choib5148892012-07-02 17:00:07 -0700207set_light_notifications(struct light_device_t* dev,
208 struct light_state_t const* state)
209{
210 pthread_mutex_lock(&g_lock);
211 g_notification = *state;
212 handle_speaker_battery_locked(dev);
213 pthread_mutex_unlock(&g_lock);
214 return 0;
215}
216
217static int
218set_light_attention(struct light_device_t* dev,
219 struct light_state_t const* state)
220{
221 pthread_mutex_lock(&g_lock);
222 if (state->flashMode == LIGHT_FLASH_HARDWARE) {
223 g_attention = state->flashOnMS;
224 } else if (state->flashMode == LIGHT_FLASH_NONE) {
225 g_attention = 0;
226 }
227 handle_speaker_battery_locked(dev);
228 pthread_mutex_unlock(&g_lock);
229 return 0;
230}
231
232
233/** Close the lights device */
234static int
235close_lights(struct light_device_t *dev)
236{
237 if (dev) {
238 free(dev);
239 }
240 return 0;
241}
242
243
244/******************************************************************************/
245
246/**
247 * module methods
248 */
249
250/** Open a new instance of a lights device using name */
251static int open_lights(const struct hw_module_t* module, char const* name,
252 struct hw_device_t** device)
253{
254 int (*set_light)(struct light_device_t* dev,
255 struct light_state_t const* state);
256
257 if (0 == strcmp(LIGHT_ID_BACKLIGHT, name))
258 set_light = set_light_backlight;
Sungmin Choib5148892012-07-02 17:00:07 -0700259 else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name))
260 set_light = set_light_notifications;
261 else if (0 == strcmp(LIGHT_ID_ATTENTION, name))
262 set_light = set_light_attention;
263 else
264 return -EINVAL;
265
266 pthread_once(&g_init, init_globals);
267
268 struct light_device_t *dev = malloc(sizeof(struct light_device_t));
269 memset(dev, 0, sizeof(*dev));
270
271 dev->common.tag = HARDWARE_DEVICE_TAG;
272 dev->common.version = 0;
273 dev->common.module = (struct hw_module_t*)module;
274 dev->common.close = (int (*)(struct hw_device_t*))close_lights;
275 dev->set_light = set_light;
276
277 *device = (struct hw_device_t*)dev;
278 return 0;
279}
280
281static struct hw_module_methods_t lights_module_methods = {
282 .open = open_lights,
283};
284
285/*
286 * The lights Module
287 */
288struct hw_module_t HAL_MODULE_INFO_SYM = {
289 .tag = HARDWARE_MODULE_TAG,
290 .version_major = 1,
291 .version_minor = 0,
292 .id = LIGHTS_HARDWARE_MODULE_ID,
293 .name = "lights Module",
294 .author = "Google, Inc.",
295 .methods = &lights_module_methods,
296};