aboutsummaryrefslogtreecommitdiff
path: root/liblight/lights.c
diff options
context:
space:
mode:
authorChristopher N. Hesse <raymanfx@gmail.com>2016-12-28 12:21:12 -0500
committerDavide Garberi <dade.garberi@gmail.com>2018-01-25 16:16:01 +0100
commit559deefc268c96d2fece436e23e4b86ef77d0308 (patch)
tree5542d8c9e4893d27c5bf6ad6093674fbf0884bae /liblight/lights.c
parentdb1d5c409cb76990db1cf35d660bc848f9c4cc55 (diff)
z2_plus: lights: Scale LCD brightness if needed
Change-Id: I7aade9ee626811e408e7f2d66b169a12695a27f6 Signed-off-by: Davide Garberi <dade.garberi@gmail.com>
Diffstat (limited to 'liblight/lights.c')
-rw-r--r--liblight/lights.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/liblight/lights.c b/liblight/lights.c
index 1f74712..3f3a1cf 100644
--- a/liblight/lights.c
+++ b/liblight/lights.c
@@ -54,6 +54,9 @@ static const char BLUE_LED_FILE[]
static const char LCD_FILE[]
= "/sys/class/leds/lcd-backlight/brightness";
+static const char LCD_MAX_BRIGHTNESS_FILE[]
+ = "/sys/class/leds/lcd-backlight/max_brightness";
+
static const char RED_DUTY_PCTS_FILE[]
= "/sys/class/leds/red/duty_pcts";
@@ -116,6 +119,9 @@ static int BRIGHTNESS_RAMP[RAMP_SIZE]
= { 0, 12, 25, 37, 50, 72, 85, 100 };
#define RAMP_STEP_DURATION 50
+#define DEFAULT_MAX_BRIGHTNESS 255
+int max_brightness;
+
/**
* device methods
*/
@@ -126,6 +132,38 @@ void init_globals(void)
pthread_mutex_init(&g_lock, NULL);
}
+static int read_int(char const* path)
+{
+ int fd, len;
+ int num_bytes = 10;
+ char buf[11];
+ int retval;
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0) {
+ ALOGE("%s: failed to open %s\n", __func__, path);
+ goto fail;
+ }
+
+ len = read(fd, buf, num_bytes - 1);
+ if (len < 0) {
+ ALOGE("%s: failed to read from %s\n", __func__, path);
+ goto fail;
+ }
+
+ buf[len] = '\0';
+ close(fd);
+
+ // no endptr, decimal base
+ retval = strtol(buf, NULL, 10);
+ return retval == 0 ? -1 : retval;
+
+fail:
+ if (fd >= 0)
+ close(fd);
+ return -1;
+}
+
static int
write_int(char const* path, int value)
{
@@ -193,6 +231,15 @@ set_light_backlight(struct light_device_t* dev,
if(!dev) {
return -1;
}
+
+ // If max panel brightness is not the default (255),
+ // apply linear scaling across the accepted range.
+ if (max_brightness != DEFAULT_MAX_BRIGHTNESS) {
+ int old_brightness = brightness;
+ brightness = brightness * max_brightness / DEFAULT_MAX_BRIGHTNESS;
+ ALOGV("%s: scaling brightness %d => %d\n", __func__, old_brightness, brightness);
+ }
+
pthread_mutex_lock(&g_lock);
err = write_int(LCD_FILE, brightness);
pthread_mutex_unlock(&g_lock);
@@ -424,6 +471,12 @@ static int open_lights(const struct hw_module_t* module, char const* name,
else
return -EINVAL;
+ max_brightness = read_int(LCD_MAX_BRIGHTNESS_FILE);
+ if (max_brightness < 0) {
+ ALOGE("%s: failed to read max panel brightness, fallback to 255!\n", __func__);
+ max_brightness = DEFAULT_MAX_BRIGHTNESS;
+ }
+
pthread_once(&g_init, init_globals);
struct light_device_t *dev = malloc(sizeof(struct light_device_t));