From patchwork Wed May 23 11:51:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Murphy X-Patchwork-Id: 136645 Delivered-To: patch@linaro.org Received: by 2002:a2e:9706:0:0:0:0:0 with SMTP id r6-v6csp789022lji; Wed, 23 May 2018 04:51:42 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqH073pBqOAB1OauRlx2L3D7xvwcc6nHRq4Nlaw67sDmChabbKsH4zTEVDn1nNiftEQF8pw X-Received: by 2002:a17:902:bf4a:: with SMTP id u10-v6mr2710726pls.322.1527076302519; Wed, 23 May 2018 04:51:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527076302; cv=none; d=google.com; s=arc-20160816; b=OPOOAsCADPjgB5mdx8Fod9HJSqfsj85gSnTfn2Y5HTlZRDr7DSsVNIA5221lVFI/CN b+a0G6+fyoWazElpdRwfYIA9nsU/APA3e5jlEpydksN39g7veR6Qxa2OWwUHH+8NfFfp KzzdCcS1XCRy9bJSYYgmsLshOZWG+KH/sCmq8QXcC6BFg9bIQ1wsW0CEPZl2RR7vDPYM 3AKaHnM08iGqwjDUYlmD4qaf7DZii6D648yA42sJOsEXx7CU/zscQ4dlgUwDtCzYPfhE 15awacBVlQsZxrL4cs/eCNC0+2Okkyge0kNCsUAqAnlXlsNoBhwJEvTmS3UpprUNG1kS 9OgA== 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=zeNoMZmE7eHyjKnzLbk0BYTZ3axkrNIsnZL46axiZwk=; b=I+CjVAsnj9b1uHYpvIzjHkQ7iYZ/VNv+qLYvDRbmX9xtxzpTwny1NQlqzEquW3WSJQ XWCopp7OfuZ76QqUum1T9syyZJglJlDuSJPmhoqu4ETgDvxyFliRSp7bJPdBVysdzTXK phfVcj6p6JhqjzKaUD8RS4AonGe/EckyQ6KGAL4wHlKUkMTpAe4EOSA0pGllkn3gqzZG SM4x1zmGxjB2FnkMrhNRPLxL9znGfUes7i22tcnzUQUJvS2HNWTF1vReRKxDOFMoNt9r h9kwdEilhfaNhC8uapu0ltJCNZdFKYfxbxFTZhZwEzk+7+LRzIzu5oKpZOfr1RgXeWur hvhg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=v4aq2RVP; spf=pass (google.com: best guess record for domain of linux-leds-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-leds-owner@vger.kernel.org; dmarc=pass (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 f71-v6si14583297pgc.641.2018.05.23.04.51.42; Wed, 23 May 2018 04:51:42 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-leds-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=v4aq2RVP; spf=pass (google.com: best guess record for domain of linux-leds-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-leds-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932709AbeEWLvl (ORCPT + 1 other); Wed, 23 May 2018 07:51:41 -0400 Received: from lelnx193.ext.ti.com ([198.47.27.77]:50406 "EHLO lelnx193.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932669AbeEWLvk (ORCPT ); Wed, 23 May 2018 07:51:40 -0400 Received: from dflxv15.itg.ti.com ([128.247.5.124]) by lelnx193.ext.ti.com (8.15.1/8.15.1) with ESMTP id w4NBpYLW012686; Wed, 23 May 2018 06:51:35 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1527076295; bh=zeNoMZmE7eHyjKnzLbk0BYTZ3axkrNIsnZL46axiZwk=; h=From:To:CC:Subject:Date; b=v4aq2RVPg6N89B8WBpzfqPkRqG6aOmACypxxTaUMOUOZuP5Wi9+fd753JwfrDdf0q ahqaQ7WJQp/LrxpidCoTe3f823W8Hh3pRhhJKSA2+SzjLUleUl5aYkass459J7KsBO jSzB6/EXKGM9Kch2ayrxKHc8RWvzsgSSZYB6dz+4= Received: from DFLE108.ent.ti.com (dfle108.ent.ti.com [10.64.6.29]) by dflxv15.itg.ti.com (8.14.3/8.13.8) with ESMTP id w4NBpYMK028032; Wed, 23 May 2018 06:51:34 -0500 Received: from DFLE105.ent.ti.com (10.64.6.26) by DFLE108.ent.ti.com (10.64.6.29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1466.3; Wed, 23 May 2018 06:51:34 -0500 Received: from dlep32.itg.ti.com (157.170.170.100) by DFLE105.ent.ti.com (10.64.6.26) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.1.1466.3 via Frontend Transport; Wed, 23 May 2018 06:51:34 -0500 Received: from legion.dal.design.ti.com (legion.dal.design.ti.com [128.247.22.53]) by dlep32.itg.ti.com (8.14.3/8.13.8) with ESMTP id w4NBpYcE021689; Wed, 23 May 2018 06:51:34 -0500 Received: from localhost (a0272616local-lt.dhcp.ti.com [172.22.151.71]) by legion.dal.design.ti.com (8.11.7p1+Sun/8.11.7) with ESMTP id w4NBpYx28444; Wed, 23 May 2018 06:51:34 -0500 (CDT) From: Dan Murphy To: , , , CC: , , , Dan Murphy Subject: [PATCH v9 1/2] dt: bindings: lm3601x: Introduce the lm3601x driver Date: Wed, 23 May 2018 06:51:28 -0500 Message-ID: <20180523115129.21674-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: linux-leds-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org Introduce the device tree bindings for the lm3601x family of LED torch, flash and IR drivers. Reviewed-by: Rob Herring Signed-off-by: Dan Murphy --- v9 - No changes - https://patchwork.kernel.org/patch/10418729/ v8 - No changes - https://patchwork.kernel.org/patch/10416161/ v7 - Removed led-sources in favor of reg, fixed malformatted ranges - https://patchwork.kernel.org/patch/10401435/ v6 - Removed multiple led child nodes, fixed example to display micro ranges for corresponding child nodes and added led-sources to define the current driver - https://patchwork.kernel.org/patch/10392121/ v5 - No changes - https://patchwork.kernel.org/patch/10391743/ v4 - Added " " around "=", changed strobe to flash on label, removed "support and register" comment and change ir lable to ir:torch - See v2 patchworks for comments v3 - Removed wildcard compatible - https://patchwork.kernel.org/patch/10386241/ v2 - No changes - https://patchwork.kernel.org/patch/10384587/ .../devicetree/bindings/leds/leds-lm3601x.txt | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Documentation/devicetree/bindings/leds/leds-lm3601x.txt -- 2.17.0.582.gccdcbd54c diff --git a/Documentation/devicetree/bindings/leds/leds-lm3601x.txt b/Documentation/devicetree/bindings/leds/leds-lm3601x.txt new file mode 100644 index 000000000000..a88b2c41e75b --- /dev/null +++ b/Documentation/devicetree/bindings/leds/leds-lm3601x.txt @@ -0,0 +1,45 @@ +* Texas Instruments - lm3601x Single-LED Flash Driver + +The LM3601X are ultra-small LED flash drivers that +provide a high level of adjustability. + +Required properties: + - compatible : Can be one of the following + "ti,lm36010" + "ti,lm36011" + - reg : I2C slave address + - #address-cells : 1 + - #size-cells : 0 + +Required child properties: + - reg : 0 - Indicates a IR mode + 1 - Indicates a Torch (white LED) mode + +Required properties for flash LED child nodes: + See Documentation/devicetree/bindings/leds/common.txt + - flash-max-microamp : Range from 11mA - 1.5A + - flash-max-timeout-us : Range from 40ms - 1600ms + - led-max-microamp : Range from 2.4mA - 376mA + +Optional child properties: + - label : see Documentation/devicetree/bindings/leds/common.txt + +Example: +led-controller@64 { + compatible = "ti,lm36010"; + #address-cells = <1>; + #size-cells = <0>; + reg = <0x64>; + + led@0 { + reg = <1>; + label = "white:torch"; + led-max-microamp = <376000>; + flash-max-microamp = <1500000>; + flash-max-timeout-us = <1600000>; + }; +} + +For more product information please see the links below: +http://www.ti.com/product/LM36010 +http://www.ti.com/product/LM36011 From patchwork Wed May 23 11:51:29 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Murphy X-Patchwork-Id: 136646 Delivered-To: patch@linaro.org Received: by 2002:a2e:9706:0:0:0:0:0 with SMTP id r6-v6csp789014lji; Wed, 23 May 2018 04:51:42 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpxOKdXhfl4lvKXZ+zzdloO6rJuVpLdbSYRwnHK9t0snp4v5wCW1QDalvir8fKF7lF1hKO2 X-Received: by 2002:a63:83c3:: with SMTP id h186-v6mr2033743pge.298.1527076302228; Wed, 23 May 2018 04:51:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527076302; cv=none; d=google.com; s=arc-20160816; b=DGUeNk3r/7CkBgsFBj9ezxjT3xK/ocy6Sp/U3jHkIecct0pEEAPG/ERf+1cwx5prvJ jTVSrnBi+v5rcOf4IIviVK5SIamyYFAzpYqBVce9sOUSrvDfmXvrCfjT8uvOZFQD3anL wRHfrc0Aywzr2polI+fzqS5HB36VvaR+yVrH6692kVEfGwYmpjM+r5FEo9SOQec/SjxP BQDjxS9Uc0xp8y5Iw0IBxYLJNMcYpPv3ALduF5gQ9KHOkGBOom3DnZsGQLYI/ZE+gMXM HvdVBLMxDinrTSGvdzjP0rX/WyjJu3VHn2x6SiKhhgmzc8B0pUG30qHTZ1V2MZGdpGrZ NQOA== 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=7Y6YtvQzlUMAkWQiprPvYJofQIeh099xXyHcoGJl5u8=; b=jNMalLlhifStUv9TmZ4R7e5thftjMeFlVirCSsMW34IJs7qd44Dxd6ROmQb/7p7YWH jOgkylN7VC9XUJyi2Fr88shr4KiaD9aikz5O05wkVbTXVDof8WvXCQ0KViMPv1CXNR1n 77x4G8V8lYJhF909YiN2T3cS1kEC+1uNtiIFS2uDMxih8FxymM/H4KrFJXKy79oKS9gc +nqDNiUdSju34dkIyCR958ZDvPGC0tvfRF0jSc+GMhBak+BGxPQce5e5XhIytumKuibs YjTJ657DAQvKkJGFRzt9AAaRXq5iXM3VrlO7UVyQTIRoWPgnD3JzufipZCFDcBCM7XuG HeBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=iIXU50dS; spf=pass (google.com: best guess record for domain of linux-leds-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-leds-owner@vger.kernel.org; dmarc=pass (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 f71-v6si14583297pgc.641.2018.05.23.04.51.42; Wed, 23 May 2018 04:51:42 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-leds-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=iIXU50dS; spf=pass (google.com: best guess record for domain of linux-leds-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-leds-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932696AbeEWLvl (ORCPT + 1 other); Wed, 23 May 2018 07:51:41 -0400 Received: from lelnx194.ext.ti.com ([198.47.27.80]:56504 "EHLO lelnx194.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932640AbeEWLvk (ORCPT ); Wed, 23 May 2018 07:51:40 -0400 Received: from dflxv15.itg.ti.com ([128.247.5.124]) by lelnx194.ext.ti.com (8.15.1/8.15.1) with ESMTP id w4NBpZc0009357; Wed, 23 May 2018 06:51:35 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1527076295; bh=7Y6YtvQzlUMAkWQiprPvYJofQIeh099xXyHcoGJl5u8=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=iIXU50dSNbZ+Rd83QerKBUsUqdWX7qKHg0Jk/Qhfr6uxt7/tWPdEa4+aIrPMChn6N iZHQ0sU+laHYWJ51+eoSaYYIC5B83Pz8feuWhNQkcU3OTRcKdaqrjy4DCteuK1ZByz l7Q/Alx5/vlsRoQLn6UXMVVjpDd0Wv5ZtxsJh5rM= Received: from DFLE114.ent.ti.com (dfle114.ent.ti.com [10.64.6.35]) by dflxv15.itg.ti.com (8.14.3/8.13.8) with ESMTP id w4NBpZob028046; Wed, 23 May 2018 06:51:35 -0500 Received: from DFLE101.ent.ti.com (10.64.6.22) by DFLE114.ent.ti.com (10.64.6.35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1466.3; Wed, 23 May 2018 06:51:35 -0500 Received: from dflp32.itg.ti.com (10.64.6.15) by DFLE101.ent.ti.com (10.64.6.22) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.1.1466.3 via Frontend Transport; Wed, 23 May 2018 06:51:35 -0500 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 w4NBpYY0028917; Wed, 23 May 2018 06:51:35 -0500 Received: from localhost (a0272616local-lt.dhcp.ti.com [172.22.151.71]) by legion.dal.design.ti.com (8.11.7p1+Sun/8.11.7) with ESMTP id w4NBpYx28448; Wed, 23 May 2018 06:51:34 -0500 (CDT) From: Dan Murphy To: , , , CC: , , , Dan Murphy Subject: [PATCH v9 2/2] leds: lm3601x: Introduce the lm3601x LED driver Date: Wed, 23 May 2018 06:51:29 -0500 Message-ID: <20180523115129.21674-2-dmurphy@ti.com> X-Mailer: git-send-email 2.12.2 In-Reply-To: <20180523115129.21674-1-dmurphy@ti.com> References: <20180523115129.21674-1-dmurphy@ti.com> MIME-Version: 1.0 X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 Sender: linux-leds-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org Introduce the family of LED devices that can drive a torch, strobe or IR LED. The LED driver can be configured with a strobe timer to execute a strobe flash. The IR LED brightness is controlled via the torch brightness register. The data sheet for each the LM36010 and LM36011 LED drivers can be found here: http://www.ti.com/product/LM36010 http://www.ti.com/product/LM36011 Reviewed-by: Andy Shevchenko Signed-off-by: Dan Murphy --- v9 - Changed "strobe" to flash in some cases, removed some redundant variable assignments, updated a couple of line to make them more readable, converted to probe_new from old probe, and change err to ret - https://patchwork.kernel.org/patch/10418731/ v8 - Removed OF Kconfig dependency, change strobe to flash, updated label generation, fixed brightness calculation, added mutex_destroy and flash_unregister - https://patchwork.kernel.org/patch/10416163/ v7 - Numerous fixes to many to list. Highlights are moved from OF APIs to device_property APIs, updated LED registration, timeout to reg algorithim, and fixed label generation - https://patchwork.kernel.org/patch/10401437/ v6 - This driver has been heavily modified from v5. There is no longer reading of individual child nodes. There are too many changes to list here see - https://patchwork.kernel.org/patch/10392123/ v5 - Fixed magic numbers, change reg cache type, added of_put_node to release the dt node ref, and I did not change the remove function to leave the LED in its state on driver removal - https://patchwork.kernel.org/patch/10391741/ v4 - Fixed Cocci issue using ARRAY_SIZE - https://patchwork.kernel.org/patch/10389259/ v3 - removed wildcard dt compatible, fixed copyright, fixed struct doc, removed RO registers from default, added regmap volatile for FLAGS_REG, updated regmap cache type, fixed unlock and extra semi colon in strobe_set, removed unnecessary out label in led register and fixed checking of the ret in brightness_set - https://patchwork.kernel.org/patch/10386243/ v2 - Fixed kbuild issue and removed unused cdev_strobe - https://patchwork.kernel.org/patch/10384585/ drivers/leds/Kconfig | 9 + drivers/leds/Makefile | 1 + drivers/leds/leds-lm3601x.c | 487 ++++++++++++++++++++++++++++++++++++ 3 files changed, 497 insertions(+) create mode 100644 drivers/leds/leds-lm3601x.c -- 2.17.0.582.gccdcbd54c diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig index 2c896c0e69e1..d95d436e6089 100644 --- a/drivers/leds/Kconfig +++ b/drivers/leds/Kconfig @@ -145,6 +145,15 @@ config LEDS_LM3692X This option enables support for the TI LM3692x family of white LED string drivers used for backlighting. +config LEDS_LM3601X + tristate "LED support for LM3601x Chips" + depends on LEDS_CLASS && I2C + depends on LEDS_CLASS_FLASH + select REGMAP_I2C + help + This option enables support for the TI LM3601x family + of flash, torch and indicator classes. + config LEDS_LOCOMO tristate "LED Support for Locomo device" depends on LEDS_CLASS diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile index 91eca81cae82..b79807fe1b67 100644 --- a/drivers/leds/Makefile +++ b/drivers/leds/Makefile @@ -76,6 +76,7 @@ obj-$(CONFIG_LEDS_MLXREG) += leds-mlxreg.o obj-$(CONFIG_LEDS_NIC78BX) += leds-nic78bx.o obj-$(CONFIG_LEDS_MT6323) += leds-mt6323.o obj-$(CONFIG_LEDS_LM3692X) += leds-lm3692x.o +obj-$(CONFIG_LEDS_LM3601X) += leds-lm3601x.o # LED SPI Drivers obj-$(CONFIG_LEDS_DAC124S085) += leds-dac124s085.o diff --git a/drivers/leds/leds-lm3601x.c b/drivers/leds/leds-lm3601x.c new file mode 100644 index 000000000000..081aa71e43a3 --- /dev/null +++ b/drivers/leds/leds-lm3601x.c @@ -0,0 +1,487 @@ +// SPDX-License-Identifier: GPL-2.0 +// Flash and torch driver for Texas Instruments LM3601X LED +// Flash driver chip family +// Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define LM3601X_LED_IR 0x0 +#define LM3601X_LED_TORCH 0x1 + +/* Registers */ +#define LM3601X_ENABLE_REG 0x01 +#define LM3601X_CFG_REG 0x02 +#define LM3601X_LED_FLASH_REG 0x03 +#define LM3601X_LED_TORCH_REG 0x04 +#define LM3601X_FLAGS_REG 0x05 +#define LM3601X_DEV_ID_REG 0x06 + +#define LM3601X_SW_RESET BIT(7) + +/* Enable Mode bits */ +#define LM3601X_MODE_STANDBY 0x00 +#define LM3601X_MODE_IR_DRV BIT(0) +#define LM3601X_MODE_TORCH BIT(1) +#define LM3601X_MODE_STROBE (BIT(0) | BIT(1)) +#define LM3601X_STRB_EN BIT(2) +#define LM3601X_STRB_EDGE_TRIG BIT(3) +#define LM3601X_IVFM_EN BIT(4) + +#define LM36010_BOOST_LIMIT_28 BIT(5) +#define LM36010_BOOST_FREQ_4MHZ BIT(6) +#define LM36010_BOOST_MODE_PASS BIT(7) + +/* Flag Mask */ +#define LM3601X_FLASH_TIME_OUT BIT(0) +#define LM3601X_UVLO_FAULT BIT(1) +#define LM3601X_THERM_SHUTDOWN BIT(2) +#define LM3601X_THERM_CURR BIT(3) +#define LM36010_CURR_LIMIT BIT(4) +#define LM3601X_SHORT_FAULT BIT(5) +#define LM3601X_IVFM_TRIP BIT(6) +#define LM36010_OVP_FAULT BIT(7) + +#define LM3601X_MAX_TORCH_I_UA 376000 +#define LM3601X_MIN_TORCH_I_UA 2400 +#define LM3601X_TORCH_REG_DIV 2965 + +#define LM3601X_MAX_STROBE_I_UA 1500000 +#define LM3601X_MIN_STROBE_I_UA 11000 +#define LM3601X_STROBE_REG_DIV 11800 + +#define LM3601X_TIMEOUT_MASK 0x1e +#define LM3601X_ENABLE_MASK (LM3601X_MODE_IR_DRV | LM3601X_MODE_TORCH) + +#define LM3601X_LOWER_STEP_US 40000 +#define LM3601X_UPPER_STEP_US 200000 +#define LM3601X_MIN_TIMEOUT_US 40000 +#define LM3601X_MAX_TIMEOUT_US 1600000 +#define LM3601X_TIMEOUT_XOVER_US 400000 + +enum lm3601x_type { + CHIP_LM36010 = 0, + CHIP_LM36011, +}; + +/** + * struct lm3601x_led - + * @fled_cdev: flash LED class device pointer + * @client: Pointer to the I2C client + * @regmap: Devices register map + * @lock: Lock for reading/writing the device + * @led_name: LED label for the Torch or IR LED + * @flash_timeout: the timeout for the flash + * @last_flag: last known flags register value + * @torch_current_max: maximum current for the torch + * @flash_current_max: maximum current for the flash + * @max_flash_timeout: maximum timeout for the flash + * @led_mode: The mode to enable either IR or Torch + */ +struct lm3601x_led { + struct led_classdev_flash fled_cdev; + struct i2c_client *client; + struct regmap *regmap; + struct mutex lock; + + char led_name[LED_MAX_NAME_SIZE]; + + unsigned int flash_timeout; + unsigned int last_flag; + + u32 torch_current_max; + u32 flash_current_max; + u32 max_flash_timeout; + + u32 led_mode; +}; + +static const struct reg_default lm3601x_regmap_defs[] = { + { LM3601X_ENABLE_REG, 0x20 }, + { LM3601X_CFG_REG, 0x15 }, + { LM3601X_LED_FLASH_REG, 0x00 }, + { LM3601X_LED_TORCH_REG, 0x00 }, +}; + +static bool lm3601x_volatile_reg(struct device *dev, unsigned int reg) +{ + switch (reg) { + case LM3601X_FLAGS_REG: + return true; + default: + return false; + } +} + +static const struct regmap_config lm3601x_regmap = { + .reg_bits = 8, + .val_bits = 8, + + .max_register = LM3601X_DEV_ID_REG, + .reg_defaults = lm3601x_regmap_defs, + .num_reg_defaults = ARRAY_SIZE(lm3601x_regmap_defs), + .cache_type = REGCACHE_RBTREE, + .volatile_reg = lm3601x_volatile_reg, +}; + +static struct lm3601x_led *fled_cdev_to_led(struct led_classdev_flash *fled_cdev) +{ + return container_of(fled_cdev, struct lm3601x_led, fled_cdev); +} + +static int lm3601x_read_faults(struct lm3601x_led *led) +{ + int flags_val; + int ret; + + ret = regmap_read(led->regmap, LM3601X_FLAGS_REG, &flags_val); + if (ret < 0) + return -EIO; + + led->last_flag = 0; + + if (flags_val & LM36010_OVP_FAULT) + led->last_flag |= LED_FAULT_OVER_VOLTAGE; + + if (flags_val & (LM3601X_THERM_SHUTDOWN | LM3601X_THERM_CURR)) + led->last_flag |= LED_FAULT_OVER_TEMPERATURE; + + if (flags_val & LM3601X_SHORT_FAULT) + led->last_flag |= LED_FAULT_SHORT_CIRCUIT; + + if (flags_val & LM36010_CURR_LIMIT) + led->last_flag |= LED_FAULT_OVER_CURRENT; + + if (flags_val & LM3601X_UVLO_FAULT) + led->last_flag |= LED_FAULT_UNDER_VOLTAGE; + + if (flags_val & LM3601X_IVFM_TRIP) + led->last_flag |= LED_FAULT_INPUT_VOLTAGE; + + if (flags_val & LM3601X_THERM_SHUTDOWN) + led->last_flag |= LED_FAULT_LED_OVER_TEMPERATURE; + + return led->last_flag; +} + +static int lm3601x_brightness_set(struct led_classdev *cdev, + enum led_brightness brightness) +{ + struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(cdev); + struct lm3601x_led *led = fled_cdev_to_led(fled_cdev); + int ret, led_mode_val; + + mutex_lock(&led->lock); + + ret = lm3601x_read_faults(led); + if (ret < 0) + goto out; + + if (led->led_mode == LM3601X_LED_TORCH) + led_mode_val = LM3601X_MODE_TORCH; + else + led_mode_val = LM3601X_MODE_IR_DRV; + + if (brightness == LED_OFF) { + ret = regmap_update_bits(led->regmap, LM3601X_ENABLE_REG, + led_mode_val, LED_OFF); + goto out; + } + + ret = regmap_write(led->regmap, LM3601X_LED_TORCH_REG, brightness); + if (ret < 0) + goto out; + + ret = regmap_update_bits(led->regmap, LM3601X_ENABLE_REG, + LM3601X_MODE_TORCH | LM3601X_MODE_IR_DRV, + led_mode_val); +out: + mutex_unlock(&led->lock); + return ret; +} + +static int lm3601x_strobe_set(struct led_classdev_flash *fled_cdev, + bool state) +{ + struct lm3601x_led *led = fled_cdev_to_led(fled_cdev); + int timeout_reg_val; + int current_timeout; + int ret; + + mutex_lock(&led->lock); + + ret = regmap_read(led->regmap, LM3601X_CFG_REG, ¤t_timeout); + if (ret < 0) + goto out; + + if (led->flash_timeout >= LM3601X_TIMEOUT_XOVER_US) + timeout_reg_val = led->flash_timeout / LM3601X_UPPER_STEP_US + 0x07; + else + timeout_reg_val = led->flash_timeout / LM3601X_LOWER_STEP_US - 0x01; + + if (led->flash_timeout != current_timeout) + ret = regmap_update_bits(led->regmap, LM3601X_CFG_REG, + LM3601X_TIMEOUT_MASK, timeout_reg_val); + + if (state) + ret = regmap_update_bits(led->regmap, LM3601X_ENABLE_REG, + LM3601X_MODE_TORCH | LM3601X_MODE_IR_DRV, + LM3601X_MODE_STROBE); + else + ret = regmap_update_bits(led->regmap, LM3601X_ENABLE_REG, + LM3601X_MODE_STROBE, LED_OFF); + + ret = lm3601x_read_faults(led); +out: + mutex_unlock(&led->lock); + return ret; +} + +static int lm3601x_flash_brightness_set(struct led_classdev_flash *fled_cdev, + u32 brightness) +{ + struct lm3601x_led *led = fled_cdev_to_led(fled_cdev); + u8 brightness_val; + int ret; + + mutex_lock(&led->lock); + ret = lm3601x_read_faults(led); + if (ret < 0) + goto out; + + if (brightness == LED_OFF) { + ret = regmap_update_bits(led->regmap, LM3601X_ENABLE_REG, + LM3601X_MODE_STROBE, LED_OFF); + goto out; + } + + brightness_val = brightness / LM3601X_STROBE_REG_DIV; + + ret = regmap_write(led->regmap, LM3601X_LED_FLASH_REG, brightness_val); +out: + mutex_unlock(&led->lock); + return ret; +} + +static int lm3601x_flash_timeout_set(struct led_classdev_flash *fled_cdev, + u32 timeout) +{ + struct lm3601x_led *led = fled_cdev_to_led(fled_cdev); + + mutex_lock(&led->lock); + + led->flash_timeout = timeout; + + mutex_unlock(&led->lock); + + return 0; +} + +static int lm3601x_strobe_get(struct led_classdev_flash *fled_cdev, bool *state) +{ + struct lm3601x_led *led = fled_cdev_to_led(fled_cdev); + int strobe_state; + int ret; + + mutex_lock(&led->lock); + + ret = regmap_read(led->regmap, LM3601X_ENABLE_REG, &strobe_state); + if (ret < 0) + goto out; + + *state = strobe_state & LM3601X_MODE_STROBE; + +out: + mutex_unlock(&led->lock); + return ret; +} + +static int lm3601x_flash_fault_get(struct led_classdev_flash *fled_cdev, + u32 *fault) +{ + struct lm3601x_led *led = fled_cdev_to_led(fled_cdev); + + lm3601x_read_faults(led); + + *fault = led->last_flag; + + return 0; +} + +static const struct led_flash_ops flash_ops = { + .flash_brightness_set = lm3601x_flash_brightness_set, + .strobe_set = lm3601x_strobe_set, + .strobe_get = lm3601x_strobe_get, + .timeout_set = lm3601x_flash_timeout_set, + .fault_get = lm3601x_flash_fault_get, +}; + +static int lm3601x_register_leds(struct lm3601x_led *led) +{ + struct led_classdev *led_cdev; + struct led_flash_setting *setting; + + led->fled_cdev.ops = &flash_ops; + + setting = &led->fled_cdev.timeout; + setting->min = LM3601X_MIN_TIMEOUT_US; + setting->max = led->max_flash_timeout; + setting->step = LM3601X_LOWER_STEP_US; + setting->val = led->max_flash_timeout; + + setting = &led->fled_cdev.brightness; + setting->min = LM3601X_MIN_STROBE_I_UA; + setting->max = led->flash_current_max; + setting->step = LM3601X_TORCH_REG_DIV; + setting->val = led->flash_current_max; + + led_cdev = &led->fled_cdev.led_cdev; + led_cdev->name = led->led_name; + led_cdev->brightness_set_blocking = lm3601x_brightness_set; + led_cdev->max_brightness = DIV_ROUND_UP(led->torch_current_max, + LM3601X_TORCH_REG_DIV); + led_cdev->flags |= LED_DEV_CAP_FLASH; + + return led_classdev_flash_register(&led->client->dev, &led->fled_cdev); +} + +static int lm3601x_parse_node(struct lm3601x_led *led) +{ + struct fwnode_handle *child = NULL; + int ret = -ENODEV; + const char *name; + + child = device_get_next_child_node(&led->client->dev, child); + if (!child) { + dev_err(&led->client->dev, "No LED Child node\n"); + return ret; + } + + ret = fwnode_property_read_u32(child, "reg", &led->led_mode); + if (ret) { + dev_err(&led->client->dev, "reg DT property missing\n"); + goto out_err; + } + + if (led->led_mode > LM3601X_LED_TORCH || + led->led_mode < LM3601X_LED_IR) { + dev_warn(&led->client->dev, "Invalid led mode requested\n"); + ret = -EINVAL; + goto out_err; + } + + ret = fwnode_property_read_string(child, "label", &name); + if (ret) { + if (led->led_mode == LM3601X_LED_TORCH) + name = "torch"; + else + name = "infrared"; + } + + snprintf(led->led_name, sizeof(led->led_name), + "%s:%s", led->client->name, name); + + ret = fwnode_property_read_u32(child, "led-max-microamp", + &led->torch_current_max); + if (ret) { + dev_warn(&led->client->dev, + "led-max-microamp DT property missing\n"); + goto out_err; + } + + ret = fwnode_property_read_u32(child, "flash-max-microamp", + &led->flash_current_max); + if (ret) { + dev_warn(&led->client->dev, + "flash-max-microamp DT property missing\n"); + goto out_err; + } + + ret = fwnode_property_read_u32(child, "flash-max-timeout-us", + &led->max_flash_timeout); + if (ret) { + dev_warn(&led->client->dev, + "flash-max-timeout-us DT property missing\n"); + goto out_err; + } + +out_err: + fwnode_handle_put(child); + return ret; +} + +static int lm3601x_probe(struct i2c_client *client) +{ + struct lm3601x_led *led; + int ret; + + led = devm_kzalloc(&client->dev, sizeof(*led), GFP_KERNEL); + if (!led) + return -ENOMEM; + + led->client = client; + i2c_set_clientdata(client, led); + + ret = lm3601x_parse_node(led); + if (ret) + return -ENODEV; + + led->regmap = devm_regmap_init_i2c(client, &lm3601x_regmap); + if (IS_ERR(led->regmap)) { + ret = PTR_ERR(led->regmap); + dev_err(&client->dev, + "Failed to allocate register map: %d\n", ret); + return ret; + } + + mutex_init(&led->lock); + + return lm3601x_register_leds(led); +} + +static int lm3601x_remove(struct i2c_client *client) +{ + struct lm3601x_led *led = i2c_get_clientdata(client); + + led_classdev_flash_unregister(&led->fled_cdev); + mutex_destroy(&led->lock); + + return regmap_update_bits(led->regmap, LM3601X_ENABLE_REG, + LM3601X_ENABLE_MASK, + LM3601X_MODE_STANDBY); +} + +static const struct i2c_device_id lm3601x_id[] = { + { "LM36010", CHIP_LM36010 }, + { "LM36011", CHIP_LM36011 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, lm3601x_id); + +static const struct of_device_id of_lm3601x_leds_match[] = { + { .compatible = "ti,lm36010", }, + { .compatible = "ti,lm36011", }, + { } +}; +MODULE_DEVICE_TABLE(of, of_lm3601x_leds_match); + +static struct i2c_driver lm3601x_i2c_driver = { + .driver = { + .name = "lm3601x", + .of_match_table = of_lm3601x_leds_match, + }, + .probe_new = lm3601x_probe, + .remove = lm3601x_remove, + .id_table = lm3601x_id, +}; +module_i2c_driver(lm3601x_i2c_driver); + +MODULE_DESCRIPTION("Texas Instruments Flash Lighting driver for LM3601X"); +MODULE_AUTHOR("Dan Murphy "); +MODULE_LICENSE("GPL v2");