From nobody Wed Dec 17 15:39:25 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 75925C61D97 for ; Fri, 24 Nov 2023 09:33:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345154AbjKXJdV (ORCPT ); Fri, 24 Nov 2023 04:33:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229708AbjKXJdT (ORCPT ); Fri, 24 Nov 2023 04:33:19 -0500 Received: from mx0a-002bf204.pphosted.com (mx0a-002bf204.pphosted.com [205.220.160.90]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3BB41EA; Fri, 24 Nov 2023 01:33:25 -0800 (PST) Received: from pps.filterd (m0207524.ppops.net [127.0.0.1]) by mx0a-002bf204.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 3AO5K3s2023268; Fri, 24 Nov 2023 01:33:17 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= monolithicpower.com; h=from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding :content-type; s=pps1; bh=yvkonXUAdwEEl/zvVVNJg+Y4EulCeGGUusjlzL PCYQs=; b=J2PfM+Q3e9dpzoxNEz7j+M93Yz0hwtNXn2IF2RtLp4zAopV69ADBuq wSdEUS1Wol7P37ZkTjtIoEXnx43X+AEJdjF5x+rucZ6tbDFo6xZPP1qOFLT3BdcZ 0nScGw6K2k/+qYxnonPP12D8y6q5vJivPSTc5TuOEmfnnFwj7RxcZz2rwn6VjSYk GLPplR/9p+AwtlRT23E37WGU/fSb+0yAPQoip7WWilYKa7t8tG7F6Zdqu/42/fHG bUyqnfjVfSfYVrBMq4Gm9g93NJA2uBFmNCT/YOb53u9GYiAVYgBnuIx2p0FbjJxz 2LhwKaT+qxrZc6aPHR/0yDrRJFz6UT4g== Received: from webmail.monolithicpower.com (mps-vpn.monolithicpower.com [12.33.0.20] (may be forged)) by mx0a-002bf204.pphosted.com (PPS) with ESMTPS id 3uesjsb24f-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Fri, 24 Nov 2023 01:33:17 -0800 (PST) Received: from CD-MSH04.monolithicpower.com (10.10.70.213) by mps-mslbn01.monolithicpower.com (10.10.10.199) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.7; Fri, 24 Nov 2023 01:33:10 -0800 Received: from smtp.gmail.com (10.10.86.78) by CD-MSH04.monolithicpower.com (10.10.70.213) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2242.4; Fri, 24 Nov 2023 17:33:08 +0800 From: Yuxi Wang To: , , , , , CC: , , , Subject: [PATCH 1/2] dt-bindings: leds: add mps mp3326 LED Date: Fri, 24 Nov 2023 17:30:32 +0800 Message-ID: <20231124093034.951-2-Yuxi.Wang@monolithicpower.com> X-Mailer: git-send-email 2.42.0.windows.2 In-Reply-To: <20231124093034.951-1-Yuxi.Wang@monolithicpower.com> References: <20231124093034.951-1-Yuxi.Wang@monolithicpower.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.10.86.78] X-ClientProxiedBy: CD-MSLBN02.monolithicpower.com (192.168.86.32) To CD-MSH04.monolithicpower.com (10.10.70.213) X-Proofpoint-ORIG-GUID: 2kApOr8jzCQAIfAeHQ5EJBUjP3W0m1me X-Proofpoint-GUID: 2kApOr8jzCQAIfAeHQ5EJBUjP3W0m1me X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.987,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-11-23_15,2023-11-22_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 adultscore=0 lowpriorityscore=0 malwarescore=0 phishscore=0 priorityscore=1501 impostorscore=0 clxscore=1015 bulkscore=0 mlxlogscore=971 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2311060001 definitions=main-2311240074 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Document mps mp3326 LED driver devicetree bindings. Signed-off-by: Yuxi Wang --- .../devicetree/bindings/leds/mps,mp3326.yaml | 143 ++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 Documentation/devicetree/bindings/leds/mps,mp3326.yaml diff --git a/Documentation/devicetree/bindings/leds/mps,mp3326.yaml b/Docum= entation/devicetree/bindings/leds/mps,mp3326.yaml new file mode 100644 index 000000000000..5f6bde46ccc8 --- /dev/null +++ b/Documentation/devicetree/bindings/leds/mps,mp3326.yaml @@ -0,0 +1,143 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/leds/mps,mp3326.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: MPS MP3326 RGB/White LED Driver + +maintainers: + - Yuxi Wang + +description: | + The MP3326 is a RGB/White LED driver with I2C interface. + + For more product information please see the link below: + https://www.monolithicpower.com/en/products/mp3326.html + +properties: + compatible: + const: mps,mp3326 + + "#address-cells": + const: 1 + + "#size-cells": + const: 0 + + reg: + maxItems: 1 + + multi-led: + type: object + unevaluatedProperties: false + + properties: + "#address-cells": + const: 1 + + "#size-cells": + const: 0 + + color: + description: RGB module + + patternProperties: + "^led@[0-3]$": + type: object + $ref: common.yaml# + unevaluatedProperties: false + + properties: + reg: + description: Index of the LED. + minimum: 1 + maximum: 16 + + required: + - reg + - color + + required: + - "#address-cells" + - "#size-cells" + - color + +patternProperties: + "^led@[0-9]$": + type: object + $ref: common.yaml# + unevaluatedProperties: false + + properties: + reg: + description: Index of the LED. + minimum: 1 + maximum: 16 + + required: + - reg + +required: + - compatible + - reg + - "#address-cells" + - "#size-cells" + +additionalProperties: false + +examples: + - | + #include + + i2c { + #address-cells =3D <1>; + #size-cells =3D <0>; + + led-controller@30 { + #address-cells =3D <1>; + #size-cells =3D <0>; + compatible =3D "mps,mp3326"; + reg =3D <0x30>; + multi-led { + #address-cells =3D <1>; + #size-cells =3D <0>; + color =3D ; + + led@0 { + reg =3D <1>; + color =3D ; + }; + + led@1 { + reg =3D <2>; + color =3D ; + }; + + led@2 { + reg =3D <3>; + color =3D ; + }; + }; + }; + }; + + - | + #include + + i2c { + #address-cells =3D <1>; + #size-cells =3D <0>; + led-controller@30 { + compatible =3D "mps,mp3326"; + reg =3D <0x30>; + #address-cells =3D <1>; + #size-cells =3D <0>; + + led@0 { + reg=3D <5>; + color =3D ; + }; + }; + }; +... --=20 2.25.1 From nobody Wed Dec 17 15:39:25 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 627CFC61D97 for ; Fri, 24 Nov 2023 09:33:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233108AbjKXJdg (ORCPT ); Fri, 24 Nov 2023 04:33:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37068 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233150AbjKXJdc (ORCPT ); Fri, 24 Nov 2023 04:33:32 -0500 Received: from mx0b-002bf204.pphosted.com (mx0b-002bf204.pphosted.com [205.220.172.86]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D10DCD54; Fri, 24 Nov 2023 01:33:36 -0800 (PST) Received: from pps.filterd (m0207525.ppops.net [127.0.0.1]) by mx0a-002bf204.pphosted.com (8.17.1.22/8.17.1.22) with ESMTP id 3AO4uPox030525; Fri, 24 Nov 2023 01:33:21 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= monolithicpower.com; h=from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding :content-type; s=pps1; bh=0eqek6SdnZkhx1HKqsJRJpuItss4lBBKCj/FFi NqpzY=; b=EBiHd/UlAZ7k4UETabXkoAfU/kZ8g1HcfePd4r4wnpX+zJ/KAxU0PW PHYvSxxAah7Ormii0WU9y1g5oGYnj5X4A+ofTASJzHcBGwGj2DevcxL7N/d3LD61 ZUfmuHlXAgkrsu/ad2UMV/onf7Y3GIOcOGxxSk6S9Bh43UFx/CKpXHjZdwscW4ow ho/GbsFlf7XHk5Mk7cazaXCK4yS2J/WBCh3Dpx+9+6griHx7QHNi5HI7+4e6aMPN wIrzilFINNcOWwtpFZ07mM2/svzo1nukKuWkec4j+ycZvHfsE+kwsd6NSi3E1HhU M8gh2SJp3qVHzIczzIDzK2A+67FRABYw== Received: from webmail.monolithicpower.com (mps-mslbn02.monolithicpower.com [12.192.228.33]) by mx0a-002bf204.pphosted.com (PPS) with ESMTPS id 3uetu4rc0a-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Fri, 24 Nov 2023 01:33:21 -0800 (PST) Received: from CD-MSH04.monolithicpower.com (10.10.70.213) by mps-mslbn02.monolithicpower.com (12.192.228.33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.7; Fri, 24 Nov 2023 01:33:13 -0800 Received: from smtp.gmail.com (10.10.86.78) by CD-MSH04.monolithicpower.com (10.10.70.213) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2242.4; Fri, 24 Nov 2023 17:33:11 +0800 From: Yuxi Wang To: , , , , , CC: , , , Subject: [PATCH 2/2] leds: add mp3326 driver Date: Fri, 24 Nov 2023 17:30:33 +0800 Message-ID: <20231124093034.951-3-Yuxi.Wang@monolithicpower.com> X-Mailer: git-send-email 2.42.0.windows.2 In-Reply-To: <20231124093034.951-1-Yuxi.Wang@monolithicpower.com> References: <20231124093034.951-1-Yuxi.Wang@monolithicpower.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.10.86.78] X-ClientProxiedBy: CD-MSLBN02.monolithicpower.com (192.168.86.32) To CD-MSH04.monolithicpower.com (10.10.70.213) X-Proofpoint-GUID: HMIacrSXq9WNaGy41g6tZA0idzQqjgoA X-Proofpoint-ORIG-GUID: HMIacrSXq9WNaGy41g6tZA0idzQqjgoA X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.987,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-11-23_15,2023-11-22_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 bulkscore=0 adultscore=0 suspectscore=0 clxscore=1015 mlxlogscore=999 mlxscore=0 impostorscore=0 spamscore=0 malwarescore=0 lowpriorityscore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2311060001 definitions=main-2311240074 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This commit adds support for MPS MP3326 LED driver. Signed-off-by: Yuxi Wang --- drivers/leds/Kconfig | 7 + drivers/leds/Makefile | 1 + drivers/leds/leds-mp3326.c | 622 +++++++++++++++++++++++++++++++++++++ 3 files changed, 630 insertions(+) create mode 100644 drivers/leds/leds-mp3326.c diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig index bfa11e7b157f..f9da210c89a2 100644 --- a/drivers/leds/Kconfig +++ b/drivers/leds/Kconfig @@ -262,6 +262,13 @@ config LEDS_MIKROTIK_RB532 This option enables support for the so called "User LED" of Mikrotik's Routerboard 532. =20 +config LEDS_MP3326 + tristate "LED Support for MPS MP3326" + depends on LEDS_CLASS + help + This option enables support for on-chip LED drivers found on + MPS MP3326. + config LEDS_MT6323 tristate "LED Support for Mediatek MT6323 PMIC" depends on LEDS_CLASS diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile index ce07dc295ff0..c0a8b4b3e7c8 100644 --- a/drivers/leds/Makefile +++ b/drivers/leds/Makefile @@ -64,6 +64,7 @@ obj-$(CONFIG_LEDS_MENF21BMC) +=3D leds-menf21bmc.o obj-$(CONFIG_LEDS_MIKROTIK_RB532) +=3D leds-rb532.o obj-$(CONFIG_LEDS_MLXCPLD) +=3D leds-mlxcpld.o obj-$(CONFIG_LEDS_MLXREG) +=3D leds-mlxreg.o +obj-$(CONFIG_LEDS_MP3326) +=3D leds-mp3326.o obj-$(CONFIG_LEDS_MT6323) +=3D leds-mt6323.o obj-$(CONFIG_LEDS_NET48XX) +=3D leds-net48xx.o obj-$(CONFIG_LEDS_NETXBIG) +=3D leds-netxbig.o diff --git a/drivers/leds/leds-mp3326.c b/drivers/leds/leds-mp3326.c new file mode 100644 index 000000000000..aca95da8c839 --- /dev/null +++ b/drivers/leds/leds-mp3326.c @@ -0,0 +1,622 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * MP3326 Led driver + * + * Copyright 2023 Monolithic Power Systems, Inc + * + * Author: Yuxi Wang + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MP3326_PWM_DIM_FREQUENCY_CONFIG 0x00 +#define MP3326_PWM_CTRL 0x01 +#define MP3326_PWM_DIM_FREQUENCY_CONFIG 0x00 +#define MP3326_PWM_CTRL_CHANNEL_9_16 0x04 +#define MP3326_PWM_CTRL_CHANNEL_1_8 0x05 +#define MP3326_PWM_OPEN_FAULT_CHANNEL_9_16 0x06 +#define MP3326_PWM_OPEN_FAULT_CHANNEL_1_8 0x07 +#define MP3326_PWM_SHORT_FAULT_CHANNEL_9_16 0x08 +#define MP3326_PWM_SHORT_FAULT_CHANNEL_1_8 0x09 +#define MP3326_PWM_CURRENT_SET_CHANNEL1 0x0A +#define MP3326_PWM_DUTY_LSB_SET_CHANNEL1 0x0B +#define MP3326_PWM_DUTY_MSB_SET_CHANNEL1 0x0C +#define MP3326_PWM_CURRENT_SET_CHANNEL2 0x0D +#define MP3326_PWM_DUTY_LSB_SET_CHANNEL2 0x0E +#define MP3326_PWM_DUTY_MSB_SET_CHANNEL2 0x0F +#define MP3326_PWM_CURRENT_SET_CHANNEL3 0x10 +#define MP3326_PWM_DUTY_LSB_SET_CHANNEL3 0x11 +#define MP3326_PWM_DUTY_MSB_SET_CHANNEL3 0x12 +#define MP3326_PWM_CURRENT_SET_CHANNEL4 0x13 +#define MP3326_PWM_DUTY_LSB_SET_CHANNEL4 0x14 +#define MP3326_PWM_DUTY_MSB_SET_CHANNEL4 0x15 +#define MP3326_PWM_CURRENT_SET_CHANNEL5 0x16 +#define MP3326_PWM_DUTY_LSB_SET_CHANNEL5 0x17 +#define MP3326_PWM_DUTY_MSB_SET_CHANNEL5 0x18 +#define MP3326_PWM_CURRENT_SET_CHANNEL6 0x19 +#define MP3326_PWM_DUTY_LSB_SET_CHANNEL6 0x1A +#define MP3326_PWM_DUTY_MSB_SET_CHANNEL6 0x1B +#define MP3326_PWM_CURRENT_SET_CHANNEL7 0x1C +#define MP3326_PWM_DUTY_LSB_SET_CHANNEL7 0x1D +#define MP3326_PWM_DUTY_MSB_SET_CHANNEL7 0x1E +#define MP3326_PWM_CURRENT_SET_CHANNEL8 0x1F +#define MP3326_PWM_DUTY_LSB_SET_CHANNEL8 0x20 +#define MP3326_PWM_DUTY_MSB_SET_CHANNEL8 0x21 +#define MP3326_PWM_CURRENT_SET_CHANNEL9 0x22 +#define MP3326_PWM_DUTY_LSB_SET_CHANNEL9 0x23 +#define MP3326_PWM_DUTY_MSB_SET_CHANNEL9 0x24 +#define MP3326_PWM_CURRENT_SET_CHANNEL10 0x25 +#define MP3326_PWM_DUTY_LSB_SET_CHANNEL10 0x26 +#define MP3326_PWM_DUTY_MSB_SET_CHANNEL10 0x27 +#define MP3326_PWM_CURRENT_SET_CHANNEL11 0x28 +#define MP3326_PWM_DUTY_LSB_SET_CHANNEL11 0x29 +#define MP3326_PWM_DUTY_MSB_SET_CHANNEL11 0x2A +#define MP3326_PWM_CURRENT_SET_CHANNEL12 0x2B +#define MP3326_PWM_DUTY_LSB_SET_CHANNEL12 0x2C +#define MP3326_PWM_DUTY_MSB_SET_CHANNEL12 0x2D +#define MP3326_PWM_CURRENT_SET_CHANNEL13 0x2E +#define MP3326_PWM_DUTY_LSB_SET_CHANNEL13 0x2F +#define MP3326_PWM_DUTY_MSB_SET_CHANNEL13 0x30 +#define MP3326_PWM_CURRENT_SET_CHANNEL14 0x31 +#define MP3326_PWM_DUTY_LSB_SET_CHANNEL14 0x32 +#define MP3326_PWM_DUTY_MSB_SET_CHANNEL14 0x33 +#define MP3326_PWM_CURRENT_SET_CHANNEL15 0x34 +#define MP3326_PWM_DUTY_LSB_SET_CHANNEL15 0x35 +#define MP3326_PWM_DUTY_MSB_SET_CHANNEL15 0x36 +#define MP3326_PWM_CURRENT_SET_CHANNEL16 0x37 +#define MP3326_PWM_DUTY_LSB_SET_CHANNEL16 0x38 +#define MP3326_PWM_DUTY_MSB_SET_CHANNEL16 0x39 +#define MAX_BRIGHTNESS 63 + +enum led_ctrl { + ENABLE =3D 0, + BRIGHTNESS, + COLOR_L4, + COLOR_H8, + OPEN_FAULT, + SHORT_FAULT, + MAX_CTRL, +}; + +enum mp3326_channel { + CHANNEL1, + CHANNEL2, + CHANNEL3, + CHANNEL4, + CHANNEL5, + CHANNEL6, + CHANNEL7, + CHANNEL8, + CHANNEL9, + CHANNEL10, + CHANNEL11, + CHANNEL12, + CHANNEL13, + CHANNEL14, + CHANNEL15, + CHANNEL16, + MAX_CHANNEL, +}; + +#define MP3326_REG_CONNECT_INNER(prefix, range) prefix##range +#define MP3326_REG_CONNECT(prefix, range) MP3326_REG_CONNECT_INNER(prefix= , range) +#define MP3326_REG_FIELD(reg, minbit, maxbit) REG_FIELD(reg, minbit, maxbi= t) +#define RANGE(a, b) MP3326_REG_CONNECT_INNER(a, b) + +#define MP3326_CHANNEL_FIELD(bit, num, range) { \ + MP3326_REG_FIELD(MP3326_REG_CONNECT(MP3326_PWM_CTRL_CHANNEL, range), bit,= bit), \ + MP3326_REG_FIELD(MP3326_REG_CONNECT(MP3326_PWM_CURRENT_SET_CHANNEL, num),= 0, 5), \ + MP3326_REG_FIELD(MP3326_REG_CONNECT(MP3326_PWM_DUTY_LSB_SET_CHANNEL, num)= , 0, 3), \ + MP3326_REG_FIELD(MP3326_REG_CONNECT(MP3326_PWM_DUTY_MSB_SET_CHANNEL, num)= , 0, 7), \ + MP3326_REG_FIELD(MP3326_REG_CONNECT(MP3326_PWM_OPEN_FAULT_CHANNEL, range)= , bit, bit), \ + MP3326_REG_FIELD(MP3326_REG_CONNECT(MP3326_PWM_SHORT_FAULT_CHANNEL, range= ), bit, bit), \ + } +struct mp3326_led { + struct mp3326 *private_data; + struct led_classdev cdev; + struct mc_subled *subled_info; + int num_colors; +}; + +struct mp3326 { + struct i2c_client *client; + struct regmap *regmap; + struct regmap_field *regmap_fields[MAX_CHANNEL][MAX_CTRL]; + int num_of_leds; + struct mp3326_led leds[]; +}; + +static const struct regmap_config MP3326_regmap_config =3D { + .reg_bits =3D 8, + .val_bits =3D 8, +}; + +static const struct reg_field channels_reg_fields[MAX_CHANNEL][MAX_CTRL] = =3D { + [CHANNEL1] =3D MP3326_CHANNEL_FIELD(0, 1, RANGE(_1, _8)), + [CHANNEL2] =3D MP3326_CHANNEL_FIELD(1, 2, RANGE(_1, _8)), + [CHANNEL3] =3D MP3326_CHANNEL_FIELD(2, 3, RANGE(_1, _8)), + [CHANNEL4] =3D MP3326_CHANNEL_FIELD(3, 4, RANGE(_1, _8)), + [CHANNEL5] =3D MP3326_CHANNEL_FIELD(4, 5, RANGE(_1, _8)), + [CHANNEL6] =3D MP3326_CHANNEL_FIELD(5, 6, RANGE(_1, _8)), + [CHANNEL7] =3D MP3326_CHANNEL_FIELD(6, 7, RANGE(_1, _8)), + [CHANNEL8] =3D MP3326_CHANNEL_FIELD(7, 8, RANGE(_1, _8)), + [CHANNEL9] =3D MP3326_CHANNEL_FIELD(0, 9, RANGE(_9, _16)), + [CHANNEL10] =3D MP3326_CHANNEL_FIELD(1, 10, RANGE(_9, _16)), + [CHANNEL11] =3D MP3326_CHANNEL_FIELD(2, 11, RANGE(_9, _16)), + [CHANNEL12] =3D MP3326_CHANNEL_FIELD(3, 12, RANGE(_9, _16)), + [CHANNEL13] =3D MP3326_CHANNEL_FIELD(4, 13, RANGE(_9, _16)), + [CHANNEL14] =3D MP3326_CHANNEL_FIELD(5, 14, RANGE(_9, _16)), + [CHANNEL15] =3D MP3326_CHANNEL_FIELD(6, 15, RANGE(_9, _16)), + [CHANNEL16] =3D MP3326_CHANNEL_FIELD(7, 16, RANGE(_9, _16)), +}; + +static int led_brightness_set(struct led_classdev *led_cdev, enum led_brig= htness brightness) +{ + struct mp3326_led *led =3D container_of(led_cdev, struct mp3326_led, cdev= ); + struct mp3326 *chip =3D led->private_data; + int ret; + int i; + + if (brightness > led_cdev->max_brightness) + brightness =3D led_cdev->max_brightness; + if (brightness < 0) + brightness =3D 0; + for (i =3D 0; i < led->num_colors; i++) { + ret =3D regmap_field_write( + chip->regmap_fields[led->subled_info[i].channel][BRIGHTNESS], + brightness); + if (ret) + return ret; + led->subled_info[i].brightness =3D brightness; + } + led_cdev->brightness =3D brightness; + return 0; +} + +static ssize_t led_pwm_store(struct device *dev, struct device_attribute *= attr, + const char *buf, size_t count) +{ + struct led_classdev *lcdev =3D dev_get_drvdata(dev); + struct mp3326_led *led =3D container_of(lcdev, struct mp3326_led, cdev); + struct mp3326 *chip =3D led->private_data; + ssize_t ret; + int r_val, g_val, b_val; + int i; + + ret =3D sscanf(buf, "%i %i %i", &r_val, &g_val, &b_val); + if (ret !=3D 3 && ret !=3D 1) + return ret; + r_val =3D r_val * 4095 / 255 + (r_val * 4095 % 255) / (255 / 2); + g_val =3D g_val * 4095 / 255 + (g_val * 4095 % 255) / (255 / 2); + b_val =3D b_val * 4095 / 255 + (b_val * 4095 % 255) / (255 / 2); + for (i =3D 0; i < led->num_colors; i++) { + switch (led->subled_info[i].color_index) { + case LED_COLOR_ID_RED: + ret =3D regmap_field_write( + chip->regmap_fields[led->subled_info[i].channel][COLOR_L4], + r_val & 0x0f); + if (ret) + return ret; + ret =3D regmap_field_write( + chip->regmap_fields[led->subled_info[i].channel][COLOR_H8], + r_val >> 4); + if (ret) + return ret; + break; + case LED_COLOR_ID_GREEN: + ret =3D regmap_field_write( + chip->regmap_fields[led->subled_info[i].channel][COLOR_L4], + g_val & 0x0f); + if (ret) + return ret; + ret =3D regmap_field_write( + chip->regmap_fields[led->subled_info[i].channel][COLOR_H8], + g_val >> 4); + if (ret) + return ret; + break; + case LED_COLOR_ID_BLUE: + ret =3D regmap_field_write( + chip->regmap_fields[led->subled_info[i].channel][COLOR_L4], + b_val & 0x0f); + if (ret) + return ret; + ret =3D regmap_field_write( + chip->regmap_fields[led->subled_info[i].channel][COLOR_H8], + b_val >> 4); + if (ret) + return ret; + break; + default: + ret =3D regmap_field_write( + chip->regmap_fields[led->subled_info[i].channel][COLOR_L4], + r_val & 0x0f); + if (ret) + return ret; + ret =3D regmap_field_write( + chip->regmap_fields[led->subled_info[i].channel][COLOR_H8], + r_val >> 4); + if (ret) + return ret; + break; + } + } + return count; +} + +static ssize_t led_pwm_show(struct device *dev, struct device_attribute *a= ttr, + char *buf) +{ + struct led_classdev *lcdev =3D dev_get_drvdata(dev); + struct mp3326_led *led =3D container_of(lcdev, struct mp3326_led, cdev); + struct mp3326 *chip =3D led->private_data; + ssize_t ret; + int r_val =3D 0, g_val =3D 0, b_val =3D 0, val; + int i; + + for (i =3D 0; i < led->num_colors; i++) { + switch (led->subled_info[i].color_index) { + case LED_COLOR_ID_RED: + ret =3D regmap_field_read( + chip->regmap_fields[led->subled_info[i].channel][COLOR_L4], &val); + if (ret) + return ret; + r_val |=3D val; + ret =3D regmap_field_read( + chip->regmap_fields[led->subled_info[i].channel][COLOR_H8], &val); + if (ret) + return ret; + r_val |=3D val << 4; + break; + case LED_COLOR_ID_GREEN: + ret =3D regmap_field_read( + chip->regmap_fields[led->subled_info[i].channel][COLOR_L4], &val); + if (ret) + return ret; + g_val |=3D val; + ret =3D regmap_field_read( + chip->regmap_fields[led->subled_info[i].channel][COLOR_H8], &val); + if (ret) + return ret; + g_val |=3D val << 4; + break; + case LED_COLOR_ID_BLUE: + ret =3D regmap_field_read( + chip->regmap_fields[led->subled_info[i].channel][COLOR_L4], &val); + if (ret) + return ret; + b_val |=3D val; + ret =3D regmap_field_read( + chip->regmap_fields[led->subled_info[i].channel][COLOR_H8], &val); + if (ret) + return ret; + b_val |=3D val << 4; + break; + default: + ret =3D regmap_field_read( + chip->regmap_fields[led->subled_info[i].channel][COLOR_L4], &val); + if (ret) + return ret; + r_val |=3D val; + ret =3D regmap_field_read( + chip->regmap_fields[led->subled_info[i].channel][COLOR_H8], &val); + if (ret) + return ret; + r_val |=3D val << 4; + break; + } + } + r_val =3D r_val * 255 / 4095 + (r_val * 255 % 4095) / (4095 / 2); + g_val =3D g_val * 255 / 4095 + (g_val * 255 % 4095) / (4095 / 2); + b_val =3D b_val * 255 / 4095 + (b_val * 255 % 4095) / (4095 / 2); + if (led->num_colors =3D=3D 1) + return sysfs_emit(buf, "0x%x\n", r_val); + else + return sysfs_emit(buf, "0x%x 0x%x 0x%x\n", r_val, g_val, b_val); +} + +static ssize_t led_enable_store(struct device *dev, struct device_attribut= e *attr, + const char *buf, size_t count) +{ + struct led_classdev *lcdev =3D dev_get_drvdata(dev); + struct mp3326_led *led =3D container_of(lcdev, struct mp3326_led, cdev); + struct mp3326 *chip =3D led->private_data; + ssize_t ret; + uint val, i; + + ret =3D kstrtouint(buf, 0, &val); + if (ret) + return ret; + for (i =3D 0; i < led->num_colors; i++) { + ret =3D regmap_field_write( + chip->regmap_fields[led->subled_info[i].channel][BRIGHTNESS], + led->subled_info[i].brightness); + if (ret) + return ret; + ret =3D regmap_field_write( + chip->regmap_fields[led->subled_info[i].channel][ENABLE], !!val); + if (ret) + return ret; + } + + return count; +} + +static ssize_t led_enable_show(struct device *dev, struct device_attribute= *attr, + char *buf) +{ + struct led_classdev *lcdev =3D dev_get_drvdata(dev); + struct mp3326_led *led =3D container_of(lcdev, struct mp3326_led, cdev); + struct mp3326 *chip =3D led->private_data; + uint val, rval =3D 0; + int i, ret; + + for (i =3D 0; i < led->num_colors; i++) { + ret =3D regmap_field_read( + chip->regmap_fields[led->subled_info[i].channel][ENABLE], &val); + + rval |=3D val << i; + if (ret) + return ret; + } + + if (rval) + return sysfs_emit(buf, "%s\n", "True"); + else + return sysfs_emit(buf, "%s\n", "False"); +} + +static ssize_t led_short_fault_show(struct device *dev, struct device_attr= ibute *attr, + char *buf) +{ + struct led_classdev *lcdev =3D dev_get_drvdata(dev); + struct mp3326_led *led =3D container_of(lcdev, struct mp3326_led, cdev); + struct mp3326 *chip =3D led->private_data; + uint val, rval =3D 0, i; + int ret; + + for (i =3D 0; i < led->num_colors; i++) { + ret =3D regmap_field_read( + chip->regmap_fields[led->subled_info[i].channel][SHORT_FAULT], &val); + rval |=3D val << i; + if (ret) + return ret; + } + + if (rval) + return sysfs_emit(buf, "%s\n", "Occur"); + else + return sysfs_emit(buf, "%s\n", "None"); +} + +static ssize_t led_open_fault_show(struct device *dev, struct device_attri= bute *attr, + char *buf) +{ + struct led_classdev *lcdev =3D dev_get_drvdata(dev); + struct mp3326_led *led =3D container_of(lcdev, struct mp3326_led, cdev); + struct mp3326 *chip =3D led->private_data; + uint val, rval =3D 0, i; + int ret; + + for (i =3D 0; i < led->num_colors; i++) { + ret =3D regmap_field_read( + chip->regmap_fields[led->subled_info[i].channel][OPEN_FAULT], &val); + rval |=3D val << i; + if (ret) + return ret; + } + + if (rval) + return sysfs_emit(buf, "%s\n", "Occur"); + else + return sysfs_emit(buf, "%s\n", "None"); +} + +static DEVICE_ATTR_RW(led_pwm); +static DEVICE_ATTR_RW(led_enable); +static DEVICE_ATTR_RO(led_short_fault); +static DEVICE_ATTR_RO(led_open_fault); + +static struct attribute *led_sysfs_attrs[] =3D { + &dev_attr_led_pwm.attr, + &dev_attr_led_enable.attr, + &dev_attr_led_short_fault.attr, + &dev_attr_led_open_fault.attr, + NULL, +}; + +ATTRIBUTE_GROUPS(led_sysfs); + +static int mp3326_add_led(struct mp3326 *chip, struct device_node *np, int= index) +{ + struct mp3326_led *led =3D &chip->leds[index]; + struct mc_subled *info; + struct device_node *child; + struct led_classdev *cdev; + struct led_init_data init_data =3D {}; + int ret; + int i =3D 0; + int count; + u32 color =3D 0; + u32 reg =3D 0; + + ret =3D of_property_read_u32(np, "color", &color); + if (ret) { + dev_err(&chip->client->dev, "Miss color in the node\n"); + return ret; + } + led->private_data =3D chip; + if (color =3D=3D LED_COLOR_ID_RGB) { + count =3D of_get_child_count(np); + if (count !=3D 3) { + dev_err(&chip->client->dev, "RGB must have three node.\n"); + return -EINVAL; + } + + info =3D devm_kcalloc(&chip->client->dev, 3, sizeof(*info), GFP_KERNEL); + if (!info) + return -ENOMEM; + + for_each_available_child_of_node(np, child) { + ret =3D of_property_read_u32(child, "reg", ®); + if (ret || reg > MAX_CHANNEL) { + dev_err(&chip->client->dev, + "reg must less or equal than %d\n", MAX_CHANNEL); + return -EINVAL; + } + + ret =3D of_property_read_u32(child, "color", &color); + if (ret) { + dev_err(&chip->client->dev, "color must have value\n"); + return ret; + } + + if (color > 3 || !color) { + dev_err(&chip->client->dev, + "color must be Red, Green and Blue. The color is %d\n", color); + return ret; + } + info[i].color_index =3D color; + info[i].channel =3D reg - 1; + info[i].brightness =3D 0; + i++; + } + + led->subled_info =3D info; + led->num_colors =3D 3; + cdev =3D &led->cdev; + cdev->max_brightness =3D MAX_BRIGHTNESS; + cdev->brightness_set_blocking =3D led_brightness_set; + cdev->groups =3D led_sysfs_groups; + init_data.fwnode =3D &np->fwnode; + + ret =3D devm_led_classdev_register_ext(&chip->client->dev, &led->cdev, &= init_data); + + if (ret) { + dev_err(&chip->client->dev, "Unable register multicolor:%s\n", cdev->na= me); + return ret; + } + } else { + ret =3D of_property_read_u32(np, "reg", ®); + if (ret || reg > MAX_CHANNEL) { + dev_err(&chip->client->dev, + "reg must less or equal than %d\n", MAX_CHANNEL); + return -EINVAL; + } + info =3D devm_kcalloc(&chip->client->dev, 1, sizeof(*info), GFP_KERNEL); + led->num_colors =3D 1; + info[i].color_index =3D LED_COLOR_ID_WHITE; + info[i].channel =3D reg - 1; + info[i].brightness =3D 0; + led->subled_info =3D info; + cdev =3D &led->cdev; + cdev->max_brightness =3D MAX_BRIGHTNESS; + cdev->brightness_set_blocking =3D led_brightness_set; + cdev->groups =3D led_sysfs_groups; + init_data.fwnode =3D &np->fwnode; + ret =3D devm_led_classdev_register_ext(&chip->client->dev, &led->cdev, &= init_data); + if (ret) { + dev_err(&chip->client->dev, "Unable register led:%s\n", cdev->name); + return ret; + } + } + return ret; +} + +static int mp3326_parse_dt(struct mp3326 *chip) +{ + struct device_node *np =3D dev_of_node(&chip->client->dev); + struct device_node *child; + int ret; + int i =3D 0; + int val; + + for_each_available_child_of_node(np, child) { + ret =3D mp3326_add_led(chip, child, i); + if (ret) + return ret; + i++; + } + + ret =3D regmap_write(chip->regmap, MP3326_PWM_CTRL_CHANNEL_9_16, 0); + if (ret) + return ret; + ret =3D regmap_write(chip->regmap, MP3326_PWM_CTRL_CHANNEL_1_8, 0); + if (ret) + return ret; + return 0; +} + +static int mp3326_leds_probe(struct i2c_client *client) +{ + struct mp3326 *chip; + const struct reg_field *reg_fields; + int count, i, j; + + count =3D device_get_child_node_count(&client->dev); + if (!count) { + return dev_err_probe(&client->dev, -EINVAL, + "Incorrect number of leds (%d)", count); + } + chip =3D devm_kzalloc(&client->dev, struct_size(chip, leds, count), GFP_K= ERNEL); + if (!chip) + return -ENOMEM; + + chip->client =3D client; + chip->num_of_leds =3D count; + i2c_set_clientdata(client, chip); + chip->regmap =3D devm_regmap_init_i2c(client, &MP3326_regmap_config); + if (IS_ERR(chip->regmap)) + return PTR_ERR(chip->regmap); + + for (i =3D 0; i < MAX_CHANNEL; i++) { + reg_fields =3D channels_reg_fields[i]; + for (j =3D 0; j < MAX_CTRL; j++) { + chip->regmap_fields[i][j] =3D devm_regmap_field_alloc(&client->dev, + chip->regmap, reg_fields[j]); + if (IS_ERR(chip->regmap_fields[i][j])) + return PTR_ERR(chip->regmap_fields[i][j]); + } + } + if (mp3326_parse_dt(chip)) + return 1; + else + return 0; +} + +static const struct i2c_device_id mp3326_id[] =3D { + {"mp3326", 0}, + {} +}; +MODULE_DEVICE_TABLE(i2c, mp3326_id); + +static const struct of_device_id mp3326_of_match[] =3D { + { .compatible =3D "mps,mp3326" }, + {} +}; +MODULE_DEVICE_TABLE(of, mp3326_of_match); + +static struct i2c_driver mp3326_driver =3D { + .probe_new =3D mp3326_leds_probe, + .driver =3D { + .name =3D "mp3326_led", + .of_match_table =3D mp3326_of_match, + }, + .id_table =3D mp3326_id, +}; + +module_i2c_driver(mp3326_driver); +MODULE_AUTHOR("Yuxi Wang "); +MODULE_DESCRIPTION("MPS MP3326 LED driver"); +MODULE_LICENSE("GPL"); --=20 2.25.1