From patchwork Fri Dec 2 03:42:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Chuanhong Guo X-Patchwork-Id: 630483 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 66FBCC4332F for ; Fri, 2 Dec 2022 03:43:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232234AbiLBDnR (ORCPT ); Thu, 1 Dec 2022 22:43:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232202AbiLBDnP (ORCPT ); Thu, 1 Dec 2022 22:43:15 -0500 Received: from mail-pg1-x536.google.com (mail-pg1-x536.google.com [IPv6:2607:f8b0:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7BA367BC09; Thu, 1 Dec 2022 19:43:12 -0800 (PST) Received: by mail-pg1-x536.google.com with SMTP id f3so3388788pgc.2; Thu, 01 Dec 2022 19:43:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=B/B476Yo3I1Iy8ujmIlJ4x8L02T6sXbrTl7idaba7To=; b=cTCMDZ3+nplFRZTAytMDb4GKY5yBolf9QWvgCYnlc0fxTUSIJG6AW8n000E+SGXyvW dxkUG7GBDlK/zTwshc37VvyztvzjsXFenbNcOpYlLolMVFMMXTijw7FETcDUbouhtBBN qPMT6r6zIZ45/7gDVJ4JbgPncJedFtki0j5sSAbUOoW149iRx9jK/iEIGWxZr36S5KqU aKp7rjkEoec46lgTCwPcAQPsI58SHfCtSR5xVkZNOT2jydJEbAc4+lpG+96ibph02U1y bnZlE06mKA+WtAkwd4H+WiLWn7AOEsKufMqddj02MjS4XknSWTzo6zPRlOMeyB1dc1Wx US6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=B/B476Yo3I1Iy8ujmIlJ4x8L02T6sXbrTl7idaba7To=; b=NQ6NL918vGsv5zJKgqLdQLKdSmsS+JSKBOAbmQOIp533eWRwcTzZRmu9rasf/6Llou unoJUKuAxv9LsTb47PRfJre8uIRj/Rx7QT8VHWCuC0ARTxP16I63zNn0iG/ua9xUZcj/ iev51+4l0z+JvIziRBGGnpK/ztpAF7L71sLmMuv92cT659+3jMYVGolL3AWIXu8Nw8tk bW268D464bM8DmITNF7YSmvSNuT5erKl67VjJkUlhwvtILkJ9U/uTXystqEaaYaf3PBC Nc5tP9FZ5Xv59PusVN9E+4OMpm4Cz5NhOQBhHJjwpE3a6ucp3ZWg26fn67/BPVbCoHIq yREg== X-Gm-Message-State: ANoB5pmkWn5gYMfzAZkXq+UIWn92PnAho8U2KcrLGBjmTUM9BC0WYeVv Uzv0i4pkPh6TmwcJTrR2Omg/OiaC89M= X-Google-Smtp-Source: AA0mqf7x+da07zHJaYEQlkiDUbhcOJEMJ9c4izkfqNbTmZg7otIvtC3EjzzGjnMgYOoBYZ4Qyv51fw== X-Received: by 2002:a63:4649:0:b0:477:f449:1e88 with SMTP id v9-20020a634649000000b00477f4491e88mr28246372pgk.2.1669952591917; Thu, 01 Dec 2022 19:43:11 -0800 (PST) Received: from guoguo-omen.scut.edu.cn ([2401:c080:1400:4da2:5400:3ff:feb4:7578]) by smtp.gmail.com with ESMTPSA id v3-20020a170902e8c300b0017f59ebafe7sm4370636plg.212.2022.12.01.19.43.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Dec 2022 19:43:11 -0800 (PST) From: Chuanhong Guo To: linux-leds@vger.kernel.org Cc: Chuanhong Guo , Pavel Machek , Rob Herring , Krzysztof Kozlowski , Stanislav Jakubek , Linus Walleij , Shawn Guo , Johan Hovold , Bjorn Andersson , Marijn Suijten , Sven Schwermer , devicetree@vger.kernel.org (open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE BINDINGS), linux-kernel@vger.kernel.org (open list) Subject: [PATCH v2 1/3] dt-bindings: vendor-prefixes: add an entry for WorldSemi Date: Fri, 2 Dec 2022 11:42:38 +0800 Message-Id: <20221202034240.455831-2-gch981213@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221202034240.455831-1-gch981213@gmail.com> References: <20221202034240.455831-1-gch981213@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org Add vendor prefix for WorldSemi that makes WS2812B individually-addressable RGB LEDs. Signed-off-by: Chuanhong Guo --- Change since v1: reword commit message Documentation/devicetree/bindings/vendor-prefixes.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Documentation/devicetree/bindings/vendor-prefixes.yaml b/Documentation/devicetree/bindings/vendor-prefixes.yaml index 10c178d97b02..32274d894664 100644 --- a/Documentation/devicetree/bindings/vendor-prefixes.yaml +++ b/Documentation/devicetree/bindings/vendor-prefixes.yaml @@ -1462,6 +1462,8 @@ patternProperties: description: Wondermedia Technologies, Inc. "^wobo,.*": description: Wobo + "^worldsemi,.*": + description: WorldSemi Co., Limited "^wanchanglong,.*": description: Wanchanglong Electronics Technology(SHENZHEN)Co.,Ltd. "^x-powers,.*": From patchwork Fri Dec 2 03:42:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuanhong Guo X-Patchwork-Id: 630751 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6C196C47088 for ; Fri, 2 Dec 2022 03:43:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231562AbiLBDnU (ORCPT ); Thu, 1 Dec 2022 22:43:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232250AbiLBDnT (ORCPT ); Thu, 1 Dec 2022 22:43:19 -0500 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F29F920AE; Thu, 1 Dec 2022 19:43:18 -0800 (PST) Received: by mail-pj1-x102d.google.com with SMTP id k2-20020a17090a4c8200b002187cce2f92so7130327pjh.2; Thu, 01 Dec 2022 19:43:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=DcgpSVWbdnGyz27O63kyyynW87PPwff4FTigIa8LAE8=; b=ZSCPwyLaDzy9gC5ISXUZhJVzlWx38kHqwDmkd3+h63VteuZkUmnF65ke8rjjIzZsoO eofp8zUMtwU0sL0Lu1dLHWVZDJOdnr7WvwNr8iJcC/WLcMvf13+d7uo3u3lCdTAlqUiF 41kcEeDqQA339abK0OojrKUF2zztAV7bZeS5LHoUFSEXVkisFylg7qOK0wT0o0+CsYQN tlH2Z7Qi+lxpN2f5XtJ5QYcv55831FJWBTBfoC2llWai3UyqASEpdr9UtILdHql09TTY KD91HcVjkxjIdCtOQ2fchV7o8OIOBNIEOJoirqdjMCioA/IF9o7lQKw1T6FnHIGevwBe 9tsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DcgpSVWbdnGyz27O63kyyynW87PPwff4FTigIa8LAE8=; b=VffP2zvkbrOVvUMwFFkRAt5jM7p0XJ+PxZ6c0TUbx0PktMzYvSq/Cdc78u1H8yP7IR tujrozIHZPGrJS1NJMpqkVD4F6s6R2ToQ0u1vogJGe68xKRgOpD5lgmCZVC+wvcG/5Ph zcFeZqSGoa62RS/4OpdnmXSZ5q8xujYkJxpHgSlGURAkW1jnv8pDzFonJhW8wRxZnqxj o1wkSltY03NS5jpCmbvogMK2MK8Ljko6SYJbGsJKwzkOzyde4ILIkJt6QReMwNdp2GTS B71vWnH43ew7WVTmN3NjmtWRJJQRyKLmflmXzf5EVVxsoimANDOZKQ67Kx4a3tJmefNU J2eA== X-Gm-Message-State: ANoB5pljvU5prp2239i+slwOAA/10OSu5JkRTGMxNqotkRV1PnbNUowL 7vbVgAAtxvfMcFtkA6D3KkMyZESlgrg= X-Google-Smtp-Source: AA0mqf5pBHuD74+gyQawU+H3V8GojiH5S3AiPfHrU1wxLJSOa4XUZEcOq4bTio/AwGenm9fg7Rmycg== X-Received: by 2002:a17:90b:264a:b0:213:7030:f69a with SMTP id pa10-20020a17090b264a00b002137030f69amr73829602pjb.231.1669952597261; Thu, 01 Dec 2022 19:43:17 -0800 (PST) Received: from guoguo-omen.scut.edu.cn ([2401:c080:1400:4da2:5400:3ff:feb4:7578]) by smtp.gmail.com with ESMTPSA id v3-20020a170902e8c300b0017f59ebafe7sm4370636plg.212.2022.12.01.19.43.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Dec 2022 19:43:16 -0800 (PST) From: Chuanhong Guo To: linux-leds@vger.kernel.org Cc: Chuanhong Guo , Pavel Machek , Rob Herring , Krzysztof Kozlowski , Stanislav Jakubek , Linus Walleij , Shawn Guo , Johan Hovold , Bjorn Andersson , Marijn Suijten , Sven Schwermer , devicetree@vger.kernel.org (open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE BINDINGS), linux-kernel@vger.kernel.org (open list) Subject: [PATCH v2 2/3] dt-bindings: leds: add dt schema for worldsemi,ws2812b-spi Date: Fri, 2 Dec 2022 11:42:39 +0800 Message-Id: <20221202034240.455831-3-gch981213@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221202034240.455831-1-gch981213@gmail.com> References: <20221202034240.455831-1-gch981213@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org This patch adds dt binding schema for WorldSemi WS2812B driven using SPI bus. Signed-off-by: Chuanhong Guo --- Changes since v1: remove linux driver reference from description remove some obvious descriptions fix unit address regex in multi-led property drop various minItems add maxItems = 1 to reg fix node names and property orders in binding example drop -spi from compatible string add default-brightness .../bindings/leds/worldsemi,ws2812b.yaml | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 Documentation/devicetree/bindings/leds/worldsemi,ws2812b.yaml diff --git a/Documentation/devicetree/bindings/leds/worldsemi,ws2812b.yaml b/Documentation/devicetree/bindings/leds/worldsemi,ws2812b.yaml new file mode 100644 index 000000000000..f91908d0acef --- /dev/null +++ b/Documentation/devicetree/bindings/leds/worldsemi,ws2812b.yaml @@ -0,0 +1,138 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/leds/worldsemi,ws2812b.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: WS2812B LEDs driven using SPI + +maintainers: + - Chuanhong Guo + +description: | + WorldSemi WS2812B is a individually addressable LED chip that can be chained + together and controlled individually using a single wire. + This binding describes a chain of WS2812B LEDs connected to the SPI MOSI pin. + Typical setups includes connecting the data pin of the LED chain to MOSI as + the only device or using CS and MOSI with a tri-state voltage-level shifter + for the data pin. + The SPI frequency needs to be 2.105MHz~2.85MHz for the timing to be correct + and the controller needs to send all the bytes continuously. + +allOf: + - $ref: /schemas/spi/spi-peripheral-props.yaml# + +properties: + compatible: + const: worldsemi,ws2812b + + reg: + maxItems: 1 + + spi-max-frequency: + minimum: 2105000 + maximum: 2850000 + + "#address-cells": + const: 1 + + "#size-cells": + const: 0 + +patternProperties: + "^multi-led@[0-9a-f]+$": + type: object + $ref: leds-class-multicolor.yaml# + unevaluatedProperties: false + + properties: + color-index: + description: | + A 3-item array specifying color of each components in this LED. It + should be one of the LED_COLOR_ID_* prefixed definitions from the + header include/dt-bindings/leds/common.h. Defaults to + + if unspecified. + $ref: /schemas/types.yaml#/definitions/uint32-array + maxItems: 3 + + default-brightness: + description: + The default brightness that should be applied to the LED by the operating + system on start-up. The brightness should not exceed the brightness the + LED can provide. + $ref: /schemas/types.yaml#/definitions/uint32 + minimum: 0 + maximum: 255 + default: 0 + + default-intensity: + description: | + An array of 3 integer specifying the default intensity of each color + components in this LED. <255 255 255> if unspecified. + $ref: /schemas/types.yaml#/definitions/uint32-array + maxItems: 3 + items: + minimum: 0 + maximum: 255 + + reg: + description: | + Which LED this node represents. The reg of the first LED on the chain + is 0. + maxItems: 1 + + required: + - reg + - color + - function + +required: + - compatible + +additionalProperties: false + +examples: + - | + #include + spi { + #address-cells = <1>; + #size-cells = <0>; + + leds@0 { + compatible = "worldsemi,ws2812b"; + reg = <0>; + #address-cells = <1>; + #size-cells = <0>; + spi-max-frequency = <2850000>; + multi-led@0 { + reg = <0>; + color-index = ; + default-brightness = <30>; + default-intensity = <10 50 90>; + color = ; + function = LED_FUNCTION_STATUS; + function-enumerator = <0>; + }; + multi-led@1 { + reg = <1>; + color = ; + function = LED_FUNCTION_STATUS; + function-enumerator = <1>; + }; + multi-led@2 { + reg = <2>; + color = ; + function = LED_FUNCTION_STATUS; + function-enumerator = <2>; + }; + multi-led@3 { + reg = <3>; + color = ; + function = LED_FUNCTION_STATUS; + function-enumerator = <3>; + }; + }; + }; + +... From patchwork Fri Dec 2 03:42:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuanhong Guo X-Patchwork-Id: 630482 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B9941C4332F for ; Fri, 2 Dec 2022 03:43:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232339AbiLBDnc (ORCPT ); Thu, 1 Dec 2022 22:43:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232226AbiLBDn3 (ORCPT ); Thu, 1 Dec 2022 22:43:29 -0500 Received: from mail-pg1-x530.google.com (mail-pg1-x530.google.com [IPv6:2607:f8b0:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B36E2A554A; Thu, 1 Dec 2022 19:43:23 -0800 (PST) Received: by mail-pg1-x530.google.com with SMTP id q1so3358672pgl.11; Thu, 01 Dec 2022 19:43:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4QAi8Caord9mLKor0viB6nvfRqG/6jsMMSTIQ4z6um4=; b=ipBezcTpvd7O+lizQAa1YmL7gX621c9mIk2lYJUQOm3f1/Ynzp8wSmo0JJLgEPuVGe 4jUUt3nf0jYh2fc0OA4yJY8Jjum5uYMvpj4WIKuIwE8Q3ARKdGaYsDvp0pIuJFNBaKEN ohvw+d+zpG7Te8IVNRRm5lAgn5/5nfssMH+RXitx/xnljmNJSUURvVxC92d0vAHzj/Mo 0Xu6MP2tZurjlzqDA8vUAMMUWZ8n+zhEUbn/CFmeHa0TLwnA6stzjcf3LhBI9cy64h8i 030zp9AUdW/qr+eM4jQ6KaQ8owR45P5xiI6kd5pdJPFG0FoefFBWTQoMgGM5XeWJreGo +s2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4QAi8Caord9mLKor0viB6nvfRqG/6jsMMSTIQ4z6um4=; b=uoyb0TqVPzdLDEBP0pf1BSiHjlqdMG9O65G7mZ34r79kH8vEff6Y+4xg9ecH8bIOQX vbqdZftvUnX/siu2PFSqE9YF5Dgrw/b3KhlPcWRXArm62q+g7MW2vYEyhq6oXiUf6UUE uthUO8NqkWuKeYTf5TJZuFH1dRVvI2GD0+sUwqRHQ4z+jQUz2Fy/u6Qp53rii82JsaeA 8GLAhxuufn0uoUHevl8RDIy6g8f4Zjj3MyoH+O0D8sbxwMuSxRCuW41pcwb8smWbtsjw 03jsaWajeCgBuJgLwMwfJ2JWshedwGhI+x2RDVPmnczpW94cUvqLRvVxl0bShNHaGN70 Kshw== X-Gm-Message-State: ANoB5pkUjcvk+pMfcd6KuFD8syK5fzIf2o9J9saCj/uRvgtdFOSqh0Lg 3l4e7o9yDhdVQelfvaqLGSaydDU3X08= X-Google-Smtp-Source: AA0mqf7+8ZlRiWrKwilE58Phpp0CbwlfRLUd1hkUylbCqtMN4JT5YAuy82vfjdo/8tUVV/UGdObPYw== X-Received: by 2002:a65:6753:0:b0:477:91c5:f9ff with SMTP id c19-20020a656753000000b0047791c5f9ffmr43531832pgu.403.1669952602631; Thu, 01 Dec 2022 19:43:22 -0800 (PST) Received: from guoguo-omen.scut.edu.cn ([2401:c080:1400:4da2:5400:3ff:feb4:7578]) by smtp.gmail.com with ESMTPSA id v3-20020a170902e8c300b0017f59ebafe7sm4370636plg.212.2022.12.01.19.43.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Dec 2022 19:43:22 -0800 (PST) From: Chuanhong Guo To: linux-leds@vger.kernel.org Cc: Chuanhong Guo , Pavel Machek , Rob Herring , Krzysztof Kozlowski , Stanislav Jakubek , Linus Walleij , Shawn Guo , Johan Hovold , Bjorn Andersson , Marijn Suijten , Sven Schwermer , devicetree@vger.kernel.org (open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE BINDINGS), linux-kernel@vger.kernel.org (open list) Subject: [PATCH v2 3/3] leds: add driver for SPI driven WorldSemi WS2812B RGB LEDs Date: Fri, 2 Dec 2022 11:42:40 +0800 Message-Id: <20221202034240.455831-4-gch981213@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221202034240.455831-1-gch981213@gmail.com> References: <20221202034240.455831-1-gch981213@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org This patch adds support for driving a chain of WS2812B LED chips using SPI bus. WorldSemi WS2812B is a individually addressable LED chip that can be chained together and controlled individually using a single wire. The chip recognize a long pulse as a bit of 1 and a short pulse as a bit of 0. Host sends a continuous stream of 24-bits color values, each LED chip takes the first 3 byte it receives as its color value and passes the leftover bytes to the next LED on the chain. This driver simulates this protocol using SPI bus by sending a long pulse as 3'b110 and a short pulse as 3'b100. The SPI frequency needs to be 2.105MHz~2.85MHz for the timing to be correct and the controller needs to transfer all the bytes continuously. Signed-off-by: Chuanhong Guo --- Changes since v1: rename the driver to drop -spi suffix add support for default-brightness use fwnode apis for properties drivers/leds/rgb/Kconfig | 11 ++ drivers/leds/rgb/Makefile | 1 + drivers/leds/rgb/leds-ws2812b.c | 235 ++++++++++++++++++++++++++++++++ 3 files changed, 247 insertions(+) create mode 100644 drivers/leds/rgb/leds-ws2812b.c diff --git a/drivers/leds/rgb/Kconfig b/drivers/leds/rgb/Kconfig index 204cf470beae..5c2081852f01 100644 --- a/drivers/leds/rgb/Kconfig +++ b/drivers/leds/rgb/Kconfig @@ -26,4 +26,15 @@ config LEDS_QCOM_LPG If compiled as a module, the module will be named leds-qcom-lpg. +config LEDS_WS2812B + tristate "SPI driven WS2812B RGB LED support" + depends on OF + depends on SPI + help + This option enables support for driving daisy-chained WS2812B RGB + LED chips using SPI bus. This driver simulates the single-wire + protocol by sending bits over the SPI MOSI pin. For this to work, + the SPI frequency should be 2.105MHz~2.85MHz and the controller + needs to transfer all the bytes continuously. + endif # LEDS_CLASS_MULTICOLOR diff --git a/drivers/leds/rgb/Makefile b/drivers/leds/rgb/Makefile index 0675bc0f6e18..a6f855eaeb14 100644 --- a/drivers/leds/rgb/Makefile +++ b/drivers/leds/rgb/Makefile @@ -2,3 +2,4 @@ obj-$(CONFIG_LEDS_PWM_MULTICOLOR) += leds-pwm-multicolor.o obj-$(CONFIG_LEDS_QCOM_LPG) += leds-qcom-lpg.o +obj-$(CONFIG_LEDS_WS2812B) += leds-ws2812b.o diff --git a/drivers/leds/rgb/leds-ws2812b.c b/drivers/leds/rgb/leds-ws2812b.c new file mode 100644 index 000000000000..ca2ae83de573 --- /dev/null +++ b/drivers/leds/rgb/leds-ws2812b.c @@ -0,0 +1,235 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * WorldSemi WS2812B individually-addressable LED driver using SPI + * + * Copyright 2022 Chuanhong Guo + * + * This driver simulates WS2812B protocol using SPI MOSI pin. A one pulse + * is transferred as 3'b110 and a zero pulse is 3'b100. For this driver to + * work properly, the SPI frequency should be 2.105MHz~2.85MHz and it needs + * to transfer all the bytes continuously. + */ + +#include +#include +#include +#include +#include +#include +#include + +#define WS2812B_BYTES_PER_COLOR 3 +#define WS2812B_NUM_COLORS 3 +#define WS2812B_RESET_LEN 18 + +struct ws2812b_led { + struct led_classdev_mc mc_cdev; + struct mc_subled subled[WS2812B_NUM_COLORS]; + struct ws2812b_priv *priv; + int reg; +}; + +struct ws2812b_priv { + struct led_classdev ldev; + struct spi_device *spi; + struct mutex mutex; + int num_leds; + size_t data_len; + u8 *data_buf; + struct ws2812b_led leds[]; +}; + +static void ws2812b_set_byte(u8 *p, u8 val) +{ + /* + * Every bit of data is represented using 3 bits: 3'b100 for + * 0 and 3'b110 for 1. + * 1 byte of data takes up 3 bytes in a SPI transfer. The higher + * 3 bits, middle 2 bits and lower 3 bits are represented + * with the 1st, 2nd and 3rd byte in the SPI transfer. + * Here's the lookup table for them. + */ + const u8 h3b[] = { 0x92, 0x93, 0x9a, 0x9b, 0xd2, 0xd3, 0xda, 0xdb }; + const u8 m2b[] = { 0x49, 0x4d, 0x69, 0x6d }; + const u8 l3b[] = { 0x24, 0x26, 0x34, 0x36, 0xa4, 0xa6, 0xb4, 0xb6 }; + + p[0] = h3b[val >> 5]; + p[1] = m2b[(val >> 3) & 0x3]; + p[2] = l3b[val & 0x7]; +} + +static int ws2812b_set(struct led_classdev *cdev, + enum led_brightness brightness) +{ + struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev); + struct ws2812b_led *led = + container_of(mc_cdev, struct ws2812b_led, mc_cdev); + struct ws2812b_priv *priv = led->priv; + u8 *buf = priv->data_buf + WS2812B_RESET_LEN + + led->reg * WS2812B_NUM_COLORS * WS2812B_BYTES_PER_COLOR; + int ret = 0; + int i; + + led_mc_calc_color_components(mc_cdev, brightness); + + mutex_lock(&priv->mutex); + for (i = 0; i < WS2812B_NUM_COLORS; i++) + ws2812b_set_byte(buf + i * WS2812B_BYTES_PER_COLOR, + led->subled[i].brightness); + ret = spi_write(priv->spi, priv->data_buf, priv->data_len); + mutex_unlock(&priv->mutex); + + return ret; +} + +static int ws2812b_probe(struct spi_device *spi) +{ + struct device *dev = &spi->dev; + int ret = 0, cur_led = 0; + struct ws2812b_priv *priv; + struct fwnode_handle *led_node; + int num_leds, i, cnt; + + num_leds = device_get_child_node_count(dev); + + priv = devm_kzalloc(dev, struct_size(priv, leds, num_leds), GFP_KERNEL); + if (!priv) + return -ENOMEM; + priv->data_len = + num_leds * WS2812B_BYTES_PER_COLOR * WS2812B_NUM_COLORS + + WS2812B_RESET_LEN; + priv->data_buf = kzalloc(priv->data_len, GFP_KERNEL); + if (!priv->data_buf) + return -ENOMEM; + + for (i = 0; i < num_leds * WS2812B_NUM_COLORS; i++) + ws2812b_set_byte(priv->data_buf + WS2812B_RESET_LEN + + i * WS2812B_BYTES_PER_COLOR, + 0); + + mutex_init(&priv->mutex); + priv->num_leds = num_leds; + priv->spi = spi; + + device_for_each_child_node(dev, led_node) { + u32 reg = -1; + struct led_init_data init_data = { + .fwnode = led_node, + }; + /* WS2812B LEDs usually come with GRB color */ + u32 color_idx[WS2812B_NUM_COLORS] = { + LED_COLOR_ID_GREEN, + LED_COLOR_ID_RED, + LED_COLOR_ID_BLUE, + }; + u32 default_intensity[WS2812B_NUM_COLORS] = { 255, 255, 255 }; + u32 default_brightness = 0; + + ret = fwnode_property_read_u32(led_node, "reg", ®); + if (ret) { + dev_err(dev, "failed to get reg of the %dth led.", + cur_led); + goto ERR_UNREG_LEDS; + } + if (reg >= num_leds) { + dev_err(dev, "reg of the %dth led is too big.", + cur_led); + ret = -EINVAL; + goto ERR_UNREG_LEDS; + } + + cnt = fwnode_property_count_u32(led_node, "color-index"); + if (cnt > 0 && cnt <= WS2812B_NUM_COLORS) + fwnode_property_read_u32_array(led_node, "color-index", + color_idx, (size_t)cnt); + cnt = fwnode_property_count_u32(led_node, "default-intensity"); + if (cnt > 0 && cnt <= WS2812B_NUM_COLORS) + fwnode_property_read_u32_array(led_node, + "default-intensity", + default_intensity, + (size_t)cnt); + fwnode_property_read_u32(led_node, "default-brightness", + &default_brightness); + + priv->leds[cur_led].mc_cdev.subled_info = + priv->leds[cur_led].subled; + priv->leds[cur_led].mc_cdev.num_colors = WS2812B_NUM_COLORS; + priv->leds[cur_led].mc_cdev.led_cdev.brightness = + default_brightness; + priv->leds[cur_led].mc_cdev.led_cdev.max_brightness = 255; + priv->leds[cur_led].mc_cdev.led_cdev.brightness_set_blocking = + ws2812b_set; + + for (i = 0; i < WS2812B_NUM_COLORS; i++) { + priv->leds[cur_led].subled[i].color_index = + color_idx[i]; + priv->leds[cur_led].subled[i].intensity = + default_intensity[i]; + } + + priv->leds[cur_led].priv = priv; + priv->leds[cur_led].reg = reg; + + ws2812b_set(&priv->leds[cur_led].mc_cdev.led_cdev, + default_brightness); + + ret = led_classdev_multicolor_register_ext( + dev, &priv->leds[cur_led].mc_cdev, &init_data); + if (ret) { + dev_err(dev, "registration of led@%d failed.", reg); + goto ERR_UNREG_LEDS; + } + cur_led++; + } + + spi_set_drvdata(spi, priv); + + return 0; +ERR_UNREG_LEDS: + for (; cur_led >= 0; cur_led--) + led_classdev_multicolor_unregister( + &priv->leds[cur_led].mc_cdev); + mutex_destroy(&priv->mutex); + kfree(priv->data_buf); + return ret; +} + +static void ws2812b_remove(struct spi_device *spi) +{ + struct ws2812b_priv *priv = spi_get_drvdata(spi); + int cur_led; + + for (cur_led = priv->num_leds - 1; cur_led >= 0; cur_led--) + led_classdev_multicolor_unregister( + &priv->leds[cur_led].mc_cdev); + kfree(priv->data_buf); + mutex_destroy(&priv->mutex); +} + +static const struct spi_device_id ws2812b_spi_ids[] = { + { "ws2812b" }, + {}, +}; +MODULE_DEVICE_TABLE(spi, ws2812b_spi_ids); + +static const struct of_device_id ws2812b_dt_ids[] = { + { .compatible = "worldsemi,ws2812b" }, + {}, +}; +MODULE_DEVICE_TABLE(of, ws2812b_dt_ids); + +static struct spi_driver ws2812b_driver = { + .probe = ws2812b_probe, + .remove = ws2812b_remove, + .id_table = ws2812b_spi_ids, + .driver = { + .name = KBUILD_MODNAME, + .of_match_table = ws2812b_dt_ids, + }, +}; + +module_spi_driver(ws2812b_driver); + +MODULE_AUTHOR("Chuanhong Guo "); +MODULE_DESCRIPTION("WS2812B LED driver using SPI"); +MODULE_LICENSE("GPL");