Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp2352105pxb; Mon, 20 Sep 2021 19:48:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzYJA43HxTq+vKNuBwRNjlUNYswTO9DEp+tsigQ3OzY85S1XJiD+XObIxxYStQc169nCC7i X-Received: by 2002:aa7:d850:: with SMTP id f16mr32603322eds.176.1632192538960; Mon, 20 Sep 2021 19:48:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632192538; cv=none; d=google.com; s=arc-20160816; b=ee3WShP95NGEnIraCCmx+DVksCPYvvT5SfctH7+9ZPeYhaL+3YVQGgwXJaV+Z2gALj thYhyqWBQ9PF/Ti7hNj+oqf9Vg+JBi6Zy6pxlksqhYTfcyD0WR/yWrNXfQCNsEYdOrGH TMrldugU78m+s/RYD7ILlPhkjcpX/2yKc8jY6/C6se+sRebE56KsrYqi0XmI9BSQCahI GYlyr9CT4ziW3BolJlp2FK88JXATfNH0oA+d6P3I44+ySOPf11kdyscZq0uJrrB1RE7R CIfUNzfgN2mXldFFyQ68vF2KmgwP7BMHFH7lj1EQ62trI+xCg3naEj5UIqRuJgkVRiq+ LTRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=JT9qYwHNaDzRdCeDRfgqJS0pUX0lXiLbjIqSyySafJk=; b=vYn86yQhHCdqhRuWZJBFU/TpGxdglCKtHyia5/KpJ9MN9w94vQEGtgXMMtz7rWq6eI 71xxJGZDbZvbT8g5XkyG3f45/AQO2A5pmWJjKxCWadNBEcaJQ169LD3YuCoRnsv7DwjK w8tUMRjCSWKo++wzYSZp7HjsbV2gxfAil+UH+hxCuHjpODKYj98JQ03lNqR3GfpH98q5 7tg5d2je6OTHjvNd1MumoZuYZj7u3ELfx0aYyUl4ngOBDUdAzPv03PsYjB7djSEN6TJ+ rctH/OdtJ9wximkxgpMdgc6VU/F21+n3uDbGy2jrh20W066sDr1tmUObh0eQeeN41bKx aSFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=e2eg84Sp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u13si15677935eju.576.2021.09.20.19.48.35; Mon, 20 Sep 2021 19:48:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=e2eg84Sp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1380915AbhITSfD (ORCPT + 99 others); Mon, 20 Sep 2021 14:35:03 -0400 Received: from mail.kernel.org ([198.145.29.99]:49712 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379868AbhITSbA (ORCPT ); Mon, 20 Sep 2021 14:31:00 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 2229361439; Mon, 20 Sep 2021 17:27:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1632158843; bh=EEmGaJ5ungMBEXkvbcXKWJw49M4x0i3FnVbZiARRQgQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=e2eg84SpKPQLrmtTjbP5xI1uD61nD8xTm5RTjL/7rPNYed2e+pTcCkwIZM/TqRYz4 gpKWGKG7MI0B8dEUABnYj4ZFjnQJOwgiWHsi4gpvUo+tC9Fa91FFClFVa0uIFhJTJr j+cvZK/0yFqZnBRo8xpWMXexbC6jdjCb7LTtQ0uY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Stephan Gerhold , newbyte@disroot.org, Daniel Thompson , Linus Walleij , Lee Jones , Sasha Levin Subject: [PATCH 5.10 081/122] backlight: ktd253: Stabilize backlight Date: Mon, 20 Sep 2021 18:44:13 +0200 Message-Id: <20210920163918.439257788@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210920163915.757887582@linuxfoundation.org> References: <20210920163915.757887582@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Linus Walleij [ Upstream commit daa37361518bf2d1f591bbdaa7c68b2a43d7af48 ] Remove interrupt disablement during backlight setting. It is way to dangerous and makes platforms instable by having it miss vblank IRQs leading to the graphics derailing. The code is using ndelay() which is not available on platforms such as ARM and will result in 32 * udelay(1) which is substantial. Add some code to detect if an interrupt occurs during the tight loop and in that case just redo it from the top. Fixes: 5317f37e48b9 ("backlight: Add Kinetic KTD253 backlight driver") Cc: Stephan Gerhold Reported-by: newbyte@disroot.org Reviewed-by: Daniel Thompson Signed-off-by: Linus Walleij Signed-off-by: Lee Jones Signed-off-by: Sasha Levin --- drivers/video/backlight/ktd253-backlight.c | 75 ++++++++++++++++------ 1 file changed, 55 insertions(+), 20 deletions(-) diff --git a/drivers/video/backlight/ktd253-backlight.c b/drivers/video/backlight/ktd253-backlight.c index e3fee3f1f582..9d355fd989d8 100644 --- a/drivers/video/backlight/ktd253-backlight.c +++ b/drivers/video/backlight/ktd253-backlight.c @@ -25,6 +25,7 @@ #define KTD253_T_LOW_NS (200 + 10) /* Additional 10ns as safety factor */ #define KTD253_T_HIGH_NS (200 + 10) /* Additional 10ns as safety factor */ +#define KTD253_T_OFF_CRIT_NS 100000 /* 100 us, now it doesn't look good */ #define KTD253_T_OFF_MS 3 struct ktd253_backlight { @@ -34,13 +35,50 @@ struct ktd253_backlight { u16 ratio; }; +static void ktd253_backlight_set_max_ratio(struct ktd253_backlight *ktd253) +{ + gpiod_set_value_cansleep(ktd253->gpiod, 1); + ndelay(KTD253_T_HIGH_NS); + /* We always fall back to this when we power on */ +} + +static int ktd253_backlight_stepdown(struct ktd253_backlight *ktd253) +{ + /* + * These GPIO operations absolutely can NOT sleep so no _cansleep + * suffixes, and no using GPIO expanders on slow buses for this! + * + * The maximum number of cycles of the loop is 32 so the time taken + * should nominally be: + * (T_LOW_NS + T_HIGH_NS + loop_time) * 32 + * + * Architectures do not always support ndelay() and we will get a few us + * instead. If we get to a critical time limit an interrupt has likely + * occured in the low part of the loop and we need to restart from the + * top so we have the backlight in a known state. + */ + u64 ns; + + ns = ktime_get_ns(); + gpiod_set_value(ktd253->gpiod, 0); + ndelay(KTD253_T_LOW_NS); + gpiod_set_value(ktd253->gpiod, 1); + ns = ktime_get_ns() - ns; + if (ns >= KTD253_T_OFF_CRIT_NS) { + dev_err(ktd253->dev, "PCM on backlight took too long (%llu ns)\n", ns); + return -EAGAIN; + } + ndelay(KTD253_T_HIGH_NS); + return 0; +} + static int ktd253_backlight_update_status(struct backlight_device *bl) { struct ktd253_backlight *ktd253 = bl_get_data(bl); int brightness = backlight_get_brightness(bl); u16 target_ratio; u16 current_ratio = ktd253->ratio; - unsigned long flags; + int ret; dev_dbg(ktd253->dev, "new brightness/ratio: %d/32\n", brightness); @@ -62,37 +100,34 @@ static int ktd253_backlight_update_status(struct backlight_device *bl) } if (current_ratio == 0) { - gpiod_set_value_cansleep(ktd253->gpiod, 1); - ndelay(KTD253_T_HIGH_NS); - /* We always fall back to this when we power on */ + ktd253_backlight_set_max_ratio(ktd253); current_ratio = KTD253_MAX_RATIO; } - /* - * WARNING: - * The loop to set the correct current level is performed - * with interrupts disabled as it is timing critical. - * The maximum number of cycles of the loop is 32 - * so the time taken will be (T_LOW_NS + T_HIGH_NS + loop_time) * 32, - */ - local_irq_save(flags); while (current_ratio != target_ratio) { /* * These GPIO operations absolutely can NOT sleep so no * _cansleep suffixes, and no using GPIO expanders on * slow buses for this! */ - gpiod_set_value(ktd253->gpiod, 0); - ndelay(KTD253_T_LOW_NS); - gpiod_set_value(ktd253->gpiod, 1); - ndelay(KTD253_T_HIGH_NS); - /* After 1/32 we loop back to 32/32 */ - if (current_ratio == KTD253_MIN_RATIO) + ret = ktd253_backlight_stepdown(ktd253); + if (ret == -EAGAIN) { + /* + * Something disturbed the backlight setting code when + * running so we need to bring the PWM back to a known + * state. This shouldn't happen too much. + */ + gpiod_set_value_cansleep(ktd253->gpiod, 0); + msleep(KTD253_T_OFF_MS); + ktd253_backlight_set_max_ratio(ktd253); + current_ratio = KTD253_MAX_RATIO; + } else if (current_ratio == KTD253_MIN_RATIO) { + /* After 1/32 we loop back to 32/32 */ current_ratio = KTD253_MAX_RATIO; - else + } else { current_ratio--; + } } - local_irq_restore(flags); ktd253->ratio = current_ratio; dev_dbg(ktd253->dev, "new ratio set to %d/32\n", target_ratio); -- 2.30.2