From patchwork Wed Nov 15 19:42:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Murphy X-Patchwork-Id: 118995 Delivered-To: patch@linaro.org Received: by 10.80.225.132 with SMTP id k4csp3891766edl; Wed, 15 Nov 2017 11:43:50 -0800 (PST) X-Google-Smtp-Source: AGs4zMap8ROS1E23dDd5hVQzylEhc3ZENA3L6poiWLWxTBZ1vWM8GDMxkwezyWC2HA2bqymEZ1ri X-Received: by 10.101.93.141 with SMTP id f13mr16038221pgt.92.1510775030080; Wed, 15 Nov 2017 11:43:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510775030; cv=none; d=google.com; s=arc-20160816; b=jObyeDb9LAHzo774RBiBSFsnbFH/R6kSMena9STu26tAzMRC0aD/x2hXlRLLPCIAvs mSFnMuW7voh56WUGKlKiPDHnkqwigA4tYfOwgWfVfROTYS/Gcb08MdWTwVbUY4FvpwqB JCqbAps+QAntVyXbUxVZWyv5eAvvm1CsCLTNk65NkSr+yIfqkAe1w/0KBHCrC+hYkjQB 3if1r0/LJsei3vPAdQRffExYBfeQ2DlqtM+vU9o7h8bM0HpA2TBzoc6Ywu2c3yl2JjZ4 SzFumkSmxssoREFhPEq4XAeGFrZ2PHtxHsryqFlJtL91UIUWhcDzfJyFjQDrI5YRVFWu e+7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from:dkim-signature:arc-authentication-results; bh=P656LmW2F4jkAT5QK/5whODND0oC3KrAPA1/cR4D3nc=; b=BQFS79l6z/LHeY9zY3B+cOTcCWtz+HgVy8kym/CBK77BSBvczdPnRtZ0XkoEqdzM4a iqWPUaqJVBrvBOdLMGljzhZEb/QKXk4E9+01vkh03aUCyRBd383RybfRjqJ5I7STCIVF KZjRDgH8K+nsp6dZr9xfoeVkDwYIGsgkwR5H/wSUa1216OBeNUXreC9iTx+TY0//eae3 Q1CfXMZY9vnd7MNuVMSYDhH0HCcEDsIXqRtAZQRH/N12wNMitxsx2XJSiqgY3XNXJ0sk VdkSpjR5fKmBvOdQ17WPEn8eqeSPH22zqMJSxoKUQv4hW10rYm+6SulagSOZCt2MxUry +e0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@ti.com header.s=ti-com-17Q1 header.b=N74M7w0F; spf=pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g13si18699278plk.466.2017.11.15.11.43.49; Wed, 15 Nov 2017 11:43:50 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@ti.com header.s=ti-com-17Q1 header.b=N74M7w0F; spf=pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758313AbdKOTns (ORCPT + 6 others); Wed, 15 Nov 2017 14:43:48 -0500 Received: from fllnx209.ext.ti.com ([198.47.19.16]:52110 "EHLO fllnx209.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757097AbdKOTnr (ORCPT ); Wed, 15 Nov 2017 14:43:47 -0500 Received: from dlelxv90.itg.ti.com ([172.17.2.17]) by fllnx209.ext.ti.com (8.15.1/8.15.1) with ESMTP id vAFJgU4Q014440; Wed, 15 Nov 2017 13:42:30 -0600 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ti.com; s=ti-com-17Q1; t=1510774950; bh=iH+mzpcgDxMSf0s7KQCJ51QR0j72NmxKUdu+hoaO5OA=; h=From:To:CC:Subject:Date; b=N74M7w0Foy5YrRVC9O5W+I8WWsdbaCVERwvHfPwO8GXM4OJGcz2m80geByOC6ktxc eDeEbjTYgnl/un3ldShOClgn3RzpE/1KwpsVSlSdMQVIRCWoM9Wu0VfgfUx+NkpmBG r76k9DnCPg/0zJsatHOy1zMK7iPU5c/HZKrJxKks= Received: from DFLE112.ent.ti.com (dfle112.ent.ti.com [10.64.6.33]) by dlelxv90.itg.ti.com (8.14.3/8.13.8) with ESMTP id vAFJgPPL019407; Wed, 15 Nov 2017 13:42:25 -0600 Received: from DFLE107.ent.ti.com (10.64.6.28) by DFLE112.ent.ti.com (10.64.6.33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.1.845.34; Wed, 15 Nov 2017 13:42:25 -0600 Received: from dflp32.itg.ti.com (10.64.6.15) by DFLE107.ent.ti.com (10.64.6.28) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.1.845.34 via Frontend Transport; Wed, 15 Nov 2017 13:42:25 -0600 Received: from legion.dal.design.ti.com (legion.dal.design.ti.com [128.247.22.53]) by dflp32.itg.ti.com (8.14.3/8.13.8) with ESMTP id vAFJgPHi030908; Wed, 15 Nov 2017 13:42:25 -0600 Received: from localhost (a0272616local-lt.dhcp.ti.com [172.22.167.247]) by legion.dal.design.ti.com (8.11.7p1+Sun/8.11.7) with ESMTP id vAFJgO300252; Wed, 15 Nov 2017 13:42:24 -0600 (CST) From: Dan Murphy To: , , , , CC: , , , Dan Murphy Subject: [PATCH v3 1/2] dt: bindings: lm3692x: Add bindings for lm3692x LED driver Date: Wed, 15 Nov 2017 13:42:02 -0600 Message-ID: <20171115194203.13572-1-dmurphy@ti.com> X-Mailer: git-send-email 2.12.2 MIME-Version: 1.0 X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 Sender: devicetree-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org This adds the devicetree bindings for the LM3692x I2C LED string driver. Acked-by: Pavel Machek Signed-off-by: Dan Murphy --- v3 - No changes v2 - No changes - https://patchwork.kernel.org/patch/10056677/ .../devicetree/bindings/leds/leds-lm3692x.txt | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Documentation/devicetree/bindings/leds/leds-lm3692x.txt -- 2.15.0.124.g7668cbc60 -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/Documentation/devicetree/bindings/leds/leds-lm3692x.txt b/Documentation/devicetree/bindings/leds/leds-lm3692x.txt new file mode 100644 index 000000000000..cfef67bd4100 --- /dev/null +++ b/Documentation/devicetree/bindings/leds/leds-lm3692x.txt @@ -0,0 +1,28 @@ +* Texas Instruments - LM3692x Highly Efficient Dual-String White LED Driver + +The LM36922 is an ultra-compact, highly efficient, +two string white-LED driver designed for LCD display +backlighting. + +Required properties: + - compatible: + "ti,lm3692x" + - reg - I2C slave address + +Optional properties: + - label - Used for naming LEDs + - enable-gpio - gpio pin to enable/disable the device. + - supply - "vled" - LED supply + +Example: + +leds: leds@6 { + compatible = "ti,lm3692x"; + reg = <0x36>; + label = "backlight_cluster"; + enable-gpio = <&gpio1 28 GPIO_ACTIVE_HIGH>; + vled-supply = <&vbatt>; +} + +For more product information please see the link below: +http://www.ti.com/lit/ds/snvsa29/snvsa29.pdf From patchwork Wed Nov 15 19:42:03 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Murphy X-Patchwork-Id: 118996 Delivered-To: patch@linaro.org Received: by 10.80.225.132 with SMTP id k4csp3891909edl; Wed, 15 Nov 2017 11:43:58 -0800 (PST) X-Google-Smtp-Source: AGs4zMZFdeBxyuZm4VOL/0NvkYZSgwbQNBkGZs26BInUjGfF0ADsuM3MlITz9AIVf1YxJ4DD4t3o X-Received: by 10.159.247.203 with SMTP id v11mr17310540plz.33.1510775038256; Wed, 15 Nov 2017 11:43:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510775038; cv=none; d=google.com; s=arc-20160816; b=ohul5AWZyEiuc0UyIFPb1SlWTRShKzaGPz2ebiCrVp7Dy4doHgr56vVQ1B5Wp6Lsa7 4zgk2ATzXNa6kQI7C1hpCIW4P1p/X/PkC3nO/Fz5gLqeLFlUhg/7HzO40lyn4uwbp1cp tLEi6xmw5mEJlsGfxDFLGRV7fYfH7UmStdRdcSdXHSzRGeFEA5pCgK7IYQBul7TKlCLa WVdRqNvEH9njJQwWcRyIblYdpG+tH9wUA58eqlQnOznNVmuI0oiRrCYQdgzzM+iWnt2p O3XCVrrmXUl9r3BkoLBdA27Cnir1FpQ5oHKJlEWIY2YmA6PExbBUm4ZOq2B0vwykBokV iZFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=ueIk3jvdSdD7a7dydtv/jOO9b2v0QSeV0fS/H1jzmaI=; b=Trtntw9cQgMNaoiQblQ096knd+1z7JKDAM6T4UIx1p0YlFirlbiM6vqG+/5gm6dY9h jtIjgSguVw879x5gqboPg98qyEBZLP51BScHbdt/1Brq2ZsP5T5474BnIBY0w50lp8RN Q8ahUAhBpWvhjIcTZjFvW2V0h4VrtqOkRlF5FCAEOzZBYMfw8Do7/I7vH2XyWnej8FNH jD+6LV4kxxJmh/dBUHtU9Ewx4pl7xC7MQnXs26DGdnXEriembuLdiM6VzKKEve4CZnsu /42t1QVeN37jzi/bqhl+USImdaM+9XRxm5N7qyHwwMMExR+7SUG9BTSmZA0TRcF1anb6 KhNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@ti.com header.s=ti-com-17Q1 header.b=e9XxFM81; spf=pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g13si18699278plk.466.2017.11.15.11.43.58; Wed, 15 Nov 2017 11:43:58 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@ti.com header.s=ti-com-17Q1 header.b=e9XxFM81; spf=pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758826AbdKOTnv (ORCPT + 6 others); Wed, 15 Nov 2017 14:43:51 -0500 Received: from fllnx209.ext.ti.com ([198.47.19.16]:52109 "EHLO fllnx209.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753083AbdKOTnr (ORCPT ); Wed, 15 Nov 2017 14:43:47 -0500 Received: from dflxv15.itg.ti.com ([128.247.5.124]) by fllnx209.ext.ti.com (8.15.1/8.15.1) with ESMTP id vAFJgVPf014442; Wed, 15 Nov 2017 13:42:31 -0600 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ti.com; s=ti-com-17Q1; t=1510774951; bh=bSKm6dLrDPFRiwozHlcNp1swtp6Eo4kDpIHDtgAJPXU=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=e9XxFM81vCM6IGwwC86MZQj0aeKl51y1vjp9MNUpCdkcy8yJjnuTCk7LGvKLNgC8n K5veOsNtRDQOsynJSmc2oW9/KJy5IRLz4KeAqL5uCAXjlonWwXI8wFG2dkqMHKhHkf ipc+TapVi4amKw+/s2NRRdU9M1K8VH3J6Ast6IUM= Received: from DLEE113.ent.ti.com (dlee113.ent.ti.com [157.170.170.24]) by dflxv15.itg.ti.com (8.14.3/8.13.8) with ESMTP id vAFJgQH3010774; Wed, 15 Nov 2017 13:42:26 -0600 Received: from DLEE105.ent.ti.com (157.170.170.35) by DLEE113.ent.ti.com (157.170.170.24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.1.845.34; Wed, 15 Nov 2017 13:42:25 -0600 Received: from dlep33.itg.ti.com (157.170.170.75) by DLEE105.ent.ti.com (157.170.170.35) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.1.845.34 via Frontend Transport; Wed, 15 Nov 2017 13:42:25 -0600 Received: from legion.dal.design.ti.com (legion.dal.design.ti.com [128.247.22.53]) by dlep33.itg.ti.com (8.14.3/8.13.8) with ESMTP id vAFJgPkN030286; Wed, 15 Nov 2017 13:42:25 -0600 Received: from localhost (a0272616local-lt.dhcp.ti.com [172.22.167.247]) by legion.dal.design.ti.com (8.11.7p1+Sun/8.11.7) with ESMTP id vAFJgP300256; Wed, 15 Nov 2017 13:42:25 -0600 (CST) From: Dan Murphy To: , , , , CC: , , , Dan Murphy Subject: [PATCH v3 2/2] leds: lm3692x: Introduce LM3692x dual string driver Date: Wed, 15 Nov 2017 13:42:03 -0600 Message-ID: <20171115194203.13572-2-dmurphy@ti.com> X-Mailer: git-send-email 2.12.2 In-Reply-To: <20171115194203.13572-1-dmurphy@ti.com> References: <20171115194203.13572-1-dmurphy@ti.com> MIME-Version: 1.0 X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 Sender: devicetree-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org Introducing the LM3692x Dual-String white LED driver. Data sheet is located http://www.ti.com/lit/ds/snvsa29/snvsa29.pdf Signed-off-by: Dan Murphy --- v3 - Add missing Makefile and Kconfig from v1 and v2 - https://patchwork.kernel.org/patch/10060075/ v2 - Added data sheet link, fixed linuxdoc format, returned on failure in init routine, return on fault_check failure, updated brightness calculation and fixed capitalization issue - https://patchwork.kernel.org/patch/10056675/ drivers/leds/Kconfig | 7 + drivers/leds/Makefile | 1 + drivers/leds/leds-lm3692x.c | 390 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 398 insertions(+) create mode 100644 drivers/leds/leds-lm3692x.c -- 2.15.0.124.g7668cbc60 -- To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Acked-by: Pavel Machek diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig index 318a28fd58fe..1363e77a7d5f 100644 --- a/drivers/leds/Kconfig +++ b/drivers/leds/Kconfig @@ -137,6 +137,13 @@ config LEDS_LM3642 converter plus 1.5A constant current driver for a high-current white LED. +config LEDS_LM3692X + tristate "LED support for LM3692x Chips" + depends on LEDS_CLASS && I2C + select REGMAP_I2C + help + This option enables support for the TI LM3692x family + of LED drivers. config LEDS_LOCOMO tristate "LED Support for Locomo device" diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile index a2a6b5a4f86d..987884a5b9a5 100644 --- a/drivers/leds/Makefile +++ b/drivers/leds/Makefile @@ -74,6 +74,7 @@ obj-$(CONFIG_LEDS_PM8058) += leds-pm8058.o obj-$(CONFIG_LEDS_MLXCPLD) += leds-mlxcpld.o obj-$(CONFIG_LEDS_NIC78BX) += leds-nic78bx.o obj-$(CONFIG_LEDS_MT6323) += leds-mt6323.o +obj-$(CONFIG_LEDS_LM3692X) += leds-lm3692x.o # LED SPI Drivers obj-$(CONFIG_LEDS_DAC124S085) += leds-dac124s085.o diff --git a/drivers/leds/leds-lm3692x.c b/drivers/leds/leds-lm3692x.c new file mode 100644 index 000000000000..9fa7f5a20466 --- /dev/null +++ b/drivers/leds/leds-lm3692x.c @@ -0,0 +1,390 @@ +/* + * TI lm3692x LED Driver + * + * Copyright (C) 2017 Texas Instruments + * + * Author: Dan Murphy + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * Data sheet is located + * http://www.ti.com/lit/ds/snvsa29/snvsa29.pdf + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define LM3692X_LED_NAME "lm3692x_led" + +#define LM3692X_REV 0x0 +#define LM3692X_RESET 0x1 +#define LM3692X_EN 0x10 +#define LM3692X_BRT_CTRL 0x11 +#define LM3692X_PWM_CTRL 0x12 +#define LM3692X_BOOST_CTRL 0x13 +#define LM3692X_AUTO_FREQ_HI 0x15 +#define LM3692X_AUTO_FREQ_LO 0x16 +#define LM3692X_BL_ADJ_THRESH 0x17 +#define LM3692X_BRT_LSB 0x18 +#define LM3692X_BRT_MSB 0x19 +#define LM3692X_FAULT_CTRL 0x1e +#define LM3692X_FAULT_FLAGS 0x1f + +#define LM3692X_SW_RESET BIT(0) +#define LM3692X_DEVICE_EN BIT(0) +#define LM3692X_LED1_EN BIT(1) +#define LM3692X_LED2_EN BIT(2) + +/* Brightness Control Bits */ +#define LM3692X_BL_ADJ_POL BIT(0) +#define LM3692X_RAMP_RATE_125us 0x00 +#define LM3692X_RAMP_RATE_250us BIT(1) +#define LM3692X_RAMP_RATE_500us BIT(2) +#define LM3692X_RAMP_RATE_1ms (BIT(1) | BIT(2)) +#define LM3692X_RAMP_RATE_2ms BIT(3) +#define LM3692X_RAMP_RATE_4ms (BIT(3) | BIT(1)) +#define LM3692X_RAMP_RATE_8ms (BIT(2) | BIT(3)) +#define LM3692X_RAMP_RATE_16ms (BIT(1) | BIT(2) | BIT(3)) +#define LM3692X_RAMP_EN BIT(4) +#define LM3692X_BRHT_MODE_REG 0x00 +#define LM3692X_BRHT_MODE_PWM BIT(5) +#define LM3692X_BRHT_MODE_MULTI_RAMP BIT(6) +#define LM3692X_BRHT_MODE_RAMP_MULTI (BIT(5) | BIT(6)) +#define LM3692X_MAP_MODE_EXP BIT(7) + +/* PWM Register Bits */ +#define LM3692X_PWM_FILTER_100 BIT(0) +#define LM3692X_PWM_FILTER_150 BIT(1) +#define LM3692X_PWM_FILTER_200 (BIT(0) | BIT(1)) +#define LM3692X_PWM_HYSTER_1LSB BIT(2) +#define LM3692X_PWM_HYSTER_2LSB BIT(3) +#define LM3692X_PWM_HYSTER_3LSB (BIT(3) | BIT(2)) +#define LM3692X_PWM_HYSTER_4LSB BIT(4) +#define LM3692X_PWM_HYSTER_5LSB (BIT(4) | BIT(2)) +#define LM3692X_PWM_HYSTER_6LSB (BIT(4) | BIT(3)) +#define LM3692X_PWM_POLARITY BIT(5) +#define LM3692X_PWM_SAMP_4MHZ BIT(6) +#define LM3692X_PWM_SAMP_24MHZ BIT(7) + +/* Boost Control Bits */ +#define LM3692X_OCP_PROT_1A BIT(0) +#define LM3692X_OCP_PROT_1_25A BIT(1) +#define LM3692X_OCP_PROT_1_5A (BIT(0) | BIT(1)) +#define LM3692X_OVP_21V BIT(2) +#define LM3692X_OVP_25V BIT(3) +#define LM3692X_OVP_29V (BIT(2) | BIT(3)) +#define LM3692X_MIN_IND_22UH BIT(4) +#define LM3692X_BOOST_SW_1MHZ BIT(5) +#define LM3692X_BOOST_SW_NO_SHIFT BIT(6) + +/* Fault Control Bits */ +#define LM3692X_FAULT_CTRL_OVP BIT(0) +#define LM3692X_FAULT_CTRL_OCP BIT(1) +#define LM3692X_FAULT_CTRL_TSD BIT(2) +#define LM3692X_FAULT_CTRL_OPEN BIT(3) + +/* Fault Flag Bits */ +#define LM3692X_FAULT_FLAG_OVP BIT(0) +#define LM3692X_FAULT_FLAG_OCP BIT(1) +#define LM3692X_FAULT_FLAG_TSD BIT(2) +#define LM3692X_FAULT_FLAG_SHRT BIT(3) +#define LM3692X_FAULT_FLAG_OPEN BIT(4) + +/** + * struct lm3692x_led - + * @lock - Lock for reading/writing the device + * @client - Pointer to the I2C client + * @led_dev - led class device pointer + * @regmap - Devices register map + * @enable_gpio - VDDIO/EN gpio to enable communication interface + * @regulator - LED supply regulator pointer + * @label - LED label + */ +struct lm3692x_led { + struct mutex lock; + struct i2c_client *client; + struct led_classdev led_dev; + struct regmap *regmap; + struct gpio_desc *enable_gpio; + struct regulator *regulator; + const char *label; +}; + +static const struct reg_default lm3692x_reg_defs[] = { + {LM3692X_EN, 0xf}, + {LM3692X_BRT_CTRL, 0x61}, + {LM3692X_PWM_CTRL, 0x73}, + {LM3692X_BOOST_CTRL, 0x6f}, + {LM3692X_AUTO_FREQ_HI, 0x0}, + {LM3692X_AUTO_FREQ_LO, 0x0}, + {LM3692X_BL_ADJ_THRESH, 0x0}, + {LM3692X_BRT_LSB, 0x7}, + {LM3692X_BRT_MSB, 0xff}, + {LM3692X_FAULT_CTRL, 0x7}, +}; + +static const struct regmap_config lm3692x_regmap_config = { + .reg_bits = 8, + .val_bits = 8, + + .max_register = LM3692X_FAULT_FLAGS, + .reg_defaults = lm3692x_reg_defs, + .num_reg_defaults = ARRAY_SIZE(lm3692x_reg_defs), + .cache_type = REGCACHE_RBTREE, +}; + +static int lm3692x_fault_check(struct lm3692x_led *led) +{ + int ret, fault; + unsigned int read_buf; + + ret = regmap_read(led->regmap, LM3692X_FAULT_FLAGS, &read_buf); + if (ret) + return ret; + + fault = read_buf; + + if (fault) + dev_err(&led->client->dev, "Detected a fault 0x%X\n", + fault); + + return ret; +} + +static int lm3692x_brightness_set(struct led_classdev *led_cdev, + enum led_brightness brt_val) +{ + struct lm3692x_led *led = + container_of(led_cdev, struct lm3692x_led, led_dev); + int ret; + int led_brightness_lsb = (brt_val >> 5); + + mutex_lock(&led->lock); + + ret = lm3692x_fault_check(led); + if (ret) { + dev_err(&led->client->dev, "Cannot read/clear faults\n"); + goto out; + } + + ret = regmap_write(led->regmap, LM3692X_BRT_MSB, brt_val); + if (ret) { + dev_err(&led->client->dev, "Cannot write MSB\n"); + goto out; + } + + ret = regmap_write(led->regmap, LM3692X_BRT_LSB, led_brightness_lsb); + if (ret) { + dev_err(&led->client->dev, "Cannot write LSB\n"); + goto out; + } +out: + mutex_unlock(&led->lock); + return ret; +} + +static int lm3692x_init(struct lm3692x_led *led) +{ + int ret; + + if (led->regulator) { + ret = regulator_enable(led->regulator); + if (ret) + dev_err(&led->client->dev, + "Failed to enable regulator\n"); + goto out; + } + + if (led->enable_gpio) + gpiod_direction_output(led->enable_gpio, 1); + + ret = lm3692x_fault_check(led); + if (ret) { + dev_err(&led->client->dev, "Cannot read/clear faults\n"); + goto out; + } + + ret = regmap_write(led->regmap, LM3692X_BRT_CTRL, 0x00); + if (ret) { + dev_err(&led->client->dev, "Fail writing BRT CTRL\n"); + goto out; + } + + /* + * For glitch free operation, the following data should + * only be written while device enable bit is 0 + * per Section 7.5.14 of the data sheet + */ + ret = regmap_write(led->regmap, LM3692X_PWM_CTRL, + LM3692X_PWM_FILTER_100 | LM3692X_PWM_SAMP_24MHZ); + if (ret) { + dev_err(&led->client->dev, "Failed programming PWM CTRL\n"); + goto out; + } + + ret = regmap_write(led->regmap, LM3692X_BOOST_CTRL, + LM3692X_BRHT_MODE_RAMP_MULTI | + LM3692X_BL_ADJ_POL | + LM3692X_RAMP_RATE_250us); + if (ret) { + dev_err(&led->client->dev, "Failed programming BOOST CTRL\n"); + goto out; + } + + ret = regmap_write(led->regmap, LM3692X_AUTO_FREQ_HI, 0x00); + if (ret) { + dev_err(&led->client->dev, "Failed programming AUTO HI FREQ\n"); + goto out; + } + + ret = regmap_write(led->regmap, LM3692X_AUTO_FREQ_LO, 0x00); + if (ret) { + dev_err(&led->client->dev, "Failed programming AUTO LO FREQ\n"); + goto out; + } + + ret = regmap_write(led->regmap, LM3692X_BL_ADJ_THRESH, 0x00); + if (ret) { + dev_err(&led->client->dev, "Failed programming BL Threshold\n"); + goto out; + } + + ret = regmap_write(led->regmap, LM3692X_BRT_CTRL, + LM3692X_BL_ADJ_POL | LM3692X_PWM_HYSTER_4LSB); + if (ret) + dev_err(&led->client->dev, "Failed programming BRT CTRL\n"); + + return ret; +out: + + if (led->enable_gpio) + gpiod_direction_output(led->enable_gpio, 0); + + if (led->regulator) { + ret = regulator_disable(led->regulator); + if (ret) + dev_err(&led->client->dev, + "Failed to disable regulator\n"); + } + + return ret; +} + +static int lm3692x_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + int ret; + struct lm3692x_led *led; + struct device_node *np = client->dev.of_node; + + led = devm_kzalloc(&client->dev, sizeof(*led), GFP_KERNEL); + if (!led) + return -ENOMEM; + + if (client->dev.of_node) { + ret = of_property_read_string(np, "label", &led->label); + if (ret) + led->label = LM3692X_LED_NAME; + } + + led->enable_gpio = devm_gpiod_get_optional(&client->dev, + "enable", GPIOD_OUT_LOW); + if (IS_ERR(led->enable_gpio)) { + ret = PTR_ERR(led->enable_gpio); + dev_err(&client->dev, "Failed to get enable gpio: %d\n", ret); + return ret; + } + + led->regulator = devm_regulator_get(&client->dev, "vled"); + if (IS_ERR(led->regulator)) + led->regulator = NULL; + + led->client = client; + led->led_dev.name = led->label; + led->led_dev.max_brightness = LED_FULL; + led->led_dev.brightness_set_blocking = lm3692x_brightness_set; + + mutex_init(&led->lock); + + i2c_set_clientdata(client, led); + + led->regmap = devm_regmap_init_i2c(client, &lm3692x_regmap_config); + if (IS_ERR(led->regmap)) { + ret = PTR_ERR(led->regmap); + dev_err(&client->dev, "Failed to allocate register map: %d\n", + ret); + return ret; + } + + ret = lm3692x_init(led); + if (ret) + return ret; + + ret = led_classdev_register(&client->dev, &led->led_dev); + if (ret) { + dev_err(&client->dev, "led register err: %d\n", ret); + return ret; + } + + return 0; +} + +static int lm3692x_remove(struct i2c_client *client) +{ + struct lm3692x_led *led = i2c_get_clientdata(client); + int ret; + + led_classdev_unregister(&led->led_dev); + + if (led->enable_gpio) + gpiod_direction_output(led->enable_gpio, 0); + + if (led->regulator) { + ret = regulator_disable(led->regulator); + if (ret) + dev_err(&led->client->dev, + "Failed to disable regulator\n"); + } + + return 0; +} + +static const struct i2c_device_id lm3692x_id[] = { + { "lm3692x", 0 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, lm3692x_id); + +#ifdef CONFIG_OF +static const struct of_device_id of_lm3692x_leds_match[] = { + { .compatible = "ti,lm3692x", }, + {}, +}; +MODULE_DEVICE_TABLE(of, of_lm3692x_leds_match); +#endif + +static struct i2c_driver lm3692x_driver = { + .driver = { + .name = "lm3692x", + .of_match_table = of_match_ptr(of_lm3692x_leds_match), + }, + .probe = lm3692x_probe, + .remove = lm3692x_remove, + .id_table = lm3692x_id, +}; +module_i2c_driver(lm3692x_driver); + +MODULE_DESCRIPTION("Texas Instruments LM3692X LED driver"); +MODULE_AUTHOR("Dan Murphy "); +MODULE_LICENSE("GPL");