Received: by 2002:a25:f815:0:0:0:0:0 with SMTP id u21csp2900911ybd; Mon, 24 Jun 2019 15:02:06 -0700 (PDT) X-Google-Smtp-Source: APXvYqxZTzqSIcV6zO+cEMkiKKPz1oLdg/qSHFWXLR3GCGom5L5aPrgnBbiT+YaL4yJkFnnMONHQ X-Received: by 2002:a17:90a:fa07:: with SMTP id cm7mr27694012pjb.115.1561413726201; Mon, 24 Jun 2019 15:02:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561413726; cv=none; d=google.com; s=arc-20160816; b=Rn6Kao3jbBKHhztq1kmNUhCt3CzLEqJAkl2q3ldXa3EXEfmET9eUrVeH0fTEkFZx6n VHA70/wBLgrtWsHYvRWgk0+nE24roFbSZWKe1+ABro4AcsSbPEDg7CrY4X0wpIOR0VUW hMFyibaKvvMwJtQ34EdaH8XigUwnv5uf8eLDNb9KvRuatRpTNU79BGIShQ9qXJ2sDQDg CI1P4IzrVbR56M53x8VBtCl81h1OpV6pj1Mm+9ascDMlIAHwg7PZX3BT/Tec9HbCoAJo kdu3BopCjyIMGxnm5bjGMdig62uzZLt9/BpyFOfXWxApm1jY2WdqEnu1jrIJesCRBFmq wjvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=uZKHvCx8L/nRTrUb/ohywx0d3IXX3vcHyiOoGdIF2Vg=; b=Fl/DaUAMrKBF2yq9gaBWdnbv3WARggbEJulvwH//EXJp4QcpcSfLml7XaBfs8rsfHb RvFTjty2p9CfI+jTbx1G5sxib1DYlls40A6G292NvDIL9iz8JuPO0U51DAKsRPcCcqvV nqub9vfmLT2C3jLc+qktZN3od56ZEMUBdUoo/VECyUP12z+mLlnmrd01BR+jlLNYvHFq Y1obHHP0dMJ3PBNRsR2oIZ6XA0mC1glaY3OZu1HSi1iONAczxrzsk4xCKzr1VPoB8Few Cj56MbXrqIakzPYmFmC2Dx/xOgwgD78hBJnozFVZR6o2aQd5/5QAazO/J9in2QjI5d+Y HjOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="G/bUhQsl"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p97si653187pjp.34.2019.06.24.15.01.50; Mon, 24 Jun 2019 15:02:06 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="G/bUhQsl"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731899AbfFXUba (ORCPT + 99 others); Mon, 24 Jun 2019 16:31:30 -0400 Received: from mail-pl1-f194.google.com ([209.85.214.194]:36567 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731819AbfFXUb0 (ORCPT ); Mon, 24 Jun 2019 16:31:26 -0400 Received: by mail-pl1-f194.google.com with SMTP id k8so7552545plt.3 for ; Mon, 24 Jun 2019 13:31:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=uZKHvCx8L/nRTrUb/ohywx0d3IXX3vcHyiOoGdIF2Vg=; b=G/bUhQslRBb9yDeN/Ju1v2H78Jn3pcV6lvi6ZsyGVCuBbzJ+oIhAcvU04QfS10U/4R pdE3J6hWbwxyP4QcHwwtH00UNcd1LgYEz2XOvY5wmOr4tiTSrA/9uD0OATvj9bqrmoPn id+vaY8rgFoQ9n7vUdhFL4XPoNNii/WHKNs9A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=uZKHvCx8L/nRTrUb/ohywx0d3IXX3vcHyiOoGdIF2Vg=; b=CAMdTPt4BbKPwk8uttgMaF6queNWcQE+4iEo72FWDe5HMgrKyN1tknDC1qKa1djDl1 jrYozWkBZz+NXF+zLKP0Vv/h5YKO79moBy6GSvkYI5rhPCLNZI4xK93wLUIbdmUyxnk3 9LiBXBkP4k5HCM2KKiml7XxfmduOoIM8cI/2NKv1cVqs7NOTRO+C3KlFDkaSZEtRe04W LOLSxZ+3uK6UaGCe4CWfWD27QxPpuHiMVFPf8NF+6JvWJkXN2n5rg4xStbQsHpjfl4P/ p4vi+X4O5X7ih0zZbQu3aGhNrh9k/yIMrYzwskokYAv3VNK6fl3ZWdqxX90yzJIas/+J Ng1w== X-Gm-Message-State: APjAAAX+HAO46GCWZJj+aWHcbtyZDnkrRKWEqJb9huUhmHI6/pt5fxZT f32LUAtWiL/CDnffRt453xm6dg== X-Received: by 2002:a17:902:2006:: with SMTP id n6mr94261265pla.232.1561408286127; Mon, 24 Jun 2019 13:31:26 -0700 (PDT) Received: from localhost ([2620:15c:202:1:75a:3f6e:21d:9374]) by smtp.gmail.com with ESMTPSA id r2sm21887470pfl.67.2019.06.24.13.31.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 24 Jun 2019 13:31:25 -0700 (PDT) From: Matthias Kaehlcke To: Thierry Reding , Lee Jones , Daniel Thompson , Jingoo Han , Bartlomiej Zolnierkiewicz Cc: linux-pwm@vger.kernel.org, dri-devel@lists.freedesktop.org, linux-fbdev@vger.kernel.org, linux-kernel@vger.kernel.org, Enric Balletbo i Serra , Douglas Anderson , Brian Norris , Pavel Machek , Jacek Anaszewski , Matthias Kaehlcke Subject: [PATCH v2 4/4] backlight: pwm_bl: Set scale type for brightness curves specified in the DT Date: Mon, 24 Jun 2019 13:31:13 -0700 Message-Id: <20190624203114.93277-5-mka@chromium.org> X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog In-Reply-To: <20190624203114.93277-1-mka@chromium.org> References: <20190624203114.93277-1-mka@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Check if a brightness curve specified in the device tree is linear or not and set the corresponding property accordingly. This makes the scale type available to userspace via the 'scale' sysfs attribute. To determine if a curve is linear it is compared to a interpolated linear curve between min and max brightness. The curve is considered linear if no value deviates more than +/-5% of ${brightness_range} from their interpolated value. Signed-off-by: Matthias Kaehlcke Acked-by: Daniel Thompson --- Changes in v2: - use 128 (power of two) instead of 100 as factor for the slope - add comment about max quantization error - added Daniel's 'Acked-by' tag --- drivers/video/backlight/pwm_bl.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/drivers/video/backlight/pwm_bl.c b/drivers/video/backlight/pwm_bl.c index f067fe7aa35d..2297fb4af49d 100644 --- a/drivers/video/backlight/pwm_bl.c +++ b/drivers/video/backlight/pwm_bl.c @@ -404,6 +404,31 @@ int pwm_backlight_brightness_default(struct device *dev, } #endif +static bool pwm_backlight_is_linear(struct platform_pwm_backlight_data *data) +{ + unsigned int nlevels = data->max_brightness + 1; + unsigned int min_val = data->levels[0]; + unsigned int max_val = data->levels[nlevels - 1]; + /* + * Multiplying by 128 means that even in pathological cases such + * as (max_val - min_val) == nlevels the error at max_val is less + * than 1%. + */ + unsigned int slope = (128 * (max_val - min_val)) / nlevels; + unsigned int margin = (max_val - min_val) / 20; /* 5% */ + int i; + + for (i = 1; i < nlevels; i++) { + unsigned int linear_value = min_val + ((i * slope) / 128); + unsigned int delta = abs(linear_value - data->levels[i]); + + if (delta > margin) + return false; + } + + return true; +} + static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb) { struct device_node *node = pb->dev->of_node; @@ -567,6 +592,11 @@ static int pwm_backlight_probe(struct platform_device *pdev) pb->levels = data->levels; } + + if (pwm_backlight_is_linear(data)) + props.scale = BACKLIGHT_SCALE_LINEAR; + else + props.scale = BACKLIGHT_SCALE_NON_LINEAR; } else if (!data->max_brightness) { /* * If no brightness levels are provided and max_brightness is -- 2.22.0.410.gd8fdbe21b5-goog