From patchwork Thu May 10 17:47:17 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Murphy X-Patchwork-Id: 135422 Delivered-To: patch@linaro.org Received: by 10.46.151.6 with SMTP id r6csp1303348lji; Thu, 10 May 2018 10:47:39 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpu1hrMEMht8cMo4VNGo+bIGCjOj6StoOY2mLlZI0JF2EN+5rySRZDNUFG2UBns+16Xb1Id X-Received: by 2002:a17:902:7209:: with SMTP id ba9-v6mr2270901plb.119.1525974459458; Thu, 10 May 2018 10:47:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525974459; cv=none; d=google.com; s=arc-20160816; b=pMwVU4Ou+9udUWrvFh1DGtzf7Lmnw4k93sx/H55KX0gUqtXuZD96QVWPct2l7SQMWm iRnwxfT6sT6XsYcEeOb3DW9n3DrKu3WFBEUKMuXfaY7rPs+Znqk/N2jzmp+JBBzulZMJ 9+k4l2Vqi1NWy8avxK4uI4iCk3PXBYAXMhxztM9hb+Duf/alRO+AVyl1Y+bpkfkeKR81 Egn3MeEDvGk9yK7CFLcIWRmvfVpfxrZJACmgewse35nFw4h5NT2c8BtEfReLuK8ydOn8 pSTy3VCLHoHm2ay2zmWBLpThiBUBnELUWLC0mBlaWGjn9e9a9UE/9wEVlhxMzIHIrS+e 1s5Q== 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=NT4+snSPo8GTZORMFRHdKlYhZgV4kMOXohbWkjiZi3M=; b=FRWK47h9JNqvs+msIThp6s/dej10fMuS5zv6seP8QroLVZgmLy/0ZbvByS3Bajx98U 7Hsv+V3wcYpR5ZEKjz8ecD8jarVXkjw2XP/L7hwz/biB4aDZis+tWwkE3Ym8nrMzt0Il xgBA5crQTGB4WCo+Z10sIUX0XvXm2QZ7O1EzSH7bnmBFyWZ36rq12mtFPBSXHdlQiQaw o2zmwe9s95mHL/DxGCwVaZe8twVF8IY6ItJSf3FZuLD4mmmCFL9WRYRulvXXN/A1nNL6 rPVA5t1966yNgThHVcwJrLyT3F8tltEGU2qbwVX9ioeqczxIyMOcj0yQNuOBSOdWyayv xAiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=aVliFit2; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-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 p10-v6si1203355plk.295.2018.05.10.10.47.38; Thu, 10 May 2018 10:47:39 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-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=aVliFit2; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-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 S966843AbeEJRrg (ORCPT + 29 others); Thu, 10 May 2018 13:47:36 -0400 Received: from fllnx209.ext.ti.com ([198.47.19.16]:27496 "EHLO fllnx209.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S966615AbeEJRrb (ORCPT ); Thu, 10 May 2018 13:47:31 -0400 Received: from dlelxv90.itg.ti.com ([172.17.2.17]) by fllnx209.ext.ti.com (8.15.1/8.15.1) with ESMTP id w4AHlNm8026914; Thu, 10 May 2018 12:47:23 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1525974443; bh=NT4+snSPo8GTZORMFRHdKlYhZgV4kMOXohbWkjiZi3M=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=aVliFit2cH1FJVwhwDQp5fT/UbA35SNYHTxLgyjxkB8Dg5fZPHj62zXY9tuNzHQ5T NphqpmnaI2xVxvTaS9CGqvmfPBQp8l+MGr9oKJ8AAqt3cIHkFA+b0xN4wHJuoo+iSA JRoNJCt56uJIAO90UZOhESGSGFkA5dZg/AX2rrnw= Received: from DFLE108.ent.ti.com (dfle108.ent.ti.com [10.64.6.29]) by dlelxv90.itg.ti.com (8.14.3/8.13.8) with ESMTP id w4AHlMEQ009217; Thu, 10 May 2018 12:47:22 -0500 Received: from DFLE113.ent.ti.com (10.64.6.34) 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; Thu, 10 May 2018 12:47:22 -0500 Received: from dlep32.itg.ti.com (157.170.170.100) by DFLE113.ent.ti.com (10.64.6.34) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.1.1466.3 via Frontend Transport; Thu, 10 May 2018 12:47:22 -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 w4AHlMMp020631; Thu, 10 May 2018 12:47:22 -0500 Received: from localhost (a0272616local-lt.dhcp.ti.com [172.22.166.14]) by legion.dal.design.ti.com (8.11.7p1+Sun/8.11.7) with ESMTP id w4AHlMx24825; Thu, 10 May 2018 12:47:22 -0500 (CDT) From: Dan Murphy To: , , , , CC: , , , Dan Murphy Subject: [PATCH v5 2/2] leds: lm3601x: Introduce the lm3601x LED driver Date: Thu, 10 May 2018 12:47:17 -0500 Message-ID: <20180510174717.26540-2-dmurphy@ti.com> X-Mailer: git-send-email 2.12.2 In-Reply-To: <20180510174717.26540-1-dmurphy@ti.com> References: <20180510174717.26540-1-dmurphy@ti.com> MIME-Version: 1.0 X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 Signed-off-by: Dan Murphy --- 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 | 622 ++++++++++++++++++++++++++++++++++++ 3 files changed, 632 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..50ae536f343f 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 && OF + 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..3c00886fbc7a --- /dev/null +++ b/drivers/leds/leds-lm3601x.c @@ -0,0 +1,622 @@ +// 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_TORCH 0x0 +#define LM3601X_LED_STROBE 0x1 +#define LM3601X_LED_IR 0x2 + +/* 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_LVL_TRIG ~BIT(3) +#define LM3601X_STRB_EDGE_TRIG BIT(3) +#define LM3601X_IVFM_EN BIT(4) + +#define LM36010_BOOST_LIMIT_19 ~BIT(5) +#define LM36010_BOOST_LIMIT_28 BIT(5) +#define LM36010_BOOST_FREQ_2MHZ ~BIT(6) +#define LM36010_BOOST_FREQ_4MHZ BIT(6) +#define LM36010_BOOST_MODE_NORM ~BIT(7) +#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_MIN_TORCH_I_UA 2400 +#define LM3601X_MIN_STROBE_I_MA 11 + +#define LM3601X_TIMEOUT_MASK 0x1e +#define LM3601X_ENABLE_MASK 0x03 + +enum lm3601x_type { + CHIP_LM36010 = 0, + CHIP_LM36011, +}; + +/** + * struct lm3601x_max_timeouts - + * @timeout: timeout value in ms + * @regval: the value of the register to write + */ +struct lm3601x_max_timeouts { + int timeout; + int reg_val; +}; + +/** + * struct lm3601x_led - + * @lock: Lock for reading/writing the device + * @regmap: Devices register map + * @client: Pointer to the I2C client + * @cdev_torch: led class device pointer for the torch + * @cdev_ir: led class device pointer for infrared + * @fled_cdev: flash led class device pointer + * @strobe_node: DT device node for the strobe + * @torch: LED label for the torch + * @strobe: LED label for the strobe + * @ir: LED label for the infrared + * @last_flag: last known flags register value + * @strobe_timeout: the timeout for the strobe + * @torch_current_max: maximum current for the torch + * @strobe_current_max: maximum current for the strobe + * @max_strobe_timeout: maximum timeout for the strobe + */ +struct lm3601x_led { + struct mutex lock; + struct regmap *regmap; + struct i2c_client *client; + + struct led_classdev cdev_torch; + struct led_classdev cdev_ir; + + struct led_classdev_flash fled_cdev; + + struct device_node *strobe_node; + struct device_node *torch_node; + struct device_node *ir_node; + + char torch[LED_MAX_NAME_SIZE]; + char strobe[LED_MAX_NAME_SIZE]; + char ir[LED_MAX_NAME_SIZE]; + + unsigned int last_flag; + unsigned int strobe_timeout; + + u32 torch_current_max; + u32 strobe_current_max; + u32 max_strobe_timeout; +}; + +static const struct lm3601x_max_timeouts strobe_timeouts[] = { + { 40, 0x00 }, + { 80, 0x01 }, + { 120, 0x02 }, + { 160, 0x03 }, + { 200, 0x04 }, + { 240, 0x05 }, + { 280, 0x06 }, + { 320, 0x07 }, + { 360, 0x08 }, + { 400, 0x09 }, + { 600, 0x0a }, + { 800, 0x0b }, + { 1000, 0x0c }, + { 1200, 0x0d }, + { 1400, 0x0e }, + { 1600, 0x0f }, +}; + +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 = flags_val; + + return led->last_flag; +} + +static int lm3601x_torch_brightness_set(struct led_classdev *cdev, + enum led_brightness brightness) +{ + struct lm3601x_led *led = + container_of(cdev, struct lm3601x_led, cdev_torch); + int ret; + u8 brightness_val; + + 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_TORCH, LED_OFF); + goto out; + } + + if (brightness == LED_ON) + brightness_val = LED_ON; + else + brightness_val = (brightness/2); + + ret = regmap_write(led->regmap, LM3601X_LED_TORCH_REG, brightness_val); + if (ret < 0) + goto out; + + ret = regmap_update_bits(led->regmap, LM3601X_ENABLE_REG, + LM3601X_MODE_TORCH, + LM3601X_MODE_TORCH); + +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 ret; + int current_timeout; + int reg_count; + int i; + int timeout_reg_val = 0; + + mutex_lock(&led->lock); + + ret = regmap_read(led->regmap, LM3601X_CFG_REG, ¤t_timeout); + if (ret < 0) + goto out; + + reg_count = ARRAY_SIZE(strobe_timeouts); + for (i = 0; i < reg_count; i++) { + if (led->strobe_timeout > strobe_timeouts[i].timeout) + continue; + + if (led->strobe_timeout <= strobe_timeouts[i].timeout) { + timeout_reg_val = (strobe_timeouts[i].reg_val << 1); + break; + } + + ret = -EINVAL; + goto out; + } + + if (led->strobe_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_STROBE, + 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_strobe_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; + u8 brightness_val; + + 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; + } + + if (brightness == LED_ON) + brightness_val = LED_ON; + else + brightness_val = (brightness/2); + + ret = regmap_write(led->regmap, LM3601X_LED_FLASH_REG, brightness_val); + +out: + mutex_unlock(&led->lock); + return ret; +} + +static int lm3601x_strobe_timeout_set(struct led_classdev_flash *fled_cdev, + u32 timeout) +{ + struct lm3601x_led *led = fled_cdev_to_led(fled_cdev); + int ret = 0; + + mutex_lock(&led->lock); + + if (timeout > led->max_strobe_timeout) + ret = -EINVAL; + else + led->strobe_timeout = timeout; + + mutex_unlock(&led->lock); + + return ret; +} + +static int lm3601x_strobe_get(struct led_classdev_flash *fled_cdev, + bool *state) +{ + struct lm3601x_led *led = fled_cdev_to_led(fled_cdev); + int ret; + int strobe_state; + + 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_ir_brightness_set(struct led_classdev *cdev, + enum led_brightness brightness) +{ + struct lm3601x_led *led = + container_of(cdev, struct lm3601x_led, cdev_ir); + int ret; + u8 brightness_val; + + 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_LED_IR, + LED_OFF); + goto out; + } + + if (brightness == LED_ON) + brightness_val = LED_ON; + else + brightness_val = (brightness/2); + + ret = regmap_write(led->regmap, LM3601X_LED_FLASH_REG, brightness_val); + if (ret < 0) + goto out; + + ret = regmap_update_bits(led->regmap, LM3601X_ENABLE_REG, + LM3601X_MODE_IR_DRV, + LM3601X_MODE_IR_DRV); + +out: + mutex_unlock(&led->lock); + return ret; +} + +static int lm3601x_strobe_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 strobe_ops = { + .strobe_set = lm3601x_strobe_set, + .strobe_get = lm3601x_strobe_get, + .timeout_set = lm3601x_strobe_timeout_set, + .fault_get = lm3601x_strobe_fault_get, +}; + +static int lm3601x_register_leds(struct lm3601x_led *led) +{ + struct led_classdev_flash *fled_cdev; + struct led_classdev *led_cdev; + int err = -ENODEV; + + if (led->torch_node) { + led->cdev_torch.name = led->torch; + led->cdev_torch.max_brightness = LED_FULL; + led->cdev_torch.brightness_set_blocking = lm3601x_torch_brightness_set; + err = devm_led_classdev_register(&led->client->dev, + &led->cdev_torch); + if (err < 0) + return err; + } + + if (led->strobe_node) { + fled_cdev = &led->fled_cdev; + fled_cdev->ops = &strobe_ops; + + led_cdev = &fled_cdev->led_cdev; + led_cdev->name = led->strobe; + led_cdev->max_brightness = LED_FULL; + led_cdev->brightness_set_blocking = lm3601x_strobe_brightness_set; + led_cdev->flags |= LED_DEV_CAP_FLASH; + + err = led_classdev_flash_register(&led->client->dev, + fled_cdev); + if (err < 0) + return err; + } + + if (led->ir_node) { + led->cdev_ir.name = led->ir; + led->cdev_ir.max_brightness = LED_FULL; + led->cdev_ir.brightness_set_blocking = + lm3601x_ir_brightness_set; + + err = devm_led_classdev_register(&led->client->dev, + &led->cdev_ir); + } + + return err; +} + +static void lm3601x_init_flash_timeout(struct lm3601x_led *led) +{ + struct led_flash_setting *setting; + + setting = &led->fled_cdev.timeout; + setting->min = strobe_timeouts[0].timeout; + setting->max = led->max_strobe_timeout; + setting->step = 40; + setting->val = led->max_strobe_timeout; +} + +static int lm3601x_parse_node(struct lm3601x_led *led, + struct device_node *node) +{ + struct device_node *child_node; + const char *name; + int ret = -ENODEV; + + for_each_available_child_of_node(node, child_node) { + u32 id = 0; + + of_property_read_u32(child_node, "reg", &id); + + switch (id) { + case LM3601X_LED_TORCH: + led->torch_node = of_node_get(child_node); + break; + case LM3601X_LED_STROBE: + led->strobe_node = of_node_get(child_node); + break; + case LM3601X_LED_IR: + led->ir_node = of_node_get(child_node); + break; + default: + dev_warn(&led->client->dev, + "unknown LED %u encountered, ignoring\n", id); + break; + } + } + + if (led->torch_node) { + ret = of_property_read_string(led->torch_node, "label", &name); + if (!ret) + snprintf(led->torch, sizeof(led->torch), + "%s:%s", led->torch_node->name, name); + else + snprintf(led->torch, sizeof(led->torch), + "%s:torch", led->torch_node->name); + ret = of_property_read_u32(led->torch_node, "led-max-microamp", + &led->torch_current_max); + if (ret < 0) { + led->torch_current_max = LM3601X_MIN_TORCH_I_UA; + dev_warn(&led->client->dev, + "led-max-microamp DT property missing\n"); + } + + of_node_put(led->torch_node); + } + + if (led->strobe_node) { + ret = of_property_read_string(led->strobe_node, "label", &name); + if (!ret) + snprintf(led->strobe, sizeof(led->strobe), + "%s:%s", led->strobe_node->name, name); + else + snprintf(led->strobe, sizeof(led->strobe), + "%s::strobe", led->strobe_node->name); + + ret = of_property_read_u32(led->strobe_node, + "flash-max-microamp", + &led->strobe_current_max); + if (ret < 0) { + led->strobe_current_max = LM3601X_MIN_STROBE_I_MA; + dev_warn(&led->client->dev, + "flash-max-microamp DT property missing\n"); + } + + ret = of_property_read_u32(led->strobe_node, + "flash-max-timeout-us", + &led->max_strobe_timeout); + if (ret < 0) { + led->max_strobe_timeout = strobe_timeouts[0].reg_val; + dev_warn(&led->client->dev, + "flash-max-timeout-us DT property missing\n"); + } + + lm3601x_init_flash_timeout(led); + + of_node_put(led->strobe_node); + } + + if (led->ir_node) { + ret = of_property_read_string(led->ir_node, "label", &name); + if (!ret) + snprintf(led->ir, sizeof(led->ir), + "%s:%s", led->ir_node->name, name); + else + snprintf(led->ir, sizeof(led->ir), + "%s::infrared", led->ir_node->name); + + of_node_put(led->ir_node); + } + + return ret; +} + +static int lm3601x_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct lm3601x_led *led; + int err; + + led = devm_kzalloc(&client->dev, + sizeof(struct lm3601x_led), GFP_KERNEL); + if (!led) + return -ENOMEM; + + err = lm3601x_parse_node(led, client->dev.of_node); + if (err < 0) + return -ENODEV; + + led->client = client; + led->regmap = devm_regmap_init_i2c(client, &lm3601x_regmap); + if (IS_ERR(led->regmap)) { + err = PTR_ERR(led->regmap); + dev_err(&client->dev, + "Failed to allocate register map: %d\n", err); + return err; + } + + mutex_init(&led->lock); + i2c_set_clientdata(client, led); + err = lm3601x_register_leds(led); + + return err; +} + +static int lm3601x_remove(struct i2c_client *client) +{ + struct lm3601x_led *led = i2c_get_clientdata(client); + + regmap_update_bits(led->regmap, LM3601X_ENABLE_REG, + LM3601X_ENABLE_MASK, + LM3601X_MODE_STANDBY); + + return 0; +} + +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 = 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");