From nobody Thu Sep 18 02:56:02 2025 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 C135EC04FDE for ; Mon, 12 Dec 2022 04:57:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231356AbiLLE5A (ORCPT ); Sun, 11 Dec 2022 23:57:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231295AbiLLE4j (ORCPT ); Sun, 11 Dec 2022 23:56:39 -0500 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 709C2BC17; Sun, 11 Dec 2022 20:56:17 -0800 (PST) Received: by mail-pj1-x1033.google.com with SMTP id js9so9732621pjb.2; Sun, 11 Dec 2022 20:56:17 -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=i76cNidMb9uGC4CAwhKLtXNE3wI2QcxYpZEpD1u1Q1s=; b=qac+05DFi2Ki19n7r2dcNZcKBe60yE6MB1fgho5H/VTvRfru72SZyfQkM5nLq/HTp7 kB719c3wQfDfZUY1s/Xl3gGz0O/u05D0jzdameu5/g3NVWLNGPRKHjyBaczNppFtpLGF ek8J0dXjXkIJp86aNVfZTIuQT1Az5WOm2Jn3U2umMNVgjQy3DC4n2AV+RwLXl7YGqQp9 nuZrp+H46RlIBvXrSR8ZxGKmcc5He8QIHnCHaydndejt3TZENjxhhni1g82WyA9eVpMZ aUvKm/uwEs+nQx64sNUGl17BPULc5hJpOhM7EQM/Li31MhAh1daXqSe3YUJ6VRZfCE1U ohcQ== 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=i76cNidMb9uGC4CAwhKLtXNE3wI2QcxYpZEpD1u1Q1s=; b=q63GU2aLU1QUs9kYumj9JU5mbBPyuavnXGNjmimup246Oe/SDvYFbTqyY1uTNszTqQ WOFv0Ew3mHj7AiYo6f4HqOpHymOSJlE99Grwhk0uRQ2+NcewvAAthJChWiqMDSXfYuKS K9gqotrSfsjlkA5+YJ0anaZek3z7mTsabrHbSe/0ppPy6jKJgd366u6SRdfdP++gv87l hMmfhO5922GDFUFYXi78od6LWZGhraz/E6BnDK/QKs9uqriOtlATGYMsl0mtjduVhjB2 MZWq82uKueYbP1DAxPx7uKugm+pY1XBtvQP6xE53J7Z3zwFboEPRm2YjDc0/OAGqOJ3h cyYw== X-Gm-Message-State: ANoB5pkCFeaS7F/CQoDV5M6r/hBDP23Ttp0E53hsXKqgRIByzz3OOnYG hd4g+QCbqR5M7QU9p2/BME5+3lsdVGk= X-Google-Smtp-Source: AA0mqf5SUFdVmA47z95v8dKeSyiKB6sIYOD0qipM9OzBReFZuEh3wghXpmkJr979yZeIQuUv5MiB/w== X-Received: by 2002:a17:90a:f2d0:b0:21e:a1d1:509f with SMTP id gt16-20020a17090af2d000b0021ea1d1509fmr11621796pjb.5.1670820977250; Sun, 11 Dec 2022 20:56:17 -0800 (PST) Received: from guoguo-omen.lan ([2401:c080:1400:4da2:5400:3ff:feb4:7578]) by smtp.gmail.com with ESMTPSA id s20-20020a639254000000b00478eb777d18sm4217674pgn.72.2022.12.11.20.56.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Dec 2022 20:56:16 -0800 (PST) From: Chuanhong Guo To: linux-leds@vger.kernel.org Cc: Chuanhong Guo , Krzysztof Kozlowski , 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 v4 1/3] dt-bindings: vendor-prefixes: add an entry for WorldSemi Date: Mon, 12 Dec 2022 12:55:56 +0800 Message-Id: <20221212045558.69602-2-gch981213@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221212045558.69602-1-gch981213@gmail.com> References: <20221212045558.69602-1-gch981213@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add vendor prefix for WorldSemi that makes WS2812B individually-addressable RGB LEDs. Signed-off-by: Chuanhong Guo Acked-by: Krzysztof Kozlowski --- Change since v1: reword commit message Change since v2: none Documentation/devicetree/bindings/vendor-prefixes.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Documentation/devicetree/bindings/vendor-prefixes.yaml b/Docum= entation/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=EF=BC=88SHENZHEN=EF= =BC=89Co.=EF=BC=8CLtd. "^x-powers,.*": --=20 2.38.1 From nobody Thu Sep 18 02:56:02 2025 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 7F49FC4332F for ; Mon, 12 Dec 2022 04:57:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231169AbiLLE5L (ORCPT ); Sun, 11 Dec 2022 23:57:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48936 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231308AbiLLE4m (ORCPT ); Sun, 11 Dec 2022 23:56:42 -0500 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B885DF2B; Sun, 11 Dec 2022 20:56:23 -0800 (PST) Received: by mail-pj1-x1031.google.com with SMTP id e7-20020a17090a77c700b00216928a3917so14348745pjs.4; Sun, 11 Dec 2022 20:56: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=hFOhHl9x/eniJ2ycOcv7dU0kegtvbeOEHvYNasTYocI=; b=OlFN+IION7DBMwiBPCqSNj3AKAt39E+vq/IUs0wQXylYuCxsmqzqzE1cuuNbnilinT SXcyl7R+MXQdwosi2/6h7MPMOzjxcFq9sLUv1acPC9NgaRjoBAkVUTKkY//PHbK1zhpS Z1uOcYXPuip2lXdEbxtN9qw8oCEwV3lFof5j6Cs5m8v/upV1Ie5hzv3g6zd025TlMM+t VV/BKHisO/YBE6KuoVx5yAXR19Z6nGSlIajTZNfdGBFn/t4NEvFCbCflKy6A7qfSuUBK e5bsgeZ7hMvTnLyfSSXetU4PXVJr6KTFvl8WxGhmXtJzgtHPPeDOI/imaccPqt5qYSl/ VjWw== 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=hFOhHl9x/eniJ2ycOcv7dU0kegtvbeOEHvYNasTYocI=; b=EFvTQfY5Ic/cOv+0n78mgVbj8IQPzQYeRr9TupgNv9MiziT1xCoJajrXFJjmUb6fGC JxS1HqBBxQ2fbL6ofqZsakGce+sWMalpcggN1yjdzsMKD5IEYA78VbVH0nwrJksjrMc2 V/4qSv1sSHBNXiEby7+h0/CQ09QIZSzfgXtbd0WzlCt5EfdGf8ApKjOPEuz7CnB2hPQr sZ0otjo/HvPPDtsvD1YVwGiFDOLJXTL41rq9wjtoUc+btQQYLFJpZupmO4C1m4DPF0Mj 4ESGHN7cUlDjGGpN2YRgNq9tqaZpWvzMxW5c3RiQY5GOzjQi0O84L/gbQ+fj5J1DG+Ep aPZg== X-Gm-Message-State: ANoB5pntIObUpDJLJuAAwou9MjSTlalVKZUJJuYiRzuOYhpzo26oKuJd OXSWqf3ZjVqCPLXA+UTvRHWREQfhGCA= X-Google-Smtp-Source: AA0mqf5fNdWyxkKMb0ttwLdVs8aCPILY7zkfeNFZuwun5AUkMNcMp6lK4y9iD434dsw4b5ADV0YiRg== X-Received: by 2002:a05:6a20:9c8f:b0:9d:efbf:6623 with SMTP id mj15-20020a056a209c8f00b0009defbf6623mr3207120pzb.49.1670820982551; Sun, 11 Dec 2022 20:56:22 -0800 (PST) Received: from guoguo-omen.lan ([2401:c080:1400:4da2:5400:3ff:feb4:7578]) by smtp.gmail.com with ESMTPSA id s20-20020a639254000000b00478eb777d18sm4217674pgn.72.2022.12.11.20.56.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Dec 2022 20:56: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 v4 2/3] dt-bindings: leds: add worldsemi,ws2812b Date: Mon, 12 Dec 2022 12:55:57 +0800 Message-Id: <20221212045558.69602-3-gch981213@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221212045558.69602-1-gch981213@gmail.com> References: <20221212045558.69602-1-gch981213@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add 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 =3D 1 to reg fix node names and property orders in binding example drop -spi from compatible string add default-brightness Change since v2: drop "this patch" from commit message rename leds to led-controller drop default-brightness and default-intensity Change since v3: reword commit title .../bindings/leds/worldsemi,ws2812b.yaml | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 Documentation/devicetree/bindings/leds/worldsemi,ws2812= b.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..548c05ac3d31 --- /dev/null +++ b/Documentation/devicetree/bindings/leds/worldsemi,ws2812b.yaml @@ -0,0 +1,116 @@ +# 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 cha= ined + 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 shif= ter + for the data pin. + The SPI frequency needs to be 2.105MHz~2.85MHz for the timing to be corr= ect + 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 + + 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 =3D <1>; + #size-cells =3D <0>; + + led-controller@0 { + compatible =3D "worldsemi,ws2812b"; + reg =3D <0>; + #address-cells =3D <1>; + #size-cells =3D <0>; + spi-max-frequency =3D <2850000>; + multi-led@0 { + reg =3D <0>; + color-index =3D ; + color =3D ; + function =3D LED_FUNCTION_STATUS; + function-enumerator =3D <0>; + }; + multi-led@1 { + reg =3D <1>; + color =3D ; + function =3D LED_FUNCTION_STATUS; + function-enumerator =3D <1>; + }; + multi-led@2 { + reg =3D <2>; + color =3D ; + function =3D LED_FUNCTION_STATUS; + function-enumerator =3D <2>; + }; + multi-led@3 { + reg =3D <3>; + color =3D ; + function =3D LED_FUNCTION_STATUS; + function-enumerator =3D <3>; + }; + }; + }; + +... --=20 2.38.1 From nobody Thu Sep 18 02:56:02 2025 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 16CFBC4332F for ; Mon, 12 Dec 2022 04:57:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231286AbiLLE5R (ORCPT ); Sun, 11 Dec 2022 23:57:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231281AbiLLE4p (ORCPT ); Sun, 11 Dec 2022 23:56:45 -0500 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0198DF3E; Sun, 11 Dec 2022 20:56:28 -0800 (PST) Received: by mail-pj1-x102e.google.com with SMTP id 3-20020a17090a098300b00219041dcbe9so10892356pjo.3; Sun, 11 Dec 2022 20:56:28 -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=ww/kIUj7rFU9UfXZFPnG3veXj7F0QvRe6SbpJGQVsK8=; b=EEmqzbe5K97vy7zEVE4M7bQddz/MotaYKqi4uwhapEw/CNx2EFtmWm8ITRkM36Pusp nDnc0PyK3wnOLWSAKJHUR8xbyijEeYN+0iPNrG8/4tpp2OF/Z5mZXuoXUFTw2md0kwqj e3YBb3xrlALM3UTpSvMbn31szQCK2rNswL7u2s90qk3lnlNBKKx713bhUX5dACQ3T3Zn z1IUhlzZFqLNSzr11afEq+sqere+K5J10xCN+sCv5c6VoPMUO04Iyxt18pNRFjxral+m SvmbmGlU0TzYBnAHxNMOT7qVoN06Pmz04ogodCSBBWs3scncUzgtNcPw86lua3IJBcpm HbBw== 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=ww/kIUj7rFU9UfXZFPnG3veXj7F0QvRe6SbpJGQVsK8=; b=CXD5hzVVoMza/mI3mn4KJsvfjf0qqj6iDMCVR3ACMZZ9RpgMhypPygIllDZqSc5EUd /rNdW96ZBYKyA5RmF7oXyRdlTFMeDVSqwZe96soxUUa+zSTu+nk7gd+FMDKhaSR0pf39 NSjRmx59kZMHFc6nO+Lnh/hI44odavPuVhnTIvEQVFWosnD7Fgu66pkU1kpvtcuKZcqQ Z2f+7GJToGaPWGQomsDNhTHiw92Y+tmFrGTYsOyrSQriePVWhn3R2pOeRUo56Ekeoy9r 792Yb+jakSestwCBQvFseQg7+rOf3mWdpA0pRdlRcNwb+zvQJbicM1dcMvcSerM3Qe8p s3NQ== X-Gm-Message-State: ANoB5pnMFyOwTGaA7T5M95N/iP/ao6XeTjpJqfTqDYrlGNcvkEn18ji3 CRl8kLzRqjYX6GXJ5bxQRIvp2wjAkY8= X-Google-Smtp-Source: AA0mqf6QeAtHvToCH9oAmj2zu7uSLScp23TaKKdFVB4tGdU5JACxqml6d0unF+sG8oocA68vrUfWUg== X-Received: by 2002:a17:902:d551:b0:18d:6244:4676 with SMTP id z17-20020a170902d55100b0018d62444676mr10507327plf.18.1670820987845; Sun, 11 Dec 2022 20:56:27 -0800 (PST) Received: from guoguo-omen.lan ([2401:c080:1400:4da2:5400:3ff:feb4:7578]) by smtp.gmail.com with ESMTPSA id s20-20020a639254000000b00478eb777d18sm4217674pgn.72.2022.12.11.20.56.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Dec 2022 20:56:27 -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 v4 3/3] leds: add driver for SPI driven WorldSemi WS2812B RGB LEDs Date: Mon, 12 Dec 2022 12:55:58 +0800 Message-Id: <20221212045558.69602-4-gch981213@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221212045558.69602-1-gch981213@gmail.com> References: <20221212045558.69602-1-gch981213@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 Changes since v2: drop default-brightness and default-intensity Changes since v3: 1. add more comments 2. rename reg to cascade 3. redo some line breaking 4. move duplicated pointer calculation into ws2812b_set_byte 5. reword error message 6. get ws2812b_priv from led cdev->dev->parent drivers/leds/rgb/Kconfig | 11 ++ drivers/leds/rgb/Makefile | 1 + drivers/leds/rgb/leds-ws2812b.c | 231 ++++++++++++++++++++++++++++++++ 3 files changed, 243 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 =20 If compiled as a module, the module will be named leds-qcom-lpg. =20 +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 @@ =20 obj-$(CONFIG_LEDS_PWM_MULTICOLOR) +=3D leds-pwm-multicolor.o obj-$(CONFIG_LEDS_QCOM_LPG) +=3D leds-qcom-lpg.o +obj-$(CONFIG_LEDS_WS2812B) +=3D leds-ws2812b.o diff --git a/drivers/leds/rgb/leds-ws2812b.c b/drivers/leds/rgb/leds-ws2812= b.c new file mode 100644 index 000000000000..ca9c5caabe48 --- /dev/null +++ b/drivers/leds/rgb/leds-ws2812b.c @@ -0,0 +1,231 @@ +// 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 +/* A continuous 0 for 50us+ as the 'reset' signal */ +#define WS2812B_RESET_LEN 18 + +struct ws2812b_led { + struct led_classdev_mc mc_cdev; + struct mc_subled subled[WS2812B_NUM_COLORS]; + int cascade; +}; + +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[]; +}; + +/** + * ws2812b_set_byte - convert a byte of data to 3-byte SPI data for pulses + * @priv: pointer to the private data structure + * @offset: offset of the target byte in the data stream + * @val: 1-byte data to be set + * + * WS2812B receives a stream of bytes from DI, takes the first 3 byte as L= ED + * brightness and pases the rest to the next LED through the DO pin. + * This function assembles a single byte of data to the LED: + * A bit is represented with a pulse of specific length. A long pulse is a= 1 + * and a short pulse is a 0. + * SPI transfers data continuously, MSB first. We can send 3'b100 to creat= e a + * 0 pulse and 3'b110 for a 1 pulse. In this way, a byte of data takes up 3 + * bytes in a SPI transfer: + * 1x0 1x0 1x0 1x0 1x0 1x0 1x0 1x0 + * Let's rearrange it in 8 bits: + * 1x01x01x 01x01x01 x01x01x0 + * The higher 3 bits, middle 2 bits and lower 3 bits are represented with = the + * 1st, 2nd and 3rd byte in the SPI transfer respectively. + * There are only 8 combinations for 3 bits and 4 for 2 bits, so we can cr= eate + * a lookup table for the 3 bytes. + * e.g. For 0x6b -> 2'b01101011: + * Bit 7-5: 3'b011 -> 10011011 -> 0x9b + * Bit 4-3: 2'b01 -> 01001101 -> 0x4d + * Bit 2-0: 3'b011 -> 00110110 -> 0x36 + */ +static void ws2812b_set_byte(struct ws2812b_priv *priv, size_t offset, u8 = val) +{ + /* The lookup table for Bit 7-5 4-3 2-0 */ + const u8 h3b[] =3D { 0x92, 0x93, 0x9a, 0x9b, 0xd2, 0xd3, 0xda, 0xdb }; + const u8 m2b[] =3D { 0x49, 0x4d, 0x69, 0x6d }; + const u8 l3b[] =3D { 0x24, 0x26, 0x34, 0x36, 0xa4, 0xa6, 0xb4, 0xb6 }; + u8 *p =3D priv->data_buf + WS2812B_RESET_LEN + (offset * WS2812B_BYTES_PE= R_COLOR); + + p[0] =3D h3b[val >> 5]; /* Bit 7-5 */ + p[1] =3D m2b[(val >> 3) & 0x3]; /* Bit 4-3 */ + p[2] =3D l3b[val & 0x7]; /* Bit 2-0 */ +} + +static int ws2812b_set(struct led_classdev *cdev, + enum led_brightness brightness) +{ + struct led_classdev_mc *mc_cdev =3D lcdev_to_mccdev(cdev); + struct ws2812b_led *led =3D + container_of(mc_cdev, struct ws2812b_led, mc_cdev); + struct ws2812b_priv *priv =3D dev_get_drvdata(cdev->dev->parent); + int ret; + int i; + + led_mc_calc_color_components(mc_cdev, brightness); + + mutex_lock(&priv->mutex); + for (i =3D 0; i < WS2812B_NUM_COLORS; i++) + ws2812b_set_byte(priv, led->cascade * WS2812B_NUM_COLORS + i, + led->subled[i].brightness); + ret =3D 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 =3D &spi->dev; + int cur_led =3D 0; + struct ws2812b_priv *priv; + struct fwnode_handle *led_node; + int num_leds, i, cnt, ret; + + num_leds =3D device_get_child_node_count(dev); + + priv =3D devm_kzalloc(dev, struct_size(priv, leds, num_leds), GFP_KERNEL); + if (!priv) + return -ENOMEM; + priv->data_len =3D + num_leds * WS2812B_BYTES_PER_COLOR * WS2812B_NUM_COLORS + + WS2812B_RESET_LEN; + priv->data_buf =3D kzalloc(priv->data_len, GFP_KERNEL); + if (!priv->data_buf) + return -ENOMEM; + + for (i =3D 0; i < num_leds * WS2812B_NUM_COLORS; i++) + ws2812b_set_byte(priv, i, 0); + + mutex_init(&priv->mutex); + priv->num_leds =3D num_leds; + priv->spi =3D spi; + + device_for_each_child_node(dev, led_node) { + struct led_init_data init_data =3D { + .fwnode =3D led_node, + }; + /* WS2812B LEDs usually come with GRB color */ + u32 color_idx[WS2812B_NUM_COLORS] =3D { + LED_COLOR_ID_GREEN, + LED_COLOR_ID_RED, + LED_COLOR_ID_BLUE, + }; + u32 cascade; + + ret =3D fwnode_property_read_u32(led_node, "reg", &cascade); + if (ret) { + dev_err(dev, "failed to obtain numerical LED index for %s", + fwnode_get_name(led_node)); + goto ERR_UNREG_LEDS; + } + if (cascade >=3D num_leds) { + dev_err(dev, "LED index of %s is larger than the number of LEDs.", + fwnode_get_name(led_node)); + ret =3D -EINVAL; + goto ERR_UNREG_LEDS; + } + + cnt =3D fwnode_property_count_u32(led_node, "color-index"); + if (cnt > 0 && cnt <=3D WS2812B_NUM_COLORS) + fwnode_property_read_u32_array(led_node, "color-index", + color_idx, (size_t)cnt); + + priv->leds[cur_led].mc_cdev.subled_info =3D + priv->leds[cur_led].subled; + priv->leds[cur_led].mc_cdev.num_colors =3D WS2812B_NUM_COLORS; + priv->leds[cur_led].mc_cdev.led_cdev.max_brightness =3D 255; + priv->leds[cur_led].mc_cdev.led_cdev.brightness_set_blocking =3D ws2812b= _set; + + for (i =3D 0; i < WS2812B_NUM_COLORS; i++) { + priv->leds[cur_led].subled[i].color_index =3D color_idx[i]; + priv->leds[cur_led].subled[i].intensity =3D 255; + } + + priv->leds[cur_led].cascade =3D cascade; + + ret =3D led_classdev_multicolor_register_ext( + dev, &priv->leds[cur_led].mc_cdev, &init_data); + if (ret) { + dev_err(dev, "registration of %s failed.", + fwnode_get_name(led_node)); + goto ERR_UNREG_LEDS; + } + cur_led++; + } + + spi_set_drvdata(spi, priv); + + return 0; +ERR_UNREG_LEDS: + for (; cur_led >=3D 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 =3D spi_get_drvdata(spi); + int cur_led; + + for (cur_led =3D priv->num_leds - 1; cur_led >=3D 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[] =3D { + { "ws2812b" }, + {}, +}; +MODULE_DEVICE_TABLE(spi, ws2812b_spi_ids); + +static const struct of_device_id ws2812b_dt_ids[] =3D { + { .compatible =3D "worldsemi,ws2812b" }, + {}, +}; +MODULE_DEVICE_TABLE(of, ws2812b_dt_ids); + +static struct spi_driver ws2812b_driver =3D { + .probe =3D ws2812b_probe, + .remove =3D ws2812b_remove, + .id_table =3D ws2812b_spi_ids, + .driver =3D { + .name =3D KBUILD_MODNAME, + .of_match_table =3D ws2812b_dt_ids, + }, +}; + +module_spi_driver(ws2812b_driver); + +MODULE_AUTHOR("Chuanhong Guo "); +MODULE_DESCRIPTION("WS2812B LED driver using SPI"); +MODULE_LICENSE("GPL"); --=20 2.38.1