blob: ad6edc63d1788cd4c96209fa69dceb4a05ed4dc7 [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
Simon Shields310d1992017-02-18 20:55:11 +11004 * Copyright (C) 2017 The LineageOS Project
Christopher N. Hesse79a9b152016-01-26 12:17:55 +01005 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#define LOG_TAG "SamsungLightsHAL"
20/* #define LOG_NDEBUG 0 */
21
22#include <cutils/log.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <errno.h>
27#include <fcntl.h>
28#include <pthread.h>
29
30#include <sys/ioctl.h>
31#include <sys/types.h>
32
33#include <hardware/lights.h>
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +010034#include <liblights/samsung_lights_helper.h>
Christopher N. Hesse79a9b152016-01-26 12:17:55 +010035
Christopher N. Hesseae271482016-12-01 15:51:12 +010036#include "samsung_lights.h"
Christopher N. Hesse79a9b152016-01-26 12:17:55 +010037
38#define COLOR_MASK 0x00ffffff
39
Christopher N. Hessef3c6a422016-12-05 22:11:55 +010040#define MAX_INPUT_BRIGHTNESS 255
41
Christopher N. Hessea8fe2a12016-12-31 17:14:47 +010042enum component_mask_t {
43 COMPONENT_BACKLIGHT = 0x1,
44 COMPONENT_BUTTON_LIGHT = 0x2,
45 COMPONENT_LED = 0x4,
46};
47
Simon Shields310d1992017-02-18 20:55:11 +110048enum light_t {
49 TYPE_BATTERY = 0,
50 TYPE_NOTIFICATION = 1,
51 TYPE_ATTENTION = 2,
52};
53
Christopher N. Hessea8fe2a12016-12-31 17:14:47 +010054// Assume backlight is always supported
55static int hw_components = COMPONENT_BACKLIGHT;
56
Christopher N. Hesse79a9b152016-01-26 12:17:55 +010057static pthread_once_t g_init = PTHREAD_ONCE_INIT;
58static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
59
Christopher N. Hessef3c6a422016-12-05 22:11:55 +010060struct backlight_config {
61 int cur_brightness, max_brightness;
62};
63
Christopher N. Hesse79a9b152016-01-26 12:17:55 +010064struct led_config {
65 unsigned int color;
66 int delay_on, delay_off;
67};
68
Christopher N. Hessef3c6a422016-12-05 22:11:55 +010069static struct backlight_config g_backlight; // For panel backlight
Christopher N. Hesse79a9b152016-01-26 12:17:55 +010070static struct led_config g_leds[3]; // For battery, notifications, and attention.
71static int g_cur_led = -1; // Presently showing LED of the above.
72
Christopher N. Hessea8fe2a12016-12-31 17:14:47 +010073void check_component_support()
74{
75 if (access(BUTTON_BRIGHTNESS_NODE, W_OK) == 0)
76 hw_components |= COMPONENT_BUTTON_LIGHT;
77 if (access(LED_BLINK_NODE, W_OK) == 0)
78 hw_components |= COMPONENT_LED;
79}
80
Christopher N. Hesse79a9b152016-01-26 12:17:55 +010081void init_g_lock(void)
82{
83 pthread_mutex_init(&g_lock, NULL);
84}
85
Christopher N. Hesse79a9b152016-01-26 12:17:55 +010086static int write_str(char const *path, const char* value)
87{
88 int fd;
89 static int already_warned;
90
91 already_warned = 0;
92
93 ALOGV("write_str: path %s, value %s", path, value);
94 fd = open(path, O_RDWR);
95
96 if (fd >= 0) {
97 int amt = write(fd, value, strlen(value));
98 close(fd);
99 return amt == -1 ? -errno : 0;
100 } else {
101 if (already_warned == 0) {
Christopher N. Hesseb5b95002017-03-04 15:04:16 +0100102 ALOGE("write_str failed to open %s", path);
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100103 already_warned = 1;
104 }
105 return -errno;
106 }
107}
108
109static int rgb_to_brightness(struct light_state_t const *state)
110{
111 int color = state->color & COLOR_MASK;
112
113 return ((77*((color>>16) & 0x00ff))
114 + (150*((color>>8) & 0x00ff)) + (29*(color & 0x00ff))) >> 8;
115}
116
117static int set_light_backlight(struct light_device_t *dev __unused,
118 struct light_state_t const *state)
119{
120 int err = 0;
121 int brightness = rgb_to_brightness(state);
Christopher N. Hessef3c6a422016-12-05 22:11:55 +0100122 int max_brightness = g_backlight.max_brightness;
123
124 /*
Luca Stefania0422092017-03-13 14:53:14 +0100125 * If max panel brightness is not the default (255),
126 * apply linear scaling across the accepted range.
Christopher N. Hessef3c6a422016-12-05 22:11:55 +0100127 */
Luca Stefania0422092017-03-13 14:53:14 +0100128 if (max_brightness != MAX_INPUT_BRIGHTNESS) {
Christopher N. Hessef3c6a422016-12-05 22:11:55 +0100129 int old_brightness = brightness;
130 brightness = brightness * max_brightness / MAX_INPUT_BRIGHTNESS;
Christopher N. Hesseb5b95002017-03-04 15:04:16 +0100131 ALOGV("%s: scaling brightness %d => %d", __func__,
Christopher N. Hessef3c6a422016-12-05 22:11:55 +0100132 old_brightness, brightness);
133 }
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100134
135 pthread_mutex_lock(&g_lock);
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +0100136 err = set_cur_panel_brightness(brightness);
Christopher N. Hessef3c6a422016-12-05 22:11:55 +0100137 if (err == 0)
138 g_backlight.cur_brightness = brightness;
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100139
140 pthread_mutex_unlock(&g_lock);
141 return err;
142}
143
144static int set_light_buttons(struct light_device_t* dev __unused,
145 struct light_state_t const* state)
146{
147 int err = 0;
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100148 pthread_mutex_lock(&g_lock);
Dominggoes Isakhd81a0a32017-05-25 14:49:46 +0200149 int brightness = (state->color & COLOR_MASK) ? 1 : 0;
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100150
Dominggoes Isakhd81a0a32017-05-25 14:49:46 +0200151#ifdef VAR_BUTTON_BRIGHTNESS
152 brightness = rgb_to_brightness(state);
153#endif
154 err = set_cur_button_brightness(brightness);
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100155 pthread_mutex_unlock(&g_lock);
156
157 return err;
158}
159
160static int close_lights(struct light_device_t *dev)
161{
162 ALOGV("close_light is called");
163 if (dev)
164 free(dev);
165
166 return 0;
167}
168
169/* LEDs */
170static int write_leds(const struct led_config *led)
171{
172 static const struct led_config led_off = {0, 0, 0};
173
174 char blink[32];
175 int count, err;
176 int color;
177
178 if (led == NULL)
179 led = &led_off;
180
181 count = snprintf(blink,
182 sizeof(blink) - 1,
183 "0x%08x %d %d",
184 led->color,
185 led->delay_on,
186 led->delay_off);
187 if (count < 0) {
188 return -errno;
189 } else if ((unsigned int)count >= sizeof(blink)-1) {
190 ALOGE("%s: Truncated string: blink=\"%s\".", __func__, blink);
191 return -EINVAL;
192 }
193
Christopher N. Hesseb5b95002017-03-04 15:04:16 +0100194 ALOGV("%s: color=0x%08x, delay_on=%d, delay_off=%d, blink=%s",
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100195 __func__, led->color, led->delay_on, led->delay_off, blink);
196
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100197 pthread_mutex_lock(&g_lock);
Christopher N. Hesseae271482016-12-01 15:51:12 +0100198 err = write_str(LED_BLINK_NODE, blink);
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100199 pthread_mutex_unlock(&g_lock);
200
201 return err;
202}
203
Simon Shields310d1992017-02-18 20:55:11 +1100204static int calibrate_color(int color, int brightness)
205{
206 int red = ((color >> 16) & 0xFF) * LED_ADJUSTMENT_R;
207 int green = ((color >> 8) & 0xFF) * LED_ADJUSTMENT_G;
208 int blue = (color & 0xFF) * LED_ADJUSTMENT_B;
209
210 return (((red * brightness) / 255) << 16) + (((green * brightness) / 255) << 8) + ((blue * brightness) / 255);
211}
212
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100213static int set_light_leds(struct light_state_t const *state, int type)
214{
215 struct led_config *led;
216 int err = 0;
Simon Shields310d1992017-02-18 20:55:11 +1100217 int adjusted_brightness;
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100218
219 ALOGV("%s: type=%d, color=0x%010x, fM=%d, fOnMS=%d, fOffMs=%d.", __func__,
220 type, state->color,state->flashMode, state->flashOnMS, state->flashOffMS);
221
222 if (type < 0 || (size_t)type >= sizeof(g_leds)/sizeof(g_leds[0])) {
223 return -EINVAL;
224 }
225
226 /* type is one of:
227 * 0. battery
228 * 1. notifications
229 * 2. attention
230 * which are multiplexed onto the same physical LED in the above order. */
231 led = &g_leds[type];
232
233 switch (state->flashMode) {
234 case LIGHT_FLASH_NONE:
235 /* Set LED to a solid color, spec is unclear on the exact behavior here. */
236 led->delay_on = led->delay_off = 0;
237 break;
238 case LIGHT_FLASH_TIMED:
239 case LIGHT_FLASH_HARDWARE:
240 led->delay_on = state->flashOnMS;
241 led->delay_off = state->flashOffMS;
242 break;
243 default:
244 return -EINVAL;
245 }
246
Simon Shields310d1992017-02-18 20:55:11 +1100247 switch (type) {
248 case TYPE_BATTERY:
249 adjusted_brightness = LED_BRIGHTNESS_BATTERY;
250 break;
251 case TYPE_NOTIFICATION:
252 adjusted_brightness = LED_BRIGHTNESS_NOTIFICATION;
253 break;
254 case TYPE_ATTENTION:
255 adjusted_brightness = LED_BRIGHTNESS_ATTENTION;
256 break;
257 default:
258 adjusted_brightness = 255;
259 }
260
261
262
263 led->color = calibrate_color(state->color & COLOR_MASK, adjusted_brightness);
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100264
265 if (led->color > 0) {
266 /* This LED is lit. */
267 if (type >= g_cur_led) {
268 /* And it has the highest priority, so show it. */
269 err = write_leds(led);
270 g_cur_led = type;
271 }
272 } else {
273 /* This LED is not (any longer) lit. */
274 if (type == g_cur_led) {
275 /* But it is currently showing, switch to a lower-priority LED. */
276 int i;
277
278 for (i = type-1; i >= 0; i--) {
279 if (g_leds[i].color > 0) {
280 /* Found a lower-priority LED to switch to. */
281 err = write_leds(&g_leds[i]);
282 goto switched;
283 }
284 }
285
286 /* No LEDs are lit, turn off. */
287 err = write_leds(NULL);
288switched:
289 g_cur_led = i;
290 }
291 }
292
293 return err;
294}
295
296static int set_light_leds_battery(struct light_device_t *dev __unused,
297 struct light_state_t const *state)
298{
Simon Shields310d1992017-02-18 20:55:11 +1100299 return set_light_leds(state, TYPE_BATTERY);
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100300}
301
302static int set_light_leds_notifications(struct light_device_t *dev __unused,
303 struct light_state_t const *state)
304{
Simon Shields310d1992017-02-18 20:55:11 +1100305 return set_light_leds(state, TYPE_NOTIFICATION);
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100306}
307
308static int set_light_leds_attention(struct light_device_t *dev __unused,
309 struct light_state_t const *state)
310{
311 struct light_state_t fixed;
312
313 memcpy(&fixed, state, sizeof(fixed));
314
315 /* The framework does odd things with the attention lights, fix them up to
316 * do something sensible here. */
317 switch (fixed.flashMode) {
318 case LIGHT_FLASH_NONE:
319 /* LightsService.Light::stopFlashing calls with non-zero color. */
320 fixed.color = 0;
321 break;
322 case LIGHT_FLASH_HARDWARE:
323 /* PowerManagerService::setAttentionLight calls with onMS=3, offMS=0, which
324 * just makes for a slightly-dimmer LED. */
325 if (fixed.flashOnMS > 0 && fixed.flashOffMS == 0)
326 fixed.flashMode = LIGHT_FLASH_NONE;
327 fixed.color = 0x000000ff;
328 break;
329 }
330
Simon Shields310d1992017-02-18 20:55:11 +1100331 return set_light_leds(&fixed, TYPE_ATTENTION);
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100332}
333
334static int open_lights(const struct hw_module_t *module, char const *name,
335 struct hw_device_t **device)
336{
Christopher N. Hessea8fe2a12016-12-31 17:14:47 +0100337 int requested_component;
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100338 int (*set_light)(struct light_device_t *dev,
339 struct light_state_t const *state);
340
Christopher N. Hessea8fe2a12016-12-31 17:14:47 +0100341 check_component_support();
342
343 if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) {
344 requested_component = COMPONENT_BACKLIGHT;
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100345 set_light = set_light_backlight;
Christopher N. Hessea8fe2a12016-12-31 17:14:47 +0100346 } else if (0 == strcmp(LIGHT_ID_BUTTONS, name)) {
347 requested_component = COMPONENT_BUTTON_LIGHT;
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100348 set_light = set_light_buttons;
Christopher N. Hessea8fe2a12016-12-31 17:14:47 +0100349 } else if (0 == strcmp(LIGHT_ID_BATTERY, name)) {
350 requested_component = COMPONENT_LED;
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100351 set_light = set_light_leds_battery;
Christopher N. Hessea8fe2a12016-12-31 17:14:47 +0100352 } else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name)) {
353 requested_component = COMPONENT_LED;
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100354 set_light = set_light_leds_notifications;
Christopher N. Hessea8fe2a12016-12-31 17:14:47 +0100355 } else if (0 == strcmp(LIGHT_ID_ATTENTION, name)) {
356 requested_component = COMPONENT_LED;
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100357 set_light = set_light_leds_attention;
Christopher N. Hessea8fe2a12016-12-31 17:14:47 +0100358 } else {
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100359 return -EINVAL;
Christopher N. Hessea8fe2a12016-12-31 17:14:47 +0100360 }
361
362 if ((hw_components & requested_component) == 0) {
363 ALOGV("%s: component 0x%x not supported by device", __func__,
364 requested_component);
365 return -EINVAL;
366 }
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100367
Christopher N. Hesse898e1fe2016-12-07 12:12:23 +0100368 int max_brightness = get_max_panel_brightness();
Christopher N. Hessef3c6a422016-12-05 22:11:55 +0100369 if (max_brightness < 0) {
370 ALOGE("%s: failed to read max panel brightness, fallback to 255!",
371 __func__);
372 max_brightness = 255;
373 }
374 g_backlight.max_brightness = max_brightness;
375
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100376 pthread_once(&g_init, init_g_lock);
377
378 struct light_device_t *dev = malloc(sizeof(struct light_device_t));
379 memset(dev, 0, sizeof(*dev));
380
381 dev->common.tag = HARDWARE_DEVICE_TAG;
382 dev->common.version = 0;
383 dev->common.module = (struct hw_module_t *)module;
384 dev->common.close = (int (*)(struct hw_device_t *))close_lights;
385 dev->set_light = set_light;
386
387 *device = (struct hw_device_t *)dev;
388
389 return 0;
390}
391
392static struct hw_module_methods_t lights_module_methods = {
393 .open = open_lights,
394};
395
396struct hw_module_t HAL_MODULE_INFO_SYM = {
397 .tag = HARDWARE_MODULE_TAG,
398 .version_major = 1,
399 .version_minor = 0,
400 .id = LIGHTS_HARDWARE_MODULE_ID,
401 .name = "Samsung Lights Module",
402 .author = "The CyanogenMod Project",
403 .methods = &lights_module_methods,
404};