blob: 36a049b66db6349621454599b933266b7b79c842 [file] [log] [blame]
Christopher N. Hesse79a9b152016-01-26 12:17:55 +01001/*
2 * Copyright (C) 2013 The Android Open Source Project
Christopher N. Hesseae271482016-12-01 15:51:12 +01003 * Copyright (C) 2015-2016 The CyanogenMod Project
Christopher N. Hesse79a9b152016-01-26 12:17:55 +01004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#define LOG_TAG "SamsungLightsHAL"
19/* #define LOG_NDEBUG 0 */
20
21#include <cutils/log.h>
22#include <stdlib.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>
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010033#include <liblights/samsung_lights_helper.h>
Christopher N. Hesse79a9b152016-01-26 12:17:55 +010034
Christopher N. Hesseae271482016-12-01 15:51:12 +010035#include "samsung_lights.h"
Christopher N. Hesse79a9b152016-01-26 12:17:55 +010036
37#define COLOR_MASK 0x00ffffff
38
Christopher N. Hessef3c6a422016-12-05 22:11:55 +010039#define MAX_INPUT_BRIGHTNESS 255
40
Christopher N. Hessea8fe2a12016-12-31 17:14:47 +010041enum component_mask_t {
42 COMPONENT_BACKLIGHT = 0x1,
43 COMPONENT_BUTTON_LIGHT = 0x2,
44 COMPONENT_LED = 0x4,
45};
46
47// Assume backlight is always supported
48static int hw_components = COMPONENT_BACKLIGHT;
49
Christopher N. Hesse79a9b152016-01-26 12:17:55 +010050static pthread_once_t g_init = PTHREAD_ONCE_INIT;
51static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
52
Christopher N. Hessef3c6a422016-12-05 22:11:55 +010053struct backlight_config {
54 int cur_brightness, max_brightness;
55};
56
Christopher N. Hesse79a9b152016-01-26 12:17:55 +010057struct led_config {
58 unsigned int color;
59 int delay_on, delay_off;
60};
61
Christopher N. Hessef3c6a422016-12-05 22:11:55 +010062static struct backlight_config g_backlight; // For panel backlight
Christopher N. Hesse79a9b152016-01-26 12:17:55 +010063static struct led_config g_leds[3]; // For battery, notifications, and attention.
64static int g_cur_led = -1; // Presently showing LED of the above.
65
Christopher N. Hessea8fe2a12016-12-31 17:14:47 +010066void check_component_support()
67{
68 if (access(BUTTON_BRIGHTNESS_NODE, W_OK) == 0)
69 hw_components |= COMPONENT_BUTTON_LIGHT;
70 if (access(LED_BLINK_NODE, W_OK) == 0)
71 hw_components |= COMPONENT_LED;
72}
73
Christopher N. Hesse79a9b152016-01-26 12:17:55 +010074void init_g_lock(void)
75{
76 pthread_mutex_init(&g_lock, NULL);
77}
78
Christopher N. Hesse79a9b152016-01-26 12:17:55 +010079static int write_str(char const *path, const char* value)
80{
81 int fd;
82 static int already_warned;
83
84 already_warned = 0;
85
86 ALOGV("write_str: path %s, value %s", path, value);
87 fd = open(path, O_RDWR);
88
89 if (fd >= 0) {
90 int amt = write(fd, value, strlen(value));
91 close(fd);
92 return amt == -1 ? -errno : 0;
93 } else {
94 if (already_warned == 0) {
95 ALOGE("write_str failed to open %s\n", path);
96 already_warned = 1;
97 }
98 return -errno;
99 }
100}
101
102static int rgb_to_brightness(struct light_state_t const *state)
103{
104 int color = state->color & COLOR_MASK;
105
106 return ((77*((color>>16) & 0x00ff))
107 + (150*((color>>8) & 0x00ff)) + (29*(color & 0x00ff))) >> 8;
108}
109
110static int set_light_backlight(struct light_device_t *dev __unused,
111 struct light_state_t const *state)
112{
113 int err = 0;
114 int brightness = rgb_to_brightness(state);
Christopher N. Hessef3c6a422016-12-05 22:11:55 +0100115 int max_brightness = g_backlight.max_brightness;
116
117 /*
118 * If our max panel brightness is > 255, apply linear scaling across the
119 * accepted range.
120 */
121 if (max_brightness > MAX_INPUT_BRIGHTNESS) {
122 int old_brightness = brightness;
123 brightness = brightness * max_brightness / MAX_INPUT_BRIGHTNESS;
124 ALOGV("%s: scaling brightness %d => %d\n", __func__,
125 old_brightness, brightness);
126 }
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100127
128 pthread_mutex_lock(&g_lock);
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +0100129 err = set_cur_panel_brightness(brightness);
Christopher N. Hessef3c6a422016-12-05 22:11:55 +0100130 if (err == 0)
131 g_backlight.cur_brightness = brightness;
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100132
133 pthread_mutex_unlock(&g_lock);
134 return err;
135}
136
137static int set_light_buttons(struct light_device_t* dev __unused,
138 struct light_state_t const* state)
139{
140 int err = 0;
141 int on = (state->color & COLOR_MASK);
142
143 pthread_mutex_lock(&g_lock);
144
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +0100145 err = set_cur_button_brightness(on ? 1 : 0);
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100146
147 pthread_mutex_unlock(&g_lock);
148
149 return err;
150}
151
152static int close_lights(struct light_device_t *dev)
153{
154 ALOGV("close_light is called");
155 if (dev)
156 free(dev);
157
158 return 0;
159}
160
161/* LEDs */
162static int write_leds(const struct led_config *led)
163{
164 static const struct led_config led_off = {0, 0, 0};
165
166 char blink[32];
167 int count, err;
168 int color;
169
170 if (led == NULL)
171 led = &led_off;
172
173 count = snprintf(blink,
174 sizeof(blink) - 1,
175 "0x%08x %d %d",
176 led->color,
177 led->delay_on,
178 led->delay_off);
179 if (count < 0) {
180 return -errno;
181 } else if ((unsigned int)count >= sizeof(blink)-1) {
182 ALOGE("%s: Truncated string: blink=\"%s\".", __func__, blink);
183 return -EINVAL;
184 }
185
186 ALOGV("%s: color=0x%08x, delay_on=%d, delay_off=%d, blink=\"%s\".",
187 __func__, led->color, led->delay_on, led->delay_off, blink);
188
189 /* Add '\n' here to make the above log message clean. */
190 blink[count] = '\n';
191 blink[count+1] = '\0';
192
193 pthread_mutex_lock(&g_lock);
Christopher N. Hesseae271482016-12-01 15:51:12 +0100194 err = write_str(LED_BLINK_NODE, blink);
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100195 pthread_mutex_unlock(&g_lock);
196
197 return err;
198}
199
200static int set_light_leds(struct light_state_t const *state, int type)
201{
202 struct led_config *led;
203 int err = 0;
204
205 ALOGV("%s: type=%d, color=0x%010x, fM=%d, fOnMS=%d, fOffMs=%d.", __func__,
206 type, state->color,state->flashMode, state->flashOnMS, state->flashOffMS);
207
208 if (type < 0 || (size_t)type >= sizeof(g_leds)/sizeof(g_leds[0])) {
209 return -EINVAL;
210 }
211
212 /* type is one of:
213 * 0. battery
214 * 1. notifications
215 * 2. attention
216 * which are multiplexed onto the same physical LED in the above order. */
217 led = &g_leds[type];
218
219 switch (state->flashMode) {
220 case LIGHT_FLASH_NONE:
221 /* Set LED to a solid color, spec is unclear on the exact behavior here. */
222 led->delay_on = led->delay_off = 0;
223 break;
224 case LIGHT_FLASH_TIMED:
225 case LIGHT_FLASH_HARDWARE:
226 led->delay_on = state->flashOnMS;
227 led->delay_off = state->flashOffMS;
228 break;
229 default:
230 return -EINVAL;
231 }
232
233 led->color = state->color & COLOR_MASK;
234
235 if (led->color > 0) {
236 /* This LED is lit. */
237 if (type >= g_cur_led) {
238 /* And it has the highest priority, so show it. */
239 err = write_leds(led);
240 g_cur_led = type;
241 }
242 } else {
243 /* This LED is not (any longer) lit. */
244 if (type == g_cur_led) {
245 /* But it is currently showing, switch to a lower-priority LED. */
246 int i;
247
248 for (i = type-1; i >= 0; i--) {
249 if (g_leds[i].color > 0) {
250 /* Found a lower-priority LED to switch to. */
251 err = write_leds(&g_leds[i]);
252 goto switched;
253 }
254 }
255
256 /* No LEDs are lit, turn off. */
257 err = write_leds(NULL);
258switched:
259 g_cur_led = i;
260 }
261 }
262
263 return err;
264}
265
266static int set_light_leds_battery(struct light_device_t *dev __unused,
267 struct light_state_t const *state)
268{
269 return set_light_leds(state, 0);
270}
271
272static int set_light_leds_notifications(struct light_device_t *dev __unused,
273 struct light_state_t const *state)
274{
275 return set_light_leds(state, 1);
276}
277
278static int set_light_leds_attention(struct light_device_t *dev __unused,
279 struct light_state_t const *state)
280{
281 struct light_state_t fixed;
282
283 memcpy(&fixed, state, sizeof(fixed));
284
285 /* The framework does odd things with the attention lights, fix them up to
286 * do something sensible here. */
287 switch (fixed.flashMode) {
288 case LIGHT_FLASH_NONE:
289 /* LightsService.Light::stopFlashing calls with non-zero color. */
290 fixed.color = 0;
291 break;
292 case LIGHT_FLASH_HARDWARE:
293 /* PowerManagerService::setAttentionLight calls with onMS=3, offMS=0, which
294 * just makes for a slightly-dimmer LED. */
295 if (fixed.flashOnMS > 0 && fixed.flashOffMS == 0)
296 fixed.flashMode = LIGHT_FLASH_NONE;
297 fixed.color = 0x000000ff;
298 break;
299 }
300
301 return set_light_leds(&fixed, 2);
302}
303
304static int open_lights(const struct hw_module_t *module, char const *name,
305 struct hw_device_t **device)
306{
Christopher N. Hessea8fe2a12016-12-31 17:14:47 +0100307 int requested_component;
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100308 int (*set_light)(struct light_device_t *dev,
309 struct light_state_t const *state);
310
Christopher N. Hessea8fe2a12016-12-31 17:14:47 +0100311 check_component_support();
312
313 if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) {
314 requested_component = COMPONENT_BACKLIGHT;
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100315 set_light = set_light_backlight;
Christopher N. Hessea8fe2a12016-12-31 17:14:47 +0100316 } else if (0 == strcmp(LIGHT_ID_BUTTONS, name)) {
317 requested_component = COMPONENT_BUTTON_LIGHT;
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100318 set_light = set_light_buttons;
Christopher N. Hessea8fe2a12016-12-31 17:14:47 +0100319 } else if (0 == strcmp(LIGHT_ID_BATTERY, name)) {
320 requested_component = COMPONENT_LED;
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100321 set_light = set_light_leds_battery;
Christopher N. Hessea8fe2a12016-12-31 17:14:47 +0100322 } else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name)) {
323 requested_component = COMPONENT_LED;
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100324 set_light = set_light_leds_notifications;
Christopher N. Hessea8fe2a12016-12-31 17:14:47 +0100325 } else if (0 == strcmp(LIGHT_ID_ATTENTION, name)) {
326 requested_component = COMPONENT_LED;
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100327 set_light = set_light_leds_attention;
Christopher N. Hessea8fe2a12016-12-31 17:14:47 +0100328 } else {
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100329 return -EINVAL;
Christopher N. Hessea8fe2a12016-12-31 17:14:47 +0100330 }
331
332 if ((hw_components & requested_component) == 0) {
333 ALOGV("%s: component 0x%x not supported by device", __func__,
334 requested_component);
335 return -EINVAL;
336 }
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100337
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +0100338 int max_brightness = get_max_panel_brightness();
Christopher N. Hessef3c6a422016-12-05 22:11:55 +0100339 if (max_brightness < 0) {
340 ALOGE("%s: failed to read max panel brightness, fallback to 255!",
341 __func__);
342 max_brightness = 255;
343 }
344 g_backlight.max_brightness = max_brightness;
345
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100346 pthread_once(&g_init, init_g_lock);
347
348 struct light_device_t *dev = malloc(sizeof(struct light_device_t));
349 memset(dev, 0, sizeof(*dev));
350
351 dev->common.tag = HARDWARE_DEVICE_TAG;
352 dev->common.version = 0;
353 dev->common.module = (struct hw_module_t *)module;
354 dev->common.close = (int (*)(struct hw_device_t *))close_lights;
355 dev->set_light = set_light;
356
357 *device = (struct hw_device_t *)dev;
358
359 return 0;
360}
361
362static struct hw_module_methods_t lights_module_methods = {
363 .open = open_lights,
364};
365
366struct hw_module_t HAL_MODULE_INFO_SYM = {
367 .tag = HARDWARE_MODULE_TAG,
368 .version_major = 1,
369 .version_minor = 0,
370 .id = LIGHTS_HARDWARE_MODULE_ID,
371 .name = "Samsung Lights Module",
372 .author = "The CyanogenMod Project",
373 .methods = &lights_module_methods,
374};