From patchwork Wed Dec 7 04:09:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Chuanhong Guo X-Patchwork-Id: 631789 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 58B5CC47089 for ; Wed, 7 Dec 2022 04:09:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229676AbiLGEJ1 (ORCPT ); Tue, 6 Dec 2022 23:09:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49280 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229603AbiLGEJX (ORCPT ); Tue, 6 Dec 2022 23:09:23 -0500 Received: from mail-pg1-x534.google.com (mail-pg1-x534.google.com [IPv6:2607:f8b0:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97DB44E6B6; Tue, 6 Dec 2022 20:09:22 -0800 (PST) Received: by mail-pg1-x534.google.com with SMTP id q71so15180640pgq.8; Tue, 06 Dec 2022 20:09:22 -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=mFBaYqQbMTnqRYbRJV6ur40dCwgEs6PxrLe4ITOZ5srtXWXyYxdkv0ZUfXZAPpu8xE ZC+bZspLpDnkVYg4qW5+VCVltbF8BfTi349NHnGXWJHJFNYYNLKbplEpSG9LX9en5JD5 5RtkZRFwBLiH4EW2Il287r0H7r+F4YV/5+SPmWRMYTinlqu8VJkSeVCnIg6q9ZNk6EMe AOWVryJwj70sJ9sFFm2MKwb7ppLsOGAoaG5Hm0yeCZA34D9YEiVJ5Dt9445c3AHsMy97 PPV8KfzguT0YyN3u8GE6psAQNOmZxwWVaW97kZYWjdp5baC4lZ/R7K/ceDVESTnMdj4O mayw== 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=wdiP3kpYmjq4OZ7LIqRvJNY/S1M4aobS5YiMybRYvPMnyEZpQB48r0pZ69bjqq7ZRI TX8BpenlfOqTaN5oehmqUMbRbX8ICDo8BYb71emu7Zp84fP1JC4qz3ZvB4CJ7yf8+B3i XnZDXsOzqKACSW8FzRXFFpc7hbokm9FhFLdORSqoY/Z5CeE9CksDUuqQLCNB/ZVnbH+4 yh3h+jrUHTilEcGE0OEr/i1SD4MH/vKH0zcbB21fYTLOCO6Q4RBif2UhgBkfmqbYH0aC ff98cybKo7SuqNcs+Nk2kVz1thtrYrJx/5YIH1ojLGRd8MVgd1PgI6/771E1AZg91Ns1 gZIA== X-Gm-Message-State: ANoB5pn0tjG4d0/rfoljidoc2xcbFbZg1iFKtLZHiMNlzryEDPL7HhN5 6XGvCZxGXViZRlQBRgryNZXOBtSfWVU= X-Google-Smtp-Source: AA0mqf4EWXNZ4h6bTO9WwDVyprVxVYV4wOii6qrSYFqPt9zgrLtOBqgi2Gx1Ts4J1DCGsRtqpcwePQ== X-Received: by 2002:a65:64d1:0:b0:478:980a:53fb with SMTP id t17-20020a6564d1000000b00478980a53fbmr16881766pgv.484.1670386161965; Tue, 06 Dec 2022 20:09:21 -0800 (PST) Received: from guoguo-omen.lan ([2401:c080:1400:4da2:5400:3ff:feb4:7578]) by smtp.gmail.com with ESMTPSA id i15-20020a63130f000000b00478eb777d18sm587822pgl.72.2022.12.06.20.09.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Dec 2022 20:09:21 -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 v3 1/3] dt-bindings: vendor-prefixes: add an entry for WorldSemi Date: Wed, 7 Dec 2022 12:09:02 +0800 Message-Id: <20221207040904.582585-2-gch981213@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221207040904.582585-1-gch981213@gmail.com> References: <20221207040904.582585-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 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/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 Wed Dec 7 04:09:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuanhong Guo X-Patchwork-Id: 632075 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 72F64C47089 for ; Wed, 7 Dec 2022 04:09:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229733AbiLGEJf (ORCPT ); Tue, 6 Dec 2022 23:09:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229739AbiLGEJ2 (ORCPT ); Tue, 6 Dec 2022 23:09:28 -0500 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CCF4855CA6; Tue, 6 Dec 2022 20:09:27 -0800 (PST) Received: by mail-pj1-x102f.google.com with SMTP id o1-20020a17090a678100b00219cf69e5f0so323842pjj.2; Tue, 06 Dec 2022 20:09:27 -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=WvITMhNpp4wujSvPRq6ryQwFJeNS/vlhuHNThznnYLU=; b=MHmfwFJ0pE/Vk4vLr87jCFtCAyGGQ4TxNVnmlPInCdeSMqymmGH15rEa2gbb5NAW8J 8nZ5su+fZZOLrN7lMTvbbzOME0/b8kWfqBJ7oIMK5v1q46sZir8vYa8plMEXx6iah7yj adC8ySSv1J6Mg0TmzjzG/ElX0KgHc2xSacRWznyy1nggn9srdTB7i23T5hMhWYgkS7og lN30qj9nXJjYH0EjvPdenjFGgJI82Ax2SkZOzR19tMZa7uGaQFUT8gAxe2xaAZ57DWnx gBdVlgaukRnxO4vxpolWSEwtCOLCNmmgyMRKBU60Sd94WQ9kQZ/tfgo2xx+jMgyJlmGi 2VEw== 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=WvITMhNpp4wujSvPRq6ryQwFJeNS/vlhuHNThznnYLU=; b=rC1/DZFqpNevw5aWtAghTRRR0PTElICPyVY0r5idHPjc62Kz/g0jf140+iDZ0sMBIa r5qyZEw8Tfp3UoKLGXKTIA/Brog7NQwaGS8Asy3L5afPR0djnIGm+GhjuaGk/JwPyOWI TMqIetN3pQukGwvfFTf9hfH5oDr0GiAH9aeerEZ/wUh2DQxFtOV48o7Flsl/wI0ujoNc Bp+8HCRNRF0JVwXY6eNKdZSApe0pDZqPg4B1xQAs02WMRhgZeyw5njFjYAlyvzoaIX5i 9J6oaIwySN+B33ZzN0kdtSz4e3QnieUTB9IGBBgBxbVx36niVz1rlmmuE3Bg8LP2Er34 y2fg== X-Gm-Message-State: ANoB5plWNfxD5HKKCQA9oxLKCZyzwqwel8hms0hkSh5BYWHIjx05VDbt yh0W6ToRETMhtkNNS6QXn3XAQM0ms/c= X-Google-Smtp-Source: AA0mqf6cooC0fTglps51eHe3Rr73O9nXDOruiYZbI5c7SeLnr/FicWyeStJ2jBGDspyWTo+QOOx1JA== X-Received: by 2002:a17:902:bf06:b0:188:5688:828b with SMTP id bi6-20020a170902bf0600b001885688828bmr78880981plb.129.1670386167146; Tue, 06 Dec 2022 20:09:27 -0800 (PST) Received: from guoguo-omen.lan ([2401:c080:1400:4da2:5400:3ff:feb4:7578]) by smtp.gmail.com with ESMTPSA id i15-20020a63130f000000b00478eb777d18sm587822pgl.72.2022.12.06.20.09.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Dec 2022 20:09:26 -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 v3 2/3] dt-bindings: leds: add dt schema for worldsemi, ws2812b Date: Wed, 7 Dec 2022 12:09:03 +0800 Message-Id: <20221207040904.582585-3-gch981213@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221207040904.582585-1-gch981213@gmail.com> References: <20221207040904.582585-1-gch981213@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org 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 = 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 .../bindings/leds/worldsemi,ws2812b.yaml | 116 ++++++++++++++++++ 1 file changed, 116 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..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 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 + + 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>; + + led-controller@0 { + compatible = "worldsemi,ws2812b"; + reg = <0>; + #address-cells = <1>; + #size-cells = <0>; + spi-max-frequency = <2850000>; + multi-led@0 { + reg = <0>; + color-index = ; + 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 Wed Dec 7 04:09:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuanhong Guo X-Patchwork-Id: 631788 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 16F42C352A1 for ; Wed, 7 Dec 2022 04:09:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229758AbiLGEJm (ORCPT ); Tue, 6 Dec 2022 23:09:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229730AbiLGEJf (ORCPT ); Tue, 6 Dec 2022 23:09:35 -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 5574A55C8D; Tue, 6 Dec 2022 20:09:33 -0800 (PST) Received: by mail-pj1-x102d.google.com with SMTP id u15-20020a17090a3fcf00b002191825cf02so337452pjm.2; Tue, 06 Dec 2022 20:09:33 -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=GcPuIm82ikXU/VF2RTYxRO5UsZrVYF3tiHPBHWAjN2s=; b=Jt1Pt+8N3m7gI/0BL6PcmIxJr5KKLHWO792Wq2JdKI6rUSlClOf9nsnWJ3WtHFHrRU x0+zH37AJBLhX3QDWlupyCYTlHFYgqm/YEq8FO8SklrjjicgOU+tZTnm/Vb+dPeWMg7M qCbd1R3yFj3qbxQ9fyVKVP7mQZkjUDCxySe/UV7fBre7nA5mrliQJJhBf0u5T5GY9g35 MsBjLqdc8lXfIOBUlqw0Xb3ycMYGL+fDMOunUEEH4VW8/3IFiA0jdhFtEyb8X3NKlNe7 2QVL0yFjyr6ISzxzET1LIcPsI38P4j5ecba6Kj2tURIdqwKK8vl5ENRkEvJAou/wZ6lH tjoA== 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=GcPuIm82ikXU/VF2RTYxRO5UsZrVYF3tiHPBHWAjN2s=; b=FHMQGprO3C9I5y7Q/K05kV9CGWMJUu7ldyOvATpM5+ojZjQOQo5u3iCVOlcMXwqYx+ 7ZYv13cBMsEC66j+ZFTTTIIldtyDIVgaP8XAnbVEVL5hGrHnEvOJ8fZ/YrUDCkSbAK/Z GH1j5jD/A4kAULG7FFaJPF5+8LUpMEoMH8tVMzKPsAGomnbnPhgHGOoEyX90oLd4RInf 7n9Gv3XDpVrXmxW3ljuuJLdlHfpa1majjq5aZ0Z8UetCfsXI4ozux8NwKOxuSXvCjssm IaKpvfr4KRrQU7gQ6jDZ45PQk6zNJ7Jn9Cirzq5/0DytipozKWQKdiGPdeRBNDZR9sCh F0jQ== X-Gm-Message-State: ANoB5pnDA0tAdj6Y3Z/CpioBIBwl+B/iJx7A2J79ivJaecZlHHLpWEdS V1HsSORAeD+gny+JyGKOQu8SIT0Lcn4= X-Google-Smtp-Source: AA0mqf5PYyNSRTZWSF4qhgpXGHZxCikztuQGUmUOx6bObckd7dOdoPySEe9lQeJr2gYdVItXJzGrWA== X-Received: by 2002:a17:902:a618:b0:189:72c2:67fe with SMTP id u24-20020a170902a61800b0018972c267femr53967501plq.135.1670386172512; Tue, 06 Dec 2022 20:09:32 -0800 (PST) Received: from guoguo-omen.lan ([2401:c080:1400:4da2:5400:3ff:feb4:7578]) by smtp.gmail.com with ESMTPSA id i15-20020a63130f000000b00478eb777d18sm587822pgl.72.2022.12.06.20.09.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Dec 2022 20:09:32 -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 v3 3/3] leds: add driver for SPI driven WorldSemi WS2812B RGB LEDs Date: Wed, 7 Dec 2022 12:09:04 +0800 Message-Id: <20221207040904.582585-4-gch981213@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221207040904.582585-1-gch981213@gmail.com> References: <20221207040904.582585-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 Changes since v2: drop default-brightness and default-intensity drivers/leds/rgb/Kconfig | 11 ++ drivers/leds/rgb/Makefile | 1 + drivers/leds/rgb/leds-ws2812b.c | 219 ++++++++++++++++++++++++++++++++ 3 files changed, 231 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..68c80beb304c --- /dev/null +++ b/drivers/leds/rgb/leds-ws2812b.c @@ -0,0 +1,219 @@ +// 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, + }; + + 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); + + 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 = 255; + } + + 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" }, + {}, +}; +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");