From patchwork Thu Sep 22 22:04:33 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 76819 Delivered-To: patches@linaro.org Received: by 10.140.106.72 with SMTP id d66csp258360qgf; Thu, 22 Sep 2016 15:04:40 -0700 (PDT) X-Received: by 10.66.251.131 with SMTP id zk3mr7157006pac.176.1474581880421; Thu, 22 Sep 2016 15:04:40 -0700 (PDT) Return-Path: Received: from mail-pa0-x230.google.com (mail-pa0-x230.google.com. [2607:f8b0:400e:c03::230]) by mx.google.com with ESMTPS id u8si3899364paz.216.2016.09.22.15.04.40 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Sep 2016 15:04:40 -0700 (PDT) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 2607:f8b0:400e:c03::230 as permitted sender) client-ip=2607:f8b0:400e:c03::230; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org; spf=pass (google.com: domain of john.stultz@linaro.org designates 2607:f8b0:400e:c03::230 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE dis=NONE) header.from=linaro.org Received: by mail-pa0-x230.google.com with SMTP id hm5so33188503pac.0 for ; Thu, 22 Sep 2016 15:04:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=DDl2PZjR2h2ALjF1B6KZJq3os4i4aa+Er0x7oqcv8GE=; b=gGW5jNYIPE1tHi7bsW8GwoXdmJVwAbCVg7b6F8M7sFCpsnwDl1I16gGt/BbBNYyWqa d4E0v1OFkCC9VQOP/z0rOyJG8uMj4qkdHuRjLlQCf3OlAVKUZkxwyjNkIXZSCdlapf6p aYXUcSKw3s9NhNqqrZS4lCcMsh/GZaxn3COtw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=DDl2PZjR2h2ALjF1B6KZJq3os4i4aa+Er0x7oqcv8GE=; b=AWLde8m5fYZumX0HX9Sy/OO3rXuCA7s4g/Hm+s/+Uk203IgutIEerBnkOa0uI2+gLA TPGabg63YwV1CmefAX0r0KeFYqqvrAmUJr9S2OPfcH7bTOXL3/8zxlGTXHwye+WReK53 A3Y04I5j2utFJ0o2BrDSpWs1Xt93mBSOnOplYWxy5wzShVylJiLAmQ3lGN+c6fHJqUMG RTEFZL6GZrDG5no7NGWnd/iuLvNGl5BFj9e9d/C6QdokEsD8nfYAbPgoFUVTI7SZu/B6 uVc7WKg8koVPsn0Wu6kLKfjyZ9zx/b6ThD7yCCyV7LHdpwO2ElJ1eqPnSvIvccEhb2K3 7J3A== X-Gm-Message-State: AE9vXwOfMJ93FwsBgGIr96AqVVcEPz9bzLNzCrqV95IjMePMj0UW/1zlRcM9j067PAmIOgGYiCw= X-Received: by 10.66.119.162 with SMTP id kv2mr7140464pab.14.1474581880148; Thu, 22 Sep 2016 15:04:40 -0700 (PDT) Return-Path: Received: from localhost.localdomain (c-73-67-244-238.hsd1.or.comcast.net. [73.67.244.238]) by smtp.gmail.com with ESMTPSA id v24sm6332531pfd.1.2016.09.22.15.04.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 22 Sep 2016 15:04:39 -0700 (PDT) From: John Stultz To: Rob Herring , Amit Pundir , Vishal Bhoj , Michael Scott Cc: John Stultz Subject: [PATCH 2/2] generic: lights: Add logic to handle max_brightness normalization Date: Thu, 22 Sep 2016 15:04:33 -0700 Message-Id: <1474581873-1389-2-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1474581873-1389-1-git-send-email-john.stultz@linaro.org> References: <1474581873-1389-1-git-send-email-john.stultz@linaro.org> Rob pointed out I was missing normalization for panels that use different scales from 0-255. So this patch checks the max_brightness value and adds a normalization function to rescale the value. Signed-off-by: John Stultz --- lights/lights.c | 47 ++++++++++++++++++++++++++++++++++++++++++++--- ueventd.rc | 1 + 2 files changed, 45 insertions(+), 3 deletions(-) -- 1.9.1 diff --git a/lights/lights.c b/lights/lights.c index 245a066..0c631b8 100644 --- a/lights/lights.c +++ b/lights/lights.c @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -37,6 +38,8 @@ char const* const BACKLIGHT_PATH_BASE = "/sys/class/backlight/"; char backlight_path[1024]; +long brightness_max; + static int write_int(char const *path, int value) { int fd; @@ -59,6 +62,20 @@ static int write_int(char const *path, int value) } } +static int read_int(char const *path) +{ + char buffer[20]; + int amt; + int fd = open(path, O_RDWR); + if (fd < 0) + return -errno; + + amt = read(fd, buffer, 20); + if (amt > 0) + return atoi(buffer); + return -1; +} + static int rgb_to_brightness(struct light_state_t const *state) { int color = state->color & 0x00ffffff; @@ -67,13 +84,33 @@ static int rgb_to_brightness(struct light_state_t const *state) (29 * (color & 0x00ff))) >> 8; } + +static int normalize_brightness(int brightness, int max_brightness) +{ + int ret; + + /* + * API specifies argb which we convert to a 8bit + * brightness value. This function then normalizes it + * to the max_brightness range given + */ + ret = brightness * max_brightness; + ret = ret / 256; + + return ret; +} + static int set_light_backlight(struct light_device_t *dev, struct light_state_t const *state) { + char brightness_path[1024]; int err; - int brightness = rgb_to_brightness(state); + int brightness; - err = write_int(backlight_path, brightness); + snprintf(brightness_path, 1024, "%s/brightness", backlight_path); + brightness = rgb_to_brightness(state); + brightness = normalize_brightness(brightness, brightness_max); + err = write_int(brightness_path, brightness); return err; } @@ -95,6 +132,7 @@ static int open_lights(const struct hw_module_t *module, char const *name, struct light_state_t const *state); DIR *dir; struct dirent *entry; + char buffer[1024]; if (dev == NULL) { ALOGE("failed to allocate memory"); @@ -117,10 +155,13 @@ static int open_lights(const struct hw_module_t *module, char const *name, if (entry->d_type == DT_DIR) { continue; } - sprintf(backlight_path, "%s%s/brightness", BACKLIGHT_PATH_BASE, entry->d_name); + sprintf(backlight_path, "%s%s", BACKLIGHT_PATH_BASE, entry->d_name); } while (entry = readdir(dir)); closedir(dir); + snprintf(buffer, 1024, "%s/max_brightness", backlight_path); + brightness_max = read_int(buffer); + ALOGE("JDB: brightness_max: %ld\n", brightness_max); dev->common.tag = HARDWARE_DEVICE_TAG; dev->common.version = 0; diff --git a/ueventd.rc b/ueventd.rc index c56b776..c7fdb17 100644 --- a/ueventd.rc +++ b/ueventd.rc @@ -14,6 +14,7 @@ /sys/bus/iio/devices/iio:device* scan_elements/in_anglvel_z_en 0660 system system /sys/bus/iio/devices/trigger* name 0660 system system /sys/class/backlight* brightness 0664 system system +/sys/class/backlight* max_brightness 0664 system system /dev/sw_sync 0660 root graphics /dev/iio:device0 0660 system system