Received: by 10.213.65.68 with SMTP id h4csp782148imn; Fri, 6 Apr 2018 08:50:06 -0700 (PDT) X-Google-Smtp-Source: AIpwx48X/6YpgJKmxi514J8/ri6vGRAqnm0d5YRV8LFUvNoIYzT9MEpiMkKEAyOB21pXkSwvaEdD X-Received: by 10.98.155.12 with SMTP id r12mr20824371pfd.15.1523029806035; Fri, 06 Apr 2018 08:50:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523029806; cv=none; d=google.com; s=arc-20160816; b=1FTONwoy8WmvhrUAkxp8MMQceEyHhTHOxCJCbn4BUavMC1taTYjzSy7aWutwAL0a5A gSILkgdpThEEWZ8cun3cui2Xaw+3t72wUAxjB90LHtEnTIZGgaubHej25XFY4kza5xgd Ayk4zZxV+hQ+/9/VpoSFzJeQRicDEnse1J3zfYHFYFytizy+hyA9o/0+DW7NBzLzMcpq IukApS6fCQD7JJ5+pkvEYSjMW2CzQhFlnsUGaWtPALJp+y+w7j71ncuqEKlBXmv82rup IDJCgTMl7YZhCKvfNJDAtF6gqiNOqARBMNP5Ugqd/ZTw2Yd5f91Iuqfy9WaolyGmc+JA WERA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=Q/ML8IN4l9Pj4LYSqKkYSPo8pFQXc9oZ6Le8eDFDVII=; b=P0QYjwY1zgoqIYPafLHwPfpMYDeiYXXtphg+LBHc5GLPljLUEnBaP0FRW/+yXaLchT NdQa/oAuMu5I9ox3AvMnrvBJ1LdNWhKHFStB2EyBZZ8Bpi55ORhfR78GKtW0MnA3V6Ky bi54KROPdj9OJe1lcctWa0v4eMAZQWryUx/LQ0i10ru4UmNOodL5/A1ybcHwjtnyfnaR torzPb2/qh3XxMzMSixmZ3SgpVvF5YlYdsaQoallIlCztc+m4a+Y2vH2Y50OJGnLotef MBY+yQ4tJgIySTnsJyDAX8vjfxObmZC/yl1ZmJ5h9ezqVZtyUCdsqhbc8M645T/+O95x 55iA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=O1M5GT8L; 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=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b5-v6si10741000pli.364.2018.04.06.08.49.28; Fri, 06 Apr 2018 08:50:05 -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=@linaro.org header.s=google header.b=O1M5GT8L; 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=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751754AbeDFPqk (ORCPT + 99 others); Fri, 6 Apr 2018 11:46:40 -0400 Received: from mail-wm0-f66.google.com ([74.125.82.66]:38481 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751417AbeDFPqh (ORCPT ); Fri, 6 Apr 2018 11:46:37 -0400 Received: by mail-wm0-f66.google.com with SMTP id i3so4145206wmf.3 for ; Fri, 06 Apr 2018 08:46:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=Q/ML8IN4l9Pj4LYSqKkYSPo8pFQXc9oZ6Le8eDFDVII=; b=O1M5GT8L/q3y9lXmct5LyVIHdCWLVhp0w6USprSeBWLi0PWPqyavgRdTr0WJ3Krwmo veutnPGLJokG1XsfQiV4Gnp4hoykPOI+M8r2+OOz8iYGdf0uNDKx0silOEoup4bRIGct HpNqdFPb2Q/vum3P8X1xNEtQ6QVHRiamjiFGM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=Q/ML8IN4l9Pj4LYSqKkYSPo8pFQXc9oZ6Le8eDFDVII=; b=Ctd9RSttqKMPmAYAS5Y5ERhUlKrdxebeybE9koAZN1Xt0dANOn9dgBGPjMHFNCr0O/ oeszGo4gI9U2sXBDPcIu0tnSIIEoI7P8RiyHUzQreW3nua3u8dQEbkYp7osCsuXR5MGu DA2Te9NY7DwuNAWE6BO0w/8wgBsI+8SW7TqZAFgI37Zn4xHMW4EOeeLUiYh/fgAdw8Ei HXxn+uLsb2mZZNmxR6VRqHm18/S8r+6BH2VjUPJc9WYdniY3DtV2LEM/eGHInWR7TRi3 ELnly4GxSoodvhq3nckMl+xixzjwksPiJJmhj6w+EZGWs33qeT8FN4cpGSk9WrJEMaSo sCBA== X-Gm-Message-State: AElRT7FNk/b6G+1IDLvBnJfniZ8mR0pilaKL3A+CYlKtFwHNfRw8Qnbu H5S284VJl2EPVe8/Gz4bG6r/kQ== X-Received: by 10.28.10.83 with SMTP id 80mr16013446wmk.70.1523029596186; Fri, 06 Apr 2018 08:46:36 -0700 (PDT) Received: from holly.lan (cpc141214-aztw34-2-0-cust773.18-1.cable.virginm.net. [86.9.19.6]) by smtp.gmail.com with ESMTPSA id 39sm19763882wry.89.2018.04.06.08.46.34 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 06 Apr 2018 08:46:35 -0700 (PDT) Date: Fri, 6 Apr 2018 16:46:32 +0100 From: Daniel Thompson To: Enric Balletbo i Serra Cc: Doug Anderson , Pavel Machek , Rob Herring , Jingoo Han , Richard Purdie , Jacek Anaszewski , Brian Norris , Guenter Roeck , Lee Jones , Alexandru Stan , linux-leds@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, kernel@collabora.com Subject: Re: [PATCH v3 1/4] backlight: pwm_bl: linear interpolation between brightness-levels Message-ID: <20180406154632.ttt5qgsbwsveipwh@holly.lan> References: <20180208113032.27810-1-enric.balletbo@collabora.com> <20180208113032.27810-2-enric.balletbo@collabora.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180208113032.27810-2-enric.balletbo@collabora.com> User-Agent: NeoMutt/20180223 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Feb 08, 2018 at 12:30:29PM +0100, Enric Balletbo i Serra wrote: > Setting num-interpolated-steps in the dts will allow you to have linear > interpolation between values of brightness-levels. This way a high > resolution pwm duty cycle can be used without having to list out every > possible value in the dts. This system also allows for gamma corrected > values. > > The most simple example is interpolate between two brightness values a > number of steps, this can be done setting the following in the dts: > > brightness-levels = <0 65535>; > num-interpolated-steps = <1024>; > default-brightness-level = <512>; > > This will create a brightness-level table with the following values: > > <0 63 126 189 252 315 378 441 ... 64260 64323 64386 64449 65535> > > Another use case can be describe a gamma corrected curve, as we have > better sensitivity at low luminance than high luminance we probably > want have smaller steps for low brightness levels values and bigger > steps for high brightness levels values. This can be achieved with > the following in the dts: > > brightness-levels = <0 4096 65535>; > num-interpolated-steps = <1024>; > default-brightness-level = <512>; > > This will create a brightness-levels table with the following values: > > <0 4 8 12 16 20 ... 4096 4156 4216 4276 ... 65535> > > Signed-off-by: Enric Balletbo i Serra Acked-by: Daniel Thompson > --- > Changes since v2: > Requested by Daniel Thompson: > - Use a devres alloc for table creatiion and then just swap pointers. > - No need to use calloc because the loop initializes every element. > Changes since v1: > - None. > > drivers/video/backlight/pwm_bl.c | 83 ++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 83 insertions(+) > > diff --git a/drivers/video/backlight/pwm_bl.c b/drivers/video/backlight/pwm_bl.c > index 8e3f1245f5c5..f0a108ab570a 100644 > --- a/drivers/video/backlight/pwm_bl.c > +++ b/drivers/video/backlight/pwm_bl.c > @@ -147,7 +147,11 @@ static int pwm_backlight_parse_dt(struct device *dev, > struct platform_pwm_backlight_data *data) > { > struct device_node *node = dev->of_node; > + unsigned int num_levels = 0; > + unsigned int levels_count; > + unsigned int num_steps; > struct property *prop; > + unsigned int *table; > int length; > u32 value; > int ret; > @@ -167,6 +171,7 @@ static int pwm_backlight_parse_dt(struct device *dev, > /* read brightness levels from DT property */ > if (data->max_brightness > 0) { > size_t size = sizeof(*data->levels) * data->max_brightness; > + unsigned int i, j, n = 0; > > data->levels = devm_kzalloc(dev, size, GFP_KERNEL); > if (!data->levels) > @@ -184,6 +189,84 @@ static int pwm_backlight_parse_dt(struct device *dev, > return ret; > > data->dft_brightness = value; > + > + /* > + * This property is optional, if is set enables linear > + * interpolation between each of the values of brightness levels > + * and creates a new pre-computed table. > + */ > + of_property_read_u32(node, "num-interpolated-steps", > + &num_steps); > + > + /* > + * Make sure that there is at least two entries in the > + * brightness-levels table, otherwise we can't interpolate > + * between two points. > + */ > + if (num_steps) { > + if (data->max_brightness < 2) { > + dev_err(dev, "can't interpolate\n"); > + return -EINVAL; > + } > + > + /* > + * Recalculate the number of brightness levels, now > + * taking in consideration the number of interpolated > + * steps between two levels. > + */ > + for (i = 0; i < data->max_brightness - 1; i++) { > + if ((data->levels[i + 1] - data->levels[i]) / > + num_steps) > + num_levels += num_steps; > + else > + num_levels++; > + } > + num_levels++; > + dev_dbg(dev, "new number of brightness levels: %d\n", > + num_levels); > + > + /* > + * Create a new table of brightness levels with all the > + * interpolated steps. > + */ > + size = sizeof(*table) * num_levels; > + table = devm_kzalloc(dev, size, GFP_KERNEL); > + if (!table) > + return -ENOMEM; > + > + /* Fill the interpolated table. */ > + levels_count = 0; > + for (i = 0; i < data->max_brightness - 1; i++) { > + value = data->levels[i]; > + n = (data->levels[i + 1] - value) / num_steps; > + if (n > 0) { > + for (j = 0; j < num_steps; j++) { > + table[levels_count] = value; > + value += n; > + levels_count++; > + } > + } else { > + table[levels_count] = data->levels[i]; > + levels_count++; > + } > + } > + table[levels_count] = data->levels[i]; > + > + /* > + * As we use interpolation lets remove current > + * brightness levels table and replace for the > + * new interpolated table. > + */ > + devm_kfree(dev, data->levels); > + data->levels = table; > + > + /* > + * Reassign max_brightness value to the new total number > + * of brightness levels. > + */ > + data->max_brightness = num_levels; > + } > + > data->max_brightness--; > } > > -- > 2.15.1 >