From patchwork Tue Nov 29 14:09:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Chuanhong Guo X-Patchwork-Id: 629335 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 1F9A3C47089 for ; Tue, 29 Nov 2022 14:10:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235017AbiK2OKl (ORCPT ); Tue, 29 Nov 2022 09:10:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235299AbiK2OKQ (ORCPT ); Tue, 29 Nov 2022 09:10:16 -0500 Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D668C5EFA7; Tue, 29 Nov 2022 06:10:10 -0800 (PST) Received: by mail-pf1-x433.google.com with SMTP id x66so13798009pfx.3; Tue, 29 Nov 2022 06:10:10 -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=MBA40M/jf7iW7dWiX7G42nvg49ZSKMGIJyGQgzCsnPU=; b=SgtKAAorY7XnakOpyX4HtaOGDIoD7CS9X3TT3yasROFE3IXidNc7lI2FgfUvNzYdQS n83h8ZINTMmxIMPHZiS3D2iq0ZwhsqmczmnEKs+vwhhtfQHlLQE0VaL4F9tF/FI+ABN6 45vlUnhbH3t79TZxaiRv7gAXaOqr4XLQ5K7EP29yqwmrcL3BZwXnYzlGVcA74c4tCjmY S1ZZw3ym0HqCAekXACX7oitQE0tmJYJ75EGHIZvpEekupiAbo39mF/NVBverKMYS7xGM A+R/UluQGlsVia3fkGvBoktT4r+7hMV5wE5MFwMDjq1VwGaNVf+kW/O2lT15rbieZh4k ce3g== 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=MBA40M/jf7iW7dWiX7G42nvg49ZSKMGIJyGQgzCsnPU=; b=6Eig2Z4cM5O8ON4hMeI7NgumvVBnemSY54Faby+IsL8Vj600q5gcmG5pAiUIzanESr dcQdjhB2rfPijRot2YzVr3t2ZM9Bo092vVsbShOJA55mXg8kEeFMXVPF49/nX08FfXA1 Ydoug1Ca6BLTwEQJKCBkks0/XOWrqyPfoN7PDm81Kg+mtZKCJHODs1YCb0pgQx3y33JZ 0KS8Uoxrf7qVAAZ+UNvKtB3zbMuyOTQuE8UXEBJ20nbKl/gXbqnY/023DnI2BqWzfEvi HoDFafIZ4zqPfVZi6Av924TzuXP1JirHvRfmXXvDRGhVx2hfOLnCbqDprsmYN4Uwarx7 SxsA== X-Gm-Message-State: ANoB5pksoBH8BCeLoZq/4spETDcmEkT8FP5uNFiV0+ZgY86QaOwmbzjS mO3ZNWLu0H0eaDJ3taIJJagJKLFjUy0= X-Google-Smtp-Source: AA0mqf4ODFr+y6ZZXfIoDmYuJXEo/sKdmGcS5SD3icoCkpTIgpdFqw/Tv81k0hw6fxaZi8dzKtkAQg== X-Received: by 2002:a63:d74b:0:b0:450:3ae9:2590 with SMTP id w11-20020a63d74b000000b004503ae92590mr33728380pgi.410.1669731010120; Tue, 29 Nov 2022 06:10:10 -0800 (PST) Received: from guoguo-omen.scut.edu.cn ([2401:c080:1400:4da2:5400:3ff:feb4:7578]) by smtp.gmail.com with ESMTPSA id a5-20020a170902710500b0017854cee6ebsm10932342pll.72.2022.11.29.06.10.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Nov 2022 06:10:09 -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 1/3] dt-bindings: vendor-prefixes: add an entry for WorldSemi Date: Tue, 29 Nov 2022 22:09:53 +0800 Message-Id: <20221129140955.137361-2-gch981213@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221129140955.137361-1-gch981213@gmail.com> References: <20221129140955.137361-1-gch981213@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org WorldSemi is well-known for their WS2812B individually-addressable RGB LEDs on the market. Add a vendor prefix for it. Signed-off-by: Chuanhong Guo --- 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 Tue Nov 29 14:09:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuanhong Guo X-Patchwork-Id: 629776 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 A7A80C4167B for ; Tue, 29 Nov 2022 14:10:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232491AbiK2OKk (ORCPT ); Tue, 29 Nov 2022 09:10:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235314AbiK2OKS (ORCPT ); Tue, 29 Nov 2022 09:10:18 -0500 Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44A3D5F858; Tue, 29 Nov 2022 06:10:16 -0800 (PST) Received: by mail-pl1-x62d.google.com with SMTP id p24so9764919plw.1; Tue, 29 Nov 2022 06:10:16 -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=cAmdPwxeV/gmwrZFUCjIqIdjxeWkPmcTFRtF8kijtD8=; b=L7E15i11LH899DwPT2jWyRY3zYfaCL5kCTEOkOHF0KRRPmojCmCpjHYU0ij1jOoREh q1w59KBmdEeCojMFJMg/I5mIS3/GMf4aaPt5TP37JPS0P+rbnzo22kaLJ3HnYSbVWi2g Juyhf8scAkjX4v/jgRKs8jBBuBxwPBQjuimrjS3YXCCbJ7MJ6aSLAXPYLhZogxdBUiBX Se1lJ5RMM8dzBwFSgsy7GS3arDKWTU9G7hEq5sGlq8KFAilP6KHgV8nLUN+iujHcniK4 txiYMY5o4efzDHnECBIWCeiQzOHg7sEBK5u4n11HbOLRgRAXAdlgyrdc/QqOBj6OUsRm +ufg== 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=cAmdPwxeV/gmwrZFUCjIqIdjxeWkPmcTFRtF8kijtD8=; b=kRn1M0yCVD+KwCXO9D1BzvnwcOl1qWhAx+7kbzyrU2XPlTCPZOx+taGrWYHs0J2W7V A5dNLjugKWIf903vDx6eUw2BBJGzlHy0MrrQeWTPxeBbR9+MVUoJ08r6WmaoHLmGcKoW XgA9xAHOcDIf5RVtO7+42yZlOZbOez66t/ud5QW6eosmBWRuk+wfh2RuKEePakSJkdPK bE72LLHkiXgBrz4NFh2rbrzB5UuI2JwUqnyOppW+aR4muVbxafcLFJAm8cZ+Ff275om/ 7UvCfqK7SwjIEXa1srkAF3i4e0kpiYi7ke7V6/P5tzdI7XGPsFgLH9X5uOOMiAckAhsl bGig== X-Gm-Message-State: ANoB5plsLCBr+zw0Hr6k372DsST9PYY++HXsoUKB/YSeSaEpBBep+3iw rgr1wSqrZDthw0FTr7n6D4DQvJHq9nc= X-Google-Smtp-Source: AA0mqf5mVohJJr822gYGrE1q5vJhKvIkHt3GESFsGio1KQfQLRdc+Ed2Lq7KrobYE4dh5F0tQbtnhQ== X-Received: by 2002:a17:90b:3d0d:b0:1fd:5b5d:f09d with SMTP id pt13-20020a17090b3d0d00b001fd5b5df09dmr60832901pjb.69.1669731015551; Tue, 29 Nov 2022 06:10:15 -0800 (PST) Received: from guoguo-omen.scut.edu.cn ([2401:c080:1400:4da2:5400:3ff:feb4:7578]) by smtp.gmail.com with ESMTPSA id a5-20020a170902710500b0017854cee6ebsm10932342pll.72.2022.11.29.06.10.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Nov 2022 06:10:15 -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 2/3] dt-bindings: leds: add dt schema for worldsemi,ws2812b-spi Date: Tue, 29 Nov 2022 22:09:54 +0800 Message-Id: <20221129140955.137361-3-gch981213@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221129140955.137361-1-gch981213@gmail.com> References: <20221129140955.137361-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 --- .../bindings/leds/worldsemi,ws2812b-spi.yaml | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 Documentation/devicetree/bindings/leds/worldsemi,ws2812b-spi.yaml diff --git a/Documentation/devicetree/bindings/leds/worldsemi,ws2812b-spi.yaml b/Documentation/devicetree/bindings/leds/worldsemi,ws2812b-spi.yaml new file mode 100644 index 000000000000..8544bde4f261 --- /dev/null +++ b/Documentation/devicetree/bindings/leds/worldsemi,ws2812b-spi.yaml @@ -0,0 +1,131 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/leds/worldsemi,ws2812b-spi.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 driver simulates the protocol used by this LED chip with SPI bus. + 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-spi + + reg: + description: The chip-select line on the SPI bus + maxItems: 1 + + spi-max-frequency: + $ref: /schemas/types.yaml#/definitions/uint32 + description: + Maximum SPI clocking speed of the device in Hz. + 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# + + 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 + minItems: 3 + maxItems: 3 + + 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 + minItems: 3 + 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. + + required: + - reg + - color + - function + +required: + - compatible + +additionalProperties: false + +examples: + - | + #include + spi { + #address-cells = <1>; + #size-cells = <0>; + + ws2812b@0 { + #address-cells = <1>; + #size-cells = <0>; + compatible = "worldsemi,ws2812b-spi"; + reg = <0>; + spi-max-frequency = <2850000>; + multi-led@0 { + reg = <0>; + color-index = ; + 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 Tue Nov 29 14:09:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuanhong Guo X-Patchwork-Id: 629775 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 0B19CC4321E for ; Tue, 29 Nov 2022 14:10:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234698AbiK2OKu (ORCPT ); Tue, 29 Nov 2022 09:10:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45214 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235201AbiK2OKX (ORCPT ); Tue, 29 Nov 2022 09:10:23 -0500 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6A3160EA2; Tue, 29 Nov 2022 06:10:21 -0800 (PST) Received: by mail-pl1-x632.google.com with SMTP id k7so13494696pll.6; Tue, 29 Nov 2022 06:10:21 -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=t7kNZMgNax6r+KXpe7Ot15O8yiq8q2YUgnIvm3OyPck=; b=jSs5Jrc4JdDy8d7qHWA9Hf8gUyNQsFMgWLI9BEJXlQftFWZAweLu0DdSWF9xutG5b3 BMpZAt7lMsAPkzHkR/QxIqSvKttl9Pj85jPLU69ELhe2ljtpdr7CmFBuBiyEKwRGynHp juCFWguTlCI0Q135cfcMutR+mioedtnaOnPM1HVIlAkZUgdzlLH+7+eCrR1jihJWhbCO RSvu8AF5LEMstSAhFL/GExIAmhPST1OaBOTFld1DxcLtWTYM2osgChxLODhjrHcESErK OC9lYj+eV3T7IpCK1zNo1cOHZphQUUsu9xSMaAfWdBqarFBNwEGRJ7QCprtbm9u8mh1p zUSw== 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=t7kNZMgNax6r+KXpe7Ot15O8yiq8q2YUgnIvm3OyPck=; b=2RnlzHaxFa1BW6kkloHjBuKvW9b0yJH8m4Y3osyQimiiEC3Sk9qtTNLUlrMj5e4fIS u5I8FdBVE0RYf6lgSlrRPR8bSpV73/m4kPUPYes7uIephmELH4XGw19+qdxiMZgD5gw6 0cBrS/REx35c9N8dKRFmXTn9aqFjnZh04KVBw6LUZ3ZsZlZYNfy8P5aZLSwbEZgqIXrb ejbat99jtrJpGiG/W6pTyE9eYtPzrWFY72acszooSXGdCLU/jLL5FZA6SSEY3AUTQxLL h37fzQvxMqK3joqjMNPCqFaW5aRV/YdcJWOSqarY+wFyJUk0hiVfRk9g9BScG+xllAR3 IcWQ== X-Gm-Message-State: ANoB5pm9s5t9NhElg2dJieEH0w6FhuzWkZeBDexk/ejxYXRwkzLcYrwM tnOGGRw669JpU20VB5pFBMixjdHXM6I= X-Google-Smtp-Source: AA0mqf6WEoNe3HIxWVNQ+6AC816MZ1YVnJD2D0PxQF/SzawP2GKBr7UNyx3OzR03GESsj5X8ZA6xDg== X-Received: by 2002:a17:90a:62c2:b0:20d:bc7f:4fee with SMTP id k2-20020a17090a62c200b0020dbc7f4feemr66724732pjs.168.1669731020908; Tue, 29 Nov 2022 06:10:20 -0800 (PST) Received: from guoguo-omen.scut.edu.cn ([2401:c080:1400:4da2:5400:3ff:feb4:7578]) by smtp.gmail.com with ESMTPSA id a5-20020a170902710500b0017854cee6ebsm10932342pll.72.2022.11.29.06.10.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Nov 2022 06:10:20 -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 3/3] leds: add driver for SPI driven WorldSemi WS2812B RGB LEDs Date: Tue, 29 Nov 2022 22:09:55 +0800 Message-Id: <20221129140955.137361-4-gch981213@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221129140955.137361-1-gch981213@gmail.com> References: <20221129140955.137361-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 --- drivers/leds/rgb/Kconfig | 11 ++ drivers/leds/rgb/Makefile | 1 + drivers/leds/rgb/leds-ws2812b-spi.c | 222 ++++++++++++++++++++++++++++ 3 files changed, 234 insertions(+) create mode 100644 drivers/leds/rgb/leds-ws2812b-spi.c diff --git a/drivers/leds/rgb/Kconfig b/drivers/leds/rgb/Kconfig index 204cf470beae..09daa90cd30a 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_SPI + 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..3af2081ea6c3 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_SPI) += leds-ws2812b-spi.o \ No newline at end of file diff --git a/drivers/leds/rgb/leds-ws2812b-spi.c b/drivers/leds/rgb/leds-ws2812b-spi.c new file mode 100644 index 000000000000..1e17bd856474 --- /dev/null +++ b/drivers/leds/rgb/leds-ws2812b-spi.c @@ -0,0 +1,222 @@ +// 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 + +#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; + struct device_node *np = dev_of_node(dev); + int ret = 0, cur_led = 0; + struct ws2812b_priv *priv; + struct device_node *led_node; + int num_leds, i; + + num_leds = of_get_available_child_count(np); + + 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; + + for_each_available_child_of_node(np, led_node) { + u32 reg = -1; + struct led_init_data init_data = { + .fwnode = of_fwnode_handle(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 }; + + ret = of_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; + } + + of_property_read_u32_array(led_node, "color-index", color_idx, + WS2812B_NUM_COLORS); + of_property_read_u32_array(led_node, "default-intensity", + default_intensity, + WS2812B_NUM_COLORS); + + 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.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; + + 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-spi" }, + {}, +}; +MODULE_DEVICE_TABLE(spi, ws2812b_spi_ids); + +static const struct of_device_id ws2812b_dt_ids[] = { + { .compatible = "worldsemi,ws2812b-spi" }, + {}, +}; +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");