blob: 14946c5c5e5f021e3fdf00316c262b99a0d6f60a [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>
33
Christopher N. Hesseae271482016-12-01 15:51:12 +010034#include "samsung_lights.h"
Christopher N. Hesse79a9b152016-01-26 12:17:55 +010035
36#define COLOR_MASK 0x00ffffff
37
Christopher N. Hessef3c6a422016-12-05 22:11:55 +010038#define MAX_INPUT_BRIGHTNESS 255
39
Christopher N. Hesse79a9b152016-01-26 12:17:55 +010040static pthread_once_t g_init = PTHREAD_ONCE_INIT;
41static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
42
Christopher N. Hessef3c6a422016-12-05 22:11:55 +010043struct backlight_config {
44 int cur_brightness, max_brightness;
45};
46
Christopher N. Hesse79a9b152016-01-26 12:17:55 +010047struct led_config {
48 unsigned int color;
49 int delay_on, delay_off;
50};
51
Christopher N. Hessef3c6a422016-12-05 22:11:55 +010052static struct backlight_config g_backlight; // For panel backlight
Christopher N. Hesse79a9b152016-01-26 12:17:55 +010053static struct led_config g_leds[3]; // For battery, notifications, and attention.
54static int g_cur_led = -1; // Presently showing LED of the above.
55
56void init_g_lock(void)
57{
58 pthread_mutex_init(&g_lock, NULL);
59}
60
Christopher N. Hessef3c6a422016-12-05 22:11:55 +010061static int read_int(char const *path)
62{
63 int fd, len;
64 int num_bytes = 10;
65 char buf[11];
66 int retval;
67
68 fd = open(path, O_RDONLY);
69 if (fd < 0) {
70 ALOGE("%s: failed to open %s\n", __func__, path);
71 goto fail;
72 }
73
74 len = read(fd, buf, num_bytes - 1);
75 if (len < 0) {
76 ALOGE("%s: failed to read from %s\n", __func__, path);
77 goto fail;
78 }
79
80 buf[len] = '\0';
81 close(fd);
82
83 // no endptr, decimal base
84 retval = strtol(buf, NULL, 10);
85 return retval == 0 ? -1 : retval;
86
87fail:
88 if (fd >= 0)
89 close(fd);
90 return -1;
91}
92
Christopher N. Hesse79a9b152016-01-26 12:17:55 +010093static int write_int(char const *path, int value)
94{
95 int fd;
96 static int already_warned;
97
98 already_warned = 0;
99
100 ALOGV("write_int: path %s, value %d", path, value);
101 fd = open(path, O_RDWR);
102
103 if (fd >= 0) {
104 char buffer[20];
105 int bytes = sprintf(buffer, "%d\n", value);
106 int amt = write(fd, buffer, bytes);
107 close(fd);
108 return amt == -1 ? -errno : 0;
109 } else {
110 if (already_warned == 0) {
111 ALOGE("write_int failed to open %s\n", path);
112 already_warned = 1;
113 }
114 return -errno;
115 }
116}
117
118static int write_str(char const *path, const char* value)
119{
120 int fd;
121 static int already_warned;
122
123 already_warned = 0;
124
125 ALOGV("write_str: path %s, value %s", path, value);
126 fd = open(path, O_RDWR);
127
128 if (fd >= 0) {
129 int amt = write(fd, value, strlen(value));
130 close(fd);
131 return amt == -1 ? -errno : 0;
132 } else {
133 if (already_warned == 0) {
134 ALOGE("write_str failed to open %s\n", path);
135 already_warned = 1;
136 }
137 return -errno;
138 }
139}
140
141static int rgb_to_brightness(struct light_state_t const *state)
142{
143 int color = state->color & COLOR_MASK;
144
145 return ((77*((color>>16) & 0x00ff))
146 + (150*((color>>8) & 0x00ff)) + (29*(color & 0x00ff))) >> 8;
147}
148
149static int set_light_backlight(struct light_device_t *dev __unused,
150 struct light_state_t const *state)
151{
152 int err = 0;
153 int brightness = rgb_to_brightness(state);
Christopher N. Hessef3c6a422016-12-05 22:11:55 +0100154 int max_brightness = g_backlight.max_brightness;
155
156 /*
157 * If our max panel brightness is > 255, apply linear scaling across the
158 * accepted range.
159 */
160 if (max_brightness > MAX_INPUT_BRIGHTNESS) {
161 int old_brightness = brightness;
162 brightness = brightness * max_brightness / MAX_INPUT_BRIGHTNESS;
163 ALOGV("%s: scaling brightness %d => %d\n", __func__,
164 old_brightness, brightness);
165 }
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100166
167 pthread_mutex_lock(&g_lock);
Christopher N. Hesseae271482016-12-01 15:51:12 +0100168 err = write_int(PANEL_BRIGHTNESS_NODE, brightness);
Christopher N. Hessef3c6a422016-12-05 22:11:55 +0100169 if (err == 0)
170 g_backlight.cur_brightness = brightness;
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100171
172 pthread_mutex_unlock(&g_lock);
173 return err;
174}
175
176static int set_light_buttons(struct light_device_t* dev __unused,
177 struct light_state_t const* state)
178{
179 int err = 0;
180 int on = (state->color & COLOR_MASK);
181
182 pthread_mutex_lock(&g_lock);
183
Christopher N. Hesseae271482016-12-01 15:51:12 +0100184 err = write_int(BUTTON_BRIGHTNESS_NODE, on ? 1 : 0);
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100185
186 pthread_mutex_unlock(&g_lock);
187
188 return err;
189}
190
191static int close_lights(struct light_device_t *dev)
192{
193 ALOGV("close_light is called");
194 if (dev)
195 free(dev);
196
197 return 0;
198}
199
200/* LEDs */
201static int write_leds(const struct led_config *led)
202{
203 static const struct led_config led_off = {0, 0, 0};
204
205 char blink[32];
206 int count, err;
207 int color;
208
209 if (led == NULL)
210 led = &led_off;
211
212 count = snprintf(blink,
213 sizeof(blink) - 1,
214 "0x%08x %d %d",
215 led->color,
216 led->delay_on,
217 led->delay_off);
218 if (count < 0) {
219 return -errno;
220 } else if ((unsigned int)count >= sizeof(blink)-1) {
221 ALOGE("%s: Truncated string: blink=\"%s\".", __func__, blink);
222 return -EINVAL;
223 }
224
225 ALOGV("%s: color=0x%08x, delay_on=%d, delay_off=%d, blink=\"%s\".",
226 __func__, led->color, led->delay_on, led->delay_off, blink);
227
228 /* Add '\n' here to make the above log message clean. */
229 blink[count] = '\n';
230 blink[count+1] = '\0';
231
232 pthread_mutex_lock(&g_lock);
Christopher N. Hesseae271482016-12-01 15:51:12 +0100233 err = write_str(LED_BLINK_NODE, blink);
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100234 pthread_mutex_unlock(&g_lock);
235
236 return err;
237}
238
239static int set_light_leds(struct light_state_t const *state, int type)
240{
241 struct led_config *led;
242 int err = 0;
243
244 ALOGV("%s: type=%d, color=0x%010x, fM=%d, fOnMS=%d, fOffMs=%d.", __func__,
245 type, state->color,state->flashMode, state->flashOnMS, state->flashOffMS);
246
247 if (type < 0 || (size_t)type >= sizeof(g_leds)/sizeof(g_leds[0])) {
248 return -EINVAL;
249 }
250
251 /* type is one of:
252 * 0. battery
253 * 1. notifications
254 * 2. attention
255 * which are multiplexed onto the same physical LED in the above order. */
256 led = &g_leds[type];
257
258 switch (state->flashMode) {
259 case LIGHT_FLASH_NONE:
260 /* Set LED to a solid color, spec is unclear on the exact behavior here. */
261 led->delay_on = led->delay_off = 0;
262 break;
263 case LIGHT_FLASH_TIMED:
264 case LIGHT_FLASH_HARDWARE:
265 led->delay_on = state->flashOnMS;
266 led->delay_off = state->flashOffMS;
267 break;
268 default:
269 return -EINVAL;
270 }
271
272 led->color = state->color & COLOR_MASK;
273
274 if (led->color > 0) {
275 /* This LED is lit. */
276 if (type >= g_cur_led) {
277 /* And it has the highest priority, so show it. */
278 err = write_leds(led);
279 g_cur_led = type;
280 }
281 } else {
282 /* This LED is not (any longer) lit. */
283 if (type == g_cur_led) {
284 /* But it is currently showing, switch to a lower-priority LED. */
285 int i;
286
287 for (i = type-1; i >= 0; i--) {
288 if (g_leds[i].color > 0) {
289 /* Found a lower-priority LED to switch to. */
290 err = write_leds(&g_leds[i]);
291 goto switched;
292 }
293 }
294
295 /* No LEDs are lit, turn off. */
296 err = write_leds(NULL);
297switched:
298 g_cur_led = i;
299 }
300 }
301
302 return err;
303}
304
305static int set_light_leds_battery(struct light_device_t *dev __unused,
306 struct light_state_t const *state)
307{
308 return set_light_leds(state, 0);
309}
310
311static int set_light_leds_notifications(struct light_device_t *dev __unused,
312 struct light_state_t const *state)
313{
314 return set_light_leds(state, 1);
315}
316
317static int set_light_leds_attention(struct light_device_t *dev __unused,
318 struct light_state_t const *state)
319{
320 struct light_state_t fixed;
321
322 memcpy(&fixed, state, sizeof(fixed));
323
324 /* The framework does odd things with the attention lights, fix them up to
325 * do something sensible here. */
326 switch (fixed.flashMode) {
327 case LIGHT_FLASH_NONE:
328 /* LightsService.Light::stopFlashing calls with non-zero color. */
329 fixed.color = 0;
330 break;
331 case LIGHT_FLASH_HARDWARE:
332 /* PowerManagerService::setAttentionLight calls with onMS=3, offMS=0, which
333 * just makes for a slightly-dimmer LED. */
334 if (fixed.flashOnMS > 0 && fixed.flashOffMS == 0)
335 fixed.flashMode = LIGHT_FLASH_NONE;
336 fixed.color = 0x000000ff;
337 break;
338 }
339
340 return set_light_leds(&fixed, 2);
341}
342
343static int open_lights(const struct hw_module_t *module, char const *name,
344 struct hw_device_t **device)
345{
346 int (*set_light)(struct light_device_t *dev,
347 struct light_state_t const *state);
348
349 if (0 == strcmp(LIGHT_ID_BACKLIGHT, name))
350 set_light = set_light_backlight;
351 else if (0 == strcmp(LIGHT_ID_BUTTONS, name))
352 set_light = set_light_buttons;
353 else if (0 == strcmp(LIGHT_ID_BATTERY, name))
354 set_light = set_light_leds_battery;
355 else if (0 == strcmp(LIGHT_ID_NOTIFICATIONS, name))
356 set_light = set_light_leds_notifications;
357 else if (0 == strcmp(LIGHT_ID_ATTENTION, name))
358 set_light = set_light_leds_attention;
359 else
360 return -EINVAL;
361
Christopher N. Hessef3c6a422016-12-05 22:11:55 +0100362 int max_brightness = read_int(PANEL_MAX_BRIGHTNESS_NODE);
363 if (max_brightness < 0) {
364 ALOGE("%s: failed to read max panel brightness, fallback to 255!",
365 __func__);
366 max_brightness = 255;
367 }
368 g_backlight.max_brightness = max_brightness;
369
Christopher N. Hesse79a9b152016-01-26 12:17:55 +0100370 pthread_once(&g_init, init_g_lock);
371
372 struct light_device_t *dev = malloc(sizeof(struct light_device_t));
373 memset(dev, 0, sizeof(*dev));
374
375 dev->common.tag = HARDWARE_DEVICE_TAG;
376 dev->common.version = 0;
377 dev->common.module = (struct hw_module_t *)module;
378 dev->common.close = (int (*)(struct hw_device_t *))close_lights;
379 dev->set_light = set_light;
380
381 *device = (struct hw_device_t *)dev;
382
383 return 0;
384}
385
386static struct hw_module_methods_t lights_module_methods = {
387 .open = open_lights,
388};
389
390struct hw_module_t HAL_MODULE_INFO_SYM = {
391 .tag = HARDWARE_MODULE_TAG,
392 .version_major = 1,
393 .version_minor = 0,
394 .id = LIGHTS_HARDWARE_MODULE_ID,
395 .name = "Samsung Lights Module",
396 .author = "The CyanogenMod Project",
397 .methods = &lights_module_methods,
398};